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
55 typedef struct SMacro SMacro
;
56 typedef struct MMacro MMacro
;
57 typedef struct Context Context
;
58 typedef struct Token Token
;
59 typedef struct Blocks Blocks
;
60 typedef struct Line Line
;
61 typedef struct Include Include
;
62 typedef struct Cond Cond
;
63 typedef struct IncPath IncPath
;
66 * Note on the storage of both SMacro and MMacros: the hash table
67 * indexes them case-insensitively, and we then have to go through a
68 * linked list of potential case aliases (and, for MMacros, parameter
69 * ranges); this is to preserve the matching semantics of the earlier
70 * code. If the number of case aliases for a specific macro is a
71 * performance issue, you may want to reconsider your coding style.
75 * Store the definition of a single-line macro.
87 * Store the definition of a multi-line macro. This is also used to
88 * store the interiors of `%rep...%endrep' blocks, which are
89 * effectively self-re-invoking multi-line macros which simply
90 * don't have a name or bother to appear in the hash tables. %rep
91 * blocks are signified by having a NULL `name' field.
93 * In a MMacro describing a `%rep' block, the `in_progress' field
94 * isn't merely boolean, but gives the number of repeats left to
97 * The `next' field is used for storing MMacros in hash tables; the
98 * `next_active' field is for stacking them on istk entries.
100 * When a MMacro is being expanded, `params', `iline', `nparam',
101 * `paramlen', `rotate' and `unique' are local to the invocation.
106 int nparam_min
, nparam_max
;
108 bool plus
; /* is the last parameter greedy? */
109 bool nolist
; /* is this macro listing-inhibited? */
111 Token
*dlist
; /* All defaults as one list */
112 Token
**defaults
; /* Parameter default pointers */
113 int ndefs
; /* number of default parameters */
117 MMacro
*rep_nest
; /* used for nesting %rep */
118 Token
**params
; /* actual parameters */
119 Token
*iline
; /* invocation line */
120 unsigned int nparam
, rotate
;
123 int lineno
; /* Current line number on expansion */
127 * The context stack is composed of a linked list of these.
132 struct hash_table localmac
;
137 * This is the internal form which we break input lines up into.
138 * Typically stored in linked lists.
140 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
141 * necessarily used as-is, but is intended to denote the number of
142 * the substituted parameter. So in the definition
144 * %define a(x,y) ( (x) & ~(y) )
146 * the token representing `x' will have its type changed to
147 * TOK_SMAC_PARAM, but the one representing `y' will be
150 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
151 * which doesn't need quotes around it. Used in the pre-include
152 * mechanism as an alternative to trying to find a sensible type of
153 * quote to use on the filename we were passed.
156 TOK_NONE
= 0, TOK_WHITESPACE
, TOK_COMMENT
, TOK_ID
,
157 TOK_PREPROC_ID
, TOK_STRING
,
158 TOK_NUMBER
, TOK_FLOAT
, TOK_SMAC_END
, TOK_OTHER
,
160 TOK_PREPROC_Q
, TOK_PREPROC_QQ
,
161 TOK_SMAC_PARAM
, /* MUST BE LAST IN THE LIST!!! */
162 TOK_MAX
= INT_MAX
/* Keep compiler from reducing the range */
168 SMacro
*mac
; /* associated macro for TOK_SMAC_END */
169 enum pp_token_type type
;
173 * Multi-line macro definitions are stored as a linked list of
174 * these, which is essentially a container to allow several linked
177 * Note that in this module, linked lists are treated as stacks
178 * wherever possible. For this reason, Lines are _pushed_ on to the
179 * `expansion' field in MMacro structures, so that the linked list,
180 * if walked, would give the macro lines in reverse order; this
181 * means that we can walk the list when expanding a macro, and thus
182 * push the lines on to the `expansion' field in _istk_ in reverse
183 * order (so that when popped back off they are in the right
184 * order). It may seem cockeyed, and it relies on my design having
185 * an even number of steps in, but it works...
187 * Some of these structures, rather than being actual lines, are
188 * markers delimiting the end of the expansion of a given macro.
189 * This is for use in the cycle-tracking and %rep-handling code.
190 * Such structures have `finishes' non-NULL, and `first' NULL. All
191 * others have `finishes' NULL, but `first' may still be NULL if
201 * To handle an arbitrary level of file inclusion, we maintain a
202 * stack (ie linked list) of these things.
211 MMacro
*mstk
; /* stack of active macros/reps */
215 * Include search path. This is simply a list of strings which get
216 * prepended, in turn, to the name of an include file, in an
217 * attempt to find the file if it's not in the current directory.
225 * Conditional assembly: we maintain a separate stack of these for
226 * each level of file inclusion. (The only reason we keep the
227 * stacks separate is to ensure that a stray `%endif' in a file
228 * included from within the true branch of a `%if' won't terminate
229 * it and cause confusion: instead, rightly, it'll cause an error.)
237 * These states are for use just after %if or %elif: IF_TRUE
238 * means the condition has evaluated to truth so we are
239 * currently emitting, whereas IF_FALSE means we are not
240 * currently emitting but will start doing so if a %else comes
241 * up. In these states, all directives are admissible: %elif,
242 * %else and %endif. (And of course %if.)
244 COND_IF_TRUE
, COND_IF_FALSE
,
246 * These states come up after a %else: ELSE_TRUE means we're
247 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
248 * any %elif or %else will cause an error.
250 COND_ELSE_TRUE
, COND_ELSE_FALSE
,
252 * This state means that we're not emitting now, and also that
253 * nothing until %endif will be emitted at all. It's for use in
254 * two circumstances: (i) when we've had our moment of emission
255 * and have now started seeing %elifs, and (ii) when the
256 * condition construct in question is contained within a
257 * non-emitting branch of a larger condition construct.
261 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
264 * These defines are used as the possible return values for do_directive
266 #define NO_DIRECTIVE_FOUND 0
267 #define DIRECTIVE_FOUND 1
270 * Condition codes. Note that we use c_ prefix not C_ because C_ is
271 * used in nasm.h for the "real" condition codes. At _this_ level,
272 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
273 * ones, so we need a different enum...
275 static const char * const conditions
[] = {
276 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
277 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
278 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
281 c_A
, c_AE
, c_B
, c_BE
, c_C
, c_CXZ
, c_E
, c_ECXZ
, c_G
, c_GE
, c_L
, c_LE
,
282 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, c_NE
, c_NG
, c_NGE
, c_NL
, c_NLE
, c_NO
,
283 c_NP
, c_NS
, c_NZ
, c_O
, c_P
, c_PE
, c_PO
, c_RCXZ
, c_S
, c_Z
,
286 static const enum pp_conds inverse_ccs
[] = {
287 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, -1, c_NE
, -1, c_NG
, c_NGE
, c_NL
, c_NLE
,
288 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
,
289 c_Z
, c_NO
, c_NP
, c_PO
, c_PE
, -1, c_NS
, c_NZ
295 /* If this is a an IF, ELIF, ELSE or ENDIF keyword */
296 static int is_condition(enum preproc_token arg
)
298 return PP_IS_COND(arg
) || (arg
== PP_ELSE
) || (arg
== PP_ENDIF
);
301 /* For TASM compatibility we need to be able to recognise TASM compatible
302 * conditional compilation directives. Using the NASM pre-processor does
303 * not work, so we look for them specifically from the following list and
304 * then jam in the equivalent NASM directive into the input stream.
308 TM_ARG
, TM_ELIF
, TM_ELSE
, TM_ENDIF
, TM_IF
, TM_IFDEF
, TM_IFDIFI
,
309 TM_IFNDEF
, TM_INCLUDE
, TM_LOCAL
312 static const char * const tasm_directives
[] = {
313 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
314 "ifndef", "include", "local"
317 static int StackSize
= 4;
318 static char *StackPointer
= "ebp";
319 static int ArgOffset
= 8;
320 static int LocalOffset
= 0;
322 static Context
*cstk
;
323 static Include
*istk
;
324 static IncPath
*ipath
= NULL
;
326 static efunc _error
; /* Pointer to client-provided error reporting function */
327 static evalfunc evaluate
;
329 static int pass
; /* HACK: pass 0 = generate dependencies only */
330 static StrList
**dephead
, **deptail
; /* Dependency list */
332 static uint64_t unique
; /* unique identifier numbers */
334 static Line
*predef
= NULL
;
336 static ListGen
*list
;
339 * The current set of multi-line macros we have defined.
341 static struct hash_table mmacros
;
344 * The current set of single-line macros we have defined.
346 static struct hash_table smacros
;
349 * The multi-line macro we are currently defining, or the %rep
350 * block we are currently reading, if any.
352 static MMacro
*defining
;
355 * The number of macro parameters to allocate space for at a time.
357 #define PARAM_DELTA 16
360 * The standard macro set: defined in macros.c in the array nasm_stdmac.
361 * This gives our position in the macro set, when we're processing it.
363 static const char * const *stdmacpos
;
366 * The extra standard macros that come from the object format, if
369 static const char * const *extrastdmac
= NULL
;
370 bool any_extrastdmac
;
373 * Tokens are allocated in blocks to improve speed
375 #define TOKEN_BLOCKSIZE 4096
376 static Token
*freeTokens
= NULL
;
382 static Blocks blocks
= { NULL
, NULL
};
385 * Forward declarations.
387 static Token
*expand_mmac_params(Token
* tline
);
388 static Token
*expand_smacro(Token
* tline
);
389 static Token
*expand_id(Token
* tline
);
390 static Context
*get_ctx(char *name
, bool all_contexts
);
391 static void make_tok_num(Token
* tok
, int64_t val
);
392 static void error(int severity
, const char *fmt
, ...);
393 static void *new_Block(size_t size
);
394 static void delete_Blocks(void);
395 static Token
*new_Token(Token
* next
, enum pp_token_type type
, char *text
, int txtlen
);
396 static Token
*delete_Token(Token
* t
);
399 * Macros for safe checking of token pointers, avoid *(NULL)
401 #define tok_type_(x,t) ((x) && (x)->type == (t))
402 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
403 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
404 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
406 /* Handle TASM specific directives, which do not contain a % in
407 * front of them. We do it here because I could not find any other
408 * place to do it for the moment, and it is a hack (ideally it would
409 * be nice to be able to use the NASM pre-processor to do it).
411 static char *check_tasm_directive(char *line
)
413 int32_t i
, j
, k
, m
, len
;
414 char *p
= line
, *oldline
, oldchar
;
416 /* Skip whitespace */
417 while (isspace(*p
) && *p
!= 0)
420 /* Binary search for the directive name */
422 j
= elements(tasm_directives
);
424 while (!isspace(p
[len
]) && p
[len
] != 0)
431 m
= nasm_stricmp(p
, tasm_directives
[k
]);
433 /* We have found a directive, so jam a % in front of it
434 * so that NASM will then recognise it as one if it's own.
439 line
= nasm_malloc(len
+ 2);
441 if (k
== TM_IFDIFI
) {
442 /* NASM does not recognise IFDIFI, so we convert it to
443 * %ifdef BOGUS. This is not used in NASM comaptible
444 * code, but does need to parse for the TASM macro
447 strcpy(line
+ 1, "ifdef BOGUS");
449 memcpy(line
+ 1, p
, len
+ 1);
464 * The pre-preprocessing stage... This function translates line
465 * number indications as they emerge from GNU cpp (`# lineno "file"
466 * flags') into NASM preprocessor line number indications (`%line
469 static char *prepreproc(char *line
)
472 char *fname
, *oldline
;
474 if (line
[0] == '#' && line
[1] == ' ') {
477 lineno
= atoi(fname
);
478 fname
+= strspn(fname
, "0123456789 ");
481 fnlen
= strcspn(fname
, "\"");
482 line
= nasm_malloc(20 + fnlen
);
483 snprintf(line
, 20 + fnlen
, "%%line %d %.*s", lineno
, fnlen
, fname
);
486 if (tasm_compatible_mode
)
487 return check_tasm_directive(line
);
492 * Free a linked list of tokens.
494 static void free_tlist(Token
* list
)
497 list
= delete_Token(list
);
502 * Free a linked list of lines.
504 static void free_llist(Line
* list
)
510 free_tlist(l
->first
);
518 static void free_mmacro(MMacro
* m
)
521 free_tlist(m
->dlist
);
522 nasm_free(m
->defaults
);
523 free_llist(m
->expansion
);
528 * Free all currently defined macros, and free the hash tables
530 static void free_smacro_table(struct hash_table
*smt
)
534 struct hash_tbl_node
*it
= NULL
;
536 while ((s
= hash_iterate(smt
, &it
, &key
)) != NULL
) {
537 nasm_free((void *)key
);
539 SMacro
*ns
= s
->next
;
541 free_tlist(s
->expansion
);
549 static void free_mmacro_table(struct hash_table
*mmt
)
553 struct hash_tbl_node
*it
= NULL
;
556 while ((m
= hash_iterate(mmt
, &it
, &key
)) != NULL
) {
557 nasm_free((void *)key
);
559 MMacro
*nm
= m
->next
;
567 static void free_macros(void)
569 free_smacro_table(&smacros
);
570 free_mmacro_table(&mmacros
);
574 * Initialize the hash tables
576 static void init_macros(void)
578 hash_init(&smacros
, HASH_LARGE
);
579 hash_init(&mmacros
, HASH_LARGE
);
583 * Pop the context stack.
585 static void ctx_pop(void)
590 free_smacro_table(&c
->localmac
);
596 * Search for a key in the hash index; adding it if necessary
597 * (in which case we initialize the data pointer to NULL.)
600 hash_findi_add(struct hash_table
*hash
, const char *str
)
602 struct hash_insert hi
;
606 r
= hash_findi(hash
, str
, &hi
);
610 strx
= nasm_strdup(str
); /* Use a more efficient allocator here? */
611 return hash_add(&hi
, strx
, NULL
);
615 * Like hash_findi, but returns the data element rather than a pointer
616 * to it. Used only when not adding a new element, hence no third
620 hash_findix(struct hash_table
*hash
, const char *str
)
624 p
= hash_findi(hash
, str
, NULL
);
625 return p
? *p
: NULL
;
628 #define BUF_DELTA 512
630 * Read a line from the top file in istk, handling multiple CR/LFs
631 * at the end of the line read, and handling spurious ^Zs. Will
632 * return lines from the standard macro set if this has not already
635 static char *read_line(void)
637 char *buffer
, *p
, *q
;
638 int bufsize
, continued_count
;
642 char *ret
= nasm_strdup(*stdmacpos
++);
643 if (!*stdmacpos
&& any_extrastdmac
) {
644 stdmacpos
= extrastdmac
;
645 any_extrastdmac
= false;
649 * Nasty hack: here we push the contents of `predef' on
650 * to the top-level expansion stack, since this is the
651 * most convenient way to implement the pre-include and
652 * pre-define features.
656 Token
*head
, **tail
, *t
;
658 for (pd
= predef
; pd
; pd
= pd
->next
) {
661 for (t
= pd
->first
; t
; t
= t
->next
) {
662 *tail
= new_Token(NULL
, t
->type
, t
->text
, 0);
663 tail
= &(*tail
)->next
;
665 l
= nasm_malloc(sizeof(Line
));
666 l
->next
= istk
->expansion
;
679 buffer
= nasm_malloc(BUF_DELTA
);
683 q
= fgets(p
, bufsize
- (p
- buffer
), istk
->fp
);
687 if (p
> buffer
&& p
[-1] == '\n') {
688 /* Convert backslash-CRLF line continuation sequences into
689 nothing at all (for DOS and Windows) */
690 if (((p
- 2) > buffer
) && (p
[-3] == '\\') && (p
[-2] == '\r')) {
695 /* Also convert backslash-LF line continuation sequences into
696 nothing at all (for Unix) */
697 else if (((p
- 1) > buffer
) && (p
[-2] == '\\')) {
705 if (p
- buffer
> bufsize
- 10) {
706 int32_t offset
= p
- buffer
;
707 bufsize
+= BUF_DELTA
;
708 buffer
= nasm_realloc(buffer
, bufsize
);
709 p
= buffer
+ offset
; /* prevent stale-pointer problems */
713 if (!q
&& p
== buffer
) {
718 src_set_linnum(src_get_linnum() + istk
->lineinc
+
719 (continued_count
* istk
->lineinc
));
722 * Play safe: remove CRs as well as LFs, if any of either are
723 * present at the end of the line.
725 while (--p
>= buffer
&& (*p
== '\n' || *p
== '\r'))
729 * Handle spurious ^Z, which may be inserted into source files
730 * by some file transfer utilities.
732 buffer
[strcspn(buffer
, "\032")] = '\0';
734 list
->line(LIST_READ
, buffer
);
740 * Tokenize a line of text. This is a very simple process since we
741 * don't need to parse the value out of e.g. numeric tokens: we
742 * simply split one string into many.
744 static Token
*tokenize(char *line
)
747 enum pp_token_type type
;
749 Token
*t
, **tail
= &list
;
756 ((*p
== '-' || *p
== '+') && isdigit(p
[1])) ||
757 ((*p
== '+') && (isspace(p
[1]) || !p
[1]))) {
762 type
= TOK_PREPROC_ID
;
763 } else if (*p
== '{') {
765 while (*p
&& *p
!= '}') {
772 type
= TOK_PREPROC_ID
;
773 } else if (*p
== '?') {
774 type
= TOK_PREPROC_Q
; /* %? */
777 type
= TOK_PREPROC_QQ
; /* %?? */
780 } else if (isidchar(*p
) ||
781 ((*p
== '!' || *p
== '%' || *p
== '$') &&
786 while (isidchar(*p
));
787 type
= TOK_PREPROC_ID
;
793 } else if (isidstart(*p
) || (*p
== '$' && isidstart(p
[1]))) {
796 while (*p
&& isidchar(*p
))
798 } else if (*p
== '\'' || *p
== '"') {
805 while (*p
&& *p
!= c
)
811 error(ERR_WARNING
, "unterminated string");
812 /* Handling unterminated strings by UNV */
815 } else if (isnumstart(*p
)) {
817 bool is_float
= false;
833 if (!is_hex
&& (c
== 'e' || c
== 'E')) {
835 if (*p
== '+' || *p
== '-') {
836 /* e can only be followed by +/- if it is either a
837 prefixed hex number or a floating-point number */
841 } else if (c
== 'H' || c
== 'h' || c
== 'X' || c
== 'x') {
843 } else if (c
== 'P' || c
== 'p') {
845 if (*p
== '+' || *p
== '-')
847 } else if (isnumchar(c
) || c
== '_')
850 /* we need to deal with consequences of the legacy
851 parser, like "1.nolist" being two tokens
852 (TOK_NUMBER, TOK_ID) here; at least give it
853 a shot for now. In the future, we probably need
854 a flex-based scanner with proper pattern matching
855 to do it as well as it can be done. Nothing in
856 the world is going to help the person who wants
857 0x123.p16 interpreted as two tokens, though. */
862 if (isdigit(*r
) || (is_hex
&& isxdigit(*r
)) ||
863 (!is_hex
&& (*r
== 'e' || *r
== 'E')) ||
864 (*r
== 'p' || *r
== 'P')) {
868 break; /* Terminate the token */
872 p
--; /* Point to first character beyond number */
874 if (has_e
&& !is_hex
) {
875 /* 1e13 is floating-point, but 1e13h is not */
879 type
= is_float
? TOK_FLOAT
: TOK_NUMBER
;
880 } else if (isspace(*p
)) {
881 type
= TOK_WHITESPACE
;
883 while (*p
&& isspace(*p
))
886 * Whitespace just before end-of-line is discarded by
887 * pretending it's a comment; whitespace just before a
888 * comment gets lumped into the comment.
890 if (!*p
|| *p
== ';') {
895 } else if (*p
== ';') {
901 * Anything else is an operator of some kind. We check
902 * for all the double-character operators (>>, <<, //,
903 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
904 * else is a single-character operator.
907 if ((p
[0] == '>' && p
[1] == '>') ||
908 (p
[0] == '<' && p
[1] == '<') ||
909 (p
[0] == '/' && p
[1] == '/') ||
910 (p
[0] == '<' && p
[1] == '=') ||
911 (p
[0] == '>' && p
[1] == '=') ||
912 (p
[0] == '=' && p
[1] == '=') ||
913 (p
[0] == '!' && p
[1] == '=') ||
914 (p
[0] == '<' && p
[1] == '>') ||
915 (p
[0] == '&' && p
[1] == '&') ||
916 (p
[0] == '|' && p
[1] == '|') ||
917 (p
[0] == '^' && p
[1] == '^')) {
923 /* Handling unterminated string by UNV */
926 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
927 t->text[p-line] = *line;
931 if (type
!= TOK_COMMENT
) {
932 *tail
= t
= new_Token(NULL
, type
, line
, p
- line
);
941 * this function allocates a new managed block of memory and
942 * returns a pointer to the block. The managed blocks are
943 * deleted only all at once by the delete_Blocks function.
945 static void *new_Block(size_t size
)
949 /* first, get to the end of the linked list */
952 /* now allocate the requested chunk */
953 b
->chunk
= nasm_malloc(size
);
955 /* now allocate a new block for the next request */
956 b
->next
= nasm_malloc(sizeof(Blocks
));
957 /* and initialize the contents of the new block */
958 b
->next
->next
= NULL
;
959 b
->next
->chunk
= NULL
;
964 * this function deletes all managed blocks of memory
966 static void delete_Blocks(void)
968 Blocks
*a
, *b
= &blocks
;
971 * keep in mind that the first block, pointed to by blocks
972 * is a static and not dynamically allocated, so we don't
986 * this function creates a new Token and passes a pointer to it
987 * back to the caller. It sets the type and text elements, and
988 * also the mac and next elements to NULL.
990 static Token
*new_Token(Token
* next
, enum pp_token_type type
, char *text
, int txtlen
)
995 if (freeTokens
== NULL
) {
996 freeTokens
= (Token
*) new_Block(TOKEN_BLOCKSIZE
* sizeof(Token
));
997 for (i
= 0; i
< TOKEN_BLOCKSIZE
- 1; i
++)
998 freeTokens
[i
].next
= &freeTokens
[i
+ 1];
999 freeTokens
[i
].next
= NULL
;
1002 freeTokens
= t
->next
;
1006 if (type
== TOK_WHITESPACE
|| text
== NULL
) {
1010 txtlen
= strlen(text
);
1011 t
->text
= nasm_malloc(1 + txtlen
);
1012 strncpy(t
->text
, text
, txtlen
);
1013 t
->text
[txtlen
] = '\0';
1018 static Token
*delete_Token(Token
* t
)
1020 Token
*next
= t
->next
;
1022 t
->next
= freeTokens
;
1028 * Convert a line of tokens back into text.
1029 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1030 * will be transformed into ..@ctxnum.xxx
1032 static char *detoken(Token
* tlist
, int expand_locals
)
1040 for (t
= tlist
; t
; t
= t
->next
) {
1041 if (t
->type
== TOK_PREPROC_ID
&& t
->text
[1] == '!') {
1042 char *p
= getenv(t
->text
+ 2);
1045 t
->text
= nasm_strdup(p
);
1049 /* Expand local macros here and not during preprocessing */
1050 if (expand_locals
&&
1051 t
->type
== TOK_PREPROC_ID
&& t
->text
&&
1052 t
->text
[0] == '%' && t
->text
[1] == '$') {
1053 Context
*ctx
= get_ctx(t
->text
, false);
1056 char *p
, *q
= t
->text
+ 2;
1058 q
+= strspn(q
, "$");
1059 snprintf(buffer
, sizeof(buffer
), "..@%"PRIu32
".", ctx
->number
);
1060 p
= nasm_strcat(buffer
, q
);
1065 if (t
->type
== TOK_WHITESPACE
) {
1067 } else if (t
->text
) {
1068 len
+= strlen(t
->text
);
1071 p
= line
= nasm_malloc(len
+ 1);
1072 for (t
= tlist
; t
; t
= t
->next
) {
1073 if (t
->type
== TOK_WHITESPACE
) {
1075 } else if (t
->text
) {
1086 * A scanner, suitable for use by the expression evaluator, which
1087 * operates on a line of Tokens. Expects a pointer to a pointer to
1088 * the first token in the line to be passed in as its private_data
1091 * FIX: This really needs to be unified with stdscan.
1093 static int ppscan(void *private_data
, struct tokenval
*tokval
)
1095 Token
**tlineptr
= private_data
;
1097 char ourcopy
[MAX_KEYWORD
+1], *p
, *r
, *s
;
1101 *tlineptr
= tline
? tline
->next
: NULL
;
1103 while (tline
&& (tline
->type
== TOK_WHITESPACE
||
1104 tline
->type
== TOK_COMMENT
));
1107 return tokval
->t_type
= TOKEN_EOS
;
1109 tokval
->t_charptr
= tline
->text
;
1111 if (tline
->text
[0] == '$' && !tline
->text
[1])
1112 return tokval
->t_type
= TOKEN_HERE
;
1113 if (tline
->text
[0] == '$' && tline
->text
[1] == '$' && !tline
->text
[2])
1114 return tokval
->t_type
= TOKEN_BASE
;
1116 if (tline
->type
== TOK_ID
) {
1117 p
= tokval
->t_charptr
= tline
->text
;
1119 tokval
->t_charptr
++;
1120 return tokval
->t_type
= TOKEN_ID
;
1123 for (r
= p
, s
= ourcopy
; *r
; r
++) {
1124 if (r
>= p
+MAX_KEYWORD
)
1125 return tokval
->t_type
= TOKEN_ID
; /* Not a keyword */
1129 /* right, so we have an identifier sitting in temp storage. now,
1130 * is it actually a register or instruction name, or what? */
1131 return nasm_token_hash(ourcopy
, tokval
);
1134 if (tline
->type
== TOK_NUMBER
) {
1136 tokval
->t_integer
= readnum(tline
->text
, &rn_error
);
1138 return tokval
->t_type
= TOKEN_ERRNUM
; /* some malformation occurred */
1139 tokval
->t_charptr
= tline
->text
;
1140 return tokval
->t_type
= TOKEN_NUM
;
1143 if (tline
->type
== TOK_FLOAT
) {
1144 return tokval
->t_type
= TOKEN_FLOAT
;
1147 if (tline
->type
== TOK_STRING
) {
1156 if (l
== 0 || r
[l
- 1] != q
)
1157 return tokval
->t_type
= TOKEN_ERRNUM
;
1158 tokval
->t_integer
= readstrnum(r
, l
- 1, &rn_warn
);
1160 error(ERR_WARNING
| ERR_PASS1
, "character constant too long");
1161 tokval
->t_charptr
= NULL
;
1162 return tokval
->t_type
= TOKEN_NUM
;
1165 if (tline
->type
== TOK_OTHER
) {
1166 if (!strcmp(tline
->text
, "<<"))
1167 return tokval
->t_type
= TOKEN_SHL
;
1168 if (!strcmp(tline
->text
, ">>"))
1169 return tokval
->t_type
= TOKEN_SHR
;
1170 if (!strcmp(tline
->text
, "//"))
1171 return tokval
->t_type
= TOKEN_SDIV
;
1172 if (!strcmp(tline
->text
, "%%"))
1173 return tokval
->t_type
= TOKEN_SMOD
;
1174 if (!strcmp(tline
->text
, "=="))
1175 return tokval
->t_type
= TOKEN_EQ
;
1176 if (!strcmp(tline
->text
, "<>"))
1177 return tokval
->t_type
= TOKEN_NE
;
1178 if (!strcmp(tline
->text
, "!="))
1179 return tokval
->t_type
= TOKEN_NE
;
1180 if (!strcmp(tline
->text
, "<="))
1181 return tokval
->t_type
= TOKEN_LE
;
1182 if (!strcmp(tline
->text
, ">="))
1183 return tokval
->t_type
= TOKEN_GE
;
1184 if (!strcmp(tline
->text
, "&&"))
1185 return tokval
->t_type
= TOKEN_DBL_AND
;
1186 if (!strcmp(tline
->text
, "^^"))
1187 return tokval
->t_type
= TOKEN_DBL_XOR
;
1188 if (!strcmp(tline
->text
, "||"))
1189 return tokval
->t_type
= TOKEN_DBL_OR
;
1193 * We have no other options: just return the first character of
1196 return tokval
->t_type
= tline
->text
[0];
1200 * Compare a string to the name of an existing macro; this is a
1201 * simple wrapper which calls either strcmp or nasm_stricmp
1202 * depending on the value of the `casesense' parameter.
1204 static int mstrcmp(const char *p
, const char *q
, bool casesense
)
1206 return casesense
? strcmp(p
, q
) : nasm_stricmp(p
, q
);
1210 * Return the Context structure associated with a %$ token. Return
1211 * NULL, having _already_ reported an error condition, if the
1212 * context stack isn't deep enough for the supplied number of $
1214 * If all_contexts == true, contexts that enclose current are
1215 * also scanned for such smacro, until it is found; if not -
1216 * only the context that directly results from the number of $'s
1217 * in variable's name.
1219 static Context
*get_ctx(char *name
, bool all_contexts
)
1225 if (!name
|| name
[0] != '%' || name
[1] != '$')
1229 error(ERR_NONFATAL
, "`%s': context stack is empty", name
);
1233 for (i
= strspn(name
+ 2, "$"), ctx
= cstk
; (i
> 0) && ctx
; i
--) {
1235 /* i--; Lino - 02/25/02 */
1238 error(ERR_NONFATAL
, "`%s': context stack is only"
1239 " %d level%s deep", name
, i
- 1, (i
== 2 ? "" : "s"));
1246 /* Search for this smacro in found context */
1247 m
= hash_findix(&ctx
->localmac
, name
);
1249 if (!mstrcmp(m
->name
, name
, m
->casesense
))
1260 * Check to see if a file is already in a string list
1262 static bool in_list(const StrList
*list
, const char *str
)
1265 if (!strcmp(list
->str
, str
))
1273 * Open an include file. This routine must always return a valid
1274 * file pointer if it returns - it's responsible for throwing an
1275 * ERR_FATAL and bombing out completely if not. It should also try
1276 * the include path one by one until it finds the file or reaches
1277 * the end of the path.
1279 static FILE *inc_fopen(const char *file
, StrList
**dhead
, StrList
**dtail
,
1284 IncPath
*ip
= ipath
;
1285 int len
= strlen(file
);
1286 size_t prefix_len
= 0;
1290 sl
= nasm_malloc(prefix_len
+len
+1+sizeof sl
->next
);
1291 memcpy(sl
->str
, prefix
, prefix_len
);
1292 memcpy(sl
->str
+prefix_len
, file
, len
+1);
1293 fp
= fopen(sl
->str
, "r");
1294 if (fp
&& dhead
&& !in_list(*dhead
, sl
->str
)) {
1312 prefix_len
= strlen(prefix
);
1314 /* -MG given and file not found */
1315 if (dhead
&& !in_list(*dhead
, file
)) {
1316 sl
= nasm_malloc(len
+1+sizeof sl
->next
);
1318 strcpy(sl
->str
, file
);
1326 error(ERR_FATAL
, "unable to open include file `%s'", file
);
1327 return NULL
; /* never reached - placate compilers */
1331 * Determine if we should warn on defining a single-line macro of
1332 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1333 * return true if _any_ single-line macro of that name is defined.
1334 * Otherwise, will return true if a single-line macro with either
1335 * `nparam' or no parameters is defined.
1337 * If a macro with precisely the right number of parameters is
1338 * defined, or nparam is -1, the address of the definition structure
1339 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1340 * is NULL, no action will be taken regarding its contents, and no
1343 * Note that this is also called with nparam zero to resolve
1346 * If you already know which context macro belongs to, you can pass
1347 * the context pointer as first parameter; if you won't but name begins
1348 * with %$ the context will be automatically computed. If all_contexts
1349 * is true, macro will be searched in outer contexts as well.
1352 smacro_defined(Context
* ctx
, char *name
, int nparam
, SMacro
** defn
,
1355 struct hash_table
*smtbl
;
1359 smtbl
= &ctx
->localmac
;
1360 } else if (name
[0] == '%' && name
[1] == '$') {
1362 ctx
= get_ctx(name
, false);
1364 return false; /* got to return _something_ */
1365 smtbl
= &ctx
->localmac
;
1369 m
= (SMacro
*) hash_findix(smtbl
, name
);
1372 if (!mstrcmp(m
->name
, name
, m
->casesense
&& nocase
) &&
1373 (nparam
<= 0 || m
->nparam
== 0 || nparam
== (int) m
->nparam
)) {
1375 if (nparam
== (int) m
->nparam
|| nparam
== -1)
1389 * Count and mark off the parameters in a multi-line macro call.
1390 * This is called both from within the multi-line macro expansion
1391 * code, and also to mark off the default parameters when provided
1392 * in a %macro definition line.
1394 static void count_mmac_params(Token
* t
, int *nparam
, Token
*** params
)
1396 int paramsize
, brace
;
1398 *nparam
= paramsize
= 0;
1401 if (*nparam
>= paramsize
) {
1402 paramsize
+= PARAM_DELTA
;
1403 *params
= nasm_realloc(*params
, sizeof(**params
) * paramsize
);
1407 if (tok_is_(t
, "{"))
1409 (*params
)[(*nparam
)++] = t
;
1410 while (tok_isnt_(t
, brace
? "}" : ","))
1412 if (t
) { /* got a comma/brace */
1416 * Now we've found the closing brace, look further
1420 if (tok_isnt_(t
, ",")) {
1422 "braces do not enclose all of macro parameter");
1423 while (tok_isnt_(t
, ","))
1427 t
= t
->next
; /* eat the comma */
1434 * Determine whether one of the various `if' conditions is true or
1437 * We must free the tline we get passed.
1439 static bool if_condition(Token
* tline
, enum preproc_token ct
)
1441 enum pp_conditional i
= PP_COND(ct
);
1443 Token
*t
, *tt
, **tptr
, *origline
;
1444 struct tokenval tokval
;
1446 enum pp_token_type needtype
;
1452 j
= false; /* have we matched yet? */
1453 while (cstk
&& tline
) {
1455 if (!tline
|| tline
->type
!= TOK_ID
) {
1457 "`%s' expects context identifiers", pp_directives
[ct
]);
1458 free_tlist(origline
);
1461 if (!nasm_stricmp(tline
->text
, cstk
->name
))
1463 tline
= tline
->next
;
1468 j
= false; /* have we matched yet? */
1471 if (!tline
|| (tline
->type
!= TOK_ID
&&
1472 (tline
->type
!= TOK_PREPROC_ID
||
1473 tline
->text
[1] != '$'))) {
1475 "`%s' expects macro identifiers", pp_directives
[ct
]);
1478 if (smacro_defined(NULL
, tline
->text
, 0, NULL
, true))
1480 tline
= tline
->next
;
1486 tline
= expand_smacro(tline
);
1488 while (tok_isnt_(tt
, ","))
1492 "`%s' expects two comma-separated arguments",
1497 j
= true; /* assume equality unless proved not */
1498 while ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) && tt
) {
1499 if (tt
->type
== TOK_OTHER
&& !strcmp(tt
->text
, ",")) {
1500 error(ERR_NONFATAL
, "`%s': more than one comma on line",
1504 if (t
->type
== TOK_WHITESPACE
) {
1508 if (tt
->type
== TOK_WHITESPACE
) {
1512 if (tt
->type
!= t
->type
) {
1513 j
= false; /* found mismatching tokens */
1516 /* Unify surrounding quotes for strings */
1517 if (t
->type
== TOK_STRING
) {
1518 tt
->text
[0] = t
->text
[0];
1519 tt
->text
[strlen(tt
->text
) - 1] = t
->text
[0];
1521 if (mstrcmp(tt
->text
, t
->text
, i
== PPC_IFIDN
) != 0) {
1522 j
= false; /* found mismatching tokens */
1529 if ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) || tt
)
1530 j
= false; /* trailing gunk on one end or other */
1536 MMacro searching
, *mmac
;
1538 tline
= tline
->next
;
1540 tline
= expand_id(tline
);
1541 if (!tok_type_(tline
, TOK_ID
)) {
1543 "`%s' expects a macro name", pp_directives
[ct
]);
1546 searching
.name
= nasm_strdup(tline
->text
);
1547 searching
.casesense
= true;
1548 searching
.plus
= false;
1549 searching
.nolist
= false;
1550 searching
.in_progress
= 0;
1551 searching
.rep_nest
= NULL
;
1552 searching
.nparam_min
= 0;
1553 searching
.nparam_max
= INT_MAX
;
1554 tline
= expand_smacro(tline
->next
);
1557 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
1559 "`%s' expects a parameter count or nothing",
1562 searching
.nparam_min
= searching
.nparam_max
=
1563 readnum(tline
->text
, &j
);
1566 "unable to parse parameter count `%s'",
1569 if (tline
&& tok_is_(tline
->next
, "-")) {
1570 tline
= tline
->next
->next
;
1571 if (tok_is_(tline
, "*"))
1572 searching
.nparam_max
= INT_MAX
;
1573 else if (!tok_type_(tline
, TOK_NUMBER
))
1575 "`%s' expects a parameter count after `-'",
1578 searching
.nparam_max
= readnum(tline
->text
, &j
);
1581 "unable to parse parameter count `%s'",
1583 if (searching
.nparam_min
> searching
.nparam_max
)
1585 "minimum parameter count exceeds maximum");
1588 if (tline
&& tok_is_(tline
->next
, "+")) {
1589 tline
= tline
->next
;
1590 searching
.plus
= true;
1592 mmac
= (MMacro
*) hash_findix(&mmacros
, searching
.name
);
1594 if (!strcmp(mmac
->name
, searching
.name
) &&
1595 (mmac
->nparam_min
<= searching
.nparam_max
1597 && (searching
.nparam_min
<= mmac
->nparam_max
1604 nasm_free(searching
.name
);
1613 needtype
= TOK_NUMBER
;
1616 needtype
= TOK_STRING
;
1620 t
= tline
= expand_smacro(tline
);
1622 while (tok_type_(t
, TOK_WHITESPACE
) ||
1623 (needtype
== TOK_NUMBER
&&
1624 tok_type_(t
, TOK_OTHER
) &&
1625 (t
->text
[0] == '-' || t
->text
[0] == '+') &&
1629 j
= tok_type_(t
, needtype
);
1633 t
= tline
= expand_smacro(tline
);
1634 while (tok_type_(t
, TOK_WHITESPACE
))
1639 t
= t
->next
; /* Skip the actual token */
1640 while (tok_type_(t
, TOK_WHITESPACE
))
1642 j
= !t
; /* Should be nothing left */
1647 t
= tline
= expand_smacro(tline
);
1648 while (tok_type_(t
, TOK_WHITESPACE
))
1651 j
= !t
; /* Should be empty */
1655 t
= tline
= expand_smacro(tline
);
1657 tokval
.t_type
= TOKEN_INVALID
;
1658 evalresult
= evaluate(ppscan
, tptr
, &tokval
,
1659 NULL
, pass
| CRITICAL
, error
, NULL
);
1664 "trailing garbage after expression ignored");
1665 if (!is_simple(evalresult
)) {
1667 "non-constant value given to `%s'", pp_directives
[ct
]);
1670 j
= reloc_value(evalresult
) != 0;
1675 "preprocessor directive `%s' not yet implemented",
1680 free_tlist(origline
);
1681 return j
^ PP_NEGATIVE(ct
);
1684 free_tlist(origline
);
1689 * Expand macros in a string. Used in %error directives (and it should
1690 * almost certainly be removed from there, too.)
1692 * First tokenize the string, apply "expand_smacro" and then de-tokenize back.
1693 * The returned variable should ALWAYS be freed after usage.
1695 void expand_macros_in_string(char **p
)
1697 Token
*line
= tokenize(*p
);
1698 line
= expand_smacro(line
);
1699 *p
= detoken(line
, false);
1703 * Common code for defining an smacro
1705 static bool define_smacro(Context
*ctx
, char *mname
, bool casesense
,
1706 int nparam
, Token
*expansion
)
1708 SMacro
*smac
, **smhead
;
1709 struct hash_table
*smtbl
;
1711 if (smacro_defined(ctx
, mname
, nparam
, &smac
, casesense
)) {
1714 "single-line macro `%s' defined both with and"
1715 " without parameters", mname
);
1717 /* Some instances of the old code considered this a failure,
1718 some others didn't. What is the right thing to do here? */
1719 free_tlist(expansion
);
1720 return false; /* Failure */
1723 * We're redefining, so we have to take over an
1724 * existing SMacro structure. This means freeing
1725 * what was already in it.
1727 nasm_free(smac
->name
);
1728 free_tlist(smac
->expansion
);
1731 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
1732 smhead
= (SMacro
**) hash_findi_add(smtbl
, mname
);
1733 smac
= nasm_malloc(sizeof(SMacro
));
1734 smac
->next
= *smhead
;
1737 smac
->name
= nasm_strdup(mname
);
1738 smac
->casesense
= casesense
;
1739 smac
->nparam
= nparam
;
1740 smac
->expansion
= expansion
;
1741 smac
->in_progress
= false;
1742 return true; /* Success */
1746 * Undefine an smacro
1748 static void undef_smacro(Context
*ctx
, const char *mname
)
1750 SMacro
**smhead
, *s
, **sp
;
1751 struct hash_table
*smtbl
;
1753 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
1754 smhead
= (SMacro
**)hash_findi(smtbl
, mname
, NULL
);
1758 * We now have a macro name... go hunt for it.
1761 while ((s
= *sp
) != NULL
) {
1762 if (!mstrcmp(s
->name
, mname
, s
->casesense
)) {
1765 free_tlist(s
->expansion
);
1775 * Decode a size directive
1777 static int parse_size(const char *str
) {
1778 static const char *size_names
[] =
1779 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
1780 static const int sizes
[] =
1781 { 0, 1, 4, 16, 8, 10, 2, 32 };
1783 return sizes
[bsii(str
, size_names
, elements(size_names
))+1];
1787 * find and process preprocessor directive in passed line
1788 * Find out if a line contains a preprocessor directive, and deal
1791 * If a directive _is_ found, it is the responsibility of this routine
1792 * (and not the caller) to free_tlist() the line.
1794 * @param tline a pointer to the current tokeninzed line linked list
1795 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
1798 static int do_directive(Token
* tline
)
1800 enum preproc_token i
;
1812 MMacro
*mmac
, **mmhead
;
1813 Token
*t
, *tt
, *param_start
, *macro_start
, *last
, **tptr
, *origline
;
1815 struct tokenval tokval
;
1817 MMacro
*tmp_defining
; /* Used when manipulating rep_nest */
1823 if (!tok_type_(tline
, TOK_PREPROC_ID
) ||
1824 (tline
->text
[1] == '%' || tline
->text
[1] == '$'
1825 || tline
->text
[1] == '!'))
1826 return NO_DIRECTIVE_FOUND
;
1828 i
= pp_token_hash(tline
->text
);
1831 * If we're in a non-emitting branch of a condition construct,
1832 * or walking to the end of an already terminated %rep block,
1833 * we should ignore all directives except for condition
1836 if (((istk
->conds
&& !emitting(istk
->conds
->state
)) ||
1837 (istk
->mstk
&& !istk
->mstk
->in_progress
)) && !is_condition(i
)) {
1838 return NO_DIRECTIVE_FOUND
;
1842 * If we're defining a macro or reading a %rep block, we should
1843 * ignore all directives except for %macro/%imacro (which
1844 * generate an error), %endm/%endmacro, and (only if we're in a
1845 * %rep block) %endrep. If we're in a %rep block, another %rep
1846 * causes an error, so should be let through.
1848 if (defining
&& i
!= PP_MACRO
&& i
!= PP_IMACRO
&&
1849 i
!= PP_ENDMACRO
&& i
!= PP_ENDM
&&
1850 (defining
->name
|| (i
!= PP_ENDREP
&& i
!= PP_REP
))) {
1851 return NO_DIRECTIVE_FOUND
;
1856 error(ERR_NONFATAL
, "unknown preprocessor directive `%s'",
1858 return NO_DIRECTIVE_FOUND
; /* didn't get it */
1861 /* Directive to tell NASM what the default stack size is. The
1862 * default is for a 16-bit stack, and this can be overriden with
1864 * the following form:
1866 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1868 tline
= tline
->next
;
1869 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1870 tline
= tline
->next
;
1871 if (!tline
|| tline
->type
!= TOK_ID
) {
1872 error(ERR_NONFATAL
, "`%%stacksize' missing size parameter");
1873 free_tlist(origline
);
1874 return DIRECTIVE_FOUND
;
1876 if (nasm_stricmp(tline
->text
, "flat") == 0) {
1877 /* All subsequent ARG directives are for a 32-bit stack */
1879 StackPointer
= "ebp";
1882 } else if (nasm_stricmp(tline
->text
, "flat64") == 0) {
1883 /* All subsequent ARG directives are for a 64-bit stack */
1885 StackPointer
= "rbp";
1888 } else if (nasm_stricmp(tline
->text
, "large") == 0) {
1889 /* All subsequent ARG directives are for a 16-bit stack,
1890 * far function call.
1893 StackPointer
= "bp";
1896 } else if (nasm_stricmp(tline
->text
, "small") == 0) {
1897 /* All subsequent ARG directives are for a 16-bit stack,
1898 * far function call. We don't support near functions.
1901 StackPointer
= "bp";
1905 error(ERR_NONFATAL
, "`%%stacksize' invalid size type");
1906 free_tlist(origline
);
1907 return DIRECTIVE_FOUND
;
1909 free_tlist(origline
);
1910 return DIRECTIVE_FOUND
;
1913 /* TASM like ARG directive to define arguments to functions, in
1914 * the following form:
1916 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1920 char *arg
, directive
[256];
1921 int size
= StackSize
;
1923 /* Find the argument name */
1924 tline
= tline
->next
;
1925 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1926 tline
= tline
->next
;
1927 if (!tline
|| tline
->type
!= TOK_ID
) {
1928 error(ERR_NONFATAL
, "`%%arg' missing argument parameter");
1929 free_tlist(origline
);
1930 return DIRECTIVE_FOUND
;
1934 /* Find the argument size type */
1935 tline
= tline
->next
;
1936 if (!tline
|| tline
->type
!= TOK_OTHER
1937 || tline
->text
[0] != ':') {
1939 "Syntax error processing `%%arg' directive");
1940 free_tlist(origline
);
1941 return DIRECTIVE_FOUND
;
1943 tline
= tline
->next
;
1944 if (!tline
|| tline
->type
!= TOK_ID
) {
1945 error(ERR_NONFATAL
, "`%%arg' missing size type parameter");
1946 free_tlist(origline
);
1947 return DIRECTIVE_FOUND
;
1950 /* Allow macro expansion of type parameter */
1951 tt
= tokenize(tline
->text
);
1952 tt
= expand_smacro(tt
);
1953 size
= parse_size(tt
->text
);
1956 "Invalid size type for `%%arg' missing directive");
1958 free_tlist(origline
);
1959 return DIRECTIVE_FOUND
;
1963 /* Round up to even stack slots */
1964 size
= (size
+StackSize
-1) & ~(StackSize
-1);
1966 /* Now define the macro for the argument */
1967 snprintf(directive
, sizeof(directive
), "%%define %s (%s+%d)",
1968 arg
, StackPointer
, offset
);
1969 do_directive(tokenize(directive
));
1972 /* Move to the next argument in the list */
1973 tline
= tline
->next
;
1974 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1975 tline
= tline
->next
;
1976 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
1978 free_tlist(origline
);
1979 return DIRECTIVE_FOUND
;
1982 /* TASM like LOCAL directive to define local variables for a
1983 * function, in the following form:
1985 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
1987 * The '= LocalSize' at the end is ignored by NASM, but is
1988 * required by TASM to define the local parameter size (and used
1989 * by the TASM macro package).
1991 offset
= LocalOffset
;
1993 char *local
, directive
[256];
1994 int size
= StackSize
;
1996 /* Find the argument name */
1997 tline
= tline
->next
;
1998 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1999 tline
= tline
->next
;
2000 if (!tline
|| tline
->type
!= TOK_ID
) {
2002 "`%%local' missing argument parameter");
2003 free_tlist(origline
);
2004 return DIRECTIVE_FOUND
;
2006 local
= tline
->text
;
2008 /* Find the argument size type */
2009 tline
= tline
->next
;
2010 if (!tline
|| tline
->type
!= TOK_OTHER
2011 || tline
->text
[0] != ':') {
2013 "Syntax error processing `%%local' directive");
2014 free_tlist(origline
);
2015 return DIRECTIVE_FOUND
;
2017 tline
= tline
->next
;
2018 if (!tline
|| tline
->type
!= TOK_ID
) {
2020 "`%%local' missing size type parameter");
2021 free_tlist(origline
);
2022 return DIRECTIVE_FOUND
;
2025 /* Allow macro expansion of type parameter */
2026 tt
= tokenize(tline
->text
);
2027 tt
= expand_smacro(tt
);
2028 size
= parse_size(tt
->text
);
2031 "Invalid size type for `%%local' missing directive");
2033 free_tlist(origline
);
2034 return DIRECTIVE_FOUND
;
2038 /* Round up to even stack slots */
2039 size
= (size
+StackSize
-1) & ~(StackSize
-1);
2041 offset
+= size
; /* Negative offset, increment before */
2043 /* Now define the macro for the argument */
2044 snprintf(directive
, sizeof(directive
), "%%define %s (%s-%d)",
2045 local
, StackPointer
, offset
);
2046 do_directive(tokenize(directive
));
2048 /* Now define the assign to setup the enter_c macro correctly */
2049 snprintf(directive
, sizeof(directive
),
2050 "%%assign %%$localsize %%$localsize+%d", size
);
2051 do_directive(tokenize(directive
));
2053 /* Move to the next argument in the list */
2054 tline
= tline
->next
;
2055 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2056 tline
= tline
->next
;
2057 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2058 LocalOffset
= offset
;
2059 free_tlist(origline
);
2060 return DIRECTIVE_FOUND
;
2064 error(ERR_WARNING
, "trailing garbage after `%%clear' ignored");
2067 free_tlist(origline
);
2068 return DIRECTIVE_FOUND
;
2071 tline
= expand_smacro(tline
->next
);
2073 if (!tline
|| (tline
->type
!= TOK_STRING
&&
2074 tline
->type
!= TOK_INTERNAL_STRING
)) {
2075 error(ERR_NONFATAL
, "`%%depend' expects a file name");
2076 free_tlist(origline
);
2077 return DIRECTIVE_FOUND
; /* but we did _something_ */
2081 "trailing garbage after `%%depend' ignored");
2082 if (tline
->type
!= TOK_INTERNAL_STRING
) {
2083 p
= tline
->text
+ 1; /* point past the quote to the name */
2084 p
[strlen(p
) - 1] = '\0'; /* remove the trailing quote */
2086 p
= tline
->text
; /* internal_string is easier */
2087 if (dephead
&& !in_list(*dephead
, p
)) {
2088 StrList
*sl
= nasm_malloc(strlen(p
)+1+sizeof sl
->next
);
2092 deptail
= &sl
->next
;
2094 free_tlist(origline
);
2095 return DIRECTIVE_FOUND
;
2098 tline
= expand_smacro(tline
->next
);
2101 if (!tline
|| (tline
->type
!= TOK_STRING
&&
2102 tline
->type
!= TOK_INTERNAL_STRING
)) {
2103 error(ERR_NONFATAL
, "`%%include' expects a file name");
2104 free_tlist(origline
);
2105 return DIRECTIVE_FOUND
; /* but we did _something_ */
2109 "trailing garbage after `%%include' ignored");
2110 if (tline
->type
!= TOK_INTERNAL_STRING
) {
2111 p
= tline
->text
+ 1; /* point past the quote to the name */
2112 p
[strlen(p
) - 1] = '\0'; /* remove the trailing quote */
2114 p
= tline
->text
; /* internal_string is easier */
2115 inc
= nasm_malloc(sizeof(Include
));
2118 inc
->fp
= inc_fopen(p
, dephead
, deptail
, pass
== 0);
2120 /* -MG given but file not found */
2123 inc
->fname
= src_set_fname(p
);
2124 inc
->lineno
= src_set_linnum(0);
2126 inc
->expansion
= NULL
;
2129 list
->uplevel(LIST_INCLUDE
);
2131 free_tlist(origline
);
2132 return DIRECTIVE_FOUND
;
2135 tline
= tline
->next
;
2137 tline
= expand_id(tline
);
2138 if (!tok_type_(tline
, TOK_ID
)) {
2139 error(ERR_NONFATAL
, "`%%push' expects a context identifier");
2140 free_tlist(origline
);
2141 return DIRECTIVE_FOUND
; /* but we did _something_ */
2144 error(ERR_WARNING
, "trailing garbage after `%%push' ignored");
2145 ctx
= nasm_malloc(sizeof(Context
));
2147 hash_init(&ctx
->localmac
, HASH_SMALL
);
2148 ctx
->name
= nasm_strdup(tline
->text
);
2149 ctx
->number
= unique
++;
2151 free_tlist(origline
);
2155 tline
= tline
->next
;
2157 tline
= expand_id(tline
);
2158 if (!tok_type_(tline
, TOK_ID
)) {
2159 error(ERR_NONFATAL
, "`%%repl' expects a context identifier");
2160 free_tlist(origline
);
2161 return DIRECTIVE_FOUND
; /* but we did _something_ */
2164 error(ERR_WARNING
, "trailing garbage after `%%repl' ignored");
2166 error(ERR_NONFATAL
, "`%%repl': context stack is empty");
2168 nasm_free(cstk
->name
);
2169 cstk
->name
= nasm_strdup(tline
->text
);
2171 free_tlist(origline
);
2176 error(ERR_WARNING
, "trailing garbage after `%%pop' ignored");
2178 error(ERR_NONFATAL
, "`%%pop': context stack is already empty");
2181 free_tlist(origline
);
2185 tline
->next
= expand_smacro(tline
->next
);
2186 tline
= tline
->next
;
2188 if (tok_type_(tline
, TOK_STRING
)) {
2189 p
= tline
->text
+ 1; /* point past the quote to the name */
2190 p
[strlen(p
) - 1] = '\0'; /* remove the trailing quote */
2191 expand_macros_in_string(&p
);
2192 error(ERR_NONFATAL
, "%s", p
);
2195 p
= detoken(tline
, false);
2196 error(ERR_WARNING
, "%s", p
);
2199 free_tlist(origline
);
2203 if (istk
->conds
&& !emitting(istk
->conds
->state
))
2206 j
= if_condition(tline
->next
, i
);
2207 tline
->next
= NULL
; /* it got freed */
2208 j
= j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2210 cond
= nasm_malloc(sizeof(Cond
));
2211 cond
->next
= istk
->conds
;
2214 free_tlist(origline
);
2215 return DIRECTIVE_FOUND
;
2219 error(ERR_FATAL
, "`%s': no matching `%%if'", pp_directives
[i
]);
2220 if (emitting(istk
->conds
->state
)
2221 || istk
->conds
->state
== COND_NEVER
)
2222 istk
->conds
->state
= COND_NEVER
;
2225 * IMPORTANT: In the case of %if, we will already have
2226 * called expand_mmac_params(); however, if we're
2227 * processing an %elif we must have been in a
2228 * non-emitting mode, which would have inhibited
2229 * the normal invocation of expand_mmac_params(). Therefore,
2230 * we have to do it explicitly here.
2232 j
= if_condition(expand_mmac_params(tline
->next
), i
);
2233 tline
->next
= NULL
; /* it got freed */
2234 istk
->conds
->state
=
2235 j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2237 free_tlist(origline
);
2238 return DIRECTIVE_FOUND
;
2242 error(ERR_WARNING
, "trailing garbage after `%%else' ignored");
2244 error(ERR_FATAL
, "`%%else': no matching `%%if'");
2245 if (emitting(istk
->conds
->state
)
2246 || istk
->conds
->state
== COND_NEVER
)
2247 istk
->conds
->state
= COND_ELSE_FALSE
;
2249 istk
->conds
->state
= COND_ELSE_TRUE
;
2250 free_tlist(origline
);
2251 return DIRECTIVE_FOUND
;
2255 error(ERR_WARNING
, "trailing garbage after `%%endif' ignored");
2257 error(ERR_FATAL
, "`%%endif': no matching `%%if'");
2259 istk
->conds
= cond
->next
;
2261 free_tlist(origline
);
2262 return DIRECTIVE_FOUND
;
2268 "`%%%smacro': already defining a macro",
2269 (i
== PP_IMACRO
? "i" : ""));
2270 tline
= tline
->next
;
2272 tline
= expand_id(tline
);
2273 if (!tok_type_(tline
, TOK_ID
)) {
2275 "`%%%smacro' expects a macro name",
2276 (i
== PP_IMACRO
? "i" : ""));
2277 return DIRECTIVE_FOUND
;
2279 defining
= nasm_malloc(sizeof(MMacro
));
2280 defining
->name
= nasm_strdup(tline
->text
);
2281 defining
->casesense
= (i
== PP_MACRO
);
2282 defining
->plus
= false;
2283 defining
->nolist
= false;
2284 defining
->in_progress
= 0;
2285 defining
->rep_nest
= NULL
;
2286 tline
= expand_smacro(tline
->next
);
2288 if (!tok_type_(tline
, TOK_NUMBER
)) {
2290 "`%%%smacro' expects a parameter count",
2291 (i
== PP_IMACRO
? "i" : ""));
2292 defining
->nparam_min
= defining
->nparam_max
= 0;
2294 defining
->nparam_min
= defining
->nparam_max
=
2295 readnum(tline
->text
, &err
);
2298 "unable to parse parameter count `%s'", tline
->text
);
2300 if (tline
&& tok_is_(tline
->next
, "-")) {
2301 tline
= tline
->next
->next
;
2302 if (tok_is_(tline
, "*"))
2303 defining
->nparam_max
= INT_MAX
;
2304 else if (!tok_type_(tline
, TOK_NUMBER
))
2306 "`%%%smacro' expects a parameter count after `-'",
2307 (i
== PP_IMACRO
? "i" : ""));
2309 defining
->nparam_max
= readnum(tline
->text
, &err
);
2312 "unable to parse parameter count `%s'",
2314 if (defining
->nparam_min
> defining
->nparam_max
)
2316 "minimum parameter count exceeds maximum");
2319 if (tline
&& tok_is_(tline
->next
, "+")) {
2320 tline
= tline
->next
;
2321 defining
->plus
= true;
2323 if (tline
&& tok_type_(tline
->next
, TOK_ID
) &&
2324 !nasm_stricmp(tline
->next
->text
, ".nolist")) {
2325 tline
= tline
->next
;
2326 defining
->nolist
= true;
2328 mmac
= (MMacro
*) hash_findix(&mmacros
, defining
->name
);
2330 if (!strcmp(mmac
->name
, defining
->name
) &&
2331 (mmac
->nparam_min
<= defining
->nparam_max
2333 && (defining
->nparam_min
<= mmac
->nparam_max
2336 "redefining multi-line macro `%s'", defining
->name
);
2342 * Handle default parameters.
2344 if (tline
&& tline
->next
) {
2345 defining
->dlist
= tline
->next
;
2347 count_mmac_params(defining
->dlist
, &defining
->ndefs
,
2348 &defining
->defaults
);
2350 defining
->dlist
= NULL
;
2351 defining
->defaults
= NULL
;
2353 defining
->expansion
= NULL
;
2354 free_tlist(origline
);
2355 return DIRECTIVE_FOUND
;
2360 error(ERR_NONFATAL
, "`%s': not defining a macro", tline
->text
);
2361 return DIRECTIVE_FOUND
;
2363 mmhead
= (MMacro
**) hash_findi_add(&mmacros
, defining
->name
);
2364 defining
->next
= *mmhead
;
2367 free_tlist(origline
);
2368 return DIRECTIVE_FOUND
;
2371 if (tline
->next
&& tline
->next
->type
== TOK_WHITESPACE
)
2372 tline
= tline
->next
;
2373 if (tline
->next
== NULL
) {
2374 free_tlist(origline
);
2375 error(ERR_NONFATAL
, "`%%rotate' missing rotate count");
2376 return DIRECTIVE_FOUND
;
2378 t
= expand_smacro(tline
->next
);
2380 free_tlist(origline
);
2383 tokval
.t_type
= TOKEN_INVALID
;
2385 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2388 return DIRECTIVE_FOUND
;
2391 "trailing garbage after expression ignored");
2392 if (!is_simple(evalresult
)) {
2393 error(ERR_NONFATAL
, "non-constant value given to `%%rotate'");
2394 return DIRECTIVE_FOUND
;
2397 while (mmac
&& !mmac
->name
) /* avoid mistaking %reps for macros */
2398 mmac
= mmac
->next_active
;
2400 error(ERR_NONFATAL
, "`%%rotate' invoked outside a macro call");
2401 } else if (mmac
->nparam
== 0) {
2403 "`%%rotate' invoked within macro without parameters");
2405 int rotate
= mmac
->rotate
+ reloc_value(evalresult
);
2407 rotate
%= (int)mmac
->nparam
;
2409 rotate
+= mmac
->nparam
;
2411 mmac
->rotate
= rotate
;
2413 return DIRECTIVE_FOUND
;
2418 tline
= tline
->next
;
2419 } while (tok_type_(tline
, TOK_WHITESPACE
));
2421 if (tok_type_(tline
, TOK_ID
) &&
2422 nasm_stricmp(tline
->text
, ".nolist") == 0) {
2425 tline
= tline
->next
;
2426 } while (tok_type_(tline
, TOK_WHITESPACE
));
2430 t
= expand_smacro(tline
);
2432 tokval
.t_type
= TOKEN_INVALID
;
2434 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2436 free_tlist(origline
);
2437 return DIRECTIVE_FOUND
;
2441 "trailing garbage after expression ignored");
2442 if (!is_simple(evalresult
)) {
2443 error(ERR_NONFATAL
, "non-constant value given to `%%rep'");
2444 return DIRECTIVE_FOUND
;
2446 count
= reloc_value(evalresult
) + 1;
2448 error(ERR_NONFATAL
, "`%%rep' expects a repeat count");
2451 free_tlist(origline
);
2453 tmp_defining
= defining
;
2454 defining
= nasm_malloc(sizeof(MMacro
));
2455 defining
->name
= NULL
; /* flags this macro as a %rep block */
2456 defining
->casesense
= false;
2457 defining
->plus
= false;
2458 defining
->nolist
= nolist
;
2459 defining
->in_progress
= count
;
2460 defining
->nparam_min
= defining
->nparam_max
= 0;
2461 defining
->defaults
= NULL
;
2462 defining
->dlist
= NULL
;
2463 defining
->expansion
= NULL
;
2464 defining
->next_active
= istk
->mstk
;
2465 defining
->rep_nest
= tmp_defining
;
2466 return DIRECTIVE_FOUND
;
2469 if (!defining
|| defining
->name
) {
2470 error(ERR_NONFATAL
, "`%%endrep': no matching `%%rep'");
2471 return DIRECTIVE_FOUND
;
2475 * Now we have a "macro" defined - although it has no name
2476 * and we won't be entering it in the hash tables - we must
2477 * push a macro-end marker for it on to istk->expansion.
2478 * After that, it will take care of propagating itself (a
2479 * macro-end marker line for a macro which is really a %rep
2480 * block will cause the macro to be re-expanded, complete
2481 * with another macro-end marker to ensure the process
2482 * continues) until the whole expansion is forcibly removed
2483 * from istk->expansion by a %exitrep.
2485 l
= nasm_malloc(sizeof(Line
));
2486 l
->next
= istk
->expansion
;
2487 l
->finishes
= defining
;
2489 istk
->expansion
= l
;
2491 istk
->mstk
= defining
;
2493 list
->uplevel(defining
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
2494 tmp_defining
= defining
;
2495 defining
= defining
->rep_nest
;
2496 free_tlist(origline
);
2497 return DIRECTIVE_FOUND
;
2501 * We must search along istk->expansion until we hit a
2502 * macro-end marker for a macro with no name. Then we set
2503 * its `in_progress' flag to 0.
2505 for (l
= istk
->expansion
; l
; l
= l
->next
)
2506 if (l
->finishes
&& !l
->finishes
->name
)
2510 l
->finishes
->in_progress
= 0;
2512 error(ERR_NONFATAL
, "`%%exitrep' not within `%%rep' block");
2513 free_tlist(origline
);
2514 return DIRECTIVE_FOUND
;
2520 casesense
= (i
== PP_DEFINE
|| i
== PP_XDEFINE
);
2522 tline
= tline
->next
;
2524 tline
= expand_id(tline
);
2525 if (!tline
|| (tline
->type
!= TOK_ID
&&
2526 (tline
->type
!= TOK_PREPROC_ID
||
2527 tline
->text
[1] != '$'))) {
2528 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
2530 free_tlist(origline
);
2531 return DIRECTIVE_FOUND
;
2534 ctx
= get_ctx(tline
->text
, false);
2536 mname
= tline
->text
;
2538 param_start
= tline
= tline
->next
;
2541 /* Expand the macro definition now for %xdefine and %ixdefine */
2542 if ((i
== PP_XDEFINE
) || (i
== PP_IXDEFINE
))
2543 tline
= expand_smacro(tline
);
2545 if (tok_is_(tline
, "(")) {
2547 * This macro has parameters.
2550 tline
= tline
->next
;
2554 error(ERR_NONFATAL
, "parameter identifier expected");
2555 free_tlist(origline
);
2556 return DIRECTIVE_FOUND
;
2558 if (tline
->type
!= TOK_ID
) {
2560 "`%s': parameter identifier expected",
2562 free_tlist(origline
);
2563 return DIRECTIVE_FOUND
;
2565 tline
->type
= TOK_SMAC_PARAM
+ nparam
++;
2566 tline
= tline
->next
;
2568 if (tok_is_(tline
, ",")) {
2569 tline
= tline
->next
;
2572 if (!tok_is_(tline
, ")")) {
2574 "`)' expected to terminate macro template");
2575 free_tlist(origline
);
2576 return DIRECTIVE_FOUND
;
2581 tline
= tline
->next
;
2583 if (tok_type_(tline
, TOK_WHITESPACE
))
2584 last
= tline
, tline
= tline
->next
;
2589 if (t
->type
== TOK_ID
) {
2590 for (tt
= param_start
; tt
; tt
= tt
->next
)
2591 if (tt
->type
>= TOK_SMAC_PARAM
&&
2592 !strcmp(tt
->text
, t
->text
))
2596 t
->next
= macro_start
;
2601 * Good. We now have a macro name, a parameter count, and a
2602 * token list (in reverse order) for an expansion. We ought
2603 * to be OK just to create an SMacro, store it, and let
2604 * free_tlist have the rest of the line (which we have
2605 * carefully re-terminated after chopping off the expansion
2608 define_smacro(ctx
, mname
, casesense
, nparam
, macro_start
);
2609 free_tlist(origline
);
2610 return DIRECTIVE_FOUND
;
2613 tline
= tline
->next
;
2615 tline
= expand_id(tline
);
2616 if (!tline
|| (tline
->type
!= TOK_ID
&&
2617 (tline
->type
!= TOK_PREPROC_ID
||
2618 tline
->text
[1] != '$'))) {
2619 error(ERR_NONFATAL
, "`%%undef' expects a macro identifier");
2620 free_tlist(origline
);
2621 return DIRECTIVE_FOUND
;
2625 "trailing garbage after macro name ignored");
2628 /* Find the context that symbol belongs to */
2629 ctx
= get_ctx(tline
->text
, false);
2630 undef_smacro(ctx
, tline
->text
);
2631 free_tlist(origline
);
2632 return DIRECTIVE_FOUND
;
2637 StrList
*xsl
= NULL
;
2641 tline
= tline
->next
;
2643 tline
= expand_id(tline
);
2644 if (!tline
|| (tline
->type
!= TOK_ID
&&
2645 (tline
->type
!= TOK_PREPROC_ID
||
2646 tline
->text
[1] != '$'))) {
2648 "`%%pathsearch' expects a macro identifier as first parameter");
2649 free_tlist(origline
);
2650 return DIRECTIVE_FOUND
;
2652 ctx
= get_ctx(tline
->text
, false);
2654 mname
= tline
->text
;
2656 tline
= expand_smacro(tline
->next
);
2660 while (tok_type_(t
, TOK_WHITESPACE
))
2663 if (!t
|| (t
->type
!= TOK_STRING
&&
2664 t
->type
!= TOK_INTERNAL_STRING
)) {
2665 error(ERR_NONFATAL
, "`%%pathsearch' expects a file name");
2667 free_tlist(origline
);
2668 return DIRECTIVE_FOUND
; /* but we did _something_ */
2672 "trailing garbage after `%%pathsearch' ignored");
2673 if (t
->type
!= TOK_INTERNAL_STRING
) {
2674 p
= t
->text
+ 1; /* point past the quote to the name */
2675 p
[strlen(p
) - 1] = '\0'; /* remove the trailing quote */
2677 p
= t
->text
; /* internal_string is easier */
2679 fp
= inc_fopen(p
, &xsl
, &xsl
, true);
2682 fclose(fp
); /* Don't actually care about the file */
2684 macro_start
= nasm_malloc(sizeof(*macro_start
));
2685 macro_start
->next
= NULL
;
2686 macro_start
->text
= nasm_strdup(p
);
2687 nasm_quote(¯o_start
->text
);
2688 macro_start
->type
= TOK_STRING
;
2689 macro_start
->mac
= NULL
;
2694 * We now have a macro name, an implicit parameter count of
2695 * zero, and a string token to use as an expansion. Create
2696 * and store an SMacro.
2698 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2700 free_tlist(origline
);
2701 return DIRECTIVE_FOUND
;
2707 tline
= tline
->next
;
2709 tline
= expand_id(tline
);
2710 if (!tline
|| (tline
->type
!= TOK_ID
&&
2711 (tline
->type
!= TOK_PREPROC_ID
||
2712 tline
->text
[1] != '$'))) {
2714 "`%%strlen' expects a macro identifier as first parameter");
2715 free_tlist(origline
);
2716 return DIRECTIVE_FOUND
;
2718 ctx
= get_ctx(tline
->text
, false);
2720 mname
= tline
->text
;
2722 tline
= expand_smacro(tline
->next
);
2726 while (tok_type_(t
, TOK_WHITESPACE
))
2728 /* t should now point to the string */
2729 if (t
->type
!= TOK_STRING
) {
2731 "`%%strlen` requires string as second parameter");
2733 free_tlist(origline
);
2734 return DIRECTIVE_FOUND
;
2737 macro_start
= nasm_malloc(sizeof(*macro_start
));
2738 macro_start
->next
= NULL
;
2739 make_tok_num(macro_start
, strlen(t
->text
) - 2);
2740 macro_start
->mac
= NULL
;
2743 * We now have a macro name, an implicit parameter count of
2744 * zero, and a numeric token to use as an expansion. Create
2745 * and store an SMacro.
2747 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2749 free_tlist(origline
);
2750 return DIRECTIVE_FOUND
;
2755 tline
= tline
->next
;
2757 tline
= expand_id(tline
);
2758 if (!tline
|| (tline
->type
!= TOK_ID
&&
2759 (tline
->type
!= TOK_PREPROC_ID
||
2760 tline
->text
[1] != '$'))) {
2762 "`%%substr' expects a macro identifier as first parameter");
2763 free_tlist(origline
);
2764 return DIRECTIVE_FOUND
;
2766 ctx
= get_ctx(tline
->text
, false);
2768 mname
= tline
->text
;
2770 tline
= expand_smacro(tline
->next
);
2774 while (tok_type_(t
, TOK_WHITESPACE
))
2777 /* t should now point to the string */
2778 if (t
->type
!= TOK_STRING
) {
2780 "`%%substr` requires string as second parameter");
2782 free_tlist(origline
);
2783 return DIRECTIVE_FOUND
;
2788 tokval
.t_type
= TOKEN_INVALID
;
2790 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2793 free_tlist(origline
);
2794 return DIRECTIVE_FOUND
;
2796 if (!is_simple(evalresult
)) {
2797 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
2799 free_tlist(origline
);
2800 return DIRECTIVE_FOUND
;
2803 macro_start
= nasm_malloc(sizeof(*macro_start
));
2804 macro_start
->next
= NULL
;
2805 macro_start
->text
= nasm_strdup("'''");
2806 if (evalresult
->value
> 0
2807 && evalresult
->value
< (int) strlen(t
->text
) - 1) {
2808 macro_start
->text
[1] = t
->text
[evalresult
->value
];
2810 macro_start
->text
[2] = '\0';
2812 macro_start
->type
= TOK_STRING
;
2813 macro_start
->mac
= NULL
;
2816 * We now have a macro name, an implicit parameter count of
2817 * zero, and a numeric token to use as an expansion. Create
2818 * and store an SMacro.
2820 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2822 free_tlist(origline
);
2823 return DIRECTIVE_FOUND
;
2827 casesense
= (i
== PP_ASSIGN
);
2829 tline
= tline
->next
;
2831 tline
= expand_id(tline
);
2832 if (!tline
|| (tline
->type
!= TOK_ID
&&
2833 (tline
->type
!= TOK_PREPROC_ID
||
2834 tline
->text
[1] != '$'))) {
2836 "`%%%sassign' expects a macro identifier",
2837 (i
== PP_IASSIGN
? "i" : ""));
2838 free_tlist(origline
);
2839 return DIRECTIVE_FOUND
;
2841 ctx
= get_ctx(tline
->text
, false);
2843 mname
= tline
->text
;
2845 tline
= expand_smacro(tline
->next
);
2850 tokval
.t_type
= TOKEN_INVALID
;
2852 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2855 free_tlist(origline
);
2856 return DIRECTIVE_FOUND
;
2861 "trailing garbage after expression ignored");
2863 if (!is_simple(evalresult
)) {
2865 "non-constant value given to `%%%sassign'",
2866 (i
== PP_IASSIGN
? "i" : ""));
2867 free_tlist(origline
);
2868 return DIRECTIVE_FOUND
;
2871 macro_start
= nasm_malloc(sizeof(*macro_start
));
2872 macro_start
->next
= NULL
;
2873 make_tok_num(macro_start
, reloc_value(evalresult
));
2874 macro_start
->mac
= NULL
;
2877 * We now have a macro name, an implicit parameter count of
2878 * zero, and a numeric token to use as an expansion. Create
2879 * and store an SMacro.
2881 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2882 free_tlist(origline
);
2883 return DIRECTIVE_FOUND
;
2887 * Syntax is `%line nnn[+mmm] [filename]'
2889 tline
= tline
->next
;
2891 if (!tok_type_(tline
, TOK_NUMBER
)) {
2892 error(ERR_NONFATAL
, "`%%line' expects line number");
2893 free_tlist(origline
);
2894 return DIRECTIVE_FOUND
;
2896 k
= readnum(tline
->text
, &err
);
2898 tline
= tline
->next
;
2899 if (tok_is_(tline
, "+")) {
2900 tline
= tline
->next
;
2901 if (!tok_type_(tline
, TOK_NUMBER
)) {
2902 error(ERR_NONFATAL
, "`%%line' expects line increment");
2903 free_tlist(origline
);
2904 return DIRECTIVE_FOUND
;
2906 m
= readnum(tline
->text
, &err
);
2907 tline
= tline
->next
;
2913 nasm_free(src_set_fname(detoken(tline
, false)));
2915 free_tlist(origline
);
2916 return DIRECTIVE_FOUND
;
2920 "preprocessor directive `%s' not yet implemented",
2924 return DIRECTIVE_FOUND
;
2928 * Ensure that a macro parameter contains a condition code and
2929 * nothing else. Return the condition code index if so, or -1
2932 static int find_cc(Token
* t
)
2938 return -1; /* Probably a %+ without a space */
2941 if (t
->type
!= TOK_ID
)
2945 if (tt
&& (tt
->type
!= TOK_OTHER
|| strcmp(tt
->text
, ",")))
2949 j
= elements(conditions
);
2952 m
= nasm_stricmp(t
->text
, conditions
[k
]);
2968 * Expand MMacro-local things: parameter references (%0, %n, %+n,
2969 * %-n) and MMacro-local identifiers (%%foo).
2971 static Token
*expand_mmac_params(Token
* tline
)
2973 Token
*t
, *tt
, **tail
, *thead
;
2979 if (tline
->type
== TOK_PREPROC_ID
&&
2980 (((tline
->text
[1] == '+' || tline
->text
[1] == '-')
2981 && tline
->text
[2]) || tline
->text
[1] == '%'
2982 || (tline
->text
[1] >= '0' && tline
->text
[1] <= '9'))) {
2984 int type
= 0, cc
; /* type = 0 to placate optimisers */
2991 tline
= tline
->next
;
2994 while (mac
&& !mac
->name
) /* avoid mistaking %reps for macros */
2995 mac
= mac
->next_active
;
2997 error(ERR_NONFATAL
, "`%s': not in a macro call", t
->text
);
2999 switch (t
->text
[1]) {
3001 * We have to make a substitution of one of the
3002 * forms %1, %-1, %+1, %%foo, %0.
3006 snprintf(tmpbuf
, sizeof(tmpbuf
), "%d", mac
->nparam
);
3007 text
= nasm_strdup(tmpbuf
);
3011 snprintf(tmpbuf
, sizeof(tmpbuf
), "..@%"PRIu64
".",
3013 text
= nasm_strcat(tmpbuf
, t
->text
+ 2);
3016 n
= atoi(t
->text
+ 2) - 1;
3017 if (n
>= mac
->nparam
)
3020 if (mac
->nparam
> 1)
3021 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3022 tt
= mac
->params
[n
];
3027 "macro parameter %d is not a condition code",
3032 if (inverse_ccs
[cc
] == -1) {
3034 "condition code `%s' is not invertible",
3039 nasm_strdup(conditions
[inverse_ccs
[cc
]]);
3043 n
= atoi(t
->text
+ 2) - 1;
3044 if (n
>= mac
->nparam
)
3047 if (mac
->nparam
> 1)
3048 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3049 tt
= mac
->params
[n
];
3054 "macro parameter %d is not a condition code",
3059 text
= nasm_strdup(conditions
[cc
]);
3063 n
= atoi(t
->text
+ 1) - 1;
3064 if (n
>= mac
->nparam
)
3067 if (mac
->nparam
> 1)
3068 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3069 tt
= mac
->params
[n
];
3072 for (i
= 0; i
< mac
->paramlen
[n
]; i
++) {
3073 *tail
= new_Token(NULL
, tt
->type
, tt
->text
, 0);
3074 tail
= &(*tail
)->next
;
3078 text
= NULL
; /* we've done it here */
3094 tline
= tline
->next
;
3101 for (; t
&& (tt
= t
->next
) != NULL
; t
= t
->next
)
3103 case TOK_WHITESPACE
:
3104 if (tt
->type
== TOK_WHITESPACE
) {
3105 t
->next
= delete_Token(tt
);
3109 if (tt
->type
== TOK_ID
|| tt
->type
== TOK_NUMBER
) {
3110 char *tmp
= nasm_strcat(t
->text
, tt
->text
);
3113 t
->next
= delete_Token(tt
);
3117 if (tt
->type
== TOK_NUMBER
) {
3118 char *tmp
= nasm_strcat(t
->text
, tt
->text
);
3121 t
->next
= delete_Token(tt
);
3132 * Expand all single-line macro calls made in the given line.
3133 * Return the expanded version of the line. The original is deemed
3134 * to be destroyed in the process. (In reality we'll just move
3135 * Tokens from input to output a lot of the time, rather than
3136 * actually bothering to destroy and replicate.)
3138 #define DEADMAN_LIMIT (1 << 20)
3140 static Token
*expand_smacro(Token
* tline
)
3142 Token
*t
, *tt
, *mstart
, **tail
, *thead
;
3143 struct hash_table
*smtbl
;
3144 SMacro
*head
= NULL
, *m
;
3147 unsigned int nparam
, sparam
;
3148 int brackets
, rescan
;
3149 Token
*org_tline
= tline
;
3152 int deadman
= DEADMAN_LIMIT
;
3155 * Trick: we should avoid changing the start token pointer since it can
3156 * be contained in "next" field of other token. Because of this
3157 * we allocate a copy of first token and work with it; at the end of
3158 * routine we copy it back
3162 new_Token(org_tline
->next
, org_tline
->type
, org_tline
->text
,
3164 tline
->mac
= org_tline
->mac
;
3165 nasm_free(org_tline
->text
);
3166 org_tline
->text
= NULL
;
3173 while (tline
) { /* main token loop */
3175 error(ERR_NONFATAL
, "interminable macro recursion");
3179 if ((mname
= tline
->text
)) {
3180 /* if this token is a local macro, look in local context */
3183 if (tline
->type
== TOK_ID
|| tline
->type
== TOK_PREPROC_ID
) {
3184 ctx
= get_ctx(mname
, true);
3186 smtbl
= &ctx
->localmac
;
3188 head
= (SMacro
*) hash_findix(smtbl
, mname
);
3191 * We've hit an identifier. As in is_mmacro below, we first
3192 * check whether the identifier is a single-line macro at
3193 * all, then think about checking for parameters if
3196 for (m
= head
; m
; m
= m
->next
)
3197 if (!mstrcmp(m
->name
, mname
, m
->casesense
))
3203 if (m
->nparam
== 0) {
3205 * Simple case: the macro is parameterless. Discard the
3206 * one token that the macro call took, and push the
3207 * expansion back on the to-do stack.
3209 if (!m
->expansion
) {
3210 if (!strcmp("__FILE__", m
->name
)) {
3212 src_get(&num
, &(tline
->text
));
3213 nasm_quote(&(tline
->text
));
3214 tline
->type
= TOK_STRING
;
3217 if (!strcmp("__LINE__", m
->name
)) {
3218 nasm_free(tline
->text
);
3219 make_tok_num(tline
, src_get_linnum());
3222 if (!strcmp("__BITS__", m
->name
)) {
3223 nasm_free(tline
->text
);
3224 make_tok_num(tline
, globalbits
);
3227 tline
= delete_Token(tline
);
3232 * Complicated case: at least one macro with this name
3233 * exists and takes parameters. We must find the
3234 * parameters in the call, count them, find the SMacro
3235 * that corresponds to that form of the macro call, and
3236 * substitute for the parameters when we expand. What a
3239 /*tline = tline->next;
3240 skip_white_(tline); */
3243 while (tok_type_(t
, TOK_SMAC_END
)) {
3244 t
->mac
->in_progress
= false;
3246 t
= tline
->next
= delete_Token(t
);
3249 } while (tok_type_(tline
, TOK_WHITESPACE
));
3250 if (!tok_is_(tline
, "(")) {
3252 * This macro wasn't called with parameters: ignore
3253 * the call. (Behaviour borrowed from gnu cpp.)
3262 sparam
= PARAM_DELTA
;
3263 params
= nasm_malloc(sparam
* sizeof(Token
*));
3264 params
[0] = tline
->next
;
3265 paramsize
= nasm_malloc(sparam
* sizeof(int));
3267 while (true) { /* parameter loop */
3269 * For some unusual expansions
3270 * which concatenates function call
3273 while (tok_type_(t
, TOK_SMAC_END
)) {
3274 t
->mac
->in_progress
= false;
3276 t
= tline
->next
= delete_Token(t
);
3282 "macro call expects terminating `)'");
3285 if (tline
->type
== TOK_WHITESPACE
3287 if (paramsize
[nparam
])
3290 params
[nparam
] = tline
->next
;
3291 continue; /* parameter loop */
3293 if (tline
->type
== TOK_OTHER
3294 && tline
->text
[1] == 0) {
3295 char ch
= tline
->text
[0];
3296 if (ch
== ',' && !paren
&& brackets
<= 0) {
3297 if (++nparam
>= sparam
) {
3298 sparam
+= PARAM_DELTA
;
3299 params
= nasm_realloc(params
,
3304 nasm_realloc(paramsize
,
3308 params
[nparam
] = tline
->next
;
3309 paramsize
[nparam
] = 0;
3311 continue; /* parameter loop */
3314 (brackets
> 0 || (brackets
== 0 &&
3315 !paramsize
[nparam
])))
3317 if (!(brackets
++)) {
3318 params
[nparam
] = tline
->next
;
3319 continue; /* parameter loop */
3322 if (ch
== '}' && brackets
> 0)
3323 if (--brackets
== 0) {
3325 continue; /* parameter loop */
3327 if (ch
== '(' && !brackets
)
3329 if (ch
== ')' && brackets
<= 0)
3335 error(ERR_NONFATAL
, "braces do not "
3336 "enclose all of macro parameter");
3338 paramsize
[nparam
] += white
+ 1;
3340 } /* parameter loop */
3342 while (m
&& (m
->nparam
!= nparam
||
3343 mstrcmp(m
->name
, mname
,
3347 error(ERR_WARNING
| ERR_WARN_MNP
,
3348 "macro `%s' exists, "
3349 "but not taking %d parameters",
3350 mstart
->text
, nparam
);
3353 if (m
&& m
->in_progress
)
3355 if (!m
) { /* in progess or didn't find '(' or wrong nparam */
3357 * Design question: should we handle !tline, which
3358 * indicates missing ')' here, or expand those
3359 * macros anyway, which requires the (t) test a few
3363 nasm_free(paramsize
);
3367 * Expand the macro: we are placed on the last token of the
3368 * call, so that we can easily split the call from the
3369 * following tokens. We also start by pushing an SMAC_END
3370 * token for the cycle removal.
3377 tt
= new_Token(tline
, TOK_SMAC_END
, NULL
, 0);
3379 m
->in_progress
= true;
3381 for (t
= m
->expansion
; t
; t
= t
->next
) {
3382 if (t
->type
>= TOK_SMAC_PARAM
) {
3383 Token
*pcopy
= tline
, **ptail
= &pcopy
;
3387 ttt
= params
[t
->type
- TOK_SMAC_PARAM
];
3388 for (i
= paramsize
[t
->type
- TOK_SMAC_PARAM
];
3391 new_Token(tline
, ttt
->type
, ttt
->text
,
3397 } else if (t
->type
== TOK_PREPROC_Q
) {
3398 tt
= new_Token(tline
, TOK_ID
, mname
, 0);
3400 } else if (t
->type
== TOK_PREPROC_QQ
) {
3401 tt
= new_Token(tline
, TOK_ID
, m
->name
, 0);
3404 tt
= new_Token(tline
, t
->type
, t
->text
, 0);
3410 * Having done that, get rid of the macro call, and clean
3411 * up the parameters.
3414 nasm_free(paramsize
);
3416 continue; /* main token loop */
3421 if (tline
->type
== TOK_SMAC_END
) {
3422 tline
->mac
->in_progress
= false;
3423 tline
= delete_Token(tline
);
3426 tline
= tline
->next
;
3434 * Now scan the entire line and look for successive TOK_IDs that resulted
3435 * after expansion (they can't be produced by tokenize()). The successive
3436 * TOK_IDs should be concatenated.
3437 * Also we look for %+ tokens and concatenate the tokens before and after
3438 * them (without white spaces in between).
3443 while (t
&& t
->type
!= TOK_ID
&& t
->type
!= TOK_PREPROC_ID
)
3447 if (t
->next
->type
== TOK_ID
||
3448 t
->next
->type
== TOK_PREPROC_ID
||
3449 t
->next
->type
== TOK_NUMBER
) {
3450 char *p
= nasm_strcat(t
->text
, t
->next
->text
);
3452 t
->next
= delete_Token(t
->next
);
3455 } else if (t
->next
->type
== TOK_WHITESPACE
&& t
->next
->next
&&
3456 t
->next
->next
->type
== TOK_PREPROC_ID
&&
3457 strcmp(t
->next
->next
->text
, "%+") == 0) {
3458 /* free the next whitespace, the %+ token and next whitespace */
3460 for (i
= 1; i
<= 3; i
++) {
3462 || (i
!= 2 && t
->next
->type
!= TOK_WHITESPACE
))
3464 t
->next
= delete_Token(t
->next
);
3469 /* If we concatenaded something, re-scan the line for macros */
3477 *org_tline
= *thead
;
3478 /* since we just gave text to org_line, don't free it */
3480 delete_Token(thead
);
3482 /* the expression expanded to empty line;
3483 we can't return NULL for some reasons
3484 we just set the line to a single WHITESPACE token. */
3485 memset(org_tline
, 0, sizeof(*org_tline
));
3486 org_tline
->text
= NULL
;
3487 org_tline
->type
= TOK_WHITESPACE
;
3496 * Similar to expand_smacro but used exclusively with macro identifiers
3497 * right before they are fetched in. The reason is that there can be
3498 * identifiers consisting of several subparts. We consider that if there
3499 * are more than one element forming the name, user wants a expansion,
3500 * otherwise it will be left as-is. Example:
3504 * the identifier %$abc will be left as-is so that the handler for %define
3505 * will suck it and define the corresponding value. Other case:
3507 * %define _%$abc cde
3509 * In this case user wants name to be expanded *before* %define starts
3510 * working, so we'll expand %$abc into something (if it has a value;
3511 * otherwise it will be left as-is) then concatenate all successive
3514 static Token
*expand_id(Token
* tline
)
3516 Token
*cur
, *oldnext
= NULL
;
3518 if (!tline
|| !tline
->next
)
3523 (cur
->next
->type
== TOK_ID
||
3524 cur
->next
->type
== TOK_PREPROC_ID
3525 || cur
->next
->type
== TOK_NUMBER
))
3528 /* If identifier consists of just one token, don't expand */
3533 oldnext
= cur
->next
; /* Detach the tail past identifier */
3534 cur
->next
= NULL
; /* so that expand_smacro stops here */
3537 tline
= expand_smacro(tline
);
3540 /* expand_smacro possibly changhed tline; re-scan for EOL */
3542 while (cur
&& cur
->next
)
3545 cur
->next
= oldnext
;
3552 * Determine whether the given line constitutes a multi-line macro
3553 * call, and return the MMacro structure called if so. Doesn't have
3554 * to check for an initial label - that's taken care of in
3555 * expand_mmacro - but must check numbers of parameters. Guaranteed
3556 * to be called with tline->type == TOK_ID, so the putative macro
3557 * name is easy to find.
3559 static MMacro
*is_mmacro(Token
* tline
, Token
*** params_array
)
3565 head
= (MMacro
*) hash_findix(&mmacros
, tline
->text
);
3568 * Efficiency: first we see if any macro exists with the given
3569 * name. If not, we can return NULL immediately. _Then_ we
3570 * count the parameters, and then we look further along the
3571 * list if necessary to find the proper MMacro.
3573 for (m
= head
; m
; m
= m
->next
)
3574 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
3580 * OK, we have a potential macro. Count and demarcate the
3583 count_mmac_params(tline
->next
, &nparam
, ¶ms
);
3586 * So we know how many parameters we've got. Find the MMacro
3587 * structure that handles this number.
3590 if (m
->nparam_min
<= nparam
3591 && (m
->plus
|| nparam
<= m
->nparam_max
)) {
3593 * This one is right. Just check if cycle removal
3594 * prohibits us using it before we actually celebrate...
3596 if (m
->in_progress
) {
3599 "self-reference in multi-line macro `%s'", m
->name
);
3605 * It's right, and we can use it. Add its default
3606 * parameters to the end of our list if necessary.
3608 if (m
->defaults
&& nparam
< m
->nparam_min
+ m
->ndefs
) {
3610 nasm_realloc(params
,
3611 ((m
->nparam_min
+ m
->ndefs
+
3612 1) * sizeof(*params
)));
3613 while (nparam
< m
->nparam_min
+ m
->ndefs
) {
3614 params
[nparam
] = m
->defaults
[nparam
- m
->nparam_min
];
3619 * If we've gone over the maximum parameter count (and
3620 * we're in Plus mode), ignore parameters beyond
3623 if (m
->plus
&& nparam
> m
->nparam_max
)
3624 nparam
= m
->nparam_max
;
3626 * Then terminate the parameter list, and leave.
3628 if (!params
) { /* need this special case */
3629 params
= nasm_malloc(sizeof(*params
));
3632 params
[nparam
] = NULL
;
3633 *params_array
= params
;
3637 * This one wasn't right: look for the next one with the
3640 for (m
= m
->next
; m
; m
= m
->next
)
3641 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
3646 * After all that, we didn't find one with the right number of
3647 * parameters. Issue a warning, and fail to expand the macro.
3649 error(ERR_WARNING
| ERR_WARN_MNP
,
3650 "macro `%s' exists, but not taking %d parameters",
3651 tline
->text
, nparam
);
3657 * Expand the multi-line macro call made by the given line, if
3658 * there is one to be expanded. If there is, push the expansion on
3659 * istk->expansion and return 1. Otherwise return 0.
3661 static int expand_mmacro(Token
* tline
)
3663 Token
*startline
= tline
;
3664 Token
*label
= NULL
;
3665 int dont_prepend
= 0;
3666 Token
**params
, *t
, *mtok
, *tt
;
3669 int i
, nparam
, *paramlen
;
3673 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
3674 if (!tok_type_(t
, TOK_ID
) && !tok_type_(t
, TOK_PREPROC_ID
))
3677 m
= is_mmacro(t
, ¶ms
);
3681 * We have an id which isn't a macro call. We'll assume
3682 * it might be a label; we'll also check to see if a
3683 * colon follows it. Then, if there's another id after
3684 * that lot, we'll check it again for macro-hood.
3688 if (tok_type_(t
, TOK_WHITESPACE
))
3689 last
= t
, t
= t
->next
;
3690 if (tok_is_(t
, ":")) {
3692 last
= t
, t
= t
->next
;
3693 if (tok_type_(t
, TOK_WHITESPACE
))
3694 last
= t
, t
= t
->next
;
3696 if (!tok_type_(t
, TOK_ID
) || (m
= is_mmacro(t
, ¶ms
)) == NULL
)
3703 * Fix up the parameters: this involves stripping leading and
3704 * trailing whitespace, then stripping braces if they are
3707 for (nparam
= 0; params
[nparam
]; nparam
++) ;
3708 paramlen
= nparam
? nasm_malloc(nparam
* sizeof(*paramlen
)) : NULL
;
3710 for (i
= 0; params
[i
]; i
++) {
3712 int comma
= (!m
->plus
|| i
< nparam
- 1);
3716 if (tok_is_(t
, "{"))
3717 t
= t
->next
, brace
= true, comma
= false;
3721 if (comma
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, ","))
3722 break; /* ... because we have hit a comma */
3723 if (comma
&& t
->type
== TOK_WHITESPACE
3724 && tok_is_(t
->next
, ","))
3725 break; /* ... or a space then a comma */
3726 if (brace
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, "}"))
3727 break; /* ... or a brace */
3734 * OK, we have a MMacro structure together with a set of
3735 * parameters. We must now go through the expansion and push
3736 * copies of each Line on to istk->expansion. Substitution of
3737 * parameter tokens and macro-local tokens doesn't get done
3738 * until the single-line macro substitution process; this is
3739 * because delaying them allows us to change the semantics
3740 * later through %rotate.
3742 * First, push an end marker on to istk->expansion, mark this
3743 * macro as in progress, and set up its invocation-specific
3746 ll
= nasm_malloc(sizeof(Line
));
3747 ll
->next
= istk
->expansion
;
3750 istk
->expansion
= ll
;
3752 m
->in_progress
= true;
3757 m
->paramlen
= paramlen
;
3758 m
->unique
= unique
++;
3761 m
->next_active
= istk
->mstk
;
3764 for (l
= m
->expansion
; l
; l
= l
->next
) {
3767 ll
= nasm_malloc(sizeof(Line
));
3768 ll
->finishes
= NULL
;
3769 ll
->next
= istk
->expansion
;
3770 istk
->expansion
= ll
;
3773 for (t
= l
->first
; t
; t
= t
->next
) {
3777 tt
= *tail
= new_Token(NULL
, TOK_ID
, mtok
->text
, 0);
3779 case TOK_PREPROC_QQ
:
3780 tt
= *tail
= new_Token(NULL
, TOK_ID
, m
->name
, 0);
3782 case TOK_PREPROC_ID
:
3783 if (t
->text
[1] == '0' && t
->text
[2] == '0') {
3791 tt
= *tail
= new_Token(NULL
, x
->type
, x
->text
, 0);
3800 * If we had a label, push it on as the first line of
3801 * the macro expansion.
3804 if (dont_prepend
< 0)
3805 free_tlist(startline
);
3807 ll
= nasm_malloc(sizeof(Line
));
3808 ll
->finishes
= NULL
;
3809 ll
->next
= istk
->expansion
;
3810 istk
->expansion
= ll
;
3811 ll
->first
= startline
;
3812 if (!dont_prepend
) {
3814 label
= label
->next
;
3815 label
->next
= tt
= new_Token(NULL
, TOK_OTHER
, ":", 0);
3820 list
->uplevel(m
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
3826 * Since preprocessor always operate only on the line that didn't
3827 * arrived yet, we should always use ERR_OFFBY1. Also since user
3828 * won't want to see same error twice (preprocessing is done once
3829 * per pass) we will want to show errors only during pass one.
3831 static void error(int severity
, const char *fmt
, ...)
3836 /* If we're in a dead branch of IF or something like it, ignore the error */
3837 if (istk
&& istk
->conds
&& !emitting(istk
->conds
->state
))
3841 vsnprintf(buff
, sizeof(buff
), fmt
, arg
);
3844 if (istk
&& istk
->mstk
&& istk
->mstk
->name
)
3845 _error(severity
| ERR_PASS1
, "(%s:%d) %s", istk
->mstk
->name
,
3846 istk
->mstk
->lineno
, buff
);
3848 _error(severity
| ERR_PASS1
, "%s", buff
);
3852 pp_reset(char *file
, int apass
, efunc errfunc
, evalfunc eval
,
3853 ListGen
* listgen
, StrList
**deplist
)
3857 istk
= nasm_malloc(sizeof(Include
));
3860 istk
->expansion
= NULL
;
3862 istk
->fp
= fopen(file
, "r");
3864 src_set_fname(nasm_strdup(file
));
3868 error(ERR_FATAL
| ERR_NOFILE
, "unable to open input file `%s'",
3873 if (tasm_compatible_mode
) {
3874 stdmacpos
= nasm_stdmac
;
3876 stdmacpos
= nasm_stdmac_after_tasm
;
3878 any_extrastdmac
= (extrastdmac
!= NULL
);
3882 dephead
= deptail
= deplist
;
3884 StrList
*sl
= nasm_malloc(strlen(file
)+1+sizeof sl
->next
);
3886 strcpy(sl
->str
, file
);
3888 deptail
= &sl
->next
;
3892 static char *pp_getline(void)
3899 * Fetch a tokenized line, either from the macro-expansion
3900 * buffer or from the input file.
3903 while (istk
->expansion
&& istk
->expansion
->finishes
) {
3904 Line
*l
= istk
->expansion
;
3905 if (!l
->finishes
->name
&& l
->finishes
->in_progress
> 1) {
3909 * This is a macro-end marker for a macro with no
3910 * name, which means it's not really a macro at all
3911 * but a %rep block, and the `in_progress' field is
3912 * more than 1, meaning that we still need to
3913 * repeat. (1 means the natural last repetition; 0
3914 * means termination by %exitrep.) We have
3915 * therefore expanded up to the %endrep, and must
3916 * push the whole block on to the expansion buffer
3917 * again. We don't bother to remove the macro-end
3918 * marker: we'd only have to generate another one
3921 l
->finishes
->in_progress
--;
3922 for (l
= l
->finishes
->expansion
; l
; l
= l
->next
) {
3923 Token
*t
, *tt
, **tail
;
3925 ll
= nasm_malloc(sizeof(Line
));
3926 ll
->next
= istk
->expansion
;
3927 ll
->finishes
= NULL
;
3931 for (t
= l
->first
; t
; t
= t
->next
) {
3932 if (t
->text
|| t
->type
== TOK_WHITESPACE
) {
3934 new_Token(NULL
, t
->type
, t
->text
, 0);
3939 istk
->expansion
= ll
;
3943 * Check whether a `%rep' was started and not ended
3944 * within this macro expansion. This can happen and
3945 * should be detected. It's a fatal error because
3946 * I'm too confused to work out how to recover
3952 "defining with name in expansion");
3953 else if (istk
->mstk
->name
)
3955 "`%%rep' without `%%endrep' within"
3956 " expansion of macro `%s'",
3961 * FIXME: investigate the relationship at this point between
3962 * istk->mstk and l->finishes
3965 MMacro
*m
= istk
->mstk
;
3966 istk
->mstk
= m
->next_active
;
3969 * This was a real macro call, not a %rep, and
3970 * therefore the parameter information needs to
3973 nasm_free(m
->params
);
3974 free_tlist(m
->iline
);
3975 nasm_free(m
->paramlen
);
3976 l
->finishes
->in_progress
= false;
3980 istk
->expansion
= l
->next
;
3982 list
->downlevel(LIST_MACRO
);
3985 while (1) { /* until we get a line we can use */
3987 if (istk
->expansion
) { /* from a macro expansion */
3989 Line
*l
= istk
->expansion
;
3991 istk
->mstk
->lineno
++;
3993 istk
->expansion
= l
->next
;
3995 p
= detoken(tline
, false);
3996 list
->line(LIST_MACRO
, p
);
4001 if (line
) { /* from the current input file */
4002 line
= prepreproc(line
);
4003 tline
= tokenize(line
);
4008 * The current file has ended; work down the istk
4015 "expected `%%endif' before end of file");
4016 /* only set line and file name if there's a next node */
4018 src_set_linnum(i
->lineno
);
4019 nasm_free(src_set_fname(i
->fname
));
4022 list
->downlevel(LIST_INCLUDE
);
4030 * We must expand MMacro parameters and MMacro-local labels
4031 * _before_ we plunge into directive processing, to cope
4032 * with things like `%define something %1' such as STRUC
4033 * uses. Unless we're _defining_ a MMacro, in which case
4034 * those tokens should be left alone to go into the
4035 * definition; and unless we're in a non-emitting
4036 * condition, in which case we don't want to meddle with
4039 if (!defining
&& !(istk
->conds
&& !emitting(istk
->conds
->state
)))
4040 tline
= expand_mmac_params(tline
);
4043 * Check the line to see if it's a preprocessor directive.
4045 if (do_directive(tline
) == DIRECTIVE_FOUND
) {
4047 } else if (defining
) {
4049 * We're defining a multi-line macro. We emit nothing
4051 * shove the tokenized line on to the macro definition.
4053 Line
*l
= nasm_malloc(sizeof(Line
));
4054 l
->next
= defining
->expansion
;
4056 l
->finishes
= false;
4057 defining
->expansion
= l
;
4059 } else if (istk
->conds
&& !emitting(istk
->conds
->state
)) {
4061 * We're in a non-emitting branch of a condition block.
4062 * Emit nothing at all, not even a blank line: when we
4063 * emerge from the condition we'll give a line-number
4064 * directive so we keep our place correctly.
4068 } else if (istk
->mstk
&& !istk
->mstk
->in_progress
) {
4070 * We're in a %rep block which has been terminated, so
4071 * we're walking through to the %endrep without
4072 * emitting anything. Emit nothing at all, not even a
4073 * blank line: when we emerge from the %rep block we'll
4074 * give a line-number directive so we keep our place
4080 tline
= expand_smacro(tline
);
4081 if (!expand_mmacro(tline
)) {
4083 * De-tokenize the line again, and emit it.
4085 line
= detoken(tline
, true);
4089 continue; /* expand_mmacro calls free_tlist */
4097 static void pp_cleanup(int pass
)
4100 error(ERR_NONFATAL
, "end of file while still defining macro `%s'",
4102 free_mmacro(defining
);
4111 nasm_free(i
->fname
);
4122 void pp_include_path(char *path
)
4126 i
= nasm_malloc(sizeof(IncPath
));
4127 i
->path
= path
? nasm_strdup(path
) : NULL
;
4130 if (ipath
!= NULL
) {
4132 while (j
->next
!= NULL
)
4140 void pp_pre_include(char *fname
)
4142 Token
*inc
, *space
, *name
;
4145 name
= new_Token(NULL
, TOK_INTERNAL_STRING
, fname
, 0);
4146 space
= new_Token(name
, TOK_WHITESPACE
, NULL
, 0);
4147 inc
= new_Token(space
, TOK_PREPROC_ID
, "%include", 0);
4149 l
= nasm_malloc(sizeof(Line
));
4152 l
->finishes
= false;
4156 void pp_pre_define(char *definition
)
4162 equals
= strchr(definition
, '=');
4163 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
4164 def
= new_Token(space
, TOK_PREPROC_ID
, "%define", 0);
4167 space
->next
= tokenize(definition
);
4171 l
= nasm_malloc(sizeof(Line
));
4174 l
->finishes
= false;
4178 void pp_pre_undefine(char *definition
)
4183 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
4184 def
= new_Token(space
, TOK_PREPROC_ID
, "%undef", 0);
4185 space
->next
= tokenize(definition
);
4187 l
= nasm_malloc(sizeof(Line
));
4190 l
->finishes
= false;
4195 * Added by Keith Kanios:
4197 * This function is used to assist with "runtime" preprocessor
4198 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4200 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4201 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4204 void pp_runtime(char *definition
)
4208 def
= tokenize(definition
);
4209 if(do_directive(def
) == NO_DIRECTIVE_FOUND
)
4214 void pp_extra_stdmac(const char **macros
)
4216 extrastdmac
= macros
;
4219 static void make_tok_num(Token
* tok
, int64_t val
)
4222 snprintf(numbuf
, sizeof(numbuf
), "%"PRId64
"", val
);
4223 tok
->text
= nasm_strdup(numbuf
);
4224 tok
->type
= TOK_NUMBER
;