1 /* preproc.c macro preprocessor for the Netwide Assembler
3 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4 * Julian Hall. All rights reserved. The software is
5 * redistributable under the licence given in the file "Licence"
6 * distributed in the NASM archive.
8 * initial version 18/iii/97 by Simon Tatham
11 /* Typical flow of text through preproc
13 * pp_getline gets tokenised lines, either
15 * from a macro expansion
19 * read_line gets raw text from stdmacpos, or predef, or current input file
20 * tokenise converts to tokens
23 * expand_mmac_params is used to expand %1 etc., unless a macro is being
24 * defined or a false conditional is being processed
25 * (%0, %1, %+1, %-1, %%foo
27 * do_directive checks for directives
29 * expand_smacro is used to expand single line macros
31 * expand_mmacro is used to expand multi-line macros
33 * detoken is used to convert the line back to text
47 typedef struct SMacro SMacro
;
48 typedef struct MMacro MMacro
;
49 typedef struct Context Context
;
50 typedef struct Token Token
;
51 typedef struct Line Line
;
52 typedef struct Include Include
;
53 typedef struct Cond Cond
;
54 typedef struct IncPath IncPath
;
57 * Store the definition of a single-line macro.
69 * Store the definition of a multi-line macro. This is also used to
70 * store the interiors of `%rep...%endrep' blocks, which are
71 * effectively self-re-invoking multi-line macros which simply
72 * don't have a name or bother to appear in the hash tables. %rep
73 * blocks are signified by having a NULL `name' field.
75 * In a MMacro describing a `%rep' block, the `in_progress' field
76 * isn't merely boolean, but gives the number of repeats left to
79 * The `next' field is used for storing MMacros in hash tables; the
80 * `next_active' field is for stacking them on istk entries.
82 * When a MMacro is being expanded, `params', `iline', `nparam',
83 * `paramlen', `rotate' and `unique' are local to the invocation.
89 int nparam_min
, nparam_max
;
90 int plus
; /* is the last parameter greedy? */
91 int nolist
; /* is this macro listing-inhibited? */
93 Token
*dlist
; /* All defaults as one list */
94 Token
**defaults
; /* Parameter default pointers */
95 int ndefs
; /* number of default parameters */
99 MMacro
*rep_nest
; /* used for nesting %rep */
100 Token
**params
; /* actual parameters */
101 Token
*iline
; /* invocation line */
102 int nparam
, rotate
, *paramlen
;
103 unsigned long unique
;
104 int lineno
; /* Current line number on expansion */
108 * The context stack is composed of a linked list of these.
114 unsigned long number
;
118 * This is the internal form which we break input lines up into.
119 * Typically stored in linked lists.
121 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
122 * necessarily used as-is, but is intended to denote the number of
123 * the substituted parameter. So in the definition
125 * %define a(x,y) ( (x) & ~(y) )
127 * the token representing `x' will have its type changed to
128 * TOK_SMAC_PARAM, but the one representing `y' will be
131 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
132 * which doesn't need quotes around it. Used in the pre-include
133 * mechanism as an alternative to trying to find a sensible type of
134 * quote to use on the filename we were passed.
139 SMacro
*mac
; /* associated macro for TOK_SMAC_END */
143 TOK_WHITESPACE
= 1, TOK_COMMENT
, TOK_ID
, TOK_PREPROC_ID
, TOK_STRING
,
144 TOK_NUMBER
, TOK_SMAC_END
, TOK_OTHER
, TOK_SMAC_PARAM
,
149 * Multi-line macro definitions are stored as a linked list of
150 * these, which is essentially a container to allow several linked
153 * Note that in this module, linked lists are treated as stacks
154 * wherever possible. For this reason, Lines are _pushed_ on to the
155 * `expansion' field in MMacro structures, so that the linked list,
156 * if walked, would give the macro lines in reverse order; this
157 * means that we can walk the list when expanding a macro, and thus
158 * push the lines on to the `expansion' field in _istk_ in reverse
159 * order (so that when popped back off they are in the right
160 * order). It may seem cockeyed, and it relies on my design having
161 * an even number of steps in, but it works...
163 * Some of these structures, rather than being actual lines, are
164 * markers delimiting the end of the expansion of a given macro.
165 * This is for use in the cycle-tracking and %rep-handling code.
166 * Such structures have `finishes' non-NULL, and `first' NULL. All
167 * others have `finishes' NULL, but `first' may still be NULL if
177 * To handle an arbitrary level of file inclusion, we maintain a
178 * stack (ie linked list) of these things.
187 MMacro
*mstk
; /* stack of active macros/reps */
191 * Include search path. This is simply a list of strings which get
192 * prepended, in turn, to the name of an include file, in an
193 * attempt to find the file if it's not in the current directory.
201 * Conditional assembly: we maintain a separate stack of these for
202 * each level of file inclusion. (The only reason we keep the
203 * stacks separate is to ensure that a stray `%endif' in a file
204 * included from within the true branch of a `%if' won't terminate
205 * it and cause confusion: instead, rightly, it'll cause an error.)
213 * These states are for use just after %if or %elif: IF_TRUE
214 * means the condition has evaluated to truth so we are
215 * currently emitting, whereas IF_FALSE means we are not
216 * currently emitting but will start doing so if a %else comes
217 * up. In these states, all directives are admissible: %elif,
218 * %else and %endif. (And of course %if.)
220 COND_IF_TRUE
, COND_IF_FALSE
,
222 * These states come up after a %else: ELSE_TRUE means we're
223 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
224 * any %elif or %else will cause an error.
226 COND_ELSE_TRUE
, COND_ELSE_FALSE
,
228 * This state means that we're not emitting now, and also that
229 * nothing until %endif will be emitted at all. It's for use in
230 * two circumstances: (i) when we've had our moment of emission
231 * and have now started seeing %elifs, and (ii) when the
232 * condition construct in question is contained within a
233 * non-emitting branch of a larger condition construct.
237 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
240 * Condition codes. Note that we use c_ prefix not C_ because C_ is
241 * used in nasm.h for the "real" condition codes. At _this_ level,
242 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
243 * ones, so we need a different enum...
245 static char *conditions
[] = {
246 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
247 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
248 "np", "ns", "nz", "o", "p", "pe", "po", "s", "z"
251 c_A
, c_AE
, c_B
, c_BE
, c_C
, c_CXZ
, c_E
, c_ECXZ
, c_G
, c_GE
, c_L
, c_LE
,
252 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, c_NE
, c_NG
, c_NGE
, c_NL
, c_NLE
, c_NO
,
253 c_NP
, c_NS
, c_NZ
, c_O
, c_P
, c_PE
, c_PO
, c_S
, c_Z
255 static int inverse_ccs
[] = {
256 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, -1, c_NE
, -1, c_NG
, c_NGE
, c_NL
, c_NLE
,
257 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
,
258 c_Z
, c_NO
, c_NP
, c_PO
, c_PE
, c_NS
, c_NZ
264 static char *directives
[] = {
268 "%assign", "%clear", "%define", "%elif", "%elifctx", "%elifdef",
269 "%elifid", "%elifidn", "%elifidni", "%elifnctx", "%elifndef",
270 "%elifnid", "%elifnidn", "%elifnidni", "%elifnnum", "%elifnstr",
271 "%elifnum", "%elifstr", "%else", "%endif", "%endm", "%endmacro",
272 "%endrep", "%error", "%exitrep", "%iassign", "%idefine", "%if",
273 "%ifctx", "%ifdef", "%ifid", "%ifidn", "%ifidni", "%ifnctx",
274 "%ifndef", "%ifnid", "%ifnidn", "%ifnidni", "%ifnnum",
275 "%ifnstr", "%ifnum", "%ifstr", "%imacro", "%include",
276 "%ixdefine", "%line",
280 "%macro", "%pop", "%push", "%rep", "%repl", "%rotate",
284 "%strlen", "%substr", "%undef", "%xdefine"
290 PP_ASSIGN
, PP_CLEAR
, PP_DEFINE
, PP_ELIF
, PP_ELIFCTX
, PP_ELIFDEF
,
291 PP_ELIFID
, PP_ELIFIDN
, PP_ELIFIDNI
, PP_ELIFNCTX
, PP_ELIFNDEF
,
292 PP_ELIFNID
, PP_ELIFNIDN
, PP_ELIFNIDNI
, PP_ELIFNNUM
, PP_ELIFNSTR
,
293 PP_ELIFNUM
, PP_ELIFSTR
, PP_ELSE
, PP_ENDIF
, PP_ENDM
, PP_ENDMACRO
,
294 PP_ENDREP
, PP_ERROR
, PP_EXITREP
, PP_IASSIGN
, PP_IDEFINE
, PP_IF
,
295 PP_IFCTX
, PP_IFDEF
, PP_IFID
, PP_IFIDN
, PP_IFIDNI
, PP_IFNCTX
,
296 PP_IFNDEF
, PP_IFNID
, PP_IFNIDN
, PP_IFNIDNI
, PP_IFNNUM
,
297 PP_IFNSTR
, PP_IFNUM
, PP_IFSTR
, PP_IMACRO
, PP_INCLUDE
,
298 PP_IXDEFINE
, PP_LINE
,
302 PP_MACRO
, PP_POP
, PP_PUSH
, PP_REP
, PP_REPL
, PP_ROTATE
,
306 PP_STRLEN
, PP_SUBSTR
, PP_UNDEF
, PP_XDEFINE
311 /* For TASM compatibility we need to be able to recognise TASM compatible
312 * conditional compilation directives. Using the NASM pre-processor does
313 * not work, so we look for them specifically from the following list and
314 * then jam in the equivalent NASM directive into the input stream.
318 # define MAX(a,b) ( ((a) > (b)) ? (a) : (b))
322 TM_ARG
, TM_ELIF
, TM_ELSE
, TM_ENDIF
, TM_IF
, TM_IFDEF
, TM_IFDIFI
,
323 TM_IFNDEF
, TM_INCLUDE
, TM_LOCAL
326 static char *tasm_directives
[] = {
327 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
328 "ifndef", "include", "local"
331 static int StackSize
= 4;
332 static char *StackPointer
= "ebp";
333 static int ArgOffset
= 8;
334 static int LocalOffset
= 4;
338 static Context
*cstk
;
339 static Include
*istk
;
340 static IncPath
*ipath
= NULL
;
342 static efunc __error
; /* Pointer to client-provided error reporting function */
343 static evalfunc evaluate
;
345 static int pass
; /* HACK: pass 0 = generate dependencies only */
347 static unsigned long unique
; /* unique identifier numbers */
349 static Line
*predef
= NULL
;
351 static ListGen
*list
;
354 * The number of hash values we use for the macro lookup tables.
355 * FIXME: We should *really* be able to configure this at run time,
356 * or even have the hash table automatically expanding when necessary.
361 * The current set of multi-line macros we have defined.
363 static MMacro
*mmacros
[NHASH
];
366 * The current set of single-line macros we have defined.
368 static SMacro
*smacros
[NHASH
];
371 * The multi-line macro we are currently defining, or the %rep
372 * block we are currently reading, if any.
374 static MMacro
*defining
;
377 * The number of macro parameters to allocate space for at a time.
379 #define PARAM_DELTA 16
382 * The standard macro set: defined as `static char *stdmac[]'. Also
383 * gives our position in the macro set, when we're processing it.
386 static char **stdmacpos
;
389 * The extra standard macros that come from the object format, if
392 static char **extrastdmac
= NULL
;
396 * Forward declarations.
398 static Token
*expand_mmac_params (Token
*tline
);
399 static Token
*expand_smacro (Token
*tline
);
400 static Token
*expand_id (Token
*tline
);
401 static Context
*get_ctx (char *name
, int all_contexts
);
402 static void make_tok_num(Token
*tok
, long val
);
403 static void error (int severity
, char *fmt
, ...);
406 * Macros for safe checking of token pointers, avoid *(NULL)
408 #define tok_type_(x,t) ((x) && (x)->type == (t))
409 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
410 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
411 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
414 /* Handle TASM specific directives, which do not contain a % in
415 * front of them. We do it here because I could not find any other
416 * place to do it for the moment, and it is a hack (ideally it would
417 * be nice to be able to use the NASM pre-processor to do it).
419 static char *check_tasm_directive(char *line
)
422 char *p
= line
, *oldline
, oldchar
;
424 /* Skip whitespace */
425 while (isspace(*p
) && *p
!= 0)
428 /* Binary search for the directive name */
430 j
= sizeof(tasm_directives
) / sizeof(*tasm_directives
);
432 while (!isspace(p
[len
]) && p
[len
] != 0)
439 m
= nasm_stricmp(p
, tasm_directives
[k
]);
441 /* We have found a directive, so jam a % in front of it
442 * so that NASM will then recognise it as one if it's own.
447 line
= nasm_malloc(len
+ 2);
449 if (k
== TM_IFDIFI
) {
450 /* NASM does not recognise IFDIFI, so we convert it to
451 * %ifdef BOGUS. This is not used in NASM comaptible
452 * code, but does need to parse for the TASM macro
455 strcpy(line
+ 1,"ifdef BOGUS");
457 memcpy(line
+ 1, p
, len
+ 1);
473 * The pre-preprocessing stage... This function translates line
474 * number indications as they emerge from GNU cpp (`# lineno "file"
475 * flags') into NASM preprocessor line number indications (`%line
478 static char *prepreproc(char *line
)
481 char *fname
, *oldline
;
483 if (line
[0] == '#' && line
[1] == ' ') {
486 lineno
= atoi(fname
);
487 fname
+= strspn(fname
, "0123456789 ");
490 fnlen
= strcspn(fname
, "\"");
491 line
= nasm_malloc(20+fnlen
);
492 sprintf(line
, "%%line %d %.*s", lineno
, fnlen
, fname
);
496 if (tasm_compatible_mode
)
497 return check_tasm_directive(line
);
503 * The hash function for macro lookups. Note that due to some
504 * macros having case-insensitive names, the hash function must be
505 * invariant under case changes. We implement this by applying a
506 * perfectly normal hash function to the uppercase of the string.
508 static int hash(char *s
)
513 * Powers of three, mod 31.
515 static const int multipliers
[] = {
516 1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10,
517 30, 28, 22, 4, 12, 5, 15, 14, 11, 2, 6, 18, 23, 7, 21
522 h
+= multipliers
[i
] * (unsigned char) (toupper(*s
));
524 if (++i
>= sizeof(multipliers
)/sizeof(*multipliers
))
532 * Free a linked list of tokens.
534 static void free_tlist (Token
*list
)
546 * Free a linked list of lines.
548 static void free_llist (Line
*list
)
554 free_tlist (l
->first
);
562 static void free_mmacro (MMacro
*m
)
565 free_tlist (m
->dlist
);
566 nasm_free (m
->defaults
);
567 free_llist (m
->expansion
);
572 * Pop the context stack.
574 static void ctx_pop (void)
585 free_tlist (s
->expansion
);
592 #define BUF_DELTA 512
594 * Read a line from the top file in istk, handling multiple CR/LFs
595 * at the end of the line read, and handling spurious ^Zs. Will
596 * return lines from the standard macro set if this has not already
599 static char *read_line (void)
601 char *buffer
, *p
, *q
;
606 char *ret
= nasm_strdup(*stdmacpos
++);
607 if (!*stdmacpos
&& any_extrastdmac
)
609 stdmacpos
= extrastdmac
;
610 any_extrastdmac
= FALSE
;
614 * Nasty hack: here we push the contents of `predef' on
615 * to the top-level expansion stack, since this is the
616 * most convenient way to implement the pre-include and
617 * pre-define features.
622 Token
*head
, **tail
, *t
, *tt
;
624 for (pd
= predef
; pd
; pd
= pd
->next
) {
627 for (t
= pd
->first
; t
; t
= t
->next
) {
628 tt
= *tail
= nasm_malloc(sizeof(Token
));
632 tt
->text
= nasm_strdup(t
->text
);
633 tt
->mac
= t
->mac
; /* always NULL here, in fact */
635 l
= nasm_malloc(sizeof(Line
));
636 l
->next
= istk
->expansion
;
650 buffer
= nasm_malloc(BUF_DELTA
);
653 q
= fgets(p
, bufsize
-(p
-buffer
), istk
->fp
);
657 if (p
> buffer
&& p
[-1] == '\n') {
660 if (p
-buffer
> bufsize
-10) {
661 long offset
= p
-buffer
;
662 bufsize
+= BUF_DELTA
;
663 buffer
= nasm_realloc(buffer
, bufsize
);
664 p
= buffer
+offset
; /* prevent stale-pointer problems */
668 if (!q
&& p
== buffer
) {
673 src_set_linnum(src_get_linnum() + istk
->lineinc
);
676 * Play safe: remove CRs as well as LFs, if any of either are
677 * present at the end of the line.
679 while (--p
>= buffer
&& (*p
== '\n' || *p
== '\r'))
683 * Handle spurious ^Z, which may be inserted into source files
684 * by some file transfer utilities.
686 buffer
[strcspn(buffer
, "\032")] = '\0';
688 list
->line (LIST_READ
, buffer
);
694 * Tokenise a line of text. This is a very simple process since we
695 * don't need to parse the value out of e.g. numeric tokens: we
696 * simply split one string into many.
698 static Token
*tokenise (char *line
)
703 Token
*t
, **tail
= &list
;
709 ((p
[1] == '-' || p
[1] == '+') && isdigit(p
[2])) ||
710 ((p
[1] == '+') && (isspace (p
[2]) || !p
[2]))))
715 } while (isdigit(*p
));
716 type
= TOK_PREPROC_ID
;
718 else if (*p
== '%' && p
[1] == '{') {
720 while (*p
&& *p
!= '}') {
726 type
= TOK_PREPROC_ID
;
728 else if (*p
== '%' && (isidchar(p
[1]) ||
729 ((p
[1] == '!' || p
[1] == '%' || p
[1] == '$') &&
735 } while (isidchar(*p
));
736 type
= TOK_PREPROC_ID
;
738 else if (isidstart(*p
) || (*p
== '$' && isidstart(p
[1]))) {
741 while (*p
&& isidchar(*p
))
744 else if (*p
== '\'' || *p
== '"') {
751 while (*p
&& *p
!= c
)
757 error(ERR_WARNING
, "unterminated string");
760 else if (isnumstart(*p
)) {
766 while (*p
&& isnumchar(*p
))
769 else if (isspace(*p
)) {
770 type
= TOK_WHITESPACE
;
772 while (*p
&& isspace(*p
))
775 * Whitespace just before end-of-line is discarded by
776 * pretending it's a comment; whitespace just before a
777 * comment gets lumped into the comment.
779 if (!*p
|| *p
== ';') {
784 else if (*p
== ';') {
790 * Anything else is an operator of some kind. We check
791 * for all the double-character operators (>>, <<, //,
792 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
793 * else is a single-character operator.
796 if ((p
[0] == '>' && p
[1] == '>') ||
797 (p
[0] == '<' && p
[1] == '<') ||
798 (p
[0] == '/' && p
[1] == '/') ||
799 (p
[0] == '%' && p
[1] == '%') ||
800 (p
[0] == '<' && p
[1] == '=') ||
801 (p
[0] == '>' && p
[1] == '=') ||
802 (p
[0] == '=' && p
[1] == '=') ||
803 (p
[0] == '!' && p
[1] == '=') ||
804 (p
[0] == '<' && p
[1] == '>') ||
805 (p
[0] == '&' && p
[1] == '&') ||
806 (p
[0] == '|' && p
[1] == '|') ||
807 (p
[0] == '^' && p
[1] == '^'))
813 if (type
!= TOK_COMMENT
) {
814 *tail
= t
= nasm_malloc (sizeof(Token
));
818 t
->text
= nasm_malloc(1+p
-line
);
819 strncpy(t
->text
, line
, p
-line
);
820 t
->text
[p
-line
] = '\0';
829 * Convert a line of tokens back into text.
830 * If expand_locals is not zero, identifiers of the form "%$*xxx"
831 * will be transformed into ..@ctxnum.xxx
833 static char *detoken (Token
*tlist
, int expand_locals
)
840 for (t
= tlist
; t
; t
= t
->next
) {
841 if (t
->type
== TOK_PREPROC_ID
&& t
->text
[1] == '!') {
842 char *p
= getenv(t
->text
+2);
845 t
->text
= nasm_strdup(p
);
849 /* Expand local macros here and not during preprocessing */
851 t
->type
== TOK_PREPROC_ID
&& t
->text
&&
852 t
->text
[0] == '%' && t
->text
[1] == '$') {
853 Context
*ctx
= get_ctx (t
->text
, FALSE
);
856 char *p
, *q
= t
->text
+ 2;
858 q
+= strspn (q
, "$");
859 sprintf (buffer
, "..@%lu.", ctx
->number
);
860 p
= nasm_malloc (strlen(buffer
)+strlen(q
)+1);
868 len
+= strlen(t
->text
);
870 p
= line
= nasm_malloc(len
+1);
871 for (t
= tlist
; t
; t
= t
->next
) {
882 * A scanner, suitable for use by the expression evaluator, which
883 * operates on a line of Tokens. Expects a pointer to a pointer to
884 * the first token in the line to be passed in as its private_data
887 static int ppscan(void *private_data
, struct tokenval
*tokval
)
889 Token
**tlineptr
= private_data
;
894 *tlineptr
= tline
? tline
->next
: NULL
;
895 } while (tline
&& (tline
->type
== TOK_WHITESPACE
||
896 tline
->type
== TOK_COMMENT
));
899 return tokval
->t_type
= TOKEN_EOS
;
901 if (tline
->text
[0] == '$' && !tline
->text
[1])
902 return tokval
->t_type
= TOKEN_HERE
;
903 if (tline
->text
[0] == '$' && tline
->text
[1] == '$' && !tline
->text
[1])
904 return tokval
->t_type
= TOKEN_BASE
;
906 if (tline
->type
== TOK_ID
) {
907 tokval
->t_charptr
= tline
->text
;
908 if (tline
->text
[0] == '$') {
910 return tokval
->t_type
= TOKEN_ID
;
914 * This is the only special case we actually need to worry
915 * about in this restricted context.
917 if (!nasm_stricmp(tline
->text
, "seg"))
918 return tokval
->t_type
= TOKEN_SEG
;
920 return tokval
->t_type
= TOKEN_ID
;
923 if (tline
->type
== TOK_NUMBER
) {
926 tokval
->t_integer
= readnum(tline
->text
, &rn_error
);
928 return tokval
->t_type
= TOKEN_ERRNUM
;
929 tokval
->t_charptr
= NULL
;
930 return tokval
->t_type
= TOKEN_NUM
;
933 if (tline
->type
== TOK_STRING
) {
942 if (l
== 0 || r
[l
-1] != q
)
943 return tokval
->t_type
= TOKEN_ERRNUM
;
944 tokval
->t_integer
= readstrnum(r
, l
-1, &rn_warn
);
946 error(ERR_WARNING
|ERR_PASS1
,
947 "character constant too long");
948 tokval
->t_charptr
= NULL
;
949 return tokval
->t_type
= TOKEN_NUM
;
952 if (tline
->type
== TOK_OTHER
) {
953 if (!strcmp(tline
->text
, "<<")) return tokval
->t_type
= TOKEN_SHL
;
954 if (!strcmp(tline
->text
, ">>")) return tokval
->t_type
= TOKEN_SHR
;
955 if (!strcmp(tline
->text
, "//")) return tokval
->t_type
= TOKEN_SDIV
;
956 if (!strcmp(tline
->text
, "%%")) return tokval
->t_type
= TOKEN_SMOD
;
957 if (!strcmp(tline
->text
, "==")) return tokval
->t_type
= TOKEN_EQ
;
958 if (!strcmp(tline
->text
, "<>")) return tokval
->t_type
= TOKEN_NE
;
959 if (!strcmp(tline
->text
, "!=")) return tokval
->t_type
= TOKEN_NE
;
960 if (!strcmp(tline
->text
, "<=")) return tokval
->t_type
= TOKEN_LE
;
961 if (!strcmp(tline
->text
, ">=")) return tokval
->t_type
= TOKEN_GE
;
962 if (!strcmp(tline
->text
, "&&")) return tokval
->t_type
= TOKEN_DBL_AND
;
963 if (!strcmp(tline
->text
, "^^")) return tokval
->t_type
= TOKEN_DBL_XOR
;
964 if (!strcmp(tline
->text
, "||")) return tokval
->t_type
= TOKEN_DBL_OR
;
968 * We have no other options: just return the first character of
971 return tokval
->t_type
= tline
->text
[0];
975 * Compare a string to the name of an existing macro; this is a
976 * simple wrapper which calls either strcmp or nasm_stricmp
977 * depending on the value of the `casesense' parameter.
979 static int mstrcmp(char *p
, char *q
, int casesense
)
981 return casesense
? strcmp(p
,q
) : nasm_stricmp(p
,q
);
985 * Return the Context structure associated with a %$ token. Return
986 * NULL, having _already_ reported an error condition, if the
987 * context stack isn't deep enough for the supplied number of $
989 * If all_contexts == TRUE, contexts that enclose current are
990 * also scanned for such smacro, until it is found; if not -
991 * only the context that directly results from the number of $'s
992 * in variable's name.
994 static Context
*get_ctx (char *name
, int all_contexts
)
1000 if (!name
|| name
[0] != '%' || name
[1] != '$')
1004 error (ERR_NONFATAL
, "`%s': context stack is empty", name
);
1008 for (i
= strspn (name
+2, "$"), ctx
= cstk
; (i
> 0) && ctx
; i
--) {
1013 error (ERR_NONFATAL
, "`%s': context stack is only"
1014 " %d level%s deep", name
, i
-1, (i
==2 ? "" : "s"));
1021 /* Search for this smacro in found context */
1024 if (!mstrcmp(m
->name
, name
, m
->casesense
))
1034 /* Add a slash to the end of a path if it is missing. We use the
1035 * forward slash to make it compatible with Unix systems.
1037 static void backslash(char *s
)
1039 int pos
= strlen(s
);
1040 if (s
[pos
-1] != '\\' && s
[pos
-1] != '/') {
1048 * Open an include file. This routine must always return a valid
1049 * file pointer if it returns - it's responsible for throwing an
1050 * ERR_FATAL and bombing out completely if not. It should also try
1051 * the include path one by one until it finds the file or reaches
1052 * the end of the path.
1054 static FILE *inc_fopen(char *file
)
1057 char *prefix
= "", *combine
;
1058 IncPath
*ip
= ipath
;
1059 static int namelen
= 0;
1061 int len
= strlen(file
);
1066 combine
= nasm_malloc(strlen(prefix
)+1+len
+1);
1067 strcpy(combine
, prefix
);
1070 strcat(combine
, file
);
1072 combine
= nasm_strcat(prefix
,file
);
1074 fp
= fopen(combine
, "r");
1075 if (pass
== 0 && fp
)
1077 namelen
+= strlen(combine
) + 1;
1083 printf(" %s", combine
);
1085 nasm_free (combine
);
1095 "unable to open include file `%s'", file
);
1096 return NULL
; /* never reached - placate compilers */
1100 * Determine if we should warn on defining a single-line macro of
1101 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1102 * return TRUE if _any_ single-line macro of that name is defined.
1103 * Otherwise, will return TRUE if a single-line macro with either
1104 * `nparam' or no parameters is defined.
1106 * If a macro with precisely the right number of parameters is
1107 * defined, or nparam is -1, the address of the definition structure
1108 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1109 * is NULL, no action will be taken regarding its contents, and no
1112 * Note that this is also called with nparam zero to resolve
1115 * If you already know which context macro belongs to, you can pass
1116 * the context pointer as first parameter; if you won't but name begins
1117 * with %$ the context will be automatically computed. If all_contexts
1118 * is true, macro will be searched in outer contexts as well.
1120 static int smacro_defined (Context
*ctx
, char *name
, int nparam
, SMacro
**defn
,
1127 else if (name
[0] == '%' && name
[1] == '$') {
1129 ctx
= get_ctx (name
, FALSE
);
1131 return FALSE
; /* got to return _something_ */
1134 m
= smacros
[hash(name
)];
1137 if (!mstrcmp(m
->name
, name
, m
->casesense
&& nocase
) &&
1138 (nparam
<= 0 || m
->nparam
== 0 || nparam
== m
->nparam
)) {
1140 if (nparam
== m
->nparam
|| nparam
== -1)
1154 * Count and mark off the parameters in a multi-line macro call.
1155 * This is called both from within the multi-line macro expansion
1156 * code, and also to mark off the default parameters when provided
1157 * in a %macro definition line.
1159 static void count_mmac_params (Token
*t
, int *nparam
, Token
***params
)
1161 int paramsize
, brace
;
1163 *nparam
= paramsize
= 0;
1166 if (*nparam
>= paramsize
) {
1167 paramsize
+= PARAM_DELTA
;
1168 *params
= nasm_realloc(*params
, sizeof(**params
) * paramsize
);
1172 if (tok_is_(t
, "{"))
1174 (*params
)[(*nparam
)++] = t
;
1175 while (tok_isnt_(t
, brace
? "}" : ","))
1177 if (t
) { /* got a comma/brace */
1181 * Now we've found the closing brace, look further
1185 if (tok_isnt_(t
, ",")) {
1186 error (ERR_NONFATAL
,
1187 "braces do not enclose all of macro parameter");
1188 while (tok_isnt_(t
, ","))
1192 t
= t
->next
; /* eat the comma */
1199 * Determine whether one of the various `if' conditions is true or
1202 * We must free the tline we get passed.
1204 static int if_condition (Token
*tline
, int i
)
1207 Token
* t
, * tt
, ** tptr
, * origline
;
1208 struct tokenval tokval
;
1214 case PP_IFCTX
: case PP_ELIFCTX
:
1215 case PP_IFNCTX
: case PP_ELIFNCTX
:
1216 j
= FALSE
; /* have we matched yet? */
1217 while (cstk
&& tline
) {
1219 if (!tline
|| tline
->type
!= TOK_ID
) {
1221 "`%s' expects context identifiers", directives
[i
]);
1222 free_tlist (origline
);
1225 if (!nasm_stricmp(tline
->text
, cstk
->name
))
1227 tline
= tline
->next
;
1229 if (i
== PP_IFNCTX
|| i
== PP_ELIFNCTX
)
1231 free_tlist (origline
);
1234 case PP_IFDEF
: case PP_ELIFDEF
:
1235 case PP_IFNDEF
: case PP_ELIFNDEF
:
1236 j
= FALSE
; /* have we matched yet? */
1239 if (!tline
|| (tline
->type
!= TOK_ID
&&
1240 (tline
->type
!= TOK_PREPROC_ID
||
1241 tline
->text
[1] != '$'))) {
1243 "`%%if%sdef' expects macro identifiers",
1244 (i
==PP_ELIFNDEF
? "n" : ""));
1245 free_tlist (origline
);
1248 if (smacro_defined (NULL
, tline
->text
, 0, NULL
, 1))
1250 tline
= tline
->next
;
1252 if (i
== PP_IFNDEF
|| i
== PP_ELIFNDEF
)
1254 free_tlist (origline
);
1257 case PP_IFIDN
: case PP_ELIFIDN
: case PP_IFNIDN
: case PP_ELIFNIDN
:
1258 case PP_IFIDNI
: case PP_ELIFIDNI
: case PP_IFNIDNI
: case PP_ELIFNIDNI
:
1259 tline
= expand_smacro(tline
);
1261 while (tok_isnt_(tt
, ","))
1264 error(ERR_NONFATAL
, "`%s' expects two comma-separated arguments",
1270 casesense
= (i
== PP_IFIDN
|| i
== PP_ELIFIDN
||
1271 i
== PP_IFNIDN
|| i
== PP_ELIFNIDN
);
1272 j
= TRUE
; /* assume equality unless proved not */
1273 while ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) && tt
) {
1274 if (tt
->type
== TOK_OTHER
&& !strcmp(tt
->text
, ",")) {
1275 error(ERR_NONFATAL
, "`%s': more than one comma on line",
1280 if (t
->type
== TOK_WHITESPACE
) {
1283 } else if (tt
->type
== TOK_WHITESPACE
) {
1286 } else if (tt
->type
!= t
->type
||
1287 (casesense
? strcmp(tt
->text
, t
->text
) :
1288 nasm_stricmp(tt
->text
, t
->text
))) {
1289 j
= FALSE
; /* found mismatching tokens */
1297 if ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) || tt
)
1298 j
= FALSE
; /* trailing gunk on one end or other */
1299 if (i
== PP_IFNIDN
|| i
== PP_ELIFNIDN
||
1300 i
== PP_IFNIDNI
|| i
== PP_ELIFNIDNI
)
1305 case PP_IFID
: case PP_ELIFID
: case PP_IFNID
: case PP_ELIFNID
:
1306 case PP_IFNUM
: case PP_ELIFNUM
: case PP_IFNNUM
: case PP_ELIFNNUM
:
1307 case PP_IFSTR
: case PP_ELIFSTR
: case PP_IFNSTR
: case PP_ELIFNSTR
:
1308 tline
= expand_smacro(tline
);
1310 while (tok_type_(t
, TOK_WHITESPACE
))
1312 j
= FALSE
; /* placate optimiser */
1314 case PP_IFID
: case PP_ELIFID
: case PP_IFNID
: case PP_ELIFNID
:
1315 j
= (t
->type
== TOK_ID
);
1317 case PP_IFNUM
: case PP_ELIFNUM
: case PP_IFNNUM
: case PP_ELIFNNUM
:
1318 j
= (t
->type
== TOK_NUMBER
);
1320 case PP_IFSTR
: case PP_ELIFSTR
: case PP_IFNSTR
: case PP_ELIFNSTR
:
1321 j
= (t
->type
== TOK_STRING
);
1324 if (i
== PP_IFNID
|| i
== PP_ELIFNID
||
1325 i
== PP_IFNNUM
|| i
== PP_ELIFNNUM
||
1326 i
== PP_IFNSTR
|| i
== PP_ELIFNSTR
)
1331 case PP_IF
: case PP_ELIF
:
1332 t
= tline
= expand_smacro(tline
);
1334 tokval
.t_type
= TOKEN_INVALID
;
1335 evalresult
= evaluate (ppscan
, tptr
, &tokval
,
1336 NULL
, pass
| CRITICAL
, error
, NULL
);
1342 "trailing garbage after expression ignored");
1343 if (!is_simple(evalresult
)) {
1345 "non-constant value given to `%s'", directives
[i
]);
1348 return reloc_value(evalresult
) != 0;
1352 "preprocessor directive `%s' not yet implemented",
1354 free_tlist (origline
);
1355 return -1; /* yeah, right */
1360 * Expand macros in a string. Used in %error and %include directives.
1361 * First tokenise the string, apply "expand_smacro" and then de-tokenise back.
1362 * The returned variable should ALWAYS be freed after usage.
1364 void expand_macros_in_string (char **p
)
1366 Token
*line
= tokenise (*p
);
1367 line
= expand_smacro (line
);
1368 *p
= detoken (line
, FALSE
);
1372 * Find out if a line contains a preprocessor directive, and deal
1375 * If a directive _is_ found, we are expected to free_tlist() the
1378 * Return values go like this:
1380 * bit 0 is set if a directive was found (so the line gets freed)
1382 static int do_directive (Token
*tline
)
1384 int i
, j
, k
, m
, nparam
, nolist
;
1392 SMacro
*smac
, **smhead
;
1394 Token
*t
, *tt
, *param_start
, *macro_start
, *last
, **tptr
, *origline
;
1396 struct tokenval tokval
;
1398 MMacro
*tmp_defining
; /* Used when manipulating rep_nest */
1403 if (!tok_type_(tline
, TOK_PREPROC_ID
) ||
1404 (tline
->text
[1]=='%' || tline
->text
[1]=='$' || tline
->text
[1]=='!'))
1408 j
= sizeof(directives
)/sizeof(*directives
);
1411 m
= nasm_stricmp(tline
->text
, directives
[k
]);
1423 * If we're in a non-emitting branch of a condition construct,
1424 * or walking to the end of an already terminated %rep block,
1425 * we should ignore all directives except for condition
1428 if (((istk
->conds
&& !emitting(istk
->conds
->state
)) ||
1429 (istk
->mstk
&& !istk
->mstk
->in_progress
)) &&
1430 i
!= PP_IF
&& i
!= PP_ELIF
&&
1431 i
!= PP_IFCTX
&& i
!= PP_ELIFCTX
&&
1432 i
!= PP_IFDEF
&& i
!= PP_ELIFDEF
&&
1433 i
!= PP_IFID
&& i
!= PP_ELIFID
&&
1434 i
!= PP_IFIDN
&& i
!= PP_ELIFIDN
&&
1435 i
!= PP_IFIDNI
&& i
!= PP_ELIFIDNI
&&
1436 i
!= PP_IFNCTX
&& i
!= PP_ELIFNCTX
&&
1437 i
!= PP_IFNDEF
&& i
!= PP_ELIFNDEF
&&
1438 i
!= PP_IFNID
&& i
!= PP_ELIFNID
&&
1439 i
!= PP_IFNIDN
&& i
!= PP_ELIFNIDN
&&
1440 i
!= PP_IFNIDNI
&& i
!= PP_ELIFNIDNI
&&
1441 i
!= PP_IFNNUM
&& i
!= PP_ELIFNNUM
&&
1442 i
!= PP_IFNSTR
&& i
!= PP_ELIFNSTR
&&
1443 i
!= PP_IFNUM
&& i
!= PP_ELIFNUM
&&
1444 i
!= PP_IFSTR
&& i
!= PP_ELIFSTR
&&
1445 i
!= PP_ELSE
&& i
!= PP_ENDIF
)
1451 * If we're defining a macro or reading a %rep block, we should
1452 * ignore all directives except for %macro/%imacro (which
1453 * generate an error), %endm/%endmacro, and (only if we're in a
1454 * %rep block) %endrep. If we're in a %rep block, another %rep
1455 * causes an error, so should be let through.
1457 if (defining
&& i
!= PP_MACRO
&& i
!= PP_IMACRO
&&
1458 i
!= PP_ENDMACRO
&& i
!= PP_ENDM
&&
1459 (defining
->name
|| (i
!= PP_ENDREP
&& i
!= PP_REP
)))
1465 error(ERR_NONFATAL
, "unknown preprocessor directive `%s'",
1467 return 0; /* didn't get it */
1473 /* Directive to tell NASM what the default stack size is. The
1474 * default is for a 16-bit stack, and this can be overriden with
1476 * the following form:
1478 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1480 tline
= tline
->next
;
1481 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1482 tline
= tline
->next
;
1483 if (!tline
|| tline
->type
!= TOK_ID
) {
1484 error (ERR_NONFATAL
,"`%%stacksize' missing size parameter");
1485 free_tlist (origline
);
1488 if (nasm_stricmp(tline
->text
,"flat") == 0) {
1489 /* All subsequent ARG directives are for a 32-bit stack */
1491 StackPointer
= "ebp";
1494 } else if (nasm_stricmp(tline
->text
,"large") == 0) {
1495 /* All subsequent ARG directives are for a 16-bit stack,
1496 * far function call.
1499 StackPointer
= "bp";
1502 } else if (nasm_stricmp(tline
->text
,"small") == 0) {
1503 /* All subsequent ARG directives are for a 16-bit stack,
1504 * far function call. We don't support near functions.
1507 StackPointer
= "bp";
1511 error (ERR_NONFATAL
,"`%%stacksize' invalid size type");
1512 free_tlist (origline
);
1515 free_tlist(origline
);
1519 /* TASM like ARG directive to define arguments to functions, in
1520 * the following form:
1522 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1526 char *arg
,directive
[256];
1527 int size
= StackSize
;
1529 /* Find the argument name */
1530 tline
= tline
->next
;
1531 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1532 tline
= tline
->next
;
1533 if (!tline
|| tline
->type
!= TOK_ID
) {
1534 error (ERR_NONFATAL
,"`%%arg' missing argument parameter");
1535 free_tlist (origline
);
1540 /* Find the argument size type */
1541 tline
= tline
->next
;
1542 if (!tline
|| tline
->type
!= TOK_OTHER
|| tline
->text
[0] != ':') {
1543 error (ERR_NONFATAL
,"Syntax error processing `%%arg' directive");
1544 free_tlist (origline
);
1547 tline
= tline
->next
;
1548 if (!tline
|| tline
->type
!= TOK_ID
) {
1549 error (ERR_NONFATAL
,"`%%arg' missing size type parameter");
1550 free_tlist (origline
);
1554 /* Allow macro expansion of type parameter */
1555 tt
= tokenise(tline
->text
);
1556 tt
= expand_smacro(tt
);
1557 if (nasm_stricmp(tt
->text
,"byte") == 0) {
1558 size
= MAX(StackSize
,1);
1559 } else if (nasm_stricmp(tt
->text
,"word") == 0) {
1560 size
= MAX(StackSize
,2);
1561 } else if (nasm_stricmp(tt
->text
,"dword") == 0) {
1562 size
= MAX(StackSize
,4);
1563 } else if (nasm_stricmp(tt
->text
,"qword") == 0) {
1564 size
= MAX(StackSize
,8);
1565 } else if (nasm_stricmp(tt
->text
,"tword") == 0) {
1566 size
= MAX(StackSize
,10);
1568 error (ERR_NONFATAL
,"Invalid size type for `%%arg' missing directive");
1570 free_tlist (origline
);
1575 /* Now define the macro for the argument */
1576 sprintf(directive
,"%%define %s (%s+%d)", arg
, StackPointer
, offset
);
1577 do_directive(tokenise(directive
));
1580 /* Move to the next argument in the list */
1581 tline
= tline
->next
;
1582 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1583 tline
= tline
->next
;
1584 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
1585 free_tlist (origline
);
1589 /* TASM like LOCAL directive to define local variables for a
1590 * function, in the following form:
1592 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
1594 * The '= LocalSize' at the end is ignored by NASM, but is
1595 * required by TASM to define the local parameter size (and used
1596 * by the TASM macro package).
1598 offset
= LocalOffset
;
1600 char *local
,directive
[256];
1601 int size
= StackSize
;
1603 /* Find the argument name */
1604 tline
= tline
->next
;
1605 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1606 tline
= tline
->next
;
1607 if (!tline
|| tline
->type
!= TOK_ID
) {
1608 error (ERR_NONFATAL
,"`%%local' missing argument parameter");
1609 free_tlist (origline
);
1612 local
= tline
->text
;
1614 /* Find the argument size type */
1615 tline
= tline
->next
;
1616 if (!tline
|| tline
->type
!= TOK_OTHER
|| tline
->text
[0] != ':') {
1617 error (ERR_NONFATAL
,"Syntax error processing `%%local' directive");
1618 free_tlist (origline
);
1621 tline
= tline
->next
;
1622 if (!tline
|| tline
->type
!= TOK_ID
) {
1623 error (ERR_NONFATAL
,"`%%local' missing size type parameter");
1624 free_tlist (origline
);
1628 /* Allow macro expansion of type parameter */
1629 tt
= tokenise(tline
->text
);
1630 tt
= expand_smacro(tt
);
1631 if (nasm_stricmp(tt
->text
,"byte") == 0) {
1632 size
= MAX(StackSize
,1);
1633 } else if (nasm_stricmp(tt
->text
,"word") == 0) {
1634 size
= MAX(StackSize
,2);
1635 } else if (nasm_stricmp(tt
->text
,"dword") == 0) {
1636 size
= MAX(StackSize
,4);
1637 } else if (nasm_stricmp(tt
->text
,"qword") == 0) {
1638 size
= MAX(StackSize
,8);
1639 } else if (nasm_stricmp(tt
->text
,"tword") == 0) {
1640 size
= MAX(StackSize
,10);
1642 error (ERR_NONFATAL
,"Invalid size type for `%%local' missing directive");
1644 free_tlist (origline
);
1649 /* Now define the macro for the argument */
1650 sprintf(directive
,"%%define %s (%s-%d)", local
, StackPointer
, offset
);
1651 do_directive(tokenise(directive
));
1654 /* Now define the assign to setup the enter_c macro correctly */
1655 sprintf(directive
,"%%assign %%$localsize %%$localsize+%d", size
);
1656 do_directive(tokenise(directive
));
1658 /* Move to the next argument in the list */
1659 tline
= tline
->next
;
1660 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1661 tline
= tline
->next
;
1662 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
1663 free_tlist (origline
);
1670 "trailing garbage after `%%clear' ignored");
1671 for (j
=0; j
<NHASH
; j
++) {
1672 while (mmacros
[j
]) {
1673 MMacro
*m
= mmacros
[j
];
1674 mmacros
[j
] = m
->next
;
1677 while (smacros
[j
]) {
1678 SMacro
*s
= smacros
[j
];
1679 smacros
[j
] = smacros
[j
]->next
;
1680 nasm_free (s
->name
);
1681 free_tlist (s
->expansion
);
1685 free_tlist (origline
);
1689 tline
= tline
->next
;
1691 if (!tline
|| (tline
->type
!= TOK_STRING
&&
1692 tline
->type
!= TOK_INTERNAL_STRING
))
1694 error(ERR_NONFATAL
, "`%%include' expects a file name");
1695 free_tlist (origline
);
1696 return 3; /* but we did _something_ */
1700 "trailing garbage after `%%include' ignored");
1701 if (tline
->type
!= TOK_INTERNAL_STRING
) {
1702 p
= tline
->text
+1; /* point past the quote to the name */
1703 p
[strlen(p
)-1] = '\0'; /* remove the trailing quote */
1705 p
= tline
->text
; /* internal_string is easier */
1706 expand_macros_in_string (&p
);
1707 inc
= nasm_malloc(sizeof(Include
));
1710 inc
->fp
= inc_fopen(p
);
1711 inc
->fname
= src_set_fname (p
);
1712 inc
->lineno
= src_set_linnum(0);
1714 inc
->expansion
= NULL
;
1717 list
->uplevel (LIST_INCLUDE
);
1718 free_tlist (origline
);
1722 tline
= tline
->next
;
1724 tline
= expand_id (tline
);
1725 if (!tok_type_(tline
, TOK_ID
)) {
1727 "`%%push' expects a context identifier");
1728 free_tlist (origline
);
1729 return 3; /* but we did _something_ */
1733 "trailing garbage after `%%push' ignored");
1734 ctx
= nasm_malloc(sizeof(Context
));
1736 ctx
->localmac
= NULL
;
1737 ctx
->name
= nasm_strdup(tline
->text
);
1738 ctx
->number
= unique
++;
1740 free_tlist (origline
);
1744 tline
= tline
->next
;
1746 tline
= expand_id (tline
);
1747 if (!tok_type_(tline
, TOK_ID
)) {
1749 "`%%repl' expects a context identifier");
1750 free_tlist (origline
);
1751 return 3; /* but we did _something_ */
1755 "trailing garbage after `%%repl' ignored");
1758 "`%%repl': context stack is empty");
1760 nasm_free (cstk
->name
);
1761 cstk
->name
= nasm_strdup(tline
->text
);
1763 free_tlist (origline
);
1769 "trailing garbage after `%%pop' ignored");
1772 "`%%pop': context stack is already empty");
1775 free_tlist (origline
);
1779 tline
->next
= expand_smacro (tline
->next
);
1780 tline
= tline
->next
;
1782 if (tok_type_(tline
, TOK_STRING
)) {
1783 p
= tline
->text
+1; /* point past the quote to the name */
1784 p
[strlen(p
)-1] = '\0'; /* remove the trailing quote */
1785 expand_macros_in_string (&p
);
1786 error (ERR_NONFATAL
, "%s", p
);
1789 p
= detoken(tline
, FALSE
);
1790 error (ERR_WARNING
, "%s", p
);
1793 free_tlist (origline
);
1811 if (istk
->conds
&& !emitting(istk
->conds
->state
))
1814 j
= if_condition(tline
->next
, i
);
1815 tline
->next
= NULL
; /* it got freed */
1816 free_tlist (origline
);
1817 j
= j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
1819 cond
= nasm_malloc(sizeof(Cond
));
1820 cond
->next
= istk
->conds
;
1823 return (j
== COND_IF_TRUE
? 3 : 1);
1841 error(ERR_FATAL
, "`%s': no matching `%%if'",
1843 if (emitting(istk
->conds
->state
) || istk
->conds
->state
== COND_NEVER
)
1844 istk
->conds
->state
= COND_NEVER
;
1846 j
= if_condition(expand_mmac_params(tline
->next
), i
);
1847 tline
->next
= NULL
; /* it got freed */
1848 free_tlist (origline
);
1849 istk
->conds
->state
= j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
1851 return (istk
->conds
->state
== COND_IF_TRUE
? 5 : 1);
1856 "trailing garbage after `%%else' ignored");
1859 "`%%else': no matching `%%if'");
1860 if (emitting(istk
->conds
->state
) || istk
->conds
->state
== COND_NEVER
)
1861 istk
->conds
->state
= COND_ELSE_FALSE
;
1863 istk
->conds
->state
= COND_ELSE_TRUE
;
1864 free_tlist (origline
);
1870 "trailing garbage after `%%endif' ignored");
1873 "`%%endif': no matching `%%if'");
1875 istk
->conds
= cond
->next
;
1877 free_tlist (origline
);
1884 "`%%%smacro': already defining a macro",
1885 (i
== PP_IMACRO
? "i" : ""));
1886 tline
= tline
->next
;
1888 tline
= expand_id (tline
);
1889 if (!tok_type_(tline
, TOK_ID
)) {
1890 error (ERR_NONFATAL
,
1891 "`%%%smacro' expects a macro name",
1892 (i
== PP_IMACRO
? "i" : ""));
1895 defining
= nasm_malloc(sizeof(MMacro
));
1896 defining
->name
= nasm_strdup(tline
->text
);
1897 defining
->casesense
= (i
== PP_MACRO
);
1898 defining
->plus
= FALSE
;
1899 defining
->nolist
= FALSE
;
1900 defining
->in_progress
= FALSE
;
1901 defining
->rep_nest
= NULL
;
1902 tline
= expand_smacro (tline
->next
);
1904 if (!tok_type_(tline
, TOK_NUMBER
)) {
1905 error (ERR_NONFATAL
,
1906 "`%%%smacro' expects a parameter count",
1907 (i
== PP_IMACRO
? "i" : ""));
1908 defining
->nparam_min
= defining
->nparam_max
= 0;
1910 defining
->nparam_min
= defining
->nparam_max
=
1911 readnum(tline
->text
, &j
);
1913 error (ERR_NONFATAL
,
1914 "unable to parse parameter count `%s'", tline
->text
);
1916 if (tline
&& tok_is_(tline
->next
, "-")) {
1917 tline
= tline
->next
->next
;
1918 if (tok_is_(tline
, "*"))
1919 defining
->nparam_max
= INT_MAX
;
1920 else if (!tok_type_(tline
, TOK_NUMBER
))
1921 error (ERR_NONFATAL
,
1922 "`%%%smacro' expects a parameter count after `-'",
1923 (i
== PP_IMACRO
? "i" : ""));
1925 defining
->nparam_max
= readnum(tline
->text
, &j
);
1927 error (ERR_NONFATAL
,
1928 "unable to parse parameter count `%s'",
1930 if (defining
->nparam_min
> defining
->nparam_max
)
1931 error (ERR_NONFATAL
,
1932 "minimum parameter count exceeds maximum");
1935 if (tline
&& tok_is_(tline
->next
, "+")) {
1936 tline
= tline
->next
;
1937 defining
->plus
= TRUE
;
1939 if (tline
&& tok_type_(tline
->next
, TOK_ID
) &&
1940 !nasm_stricmp(tline
->next
->text
, ".nolist"))
1942 tline
= tline
->next
;
1943 defining
->nolist
= TRUE
;
1945 mmac
= mmacros
[hash(defining
->name
)];
1947 if (!strcmp(mmac
->name
, defining
->name
) &&
1948 (mmac
->nparam_min
<=defining
->nparam_max
|| defining
->plus
) &&
1949 (defining
->nparam_min
<=mmac
->nparam_max
|| mmac
->plus
))
1952 "redefining multi-line macro `%s'", defining
->name
);
1958 * Handle default parameters.
1960 if (tline
&& tline
->next
) {
1961 defining
->dlist
= tline
->next
;
1963 count_mmac_params (defining
->dlist
, &defining
->ndefs
,
1964 &defining
->defaults
);
1966 defining
->dlist
= NULL
;
1967 defining
->defaults
= NULL
;
1969 defining
->expansion
= NULL
;
1970 free_tlist (origline
);
1976 error (ERR_NONFATAL
, "`%s': not defining a macro",
1980 k
= hash(defining
->name
);
1981 defining
->next
= mmacros
[k
];
1982 mmacros
[k
] = defining
;
1984 free_tlist (origline
);
1988 if (tline
->next
&& tline
->next
->type
== TOK_WHITESPACE
)
1989 tline
= tline
->next
;
1990 t
= expand_smacro(tline
->next
);
1992 free_tlist (origline
);
1995 tokval
.t_type
= TOKEN_INVALID
;
1996 evalresult
= evaluate (ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2002 "trailing garbage after expression ignored");
2003 if (!is_simple(evalresult
)) {
2005 "non-constant value given to `%%rotate'");
2009 while (mmac
&& !mmac
->name
) /* avoid mistaking %reps for macros */
2010 mmac
= mmac
->next_active
;
2012 error(ERR_NONFATAL
, "`%%rotate' invoked outside a macro call");
2013 mmac
->rotate
= mmac
->rotate
+ reloc_value(evalresult
);
2014 if (mmac
->rotate
< 0)
2015 mmac
->rotate
= mmac
->nparam
- (-mmac
->rotate
) % mmac
->nparam
;
2016 mmac
->rotate
%= mmac
->nparam
;
2021 tline
= tline
->next
;
2022 if (tline
->next
&& tline
->next
->type
== TOK_WHITESPACE
)
2023 tline
= tline
->next
;
2024 if (tline
->next
&& tline
->next
->type
== TOK_ID
&&
2025 !nasm_stricmp(tline
->next
->text
, ".nolist")) {
2026 tline
= tline
->next
;
2029 t
= expand_smacro(tline
->next
);
2031 free_tlist (origline
);
2034 tokval
.t_type
= TOKEN_INVALID
;
2035 evalresult
= evaluate (ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2041 "trailing garbage after expression ignored");
2042 if (!is_simple(evalresult
)) {
2044 "non-constant value given to `%%rep'");
2047 tmp_defining
= defining
;
2048 defining
= nasm_malloc(sizeof(MMacro
));
2049 defining
->name
= NULL
; /* flags this macro as a %rep block */
2050 defining
->casesense
= 0;
2051 defining
->plus
= FALSE
;
2052 defining
->nolist
= nolist
;
2053 defining
->in_progress
= reloc_value(evalresult
) + 1;
2054 defining
->nparam_min
= defining
->nparam_max
= 0;
2055 defining
->defaults
= NULL
;
2056 defining
->dlist
= NULL
;
2057 defining
->expansion
= NULL
;
2058 defining
->next_active
= istk
->mstk
;
2059 defining
->rep_nest
= tmp_defining
;
2063 if (!defining
|| defining
->name
) {
2064 error (ERR_NONFATAL
,
2065 "`%%endrep': no matching `%%rep'");
2070 * Now we have a "macro" defined - although it has no name
2071 * and we won't be entering it in the hash tables - we must
2072 * push a macro-end marker for it on to istk->expansion.
2073 * After that, it will take care of propagating itself (a
2074 * macro-end marker line for a macro which is really a %rep
2075 * block will cause the macro to be re-expanded, complete
2076 * with another macro-end marker to ensure the process
2077 * continues) until the whole expansion is forcibly removed
2078 * from istk->expansion by a %exitrep.
2080 l
= nasm_malloc(sizeof(Line
));
2081 l
->next
= istk
->expansion
;
2082 l
->finishes
= defining
;
2084 istk
->expansion
= l
;
2086 istk
->mstk
= defining
;
2088 list
->uplevel (defining
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
2089 tmp_defining
= defining
;
2090 defining
= defining
->rep_nest
;
2091 free_tlist (origline
);
2096 * We must search along istk->expansion until we hit a
2097 * macro-end marker for a macro with no name. Then we set
2098 * its `in_progress' flag to 0.
2100 for (l
= istk
->expansion
; l
; l
= l
->next
)
2101 if (l
->finishes
&& !l
->finishes
->name
)
2105 l
->finishes
->in_progress
= 0;
2107 error (ERR_NONFATAL
, "`%%exitrep' not within `%%rep' block");
2108 free_tlist (origline
);
2115 tline
= tline
->next
;
2117 tline
= expand_id (tline
);
2118 if (!tline
|| (tline
->type
!= TOK_ID
&&
2119 (tline
->type
!= TOK_PREPROC_ID
||
2120 tline
->text
[1] != '$'))) {
2121 error (ERR_NONFATAL
,
2122 "`%%%s%sdefine' expects a macro identifier",
2123 ((i
== PP_IDEFINE
|| i
== PP_IXDEFINE
) ? "i" : ""),
2124 ((i
== PP_XDEFINE
|| i
== PP_IXDEFINE
) ? "x" : ""));
2125 free_tlist (origline
);
2129 ctx
= get_ctx (tline
->text
, FALSE
);
2131 smhead
= &smacros
[hash(tline
->text
)];
2133 smhead
= &ctx
->localmac
;
2134 mname
= tline
->text
;
2136 param_start
= tline
= tline
->next
;
2139 /* Expand the macro definition now for %xdefine and %ixdefine */
2140 if ((i
== PP_XDEFINE
) || (i
== PP_IXDEFINE
))
2141 tline
= expand_smacro (tline
);
2143 if (tok_is_(tline
, "(")) {
2145 * This macro has parameters.
2148 tline
= tline
->next
;
2152 error (ERR_NONFATAL
,
2153 "parameter identifier expected");
2154 free_tlist (origline
);
2157 if (tline
->type
!= TOK_ID
) {
2158 error (ERR_NONFATAL
,
2159 "`%s': parameter identifier expected",
2161 free_tlist (origline
);
2164 tline
->type
= TOK_SMAC_PARAM
+ nparam
++;
2165 tline
= tline
->next
;
2167 if (tok_is_(tline
, ",")) {
2168 tline
= tline
->next
;
2171 if (!tok_is_(tline
, ")")) {
2172 error (ERR_NONFATAL
,
2173 "`)' expected to terminate macro template");
2174 free_tlist (origline
);
2180 tline
= tline
->next
;
2182 if (tok_type_(tline
, TOK_WHITESPACE
))
2183 last
= tline
, tline
= tline
->next
;
2188 if (t
->type
== TOK_ID
) {
2189 for (tt
= param_start
; tt
; tt
= tt
->next
)
2190 if (tt
->type
>= TOK_SMAC_PARAM
&&
2191 !strcmp(tt
->text
, t
->text
))
2195 t
->next
= macro_start
;
2200 * Good. We now have a macro name, a parameter count, and a
2201 * token list (in reverse order) for an expansion. We ought
2202 * to be OK just to create an SMacro, store it, and let
2203 * free_tlist have the rest of the line (which we have
2204 * carefully re-terminated after chopping off the expansion
2207 if (smacro_defined (ctx
, mname
, nparam
, &smac
, i
== PP_DEFINE
)) {
2210 "single-line macro `%s' defined both with and"
2211 " without parameters", mname
);
2212 free_tlist (origline
);
2213 free_tlist (macro_start
);
2217 * We're redefining, so we have to take over an
2218 * existing SMacro structure. This means freeing
2219 * what was already in it.
2221 nasm_free (smac
->name
);
2222 free_tlist (smac
->expansion
);
2225 smac
= nasm_malloc(sizeof(SMacro
));
2226 smac
->next
= *smhead
;
2229 smac
->name
= nasm_strdup(mname
);
2230 smac
->casesense
= ((i
== PP_DEFINE
) || (i
== PP_XDEFINE
));
2231 smac
->nparam
= nparam
;
2232 smac
->expansion
= macro_start
;
2233 smac
->in_progress
= FALSE
;
2234 free_tlist (origline
);
2238 tline
= tline
->next
;
2240 tline
= expand_id (tline
);
2241 if (!tline
|| (tline
->type
!= TOK_ID
&&
2242 (tline
->type
!= TOK_PREPROC_ID
||
2243 tline
->text
[1] != '$'))) {
2244 error (ERR_NONFATAL
,
2245 "`%%undef' expects a macro identifier");
2246 free_tlist (origline
);
2251 "trailing garbage after macro name ignored");
2254 /* Find the context that symbol belongs to */
2255 ctx
= get_ctx (tline
->text
, FALSE
);
2257 smhead
= &smacros
[hash(tline
->text
)];
2259 smhead
= &ctx
->localmac
;
2261 mname
= tline
->text
;
2266 * We now have a macro name... go hunt for it.
2268 while (smacro_defined (ctx
, mname
, -1, &smac
, 1)) {
2269 /* Defined, so we need to find its predecessor and nuke it */
2271 for ( s
= smhead
; *s
&& *s
!= smac
; s
= &(*s
)->next
);
2274 nasm_free(smac
->name
);
2275 free_tlist(smac
->expansion
);
2279 free_tlist (origline
);
2283 tline
= tline
->next
;
2285 tline
= expand_id (tline
);
2286 if (!tline
|| (tline
->type
!= TOK_ID
&&
2287 (tline
->type
!= TOK_PREPROC_ID
||
2288 tline
->text
[1] != '$'))) {
2289 error (ERR_NONFATAL
,
2290 "`%%strlen' expects a macro identifier as first parameter");
2291 free_tlist (origline
);
2294 ctx
= get_ctx (tline
->text
, FALSE
);
2296 smhead
= &smacros
[hash(tline
->text
)];
2298 smhead
= &ctx
->localmac
;
2299 mname
= tline
->text
;
2301 tline
= expand_smacro (tline
->next
);
2305 while (tok_type_(t
, TOK_WHITESPACE
))
2307 /* t should now point to the string */
2308 if (t
->type
!= TOK_STRING
) {
2310 "`%%strlen` requires string as second parameter");
2312 free_tlist(origline
);
2316 macro_start
= nasm_malloc(sizeof(*macro_start
));
2317 macro_start
->next
= NULL
;
2318 make_tok_num(macro_start
, strlen(t
->text
)-2);
2319 macro_start
->mac
= NULL
;
2322 * We now have a macro name, an implicit parameter count of
2323 * zero, and a numeric token to use as an expansion. Create
2324 * and store an SMacro.
2326 if (smacro_defined (ctx
, mname
, 0, &smac
, i
== PP_STRLEN
)) {
2329 "single-line macro `%s' defined both with and"
2330 " without parameters", mname
);
2333 * We're redefining, so we have to take over an
2334 * existing SMacro structure. This means freeing
2335 * what was already in it.
2337 nasm_free (smac
->name
);
2338 free_tlist (smac
->expansion
);
2342 smac
= nasm_malloc(sizeof(SMacro
));
2343 smac
->next
= *smhead
;
2346 smac
->name
= nasm_strdup(mname
);
2347 smac
->casesense
= (i
== PP_STRLEN
);
2349 smac
->expansion
= macro_start
;
2350 smac
->in_progress
= FALSE
;
2352 free_tlist (origline
);
2356 tline
= tline
->next
;
2358 tline
= expand_id (tline
);
2359 if (!tline
|| (tline
->type
!= TOK_ID
&&
2360 (tline
->type
!= TOK_PREPROC_ID
||
2361 tline
->text
[1] != '$'))) {
2362 error (ERR_NONFATAL
,
2363 "`%%substr' expects a macro identifier as first parameter");
2364 free_tlist (origline
);
2367 ctx
= get_ctx (tline
->text
, FALSE
);
2369 smhead
= &smacros
[hash(tline
->text
)];
2371 smhead
= &ctx
->localmac
;
2372 mname
= tline
->text
;
2374 tline
= expand_smacro (tline
->next
);
2378 while (tok_type_(t
, TOK_WHITESPACE
))
2381 /* t should now point to the string */
2382 if (t
->type
!= TOK_STRING
) {
2384 "`%%substr` requires string as second parameter");
2386 free_tlist(origline
);
2392 tokval
.t_type
= TOKEN_INVALID
;
2393 evalresult
= evaluate (ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2396 free_tlist(origline
);
2399 if (!is_simple(evalresult
)) {
2401 "non-constant value given to `%%substr`");
2403 free_tlist(origline
);
2407 macro_start
= nasm_malloc(sizeof(*macro_start
));
2408 macro_start
->next
= NULL
;
2409 macro_start
->text
= nasm_strdup("'''");
2410 if (evalresult
->value
> 0 && evalresult
->value
< strlen(t
->text
)-1) {
2411 macro_start
->text
[1] = t
->text
[evalresult
->value
];
2414 macro_start
->text
[2] = '\0';
2416 macro_start
->type
= TOK_STRING
;
2417 macro_start
->mac
= NULL
;
2420 * We now have a macro name, an implicit parameter count of
2421 * zero, and a numeric token to use as an expansion. Create
2422 * and store an SMacro.
2424 if (smacro_defined (ctx
, mname
, 0, &smac
, i
== PP_SUBSTR
)) {
2427 "single-line macro `%s' defined both with and"
2428 " without parameters", mname
);
2431 * We're redefining, so we have to take over an
2432 * existing SMacro structure. This means freeing
2433 * what was already in it.
2435 nasm_free (smac
->name
);
2436 free_tlist (smac
->expansion
);
2440 smac
= nasm_malloc(sizeof(SMacro
));
2441 smac
->next
= *smhead
;
2444 smac
->name
= nasm_strdup(mname
);
2445 smac
->casesense
= (i
== PP_SUBSTR
);
2447 smac
->expansion
= macro_start
;
2448 smac
->in_progress
= FALSE
;
2450 free_tlist (origline
);
2456 tline
= tline
->next
;
2458 tline
= expand_id (tline
);
2459 if (!tline
|| (tline
->type
!= TOK_ID
&&
2460 (tline
->type
!= TOK_PREPROC_ID
||
2461 tline
->text
[1] != '$'))) {
2462 error (ERR_NONFATAL
,
2463 "`%%%sassign' expects a macro identifier",
2464 (i
== PP_IASSIGN
? "i" : ""));
2465 free_tlist (origline
);
2468 ctx
= get_ctx (tline
->text
, FALSE
);
2470 smhead
= &smacros
[hash(tline
->text
)];
2472 smhead
= &ctx
->localmac
;
2473 mname
= tline
->text
;
2475 tline
= expand_smacro (tline
->next
);
2480 tokval
.t_type
= TOKEN_INVALID
;
2481 evalresult
= evaluate (ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2484 free_tlist (origline
);
2490 "trailing garbage after expression ignored");
2492 if (!is_simple(evalresult
)) {
2494 "non-constant value given to `%%%sassign'",
2495 (i
== PP_IASSIGN
? "i" : ""));
2496 free_tlist (origline
);
2500 macro_start
= nasm_malloc(sizeof(*macro_start
));
2501 macro_start
->next
= NULL
;
2502 make_tok_num(macro_start
, reloc_value(evalresult
));
2503 macro_start
->mac
= NULL
;
2506 * We now have a macro name, an implicit parameter count of
2507 * zero, and a numeric token to use as an expansion. Create
2508 * and store an SMacro.
2510 if (smacro_defined (ctx
, mname
, 0, &smac
, i
== PP_ASSIGN
)) {
2513 "single-line macro `%s' defined both with and"
2514 " without parameters", mname
);
2517 * We're redefining, so we have to take over an
2518 * existing SMacro structure. This means freeing
2519 * what was already in it.
2521 nasm_free (smac
->name
);
2522 free_tlist (smac
->expansion
);
2526 smac
= nasm_malloc(sizeof(SMacro
));
2527 smac
->next
= *smhead
;
2530 smac
->name
= nasm_strdup(mname
);
2531 smac
->casesense
= (i
== PP_ASSIGN
);
2533 smac
->expansion
= macro_start
;
2534 smac
->in_progress
= FALSE
;
2535 free_tlist (origline
);
2540 * Syntax is `%line nnn[+mmm] [filename]'
2542 tline
= tline
->next
;
2544 if (!tok_type_(tline
, TOK_NUMBER
)) {
2545 error (ERR_NONFATAL
, "`%%line' expects line number");
2546 free_tlist (origline
);
2549 k
= readnum(tline
->text
, &j
);
2551 tline
= tline
->next
;
2552 if (tok_is_(tline
, "+")) {
2553 tline
= tline
->next
;
2554 if (!tok_type_(tline
, TOK_NUMBER
)) {
2555 error (ERR_NONFATAL
,
2556 "`%%line' expects line increment");
2557 free_tlist (origline
);
2560 m
= readnum(tline
->text
, &j
);
2561 tline
= tline
->next
;
2567 nasm_free (src_set_fname (detoken (tline
, FALSE
)));
2569 free_tlist (origline
);
2574 "preprocessor directive `%s' not yet implemented",
2582 * Ensure that a macro parameter contains a condition code and
2583 * nothing else. Return the condition code index if so, or -1
2586 static int find_cc (Token
*t
)
2592 if (t
->type
!= TOK_ID
)
2596 if (tt
&& (tt
->type
!= TOK_OTHER
|| strcmp(tt
->text
, ",")))
2600 j
= sizeof(conditions
)/sizeof(*conditions
);
2603 m
= nasm_stricmp(t
->text
, conditions
[k
]);
2619 * Expand MMacro-local things: parameter references (%0, %n, %+n,
2620 * %-n) and MMacro-local identifiers (%%foo).
2622 static Token
*expand_mmac_params (Token
*tline
)
2624 Token
*t
, *tt
, *ttt
, **tail
, *thead
;
2630 if (tline
->type
== TOK_PREPROC_ID
&&
2631 (((tline
->text
[1] == '+' || tline
->text
[1] == '-') && tline
->text
[2]) ||
2632 tline
->text
[1] == '%' ||
2633 (tline
->text
[1] >= '0' && tline
->text
[1] <= '9'))) {
2635 int type
= 0, cc
; /* type = 0 to placate optimisers */
2641 tline
= tline
->next
;
2644 while (mac
&& !mac
->name
) /* avoid mistaking %reps for macros */
2645 mac
= mac
->next_active
;
2647 error(ERR_NONFATAL
, "`%s': not in a macro call", t
->text
);
2648 else switch (t
->text
[1]) {
2650 * We have to make a substitution of one of the
2651 * forms %1, %-1, %+1, %%foo, %0.
2655 sprintf(tmpbuf
, "%d", mac
->nparam
);
2656 text
= nasm_strdup(tmpbuf
);
2660 sprintf(tmpbuf
, "..@%lu.", mac
->unique
);
2661 text
= nasm_strcat(tmpbuf
, t
->text
+2);
2664 n
= atoi(t
->text
+2)-1;
2665 if (n
>= mac
->nparam
)
2668 if (mac
->nparam
> 1)
2669 n
= (n
+ mac
->rotate
) % mac
->nparam
;
2670 tt
= mac
->params
[n
];
2674 error (ERR_NONFATAL
,
2675 "macro parameter %d is not a condition code",
2680 if (inverse_ccs
[cc
] == -1) {
2681 error (ERR_NONFATAL
,
2682 "condition code `%s' is not invertible",
2686 text
= nasm_strdup(conditions
[inverse_ccs
[cc
]]);
2690 n
= atoi(t
->text
+2)-1;
2691 if (n
>= mac
->nparam
)
2694 if (mac
->nparam
> 1)
2695 n
= (n
+ mac
->rotate
) % mac
->nparam
;
2696 tt
= mac
->params
[n
];
2700 error (ERR_NONFATAL
,
2701 "macro parameter %d is not a condition code",
2706 text
= nasm_strdup(conditions
[cc
]);
2710 n
= atoi(t
->text
+1)-1;
2711 if (n
>= mac
->nparam
)
2714 if (mac
->nparam
> 1)
2715 n
= (n
+ mac
->rotate
) % mac
->nparam
;
2716 tt
= mac
->params
[n
];
2719 for (i
=0; i
<mac
->paramlen
[n
]; i
++) {
2720 ttt
= *tail
= nasm_malloc(sizeof(Token
));
2722 ttt
->type
= tt
->type
;
2723 ttt
->text
= nasm_strdup(tt
->text
);
2728 text
= NULL
; /* we've done it here */
2731 nasm_free (t
->text
);
2744 tline
= tline
->next
;
2751 for (; t
&& (tt
=t
->next
)!=NULL
; t
= t
->next
)
2753 case TOK_WHITESPACE
:
2754 if (tt
->type
== TOK_WHITESPACE
) {
2756 nasm_free(tt
->text
);
2761 if (tt
->type
== TOK_ID
|| tt
->type
== TOK_NUMBER
) {
2762 char *tmp
= nasm_strcat(t
->text
, tt
->text
);
2766 nasm_free(tt
->text
);
2771 if (tt
->type
== TOK_NUMBER
) {
2772 char *tmp
= nasm_strcat(t
->text
, tt
->text
);
2776 nasm_free(tt
->text
);
2786 * Expand all single-line macro calls made in the given line.
2787 * Return the expanded version of the line. The original is deemed
2788 * to be destroyed in the process. (In reality we'll just move
2789 * Tokens from input to output a lot of the time, rather than
2790 * actually bothering to destroy and replicate.)
2792 static Token
*expand_smacro (Token
*tline
)
2794 Token
*t
, *tt
, *mstart
, **tail
, *thead
;
2795 SMacro
*head
= NULL
, *m
;
2798 int nparam
, sparam
, brackets
, rescan
;
2799 Token
*org_tline
= tline
;
2804 * Trick: we should avoid changing the start token pointer since it can
2805 * be contained in "next" field of other token. Because of this
2806 * we allocate a copy of first token and work with it; at the end of
2807 * routine we copy it back
2811 tline
= nasm_malloc (sizeof (Token
));
2812 *tline
= *org_tline
;
2819 while (tline
) { /* main token loop */
2820 if ((mname
= tline
->text
)) {
2821 /* if this token is a local macro, look in local context */
2822 if (tline
->type
== TOK_ID
|| tline
->type
== TOK_PREPROC_ID
)
2823 ctx
= get_ctx (mname
, TRUE
);
2827 head
= smacros
[hash(mname
)];
2829 head
= ctx
->localmac
;
2831 * We've hit an identifier. As in is_mmacro below, we first
2832 * check whether the identifier is a single-line macro at
2833 * all, then think about checking for parameters if
2836 for (m
= head
; m
; m
= m
->next
)
2837 if (!mstrcmp(m
->name
, mname
, m
->casesense
))
2843 if (m
->nparam
== 0) {
2845 * Simple case: the macro is parameterless. Discard the
2846 * one token that the macro call took, and push the
2847 * expansion back on the to-do stack.
2851 if (!strcmp("__FILE__", m
->name
)) {
2853 src_get(&num
, &(tline
->text
));
2854 nasm_quote(&(tline
->text
));
2855 tline
->type
= TOK_STRING
;
2858 if (!strcmp("__LINE__", m
->name
)) {
2859 nasm_free(tline
->text
);
2860 make_tok_num(tline
, src_get_linnum());
2864 tline
= tline
->next
;
2865 nasm_free (t
->text
);
2871 * Complicated case: at least one macro with this name
2872 * exists and takes parameters. We must find the
2873 * parameters in the call, count them, find the SMacro
2874 * that corresponds to that form of the macro call, and
2875 * substitute for the parameters when we expand. What a
2878 tline
= tline
->next
;
2880 if (!tok_is_(tline
, "(")) {
2882 * This macro wasn't called with parameters: ignore
2883 * the call. (Behaviour borrowed from gnu cpp.)
2893 tline
= tline
->next
;
2894 sparam
= PARAM_DELTA
;
2895 params
= nasm_malloc (sparam
*sizeof(Token
*));
2897 paramsize
= nasm_malloc (sparam
*sizeof(int));
2899 for (;;tline
= tline
->next
) { /* parameter loop */
2902 "macro call expects terminating `)'");
2905 if (tline
->type
== TOK_WHITESPACE
&& brackets
<=0) {
2906 if (paramsize
[nparam
])
2909 params
[nparam
] = tline
->next
;
2910 continue; /* parameter loop */
2912 if (tline
->type
== TOK_OTHER
&& tline
->text
[1]==0) {
2913 char ch
= tline
->text
[0];
2914 if (ch
== ',' && !paren
&& brackets
<=0) {
2915 if (++nparam
>= sparam
) {
2916 sparam
+= PARAM_DELTA
;
2917 params
= nasm_realloc (params
,
2918 sparam
*sizeof(Token
*));
2919 paramsize
= nasm_realloc (paramsize
,
2920 sparam
*sizeof(int));
2922 params
[nparam
] = tline
->next
;
2923 paramsize
[nparam
] = 0;
2925 continue; /* parameter loop */
2928 (brackets
>0 || (brackets
==0 &&
2929 !paramsize
[nparam
])))
2933 params
[nparam
] = tline
->next
;
2934 continue; /* parameter loop */
2937 if (ch
== '}' && brackets
>0)
2938 if (--brackets
== 0) {
2940 continue; /* parameter loop */
2942 if (ch
== '(' && !brackets
)
2944 if (ch
== ')' && brackets
<=0)
2950 error (ERR_NONFATAL
, "braces do not "
2951 "enclose all of macro parameter");
2953 paramsize
[nparam
] += white
+1;
2955 } /* parameter loop */
2957 while (m
&& (m
->nparam
!= nparam
||
2958 mstrcmp(m
->name
, mname
, m
->casesense
)))
2961 error (ERR_WARNING
|ERR_WARN_MNP
,
2962 "macro `%s' exists, "
2963 "but not taking %d parameters",
2964 mstart
->text
, nparam
);
2967 if (m
&& m
->in_progress
)
2969 if (!m
) /* in progess or didn't find '(' or wrong nparam */
2972 * Design question: should we handle !tline, which
2973 * indicates missing ')' here, or expand those
2974 * macros anyway, which requires the (t) test a few
2978 nasm_free (paramsize
);
2982 * Expand the macro: we are placed on the last token of the
2983 * call, so that we can easily split the call from the
2984 * following tokens. We also start by pushing an SMAC_END
2985 * token for the cycle removal.
2992 tt
= nasm_malloc(sizeof(Token
));
2993 tt
->type
= TOK_SMAC_END
;
2996 m
->in_progress
= TRUE
;
2999 for (t
= m
->expansion
; t
; t
= t
->next
) {
3000 if (t
->type
>= TOK_SMAC_PARAM
) {
3001 Token
*pcopy
= tline
, **ptail
= &pcopy
;
3005 ttt
= params
[t
->type
- TOK_SMAC_PARAM
];
3006 for (i
=paramsize
[t
->type
-TOK_SMAC_PARAM
]; --i
>=0;) {
3007 pt
= *ptail
= nasm_malloc(sizeof(Token
));
3010 pt
->text
= nasm_strdup(ttt
->text
);
3011 pt
->type
= ttt
->type
;
3017 tt
= nasm_malloc(sizeof(Token
));
3019 tt
->text
= nasm_strdup(t
->text
);
3027 * Having done that, get rid of the macro call, and clean
3028 * up the parameters.
3031 nasm_free (paramsize
);
3032 free_tlist (mstart
);
3033 continue; /* main token loop */
3038 if (tline
->type
== TOK_SMAC_END
) {
3039 tline
->mac
->in_progress
= FALSE
;
3041 tline
= tline
->next
;
3045 tline
= tline
->next
;
3053 * Now scan the entire line and look for successive TOK_IDs that resulted
3054 * after expansion (they can't be produced by tokenise()). The successive
3055 * TOK_IDs should be concatenated.
3056 * Also we look for %+ tokens and concatenate the tokens before and after
3057 * them (without white spaces in between).
3062 while (t
&& t
->type
!= TOK_ID
&& t
->type
!= TOK_PREPROC_ID
)
3066 if (t
->next
->type
== TOK_ID
||
3067 t
->next
->type
== TOK_PREPROC_ID
||
3068 t
->next
->type
== TOK_NUMBER
) {
3069 Token
*next
= t
->next
->next
;
3070 char *p
= nasm_malloc (strlen (t
->text
) + strlen (t
->next
->text
) + 1);
3071 strcpy (p
, t
->text
);
3072 strcat (p
, t
->next
->text
);
3073 nasm_free (t
->text
);
3074 nasm_free (t
->next
->text
);
3075 nasm_free (t
->next
);
3079 } else if (t
->next
->type
== TOK_WHITESPACE
&& t
->next
->next
&&
3080 t
->next
->next
->type
== TOK_PREPROC_ID
&&
3081 strcmp (t
->next
->next
->text
, "%+") == 0) {
3082 /* free the next whitespace, the %+ token and next whitespace */
3084 for (i
= 1; i
<= 3; i
++)
3087 if (!t
->next
|| (i
!= 2 && t
->next
->type
!= TOK_WHITESPACE
))
3089 next
= t
->next
->next
;
3090 nasm_free (t
->next
->text
);
3091 nasm_free (t
->next
);
3097 /* If we concatenaded something, re-scan the line for macros */
3106 *org_tline
= *thead
;
3110 /* the expression expanded to empty line;
3111 we can't return NULL for some reasons
3112 we just set the line to a single WHITESPACE token. */
3113 memset (org_tline
, 0, sizeof (*org_tline
));
3114 org_tline
->text
= nasm_strdup (" ");
3115 org_tline
->type
= TOK_WHITESPACE
;
3124 * Similar to expand_smacro but used exclusively with macro identifiers
3125 * right before they are fetched in. The reason is that there can be
3126 * identifiers consisting of several subparts. We consider that if there
3127 * are more than one element forming the name, user wants a expansion,
3128 * otherwise it will be left as-is. Example:
3132 * the identifier %$abc will be left as-is so that the handler for %define
3133 * will suck it and define the corresponding value. Other case:
3135 * %define _%$abc cde
3137 * In this case user wants name to be expanded *before* %define starts
3138 * working, so we'll expand %$abc into something (if it has a value;
3139 * otherwise it will be left as-is) then concatenate all successive
3142 static Token
*expand_id (Token
*tline
)
3144 Token
*cur
, *oldnext
= NULL
;
3152 (cur
->next
->type
== TOK_ID
||
3153 cur
->next
->type
== TOK_PREPROC_ID
||
3154 cur
->next
->type
== TOK_NUMBER
))
3157 /* If identifier consists of just one token, don't expand */
3162 oldnext
= cur
->next
; /* Detach the tail past identifier */
3163 cur
->next
= NULL
; /* so that expand_smacro stops here */
3166 tline
= expand_smacro (tline
);
3169 /* expand_smacro possibly changhed tline; re-scan for EOL */
3171 while (cur
&& cur
->next
)
3174 cur
->next
= oldnext
;
3181 * Determine whether the given line constitutes a multi-line macro
3182 * call, and return the MMacro structure called if so. Doesn't have
3183 * to check for an initial label - that's taken care of in
3184 * expand_mmacro - but must check numbers of parameters. Guaranteed
3185 * to be called with tline->type == TOK_ID, so the putative macro
3186 * name is easy to find.
3188 static MMacro
*is_mmacro (Token
*tline
, Token
***params_array
)
3194 head
= mmacros
[hash(tline
->text
)];
3197 * Efficiency: first we see if any macro exists with the given
3198 * name. If not, we can return NULL immediately. _Then_ we
3199 * count the parameters, and then we look further along the
3200 * list if necessary to find the proper MMacro.
3202 for (m
= head
; m
; m
= m
->next
)
3203 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
3209 * OK, we have a potential macro. Count and demarcate the
3212 count_mmac_params (tline
->next
, &nparam
, ¶ms
);
3215 * So we know how many parameters we've got. Find the MMacro
3216 * structure that handles this number.
3219 if (m
->nparam_min
<= nparam
&& (m
->plus
|| nparam
<= m
->nparam_max
)) {
3221 * This one is right. Just check if cycle removal
3222 * prohibits us using it before we actually celebrate...
3224 if (m
->in_progress
) {
3226 error (ERR_NONFATAL
,
3227 "self-reference in multi-line macro `%s'",
3234 * It's right, and we can use it. Add its default
3235 * parameters to the end of our list if necessary.
3237 if (m
->defaults
&& nparam
< m
->nparam_min
+ m
->ndefs
) {
3238 params
= nasm_realloc (params
, ((m
->nparam_min
+m
->ndefs
+1) *
3240 while (nparam
< m
->nparam_min
+ m
->ndefs
) {
3241 params
[nparam
] = m
->defaults
[nparam
- m
->nparam_min
];
3246 * If we've gone over the maximum parameter count (and
3247 * we're in Plus mode), ignore parameters beyond
3250 if (m
->plus
&& nparam
> m
->nparam_max
)
3251 nparam
= m
->nparam_max
;
3253 * Then terminate the parameter list, and leave.
3255 if (!params
) { /* need this special case */
3256 params
= nasm_malloc(sizeof(*params
));
3259 params
[nparam
] = NULL
;
3260 *params_array
= params
;
3264 * This one wasn't right: look for the next one with the
3267 for (m
= m
->next
; m
; m
= m
->next
)
3268 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
3273 * After all that, we didn't find one with the right number of
3274 * parameters. Issue a warning, and fail to expand the macro.
3276 error (ERR_WARNING
|ERR_WARN_MNP
,
3277 "macro `%s' exists, but not taking %d parameters",
3278 tline
->text
, nparam
);
3284 * Expand the multi-line macro call made by the given line, if
3285 * there is one to be expanded. If there is, push the expansion on
3286 * istk->expansion and return 1. Otherwise return 0.
3288 static int expand_mmacro (Token
*tline
)
3290 Token
*startline
= tline
;
3291 Token
*label
= NULL
;
3292 int dont_prepend
= 0;
3293 Token
**params
, *t
, *tt
;
3296 int i
, nparam
, *paramlen
;
3300 if (!tok_type_(t
, TOK_ID
))
3302 m
= is_mmacro (t
, ¶ms
);
3306 * We have an id which isn't a macro call. We'll assume
3307 * it might be a label; we'll also check to see if a
3308 * colon follows it. Then, if there's another id after
3309 * that lot, we'll check it again for macro-hood.
3313 if (tok_type_(t
, TOK_WHITESPACE
))
3314 last
= t
, t
= t
->next
;
3315 if (tok_is_(t
, ":")) {
3317 last
= t
, t
= t
->next
;
3318 if (tok_type_(t
, TOK_WHITESPACE
))
3319 last
= t
, t
= t
->next
;
3321 if (!tok_type_(t
, TOK_ID
) || (m
= is_mmacro(t
, ¶ms
)) == NULL
)
3328 * Fix up the parameters: this involves stripping leading and
3329 * trailing whitespace, then stripping braces if they are
3332 for (nparam
= 0; params
[nparam
]; nparam
++)
3334 paramlen
= nparam
? nasm_malloc(nparam
*sizeof(*paramlen
)) : NULL
;
3336 for (i
= 0; params
[i
]; i
++) {
3338 int comma
= (!m
->plus
|| i
< nparam
-1);
3342 if (tok_is_(t
, "{"))
3343 t
= t
->next
, brace
= TRUE
, comma
= FALSE
;
3347 if (comma
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, ","))
3348 break; /* ... because we have hit a comma */
3349 if (comma
&& t
->type
== TOK_WHITESPACE
&& tok_is_(t
->next
, ","))
3350 break; /* ... or a space then a comma */
3351 if (brace
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, "}"))
3352 break; /* ... or a brace */
3359 * OK, we have a MMacro structure together with a set of
3360 * parameters. We must now go through the expansion and push
3361 * copies of each Line on to istk->expansion. Substitution of
3362 * parameter tokens and macro-local tokens doesn't get done
3363 * until the single-line macro substitution process; this is
3364 * because delaying them allows us to change the semantics
3365 * later through %rotate.
3367 * First, push an end marker on to istk->expansion, mark this
3368 * macro as in progress, and set up its invocation-specific
3371 ll
= nasm_malloc(sizeof(Line
));
3372 ll
->next
= istk
->expansion
;
3375 istk
->expansion
= ll
;
3377 m
->in_progress
= TRUE
;
3382 m
->paramlen
= paramlen
;
3383 m
->unique
= unique
++;
3386 m
->next_active
= istk
->mstk
;
3389 for (l
= m
->expansion
; l
; l
= l
->next
) {
3392 ll
= nasm_malloc(sizeof(Line
));
3393 ll
->finishes
= NULL
;
3394 ll
->next
= istk
->expansion
;
3395 istk
->expansion
= ll
;
3398 for (t
= l
->first
; t
; t
= t
->next
) {
3400 if (t
->type
== TOK_PREPROC_ID
&&
3401 t
->text
[1]=='0' && t
->text
[2]=='0')
3408 tt
= *tail
= nasm_malloc(sizeof(Token
));
3411 tt
->text
= nasm_strdup(x
->text
);
3418 * If we had a label, push it on as the first line of
3419 * the macro expansion.
3423 free_tlist(startline
);
3425 ll
= nasm_malloc(sizeof(Line
));
3426 ll
->finishes
= NULL
;
3427 ll
->next
= istk
->expansion
;
3428 istk
->expansion
= ll
;
3429 ll
->first
= startline
;
3430 if (!dont_prepend
) {
3432 label
= label
->next
;
3433 label
->next
= tt
= nasm_malloc(sizeof(Token
));
3436 tt
->type
= TOK_OTHER
;
3437 tt
->text
= nasm_strdup(":");
3442 list
->uplevel (m
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
3448 * Since preprocessor always operate only on the line that didn't
3449 * arrived yet, we should always use ERR_OFFBY1. Also since user
3450 * won't want to see same error twice (preprocessing is done once
3451 * per pass) we will want to show errors only during pass one.
3453 static void error (int severity
, char *fmt
, ...)
3458 /* If we're in a dead branch of IF or something like it, ignore the error */
3459 if (istk
->conds
&& !emitting(istk
->conds
->state
))
3462 va_start (arg
, fmt
);
3463 vsprintf (buff
, fmt
, arg
);
3466 if (istk
->mstk
&& istk
->mstk
->name
)
3467 __error (severity
|ERR_PASS1
, "(%s:%d) %s", istk
->mstk
->name
,
3468 istk
->mstk
->lineno
, buff
);
3470 __error (severity
|ERR_PASS1
, "%s", buff
);
3473 static void pp_reset (char *file
, int apass
, efunc errfunc
, evalfunc eval
,
3480 istk
= nasm_malloc(sizeof(Include
));
3483 istk
->expansion
= NULL
;
3485 istk
->fp
= fopen(file
, "r");
3487 src_set_fname(nasm_strdup(file
));
3491 error (ERR_FATAL
|ERR_NOFILE
, "unable to open input file `%s'", file
);
3493 for (h
=0; h
<NHASH
; h
++) {
3499 any_extrastdmac
= (extrastdmac
!= NULL
);
3505 static char *pp_getline (void)
3513 * Fetch a tokenised line, either from the macro-expansion
3514 * buffer or from the input file.
3517 while (istk
->expansion
&& istk
->expansion
->finishes
) {
3518 Line
*l
= istk
->expansion
;
3519 if (!l
->finishes
->name
&& l
->finishes
->in_progress
> 1) {
3523 * This is a macro-end marker for a macro with no
3524 * name, which means it's not really a macro at all
3525 * but a %rep block, and the `in_progress' field is
3526 * more than 1, meaning that we still need to
3527 * repeat. (1 means the natural last repetition; 0
3528 * means termination by %exitrep.) We have
3529 * therefore expanded up to the %endrep, and must
3530 * push the whole block on to the expansion buffer
3531 * again. We don't bother to remove the macro-end
3532 * marker: we'd only have to generate another one
3535 l
->finishes
->in_progress
--;
3536 for (l
= l
->finishes
->expansion
; l
; l
= l
->next
) {
3537 Token
*t
, *tt
, **tail
;
3539 ll
= nasm_malloc(sizeof(Line
));
3540 ll
->next
= istk
->expansion
;
3541 ll
->finishes
= NULL
;
3545 for (t
= l
->first
; t
; t
= t
->next
) {
3547 tt
= *tail
= nasm_malloc(sizeof(Token
));
3551 tt
->text
= nasm_strdup(t
->text
);
3556 istk
->expansion
= ll
;
3560 * Check whether a `%rep' was started and not ended
3561 * within this macro expansion. This can happen and
3562 * should be detected. It's a fatal error because
3563 * I'm too confused to work out how to recover
3569 "defining with name in expansion");
3570 else if (istk
->mstk
->name
)
3571 error (ERR_FATAL
, "`%%rep' without `%%endrep' within"
3572 " expansion of macro `%s'", istk
->mstk
->name
);
3576 * FIXME: investigate the relationship at this point between
3577 * istk->mstk and l->finishes
3580 MMacro
*m
= istk
->mstk
;
3581 istk
->mstk
= m
->next_active
;
3584 * This was a real macro call, not a %rep, and
3585 * therefore the parameter information needs to
3588 nasm_free(m
->params
);
3589 free_tlist(m
->iline
);
3590 nasm_free(m
->paramlen
);
3591 l
->finishes
->in_progress
= FALSE
;
3596 istk
->expansion
= l
->next
;
3598 list
->downlevel (LIST_MACRO
);
3601 while (1) { /* until we get a line we can use */
3603 if (istk
->expansion
) { /* from a macro expansion */
3605 Line
*l
= istk
->expansion
;
3607 istk
->mstk
->lineno
++;
3609 istk
->expansion
= l
->next
;
3611 p
= detoken (tline
, FALSE
);
3612 list
->line (LIST_MACRO
, p
);
3617 if (line
) { /* from the current input file */
3618 line
= prepreproc(line
);
3619 tline
= tokenise(line
);
3624 * The current file has ended; work down the istk
3630 error(ERR_FATAL
, "expected `%%endif' before end of file");
3632 list
->downlevel (LIST_INCLUDE
);
3633 src_set_linnum(i
->lineno
);
3634 nasm_free ( src_set_fname(i
->fname
) );
3642 * We must expand MMacro parameters and MMacro-local labels
3643 * _before_ we plunge into directive processing, to cope
3644 * with things like `%define something %1' such as STRUC
3645 * uses. Unless we're _defining_ a MMacro, in which case
3646 * those tokens should be left alone to go into the
3647 * definition; and unless we're in a non-emitting
3648 * condition, in which case we don't want to meddle with
3651 if (!defining
&& !(istk
->conds
&& !emitting(istk
->conds
->state
)))
3652 tline
= expand_mmac_params(tline
);
3655 * Check the line to see if it's a preprocessor directive.
3657 ret
= do_directive(tline
);
3660 } else if (defining
) {
3662 * We're defining a multi-line macro. We emit nothing
3664 * shove the tokenised line on to the macro definition.
3666 Line
*l
= nasm_malloc(sizeof(Line
));
3667 l
->next
= defining
->expansion
;
3669 l
->finishes
= FALSE
;
3670 defining
->expansion
= l
;
3672 } else if (istk
->conds
&& !emitting(istk
->conds
->state
)) {
3674 * We're in a non-emitting branch of a condition block.
3675 * Emit nothing at all, not even a blank line: when we
3676 * emerge from the condition we'll give a line-number
3677 * directive so we keep our place correctly.
3681 } else if (istk
->mstk
&& !istk
->mstk
->in_progress
) {
3683 * We're in a %rep block which has been terminated, so
3684 * we're walking through to the %endrep without
3685 * emitting anything. Emit nothing at all, not even a
3686 * blank line: when we emerge from the %rep block we'll
3687 * give a line-number directive so we keep our place
3693 tline
= expand_smacro(tline
);
3694 ret
= expand_mmacro(tline
);
3697 * De-tokenise the line again, and emit it.
3699 line
= detoken(tline
, TRUE
);
3703 continue; /* expand_mmacro calls free_tlist */
3711 static void pp_cleanup (void)
3716 error (ERR_NONFATAL
, "end of file while still defining macro `%s'",
3718 free_mmacro (defining
);
3722 for (h
=0; h
<NHASH
; h
++) {
3723 while (mmacros
[h
]) {
3724 MMacro
*m
= mmacros
[h
];
3725 mmacros
[h
] = mmacros
[h
]->next
;
3728 while (smacros
[h
]) {
3729 SMacro
*s
= smacros
[h
];
3730 smacros
[h
] = smacros
[h
]->next
;
3731 nasm_free (s
->name
);
3732 free_tlist (s
->expansion
);
3740 nasm_free (i
->fname
);
3747 void pp_include_path (char *path
)
3751 i
= nasm_malloc(sizeof(IncPath
));
3752 i
->path
= nasm_strdup(path
);
3757 void pp_pre_include (char *fname
)
3759 Token
*inc
, *space
, *name
;
3762 inc
= nasm_malloc(sizeof(Token
));
3763 inc
->next
= space
= nasm_malloc(sizeof(Token
));
3764 space
->next
= name
= nasm_malloc(sizeof(Token
));
3767 inc
->type
= TOK_PREPROC_ID
;
3768 inc
->text
= nasm_strdup("%include");
3769 space
->type
= TOK_WHITESPACE
;
3770 space
->text
= nasm_strdup(" ");
3771 name
->type
= TOK_INTERNAL_STRING
;
3772 name
->text
= nasm_strdup(fname
);
3774 inc
->mac
= space
->mac
= name
->mac
= NULL
;
3776 l
= nasm_malloc(sizeof(Line
));
3779 l
->finishes
= FALSE
;
3783 void pp_pre_define (char *definition
)
3789 equals
= strchr(definition
, '=');
3791 def
= nasm_malloc(sizeof(Token
));
3792 def
->next
= space
= nasm_malloc(sizeof(Token
));
3795 space
->next
= tokenise(definition
);
3799 def
->type
= TOK_PREPROC_ID
;
3800 def
->text
= nasm_strdup("%define");
3801 space
->type
= TOK_WHITESPACE
;
3802 space
->text
= nasm_strdup(" ");
3804 def
->mac
= space
->mac
= NULL
;
3806 l
= nasm_malloc(sizeof(Line
));
3809 l
->finishes
= FALSE
;
3813 void pp_pre_undefine (char *definition
)
3818 def
= nasm_malloc(sizeof(Token
));
3819 def
->next
= space
= nasm_malloc(sizeof(Token
));
3820 space
->next
= tokenise(definition
);
3822 def
->type
= TOK_PREPROC_ID
;
3823 def
->text
= nasm_strdup("%undef");
3824 space
->type
= TOK_WHITESPACE
;
3825 space
->text
= nasm_strdup(" ");
3827 def
->mac
= space
->mac
= NULL
;
3829 l
= nasm_malloc(sizeof(Line
));
3832 l
->finishes
= FALSE
;
3836 void pp_extra_stdmac (char **macros
)
3838 extrastdmac
= macros
;
3841 static void make_tok_num(Token
*tok
, long val
)
3844 sprintf(numbuf
, "%ld", val
);
3845 tok
->text
= nasm_strdup(numbuf
);
3846 tok
->type
= TOK_NUMBER
;