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
, const char **namep
,
402 static void make_tok_num(Token
* tok
, int64_t val
);
403 static void error(int severity
, const char *fmt
, ...);
404 static void error_precond(int severity
, const char *fmt
, ...);
405 static void *new_Block(size_t size
);
406 static void delete_Blocks(void);
407 static Token
*new_Token(Token
* next
, enum pp_token_type type
,
408 const char *text
, int txtlen
);
409 static Token
*delete_Token(Token
* t
);
412 * Macros for safe checking of token pointers, avoid *(NULL)
414 #define tok_type_(x,t) ((x) && (x)->type == (t))
415 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
416 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
417 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
419 /* Handle TASM specific directives, which do not contain a % in
420 * front of them. We do it here because I could not find any other
421 * place to do it for the moment, and it is a hack (ideally it would
422 * be nice to be able to use the NASM pre-processor to do it).
424 static char *check_tasm_directive(char *line
)
426 int32_t i
, j
, k
, m
, len
;
427 char *p
= line
, *oldline
, oldchar
;
429 /* Skip whitespace */
430 while (nasm_isspace(*p
) && *p
!= 0)
433 /* Binary search for the directive name */
435 j
= elements(tasm_directives
);
437 while (!nasm_isspace(p
[len
]) && p
[len
] != 0)
444 m
= nasm_stricmp(p
, tasm_directives
[k
]);
446 /* We have found a directive, so jam a % in front of it
447 * so that NASM will then recognise it as one if it's own.
452 line
= nasm_malloc(len
+ 2);
454 if (k
== TM_IFDIFI
) {
455 /* NASM does not recognise IFDIFI, so we convert it to
456 * %ifdef BOGUS. This is not used in NASM comaptible
457 * code, but does need to parse for the TASM macro
460 strcpy(line
+ 1, "ifdef BOGUS");
462 memcpy(line
+ 1, p
, len
+ 1);
477 * The pre-preprocessing stage... This function translates line
478 * number indications as they emerge from GNU cpp (`# lineno "file"
479 * flags') into NASM preprocessor line number indications (`%line
482 static char *prepreproc(char *line
)
485 char *fname
, *oldline
;
487 if (line
[0] == '#' && line
[1] == ' ') {
490 lineno
= atoi(fname
);
491 fname
+= strspn(fname
, "0123456789 ");
494 fnlen
= strcspn(fname
, "\"");
495 line
= nasm_malloc(20 + fnlen
);
496 snprintf(line
, 20 + fnlen
, "%%line %d %.*s", lineno
, fnlen
, fname
);
499 if (tasm_compatible_mode
)
500 return check_tasm_directive(line
);
505 * Free a linked list of tokens.
507 static void free_tlist(Token
* list
)
510 list
= delete_Token(list
);
515 * Free a linked list of lines.
517 static void free_llist(Line
* list
)
523 free_tlist(l
->first
);
531 static void free_mmacro(MMacro
* m
)
534 free_tlist(m
->dlist
);
535 nasm_free(m
->defaults
);
536 free_llist(m
->expansion
);
541 * Free all currently defined macros, and free the hash tables
543 static void free_smacro_table(struct hash_table
*smt
)
547 struct hash_tbl_node
*it
= NULL
;
549 while ((s
= hash_iterate(smt
, &it
, &key
)) != NULL
) {
550 nasm_free((void *)key
);
552 SMacro
*ns
= s
->next
;
554 free_tlist(s
->expansion
);
562 static void free_mmacro_table(struct hash_table
*mmt
)
566 struct hash_tbl_node
*it
= NULL
;
569 while ((m
= hash_iterate(mmt
, &it
, &key
)) != NULL
) {
570 nasm_free((void *)key
);
572 MMacro
*nm
= m
->next
;
580 static void free_macros(void)
582 free_smacro_table(&smacros
);
583 free_mmacro_table(&mmacros
);
587 * Initialize the hash tables
589 static void init_macros(void)
591 hash_init(&smacros
, HASH_LARGE
);
592 hash_init(&mmacros
, HASH_LARGE
);
596 * Pop the context stack.
598 static void ctx_pop(void)
603 free_smacro_table(&c
->localmac
);
609 * Search for a key in the hash index; adding it if necessary
610 * (in which case we initialize the data pointer to NULL.)
613 hash_findi_add(struct hash_table
*hash
, const char *str
)
615 struct hash_insert hi
;
619 r
= hash_findi(hash
, str
, &hi
);
623 strx
= nasm_strdup(str
); /* Use a more efficient allocator here? */
624 return hash_add(&hi
, strx
, NULL
);
628 * Like hash_findi, but returns the data element rather than a pointer
629 * to it. Used only when not adding a new element, hence no third
633 hash_findix(struct hash_table
*hash
, const char *str
)
637 p
= hash_findi(hash
, str
, NULL
);
638 return p
? *p
: NULL
;
641 #define BUF_DELTA 512
643 * Read a line from the top file in istk, handling multiple CR/LFs
644 * at the end of the line read, and handling spurious ^Zs. Will
645 * return lines from the standard macro set if this has not already
648 static char *read_line(void)
650 char *buffer
, *p
, *q
;
651 int bufsize
, continued_count
;
655 const unsigned char *p
= stdmacpos
;
660 len
+= pp_directives_len
[c
-0x80]+1;
664 ret
= nasm_malloc(len
+1);
666 while ((c
= *stdmacpos
++)) {
668 memcpy(q
, pp_directives
[c
-0x80], pp_directives_len
[c
-0x80]);
669 q
+= pp_directives_len
[c
-0x80];
679 /* This was the last of the standard macro chain... */
681 if (any_extrastdmac
) {
682 stdmacpos
= extrastdmac
;
683 any_extrastdmac
= false;
684 } else if (do_predef
) {
686 Token
*head
, **tail
, *t
;
689 * Nasty hack: here we push the contents of
690 * `predef' on to the top-level expansion stack,
691 * since this is the most convenient way to
692 * implement the pre-include and pre-define
695 for (pd
= predef
; pd
; pd
= pd
->next
) {
698 for (t
= pd
->first
; t
; t
= t
->next
) {
699 *tail
= new_Token(NULL
, t
->type
, t
->text
, 0);
700 tail
= &(*tail
)->next
;
702 l
= nasm_malloc(sizeof(Line
));
703 l
->next
= istk
->expansion
;
715 buffer
= nasm_malloc(BUF_DELTA
);
719 q
= fgets(p
, bufsize
- (p
- buffer
), istk
->fp
);
723 if (p
> buffer
&& p
[-1] == '\n') {
724 /* Convert backslash-CRLF line continuation sequences into
725 nothing at all (for DOS and Windows) */
726 if (((p
- 2) > buffer
) && (p
[-3] == '\\') && (p
[-2] == '\r')) {
731 /* Also convert backslash-LF line continuation sequences into
732 nothing at all (for Unix) */
733 else if (((p
- 1) > buffer
) && (p
[-2] == '\\')) {
741 if (p
- buffer
> bufsize
- 10) {
742 int32_t offset
= p
- buffer
;
743 bufsize
+= BUF_DELTA
;
744 buffer
= nasm_realloc(buffer
, bufsize
);
745 p
= buffer
+ offset
; /* prevent stale-pointer problems */
749 if (!q
&& p
== buffer
) {
754 src_set_linnum(src_get_linnum() + istk
->lineinc
+
755 (continued_count
* istk
->lineinc
));
758 * Play safe: remove CRs as well as LFs, if any of either are
759 * present at the end of the line.
761 while (--p
>= buffer
&& (*p
== '\n' || *p
== '\r'))
765 * Handle spurious ^Z, which may be inserted into source files
766 * by some file transfer utilities.
768 buffer
[strcspn(buffer
, "\032")] = '\0';
770 list
->line(LIST_READ
, buffer
);
776 * Tokenize a line of text. This is a very simple process since we
777 * don't need to parse the value out of e.g. numeric tokens: we
778 * simply split one string into many.
780 static Token
*tokenize(char *line
)
783 enum pp_token_type type
;
785 Token
*t
, **tail
= &list
;
791 if (nasm_isdigit(*p
) ||
792 ((*p
== '-' || *p
== '+') && nasm_isdigit(p
[1])) ||
793 ((*p
== '+') && (nasm_isspace(p
[1]) || !p
[1]))) {
797 while (nasm_isdigit(*p
));
798 type
= TOK_PREPROC_ID
;
799 } else if (*p
== '{') {
801 while (*p
&& *p
!= '}') {
808 type
= TOK_PREPROC_ID
;
809 } else if (*p
== '[') {
811 line
+= 2; /* Skip the leading %[ */
813 while (lvl
&& (c
= *p
++)) {
825 p
= nasm_skip_string(p
)+1;
835 error(ERR_NONFATAL
, "unterminated %[ construct");
837 } else if (*p
== '?') {
838 type
= TOK_PREPROC_Q
; /* %? */
841 type
= TOK_PREPROC_QQ
; /* %?? */
844 } else if (isidchar(*p
) ||
845 ((*p
== '!' || *p
== '%' || *p
== '$') &&
850 while (isidchar(*p
));
851 type
= TOK_PREPROC_ID
;
857 } else if (isidstart(*p
) || (*p
== '$' && isidstart(p
[1]))) {
860 while (*p
&& isidchar(*p
))
862 } else if (*p
== '\'' || *p
== '"' || *p
== '`') {
867 p
= nasm_skip_string(p
);
872 error(ERR_WARNING
|ERR_PASS1
, "unterminated string");
873 /* Handling unterminated strings by UNV */
876 } else if (p
[0] == '$' && p
[1] == '$') {
877 type
= TOK_OTHER
; /* TOKEN_BASE */
879 } else if (isnumstart(*p
)) {
881 bool is_float
= false;
897 if (!is_hex
&& (c
== 'e' || c
== 'E')) {
899 if (*p
== '+' || *p
== '-') {
900 /* e can only be followed by +/- if it is either a
901 prefixed hex number or a floating-point number */
905 } else if (c
== 'H' || c
== 'h' || c
== 'X' || c
== 'x') {
907 } else if (c
== 'P' || c
== 'p') {
909 if (*p
== '+' || *p
== '-')
911 } else if (isnumchar(c
) || c
== '_')
914 /* we need to deal with consequences of the legacy
915 parser, like "1.nolist" being two tokens
916 (TOK_NUMBER, TOK_ID) here; at least give it
917 a shot for now. In the future, we probably need
918 a flex-based scanner with proper pattern matching
919 to do it as well as it can be done. Nothing in
920 the world is going to help the person who wants
921 0x123.p16 interpreted as two tokens, though. */
926 if (nasm_isdigit(*r
) || (is_hex
&& nasm_isxdigit(*r
)) ||
927 (!is_hex
&& (*r
== 'e' || *r
== 'E')) ||
928 (*r
== 'p' || *r
== 'P')) {
932 break; /* Terminate the token */
936 p
--; /* Point to first character beyond number */
938 if (p
== line
+1 && *line
== '$') {
939 type
= TOK_OTHER
; /* TOKEN_HERE */
941 if (has_e
&& !is_hex
) {
942 /* 1e13 is floating-point, but 1e13h is not */
946 type
= is_float
? TOK_FLOAT
: TOK_NUMBER
;
948 } else if (nasm_isspace(*p
)) {
949 type
= TOK_WHITESPACE
;
951 while (*p
&& nasm_isspace(*p
))
954 * Whitespace just before end-of-line is discarded by
955 * pretending it's a comment; whitespace just before a
956 * comment gets lumped into the comment.
958 if (!*p
|| *p
== ';') {
963 } else if (*p
== ';') {
969 * Anything else is an operator of some kind. We check
970 * for all the double-character operators (>>, <<, //,
971 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
972 * else is a single-character operator.
975 if ((p
[0] == '>' && p
[1] == '>') ||
976 (p
[0] == '<' && p
[1] == '<') ||
977 (p
[0] == '/' && p
[1] == '/') ||
978 (p
[0] == '<' && p
[1] == '=') ||
979 (p
[0] == '>' && p
[1] == '=') ||
980 (p
[0] == '=' && p
[1] == '=') ||
981 (p
[0] == '!' && p
[1] == '=') ||
982 (p
[0] == '<' && p
[1] == '>') ||
983 (p
[0] == '&' && p
[1] == '&') ||
984 (p
[0] == '|' && p
[1] == '|') ||
985 (p
[0] == '^' && p
[1] == '^')) {
991 /* Handling unterminated string by UNV */
994 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
995 t->text[p-line] = *line;
999 if (type
!= TOK_COMMENT
) {
1000 *tail
= t
= new_Token(NULL
, type
, line
, p
- line
);
1009 * this function allocates a new managed block of memory and
1010 * returns a pointer to the block. The managed blocks are
1011 * deleted only all at once by the delete_Blocks function.
1013 static void *new_Block(size_t size
)
1015 Blocks
*b
= &blocks
;
1017 /* first, get to the end of the linked list */
1020 /* now allocate the requested chunk */
1021 b
->chunk
= nasm_malloc(size
);
1023 /* now allocate a new block for the next request */
1024 b
->next
= nasm_malloc(sizeof(Blocks
));
1025 /* and initialize the contents of the new block */
1026 b
->next
->next
= NULL
;
1027 b
->next
->chunk
= NULL
;
1032 * this function deletes all managed blocks of memory
1034 static void delete_Blocks(void)
1036 Blocks
*a
, *b
= &blocks
;
1039 * keep in mind that the first block, pointed to by blocks
1040 * is a static and not dynamically allocated, so we don't
1045 nasm_free(b
->chunk
);
1054 * this function creates a new Token and passes a pointer to it
1055 * back to the caller. It sets the type and text elements, and
1056 * also the a.mac and next elements to NULL.
1058 static Token
*new_Token(Token
* next
, enum pp_token_type type
,
1059 const char *text
, int txtlen
)
1064 if (freeTokens
== NULL
) {
1065 freeTokens
= (Token
*) new_Block(TOKEN_BLOCKSIZE
* sizeof(Token
));
1066 for (i
= 0; i
< TOKEN_BLOCKSIZE
- 1; i
++)
1067 freeTokens
[i
].next
= &freeTokens
[i
+ 1];
1068 freeTokens
[i
].next
= NULL
;
1071 freeTokens
= t
->next
;
1075 if (type
== TOK_WHITESPACE
|| text
== NULL
) {
1079 txtlen
= strlen(text
);
1080 t
->text
= nasm_malloc(txtlen
+1);
1081 memcpy(t
->text
, text
, txtlen
);
1082 t
->text
[txtlen
] = '\0';
1087 static Token
*delete_Token(Token
* t
)
1089 Token
*next
= t
->next
;
1091 t
->next
= freeTokens
;
1097 * Convert a line of tokens back into text.
1098 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1099 * will be transformed into ..@ctxnum.xxx
1101 static char *detoken(Token
* tlist
, bool expand_locals
)
1109 for (t
= tlist
; t
; t
= t
->next
) {
1110 if (t
->type
== TOK_PREPROC_ID
&& t
->text
[1] == '!') {
1111 char *p
= getenv(t
->text
+ 2);
1114 t
->text
= nasm_strdup(p
);
1118 /* Expand local macros here and not during preprocessing */
1119 if (expand_locals
&&
1120 t
->type
== TOK_PREPROC_ID
&& t
->text
&&
1121 t
->text
[0] == '%' && t
->text
[1] == '$') {
1124 Context
*ctx
= get_ctx(t
->text
, &q
, false);
1127 snprintf(buffer
, sizeof(buffer
), "..@%"PRIu32
".", ctx
->number
);
1128 p
= nasm_strcat(buffer
, q
);
1133 if (t
->type
== TOK_WHITESPACE
) {
1135 } else if (t
->text
) {
1136 len
+= strlen(t
->text
);
1139 p
= line
= nasm_malloc(len
+ 1);
1140 for (t
= tlist
; t
; t
= t
->next
) {
1141 if (t
->type
== TOK_WHITESPACE
) {
1143 } else if (t
->text
) {
1154 * A scanner, suitable for use by the expression evaluator, which
1155 * operates on a line of Tokens. Expects a pointer to a pointer to
1156 * the first token in the line to be passed in as its private_data
1159 * FIX: This really needs to be unified with stdscan.
1161 static int ppscan(void *private_data
, struct tokenval
*tokval
)
1163 Token
**tlineptr
= private_data
;
1165 char ourcopy
[MAX_KEYWORD
+1], *p
, *r
, *s
;
1169 *tlineptr
= tline
? tline
->next
: NULL
;
1171 while (tline
&& (tline
->type
== TOK_WHITESPACE
||
1172 tline
->type
== TOK_COMMENT
));
1175 return tokval
->t_type
= TOKEN_EOS
;
1177 tokval
->t_charptr
= tline
->text
;
1179 if (tline
->text
[0] == '$' && !tline
->text
[1])
1180 return tokval
->t_type
= TOKEN_HERE
;
1181 if (tline
->text
[0] == '$' && tline
->text
[1] == '$' && !tline
->text
[2])
1182 return tokval
->t_type
= TOKEN_BASE
;
1184 if (tline
->type
== TOK_ID
) {
1185 p
= tokval
->t_charptr
= tline
->text
;
1187 tokval
->t_charptr
++;
1188 return tokval
->t_type
= TOKEN_ID
;
1191 for (r
= p
, s
= ourcopy
; *r
; r
++) {
1192 if (r
>= p
+MAX_KEYWORD
)
1193 return tokval
->t_type
= TOKEN_ID
; /* Not a keyword */
1194 *s
++ = nasm_tolower(*r
);
1197 /* right, so we have an identifier sitting in temp storage. now,
1198 * is it actually a register or instruction name, or what? */
1199 return nasm_token_hash(ourcopy
, tokval
);
1202 if (tline
->type
== TOK_NUMBER
) {
1204 tokval
->t_integer
= readnum(tline
->text
, &rn_error
);
1205 tokval
->t_charptr
= tline
->text
;
1207 return tokval
->t_type
= TOKEN_ERRNUM
;
1209 return tokval
->t_type
= TOKEN_NUM
;
1212 if (tline
->type
== TOK_FLOAT
) {
1213 return tokval
->t_type
= TOKEN_FLOAT
;
1216 if (tline
->type
== TOK_STRING
) {
1219 bq
= tline
->text
[0];
1220 tokval
->t_charptr
= tline
->text
;
1221 tokval
->t_inttwo
= nasm_unquote(tline
->text
, &ep
);
1223 if (ep
[0] != bq
|| ep
[1] != '\0')
1224 return tokval
->t_type
= TOKEN_ERRSTR
;
1226 return tokval
->t_type
= TOKEN_STR
;
1229 if (tline
->type
== TOK_OTHER
) {
1230 if (!strcmp(tline
->text
, "<<"))
1231 return tokval
->t_type
= TOKEN_SHL
;
1232 if (!strcmp(tline
->text
, ">>"))
1233 return tokval
->t_type
= TOKEN_SHR
;
1234 if (!strcmp(tline
->text
, "//"))
1235 return tokval
->t_type
= TOKEN_SDIV
;
1236 if (!strcmp(tline
->text
, "%%"))
1237 return tokval
->t_type
= TOKEN_SMOD
;
1238 if (!strcmp(tline
->text
, "=="))
1239 return tokval
->t_type
= TOKEN_EQ
;
1240 if (!strcmp(tline
->text
, "<>"))
1241 return tokval
->t_type
= TOKEN_NE
;
1242 if (!strcmp(tline
->text
, "!="))
1243 return tokval
->t_type
= TOKEN_NE
;
1244 if (!strcmp(tline
->text
, "<="))
1245 return tokval
->t_type
= TOKEN_LE
;
1246 if (!strcmp(tline
->text
, ">="))
1247 return tokval
->t_type
= TOKEN_GE
;
1248 if (!strcmp(tline
->text
, "&&"))
1249 return tokval
->t_type
= TOKEN_DBL_AND
;
1250 if (!strcmp(tline
->text
, "^^"))
1251 return tokval
->t_type
= TOKEN_DBL_XOR
;
1252 if (!strcmp(tline
->text
, "||"))
1253 return tokval
->t_type
= TOKEN_DBL_OR
;
1257 * We have no other options: just return the first character of
1260 return tokval
->t_type
= tline
->text
[0];
1264 * Compare a string to the name of an existing macro; this is a
1265 * simple wrapper which calls either strcmp or nasm_stricmp
1266 * depending on the value of the `casesense' parameter.
1268 static int mstrcmp(const char *p
, const char *q
, bool casesense
)
1270 return casesense
? strcmp(p
, q
) : nasm_stricmp(p
, q
);
1274 * Compare a string to the name of an existing macro; this is a
1275 * simple wrapper which calls either strcmp or nasm_stricmp
1276 * depending on the value of the `casesense' parameter.
1278 static int mmemcmp(const char *p
, const char *q
, size_t l
, bool casesense
)
1280 return casesense
? memcmp(p
, q
, l
) : nasm_memicmp(p
, q
, l
);
1284 * Return the Context structure associated with a %$ token. Return
1285 * NULL, having _already_ reported an error condition, if the
1286 * context stack isn't deep enough for the supplied number of $
1288 * If all_contexts == true, contexts that enclose current are
1289 * also scanned for such smacro, until it is found; if not -
1290 * only the context that directly results from the number of $'s
1291 * in variable's name.
1293 * If "namep" is non-NULL, set it to the pointer to the macro name
1294 * tail, i.e. the part beyond %$...
1296 static Context
*get_ctx(const char *name
, const char **namep
,
1306 if (!name
|| name
[0] != '%' || name
[1] != '$')
1310 error(ERR_NONFATAL
, "`%s': context stack is empty", name
);
1317 while (ctx
&& *name
== '$') {
1323 error(ERR_NONFATAL
, "`%s': context stack is only"
1324 " %d level%s deep", name
, i
, (i
== 1 ? "" : "s"));
1335 /* Search for this smacro in found context */
1336 m
= hash_findix(&ctx
->localmac
, name
);
1338 if (!mstrcmp(m
->name
, name
, m
->casesense
))
1349 * Check to see if a file is already in a string list
1351 static bool in_list(const StrList
*list
, const char *str
)
1354 if (!strcmp(list
->str
, str
))
1362 * Open an include file. This routine must always return a valid
1363 * file pointer if it returns - it's responsible for throwing an
1364 * ERR_FATAL and bombing out completely if not. It should also try
1365 * the include path one by one until it finds the file or reaches
1366 * the end of the path.
1368 static FILE *inc_fopen(const char *file
, StrList
**dhead
, StrList
***dtail
,
1373 IncPath
*ip
= ipath
;
1374 int len
= strlen(file
);
1375 size_t prefix_len
= 0;
1379 sl
= nasm_malloc(prefix_len
+len
+1+sizeof sl
->next
);
1380 memcpy(sl
->str
, prefix
, prefix_len
);
1381 memcpy(sl
->str
+prefix_len
, file
, len
+1);
1382 fp
= fopen(sl
->str
, "r");
1383 if (fp
&& dhead
&& !in_list(*dhead
, sl
->str
)) {
1401 prefix_len
= strlen(prefix
);
1403 /* -MG given and file not found */
1404 if (dhead
&& !in_list(*dhead
, file
)) {
1405 sl
= nasm_malloc(len
+1+sizeof sl
->next
);
1407 strcpy(sl
->str
, file
);
1415 error(ERR_FATAL
, "unable to open include file `%s'", file
);
1416 return NULL
; /* never reached - placate compilers */
1420 * Determine if we should warn on defining a single-line macro of
1421 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1422 * return true if _any_ single-line macro of that name is defined.
1423 * Otherwise, will return true if a single-line macro with either
1424 * `nparam' or no parameters is defined.
1426 * If a macro with precisely the right number of parameters is
1427 * defined, or nparam is -1, the address of the definition structure
1428 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1429 * is NULL, no action will be taken regarding its contents, and no
1432 * Note that this is also called with nparam zero to resolve
1435 * If you already know which context macro belongs to, you can pass
1436 * the context pointer as first parameter; if you won't but name begins
1437 * with %$ the context will be automatically computed. If all_contexts
1438 * is true, macro will be searched in outer contexts as well.
1441 smacro_defined(Context
* ctx
, const char *name
, int nparam
, SMacro
** defn
,
1444 struct hash_table
*smtbl
;
1448 smtbl
= &ctx
->localmac
;
1449 } else if (name
[0] == '%' && name
[1] == '$') {
1451 ctx
= get_ctx(name
, &name
, false);
1453 return false; /* got to return _something_ */
1454 smtbl
= &ctx
->localmac
;
1458 m
= (SMacro
*) hash_findix(smtbl
, name
);
1461 if (!mstrcmp(m
->name
, name
, m
->casesense
&& nocase
) &&
1462 (nparam
<= 0 || m
->nparam
== 0 || nparam
== (int) m
->nparam
)) {
1464 if (nparam
== (int) m
->nparam
|| nparam
== -1)
1478 * Count and mark off the parameters in a multi-line macro call.
1479 * This is called both from within the multi-line macro expansion
1480 * code, and also to mark off the default parameters when provided
1481 * in a %macro definition line.
1483 static void count_mmac_params(Token
* t
, int *nparam
, Token
*** params
)
1485 int paramsize
, brace
;
1487 *nparam
= paramsize
= 0;
1490 /* +1: we need space for the final NULL */
1491 if (*nparam
+1 >= paramsize
) {
1492 paramsize
+= PARAM_DELTA
;
1493 *params
= nasm_realloc(*params
, sizeof(**params
) * paramsize
);
1497 if (tok_is_(t
, "{"))
1499 (*params
)[(*nparam
)++] = t
;
1500 while (tok_isnt_(t
, brace
? "}" : ","))
1502 if (t
) { /* got a comma/brace */
1506 * Now we've found the closing brace, look further
1510 if (tok_isnt_(t
, ",")) {
1512 "braces do not enclose all of macro parameter");
1513 while (tok_isnt_(t
, ","))
1517 t
= t
->next
; /* eat the comma */
1524 * Determine whether one of the various `if' conditions is true or
1527 * We must free the tline we get passed.
1529 static bool if_condition(Token
* tline
, enum preproc_token ct
)
1531 enum pp_conditional i
= PP_COND(ct
);
1533 Token
*t
, *tt
, **tptr
, *origline
;
1534 struct tokenval tokval
;
1536 enum pp_token_type needtype
;
1542 j
= false; /* have we matched yet? */
1547 if (tline
->type
!= TOK_ID
) {
1549 "`%s' expects context identifiers", pp_directives
[ct
]);
1550 free_tlist(origline
);
1553 if (cstk
&& cstk
->name
&& !nasm_stricmp(tline
->text
, cstk
->name
))
1555 tline
= tline
->next
;
1560 j
= false; /* have we matched yet? */
1563 if (!tline
|| (tline
->type
!= TOK_ID
&&
1564 (tline
->type
!= TOK_PREPROC_ID
||
1565 tline
->text
[1] != '$'))) {
1567 "`%s' expects macro identifiers", pp_directives
[ct
]);
1570 if (smacro_defined(NULL
, tline
->text
, 0, NULL
, true))
1572 tline
= tline
->next
;
1578 tline
= expand_smacro(tline
);
1580 while (tok_isnt_(tt
, ","))
1584 "`%s' expects two comma-separated arguments",
1589 j
= true; /* assume equality unless proved not */
1590 while ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) && tt
) {
1591 if (tt
->type
== TOK_OTHER
&& !strcmp(tt
->text
, ",")) {
1592 error(ERR_NONFATAL
, "`%s': more than one comma on line",
1596 if (t
->type
== TOK_WHITESPACE
) {
1600 if (tt
->type
== TOK_WHITESPACE
) {
1604 if (tt
->type
!= t
->type
) {
1605 j
= false; /* found mismatching tokens */
1608 /* When comparing strings, need to unquote them first */
1609 if (t
->type
== TOK_STRING
) {
1610 size_t l1
= nasm_unquote(t
->text
, NULL
);
1611 size_t l2
= nasm_unquote(tt
->text
, NULL
);
1617 if (mmemcmp(t
->text
, tt
->text
, l1
, i
== PPC_IFIDN
)) {
1621 } else if (mstrcmp(tt
->text
, t
->text
, i
== PPC_IFIDN
) != 0) {
1622 j
= false; /* found mismatching tokens */
1629 if ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) || tt
)
1630 j
= false; /* trailing gunk on one end or other */
1636 MMacro searching
, *mmac
;
1639 tline
= expand_id(tline
);
1640 if (!tok_type_(tline
, TOK_ID
)) {
1642 "`%s' expects a macro name", pp_directives
[ct
]);
1645 searching
.name
= nasm_strdup(tline
->text
);
1646 searching
.casesense
= true;
1647 searching
.plus
= false;
1648 searching
.nolist
= false;
1649 searching
.in_progress
= 0;
1650 searching
.rep_nest
= NULL
;
1651 searching
.nparam_min
= 0;
1652 searching
.nparam_max
= INT_MAX
;
1653 tline
= expand_smacro(tline
->next
);
1656 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
1658 "`%s' expects a parameter count or nothing",
1661 searching
.nparam_min
= searching
.nparam_max
=
1662 readnum(tline
->text
, &j
);
1665 "unable to parse parameter count `%s'",
1668 if (tline
&& tok_is_(tline
->next
, "-")) {
1669 tline
= tline
->next
->next
;
1670 if (tok_is_(tline
, "*"))
1671 searching
.nparam_max
= INT_MAX
;
1672 else if (!tok_type_(tline
, TOK_NUMBER
))
1674 "`%s' expects a parameter count after `-'",
1677 searching
.nparam_max
= readnum(tline
->text
, &j
);
1680 "unable to parse parameter count `%s'",
1682 if (searching
.nparam_min
> searching
.nparam_max
)
1684 "minimum parameter count exceeds maximum");
1687 if (tline
&& tok_is_(tline
->next
, "+")) {
1688 tline
= tline
->next
;
1689 searching
.plus
= true;
1691 mmac
= (MMacro
*) hash_findix(&mmacros
, searching
.name
);
1693 if (!strcmp(mmac
->name
, searching
.name
) &&
1694 (mmac
->nparam_min
<= searching
.nparam_max
1696 && (searching
.nparam_min
<= mmac
->nparam_max
1703 if(tline
&& tline
->next
)
1704 error(ERR_WARNING
|ERR_PASS1
,
1705 "trailing garbage after %%ifmacro ignored");
1706 nasm_free(searching
.name
);
1715 needtype
= TOK_NUMBER
;
1718 needtype
= TOK_STRING
;
1722 t
= tline
= expand_smacro(tline
);
1724 while (tok_type_(t
, TOK_WHITESPACE
) ||
1725 (needtype
== TOK_NUMBER
&&
1726 tok_type_(t
, TOK_OTHER
) &&
1727 (t
->text
[0] == '-' || t
->text
[0] == '+') &&
1731 j
= tok_type_(t
, needtype
);
1735 t
= tline
= expand_smacro(tline
);
1736 while (tok_type_(t
, TOK_WHITESPACE
))
1741 t
= t
->next
; /* Skip the actual token */
1742 while (tok_type_(t
, TOK_WHITESPACE
))
1744 j
= !t
; /* Should be nothing left */
1749 t
= tline
= expand_smacro(tline
);
1750 while (tok_type_(t
, TOK_WHITESPACE
))
1753 j
= !t
; /* Should be empty */
1757 t
= tline
= expand_smacro(tline
);
1759 tokval
.t_type
= TOKEN_INVALID
;
1760 evalresult
= evaluate(ppscan
, tptr
, &tokval
,
1761 NULL
, pass
| CRITICAL
, error
, NULL
);
1765 error(ERR_WARNING
|ERR_PASS1
,
1766 "trailing garbage after expression ignored");
1767 if (!is_simple(evalresult
)) {
1769 "non-constant value given to `%s'", pp_directives
[ct
]);
1772 j
= reloc_value(evalresult
) != 0;
1777 "preprocessor directive `%s' not yet implemented",
1782 free_tlist(origline
);
1783 return j
^ PP_NEGATIVE(ct
);
1786 free_tlist(origline
);
1791 * Common code for defining an smacro
1793 static bool define_smacro(Context
*ctx
, const char *mname
, bool casesense
,
1794 int nparam
, Token
*expansion
)
1796 SMacro
*smac
, **smhead
;
1797 struct hash_table
*smtbl
;
1799 if (smacro_defined(ctx
, mname
, nparam
, &smac
, casesense
)) {
1801 error(ERR_WARNING
|ERR_PASS1
,
1802 "single-line macro `%s' defined both with and"
1803 " without parameters", mname
);
1805 /* Some instances of the old code considered this a failure,
1806 some others didn't. What is the right thing to do here? */
1807 free_tlist(expansion
);
1808 return false; /* Failure */
1811 * We're redefining, so we have to take over an
1812 * existing SMacro structure. This means freeing
1813 * what was already in it.
1815 nasm_free(smac
->name
);
1816 free_tlist(smac
->expansion
);
1819 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
1820 smhead
= (SMacro
**) hash_findi_add(smtbl
, mname
);
1821 smac
= nasm_malloc(sizeof(SMacro
));
1822 smac
->next
= *smhead
;
1825 smac
->name
= nasm_strdup(mname
);
1826 smac
->casesense
= casesense
;
1827 smac
->nparam
= nparam
;
1828 smac
->expansion
= expansion
;
1829 smac
->in_progress
= false;
1830 return true; /* Success */
1834 * Undefine an smacro
1836 static void undef_smacro(Context
*ctx
, const char *mname
)
1838 SMacro
**smhead
, *s
, **sp
;
1839 struct hash_table
*smtbl
;
1841 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
1842 smhead
= (SMacro
**)hash_findi(smtbl
, mname
, NULL
);
1846 * We now have a macro name... go hunt for it.
1849 while ((s
= *sp
) != NULL
) {
1850 if (!mstrcmp(s
->name
, mname
, s
->casesense
)) {
1853 free_tlist(s
->expansion
);
1863 * Parse a mmacro specification.
1865 static bool parse_mmacro_spec(Token
*tline
, MMacro
*def
, const char *directive
)
1869 tline
= tline
->next
;
1871 tline
= expand_id(tline
);
1872 if (!tok_type_(tline
, TOK_ID
)) {
1873 error(ERR_NONFATAL
, "`%s' expects a macro name", directive
);
1877 def
->name
= nasm_strdup(tline
->text
);
1879 def
->nolist
= false;
1880 def
->in_progress
= 0;
1881 def
->rep_nest
= NULL
;
1882 def
->nparam_min
= 0;
1883 def
->nparam_max
= 0;
1885 tline
= expand_smacro(tline
->next
);
1887 if (!tok_type_(tline
, TOK_NUMBER
)) {
1888 error(ERR_NONFATAL
, "`%s' expects a parameter count", directive
);
1890 def
->nparam_min
= def
->nparam_max
=
1891 readnum(tline
->text
, &err
);
1894 "unable to parse parameter count `%s'", tline
->text
);
1896 if (tline
&& tok_is_(tline
->next
, "-")) {
1897 tline
= tline
->next
->next
;
1898 if (tok_is_(tline
, "*")) {
1899 def
->nparam_max
= INT_MAX
;
1900 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
1902 "`%s' expects a parameter count after `-'", directive
);
1904 def
->nparam_max
= readnum(tline
->text
, &err
);
1906 error(ERR_NONFATAL
, "unable to parse parameter count `%s'",
1909 if (def
->nparam_min
> def
->nparam_max
) {
1910 error(ERR_NONFATAL
, "minimum parameter count exceeds maximum");
1914 if (tline
&& tok_is_(tline
->next
, "+")) {
1915 tline
= tline
->next
;
1918 if (tline
&& tok_type_(tline
->next
, TOK_ID
) &&
1919 !nasm_stricmp(tline
->next
->text
, ".nolist")) {
1920 tline
= tline
->next
;
1925 * Handle default parameters.
1927 if (tline
&& tline
->next
) {
1928 def
->dlist
= tline
->next
;
1930 count_mmac_params(def
->dlist
, &def
->ndefs
, &def
->defaults
);
1933 def
->defaults
= NULL
;
1935 def
->expansion
= NULL
;
1938 def
->ndefs
> def
->nparam_max
- def
->nparam_min
&&
1940 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MDP
,
1941 "too many default macro parameters");
1948 * Decode a size directive
1950 static int parse_size(const char *str
) {
1951 static const char *size_names
[] =
1952 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
1953 static const int sizes
[] =
1954 { 0, 1, 4, 16, 8, 10, 2, 32 };
1956 return sizes
[bsii(str
, size_names
, elements(size_names
))+1];
1960 * find and process preprocessor directive in passed line
1961 * Find out if a line contains a preprocessor directive, and deal
1964 * If a directive _is_ found, it is the responsibility of this routine
1965 * (and not the caller) to free_tlist() the line.
1967 * @param tline a pointer to the current tokeninzed line linked list
1968 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
1971 static int do_directive(Token
* tline
)
1973 enum preproc_token i
;
1986 MMacro
*mmac
, **mmhead
;
1987 Token
*t
, *tt
, *param_start
, *macro_start
, *last
, **tptr
, *origline
;
1989 struct tokenval tokval
;
1991 MMacro
*tmp_defining
; /* Used when manipulating rep_nest */
1999 if (!tline
|| !tok_type_(tline
, TOK_PREPROC_ID
) ||
2000 (tline
->text
[1] == '%' || tline
->text
[1] == '$'
2001 || tline
->text
[1] == '!'))
2002 return NO_DIRECTIVE_FOUND
;
2004 i
= pp_token_hash(tline
->text
);
2007 * If we're in a non-emitting branch of a condition construct,
2008 * or walking to the end of an already terminated %rep block,
2009 * we should ignore all directives except for condition
2012 if (((istk
->conds
&& !emitting(istk
->conds
->state
)) ||
2013 (istk
->mstk
&& !istk
->mstk
->in_progress
)) && !is_condition(i
)) {
2014 return NO_DIRECTIVE_FOUND
;
2018 * If we're defining a macro or reading a %rep block, we should
2019 * ignore all directives except for %macro/%imacro (which nest),
2020 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2021 * If we're in a %rep block, another %rep nests, so should be let through.
2023 if (defining
&& i
!= PP_MACRO
&& i
!= PP_IMACRO
&&
2024 i
!= PP_ENDMACRO
&& i
!= PP_ENDM
&&
2025 (defining
->name
|| (i
!= PP_ENDREP
&& i
!= PP_REP
))) {
2026 return NO_DIRECTIVE_FOUND
;
2030 if (i
== PP_MACRO
|| i
== PP_IMACRO
) {
2032 return NO_DIRECTIVE_FOUND
;
2033 } else if (nested_mac_count
> 0) {
2034 if (i
== PP_ENDMACRO
) {
2036 return NO_DIRECTIVE_FOUND
;
2039 if (!defining
->name
) {
2042 return NO_DIRECTIVE_FOUND
;
2043 } else if (nested_rep_count
> 0) {
2044 if (i
== PP_ENDREP
) {
2046 return NO_DIRECTIVE_FOUND
;
2054 error(ERR_NONFATAL
, "unknown preprocessor directive `%s'",
2056 return NO_DIRECTIVE_FOUND
; /* didn't get it */
2059 /* Directive to tell NASM what the default stack size is. The
2060 * default is for a 16-bit stack, and this can be overriden with
2062 * the following form:
2064 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2066 tline
= tline
->next
;
2067 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2068 tline
= tline
->next
;
2069 if (!tline
|| tline
->type
!= TOK_ID
) {
2070 error(ERR_NONFATAL
, "`%%stacksize' missing size parameter");
2071 free_tlist(origline
);
2072 return DIRECTIVE_FOUND
;
2074 if (nasm_stricmp(tline
->text
, "flat") == 0) {
2075 /* All subsequent ARG directives are for a 32-bit stack */
2077 StackPointer
= "ebp";
2080 } else if (nasm_stricmp(tline
->text
, "flat64") == 0) {
2081 /* All subsequent ARG directives are for a 64-bit stack */
2083 StackPointer
= "rbp";
2086 } else if (nasm_stricmp(tline
->text
, "large") == 0) {
2087 /* All subsequent ARG directives are for a 16-bit stack,
2088 * far function call.
2091 StackPointer
= "bp";
2094 } else if (nasm_stricmp(tline
->text
, "small") == 0) {
2095 /* All subsequent ARG directives are for a 16-bit stack,
2096 * far function call. We don't support near functions.
2099 StackPointer
= "bp";
2103 error(ERR_NONFATAL
, "`%%stacksize' invalid size type");
2104 free_tlist(origline
);
2105 return DIRECTIVE_FOUND
;
2107 free_tlist(origline
);
2108 return DIRECTIVE_FOUND
;
2111 /* TASM like ARG directive to define arguments to functions, in
2112 * the following form:
2114 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2118 char *arg
, directive
[256];
2119 int size
= StackSize
;
2121 /* Find the argument name */
2122 tline
= tline
->next
;
2123 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2124 tline
= tline
->next
;
2125 if (!tline
|| tline
->type
!= TOK_ID
) {
2126 error(ERR_NONFATAL
, "`%%arg' missing argument parameter");
2127 free_tlist(origline
);
2128 return DIRECTIVE_FOUND
;
2132 /* Find the argument size type */
2133 tline
= tline
->next
;
2134 if (!tline
|| tline
->type
!= TOK_OTHER
2135 || tline
->text
[0] != ':') {
2137 "Syntax error processing `%%arg' directive");
2138 free_tlist(origline
);
2139 return DIRECTIVE_FOUND
;
2141 tline
= tline
->next
;
2142 if (!tline
|| tline
->type
!= TOK_ID
) {
2143 error(ERR_NONFATAL
, "`%%arg' missing size type parameter");
2144 free_tlist(origline
);
2145 return DIRECTIVE_FOUND
;
2148 /* Allow macro expansion of type parameter */
2149 tt
= tokenize(tline
->text
);
2150 tt
= expand_smacro(tt
);
2151 size
= parse_size(tt
->text
);
2154 "Invalid size type for `%%arg' missing directive");
2156 free_tlist(origline
);
2157 return DIRECTIVE_FOUND
;
2161 /* Round up to even stack slots */
2162 size
= (size
+StackSize
-1) & ~(StackSize
-1);
2164 /* Now define the macro for the argument */
2165 snprintf(directive
, sizeof(directive
), "%%define %s (%s+%d)",
2166 arg
, StackPointer
, offset
);
2167 do_directive(tokenize(directive
));
2170 /* Move to the next argument in the list */
2171 tline
= tline
->next
;
2172 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2173 tline
= tline
->next
;
2174 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2176 free_tlist(origline
);
2177 return DIRECTIVE_FOUND
;
2180 /* TASM like LOCAL directive to define local variables for a
2181 * function, in the following form:
2183 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2185 * The '= LocalSize' at the end is ignored by NASM, but is
2186 * required by TASM to define the local parameter size (and used
2187 * by the TASM macro package).
2189 offset
= LocalOffset
;
2191 char *local
, directive
[256];
2192 int size
= StackSize
;
2194 /* Find the argument name */
2195 tline
= tline
->next
;
2196 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2197 tline
= tline
->next
;
2198 if (!tline
|| tline
->type
!= TOK_ID
) {
2200 "`%%local' missing argument parameter");
2201 free_tlist(origline
);
2202 return DIRECTIVE_FOUND
;
2204 local
= tline
->text
;
2206 /* Find the argument size type */
2207 tline
= tline
->next
;
2208 if (!tline
|| tline
->type
!= TOK_OTHER
2209 || tline
->text
[0] != ':') {
2211 "Syntax error processing `%%local' directive");
2212 free_tlist(origline
);
2213 return DIRECTIVE_FOUND
;
2215 tline
= tline
->next
;
2216 if (!tline
|| tline
->type
!= TOK_ID
) {
2218 "`%%local' missing size type parameter");
2219 free_tlist(origline
);
2220 return DIRECTIVE_FOUND
;
2223 /* Allow macro expansion of type parameter */
2224 tt
= tokenize(tline
->text
);
2225 tt
= expand_smacro(tt
);
2226 size
= parse_size(tt
->text
);
2229 "Invalid size type for `%%local' missing directive");
2231 free_tlist(origline
);
2232 return DIRECTIVE_FOUND
;
2236 /* Round up to even stack slots */
2237 size
= (size
+StackSize
-1) & ~(StackSize
-1);
2239 offset
+= size
; /* Negative offset, increment before */
2241 /* Now define the macro for the argument */
2242 snprintf(directive
, sizeof(directive
), "%%define %s (%s-%d)",
2243 local
, StackPointer
, offset
);
2244 do_directive(tokenize(directive
));
2246 /* Now define the assign to setup the enter_c macro correctly */
2247 snprintf(directive
, sizeof(directive
),
2248 "%%assign %%$localsize %%$localsize+%d", size
);
2249 do_directive(tokenize(directive
));
2251 /* Move to the next argument in the list */
2252 tline
= tline
->next
;
2253 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2254 tline
= tline
->next
;
2255 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2256 LocalOffset
= offset
;
2257 free_tlist(origline
);
2258 return DIRECTIVE_FOUND
;
2262 error(ERR_WARNING
|ERR_PASS1
,
2263 "trailing garbage after `%%clear' ignored");
2266 free_tlist(origline
);
2267 return DIRECTIVE_FOUND
;
2270 t
= tline
->next
= expand_smacro(tline
->next
);
2272 if (!t
|| (t
->type
!= TOK_STRING
&&
2273 t
->type
!= TOK_INTERNAL_STRING
)) {
2274 error(ERR_NONFATAL
, "`%%depend' expects a file name");
2275 free_tlist(origline
);
2276 return DIRECTIVE_FOUND
; /* but we did _something_ */
2279 error(ERR_WARNING
|ERR_PASS1
,
2280 "trailing garbage after `%%depend' ignored");
2282 if (t
->type
!= TOK_INTERNAL_STRING
)
2283 nasm_unquote(p
, NULL
);
2284 if (dephead
&& !in_list(*dephead
, p
)) {
2285 StrList
*sl
= nasm_malloc(strlen(p
)+1+sizeof sl
->next
);
2289 deptail
= &sl
->next
;
2291 free_tlist(origline
);
2292 return DIRECTIVE_FOUND
;
2295 t
= tline
->next
= expand_smacro(tline
->next
);
2298 if (!t
|| (t
->type
!= TOK_STRING
&&
2299 t
->type
!= TOK_INTERNAL_STRING
)) {
2300 error(ERR_NONFATAL
, "`%%include' expects a file name");
2301 free_tlist(origline
);
2302 return DIRECTIVE_FOUND
; /* but we did _something_ */
2305 error(ERR_WARNING
|ERR_PASS1
,
2306 "trailing garbage after `%%include' ignored");
2308 if (t
->type
!= TOK_INTERNAL_STRING
)
2309 nasm_unquote(p
, NULL
);
2310 inc
= nasm_malloc(sizeof(Include
));
2313 inc
->fp
= inc_fopen(p
, dephead
, &deptail
, pass
== 0);
2315 /* -MG given but file not found */
2318 inc
->fname
= src_set_fname(nasm_strdup(p
));
2319 inc
->lineno
= src_set_linnum(0);
2321 inc
->expansion
= NULL
;
2324 list
->uplevel(LIST_INCLUDE
);
2326 free_tlist(origline
);
2327 return DIRECTIVE_FOUND
;
2331 static macros_t
*use_pkg
;
2332 const char *pkg_macro
;
2334 tline
= tline
->next
;
2336 tline
= expand_id(tline
);
2338 if (!tline
|| (tline
->type
!= TOK_STRING
&&
2339 tline
->type
!= TOK_INTERNAL_STRING
&&
2340 tline
->type
!= TOK_ID
)) {
2341 error(ERR_NONFATAL
, "`%%use' expects a package name");
2342 free_tlist(origline
);
2343 return DIRECTIVE_FOUND
; /* but we did _something_ */
2346 error(ERR_WARNING
|ERR_PASS1
,
2347 "trailing garbage after `%%use' ignored");
2348 if (tline
->type
== TOK_STRING
)
2349 nasm_unquote(tline
->text
, NULL
);
2350 use_pkg
= nasm_stdmac_find_package(tline
->text
);
2352 error(ERR_NONFATAL
, "unknown `%%use' package: %s", tline
->text
);
2353 /* The first string will be <%define>__USE_*__ */
2354 pkg_macro
= (char *)use_pkg
+ 1;
2355 if (!smacro_defined(NULL
, pkg_macro
, 0, NULL
, true)) {
2356 /* Not already included, go ahead and include it */
2357 stdmacpos
= use_pkg
;
2359 free_tlist(origline
);
2360 return DIRECTIVE_FOUND
;
2365 tline
= tline
->next
;
2367 tline
= expand_id(tline
);
2369 if (!tok_type_(tline
, TOK_ID
)) {
2370 error(ERR_NONFATAL
, "`%s' expects a context identifier",
2372 free_tlist(origline
);
2373 return DIRECTIVE_FOUND
; /* but we did _something_ */
2376 error(ERR_WARNING
|ERR_PASS1
,
2377 "trailing garbage after `%s' ignored",
2379 p
= nasm_strdup(tline
->text
);
2381 p
= NULL
; /* Anonymous */
2385 ctx
= nasm_malloc(sizeof(Context
));
2387 hash_init(&ctx
->localmac
, HASH_SMALL
);
2389 ctx
->number
= unique
++;
2394 error(ERR_NONFATAL
, "`%s': context stack is empty",
2396 } else if (i
== PP_POP
) {
2397 if (p
&& (!cstk
->name
|| nasm_stricmp(p
, cstk
->name
)))
2398 error(ERR_NONFATAL
, "`%%pop' in wrong context: %s, "
2400 cstk
->name
? cstk
->name
: "anonymous", p
);
2405 nasm_free(cstk
->name
);
2411 free_tlist(origline
);
2412 return DIRECTIVE_FOUND
;
2414 severity
= ERR_FATAL
|ERR_NO_SEVERITY
;
2417 severity
= ERR_NONFATAL
|ERR_NO_SEVERITY
;
2420 severity
= ERR_WARNING
|ERR_NO_SEVERITY
|ERR_WARN_USER
;
2425 /* Only error out if this is the final pass */
2426 if (pass
!= 2 && i
!= PP_FATAL
)
2427 return DIRECTIVE_FOUND
;
2429 tline
->next
= expand_smacro(tline
->next
);
2430 tline
= tline
->next
;
2432 t
= tline
? tline
->next
: NULL
;
2434 if (tok_type_(tline
, TOK_STRING
) && !t
) {
2435 /* The line contains only a quoted string */
2437 nasm_unquote(p
, NULL
);
2438 error(severity
, "%s: %s", pp_directives
[i
], p
);
2440 /* Not a quoted string, or more than a quoted string */
2441 p
= detoken(tline
, false);
2442 error(severity
, "%s: %s", pp_directives
[i
], p
);
2445 free_tlist(origline
);
2446 return DIRECTIVE_FOUND
;
2450 if (istk
->conds
&& !emitting(istk
->conds
->state
))
2453 j
= if_condition(tline
->next
, i
);
2454 tline
->next
= NULL
; /* it got freed */
2455 j
= j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2457 cond
= nasm_malloc(sizeof(Cond
));
2458 cond
->next
= istk
->conds
;
2461 free_tlist(origline
);
2462 return DIRECTIVE_FOUND
;
2466 error(ERR_FATAL
, "`%s': no matching `%%if'", pp_directives
[i
]);
2467 switch(istk
->conds
->state
) {
2469 istk
->conds
->state
= COND_DONE
;
2476 case COND_ELSE_TRUE
:
2477 case COND_ELSE_FALSE
:
2478 error_precond(ERR_WARNING
|ERR_PASS1
,
2479 "`%%elif' after `%%else' ignored");
2480 istk
->conds
->state
= COND_NEVER
;
2485 * IMPORTANT: In the case of %if, we will already have
2486 * called expand_mmac_params(); however, if we're
2487 * processing an %elif we must have been in a
2488 * non-emitting mode, which would have inhibited
2489 * the normal invocation of expand_mmac_params().
2490 * Therefore, we have to do it explicitly here.
2492 j
= if_condition(expand_mmac_params(tline
->next
), i
);
2493 tline
->next
= NULL
; /* it got freed */
2494 istk
->conds
->state
=
2495 j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2498 free_tlist(origline
);
2499 return DIRECTIVE_FOUND
;
2503 error_precond(ERR_WARNING
|ERR_PASS1
,
2504 "trailing garbage after `%%else' ignored");
2506 error(ERR_FATAL
, "`%%else': no matching `%%if'");
2507 switch(istk
->conds
->state
) {
2510 istk
->conds
->state
= COND_ELSE_FALSE
;
2517 istk
->conds
->state
= COND_ELSE_TRUE
;
2520 case COND_ELSE_TRUE
:
2521 case COND_ELSE_FALSE
:
2522 error_precond(ERR_WARNING
|ERR_PASS1
,
2523 "`%%else' after `%%else' ignored.");
2524 istk
->conds
->state
= COND_NEVER
;
2527 free_tlist(origline
);
2528 return DIRECTIVE_FOUND
;
2532 error_precond(ERR_WARNING
|ERR_PASS1
,
2533 "trailing garbage after `%%endif' ignored");
2535 error(ERR_FATAL
, "`%%endif': no matching `%%if'");
2537 istk
->conds
= cond
->next
;
2539 free_tlist(origline
);
2540 return DIRECTIVE_FOUND
;
2546 "`%%%smacro': already defining a macro",
2547 (i
== PP_IMACRO
? "i" : ""));
2548 return DIRECTIVE_FOUND
;
2550 defining
= nasm_malloc(sizeof(MMacro
));
2551 defining
->casesense
= (i
== PP_MACRO
);
2552 if (!parse_mmacro_spec(tline
, defining
, pp_directives
[i
])) {
2553 nasm_free(defining
);
2555 return DIRECTIVE_FOUND
;
2558 mmac
= (MMacro
*) hash_findix(&mmacros
, defining
->name
);
2560 if (!strcmp(mmac
->name
, defining
->name
) &&
2561 (mmac
->nparam_min
<= defining
->nparam_max
2563 && (defining
->nparam_min
<= mmac
->nparam_max
2565 error(ERR_WARNING
|ERR_PASS1
,
2566 "redefining multi-line macro `%s'", defining
->name
);
2567 return DIRECTIVE_FOUND
;
2571 free_tlist(origline
);
2572 return DIRECTIVE_FOUND
;
2576 if (! (defining
&& defining
->name
)) {
2577 error(ERR_NONFATAL
, "`%s': not defining a macro", tline
->text
);
2578 return DIRECTIVE_FOUND
;
2580 mmhead
= (MMacro
**) hash_findi_add(&mmacros
, defining
->name
);
2581 defining
->next
= *mmhead
;
2584 free_tlist(origline
);
2585 return DIRECTIVE_FOUND
;
2593 spec
.casesense
= (i
== PP_UNMACRO
);
2594 if (!parse_mmacro_spec(tline
, &spec
, pp_directives
[i
])) {
2595 return DIRECTIVE_FOUND
;
2597 mmac_p
= (MMacro
**) hash_findi(&mmacros
, spec
.name
, NULL
);
2598 while (mmac_p
&& *mmac_p
) {
2600 if (mmac
->casesense
== spec
.casesense
&&
2601 !mstrcmp(mmac
->name
, spec
.name
, spec
.casesense
) &&
2602 mmac
->nparam_min
== spec
.nparam_min
&&
2603 mmac
->nparam_max
== spec
.nparam_max
&&
2604 mmac
->plus
== spec
.plus
) {
2605 *mmac_p
= mmac
->next
;
2608 mmac_p
= &mmac
->next
;
2611 free_tlist(origline
);
2612 free_tlist(spec
.dlist
);
2613 return DIRECTIVE_FOUND
;
2617 if (tline
->next
&& tline
->next
->type
== TOK_WHITESPACE
)
2618 tline
= tline
->next
;
2619 if (tline
->next
== NULL
) {
2620 free_tlist(origline
);
2621 error(ERR_NONFATAL
, "`%%rotate' missing rotate count");
2622 return DIRECTIVE_FOUND
;
2624 t
= expand_smacro(tline
->next
);
2626 free_tlist(origline
);
2629 tokval
.t_type
= TOKEN_INVALID
;
2631 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2634 return DIRECTIVE_FOUND
;
2636 error(ERR_WARNING
|ERR_PASS1
,
2637 "trailing garbage after expression ignored");
2638 if (!is_simple(evalresult
)) {
2639 error(ERR_NONFATAL
, "non-constant value given to `%%rotate'");
2640 return DIRECTIVE_FOUND
;
2643 while (mmac
&& !mmac
->name
) /* avoid mistaking %reps for macros */
2644 mmac
= mmac
->next_active
;
2646 error(ERR_NONFATAL
, "`%%rotate' invoked outside a macro call");
2647 } else if (mmac
->nparam
== 0) {
2649 "`%%rotate' invoked within macro without parameters");
2651 int rotate
= mmac
->rotate
+ reloc_value(evalresult
);
2653 rotate
%= (int)mmac
->nparam
;
2655 rotate
+= mmac
->nparam
;
2657 mmac
->rotate
= rotate
;
2659 return DIRECTIVE_FOUND
;
2664 tline
= tline
->next
;
2665 } while (tok_type_(tline
, TOK_WHITESPACE
));
2667 if (tok_type_(tline
, TOK_ID
) &&
2668 nasm_stricmp(tline
->text
, ".nolist") == 0) {
2671 tline
= tline
->next
;
2672 } while (tok_type_(tline
, TOK_WHITESPACE
));
2676 t
= expand_smacro(tline
);
2678 tokval
.t_type
= TOKEN_INVALID
;
2680 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2682 free_tlist(origline
);
2683 return DIRECTIVE_FOUND
;
2686 error(ERR_WARNING
|ERR_PASS1
,
2687 "trailing garbage after expression ignored");
2688 if (!is_simple(evalresult
)) {
2689 error(ERR_NONFATAL
, "non-constant value given to `%%rep'");
2690 return DIRECTIVE_FOUND
;
2692 count
= reloc_value(evalresult
) + 1;
2694 error(ERR_NONFATAL
, "`%%rep' expects a repeat count");
2697 free_tlist(origline
);
2699 tmp_defining
= defining
;
2700 defining
= nasm_malloc(sizeof(MMacro
));
2701 defining
->name
= NULL
; /* flags this macro as a %rep block */
2702 defining
->casesense
= false;
2703 defining
->plus
= false;
2704 defining
->nolist
= nolist
;
2705 defining
->in_progress
= count
;
2706 defining
->nparam_min
= defining
->nparam_max
= 0;
2707 defining
->defaults
= NULL
;
2708 defining
->dlist
= NULL
;
2709 defining
->expansion
= NULL
;
2710 defining
->next_active
= istk
->mstk
;
2711 defining
->rep_nest
= tmp_defining
;
2712 return DIRECTIVE_FOUND
;
2715 if (!defining
|| defining
->name
) {
2716 error(ERR_NONFATAL
, "`%%endrep': no matching `%%rep'");
2717 return DIRECTIVE_FOUND
;
2721 * Now we have a "macro" defined - although it has no name
2722 * and we won't be entering it in the hash tables - we must
2723 * push a macro-end marker for it on to istk->expansion.
2724 * After that, it will take care of propagating itself (a
2725 * macro-end marker line for a macro which is really a %rep
2726 * block will cause the macro to be re-expanded, complete
2727 * with another macro-end marker to ensure the process
2728 * continues) until the whole expansion is forcibly removed
2729 * from istk->expansion by a %exitrep.
2731 l
= nasm_malloc(sizeof(Line
));
2732 l
->next
= istk
->expansion
;
2733 l
->finishes
= defining
;
2735 istk
->expansion
= l
;
2737 istk
->mstk
= defining
;
2739 list
->uplevel(defining
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
2740 tmp_defining
= defining
;
2741 defining
= defining
->rep_nest
;
2742 free_tlist(origline
);
2743 return DIRECTIVE_FOUND
;
2747 * We must search along istk->expansion until we hit a
2748 * macro-end marker for a macro with no name. Then we set
2749 * its `in_progress' flag to 0.
2751 for (l
= istk
->expansion
; l
; l
= l
->next
)
2752 if (l
->finishes
&& !l
->finishes
->name
)
2756 l
->finishes
->in_progress
= 1;
2758 error(ERR_NONFATAL
, "`%%exitrep' not within `%%rep' block");
2759 free_tlist(origline
);
2760 return DIRECTIVE_FOUND
;
2766 casesense
= (i
== PP_DEFINE
|| i
== PP_XDEFINE
);
2768 tline
= tline
->next
;
2770 tline
= expand_id(tline
);
2771 if (!tline
|| (tline
->type
!= TOK_ID
&&
2772 (tline
->type
!= TOK_PREPROC_ID
||
2773 tline
->text
[1] != '$'))) {
2774 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
2776 free_tlist(origline
);
2777 return DIRECTIVE_FOUND
;
2780 ctx
= get_ctx(tline
->text
, &mname
, false);
2782 param_start
= tline
= tline
->next
;
2785 /* Expand the macro definition now for %xdefine and %ixdefine */
2786 if ((i
== PP_XDEFINE
) || (i
== PP_IXDEFINE
))
2787 tline
= expand_smacro(tline
);
2789 if (tok_is_(tline
, "(")) {
2791 * This macro has parameters.
2794 tline
= tline
->next
;
2798 error(ERR_NONFATAL
, "parameter identifier expected");
2799 free_tlist(origline
);
2800 return DIRECTIVE_FOUND
;
2802 if (tline
->type
!= TOK_ID
) {
2804 "`%s': parameter identifier expected",
2806 free_tlist(origline
);
2807 return DIRECTIVE_FOUND
;
2809 tline
->type
= TOK_SMAC_PARAM
+ nparam
++;
2810 tline
= tline
->next
;
2812 if (tok_is_(tline
, ",")) {
2813 tline
= tline
->next
;
2815 if (!tok_is_(tline
, ")")) {
2817 "`)' expected to terminate macro template");
2818 free_tlist(origline
);
2819 return DIRECTIVE_FOUND
;
2825 tline
= tline
->next
;
2827 if (tok_type_(tline
, TOK_WHITESPACE
))
2828 last
= tline
, tline
= tline
->next
;
2833 if (t
->type
== TOK_ID
) {
2834 for (tt
= param_start
; tt
; tt
= tt
->next
)
2835 if (tt
->type
>= TOK_SMAC_PARAM
&&
2836 !strcmp(tt
->text
, t
->text
))
2840 t
->next
= macro_start
;
2845 * Good. We now have a macro name, a parameter count, and a
2846 * token list (in reverse order) for an expansion. We ought
2847 * to be OK just to create an SMacro, store it, and let
2848 * free_tlist have the rest of the line (which we have
2849 * carefully re-terminated after chopping off the expansion
2852 define_smacro(ctx
, mname
, casesense
, nparam
, macro_start
);
2853 free_tlist(origline
);
2854 return DIRECTIVE_FOUND
;
2857 tline
= tline
->next
;
2859 tline
= expand_id(tline
);
2860 if (!tline
|| (tline
->type
!= TOK_ID
&&
2861 (tline
->type
!= TOK_PREPROC_ID
||
2862 tline
->text
[1] != '$'))) {
2863 error(ERR_NONFATAL
, "`%%undef' expects a macro identifier");
2864 free_tlist(origline
);
2865 return DIRECTIVE_FOUND
;
2868 error(ERR_WARNING
|ERR_PASS1
,
2869 "trailing garbage after macro name ignored");
2872 /* Find the context that symbol belongs to */
2873 ctx
= get_ctx(tline
->text
, &mname
, false);
2874 undef_smacro(ctx
, mname
);
2875 free_tlist(origline
);
2876 return DIRECTIVE_FOUND
;
2880 casesense
= (i
== PP_DEFSTR
);
2882 tline
= tline
->next
;
2884 tline
= expand_id(tline
);
2885 if (!tline
|| (tline
->type
!= TOK_ID
&&
2886 (tline
->type
!= TOK_PREPROC_ID
||
2887 tline
->text
[1] != '$'))) {
2888 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
2890 free_tlist(origline
);
2891 return DIRECTIVE_FOUND
;
2894 ctx
= get_ctx(tline
->text
, &mname
, false);
2896 tline
= expand_smacro(tline
->next
);
2899 while (tok_type_(tline
, TOK_WHITESPACE
))
2900 tline
= delete_Token(tline
);
2902 p
= detoken(tline
, false);
2903 macro_start
= nasm_malloc(sizeof(*macro_start
));
2904 macro_start
->next
= NULL
;
2905 macro_start
->text
= nasm_quote(p
, strlen(p
));
2906 macro_start
->type
= TOK_STRING
;
2907 macro_start
->a
.mac
= NULL
;
2911 * We now have a macro name, an implicit parameter count of
2912 * zero, and a string token to use as an expansion. Create
2913 * and store an SMacro.
2915 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2916 free_tlist(origline
);
2917 return DIRECTIVE_FOUND
;
2922 StrList
*xsl
= NULL
;
2923 StrList
**xst
= &xsl
;
2927 tline
= tline
->next
;
2929 tline
= expand_id(tline
);
2930 if (!tline
|| (tline
->type
!= TOK_ID
&&
2931 (tline
->type
!= TOK_PREPROC_ID
||
2932 tline
->text
[1] != '$'))) {
2934 "`%%pathsearch' expects a macro identifier as first parameter");
2935 free_tlist(origline
);
2936 return DIRECTIVE_FOUND
;
2938 ctx
= get_ctx(tline
->text
, &mname
, false);
2940 tline
= expand_smacro(tline
->next
);
2944 while (tok_type_(t
, TOK_WHITESPACE
))
2947 if (!t
|| (t
->type
!= TOK_STRING
&&
2948 t
->type
!= TOK_INTERNAL_STRING
)) {
2949 error(ERR_NONFATAL
, "`%%pathsearch' expects a file name");
2951 free_tlist(origline
);
2952 return DIRECTIVE_FOUND
; /* but we did _something_ */
2955 error(ERR_WARNING
|ERR_PASS1
,
2956 "trailing garbage after `%%pathsearch' ignored");
2958 if (t
->type
!= TOK_INTERNAL_STRING
)
2959 nasm_unquote(p
, NULL
);
2961 fp
= inc_fopen(p
, &xsl
, &xst
, true);
2964 fclose(fp
); /* Don't actually care about the file */
2966 macro_start
= nasm_malloc(sizeof(*macro_start
));
2967 macro_start
->next
= NULL
;
2968 macro_start
->text
= nasm_quote(p
, strlen(p
));
2969 macro_start
->type
= TOK_STRING
;
2970 macro_start
->a
.mac
= NULL
;
2975 * We now have a macro name, an implicit parameter count of
2976 * zero, and a string token to use as an expansion. Create
2977 * and store an SMacro.
2979 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2981 free_tlist(origline
);
2982 return DIRECTIVE_FOUND
;
2988 tline
= tline
->next
;
2990 tline
= expand_id(tline
);
2991 if (!tline
|| (tline
->type
!= TOK_ID
&&
2992 (tline
->type
!= TOK_PREPROC_ID
||
2993 tline
->text
[1] != '$'))) {
2995 "`%%strlen' expects a macro identifier as first parameter");
2996 free_tlist(origline
);
2997 return DIRECTIVE_FOUND
;
2999 ctx
= get_ctx(tline
->text
, &mname
, false);
3001 tline
= expand_smacro(tline
->next
);
3005 while (tok_type_(t
, TOK_WHITESPACE
))
3007 /* t should now point to the string */
3008 if (t
->type
!= TOK_STRING
) {
3010 "`%%strlen` requires string as second parameter");
3012 free_tlist(origline
);
3013 return DIRECTIVE_FOUND
;
3016 macro_start
= nasm_malloc(sizeof(*macro_start
));
3017 macro_start
->next
= NULL
;
3018 make_tok_num(macro_start
, nasm_unquote(t
->text
, NULL
));
3019 macro_start
->a
.mac
= NULL
;
3022 * We now have a macro name, an implicit parameter count of
3023 * zero, and a numeric token to use as an expansion. Create
3024 * and store an SMacro.
3026 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3028 free_tlist(origline
);
3029 return DIRECTIVE_FOUND
;
3034 tline
= tline
->next
;
3036 tline
= expand_id(tline
);
3037 if (!tline
|| (tline
->type
!= TOK_ID
&&
3038 (tline
->type
!= TOK_PREPROC_ID
||
3039 tline
->text
[1] != '$'))) {
3041 "`%%strcat' expects a macro identifier as first parameter");
3042 free_tlist(origline
);
3043 return DIRECTIVE_FOUND
;
3045 ctx
= get_ctx(tline
->text
, &mname
, false);
3047 tline
= expand_smacro(tline
->next
);
3051 for (t
= tline
; t
; t
= t
->next
) {
3053 case TOK_WHITESPACE
:
3056 len
+= t
->a
.len
= nasm_unquote(t
->text
, NULL
);
3059 if (!strcmp(t
->text
, ",")) /* permit comma separators */
3061 /* else fall through */
3064 "non-string passed to `%%strcat' (%d)", t
->type
);
3066 free_tlist(origline
);
3067 return DIRECTIVE_FOUND
;
3071 p
= pp
= nasm_malloc(len
);
3073 for (t
= tline
; t
; t
= t
->next
) {
3074 if (t
->type
== TOK_STRING
) {
3075 memcpy(p
, t
->text
, t
->a
.len
);
3081 * We now have a macro name, an implicit parameter count of
3082 * zero, and a numeric token to use as an expansion. Create
3083 * and store an SMacro.
3085 macro_start
= new_Token(NULL
, TOK_STRING
, NULL
, 0);
3086 macro_start
->text
= nasm_quote(pp
, len
);
3088 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3090 free_tlist(origline
);
3091 return DIRECTIVE_FOUND
;
3100 tline
= tline
->next
;
3102 tline
= expand_id(tline
);
3103 if (!tline
|| (tline
->type
!= TOK_ID
&&
3104 (tline
->type
!= TOK_PREPROC_ID
||
3105 tline
->text
[1] != '$'))) {
3107 "`%%substr' expects a macro identifier as first parameter");
3108 free_tlist(origline
);
3109 return DIRECTIVE_FOUND
;
3111 ctx
= get_ctx(tline
->text
, &mname
, false);
3113 tline
= expand_smacro(tline
->next
);
3117 while (tok_type_(t
, TOK_WHITESPACE
))
3120 /* t should now point to the string */
3121 if (t
->type
!= TOK_STRING
) {
3123 "`%%substr` requires string as second parameter");
3125 free_tlist(origline
);
3126 return DIRECTIVE_FOUND
;
3131 tokval
.t_type
= TOKEN_INVALID
;
3132 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
,
3136 free_tlist(origline
);
3137 return DIRECTIVE_FOUND
;
3138 } else if (!is_simple(evalresult
)) {
3139 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
3141 free_tlist(origline
);
3142 return DIRECTIVE_FOUND
;
3144 a1
= evalresult
->value
-1;
3146 while (tok_type_(tt
, TOK_WHITESPACE
))
3149 a2
= 1; /* Backwards compatibility: one character */
3151 tokval
.t_type
= TOKEN_INVALID
;
3152 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
,
3156 free_tlist(origline
);
3157 return DIRECTIVE_FOUND
;
3158 } else if (!is_simple(evalresult
)) {
3159 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
3161 free_tlist(origline
);
3162 return DIRECTIVE_FOUND
;
3164 a2
= evalresult
->value
;
3167 len
= nasm_unquote(t
->text
, NULL
);
3170 if (a1
+a2
> (int64_t)len
)
3173 macro_start
= nasm_malloc(sizeof(*macro_start
));
3174 macro_start
->next
= NULL
;
3175 macro_start
->text
= nasm_quote((a1
< 0) ? "" : t
->text
+a1
, a2
);
3176 macro_start
->type
= TOK_STRING
;
3177 macro_start
->a
.mac
= NULL
;
3180 * We now have a macro name, an implicit parameter count of
3181 * zero, and a numeric token to use as an expansion. Create
3182 * and store an SMacro.
3184 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3186 free_tlist(origline
);
3187 return DIRECTIVE_FOUND
;
3192 casesense
= (i
== PP_ASSIGN
);
3194 tline
= tline
->next
;
3196 tline
= expand_id(tline
);
3197 if (!tline
|| (tline
->type
!= TOK_ID
&&
3198 (tline
->type
!= TOK_PREPROC_ID
||
3199 tline
->text
[1] != '$'))) {
3201 "`%%%sassign' expects a macro identifier",
3202 (i
== PP_IASSIGN
? "i" : ""));
3203 free_tlist(origline
);
3204 return DIRECTIVE_FOUND
;
3206 ctx
= get_ctx(tline
->text
, &mname
, false);
3208 tline
= expand_smacro(tline
->next
);
3213 tokval
.t_type
= TOKEN_INVALID
;
3215 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
3218 free_tlist(origline
);
3219 return DIRECTIVE_FOUND
;
3223 error(ERR_WARNING
|ERR_PASS1
,
3224 "trailing garbage after expression ignored");
3226 if (!is_simple(evalresult
)) {
3228 "non-constant value given to `%%%sassign'",
3229 (i
== PP_IASSIGN
? "i" : ""));
3230 free_tlist(origline
);
3231 return DIRECTIVE_FOUND
;
3234 macro_start
= nasm_malloc(sizeof(*macro_start
));
3235 macro_start
->next
= NULL
;
3236 make_tok_num(macro_start
, reloc_value(evalresult
));
3237 macro_start
->a
.mac
= NULL
;
3240 * We now have a macro name, an implicit parameter count of
3241 * zero, and a numeric token to use as an expansion. Create
3242 * and store an SMacro.
3244 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3245 free_tlist(origline
);
3246 return DIRECTIVE_FOUND
;
3250 * Syntax is `%line nnn[+mmm] [filename]'
3252 tline
= tline
->next
;
3254 if (!tok_type_(tline
, TOK_NUMBER
)) {
3255 error(ERR_NONFATAL
, "`%%line' expects line number");
3256 free_tlist(origline
);
3257 return DIRECTIVE_FOUND
;
3259 k
= readnum(tline
->text
, &err
);
3261 tline
= tline
->next
;
3262 if (tok_is_(tline
, "+")) {
3263 tline
= tline
->next
;
3264 if (!tok_type_(tline
, TOK_NUMBER
)) {
3265 error(ERR_NONFATAL
, "`%%line' expects line increment");
3266 free_tlist(origline
);
3267 return DIRECTIVE_FOUND
;
3269 m
= readnum(tline
->text
, &err
);
3270 tline
= tline
->next
;
3276 nasm_free(src_set_fname(detoken(tline
, false)));
3278 free_tlist(origline
);
3279 return DIRECTIVE_FOUND
;
3283 "preprocessor directive `%s' not yet implemented",
3285 return DIRECTIVE_FOUND
;
3290 * Ensure that a macro parameter contains a condition code and
3291 * nothing else. Return the condition code index if so, or -1
3294 static int find_cc(Token
* t
)
3300 return -1; /* Probably a %+ without a space */
3303 if (t
->type
!= TOK_ID
)
3307 if (tt
&& (tt
->type
!= TOK_OTHER
|| strcmp(tt
->text
, ",")))
3311 j
= elements(conditions
);
3314 m
= nasm_stricmp(t
->text
, conditions
[k
]);
3330 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3331 * %-n) and MMacro-local identifiers (%%foo) as well as
3332 * macro indirection (%[...]).
3334 static Token
*expand_mmac_params(Token
* tline
)
3336 Token
*t
, *tt
, **tail
, *thead
;
3337 bool changed
= false;
3343 if (tline
->type
== TOK_PREPROC_ID
&&
3344 (((tline
->text
[1] == '+' || tline
->text
[1] == '-')
3345 && tline
->text
[2]) || tline
->text
[1] == '%'
3346 || (tline
->text
[1] >= '0' && tline
->text
[1] <= '9'))) {
3348 int type
= 0, cc
; /* type = 0 to placate optimisers */
3355 tline
= tline
->next
;
3358 while (mac
&& !mac
->name
) /* avoid mistaking %reps for macros */
3359 mac
= mac
->next_active
;
3361 error(ERR_NONFATAL
, "`%s': not in a macro call", t
->text
);
3363 switch (t
->text
[1]) {
3365 * We have to make a substitution of one of the
3366 * forms %1, %-1, %+1, %%foo, %0.
3370 snprintf(tmpbuf
, sizeof(tmpbuf
), "%d", mac
->nparam
);
3371 text
= nasm_strdup(tmpbuf
);
3375 snprintf(tmpbuf
, sizeof(tmpbuf
), "..@%"PRIu64
".",
3377 text
= nasm_strcat(tmpbuf
, t
->text
+ 2);
3380 n
= atoi(t
->text
+ 2) - 1;
3381 if (n
>= mac
->nparam
)
3384 if (mac
->nparam
> 1)
3385 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3386 tt
= mac
->params
[n
];
3391 "macro parameter %d is not a condition code",
3396 if (inverse_ccs
[cc
] == -1) {
3398 "condition code `%s' is not invertible",
3402 text
= nasm_strdup(conditions
[inverse_ccs
[cc
]]);
3406 n
= atoi(t
->text
+ 2) - 1;
3407 if (n
>= mac
->nparam
)
3410 if (mac
->nparam
> 1)
3411 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3412 tt
= mac
->params
[n
];
3417 "macro parameter %d is not a condition code",
3422 text
= nasm_strdup(conditions
[cc
]);
3426 n
= atoi(t
->text
+ 1) - 1;
3427 if (n
>= mac
->nparam
)
3430 if (mac
->nparam
> 1)
3431 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3432 tt
= mac
->params
[n
];
3435 for (i
= 0; i
< mac
->paramlen
[n
]; i
++) {
3436 *tail
= new_Token(NULL
, tt
->type
, tt
->text
, 0);
3437 tail
= &(*tail
)->next
;
3441 text
= NULL
; /* we've done it here */
3456 } else if (tline
->type
== TOK_INDIRECT
) {
3458 tline
= tline
->next
;
3459 tt
= tokenize(t
->text
);
3460 tt
= expand_mmac_params(tt
);
3461 tt
= expand_smacro(tt
);
3464 tt
->a
.mac
= NULL
; /* Necessary? */
3472 tline
= tline
->next
;
3482 /* Now handle token pasting... */
3484 while ((t
= *tail
) && (tt
= t
->next
)) {
3486 case TOK_WHITESPACE
:
3487 if (tt
->type
== TOK_WHITESPACE
) {
3488 t
->next
= delete_Token(tt
);
3501 (tt
->type
== TOK_ID
|| tt
->type
== TOK_NUMBER
||
3502 tt
->type
== TOK_FLOAT
|| tt
->type
== TOK_OTHER
)) {
3503 len
+= strlen(tt
->text
);
3507 /* Now tt points to the first token after the potential
3509 if (tt
!= t
->next
) {
3510 /* We have at least two tokens... */
3511 len
+= strlen(t
->text
);
3512 p
= tmp
= nasm_malloc(len
+1);
3516 p
= strchr(p
, '\0');
3517 t
= delete_Token(t
);
3520 t
= *tail
= tokenize(tmp
);
3526 t
->next
= tt
; /* Attach the remaining token chain */
3540 * Expand all single-line macro calls made in the given line.
3541 * Return the expanded version of the line. The original is deemed
3542 * to be destroyed in the process. (In reality we'll just move
3543 * Tokens from input to output a lot of the time, rather than
3544 * actually bothering to destroy and replicate.)
3546 #define DEADMAN_LIMIT (1 << 20)
3548 static Token
*expand_smacro(Token
* tline
)
3550 Token
*t
, *tt
, *mstart
, **tail
, *thead
;
3551 struct hash_table
*smtbl
;
3552 SMacro
*head
= NULL
, *m
;
3555 unsigned int nparam
, sparam
;
3556 int brackets
, rescan
;
3557 Token
*org_tline
= tline
;
3560 int deadman
= DEADMAN_LIMIT
;
3563 * Trick: we should avoid changing the start token pointer since it can
3564 * be contained in "next" field of other token. Because of this
3565 * we allocate a copy of first token and work with it; at the end of
3566 * routine we copy it back
3570 new_Token(org_tline
->next
, org_tline
->type
, org_tline
->text
,
3572 tline
->a
.mac
= org_tline
->a
.mac
;
3573 nasm_free(org_tline
->text
);
3574 org_tline
->text
= NULL
;
3581 while (tline
) { /* main token loop */
3583 error(ERR_NONFATAL
, "interminable macro recursion");
3587 if ((mname
= tline
->text
)) {
3588 /* if this token is a local macro, look in local context */
3589 if (tline
->type
== TOK_ID
|| tline
->type
== TOK_PREPROC_ID
)
3590 ctx
= get_ctx(mname
, &mname
, true);
3593 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
3594 head
= (SMacro
*) hash_findix(smtbl
, mname
);
3597 * We've hit an identifier. As in is_mmacro below, we first
3598 * check whether the identifier is a single-line macro at
3599 * all, then think about checking for parameters if
3602 for (m
= head
; m
; m
= m
->next
)
3603 if (!mstrcmp(m
->name
, mname
, m
->casesense
))
3609 if (m
->nparam
== 0) {
3611 * Simple case: the macro is parameterless. Discard the
3612 * one token that the macro call took, and push the
3613 * expansion back on the to-do stack.
3615 if (!m
->expansion
) {
3616 if (!strcmp("__FILE__", m
->name
)) {
3619 src_get(&num
, &file
);
3620 tline
->text
= nasm_quote(file
, strlen(file
));
3621 tline
->type
= TOK_STRING
;
3625 if (!strcmp("__LINE__", m
->name
)) {
3626 nasm_free(tline
->text
);
3627 make_tok_num(tline
, src_get_linnum());
3630 if (!strcmp("__BITS__", m
->name
)) {
3631 nasm_free(tline
->text
);
3632 make_tok_num(tline
, globalbits
);
3635 tline
= delete_Token(tline
);
3640 * Complicated case: at least one macro with this name
3641 * exists and takes parameters. We must find the
3642 * parameters in the call, count them, find the SMacro
3643 * that corresponds to that form of the macro call, and
3644 * substitute for the parameters when we expand. What a
3647 /*tline = tline->next;
3648 skip_white_(tline); */
3651 while (tok_type_(t
, TOK_SMAC_END
)) {
3652 t
->a
.mac
->in_progress
= false;
3654 t
= tline
->next
= delete_Token(t
);
3657 } while (tok_type_(tline
, TOK_WHITESPACE
));
3658 if (!tok_is_(tline
, "(")) {
3660 * This macro wasn't called with parameters: ignore
3661 * the call. (Behaviour borrowed from gnu cpp.)
3670 sparam
= PARAM_DELTA
;
3671 params
= nasm_malloc(sparam
* sizeof(Token
*));
3672 params
[0] = tline
->next
;
3673 paramsize
= nasm_malloc(sparam
* sizeof(int));
3675 while (true) { /* parameter loop */
3677 * For some unusual expansions
3678 * which concatenates function call
3681 while (tok_type_(t
, TOK_SMAC_END
)) {
3682 t
->a
.mac
->in_progress
= false;
3684 t
= tline
->next
= delete_Token(t
);
3690 "macro call expects terminating `)'");
3693 if (tline
->type
== TOK_WHITESPACE
3695 if (paramsize
[nparam
])
3698 params
[nparam
] = tline
->next
;
3699 continue; /* parameter loop */
3701 if (tline
->type
== TOK_OTHER
3702 && tline
->text
[1] == 0) {
3703 char ch
= tline
->text
[0];
3704 if (ch
== ',' && !paren
&& brackets
<= 0) {
3705 if (++nparam
>= sparam
) {
3706 sparam
+= PARAM_DELTA
;
3707 params
= nasm_realloc(params
,
3712 nasm_realloc(paramsize
,
3716 params
[nparam
] = tline
->next
;
3717 paramsize
[nparam
] = 0;
3719 continue; /* parameter loop */
3722 (brackets
> 0 || (brackets
== 0 &&
3723 !paramsize
[nparam
])))
3725 if (!(brackets
++)) {
3726 params
[nparam
] = tline
->next
;
3727 continue; /* parameter loop */
3730 if (ch
== '}' && brackets
> 0)
3731 if (--brackets
== 0) {
3733 continue; /* parameter loop */
3735 if (ch
== '(' && !brackets
)
3737 if (ch
== ')' && brackets
<= 0)
3743 error(ERR_NONFATAL
, "braces do not "
3744 "enclose all of macro parameter");
3746 paramsize
[nparam
] += white
+ 1;
3748 } /* parameter loop */
3750 while (m
&& (m
->nparam
!= nparam
||
3751 mstrcmp(m
->name
, mname
,
3755 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MNP
,
3756 "macro `%s' exists, "
3757 "but not taking %d parameters",
3758 mstart
->text
, nparam
);
3761 if (m
&& m
->in_progress
)
3763 if (!m
) { /* in progess or didn't find '(' or wrong nparam */
3765 * Design question: should we handle !tline, which
3766 * indicates missing ')' here, or expand those
3767 * macros anyway, which requires the (t) test a few
3771 nasm_free(paramsize
);
3775 * Expand the macro: we are placed on the last token of the
3776 * call, so that we can easily split the call from the
3777 * following tokens. We also start by pushing an SMAC_END
3778 * token for the cycle removal.
3785 tt
= new_Token(tline
, TOK_SMAC_END
, NULL
, 0);
3787 m
->in_progress
= true;
3789 for (t
= m
->expansion
; t
; t
= t
->next
) {
3790 if (t
->type
>= TOK_SMAC_PARAM
) {
3791 Token
*pcopy
= tline
, **ptail
= &pcopy
;
3795 ttt
= params
[t
->type
- TOK_SMAC_PARAM
];
3796 for (i
= paramsize
[t
->type
- TOK_SMAC_PARAM
];
3799 new_Token(tline
, ttt
->type
, ttt
->text
,
3805 } else if (t
->type
== TOK_PREPROC_Q
) {
3806 tt
= new_Token(tline
, TOK_ID
, mname
, 0);
3808 } else if (t
->type
== TOK_PREPROC_QQ
) {
3809 tt
= new_Token(tline
, TOK_ID
, m
->name
, 0);
3812 tt
= new_Token(tline
, t
->type
, t
->text
, 0);
3818 * Having done that, get rid of the macro call, and clean
3819 * up the parameters.
3822 nasm_free(paramsize
);
3824 continue; /* main token loop */
3829 if (tline
->type
== TOK_SMAC_END
) {
3830 tline
->a
.mac
->in_progress
= false;
3831 tline
= delete_Token(tline
);
3834 tline
= tline
->next
;
3842 * Now scan the entire line and look for successive TOK_IDs that resulted
3843 * after expansion (they can't be produced by tokenize()). The successive
3844 * TOK_IDs should be concatenated.
3845 * Also we look for %+ tokens and concatenate the tokens before and after
3846 * them (without white spaces in between).
3851 while (t
&& t
->type
!= TOK_ID
&& t
->type
!= TOK_PREPROC_ID
)
3855 if (t
->next
->type
== TOK_ID
||
3856 t
->next
->type
== TOK_PREPROC_ID
||
3857 t
->next
->type
== TOK_NUMBER
) {
3858 char *p
= nasm_strcat(t
->text
, t
->next
->text
);
3860 t
->next
= delete_Token(t
->next
);
3863 } else if (t
->next
->type
== TOK_WHITESPACE
&& t
->next
->next
&&
3864 t
->next
->next
->type
== TOK_PREPROC_ID
&&
3865 strcmp(t
->next
->next
->text
, "%+") == 0) {
3866 /* free the next whitespace, the %+ token and next whitespace */
3868 for (i
= 1; i
<= 3; i
++) {
3870 || (i
!= 2 && t
->next
->type
!= TOK_WHITESPACE
))
3872 t
->next
= delete_Token(t
->next
);
3877 /* If we concatenaded something, re-scan the line for macros */
3885 *org_tline
= *thead
;
3886 /* since we just gave text to org_line, don't free it */
3888 delete_Token(thead
);
3890 /* the expression expanded to empty line;
3891 we can't return NULL for some reasons
3892 we just set the line to a single WHITESPACE token. */
3893 memset(org_tline
, 0, sizeof(*org_tline
));
3894 org_tline
->text
= NULL
;
3895 org_tline
->type
= TOK_WHITESPACE
;
3904 * Similar to expand_smacro but used exclusively with macro identifiers
3905 * right before they are fetched in. The reason is that there can be
3906 * identifiers consisting of several subparts. We consider that if there
3907 * are more than one element forming the name, user wants a expansion,
3908 * otherwise it will be left as-is. Example:
3912 * the identifier %$abc will be left as-is so that the handler for %define
3913 * will suck it and define the corresponding value. Other case:
3915 * %define _%$abc cde
3917 * In this case user wants name to be expanded *before* %define starts
3918 * working, so we'll expand %$abc into something (if it has a value;
3919 * otherwise it will be left as-is) then concatenate all successive
3922 static Token
*expand_id(Token
* tline
)
3924 Token
*cur
, *oldnext
= NULL
;
3926 if (!tline
|| !tline
->next
)
3931 (cur
->next
->type
== TOK_ID
||
3932 cur
->next
->type
== TOK_PREPROC_ID
3933 || cur
->next
->type
== TOK_NUMBER
))
3936 /* If identifier consists of just one token, don't expand */
3941 oldnext
= cur
->next
; /* Detach the tail past identifier */
3942 cur
->next
= NULL
; /* so that expand_smacro stops here */
3945 tline
= expand_smacro(tline
);
3948 /* expand_smacro possibly changhed tline; re-scan for EOL */
3950 while (cur
&& cur
->next
)
3953 cur
->next
= oldnext
;
3960 * Determine whether the given line constitutes a multi-line macro
3961 * call, and return the MMacro structure called if so. Doesn't have
3962 * to check for an initial label - that's taken care of in
3963 * expand_mmacro - but must check numbers of parameters. Guaranteed
3964 * to be called with tline->type == TOK_ID, so the putative macro
3965 * name is easy to find.
3967 static MMacro
*is_mmacro(Token
* tline
, Token
*** params_array
)
3973 head
= (MMacro
*) hash_findix(&mmacros
, tline
->text
);
3976 * Efficiency: first we see if any macro exists with the given
3977 * name. If not, we can return NULL immediately. _Then_ we
3978 * count the parameters, and then we look further along the
3979 * list if necessary to find the proper MMacro.
3981 for (m
= head
; m
; m
= m
->next
)
3982 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
3988 * OK, we have a potential macro. Count and demarcate the
3991 count_mmac_params(tline
->next
, &nparam
, ¶ms
);
3994 * So we know how many parameters we've got. Find the MMacro
3995 * structure that handles this number.
3998 if (m
->nparam_min
<= nparam
3999 && (m
->plus
|| nparam
<= m
->nparam_max
)) {
4001 * This one is right. Just check if cycle removal
4002 * prohibits us using it before we actually celebrate...
4004 if (m
->in_progress
) {
4007 "self-reference in multi-line macro `%s'", m
->name
);
4013 * It's right, and we can use it. Add its default
4014 * parameters to the end of our list if necessary.
4016 if (m
->defaults
&& nparam
< m
->nparam_min
+ m
->ndefs
) {
4018 nasm_realloc(params
,
4019 ((m
->nparam_min
+ m
->ndefs
+
4020 1) * sizeof(*params
)));
4021 while (nparam
< m
->nparam_min
+ m
->ndefs
) {
4022 params
[nparam
] = m
->defaults
[nparam
- m
->nparam_min
];
4027 * If we've gone over the maximum parameter count (and
4028 * we're in Plus mode), ignore parameters beyond
4031 if (m
->plus
&& nparam
> m
->nparam_max
)
4032 nparam
= m
->nparam_max
;
4034 * Then terminate the parameter list, and leave.
4036 if (!params
) { /* need this special case */
4037 params
= nasm_malloc(sizeof(*params
));
4040 params
[nparam
] = NULL
;
4041 *params_array
= params
;
4045 * This one wasn't right: look for the next one with the
4048 for (m
= m
->next
; m
; m
= m
->next
)
4049 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
4054 * After all that, we didn't find one with the right number of
4055 * parameters. Issue a warning, and fail to expand the macro.
4057 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MNP
,
4058 "macro `%s' exists, but not taking %d parameters",
4059 tline
->text
, nparam
);
4065 * Expand the multi-line macro call made by the given line, if
4066 * there is one to be expanded. If there is, push the expansion on
4067 * istk->expansion and return 1. Otherwise return 0.
4069 static int expand_mmacro(Token
* tline
)
4071 Token
*startline
= tline
;
4072 Token
*label
= NULL
;
4073 int dont_prepend
= 0;
4074 Token
**params
, *t
, *mtok
, *tt
;
4077 int i
, nparam
, *paramlen
;
4082 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
4083 if (!tok_type_(t
, TOK_ID
) && !tok_type_(t
, TOK_PREPROC_ID
))
4086 m
= is_mmacro(t
, ¶ms
);
4092 * We have an id which isn't a macro call. We'll assume
4093 * it might be a label; we'll also check to see if a
4094 * colon follows it. Then, if there's another id after
4095 * that lot, we'll check it again for macro-hood.
4099 if (tok_type_(t
, TOK_WHITESPACE
))
4100 last
= t
, t
= t
->next
;
4101 if (tok_is_(t
, ":")) {
4103 last
= t
, t
= t
->next
;
4104 if (tok_type_(t
, TOK_WHITESPACE
))
4105 last
= t
, t
= t
->next
;
4107 if (!tok_type_(t
, TOK_ID
) || (m
= is_mmacro(t
, ¶ms
)) == NULL
)
4115 * Fix up the parameters: this involves stripping leading and
4116 * trailing whitespace, then stripping braces if they are
4119 for (nparam
= 0; params
[nparam
]; nparam
++) ;
4120 paramlen
= nparam
? nasm_malloc(nparam
* sizeof(*paramlen
)) : NULL
;
4122 for (i
= 0; params
[i
]; i
++) {
4124 int comma
= (!m
->plus
|| i
< nparam
- 1);
4128 if (tok_is_(t
, "{"))
4129 t
= t
->next
, brace
= true, comma
= false;
4133 if (comma
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, ","))
4134 break; /* ... because we have hit a comma */
4135 if (comma
&& t
->type
== TOK_WHITESPACE
4136 && tok_is_(t
->next
, ","))
4137 break; /* ... or a space then a comma */
4138 if (brace
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, "}"))
4139 break; /* ... or a brace */
4146 * OK, we have a MMacro structure together with a set of
4147 * parameters. We must now go through the expansion and push
4148 * copies of each Line on to istk->expansion. Substitution of
4149 * parameter tokens and macro-local tokens doesn't get done
4150 * until the single-line macro substitution process; this is
4151 * because delaying them allows us to change the semantics
4152 * later through %rotate.
4154 * First, push an end marker on to istk->expansion, mark this
4155 * macro as in progress, and set up its invocation-specific
4158 ll
= nasm_malloc(sizeof(Line
));
4159 ll
->next
= istk
->expansion
;
4162 istk
->expansion
= ll
;
4164 m
->in_progress
= true;
4169 m
->paramlen
= paramlen
;
4170 m
->unique
= unique
++;
4173 m
->next_active
= istk
->mstk
;
4176 for (l
= m
->expansion
; l
; l
= l
->next
) {
4179 ll
= nasm_malloc(sizeof(Line
));
4180 ll
->finishes
= NULL
;
4181 ll
->next
= istk
->expansion
;
4182 istk
->expansion
= ll
;
4185 for (t
= l
->first
; t
; t
= t
->next
) {
4189 tt
= *tail
= new_Token(NULL
, TOK_ID
, mname
, 0);
4191 case TOK_PREPROC_QQ
:
4192 tt
= *tail
= new_Token(NULL
, TOK_ID
, m
->name
, 0);
4194 case TOK_PREPROC_ID
:
4195 if (t
->text
[1] == '0' && t
->text
[2] == '0') {
4203 tt
= *tail
= new_Token(NULL
, x
->type
, x
->text
, 0);
4212 * If we had a label, push it on as the first line of
4213 * the macro expansion.
4216 if (dont_prepend
< 0)
4217 free_tlist(startline
);
4219 ll
= nasm_malloc(sizeof(Line
));
4220 ll
->finishes
= NULL
;
4221 ll
->next
= istk
->expansion
;
4222 istk
->expansion
= ll
;
4223 ll
->first
= startline
;
4224 if (!dont_prepend
) {
4226 label
= label
->next
;
4227 label
->next
= tt
= new_Token(NULL
, TOK_OTHER
, ":", 0);
4232 list
->uplevel(m
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
4237 /* The function that actually does the error reporting */
4238 static void verror(int severity
, const char *fmt
, va_list arg
)
4242 vsnprintf(buff
, sizeof(buff
), fmt
, arg
);
4244 if (istk
&& istk
->mstk
&& istk
->mstk
->name
)
4245 _error(severity
, "(%s:%d) %s", istk
->mstk
->name
,
4246 istk
->mstk
->lineno
, buff
);
4248 _error(severity
, "%s", buff
);
4252 * Since preprocessor always operate only on the line that didn't
4253 * arrived yet, we should always use ERR_OFFBY1.
4255 static void error(int severity
, const char *fmt
, ...)
4259 /* If we're in a dead branch of IF or something like it, ignore the error */
4260 if (istk
&& istk
->conds
&& !emitting(istk
->conds
->state
))
4264 verror(severity
, fmt
, arg
);
4269 * Because %else etc are evaluated in the state context
4270 * of the previous branch, errors might get lost with error():
4271 * %if 0 ... %else trailing garbage ... %endif
4272 * So %else etc should report errors with this function.
4274 static void error_precond(int severity
, const char *fmt
, ...)
4278 /* Only ignore the error if it's really in a dead branch */
4279 if (istk
&& istk
->conds
&& istk
->conds
->state
== COND_NEVER
)
4283 verror(severity
, fmt
, arg
);
4288 pp_reset(char *file
, int apass
, efunc errfunc
, evalfunc eval
,
4289 ListGen
* listgen
, StrList
**deplist
)
4295 istk
= nasm_malloc(sizeof(Include
));
4298 istk
->expansion
= NULL
;
4300 istk
->fp
= fopen(file
, "r");
4302 src_set_fname(nasm_strdup(file
));
4306 error(ERR_FATAL
|ERR_NOFILE
, "unable to open input file `%s'",
4309 nested_mac_count
= 0;
4310 nested_rep_count
= 0;
4313 if (tasm_compatible_mode
) {
4314 stdmacpos
= nasm_stdmac
;
4316 stdmacpos
= nasm_stdmac_after_tasm
;
4318 any_extrastdmac
= extrastdmac
&& *extrastdmac
;
4324 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4325 * The caller, however, will also pass in 3 for preprocess-only so
4326 * we can set __PASS__ accordingly.
4328 pass
= apass
> 2 ? 2 : apass
;
4330 dephead
= deptail
= deplist
;
4332 StrList
*sl
= nasm_malloc(strlen(file
)+1+sizeof sl
->next
);
4334 strcpy(sl
->str
, file
);
4336 deptail
= &sl
->next
;
4340 * Define the __PASS__ macro. This is defined here unlike
4341 * all the other builtins, because it is special -- it varies between
4344 t
= nasm_malloc(sizeof(*t
));
4346 make_tok_num(t
, apass
);
4348 define_smacro(NULL
, "__PASS__", true, 0, t
);
4351 static char *pp_getline(void)
4358 * Fetch a tokenized line, either from the macro-expansion
4359 * buffer or from the input file.
4362 while (istk
->expansion
&& istk
->expansion
->finishes
) {
4363 Line
*l
= istk
->expansion
;
4364 if (!l
->finishes
->name
&& l
->finishes
->in_progress
> 1) {
4368 * This is a macro-end marker for a macro with no
4369 * name, which means it's not really a macro at all
4370 * but a %rep block, and the `in_progress' field is
4371 * more than 1, meaning that we still need to
4372 * repeat. (1 means the natural last repetition; 0
4373 * means termination by %exitrep.) We have
4374 * therefore expanded up to the %endrep, and must
4375 * push the whole block on to the expansion buffer
4376 * again. We don't bother to remove the macro-end
4377 * marker: we'd only have to generate another one
4380 l
->finishes
->in_progress
--;
4381 for (l
= l
->finishes
->expansion
; l
; l
= l
->next
) {
4382 Token
*t
, *tt
, **tail
;
4384 ll
= nasm_malloc(sizeof(Line
));
4385 ll
->next
= istk
->expansion
;
4386 ll
->finishes
= NULL
;
4390 for (t
= l
->first
; t
; t
= t
->next
) {
4391 if (t
->text
|| t
->type
== TOK_WHITESPACE
) {
4393 new_Token(NULL
, t
->type
, t
->text
, 0);
4398 istk
->expansion
= ll
;
4402 * Check whether a `%rep' was started and not ended
4403 * within this macro expansion. This can happen and
4404 * should be detected. It's a fatal error because
4405 * I'm too confused to work out how to recover
4411 "defining with name in expansion");
4412 else if (istk
->mstk
->name
)
4414 "`%%rep' without `%%endrep' within"
4415 " expansion of macro `%s'",
4420 * FIXME: investigate the relationship at this point between
4421 * istk->mstk and l->finishes
4424 MMacro
*m
= istk
->mstk
;
4425 istk
->mstk
= m
->next_active
;
4428 * This was a real macro call, not a %rep, and
4429 * therefore the parameter information needs to
4432 nasm_free(m
->params
);
4433 free_tlist(m
->iline
);
4434 nasm_free(m
->paramlen
);
4435 l
->finishes
->in_progress
= false;
4439 istk
->expansion
= l
->next
;
4441 list
->downlevel(LIST_MACRO
);
4444 while (1) { /* until we get a line we can use */
4446 if (istk
->expansion
) { /* from a macro expansion */
4448 Line
*l
= istk
->expansion
;
4450 istk
->mstk
->lineno
++;
4452 istk
->expansion
= l
->next
;
4454 p
= detoken(tline
, false);
4455 list
->line(LIST_MACRO
, p
);
4460 if (line
) { /* from the current input file */
4461 line
= prepreproc(line
);
4462 tline
= tokenize(line
);
4467 * The current file has ended; work down the istk
4474 "expected `%%endif' before end of file");
4475 /* only set line and file name if there's a next node */
4477 src_set_linnum(i
->lineno
);
4478 nasm_free(src_set_fname(i
->fname
));
4481 list
->downlevel(LIST_INCLUDE
);
4485 if (istk
->expansion
&& istk
->expansion
->finishes
)
4491 * We must expand MMacro parameters and MMacro-local labels
4492 * _before_ we plunge into directive processing, to cope
4493 * with things like `%define something %1' such as STRUC
4494 * uses. Unless we're _defining_ a MMacro, in which case
4495 * those tokens should be left alone to go into the
4496 * definition; and unless we're in a non-emitting
4497 * condition, in which case we don't want to meddle with
4500 if (!defining
&& !(istk
->conds
&& !emitting(istk
->conds
->state
))
4501 && !(istk
->mstk
&& !istk
->mstk
->in_progress
)) {
4502 tline
= expand_mmac_params(tline
);
4506 * Check the line to see if it's a preprocessor directive.
4508 if (do_directive(tline
) == DIRECTIVE_FOUND
) {
4510 } else if (defining
) {
4512 * We're defining a multi-line macro. We emit nothing
4514 * shove the tokenized line on to the macro definition.
4516 Line
*l
= nasm_malloc(sizeof(Line
));
4517 l
->next
= defining
->expansion
;
4520 defining
->expansion
= l
;
4522 } else if (istk
->conds
&& !emitting(istk
->conds
->state
)) {
4524 * We're in a non-emitting branch of a condition block.
4525 * Emit nothing at all, not even a blank line: when we
4526 * emerge from the condition we'll give a line-number
4527 * directive so we keep our place correctly.
4531 } else if (istk
->mstk
&& !istk
->mstk
->in_progress
) {
4533 * We're in a %rep block which has been terminated, so
4534 * we're walking through to the %endrep without
4535 * emitting anything. Emit nothing at all, not even a
4536 * blank line: when we emerge from the %rep block we'll
4537 * give a line-number directive so we keep our place
4543 tline
= expand_smacro(tline
);
4544 if (!expand_mmacro(tline
)) {
4546 * De-tokenize the line again, and emit it.
4548 line
= detoken(tline
, true);
4552 continue; /* expand_mmacro calls free_tlist */
4560 static void pp_cleanup(int pass
)
4563 if(defining
->name
) {
4565 "end of file while still defining macro `%s'",
4568 error(ERR_NONFATAL
, "end of file while still in %%rep");
4571 free_mmacro(defining
);
4580 nasm_free(i
->fname
);
4585 nasm_free(src_set_fname(NULL
));
4590 while ((i
= ipath
)) {
4599 void pp_include_path(char *path
)
4603 i
= nasm_malloc(sizeof(IncPath
));
4604 i
->path
= path
? nasm_strdup(path
) : NULL
;
4607 if (ipath
!= NULL
) {
4609 while (j
->next
!= NULL
)
4617 void pp_pre_include(char *fname
)
4619 Token
*inc
, *space
, *name
;
4622 name
= new_Token(NULL
, TOK_INTERNAL_STRING
, fname
, 0);
4623 space
= new_Token(name
, TOK_WHITESPACE
, NULL
, 0);
4624 inc
= new_Token(space
, TOK_PREPROC_ID
, "%include", 0);
4626 l
= nasm_malloc(sizeof(Line
));
4633 void pp_pre_define(char *definition
)
4639 equals
= strchr(definition
, '=');
4640 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
4641 def
= new_Token(space
, TOK_PREPROC_ID
, "%define", 0);
4644 space
->next
= tokenize(definition
);
4648 l
= nasm_malloc(sizeof(Line
));
4655 void pp_pre_undefine(char *definition
)
4660 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
4661 def
= new_Token(space
, TOK_PREPROC_ID
, "%undef", 0);
4662 space
->next
= tokenize(definition
);
4664 l
= nasm_malloc(sizeof(Line
));
4672 * Added by Keith Kanios:
4674 * This function is used to assist with "runtime" preprocessor
4675 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4677 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4678 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4681 void pp_runtime(char *definition
)
4685 def
= tokenize(definition
);
4686 if(do_directive(def
) == NO_DIRECTIVE_FOUND
)
4691 void pp_extra_stdmac(macros_t
*macros
)
4693 extrastdmac
= macros
;
4696 static void make_tok_num(Token
* tok
, int64_t val
)
4699 snprintf(numbuf
, sizeof(numbuf
), "%"PRId64
"", val
);
4700 tok
->text
= nasm_strdup(numbuf
);
4701 tok
->type
= TOK_NUMBER
;