1 /* -*- mode: c; c-file-style: "bsd" -*- */
2 /* preproc.c macro preprocessor for the Netwide Assembler
4 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
5 * Julian Hall. All rights reserved. The software is
6 * redistributable under the licence given in the file "Licence"
7 * distributed in the NASM archive.
9 * initial version 18/iii/97 by Simon Tatham
12 /* Typical flow of text through preproc
14 * pp_getline gets tokenised lines, either
16 * from a macro expansion
20 * read_line gets raw text from stdmacpos, or predef, or current input file
21 * tokenise converts to tokens
24 * expand_mmac_params is used to expand %1 etc., unless a macro is being
25 * defined or a false conditional is being processed
26 * (%0, %1, %+1, %-1, %%foo
28 * do_directive checks for directives
30 * expand_smacro is used to expand single line macros
32 * expand_mmacro is used to expand multi-line macros
34 * detoken is used to convert the line back to text
48 typedef struct SMacro SMacro
;
49 typedef struct MMacro MMacro
;
50 typedef struct Context Context
;
51 typedef struct Token Token
;
52 typedef struct Blocks Blocks
;
53 typedef struct Line Line
;
54 typedef struct Include Include
;
55 typedef struct Cond Cond
;
56 typedef struct IncPath IncPath
;
59 * Store the definition of a single-line macro.
72 * Store the definition of a multi-line macro. This is also used to
73 * store the interiors of `%rep...%endrep' blocks, which are
74 * effectively self-re-invoking multi-line macros which simply
75 * don't have a name or bother to appear in the hash tables. %rep
76 * blocks are signified by having a NULL `name' field.
78 * In a MMacro describing a `%rep' block, the `in_progress' field
79 * isn't merely boolean, but gives the number of repeats left to
82 * The `next' field is used for storing MMacros in hash tables; the
83 * `next_active' field is for stacking them on istk entries.
85 * When a MMacro is being expanded, `params', `iline', `nparam',
86 * `paramlen', `rotate' and `unique' are local to the invocation.
93 int nparam_min
, nparam_max
;
94 int plus
; /* is the last parameter greedy? */
95 int nolist
; /* is this macro listing-inhibited? */
97 Token
*dlist
; /* All defaults as one list */
98 Token
**defaults
; /* Parameter default pointers */
99 int ndefs
; /* number of default parameters */
103 MMacro
*rep_nest
; /* used for nesting %rep */
104 Token
**params
; /* actual parameters */
105 Token
*iline
; /* invocation line */
106 int nparam
, rotate
, *paramlen
;
107 unsigned long unique
;
108 int lineno
; /* Current line number on expansion */
112 * The context stack is composed of a linked list of these.
119 unsigned long number
;
123 * This is the internal form which we break input lines up into.
124 * Typically stored in linked lists.
126 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
127 * necessarily used as-is, but is intended to denote the number of
128 * the substituted parameter. So in the definition
130 * %define a(x,y) ( (x) & ~(y) )
132 * the token representing `x' will have its type changed to
133 * TOK_SMAC_PARAM, but the one representing `y' will be
136 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
137 * which doesn't need quotes around it. Used in the pre-include
138 * mechanism as an alternative to trying to find a sensible type of
139 * quote to use on the filename we were passed.
145 SMacro
*mac
; /* associated macro for TOK_SMAC_END */
150 TOK_WHITESPACE
= 1, TOK_COMMENT
, TOK_ID
, TOK_PREPROC_ID
, TOK_STRING
,
151 TOK_NUMBER
, TOK_SMAC_END
, TOK_OTHER
, TOK_SMAC_PARAM
,
156 * Multi-line macro definitions are stored as a linked list of
157 * these, which is essentially a container to allow several linked
160 * Note that in this module, linked lists are treated as stacks
161 * wherever possible. For this reason, Lines are _pushed_ on to the
162 * `expansion' field in MMacro structures, so that the linked list,
163 * if walked, would give the macro lines in reverse order; this
164 * means that we can walk the list when expanding a macro, and thus
165 * push the lines on to the `expansion' field in _istk_ in reverse
166 * order (so that when popped back off they are in the right
167 * order). It may seem cockeyed, and it relies on my design having
168 * an even number of steps in, but it works...
170 * Some of these structures, rather than being actual lines, are
171 * markers delimiting the end of the expansion of a given macro.
172 * This is for use in the cycle-tracking and %rep-handling code.
173 * Such structures have `finishes' non-NULL, and `first' NULL. All
174 * others have `finishes' NULL, but `first' may still be NULL if
185 * To handle an arbitrary level of file inclusion, we maintain a
186 * stack (ie linked list) of these things.
196 MMacro
*mstk
; /* stack of active macros/reps */
200 * Include search path. This is simply a list of strings which get
201 * prepended, in turn, to the name of an include file, in an
202 * attempt to find the file if it's not in the current directory.
211 * Conditional assembly: we maintain a separate stack of these for
212 * each level of file inclusion. (The only reason we keep the
213 * stacks separate is to ensure that a stray `%endif' in a file
214 * included from within the true branch of a `%if' won't terminate
215 * it and cause confusion: instead, rightly, it'll cause an error.)
225 * These states are for use just after %if or %elif: IF_TRUE
226 * means the condition has evaluated to truth so we are
227 * currently emitting, whereas IF_FALSE means we are not
228 * currently emitting but will start doing so if a %else comes
229 * up. In these states, all directives are admissible: %elif,
230 * %else and %endif. (And of course %if.)
232 COND_IF_TRUE
, COND_IF_FALSE
,
234 * These states come up after a %else: ELSE_TRUE means we're
235 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
236 * any %elif or %else will cause an error.
238 COND_ELSE_TRUE
, COND_ELSE_FALSE
,
240 * This state means that we're not emitting now, and also that
241 * nothing until %endif will be emitted at all. It's for use in
242 * two circumstances: (i) when we've had our moment of emission
243 * and have now started seeing %elifs, and (ii) when the
244 * condition construct in question is contained within a
245 * non-emitting branch of a larger condition construct.
249 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
252 * Condition codes. Note that we use c_ prefix not C_ because C_ is
253 * used in nasm.h for the "real" condition codes. At _this_ level,
254 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
255 * ones, so we need a different enum...
257 static const char *conditions
[] = {
258 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
259 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
260 "np", "ns", "nz", "o", "p", "pe", "po", "s", "z"
264 c_A
, c_AE
, c_B
, c_BE
, c_C
, c_CXZ
, c_E
, c_ECXZ
, c_G
, c_GE
, c_L
, c_LE
,
265 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, c_NE
, c_NG
, c_NGE
, c_NL
, c_NLE
, c_NO
,
266 c_NP
, c_NS
, c_NZ
, c_O
, c_P
, c_PE
, c_PO
, c_S
, c_Z
268 static int inverse_ccs
[] = {
269 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, -1, c_NE
, -1, c_NG
, c_NGE
, c_NL
, c_NLE
,
270 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
,
271 c_Z
, c_NO
, c_NP
, c_PO
, c_PE
, c_NS
, c_NZ
277 static const char *directives
[] = {
279 "%assign", "%clear", "%define", "%elif", "%elifctx", "%elifdef",
280 "%elifid", "%elifidn", "%elifidni", "%elifmacro", "%elifnctx", "%elifndef",
281 "%elifnid", "%elifnidn", "%elifnidni", "%elifnmacro", "%elifnnum", "%elifnstr",
282 "%elifnum", "%elifstr", "%else", "%endif", "%endm", "%endmacro",
283 "%endrep", "%error", "%exitrep", "%iassign", "%idefine", "%if",
284 "%ifctx", "%ifdef", "%ifid", "%ifidn", "%ifidni", "%ifmacro", "%ifnctx",
285 "%ifndef", "%ifnid", "%ifnidn", "%ifnidni", "%ifnmacro", "%ifnnum",
286 "%ifnstr", "%ifnum", "%ifstr", "%imacro", "%include",
287 "%ixdefine", "%line",
289 "%macro", "%pop", "%push", "%rep", "%repl", "%rotate",
291 "%strlen", "%substr", "%undef", "%xdefine"
296 PP_ASSIGN
, PP_CLEAR
, PP_DEFINE
, PP_ELIF
, PP_ELIFCTX
, PP_ELIFDEF
,
297 PP_ELIFID
, PP_ELIFIDN
, PP_ELIFIDNI
, PP_ELIFMACRO
, PP_ELIFNCTX
, PP_ELIFNDEF
,
298 PP_ELIFNID
, PP_ELIFNIDN
, PP_ELIFNIDNI
, PP_ELIFNMACRO
, PP_ELIFNNUM
, PP_ELIFNSTR
,
299 PP_ELIFNUM
, PP_ELIFSTR
, PP_ELSE
, PP_ENDIF
, PP_ENDM
, PP_ENDMACRO
,
300 PP_ENDREP
, PP_ERROR
, PP_EXITREP
, PP_IASSIGN
, PP_IDEFINE
, PP_IF
,
301 PP_IFCTX
, PP_IFDEF
, PP_IFID
, PP_IFIDN
, PP_IFIDNI
, PP_IFMACRO
, PP_IFNCTX
,
302 PP_IFNDEF
, PP_IFNID
, PP_IFNIDN
, PP_IFNIDNI
, PP_IFNMACRO
, PP_IFNNUM
,
303 PP_IFNSTR
, PP_IFNUM
, PP_IFSTR
, PP_IMACRO
, PP_INCLUDE
,
304 PP_IXDEFINE
, PP_LINE
,
306 PP_MACRO
, PP_POP
, PP_PUSH
, PP_REP
, PP_REPL
, PP_ROTATE
,
308 PP_STRLEN
, PP_SUBSTR
, PP_UNDEF
, PP_XDEFINE
311 /* If this is a an IF, ELIF, ELSE or ENDIF keyword */
312 static int is_condition(int arg
)
314 return ((arg
>= PP_ELIF
) && (arg
<= PP_ENDIF
)) ||
315 ((arg
>= PP_IF
) && (arg
<= PP_IFSTR
));
318 /* For TASM compatibility we need to be able to recognise TASM compatible
319 * conditional compilation directives. Using the NASM pre-processor does
320 * not work, so we look for them specifically from the following list and
321 * then jam in the equivalent NASM directive into the input stream.
325 # define MAX(a,b) ( ((a) > (b)) ? (a) : (b))
330 TM_ARG
, TM_ELIF
, TM_ELSE
, TM_ENDIF
, TM_IF
, TM_IFDEF
, TM_IFDIFI
,
331 TM_IFNDEF
, TM_INCLUDE
, TM_LOCAL
334 static const char *tasm_directives
[] = {
335 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
336 "ifndef", "include", "local"
339 static int StackSize
= 4;
340 static char *StackPointer
= "ebp";
341 static int ArgOffset
= 8;
342 static int LocalOffset
= 4;
345 static Context
*cstk
;
346 static Include
*istk
;
347 static IncPath
*ipath
= NULL
;
349 static efunc _error
; /* Pointer to client-provided error reporting function */
350 static evalfunc evaluate
;
352 static int pass
; /* HACK: pass 0 = generate dependencies only */
354 static unsigned long unique
; /* unique identifier numbers */
356 static Line
*predef
= NULL
;
358 static ListGen
*list
;
361 * The number of hash values we use for the macro lookup tables.
362 * FIXME: We should *really* be able to configure this at run time,
363 * or even have the hash table automatically expanding when necessary.
368 * The current set of multi-line macros we have defined.
370 static MMacro
*mmacros
[NHASH
];
373 * The current set of single-line macros we have defined.
375 static SMacro
*smacros
[NHASH
];
378 * The multi-line macro we are currently defining, or the %rep
379 * block we are currently reading, if any.
381 static MMacro
*defining
;
384 * The number of macro parameters to allocate space for at a time.
386 #define PARAM_DELTA 16
389 * The standard macro set: defined as `static char *stdmac[]'. Also
390 * gives our position in the macro set, when we're processing it.
393 static const char **stdmacpos
;
396 * The extra standard macros that come from the object format, if
399 static char **extrastdmac
= NULL
;
403 * Tokens are allocated in blocks to improve speed
405 #define TOKEN_BLOCKSIZE 4096
406 static Token
*freeTokens
= NULL
;
412 static Blocks blocks
= { NULL
, NULL
};
415 * Forward declarations.
417 static Token
*expand_mmac_params(Token
* tline
);
418 static Token
*expand_smacro(Token
* tline
);
419 static Token
*expand_id(Token
* tline
);
420 static Context
*get_ctx(char *name
, int all_contexts
);
421 static void make_tok_num(Token
* tok
, long val
);
422 static void error(int severity
, const char *fmt
, ...);
423 static void *new_Block(size_t size
);
424 static void delete_Blocks(void);
425 static Token
*new_Token(Token
* next
, int type
, char *text
, int txtlen
);
426 static Token
*delete_Token(Token
* t
);
429 * Macros for safe checking of token pointers, avoid *(NULL)
431 #define tok_type_(x,t) ((x) && (x)->type == (t))
432 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
433 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
434 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
436 /* Handle TASM specific directives, which do not contain a % in
437 * front of them. We do it here because I could not find any other
438 * place to do it for the moment, and it is a hack (ideally it would
439 * be nice to be able to use the NASM pre-processor to do it).
442 check_tasm_directive(char *line
)
445 char *p
= line
, *oldline
, oldchar
;
447 /* Skip whitespace */
448 while (isspace(*p
) && *p
!= 0)
451 /* Binary search for the directive name */
453 j
= sizeof(tasm_directives
) / sizeof(*tasm_directives
);
455 while (!isspace(p
[len
]) && p
[len
] != 0)
464 m
= nasm_stricmp(p
, tasm_directives
[k
]);
467 /* We have found a directive, so jam a % in front of it
468 * so that NASM will then recognise it as one if it's own.
473 line
= nasm_malloc(len
+ 2);
477 /* NASM does not recognise IFDIFI, so we convert it to
478 * %ifdef BOGUS. This is not used in NASM comaptible
479 * code, but does need to parse for the TASM macro
482 strcpy(line
+ 1, "ifdef BOGUS");
486 memcpy(line
+ 1, p
, len
+ 1);
504 * The pre-preprocessing stage... This function translates line
505 * number indications as they emerge from GNU cpp (`# lineno "file"
506 * flags') into NASM preprocessor line number indications (`%line
510 prepreproc(char *line
)
513 char *fname
, *oldline
;
515 if (line
[0] == '#' && line
[1] == ' ')
519 lineno
= atoi(fname
);
520 fname
+= strspn(fname
, "0123456789 ");
523 fnlen
= strcspn(fname
, "\"");
524 line
= nasm_malloc(20 + fnlen
);
525 sprintf(line
, "%%line %d %.*s", lineno
, fnlen
, fname
);
528 if (tasm_compatible_mode
)
529 return check_tasm_directive(line
);
534 * The hash function for macro lookups. Note that due to some
535 * macros having case-insensitive names, the hash function must be
536 * invariant under case changes. We implement this by applying a
537 * perfectly normal hash function to the uppercase of the string.
545 * Powers of three, mod 31.
547 static const int multipliers
[] = {
548 1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10,
549 30, 28, 22, 4, 12, 5, 15, 14, 11, 2, 6, 18, 23, 7, 21
555 h
+= multipliers
[i
] * (unsigned char) (toupper(*s
));
557 if (++i
>= sizeof(multipliers
) / sizeof(*multipliers
))
565 * Free a linked list of tokens.
568 free_tlist(Token
* list
)
572 list
= delete_Token(list
);
577 * Free a linked list of lines.
580 free_llist(Line
* list
)
587 free_tlist(l
->first
);
596 free_mmacro(MMacro
* m
)
599 free_tlist(m
->dlist
);
600 nasm_free(m
->defaults
);
601 free_llist(m
->expansion
);
606 * Pop the context stack.
621 free_tlist(s
->expansion
);
628 #define BUF_DELTA 512
630 * Read a line from the top file in istk, handling multiple CR/LFs
631 * at the end of the line read, and handling spurious ^Zs. Will
632 * return lines from the standard macro set if this has not already
638 char *buffer
, *p
, *q
;
639 int bufsize
, continued_count
;
645 char *ret
= nasm_strdup(*stdmacpos
++);
646 if (!*stdmacpos
&& any_extrastdmac
)
648 stdmacpos
= extrastdmac
;
649 any_extrastdmac
= FALSE
;
653 * Nasty hack: here we push the contents of `predef' on
654 * to the top-level expansion stack, since this is the
655 * most convenient way to implement the pre-include and
656 * pre-define features.
661 Token
*head
, **tail
, *t
;
663 for (pd
= predef
; pd
; pd
= pd
->next
)
667 for (t
= pd
->first
; t
; t
= t
->next
)
669 *tail
= new_Token(NULL
, t
->type
, t
->text
, 0);
670 tail
= &(*tail
)->next
;
672 l
= nasm_malloc(sizeof(Line
));
673 l
->next
= istk
->expansion
;
688 buffer
= nasm_malloc(BUF_DELTA
);
693 q
= fgets(p
, bufsize
- (p
- buffer
), istk
->fp
);
697 if (p
> buffer
&& p
[-1] == '\n')
699 /* Convert backslash-CRLF line continuation sequences into
700 nothing at all (for DOS and Windows) */
701 if (((p
- 2) > buffer
) && (p
[-3] == '\\') && (p
[-2] == '\r')) {
706 /* Also convert backslash-LF line continuation sequences into
707 nothing at all (for Unix) */
708 else if (((p
- 1) > buffer
) && (p
[-2] == '\\')) {
717 if (p
- buffer
> bufsize
- 10)
719 long offset
= p
- buffer
;
720 bufsize
+= BUF_DELTA
;
721 buffer
= nasm_realloc(buffer
, bufsize
);
722 p
= buffer
+ offset
; /* prevent stale-pointer problems */
726 if (!q
&& p
== buffer
)
732 src_set_linnum(src_get_linnum() + istk
->lineinc
+ (continued_count
* istk
->lineinc
));
735 * Play safe: remove CRs as well as LFs, if any of either are
736 * present at the end of the line.
738 while (--p
>= buffer
&& (*p
== '\n' || *p
== '\r'))
742 * Handle spurious ^Z, which may be inserted into source files
743 * by some file transfer utilities.
745 buffer
[strcspn(buffer
, "\032")] = '\0';
747 list
->line(LIST_READ
, buffer
);
753 * Tokenise a line of text. This is a very simple process since we
754 * don't need to parse the value out of e.g. numeric tokens: we
755 * simply split one string into many.
763 Token
*t
, **tail
= &list
;
772 ((*p
== '-' || *p
== '+') && isdigit(p
[1])) ||
773 ((*p
== '+') && (isspace(p
[1]) || !p
[1])))
780 type
= TOK_PREPROC_ID
;
785 while (*p
&& *p
!= '}')
793 type
= TOK_PREPROC_ID
;
795 else if (isidchar(*p
) ||
796 ((*p
== '!' || *p
== '%' || *p
== '$') &&
803 while (isidchar(*p
));
804 type
= TOK_PREPROC_ID
;
813 else if (isidstart(*p
) || (*p
== '$' && isidstart(p
[1])))
817 while (*p
&& isidchar(*p
))
820 else if (*p
== '\'' || *p
== '"')
828 while (*p
&& *p
!= c
)
836 error(ERR_WARNING
, "unterminated string");
839 else if (isnumstart(*p
))
846 while (*p
&& isnumchar(*p
))
849 else if (isspace(*p
))
851 type
= TOK_WHITESPACE
;
853 while (*p
&& isspace(*p
))
856 * Whitespace just before end-of-line is discarded by
857 * pretending it's a comment; whitespace just before a
858 * comment gets lumped into the comment.
860 if (!*p
|| *p
== ';')
876 * Anything else is an operator of some kind. We check
877 * for all the double-character operators (>>, <<, //,
878 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
879 * else is a single-character operator.
882 if ((p
[0] == '>' && p
[1] == '>') ||
883 (p
[0] == '<' && p
[1] == '<') ||
884 (p
[0] == '/' && p
[1] == '/') ||
885 (p
[0] == '<' && p
[1] == '=') ||
886 (p
[0] == '>' && p
[1] == '=') ||
887 (p
[0] == '=' && p
[1] == '=') ||
888 (p
[0] == '!' && p
[1] == '=') ||
889 (p
[0] == '<' && p
[1] == '>') ||
890 (p
[0] == '&' && p
[1] == '&') ||
891 (p
[0] == '|' && p
[1] == '|') ||
892 (p
[0] == '^' && p
[1] == '^'))
898 if (type
!= TOK_COMMENT
)
900 *tail
= t
= new_Token(NULL
, type
, line
, p
- line
);
909 * this function allocates a new managed block of memory and
910 * returns a pointer to the block. The managed blocks are
911 * deleted only all at once by the delete_Blocks function.
914 new_Block(size_t size
)
918 /* first, get to the end of the linked list */
921 /* now allocate the requested chunk */
922 b
->chunk
= nasm_malloc(size
);
924 /* now allocate a new block for the next request */
925 b
->next
= nasm_malloc(sizeof(Blocks
));
926 /* and initialize the contents of the new block */
927 b
->next
->next
= NULL
;
928 b
->next
->chunk
= NULL
;
933 * this function deletes all managed blocks of memory
938 Blocks
*a
,*b
= &blocks
;
941 * keep in mind that the first block, pointed to by blocks
942 * is a static and not dynamically allocated, so we don't
957 * this function creates a new Token and passes a pointer to it
958 * back to the caller. It sets the type and text elements, and
959 * also the mac and next elements to NULL.
962 new_Token(Token
* next
, int type
, char *text
, int txtlen
)
967 if (freeTokens
== NULL
)
969 freeTokens
= (Token
*)new_Block(TOKEN_BLOCKSIZE
* sizeof(Token
));
970 for (i
= 0; i
< TOKEN_BLOCKSIZE
- 1; i
++)
971 freeTokens
[i
].next
= &freeTokens
[i
+ 1];
972 freeTokens
[i
].next
= NULL
;
975 freeTokens
= t
->next
;
979 if (type
== TOK_WHITESPACE
|| text
== NULL
)
986 txtlen
= strlen(text
);
987 t
->text
= nasm_malloc(1 + txtlen
);
988 strncpy(t
->text
, text
, txtlen
);
989 t
->text
[txtlen
] = '\0';
995 delete_Token(Token
* t
)
997 Token
*next
= t
->next
;
999 t
->next
= freeTokens
;
1005 * Convert a line of tokens back into text.
1006 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1007 * will be transformed into ..@ctxnum.xxx
1010 detoken(Token
* tlist
, int expand_locals
)
1017 for (t
= tlist
; t
; t
= t
->next
)
1019 if (t
->type
== TOK_PREPROC_ID
&& t
->text
[1] == '!')
1021 char *p
= getenv(t
->text
+ 2);
1024 t
->text
= nasm_strdup(p
);
1028 /* Expand local macros here and not during preprocessing */
1029 if (expand_locals
&&
1030 t
->type
== TOK_PREPROC_ID
&& t
->text
&&
1031 t
->text
[0] == '%' && t
->text
[1] == '$')
1033 Context
*ctx
= get_ctx(t
->text
, FALSE
);
1037 char *p
, *q
= t
->text
+ 2;
1039 q
+= strspn(q
, "$");
1040 sprintf(buffer
, "..@%lu.", ctx
->number
);
1041 p
= nasm_strcat(buffer
, q
);
1046 if (t
->type
== TOK_WHITESPACE
)
1052 len
+= strlen(t
->text
);
1055 p
= line
= nasm_malloc(len
+ 1);
1056 for (t
= tlist
; t
; t
= t
->next
)
1058 if (t
->type
== TOK_WHITESPACE
)
1075 * A scanner, suitable for use by the expression evaluator, which
1076 * operates on a line of Tokens. Expects a pointer to a pointer to
1077 * the first token in the line to be passed in as its private_data
1081 ppscan(void *private_data
, struct tokenval
*tokval
)
1083 Token
**tlineptr
= private_data
;
1089 *tlineptr
= tline
? tline
->next
: NULL
;
1091 while (tline
&& (tline
->type
== TOK_WHITESPACE
||
1092 tline
->type
== TOK_COMMENT
));
1095 return tokval
->t_type
= TOKEN_EOS
;
1097 if (tline
->text
[0] == '$' && !tline
->text
[1])
1098 return tokval
->t_type
= TOKEN_HERE
;
1099 if (tline
->text
[0] == '$' && tline
->text
[1] == '$' && !tline
->text
[2])
1100 return tokval
->t_type
= TOKEN_BASE
;
1102 if (tline
->type
== TOK_ID
)
1104 tokval
->t_charptr
= tline
->text
;
1105 if (tline
->text
[0] == '$')
1107 tokval
->t_charptr
++;
1108 return tokval
->t_type
= TOKEN_ID
;
1112 * This is the only special case we actually need to worry
1113 * about in this restricted context.
1115 if (!nasm_stricmp(tline
->text
, "seg"))
1116 return tokval
->t_type
= TOKEN_SEG
;
1118 return tokval
->t_type
= TOKEN_ID
;
1121 if (tline
->type
== TOK_NUMBER
)
1125 tokval
->t_integer
= readnum(tline
->text
, &rn_error
);
1127 return tokval
->t_type
= TOKEN_ERRNUM
;
1128 tokval
->t_charptr
= NULL
;
1129 return tokval
->t_type
= TOKEN_NUM
;
1132 if (tline
->type
== TOK_STRING
)
1142 if (l
== 0 || r
[l
- 1] != q
)
1143 return tokval
->t_type
= TOKEN_ERRNUM
;
1144 tokval
->t_integer
= readstrnum(r
, l
- 1, &rn_warn
);
1146 error(ERR_WARNING
| ERR_PASS1
, "character constant too long");
1147 tokval
->t_charptr
= NULL
;
1148 return tokval
->t_type
= TOKEN_NUM
;
1151 if (tline
->type
== TOK_OTHER
)
1153 if (!strcmp(tline
->text
, "<<"))
1154 return tokval
->t_type
= TOKEN_SHL
;
1155 if (!strcmp(tline
->text
, ">>"))
1156 return tokval
->t_type
= TOKEN_SHR
;
1157 if (!strcmp(tline
->text
, "//"))
1158 return tokval
->t_type
= TOKEN_SDIV
;
1159 if (!strcmp(tline
->text
, "%%"))
1160 return tokval
->t_type
= TOKEN_SMOD
;
1161 if (!strcmp(tline
->text
, "=="))
1162 return tokval
->t_type
= TOKEN_EQ
;
1163 if (!strcmp(tline
->text
, "<>"))
1164 return tokval
->t_type
= TOKEN_NE
;
1165 if (!strcmp(tline
->text
, "!="))
1166 return tokval
->t_type
= TOKEN_NE
;
1167 if (!strcmp(tline
->text
, "<="))
1168 return tokval
->t_type
= TOKEN_LE
;
1169 if (!strcmp(tline
->text
, ">="))
1170 return tokval
->t_type
= TOKEN_GE
;
1171 if (!strcmp(tline
->text
, "&&"))
1172 return tokval
->t_type
= TOKEN_DBL_AND
;
1173 if (!strcmp(tline
->text
, "^^"))
1174 return tokval
->t_type
= TOKEN_DBL_XOR
;
1175 if (!strcmp(tline
->text
, "||"))
1176 return tokval
->t_type
= TOKEN_DBL_OR
;
1180 * We have no other options: just return the first character of
1183 return tokval
->t_type
= tline
->text
[0];
1187 * Compare a string to the name of an existing macro; this is a
1188 * simple wrapper which calls either strcmp or nasm_stricmp
1189 * depending on the value of the `casesense' parameter.
1192 mstrcmp(char *p
, char *q
, int casesense
)
1194 return casesense
? strcmp(p
, q
) : nasm_stricmp(p
, q
);
1198 * Return the Context structure associated with a %$ token. Return
1199 * NULL, having _already_ reported an error condition, if the
1200 * context stack isn't deep enough for the supplied number of $
1202 * If all_contexts == TRUE, contexts that enclose current are
1203 * also scanned for such smacro, until it is found; if not -
1204 * only the context that directly results from the number of $'s
1205 * in variable's name.
1208 get_ctx(char *name
, int all_contexts
)
1214 if (!name
|| name
[0] != '%' || name
[1] != '$')
1219 error(ERR_NONFATAL
, "`%s': context stack is empty", name
);
1223 for (i
= strspn(name
+ 2, "$"), ctx
= cstk
; (i
> 0) && ctx
; i
--)
1226 /* i--; Lino - 02/25/02 */
1230 error(ERR_NONFATAL
, "`%s': context stack is only"
1231 " %d level%s deep", name
, i
- 1, (i
== 2 ? "" : "s"));
1239 /* Search for this smacro in found context */
1243 if (!mstrcmp(m
->name
, name
, m
->casesense
))
1253 /* Add a slash to the end of a path if it is missing. We use the
1254 * forward slash to make it compatible with Unix systems.
1259 int pos
= strlen(s
);
1260 if (s
[pos
- 1] != '\\' && s
[pos
- 1] != '/')
1268 * Open an include file. This routine must always return a valid
1269 * file pointer if it returns - it's responsible for throwing an
1270 * ERR_FATAL and bombing out completely if not. It should also try
1271 * the include path one by one until it finds the file or reaches
1272 * the end of the path.
1275 inc_fopen(char *file
)
1278 char *prefix
= "", *combine
;
1279 IncPath
*ip
= ipath
;
1280 static int namelen
= 0;
1281 int len
= strlen(file
);
1285 combine
= nasm_malloc(strlen(prefix
) + 1 + len
+ 1);
1286 strcpy(combine
, prefix
);
1289 strcat(combine
, file
);
1290 fp
= fopen(combine
, "r");
1291 if (pass
== 0 && fp
)
1293 namelen
+= strlen(combine
) + 1;
1299 printf(" %s", combine
);
1310 error(ERR_FATAL
, "unable to open include file `%s'", file
);
1311 return NULL
; /* never reached - placate compilers */
1315 * Determine if we should warn on defining a single-line macro of
1316 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1317 * return TRUE if _any_ single-line macro of that name is defined.
1318 * Otherwise, will return TRUE if a single-line macro with either
1319 * `nparam' or no parameters is defined.
1321 * If a macro with precisely the right number of parameters is
1322 * defined, or nparam is -1, the address of the definition structure
1323 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1324 * is NULL, no action will be taken regarding its contents, and no
1327 * Note that this is also called with nparam zero to resolve
1330 * If you already know which context macro belongs to, you can pass
1331 * the context pointer as first parameter; if you won't but name begins
1332 * with %$ the context will be automatically computed. If all_contexts
1333 * is true, macro will be searched in outer contexts as well.
1336 smacro_defined(Context
* ctx
, char *name
, int nparam
, SMacro
** defn
,
1343 else if (name
[0] == '%' && name
[1] == '$')
1346 ctx
= get_ctx(name
, FALSE
);
1348 return FALSE
; /* got to return _something_ */
1352 m
= smacros
[hash(name
)];
1356 if (!mstrcmp(m
->name
, name
, m
->casesense
&& nocase
) &&
1357 (nparam
<= 0 || m
->nparam
== 0 || nparam
== m
->nparam
))
1361 if (nparam
== m
->nparam
|| nparam
== -1)
1375 * Count and mark off the parameters in a multi-line macro call.
1376 * This is called both from within the multi-line macro expansion
1377 * code, and also to mark off the default parameters when provided
1378 * in a %macro definition line.
1381 count_mmac_params(Token
* t
, int *nparam
, Token
*** params
)
1383 int paramsize
, brace
;
1385 *nparam
= paramsize
= 0;
1389 if (*nparam
>= paramsize
)
1391 paramsize
+= PARAM_DELTA
;
1392 *params
= nasm_realloc(*params
, sizeof(**params
) * paramsize
);
1396 if (tok_is_(t
, "{"))
1398 (*params
)[(*nparam
)++] = t
;
1399 while (tok_isnt_(t
, brace
? "}" : ","))
1402 { /* got a comma/brace */
1407 * Now we've found the closing brace, look further
1411 if (tok_isnt_(t
, ","))
1414 "braces do not enclose all of macro parameter");
1415 while (tok_isnt_(t
, ","))
1419 t
= t
->next
; /* eat the comma */
1426 * Determine whether one of the various `if' conditions is true or
1429 * We must free the tline we get passed.
1432 if_condition(Token
* tline
, int i
)
1435 Token
*t
, *tt
, **tptr
, *origline
;
1436 struct tokenval tokval
;
1447 j
= FALSE
; /* have we matched yet? */
1448 while (cstk
&& tline
)
1451 if (!tline
|| tline
->type
!= TOK_ID
)
1454 "`%s' expects context identifiers",
1456 free_tlist(origline
);
1459 if (!nasm_stricmp(tline
->text
, cstk
->name
))
1461 tline
= tline
->next
;
1463 if (i
== PP_IFNCTX
|| i
== PP_ELIFNCTX
)
1465 free_tlist(origline
);
1472 j
= FALSE
; /* have we matched yet? */
1476 if (!tline
|| (tline
->type
!= TOK_ID
&&
1477 (tline
->type
!= TOK_PREPROC_ID
||
1478 tline
->text
[1] != '$')))
1481 "`%s' expects macro identifiers",
1483 free_tlist(origline
);
1486 if (smacro_defined(NULL
, tline
->text
, 0, NULL
, 1))
1488 tline
= tline
->next
;
1490 if (i
== PP_IFNDEF
|| i
== PP_ELIFNDEF
)
1492 free_tlist(origline
);
1503 tline
= expand_smacro(tline
);
1505 while (tok_isnt_(tt
, ","))
1510 "`%s' expects two comma-separated arguments",
1516 casesense
= (i
== PP_IFIDN
|| i
== PP_ELIFIDN
||
1517 i
== PP_IFNIDN
|| i
== PP_ELIFNIDN
);
1518 j
= TRUE
; /* assume equality unless proved not */
1519 while ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) && tt
)
1521 if (tt
->type
== TOK_OTHER
&& !strcmp(tt
->text
, ","))
1523 error(ERR_NONFATAL
, "`%s': more than one comma on line",
1528 if (t
->type
== TOK_WHITESPACE
)
1533 else if (tt
->type
== TOK_WHITESPACE
)
1538 else if (tt
->type
!= t
->type
||
1539 mstrcmp(tt
->text
, t
->text
, casesense
))
1541 j
= FALSE
; /* found mismatching tokens */
1551 if ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) || tt
)
1552 j
= FALSE
; /* trailing gunk on one end or other */
1553 if (i
== PP_IFNIDN
|| i
== PP_ELIFNIDN
||
1554 i
== PP_IFNIDNI
|| i
== PP_ELIFNIDNI
)
1565 MMacro searching
, *mmac
;
1567 tline
= tline
->next
;
1569 tline
= expand_id(tline
);
1570 if (!tok_type_(tline
, TOK_ID
))
1573 "`%s' expects a macro name",
1577 searching
.name
= nasm_strdup(tline
->text
);
1578 searching
.casesense
= (i
== PP_MACRO
);
1579 searching
.plus
= FALSE
;
1580 searching
.nolist
= FALSE
;
1581 searching
.in_progress
= FALSE
;
1582 searching
.rep_nest
= NULL
;
1583 searching
.nparam_min
= 0;
1584 searching
.nparam_max
= INT_MAX
;
1585 tline
= expand_smacro(tline
->next
);
1589 } else if (!tok_type_(tline
, TOK_NUMBER
))
1592 "`%s' expects a parameter count or nothing",
1597 searching
.nparam_min
= searching
.nparam_max
=
1598 readnum(tline
->text
, &j
);
1601 "unable to parse parameter count `%s'",
1604 if (tline
&& tok_is_(tline
->next
, "-"))
1606 tline
= tline
->next
->next
;
1607 if (tok_is_(tline
, "*"))
1608 searching
.nparam_max
= INT_MAX
;
1609 else if (!tok_type_(tline
, TOK_NUMBER
))
1611 "`%s' expects a parameter count after `-'",
1615 searching
.nparam_max
= readnum(tline
->text
, &j
);
1618 "unable to parse parameter count `%s'",
1620 if (searching
.nparam_min
> searching
.nparam_max
)
1622 "minimum parameter count exceeds maximum");
1625 if (tline
&& tok_is_(tline
->next
, "+"))
1627 tline
= tline
->next
;
1628 searching
.plus
= TRUE
;
1630 mmac
= mmacros
[hash(searching
.name
)];
1633 if (!strcmp(mmac
->name
, searching
.name
) &&
1634 (mmac
->nparam_min
<= searching
.nparam_max
1636 && (searching
.nparam_min
<= mmac
->nparam_max
1644 nasm_free(searching
.name
);
1645 free_tlist(origline
);
1646 if (i
== PP_IFNMACRO
|| i
== PP_ELIFNMACRO
)
1663 tline
= expand_smacro(tline
);
1665 while (tok_type_(t
, TOK_WHITESPACE
))
1667 j
= FALSE
; /* placate optimiser */
1675 j
= (t
->type
== TOK_ID
);
1681 j
= (t
->type
== TOK_NUMBER
);
1687 j
= (t
->type
== TOK_STRING
);
1690 if (i
== PP_IFNID
|| i
== PP_ELIFNID
||
1691 i
== PP_IFNNUM
|| i
== PP_ELIFNNUM
||
1692 i
== PP_IFNSTR
|| i
== PP_ELIFNSTR
)
1699 t
= tline
= expand_smacro(tline
);
1701 tokval
.t_type
= TOKEN_INVALID
;
1702 evalresult
= evaluate(ppscan
, tptr
, &tokval
,
1703 NULL
, pass
| CRITICAL
, error
, NULL
);
1709 "trailing garbage after expression ignored");
1710 if (!is_simple(evalresult
))
1713 "non-constant value given to `%s'", directives
[i
]);
1716 return reloc_value(evalresult
) != 0;
1720 "preprocessor directive `%s' not yet implemented",
1722 free_tlist(origline
);
1723 return -1; /* yeah, right */
1728 * Expand macros in a string. Used in %error and %include directives.
1729 * First tokenise the string, apply "expand_smacro" and then de-tokenise back.
1730 * The returned variable should ALWAYS be freed after usage.
1733 expand_macros_in_string(char **p
)
1735 Token
*line
= tokenise(*p
);
1736 line
= expand_smacro(line
);
1737 *p
= detoken(line
, FALSE
);
1741 * Find out if a line contains a preprocessor directive, and deal
1744 * If a directive _is_ found, we are expected to free_tlist() the
1747 * Return values go like this:
1749 * bit 0 is set if a directive was found (so the line gets freed)
1752 do_directive(Token
* tline
)
1754 int i
, j
, k
, m
, nparam
, nolist
;
1760 SMacro
*smac
, **smhead
;
1762 Token
*t
, *tt
, *param_start
, *macro_start
, *last
, **tptr
, *origline
;
1764 struct tokenval tokval
;
1766 MMacro
*tmp_defining
; /* Used when manipulating rep_nest */
1771 if (!tok_type_(tline
, TOK_PREPROC_ID
) ||
1772 (tline
->text
[1] == '%' || tline
->text
[1] == '$'
1773 || tline
->text
[1] == '!'))
1777 j
= sizeof(directives
) / sizeof(*directives
);
1781 m
= nasm_stricmp(tline
->text
, directives
[k
]);
1783 if (tasm_compatible_mode
) {
1786 } else if (k
!= PP_ARG
&& k
!= PP_LOCAL
&& k
!= PP_STACKSIZE
) {
1800 * If we're in a non-emitting branch of a condition construct,
1801 * or walking to the end of an already terminated %rep block,
1802 * we should ignore all directives except for condition
1805 if (((istk
->conds
&& !emitting(istk
->conds
->state
)) ||
1806 (istk
->mstk
&& !istk
->mstk
->in_progress
)) &&
1813 * If we're defining a macro or reading a %rep block, we should
1814 * ignore all directives except for %macro/%imacro (which
1815 * generate an error), %endm/%endmacro, and (only if we're in a
1816 * %rep block) %endrep. If we're in a %rep block, another %rep
1817 * causes an error, so should be let through.
1819 if (defining
&& i
!= PP_MACRO
&& i
!= PP_IMACRO
&&
1820 i
!= PP_ENDMACRO
&& i
!= PP_ENDM
&&
1821 (defining
->name
|| (i
!= PP_ENDREP
&& i
!= PP_REP
)))
1828 error(ERR_NONFATAL
, "unknown preprocessor directive `%s'",
1830 return 0; /* didn't get it */
1836 /* Directive to tell NASM what the default stack size is. The
1837 * default is for a 16-bit stack, and this can be overriden with
1839 * the following form:
1841 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1843 tline
= tline
->next
;
1844 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1845 tline
= tline
->next
;
1846 if (!tline
|| tline
->type
!= TOK_ID
)
1848 error(ERR_NONFATAL
, "`%%stacksize' missing size parameter");
1849 free_tlist(origline
);
1852 if (nasm_stricmp(tline
->text
, "flat") == 0)
1854 /* All subsequent ARG directives are for a 32-bit stack */
1856 StackPointer
= "ebp";
1860 else if (nasm_stricmp(tline
->text
, "large") == 0)
1862 /* All subsequent ARG directives are for a 16-bit stack,
1863 * far function call.
1866 StackPointer
= "bp";
1870 else if (nasm_stricmp(tline
->text
, "small") == 0)
1872 /* All subsequent ARG directives are for a 16-bit stack,
1873 * far function call. We don't support near functions.
1876 StackPointer
= "bp";
1882 error(ERR_NONFATAL
, "`%%stacksize' invalid size type");
1883 free_tlist(origline
);
1886 free_tlist(origline
);
1890 /* TASM like ARG directive to define arguments to functions, in
1891 * the following form:
1893 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1898 char *arg
, directive
[256];
1899 int size
= StackSize
;
1901 /* Find the argument name */
1902 tline
= tline
->next
;
1903 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1904 tline
= tline
->next
;
1905 if (!tline
|| tline
->type
!= TOK_ID
)
1907 error(ERR_NONFATAL
, "`%%arg' missing argument parameter");
1908 free_tlist(origline
);
1913 /* Find the argument size type */
1914 tline
= tline
->next
;
1915 if (!tline
|| tline
->type
!= TOK_OTHER
1916 || tline
->text
[0] != ':')
1919 "Syntax error processing `%%arg' directive");
1920 free_tlist(origline
);
1923 tline
= tline
->next
;
1924 if (!tline
|| tline
->type
!= TOK_ID
)
1927 "`%%arg' missing size type parameter");
1928 free_tlist(origline
);
1932 /* Allow macro expansion of type parameter */
1933 tt
= tokenise(tline
->text
);
1934 tt
= expand_smacro(tt
);
1935 if (nasm_stricmp(tt
->text
, "byte") == 0)
1937 size
= MAX(StackSize
, 1);
1939 else if (nasm_stricmp(tt
->text
, "word") == 0)
1941 size
= MAX(StackSize
, 2);
1943 else if (nasm_stricmp(tt
->text
, "dword") == 0)
1945 size
= MAX(StackSize
, 4);
1947 else if (nasm_stricmp(tt
->text
, "qword") == 0)
1949 size
= MAX(StackSize
, 8);
1951 else if (nasm_stricmp(tt
->text
, "tword") == 0)
1953 size
= MAX(StackSize
, 10);
1958 "Invalid size type for `%%arg' missing directive");
1960 free_tlist(origline
);
1965 /* Now define the macro for the argument */
1966 sprintf(directive
, "%%define %s (%s+%d)", arg
, StackPointer
,
1968 do_directive(tokenise(directive
));
1971 /* Move to the next argument in the list */
1972 tline
= tline
->next
;
1973 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1974 tline
= tline
->next
;
1976 while (tline
&& tline
->type
== TOK_OTHER
1977 && tline
->text
[0] == ',');
1978 free_tlist(origline
);
1982 /* TASM like LOCAL directive to define local variables for a
1983 * function, in the following form:
1985 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
1987 * The '= LocalSize' at the end is ignored by NASM, but is
1988 * required by TASM to define the local parameter size (and used
1989 * by the TASM macro package).
1991 offset
= LocalOffset
;
1994 char *local
, directive
[256];
1995 int size
= StackSize
;
1997 /* Find the argument name */
1998 tline
= tline
->next
;
1999 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2000 tline
= tline
->next
;
2001 if (!tline
|| tline
->type
!= TOK_ID
)
2004 "`%%local' missing argument parameter");
2005 free_tlist(origline
);
2008 local
= tline
->text
;
2010 /* Find the argument size type */
2011 tline
= tline
->next
;
2012 if (!tline
|| tline
->type
!= TOK_OTHER
2013 || tline
->text
[0] != ':')
2016 "Syntax error processing `%%local' directive");
2017 free_tlist(origline
);
2020 tline
= tline
->next
;
2021 if (!tline
|| tline
->type
!= TOK_ID
)
2024 "`%%local' missing size type parameter");
2025 free_tlist(origline
);
2029 /* Allow macro expansion of type parameter */
2030 tt
= tokenise(tline
->text
);
2031 tt
= expand_smacro(tt
);
2032 if (nasm_stricmp(tt
->text
, "byte") == 0)
2034 size
= MAX(StackSize
, 1);
2036 else if (nasm_stricmp(tt
->text
, "word") == 0)
2038 size
= MAX(StackSize
, 2);
2040 else if (nasm_stricmp(tt
->text
, "dword") == 0)
2042 size
= MAX(StackSize
, 4);
2044 else if (nasm_stricmp(tt
->text
, "qword") == 0)
2046 size
= MAX(StackSize
, 8);
2048 else if (nasm_stricmp(tt
->text
, "tword") == 0)
2050 size
= MAX(StackSize
, 10);
2055 "Invalid size type for `%%local' missing directive");
2057 free_tlist(origline
);
2062 /* Now define the macro for the argument */
2063 sprintf(directive
, "%%define %s (%s-%d)", local
, StackPointer
,
2065 do_directive(tokenise(directive
));
2068 /* Now define the assign to setup the enter_c macro correctly */
2069 sprintf(directive
, "%%assign %%$localsize %%$localsize+%d",
2071 do_directive(tokenise(directive
));
2073 /* Move to the next argument in the list */
2074 tline
= tline
->next
;
2075 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2076 tline
= tline
->next
;
2078 while (tline
&& tline
->type
== TOK_OTHER
2079 && tline
->text
[0] == ',');
2080 free_tlist(origline
);
2086 "trailing garbage after `%%clear' ignored");
2087 for (j
= 0; j
< NHASH
; j
++)
2091 MMacro
*m
= mmacros
[j
];
2092 mmacros
[j
] = m
->next
;
2097 SMacro
*s
= smacros
[j
];
2098 smacros
[j
] = smacros
[j
]->next
;
2100 free_tlist(s
->expansion
);
2104 free_tlist(origline
);
2108 tline
= tline
->next
;
2110 if (!tline
|| (tline
->type
!= TOK_STRING
&&
2111 tline
->type
!= TOK_INTERNAL_STRING
))
2113 error(ERR_NONFATAL
, "`%%include' expects a file name");
2114 free_tlist(origline
);
2115 return 3; /* but we did _something_ */
2119 "trailing garbage after `%%include' ignored");
2120 if (tline
->type
!= TOK_INTERNAL_STRING
)
2122 p
= tline
->text
+ 1; /* point past the quote to the name */
2123 p
[strlen(p
) - 1] = '\0'; /* remove the trailing quote */
2126 p
= tline
->text
; /* internal_string is easier */
2127 expand_macros_in_string(&p
);
2128 inc
= nasm_malloc(sizeof(Include
));
2131 inc
->fp
= inc_fopen(p
);
2132 inc
->fname
= src_set_fname(p
);
2133 inc
->lineno
= src_set_linnum(0);
2135 inc
->expansion
= NULL
;
2138 list
->uplevel(LIST_INCLUDE
);
2139 free_tlist(origline
);
2143 tline
= tline
->next
;
2145 tline
= expand_id(tline
);
2146 if (!tok_type_(tline
, TOK_ID
))
2148 error(ERR_NONFATAL
, "`%%push' expects a context identifier");
2149 free_tlist(origline
);
2150 return 3; /* but we did _something_ */
2153 error(ERR_WARNING
, "trailing garbage after `%%push' ignored");
2154 ctx
= nasm_malloc(sizeof(Context
));
2156 ctx
->localmac
= NULL
;
2157 ctx
->name
= nasm_strdup(tline
->text
);
2158 ctx
->number
= unique
++;
2160 free_tlist(origline
);
2164 tline
= tline
->next
;
2166 tline
= expand_id(tline
);
2167 if (!tok_type_(tline
, TOK_ID
))
2169 error(ERR_NONFATAL
, "`%%repl' expects a context identifier");
2170 free_tlist(origline
);
2171 return 3; /* but we did _something_ */
2174 error(ERR_WARNING
, "trailing garbage after `%%repl' ignored");
2176 error(ERR_NONFATAL
, "`%%repl': context stack is empty");
2179 nasm_free(cstk
->name
);
2180 cstk
->name
= nasm_strdup(tline
->text
);
2182 free_tlist(origline
);
2187 error(ERR_WARNING
, "trailing garbage after `%%pop' ignored");
2190 "`%%pop': context stack is already empty");
2193 free_tlist(origline
);
2197 tline
->next
= expand_smacro(tline
->next
);
2198 tline
= tline
->next
;
2200 if (tok_type_(tline
, TOK_STRING
))
2202 p
= tline
->text
+ 1; /* point past the quote to the name */
2203 p
[strlen(p
) - 1] = '\0'; /* remove the trailing quote */
2204 expand_macros_in_string(&p
);
2205 error(ERR_NONFATAL
, "%s", p
);
2210 p
= detoken(tline
, FALSE
);
2211 error(ERR_WARNING
, "%s", p
);
2214 free_tlist(origline
);
2234 if (istk
->conds
&& !emitting(istk
->conds
->state
))
2238 j
= if_condition(tline
->next
, i
);
2239 tline
->next
= NULL
; /* it got freed */
2240 free_tlist(origline
);
2241 j
= j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2243 cond
= nasm_malloc(sizeof(Cond
));
2244 cond
->next
= istk
->conds
;
2247 return (j
== COND_IF_TRUE
? 3 : 1);
2267 error(ERR_FATAL
, "`%s': no matching `%%if'", directives
[i
]);
2268 if (emitting(istk
->conds
->state
)
2269 || istk
->conds
->state
== COND_NEVER
)
2270 istk
->conds
->state
= COND_NEVER
;
2274 * IMPORTANT: In the case of %if, we will already have
2275 * called expand_mmac_params(); however, if we're
2276 * processing an %elif we must have been in a
2277 * non-emitting mode, which would have inhibited
2278 * the normal invocation of expand_mmac_params(). Therefore,
2279 * we have to do it explicitly here.
2281 j
= if_condition(expand_mmac_params(tline
->next
), i
);
2282 tline
->next
= NULL
; /* it got freed */
2283 free_tlist(origline
);
2284 istk
->conds
->state
=
2285 j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2287 return (istk
->conds
->state
== COND_IF_TRUE
? 5 : 1);
2291 error(ERR_WARNING
, "trailing garbage after `%%else' ignored");
2293 error(ERR_FATAL
, "`%%else': no matching `%%if'");
2294 if (emitting(istk
->conds
->state
)
2295 || istk
->conds
->state
== COND_NEVER
)
2296 istk
->conds
->state
= COND_ELSE_FALSE
;
2298 istk
->conds
->state
= COND_ELSE_TRUE
;
2299 free_tlist(origline
);
2305 "trailing garbage after `%%endif' ignored");
2307 error(ERR_FATAL
, "`%%endif': no matching `%%if'");
2309 istk
->conds
= cond
->next
;
2311 free_tlist(origline
);
2318 "`%%%smacro': already defining a macro",
2319 (i
== PP_IMACRO
? "i" : ""));
2320 tline
= tline
->next
;
2322 tline
= expand_id(tline
);
2323 if (!tok_type_(tline
, TOK_ID
))
2326 "`%%%smacro' expects a macro name",
2327 (i
== PP_IMACRO
? "i" : ""));
2330 defining
= nasm_malloc(sizeof(MMacro
));
2331 defining
->name
= nasm_strdup(tline
->text
);
2332 defining
->casesense
= (i
== PP_MACRO
);
2333 defining
->plus
= FALSE
;
2334 defining
->nolist
= FALSE
;
2335 defining
->in_progress
= FALSE
;
2336 defining
->rep_nest
= NULL
;
2337 tline
= expand_smacro(tline
->next
);
2339 if (!tok_type_(tline
, TOK_NUMBER
))
2342 "`%%%smacro' expects a parameter count",
2343 (i
== PP_IMACRO
? "i" : ""));
2344 defining
->nparam_min
= defining
->nparam_max
= 0;
2348 defining
->nparam_min
= defining
->nparam_max
=
2349 readnum(tline
->text
, &j
);
2352 "unable to parse parameter count `%s'",
2355 if (tline
&& tok_is_(tline
->next
, "-"))
2357 tline
= tline
->next
->next
;
2358 if (tok_is_(tline
, "*"))
2359 defining
->nparam_max
= INT_MAX
;
2360 else if (!tok_type_(tline
, TOK_NUMBER
))
2362 "`%%%smacro' expects a parameter count after `-'",
2363 (i
== PP_IMACRO
? "i" : ""));
2366 defining
->nparam_max
= readnum(tline
->text
, &j
);
2369 "unable to parse parameter count `%s'",
2371 if (defining
->nparam_min
> defining
->nparam_max
)
2373 "minimum parameter count exceeds maximum");
2376 if (tline
&& tok_is_(tline
->next
, "+"))
2378 tline
= tline
->next
;
2379 defining
->plus
= TRUE
;
2381 if (tline
&& tok_type_(tline
->next
, TOK_ID
) &&
2382 !nasm_stricmp(tline
->next
->text
, ".nolist"))
2384 tline
= tline
->next
;
2385 defining
->nolist
= TRUE
;
2387 mmac
= mmacros
[hash(defining
->name
)];
2390 if (!strcmp(mmac
->name
, defining
->name
) &&
2391 (mmac
->nparam_min
<= defining
->nparam_max
2393 && (defining
->nparam_min
<= mmac
->nparam_max
2397 "redefining multi-line macro `%s'",
2404 * Handle default parameters.
2406 if (tline
&& tline
->next
)
2408 defining
->dlist
= tline
->next
;
2410 count_mmac_params(defining
->dlist
, &defining
->ndefs
,
2411 &defining
->defaults
);
2415 defining
->dlist
= NULL
;
2416 defining
->defaults
= NULL
;
2418 defining
->expansion
= NULL
;
2419 free_tlist(origline
);
2426 error(ERR_NONFATAL
, "`%s': not defining a macro",
2430 k
= hash(defining
->name
);
2431 defining
->next
= mmacros
[k
];
2432 mmacros
[k
] = defining
;
2434 free_tlist(origline
);
2438 if (tline
->next
&& tline
->next
->type
== TOK_WHITESPACE
)
2439 tline
= tline
->next
;
2440 t
= expand_smacro(tline
->next
);
2442 free_tlist(origline
);
2445 tokval
.t_type
= TOKEN_INVALID
;
2447 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2453 "trailing garbage after expression ignored");
2454 if (!is_simple(evalresult
))
2456 error(ERR_NONFATAL
, "non-constant value given to `%%rotate'");
2460 while (mmac
&& !mmac
->name
) /* avoid mistaking %reps for macros */
2461 mmac
= mmac
->next_active
;
2464 "`%%rotate' invoked outside a macro call");
2465 mmac
->rotate
= mmac
->rotate
+ reloc_value(evalresult
);
2466 if (mmac
->rotate
< 0)
2467 mmac
->rotate
= mmac
->nparam
- (-mmac
->rotate
) % mmac
->nparam
;
2468 mmac
->rotate
%= mmac
->nparam
;
2473 tline
= tline
->next
;
2474 if (tline
->next
&& tline
->next
->type
== TOK_WHITESPACE
)
2475 tline
= tline
->next
;
2476 if (tline
->next
&& tline
->next
->type
== TOK_ID
&&
2477 !nasm_stricmp(tline
->next
->text
, ".nolist"))
2479 tline
= tline
->next
;
2482 t
= expand_smacro(tline
->next
);
2484 free_tlist(origline
);
2487 tokval
.t_type
= TOKEN_INVALID
;
2489 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2495 "trailing garbage after expression ignored");
2496 if (!is_simple(evalresult
))
2498 error(ERR_NONFATAL
, "non-constant value given to `%%rep'");
2501 tmp_defining
= defining
;
2502 defining
= nasm_malloc(sizeof(MMacro
));
2503 defining
->name
= NULL
; /* flags this macro as a %rep block */
2504 defining
->casesense
= 0;
2505 defining
->plus
= FALSE
;
2506 defining
->nolist
= nolist
;
2507 defining
->in_progress
= reloc_value(evalresult
) + 1;
2508 defining
->nparam_min
= defining
->nparam_max
= 0;
2509 defining
->defaults
= NULL
;
2510 defining
->dlist
= NULL
;
2511 defining
->expansion
= NULL
;
2512 defining
->next_active
= istk
->mstk
;
2513 defining
->rep_nest
= tmp_defining
;
2517 if (!defining
|| defining
->name
)
2519 error(ERR_NONFATAL
, "`%%endrep': no matching `%%rep'");
2524 * Now we have a "macro" defined - although it has no name
2525 * and we won't be entering it in the hash tables - we must
2526 * push a macro-end marker for it on to istk->expansion.
2527 * After that, it will take care of propagating itself (a
2528 * macro-end marker line for a macro which is really a %rep
2529 * block will cause the macro to be re-expanded, complete
2530 * with another macro-end marker to ensure the process
2531 * continues) until the whole expansion is forcibly removed
2532 * from istk->expansion by a %exitrep.
2534 l
= nasm_malloc(sizeof(Line
));
2535 l
->next
= istk
->expansion
;
2536 l
->finishes
= defining
;
2538 istk
->expansion
= l
;
2540 istk
->mstk
= defining
;
2542 list
->uplevel(defining
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
2543 tmp_defining
= defining
;
2544 defining
= defining
->rep_nest
;
2545 free_tlist(origline
);
2550 * We must search along istk->expansion until we hit a
2551 * macro-end marker for a macro with no name. Then we set
2552 * its `in_progress' flag to 0.
2554 for (l
= istk
->expansion
; l
; l
= l
->next
)
2555 if (l
->finishes
&& !l
->finishes
->name
)
2559 l
->finishes
->in_progress
= 0;
2561 error(ERR_NONFATAL
, "`%%exitrep' not within `%%rep' block");
2562 free_tlist(origline
);
2569 tline
= tline
->next
;
2571 tline
= expand_id(tline
);
2572 if (!tline
|| (tline
->type
!= TOK_ID
&&
2573 (tline
->type
!= TOK_PREPROC_ID
||
2574 tline
->text
[1] != '$')))
2577 "`%%%s%sdefine' expects a macro identifier",
2578 ((i
== PP_IDEFINE
|| i
== PP_IXDEFINE
) ? "i" : ""),
2579 ((i
== PP_XDEFINE
|| i
== PP_IXDEFINE
) ? "x" : ""));
2580 free_tlist(origline
);
2584 ctx
= get_ctx(tline
->text
, FALSE
);
2586 smhead
= &smacros
[hash(tline
->text
)];
2588 smhead
= &ctx
->localmac
;
2589 mname
= tline
->text
;
2591 param_start
= tline
= tline
->next
;
2594 /* Expand the macro definition now for %xdefine and %ixdefine */
2595 if ((i
== PP_XDEFINE
) || (i
== PP_IXDEFINE
))
2596 tline
= expand_smacro(tline
);
2598 if (tok_is_(tline
, "("))
2601 * This macro has parameters.
2604 tline
= tline
->next
;
2610 error(ERR_NONFATAL
, "parameter identifier expected");
2611 free_tlist(origline
);
2614 if (tline
->type
!= TOK_ID
)
2617 "`%s': parameter identifier expected",
2619 free_tlist(origline
);
2622 tline
->type
= TOK_SMAC_PARAM
+ nparam
++;
2623 tline
= tline
->next
;
2625 if (tok_is_(tline
, ","))
2627 tline
= tline
->next
;
2630 if (!tok_is_(tline
, ")"))
2633 "`)' expected to terminate macro template");
2634 free_tlist(origline
);
2640 tline
= tline
->next
;
2642 if (tok_type_(tline
, TOK_WHITESPACE
))
2643 last
= tline
, tline
= tline
->next
;
2649 if (t
->type
== TOK_ID
)
2651 for (tt
= param_start
; tt
; tt
= tt
->next
)
2652 if (tt
->type
>= TOK_SMAC_PARAM
&&
2653 !strcmp(tt
->text
, t
->text
))
2657 t
->next
= macro_start
;
2662 * Good. We now have a macro name, a parameter count, and a
2663 * token list (in reverse order) for an expansion. We ought
2664 * to be OK just to create an SMacro, store it, and let
2665 * free_tlist have the rest of the line (which we have
2666 * carefully re-terminated after chopping off the expansion
2669 if (smacro_defined(ctx
, mname
, nparam
, &smac
, i
== PP_DEFINE
))
2674 "single-line macro `%s' defined both with and"
2675 " without parameters", mname
);
2676 free_tlist(origline
);
2677 free_tlist(macro_start
);
2683 * We're redefining, so we have to take over an
2684 * existing SMacro structure. This means freeing
2685 * what was already in it.
2687 nasm_free(smac
->name
);
2688 free_tlist(smac
->expansion
);
2693 smac
= nasm_malloc(sizeof(SMacro
));
2694 smac
->next
= *smhead
;
2697 smac
->name
= nasm_strdup(mname
);
2698 smac
->casesense
= ((i
== PP_DEFINE
) || (i
== PP_XDEFINE
));
2699 smac
->nparam
= nparam
;
2700 smac
->expansion
= macro_start
;
2701 smac
->in_progress
= FALSE
;
2702 free_tlist(origline
);
2706 tline
= tline
->next
;
2708 tline
= expand_id(tline
);
2709 if (!tline
|| (tline
->type
!= TOK_ID
&&
2710 (tline
->type
!= TOK_PREPROC_ID
||
2711 tline
->text
[1] != '$')))
2713 error(ERR_NONFATAL
, "`%%undef' expects a macro identifier");
2714 free_tlist(origline
);
2720 "trailing garbage after macro name ignored");
2723 /* Find the context that symbol belongs to */
2724 ctx
= get_ctx(tline
->text
, FALSE
);
2726 smhead
= &smacros
[hash(tline
->text
)];
2728 smhead
= &ctx
->localmac
;
2730 mname
= tline
->text
;
2735 * We now have a macro name... go hunt for it.
2737 while (smacro_defined(ctx
, mname
, -1, &smac
, 1))
2739 /* Defined, so we need to find its predecessor and nuke it */
2741 for (s
= smhead
; *s
&& *s
!= smac
; s
= &(*s
)->next
);
2745 nasm_free(smac
->name
);
2746 free_tlist(smac
->expansion
);
2750 free_tlist(origline
);
2754 tline
= tline
->next
;
2756 tline
= expand_id(tline
);
2757 if (!tline
|| (tline
->type
!= TOK_ID
&&
2758 (tline
->type
!= TOK_PREPROC_ID
||
2759 tline
->text
[1] != '$')))
2762 "`%%strlen' expects a macro identifier as first parameter");
2763 free_tlist(origline
);
2766 ctx
= get_ctx(tline
->text
, FALSE
);
2768 smhead
= &smacros
[hash(tline
->text
)];
2770 smhead
= &ctx
->localmac
;
2771 mname
= tline
->text
;
2773 tline
= expand_smacro(tline
->next
);
2777 while (tok_type_(t
, TOK_WHITESPACE
))
2779 /* t should now point to the string */
2780 if (t
->type
!= TOK_STRING
)
2783 "`%%strlen` requires string as second parameter");
2785 free_tlist(origline
);
2789 macro_start
= nasm_malloc(sizeof(*macro_start
));
2790 macro_start
->next
= NULL
;
2791 make_tok_num(macro_start
, strlen(t
->text
) - 2);
2792 macro_start
->mac
= NULL
;
2795 * We now have a macro name, an implicit parameter count of
2796 * zero, and a numeric token to use as an expansion. Create
2797 * and store an SMacro.
2799 if (smacro_defined(ctx
, mname
, 0, &smac
, i
== PP_STRLEN
))
2803 "single-line macro `%s' defined both with and"
2804 " without parameters", mname
);
2808 * We're redefining, so we have to take over an
2809 * existing SMacro structure. This means freeing
2810 * what was already in it.
2812 nasm_free(smac
->name
);
2813 free_tlist(smac
->expansion
);
2818 smac
= nasm_malloc(sizeof(SMacro
));
2819 smac
->next
= *smhead
;
2822 smac
->name
= nasm_strdup(mname
);
2823 smac
->casesense
= (i
== PP_STRLEN
);
2825 smac
->expansion
= macro_start
;
2826 smac
->in_progress
= FALSE
;
2828 free_tlist(origline
);
2832 tline
= tline
->next
;
2834 tline
= expand_id(tline
);
2835 if (!tline
|| (tline
->type
!= TOK_ID
&&
2836 (tline
->type
!= TOK_PREPROC_ID
||
2837 tline
->text
[1] != '$')))
2840 "`%%substr' expects a macro identifier as first parameter");
2841 free_tlist(origline
);
2844 ctx
= get_ctx(tline
->text
, FALSE
);
2846 smhead
= &smacros
[hash(tline
->text
)];
2848 smhead
= &ctx
->localmac
;
2849 mname
= tline
->text
;
2851 tline
= expand_smacro(tline
->next
);
2855 while (tok_type_(t
, TOK_WHITESPACE
))
2858 /* t should now point to the string */
2859 if (t
->type
!= TOK_STRING
)
2862 "`%%substr` requires string as second parameter");
2864 free_tlist(origline
);
2870 tokval
.t_type
= TOKEN_INVALID
;
2872 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2876 free_tlist(origline
);
2879 if (!is_simple(evalresult
))
2881 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
2883 free_tlist(origline
);
2887 macro_start
= nasm_malloc(sizeof(*macro_start
));
2888 macro_start
->next
= NULL
;
2889 macro_start
->text
= nasm_strdup("'''");
2890 if (evalresult
->value
> 0
2891 && evalresult
->value
< strlen(t
->text
) - 1)
2893 macro_start
->text
[1] = t
->text
[evalresult
->value
];
2897 macro_start
->text
[2] = '\0';
2899 macro_start
->type
= TOK_STRING
;
2900 macro_start
->mac
= NULL
;
2903 * We now have a macro name, an implicit parameter count of
2904 * zero, and a numeric token to use as an expansion. Create
2905 * and store an SMacro.
2907 if (smacro_defined(ctx
, mname
, 0, &smac
, i
== PP_SUBSTR
))
2911 "single-line macro `%s' defined both with and"
2912 " without parameters", mname
);
2916 * We're redefining, so we have to take over an
2917 * existing SMacro structure. This means freeing
2918 * what was already in it.
2920 nasm_free(smac
->name
);
2921 free_tlist(smac
->expansion
);
2926 smac
= nasm_malloc(sizeof(SMacro
));
2927 smac
->next
= *smhead
;
2930 smac
->name
= nasm_strdup(mname
);
2931 smac
->casesense
= (i
== PP_SUBSTR
);
2933 smac
->expansion
= macro_start
;
2934 smac
->in_progress
= FALSE
;
2936 free_tlist(origline
);
2942 tline
= tline
->next
;
2944 tline
= expand_id(tline
);
2945 if (!tline
|| (tline
->type
!= TOK_ID
&&
2946 (tline
->type
!= TOK_PREPROC_ID
||
2947 tline
->text
[1] != '$')))
2950 "`%%%sassign' expects a macro identifier",
2951 (i
== PP_IASSIGN
? "i" : ""));
2952 free_tlist(origline
);
2955 ctx
= get_ctx(tline
->text
, FALSE
);
2957 smhead
= &smacros
[hash(tline
->text
)];
2959 smhead
= &ctx
->localmac
;
2960 mname
= tline
->text
;
2962 tline
= expand_smacro(tline
->next
);
2967 tokval
.t_type
= TOKEN_INVALID
;
2969 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2973 free_tlist(origline
);
2979 "trailing garbage after expression ignored");
2981 if (!is_simple(evalresult
))
2984 "non-constant value given to `%%%sassign'",
2985 (i
== PP_IASSIGN
? "i" : ""));
2986 free_tlist(origline
);
2990 macro_start
= nasm_malloc(sizeof(*macro_start
));
2991 macro_start
->next
= NULL
;
2992 make_tok_num(macro_start
, reloc_value(evalresult
));
2993 macro_start
->mac
= NULL
;
2996 * We now have a macro name, an implicit parameter count of
2997 * zero, and a numeric token to use as an expansion. Create
2998 * and store an SMacro.
3000 if (smacro_defined(ctx
, mname
, 0, &smac
, i
== PP_ASSIGN
))
3004 "single-line macro `%s' defined both with and"
3005 " without parameters", mname
);
3009 * We're redefining, so we have to take over an
3010 * existing SMacro structure. This means freeing
3011 * what was already in it.
3013 nasm_free(smac
->name
);
3014 free_tlist(smac
->expansion
);
3019 smac
= nasm_malloc(sizeof(SMacro
));
3020 smac
->next
= *smhead
;
3023 smac
->name
= nasm_strdup(mname
);
3024 smac
->casesense
= (i
== PP_ASSIGN
);
3026 smac
->expansion
= macro_start
;
3027 smac
->in_progress
= FALSE
;
3028 free_tlist(origline
);
3033 * Syntax is `%line nnn[+mmm] [filename]'
3035 tline
= tline
->next
;
3037 if (!tok_type_(tline
, TOK_NUMBER
))
3039 error(ERR_NONFATAL
, "`%%line' expects line number");
3040 free_tlist(origline
);
3043 k
= readnum(tline
->text
, &j
);
3045 tline
= tline
->next
;
3046 if (tok_is_(tline
, "+"))
3048 tline
= tline
->next
;
3049 if (!tok_type_(tline
, TOK_NUMBER
))
3051 error(ERR_NONFATAL
, "`%%line' expects line increment");
3052 free_tlist(origline
);
3055 m
= readnum(tline
->text
, &j
);
3056 tline
= tline
->next
;
3063 nasm_free(src_set_fname(detoken(tline
, FALSE
)));
3065 free_tlist(origline
);
3070 "preprocessor directive `%s' not yet implemented",
3078 * Ensure that a macro parameter contains a condition code and
3079 * nothing else. Return the condition code index if so, or -1
3089 if (t
->type
!= TOK_ID
)
3093 if (tt
&& (tt
->type
!= TOK_OTHER
|| strcmp(tt
->text
, ",")))
3097 j
= sizeof(conditions
) / sizeof(*conditions
);
3101 m
= nasm_stricmp(t
->text
, conditions
[k
]);
3121 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3122 * %-n) and MMacro-local identifiers (%%foo).
3125 expand_mmac_params(Token
* tline
)
3127 Token
*t
, *tt
, **tail
, *thead
;
3134 if (tline
->type
== TOK_PREPROC_ID
&&
3135 (((tline
->text
[1] == '+' || tline
->text
[1] == '-')
3136 && tline
->text
[2]) || tline
->text
[1] == '%'
3137 || (tline
->text
[1] >= '0' && tline
->text
[1] <= '9')))
3140 int type
= 0, cc
; /* type = 0 to placate optimisers */
3146 tline
= tline
->next
;
3149 while (mac
&& !mac
->name
) /* avoid mistaking %reps for macros */
3150 mac
= mac
->next_active
;
3152 error(ERR_NONFATAL
, "`%s': not in a macro call", t
->text
);
3157 * We have to make a substitution of one of the
3158 * forms %1, %-1, %+1, %%foo, %0.
3162 sprintf(tmpbuf
, "%d", mac
->nparam
);
3163 text
= nasm_strdup(tmpbuf
);
3167 sprintf(tmpbuf
, "..@%lu.", mac
->unique
);
3168 text
= nasm_strcat(tmpbuf
, t
->text
+ 2);
3171 n
= atoi(t
->text
+ 2) - 1;
3172 if (n
>= mac
->nparam
)
3176 if (mac
->nparam
> 1)
3177 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3178 tt
= mac
->params
[n
];
3184 "macro parameter %d is not a condition code",
3191 if (inverse_ccs
[cc
] == -1)
3194 "condition code `%s' is not invertible",
3200 nasm_strdup(conditions
[inverse_ccs
3205 n
= atoi(t
->text
+ 2) - 1;
3206 if (n
>= mac
->nparam
)
3210 if (mac
->nparam
> 1)
3211 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3212 tt
= mac
->params
[n
];
3218 "macro parameter %d is not a condition code",
3225 text
= nasm_strdup(conditions
[cc
]);
3229 n
= atoi(t
->text
+ 1) - 1;
3230 if (n
>= mac
->nparam
)
3234 if (mac
->nparam
> 1)
3235 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3236 tt
= mac
->params
[n
];
3240 for (i
= 0; i
< mac
->paramlen
[n
]; i
++)
3243 new_Token(NULL
, tt
->type
, tt
->text
,
3245 tail
= &(*tail
)->next
;
3249 text
= NULL
; /* we've done it here */
3270 tline
= tline
->next
;
3277 for (; t
&& (tt
= t
->next
) != NULL
; t
= t
->next
)
3280 case TOK_WHITESPACE
:
3281 if (tt
->type
== TOK_WHITESPACE
)
3283 t
->next
= delete_Token(tt
);
3287 if (tt
->type
== TOK_ID
|| tt
->type
== TOK_NUMBER
)
3289 char *tmp
= nasm_strcat(t
->text
, tt
->text
);
3292 t
->next
= delete_Token(tt
);
3296 if (tt
->type
== TOK_NUMBER
)
3298 char *tmp
= nasm_strcat(t
->text
, tt
->text
);
3301 t
->next
= delete_Token(tt
);
3310 * Expand all single-line macro calls made in the given line.
3311 * Return the expanded version of the line. The original is deemed
3312 * to be destroyed in the process. (In reality we'll just move
3313 * Tokens from input to output a lot of the time, rather than
3314 * actually bothering to destroy and replicate.)
3317 expand_smacro(Token
* tline
)
3319 Token
*t
, *tt
, *mstart
, **tail
, *thead
;
3320 SMacro
*head
= NULL
, *m
;
3323 int nparam
, sparam
, brackets
, rescan
;
3324 Token
*org_tline
= tline
;
3329 * Trick: we should avoid changing the start token pointer since it can
3330 * be contained in "next" field of other token. Because of this
3331 * we allocate a copy of first token and work with it; at the end of
3332 * routine we copy it back
3337 new_Token(org_tline
->next
, org_tline
->type
, org_tline
->text
,
3339 tline
->mac
= org_tline
->mac
;
3340 nasm_free(org_tline
->text
);
3341 org_tline
->text
= NULL
;
3349 { /* main token loop */
3350 if ((mname
= tline
->text
))
3352 /* if this token is a local macro, look in local context */
3353 if (tline
->type
== TOK_ID
|| tline
->type
== TOK_PREPROC_ID
)
3354 ctx
= get_ctx(mname
, TRUE
);
3358 head
= smacros
[hash(mname
)];
3360 head
= ctx
->localmac
;
3362 * We've hit an identifier. As in is_mmacro below, we first
3363 * check whether the identifier is a single-line macro at
3364 * all, then think about checking for parameters if
3367 for (m
= head
; m
; m
= m
->next
)
3368 if (!mstrcmp(m
->name
, mname
, m
->casesense
))
3378 * Simple case: the macro is parameterless. Discard the
3379 * one token that the macro call took, and push the
3380 * expansion back on the to-do stack.
3384 if (!strcmp("__FILE__", m
->name
))
3387 src_get(&num
, &(tline
->text
));
3388 nasm_quote(&(tline
->text
));
3389 tline
->type
= TOK_STRING
;
3392 if (!strcmp("__LINE__", m
->name
))
3394 nasm_free(tline
->text
);
3395 make_tok_num(tline
, src_get_linnum());
3398 tline
= delete_Token(tline
);
3405 * Complicated case: at least one macro with this name
3406 * exists and takes parameters. We must find the
3407 * parameters in the call, count them, find the SMacro
3408 * that corresponds to that form of the macro call, and
3409 * substitute for the parameters when we expand. What a
3412 tline
= tline
->next
;
3414 if (!tok_is_(tline
, "("))
3417 * This macro wasn't called with parameters: ignore
3418 * the call. (Behaviour borrowed from gnu cpp.)
3429 tline
= tline
->next
;
3430 sparam
= PARAM_DELTA
;
3431 params
= nasm_malloc(sparam
* sizeof(Token
*));
3433 paramsize
= nasm_malloc(sparam
* sizeof(int));
3435 for (;; tline
= tline
->next
)
3436 { /* parameter loop */
3440 "macro call expects terminating `)'");
3443 if (tline
->type
== TOK_WHITESPACE
3446 if (paramsize
[nparam
])
3449 params
[nparam
] = tline
->next
;
3450 continue; /* parameter loop */
3452 if (tline
->type
== TOK_OTHER
3453 && tline
->text
[1] == 0)
3455 char ch
= tline
->text
[0];
3456 if (ch
== ',' && !paren
&& brackets
<= 0)
3458 if (++nparam
>= sparam
)
3460 sparam
+= PARAM_DELTA
;
3461 params
= nasm_realloc(params
,
3462 sparam
* sizeof(Token
*));
3463 paramsize
= nasm_realloc(paramsize
,
3464 sparam
* sizeof(int));
3466 params
[nparam
] = tline
->next
;
3467 paramsize
[nparam
] = 0;
3469 continue; /* parameter loop */
3472 (brackets
> 0 || (brackets
== 0 &&
3473 !paramsize
[nparam
])))
3477 params
[nparam
] = tline
->next
;
3478 continue; /* parameter loop */
3481 if (ch
== '}' && brackets
> 0)
3482 if (--brackets
== 0)
3485 continue; /* parameter loop */
3487 if (ch
== '(' && !brackets
)
3489 if (ch
== ')' && brackets
<= 0)
3496 error(ERR_NONFATAL
, "braces do not "
3497 "enclose all of macro parameter");
3499 paramsize
[nparam
] += white
+ 1;
3501 } /* parameter loop */
3503 while (m
&& (m
->nparam
!= nparam
||
3504 mstrcmp(m
->name
, mname
,
3508 error(ERR_WARNING
| ERR_WARN_MNP
,
3509 "macro `%s' exists, "
3510 "but not taking %d parameters",
3511 mstart
->text
, nparam
);
3514 if (m
&& m
->in_progress
)
3516 if (!m
) /* in progess or didn't find '(' or wrong nparam */
3519 * Design question: should we handle !tline, which
3520 * indicates missing ')' here, or expand those
3521 * macros anyway, which requires the (t) test a few
3525 nasm_free(paramsize
);
3531 * Expand the macro: we are placed on the last token of the
3532 * call, so that we can easily split the call from the
3533 * following tokens. We also start by pushing an SMAC_END
3534 * token for the cycle removal.
3542 tt
= new_Token(tline
, TOK_SMAC_END
, NULL
, 0);
3544 m
->in_progress
= TRUE
;
3546 for (t
= m
->expansion
; t
; t
= t
->next
)
3548 if (t
->type
>= TOK_SMAC_PARAM
)
3550 Token
*pcopy
= tline
, **ptail
= &pcopy
;
3554 ttt
= params
[t
->type
- TOK_SMAC_PARAM
];
3555 for (i
= paramsize
[t
->type
- TOK_SMAC_PARAM
];
3559 new_Token(tline
, ttt
->type
, ttt
->text
,
3568 tt
= new_Token(tline
, t
->type
, t
->text
, 0);
3574 * Having done that, get rid of the macro call, and clean
3575 * up the parameters.
3578 nasm_free(paramsize
);
3580 continue; /* main token loop */
3585 if (tline
->type
== TOK_SMAC_END
)
3587 tline
->mac
->in_progress
= FALSE
;
3588 tline
= delete_Token(tline
);
3593 tline
= tline
->next
;
3601 * Now scan the entire line and look for successive TOK_IDs that resulted
3602 * after expansion (they can't be produced by tokenise()). The successive
3603 * TOK_IDs should be concatenated.
3604 * Also we look for %+ tokens and concatenate the tokens before and after
3605 * them (without white spaces in between).
3611 while (t
&& t
->type
!= TOK_ID
&& t
->type
!= TOK_PREPROC_ID
)
3615 if (t
->next
->type
== TOK_ID
||
3616 t
->next
->type
== TOK_PREPROC_ID
||
3617 t
->next
->type
== TOK_NUMBER
)
3619 char *p
= nasm_strcat(t
->text
, t
->next
->text
);
3621 t
->next
= delete_Token(t
->next
);
3625 else if (t
->next
->type
== TOK_WHITESPACE
&& t
->next
->next
&&
3626 t
->next
->next
->type
== TOK_PREPROC_ID
&&
3627 strcmp(t
->next
->next
->text
, "%+") == 0)
3629 /* free the next whitespace, the %+ token and next whitespace */
3631 for (i
= 1; i
<= 3; i
++)
3633 if (!t
->next
|| (i
!= 2 && t
->next
->type
!= TOK_WHITESPACE
))
3635 t
->next
= delete_Token(t
->next
);
3641 /* If we concatenaded something, re-scan the line for macros */
3652 *org_tline
= *thead
;
3653 /* since we just gave text to org_line, don't free it */
3655 delete_Token(thead
);
3659 /* the expression expanded to empty line;
3660 we can't return NULL for some reasons
3661 we just set the line to a single WHITESPACE token. */
3662 memset(org_tline
, 0, sizeof(*org_tline
));
3663 org_tline
->text
= NULL
;
3664 org_tline
->type
= TOK_WHITESPACE
;
3673 * Similar to expand_smacro but used exclusively with macro identifiers
3674 * right before they are fetched in. The reason is that there can be
3675 * identifiers consisting of several subparts. We consider that if there
3676 * are more than one element forming the name, user wants a expansion,
3677 * otherwise it will be left as-is. Example:
3681 * the identifier %$abc will be left as-is so that the handler for %define
3682 * will suck it and define the corresponding value. Other case:
3684 * %define _%$abc cde
3686 * In this case user wants name to be expanded *before* %define starts
3687 * working, so we'll expand %$abc into something (if it has a value;
3688 * otherwise it will be left as-is) then concatenate all successive
3692 expand_id(Token
* tline
)
3694 Token
*cur
, *oldnext
= NULL
;
3696 if (!tline
|| !tline
->next
)
3701 (cur
->next
->type
== TOK_ID
||
3702 cur
->next
->type
== TOK_PREPROC_ID
|| cur
->next
->type
== TOK_NUMBER
))
3705 /* If identifier consists of just one token, don't expand */
3711 oldnext
= cur
->next
; /* Detach the tail past identifier */
3712 cur
->next
= NULL
; /* so that expand_smacro stops here */
3715 tline
= expand_smacro(tline
);
3719 /* expand_smacro possibly changhed tline; re-scan for EOL */
3721 while (cur
&& cur
->next
)
3724 cur
->next
= oldnext
;
3731 * Determine whether the given line constitutes a multi-line macro
3732 * call, and return the MMacro structure called if so. Doesn't have
3733 * to check for an initial label - that's taken care of in
3734 * expand_mmacro - but must check numbers of parameters. Guaranteed
3735 * to be called with tline->type == TOK_ID, so the putative macro
3736 * name is easy to find.
3739 is_mmacro(Token
* tline
, Token
*** params_array
)
3745 head
= mmacros
[hash(tline
->text
)];
3748 * Efficiency: first we see if any macro exists with the given
3749 * name. If not, we can return NULL immediately. _Then_ we
3750 * count the parameters, and then we look further along the
3751 * list if necessary to find the proper MMacro.
3753 for (m
= head
; m
; m
= m
->next
)
3754 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
3760 * OK, we have a potential macro. Count and demarcate the
3763 count_mmac_params(tline
->next
, &nparam
, ¶ms
);
3766 * So we know how many parameters we've got. Find the MMacro
3767 * structure that handles this number.
3771 if (m
->nparam_min
<= nparam
&& (m
->plus
|| nparam
<= m
->nparam_max
))
3774 * This one is right. Just check if cycle removal
3775 * prohibits us using it before we actually celebrate...
3781 "self-reference in multi-line macro `%s'", m
->name
);
3787 * It's right, and we can use it. Add its default
3788 * parameters to the end of our list if necessary.
3790 if (m
->defaults
&& nparam
< m
->nparam_min
+ m
->ndefs
)
3793 nasm_realloc(params
,
3794 ((m
->nparam_min
+ m
->ndefs
+ 1) * sizeof(*params
)));
3795 while (nparam
< m
->nparam_min
+ m
->ndefs
)
3797 params
[nparam
] = m
->defaults
[nparam
- m
->nparam_min
];
3802 * If we've gone over the maximum parameter count (and
3803 * we're in Plus mode), ignore parameters beyond
3806 if (m
->plus
&& nparam
> m
->nparam_max
)
3807 nparam
= m
->nparam_max
;
3809 * Then terminate the parameter list, and leave.
3812 { /* need this special case */
3813 params
= nasm_malloc(sizeof(*params
));
3816 params
[nparam
] = NULL
;
3817 *params_array
= params
;
3821 * This one wasn't right: look for the next one with the
3824 for (m
= m
->next
; m
; m
= m
->next
)
3825 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
3830 * After all that, we didn't find one with the right number of
3831 * parameters. Issue a warning, and fail to expand the macro.
3833 error(ERR_WARNING
| ERR_WARN_MNP
,
3834 "macro `%s' exists, but not taking %d parameters",
3835 tline
->text
, nparam
);
3841 * Expand the multi-line macro call made by the given line, if
3842 * there is one to be expanded. If there is, push the expansion on
3843 * istk->expansion and return 1. Otherwise return 0.
3846 expand_mmacro(Token
* tline
)
3848 Token
*startline
= tline
;
3849 Token
*label
= NULL
;
3850 int dont_prepend
= 0;
3851 Token
**params
, *t
, *tt
;
3854 int i
, nparam
, *paramlen
;
3858 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
3859 if (!tok_type_(t
, TOK_ID
) && !tok_type_(t
, TOK_PREPROC_ID
))
3861 m
= is_mmacro(t
, ¶ms
);
3866 * We have an id which isn't a macro call. We'll assume
3867 * it might be a label; we'll also check to see if a
3868 * colon follows it. Then, if there's another id after
3869 * that lot, we'll check it again for macro-hood.
3873 if (tok_type_(t
, TOK_WHITESPACE
))
3874 last
= t
, t
= t
->next
;
3875 if (tok_is_(t
, ":"))
3878 last
= t
, t
= t
->next
;
3879 if (tok_type_(t
, TOK_WHITESPACE
))
3880 last
= t
, t
= t
->next
;
3882 if (!tok_type_(t
, TOK_ID
) || (m
= is_mmacro(t
, ¶ms
)) == NULL
)
3889 * Fix up the parameters: this involves stripping leading and
3890 * trailing whitespace, then stripping braces if they are
3893 for (nparam
= 0; params
[nparam
]; nparam
++)
3895 paramlen
= nparam
? nasm_malloc(nparam
* sizeof(*paramlen
)) : NULL
;
3897 for (i
= 0; params
[i
]; i
++)
3900 int comma
= (!m
->plus
|| i
< nparam
- 1);
3904 if (tok_is_(t
, "{"))
3905 t
= t
->next
, brace
= TRUE
, comma
= FALSE
;
3910 if (comma
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, ","))
3911 break; /* ... because we have hit a comma */
3912 if (comma
&& t
->type
== TOK_WHITESPACE
&& tok_is_(t
->next
, ","))
3913 break; /* ... or a space then a comma */
3914 if (brace
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, "}"))
3915 break; /* ... or a brace */
3922 * OK, we have a MMacro structure together with a set of
3923 * parameters. We must now go through the expansion and push
3924 * copies of each Line on to istk->expansion. Substitution of
3925 * parameter tokens and macro-local tokens doesn't get done
3926 * until the single-line macro substitution process; this is
3927 * because delaying them allows us to change the semantics
3928 * later through %rotate.
3930 * First, push an end marker on to istk->expansion, mark this
3931 * macro as in progress, and set up its invocation-specific
3934 ll
= nasm_malloc(sizeof(Line
));
3935 ll
->next
= istk
->expansion
;
3938 istk
->expansion
= ll
;
3940 m
->in_progress
= TRUE
;
3945 m
->paramlen
= paramlen
;
3946 m
->unique
= unique
++;
3949 m
->next_active
= istk
->mstk
;
3952 for (l
= m
->expansion
; l
; l
= l
->next
)
3956 ll
= nasm_malloc(sizeof(Line
));
3957 ll
->finishes
= NULL
;
3958 ll
->next
= istk
->expansion
;
3959 istk
->expansion
= ll
;
3962 for (t
= l
->first
; t
; t
= t
->next
)
3965 if (t
->type
== TOK_PREPROC_ID
&&
3966 t
->text
[1] == '0' && t
->text
[2] == '0')
3973 tt
= *tail
= new_Token(NULL
, x
->type
, x
->text
, 0);
3980 * If we had a label, push it on as the first line of
3981 * the macro expansion.
3985 if (dont_prepend
< 0)
3986 free_tlist(startline
);
3989 ll
= nasm_malloc(sizeof(Line
));
3990 ll
->finishes
= NULL
;
3991 ll
->next
= istk
->expansion
;
3992 istk
->expansion
= ll
;
3993 ll
->first
= startline
;
3997 label
= label
->next
;
3998 label
->next
= tt
= new_Token(NULL
, TOK_OTHER
, ":", 0);
4003 list
->uplevel(m
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
4009 * Since preprocessor always operate only on the line that didn't
4010 * arrived yet, we should always use ERR_OFFBY1. Also since user
4011 * won't want to see same error twice (preprocessing is done once
4012 * per pass) we will want to show errors only during pass one.
4015 error(int severity
, const char *fmt
, ...)
4020 /* If we're in a dead branch of IF or something like it, ignore the error */
4021 if (istk
&& istk
->conds
&& !emitting(istk
->conds
->state
))
4025 vsprintf(buff
, fmt
, arg
);
4028 if (istk
&& istk
->mstk
&& istk
->mstk
->name
)
4029 _error(severity
| ERR_PASS1
, "(%s:%d) %s", istk
->mstk
->name
,
4030 istk
->mstk
->lineno
, buff
);
4032 _error(severity
| ERR_PASS1
, "%s", buff
);
4036 pp_reset(char *file
, int apass
, efunc errfunc
, evalfunc eval
,
4043 istk
= nasm_malloc(sizeof(Include
));
4046 istk
->expansion
= NULL
;
4048 istk
->fp
= fopen(file
, "r");
4050 src_set_fname(nasm_strdup(file
));
4054 error(ERR_FATAL
| ERR_NOFILE
, "unable to open input file `%s'", file
);
4056 for (h
= 0; h
< NHASH
; h
++)
4062 if (tasm_compatible_mode
) {
4065 stdmacpos
= &stdmac
[TASM_MACRO_COUNT
];
4067 any_extrastdmac
= (extrastdmac
!= NULL
);
4082 * Fetch a tokenised line, either from the macro-expansion
4083 * buffer or from the input file.
4086 while (istk
->expansion
&& istk
->expansion
->finishes
)
4088 Line
*l
= istk
->expansion
;
4089 if (!l
->finishes
->name
&& l
->finishes
->in_progress
> 1)
4094 * This is a macro-end marker for a macro with no
4095 * name, which means it's not really a macro at all
4096 * but a %rep block, and the `in_progress' field is
4097 * more than 1, meaning that we still need to
4098 * repeat. (1 means the natural last repetition; 0
4099 * means termination by %exitrep.) We have
4100 * therefore expanded up to the %endrep, and must
4101 * push the whole block on to the expansion buffer
4102 * again. We don't bother to remove the macro-end
4103 * marker: we'd only have to generate another one
4106 l
->finishes
->in_progress
--;
4107 for (l
= l
->finishes
->expansion
; l
; l
= l
->next
)
4109 Token
*t
, *tt
, **tail
;
4111 ll
= nasm_malloc(sizeof(Line
));
4112 ll
->next
= istk
->expansion
;
4113 ll
->finishes
= NULL
;
4117 for (t
= l
->first
; t
; t
= t
->next
)
4119 if (t
->text
|| t
->type
== TOK_WHITESPACE
)
4121 tt
= *tail
= new_Token(NULL
, t
->type
, t
->text
, 0);
4126 istk
->expansion
= ll
;
4132 * Check whether a `%rep' was started and not ended
4133 * within this macro expansion. This can happen and
4134 * should be detected. It's a fatal error because
4135 * I'm too confused to work out how to recover
4141 error(ERR_PANIC
, "defining with name in expansion");
4142 else if (istk
->mstk
->name
)
4143 error(ERR_FATAL
, "`%%rep' without `%%endrep' within"
4144 " expansion of macro `%s'", istk
->mstk
->name
);
4148 * FIXME: investigate the relationship at this point between
4149 * istk->mstk and l->finishes
4152 MMacro
*m
= istk
->mstk
;
4153 istk
->mstk
= m
->next_active
;
4157 * This was a real macro call, not a %rep, and
4158 * therefore the parameter information needs to
4161 nasm_free(m
->params
);
4162 free_tlist(m
->iline
);
4163 nasm_free(m
->paramlen
);
4164 l
->finishes
->in_progress
= FALSE
;
4169 istk
->expansion
= l
->next
;
4171 list
->downlevel(LIST_MACRO
);
4175 { /* until we get a line we can use */
4177 if (istk
->expansion
)
4178 { /* from a macro expansion */
4180 Line
*l
= istk
->expansion
;
4182 istk
->mstk
->lineno
++;
4184 istk
->expansion
= l
->next
;
4186 p
= detoken(tline
, FALSE
);
4187 list
->line(LIST_MACRO
, p
);
4193 { /* from the current input file */
4194 line
= prepreproc(line
);
4195 tline
= tokenise(line
);
4200 * The current file has ended; work down the istk
4206 error(ERR_FATAL
, "expected `%%endif' before end of file");
4207 /* only set line and file name if there's a next node */
4210 src_set_linnum(i
->lineno
);
4211 nasm_free(src_set_fname(i
->fname
));
4214 list
->downlevel(LIST_INCLUDE
);
4222 * We must expand MMacro parameters and MMacro-local labels
4223 * _before_ we plunge into directive processing, to cope
4224 * with things like `%define something %1' such as STRUC
4225 * uses. Unless we're _defining_ a MMacro, in which case
4226 * those tokens should be left alone to go into the
4227 * definition; and unless we're in a non-emitting
4228 * condition, in which case we don't want to meddle with
4231 if (!defining
&& !(istk
->conds
&& !emitting(istk
->conds
->state
)))
4232 tline
= expand_mmac_params(tline
);
4235 * Check the line to see if it's a preprocessor directive.
4237 if (do_directive(tline
) & 1)
4244 * We're defining a multi-line macro. We emit nothing
4246 * shove the tokenised line on to the macro definition.
4248 Line
*l
= nasm_malloc(sizeof(Line
));
4249 l
->next
= defining
->expansion
;
4251 l
->finishes
= FALSE
;
4252 defining
->expansion
= l
;
4255 else if (istk
->conds
&& !emitting(istk
->conds
->state
))
4258 * We're in a non-emitting branch of a condition block.
4259 * Emit nothing at all, not even a blank line: when we
4260 * emerge from the condition we'll give a line-number
4261 * directive so we keep our place correctly.
4266 else if (istk
->mstk
&& !istk
->mstk
->in_progress
)
4269 * We're in a %rep block which has been terminated, so
4270 * we're walking through to the %endrep without
4271 * emitting anything. Emit nothing at all, not even a
4272 * blank line: when we emerge from the %rep block we'll
4273 * give a line-number directive so we keep our place
4281 tline
= expand_smacro(tline
);
4282 if (!expand_mmacro(tline
))
4285 * De-tokenise the line again, and emit it.
4287 line
= detoken(tline
, TRUE
);
4293 continue; /* expand_mmacro calls free_tlist */
4302 pp_cleanup(int pass
)
4308 error(ERR_NONFATAL
, "end of file while still defining macro `%s'",
4310 free_mmacro(defining
);
4314 for (h
= 0; h
< NHASH
; h
++)
4318 MMacro
*m
= mmacros
[h
];
4319 mmacros
[h
] = mmacros
[h
]->next
;
4324 SMacro
*s
= smacros
[h
];
4325 smacros
[h
] = smacros
[h
]->next
;
4327 free_tlist(s
->expansion
);
4336 nasm_free(i
->fname
);
4349 pp_include_path(char *path
)
4353 i
= nasm_malloc(sizeof(IncPath
));
4354 i
->path
= nasm_strdup(path
);
4360 pp_pre_include(char *fname
)
4362 Token
*inc
, *space
, *name
;
4365 name
= new_Token(NULL
, TOK_INTERNAL_STRING
, fname
, 0);
4366 space
= new_Token(name
, TOK_WHITESPACE
, NULL
, 0);
4367 inc
= new_Token(space
, TOK_PREPROC_ID
, "%include", 0);
4369 l
= nasm_malloc(sizeof(Line
));
4372 l
->finishes
= FALSE
;
4377 pp_pre_define(char *definition
)
4383 equals
= strchr(definition
, '=');
4384 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
4385 def
= new_Token(space
, TOK_PREPROC_ID
, "%define", 0);
4388 space
->next
= tokenise(definition
);
4392 l
= nasm_malloc(sizeof(Line
));
4395 l
->finishes
= FALSE
;
4400 pp_pre_undefine(char *definition
)
4405 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
4406 def
= new_Token(space
, TOK_PREPROC_ID
, "%undef", 0);
4408 l
= nasm_malloc(sizeof(Line
));
4411 l
->finishes
= FALSE
;
4416 pp_extra_stdmac(char **macros
)
4418 extrastdmac
= macros
;
4422 make_tok_num(Token
* tok
, long val
)
4425 sprintf(numbuf
, "%ld", val
);
4426 tok
->text
= nasm_strdup(numbuf
);
4427 tok
->type
= TOK_NUMBER
;