1 /* preproc.c macro preprocessor for the Netwide Assembler
3 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4 * Julian Hall. All rights reserved. The software is
5 * redistributable under the licence given in the file "Licence"
6 * distributed in the NASM archive.
8 * initial version 18/iii/97 by Simon Tatham
11 /* Typical flow of text through preproc
13 * pp_getline gets tokenised lines, either
15 * from a macro expansion
19 * read_line gets raw text from stdmacpos, or predef, or current input file
20 * tokenise converts to tokens
23 * expand_mmac_params is used to expand %1 etc., unless a macro is being
24 * defined or a false conditional is being processed
25 * (%0, %1, %+1, %-1, %%foo
27 * do_directive checks for directives
29 * expand_smacro is used to expand single line macros
31 * expand_mmacro is used to expand multi-line macros
33 * detoken is used to convert the line back to text
47 typedef struct SMacro SMacro
;
48 typedef struct MMacro MMacro
;
49 typedef struct Context Context
;
50 typedef struct Token Token
;
51 typedef struct Line Line
;
52 typedef struct Include Include
;
53 typedef struct Cond Cond
;
54 typedef struct IncPath IncPath
;
57 * Store the definition of a single-line macro.
70 * Store the definition of a multi-line macro. This is also used to
71 * store the interiors of `%rep...%endrep' blocks, which are
72 * effectively self-re-invoking multi-line macros which simply
73 * don't have a name or bother to appear in the hash tables. %rep
74 * blocks are signified by having a NULL `name' field.
76 * In a MMacro describing a `%rep' block, the `in_progress' field
77 * isn't merely boolean, but gives the number of repeats left to
80 * The `next' field is used for storing MMacros in hash tables; the
81 * `next_active' field is for stacking them on istk entries.
83 * When a MMacro is being expanded, `params', `iline', `nparam',
84 * `paramlen', `rotate' and `unique' are local to the invocation.
91 int nparam_min
, nparam_max
;
92 int plus
; /* is the last parameter greedy? */
93 int nolist
; /* is this macro listing-inhibited? */
95 Token
*dlist
; /* All defaults as one list */
96 Token
**defaults
; /* Parameter default pointers */
97 int ndefs
; /* number of default parameters */
101 MMacro
*rep_nest
; /* used for nesting %rep */
102 Token
**params
; /* actual parameters */
103 Token
*iline
; /* invocation line */
104 int nparam
, rotate
, *paramlen
;
105 unsigned long unique
;
106 int lineno
; /* Current line number on expansion */
110 * The context stack is composed of a linked list of these.
117 unsigned long number
;
121 * This is the internal form which we break input lines up into.
122 * Typically stored in linked lists.
124 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
125 * necessarily used as-is, but is intended to denote the number of
126 * the substituted parameter. So in the definition
128 * %define a(x,y) ( (x) & ~(y) )
130 * the token representing `x' will have its type changed to
131 * TOK_SMAC_PARAM, but the one representing `y' will be
134 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
135 * which doesn't need quotes around it. Used in the pre-include
136 * mechanism as an alternative to trying to find a sensible type of
137 * quote to use on the filename we were passed.
143 SMacro
*mac
; /* associated macro for TOK_SMAC_END */
148 TOK_WHITESPACE
= 1, TOK_COMMENT
, TOK_ID
, TOK_PREPROC_ID
, TOK_STRING
,
149 TOK_NUMBER
, TOK_SMAC_END
, TOK_OTHER
, TOK_SMAC_PARAM
,
154 * Multi-line macro definitions are stored as a linked list of
155 * these, which is essentially a container to allow several linked
158 * Note that in this module, linked lists are treated as stacks
159 * wherever possible. For this reason, Lines are _pushed_ on to the
160 * `expansion' field in MMacro structures, so that the linked list,
161 * if walked, would give the macro lines in reverse order; this
162 * means that we can walk the list when expanding a macro, and thus
163 * push the lines on to the `expansion' field in _istk_ in reverse
164 * order (so that when popped back off they are in the right
165 * order). It may seem cockeyed, and it relies on my design having
166 * an even number of steps in, but it works...
168 * Some of these structures, rather than being actual lines, are
169 * markers delimiting the end of the expansion of a given macro.
170 * This is for use in the cycle-tracking and %rep-handling code.
171 * Such structures have `finishes' non-NULL, and `first' NULL. All
172 * others have `finishes' NULL, but `first' may still be NULL if
183 * To handle an arbitrary level of file inclusion, we maintain a
184 * stack (ie linked list) of these things.
194 MMacro
*mstk
; /* stack of active macros/reps */
198 * Include search path. This is simply a list of strings which get
199 * prepended, in turn, to the name of an include file, in an
200 * attempt to find the file if it's not in the current directory.
209 * Conditional assembly: we maintain a separate stack of these for
210 * each level of file inclusion. (The only reason we keep the
211 * stacks separate is to ensure that a stray `%endif' in a file
212 * included from within the true branch of a `%if' won't terminate
213 * it and cause confusion: instead, rightly, it'll cause an error.)
223 * These states are for use just after %if or %elif: IF_TRUE
224 * means the condition has evaluated to truth so we are
225 * currently emitting, whereas IF_FALSE means we are not
226 * currently emitting but will start doing so if a %else comes
227 * up. In these states, all directives are admissible: %elif,
228 * %else and %endif. (And of course %if.)
230 COND_IF_TRUE
, COND_IF_FALSE
,
232 * These states come up after a %else: ELSE_TRUE means we're
233 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
234 * any %elif or %else will cause an error.
236 COND_ELSE_TRUE
, COND_ELSE_FALSE
,
238 * This state means that we're not emitting now, and also that
239 * nothing until %endif will be emitted at all. It's for use in
240 * two circumstances: (i) when we've had our moment of emission
241 * and have now started seeing %elifs, and (ii) when the
242 * condition construct in question is contained within a
243 * non-emitting branch of a larger condition construct.
247 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
250 * Condition codes. Note that we use c_ prefix not C_ because C_ is
251 * used in nasm.h for the "real" condition codes. At _this_ level,
252 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
253 * ones, so we need a different enum...
255 static char *conditions
[] = {
256 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
257 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
258 "np", "ns", "nz", "o", "p", "pe", "po", "s", "z"
262 c_A
, c_AE
, c_B
, c_BE
, c_C
, c_CXZ
, c_E
, c_ECXZ
, c_G
, c_GE
, c_L
, c_LE
,
263 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, c_NE
, c_NG
, c_NGE
, c_NL
, c_NLE
, c_NO
,
264 c_NP
, c_NS
, c_NZ
, c_O
, c_P
, c_PE
, c_PO
, c_S
, c_Z
266 static int inverse_ccs
[] = {
267 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, -1, c_NE
, -1, c_NG
, c_NGE
, c_NL
, c_NLE
,
268 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
,
269 c_Z
, c_NO
, c_NP
, c_PO
, c_PE
, c_NS
, c_NZ
275 static char *directives
[] = {
277 "%assign", "%clear", "%define", "%elif", "%elifctx", "%elifdef",
278 "%elifid", "%elifidn", "%elifidni", "%elifnctx", "%elifndef",
279 "%elifnid", "%elifnidn", "%elifnidni", "%elifnnum", "%elifnstr",
280 "%elifnum", "%elifstr", "%else", "%endif", "%endm", "%endmacro",
281 "%endrep", "%error", "%exitrep", "%iassign", "%idefine", "%if",
282 "%ifctx", "%ifdef", "%ifid", "%ifidn", "%ifidni", "%ifnctx",
283 "%ifndef", "%ifnid", "%ifnidn", "%ifnidni", "%ifnnum",
284 "%ifnstr", "%ifnum", "%ifstr", "%imacro", "%include",
285 "%ixdefine", "%line",
287 "%macro", "%pop", "%push", "%rep", "%repl", "%rotate",
289 "%strlen", "%substr", "%undef", "%xdefine"
294 PP_ASSIGN
, PP_CLEAR
, PP_DEFINE
, PP_ELIF
, PP_ELIFCTX
, PP_ELIFDEF
,
295 PP_ELIFID
, PP_ELIFIDN
, PP_ELIFIDNI
, PP_ELIFNCTX
, PP_ELIFNDEF
,
296 PP_ELIFNID
, PP_ELIFNIDN
, PP_ELIFNIDNI
, PP_ELIFNNUM
, PP_ELIFNSTR
,
297 PP_ELIFNUM
, PP_ELIFSTR
, PP_ELSE
, PP_ENDIF
, PP_ENDM
, PP_ENDMACRO
,
298 PP_ENDREP
, PP_ERROR
, PP_EXITREP
, PP_IASSIGN
, PP_IDEFINE
, PP_IF
,
299 PP_IFCTX
, PP_IFDEF
, PP_IFID
, PP_IFIDN
, PP_IFIDNI
, PP_IFNCTX
,
300 PP_IFNDEF
, PP_IFNID
, PP_IFNIDN
, PP_IFNIDNI
, PP_IFNNUM
,
301 PP_IFNSTR
, PP_IFNUM
, PP_IFSTR
, PP_IMACRO
, PP_INCLUDE
,
302 PP_IXDEFINE
, PP_LINE
,
304 PP_MACRO
, PP_POP
, PP_PUSH
, PP_REP
, PP_REPL
, PP_ROTATE
,
306 PP_STRLEN
, PP_SUBSTR
, PP_UNDEF
, PP_XDEFINE
310 /* For TASM compatibility we need to be able to recognise TASM compatible
311 * conditional compilation directives. Using the NASM pre-processor does
312 * not work, so we look for them specifically from the following list and
313 * then jam in the equivalent NASM directive into the input stream.
317 # define MAX(a,b) ( ((a) > (b)) ? (a) : (b))
322 TM_ARG
, TM_ELIF
, TM_ELSE
, TM_ENDIF
, TM_IF
, TM_IFDEF
, TM_IFDIFI
,
323 TM_IFNDEF
, TM_INCLUDE
, TM_LOCAL
326 static char *tasm_directives
[] = {
327 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
328 "ifndef", "include", "local"
331 static int StackSize
= 4;
332 static char *StackPointer
= "ebp";
333 static int ArgOffset
= 8;
334 static int LocalOffset
= 4;
337 static Context
*cstk
;
338 static Include
*istk
;
339 static IncPath
*ipath
= NULL
;
341 static efunc __error
; /* Pointer to client-provided error reporting function */
342 static evalfunc evaluate
;
344 static int pass
; /* HACK: pass 0 = generate dependencies only */
346 static unsigned long unique
; /* unique identifier numbers */
348 static Line
*predef
= NULL
;
350 static ListGen
*list
;
353 * The number of hash values we use for the macro lookup tables.
354 * FIXME: We should *really* be able to configure this at run time,
355 * or even have the hash table automatically expanding when necessary.
360 * The current set of multi-line macros we have defined.
362 static MMacro
*mmacros
[NHASH
];
365 * The current set of single-line macros we have defined.
367 static SMacro
*smacros
[NHASH
];
370 * The multi-line macro we are currently defining, or the %rep
371 * block we are currently reading, if any.
373 static MMacro
*defining
;
376 * The number of macro parameters to allocate space for at a time.
378 #define PARAM_DELTA 16
381 * The standard macro set: defined as `static char *stdmac[]'. Also
382 * gives our position in the macro set, when we're processing it.
385 static char **stdmacpos
;
388 * The extra standard macros that come from the object format, if
391 static char **extrastdmac
= NULL
;
395 * Tokens are allocated in blocks to improve speed
397 #define TOKEN_BLOCKSIZE 4096
398 static Token
*freeTokens
= NULL
;
401 * Forward declarations.
403 static Token
*expand_mmac_params(Token
* tline
);
404 static Token
*expand_smacro(Token
* tline
);
405 static Token
*expand_id(Token
* tline
);
406 static Context
*get_ctx(char *name
, int all_contexts
);
407 static void make_tok_num(Token
* tok
, long val
);
408 static void error(int severity
, char *fmt
, ...);
409 static Token
*new_Token(Token
* next
, int type
, char *text
, int txtlen
);
410 static Token
*delete_Token(Token
* t
);
413 * Macros for safe checking of token pointers, avoid *(NULL)
415 #define tok_type_(x,t) ((x) && (x)->type == (t))
416 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
417 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
418 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
420 /* Handle TASM specific directives, which do not contain a % in
421 * front of them. We do it here because I could not find any other
422 * place to do it for the moment, and it is a hack (ideally it would
423 * be nice to be able to use the NASM pre-processor to do it).
426 check_tasm_directive(char *line
)
429 char *p
= line
, *oldline
, oldchar
;
431 /* Skip whitespace */
432 while (isspace(*p
) && *p
!= 0)
435 /* Binary search for the directive name */
437 j
= sizeof(tasm_directives
) / sizeof(*tasm_directives
);
439 while (!isspace(p
[len
]) && p
[len
] != 0)
448 m
= nasm_stricmp(p
, tasm_directives
[k
]);
451 /* We have found a directive, so jam a % in front of it
452 * so that NASM will then recognise it as one if it's own.
457 line
= nasm_malloc(len
+ 2);
461 /* NASM does not recognise IFDIFI, so we convert it to
462 * %ifdef BOGUS. This is not used in NASM comaptible
463 * code, but does need to parse for the TASM macro
466 strcpy(line
+ 1, "ifdef BOGUS");
470 memcpy(line
+ 1, p
, len
+ 1);
488 * The pre-preprocessing stage... This function translates line
489 * number indications as they emerge from GNU cpp (`# lineno "file"
490 * flags') into NASM preprocessor line number indications (`%line
494 prepreproc(char *line
)
497 char *fname
, *oldline
;
499 if (line
[0] == '#' && line
[1] == ' ')
503 lineno
= atoi(fname
);
504 fname
+= strspn(fname
, "0123456789 ");
507 fnlen
= strcspn(fname
, "\"");
508 line
= nasm_malloc(20 + fnlen
);
509 sprintf(line
, "%%line %d %.*s", lineno
, fnlen
, fname
);
512 if (tasm_compatible_mode
)
513 return check_tasm_directive(line
);
518 * The hash function for macro lookups. Note that due to some
519 * macros having case-insensitive names, the hash function must be
520 * invariant under case changes. We implement this by applying a
521 * perfectly normal hash function to the uppercase of the string.
529 * Powers of three, mod 31.
531 static const int multipliers
[] = {
532 1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10,
533 30, 28, 22, 4, 12, 5, 15, 14, 11, 2, 6, 18, 23, 7, 21
539 h
+= multipliers
[i
] * (unsigned char) (toupper(*s
));
541 if (++i
>= sizeof(multipliers
) / sizeof(*multipliers
))
549 * Free a linked list of tokens.
552 free_tlist(Token
* list
)
556 list
= delete_Token(list
);
561 * Free a linked list of lines.
564 free_llist(Line
* list
)
571 free_tlist(l
->first
);
580 free_mmacro(MMacro
* m
)
583 free_tlist(m
->dlist
);
584 nasm_free(m
->defaults
);
585 free_llist(m
->expansion
);
590 * Pop the context stack.
605 free_tlist(s
->expansion
);
612 #define BUF_DELTA 512
614 * Read a line from the top file in istk, handling multiple CR/LFs
615 * at the end of the line read, and handling spurious ^Zs. Will
616 * return lines from the standard macro set if this has not already
622 char *buffer
, *p
, *q
;
629 char *ret
= nasm_strdup(*stdmacpos
++);
630 if (!*stdmacpos
&& any_extrastdmac
)
632 stdmacpos
= extrastdmac
;
633 any_extrastdmac
= FALSE
;
637 * Nasty hack: here we push the contents of `predef' on
638 * to the top-level expansion stack, since this is the
639 * most convenient way to implement the pre-include and
640 * pre-define features.
645 Token
*head
, **tail
, *t
;
647 for (pd
= predef
; pd
; pd
= pd
->next
)
651 for (t
= pd
->first
; t
; t
= t
->next
)
653 *tail
= new_Token(NULL
, t
->type
, t
->text
, 0);
654 tail
= &(*tail
)->next
;
656 l
= nasm_malloc(sizeof(Line
));
657 l
->next
= istk
->expansion
;
672 buffer
= nasm_malloc(BUF_DELTA
);
676 q
= fgets(p
, bufsize
- (p
- buffer
), istk
->fp
);
680 if (p
> buffer
&& p
[-1] == '\n')
684 if (p
- buffer
> bufsize
- 10)
686 long offset
= p
- buffer
;
687 bufsize
+= BUF_DELTA
;
688 buffer
= nasm_realloc(buffer
, bufsize
);
689 p
= buffer
+ offset
; /* prevent stale-pointer problems */
693 if (!q
&& p
== buffer
)
699 src_set_linnum(src_get_linnum() + istk
->lineinc
);
702 * Play safe: remove CRs as well as LFs, if any of either are
703 * present at the end of the line.
705 while (--p
>= buffer
&& (*p
== '\n' || *p
== '\r'))
709 * Handle spurious ^Z, which may be inserted into source files
710 * by some file transfer utilities.
712 buffer
[strcspn(buffer
, "\032")] = '\0';
714 list
->line(LIST_READ
, buffer
);
720 * Tokenise a line of text. This is a very simple process since we
721 * don't need to parse the value out of e.g. numeric tokens: we
722 * simply split one string into many.
730 Token
*t
, **tail
= &list
;
739 ((*p
== '-' || *p
== '+') && isdigit(p
[1])) ||
740 ((*p
== '+') && (isspace(p
[1]) || !p
[1])))
747 type
= TOK_PREPROC_ID
;
752 while (*p
&& *p
!= '}')
760 type
= TOK_PREPROC_ID
;
762 else if (isidchar(*p
) ||
763 ((*p
== '!' || *p
== '%' || *p
== '$') &&
770 while (isidchar(*p
));
771 type
= TOK_PREPROC_ID
;
780 else if (isidstart(*p
) || (*p
== '$' && isidstart(p
[1])))
784 while (*p
&& isidchar(*p
))
787 else if (*p
== '\'' || *p
== '"')
795 while (*p
&& *p
!= c
)
803 error(ERR_WARNING
, "unterminated string");
806 else if (isnumstart(*p
))
813 while (*p
&& isnumchar(*p
))
816 else if (isspace(*p
))
818 type
= TOK_WHITESPACE
;
820 while (*p
&& isspace(*p
))
823 * Whitespace just before end-of-line is discarded by
824 * pretending it's a comment; whitespace just before a
825 * comment gets lumped into the comment.
827 if (!*p
|| *p
== ';')
843 * Anything else is an operator of some kind. We check
844 * for all the double-character operators (>>, <<, //,
845 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
846 * else is a single-character operator.
849 if ((p
[0] == '>' && p
[1] == '>') ||
850 (p
[0] == '<' && p
[1] == '<') ||
851 (p
[0] == '/' && p
[1] == '/') ||
852 (p
[0] == '<' && p
[1] == '=') ||
853 (p
[0] == '>' && p
[1] == '=') ||
854 (p
[0] == '=' && p
[1] == '=') ||
855 (p
[0] == '!' && p
[1] == '=') ||
856 (p
[0] == '<' && p
[1] == '>') ||
857 (p
[0] == '&' && p
[1] == '&') ||
858 (p
[0] == '|' && p
[1] == '|') ||
859 (p
[0] == '^' && p
[1] == '^'))
865 if (type
!= TOK_COMMENT
)
867 *tail
= t
= new_Token(NULL
, type
, line
, p
- line
);
878 * this function creates a new Token and passes a pointer to it
879 * back to the caller. It sets the type and text elements, and
880 * also the mac and next elements to NULL.
883 new_Token(Token
* next
, int type
, char *text
, int txtlen
)
888 if (freeTokens
== NULL
)
890 freeTokens
= nasm_malloc(TOKEN_BLOCKSIZE
* sizeof(Token
));
891 for (i
= 0; i
< TOKEN_BLOCKSIZE
- 1; i
++)
892 freeTokens
[i
].next
= &freeTokens
[i
+ 1];
893 freeTokens
[i
].next
= NULL
;
896 freeTokens
= t
->next
;
900 if (type
== TOK_WHITESPACE
|| text
== NULL
)
907 txtlen
= strlen(text
);
908 t
->text
= nasm_malloc(1 + txtlen
);
909 strncpy(t
->text
, text
, txtlen
);
910 t
->text
[txtlen
] = '\0';
916 delete_Token(Token
* t
)
918 Token
*next
= t
->next
;
920 /* t->next = freeTokens ? freeTokens->next : NULL; */
921 t
->next
= freeTokens
;
927 * Convert a line of tokens back into text.
928 * If expand_locals is not zero, identifiers of the form "%$*xxx"
929 * will be transformed into ..@ctxnum.xxx
932 detoken(Token
* tlist
, int expand_locals
)
939 for (t
= tlist
; t
; t
= t
->next
)
941 if (t
->type
== TOK_PREPROC_ID
&& t
->text
[1] == '!')
943 char *p
= getenv(t
->text
+ 2);
946 t
->text
= nasm_strdup(p
);
950 /* Expand local macros here and not during preprocessing */
952 t
->type
== TOK_PREPROC_ID
&& t
->text
&&
953 t
->text
[0] == '%' && t
->text
[1] == '$')
955 Context
*ctx
= get_ctx(t
->text
, FALSE
);
959 char *p
, *q
= t
->text
+ 2;
962 sprintf(buffer
, "..@%lu.", ctx
->number
);
963 p
= nasm_strcat(buffer
, q
);
968 if (t
->type
== TOK_WHITESPACE
)
974 len
+= strlen(t
->text
);
977 p
= line
= nasm_malloc(len
+ 1);
978 for (t
= tlist
; t
; t
= t
->next
)
980 if (t
->type
== TOK_WHITESPACE
)
997 * A scanner, suitable for use by the expression evaluator, which
998 * operates on a line of Tokens. Expects a pointer to a pointer to
999 * the first token in the line to be passed in as its private_data
1003 ppscan(void *private_data
, struct tokenval
*tokval
)
1005 Token
**tlineptr
= private_data
;
1011 *tlineptr
= tline
? tline
->next
: NULL
;
1013 while (tline
&& (tline
->type
== TOK_WHITESPACE
||
1014 tline
->type
== TOK_COMMENT
));
1017 return tokval
->t_type
= TOKEN_EOS
;
1019 if (tline
->text
[0] == '$' && !tline
->text
[1])
1020 return tokval
->t_type
= TOKEN_HERE
;
1021 if (tline
->text
[0] == '$' && tline
->text
[1] == '$' && !tline
->text
[1])
1022 return tokval
->t_type
= TOKEN_BASE
;
1024 if (tline
->type
== TOK_ID
)
1026 tokval
->t_charptr
= tline
->text
;
1027 if (tline
->text
[0] == '$')
1029 tokval
->t_charptr
++;
1030 return tokval
->t_type
= TOKEN_ID
;
1034 * This is the only special case we actually need to worry
1035 * about in this restricted context.
1037 if (!nasm_stricmp(tline
->text
, "seg"))
1038 return tokval
->t_type
= TOKEN_SEG
;
1040 return tokval
->t_type
= TOKEN_ID
;
1043 if (tline
->type
== TOK_NUMBER
)
1047 tokval
->t_integer
= readnum(tline
->text
, &rn_error
);
1049 return tokval
->t_type
= TOKEN_ERRNUM
;
1050 tokval
->t_charptr
= NULL
;
1051 return tokval
->t_type
= TOKEN_NUM
;
1054 if (tline
->type
== TOK_STRING
)
1064 if (l
== 0 || r
[l
- 1] != q
)
1065 return tokval
->t_type
= TOKEN_ERRNUM
;
1066 tokval
->t_integer
= readstrnum(r
, l
- 1, &rn_warn
);
1068 error(ERR_WARNING
| ERR_PASS1
, "character constant too long");
1069 tokval
->t_charptr
= NULL
;
1070 return tokval
->t_type
= TOKEN_NUM
;
1073 if (tline
->type
== TOK_OTHER
)
1075 if (!strcmp(tline
->text
, "<<"))
1076 return tokval
->t_type
= TOKEN_SHL
;
1077 if (!strcmp(tline
->text
, ">>"))
1078 return tokval
->t_type
= TOKEN_SHR
;
1079 if (!strcmp(tline
->text
, "//"))
1080 return tokval
->t_type
= TOKEN_SDIV
;
1081 if (!strcmp(tline
->text
, "%%"))
1082 return tokval
->t_type
= TOKEN_SMOD
;
1083 if (!strcmp(tline
->text
, "=="))
1084 return tokval
->t_type
= TOKEN_EQ
;
1085 if (!strcmp(tline
->text
, "<>"))
1086 return tokval
->t_type
= TOKEN_NE
;
1087 if (!strcmp(tline
->text
, "!="))
1088 return tokval
->t_type
= TOKEN_NE
;
1089 if (!strcmp(tline
->text
, "<="))
1090 return tokval
->t_type
= TOKEN_LE
;
1091 if (!strcmp(tline
->text
, ">="))
1092 return tokval
->t_type
= TOKEN_GE
;
1093 if (!strcmp(tline
->text
, "&&"))
1094 return tokval
->t_type
= TOKEN_DBL_AND
;
1095 if (!strcmp(tline
->text
, "^^"))
1096 return tokval
->t_type
= TOKEN_DBL_XOR
;
1097 if (!strcmp(tline
->text
, "||"))
1098 return tokval
->t_type
= TOKEN_DBL_OR
;
1102 * We have no other options: just return the first character of
1105 return tokval
->t_type
= tline
->text
[0];
1109 * Compare a string to the name of an existing macro; this is a
1110 * simple wrapper which calls either strcmp or nasm_stricmp
1111 * depending on the value of the `casesense' parameter.
1114 mstrcmp(char *p
, char *q
, int casesense
)
1116 return casesense
? strcmp(p
, q
) : nasm_stricmp(p
, q
);
1120 * Return the Context structure associated with a %$ token. Return
1121 * NULL, having _already_ reported an error condition, if the
1122 * context stack isn't deep enough for the supplied number of $
1124 * If all_contexts == TRUE, contexts that enclose current are
1125 * also scanned for such smacro, until it is found; if not -
1126 * only the context that directly results from the number of $'s
1127 * in variable's name.
1130 get_ctx(char *name
, int all_contexts
)
1136 if (!name
|| name
[0] != '%' || name
[1] != '$')
1141 error(ERR_NONFATAL
, "`%s': context stack is empty", name
);
1145 for (i
= strspn(name
+ 2, "$"), ctx
= cstk
; (i
> 0) && ctx
; i
--)
1152 error(ERR_NONFATAL
, "`%s': context stack is only"
1153 " %d level%s deep", name
, i
- 1, (i
== 2 ? "" : "s"));
1161 /* Search for this smacro in found context */
1165 if (!mstrcmp(m
->name
, name
, m
->casesense
))
1175 /* Add a slash to the end of a path if it is missing. We use the
1176 * forward slash to make it compatible with Unix systems.
1181 int pos
= strlen(s
);
1182 if (s
[pos
- 1] != '\\' && s
[pos
- 1] != '/')
1190 * Open an include file. This routine must always return a valid
1191 * file pointer if it returns - it's responsible for throwing an
1192 * ERR_FATAL and bombing out completely if not. It should also try
1193 * the include path one by one until it finds the file or reaches
1194 * the end of the path.
1197 inc_fopen(char *file
)
1200 char *prefix
= "", *combine
;
1201 IncPath
*ip
= ipath
;
1202 static int namelen
= 0;
1203 int len
= strlen(file
);
1207 combine
= nasm_malloc(strlen(prefix
) + 1 + len
+ 1);
1208 strcpy(combine
, prefix
);
1211 strcat(combine
, file
);
1212 fp
= fopen(combine
, "r");
1213 if (pass
== 0 && fp
)
1215 namelen
+= strlen(combine
) + 1;
1221 printf(" %s", combine
);
1232 error(ERR_FATAL
, "unable to open include file `%s'", file
);
1233 return NULL
; /* never reached - placate compilers */
1237 * Determine if we should warn on defining a single-line macro of
1238 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1239 * return TRUE if _any_ single-line macro of that name is defined.
1240 * Otherwise, will return TRUE if a single-line macro with either
1241 * `nparam' or no parameters is defined.
1243 * If a macro with precisely the right number of parameters is
1244 * defined, or nparam is -1, the address of the definition structure
1245 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1246 * is NULL, no action will be taken regarding its contents, and no
1249 * Note that this is also called with nparam zero to resolve
1252 * If you already know which context macro belongs to, you can pass
1253 * the context pointer as first parameter; if you won't but name begins
1254 * with %$ the context will be automatically computed. If all_contexts
1255 * is true, macro will be searched in outer contexts as well.
1258 smacro_defined(Context
* ctx
, char *name
, int nparam
, SMacro
** defn
,
1265 else if (name
[0] == '%' && name
[1] == '$')
1268 ctx
= get_ctx(name
, FALSE
);
1270 return FALSE
; /* got to return _something_ */
1274 m
= smacros
[hash(name
)];
1278 if (!mstrcmp(m
->name
, name
, m
->casesense
&& nocase
) &&
1279 (nparam
<= 0 || m
->nparam
== 0 || nparam
== m
->nparam
))
1283 if (nparam
== m
->nparam
|| nparam
== -1)
1297 * Count and mark off the parameters in a multi-line macro call.
1298 * This is called both from within the multi-line macro expansion
1299 * code, and also to mark off the default parameters when provided
1300 * in a %macro definition line.
1303 count_mmac_params(Token
* t
, int *nparam
, Token
*** params
)
1305 int paramsize
, brace
;
1307 *nparam
= paramsize
= 0;
1311 if (*nparam
>= paramsize
)
1313 paramsize
+= PARAM_DELTA
;
1314 *params
= nasm_realloc(*params
, sizeof(**params
) * paramsize
);
1318 if (tok_is_(t
, "{"))
1320 (*params
)[(*nparam
)++] = t
;
1321 while (tok_isnt_(t
, brace
? "}" : ","))
1324 { /* got a comma/brace */
1329 * Now we've found the closing brace, look further
1333 if (tok_isnt_(t
, ","))
1336 "braces do not enclose all of macro parameter");
1337 while (tok_isnt_(t
, ","))
1341 t
= t
->next
; /* eat the comma */
1348 * Determine whether one of the various `if' conditions is true or
1351 * We must free the tline we get passed.
1354 if_condition(Token
* tline
, int i
)
1357 Token
*t
, *tt
, **tptr
, *origline
;
1358 struct tokenval tokval
;
1369 j
= FALSE
; /* have we matched yet? */
1370 while (cstk
&& tline
)
1373 if (!tline
|| tline
->type
!= TOK_ID
)
1376 "`%s' expects context identifiers",
1378 free_tlist(origline
);
1381 if (!nasm_stricmp(tline
->text
, cstk
->name
))
1383 tline
= tline
->next
;
1385 if (i
== PP_IFNCTX
|| i
== PP_ELIFNCTX
)
1387 free_tlist(origline
);
1394 j
= FALSE
; /* have we matched yet? */
1398 if (!tline
|| (tline
->type
!= TOK_ID
&&
1399 (tline
->type
!= TOK_PREPROC_ID
||
1400 tline
->text
[1] != '$')))
1403 "`%%if%sdef' expects macro identifiers",
1404 (i
== PP_ELIFNDEF
? "n" : ""));
1405 free_tlist(origline
);
1408 if (smacro_defined(NULL
, tline
->text
, 0, NULL
, 1))
1410 tline
= tline
->next
;
1412 if (i
== PP_IFNDEF
|| i
== PP_ELIFNDEF
)
1414 free_tlist(origline
);
1425 tline
= expand_smacro(tline
);
1427 while (tok_isnt_(tt
, ","))
1432 "`%s' expects two comma-separated arguments",
1438 casesense
= (i
== PP_IFIDN
|| i
== PP_ELIFIDN
||
1439 i
== PP_IFNIDN
|| i
== PP_ELIFNIDN
);
1440 j
= TRUE
; /* assume equality unless proved not */
1441 while ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) && tt
)
1443 if (tt
->type
== TOK_OTHER
&& !strcmp(tt
->text
, ","))
1445 error(ERR_NONFATAL
, "`%s': more than one comma on line",
1450 if (t
->type
== TOK_WHITESPACE
)
1455 else if (tt
->type
== TOK_WHITESPACE
)
1460 else if (tt
->type
!= t
->type
||
1461 mstrcmp(tt
->text
, t
->text
, casesense
))
1463 j
= FALSE
; /* found mismatching tokens */
1473 if ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) || tt
)
1474 j
= FALSE
; /* trailing gunk on one end or other */
1475 if (i
== PP_IFNIDN
|| i
== PP_ELIFNIDN
||
1476 i
== PP_IFNIDNI
|| i
== PP_ELIFNIDNI
)
1493 tline
= expand_smacro(tline
);
1495 while (tok_type_(t
, TOK_WHITESPACE
))
1497 j
= FALSE
; /* placate optimiser */
1505 j
= (t
->type
== TOK_ID
);
1511 j
= (t
->type
== TOK_NUMBER
);
1517 j
= (t
->type
== TOK_STRING
);
1520 if (i
== PP_IFNID
|| i
== PP_ELIFNID
||
1521 i
== PP_IFNNUM
|| i
== PP_ELIFNNUM
||
1522 i
== PP_IFNSTR
|| i
== PP_ELIFNSTR
)
1529 t
= tline
= expand_smacro(tline
);
1531 tokval
.t_type
= TOKEN_INVALID
;
1532 evalresult
= evaluate(ppscan
, tptr
, &tokval
,
1533 NULL
, pass
| CRITICAL
, error
, NULL
);
1539 "trailing garbage after expression ignored");
1540 if (!is_simple(evalresult
))
1543 "non-constant value given to `%s'", directives
[i
]);
1546 return reloc_value(evalresult
) != 0;
1550 "preprocessor directive `%s' not yet implemented",
1552 free_tlist(origline
);
1553 return -1; /* yeah, right */
1558 * Expand macros in a string. Used in %error and %include directives.
1559 * First tokenise the string, apply "expand_smacro" and then de-tokenise back.
1560 * The returned variable should ALWAYS be freed after usage.
1563 expand_macros_in_string(char **p
)
1565 Token
*line
= tokenise(*p
);
1566 line
= expand_smacro(line
);
1567 *p
= detoken(line
, FALSE
);
1571 * Find out if a line contains a preprocessor directive, and deal
1574 * If a directive _is_ found, we are expected to free_tlist() the
1577 * Return values go like this:
1579 * bit 0 is set if a directive was found (so the line gets freed)
1582 do_directive(Token
* tline
)
1584 int i
, j
, k
, m
, nparam
, nolist
;
1590 SMacro
*smac
, **smhead
;
1592 Token
*t
, *tt
, *param_start
, *macro_start
, *last
, **tptr
, *origline
;
1594 struct tokenval tokval
;
1596 MMacro
*tmp_defining
; /* Used when manipulating rep_nest */
1601 if (!tok_type_(tline
, TOK_PREPROC_ID
) ||
1602 (tline
->text
[1] == '%' || tline
->text
[1] == '$'
1603 || tline
->text
[1] == '!'))
1607 j
= sizeof(directives
) / sizeof(*directives
);
1611 m
= nasm_stricmp(tline
->text
, directives
[k
]);
1613 if (tasm_compatible_mode
) {
1616 } else if (k
!= PP_ARG
&& k
!= PP_LOCAL
&& k
!= PP_STACKSIZE
) {
1630 * If we're in a non-emitting branch of a condition construct,
1631 * or walking to the end of an already terminated %rep block,
1632 * we should ignore all directives except for condition
1635 if (((istk
->conds
&& !emitting(istk
->conds
->state
)) ||
1636 (istk
->mstk
&& !istk
->mstk
->in_progress
)) &&
1637 i
!= PP_IF
&& i
!= PP_ELIF
&&
1638 i
!= PP_IFCTX
&& i
!= PP_ELIFCTX
&&
1639 i
!= PP_IFDEF
&& i
!= PP_ELIFDEF
&&
1640 i
!= PP_IFID
&& i
!= PP_ELIFID
&&
1641 i
!= PP_IFIDN
&& i
!= PP_ELIFIDN
&&
1642 i
!= PP_IFIDNI
&& i
!= PP_ELIFIDNI
&&
1643 i
!= PP_IFNCTX
&& i
!= PP_ELIFNCTX
&&
1644 i
!= PP_IFNDEF
&& i
!= PP_ELIFNDEF
&&
1645 i
!= PP_IFNID
&& i
!= PP_ELIFNID
&&
1646 i
!= PP_IFNIDN
&& i
!= PP_ELIFNIDN
&&
1647 i
!= PP_IFNIDNI
&& i
!= PP_ELIFNIDNI
&&
1648 i
!= PP_IFNNUM
&& i
!= PP_ELIFNNUM
&&
1649 i
!= PP_IFNSTR
&& i
!= PP_ELIFNSTR
&&
1650 i
!= PP_IFNUM
&& i
!= PP_ELIFNUM
&&
1651 i
!= PP_IFSTR
&& i
!= PP_ELIFSTR
&& i
!= PP_ELSE
&& i
!= PP_ENDIF
)
1657 * If we're defining a macro or reading a %rep block, we should
1658 * ignore all directives except for %macro/%imacro (which
1659 * generate an error), %endm/%endmacro, and (only if we're in a
1660 * %rep block) %endrep. If we're in a %rep block, another %rep
1661 * causes an error, so should be let through.
1663 if (defining
&& i
!= PP_MACRO
&& i
!= PP_IMACRO
&&
1664 i
!= PP_ENDMACRO
&& i
!= PP_ENDM
&&
1665 (defining
->name
|| (i
!= PP_ENDREP
&& i
!= PP_REP
)))
1672 error(ERR_NONFATAL
, "unknown preprocessor directive `%s'",
1674 return 0; /* didn't get it */
1680 /* Directive to tell NASM what the default stack size is. The
1681 * default is for a 16-bit stack, and this can be overriden with
1683 * the following form:
1685 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1687 tline
= tline
->next
;
1688 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1689 tline
= tline
->next
;
1690 if (!tline
|| tline
->type
!= TOK_ID
)
1692 error(ERR_NONFATAL
, "`%%stacksize' missing size parameter");
1693 free_tlist(origline
);
1696 if (nasm_stricmp(tline
->text
, "flat") == 0)
1698 /* All subsequent ARG directives are for a 32-bit stack */
1700 StackPointer
= "ebp";
1704 else if (nasm_stricmp(tline
->text
, "large") == 0)
1706 /* All subsequent ARG directives are for a 16-bit stack,
1707 * far function call.
1710 StackPointer
= "bp";
1714 else if (nasm_stricmp(tline
->text
, "small") == 0)
1716 /* All subsequent ARG directives are for a 16-bit stack,
1717 * far function call. We don't support near functions.
1720 StackPointer
= "bp";
1726 error(ERR_NONFATAL
, "`%%stacksize' invalid size type");
1727 free_tlist(origline
);
1730 free_tlist(origline
);
1734 /* TASM like ARG directive to define arguments to functions, in
1735 * the following form:
1737 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1742 char *arg
, directive
[256];
1743 int size
= StackSize
;
1745 /* Find the argument name */
1746 tline
= tline
->next
;
1747 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1748 tline
= tline
->next
;
1749 if (!tline
|| tline
->type
!= TOK_ID
)
1751 error(ERR_NONFATAL
, "`%%arg' missing argument parameter");
1752 free_tlist(origline
);
1757 /* Find the argument size type */
1758 tline
= tline
->next
;
1759 if (!tline
|| tline
->type
!= TOK_OTHER
1760 || tline
->text
[0] != ':')
1763 "Syntax error processing `%%arg' directive");
1764 free_tlist(origline
);
1767 tline
= tline
->next
;
1768 if (!tline
|| tline
->type
!= TOK_ID
)
1771 "`%%arg' missing size type parameter");
1772 free_tlist(origline
);
1776 /* Allow macro expansion of type parameter */
1777 tt
= tokenise(tline
->text
);
1778 tt
= expand_smacro(tt
);
1779 if (nasm_stricmp(tt
->text
, "byte") == 0)
1781 size
= MAX(StackSize
, 1);
1783 else if (nasm_stricmp(tt
->text
, "word") == 0)
1785 size
= MAX(StackSize
, 2);
1787 else if (nasm_stricmp(tt
->text
, "dword") == 0)
1789 size
= MAX(StackSize
, 4);
1791 else if (nasm_stricmp(tt
->text
, "qword") == 0)
1793 size
= MAX(StackSize
, 8);
1795 else if (nasm_stricmp(tt
->text
, "tword") == 0)
1797 size
= MAX(StackSize
, 10);
1802 "Invalid size type for `%%arg' missing directive");
1804 free_tlist(origline
);
1809 /* Now define the macro for the argument */
1810 sprintf(directive
, "%%define %s (%s+%d)", arg
, StackPointer
,
1812 do_directive(tokenise(directive
));
1815 /* Move to the next argument in the list */
1816 tline
= tline
->next
;
1817 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1818 tline
= tline
->next
;
1820 while (tline
&& tline
->type
== TOK_OTHER
1821 && tline
->text
[0] == ',');
1822 free_tlist(origline
);
1826 /* TASM like LOCAL directive to define local variables for a
1827 * function, in the following form:
1829 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
1831 * The '= LocalSize' at the end is ignored by NASM, but is
1832 * required by TASM to define the local parameter size (and used
1833 * by the TASM macro package).
1835 offset
= LocalOffset
;
1838 char *local
, directive
[256];
1839 int size
= StackSize
;
1841 /* Find the argument name */
1842 tline
= tline
->next
;
1843 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1844 tline
= tline
->next
;
1845 if (!tline
|| tline
->type
!= TOK_ID
)
1848 "`%%local' missing argument parameter");
1849 free_tlist(origline
);
1852 local
= tline
->text
;
1854 /* Find the argument size type */
1855 tline
= tline
->next
;
1856 if (!tline
|| tline
->type
!= TOK_OTHER
1857 || tline
->text
[0] != ':')
1860 "Syntax error processing `%%local' directive");
1861 free_tlist(origline
);
1864 tline
= tline
->next
;
1865 if (!tline
|| tline
->type
!= TOK_ID
)
1868 "`%%local' missing size type parameter");
1869 free_tlist(origline
);
1873 /* Allow macro expansion of type parameter */
1874 tt
= tokenise(tline
->text
);
1875 tt
= expand_smacro(tt
);
1876 if (nasm_stricmp(tt
->text
, "byte") == 0)
1878 size
= MAX(StackSize
, 1);
1880 else if (nasm_stricmp(tt
->text
, "word") == 0)
1882 size
= MAX(StackSize
, 2);
1884 else if (nasm_stricmp(tt
->text
, "dword") == 0)
1886 size
= MAX(StackSize
, 4);
1888 else if (nasm_stricmp(tt
->text
, "qword") == 0)
1890 size
= MAX(StackSize
, 8);
1892 else if (nasm_stricmp(tt
->text
, "tword") == 0)
1894 size
= MAX(StackSize
, 10);
1899 "Invalid size type for `%%local' missing directive");
1901 free_tlist(origline
);
1906 /* Now define the macro for the argument */
1907 sprintf(directive
, "%%define %s (%s-%d)", local
, StackPointer
,
1909 do_directive(tokenise(directive
));
1912 /* Now define the assign to setup the enter_c macro correctly */
1913 sprintf(directive
, "%%assign %%$localsize %%$localsize+%d",
1915 do_directive(tokenise(directive
));
1917 /* Move to the next argument in the list */
1918 tline
= tline
->next
;
1919 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1920 tline
= tline
->next
;
1922 while (tline
&& tline
->type
== TOK_OTHER
1923 && tline
->text
[0] == ',');
1924 free_tlist(origline
);
1930 "trailing garbage after `%%clear' ignored");
1931 for (j
= 0; j
< NHASH
; j
++)
1935 MMacro
*m
= mmacros
[j
];
1936 mmacros
[j
] = m
->next
;
1941 SMacro
*s
= smacros
[j
];
1942 smacros
[j
] = smacros
[j
]->next
;
1944 free_tlist(s
->expansion
);
1948 free_tlist(origline
);
1952 tline
= tline
->next
;
1954 if (!tline
|| (tline
->type
!= TOK_STRING
&&
1955 tline
->type
!= TOK_INTERNAL_STRING
))
1957 error(ERR_NONFATAL
, "`%%include' expects a file name");
1958 free_tlist(origline
);
1959 return 3; /* but we did _something_ */
1963 "trailing garbage after `%%include' ignored");
1964 if (tline
->type
!= TOK_INTERNAL_STRING
)
1966 p
= tline
->text
+ 1; /* point past the quote to the name */
1967 p
[strlen(p
) - 1] = '\0'; /* remove the trailing quote */
1970 p
= tline
->text
; /* internal_string is easier */
1971 expand_macros_in_string(&p
);
1972 inc
= nasm_malloc(sizeof(Include
));
1975 inc
->fp
= inc_fopen(p
);
1976 inc
->fname
= src_set_fname(p
);
1977 inc
->lineno
= src_set_linnum(0);
1979 inc
->expansion
= NULL
;
1982 list
->uplevel(LIST_INCLUDE
);
1983 free_tlist(origline
);
1987 tline
= tline
->next
;
1989 tline
= expand_id(tline
);
1990 if (!tok_type_(tline
, TOK_ID
))
1992 error(ERR_NONFATAL
, "`%%push' expects a context identifier");
1993 free_tlist(origline
);
1994 return 3; /* but we did _something_ */
1997 error(ERR_WARNING
, "trailing garbage after `%%push' ignored");
1998 ctx
= nasm_malloc(sizeof(Context
));
2000 ctx
->localmac
= NULL
;
2001 ctx
->name
= nasm_strdup(tline
->text
);
2002 ctx
->number
= unique
++;
2004 free_tlist(origline
);
2008 tline
= tline
->next
;
2010 tline
= expand_id(tline
);
2011 if (!tok_type_(tline
, TOK_ID
))
2013 error(ERR_NONFATAL
, "`%%repl' expects a context identifier");
2014 free_tlist(origline
);
2015 return 3; /* but we did _something_ */
2018 error(ERR_WARNING
, "trailing garbage after `%%repl' ignored");
2020 error(ERR_NONFATAL
, "`%%repl': context stack is empty");
2023 nasm_free(cstk
->name
);
2024 cstk
->name
= nasm_strdup(tline
->text
);
2026 free_tlist(origline
);
2031 error(ERR_WARNING
, "trailing garbage after `%%pop' ignored");
2034 "`%%pop': context stack is already empty");
2037 free_tlist(origline
);
2041 tline
->next
= expand_smacro(tline
->next
);
2042 tline
= tline
->next
;
2044 if (tok_type_(tline
, TOK_STRING
))
2046 p
= tline
->text
+ 1; /* point past the quote to the name */
2047 p
[strlen(p
) - 1] = '\0'; /* remove the trailing quote */
2048 expand_macros_in_string(&p
);
2049 error(ERR_NONFATAL
, "%s", p
);
2054 p
= detoken(tline
, FALSE
);
2055 error(ERR_WARNING
, "%s", p
);
2058 free_tlist(origline
);
2076 if (istk
->conds
&& !emitting(istk
->conds
->state
))
2080 j
= if_condition(tline
->next
, i
);
2081 tline
->next
= NULL
; /* it got freed */
2082 free_tlist(origline
);
2083 j
= j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2085 cond
= nasm_malloc(sizeof(Cond
));
2086 cond
->next
= istk
->conds
;
2089 return (j
== COND_IF_TRUE
? 3 : 1);
2107 error(ERR_FATAL
, "`%s': no matching `%%if'", directives
[i
]);
2108 if (emitting(istk
->conds
->state
)
2109 || istk
->conds
->state
== COND_NEVER
)
2110 istk
->conds
->state
= COND_NEVER
;
2113 j
= if_condition(expand_mmac_params(tline
->next
), i
);
2114 tline
->next
= NULL
; /* it got freed */
2115 free_tlist(origline
);
2116 istk
->conds
->state
=
2117 j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2119 return (istk
->conds
->state
== COND_IF_TRUE
? 5 : 1);
2123 error(ERR_WARNING
, "trailing garbage after `%%else' ignored");
2125 error(ERR_FATAL
, "`%%else': no matching `%%if'");
2126 if (emitting(istk
->conds
->state
)
2127 || istk
->conds
->state
== COND_NEVER
)
2128 istk
->conds
->state
= COND_ELSE_FALSE
;
2130 istk
->conds
->state
= COND_ELSE_TRUE
;
2131 free_tlist(origline
);
2137 "trailing garbage after `%%endif' ignored");
2139 error(ERR_FATAL
, "`%%endif': no matching `%%if'");
2141 istk
->conds
= cond
->next
;
2143 free_tlist(origline
);
2150 "`%%%smacro': already defining a macro",
2151 (i
== PP_IMACRO
? "i" : ""));
2152 tline
= tline
->next
;
2154 tline
= expand_id(tline
);
2155 if (!tok_type_(tline
, TOK_ID
))
2158 "`%%%smacro' expects a macro name",
2159 (i
== PP_IMACRO
? "i" : ""));
2162 defining
= nasm_malloc(sizeof(MMacro
));
2163 defining
->name
= nasm_strdup(tline
->text
);
2164 defining
->casesense
= (i
== PP_MACRO
);
2165 defining
->plus
= FALSE
;
2166 defining
->nolist
= FALSE
;
2167 defining
->in_progress
= FALSE
;
2168 defining
->rep_nest
= NULL
;
2169 tline
= expand_smacro(tline
->next
);
2171 if (!tok_type_(tline
, TOK_NUMBER
))
2174 "`%%%smacro' expects a parameter count",
2175 (i
== PP_IMACRO
? "i" : ""));
2176 defining
->nparam_min
= defining
->nparam_max
= 0;
2180 defining
->nparam_min
= defining
->nparam_max
=
2181 readnum(tline
->text
, &j
);
2184 "unable to parse parameter count `%s'",
2187 if (tline
&& tok_is_(tline
->next
, "-"))
2189 tline
= tline
->next
->next
;
2190 if (tok_is_(tline
, "*"))
2191 defining
->nparam_max
= INT_MAX
;
2192 else if (!tok_type_(tline
, TOK_NUMBER
))
2194 "`%%%smacro' expects a parameter count after `-'",
2195 (i
== PP_IMACRO
? "i" : ""));
2198 defining
->nparam_max
= readnum(tline
->text
, &j
);
2201 "unable to parse parameter count `%s'",
2203 if (defining
->nparam_min
> defining
->nparam_max
)
2205 "minimum parameter count exceeds maximum");
2208 if (tline
&& tok_is_(tline
->next
, "+"))
2210 tline
= tline
->next
;
2211 defining
->plus
= TRUE
;
2213 if (tline
&& tok_type_(tline
->next
, TOK_ID
) &&
2214 !nasm_stricmp(tline
->next
->text
, ".nolist"))
2216 tline
= tline
->next
;
2217 defining
->nolist
= TRUE
;
2219 mmac
= mmacros
[hash(defining
->name
)];
2222 if (!strcmp(mmac
->name
, defining
->name
) &&
2223 (mmac
->nparam_min
<= defining
->nparam_max
2225 && (defining
->nparam_min
<= mmac
->nparam_max
2229 "redefining multi-line macro `%s'",
2236 * Handle default parameters.
2238 if (tline
&& tline
->next
)
2240 defining
->dlist
= tline
->next
;
2242 count_mmac_params(defining
->dlist
, &defining
->ndefs
,
2243 &defining
->defaults
);
2247 defining
->dlist
= NULL
;
2248 defining
->defaults
= NULL
;
2250 defining
->expansion
= NULL
;
2251 free_tlist(origline
);
2258 error(ERR_NONFATAL
, "`%s': not defining a macro",
2262 k
= hash(defining
->name
);
2263 defining
->next
= mmacros
[k
];
2264 mmacros
[k
] = defining
;
2266 free_tlist(origline
);
2270 if (tline
->next
&& tline
->next
->type
== TOK_WHITESPACE
)
2271 tline
= tline
->next
;
2272 t
= expand_smacro(tline
->next
);
2274 free_tlist(origline
);
2277 tokval
.t_type
= TOKEN_INVALID
;
2279 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2285 "trailing garbage after expression ignored");
2286 if (!is_simple(evalresult
))
2288 error(ERR_NONFATAL
, "non-constant value given to `%%rotate'");
2292 while (mmac
&& !mmac
->name
) /* avoid mistaking %reps for macros */
2293 mmac
= mmac
->next_active
;
2296 "`%%rotate' invoked outside a macro call");
2297 mmac
->rotate
= mmac
->rotate
+ reloc_value(evalresult
);
2298 if (mmac
->rotate
< 0)
2299 mmac
->rotate
= mmac
->nparam
- (-mmac
->rotate
) % mmac
->nparam
;
2300 mmac
->rotate
%= mmac
->nparam
;
2305 tline
= tline
->next
;
2306 if (tline
->next
&& tline
->next
->type
== TOK_WHITESPACE
)
2307 tline
= tline
->next
;
2308 if (tline
->next
&& tline
->next
->type
== TOK_ID
&&
2309 !nasm_stricmp(tline
->next
->text
, ".nolist"))
2311 tline
= tline
->next
;
2314 t
= expand_smacro(tline
->next
);
2316 free_tlist(origline
);
2319 tokval
.t_type
= TOKEN_INVALID
;
2321 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2327 "trailing garbage after expression ignored");
2328 if (!is_simple(evalresult
))
2330 error(ERR_NONFATAL
, "non-constant value given to `%%rep'");
2333 tmp_defining
= defining
;
2334 defining
= nasm_malloc(sizeof(MMacro
));
2335 defining
->name
= NULL
; /* flags this macro as a %rep block */
2336 defining
->casesense
= 0;
2337 defining
->plus
= FALSE
;
2338 defining
->nolist
= nolist
;
2339 defining
->in_progress
= reloc_value(evalresult
) + 1;
2340 defining
->nparam_min
= defining
->nparam_max
= 0;
2341 defining
->defaults
= NULL
;
2342 defining
->dlist
= NULL
;
2343 defining
->expansion
= NULL
;
2344 defining
->next_active
= istk
->mstk
;
2345 defining
->rep_nest
= tmp_defining
;
2349 if (!defining
|| defining
->name
)
2351 error(ERR_NONFATAL
, "`%%endrep': no matching `%%rep'");
2356 * Now we have a "macro" defined - although it has no name
2357 * and we won't be entering it in the hash tables - we must
2358 * push a macro-end marker for it on to istk->expansion.
2359 * After that, it will take care of propagating itself (a
2360 * macro-end marker line for a macro which is really a %rep
2361 * block will cause the macro to be re-expanded, complete
2362 * with another macro-end marker to ensure the process
2363 * continues) until the whole expansion is forcibly removed
2364 * from istk->expansion by a %exitrep.
2366 l
= nasm_malloc(sizeof(Line
));
2367 l
->next
= istk
->expansion
;
2368 l
->finishes
= defining
;
2370 istk
->expansion
= l
;
2372 istk
->mstk
= defining
;
2374 list
->uplevel(defining
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
2375 tmp_defining
= defining
;
2376 defining
= defining
->rep_nest
;
2377 free_tlist(origline
);
2382 * We must search along istk->expansion until we hit a
2383 * macro-end marker for a macro with no name. Then we set
2384 * its `in_progress' flag to 0.
2386 for (l
= istk
->expansion
; l
; l
= l
->next
)
2387 if (l
->finishes
&& !l
->finishes
->name
)
2391 l
->finishes
->in_progress
= 0;
2393 error(ERR_NONFATAL
, "`%%exitrep' not within `%%rep' block");
2394 free_tlist(origline
);
2401 tline
= tline
->next
;
2403 tline
= expand_id(tline
);
2404 if (!tline
|| (tline
->type
!= TOK_ID
&&
2405 (tline
->type
!= TOK_PREPROC_ID
||
2406 tline
->text
[1] != '$')))
2409 "`%%%s%sdefine' expects a macro identifier",
2410 ((i
== PP_IDEFINE
|| i
== PP_IXDEFINE
) ? "i" : ""),
2411 ((i
== PP_XDEFINE
|| i
== PP_IXDEFINE
) ? "x" : ""));
2412 free_tlist(origline
);
2416 ctx
= get_ctx(tline
->text
, FALSE
);
2418 smhead
= &smacros
[hash(tline
->text
)];
2420 smhead
= &ctx
->localmac
;
2421 mname
= tline
->text
;
2423 param_start
= tline
= tline
->next
;
2426 /* Expand the macro definition now for %xdefine and %ixdefine */
2427 if ((i
== PP_XDEFINE
) || (i
== PP_IXDEFINE
))
2428 tline
= expand_smacro(tline
);
2430 if (tok_is_(tline
, "("))
2433 * This macro has parameters.
2436 tline
= tline
->next
;
2442 error(ERR_NONFATAL
, "parameter identifier expected");
2443 free_tlist(origline
);
2446 if (tline
->type
!= TOK_ID
)
2449 "`%s': parameter identifier expected",
2451 free_tlist(origline
);
2454 tline
->type
= TOK_SMAC_PARAM
+ nparam
++;
2455 tline
= tline
->next
;
2457 if (tok_is_(tline
, ","))
2459 tline
= tline
->next
;
2462 if (!tok_is_(tline
, ")"))
2465 "`)' expected to terminate macro template");
2466 free_tlist(origline
);
2472 tline
= tline
->next
;
2474 if (tok_type_(tline
, TOK_WHITESPACE
))
2475 last
= tline
, tline
= tline
->next
;
2481 if (t
->type
== TOK_ID
)
2483 for (tt
= param_start
; tt
; tt
= tt
->next
)
2484 if (tt
->type
>= TOK_SMAC_PARAM
&&
2485 !strcmp(tt
->text
, t
->text
))
2489 t
->next
= macro_start
;
2494 * Good. We now have a macro name, a parameter count, and a
2495 * token list (in reverse order) for an expansion. We ought
2496 * to be OK just to create an SMacro, store it, and let
2497 * free_tlist have the rest of the line (which we have
2498 * carefully re-terminated after chopping off the expansion
2501 if (smacro_defined(ctx
, mname
, nparam
, &smac
, i
== PP_DEFINE
))
2506 "single-line macro `%s' defined both with and"
2507 " without parameters", mname
);
2508 free_tlist(origline
);
2509 free_tlist(macro_start
);
2515 * We're redefining, so we have to take over an
2516 * existing SMacro structure. This means freeing
2517 * what was already in it.
2519 nasm_free(smac
->name
);
2520 free_tlist(smac
->expansion
);
2525 smac
= nasm_malloc(sizeof(SMacro
));
2526 smac
->next
= *smhead
;
2529 smac
->name
= nasm_strdup(mname
);
2530 smac
->casesense
= ((i
== PP_DEFINE
) || (i
== PP_XDEFINE
));
2531 smac
->nparam
= nparam
;
2532 smac
->expansion
= macro_start
;
2533 smac
->in_progress
= FALSE
;
2534 free_tlist(origline
);
2538 tline
= tline
->next
;
2540 tline
= expand_id(tline
);
2541 if (!tline
|| (tline
->type
!= TOK_ID
&&
2542 (tline
->type
!= TOK_PREPROC_ID
||
2543 tline
->text
[1] != '$')))
2545 error(ERR_NONFATAL
, "`%%undef' expects a macro identifier");
2546 free_tlist(origline
);
2552 "trailing garbage after macro name ignored");
2555 /* Find the context that symbol belongs to */
2556 ctx
= get_ctx(tline
->text
, FALSE
);
2558 smhead
= &smacros
[hash(tline
->text
)];
2560 smhead
= &ctx
->localmac
;
2562 mname
= tline
->text
;
2567 * We now have a macro name... go hunt for it.
2569 while (smacro_defined(ctx
, mname
, -1, &smac
, 1))
2571 /* Defined, so we need to find its predecessor and nuke it */
2573 for (s
= smhead
; *s
&& *s
!= smac
; s
= &(*s
)->next
);
2577 nasm_free(smac
->name
);
2578 free_tlist(smac
->expansion
);
2582 free_tlist(origline
);
2586 tline
= tline
->next
;
2588 tline
= expand_id(tline
);
2589 if (!tline
|| (tline
->type
!= TOK_ID
&&
2590 (tline
->type
!= TOK_PREPROC_ID
||
2591 tline
->text
[1] != '$')))
2594 "`%%strlen' expects a macro identifier as first parameter");
2595 free_tlist(origline
);
2598 ctx
= get_ctx(tline
->text
, FALSE
);
2600 smhead
= &smacros
[hash(tline
->text
)];
2602 smhead
= &ctx
->localmac
;
2603 mname
= tline
->text
;
2605 tline
= expand_smacro(tline
->next
);
2609 while (tok_type_(t
, TOK_WHITESPACE
))
2611 /* t should now point to the string */
2612 if (t
->type
!= TOK_STRING
)
2615 "`%%strlen` requires string as second parameter");
2617 free_tlist(origline
);
2621 macro_start
= nasm_malloc(sizeof(*macro_start
));
2622 macro_start
->next
= NULL
;
2623 make_tok_num(macro_start
, strlen(t
->text
) - 2);
2624 macro_start
->mac
= NULL
;
2627 * We now have a macro name, an implicit parameter count of
2628 * zero, and a numeric token to use as an expansion. Create
2629 * and store an SMacro.
2631 if (smacro_defined(ctx
, mname
, 0, &smac
, i
== PP_STRLEN
))
2635 "single-line macro `%s' defined both with and"
2636 " without parameters", mname
);
2640 * We're redefining, so we have to take over an
2641 * existing SMacro structure. This means freeing
2642 * what was already in it.
2644 nasm_free(smac
->name
);
2645 free_tlist(smac
->expansion
);
2650 smac
= nasm_malloc(sizeof(SMacro
));
2651 smac
->next
= *smhead
;
2654 smac
->name
= nasm_strdup(mname
);
2655 smac
->casesense
= (i
== PP_STRLEN
);
2657 smac
->expansion
= macro_start
;
2658 smac
->in_progress
= FALSE
;
2660 free_tlist(origline
);
2664 tline
= tline
->next
;
2666 tline
= expand_id(tline
);
2667 if (!tline
|| (tline
->type
!= TOK_ID
&&
2668 (tline
->type
!= TOK_PREPROC_ID
||
2669 tline
->text
[1] != '$')))
2672 "`%%substr' expects a macro identifier as first parameter");
2673 free_tlist(origline
);
2676 ctx
= get_ctx(tline
->text
, FALSE
);
2678 smhead
= &smacros
[hash(tline
->text
)];
2680 smhead
= &ctx
->localmac
;
2681 mname
= tline
->text
;
2683 tline
= expand_smacro(tline
->next
);
2687 while (tok_type_(t
, TOK_WHITESPACE
))
2690 /* t should now point to the string */
2691 if (t
->type
!= TOK_STRING
)
2694 "`%%substr` requires string as second parameter");
2696 free_tlist(origline
);
2702 tokval
.t_type
= TOKEN_INVALID
;
2704 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2708 free_tlist(origline
);
2711 if (!is_simple(evalresult
))
2713 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
2715 free_tlist(origline
);
2719 macro_start
= nasm_malloc(sizeof(*macro_start
));
2720 macro_start
->next
= NULL
;
2721 macro_start
->text
= nasm_strdup("'''");
2722 if (evalresult
->value
> 0
2723 && evalresult
->value
< strlen(t
->text
) - 1)
2725 macro_start
->text
[1] = t
->text
[evalresult
->value
];
2729 macro_start
->text
[2] = '\0';
2731 macro_start
->type
= TOK_STRING
;
2732 macro_start
->mac
= NULL
;
2735 * We now have a macro name, an implicit parameter count of
2736 * zero, and a numeric token to use as an expansion. Create
2737 * and store an SMacro.
2739 if (smacro_defined(ctx
, mname
, 0, &smac
, i
== PP_SUBSTR
))
2743 "single-line macro `%s' defined both with and"
2744 " without parameters", mname
);
2748 * We're redefining, so we have to take over an
2749 * existing SMacro structure. This means freeing
2750 * what was already in it.
2752 nasm_free(smac
->name
);
2753 free_tlist(smac
->expansion
);
2758 smac
= nasm_malloc(sizeof(SMacro
));
2759 smac
->next
= *smhead
;
2762 smac
->name
= nasm_strdup(mname
);
2763 smac
->casesense
= (i
== PP_SUBSTR
);
2765 smac
->expansion
= macro_start
;
2766 smac
->in_progress
= FALSE
;
2768 free_tlist(origline
);
2774 tline
= tline
->next
;
2776 tline
= expand_id(tline
);
2777 if (!tline
|| (tline
->type
!= TOK_ID
&&
2778 (tline
->type
!= TOK_PREPROC_ID
||
2779 tline
->text
[1] != '$')))
2782 "`%%%sassign' expects a macro identifier",
2783 (i
== PP_IASSIGN
? "i" : ""));
2784 free_tlist(origline
);
2787 ctx
= get_ctx(tline
->text
, FALSE
);
2789 smhead
= &smacros
[hash(tline
->text
)];
2791 smhead
= &ctx
->localmac
;
2792 mname
= tline
->text
;
2794 tline
= expand_smacro(tline
->next
);
2799 tokval
.t_type
= TOKEN_INVALID
;
2801 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2805 free_tlist(origline
);
2811 "trailing garbage after expression ignored");
2813 if (!is_simple(evalresult
))
2816 "non-constant value given to `%%%sassign'",
2817 (i
== PP_IASSIGN
? "i" : ""));
2818 free_tlist(origline
);
2822 macro_start
= nasm_malloc(sizeof(*macro_start
));
2823 macro_start
->next
= NULL
;
2824 make_tok_num(macro_start
, reloc_value(evalresult
));
2825 macro_start
->mac
= NULL
;
2828 * We now have a macro name, an implicit parameter count of
2829 * zero, and a numeric token to use as an expansion. Create
2830 * and store an SMacro.
2832 if (smacro_defined(ctx
, mname
, 0, &smac
, i
== PP_ASSIGN
))
2836 "single-line macro `%s' defined both with and"
2837 " without parameters", mname
);
2841 * We're redefining, so we have to take over an
2842 * existing SMacro structure. This means freeing
2843 * what was already in it.
2845 nasm_free(smac
->name
);
2846 free_tlist(smac
->expansion
);
2851 smac
= nasm_malloc(sizeof(SMacro
));
2852 smac
->next
= *smhead
;
2855 smac
->name
= nasm_strdup(mname
);
2856 smac
->casesense
= (i
== PP_ASSIGN
);
2858 smac
->expansion
= macro_start
;
2859 smac
->in_progress
= FALSE
;
2860 free_tlist(origline
);
2865 * Syntax is `%line nnn[+mmm] [filename]'
2867 tline
= tline
->next
;
2869 if (!tok_type_(tline
, TOK_NUMBER
))
2871 error(ERR_NONFATAL
, "`%%line' expects line number");
2872 free_tlist(origline
);
2875 k
= readnum(tline
->text
, &j
);
2877 tline
= tline
->next
;
2878 if (tok_is_(tline
, "+"))
2880 tline
= tline
->next
;
2881 if (!tok_type_(tline
, TOK_NUMBER
))
2883 error(ERR_NONFATAL
, "`%%line' expects line increment");
2884 free_tlist(origline
);
2887 m
= readnum(tline
->text
, &j
);
2888 tline
= tline
->next
;
2895 nasm_free(src_set_fname(detoken(tline
, FALSE
)));
2897 free_tlist(origline
);
2902 "preprocessor directive `%s' not yet implemented",
2910 * Ensure that a macro parameter contains a condition code and
2911 * nothing else. Return the condition code index if so, or -1
2921 if (t
->type
!= TOK_ID
)
2925 if (tt
&& (tt
->type
!= TOK_OTHER
|| strcmp(tt
->text
, ",")))
2929 j
= sizeof(conditions
) / sizeof(*conditions
);
2933 m
= nasm_stricmp(t
->text
, conditions
[k
]);
2953 * Expand MMacro-local things: parameter references (%0, %n, %+n,
2954 * %-n) and MMacro-local identifiers (%%foo).
2957 expand_mmac_params(Token
* tline
)
2959 Token
*t
, *tt
, **tail
, *thead
;
2966 if (tline
->type
== TOK_PREPROC_ID
&&
2967 (((tline
->text
[1] == '+' || tline
->text
[1] == '-')
2968 && tline
->text
[2]) || tline
->text
[1] == '%'
2969 || (tline
->text
[1] >= '0' && tline
->text
[1] <= '9')))
2972 int type
= 0, cc
; /* type = 0 to placate optimisers */
2978 tline
= tline
->next
;
2981 while (mac
&& !mac
->name
) /* avoid mistaking %reps for macros */
2982 mac
= mac
->next_active
;
2984 error(ERR_NONFATAL
, "`%s': not in a macro call", t
->text
);
2989 * We have to make a substitution of one of the
2990 * forms %1, %-1, %+1, %%foo, %0.
2994 sprintf(tmpbuf
, "%d", mac
->nparam
);
2995 text
= nasm_strdup(tmpbuf
);
2999 sprintf(tmpbuf
, "..@%lu.", mac
->unique
);
3000 text
= nasm_strcat(tmpbuf
, t
->text
+ 2);
3003 n
= atoi(t
->text
+ 2) - 1;
3004 if (n
>= mac
->nparam
)
3008 if (mac
->nparam
> 1)
3009 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3010 tt
= mac
->params
[n
];
3016 "macro parameter %d is not a condition code",
3023 if (inverse_ccs
[cc
] == -1)
3026 "condition code `%s' is not invertible",
3032 nasm_strdup(conditions
[inverse_ccs
3037 n
= atoi(t
->text
+ 2) - 1;
3038 if (n
>= mac
->nparam
)
3042 if (mac
->nparam
> 1)
3043 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3044 tt
= mac
->params
[n
];
3050 "macro parameter %d is not a condition code",
3057 text
= nasm_strdup(conditions
[cc
]);
3061 n
= atoi(t
->text
+ 1) - 1;
3062 if (n
>= mac
->nparam
)
3066 if (mac
->nparam
> 1)
3067 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3068 tt
= mac
->params
[n
];
3072 for (i
= 0; i
< mac
->paramlen
[n
]; i
++)
3075 new_Token(NULL
, tt
->type
, tt
->text
,
3077 tail
= &(*tail
)->next
;
3081 text
= NULL
; /* we've done it here */
3102 tline
= tline
->next
;
3109 for (; t
&& (tt
= t
->next
) != NULL
; t
= t
->next
)
3112 case TOK_WHITESPACE
:
3113 if (tt
->type
== TOK_WHITESPACE
)
3115 t
->next
= delete_Token(tt
);
3119 if (tt
->type
== TOK_ID
|| tt
->type
== TOK_NUMBER
)
3121 char *tmp
= nasm_strcat(t
->text
, tt
->text
);
3124 t
->next
= delete_Token(tt
);
3128 if (tt
->type
== TOK_NUMBER
)
3130 char *tmp
= nasm_strcat(t
->text
, tt
->text
);
3133 t
->next
= delete_Token(tt
);
3142 * Expand all single-line macro calls made in the given line.
3143 * Return the expanded version of the line. The original is deemed
3144 * to be destroyed in the process. (In reality we'll just move
3145 * Tokens from input to output a lot of the time, rather than
3146 * actually bothering to destroy and replicate.)
3149 expand_smacro(Token
* tline
)
3151 Token
*t
, *tt
, *mstart
, **tail
, *thead
;
3152 SMacro
*head
= NULL
, *m
;
3155 int nparam
, sparam
, brackets
, rescan
;
3156 Token
*org_tline
= tline
;
3161 * Trick: we should avoid changing the start token pointer since it can
3162 * be contained in "next" field of other token. Because of this
3163 * we allocate a copy of first token and work with it; at the end of
3164 * routine we copy it back
3169 new_Token(org_tline
->next
, org_tline
->type
, org_tline
->text
,
3171 tline
->mac
= org_tline
->mac
;
3179 { /* main token loop */
3180 if ((mname
= tline
->text
))
3182 /* if this token is a local macro, look in local context */
3183 if (tline
->type
== TOK_ID
|| tline
->type
== TOK_PREPROC_ID
)
3184 ctx
= get_ctx(mname
, TRUE
);
3188 head
= smacros
[hash(mname
)];
3190 head
= ctx
->localmac
;
3192 * We've hit an identifier. As in is_mmacro below, we first
3193 * check whether the identifier is a single-line macro at
3194 * all, then think about checking for parameters if
3197 for (m
= head
; m
; m
= m
->next
)
3198 if (!mstrcmp(m
->name
, mname
, m
->casesense
))
3208 * Simple case: the macro is parameterless. Discard the
3209 * one token that the macro call took, and push the
3210 * expansion back on the to-do stack.
3214 if (!strcmp("__FILE__", m
->name
))
3217 src_get(&num
, &(tline
->text
));
3218 nasm_quote(&(tline
->text
));
3219 tline
->type
= TOK_STRING
;
3222 if (!strcmp("__LINE__", m
->name
))
3224 nasm_free(tline
->text
);
3225 make_tok_num(tline
, src_get_linnum());
3228 tline
= delete_Token(tline
);
3235 * Complicated case: at least one macro with this name
3236 * exists and takes parameters. We must find the
3237 * parameters in the call, count them, find the SMacro
3238 * that corresponds to that form of the macro call, and
3239 * substitute for the parameters when we expand. What a
3242 tline
= tline
->next
;
3244 if (!tok_is_(tline
, "("))
3247 * This macro wasn't called with parameters: ignore
3248 * the call. (Behaviour borrowed from gnu cpp.)
3259 tline
= tline
->next
;
3260 sparam
= PARAM_DELTA
;
3261 params
= nasm_malloc(sparam
* sizeof(Token
*));
3263 paramsize
= nasm_malloc(sparam
* sizeof(int));
3265 for (;; tline
= tline
->next
)
3266 { /* parameter loop */
3270 "macro call expects terminating `)'");
3273 if (tline
->type
== TOK_WHITESPACE
3276 if (paramsize
[nparam
])
3279 params
[nparam
] = tline
->next
;
3280 continue; /* parameter loop */
3282 if (tline
->type
== TOK_OTHER
3283 && tline
->text
[1] == 0)
3285 char ch
= tline
->text
[0];
3286 if (ch
== ',' && !paren
&& brackets
<= 0)
3288 if (++nparam
>= sparam
)
3290 sparam
+= PARAM_DELTA
;
3291 params
= nasm_realloc(params
,
3292 sparam
* sizeof(Token
*));
3293 paramsize
= nasm_realloc(paramsize
,
3294 sparam
* sizeof(int));
3296 params
[nparam
] = tline
->next
;
3297 paramsize
[nparam
] = 0;
3299 continue; /* parameter loop */
3302 (brackets
> 0 || (brackets
== 0 &&
3303 !paramsize
[nparam
])))
3307 params
[nparam
] = tline
->next
;
3308 continue; /* parameter loop */
3311 if (ch
== '}' && brackets
> 0)
3312 if (--brackets
== 0)
3315 continue; /* parameter loop */
3317 if (ch
== '(' && !brackets
)
3319 if (ch
== ')' && brackets
<= 0)
3326 error(ERR_NONFATAL
, "braces do not "
3327 "enclose all of macro parameter");
3329 paramsize
[nparam
] += white
+ 1;
3331 } /* parameter loop */
3333 while (m
&& (m
->nparam
!= nparam
||
3334 mstrcmp(m
->name
, mname
,
3338 error(ERR_WARNING
| ERR_WARN_MNP
,
3339 "macro `%s' exists, "
3340 "but not taking %d parameters",
3341 mstart
->text
, nparam
);
3344 if (m
&& m
->in_progress
)
3346 if (!m
) /* in progess or didn't find '(' or wrong nparam */
3349 * Design question: should we handle !tline, which
3350 * indicates missing ')' here, or expand those
3351 * macros anyway, which requires the (t) test a few
3355 nasm_free(paramsize
);
3361 * Expand the macro: we are placed on the last token of the
3362 * call, so that we can easily split the call from the
3363 * following tokens. We also start by pushing an SMAC_END
3364 * token for the cycle removal.
3372 tt
= new_Token(tline
, TOK_SMAC_END
, NULL
, 0);
3374 m
->in_progress
= TRUE
;
3376 for (t
= m
->expansion
; t
; t
= t
->next
)
3378 if (t
->type
>= TOK_SMAC_PARAM
)
3380 Token
*pcopy
= tline
, **ptail
= &pcopy
;
3384 ttt
= params
[t
->type
- TOK_SMAC_PARAM
];
3385 for (i
= paramsize
[t
->type
- TOK_SMAC_PARAM
];
3389 new_Token(tline
, ttt
->type
, ttt
->text
,
3398 tt
= new_Token(tline
, t
->type
, t
->text
, 0);
3404 * Having done that, get rid of the macro call, and clean
3405 * up the parameters.
3408 nasm_free(paramsize
);
3410 continue; /* main token loop */
3415 if (tline
->type
== TOK_SMAC_END
)
3417 tline
->mac
->in_progress
= FALSE
;
3418 tline
= delete_Token(tline
);
3423 tline
= tline
->next
;
3431 * Now scan the entire line and look for successive TOK_IDs that resulted
3432 * after expansion (they can't be produced by tokenise()). The successive
3433 * TOK_IDs should be concatenated.
3434 * Also we look for %+ tokens and concatenate the tokens before and after
3435 * them (without white spaces in between).
3441 while (t
&& t
->type
!= TOK_ID
&& t
->type
!= TOK_PREPROC_ID
)
3445 if (t
->next
->type
== TOK_ID
||
3446 t
->next
->type
== TOK_PREPROC_ID
||
3447 t
->next
->type
== TOK_NUMBER
)
3449 char *p
= nasm_strcat(t
->text
, t
->next
->text
);
3451 t
->next
= delete_Token(t
->next
);
3455 else if (t
->next
->type
== TOK_WHITESPACE
&& t
->next
->next
&&
3456 t
->next
->next
->type
== TOK_PREPROC_ID
&&
3457 strcmp(t
->next
->next
->text
, "%+") == 0)
3459 /* free the next whitespace, the %+ token and next whitespace */
3461 for (i
= 1; i
<= 3; i
++)
3463 if (!t
->next
|| (i
!= 2 && t
->next
->type
!= TOK_WHITESPACE
))
3465 t
->next
= delete_Token(t
->next
);
3471 /* If we concatenaded something, re-scan the line for macros */
3482 *org_tline
= *thead
;
3483 /* since we just gave text to org_line, don't free it */
3485 delete_Token(thead
);
3489 /* the expression expanded to empty line;
3490 we can't return NULL for some reasons
3491 we just set the line to a single WHITESPACE token. */
3492 memset(org_tline
, 0, sizeof(*org_tline
));
3493 org_tline
->text
= NULL
;
3494 org_tline
->type
= TOK_WHITESPACE
;
3503 * Similar to expand_smacro but used exclusively with macro identifiers
3504 * right before they are fetched in. The reason is that there can be
3505 * identifiers consisting of several subparts. We consider that if there
3506 * are more than one element forming the name, user wants a expansion,
3507 * otherwise it will be left as-is. Example:
3511 * the identifier %$abc will be left as-is so that the handler for %define
3512 * will suck it and define the corresponding value. Other case:
3514 * %define _%$abc cde
3516 * In this case user wants name to be expanded *before* %define starts
3517 * working, so we'll expand %$abc into something (if it has a value;
3518 * otherwise it will be left as-is) then concatenate all successive
3522 expand_id(Token
* tline
)
3524 Token
*cur
, *oldnext
= NULL
;
3526 if (!tline
|| !tline
->next
)
3531 (cur
->next
->type
== TOK_ID
||
3532 cur
->next
->type
== TOK_PREPROC_ID
|| cur
->next
->type
== TOK_NUMBER
))
3535 /* If identifier consists of just one token, don't expand */
3541 oldnext
= cur
->next
; /* Detach the tail past identifier */
3542 cur
->next
= NULL
; /* so that expand_smacro stops here */
3545 tline
= expand_smacro(tline
);
3549 /* expand_smacro possibly changhed tline; re-scan for EOL */
3551 while (cur
&& cur
->next
)
3554 cur
->next
= oldnext
;
3561 * Determine whether the given line constitutes a multi-line macro
3562 * call, and return the MMacro structure called if so. Doesn't have
3563 * to check for an initial label - that's taken care of in
3564 * expand_mmacro - but must check numbers of parameters. Guaranteed
3565 * to be called with tline->type == TOK_ID, so the putative macro
3566 * name is easy to find.
3569 is_mmacro(Token
* tline
, Token
*** params_array
)
3575 head
= mmacros
[hash(tline
->text
)];
3578 * Efficiency: first we see if any macro exists with the given
3579 * name. If not, we can return NULL immediately. _Then_ we
3580 * count the parameters, and then we look further along the
3581 * list if necessary to find the proper MMacro.
3583 for (m
= head
; m
; m
= m
->next
)
3584 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
3590 * OK, we have a potential macro. Count and demarcate the
3593 count_mmac_params(tline
->next
, &nparam
, ¶ms
);
3596 * So we know how many parameters we've got. Find the MMacro
3597 * structure that handles this number.
3601 if (m
->nparam_min
<= nparam
&& (m
->plus
|| nparam
<= m
->nparam_max
))
3604 * This one is right. Just check if cycle removal
3605 * prohibits us using it before we actually celebrate...
3611 "self-reference in multi-line macro `%s'", m
->name
);
3617 * It's right, and we can use it. Add its default
3618 * parameters to the end of our list if necessary.
3620 if (m
->defaults
&& nparam
< m
->nparam_min
+ m
->ndefs
)
3623 nasm_realloc(params
,
3624 ((m
->nparam_min
+ m
->ndefs
+ 1) * sizeof(*params
)));
3625 while (nparam
< m
->nparam_min
+ m
->ndefs
)
3627 params
[nparam
] = m
->defaults
[nparam
- m
->nparam_min
];
3632 * If we've gone over the maximum parameter count (and
3633 * we're in Plus mode), ignore parameters beyond
3636 if (m
->plus
&& nparam
> m
->nparam_max
)
3637 nparam
= m
->nparam_max
;
3639 * Then terminate the parameter list, and leave.
3642 { /* need this special case */
3643 params
= nasm_malloc(sizeof(*params
));
3646 params
[nparam
] = NULL
;
3647 *params_array
= params
;
3651 * This one wasn't right: look for the next one with the
3654 for (m
= m
->next
; m
; m
= m
->next
)
3655 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
3660 * After all that, we didn't find one with the right number of
3661 * parameters. Issue a warning, and fail to expand the macro.
3663 error(ERR_WARNING
| ERR_WARN_MNP
,
3664 "macro `%s' exists, but not taking %d parameters",
3665 tline
->text
, nparam
);
3671 * Expand the multi-line macro call made by the given line, if
3672 * there is one to be expanded. If there is, push the expansion on
3673 * istk->expansion and return 1. Otherwise return 0.
3676 expand_mmacro(Token
* tline
)
3678 Token
*startline
= tline
;
3679 Token
*label
= NULL
;
3680 int dont_prepend
= 0;
3681 Token
**params
, *t
, *tt
;
3684 int i
, nparam
, *paramlen
;
3688 if (!tok_type_(t
, TOK_ID
))
3690 m
= is_mmacro(t
, ¶ms
);
3695 * We have an id which isn't a macro call. We'll assume
3696 * it might be a label; we'll also check to see if a
3697 * colon follows it. Then, if there's another id after
3698 * that lot, we'll check it again for macro-hood.
3702 if (tok_type_(t
, TOK_WHITESPACE
))
3703 last
= t
, t
= t
->next
;
3704 if (tok_is_(t
, ":"))
3707 last
= t
, t
= t
->next
;
3708 if (tok_type_(t
, TOK_WHITESPACE
))
3709 last
= t
, t
= t
->next
;
3711 if (!tok_type_(t
, TOK_ID
) || (m
= is_mmacro(t
, ¶ms
)) == NULL
)
3718 * Fix up the parameters: this involves stripping leading and
3719 * trailing whitespace, then stripping braces if they are
3722 for (nparam
= 0; params
[nparam
]; nparam
++)
3724 paramlen
= nparam
? nasm_malloc(nparam
* sizeof(*paramlen
)) : NULL
;
3726 for (i
= 0; params
[i
]; i
++)
3729 int comma
= (!m
->plus
|| i
< nparam
- 1);
3733 if (tok_is_(t
, "{"))
3734 t
= t
->next
, brace
= TRUE
, comma
= FALSE
;
3739 if (comma
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, ","))
3740 break; /* ... because we have hit a comma */
3741 if (comma
&& t
->type
== TOK_WHITESPACE
&& tok_is_(t
->next
, ","))
3742 break; /* ... or a space then a comma */
3743 if (brace
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, "}"))
3744 break; /* ... or a brace */
3751 * OK, we have a MMacro structure together with a set of
3752 * parameters. We must now go through the expansion and push
3753 * copies of each Line on to istk->expansion. Substitution of
3754 * parameter tokens and macro-local tokens doesn't get done
3755 * until the single-line macro substitution process; this is
3756 * because delaying them allows us to change the semantics
3757 * later through %rotate.
3759 * First, push an end marker on to istk->expansion, mark this
3760 * macro as in progress, and set up its invocation-specific
3763 ll
= nasm_malloc(sizeof(Line
));
3764 ll
->next
= istk
->expansion
;
3767 istk
->expansion
= ll
;
3769 m
->in_progress
= TRUE
;
3774 m
->paramlen
= paramlen
;
3775 m
->unique
= unique
++;
3778 m
->next_active
= istk
->mstk
;
3781 for (l
= m
->expansion
; l
; l
= l
->next
)
3785 ll
= nasm_malloc(sizeof(Line
));
3786 ll
->finishes
= NULL
;
3787 ll
->next
= istk
->expansion
;
3788 istk
->expansion
= ll
;
3791 for (t
= l
->first
; t
; t
= t
->next
)
3794 if (t
->type
== TOK_PREPROC_ID
&&
3795 t
->text
[1] == '0' && t
->text
[2] == '0')
3802 tt
= *tail
= new_Token(NULL
, x
->type
, x
->text
, 0);
3809 * If we had a label, push it on as the first line of
3810 * the macro expansion.
3814 if (dont_prepend
< 0)
3815 free_tlist(startline
);
3818 ll
= nasm_malloc(sizeof(Line
));
3819 ll
->finishes
= NULL
;
3820 ll
->next
= istk
->expansion
;
3821 istk
->expansion
= ll
;
3822 ll
->first
= startline
;
3826 label
= label
->next
;
3827 label
->next
= tt
= new_Token(NULL
, TOK_OTHER
, ":", 0);
3832 list
->uplevel(m
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
3838 * Since preprocessor always operate only on the line that didn't
3839 * arrived yet, we should always use ERR_OFFBY1. Also since user
3840 * won't want to see same error twice (preprocessing is done once
3841 * per pass) we will want to show errors only during pass one.
3844 error(int severity
, char *fmt
, ...)
3849 /* If we're in a dead branch of IF or something like it, ignore the error */
3850 if (istk
->conds
&& !emitting(istk
->conds
->state
))
3854 vsprintf(buff
, fmt
, arg
);
3857 if (istk
->mstk
&& istk
->mstk
->name
)
3858 __error(severity
| ERR_PASS1
, "(%s:%d) %s", istk
->mstk
->name
,
3859 istk
->mstk
->lineno
, buff
);
3861 __error(severity
| ERR_PASS1
, "%s", buff
);
3865 pp_reset(char *file
, int apass
, efunc errfunc
, evalfunc eval
,
3872 istk
= nasm_malloc(sizeof(Include
));
3875 istk
->expansion
= NULL
;
3877 istk
->fp
= fopen(file
, "r");
3879 src_set_fname(nasm_strdup(file
));
3883 error(ERR_FATAL
| ERR_NOFILE
, "unable to open input file `%s'", file
);
3885 for (h
= 0; h
< NHASH
; h
++)
3891 if (tasm_compatible_mode
) {
3894 stdmacpos
= &stdmac
[TASM_MACRO_COUNT
];
3896 any_extrastdmac
= (extrastdmac
!= NULL
);
3911 * Fetch a tokenised line, either from the macro-expansion
3912 * buffer or from the input file.
3915 while (istk
->expansion
&& istk
->expansion
->finishes
)
3917 Line
*l
= istk
->expansion
;
3918 if (!l
->finishes
->name
&& l
->finishes
->in_progress
> 1)
3923 * This is a macro-end marker for a macro with no
3924 * name, which means it's not really a macro at all
3925 * but a %rep block, and the `in_progress' field is
3926 * more than 1, meaning that we still need to
3927 * repeat. (1 means the natural last repetition; 0
3928 * means termination by %exitrep.) We have
3929 * therefore expanded up to the %endrep, and must
3930 * push the whole block on to the expansion buffer
3931 * again. We don't bother to remove the macro-end
3932 * marker: we'd only have to generate another one
3935 l
->finishes
->in_progress
--;
3936 for (l
= l
->finishes
->expansion
; l
; l
= l
->next
)
3938 Token
*t
, *tt
, **tail
;
3940 ll
= nasm_malloc(sizeof(Line
));
3941 ll
->next
= istk
->expansion
;
3942 ll
->finishes
= NULL
;
3946 for (t
= l
->first
; t
; t
= t
->next
)
3948 if (t
->text
|| t
->type
== TOK_WHITESPACE
)
3950 tt
= *tail
= new_Token(NULL
, t
->type
, t
->text
, 0);
3955 istk
->expansion
= ll
;
3961 * Check whether a `%rep' was started and not ended
3962 * within this macro expansion. This can happen and
3963 * should be detected. It's a fatal error because
3964 * I'm too confused to work out how to recover
3970 error(ERR_PANIC
, "defining with name in expansion");
3971 else if (istk
->mstk
->name
)
3972 error(ERR_FATAL
, "`%%rep' without `%%endrep' within"
3973 " expansion of macro `%s'", istk
->mstk
->name
);
3977 * FIXME: investigate the relationship at this point between
3978 * istk->mstk and l->finishes
3981 MMacro
*m
= istk
->mstk
;
3982 istk
->mstk
= m
->next_active
;
3986 * This was a real macro call, not a %rep, and
3987 * therefore the parameter information needs to
3990 nasm_free(m
->params
);
3991 free_tlist(m
->iline
);
3992 nasm_free(m
->paramlen
);
3993 l
->finishes
->in_progress
= FALSE
;
3998 istk
->expansion
= l
->next
;
4000 list
->downlevel(LIST_MACRO
);
4004 { /* until we get a line we can use */
4006 if (istk
->expansion
)
4007 { /* from a macro expansion */
4009 Line
*l
= istk
->expansion
;
4011 istk
->mstk
->lineno
++;
4013 istk
->expansion
= l
->next
;
4015 p
= detoken(tline
, FALSE
);
4016 list
->line(LIST_MACRO
, p
);
4022 { /* from the current input file */
4023 line
= prepreproc(line
);
4024 tline
= tokenise(line
);
4029 * The current file has ended; work down the istk
4035 error(ERR_FATAL
, "expected `%%endif' before end of file");
4037 list
->downlevel(LIST_INCLUDE
);
4038 src_set_linnum(i
->lineno
);
4039 nasm_free(src_set_fname(i
->fname
));
4047 * We must expand MMacro parameters and MMacro-local labels
4048 * _before_ we plunge into directive processing, to cope
4049 * with things like `%define something %1' such as STRUC
4050 * uses. Unless we're _defining_ a MMacro, in which case
4051 * those tokens should be left alone to go into the
4052 * definition; and unless we're in a non-emitting
4053 * condition, in which case we don't want to meddle with
4056 if (!defining
&& !(istk
->conds
&& !emitting(istk
->conds
->state
)))
4057 tline
= expand_mmac_params(tline
);
4060 * Check the line to see if it's a preprocessor directive.
4062 if (do_directive(tline
) & 1)
4069 * We're defining a multi-line macro. We emit nothing
4071 * shove the tokenised line on to the macro definition.
4073 Line
*l
= nasm_malloc(sizeof(Line
));
4074 l
->next
= defining
->expansion
;
4076 l
->finishes
= FALSE
;
4077 defining
->expansion
= l
;
4080 else if (istk
->conds
&& !emitting(istk
->conds
->state
))
4083 * We're in a non-emitting branch of a condition block.
4084 * Emit nothing at all, not even a blank line: when we
4085 * emerge from the condition we'll give a line-number
4086 * directive so we keep our place correctly.
4091 else if (istk
->mstk
&& !istk
->mstk
->in_progress
)
4094 * We're in a %rep block which has been terminated, so
4095 * we're walking through to the %endrep without
4096 * emitting anything. Emit nothing at all, not even a
4097 * blank line: when we emerge from the %rep block we'll
4098 * give a line-number directive so we keep our place
4106 tline
= expand_smacro(tline
);
4107 if (!expand_mmacro(tline
))
4110 * De-tokenise the line again, and emit it.
4112 line
= detoken(tline
, TRUE
);
4118 continue; /* expand_mmacro calls free_tlist */
4133 error(ERR_NONFATAL
, "end of file while still defining macro `%s'",
4135 free_mmacro(defining
);
4139 for (h
= 0; h
< NHASH
; h
++)
4143 MMacro
*m
= mmacros
[h
];
4144 mmacros
[h
] = mmacros
[h
]->next
;
4149 SMacro
*s
= smacros
[h
];
4150 smacros
[h
] = smacros
[h
]->next
;
4152 free_tlist(s
->expansion
);
4161 nasm_free(i
->fname
);
4169 pp_include_path(char *path
)
4173 i
= nasm_malloc(sizeof(IncPath
));
4174 i
->path
= nasm_strdup(path
);
4180 pp_pre_include(char *fname
)
4182 Token
*inc
, *space
, *name
;
4185 name
= new_Token(NULL
, TOK_INTERNAL_STRING
, fname
, 0);
4186 space
= new_Token(name
, TOK_WHITESPACE
, NULL
, 0);
4187 inc
= new_Token(space
, TOK_PREPROC_ID
, "%include", 0);
4189 l
= nasm_malloc(sizeof(Line
));
4192 l
->finishes
= FALSE
;
4197 pp_pre_define(char *definition
)
4203 equals
= strchr(definition
, '=');
4204 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
4205 def
= new_Token(space
, TOK_PREPROC_ID
, "%define", 0);
4208 space
->next
= tokenise(definition
);
4212 l
= nasm_malloc(sizeof(Line
));
4215 l
->finishes
= FALSE
;
4220 pp_pre_undefine(char *definition
)
4225 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
4226 def
= new_Token(space
, TOK_PREPROC_ID
, "%undef", 0);
4228 l
= nasm_malloc(sizeof(Line
));
4231 l
->finishes
= FALSE
;
4236 pp_extra_stdmac(char **macros
)
4238 extrastdmac
= macros
;
4242 make_tok_num(Token
* tok
, long val
)
4245 sprintf(numbuf
, "%ld", val
);
4246 tok
->text
= nasm_strdup(numbuf
);
4247 tok
->type
= TOK_NUMBER
;