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
26 typedef struct SMacro SMacro
;
27 typedef struct MMacro MMacro
;
28 typedef struct Context Context
;
29 typedef struct Token Token
;
30 typedef struct Line Line
;
31 typedef struct Include Include
;
32 typedef struct Cond Cond
;
33 typedef struct IncPath IncPath
;
36 * Store the definition of a single-line macro.
48 * Store the definition of a multi-line macro. This is also used to
49 * store the interiors of `%rep...%endrep' blocks, which are
50 * effectively self-re-invoking multi-line macros which simply
51 * don't have a name or bother to appear in the hash tables. %rep
52 * blocks are signified by having a NULL `name' field.
54 * In a MMacro describing a `%rep' block, the `in_progress' field
55 * isn't merely boolean, but gives the number of repeats left to
58 * The `next' field is used for storing MMacros in hash tables; the
59 * `next_active' field is for stacking them on istk entries.
61 * When a MMacro is being expanded, `params', `iline', `nparam',
62 * `paramlen', `rotate' and `unique' are local to the invocation.
68 int nparam_min
, nparam_max
;
69 int plus
; /* is the last parameter greedy? */
70 int nolist
; /* is this macro listing-inhibited? */
72 Token
*dlist
; /* All defaults as one list */
73 Token
**defaults
; /* Parameter default pointers */
74 int ndefs
; /* number of default parameters */
78 MMacro
*rep_nest
; /* used for nesting %rep */
79 Token
**params
; /* actual parameters */
80 Token
*iline
; /* invocation line */
81 int nparam
, rotate
, *paramlen
;
86 * The context stack is composed of a linked list of these.
96 * This is the internal form which we break input lines up into.
97 * Typically stored in linked lists.
99 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
100 * necessarily used as-is, but is intended to denote the number of
101 * the substituted parameter. So in the definition
103 * %define a(x,y) ( (x) & ~(y) )
105 * the token representing `x' will have its type changed to
106 * TOK_SMAC_PARAM, but the one representing `y' will be
109 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
110 * which doesn't need quotes around it. Used in the pre-include
111 * mechanism as an alternative to trying to find a sensible type of
112 * quote to use on the filename we were passed.
117 SMacro
*mac
; /* associated macro for TOK_SMAC_END */
121 TOK_WHITESPACE
= 1, TOK_COMMENT
, TOK_ID
, TOK_PREPROC_ID
, TOK_STRING
,
122 TOK_NUMBER
, TOK_SMAC_END
, TOK_OTHER
, TOK_SMAC_PARAM
,
127 * Multi-line macro definitions are stored as a linked list of
128 * these, which is essentially a container to allow several linked
131 * Note that in this module, linked lists are treated as stacks
132 * wherever possible. For this reason, Lines are _pushed_ on to the
133 * `expansion' field in MMacro structures, so that the linked list,
134 * if walked, would give the macro lines in reverse order; this
135 * means that we can walk the list when expanding a macro, and thus
136 * push the lines on to the `expansion' field in _istk_ in reverse
137 * order (so that when popped back off they are in the right
138 * order). It may seem cockeyed, and it relies on my design having
139 * an even number of steps in, but it works...
141 * Some of these structures, rather than being actual lines, are
142 * markers delimiting the end of the expansion of a given macro.
143 * This is for use in the cycle-tracking and %rep-handling code.
144 * Such structures have `finishes' non-NULL, and `first' NULL. All
145 * others have `finishes' NULL, but `first' may still be NULL if
155 * To handle an arbitrary level of file inclusion, we maintain a
156 * stack (ie linked list) of these things.
165 MMacro
*mstk
; /* stack of active macros/reps */
169 * Include search path. This is simply a list of strings which get
170 * prepended, in turn, to the name of an include file, in an
171 * attempt to find the file if it's not in the current directory.
179 * Conditional assembly: we maintain a separate stack of these for
180 * each level of file inclusion. (The only reason we keep the
181 * stacks separate is to ensure that a stray `%endif' in a file
182 * included from within the true branch of a `%if' won't terminate
183 * it and cause confusion: instead, rightly, it'll cause an error.)
191 * These states are for use just after %if or %elif: IF_TRUE
192 * means the condition has evaluated to truth so we are
193 * currently emitting, whereas IF_FALSE means we are not
194 * currently emitting but will start doing so if a %else comes
195 * up. In these states, all directives are admissible: %elif,
196 * %else and %endif. (And of course %if.)
198 COND_IF_TRUE
, COND_IF_FALSE
,
200 * These states come up after a %else: ELSE_TRUE means we're
201 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
202 * any %elif or %else will cause an error.
204 COND_ELSE_TRUE
, COND_ELSE_FALSE
,
206 * This state means that we're not emitting now, and also that
207 * nothing until %endif will be emitted at all. It's for use in
208 * two circumstances: (i) when we've had our moment of emission
209 * and have now started seeing %elifs, and (ii) when the
210 * condition construct in question is contained within a
211 * non-emitting branch of a larger condition construct.
215 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
218 * Condition codes. Note that we use c_ prefix not C_ because C_ is
219 * used in nasm.h for the "real" condition codes. At _this_ level,
220 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
221 * ones, so we need a different enum...
223 static char *conditions
[] = {
224 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
225 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
226 "np", "ns", "nz", "o", "p", "pe", "po", "s", "z"
229 c_A
, c_AE
, c_B
, c_BE
, c_C
, c_CXZ
, c_E
, c_ECXZ
, c_G
, c_GE
, c_L
, c_LE
,
230 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, c_NE
, c_NG
, c_NGE
, c_NL
, c_NLE
, c_NO
,
231 c_NP
, c_NS
, c_NZ
, c_O
, c_P
, c_PE
, c_PO
, c_S
, c_Z
233 static int inverse_ccs
[] = {
234 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, -1, c_NE
, -1, c_NG
, c_NGE
, c_NL
, c_NLE
,
235 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
,
236 c_Z
, c_NO
, c_NP
, c_PO
, c_PE
, c_NS
, c_NZ
242 static char *directives
[] = {
243 "%assign", "%clear", "%define", "%elif", "%elifctx", "%elifdef",
244 "%elifid", "%elifidn", "%elifidni", "%elifnctx", "%elifndef",
245 "%elifnid", "%elifnidn", "%elifnidni", "%elifnnum", "%elifnstr",
246 "%elifnum", "%elifstr", "%else", "%endif", "%endm", "%endmacro",
247 "%endrep", "%error", "%exitrep", "%iassign", "%idefine", "%if",
248 "%ifctx", "%ifdef", "%ifid", "%ifidn", "%ifidni", "%ifnctx",
249 "%ifndef", "%ifnid", "%ifnidn", "%ifnidni", "%ifnnum",
250 "%ifnstr", "%ifnum", "%ifstr", "%imacro", "%include", "%line",
251 "%macro", "%pop", "%push", "%rep", "%repl", "%rotate"
254 PP_ASSIGN
, PP_CLEAR
, PP_DEFINE
, PP_ELIF
, PP_ELIFCTX
, PP_ELIFDEF
,
255 PP_ELIFID
, PP_ELIFIDN
, PP_ELIFIDNI
, PP_ELIFNCTX
, PP_ELIFNDEF
,
256 PP_ELIFNID
, PP_ELIFNIDN
, PP_ELIFNIDNI
, PP_ELIFNNUM
, PP_ELIFNSTR
,
257 PP_ELIFNUM
, PP_ELIFSTR
, PP_ELSE
, PP_ENDIF
, PP_ENDM
, PP_ENDMACRO
,
258 PP_ENDREP
, PP_ERROR
, PP_EXITREP
, PP_IASSIGN
, PP_IDEFINE
, PP_IF
,
259 PP_IFCTX
, PP_IFDEF
, PP_IFID
, PP_IFIDN
, PP_IFIDNI
, PP_IFNCTX
,
260 PP_IFNDEF
, PP_IFNID
, PP_IFNIDN
, PP_IFNIDNI
, PP_IFNNUM
,
261 PP_IFNSTR
, PP_IFNUM
, PP_IFSTR
, PP_IMACRO
, PP_INCLUDE
, PP_LINE
,
262 PP_MACRO
, PP_POP
, PP_PUSH
, PP_REP
, PP_REPL
, PP_ROTATE
266 static Context
*cstk
;
267 static Include
*istk
;
268 static IncPath
*ipath
= NULL
;
271 static evalfunc evaluate
;
275 static unsigned long unique
; /* unique identifier numbers */
277 static Line
*predef
= NULL
;
279 static ListGen
*list
;
282 * The number of hash values we use for the macro lookup tables.
283 * FIXME: We should *really* be able to configure this at run time,
284 * or even have the hash table automatically expanding when necessary.
289 * The current set of multi-line macros we have defined.
291 static MMacro
*mmacros
[NHASH
];
294 * The current set of single-line macros we have defined.
296 static SMacro
*smacros
[NHASH
];
299 * The multi-line macro we are currently defining, or the %rep
300 * block we are currently reading, if any.
302 static MMacro
*defining
;
305 * The number of macro parameters to allocate space for at a time.
307 #define PARAM_DELTA 16
310 * The standard macro set: defined as `static char *stdmac[]'. Also
311 * gives our position in the macro set, when we're processing it.
314 static char **stdmacpos
;
317 * The extra standard macros that come from the object format, if
320 static char **extrastdmac
= NULL
;
324 * Forward declarations.
326 static Token
*expand_smacro (Token
*tline
);
327 static void make_tok_num(Token
*tok
, long val
);
330 * Macros for safe checking of token pointers, avoid *(NULL)
332 #define tok_type_(x,t) ((x) && (x)->type == (t))
333 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
334 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
335 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
338 * The pre-preprocessing stage... This function translates line
339 * number indications as they emerge from GNU cpp (`# lineno "file"
340 * flags') into NASM preprocessor line number indications (`%line
343 static char *prepreproc(char *line
)
346 char *fname
, *oldline
;
348 if (line
[0] == '#' && line
[1] == ' ') {
351 lineno
= atoi(fname
);
352 fname
+= strspn(fname
, "0123456789 ");
355 fnlen
= strcspn(fname
, "\"");
356 line
= nasm_malloc(20+fnlen
);
357 sprintf(line
, "%%line %d %.*s", lineno
, fnlen
, fname
);
364 * The hash function for macro lookups. Note that due to some
365 * macros having case-insensitive names, the hash function must be
366 * invariant under case changes. We implement this by applying a
367 * perfectly normal hash function to the uppercase of the string.
369 static int hash(char *s
)
374 * Powers of three, mod 31.
376 static const int multipliers
[] = {
377 1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10,
378 30, 28, 22, 4, 12, 5, 15, 14, 11, 2, 6, 18, 23, 7, 21
383 h
+= multipliers
[i
] * (unsigned char) (toupper(*s
));
385 if (++i
>= sizeof(multipliers
)/sizeof(*multipliers
))
393 * Free a linked list of tokens.
395 static void free_tlist (Token
*list
)
407 * Free a linked list of lines.
409 static void free_llist (Line
*list
)
415 free_tlist (l
->first
);
423 static void free_mmacro (MMacro
*m
)
426 free_tlist (m
->dlist
);
427 nasm_free (m
->defaults
);
428 free_llist (m
->expansion
);
433 * Pop the context stack.
435 static void ctx_pop (void)
446 free_tlist (s
->expansion
);
453 #define BUF_DELTA 512
455 * Read a line from the top file in istk, handling multiple CR/LFs
456 * at the end of the line read, and handling spurious ^Zs. Will
457 * return lines from the standard macro set if this has not already
460 static char *read_line (void)
462 char *buffer
, *p
, *q
;
467 char *ret
= nasm_strdup(*stdmacpos
++);
468 if (!*stdmacpos
&& any_extrastdmac
)
470 stdmacpos
= extrastdmac
;
471 any_extrastdmac
= FALSE
;
475 * Nasty hack: here we push the contents of `predef' on
476 * to the top-level expansion stack, since this is the
477 * most convenient way to implement the pre-include and
478 * pre-define features.
483 Token
*head
, **tail
, *t
, *tt
;
485 for (pd
= predef
; pd
; pd
= pd
->next
) {
488 for (t
= pd
->first
; t
; t
= t
->next
) {
489 tt
= *tail
= nasm_malloc(sizeof(Token
));
493 tt
->text
= nasm_strdup(t
->text
);
494 tt
->mac
= t
->mac
; /* always NULL here, in fact */
496 l
= nasm_malloc(sizeof(Line
));
497 l
->next
= istk
->expansion
;
511 buffer
= nasm_malloc(BUF_DELTA
);
514 q
= fgets(p
, bufsize
-(p
-buffer
), istk
->fp
);
518 if (p
> buffer
&& p
[-1] == '\n') {
521 if (p
-buffer
> bufsize
-10) {
522 long offset
= p
-buffer
;
523 bufsize
+= BUF_DELTA
;
524 buffer
= nasm_realloc(buffer
, bufsize
);
525 p
= buffer
+offset
; /* prevent stale-pointer problems */
529 if (!q
&& p
== buffer
) {
534 src_set_linnum(src_get_linnum() + istk
->lineinc
);
537 * Play safe: remove CRs as well as LFs, if any of either are
538 * present at the end of the line.
540 while (--p
>= buffer
&& (*p
== '\n' || *p
== '\r'))
544 * Handle spurious ^Z, which may be inserted into source files
545 * by some file transfer utilities.
547 buffer
[strcspn(buffer
, "\032")] = '\0';
549 list
->line (LIST_READ
, buffer
);
555 * Tokenise a line of text. This is a very simple process since we
556 * don't need to parse the value out of e.g. numeric tokens: we
557 * simply split one string into many.
559 static Token
*tokenise (char *line
)
564 Token
*t
, **tail
= &list
;
568 if (*p
== '%' && ( isdigit(p
[1]) ||
569 ((p
[1] == '-' || p
[1] == '+') && isdigit(p
[2]))))
574 } while (isdigit(*p
));
575 type
= TOK_PREPROC_ID
;
577 else if (*p
== '%' && p
[1] == '{') {
579 while (*p
&& *p
!= '}') {
585 type
= TOK_PREPROC_ID
;
587 else if (*p
== '%' && (isidchar(p
[1]) ||
588 ((p
[1] == '!' || p
[1] == '%' || p
[1] == '$') &&
594 } while (isidchar(*p
));
595 type
= TOK_PREPROC_ID
;
597 else if (isidstart(*p
) || (*p
== '$' && isidstart(p
[1]))) {
600 while (*p
&& isidchar(*p
))
603 else if (*p
== '\'' || *p
== '"') {
610 while (*p
&& *p
!= c
)
614 else if (isnumstart(*p
)) {
620 while (*p
&& isnumchar(*p
))
623 else if (isspace(*p
)) {
624 type
= TOK_WHITESPACE
;
626 while (*p
&& isspace(*p
))
629 * Whitespace just before end-of-line is discarded by
630 * pretending it's a comment; whitespace just before a
631 * comment gets lumped into the comment.
633 if (!*p
|| *p
== ';') {
638 else if (*p
== ';') {
644 * Anything else is an operator of some kind. We check
645 * for all the double-character operators (>>, <<, //,
646 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
647 * else is a single-character operator.
650 if ((p
[0] == '>' && p
[1] == '>') ||
651 (p
[0] == '<' && p
[1] == '<') ||
652 (p
[0] == '/' && p
[1] == '/') ||
653 (p
[0] == '%' && p
[1] == '%') ||
654 (p
[0] == '<' && p
[1] == '=') ||
655 (p
[0] == '>' && p
[1] == '=') ||
656 (p
[0] == '=' && p
[1] == '=') ||
657 (p
[0] == '!' && p
[1] == '=') ||
658 (p
[0] == '<' && p
[1] == '>') ||
659 (p
[0] == '&' && p
[1] == '&') ||
660 (p
[0] == '|' && p
[1] == '|') ||
661 (p
[0] == '^' && p
[1] == '^'))
667 if (type
!= TOK_COMMENT
) {
668 *tail
= t
= nasm_malloc (sizeof(Token
));
672 t
->text
= nasm_malloc(1+p
-line
);
673 strncpy(t
->text
, line
, p
-line
);
674 t
->text
[p
-line
] = '\0';
683 * Convert a line of tokens back into text.
685 char *detoken (Token
*tlist
)
692 for (t
= tlist
; t
; t
= t
->next
) {
693 if (t
->type
== TOK_PREPROC_ID
&& t
->text
[1] == '!') {
694 char *p
= getenv(t
->text
+2);
697 t
->text
= nasm_strdup(p
);
702 len
+= strlen(t
->text
);
704 p
= line
= nasm_malloc(len
+1);
705 for (t
= tlist
; t
; t
= t
->next
) {
716 * A scanner, suitable for use by the expression evaluator, which
717 * operates on a line of Tokens. Expects a pointer to a pointer to
718 * the first token in the line to be passed in as its private_data
721 static int ppscan(void *private_data
, struct tokenval
*tokval
)
723 Token
**tlineptr
= private_data
;
728 *tlineptr
= tline
? tline
->next
: NULL
;
729 } while (tline
&& (tline
->type
== TOK_WHITESPACE
||
730 tline
->type
== TOK_COMMENT
));
733 return tokval
->t_type
= TOKEN_EOS
;
735 if (tline
->text
[0] == '$' && !tline
->text
[1])
736 return tokval
->t_type
= TOKEN_HERE
;
737 if (tline
->text
[0] == '$' && tline
->text
[1] == '$' && !tline
->text
[1])
738 return tokval
->t_type
= TOKEN_BASE
;
740 if (tline
->type
== TOK_ID
) {
741 tokval
->t_charptr
= tline
->text
;
742 if (tline
->text
[0] == '$') {
744 return tokval
->t_type
= TOKEN_ID
;
748 * This is the only special case we actually need to worry
749 * about in this restricted context.
751 if (!nasm_stricmp(tline
->text
, "seg"))
752 return tokval
->t_type
= TOKEN_SEG
;
754 return tokval
->t_type
= TOKEN_ID
;
757 if (tline
->type
== TOK_NUMBER
) {
760 tokval
->t_integer
= readnum(tline
->text
, &rn_error
);
762 return tokval
->t_type
= TOKEN_ERRNUM
;
763 tokval
->t_charptr
= NULL
;
764 return tokval
->t_type
= TOKEN_NUM
;
767 if (tline
->type
== TOK_STRING
) {
776 if (l
== 0 || r
[l
-1] != q
)
777 return tokval
->t_type
= TOKEN_ERRNUM
;
778 tokval
->t_integer
= readstrnum(r
, l
-1, &rn_warn
);
780 error(ERR_WARNING
|ERR_PASS1
,
781 "character constant too long");
782 tokval
->t_charptr
= NULL
;
783 return tokval
->t_type
= TOKEN_NUM
;
786 if (tline
->type
== TOK_OTHER
) {
787 if (!strcmp(tline
->text
, "<<")) return tokval
->t_type
= TOKEN_SHL
;
788 if (!strcmp(tline
->text
, ">>")) return tokval
->t_type
= TOKEN_SHR
;
789 if (!strcmp(tline
->text
, "//")) return tokval
->t_type
= TOKEN_SDIV
;
790 if (!strcmp(tline
->text
, "%%")) return tokval
->t_type
= TOKEN_SMOD
;
791 if (!strcmp(tline
->text
, "==")) return tokval
->t_type
= TOKEN_EQ
;
792 if (!strcmp(tline
->text
, "<>")) return tokval
->t_type
= TOKEN_NE
;
793 if (!strcmp(tline
->text
, "!=")) return tokval
->t_type
= TOKEN_NE
;
794 if (!strcmp(tline
->text
, "<=")) return tokval
->t_type
= TOKEN_LE
;
795 if (!strcmp(tline
->text
, ">=")) return tokval
->t_type
= TOKEN_GE
;
796 if (!strcmp(tline
->text
, "&&")) return tokval
->t_type
= TOKEN_DBL_AND
;
797 if (!strcmp(tline
->text
, "^^")) return tokval
->t_type
= TOKEN_DBL_XOR
;
798 if (!strcmp(tline
->text
, "||")) return tokval
->t_type
= TOKEN_DBL_OR
;
802 * We have no other options: just return the first character of
805 return tokval
->t_type
= tline
->text
[0];
809 * Return the Context structure associated with a %$ token. Return
810 * NULL, having _already_ reported an error condition, if the
811 * context stack isn't deep enough for the supplied number of $
814 static Context
*get_ctx (char *name
)
820 error (ERR_NONFATAL
, "`%s': context stack is empty", name
);
826 while (name
[i
+1] == '$') {
830 error (ERR_NONFATAL
, "`%s': context stack is only"
831 " %d level%s deep", name
, i
-1, (i
==2 ? "" : "s"));
839 * Compare a string to the name of an existing macro; this is a
840 * simple wrapper which calls either strcmp or nasm_stricmp
841 * depending on the value of the `casesense' parameter.
843 static int mstrcmp(char *p
, char *q
, int casesense
)
845 return casesense
? strcmp(p
,q
) : nasm_stricmp(p
,q
);
849 * Open an include file. This routine must always return a valid
850 * file pointer if it returns - it's responsible for throwing an
851 * ERR_FATAL and bombing out completely if not. It should also try
852 * the include path one by one until it finds the file or reaches
853 * the end of the path.
855 static FILE *inc_fopen(char *file
)
858 char *prefix
= "", *combine
;
862 combine
= nasm_strcat(prefix
,file
);
863 fp
= fopen(combine
, "r");
874 "unable to open include file `%s'", file
);
875 return NULL
; /* never reached - placate compilers */
879 * Determine if we should warn on defining a single-line macro of
880 * name `name', with `nparam' parameters. If nparam is 0, will
881 * return TRUE if _any_ single-line macro of that name is defined.
882 * Otherwise, will return TRUE if a single-line macro with either
883 * `nparam' or no parameters is defined.
885 * If a macro with precisely the right number of parameters is
886 * defined, the address of the definition structure will be
887 * returned in `defn'; otherwise NULL will be returned. If `defn'
888 * is NULL, no action will be taken regarding its contents, and no
891 * Note that this is also called with nparam zero to resolve
894 static int smacro_defined (char *name
, int nparam
, SMacro
**defn
, int nocase
)
900 if (name
[0] == '%' && name
[1] == '$') {
901 ctx
= get_ctx (name
);
903 return FALSE
; /* got to return _something_ */
908 m
= smacros
[hash(name
)];
913 if (!mstrcmp(m
->name
, p
, m
->casesense
& nocase
) &&
914 (nparam
== 0 || m
->nparam
== 0 || nparam
== m
->nparam
)) {
916 if (nparam
== m
->nparam
)
929 * Count and mark off the parameters in a multi-line macro call.
930 * This is called both from within the multi-line macro expansion
931 * code, and also to mark off the default parameters when provided
932 * in a %macro definition line.
934 static void count_mmac_params (Token
*t
, int *nparam
, Token
***params
)
936 int paramsize
, brace
;
938 *nparam
= paramsize
= 0;
941 if (*nparam
>= paramsize
) {
942 paramsize
+= PARAM_DELTA
;
943 *params
= nasm_realloc(*params
, sizeof(**params
) * paramsize
);
949 (*params
)[(*nparam
)++] = t
;
950 while (tok_isnt_(t
, brace
? "}" : ","))
952 if (t
) { /* got a comma/brace */
956 * Now we've found the closing brace, look further
960 if (tok_isnt_(t
, ",")) {
962 "braces do not enclose all of macro parameter");
963 while (tok_isnt_(t
, ","))
967 t
= t
->next
; /* eat the comma */
974 * Determine whether one of the various `if' conditions is true or
977 * We must free the tline we get passed.
979 static int if_condition (Token
*tline
, int i
)
982 Token
* t
, * tt
, ** tptr
, * origline
;
983 struct tokenval tokval
;
989 case PP_IFCTX
: case PP_ELIFCTX
:
990 case PP_IFNCTX
: case PP_ELIFNCTX
:
991 j
= FALSE
; /* have we matched yet? */
994 "`%s': context stack is empty", directives
[i
]);
997 if (!tline
|| tline
->type
!= TOK_ID
) {
999 "`%s' expects context identifiers", directives
[i
]);
1000 free_tlist (origline
);
1003 if (!nasm_stricmp(tline
->text
, cstk
->name
))
1005 tline
= tline
->next
;
1007 if (i
== PP_IFNCTX
|| i
== PP_ELIFNCTX
)
1009 free_tlist (origline
);
1012 case PP_IFDEF
: case PP_ELIFDEF
:
1013 case PP_IFNDEF
: case PP_ELIFNDEF
:
1014 j
= FALSE
; /* have we matched yet? */
1017 if (!tline
|| (tline
->type
!= TOK_ID
&&
1018 (tline
->type
!= TOK_PREPROC_ID
||
1019 tline
->text
[1] != '$'))) {
1021 "`%%if%sdef' expects macro identifiers",
1022 (i
==PP_ELIFNDEF
? "n" : ""));
1023 free_tlist (origline
);
1026 if (smacro_defined(tline
->text
, 0, NULL
, 1))
1028 tline
= tline
->next
;
1030 if (i
== PP_IFNDEF
|| i
== PP_ELIFNDEF
)
1032 free_tlist (origline
);
1035 case PP_IFIDN
: case PP_ELIFIDN
: case PP_IFNIDN
: case PP_ELIFNIDN
:
1036 case PP_IFIDNI
: case PP_ELIFIDNI
: case PP_IFNIDNI
: case PP_ELIFNIDNI
:
1037 tline
= expand_smacro(tline
);
1039 while (tok_isnt_(tt
, ","))
1042 error(ERR_NONFATAL
, "`%s' expects two comma-separated arguments");
1047 casesense
= (i
== PP_IFIDN
|| i
== PP_ELIFIDN
||
1048 i
== PP_IFNIDN
|| i
== PP_ELIFNIDN
);
1049 j
= TRUE
; /* assume equality unless proved not */
1050 while ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) && tt
) {
1051 if (tt
->type
== TOK_OTHER
&& !strcmp(tt
->text
, ",")) {
1052 error(ERR_NONFATAL
, "`%s': more than one comma on line",
1057 if (t
->type
== TOK_WHITESPACE
) {
1060 } else if (tt
->type
== TOK_WHITESPACE
) {
1063 } else if (tt
->type
!= t
->type
||
1064 (casesense
? strcmp(tt
->text
, t
->text
) :
1065 nasm_stricmp(tt
->text
, t
->text
))) {
1066 j
= FALSE
; /* found mismatching tokens */
1074 if ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) || tt
)
1075 j
= FALSE
; /* trailing gunk on one end or other */
1076 if (i
== PP_IFNIDN
|| i
== PP_ELIFNIDN
||
1077 i
== PP_IFNIDNI
|| i
== PP_ELIFNIDNI
)
1082 case PP_IFID
: case PP_ELIFID
: case PP_IFNID
: case PP_ELIFNID
:
1083 case PP_IFNUM
: case PP_ELIFNUM
: case PP_IFNNUM
: case PP_ELIFNNUM
:
1084 case PP_IFSTR
: case PP_ELIFSTR
: case PP_IFNSTR
: case PP_ELIFNSTR
:
1085 tline
= expand_smacro(tline
);
1087 while (tok_type_(t
, TOK_WHITESPACE
))
1089 j
= FALSE
; /* placate optimiser */
1091 case PP_IFID
: case PP_ELIFID
: case PP_IFNID
: case PP_ELIFNID
:
1092 j
= (t
->type
== TOK_ID
);
1094 case PP_IFNUM
: case PP_ELIFNUM
: case PP_IFNNUM
: case PP_ELIFNNUM
:
1095 j
= (t
->type
== TOK_NUMBER
);
1097 case PP_IFSTR
: case PP_ELIFSTR
: case PP_IFNSTR
: case PP_ELIFNSTR
:
1098 j
= (t
->type
== TOK_STRING
);
1101 if (i
== PP_IFNID
|| i
== PP_ELIFNID
||
1102 i
== PP_IFNNUM
|| i
== PP_ELIFNNUM
||
1103 i
== PP_IFNSTR
|| i
== PP_ELIFNSTR
)
1108 case PP_IF
: case PP_ELIF
:
1109 t
= tline
= expand_smacro(tline
);
1111 tokval
.t_type
= TOKEN_INVALID
;
1112 evalresult
= evaluate (ppscan
, tptr
, &tokval
,
1113 NULL
, pass
| 0x10, error
, NULL
);
1119 "trailing garbage after expression ignored");
1120 if (!is_simple(evalresult
)) {
1122 "non-constant value given to `%s'", directives
[i
]);
1125 return reloc_value(evalresult
) != 0;
1129 "preprocessor directive `%s' not yet implemented",
1131 free_tlist (origline
);
1132 return -1; /* yeah, right */
1137 * Find out if a line contains a preprocessor directive, and deal
1140 * If a directive _is_ found, we are expected to free_tlist() the
1143 * Return values go like this:
1145 * bit 0 is set if a directive was found (so the line gets freed)
1147 static int do_directive (Token
*tline
)
1149 int i
, j
, k
, m
, nparam
, nolist
;
1154 SMacro
*smac
, **smhead
;
1156 Token
*t
, *tt
, *param_start
, *macro_start
, *last
, **tptr
, *origline
;
1158 struct tokenval tokval
;
1160 MMacro
*tmp_defining
; /* Used when manipulating rep_nest */
1165 if (!tok_type_(tline
, TOK_PREPROC_ID
) ||
1166 (tline
->text
[1]=='%' || tline
->text
[1]=='$' || tline
->text
[1]=='!'))
1170 j
= sizeof(directives
)/sizeof(*directives
);
1173 m
= nasm_stricmp(tline
->text
, directives
[k
]);
1185 * If we're in a non-emitting branch of a condition construct,
1186 * or walking to the end of an already terminated %rep block,
1187 * we should ignore all directives except for condition
1190 if (((istk
->conds
&& !emitting(istk
->conds
->state
)) ||
1191 (istk
->mstk
&& !istk
->mstk
->in_progress
)) &&
1192 i
!= PP_IF
&& i
!= PP_ELIF
&&
1193 i
!= PP_IFCTX
&& i
!= PP_ELIFCTX
&&
1194 i
!= PP_IFDEF
&& i
!= PP_ELIFDEF
&&
1195 i
!= PP_IFID
&& i
!= PP_ELIFID
&&
1196 i
!= PP_IFIDN
&& i
!= PP_ELIFIDN
&&
1197 i
!= PP_IFIDNI
&& i
!= PP_ELIFIDNI
&&
1198 i
!= PP_IFNCTX
&& i
!= PP_ELIFNCTX
&&
1199 i
!= PP_IFNDEF
&& i
!= PP_ELIFNDEF
&&
1200 i
!= PP_IFNID
&& i
!= PP_ELIFNID
&&
1201 i
!= PP_IFNIDN
&& i
!= PP_ELIFNIDN
&&
1202 i
!= PP_IFNIDNI
&& i
!= PP_ELIFNIDNI
&&
1203 i
!= PP_IFNNUM
&& i
!= PP_ELIFNNUM
&&
1204 i
!= PP_IFNSTR
&& i
!= PP_ELIFNSTR
&&
1205 i
!= PP_IFNUM
&& i
!= PP_ELIFNUM
&&
1206 i
!= PP_IFSTR
&& i
!= PP_ELIFSTR
&&
1207 i
!= PP_ELSE
&& i
!= PP_ENDIF
)
1213 * If we're defining a macro or reading a %rep block, we should
1214 * ignore all directives except for %macro/%imacro (which
1215 * generate an error), %endm/%endmacro, and (only if we're in a
1216 * %rep block) %endrep. If we're in a %rep block, another %rep
1217 * causes an error, so should be let through.
1219 if (defining
&& i
!= PP_MACRO
&& i
!= PP_IMACRO
&&
1220 i
!= PP_ENDMACRO
&& i
!= PP_ENDM
&&
1221 (defining
->name
|| (i
!= PP_ENDREP
&& i
!= PP_REP
)))
1227 error(ERR_NONFATAL
, "unknown preprocessor directive `%s'",
1229 return 0; /* didn't get it */
1237 "trailing garbage after `%%clear' ignored");
1238 for (j
=0; j
<NHASH
; j
++) {
1239 while (mmacros
[j
]) {
1240 MMacro
*m
= mmacros
[j
];
1241 mmacros
[j
] = m
->next
;
1244 while (smacros
[j
]) {
1245 SMacro
*s
= smacros
[j
];
1246 smacros
[j
] = smacros
[j
]->next
;
1247 nasm_free (s
->name
);
1248 free_tlist (s
->expansion
);
1252 free_tlist (origline
);
1256 tline
= tline
->next
;
1258 if (!tline
|| (tline
->type
!= TOK_STRING
&&
1259 tline
->type
!= TOK_INTERNAL_STRING
))
1261 error(ERR_NONFATAL
, "`%%include' expects a file name");
1262 free_tlist (origline
);
1263 return 3; /* but we did _something_ */
1267 "trailing garbage after `%%include' ignored");
1268 if (tline
->type
!= TOK_INTERNAL_STRING
) {
1269 p
= tline
->text
+1; /* point past the quote to the name */
1270 p
[strlen(p
)-1] = '\0'; /* remove the trailing quote */
1272 p
= tline
->text
; /* internal_string is easier */
1273 inc
= nasm_malloc(sizeof(Include
));
1276 inc
->fp
= inc_fopen(p
);
1277 inc
->fname
= src_set_fname(nasm_strdup(p
));
1278 inc
->lineno
= src_set_linnum(0);
1280 inc
->expansion
= NULL
;
1283 list
->uplevel (LIST_INCLUDE
);
1284 free_tlist (origline
);
1288 tline
= tline
->next
;
1290 if (!tok_type_(tline
, TOK_ID
)) {
1292 "`%%push' expects a context identifier");
1293 free_tlist (origline
);
1294 return 3; /* but we did _something_ */
1298 "trailing garbage after `%%push' ignored");
1299 ctx
= nasm_malloc(sizeof(Context
));
1301 ctx
->localmac
= NULL
;
1302 ctx
->name
= nasm_strdup(tline
->text
);
1303 ctx
->number
= unique
++;
1305 free_tlist (origline
);
1309 tline
= tline
->next
;
1311 if (!tok_type_(tline
, TOK_ID
)) {
1313 "`%%repl' expects a context identifier");
1314 free_tlist (origline
);
1315 return 3; /* but we did _something_ */
1319 "trailing garbage after `%%repl' ignored");
1322 "`%%repl': context stack is empty");
1324 nasm_free (cstk
->name
);
1325 cstk
->name
= nasm_strdup(tline
->text
);
1327 free_tlist (origline
);
1333 "trailing garbage after `%%pop' ignored");
1336 "`%%pop': context stack is already empty");
1339 free_tlist (origline
);
1343 tline
->next
= expand_smacro (tline
->next
);
1344 tline
= tline
->next
;
1346 if (tok_type_(tline
, TOK_STRING
)) {
1347 p
= tline
->text
+1; /* point past the quote to the name */
1348 p
[strlen(p
)-1] = '\0'; /* remove the trailing quote */
1349 error(ERR_NONFATAL
, "user error: %s", p
);
1352 error(ERR_WARNING
, "user error: %s", p
);
1355 free_tlist (origline
);
1373 if (istk
->conds
&& !emitting(istk
->conds
->state
))
1376 j
= if_condition(tline
->next
, i
);
1377 tline
->next
= NULL
; /* it got freed */
1378 free_tlist (origline
);
1381 * Bogus expression in %if, but we should pretend
1382 * it was OK anyway, so that we don't get an error
1383 * cascade on the subsequent %else / %endif.
1387 j
= j
? COND_IF_TRUE
: COND_IF_FALSE
;
1389 cond
= nasm_malloc(sizeof(Cond
));
1390 cond
->next
= istk
->conds
;
1393 return (j
== COND_IF_TRUE
? 3 : 1);
1411 error(ERR_FATAL
, "`%s': no matching `%%if'",
1413 if (emitting(istk
->conds
->state
) || istk
->conds
->state
== COND_NEVER
)
1414 istk
->conds
->state
= COND_NEVER
;
1416 j
= if_condition(tline
->next
, i
);
1417 tline
->next
= NULL
; /* it got freed */
1418 free_tlist (origline
);
1421 * The expression was bogus, but let's make
1422 * %endif not complain about missing %if
1426 istk
->conds
->state
= j
? COND_IF_TRUE
: COND_IF_FALSE
;
1428 return (istk
->conds
->state
== COND_IF_TRUE
? 5 : 1);
1433 "trailing garbage after `%%else' ignored");
1436 "`%%else': no matching `%%if'");
1437 if (emitting(istk
->conds
->state
) || istk
->conds
->state
== COND_NEVER
)
1438 istk
->conds
->state
= COND_ELSE_FALSE
;
1440 istk
->conds
->state
= COND_ELSE_TRUE
;
1441 free_tlist (origline
);
1447 "trailing garbage after `%%endif' ignored");
1450 "`%%endif': no matching `%%if'");
1452 istk
->conds
= cond
->next
;
1454 free_tlist (origline
);
1461 "`%%%smacro': already defining a macro",
1462 (i
== PP_IMACRO
? "i" : ""));
1463 tline
= tline
->next
;
1465 if (!tok_type_(tline
, TOK_ID
)) {
1466 error (ERR_NONFATAL
,
1467 "`%%%smacro' expects a macro name",
1468 (i
== PP_IMACRO
? "i" : ""));
1471 defining
= nasm_malloc(sizeof(MMacro
));
1472 defining
->name
= nasm_strdup(tline
->text
);
1473 defining
->casesense
= (i
== PP_MACRO
);
1474 defining
->plus
= FALSE
;
1475 defining
->nolist
= FALSE
;
1476 defining
->in_progress
= FALSE
;
1477 defining
->rep_nest
= NULL
;
1478 tline
= tline
->next
;
1480 if (!tok_type_(tline
, TOK_NUMBER
)) {
1481 error (ERR_NONFATAL
,
1482 "`%%%smacro' expects a parameter count",
1483 (i
== PP_IMACRO
? "i" : ""));
1484 defining
->nparam_min
= defining
->nparam_max
= 0;
1486 defining
->nparam_min
= defining
->nparam_max
=
1487 readnum(tline
->text
, &j
);
1489 error (ERR_NONFATAL
,
1490 "unable to parse parameter count `%s'", tline
->text
);
1492 if (tline
&& tok_is_(tline
->next
, "-")) {
1493 tline
= tline
->next
->next
;
1494 if (tok_is_(tline
, "*"))
1495 defining
->nparam_max
= INT_MAX
;
1496 else if (!tok_type_(tline
, TOK_NUMBER
))
1497 error (ERR_NONFATAL
,
1498 "`%%%smacro' expects a parameter count after `-'",
1499 (i
== PP_IMACRO
? "i" : ""));
1501 defining
->nparam_max
= readnum(tline
->text
, &j
);
1503 error (ERR_NONFATAL
,
1504 "unable to parse parameter count `%s'",
1506 if (defining
->nparam_min
> defining
->nparam_max
)
1507 error (ERR_NONFATAL
,
1508 "minimum parameter count exceeds maximum");
1511 if (tline
&& tok_is_(tline
->next
, "+")) {
1512 tline
= tline
->next
;
1513 defining
->plus
= TRUE
;
1515 if (tline
&& tok_type_(tline
->next
, TOK_ID
) &&
1516 !nasm_stricmp(tline
->next
->text
, ".nolist"))
1518 tline
= tline
->next
;
1519 defining
->nolist
= TRUE
;
1521 mmac
= mmacros
[hash(defining
->name
)];
1523 if (!strcmp(mmac
->name
, defining
->name
) &&
1524 (mmac
->nparam_min
<=defining
->nparam_max
|| defining
->plus
) &&
1525 (defining
->nparam_min
<=mmac
->nparam_max
|| mmac
->plus
))
1528 "redefining multi-line macro `%s'", defining
->name
);
1534 * Handle default parameters.
1536 if (tline
&& tline
->next
) {
1537 defining
->dlist
= tline
->next
;
1539 count_mmac_params (defining
->dlist
, &defining
->ndefs
,
1540 &defining
->defaults
);
1542 defining
->dlist
= NULL
;
1543 defining
->defaults
= NULL
;
1545 defining
->expansion
= NULL
;
1546 free_tlist (origline
);
1552 error (ERR_NONFATAL
, "`%s': not defining a macro",
1556 k
= hash(defining
->name
);
1557 defining
->next
= mmacros
[k
];
1558 mmacros
[k
] = defining
;
1560 free_tlist (origline
);
1564 if (tline
->next
&& tline
->next
->type
== TOK_WHITESPACE
)
1565 tline
= tline
->next
;
1566 t
= expand_smacro(tline
->next
);
1568 free_tlist (origline
);
1571 tokval
.t_type
= TOKEN_INVALID
;
1572 evalresult
= evaluate (ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
1578 "trailing garbage after expression ignored");
1579 if (!is_simple(evalresult
)) {
1581 "non-constant value given to `%%rotate'");
1585 while (mmac
&& !mmac
->name
) /* avoid mistaking %reps for macros */
1586 mmac
= mmac
->next_active
;
1588 error(ERR_NONFATAL
, "`%%rotate' invoked outside a macro call");
1589 mmac
->rotate
= mmac
->rotate
+ reloc_value(evalresult
);
1590 if (mmac
->rotate
< 0)
1591 mmac
->rotate
= mmac
->nparam
- (-mmac
->rotate
) % mmac
->nparam
;
1592 mmac
->rotate
%= mmac
->nparam
;
1597 tline
= tline
->next
;
1598 if (tline
->next
&& tline
->next
->type
== TOK_WHITESPACE
)
1599 tline
= tline
->next
;
1600 if (tline
->next
&& tline
->next
->type
== TOK_ID
&&
1601 !nasm_stricmp(tline
->next
->text
, ".nolist")) {
1602 tline
= tline
->next
;
1605 t
= expand_smacro(tline
->next
);
1607 free_tlist (origline
);
1610 tokval
.t_type
= TOKEN_INVALID
;
1611 evalresult
= evaluate (ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
1617 "trailing garbage after expression ignored");
1618 if (!is_simple(evalresult
)) {
1620 "non-constant value given to `%%rep'");
1623 tmp_defining
= defining
;
1624 defining
= nasm_malloc(sizeof(MMacro
));
1625 defining
->name
= NULL
; /* flags this macro as a %rep block */
1626 defining
->casesense
= 0;
1627 defining
->plus
= FALSE
;
1628 defining
->nolist
= nolist
;
1629 defining
->in_progress
= reloc_value(evalresult
) + 1;
1630 defining
->nparam_min
= defining
->nparam_max
= 0;
1631 defining
->defaults
= NULL
;
1632 defining
->dlist
= NULL
;
1633 defining
->expansion
= NULL
;
1634 defining
->next_active
= istk
->mstk
;
1635 defining
->rep_nest
= tmp_defining
;
1639 if (!defining
|| defining
->name
) {
1640 error (ERR_NONFATAL
,
1641 "`%%endrep': no matching `%%rep'");
1646 * Now we have a "macro" defined - although it has no name
1647 * and we won't be entering it in the hash tables - we must
1648 * push a macro-end marker for it on to istk->expansion.
1649 * After that, it will take care of propagating itself (a
1650 * macro-end marker line for a macro which is really a %rep
1651 * block will cause the macro to be re-expanded, complete
1652 * with another macro-end marker to ensure the process
1653 * continues) until the whole expansion is forcibly removed
1654 * from istk->expansion by a %exitrep.
1656 l
= nasm_malloc(sizeof(Line
));
1657 l
->next
= istk
->expansion
;
1658 l
->finishes
= defining
;
1660 istk
->expansion
= l
;
1662 istk
->mstk
= defining
;
1664 list
->uplevel (defining
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
1665 tmp_defining
= defining
;
1666 defining
= defining
->rep_nest
;
1667 free_tlist (origline
);
1672 * We must search along istk->expansion until we hit a
1673 * macro-end marker for a macro with no name. Then we set
1674 * its `in_progress' flag to 0.
1676 for (l
= istk
->expansion
; l
; l
= l
->next
)
1677 if (l
->finishes
&& !l
->finishes
->name
)
1681 l
->finishes
->in_progress
= 0;
1683 error (ERR_NONFATAL
, "`%%exitrep' not within `%%rep' block");
1684 free_tlist (origline
);
1689 tline
= tline
->next
;
1691 if (!tline
|| (tline
->type
!= TOK_ID
&&
1692 (tline
->type
!= TOK_PREPROC_ID
||
1693 tline
->text
[1] != '$'))) {
1694 error (ERR_NONFATAL
,
1695 "`%%%sdefine' expects a macro identifier",
1696 (i
== PP_IDEFINE
? "i" : ""));
1697 free_tlist (origline
);
1700 mname
= tline
->text
;
1701 if (tline
->type
== TOK_ID
) {
1703 smhead
= &smacros
[hash(mname
)];
1705 ctx
= get_ctx (tline
->text
);
1710 p
+= strspn(p
, "$");
1711 smhead
= &ctx
->localmac
;
1715 param_start
= tline
= tline
->next
;
1717 if (tok_is_(tline
, "(")) {
1719 * This macro has parameters.
1722 tline
= tline
->next
;
1726 error (ERR_NONFATAL
,
1727 "parameter identifier expected");
1728 free_tlist (origline
);
1731 if (tline
->type
!= TOK_ID
) {
1732 error (ERR_NONFATAL
,
1733 "`%s': parameter identifier expected",
1735 free_tlist (origline
);
1738 tline
->type
= TOK_SMAC_PARAM
+ nparam
++;
1739 tline
= tline
->next
;
1741 if (tok_is_(tline
, ",")) {
1742 tline
= tline
->next
;
1745 if (!tok_is_(tline
, ")")) {
1746 error (ERR_NONFATAL
,
1747 "`)' expected to terminate macro template");
1748 free_tlist (origline
);
1754 tline
= tline
->next
;
1756 if (tok_type_(tline
, TOK_WHITESPACE
))
1757 last
= tline
, tline
= tline
->next
;
1762 if (t
->type
== TOK_ID
) {
1763 for (tt
= param_start
; tt
; tt
= tt
->next
)
1764 if (tt
->type
>= TOK_SMAC_PARAM
&&
1765 !strcmp(tt
->text
, t
->text
))
1769 t
->next
= macro_start
;
1774 * Good. We now have a macro name, a parameter count, and a
1775 * token list (in reverse order) for an expansion. We ought
1776 * to be OK just to create an SMacro, store it, and let
1777 * free_tlist have the rest of the line (which we have
1778 * carefully re-terminated after chopping off the expansion
1781 if (smacro_defined (mname
, nparam
, &smac
, i
==PP_DEFINE
)) {
1784 "single-line macro `%s' defined both with and"
1785 " without parameters", mname
);
1786 free_tlist (origline
);
1787 free_tlist (macro_start
);
1791 * We're redefining, so we have to take over an
1792 * existing SMacro structure. This means freeing
1793 * what was already in it.
1795 nasm_free (smac
->name
);
1796 free_tlist (smac
->expansion
);
1799 smac
= nasm_malloc(sizeof(SMacro
));
1800 smac
->next
= *smhead
;
1803 smac
->name
= nasm_strdup(p
);
1804 smac
->casesense
= (i
== PP_DEFINE
);
1805 smac
->nparam
= nparam
;
1806 smac
->expansion
= macro_start
;
1807 smac
->in_progress
= FALSE
;
1808 free_tlist (origline
);
1813 tline
= tline
->next
;
1815 if (!tline
|| (tline
->type
!= TOK_ID
&&
1816 (tline
->type
!= TOK_PREPROC_ID
||
1817 tline
->text
[1] != '$'))) {
1818 error (ERR_NONFATAL
,
1819 "`%%%sassign' expects a macro identifier",
1820 (i
== PP_IASSIGN
? "i" : ""));
1821 free_tlist (origline
);
1824 mname
= tline
->text
;
1825 if (tline
->type
== TOK_ID
) {
1827 smhead
= &smacros
[hash(mname
)];
1829 ctx
= get_ctx (tline
->text
);
1831 free_tlist (origline
);
1835 p
+= strspn(p
, "$");
1836 smhead
= &ctx
->localmac
;
1840 tline
= tline
->next
;
1843 tline
= expand_smacro (tline
);
1846 tokval
.t_type
= TOKEN_INVALID
;
1847 evalresult
= evaluate (ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
1850 free_tlist (origline
);
1856 "trailing garbage after expression ignored");
1858 if (!is_simple(evalresult
)) {
1860 "non-constant value given to `%%%sassign'",
1861 (i
== PP_IASSIGN
? "i" : ""));
1862 free_tlist (origline
);
1866 macro_start
= nasm_malloc(sizeof(*macro_start
));
1867 macro_start
->next
= NULL
;
1868 make_tok_num(macro_start
, reloc_value(evalresult
));
1869 macro_start
->mac
= NULL
;
1872 * We now have a macro name, an implicit parameter count of
1873 * zero, and a numeric token to use as an expansion. Create
1874 * and store an SMacro.
1876 if (smacro_defined (mname
, 0, &smac
, i
==PP_ASSIGN
)) {
1879 "single-line macro `%s' defined both with and"
1880 " without parameters", mname
);
1883 * We're redefining, so we have to take over an
1884 * existing SMacro structure. This means freeing
1885 * what was already in it.
1887 nasm_free (smac
->name
);
1888 free_tlist (smac
->expansion
);
1892 smac
= nasm_malloc(sizeof(SMacro
));
1893 smac
->next
= *smhead
;
1896 smac
->name
= nasm_strdup(p
);
1897 smac
->casesense
= (i
== PP_ASSIGN
);
1899 smac
->expansion
= macro_start
;
1900 smac
->in_progress
= FALSE
;
1901 free_tlist (origline
);
1906 * Syntax is `%line nnn[+mmm] [filename]'
1908 tline
= tline
->next
;
1910 if (!tok_type_(tline
, TOK_NUMBER
)) {
1911 error (ERR_NONFATAL
, "`%%line' expects line number");
1912 free_tlist (origline
);
1915 k
= readnum(tline
->text
, &j
);
1917 tline
= tline
->next
;
1918 if (tok_is_(tline
, "+")) {
1919 tline
= tline
->next
;
1920 if (!tok_type_(tline
, TOK_NUMBER
)) {
1921 error (ERR_NONFATAL
,
1922 "`%%line' expects line increment");
1923 free_tlist (origline
);
1926 m
= readnum(tline
->text
, &j
);
1927 tline
= tline
->next
;
1933 nasm_free ( src_set_fname ( detoken(tline
) ) );
1935 free_tlist (origline
);
1940 "preprocessor directive `%s' not yet implemented",
1948 * Ensure that a macro parameter contains a condition code and
1949 * nothing else. Return the condition code index if so, or -1
1952 static int find_cc (Token
*t
)
1958 if (t
->type
!= TOK_ID
)
1962 if (tt
&& (tt
->type
!= TOK_OTHER
|| strcmp(tt
->text
, ",")))
1966 j
= sizeof(conditions
)/sizeof(*conditions
);
1969 m
= nasm_stricmp(t
->text
, conditions
[k
]);
1985 * Expand MMacro-local things: parameter references (%0, %n, %+n,
1986 * %-n) and MMacro-local identifiers (%%foo).
1988 static Token
*expand_mmac_params (Token
*tline
)
1990 Token
*t
, *tt
, *ttt
, **tail
, *thead
;
1996 if (tline
->type
== TOK_PREPROC_ID
&&
1997 (tline
->text
[1] == '+' || tline
->text
[1] == '-' ||
1998 tline
->text
[1] == '%' ||
1999 (tline
->text
[1] >= '0' && tline
->text
[1] <= '9'))) {
2001 int type
= 0, cc
; /* type = 0 to placate optimisers */
2007 tline
= tline
->next
;
2010 while (mac
&& !mac
->name
) /* avoid mistaking %reps for macros */
2011 mac
= mac
->next_active
;
2013 error(ERR_NONFATAL
, "`%s': not in a macro call", t
->text
);
2014 else switch (t
->text
[1]) {
2016 * We have to make a substitution of one of the
2017 * forms %1, %-1, %+1, %%foo, %0.
2021 sprintf(tmpbuf
, "%d", mac
->nparam
);
2022 text
= nasm_strdup(tmpbuf
);
2026 sprintf(tmpbuf
, "..@%lu.", mac
->unique
);
2027 text
= nasm_strcat(tmpbuf
, t
->text
+2);
2030 n
= atoi(t
->text
+2)-1;
2031 if (n
>= mac
->nparam
)
2034 if (mac
->nparam
> 1)
2035 n
= (n
+ mac
->rotate
) % mac
->nparam
;
2036 tt
= mac
->params
[n
];
2040 error (ERR_NONFATAL
,
2041 "macro parameter %d is not a condition code",
2046 if (inverse_ccs
[cc
] == -1) {
2047 error (ERR_NONFATAL
,
2048 "condition code `%s' is not invertible",
2052 text
= nasm_strdup(conditions
[inverse_ccs
[cc
]]);
2056 n
= atoi(t
->text
+2)-1;
2057 if (n
>= mac
->nparam
)
2060 if (mac
->nparam
> 1)
2061 n
= (n
+ mac
->rotate
) % mac
->nparam
;
2062 tt
= mac
->params
[n
];
2066 error (ERR_NONFATAL
,
2067 "macro parameter %d is not a condition code",
2072 text
= nasm_strdup(conditions
[cc
]);
2076 n
= atoi(t
->text
+1)-1;
2077 if (n
>= mac
->nparam
)
2080 if (mac
->nparam
> 1)
2081 n
= (n
+ mac
->rotate
) % mac
->nparam
;
2082 tt
= mac
->params
[n
];
2085 for (i
=0; i
<mac
->paramlen
[n
]; i
++) {
2086 ttt
= *tail
= nasm_malloc(sizeof(Token
));
2088 ttt
->type
= tt
->type
;
2089 ttt
->text
= nasm_strdup(tt
->text
);
2094 text
= NULL
; /* we've done it here */
2097 nasm_free (t
->text
);
2110 tline
= tline
->next
;
2117 for (; t
&& (tt
=t
->next
)!=NULL
; t
= t
->next
)
2119 case TOK_WHITESPACE
:
2120 if (tt
->type
== TOK_WHITESPACE
) {
2122 nasm_free(tt
->text
);
2127 if (tt
->type
== TOK_ID
|| tt
->type
== TOK_NUMBER
) {
2128 char *tmp
= nasm_strcat(t
->text
, tt
->text
);
2132 nasm_free(tt
->text
);
2137 if (tt
->type
== TOK_NUMBER
) {
2138 char *tmp
= nasm_strcat(t
->text
, tt
->text
);
2142 nasm_free(tt
->text
);
2152 * Expand all single-line macro calls made in the given line.
2153 * Return the expanded version of the line. The original is deemed
2154 * to be destroyed in the process. (In reality we'll just move
2155 * Tokens from input to output a lot of the time, rather than
2156 * actually bothering to destroy and replicate.)
2158 static Token
*expand_smacro (Token
*tline
)
2160 Token
*t
, *tt
, *mstart
, **tail
, *thead
;
2161 SMacro
*head
= NULL
, *m
;
2164 int nparam
, sparam
, brackets
;
2170 while (tline
) { /* main token loop */
2172 if (tline
->type
== TOK_ID
) {
2173 head
= smacros
[hash(tline
->text
)];
2175 } else if (tline
->type
== TOK_PREPROC_ID
&& tline
->text
[1] == '$') {
2176 Context
*ctx
= get_ctx (tline
->text
);
2178 head
= ctx
->localmac
;
2180 p
+= strspn(p
, "$");
2185 * We've hit an identifier. As in is_mmacro below, we first
2186 * check whether the identifier is a single-line macro at
2187 * all, then think about checking for parameters if
2190 for (m
= head
; m
; m
= m
->next
)
2191 if (!mstrcmp(m
->name
, p
, m
->casesense
))
2197 if (m
->nparam
== 0) {
2199 * Simple case: the macro is parameterless. Discard the
2200 * one token that the macro call took, and push the
2201 * expansion back on the to-do stack.
2205 if (!strcmp("__FILE__", m
->name
)) {
2207 src_get(&num
, &(tline
->text
));
2208 nasm_quote(&(tline
->text
));
2209 tline
->type
= TOK_STRING
;
2212 if (!strcmp("__LINE__", m
->name
)) {
2213 nasm_free(tline
->text
);
2214 make_tok_num(tline
, src_get_linnum());
2218 tline
= tline
->next
;
2219 nasm_free (t
->text
);
2226 * Complicated case: at least one macro with this name
2227 * exists and takes parameters. We must find the
2228 * parameters in the call, count them, find the SMacro
2229 * that corresponds to that form of the macro call, and
2230 * substitute for the parameters when we expand. What a
2233 tline
= tline
->next
;
2235 if (!tok_is_(tline
, "(")) {
2237 * This macro wasn't called with parameters: ignore
2238 * the call. (Behaviour borrowed from gnu cpp.)
2248 tline
= tline
->next
;
2249 sparam
= PARAM_DELTA
;
2250 params
= nasm_malloc (sparam
*sizeof(Token
*));
2252 paramsize
= nasm_malloc (sparam
*sizeof(int));
2254 for (;;tline
= tline
->next
) { /* parameter loop */
2257 "macro call expects terminating `)'");
2260 if (tline
->type
== TOK_WHITESPACE
&& brackets
<=0) {
2261 if (paramsize
[nparam
])
2264 params
[nparam
] = tline
->next
;
2265 continue; /* parameter loop */
2267 if (tline
->type
== TOK_OTHER
&& tline
->text
[1]==0) {
2268 char ch
= tline
->text
[0];
2269 if (ch
== ',' && !paren
&& brackets
<=0) {
2270 if (++nparam
>= sparam
) {
2271 sparam
+= PARAM_DELTA
;
2272 params
= nasm_realloc (params
,
2273 sparam
*sizeof(Token
*));
2274 paramsize
= nasm_realloc (paramsize
,
2275 sparam
*sizeof(int));
2277 params
[nparam
] = tline
->next
;
2278 paramsize
[nparam
] = 0;
2280 continue; /* parameter loop */
2283 (brackets
>0 || (brackets
==0 &&
2284 !paramsize
[nparam
])))
2288 params
[nparam
] = tline
->next
;
2289 continue; /* parameter loop */
2292 if (ch
== br2
&& brackets
>0)
2293 if (--brackets
== 0) {
2295 continue; /* parameter loop */
2297 if (ch
== '(' && !brackets
)
2299 if (ch
== ')' && brackets
<=0)
2305 error (ERR_NONFATAL
, "braces do not "
2306 "enclose all of macro parameter");
2308 paramsize
[nparam
] += white
+1;
2310 } /* parameter loop */
2312 while (m
&& (m
->nparam
!= nparam
||
2313 mstrcmp(m
->name
, p
, m
->casesense
)))
2316 error (ERR_WARNING
|ERR_WARN_MNP
,
2317 "macro `%s' exists, "
2318 "but not taking %d parameters",
2319 mstart
->text
, nparam
);
2322 if (m
&& m
->in_progress
)
2324 if (!m
) /* in progess or didn't find '(' or wrong nparam */
2327 * Design question: should we handle !tline, which
2328 * indicates missing ')' here, or expand those
2329 * macros anyway, which requires the (t) test a few
2333 nasm_free (paramsize
);
2338 * Expand the macro: we are placed on the last token of the
2339 * call, so that we can easily split the call from the
2340 * following tokens. We also start by pushing an SMAC_END
2341 * token for the cycle removal.
2348 tt
= nasm_malloc(sizeof(Token
));
2349 tt
->type
= TOK_SMAC_END
;
2352 m
->in_progress
= TRUE
;
2355 for (t
= m
->expansion
; t
; t
= t
->next
) {
2356 if (t
->type
>= TOK_SMAC_PARAM
) {
2357 Token
*pcopy
= tline
, **ptail
= &pcopy
;
2361 ttt
= params
[t
->type
- TOK_SMAC_PARAM
];
2362 for (i
=paramsize
[t
->type
-TOK_SMAC_PARAM
]; --i
>=0;) {
2363 pt
= *ptail
= nasm_malloc(sizeof(Token
));
2366 pt
->text
= nasm_strdup(ttt
->text
);
2367 pt
->type
= ttt
->type
;
2373 tt
= nasm_malloc(sizeof(Token
));
2375 tt
->text
= nasm_strdup(t
->text
);
2383 * Having done that, get rid of the macro call, and clean
2384 * up the parameters.
2387 nasm_free (paramsize
);
2388 free_tlist (mstart
);
2389 continue; /* main token loop */
2394 if (tline
->type
== TOK_SMAC_END
) {
2395 tline
->mac
->in_progress
= FALSE
;
2397 tline
= tline
->next
;
2401 tline
= tline
->next
;
2405 if (t
->type
== TOK_PREPROC_ID
&& t
->text
[1] == '$') {
2406 Context
*c
= get_ctx (t
->text
);
2407 char *p
, *q
, buffer
[40];
2412 q
+= strspn(q
, "$");
2413 sprintf(buffer
, "..@%lu.", c
->number
);
2414 p
= nasm_strcat (buffer
,q
);
2415 nasm_free (t
->text
);
2426 * Determine whether the given line constitutes a multi-line macro
2427 * call, and return the MMacro structure called if so. Doesn't have
2428 * to check for an initial label - that's taken care of in
2429 * expand_mmacro - but must check numbers of parameters. Guaranteed
2430 * to be called with tline->type == TOK_ID, so the putative macro
2431 * name is easy to find.
2433 static MMacro
*is_mmacro (Token
*tline
, Token
***params_array
)
2439 head
= mmacros
[hash(tline
->text
)];
2442 * Efficiency: first we see if any macro exists with the given
2443 * name. If not, we can return NULL immediately. _Then_ we
2444 * count the parameters, and then we look further along the
2445 * list if necessary to find the proper MMacro.
2447 for (m
= head
; m
; m
= m
->next
)
2448 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
2454 * OK, we have a potential macro. Count and demarcate the
2457 count_mmac_params (tline
->next
, &nparam
, ¶ms
);
2460 * So we know how many parameters we've got. Find the MMacro
2461 * structure that handles this number.
2464 if (m
->nparam_min
<= nparam
&& (m
->plus
|| nparam
<= m
->nparam_max
)) {
2466 * This one is right. Just check if cycle removal
2467 * prohibits us using it before we actually celebrate...
2469 if (m
->in_progress
) {
2471 error (ERR_NONFATAL
,
2472 "self-reference in multi-line macro `%s'",
2479 * It's right, and we can use it. Add its default
2480 * parameters to the end of our list if necessary.
2482 if (m
->defaults
&& nparam
< m
->nparam_min
+ m
->ndefs
) {
2483 params
= nasm_realloc (params
, ((m
->nparam_min
+m
->ndefs
+1) *
2485 while (nparam
< m
->nparam_min
+ m
->ndefs
) {
2486 params
[nparam
] = m
->defaults
[nparam
- m
->nparam_min
];
2491 * If we've gone over the maximum parameter count (and
2492 * we're in Plus mode), ignore parameters beyond
2495 if (m
->plus
&& nparam
> m
->nparam_max
)
2496 nparam
= m
->nparam_max
;
2498 * Then terminate the parameter list, and leave.
2500 if (!params
) { /* need this special case */
2501 params
= nasm_malloc(sizeof(*params
));
2504 params
[nparam
] = NULL
;
2505 *params_array
= params
;
2509 * This one wasn't right: look for the next one with the
2512 for (m
= m
->next
; m
; m
= m
->next
)
2513 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
2518 * After all that, we didn't find one with the right number of
2519 * parameters. Issue a warning, and fail to expand the macro.
2521 error (ERR_WARNING
|ERR_WARN_MNP
,
2522 "macro `%s' exists, but not taking %d parameters",
2523 tline
->text
, nparam
);
2529 * Expand the multi-line macro call made by the given line, if
2530 * there is one to be expanded. If there is, push the expansion on
2531 * istk->expansion and return 1. Otherwise return 0.
2533 static int expand_mmacro (Token
*tline
)
2535 Token
*startline
= tline
;
2536 Token
*label
= NULL
;
2537 int dont_prepend
= 0;
2538 Token
**params
, *t
, *tt
;
2541 int i
, nparam
, *paramlen
;
2545 if (!tok_type_(t
, TOK_ID
))
2547 m
= is_mmacro (t
, ¶ms
);
2551 * We have an id which isn't a macro call. We'll assume
2552 * it might be a label; we'll also check to see if a
2553 * colon follows it. Then, if there's another id after
2554 * that lot, we'll check it again for macro-hood.
2558 if (tok_type_(t
, TOK_WHITESPACE
))
2559 last
= t
, t
= t
->next
;
2560 if (tok_is_(t
, ":")) {
2562 last
= t
, t
= t
->next
;
2563 if (tok_type_(t
, TOK_WHITESPACE
))
2564 last
= t
, t
= t
->next
;
2566 if (!tok_type_(t
, TOK_ID
) || (m
= is_mmacro(t
, ¶ms
)) == NULL
)
2573 * Fix up the parameters: this involves stripping leading and
2574 * trailing whitespace, then stripping braces if they are
2577 for (nparam
= 0; params
[nparam
]; nparam
++)
2579 paramlen
= nparam
? nasm_malloc(nparam
*sizeof(*paramlen
)) : NULL
;
2581 for (i
= 0; params
[i
]; i
++) {
2583 int comma
= (!m
->plus
|| i
< nparam
-1);
2587 if (tok_is_(t
, "{"))
2588 t
= t
->next
, brace
= TRUE
, comma
= FALSE
;
2592 if (comma
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, ","))
2593 break; /* ... because we have hit a comma */
2594 if (comma
&& t
->type
== TOK_WHITESPACE
&& tok_is_(t
->next
, ","))
2595 break; /* ... or a space then a comma */
2596 if (brace
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, "}"))
2597 break; /* ... or a brace */
2604 * OK, we have a MMacro structure together with a set of
2605 * parameters. We must now go through the expansion and push
2606 * copies of each Line on to istk->expansion. Substitution of
2607 * parameter tokens and macro-local tokens doesn't get done
2608 * until the single-line macro substitution process; this is
2609 * because delaying them allows us to change the semantics
2610 * later through %rotate.
2612 * First, push an end marker on to istk->expansion, mark this
2613 * macro as in progress, and set up its invocation-specific
2616 ll
= nasm_malloc(sizeof(Line
));
2617 ll
->next
= istk
->expansion
;
2620 istk
->expansion
= ll
;
2622 m
->in_progress
= TRUE
;
2627 m
->paramlen
= paramlen
;
2628 m
->unique
= unique
++;
2630 m
->next_active
= istk
->mstk
;
2633 for (l
= m
->expansion
; l
; l
= l
->next
) {
2636 ll
= nasm_malloc(sizeof(Line
));
2637 ll
->finishes
= NULL
;
2638 ll
->next
= istk
->expansion
;
2639 istk
->expansion
= ll
;
2642 for (t
= l
->first
; t
; t
= t
->next
) {
2644 if (t
->type
== TOK_PREPROC_ID
&&
2645 t
->text
[1]=='0' && t
->text
[2]=='0')
2652 tt
= *tail
= nasm_malloc(sizeof(Token
));
2655 tt
->text
= nasm_strdup(x
->text
);
2662 * If we had a label, push it on as the first line of
2663 * the macro expansion.
2667 free_tlist(startline
);
2669 ll
= nasm_malloc(sizeof(Line
));
2670 ll
->finishes
= NULL
;
2671 ll
->next
= istk
->expansion
;
2672 istk
->expansion
= ll
;
2673 ll
->first
= startline
;
2674 if (!dont_prepend
) {
2676 label
= label
->next
;
2677 label
->next
= tt
= nasm_malloc(sizeof(Token
));
2680 tt
->type
= TOK_OTHER
;
2681 tt
->text
= nasm_strdup(":");
2685 list
->uplevel (m
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
2690 static void pp_reset (char *file
, int apass
, efunc errfunc
, evalfunc eval
,
2697 istk
= nasm_malloc(sizeof(Include
));
2700 istk
->expansion
= NULL
;
2702 istk
->fp
= fopen(file
, "r");
2704 src_set_fname(nasm_strdup(file
));
2708 error (ERR_FATAL
|ERR_NOFILE
, "unable to open input file `%s'", file
);
2710 for (h
=0; h
<NHASH
; h
++) {
2716 any_extrastdmac
= (extrastdmac
!= NULL
);
2722 static char *pp_getline (void)
2730 * Fetch a tokenised line, either from the macro-expansion
2731 * buffer or from the input file.
2734 while (istk
->expansion
&& istk
->expansion
->finishes
) {
2735 Line
*l
= istk
->expansion
;
2736 if (!l
->finishes
->name
&& l
->finishes
->in_progress
> 1) {
2740 * This is a macro-end marker for a macro with no
2741 * name, which means it's not really a macro at all
2742 * but a %rep block, and the `in_progress' field is
2743 * more than 1, meaning that we still need to
2744 * repeat. (1 means the natural last repetition; 0
2745 * means termination by %exitrep.) We have
2746 * therefore expanded up to the %endrep, and must
2747 * push the whole block on to the expansion buffer
2748 * again. We don't bother to remove the macro-end
2749 * marker: we'd only have to generate another one
2752 l
->finishes
->in_progress
--;
2753 for (l
= l
->finishes
->expansion
; l
; l
= l
->next
) {
2754 Token
*t
, *tt
, **tail
;
2756 ll
= nasm_malloc(sizeof(Line
));
2757 ll
->next
= istk
->expansion
;
2758 ll
->finishes
= NULL
;
2762 for (t
= l
->first
; t
; t
= t
->next
) {
2764 tt
= *tail
= nasm_malloc(sizeof(Token
));
2768 tt
->text
= nasm_strdup(t
->text
);
2773 istk
->expansion
= ll
;
2777 * Check whether a `%rep' was started and not ended
2778 * within this macro expansion. This can happen and
2779 * should be detected. It's a fatal error because
2780 * I'm too confused to work out how to recover
2786 "defining with name in expansion");
2787 else if (istk
->mstk
->name
)
2788 error (ERR_FATAL
, "`%%rep' without `%%endrep' within"
2789 " expansion of macro `%s'", istk
->mstk
->name
);
2793 * FIXME: investigate the relationship at this point between
2794 * istk->mstk and l->finishes
2797 MMacro
*m
= istk
->mstk
;
2798 istk
->mstk
= m
->next_active
;
2801 * This was a real macro call, not a %rep, and
2802 * therefore the parameter information needs to
2805 nasm_free(m
->params
);
2806 free_tlist(m
->iline
);
2807 nasm_free(m
->paramlen
);
2808 l
->finishes
->in_progress
= FALSE
;
2813 istk
->expansion
= l
->next
;
2815 list
->downlevel (LIST_MACRO
);
2818 while (1) { /* until we get a line we can use */
2820 if (istk
->expansion
) { /* from a macro expansion */
2822 Line
*l
= istk
->expansion
;
2824 istk
->expansion
= l
->next
;
2827 list
->line (LIST_MACRO
, p
);
2832 if (line
) { /* from the current input file */
2833 line
= prepreproc(line
);
2834 tline
= tokenise(line
);
2839 * The current file has ended; work down the istk
2845 error(ERR_FATAL
, "expected `%%endif' before end of file");
2847 list
->downlevel (LIST_INCLUDE
);
2848 src_set_linnum(i
->lineno
);
2849 nasm_free ( src_set_fname(i
->fname
) );
2857 * We must expand MMacro parameters and MMacro-local labels
2858 * _before_ we plunge into directive processing, to cope
2859 * with things like `%define something %1' such as STRUC
2860 * uses. Unless we're _defining_ a MMacro, in which case
2861 * those tokens should be left alone to go into the
2862 * definition; and unless we're in a non-emitting
2863 * condition, in which case we don't want to meddle with
2866 if (!defining
&& !(istk
->conds
&& !emitting(istk
->conds
->state
)))
2867 tline
= expand_mmac_params(tline
);
2870 * Check the line to see if it's a preprocessor directive.
2872 ret
= do_directive(tline
);
2875 } else if (defining
) {
2877 * We're defining a multi-line macro. We emit nothing
2879 * shove the tokenised line on to the macro definition.
2881 Line
*l
= nasm_malloc(sizeof(Line
));
2882 l
->next
= defining
->expansion
;
2884 l
->finishes
= FALSE
;
2885 defining
->expansion
= l
;
2887 } else if (istk
->conds
&& !emitting(istk
->conds
->state
)) {
2889 * We're in a non-emitting branch of a condition block.
2890 * Emit nothing at all, not even a blank line: when we
2891 * emerge from the condition we'll give a line-number
2892 * directive so we keep our place correctly.
2896 } else if (istk
->mstk
&& !istk
->mstk
->in_progress
) {
2898 * We're in a %rep block which has been terminated, so
2899 * we're walking through to the %endrep without
2900 * emitting anything. Emit nothing at all, not even a
2901 * blank line: when we emerge from the %rep block we'll
2902 * give a line-number directive so we keep our place
2908 tline
= expand_smacro(tline
);
2909 ret
= expand_mmacro(tline
);
2912 * De-tokenise the line again, and emit it.
2914 line
= detoken(tline
);
2918 continue; /* expand_mmacro calls free_tlist */
2926 static void pp_cleanup (void)
2931 error (ERR_NONFATAL
, "end of file while still defining macro `%s'",
2933 free_mmacro (defining
);
2937 for (h
=0; h
<NHASH
; h
++) {
2938 while (mmacros
[h
]) {
2939 MMacro
*m
= mmacros
[h
];
2940 mmacros
[h
] = mmacros
[h
]->next
;
2943 while (smacros
[h
]) {
2944 SMacro
*s
= smacros
[h
];
2945 smacros
[h
] = smacros
[h
]->next
;
2946 nasm_free (s
->name
);
2947 free_tlist (s
->expansion
);
2955 nasm_free (i
->fname
);
2962 void pp_include_path (char *path
)
2966 i
= nasm_malloc(sizeof(IncPath
));
2967 i
->path
= nasm_strdup(path
);
2973 void pp_pre_include (char *fname
)
2975 Token
*inc
, *space
, *name
;
2978 inc
= nasm_malloc(sizeof(Token
));
2979 inc
->next
= space
= nasm_malloc(sizeof(Token
));
2980 space
->next
= name
= nasm_malloc(sizeof(Token
));
2983 inc
->type
= TOK_PREPROC_ID
;
2984 inc
->text
= nasm_strdup("%include");
2985 space
->type
= TOK_WHITESPACE
;
2986 space
->text
= nasm_strdup(" ");
2987 name
->type
= TOK_INTERNAL_STRING
;
2988 name
->text
= nasm_strdup(fname
);
2990 inc
->mac
= space
->mac
= name
->mac
= NULL
;
2992 l
= nasm_malloc(sizeof(Line
));
2995 l
->finishes
= FALSE
;
2999 void pp_pre_define (char *definition
)
3005 equals
= strchr(definition
, '=');
3007 def
= nasm_malloc(sizeof(Token
));
3008 def
->next
= space
= nasm_malloc(sizeof(Token
));
3011 space
->next
= tokenise(definition
);
3015 def
->type
= TOK_PREPROC_ID
;
3016 def
->text
= nasm_strdup("%define");
3017 space
->type
= TOK_WHITESPACE
;
3018 space
->text
= nasm_strdup(" ");
3020 def
->mac
= space
->mac
= NULL
;
3022 l
= nasm_malloc(sizeof(Line
));
3025 l
->finishes
= FALSE
;
3029 void pp_extra_stdmac (char **macros
)
3031 extrastdmac
= macros
;
3034 static void make_tok_num(Token
*tok
, long val
)
3037 sprintf(numbuf
, "%ld", val
);
3038 tok
->text
= nasm_strdup(numbuf
);
3039 tok
->type
= TOK_NUMBER
;