1 /* preproc.c macro preprocessor for the Netwide Assembler
3 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4 * Julian Hall. All rights reserved. The software is
5 * redistributable under the license given in the file "LICENSE"
6 * distributed in the NASM archive.
8 * initial version 18/iii/97 by Simon Tatham
11 /* Typical flow of text through preproc
13 * pp_getline gets tokenized lines, either
15 * from a macro expansion
19 * read_line gets raw text from stdmacpos, or predef, or current input file
20 * tokenize converts to tokens
23 * expand_mmac_params is used to expand %1 etc., unless a macro is being
24 * defined or a false conditional is being processed
25 * (%0, %1, %+1, %-1, %%foo
27 * do_directive checks for directives
29 * expand_smacro is used to expand single line macros
31 * expand_mmacro is used to expand multi-line macros
33 * detoken is used to convert the line back to text
56 typedef struct SMacro SMacro
;
57 typedef struct MMacro MMacro
;
58 typedef struct Context Context
;
59 typedef struct Token Token
;
60 typedef struct Blocks Blocks
;
61 typedef struct Line Line
;
62 typedef struct Include Include
;
63 typedef struct Cond Cond
;
64 typedef struct IncPath IncPath
;
67 * Note on the storage of both SMacro and MMacros: the hash table
68 * indexes them case-insensitively, and we then have to go through a
69 * linked list of potential case aliases (and, for MMacros, parameter
70 * ranges); this is to preserve the matching semantics of the earlier
71 * code. If the number of case aliases for a specific macro is a
72 * performance issue, you may want to reconsider your coding style.
76 * Store the definition of a single-line macro.
88 * Store the definition of a multi-line macro. This is also used to
89 * store the interiors of `%rep...%endrep' blocks, which are
90 * effectively self-re-invoking multi-line macros which simply
91 * don't have a name or bother to appear in the hash tables. %rep
92 * blocks are signified by having a NULL `name' field.
94 * In a MMacro describing a `%rep' block, the `in_progress' field
95 * isn't merely boolean, but gives the number of repeats left to
98 * The `next' field is used for storing MMacros in hash tables; the
99 * `next_active' field is for stacking them on istk entries.
101 * When a MMacro is being expanded, `params', `iline', `nparam',
102 * `paramlen', `rotate' and `unique' are local to the invocation.
107 int nparam_min
, nparam_max
;
109 bool plus
; /* is the last parameter greedy? */
110 bool nolist
; /* is this macro listing-inhibited? */
112 Token
*dlist
; /* All defaults as one list */
113 Token
**defaults
; /* Parameter default pointers */
114 int ndefs
; /* number of default parameters */
118 MMacro
*rep_nest
; /* used for nesting %rep */
119 Token
**params
; /* actual parameters */
120 Token
*iline
; /* invocation line */
121 unsigned int nparam
, rotate
;
124 int lineno
; /* Current line number on expansion */
128 * The context stack is composed of a linked list of these.
133 struct hash_table localmac
;
138 * This is the internal form which we break input lines up into.
139 * Typically stored in linked lists.
141 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
142 * necessarily used as-is, but is intended to denote the number of
143 * the substituted parameter. So in the definition
145 * %define a(x,y) ( (x) & ~(y) )
147 * the token representing `x' will have its type changed to
148 * TOK_SMAC_PARAM, but the one representing `y' will be
151 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
152 * which doesn't need quotes around it. Used in the pre-include
153 * mechanism as an alternative to trying to find a sensible type of
154 * quote to use on the filename we were passed.
157 TOK_NONE
= 0, TOK_WHITESPACE
, TOK_COMMENT
, TOK_ID
,
158 TOK_PREPROC_ID
, TOK_STRING
,
159 TOK_NUMBER
, TOK_FLOAT
, TOK_SMAC_END
, TOK_OTHER
,
161 TOK_PREPROC_Q
, TOK_PREPROC_QQ
,
162 TOK_SMAC_PARAM
, /* MUST BE LAST IN THE LIST!!! */
163 TOK_MAX
= INT_MAX
/* Keep compiler from reducing the range */
170 SMacro
*mac
; /* associated macro for TOK_SMAC_END */
171 size_t len
; /* scratch length field */
172 } a
; /* Auxiliary data */
173 enum pp_token_type type
;
177 * Multi-line macro definitions are stored as a linked list of
178 * these, which is essentially a container to allow several linked
181 * Note that in this module, linked lists are treated as stacks
182 * wherever possible. For this reason, Lines are _pushed_ on to the
183 * `expansion' field in MMacro structures, so that the linked list,
184 * if walked, would give the macro lines in reverse order; this
185 * means that we can walk the list when expanding a macro, and thus
186 * push the lines on to the `expansion' field in _istk_ in reverse
187 * order (so that when popped back off they are in the right
188 * order). It may seem cockeyed, and it relies on my design having
189 * an even number of steps in, but it works...
191 * Some of these structures, rather than being actual lines, are
192 * markers delimiting the end of the expansion of a given macro.
193 * This is for use in the cycle-tracking and %rep-handling code.
194 * Such structures have `finishes' non-NULL, and `first' NULL. All
195 * others have `finishes' NULL, but `first' may still be NULL if
205 * To handle an arbitrary level of file inclusion, we maintain a
206 * stack (ie linked list) of these things.
215 MMacro
*mstk
; /* stack of active macros/reps */
219 * Include search path. This is simply a list of strings which get
220 * prepended, in turn, to the name of an include file, in an
221 * attempt to find the file if it's not in the current directory.
229 * Conditional assembly: we maintain a separate stack of these for
230 * each level of file inclusion. (The only reason we keep the
231 * stacks separate is to ensure that a stray `%endif' in a file
232 * included from within the true branch of a `%if' won't terminate
233 * it and cause confusion: instead, rightly, it'll cause an error.)
241 * These states are for use just after %if or %elif: IF_TRUE
242 * means the condition has evaluated to truth so we are
243 * currently emitting, whereas IF_FALSE means we are not
244 * currently emitting but will start doing so if a %else comes
245 * up. In these states, all directives are admissible: %elif,
246 * %else and %endif. (And of course %if.)
248 COND_IF_TRUE
, COND_IF_FALSE
,
250 * These states come up after a %else: ELSE_TRUE means we're
251 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
252 * any %elif or %else will cause an error.
254 COND_ELSE_TRUE
, COND_ELSE_FALSE
,
256 * These states mean that we're not emitting now, and also that
257 * nothing until %endif will be emitted at all. COND_DONE is
258 * used when we've had our moment of emission
259 * and have now started seeing %elifs. COND_NEVER is used when
260 * the condition construct in question is contained within a
261 * non-emitting branch of a larger condition construct,
262 * or if there is an error.
264 COND_DONE
, COND_NEVER
266 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
269 * These defines are used as the possible return values for do_directive
271 #define NO_DIRECTIVE_FOUND 0
272 #define DIRECTIVE_FOUND 1
275 * Condition codes. Note that we use c_ prefix not C_ because C_ is
276 * used in nasm.h for the "real" condition codes. At _this_ level,
277 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
278 * ones, so we need a different enum...
280 static const char * const conditions
[] = {
281 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
282 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
283 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
286 c_A
, c_AE
, c_B
, c_BE
, c_C
, c_CXZ
, c_E
, c_ECXZ
, c_G
, c_GE
, c_L
, c_LE
,
287 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, c_NE
, c_NG
, c_NGE
, c_NL
, c_NLE
, c_NO
,
288 c_NP
, c_NS
, c_NZ
, c_O
, c_P
, c_PE
, c_PO
, c_RCXZ
, c_S
, c_Z
,
291 static const enum pp_conds inverse_ccs
[] = {
292 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, -1, c_NE
, -1, c_NG
, c_NGE
, c_NL
, c_NLE
,
293 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
,
294 c_Z
, c_NO
, c_NP
, c_PO
, c_PE
, -1, c_NS
, c_NZ
300 /* If this is a an IF, ELIF, ELSE or ENDIF keyword */
301 static int is_condition(enum preproc_token arg
)
303 return PP_IS_COND(arg
) || (arg
== PP_ELSE
) || (arg
== PP_ENDIF
);
306 /* For TASM compatibility we need to be able to recognise TASM compatible
307 * conditional compilation directives. Using the NASM pre-processor does
308 * not work, so we look for them specifically from the following list and
309 * then jam in the equivalent NASM directive into the input stream.
313 TM_ARG
, TM_ELIF
, TM_ELSE
, TM_ENDIF
, TM_IF
, TM_IFDEF
, TM_IFDIFI
,
314 TM_IFNDEF
, TM_INCLUDE
, TM_LOCAL
317 static const char * const tasm_directives
[] = {
318 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
319 "ifndef", "include", "local"
322 static int StackSize
= 4;
323 static char *StackPointer
= "ebp";
324 static int ArgOffset
= 8;
325 static int LocalOffset
= 0;
327 static Context
*cstk
;
328 static Include
*istk
;
329 static IncPath
*ipath
= NULL
;
331 static efunc _error
; /* Pointer to client-provided error reporting function */
332 static evalfunc evaluate
;
334 static int pass
; /* HACK: pass 0 = generate dependencies only */
335 static StrList
**dephead
, **deptail
; /* Dependency list */
337 static uint64_t unique
; /* unique identifier numbers */
339 static Line
*predef
= NULL
;
340 static bool do_predef
;
342 static ListGen
*list
;
345 * The current set of multi-line macros we have defined.
347 static struct hash_table mmacros
;
350 * The current set of single-line macros we have defined.
352 static struct hash_table smacros
;
355 * The multi-line macro we are currently defining, or the %rep
356 * block we are currently reading, if any.
358 static MMacro
*defining
;
360 static uint64_t nested_mac_count
;
361 static uint64_t nested_rep_count
;
364 * The number of macro parameters to allocate space for at a time.
366 #define PARAM_DELTA 16
369 * The standard macro set: defined in macros.c in the array nasm_stdmac.
370 * This gives our position in the macro set, when we're processing it.
372 static macros_t
*stdmacpos
;
375 * The extra standard macros that come from the object format, if
378 static macros_t
*extrastdmac
= NULL
;
379 static bool any_extrastdmac
;
382 * Tokens are allocated in blocks to improve speed
384 #define TOKEN_BLOCKSIZE 4096
385 static Token
*freeTokens
= NULL
;
391 static Blocks blocks
= { NULL
, NULL
};
394 * Forward declarations.
396 static Token
*expand_mmac_params(Token
* tline
);
397 static Token
*expand_smacro(Token
* tline
);
398 static Token
*expand_id(Token
* tline
);
399 static Context
*get_ctx(const char *name
, bool all_contexts
);
400 static void make_tok_num(Token
* tok
, int64_t val
);
401 static void error(int severity
, const char *fmt
, ...);
402 static void error_precond(int severity
, const char *fmt
, ...);
403 static void *new_Block(size_t size
);
404 static void delete_Blocks(void);
405 static Token
*new_Token(Token
* next
, enum pp_token_type type
,
406 const char *text
, int txtlen
);
407 static Token
*delete_Token(Token
* t
);
410 * Macros for safe checking of token pointers, avoid *(NULL)
412 #define tok_type_(x,t) ((x) && (x)->type == (t))
413 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
414 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
415 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
417 /* Handle TASM specific directives, which do not contain a % in
418 * front of them. We do it here because I could not find any other
419 * place to do it for the moment, and it is a hack (ideally it would
420 * be nice to be able to use the NASM pre-processor to do it).
422 static char *check_tasm_directive(char *line
)
424 int32_t i
, j
, k
, m
, len
;
425 char *p
= line
, *oldline
, oldchar
;
427 /* Skip whitespace */
428 while (nasm_isspace(*p
) && *p
!= 0)
431 /* Binary search for the directive name */
433 j
= elements(tasm_directives
);
435 while (!nasm_isspace(p
[len
]) && p
[len
] != 0)
442 m
= nasm_stricmp(p
, tasm_directives
[k
]);
444 /* We have found a directive, so jam a % in front of it
445 * so that NASM will then recognise it as one if it's own.
450 line
= nasm_malloc(len
+ 2);
452 if (k
== TM_IFDIFI
) {
453 /* NASM does not recognise IFDIFI, so we convert it to
454 * %ifdef BOGUS. This is not used in NASM comaptible
455 * code, but does need to parse for the TASM macro
458 strcpy(line
+ 1, "ifdef BOGUS");
460 memcpy(line
+ 1, p
, len
+ 1);
475 * The pre-preprocessing stage... This function translates line
476 * number indications as they emerge from GNU cpp (`# lineno "file"
477 * flags') into NASM preprocessor line number indications (`%line
480 static char *prepreproc(char *line
)
483 char *fname
, *oldline
;
485 if (line
[0] == '#' && line
[1] == ' ') {
488 lineno
= atoi(fname
);
489 fname
+= strspn(fname
, "0123456789 ");
492 fnlen
= strcspn(fname
, "\"");
493 line
= nasm_malloc(20 + fnlen
);
494 snprintf(line
, 20 + fnlen
, "%%line %d %.*s", lineno
, fnlen
, fname
);
497 if (tasm_compatible_mode
)
498 return check_tasm_directive(line
);
503 * Free a linked list of tokens.
505 static void free_tlist(Token
* list
)
508 list
= delete_Token(list
);
513 * Free a linked list of lines.
515 static void free_llist(Line
* list
)
521 free_tlist(l
->first
);
529 static void free_mmacro(MMacro
* m
)
532 free_tlist(m
->dlist
);
533 nasm_free(m
->defaults
);
534 free_llist(m
->expansion
);
539 * Free all currently defined macros, and free the hash tables
541 static void free_smacro_table(struct hash_table
*smt
)
545 struct hash_tbl_node
*it
= NULL
;
547 while ((s
= hash_iterate(smt
, &it
, &key
)) != NULL
) {
548 nasm_free((void *)key
);
550 SMacro
*ns
= s
->next
;
552 free_tlist(s
->expansion
);
560 static void free_mmacro_table(struct hash_table
*mmt
)
564 struct hash_tbl_node
*it
= NULL
;
567 while ((m
= hash_iterate(mmt
, &it
, &key
)) != NULL
) {
568 nasm_free((void *)key
);
570 MMacro
*nm
= m
->next
;
578 static void free_macros(void)
580 free_smacro_table(&smacros
);
581 free_mmacro_table(&mmacros
);
585 * Initialize the hash tables
587 static void init_macros(void)
589 hash_init(&smacros
, HASH_LARGE
);
590 hash_init(&mmacros
, HASH_LARGE
);
594 * Pop the context stack.
596 static void ctx_pop(void)
601 free_smacro_table(&c
->localmac
);
607 * Search for a key in the hash index; adding it if necessary
608 * (in which case we initialize the data pointer to NULL.)
611 hash_findi_add(struct hash_table
*hash
, const char *str
)
613 struct hash_insert hi
;
617 r
= hash_findi(hash
, str
, &hi
);
621 strx
= nasm_strdup(str
); /* Use a more efficient allocator here? */
622 return hash_add(&hi
, strx
, NULL
);
626 * Like hash_findi, but returns the data element rather than a pointer
627 * to it. Used only when not adding a new element, hence no third
631 hash_findix(struct hash_table
*hash
, const char *str
)
635 p
= hash_findi(hash
, str
, NULL
);
636 return p
? *p
: NULL
;
639 #define BUF_DELTA 512
641 * Read a line from the top file in istk, handling multiple CR/LFs
642 * at the end of the line read, and handling spurious ^Zs. Will
643 * return lines from the standard macro set if this has not already
646 static char *read_line(void)
648 char *buffer
, *p
, *q
;
649 int bufsize
, continued_count
;
653 const unsigned char *p
= stdmacpos
;
658 len
+= pp_directives_len
[c
-0x80]+1;
662 ret
= nasm_malloc(len
+1);
664 while ((c
= *stdmacpos
++)) {
666 memcpy(q
, pp_directives
[c
-0x80], pp_directives_len
[c
-0x80]);
667 q
+= pp_directives_len
[c
-0x80];
677 /* This was the last of the standard macro chain... */
679 if (any_extrastdmac
) {
680 stdmacpos
= extrastdmac
;
681 any_extrastdmac
= false;
682 } else if (do_predef
) {
684 Token
*head
, **tail
, *t
;
687 * Nasty hack: here we push the contents of
688 * `predef' on to the top-level expansion stack,
689 * since this is the most convenient way to
690 * implement the pre-include and pre-define
693 for (pd
= predef
; pd
; pd
= pd
->next
) {
696 for (t
= pd
->first
; t
; t
= t
->next
) {
697 *tail
= new_Token(NULL
, t
->type
, t
->text
, 0);
698 tail
= &(*tail
)->next
;
700 l
= nasm_malloc(sizeof(Line
));
701 l
->next
= istk
->expansion
;
713 buffer
= nasm_malloc(BUF_DELTA
);
717 q
= fgets(p
, bufsize
- (p
- buffer
), istk
->fp
);
721 if (p
> buffer
&& p
[-1] == '\n') {
722 /* Convert backslash-CRLF line continuation sequences into
723 nothing at all (for DOS and Windows) */
724 if (((p
- 2) > buffer
) && (p
[-3] == '\\') && (p
[-2] == '\r')) {
729 /* Also convert backslash-LF line continuation sequences into
730 nothing at all (for Unix) */
731 else if (((p
- 1) > buffer
) && (p
[-2] == '\\')) {
739 if (p
- buffer
> bufsize
- 10) {
740 int32_t offset
= p
- buffer
;
741 bufsize
+= BUF_DELTA
;
742 buffer
= nasm_realloc(buffer
, bufsize
);
743 p
= buffer
+ offset
; /* prevent stale-pointer problems */
747 if (!q
&& p
== buffer
) {
752 src_set_linnum(src_get_linnum() + istk
->lineinc
+
753 (continued_count
* istk
->lineinc
));
756 * Play safe: remove CRs as well as LFs, if any of either are
757 * present at the end of the line.
759 while (--p
>= buffer
&& (*p
== '\n' || *p
== '\r'))
763 * Handle spurious ^Z, which may be inserted into source files
764 * by some file transfer utilities.
766 buffer
[strcspn(buffer
, "\032")] = '\0';
768 list
->line(LIST_READ
, buffer
);
774 * Tokenize a line of text. This is a very simple process since we
775 * don't need to parse the value out of e.g. numeric tokens: we
776 * simply split one string into many.
778 static Token
*tokenize(char *line
)
781 enum pp_token_type type
;
783 Token
*t
, **tail
= &list
;
789 if (nasm_isdigit(*p
) ||
790 ((*p
== '-' || *p
== '+') && nasm_isdigit(p
[1])) ||
791 ((*p
== '+') && (nasm_isspace(p
[1]) || !p
[1]))) {
795 while (nasm_isdigit(*p
));
796 type
= TOK_PREPROC_ID
;
797 } else if (*p
== '{') {
799 while (*p
&& *p
!= '}') {
806 type
= TOK_PREPROC_ID
;
807 } else if (*p
== '?') {
808 type
= TOK_PREPROC_Q
; /* %? */
811 type
= TOK_PREPROC_QQ
; /* %?? */
814 } else if (isidchar(*p
) ||
815 ((*p
== '!' || *p
== '%' || *p
== '$') &&
820 while (isidchar(*p
));
821 type
= TOK_PREPROC_ID
;
827 } else if (isidstart(*p
) || (*p
== '$' && isidstart(p
[1]))) {
830 while (*p
&& isidchar(*p
))
832 } else if (*p
== '\'' || *p
== '"' || *p
== '`') {
837 p
= nasm_skip_string(p
);
842 error(ERR_WARNING
, "unterminated string");
843 /* Handling unterminated strings by UNV */
846 } else if (isnumstart(*p
)) {
848 bool is_float
= false;
864 if (!is_hex
&& (c
== 'e' || c
== 'E')) {
866 if (*p
== '+' || *p
== '-') {
867 /* e can only be followed by +/- if it is either a
868 prefixed hex number or a floating-point number */
872 } else if (c
== 'H' || c
== 'h' || c
== 'X' || c
== 'x') {
874 } else if (c
== 'P' || c
== 'p') {
876 if (*p
== '+' || *p
== '-')
878 } else if (isnumchar(c
) || c
== '_')
881 /* we need to deal with consequences of the legacy
882 parser, like "1.nolist" being two tokens
883 (TOK_NUMBER, TOK_ID) here; at least give it
884 a shot for now. In the future, we probably need
885 a flex-based scanner with proper pattern matching
886 to do it as well as it can be done. Nothing in
887 the world is going to help the person who wants
888 0x123.p16 interpreted as two tokens, though. */
893 if (nasm_isdigit(*r
) || (is_hex
&& nasm_isxdigit(*r
)) ||
894 (!is_hex
&& (*r
== 'e' || *r
== 'E')) ||
895 (*r
== 'p' || *r
== 'P')) {
899 break; /* Terminate the token */
903 p
--; /* Point to first character beyond number */
905 if (has_e
&& !is_hex
) {
906 /* 1e13 is floating-point, but 1e13h is not */
910 type
= is_float
? TOK_FLOAT
: TOK_NUMBER
;
911 } else if (nasm_isspace(*p
)) {
912 type
= TOK_WHITESPACE
;
914 while (*p
&& nasm_isspace(*p
))
917 * Whitespace just before end-of-line is discarded by
918 * pretending it's a comment; whitespace just before a
919 * comment gets lumped into the comment.
921 if (!*p
|| *p
== ';') {
926 } else if (*p
== ';') {
932 * Anything else is an operator of some kind. We check
933 * for all the double-character operators (>>, <<, //,
934 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
935 * else is a single-character operator.
938 if ((p
[0] == '>' && p
[1] == '>') ||
939 (p
[0] == '<' && p
[1] == '<') ||
940 (p
[0] == '/' && p
[1] == '/') ||
941 (p
[0] == '<' && p
[1] == '=') ||
942 (p
[0] == '>' && p
[1] == '=') ||
943 (p
[0] == '=' && p
[1] == '=') ||
944 (p
[0] == '!' && p
[1] == '=') ||
945 (p
[0] == '<' && p
[1] == '>') ||
946 (p
[0] == '&' && p
[1] == '&') ||
947 (p
[0] == '|' && p
[1] == '|') ||
948 (p
[0] == '^' && p
[1] == '^')) {
954 /* Handling unterminated string by UNV */
957 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
958 t->text[p-line] = *line;
962 if (type
!= TOK_COMMENT
) {
963 *tail
= t
= new_Token(NULL
, type
, line
, p
- line
);
972 * this function allocates a new managed block of memory and
973 * returns a pointer to the block. The managed blocks are
974 * deleted only all at once by the delete_Blocks function.
976 static void *new_Block(size_t size
)
980 /* first, get to the end of the linked list */
983 /* now allocate the requested chunk */
984 b
->chunk
= nasm_malloc(size
);
986 /* now allocate a new block for the next request */
987 b
->next
= nasm_malloc(sizeof(Blocks
));
988 /* and initialize the contents of the new block */
989 b
->next
->next
= NULL
;
990 b
->next
->chunk
= NULL
;
995 * this function deletes all managed blocks of memory
997 static void delete_Blocks(void)
999 Blocks
*a
, *b
= &blocks
;
1002 * keep in mind that the first block, pointed to by blocks
1003 * is a static and not dynamically allocated, so we don't
1008 nasm_free(b
->chunk
);
1017 * this function creates a new Token and passes a pointer to it
1018 * back to the caller. It sets the type and text elements, and
1019 * also the a.mac and next elements to NULL.
1021 static Token
*new_Token(Token
* next
, enum pp_token_type type
,
1022 const char *text
, int txtlen
)
1027 if (freeTokens
== NULL
) {
1028 freeTokens
= (Token
*) new_Block(TOKEN_BLOCKSIZE
* sizeof(Token
));
1029 for (i
= 0; i
< TOKEN_BLOCKSIZE
- 1; i
++)
1030 freeTokens
[i
].next
= &freeTokens
[i
+ 1];
1031 freeTokens
[i
].next
= NULL
;
1034 freeTokens
= t
->next
;
1038 if (type
== TOK_WHITESPACE
|| text
== NULL
) {
1042 txtlen
= strlen(text
);
1043 t
->text
= nasm_malloc(txtlen
+1);
1044 memcpy(t
->text
, text
, txtlen
);
1045 t
->text
[txtlen
] = '\0';
1050 static Token
*delete_Token(Token
* t
)
1052 Token
*next
= t
->next
;
1054 t
->next
= freeTokens
;
1060 * Convert a line of tokens back into text.
1061 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1062 * will be transformed into ..@ctxnum.xxx
1064 static char *detoken(Token
* tlist
, bool expand_locals
)
1072 for (t
= tlist
; t
; t
= t
->next
) {
1073 if (t
->type
== TOK_PREPROC_ID
&& t
->text
[1] == '!') {
1074 char *p
= getenv(t
->text
+ 2);
1077 t
->text
= nasm_strdup(p
);
1081 /* Expand local macros here and not during preprocessing */
1082 if (expand_locals
&&
1083 t
->type
== TOK_PREPROC_ID
&& t
->text
&&
1084 t
->text
[0] == '%' && t
->text
[1] == '$') {
1085 Context
*ctx
= get_ctx(t
->text
, false);
1088 char *p
, *q
= t
->text
+ 2;
1090 q
+= strspn(q
, "$");
1091 snprintf(buffer
, sizeof(buffer
), "..@%"PRIu32
".", ctx
->number
);
1092 p
= nasm_strcat(buffer
, q
);
1097 if (t
->type
== TOK_WHITESPACE
) {
1099 } else if (t
->text
) {
1100 len
+= strlen(t
->text
);
1103 p
= line
= nasm_malloc(len
+ 1);
1104 for (t
= tlist
; t
; t
= t
->next
) {
1105 if (t
->type
== TOK_WHITESPACE
) {
1107 } else if (t
->text
) {
1118 * A scanner, suitable for use by the expression evaluator, which
1119 * operates on a line of Tokens. Expects a pointer to a pointer to
1120 * the first token in the line to be passed in as its private_data
1123 * FIX: This really needs to be unified with stdscan.
1125 static int ppscan(void *private_data
, struct tokenval
*tokval
)
1127 Token
**tlineptr
= private_data
;
1129 char ourcopy
[MAX_KEYWORD
+1], *p
, *r
, *s
;
1133 *tlineptr
= tline
? tline
->next
: NULL
;
1135 while (tline
&& (tline
->type
== TOK_WHITESPACE
||
1136 tline
->type
== TOK_COMMENT
));
1139 return tokval
->t_type
= TOKEN_EOS
;
1141 tokval
->t_charptr
= tline
->text
;
1143 if (tline
->text
[0] == '$' && !tline
->text
[1])
1144 return tokval
->t_type
= TOKEN_HERE
;
1145 if (tline
->text
[0] == '$' && tline
->text
[1] == '$' && !tline
->text
[2])
1146 return tokval
->t_type
= TOKEN_BASE
;
1148 if (tline
->type
== TOK_ID
) {
1149 p
= tokval
->t_charptr
= tline
->text
;
1151 tokval
->t_charptr
++;
1152 return tokval
->t_type
= TOKEN_ID
;
1155 for (r
= p
, s
= ourcopy
; *r
; r
++) {
1156 if (r
>= p
+MAX_KEYWORD
)
1157 return tokval
->t_type
= TOKEN_ID
; /* Not a keyword */
1158 *s
++ = nasm_tolower(*r
);
1161 /* right, so we have an identifier sitting in temp storage. now,
1162 * is it actually a register or instruction name, or what? */
1163 return nasm_token_hash(ourcopy
, tokval
);
1166 if (tline
->type
== TOK_NUMBER
) {
1168 tokval
->t_integer
= readnum(tline
->text
, &rn_error
);
1169 tokval
->t_charptr
= tline
->text
;
1171 return tokval
->t_type
= TOKEN_ERRNUM
;
1173 return tokval
->t_type
= TOKEN_NUM
;
1176 if (tline
->type
== TOK_FLOAT
) {
1177 return tokval
->t_type
= TOKEN_FLOAT
;
1180 if (tline
->type
== TOK_STRING
) {
1183 bq
= tline
->text
[0];
1184 tokval
->t_charptr
= tline
->text
;
1185 tokval
->t_inttwo
= nasm_unquote(tline
->text
, &ep
);
1187 if (ep
[0] != bq
|| ep
[1] != '\0')
1188 return tokval
->t_type
= TOKEN_ERRSTR
;
1190 return tokval
->t_type
= TOKEN_STR
;
1193 if (tline
->type
== TOK_OTHER
) {
1194 if (!strcmp(tline
->text
, "<<"))
1195 return tokval
->t_type
= TOKEN_SHL
;
1196 if (!strcmp(tline
->text
, ">>"))
1197 return tokval
->t_type
= TOKEN_SHR
;
1198 if (!strcmp(tline
->text
, "//"))
1199 return tokval
->t_type
= TOKEN_SDIV
;
1200 if (!strcmp(tline
->text
, "%%"))
1201 return tokval
->t_type
= TOKEN_SMOD
;
1202 if (!strcmp(tline
->text
, "=="))
1203 return tokval
->t_type
= TOKEN_EQ
;
1204 if (!strcmp(tline
->text
, "<>"))
1205 return tokval
->t_type
= TOKEN_NE
;
1206 if (!strcmp(tline
->text
, "!="))
1207 return tokval
->t_type
= TOKEN_NE
;
1208 if (!strcmp(tline
->text
, "<="))
1209 return tokval
->t_type
= TOKEN_LE
;
1210 if (!strcmp(tline
->text
, ">="))
1211 return tokval
->t_type
= TOKEN_GE
;
1212 if (!strcmp(tline
->text
, "&&"))
1213 return tokval
->t_type
= TOKEN_DBL_AND
;
1214 if (!strcmp(tline
->text
, "^^"))
1215 return tokval
->t_type
= TOKEN_DBL_XOR
;
1216 if (!strcmp(tline
->text
, "||"))
1217 return tokval
->t_type
= TOKEN_DBL_OR
;
1221 * We have no other options: just return the first character of
1224 return tokval
->t_type
= tline
->text
[0];
1228 * Compare a string to the name of an existing macro; this is a
1229 * simple wrapper which calls either strcmp or nasm_stricmp
1230 * depending on the value of the `casesense' parameter.
1232 static int mstrcmp(const char *p
, const char *q
, bool casesense
)
1234 return casesense
? strcmp(p
, q
) : nasm_stricmp(p
, q
);
1238 * Compare a string to the name of an existing macro; this is a
1239 * simple wrapper which calls either strcmp or nasm_stricmp
1240 * depending on the value of the `casesense' parameter.
1242 static int mmemcmp(const char *p
, const char *q
, size_t l
, bool casesense
)
1244 return casesense
? memcmp(p
, q
, l
) : nasm_memicmp(p
, q
, l
);
1248 * Return the Context structure associated with a %$ token. Return
1249 * NULL, having _already_ reported an error condition, if the
1250 * context stack isn't deep enough for the supplied number of $
1252 * If all_contexts == true, contexts that enclose current are
1253 * also scanned for such smacro, until it is found; if not -
1254 * only the context that directly results from the number of $'s
1255 * in variable's name.
1257 static Context
*get_ctx(const char *name
, bool all_contexts
)
1263 if (!name
|| name
[0] != '%' || name
[1] != '$')
1267 error(ERR_NONFATAL
, "`%s': context stack is empty", name
);
1271 for (i
= strspn(name
+ 2, "$"), ctx
= cstk
; (i
> 0) && ctx
; i
--) {
1273 /* i--; Lino - 02/25/02 */
1276 error(ERR_NONFATAL
, "`%s': context stack is only"
1277 " %d level%s deep", name
, i
- 1, (i
== 2 ? "" : "s"));
1284 /* Search for this smacro in found context */
1285 m
= hash_findix(&ctx
->localmac
, name
);
1287 if (!mstrcmp(m
->name
, name
, m
->casesense
))
1298 * Check to see if a file is already in a string list
1300 static bool in_list(const StrList
*list
, const char *str
)
1303 if (!strcmp(list
->str
, str
))
1311 * Open an include file. This routine must always return a valid
1312 * file pointer if it returns - it's responsible for throwing an
1313 * ERR_FATAL and bombing out completely if not. It should also try
1314 * the include path one by one until it finds the file or reaches
1315 * the end of the path.
1317 static FILE *inc_fopen(const char *file
, StrList
**dhead
, StrList
***dtail
,
1322 IncPath
*ip
= ipath
;
1323 int len
= strlen(file
);
1324 size_t prefix_len
= 0;
1328 sl
= nasm_malloc(prefix_len
+len
+1+sizeof sl
->next
);
1329 memcpy(sl
->str
, prefix
, prefix_len
);
1330 memcpy(sl
->str
+prefix_len
, file
, len
+1);
1331 fp
= fopen(sl
->str
, "r");
1332 if (fp
&& dhead
&& !in_list(*dhead
, sl
->str
)) {
1350 prefix_len
= strlen(prefix
);
1352 /* -MG given and file not found */
1353 if (dhead
&& !in_list(*dhead
, file
)) {
1354 sl
= nasm_malloc(len
+1+sizeof sl
->next
);
1356 strcpy(sl
->str
, file
);
1364 error(ERR_FATAL
, "unable to open include file `%s'", file
);
1365 return NULL
; /* never reached - placate compilers */
1369 * Determine if we should warn on defining a single-line macro of
1370 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1371 * return true if _any_ single-line macro of that name is defined.
1372 * Otherwise, will return true if a single-line macro with either
1373 * `nparam' or no parameters is defined.
1375 * If a macro with precisely the right number of parameters is
1376 * defined, or nparam is -1, the address of the definition structure
1377 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1378 * is NULL, no action will be taken regarding its contents, and no
1381 * Note that this is also called with nparam zero to resolve
1384 * If you already know which context macro belongs to, you can pass
1385 * the context pointer as first parameter; if you won't but name begins
1386 * with %$ the context will be automatically computed. If all_contexts
1387 * is true, macro will be searched in outer contexts as well.
1390 smacro_defined(Context
* ctx
, const char *name
, int nparam
, SMacro
** defn
,
1393 struct hash_table
*smtbl
;
1397 smtbl
= &ctx
->localmac
;
1398 } else if (name
[0] == '%' && name
[1] == '$') {
1400 ctx
= get_ctx(name
, false);
1402 return false; /* got to return _something_ */
1403 smtbl
= &ctx
->localmac
;
1407 m
= (SMacro
*) hash_findix(smtbl
, name
);
1410 if (!mstrcmp(m
->name
, name
, m
->casesense
&& nocase
) &&
1411 (nparam
<= 0 || m
->nparam
== 0 || nparam
== (int) m
->nparam
)) {
1413 if (nparam
== (int) m
->nparam
|| nparam
== -1)
1427 * Count and mark off the parameters in a multi-line macro call.
1428 * This is called both from within the multi-line macro expansion
1429 * code, and also to mark off the default parameters when provided
1430 * in a %macro definition line.
1432 static void count_mmac_params(Token
* t
, int *nparam
, Token
*** params
)
1434 int paramsize
, brace
;
1436 *nparam
= paramsize
= 0;
1439 /* +1: we need space for the final NULL */
1440 if (*nparam
+1 >= paramsize
) {
1441 paramsize
+= PARAM_DELTA
;
1442 *params
= nasm_realloc(*params
, sizeof(**params
) * paramsize
);
1446 if (tok_is_(t
, "{"))
1448 (*params
)[(*nparam
)++] = t
;
1449 while (tok_isnt_(t
, brace
? "}" : ","))
1451 if (t
) { /* got a comma/brace */
1455 * Now we've found the closing brace, look further
1459 if (tok_isnt_(t
, ",")) {
1461 "braces do not enclose all of macro parameter");
1462 while (tok_isnt_(t
, ","))
1466 t
= t
->next
; /* eat the comma */
1473 * Determine whether one of the various `if' conditions is true or
1476 * We must free the tline we get passed.
1478 static bool if_condition(Token
* tline
, enum preproc_token ct
)
1480 enum pp_conditional i
= PP_COND(ct
);
1482 Token
*t
, *tt
, **tptr
, *origline
;
1483 struct tokenval tokval
;
1485 enum pp_token_type needtype
;
1491 j
= false; /* have we matched yet? */
1496 if (tline
->type
!= TOK_ID
) {
1498 "`%s' expects context identifiers", pp_directives
[ct
]);
1499 free_tlist(origline
);
1502 if (cstk
&& cstk
->name
&& !nasm_stricmp(tline
->text
, cstk
->name
))
1504 tline
= tline
->next
;
1509 j
= false; /* have we matched yet? */
1512 if (!tline
|| (tline
->type
!= TOK_ID
&&
1513 (tline
->type
!= TOK_PREPROC_ID
||
1514 tline
->text
[1] != '$'))) {
1516 "`%s' expects macro identifiers", pp_directives
[ct
]);
1519 if (smacro_defined(NULL
, tline
->text
, 0, NULL
, true))
1521 tline
= tline
->next
;
1527 tline
= expand_smacro(tline
);
1529 while (tok_isnt_(tt
, ","))
1533 "`%s' expects two comma-separated arguments",
1538 j
= true; /* assume equality unless proved not */
1539 while ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) && tt
) {
1540 if (tt
->type
== TOK_OTHER
&& !strcmp(tt
->text
, ",")) {
1541 error(ERR_NONFATAL
, "`%s': more than one comma on line",
1545 if (t
->type
== TOK_WHITESPACE
) {
1549 if (tt
->type
== TOK_WHITESPACE
) {
1553 if (tt
->type
!= t
->type
) {
1554 j
= false; /* found mismatching tokens */
1557 /* When comparing strings, need to unquote them first */
1558 if (t
->type
== TOK_STRING
) {
1559 size_t l1
= nasm_unquote(t
->text
, NULL
);
1560 size_t l2
= nasm_unquote(tt
->text
, NULL
);
1566 if (mmemcmp(t
->text
, tt
->text
, l1
, i
== PPC_IFIDN
)) {
1570 } else if (mstrcmp(tt
->text
, t
->text
, i
== PPC_IFIDN
) != 0) {
1571 j
= false; /* found mismatching tokens */
1578 if ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) || tt
)
1579 j
= false; /* trailing gunk on one end or other */
1585 MMacro searching
, *mmac
;
1587 tline
= tline
->next
;
1589 tline
= expand_id(tline
);
1590 if (!tok_type_(tline
, TOK_ID
)) {
1592 "`%s' expects a macro name", pp_directives
[ct
]);
1595 searching
.name
= nasm_strdup(tline
->text
);
1596 searching
.casesense
= true;
1597 searching
.plus
= false;
1598 searching
.nolist
= false;
1599 searching
.in_progress
= 0;
1600 searching
.rep_nest
= NULL
;
1601 searching
.nparam_min
= 0;
1602 searching
.nparam_max
= INT_MAX
;
1603 tline
= expand_smacro(tline
->next
);
1606 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
1608 "`%s' expects a parameter count or nothing",
1611 searching
.nparam_min
= searching
.nparam_max
=
1612 readnum(tline
->text
, &j
);
1615 "unable to parse parameter count `%s'",
1618 if (tline
&& tok_is_(tline
->next
, "-")) {
1619 tline
= tline
->next
->next
;
1620 if (tok_is_(tline
, "*"))
1621 searching
.nparam_max
= INT_MAX
;
1622 else if (!tok_type_(tline
, TOK_NUMBER
))
1624 "`%s' expects a parameter count after `-'",
1627 searching
.nparam_max
= readnum(tline
->text
, &j
);
1630 "unable to parse parameter count `%s'",
1632 if (searching
.nparam_min
> searching
.nparam_max
)
1634 "minimum parameter count exceeds maximum");
1637 if (tline
&& tok_is_(tline
->next
, "+")) {
1638 tline
= tline
->next
;
1639 searching
.plus
= true;
1641 mmac
= (MMacro
*) hash_findix(&mmacros
, searching
.name
);
1643 if (!strcmp(mmac
->name
, searching
.name
) &&
1644 (mmac
->nparam_min
<= searching
.nparam_max
1646 && (searching
.nparam_min
<= mmac
->nparam_max
1653 if(tline
&& tline
->next
)
1654 error(ERR_WARNING
, "trailing garbage after %%ifmacro ignored");
1655 nasm_free(searching
.name
);
1664 needtype
= TOK_NUMBER
;
1667 needtype
= TOK_STRING
;
1671 t
= tline
= expand_smacro(tline
);
1673 while (tok_type_(t
, TOK_WHITESPACE
) ||
1674 (needtype
== TOK_NUMBER
&&
1675 tok_type_(t
, TOK_OTHER
) &&
1676 (t
->text
[0] == '-' || t
->text
[0] == '+') &&
1680 j
= tok_type_(t
, needtype
);
1684 t
= tline
= expand_smacro(tline
);
1685 while (tok_type_(t
, TOK_WHITESPACE
))
1690 t
= t
->next
; /* Skip the actual token */
1691 while (tok_type_(t
, TOK_WHITESPACE
))
1693 j
= !t
; /* Should be nothing left */
1698 t
= tline
= expand_smacro(tline
);
1699 while (tok_type_(t
, TOK_WHITESPACE
))
1702 j
= !t
; /* Should be empty */
1706 t
= tline
= expand_smacro(tline
);
1708 tokval
.t_type
= TOKEN_INVALID
;
1709 evalresult
= evaluate(ppscan
, tptr
, &tokval
,
1710 NULL
, pass
| CRITICAL
, error
, NULL
);
1715 "trailing garbage after expression ignored");
1716 if (!is_simple(evalresult
)) {
1718 "non-constant value given to `%s'", pp_directives
[ct
]);
1721 j
= reloc_value(evalresult
) != 0;
1726 "preprocessor directive `%s' not yet implemented",
1731 free_tlist(origline
);
1732 return j
^ PP_NEGATIVE(ct
);
1735 free_tlist(origline
);
1740 * Common code for defining an smacro
1742 static bool define_smacro(Context
*ctx
, char *mname
, bool casesense
,
1743 int nparam
, Token
*expansion
)
1745 SMacro
*smac
, **smhead
;
1746 struct hash_table
*smtbl
;
1748 if (smacro_defined(ctx
, mname
, nparam
, &smac
, casesense
)) {
1751 "single-line macro `%s' defined both with and"
1752 " without parameters", mname
);
1754 /* Some instances of the old code considered this a failure,
1755 some others didn't. What is the right thing to do here? */
1756 free_tlist(expansion
);
1757 return false; /* Failure */
1760 * We're redefining, so we have to take over an
1761 * existing SMacro structure. This means freeing
1762 * what was already in it.
1764 nasm_free(smac
->name
);
1765 free_tlist(smac
->expansion
);
1768 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
1769 smhead
= (SMacro
**) hash_findi_add(smtbl
, mname
);
1770 smac
= nasm_malloc(sizeof(SMacro
));
1771 smac
->next
= *smhead
;
1774 smac
->name
= nasm_strdup(mname
);
1775 smac
->casesense
= casesense
;
1776 smac
->nparam
= nparam
;
1777 smac
->expansion
= expansion
;
1778 smac
->in_progress
= false;
1779 return true; /* Success */
1783 * Undefine an smacro
1785 static void undef_smacro(Context
*ctx
, const char *mname
)
1787 SMacro
**smhead
, *s
, **sp
;
1788 struct hash_table
*smtbl
;
1790 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
1791 smhead
= (SMacro
**)hash_findi(smtbl
, mname
, NULL
);
1795 * We now have a macro name... go hunt for it.
1798 while ((s
= *sp
) != NULL
) {
1799 if (!mstrcmp(s
->name
, mname
, s
->casesense
)) {
1802 free_tlist(s
->expansion
);
1812 * Parse a mmacro specification.
1814 static bool parse_mmacro_spec(Token
*tline
, MMacro
*def
, const char *directive
)
1818 tline
= tline
->next
;
1820 tline
= expand_id(tline
);
1821 if (!tok_type_(tline
, TOK_ID
)) {
1822 error(ERR_NONFATAL
, "`%s' expects a macro name", directive
);
1826 def
->name
= nasm_strdup(tline
->text
);
1828 def
->nolist
= false;
1829 def
->in_progress
= 0;
1830 def
->rep_nest
= NULL
;
1831 def
->nparam_min
= 0;
1832 def
->nparam_max
= 0;
1834 tline
= expand_smacro(tline
->next
);
1836 if (!tok_type_(tline
, TOK_NUMBER
)) {
1837 error(ERR_NONFATAL
, "`%s' expects a parameter count", directive
);
1839 def
->nparam_min
= def
->nparam_max
=
1840 readnum(tline
->text
, &err
);
1843 "unable to parse parameter count `%s'", tline
->text
);
1845 if (tline
&& tok_is_(tline
->next
, "-")) {
1846 tline
= tline
->next
->next
;
1847 if (tok_is_(tline
, "*")) {
1848 def
->nparam_max
= INT_MAX
;
1849 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
1851 "`%s' expects a parameter count after `-'", directive
);
1853 def
->nparam_max
= readnum(tline
->text
, &err
);
1855 error(ERR_NONFATAL
, "unable to parse parameter count `%s'",
1858 if (def
->nparam_min
> def
->nparam_max
) {
1859 error(ERR_NONFATAL
, "minimum parameter count exceeds maximum");
1863 if (tline
&& tok_is_(tline
->next
, "+")) {
1864 tline
= tline
->next
;
1867 if (tline
&& tok_type_(tline
->next
, TOK_ID
) &&
1868 !nasm_stricmp(tline
->next
->text
, ".nolist")) {
1869 tline
= tline
->next
;
1874 * Handle default parameters.
1876 if (tline
&& tline
->next
) {
1877 def
->dlist
= tline
->next
;
1879 count_mmac_params(def
->dlist
, &def
->ndefs
, &def
->defaults
);
1882 def
->defaults
= NULL
;
1884 def
->expansion
= NULL
;
1887 def
->ndefs
> def
->nparam_max
- def
->nparam_min
&&
1889 error(ERR_WARNING
| ERR_WARN_MDP
, "too many default macro parameters");
1896 * Decode a size directive
1898 static int parse_size(const char *str
) {
1899 static const char *size_names
[] =
1900 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
1901 static const int sizes
[] =
1902 { 0, 1, 4, 16, 8, 10, 2, 32 };
1904 return sizes
[bsii(str
, size_names
, elements(size_names
))+1];
1908 * find and process preprocessor directive in passed line
1909 * Find out if a line contains a preprocessor directive, and deal
1912 * If a directive _is_ found, it is the responsibility of this routine
1913 * (and not the caller) to free_tlist() the line.
1915 * @param tline a pointer to the current tokeninzed line linked list
1916 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
1919 static int do_directive(Token
* tline
)
1921 enum preproc_token i
;
1929 char *p
, *pp
, *mname
;
1933 MMacro
*mmac
, **mmhead
;
1934 Token
*t
, *tt
, *param_start
, *macro_start
, *last
, **tptr
, *origline
;
1936 struct tokenval tokval
;
1938 MMacro
*tmp_defining
; /* Used when manipulating rep_nest */
1946 if (!tline
|| !tok_type_(tline
, TOK_PREPROC_ID
) ||
1947 (tline
->text
[1] == '%' || tline
->text
[1] == '$'
1948 || tline
->text
[1] == '!'))
1949 return NO_DIRECTIVE_FOUND
;
1951 i
= pp_token_hash(tline
->text
);
1954 * If we're in a non-emitting branch of a condition construct,
1955 * or walking to the end of an already terminated %rep block,
1956 * we should ignore all directives except for condition
1959 if (((istk
->conds
&& !emitting(istk
->conds
->state
)) ||
1960 (istk
->mstk
&& !istk
->mstk
->in_progress
)) && !is_condition(i
)) {
1961 return NO_DIRECTIVE_FOUND
;
1965 * If we're defining a macro or reading a %rep block, we should
1966 * ignore all directives except for %macro/%imacro (which nest),
1967 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
1968 * If we're in a %rep block, another %rep nests, so should be let through.
1970 if (defining
&& i
!= PP_MACRO
&& i
!= PP_IMACRO
&&
1971 i
!= PP_ENDMACRO
&& i
!= PP_ENDM
&&
1972 (defining
->name
|| (i
!= PP_ENDREP
&& i
!= PP_REP
))) {
1973 return NO_DIRECTIVE_FOUND
;
1977 if (i
== PP_MACRO
|| i
== PP_IMACRO
) {
1979 return NO_DIRECTIVE_FOUND
;
1980 } else if (nested_mac_count
> 0) {
1981 if (i
== PP_ENDMACRO
) {
1983 return NO_DIRECTIVE_FOUND
;
1986 if (!defining
->name
) {
1989 return NO_DIRECTIVE_FOUND
;
1990 } else if (nested_rep_count
> 0) {
1991 if (i
== PP_ENDREP
) {
1993 return NO_DIRECTIVE_FOUND
;
2001 error(ERR_NONFATAL
, "unknown preprocessor directive `%s'",
2003 return NO_DIRECTIVE_FOUND
; /* didn't get it */
2006 /* Directive to tell NASM what the default stack size is. The
2007 * default is for a 16-bit stack, and this can be overriden with
2009 * the following form:
2011 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2013 tline
= tline
->next
;
2014 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2015 tline
= tline
->next
;
2016 if (!tline
|| tline
->type
!= TOK_ID
) {
2017 error(ERR_NONFATAL
, "`%%stacksize' missing size parameter");
2018 free_tlist(origline
);
2019 return DIRECTIVE_FOUND
;
2021 if (nasm_stricmp(tline
->text
, "flat") == 0) {
2022 /* All subsequent ARG directives are for a 32-bit stack */
2024 StackPointer
= "ebp";
2027 } else if (nasm_stricmp(tline
->text
, "flat64") == 0) {
2028 /* All subsequent ARG directives are for a 64-bit stack */
2030 StackPointer
= "rbp";
2033 } else if (nasm_stricmp(tline
->text
, "large") == 0) {
2034 /* All subsequent ARG directives are for a 16-bit stack,
2035 * far function call.
2038 StackPointer
= "bp";
2041 } else if (nasm_stricmp(tline
->text
, "small") == 0) {
2042 /* All subsequent ARG directives are for a 16-bit stack,
2043 * far function call. We don't support near functions.
2046 StackPointer
= "bp";
2050 error(ERR_NONFATAL
, "`%%stacksize' invalid size type");
2051 free_tlist(origline
);
2052 return DIRECTIVE_FOUND
;
2054 free_tlist(origline
);
2055 return DIRECTIVE_FOUND
;
2058 /* TASM like ARG directive to define arguments to functions, in
2059 * the following form:
2061 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2065 char *arg
, directive
[256];
2066 int size
= StackSize
;
2068 /* Find the argument name */
2069 tline
= tline
->next
;
2070 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2071 tline
= tline
->next
;
2072 if (!tline
|| tline
->type
!= TOK_ID
) {
2073 error(ERR_NONFATAL
, "`%%arg' missing argument parameter");
2074 free_tlist(origline
);
2075 return DIRECTIVE_FOUND
;
2079 /* Find the argument size type */
2080 tline
= tline
->next
;
2081 if (!tline
|| tline
->type
!= TOK_OTHER
2082 || tline
->text
[0] != ':') {
2084 "Syntax error processing `%%arg' directive");
2085 free_tlist(origline
);
2086 return DIRECTIVE_FOUND
;
2088 tline
= tline
->next
;
2089 if (!tline
|| tline
->type
!= TOK_ID
) {
2090 error(ERR_NONFATAL
, "`%%arg' missing size type parameter");
2091 free_tlist(origline
);
2092 return DIRECTIVE_FOUND
;
2095 /* Allow macro expansion of type parameter */
2096 tt
= tokenize(tline
->text
);
2097 tt
= expand_smacro(tt
);
2098 size
= parse_size(tt
->text
);
2101 "Invalid size type for `%%arg' missing directive");
2103 free_tlist(origline
);
2104 return DIRECTIVE_FOUND
;
2108 /* Round up to even stack slots */
2109 size
= (size
+StackSize
-1) & ~(StackSize
-1);
2111 /* Now define the macro for the argument */
2112 snprintf(directive
, sizeof(directive
), "%%define %s (%s+%d)",
2113 arg
, StackPointer
, offset
);
2114 do_directive(tokenize(directive
));
2117 /* Move to the next argument in the list */
2118 tline
= tline
->next
;
2119 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2120 tline
= tline
->next
;
2121 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2123 free_tlist(origline
);
2124 return DIRECTIVE_FOUND
;
2127 /* TASM like LOCAL directive to define local variables for a
2128 * function, in the following form:
2130 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2132 * The '= LocalSize' at the end is ignored by NASM, but is
2133 * required by TASM to define the local parameter size (and used
2134 * by the TASM macro package).
2136 offset
= LocalOffset
;
2138 char *local
, directive
[256];
2139 int size
= StackSize
;
2141 /* Find the argument name */
2142 tline
= tline
->next
;
2143 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2144 tline
= tline
->next
;
2145 if (!tline
|| tline
->type
!= TOK_ID
) {
2147 "`%%local' missing argument parameter");
2148 free_tlist(origline
);
2149 return DIRECTIVE_FOUND
;
2151 local
= tline
->text
;
2153 /* Find the argument size type */
2154 tline
= tline
->next
;
2155 if (!tline
|| tline
->type
!= TOK_OTHER
2156 || tline
->text
[0] != ':') {
2158 "Syntax error processing `%%local' directive");
2159 free_tlist(origline
);
2160 return DIRECTIVE_FOUND
;
2162 tline
= tline
->next
;
2163 if (!tline
|| tline
->type
!= TOK_ID
) {
2165 "`%%local' missing size type parameter");
2166 free_tlist(origline
);
2167 return DIRECTIVE_FOUND
;
2170 /* Allow macro expansion of type parameter */
2171 tt
= tokenize(tline
->text
);
2172 tt
= expand_smacro(tt
);
2173 size
= parse_size(tt
->text
);
2176 "Invalid size type for `%%local' missing directive");
2178 free_tlist(origline
);
2179 return DIRECTIVE_FOUND
;
2183 /* Round up to even stack slots */
2184 size
= (size
+StackSize
-1) & ~(StackSize
-1);
2186 offset
+= size
; /* Negative offset, increment before */
2188 /* Now define the macro for the argument */
2189 snprintf(directive
, sizeof(directive
), "%%define %s (%s-%d)",
2190 local
, StackPointer
, offset
);
2191 do_directive(tokenize(directive
));
2193 /* Now define the assign to setup the enter_c macro correctly */
2194 snprintf(directive
, sizeof(directive
),
2195 "%%assign %%$localsize %%$localsize+%d", size
);
2196 do_directive(tokenize(directive
));
2198 /* Move to the next argument in the list */
2199 tline
= tline
->next
;
2200 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2201 tline
= tline
->next
;
2202 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2203 LocalOffset
= offset
;
2204 free_tlist(origline
);
2205 return DIRECTIVE_FOUND
;
2209 error(ERR_WARNING
, "trailing garbage after `%%clear' ignored");
2212 free_tlist(origline
);
2213 return DIRECTIVE_FOUND
;
2216 t
= tline
->next
= expand_smacro(tline
->next
);
2218 if (!t
|| (t
->type
!= TOK_STRING
&&
2219 t
->type
!= TOK_INTERNAL_STRING
)) {
2220 error(ERR_NONFATAL
, "`%%depend' expects a file name");
2221 free_tlist(origline
);
2222 return DIRECTIVE_FOUND
; /* but we did _something_ */
2226 "trailing garbage after `%%depend' ignored");
2228 if (t
->type
!= TOK_INTERNAL_STRING
)
2229 nasm_unquote(p
, NULL
);
2230 if (dephead
&& !in_list(*dephead
, p
)) {
2231 StrList
*sl
= nasm_malloc(strlen(p
)+1+sizeof sl
->next
);
2235 deptail
= &sl
->next
;
2237 free_tlist(origline
);
2238 return DIRECTIVE_FOUND
;
2241 t
= tline
->next
= expand_smacro(tline
->next
);
2244 if (!t
|| (t
->type
!= TOK_STRING
&&
2245 t
->type
!= TOK_INTERNAL_STRING
)) {
2246 error(ERR_NONFATAL
, "`%%include' expects a file name");
2247 free_tlist(origline
);
2248 return DIRECTIVE_FOUND
; /* but we did _something_ */
2252 "trailing garbage after `%%include' ignored");
2254 if (t
->type
!= TOK_INTERNAL_STRING
)
2255 nasm_unquote(p
, NULL
);
2256 inc
= nasm_malloc(sizeof(Include
));
2259 inc
->fp
= inc_fopen(p
, dephead
, &deptail
, pass
== 0);
2261 /* -MG given but file not found */
2264 inc
->fname
= src_set_fname(nasm_strdup(p
));
2265 inc
->lineno
= src_set_linnum(0);
2267 inc
->expansion
= NULL
;
2270 list
->uplevel(LIST_INCLUDE
);
2272 free_tlist(origline
);
2273 return DIRECTIVE_FOUND
;
2277 static macros_t
*use_pkg
;
2278 const char *pkg_macro
;
2280 t
= tline
->next
= expand_smacro(tline
->next
);
2283 if (!t
|| (t
->type
!= TOK_STRING
&&
2284 t
->type
!= TOK_INTERNAL_STRING
&&
2285 t
->type
!= TOK_ID
)) {
2286 error(ERR_NONFATAL
, "`%%use' expects a package name");
2287 free_tlist(origline
);
2288 return DIRECTIVE_FOUND
; /* but we did _something_ */
2292 "trailing garbage after `%%use' ignored");
2293 if (t
->type
== TOK_STRING
)
2294 nasm_unquote(t
->text
, NULL
);
2295 use_pkg
= nasm_stdmac_find_package(t
->text
);
2297 error(ERR_NONFATAL
, "unknown `%%use' package: %s", t
->text
);
2298 /* The first string will be <%define>__USE_*__ */
2299 pkg_macro
= (char *)use_pkg
+ 1;
2300 if (!smacro_defined(NULL
, pkg_macro
, 0, NULL
, true)) {
2301 /* Not already included, go ahead and include it */
2302 stdmacpos
= use_pkg
;
2304 free_tlist(origline
);
2305 return DIRECTIVE_FOUND
;
2308 tline
= tline
->next
;
2310 tline
= expand_id(tline
);
2312 if (!tok_type_(tline
, TOK_ID
)) {
2313 error(ERR_NONFATAL
, "`%%push' expects a context identifier");
2314 free_tlist(origline
);
2315 return DIRECTIVE_FOUND
; /* but we did _something_ */
2318 error(ERR_WARNING
, "trailing garbage after `%%push' ignored");
2319 p
= nasm_strdup(tline
->text
);
2321 p
= NULL
; /* Anonymous context */
2323 ctx
= nasm_malloc(sizeof(Context
));
2325 hash_init(&ctx
->localmac
, HASH_SMALL
);
2327 ctx
->number
= unique
++;
2329 free_tlist(origline
);
2330 return DIRECTIVE_FOUND
;
2333 tline
= tline
->next
;
2335 tline
= expand_id(tline
);
2337 if (!tok_type_(tline
, TOK_ID
)) {
2338 error(ERR_NONFATAL
, "`%%repl' expects a context identifier");
2339 free_tlist(origline
);
2340 return DIRECTIVE_FOUND
; /* but we did _something_ */
2343 error(ERR_WARNING
, "trailing garbage after `%%repl' ignored");
2344 p
= nasm_strdup(tline
->text
);
2349 error(ERR_NONFATAL
, "`%%repl': context stack is empty");
2351 nasm_free(cstk
->name
);
2354 free_tlist(origline
);
2355 return DIRECTIVE_FOUND
;
2359 error(ERR_WARNING
, "trailing garbage after `%%pop' ignored");
2361 error(ERR_NONFATAL
, "`%%pop': context stack is already empty");
2364 free_tlist(origline
);
2365 return DIRECTIVE_FOUND
;
2368 severity
= ERR_FATAL
|ERR_NO_SEVERITY
;
2371 severity
= ERR_NONFATAL
|ERR_NO_SEVERITY
;
2374 severity
= ERR_WARNING
|ERR_NO_SEVERITY
;
2379 /* Only error out if this is the final pass */
2380 if (pass
!= 2 && i
!= PP_FATAL
)
2381 return DIRECTIVE_FOUND
;
2383 tline
->next
= expand_smacro(tline
->next
);
2384 tline
= tline
->next
;
2386 t
= tline
? tline
->next
: NULL
;
2388 if (tok_type_(tline
, TOK_STRING
) && !t
) {
2389 /* The line contains only a quoted string */
2391 nasm_unquote(p
, NULL
);
2392 error(severity
, "%s: %s", pp_directives
[i
], p
);
2394 /* Not a quoted string, or more than a quoted string */
2395 p
= detoken(tline
, false);
2396 error(severity
, "%s: %s", pp_directives
[i
], p
);
2399 free_tlist(origline
);
2400 return DIRECTIVE_FOUND
;
2404 if (istk
->conds
&& !emitting(istk
->conds
->state
))
2407 j
= if_condition(tline
->next
, i
);
2408 tline
->next
= NULL
; /* it got freed */
2409 j
= j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2411 cond
= nasm_malloc(sizeof(Cond
));
2412 cond
->next
= istk
->conds
;
2415 free_tlist(origline
);
2416 return DIRECTIVE_FOUND
;
2420 error(ERR_FATAL
, "`%s': no matching `%%if'", pp_directives
[i
]);
2421 switch(istk
->conds
->state
) {
2423 istk
->conds
->state
= COND_DONE
;
2430 case COND_ELSE_TRUE
:
2431 case COND_ELSE_FALSE
:
2432 error_precond(ERR_WARNING
, "`%%elif' after `%%else' ignored");
2433 istk
->conds
->state
= COND_NEVER
;
2438 * IMPORTANT: In the case of %if, we will already have
2439 * called expand_mmac_params(); however, if we're
2440 * processing an %elif we must have been in a
2441 * non-emitting mode, which would have inhibited
2442 * the normal invocation of expand_mmac_params(). Therefore,
2443 * we have to do it explicitly here.
2445 j
= if_condition(expand_mmac_params(tline
->next
), i
);
2446 tline
->next
= NULL
; /* it got freed */
2447 istk
->conds
->state
=
2448 j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2451 free_tlist(origline
);
2452 return DIRECTIVE_FOUND
;
2456 error_precond(ERR_WARNING
, "trailing garbage after `%%else' ignored");
2458 error(ERR_FATAL
, "`%%else': no matching `%%if'");
2459 switch(istk
->conds
->state
) {
2462 istk
->conds
->state
= COND_ELSE_FALSE
;
2469 istk
->conds
->state
= COND_ELSE_TRUE
;
2472 case COND_ELSE_TRUE
:
2473 case COND_ELSE_FALSE
:
2474 error_precond(ERR_WARNING
, "`%%else' after `%%else' ignored.");
2475 istk
->conds
->state
= COND_NEVER
;
2478 free_tlist(origline
);
2479 return DIRECTIVE_FOUND
;
2483 error_precond(ERR_WARNING
, "trailing garbage after `%%endif' ignored");
2485 error(ERR_FATAL
, "`%%endif': no matching `%%if'");
2487 istk
->conds
= cond
->next
;
2489 free_tlist(origline
);
2490 return DIRECTIVE_FOUND
;
2496 "`%%%smacro': already defining a macro",
2497 (i
== PP_IMACRO
? "i" : ""));
2498 return DIRECTIVE_FOUND
;
2500 defining
= nasm_malloc(sizeof(MMacro
));
2501 defining
->casesense
= (i
== PP_MACRO
);
2502 if (!parse_mmacro_spec(tline
, defining
, pp_directives
[i
])) {
2503 nasm_free(defining
);
2505 return DIRECTIVE_FOUND
;
2508 mmac
= (MMacro
*) hash_findix(&mmacros
, defining
->name
);
2510 if (!strcmp(mmac
->name
, defining
->name
) &&
2511 (mmac
->nparam_min
<= defining
->nparam_max
2513 && (defining
->nparam_min
<= mmac
->nparam_max
2516 "redefining multi-line macro `%s'", defining
->name
);
2517 return DIRECTIVE_FOUND
;
2521 free_tlist(origline
);
2522 return DIRECTIVE_FOUND
;
2526 if (! (defining
&& defining
->name
)) {
2527 error(ERR_NONFATAL
, "`%s': not defining a macro", tline
->text
);
2528 return DIRECTIVE_FOUND
;
2530 mmhead
= (MMacro
**) hash_findi_add(&mmacros
, defining
->name
);
2531 defining
->next
= *mmhead
;
2534 free_tlist(origline
);
2535 return DIRECTIVE_FOUND
;
2543 spec
.casesense
= (i
== PP_UNMACRO
);
2544 if (!parse_mmacro_spec(tline
, &spec
, pp_directives
[i
])) {
2545 return DIRECTIVE_FOUND
;
2547 mmac_p
= (MMacro
**) hash_findi(&mmacros
, spec
.name
, NULL
);
2548 while (mmac_p
&& *mmac_p
) {
2550 if (mmac
->casesense
== spec
.casesense
&&
2551 !mstrcmp(mmac
->name
, spec
.name
, spec
.casesense
) &&
2552 mmac
->nparam_min
== spec
.nparam_min
&&
2553 mmac
->nparam_max
== spec
.nparam_max
&&
2554 mmac
->plus
== spec
.plus
) {
2555 *mmac_p
= mmac
->next
;
2558 mmac_p
= &mmac
->next
;
2561 free_tlist(origline
);
2562 free_tlist(spec
.dlist
);
2563 return DIRECTIVE_FOUND
;
2567 if (tline
->next
&& tline
->next
->type
== TOK_WHITESPACE
)
2568 tline
= tline
->next
;
2569 if (tline
->next
== NULL
) {
2570 free_tlist(origline
);
2571 error(ERR_NONFATAL
, "`%%rotate' missing rotate count");
2572 return DIRECTIVE_FOUND
;
2574 t
= expand_smacro(tline
->next
);
2576 free_tlist(origline
);
2579 tokval
.t_type
= TOKEN_INVALID
;
2581 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2584 return DIRECTIVE_FOUND
;
2587 "trailing garbage after expression ignored");
2588 if (!is_simple(evalresult
)) {
2589 error(ERR_NONFATAL
, "non-constant value given to `%%rotate'");
2590 return DIRECTIVE_FOUND
;
2593 while (mmac
&& !mmac
->name
) /* avoid mistaking %reps for macros */
2594 mmac
= mmac
->next_active
;
2596 error(ERR_NONFATAL
, "`%%rotate' invoked outside a macro call");
2597 } else if (mmac
->nparam
== 0) {
2599 "`%%rotate' invoked within macro without parameters");
2601 int rotate
= mmac
->rotate
+ reloc_value(evalresult
);
2603 rotate
%= (int)mmac
->nparam
;
2605 rotate
+= mmac
->nparam
;
2607 mmac
->rotate
= rotate
;
2609 return DIRECTIVE_FOUND
;
2614 tline
= tline
->next
;
2615 } while (tok_type_(tline
, TOK_WHITESPACE
));
2617 if (tok_type_(tline
, TOK_ID
) &&
2618 nasm_stricmp(tline
->text
, ".nolist") == 0) {
2621 tline
= tline
->next
;
2622 } while (tok_type_(tline
, TOK_WHITESPACE
));
2626 t
= expand_smacro(tline
);
2628 tokval
.t_type
= TOKEN_INVALID
;
2630 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2632 free_tlist(origline
);
2633 return DIRECTIVE_FOUND
;
2637 "trailing garbage after expression ignored");
2638 if (!is_simple(evalresult
)) {
2639 error(ERR_NONFATAL
, "non-constant value given to `%%rep'");
2640 return DIRECTIVE_FOUND
;
2642 count
= reloc_value(evalresult
) + 1;
2644 error(ERR_NONFATAL
, "`%%rep' expects a repeat count");
2647 free_tlist(origline
);
2649 tmp_defining
= defining
;
2650 defining
= nasm_malloc(sizeof(MMacro
));
2651 defining
->name
= NULL
; /* flags this macro as a %rep block */
2652 defining
->casesense
= false;
2653 defining
->plus
= false;
2654 defining
->nolist
= nolist
;
2655 defining
->in_progress
= count
;
2656 defining
->nparam_min
= defining
->nparam_max
= 0;
2657 defining
->defaults
= NULL
;
2658 defining
->dlist
= NULL
;
2659 defining
->expansion
= NULL
;
2660 defining
->next_active
= istk
->mstk
;
2661 defining
->rep_nest
= tmp_defining
;
2662 return DIRECTIVE_FOUND
;
2665 if (!defining
|| defining
->name
) {
2666 error(ERR_NONFATAL
, "`%%endrep': no matching `%%rep'");
2667 return DIRECTIVE_FOUND
;
2671 * Now we have a "macro" defined - although it has no name
2672 * and we won't be entering it in the hash tables - we must
2673 * push a macro-end marker for it on to istk->expansion.
2674 * After that, it will take care of propagating itself (a
2675 * macro-end marker line for a macro which is really a %rep
2676 * block will cause the macro to be re-expanded, complete
2677 * with another macro-end marker to ensure the process
2678 * continues) until the whole expansion is forcibly removed
2679 * from istk->expansion by a %exitrep.
2681 l
= nasm_malloc(sizeof(Line
));
2682 l
->next
= istk
->expansion
;
2683 l
->finishes
= defining
;
2685 istk
->expansion
= l
;
2687 istk
->mstk
= defining
;
2689 list
->uplevel(defining
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
2690 tmp_defining
= defining
;
2691 defining
= defining
->rep_nest
;
2692 free_tlist(origline
);
2693 return DIRECTIVE_FOUND
;
2697 * We must search along istk->expansion until we hit a
2698 * macro-end marker for a macro with no name. Then we set
2699 * its `in_progress' flag to 0.
2701 for (l
= istk
->expansion
; l
; l
= l
->next
)
2702 if (l
->finishes
&& !l
->finishes
->name
)
2706 l
->finishes
->in_progress
= 1;
2708 error(ERR_NONFATAL
, "`%%exitrep' not within `%%rep' block");
2709 free_tlist(origline
);
2710 return DIRECTIVE_FOUND
;
2716 casesense
= (i
== PP_DEFINE
|| i
== PP_XDEFINE
);
2718 tline
= tline
->next
;
2720 tline
= expand_id(tline
);
2721 if (!tline
|| (tline
->type
!= TOK_ID
&&
2722 (tline
->type
!= TOK_PREPROC_ID
||
2723 tline
->text
[1] != '$'))) {
2724 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
2726 free_tlist(origline
);
2727 return DIRECTIVE_FOUND
;
2730 ctx
= get_ctx(tline
->text
, false);
2732 mname
= tline
->text
;
2734 param_start
= tline
= tline
->next
;
2737 /* Expand the macro definition now for %xdefine and %ixdefine */
2738 if ((i
== PP_XDEFINE
) || (i
== PP_IXDEFINE
))
2739 tline
= expand_smacro(tline
);
2741 if (tok_is_(tline
, "(")) {
2743 * This macro has parameters.
2746 tline
= tline
->next
;
2750 error(ERR_NONFATAL
, "parameter identifier expected");
2751 free_tlist(origline
);
2752 return DIRECTIVE_FOUND
;
2754 if (tline
->type
!= TOK_ID
) {
2756 "`%s': parameter identifier expected",
2758 free_tlist(origline
);
2759 return DIRECTIVE_FOUND
;
2761 tline
->type
= TOK_SMAC_PARAM
+ nparam
++;
2762 tline
= tline
->next
;
2764 if (tok_is_(tline
, ",")) {
2765 tline
= tline
->next
;
2767 if (!tok_is_(tline
, ")")) {
2769 "`)' expected to terminate macro template");
2770 free_tlist(origline
);
2771 return DIRECTIVE_FOUND
;
2777 tline
= tline
->next
;
2779 if (tok_type_(tline
, TOK_WHITESPACE
))
2780 last
= tline
, tline
= tline
->next
;
2785 if (t
->type
== TOK_ID
) {
2786 for (tt
= param_start
; tt
; tt
= tt
->next
)
2787 if (tt
->type
>= TOK_SMAC_PARAM
&&
2788 !strcmp(tt
->text
, t
->text
))
2792 t
->next
= macro_start
;
2797 * Good. We now have a macro name, a parameter count, and a
2798 * token list (in reverse order) for an expansion. We ought
2799 * to be OK just to create an SMacro, store it, and let
2800 * free_tlist have the rest of the line (which we have
2801 * carefully re-terminated after chopping off the expansion
2804 define_smacro(ctx
, mname
, casesense
, nparam
, macro_start
);
2805 free_tlist(origline
);
2806 return DIRECTIVE_FOUND
;
2809 tline
= tline
->next
;
2811 tline
= expand_id(tline
);
2812 if (!tline
|| (tline
->type
!= TOK_ID
&&
2813 (tline
->type
!= TOK_PREPROC_ID
||
2814 tline
->text
[1] != '$'))) {
2815 error(ERR_NONFATAL
, "`%%undef' expects a macro identifier");
2816 free_tlist(origline
);
2817 return DIRECTIVE_FOUND
;
2821 "trailing garbage after macro name ignored");
2824 /* Find the context that symbol belongs to */
2825 ctx
= get_ctx(tline
->text
, false);
2826 undef_smacro(ctx
, tline
->text
);
2827 free_tlist(origline
);
2828 return DIRECTIVE_FOUND
;
2832 casesense
= (i
== PP_DEFSTR
);
2834 tline
= tline
->next
;
2836 tline
= expand_id(tline
);
2837 if (!tline
|| (tline
->type
!= TOK_ID
&&
2838 (tline
->type
!= TOK_PREPROC_ID
||
2839 tline
->text
[1] != '$'))) {
2840 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
2842 free_tlist(origline
);
2843 return DIRECTIVE_FOUND
;
2846 ctx
= get_ctx(tline
->text
, false);
2848 mname
= tline
->text
;
2850 tline
= expand_smacro(tline
->next
);
2853 while (tok_type_(tline
, TOK_WHITESPACE
))
2854 tline
= delete_Token(tline
);
2856 p
= detoken(tline
, false);
2857 macro_start
= nasm_malloc(sizeof(*macro_start
));
2858 macro_start
->next
= NULL
;
2859 macro_start
->text
= nasm_quote(p
, strlen(p
));
2860 macro_start
->type
= TOK_STRING
;
2861 macro_start
->a
.mac
= NULL
;
2865 * We now have a macro name, an implicit parameter count of
2866 * zero, and a string token to use as an expansion. Create
2867 * and store an SMacro.
2869 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2870 free_tlist(origline
);
2871 return DIRECTIVE_FOUND
;
2876 StrList
*xsl
= NULL
;
2877 StrList
**xst
= &xsl
;
2881 tline
= tline
->next
;
2883 tline
= expand_id(tline
);
2884 if (!tline
|| (tline
->type
!= TOK_ID
&&
2885 (tline
->type
!= TOK_PREPROC_ID
||
2886 tline
->text
[1] != '$'))) {
2888 "`%%pathsearch' expects a macro identifier as first parameter");
2889 free_tlist(origline
);
2890 return DIRECTIVE_FOUND
;
2892 ctx
= get_ctx(tline
->text
, false);
2894 mname
= tline
->text
;
2896 tline
= expand_smacro(tline
->next
);
2900 while (tok_type_(t
, TOK_WHITESPACE
))
2903 if (!t
|| (t
->type
!= TOK_STRING
&&
2904 t
->type
!= TOK_INTERNAL_STRING
)) {
2905 error(ERR_NONFATAL
, "`%%pathsearch' expects a file name");
2907 free_tlist(origline
);
2908 return DIRECTIVE_FOUND
; /* but we did _something_ */
2912 "trailing garbage after `%%pathsearch' ignored");
2914 if (t
->type
!= TOK_INTERNAL_STRING
)
2915 nasm_unquote(p
, NULL
);
2917 fp
= inc_fopen(p
, &xsl
, &xst
, true);
2920 fclose(fp
); /* Don't actually care about the file */
2922 macro_start
= nasm_malloc(sizeof(*macro_start
));
2923 macro_start
->next
= NULL
;
2924 macro_start
->text
= nasm_quote(p
, strlen(p
));
2925 macro_start
->type
= TOK_STRING
;
2926 macro_start
->a
.mac
= NULL
;
2931 * We now have a macro name, an implicit parameter count of
2932 * zero, and a string token to use as an expansion. Create
2933 * and store an SMacro.
2935 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2937 free_tlist(origline
);
2938 return DIRECTIVE_FOUND
;
2944 tline
= tline
->next
;
2946 tline
= expand_id(tline
);
2947 if (!tline
|| (tline
->type
!= TOK_ID
&&
2948 (tline
->type
!= TOK_PREPROC_ID
||
2949 tline
->text
[1] != '$'))) {
2951 "`%%strlen' expects a macro identifier as first parameter");
2952 free_tlist(origline
);
2953 return DIRECTIVE_FOUND
;
2955 ctx
= get_ctx(tline
->text
, false);
2957 mname
= tline
->text
;
2959 tline
= expand_smacro(tline
->next
);
2963 while (tok_type_(t
, TOK_WHITESPACE
))
2965 /* t should now point to the string */
2966 if (t
->type
!= TOK_STRING
) {
2968 "`%%strlen` requires string as second parameter");
2970 free_tlist(origline
);
2971 return DIRECTIVE_FOUND
;
2974 macro_start
= nasm_malloc(sizeof(*macro_start
));
2975 macro_start
->next
= NULL
;
2976 make_tok_num(macro_start
, nasm_unquote(t
->text
, NULL
));
2977 macro_start
->a
.mac
= NULL
;
2980 * We now have a macro name, an implicit parameter count of
2981 * zero, and a numeric token to use as an expansion. Create
2982 * and store an SMacro.
2984 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2986 free_tlist(origline
);
2987 return DIRECTIVE_FOUND
;
2992 tline
= tline
->next
;
2994 tline
= expand_id(tline
);
2995 if (!tline
|| (tline
->type
!= TOK_ID
&&
2996 (tline
->type
!= TOK_PREPROC_ID
||
2997 tline
->text
[1] != '$'))) {
2999 "`%%strcat' expects a macro identifier as first parameter");
3000 free_tlist(origline
);
3001 return DIRECTIVE_FOUND
;
3003 ctx
= get_ctx(tline
->text
, false);
3005 mname
= tline
->text
;
3007 tline
= expand_smacro(tline
->next
);
3011 for (t
= tline
; t
; t
= t
->next
) {
3013 case TOK_WHITESPACE
:
3016 len
+= t
->a
.len
= nasm_unquote(t
->text
, NULL
);
3019 if (!strcmp(t
->text
, ",")) /* permit comma separators */
3021 /* else fall through */
3024 "non-string passed to `%%strcat' (%d)", t
->type
);
3026 free_tlist(origline
);
3027 return DIRECTIVE_FOUND
;
3031 p
= pp
= nasm_malloc(len
);
3033 for (t
= tline
; t
; t
= t
->next
) {
3034 if (t
->type
== TOK_STRING
) {
3035 memcpy(p
, t
->text
, t
->a
.len
);
3041 * We now have a macro name, an implicit parameter count of
3042 * zero, and a numeric token to use as an expansion. Create
3043 * and store an SMacro.
3045 macro_start
= new_Token(NULL
, TOK_STRING
, NULL
, 0);
3046 macro_start
->text
= nasm_quote(pp
, len
);
3048 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3050 free_tlist(origline
);
3051 return DIRECTIVE_FOUND
;
3060 tline
= tline
->next
;
3062 tline
= expand_id(tline
);
3063 if (!tline
|| (tline
->type
!= TOK_ID
&&
3064 (tline
->type
!= TOK_PREPROC_ID
||
3065 tline
->text
[1] != '$'))) {
3067 "`%%substr' expects a macro identifier as first parameter");
3068 free_tlist(origline
);
3069 return DIRECTIVE_FOUND
;
3071 ctx
= get_ctx(tline
->text
, false);
3073 mname
= tline
->text
;
3075 tline
= expand_smacro(tline
->next
);
3079 while (tok_type_(t
, TOK_WHITESPACE
))
3082 /* t should now point to the string */
3083 if (t
->type
!= TOK_STRING
) {
3085 "`%%substr` requires string as second parameter");
3087 free_tlist(origline
);
3088 return DIRECTIVE_FOUND
;
3093 tokval
.t_type
= TOKEN_INVALID
;
3094 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
,
3098 free_tlist(origline
);
3099 return DIRECTIVE_FOUND
;
3100 } else if (!is_simple(evalresult
)) {
3101 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
3103 free_tlist(origline
);
3104 return DIRECTIVE_FOUND
;
3106 a1
= evalresult
->value
-1;
3108 while (tok_type_(tt
, TOK_WHITESPACE
))
3111 a2
= 1; /* Backwards compatibility: one character */
3113 tokval
.t_type
= TOKEN_INVALID
;
3114 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
,
3118 free_tlist(origline
);
3119 return DIRECTIVE_FOUND
;
3120 } else if (!is_simple(evalresult
)) {
3121 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
3123 free_tlist(origline
);
3124 return DIRECTIVE_FOUND
;
3126 a2
= evalresult
->value
;
3129 len
= nasm_unquote(t
->text
, NULL
);
3132 if (a1
+a2
> (int64_t)len
)
3135 macro_start
= nasm_malloc(sizeof(*macro_start
));
3136 macro_start
->next
= NULL
;
3137 macro_start
->text
= nasm_quote((a1
< 0) ? "" : t
->text
+a1
, a2
);
3138 macro_start
->type
= TOK_STRING
;
3139 macro_start
->a
.mac
= NULL
;
3142 * We now have a macro name, an implicit parameter count of
3143 * zero, and a numeric token to use as an expansion. Create
3144 * and store an SMacro.
3146 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3148 free_tlist(origline
);
3149 return DIRECTIVE_FOUND
;
3154 casesense
= (i
== PP_ASSIGN
);
3156 tline
= tline
->next
;
3158 tline
= expand_id(tline
);
3159 if (!tline
|| (tline
->type
!= TOK_ID
&&
3160 (tline
->type
!= TOK_PREPROC_ID
||
3161 tline
->text
[1] != '$'))) {
3163 "`%%%sassign' expects a macro identifier",
3164 (i
== PP_IASSIGN
? "i" : ""));
3165 free_tlist(origline
);
3166 return DIRECTIVE_FOUND
;
3168 ctx
= get_ctx(tline
->text
, false);
3170 mname
= tline
->text
;
3172 tline
= expand_smacro(tline
->next
);
3177 tokval
.t_type
= TOKEN_INVALID
;
3179 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
3182 free_tlist(origline
);
3183 return DIRECTIVE_FOUND
;
3188 "trailing garbage after expression ignored");
3190 if (!is_simple(evalresult
)) {
3192 "non-constant value given to `%%%sassign'",
3193 (i
== PP_IASSIGN
? "i" : ""));
3194 free_tlist(origline
);
3195 return DIRECTIVE_FOUND
;
3198 macro_start
= nasm_malloc(sizeof(*macro_start
));
3199 macro_start
->next
= NULL
;
3200 make_tok_num(macro_start
, reloc_value(evalresult
));
3201 macro_start
->a
.mac
= NULL
;
3204 * We now have a macro name, an implicit parameter count of
3205 * zero, and a numeric token to use as an expansion. Create
3206 * and store an SMacro.
3208 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3209 free_tlist(origline
);
3210 return DIRECTIVE_FOUND
;
3214 * Syntax is `%line nnn[+mmm] [filename]'
3216 tline
= tline
->next
;
3218 if (!tok_type_(tline
, TOK_NUMBER
)) {
3219 error(ERR_NONFATAL
, "`%%line' expects line number");
3220 free_tlist(origline
);
3221 return DIRECTIVE_FOUND
;
3223 k
= readnum(tline
->text
, &err
);
3225 tline
= tline
->next
;
3226 if (tok_is_(tline
, "+")) {
3227 tline
= tline
->next
;
3228 if (!tok_type_(tline
, TOK_NUMBER
)) {
3229 error(ERR_NONFATAL
, "`%%line' expects line increment");
3230 free_tlist(origline
);
3231 return DIRECTIVE_FOUND
;
3233 m
= readnum(tline
->text
, &err
);
3234 tline
= tline
->next
;
3240 nasm_free(src_set_fname(detoken(tline
, false)));
3242 free_tlist(origline
);
3243 return DIRECTIVE_FOUND
;
3247 "preprocessor directive `%s' not yet implemented",
3249 return DIRECTIVE_FOUND
;
3254 * Ensure that a macro parameter contains a condition code and
3255 * nothing else. Return the condition code index if so, or -1
3258 static int find_cc(Token
* t
)
3264 return -1; /* Probably a %+ without a space */
3267 if (t
->type
!= TOK_ID
)
3271 if (tt
&& (tt
->type
!= TOK_OTHER
|| strcmp(tt
->text
, ",")))
3275 j
= elements(conditions
);
3278 m
= nasm_stricmp(t
->text
, conditions
[k
]);
3294 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3295 * %-n) and MMacro-local identifiers (%%foo).
3297 static Token
*expand_mmac_params(Token
* tline
)
3299 Token
*t
, *tt
, **tail
, *thead
;
3305 if (tline
->type
== TOK_PREPROC_ID
&&
3306 (((tline
->text
[1] == '+' || tline
->text
[1] == '-')
3307 && tline
->text
[2]) || tline
->text
[1] == '%'
3308 || (tline
->text
[1] >= '0' && tline
->text
[1] <= '9'))) {
3310 int type
= 0, cc
; /* type = 0 to placate optimisers */
3317 tline
= tline
->next
;
3320 while (mac
&& !mac
->name
) /* avoid mistaking %reps for macros */
3321 mac
= mac
->next_active
;
3323 error(ERR_NONFATAL
, "`%s': not in a macro call", t
->text
);
3325 switch (t
->text
[1]) {
3327 * We have to make a substitution of one of the
3328 * forms %1, %-1, %+1, %%foo, %0.
3332 snprintf(tmpbuf
, sizeof(tmpbuf
), "%d", mac
->nparam
);
3333 text
= nasm_strdup(tmpbuf
);
3337 snprintf(tmpbuf
, sizeof(tmpbuf
), "..@%"PRIu64
".",
3339 text
= nasm_strcat(tmpbuf
, t
->text
+ 2);
3342 n
= atoi(t
->text
+ 2) - 1;
3343 if (n
>= mac
->nparam
)
3346 if (mac
->nparam
> 1)
3347 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3348 tt
= mac
->params
[n
];
3353 "macro parameter %d is not a condition code",
3358 if (inverse_ccs
[cc
] == -1) {
3360 "condition code `%s' is not invertible",
3365 nasm_strdup(conditions
[inverse_ccs
[cc
]]);
3369 n
= atoi(t
->text
+ 2) - 1;
3370 if (n
>= mac
->nparam
)
3373 if (mac
->nparam
> 1)
3374 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3375 tt
= mac
->params
[n
];
3380 "macro parameter %d is not a condition code",
3385 text
= nasm_strdup(conditions
[cc
]);
3389 n
= atoi(t
->text
+ 1) - 1;
3390 if (n
>= mac
->nparam
)
3393 if (mac
->nparam
> 1)
3394 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3395 tt
= mac
->params
[n
];
3398 for (i
= 0; i
< mac
->paramlen
[n
]; i
++) {
3399 *tail
= new_Token(NULL
, tt
->type
, tt
->text
, 0);
3400 tail
= &(*tail
)->next
;
3404 text
= NULL
; /* we've done it here */
3420 tline
= tline
->next
;
3427 for (; t
&& (tt
= t
->next
) != NULL
; t
= t
->next
)
3429 case TOK_WHITESPACE
:
3430 if (tt
->type
== TOK_WHITESPACE
) {
3431 t
->next
= delete_Token(tt
);
3435 if (tt
->type
== TOK_ID
|| tt
->type
== TOK_NUMBER
) {
3436 char *tmp
= nasm_strcat(t
->text
, tt
->text
);
3439 t
->next
= delete_Token(tt
);
3443 if (tt
->type
== TOK_NUMBER
) {
3444 char *tmp
= nasm_strcat(t
->text
, tt
->text
);
3447 t
->next
= delete_Token(tt
);
3458 * Expand all single-line macro calls made in the given line.
3459 * Return the expanded version of the line. The original is deemed
3460 * to be destroyed in the process. (In reality we'll just move
3461 * Tokens from input to output a lot of the time, rather than
3462 * actually bothering to destroy and replicate.)
3464 #define DEADMAN_LIMIT (1 << 20)
3466 static Token
*expand_smacro(Token
* tline
)
3468 Token
*t
, *tt
, *mstart
, **tail
, *thead
;
3469 struct hash_table
*smtbl
;
3470 SMacro
*head
= NULL
, *m
;
3473 unsigned int nparam
, sparam
;
3474 int brackets
, rescan
;
3475 Token
*org_tline
= tline
;
3478 int deadman
= DEADMAN_LIMIT
;
3481 * Trick: we should avoid changing the start token pointer since it can
3482 * be contained in "next" field of other token. Because of this
3483 * we allocate a copy of first token and work with it; at the end of
3484 * routine we copy it back
3488 new_Token(org_tline
->next
, org_tline
->type
, org_tline
->text
,
3490 tline
->a
.mac
= org_tline
->a
.mac
;
3491 nasm_free(org_tline
->text
);
3492 org_tline
->text
= NULL
;
3499 while (tline
) { /* main token loop */
3501 error(ERR_NONFATAL
, "interminable macro recursion");
3505 if ((mname
= tline
->text
)) {
3506 /* if this token is a local macro, look in local context */
3509 if (tline
->type
== TOK_ID
|| tline
->type
== TOK_PREPROC_ID
) {
3510 ctx
= get_ctx(mname
, true);
3512 smtbl
= &ctx
->localmac
;
3514 head
= (SMacro
*) hash_findix(smtbl
, mname
);
3517 * We've hit an identifier. As in is_mmacro below, we first
3518 * check whether the identifier is a single-line macro at
3519 * all, then think about checking for parameters if
3522 for (m
= head
; m
; m
= m
->next
)
3523 if (!mstrcmp(m
->name
, mname
, m
->casesense
))
3529 if (m
->nparam
== 0) {
3531 * Simple case: the macro is parameterless. Discard the
3532 * one token that the macro call took, and push the
3533 * expansion back on the to-do stack.
3535 if (!m
->expansion
) {
3536 if (!strcmp("__FILE__", m
->name
)) {
3539 src_get(&num
, &file
);
3540 tline
->text
= nasm_quote(file
, strlen(file
));
3541 tline
->type
= TOK_STRING
;
3545 if (!strcmp("__LINE__", m
->name
)) {
3546 nasm_free(tline
->text
);
3547 make_tok_num(tline
, src_get_linnum());
3550 if (!strcmp("__BITS__", m
->name
)) {
3551 nasm_free(tline
->text
);
3552 make_tok_num(tline
, globalbits
);
3555 tline
= delete_Token(tline
);
3560 * Complicated case: at least one macro with this name
3561 * exists and takes parameters. We must find the
3562 * parameters in the call, count them, find the SMacro
3563 * that corresponds to that form of the macro call, and
3564 * substitute for the parameters when we expand. What a
3567 /*tline = tline->next;
3568 skip_white_(tline); */
3571 while (tok_type_(t
, TOK_SMAC_END
)) {
3572 t
->a
.mac
->in_progress
= false;
3574 t
= tline
->next
= delete_Token(t
);
3577 } while (tok_type_(tline
, TOK_WHITESPACE
));
3578 if (!tok_is_(tline
, "(")) {
3580 * This macro wasn't called with parameters: ignore
3581 * the call. (Behaviour borrowed from gnu cpp.)
3590 sparam
= PARAM_DELTA
;
3591 params
= nasm_malloc(sparam
* sizeof(Token
*));
3592 params
[0] = tline
->next
;
3593 paramsize
= nasm_malloc(sparam
* sizeof(int));
3595 while (true) { /* parameter loop */
3597 * For some unusual expansions
3598 * which concatenates function call
3601 while (tok_type_(t
, TOK_SMAC_END
)) {
3602 t
->a
.mac
->in_progress
= false;
3604 t
= tline
->next
= delete_Token(t
);
3610 "macro call expects terminating `)'");
3613 if (tline
->type
== TOK_WHITESPACE
3615 if (paramsize
[nparam
])
3618 params
[nparam
] = tline
->next
;
3619 continue; /* parameter loop */
3621 if (tline
->type
== TOK_OTHER
3622 && tline
->text
[1] == 0) {
3623 char ch
= tline
->text
[0];
3624 if (ch
== ',' && !paren
&& brackets
<= 0) {
3625 if (++nparam
>= sparam
) {
3626 sparam
+= PARAM_DELTA
;
3627 params
= nasm_realloc(params
,
3632 nasm_realloc(paramsize
,
3636 params
[nparam
] = tline
->next
;
3637 paramsize
[nparam
] = 0;
3639 continue; /* parameter loop */
3642 (brackets
> 0 || (brackets
== 0 &&
3643 !paramsize
[nparam
])))
3645 if (!(brackets
++)) {
3646 params
[nparam
] = tline
->next
;
3647 continue; /* parameter loop */
3650 if (ch
== '}' && brackets
> 0)
3651 if (--brackets
== 0) {
3653 continue; /* parameter loop */
3655 if (ch
== '(' && !brackets
)
3657 if (ch
== ')' && brackets
<= 0)
3663 error(ERR_NONFATAL
, "braces do not "
3664 "enclose all of macro parameter");
3666 paramsize
[nparam
] += white
+ 1;
3668 } /* parameter loop */
3670 while (m
&& (m
->nparam
!= nparam
||
3671 mstrcmp(m
->name
, mname
,
3675 error(ERR_WARNING
| ERR_WARN_MNP
,
3676 "macro `%s' exists, "
3677 "but not taking %d parameters",
3678 mstart
->text
, nparam
);
3681 if (m
&& m
->in_progress
)
3683 if (!m
) { /* in progess or didn't find '(' or wrong nparam */
3685 * Design question: should we handle !tline, which
3686 * indicates missing ')' here, or expand those
3687 * macros anyway, which requires the (t) test a few
3691 nasm_free(paramsize
);
3695 * Expand the macro: we are placed on the last token of the
3696 * call, so that we can easily split the call from the
3697 * following tokens. We also start by pushing an SMAC_END
3698 * token for the cycle removal.
3705 tt
= new_Token(tline
, TOK_SMAC_END
, NULL
, 0);
3707 m
->in_progress
= true;
3709 for (t
= m
->expansion
; t
; t
= t
->next
) {
3710 if (t
->type
>= TOK_SMAC_PARAM
) {
3711 Token
*pcopy
= tline
, **ptail
= &pcopy
;
3715 ttt
= params
[t
->type
- TOK_SMAC_PARAM
];
3716 for (i
= paramsize
[t
->type
- TOK_SMAC_PARAM
];
3719 new_Token(tline
, ttt
->type
, ttt
->text
,
3725 } else if (t
->type
== TOK_PREPROC_Q
) {
3726 tt
= new_Token(tline
, TOK_ID
, mname
, 0);
3728 } else if (t
->type
== TOK_PREPROC_QQ
) {
3729 tt
= new_Token(tline
, TOK_ID
, m
->name
, 0);
3732 tt
= new_Token(tline
, t
->type
, t
->text
, 0);
3738 * Having done that, get rid of the macro call, and clean
3739 * up the parameters.
3742 nasm_free(paramsize
);
3744 continue; /* main token loop */
3749 if (tline
->type
== TOK_SMAC_END
) {
3750 tline
->a
.mac
->in_progress
= false;
3751 tline
= delete_Token(tline
);
3754 tline
= tline
->next
;
3762 * Now scan the entire line and look for successive TOK_IDs that resulted
3763 * after expansion (they can't be produced by tokenize()). The successive
3764 * TOK_IDs should be concatenated.
3765 * Also we look for %+ tokens and concatenate the tokens before and after
3766 * them (without white spaces in between).
3771 while (t
&& t
->type
!= TOK_ID
&& t
->type
!= TOK_PREPROC_ID
)
3775 if (t
->next
->type
== TOK_ID
||
3776 t
->next
->type
== TOK_PREPROC_ID
||
3777 t
->next
->type
== TOK_NUMBER
) {
3778 char *p
= nasm_strcat(t
->text
, t
->next
->text
);
3780 t
->next
= delete_Token(t
->next
);
3783 } else if (t
->next
->type
== TOK_WHITESPACE
&& t
->next
->next
&&
3784 t
->next
->next
->type
== TOK_PREPROC_ID
&&
3785 strcmp(t
->next
->next
->text
, "%+") == 0) {
3786 /* free the next whitespace, the %+ token and next whitespace */
3788 for (i
= 1; i
<= 3; i
++) {
3790 || (i
!= 2 && t
->next
->type
!= TOK_WHITESPACE
))
3792 t
->next
= delete_Token(t
->next
);
3797 /* If we concatenaded something, re-scan the line for macros */
3805 *org_tline
= *thead
;
3806 /* since we just gave text to org_line, don't free it */
3808 delete_Token(thead
);
3810 /* the expression expanded to empty line;
3811 we can't return NULL for some reasons
3812 we just set the line to a single WHITESPACE token. */
3813 memset(org_tline
, 0, sizeof(*org_tline
));
3814 org_tline
->text
= NULL
;
3815 org_tline
->type
= TOK_WHITESPACE
;
3824 * Similar to expand_smacro but used exclusively with macro identifiers
3825 * right before they are fetched in. The reason is that there can be
3826 * identifiers consisting of several subparts. We consider that if there
3827 * are more than one element forming the name, user wants a expansion,
3828 * otherwise it will be left as-is. Example:
3832 * the identifier %$abc will be left as-is so that the handler for %define
3833 * will suck it and define the corresponding value. Other case:
3835 * %define _%$abc cde
3837 * In this case user wants name to be expanded *before* %define starts
3838 * working, so we'll expand %$abc into something (if it has a value;
3839 * otherwise it will be left as-is) then concatenate all successive
3842 static Token
*expand_id(Token
* tline
)
3844 Token
*cur
, *oldnext
= NULL
;
3846 if (!tline
|| !tline
->next
)
3851 (cur
->next
->type
== TOK_ID
||
3852 cur
->next
->type
== TOK_PREPROC_ID
3853 || cur
->next
->type
== TOK_NUMBER
))
3856 /* If identifier consists of just one token, don't expand */
3861 oldnext
= cur
->next
; /* Detach the tail past identifier */
3862 cur
->next
= NULL
; /* so that expand_smacro stops here */
3865 tline
= expand_smacro(tline
);
3868 /* expand_smacro possibly changhed tline; re-scan for EOL */
3870 while (cur
&& cur
->next
)
3873 cur
->next
= oldnext
;
3880 * Determine whether the given line constitutes a multi-line macro
3881 * call, and return the MMacro structure called if so. Doesn't have
3882 * to check for an initial label - that's taken care of in
3883 * expand_mmacro - but must check numbers of parameters. Guaranteed
3884 * to be called with tline->type == TOK_ID, so the putative macro
3885 * name is easy to find.
3887 static MMacro
*is_mmacro(Token
* tline
, Token
*** params_array
)
3893 head
= (MMacro
*) hash_findix(&mmacros
, tline
->text
);
3896 * Efficiency: first we see if any macro exists with the given
3897 * name. If not, we can return NULL immediately. _Then_ we
3898 * count the parameters, and then we look further along the
3899 * list if necessary to find the proper MMacro.
3901 for (m
= head
; m
; m
= m
->next
)
3902 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
3908 * OK, we have a potential macro. Count and demarcate the
3911 count_mmac_params(tline
->next
, &nparam
, ¶ms
);
3914 * So we know how many parameters we've got. Find the MMacro
3915 * structure that handles this number.
3918 if (m
->nparam_min
<= nparam
3919 && (m
->plus
|| nparam
<= m
->nparam_max
)) {
3921 * This one is right. Just check if cycle removal
3922 * prohibits us using it before we actually celebrate...
3924 if (m
->in_progress
) {
3927 "self-reference in multi-line macro `%s'", m
->name
);
3933 * It's right, and we can use it. Add its default
3934 * parameters to the end of our list if necessary.
3936 if (m
->defaults
&& nparam
< m
->nparam_min
+ m
->ndefs
) {
3938 nasm_realloc(params
,
3939 ((m
->nparam_min
+ m
->ndefs
+
3940 1) * sizeof(*params
)));
3941 while (nparam
< m
->nparam_min
+ m
->ndefs
) {
3942 params
[nparam
] = m
->defaults
[nparam
- m
->nparam_min
];
3947 * If we've gone over the maximum parameter count (and
3948 * we're in Plus mode), ignore parameters beyond
3951 if (m
->plus
&& nparam
> m
->nparam_max
)
3952 nparam
= m
->nparam_max
;
3954 * Then terminate the parameter list, and leave.
3956 if (!params
) { /* need this special case */
3957 params
= nasm_malloc(sizeof(*params
));
3960 params
[nparam
] = NULL
;
3961 *params_array
= params
;
3965 * This one wasn't right: look for the next one with the
3968 for (m
= m
->next
; m
; m
= m
->next
)
3969 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
3974 * After all that, we didn't find one with the right number of
3975 * parameters. Issue a warning, and fail to expand the macro.
3977 error(ERR_WARNING
| ERR_WARN_MNP
,
3978 "macro `%s' exists, but not taking %d parameters",
3979 tline
->text
, nparam
);
3985 * Expand the multi-line macro call made by the given line, if
3986 * there is one to be expanded. If there is, push the expansion on
3987 * istk->expansion and return 1. Otherwise return 0.
3989 static int expand_mmacro(Token
* tline
)
3991 Token
*startline
= tline
;
3992 Token
*label
= NULL
;
3993 int dont_prepend
= 0;
3994 Token
**params
, *t
, *mtok
, *tt
;
3997 int i
, nparam
, *paramlen
;
4002 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
4003 if (!tok_type_(t
, TOK_ID
) && !tok_type_(t
, TOK_PREPROC_ID
))
4006 m
= is_mmacro(t
, ¶ms
);
4012 * We have an id which isn't a macro call. We'll assume
4013 * it might be a label; we'll also check to see if a
4014 * colon follows it. Then, if there's another id after
4015 * that lot, we'll check it again for macro-hood.
4019 if (tok_type_(t
, TOK_WHITESPACE
))
4020 last
= t
, t
= t
->next
;
4021 if (tok_is_(t
, ":")) {
4023 last
= t
, t
= t
->next
;
4024 if (tok_type_(t
, TOK_WHITESPACE
))
4025 last
= t
, t
= t
->next
;
4027 if (!tok_type_(t
, TOK_ID
) || (m
= is_mmacro(t
, ¶ms
)) == NULL
)
4035 * Fix up the parameters: this involves stripping leading and
4036 * trailing whitespace, then stripping braces if they are
4039 for (nparam
= 0; params
[nparam
]; nparam
++) ;
4040 paramlen
= nparam
? nasm_malloc(nparam
* sizeof(*paramlen
)) : NULL
;
4042 for (i
= 0; params
[i
]; i
++) {
4044 int comma
= (!m
->plus
|| i
< nparam
- 1);
4048 if (tok_is_(t
, "{"))
4049 t
= t
->next
, brace
= true, comma
= false;
4053 if (comma
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, ","))
4054 break; /* ... because we have hit a comma */
4055 if (comma
&& t
->type
== TOK_WHITESPACE
4056 && tok_is_(t
->next
, ","))
4057 break; /* ... or a space then a comma */
4058 if (brace
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, "}"))
4059 break; /* ... or a brace */
4066 * OK, we have a MMacro structure together with a set of
4067 * parameters. We must now go through the expansion and push
4068 * copies of each Line on to istk->expansion. Substitution of
4069 * parameter tokens and macro-local tokens doesn't get done
4070 * until the single-line macro substitution process; this is
4071 * because delaying them allows us to change the semantics
4072 * later through %rotate.
4074 * First, push an end marker on to istk->expansion, mark this
4075 * macro as in progress, and set up its invocation-specific
4078 ll
= nasm_malloc(sizeof(Line
));
4079 ll
->next
= istk
->expansion
;
4082 istk
->expansion
= ll
;
4084 m
->in_progress
= true;
4089 m
->paramlen
= paramlen
;
4090 m
->unique
= unique
++;
4093 m
->next_active
= istk
->mstk
;
4096 for (l
= m
->expansion
; l
; l
= l
->next
) {
4099 ll
= nasm_malloc(sizeof(Line
));
4100 ll
->finishes
= NULL
;
4101 ll
->next
= istk
->expansion
;
4102 istk
->expansion
= ll
;
4105 for (t
= l
->first
; t
; t
= t
->next
) {
4109 tt
= *tail
= new_Token(NULL
, TOK_ID
, mname
, 0);
4111 case TOK_PREPROC_QQ
:
4112 tt
= *tail
= new_Token(NULL
, TOK_ID
, m
->name
, 0);
4114 case TOK_PREPROC_ID
:
4115 if (t
->text
[1] == '0' && t
->text
[2] == '0') {
4123 tt
= *tail
= new_Token(NULL
, x
->type
, x
->text
, 0);
4132 * If we had a label, push it on as the first line of
4133 * the macro expansion.
4136 if (dont_prepend
< 0)
4137 free_tlist(startline
);
4139 ll
= nasm_malloc(sizeof(Line
));
4140 ll
->finishes
= NULL
;
4141 ll
->next
= istk
->expansion
;
4142 istk
->expansion
= ll
;
4143 ll
->first
= startline
;
4144 if (!dont_prepend
) {
4146 label
= label
->next
;
4147 label
->next
= tt
= new_Token(NULL
, TOK_OTHER
, ":", 0);
4152 list
->uplevel(m
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
4157 /* The function that actually does the error reporting */
4158 static void verror(int severity
, const char *fmt
, va_list arg
)
4162 vsnprintf(buff
, sizeof(buff
), fmt
, arg
);
4164 if (istk
&& istk
->mstk
&& istk
->mstk
->name
)
4165 _error(severity
| ERR_PASS1
, "(%s:%d) %s", istk
->mstk
->name
,
4166 istk
->mstk
->lineno
, buff
);
4168 _error(severity
| ERR_PASS1
, "%s", buff
);
4172 * Since preprocessor always operate only on the line that didn't
4173 * arrived yet, we should always use ERR_OFFBY1. Also since user
4174 * won't want to see same error twice (preprocessing is done once
4175 * per pass) we will want to show errors only during pass one.
4177 static void error(int severity
, const char *fmt
, ...)
4181 /* If we're in a dead branch of IF or something like it, ignore the error */
4182 if (istk
&& istk
->conds
&& !emitting(istk
->conds
->state
))
4186 verror(severity
, fmt
, arg
);
4191 * Because %else etc are evaluated in the state context
4192 * of the previous branch, errors might get lost with error():
4193 * %if 0 ... %else trailing garbage ... %endif
4194 * So %else etc should report errors with this function.
4196 static void error_precond(int severity
, const char *fmt
, ...)
4200 /* Only ignore the error if it's really in a dead branch */
4201 if (istk
&& istk
->conds
&& istk
->conds
->state
== COND_NEVER
)
4205 verror(severity
, fmt
, arg
);
4210 pp_reset(char *file
, int apass
, efunc errfunc
, evalfunc eval
,
4211 ListGen
* listgen
, StrList
**deplist
)
4215 istk
= nasm_malloc(sizeof(Include
));
4218 istk
->expansion
= NULL
;
4220 istk
->fp
= fopen(file
, "r");
4222 src_set_fname(nasm_strdup(file
));
4226 error(ERR_FATAL
| ERR_NOFILE
, "unable to open input file `%s'",
4229 nested_mac_count
= 0;
4230 nested_rep_count
= 0;
4233 if (tasm_compatible_mode
) {
4234 stdmacpos
= nasm_stdmac
;
4236 stdmacpos
= nasm_stdmac_after_tasm
;
4238 any_extrastdmac
= extrastdmac
&& *extrastdmac
;
4243 dephead
= deptail
= deplist
;
4245 StrList
*sl
= nasm_malloc(strlen(file
)+1+sizeof sl
->next
);
4247 strcpy(sl
->str
, file
);
4249 deptail
= &sl
->next
;
4253 static char *pp_getline(void)
4260 * Fetch a tokenized line, either from the macro-expansion
4261 * buffer or from the input file.
4264 while (istk
->expansion
&& istk
->expansion
->finishes
) {
4265 Line
*l
= istk
->expansion
;
4266 if (!l
->finishes
->name
&& l
->finishes
->in_progress
> 1) {
4270 * This is a macro-end marker for a macro with no
4271 * name, which means it's not really a macro at all
4272 * but a %rep block, and the `in_progress' field is
4273 * more than 1, meaning that we still need to
4274 * repeat. (1 means the natural last repetition; 0
4275 * means termination by %exitrep.) We have
4276 * therefore expanded up to the %endrep, and must
4277 * push the whole block on to the expansion buffer
4278 * again. We don't bother to remove the macro-end
4279 * marker: we'd only have to generate another one
4282 l
->finishes
->in_progress
--;
4283 for (l
= l
->finishes
->expansion
; l
; l
= l
->next
) {
4284 Token
*t
, *tt
, **tail
;
4286 ll
= nasm_malloc(sizeof(Line
));
4287 ll
->next
= istk
->expansion
;
4288 ll
->finishes
= NULL
;
4292 for (t
= l
->first
; t
; t
= t
->next
) {
4293 if (t
->text
|| t
->type
== TOK_WHITESPACE
) {
4295 new_Token(NULL
, t
->type
, t
->text
, 0);
4300 istk
->expansion
= ll
;
4304 * Check whether a `%rep' was started and not ended
4305 * within this macro expansion. This can happen and
4306 * should be detected. It's a fatal error because
4307 * I'm too confused to work out how to recover
4313 "defining with name in expansion");
4314 else if (istk
->mstk
->name
)
4316 "`%%rep' without `%%endrep' within"
4317 " expansion of macro `%s'",
4322 * FIXME: investigate the relationship at this point between
4323 * istk->mstk and l->finishes
4326 MMacro
*m
= istk
->mstk
;
4327 istk
->mstk
= m
->next_active
;
4330 * This was a real macro call, not a %rep, and
4331 * therefore the parameter information needs to
4334 nasm_free(m
->params
);
4335 free_tlist(m
->iline
);
4336 nasm_free(m
->paramlen
);
4337 l
->finishes
->in_progress
= false;
4341 istk
->expansion
= l
->next
;
4343 list
->downlevel(LIST_MACRO
);
4346 while (1) { /* until we get a line we can use */
4348 if (istk
->expansion
) { /* from a macro expansion */
4350 Line
*l
= istk
->expansion
;
4352 istk
->mstk
->lineno
++;
4354 istk
->expansion
= l
->next
;
4356 p
= detoken(tline
, false);
4357 list
->line(LIST_MACRO
, p
);
4362 if (line
) { /* from the current input file */
4363 line
= prepreproc(line
);
4364 tline
= tokenize(line
);
4369 * The current file has ended; work down the istk
4376 "expected `%%endif' before end of file");
4377 /* only set line and file name if there's a next node */
4379 src_set_linnum(i
->lineno
);
4380 nasm_free(src_set_fname(i
->fname
));
4383 list
->downlevel(LIST_INCLUDE
);
4391 * We must expand MMacro parameters and MMacro-local labels
4392 * _before_ we plunge into directive processing, to cope
4393 * with things like `%define something %1' such as STRUC
4394 * uses. Unless we're _defining_ a MMacro, in which case
4395 * those tokens should be left alone to go into the
4396 * definition; and unless we're in a non-emitting
4397 * condition, in which case we don't want to meddle with
4400 if (!defining
&& !(istk
->conds
&& !emitting(istk
->conds
->state
))
4401 && !(istk
->mstk
&& !istk
->mstk
->in_progress
))
4402 tline
= expand_mmac_params(tline
);
4405 * Check the line to see if it's a preprocessor directive.
4407 if (do_directive(tline
) == DIRECTIVE_FOUND
) {
4409 } else if (defining
) {
4411 * We're defining a multi-line macro. We emit nothing
4413 * shove the tokenized line on to the macro definition.
4415 Line
*l
= nasm_malloc(sizeof(Line
));
4416 l
->next
= defining
->expansion
;
4419 defining
->expansion
= l
;
4421 } else if (istk
->conds
&& !emitting(istk
->conds
->state
)) {
4423 * We're in a non-emitting branch of a condition block.
4424 * Emit nothing at all, not even a blank line: when we
4425 * emerge from the condition we'll give a line-number
4426 * directive so we keep our place correctly.
4430 } else if (istk
->mstk
&& !istk
->mstk
->in_progress
) {
4432 * We're in a %rep block which has been terminated, so
4433 * we're walking through to the %endrep without
4434 * emitting anything. Emit nothing at all, not even a
4435 * blank line: when we emerge from the %rep block we'll
4436 * give a line-number directive so we keep our place
4442 tline
= expand_smacro(tline
);
4443 if (!expand_mmacro(tline
)) {
4445 * De-tokenize the line again, and emit it.
4447 line
= detoken(tline
, true);
4451 continue; /* expand_mmacro calls free_tlist */
4459 static void pp_cleanup(int pass
)
4462 if(defining
->name
) {
4464 "end of file while still defining macro `%s'",
4467 error(ERR_NONFATAL
, "end of file while still in %%rep");
4470 free_mmacro(defining
);
4479 nasm_free(i
->fname
);
4484 nasm_free(src_set_fname(NULL
));
4489 while ((i
= ipath
)) {
4498 void pp_include_path(char *path
)
4502 i
= nasm_malloc(sizeof(IncPath
));
4503 i
->path
= path
? nasm_strdup(path
) : NULL
;
4506 if (ipath
!= NULL
) {
4508 while (j
->next
!= NULL
)
4516 void pp_pre_include(char *fname
)
4518 Token
*inc
, *space
, *name
;
4521 name
= new_Token(NULL
, TOK_INTERNAL_STRING
, fname
, 0);
4522 space
= new_Token(name
, TOK_WHITESPACE
, NULL
, 0);
4523 inc
= new_Token(space
, TOK_PREPROC_ID
, "%include", 0);
4525 l
= nasm_malloc(sizeof(Line
));
4532 void pp_pre_define(char *definition
)
4538 equals
= strchr(definition
, '=');
4539 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
4540 def
= new_Token(space
, TOK_PREPROC_ID
, "%define", 0);
4543 space
->next
= tokenize(definition
);
4547 l
= nasm_malloc(sizeof(Line
));
4554 void pp_pre_undefine(char *definition
)
4559 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
4560 def
= new_Token(space
, TOK_PREPROC_ID
, "%undef", 0);
4561 space
->next
= tokenize(definition
);
4563 l
= nasm_malloc(sizeof(Line
));
4571 * Added by Keith Kanios:
4573 * This function is used to assist with "runtime" preprocessor
4574 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4576 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4577 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4580 void pp_runtime(char *definition
)
4584 def
= tokenize(definition
);
4585 if(do_directive(def
) == NO_DIRECTIVE_FOUND
)
4590 void pp_extra_stdmac(macros_t
*macros
)
4592 extrastdmac
= macros
;
4595 static void make_tok_num(Token
* tok
, int64_t val
)
4598 snprintf(numbuf
, sizeof(numbuf
), "%"PRId64
"", val
);
4599 tok
->text
= nasm_strdup(numbuf
);
4600 tok
->type
= TOK_NUMBER
;