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 * These defines are used as the possible return values for do_directive
254 #define NO_DIRECTIVE_FOUND 0
255 #define DIRECTIVE_FOUND 1
258 * Condition codes. Note that we use c_ prefix not C_ because C_ is
259 * used in nasm.h for the "real" condition codes. At _this_ level,
260 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
261 * ones, so we need a different enum...
263 static const char *conditions
[] = {
264 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
265 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
266 "np", "ns", "nz", "o", "p", "pe", "po", "s", "z"
270 c_A
, c_AE
, c_B
, c_BE
, c_C
, c_CXZ
, c_E
, c_ECXZ
, c_G
, c_GE
, c_L
, c_LE
,
271 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, c_NE
, c_NG
, c_NGE
, c_NL
, c_NLE
, c_NO
,
272 c_NP
, c_NS
, c_NZ
, c_O
, c_P
, c_PE
, c_PO
, c_S
, c_Z
274 static int inverse_ccs
[] = {
275 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, -1, c_NE
, -1, c_NG
, c_NGE
, c_NL
, c_NLE
,
276 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
,
277 c_Z
, c_NO
, c_NP
, c_PO
, c_PE
, c_NS
, c_NZ
283 static const char *directives
[] = {
285 "%assign", "%clear", "%define", "%elif", "%elifctx", "%elifdef",
286 "%elifid", "%elifidn", "%elifidni", "%elifmacro", "%elifnctx", "%elifndef",
287 "%elifnid", "%elifnidn", "%elifnidni", "%elifnmacro", "%elifnnum", "%elifnstr",
288 "%elifnum", "%elifstr", "%else", "%endif", "%endm", "%endmacro",
289 "%endrep", "%error", "%exitrep", "%iassign", "%idefine", "%if",
290 "%ifctx", "%ifdef", "%ifid", "%ifidn", "%ifidni", "%ifmacro", "%ifnctx",
291 "%ifndef", "%ifnid", "%ifnidn", "%ifnidni", "%ifnmacro", "%ifnnum",
292 "%ifnstr", "%ifnum", "%ifstr", "%imacro", "%include",
293 "%ixdefine", "%line",
295 "%macro", "%pop", "%push", "%rep", "%repl", "%rotate",
297 "%strlen", "%substr", "%undef", "%xdefine"
302 PP_ASSIGN
, PP_CLEAR
, PP_DEFINE
, PP_ELIF
, PP_ELIFCTX
, PP_ELIFDEF
,
303 PP_ELIFID
, PP_ELIFIDN
, PP_ELIFIDNI
, PP_ELIFMACRO
, PP_ELIFNCTX
, PP_ELIFNDEF
,
304 PP_ELIFNID
, PP_ELIFNIDN
, PP_ELIFNIDNI
, PP_ELIFNMACRO
, PP_ELIFNNUM
, PP_ELIFNSTR
,
305 PP_ELIFNUM
, PP_ELIFSTR
, PP_ELSE
, PP_ENDIF
, PP_ENDM
, PP_ENDMACRO
,
306 PP_ENDREP
, PP_ERROR
, PP_EXITREP
, PP_IASSIGN
, PP_IDEFINE
, PP_IF
,
307 PP_IFCTX
, PP_IFDEF
, PP_IFID
, PP_IFIDN
, PP_IFIDNI
, PP_IFMACRO
, PP_IFNCTX
,
308 PP_IFNDEF
, PP_IFNID
, PP_IFNIDN
, PP_IFNIDNI
, PP_IFNMACRO
, PP_IFNNUM
,
309 PP_IFNSTR
, PP_IFNUM
, PP_IFSTR
, PP_IMACRO
, PP_INCLUDE
,
310 PP_IXDEFINE
, PP_LINE
,
312 PP_MACRO
, PP_POP
, PP_PUSH
, PP_REP
, PP_REPL
, PP_ROTATE
,
314 PP_STRLEN
, PP_SUBSTR
, PP_UNDEF
, PP_XDEFINE
317 /* If this is a an IF, ELIF, ELSE or ENDIF keyword */
318 static int is_condition(int arg
)
320 return ((arg
>= PP_ELIF
) && (arg
<= PP_ENDIF
)) ||
321 ((arg
>= PP_IF
) && (arg
<= PP_IFSTR
));
324 /* For TASM compatibility we need to be able to recognise TASM compatible
325 * conditional compilation directives. Using the NASM pre-processor does
326 * not work, so we look for them specifically from the following list and
327 * then jam in the equivalent NASM directive into the input stream.
331 # define MAX(a,b) ( ((a) > (b)) ? (a) : (b))
336 TM_ARG
, TM_ELIF
, TM_ELSE
, TM_ENDIF
, TM_IF
, TM_IFDEF
, TM_IFDIFI
,
337 TM_IFNDEF
, TM_INCLUDE
, TM_LOCAL
340 static const char *tasm_directives
[] = {
341 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
342 "ifndef", "include", "local"
345 static int StackSize
= 4;
346 static char *StackPointer
= "ebp";
347 static int ArgOffset
= 8;
348 static int LocalOffset
= 4;
351 static Context
*cstk
;
352 static Include
*istk
;
353 static IncPath
*ipath
= NULL
;
355 static efunc _error
; /* Pointer to client-provided error reporting function */
356 static evalfunc evaluate
;
358 static int pass
; /* HACK: pass 0 = generate dependencies only */
360 static unsigned long unique
; /* unique identifier numbers */
362 static Line
*predef
= NULL
;
364 static ListGen
*list
;
367 * The number of hash values we use for the macro lookup tables.
368 * FIXME: We should *really* be able to configure this at run time,
369 * or even have the hash table automatically expanding when necessary.
374 * The current set of multi-line macros we have defined.
376 static MMacro
*mmacros
[NHASH
];
379 * The current set of single-line macros we have defined.
381 static SMacro
*smacros
[NHASH
];
384 * The multi-line macro we are currently defining, or the %rep
385 * block we are currently reading, if any.
387 static MMacro
*defining
;
390 * The number of macro parameters to allocate space for at a time.
392 #define PARAM_DELTA 16
395 * The standard macro set: defined as `static char *stdmac[]'. Also
396 * gives our position in the macro set, when we're processing it.
399 static const char **stdmacpos
;
402 * The extra standard macros that come from the object format, if
405 static const char **extrastdmac
= NULL
;
409 * Tokens are allocated in blocks to improve speed
411 #define TOKEN_BLOCKSIZE 4096
412 static Token
*freeTokens
= NULL
;
418 static Blocks blocks
= { NULL
, NULL
};
421 * Forward declarations.
423 static Token
*expand_mmac_params(Token
* tline
);
424 static Token
*expand_smacro(Token
* tline
);
425 static Token
*expand_id(Token
* tline
);
426 static Context
*get_ctx(char *name
, int all_contexts
);
427 static void make_tok_num(Token
* tok
, long val
);
428 static void error(int severity
, const char *fmt
, ...);
429 static void *new_Block(size_t size
);
430 static void delete_Blocks(void);
431 static Token
*new_Token(Token
* next
, int type
, char *text
, int txtlen
);
432 static Token
*delete_Token(Token
* t
);
435 * Macros for safe checking of token pointers, avoid *(NULL)
437 #define tok_type_(x,t) ((x) && (x)->type == (t))
438 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
439 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
440 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
442 /* Handle TASM specific directives, which do not contain a % in
443 * front of them. We do it here because I could not find any other
444 * place to do it for the moment, and it is a hack (ideally it would
445 * be nice to be able to use the NASM pre-processor to do it).
448 check_tasm_directive(char *line
)
451 char *p
= line
, *oldline
, oldchar
;
453 /* Skip whitespace */
454 while (isspace(*p
) && *p
!= 0)
457 /* Binary search for the directive name */
459 j
= elements(tasm_directives
);
461 while (!isspace(p
[len
]) && p
[len
] != 0)
470 m
= nasm_stricmp(p
, tasm_directives
[k
]);
473 /* We have found a directive, so jam a % in front of it
474 * so that NASM will then recognise it as one if it's own.
479 line
= nasm_malloc(len
+ 2);
483 /* NASM does not recognise IFDIFI, so we convert it to
484 * %ifdef BOGUS. This is not used in NASM comaptible
485 * code, but does need to parse for the TASM macro
488 strcpy(line
+ 1, "ifdef BOGUS");
492 memcpy(line
+ 1, p
, len
+ 1);
510 * The pre-preprocessing stage... This function translates line
511 * number indications as they emerge from GNU cpp (`# lineno "file"
512 * flags') into NASM preprocessor line number indications (`%line
516 prepreproc(char *line
)
519 char *fname
, *oldline
;
521 if (line
[0] == '#' && line
[1] == ' ')
525 lineno
= atoi(fname
);
526 fname
+= strspn(fname
, "0123456789 ");
529 fnlen
= strcspn(fname
, "\"");
530 line
= nasm_malloc(20 + fnlen
);
531 sprintf(line
, "%%line %d %.*s", lineno
, fnlen
, fname
);
534 if (tasm_compatible_mode
)
535 return check_tasm_directive(line
);
540 * The hash function for macro lookups. Note that due to some
541 * macros having case-insensitive names, the hash function must be
542 * invariant under case changes. We implement this by applying a
543 * perfectly normal hash function to the uppercase of the string.
551 * Powers of three, mod 31.
553 static const int multipliers
[] = {
554 1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10,
555 30, 28, 22, 4, 12, 5, 15, 14, 11, 2, 6, 18, 23, 7, 21
561 h
+= multipliers
[i
] * (unsigned char) (toupper(*s
));
563 if (++i
>= elements(multipliers
))
571 * Free a linked list of tokens.
574 free_tlist(Token
* list
)
578 list
= delete_Token(list
);
583 * Free a linked list of lines.
586 free_llist(Line
* list
)
593 free_tlist(l
->first
);
602 free_mmacro(MMacro
* m
)
605 free_tlist(m
->dlist
);
606 nasm_free(m
->defaults
);
607 free_llist(m
->expansion
);
612 * Pop the context stack.
627 free_tlist(s
->expansion
);
634 #define BUF_DELTA 512
636 * Read a line from the top file in istk, handling multiple CR/LFs
637 * at the end of the line read, and handling spurious ^Zs. Will
638 * return lines from the standard macro set if this has not already
644 char *buffer
, *p
, *q
;
645 int bufsize
, continued_count
;
651 char *ret
= nasm_strdup(*stdmacpos
++);
652 if (!*stdmacpos
&& any_extrastdmac
)
654 stdmacpos
= extrastdmac
;
655 any_extrastdmac
= FALSE
;
659 * Nasty hack: here we push the contents of `predef' on
660 * to the top-level expansion stack, since this is the
661 * most convenient way to implement the pre-include and
662 * pre-define features.
667 Token
*head
, **tail
, *t
;
669 for (pd
= predef
; pd
; pd
= pd
->next
)
673 for (t
= pd
->first
; t
; t
= t
->next
)
675 *tail
= new_Token(NULL
, t
->type
, t
->text
, 0);
676 tail
= &(*tail
)->next
;
678 l
= nasm_malloc(sizeof(Line
));
679 l
->next
= istk
->expansion
;
694 buffer
= nasm_malloc(BUF_DELTA
);
699 q
= fgets(p
, bufsize
- (p
- buffer
), istk
->fp
);
703 if (p
> buffer
&& p
[-1] == '\n')
705 /* Convert backslash-CRLF line continuation sequences into
706 nothing at all (for DOS and Windows) */
707 if (((p
- 2) > buffer
) && (p
[-3] == '\\') && (p
[-2] == '\r')) {
712 /* Also convert backslash-LF line continuation sequences into
713 nothing at all (for Unix) */
714 else if (((p
- 1) > buffer
) && (p
[-2] == '\\')) {
723 if (p
- buffer
> bufsize
- 10)
725 long offset
= p
- buffer
;
726 bufsize
+= BUF_DELTA
;
727 buffer
= nasm_realloc(buffer
, bufsize
);
728 p
= buffer
+ offset
; /* prevent stale-pointer problems */
732 if (!q
&& p
== buffer
)
738 src_set_linnum(src_get_linnum() + istk
->lineinc
+ (continued_count
* istk
->lineinc
));
741 * Play safe: remove CRs as well as LFs, if any of either are
742 * present at the end of the line.
744 while (--p
>= buffer
&& (*p
== '\n' || *p
== '\r'))
748 * Handle spurious ^Z, which may be inserted into source files
749 * by some file transfer utilities.
751 buffer
[strcspn(buffer
, "\032")] = '\0';
753 list
->line(LIST_READ
, buffer
);
759 * Tokenise a line of text. This is a very simple process since we
760 * don't need to parse the value out of e.g. numeric tokens: we
761 * simply split one string into many.
769 Token
*t
, **tail
= &list
;
778 ((*p
== '-' || *p
== '+') && isdigit(p
[1])) ||
779 ((*p
== '+') && (isspace(p
[1]) || !p
[1])))
786 type
= TOK_PREPROC_ID
;
791 while (*p
&& *p
!= '}')
799 type
= TOK_PREPROC_ID
;
801 else if (isidchar(*p
) ||
802 ((*p
== '!' || *p
== '%' || *p
== '$') &&
809 while (isidchar(*p
));
810 type
= TOK_PREPROC_ID
;
819 else if (isidstart(*p
) || (*p
== '$' && isidstart(p
[1])))
823 while (*p
&& isidchar(*p
))
826 else if (*p
== '\'' || *p
== '"')
834 while (*p
&& *p
!= c
)
842 error(ERR_WARNING
, "unterminated string");
845 else if (isnumstart(*p
))
852 while (*p
&& isnumchar(*p
))
855 else if (isspace(*p
))
857 type
= TOK_WHITESPACE
;
859 while (*p
&& isspace(*p
))
862 * Whitespace just before end-of-line is discarded by
863 * pretending it's a comment; whitespace just before a
864 * comment gets lumped into the comment.
866 if (!*p
|| *p
== ';')
882 * Anything else is an operator of some kind. We check
883 * for all the double-character operators (>>, <<, //,
884 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
885 * else is a single-character operator.
888 if ((p
[0] == '>' && p
[1] == '>') ||
889 (p
[0] == '<' && p
[1] == '<') ||
890 (p
[0] == '/' && p
[1] == '/') ||
891 (p
[0] == '<' && p
[1] == '=') ||
892 (p
[0] == '>' && p
[1] == '=') ||
893 (p
[0] == '=' && p
[1] == '=') ||
894 (p
[0] == '!' && p
[1] == '=') ||
895 (p
[0] == '<' && p
[1] == '>') ||
896 (p
[0] == '&' && p
[1] == '&') ||
897 (p
[0] == '|' && p
[1] == '|') ||
898 (p
[0] == '^' && p
[1] == '^'))
904 if (type
!= TOK_COMMENT
)
906 *tail
= t
= new_Token(NULL
, type
, line
, p
- line
);
915 * this function allocates a new managed block of memory and
916 * returns a pointer to the block. The managed blocks are
917 * deleted only all at once by the delete_Blocks function.
920 new_Block(size_t size
)
924 /* first, get to the end of the linked list */
927 /* now allocate the requested chunk */
928 b
->chunk
= nasm_malloc(size
);
930 /* now allocate a new block for the next request */
931 b
->next
= nasm_malloc(sizeof(Blocks
));
932 /* and initialize the contents of the new block */
933 b
->next
->next
= NULL
;
934 b
->next
->chunk
= NULL
;
939 * this function deletes all managed blocks of memory
944 Blocks
*a
,*b
= &blocks
;
947 * keep in mind that the first block, pointed to by blocks
948 * is a static and not dynamically allocated, so we don't
963 * this function creates a new Token and passes a pointer to it
964 * back to the caller. It sets the type and text elements, and
965 * also the mac and next elements to NULL.
968 new_Token(Token
* next
, int type
, char *text
, int txtlen
)
973 if (freeTokens
== NULL
)
975 freeTokens
= (Token
*)new_Block(TOKEN_BLOCKSIZE
* sizeof(Token
));
976 for (i
= 0; i
< TOKEN_BLOCKSIZE
- 1; i
++)
977 freeTokens
[i
].next
= &freeTokens
[i
+ 1];
978 freeTokens
[i
].next
= NULL
;
981 freeTokens
= t
->next
;
985 if (type
== TOK_WHITESPACE
|| text
== NULL
)
992 txtlen
= strlen(text
);
993 t
->text
= nasm_malloc(1 + txtlen
);
994 strncpy(t
->text
, text
, txtlen
);
995 t
->text
[txtlen
] = '\0';
1001 delete_Token(Token
* t
)
1003 Token
*next
= t
->next
;
1005 t
->next
= freeTokens
;
1011 * Convert a line of tokens back into text.
1012 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1013 * will be transformed into ..@ctxnum.xxx
1016 detoken(Token
* tlist
, int expand_locals
)
1023 for (t
= tlist
; t
; t
= t
->next
)
1025 if (t
->type
== TOK_PREPROC_ID
&& t
->text
[1] == '!')
1027 char *p
= getenv(t
->text
+ 2);
1030 t
->text
= nasm_strdup(p
);
1034 /* Expand local macros here and not during preprocessing */
1035 if (expand_locals
&&
1036 t
->type
== TOK_PREPROC_ID
&& t
->text
&&
1037 t
->text
[0] == '%' && t
->text
[1] == '$')
1039 Context
*ctx
= get_ctx(t
->text
, FALSE
);
1043 char *p
, *q
= t
->text
+ 2;
1045 q
+= strspn(q
, "$");
1046 sprintf(buffer
, "..@%lu.", ctx
->number
);
1047 p
= nasm_strcat(buffer
, q
);
1052 if (t
->type
== TOK_WHITESPACE
)
1058 len
+= strlen(t
->text
);
1061 p
= line
= nasm_malloc(len
+ 1);
1062 for (t
= tlist
; t
; t
= t
->next
)
1064 if (t
->type
== TOK_WHITESPACE
)
1081 * A scanner, suitable for use by the expression evaluator, which
1082 * operates on a line of Tokens. Expects a pointer to a pointer to
1083 * the first token in the line to be passed in as its private_data
1087 ppscan(void *private_data
, struct tokenval
*tokval
)
1089 Token
**tlineptr
= private_data
;
1095 *tlineptr
= tline
? tline
->next
: NULL
;
1097 while (tline
&& (tline
->type
== TOK_WHITESPACE
||
1098 tline
->type
== TOK_COMMENT
));
1101 return tokval
->t_type
= TOKEN_EOS
;
1103 if (tline
->text
[0] == '$' && !tline
->text
[1])
1104 return tokval
->t_type
= TOKEN_HERE
;
1105 if (tline
->text
[0] == '$' && tline
->text
[1] == '$' && !tline
->text
[2])
1106 return tokval
->t_type
= TOKEN_BASE
;
1108 if (tline
->type
== TOK_ID
)
1110 tokval
->t_charptr
= tline
->text
;
1111 if (tline
->text
[0] == '$')
1113 tokval
->t_charptr
++;
1114 return tokval
->t_type
= TOKEN_ID
;
1118 * This is the only special case we actually need to worry
1119 * about in this restricted context.
1121 if (!nasm_stricmp(tline
->text
, "seg"))
1122 return tokval
->t_type
= TOKEN_SEG
;
1124 return tokval
->t_type
= TOKEN_ID
;
1127 if (tline
->type
== TOK_NUMBER
)
1131 tokval
->t_integer
= readnum(tline
->text
, &rn_error
);
1133 return tokval
->t_type
= TOKEN_ERRNUM
;
1134 tokval
->t_charptr
= NULL
;
1135 return tokval
->t_type
= TOKEN_NUM
;
1138 if (tline
->type
== TOK_STRING
)
1148 if (l
== 0 || r
[l
- 1] != q
)
1149 return tokval
->t_type
= TOKEN_ERRNUM
;
1150 tokval
->t_integer
= readstrnum(r
, l
- 1, &rn_warn
);
1152 error(ERR_WARNING
| ERR_PASS1
, "character constant too long");
1153 tokval
->t_charptr
= NULL
;
1154 return tokval
->t_type
= TOKEN_NUM
;
1157 if (tline
->type
== TOK_OTHER
)
1159 if (!strcmp(tline
->text
, "<<"))
1160 return tokval
->t_type
= TOKEN_SHL
;
1161 if (!strcmp(tline
->text
, ">>"))
1162 return tokval
->t_type
= TOKEN_SHR
;
1163 if (!strcmp(tline
->text
, "//"))
1164 return tokval
->t_type
= TOKEN_SDIV
;
1165 if (!strcmp(tline
->text
, "%%"))
1166 return tokval
->t_type
= TOKEN_SMOD
;
1167 if (!strcmp(tline
->text
, "=="))
1168 return tokval
->t_type
= TOKEN_EQ
;
1169 if (!strcmp(tline
->text
, "<>"))
1170 return tokval
->t_type
= TOKEN_NE
;
1171 if (!strcmp(tline
->text
, "!="))
1172 return tokval
->t_type
= TOKEN_NE
;
1173 if (!strcmp(tline
->text
, "<="))
1174 return tokval
->t_type
= TOKEN_LE
;
1175 if (!strcmp(tline
->text
, ">="))
1176 return tokval
->t_type
= TOKEN_GE
;
1177 if (!strcmp(tline
->text
, "&&"))
1178 return tokval
->t_type
= TOKEN_DBL_AND
;
1179 if (!strcmp(tline
->text
, "^^"))
1180 return tokval
->t_type
= TOKEN_DBL_XOR
;
1181 if (!strcmp(tline
->text
, "||"))
1182 return tokval
->t_type
= TOKEN_DBL_OR
;
1186 * We have no other options: just return the first character of
1189 return tokval
->t_type
= tline
->text
[0];
1193 * Compare a string to the name of an existing macro; this is a
1194 * simple wrapper which calls either strcmp or nasm_stricmp
1195 * depending on the value of the `casesense' parameter.
1198 mstrcmp(char *p
, char *q
, int casesense
)
1200 return casesense
? strcmp(p
, q
) : nasm_stricmp(p
, q
);
1204 * Return the Context structure associated with a %$ token. Return
1205 * NULL, having _already_ reported an error condition, if the
1206 * context stack isn't deep enough for the supplied number of $
1208 * If all_contexts == TRUE, contexts that enclose current are
1209 * also scanned for such smacro, until it is found; if not -
1210 * only the context that directly results from the number of $'s
1211 * in variable's name.
1214 get_ctx(char *name
, int all_contexts
)
1220 if (!name
|| name
[0] != '%' || name
[1] != '$')
1225 error(ERR_NONFATAL
, "`%s': context stack is empty", name
);
1229 for (i
= strspn(name
+ 2, "$"), ctx
= cstk
; (i
> 0) && ctx
; i
--)
1232 /* i--; Lino - 02/25/02 */
1236 error(ERR_NONFATAL
, "`%s': context stack is only"
1237 " %d level%s deep", name
, i
- 1, (i
== 2 ? "" : "s"));
1245 /* Search for this smacro in found context */
1249 if (!mstrcmp(m
->name
, name
, m
->casesense
))
1259 /* Add a slash to the end of a path if it is missing. We use the
1260 * forward slash to make it compatible with Unix systems.
1265 int pos
= strlen(s
);
1266 if (s
[pos
- 1] != '\\' && s
[pos
- 1] != '/')
1274 * Open an include file. This routine must always return a valid
1275 * file pointer if it returns - it's responsible for throwing an
1276 * ERR_FATAL and bombing out completely if not. It should also try
1277 * the include path one by one until it finds the file or reaches
1278 * the end of the path.
1281 inc_fopen(char *file
)
1284 char *prefix
= "", *combine
;
1285 IncPath
*ip
= ipath
;
1286 static int namelen
= 0;
1287 int len
= strlen(file
);
1291 combine
= nasm_malloc(strlen(prefix
) + 1 + len
+ 1);
1292 strcpy(combine
, prefix
);
1295 strcat(combine
, file
);
1296 fp
= fopen(combine
, "r");
1297 if (pass
== 0 && fp
)
1299 namelen
+= strlen(combine
) + 1;
1305 printf(" %s", combine
);
1316 error(ERR_FATAL
, "unable to open include file `%s'", file
);
1317 return NULL
; /* never reached - placate compilers */
1321 * Determine if we should warn on defining a single-line macro of
1322 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1323 * return TRUE if _any_ single-line macro of that name is defined.
1324 * Otherwise, will return TRUE if a single-line macro with either
1325 * `nparam' or no parameters is defined.
1327 * If a macro with precisely the right number of parameters is
1328 * defined, or nparam is -1, the address of the definition structure
1329 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1330 * is NULL, no action will be taken regarding its contents, and no
1333 * Note that this is also called with nparam zero to resolve
1336 * If you already know which context macro belongs to, you can pass
1337 * the context pointer as first parameter; if you won't but name begins
1338 * with %$ the context will be automatically computed. If all_contexts
1339 * is true, macro will be searched in outer contexts as well.
1342 smacro_defined(Context
* ctx
, char *name
, int nparam
, SMacro
** defn
,
1349 else if (name
[0] == '%' && name
[1] == '$')
1352 ctx
= get_ctx(name
, FALSE
);
1354 return FALSE
; /* got to return _something_ */
1358 m
= smacros
[hash(name
)];
1362 if (!mstrcmp(m
->name
, name
, m
->casesense
&& nocase
) &&
1363 (nparam
<= 0 || m
->nparam
== 0 || nparam
== m
->nparam
))
1367 if (nparam
== m
->nparam
|| nparam
== -1)
1381 * Count and mark off the parameters in a multi-line macro call.
1382 * This is called both from within the multi-line macro expansion
1383 * code, and also to mark off the default parameters when provided
1384 * in a %macro definition line.
1387 count_mmac_params(Token
* t
, int *nparam
, Token
*** params
)
1389 int paramsize
, brace
;
1391 *nparam
= paramsize
= 0;
1395 if (*nparam
>= paramsize
)
1397 paramsize
+= PARAM_DELTA
;
1398 *params
= nasm_realloc(*params
, sizeof(**params
) * paramsize
);
1402 if (tok_is_(t
, "{"))
1404 (*params
)[(*nparam
)++] = t
;
1405 while (tok_isnt_(t
, brace
? "}" : ","))
1408 { /* got a comma/brace */
1413 * Now we've found the closing brace, look further
1417 if (tok_isnt_(t
, ","))
1420 "braces do not enclose all of macro parameter");
1421 while (tok_isnt_(t
, ","))
1425 t
= t
->next
; /* eat the comma */
1432 * Determine whether one of the various `if' conditions is true or
1435 * We must free the tline we get passed.
1438 if_condition(Token
* tline
, int i
)
1441 Token
*t
, *tt
, **tptr
, *origline
;
1442 struct tokenval tokval
;
1453 j
= FALSE
; /* have we matched yet? */
1454 while (cstk
&& tline
)
1457 if (!tline
|| tline
->type
!= TOK_ID
)
1460 "`%s' expects context identifiers",
1462 free_tlist(origline
);
1465 if (!nasm_stricmp(tline
->text
, cstk
->name
))
1467 tline
= tline
->next
;
1469 if (i
== PP_IFNCTX
|| i
== PP_ELIFNCTX
)
1471 free_tlist(origline
);
1478 j
= FALSE
; /* have we matched yet? */
1482 if (!tline
|| (tline
->type
!= TOK_ID
&&
1483 (tline
->type
!= TOK_PREPROC_ID
||
1484 tline
->text
[1] != '$')))
1487 "`%s' expects macro identifiers",
1489 free_tlist(origline
);
1492 if (smacro_defined(NULL
, tline
->text
, 0, NULL
, 1))
1494 tline
= tline
->next
;
1496 if (i
== PP_IFNDEF
|| i
== PP_ELIFNDEF
)
1498 free_tlist(origline
);
1509 tline
= expand_smacro(tline
);
1511 while (tok_isnt_(tt
, ","))
1516 "`%s' expects two comma-separated arguments",
1522 casesense
= (i
== PP_IFIDN
|| i
== PP_ELIFIDN
||
1523 i
== PP_IFNIDN
|| i
== PP_ELIFNIDN
);
1524 j
= TRUE
; /* assume equality unless proved not */
1525 while ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) && tt
)
1527 if (tt
->type
== TOK_OTHER
&& !strcmp(tt
->text
, ","))
1529 error(ERR_NONFATAL
, "`%s': more than one comma on line",
1534 if (t
->type
== TOK_WHITESPACE
)
1539 else if (tt
->type
== TOK_WHITESPACE
)
1544 else if (tt
->type
!= t
->type
||
1545 mstrcmp(tt
->text
, t
->text
, casesense
))
1547 j
= FALSE
; /* found mismatching tokens */
1557 if ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) || tt
)
1558 j
= FALSE
; /* trailing gunk on one end or other */
1559 if (i
== PP_IFNIDN
|| i
== PP_ELIFNIDN
||
1560 i
== PP_IFNIDNI
|| i
== PP_ELIFNIDNI
)
1571 MMacro searching
, *mmac
;
1573 tline
= tline
->next
;
1575 tline
= expand_id(tline
);
1576 if (!tok_type_(tline
, TOK_ID
))
1579 "`%s' expects a macro name",
1583 searching
.name
= nasm_strdup(tline
->text
);
1584 searching
.casesense
= (i
== PP_MACRO
);
1585 searching
.plus
= FALSE
;
1586 searching
.nolist
= FALSE
;
1587 searching
.in_progress
= FALSE
;
1588 searching
.rep_nest
= NULL
;
1589 searching
.nparam_min
= 0;
1590 searching
.nparam_max
= INT_MAX
;
1591 tline
= expand_smacro(tline
->next
);
1595 } else if (!tok_type_(tline
, TOK_NUMBER
))
1598 "`%s' expects a parameter count or nothing",
1603 searching
.nparam_min
= searching
.nparam_max
=
1604 readnum(tline
->text
, &j
);
1607 "unable to parse parameter count `%s'",
1610 if (tline
&& tok_is_(tline
->next
, "-"))
1612 tline
= tline
->next
->next
;
1613 if (tok_is_(tline
, "*"))
1614 searching
.nparam_max
= INT_MAX
;
1615 else if (!tok_type_(tline
, TOK_NUMBER
))
1617 "`%s' expects a parameter count after `-'",
1621 searching
.nparam_max
= readnum(tline
->text
, &j
);
1624 "unable to parse parameter count `%s'",
1626 if (searching
.nparam_min
> searching
.nparam_max
)
1628 "minimum parameter count exceeds maximum");
1631 if (tline
&& tok_is_(tline
->next
, "+"))
1633 tline
= tline
->next
;
1634 searching
.plus
= TRUE
;
1636 mmac
= mmacros
[hash(searching
.name
)];
1639 if (!strcmp(mmac
->name
, searching
.name
) &&
1640 (mmac
->nparam_min
<= searching
.nparam_max
1642 && (searching
.nparam_min
<= mmac
->nparam_max
1650 nasm_free(searching
.name
);
1651 free_tlist(origline
);
1652 if (i
== PP_IFNMACRO
|| i
== PP_ELIFNMACRO
)
1669 tline
= expand_smacro(tline
);
1671 while (tok_type_(t
, TOK_WHITESPACE
))
1673 j
= FALSE
; /* placate optimiser */
1681 j
= (t
->type
== TOK_ID
);
1687 j
= (t
->type
== TOK_NUMBER
);
1693 j
= (t
->type
== TOK_STRING
);
1696 if (i
== PP_IFNID
|| i
== PP_ELIFNID
||
1697 i
== PP_IFNNUM
|| i
== PP_ELIFNNUM
||
1698 i
== PP_IFNSTR
|| i
== PP_ELIFNSTR
)
1705 t
= tline
= expand_smacro(tline
);
1707 tokval
.t_type
= TOKEN_INVALID
;
1708 evalresult
= evaluate(ppscan
, tptr
, &tokval
,
1709 NULL
, pass
| CRITICAL
, error
, NULL
);
1715 "trailing garbage after expression ignored");
1716 if (!is_simple(evalresult
))
1719 "non-constant value given to `%s'", directives
[i
]);
1722 return reloc_value(evalresult
) != 0;
1726 "preprocessor directive `%s' not yet implemented",
1728 free_tlist(origline
);
1729 return -1; /* yeah, right */
1734 * Expand macros in a string. Used in %error and %include directives.
1735 * First tokenise the string, apply "expand_smacro" and then de-tokenise back.
1736 * The returned variable should ALWAYS be freed after usage.
1739 expand_macros_in_string(char **p
)
1741 Token
*line
= tokenise(*p
);
1742 line
= expand_smacro(line
);
1743 *p
= detoken(line
, FALSE
);
1747 * find and process preprocessor directive in passed line
1748 * Find out if a line contains a preprocessor directive, and deal
1751 * If a directive _is_ found, it is the responsibility of this routine
1752 * (and not the caller) to free_tlist() the line.
1754 * @param tline a pointer to the current tokeninzed line linked list
1755 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
1759 do_directive(Token
* tline
)
1761 int i
, j
, k
, m
, nparam
, nolist
;
1767 SMacro
*smac
, **smhead
;
1769 Token
*t
, *tt
, *param_start
, *macro_start
, *last
, **tptr
, *origline
;
1771 struct tokenval tokval
;
1773 MMacro
*tmp_defining
; /* Used when manipulating rep_nest */
1778 if (!tok_type_(tline
, TOK_PREPROC_ID
) ||
1779 (tline
->text
[1] == '%' || tline
->text
[1] == '$'
1780 || tline
->text
[1] == '!'))
1781 return NO_DIRECTIVE_FOUND
;
1784 j
= elements(directives
);
1788 m
= nasm_stricmp(tline
->text
, directives
[k
]);
1790 if (tasm_compatible_mode
) {
1793 } else if (k
!= PP_ARG
&& k
!= PP_LOCAL
&& k
!= PP_STACKSIZE
) {
1807 * If we're in a non-emitting branch of a condition construct,
1808 * or walking to the end of an already terminated %rep block,
1809 * we should ignore all directives except for condition
1812 if (((istk
->conds
&& !emitting(istk
->conds
->state
)) ||
1813 (istk
->mstk
&& !istk
->mstk
->in_progress
)) &&
1816 return NO_DIRECTIVE_FOUND
;
1820 * If we're defining a macro or reading a %rep block, we should
1821 * ignore all directives except for %macro/%imacro (which
1822 * generate an error), %endm/%endmacro, and (only if we're in a
1823 * %rep block) %endrep. If we're in a %rep block, another %rep
1824 * causes an error, so should be let through.
1826 if (defining
&& i
!= PP_MACRO
&& i
!= PP_IMACRO
&&
1827 i
!= PP_ENDMACRO
&& i
!= PP_ENDM
&&
1828 (defining
->name
|| (i
!= PP_ENDREP
&& i
!= PP_REP
)))
1830 return NO_DIRECTIVE_FOUND
;
1835 error(ERR_NONFATAL
, "unknown preprocessor directive `%s'",
1837 return NO_DIRECTIVE_FOUND
; /* didn't get it */
1843 /* Directive to tell NASM what the default stack size is. The
1844 * default is for a 16-bit stack, and this can be overriden with
1846 * the following form:
1848 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1850 tline
= tline
->next
;
1851 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1852 tline
= tline
->next
;
1853 if (!tline
|| tline
->type
!= TOK_ID
)
1855 error(ERR_NONFATAL
, "`%%stacksize' missing size parameter");
1856 free_tlist(origline
);
1857 return DIRECTIVE_FOUND
;
1859 if (nasm_stricmp(tline
->text
, "flat") == 0)
1861 /* All subsequent ARG directives are for a 32-bit stack */
1863 StackPointer
= "ebp";
1867 else if (nasm_stricmp(tline
->text
, "large") == 0)
1869 /* All subsequent ARG directives are for a 16-bit stack,
1870 * far function call.
1873 StackPointer
= "bp";
1877 else if (nasm_stricmp(tline
->text
, "small") == 0)
1879 /* All subsequent ARG directives are for a 16-bit stack,
1880 * far function call. We don't support near functions.
1883 StackPointer
= "bp";
1889 error(ERR_NONFATAL
, "`%%stacksize' invalid size type");
1890 free_tlist(origline
);
1891 return DIRECTIVE_FOUND
;
1893 free_tlist(origline
);
1894 return DIRECTIVE_FOUND
;
1897 /* TASM like ARG directive to define arguments to functions, in
1898 * the following form:
1900 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1905 char *arg
, directive
[256];
1906 int size
= StackSize
;
1908 /* Find the argument name */
1909 tline
= tline
->next
;
1910 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1911 tline
= tline
->next
;
1912 if (!tline
|| tline
->type
!= TOK_ID
)
1914 error(ERR_NONFATAL
, "`%%arg' missing argument parameter");
1915 free_tlist(origline
);
1916 return DIRECTIVE_FOUND
;
1920 /* Find the argument size type */
1921 tline
= tline
->next
;
1922 if (!tline
|| tline
->type
!= TOK_OTHER
1923 || tline
->text
[0] != ':')
1926 "Syntax error processing `%%arg' directive");
1927 free_tlist(origline
);
1928 return DIRECTIVE_FOUND
;
1930 tline
= tline
->next
;
1931 if (!tline
|| tline
->type
!= TOK_ID
)
1934 "`%%arg' missing size type parameter");
1935 free_tlist(origline
);
1936 return DIRECTIVE_FOUND
;
1939 /* Allow macro expansion of type parameter */
1940 tt
= tokenise(tline
->text
);
1941 tt
= expand_smacro(tt
);
1942 if (nasm_stricmp(tt
->text
, "byte") == 0)
1944 size
= MAX(StackSize
, 1);
1946 else if (nasm_stricmp(tt
->text
, "word") == 0)
1948 size
= MAX(StackSize
, 2);
1950 else if (nasm_stricmp(tt
->text
, "dword") == 0)
1952 size
= MAX(StackSize
, 4);
1954 else if (nasm_stricmp(tt
->text
, "qword") == 0)
1956 size
= MAX(StackSize
, 8);
1958 else if (nasm_stricmp(tt
->text
, "tword") == 0)
1960 size
= MAX(StackSize
, 10);
1965 "Invalid size type for `%%arg' missing directive");
1967 free_tlist(origline
);
1968 return DIRECTIVE_FOUND
;
1972 /* Now define the macro for the argument */
1973 sprintf(directive
, "%%define %s (%s+%d)", arg
, StackPointer
,
1975 do_directive(tokenise(directive
));
1978 /* Move to the next argument in the list */
1979 tline
= tline
->next
;
1980 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1981 tline
= tline
->next
;
1983 while (tline
&& tline
->type
== TOK_OTHER
1984 && tline
->text
[0] == ',');
1985 free_tlist(origline
);
1986 return DIRECTIVE_FOUND
;
1989 /* TASM like LOCAL directive to define local variables for a
1990 * function, in the following form:
1992 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
1994 * The '= LocalSize' at the end is ignored by NASM, but is
1995 * required by TASM to define the local parameter size (and used
1996 * by the TASM macro package).
1998 offset
= LocalOffset
;
2001 char *local
, directive
[256];
2002 int size
= StackSize
;
2004 /* Find the argument name */
2005 tline
= tline
->next
;
2006 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2007 tline
= tline
->next
;
2008 if (!tline
|| tline
->type
!= TOK_ID
)
2011 "`%%local' missing argument parameter");
2012 free_tlist(origline
);
2013 return DIRECTIVE_FOUND
;
2015 local
= tline
->text
;
2017 /* Find the argument size type */
2018 tline
= tline
->next
;
2019 if (!tline
|| tline
->type
!= TOK_OTHER
2020 || tline
->text
[0] != ':')
2023 "Syntax error processing `%%local' directive");
2024 free_tlist(origline
);
2025 return DIRECTIVE_FOUND
;
2027 tline
= tline
->next
;
2028 if (!tline
|| tline
->type
!= TOK_ID
)
2031 "`%%local' missing size type parameter");
2032 free_tlist(origline
);
2033 return DIRECTIVE_FOUND
;
2036 /* Allow macro expansion of type parameter */
2037 tt
= tokenise(tline
->text
);
2038 tt
= expand_smacro(tt
);
2039 if (nasm_stricmp(tt
->text
, "byte") == 0)
2041 size
= MAX(StackSize
, 1);
2043 else if (nasm_stricmp(tt
->text
, "word") == 0)
2045 size
= MAX(StackSize
, 2);
2047 else if (nasm_stricmp(tt
->text
, "dword") == 0)
2049 size
= MAX(StackSize
, 4);
2051 else if (nasm_stricmp(tt
->text
, "qword") == 0)
2053 size
= MAX(StackSize
, 8);
2055 else if (nasm_stricmp(tt
->text
, "tword") == 0)
2057 size
= MAX(StackSize
, 10);
2062 "Invalid size type for `%%local' missing directive");
2064 free_tlist(origline
);
2065 return DIRECTIVE_FOUND
;
2069 /* Now define the macro for the argument */
2070 sprintf(directive
, "%%define %s (%s-%d)", local
, StackPointer
,
2072 do_directive(tokenise(directive
));
2075 /* Now define the assign to setup the enter_c macro correctly */
2076 sprintf(directive
, "%%assign %%$localsize %%$localsize+%d",
2078 do_directive(tokenise(directive
));
2080 /* Move to the next argument in the list */
2081 tline
= tline
->next
;
2082 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2083 tline
= tline
->next
;
2085 while (tline
&& tline
->type
== TOK_OTHER
2086 && tline
->text
[0] == ',');
2087 free_tlist(origline
);
2088 return DIRECTIVE_FOUND
;
2093 "trailing garbage after `%%clear' ignored");
2094 for (j
= 0; j
< NHASH
; j
++)
2098 MMacro
*m
= mmacros
[j
];
2099 mmacros
[j
] = m
->next
;
2104 SMacro
*s
= smacros
[j
];
2105 smacros
[j
] = smacros
[j
]->next
;
2107 free_tlist(s
->expansion
);
2111 free_tlist(origline
);
2112 return DIRECTIVE_FOUND
;
2115 tline
= tline
->next
;
2117 if (!tline
|| (tline
->type
!= TOK_STRING
&&
2118 tline
->type
!= TOK_INTERNAL_STRING
))
2120 error(ERR_NONFATAL
, "`%%include' expects a file name");
2121 free_tlist(origline
);
2122 return DIRECTIVE_FOUND
; /* but we did _something_ */
2126 "trailing garbage after `%%include' ignored");
2127 if (tline
->type
!= TOK_INTERNAL_STRING
)
2129 p
= tline
->text
+ 1; /* point past the quote to the name */
2130 p
[strlen(p
) - 1] = '\0'; /* remove the trailing quote */
2133 p
= tline
->text
; /* internal_string is easier */
2134 expand_macros_in_string(&p
);
2135 inc
= nasm_malloc(sizeof(Include
));
2138 inc
->fp
= inc_fopen(p
);
2139 inc
->fname
= src_set_fname(p
);
2140 inc
->lineno
= src_set_linnum(0);
2142 inc
->expansion
= NULL
;
2145 list
->uplevel(LIST_INCLUDE
);
2146 free_tlist(origline
);
2147 return DIRECTIVE_FOUND
;
2150 tline
= tline
->next
;
2152 tline
= expand_id(tline
);
2153 if (!tok_type_(tline
, TOK_ID
))
2155 error(ERR_NONFATAL
, "`%%push' expects a context identifier");
2156 free_tlist(origline
);
2157 return DIRECTIVE_FOUND
; /* but we did _something_ */
2160 error(ERR_WARNING
, "trailing garbage after `%%push' ignored");
2161 ctx
= nasm_malloc(sizeof(Context
));
2163 ctx
->localmac
= NULL
;
2164 ctx
->name
= nasm_strdup(tline
->text
);
2165 ctx
->number
= unique
++;
2167 free_tlist(origline
);
2171 tline
= tline
->next
;
2173 tline
= expand_id(tline
);
2174 if (!tok_type_(tline
, TOK_ID
))
2176 error(ERR_NONFATAL
, "`%%repl' expects a context identifier");
2177 free_tlist(origline
);
2178 return DIRECTIVE_FOUND
; /* but we did _something_ */
2181 error(ERR_WARNING
, "trailing garbage after `%%repl' ignored");
2183 error(ERR_NONFATAL
, "`%%repl': context stack is empty");
2186 nasm_free(cstk
->name
);
2187 cstk
->name
= nasm_strdup(tline
->text
);
2189 free_tlist(origline
);
2194 error(ERR_WARNING
, "trailing garbage after `%%pop' ignored");
2197 "`%%pop': context stack is already empty");
2200 free_tlist(origline
);
2204 tline
->next
= expand_smacro(tline
->next
);
2205 tline
= tline
->next
;
2207 if (tok_type_(tline
, TOK_STRING
))
2209 p
= tline
->text
+ 1; /* point past the quote to the name */
2210 p
[strlen(p
) - 1] = '\0'; /* remove the trailing quote */
2211 expand_macros_in_string(&p
);
2212 error(ERR_NONFATAL
, "%s", p
);
2217 p
= detoken(tline
, FALSE
);
2218 error(ERR_WARNING
, "%s", p
);
2221 free_tlist(origline
);
2241 if (istk
->conds
&& !emitting(istk
->conds
->state
))
2245 j
= if_condition(tline
->next
, i
);
2246 tline
->next
= NULL
; /* it got freed */
2247 free_tlist(origline
);
2248 j
= j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2250 cond
= nasm_malloc(sizeof(Cond
));
2251 cond
->next
= istk
->conds
;
2254 return DIRECTIVE_FOUND
;
2274 error(ERR_FATAL
, "`%s': no matching `%%if'", directives
[i
]);
2275 if (emitting(istk
->conds
->state
)
2276 || istk
->conds
->state
== COND_NEVER
)
2277 istk
->conds
->state
= COND_NEVER
;
2281 * IMPORTANT: In the case of %if, we will already have
2282 * called expand_mmac_params(); however, if we're
2283 * processing an %elif we must have been in a
2284 * non-emitting mode, which would have inhibited
2285 * the normal invocation of expand_mmac_params(). Therefore,
2286 * we have to do it explicitly here.
2288 j
= if_condition(expand_mmac_params(tline
->next
), i
);
2289 tline
->next
= NULL
; /* it got freed */
2290 free_tlist(origline
);
2291 istk
->conds
->state
=
2292 j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2294 return DIRECTIVE_FOUND
;
2298 error(ERR_WARNING
, "trailing garbage after `%%else' ignored");
2300 error(ERR_FATAL
, "`%%else': no matching `%%if'");
2301 if (emitting(istk
->conds
->state
)
2302 || istk
->conds
->state
== COND_NEVER
)
2303 istk
->conds
->state
= COND_ELSE_FALSE
;
2305 istk
->conds
->state
= COND_ELSE_TRUE
;
2306 free_tlist(origline
);
2307 return DIRECTIVE_FOUND
;
2312 "trailing garbage after `%%endif' ignored");
2314 error(ERR_FATAL
, "`%%endif': no matching `%%if'");
2316 istk
->conds
= cond
->next
;
2318 free_tlist(origline
);
2319 return DIRECTIVE_FOUND
;
2325 "`%%%smacro': already defining a macro",
2326 (i
== PP_IMACRO
? "i" : ""));
2327 tline
= tline
->next
;
2329 tline
= expand_id(tline
);
2330 if (!tok_type_(tline
, TOK_ID
))
2333 "`%%%smacro' expects a macro name",
2334 (i
== PP_IMACRO
? "i" : ""));
2335 return DIRECTIVE_FOUND
;
2337 defining
= nasm_malloc(sizeof(MMacro
));
2338 defining
->name
= nasm_strdup(tline
->text
);
2339 defining
->casesense
= (i
== PP_MACRO
);
2340 defining
->plus
= FALSE
;
2341 defining
->nolist
= FALSE
;
2342 defining
->in_progress
= FALSE
;
2343 defining
->rep_nest
= NULL
;
2344 tline
= expand_smacro(tline
->next
);
2346 if (!tok_type_(tline
, TOK_NUMBER
))
2349 "`%%%smacro' expects a parameter count",
2350 (i
== PP_IMACRO
? "i" : ""));
2351 defining
->nparam_min
= defining
->nparam_max
= 0;
2355 defining
->nparam_min
= defining
->nparam_max
=
2356 readnum(tline
->text
, &j
);
2359 "unable to parse parameter count `%s'",
2362 if (tline
&& tok_is_(tline
->next
, "-"))
2364 tline
= tline
->next
->next
;
2365 if (tok_is_(tline
, "*"))
2366 defining
->nparam_max
= INT_MAX
;
2367 else if (!tok_type_(tline
, TOK_NUMBER
))
2369 "`%%%smacro' expects a parameter count after `-'",
2370 (i
== PP_IMACRO
? "i" : ""));
2373 defining
->nparam_max
= readnum(tline
->text
, &j
);
2376 "unable to parse parameter count `%s'",
2378 if (defining
->nparam_min
> defining
->nparam_max
)
2380 "minimum parameter count exceeds maximum");
2383 if (tline
&& tok_is_(tline
->next
, "+"))
2385 tline
= tline
->next
;
2386 defining
->plus
= TRUE
;
2388 if (tline
&& tok_type_(tline
->next
, TOK_ID
) &&
2389 !nasm_stricmp(tline
->next
->text
, ".nolist"))
2391 tline
= tline
->next
;
2392 defining
->nolist
= TRUE
;
2394 mmac
= mmacros
[hash(defining
->name
)];
2397 if (!strcmp(mmac
->name
, defining
->name
) &&
2398 (mmac
->nparam_min
<= defining
->nparam_max
2400 && (defining
->nparam_min
<= mmac
->nparam_max
2404 "redefining multi-line macro `%s'",
2411 * Handle default parameters.
2413 if (tline
&& tline
->next
)
2415 defining
->dlist
= tline
->next
;
2417 count_mmac_params(defining
->dlist
, &defining
->ndefs
,
2418 &defining
->defaults
);
2422 defining
->dlist
= NULL
;
2423 defining
->defaults
= NULL
;
2425 defining
->expansion
= NULL
;
2426 free_tlist(origline
);
2427 return DIRECTIVE_FOUND
;
2433 error(ERR_NONFATAL
, "`%s': not defining a macro",
2435 return DIRECTIVE_FOUND
;
2437 k
= hash(defining
->name
);
2438 defining
->next
= mmacros
[k
];
2439 mmacros
[k
] = defining
;
2441 free_tlist(origline
);
2442 return DIRECTIVE_FOUND
;
2445 if (tline
->next
&& tline
->next
->type
== TOK_WHITESPACE
)
2446 tline
= tline
->next
;
2447 if (tline
->next
== NULL
)
2449 free_tlist(origline
);
2450 error(ERR_NONFATAL
, "`%%rotate' missing rotate count");
2451 return DIRECTIVE_FOUND
;
2453 t
= expand_smacro(tline
->next
);
2455 free_tlist(origline
);
2458 tokval
.t_type
= TOKEN_INVALID
;
2460 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2463 return DIRECTIVE_FOUND
;
2466 "trailing garbage after expression ignored");
2467 if (!is_simple(evalresult
))
2469 error(ERR_NONFATAL
, "non-constant value given to `%%rotate'");
2470 return DIRECTIVE_FOUND
;
2473 while (mmac
&& !mmac
->name
) /* avoid mistaking %reps for macros */
2474 mmac
= mmac
->next_active
;
2478 "`%%rotate' invoked outside a macro call");
2480 else if (mmac
->nparam
== 0)
2483 "`%%rotate' invoked within macro without parameters");
2487 mmac
->rotate
= mmac
->rotate
+ reloc_value(evalresult
);
2489 if (mmac
->rotate
< 0)
2491 mmac
->nparam
- (-mmac
->rotate
) % mmac
->nparam
;
2492 mmac
->rotate
%= mmac
->nparam
;
2494 return DIRECTIVE_FOUND
;
2498 tline
= tline
->next
;
2499 if (tline
->next
&& tline
->next
->type
== TOK_WHITESPACE
)
2500 tline
= tline
->next
;
2501 if (tline
->next
&& tline
->next
->type
== TOK_ID
&&
2502 !nasm_stricmp(tline
->next
->text
, ".nolist"))
2504 tline
= tline
->next
;
2507 t
= expand_smacro(tline
->next
);
2509 free_tlist(origline
);
2512 tokval
.t_type
= TOKEN_INVALID
;
2514 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2517 return DIRECTIVE_FOUND
;
2520 "trailing garbage after expression ignored");
2521 if (!is_simple(evalresult
))
2523 error(ERR_NONFATAL
, "non-constant value given to `%%rep'");
2524 return DIRECTIVE_FOUND
;
2526 tmp_defining
= defining
;
2527 defining
= nasm_malloc(sizeof(MMacro
));
2528 defining
->name
= NULL
; /* flags this macro as a %rep block */
2529 defining
->casesense
= 0;
2530 defining
->plus
= FALSE
;
2531 defining
->nolist
= nolist
;
2532 defining
->in_progress
= reloc_value(evalresult
) + 1;
2533 defining
->nparam_min
= defining
->nparam_max
= 0;
2534 defining
->defaults
= NULL
;
2535 defining
->dlist
= NULL
;
2536 defining
->expansion
= NULL
;
2537 defining
->next_active
= istk
->mstk
;
2538 defining
->rep_nest
= tmp_defining
;
2539 return DIRECTIVE_FOUND
;
2542 if (!defining
|| defining
->name
)
2544 error(ERR_NONFATAL
, "`%%endrep': no matching `%%rep'");
2545 return DIRECTIVE_FOUND
;
2549 * Now we have a "macro" defined - although it has no name
2550 * and we won't be entering it in the hash tables - we must
2551 * push a macro-end marker for it on to istk->expansion.
2552 * After that, it will take care of propagating itself (a
2553 * macro-end marker line for a macro which is really a %rep
2554 * block will cause the macro to be re-expanded, complete
2555 * with another macro-end marker to ensure the process
2556 * continues) until the whole expansion is forcibly removed
2557 * from istk->expansion by a %exitrep.
2559 l
= nasm_malloc(sizeof(Line
));
2560 l
->next
= istk
->expansion
;
2561 l
->finishes
= defining
;
2563 istk
->expansion
= l
;
2565 istk
->mstk
= defining
;
2567 list
->uplevel(defining
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
2568 tmp_defining
= defining
;
2569 defining
= defining
->rep_nest
;
2570 free_tlist(origline
);
2571 return DIRECTIVE_FOUND
;
2575 * We must search along istk->expansion until we hit a
2576 * macro-end marker for a macro with no name. Then we set
2577 * its `in_progress' flag to 0.
2579 for (l
= istk
->expansion
; l
; l
= l
->next
)
2580 if (l
->finishes
&& !l
->finishes
->name
)
2584 l
->finishes
->in_progress
= 0;
2586 error(ERR_NONFATAL
, "`%%exitrep' not within `%%rep' block");
2587 free_tlist(origline
);
2588 return DIRECTIVE_FOUND
;
2594 tline
= tline
->next
;
2596 tline
= expand_id(tline
);
2597 if (!tline
|| (tline
->type
!= TOK_ID
&&
2598 (tline
->type
!= TOK_PREPROC_ID
||
2599 tline
->text
[1] != '$')))
2602 "`%%%s%sdefine' expects a macro identifier",
2603 ((i
== PP_IDEFINE
|| i
== PP_IXDEFINE
) ? "i" : ""),
2604 ((i
== PP_XDEFINE
|| i
== PP_IXDEFINE
) ? "x" : ""));
2605 free_tlist(origline
);
2606 return DIRECTIVE_FOUND
;
2609 ctx
= get_ctx(tline
->text
, FALSE
);
2611 smhead
= &smacros
[hash(tline
->text
)];
2613 smhead
= &ctx
->localmac
;
2614 mname
= tline
->text
;
2616 param_start
= tline
= tline
->next
;
2619 /* Expand the macro definition now for %xdefine and %ixdefine */
2620 if ((i
== PP_XDEFINE
) || (i
== PP_IXDEFINE
))
2621 tline
= expand_smacro(tline
);
2623 if (tok_is_(tline
, "("))
2626 * This macro has parameters.
2629 tline
= tline
->next
;
2635 error(ERR_NONFATAL
, "parameter identifier expected");
2636 free_tlist(origline
);
2637 return DIRECTIVE_FOUND
;
2639 if (tline
->type
!= TOK_ID
)
2642 "`%s': parameter identifier expected",
2644 free_tlist(origline
);
2645 return DIRECTIVE_FOUND
;
2647 tline
->type
= TOK_SMAC_PARAM
+ nparam
++;
2648 tline
= tline
->next
;
2650 if (tok_is_(tline
, ","))
2652 tline
= tline
->next
;
2655 if (!tok_is_(tline
, ")"))
2658 "`)' expected to terminate macro template");
2659 free_tlist(origline
);
2660 return DIRECTIVE_FOUND
;
2665 tline
= tline
->next
;
2667 if (tok_type_(tline
, TOK_WHITESPACE
))
2668 last
= tline
, tline
= tline
->next
;
2674 if (t
->type
== TOK_ID
)
2676 for (tt
= param_start
; tt
; tt
= tt
->next
)
2677 if (tt
->type
>= TOK_SMAC_PARAM
&&
2678 !strcmp(tt
->text
, t
->text
))
2682 t
->next
= macro_start
;
2687 * Good. We now have a macro name, a parameter count, and a
2688 * token list (in reverse order) for an expansion. We ought
2689 * to be OK just to create an SMacro, store it, and let
2690 * free_tlist have the rest of the line (which we have
2691 * carefully re-terminated after chopping off the expansion
2694 if (smacro_defined(ctx
, mname
, nparam
, &smac
, i
== PP_DEFINE
))
2699 "single-line macro `%s' defined both with and"
2700 " without parameters", mname
);
2701 free_tlist(origline
);
2702 free_tlist(macro_start
);
2703 return DIRECTIVE_FOUND
;
2708 * We're redefining, so we have to take over an
2709 * existing SMacro structure. This means freeing
2710 * what was already in it.
2712 nasm_free(smac
->name
);
2713 free_tlist(smac
->expansion
);
2718 smac
= nasm_malloc(sizeof(SMacro
));
2719 smac
->next
= *smhead
;
2722 smac
->name
= nasm_strdup(mname
);
2723 smac
->casesense
= ((i
== PP_DEFINE
) || (i
== PP_XDEFINE
));
2724 smac
->nparam
= nparam
;
2725 smac
->expansion
= macro_start
;
2726 smac
->in_progress
= FALSE
;
2727 free_tlist(origline
);
2728 return DIRECTIVE_FOUND
;
2731 tline
= tline
->next
;
2733 tline
= expand_id(tline
);
2734 if (!tline
|| (tline
->type
!= TOK_ID
&&
2735 (tline
->type
!= TOK_PREPROC_ID
||
2736 tline
->text
[1] != '$')))
2738 error(ERR_NONFATAL
, "`%%undef' expects a macro identifier");
2739 free_tlist(origline
);
2740 return DIRECTIVE_FOUND
;
2745 "trailing garbage after macro name ignored");
2748 /* Find the context that symbol belongs to */
2749 ctx
= get_ctx(tline
->text
, FALSE
);
2751 smhead
= &smacros
[hash(tline
->text
)];
2753 smhead
= &ctx
->localmac
;
2755 mname
= tline
->text
;
2760 * We now have a macro name... go hunt for it.
2762 while (smacro_defined(ctx
, mname
, -1, &smac
, 1))
2764 /* Defined, so we need to find its predecessor and nuke it */
2766 for (s
= smhead
; *s
&& *s
!= smac
; s
= &(*s
)->next
);
2770 nasm_free(smac
->name
);
2771 free_tlist(smac
->expansion
);
2775 free_tlist(origline
);
2776 return DIRECTIVE_FOUND
;
2779 tline
= tline
->next
;
2781 tline
= expand_id(tline
);
2782 if (!tline
|| (tline
->type
!= TOK_ID
&&
2783 (tline
->type
!= TOK_PREPROC_ID
||
2784 tline
->text
[1] != '$')))
2787 "`%%strlen' expects a macro identifier as first parameter");
2788 free_tlist(origline
);
2789 return DIRECTIVE_FOUND
;
2791 ctx
= get_ctx(tline
->text
, FALSE
);
2793 smhead
= &smacros
[hash(tline
->text
)];
2795 smhead
= &ctx
->localmac
;
2796 mname
= tline
->text
;
2798 tline
= expand_smacro(tline
->next
);
2802 while (tok_type_(t
, TOK_WHITESPACE
))
2804 /* t should now point to the string */
2805 if (t
->type
!= TOK_STRING
)
2808 "`%%strlen` requires string as second parameter");
2810 free_tlist(origline
);
2811 return DIRECTIVE_FOUND
;
2814 macro_start
= nasm_malloc(sizeof(*macro_start
));
2815 macro_start
->next
= NULL
;
2816 make_tok_num(macro_start
, strlen(t
->text
) - 2);
2817 macro_start
->mac
= NULL
;
2820 * We now have a macro name, an implicit parameter count of
2821 * zero, and a numeric token to use as an expansion. Create
2822 * and store an SMacro.
2824 if (smacro_defined(ctx
, mname
, 0, &smac
, i
== PP_STRLEN
))
2828 "single-line macro `%s' defined both with and"
2829 " without parameters", mname
);
2833 * We're redefining, so we have to take over an
2834 * existing SMacro structure. This means freeing
2835 * what was already in it.
2837 nasm_free(smac
->name
);
2838 free_tlist(smac
->expansion
);
2843 smac
= nasm_malloc(sizeof(SMacro
));
2844 smac
->next
= *smhead
;
2847 smac
->name
= nasm_strdup(mname
);
2848 smac
->casesense
= (i
== PP_STRLEN
);
2850 smac
->expansion
= macro_start
;
2851 smac
->in_progress
= FALSE
;
2853 free_tlist(origline
);
2854 return DIRECTIVE_FOUND
;
2857 tline
= tline
->next
;
2859 tline
= expand_id(tline
);
2860 if (!tline
|| (tline
->type
!= TOK_ID
&&
2861 (tline
->type
!= TOK_PREPROC_ID
||
2862 tline
->text
[1] != '$')))
2865 "`%%substr' expects a macro identifier as first parameter");
2866 free_tlist(origline
);
2867 return DIRECTIVE_FOUND
;
2869 ctx
= get_ctx(tline
->text
, FALSE
);
2871 smhead
= &smacros
[hash(tline
->text
)];
2873 smhead
= &ctx
->localmac
;
2874 mname
= tline
->text
;
2876 tline
= expand_smacro(tline
->next
);
2880 while (tok_type_(t
, TOK_WHITESPACE
))
2883 /* t should now point to the string */
2884 if (t
->type
!= TOK_STRING
)
2887 "`%%substr` requires string as second parameter");
2889 free_tlist(origline
);
2890 return DIRECTIVE_FOUND
;
2895 tokval
.t_type
= TOKEN_INVALID
;
2897 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2901 free_tlist(origline
);
2902 return DIRECTIVE_FOUND
;
2904 if (!is_simple(evalresult
))
2906 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
2908 free_tlist(origline
);
2909 return DIRECTIVE_FOUND
;
2912 macro_start
= nasm_malloc(sizeof(*macro_start
));
2913 macro_start
->next
= NULL
;
2914 macro_start
->text
= nasm_strdup("'''");
2915 if (evalresult
->value
> 0
2916 && evalresult
->value
< strlen(t
->text
) - 1)
2918 macro_start
->text
[1] = t
->text
[evalresult
->value
];
2922 macro_start
->text
[2] = '\0';
2924 macro_start
->type
= TOK_STRING
;
2925 macro_start
->mac
= NULL
;
2928 * We now have a macro name, an implicit parameter count of
2929 * zero, and a numeric token to use as an expansion. Create
2930 * and store an SMacro.
2932 if (smacro_defined(ctx
, mname
, 0, &smac
, i
== PP_SUBSTR
))
2936 "single-line macro `%s' defined both with and"
2937 " without parameters", mname
);
2941 * We're redefining, so we have to take over an
2942 * existing SMacro structure. This means freeing
2943 * what was already in it.
2945 nasm_free(smac
->name
);
2946 free_tlist(smac
->expansion
);
2951 smac
= nasm_malloc(sizeof(SMacro
));
2952 smac
->next
= *smhead
;
2955 smac
->name
= nasm_strdup(mname
);
2956 smac
->casesense
= (i
== PP_SUBSTR
);
2958 smac
->expansion
= macro_start
;
2959 smac
->in_progress
= FALSE
;
2961 free_tlist(origline
);
2962 return DIRECTIVE_FOUND
;
2967 tline
= tline
->next
;
2969 tline
= expand_id(tline
);
2970 if (!tline
|| (tline
->type
!= TOK_ID
&&
2971 (tline
->type
!= TOK_PREPROC_ID
||
2972 tline
->text
[1] != '$')))
2975 "`%%%sassign' expects a macro identifier",
2976 (i
== PP_IASSIGN
? "i" : ""));
2977 free_tlist(origline
);
2978 return DIRECTIVE_FOUND
;
2980 ctx
= get_ctx(tline
->text
, FALSE
);
2982 smhead
= &smacros
[hash(tline
->text
)];
2984 smhead
= &ctx
->localmac
;
2985 mname
= tline
->text
;
2987 tline
= expand_smacro(tline
->next
);
2992 tokval
.t_type
= TOKEN_INVALID
;
2994 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2998 free_tlist(origline
);
2999 return DIRECTIVE_FOUND
;
3004 "trailing garbage after expression ignored");
3006 if (!is_simple(evalresult
))
3009 "non-constant value given to `%%%sassign'",
3010 (i
== PP_IASSIGN
? "i" : ""));
3011 free_tlist(origline
);
3012 return DIRECTIVE_FOUND
;
3015 macro_start
= nasm_malloc(sizeof(*macro_start
));
3016 macro_start
->next
= NULL
;
3017 make_tok_num(macro_start
, reloc_value(evalresult
));
3018 macro_start
->mac
= NULL
;
3021 * We now have a macro name, an implicit parameter count of
3022 * zero, and a numeric token to use as an expansion. Create
3023 * and store an SMacro.
3025 if (smacro_defined(ctx
, mname
, 0, &smac
, i
== PP_ASSIGN
))
3029 "single-line macro `%s' defined both with and"
3030 " without parameters", mname
);
3034 * We're redefining, so we have to take over an
3035 * existing SMacro structure. This means freeing
3036 * what was already in it.
3038 nasm_free(smac
->name
);
3039 free_tlist(smac
->expansion
);
3044 smac
= nasm_malloc(sizeof(SMacro
));
3045 smac
->next
= *smhead
;
3048 smac
->name
= nasm_strdup(mname
);
3049 smac
->casesense
= (i
== PP_ASSIGN
);
3051 smac
->expansion
= macro_start
;
3052 smac
->in_progress
= FALSE
;
3053 free_tlist(origline
);
3054 return DIRECTIVE_FOUND
;
3058 * Syntax is `%line nnn[+mmm] [filename]'
3060 tline
= tline
->next
;
3062 if (!tok_type_(tline
, TOK_NUMBER
))
3064 error(ERR_NONFATAL
, "`%%line' expects line number");
3065 free_tlist(origline
);
3066 return DIRECTIVE_FOUND
;
3068 k
= readnum(tline
->text
, &j
);
3070 tline
= tline
->next
;
3071 if (tok_is_(tline
, "+"))
3073 tline
= tline
->next
;
3074 if (!tok_type_(tline
, TOK_NUMBER
))
3076 error(ERR_NONFATAL
, "`%%line' expects line increment");
3077 free_tlist(origline
);
3078 return DIRECTIVE_FOUND
;
3080 m
= readnum(tline
->text
, &j
);
3081 tline
= tline
->next
;
3088 nasm_free(src_set_fname(detoken(tline
, FALSE
)));
3090 free_tlist(origline
);
3091 return DIRECTIVE_FOUND
;
3095 "preprocessor directive `%s' not yet implemented",
3099 return DIRECTIVE_FOUND
;
3103 * Ensure that a macro parameter contains a condition code and
3104 * nothing else. Return the condition code index if so, or -1
3114 if (t
->type
!= TOK_ID
)
3118 if (tt
&& (tt
->type
!= TOK_OTHER
|| strcmp(tt
->text
, ",")))
3122 j
= elements(conditions
);
3126 m
= nasm_stricmp(t
->text
, conditions
[k
]);
3146 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3147 * %-n) and MMacro-local identifiers (%%foo).
3150 expand_mmac_params(Token
* tline
)
3152 Token
*t
, *tt
, **tail
, *thead
;
3159 if (tline
->type
== TOK_PREPROC_ID
&&
3160 (((tline
->text
[1] == '+' || tline
->text
[1] == '-')
3161 && tline
->text
[2]) || tline
->text
[1] == '%'
3162 || (tline
->text
[1] >= '0' && tline
->text
[1] <= '9')))
3165 int type
= 0, cc
; /* type = 0 to placate optimisers */
3171 tline
= tline
->next
;
3174 while (mac
&& !mac
->name
) /* avoid mistaking %reps for macros */
3175 mac
= mac
->next_active
;
3177 error(ERR_NONFATAL
, "`%s': not in a macro call", t
->text
);
3182 * We have to make a substitution of one of the
3183 * forms %1, %-1, %+1, %%foo, %0.
3187 sprintf(tmpbuf
, "%d", mac
->nparam
);
3188 text
= nasm_strdup(tmpbuf
);
3192 sprintf(tmpbuf
, "..@%lu.", mac
->unique
);
3193 text
= nasm_strcat(tmpbuf
, t
->text
+ 2);
3196 n
= atoi(t
->text
+ 2) - 1;
3197 if (n
>= mac
->nparam
)
3201 if (mac
->nparam
> 1)
3202 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3203 tt
= mac
->params
[n
];
3209 "macro parameter %d is not a condition code",
3216 if (inverse_ccs
[cc
] == -1)
3219 "condition code `%s' is not invertible",
3225 nasm_strdup(conditions
[inverse_ccs
3230 n
= atoi(t
->text
+ 2) - 1;
3231 if (n
>= mac
->nparam
)
3235 if (mac
->nparam
> 1)
3236 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3237 tt
= mac
->params
[n
];
3243 "macro parameter %d is not a condition code",
3250 text
= nasm_strdup(conditions
[cc
]);
3254 n
= atoi(t
->text
+ 1) - 1;
3255 if (n
>= mac
->nparam
)
3259 if (mac
->nparam
> 1)
3260 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3261 tt
= mac
->params
[n
];
3265 for (i
= 0; i
< mac
->paramlen
[n
]; i
++)
3268 new_Token(NULL
, tt
->type
, tt
->text
,
3270 tail
= &(*tail
)->next
;
3274 text
= NULL
; /* we've done it here */
3295 tline
= tline
->next
;
3302 for (; t
&& (tt
= t
->next
) != NULL
; t
= t
->next
)
3305 case TOK_WHITESPACE
:
3306 if (tt
->type
== TOK_WHITESPACE
)
3308 t
->next
= delete_Token(tt
);
3312 if (tt
->type
== TOK_ID
|| tt
->type
== TOK_NUMBER
)
3314 char *tmp
= nasm_strcat(t
->text
, tt
->text
);
3317 t
->next
= delete_Token(tt
);
3321 if (tt
->type
== TOK_NUMBER
)
3323 char *tmp
= nasm_strcat(t
->text
, tt
->text
);
3326 t
->next
= delete_Token(tt
);
3335 * Expand all single-line macro calls made in the given line.
3336 * Return the expanded version of the line. The original is deemed
3337 * to be destroyed in the process. (In reality we'll just move
3338 * Tokens from input to output a lot of the time, rather than
3339 * actually bothering to destroy and replicate.)
3342 expand_smacro(Token
* tline
)
3344 Token
*t
, *tt
, *mstart
, **tail
, *thead
;
3345 SMacro
*head
= NULL
, *m
;
3348 int nparam
, sparam
, brackets
, rescan
;
3349 Token
*org_tline
= tline
;
3354 * Trick: we should avoid changing the start token pointer since it can
3355 * be contained in "next" field of other token. Because of this
3356 * we allocate a copy of first token and work with it; at the end of
3357 * routine we copy it back
3362 new_Token(org_tline
->next
, org_tline
->type
, org_tline
->text
,
3364 tline
->mac
= org_tline
->mac
;
3365 nasm_free(org_tline
->text
);
3366 org_tline
->text
= NULL
;
3374 { /* main token loop */
3375 if ((mname
= tline
->text
))
3377 /* if this token is a local macro, look in local context */
3378 if (tline
->type
== TOK_ID
|| tline
->type
== TOK_PREPROC_ID
)
3379 ctx
= get_ctx(mname
, TRUE
);
3383 head
= smacros
[hash(mname
)];
3385 head
= ctx
->localmac
;
3387 * We've hit an identifier. As in is_mmacro below, we first
3388 * check whether the identifier is a single-line macro at
3389 * all, then think about checking for parameters if
3392 for (m
= head
; m
; m
= m
->next
)
3393 if (!mstrcmp(m
->name
, mname
, m
->casesense
))
3403 * Simple case: the macro is parameterless. Discard the
3404 * one token that the macro call took, and push the
3405 * expansion back on the to-do stack.
3409 if (!strcmp("__FILE__", m
->name
))
3412 src_get(&num
, &(tline
->text
));
3413 nasm_quote(&(tline
->text
));
3414 tline
->type
= TOK_STRING
;
3417 if (!strcmp("__LINE__", m
->name
))
3419 nasm_free(tline
->text
);
3420 make_tok_num(tline
, src_get_linnum());
3423 tline
= delete_Token(tline
);
3430 * Complicated case: at least one macro with this name
3431 * exists and takes parameters. We must find the
3432 * parameters in the call, count them, find the SMacro
3433 * that corresponds to that form of the macro call, and
3434 * substitute for the parameters when we expand. What a
3437 tline
= tline
->next
;
3439 if (!tok_is_(tline
, "("))
3442 * This macro wasn't called with parameters: ignore
3443 * the call. (Behaviour borrowed from gnu cpp.)
3454 tline
= tline
->next
;
3455 sparam
= PARAM_DELTA
;
3456 params
= nasm_malloc(sparam
* sizeof(Token
*));
3458 paramsize
= nasm_malloc(sparam
* sizeof(int));
3460 for (;; tline
= tline
->next
)
3461 { /* parameter loop */
3465 "macro call expects terminating `)'");
3468 if (tline
->type
== TOK_WHITESPACE
3471 if (paramsize
[nparam
])
3474 params
[nparam
] = tline
->next
;
3475 continue; /* parameter loop */
3477 if (tline
->type
== TOK_OTHER
3478 && tline
->text
[1] == 0)
3480 char ch
= tline
->text
[0];
3481 if (ch
== ',' && !paren
&& brackets
<= 0)
3483 if (++nparam
>= sparam
)
3485 sparam
+= PARAM_DELTA
;
3486 params
= nasm_realloc(params
,
3487 sparam
* sizeof(Token
*));
3488 paramsize
= nasm_realloc(paramsize
,
3489 sparam
* sizeof(int));
3491 params
[nparam
] = tline
->next
;
3492 paramsize
[nparam
] = 0;
3494 continue; /* parameter loop */
3497 (brackets
> 0 || (brackets
== 0 &&
3498 !paramsize
[nparam
])))
3502 params
[nparam
] = tline
->next
;
3503 continue; /* parameter loop */
3506 if (ch
== '}' && brackets
> 0)
3507 if (--brackets
== 0)
3510 continue; /* parameter loop */
3512 if (ch
== '(' && !brackets
)
3514 if (ch
== ')' && brackets
<= 0)
3521 error(ERR_NONFATAL
, "braces do not "
3522 "enclose all of macro parameter");
3524 paramsize
[nparam
] += white
+ 1;
3526 } /* parameter loop */
3528 while (m
&& (m
->nparam
!= nparam
||
3529 mstrcmp(m
->name
, mname
,
3533 error(ERR_WARNING
| ERR_WARN_MNP
,
3534 "macro `%s' exists, "
3535 "but not taking %d parameters",
3536 mstart
->text
, nparam
);
3539 if (m
&& m
->in_progress
)
3541 if (!m
) /* in progess or didn't find '(' or wrong nparam */
3544 * Design question: should we handle !tline, which
3545 * indicates missing ')' here, or expand those
3546 * macros anyway, which requires the (t) test a few
3550 nasm_free(paramsize
);
3556 * Expand the macro: we are placed on the last token of the
3557 * call, so that we can easily split the call from the
3558 * following tokens. We also start by pushing an SMAC_END
3559 * token for the cycle removal.
3567 tt
= new_Token(tline
, TOK_SMAC_END
, NULL
, 0);
3569 m
->in_progress
= TRUE
;
3571 for (t
= m
->expansion
; t
; t
= t
->next
)
3573 if (t
->type
>= TOK_SMAC_PARAM
)
3575 Token
*pcopy
= tline
, **ptail
= &pcopy
;
3579 ttt
= params
[t
->type
- TOK_SMAC_PARAM
];
3580 for (i
= paramsize
[t
->type
- TOK_SMAC_PARAM
];
3584 new_Token(tline
, ttt
->type
, ttt
->text
,
3593 tt
= new_Token(tline
, t
->type
, t
->text
, 0);
3599 * Having done that, get rid of the macro call, and clean
3600 * up the parameters.
3603 nasm_free(paramsize
);
3605 continue; /* main token loop */
3610 if (tline
->type
== TOK_SMAC_END
)
3612 tline
->mac
->in_progress
= FALSE
;
3613 tline
= delete_Token(tline
);
3618 tline
= tline
->next
;
3626 * Now scan the entire line and look for successive TOK_IDs that resulted
3627 * after expansion (they can't be produced by tokenise()). The successive
3628 * TOK_IDs should be concatenated.
3629 * Also we look for %+ tokens and concatenate the tokens before and after
3630 * them (without white spaces in between).
3636 while (t
&& t
->type
!= TOK_ID
&& t
->type
!= TOK_PREPROC_ID
)
3640 if (t
->next
->type
== TOK_ID
||
3641 t
->next
->type
== TOK_PREPROC_ID
||
3642 t
->next
->type
== TOK_NUMBER
)
3644 char *p
= nasm_strcat(t
->text
, t
->next
->text
);
3646 t
->next
= delete_Token(t
->next
);
3650 else if (t
->next
->type
== TOK_WHITESPACE
&& t
->next
->next
&&
3651 t
->next
->next
->type
== TOK_PREPROC_ID
&&
3652 strcmp(t
->next
->next
->text
, "%+") == 0)
3654 /* free the next whitespace, the %+ token and next whitespace */
3656 for (i
= 1; i
<= 3; i
++)
3658 if (!t
->next
|| (i
!= 2 && t
->next
->type
!= TOK_WHITESPACE
))
3660 t
->next
= delete_Token(t
->next
);
3666 /* If we concatenaded something, re-scan the line for macros */
3677 *org_tline
= *thead
;
3678 /* since we just gave text to org_line, don't free it */
3680 delete_Token(thead
);
3684 /* the expression expanded to empty line;
3685 we can't return NULL for some reasons
3686 we just set the line to a single WHITESPACE token. */
3687 memset(org_tline
, 0, sizeof(*org_tline
));
3688 org_tline
->text
= NULL
;
3689 org_tline
->type
= TOK_WHITESPACE
;
3698 * Similar to expand_smacro but used exclusively with macro identifiers
3699 * right before they are fetched in. The reason is that there can be
3700 * identifiers consisting of several subparts. We consider that if there
3701 * are more than one element forming the name, user wants a expansion,
3702 * otherwise it will be left as-is. Example:
3706 * the identifier %$abc will be left as-is so that the handler for %define
3707 * will suck it and define the corresponding value. Other case:
3709 * %define _%$abc cde
3711 * In this case user wants name to be expanded *before* %define starts
3712 * working, so we'll expand %$abc into something (if it has a value;
3713 * otherwise it will be left as-is) then concatenate all successive
3717 expand_id(Token
* tline
)
3719 Token
*cur
, *oldnext
= NULL
;
3721 if (!tline
|| !tline
->next
)
3726 (cur
->next
->type
== TOK_ID
||
3727 cur
->next
->type
== TOK_PREPROC_ID
|| cur
->next
->type
== TOK_NUMBER
))
3730 /* If identifier consists of just one token, don't expand */
3736 oldnext
= cur
->next
; /* Detach the tail past identifier */
3737 cur
->next
= NULL
; /* so that expand_smacro stops here */
3740 tline
= expand_smacro(tline
);
3744 /* expand_smacro possibly changhed tline; re-scan for EOL */
3746 while (cur
&& cur
->next
)
3749 cur
->next
= oldnext
;
3756 * Determine whether the given line constitutes a multi-line macro
3757 * call, and return the MMacro structure called if so. Doesn't have
3758 * to check for an initial label - that's taken care of in
3759 * expand_mmacro - but must check numbers of parameters. Guaranteed
3760 * to be called with tline->type == TOK_ID, so the putative macro
3761 * name is easy to find.
3764 is_mmacro(Token
* tline
, Token
*** params_array
)
3770 head
= mmacros
[hash(tline
->text
)];
3773 * Efficiency: first we see if any macro exists with the given
3774 * name. If not, we can return NULL immediately. _Then_ we
3775 * count the parameters, and then we look further along the
3776 * list if necessary to find the proper MMacro.
3778 for (m
= head
; m
; m
= m
->next
)
3779 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
3785 * OK, we have a potential macro. Count and demarcate the
3788 count_mmac_params(tline
->next
, &nparam
, ¶ms
);
3791 * So we know how many parameters we've got. Find the MMacro
3792 * structure that handles this number.
3796 if (m
->nparam_min
<= nparam
&& (m
->plus
|| nparam
<= m
->nparam_max
))
3799 * This one is right. Just check if cycle removal
3800 * prohibits us using it before we actually celebrate...
3806 "self-reference in multi-line macro `%s'", m
->name
);
3812 * It's right, and we can use it. Add its default
3813 * parameters to the end of our list if necessary.
3815 if (m
->defaults
&& nparam
< m
->nparam_min
+ m
->ndefs
)
3818 nasm_realloc(params
,
3819 ((m
->nparam_min
+ m
->ndefs
+ 1) * sizeof(*params
)));
3820 while (nparam
< m
->nparam_min
+ m
->ndefs
)
3822 params
[nparam
] = m
->defaults
[nparam
- m
->nparam_min
];
3827 * If we've gone over the maximum parameter count (and
3828 * we're in Plus mode), ignore parameters beyond
3831 if (m
->plus
&& nparam
> m
->nparam_max
)
3832 nparam
= m
->nparam_max
;
3834 * Then terminate the parameter list, and leave.
3837 { /* need this special case */
3838 params
= nasm_malloc(sizeof(*params
));
3841 params
[nparam
] = NULL
;
3842 *params_array
= params
;
3846 * This one wasn't right: look for the next one with the
3849 for (m
= m
->next
; m
; m
= m
->next
)
3850 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
3855 * After all that, we didn't find one with the right number of
3856 * parameters. Issue a warning, and fail to expand the macro.
3858 error(ERR_WARNING
| ERR_WARN_MNP
,
3859 "macro `%s' exists, but not taking %d parameters",
3860 tline
->text
, nparam
);
3866 * Expand the multi-line macro call made by the given line, if
3867 * there is one to be expanded. If there is, push the expansion on
3868 * istk->expansion and return 1. Otherwise return 0.
3871 expand_mmacro(Token
* tline
)
3873 Token
*startline
= tline
;
3874 Token
*label
= NULL
;
3875 int dont_prepend
= 0;
3876 Token
**params
, *t
, *tt
;
3879 int i
, nparam
, *paramlen
;
3883 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
3884 if (!tok_type_(t
, TOK_ID
) && !tok_type_(t
, TOK_PREPROC_ID
))
3886 m
= is_mmacro(t
, ¶ms
);
3891 * We have an id which isn't a macro call. We'll assume
3892 * it might be a label; we'll also check to see if a
3893 * colon follows it. Then, if there's another id after
3894 * that lot, we'll check it again for macro-hood.
3898 if (tok_type_(t
, TOK_WHITESPACE
))
3899 last
= t
, t
= t
->next
;
3900 if (tok_is_(t
, ":"))
3903 last
= t
, t
= t
->next
;
3904 if (tok_type_(t
, TOK_WHITESPACE
))
3905 last
= t
, t
= t
->next
;
3907 if (!tok_type_(t
, TOK_ID
) || (m
= is_mmacro(t
, ¶ms
)) == NULL
)
3914 * Fix up the parameters: this involves stripping leading and
3915 * trailing whitespace, then stripping braces if they are
3918 for (nparam
= 0; params
[nparam
]; nparam
++)
3920 paramlen
= nparam
? nasm_malloc(nparam
* sizeof(*paramlen
)) : NULL
;
3922 for (i
= 0; params
[i
]; i
++)
3925 int comma
= (!m
->plus
|| i
< nparam
- 1);
3929 if (tok_is_(t
, "{"))
3930 t
= t
->next
, brace
= TRUE
, comma
= FALSE
;
3935 if (comma
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, ","))
3936 break; /* ... because we have hit a comma */
3937 if (comma
&& t
->type
== TOK_WHITESPACE
&& tok_is_(t
->next
, ","))
3938 break; /* ... or a space then a comma */
3939 if (brace
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, "}"))
3940 break; /* ... or a brace */
3947 * OK, we have a MMacro structure together with a set of
3948 * parameters. We must now go through the expansion and push
3949 * copies of each Line on to istk->expansion. Substitution of
3950 * parameter tokens and macro-local tokens doesn't get done
3951 * until the single-line macro substitution process; this is
3952 * because delaying them allows us to change the semantics
3953 * later through %rotate.
3955 * First, push an end marker on to istk->expansion, mark this
3956 * macro as in progress, and set up its invocation-specific
3959 ll
= nasm_malloc(sizeof(Line
));
3960 ll
->next
= istk
->expansion
;
3963 istk
->expansion
= ll
;
3965 m
->in_progress
= TRUE
;
3970 m
->paramlen
= paramlen
;
3971 m
->unique
= unique
++;
3974 m
->next_active
= istk
->mstk
;
3977 for (l
= m
->expansion
; l
; l
= l
->next
)
3981 ll
= nasm_malloc(sizeof(Line
));
3982 ll
->finishes
= NULL
;
3983 ll
->next
= istk
->expansion
;
3984 istk
->expansion
= ll
;
3987 for (t
= l
->first
; t
; t
= t
->next
)
3990 if (t
->type
== TOK_PREPROC_ID
&&
3991 t
->text
[1] == '0' && t
->text
[2] == '0')
3998 tt
= *tail
= new_Token(NULL
, x
->type
, x
->text
, 0);
4005 * If we had a label, push it on as the first line of
4006 * the macro expansion.
4010 if (dont_prepend
< 0)
4011 free_tlist(startline
);
4014 ll
= nasm_malloc(sizeof(Line
));
4015 ll
->finishes
= NULL
;
4016 ll
->next
= istk
->expansion
;
4017 istk
->expansion
= ll
;
4018 ll
->first
= startline
;
4022 label
= label
->next
;
4023 label
->next
= tt
= new_Token(NULL
, TOK_OTHER
, ":", 0);
4028 list
->uplevel(m
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
4034 * Since preprocessor always operate only on the line that didn't
4035 * arrived yet, we should always use ERR_OFFBY1. Also since user
4036 * won't want to see same error twice (preprocessing is done once
4037 * per pass) we will want to show errors only during pass one.
4040 error(int severity
, const char *fmt
, ...)
4045 /* If we're in a dead branch of IF or something like it, ignore the error */
4046 if (istk
&& istk
->conds
&& !emitting(istk
->conds
->state
))
4050 vsprintf(buff
, fmt
, arg
);
4053 if (istk
&& istk
->mstk
&& istk
->mstk
->name
)
4054 _error(severity
| ERR_PASS1
, "(%s:%d) %s", istk
->mstk
->name
,
4055 istk
->mstk
->lineno
, buff
);
4057 _error(severity
| ERR_PASS1
, "%s", buff
);
4061 pp_reset(char *file
, int apass
, efunc errfunc
, evalfunc eval
,
4068 istk
= nasm_malloc(sizeof(Include
));
4071 istk
->expansion
= NULL
;
4073 istk
->fp
= fopen(file
, "r");
4075 src_set_fname(nasm_strdup(file
));
4079 error(ERR_FATAL
| ERR_NOFILE
, "unable to open input file `%s'", file
);
4081 for (h
= 0; h
< NHASH
; h
++)
4087 if (tasm_compatible_mode
) {
4090 stdmacpos
= &stdmac
[TASM_MACRO_COUNT
];
4092 any_extrastdmac
= (extrastdmac
!= NULL
);
4107 * Fetch a tokenised line, either from the macro-expansion
4108 * buffer or from the input file.
4111 while (istk
->expansion
&& istk
->expansion
->finishes
)
4113 Line
*l
= istk
->expansion
;
4114 if (!l
->finishes
->name
&& l
->finishes
->in_progress
> 1)
4119 * This is a macro-end marker for a macro with no
4120 * name, which means it's not really a macro at all
4121 * but a %rep block, and the `in_progress' field is
4122 * more than 1, meaning that we still need to
4123 * repeat. (1 means the natural last repetition; 0
4124 * means termination by %exitrep.) We have
4125 * therefore expanded up to the %endrep, and must
4126 * push the whole block on to the expansion buffer
4127 * again. We don't bother to remove the macro-end
4128 * marker: we'd only have to generate another one
4131 l
->finishes
->in_progress
--;
4132 for (l
= l
->finishes
->expansion
; l
; l
= l
->next
)
4134 Token
*t
, *tt
, **tail
;
4136 ll
= nasm_malloc(sizeof(Line
));
4137 ll
->next
= istk
->expansion
;
4138 ll
->finishes
= NULL
;
4142 for (t
= l
->first
; t
; t
= t
->next
)
4144 if (t
->text
|| t
->type
== TOK_WHITESPACE
)
4146 tt
= *tail
= new_Token(NULL
, t
->type
, t
->text
, 0);
4151 istk
->expansion
= ll
;
4157 * Check whether a `%rep' was started and not ended
4158 * within this macro expansion. This can happen and
4159 * should be detected. It's a fatal error because
4160 * I'm too confused to work out how to recover
4166 error(ERR_PANIC
, "defining with name in expansion");
4167 else if (istk
->mstk
->name
)
4168 error(ERR_FATAL
, "`%%rep' without `%%endrep' within"
4169 " expansion of macro `%s'", istk
->mstk
->name
);
4173 * FIXME: investigate the relationship at this point between
4174 * istk->mstk and l->finishes
4177 MMacro
*m
= istk
->mstk
;
4178 istk
->mstk
= m
->next_active
;
4182 * This was a real macro call, not a %rep, and
4183 * therefore the parameter information needs to
4186 nasm_free(m
->params
);
4187 free_tlist(m
->iline
);
4188 nasm_free(m
->paramlen
);
4189 l
->finishes
->in_progress
= FALSE
;
4194 istk
->expansion
= l
->next
;
4196 list
->downlevel(LIST_MACRO
);
4200 { /* until we get a line we can use */
4202 if (istk
->expansion
)
4203 { /* from a macro expansion */
4205 Line
*l
= istk
->expansion
;
4207 istk
->mstk
->lineno
++;
4209 istk
->expansion
= l
->next
;
4211 p
= detoken(tline
, FALSE
);
4212 list
->line(LIST_MACRO
, p
);
4218 { /* from the current input file */
4219 line
= prepreproc(line
);
4220 tline
= tokenise(line
);
4225 * The current file has ended; work down the istk
4231 error(ERR_FATAL
, "expected `%%endif' before end of file");
4232 /* only set line and file name if there's a next node */
4235 src_set_linnum(i
->lineno
);
4236 nasm_free(src_set_fname(i
->fname
));
4239 list
->downlevel(LIST_INCLUDE
);
4247 * We must expand MMacro parameters and MMacro-local labels
4248 * _before_ we plunge into directive processing, to cope
4249 * with things like `%define something %1' such as STRUC
4250 * uses. Unless we're _defining_ a MMacro, in which case
4251 * those tokens should be left alone to go into the
4252 * definition; and unless we're in a non-emitting
4253 * condition, in which case we don't want to meddle with
4256 if (!defining
&& !(istk
->conds
&& !emitting(istk
->conds
->state
)))
4257 tline
= expand_mmac_params(tline
);
4260 * Check the line to see if it's a preprocessor directive.
4262 if (do_directive(tline
) == DIRECTIVE_FOUND
)
4269 * We're defining a multi-line macro. We emit nothing
4271 * shove the tokenised line on to the macro definition.
4273 Line
*l
= nasm_malloc(sizeof(Line
));
4274 l
->next
= defining
->expansion
;
4276 l
->finishes
= FALSE
;
4277 defining
->expansion
= l
;
4280 else if (istk
->conds
&& !emitting(istk
->conds
->state
))
4283 * We're in a non-emitting branch of a condition block.
4284 * Emit nothing at all, not even a blank line: when we
4285 * emerge from the condition we'll give a line-number
4286 * directive so we keep our place correctly.
4291 else if (istk
->mstk
&& !istk
->mstk
->in_progress
)
4294 * We're in a %rep block which has been terminated, so
4295 * we're walking through to the %endrep without
4296 * emitting anything. Emit nothing at all, not even a
4297 * blank line: when we emerge from the %rep block we'll
4298 * give a line-number directive so we keep our place
4306 tline
= expand_smacro(tline
);
4307 if (!expand_mmacro(tline
))
4310 * De-tokenise the line again, and emit it.
4312 line
= detoken(tline
, TRUE
);
4318 continue; /* expand_mmacro calls free_tlist */
4327 pp_cleanup(int pass
)
4333 error(ERR_NONFATAL
, "end of file while still defining macro `%s'",
4335 free_mmacro(defining
);
4339 for (h
= 0; h
< NHASH
; h
++)
4343 MMacro
*m
= mmacros
[h
];
4344 mmacros
[h
] = mmacros
[h
]->next
;
4349 SMacro
*s
= smacros
[h
];
4350 smacros
[h
] = smacros
[h
]->next
;
4352 free_tlist(s
->expansion
);
4361 nasm_free(i
->fname
);
4374 pp_include_path(char *path
)
4378 i
= nasm_malloc(sizeof(IncPath
));
4379 i
->path
= nasm_strdup(path
);
4385 pp_pre_include(char *fname
)
4387 Token
*inc
, *space
, *name
;
4390 name
= new_Token(NULL
, TOK_INTERNAL_STRING
, fname
, 0);
4391 space
= new_Token(name
, TOK_WHITESPACE
, NULL
, 0);
4392 inc
= new_Token(space
, TOK_PREPROC_ID
, "%include", 0);
4394 l
= nasm_malloc(sizeof(Line
));
4397 l
->finishes
= FALSE
;
4402 pp_pre_define(char *definition
)
4408 equals
= strchr(definition
, '=');
4409 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
4410 def
= new_Token(space
, TOK_PREPROC_ID
, "%define", 0);
4413 space
->next
= tokenise(definition
);
4417 l
= nasm_malloc(sizeof(Line
));
4420 l
->finishes
= FALSE
;
4425 pp_pre_undefine(char *definition
)
4430 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
4431 def
= new_Token(space
, TOK_PREPROC_ID
, "%undef", 0);
4433 l
= nasm_malloc(sizeof(Line
));
4436 l
->finishes
= FALSE
;
4441 pp_extra_stdmac(const char **macros
)
4443 extrastdmac
= macros
;
4447 make_tok_num(Token
* tok
, long val
)
4450 sprintf(numbuf
, "%ld", val
);
4451 tok
->text
= nasm_strdup(numbuf
);
4452 tok
->type
= TOK_NUMBER
;