1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2009 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * preproc.c macro preprocessor for the Netwide Assembler
38 /* Typical flow of text through preproc
40 * pp_getline gets tokenized lines, either
42 * from a macro expansion
46 * read_line gets raw text from stdmacpos, or predef, or current input file
47 * tokenize converts to tokens
50 * expand_mmac_params is used to expand %1 etc., unless a macro is being
51 * defined or a false conditional is being processed
52 * (%0, %1, %+1, %-1, %%foo
54 * do_directive checks for directives
56 * expand_smacro is used to expand single line macros
58 * expand_mmacro is used to expand multi-line macros
60 * detoken is used to convert the line back to text
83 typedef struct SMacro SMacro
;
84 typedef struct MMacro MMacro
;
85 typedef struct MMacroInvocation MMacroInvocation
;
86 typedef struct Context Context
;
87 typedef struct Token Token
;
88 typedef struct Blocks Blocks
;
89 typedef struct Line Line
;
90 typedef struct Include Include
;
91 typedef struct Cond Cond
;
92 typedef struct IncPath IncPath
;
95 * Note on the storage of both SMacro and MMacros: the hash table
96 * indexes them case-insensitively, and we then have to go through a
97 * linked list of potential case aliases (and, for MMacros, parameter
98 * ranges); this is to preserve the matching semantics of the earlier
99 * code. If the number of case aliases for a specific macro is a
100 * performance issue, you may want to reconsider your coding style.
104 * Store the definition of a single-line macro.
116 * Store the definition of a multi-line macro. This is also used to
117 * store the interiors of `%rep...%endrep' blocks, which are
118 * effectively self-re-invoking multi-line macros which simply
119 * don't have a name or bother to appear in the hash tables. %rep
120 * blocks are signified by having a NULL `name' field.
122 * In a MMacro describing a `%rep' block, the `in_progress' field
123 * isn't merely boolean, but gives the number of repeats left to
126 * The `next' field is used for storing MMacros in hash tables; the
127 * `next_active' field is for stacking them on istk entries.
129 * When a MMacro is being expanded, `params', `iline', `nparam',
130 * `paramlen', `rotate' and `unique' are local to the invocation.
134 MMacroInvocation
*prev
; /* previous invocation */
136 int nparam_min
, nparam_max
;
138 bool plus
; /* is the last parameter greedy? */
139 bool nolist
; /* is this macro listing-inhibited? */
140 int64_t in_progress
; /* is this macro currently being expanded? */
141 int64_t max_depth
; /* maximum number of recursive expansions allowed */
142 Token
*dlist
; /* All defaults as one list */
143 Token
**defaults
; /* Parameter default pointers */
144 int ndefs
; /* number of default parameters */
148 MMacro
*rep_nest
; /* used for nesting %rep */
149 Token
**params
; /* actual parameters */
150 Token
*iline
; /* invocation line */
151 unsigned int nparam
, rotate
;
154 int lineno
; /* Current line number on expansion */
158 /* Store the definition of a multi-line macro, as defined in a
159 * previous recursive macro expansion.
161 struct MMacroInvocation
{
162 MMacroInvocation
*prev
; /* previous invocation */
163 Token
**params
; /* actual parameters */
164 Token
*iline
; /* invocation line */
165 unsigned int nparam
, rotate
;
172 * The context stack is composed of a linked list of these.
177 struct hash_table localmac
;
182 * This is the internal form which we break input lines up into.
183 * Typically stored in linked lists.
185 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
186 * necessarily used as-is, but is intended to denote the number of
187 * the substituted parameter. So in the definition
189 * %define a(x,y) ( (x) & ~(y) )
191 * the token representing `x' will have its type changed to
192 * TOK_SMAC_PARAM, but the one representing `y' will be
195 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
196 * which doesn't need quotes around it. Used in the pre-include
197 * mechanism as an alternative to trying to find a sensible type of
198 * quote to use on the filename we were passed.
201 TOK_NONE
= 0, TOK_WHITESPACE
, TOK_COMMENT
, TOK_ID
,
202 TOK_PREPROC_ID
, TOK_STRING
,
203 TOK_NUMBER
, TOK_FLOAT
, TOK_SMAC_END
, TOK_OTHER
,
205 TOK_PREPROC_Q
, TOK_PREPROC_QQ
,
207 TOK_INDIRECT
, /* %[...] */
208 TOK_SMAC_PARAM
, /* MUST BE LAST IN THE LIST!!! */
209 TOK_MAX
= INT_MAX
/* Keep compiler from reducing the range */
216 SMacro
*mac
; /* associated macro for TOK_SMAC_END */
217 size_t len
; /* scratch length field */
218 } a
; /* Auxiliary data */
219 enum pp_token_type type
;
223 * Multi-line macro definitions are stored as a linked list of
224 * these, which is essentially a container to allow several linked
227 * Note that in this module, linked lists are treated as stacks
228 * wherever possible. For this reason, Lines are _pushed_ on to the
229 * `expansion' field in MMacro structures, so that the linked list,
230 * if walked, would give the macro lines in reverse order; this
231 * means that we can walk the list when expanding a macro, and thus
232 * push the lines on to the `expansion' field in _istk_ in reverse
233 * order (so that when popped back off they are in the right
234 * order). It may seem cockeyed, and it relies on my design having
235 * an even number of steps in, but it works...
237 * Some of these structures, rather than being actual lines, are
238 * markers delimiting the end of the expansion of a given macro.
239 * This is for use in the cycle-tracking and %rep-handling code.
240 * Such structures have `finishes' non-NULL, and `first' NULL. All
241 * others have `finishes' NULL, but `first' may still be NULL if
251 * To handle an arbitrary level of file inclusion, we maintain a
252 * stack (ie linked list) of these things.
261 MMacro
*mstk
; /* stack of active macros/reps */
265 * Include search path. This is simply a list of strings which get
266 * prepended, in turn, to the name of an include file, in an
267 * attempt to find the file if it's not in the current directory.
275 * Conditional assembly: we maintain a separate stack of these for
276 * each level of file inclusion. (The only reason we keep the
277 * stacks separate is to ensure that a stray `%endif' in a file
278 * included from within the true branch of a `%if' won't terminate
279 * it and cause confusion: instead, rightly, it'll cause an error.)
287 * These states are for use just after %if or %elif: IF_TRUE
288 * means the condition has evaluated to truth so we are
289 * currently emitting, whereas IF_FALSE means we are not
290 * currently emitting but will start doing so if a %else comes
291 * up. In these states, all directives are admissible: %elif,
292 * %else and %endif. (And of course %if.)
294 COND_IF_TRUE
, COND_IF_FALSE
,
296 * These states come up after a %else: ELSE_TRUE means we're
297 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
298 * any %elif or %else will cause an error.
300 COND_ELSE_TRUE
, COND_ELSE_FALSE
,
302 * These states mean that we're not emitting now, and also that
303 * nothing until %endif will be emitted at all. COND_DONE is
304 * used when we've had our moment of emission
305 * and have now started seeing %elifs. COND_NEVER is used when
306 * the condition construct in question is contained within a
307 * non-emitting branch of a larger condition construct,
308 * or if there is an error.
310 COND_DONE
, COND_NEVER
312 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
315 * These defines are used as the possible return values for do_directive
317 #define NO_DIRECTIVE_FOUND 0
318 #define DIRECTIVE_FOUND 1
321 * Condition codes. Note that we use c_ prefix not C_ because C_ is
322 * used in nasm.h for the "real" condition codes. At _this_ level,
323 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
324 * ones, so we need a different enum...
326 static const char * const conditions
[] = {
327 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
328 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
329 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
332 c_A
, c_AE
, c_B
, c_BE
, c_C
, c_CXZ
, c_E
, c_ECXZ
, c_G
, c_GE
, c_L
, c_LE
,
333 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, c_NE
, c_NG
, c_NGE
, c_NL
, c_NLE
, c_NO
,
334 c_NP
, c_NS
, c_NZ
, c_O
, c_P
, c_PE
, c_PO
, c_RCXZ
, c_S
, c_Z
,
337 static const enum pp_conds inverse_ccs
[] = {
338 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, -1, c_NE
, -1, c_NG
, c_NGE
, c_NL
, c_NLE
,
339 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
,
340 c_Z
, c_NO
, c_NP
, c_PO
, c_PE
, -1, c_NS
, c_NZ
346 /* If this is a an IF, ELIF, ELSE or ENDIF keyword */
347 static int is_condition(enum preproc_token arg
)
349 return PP_IS_COND(arg
) || (arg
== PP_ELSE
) || (arg
== PP_ENDIF
);
352 /* For TASM compatibility we need to be able to recognise TASM compatible
353 * conditional compilation directives. Using the NASM pre-processor does
354 * not work, so we look for them specifically from the following list and
355 * then jam in the equivalent NASM directive into the input stream.
359 TM_ARG
, TM_ELIF
, TM_ELSE
, TM_ENDIF
, TM_IF
, TM_IFDEF
, TM_IFDIFI
,
360 TM_IFNDEF
, TM_INCLUDE
, TM_LOCAL
363 static const char * const tasm_directives
[] = {
364 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
365 "ifndef", "include", "local"
368 static int StackSize
= 4;
369 static char *StackPointer
= "ebp";
370 static int ArgOffset
= 8;
371 static int LocalOffset
= 0;
373 static Context
*cstk
;
374 static Include
*istk
;
375 static IncPath
*ipath
= NULL
;
377 static efunc _error
; /* Pointer to client-provided error reporting function */
378 static evalfunc evaluate
;
380 static int pass
; /* HACK: pass 0 = generate dependencies only */
381 static StrList
**dephead
, **deptail
; /* Dependency list */
383 static uint64_t unique
; /* unique identifier numbers */
385 static Line
*predef
= NULL
;
386 static bool do_predef
;
388 static ListGen
*list
;
391 * The current set of multi-line macros we have defined.
393 static struct hash_table mmacros
;
396 * The current set of single-line macros we have defined.
398 static struct hash_table smacros
;
401 * The multi-line macro we are currently defining, or the %rep
402 * block we are currently reading, if any.
404 static MMacro
*defining
;
406 static uint64_t nested_mac_count
;
407 static uint64_t nested_rep_count
;
410 * The number of macro parameters to allocate space for at a time.
412 #define PARAM_DELTA 16
415 * The standard macro set: defined in macros.c in the array nasm_stdmac.
416 * This gives our position in the macro set, when we're processing it.
418 static macros_t
*stdmacpos
;
421 * The extra standard macros that come from the object format, if
424 static macros_t
*extrastdmac
= NULL
;
425 static bool any_extrastdmac
;
428 * Tokens are allocated in blocks to improve speed
430 #define TOKEN_BLOCKSIZE 4096
431 static Token
*freeTokens
= NULL
;
437 static Blocks blocks
= { NULL
, NULL
};
440 * Forward declarations.
442 static Token
*expand_mmac_params(Token
* tline
);
443 static Token
*expand_smacro(Token
* tline
);
444 static Token
*expand_id(Token
* tline
);
445 static Context
*get_ctx(const char *name
, const char **namep
,
447 static void make_tok_num(Token
* tok
, int64_t val
);
448 static void error(int severity
, const char *fmt
, ...);
449 static void error_precond(int severity
, const char *fmt
, ...);
450 static void *new_Block(size_t size
);
451 static void delete_Blocks(void);
452 static Token
*new_Token(Token
* next
, enum pp_token_type type
,
453 const char *text
, int txtlen
);
454 static Token
*delete_Token(Token
* t
);
457 * Macros for safe checking of token pointers, avoid *(NULL)
459 #define tok_type_(x,t) ((x) && (x)->type == (t))
460 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
461 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
462 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
464 /* Handle TASM specific directives, which do not contain a % in
465 * front of them. We do it here because I could not find any other
466 * place to do it for the moment, and it is a hack (ideally it would
467 * be nice to be able to use the NASM pre-processor to do it).
469 static char *check_tasm_directive(char *line
)
471 int32_t i
, j
, k
, m
, len
;
472 char *p
= line
, *oldline
, oldchar
;
474 /* Skip whitespace */
475 while (nasm_isspace(*p
) && *p
!= 0)
478 /* Binary search for the directive name */
480 j
= elements(tasm_directives
);
482 while (!nasm_isspace(p
[len
]) && p
[len
] != 0)
489 m
= nasm_stricmp(p
, tasm_directives
[k
]);
491 /* We have found a directive, so jam a % in front of it
492 * so that NASM will then recognise it as one if it's own.
497 line
= nasm_malloc(len
+ 2);
499 if (k
== TM_IFDIFI
) {
501 * NASM does not recognise IFDIFI, so we convert
502 * it to %if 0. This is not used in NASM
503 * compatible code, but does need to parse for the
504 * TASM macro package.
506 strcpy(line
+ 1, "if 0");
508 memcpy(line
+ 1, p
, len
+ 1);
523 * The pre-preprocessing stage... This function translates line
524 * number indications as they emerge from GNU cpp (`# lineno "file"
525 * flags') into NASM preprocessor line number indications (`%line
528 static char *prepreproc(char *line
)
531 char *fname
, *oldline
;
533 if (line
[0] == '#' && line
[1] == ' ') {
536 lineno
= atoi(fname
);
537 fname
+= strspn(fname
, "0123456789 ");
540 fnlen
= strcspn(fname
, "\"");
541 line
= nasm_malloc(20 + fnlen
);
542 snprintf(line
, 20 + fnlen
, "%%line %d %.*s", lineno
, fnlen
, fname
);
545 if (tasm_compatible_mode
)
546 return check_tasm_directive(line
);
551 * Free a linked list of tokens.
553 static void free_tlist(Token
* list
)
556 list
= delete_Token(list
);
561 * Free a linked list of lines.
563 static void free_llist(Line
* list
)
569 free_tlist(l
->first
);
577 static void free_mmacro(MMacro
* m
)
580 free_tlist(m
->dlist
);
581 nasm_free(m
->defaults
);
582 free_llist(m
->expansion
);
587 * Free all currently defined macros, and free the hash tables
589 static void free_smacro_table(struct hash_table
*smt
)
593 struct hash_tbl_node
*it
= NULL
;
595 while ((s
= hash_iterate(smt
, &it
, &key
)) != NULL
) {
596 nasm_free((void *)key
);
598 SMacro
*ns
= s
->next
;
600 free_tlist(s
->expansion
);
608 static void free_mmacro_table(struct hash_table
*mmt
)
612 struct hash_tbl_node
*it
= NULL
;
615 while ((m
= hash_iterate(mmt
, &it
, &key
)) != NULL
) {
616 nasm_free((void *)key
);
618 MMacro
*nm
= m
->next
;
626 static void free_macros(void)
628 free_smacro_table(&smacros
);
629 free_mmacro_table(&mmacros
);
633 * Initialize the hash tables
635 static void init_macros(void)
637 hash_init(&smacros
, HASH_LARGE
);
638 hash_init(&mmacros
, HASH_LARGE
);
642 * Pop the context stack.
644 static void ctx_pop(void)
649 free_smacro_table(&c
->localmac
);
655 * Search for a key in the hash index; adding it if necessary
656 * (in which case we initialize the data pointer to NULL.)
659 hash_findi_add(struct hash_table
*hash
, const char *str
)
661 struct hash_insert hi
;
665 r
= hash_findi(hash
, str
, &hi
);
669 strx
= nasm_strdup(str
); /* Use a more efficient allocator here? */
670 return hash_add(&hi
, strx
, NULL
);
674 * Like hash_findi, but returns the data element rather than a pointer
675 * to it. Used only when not adding a new element, hence no third
679 hash_findix(struct hash_table
*hash
, const char *str
)
683 p
= hash_findi(hash
, str
, NULL
);
684 return p
? *p
: NULL
;
687 #define BUF_DELTA 512
689 * Read a line from the top file in istk, handling multiple CR/LFs
690 * at the end of the line read, and handling spurious ^Zs. Will
691 * return lines from the standard macro set if this has not already
694 static char *read_line(void)
696 char *buffer
, *p
, *q
;
697 int bufsize
, continued_count
;
701 const unsigned char *p
= stdmacpos
;
706 len
+= pp_directives_len
[c
-0x80]+1;
710 ret
= nasm_malloc(len
+1);
712 while ((c
= *stdmacpos
++)) {
714 memcpy(q
, pp_directives
[c
-0x80], pp_directives_len
[c
-0x80]);
715 q
+= pp_directives_len
[c
-0x80];
725 /* This was the last of the standard macro chain... */
727 if (any_extrastdmac
) {
728 stdmacpos
= extrastdmac
;
729 any_extrastdmac
= false;
730 } else if (do_predef
) {
732 Token
*head
, **tail
, *t
;
735 * Nasty hack: here we push the contents of
736 * `predef' on to the top-level expansion stack,
737 * since this is the most convenient way to
738 * implement the pre-include and pre-define
741 for (pd
= predef
; pd
; pd
= pd
->next
) {
744 for (t
= pd
->first
; t
; t
= t
->next
) {
745 *tail
= new_Token(NULL
, t
->type
, t
->text
, 0);
746 tail
= &(*tail
)->next
;
748 l
= nasm_malloc(sizeof(Line
));
749 l
->next
= istk
->expansion
;
761 buffer
= nasm_malloc(BUF_DELTA
);
765 q
= fgets(p
, bufsize
- (p
- buffer
), istk
->fp
);
769 if (p
> buffer
&& p
[-1] == '\n') {
770 /* Convert backslash-CRLF line continuation sequences into
771 nothing at all (for DOS and Windows) */
772 if (((p
- 2) > buffer
) && (p
[-3] == '\\') && (p
[-2] == '\r')) {
777 /* Also convert backslash-LF line continuation sequences into
778 nothing at all (for Unix) */
779 else if (((p
- 1) > buffer
) && (p
[-2] == '\\')) {
787 if (p
- buffer
> bufsize
- 10) {
788 int32_t offset
= p
- buffer
;
789 bufsize
+= BUF_DELTA
;
790 buffer
= nasm_realloc(buffer
, bufsize
);
791 p
= buffer
+ offset
; /* prevent stale-pointer problems */
795 if (!q
&& p
== buffer
) {
800 src_set_linnum(src_get_linnum() + istk
->lineinc
+
801 (continued_count
* istk
->lineinc
));
804 * Play safe: remove CRs as well as LFs, if any of either are
805 * present at the end of the line.
807 while (--p
>= buffer
&& (*p
== '\n' || *p
== '\r'))
811 * Handle spurious ^Z, which may be inserted into source files
812 * by some file transfer utilities.
814 buffer
[strcspn(buffer
, "\032")] = '\0';
816 list
->line(LIST_READ
, buffer
);
822 * Tokenize a line of text. This is a very simple process since we
823 * don't need to parse the value out of e.g. numeric tokens: we
824 * simply split one string into many.
826 static Token
*tokenize(char *line
)
829 enum pp_token_type type
;
831 Token
*t
, **tail
= &list
;
837 if (*p
== '+' && !nasm_isdigit(p
[1])) {
840 } else if (nasm_isdigit(*p
) ||
841 ((*p
== '-' || *p
== '+') && nasm_isdigit(p
[1]))) {
845 while (nasm_isdigit(*p
));
846 type
= TOK_PREPROC_ID
;
847 } else if (*p
== '{') {
849 while (*p
&& *p
!= '}') {
856 type
= TOK_PREPROC_ID
;
857 } else if (*p
== '[') {
859 line
+= 2; /* Skip the leading %[ */
861 while (lvl
&& (c
= *p
++)) {
873 p
= nasm_skip_string(p
)+1;
883 error(ERR_NONFATAL
, "unterminated %[ construct");
885 } else if (*p
== '?') {
886 type
= TOK_PREPROC_Q
; /* %? */
889 type
= TOK_PREPROC_QQ
; /* %?? */
892 } else if (isidchar(*p
) ||
893 ((*p
== '!' || *p
== '%' || *p
== '$') &&
898 while (isidchar(*p
));
899 type
= TOK_PREPROC_ID
;
905 } else if (isidstart(*p
) || (*p
== '$' && isidstart(p
[1]))) {
908 while (*p
&& isidchar(*p
))
910 } else if (*p
== '\'' || *p
== '"' || *p
== '`') {
915 p
= nasm_skip_string(p
);
920 error(ERR_WARNING
|ERR_PASS1
, "unterminated string");
921 /* Handling unterminated strings by UNV */
924 } else if (p
[0] == '$' && p
[1] == '$') {
925 type
= TOK_OTHER
; /* TOKEN_BASE */
927 } else if (isnumstart(*p
)) {
929 bool is_float
= false;
945 if (!is_hex
&& (c
== 'e' || c
== 'E')) {
947 if (*p
== '+' || *p
== '-') {
948 /* e can only be followed by +/- if it is either a
949 prefixed hex number or a floating-point number */
953 } else if (c
== 'H' || c
== 'h' || c
== 'X' || c
== 'x') {
955 } else if (c
== 'P' || c
== 'p') {
957 if (*p
== '+' || *p
== '-')
959 } else if (isnumchar(c
) || c
== '_')
962 /* we need to deal with consequences of the legacy
963 parser, like "1.nolist" being two tokens
964 (TOK_NUMBER, TOK_ID) here; at least give it
965 a shot for now. In the future, we probably need
966 a flex-based scanner with proper pattern matching
967 to do it as well as it can be done. Nothing in
968 the world is going to help the person who wants
969 0x123.p16 interpreted as two tokens, though. */
974 if (nasm_isdigit(*r
) || (is_hex
&& nasm_isxdigit(*r
)) ||
975 (!is_hex
&& (*r
== 'e' || *r
== 'E')) ||
976 (*r
== 'p' || *r
== 'P')) {
980 break; /* Terminate the token */
984 p
--; /* Point to first character beyond number */
986 if (p
== line
+1 && *line
== '$') {
987 type
= TOK_OTHER
; /* TOKEN_HERE */
989 if (has_e
&& !is_hex
) {
990 /* 1e13 is floating-point, but 1e13h is not */
994 type
= is_float
? TOK_FLOAT
: TOK_NUMBER
;
996 } else if (nasm_isspace(*p
)) {
997 type
= TOK_WHITESPACE
;
999 while (*p
&& nasm_isspace(*p
))
1002 * Whitespace just before end-of-line is discarded by
1003 * pretending it's a comment; whitespace just before a
1004 * comment gets lumped into the comment.
1006 if (!*p
|| *p
== ';') {
1011 } else if (*p
== ';') {
1017 * Anything else is an operator of some kind. We check
1018 * for all the double-character operators (>>, <<, //,
1019 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
1020 * else is a single-character operator.
1023 if ((p
[0] == '>' && p
[1] == '>') ||
1024 (p
[0] == '<' && p
[1] == '<') ||
1025 (p
[0] == '/' && p
[1] == '/') ||
1026 (p
[0] == '<' && p
[1] == '=') ||
1027 (p
[0] == '>' && p
[1] == '=') ||
1028 (p
[0] == '=' && p
[1] == '=') ||
1029 (p
[0] == '!' && p
[1] == '=') ||
1030 (p
[0] == '<' && p
[1] == '>') ||
1031 (p
[0] == '&' && p
[1] == '&') ||
1032 (p
[0] == '|' && p
[1] == '|') ||
1033 (p
[0] == '^' && p
[1] == '^')) {
1039 /* Handling unterminated string by UNV */
1042 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1043 t->text[p-line] = *line;
1047 if (type
!= TOK_COMMENT
) {
1048 *tail
= t
= new_Token(NULL
, type
, line
, p
- line
);
1057 * this function allocates a new managed block of memory and
1058 * returns a pointer to the block. The managed blocks are
1059 * deleted only all at once by the delete_Blocks function.
1061 static void *new_Block(size_t size
)
1063 Blocks
*b
= &blocks
;
1065 /* first, get to the end of the linked list */
1068 /* now allocate the requested chunk */
1069 b
->chunk
= nasm_malloc(size
);
1071 /* now allocate a new block for the next request */
1072 b
->next
= nasm_malloc(sizeof(Blocks
));
1073 /* and initialize the contents of the new block */
1074 b
->next
->next
= NULL
;
1075 b
->next
->chunk
= NULL
;
1080 * this function deletes all managed blocks of memory
1082 static void delete_Blocks(void)
1084 Blocks
*a
, *b
= &blocks
;
1087 * keep in mind that the first block, pointed to by blocks
1088 * is a static and not dynamically allocated, so we don't
1093 nasm_free(b
->chunk
);
1102 * this function creates a new Token and passes a pointer to it
1103 * back to the caller. It sets the type and text elements, and
1104 * also the a.mac and next elements to NULL.
1106 static Token
*new_Token(Token
* next
, enum pp_token_type type
,
1107 const char *text
, int txtlen
)
1112 if (freeTokens
== NULL
) {
1113 freeTokens
= (Token
*) new_Block(TOKEN_BLOCKSIZE
* sizeof(Token
));
1114 for (i
= 0; i
< TOKEN_BLOCKSIZE
- 1; i
++)
1115 freeTokens
[i
].next
= &freeTokens
[i
+ 1];
1116 freeTokens
[i
].next
= NULL
;
1119 freeTokens
= t
->next
;
1123 if (type
== TOK_WHITESPACE
|| text
== NULL
) {
1127 txtlen
= strlen(text
);
1128 t
->text
= nasm_malloc(txtlen
+1);
1129 memcpy(t
->text
, text
, txtlen
);
1130 t
->text
[txtlen
] = '\0';
1135 static Token
*delete_Token(Token
* t
)
1137 Token
*next
= t
->next
;
1139 t
->next
= freeTokens
;
1145 * Convert a line of tokens back into text.
1146 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1147 * will be transformed into ..@ctxnum.xxx
1149 static char *detoken(Token
* tlist
, bool expand_locals
)
1157 for (t
= tlist
; t
; t
= t
->next
) {
1158 if (t
->type
== TOK_PREPROC_ID
&& t
->text
[1] == '!') {
1159 char *p
= getenv(t
->text
+ 2);
1162 t
->text
= nasm_strdup(p
);
1166 /* Expand local macros here and not during preprocessing */
1167 if (expand_locals
&&
1168 t
->type
== TOK_PREPROC_ID
&& t
->text
&&
1169 t
->text
[0] == '%' && t
->text
[1] == '$') {
1172 Context
*ctx
= get_ctx(t
->text
, &q
, false);
1175 snprintf(buffer
, sizeof(buffer
), "..@%"PRIu32
".", ctx
->number
);
1176 p
= nasm_strcat(buffer
, q
);
1181 if (t
->type
== TOK_WHITESPACE
) {
1183 } else if (t
->text
) {
1184 len
+= strlen(t
->text
);
1187 p
= line
= nasm_malloc(len
+ 1);
1188 for (t
= tlist
; t
; t
= t
->next
) {
1189 if (t
->type
== TOK_WHITESPACE
) {
1191 } else if (t
->text
) {
1202 * A scanner, suitable for use by the expression evaluator, which
1203 * operates on a line of Tokens. Expects a pointer to a pointer to
1204 * the first token in the line to be passed in as its private_data
1207 * FIX: This really needs to be unified with stdscan.
1209 static int ppscan(void *private_data
, struct tokenval
*tokval
)
1211 Token
**tlineptr
= private_data
;
1213 char ourcopy
[MAX_KEYWORD
+1], *p
, *r
, *s
;
1217 *tlineptr
= tline
? tline
->next
: NULL
;
1219 while (tline
&& (tline
->type
== TOK_WHITESPACE
||
1220 tline
->type
== TOK_COMMENT
));
1223 return tokval
->t_type
= TOKEN_EOS
;
1225 tokval
->t_charptr
= tline
->text
;
1227 if (tline
->text
[0] == '$' && !tline
->text
[1])
1228 return tokval
->t_type
= TOKEN_HERE
;
1229 if (tline
->text
[0] == '$' && tline
->text
[1] == '$' && !tline
->text
[2])
1230 return tokval
->t_type
= TOKEN_BASE
;
1232 if (tline
->type
== TOK_ID
) {
1233 p
= tokval
->t_charptr
= tline
->text
;
1235 tokval
->t_charptr
++;
1236 return tokval
->t_type
= TOKEN_ID
;
1239 for (r
= p
, s
= ourcopy
; *r
; r
++) {
1240 if (r
>= p
+MAX_KEYWORD
)
1241 return tokval
->t_type
= TOKEN_ID
; /* Not a keyword */
1242 *s
++ = nasm_tolower(*r
);
1245 /* right, so we have an identifier sitting in temp storage. now,
1246 * is it actually a register or instruction name, or what? */
1247 return nasm_token_hash(ourcopy
, tokval
);
1250 if (tline
->type
== TOK_NUMBER
) {
1252 tokval
->t_integer
= readnum(tline
->text
, &rn_error
);
1253 tokval
->t_charptr
= tline
->text
;
1255 return tokval
->t_type
= TOKEN_ERRNUM
;
1257 return tokval
->t_type
= TOKEN_NUM
;
1260 if (tline
->type
== TOK_FLOAT
) {
1261 return tokval
->t_type
= TOKEN_FLOAT
;
1264 if (tline
->type
== TOK_STRING
) {
1267 bq
= tline
->text
[0];
1268 tokval
->t_charptr
= tline
->text
;
1269 tokval
->t_inttwo
= nasm_unquote(tline
->text
, &ep
);
1271 if (ep
[0] != bq
|| ep
[1] != '\0')
1272 return tokval
->t_type
= TOKEN_ERRSTR
;
1274 return tokval
->t_type
= TOKEN_STR
;
1277 if (tline
->type
== TOK_OTHER
) {
1278 if (!strcmp(tline
->text
, "<<"))
1279 return tokval
->t_type
= TOKEN_SHL
;
1280 if (!strcmp(tline
->text
, ">>"))
1281 return tokval
->t_type
= TOKEN_SHR
;
1282 if (!strcmp(tline
->text
, "//"))
1283 return tokval
->t_type
= TOKEN_SDIV
;
1284 if (!strcmp(tline
->text
, "%%"))
1285 return tokval
->t_type
= TOKEN_SMOD
;
1286 if (!strcmp(tline
->text
, "=="))
1287 return tokval
->t_type
= TOKEN_EQ
;
1288 if (!strcmp(tline
->text
, "<>"))
1289 return tokval
->t_type
= TOKEN_NE
;
1290 if (!strcmp(tline
->text
, "!="))
1291 return tokval
->t_type
= TOKEN_NE
;
1292 if (!strcmp(tline
->text
, "<="))
1293 return tokval
->t_type
= TOKEN_LE
;
1294 if (!strcmp(tline
->text
, ">="))
1295 return tokval
->t_type
= TOKEN_GE
;
1296 if (!strcmp(tline
->text
, "&&"))
1297 return tokval
->t_type
= TOKEN_DBL_AND
;
1298 if (!strcmp(tline
->text
, "^^"))
1299 return tokval
->t_type
= TOKEN_DBL_XOR
;
1300 if (!strcmp(tline
->text
, "||"))
1301 return tokval
->t_type
= TOKEN_DBL_OR
;
1305 * We have no other options: just return the first character of
1308 return tokval
->t_type
= tline
->text
[0];
1312 * Compare a string to the name of an existing macro; this is a
1313 * simple wrapper which calls either strcmp or nasm_stricmp
1314 * depending on the value of the `casesense' parameter.
1316 static int mstrcmp(const char *p
, const char *q
, bool casesense
)
1318 return casesense
? strcmp(p
, q
) : nasm_stricmp(p
, q
);
1322 * Compare a string to the name of an existing macro; this is a
1323 * simple wrapper which calls either strcmp or nasm_stricmp
1324 * depending on the value of the `casesense' parameter.
1326 static int mmemcmp(const char *p
, const char *q
, size_t l
, bool casesense
)
1328 return casesense
? memcmp(p
, q
, l
) : nasm_memicmp(p
, q
, l
);
1332 * Return the Context structure associated with a %$ token. Return
1333 * NULL, having _already_ reported an error condition, if the
1334 * context stack isn't deep enough for the supplied number of $
1336 * If all_contexts == true, contexts that enclose current are
1337 * also scanned for such smacro, until it is found; if not -
1338 * only the context that directly results from the number of $'s
1339 * in variable's name.
1341 * If "namep" is non-NULL, set it to the pointer to the macro name
1342 * tail, i.e. the part beyond %$...
1344 static Context
*get_ctx(const char *name
, const char **namep
,
1354 if (!name
|| name
[0] != '%' || name
[1] != '$')
1358 error(ERR_NONFATAL
, "`%s': context stack is empty", name
);
1365 while (ctx
&& *name
== '$') {
1371 error(ERR_NONFATAL
, "`%s': context stack is only"
1372 " %d level%s deep", name
, i
, (i
== 1 ? "" : "s"));
1383 /* Search for this smacro in found context */
1384 m
= hash_findix(&ctx
->localmac
, name
);
1386 if (!mstrcmp(m
->name
, name
, m
->casesense
))
1397 * Check to see if a file is already in a string list
1399 static bool in_list(const StrList
*list
, const char *str
)
1402 if (!strcmp(list
->str
, str
))
1410 * Open an include file. This routine must always return a valid
1411 * file pointer if it returns - it's responsible for throwing an
1412 * ERR_FATAL and bombing out completely if not. It should also try
1413 * the include path one by one until it finds the file or reaches
1414 * the end of the path.
1416 static FILE *inc_fopen(const char *file
, StrList
**dhead
, StrList
***dtail
,
1421 IncPath
*ip
= ipath
;
1422 int len
= strlen(file
);
1423 size_t prefix_len
= 0;
1427 sl
= nasm_malloc(prefix_len
+len
+1+sizeof sl
->next
);
1428 memcpy(sl
->str
, prefix
, prefix_len
);
1429 memcpy(sl
->str
+prefix_len
, file
, len
+1);
1430 fp
= fopen(sl
->str
, "r");
1431 if (fp
&& dhead
&& !in_list(*dhead
, sl
->str
)) {
1449 prefix_len
= strlen(prefix
);
1451 /* -MG given and file not found */
1452 if (dhead
&& !in_list(*dhead
, file
)) {
1453 sl
= nasm_malloc(len
+1+sizeof sl
->next
);
1455 strcpy(sl
->str
, file
);
1463 error(ERR_FATAL
, "unable to open include file `%s'", file
);
1464 return NULL
; /* never reached - placate compilers */
1468 * Determine if we should warn on defining a single-line macro of
1469 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1470 * return true if _any_ single-line macro of that name is defined.
1471 * Otherwise, will return true if a single-line macro with either
1472 * `nparam' or no parameters is defined.
1474 * If a macro with precisely the right number of parameters is
1475 * defined, or nparam is -1, the address of the definition structure
1476 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1477 * is NULL, no action will be taken regarding its contents, and no
1480 * Note that this is also called with nparam zero to resolve
1483 * If you already know which context macro belongs to, you can pass
1484 * the context pointer as first parameter; if you won't but name begins
1485 * with %$ the context will be automatically computed. If all_contexts
1486 * is true, macro will be searched in outer contexts as well.
1489 smacro_defined(Context
* ctx
, const char *name
, int nparam
, SMacro
** defn
,
1492 struct hash_table
*smtbl
;
1496 smtbl
= &ctx
->localmac
;
1497 } else if (name
[0] == '%' && name
[1] == '$') {
1499 ctx
= get_ctx(name
, &name
, false);
1501 return false; /* got to return _something_ */
1502 smtbl
= &ctx
->localmac
;
1506 m
= (SMacro
*) hash_findix(smtbl
, name
);
1509 if (!mstrcmp(m
->name
, name
, m
->casesense
&& nocase
) &&
1510 (nparam
<= 0 || m
->nparam
== 0 || nparam
== (int) m
->nparam
)) {
1512 if (nparam
== (int) m
->nparam
|| nparam
== -1)
1526 * Count and mark off the parameters in a multi-line macro call.
1527 * This is called both from within the multi-line macro expansion
1528 * code, and also to mark off the default parameters when provided
1529 * in a %macro definition line.
1531 static void count_mmac_params(Token
* t
, int *nparam
, Token
*** params
)
1533 int paramsize
, brace
;
1535 *nparam
= paramsize
= 0;
1538 /* +1: we need space for the final NULL */
1539 if (*nparam
+1 >= paramsize
) {
1540 paramsize
+= PARAM_DELTA
;
1541 *params
= nasm_realloc(*params
, sizeof(**params
) * paramsize
);
1545 if (tok_is_(t
, "{"))
1547 (*params
)[(*nparam
)++] = t
;
1548 while (tok_isnt_(t
, brace
? "}" : ","))
1550 if (t
) { /* got a comma/brace */
1554 * Now we've found the closing brace, look further
1558 if (tok_isnt_(t
, ",")) {
1560 "braces do not enclose all of macro parameter");
1561 while (tok_isnt_(t
, ","))
1565 t
= t
->next
; /* eat the comma */
1572 * Determine whether one of the various `if' conditions is true or
1575 * We must free the tline we get passed.
1577 static bool if_condition(Token
* tline
, enum preproc_token ct
)
1579 enum pp_conditional i
= PP_COND(ct
);
1581 Token
*t
, *tt
, **tptr
, *origline
;
1582 struct tokenval tokval
;
1584 enum pp_token_type needtype
;
1590 j
= false; /* have we matched yet? */
1595 if (tline
->type
!= TOK_ID
) {
1597 "`%s' expects context identifiers", pp_directives
[ct
]);
1598 free_tlist(origline
);
1601 if (cstk
&& cstk
->name
&& !nasm_stricmp(tline
->text
, cstk
->name
))
1603 tline
= tline
->next
;
1608 j
= false; /* have we matched yet? */
1611 if (!tline
|| (tline
->type
!= TOK_ID
&&
1612 (tline
->type
!= TOK_PREPROC_ID
||
1613 tline
->text
[1] != '$'))) {
1615 "`%s' expects macro identifiers", pp_directives
[ct
]);
1618 if (smacro_defined(NULL
, tline
->text
, 0, NULL
, true))
1620 tline
= tline
->next
;
1626 tline
= expand_smacro(tline
);
1628 while (tok_isnt_(tt
, ","))
1632 "`%s' expects two comma-separated arguments",
1637 j
= true; /* assume equality unless proved not */
1638 while ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) && tt
) {
1639 if (tt
->type
== TOK_OTHER
&& !strcmp(tt
->text
, ",")) {
1640 error(ERR_NONFATAL
, "`%s': more than one comma on line",
1644 if (t
->type
== TOK_WHITESPACE
) {
1648 if (tt
->type
== TOK_WHITESPACE
) {
1652 if (tt
->type
!= t
->type
) {
1653 j
= false; /* found mismatching tokens */
1656 /* When comparing strings, need to unquote them first */
1657 if (t
->type
== TOK_STRING
) {
1658 size_t l1
= nasm_unquote(t
->text
, NULL
);
1659 size_t l2
= nasm_unquote(tt
->text
, NULL
);
1665 if (mmemcmp(t
->text
, tt
->text
, l1
, i
== PPC_IFIDN
)) {
1669 } else if (mstrcmp(tt
->text
, t
->text
, i
== PPC_IFIDN
) != 0) {
1670 j
= false; /* found mismatching tokens */
1677 if ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) || tt
)
1678 j
= false; /* trailing gunk on one end or other */
1684 MMacro searching
, *mmac
;
1687 tline
= expand_id(tline
);
1688 if (!tok_type_(tline
, TOK_ID
)) {
1690 "`%s' expects a macro name", pp_directives
[ct
]);
1693 searching
.name
= nasm_strdup(tline
->text
);
1694 searching
.casesense
= true;
1695 searching
.plus
= false;
1696 searching
.nolist
= false;
1697 searching
.in_progress
= 0;
1698 searching
.max_depth
= 0;
1699 searching
.rep_nest
= NULL
;
1700 searching
.nparam_min
= 0;
1701 searching
.nparam_max
= INT_MAX
;
1702 tline
= expand_smacro(tline
->next
);
1705 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
1707 "`%s' expects a parameter count or nothing",
1710 searching
.nparam_min
= searching
.nparam_max
=
1711 readnum(tline
->text
, &j
);
1714 "unable to parse parameter count `%s'",
1717 if (tline
&& tok_is_(tline
->next
, "-")) {
1718 tline
= tline
->next
->next
;
1719 if (tok_is_(tline
, "*"))
1720 searching
.nparam_max
= INT_MAX
;
1721 else if (!tok_type_(tline
, TOK_NUMBER
))
1723 "`%s' expects a parameter count after `-'",
1726 searching
.nparam_max
= readnum(tline
->text
, &j
);
1729 "unable to parse parameter count `%s'",
1731 if (searching
.nparam_min
> searching
.nparam_max
)
1733 "minimum parameter count exceeds maximum");
1736 if (tline
&& tok_is_(tline
->next
, "+")) {
1737 tline
= tline
->next
;
1738 searching
.plus
= true;
1740 mmac
= (MMacro
*) hash_findix(&mmacros
, searching
.name
);
1742 if (!strcmp(mmac
->name
, searching
.name
) &&
1743 (mmac
->nparam_min
<= searching
.nparam_max
1745 && (searching
.nparam_min
<= mmac
->nparam_max
1752 if(tline
&& tline
->next
)
1753 error(ERR_WARNING
|ERR_PASS1
,
1754 "trailing garbage after %%ifmacro ignored");
1755 nasm_free(searching
.name
);
1764 needtype
= TOK_NUMBER
;
1767 needtype
= TOK_STRING
;
1771 t
= tline
= expand_smacro(tline
);
1773 while (tok_type_(t
, TOK_WHITESPACE
) ||
1774 (needtype
== TOK_NUMBER
&&
1775 tok_type_(t
, TOK_OTHER
) &&
1776 (t
->text
[0] == '-' || t
->text
[0] == '+') &&
1780 j
= tok_type_(t
, needtype
);
1784 t
= tline
= expand_smacro(tline
);
1785 while (tok_type_(t
, TOK_WHITESPACE
))
1790 t
= t
->next
; /* Skip the actual token */
1791 while (tok_type_(t
, TOK_WHITESPACE
))
1793 j
= !t
; /* Should be nothing left */
1798 t
= tline
= expand_smacro(tline
);
1799 while (tok_type_(t
, TOK_WHITESPACE
))
1802 j
= !t
; /* Should be empty */
1806 t
= tline
= expand_smacro(tline
);
1808 tokval
.t_type
= TOKEN_INVALID
;
1809 evalresult
= evaluate(ppscan
, tptr
, &tokval
,
1810 NULL
, pass
| CRITICAL
, error
, NULL
);
1814 error(ERR_WARNING
|ERR_PASS1
,
1815 "trailing garbage after expression ignored");
1816 if (!is_simple(evalresult
)) {
1818 "non-constant value given to `%s'", pp_directives
[ct
]);
1821 j
= reloc_value(evalresult
) != 0;
1826 "preprocessor directive `%s' not yet implemented",
1831 free_tlist(origline
);
1832 return j
^ PP_NEGATIVE(ct
);
1835 free_tlist(origline
);
1840 * Common code for defining an smacro
1842 static bool define_smacro(Context
*ctx
, const char *mname
, bool casesense
,
1843 int nparam
, Token
*expansion
)
1845 SMacro
*smac
, **smhead
;
1846 struct hash_table
*smtbl
;
1848 if (smacro_defined(ctx
, mname
, nparam
, &smac
, casesense
)) {
1850 error(ERR_WARNING
|ERR_PASS1
,
1851 "single-line macro `%s' defined both with and"
1852 " without parameters", mname
);
1854 /* Some instances of the old code considered this a failure,
1855 some others didn't. What is the right thing to do here? */
1856 free_tlist(expansion
);
1857 return false; /* Failure */
1860 * We're redefining, so we have to take over an
1861 * existing SMacro structure. This means freeing
1862 * what was already in it.
1864 nasm_free(smac
->name
);
1865 free_tlist(smac
->expansion
);
1868 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
1869 smhead
= (SMacro
**) hash_findi_add(smtbl
, mname
);
1870 smac
= nasm_malloc(sizeof(SMacro
));
1871 smac
->next
= *smhead
;
1874 smac
->name
= nasm_strdup(mname
);
1875 smac
->casesense
= casesense
;
1876 smac
->nparam
= nparam
;
1877 smac
->expansion
= expansion
;
1878 smac
->in_progress
= false;
1879 return true; /* Success */
1883 * Undefine an smacro
1885 static void undef_smacro(Context
*ctx
, const char *mname
)
1887 SMacro
**smhead
, *s
, **sp
;
1888 struct hash_table
*smtbl
;
1890 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
1891 smhead
= (SMacro
**)hash_findi(smtbl
, mname
, NULL
);
1895 * We now have a macro name... go hunt for it.
1898 while ((s
= *sp
) != NULL
) {
1899 if (!mstrcmp(s
->name
, mname
, s
->casesense
)) {
1902 free_tlist(s
->expansion
);
1912 * Parse a mmacro specification.
1914 static bool parse_mmacro_spec(Token
*tline
, MMacro
*def
, const char *directive
)
1918 tline
= tline
->next
;
1920 tline
= expand_id(tline
);
1921 if (!tok_type_(tline
, TOK_ID
)) {
1922 error(ERR_NONFATAL
, "`%s' expects a macro name", directive
);
1927 def
->name
= nasm_strdup(tline
->text
);
1929 def
->nolist
= false;
1930 def
->in_progress
= 0;
1932 def
->rep_nest
= NULL
;
1933 def
->nparam_min
= 0;
1934 def
->nparam_max
= 0;
1936 tline
= expand_smacro(tline
->next
);
1938 if (!tok_type_(tline
, TOK_NUMBER
)) {
1939 error(ERR_NONFATAL
, "`%s' expects a parameter count", directive
);
1941 def
->nparam_min
= def
->nparam_max
=
1942 readnum(tline
->text
, &err
);
1945 "unable to parse parameter count `%s'", tline
->text
);
1947 if (tline
&& tok_is_(tline
->next
, "-")) {
1948 tline
= tline
->next
->next
;
1949 if (tok_is_(tline
, "*")) {
1950 def
->nparam_max
= INT_MAX
;
1951 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
1953 "`%s' expects a parameter count after `-'", directive
);
1955 def
->nparam_max
= readnum(tline
->text
, &err
);
1957 error(ERR_NONFATAL
, "unable to parse parameter count `%s'",
1960 if (def
->nparam_min
> def
->nparam_max
) {
1961 error(ERR_NONFATAL
, "minimum parameter count exceeds maximum");
1965 if (tline
&& tok_is_(tline
->next
, "+")) {
1966 tline
= tline
->next
;
1969 if (tline
&& tok_type_(tline
->next
, TOK_ID
) &&
1970 !nasm_stricmp(tline
->next
->text
, ".nolist")) {
1971 tline
= tline
->next
;
1976 * Handle maximum recursion depth.
1978 if (tline
&& tok_is_(tline
->next
, ",")) {
1979 tline
= tline
->next
->next
;
1980 if (tok_is_(tline
, "*")) {
1981 def
->max_depth
= 65536; /* should we eliminate this? */
1982 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
1984 "`%s' expects a maximum recursion depth after `,'", directive
);
1986 def
->max_depth
= readnum(tline
->text
, &err
);
1988 error(ERR_NONFATAL
, "unable to parse maximum recursion depth `%s'",
1995 * Handle default parameters.
1997 if (tline
&& tline
->next
) {
1998 def
->dlist
= tline
->next
;
2000 count_mmac_params(def
->dlist
, &def
->ndefs
, &def
->defaults
);
2003 def
->defaults
= NULL
;
2005 def
->expansion
= NULL
;
2008 def
->ndefs
> def
->nparam_max
- def
->nparam_min
&&
2010 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MDP
,
2011 "too many default macro parameters");
2018 * Decode a size directive
2020 static int parse_size(const char *str
) {
2021 static const char *size_names
[] =
2022 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
2023 static const int sizes
[] =
2024 { 0, 1, 4, 16, 8, 10, 2, 32 };
2026 return sizes
[bsii(str
, size_names
, elements(size_names
))+1];
2030 * find and process preprocessor directive in passed line
2031 * Find out if a line contains a preprocessor directive, and deal
2034 * If a directive _is_ found, it is the responsibility of this routine
2035 * (and not the caller) to free_tlist() the line.
2037 * @param tline a pointer to the current tokeninzed line linked list
2038 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
2041 static int do_directive(Token
* tline
)
2043 enum preproc_token i
;
2056 MMacro
*mmac
, **mmhead
;
2057 Token
*t
, *tt
, *param_start
, *macro_start
, *last
, **tptr
, *origline
;
2059 struct tokenval tokval
;
2061 MMacro
*tmp_defining
; /* Used when manipulating rep_nest */
2069 if (!tline
|| !tok_type_(tline
, TOK_PREPROC_ID
) ||
2070 (tline
->text
[1] == '%' || tline
->text
[1] == '$'
2071 || tline
->text
[1] == '!'))
2072 return NO_DIRECTIVE_FOUND
;
2074 i
= pp_token_hash(tline
->text
);
2077 * If we're in a non-emitting branch of a condition construct,
2078 * or walking to the end of an already terminated %rep block,
2079 * we should ignore all directives except for condition
2082 if (((istk
->conds
&& !emitting(istk
->conds
->state
)) ||
2083 (istk
->mstk
&& !istk
->mstk
->in_progress
)) && !is_condition(i
)) {
2084 return NO_DIRECTIVE_FOUND
;
2088 * If we're defining a macro or reading a %rep block, we should
2089 * ignore all directives except for %macro/%imacro (which nest),
2090 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2091 * If we're in a %rep block, another %rep nests, so should be let through.
2093 if (defining
&& i
!= PP_MACRO
&& i
!= PP_IMACRO
&&
2094 i
!= PP_ENDMACRO
&& i
!= PP_ENDM
&&
2095 (defining
->name
|| (i
!= PP_ENDREP
&& i
!= PP_REP
))) {
2096 return NO_DIRECTIVE_FOUND
;
2100 if (i
== PP_MACRO
|| i
== PP_IMACRO
) {
2102 return NO_DIRECTIVE_FOUND
;
2103 } else if (nested_mac_count
> 0) {
2104 if (i
== PP_ENDMACRO
) {
2106 return NO_DIRECTIVE_FOUND
;
2109 if (!defining
->name
) {
2112 return NO_DIRECTIVE_FOUND
;
2113 } else if (nested_rep_count
> 0) {
2114 if (i
== PP_ENDREP
) {
2116 return NO_DIRECTIVE_FOUND
;
2124 error(ERR_NONFATAL
, "unknown preprocessor directive `%s'",
2126 return NO_DIRECTIVE_FOUND
; /* didn't get it */
2129 /* Directive to tell NASM what the default stack size is. The
2130 * default is for a 16-bit stack, and this can be overriden with
2132 * the following form:
2134 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2136 tline
= tline
->next
;
2137 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2138 tline
= tline
->next
;
2139 if (!tline
|| tline
->type
!= TOK_ID
) {
2140 error(ERR_NONFATAL
, "`%%stacksize' missing size parameter");
2141 free_tlist(origline
);
2142 return DIRECTIVE_FOUND
;
2144 if (nasm_stricmp(tline
->text
, "flat") == 0) {
2145 /* All subsequent ARG directives are for a 32-bit stack */
2147 StackPointer
= "ebp";
2150 } else if (nasm_stricmp(tline
->text
, "flat64") == 0) {
2151 /* All subsequent ARG directives are for a 64-bit stack */
2153 StackPointer
= "rbp";
2156 } else if (nasm_stricmp(tline
->text
, "large") == 0) {
2157 /* All subsequent ARG directives are for a 16-bit stack,
2158 * far function call.
2161 StackPointer
= "bp";
2164 } else if (nasm_stricmp(tline
->text
, "small") == 0) {
2165 /* All subsequent ARG directives are for a 16-bit stack,
2166 * far function call. We don't support near functions.
2169 StackPointer
= "bp";
2173 error(ERR_NONFATAL
, "`%%stacksize' invalid size type");
2174 free_tlist(origline
);
2175 return DIRECTIVE_FOUND
;
2177 free_tlist(origline
);
2178 return DIRECTIVE_FOUND
;
2181 /* TASM like ARG directive to define arguments to functions, in
2182 * the following form:
2184 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2188 char *arg
, directive
[256];
2189 int size
= StackSize
;
2191 /* Find the argument name */
2192 tline
= tline
->next
;
2193 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2194 tline
= tline
->next
;
2195 if (!tline
|| tline
->type
!= TOK_ID
) {
2196 error(ERR_NONFATAL
, "`%%arg' missing argument parameter");
2197 free_tlist(origline
);
2198 return DIRECTIVE_FOUND
;
2202 /* Find the argument size type */
2203 tline
= tline
->next
;
2204 if (!tline
|| tline
->type
!= TOK_OTHER
2205 || tline
->text
[0] != ':') {
2207 "Syntax error processing `%%arg' directive");
2208 free_tlist(origline
);
2209 return DIRECTIVE_FOUND
;
2211 tline
= tline
->next
;
2212 if (!tline
|| tline
->type
!= TOK_ID
) {
2213 error(ERR_NONFATAL
, "`%%arg' missing size type parameter");
2214 free_tlist(origline
);
2215 return DIRECTIVE_FOUND
;
2218 /* Allow macro expansion of type parameter */
2219 tt
= tokenize(tline
->text
);
2220 tt
= expand_smacro(tt
);
2221 size
= parse_size(tt
->text
);
2224 "Invalid size type for `%%arg' missing directive");
2226 free_tlist(origline
);
2227 return DIRECTIVE_FOUND
;
2231 /* Round up to even stack slots */
2232 size
= (size
+StackSize
-1) & ~(StackSize
-1);
2234 /* Now define the macro for the argument */
2235 snprintf(directive
, sizeof(directive
), "%%define %s (%s+%d)",
2236 arg
, StackPointer
, offset
);
2237 do_directive(tokenize(directive
));
2240 /* Move to the next argument in the list */
2241 tline
= tline
->next
;
2242 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2243 tline
= tline
->next
;
2244 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2246 free_tlist(origline
);
2247 return DIRECTIVE_FOUND
;
2250 /* TASM like LOCAL directive to define local variables for a
2251 * function, in the following form:
2253 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2255 * The '= LocalSize' at the end is ignored by NASM, but is
2256 * required by TASM to define the local parameter size (and used
2257 * by the TASM macro package).
2259 offset
= LocalOffset
;
2261 char *local
, directive
[256];
2262 int size
= StackSize
;
2264 /* Find the argument name */
2265 tline
= tline
->next
;
2266 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2267 tline
= tline
->next
;
2268 if (!tline
|| tline
->type
!= TOK_ID
) {
2270 "`%%local' missing argument parameter");
2271 free_tlist(origline
);
2272 return DIRECTIVE_FOUND
;
2274 local
= tline
->text
;
2276 /* Find the argument size type */
2277 tline
= tline
->next
;
2278 if (!tline
|| tline
->type
!= TOK_OTHER
2279 || tline
->text
[0] != ':') {
2281 "Syntax error processing `%%local' directive");
2282 free_tlist(origline
);
2283 return DIRECTIVE_FOUND
;
2285 tline
= tline
->next
;
2286 if (!tline
|| tline
->type
!= TOK_ID
) {
2288 "`%%local' missing size type parameter");
2289 free_tlist(origline
);
2290 return DIRECTIVE_FOUND
;
2293 /* Allow macro expansion of type parameter */
2294 tt
= tokenize(tline
->text
);
2295 tt
= expand_smacro(tt
);
2296 size
= parse_size(tt
->text
);
2299 "Invalid size type for `%%local' missing directive");
2301 free_tlist(origline
);
2302 return DIRECTIVE_FOUND
;
2306 /* Round up to even stack slots */
2307 size
= (size
+StackSize
-1) & ~(StackSize
-1);
2309 offset
+= size
; /* Negative offset, increment before */
2311 /* Now define the macro for the argument */
2312 snprintf(directive
, sizeof(directive
), "%%define %s (%s-%d)",
2313 local
, StackPointer
, offset
);
2314 do_directive(tokenize(directive
));
2316 /* Now define the assign to setup the enter_c macro correctly */
2317 snprintf(directive
, sizeof(directive
),
2318 "%%assign %%$localsize %%$localsize+%d", size
);
2319 do_directive(tokenize(directive
));
2321 /* Move to the next argument in the list */
2322 tline
= tline
->next
;
2323 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2324 tline
= tline
->next
;
2325 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2326 LocalOffset
= offset
;
2327 free_tlist(origline
);
2328 return DIRECTIVE_FOUND
;
2332 error(ERR_WARNING
|ERR_PASS1
,
2333 "trailing garbage after `%%clear' ignored");
2336 free_tlist(origline
);
2337 return DIRECTIVE_FOUND
;
2340 t
= tline
->next
= expand_smacro(tline
->next
);
2342 if (!t
|| (t
->type
!= TOK_STRING
&&
2343 t
->type
!= TOK_INTERNAL_STRING
)) {
2344 error(ERR_NONFATAL
, "`%%depend' expects a file name");
2345 free_tlist(origline
);
2346 return DIRECTIVE_FOUND
; /* but we did _something_ */
2349 error(ERR_WARNING
|ERR_PASS1
,
2350 "trailing garbage after `%%depend' ignored");
2352 if (t
->type
!= TOK_INTERNAL_STRING
)
2353 nasm_unquote(p
, NULL
);
2354 if (dephead
&& !in_list(*dephead
, p
)) {
2355 StrList
*sl
= nasm_malloc(strlen(p
)+1+sizeof sl
->next
);
2359 deptail
= &sl
->next
;
2361 free_tlist(origline
);
2362 return DIRECTIVE_FOUND
;
2365 t
= tline
->next
= expand_smacro(tline
->next
);
2368 if (!t
|| (t
->type
!= TOK_STRING
&&
2369 t
->type
!= TOK_INTERNAL_STRING
)) {
2370 error(ERR_NONFATAL
, "`%%include' expects a file name");
2371 free_tlist(origline
);
2372 return DIRECTIVE_FOUND
; /* but we did _something_ */
2375 error(ERR_WARNING
|ERR_PASS1
,
2376 "trailing garbage after `%%include' ignored");
2378 if (t
->type
!= TOK_INTERNAL_STRING
)
2379 nasm_unquote(p
, NULL
);
2380 inc
= nasm_malloc(sizeof(Include
));
2383 inc
->fp
= inc_fopen(p
, dephead
, &deptail
, pass
== 0);
2385 /* -MG given but file not found */
2388 inc
->fname
= src_set_fname(nasm_strdup(p
));
2389 inc
->lineno
= src_set_linnum(0);
2391 inc
->expansion
= NULL
;
2394 list
->uplevel(LIST_INCLUDE
);
2396 free_tlist(origline
);
2397 return DIRECTIVE_FOUND
;
2401 static macros_t
*use_pkg
;
2402 const char *pkg_macro
;
2404 tline
= tline
->next
;
2406 tline
= expand_id(tline
);
2408 if (!tline
|| (tline
->type
!= TOK_STRING
&&
2409 tline
->type
!= TOK_INTERNAL_STRING
&&
2410 tline
->type
!= TOK_ID
)) {
2411 error(ERR_NONFATAL
, "`%%use' expects a package name");
2412 free_tlist(origline
);
2413 return DIRECTIVE_FOUND
; /* but we did _something_ */
2416 error(ERR_WARNING
|ERR_PASS1
,
2417 "trailing garbage after `%%use' ignored");
2418 if (tline
->type
== TOK_STRING
)
2419 nasm_unquote(tline
->text
, NULL
);
2420 use_pkg
= nasm_stdmac_find_package(tline
->text
);
2422 error(ERR_NONFATAL
, "unknown `%%use' package: %s", tline
->text
);
2423 /* The first string will be <%define>__USE_*__ */
2424 pkg_macro
= (char *)use_pkg
+ 1;
2425 if (!smacro_defined(NULL
, pkg_macro
, 0, NULL
, true)) {
2426 /* Not already included, go ahead and include it */
2427 stdmacpos
= use_pkg
;
2429 free_tlist(origline
);
2430 return DIRECTIVE_FOUND
;
2435 tline
= tline
->next
;
2437 tline
= expand_id(tline
);
2439 if (!tok_type_(tline
, TOK_ID
)) {
2440 error(ERR_NONFATAL
, "`%s' expects a context identifier",
2442 free_tlist(origline
);
2443 return DIRECTIVE_FOUND
; /* but we did _something_ */
2446 error(ERR_WARNING
|ERR_PASS1
,
2447 "trailing garbage after `%s' ignored",
2449 p
= nasm_strdup(tline
->text
);
2451 p
= NULL
; /* Anonymous */
2455 ctx
= nasm_malloc(sizeof(Context
));
2457 hash_init(&ctx
->localmac
, HASH_SMALL
);
2459 ctx
->number
= unique
++;
2464 error(ERR_NONFATAL
, "`%s': context stack is empty",
2466 } else if (i
== PP_POP
) {
2467 if (p
&& (!cstk
->name
|| nasm_stricmp(p
, cstk
->name
)))
2468 error(ERR_NONFATAL
, "`%%pop' in wrong context: %s, "
2470 cstk
->name
? cstk
->name
: "anonymous", p
);
2475 nasm_free(cstk
->name
);
2481 free_tlist(origline
);
2482 return DIRECTIVE_FOUND
;
2484 severity
= ERR_FATAL
;
2487 severity
= ERR_NONFATAL
;
2490 severity
= ERR_WARNING
|ERR_WARN_USER
;
2495 /* Only error out if this is the final pass */
2496 if (pass
!= 2 && i
!= PP_FATAL
)
2497 return DIRECTIVE_FOUND
;
2499 tline
->next
= expand_smacro(tline
->next
);
2500 tline
= tline
->next
;
2502 t
= tline
? tline
->next
: NULL
;
2504 if (tok_type_(tline
, TOK_STRING
) && !t
) {
2505 /* The line contains only a quoted string */
2507 nasm_unquote(p
, NULL
);
2508 error(severity
, "%s", p
);
2510 /* Not a quoted string, or more than a quoted string */
2511 p
= detoken(tline
, false);
2512 error(severity
, "%s", p
);
2515 free_tlist(origline
);
2516 return DIRECTIVE_FOUND
;
2520 if (istk
->conds
&& !emitting(istk
->conds
->state
))
2523 j
= if_condition(tline
->next
, i
);
2524 tline
->next
= NULL
; /* it got freed */
2525 j
= j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2527 cond
= nasm_malloc(sizeof(Cond
));
2528 cond
->next
= istk
->conds
;
2531 free_tlist(origline
);
2532 return DIRECTIVE_FOUND
;
2536 error(ERR_FATAL
, "`%s': no matching `%%if'", pp_directives
[i
]);
2537 switch(istk
->conds
->state
) {
2539 istk
->conds
->state
= COND_DONE
;
2546 case COND_ELSE_TRUE
:
2547 case COND_ELSE_FALSE
:
2548 error_precond(ERR_WARNING
|ERR_PASS1
,
2549 "`%%elif' after `%%else' ignored");
2550 istk
->conds
->state
= COND_NEVER
;
2555 * IMPORTANT: In the case of %if, we will already have
2556 * called expand_mmac_params(); however, if we're
2557 * processing an %elif we must have been in a
2558 * non-emitting mode, which would have inhibited
2559 * the normal invocation of expand_mmac_params().
2560 * Therefore, we have to do it explicitly here.
2562 j
= if_condition(expand_mmac_params(tline
->next
), i
);
2563 tline
->next
= NULL
; /* it got freed */
2564 istk
->conds
->state
=
2565 j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2568 free_tlist(origline
);
2569 return DIRECTIVE_FOUND
;
2573 error_precond(ERR_WARNING
|ERR_PASS1
,
2574 "trailing garbage after `%%else' ignored");
2576 error(ERR_FATAL
, "`%%else': no matching `%%if'");
2577 switch(istk
->conds
->state
) {
2580 istk
->conds
->state
= COND_ELSE_FALSE
;
2587 istk
->conds
->state
= COND_ELSE_TRUE
;
2590 case COND_ELSE_TRUE
:
2591 case COND_ELSE_FALSE
:
2592 error_precond(ERR_WARNING
|ERR_PASS1
,
2593 "`%%else' after `%%else' ignored.");
2594 istk
->conds
->state
= COND_NEVER
;
2597 free_tlist(origline
);
2598 return DIRECTIVE_FOUND
;
2602 error_precond(ERR_WARNING
|ERR_PASS1
,
2603 "trailing garbage after `%%endif' ignored");
2605 error(ERR_FATAL
, "`%%endif': no matching `%%if'");
2607 istk
->conds
= cond
->next
;
2609 free_tlist(origline
);
2610 return DIRECTIVE_FOUND
;
2616 "`%%%smacro': already defining a macro",
2617 (i
== PP_IMACRO
? "i" : ""));
2618 return DIRECTIVE_FOUND
;
2620 defining
= nasm_malloc(sizeof(MMacro
));
2621 defining
->casesense
= (i
== PP_MACRO
);
2622 if (!parse_mmacro_spec(tline
, defining
, pp_directives
[i
])) {
2623 nasm_free(defining
);
2625 return DIRECTIVE_FOUND
;
2628 mmac
= (MMacro
*) hash_findix(&mmacros
, defining
->name
);
2630 if (!strcmp(mmac
->name
, defining
->name
) &&
2631 (mmac
->nparam_min
<= defining
->nparam_max
2633 && (defining
->nparam_min
<= mmac
->nparam_max
2635 error(ERR_WARNING
|ERR_PASS1
,
2636 "redefining multi-line macro `%s'", defining
->name
);
2637 return DIRECTIVE_FOUND
;
2641 free_tlist(origline
);
2642 return DIRECTIVE_FOUND
;
2646 if (! (defining
&& defining
->name
)) {
2647 error(ERR_NONFATAL
, "`%s': not defining a macro", tline
->text
);
2648 return DIRECTIVE_FOUND
;
2650 mmhead
= (MMacro
**) hash_findi_add(&mmacros
, defining
->name
);
2651 defining
->next
= *mmhead
;
2654 free_tlist(origline
);
2655 return DIRECTIVE_FOUND
;
2663 spec
.casesense
= (i
== PP_UNMACRO
);
2664 if (!parse_mmacro_spec(tline
, &spec
, pp_directives
[i
])) {
2665 return DIRECTIVE_FOUND
;
2667 mmac_p
= (MMacro
**) hash_findi(&mmacros
, spec
.name
, NULL
);
2668 while (mmac_p
&& *mmac_p
) {
2670 if (mmac
->casesense
== spec
.casesense
&&
2671 !mstrcmp(mmac
->name
, spec
.name
, spec
.casesense
) &&
2672 mmac
->nparam_min
== spec
.nparam_min
&&
2673 mmac
->nparam_max
== spec
.nparam_max
&&
2674 mmac
->plus
== spec
.plus
) {
2675 *mmac_p
= mmac
->next
;
2678 mmac_p
= &mmac
->next
;
2681 free_tlist(origline
);
2682 free_tlist(spec
.dlist
);
2683 return DIRECTIVE_FOUND
;
2687 if (tline
->next
&& tline
->next
->type
== TOK_WHITESPACE
)
2688 tline
= tline
->next
;
2689 if (tline
->next
== NULL
) {
2690 free_tlist(origline
);
2691 error(ERR_NONFATAL
, "`%%rotate' missing rotate count");
2692 return DIRECTIVE_FOUND
;
2694 t
= expand_smacro(tline
->next
);
2696 free_tlist(origline
);
2699 tokval
.t_type
= TOKEN_INVALID
;
2701 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2704 return DIRECTIVE_FOUND
;
2706 error(ERR_WARNING
|ERR_PASS1
,
2707 "trailing garbage after expression ignored");
2708 if (!is_simple(evalresult
)) {
2709 error(ERR_NONFATAL
, "non-constant value given to `%%rotate'");
2710 return DIRECTIVE_FOUND
;
2713 while (mmac
&& !mmac
->name
) /* avoid mistaking %reps for macros */
2714 mmac
= mmac
->next_active
;
2716 error(ERR_NONFATAL
, "`%%rotate' invoked outside a macro call");
2717 } else if (mmac
->nparam
== 0) {
2719 "`%%rotate' invoked within macro without parameters");
2721 int rotate
= mmac
->rotate
+ reloc_value(evalresult
);
2723 rotate
%= (int)mmac
->nparam
;
2725 rotate
+= mmac
->nparam
;
2727 mmac
->rotate
= rotate
;
2729 return DIRECTIVE_FOUND
;
2734 tline
= tline
->next
;
2735 } while (tok_type_(tline
, TOK_WHITESPACE
));
2737 if (tok_type_(tline
, TOK_ID
) &&
2738 nasm_stricmp(tline
->text
, ".nolist") == 0) {
2741 tline
= tline
->next
;
2742 } while (tok_type_(tline
, TOK_WHITESPACE
));
2746 t
= expand_smacro(tline
);
2748 tokval
.t_type
= TOKEN_INVALID
;
2750 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2752 free_tlist(origline
);
2753 return DIRECTIVE_FOUND
;
2756 error(ERR_WARNING
|ERR_PASS1
,
2757 "trailing garbage after expression ignored");
2758 if (!is_simple(evalresult
)) {
2759 error(ERR_NONFATAL
, "non-constant value given to `%%rep'");
2760 return DIRECTIVE_FOUND
;
2762 count
= reloc_value(evalresult
) + 1;
2764 error(ERR_NONFATAL
, "`%%rep' expects a repeat count");
2767 free_tlist(origline
);
2769 tmp_defining
= defining
;
2770 defining
= nasm_malloc(sizeof(MMacro
));
2771 defining
->prev
= NULL
;
2772 defining
->name
= NULL
; /* flags this macro as a %rep block */
2773 defining
->casesense
= false;
2774 defining
->plus
= false;
2775 defining
->nolist
= nolist
;
2776 defining
->in_progress
= count
;
2777 defining
->max_depth
= 0;
2778 defining
->nparam_min
= defining
->nparam_max
= 0;
2779 defining
->defaults
= NULL
;
2780 defining
->dlist
= NULL
;
2781 defining
->expansion
= NULL
;
2782 defining
->next_active
= istk
->mstk
;
2783 defining
->rep_nest
= tmp_defining
;
2784 return DIRECTIVE_FOUND
;
2787 if (!defining
|| defining
->name
) {
2788 error(ERR_NONFATAL
, "`%%endrep': no matching `%%rep'");
2789 return DIRECTIVE_FOUND
;
2793 * Now we have a "macro" defined - although it has no name
2794 * and we won't be entering it in the hash tables - we must
2795 * push a macro-end marker for it on to istk->expansion.
2796 * After that, it will take care of propagating itself (a
2797 * macro-end marker line for a macro which is really a %rep
2798 * block will cause the macro to be re-expanded, complete
2799 * with another macro-end marker to ensure the process
2800 * continues) until the whole expansion is forcibly removed
2801 * from istk->expansion by a %exitrep.
2803 l
= nasm_malloc(sizeof(Line
));
2804 l
->next
= istk
->expansion
;
2805 l
->finishes
= defining
;
2807 istk
->expansion
= l
;
2809 istk
->mstk
= defining
;
2811 list
->uplevel(defining
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
2812 tmp_defining
= defining
;
2813 defining
= defining
->rep_nest
;
2814 free_tlist(origline
);
2815 return DIRECTIVE_FOUND
;
2819 * We must search along istk->expansion until we hit a
2820 * macro-end marker for a macro with no name. Then we set
2821 * its `in_progress' flag to 0.
2823 for (l
= istk
->expansion
; l
; l
= l
->next
)
2824 if (l
->finishes
&& !l
->finishes
->name
)
2828 l
->finishes
->in_progress
= 1;
2830 error(ERR_NONFATAL
, "`%%exitrep' not within `%%rep' block");
2831 free_tlist(origline
);
2832 return DIRECTIVE_FOUND
;
2838 casesense
= (i
== PP_DEFINE
|| i
== PP_XDEFINE
);
2840 tline
= tline
->next
;
2842 tline
= expand_id(tline
);
2843 if (!tline
|| (tline
->type
!= TOK_ID
&&
2844 (tline
->type
!= TOK_PREPROC_ID
||
2845 tline
->text
[1] != '$'))) {
2846 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
2848 free_tlist(origline
);
2849 return DIRECTIVE_FOUND
;
2852 ctx
= get_ctx(tline
->text
, &mname
, false);
2854 param_start
= tline
= tline
->next
;
2857 /* Expand the macro definition now for %xdefine and %ixdefine */
2858 if ((i
== PP_XDEFINE
) || (i
== PP_IXDEFINE
))
2859 tline
= expand_smacro(tline
);
2861 if (tok_is_(tline
, "(")) {
2863 * This macro has parameters.
2866 tline
= tline
->next
;
2870 error(ERR_NONFATAL
, "parameter identifier expected");
2871 free_tlist(origline
);
2872 return DIRECTIVE_FOUND
;
2874 if (tline
->type
!= TOK_ID
) {
2876 "`%s': parameter identifier expected",
2878 free_tlist(origline
);
2879 return DIRECTIVE_FOUND
;
2881 tline
->type
= TOK_SMAC_PARAM
+ nparam
++;
2882 tline
= tline
->next
;
2884 if (tok_is_(tline
, ",")) {
2885 tline
= tline
->next
;
2887 if (!tok_is_(tline
, ")")) {
2889 "`)' expected to terminate macro template");
2890 free_tlist(origline
);
2891 return DIRECTIVE_FOUND
;
2897 tline
= tline
->next
;
2899 if (tok_type_(tline
, TOK_WHITESPACE
))
2900 last
= tline
, tline
= tline
->next
;
2905 if (t
->type
== TOK_ID
) {
2906 for (tt
= param_start
; tt
; tt
= tt
->next
)
2907 if (tt
->type
>= TOK_SMAC_PARAM
&&
2908 !strcmp(tt
->text
, t
->text
))
2912 t
->next
= macro_start
;
2917 * Good. We now have a macro name, a parameter count, and a
2918 * token list (in reverse order) for an expansion. We ought
2919 * to be OK just to create an SMacro, store it, and let
2920 * free_tlist have the rest of the line (which we have
2921 * carefully re-terminated after chopping off the expansion
2924 define_smacro(ctx
, mname
, casesense
, nparam
, macro_start
);
2925 free_tlist(origline
);
2926 return DIRECTIVE_FOUND
;
2929 tline
= tline
->next
;
2931 tline
= expand_id(tline
);
2932 if (!tline
|| (tline
->type
!= TOK_ID
&&
2933 (tline
->type
!= TOK_PREPROC_ID
||
2934 tline
->text
[1] != '$'))) {
2935 error(ERR_NONFATAL
, "`%%undef' expects a macro identifier");
2936 free_tlist(origline
);
2937 return DIRECTIVE_FOUND
;
2940 error(ERR_WARNING
|ERR_PASS1
,
2941 "trailing garbage after macro name ignored");
2944 /* Find the context that symbol belongs to */
2945 ctx
= get_ctx(tline
->text
, &mname
, false);
2946 undef_smacro(ctx
, mname
);
2947 free_tlist(origline
);
2948 return DIRECTIVE_FOUND
;
2952 casesense
= (i
== PP_DEFSTR
);
2954 tline
= tline
->next
;
2956 tline
= expand_id(tline
);
2957 if (!tline
|| (tline
->type
!= TOK_ID
&&
2958 (tline
->type
!= TOK_PREPROC_ID
||
2959 tline
->text
[1] != '$'))) {
2960 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
2962 free_tlist(origline
);
2963 return DIRECTIVE_FOUND
;
2966 ctx
= get_ctx(tline
->text
, &mname
, false);
2968 tline
= expand_smacro(tline
->next
);
2971 while (tok_type_(tline
, TOK_WHITESPACE
))
2972 tline
= delete_Token(tline
);
2974 p
= detoken(tline
, false);
2975 macro_start
= nasm_malloc(sizeof(*macro_start
));
2976 macro_start
->next
= NULL
;
2977 macro_start
->text
= nasm_quote(p
, strlen(p
));
2978 macro_start
->type
= TOK_STRING
;
2979 macro_start
->a
.mac
= NULL
;
2983 * We now have a macro name, an implicit parameter count of
2984 * zero, and a string token to use as an expansion. Create
2985 * and store an SMacro.
2987 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2988 free_tlist(origline
);
2989 return DIRECTIVE_FOUND
;
2994 StrList
*xsl
= NULL
;
2995 StrList
**xst
= &xsl
;
2999 tline
= tline
->next
;
3001 tline
= expand_id(tline
);
3002 if (!tline
|| (tline
->type
!= TOK_ID
&&
3003 (tline
->type
!= TOK_PREPROC_ID
||
3004 tline
->text
[1] != '$'))) {
3006 "`%%pathsearch' expects a macro identifier as first parameter");
3007 free_tlist(origline
);
3008 return DIRECTIVE_FOUND
;
3010 ctx
= get_ctx(tline
->text
, &mname
, false);
3012 tline
= expand_smacro(tline
->next
);
3016 while (tok_type_(t
, TOK_WHITESPACE
))
3019 if (!t
|| (t
->type
!= TOK_STRING
&&
3020 t
->type
!= TOK_INTERNAL_STRING
)) {
3021 error(ERR_NONFATAL
, "`%%pathsearch' expects a file name");
3023 free_tlist(origline
);
3024 return DIRECTIVE_FOUND
; /* but we did _something_ */
3027 error(ERR_WARNING
|ERR_PASS1
,
3028 "trailing garbage after `%%pathsearch' ignored");
3030 if (t
->type
!= TOK_INTERNAL_STRING
)
3031 nasm_unquote(p
, NULL
);
3033 fp
= inc_fopen(p
, &xsl
, &xst
, true);
3036 fclose(fp
); /* Don't actually care about the file */
3038 macro_start
= nasm_malloc(sizeof(*macro_start
));
3039 macro_start
->next
= NULL
;
3040 macro_start
->text
= nasm_quote(p
, strlen(p
));
3041 macro_start
->type
= TOK_STRING
;
3042 macro_start
->a
.mac
= NULL
;
3047 * We now have a macro name, an implicit parameter count of
3048 * zero, and a string token to use as an expansion. Create
3049 * and store an SMacro.
3051 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3053 free_tlist(origline
);
3054 return DIRECTIVE_FOUND
;
3060 tline
= tline
->next
;
3062 tline
= expand_id(tline
);
3063 if (!tline
|| (tline
->type
!= TOK_ID
&&
3064 (tline
->type
!= TOK_PREPROC_ID
||
3065 tline
->text
[1] != '$'))) {
3067 "`%%strlen' expects a macro identifier as first parameter");
3068 free_tlist(origline
);
3069 return DIRECTIVE_FOUND
;
3071 ctx
= get_ctx(tline
->text
, &mname
, false);
3073 tline
= expand_smacro(tline
->next
);
3077 while (tok_type_(t
, TOK_WHITESPACE
))
3079 /* t should now point to the string */
3080 if (t
->type
!= TOK_STRING
) {
3082 "`%%strlen` requires string as second parameter");
3084 free_tlist(origline
);
3085 return DIRECTIVE_FOUND
;
3088 macro_start
= nasm_malloc(sizeof(*macro_start
));
3089 macro_start
->next
= NULL
;
3090 make_tok_num(macro_start
, nasm_unquote(t
->text
, NULL
));
3091 macro_start
->a
.mac
= NULL
;
3094 * We now have a macro name, an implicit parameter count of
3095 * zero, and a numeric token to use as an expansion. Create
3096 * and store an SMacro.
3098 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3100 free_tlist(origline
);
3101 return DIRECTIVE_FOUND
;
3106 tline
= tline
->next
;
3108 tline
= expand_id(tline
);
3109 if (!tline
|| (tline
->type
!= TOK_ID
&&
3110 (tline
->type
!= TOK_PREPROC_ID
||
3111 tline
->text
[1] != '$'))) {
3113 "`%%strcat' expects a macro identifier as first parameter");
3114 free_tlist(origline
);
3115 return DIRECTIVE_FOUND
;
3117 ctx
= get_ctx(tline
->text
, &mname
, false);
3119 tline
= expand_smacro(tline
->next
);
3123 for (t
= tline
; t
; t
= t
->next
) {
3125 case TOK_WHITESPACE
:
3128 len
+= t
->a
.len
= nasm_unquote(t
->text
, NULL
);
3131 if (!strcmp(t
->text
, ",")) /* permit comma separators */
3133 /* else fall through */
3136 "non-string passed to `%%strcat' (%d)", t
->type
);
3138 free_tlist(origline
);
3139 return DIRECTIVE_FOUND
;
3143 p
= pp
= nasm_malloc(len
);
3145 for (t
= tline
; t
; t
= t
->next
) {
3146 if (t
->type
== TOK_STRING
) {
3147 memcpy(p
, t
->text
, t
->a
.len
);
3153 * We now have a macro name, an implicit parameter count of
3154 * zero, and a numeric token to use as an expansion. Create
3155 * and store an SMacro.
3157 macro_start
= new_Token(NULL
, TOK_STRING
, NULL
, 0);
3158 macro_start
->text
= nasm_quote(pp
, len
);
3160 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3162 free_tlist(origline
);
3163 return DIRECTIVE_FOUND
;
3172 tline
= tline
->next
;
3174 tline
= expand_id(tline
);
3175 if (!tline
|| (tline
->type
!= TOK_ID
&&
3176 (tline
->type
!= TOK_PREPROC_ID
||
3177 tline
->text
[1] != '$'))) {
3179 "`%%substr' expects a macro identifier as first parameter");
3180 free_tlist(origline
);
3181 return DIRECTIVE_FOUND
;
3183 ctx
= get_ctx(tline
->text
, &mname
, false);
3185 tline
= expand_smacro(tline
->next
);
3189 while (tok_type_(t
, TOK_WHITESPACE
))
3192 /* t should now point to the string */
3193 if (t
->type
!= TOK_STRING
) {
3195 "`%%substr` requires string as second parameter");
3197 free_tlist(origline
);
3198 return DIRECTIVE_FOUND
;
3203 tokval
.t_type
= TOKEN_INVALID
;
3204 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
,
3208 free_tlist(origline
);
3209 return DIRECTIVE_FOUND
;
3210 } else if (!is_simple(evalresult
)) {
3211 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
3213 free_tlist(origline
);
3214 return DIRECTIVE_FOUND
;
3216 a1
= evalresult
->value
-1;
3218 while (tok_type_(tt
, TOK_WHITESPACE
))
3221 a2
= 1; /* Backwards compatibility: one character */
3223 tokval
.t_type
= TOKEN_INVALID
;
3224 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
,
3228 free_tlist(origline
);
3229 return DIRECTIVE_FOUND
;
3230 } else if (!is_simple(evalresult
)) {
3231 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
3233 free_tlist(origline
);
3234 return DIRECTIVE_FOUND
;
3236 a2
= evalresult
->value
;
3239 len
= nasm_unquote(t
->text
, NULL
);
3242 if (a1
+a2
> (int64_t)len
)
3245 macro_start
= nasm_malloc(sizeof(*macro_start
));
3246 macro_start
->next
= NULL
;
3247 macro_start
->text
= nasm_quote((a1
< 0) ? "" : t
->text
+a1
, a2
);
3248 macro_start
->type
= TOK_STRING
;
3249 macro_start
->a
.mac
= NULL
;
3252 * We now have a macro name, an implicit parameter count of
3253 * zero, and a numeric token to use as an expansion. Create
3254 * and store an SMacro.
3256 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3258 free_tlist(origline
);
3259 return DIRECTIVE_FOUND
;
3264 casesense
= (i
== PP_ASSIGN
);
3266 tline
= tline
->next
;
3268 tline
= expand_id(tline
);
3269 if (!tline
|| (tline
->type
!= TOK_ID
&&
3270 (tline
->type
!= TOK_PREPROC_ID
||
3271 tline
->text
[1] != '$'))) {
3273 "`%%%sassign' expects a macro identifier",
3274 (i
== PP_IASSIGN
? "i" : ""));
3275 free_tlist(origline
);
3276 return DIRECTIVE_FOUND
;
3278 ctx
= get_ctx(tline
->text
, &mname
, false);
3280 tline
= expand_smacro(tline
->next
);
3285 tokval
.t_type
= TOKEN_INVALID
;
3287 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
3290 free_tlist(origline
);
3291 return DIRECTIVE_FOUND
;
3295 error(ERR_WARNING
|ERR_PASS1
,
3296 "trailing garbage after expression ignored");
3298 if (!is_simple(evalresult
)) {
3300 "non-constant value given to `%%%sassign'",
3301 (i
== PP_IASSIGN
? "i" : ""));
3302 free_tlist(origline
);
3303 return DIRECTIVE_FOUND
;
3306 macro_start
= nasm_malloc(sizeof(*macro_start
));
3307 macro_start
->next
= NULL
;
3308 make_tok_num(macro_start
, reloc_value(evalresult
));
3309 macro_start
->a
.mac
= NULL
;
3312 * We now have a macro name, an implicit parameter count of
3313 * zero, and a numeric token to use as an expansion. Create
3314 * and store an SMacro.
3316 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3317 free_tlist(origline
);
3318 return DIRECTIVE_FOUND
;
3322 * Syntax is `%line nnn[+mmm] [filename]'
3324 tline
= tline
->next
;
3326 if (!tok_type_(tline
, TOK_NUMBER
)) {
3327 error(ERR_NONFATAL
, "`%%line' expects line number");
3328 free_tlist(origline
);
3329 return DIRECTIVE_FOUND
;
3331 k
= readnum(tline
->text
, &err
);
3333 tline
= tline
->next
;
3334 if (tok_is_(tline
, "+")) {
3335 tline
= tline
->next
;
3336 if (!tok_type_(tline
, TOK_NUMBER
)) {
3337 error(ERR_NONFATAL
, "`%%line' expects line increment");
3338 free_tlist(origline
);
3339 return DIRECTIVE_FOUND
;
3341 m
= readnum(tline
->text
, &err
);
3342 tline
= tline
->next
;
3348 nasm_free(src_set_fname(detoken(tline
, false)));
3350 free_tlist(origline
);
3351 return DIRECTIVE_FOUND
;
3355 "preprocessor directive `%s' not yet implemented",
3357 return DIRECTIVE_FOUND
;
3362 * Ensure that a macro parameter contains a condition code and
3363 * nothing else. Return the condition code index if so, or -1
3366 static int find_cc(Token
* t
)
3372 return -1; /* Probably a %+ without a space */
3375 if (t
->type
!= TOK_ID
)
3379 if (tt
&& (tt
->type
!= TOK_OTHER
|| strcmp(tt
->text
, ",")))
3383 j
= elements(conditions
);
3386 m
= nasm_stricmp(t
->text
, conditions
[k
]);
3401 static bool paste_tokens(Token
**head
, bool handle_paste_tokens
)
3403 Token
**tail
, *t
, *tt
;
3405 bool did_paste
= false;
3408 /* Now handle token pasting... */
3411 while ((t
= *tail
) && (tt
= t
->next
)) {
3413 case TOK_WHITESPACE
:
3414 if (tt
->type
== TOK_WHITESPACE
) {
3415 /* Zap adjacent whitespace tokens */
3416 t
->next
= delete_Token(tt
);
3418 /* Do not advance paste_head here */
3423 case TOK_PREPROC_ID
:
3430 while (tt
&& (tt
->type
== TOK_ID
|| tt
->type
== TOK_PREPROC_ID
||
3431 tt
->type
== TOK_NUMBER
|| tt
->type
== TOK_FLOAT
||
3432 tt
->type
== TOK_OTHER
)) {
3433 len
+= strlen(tt
->text
);
3437 /* Now tt points to the first token after the potential
3439 if (tt
!= t
->next
) {
3440 /* We have at least two tokens... */
3441 len
+= strlen(t
->text
);
3442 p
= tmp
= nasm_malloc(len
+1);
3446 p
= strchr(p
, '\0');
3447 t
= delete_Token(t
);
3450 t
= *tail
= tokenize(tmp
);
3457 t
->next
= tt
; /* Attach the remaining token chain */
3465 case TOK_PASTE
: /* %+ */
3466 if (handle_paste_tokens
) {
3467 /* Zap %+ and whitespace tokens to the right */
3468 while (t
&& (t
->type
== TOK_WHITESPACE
||
3469 t
->type
== TOK_PASTE
))
3470 t
= *tail
= delete_Token(t
);
3471 if (!paste_head
|| !t
)
3472 break; /* Nothing to paste with */
3476 while (tok_type_(tt
, TOK_WHITESPACE
))
3477 tt
= t
->next
= delete_Token(tt
);
3480 tmp
= nasm_strcat(t
->text
, tt
->text
);
3482 tt
= delete_Token(tt
);
3483 t
= *tail
= tokenize(tmp
);
3489 t
->next
= tt
; /* Attach the remaining token chain */
3496 /* else fall through */
3498 tail
= paste_head
= &t
->next
;
3505 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3506 * %-n) and MMacro-local identifiers (%%foo) as well as
3507 * macro indirection (%[...]).
3509 static Token
*expand_mmac_params(Token
* tline
)
3511 Token
*t
, *tt
, **tail
, *thead
;
3512 bool changed
= false;
3518 if (tline
->type
== TOK_PREPROC_ID
&&
3519 (((tline
->text
[1] == '+' || tline
->text
[1] == '-')
3520 && tline
->text
[2]) || tline
->text
[1] == '%'
3521 || (tline
->text
[1] >= '0' && tline
->text
[1] <= '9'))) {
3523 int type
= 0, cc
; /* type = 0 to placate optimisers */
3530 tline
= tline
->next
;
3533 while (mac
&& !mac
->name
) /* avoid mistaking %reps for macros */
3534 mac
= mac
->next_active
;
3536 error(ERR_NONFATAL
, "`%s': not in a macro call", t
->text
);
3538 switch (t
->text
[1]) {
3540 * We have to make a substitution of one of the
3541 * forms %1, %-1, %+1, %%foo, %0.
3545 snprintf(tmpbuf
, sizeof(tmpbuf
), "%d", mac
->nparam
);
3546 text
= nasm_strdup(tmpbuf
);
3550 snprintf(tmpbuf
, sizeof(tmpbuf
), "..@%"PRIu64
".",
3552 text
= nasm_strcat(tmpbuf
, t
->text
+ 2);
3555 n
= atoi(t
->text
+ 2) - 1;
3556 if (n
>= mac
->nparam
)
3559 if (mac
->nparam
> 1)
3560 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3561 tt
= mac
->params
[n
];
3566 "macro parameter %d is not a condition code",
3571 if (inverse_ccs
[cc
] == -1) {
3573 "condition code `%s' is not invertible",
3577 text
= nasm_strdup(conditions
[inverse_ccs
[cc
]]);
3581 n
= atoi(t
->text
+ 2) - 1;
3582 if (n
>= mac
->nparam
)
3585 if (mac
->nparam
> 1)
3586 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3587 tt
= mac
->params
[n
];
3592 "macro parameter %d is not a condition code",
3597 text
= nasm_strdup(conditions
[cc
]);
3601 n
= atoi(t
->text
+ 1) - 1;
3602 if (n
>= mac
->nparam
)
3605 if (mac
->nparam
> 1)
3606 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3607 tt
= mac
->params
[n
];
3610 for (i
= 0; i
< mac
->paramlen
[n
]; i
++) {
3611 *tail
= new_Token(NULL
, tt
->type
, tt
->text
, 0);
3612 tail
= &(*tail
)->next
;
3616 text
= NULL
; /* we've done it here */
3631 } else if (tline
->type
== TOK_INDIRECT
) {
3633 tline
= tline
->next
;
3634 tt
= tokenize(t
->text
);
3635 tt
= expand_mmac_params(tt
);
3636 tt
= expand_smacro(tt
);
3639 tt
->a
.mac
= NULL
; /* Necessary? */
3647 tline
= tline
->next
;
3655 paste_tokens(&thead
, false);
3661 * Expand all single-line macro calls made in the given line.
3662 * Return the expanded version of the line. The original is deemed
3663 * to be destroyed in the process. (In reality we'll just move
3664 * Tokens from input to output a lot of the time, rather than
3665 * actually bothering to destroy and replicate.)
3667 #define DEADMAN_LIMIT (1 << 20)
3669 static Token
*expand_smacro(Token
* tline
)
3671 Token
*t
, *tt
, *mstart
, **tail
, *thead
;
3672 struct hash_table
*smtbl
;
3673 SMacro
*head
= NULL
, *m
;
3676 unsigned int nparam
, sparam
;
3678 Token
*org_tline
= tline
;
3681 int deadman
= DEADMAN_LIMIT
;
3685 * Trick: we should avoid changing the start token pointer since it can
3686 * be contained in "next" field of other token. Because of this
3687 * we allocate a copy of first token and work with it; at the end of
3688 * routine we copy it back
3692 new_Token(org_tline
->next
, org_tline
->type
, org_tline
->text
,
3694 tline
->a
.mac
= org_tline
->a
.mac
;
3695 nasm_free(org_tline
->text
);
3696 org_tline
->text
= NULL
;
3699 expanded
= true; /* Always expand %+ at least once */
3705 while (tline
) { /* main token loop */
3707 error(ERR_NONFATAL
, "interminable macro recursion");
3711 if ((mname
= tline
->text
)) {
3712 /* if this token is a local macro, look in local context */
3713 if (tline
->type
== TOK_ID
|| tline
->type
== TOK_PREPROC_ID
)
3714 ctx
= get_ctx(mname
, &mname
, true);
3717 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
3718 head
= (SMacro
*) hash_findix(smtbl
, mname
);
3721 * We've hit an identifier. As in is_mmacro below, we first
3722 * check whether the identifier is a single-line macro at
3723 * all, then think about checking for parameters if
3726 for (m
= head
; m
; m
= m
->next
)
3727 if (!mstrcmp(m
->name
, mname
, m
->casesense
))
3733 if (m
->nparam
== 0) {
3735 * Simple case: the macro is parameterless. Discard the
3736 * one token that the macro call took, and push the
3737 * expansion back on the to-do stack.
3739 if (!m
->expansion
) {
3740 if (!strcmp("__FILE__", m
->name
)) {
3743 src_get(&num
, &file
);
3744 tline
->text
= nasm_quote(file
, strlen(file
));
3745 tline
->type
= TOK_STRING
;
3749 if (!strcmp("__LINE__", m
->name
)) {
3750 nasm_free(tline
->text
);
3751 make_tok_num(tline
, src_get_linnum());
3754 if (!strcmp("__BITS__", m
->name
)) {
3755 nasm_free(tline
->text
);
3756 make_tok_num(tline
, globalbits
);
3759 tline
= delete_Token(tline
);
3764 * Complicated case: at least one macro with this name
3765 * exists and takes parameters. We must find the
3766 * parameters in the call, count them, find the SMacro
3767 * that corresponds to that form of the macro call, and
3768 * substitute for the parameters when we expand. What a
3771 /*tline = tline->next;
3772 skip_white_(tline); */
3775 while (tok_type_(t
, TOK_SMAC_END
)) {
3776 t
->a
.mac
->in_progress
= false;
3778 t
= tline
->next
= delete_Token(t
);
3781 } while (tok_type_(tline
, TOK_WHITESPACE
));
3782 if (!tok_is_(tline
, "(")) {
3784 * This macro wasn't called with parameters: ignore
3785 * the call. (Behaviour borrowed from gnu cpp.)
3794 sparam
= PARAM_DELTA
;
3795 params
= nasm_malloc(sparam
* sizeof(Token
*));
3796 params
[0] = tline
->next
;
3797 paramsize
= nasm_malloc(sparam
* sizeof(int));
3799 while (true) { /* parameter loop */
3801 * For some unusual expansions
3802 * which concatenates function call
3805 while (tok_type_(t
, TOK_SMAC_END
)) {
3806 t
->a
.mac
->in_progress
= false;
3808 t
= tline
->next
= delete_Token(t
);
3814 "macro call expects terminating `)'");
3817 if (tline
->type
== TOK_WHITESPACE
3819 if (paramsize
[nparam
])
3822 params
[nparam
] = tline
->next
;
3823 continue; /* parameter loop */
3825 if (tline
->type
== TOK_OTHER
3826 && tline
->text
[1] == 0) {
3827 char ch
= tline
->text
[0];
3828 if (ch
== ',' && !paren
&& brackets
<= 0) {
3829 if (++nparam
>= sparam
) {
3830 sparam
+= PARAM_DELTA
;
3831 params
= nasm_realloc(params
,
3836 nasm_realloc(paramsize
,
3840 params
[nparam
] = tline
->next
;
3841 paramsize
[nparam
] = 0;
3843 continue; /* parameter loop */
3846 (brackets
> 0 || (brackets
== 0 &&
3847 !paramsize
[nparam
])))
3849 if (!(brackets
++)) {
3850 params
[nparam
] = tline
->next
;
3851 continue; /* parameter loop */
3854 if (ch
== '}' && brackets
> 0)
3855 if (--brackets
== 0) {
3857 continue; /* parameter loop */
3859 if (ch
== '(' && !brackets
)
3861 if (ch
== ')' && brackets
<= 0)
3867 error(ERR_NONFATAL
, "braces do not "
3868 "enclose all of macro parameter");
3870 paramsize
[nparam
] += white
+ 1;
3872 } /* parameter loop */
3874 while (m
&& (m
->nparam
!= nparam
||
3875 mstrcmp(m
->name
, mname
,
3879 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MNP
,
3880 "macro `%s' exists, "
3881 "but not taking %d parameters",
3882 mstart
->text
, nparam
);
3885 if (m
&& m
->in_progress
)
3887 if (!m
) { /* in progess or didn't find '(' or wrong nparam */
3889 * Design question: should we handle !tline, which
3890 * indicates missing ')' here, or expand those
3891 * macros anyway, which requires the (t) test a few
3895 nasm_free(paramsize
);
3899 * Expand the macro: we are placed on the last token of the
3900 * call, so that we can easily split the call from the
3901 * following tokens. We also start by pushing an SMAC_END
3902 * token for the cycle removal.
3909 tt
= new_Token(tline
, TOK_SMAC_END
, NULL
, 0);
3911 m
->in_progress
= true;
3913 for (t
= m
->expansion
; t
; t
= t
->next
) {
3914 if (t
->type
>= TOK_SMAC_PARAM
) {
3915 Token
*pcopy
= tline
, **ptail
= &pcopy
;
3919 ttt
= params
[t
->type
- TOK_SMAC_PARAM
];
3920 for (i
= paramsize
[t
->type
- TOK_SMAC_PARAM
];
3923 new_Token(tline
, ttt
->type
, ttt
->text
,
3929 } else if (t
->type
== TOK_PREPROC_Q
) {
3930 tt
= new_Token(tline
, TOK_ID
, mname
, 0);
3932 } else if (t
->type
== TOK_PREPROC_QQ
) {
3933 tt
= new_Token(tline
, TOK_ID
, m
->name
, 0);
3936 tt
= new_Token(tline
, t
->type
, t
->text
, 0);
3942 * Having done that, get rid of the macro call, and clean
3943 * up the parameters.
3946 nasm_free(paramsize
);
3949 continue; /* main token loop */
3954 if (tline
->type
== TOK_SMAC_END
) {
3955 tline
->a
.mac
->in_progress
= false;
3956 tline
= delete_Token(tline
);
3959 tline
= tline
->next
;
3967 * Now scan the entire line and look for successive TOK_IDs that resulted
3968 * after expansion (they can't be produced by tokenize()). The successive
3969 * TOK_IDs should be concatenated.
3970 * Also we look for %+ tokens and concatenate the tokens before and after
3971 * them (without white spaces in between).
3973 if (expanded
&& paste_tokens(&thead
, true)) {
3975 * If we concatenated something, *and* we had previously expanded
3976 * an actual macro, scan the lines again for macros...
3985 *org_tline
= *thead
;
3986 /* since we just gave text to org_line, don't free it */
3988 delete_Token(thead
);
3990 /* the expression expanded to empty line;
3991 we can't return NULL for some reasons
3992 we just set the line to a single WHITESPACE token. */
3993 memset(org_tline
, 0, sizeof(*org_tline
));
3994 org_tline
->text
= NULL
;
3995 org_tline
->type
= TOK_WHITESPACE
;
4004 * Similar to expand_smacro but used exclusively with macro identifiers
4005 * right before they are fetched in. The reason is that there can be
4006 * identifiers consisting of several subparts. We consider that if there
4007 * are more than one element forming the name, user wants a expansion,
4008 * otherwise it will be left as-is. Example:
4012 * the identifier %$abc will be left as-is so that the handler for %define
4013 * will suck it and define the corresponding value. Other case:
4015 * %define _%$abc cde
4017 * In this case user wants name to be expanded *before* %define starts
4018 * working, so we'll expand %$abc into something (if it has a value;
4019 * otherwise it will be left as-is) then concatenate all successive
4022 static Token
*expand_id(Token
* tline
)
4024 Token
*cur
, *oldnext
= NULL
;
4026 if (!tline
|| !tline
->next
)
4031 (cur
->next
->type
== TOK_ID
||
4032 cur
->next
->type
== TOK_PREPROC_ID
4033 || cur
->next
->type
== TOK_NUMBER
))
4036 /* If identifier consists of just one token, don't expand */
4041 oldnext
= cur
->next
; /* Detach the tail past identifier */
4042 cur
->next
= NULL
; /* so that expand_smacro stops here */
4045 tline
= expand_smacro(tline
);
4048 /* expand_smacro possibly changhed tline; re-scan for EOL */
4050 while (cur
&& cur
->next
)
4053 cur
->next
= oldnext
;
4060 * Determine whether the given line constitutes a multi-line macro
4061 * call, and return the MMacro structure called if so. Doesn't have
4062 * to check for an initial label - that's taken care of in
4063 * expand_mmacro - but must check numbers of parameters. Guaranteed
4064 * to be called with tline->type == TOK_ID, so the putative macro
4065 * name is easy to find.
4067 static MMacro
*is_mmacro(Token
* tline
, Token
*** params_array
)
4073 head
= (MMacro
*) hash_findix(&mmacros
, tline
->text
);
4076 * Efficiency: first we see if any macro exists with the given
4077 * name. If not, we can return NULL immediately. _Then_ we
4078 * count the parameters, and then we look further along the
4079 * list if necessary to find the proper MMacro.
4081 for (m
= head
; m
; m
= m
->next
)
4082 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
4088 * OK, we have a potential macro. Count and demarcate the
4091 count_mmac_params(tline
->next
, &nparam
, ¶ms
);
4094 * So we know how many parameters we've got. Find the MMacro
4095 * structure that handles this number.
4098 if (m
->nparam_min
<= nparam
4099 && (m
->plus
|| nparam
<= m
->nparam_max
)) {
4101 * This one is right. Just check if cycle removal
4102 * prohibits us using it before we actually celebrate...
4104 if (m
->in_progress
> m
->max_depth
) {
4105 if (m
->max_depth
> 0) {
4107 "reached maximum recursion depth of %i",
4114 * It's right, and we can use it. Add its default
4115 * parameters to the end of our list if necessary.
4117 if (m
->defaults
&& nparam
< m
->nparam_min
+ m
->ndefs
) {
4119 nasm_realloc(params
,
4120 ((m
->nparam_min
+ m
->ndefs
+
4121 1) * sizeof(*params
)));
4122 while (nparam
< m
->nparam_min
+ m
->ndefs
) {
4123 params
[nparam
] = m
->defaults
[nparam
- m
->nparam_min
];
4128 * If we've gone over the maximum parameter count (and
4129 * we're in Plus mode), ignore parameters beyond
4132 if (m
->plus
&& nparam
> m
->nparam_max
)
4133 nparam
= m
->nparam_max
;
4135 * Then terminate the parameter list, and leave.
4137 if (!params
) { /* need this special case */
4138 params
= nasm_malloc(sizeof(*params
));
4141 params
[nparam
] = NULL
;
4142 *params_array
= params
;
4146 * This one wasn't right: look for the next one with the
4149 for (m
= m
->next
; m
; m
= m
->next
)
4150 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
4155 * After all that, we didn't find one with the right number of
4156 * parameters. Issue a warning, and fail to expand the macro.
4158 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MNP
,
4159 "macro `%s' exists, but not taking %d parameters",
4160 tline
->text
, nparam
);
4167 * Save MMacro invocation specific fields in
4168 * preparation for a recursive macro expansion
4170 static void push_mmacro(MMacro
*m
)
4172 MMacroInvocation
*i
;
4174 i
= nasm_malloc(sizeof(MMacroInvocation
));
4176 i
->params
= m
->params
;
4177 i
->iline
= m
->iline
;
4178 i
->nparam
= m
->nparam
;
4179 i
->rotate
= m
->rotate
;
4180 i
->paramlen
= m
->paramlen
;
4181 i
->unique
= m
->unique
;
4187 * Restore MMacro invocation specific fields that were
4188 * saved during a previous recursive macro expansion
4190 static void pop_mmacro(MMacro
*m
)
4192 MMacroInvocation
*i
;
4194 if(m
->prev
!= NULL
){
4197 m
->params
= i
->params
;
4198 m
->iline
= i
->iline
;
4199 m
->nparam
= i
->nparam
;
4200 m
->rotate
= i
->rotate
;
4201 m
->paramlen
= i
->paramlen
;
4202 m
->unique
= i
->unique
;
4209 * Expand the multi-line macro call made by the given line, if
4210 * there is one to be expanded. If there is, push the expansion on
4211 * istk->expansion and return 1. Otherwise return 0.
4213 static int expand_mmacro(Token
* tline
)
4215 Token
*startline
= tline
;
4216 Token
*label
= NULL
;
4217 int dont_prepend
= 0;
4218 Token
**params
, *t
, *mtok
, *tt
;
4221 int i
, nparam
, *paramlen
;
4226 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
4227 if (!tok_type_(t
, TOK_ID
) && !tok_type_(t
, TOK_PREPROC_ID
))
4230 m
= is_mmacro(t
, ¶ms
);
4236 * We have an id which isn't a macro call. We'll assume
4237 * it might be a label; we'll also check to see if a
4238 * colon follows it. Then, if there's another id after
4239 * that lot, we'll check it again for macro-hood.
4243 if (tok_type_(t
, TOK_WHITESPACE
))
4244 last
= t
, t
= t
->next
;
4245 if (tok_is_(t
, ":")) {
4247 last
= t
, t
= t
->next
;
4248 if (tok_type_(t
, TOK_WHITESPACE
))
4249 last
= t
, t
= t
->next
;
4251 if (!tok_type_(t
, TOK_ID
) || (m
= is_mmacro(t
, ¶ms
)) == NULL
)
4259 * Fix up the parameters: this involves stripping leading and
4260 * trailing whitespace, then stripping braces if they are
4263 for (nparam
= 0; params
[nparam
]; nparam
++) ;
4264 paramlen
= nparam
? nasm_malloc(nparam
* sizeof(*paramlen
)) : NULL
;
4266 for (i
= 0; params
[i
]; i
++) {
4268 int comma
= (!m
->plus
|| i
< nparam
- 1);
4272 if (tok_is_(t
, "{"))
4273 t
= t
->next
, brace
= true, comma
= false;
4277 if (comma
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, ","))
4278 break; /* ... because we have hit a comma */
4279 if (comma
&& t
->type
== TOK_WHITESPACE
4280 && tok_is_(t
->next
, ","))
4281 break; /* ... or a space then a comma */
4282 if (brace
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, "}"))
4283 break; /* ... or a brace */
4290 * OK, we have a MMacro structure together with a set of
4291 * parameters. We must now go through the expansion and push
4292 * copies of each Line on to istk->expansion. Substitution of
4293 * parameter tokens and macro-local tokens doesn't get done
4294 * until the single-line macro substitution process; this is
4295 * because delaying them allows us to change the semantics
4296 * later through %rotate.
4298 * First, push an end marker on to istk->expansion, mark this
4299 * macro as in progress, and set up its invocation-specific
4302 ll
= nasm_malloc(sizeof(Line
));
4303 ll
->next
= istk
->expansion
;
4306 istk
->expansion
= ll
;
4309 * Save the previous MMacro expansion in the case of
4312 if (m
->max_depth
&& m
->in_progress
)
4320 m
->paramlen
= paramlen
;
4321 m
->unique
= unique
++;
4324 m
->next_active
= istk
->mstk
;
4327 for (l
= m
->expansion
; l
; l
= l
->next
) {
4330 ll
= nasm_malloc(sizeof(Line
));
4331 ll
->finishes
= NULL
;
4332 ll
->next
= istk
->expansion
;
4333 istk
->expansion
= ll
;
4336 for (t
= l
->first
; t
; t
= t
->next
) {
4340 tt
= *tail
= new_Token(NULL
, TOK_ID
, mname
, 0);
4342 case TOK_PREPROC_QQ
:
4343 tt
= *tail
= new_Token(NULL
, TOK_ID
, m
->name
, 0);
4345 case TOK_PREPROC_ID
:
4346 if (t
->text
[1] == '0' && t
->text
[2] == '0') {
4354 tt
= *tail
= new_Token(NULL
, x
->type
, x
->text
, 0);
4363 * If we had a label, push it on as the first line of
4364 * the macro expansion.
4367 if (dont_prepend
< 0)
4368 free_tlist(startline
);
4370 ll
= nasm_malloc(sizeof(Line
));
4371 ll
->finishes
= NULL
;
4372 ll
->next
= istk
->expansion
;
4373 istk
->expansion
= ll
;
4374 ll
->first
= startline
;
4375 if (!dont_prepend
) {
4377 label
= label
->next
;
4378 label
->next
= tt
= new_Token(NULL
, TOK_OTHER
, ":", 0);
4383 list
->uplevel(m
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
4388 /* The function that actually does the error reporting */
4389 static void verror(int severity
, const char *fmt
, va_list arg
)
4393 vsnprintf(buff
, sizeof(buff
), fmt
, arg
);
4395 if (istk
&& istk
->mstk
&& istk
->mstk
->name
)
4396 _error(severity
, "(%s:%d) %s", istk
->mstk
->name
,
4397 istk
->mstk
->lineno
, buff
);
4399 _error(severity
, "%s", buff
);
4403 * Since preprocessor always operate only on the line that didn't
4404 * arrived yet, we should always use ERR_OFFBY1.
4406 static void error(int severity
, const char *fmt
, ...)
4410 /* If we're in a dead branch of IF or something like it, ignore the error */
4411 if (istk
&& istk
->conds
&& !emitting(istk
->conds
->state
))
4415 verror(severity
, fmt
, arg
);
4420 * Because %else etc are evaluated in the state context
4421 * of the previous branch, errors might get lost with error():
4422 * %if 0 ... %else trailing garbage ... %endif
4423 * So %else etc should report errors with this function.
4425 static void error_precond(int severity
, const char *fmt
, ...)
4429 /* Only ignore the error if it's really in a dead branch */
4430 if (istk
&& istk
->conds
&& istk
->conds
->state
== COND_NEVER
)
4434 verror(severity
, fmt
, arg
);
4439 pp_reset(char *file
, int apass
, efunc errfunc
, evalfunc eval
,
4440 ListGen
* listgen
, StrList
**deplist
)
4446 istk
= nasm_malloc(sizeof(Include
));
4449 istk
->expansion
= NULL
;
4451 istk
->fp
= fopen(file
, "r");
4453 src_set_fname(nasm_strdup(file
));
4457 error(ERR_FATAL
|ERR_NOFILE
, "unable to open input file `%s'",
4460 nested_mac_count
= 0;
4461 nested_rep_count
= 0;
4464 if (tasm_compatible_mode
) {
4465 stdmacpos
= nasm_stdmac
;
4467 stdmacpos
= nasm_stdmac_after_tasm
;
4469 any_extrastdmac
= extrastdmac
&& *extrastdmac
;
4475 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4476 * The caller, however, will also pass in 3 for preprocess-only so
4477 * we can set __PASS__ accordingly.
4479 pass
= apass
> 2 ? 2 : apass
;
4481 dephead
= deptail
= deplist
;
4483 StrList
*sl
= nasm_malloc(strlen(file
)+1+sizeof sl
->next
);
4485 strcpy(sl
->str
, file
);
4487 deptail
= &sl
->next
;
4491 * Define the __PASS__ macro. This is defined here unlike
4492 * all the other builtins, because it is special -- it varies between
4495 t
= nasm_malloc(sizeof(*t
));
4497 make_tok_num(t
, apass
);
4499 define_smacro(NULL
, "__PASS__", true, 0, t
);
4502 static char *pp_getline(void)
4509 * Fetch a tokenized line, either from the macro-expansion
4510 * buffer or from the input file.
4513 while (istk
->expansion
&& istk
->expansion
->finishes
) {
4514 Line
*l
= istk
->expansion
;
4515 if (!l
->finishes
->name
&& l
->finishes
->in_progress
> 1) {
4519 * This is a macro-end marker for a macro with no
4520 * name, which means it's not really a macro at all
4521 * but a %rep block, and the `in_progress' field is
4522 * more than 1, meaning that we still need to
4523 * repeat. (1 means the natural last repetition; 0
4524 * means termination by %exitrep.) We have
4525 * therefore expanded up to the %endrep, and must
4526 * push the whole block on to the expansion buffer
4527 * again. We don't bother to remove the macro-end
4528 * marker: we'd only have to generate another one
4531 l
->finishes
->in_progress
--;
4532 for (l
= l
->finishes
->expansion
; l
; l
= l
->next
) {
4533 Token
*t
, *tt
, **tail
;
4535 ll
= nasm_malloc(sizeof(Line
));
4536 ll
->next
= istk
->expansion
;
4537 ll
->finishes
= NULL
;
4541 for (t
= l
->first
; t
; t
= t
->next
) {
4542 if (t
->text
|| t
->type
== TOK_WHITESPACE
) {
4544 new_Token(NULL
, t
->type
, t
->text
, 0);
4549 istk
->expansion
= ll
;
4553 * Check whether a `%rep' was started and not ended
4554 * within this macro expansion. This can happen and
4555 * should be detected. It's a fatal error because
4556 * I'm too confused to work out how to recover
4562 "defining with name in expansion");
4563 else if (istk
->mstk
->name
)
4565 "`%%rep' without `%%endrep' within"
4566 " expansion of macro `%s'",
4571 * FIXME: investigate the relationship at this point between
4572 * istk->mstk and l->finishes
4575 MMacro
*m
= istk
->mstk
;
4576 istk
->mstk
= m
->next_active
;
4579 * This was a real macro call, not a %rep, and
4580 * therefore the parameter information needs to
4583 if (m
->prev
!= NULL
) {
4586 nasm_free(m
->params
);
4587 free_tlist(m
->iline
);
4588 nasm_free(m
->paramlen
);
4590 l
->finishes
->in_progress
--;
4594 istk
->expansion
= l
->next
;
4596 list
->downlevel(LIST_MACRO
);
4599 while (1) { /* until we get a line we can use */
4601 if (istk
->expansion
) { /* from a macro expansion */
4603 Line
*l
= istk
->expansion
;
4605 istk
->mstk
->lineno
++;
4607 istk
->expansion
= l
->next
;
4609 p
= detoken(tline
, false);
4610 list
->line(LIST_MACRO
, p
);
4615 if (line
) { /* from the current input file */
4616 line
= prepreproc(line
);
4617 tline
= tokenize(line
);
4622 * The current file has ended; work down the istk
4629 "expected `%%endif' before end of file");
4630 /* only set line and file name if there's a next node */
4632 src_set_linnum(i
->lineno
);
4633 nasm_free(src_set_fname(i
->fname
));
4636 list
->downlevel(LIST_INCLUDE
);
4640 if (istk
->expansion
&& istk
->expansion
->finishes
)
4646 * We must expand MMacro parameters and MMacro-local labels
4647 * _before_ we plunge into directive processing, to cope
4648 * with things like `%define something %1' such as STRUC
4649 * uses. Unless we're _defining_ a MMacro, in which case
4650 * those tokens should be left alone to go into the
4651 * definition; and unless we're in a non-emitting
4652 * condition, in which case we don't want to meddle with
4655 if (!defining
&& !(istk
->conds
&& !emitting(istk
->conds
->state
))
4656 && !(istk
->mstk
&& !istk
->mstk
->in_progress
)) {
4657 tline
= expand_mmac_params(tline
);
4661 * Check the line to see if it's a preprocessor directive.
4663 if (do_directive(tline
) == DIRECTIVE_FOUND
) {
4665 } else if (defining
) {
4667 * We're defining a multi-line macro. We emit nothing
4669 * shove the tokenized line on to the macro definition.
4671 Line
*l
= nasm_malloc(sizeof(Line
));
4672 l
->next
= defining
->expansion
;
4675 defining
->expansion
= l
;
4677 } else if (istk
->conds
&& !emitting(istk
->conds
->state
)) {
4679 * We're in a non-emitting branch of a condition block.
4680 * Emit nothing at all, not even a blank line: when we
4681 * emerge from the condition we'll give a line-number
4682 * directive so we keep our place correctly.
4686 } else if (istk
->mstk
&& !istk
->mstk
->in_progress
) {
4688 * We're in a %rep block which has been terminated, so
4689 * we're walking through to the %endrep without
4690 * emitting anything. Emit nothing at all, not even a
4691 * blank line: when we emerge from the %rep block we'll
4692 * give a line-number directive so we keep our place
4698 tline
= expand_smacro(tline
);
4699 if (!expand_mmacro(tline
)) {
4701 * De-tokenize the line again, and emit it.
4703 line
= detoken(tline
, true);
4707 continue; /* expand_mmacro calls free_tlist */
4715 static void pp_cleanup(int pass
)
4718 if(defining
->name
) {
4720 "end of file while still defining macro `%s'",
4723 error(ERR_NONFATAL
, "end of file while still in %%rep");
4726 free_mmacro(defining
);
4735 nasm_free(i
->fname
);
4740 nasm_free(src_set_fname(NULL
));
4745 while ((i
= ipath
)) {
4754 void pp_include_path(char *path
)
4758 i
= nasm_malloc(sizeof(IncPath
));
4759 i
->path
= path
? nasm_strdup(path
) : NULL
;
4762 if (ipath
!= NULL
) {
4764 while (j
->next
!= NULL
)
4772 void pp_pre_include(char *fname
)
4774 Token
*inc
, *space
, *name
;
4777 name
= new_Token(NULL
, TOK_INTERNAL_STRING
, fname
, 0);
4778 space
= new_Token(name
, TOK_WHITESPACE
, NULL
, 0);
4779 inc
= new_Token(space
, TOK_PREPROC_ID
, "%include", 0);
4781 l
= nasm_malloc(sizeof(Line
));
4788 void pp_pre_define(char *definition
)
4794 equals
= strchr(definition
, '=');
4795 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
4796 def
= new_Token(space
, TOK_PREPROC_ID
, "%define", 0);
4799 space
->next
= tokenize(definition
);
4803 l
= nasm_malloc(sizeof(Line
));
4810 void pp_pre_undefine(char *definition
)
4815 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
4816 def
= new_Token(space
, TOK_PREPROC_ID
, "%undef", 0);
4817 space
->next
= tokenize(definition
);
4819 l
= nasm_malloc(sizeof(Line
));
4827 * Added by Keith Kanios:
4829 * This function is used to assist with "runtime" preprocessor
4830 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4832 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4833 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4836 void pp_runtime(char *definition
)
4840 def
= tokenize(definition
);
4841 if(do_directive(def
) == NO_DIRECTIVE_FOUND
)
4846 void pp_extra_stdmac(macros_t
*macros
)
4848 extrastdmac
= macros
;
4851 static void make_tok_num(Token
* tok
, int64_t val
)
4854 snprintf(numbuf
, sizeof(numbuf
), "%"PRId64
"", val
);
4855 tok
->text
= nasm_strdup(numbuf
);
4856 tok
->type
= TOK_NUMBER
;