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 Blocks Blocks
;
52 typedef struct Line Line
;
53 typedef struct Include Include
;
54 typedef struct Cond Cond
;
55 typedef struct IncPath IncPath
;
58 * Store the definition of a single-line macro.
71 * Store the definition of a multi-line macro. This is also used to
72 * store the interiors of `%rep...%endrep' blocks, which are
73 * effectively self-re-invoking multi-line macros which simply
74 * don't have a name or bother to appear in the hash tables. %rep
75 * blocks are signified by having a NULL `name' field.
77 * In a MMacro describing a `%rep' block, the `in_progress' field
78 * isn't merely boolean, but gives the number of repeats left to
81 * The `next' field is used for storing MMacros in hash tables; the
82 * `next_active' field is for stacking them on istk entries.
84 * When a MMacro is being expanded, `params', `iline', `nparam',
85 * `paramlen', `rotate' and `unique' are local to the invocation.
92 int nparam_min
, nparam_max
;
93 int plus
; /* is the last parameter greedy? */
94 int nolist
; /* is this macro listing-inhibited? */
96 Token
*dlist
; /* All defaults as one list */
97 Token
**defaults
; /* Parameter default pointers */
98 int ndefs
; /* number of default parameters */
102 MMacro
*rep_nest
; /* used for nesting %rep */
103 Token
**params
; /* actual parameters */
104 Token
*iline
; /* invocation line */
105 int nparam
, rotate
, *paramlen
;
106 unsigned long unique
;
107 int lineno
; /* Current line number on expansion */
111 * The context stack is composed of a linked list of these.
118 unsigned long number
;
122 * This is the internal form which we break input lines up into.
123 * Typically stored in linked lists.
125 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
126 * necessarily used as-is, but is intended to denote the number of
127 * the substituted parameter. So in the definition
129 * %define a(x,y) ( (x) & ~(y) )
131 * the token representing `x' will have its type changed to
132 * TOK_SMAC_PARAM, but the one representing `y' will be
135 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
136 * which doesn't need quotes around it. Used in the pre-include
137 * mechanism as an alternative to trying to find a sensible type of
138 * quote to use on the filename we were passed.
144 SMacro
*mac
; /* associated macro for TOK_SMAC_END */
149 TOK_WHITESPACE
= 1, TOK_COMMENT
, TOK_ID
, TOK_PREPROC_ID
, TOK_STRING
,
150 TOK_NUMBER
, TOK_SMAC_END
, TOK_OTHER
, TOK_SMAC_PARAM
,
155 * Multi-line macro definitions are stored as a linked list of
156 * these, which is essentially a container to allow several linked
159 * Note that in this module, linked lists are treated as stacks
160 * wherever possible. For this reason, Lines are _pushed_ on to the
161 * `expansion' field in MMacro structures, so that the linked list,
162 * if walked, would give the macro lines in reverse order; this
163 * means that we can walk the list when expanding a macro, and thus
164 * push the lines on to the `expansion' field in _istk_ in reverse
165 * order (so that when popped back off they are in the right
166 * order). It may seem cockeyed, and it relies on my design having
167 * an even number of steps in, but it works...
169 * Some of these structures, rather than being actual lines, are
170 * markers delimiting the end of the expansion of a given macro.
171 * This is for use in the cycle-tracking and %rep-handling code.
172 * Such structures have `finishes' non-NULL, and `first' NULL. All
173 * others have `finishes' NULL, but `first' may still be NULL if
184 * To handle an arbitrary level of file inclusion, we maintain a
185 * stack (ie linked list) of these things.
195 MMacro
*mstk
; /* stack of active macros/reps */
199 * Include search path. This is simply a list of strings which get
200 * prepended, in turn, to the name of an include file, in an
201 * attempt to find the file if it's not in the current directory.
210 * Conditional assembly: we maintain a separate stack of these for
211 * each level of file inclusion. (The only reason we keep the
212 * stacks separate is to ensure that a stray `%endif' in a file
213 * included from within the true branch of a `%if' won't terminate
214 * it and cause confusion: instead, rightly, it'll cause an error.)
224 * These states are for use just after %if or %elif: IF_TRUE
225 * means the condition has evaluated to truth so we are
226 * currently emitting, whereas IF_FALSE means we are not
227 * currently emitting but will start doing so if a %else comes
228 * up. In these states, all directives are admissible: %elif,
229 * %else and %endif. (And of course %if.)
231 COND_IF_TRUE
, COND_IF_FALSE
,
233 * These states come up after a %else: ELSE_TRUE means we're
234 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
235 * any %elif or %else will cause an error.
237 COND_ELSE_TRUE
, COND_ELSE_FALSE
,
239 * This state means that we're not emitting now, and also that
240 * nothing until %endif will be emitted at all. It's for use in
241 * two circumstances: (i) when we've had our moment of emission
242 * and have now started seeing %elifs, and (ii) when the
243 * condition construct in question is contained within a
244 * non-emitting branch of a larger condition construct.
248 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
251 * Condition codes. Note that we use c_ prefix not C_ because C_ is
252 * used in nasm.h for the "real" condition codes. At _this_ level,
253 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
254 * ones, so we need a different enum...
256 static char *conditions
[] = {
257 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
258 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
259 "np", "ns", "nz", "o", "p", "pe", "po", "s", "z"
263 c_A
, c_AE
, c_B
, c_BE
, c_C
, c_CXZ
, c_E
, c_ECXZ
, c_G
, c_GE
, c_L
, c_LE
,
264 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, c_NE
, c_NG
, c_NGE
, c_NL
, c_NLE
, c_NO
,
265 c_NP
, c_NS
, c_NZ
, c_O
, c_P
, c_PE
, c_PO
, c_S
, c_Z
267 static int inverse_ccs
[] = {
268 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, -1, c_NE
, -1, c_NG
, c_NGE
, c_NL
, c_NLE
,
269 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
,
270 c_Z
, c_NO
, c_NP
, c_PO
, c_PE
, c_NS
, c_NZ
276 static char *directives
[] = {
278 "%assign", "%clear", "%define", "%elif", "%elifctx", "%elifdef",
279 "%elifid", "%elifidn", "%elifidni", "%elifnctx", "%elifndef",
280 "%elifnid", "%elifnidn", "%elifnidni", "%elifnnum", "%elifnstr",
281 "%elifnum", "%elifstr", "%else", "%endif", "%endm", "%endmacro",
282 "%endrep", "%error", "%exitrep", "%iassign", "%idefine", "%if",
283 "%ifctx", "%ifdef", "%ifid", "%ifidn", "%ifidni", "%ifnctx",
284 "%ifndef", "%ifnid", "%ifnidn", "%ifnidni", "%ifnnum",
285 "%ifnstr", "%ifnum", "%ifstr", "%imacro", "%include",
286 "%ixdefine", "%line",
288 "%macro", "%pop", "%push", "%rep", "%repl", "%rotate",
290 "%strlen", "%substr", "%undef", "%xdefine"
295 PP_ASSIGN
, PP_CLEAR
, PP_DEFINE
, PP_ELIF
, PP_ELIFCTX
, PP_ELIFDEF
,
296 PP_ELIFID
, PP_ELIFIDN
, PP_ELIFIDNI
, PP_ELIFNCTX
, PP_ELIFNDEF
,
297 PP_ELIFNID
, PP_ELIFNIDN
, PP_ELIFNIDNI
, PP_ELIFNNUM
, PP_ELIFNSTR
,
298 PP_ELIFNUM
, PP_ELIFSTR
, PP_ELSE
, PP_ENDIF
, PP_ENDM
, PP_ENDMACRO
,
299 PP_ENDREP
, PP_ERROR
, PP_EXITREP
, PP_IASSIGN
, PP_IDEFINE
, PP_IF
,
300 PP_IFCTX
, PP_IFDEF
, PP_IFID
, PP_IFIDN
, PP_IFIDNI
, PP_IFNCTX
,
301 PP_IFNDEF
, PP_IFNID
, PP_IFNIDN
, PP_IFNIDNI
, PP_IFNNUM
,
302 PP_IFNSTR
, PP_IFNUM
, PP_IFSTR
, PP_IMACRO
, PP_INCLUDE
,
303 PP_IXDEFINE
, PP_LINE
,
305 PP_MACRO
, PP_POP
, PP_PUSH
, PP_REP
, PP_REPL
, PP_ROTATE
,
307 PP_STRLEN
, PP_SUBSTR
, PP_UNDEF
, PP_XDEFINE
311 /* For TASM compatibility we need to be able to recognise TASM compatible
312 * conditional compilation directives. Using the NASM pre-processor does
313 * not work, so we look for them specifically from the following list and
314 * then jam in the equivalent NASM directive into the input stream.
318 # define MAX(a,b) ( ((a) > (b)) ? (a) : (b))
323 TM_ARG
, TM_ELIF
, TM_ELSE
, TM_ENDIF
, TM_IF
, TM_IFDEF
, TM_IFDIFI
,
324 TM_IFNDEF
, TM_INCLUDE
, TM_LOCAL
327 static char *tasm_directives
[] = {
328 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
329 "ifndef", "include", "local"
332 static int StackSize
= 4;
333 static char *StackPointer
= "ebp";
334 static int ArgOffset
= 8;
335 static int LocalOffset
= 4;
338 static Context
*cstk
;
339 static Include
*istk
;
340 static IncPath
*ipath
= NULL
;
342 static efunc __error
; /* Pointer to client-provided error reporting function */
343 static evalfunc evaluate
;
345 static int pass
; /* HACK: pass 0 = generate dependencies only */
347 static unsigned long unique
; /* unique identifier numbers */
349 static Line
*predef
= NULL
;
351 static ListGen
*list
;
354 * The number of hash values we use for the macro lookup tables.
355 * FIXME: We should *really* be able to configure this at run time,
356 * or even have the hash table automatically expanding when necessary.
361 * The current set of multi-line macros we have defined.
363 static MMacro
*mmacros
[NHASH
];
366 * The current set of single-line macros we have defined.
368 static SMacro
*smacros
[NHASH
];
371 * The multi-line macro we are currently defining, or the %rep
372 * block we are currently reading, if any.
374 static MMacro
*defining
;
377 * The number of macro parameters to allocate space for at a time.
379 #define PARAM_DELTA 16
382 * The standard macro set: defined as `static char *stdmac[]'. Also
383 * gives our position in the macro set, when we're processing it.
386 static char **stdmacpos
;
389 * The extra standard macros that come from the object format, if
392 static char **extrastdmac
= NULL
;
396 * Tokens are allocated in blocks to improve speed
398 #define TOKEN_BLOCKSIZE 4096
399 static Token
*freeTokens
= NULL
;
405 static Blocks blocks
= { NULL
, NULL
};
408 * Forward declarations.
410 static Token
*expand_mmac_params(Token
* tline
);
411 static Token
*expand_smacro(Token
* tline
);
412 static Token
*expand_id(Token
* tline
);
413 static Context
*get_ctx(char *name
, int all_contexts
);
414 static void make_tok_num(Token
* tok
, long val
);
415 static void error(int severity
, char *fmt
, ...);
416 static void *new_Block(size_t size
);
417 static void delete_Blocks(void);
418 static Token
*new_Token(Token
* next
, int type
, char *text
, int txtlen
);
419 static Token
*delete_Token(Token
* t
);
422 * Macros for safe checking of token pointers, avoid *(NULL)
424 #define tok_type_(x,t) ((x) && (x)->type == (t))
425 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
426 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
427 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
429 /* Handle TASM specific directives, which do not contain a % in
430 * front of them. We do it here because I could not find any other
431 * place to do it for the moment, and it is a hack (ideally it would
432 * be nice to be able to use the NASM pre-processor to do it).
435 check_tasm_directive(char *line
)
438 char *p
= line
, *oldline
, oldchar
;
440 /* Skip whitespace */
441 while (isspace(*p
) && *p
!= 0)
444 /* Binary search for the directive name */
446 j
= sizeof(tasm_directives
) / sizeof(*tasm_directives
);
448 while (!isspace(p
[len
]) && p
[len
] != 0)
457 m
= nasm_stricmp(p
, tasm_directives
[k
]);
460 /* We have found a directive, so jam a % in front of it
461 * so that NASM will then recognise it as one if it's own.
466 line
= nasm_malloc(len
+ 2);
470 /* NASM does not recognise IFDIFI, so we convert it to
471 * %ifdef BOGUS. This is not used in NASM comaptible
472 * code, but does need to parse for the TASM macro
475 strcpy(line
+ 1, "ifdef BOGUS");
479 memcpy(line
+ 1, p
, len
+ 1);
497 * The pre-preprocessing stage... This function translates line
498 * number indications as they emerge from GNU cpp (`# lineno "file"
499 * flags') into NASM preprocessor line number indications (`%line
503 prepreproc(char *line
)
506 char *fname
, *oldline
;
508 if (line
[0] == '#' && line
[1] == ' ')
512 lineno
= atoi(fname
);
513 fname
+= strspn(fname
, "0123456789 ");
516 fnlen
= strcspn(fname
, "\"");
517 line
= nasm_malloc(20 + fnlen
);
518 sprintf(line
, "%%line %d %.*s", lineno
, fnlen
, fname
);
521 if (tasm_compatible_mode
)
522 return check_tasm_directive(line
);
527 * The hash function for macro lookups. Note that due to some
528 * macros having case-insensitive names, the hash function must be
529 * invariant under case changes. We implement this by applying a
530 * perfectly normal hash function to the uppercase of the string.
538 * Powers of three, mod 31.
540 static const int multipliers
[] = {
541 1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10,
542 30, 28, 22, 4, 12, 5, 15, 14, 11, 2, 6, 18, 23, 7, 21
548 h
+= multipliers
[i
] * (unsigned char) (toupper(*s
));
550 if (++i
>= sizeof(multipliers
) / sizeof(*multipliers
))
558 * Free a linked list of tokens.
561 free_tlist(Token
* list
)
565 list
= delete_Token(list
);
570 * Free a linked list of lines.
573 free_llist(Line
* list
)
580 free_tlist(l
->first
);
589 free_mmacro(MMacro
* m
)
592 free_tlist(m
->dlist
);
593 nasm_free(m
->defaults
);
594 free_llist(m
->expansion
);
599 * Pop the context stack.
614 free_tlist(s
->expansion
);
621 #define BUF_DELTA 512
623 * Read a line from the top file in istk, handling multiple CR/LFs
624 * at the end of the line read, and handling spurious ^Zs. Will
625 * return lines from the standard macro set if this has not already
631 char *buffer
, *p
, *q
;
632 int bufsize
, continued_count
;
638 char *ret
= nasm_strdup(*stdmacpos
++);
639 if (!*stdmacpos
&& any_extrastdmac
)
641 stdmacpos
= extrastdmac
;
642 any_extrastdmac
= FALSE
;
646 * Nasty hack: here we push the contents of `predef' on
647 * to the top-level expansion stack, since this is the
648 * most convenient way to implement the pre-include and
649 * pre-define features.
654 Token
*head
, **tail
, *t
;
656 for (pd
= predef
; pd
; pd
= pd
->next
)
660 for (t
= pd
->first
; t
; t
= t
->next
)
662 *tail
= new_Token(NULL
, t
->type
, t
->text
, 0);
663 tail
= &(*tail
)->next
;
665 l
= nasm_malloc(sizeof(Line
));
666 l
->next
= istk
->expansion
;
681 buffer
= nasm_malloc(BUF_DELTA
);
686 q
= fgets(p
, bufsize
- (p
- buffer
), istk
->fp
);
690 if (p
> buffer
&& p
[-1] == '\n')
692 /* Convert backslash-CRLF line continuation sequences into
693 nothing at all (for DOS and Windows) */
694 if (((p
- 2) > buffer
) && (p
[-3] == '\\') && (p
[-2] == '\r')) {
699 /* Also convert backslash-LF line continuation sequences into
700 nothing at all (for Unix) */
701 else if (((p
- 1) > buffer
) && (p
[-2] == '\\')) {
710 if (p
- buffer
> bufsize
- 10)
712 long offset
= p
- buffer
;
713 bufsize
+= BUF_DELTA
;
714 buffer
= nasm_realloc(buffer
, bufsize
);
715 p
= buffer
+ offset
; /* prevent stale-pointer problems */
719 if (!q
&& p
== buffer
)
725 src_set_linnum(src_get_linnum() + istk
->lineinc
+ (continued_count
* istk
->lineinc
));
728 * Play safe: remove CRs as well as LFs, if any of either are
729 * present at the end of the line.
731 while (--p
>= buffer
&& (*p
== '\n' || *p
== '\r'))
735 * Handle spurious ^Z, which may be inserted into source files
736 * by some file transfer utilities.
738 buffer
[strcspn(buffer
, "\032")] = '\0';
740 list
->line(LIST_READ
, buffer
);
746 * Tokenise a line of text. This is a very simple process since we
747 * don't need to parse the value out of e.g. numeric tokens: we
748 * simply split one string into many.
756 Token
*t
, **tail
= &list
;
765 ((*p
== '-' || *p
== '+') && isdigit(p
[1])) ||
766 ((*p
== '+') && (isspace(p
[1]) || !p
[1])))
773 type
= TOK_PREPROC_ID
;
778 while (*p
&& *p
!= '}')
786 type
= TOK_PREPROC_ID
;
788 else if (isidchar(*p
) ||
789 ((*p
== '!' || *p
== '%' || *p
== '$') &&
796 while (isidchar(*p
));
797 type
= TOK_PREPROC_ID
;
806 else if (isidstart(*p
) || (*p
== '$' && isidstart(p
[1])))
810 while (*p
&& isidchar(*p
))
813 else if (*p
== '\'' || *p
== '"')
821 while (*p
&& *p
!= c
)
829 error(ERR_WARNING
, "unterminated string");
832 else if (isnumstart(*p
))
839 while (*p
&& isnumchar(*p
))
842 else if (isspace(*p
))
844 type
= TOK_WHITESPACE
;
846 while (*p
&& isspace(*p
))
849 * Whitespace just before end-of-line is discarded by
850 * pretending it's a comment; whitespace just before a
851 * comment gets lumped into the comment.
853 if (!*p
|| *p
== ';')
869 * Anything else is an operator of some kind. We check
870 * for all the double-character operators (>>, <<, //,
871 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
872 * else is a single-character operator.
875 if ((p
[0] == '>' && p
[1] == '>') ||
876 (p
[0] == '<' && p
[1] == '<') ||
877 (p
[0] == '/' && p
[1] == '/') ||
878 (p
[0] == '<' && p
[1] == '=') ||
879 (p
[0] == '>' && p
[1] == '=') ||
880 (p
[0] == '=' && p
[1] == '=') ||
881 (p
[0] == '!' && p
[1] == '=') ||
882 (p
[0] == '<' && p
[1] == '>') ||
883 (p
[0] == '&' && p
[1] == '&') ||
884 (p
[0] == '|' && p
[1] == '|') ||
885 (p
[0] == '^' && p
[1] == '^'))
891 if (type
!= TOK_COMMENT
)
893 *tail
= t
= new_Token(NULL
, type
, line
, p
- line
);
902 * this function allocates a new managed block of memory and
903 * returns a pointer to the block. The managed blocks are
904 * deleted only all at once by the delete_Blocks function.
907 new_Block(size_t size
)
911 /* first, get to the end of the linked list */
914 /* now allocate the requested chunk */
915 b
->chunk
= nasm_malloc(size
);
917 /* now allocate a new block for the next request */
918 b
->next
= nasm_malloc(sizeof(Blocks
));
919 /* and initialize the contents of the new block */
920 b
->next
->next
= NULL
;
921 b
->next
->chunk
= NULL
;
926 * this function deletes all managed blocks of memory
931 Blocks
*a
,*b
= &blocks
;
934 * keep in mind that the first block, pointed to by blocks
935 * is a static and not dynamically allocated, so we don't
950 * this function creates a new Token and passes a pointer to it
951 * back to the caller. It sets the type and text elements, and
952 * also the mac and next elements to NULL.
955 new_Token(Token
* next
, int type
, char *text
, int txtlen
)
960 if (freeTokens
== NULL
)
962 freeTokens
= (Token
*)new_Block(TOKEN_BLOCKSIZE
* sizeof(Token
));
963 for (i
= 0; i
< TOKEN_BLOCKSIZE
- 1; i
++)
964 freeTokens
[i
].next
= &freeTokens
[i
+ 1];
965 freeTokens
[i
].next
= NULL
;
968 freeTokens
= t
->next
;
972 if (type
== TOK_WHITESPACE
|| text
== NULL
)
979 txtlen
= strlen(text
);
980 t
->text
= nasm_malloc(1 + txtlen
);
981 strncpy(t
->text
, text
, txtlen
);
982 t
->text
[txtlen
] = '\0';
988 delete_Token(Token
* t
)
990 Token
*next
= t
->next
;
992 t
->next
= freeTokens
;
998 * Convert a line of tokens back into text.
999 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1000 * will be transformed into ..@ctxnum.xxx
1003 detoken(Token
* tlist
, int expand_locals
)
1010 for (t
= tlist
; t
; t
= t
->next
)
1012 if (t
->type
== TOK_PREPROC_ID
&& t
->text
[1] == '!')
1014 char *p
= getenv(t
->text
+ 2);
1017 t
->text
= nasm_strdup(p
);
1021 /* Expand local macros here and not during preprocessing */
1022 if (expand_locals
&&
1023 t
->type
== TOK_PREPROC_ID
&& t
->text
&&
1024 t
->text
[0] == '%' && t
->text
[1] == '$')
1026 Context
*ctx
= get_ctx(t
->text
, FALSE
);
1030 char *p
, *q
= t
->text
+ 2;
1032 q
+= strspn(q
, "$");
1033 sprintf(buffer
, "..@%lu.", ctx
->number
);
1034 p
= nasm_strcat(buffer
, q
);
1039 if (t
->type
== TOK_WHITESPACE
)
1045 len
+= strlen(t
->text
);
1048 p
= line
= nasm_malloc(len
+ 1);
1049 for (t
= tlist
; t
; t
= t
->next
)
1051 if (t
->type
== TOK_WHITESPACE
)
1068 * A scanner, suitable for use by the expression evaluator, which
1069 * operates on a line of Tokens. Expects a pointer to a pointer to
1070 * the first token in the line to be passed in as its private_data
1074 ppscan(void *private_data
, struct tokenval
*tokval
)
1076 Token
**tlineptr
= private_data
;
1082 *tlineptr
= tline
? tline
->next
: NULL
;
1084 while (tline
&& (tline
->type
== TOK_WHITESPACE
||
1085 tline
->type
== TOK_COMMENT
));
1088 return tokval
->t_type
= TOKEN_EOS
;
1090 if (tline
->text
[0] == '$' && !tline
->text
[1])
1091 return tokval
->t_type
= TOKEN_HERE
;
1092 if (tline
->text
[0] == '$' && tline
->text
[1] == '$' && !tline
->text
[1])
1093 return tokval
->t_type
= TOKEN_BASE
;
1095 if (tline
->type
== TOK_ID
)
1097 tokval
->t_charptr
= tline
->text
;
1098 if (tline
->text
[0] == '$')
1100 tokval
->t_charptr
++;
1101 return tokval
->t_type
= TOKEN_ID
;
1105 * This is the only special case we actually need to worry
1106 * about in this restricted context.
1108 if (!nasm_stricmp(tline
->text
, "seg"))
1109 return tokval
->t_type
= TOKEN_SEG
;
1111 return tokval
->t_type
= TOKEN_ID
;
1114 if (tline
->type
== TOK_NUMBER
)
1118 tokval
->t_integer
= readnum(tline
->text
, &rn_error
);
1120 return tokval
->t_type
= TOKEN_ERRNUM
;
1121 tokval
->t_charptr
= NULL
;
1122 return tokval
->t_type
= TOKEN_NUM
;
1125 if (tline
->type
== TOK_STRING
)
1135 if (l
== 0 || r
[l
- 1] != q
)
1136 return tokval
->t_type
= TOKEN_ERRNUM
;
1137 tokval
->t_integer
= readstrnum(r
, l
- 1, &rn_warn
);
1139 error(ERR_WARNING
| ERR_PASS1
, "character constant too long");
1140 tokval
->t_charptr
= NULL
;
1141 return tokval
->t_type
= TOKEN_NUM
;
1144 if (tline
->type
== TOK_OTHER
)
1146 if (!strcmp(tline
->text
, "<<"))
1147 return tokval
->t_type
= TOKEN_SHL
;
1148 if (!strcmp(tline
->text
, ">>"))
1149 return tokval
->t_type
= TOKEN_SHR
;
1150 if (!strcmp(tline
->text
, "//"))
1151 return tokval
->t_type
= TOKEN_SDIV
;
1152 if (!strcmp(tline
->text
, "%%"))
1153 return tokval
->t_type
= TOKEN_SMOD
;
1154 if (!strcmp(tline
->text
, "=="))
1155 return tokval
->t_type
= TOKEN_EQ
;
1156 if (!strcmp(tline
->text
, "<>"))
1157 return tokval
->t_type
= TOKEN_NE
;
1158 if (!strcmp(tline
->text
, "!="))
1159 return tokval
->t_type
= TOKEN_NE
;
1160 if (!strcmp(tline
->text
, "<="))
1161 return tokval
->t_type
= TOKEN_LE
;
1162 if (!strcmp(tline
->text
, ">="))
1163 return tokval
->t_type
= TOKEN_GE
;
1164 if (!strcmp(tline
->text
, "&&"))
1165 return tokval
->t_type
= TOKEN_DBL_AND
;
1166 if (!strcmp(tline
->text
, "^^"))
1167 return tokval
->t_type
= TOKEN_DBL_XOR
;
1168 if (!strcmp(tline
->text
, "||"))
1169 return tokval
->t_type
= TOKEN_DBL_OR
;
1173 * We have no other options: just return the first character of
1176 return tokval
->t_type
= tline
->text
[0];
1180 * Compare a string to the name of an existing macro; this is a
1181 * simple wrapper which calls either strcmp or nasm_stricmp
1182 * depending on the value of the `casesense' parameter.
1185 mstrcmp(char *p
, char *q
, int casesense
)
1187 return casesense
? strcmp(p
, q
) : nasm_stricmp(p
, q
);
1191 * Return the Context structure associated with a %$ token. Return
1192 * NULL, having _already_ reported an error condition, if the
1193 * context stack isn't deep enough for the supplied number of $
1195 * If all_contexts == TRUE, contexts that enclose current are
1196 * also scanned for such smacro, until it is found; if not -
1197 * only the context that directly results from the number of $'s
1198 * in variable's name.
1201 get_ctx(char *name
, int all_contexts
)
1207 if (!name
|| name
[0] != '%' || name
[1] != '$')
1212 error(ERR_NONFATAL
, "`%s': context stack is empty", name
);
1216 for (i
= strspn(name
+ 2, "$"), ctx
= cstk
; (i
> 0) && ctx
; i
--)
1219 /* i--; Lino - 02/25/02 */
1223 error(ERR_NONFATAL
, "`%s': context stack is only"
1224 " %d level%s deep", name
, i
- 1, (i
== 2 ? "" : "s"));
1232 /* Search for this smacro in found context */
1236 if (!mstrcmp(m
->name
, name
, m
->casesense
))
1246 /* Add a slash to the end of a path if it is missing. We use the
1247 * forward slash to make it compatible with Unix systems.
1252 int pos
= strlen(s
);
1253 if (s
[pos
- 1] != '\\' && s
[pos
- 1] != '/')
1261 * Open an include file. This routine must always return a valid
1262 * file pointer if it returns - it's responsible for throwing an
1263 * ERR_FATAL and bombing out completely if not. It should also try
1264 * the include path one by one until it finds the file or reaches
1265 * the end of the path.
1268 inc_fopen(char *file
)
1271 char *prefix
= "", *combine
;
1272 IncPath
*ip
= ipath
;
1273 static int namelen
= 0;
1274 int len
= strlen(file
);
1278 combine
= nasm_malloc(strlen(prefix
) + 1 + len
+ 1);
1279 strcpy(combine
, prefix
);
1282 strcat(combine
, file
);
1283 fp
= fopen(combine
, "r");
1284 if (pass
== 0 && fp
)
1286 namelen
+= strlen(combine
) + 1;
1292 printf(" %s", combine
);
1303 error(ERR_FATAL
, "unable to open include file `%s'", file
);
1304 return NULL
; /* never reached - placate compilers */
1308 * Determine if we should warn on defining a single-line macro of
1309 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1310 * return TRUE if _any_ single-line macro of that name is defined.
1311 * Otherwise, will return TRUE if a single-line macro with either
1312 * `nparam' or no parameters is defined.
1314 * If a macro with precisely the right number of parameters is
1315 * defined, or nparam is -1, the address of the definition structure
1316 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1317 * is NULL, no action will be taken regarding its contents, and no
1320 * Note that this is also called with nparam zero to resolve
1323 * If you already know which context macro belongs to, you can pass
1324 * the context pointer as first parameter; if you won't but name begins
1325 * with %$ the context will be automatically computed. If all_contexts
1326 * is true, macro will be searched in outer contexts as well.
1329 smacro_defined(Context
* ctx
, char *name
, int nparam
, SMacro
** defn
,
1336 else if (name
[0] == '%' && name
[1] == '$')
1339 ctx
= get_ctx(name
, FALSE
);
1341 return FALSE
; /* got to return _something_ */
1345 m
= smacros
[hash(name
)];
1349 if (!mstrcmp(m
->name
, name
, m
->casesense
&& nocase
) &&
1350 (nparam
<= 0 || m
->nparam
== 0 || nparam
== m
->nparam
))
1354 if (nparam
== m
->nparam
|| nparam
== -1)
1368 * Count and mark off the parameters in a multi-line macro call.
1369 * This is called both from within the multi-line macro expansion
1370 * code, and also to mark off the default parameters when provided
1371 * in a %macro definition line.
1374 count_mmac_params(Token
* t
, int *nparam
, Token
*** params
)
1376 int paramsize
, brace
;
1378 *nparam
= paramsize
= 0;
1382 if (*nparam
>= paramsize
)
1384 paramsize
+= PARAM_DELTA
;
1385 *params
= nasm_realloc(*params
, sizeof(**params
) * paramsize
);
1389 if (tok_is_(t
, "{"))
1391 (*params
)[(*nparam
)++] = t
;
1392 while (tok_isnt_(t
, brace
? "}" : ","))
1395 { /* got a comma/brace */
1400 * Now we've found the closing brace, look further
1404 if (tok_isnt_(t
, ","))
1407 "braces do not enclose all of macro parameter");
1408 while (tok_isnt_(t
, ","))
1412 t
= t
->next
; /* eat the comma */
1419 * Determine whether one of the various `if' conditions is true or
1422 * We must free the tline we get passed.
1425 if_condition(Token
* tline
, int i
)
1428 Token
*t
, *tt
, **tptr
, *origline
;
1429 struct tokenval tokval
;
1440 j
= FALSE
; /* have we matched yet? */
1441 while (cstk
&& tline
)
1444 if (!tline
|| tline
->type
!= TOK_ID
)
1447 "`%s' expects context identifiers",
1449 free_tlist(origline
);
1452 if (!nasm_stricmp(tline
->text
, cstk
->name
))
1454 tline
= tline
->next
;
1456 if (i
== PP_IFNCTX
|| i
== PP_ELIFNCTX
)
1458 free_tlist(origline
);
1465 j
= FALSE
; /* have we matched yet? */
1469 if (!tline
|| (tline
->type
!= TOK_ID
&&
1470 (tline
->type
!= TOK_PREPROC_ID
||
1471 tline
->text
[1] != '$')))
1474 "`%%if%sdef' expects macro identifiers",
1475 (i
== PP_ELIFNDEF
? "n" : ""));
1476 free_tlist(origline
);
1479 if (smacro_defined(NULL
, tline
->text
, 0, NULL
, 1))
1481 tline
= tline
->next
;
1483 if (i
== PP_IFNDEF
|| i
== PP_ELIFNDEF
)
1485 free_tlist(origline
);
1496 tline
= expand_smacro(tline
);
1498 while (tok_isnt_(tt
, ","))
1503 "`%s' expects two comma-separated arguments",
1509 casesense
= (i
== PP_IFIDN
|| i
== PP_ELIFIDN
||
1510 i
== PP_IFNIDN
|| i
== PP_ELIFNIDN
);
1511 j
= TRUE
; /* assume equality unless proved not */
1512 while ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) && tt
)
1514 if (tt
->type
== TOK_OTHER
&& !strcmp(tt
->text
, ","))
1516 error(ERR_NONFATAL
, "`%s': more than one comma on line",
1521 if (t
->type
== TOK_WHITESPACE
)
1526 else if (tt
->type
== TOK_WHITESPACE
)
1531 else if (tt
->type
!= t
->type
||
1532 mstrcmp(tt
->text
, t
->text
, casesense
))
1534 j
= FALSE
; /* found mismatching tokens */
1544 if ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) || tt
)
1545 j
= FALSE
; /* trailing gunk on one end or other */
1546 if (i
== PP_IFNIDN
|| i
== PP_ELIFNIDN
||
1547 i
== PP_IFNIDNI
|| i
== PP_ELIFNIDNI
)
1564 tline
= expand_smacro(tline
);
1566 while (tok_type_(t
, TOK_WHITESPACE
))
1568 j
= FALSE
; /* placate optimiser */
1576 j
= (t
->type
== TOK_ID
);
1582 j
= (t
->type
== TOK_NUMBER
);
1588 j
= (t
->type
== TOK_STRING
);
1591 if (i
== PP_IFNID
|| i
== PP_ELIFNID
||
1592 i
== PP_IFNNUM
|| i
== PP_ELIFNNUM
||
1593 i
== PP_IFNSTR
|| i
== PP_ELIFNSTR
)
1600 t
= tline
= expand_smacro(tline
);
1602 tokval
.t_type
= TOKEN_INVALID
;
1603 evalresult
= evaluate(ppscan
, tptr
, &tokval
,
1604 NULL
, pass
| CRITICAL
, error
, NULL
);
1610 "trailing garbage after expression ignored");
1611 if (!is_simple(evalresult
))
1614 "non-constant value given to `%s'", directives
[i
]);
1617 return reloc_value(evalresult
) != 0;
1621 "preprocessor directive `%s' not yet implemented",
1623 free_tlist(origline
);
1624 return -1; /* yeah, right */
1629 * Expand macros in a string. Used in %error and %include directives.
1630 * First tokenise the string, apply "expand_smacro" and then de-tokenise back.
1631 * The returned variable should ALWAYS be freed after usage.
1634 expand_macros_in_string(char **p
)
1636 Token
*line
= tokenise(*p
);
1637 line
= expand_smacro(line
);
1638 *p
= detoken(line
, FALSE
);
1642 * Find out if a line contains a preprocessor directive, and deal
1645 * If a directive _is_ found, we are expected to free_tlist() the
1648 * Return values go like this:
1650 * bit 0 is set if a directive was found (so the line gets freed)
1653 do_directive(Token
* tline
)
1655 int i
, j
, k
, m
, nparam
, nolist
;
1661 SMacro
*smac
, **smhead
;
1663 Token
*t
, *tt
, *param_start
, *macro_start
, *last
, **tptr
, *origline
;
1665 struct tokenval tokval
;
1667 MMacro
*tmp_defining
; /* Used when manipulating rep_nest */
1672 if (!tok_type_(tline
, TOK_PREPROC_ID
) ||
1673 (tline
->text
[1] == '%' || tline
->text
[1] == '$'
1674 || tline
->text
[1] == '!'))
1678 j
= sizeof(directives
) / sizeof(*directives
);
1682 m
= nasm_stricmp(tline
->text
, directives
[k
]);
1684 if (tasm_compatible_mode
) {
1687 } else if (k
!= PP_ARG
&& k
!= PP_LOCAL
&& k
!= PP_STACKSIZE
) {
1701 * If we're in a non-emitting branch of a condition construct,
1702 * or walking to the end of an already terminated %rep block,
1703 * we should ignore all directives except for condition
1706 if (((istk
->conds
&& !emitting(istk
->conds
->state
)) ||
1707 (istk
->mstk
&& !istk
->mstk
->in_progress
)) &&
1708 i
!= PP_IF
&& i
!= PP_ELIF
&&
1709 i
!= PP_IFCTX
&& i
!= PP_ELIFCTX
&&
1710 i
!= PP_IFDEF
&& i
!= PP_ELIFDEF
&&
1711 i
!= PP_IFID
&& i
!= PP_ELIFID
&&
1712 i
!= PP_IFIDN
&& i
!= PP_ELIFIDN
&&
1713 i
!= PP_IFIDNI
&& i
!= PP_ELIFIDNI
&&
1714 i
!= PP_IFNCTX
&& i
!= PP_ELIFNCTX
&&
1715 i
!= PP_IFNDEF
&& i
!= PP_ELIFNDEF
&&
1716 i
!= PP_IFNID
&& i
!= PP_ELIFNID
&&
1717 i
!= PP_IFNIDN
&& i
!= PP_ELIFNIDN
&&
1718 i
!= PP_IFNIDNI
&& i
!= PP_ELIFNIDNI
&&
1719 i
!= PP_IFNNUM
&& i
!= PP_ELIFNNUM
&&
1720 i
!= PP_IFNSTR
&& i
!= PP_ELIFNSTR
&&
1721 i
!= PP_IFNUM
&& i
!= PP_ELIFNUM
&&
1722 i
!= PP_IFSTR
&& i
!= PP_ELIFSTR
&& i
!= PP_ELSE
&& i
!= PP_ENDIF
)
1728 * If we're defining a macro or reading a %rep block, we should
1729 * ignore all directives except for %macro/%imacro (which
1730 * generate an error), %endm/%endmacro, and (only if we're in a
1731 * %rep block) %endrep. If we're in a %rep block, another %rep
1732 * causes an error, so should be let through.
1734 if (defining
&& i
!= PP_MACRO
&& i
!= PP_IMACRO
&&
1735 i
!= PP_ENDMACRO
&& i
!= PP_ENDM
&&
1736 (defining
->name
|| (i
!= PP_ENDREP
&& i
!= PP_REP
)))
1743 error(ERR_NONFATAL
, "unknown preprocessor directive `%s'",
1745 return 0; /* didn't get it */
1751 /* Directive to tell NASM what the default stack size is. The
1752 * default is for a 16-bit stack, and this can be overriden with
1754 * the following form:
1756 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1758 tline
= tline
->next
;
1759 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1760 tline
= tline
->next
;
1761 if (!tline
|| tline
->type
!= TOK_ID
)
1763 error(ERR_NONFATAL
, "`%%stacksize' missing size parameter");
1764 free_tlist(origline
);
1767 if (nasm_stricmp(tline
->text
, "flat") == 0)
1769 /* All subsequent ARG directives are for a 32-bit stack */
1771 StackPointer
= "ebp";
1775 else if (nasm_stricmp(tline
->text
, "large") == 0)
1777 /* All subsequent ARG directives are for a 16-bit stack,
1778 * far function call.
1781 StackPointer
= "bp";
1785 else if (nasm_stricmp(tline
->text
, "small") == 0)
1787 /* All subsequent ARG directives are for a 16-bit stack,
1788 * far function call. We don't support near functions.
1791 StackPointer
= "bp";
1797 error(ERR_NONFATAL
, "`%%stacksize' invalid size type");
1798 free_tlist(origline
);
1801 free_tlist(origline
);
1805 /* TASM like ARG directive to define arguments to functions, in
1806 * the following form:
1808 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1813 char *arg
, directive
[256];
1814 int size
= StackSize
;
1816 /* Find the argument name */
1817 tline
= tline
->next
;
1818 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1819 tline
= tline
->next
;
1820 if (!tline
|| tline
->type
!= TOK_ID
)
1822 error(ERR_NONFATAL
, "`%%arg' missing argument parameter");
1823 free_tlist(origline
);
1828 /* Find the argument size type */
1829 tline
= tline
->next
;
1830 if (!tline
|| tline
->type
!= TOK_OTHER
1831 || tline
->text
[0] != ':')
1834 "Syntax error processing `%%arg' directive");
1835 free_tlist(origline
);
1838 tline
= tline
->next
;
1839 if (!tline
|| tline
->type
!= TOK_ID
)
1842 "`%%arg' missing size type parameter");
1843 free_tlist(origline
);
1847 /* Allow macro expansion of type parameter */
1848 tt
= tokenise(tline
->text
);
1849 tt
= expand_smacro(tt
);
1850 if (nasm_stricmp(tt
->text
, "byte") == 0)
1852 size
= MAX(StackSize
, 1);
1854 else if (nasm_stricmp(tt
->text
, "word") == 0)
1856 size
= MAX(StackSize
, 2);
1858 else if (nasm_stricmp(tt
->text
, "dword") == 0)
1860 size
= MAX(StackSize
, 4);
1862 else if (nasm_stricmp(tt
->text
, "qword") == 0)
1864 size
= MAX(StackSize
, 8);
1866 else if (nasm_stricmp(tt
->text
, "tword") == 0)
1868 size
= MAX(StackSize
, 10);
1873 "Invalid size type for `%%arg' missing directive");
1875 free_tlist(origline
);
1880 /* Now define the macro for the argument */
1881 sprintf(directive
, "%%define %s (%s+%d)", arg
, StackPointer
,
1883 do_directive(tokenise(directive
));
1886 /* Move to the next argument in the list */
1887 tline
= tline
->next
;
1888 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1889 tline
= tline
->next
;
1891 while (tline
&& tline
->type
== TOK_OTHER
1892 && tline
->text
[0] == ',');
1893 free_tlist(origline
);
1897 /* TASM like LOCAL directive to define local variables for a
1898 * function, in the following form:
1900 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
1902 * The '= LocalSize' at the end is ignored by NASM, but is
1903 * required by TASM to define the local parameter size (and used
1904 * by the TASM macro package).
1906 offset
= LocalOffset
;
1909 char *local
, directive
[256];
1910 int size
= StackSize
;
1912 /* Find the argument name */
1913 tline
= tline
->next
;
1914 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1915 tline
= tline
->next
;
1916 if (!tline
|| tline
->type
!= TOK_ID
)
1919 "`%%local' missing argument parameter");
1920 free_tlist(origline
);
1923 local
= tline
->text
;
1925 /* Find the argument size type */
1926 tline
= tline
->next
;
1927 if (!tline
|| tline
->type
!= TOK_OTHER
1928 || tline
->text
[0] != ':')
1931 "Syntax error processing `%%local' directive");
1932 free_tlist(origline
);
1935 tline
= tline
->next
;
1936 if (!tline
|| tline
->type
!= TOK_ID
)
1939 "`%%local' missing size type parameter");
1940 free_tlist(origline
);
1944 /* Allow macro expansion of type parameter */
1945 tt
= tokenise(tline
->text
);
1946 tt
= expand_smacro(tt
);
1947 if (nasm_stricmp(tt
->text
, "byte") == 0)
1949 size
= MAX(StackSize
, 1);
1951 else if (nasm_stricmp(tt
->text
, "word") == 0)
1953 size
= MAX(StackSize
, 2);
1955 else if (nasm_stricmp(tt
->text
, "dword") == 0)
1957 size
= MAX(StackSize
, 4);
1959 else if (nasm_stricmp(tt
->text
, "qword") == 0)
1961 size
= MAX(StackSize
, 8);
1963 else if (nasm_stricmp(tt
->text
, "tword") == 0)
1965 size
= MAX(StackSize
, 10);
1970 "Invalid size type for `%%local' missing directive");
1972 free_tlist(origline
);
1977 /* Now define the macro for the argument */
1978 sprintf(directive
, "%%define %s (%s-%d)", local
, StackPointer
,
1980 do_directive(tokenise(directive
));
1983 /* Now define the assign to setup the enter_c macro correctly */
1984 sprintf(directive
, "%%assign %%$localsize %%$localsize+%d",
1986 do_directive(tokenise(directive
));
1988 /* Move to the next argument in the list */
1989 tline
= tline
->next
;
1990 if (tline
&& tline
->type
== TOK_WHITESPACE
)
1991 tline
= tline
->next
;
1993 while (tline
&& tline
->type
== TOK_OTHER
1994 && tline
->text
[0] == ',');
1995 free_tlist(origline
);
2001 "trailing garbage after `%%clear' ignored");
2002 for (j
= 0; j
< NHASH
; j
++)
2006 MMacro
*m
= mmacros
[j
];
2007 mmacros
[j
] = m
->next
;
2012 SMacro
*s
= smacros
[j
];
2013 smacros
[j
] = smacros
[j
]->next
;
2015 free_tlist(s
->expansion
);
2019 free_tlist(origline
);
2023 tline
= tline
->next
;
2025 if (!tline
|| (tline
->type
!= TOK_STRING
&&
2026 tline
->type
!= TOK_INTERNAL_STRING
))
2028 error(ERR_NONFATAL
, "`%%include' expects a file name");
2029 free_tlist(origline
);
2030 return 3; /* but we did _something_ */
2034 "trailing garbage after `%%include' ignored");
2035 if (tline
->type
!= TOK_INTERNAL_STRING
)
2037 p
= tline
->text
+ 1; /* point past the quote to the name */
2038 p
[strlen(p
) - 1] = '\0'; /* remove the trailing quote */
2041 p
= tline
->text
; /* internal_string is easier */
2042 expand_macros_in_string(&p
);
2043 inc
= nasm_malloc(sizeof(Include
));
2046 inc
->fp
= inc_fopen(p
);
2047 inc
->fname
= src_set_fname(p
);
2048 inc
->lineno
= src_set_linnum(0);
2050 inc
->expansion
= NULL
;
2053 list
->uplevel(LIST_INCLUDE
);
2054 free_tlist(origline
);
2058 tline
= tline
->next
;
2060 tline
= expand_id(tline
);
2061 if (!tok_type_(tline
, TOK_ID
))
2063 error(ERR_NONFATAL
, "`%%push' expects a context identifier");
2064 free_tlist(origline
);
2065 return 3; /* but we did _something_ */
2068 error(ERR_WARNING
, "trailing garbage after `%%push' ignored");
2069 ctx
= nasm_malloc(sizeof(Context
));
2071 ctx
->localmac
= NULL
;
2072 ctx
->name
= nasm_strdup(tline
->text
);
2073 ctx
->number
= unique
++;
2075 free_tlist(origline
);
2079 tline
= tline
->next
;
2081 tline
= expand_id(tline
);
2082 if (!tok_type_(tline
, TOK_ID
))
2084 error(ERR_NONFATAL
, "`%%repl' expects a context identifier");
2085 free_tlist(origline
);
2086 return 3; /* but we did _something_ */
2089 error(ERR_WARNING
, "trailing garbage after `%%repl' ignored");
2091 error(ERR_NONFATAL
, "`%%repl': context stack is empty");
2094 nasm_free(cstk
->name
);
2095 cstk
->name
= nasm_strdup(tline
->text
);
2097 free_tlist(origline
);
2102 error(ERR_WARNING
, "trailing garbage after `%%pop' ignored");
2105 "`%%pop': context stack is already empty");
2108 free_tlist(origline
);
2112 tline
->next
= expand_smacro(tline
->next
);
2113 tline
= tline
->next
;
2115 if (tok_type_(tline
, TOK_STRING
))
2117 p
= tline
->text
+ 1; /* point past the quote to the name */
2118 p
[strlen(p
) - 1] = '\0'; /* remove the trailing quote */
2119 expand_macros_in_string(&p
);
2120 error(ERR_NONFATAL
, "%s", p
);
2125 p
= detoken(tline
, FALSE
);
2126 error(ERR_WARNING
, "%s", p
);
2129 free_tlist(origline
);
2147 if (istk
->conds
&& !emitting(istk
->conds
->state
))
2151 j
= if_condition(tline
->next
, i
);
2152 tline
->next
= NULL
; /* it got freed */
2153 free_tlist(origline
);
2154 j
= j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2156 cond
= nasm_malloc(sizeof(Cond
));
2157 cond
->next
= istk
->conds
;
2160 return (j
== COND_IF_TRUE
? 3 : 1);
2178 error(ERR_FATAL
, "`%s': no matching `%%if'", directives
[i
]);
2179 if (emitting(istk
->conds
->state
)
2180 || istk
->conds
->state
== COND_NEVER
)
2181 istk
->conds
->state
= COND_NEVER
;
2184 j
= if_condition(expand_mmac_params(tline
->next
), i
);
2185 tline
->next
= NULL
; /* it got freed */
2186 free_tlist(origline
);
2187 istk
->conds
->state
=
2188 j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2190 return (istk
->conds
->state
== COND_IF_TRUE
? 5 : 1);
2194 error(ERR_WARNING
, "trailing garbage after `%%else' ignored");
2196 error(ERR_FATAL
, "`%%else': no matching `%%if'");
2197 if (emitting(istk
->conds
->state
)
2198 || istk
->conds
->state
== COND_NEVER
)
2199 istk
->conds
->state
= COND_ELSE_FALSE
;
2201 istk
->conds
->state
= COND_ELSE_TRUE
;
2202 free_tlist(origline
);
2208 "trailing garbage after `%%endif' ignored");
2210 error(ERR_FATAL
, "`%%endif': no matching `%%if'");
2212 istk
->conds
= cond
->next
;
2214 free_tlist(origline
);
2221 "`%%%smacro': already defining a macro",
2222 (i
== PP_IMACRO
? "i" : ""));
2223 tline
= tline
->next
;
2225 tline
= expand_id(tline
);
2226 if (!tok_type_(tline
, TOK_ID
))
2229 "`%%%smacro' expects a macro name",
2230 (i
== PP_IMACRO
? "i" : ""));
2233 defining
= nasm_malloc(sizeof(MMacro
));
2234 defining
->name
= nasm_strdup(tline
->text
);
2235 defining
->casesense
= (i
== PP_MACRO
);
2236 defining
->plus
= FALSE
;
2237 defining
->nolist
= FALSE
;
2238 defining
->in_progress
= FALSE
;
2239 defining
->rep_nest
= NULL
;
2240 tline
= expand_smacro(tline
->next
);
2242 if (!tok_type_(tline
, TOK_NUMBER
))
2245 "`%%%smacro' expects a parameter count",
2246 (i
== PP_IMACRO
? "i" : ""));
2247 defining
->nparam_min
= defining
->nparam_max
= 0;
2251 defining
->nparam_min
= defining
->nparam_max
=
2252 readnum(tline
->text
, &j
);
2255 "unable to parse parameter count `%s'",
2258 if (tline
&& tok_is_(tline
->next
, "-"))
2260 tline
= tline
->next
->next
;
2261 if (tok_is_(tline
, "*"))
2262 defining
->nparam_max
= INT_MAX
;
2263 else if (!tok_type_(tline
, TOK_NUMBER
))
2265 "`%%%smacro' expects a parameter count after `-'",
2266 (i
== PP_IMACRO
? "i" : ""));
2269 defining
->nparam_max
= readnum(tline
->text
, &j
);
2272 "unable to parse parameter count `%s'",
2274 if (defining
->nparam_min
> defining
->nparam_max
)
2276 "minimum parameter count exceeds maximum");
2279 if (tline
&& tok_is_(tline
->next
, "+"))
2281 tline
= tline
->next
;
2282 defining
->plus
= TRUE
;
2284 if (tline
&& tok_type_(tline
->next
, TOK_ID
) &&
2285 !nasm_stricmp(tline
->next
->text
, ".nolist"))
2287 tline
= tline
->next
;
2288 defining
->nolist
= TRUE
;
2290 mmac
= mmacros
[hash(defining
->name
)];
2293 if (!strcmp(mmac
->name
, defining
->name
) &&
2294 (mmac
->nparam_min
<= defining
->nparam_max
2296 && (defining
->nparam_min
<= mmac
->nparam_max
2300 "redefining multi-line macro `%s'",
2307 * Handle default parameters.
2309 if (tline
&& tline
->next
)
2311 defining
->dlist
= tline
->next
;
2313 count_mmac_params(defining
->dlist
, &defining
->ndefs
,
2314 &defining
->defaults
);
2318 defining
->dlist
= NULL
;
2319 defining
->defaults
= NULL
;
2321 defining
->expansion
= NULL
;
2322 free_tlist(origline
);
2329 error(ERR_NONFATAL
, "`%s': not defining a macro",
2333 k
= hash(defining
->name
);
2334 defining
->next
= mmacros
[k
];
2335 mmacros
[k
] = defining
;
2337 free_tlist(origline
);
2341 if (tline
->next
&& tline
->next
->type
== TOK_WHITESPACE
)
2342 tline
= tline
->next
;
2343 t
= expand_smacro(tline
->next
);
2345 free_tlist(origline
);
2348 tokval
.t_type
= TOKEN_INVALID
;
2350 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2356 "trailing garbage after expression ignored");
2357 if (!is_simple(evalresult
))
2359 error(ERR_NONFATAL
, "non-constant value given to `%%rotate'");
2363 while (mmac
&& !mmac
->name
) /* avoid mistaking %reps for macros */
2364 mmac
= mmac
->next_active
;
2367 "`%%rotate' invoked outside a macro call");
2368 mmac
->rotate
= mmac
->rotate
+ reloc_value(evalresult
);
2369 if (mmac
->rotate
< 0)
2370 mmac
->rotate
= mmac
->nparam
- (-mmac
->rotate
) % mmac
->nparam
;
2371 mmac
->rotate
%= mmac
->nparam
;
2376 tline
= tline
->next
;
2377 if (tline
->next
&& tline
->next
->type
== TOK_WHITESPACE
)
2378 tline
= tline
->next
;
2379 if (tline
->next
&& tline
->next
->type
== TOK_ID
&&
2380 !nasm_stricmp(tline
->next
->text
, ".nolist"))
2382 tline
= tline
->next
;
2385 t
= expand_smacro(tline
->next
);
2387 free_tlist(origline
);
2390 tokval
.t_type
= TOKEN_INVALID
;
2392 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2398 "trailing garbage after expression ignored");
2399 if (!is_simple(evalresult
))
2401 error(ERR_NONFATAL
, "non-constant value given to `%%rep'");
2404 tmp_defining
= defining
;
2405 defining
= nasm_malloc(sizeof(MMacro
));
2406 defining
->name
= NULL
; /* flags this macro as a %rep block */
2407 defining
->casesense
= 0;
2408 defining
->plus
= FALSE
;
2409 defining
->nolist
= nolist
;
2410 defining
->in_progress
= reloc_value(evalresult
) + 1;
2411 defining
->nparam_min
= defining
->nparam_max
= 0;
2412 defining
->defaults
= NULL
;
2413 defining
->dlist
= NULL
;
2414 defining
->expansion
= NULL
;
2415 defining
->next_active
= istk
->mstk
;
2416 defining
->rep_nest
= tmp_defining
;
2420 if (!defining
|| defining
->name
)
2422 error(ERR_NONFATAL
, "`%%endrep': no matching `%%rep'");
2427 * Now we have a "macro" defined - although it has no name
2428 * and we won't be entering it in the hash tables - we must
2429 * push a macro-end marker for it on to istk->expansion.
2430 * After that, it will take care of propagating itself (a
2431 * macro-end marker line for a macro which is really a %rep
2432 * block will cause the macro to be re-expanded, complete
2433 * with another macro-end marker to ensure the process
2434 * continues) until the whole expansion is forcibly removed
2435 * from istk->expansion by a %exitrep.
2437 l
= nasm_malloc(sizeof(Line
));
2438 l
->next
= istk
->expansion
;
2439 l
->finishes
= defining
;
2441 istk
->expansion
= l
;
2443 istk
->mstk
= defining
;
2445 list
->uplevel(defining
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
2446 tmp_defining
= defining
;
2447 defining
= defining
->rep_nest
;
2448 free_tlist(origline
);
2453 * We must search along istk->expansion until we hit a
2454 * macro-end marker for a macro with no name. Then we set
2455 * its `in_progress' flag to 0.
2457 for (l
= istk
->expansion
; l
; l
= l
->next
)
2458 if (l
->finishes
&& !l
->finishes
->name
)
2462 l
->finishes
->in_progress
= 0;
2464 error(ERR_NONFATAL
, "`%%exitrep' not within `%%rep' block");
2465 free_tlist(origline
);
2472 tline
= tline
->next
;
2474 tline
= expand_id(tline
);
2475 if (!tline
|| (tline
->type
!= TOK_ID
&&
2476 (tline
->type
!= TOK_PREPROC_ID
||
2477 tline
->text
[1] != '$')))
2480 "`%%%s%sdefine' expects a macro identifier",
2481 ((i
== PP_IDEFINE
|| i
== PP_IXDEFINE
) ? "i" : ""),
2482 ((i
== PP_XDEFINE
|| i
== PP_IXDEFINE
) ? "x" : ""));
2483 free_tlist(origline
);
2487 ctx
= get_ctx(tline
->text
, FALSE
);
2489 smhead
= &smacros
[hash(tline
->text
)];
2491 smhead
= &ctx
->localmac
;
2492 mname
= tline
->text
;
2494 param_start
= tline
= tline
->next
;
2497 /* Expand the macro definition now for %xdefine and %ixdefine */
2498 if ((i
== PP_XDEFINE
) || (i
== PP_IXDEFINE
))
2499 tline
= expand_smacro(tline
);
2501 if (tok_is_(tline
, "("))
2504 * This macro has parameters.
2507 tline
= tline
->next
;
2513 error(ERR_NONFATAL
, "parameter identifier expected");
2514 free_tlist(origline
);
2517 if (tline
->type
!= TOK_ID
)
2520 "`%s': parameter identifier expected",
2522 free_tlist(origline
);
2525 tline
->type
= TOK_SMAC_PARAM
+ nparam
++;
2526 tline
= tline
->next
;
2528 if (tok_is_(tline
, ","))
2530 tline
= tline
->next
;
2533 if (!tok_is_(tline
, ")"))
2536 "`)' expected to terminate macro template");
2537 free_tlist(origline
);
2543 tline
= tline
->next
;
2545 if (tok_type_(tline
, TOK_WHITESPACE
))
2546 last
= tline
, tline
= tline
->next
;
2552 if (t
->type
== TOK_ID
)
2554 for (tt
= param_start
; tt
; tt
= tt
->next
)
2555 if (tt
->type
>= TOK_SMAC_PARAM
&&
2556 !strcmp(tt
->text
, t
->text
))
2560 t
->next
= macro_start
;
2565 * Good. We now have a macro name, a parameter count, and a
2566 * token list (in reverse order) for an expansion. We ought
2567 * to be OK just to create an SMacro, store it, and let
2568 * free_tlist have the rest of the line (which we have
2569 * carefully re-terminated after chopping off the expansion
2572 if (smacro_defined(ctx
, mname
, nparam
, &smac
, i
== PP_DEFINE
))
2577 "single-line macro `%s' defined both with and"
2578 " without parameters", mname
);
2579 free_tlist(origline
);
2580 free_tlist(macro_start
);
2586 * We're redefining, so we have to take over an
2587 * existing SMacro structure. This means freeing
2588 * what was already in it.
2590 nasm_free(smac
->name
);
2591 free_tlist(smac
->expansion
);
2596 smac
= nasm_malloc(sizeof(SMacro
));
2597 smac
->next
= *smhead
;
2600 smac
->name
= nasm_strdup(mname
);
2601 smac
->casesense
= ((i
== PP_DEFINE
) || (i
== PP_XDEFINE
));
2602 smac
->nparam
= nparam
;
2603 smac
->expansion
= macro_start
;
2604 smac
->in_progress
= FALSE
;
2605 free_tlist(origline
);
2609 tline
= tline
->next
;
2611 tline
= expand_id(tline
);
2612 if (!tline
|| (tline
->type
!= TOK_ID
&&
2613 (tline
->type
!= TOK_PREPROC_ID
||
2614 tline
->text
[1] != '$')))
2616 error(ERR_NONFATAL
, "`%%undef' expects a macro identifier");
2617 free_tlist(origline
);
2623 "trailing garbage after macro name ignored");
2626 /* Find the context that symbol belongs to */
2627 ctx
= get_ctx(tline
->text
, FALSE
);
2629 smhead
= &smacros
[hash(tline
->text
)];
2631 smhead
= &ctx
->localmac
;
2633 mname
= tline
->text
;
2638 * We now have a macro name... go hunt for it.
2640 while (smacro_defined(ctx
, mname
, -1, &smac
, 1))
2642 /* Defined, so we need to find its predecessor and nuke it */
2644 for (s
= smhead
; *s
&& *s
!= smac
; s
= &(*s
)->next
);
2648 nasm_free(smac
->name
);
2649 free_tlist(smac
->expansion
);
2653 free_tlist(origline
);
2657 tline
= tline
->next
;
2659 tline
= expand_id(tline
);
2660 if (!tline
|| (tline
->type
!= TOK_ID
&&
2661 (tline
->type
!= TOK_PREPROC_ID
||
2662 tline
->text
[1] != '$')))
2665 "`%%strlen' expects a macro identifier as first parameter");
2666 free_tlist(origline
);
2669 ctx
= get_ctx(tline
->text
, FALSE
);
2671 smhead
= &smacros
[hash(tline
->text
)];
2673 smhead
= &ctx
->localmac
;
2674 mname
= tline
->text
;
2676 tline
= expand_smacro(tline
->next
);
2680 while (tok_type_(t
, TOK_WHITESPACE
))
2682 /* t should now point to the string */
2683 if (t
->type
!= TOK_STRING
)
2686 "`%%strlen` requires string as second parameter");
2688 free_tlist(origline
);
2692 macro_start
= nasm_malloc(sizeof(*macro_start
));
2693 macro_start
->next
= NULL
;
2694 make_tok_num(macro_start
, strlen(t
->text
) - 2);
2695 macro_start
->mac
= NULL
;
2698 * We now have a macro name, an implicit parameter count of
2699 * zero, and a numeric token to use as an expansion. Create
2700 * and store an SMacro.
2702 if (smacro_defined(ctx
, mname
, 0, &smac
, i
== PP_STRLEN
))
2706 "single-line macro `%s' defined both with and"
2707 " without parameters", mname
);
2711 * We're redefining, so we have to take over an
2712 * existing SMacro structure. This means freeing
2713 * what was already in it.
2715 nasm_free(smac
->name
);
2716 free_tlist(smac
->expansion
);
2721 smac
= nasm_malloc(sizeof(SMacro
));
2722 smac
->next
= *smhead
;
2725 smac
->name
= nasm_strdup(mname
);
2726 smac
->casesense
= (i
== PP_STRLEN
);
2728 smac
->expansion
= macro_start
;
2729 smac
->in_progress
= FALSE
;
2731 free_tlist(origline
);
2735 tline
= tline
->next
;
2737 tline
= expand_id(tline
);
2738 if (!tline
|| (tline
->type
!= TOK_ID
&&
2739 (tline
->type
!= TOK_PREPROC_ID
||
2740 tline
->text
[1] != '$')))
2743 "`%%substr' expects a macro identifier as first parameter");
2744 free_tlist(origline
);
2747 ctx
= get_ctx(tline
->text
, FALSE
);
2749 smhead
= &smacros
[hash(tline
->text
)];
2751 smhead
= &ctx
->localmac
;
2752 mname
= tline
->text
;
2754 tline
= expand_smacro(tline
->next
);
2758 while (tok_type_(t
, TOK_WHITESPACE
))
2761 /* t should now point to the string */
2762 if (t
->type
!= TOK_STRING
)
2765 "`%%substr` requires string as second parameter");
2767 free_tlist(origline
);
2773 tokval
.t_type
= TOKEN_INVALID
;
2775 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2779 free_tlist(origline
);
2782 if (!is_simple(evalresult
))
2784 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
2786 free_tlist(origline
);
2790 macro_start
= nasm_malloc(sizeof(*macro_start
));
2791 macro_start
->next
= NULL
;
2792 macro_start
->text
= nasm_strdup("'''");
2793 if (evalresult
->value
> 0
2794 && evalresult
->value
< strlen(t
->text
) - 1)
2796 macro_start
->text
[1] = t
->text
[evalresult
->value
];
2800 macro_start
->text
[2] = '\0';
2802 macro_start
->type
= TOK_STRING
;
2803 macro_start
->mac
= NULL
;
2806 * We now have a macro name, an implicit parameter count of
2807 * zero, and a numeric token to use as an expansion. Create
2808 * and store an SMacro.
2810 if (smacro_defined(ctx
, mname
, 0, &smac
, i
== PP_SUBSTR
))
2814 "single-line macro `%s' defined both with and"
2815 " without parameters", mname
);
2819 * We're redefining, so we have to take over an
2820 * existing SMacro structure. This means freeing
2821 * what was already in it.
2823 nasm_free(smac
->name
);
2824 free_tlist(smac
->expansion
);
2829 smac
= nasm_malloc(sizeof(SMacro
));
2830 smac
->next
= *smhead
;
2833 smac
->name
= nasm_strdup(mname
);
2834 smac
->casesense
= (i
== PP_SUBSTR
);
2836 smac
->expansion
= macro_start
;
2837 smac
->in_progress
= FALSE
;
2839 free_tlist(origline
);
2845 tline
= tline
->next
;
2847 tline
= expand_id(tline
);
2848 if (!tline
|| (tline
->type
!= TOK_ID
&&
2849 (tline
->type
!= TOK_PREPROC_ID
||
2850 tline
->text
[1] != '$')))
2853 "`%%%sassign' expects a macro identifier",
2854 (i
== PP_IASSIGN
? "i" : ""));
2855 free_tlist(origline
);
2858 ctx
= get_ctx(tline
->text
, FALSE
);
2860 smhead
= &smacros
[hash(tline
->text
)];
2862 smhead
= &ctx
->localmac
;
2863 mname
= tline
->text
;
2865 tline
= expand_smacro(tline
->next
);
2870 tokval
.t_type
= TOKEN_INVALID
;
2872 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2876 free_tlist(origline
);
2882 "trailing garbage after expression ignored");
2884 if (!is_simple(evalresult
))
2887 "non-constant value given to `%%%sassign'",
2888 (i
== PP_IASSIGN
? "i" : ""));
2889 free_tlist(origline
);
2893 macro_start
= nasm_malloc(sizeof(*macro_start
));
2894 macro_start
->next
= NULL
;
2895 make_tok_num(macro_start
, reloc_value(evalresult
));
2896 macro_start
->mac
= NULL
;
2899 * We now have a macro name, an implicit parameter count of
2900 * zero, and a numeric token to use as an expansion. Create
2901 * and store an SMacro.
2903 if (smacro_defined(ctx
, mname
, 0, &smac
, i
== PP_ASSIGN
))
2907 "single-line macro `%s' defined both with and"
2908 " without parameters", mname
);
2912 * We're redefining, so we have to take over an
2913 * existing SMacro structure. This means freeing
2914 * what was already in it.
2916 nasm_free(smac
->name
);
2917 free_tlist(smac
->expansion
);
2922 smac
= nasm_malloc(sizeof(SMacro
));
2923 smac
->next
= *smhead
;
2926 smac
->name
= nasm_strdup(mname
);
2927 smac
->casesense
= (i
== PP_ASSIGN
);
2929 smac
->expansion
= macro_start
;
2930 smac
->in_progress
= FALSE
;
2931 free_tlist(origline
);
2936 * Syntax is `%line nnn[+mmm] [filename]'
2938 tline
= tline
->next
;
2940 if (!tok_type_(tline
, TOK_NUMBER
))
2942 error(ERR_NONFATAL
, "`%%line' expects line number");
2943 free_tlist(origline
);
2946 k
= readnum(tline
->text
, &j
);
2948 tline
= tline
->next
;
2949 if (tok_is_(tline
, "+"))
2951 tline
= tline
->next
;
2952 if (!tok_type_(tline
, TOK_NUMBER
))
2954 error(ERR_NONFATAL
, "`%%line' expects line increment");
2955 free_tlist(origline
);
2958 m
= readnum(tline
->text
, &j
);
2959 tline
= tline
->next
;
2966 nasm_free(src_set_fname(detoken(tline
, FALSE
)));
2968 free_tlist(origline
);
2973 "preprocessor directive `%s' not yet implemented",
2981 * Ensure that a macro parameter contains a condition code and
2982 * nothing else. Return the condition code index if so, or -1
2992 if (t
->type
!= TOK_ID
)
2996 if (tt
&& (tt
->type
!= TOK_OTHER
|| strcmp(tt
->text
, ",")))
3000 j
= sizeof(conditions
) / sizeof(*conditions
);
3004 m
= nasm_stricmp(t
->text
, conditions
[k
]);
3024 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3025 * %-n) and MMacro-local identifiers (%%foo).
3028 expand_mmac_params(Token
* tline
)
3030 Token
*t
, *tt
, **tail
, *thead
;
3037 if (tline
->type
== TOK_PREPROC_ID
&&
3038 (((tline
->text
[1] == '+' || tline
->text
[1] == '-')
3039 && tline
->text
[2]) || tline
->text
[1] == '%'
3040 || (tline
->text
[1] >= '0' && tline
->text
[1] <= '9')))
3043 int type
= 0, cc
; /* type = 0 to placate optimisers */
3049 tline
= tline
->next
;
3052 while (mac
&& !mac
->name
) /* avoid mistaking %reps for macros */
3053 mac
= mac
->next_active
;
3055 error(ERR_NONFATAL
, "`%s': not in a macro call", t
->text
);
3060 * We have to make a substitution of one of the
3061 * forms %1, %-1, %+1, %%foo, %0.
3065 sprintf(tmpbuf
, "%d", mac
->nparam
);
3066 text
= nasm_strdup(tmpbuf
);
3070 sprintf(tmpbuf
, "..@%lu.", mac
->unique
);
3071 text
= nasm_strcat(tmpbuf
, t
->text
+ 2);
3074 n
= atoi(t
->text
+ 2) - 1;
3075 if (n
>= mac
->nparam
)
3079 if (mac
->nparam
> 1)
3080 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3081 tt
= mac
->params
[n
];
3087 "macro parameter %d is not a condition code",
3094 if (inverse_ccs
[cc
] == -1)
3097 "condition code `%s' is not invertible",
3103 nasm_strdup(conditions
[inverse_ccs
3108 n
= atoi(t
->text
+ 2) - 1;
3109 if (n
>= mac
->nparam
)
3113 if (mac
->nparam
> 1)
3114 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3115 tt
= mac
->params
[n
];
3121 "macro parameter %d is not a condition code",
3128 text
= nasm_strdup(conditions
[cc
]);
3132 n
= atoi(t
->text
+ 1) - 1;
3133 if (n
>= mac
->nparam
)
3137 if (mac
->nparam
> 1)
3138 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3139 tt
= mac
->params
[n
];
3143 for (i
= 0; i
< mac
->paramlen
[n
]; i
++)
3146 new_Token(NULL
, tt
->type
, tt
->text
,
3148 tail
= &(*tail
)->next
;
3152 text
= NULL
; /* we've done it here */
3173 tline
= tline
->next
;
3180 for (; t
&& (tt
= t
->next
) != NULL
; t
= t
->next
)
3183 case TOK_WHITESPACE
:
3184 if (tt
->type
== TOK_WHITESPACE
)
3186 t
->next
= delete_Token(tt
);
3190 if (tt
->type
== TOK_ID
|| tt
->type
== TOK_NUMBER
)
3192 char *tmp
= nasm_strcat(t
->text
, tt
->text
);
3195 t
->next
= delete_Token(tt
);
3199 if (tt
->type
== TOK_NUMBER
)
3201 char *tmp
= nasm_strcat(t
->text
, tt
->text
);
3204 t
->next
= delete_Token(tt
);
3213 * Expand all single-line macro calls made in the given line.
3214 * Return the expanded version of the line. The original is deemed
3215 * to be destroyed in the process. (In reality we'll just move
3216 * Tokens from input to output a lot of the time, rather than
3217 * actually bothering to destroy and replicate.)
3220 expand_smacro(Token
* tline
)
3222 Token
*t
, *tt
, *mstart
, **tail
, *thead
;
3223 SMacro
*head
= NULL
, *m
;
3226 int nparam
, sparam
, brackets
, rescan
;
3227 Token
*org_tline
= tline
;
3232 * Trick: we should avoid changing the start token pointer since it can
3233 * be contained in "next" field of other token. Because of this
3234 * we allocate a copy of first token and work with it; at the end of
3235 * routine we copy it back
3240 new_Token(org_tline
->next
, org_tline
->type
, org_tline
->text
,
3242 tline
->mac
= org_tline
->mac
;
3243 nasm_free(org_tline
->text
);
3244 org_tline
->text
= NULL
;
3252 { /* main token loop */
3253 if ((mname
= tline
->text
))
3255 /* if this token is a local macro, look in local context */
3256 if (tline
->type
== TOK_ID
|| tline
->type
== TOK_PREPROC_ID
)
3257 ctx
= get_ctx(mname
, TRUE
);
3261 head
= smacros
[hash(mname
)];
3263 head
= ctx
->localmac
;
3265 * We've hit an identifier. As in is_mmacro below, we first
3266 * check whether the identifier is a single-line macro at
3267 * all, then think about checking for parameters if
3270 for (m
= head
; m
; m
= m
->next
)
3271 if (!mstrcmp(m
->name
, mname
, m
->casesense
))
3281 * Simple case: the macro is parameterless. Discard the
3282 * one token that the macro call took, and push the
3283 * expansion back on the to-do stack.
3287 if (!strcmp("__FILE__", m
->name
))
3290 src_get(&num
, &(tline
->text
));
3291 nasm_quote(&(tline
->text
));
3292 tline
->type
= TOK_STRING
;
3295 if (!strcmp("__LINE__", m
->name
))
3297 nasm_free(tline
->text
);
3298 make_tok_num(tline
, src_get_linnum());
3301 tline
= delete_Token(tline
);
3308 * Complicated case: at least one macro with this name
3309 * exists and takes parameters. We must find the
3310 * parameters in the call, count them, find the SMacro
3311 * that corresponds to that form of the macro call, and
3312 * substitute for the parameters when we expand. What a
3315 tline
= tline
->next
;
3317 if (!tok_is_(tline
, "("))
3320 * This macro wasn't called with parameters: ignore
3321 * the call. (Behaviour borrowed from gnu cpp.)
3332 tline
= tline
->next
;
3333 sparam
= PARAM_DELTA
;
3334 params
= nasm_malloc(sparam
* sizeof(Token
*));
3336 paramsize
= nasm_malloc(sparam
* sizeof(int));
3338 for (;; tline
= tline
->next
)
3339 { /* parameter loop */
3343 "macro call expects terminating `)'");
3346 if (tline
->type
== TOK_WHITESPACE
3349 if (paramsize
[nparam
])
3352 params
[nparam
] = tline
->next
;
3353 continue; /* parameter loop */
3355 if (tline
->type
== TOK_OTHER
3356 && tline
->text
[1] == 0)
3358 char ch
= tline
->text
[0];
3359 if (ch
== ',' && !paren
&& brackets
<= 0)
3361 if (++nparam
>= sparam
)
3363 sparam
+= PARAM_DELTA
;
3364 params
= nasm_realloc(params
,
3365 sparam
* sizeof(Token
*));
3366 paramsize
= nasm_realloc(paramsize
,
3367 sparam
* sizeof(int));
3369 params
[nparam
] = tline
->next
;
3370 paramsize
[nparam
] = 0;
3372 continue; /* parameter loop */
3375 (brackets
> 0 || (brackets
== 0 &&
3376 !paramsize
[nparam
])))
3380 params
[nparam
] = tline
->next
;
3381 continue; /* parameter loop */
3384 if (ch
== '}' && brackets
> 0)
3385 if (--brackets
== 0)
3388 continue; /* parameter loop */
3390 if (ch
== '(' && !brackets
)
3392 if (ch
== ')' && brackets
<= 0)
3399 error(ERR_NONFATAL
, "braces do not "
3400 "enclose all of macro parameter");
3402 paramsize
[nparam
] += white
+ 1;
3404 } /* parameter loop */
3406 while (m
&& (m
->nparam
!= nparam
||
3407 mstrcmp(m
->name
, mname
,
3411 error(ERR_WARNING
| ERR_WARN_MNP
,
3412 "macro `%s' exists, "
3413 "but not taking %d parameters",
3414 mstart
->text
, nparam
);
3417 if (m
&& m
->in_progress
)
3419 if (!m
) /* in progess or didn't find '(' or wrong nparam */
3422 * Design question: should we handle !tline, which
3423 * indicates missing ')' here, or expand those
3424 * macros anyway, which requires the (t) test a few
3428 nasm_free(paramsize
);
3434 * Expand the macro: we are placed on the last token of the
3435 * call, so that we can easily split the call from the
3436 * following tokens. We also start by pushing an SMAC_END
3437 * token for the cycle removal.
3445 tt
= new_Token(tline
, TOK_SMAC_END
, NULL
, 0);
3447 m
->in_progress
= TRUE
;
3449 for (t
= m
->expansion
; t
; t
= t
->next
)
3451 if (t
->type
>= TOK_SMAC_PARAM
)
3453 Token
*pcopy
= tline
, **ptail
= &pcopy
;
3457 ttt
= params
[t
->type
- TOK_SMAC_PARAM
];
3458 for (i
= paramsize
[t
->type
- TOK_SMAC_PARAM
];
3462 new_Token(tline
, ttt
->type
, ttt
->text
,
3471 tt
= new_Token(tline
, t
->type
, t
->text
, 0);
3477 * Having done that, get rid of the macro call, and clean
3478 * up the parameters.
3481 nasm_free(paramsize
);
3483 continue; /* main token loop */
3488 if (tline
->type
== TOK_SMAC_END
)
3490 tline
->mac
->in_progress
= FALSE
;
3491 tline
= delete_Token(tline
);
3496 tline
= tline
->next
;
3504 * Now scan the entire line and look for successive TOK_IDs that resulted
3505 * after expansion (they can't be produced by tokenise()). The successive
3506 * TOK_IDs should be concatenated.
3507 * Also we look for %+ tokens and concatenate the tokens before and after
3508 * them (without white spaces in between).
3514 while (t
&& t
->type
!= TOK_ID
&& t
->type
!= TOK_PREPROC_ID
)
3518 if (t
->next
->type
== TOK_ID
||
3519 t
->next
->type
== TOK_PREPROC_ID
||
3520 t
->next
->type
== TOK_NUMBER
)
3522 char *p
= nasm_strcat(t
->text
, t
->next
->text
);
3524 t
->next
= delete_Token(t
->next
);
3528 else if (t
->next
->type
== TOK_WHITESPACE
&& t
->next
->next
&&
3529 t
->next
->next
->type
== TOK_PREPROC_ID
&&
3530 strcmp(t
->next
->next
->text
, "%+") == 0)
3532 /* free the next whitespace, the %+ token and next whitespace */
3534 for (i
= 1; i
<= 3; i
++)
3536 if (!t
->next
|| (i
!= 2 && t
->next
->type
!= TOK_WHITESPACE
))
3538 t
->next
= delete_Token(t
->next
);
3544 /* If we concatenaded something, re-scan the line for macros */
3555 *org_tline
= *thead
;
3556 /* since we just gave text to org_line, don't free it */
3558 delete_Token(thead
);
3562 /* the expression expanded to empty line;
3563 we can't return NULL for some reasons
3564 we just set the line to a single WHITESPACE token. */
3565 memset(org_tline
, 0, sizeof(*org_tline
));
3566 org_tline
->text
= NULL
;
3567 org_tline
->type
= TOK_WHITESPACE
;
3576 * Similar to expand_smacro but used exclusively with macro identifiers
3577 * right before they are fetched in. The reason is that there can be
3578 * identifiers consisting of several subparts. We consider that if there
3579 * are more than one element forming the name, user wants a expansion,
3580 * otherwise it will be left as-is. Example:
3584 * the identifier %$abc will be left as-is so that the handler for %define
3585 * will suck it and define the corresponding value. Other case:
3587 * %define _%$abc cde
3589 * In this case user wants name to be expanded *before* %define starts
3590 * working, so we'll expand %$abc into something (if it has a value;
3591 * otherwise it will be left as-is) then concatenate all successive
3595 expand_id(Token
* tline
)
3597 Token
*cur
, *oldnext
= NULL
;
3599 if (!tline
|| !tline
->next
)
3604 (cur
->next
->type
== TOK_ID
||
3605 cur
->next
->type
== TOK_PREPROC_ID
|| cur
->next
->type
== TOK_NUMBER
))
3608 /* If identifier consists of just one token, don't expand */
3614 oldnext
= cur
->next
; /* Detach the tail past identifier */
3615 cur
->next
= NULL
; /* so that expand_smacro stops here */
3618 tline
= expand_smacro(tline
);
3622 /* expand_smacro possibly changhed tline; re-scan for EOL */
3624 while (cur
&& cur
->next
)
3627 cur
->next
= oldnext
;
3634 * Determine whether the given line constitutes a multi-line macro
3635 * call, and return the MMacro structure called if so. Doesn't have
3636 * to check for an initial label - that's taken care of in
3637 * expand_mmacro - but must check numbers of parameters. Guaranteed
3638 * to be called with tline->type == TOK_ID, so the putative macro
3639 * name is easy to find.
3642 is_mmacro(Token
* tline
, Token
*** params_array
)
3648 head
= mmacros
[hash(tline
->text
)];
3651 * Efficiency: first we see if any macro exists with the given
3652 * name. If not, we can return NULL immediately. _Then_ we
3653 * count the parameters, and then we look further along the
3654 * list if necessary to find the proper MMacro.
3656 for (m
= head
; m
; m
= m
->next
)
3657 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
3663 * OK, we have a potential macro. Count and demarcate the
3666 count_mmac_params(tline
->next
, &nparam
, ¶ms
);
3669 * So we know how many parameters we've got. Find the MMacro
3670 * structure that handles this number.
3674 if (m
->nparam_min
<= nparam
&& (m
->plus
|| nparam
<= m
->nparam_max
))
3677 * This one is right. Just check if cycle removal
3678 * prohibits us using it before we actually celebrate...
3684 "self-reference in multi-line macro `%s'", m
->name
);
3690 * It's right, and we can use it. Add its default
3691 * parameters to the end of our list if necessary.
3693 if (m
->defaults
&& nparam
< m
->nparam_min
+ m
->ndefs
)
3696 nasm_realloc(params
,
3697 ((m
->nparam_min
+ m
->ndefs
+ 1) * sizeof(*params
)));
3698 while (nparam
< m
->nparam_min
+ m
->ndefs
)
3700 params
[nparam
] = m
->defaults
[nparam
- m
->nparam_min
];
3705 * If we've gone over the maximum parameter count (and
3706 * we're in Plus mode), ignore parameters beyond
3709 if (m
->plus
&& nparam
> m
->nparam_max
)
3710 nparam
= m
->nparam_max
;
3712 * Then terminate the parameter list, and leave.
3715 { /* need this special case */
3716 params
= nasm_malloc(sizeof(*params
));
3719 params
[nparam
] = NULL
;
3720 *params_array
= params
;
3724 * This one wasn't right: look for the next one with the
3727 for (m
= m
->next
; m
; m
= m
->next
)
3728 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
3733 * After all that, we didn't find one with the right number of
3734 * parameters. Issue a warning, and fail to expand the macro.
3736 error(ERR_WARNING
| ERR_WARN_MNP
,
3737 "macro `%s' exists, but not taking %d parameters",
3738 tline
->text
, nparam
);
3744 * Expand the multi-line macro call made by the given line, if
3745 * there is one to be expanded. If there is, push the expansion on
3746 * istk->expansion and return 1. Otherwise return 0.
3749 expand_mmacro(Token
* tline
)
3751 Token
*startline
= tline
;
3752 Token
*label
= NULL
;
3753 int dont_prepend
= 0;
3754 Token
**params
, *t
, *tt
;
3757 int i
, nparam
, *paramlen
;
3761 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
3762 if (!tok_type_(t
, TOK_ID
) && !tok_type_(t
, TOK_PREPROC_ID
))
3764 m
= is_mmacro(t
, ¶ms
);
3769 * We have an id which isn't a macro call. We'll assume
3770 * it might be a label; we'll also check to see if a
3771 * colon follows it. Then, if there's another id after
3772 * that lot, we'll check it again for macro-hood.
3776 if (tok_type_(t
, TOK_WHITESPACE
))
3777 last
= t
, t
= t
->next
;
3778 if (tok_is_(t
, ":"))
3781 last
= t
, t
= t
->next
;
3782 if (tok_type_(t
, TOK_WHITESPACE
))
3783 last
= t
, t
= t
->next
;
3785 if (!tok_type_(t
, TOK_ID
) || (m
= is_mmacro(t
, ¶ms
)) == NULL
)
3792 * Fix up the parameters: this involves stripping leading and
3793 * trailing whitespace, then stripping braces if they are
3796 for (nparam
= 0; params
[nparam
]; nparam
++)
3798 paramlen
= nparam
? nasm_malloc(nparam
* sizeof(*paramlen
)) : NULL
;
3800 for (i
= 0; params
[i
]; i
++)
3803 int comma
= (!m
->plus
|| i
< nparam
- 1);
3807 if (tok_is_(t
, "{"))
3808 t
= t
->next
, brace
= TRUE
, comma
= FALSE
;
3813 if (comma
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, ","))
3814 break; /* ... because we have hit a comma */
3815 if (comma
&& t
->type
== TOK_WHITESPACE
&& tok_is_(t
->next
, ","))
3816 break; /* ... or a space then a comma */
3817 if (brace
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, "}"))
3818 break; /* ... or a brace */
3825 * OK, we have a MMacro structure together with a set of
3826 * parameters. We must now go through the expansion and push
3827 * copies of each Line on to istk->expansion. Substitution of
3828 * parameter tokens and macro-local tokens doesn't get done
3829 * until the single-line macro substitution process; this is
3830 * because delaying them allows us to change the semantics
3831 * later through %rotate.
3833 * First, push an end marker on to istk->expansion, mark this
3834 * macro as in progress, and set up its invocation-specific
3837 ll
= nasm_malloc(sizeof(Line
));
3838 ll
->next
= istk
->expansion
;
3841 istk
->expansion
= ll
;
3843 m
->in_progress
= TRUE
;
3848 m
->paramlen
= paramlen
;
3849 m
->unique
= unique
++;
3852 m
->next_active
= istk
->mstk
;
3855 for (l
= m
->expansion
; l
; l
= l
->next
)
3859 ll
= nasm_malloc(sizeof(Line
));
3860 ll
->finishes
= NULL
;
3861 ll
->next
= istk
->expansion
;
3862 istk
->expansion
= ll
;
3865 for (t
= l
->first
; t
; t
= t
->next
)
3868 if (t
->type
== TOK_PREPROC_ID
&&
3869 t
->text
[1] == '0' && t
->text
[2] == '0')
3876 tt
= *tail
= new_Token(NULL
, x
->type
, x
->text
, 0);
3883 * If we had a label, push it on as the first line of
3884 * the macro expansion.
3888 if (dont_prepend
< 0)
3889 free_tlist(startline
);
3892 ll
= nasm_malloc(sizeof(Line
));
3893 ll
->finishes
= NULL
;
3894 ll
->next
= istk
->expansion
;
3895 istk
->expansion
= ll
;
3896 ll
->first
= startline
;
3900 label
= label
->next
;
3901 label
->next
= tt
= new_Token(NULL
, TOK_OTHER
, ":", 0);
3906 list
->uplevel(m
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
3912 * Since preprocessor always operate only on the line that didn't
3913 * arrived yet, we should always use ERR_OFFBY1. Also since user
3914 * won't want to see same error twice (preprocessing is done once
3915 * per pass) we will want to show errors only during pass one.
3918 error(int severity
, char *fmt
, ...)
3923 /* If we're in a dead branch of IF or something like it, ignore the error */
3924 if (istk
->conds
&& !emitting(istk
->conds
->state
))
3928 vsprintf(buff
, fmt
, arg
);
3931 if (istk
->mstk
&& istk
->mstk
->name
)
3932 __error(severity
| ERR_PASS1
, "(%s:%d) %s", istk
->mstk
->name
,
3933 istk
->mstk
->lineno
, buff
);
3935 __error(severity
| ERR_PASS1
, "%s", buff
);
3939 pp_reset(char *file
, int apass
, efunc errfunc
, evalfunc eval
,
3946 istk
= nasm_malloc(sizeof(Include
));
3949 istk
->expansion
= NULL
;
3951 istk
->fp
= fopen(file
, "r");
3953 src_set_fname(nasm_strdup(file
));
3957 error(ERR_FATAL
| ERR_NOFILE
, "unable to open input file `%s'", file
);
3959 for (h
= 0; h
< NHASH
; h
++)
3965 if (tasm_compatible_mode
) {
3968 stdmacpos
= &stdmac
[TASM_MACRO_COUNT
];
3970 any_extrastdmac
= (extrastdmac
!= NULL
);
3985 * Fetch a tokenised line, either from the macro-expansion
3986 * buffer or from the input file.
3989 while (istk
->expansion
&& istk
->expansion
->finishes
)
3991 Line
*l
= istk
->expansion
;
3992 if (!l
->finishes
->name
&& l
->finishes
->in_progress
> 1)
3997 * This is a macro-end marker for a macro with no
3998 * name, which means it's not really a macro at all
3999 * but a %rep block, and the `in_progress' field is
4000 * more than 1, meaning that we still need to
4001 * repeat. (1 means the natural last repetition; 0
4002 * means termination by %exitrep.) We have
4003 * therefore expanded up to the %endrep, and must
4004 * push the whole block on to the expansion buffer
4005 * again. We don't bother to remove the macro-end
4006 * marker: we'd only have to generate another one
4009 l
->finishes
->in_progress
--;
4010 for (l
= l
->finishes
->expansion
; l
; l
= l
->next
)
4012 Token
*t
, *tt
, **tail
;
4014 ll
= nasm_malloc(sizeof(Line
));
4015 ll
->next
= istk
->expansion
;
4016 ll
->finishes
= NULL
;
4020 for (t
= l
->first
; t
; t
= t
->next
)
4022 if (t
->text
|| t
->type
== TOK_WHITESPACE
)
4024 tt
= *tail
= new_Token(NULL
, t
->type
, t
->text
, 0);
4029 istk
->expansion
= ll
;
4035 * Check whether a `%rep' was started and not ended
4036 * within this macro expansion. This can happen and
4037 * should be detected. It's a fatal error because
4038 * I'm too confused to work out how to recover
4044 error(ERR_PANIC
, "defining with name in expansion");
4045 else if (istk
->mstk
->name
)
4046 error(ERR_FATAL
, "`%%rep' without `%%endrep' within"
4047 " expansion of macro `%s'", istk
->mstk
->name
);
4051 * FIXME: investigate the relationship at this point between
4052 * istk->mstk and l->finishes
4055 MMacro
*m
= istk
->mstk
;
4056 istk
->mstk
= m
->next_active
;
4060 * This was a real macro call, not a %rep, and
4061 * therefore the parameter information needs to
4064 nasm_free(m
->params
);
4065 free_tlist(m
->iline
);
4066 nasm_free(m
->paramlen
);
4067 l
->finishes
->in_progress
= FALSE
;
4072 istk
->expansion
= l
->next
;
4074 list
->downlevel(LIST_MACRO
);
4078 { /* until we get a line we can use */
4080 if (istk
->expansion
)
4081 { /* from a macro expansion */
4083 Line
*l
= istk
->expansion
;
4085 istk
->mstk
->lineno
++;
4087 istk
->expansion
= l
->next
;
4089 p
= detoken(tline
, FALSE
);
4090 list
->line(LIST_MACRO
, p
);
4096 { /* from the current input file */
4097 line
= prepreproc(line
);
4098 tline
= tokenise(line
);
4103 * The current file has ended; work down the istk
4109 error(ERR_FATAL
, "expected `%%endif' before end of file");
4111 list
->downlevel(LIST_INCLUDE
);
4112 src_set_linnum(i
->lineno
);
4113 nasm_free(src_set_fname(i
->fname
));
4121 * We must expand MMacro parameters and MMacro-local labels
4122 * _before_ we plunge into directive processing, to cope
4123 * with things like `%define something %1' such as STRUC
4124 * uses. Unless we're _defining_ a MMacro, in which case
4125 * those tokens should be left alone to go into the
4126 * definition; and unless we're in a non-emitting
4127 * condition, in which case we don't want to meddle with
4130 if (!defining
&& !(istk
->conds
&& !emitting(istk
->conds
->state
)))
4131 tline
= expand_mmac_params(tline
);
4134 * Check the line to see if it's a preprocessor directive.
4136 if (do_directive(tline
) & 1)
4143 * We're defining a multi-line macro. We emit nothing
4145 * shove the tokenised line on to the macro definition.
4147 Line
*l
= nasm_malloc(sizeof(Line
));
4148 l
->next
= defining
->expansion
;
4150 l
->finishes
= FALSE
;
4151 defining
->expansion
= l
;
4154 else if (istk
->conds
&& !emitting(istk
->conds
->state
))
4157 * We're in a non-emitting branch of a condition block.
4158 * Emit nothing at all, not even a blank line: when we
4159 * emerge from the condition we'll give a line-number
4160 * directive so we keep our place correctly.
4165 else if (istk
->mstk
&& !istk
->mstk
->in_progress
)
4168 * We're in a %rep block which has been terminated, so
4169 * we're walking through to the %endrep without
4170 * emitting anything. Emit nothing at all, not even a
4171 * blank line: when we emerge from the %rep block we'll
4172 * give a line-number directive so we keep our place
4180 tline
= expand_smacro(tline
);
4181 if (!expand_mmacro(tline
))
4184 * De-tokenise the line again, and emit it.
4186 line
= detoken(tline
, TRUE
);
4192 continue; /* expand_mmacro calls free_tlist */
4201 pp_cleanup(int pass
)
4207 error(ERR_NONFATAL
, "end of file while still defining macro `%s'",
4209 free_mmacro(defining
);
4213 for (h
= 0; h
< NHASH
; h
++)
4217 MMacro
*m
= mmacros
[h
];
4218 mmacros
[h
] = mmacros
[h
]->next
;
4223 SMacro
*s
= smacros
[h
];
4224 smacros
[h
] = smacros
[h
]->next
;
4226 free_tlist(s
->expansion
);
4235 nasm_free(i
->fname
);
4248 pp_include_path(char *path
)
4252 i
= nasm_malloc(sizeof(IncPath
));
4253 i
->path
= nasm_strdup(path
);
4259 pp_pre_include(char *fname
)
4261 Token
*inc
, *space
, *name
;
4264 name
= new_Token(NULL
, TOK_INTERNAL_STRING
, fname
, 0);
4265 space
= new_Token(name
, TOK_WHITESPACE
, NULL
, 0);
4266 inc
= new_Token(space
, TOK_PREPROC_ID
, "%include", 0);
4268 l
= nasm_malloc(sizeof(Line
));
4271 l
->finishes
= FALSE
;
4276 pp_pre_define(char *definition
)
4282 equals
= strchr(definition
, '=');
4283 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
4284 def
= new_Token(space
, TOK_PREPROC_ID
, "%define", 0);
4287 space
->next
= tokenise(definition
);
4291 l
= nasm_malloc(sizeof(Line
));
4294 l
->finishes
= FALSE
;
4299 pp_pre_undefine(char *definition
)
4304 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
4305 def
= new_Token(space
, TOK_PREPROC_ID
, "%undef", 0);
4307 l
= nasm_malloc(sizeof(Line
));
4310 l
->finishes
= FALSE
;
4315 pp_extra_stdmac(char **macros
)
4317 extrastdmac
= macros
;
4321 make_tok_num(Token
* tok
, long val
)
4324 sprintf(numbuf
, "%ld", val
);
4325 tok
->text
= nasm_strdup(numbuf
);
4326 tok
->type
= TOK_NUMBER
;