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_INDIRECT
, /* %[...] */
163 TOK_SMAC_PARAM
, /* MUST BE LAST IN THE LIST!!! */
164 TOK_MAX
= INT_MAX
/* Keep compiler from reducing the range */
171 SMacro
*mac
; /* associated macro for TOK_SMAC_END */
172 size_t len
; /* scratch length field */
173 } a
; /* Auxiliary data */
174 enum pp_token_type type
;
178 * Multi-line macro definitions are stored as a linked list of
179 * these, which is essentially a container to allow several linked
182 * Note that in this module, linked lists are treated as stacks
183 * wherever possible. For this reason, Lines are _pushed_ on to the
184 * `expansion' field in MMacro structures, so that the linked list,
185 * if walked, would give the macro lines in reverse order; this
186 * means that we can walk the list when expanding a macro, and thus
187 * push the lines on to the `expansion' field in _istk_ in reverse
188 * order (so that when popped back off they are in the right
189 * order). It may seem cockeyed, and it relies on my design having
190 * an even number of steps in, but it works...
192 * Some of these structures, rather than being actual lines, are
193 * markers delimiting the end of the expansion of a given macro.
194 * This is for use in the cycle-tracking and %rep-handling code.
195 * Such structures have `finishes' non-NULL, and `first' NULL. All
196 * others have `finishes' NULL, but `first' may still be NULL if
206 * To handle an arbitrary level of file inclusion, we maintain a
207 * stack (ie linked list) of these things.
216 MMacro
*mstk
; /* stack of active macros/reps */
220 * Include search path. This is simply a list of strings which get
221 * prepended, in turn, to the name of an include file, in an
222 * attempt to find the file if it's not in the current directory.
230 * Conditional assembly: we maintain a separate stack of these for
231 * each level of file inclusion. (The only reason we keep the
232 * stacks separate is to ensure that a stray `%endif' in a file
233 * included from within the true branch of a `%if' won't terminate
234 * it and cause confusion: instead, rightly, it'll cause an error.)
242 * These states are for use just after %if or %elif: IF_TRUE
243 * means the condition has evaluated to truth so we are
244 * currently emitting, whereas IF_FALSE means we are not
245 * currently emitting but will start doing so if a %else comes
246 * up. In these states, all directives are admissible: %elif,
247 * %else and %endif. (And of course %if.)
249 COND_IF_TRUE
, COND_IF_FALSE
,
251 * These states come up after a %else: ELSE_TRUE means we're
252 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
253 * any %elif or %else will cause an error.
255 COND_ELSE_TRUE
, COND_ELSE_FALSE
,
257 * These states mean that we're not emitting now, and also that
258 * nothing until %endif will be emitted at all. COND_DONE is
259 * used when we've had our moment of emission
260 * and have now started seeing %elifs. COND_NEVER is used when
261 * the condition construct in question is contained within a
262 * non-emitting branch of a larger condition construct,
263 * or if there is an error.
265 COND_DONE
, COND_NEVER
267 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
270 * These defines are used as the possible return values for do_directive
272 #define NO_DIRECTIVE_FOUND 0
273 #define DIRECTIVE_FOUND 1
276 * Condition codes. Note that we use c_ prefix not C_ because C_ is
277 * used in nasm.h for the "real" condition codes. At _this_ level,
278 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
279 * ones, so we need a different enum...
281 static const char * const conditions
[] = {
282 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
283 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
284 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
287 c_A
, c_AE
, c_B
, c_BE
, c_C
, c_CXZ
, c_E
, c_ECXZ
, c_G
, c_GE
, c_L
, c_LE
,
288 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, c_NE
, c_NG
, c_NGE
, c_NL
, c_NLE
, c_NO
,
289 c_NP
, c_NS
, c_NZ
, c_O
, c_P
, c_PE
, c_PO
, c_RCXZ
, c_S
, c_Z
,
292 static const enum pp_conds inverse_ccs
[] = {
293 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, -1, c_NE
, -1, c_NG
, c_NGE
, c_NL
, c_NLE
,
294 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
,
295 c_Z
, c_NO
, c_NP
, c_PO
, c_PE
, -1, c_NS
, c_NZ
301 /* If this is a an IF, ELIF, ELSE or ENDIF keyword */
302 static int is_condition(enum preproc_token arg
)
304 return PP_IS_COND(arg
) || (arg
== PP_ELSE
) || (arg
== PP_ENDIF
);
307 /* For TASM compatibility we need to be able to recognise TASM compatible
308 * conditional compilation directives. Using the NASM pre-processor does
309 * not work, so we look for them specifically from the following list and
310 * then jam in the equivalent NASM directive into the input stream.
314 TM_ARG
, TM_ELIF
, TM_ELSE
, TM_ENDIF
, TM_IF
, TM_IFDEF
, TM_IFDIFI
,
315 TM_IFNDEF
, TM_INCLUDE
, TM_LOCAL
318 static const char * const tasm_directives
[] = {
319 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
320 "ifndef", "include", "local"
323 static int StackSize
= 4;
324 static char *StackPointer
= "ebp";
325 static int ArgOffset
= 8;
326 static int LocalOffset
= 0;
328 static Context
*cstk
;
329 static Include
*istk
;
330 static IncPath
*ipath
= NULL
;
332 static efunc _error
; /* Pointer to client-provided error reporting function */
333 static evalfunc evaluate
;
335 static int pass
; /* HACK: pass 0 = generate dependencies only */
336 static StrList
**dephead
, **deptail
; /* Dependency list */
338 static uint64_t unique
; /* unique identifier numbers */
340 static Line
*predef
= NULL
;
341 static bool do_predef
;
343 static ListGen
*list
;
346 * The current set of multi-line macros we have defined.
348 static struct hash_table mmacros
;
351 * The current set of single-line macros we have defined.
353 static struct hash_table smacros
;
356 * The multi-line macro we are currently defining, or the %rep
357 * block we are currently reading, if any.
359 static MMacro
*defining
;
361 static uint64_t nested_mac_count
;
362 static uint64_t nested_rep_count
;
365 * The number of macro parameters to allocate space for at a time.
367 #define PARAM_DELTA 16
370 * The standard macro set: defined in macros.c in the array nasm_stdmac.
371 * This gives our position in the macro set, when we're processing it.
373 static macros_t
*stdmacpos
;
376 * The extra standard macros that come from the object format, if
379 static macros_t
*extrastdmac
= NULL
;
380 static bool any_extrastdmac
;
383 * Tokens are allocated in blocks to improve speed
385 #define TOKEN_BLOCKSIZE 4096
386 static Token
*freeTokens
= NULL
;
392 static Blocks blocks
= { NULL
, NULL
};
395 * Forward declarations.
397 static Token
*expand_mmac_params(Token
* tline
);
398 static Token
*expand_smacro(Token
* tline
);
399 static Token
*expand_id(Token
* tline
);
400 static Context
*get_ctx(const char *name
, bool all_contexts
);
401 static void make_tok_num(Token
* tok
, int64_t val
);
402 static void error(int severity
, const char *fmt
, ...);
403 static void error_precond(int severity
, const char *fmt
, ...);
404 static void *new_Block(size_t size
);
405 static void delete_Blocks(void);
406 static Token
*new_Token(Token
* next
, enum pp_token_type type
,
407 const char *text
, int txtlen
);
408 static Token
*delete_Token(Token
* t
);
411 * Macros for safe checking of token pointers, avoid *(NULL)
413 #define tok_type_(x,t) ((x) && (x)->type == (t))
414 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
415 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
416 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
418 /* Handle TASM specific directives, which do not contain a % in
419 * front of them. We do it here because I could not find any other
420 * place to do it for the moment, and it is a hack (ideally it would
421 * be nice to be able to use the NASM pre-processor to do it).
423 static char *check_tasm_directive(char *line
)
425 int32_t i
, j
, k
, m
, len
;
426 char *p
= line
, *oldline
, oldchar
;
428 /* Skip whitespace */
429 while (nasm_isspace(*p
) && *p
!= 0)
432 /* Binary search for the directive name */
434 j
= elements(tasm_directives
);
436 while (!nasm_isspace(p
[len
]) && p
[len
] != 0)
443 m
= nasm_stricmp(p
, tasm_directives
[k
]);
445 /* We have found a directive, so jam a % in front of it
446 * so that NASM will then recognise it as one if it's own.
451 line
= nasm_malloc(len
+ 2);
453 if (k
== TM_IFDIFI
) {
454 /* NASM does not recognise IFDIFI, so we convert it to
455 * %ifdef BOGUS. This is not used in NASM comaptible
456 * code, but does need to parse for the TASM macro
459 strcpy(line
+ 1, "ifdef BOGUS");
461 memcpy(line
+ 1, p
, len
+ 1);
476 * The pre-preprocessing stage... This function translates line
477 * number indications as they emerge from GNU cpp (`# lineno "file"
478 * flags') into NASM preprocessor line number indications (`%line
481 static char *prepreproc(char *line
)
484 char *fname
, *oldline
;
486 if (line
[0] == '#' && line
[1] == ' ') {
489 lineno
= atoi(fname
);
490 fname
+= strspn(fname
, "0123456789 ");
493 fnlen
= strcspn(fname
, "\"");
494 line
= nasm_malloc(20 + fnlen
);
495 snprintf(line
, 20 + fnlen
, "%%line %d %.*s", lineno
, fnlen
, fname
);
498 if (tasm_compatible_mode
)
499 return check_tasm_directive(line
);
504 * Free a linked list of tokens.
506 static void free_tlist(Token
* list
)
509 list
= delete_Token(list
);
514 * Free a linked list of lines.
516 static void free_llist(Line
* list
)
522 free_tlist(l
->first
);
530 static void free_mmacro(MMacro
* m
)
533 free_tlist(m
->dlist
);
534 nasm_free(m
->defaults
);
535 free_llist(m
->expansion
);
540 * Free all currently defined macros, and free the hash tables
542 static void free_smacro_table(struct hash_table
*smt
)
546 struct hash_tbl_node
*it
= NULL
;
548 while ((s
= hash_iterate(smt
, &it
, &key
)) != NULL
) {
549 nasm_free((void *)key
);
551 SMacro
*ns
= s
->next
;
553 free_tlist(s
->expansion
);
561 static void free_mmacro_table(struct hash_table
*mmt
)
565 struct hash_tbl_node
*it
= NULL
;
568 while ((m
= hash_iterate(mmt
, &it
, &key
)) != NULL
) {
569 nasm_free((void *)key
);
571 MMacro
*nm
= m
->next
;
579 static void free_macros(void)
581 free_smacro_table(&smacros
);
582 free_mmacro_table(&mmacros
);
586 * Initialize the hash tables
588 static void init_macros(void)
590 hash_init(&smacros
, HASH_LARGE
);
591 hash_init(&mmacros
, HASH_LARGE
);
595 * Pop the context stack.
597 static void ctx_pop(void)
602 free_smacro_table(&c
->localmac
);
608 * Search for a key in the hash index; adding it if necessary
609 * (in which case we initialize the data pointer to NULL.)
612 hash_findi_add(struct hash_table
*hash
, const char *str
)
614 struct hash_insert hi
;
618 r
= hash_findi(hash
, str
, &hi
);
622 strx
= nasm_strdup(str
); /* Use a more efficient allocator here? */
623 return hash_add(&hi
, strx
, NULL
);
627 * Like hash_findi, but returns the data element rather than a pointer
628 * to it. Used only when not adding a new element, hence no third
632 hash_findix(struct hash_table
*hash
, const char *str
)
636 p
= hash_findi(hash
, str
, NULL
);
637 return p
? *p
: NULL
;
640 #define BUF_DELTA 512
642 * Read a line from the top file in istk, handling multiple CR/LFs
643 * at the end of the line read, and handling spurious ^Zs. Will
644 * return lines from the standard macro set if this has not already
647 static char *read_line(void)
649 char *buffer
, *p
, *q
;
650 int bufsize
, continued_count
;
654 const unsigned char *p
= stdmacpos
;
659 len
+= pp_directives_len
[c
-0x80]+1;
663 ret
= nasm_malloc(len
+1);
665 while ((c
= *stdmacpos
++)) {
667 memcpy(q
, pp_directives
[c
-0x80], pp_directives_len
[c
-0x80]);
668 q
+= pp_directives_len
[c
-0x80];
678 /* This was the last of the standard macro chain... */
680 if (any_extrastdmac
) {
681 stdmacpos
= extrastdmac
;
682 any_extrastdmac
= false;
683 } else if (do_predef
) {
685 Token
*head
, **tail
, *t
;
688 * Nasty hack: here we push the contents of
689 * `predef' on to the top-level expansion stack,
690 * since this is the most convenient way to
691 * implement the pre-include and pre-define
694 for (pd
= predef
; pd
; pd
= pd
->next
) {
697 for (t
= pd
->first
; t
; t
= t
->next
) {
698 *tail
= new_Token(NULL
, t
->type
, t
->text
, 0);
699 tail
= &(*tail
)->next
;
701 l
= nasm_malloc(sizeof(Line
));
702 l
->next
= istk
->expansion
;
714 buffer
= nasm_malloc(BUF_DELTA
);
718 q
= fgets(p
, bufsize
- (p
- buffer
), istk
->fp
);
722 if (p
> buffer
&& p
[-1] == '\n') {
723 /* Convert backslash-CRLF line continuation sequences into
724 nothing at all (for DOS and Windows) */
725 if (((p
- 2) > buffer
) && (p
[-3] == '\\') && (p
[-2] == '\r')) {
730 /* Also convert backslash-LF line continuation sequences into
731 nothing at all (for Unix) */
732 else if (((p
- 1) > buffer
) && (p
[-2] == '\\')) {
740 if (p
- buffer
> bufsize
- 10) {
741 int32_t offset
= p
- buffer
;
742 bufsize
+= BUF_DELTA
;
743 buffer
= nasm_realloc(buffer
, bufsize
);
744 p
= buffer
+ offset
; /* prevent stale-pointer problems */
748 if (!q
&& p
== buffer
) {
753 src_set_linnum(src_get_linnum() + istk
->lineinc
+
754 (continued_count
* istk
->lineinc
));
757 * Play safe: remove CRs as well as LFs, if any of either are
758 * present at the end of the line.
760 while (--p
>= buffer
&& (*p
== '\n' || *p
== '\r'))
764 * Handle spurious ^Z, which may be inserted into source files
765 * by some file transfer utilities.
767 buffer
[strcspn(buffer
, "\032")] = '\0';
769 list
->line(LIST_READ
, buffer
);
775 * Tokenize a line of text. This is a very simple process since we
776 * don't need to parse the value out of e.g. numeric tokens: we
777 * simply split one string into many.
779 static Token
*tokenize(char *line
)
782 enum pp_token_type type
;
784 Token
*t
, **tail
= &list
;
790 if (nasm_isdigit(*p
) ||
791 ((*p
== '-' || *p
== '+') && nasm_isdigit(p
[1])) ||
792 ((*p
== '+') && (nasm_isspace(p
[1]) || !p
[1]))) {
796 while (nasm_isdigit(*p
));
797 type
= TOK_PREPROC_ID
;
798 } else if (*p
== '{') {
800 while (*p
&& *p
!= '}') {
807 type
= TOK_PREPROC_ID
;
808 } else if (*p
== '[') {
810 line
+= 2; /* Skip the leading %[ */
812 while (lvl
&& (c
= *p
++)) {
824 p
= nasm_skip_string(p
)+1;
834 error(ERR_NONFATAL
, "unterminated %[ construct");
836 } else if (*p
== '?') {
837 type
= TOK_PREPROC_Q
; /* %? */
840 type
= TOK_PREPROC_QQ
; /* %?? */
843 } else if (isidchar(*p
) ||
844 ((*p
== '!' || *p
== '%' || *p
== '$') &&
849 while (isidchar(*p
));
850 type
= TOK_PREPROC_ID
;
856 } else if (isidstart(*p
) || (*p
== '$' && isidstart(p
[1]))) {
859 while (*p
&& isidchar(*p
))
861 } else if (*p
== '\'' || *p
== '"' || *p
== '`') {
866 p
= nasm_skip_string(p
);
871 error(ERR_WARNING
|ERR_PASS1
, "unterminated string");
872 /* Handling unterminated strings by UNV */
875 } else if (isnumstart(*p
)) {
877 bool is_float
= false;
893 if (!is_hex
&& (c
== 'e' || c
== 'E')) {
895 if (*p
== '+' || *p
== '-') {
896 /* e can only be followed by +/- if it is either a
897 prefixed hex number or a floating-point number */
901 } else if (c
== 'H' || c
== 'h' || c
== 'X' || c
== 'x') {
903 } else if (c
== 'P' || c
== 'p') {
905 if (*p
== '+' || *p
== '-')
907 } else if (isnumchar(c
) || c
== '_')
910 /* we need to deal with consequences of the legacy
911 parser, like "1.nolist" being two tokens
912 (TOK_NUMBER, TOK_ID) here; at least give it
913 a shot for now. In the future, we probably need
914 a flex-based scanner with proper pattern matching
915 to do it as well as it can be done. Nothing in
916 the world is going to help the person who wants
917 0x123.p16 interpreted as two tokens, though. */
922 if (nasm_isdigit(*r
) || (is_hex
&& nasm_isxdigit(*r
)) ||
923 (!is_hex
&& (*r
== 'e' || *r
== 'E')) ||
924 (*r
== 'p' || *r
== 'P')) {
928 break; /* Terminate the token */
932 p
--; /* Point to first character beyond number */
934 if (has_e
&& !is_hex
) {
935 /* 1e13 is floating-point, but 1e13h is not */
939 type
= is_float
? TOK_FLOAT
: TOK_NUMBER
;
940 } else if (nasm_isspace(*p
)) {
941 type
= TOK_WHITESPACE
;
943 while (*p
&& nasm_isspace(*p
))
946 * Whitespace just before end-of-line is discarded by
947 * pretending it's a comment; whitespace just before a
948 * comment gets lumped into the comment.
950 if (!*p
|| *p
== ';') {
955 } else if (*p
== ';') {
961 * Anything else is an operator of some kind. We check
962 * for all the double-character operators (>>, <<, //,
963 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
964 * else is a single-character operator.
967 if ((p
[0] == '>' && p
[1] == '>') ||
968 (p
[0] == '<' && p
[1] == '<') ||
969 (p
[0] == '/' && p
[1] == '/') ||
970 (p
[0] == '<' && p
[1] == '=') ||
971 (p
[0] == '>' && p
[1] == '=') ||
972 (p
[0] == '=' && p
[1] == '=') ||
973 (p
[0] == '!' && p
[1] == '=') ||
974 (p
[0] == '<' && p
[1] == '>') ||
975 (p
[0] == '&' && p
[1] == '&') ||
976 (p
[0] == '|' && p
[1] == '|') ||
977 (p
[0] == '^' && p
[1] == '^')) {
983 /* Handling unterminated string by UNV */
986 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
987 t->text[p-line] = *line;
991 if (type
!= TOK_COMMENT
) {
992 *tail
= t
= new_Token(NULL
, type
, line
, p
- line
);
1001 * this function allocates a new managed block of memory and
1002 * returns a pointer to the block. The managed blocks are
1003 * deleted only all at once by the delete_Blocks function.
1005 static void *new_Block(size_t size
)
1007 Blocks
*b
= &blocks
;
1009 /* first, get to the end of the linked list */
1012 /* now allocate the requested chunk */
1013 b
->chunk
= nasm_malloc(size
);
1015 /* now allocate a new block for the next request */
1016 b
->next
= nasm_malloc(sizeof(Blocks
));
1017 /* and initialize the contents of the new block */
1018 b
->next
->next
= NULL
;
1019 b
->next
->chunk
= NULL
;
1024 * this function deletes all managed blocks of memory
1026 static void delete_Blocks(void)
1028 Blocks
*a
, *b
= &blocks
;
1031 * keep in mind that the first block, pointed to by blocks
1032 * is a static and not dynamically allocated, so we don't
1037 nasm_free(b
->chunk
);
1046 * this function creates a new Token and passes a pointer to it
1047 * back to the caller. It sets the type and text elements, and
1048 * also the a.mac and next elements to NULL.
1050 static Token
*new_Token(Token
* next
, enum pp_token_type type
,
1051 const char *text
, int txtlen
)
1056 if (freeTokens
== NULL
) {
1057 freeTokens
= (Token
*) new_Block(TOKEN_BLOCKSIZE
* sizeof(Token
));
1058 for (i
= 0; i
< TOKEN_BLOCKSIZE
- 1; i
++)
1059 freeTokens
[i
].next
= &freeTokens
[i
+ 1];
1060 freeTokens
[i
].next
= NULL
;
1063 freeTokens
= t
->next
;
1067 if (type
== TOK_WHITESPACE
|| text
== NULL
) {
1071 txtlen
= strlen(text
);
1072 t
->text
= nasm_malloc(txtlen
+1);
1073 memcpy(t
->text
, text
, txtlen
);
1074 t
->text
[txtlen
] = '\0';
1079 static Token
*delete_Token(Token
* t
)
1081 Token
*next
= t
->next
;
1083 t
->next
= freeTokens
;
1089 * Convert a line of tokens back into text.
1090 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1091 * will be transformed into ..@ctxnum.xxx
1093 static char *detoken(Token
* tlist
, bool expand_locals
)
1101 for (t
= tlist
; t
; t
= t
->next
) {
1102 if (t
->type
== TOK_PREPROC_ID
&& t
->text
[1] == '!') {
1103 char *p
= getenv(t
->text
+ 2);
1106 t
->text
= nasm_strdup(p
);
1110 /* Expand local macros here and not during preprocessing */
1111 if (expand_locals
&&
1112 t
->type
== TOK_PREPROC_ID
&& t
->text
&&
1113 t
->text
[0] == '%' && t
->text
[1] == '$') {
1114 Context
*ctx
= get_ctx(t
->text
, false);
1117 char *p
, *q
= t
->text
+ 2;
1119 q
+= strspn(q
, "$");
1120 snprintf(buffer
, sizeof(buffer
), "..@%"PRIu32
".", ctx
->number
);
1121 p
= nasm_strcat(buffer
, q
);
1126 if (t
->type
== TOK_WHITESPACE
) {
1128 } else if (t
->text
) {
1129 len
+= strlen(t
->text
);
1132 p
= line
= nasm_malloc(len
+ 1);
1133 for (t
= tlist
; t
; t
= t
->next
) {
1134 if (t
->type
== TOK_WHITESPACE
) {
1136 } else if (t
->text
) {
1147 * A scanner, suitable for use by the expression evaluator, which
1148 * operates on a line of Tokens. Expects a pointer to a pointer to
1149 * the first token in the line to be passed in as its private_data
1152 * FIX: This really needs to be unified with stdscan.
1154 static int ppscan(void *private_data
, struct tokenval
*tokval
)
1156 Token
**tlineptr
= private_data
;
1158 char ourcopy
[MAX_KEYWORD
+1], *p
, *r
, *s
;
1162 *tlineptr
= tline
? tline
->next
: NULL
;
1164 while (tline
&& (tline
->type
== TOK_WHITESPACE
||
1165 tline
->type
== TOK_COMMENT
));
1168 return tokval
->t_type
= TOKEN_EOS
;
1170 tokval
->t_charptr
= tline
->text
;
1172 if (tline
->text
[0] == '$' && !tline
->text
[1])
1173 return tokval
->t_type
= TOKEN_HERE
;
1174 if (tline
->text
[0] == '$' && tline
->text
[1] == '$' && !tline
->text
[2])
1175 return tokval
->t_type
= TOKEN_BASE
;
1177 if (tline
->type
== TOK_ID
) {
1178 p
= tokval
->t_charptr
= tline
->text
;
1180 tokval
->t_charptr
++;
1181 return tokval
->t_type
= TOKEN_ID
;
1184 for (r
= p
, s
= ourcopy
; *r
; r
++) {
1185 if (r
>= p
+MAX_KEYWORD
)
1186 return tokval
->t_type
= TOKEN_ID
; /* Not a keyword */
1187 *s
++ = nasm_tolower(*r
);
1190 /* right, so we have an identifier sitting in temp storage. now,
1191 * is it actually a register or instruction name, or what? */
1192 return nasm_token_hash(ourcopy
, tokval
);
1195 if (tline
->type
== TOK_NUMBER
) {
1197 tokval
->t_integer
= readnum(tline
->text
, &rn_error
);
1198 tokval
->t_charptr
= tline
->text
;
1200 return tokval
->t_type
= TOKEN_ERRNUM
;
1202 return tokval
->t_type
= TOKEN_NUM
;
1205 if (tline
->type
== TOK_FLOAT
) {
1206 return tokval
->t_type
= TOKEN_FLOAT
;
1209 if (tline
->type
== TOK_STRING
) {
1212 bq
= tline
->text
[0];
1213 tokval
->t_charptr
= tline
->text
;
1214 tokval
->t_inttwo
= nasm_unquote(tline
->text
, &ep
);
1216 if (ep
[0] != bq
|| ep
[1] != '\0')
1217 return tokval
->t_type
= TOKEN_ERRSTR
;
1219 return tokval
->t_type
= TOKEN_STR
;
1222 if (tline
->type
== TOK_OTHER
) {
1223 if (!strcmp(tline
->text
, "<<"))
1224 return tokval
->t_type
= TOKEN_SHL
;
1225 if (!strcmp(tline
->text
, ">>"))
1226 return tokval
->t_type
= TOKEN_SHR
;
1227 if (!strcmp(tline
->text
, "//"))
1228 return tokval
->t_type
= TOKEN_SDIV
;
1229 if (!strcmp(tline
->text
, "%%"))
1230 return tokval
->t_type
= TOKEN_SMOD
;
1231 if (!strcmp(tline
->text
, "=="))
1232 return tokval
->t_type
= TOKEN_EQ
;
1233 if (!strcmp(tline
->text
, "<>"))
1234 return tokval
->t_type
= TOKEN_NE
;
1235 if (!strcmp(tline
->text
, "!="))
1236 return tokval
->t_type
= TOKEN_NE
;
1237 if (!strcmp(tline
->text
, "<="))
1238 return tokval
->t_type
= TOKEN_LE
;
1239 if (!strcmp(tline
->text
, ">="))
1240 return tokval
->t_type
= TOKEN_GE
;
1241 if (!strcmp(tline
->text
, "&&"))
1242 return tokval
->t_type
= TOKEN_DBL_AND
;
1243 if (!strcmp(tline
->text
, "^^"))
1244 return tokval
->t_type
= TOKEN_DBL_XOR
;
1245 if (!strcmp(tline
->text
, "||"))
1246 return tokval
->t_type
= TOKEN_DBL_OR
;
1250 * We have no other options: just return the first character of
1253 return tokval
->t_type
= tline
->text
[0];
1257 * Compare a string to the name of an existing macro; this is a
1258 * simple wrapper which calls either strcmp or nasm_stricmp
1259 * depending on the value of the `casesense' parameter.
1261 static int mstrcmp(const char *p
, const char *q
, bool casesense
)
1263 return casesense
? strcmp(p
, q
) : nasm_stricmp(p
, q
);
1267 * Compare a string to the name of an existing macro; this is a
1268 * simple wrapper which calls either strcmp or nasm_stricmp
1269 * depending on the value of the `casesense' parameter.
1271 static int mmemcmp(const char *p
, const char *q
, size_t l
, bool casesense
)
1273 return casesense
? memcmp(p
, q
, l
) : nasm_memicmp(p
, q
, l
);
1277 * Return the Context structure associated with a %$ token. Return
1278 * NULL, having _already_ reported an error condition, if the
1279 * context stack isn't deep enough for the supplied number of $
1281 * If all_contexts == true, contexts that enclose current are
1282 * also scanned for such smacro, until it is found; if not -
1283 * only the context that directly results from the number of $'s
1284 * in variable's name.
1286 static Context
*get_ctx(const char *name
, bool all_contexts
)
1292 if (!name
|| name
[0] != '%' || name
[1] != '$')
1296 error(ERR_NONFATAL
, "`%s': context stack is empty", name
);
1300 for (i
= strspn(name
+ 2, "$"), ctx
= cstk
; (i
> 0) && ctx
; i
--) {
1302 /* i--; Lino - 02/25/02 */
1305 error(ERR_NONFATAL
, "`%s': context stack is only"
1306 " %d level%s deep", name
, i
- 1, (i
== 2 ? "" : "s"));
1313 /* Search for this smacro in found context */
1314 m
= hash_findix(&ctx
->localmac
, name
);
1316 if (!mstrcmp(m
->name
, name
, m
->casesense
))
1327 * Check to see if a file is already in a string list
1329 static bool in_list(const StrList
*list
, const char *str
)
1332 if (!strcmp(list
->str
, str
))
1340 * Open an include file. This routine must always return a valid
1341 * file pointer if it returns - it's responsible for throwing an
1342 * ERR_FATAL and bombing out completely if not. It should also try
1343 * the include path one by one until it finds the file or reaches
1344 * the end of the path.
1346 static FILE *inc_fopen(const char *file
, StrList
**dhead
, StrList
***dtail
,
1351 IncPath
*ip
= ipath
;
1352 int len
= strlen(file
);
1353 size_t prefix_len
= 0;
1357 sl
= nasm_malloc(prefix_len
+len
+1+sizeof sl
->next
);
1358 memcpy(sl
->str
, prefix
, prefix_len
);
1359 memcpy(sl
->str
+prefix_len
, file
, len
+1);
1360 fp
= fopen(sl
->str
, "r");
1361 if (fp
&& dhead
&& !in_list(*dhead
, sl
->str
)) {
1379 prefix_len
= strlen(prefix
);
1381 /* -MG given and file not found */
1382 if (dhead
&& !in_list(*dhead
, file
)) {
1383 sl
= nasm_malloc(len
+1+sizeof sl
->next
);
1385 strcpy(sl
->str
, file
);
1393 error(ERR_FATAL
, "unable to open include file `%s'", file
);
1394 return NULL
; /* never reached - placate compilers */
1398 * Determine if we should warn on defining a single-line macro of
1399 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1400 * return true if _any_ single-line macro of that name is defined.
1401 * Otherwise, will return true if a single-line macro with either
1402 * `nparam' or no parameters is defined.
1404 * If a macro with precisely the right number of parameters is
1405 * defined, or nparam is -1, the address of the definition structure
1406 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1407 * is NULL, no action will be taken regarding its contents, and no
1410 * Note that this is also called with nparam zero to resolve
1413 * If you already know which context macro belongs to, you can pass
1414 * the context pointer as first parameter; if you won't but name begins
1415 * with %$ the context will be automatically computed. If all_contexts
1416 * is true, macro will be searched in outer contexts as well.
1419 smacro_defined(Context
* ctx
, const char *name
, int nparam
, SMacro
** defn
,
1422 struct hash_table
*smtbl
;
1426 smtbl
= &ctx
->localmac
;
1427 } else if (name
[0] == '%' && name
[1] == '$') {
1429 ctx
= get_ctx(name
, false);
1431 return false; /* got to return _something_ */
1432 smtbl
= &ctx
->localmac
;
1436 m
= (SMacro
*) hash_findix(smtbl
, name
);
1439 if (!mstrcmp(m
->name
, name
, m
->casesense
&& nocase
) &&
1440 (nparam
<= 0 || m
->nparam
== 0 || nparam
== (int) m
->nparam
)) {
1442 if (nparam
== (int) m
->nparam
|| nparam
== -1)
1456 * Count and mark off the parameters in a multi-line macro call.
1457 * This is called both from within the multi-line macro expansion
1458 * code, and also to mark off the default parameters when provided
1459 * in a %macro definition line.
1461 static void count_mmac_params(Token
* t
, int *nparam
, Token
*** params
)
1463 int paramsize
, brace
;
1465 *nparam
= paramsize
= 0;
1468 /* +1: we need space for the final NULL */
1469 if (*nparam
+1 >= paramsize
) {
1470 paramsize
+= PARAM_DELTA
;
1471 *params
= nasm_realloc(*params
, sizeof(**params
) * paramsize
);
1475 if (tok_is_(t
, "{"))
1477 (*params
)[(*nparam
)++] = t
;
1478 while (tok_isnt_(t
, brace
? "}" : ","))
1480 if (t
) { /* got a comma/brace */
1484 * Now we've found the closing brace, look further
1488 if (tok_isnt_(t
, ",")) {
1490 "braces do not enclose all of macro parameter");
1491 while (tok_isnt_(t
, ","))
1495 t
= t
->next
; /* eat the comma */
1502 * Determine whether one of the various `if' conditions is true or
1505 * We must free the tline we get passed.
1507 static bool if_condition(Token
* tline
, enum preproc_token ct
)
1509 enum pp_conditional i
= PP_COND(ct
);
1511 Token
*t
, *tt
, **tptr
, *origline
;
1512 struct tokenval tokval
;
1514 enum pp_token_type needtype
;
1520 j
= false; /* have we matched yet? */
1525 if (tline
->type
!= TOK_ID
) {
1527 "`%s' expects context identifiers", pp_directives
[ct
]);
1528 free_tlist(origline
);
1531 if (cstk
&& cstk
->name
&& !nasm_stricmp(tline
->text
, cstk
->name
))
1533 tline
= tline
->next
;
1538 j
= false; /* have we matched yet? */
1541 if (!tline
|| (tline
->type
!= TOK_ID
&&
1542 (tline
->type
!= TOK_PREPROC_ID
||
1543 tline
->text
[1] != '$'))) {
1545 "`%s' expects macro identifiers", pp_directives
[ct
]);
1548 if (smacro_defined(NULL
, tline
->text
, 0, NULL
, true))
1550 tline
= tline
->next
;
1556 tline
= expand_smacro(tline
);
1558 while (tok_isnt_(tt
, ","))
1562 "`%s' expects two comma-separated arguments",
1567 j
= true; /* assume equality unless proved not */
1568 while ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) && tt
) {
1569 if (tt
->type
== TOK_OTHER
&& !strcmp(tt
->text
, ",")) {
1570 error(ERR_NONFATAL
, "`%s': more than one comma on line",
1574 if (t
->type
== TOK_WHITESPACE
) {
1578 if (tt
->type
== TOK_WHITESPACE
) {
1582 if (tt
->type
!= t
->type
) {
1583 j
= false; /* found mismatching tokens */
1586 /* When comparing strings, need to unquote them first */
1587 if (t
->type
== TOK_STRING
) {
1588 size_t l1
= nasm_unquote(t
->text
, NULL
);
1589 size_t l2
= nasm_unquote(tt
->text
, NULL
);
1595 if (mmemcmp(t
->text
, tt
->text
, l1
, i
== PPC_IFIDN
)) {
1599 } else if (mstrcmp(tt
->text
, t
->text
, i
== PPC_IFIDN
) != 0) {
1600 j
= false; /* found mismatching tokens */
1607 if ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) || tt
)
1608 j
= false; /* trailing gunk on one end or other */
1614 MMacro searching
, *mmac
;
1617 tline
= expand_id(tline
);
1618 if (!tok_type_(tline
, TOK_ID
)) {
1620 "`%s' expects a macro name", pp_directives
[ct
]);
1623 searching
.name
= nasm_strdup(tline
->text
);
1624 searching
.casesense
= true;
1625 searching
.plus
= false;
1626 searching
.nolist
= false;
1627 searching
.in_progress
= 0;
1628 searching
.rep_nest
= NULL
;
1629 searching
.nparam_min
= 0;
1630 searching
.nparam_max
= INT_MAX
;
1631 tline
= expand_smacro(tline
->next
);
1634 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
1636 "`%s' expects a parameter count or nothing",
1639 searching
.nparam_min
= searching
.nparam_max
=
1640 readnum(tline
->text
, &j
);
1643 "unable to parse parameter count `%s'",
1646 if (tline
&& tok_is_(tline
->next
, "-")) {
1647 tline
= tline
->next
->next
;
1648 if (tok_is_(tline
, "*"))
1649 searching
.nparam_max
= INT_MAX
;
1650 else if (!tok_type_(tline
, TOK_NUMBER
))
1652 "`%s' expects a parameter count after `-'",
1655 searching
.nparam_max
= readnum(tline
->text
, &j
);
1658 "unable to parse parameter count `%s'",
1660 if (searching
.nparam_min
> searching
.nparam_max
)
1662 "minimum parameter count exceeds maximum");
1665 if (tline
&& tok_is_(tline
->next
, "+")) {
1666 tline
= tline
->next
;
1667 searching
.plus
= true;
1669 mmac
= (MMacro
*) hash_findix(&mmacros
, searching
.name
);
1671 if (!strcmp(mmac
->name
, searching
.name
) &&
1672 (mmac
->nparam_min
<= searching
.nparam_max
1674 && (searching
.nparam_min
<= mmac
->nparam_max
1681 if(tline
&& tline
->next
)
1682 error(ERR_WARNING
|ERR_PASS1
,
1683 "trailing garbage after %%ifmacro ignored");
1684 nasm_free(searching
.name
);
1693 needtype
= TOK_NUMBER
;
1696 needtype
= TOK_STRING
;
1700 t
= tline
= expand_smacro(tline
);
1702 while (tok_type_(t
, TOK_WHITESPACE
) ||
1703 (needtype
== TOK_NUMBER
&&
1704 tok_type_(t
, TOK_OTHER
) &&
1705 (t
->text
[0] == '-' || t
->text
[0] == '+') &&
1709 j
= tok_type_(t
, needtype
);
1713 t
= tline
= expand_smacro(tline
);
1714 while (tok_type_(t
, TOK_WHITESPACE
))
1719 t
= t
->next
; /* Skip the actual token */
1720 while (tok_type_(t
, TOK_WHITESPACE
))
1722 j
= !t
; /* Should be nothing left */
1727 t
= tline
= expand_smacro(tline
);
1728 while (tok_type_(t
, TOK_WHITESPACE
))
1731 j
= !t
; /* Should be empty */
1735 t
= tline
= expand_smacro(tline
);
1737 tokval
.t_type
= TOKEN_INVALID
;
1738 evalresult
= evaluate(ppscan
, tptr
, &tokval
,
1739 NULL
, pass
| CRITICAL
, error
, NULL
);
1743 error(ERR_WARNING
|ERR_PASS1
,
1744 "trailing garbage after expression ignored");
1745 if (!is_simple(evalresult
)) {
1747 "non-constant value given to `%s'", pp_directives
[ct
]);
1750 j
= reloc_value(evalresult
) != 0;
1755 "preprocessor directive `%s' not yet implemented",
1760 free_tlist(origline
);
1761 return j
^ PP_NEGATIVE(ct
);
1764 free_tlist(origline
);
1769 * Common code for defining an smacro
1771 static bool define_smacro(Context
*ctx
, char *mname
, bool casesense
,
1772 int nparam
, Token
*expansion
)
1774 SMacro
*smac
, **smhead
;
1775 struct hash_table
*smtbl
;
1777 if (smacro_defined(ctx
, mname
, nparam
, &smac
, casesense
)) {
1779 error(ERR_WARNING
|ERR_PASS1
,
1780 "single-line macro `%s' defined both with and"
1781 " without parameters", mname
);
1783 /* Some instances of the old code considered this a failure,
1784 some others didn't. What is the right thing to do here? */
1785 free_tlist(expansion
);
1786 return false; /* Failure */
1789 * We're redefining, so we have to take over an
1790 * existing SMacro structure. This means freeing
1791 * what was already in it.
1793 nasm_free(smac
->name
);
1794 free_tlist(smac
->expansion
);
1797 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
1798 smhead
= (SMacro
**) hash_findi_add(smtbl
, mname
);
1799 smac
= nasm_malloc(sizeof(SMacro
));
1800 smac
->next
= *smhead
;
1803 smac
->name
= nasm_strdup(mname
);
1804 smac
->casesense
= casesense
;
1805 smac
->nparam
= nparam
;
1806 smac
->expansion
= expansion
;
1807 smac
->in_progress
= false;
1808 return true; /* Success */
1812 * Undefine an smacro
1814 static void undef_smacro(Context
*ctx
, const char *mname
)
1816 SMacro
**smhead
, *s
, **sp
;
1817 struct hash_table
*smtbl
;
1819 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
1820 smhead
= (SMacro
**)hash_findi(smtbl
, mname
, NULL
);
1824 * We now have a macro name... go hunt for it.
1827 while ((s
= *sp
) != NULL
) {
1828 if (!mstrcmp(s
->name
, mname
, s
->casesense
)) {
1831 free_tlist(s
->expansion
);
1841 * Parse a mmacro specification.
1843 static bool parse_mmacro_spec(Token
*tline
, MMacro
*def
, const char *directive
)
1847 tline
= tline
->next
;
1849 tline
= expand_id(tline
);
1850 if (!tok_type_(tline
, TOK_ID
)) {
1851 error(ERR_NONFATAL
, "`%s' expects a macro name", directive
);
1855 def
->name
= nasm_strdup(tline
->text
);
1857 def
->nolist
= false;
1858 def
->in_progress
= 0;
1859 def
->rep_nest
= NULL
;
1860 def
->nparam_min
= 0;
1861 def
->nparam_max
= 0;
1863 tline
= expand_smacro(tline
->next
);
1865 if (!tok_type_(tline
, TOK_NUMBER
)) {
1866 error(ERR_NONFATAL
, "`%s' expects a parameter count", directive
);
1868 def
->nparam_min
= def
->nparam_max
=
1869 readnum(tline
->text
, &err
);
1872 "unable to parse parameter count `%s'", tline
->text
);
1874 if (tline
&& tok_is_(tline
->next
, "-")) {
1875 tline
= tline
->next
->next
;
1876 if (tok_is_(tline
, "*")) {
1877 def
->nparam_max
= INT_MAX
;
1878 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
1880 "`%s' expects a parameter count after `-'", directive
);
1882 def
->nparam_max
= readnum(tline
->text
, &err
);
1884 error(ERR_NONFATAL
, "unable to parse parameter count `%s'",
1887 if (def
->nparam_min
> def
->nparam_max
) {
1888 error(ERR_NONFATAL
, "minimum parameter count exceeds maximum");
1892 if (tline
&& tok_is_(tline
->next
, "+")) {
1893 tline
= tline
->next
;
1896 if (tline
&& tok_type_(tline
->next
, TOK_ID
) &&
1897 !nasm_stricmp(tline
->next
->text
, ".nolist")) {
1898 tline
= tline
->next
;
1903 * Handle default parameters.
1905 if (tline
&& tline
->next
) {
1906 def
->dlist
= tline
->next
;
1908 count_mmac_params(def
->dlist
, &def
->ndefs
, &def
->defaults
);
1911 def
->defaults
= NULL
;
1913 def
->expansion
= NULL
;
1916 def
->ndefs
> def
->nparam_max
- def
->nparam_min
&&
1918 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MDP
,
1919 "too many default macro parameters");
1926 * Decode a size directive
1928 static int parse_size(const char *str
) {
1929 static const char *size_names
[] =
1930 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
1931 static const int sizes
[] =
1932 { 0, 1, 4, 16, 8, 10, 2, 32 };
1934 return sizes
[bsii(str
, size_names
, elements(size_names
))+1];
1938 * find and process preprocessor directive in passed line
1939 * Find out if a line contains a preprocessor directive, and deal
1942 * If a directive _is_ found, it is the responsibility of this routine
1943 * (and not the caller) to free_tlist() the line.
1945 * @param tline a pointer to the current tokeninzed line linked list
1946 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
1949 static int do_directive(Token
* tline
)
1951 enum preproc_token i
;
1959 char *p
, *pp
, *mname
;
1963 MMacro
*mmac
, **mmhead
;
1964 Token
*t
, *tt
, *param_start
, *macro_start
, *last
, **tptr
, *origline
;
1966 struct tokenval tokval
;
1968 MMacro
*tmp_defining
; /* Used when manipulating rep_nest */
1976 if (!tline
|| !tok_type_(tline
, TOK_PREPROC_ID
) ||
1977 (tline
->text
[1] == '%' || tline
->text
[1] == '$'
1978 || tline
->text
[1] == '!'))
1979 return NO_DIRECTIVE_FOUND
;
1981 i
= pp_token_hash(tline
->text
);
1984 * If we're in a non-emitting branch of a condition construct,
1985 * or walking to the end of an already terminated %rep block,
1986 * we should ignore all directives except for condition
1989 if (((istk
->conds
&& !emitting(istk
->conds
->state
)) ||
1990 (istk
->mstk
&& !istk
->mstk
->in_progress
)) && !is_condition(i
)) {
1991 return NO_DIRECTIVE_FOUND
;
1995 * If we're defining a macro or reading a %rep block, we should
1996 * ignore all directives except for %macro/%imacro (which nest),
1997 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
1998 * If we're in a %rep block, another %rep nests, so should be let through.
2000 if (defining
&& i
!= PP_MACRO
&& i
!= PP_IMACRO
&&
2001 i
!= PP_ENDMACRO
&& i
!= PP_ENDM
&&
2002 (defining
->name
|| (i
!= PP_ENDREP
&& i
!= PP_REP
))) {
2003 return NO_DIRECTIVE_FOUND
;
2007 if (i
== PP_MACRO
|| i
== PP_IMACRO
) {
2009 return NO_DIRECTIVE_FOUND
;
2010 } else if (nested_mac_count
> 0) {
2011 if (i
== PP_ENDMACRO
) {
2013 return NO_DIRECTIVE_FOUND
;
2016 if (!defining
->name
) {
2019 return NO_DIRECTIVE_FOUND
;
2020 } else if (nested_rep_count
> 0) {
2021 if (i
== PP_ENDREP
) {
2023 return NO_DIRECTIVE_FOUND
;
2031 error(ERR_NONFATAL
, "unknown preprocessor directive `%s'",
2033 return NO_DIRECTIVE_FOUND
; /* didn't get it */
2036 /* Directive to tell NASM what the default stack size is. The
2037 * default is for a 16-bit stack, and this can be overriden with
2039 * the following form:
2041 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2043 tline
= tline
->next
;
2044 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2045 tline
= tline
->next
;
2046 if (!tline
|| tline
->type
!= TOK_ID
) {
2047 error(ERR_NONFATAL
, "`%%stacksize' missing size parameter");
2048 free_tlist(origline
);
2049 return DIRECTIVE_FOUND
;
2051 if (nasm_stricmp(tline
->text
, "flat") == 0) {
2052 /* All subsequent ARG directives are for a 32-bit stack */
2054 StackPointer
= "ebp";
2057 } else if (nasm_stricmp(tline
->text
, "flat64") == 0) {
2058 /* All subsequent ARG directives are for a 64-bit stack */
2060 StackPointer
= "rbp";
2063 } else if (nasm_stricmp(tline
->text
, "large") == 0) {
2064 /* All subsequent ARG directives are for a 16-bit stack,
2065 * far function call.
2068 StackPointer
= "bp";
2071 } else if (nasm_stricmp(tline
->text
, "small") == 0) {
2072 /* All subsequent ARG directives are for a 16-bit stack,
2073 * far function call. We don't support near functions.
2076 StackPointer
= "bp";
2080 error(ERR_NONFATAL
, "`%%stacksize' invalid size type");
2081 free_tlist(origline
);
2082 return DIRECTIVE_FOUND
;
2084 free_tlist(origline
);
2085 return DIRECTIVE_FOUND
;
2088 /* TASM like ARG directive to define arguments to functions, in
2089 * the following form:
2091 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2095 char *arg
, directive
[256];
2096 int size
= StackSize
;
2098 /* Find the argument name */
2099 tline
= tline
->next
;
2100 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2101 tline
= tline
->next
;
2102 if (!tline
|| tline
->type
!= TOK_ID
) {
2103 error(ERR_NONFATAL
, "`%%arg' missing argument parameter");
2104 free_tlist(origline
);
2105 return DIRECTIVE_FOUND
;
2109 /* Find the argument size type */
2110 tline
= tline
->next
;
2111 if (!tline
|| tline
->type
!= TOK_OTHER
2112 || tline
->text
[0] != ':') {
2114 "Syntax error processing `%%arg' directive");
2115 free_tlist(origline
);
2116 return DIRECTIVE_FOUND
;
2118 tline
= tline
->next
;
2119 if (!tline
|| tline
->type
!= TOK_ID
) {
2120 error(ERR_NONFATAL
, "`%%arg' missing size type parameter");
2121 free_tlist(origline
);
2122 return DIRECTIVE_FOUND
;
2125 /* Allow macro expansion of type parameter */
2126 tt
= tokenize(tline
->text
);
2127 tt
= expand_smacro(tt
);
2128 size
= parse_size(tt
->text
);
2131 "Invalid size type for `%%arg' missing directive");
2133 free_tlist(origline
);
2134 return DIRECTIVE_FOUND
;
2138 /* Round up to even stack slots */
2139 size
= (size
+StackSize
-1) & ~(StackSize
-1);
2141 /* Now define the macro for the argument */
2142 snprintf(directive
, sizeof(directive
), "%%define %s (%s+%d)",
2143 arg
, StackPointer
, offset
);
2144 do_directive(tokenize(directive
));
2147 /* Move to the next argument in the list */
2148 tline
= tline
->next
;
2149 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2150 tline
= tline
->next
;
2151 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2153 free_tlist(origline
);
2154 return DIRECTIVE_FOUND
;
2157 /* TASM like LOCAL directive to define local variables for a
2158 * function, in the following form:
2160 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2162 * The '= LocalSize' at the end is ignored by NASM, but is
2163 * required by TASM to define the local parameter size (and used
2164 * by the TASM macro package).
2166 offset
= LocalOffset
;
2168 char *local
, directive
[256];
2169 int size
= StackSize
;
2171 /* Find the argument name */
2172 tline
= tline
->next
;
2173 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2174 tline
= tline
->next
;
2175 if (!tline
|| tline
->type
!= TOK_ID
) {
2177 "`%%local' missing argument parameter");
2178 free_tlist(origline
);
2179 return DIRECTIVE_FOUND
;
2181 local
= tline
->text
;
2183 /* Find the argument size type */
2184 tline
= tline
->next
;
2185 if (!tline
|| tline
->type
!= TOK_OTHER
2186 || tline
->text
[0] != ':') {
2188 "Syntax error processing `%%local' directive");
2189 free_tlist(origline
);
2190 return DIRECTIVE_FOUND
;
2192 tline
= tline
->next
;
2193 if (!tline
|| tline
->type
!= TOK_ID
) {
2195 "`%%local' missing size type parameter");
2196 free_tlist(origline
);
2197 return DIRECTIVE_FOUND
;
2200 /* Allow macro expansion of type parameter */
2201 tt
= tokenize(tline
->text
);
2202 tt
= expand_smacro(tt
);
2203 size
= parse_size(tt
->text
);
2206 "Invalid size type for `%%local' missing directive");
2208 free_tlist(origline
);
2209 return DIRECTIVE_FOUND
;
2213 /* Round up to even stack slots */
2214 size
= (size
+StackSize
-1) & ~(StackSize
-1);
2216 offset
+= size
; /* Negative offset, increment before */
2218 /* Now define the macro for the argument */
2219 snprintf(directive
, sizeof(directive
), "%%define %s (%s-%d)",
2220 local
, StackPointer
, offset
);
2221 do_directive(tokenize(directive
));
2223 /* Now define the assign to setup the enter_c macro correctly */
2224 snprintf(directive
, sizeof(directive
),
2225 "%%assign %%$localsize %%$localsize+%d", size
);
2226 do_directive(tokenize(directive
));
2228 /* Move to the next argument in the list */
2229 tline
= tline
->next
;
2230 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2231 tline
= tline
->next
;
2232 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2233 LocalOffset
= offset
;
2234 free_tlist(origline
);
2235 return DIRECTIVE_FOUND
;
2239 error(ERR_WARNING
|ERR_PASS1
,
2240 "trailing garbage after `%%clear' ignored");
2243 free_tlist(origline
);
2244 return DIRECTIVE_FOUND
;
2247 t
= tline
->next
= expand_smacro(tline
->next
);
2249 if (!t
|| (t
->type
!= TOK_STRING
&&
2250 t
->type
!= TOK_INTERNAL_STRING
)) {
2251 error(ERR_NONFATAL
, "`%%depend' expects a file name");
2252 free_tlist(origline
);
2253 return DIRECTIVE_FOUND
; /* but we did _something_ */
2256 error(ERR_WARNING
|ERR_PASS1
,
2257 "trailing garbage after `%%depend' ignored");
2259 if (t
->type
!= TOK_INTERNAL_STRING
)
2260 nasm_unquote(p
, NULL
);
2261 if (dephead
&& !in_list(*dephead
, p
)) {
2262 StrList
*sl
= nasm_malloc(strlen(p
)+1+sizeof sl
->next
);
2266 deptail
= &sl
->next
;
2268 free_tlist(origline
);
2269 return DIRECTIVE_FOUND
;
2272 t
= tline
->next
= expand_smacro(tline
->next
);
2275 if (!t
|| (t
->type
!= TOK_STRING
&&
2276 t
->type
!= TOK_INTERNAL_STRING
)) {
2277 error(ERR_NONFATAL
, "`%%include' expects a file name");
2278 free_tlist(origline
);
2279 return DIRECTIVE_FOUND
; /* but we did _something_ */
2282 error(ERR_WARNING
|ERR_PASS1
,
2283 "trailing garbage after `%%include' ignored");
2285 if (t
->type
!= TOK_INTERNAL_STRING
)
2286 nasm_unquote(p
, NULL
);
2287 inc
= nasm_malloc(sizeof(Include
));
2290 inc
->fp
= inc_fopen(p
, dephead
, &deptail
, pass
== 0);
2292 /* -MG given but file not found */
2295 inc
->fname
= src_set_fname(nasm_strdup(p
));
2296 inc
->lineno
= src_set_linnum(0);
2298 inc
->expansion
= NULL
;
2301 list
->uplevel(LIST_INCLUDE
);
2303 free_tlist(origline
);
2304 return DIRECTIVE_FOUND
;
2308 static macros_t
*use_pkg
;
2309 const char *pkg_macro
;
2311 tline
= tline
->next
;
2313 tline
= expand_id(tline
);
2315 if (!tline
|| (tline
->type
!= TOK_STRING
&&
2316 tline
->type
!= TOK_INTERNAL_STRING
&&
2317 tline
->type
!= TOK_ID
)) {
2318 error(ERR_NONFATAL
, "`%%use' expects a package name");
2319 free_tlist(origline
);
2320 return DIRECTIVE_FOUND
; /* but we did _something_ */
2323 error(ERR_WARNING
|ERR_PASS1
,
2324 "trailing garbage after `%%use' ignored");
2325 if (tline
->type
== TOK_STRING
)
2326 nasm_unquote(tline
->text
, NULL
);
2327 use_pkg
= nasm_stdmac_find_package(tline
->text
);
2329 error(ERR_NONFATAL
, "unknown `%%use' package: %s", tline
->text
);
2330 /* The first string will be <%define>__USE_*__ */
2331 pkg_macro
= (char *)use_pkg
+ 1;
2332 if (!smacro_defined(NULL
, pkg_macro
, 0, NULL
, true)) {
2333 /* Not already included, go ahead and include it */
2334 stdmacpos
= use_pkg
;
2336 free_tlist(origline
);
2337 return DIRECTIVE_FOUND
;
2342 tline
= tline
->next
;
2344 tline
= expand_id(tline
);
2346 if (!tok_type_(tline
, TOK_ID
)) {
2347 error(ERR_NONFATAL
, "`%s' expects a context identifier",
2349 free_tlist(origline
);
2350 return DIRECTIVE_FOUND
; /* but we did _something_ */
2353 error(ERR_WARNING
|ERR_PASS1
,
2354 "trailing garbage after `%s' ignored",
2356 p
= nasm_strdup(tline
->text
);
2358 p
= NULL
; /* Anonymous */
2362 ctx
= nasm_malloc(sizeof(Context
));
2364 hash_init(&ctx
->localmac
, HASH_SMALL
);
2366 ctx
->number
= unique
++;
2371 error(ERR_NONFATAL
, "`%s': context stack is empty",
2373 } else if (i
== PP_POP
) {
2374 if (p
&& (!cstk
->name
|| nasm_stricmp(p
, cstk
->name
)))
2375 error(ERR_NONFATAL
, "`%%pop' in wrong context: %s, "
2377 cstk
->name
? cstk
->name
: "anonymous", p
);
2382 nasm_free(cstk
->name
);
2388 free_tlist(origline
);
2389 return DIRECTIVE_FOUND
;
2391 severity
= ERR_FATAL
|ERR_NO_SEVERITY
;
2394 severity
= ERR_NONFATAL
|ERR_NO_SEVERITY
;
2397 severity
= ERR_WARNING
|ERR_NO_SEVERITY
|ERR_WARN_USER
;
2402 /* Only error out if this is the final pass */
2403 if (pass
!= 2 && i
!= PP_FATAL
)
2404 return DIRECTIVE_FOUND
;
2406 tline
->next
= expand_smacro(tline
->next
);
2407 tline
= tline
->next
;
2409 t
= tline
? tline
->next
: NULL
;
2411 if (tok_type_(tline
, TOK_STRING
) && !t
) {
2412 /* The line contains only a quoted string */
2414 nasm_unquote(p
, NULL
);
2415 error(severity
, "%s: %s", pp_directives
[i
], p
);
2417 /* Not a quoted string, or more than a quoted string */
2418 p
= detoken(tline
, false);
2419 error(severity
, "%s: %s", pp_directives
[i
], p
);
2422 free_tlist(origline
);
2423 return DIRECTIVE_FOUND
;
2427 if (istk
->conds
&& !emitting(istk
->conds
->state
))
2430 j
= if_condition(tline
->next
, i
);
2431 tline
->next
= NULL
; /* it got freed */
2432 j
= j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2434 cond
= nasm_malloc(sizeof(Cond
));
2435 cond
->next
= istk
->conds
;
2438 free_tlist(origline
);
2439 return DIRECTIVE_FOUND
;
2443 error(ERR_FATAL
, "`%s': no matching `%%if'", pp_directives
[i
]);
2444 switch(istk
->conds
->state
) {
2446 istk
->conds
->state
= COND_DONE
;
2453 case COND_ELSE_TRUE
:
2454 case COND_ELSE_FALSE
:
2455 error_precond(ERR_WARNING
|ERR_PASS1
,
2456 "`%%elif' after `%%else' ignored");
2457 istk
->conds
->state
= COND_NEVER
;
2462 * IMPORTANT: In the case of %if, we will already have
2463 * called expand_mmac_params(); however, if we're
2464 * processing an %elif we must have been in a
2465 * non-emitting mode, which would have inhibited
2466 * the normal invocation of expand_mmac_params().
2467 * Therefore, we have to do it explicitly here.
2469 j
= if_condition(expand_mmac_params(tline
->next
), i
);
2470 tline
->next
= NULL
; /* it got freed */
2471 istk
->conds
->state
=
2472 j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2475 free_tlist(origline
);
2476 return DIRECTIVE_FOUND
;
2480 error_precond(ERR_WARNING
|ERR_PASS1
,
2481 "trailing garbage after `%%else' ignored");
2483 error(ERR_FATAL
, "`%%else': no matching `%%if'");
2484 switch(istk
->conds
->state
) {
2487 istk
->conds
->state
= COND_ELSE_FALSE
;
2494 istk
->conds
->state
= COND_ELSE_TRUE
;
2497 case COND_ELSE_TRUE
:
2498 case COND_ELSE_FALSE
:
2499 error_precond(ERR_WARNING
|ERR_PASS1
,
2500 "`%%else' after `%%else' ignored.");
2501 istk
->conds
->state
= COND_NEVER
;
2504 free_tlist(origline
);
2505 return DIRECTIVE_FOUND
;
2509 error_precond(ERR_WARNING
|ERR_PASS1
,
2510 "trailing garbage after `%%endif' ignored");
2512 error(ERR_FATAL
, "`%%endif': no matching `%%if'");
2514 istk
->conds
= cond
->next
;
2516 free_tlist(origline
);
2517 return DIRECTIVE_FOUND
;
2523 "`%%%smacro': already defining a macro",
2524 (i
== PP_IMACRO
? "i" : ""));
2525 return DIRECTIVE_FOUND
;
2527 defining
= nasm_malloc(sizeof(MMacro
));
2528 defining
->casesense
= (i
== PP_MACRO
);
2529 if (!parse_mmacro_spec(tline
, defining
, pp_directives
[i
])) {
2530 nasm_free(defining
);
2532 return DIRECTIVE_FOUND
;
2535 mmac
= (MMacro
*) hash_findix(&mmacros
, defining
->name
);
2537 if (!strcmp(mmac
->name
, defining
->name
) &&
2538 (mmac
->nparam_min
<= defining
->nparam_max
2540 && (defining
->nparam_min
<= mmac
->nparam_max
2542 error(ERR_WARNING
|ERR_PASS1
,
2543 "redefining multi-line macro `%s'", defining
->name
);
2544 return DIRECTIVE_FOUND
;
2548 free_tlist(origline
);
2549 return DIRECTIVE_FOUND
;
2553 if (! (defining
&& defining
->name
)) {
2554 error(ERR_NONFATAL
, "`%s': not defining a macro", tline
->text
);
2555 return DIRECTIVE_FOUND
;
2557 mmhead
= (MMacro
**) hash_findi_add(&mmacros
, defining
->name
);
2558 defining
->next
= *mmhead
;
2561 free_tlist(origline
);
2562 return DIRECTIVE_FOUND
;
2570 spec
.casesense
= (i
== PP_UNMACRO
);
2571 if (!parse_mmacro_spec(tline
, &spec
, pp_directives
[i
])) {
2572 return DIRECTIVE_FOUND
;
2574 mmac_p
= (MMacro
**) hash_findi(&mmacros
, spec
.name
, NULL
);
2575 while (mmac_p
&& *mmac_p
) {
2577 if (mmac
->casesense
== spec
.casesense
&&
2578 !mstrcmp(mmac
->name
, spec
.name
, spec
.casesense
) &&
2579 mmac
->nparam_min
== spec
.nparam_min
&&
2580 mmac
->nparam_max
== spec
.nparam_max
&&
2581 mmac
->plus
== spec
.plus
) {
2582 *mmac_p
= mmac
->next
;
2585 mmac_p
= &mmac
->next
;
2588 free_tlist(origline
);
2589 free_tlist(spec
.dlist
);
2590 return DIRECTIVE_FOUND
;
2594 if (tline
->next
&& tline
->next
->type
== TOK_WHITESPACE
)
2595 tline
= tline
->next
;
2596 if (tline
->next
== NULL
) {
2597 free_tlist(origline
);
2598 error(ERR_NONFATAL
, "`%%rotate' missing rotate count");
2599 return DIRECTIVE_FOUND
;
2601 t
= expand_smacro(tline
->next
);
2603 free_tlist(origline
);
2606 tokval
.t_type
= TOKEN_INVALID
;
2608 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2611 return DIRECTIVE_FOUND
;
2613 error(ERR_WARNING
|ERR_PASS1
,
2614 "trailing garbage after expression ignored");
2615 if (!is_simple(evalresult
)) {
2616 error(ERR_NONFATAL
, "non-constant value given to `%%rotate'");
2617 return DIRECTIVE_FOUND
;
2620 while (mmac
&& !mmac
->name
) /* avoid mistaking %reps for macros */
2621 mmac
= mmac
->next_active
;
2623 error(ERR_NONFATAL
, "`%%rotate' invoked outside a macro call");
2624 } else if (mmac
->nparam
== 0) {
2626 "`%%rotate' invoked within macro without parameters");
2628 int rotate
= mmac
->rotate
+ reloc_value(evalresult
);
2630 rotate
%= (int)mmac
->nparam
;
2632 rotate
+= mmac
->nparam
;
2634 mmac
->rotate
= rotate
;
2636 return DIRECTIVE_FOUND
;
2641 tline
= tline
->next
;
2642 } while (tok_type_(tline
, TOK_WHITESPACE
));
2644 if (tok_type_(tline
, TOK_ID
) &&
2645 nasm_stricmp(tline
->text
, ".nolist") == 0) {
2648 tline
= tline
->next
;
2649 } while (tok_type_(tline
, TOK_WHITESPACE
));
2653 t
= expand_smacro(tline
);
2655 tokval
.t_type
= TOKEN_INVALID
;
2657 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2659 free_tlist(origline
);
2660 return DIRECTIVE_FOUND
;
2663 error(ERR_WARNING
|ERR_PASS1
,
2664 "trailing garbage after expression ignored");
2665 if (!is_simple(evalresult
)) {
2666 error(ERR_NONFATAL
, "non-constant value given to `%%rep'");
2667 return DIRECTIVE_FOUND
;
2669 count
= reloc_value(evalresult
) + 1;
2671 error(ERR_NONFATAL
, "`%%rep' expects a repeat count");
2674 free_tlist(origline
);
2676 tmp_defining
= defining
;
2677 defining
= nasm_malloc(sizeof(MMacro
));
2678 defining
->name
= NULL
; /* flags this macro as a %rep block */
2679 defining
->casesense
= false;
2680 defining
->plus
= false;
2681 defining
->nolist
= nolist
;
2682 defining
->in_progress
= count
;
2683 defining
->nparam_min
= defining
->nparam_max
= 0;
2684 defining
->defaults
= NULL
;
2685 defining
->dlist
= NULL
;
2686 defining
->expansion
= NULL
;
2687 defining
->next_active
= istk
->mstk
;
2688 defining
->rep_nest
= tmp_defining
;
2689 return DIRECTIVE_FOUND
;
2692 if (!defining
|| defining
->name
) {
2693 error(ERR_NONFATAL
, "`%%endrep': no matching `%%rep'");
2694 return DIRECTIVE_FOUND
;
2698 * Now we have a "macro" defined - although it has no name
2699 * and we won't be entering it in the hash tables - we must
2700 * push a macro-end marker for it on to istk->expansion.
2701 * After that, it will take care of propagating itself (a
2702 * macro-end marker line for a macro which is really a %rep
2703 * block will cause the macro to be re-expanded, complete
2704 * with another macro-end marker to ensure the process
2705 * continues) until the whole expansion is forcibly removed
2706 * from istk->expansion by a %exitrep.
2708 l
= nasm_malloc(sizeof(Line
));
2709 l
->next
= istk
->expansion
;
2710 l
->finishes
= defining
;
2712 istk
->expansion
= l
;
2714 istk
->mstk
= defining
;
2716 list
->uplevel(defining
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
2717 tmp_defining
= defining
;
2718 defining
= defining
->rep_nest
;
2719 free_tlist(origline
);
2720 return DIRECTIVE_FOUND
;
2724 * We must search along istk->expansion until we hit a
2725 * macro-end marker for a macro with no name. Then we set
2726 * its `in_progress' flag to 0.
2728 for (l
= istk
->expansion
; l
; l
= l
->next
)
2729 if (l
->finishes
&& !l
->finishes
->name
)
2733 l
->finishes
->in_progress
= 1;
2735 error(ERR_NONFATAL
, "`%%exitrep' not within `%%rep' block");
2736 free_tlist(origline
);
2737 return DIRECTIVE_FOUND
;
2743 casesense
= (i
== PP_DEFINE
|| i
== PP_XDEFINE
);
2745 tline
= tline
->next
;
2747 tline
= expand_id(tline
);
2748 if (!tline
|| (tline
->type
!= TOK_ID
&&
2749 (tline
->type
!= TOK_PREPROC_ID
||
2750 tline
->text
[1] != '$'))) {
2751 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
2753 free_tlist(origline
);
2754 return DIRECTIVE_FOUND
;
2757 ctx
= get_ctx(tline
->text
, false);
2759 mname
= tline
->text
;
2761 param_start
= tline
= tline
->next
;
2764 /* Expand the macro definition now for %xdefine and %ixdefine */
2765 if ((i
== PP_XDEFINE
) || (i
== PP_IXDEFINE
))
2766 tline
= expand_smacro(tline
);
2768 if (tok_is_(tline
, "(")) {
2770 * This macro has parameters.
2773 tline
= tline
->next
;
2777 error(ERR_NONFATAL
, "parameter identifier expected");
2778 free_tlist(origline
);
2779 return DIRECTIVE_FOUND
;
2781 if (tline
->type
!= TOK_ID
) {
2783 "`%s': parameter identifier expected",
2785 free_tlist(origline
);
2786 return DIRECTIVE_FOUND
;
2788 tline
->type
= TOK_SMAC_PARAM
+ nparam
++;
2789 tline
= tline
->next
;
2791 if (tok_is_(tline
, ",")) {
2792 tline
= tline
->next
;
2794 if (!tok_is_(tline
, ")")) {
2796 "`)' expected to terminate macro template");
2797 free_tlist(origline
);
2798 return DIRECTIVE_FOUND
;
2804 tline
= tline
->next
;
2806 if (tok_type_(tline
, TOK_WHITESPACE
))
2807 last
= tline
, tline
= tline
->next
;
2812 if (t
->type
== TOK_ID
) {
2813 for (tt
= param_start
; tt
; tt
= tt
->next
)
2814 if (tt
->type
>= TOK_SMAC_PARAM
&&
2815 !strcmp(tt
->text
, t
->text
))
2819 t
->next
= macro_start
;
2824 * Good. We now have a macro name, a parameter count, and a
2825 * token list (in reverse order) for an expansion. We ought
2826 * to be OK just to create an SMacro, store it, and let
2827 * free_tlist have the rest of the line (which we have
2828 * carefully re-terminated after chopping off the expansion
2831 define_smacro(ctx
, mname
, casesense
, nparam
, macro_start
);
2832 free_tlist(origline
);
2833 return DIRECTIVE_FOUND
;
2836 tline
= tline
->next
;
2838 tline
= expand_id(tline
);
2839 if (!tline
|| (tline
->type
!= TOK_ID
&&
2840 (tline
->type
!= TOK_PREPROC_ID
||
2841 tline
->text
[1] != '$'))) {
2842 error(ERR_NONFATAL
, "`%%undef' expects a macro identifier");
2843 free_tlist(origline
);
2844 return DIRECTIVE_FOUND
;
2847 error(ERR_WARNING
|ERR_PASS1
,
2848 "trailing garbage after macro name ignored");
2851 /* Find the context that symbol belongs to */
2852 ctx
= get_ctx(tline
->text
, false);
2853 undef_smacro(ctx
, tline
->text
);
2854 free_tlist(origline
);
2855 return DIRECTIVE_FOUND
;
2859 casesense
= (i
== PP_DEFSTR
);
2861 tline
= tline
->next
;
2863 tline
= expand_id(tline
);
2864 if (!tline
|| (tline
->type
!= TOK_ID
&&
2865 (tline
->type
!= TOK_PREPROC_ID
||
2866 tline
->text
[1] != '$'))) {
2867 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
2869 free_tlist(origline
);
2870 return DIRECTIVE_FOUND
;
2873 ctx
= get_ctx(tline
->text
, false);
2875 mname
= tline
->text
;
2877 tline
= expand_smacro(tline
->next
);
2880 while (tok_type_(tline
, TOK_WHITESPACE
))
2881 tline
= delete_Token(tline
);
2883 p
= detoken(tline
, false);
2884 macro_start
= nasm_malloc(sizeof(*macro_start
));
2885 macro_start
->next
= NULL
;
2886 macro_start
->text
= nasm_quote(p
, strlen(p
));
2887 macro_start
->type
= TOK_STRING
;
2888 macro_start
->a
.mac
= NULL
;
2892 * We now have a macro name, an implicit parameter count of
2893 * zero, and a string token to use as an expansion. Create
2894 * and store an SMacro.
2896 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2897 free_tlist(origline
);
2898 return DIRECTIVE_FOUND
;
2903 StrList
*xsl
= NULL
;
2904 StrList
**xst
= &xsl
;
2908 tline
= tline
->next
;
2910 tline
= expand_id(tline
);
2911 if (!tline
|| (tline
->type
!= TOK_ID
&&
2912 (tline
->type
!= TOK_PREPROC_ID
||
2913 tline
->text
[1] != '$'))) {
2915 "`%%pathsearch' expects a macro identifier as first parameter");
2916 free_tlist(origline
);
2917 return DIRECTIVE_FOUND
;
2919 ctx
= get_ctx(tline
->text
, false);
2921 mname
= tline
->text
;
2923 tline
= expand_smacro(tline
->next
);
2927 while (tok_type_(t
, TOK_WHITESPACE
))
2930 if (!t
|| (t
->type
!= TOK_STRING
&&
2931 t
->type
!= TOK_INTERNAL_STRING
)) {
2932 error(ERR_NONFATAL
, "`%%pathsearch' expects a file name");
2934 free_tlist(origline
);
2935 return DIRECTIVE_FOUND
; /* but we did _something_ */
2938 error(ERR_WARNING
|ERR_PASS1
,
2939 "trailing garbage after `%%pathsearch' ignored");
2941 if (t
->type
!= TOK_INTERNAL_STRING
)
2942 nasm_unquote(p
, NULL
);
2944 fp
= inc_fopen(p
, &xsl
, &xst
, true);
2947 fclose(fp
); /* Don't actually care about the file */
2949 macro_start
= nasm_malloc(sizeof(*macro_start
));
2950 macro_start
->next
= NULL
;
2951 macro_start
->text
= nasm_quote(p
, strlen(p
));
2952 macro_start
->type
= TOK_STRING
;
2953 macro_start
->a
.mac
= NULL
;
2958 * We now have a macro name, an implicit parameter count of
2959 * zero, and a string token to use as an expansion. Create
2960 * and store an SMacro.
2962 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2964 free_tlist(origline
);
2965 return DIRECTIVE_FOUND
;
2971 tline
= tline
->next
;
2973 tline
= expand_id(tline
);
2974 if (!tline
|| (tline
->type
!= TOK_ID
&&
2975 (tline
->type
!= TOK_PREPROC_ID
||
2976 tline
->text
[1] != '$'))) {
2978 "`%%strlen' expects a macro identifier as first parameter");
2979 free_tlist(origline
);
2980 return DIRECTIVE_FOUND
;
2982 ctx
= get_ctx(tline
->text
, false);
2984 mname
= tline
->text
;
2986 tline
= expand_smacro(tline
->next
);
2990 while (tok_type_(t
, TOK_WHITESPACE
))
2992 /* t should now point to the string */
2993 if (t
->type
!= TOK_STRING
) {
2995 "`%%strlen` requires string as second parameter");
2997 free_tlist(origline
);
2998 return DIRECTIVE_FOUND
;
3001 macro_start
= nasm_malloc(sizeof(*macro_start
));
3002 macro_start
->next
= NULL
;
3003 make_tok_num(macro_start
, nasm_unquote(t
->text
, NULL
));
3004 macro_start
->a
.mac
= NULL
;
3007 * We now have a macro name, an implicit parameter count of
3008 * zero, and a numeric token to use as an expansion. Create
3009 * and store an SMacro.
3011 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3013 free_tlist(origline
);
3014 return DIRECTIVE_FOUND
;
3019 tline
= tline
->next
;
3021 tline
= expand_id(tline
);
3022 if (!tline
|| (tline
->type
!= TOK_ID
&&
3023 (tline
->type
!= TOK_PREPROC_ID
||
3024 tline
->text
[1] != '$'))) {
3026 "`%%strcat' expects a macro identifier as first parameter");
3027 free_tlist(origline
);
3028 return DIRECTIVE_FOUND
;
3030 ctx
= get_ctx(tline
->text
, false);
3032 mname
= tline
->text
;
3034 tline
= expand_smacro(tline
->next
);
3038 for (t
= tline
; t
; t
= t
->next
) {
3040 case TOK_WHITESPACE
:
3043 len
+= t
->a
.len
= nasm_unquote(t
->text
, NULL
);
3046 if (!strcmp(t
->text
, ",")) /* permit comma separators */
3048 /* else fall through */
3051 "non-string passed to `%%strcat' (%d)", t
->type
);
3053 free_tlist(origline
);
3054 return DIRECTIVE_FOUND
;
3058 p
= pp
= nasm_malloc(len
);
3060 for (t
= tline
; t
; t
= t
->next
) {
3061 if (t
->type
== TOK_STRING
) {
3062 memcpy(p
, t
->text
, t
->a
.len
);
3068 * We now have a macro name, an implicit parameter count of
3069 * zero, and a numeric token to use as an expansion. Create
3070 * and store an SMacro.
3072 macro_start
= new_Token(NULL
, TOK_STRING
, NULL
, 0);
3073 macro_start
->text
= nasm_quote(pp
, len
);
3075 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3077 free_tlist(origline
);
3078 return DIRECTIVE_FOUND
;
3087 tline
= tline
->next
;
3089 tline
= expand_id(tline
);
3090 if (!tline
|| (tline
->type
!= TOK_ID
&&
3091 (tline
->type
!= TOK_PREPROC_ID
||
3092 tline
->text
[1] != '$'))) {
3094 "`%%substr' expects a macro identifier as first parameter");
3095 free_tlist(origline
);
3096 return DIRECTIVE_FOUND
;
3098 ctx
= get_ctx(tline
->text
, false);
3100 mname
= tline
->text
;
3102 tline
= expand_smacro(tline
->next
);
3106 while (tok_type_(t
, TOK_WHITESPACE
))
3109 /* t should now point to the string */
3110 if (t
->type
!= TOK_STRING
) {
3112 "`%%substr` requires string as second parameter");
3114 free_tlist(origline
);
3115 return DIRECTIVE_FOUND
;
3120 tokval
.t_type
= TOKEN_INVALID
;
3121 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
,
3125 free_tlist(origline
);
3126 return DIRECTIVE_FOUND
;
3127 } else if (!is_simple(evalresult
)) {
3128 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
3130 free_tlist(origline
);
3131 return DIRECTIVE_FOUND
;
3133 a1
= evalresult
->value
-1;
3135 while (tok_type_(tt
, TOK_WHITESPACE
))
3138 a2
= 1; /* Backwards compatibility: one character */
3140 tokval
.t_type
= TOKEN_INVALID
;
3141 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
,
3145 free_tlist(origline
);
3146 return DIRECTIVE_FOUND
;
3147 } else if (!is_simple(evalresult
)) {
3148 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
3150 free_tlist(origline
);
3151 return DIRECTIVE_FOUND
;
3153 a2
= evalresult
->value
;
3156 len
= nasm_unquote(t
->text
, NULL
);
3159 if (a1
+a2
> (int64_t)len
)
3162 macro_start
= nasm_malloc(sizeof(*macro_start
));
3163 macro_start
->next
= NULL
;
3164 macro_start
->text
= nasm_quote((a1
< 0) ? "" : t
->text
+a1
, a2
);
3165 macro_start
->type
= TOK_STRING
;
3166 macro_start
->a
.mac
= NULL
;
3169 * We now have a macro name, an implicit parameter count of
3170 * zero, and a numeric token to use as an expansion. Create
3171 * and store an SMacro.
3173 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3175 free_tlist(origline
);
3176 return DIRECTIVE_FOUND
;
3181 casesense
= (i
== PP_ASSIGN
);
3183 tline
= tline
->next
;
3185 tline
= expand_id(tline
);
3186 if (!tline
|| (tline
->type
!= TOK_ID
&&
3187 (tline
->type
!= TOK_PREPROC_ID
||
3188 tline
->text
[1] != '$'))) {
3190 "`%%%sassign' expects a macro identifier",
3191 (i
== PP_IASSIGN
? "i" : ""));
3192 free_tlist(origline
);
3193 return DIRECTIVE_FOUND
;
3195 ctx
= get_ctx(tline
->text
, false);
3197 mname
= tline
->text
;
3199 tline
= expand_smacro(tline
->next
);
3204 tokval
.t_type
= TOKEN_INVALID
;
3206 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
3209 free_tlist(origline
);
3210 return DIRECTIVE_FOUND
;
3214 error(ERR_WARNING
|ERR_PASS1
,
3215 "trailing garbage after expression ignored");
3217 if (!is_simple(evalresult
)) {
3219 "non-constant value given to `%%%sassign'",
3220 (i
== PP_IASSIGN
? "i" : ""));
3221 free_tlist(origline
);
3222 return DIRECTIVE_FOUND
;
3225 macro_start
= nasm_malloc(sizeof(*macro_start
));
3226 macro_start
->next
= NULL
;
3227 make_tok_num(macro_start
, reloc_value(evalresult
));
3228 macro_start
->a
.mac
= NULL
;
3231 * We now have a macro name, an implicit parameter count of
3232 * zero, and a numeric token to use as an expansion. Create
3233 * and store an SMacro.
3235 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3236 free_tlist(origline
);
3237 return DIRECTIVE_FOUND
;
3241 * Syntax is `%line nnn[+mmm] [filename]'
3243 tline
= tline
->next
;
3245 if (!tok_type_(tline
, TOK_NUMBER
)) {
3246 error(ERR_NONFATAL
, "`%%line' expects line number");
3247 free_tlist(origline
);
3248 return DIRECTIVE_FOUND
;
3250 k
= readnum(tline
->text
, &err
);
3252 tline
= tline
->next
;
3253 if (tok_is_(tline
, "+")) {
3254 tline
= tline
->next
;
3255 if (!tok_type_(tline
, TOK_NUMBER
)) {
3256 error(ERR_NONFATAL
, "`%%line' expects line increment");
3257 free_tlist(origline
);
3258 return DIRECTIVE_FOUND
;
3260 m
= readnum(tline
->text
, &err
);
3261 tline
= tline
->next
;
3267 nasm_free(src_set_fname(detoken(tline
, false)));
3269 free_tlist(origline
);
3270 return DIRECTIVE_FOUND
;
3274 "preprocessor directive `%s' not yet implemented",
3276 return DIRECTIVE_FOUND
;
3281 * Ensure that a macro parameter contains a condition code and
3282 * nothing else. Return the condition code index if so, or -1
3285 static int find_cc(Token
* t
)
3291 return -1; /* Probably a %+ without a space */
3294 if (t
->type
!= TOK_ID
)
3298 if (tt
&& (tt
->type
!= TOK_OTHER
|| strcmp(tt
->text
, ",")))
3302 j
= elements(conditions
);
3305 m
= nasm_stricmp(t
->text
, conditions
[k
]);
3321 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3322 * %-n) and MMacro-local identifiers (%%foo) as well as
3323 * macro indirection (%[...]).
3325 static Token
*expand_mmac_params(Token
* tline
)
3327 Token
*t
, *tt
, **tail
, *thead
;
3333 if (tline
->type
== TOK_PREPROC_ID
&&
3334 (((tline
->text
[1] == '+' || tline
->text
[1] == '-')
3335 && tline
->text
[2]) || tline
->text
[1] == '%'
3336 || (tline
->text
[1] >= '0' && tline
->text
[1] <= '9'))) {
3338 int type
= 0, cc
; /* type = 0 to placate optimisers */
3345 tline
= tline
->next
;
3348 while (mac
&& !mac
->name
) /* avoid mistaking %reps for macros */
3349 mac
= mac
->next_active
;
3351 error(ERR_NONFATAL
, "`%s': not in a macro call", t
->text
);
3353 switch (t
->text
[1]) {
3355 * We have to make a substitution of one of the
3356 * forms %1, %-1, %+1, %%foo, %0.
3360 snprintf(tmpbuf
, sizeof(tmpbuf
), "%d", mac
->nparam
);
3361 text
= nasm_strdup(tmpbuf
);
3365 snprintf(tmpbuf
, sizeof(tmpbuf
), "..@%"PRIu64
".",
3367 text
= nasm_strcat(tmpbuf
, t
->text
+ 2);
3370 n
= atoi(t
->text
+ 2) - 1;
3371 if (n
>= mac
->nparam
)
3374 if (mac
->nparam
> 1)
3375 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3376 tt
= mac
->params
[n
];
3381 "macro parameter %d is not a condition code",
3386 if (inverse_ccs
[cc
] == -1) {
3388 "condition code `%s' is not invertible",
3392 text
= nasm_strdup(conditions
[inverse_ccs
[cc
]]);
3396 n
= atoi(t
->text
+ 2) - 1;
3397 if (n
>= mac
->nparam
)
3400 if (mac
->nparam
> 1)
3401 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3402 tt
= mac
->params
[n
];
3407 "macro parameter %d is not a condition code",
3412 text
= nasm_strdup(conditions
[cc
]);
3416 n
= atoi(t
->text
+ 1) - 1;
3417 if (n
>= mac
->nparam
)
3420 if (mac
->nparam
> 1)
3421 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3422 tt
= mac
->params
[n
];
3425 for (i
= 0; i
< mac
->paramlen
[n
]; i
++) {
3426 *tail
= new_Token(NULL
, tt
->type
, tt
->text
, 0);
3427 tail
= &(*tail
)->next
;
3431 text
= NULL
; /* we've done it here */
3445 } else if (tline
->type
== TOK_INDIRECT
) {
3447 tline
= tline
->next
;
3448 tt
= tokenize(t
->text
);
3449 tt
= expand_mmac_params(tt
);
3450 tt
= expand_smacro(tt
);
3453 tt
->a
.mac
= NULL
; /* Necessary? */
3460 tline
= tline
->next
;
3467 /* Now handle token pasting... */
3469 while (t
&& (tt
= t
->next
)) {
3471 case TOK_WHITESPACE
:
3472 if (tt
->type
== TOK_WHITESPACE
) {
3473 t
->next
= delete_Token(tt
);
3480 if (tt
->type
== t
->type
|| tt
->type
== TOK_NUMBER
) {
3481 char *tmp
= nasm_strcat(t
->text
, tt
->text
);
3484 t
->next
= delete_Token(tt
);
3498 * Expand all single-line macro calls made in the given line.
3499 * Return the expanded version of the line. The original is deemed
3500 * to be destroyed in the process. (In reality we'll just move
3501 * Tokens from input to output a lot of the time, rather than
3502 * actually bothering to destroy and replicate.)
3504 #define DEADMAN_LIMIT (1 << 20)
3506 static Token
*expand_smacro(Token
* tline
)
3508 Token
*t
, *tt
, *mstart
, **tail
, *thead
;
3509 struct hash_table
*smtbl
;
3510 SMacro
*head
= NULL
, *m
;
3513 unsigned int nparam
, sparam
;
3514 int brackets
, rescan
;
3515 Token
*org_tline
= tline
;
3518 int deadman
= DEADMAN_LIMIT
;
3521 * Trick: we should avoid changing the start token pointer since it can
3522 * be contained in "next" field of other token. Because of this
3523 * we allocate a copy of first token and work with it; at the end of
3524 * routine we copy it back
3528 new_Token(org_tline
->next
, org_tline
->type
, org_tline
->text
,
3530 tline
->a
.mac
= org_tline
->a
.mac
;
3531 nasm_free(org_tline
->text
);
3532 org_tline
->text
= NULL
;
3539 while (tline
) { /* main token loop */
3541 error(ERR_NONFATAL
, "interminable macro recursion");
3545 if ((mname
= tline
->text
)) {
3546 /* if this token is a local macro, look in local context */
3549 if (tline
->type
== TOK_ID
|| tline
->type
== TOK_PREPROC_ID
) {
3550 ctx
= get_ctx(mname
, true);
3552 smtbl
= &ctx
->localmac
;
3554 head
= (SMacro
*) hash_findix(smtbl
, mname
);
3557 * We've hit an identifier. As in is_mmacro below, we first
3558 * check whether the identifier is a single-line macro at
3559 * all, then think about checking for parameters if
3562 for (m
= head
; m
; m
= m
->next
)
3563 if (!mstrcmp(m
->name
, mname
, m
->casesense
))
3569 if (m
->nparam
== 0) {
3571 * Simple case: the macro is parameterless. Discard the
3572 * one token that the macro call took, and push the
3573 * expansion back on the to-do stack.
3575 if (!m
->expansion
) {
3576 if (!strcmp("__FILE__", m
->name
)) {
3579 src_get(&num
, &file
);
3580 tline
->text
= nasm_quote(file
, strlen(file
));
3581 tline
->type
= TOK_STRING
;
3585 if (!strcmp("__LINE__", m
->name
)) {
3586 nasm_free(tline
->text
);
3587 make_tok_num(tline
, src_get_linnum());
3590 if (!strcmp("__BITS__", m
->name
)) {
3591 nasm_free(tline
->text
);
3592 make_tok_num(tline
, globalbits
);
3595 tline
= delete_Token(tline
);
3600 * Complicated case: at least one macro with this name
3601 * exists and takes parameters. We must find the
3602 * parameters in the call, count them, find the SMacro
3603 * that corresponds to that form of the macro call, and
3604 * substitute for the parameters when we expand. What a
3607 /*tline = tline->next;
3608 skip_white_(tline); */
3611 while (tok_type_(t
, TOK_SMAC_END
)) {
3612 t
->a
.mac
->in_progress
= false;
3614 t
= tline
->next
= delete_Token(t
);
3617 } while (tok_type_(tline
, TOK_WHITESPACE
));
3618 if (!tok_is_(tline
, "(")) {
3620 * This macro wasn't called with parameters: ignore
3621 * the call. (Behaviour borrowed from gnu cpp.)
3630 sparam
= PARAM_DELTA
;
3631 params
= nasm_malloc(sparam
* sizeof(Token
*));
3632 params
[0] = tline
->next
;
3633 paramsize
= nasm_malloc(sparam
* sizeof(int));
3635 while (true) { /* parameter loop */
3637 * For some unusual expansions
3638 * which concatenates function call
3641 while (tok_type_(t
, TOK_SMAC_END
)) {
3642 t
->a
.mac
->in_progress
= false;
3644 t
= tline
->next
= delete_Token(t
);
3650 "macro call expects terminating `)'");
3653 if (tline
->type
== TOK_WHITESPACE
3655 if (paramsize
[nparam
])
3658 params
[nparam
] = tline
->next
;
3659 continue; /* parameter loop */
3661 if (tline
->type
== TOK_OTHER
3662 && tline
->text
[1] == 0) {
3663 char ch
= tline
->text
[0];
3664 if (ch
== ',' && !paren
&& brackets
<= 0) {
3665 if (++nparam
>= sparam
) {
3666 sparam
+= PARAM_DELTA
;
3667 params
= nasm_realloc(params
,
3672 nasm_realloc(paramsize
,
3676 params
[nparam
] = tline
->next
;
3677 paramsize
[nparam
] = 0;
3679 continue; /* parameter loop */
3682 (brackets
> 0 || (brackets
== 0 &&
3683 !paramsize
[nparam
])))
3685 if (!(brackets
++)) {
3686 params
[nparam
] = tline
->next
;
3687 continue; /* parameter loop */
3690 if (ch
== '}' && brackets
> 0)
3691 if (--brackets
== 0) {
3693 continue; /* parameter loop */
3695 if (ch
== '(' && !brackets
)
3697 if (ch
== ')' && brackets
<= 0)
3703 error(ERR_NONFATAL
, "braces do not "
3704 "enclose all of macro parameter");
3706 paramsize
[nparam
] += white
+ 1;
3708 } /* parameter loop */
3710 while (m
&& (m
->nparam
!= nparam
||
3711 mstrcmp(m
->name
, mname
,
3715 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MNP
,
3716 "macro `%s' exists, "
3717 "but not taking %d parameters",
3718 mstart
->text
, nparam
);
3721 if (m
&& m
->in_progress
)
3723 if (!m
) { /* in progess or didn't find '(' or wrong nparam */
3725 * Design question: should we handle !tline, which
3726 * indicates missing ')' here, or expand those
3727 * macros anyway, which requires the (t) test a few
3731 nasm_free(paramsize
);
3735 * Expand the macro: we are placed on the last token of the
3736 * call, so that we can easily split the call from the
3737 * following tokens. We also start by pushing an SMAC_END
3738 * token for the cycle removal.
3745 tt
= new_Token(tline
, TOK_SMAC_END
, NULL
, 0);
3747 m
->in_progress
= true;
3749 for (t
= m
->expansion
; t
; t
= t
->next
) {
3750 if (t
->type
>= TOK_SMAC_PARAM
) {
3751 Token
*pcopy
= tline
, **ptail
= &pcopy
;
3755 ttt
= params
[t
->type
- TOK_SMAC_PARAM
];
3756 for (i
= paramsize
[t
->type
- TOK_SMAC_PARAM
];
3759 new_Token(tline
, ttt
->type
, ttt
->text
,
3765 } else if (t
->type
== TOK_PREPROC_Q
) {
3766 tt
= new_Token(tline
, TOK_ID
, mname
, 0);
3768 } else if (t
->type
== TOK_PREPROC_QQ
) {
3769 tt
= new_Token(tline
, TOK_ID
, m
->name
, 0);
3772 tt
= new_Token(tline
, t
->type
, t
->text
, 0);
3778 * Having done that, get rid of the macro call, and clean
3779 * up the parameters.
3782 nasm_free(paramsize
);
3784 continue; /* main token loop */
3789 if (tline
->type
== TOK_SMAC_END
) {
3790 tline
->a
.mac
->in_progress
= false;
3791 tline
= delete_Token(tline
);
3794 tline
= tline
->next
;
3802 * Now scan the entire line and look for successive TOK_IDs that resulted
3803 * after expansion (they can't be produced by tokenize()). The successive
3804 * TOK_IDs should be concatenated.
3805 * Also we look for %+ tokens and concatenate the tokens before and after
3806 * them (without white spaces in between).
3811 while (t
&& t
->type
!= TOK_ID
&& t
->type
!= TOK_PREPROC_ID
)
3815 if (t
->next
->type
== TOK_ID
||
3816 t
->next
->type
== TOK_PREPROC_ID
||
3817 t
->next
->type
== TOK_NUMBER
) {
3818 char *p
= nasm_strcat(t
->text
, t
->next
->text
);
3820 t
->next
= delete_Token(t
->next
);
3823 } else if (t
->next
->type
== TOK_WHITESPACE
&& t
->next
->next
&&
3824 t
->next
->next
->type
== TOK_PREPROC_ID
&&
3825 strcmp(t
->next
->next
->text
, "%+") == 0) {
3826 /* free the next whitespace, the %+ token and next whitespace */
3828 for (i
= 1; i
<= 3; i
++) {
3830 || (i
!= 2 && t
->next
->type
!= TOK_WHITESPACE
))
3832 t
->next
= delete_Token(t
->next
);
3837 /* If we concatenaded something, re-scan the line for macros */
3845 *org_tline
= *thead
;
3846 /* since we just gave text to org_line, don't free it */
3848 delete_Token(thead
);
3850 /* the expression expanded to empty line;
3851 we can't return NULL for some reasons
3852 we just set the line to a single WHITESPACE token. */
3853 memset(org_tline
, 0, sizeof(*org_tline
));
3854 org_tline
->text
= NULL
;
3855 org_tline
->type
= TOK_WHITESPACE
;
3864 * Similar to expand_smacro but used exclusively with macro identifiers
3865 * right before they are fetched in. The reason is that there can be
3866 * identifiers consisting of several subparts. We consider that if there
3867 * are more than one element forming the name, user wants a expansion,
3868 * otherwise it will be left as-is. Example:
3872 * the identifier %$abc will be left as-is so that the handler for %define
3873 * will suck it and define the corresponding value. Other case:
3875 * %define _%$abc cde
3877 * In this case user wants name to be expanded *before* %define starts
3878 * working, so we'll expand %$abc into something (if it has a value;
3879 * otherwise it will be left as-is) then concatenate all successive
3882 static Token
*expand_id(Token
* tline
)
3884 Token
*cur
, *oldnext
= NULL
;
3886 if (!tline
|| !tline
->next
)
3891 (cur
->next
->type
== TOK_ID
||
3892 cur
->next
->type
== TOK_PREPROC_ID
3893 || cur
->next
->type
== TOK_NUMBER
))
3896 /* If identifier consists of just one token, don't expand */
3901 oldnext
= cur
->next
; /* Detach the tail past identifier */
3902 cur
->next
= NULL
; /* so that expand_smacro stops here */
3905 tline
= expand_smacro(tline
);
3908 /* expand_smacro possibly changhed tline; re-scan for EOL */
3910 while (cur
&& cur
->next
)
3913 cur
->next
= oldnext
;
3920 * Determine whether the given line constitutes a multi-line macro
3921 * call, and return the MMacro structure called if so. Doesn't have
3922 * to check for an initial label - that's taken care of in
3923 * expand_mmacro - but must check numbers of parameters. Guaranteed
3924 * to be called with tline->type == TOK_ID, so the putative macro
3925 * name is easy to find.
3927 static MMacro
*is_mmacro(Token
* tline
, Token
*** params_array
)
3933 head
= (MMacro
*) hash_findix(&mmacros
, tline
->text
);
3936 * Efficiency: first we see if any macro exists with the given
3937 * name. If not, we can return NULL immediately. _Then_ we
3938 * count the parameters, and then we look further along the
3939 * list if necessary to find the proper MMacro.
3941 for (m
= head
; m
; m
= m
->next
)
3942 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
3948 * OK, we have a potential macro. Count and demarcate the
3951 count_mmac_params(tline
->next
, &nparam
, ¶ms
);
3954 * So we know how many parameters we've got. Find the MMacro
3955 * structure that handles this number.
3958 if (m
->nparam_min
<= nparam
3959 && (m
->plus
|| nparam
<= m
->nparam_max
)) {
3961 * This one is right. Just check if cycle removal
3962 * prohibits us using it before we actually celebrate...
3964 if (m
->in_progress
) {
3967 "self-reference in multi-line macro `%s'", m
->name
);
3973 * It's right, and we can use it. Add its default
3974 * parameters to the end of our list if necessary.
3976 if (m
->defaults
&& nparam
< m
->nparam_min
+ m
->ndefs
) {
3978 nasm_realloc(params
,
3979 ((m
->nparam_min
+ m
->ndefs
+
3980 1) * sizeof(*params
)));
3981 while (nparam
< m
->nparam_min
+ m
->ndefs
) {
3982 params
[nparam
] = m
->defaults
[nparam
- m
->nparam_min
];
3987 * If we've gone over the maximum parameter count (and
3988 * we're in Plus mode), ignore parameters beyond
3991 if (m
->plus
&& nparam
> m
->nparam_max
)
3992 nparam
= m
->nparam_max
;
3994 * Then terminate the parameter list, and leave.
3996 if (!params
) { /* need this special case */
3997 params
= nasm_malloc(sizeof(*params
));
4000 params
[nparam
] = NULL
;
4001 *params_array
= params
;
4005 * This one wasn't right: look for the next one with the
4008 for (m
= m
->next
; m
; m
= m
->next
)
4009 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
4014 * After all that, we didn't find one with the right number of
4015 * parameters. Issue a warning, and fail to expand the macro.
4017 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MNP
,
4018 "macro `%s' exists, but not taking %d parameters",
4019 tline
->text
, nparam
);
4025 * Expand the multi-line macro call made by the given line, if
4026 * there is one to be expanded. If there is, push the expansion on
4027 * istk->expansion and return 1. Otherwise return 0.
4029 static int expand_mmacro(Token
* tline
)
4031 Token
*startline
= tline
;
4032 Token
*label
= NULL
;
4033 int dont_prepend
= 0;
4034 Token
**params
, *t
, *mtok
, *tt
;
4037 int i
, nparam
, *paramlen
;
4042 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
4043 if (!tok_type_(t
, TOK_ID
) && !tok_type_(t
, TOK_PREPROC_ID
))
4046 m
= is_mmacro(t
, ¶ms
);
4052 * We have an id which isn't a macro call. We'll assume
4053 * it might be a label; we'll also check to see if a
4054 * colon follows it. Then, if there's another id after
4055 * that lot, we'll check it again for macro-hood.
4059 if (tok_type_(t
, TOK_WHITESPACE
))
4060 last
= t
, t
= t
->next
;
4061 if (tok_is_(t
, ":")) {
4063 last
= t
, t
= t
->next
;
4064 if (tok_type_(t
, TOK_WHITESPACE
))
4065 last
= t
, t
= t
->next
;
4067 if (!tok_type_(t
, TOK_ID
) || (m
= is_mmacro(t
, ¶ms
)) == NULL
)
4075 * Fix up the parameters: this involves stripping leading and
4076 * trailing whitespace, then stripping braces if they are
4079 for (nparam
= 0; params
[nparam
]; nparam
++) ;
4080 paramlen
= nparam
? nasm_malloc(nparam
* sizeof(*paramlen
)) : NULL
;
4082 for (i
= 0; params
[i
]; i
++) {
4084 int comma
= (!m
->plus
|| i
< nparam
- 1);
4088 if (tok_is_(t
, "{"))
4089 t
= t
->next
, brace
= true, comma
= false;
4093 if (comma
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, ","))
4094 break; /* ... because we have hit a comma */
4095 if (comma
&& t
->type
== TOK_WHITESPACE
4096 && tok_is_(t
->next
, ","))
4097 break; /* ... or a space then a comma */
4098 if (brace
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, "}"))
4099 break; /* ... or a brace */
4106 * OK, we have a MMacro structure together with a set of
4107 * parameters. We must now go through the expansion and push
4108 * copies of each Line on to istk->expansion. Substitution of
4109 * parameter tokens and macro-local tokens doesn't get done
4110 * until the single-line macro substitution process; this is
4111 * because delaying them allows us to change the semantics
4112 * later through %rotate.
4114 * First, push an end marker on to istk->expansion, mark this
4115 * macro as in progress, and set up its invocation-specific
4118 ll
= nasm_malloc(sizeof(Line
));
4119 ll
->next
= istk
->expansion
;
4122 istk
->expansion
= ll
;
4124 m
->in_progress
= true;
4129 m
->paramlen
= paramlen
;
4130 m
->unique
= unique
++;
4133 m
->next_active
= istk
->mstk
;
4136 for (l
= m
->expansion
; l
; l
= l
->next
) {
4139 ll
= nasm_malloc(sizeof(Line
));
4140 ll
->finishes
= NULL
;
4141 ll
->next
= istk
->expansion
;
4142 istk
->expansion
= ll
;
4145 for (t
= l
->first
; t
; t
= t
->next
) {
4149 tt
= *tail
= new_Token(NULL
, TOK_ID
, mname
, 0);
4151 case TOK_PREPROC_QQ
:
4152 tt
= *tail
= new_Token(NULL
, TOK_ID
, m
->name
, 0);
4154 case TOK_PREPROC_ID
:
4155 if (t
->text
[1] == '0' && t
->text
[2] == '0') {
4163 tt
= *tail
= new_Token(NULL
, x
->type
, x
->text
, 0);
4172 * If we had a label, push it on as the first line of
4173 * the macro expansion.
4176 if (dont_prepend
< 0)
4177 free_tlist(startline
);
4179 ll
= nasm_malloc(sizeof(Line
));
4180 ll
->finishes
= NULL
;
4181 ll
->next
= istk
->expansion
;
4182 istk
->expansion
= ll
;
4183 ll
->first
= startline
;
4184 if (!dont_prepend
) {
4186 label
= label
->next
;
4187 label
->next
= tt
= new_Token(NULL
, TOK_OTHER
, ":", 0);
4192 list
->uplevel(m
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
4197 /* The function that actually does the error reporting */
4198 static void verror(int severity
, const char *fmt
, va_list arg
)
4202 vsnprintf(buff
, sizeof(buff
), fmt
, arg
);
4204 if (istk
&& istk
->mstk
&& istk
->mstk
->name
)
4205 _error(severity
, "(%s:%d) %s", istk
->mstk
->name
,
4206 istk
->mstk
->lineno
, buff
);
4208 _error(severity
, "%s", buff
);
4212 * Since preprocessor always operate only on the line that didn't
4213 * arrived yet, we should always use ERR_OFFBY1.
4215 static void error(int severity
, const char *fmt
, ...)
4219 /* If we're in a dead branch of IF or something like it, ignore the error */
4220 if (istk
&& istk
->conds
&& !emitting(istk
->conds
->state
))
4224 verror(severity
, fmt
, arg
);
4229 * Because %else etc are evaluated in the state context
4230 * of the previous branch, errors might get lost with error():
4231 * %if 0 ... %else trailing garbage ... %endif
4232 * So %else etc should report errors with this function.
4234 static void error_precond(int severity
, const char *fmt
, ...)
4238 /* Only ignore the error if it's really in a dead branch */
4239 if (istk
&& istk
->conds
&& istk
->conds
->state
== COND_NEVER
)
4243 verror(severity
, fmt
, arg
);
4248 pp_reset(char *file
, int apass
, efunc errfunc
, evalfunc eval
,
4249 ListGen
* listgen
, StrList
**deplist
)
4255 istk
= nasm_malloc(sizeof(Include
));
4258 istk
->expansion
= NULL
;
4260 istk
->fp
= fopen(file
, "r");
4262 src_set_fname(nasm_strdup(file
));
4266 error(ERR_FATAL
|ERR_NOFILE
, "unable to open input file `%s'",
4269 nested_mac_count
= 0;
4270 nested_rep_count
= 0;
4273 if (tasm_compatible_mode
) {
4274 stdmacpos
= nasm_stdmac
;
4276 stdmacpos
= nasm_stdmac_after_tasm
;
4278 any_extrastdmac
= extrastdmac
&& *extrastdmac
;
4284 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4285 * The caller, however, will also pass in 3 for preprocess-only so
4286 * we can set __PASS__ accordingly.
4288 pass
= apass
> 2 ? 2 : apass
;
4290 dephead
= deptail
= deplist
;
4292 StrList
*sl
= nasm_malloc(strlen(file
)+1+sizeof sl
->next
);
4294 strcpy(sl
->str
, file
);
4296 deptail
= &sl
->next
;
4300 * Define the __PASS__ macro. This is defined here unlike
4301 * all the other builtins, because it is special -- it varies between
4304 t
= nasm_malloc(sizeof(*t
));
4306 make_tok_num(t
, apass
);
4308 define_smacro(NULL
, "__PASS__", true, 0, t
);
4311 static char *pp_getline(void)
4318 * Fetch a tokenized line, either from the macro-expansion
4319 * buffer or from the input file.
4322 while (istk
->expansion
&& istk
->expansion
->finishes
) {
4323 Line
*l
= istk
->expansion
;
4324 if (!l
->finishes
->name
&& l
->finishes
->in_progress
> 1) {
4328 * This is a macro-end marker for a macro with no
4329 * name, which means it's not really a macro at all
4330 * but a %rep block, and the `in_progress' field is
4331 * more than 1, meaning that we still need to
4332 * repeat. (1 means the natural last repetition; 0
4333 * means termination by %exitrep.) We have
4334 * therefore expanded up to the %endrep, and must
4335 * push the whole block on to the expansion buffer
4336 * again. We don't bother to remove the macro-end
4337 * marker: we'd only have to generate another one
4340 l
->finishes
->in_progress
--;
4341 for (l
= l
->finishes
->expansion
; l
; l
= l
->next
) {
4342 Token
*t
, *tt
, **tail
;
4344 ll
= nasm_malloc(sizeof(Line
));
4345 ll
->next
= istk
->expansion
;
4346 ll
->finishes
= NULL
;
4350 for (t
= l
->first
; t
; t
= t
->next
) {
4351 if (t
->text
|| t
->type
== TOK_WHITESPACE
) {
4353 new_Token(NULL
, t
->type
, t
->text
, 0);
4358 istk
->expansion
= ll
;
4362 * Check whether a `%rep' was started and not ended
4363 * within this macro expansion. This can happen and
4364 * should be detected. It's a fatal error because
4365 * I'm too confused to work out how to recover
4371 "defining with name in expansion");
4372 else if (istk
->mstk
->name
)
4374 "`%%rep' without `%%endrep' within"
4375 " expansion of macro `%s'",
4380 * FIXME: investigate the relationship at this point between
4381 * istk->mstk and l->finishes
4384 MMacro
*m
= istk
->mstk
;
4385 istk
->mstk
= m
->next_active
;
4388 * This was a real macro call, not a %rep, and
4389 * therefore the parameter information needs to
4392 nasm_free(m
->params
);
4393 free_tlist(m
->iline
);
4394 nasm_free(m
->paramlen
);
4395 l
->finishes
->in_progress
= false;
4399 istk
->expansion
= l
->next
;
4401 list
->downlevel(LIST_MACRO
);
4404 while (1) { /* until we get a line we can use */
4406 if (istk
->expansion
) { /* from a macro expansion */
4408 Line
*l
= istk
->expansion
;
4410 istk
->mstk
->lineno
++;
4412 istk
->expansion
= l
->next
;
4414 p
= detoken(tline
, false);
4415 list
->line(LIST_MACRO
, p
);
4420 if (line
) { /* from the current input file */
4421 line
= prepreproc(line
);
4422 tline
= tokenize(line
);
4427 * The current file has ended; work down the istk
4434 "expected `%%endif' before end of file");
4435 /* only set line and file name if there's a next node */
4437 src_set_linnum(i
->lineno
);
4438 nasm_free(src_set_fname(i
->fname
));
4441 list
->downlevel(LIST_INCLUDE
);
4445 if (istk
->expansion
&& istk
->expansion
->finishes
)
4451 * We must expand MMacro parameters and MMacro-local labels
4452 * _before_ we plunge into directive processing, to cope
4453 * with things like `%define something %1' such as STRUC
4454 * uses. Unless we're _defining_ a MMacro, in which case
4455 * those tokens should be left alone to go into the
4456 * definition; and unless we're in a non-emitting
4457 * condition, in which case we don't want to meddle with
4460 if (!defining
&& !(istk
->conds
&& !emitting(istk
->conds
->state
))
4461 && !(istk
->mstk
&& !istk
->mstk
->in_progress
)) {
4462 tline
= expand_mmac_params(tline
);
4466 * Check the line to see if it's a preprocessor directive.
4468 if (do_directive(tline
) == DIRECTIVE_FOUND
) {
4470 } else if (defining
) {
4472 * We're defining a multi-line macro. We emit nothing
4474 * shove the tokenized line on to the macro definition.
4476 Line
*l
= nasm_malloc(sizeof(Line
));
4477 l
->next
= defining
->expansion
;
4480 defining
->expansion
= l
;
4482 } else if (istk
->conds
&& !emitting(istk
->conds
->state
)) {
4484 * We're in a non-emitting branch of a condition block.
4485 * Emit nothing at all, not even a blank line: when we
4486 * emerge from the condition we'll give a line-number
4487 * directive so we keep our place correctly.
4491 } else if (istk
->mstk
&& !istk
->mstk
->in_progress
) {
4493 * We're in a %rep block which has been terminated, so
4494 * we're walking through to the %endrep without
4495 * emitting anything. Emit nothing at all, not even a
4496 * blank line: when we emerge from the %rep block we'll
4497 * give a line-number directive so we keep our place
4503 tline
= expand_smacro(tline
);
4504 if (!expand_mmacro(tline
)) {
4506 * De-tokenize the line again, and emit it.
4508 line
= detoken(tline
, true);
4512 continue; /* expand_mmacro calls free_tlist */
4520 static void pp_cleanup(int pass
)
4523 if(defining
->name
) {
4525 "end of file while still defining macro `%s'",
4528 error(ERR_NONFATAL
, "end of file while still in %%rep");
4531 free_mmacro(defining
);
4540 nasm_free(i
->fname
);
4545 nasm_free(src_set_fname(NULL
));
4550 while ((i
= ipath
)) {
4559 void pp_include_path(char *path
)
4563 i
= nasm_malloc(sizeof(IncPath
));
4564 i
->path
= path
? nasm_strdup(path
) : NULL
;
4567 if (ipath
!= NULL
) {
4569 while (j
->next
!= NULL
)
4577 void pp_pre_include(char *fname
)
4579 Token
*inc
, *space
, *name
;
4582 name
= new_Token(NULL
, TOK_INTERNAL_STRING
, fname
, 0);
4583 space
= new_Token(name
, TOK_WHITESPACE
, NULL
, 0);
4584 inc
= new_Token(space
, TOK_PREPROC_ID
, "%include", 0);
4586 l
= nasm_malloc(sizeof(Line
));
4593 void pp_pre_define(char *definition
)
4599 equals
= strchr(definition
, '=');
4600 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
4601 def
= new_Token(space
, TOK_PREPROC_ID
, "%define", 0);
4604 space
->next
= tokenize(definition
);
4608 l
= nasm_malloc(sizeof(Line
));
4615 void pp_pre_undefine(char *definition
)
4620 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
4621 def
= new_Token(space
, TOK_PREPROC_ID
, "%undef", 0);
4622 space
->next
= tokenize(definition
);
4624 l
= nasm_malloc(sizeof(Line
));
4632 * Added by Keith Kanios:
4634 * This function is used to assist with "runtime" preprocessor
4635 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4637 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4638 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4641 void pp_runtime(char *definition
)
4645 def
= tokenize(definition
);
4646 if(do_directive(def
) == NO_DIRECTIVE_FOUND
)
4651 void pp_extra_stdmac(macros_t
*macros
)
4653 extrastdmac
= macros
;
4656 static void make_tok_num(Token
* tok
, int64_t val
)
4659 snprintf(numbuf
, sizeof(numbuf
), "%"PRId64
"", val
);
4660 tok
->text
= nasm_strdup(numbuf
);
4661 tok
->type
= TOK_NUMBER
;