1 /* preproc.c macro preprocessor for the Netwide Assembler
3 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4 * Julian Hall. All rights reserved. The software is
5 * redistributable under the license given in the file "LICENSE"
6 * distributed in the NASM archive.
8 * initial version 18/iii/97 by Simon Tatham
11 /* Typical flow of text through preproc
13 * pp_getline gets tokenized lines, either
15 * from a macro expansion
19 * read_line gets raw text from stdmacpos, or predef, or current input file
20 * tokenize converts to tokens
23 * expand_mmac_params is used to expand %1 etc., unless a macro is being
24 * defined or a false conditional is being processed
25 * (%0, %1, %+1, %-1, %%foo
27 * do_directive checks for directives
29 * expand_smacro is used to expand single line macros
31 * expand_mmacro is used to expand multi-line macros
33 * detoken is used to convert the line back to text
56 typedef struct SMacro SMacro
;
57 typedef struct MMacro MMacro
;
58 typedef struct Context Context
;
59 typedef struct Token Token
;
60 typedef struct Blocks Blocks
;
61 typedef struct Line Line
;
62 typedef struct Include Include
;
63 typedef struct Cond Cond
;
64 typedef struct IncPath IncPath
;
67 * Note on the storage of both SMacro and MMacros: the hash table
68 * indexes them case-insensitively, and we then have to go through a
69 * linked list of potential case aliases (and, for MMacros, parameter
70 * ranges); this is to preserve the matching semantics of the earlier
71 * code. If the number of case aliases for a specific macro is a
72 * performance issue, you may want to reconsider your coding style.
76 * Store the definition of a single-line macro.
88 * Store the definition of a multi-line macro. This is also used to
89 * store the interiors of `%rep...%endrep' blocks, which are
90 * effectively self-re-invoking multi-line macros which simply
91 * don't have a name or bother to appear in the hash tables. %rep
92 * blocks are signified by having a NULL `name' field.
94 * In a MMacro describing a `%rep' block, the `in_progress' field
95 * isn't merely boolean, but gives the number of repeats left to
98 * The `next' field is used for storing MMacros in hash tables; the
99 * `next_active' field is for stacking them on istk entries.
101 * When a MMacro is being expanded, `params', `iline', `nparam',
102 * `paramlen', `rotate' and `unique' are local to the invocation.
107 int nparam_min
, nparam_max
;
109 bool plus
; /* is the last parameter greedy? */
110 bool nolist
; /* is this macro listing-inhibited? */
112 Token
*dlist
; /* All defaults as one list */
113 Token
**defaults
; /* Parameter default pointers */
114 int ndefs
; /* number of default parameters */
118 MMacro
*rep_nest
; /* used for nesting %rep */
119 Token
**params
; /* actual parameters */
120 Token
*iline
; /* invocation line */
121 unsigned int nparam
, rotate
;
124 int lineno
; /* Current line number on expansion */
128 * The context stack is composed of a linked list of these.
133 struct hash_table localmac
;
138 * This is the internal form which we break input lines up into.
139 * Typically stored in linked lists.
141 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
142 * necessarily used as-is, but is intended to denote the number of
143 * the substituted parameter. So in the definition
145 * %define a(x,y) ( (x) & ~(y) )
147 * the token representing `x' will have its type changed to
148 * TOK_SMAC_PARAM, but the one representing `y' will be
151 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
152 * which doesn't need quotes around it. Used in the pre-include
153 * mechanism as an alternative to trying to find a sensible type of
154 * quote to use on the filename we were passed.
157 TOK_NONE
= 0, TOK_WHITESPACE
, TOK_COMMENT
, TOK_ID
,
158 TOK_PREPROC_ID
, TOK_STRING
,
159 TOK_NUMBER
, TOK_FLOAT
, TOK_SMAC_END
, TOK_OTHER
,
161 TOK_PREPROC_Q
, TOK_PREPROC_QQ
,
162 TOK_SMAC_PARAM
, /* MUST BE LAST IN THE LIST!!! */
163 TOK_MAX
= INT_MAX
/* Keep compiler from reducing the range */
170 SMacro
*mac
; /* associated macro for TOK_SMAC_END */
171 size_t len
; /* scratch length field */
172 } a
; /* Auxiliary data */
173 enum pp_token_type type
;
177 * Multi-line macro definitions are stored as a linked list of
178 * these, which is essentially a container to allow several linked
181 * Note that in this module, linked lists are treated as stacks
182 * wherever possible. For this reason, Lines are _pushed_ on to the
183 * `expansion' field in MMacro structures, so that the linked list,
184 * if walked, would give the macro lines in reverse order; this
185 * means that we can walk the list when expanding a macro, and thus
186 * push the lines on to the `expansion' field in _istk_ in reverse
187 * order (so that when popped back off they are in the right
188 * order). It may seem cockeyed, and it relies on my design having
189 * an even number of steps in, but it works...
191 * Some of these structures, rather than being actual lines, are
192 * markers delimiting the end of the expansion of a given macro.
193 * This is for use in the cycle-tracking and %rep-handling code.
194 * Such structures have `finishes' non-NULL, and `first' NULL. All
195 * others have `finishes' NULL, but `first' may still be NULL if
205 * To handle an arbitrary level of file inclusion, we maintain a
206 * stack (ie linked list) of these things.
215 MMacro
*mstk
; /* stack of active macros/reps */
219 * Include search path. This is simply a list of strings which get
220 * prepended, in turn, to the name of an include file, in an
221 * attempt to find the file if it's not in the current directory.
229 * Conditional assembly: we maintain a separate stack of these for
230 * each level of file inclusion. (The only reason we keep the
231 * stacks separate is to ensure that a stray `%endif' in a file
232 * included from within the true branch of a `%if' won't terminate
233 * it and cause confusion: instead, rightly, it'll cause an error.)
241 * These states are for use just after %if or %elif: IF_TRUE
242 * means the condition has evaluated to truth so we are
243 * currently emitting, whereas IF_FALSE means we are not
244 * currently emitting but will start doing so if a %else comes
245 * up. In these states, all directives are admissible: %elif,
246 * %else and %endif. (And of course %if.)
248 COND_IF_TRUE
, COND_IF_FALSE
,
250 * These states come up after a %else: ELSE_TRUE means we're
251 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
252 * any %elif or %else will cause an error.
254 COND_ELSE_TRUE
, COND_ELSE_FALSE
,
256 * This state means that we're not emitting now, and also that
257 * nothing until %endif will be emitted at all. It's for use in
258 * two circumstances: (i) when we've had our moment of emission
259 * and have now started seeing %elifs, and (ii) when the
260 * condition construct in question is contained within a
261 * non-emitting branch of a larger condition construct.
265 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
268 * These defines are used as the possible return values for do_directive
270 #define NO_DIRECTIVE_FOUND 0
271 #define DIRECTIVE_FOUND 1
274 * Condition codes. Note that we use c_ prefix not C_ because C_ is
275 * used in nasm.h for the "real" condition codes. At _this_ level,
276 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
277 * ones, so we need a different enum...
279 static const char * const conditions
[] = {
280 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
281 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
282 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
285 c_A
, c_AE
, c_B
, c_BE
, c_C
, c_CXZ
, c_E
, c_ECXZ
, c_G
, c_GE
, c_L
, c_LE
,
286 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, c_NE
, c_NG
, c_NGE
, c_NL
, c_NLE
, c_NO
,
287 c_NP
, c_NS
, c_NZ
, c_O
, c_P
, c_PE
, c_PO
, c_RCXZ
, c_S
, c_Z
,
290 static const enum pp_conds inverse_ccs
[] = {
291 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, -1, c_NE
, -1, c_NG
, c_NGE
, c_NL
, c_NLE
,
292 c_A
, c_AE
, c_B
, c_BE
, c_C
, c_E
, c_G
, c_GE
, c_L
, c_LE
, c_O
, c_P
, c_S
,
293 c_Z
, c_NO
, c_NP
, c_PO
, c_PE
, -1, c_NS
, c_NZ
299 /* If this is a an IF, ELIF, ELSE or ENDIF keyword */
300 static int is_condition(enum preproc_token arg
)
302 return PP_IS_COND(arg
) || (arg
== PP_ELSE
) || (arg
== PP_ENDIF
);
305 /* For TASM compatibility we need to be able to recognise TASM compatible
306 * conditional compilation directives. Using the NASM pre-processor does
307 * not work, so we look for them specifically from the following list and
308 * then jam in the equivalent NASM directive into the input stream.
312 TM_ARG
, TM_ELIF
, TM_ELSE
, TM_ENDIF
, TM_IF
, TM_IFDEF
, TM_IFDIFI
,
313 TM_IFNDEF
, TM_INCLUDE
, TM_LOCAL
316 static const char * const tasm_directives
[] = {
317 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
318 "ifndef", "include", "local"
321 static int StackSize
= 4;
322 static char *StackPointer
= "ebp";
323 static int ArgOffset
= 8;
324 static int LocalOffset
= 0;
326 static Context
*cstk
;
327 static Include
*istk
;
328 static IncPath
*ipath
= NULL
;
330 static efunc _error
; /* Pointer to client-provided error reporting function */
331 static evalfunc evaluate
;
333 static int pass
; /* HACK: pass 0 = generate dependencies only */
334 static StrList
**dephead
, **deptail
; /* Dependency list */
336 static uint64_t unique
; /* unique identifier numbers */
338 static Line
*predef
= NULL
;
339 static bool do_predef
;
341 static ListGen
*list
;
344 * The current set of multi-line macros we have defined.
346 static struct hash_table mmacros
;
349 * The current set of single-line macros we have defined.
351 static struct hash_table smacros
;
354 * The multi-line macro we are currently defining, or the %rep
355 * block we are currently reading, if any.
357 static MMacro
*defining
;
360 * The number of macro parameters to allocate space for at a time.
362 #define PARAM_DELTA 16
365 * The standard macro set: defined in macros.c in the array nasm_stdmac.
366 * This gives our position in the macro set, when we're processing it.
368 static const macros_t
*stdmacpos
;
371 * The extra standard macros that come from the object format, if
374 static const macros_t
*extrastdmac
= NULL
;
375 static bool any_extrastdmac
;
378 * Tokens are allocated in blocks to improve speed
380 #define TOKEN_BLOCKSIZE 4096
381 static Token
*freeTokens
= NULL
;
387 static Blocks blocks
= { NULL
, NULL
};
390 * Forward declarations.
392 static Token
*expand_mmac_params(Token
* tline
);
393 static Token
*expand_smacro(Token
* tline
);
394 static Token
*expand_id(Token
* tline
);
395 static Context
*get_ctx(const char *name
, bool all_contexts
);
396 static void make_tok_num(Token
* tok
, int64_t val
);
397 static void error(int severity
, const char *fmt
, ...);
398 static void *new_Block(size_t size
);
399 static void delete_Blocks(void);
400 static Token
*new_Token(Token
* next
, enum pp_token_type type
,
401 const char *text
, int txtlen
);
402 static Token
*delete_Token(Token
* t
);
405 * Macros for safe checking of token pointers, avoid *(NULL)
407 #define tok_type_(x,t) ((x) && (x)->type == (t))
408 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
409 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
410 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
412 /* Handle TASM specific directives, which do not contain a % in
413 * front of them. We do it here because I could not find any other
414 * place to do it for the moment, and it is a hack (ideally it would
415 * be nice to be able to use the NASM pre-processor to do it).
417 static char *check_tasm_directive(char *line
)
419 int32_t i
, j
, k
, m
, len
;
420 char *p
= line
, *oldline
, oldchar
;
422 /* Skip whitespace */
423 while (nasm_isspace(*p
) && *p
!= 0)
426 /* Binary search for the directive name */
428 j
= elements(tasm_directives
);
430 while (!nasm_isspace(p
[len
]) && p
[len
] != 0)
437 m
= nasm_stricmp(p
, tasm_directives
[k
]);
439 /* We have found a directive, so jam a % in front of it
440 * so that NASM will then recognise it as one if it's own.
445 line
= nasm_malloc(len
+ 2);
447 if (k
== TM_IFDIFI
) {
448 /* NASM does not recognise IFDIFI, so we convert it to
449 * %ifdef BOGUS. This is not used in NASM comaptible
450 * code, but does need to parse for the TASM macro
453 strcpy(line
+ 1, "ifdef BOGUS");
455 memcpy(line
+ 1, p
, len
+ 1);
470 * The pre-preprocessing stage... This function translates line
471 * number indications as they emerge from GNU cpp (`# lineno "file"
472 * flags') into NASM preprocessor line number indications (`%line
475 static char *prepreproc(char *line
)
478 char *fname
, *oldline
;
480 if (line
[0] == '#' && line
[1] == ' ') {
483 lineno
= atoi(fname
);
484 fname
+= strspn(fname
, "0123456789 ");
487 fnlen
= strcspn(fname
, "\"");
488 line
= nasm_malloc(20 + fnlen
);
489 snprintf(line
, 20 + fnlen
, "%%line %d %.*s", lineno
, fnlen
, fname
);
492 if (tasm_compatible_mode
)
493 return check_tasm_directive(line
);
498 * Free a linked list of tokens.
500 static void free_tlist(Token
* list
)
503 list
= delete_Token(list
);
508 * Free a linked list of lines.
510 static void free_llist(Line
* list
)
516 free_tlist(l
->first
);
524 static void free_mmacro(MMacro
* m
)
527 free_tlist(m
->dlist
);
528 nasm_free(m
->defaults
);
529 free_llist(m
->expansion
);
534 * Free all currently defined macros, and free the hash tables
536 static void free_smacro_table(struct hash_table
*smt
)
540 struct hash_tbl_node
*it
= NULL
;
542 while ((s
= hash_iterate(smt
, &it
, &key
)) != NULL
) {
543 nasm_free((void *)key
);
545 SMacro
*ns
= s
->next
;
547 free_tlist(s
->expansion
);
555 static void free_mmacro_table(struct hash_table
*mmt
)
559 struct hash_tbl_node
*it
= NULL
;
562 while ((m
= hash_iterate(mmt
, &it
, &key
)) != NULL
) {
563 nasm_free((void *)key
);
565 MMacro
*nm
= m
->next
;
573 static void free_macros(void)
575 free_smacro_table(&smacros
);
576 free_mmacro_table(&mmacros
);
580 * Initialize the hash tables
582 static void init_macros(void)
584 hash_init(&smacros
, HASH_LARGE
);
585 hash_init(&mmacros
, HASH_LARGE
);
589 * Pop the context stack.
591 static void ctx_pop(void)
596 free_smacro_table(&c
->localmac
);
602 * Search for a key in the hash index; adding it if necessary
603 * (in which case we initialize the data pointer to NULL.)
606 hash_findi_add(struct hash_table
*hash
, const char *str
)
608 struct hash_insert hi
;
612 r
= hash_findi(hash
, str
, &hi
);
616 strx
= nasm_strdup(str
); /* Use a more efficient allocator here? */
617 return hash_add(&hi
, strx
, NULL
);
621 * Like hash_findi, but returns the data element rather than a pointer
622 * to it. Used only when not adding a new element, hence no third
626 hash_findix(struct hash_table
*hash
, const char *str
)
630 p
= hash_findi(hash
, str
, NULL
);
631 return p
? *p
: NULL
;
634 #define BUF_DELTA 512
636 * Read a line from the top file in istk, handling multiple CR/LFs
637 * at the end of the line read, and handling spurious ^Zs. Will
638 * return lines from the standard macro set if this has not already
641 static char *read_line(void)
643 char *buffer
, *p
, *q
;
644 int bufsize
, continued_count
;
648 const unsigned char *p
= stdmacpos
;
653 len
+= pp_directives_len
[c
-0x80]+1;
657 ret
= nasm_malloc(len
+1);
659 while ((c
= *stdmacpos
++)) {
661 memcpy(q
, pp_directives
[c
-0x80], pp_directives_len
[c
-0x80]);
662 q
+= pp_directives_len
[c
-0x80];
672 /* This was the last of the standard macro chain... */
674 if (any_extrastdmac
) {
675 stdmacpos
= extrastdmac
;
676 any_extrastdmac
= false;
677 } else if (do_predef
) {
679 Token
*head
, **tail
, *t
;
682 * Nasty hack: here we push the contents of
683 * `predef' on to the top-level expansion stack,
684 * since this is the most convenient way to
685 * implement the pre-include and pre-define
688 for (pd
= predef
; pd
; pd
= pd
->next
) {
691 for (t
= pd
->first
; t
; t
= t
->next
) {
692 *tail
= new_Token(NULL
, t
->type
, t
->text
, 0);
693 tail
= &(*tail
)->next
;
695 l
= nasm_malloc(sizeof(Line
));
696 l
->next
= istk
->expansion
;
708 buffer
= nasm_malloc(BUF_DELTA
);
712 q
= fgets(p
, bufsize
- (p
- buffer
), istk
->fp
);
716 if (p
> buffer
&& p
[-1] == '\n') {
717 /* Convert backslash-CRLF line continuation sequences into
718 nothing at all (for DOS and Windows) */
719 if (((p
- 2) > buffer
) && (p
[-3] == '\\') && (p
[-2] == '\r')) {
724 /* Also convert backslash-LF line continuation sequences into
725 nothing at all (for Unix) */
726 else if (((p
- 1) > buffer
) && (p
[-2] == '\\')) {
734 if (p
- buffer
> bufsize
- 10) {
735 int32_t offset
= p
- buffer
;
736 bufsize
+= BUF_DELTA
;
737 buffer
= nasm_realloc(buffer
, bufsize
);
738 p
= buffer
+ offset
; /* prevent stale-pointer problems */
742 if (!q
&& p
== buffer
) {
747 src_set_linnum(src_get_linnum() + istk
->lineinc
+
748 (continued_count
* istk
->lineinc
));
751 * Play safe: remove CRs as well as LFs, if any of either are
752 * present at the end of the line.
754 while (--p
>= buffer
&& (*p
== '\n' || *p
== '\r'))
758 * Handle spurious ^Z, which may be inserted into source files
759 * by some file transfer utilities.
761 buffer
[strcspn(buffer
, "\032")] = '\0';
763 list
->line(LIST_READ
, buffer
);
769 * Tokenize a line of text. This is a very simple process since we
770 * don't need to parse the value out of e.g. numeric tokens: we
771 * simply split one string into many.
773 static Token
*tokenize(char *line
)
776 enum pp_token_type type
;
778 Token
*t
, **tail
= &list
;
784 if (nasm_isdigit(*p
) ||
785 ((*p
== '-' || *p
== '+') && nasm_isdigit(p
[1])) ||
786 ((*p
== '+') && (nasm_isspace(p
[1]) || !p
[1]))) {
790 while (nasm_isdigit(*p
));
791 type
= TOK_PREPROC_ID
;
792 } else if (*p
== '{') {
794 while (*p
&& *p
!= '}') {
801 type
= TOK_PREPROC_ID
;
802 } else if (*p
== '?') {
803 type
= TOK_PREPROC_Q
; /* %? */
806 type
= TOK_PREPROC_QQ
; /* %?? */
809 } else if (isidchar(*p
) ||
810 ((*p
== '!' || *p
== '%' || *p
== '$') &&
815 while (isidchar(*p
));
816 type
= TOK_PREPROC_ID
;
822 } else if (isidstart(*p
) || (*p
== '$' && isidstart(p
[1]))) {
825 while (*p
&& isidchar(*p
))
827 } else if (*p
== '\'' || *p
== '"' || *p
== '`') {
832 p
= nasm_skip_string(p
);
837 error(ERR_WARNING
, "unterminated string");
838 /* Handling unterminated strings by UNV */
841 } else if (isnumstart(*p
)) {
843 bool is_float
= false;
859 if (!is_hex
&& (c
== 'e' || c
== 'E')) {
861 if (*p
== '+' || *p
== '-') {
862 /* e can only be followed by +/- if it is either a
863 prefixed hex number or a floating-point number */
867 } else if (c
== 'H' || c
== 'h' || c
== 'X' || c
== 'x') {
869 } else if (c
== 'P' || c
== 'p') {
871 if (*p
== '+' || *p
== '-')
873 } else if (isnumchar(c
) || c
== '_')
876 /* we need to deal with consequences of the legacy
877 parser, like "1.nolist" being two tokens
878 (TOK_NUMBER, TOK_ID) here; at least give it
879 a shot for now. In the future, we probably need
880 a flex-based scanner with proper pattern matching
881 to do it as well as it can be done. Nothing in
882 the world is going to help the person who wants
883 0x123.p16 interpreted as two tokens, though. */
888 if (nasm_isdigit(*r
) || (is_hex
&& nasm_isxdigit(*r
)) ||
889 (!is_hex
&& (*r
== 'e' || *r
== 'E')) ||
890 (*r
== 'p' || *r
== 'P')) {
894 break; /* Terminate the token */
898 p
--; /* Point to first character beyond number */
900 if (has_e
&& !is_hex
) {
901 /* 1e13 is floating-point, but 1e13h is not */
905 type
= is_float
? TOK_FLOAT
: TOK_NUMBER
;
906 } else if (nasm_isspace(*p
)) {
907 type
= TOK_WHITESPACE
;
909 while (*p
&& nasm_isspace(*p
))
912 * Whitespace just before end-of-line is discarded by
913 * pretending it's a comment; whitespace just before a
914 * comment gets lumped into the comment.
916 if (!*p
|| *p
== ';') {
921 } else if (*p
== ';') {
927 * Anything else is an operator of some kind. We check
928 * for all the double-character operators (>>, <<, //,
929 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
930 * else is a single-character operator.
933 if ((p
[0] == '>' && p
[1] == '>') ||
934 (p
[0] == '<' && p
[1] == '<') ||
935 (p
[0] == '/' && p
[1] == '/') ||
936 (p
[0] == '<' && p
[1] == '=') ||
937 (p
[0] == '>' && p
[1] == '=') ||
938 (p
[0] == '=' && p
[1] == '=') ||
939 (p
[0] == '!' && p
[1] == '=') ||
940 (p
[0] == '<' && p
[1] == '>') ||
941 (p
[0] == '&' && p
[1] == '&') ||
942 (p
[0] == '|' && p
[1] == '|') ||
943 (p
[0] == '^' && p
[1] == '^')) {
949 /* Handling unterminated string by UNV */
952 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
953 t->text[p-line] = *line;
957 if (type
!= TOK_COMMENT
) {
958 *tail
= t
= new_Token(NULL
, type
, line
, p
- line
);
967 * this function allocates a new managed block of memory and
968 * returns a pointer to the block. The managed blocks are
969 * deleted only all at once by the delete_Blocks function.
971 static void *new_Block(size_t size
)
975 /* first, get to the end of the linked list */
978 /* now allocate the requested chunk */
979 b
->chunk
= nasm_malloc(size
);
981 /* now allocate a new block for the next request */
982 b
->next
= nasm_malloc(sizeof(Blocks
));
983 /* and initialize the contents of the new block */
984 b
->next
->next
= NULL
;
985 b
->next
->chunk
= NULL
;
990 * this function deletes all managed blocks of memory
992 static void delete_Blocks(void)
994 Blocks
*a
, *b
= &blocks
;
997 * keep in mind that the first block, pointed to by blocks
998 * is a static and not dynamically allocated, so we don't
1003 nasm_free(b
->chunk
);
1012 * this function creates a new Token and passes a pointer to it
1013 * back to the caller. It sets the type and text elements, and
1014 * also the a.mac and next elements to NULL.
1016 static Token
*new_Token(Token
* next
, enum pp_token_type type
,
1017 const char *text
, int txtlen
)
1022 if (freeTokens
== NULL
) {
1023 freeTokens
= (Token
*) new_Block(TOKEN_BLOCKSIZE
* sizeof(Token
));
1024 for (i
= 0; i
< TOKEN_BLOCKSIZE
- 1; i
++)
1025 freeTokens
[i
].next
= &freeTokens
[i
+ 1];
1026 freeTokens
[i
].next
= NULL
;
1029 freeTokens
= t
->next
;
1033 if (type
== TOK_WHITESPACE
|| text
== NULL
) {
1037 txtlen
= strlen(text
);
1038 t
->text
= nasm_malloc(txtlen
+1);
1039 memcpy(t
->text
, text
, txtlen
);
1040 t
->text
[txtlen
] = '\0';
1045 static Token
*delete_Token(Token
* t
)
1047 Token
*next
= t
->next
;
1049 t
->next
= freeTokens
;
1055 * Convert a line of tokens back into text.
1056 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1057 * will be transformed into ..@ctxnum.xxx
1059 static char *detoken(Token
* tlist
, bool expand_locals
)
1067 for (t
= tlist
; t
; t
= t
->next
) {
1068 if (t
->type
== TOK_PREPROC_ID
&& t
->text
[1] == '!') {
1069 char *p
= getenv(t
->text
+ 2);
1072 t
->text
= nasm_strdup(p
);
1076 /* Expand local macros here and not during preprocessing */
1077 if (expand_locals
&&
1078 t
->type
== TOK_PREPROC_ID
&& t
->text
&&
1079 t
->text
[0] == '%' && t
->text
[1] == '$') {
1080 Context
*ctx
= get_ctx(t
->text
, false);
1083 char *p
, *q
= t
->text
+ 2;
1085 q
+= strspn(q
, "$");
1086 snprintf(buffer
, sizeof(buffer
), "..@%"PRIu32
".", ctx
->number
);
1087 p
= nasm_strcat(buffer
, q
);
1092 if (t
->type
== TOK_WHITESPACE
) {
1094 } else if (t
->text
) {
1095 len
+= strlen(t
->text
);
1098 p
= line
= nasm_malloc(len
+ 1);
1099 for (t
= tlist
; t
; t
= t
->next
) {
1100 if (t
->type
== TOK_WHITESPACE
) {
1102 } else if (t
->text
) {
1113 * A scanner, suitable for use by the expression evaluator, which
1114 * operates on a line of Tokens. Expects a pointer to a pointer to
1115 * the first token in the line to be passed in as its private_data
1118 * FIX: This really needs to be unified with stdscan.
1120 static int ppscan(void *private_data
, struct tokenval
*tokval
)
1122 Token
**tlineptr
= private_data
;
1124 char ourcopy
[MAX_KEYWORD
+1], *p
, *r
, *s
;
1128 *tlineptr
= tline
? tline
->next
: NULL
;
1130 while (tline
&& (tline
->type
== TOK_WHITESPACE
||
1131 tline
->type
== TOK_COMMENT
));
1134 return tokval
->t_type
= TOKEN_EOS
;
1136 tokval
->t_charptr
= tline
->text
;
1138 if (tline
->text
[0] == '$' && !tline
->text
[1])
1139 return tokval
->t_type
= TOKEN_HERE
;
1140 if (tline
->text
[0] == '$' && tline
->text
[1] == '$' && !tline
->text
[2])
1141 return tokval
->t_type
= TOKEN_BASE
;
1143 if (tline
->type
== TOK_ID
) {
1144 p
= tokval
->t_charptr
= tline
->text
;
1146 tokval
->t_charptr
++;
1147 return tokval
->t_type
= TOKEN_ID
;
1150 for (r
= p
, s
= ourcopy
; *r
; r
++) {
1151 if (r
>= p
+MAX_KEYWORD
)
1152 return tokval
->t_type
= TOKEN_ID
; /* Not a keyword */
1153 *s
++ = nasm_tolower(*r
);
1156 /* right, so we have an identifier sitting in temp storage. now,
1157 * is it actually a register or instruction name, or what? */
1158 return nasm_token_hash(ourcopy
, tokval
);
1161 if (tline
->type
== TOK_NUMBER
) {
1163 tokval
->t_integer
= readnum(tline
->text
, &rn_error
);
1164 tokval
->t_charptr
= tline
->text
;
1166 return tokval
->t_type
= TOKEN_ERRNUM
;
1168 return tokval
->t_type
= TOKEN_NUM
;
1171 if (tline
->type
== TOK_FLOAT
) {
1172 return tokval
->t_type
= TOKEN_FLOAT
;
1175 if (tline
->type
== TOK_STRING
) {
1178 bq
= tline
->text
[0];
1179 tokval
->t_charptr
= tline
->text
;
1180 tokval
->t_inttwo
= nasm_unquote(tline
->text
, &ep
);
1182 if (ep
[0] != bq
|| ep
[1] != '\0')
1183 return tokval
->t_type
= TOKEN_ERRSTR
;
1185 return tokval
->t_type
= TOKEN_STR
;
1188 if (tline
->type
== TOK_OTHER
) {
1189 if (!strcmp(tline
->text
, "<<"))
1190 return tokval
->t_type
= TOKEN_SHL
;
1191 if (!strcmp(tline
->text
, ">>"))
1192 return tokval
->t_type
= TOKEN_SHR
;
1193 if (!strcmp(tline
->text
, "//"))
1194 return tokval
->t_type
= TOKEN_SDIV
;
1195 if (!strcmp(tline
->text
, "%%"))
1196 return tokval
->t_type
= TOKEN_SMOD
;
1197 if (!strcmp(tline
->text
, "=="))
1198 return tokval
->t_type
= TOKEN_EQ
;
1199 if (!strcmp(tline
->text
, "<>"))
1200 return tokval
->t_type
= TOKEN_NE
;
1201 if (!strcmp(tline
->text
, "!="))
1202 return tokval
->t_type
= TOKEN_NE
;
1203 if (!strcmp(tline
->text
, "<="))
1204 return tokval
->t_type
= TOKEN_LE
;
1205 if (!strcmp(tline
->text
, ">="))
1206 return tokval
->t_type
= TOKEN_GE
;
1207 if (!strcmp(tline
->text
, "&&"))
1208 return tokval
->t_type
= TOKEN_DBL_AND
;
1209 if (!strcmp(tline
->text
, "^^"))
1210 return tokval
->t_type
= TOKEN_DBL_XOR
;
1211 if (!strcmp(tline
->text
, "||"))
1212 return tokval
->t_type
= TOKEN_DBL_OR
;
1216 * We have no other options: just return the first character of
1219 return tokval
->t_type
= tline
->text
[0];
1223 * Compare a string to the name of an existing macro; this is a
1224 * simple wrapper which calls either strcmp or nasm_stricmp
1225 * depending on the value of the `casesense' parameter.
1227 static int mstrcmp(const char *p
, const char *q
, bool casesense
)
1229 return casesense
? strcmp(p
, q
) : nasm_stricmp(p
, q
);
1233 * Compare a string to the name of an existing macro; this is a
1234 * simple wrapper which calls either strcmp or nasm_stricmp
1235 * depending on the value of the `casesense' parameter.
1237 static int mmemcmp(const char *p
, const char *q
, size_t l
, bool casesense
)
1239 return casesense
? memcmp(p
, q
, l
) : nasm_memicmp(p
, q
, l
);
1243 * Return the Context structure associated with a %$ token. Return
1244 * NULL, having _already_ reported an error condition, if the
1245 * context stack isn't deep enough for the supplied number of $
1247 * If all_contexts == true, contexts that enclose current are
1248 * also scanned for such smacro, until it is found; if not -
1249 * only the context that directly results from the number of $'s
1250 * in variable's name.
1252 static Context
*get_ctx(const char *name
, bool all_contexts
)
1258 if (!name
|| name
[0] != '%' || name
[1] != '$')
1262 error(ERR_NONFATAL
, "`%s': context stack is empty", name
);
1266 for (i
= strspn(name
+ 2, "$"), ctx
= cstk
; (i
> 0) && ctx
; i
--) {
1268 /* i--; Lino - 02/25/02 */
1271 error(ERR_NONFATAL
, "`%s': context stack is only"
1272 " %d level%s deep", name
, i
- 1, (i
== 2 ? "" : "s"));
1279 /* Search for this smacro in found context */
1280 m
= hash_findix(&ctx
->localmac
, name
);
1282 if (!mstrcmp(m
->name
, name
, m
->casesense
))
1293 * Check to see if a file is already in a string list
1295 static bool in_list(const StrList
*list
, const char *str
)
1298 if (!strcmp(list
->str
, str
))
1306 * Open an include file. This routine must always return a valid
1307 * file pointer if it returns - it's responsible for throwing an
1308 * ERR_FATAL and bombing out completely if not. It should also try
1309 * the include path one by one until it finds the file or reaches
1310 * the end of the path.
1312 static FILE *inc_fopen(const char *file
, StrList
**dhead
, StrList
***dtail
,
1317 IncPath
*ip
= ipath
;
1318 int len
= strlen(file
);
1319 size_t prefix_len
= 0;
1323 sl
= nasm_malloc(prefix_len
+len
+1+sizeof sl
->next
);
1324 memcpy(sl
->str
, prefix
, prefix_len
);
1325 memcpy(sl
->str
+prefix_len
, file
, len
+1);
1326 fp
= fopen(sl
->str
, "r");
1327 if (fp
&& dhead
&& !in_list(*dhead
, sl
->str
)) {
1345 prefix_len
= strlen(prefix
);
1347 /* -MG given and file not found */
1348 if (dhead
&& !in_list(*dhead
, file
)) {
1349 sl
= nasm_malloc(len
+1+sizeof sl
->next
);
1351 strcpy(sl
->str
, file
);
1359 error(ERR_FATAL
, "unable to open include file `%s'", file
);
1360 return NULL
; /* never reached - placate compilers */
1364 * Determine if we should warn on defining a single-line macro of
1365 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1366 * return true if _any_ single-line macro of that name is defined.
1367 * Otherwise, will return true if a single-line macro with either
1368 * `nparam' or no parameters is defined.
1370 * If a macro with precisely the right number of parameters is
1371 * defined, or nparam is -1, the address of the definition structure
1372 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1373 * is NULL, no action will be taken regarding its contents, and no
1376 * Note that this is also called with nparam zero to resolve
1379 * If you already know which context macro belongs to, you can pass
1380 * the context pointer as first parameter; if you won't but name begins
1381 * with %$ the context will be automatically computed. If all_contexts
1382 * is true, macro will be searched in outer contexts as well.
1385 smacro_defined(Context
* ctx
, const char *name
, int nparam
, SMacro
** defn
,
1388 struct hash_table
*smtbl
;
1392 smtbl
= &ctx
->localmac
;
1393 } else if (name
[0] == '%' && name
[1] == '$') {
1395 ctx
= get_ctx(name
, false);
1397 return false; /* got to return _something_ */
1398 smtbl
= &ctx
->localmac
;
1402 m
= (SMacro
*) hash_findix(smtbl
, name
);
1405 if (!mstrcmp(m
->name
, name
, m
->casesense
&& nocase
) &&
1406 (nparam
<= 0 || m
->nparam
== 0 || nparam
== (int) m
->nparam
)) {
1408 if (nparam
== (int) m
->nparam
|| nparam
== -1)
1422 * Count and mark off the parameters in a multi-line macro call.
1423 * This is called both from within the multi-line macro expansion
1424 * code, and also to mark off the default parameters when provided
1425 * in a %macro definition line.
1427 static void count_mmac_params(Token
* t
, int *nparam
, Token
*** params
)
1429 int paramsize
, brace
;
1431 *nparam
= paramsize
= 0;
1434 if (*nparam
>= paramsize
) {
1435 paramsize
+= PARAM_DELTA
;
1436 *params
= nasm_realloc(*params
, sizeof(**params
) * paramsize
);
1440 if (tok_is_(t
, "{"))
1442 (*params
)[(*nparam
)++] = t
;
1443 while (tok_isnt_(t
, brace
? "}" : ","))
1445 if (t
) { /* got a comma/brace */
1449 * Now we've found the closing brace, look further
1453 if (tok_isnt_(t
, ",")) {
1455 "braces do not enclose all of macro parameter");
1456 while (tok_isnt_(t
, ","))
1460 t
= t
->next
; /* eat the comma */
1467 * Determine whether one of the various `if' conditions is true or
1470 * We must free the tline we get passed.
1472 static bool if_condition(Token
* tline
, enum preproc_token ct
)
1474 enum pp_conditional i
= PP_COND(ct
);
1476 Token
*t
, *tt
, **tptr
, *origline
;
1477 struct tokenval tokval
;
1479 enum pp_token_type needtype
;
1485 j
= false; /* have we matched yet? */
1486 while (cstk
&& tline
) {
1488 if (!tline
|| tline
->type
!= TOK_ID
) {
1490 "`%s' expects context identifiers", pp_directives
[ct
]);
1491 free_tlist(origline
);
1494 if (cstk
->name
&& !nasm_stricmp(tline
->text
, cstk
->name
))
1496 tline
= tline
->next
;
1501 j
= false; /* have we matched yet? */
1504 if (!tline
|| (tline
->type
!= TOK_ID
&&
1505 (tline
->type
!= TOK_PREPROC_ID
||
1506 tline
->text
[1] != '$'))) {
1508 "`%s' expects macro identifiers", pp_directives
[ct
]);
1511 if (smacro_defined(NULL
, tline
->text
, 0, NULL
, true))
1513 tline
= tline
->next
;
1519 tline
= expand_smacro(tline
);
1521 while (tok_isnt_(tt
, ","))
1525 "`%s' expects two comma-separated arguments",
1530 j
= true; /* assume equality unless proved not */
1531 while ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) && tt
) {
1532 if (tt
->type
== TOK_OTHER
&& !strcmp(tt
->text
, ",")) {
1533 error(ERR_NONFATAL
, "`%s': more than one comma on line",
1537 if (t
->type
== TOK_WHITESPACE
) {
1541 if (tt
->type
== TOK_WHITESPACE
) {
1545 if (tt
->type
!= t
->type
) {
1546 j
= false; /* found mismatching tokens */
1549 /* When comparing strings, need to unquote them first */
1550 if (t
->type
== TOK_STRING
) {
1551 size_t l1
= nasm_unquote(t
->text
, NULL
);
1552 size_t l2
= nasm_unquote(tt
->text
, NULL
);
1558 if (mmemcmp(t
->text
, tt
->text
, l1
, i
== PPC_IFIDN
)) {
1562 } else if (mstrcmp(tt
->text
, t
->text
, i
== PPC_IFIDN
) != 0) {
1563 j
= false; /* found mismatching tokens */
1570 if ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) || tt
)
1571 j
= false; /* trailing gunk on one end or other */
1577 MMacro searching
, *mmac
;
1579 tline
= tline
->next
;
1581 tline
= expand_id(tline
);
1582 if (!tok_type_(tline
, TOK_ID
)) {
1584 "`%s' expects a macro name", pp_directives
[ct
]);
1587 searching
.name
= nasm_strdup(tline
->text
);
1588 searching
.casesense
= true;
1589 searching
.plus
= false;
1590 searching
.nolist
= false;
1591 searching
.in_progress
= 0;
1592 searching
.rep_nest
= NULL
;
1593 searching
.nparam_min
= 0;
1594 searching
.nparam_max
= INT_MAX
;
1595 tline
= expand_smacro(tline
->next
);
1598 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
1600 "`%s' expects a parameter count or nothing",
1603 searching
.nparam_min
= searching
.nparam_max
=
1604 readnum(tline
->text
, &j
);
1607 "unable to parse parameter count `%s'",
1610 if (tline
&& tok_is_(tline
->next
, "-")) {
1611 tline
= tline
->next
->next
;
1612 if (tok_is_(tline
, "*"))
1613 searching
.nparam_max
= INT_MAX
;
1614 else if (!tok_type_(tline
, TOK_NUMBER
))
1616 "`%s' expects a parameter count after `-'",
1619 searching
.nparam_max
= readnum(tline
->text
, &j
);
1622 "unable to parse parameter count `%s'",
1624 if (searching
.nparam_min
> searching
.nparam_max
)
1626 "minimum parameter count exceeds maximum");
1629 if (tline
&& tok_is_(tline
->next
, "+")) {
1630 tline
= tline
->next
;
1631 searching
.plus
= true;
1633 mmac
= (MMacro
*) hash_findix(&mmacros
, searching
.name
);
1635 if (!strcmp(mmac
->name
, searching
.name
) &&
1636 (mmac
->nparam_min
<= searching
.nparam_max
1638 && (searching
.nparam_min
<= mmac
->nparam_max
1645 nasm_free(searching
.name
);
1654 needtype
= TOK_NUMBER
;
1657 needtype
= TOK_STRING
;
1661 t
= tline
= expand_smacro(tline
);
1663 while (tok_type_(t
, TOK_WHITESPACE
) ||
1664 (needtype
== TOK_NUMBER
&&
1665 tok_type_(t
, TOK_OTHER
) &&
1666 (t
->text
[0] == '-' || t
->text
[0] == '+') &&
1670 j
= tok_type_(t
, needtype
);
1674 t
= tline
= expand_smacro(tline
);
1675 while (tok_type_(t
, TOK_WHITESPACE
))
1680 t
= t
->next
; /* Skip the actual token */
1681 while (tok_type_(t
, TOK_WHITESPACE
))
1683 j
= !t
; /* Should be nothing left */
1688 t
= tline
= expand_smacro(tline
);
1689 while (tok_type_(t
, TOK_WHITESPACE
))
1692 j
= !t
; /* Should be empty */
1696 t
= tline
= expand_smacro(tline
);
1698 tokval
.t_type
= TOKEN_INVALID
;
1699 evalresult
= evaluate(ppscan
, tptr
, &tokval
,
1700 NULL
, pass
| CRITICAL
, error
, NULL
);
1705 "trailing garbage after expression ignored");
1706 if (!is_simple(evalresult
)) {
1708 "non-constant value given to `%s'", pp_directives
[ct
]);
1711 j
= reloc_value(evalresult
) != 0;
1716 "preprocessor directive `%s' not yet implemented",
1721 free_tlist(origline
);
1722 return j
^ PP_NEGATIVE(ct
);
1725 free_tlist(origline
);
1730 * Common code for defining an smacro
1732 static bool define_smacro(Context
*ctx
, char *mname
, bool casesense
,
1733 int nparam
, Token
*expansion
)
1735 SMacro
*smac
, **smhead
;
1736 struct hash_table
*smtbl
;
1738 if (smacro_defined(ctx
, mname
, nparam
, &smac
, casesense
)) {
1741 "single-line macro `%s' defined both with and"
1742 " without parameters", mname
);
1744 /* Some instances of the old code considered this a failure,
1745 some others didn't. What is the right thing to do here? */
1746 free_tlist(expansion
);
1747 return false; /* Failure */
1750 * We're redefining, so we have to take over an
1751 * existing SMacro structure. This means freeing
1752 * what was already in it.
1754 nasm_free(smac
->name
);
1755 free_tlist(smac
->expansion
);
1758 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
1759 smhead
= (SMacro
**) hash_findi_add(smtbl
, mname
);
1760 smac
= nasm_malloc(sizeof(SMacro
));
1761 smac
->next
= *smhead
;
1764 smac
->name
= nasm_strdup(mname
);
1765 smac
->casesense
= casesense
;
1766 smac
->nparam
= nparam
;
1767 smac
->expansion
= expansion
;
1768 smac
->in_progress
= false;
1769 return true; /* Success */
1773 * Undefine an smacro
1775 static void undef_smacro(Context
*ctx
, const char *mname
)
1777 SMacro
**smhead
, *s
, **sp
;
1778 struct hash_table
*smtbl
;
1780 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
1781 smhead
= (SMacro
**)hash_findi(smtbl
, mname
, NULL
);
1785 * We now have a macro name... go hunt for it.
1788 while ((s
= *sp
) != NULL
) {
1789 if (!mstrcmp(s
->name
, mname
, s
->casesense
)) {
1792 free_tlist(s
->expansion
);
1802 * Decode a size directive
1804 static int parse_size(const char *str
) {
1805 static const char *size_names
[] =
1806 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
1807 static const int sizes
[] =
1808 { 0, 1, 4, 16, 8, 10, 2, 32 };
1810 return sizes
[bsii(str
, size_names
, elements(size_names
))+1];
1814 * find and process preprocessor directive in passed line
1815 * Find out if a line contains a preprocessor directive, and deal
1818 * If a directive _is_ found, it is the responsibility of this routine
1819 * (and not the caller) to free_tlist() the line.
1821 * @param tline a pointer to the current tokeninzed line linked list
1822 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
1825 static int do_directive(Token
* tline
)
1827 enum preproc_token i
;
1835 char *p
, *pp
, *mname
;
1839 MMacro
*mmac
, **mmhead
;
1840 Token
*t
, *tt
, *param_start
, *macro_start
, *last
, **tptr
, *origline
;
1842 struct tokenval tokval
;
1844 MMacro
*tmp_defining
; /* Used when manipulating rep_nest */
1851 if (!tline
|| !tok_type_(tline
, TOK_PREPROC_ID
) ||
1852 (tline
->text
[1] == '%' || tline
->text
[1] == '$'
1853 || tline
->text
[1] == '!'))
1854 return NO_DIRECTIVE_FOUND
;
1856 i
= pp_token_hash(tline
->text
);
1859 * If we're in a non-emitting branch of a condition construct,
1860 * or walking to the end of an already terminated %rep block,
1861 * we should ignore all directives except for condition
1864 if (((istk
->conds
&& !emitting(istk
->conds
->state
)) ||
1865 (istk
->mstk
&& !istk
->mstk
->in_progress
)) && !is_condition(i
)) {
1866 return NO_DIRECTIVE_FOUND
;
1870 * If we're defining a macro or reading a %rep block, we should
1871 * ignore all directives except for %macro/%imacro (which
1872 * generate an error), %endm/%endmacro, and (only if we're in a
1873 * %rep block) %endrep. If we're in a %rep block, another %rep
1874 * causes an error, so should be let through.
1876 if (defining
&& i
!= PP_MACRO
&& i
!= PP_IMACRO
&&
1877 i
!= PP_ENDMACRO
&& i
!= PP_ENDM
&&
1878 (defining
->name
|| (i
!= PP_ENDREP
&& i
!= PP_REP
))) {
1879 return NO_DIRECTIVE_FOUND
;
1884 error(ERR_NONFATAL
, "unknown preprocessor directive `%s'",
1886 return NO_DIRECTIVE_FOUND
; /* didn't get it */
1889 /* Directive to tell NASM what the default stack size is. The
1890 * default is for a 16-bit stack, and this can be overriden with
1892 * the following form:
1894 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1896 tline
= tline
->next
;
1897 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1898 tline
= tline
->next
;
1899 if (!tline
|| tline
->type
!= TOK_ID
) {
1900 error(ERR_NONFATAL
, "`%%stacksize' missing size parameter");
1901 free_tlist(origline
);
1902 return DIRECTIVE_FOUND
;
1904 if (nasm_stricmp(tline
->text
, "flat") == 0) {
1905 /* All subsequent ARG directives are for a 32-bit stack */
1907 StackPointer
= "ebp";
1910 } else if (nasm_stricmp(tline
->text
, "flat64") == 0) {
1911 /* All subsequent ARG directives are for a 64-bit stack */
1913 StackPointer
= "rbp";
1916 } else if (nasm_stricmp(tline
->text
, "large") == 0) {
1917 /* All subsequent ARG directives are for a 16-bit stack,
1918 * far function call.
1921 StackPointer
= "bp";
1924 } else if (nasm_stricmp(tline
->text
, "small") == 0) {
1925 /* All subsequent ARG directives are for a 16-bit stack,
1926 * far function call. We don't support near functions.
1929 StackPointer
= "bp";
1933 error(ERR_NONFATAL
, "`%%stacksize' invalid size type");
1934 free_tlist(origline
);
1935 return DIRECTIVE_FOUND
;
1937 free_tlist(origline
);
1938 return DIRECTIVE_FOUND
;
1941 /* TASM like ARG directive to define arguments to functions, in
1942 * the following form:
1944 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1948 char *arg
, directive
[256];
1949 int size
= StackSize
;
1951 /* Find the argument name */
1952 tline
= tline
->next
;
1953 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1954 tline
= tline
->next
;
1955 if (!tline
|| tline
->type
!= TOK_ID
) {
1956 error(ERR_NONFATAL
, "`%%arg' missing argument parameter");
1957 free_tlist(origline
);
1958 return DIRECTIVE_FOUND
;
1962 /* Find the argument size type */
1963 tline
= tline
->next
;
1964 if (!tline
|| tline
->type
!= TOK_OTHER
1965 || tline
->text
[0] != ':') {
1967 "Syntax error processing `%%arg' directive");
1968 free_tlist(origline
);
1969 return DIRECTIVE_FOUND
;
1971 tline
= tline
->next
;
1972 if (!tline
|| tline
->type
!= TOK_ID
) {
1973 error(ERR_NONFATAL
, "`%%arg' missing size type parameter");
1974 free_tlist(origline
);
1975 return DIRECTIVE_FOUND
;
1978 /* Allow macro expansion of type parameter */
1979 tt
= tokenize(tline
->text
);
1980 tt
= expand_smacro(tt
);
1981 size
= parse_size(tt
->text
);
1984 "Invalid size type for `%%arg' missing directive");
1986 free_tlist(origline
);
1987 return DIRECTIVE_FOUND
;
1991 /* Round up to even stack slots */
1992 size
= (size
+StackSize
-1) & ~(StackSize
-1);
1994 /* Now define the macro for the argument */
1995 snprintf(directive
, sizeof(directive
), "%%define %s (%s+%d)",
1996 arg
, StackPointer
, offset
);
1997 do_directive(tokenize(directive
));
2000 /* Move to the next argument in the list */
2001 tline
= tline
->next
;
2002 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2003 tline
= tline
->next
;
2004 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2006 free_tlist(origline
);
2007 return DIRECTIVE_FOUND
;
2010 /* TASM like LOCAL directive to define local variables for a
2011 * function, in the following form:
2013 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2015 * The '= LocalSize' at the end is ignored by NASM, but is
2016 * required by TASM to define the local parameter size (and used
2017 * by the TASM macro package).
2019 offset
= LocalOffset
;
2021 char *local
, directive
[256];
2022 int size
= StackSize
;
2024 /* Find the argument name */
2025 tline
= tline
->next
;
2026 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2027 tline
= tline
->next
;
2028 if (!tline
|| tline
->type
!= TOK_ID
) {
2030 "`%%local' missing argument parameter");
2031 free_tlist(origline
);
2032 return DIRECTIVE_FOUND
;
2034 local
= tline
->text
;
2036 /* Find the argument size type */
2037 tline
= tline
->next
;
2038 if (!tline
|| tline
->type
!= TOK_OTHER
2039 || tline
->text
[0] != ':') {
2041 "Syntax error processing `%%local' directive");
2042 free_tlist(origline
);
2043 return DIRECTIVE_FOUND
;
2045 tline
= tline
->next
;
2046 if (!tline
|| tline
->type
!= TOK_ID
) {
2048 "`%%local' missing size type parameter");
2049 free_tlist(origline
);
2050 return DIRECTIVE_FOUND
;
2053 /* Allow macro expansion of type parameter */
2054 tt
= tokenize(tline
->text
);
2055 tt
= expand_smacro(tt
);
2056 size
= parse_size(tt
->text
);
2059 "Invalid size type for `%%local' missing directive");
2061 free_tlist(origline
);
2062 return DIRECTIVE_FOUND
;
2066 /* Round up to even stack slots */
2067 size
= (size
+StackSize
-1) & ~(StackSize
-1);
2069 offset
+= size
; /* Negative offset, increment before */
2071 /* Now define the macro for the argument */
2072 snprintf(directive
, sizeof(directive
), "%%define %s (%s-%d)",
2073 local
, StackPointer
, offset
);
2074 do_directive(tokenize(directive
));
2076 /* Now define the assign to setup the enter_c macro correctly */
2077 snprintf(directive
, sizeof(directive
),
2078 "%%assign %%$localsize %%$localsize+%d", size
);
2079 do_directive(tokenize(directive
));
2081 /* Move to the next argument in the list */
2082 tline
= tline
->next
;
2083 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2084 tline
= tline
->next
;
2085 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2086 LocalOffset
= offset
;
2087 free_tlist(origline
);
2088 return DIRECTIVE_FOUND
;
2092 error(ERR_WARNING
, "trailing garbage after `%%clear' ignored");
2095 free_tlist(origline
);
2096 return DIRECTIVE_FOUND
;
2099 t
= tline
->next
= expand_smacro(tline
->next
);
2101 if (!t
|| (t
->type
!= TOK_STRING
&&
2102 t
->type
!= TOK_INTERNAL_STRING
)) {
2103 error(ERR_NONFATAL
, "`%%depend' expects a file name");
2104 free_tlist(origline
);
2105 return DIRECTIVE_FOUND
; /* but we did _something_ */
2109 "trailing garbage after `%%depend' ignored");
2111 if (t
->type
!= TOK_INTERNAL_STRING
)
2112 nasm_unquote(p
, NULL
);
2113 if (dephead
&& !in_list(*dephead
, p
)) {
2114 StrList
*sl
= nasm_malloc(strlen(p
)+1+sizeof sl
->next
);
2118 deptail
= &sl
->next
;
2120 free_tlist(origline
);
2121 return DIRECTIVE_FOUND
;
2124 t
= tline
->next
= expand_smacro(tline
->next
);
2127 if (!t
|| (t
->type
!= TOK_STRING
&&
2128 t
->type
!= TOK_INTERNAL_STRING
)) {
2129 error(ERR_NONFATAL
, "`%%include' expects a file name");
2130 free_tlist(origline
);
2131 return DIRECTIVE_FOUND
; /* but we did _something_ */
2135 "trailing garbage after `%%include' ignored");
2137 if (t
->type
!= TOK_INTERNAL_STRING
)
2138 nasm_unquote(p
, NULL
);
2139 inc
= nasm_malloc(sizeof(Include
));
2142 inc
->fp
= inc_fopen(p
, dephead
, &deptail
, pass
== 0);
2144 /* -MG given but file not found */
2147 inc
->fname
= src_set_fname(nasm_strdup(p
));
2148 inc
->lineno
= src_set_linnum(0);
2150 inc
->expansion
= NULL
;
2153 list
->uplevel(LIST_INCLUDE
);
2155 free_tlist(origline
);
2156 return DIRECTIVE_FOUND
;
2160 static const macros_t
*use_pkg
;
2161 const char *pkg_macro
;
2163 t
= tline
->next
= expand_smacro(tline
->next
);
2166 if (!t
|| (t
->type
!= TOK_STRING
&&
2167 t
->type
!= TOK_INTERNAL_STRING
&&
2168 t
->type
!= TOK_ID
)) {
2169 error(ERR_NONFATAL
, "`%%use' expects a package name");
2170 free_tlist(origline
);
2171 return DIRECTIVE_FOUND
; /* but we did _something_ */
2175 "trailing garbage after `%%use' ignored");
2176 if (t
->type
== TOK_STRING
)
2177 nasm_unquote(t
->text
, NULL
);
2178 use_pkg
= nasm_stdmac_find_package(t
->text
);
2180 error(ERR_NONFATAL
, "unknown `%%use' package: %s", t
->text
);
2181 /* The first string will be <%define>__USE_*__ */
2182 pkg_macro
= (char *)use_pkg
+ 1;
2183 if (!smacro_defined(NULL
, pkg_macro
, 0, NULL
, true)) {
2184 /* Not already included, go ahead and include it */
2185 stdmacpos
= use_pkg
;
2187 free_tlist(origline
);
2188 return DIRECTIVE_FOUND
;
2191 tline
= tline
->next
;
2193 tline
= expand_id(tline
);
2195 if (!tok_type_(tline
, TOK_ID
)) {
2196 error(ERR_NONFATAL
, "`%%push' expects a context identifier");
2197 free_tlist(origline
);
2198 return DIRECTIVE_FOUND
; /* but we did _something_ */
2201 error(ERR_WARNING
, "trailing garbage after `%%push' ignored");
2202 p
= nasm_strdup(tline
->text
);
2204 p
= NULL
; /* Anonymous context */
2206 ctx
= nasm_malloc(sizeof(Context
));
2208 hash_init(&ctx
->localmac
, HASH_SMALL
);
2210 ctx
->number
= unique
++;
2212 free_tlist(origline
);
2216 tline
= tline
->next
;
2218 tline
= expand_id(tline
);
2220 if (!tok_type_(tline
, TOK_ID
)) {
2221 error(ERR_NONFATAL
, "`%%repl' expects a context identifier");
2222 free_tlist(origline
);
2223 return DIRECTIVE_FOUND
; /* but we did _something_ */
2226 error(ERR_WARNING
, "trailing garbage after `%%repl' ignored");
2227 p
= nasm_strdup(tline
->text
);
2232 error(ERR_NONFATAL
, "`%%repl': context stack is empty");
2234 nasm_free(cstk
->name
);
2237 free_tlist(origline
);
2242 error(ERR_WARNING
, "trailing garbage after `%%pop' ignored");
2244 error(ERR_NONFATAL
, "`%%pop': context stack is already empty");
2247 free_tlist(origline
);
2253 int severity
= PP_ERROR
? ERR_NONFATAL
|ERR_NO_SEVERITY
:
2254 ERR_WARNING
|ERR_NO_SEVERITY
;
2256 tline
->next
= expand_smacro(tline
->next
);
2257 tline
= tline
->next
;
2259 t
= tline
? tline
->next
: NULL
;
2261 if (tok_type_(tline
, TOK_STRING
) && !t
) {
2262 /* The line contains only a quoted string */
2264 nasm_unquote(p
, NULL
);
2265 error(severity
, "%s: %s", pp_directives
[i
], p
);
2267 /* Not a quoted string, or more than a quoted string */
2268 p
= detoken(tline
, false);
2269 error(severity
, "%s: %s", pp_directives
[i
], p
);
2272 free_tlist(origline
);
2277 if (istk
->conds
&& !emitting(istk
->conds
->state
))
2280 j
= if_condition(tline
->next
, i
);
2281 tline
->next
= NULL
; /* it got freed */
2282 j
= j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2284 cond
= nasm_malloc(sizeof(Cond
));
2285 cond
->next
= istk
->conds
;
2288 free_tlist(origline
);
2289 return DIRECTIVE_FOUND
;
2293 error(ERR_FATAL
, "`%s': no matching `%%if'", pp_directives
[i
]);
2294 if (emitting(istk
->conds
->state
)
2295 || istk
->conds
->state
== COND_NEVER
)
2296 istk
->conds
->state
= COND_NEVER
;
2299 * IMPORTANT: In the case of %if, we will already have
2300 * called expand_mmac_params(); however, if we're
2301 * processing an %elif we must have been in a
2302 * non-emitting mode, which would have inhibited
2303 * the normal invocation of expand_mmac_params(). Therefore,
2304 * we have to do it explicitly here.
2306 j
= if_condition(expand_mmac_params(tline
->next
), i
);
2307 tline
->next
= NULL
; /* it got freed */
2308 istk
->conds
->state
=
2309 j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2311 free_tlist(origline
);
2312 return DIRECTIVE_FOUND
;
2316 error(ERR_WARNING
, "trailing garbage after `%%else' ignored");
2318 error(ERR_FATAL
, "`%%else': no matching `%%if'");
2319 if (emitting(istk
->conds
->state
)
2320 || istk
->conds
->state
== COND_NEVER
)
2321 istk
->conds
->state
= COND_ELSE_FALSE
;
2323 istk
->conds
->state
= COND_ELSE_TRUE
;
2324 free_tlist(origline
);
2325 return DIRECTIVE_FOUND
;
2329 error(ERR_WARNING
, "trailing garbage after `%%endif' ignored");
2331 error(ERR_FATAL
, "`%%endif': no matching `%%if'");
2333 istk
->conds
= cond
->next
;
2335 free_tlist(origline
);
2336 return DIRECTIVE_FOUND
;
2342 "`%%%smacro': already defining a macro",
2343 (i
== PP_IMACRO
? "i" : ""));
2344 tline
= tline
->next
;
2346 tline
= expand_id(tline
);
2347 if (!tok_type_(tline
, TOK_ID
)) {
2349 "`%%%smacro' expects a macro name",
2350 (i
== PP_IMACRO
? "i" : ""));
2351 return DIRECTIVE_FOUND
;
2353 defining
= nasm_malloc(sizeof(MMacro
));
2354 defining
->name
= nasm_strdup(tline
->text
);
2355 defining
->casesense
= (i
== PP_MACRO
);
2356 defining
->plus
= false;
2357 defining
->nolist
= false;
2358 defining
->in_progress
= 0;
2359 defining
->rep_nest
= NULL
;
2360 tline
= expand_smacro(tline
->next
);
2362 if (!tok_type_(tline
, TOK_NUMBER
)) {
2364 "`%%%smacro' expects a parameter count",
2365 (i
== PP_IMACRO
? "i" : ""));
2366 defining
->nparam_min
= defining
->nparam_max
= 0;
2368 defining
->nparam_min
= defining
->nparam_max
=
2369 readnum(tline
->text
, &err
);
2372 "unable to parse parameter count `%s'", tline
->text
);
2374 if (tline
&& tok_is_(tline
->next
, "-")) {
2375 tline
= tline
->next
->next
;
2376 if (tok_is_(tline
, "*"))
2377 defining
->nparam_max
= INT_MAX
;
2378 else if (!tok_type_(tline
, TOK_NUMBER
))
2380 "`%%%smacro' expects a parameter count after `-'",
2381 (i
== PP_IMACRO
? "i" : ""));
2383 defining
->nparam_max
= readnum(tline
->text
, &err
);
2386 "unable to parse parameter count `%s'",
2388 if (defining
->nparam_min
> defining
->nparam_max
)
2390 "minimum parameter count exceeds maximum");
2393 if (tline
&& tok_is_(tline
->next
, "+")) {
2394 tline
= tline
->next
;
2395 defining
->plus
= true;
2397 if (tline
&& tok_type_(tline
->next
, TOK_ID
) &&
2398 !nasm_stricmp(tline
->next
->text
, ".nolist")) {
2399 tline
= tline
->next
;
2400 defining
->nolist
= true;
2402 mmac
= (MMacro
*) hash_findix(&mmacros
, defining
->name
);
2404 if (!strcmp(mmac
->name
, defining
->name
) &&
2405 (mmac
->nparam_min
<= defining
->nparam_max
2407 && (defining
->nparam_min
<= mmac
->nparam_max
2410 "redefining multi-line macro `%s'", defining
->name
);
2416 * Handle default parameters.
2418 if (tline
&& tline
->next
) {
2419 defining
->dlist
= tline
->next
;
2421 count_mmac_params(defining
->dlist
, &defining
->ndefs
,
2422 &defining
->defaults
);
2424 defining
->dlist
= NULL
;
2425 defining
->defaults
= NULL
;
2427 defining
->expansion
= NULL
;
2428 free_tlist(origline
);
2429 return DIRECTIVE_FOUND
;
2434 error(ERR_NONFATAL
, "`%s': not defining a macro", tline
->text
);
2435 return DIRECTIVE_FOUND
;
2437 mmhead
= (MMacro
**) hash_findi_add(&mmacros
, defining
->name
);
2438 defining
->next
= *mmhead
;
2441 free_tlist(origline
);
2442 return DIRECTIVE_FOUND
;
2445 if (tline
->next
&& tline
->next
->type
== TOK_WHITESPACE
)
2446 tline
= tline
->next
;
2447 if (tline
->next
== NULL
) {
2448 free_tlist(origline
);
2449 error(ERR_NONFATAL
, "`%%rotate' missing rotate count");
2450 return DIRECTIVE_FOUND
;
2452 t
= expand_smacro(tline
->next
);
2454 free_tlist(origline
);
2457 tokval
.t_type
= TOKEN_INVALID
;
2459 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2462 return DIRECTIVE_FOUND
;
2465 "trailing garbage after expression ignored");
2466 if (!is_simple(evalresult
)) {
2467 error(ERR_NONFATAL
, "non-constant value given to `%%rotate'");
2468 return DIRECTIVE_FOUND
;
2471 while (mmac
&& !mmac
->name
) /* avoid mistaking %reps for macros */
2472 mmac
= mmac
->next_active
;
2474 error(ERR_NONFATAL
, "`%%rotate' invoked outside a macro call");
2475 } else if (mmac
->nparam
== 0) {
2477 "`%%rotate' invoked within macro without parameters");
2479 int rotate
= mmac
->rotate
+ reloc_value(evalresult
);
2481 rotate
%= (int)mmac
->nparam
;
2483 rotate
+= mmac
->nparam
;
2485 mmac
->rotate
= rotate
;
2487 return DIRECTIVE_FOUND
;
2492 tline
= tline
->next
;
2493 } while (tok_type_(tline
, TOK_WHITESPACE
));
2495 if (tok_type_(tline
, TOK_ID
) &&
2496 nasm_stricmp(tline
->text
, ".nolist") == 0) {
2499 tline
= tline
->next
;
2500 } while (tok_type_(tline
, TOK_WHITESPACE
));
2504 t
= expand_smacro(tline
);
2506 tokval
.t_type
= TOKEN_INVALID
;
2508 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2510 free_tlist(origline
);
2511 return DIRECTIVE_FOUND
;
2515 "trailing garbage after expression ignored");
2516 if (!is_simple(evalresult
)) {
2517 error(ERR_NONFATAL
, "non-constant value given to `%%rep'");
2518 return DIRECTIVE_FOUND
;
2520 count
= reloc_value(evalresult
) + 1;
2522 error(ERR_NONFATAL
, "`%%rep' expects a repeat count");
2525 free_tlist(origline
);
2527 tmp_defining
= defining
;
2528 defining
= nasm_malloc(sizeof(MMacro
));
2529 defining
->name
= NULL
; /* flags this macro as a %rep block */
2530 defining
->casesense
= false;
2531 defining
->plus
= false;
2532 defining
->nolist
= nolist
;
2533 defining
->in_progress
= count
;
2534 defining
->nparam_min
= defining
->nparam_max
= 0;
2535 defining
->defaults
= NULL
;
2536 defining
->dlist
= NULL
;
2537 defining
->expansion
= NULL
;
2538 defining
->next_active
= istk
->mstk
;
2539 defining
->rep_nest
= tmp_defining
;
2540 return DIRECTIVE_FOUND
;
2543 if (!defining
|| defining
->name
) {
2544 error(ERR_NONFATAL
, "`%%endrep': no matching `%%rep'");
2545 return DIRECTIVE_FOUND
;
2549 * Now we have a "macro" defined - although it has no name
2550 * and we won't be entering it in the hash tables - we must
2551 * push a macro-end marker for it on to istk->expansion.
2552 * After that, it will take care of propagating itself (a
2553 * macro-end marker line for a macro which is really a %rep
2554 * block will cause the macro to be re-expanded, complete
2555 * with another macro-end marker to ensure the process
2556 * continues) until the whole expansion is forcibly removed
2557 * from istk->expansion by a %exitrep.
2559 l
= nasm_malloc(sizeof(Line
));
2560 l
->next
= istk
->expansion
;
2561 l
->finishes
= defining
;
2563 istk
->expansion
= l
;
2565 istk
->mstk
= defining
;
2567 list
->uplevel(defining
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
2568 tmp_defining
= defining
;
2569 defining
= defining
->rep_nest
;
2570 free_tlist(origline
);
2571 return DIRECTIVE_FOUND
;
2575 * We must search along istk->expansion until we hit a
2576 * macro-end marker for a macro with no name. Then we set
2577 * its `in_progress' flag to 0.
2579 for (l
= istk
->expansion
; l
; l
= l
->next
)
2580 if (l
->finishes
&& !l
->finishes
->name
)
2584 l
->finishes
->in_progress
= 0;
2586 error(ERR_NONFATAL
, "`%%exitrep' not within `%%rep' block");
2587 free_tlist(origline
);
2588 return DIRECTIVE_FOUND
;
2594 casesense
= (i
== PP_DEFINE
|| i
== PP_XDEFINE
);
2596 tline
= tline
->next
;
2598 tline
= expand_id(tline
);
2599 if (!tline
|| (tline
->type
!= TOK_ID
&&
2600 (tline
->type
!= TOK_PREPROC_ID
||
2601 tline
->text
[1] != '$'))) {
2602 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
2604 free_tlist(origline
);
2605 return DIRECTIVE_FOUND
;
2608 ctx
= get_ctx(tline
->text
, false);
2610 mname
= tline
->text
;
2612 param_start
= tline
= tline
->next
;
2615 /* Expand the macro definition now for %xdefine and %ixdefine */
2616 if ((i
== PP_XDEFINE
) || (i
== PP_IXDEFINE
))
2617 tline
= expand_smacro(tline
);
2619 if (tok_is_(tline
, "(")) {
2621 * This macro has parameters.
2624 tline
= tline
->next
;
2628 error(ERR_NONFATAL
, "parameter identifier expected");
2629 free_tlist(origline
);
2630 return DIRECTIVE_FOUND
;
2632 if (tline
->type
!= TOK_ID
) {
2634 "`%s': parameter identifier expected",
2636 free_tlist(origline
);
2637 return DIRECTIVE_FOUND
;
2639 tline
->type
= TOK_SMAC_PARAM
+ nparam
++;
2640 tline
= tline
->next
;
2642 if (tok_is_(tline
, ",")) {
2643 tline
= tline
->next
;
2646 if (!tok_is_(tline
, ")")) {
2648 "`)' expected to terminate macro template");
2649 free_tlist(origline
);
2650 return DIRECTIVE_FOUND
;
2655 tline
= tline
->next
;
2657 if (tok_type_(tline
, TOK_WHITESPACE
))
2658 last
= tline
, tline
= tline
->next
;
2663 if (t
->type
== TOK_ID
) {
2664 for (tt
= param_start
; tt
; tt
= tt
->next
)
2665 if (tt
->type
>= TOK_SMAC_PARAM
&&
2666 !strcmp(tt
->text
, t
->text
))
2670 t
->next
= macro_start
;
2675 * Good. We now have a macro name, a parameter count, and a
2676 * token list (in reverse order) for an expansion. We ought
2677 * to be OK just to create an SMacro, store it, and let
2678 * free_tlist have the rest of the line (which we have
2679 * carefully re-terminated after chopping off the expansion
2682 define_smacro(ctx
, mname
, casesense
, nparam
, macro_start
);
2683 free_tlist(origline
);
2684 return DIRECTIVE_FOUND
;
2687 tline
= tline
->next
;
2689 tline
= expand_id(tline
);
2690 if (!tline
|| (tline
->type
!= TOK_ID
&&
2691 (tline
->type
!= TOK_PREPROC_ID
||
2692 tline
->text
[1] != '$'))) {
2693 error(ERR_NONFATAL
, "`%%undef' expects a macro identifier");
2694 free_tlist(origline
);
2695 return DIRECTIVE_FOUND
;
2699 "trailing garbage after macro name ignored");
2702 /* Find the context that symbol belongs to */
2703 ctx
= get_ctx(tline
->text
, false);
2704 undef_smacro(ctx
, tline
->text
);
2705 free_tlist(origline
);
2706 return DIRECTIVE_FOUND
;
2710 casesense
= (i
== PP_DEFSTR
);
2712 tline
= tline
->next
;
2714 tline
= expand_id(tline
);
2715 if (!tline
|| (tline
->type
!= TOK_ID
&&
2716 (tline
->type
!= TOK_PREPROC_ID
||
2717 tline
->text
[1] != '$'))) {
2718 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
2720 free_tlist(origline
);
2721 return DIRECTIVE_FOUND
;
2724 ctx
= get_ctx(tline
->text
, false);
2726 mname
= tline
->text
;
2728 tline
= expand_smacro(tline
->next
);
2731 while (tok_type_(tline
, TOK_WHITESPACE
))
2732 tline
= delete_Token(tline
);
2734 p
= detoken(tline
, false);
2735 macro_start
= nasm_malloc(sizeof(*macro_start
));
2736 macro_start
->next
= NULL
;
2737 macro_start
->text
= nasm_quote(p
, strlen(p
));
2738 macro_start
->type
= TOK_STRING
;
2739 macro_start
->a
.mac
= NULL
;
2743 * We now have a macro name, an implicit parameter count of
2744 * zero, and a string token to use as an expansion. Create
2745 * and store an SMacro.
2747 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2748 free_tlist(origline
);
2749 return DIRECTIVE_FOUND
;
2754 StrList
*xsl
= NULL
;
2755 StrList
**xst
= &xsl
;
2759 tline
= tline
->next
;
2761 tline
= expand_id(tline
);
2762 if (!tline
|| (tline
->type
!= TOK_ID
&&
2763 (tline
->type
!= TOK_PREPROC_ID
||
2764 tline
->text
[1] != '$'))) {
2766 "`%%pathsearch' expects a macro identifier as first parameter");
2767 free_tlist(origline
);
2768 return DIRECTIVE_FOUND
;
2770 ctx
= get_ctx(tline
->text
, false);
2772 mname
= tline
->text
;
2774 tline
= expand_smacro(tline
->next
);
2778 while (tok_type_(t
, TOK_WHITESPACE
))
2781 if (!t
|| (t
->type
!= TOK_STRING
&&
2782 t
->type
!= TOK_INTERNAL_STRING
)) {
2783 error(ERR_NONFATAL
, "`%%pathsearch' expects a file name");
2785 free_tlist(origline
);
2786 return DIRECTIVE_FOUND
; /* but we did _something_ */
2790 "trailing garbage after `%%pathsearch' ignored");
2792 if (t
->type
!= TOK_INTERNAL_STRING
)
2793 nasm_unquote(p
, NULL
);
2795 fp
= inc_fopen(p
, &xsl
, &xst
, true);
2798 fclose(fp
); /* Don't actually care about the file */
2800 macro_start
= nasm_malloc(sizeof(*macro_start
));
2801 macro_start
->next
= NULL
;
2802 macro_start
->text
= nasm_quote(p
, strlen(p
));
2803 macro_start
->type
= TOK_STRING
;
2804 macro_start
->a
.mac
= NULL
;
2809 * We now have a macro name, an implicit parameter count of
2810 * zero, and a string token to use as an expansion. Create
2811 * and store an SMacro.
2813 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2815 free_tlist(origline
);
2816 return DIRECTIVE_FOUND
;
2822 tline
= tline
->next
;
2824 tline
= expand_id(tline
);
2825 if (!tline
|| (tline
->type
!= TOK_ID
&&
2826 (tline
->type
!= TOK_PREPROC_ID
||
2827 tline
->text
[1] != '$'))) {
2829 "`%%strlen' expects a macro identifier as first parameter");
2830 free_tlist(origline
);
2831 return DIRECTIVE_FOUND
;
2833 ctx
= get_ctx(tline
->text
, false);
2835 mname
= tline
->text
;
2837 tline
= expand_smacro(tline
->next
);
2841 while (tok_type_(t
, TOK_WHITESPACE
))
2843 /* t should now point to the string */
2844 if (t
->type
!= TOK_STRING
) {
2846 "`%%strlen` requires string as second parameter");
2848 free_tlist(origline
);
2849 return DIRECTIVE_FOUND
;
2852 macro_start
= nasm_malloc(sizeof(*macro_start
));
2853 macro_start
->next
= NULL
;
2854 make_tok_num(macro_start
, nasm_unquote(t
->text
, NULL
));
2855 macro_start
->a
.mac
= NULL
;
2858 * We now have a macro name, an implicit parameter count of
2859 * zero, and a numeric token to use as an expansion. Create
2860 * and store an SMacro.
2862 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2864 free_tlist(origline
);
2865 return DIRECTIVE_FOUND
;
2870 tline
= tline
->next
;
2872 tline
= expand_id(tline
);
2873 if (!tline
|| (tline
->type
!= TOK_ID
&&
2874 (tline
->type
!= TOK_PREPROC_ID
||
2875 tline
->text
[1] != '$'))) {
2877 "`%%strcat' expects a macro identifier as first parameter");
2878 free_tlist(origline
);
2879 return DIRECTIVE_FOUND
;
2881 ctx
= get_ctx(tline
->text
, false);
2883 mname
= tline
->text
;
2885 tline
= expand_smacro(tline
->next
);
2889 for (t
= tline
; t
; t
= t
->next
) {
2891 case TOK_WHITESPACE
:
2894 len
+= t
->a
.len
= nasm_unquote(t
->text
, NULL
);
2897 if (!strcmp(t
->text
, ",")) /* permit comma separators */
2899 /* else fall through */
2902 "non-string passed to `%%strcat' (%d)", t
->type
);
2904 free_tlist(origline
);
2905 return DIRECTIVE_FOUND
;
2909 p
= pp
= nasm_malloc(len
);
2911 for (t
= tline
; t
; t
= t
->next
) {
2912 if (t
->type
== TOK_STRING
) {
2913 memcpy(p
, t
->text
, t
->a
.len
);
2919 * We now have a macro name, an implicit parameter count of
2920 * zero, and a numeric token to use as an expansion. Create
2921 * and store an SMacro.
2923 macro_start
= new_Token(NULL
, TOK_STRING
, NULL
, 0);
2924 macro_start
->text
= nasm_quote(pp
, len
);
2926 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2928 free_tlist(origline
);
2929 return DIRECTIVE_FOUND
;
2938 tline
= tline
->next
;
2940 tline
= expand_id(tline
);
2941 if (!tline
|| (tline
->type
!= TOK_ID
&&
2942 (tline
->type
!= TOK_PREPROC_ID
||
2943 tline
->text
[1] != '$'))) {
2945 "`%%substr' expects a macro identifier as first parameter");
2946 free_tlist(origline
);
2947 return DIRECTIVE_FOUND
;
2949 ctx
= get_ctx(tline
->text
, false);
2951 mname
= tline
->text
;
2953 tline
= expand_smacro(tline
->next
);
2957 while (tok_type_(t
, TOK_WHITESPACE
))
2960 /* t should now point to the string */
2961 if (t
->type
!= TOK_STRING
) {
2963 "`%%substr` requires string as second parameter");
2965 free_tlist(origline
);
2966 return DIRECTIVE_FOUND
;
2971 tokval
.t_type
= TOKEN_INVALID
;
2972 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
,
2976 free_tlist(origline
);
2977 return DIRECTIVE_FOUND
;
2978 } else if (!is_simple(evalresult
)) {
2979 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
2981 free_tlist(origline
);
2982 return DIRECTIVE_FOUND
;
2984 a1
= evalresult
->value
-1;
2986 while (tok_type_(tt
, TOK_WHITESPACE
))
2989 a2
= 1; /* Backwards compatibility: one character */
2991 tokval
.t_type
= TOKEN_INVALID
;
2992 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
,
2996 free_tlist(origline
);
2997 return DIRECTIVE_FOUND
;
2998 } else if (!is_simple(evalresult
)) {
2999 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
3001 free_tlist(origline
);
3002 return DIRECTIVE_FOUND
;
3004 a2
= evalresult
->value
;
3007 len
= nasm_unquote(t
->text
, NULL
);
3010 if (a1
+a2
> (int64_t)len
)
3013 macro_start
= nasm_malloc(sizeof(*macro_start
));
3014 macro_start
->next
= NULL
;
3015 macro_start
->text
= nasm_quote((a1
< 0) ? "" : t
->text
+a1
, a2
);
3016 macro_start
->type
= TOK_STRING
;
3017 macro_start
->a
.mac
= NULL
;
3020 * We now have a macro name, an implicit parameter count of
3021 * zero, and a numeric token to use as an expansion. Create
3022 * and store an SMacro.
3024 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3026 free_tlist(origline
);
3027 return DIRECTIVE_FOUND
;
3032 casesense
= (i
== PP_ASSIGN
);
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 "`%%%sassign' expects a macro identifier",
3042 (i
== PP_IASSIGN
? "i" : ""));
3043 free_tlist(origline
);
3044 return DIRECTIVE_FOUND
;
3046 ctx
= get_ctx(tline
->text
, false);
3048 mname
= tline
->text
;
3050 tline
= expand_smacro(tline
->next
);
3055 tokval
.t_type
= TOKEN_INVALID
;
3057 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
3060 free_tlist(origline
);
3061 return DIRECTIVE_FOUND
;
3066 "trailing garbage after expression ignored");
3068 if (!is_simple(evalresult
)) {
3070 "non-constant value given to `%%%sassign'",
3071 (i
== PP_IASSIGN
? "i" : ""));
3072 free_tlist(origline
);
3073 return DIRECTIVE_FOUND
;
3076 macro_start
= nasm_malloc(sizeof(*macro_start
));
3077 macro_start
->next
= NULL
;
3078 make_tok_num(macro_start
, reloc_value(evalresult
));
3079 macro_start
->a
.mac
= NULL
;
3082 * We now have a macro name, an implicit parameter count of
3083 * zero, and a numeric token to use as an expansion. Create
3084 * and store an SMacro.
3086 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3087 free_tlist(origline
);
3088 return DIRECTIVE_FOUND
;
3092 * Syntax is `%line nnn[+mmm] [filename]'
3094 tline
= tline
->next
;
3096 if (!tok_type_(tline
, TOK_NUMBER
)) {
3097 error(ERR_NONFATAL
, "`%%line' expects line number");
3098 free_tlist(origline
);
3099 return DIRECTIVE_FOUND
;
3101 k
= readnum(tline
->text
, &err
);
3103 tline
= tline
->next
;
3104 if (tok_is_(tline
, "+")) {
3105 tline
= tline
->next
;
3106 if (!tok_type_(tline
, TOK_NUMBER
)) {
3107 error(ERR_NONFATAL
, "`%%line' expects line increment");
3108 free_tlist(origline
);
3109 return DIRECTIVE_FOUND
;
3111 m
= readnum(tline
->text
, &err
);
3112 tline
= tline
->next
;
3118 nasm_free(src_set_fname(detoken(tline
, false)));
3120 free_tlist(origline
);
3121 return DIRECTIVE_FOUND
;
3125 "preprocessor directive `%s' not yet implemented",
3129 return DIRECTIVE_FOUND
;
3133 * Ensure that a macro parameter contains a condition code and
3134 * nothing else. Return the condition code index if so, or -1
3137 static int find_cc(Token
* t
)
3143 return -1; /* Probably a %+ without a space */
3146 if (t
->type
!= TOK_ID
)
3150 if (tt
&& (tt
->type
!= TOK_OTHER
|| strcmp(tt
->text
, ",")))
3154 j
= elements(conditions
);
3157 m
= nasm_stricmp(t
->text
, conditions
[k
]);
3173 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3174 * %-n) and MMacro-local identifiers (%%foo).
3176 static Token
*expand_mmac_params(Token
* tline
)
3178 Token
*t
, *tt
, **tail
, *thead
;
3184 if (tline
->type
== TOK_PREPROC_ID
&&
3185 (((tline
->text
[1] == '+' || tline
->text
[1] == '-')
3186 && tline
->text
[2]) || tline
->text
[1] == '%'
3187 || (tline
->text
[1] >= '0' && tline
->text
[1] <= '9'))) {
3189 int type
= 0, cc
; /* type = 0 to placate optimisers */
3196 tline
= tline
->next
;
3199 while (mac
&& !mac
->name
) /* avoid mistaking %reps for macros */
3200 mac
= mac
->next_active
;
3202 error(ERR_NONFATAL
, "`%s': not in a macro call", t
->text
);
3204 switch (t
->text
[1]) {
3206 * We have to make a substitution of one of the
3207 * forms %1, %-1, %+1, %%foo, %0.
3211 snprintf(tmpbuf
, sizeof(tmpbuf
), "%d", mac
->nparam
);
3212 text
= nasm_strdup(tmpbuf
);
3216 snprintf(tmpbuf
, sizeof(tmpbuf
), "..@%"PRIu64
".",
3218 text
= nasm_strcat(tmpbuf
, t
->text
+ 2);
3221 n
= atoi(t
->text
+ 2) - 1;
3222 if (n
>= mac
->nparam
)
3225 if (mac
->nparam
> 1)
3226 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3227 tt
= mac
->params
[n
];
3232 "macro parameter %d is not a condition code",
3237 if (inverse_ccs
[cc
] == -1) {
3239 "condition code `%s' is not invertible",
3244 nasm_strdup(conditions
[inverse_ccs
[cc
]]);
3248 n
= atoi(t
->text
+ 2) - 1;
3249 if (n
>= mac
->nparam
)
3252 if (mac
->nparam
> 1)
3253 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3254 tt
= mac
->params
[n
];
3259 "macro parameter %d is not a condition code",
3264 text
= nasm_strdup(conditions
[cc
]);
3268 n
= atoi(t
->text
+ 1) - 1;
3269 if (n
>= mac
->nparam
)
3272 if (mac
->nparam
> 1)
3273 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3274 tt
= mac
->params
[n
];
3277 for (i
= 0; i
< mac
->paramlen
[n
]; i
++) {
3278 *tail
= new_Token(NULL
, tt
->type
, tt
->text
, 0);
3279 tail
= &(*tail
)->next
;
3283 text
= NULL
; /* we've done it here */
3299 tline
= tline
->next
;
3306 for (; t
&& (tt
= t
->next
) != NULL
; t
= t
->next
)
3308 case TOK_WHITESPACE
:
3309 if (tt
->type
== TOK_WHITESPACE
) {
3310 t
->next
= delete_Token(tt
);
3314 if (tt
->type
== TOK_ID
|| tt
->type
== TOK_NUMBER
) {
3315 char *tmp
= nasm_strcat(t
->text
, tt
->text
);
3318 t
->next
= delete_Token(tt
);
3322 if (tt
->type
== TOK_NUMBER
) {
3323 char *tmp
= nasm_strcat(t
->text
, tt
->text
);
3326 t
->next
= delete_Token(tt
);
3337 * Expand all single-line macro calls made in the given line.
3338 * Return the expanded version of the line. The original is deemed
3339 * to be destroyed in the process. (In reality we'll just move
3340 * Tokens from input to output a lot of the time, rather than
3341 * actually bothering to destroy and replicate.)
3343 #define DEADMAN_LIMIT (1 << 20)
3345 static Token
*expand_smacro(Token
* tline
)
3347 Token
*t
, *tt
, *mstart
, **tail
, *thead
;
3348 struct hash_table
*smtbl
;
3349 SMacro
*head
= NULL
, *m
;
3352 unsigned int nparam
, sparam
;
3353 int brackets
, rescan
;
3354 Token
*org_tline
= tline
;
3357 int deadman
= DEADMAN_LIMIT
;
3360 * Trick: we should avoid changing the start token pointer since it can
3361 * be contained in "next" field of other token. Because of this
3362 * we allocate a copy of first token and work with it; at the end of
3363 * routine we copy it back
3367 new_Token(org_tline
->next
, org_tline
->type
, org_tline
->text
,
3369 tline
->a
.mac
= org_tline
->a
.mac
;
3370 nasm_free(org_tline
->text
);
3371 org_tline
->text
= NULL
;
3378 while (tline
) { /* main token loop */
3380 error(ERR_NONFATAL
, "interminable macro recursion");
3384 if ((mname
= tline
->text
)) {
3385 /* if this token is a local macro, look in local context */
3388 if (tline
->type
== TOK_ID
|| tline
->type
== TOK_PREPROC_ID
) {
3389 ctx
= get_ctx(mname
, true);
3391 smtbl
= &ctx
->localmac
;
3393 head
= (SMacro
*) hash_findix(smtbl
, mname
);
3396 * We've hit an identifier. As in is_mmacro below, we first
3397 * check whether the identifier is a single-line macro at
3398 * all, then think about checking for parameters if
3401 for (m
= head
; m
; m
= m
->next
)
3402 if (!mstrcmp(m
->name
, mname
, m
->casesense
))
3408 if (m
->nparam
== 0) {
3410 * Simple case: the macro is parameterless. Discard the
3411 * one token that the macro call took, and push the
3412 * expansion back on the to-do stack.
3414 if (!m
->expansion
) {
3415 if (!strcmp("__FILE__", m
->name
)) {
3418 src_get(&num
, &file
);
3419 tline
->text
= nasm_quote(file
, strlen(file
));
3420 tline
->type
= TOK_STRING
;
3424 if (!strcmp("__LINE__", m
->name
)) {
3425 nasm_free(tline
->text
);
3426 make_tok_num(tline
, src_get_linnum());
3429 if (!strcmp("__BITS__", m
->name
)) {
3430 nasm_free(tline
->text
);
3431 make_tok_num(tline
, globalbits
);
3434 tline
= delete_Token(tline
);
3439 * Complicated case: at least one macro with this name
3440 * exists and takes parameters. We must find the
3441 * parameters in the call, count them, find the SMacro
3442 * that corresponds to that form of the macro call, and
3443 * substitute for the parameters when we expand. What a
3446 /*tline = tline->next;
3447 skip_white_(tline); */
3450 while (tok_type_(t
, TOK_SMAC_END
)) {
3451 t
->a
.mac
->in_progress
= false;
3453 t
= tline
->next
= delete_Token(t
);
3456 } while (tok_type_(tline
, TOK_WHITESPACE
));
3457 if (!tok_is_(tline
, "(")) {
3459 * This macro wasn't called with parameters: ignore
3460 * the call. (Behaviour borrowed from gnu cpp.)
3469 sparam
= PARAM_DELTA
;
3470 params
= nasm_malloc(sparam
* sizeof(Token
*));
3471 params
[0] = tline
->next
;
3472 paramsize
= nasm_malloc(sparam
* sizeof(int));
3474 while (true) { /* parameter loop */
3476 * For some unusual expansions
3477 * which concatenates function call
3480 while (tok_type_(t
, TOK_SMAC_END
)) {
3481 t
->a
.mac
->in_progress
= false;
3483 t
= tline
->next
= delete_Token(t
);
3489 "macro call expects terminating `)'");
3492 if (tline
->type
== TOK_WHITESPACE
3494 if (paramsize
[nparam
])
3497 params
[nparam
] = tline
->next
;
3498 continue; /* parameter loop */
3500 if (tline
->type
== TOK_OTHER
3501 && tline
->text
[1] == 0) {
3502 char ch
= tline
->text
[0];
3503 if (ch
== ',' && !paren
&& brackets
<= 0) {
3504 if (++nparam
>= sparam
) {
3505 sparam
+= PARAM_DELTA
;
3506 params
= nasm_realloc(params
,
3511 nasm_realloc(paramsize
,
3515 params
[nparam
] = tline
->next
;
3516 paramsize
[nparam
] = 0;
3518 continue; /* parameter loop */
3521 (brackets
> 0 || (brackets
== 0 &&
3522 !paramsize
[nparam
])))
3524 if (!(brackets
++)) {
3525 params
[nparam
] = tline
->next
;
3526 continue; /* parameter loop */
3529 if (ch
== '}' && brackets
> 0)
3530 if (--brackets
== 0) {
3532 continue; /* parameter loop */
3534 if (ch
== '(' && !brackets
)
3536 if (ch
== ')' && brackets
<= 0)
3542 error(ERR_NONFATAL
, "braces do not "
3543 "enclose all of macro parameter");
3545 paramsize
[nparam
] += white
+ 1;
3547 } /* parameter loop */
3549 while (m
&& (m
->nparam
!= nparam
||
3550 mstrcmp(m
->name
, mname
,
3554 error(ERR_WARNING
| ERR_WARN_MNP
,
3555 "macro `%s' exists, "
3556 "but not taking %d parameters",
3557 mstart
->text
, nparam
);
3560 if (m
&& m
->in_progress
)
3562 if (!m
) { /* in progess or didn't find '(' or wrong nparam */
3564 * Design question: should we handle !tline, which
3565 * indicates missing ')' here, or expand those
3566 * macros anyway, which requires the (t) test a few
3570 nasm_free(paramsize
);
3574 * Expand the macro: we are placed on the last token of the
3575 * call, so that we can easily split the call from the
3576 * following tokens. We also start by pushing an SMAC_END
3577 * token for the cycle removal.
3584 tt
= new_Token(tline
, TOK_SMAC_END
, NULL
, 0);
3586 m
->in_progress
= true;
3588 for (t
= m
->expansion
; t
; t
= t
->next
) {
3589 if (t
->type
>= TOK_SMAC_PARAM
) {
3590 Token
*pcopy
= tline
, **ptail
= &pcopy
;
3594 ttt
= params
[t
->type
- TOK_SMAC_PARAM
];
3595 for (i
= paramsize
[t
->type
- TOK_SMAC_PARAM
];
3598 new_Token(tline
, ttt
->type
, ttt
->text
,
3604 } else if (t
->type
== TOK_PREPROC_Q
) {
3605 tt
= new_Token(tline
, TOK_ID
, mname
, 0);
3607 } else if (t
->type
== TOK_PREPROC_QQ
) {
3608 tt
= new_Token(tline
, TOK_ID
, m
->name
, 0);
3611 tt
= new_Token(tline
, t
->type
, t
->text
, 0);
3617 * Having done that, get rid of the macro call, and clean
3618 * up the parameters.
3621 nasm_free(paramsize
);
3623 continue; /* main token loop */
3628 if (tline
->type
== TOK_SMAC_END
) {
3629 tline
->a
.mac
->in_progress
= false;
3630 tline
= delete_Token(tline
);
3633 tline
= tline
->next
;
3641 * Now scan the entire line and look for successive TOK_IDs that resulted
3642 * after expansion (they can't be produced by tokenize()). The successive
3643 * TOK_IDs should be concatenated.
3644 * Also we look for %+ tokens and concatenate the tokens before and after
3645 * them (without white spaces in between).
3650 while (t
&& t
->type
!= TOK_ID
&& t
->type
!= TOK_PREPROC_ID
)
3654 if (t
->next
->type
== TOK_ID
||
3655 t
->next
->type
== TOK_PREPROC_ID
||
3656 t
->next
->type
== TOK_NUMBER
) {
3657 char *p
= nasm_strcat(t
->text
, t
->next
->text
);
3659 t
->next
= delete_Token(t
->next
);
3662 } else if (t
->next
->type
== TOK_WHITESPACE
&& t
->next
->next
&&
3663 t
->next
->next
->type
== TOK_PREPROC_ID
&&
3664 strcmp(t
->next
->next
->text
, "%+") == 0) {
3665 /* free the next whitespace, the %+ token and next whitespace */
3667 for (i
= 1; i
<= 3; i
++) {
3669 || (i
!= 2 && t
->next
->type
!= TOK_WHITESPACE
))
3671 t
->next
= delete_Token(t
->next
);
3676 /* If we concatenaded something, re-scan the line for macros */
3684 *org_tline
= *thead
;
3685 /* since we just gave text to org_line, don't free it */
3687 delete_Token(thead
);
3689 /* the expression expanded to empty line;
3690 we can't return NULL for some reasons
3691 we just set the line to a single WHITESPACE token. */
3692 memset(org_tline
, 0, sizeof(*org_tline
));
3693 org_tline
->text
= NULL
;
3694 org_tline
->type
= TOK_WHITESPACE
;
3703 * Similar to expand_smacro but used exclusively with macro identifiers
3704 * right before they are fetched in. The reason is that there can be
3705 * identifiers consisting of several subparts. We consider that if there
3706 * are more than one element forming the name, user wants a expansion,
3707 * otherwise it will be left as-is. Example:
3711 * the identifier %$abc will be left as-is so that the handler for %define
3712 * will suck it and define the corresponding value. Other case:
3714 * %define _%$abc cde
3716 * In this case user wants name to be expanded *before* %define starts
3717 * working, so we'll expand %$abc into something (if it has a value;
3718 * otherwise it will be left as-is) then concatenate all successive
3721 static Token
*expand_id(Token
* tline
)
3723 Token
*cur
, *oldnext
= NULL
;
3725 if (!tline
|| !tline
->next
)
3730 (cur
->next
->type
== TOK_ID
||
3731 cur
->next
->type
== TOK_PREPROC_ID
3732 || cur
->next
->type
== TOK_NUMBER
))
3735 /* If identifier consists of just one token, don't expand */
3740 oldnext
= cur
->next
; /* Detach the tail past identifier */
3741 cur
->next
= NULL
; /* so that expand_smacro stops here */
3744 tline
= expand_smacro(tline
);
3747 /* expand_smacro possibly changhed tline; re-scan for EOL */
3749 while (cur
&& cur
->next
)
3752 cur
->next
= oldnext
;
3759 * Determine whether the given line constitutes a multi-line macro
3760 * call, and return the MMacro structure called if so. Doesn't have
3761 * to check for an initial label - that's taken care of in
3762 * expand_mmacro - but must check numbers of parameters. Guaranteed
3763 * to be called with tline->type == TOK_ID, so the putative macro
3764 * name is easy to find.
3766 static MMacro
*is_mmacro(Token
* tline
, Token
*** params_array
)
3772 head
= (MMacro
*) hash_findix(&mmacros
, tline
->text
);
3775 * Efficiency: first we see if any macro exists with the given
3776 * name. If not, we can return NULL immediately. _Then_ we
3777 * count the parameters, and then we look further along the
3778 * list if necessary to find the proper MMacro.
3780 for (m
= head
; m
; m
= m
->next
)
3781 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
3787 * OK, we have a potential macro. Count and demarcate the
3790 count_mmac_params(tline
->next
, &nparam
, ¶ms
);
3793 * So we know how many parameters we've got. Find the MMacro
3794 * structure that handles this number.
3797 if (m
->nparam_min
<= nparam
3798 && (m
->plus
|| nparam
<= m
->nparam_max
)) {
3800 * This one is right. Just check if cycle removal
3801 * prohibits us using it before we actually celebrate...
3803 if (m
->in_progress
) {
3806 "self-reference in multi-line macro `%s'", m
->name
);
3812 * It's right, and we can use it. Add its default
3813 * parameters to the end of our list if necessary.
3815 if (m
->defaults
&& nparam
< m
->nparam_min
+ m
->ndefs
) {
3817 nasm_realloc(params
,
3818 ((m
->nparam_min
+ m
->ndefs
+
3819 1) * sizeof(*params
)));
3820 while (nparam
< m
->nparam_min
+ m
->ndefs
) {
3821 params
[nparam
] = m
->defaults
[nparam
- m
->nparam_min
];
3826 * If we've gone over the maximum parameter count (and
3827 * we're in Plus mode), ignore parameters beyond
3830 if (m
->plus
&& nparam
> m
->nparam_max
)
3831 nparam
= m
->nparam_max
;
3833 * Then terminate the parameter list, and leave.
3835 if (!params
) { /* need this special case */
3836 params
= nasm_malloc(sizeof(*params
));
3839 params
[nparam
] = NULL
;
3840 *params_array
= params
;
3844 * This one wasn't right: look for the next one with the
3847 for (m
= m
->next
; m
; m
= m
->next
)
3848 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
3853 * After all that, we didn't find one with the right number of
3854 * parameters. Issue a warning, and fail to expand the macro.
3856 error(ERR_WARNING
| ERR_WARN_MNP
,
3857 "macro `%s' exists, but not taking %d parameters",
3858 tline
->text
, nparam
);
3864 * Expand the multi-line macro call made by the given line, if
3865 * there is one to be expanded. If there is, push the expansion on
3866 * istk->expansion and return 1. Otherwise return 0.
3868 static int expand_mmacro(Token
* tline
)
3870 Token
*startline
= tline
;
3871 Token
*label
= NULL
;
3872 int dont_prepend
= 0;
3873 Token
**params
, *t
, *mtok
, *tt
;
3876 int i
, nparam
, *paramlen
;
3881 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
3882 if (!tok_type_(t
, TOK_ID
) && !tok_type_(t
, TOK_PREPROC_ID
))
3885 m
= is_mmacro(t
, ¶ms
);
3891 * We have an id which isn't a macro call. We'll assume
3892 * it might be a label; we'll also check to see if a
3893 * colon follows it. Then, if there's another id after
3894 * that lot, we'll check it again for macro-hood.
3898 if (tok_type_(t
, TOK_WHITESPACE
))
3899 last
= t
, t
= t
->next
;
3900 if (tok_is_(t
, ":")) {
3902 last
= t
, t
= t
->next
;
3903 if (tok_type_(t
, TOK_WHITESPACE
))
3904 last
= t
, t
= t
->next
;
3906 if (!tok_type_(t
, TOK_ID
) || (m
= is_mmacro(t
, ¶ms
)) == NULL
)
3914 * Fix up the parameters: this involves stripping leading and
3915 * trailing whitespace, then stripping braces if they are
3918 for (nparam
= 0; params
[nparam
]; nparam
++) ;
3919 paramlen
= nparam
? nasm_malloc(nparam
* sizeof(*paramlen
)) : NULL
;
3921 for (i
= 0; params
[i
]; i
++) {
3923 int comma
= (!m
->plus
|| i
< nparam
- 1);
3927 if (tok_is_(t
, "{"))
3928 t
= t
->next
, brace
= true, comma
= false;
3932 if (comma
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, ","))
3933 break; /* ... because we have hit a comma */
3934 if (comma
&& t
->type
== TOK_WHITESPACE
3935 && tok_is_(t
->next
, ","))
3936 break; /* ... or a space then a comma */
3937 if (brace
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, "}"))
3938 break; /* ... or a brace */
3945 * OK, we have a MMacro structure together with a set of
3946 * parameters. We must now go through the expansion and push
3947 * copies of each Line on to istk->expansion. Substitution of
3948 * parameter tokens and macro-local tokens doesn't get done
3949 * until the single-line macro substitution process; this is
3950 * because delaying them allows us to change the semantics
3951 * later through %rotate.
3953 * First, push an end marker on to istk->expansion, mark this
3954 * macro as in progress, and set up its invocation-specific
3957 ll
= nasm_malloc(sizeof(Line
));
3958 ll
->next
= istk
->expansion
;
3961 istk
->expansion
= ll
;
3963 m
->in_progress
= true;
3968 m
->paramlen
= paramlen
;
3969 m
->unique
= unique
++;
3972 m
->next_active
= istk
->mstk
;
3975 for (l
= m
->expansion
; l
; l
= l
->next
) {
3978 ll
= nasm_malloc(sizeof(Line
));
3979 ll
->finishes
= NULL
;
3980 ll
->next
= istk
->expansion
;
3981 istk
->expansion
= ll
;
3984 for (t
= l
->first
; t
; t
= t
->next
) {
3988 tt
= *tail
= new_Token(NULL
, TOK_ID
, mname
, 0);
3990 case TOK_PREPROC_QQ
:
3991 tt
= *tail
= new_Token(NULL
, TOK_ID
, m
->name
, 0);
3993 case TOK_PREPROC_ID
:
3994 if (t
->text
[1] == '0' && t
->text
[2] == '0') {
4002 tt
= *tail
= new_Token(NULL
, x
->type
, x
->text
, 0);
4011 * If we had a label, push it on as the first line of
4012 * the macro expansion.
4015 if (dont_prepend
< 0)
4016 free_tlist(startline
);
4018 ll
= nasm_malloc(sizeof(Line
));
4019 ll
->finishes
= NULL
;
4020 ll
->next
= istk
->expansion
;
4021 istk
->expansion
= ll
;
4022 ll
->first
= startline
;
4023 if (!dont_prepend
) {
4025 label
= label
->next
;
4026 label
->next
= tt
= new_Token(NULL
, TOK_OTHER
, ":", 0);
4031 list
->uplevel(m
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
4037 * Since preprocessor always operate only on the line that didn't
4038 * arrived yet, we should always use ERR_OFFBY1. Also since user
4039 * won't want to see same error twice (preprocessing is done once
4040 * per pass) we will want to show errors only during pass one.
4042 static void error(int severity
, const char *fmt
, ...)
4047 /* If we're in a dead branch of IF or something like it, ignore the error */
4048 if (istk
&& istk
->conds
&& !emitting(istk
->conds
->state
))
4052 vsnprintf(buff
, sizeof(buff
), fmt
, arg
);
4055 if (istk
&& istk
->mstk
&& istk
->mstk
->name
)
4056 _error(severity
| ERR_PASS1
, "(%s:%d) %s", istk
->mstk
->name
,
4057 istk
->mstk
->lineno
, buff
);
4059 _error(severity
| ERR_PASS1
, "%s", buff
);
4063 pp_reset(char *file
, int apass
, efunc errfunc
, evalfunc eval
,
4064 ListGen
* listgen
, StrList
**deplist
)
4068 istk
= nasm_malloc(sizeof(Include
));
4071 istk
->expansion
= NULL
;
4073 istk
->fp
= fopen(file
, "r");
4075 src_set_fname(nasm_strdup(file
));
4079 error(ERR_FATAL
| ERR_NOFILE
, "unable to open input file `%s'",
4084 if (tasm_compatible_mode
) {
4085 stdmacpos
= nasm_stdmac
;
4087 stdmacpos
= nasm_stdmac_after_tasm
;
4089 any_extrastdmac
= extrastdmac
&& *extrastdmac
;
4094 dephead
= deptail
= deplist
;
4096 StrList
*sl
= nasm_malloc(strlen(file
)+1+sizeof sl
->next
);
4098 strcpy(sl
->str
, file
);
4100 deptail
= &sl
->next
;
4104 static char *pp_getline(void)
4111 * Fetch a tokenized line, either from the macro-expansion
4112 * buffer or from the input file.
4115 while (istk
->expansion
&& istk
->expansion
->finishes
) {
4116 Line
*l
= istk
->expansion
;
4117 if (!l
->finishes
->name
&& l
->finishes
->in_progress
> 1) {
4121 * This is a macro-end marker for a macro with no
4122 * name, which means it's not really a macro at all
4123 * but a %rep block, and the `in_progress' field is
4124 * more than 1, meaning that we still need to
4125 * repeat. (1 means the natural last repetition; 0
4126 * means termination by %exitrep.) We have
4127 * therefore expanded up to the %endrep, and must
4128 * push the whole block on to the expansion buffer
4129 * again. We don't bother to remove the macro-end
4130 * marker: we'd only have to generate another one
4133 l
->finishes
->in_progress
--;
4134 for (l
= l
->finishes
->expansion
; l
; l
= l
->next
) {
4135 Token
*t
, *tt
, **tail
;
4137 ll
= nasm_malloc(sizeof(Line
));
4138 ll
->next
= istk
->expansion
;
4139 ll
->finishes
= NULL
;
4143 for (t
= l
->first
; t
; t
= t
->next
) {
4144 if (t
->text
|| t
->type
== TOK_WHITESPACE
) {
4146 new_Token(NULL
, t
->type
, t
->text
, 0);
4151 istk
->expansion
= ll
;
4155 * Check whether a `%rep' was started and not ended
4156 * within this macro expansion. This can happen and
4157 * should be detected. It's a fatal error because
4158 * I'm too confused to work out how to recover
4164 "defining with name in expansion");
4165 else if (istk
->mstk
->name
)
4167 "`%%rep' without `%%endrep' within"
4168 " expansion of macro `%s'",
4173 * FIXME: investigate the relationship at this point between
4174 * istk->mstk and l->finishes
4177 MMacro
*m
= istk
->mstk
;
4178 istk
->mstk
= m
->next_active
;
4181 * This was a real macro call, not a %rep, and
4182 * therefore the parameter information needs to
4185 nasm_free(m
->params
);
4186 free_tlist(m
->iline
);
4187 nasm_free(m
->paramlen
);
4188 l
->finishes
->in_progress
= false;
4192 istk
->expansion
= l
->next
;
4194 list
->downlevel(LIST_MACRO
);
4197 while (1) { /* until we get a line we can use */
4199 if (istk
->expansion
) { /* from a macro expansion */
4201 Line
*l
= istk
->expansion
;
4203 istk
->mstk
->lineno
++;
4205 istk
->expansion
= l
->next
;
4207 p
= detoken(tline
, false);
4208 list
->line(LIST_MACRO
, p
);
4213 if (line
) { /* from the current input file */
4214 line
= prepreproc(line
);
4215 tline
= tokenize(line
);
4220 * The current file has ended; work down the istk
4227 "expected `%%endif' before end of file");
4228 /* only set line and file name if there's a next node */
4230 src_set_linnum(i
->lineno
);
4231 nasm_free(src_set_fname(i
->fname
));
4234 list
->downlevel(LIST_INCLUDE
);
4242 * We must expand MMacro parameters and MMacro-local labels
4243 * _before_ we plunge into directive processing, to cope
4244 * with things like `%define something %1' such as STRUC
4245 * uses. Unless we're _defining_ a MMacro, in which case
4246 * those tokens should be left alone to go into the
4247 * definition; and unless we're in a non-emitting
4248 * condition, in which case we don't want to meddle with
4251 if (!defining
&& !(istk
->conds
&& !emitting(istk
->conds
->state
)))
4252 tline
= expand_mmac_params(tline
);
4255 * Check the line to see if it's a preprocessor directive.
4257 if (do_directive(tline
) == DIRECTIVE_FOUND
) {
4259 } else if (defining
) {
4261 * We're defining a multi-line macro. We emit nothing
4263 * shove the tokenized line on to the macro definition.
4265 Line
*l
= nasm_malloc(sizeof(Line
));
4266 l
->next
= defining
->expansion
;
4269 defining
->expansion
= l
;
4271 } else if (istk
->conds
&& !emitting(istk
->conds
->state
)) {
4273 * We're in a non-emitting branch of a condition block.
4274 * Emit nothing at all, not even a blank line: when we
4275 * emerge from the condition we'll give a line-number
4276 * directive so we keep our place correctly.
4280 } else if (istk
->mstk
&& !istk
->mstk
->in_progress
) {
4282 * We're in a %rep block which has been terminated, so
4283 * we're walking through to the %endrep without
4284 * emitting anything. Emit nothing at all, not even a
4285 * blank line: when we emerge from the %rep block we'll
4286 * give a line-number directive so we keep our place
4292 tline
= expand_smacro(tline
);
4293 if (!expand_mmacro(tline
)) {
4295 * De-tokenize the line again, and emit it.
4297 line
= detoken(tline
, true);
4301 continue; /* expand_mmacro calls free_tlist */
4309 static void pp_cleanup(int pass
)
4312 error(ERR_NONFATAL
, "end of file while still defining macro `%s'",
4314 free_mmacro(defining
);
4323 nasm_free(i
->fname
);
4328 nasm_free(src_set_fname(NULL
));
4333 while ((i
= ipath
)) {
4342 void pp_include_path(char *path
)
4346 i
= nasm_malloc(sizeof(IncPath
));
4347 i
->path
= path
? nasm_strdup(path
) : NULL
;
4350 if (ipath
!= NULL
) {
4352 while (j
->next
!= NULL
)
4360 void pp_pre_include(char *fname
)
4362 Token
*inc
, *space
, *name
;
4365 name
= new_Token(NULL
, TOK_INTERNAL_STRING
, fname
, 0);
4366 space
= new_Token(name
, TOK_WHITESPACE
, NULL
, 0);
4367 inc
= new_Token(space
, TOK_PREPROC_ID
, "%include", 0);
4369 l
= nasm_malloc(sizeof(Line
));
4376 void pp_pre_define(char *definition
)
4382 equals
= strchr(definition
, '=');
4383 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
4384 def
= new_Token(space
, TOK_PREPROC_ID
, "%define", 0);
4387 space
->next
= tokenize(definition
);
4391 l
= nasm_malloc(sizeof(Line
));
4398 void pp_pre_undefine(char *definition
)
4403 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
4404 def
= new_Token(space
, TOK_PREPROC_ID
, "%undef", 0);
4405 space
->next
= tokenize(definition
);
4407 l
= nasm_malloc(sizeof(Line
));
4415 * Added by Keith Kanios:
4417 * This function is used to assist with "runtime" preprocessor
4418 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4420 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4421 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4424 void pp_runtime(char *definition
)
4428 def
= tokenize(definition
);
4429 if(do_directive(def
) == NO_DIRECTIVE_FOUND
)
4434 void pp_extra_stdmac(const macros_t
*macros
)
4436 extrastdmac
= macros
;
4439 static void make_tok_num(Token
* tok
, int64_t val
)
4442 snprintf(numbuf
, sizeof(numbuf
), "%"PRId64
"", val
);
4443 tok
->text
= nasm_strdup(numbuf
);
4444 tok
->type
= TOK_NUMBER
;