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 licence given in the file "Licence"
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
52 typedef struct SMacro SMacro
;
53 typedef struct MMacro MMacro
;
54 typedef struct Context Context
;
55 typedef struct Token Token
;
56 typedef struct Blocks Blocks
;
57 typedef struct Line Line
;
58 typedef struct Include Include
;
59 typedef struct Cond Cond
;
60 typedef struct IncPath IncPath
;
63 * Note on the storage of both SMacro and MMacros: the hash table
64 * indexes them case-insensitively, and we then have to go through a
65 * linked list of potential case aliases (and, for MMacros, parameter
66 * ranges); this is to preserve the matching semantics of the earlier
67 * code. If the number of case aliases for a specific macro is a
68 * performance issue, you may want to reconsider your coding style.
72 * Store the definition of a single-line macro.
84 * Store the definition of a multi-line macro. This is also used to
85 * store the interiors of `%rep...%endrep' blocks, which are
86 * effectively self-re-invoking multi-line macros which simply
87 * don't have a name or bother to appear in the hash tables. %rep
88 * blocks are signified by having a NULL `name' field.
90 * In a MMacro describing a `%rep' block, the `in_progress' field
91 * isn't merely boolean, but gives the number of repeats left to
94 * The `next' field is used for storing MMacros in hash tables; the
95 * `next_active' field is for stacking them on istk entries.
97 * When a MMacro is being expanded, `params', `iline', `nparam',
98 * `paramlen', `rotate' and `unique' are local to the invocation.
103 int nparam_min
, nparam_max
;
105 bool plus
; /* is the last parameter greedy? */
106 bool nolist
; /* is this macro listing-inhibited? */
108 Token
*dlist
; /* All defaults as one list */
109 Token
**defaults
; /* Parameter default pointers */
110 int ndefs
; /* number of default parameters */
114 MMacro
*rep_nest
; /* used for nesting %rep */
115 Token
**params
; /* actual parameters */
116 Token
*iline
; /* invocation line */
117 unsigned int nparam
, rotate
;
120 int lineno
; /* Current line number on expansion */
124 * The context stack is composed of a linked list of these.
134 * This is the internal form which we break input lines up into.
135 * Typically stored in linked lists.
137 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
138 * necessarily used as-is, but is intended to denote the number of
139 * the substituted parameter. So in the definition
141 * %define a(x,y) ( (x) & ~(y) )
143 * the token representing `x' will have its type changed to
144 * TOK_SMAC_PARAM, but the one representing `y' will be
147 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
148 * which doesn't need quotes around it. Used in the pre-include
149 * mechanism as an alternative to trying to find a sensible type of
150 * quote to use on the filename we were passed.
153 TOK_NONE
= 0, TOK_WHITESPACE
, TOK_COMMENT
, TOK_ID
,
154 TOK_PREPROC_ID
, TOK_STRING
,
155 TOK_NUMBER
, TOK_SMAC_END
, TOK_OTHER
, TOK_SMAC_PARAM
,
162 SMacro
*mac
; /* associated macro for TOK_SMAC_END */
163 enum pp_token_type type
;
167 * Multi-line macro definitions are stored as a linked list of
168 * these, which is essentially a container to allow several linked
171 * Note that in this module, linked lists are treated as stacks
172 * wherever possible. For this reason, Lines are _pushed_ on to the
173 * `expansion' field in MMacro structures, so that the linked list,
174 * if walked, would give the macro lines in reverse order; this
175 * means that we can walk the list when expanding a macro, and thus
176 * push the lines on to the `expansion' field in _istk_ in reverse
177 * order (so that when popped back off they are in the right
178 * order). It may seem cockeyed, and it relies on my design having
179 * an even number of steps in, but it works...
181 * Some of these structures, rather than being actual lines, are
182 * markers delimiting the end of the expansion of a given macro.
183 * This is for use in the cycle-tracking and %rep-handling code.
184 * Such structures have `finishes' non-NULL, and `first' NULL. All
185 * others have `finishes' NULL, but `first' may still be NULL if
195 * To handle an arbitrary level of file inclusion, we maintain a
196 * stack (ie linked list) of these things.
205 MMacro
*mstk
; /* stack of active macros/reps */
209 * Include search path. This is simply a list of strings which get
210 * prepended, in turn, to the name of an include file, in an
211 * attempt to find the file if it's not in the current directory.
219 * Conditional assembly: we maintain a separate stack of these for
220 * each level of file inclusion. (The only reason we keep the
221 * stacks separate is to ensure that a stray `%endif' in a file
222 * included from within the true branch of a `%if' won't terminate
223 * it and cause confusion: instead, rightly, it'll cause an error.)
231 * These states are for use just after %if or %elif: IF_TRUE
232 * means the condition has evaluated to truth so we are
233 * currently emitting, whereas IF_FALSE means we are not
234 * currently emitting but will start doing so if a %else comes
235 * up. In these states, all directives are admissible: %elif,
236 * %else and %endif. (And of course %if.)
238 COND_IF_TRUE
, COND_IF_FALSE
,
240 * These states come up after a %else: ELSE_TRUE means we're
241 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
242 * any %elif or %else will cause an error.
244 COND_ELSE_TRUE
, COND_ELSE_FALSE
,
246 * This state means that we're not emitting now, and also that
247 * nothing until %endif will be emitted at all. It's for use in
248 * two circumstances: (i) when we've had our moment of emission
249 * and have now started seeing %elifs, and (ii) when the
250 * condition construct in question is contained within a
251 * non-emitting branch of a larger condition construct.
255 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
258 * These defines are used as the possible return values for do_directive
260 #define NO_DIRECTIVE_FOUND 0
261 #define DIRECTIVE_FOUND 1
264 * Condition codes. Note that we use c_ prefix not C_ because C_ is
265 * used in nasm.h for the "real" condition codes. At _this_ level,
266 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
267 * ones, so we need a different enum...
269 static const char * const conditions
[] = {
270 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
271 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
272 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
275 c_A
, c_AE
, c_B
, c_BE
, c_C
, c_CXZ
, c_E
, c_ECXZ
, c_G
, c_GE
, c_L
, c_LE
,
276 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, c_NE
, c_NG
, c_NGE
, c_NL
, c_NLE
, c_NO
,
277 c_NP
, c_NS
, c_NZ
, c_O
, c_P
, c_PE
, c_PO
, c_RCXZ
, c_S
, c_Z
,
280 static const enum pp_conds inverse_ccs
[] = {
281 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, -1, c_NE
, -1, c_NG
, c_NGE
, c_NL
, c_NLE
,
282 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
,
283 c_Z
, c_NO
, c_NP
, c_PO
, c_PE
, -1, c_NS
, c_NZ
289 /* If this is a an IF, ELIF, ELSE or ENDIF keyword */
290 static int is_condition(enum preproc_token arg
)
292 return PP_IS_COND(arg
) || (arg
== PP_ELSE
) || (arg
== PP_ENDIF
);
295 /* For TASM compatibility we need to be able to recognise TASM compatible
296 * conditional compilation directives. Using the NASM pre-processor does
297 * not work, so we look for them specifically from the following list and
298 * then jam in the equivalent NASM directive into the input stream.
302 # define MAX(a,b) ( ((a) > (b)) ? (a) : (b))
306 TM_ARG
, TM_ELIF
, TM_ELSE
, TM_ENDIF
, TM_IF
, TM_IFDEF
, TM_IFDIFI
,
307 TM_IFNDEF
, TM_INCLUDE
, TM_LOCAL
310 static const char * const tasm_directives
[] = {
311 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
312 "ifndef", "include", "local"
315 static int StackSize
= 4;
316 static char *StackPointer
= "ebp";
317 static int ArgOffset
= 8;
318 static int LocalOffset
= 4;
320 static Context
*cstk
;
321 static Include
*istk
;
322 static IncPath
*ipath
= NULL
;
324 static efunc _error
; /* Pointer to client-provided error reporting function */
325 static evalfunc evaluate
;
327 static int pass
; /* HACK: pass 0 = generate dependencies only */
329 static uint64_t unique
; /* unique identifier numbers */
331 static Line
*predef
= NULL
;
333 static ListGen
*list
;
336 * The current set of multi-line macros we have defined.
338 static struct hash_table
*mmacros
;
341 * The current set of single-line macros we have defined.
343 static struct hash_table
*smacros
;
346 * The multi-line macro we are currently defining, or the %rep
347 * block we are currently reading, if any.
349 static MMacro
*defining
;
352 * The number of macro parameters to allocate space for at a time.
354 #define PARAM_DELTA 16
357 * The standard macro set: defined as `static char *stdmac[]'. Also
358 * gives our position in the macro set, when we're processing it.
361 static const char **stdmacpos
;
364 * The extra standard macros that come from the object format, if
367 static const char **extrastdmac
= NULL
;
368 bool any_extrastdmac
;
371 * Tokens are allocated in blocks to improve speed
373 #define TOKEN_BLOCKSIZE 4096
374 static Token
*freeTokens
= NULL
;
380 static Blocks blocks
= { NULL
, NULL
};
383 * Forward declarations.
385 static Token
*expand_mmac_params(Token
* tline
);
386 static Token
*expand_smacro(Token
* tline
);
387 static Token
*expand_id(Token
* tline
);
388 static Context
*get_ctx(char *name
, bool all_contexts
);
389 static void make_tok_num(Token
* tok
, int64_t val
);
390 static void error(int severity
, const char *fmt
, ...);
391 static void *new_Block(size_t size
);
392 static void delete_Blocks(void);
393 static Token
*new_Token(Token
* next
, enum pp_token_type type
, char *text
, int txtlen
);
394 static Token
*delete_Token(Token
* t
);
397 * Macros for safe checking of token pointers, avoid *(NULL)
399 #define tok_type_(x,t) ((x) && (x)->type == (t))
400 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
401 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
402 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
404 /* Handle TASM specific directives, which do not contain a % in
405 * front of them. We do it here because I could not find any other
406 * place to do it for the moment, and it is a hack (ideally it would
407 * be nice to be able to use the NASM pre-processor to do it).
409 static char *check_tasm_directive(char *line
)
411 int32_t i
, j
, k
, m
, len
;
412 char *p
= line
, *oldline
, oldchar
;
414 /* Skip whitespace */
415 while (isspace(*p
) && *p
!= 0)
418 /* Binary search for the directive name */
420 j
= elements(tasm_directives
);
422 while (!isspace(p
[len
]) && p
[len
] != 0)
429 m
= nasm_stricmp(p
, tasm_directives
[k
]);
431 /* We have found a directive, so jam a % in front of it
432 * so that NASM will then recognise it as one if it's own.
437 line
= nasm_malloc(len
+ 2);
439 if (k
== TM_IFDIFI
) {
440 /* NASM does not recognise IFDIFI, so we convert it to
441 * %ifdef BOGUS. This is not used in NASM comaptible
442 * code, but does need to parse for the TASM macro
445 strcpy(line
+ 1, "ifdef BOGUS");
447 memcpy(line
+ 1, p
, len
+ 1);
462 * The pre-preprocessing stage... This function translates line
463 * number indications as they emerge from GNU cpp (`# lineno "file"
464 * flags') into NASM preprocessor line number indications (`%line
467 static char *prepreproc(char *line
)
470 char *fname
, *oldline
;
472 if (line
[0] == '#' && line
[1] == ' ') {
475 lineno
= atoi(fname
);
476 fname
+= strspn(fname
, "0123456789 ");
479 fnlen
= strcspn(fname
, "\"");
480 line
= nasm_malloc(20 + fnlen
);
481 snprintf(line
, 20 + fnlen
, "%%line %d %.*s", lineno
, fnlen
, fname
);
484 if (tasm_compatible_mode
)
485 return check_tasm_directive(line
);
490 * Free a linked list of tokens.
492 static void free_tlist(Token
* list
)
495 list
= delete_Token(list
);
500 * Free a linked list of lines.
502 static void free_llist(Line
* list
)
508 free_tlist(l
->first
);
516 static void free_mmacro(MMacro
* m
)
519 free_tlist(m
->dlist
);
520 nasm_free(m
->defaults
);
521 free_llist(m
->expansion
);
526 * Free all currently defined macros, and free the hash tables
528 static void free_macros(void)
530 struct hash_tbl_node
*it
;
536 while ((s
= hash_iterate(smacros
, &it
, &key
)) != NULL
) {
537 nasm_free((void *)key
);
539 SMacro
*ns
= s
->next
;
541 free_tlist(s
->expansion
);
549 while ((m
= hash_iterate(mmacros
, &it
, &key
)) != NULL
) {
550 nasm_free((void *)key
);
552 MMacro
*nm
= m
->next
;
561 * Initialize the hash tables
563 static void init_macros(void)
565 smacros
= hash_init();
566 mmacros
= hash_init();
570 * Pop the context stack.
572 static void ctx_pop(void)
583 free_tlist(s
->expansion
);
590 #define BUF_DELTA 512
592 * Read a line from the top file in istk, handling multiple CR/LFs
593 * at the end of the line read, and handling spurious ^Zs. Will
594 * return lines from the standard macro set if this has not already
597 static char *read_line(void)
599 char *buffer
, *p
, *q
;
600 int bufsize
, continued_count
;
604 char *ret
= nasm_strdup(*stdmacpos
++);
605 if (!*stdmacpos
&& any_extrastdmac
) {
606 stdmacpos
= extrastdmac
;
607 any_extrastdmac
= false;
611 * Nasty hack: here we push the contents of `predef' on
612 * to the top-level expansion stack, since this is the
613 * most convenient way to implement the pre-include and
614 * pre-define features.
618 Token
*head
, **tail
, *t
;
620 for (pd
= predef
; pd
; pd
= pd
->next
) {
623 for (t
= pd
->first
; t
; t
= t
->next
) {
624 *tail
= new_Token(NULL
, t
->type
, t
->text
, 0);
625 tail
= &(*tail
)->next
;
627 l
= nasm_malloc(sizeof(Line
));
628 l
->next
= istk
->expansion
;
641 buffer
= nasm_malloc(BUF_DELTA
);
645 q
= fgets(p
, bufsize
- (p
- buffer
), istk
->fp
);
649 if (p
> buffer
&& p
[-1] == '\n') {
650 /* Convert backslash-CRLF line continuation sequences into
651 nothing at all (for DOS and Windows) */
652 if (((p
- 2) > buffer
) && (p
[-3] == '\\') && (p
[-2] == '\r')) {
657 /* Also convert backslash-LF line continuation sequences into
658 nothing at all (for Unix) */
659 else if (((p
- 1) > buffer
) && (p
[-2] == '\\')) {
667 if (p
- buffer
> bufsize
- 10) {
668 int32_t offset
= p
- buffer
;
669 bufsize
+= BUF_DELTA
;
670 buffer
= nasm_realloc(buffer
, bufsize
);
671 p
= buffer
+ offset
; /* prevent stale-pointer problems */
675 if (!q
&& p
== buffer
) {
680 src_set_linnum(src_get_linnum() + istk
->lineinc
+
681 (continued_count
* istk
->lineinc
));
684 * Play safe: remove CRs as well as LFs, if any of either are
685 * present at the end of the line.
687 while (--p
>= buffer
&& (*p
== '\n' || *p
== '\r'))
691 * Handle spurious ^Z, which may be inserted into source files
692 * by some file transfer utilities.
694 buffer
[strcspn(buffer
, "\032")] = '\0';
696 list
->line(LIST_READ
, buffer
);
702 * Tokenize a line of text. This is a very simple process since we
703 * don't need to parse the value out of e.g. numeric tokens: we
704 * simply split one string into many.
706 static Token
*tokenize(char *line
)
709 enum pp_token_type type
;
711 Token
*t
, **tail
= &list
;
718 ((*p
== '-' || *p
== '+') && isdigit(p
[1])) ||
719 ((*p
== '+') && (isspace(p
[1]) || !p
[1]))) {
724 type
= TOK_PREPROC_ID
;
725 } else if (*p
== '{') {
727 while (*p
&& *p
!= '}') {
734 type
= TOK_PREPROC_ID
;
735 } else if (isidchar(*p
) ||
736 ((*p
== '!' || *p
== '%' || *p
== '$') &&
741 while (isidchar(*p
));
742 type
= TOK_PREPROC_ID
;
748 } else if (isidstart(*p
) || (*p
== '$' && isidstart(p
[1]))) {
751 while (*p
&& isidchar(*p
))
753 } else if (*p
== '\'' || *p
== '"') {
760 while (*p
&& *p
!= c
)
766 error(ERR_WARNING
, "unterminated string");
767 /* Handling unterminated strings by UNV */
770 } else if (isnumstart(*p
)) {
776 while (*p
&& isnumchar(*p
))
778 } else if (isspace(*p
)) {
779 type
= TOK_WHITESPACE
;
781 while (*p
&& isspace(*p
))
784 * Whitespace just before end-of-line is discarded by
785 * pretending it's a comment; whitespace just before a
786 * comment gets lumped into the comment.
788 if (!*p
|| *p
== ';') {
793 } else if (*p
== ';') {
799 * Anything else is an operator of some kind. We check
800 * for all the double-character operators (>>, <<, //,
801 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
802 * else is a single-character operator.
805 if ((p
[0] == '>' && p
[1] == '>') ||
806 (p
[0] == '<' && p
[1] == '<') ||
807 (p
[0] == '/' && p
[1] == '/') ||
808 (p
[0] == '<' && p
[1] == '=') ||
809 (p
[0] == '>' && p
[1] == '=') ||
810 (p
[0] == '=' && p
[1] == '=') ||
811 (p
[0] == '!' && p
[1] == '=') ||
812 (p
[0] == '<' && p
[1] == '>') ||
813 (p
[0] == '&' && p
[1] == '&') ||
814 (p
[0] == '|' && p
[1] == '|') ||
815 (p
[0] == '^' && p
[1] == '^')) {
821 /* Handling unterminated string by UNV */
824 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
825 t->text[p-line] = *line;
829 if (type
!= TOK_COMMENT
) {
830 *tail
= t
= new_Token(NULL
, type
, line
, p
- line
);
839 * this function allocates a new managed block of memory and
840 * returns a pointer to the block. The managed blocks are
841 * deleted only all at once by the delete_Blocks function.
843 static void *new_Block(size_t size
)
847 /* first, get to the end of the linked list */
850 /* now allocate the requested chunk */
851 b
->chunk
= nasm_malloc(size
);
853 /* now allocate a new block for the next request */
854 b
->next
= nasm_malloc(sizeof(Blocks
));
855 /* and initialize the contents of the new block */
856 b
->next
->next
= NULL
;
857 b
->next
->chunk
= NULL
;
862 * this function deletes all managed blocks of memory
864 static void delete_Blocks(void)
866 Blocks
*a
, *b
= &blocks
;
869 * keep in mind that the first block, pointed to by blocks
870 * is a static and not dynamically allocated, so we don't
884 * this function creates a new Token and passes a pointer to it
885 * back to the caller. It sets the type and text elements, and
886 * also the mac and next elements to NULL.
888 static Token
*new_Token(Token
* next
, enum pp_token_type type
, char *text
, int txtlen
)
893 if (freeTokens
== NULL
) {
894 freeTokens
= (Token
*) new_Block(TOKEN_BLOCKSIZE
* sizeof(Token
));
895 for (i
= 0; i
< TOKEN_BLOCKSIZE
- 1; i
++)
896 freeTokens
[i
].next
= &freeTokens
[i
+ 1];
897 freeTokens
[i
].next
= NULL
;
900 freeTokens
= t
->next
;
904 if (type
== TOK_WHITESPACE
|| text
== NULL
) {
908 txtlen
= strlen(text
);
909 t
->text
= nasm_malloc(1 + txtlen
);
910 strncpy(t
->text
, text
, txtlen
);
911 t
->text
[txtlen
] = '\0';
916 static Token
*delete_Token(Token
* t
)
918 Token
*next
= t
->next
;
920 t
->next
= freeTokens
;
926 * Convert a line of tokens back into text.
927 * If expand_locals is not zero, identifiers of the form "%$*xxx"
928 * will be transformed into ..@ctxnum.xxx
930 static char *detoken(Token
* tlist
, int expand_locals
)
937 for (t
= tlist
; t
; t
= t
->next
) {
938 if (t
->type
== TOK_PREPROC_ID
&& t
->text
[1] == '!') {
939 char *p
= getenv(t
->text
+ 2);
942 t
->text
= nasm_strdup(p
);
946 /* Expand local macros here and not during preprocessing */
948 t
->type
== TOK_PREPROC_ID
&& t
->text
&&
949 t
->text
[0] == '%' && t
->text
[1] == '$') {
950 Context
*ctx
= get_ctx(t
->text
, false);
953 char *p
, *q
= t
->text
+ 2;
956 snprintf(buffer
, sizeof(buffer
), "..@%"PRIu32
".", ctx
->number
);
957 p
= nasm_strcat(buffer
, q
);
962 if (t
->type
== TOK_WHITESPACE
) {
964 } else if (t
->text
) {
965 len
+= strlen(t
->text
);
968 p
= line
= nasm_malloc(len
+ 1);
969 for (t
= tlist
; t
; t
= t
->next
) {
970 if (t
->type
== TOK_WHITESPACE
) {
974 } else if (t
->text
) {
984 * A scanner, suitable for use by the expression evaluator, which
985 * operates on a line of Tokens. Expects a pointer to a pointer to
986 * the first token in the line to be passed in as its private_data
989 static int ppscan(void *private_data
, struct tokenval
*tokval
)
991 Token
**tlineptr
= private_data
;
996 *tlineptr
= tline
? tline
->next
: NULL
;
998 while (tline
&& (tline
->type
== TOK_WHITESPACE
||
999 tline
->type
== TOK_COMMENT
));
1002 return tokval
->t_type
= TOKEN_EOS
;
1004 if (tline
->text
[0] == '$' && !tline
->text
[1])
1005 return tokval
->t_type
= TOKEN_HERE
;
1006 if (tline
->text
[0] == '$' && tline
->text
[1] == '$' && !tline
->text
[2])
1007 return tokval
->t_type
= TOKEN_BASE
;
1009 if (tline
->type
== TOK_ID
) {
1010 tokval
->t_charptr
= tline
->text
;
1011 if (tline
->text
[0] == '$') {
1012 tokval
->t_charptr
++;
1013 return tokval
->t_type
= TOKEN_ID
;
1017 * This is the only special case we actually need to worry
1018 * about in this restricted context.
1020 if (!nasm_stricmp(tline
->text
, "seg"))
1021 return tokval
->t_type
= TOKEN_SEG
;
1023 return tokval
->t_type
= TOKEN_ID
;
1026 if (tline
->type
== TOK_NUMBER
) {
1029 tokval
->t_integer
= readnum(tline
->text
, &rn_error
);
1031 return tokval
->t_type
= TOKEN_ERRNUM
;
1032 tokval
->t_charptr
= NULL
;
1033 return tokval
->t_type
= TOKEN_NUM
;
1036 if (tline
->type
== TOK_STRING
) {
1045 if (l
== 0 || r
[l
- 1] != q
)
1046 return tokval
->t_type
= TOKEN_ERRNUM
;
1047 tokval
->t_integer
= readstrnum(r
, l
- 1, &rn_warn
);
1049 error(ERR_WARNING
| ERR_PASS1
, "character constant too long");
1050 tokval
->t_charptr
= NULL
;
1051 return tokval
->t_type
= TOKEN_NUM
;
1054 if (tline
->type
== TOK_OTHER
) {
1055 if (!strcmp(tline
->text
, "<<"))
1056 return tokval
->t_type
= TOKEN_SHL
;
1057 if (!strcmp(tline
->text
, ">>"))
1058 return tokval
->t_type
= TOKEN_SHR
;
1059 if (!strcmp(tline
->text
, "//"))
1060 return tokval
->t_type
= TOKEN_SDIV
;
1061 if (!strcmp(tline
->text
, "%%"))
1062 return tokval
->t_type
= TOKEN_SMOD
;
1063 if (!strcmp(tline
->text
, "=="))
1064 return tokval
->t_type
= TOKEN_EQ
;
1065 if (!strcmp(tline
->text
, "<>"))
1066 return tokval
->t_type
= TOKEN_NE
;
1067 if (!strcmp(tline
->text
, "!="))
1068 return tokval
->t_type
= TOKEN_NE
;
1069 if (!strcmp(tline
->text
, "<="))
1070 return tokval
->t_type
= TOKEN_LE
;
1071 if (!strcmp(tline
->text
, ">="))
1072 return tokval
->t_type
= TOKEN_GE
;
1073 if (!strcmp(tline
->text
, "&&"))
1074 return tokval
->t_type
= TOKEN_DBL_AND
;
1075 if (!strcmp(tline
->text
, "^^"))
1076 return tokval
->t_type
= TOKEN_DBL_XOR
;
1077 if (!strcmp(tline
->text
, "||"))
1078 return tokval
->t_type
= TOKEN_DBL_OR
;
1082 * We have no other options: just return the first character of
1085 return tokval
->t_type
= tline
->text
[0];
1089 * Compare a string to the name of an existing macro; this is a
1090 * simple wrapper which calls either strcmp or nasm_stricmp
1091 * depending on the value of the `casesense' parameter.
1093 static int mstrcmp(const char *p
, const char *q
, bool casesense
)
1095 return casesense
? strcmp(p
, q
) : nasm_stricmp(p
, q
);
1099 * Return the Context structure associated with a %$ token. Return
1100 * NULL, having _already_ reported an error condition, if the
1101 * context stack isn't deep enough for the supplied number of $
1103 * If all_contexts == true, contexts that enclose current are
1104 * also scanned for such smacro, until it is found; if not -
1105 * only the context that directly results from the number of $'s
1106 * in variable's name.
1108 static Context
*get_ctx(char *name
, bool all_contexts
)
1114 if (!name
|| name
[0] != '%' || name
[1] != '$')
1118 error(ERR_NONFATAL
, "`%s': context stack is empty", name
);
1122 for (i
= strspn(name
+ 2, "$"), ctx
= cstk
; (i
> 0) && ctx
; i
--) {
1124 /* i--; Lino - 02/25/02 */
1127 error(ERR_NONFATAL
, "`%s': context stack is only"
1128 " %d level%s deep", name
, i
- 1, (i
== 2 ? "" : "s"));
1135 /* Search for this smacro in found context */
1138 if (!mstrcmp(m
->name
, name
, m
->casesense
))
1149 * Open an include file. This routine must always return a valid
1150 * file pointer if it returns - it's responsible for throwing an
1151 * ERR_FATAL and bombing out completely if not. It should also try
1152 * the include path one by one until it finds the file or reaches
1153 * the end of the path.
1155 static FILE *inc_fopen(char *file
)
1158 char *prefix
= "", *combine
;
1159 IncPath
*ip
= ipath
;
1160 static int namelen
= 0;
1161 int len
= strlen(file
);
1164 combine
= nasm_malloc(strlen(prefix
) + len
+ 1);
1165 strcpy(combine
, prefix
);
1166 strcat(combine
, file
);
1167 fp
= fopen(combine
, "r");
1168 if (pass
== 0 && fp
) {
1169 namelen
+= strlen(combine
) + 1;
1174 printf(" %s", combine
);
1185 /* -MG given and file not found */
1187 namelen
+= strlen(file
) + 1;
1192 printf(" %s", file
);
1198 error(ERR_FATAL
, "unable to open include file `%s'", file
);
1199 return NULL
; /* never reached - placate compilers */
1203 * Search for a key in the hash index; adding it if necessary
1204 * (in which case we initialize the data pointer to NULL.)
1207 hash_findi_add(struct hash_table
*hash
, const char *str
)
1209 struct hash_insert hi
;
1213 r
= hash_findi(hash
, str
, &hi
);
1217 strx
= nasm_strdup(str
); /* Use a more efficient allocator here? */
1218 return hash_add(&hi
, strx
, NULL
);
1222 * Like hash_findi, but returns the data element rather than a pointer
1223 * to it. Used only when not adding a new element, hence no third
1227 hash_findix(struct hash_table
*hash
, const char *str
)
1231 p
= hash_findi(hash
, str
, NULL
);
1232 return p
? *p
: NULL
;
1236 * Determine if we should warn on defining a single-line macro of
1237 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1238 * return true if _any_ single-line macro of that name is defined.
1239 * Otherwise, will return true if a single-line macro with either
1240 * `nparam' or no parameters is defined.
1242 * If a macro with precisely the right number of parameters is
1243 * defined, or nparam is -1, the address of the definition structure
1244 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1245 * is NULL, no action will be taken regarding its contents, and no
1248 * Note that this is also called with nparam zero to resolve
1251 * If you already know which context macro belongs to, you can pass
1252 * the context pointer as first parameter; if you won't but name begins
1253 * with %$ the context will be automatically computed. If all_contexts
1254 * is true, macro will be searched in outer contexts as well.
1257 smacro_defined(Context
* ctx
, char *name
, int nparam
, SMacro
** defn
,
1264 } else if (name
[0] == '%' && name
[1] == '$') {
1266 ctx
= get_ctx(name
, false);
1268 return false; /* got to return _something_ */
1271 m
= (SMacro
*) hash_findix(smacros
, name
);
1275 if (!mstrcmp(m
->name
, name
, m
->casesense
&& nocase
) &&
1276 (nparam
<= 0 || m
->nparam
== 0 || nparam
== m
->nparam
)) {
1278 if (nparam
== m
->nparam
|| nparam
== -1)
1292 * Count and mark off the parameters in a multi-line macro call.
1293 * This is called both from within the multi-line macro expansion
1294 * code, and also to mark off the default parameters when provided
1295 * in a %macro definition line.
1297 static void count_mmac_params(Token
* t
, int *nparam
, Token
*** params
)
1299 int paramsize
, brace
;
1301 *nparam
= paramsize
= 0;
1304 if (*nparam
>= paramsize
) {
1305 paramsize
+= PARAM_DELTA
;
1306 *params
= nasm_realloc(*params
, sizeof(**params
) * paramsize
);
1310 if (tok_is_(t
, "{"))
1312 (*params
)[(*nparam
)++] = t
;
1313 while (tok_isnt_(t
, brace
? "}" : ","))
1315 if (t
) { /* got a comma/brace */
1319 * Now we've found the closing brace, look further
1323 if (tok_isnt_(t
, ",")) {
1325 "braces do not enclose all of macro parameter");
1326 while (tok_isnt_(t
, ","))
1330 t
= t
->next
; /* eat the comma */
1337 * Determine whether one of the various `if' conditions is true or
1340 * We must free the tline we get passed.
1342 static bool if_condition(Token
* tline
, enum preproc_token ct
)
1344 enum pp_conditional i
= PP_COND(ct
);
1346 Token
*t
, *tt
, **tptr
, *origline
;
1347 struct tokenval tokval
;
1349 enum pp_token_type needtype
;
1355 j
= false; /* have we matched yet? */
1356 while (cstk
&& tline
) {
1358 if (!tline
|| tline
->type
!= TOK_ID
) {
1360 "`%s' expects context identifiers", pp_directives
[ct
]);
1361 free_tlist(origline
);
1364 if (!nasm_stricmp(tline
->text
, cstk
->name
))
1366 tline
= tline
->next
;
1371 j
= false; /* have we matched yet? */
1374 if (!tline
|| (tline
->type
!= TOK_ID
&&
1375 (tline
->type
!= TOK_PREPROC_ID
||
1376 tline
->text
[1] != '$'))) {
1378 "`%s' expects macro identifiers", pp_directives
[ct
]);
1381 if (smacro_defined(NULL
, tline
->text
, 0, NULL
, true))
1383 tline
= tline
->next
;
1389 tline
= expand_smacro(tline
);
1391 while (tok_isnt_(tt
, ","))
1395 "`%s' expects two comma-separated arguments",
1400 j
= true; /* assume equality unless proved not */
1401 while ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) && tt
) {
1402 if (tt
->type
== TOK_OTHER
&& !strcmp(tt
->text
, ",")) {
1403 error(ERR_NONFATAL
, "`%s': more than one comma on line",
1407 if (t
->type
== TOK_WHITESPACE
) {
1411 if (tt
->type
== TOK_WHITESPACE
) {
1415 if (tt
->type
!= t
->type
) {
1416 j
= false; /* found mismatching tokens */
1419 /* Unify surrounding quotes for strings */
1420 if (t
->type
== TOK_STRING
) {
1421 tt
->text
[0] = t
->text
[0];
1422 tt
->text
[strlen(tt
->text
) - 1] = t
->text
[0];
1424 if (mstrcmp(tt
->text
, t
->text
, i
== PPC_IFIDN
) != 0) {
1425 j
= false; /* found mismatching tokens */
1432 if ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) || tt
)
1433 j
= false; /* trailing gunk on one end or other */
1439 MMacro searching
, *mmac
;
1441 tline
= tline
->next
;
1443 tline
= expand_id(tline
);
1444 if (!tok_type_(tline
, TOK_ID
)) {
1446 "`%s' expects a macro name", pp_directives
[ct
]);
1449 searching
.name
= nasm_strdup(tline
->text
);
1450 searching
.casesense
= true;
1451 searching
.plus
= false;
1452 searching
.nolist
= false;
1453 searching
.in_progress
= 0;
1454 searching
.rep_nest
= NULL
;
1455 searching
.nparam_min
= 0;
1456 searching
.nparam_max
= INT_MAX
;
1457 tline
= expand_smacro(tline
->next
);
1460 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
1462 "`%s' expects a parameter count or nothing",
1465 searching
.nparam_min
= searching
.nparam_max
=
1466 readnum(tline
->text
, &j
);
1469 "unable to parse parameter count `%s'",
1472 if (tline
&& tok_is_(tline
->next
, "-")) {
1473 tline
= tline
->next
->next
;
1474 if (tok_is_(tline
, "*"))
1475 searching
.nparam_max
= INT_MAX
;
1476 else if (!tok_type_(tline
, TOK_NUMBER
))
1478 "`%s' expects a parameter count after `-'",
1481 searching
.nparam_max
= readnum(tline
->text
, &j
);
1484 "unable to parse parameter count `%s'",
1486 if (searching
.nparam_min
> searching
.nparam_max
)
1488 "minimum parameter count exceeds maximum");
1491 if (tline
&& tok_is_(tline
->next
, "+")) {
1492 tline
= tline
->next
;
1493 searching
.plus
= true;
1495 mmac
= (MMacro
*) hash_findix(mmacros
, searching
.name
);
1497 if (!strcmp(mmac
->name
, searching
.name
) &&
1498 (mmac
->nparam_min
<= searching
.nparam_max
1500 && (searching
.nparam_min
<= mmac
->nparam_max
1507 nasm_free(searching
.name
);
1516 needtype
= TOK_NUMBER
;
1519 needtype
= TOK_STRING
;
1523 tline
= expand_smacro(tline
);
1525 while (tok_type_(t
, TOK_WHITESPACE
))
1527 j
= false; /* placate optimiser */
1529 j
= t
->type
== needtype
;
1533 t
= tline
= expand_smacro(tline
);
1535 tokval
.t_type
= TOKEN_INVALID
;
1536 evalresult
= evaluate(ppscan
, tptr
, &tokval
,
1537 NULL
, pass
| CRITICAL
, error
, NULL
);
1542 "trailing garbage after expression ignored");
1543 if (!is_simple(evalresult
)) {
1545 "non-constant value given to `%s'", pp_directives
[ct
]);
1548 j
= reloc_value(evalresult
) != 0;
1553 "preprocessor directive `%s' not yet implemented",
1558 free_tlist(origline
);
1559 return j
^ PP_NEGATIVE(ct
);
1562 free_tlist(origline
);
1567 * Expand macros in a string. Used in %error and %include directives.
1568 * First tokenize the string, apply "expand_smacro" and then de-tokenize back.
1569 * The returned variable should ALWAYS be freed after usage.
1571 void expand_macros_in_string(char **p
)
1573 Token
*line
= tokenize(*p
);
1574 line
= expand_smacro(line
);
1575 *p
= detoken(line
, false);
1579 * Common code for defining an smacro
1581 static bool define_smacro(Context
*ctx
, char *mname
, bool casesense
,
1582 int nparam
, Token
*expansion
)
1584 SMacro
*smac
, **smhead
;
1586 if (smacro_defined(ctx
, mname
, nparam
, &smac
, casesense
)) {
1589 "single-line macro `%s' defined both with and"
1590 " without parameters", mname
);
1592 /* Some instances of the old code considered this a failure,
1593 some others didn't. What is the right thing to do here? */
1594 free_tlist(expansion
);
1595 return false; /* Failure */
1598 * We're redefining, so we have to take over an
1599 * existing SMacro structure. This means freeing
1600 * what was already in it.
1602 nasm_free(smac
->name
);
1603 free_tlist(smac
->expansion
);
1607 smhead
= (SMacro
**) hash_findi_add(smacros
, mname
);
1609 smhead
= &ctx
->localmac
;
1611 smac
= nasm_malloc(sizeof(SMacro
));
1612 smac
->next
= *smhead
;
1615 smac
->name
= nasm_strdup(mname
);
1616 smac
->casesense
= casesense
;
1617 smac
->nparam
= nparam
;
1618 smac
->expansion
= expansion
;
1619 smac
->in_progress
= false;
1620 return true; /* Success */
1624 * Undefine an smacro
1626 static void undef_smacro(Context
*ctx
, const char *mname
)
1628 SMacro
**smhead
, *s
, **sp
;
1631 smhead
= (SMacro
**) hash_findi(smacros
, mname
, NULL
);
1633 smhead
= &ctx
->localmac
;
1637 * We now have a macro name... go hunt for it.
1640 while ((s
= *sp
) != NULL
) {
1641 if (!mstrcmp(s
->name
, mname
, s
->casesense
)) {
1644 free_tlist(s
->expansion
);
1654 * find and process preprocessor directive in passed line
1655 * Find out if a line contains a preprocessor directive, and deal
1658 * If a directive _is_ found, it is the responsibility of this routine
1659 * (and not the caller) to free_tlist() the line.
1661 * @param tline a pointer to the current tokeninzed line linked list
1662 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
1665 static int do_directive(Token
* tline
)
1667 enum preproc_token i
;
1679 MMacro
*mmac
, **mmhead
;
1680 Token
*t
, *tt
, *param_start
, *macro_start
, *last
, **tptr
, *origline
;
1682 struct tokenval tokval
;
1684 MMacro
*tmp_defining
; /* Used when manipulating rep_nest */
1690 if (!tok_type_(tline
, TOK_PREPROC_ID
) ||
1691 (tline
->text
[1] == '%' || tline
->text
[1] == '$'
1692 || tline
->text
[1] == '!'))
1693 return NO_DIRECTIVE_FOUND
;
1695 i
= pp_token_hash(tline
->text
);
1698 * If we're in a non-emitting branch of a condition construct,
1699 * or walking to the end of an already terminated %rep block,
1700 * we should ignore all directives except for condition
1703 if (((istk
->conds
&& !emitting(istk
->conds
->state
)) ||
1704 (istk
->mstk
&& !istk
->mstk
->in_progress
)) && !is_condition(i
)) {
1705 return NO_DIRECTIVE_FOUND
;
1709 * If we're defining a macro or reading a %rep block, we should
1710 * ignore all directives except for %macro/%imacro (which
1711 * generate an error), %endm/%endmacro, and (only if we're in a
1712 * %rep block) %endrep. If we're in a %rep block, another %rep
1713 * causes an error, so should be let through.
1715 if (defining
&& i
!= PP_MACRO
&& i
!= PP_IMACRO
&&
1716 i
!= PP_ENDMACRO
&& i
!= PP_ENDM
&&
1717 (defining
->name
|| (i
!= PP_ENDREP
&& i
!= PP_REP
))) {
1718 return NO_DIRECTIVE_FOUND
;
1723 error(ERR_NONFATAL
, "unknown preprocessor directive `%s'",
1725 return NO_DIRECTIVE_FOUND
; /* didn't get it */
1728 /* Directive to tell NASM what the default stack size is. The
1729 * default is for a 16-bit stack, and this can be overriden with
1731 * the following form:
1733 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1735 tline
= tline
->next
;
1736 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1737 tline
= tline
->next
;
1738 if (!tline
|| tline
->type
!= TOK_ID
) {
1739 error(ERR_NONFATAL
, "`%%stacksize' missing size parameter");
1740 free_tlist(origline
);
1741 return DIRECTIVE_FOUND
;
1743 if (nasm_stricmp(tline
->text
, "flat") == 0) {
1744 /* All subsequent ARG directives are for a 32-bit stack */
1746 StackPointer
= "ebp";
1749 } else if (nasm_stricmp(tline
->text
, "large") == 0) {
1750 /* All subsequent ARG directives are for a 16-bit stack,
1751 * far function call.
1754 StackPointer
= "bp";
1757 } else if (nasm_stricmp(tline
->text
, "small") == 0) {
1758 /* All subsequent ARG directives are for a 16-bit stack,
1759 * far function call. We don't support near functions.
1762 StackPointer
= "bp";
1766 error(ERR_NONFATAL
, "`%%stacksize' invalid size type");
1767 free_tlist(origline
);
1768 return DIRECTIVE_FOUND
;
1770 free_tlist(origline
);
1771 return DIRECTIVE_FOUND
;
1774 /* TASM like ARG directive to define arguments to functions, in
1775 * the following form:
1777 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1781 char *arg
, directive
[256];
1782 int size
= StackSize
;
1784 /* Find the argument name */
1785 tline
= tline
->next
;
1786 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1787 tline
= tline
->next
;
1788 if (!tline
|| tline
->type
!= TOK_ID
) {
1789 error(ERR_NONFATAL
, "`%%arg' missing argument parameter");
1790 free_tlist(origline
);
1791 return DIRECTIVE_FOUND
;
1795 /* Find the argument size type */
1796 tline
= tline
->next
;
1797 if (!tline
|| tline
->type
!= TOK_OTHER
1798 || tline
->text
[0] != ':') {
1800 "Syntax error processing `%%arg' directive");
1801 free_tlist(origline
);
1802 return DIRECTIVE_FOUND
;
1804 tline
= tline
->next
;
1805 if (!tline
|| tline
->type
!= TOK_ID
) {
1806 error(ERR_NONFATAL
, "`%%arg' missing size type parameter");
1807 free_tlist(origline
);
1808 return DIRECTIVE_FOUND
;
1811 /* Allow macro expansion of type parameter */
1812 tt
= tokenize(tline
->text
);
1813 tt
= expand_smacro(tt
);
1814 if (nasm_stricmp(tt
->text
, "byte") == 0) {
1815 size
= MAX(StackSize
, 1);
1816 } else if (nasm_stricmp(tt
->text
, "word") == 0) {
1817 size
= MAX(StackSize
, 2);
1818 } else if (nasm_stricmp(tt
->text
, "dword") == 0) {
1819 size
= MAX(StackSize
, 4);
1820 } else if (nasm_stricmp(tt
->text
, "qword") == 0) {
1821 size
= MAX(StackSize
, 8);
1822 } else if (nasm_stricmp(tt
->text
, "tword") == 0) {
1823 size
= MAX(StackSize
, 10);
1826 "Invalid size type for `%%arg' missing directive");
1828 free_tlist(origline
);
1829 return DIRECTIVE_FOUND
;
1833 /* Now define the macro for the argument */
1834 snprintf(directive
, sizeof(directive
), "%%define %s (%s+%d)",
1835 arg
, StackPointer
, offset
);
1836 do_directive(tokenize(directive
));
1839 /* Move to the next argument in the list */
1840 tline
= tline
->next
;
1841 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1842 tline
= tline
->next
;
1844 while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
1845 free_tlist(origline
);
1846 return DIRECTIVE_FOUND
;
1849 /* TASM like LOCAL directive to define local variables for a
1850 * function, in the following form:
1852 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
1854 * The '= LocalSize' at the end is ignored by NASM, but is
1855 * required by TASM to define the local parameter size (and used
1856 * by the TASM macro package).
1858 offset
= LocalOffset
;
1860 char *local
, directive
[256];
1861 int size
= StackSize
;
1863 /* Find the argument name */
1864 tline
= tline
->next
;
1865 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1866 tline
= tline
->next
;
1867 if (!tline
|| tline
->type
!= TOK_ID
) {
1869 "`%%local' missing argument parameter");
1870 free_tlist(origline
);
1871 return DIRECTIVE_FOUND
;
1873 local
= tline
->text
;
1875 /* Find the argument size type */
1876 tline
= tline
->next
;
1877 if (!tline
|| tline
->type
!= TOK_OTHER
1878 || tline
->text
[0] != ':') {
1880 "Syntax error processing `%%local' directive");
1881 free_tlist(origline
);
1882 return DIRECTIVE_FOUND
;
1884 tline
= tline
->next
;
1885 if (!tline
|| tline
->type
!= TOK_ID
) {
1887 "`%%local' missing size type parameter");
1888 free_tlist(origline
);
1889 return DIRECTIVE_FOUND
;
1892 /* Allow macro expansion of type parameter */
1893 tt
= tokenize(tline
->text
);
1894 tt
= expand_smacro(tt
);
1895 if (nasm_stricmp(tt
->text
, "byte") == 0) {
1896 size
= MAX(StackSize
, 1);
1897 } else if (nasm_stricmp(tt
->text
, "word") == 0) {
1898 size
= MAX(StackSize
, 2);
1899 } else if (nasm_stricmp(tt
->text
, "dword") == 0) {
1900 size
= MAX(StackSize
, 4);
1901 } else if (nasm_stricmp(tt
->text
, "qword") == 0) {
1902 size
= MAX(StackSize
, 8);
1903 } else if (nasm_stricmp(tt
->text
, "tword") == 0) {
1904 size
= MAX(StackSize
, 10);
1907 "Invalid size type for `%%local' missing directive");
1909 free_tlist(origline
);
1910 return DIRECTIVE_FOUND
;
1914 /* Now define the macro for the argument */
1915 snprintf(directive
, sizeof(directive
), "%%define %s (%s-%d)",
1916 local
, StackPointer
, offset
);
1917 do_directive(tokenize(directive
));
1920 /* Now define the assign to setup the enter_c macro correctly */
1921 snprintf(directive
, sizeof(directive
),
1922 "%%assign %%$localsize %%$localsize+%d", size
);
1923 do_directive(tokenize(directive
));
1925 /* Move to the next argument in the list */
1926 tline
= tline
->next
;
1927 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1928 tline
= tline
->next
;
1930 while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
1931 free_tlist(origline
);
1932 return DIRECTIVE_FOUND
;
1936 error(ERR_WARNING
, "trailing garbage after `%%clear' ignored");
1939 free_tlist(origline
);
1940 return DIRECTIVE_FOUND
;
1943 tline
= tline
->next
;
1945 if (!tline
|| (tline
->type
!= TOK_STRING
&&
1946 tline
->type
!= TOK_INTERNAL_STRING
)) {
1947 error(ERR_NONFATAL
, "`%%include' expects a file name");
1948 free_tlist(origline
);
1949 return DIRECTIVE_FOUND
; /* but we did _something_ */
1953 "trailing garbage after `%%include' ignored");
1954 if (tline
->type
!= TOK_INTERNAL_STRING
) {
1955 p
= tline
->text
+ 1; /* point past the quote to the name */
1956 p
[strlen(p
) - 1] = '\0'; /* remove the trailing quote */
1958 p
= tline
->text
; /* internal_string is easier */
1959 expand_macros_in_string(&p
);
1960 inc
= nasm_malloc(sizeof(Include
));
1963 inc
->fp
= inc_fopen(p
);
1964 if (!inc
->fp
&& pass
== 0) {
1965 /* -MG given but file not found */
1968 inc
->fname
= src_set_fname(p
);
1969 inc
->lineno
= src_set_linnum(0);
1971 inc
->expansion
= NULL
;
1974 list
->uplevel(LIST_INCLUDE
);
1976 free_tlist(origline
);
1977 return DIRECTIVE_FOUND
;
1980 tline
= tline
->next
;
1982 tline
= expand_id(tline
);
1983 if (!tok_type_(tline
, TOK_ID
)) {
1984 error(ERR_NONFATAL
, "`%%push' expects a context identifier");
1985 free_tlist(origline
);
1986 return DIRECTIVE_FOUND
; /* but we did _something_ */
1989 error(ERR_WARNING
, "trailing garbage after `%%push' ignored");
1990 ctx
= nasm_malloc(sizeof(Context
));
1992 ctx
->localmac
= NULL
;
1993 ctx
->name
= nasm_strdup(tline
->text
);
1994 ctx
->number
= unique
++;
1996 free_tlist(origline
);
2000 tline
= tline
->next
;
2002 tline
= expand_id(tline
);
2003 if (!tok_type_(tline
, TOK_ID
)) {
2004 error(ERR_NONFATAL
, "`%%repl' expects a context identifier");
2005 free_tlist(origline
);
2006 return DIRECTIVE_FOUND
; /* but we did _something_ */
2009 error(ERR_WARNING
, "trailing garbage after `%%repl' ignored");
2011 error(ERR_NONFATAL
, "`%%repl': context stack is empty");
2013 nasm_free(cstk
->name
);
2014 cstk
->name
= nasm_strdup(tline
->text
);
2016 free_tlist(origline
);
2021 error(ERR_WARNING
, "trailing garbage after `%%pop' ignored");
2023 error(ERR_NONFATAL
, "`%%pop': context stack is already empty");
2026 free_tlist(origline
);
2030 tline
->next
= expand_smacro(tline
->next
);
2031 tline
= tline
->next
;
2033 if (tok_type_(tline
, TOK_STRING
)) {
2034 p
= tline
->text
+ 1; /* point past the quote to the name */
2035 p
[strlen(p
) - 1] = '\0'; /* remove the trailing quote */
2036 expand_macros_in_string(&p
);
2037 error(ERR_NONFATAL
, "%s", p
);
2040 p
= detoken(tline
, false);
2041 error(ERR_WARNING
, "%s", p
);
2044 free_tlist(origline
);
2048 if (istk
->conds
&& !emitting(istk
->conds
->state
))
2051 j
= if_condition(tline
->next
, i
);
2052 tline
->next
= NULL
; /* it got freed */
2053 free_tlist(origline
);
2054 j
= j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2056 cond
= nasm_malloc(sizeof(Cond
));
2057 cond
->next
= istk
->conds
;
2060 return DIRECTIVE_FOUND
;
2064 error(ERR_FATAL
, "`%s': no matching `%%if'", pp_directives
[i
]);
2065 if (emitting(istk
->conds
->state
)
2066 || istk
->conds
->state
== COND_NEVER
)
2067 istk
->conds
->state
= COND_NEVER
;
2070 * IMPORTANT: In the case of %if, we will already have
2071 * called expand_mmac_params(); however, if we're
2072 * processing an %elif we must have been in a
2073 * non-emitting mode, which would have inhibited
2074 * the normal invocation of expand_mmac_params(). Therefore,
2075 * we have to do it explicitly here.
2077 j
= if_condition(expand_mmac_params(tline
->next
), i
);
2078 tline
->next
= NULL
; /* it got freed */
2079 free_tlist(origline
);
2080 istk
->conds
->state
=
2081 j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2083 return DIRECTIVE_FOUND
;
2087 error(ERR_WARNING
, "trailing garbage after `%%else' ignored");
2089 error(ERR_FATAL
, "`%%else': no matching `%%if'");
2090 if (emitting(istk
->conds
->state
)
2091 || istk
->conds
->state
== COND_NEVER
)
2092 istk
->conds
->state
= COND_ELSE_FALSE
;
2094 istk
->conds
->state
= COND_ELSE_TRUE
;
2095 free_tlist(origline
);
2096 return DIRECTIVE_FOUND
;
2100 error(ERR_WARNING
, "trailing garbage after `%%endif' ignored");
2102 error(ERR_FATAL
, "`%%endif': no matching `%%if'");
2104 istk
->conds
= cond
->next
;
2106 free_tlist(origline
);
2107 return DIRECTIVE_FOUND
;
2113 "`%%%smacro': already defining a macro",
2114 (i
== PP_IMACRO
? "i" : ""));
2115 tline
= tline
->next
;
2117 tline
= expand_id(tline
);
2118 if (!tok_type_(tline
, TOK_ID
)) {
2120 "`%%%smacro' expects a macro name",
2121 (i
== PP_IMACRO
? "i" : ""));
2122 return DIRECTIVE_FOUND
;
2124 defining
= nasm_malloc(sizeof(MMacro
));
2125 defining
->name
= nasm_strdup(tline
->text
);
2126 defining
->casesense
= (i
== PP_MACRO
);
2127 defining
->plus
= false;
2128 defining
->nolist
= false;
2129 defining
->in_progress
= 0;
2130 defining
->rep_nest
= NULL
;
2131 tline
= expand_smacro(tline
->next
);
2133 if (!tok_type_(tline
, TOK_NUMBER
)) {
2135 "`%%%smacro' expects a parameter count",
2136 (i
== PP_IMACRO
? "i" : ""));
2137 defining
->nparam_min
= defining
->nparam_max
= 0;
2139 defining
->nparam_min
= defining
->nparam_max
=
2140 readnum(tline
->text
, &err
);
2143 "unable to parse parameter count `%s'", tline
->text
);
2145 if (tline
&& tok_is_(tline
->next
, "-")) {
2146 tline
= tline
->next
->next
;
2147 if (tok_is_(tline
, "*"))
2148 defining
->nparam_max
= INT_MAX
;
2149 else if (!tok_type_(tline
, TOK_NUMBER
))
2151 "`%%%smacro' expects a parameter count after `-'",
2152 (i
== PP_IMACRO
? "i" : ""));
2154 defining
->nparam_max
= readnum(tline
->text
, &err
);
2157 "unable to parse parameter count `%s'",
2159 if (defining
->nparam_min
> defining
->nparam_max
)
2161 "minimum parameter count exceeds maximum");
2164 if (tline
&& tok_is_(tline
->next
, "+")) {
2165 tline
= tline
->next
;
2166 defining
->plus
= true;
2168 if (tline
&& tok_type_(tline
->next
, TOK_ID
) &&
2169 !nasm_stricmp(tline
->next
->text
, ".nolist")) {
2170 tline
= tline
->next
;
2171 defining
->nolist
= true;
2173 mmac
= (MMacro
*) hash_findix(mmacros
, defining
->name
);
2175 if (!strcmp(mmac
->name
, defining
->name
) &&
2176 (mmac
->nparam_min
<= defining
->nparam_max
2178 && (defining
->nparam_min
<= mmac
->nparam_max
2181 "redefining multi-line macro `%s'", defining
->name
);
2187 * Handle default parameters.
2189 if (tline
&& tline
->next
) {
2190 defining
->dlist
= tline
->next
;
2192 count_mmac_params(defining
->dlist
, &defining
->ndefs
,
2193 &defining
->defaults
);
2195 defining
->dlist
= NULL
;
2196 defining
->defaults
= NULL
;
2198 defining
->expansion
= NULL
;
2199 free_tlist(origline
);
2200 return DIRECTIVE_FOUND
;
2205 error(ERR_NONFATAL
, "`%s': not defining a macro", tline
->text
);
2206 return DIRECTIVE_FOUND
;
2208 mmhead
= (MMacro
**) hash_findi_add(mmacros
, defining
->name
);
2209 defining
->next
= *mmhead
;
2212 free_tlist(origline
);
2213 return DIRECTIVE_FOUND
;
2216 if (tline
->next
&& tline
->next
->type
== TOK_WHITESPACE
)
2217 tline
= tline
->next
;
2218 if (tline
->next
== NULL
) {
2219 free_tlist(origline
);
2220 error(ERR_NONFATAL
, "`%%rotate' missing rotate count");
2221 return DIRECTIVE_FOUND
;
2223 t
= expand_smacro(tline
->next
);
2225 free_tlist(origline
);
2228 tokval
.t_type
= TOKEN_INVALID
;
2230 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2233 return DIRECTIVE_FOUND
;
2236 "trailing garbage after expression ignored");
2237 if (!is_simple(evalresult
)) {
2238 error(ERR_NONFATAL
, "non-constant value given to `%%rotate'");
2239 return DIRECTIVE_FOUND
;
2242 while (mmac
&& !mmac
->name
) /* avoid mistaking %reps for macros */
2243 mmac
= mmac
->next_active
;
2245 error(ERR_NONFATAL
, "`%%rotate' invoked outside a macro call");
2246 } else if (mmac
->nparam
== 0) {
2248 "`%%rotate' invoked within macro without parameters");
2250 int rotate
= mmac
->rotate
+ reloc_value(evalresult
);
2252 rotate
%= (int)mmac
->nparam
;
2254 rotate
+= mmac
->nparam
;
2256 mmac
->rotate
= rotate
;
2258 return DIRECTIVE_FOUND
;
2263 tline
= tline
->next
;
2264 } while (tok_type_(tline
, TOK_WHITESPACE
));
2266 if (tok_type_(tline
, TOK_ID
) &&
2267 nasm_stricmp(tline
->text
, ".nolist") == 0) {
2270 tline
= tline
->next
;
2271 } while (tok_type_(tline
, TOK_WHITESPACE
));
2275 t
= expand_smacro(tline
);
2277 tokval
.t_type
= TOKEN_INVALID
;
2279 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2281 free_tlist(origline
);
2282 return DIRECTIVE_FOUND
;
2286 "trailing garbage after expression ignored");
2287 if (!is_simple(evalresult
)) {
2288 error(ERR_NONFATAL
, "non-constant value given to `%%rep'");
2289 return DIRECTIVE_FOUND
;
2291 count
= reloc_value(evalresult
) + 1;
2293 error(ERR_NONFATAL
, "`%%rep' expects a repeat count");
2296 free_tlist(origline
);
2298 tmp_defining
= defining
;
2299 defining
= nasm_malloc(sizeof(MMacro
));
2300 defining
->name
= NULL
; /* flags this macro as a %rep block */
2301 defining
->casesense
= false;
2302 defining
->plus
= false;
2303 defining
->nolist
= nolist
;
2304 defining
->in_progress
= count
;
2305 defining
->nparam_min
= defining
->nparam_max
= 0;
2306 defining
->defaults
= NULL
;
2307 defining
->dlist
= NULL
;
2308 defining
->expansion
= NULL
;
2309 defining
->next_active
= istk
->mstk
;
2310 defining
->rep_nest
= tmp_defining
;
2311 return DIRECTIVE_FOUND
;
2314 if (!defining
|| defining
->name
) {
2315 error(ERR_NONFATAL
, "`%%endrep': no matching `%%rep'");
2316 return DIRECTIVE_FOUND
;
2320 * Now we have a "macro" defined - although it has no name
2321 * and we won't be entering it in the hash tables - we must
2322 * push a macro-end marker for it on to istk->expansion.
2323 * After that, it will take care of propagating itself (a
2324 * macro-end marker line for a macro which is really a %rep
2325 * block will cause the macro to be re-expanded, complete
2326 * with another macro-end marker to ensure the process
2327 * continues) until the whole expansion is forcibly removed
2328 * from istk->expansion by a %exitrep.
2330 l
= nasm_malloc(sizeof(Line
));
2331 l
->next
= istk
->expansion
;
2332 l
->finishes
= defining
;
2334 istk
->expansion
= l
;
2336 istk
->mstk
= defining
;
2338 list
->uplevel(defining
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
2339 tmp_defining
= defining
;
2340 defining
= defining
->rep_nest
;
2341 free_tlist(origline
);
2342 return DIRECTIVE_FOUND
;
2346 * We must search along istk->expansion until we hit a
2347 * macro-end marker for a macro with no name. Then we set
2348 * its `in_progress' flag to 0.
2350 for (l
= istk
->expansion
; l
; l
= l
->next
)
2351 if (l
->finishes
&& !l
->finishes
->name
)
2355 l
->finishes
->in_progress
= 0;
2357 error(ERR_NONFATAL
, "`%%exitrep' not within `%%rep' block");
2358 free_tlist(origline
);
2359 return DIRECTIVE_FOUND
;
2365 casesense
= (i
== PP_DEFINE
|| i
== PP_XDEFINE
);
2367 tline
= tline
->next
;
2369 tline
= expand_id(tline
);
2370 if (!tline
|| (tline
->type
!= TOK_ID
&&
2371 (tline
->type
!= TOK_PREPROC_ID
||
2372 tline
->text
[1] != '$'))) {
2373 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
2375 free_tlist(origline
);
2376 return DIRECTIVE_FOUND
;
2379 ctx
= get_ctx(tline
->text
, false);
2381 mname
= tline
->text
;
2383 param_start
= tline
= tline
->next
;
2386 /* Expand the macro definition now for %xdefine and %ixdefine */
2387 if ((i
== PP_XDEFINE
) || (i
== PP_IXDEFINE
))
2388 tline
= expand_smacro(tline
);
2390 if (tok_is_(tline
, "(")) {
2392 * This macro has parameters.
2395 tline
= tline
->next
;
2399 error(ERR_NONFATAL
, "parameter identifier expected");
2400 free_tlist(origline
);
2401 return DIRECTIVE_FOUND
;
2403 if (tline
->type
!= TOK_ID
) {
2405 "`%s': parameter identifier expected",
2407 free_tlist(origline
);
2408 return DIRECTIVE_FOUND
;
2410 tline
->type
= TOK_SMAC_PARAM
+ nparam
++;
2411 tline
= tline
->next
;
2413 if (tok_is_(tline
, ",")) {
2414 tline
= tline
->next
;
2417 if (!tok_is_(tline
, ")")) {
2419 "`)' expected to terminate macro template");
2420 free_tlist(origline
);
2421 return DIRECTIVE_FOUND
;
2426 tline
= tline
->next
;
2428 if (tok_type_(tline
, TOK_WHITESPACE
))
2429 last
= tline
, tline
= tline
->next
;
2434 if (t
->type
== TOK_ID
) {
2435 for (tt
= param_start
; tt
; tt
= tt
->next
)
2436 if (tt
->type
>= TOK_SMAC_PARAM
&&
2437 !strcmp(tt
->text
, t
->text
))
2441 t
->next
= macro_start
;
2446 * Good. We now have a macro name, a parameter count, and a
2447 * token list (in reverse order) for an expansion. We ought
2448 * to be OK just to create an SMacro, store it, and let
2449 * free_tlist have the rest of the line (which we have
2450 * carefully re-terminated after chopping off the expansion
2453 define_smacro(ctx
, mname
, casesense
, nparam
, macro_start
);
2454 free_tlist(origline
);
2455 return DIRECTIVE_FOUND
;
2458 tline
= tline
->next
;
2460 tline
= expand_id(tline
);
2461 if (!tline
|| (tline
->type
!= TOK_ID
&&
2462 (tline
->type
!= TOK_PREPROC_ID
||
2463 tline
->text
[1] != '$'))) {
2464 error(ERR_NONFATAL
, "`%%undef' expects a macro identifier");
2465 free_tlist(origline
);
2466 return DIRECTIVE_FOUND
;
2470 "trailing garbage after macro name ignored");
2473 /* Find the context that symbol belongs to */
2474 ctx
= get_ctx(tline
->text
, false);
2475 undef_smacro(ctx
, tline
->text
);
2476 free_tlist(origline
);
2477 return DIRECTIVE_FOUND
;
2482 tline
= tline
->next
;
2484 tline
= expand_id(tline
);
2485 if (!tline
|| (tline
->type
!= TOK_ID
&&
2486 (tline
->type
!= TOK_PREPROC_ID
||
2487 tline
->text
[1] != '$'))) {
2489 "`%%strlen' expects a macro identifier as first parameter");
2490 free_tlist(origline
);
2491 return DIRECTIVE_FOUND
;
2493 ctx
= get_ctx(tline
->text
, false);
2495 mname
= tline
->text
;
2497 tline
= expand_smacro(tline
->next
);
2501 while (tok_type_(t
, TOK_WHITESPACE
))
2503 /* t should now point to the string */
2504 if (t
->type
!= TOK_STRING
) {
2506 "`%%strlen` requires string as second parameter");
2508 free_tlist(origline
);
2509 return DIRECTIVE_FOUND
;
2512 macro_start
= nasm_malloc(sizeof(*macro_start
));
2513 macro_start
->next
= NULL
;
2514 make_tok_num(macro_start
, strlen(t
->text
) - 2);
2515 macro_start
->mac
= NULL
;
2518 * We now have a macro name, an implicit parameter count of
2519 * zero, and a numeric token to use as an expansion. Create
2520 * and store an SMacro.
2522 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2524 free_tlist(origline
);
2525 return DIRECTIVE_FOUND
;
2530 tline
= tline
->next
;
2532 tline
= expand_id(tline
);
2533 if (!tline
|| (tline
->type
!= TOK_ID
&&
2534 (tline
->type
!= TOK_PREPROC_ID
||
2535 tline
->text
[1] != '$'))) {
2537 "`%%substr' expects a macro identifier as first parameter");
2538 free_tlist(origline
);
2539 return DIRECTIVE_FOUND
;
2541 ctx
= get_ctx(tline
->text
, false);
2543 mname
= tline
->text
;
2545 tline
= expand_smacro(tline
->next
);
2549 while (tok_type_(t
, TOK_WHITESPACE
))
2552 /* t should now point to the string */
2553 if (t
->type
!= TOK_STRING
) {
2555 "`%%substr` requires string as second parameter");
2557 free_tlist(origline
);
2558 return DIRECTIVE_FOUND
;
2563 tokval
.t_type
= TOKEN_INVALID
;
2565 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2568 free_tlist(origline
);
2569 return DIRECTIVE_FOUND
;
2571 if (!is_simple(evalresult
)) {
2572 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
2574 free_tlist(origline
);
2575 return DIRECTIVE_FOUND
;
2578 macro_start
= nasm_malloc(sizeof(*macro_start
));
2579 macro_start
->next
= NULL
;
2580 macro_start
->text
= nasm_strdup("'''");
2581 if (evalresult
->value
> 0
2582 && evalresult
->value
< strlen(t
->text
) - 1) {
2583 macro_start
->text
[1] = t
->text
[evalresult
->value
];
2585 macro_start
->text
[2] = '\0';
2587 macro_start
->type
= TOK_STRING
;
2588 macro_start
->mac
= NULL
;
2591 * We now have a macro name, an implicit parameter count of
2592 * zero, and a numeric token to use as an expansion. Create
2593 * and store an SMacro.
2595 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2597 free_tlist(origline
);
2598 return DIRECTIVE_FOUND
;
2602 casesense
= (i
== PP_ASSIGN
);
2604 tline
= tline
->next
;
2606 tline
= expand_id(tline
);
2607 if (!tline
|| (tline
->type
!= TOK_ID
&&
2608 (tline
->type
!= TOK_PREPROC_ID
||
2609 tline
->text
[1] != '$'))) {
2611 "`%%%sassign' expects a macro identifier",
2612 (i
== PP_IASSIGN
? "i" : ""));
2613 free_tlist(origline
);
2614 return DIRECTIVE_FOUND
;
2616 ctx
= get_ctx(tline
->text
, false);
2618 mname
= tline
->text
;
2620 tline
= expand_smacro(tline
->next
);
2625 tokval
.t_type
= TOKEN_INVALID
;
2627 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2630 free_tlist(origline
);
2631 return DIRECTIVE_FOUND
;
2636 "trailing garbage after expression ignored");
2638 if (!is_simple(evalresult
)) {
2640 "non-constant value given to `%%%sassign'",
2641 (i
== PP_IASSIGN
? "i" : ""));
2642 free_tlist(origline
);
2643 return DIRECTIVE_FOUND
;
2646 macro_start
= nasm_malloc(sizeof(*macro_start
));
2647 macro_start
->next
= NULL
;
2648 make_tok_num(macro_start
, reloc_value(evalresult
));
2649 macro_start
->mac
= NULL
;
2652 * We now have a macro name, an implicit parameter count of
2653 * zero, and a numeric token to use as an expansion. Create
2654 * and store an SMacro.
2656 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2657 free_tlist(origline
);
2658 return DIRECTIVE_FOUND
;
2662 * Syntax is `%line nnn[+mmm] [filename]'
2664 tline
= tline
->next
;
2666 if (!tok_type_(tline
, TOK_NUMBER
)) {
2667 error(ERR_NONFATAL
, "`%%line' expects line number");
2668 free_tlist(origline
);
2669 return DIRECTIVE_FOUND
;
2671 k
= readnum(tline
->text
, &err
);
2673 tline
= tline
->next
;
2674 if (tok_is_(tline
, "+")) {
2675 tline
= tline
->next
;
2676 if (!tok_type_(tline
, TOK_NUMBER
)) {
2677 error(ERR_NONFATAL
, "`%%line' expects line increment");
2678 free_tlist(origline
);
2679 return DIRECTIVE_FOUND
;
2681 m
= readnum(tline
->text
, &err
);
2682 tline
= tline
->next
;
2688 nasm_free(src_set_fname(detoken(tline
, false)));
2690 free_tlist(origline
);
2691 return DIRECTIVE_FOUND
;
2695 "preprocessor directive `%s' not yet implemented",
2699 return DIRECTIVE_FOUND
;
2703 * Ensure that a macro parameter contains a condition code and
2704 * nothing else. Return the condition code index if so, or -1
2707 static int find_cc(Token
* t
)
2713 return -1; /* Probably a %+ without a space */
2716 if (t
->type
!= TOK_ID
)
2720 if (tt
&& (tt
->type
!= TOK_OTHER
|| strcmp(tt
->text
, ",")))
2724 j
= elements(conditions
);
2727 m
= nasm_stricmp(t
->text
, conditions
[k
]);
2743 * Expand MMacro-local things: parameter references (%0, %n, %+n,
2744 * %-n) and MMacro-local identifiers (%%foo).
2746 static Token
*expand_mmac_params(Token
* tline
)
2748 Token
*t
, *tt
, **tail
, *thead
;
2754 if (tline
->type
== TOK_PREPROC_ID
&&
2755 (((tline
->text
[1] == '+' || tline
->text
[1] == '-')
2756 && tline
->text
[2]) || tline
->text
[1] == '%'
2757 || (tline
->text
[1] >= '0' && tline
->text
[1] <= '9'))) {
2759 int type
= 0, cc
; /* type = 0 to placate optimisers */
2766 tline
= tline
->next
;
2769 while (mac
&& !mac
->name
) /* avoid mistaking %reps for macros */
2770 mac
= mac
->next_active
;
2772 error(ERR_NONFATAL
, "`%s': not in a macro call", t
->text
);
2774 switch (t
->text
[1]) {
2776 * We have to make a substitution of one of the
2777 * forms %1, %-1, %+1, %%foo, %0.
2781 snprintf(tmpbuf
, sizeof(tmpbuf
), "%d", mac
->nparam
);
2782 text
= nasm_strdup(tmpbuf
);
2786 snprintf(tmpbuf
, sizeof(tmpbuf
), "..@%"PRIu64
".",
2788 text
= nasm_strcat(tmpbuf
, t
->text
+ 2);
2791 n
= atoi(t
->text
+ 2) - 1;
2792 if (n
>= mac
->nparam
)
2795 if (mac
->nparam
> 1)
2796 n
= (n
+ mac
->rotate
) % mac
->nparam
;
2797 tt
= mac
->params
[n
];
2802 "macro parameter %d is not a condition code",
2807 if (inverse_ccs
[cc
] == -1) {
2809 "condition code `%s' is not invertible",
2814 nasm_strdup(conditions
[inverse_ccs
[cc
]]);
2818 n
= atoi(t
->text
+ 2) - 1;
2819 if (n
>= mac
->nparam
)
2822 if (mac
->nparam
> 1)
2823 n
= (n
+ mac
->rotate
) % mac
->nparam
;
2824 tt
= mac
->params
[n
];
2829 "macro parameter %d is not a condition code",
2834 text
= nasm_strdup(conditions
[cc
]);
2838 n
= atoi(t
->text
+ 1) - 1;
2839 if (n
>= mac
->nparam
)
2842 if (mac
->nparam
> 1)
2843 n
= (n
+ mac
->rotate
) % mac
->nparam
;
2844 tt
= mac
->params
[n
];
2847 for (i
= 0; i
< mac
->paramlen
[n
]; i
++) {
2848 *tail
= new_Token(NULL
, tt
->type
, tt
->text
, 0);
2849 tail
= &(*tail
)->next
;
2853 text
= NULL
; /* we've done it here */
2869 tline
= tline
->next
;
2876 for (; t
&& (tt
= t
->next
) != NULL
; t
= t
->next
)
2878 case TOK_WHITESPACE
:
2879 if (tt
->type
== TOK_WHITESPACE
) {
2880 t
->next
= delete_Token(tt
);
2884 if (tt
->type
== TOK_ID
|| tt
->type
== TOK_NUMBER
) {
2885 char *tmp
= nasm_strcat(t
->text
, tt
->text
);
2888 t
->next
= delete_Token(tt
);
2892 if (tt
->type
== TOK_NUMBER
) {
2893 char *tmp
= nasm_strcat(t
->text
, tt
->text
);
2896 t
->next
= delete_Token(tt
);
2907 * Expand all single-line macro calls made in the given line.
2908 * Return the expanded version of the line. The original is deemed
2909 * to be destroyed in the process. (In reality we'll just move
2910 * Tokens from input to output a lot of the time, rather than
2911 * actually bothering to destroy and replicate.)
2913 static Token
*expand_smacro(Token
* tline
)
2915 Token
*t
, *tt
, *mstart
, **tail
, *thead
;
2916 SMacro
*head
= NULL
, *m
;
2919 unsigned int nparam
, sparam
;
2920 int brackets
, rescan
;
2921 Token
*org_tline
= tline
;
2926 * Trick: we should avoid changing the start token pointer since it can
2927 * be contained in "next" field of other token. Because of this
2928 * we allocate a copy of first token and work with it; at the end of
2929 * routine we copy it back
2933 new_Token(org_tline
->next
, org_tline
->type
, org_tline
->text
,
2935 tline
->mac
= org_tline
->mac
;
2936 nasm_free(org_tline
->text
);
2937 org_tline
->text
= NULL
;
2944 while (tline
) { /* main token loop */
2945 if ((mname
= tline
->text
)) {
2946 /* if this token is a local macro, look in local context */
2947 if (tline
->type
== TOK_ID
|| tline
->type
== TOK_PREPROC_ID
)
2948 ctx
= get_ctx(mname
, true);
2952 head
= (SMacro
*) hash_findix(smacros
, mname
);
2954 head
= ctx
->localmac
;
2957 * We've hit an identifier. As in is_mmacro below, we first
2958 * check whether the identifier is a single-line macro at
2959 * all, then think about checking for parameters if
2962 for (m
= head
; m
; m
= m
->next
)
2963 if (!mstrcmp(m
->name
, mname
, m
->casesense
))
2969 if (m
->nparam
== 0) {
2971 * Simple case: the macro is parameterless. Discard the
2972 * one token that the macro call took, and push the
2973 * expansion back on the to-do stack.
2975 if (!m
->expansion
) {
2976 if (!strcmp("__FILE__", m
->name
)) {
2978 src_get(&num
, &(tline
->text
));
2979 nasm_quote(&(tline
->text
));
2980 tline
->type
= TOK_STRING
;
2983 if (!strcmp("__LINE__", m
->name
)) {
2984 nasm_free(tline
->text
);
2985 make_tok_num(tline
, src_get_linnum());
2988 if (!strcmp("__BITS__", m
->name
)) {
2989 nasm_free(tline
->text
);
2990 make_tok_num(tline
, globalbits
);
2993 tline
= delete_Token(tline
);
2998 * Complicated case: at least one macro with this name
2999 * exists and takes parameters. We must find the
3000 * parameters in the call, count them, find the SMacro
3001 * that corresponds to that form of the macro call, and
3002 * substitute for the parameters when we expand. What a
3005 /*tline = tline->next;
3006 skip_white_(tline); */
3009 while (tok_type_(t
, TOK_SMAC_END
)) {
3010 t
->mac
->in_progress
= false;
3012 t
= tline
->next
= delete_Token(t
);
3015 } while (tok_type_(tline
, TOK_WHITESPACE
));
3016 if (!tok_is_(tline
, "(")) {
3018 * This macro wasn't called with parameters: ignore
3019 * the call. (Behaviour borrowed from gnu cpp.)
3028 sparam
= PARAM_DELTA
;
3029 params
= nasm_malloc(sparam
* sizeof(Token
*));
3030 params
[0] = tline
->next
;
3031 paramsize
= nasm_malloc(sparam
* sizeof(int));
3033 while (true) { /* parameter loop */
3035 * For some unusual expansions
3036 * which concatenates function call
3039 while (tok_type_(t
, TOK_SMAC_END
)) {
3040 t
->mac
->in_progress
= false;
3042 t
= tline
->next
= delete_Token(t
);
3048 "macro call expects terminating `)'");
3051 if (tline
->type
== TOK_WHITESPACE
3053 if (paramsize
[nparam
])
3056 params
[nparam
] = tline
->next
;
3057 continue; /* parameter loop */
3059 if (tline
->type
== TOK_OTHER
3060 && tline
->text
[1] == 0) {
3061 char ch
= tline
->text
[0];
3062 if (ch
== ',' && !paren
&& brackets
<= 0) {
3063 if (++nparam
>= sparam
) {
3064 sparam
+= PARAM_DELTA
;
3065 params
= nasm_realloc(params
,
3070 nasm_realloc(paramsize
,
3074 params
[nparam
] = tline
->next
;
3075 paramsize
[nparam
] = 0;
3077 continue; /* parameter loop */
3080 (brackets
> 0 || (brackets
== 0 &&
3081 !paramsize
[nparam
])))
3083 if (!(brackets
++)) {
3084 params
[nparam
] = tline
->next
;
3085 continue; /* parameter loop */
3088 if (ch
== '}' && brackets
> 0)
3089 if (--brackets
== 0) {
3091 continue; /* parameter loop */
3093 if (ch
== '(' && !brackets
)
3095 if (ch
== ')' && brackets
<= 0)
3101 error(ERR_NONFATAL
, "braces do not "
3102 "enclose all of macro parameter");
3104 paramsize
[nparam
] += white
+ 1;
3106 } /* parameter loop */
3108 while (m
&& (m
->nparam
!= nparam
||
3109 mstrcmp(m
->name
, mname
,
3113 error(ERR_WARNING
| ERR_WARN_MNP
,
3114 "macro `%s' exists, "
3115 "but not taking %d parameters",
3116 mstart
->text
, nparam
);
3119 if (m
&& m
->in_progress
)
3121 if (!m
) { /* in progess or didn't find '(' or wrong nparam */
3123 * Design question: should we handle !tline, which
3124 * indicates missing ')' here, or expand those
3125 * macros anyway, which requires the (t) test a few
3129 nasm_free(paramsize
);
3133 * Expand the macro: we are placed on the last token of the
3134 * call, so that we can easily split the call from the
3135 * following tokens. We also start by pushing an SMAC_END
3136 * token for the cycle removal.
3143 tt
= new_Token(tline
, TOK_SMAC_END
, NULL
, 0);
3145 m
->in_progress
= true;
3147 for (t
= m
->expansion
; t
; t
= t
->next
) {
3148 if (t
->type
>= TOK_SMAC_PARAM
) {
3149 Token
*pcopy
= tline
, **ptail
= &pcopy
;
3153 ttt
= params
[t
->type
- TOK_SMAC_PARAM
];
3154 for (i
= paramsize
[t
->type
- TOK_SMAC_PARAM
];
3157 new_Token(tline
, ttt
->type
, ttt
->text
,
3164 tt
= new_Token(tline
, t
->type
, t
->text
, 0);
3170 * Having done that, get rid of the macro call, and clean
3171 * up the parameters.
3174 nasm_free(paramsize
);
3176 continue; /* main token loop */
3181 if (tline
->type
== TOK_SMAC_END
) {
3182 tline
->mac
->in_progress
= false;
3183 tline
= delete_Token(tline
);
3186 tline
= tline
->next
;
3194 * Now scan the entire line and look for successive TOK_IDs that resulted
3195 * after expansion (they can't be produced by tokenize()). The successive
3196 * TOK_IDs should be concatenated.
3197 * Also we look for %+ tokens and concatenate the tokens before and after
3198 * them (without white spaces in between).
3203 while (t
&& t
->type
!= TOK_ID
&& t
->type
!= TOK_PREPROC_ID
)
3207 if (t
->next
->type
== TOK_ID
||
3208 t
->next
->type
== TOK_PREPROC_ID
||
3209 t
->next
->type
== TOK_NUMBER
) {
3210 char *p
= nasm_strcat(t
->text
, t
->next
->text
);
3212 t
->next
= delete_Token(t
->next
);
3215 } else if (t
->next
->type
== TOK_WHITESPACE
&& t
->next
->next
&&
3216 t
->next
->next
->type
== TOK_PREPROC_ID
&&
3217 strcmp(t
->next
->next
->text
, "%+") == 0) {
3218 /* free the next whitespace, the %+ token and next whitespace */
3220 for (i
= 1; i
<= 3; i
++) {
3222 || (i
!= 2 && t
->next
->type
!= TOK_WHITESPACE
))
3224 t
->next
= delete_Token(t
->next
);
3229 /* If we concatenaded something, re-scan the line for macros */
3237 *org_tline
= *thead
;
3238 /* since we just gave text to org_line, don't free it */
3240 delete_Token(thead
);
3242 /* the expression expanded to empty line;
3243 we can't return NULL for some reasons
3244 we just set the line to a single WHITESPACE token. */
3245 memset(org_tline
, 0, sizeof(*org_tline
));
3246 org_tline
->text
= NULL
;
3247 org_tline
->type
= TOK_WHITESPACE
;
3256 * Similar to expand_smacro but used exclusively with macro identifiers
3257 * right before they are fetched in. The reason is that there can be
3258 * identifiers consisting of several subparts. We consider that if there
3259 * are more than one element forming the name, user wants a expansion,
3260 * otherwise it will be left as-is. Example:
3264 * the identifier %$abc will be left as-is so that the handler for %define
3265 * will suck it and define the corresponding value. Other case:
3267 * %define _%$abc cde
3269 * In this case user wants name to be expanded *before* %define starts
3270 * working, so we'll expand %$abc into something (if it has a value;
3271 * otherwise it will be left as-is) then concatenate all successive
3274 static Token
*expand_id(Token
* tline
)
3276 Token
*cur
, *oldnext
= NULL
;
3278 if (!tline
|| !tline
->next
)
3283 (cur
->next
->type
== TOK_ID
||
3284 cur
->next
->type
== TOK_PREPROC_ID
3285 || cur
->next
->type
== TOK_NUMBER
))
3288 /* If identifier consists of just one token, don't expand */
3293 oldnext
= cur
->next
; /* Detach the tail past identifier */
3294 cur
->next
= NULL
; /* so that expand_smacro stops here */
3297 tline
= expand_smacro(tline
);
3300 /* expand_smacro possibly changhed tline; re-scan for EOL */
3302 while (cur
&& cur
->next
)
3305 cur
->next
= oldnext
;
3312 * Determine whether the given line constitutes a multi-line macro
3313 * call, and return the MMacro structure called if so. Doesn't have
3314 * to check for an initial label - that's taken care of in
3315 * expand_mmacro - but must check numbers of parameters. Guaranteed
3316 * to be called with tline->type == TOK_ID, so the putative macro
3317 * name is easy to find.
3319 static MMacro
*is_mmacro(Token
* tline
, Token
*** params_array
)
3325 head
= (MMacro
*) hash_findix(mmacros
, tline
->text
);
3328 * Efficiency: first we see if any macro exists with the given
3329 * name. If not, we can return NULL immediately. _Then_ we
3330 * count the parameters, and then we look further along the
3331 * list if necessary to find the proper MMacro.
3333 for (m
= head
; m
; m
= m
->next
)
3334 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
3340 * OK, we have a potential macro. Count and demarcate the
3343 count_mmac_params(tline
->next
, &nparam
, ¶ms
);
3346 * So we know how many parameters we've got. Find the MMacro
3347 * structure that handles this number.
3350 if (m
->nparam_min
<= nparam
3351 && (m
->plus
|| nparam
<= m
->nparam_max
)) {
3353 * This one is right. Just check if cycle removal
3354 * prohibits us using it before we actually celebrate...
3356 if (m
->in_progress
) {
3359 "self-reference in multi-line macro `%s'", m
->name
);
3365 * It's right, and we can use it. Add its default
3366 * parameters to the end of our list if necessary.
3368 if (m
->defaults
&& nparam
< m
->nparam_min
+ m
->ndefs
) {
3370 nasm_realloc(params
,
3371 ((m
->nparam_min
+ m
->ndefs
+
3372 1) * sizeof(*params
)));
3373 while (nparam
< m
->nparam_min
+ m
->ndefs
) {
3374 params
[nparam
] = m
->defaults
[nparam
- m
->nparam_min
];
3379 * If we've gone over the maximum parameter count (and
3380 * we're in Plus mode), ignore parameters beyond
3383 if (m
->plus
&& nparam
> m
->nparam_max
)
3384 nparam
= m
->nparam_max
;
3386 * Then terminate the parameter list, and leave.
3388 if (!params
) { /* need this special case */
3389 params
= nasm_malloc(sizeof(*params
));
3392 params
[nparam
] = NULL
;
3393 *params_array
= params
;
3397 * This one wasn't right: look for the next one with the
3400 for (m
= m
->next
; m
; m
= m
->next
)
3401 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
3406 * After all that, we didn't find one with the right number of
3407 * parameters. Issue a warning, and fail to expand the macro.
3409 error(ERR_WARNING
| ERR_WARN_MNP
,
3410 "macro `%s' exists, but not taking %d parameters",
3411 tline
->text
, nparam
);
3417 * Expand the multi-line macro call made by the given line, if
3418 * there is one to be expanded. If there is, push the expansion on
3419 * istk->expansion and return 1. Otherwise return 0.
3421 static int expand_mmacro(Token
* tline
)
3423 Token
*startline
= tline
;
3424 Token
*label
= NULL
;
3425 int dont_prepend
= 0;
3426 Token
**params
, *t
, *tt
;
3429 int i
, nparam
, *paramlen
;
3433 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
3434 if (!tok_type_(t
, TOK_ID
) && !tok_type_(t
, TOK_PREPROC_ID
))
3436 m
= is_mmacro(t
, ¶ms
);
3440 * We have an id which isn't a macro call. We'll assume
3441 * it might be a label; we'll also check to see if a
3442 * colon follows it. Then, if there's another id after
3443 * that lot, we'll check it again for macro-hood.
3447 if (tok_type_(t
, TOK_WHITESPACE
))
3448 last
= t
, t
= t
->next
;
3449 if (tok_is_(t
, ":")) {
3451 last
= t
, t
= t
->next
;
3452 if (tok_type_(t
, TOK_WHITESPACE
))
3453 last
= t
, t
= t
->next
;
3455 if (!tok_type_(t
, TOK_ID
) || (m
= is_mmacro(t
, ¶ms
)) == NULL
)
3462 * Fix up the parameters: this involves stripping leading and
3463 * trailing whitespace, then stripping braces if they are
3466 for (nparam
= 0; params
[nparam
]; nparam
++) ;
3467 paramlen
= nparam
? nasm_malloc(nparam
* sizeof(*paramlen
)) : NULL
;
3469 for (i
= 0; params
[i
]; i
++) {
3471 int comma
= (!m
->plus
|| i
< nparam
- 1);
3475 if (tok_is_(t
, "{"))
3476 t
= t
->next
, brace
= true, comma
= false;
3480 if (comma
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, ","))
3481 break; /* ... because we have hit a comma */
3482 if (comma
&& t
->type
== TOK_WHITESPACE
3483 && tok_is_(t
->next
, ","))
3484 break; /* ... or a space then a comma */
3485 if (brace
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, "}"))
3486 break; /* ... or a brace */
3493 * OK, we have a MMacro structure together with a set of
3494 * parameters. We must now go through the expansion and push
3495 * copies of each Line on to istk->expansion. Substitution of
3496 * parameter tokens and macro-local tokens doesn't get done
3497 * until the single-line macro substitution process; this is
3498 * because delaying them allows us to change the semantics
3499 * later through %rotate.
3501 * First, push an end marker on to istk->expansion, mark this
3502 * macro as in progress, and set up its invocation-specific
3505 ll
= nasm_malloc(sizeof(Line
));
3506 ll
->next
= istk
->expansion
;
3509 istk
->expansion
= ll
;
3511 m
->in_progress
= true;
3516 m
->paramlen
= paramlen
;
3517 m
->unique
= unique
++;
3520 m
->next_active
= istk
->mstk
;
3523 for (l
= m
->expansion
; l
; l
= l
->next
) {
3526 ll
= nasm_malloc(sizeof(Line
));
3527 ll
->finishes
= NULL
;
3528 ll
->next
= istk
->expansion
;
3529 istk
->expansion
= ll
;
3532 for (t
= l
->first
; t
; t
= t
->next
) {
3534 if (t
->type
== TOK_PREPROC_ID
&&
3535 t
->text
[1] == '0' && t
->text
[2] == '0') {
3541 tt
= *tail
= new_Token(NULL
, x
->type
, x
->text
, 0);
3548 * If we had a label, push it on as the first line of
3549 * the macro expansion.
3552 if (dont_prepend
< 0)
3553 free_tlist(startline
);
3555 ll
= nasm_malloc(sizeof(Line
));
3556 ll
->finishes
= NULL
;
3557 ll
->next
= istk
->expansion
;
3558 istk
->expansion
= ll
;
3559 ll
->first
= startline
;
3560 if (!dont_prepend
) {
3562 label
= label
->next
;
3563 label
->next
= tt
= new_Token(NULL
, TOK_OTHER
, ":", 0);
3568 list
->uplevel(m
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
3574 * Since preprocessor always operate only on the line that didn't
3575 * arrived yet, we should always use ERR_OFFBY1. Also since user
3576 * won't want to see same error twice (preprocessing is done once
3577 * per pass) we will want to show errors only during pass one.
3579 static void error(int severity
, const char *fmt
, ...)
3584 /* If we're in a dead branch of IF or something like it, ignore the error */
3585 if (istk
&& istk
->conds
&& !emitting(istk
->conds
->state
))
3589 vsnprintf(buff
, sizeof(buff
), fmt
, arg
);
3592 if (istk
&& istk
->mstk
&& istk
->mstk
->name
)
3593 _error(severity
| ERR_PASS1
, "(%s:%d) %s", istk
->mstk
->name
,
3594 istk
->mstk
->lineno
, buff
);
3596 _error(severity
| ERR_PASS1
, "%s", buff
);
3600 pp_reset(char *file
, int apass
, efunc errfunc
, evalfunc eval
,
3605 istk
= nasm_malloc(sizeof(Include
));
3608 istk
->expansion
= NULL
;
3610 istk
->fp
= fopen(file
, "r");
3612 src_set_fname(nasm_strdup(file
));
3616 error(ERR_FATAL
| ERR_NOFILE
, "unable to open input file `%s'",
3621 if (tasm_compatible_mode
) {
3624 stdmacpos
= &stdmac
[TASM_MACRO_COUNT
];
3626 any_extrastdmac
= (extrastdmac
!= NULL
);
3632 static char *pp_getline(void)
3639 * Fetch a tokenized line, either from the macro-expansion
3640 * buffer or from the input file.
3643 while (istk
->expansion
&& istk
->expansion
->finishes
) {
3644 Line
*l
= istk
->expansion
;
3645 if (!l
->finishes
->name
&& l
->finishes
->in_progress
> 1) {
3649 * This is a macro-end marker for a macro with no
3650 * name, which means it's not really a macro at all
3651 * but a %rep block, and the `in_progress' field is
3652 * more than 1, meaning that we still need to
3653 * repeat. (1 means the natural last repetition; 0
3654 * means termination by %exitrep.) We have
3655 * therefore expanded up to the %endrep, and must
3656 * push the whole block on to the expansion buffer
3657 * again. We don't bother to remove the macro-end
3658 * marker: we'd only have to generate another one
3661 l
->finishes
->in_progress
--;
3662 for (l
= l
->finishes
->expansion
; l
; l
= l
->next
) {
3663 Token
*t
, *tt
, **tail
;
3665 ll
= nasm_malloc(sizeof(Line
));
3666 ll
->next
= istk
->expansion
;
3667 ll
->finishes
= NULL
;
3671 for (t
= l
->first
; t
; t
= t
->next
) {
3672 if (t
->text
|| t
->type
== TOK_WHITESPACE
) {
3674 new_Token(NULL
, t
->type
, t
->text
, 0);
3679 istk
->expansion
= ll
;
3683 * Check whether a `%rep' was started and not ended
3684 * within this macro expansion. This can happen and
3685 * should be detected. It's a fatal error because
3686 * I'm too confused to work out how to recover
3692 "defining with name in expansion");
3693 else if (istk
->mstk
->name
)
3695 "`%%rep' without `%%endrep' within"
3696 " expansion of macro `%s'",
3701 * FIXME: investigate the relationship at this point between
3702 * istk->mstk and l->finishes
3705 MMacro
*m
= istk
->mstk
;
3706 istk
->mstk
= m
->next_active
;
3709 * This was a real macro call, not a %rep, and
3710 * therefore the parameter information needs to
3713 nasm_free(m
->params
);
3714 free_tlist(m
->iline
);
3715 nasm_free(m
->paramlen
);
3716 l
->finishes
->in_progress
= false;
3720 istk
->expansion
= l
->next
;
3722 list
->downlevel(LIST_MACRO
);
3725 while (1) { /* until we get a line we can use */
3727 if (istk
->expansion
) { /* from a macro expansion */
3729 Line
*l
= istk
->expansion
;
3731 istk
->mstk
->lineno
++;
3733 istk
->expansion
= l
->next
;
3735 p
= detoken(tline
, false);
3736 list
->line(LIST_MACRO
, p
);
3741 if (line
) { /* from the current input file */
3742 line
= prepreproc(line
);
3743 tline
= tokenize(line
);
3748 * The current file has ended; work down the istk
3755 "expected `%%endif' before end of file");
3756 /* only set line and file name if there's a next node */
3758 src_set_linnum(i
->lineno
);
3759 nasm_free(src_set_fname(i
->fname
));
3762 list
->downlevel(LIST_INCLUDE
);
3770 * We must expand MMacro parameters and MMacro-local labels
3771 * _before_ we plunge into directive processing, to cope
3772 * with things like `%define something %1' such as STRUC
3773 * uses. Unless we're _defining_ a MMacro, in which case
3774 * those tokens should be left alone to go into the
3775 * definition; and unless we're in a non-emitting
3776 * condition, in which case we don't want to meddle with
3779 if (!defining
&& !(istk
->conds
&& !emitting(istk
->conds
->state
)))
3780 tline
= expand_mmac_params(tline
);
3783 * Check the line to see if it's a preprocessor directive.
3785 if (do_directive(tline
) == DIRECTIVE_FOUND
) {
3787 } else if (defining
) {
3789 * We're defining a multi-line macro. We emit nothing
3791 * shove the tokenized line on to the macro definition.
3793 Line
*l
= nasm_malloc(sizeof(Line
));
3794 l
->next
= defining
->expansion
;
3796 l
->finishes
= false;
3797 defining
->expansion
= l
;
3799 } else if (istk
->conds
&& !emitting(istk
->conds
->state
)) {
3801 * We're in a non-emitting branch of a condition block.
3802 * Emit nothing at all, not even a blank line: when we
3803 * emerge from the condition we'll give a line-number
3804 * directive so we keep our place correctly.
3808 } else if (istk
->mstk
&& !istk
->mstk
->in_progress
) {
3810 * We're in a %rep block which has been terminated, so
3811 * we're walking through to the %endrep without
3812 * emitting anything. Emit nothing at all, not even a
3813 * blank line: when we emerge from the %rep block we'll
3814 * give a line-number directive so we keep our place
3820 tline
= expand_smacro(tline
);
3821 if (!expand_mmacro(tline
)) {
3823 * De-tokenize the line again, and emit it.
3825 line
= detoken(tline
, true);
3829 continue; /* expand_mmacro calls free_tlist */
3837 static void pp_cleanup(int pass
)
3840 error(ERR_NONFATAL
, "end of file while still defining macro `%s'",
3842 free_mmacro(defining
);
3851 nasm_free(i
->fname
);
3862 void pp_include_path(char *path
)
3866 i
= nasm_malloc(sizeof(IncPath
));
3867 i
->path
= path
? nasm_strdup(path
) : NULL
;
3870 if (ipath
!= NULL
) {
3872 while (j
->next
!= NULL
)
3883 * This function is used to "export" the include paths, e.g.
3884 * the paths specified in the '-I' command switch.
3885 * The need for such exporting is due to the 'incbin' directive,
3886 * which includes raw binary files (unlike '%include', which
3887 * includes text source files). It would be real nice to be
3888 * able to specify paths to search for incbin'ned files also.
3889 * So, this is a simple workaround.
3891 * The function use is simple:
3893 * The 1st call (with NULL argument) returns a pointer to the 1st path
3894 * (char** type) or NULL if none include paths available.
3896 * All subsequent calls take as argument the value returned by this
3897 * function last. The return value is either the next path
3898 * (char** type) or NULL if the end of the paths list is reached.
3900 * It is maybe not the best way to do things, but I didn't want
3901 * to export too much, just one or two functions and no types or
3902 * variables exported.
3904 * Can't say I like the current situation with e.g. this path list either,
3905 * it seems to be never deallocated after creation...
3907 char **pp_get_include_path_ptr(char **pPrevPath
)
3909 /* This macro returns offset of a member of a structure */
3910 #define GetMemberOffset(StructType,MemberName)\
3911 ((size_t)&((StructType*)0)->MemberName)
3914 if (pPrevPath
== NULL
) {
3916 return &ipath
->path
;
3920 i
= (IncPath
*) ((char *)pPrevPath
- GetMemberOffset(IncPath
, path
));
3926 #undef GetMemberOffset
3929 void pp_pre_include(char *fname
)
3931 Token
*inc
, *space
, *name
;
3934 name
= new_Token(NULL
, TOK_INTERNAL_STRING
, fname
, 0);
3935 space
= new_Token(name
, TOK_WHITESPACE
, NULL
, 0);
3936 inc
= new_Token(space
, TOK_PREPROC_ID
, "%include", 0);
3938 l
= nasm_malloc(sizeof(Line
));
3941 l
->finishes
= false;
3945 void pp_pre_define(char *definition
)
3951 equals
= strchr(definition
, '=');
3952 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
3953 def
= new_Token(space
, TOK_PREPROC_ID
, "%define", 0);
3956 space
->next
= tokenize(definition
);
3960 l
= nasm_malloc(sizeof(Line
));
3963 l
->finishes
= false;
3967 void pp_pre_undefine(char *definition
)
3972 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
3973 def
= new_Token(space
, TOK_PREPROC_ID
, "%undef", 0);
3974 space
->next
= tokenize(definition
);
3976 l
= nasm_malloc(sizeof(Line
));
3979 l
->finishes
= false;
3984 * Added by Keith Kanios:
3986 * This function is used to assist with "runtime" preprocessor
3987 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
3989 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
3990 * PASS A VALID STRING TO THIS FUNCTION!!!!!
3993 void pp_runtime(char *definition
)
3997 def
= tokenize(definition
);
3998 if(do_directive(def
) == NO_DIRECTIVE_FOUND
)
4003 void pp_extra_stdmac(const char **macros
)
4005 extrastdmac
= macros
;
4008 static void make_tok_num(Token
* tok
, int64_t val
)
4011 snprintf(numbuf
, sizeof(numbuf
), "%"PRId64
"", val
);
4012 tok
->text
= nasm_strdup(numbuf
);
4013 tok
->type
= TOK_NUMBER
;