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 license given in the file "LICENSE"
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 tokenized lines, either
15 * from a macro expansion
19 * read_line gets raw text from stdmacpos, or predef, or current input file
20 * tokenize 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
56 typedef struct SMacro SMacro
;
57 typedef struct MMacro MMacro
;
58 typedef struct Context Context
;
59 typedef struct Token Token
;
60 typedef struct Blocks Blocks
;
61 typedef struct Line Line
;
62 typedef struct Include Include
;
63 typedef struct Cond Cond
;
64 typedef struct IncPath IncPath
;
67 * Note on the storage of both SMacro and MMacros: the hash table
68 * indexes them case-insensitively, and we then have to go through a
69 * linked list of potential case aliases (and, for MMacros, parameter
70 * ranges); this is to preserve the matching semantics of the earlier
71 * code. If the number of case aliases for a specific macro is a
72 * performance issue, you may want to reconsider your coding style.
76 * Store the definition of a single-line macro.
88 * Store the definition of a multi-line macro. This is also used to
89 * store the interiors of `%rep...%endrep' blocks, which are
90 * effectively self-re-invoking multi-line macros which simply
91 * don't have a name or bother to appear in the hash tables. %rep
92 * blocks are signified by having a NULL `name' field.
94 * In a MMacro describing a `%rep' block, the `in_progress' field
95 * isn't merely boolean, but gives the number of repeats left to
98 * The `next' field is used for storing MMacros in hash tables; the
99 * `next_active' field is for stacking them on istk entries.
101 * When a MMacro is being expanded, `params', `iline', `nparam',
102 * `paramlen', `rotate' and `unique' are local to the invocation.
107 int nparam_min
, nparam_max
;
109 bool plus
; /* is the last parameter greedy? */
110 bool nolist
; /* is this macro listing-inhibited? */
112 Token
*dlist
; /* All defaults as one list */
113 Token
**defaults
; /* Parameter default pointers */
114 int ndefs
; /* number of default parameters */
118 MMacro
*rep_nest
; /* used for nesting %rep */
119 Token
**params
; /* actual parameters */
120 Token
*iline
; /* invocation line */
121 unsigned int nparam
, rotate
;
124 int lineno
; /* Current line number on expansion */
128 * The context stack is composed of a linked list of these.
133 struct hash_table localmac
;
138 * This is the internal form which we break input lines up into.
139 * Typically stored in linked lists.
141 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
142 * necessarily used as-is, but is intended to denote the number of
143 * the substituted parameter. So in the definition
145 * %define a(x,y) ( (x) & ~(y) )
147 * the token representing `x' will have its type changed to
148 * TOK_SMAC_PARAM, but the one representing `y' will be
151 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
152 * which doesn't need quotes around it. Used in the pre-include
153 * mechanism as an alternative to trying to find a sensible type of
154 * quote to use on the filename we were passed.
157 TOK_NONE
= 0, TOK_WHITESPACE
, TOK_COMMENT
, TOK_ID
,
158 TOK_PREPROC_ID
, TOK_STRING
,
159 TOK_NUMBER
, TOK_FLOAT
, TOK_SMAC_END
, TOK_OTHER
,
161 TOK_PREPROC_Q
, TOK_PREPROC_QQ
,
162 TOK_INDIRECT
, /* %[...] */
163 TOK_SMAC_PARAM
, /* MUST BE LAST IN THE LIST!!! */
164 TOK_MAX
= INT_MAX
/* Keep compiler from reducing the range */
171 SMacro
*mac
; /* associated macro for TOK_SMAC_END */
172 size_t len
; /* scratch length field */
173 } a
; /* Auxiliary data */
174 enum pp_token_type type
;
178 * Multi-line macro definitions are stored as a linked list of
179 * these, which is essentially a container to allow several linked
182 * Note that in this module, linked lists are treated as stacks
183 * wherever possible. For this reason, Lines are _pushed_ on to the
184 * `expansion' field in MMacro structures, so that the linked list,
185 * if walked, would give the macro lines in reverse order; this
186 * means that we can walk the list when expanding a macro, and thus
187 * push the lines on to the `expansion' field in _istk_ in reverse
188 * order (so that when popped back off they are in the right
189 * order). It may seem cockeyed, and it relies on my design having
190 * an even number of steps in, but it works...
192 * Some of these structures, rather than being actual lines, are
193 * markers delimiting the end of the expansion of a given macro.
194 * This is for use in the cycle-tracking and %rep-handling code.
195 * Such structures have `finishes' non-NULL, and `first' NULL. All
196 * others have `finishes' NULL, but `first' may still be NULL if
206 * To handle an arbitrary level of file inclusion, we maintain a
207 * stack (ie linked list) of these things.
216 MMacro
*mstk
; /* stack of active macros/reps */
220 * Include search path. This is simply a list of strings which get
221 * prepended, in turn, to the name of an include file, in an
222 * attempt to find the file if it's not in the current directory.
230 * Conditional assembly: we maintain a separate stack of these for
231 * each level of file inclusion. (The only reason we keep the
232 * stacks separate is to ensure that a stray `%endif' in a file
233 * included from within the true branch of a `%if' won't terminate
234 * it and cause confusion: instead, rightly, it'll cause an error.)
242 * These states are for use just after %if or %elif: IF_TRUE
243 * means the condition has evaluated to truth so we are
244 * currently emitting, whereas IF_FALSE means we are not
245 * currently emitting but will start doing so if a %else comes
246 * up. In these states, all directives are admissible: %elif,
247 * %else and %endif. (And of course %if.)
249 COND_IF_TRUE
, COND_IF_FALSE
,
251 * These states come up after a %else: ELSE_TRUE means we're
252 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
253 * any %elif or %else will cause an error.
255 COND_ELSE_TRUE
, COND_ELSE_FALSE
,
257 * These states mean that we're not emitting now, and also that
258 * nothing until %endif will be emitted at all. COND_DONE is
259 * used when we've had our moment of emission
260 * and have now started seeing %elifs. COND_NEVER is used when
261 * the condition construct in question is contained within a
262 * non-emitting branch of a larger condition construct,
263 * or if there is an error.
265 COND_DONE
, COND_NEVER
267 #define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
270 * These defines are used as the possible return values for do_directive
272 #define NO_DIRECTIVE_FOUND 0
273 #define DIRECTIVE_FOUND 1
276 * Condition codes. Note that we use c_ prefix not C_ because C_ is
277 * used in nasm.h for the "real" condition codes. At _this_ level,
278 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
279 * ones, so we need a different enum...
281 static const char * const conditions
[] = {
282 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
283 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
284 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
287 c_A
, c_AE
, c_B
, c_BE
, c_C
, c_CXZ
, c_E
, c_ECXZ
, c_G
, c_GE
, c_L
, c_LE
,
288 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, c_NE
, c_NG
, c_NGE
, c_NL
, c_NLE
, c_NO
,
289 c_NP
, c_NS
, c_NZ
, c_O
, c_P
, c_PE
, c_PO
, c_RCXZ
, c_S
, c_Z
,
292 static const enum pp_conds inverse_ccs
[] = {
293 c_NA
, c_NAE
, c_NB
, c_NBE
, c_NC
, -1, c_NE
, -1, c_NG
, c_NGE
, c_NL
, c_NLE
,
294 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
,
295 c_Z
, c_NO
, c_NP
, c_PO
, c_PE
, -1, c_NS
, c_NZ
301 /* If this is a an IF, ELIF, ELSE or ENDIF keyword */
302 static int is_condition(enum preproc_token arg
)
304 return PP_IS_COND(arg
) || (arg
== PP_ELSE
) || (arg
== PP_ENDIF
);
307 /* For TASM compatibility we need to be able to recognise TASM compatible
308 * conditional compilation directives. Using the NASM pre-processor does
309 * not work, so we look for them specifically from the following list and
310 * then jam in the equivalent NASM directive into the input stream.
314 TM_ARG
, TM_ELIF
, TM_ELSE
, TM_ENDIF
, TM_IF
, TM_IFDEF
, TM_IFDIFI
,
315 TM_IFNDEF
, TM_INCLUDE
, TM_LOCAL
318 static const char * const tasm_directives
[] = {
319 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
320 "ifndef", "include", "local"
323 static int StackSize
= 4;
324 static char *StackPointer
= "ebp";
325 static int ArgOffset
= 8;
326 static int LocalOffset
= 0;
328 static Context
*cstk
;
329 static Include
*istk
;
330 static IncPath
*ipath
= NULL
;
332 static efunc _error
; /* Pointer to client-provided error reporting function */
333 static evalfunc evaluate
;
335 static int pass
; /* HACK: pass 0 = generate dependencies only */
336 static StrList
**dephead
, **deptail
; /* Dependency list */
338 static uint64_t unique
; /* unique identifier numbers */
340 static Line
*predef
= NULL
;
341 static bool do_predef
;
343 static ListGen
*list
;
346 * The current set of multi-line macros we have defined.
348 static struct hash_table mmacros
;
351 * The current set of single-line macros we have defined.
353 static struct hash_table smacros
;
356 * The multi-line macro we are currently defining, or the %rep
357 * block we are currently reading, if any.
359 static MMacro
*defining
;
361 static uint64_t nested_mac_count
;
362 static uint64_t nested_rep_count
;
365 * The number of macro parameters to allocate space for at a time.
367 #define PARAM_DELTA 16
370 * The standard macro set: defined in macros.c in the array nasm_stdmac.
371 * This gives our position in the macro set, when we're processing it.
373 static macros_t
*stdmacpos
;
376 * The extra standard macros that come from the object format, if
379 static macros_t
*extrastdmac
= NULL
;
380 static bool any_extrastdmac
;
383 * Tokens are allocated in blocks to improve speed
385 #define TOKEN_BLOCKSIZE 4096
386 static Token
*freeTokens
= NULL
;
392 static Blocks blocks
= { NULL
, NULL
};
395 * Forward declarations.
397 static Token
*expand_mmac_params(Token
* tline
);
398 static Token
*expand_smacro(Token
* tline
);
399 static Token
*expand_id(Token
* tline
);
400 static Token
*expand_indirect(Token
* tline
, int level
);
401 static Context
*get_ctx(const char *name
, bool all_contexts
);
402 static void make_tok_num(Token
* tok
, int64_t val
);
403 static void error(int severity
, const char *fmt
, ...);
404 static void error_precond(int severity
, const char *fmt
, ...);
405 static void *new_Block(size_t size
);
406 static void delete_Blocks(void);
407 static Token
*new_Token(Token
* next
, enum pp_token_type type
,
408 const char *text
, int txtlen
);
409 static Token
*delete_Token(Token
* t
);
412 * Macros for safe checking of token pointers, avoid *(NULL)
414 #define tok_type_(x,t) ((x) && (x)->type == (t))
415 #define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
416 #define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
417 #define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
419 /* Handle TASM specific directives, which do not contain a % in
420 * front of them. We do it here because I could not find any other
421 * place to do it for the moment, and it is a hack (ideally it would
422 * be nice to be able to use the NASM pre-processor to do it).
424 static char *check_tasm_directive(char *line
)
426 int32_t i
, j
, k
, m
, len
;
427 char *p
= line
, *oldline
, oldchar
;
429 /* Skip whitespace */
430 while (nasm_isspace(*p
) && *p
!= 0)
433 /* Binary search for the directive name */
435 j
= elements(tasm_directives
);
437 while (!nasm_isspace(p
[len
]) && p
[len
] != 0)
444 m
= nasm_stricmp(p
, tasm_directives
[k
]);
446 /* We have found a directive, so jam a % in front of it
447 * so that NASM will then recognise it as one if it's own.
452 line
= nasm_malloc(len
+ 2);
454 if (k
== TM_IFDIFI
) {
455 /* NASM does not recognise IFDIFI, so we convert it to
456 * %ifdef BOGUS. This is not used in NASM comaptible
457 * code, but does need to parse for the TASM macro
460 strcpy(line
+ 1, "ifdef BOGUS");
462 memcpy(line
+ 1, p
, len
+ 1);
477 * The pre-preprocessing stage... This function translates line
478 * number indications as they emerge from GNU cpp (`# lineno "file"
479 * flags') into NASM preprocessor line number indications (`%line
482 static char *prepreproc(char *line
)
485 char *fname
, *oldline
;
487 if (line
[0] == '#' && line
[1] == ' ') {
490 lineno
= atoi(fname
);
491 fname
+= strspn(fname
, "0123456789 ");
494 fnlen
= strcspn(fname
, "\"");
495 line
= nasm_malloc(20 + fnlen
);
496 snprintf(line
, 20 + fnlen
, "%%line %d %.*s", lineno
, fnlen
, fname
);
499 if (tasm_compatible_mode
)
500 return check_tasm_directive(line
);
505 * Free a linked list of tokens.
507 static void free_tlist(Token
* list
)
510 list
= delete_Token(list
);
515 * Free a linked list of lines.
517 static void free_llist(Line
* list
)
523 free_tlist(l
->first
);
531 static void free_mmacro(MMacro
* m
)
534 free_tlist(m
->dlist
);
535 nasm_free(m
->defaults
);
536 free_llist(m
->expansion
);
541 * Free all currently defined macros, and free the hash tables
543 static void free_smacro_table(struct hash_table
*smt
)
547 struct hash_tbl_node
*it
= NULL
;
549 while ((s
= hash_iterate(smt
, &it
, &key
)) != NULL
) {
550 nasm_free((void *)key
);
552 SMacro
*ns
= s
->next
;
554 free_tlist(s
->expansion
);
562 static void free_mmacro_table(struct hash_table
*mmt
)
566 struct hash_tbl_node
*it
= NULL
;
569 while ((m
= hash_iterate(mmt
, &it
, &key
)) != NULL
) {
570 nasm_free((void *)key
);
572 MMacro
*nm
= m
->next
;
580 static void free_macros(void)
582 free_smacro_table(&smacros
);
583 free_mmacro_table(&mmacros
);
587 * Initialize the hash tables
589 static void init_macros(void)
591 hash_init(&smacros
, HASH_LARGE
);
592 hash_init(&mmacros
, HASH_LARGE
);
596 * Pop the context stack.
598 static void ctx_pop(void)
603 free_smacro_table(&c
->localmac
);
609 * Search for a key in the hash index; adding it if necessary
610 * (in which case we initialize the data pointer to NULL.)
613 hash_findi_add(struct hash_table
*hash
, const char *str
)
615 struct hash_insert hi
;
619 r
= hash_findi(hash
, str
, &hi
);
623 strx
= nasm_strdup(str
); /* Use a more efficient allocator here? */
624 return hash_add(&hi
, strx
, NULL
);
628 * Like hash_findi, but returns the data element rather than a pointer
629 * to it. Used only when not adding a new element, hence no third
633 hash_findix(struct hash_table
*hash
, const char *str
)
637 p
= hash_findi(hash
, str
, NULL
);
638 return p
? *p
: NULL
;
641 #define BUF_DELTA 512
643 * Read a line from the top file in istk, handling multiple CR/LFs
644 * at the end of the line read, and handling spurious ^Zs. Will
645 * return lines from the standard macro set if this has not already
648 static char *read_line(void)
650 char *buffer
, *p
, *q
;
651 int bufsize
, continued_count
;
655 const unsigned char *p
= stdmacpos
;
660 len
+= pp_directives_len
[c
-0x80]+1;
664 ret
= nasm_malloc(len
+1);
666 while ((c
= *stdmacpos
++)) {
668 memcpy(q
, pp_directives
[c
-0x80], pp_directives_len
[c
-0x80]);
669 q
+= pp_directives_len
[c
-0x80];
679 /* This was the last of the standard macro chain... */
681 if (any_extrastdmac
) {
682 stdmacpos
= extrastdmac
;
683 any_extrastdmac
= false;
684 } else if (do_predef
) {
686 Token
*head
, **tail
, *t
;
689 * Nasty hack: here we push the contents of
690 * `predef' on to the top-level expansion stack,
691 * since this is the most convenient way to
692 * implement the pre-include and pre-define
695 for (pd
= predef
; pd
; pd
= pd
->next
) {
698 for (t
= pd
->first
; t
; t
= t
->next
) {
699 *tail
= new_Token(NULL
, t
->type
, t
->text
, 0);
700 tail
= &(*tail
)->next
;
702 l
= nasm_malloc(sizeof(Line
));
703 l
->next
= istk
->expansion
;
715 buffer
= nasm_malloc(BUF_DELTA
);
719 q
= fgets(p
, bufsize
- (p
- buffer
), istk
->fp
);
723 if (p
> buffer
&& p
[-1] == '\n') {
724 /* Convert backslash-CRLF line continuation sequences into
725 nothing at all (for DOS and Windows) */
726 if (((p
- 2) > buffer
) && (p
[-3] == '\\') && (p
[-2] == '\r')) {
731 /* Also convert backslash-LF line continuation sequences into
732 nothing at all (for Unix) */
733 else if (((p
- 1) > buffer
) && (p
[-2] == '\\')) {
741 if (p
- buffer
> bufsize
- 10) {
742 int32_t offset
= p
- buffer
;
743 bufsize
+= BUF_DELTA
;
744 buffer
= nasm_realloc(buffer
, bufsize
);
745 p
= buffer
+ offset
; /* prevent stale-pointer problems */
749 if (!q
&& p
== buffer
) {
754 src_set_linnum(src_get_linnum() + istk
->lineinc
+
755 (continued_count
* istk
->lineinc
));
758 * Play safe: remove CRs as well as LFs, if any of either are
759 * present at the end of the line.
761 while (--p
>= buffer
&& (*p
== '\n' || *p
== '\r'))
765 * Handle spurious ^Z, which may be inserted into source files
766 * by some file transfer utilities.
768 buffer
[strcspn(buffer
, "\032")] = '\0';
770 list
->line(LIST_READ
, buffer
);
776 * Tokenize a line of text. This is a very simple process since we
777 * don't need to parse the value out of e.g. numeric tokens: we
778 * simply split one string into many.
780 static Token
*tokenize(char *line
)
783 enum pp_token_type type
;
785 Token
*t
, **tail
= &list
;
791 if (nasm_isdigit(*p
) ||
792 ((*p
== '-' || *p
== '+') && nasm_isdigit(p
[1])) ||
793 ((*p
== '+') && (nasm_isspace(p
[1]) || !p
[1]))) {
797 while (nasm_isdigit(*p
));
798 type
= TOK_PREPROC_ID
;
799 } else if (*p
== '{') {
801 while (*p
&& *p
!= '}') {
808 type
= TOK_PREPROC_ID
;
809 } else if (*p
== '[') {
811 line
+= 2; /* Skip the leading %[ */
817 } else if (*p
== '%' && p
[1] == '[') {
825 } else if (*p
== '?') {
826 type
= TOK_PREPROC_Q
; /* %? */
829 type
= TOK_PREPROC_QQ
; /* %?? */
832 } else if (isidchar(*p
) ||
833 ((*p
== '!' || *p
== '%' || *p
== '$') &&
838 while (isidchar(*p
));
839 type
= TOK_PREPROC_ID
;
845 } else if (isidstart(*p
) || (*p
== '$' && isidstart(p
[1]))) {
848 while (*p
&& isidchar(*p
))
850 } else if (*p
== '\'' || *p
== '"' || *p
== '`') {
855 p
= nasm_skip_string(p
);
860 error(ERR_WARNING
|ERR_PASS1
, "unterminated string");
861 /* Handling unterminated strings by UNV */
864 } else if (isnumstart(*p
)) {
866 bool is_float
= false;
882 if (!is_hex
&& (c
== 'e' || c
== 'E')) {
884 if (*p
== '+' || *p
== '-') {
885 /* e can only be followed by +/- if it is either a
886 prefixed hex number or a floating-point number */
890 } else if (c
== 'H' || c
== 'h' || c
== 'X' || c
== 'x') {
892 } else if (c
== 'P' || c
== 'p') {
894 if (*p
== '+' || *p
== '-')
896 } else if (isnumchar(c
) || c
== '_')
899 /* we need to deal with consequences of the legacy
900 parser, like "1.nolist" being two tokens
901 (TOK_NUMBER, TOK_ID) here; at least give it
902 a shot for now. In the future, we probably need
903 a flex-based scanner with proper pattern matching
904 to do it as well as it can be done. Nothing in
905 the world is going to help the person who wants
906 0x123.p16 interpreted as two tokens, though. */
911 if (nasm_isdigit(*r
) || (is_hex
&& nasm_isxdigit(*r
)) ||
912 (!is_hex
&& (*r
== 'e' || *r
== 'E')) ||
913 (*r
== 'p' || *r
== 'P')) {
917 break; /* Terminate the token */
921 p
--; /* Point to first character beyond number */
923 if (has_e
&& !is_hex
) {
924 /* 1e13 is floating-point, but 1e13h is not */
928 type
= is_float
? TOK_FLOAT
: TOK_NUMBER
;
929 } else if (nasm_isspace(*p
)) {
930 type
= TOK_WHITESPACE
;
932 while (*p
&& nasm_isspace(*p
))
935 * Whitespace just before end-of-line is discarded by
936 * pretending it's a comment; whitespace just before a
937 * comment gets lumped into the comment.
939 if (!*p
|| *p
== ';') {
944 } else if (*p
== ';') {
950 * Anything else is an operator of some kind. We check
951 * for all the double-character operators (>>, <<, //,
952 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
953 * else is a single-character operator.
956 if ((p
[0] == '>' && p
[1] == '>') ||
957 (p
[0] == '<' && p
[1] == '<') ||
958 (p
[0] == '/' && p
[1] == '/') ||
959 (p
[0] == '<' && p
[1] == '=') ||
960 (p
[0] == '>' && p
[1] == '=') ||
961 (p
[0] == '=' && p
[1] == '=') ||
962 (p
[0] == '!' && p
[1] == '=') ||
963 (p
[0] == '<' && p
[1] == '>') ||
964 (p
[0] == '&' && p
[1] == '&') ||
965 (p
[0] == '|' && p
[1] == '|') ||
966 (p
[0] == '^' && p
[1] == '^')) {
972 /* Handling unterminated string by UNV */
975 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
976 t->text[p-line] = *line;
980 if (type
!= TOK_COMMENT
) {
981 *tail
= t
= new_Token(NULL
, type
, line
, p
- line
);
990 * this function allocates a new managed block of memory and
991 * returns a pointer to the block. The managed blocks are
992 * deleted only all at once by the delete_Blocks function.
994 static void *new_Block(size_t size
)
998 /* first, get to the end of the linked list */
1001 /* now allocate the requested chunk */
1002 b
->chunk
= nasm_malloc(size
);
1004 /* now allocate a new block for the next request */
1005 b
->next
= nasm_malloc(sizeof(Blocks
));
1006 /* and initialize the contents of the new block */
1007 b
->next
->next
= NULL
;
1008 b
->next
->chunk
= NULL
;
1013 * this function deletes all managed blocks of memory
1015 static void delete_Blocks(void)
1017 Blocks
*a
, *b
= &blocks
;
1020 * keep in mind that the first block, pointed to by blocks
1021 * is a static and not dynamically allocated, so we don't
1026 nasm_free(b
->chunk
);
1035 * this function creates a new Token and passes a pointer to it
1036 * back to the caller. It sets the type and text elements, and
1037 * also the a.mac and next elements to NULL.
1039 static Token
*new_Token(Token
* next
, enum pp_token_type type
,
1040 const char *text
, int txtlen
)
1045 if (freeTokens
== NULL
) {
1046 freeTokens
= (Token
*) new_Block(TOKEN_BLOCKSIZE
* sizeof(Token
));
1047 for (i
= 0; i
< TOKEN_BLOCKSIZE
- 1; i
++)
1048 freeTokens
[i
].next
= &freeTokens
[i
+ 1];
1049 freeTokens
[i
].next
= NULL
;
1052 freeTokens
= t
->next
;
1056 if (type
== TOK_WHITESPACE
|| text
== NULL
) {
1060 txtlen
= strlen(text
);
1061 t
->text
= nasm_malloc(txtlen
+1);
1062 memcpy(t
->text
, text
, txtlen
);
1063 t
->text
[txtlen
] = '\0';
1068 static Token
*delete_Token(Token
* t
)
1070 Token
*next
= t
->next
;
1072 t
->next
= freeTokens
;
1078 * Convert a line of tokens back into text.
1079 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1080 * will be transformed into ..@ctxnum.xxx
1082 static char *detoken(Token
* tlist
, bool expand_locals
)
1090 for (t
= tlist
; t
; t
= t
->next
) {
1091 if (t
->type
== TOK_PREPROC_ID
&& t
->text
[1] == '!') {
1092 char *p
= getenv(t
->text
+ 2);
1095 t
->text
= nasm_strdup(p
);
1099 /* Expand local macros here and not during preprocessing */
1100 if (expand_locals
&&
1101 t
->type
== TOK_PREPROC_ID
&& t
->text
&&
1102 t
->text
[0] == '%' && t
->text
[1] == '$') {
1103 Context
*ctx
= get_ctx(t
->text
, false);
1106 char *p
, *q
= t
->text
+ 2;
1108 q
+= strspn(q
, "$");
1109 snprintf(buffer
, sizeof(buffer
), "..@%"PRIu32
".", ctx
->number
);
1110 p
= nasm_strcat(buffer
, q
);
1115 if (t
->type
== TOK_WHITESPACE
) {
1117 } else if (t
->text
) {
1118 len
+= strlen(t
->text
);
1121 p
= line
= nasm_malloc(len
+ 1);
1122 for (t
= tlist
; t
; t
= t
->next
) {
1123 if (t
->type
== TOK_WHITESPACE
) {
1125 } else if (t
->text
) {
1136 * A scanner, suitable for use by the expression evaluator, which
1137 * operates on a line of Tokens. Expects a pointer to a pointer to
1138 * the first token in the line to be passed in as its private_data
1141 * FIX: This really needs to be unified with stdscan.
1143 static int ppscan(void *private_data
, struct tokenval
*tokval
)
1145 Token
**tlineptr
= private_data
;
1147 char ourcopy
[MAX_KEYWORD
+1], *p
, *r
, *s
;
1151 *tlineptr
= tline
? tline
->next
: NULL
;
1153 while (tline
&& (tline
->type
== TOK_WHITESPACE
||
1154 tline
->type
== TOK_COMMENT
));
1157 return tokval
->t_type
= TOKEN_EOS
;
1159 tokval
->t_charptr
= tline
->text
;
1161 if (tline
->text
[0] == '$' && !tline
->text
[1])
1162 return tokval
->t_type
= TOKEN_HERE
;
1163 if (tline
->text
[0] == '$' && tline
->text
[1] == '$' && !tline
->text
[2])
1164 return tokval
->t_type
= TOKEN_BASE
;
1166 if (tline
->type
== TOK_ID
) {
1167 p
= tokval
->t_charptr
= tline
->text
;
1169 tokval
->t_charptr
++;
1170 return tokval
->t_type
= TOKEN_ID
;
1173 for (r
= p
, s
= ourcopy
; *r
; r
++) {
1174 if (r
>= p
+MAX_KEYWORD
)
1175 return tokval
->t_type
= TOKEN_ID
; /* Not a keyword */
1176 *s
++ = nasm_tolower(*r
);
1179 /* right, so we have an identifier sitting in temp storage. now,
1180 * is it actually a register or instruction name, or what? */
1181 return nasm_token_hash(ourcopy
, tokval
);
1184 if (tline
->type
== TOK_NUMBER
) {
1186 tokval
->t_integer
= readnum(tline
->text
, &rn_error
);
1187 tokval
->t_charptr
= tline
->text
;
1189 return tokval
->t_type
= TOKEN_ERRNUM
;
1191 return tokval
->t_type
= TOKEN_NUM
;
1194 if (tline
->type
== TOK_FLOAT
) {
1195 return tokval
->t_type
= TOKEN_FLOAT
;
1198 if (tline
->type
== TOK_STRING
) {
1201 bq
= tline
->text
[0];
1202 tokval
->t_charptr
= tline
->text
;
1203 tokval
->t_inttwo
= nasm_unquote(tline
->text
, &ep
);
1205 if (ep
[0] != bq
|| ep
[1] != '\0')
1206 return tokval
->t_type
= TOKEN_ERRSTR
;
1208 return tokval
->t_type
= TOKEN_STR
;
1211 if (tline
->type
== TOK_OTHER
) {
1212 if (!strcmp(tline
->text
, "<<"))
1213 return tokval
->t_type
= TOKEN_SHL
;
1214 if (!strcmp(tline
->text
, ">>"))
1215 return tokval
->t_type
= TOKEN_SHR
;
1216 if (!strcmp(tline
->text
, "//"))
1217 return tokval
->t_type
= TOKEN_SDIV
;
1218 if (!strcmp(tline
->text
, "%%"))
1219 return tokval
->t_type
= TOKEN_SMOD
;
1220 if (!strcmp(tline
->text
, "=="))
1221 return tokval
->t_type
= TOKEN_EQ
;
1222 if (!strcmp(tline
->text
, "<>"))
1223 return tokval
->t_type
= TOKEN_NE
;
1224 if (!strcmp(tline
->text
, "!="))
1225 return tokval
->t_type
= TOKEN_NE
;
1226 if (!strcmp(tline
->text
, "<="))
1227 return tokval
->t_type
= TOKEN_LE
;
1228 if (!strcmp(tline
->text
, ">="))
1229 return tokval
->t_type
= TOKEN_GE
;
1230 if (!strcmp(tline
->text
, "&&"))
1231 return tokval
->t_type
= TOKEN_DBL_AND
;
1232 if (!strcmp(tline
->text
, "^^"))
1233 return tokval
->t_type
= TOKEN_DBL_XOR
;
1234 if (!strcmp(tline
->text
, "||"))
1235 return tokval
->t_type
= TOKEN_DBL_OR
;
1239 * We have no other options: just return the first character of
1242 return tokval
->t_type
= tline
->text
[0];
1246 * Compare a string to the name of an existing macro; this is a
1247 * simple wrapper which calls either strcmp or nasm_stricmp
1248 * depending on the value of the `casesense' parameter.
1250 static int mstrcmp(const char *p
, const char *q
, bool casesense
)
1252 return casesense
? strcmp(p
, q
) : nasm_stricmp(p
, q
);
1256 * Compare a string to the name of an existing macro; this is a
1257 * simple wrapper which calls either strcmp or nasm_stricmp
1258 * depending on the value of the `casesense' parameter.
1260 static int mmemcmp(const char *p
, const char *q
, size_t l
, bool casesense
)
1262 return casesense
? memcmp(p
, q
, l
) : nasm_memicmp(p
, q
, l
);
1266 * Return the Context structure associated with a %$ token. Return
1267 * NULL, having _already_ reported an error condition, if the
1268 * context stack isn't deep enough for the supplied number of $
1270 * If all_contexts == true, contexts that enclose current are
1271 * also scanned for such smacro, until it is found; if not -
1272 * only the context that directly results from the number of $'s
1273 * in variable's name.
1275 static Context
*get_ctx(const char *name
, bool all_contexts
)
1281 if (!name
|| name
[0] != '%' || name
[1] != '$')
1285 error(ERR_NONFATAL
, "`%s': context stack is empty", name
);
1289 for (i
= strspn(name
+ 2, "$"), ctx
= cstk
; (i
> 0) && ctx
; i
--) {
1291 /* i--; Lino - 02/25/02 */
1294 error(ERR_NONFATAL
, "`%s': context stack is only"
1295 " %d level%s deep", name
, i
- 1, (i
== 2 ? "" : "s"));
1302 /* Search for this smacro in found context */
1303 m
= hash_findix(&ctx
->localmac
, name
);
1305 if (!mstrcmp(m
->name
, name
, m
->casesense
))
1316 * Check to see if a file is already in a string list
1318 static bool in_list(const StrList
*list
, const char *str
)
1321 if (!strcmp(list
->str
, str
))
1329 * Open an include file. This routine must always return a valid
1330 * file pointer if it returns - it's responsible for throwing an
1331 * ERR_FATAL and bombing out completely if not. It should also try
1332 * the include path one by one until it finds the file or reaches
1333 * the end of the path.
1335 static FILE *inc_fopen(const char *file
, StrList
**dhead
, StrList
***dtail
,
1340 IncPath
*ip
= ipath
;
1341 int len
= strlen(file
);
1342 size_t prefix_len
= 0;
1346 sl
= nasm_malloc(prefix_len
+len
+1+sizeof sl
->next
);
1347 memcpy(sl
->str
, prefix
, prefix_len
);
1348 memcpy(sl
->str
+prefix_len
, file
, len
+1);
1349 fp
= fopen(sl
->str
, "r");
1350 if (fp
&& dhead
&& !in_list(*dhead
, sl
->str
)) {
1368 prefix_len
= strlen(prefix
);
1370 /* -MG given and file not found */
1371 if (dhead
&& !in_list(*dhead
, file
)) {
1372 sl
= nasm_malloc(len
+1+sizeof sl
->next
);
1374 strcpy(sl
->str
, file
);
1382 error(ERR_FATAL
, "unable to open include file `%s'", file
);
1383 return NULL
; /* never reached - placate compilers */
1387 * Determine if we should warn on defining a single-line macro of
1388 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
1389 * return true if _any_ single-line macro of that name is defined.
1390 * Otherwise, will return true if a single-line macro with either
1391 * `nparam' or no parameters is defined.
1393 * If a macro with precisely the right number of parameters is
1394 * defined, or nparam is -1, the address of the definition structure
1395 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
1396 * is NULL, no action will be taken regarding its contents, and no
1399 * Note that this is also called with nparam zero to resolve
1402 * If you already know which context macro belongs to, you can pass
1403 * the context pointer as first parameter; if you won't but name begins
1404 * with %$ the context will be automatically computed. If all_contexts
1405 * is true, macro will be searched in outer contexts as well.
1408 smacro_defined(Context
* ctx
, const char *name
, int nparam
, SMacro
** defn
,
1411 struct hash_table
*smtbl
;
1415 smtbl
= &ctx
->localmac
;
1416 } else if (name
[0] == '%' && name
[1] == '$') {
1418 ctx
= get_ctx(name
, false);
1420 return false; /* got to return _something_ */
1421 smtbl
= &ctx
->localmac
;
1425 m
= (SMacro
*) hash_findix(smtbl
, name
);
1428 if (!mstrcmp(m
->name
, name
, m
->casesense
&& nocase
) &&
1429 (nparam
<= 0 || m
->nparam
== 0 || nparam
== (int) m
->nparam
)) {
1431 if (nparam
== (int) m
->nparam
|| nparam
== -1)
1445 * Count and mark off the parameters in a multi-line macro call.
1446 * This is called both from within the multi-line macro expansion
1447 * code, and also to mark off the default parameters when provided
1448 * in a %macro definition line.
1450 static void count_mmac_params(Token
* t
, int *nparam
, Token
*** params
)
1452 int paramsize
, brace
;
1454 *nparam
= paramsize
= 0;
1457 /* +1: we need space for the final NULL */
1458 if (*nparam
+1 >= paramsize
) {
1459 paramsize
+= PARAM_DELTA
;
1460 *params
= nasm_realloc(*params
, sizeof(**params
) * paramsize
);
1464 if (tok_is_(t
, "{"))
1466 (*params
)[(*nparam
)++] = t
;
1467 while (tok_isnt_(t
, brace
? "}" : ","))
1469 if (t
) { /* got a comma/brace */
1473 * Now we've found the closing brace, look further
1477 if (tok_isnt_(t
, ",")) {
1479 "braces do not enclose all of macro parameter");
1480 while (tok_isnt_(t
, ","))
1484 t
= t
->next
; /* eat the comma */
1491 * Determine whether one of the various `if' conditions is true or
1494 * We must free the tline we get passed.
1496 static bool if_condition(Token
* tline
, enum preproc_token ct
)
1498 enum pp_conditional i
= PP_COND(ct
);
1500 Token
*t
, *tt
, **tptr
, *origline
;
1501 struct tokenval tokval
;
1503 enum pp_token_type needtype
;
1509 j
= false; /* have we matched yet? */
1514 if (tline
->type
!= TOK_ID
) {
1516 "`%s' expects context identifiers", pp_directives
[ct
]);
1517 free_tlist(origline
);
1520 if (cstk
&& cstk
->name
&& !nasm_stricmp(tline
->text
, cstk
->name
))
1522 tline
= tline
->next
;
1527 j
= false; /* have we matched yet? */
1530 if (!tline
|| (tline
->type
!= TOK_ID
&&
1531 (tline
->type
!= TOK_PREPROC_ID
||
1532 tline
->text
[1] != '$'))) {
1534 "`%s' expects macro identifiers", pp_directives
[ct
]);
1537 if (smacro_defined(NULL
, tline
->text
, 0, NULL
, true))
1539 tline
= tline
->next
;
1545 tline
= expand_smacro(tline
);
1547 while (tok_isnt_(tt
, ","))
1551 "`%s' expects two comma-separated arguments",
1556 j
= true; /* assume equality unless proved not */
1557 while ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) && tt
) {
1558 if (tt
->type
== TOK_OTHER
&& !strcmp(tt
->text
, ",")) {
1559 error(ERR_NONFATAL
, "`%s': more than one comma on line",
1563 if (t
->type
== TOK_WHITESPACE
) {
1567 if (tt
->type
== TOK_WHITESPACE
) {
1571 if (tt
->type
!= t
->type
) {
1572 j
= false; /* found mismatching tokens */
1575 /* When comparing strings, need to unquote them first */
1576 if (t
->type
== TOK_STRING
) {
1577 size_t l1
= nasm_unquote(t
->text
, NULL
);
1578 size_t l2
= nasm_unquote(tt
->text
, NULL
);
1584 if (mmemcmp(t
->text
, tt
->text
, l1
, i
== PPC_IFIDN
)) {
1588 } else if (mstrcmp(tt
->text
, t
->text
, i
== PPC_IFIDN
) != 0) {
1589 j
= false; /* found mismatching tokens */
1596 if ((t
->type
!= TOK_OTHER
|| strcmp(t
->text
, ",")) || tt
)
1597 j
= false; /* trailing gunk on one end or other */
1603 MMacro searching
, *mmac
;
1605 tline
= tline
->next
;
1607 tline
= expand_id(tline
);
1608 if (!tok_type_(tline
, TOK_ID
)) {
1610 "`%s' expects a macro name", pp_directives
[ct
]);
1613 searching
.name
= nasm_strdup(tline
->text
);
1614 searching
.casesense
= true;
1615 searching
.plus
= false;
1616 searching
.nolist
= false;
1617 searching
.in_progress
= 0;
1618 searching
.rep_nest
= NULL
;
1619 searching
.nparam_min
= 0;
1620 searching
.nparam_max
= INT_MAX
;
1621 tline
= expand_smacro(tline
->next
);
1624 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
1626 "`%s' expects a parameter count or nothing",
1629 searching
.nparam_min
= searching
.nparam_max
=
1630 readnum(tline
->text
, &j
);
1633 "unable to parse parameter count `%s'",
1636 if (tline
&& tok_is_(tline
->next
, "-")) {
1637 tline
= tline
->next
->next
;
1638 if (tok_is_(tline
, "*"))
1639 searching
.nparam_max
= INT_MAX
;
1640 else if (!tok_type_(tline
, TOK_NUMBER
))
1642 "`%s' expects a parameter count after `-'",
1645 searching
.nparam_max
= readnum(tline
->text
, &j
);
1648 "unable to parse parameter count `%s'",
1650 if (searching
.nparam_min
> searching
.nparam_max
)
1652 "minimum parameter count exceeds maximum");
1655 if (tline
&& tok_is_(tline
->next
, "+")) {
1656 tline
= tline
->next
;
1657 searching
.plus
= true;
1659 mmac
= (MMacro
*) hash_findix(&mmacros
, searching
.name
);
1661 if (!strcmp(mmac
->name
, searching
.name
) &&
1662 (mmac
->nparam_min
<= searching
.nparam_max
1664 && (searching
.nparam_min
<= mmac
->nparam_max
1671 if(tline
&& tline
->next
)
1672 error(ERR_WARNING
|ERR_PASS1
,
1673 "trailing garbage after %%ifmacro ignored");
1674 nasm_free(searching
.name
);
1683 needtype
= TOK_NUMBER
;
1686 needtype
= TOK_STRING
;
1690 t
= tline
= expand_smacro(tline
);
1692 while (tok_type_(t
, TOK_WHITESPACE
) ||
1693 (needtype
== TOK_NUMBER
&&
1694 tok_type_(t
, TOK_OTHER
) &&
1695 (t
->text
[0] == '-' || t
->text
[0] == '+') &&
1699 j
= tok_type_(t
, needtype
);
1703 t
= tline
= expand_smacro(tline
);
1704 while (tok_type_(t
, TOK_WHITESPACE
))
1709 t
= t
->next
; /* Skip the actual token */
1710 while (tok_type_(t
, TOK_WHITESPACE
))
1712 j
= !t
; /* Should be nothing left */
1717 t
= tline
= expand_smacro(tline
);
1718 while (tok_type_(t
, TOK_WHITESPACE
))
1721 j
= !t
; /* Should be empty */
1725 t
= tline
= expand_smacro(tline
);
1727 tokval
.t_type
= TOKEN_INVALID
;
1728 evalresult
= evaluate(ppscan
, tptr
, &tokval
,
1729 NULL
, pass
| CRITICAL
, error
, NULL
);
1733 error(ERR_WARNING
|ERR_PASS1
,
1734 "trailing garbage after expression ignored");
1735 if (!is_simple(evalresult
)) {
1737 "non-constant value given to `%s'", pp_directives
[ct
]);
1740 j
= reloc_value(evalresult
) != 0;
1745 "preprocessor directive `%s' not yet implemented",
1750 free_tlist(origline
);
1751 return j
^ PP_NEGATIVE(ct
);
1754 free_tlist(origline
);
1759 * Common code for defining an smacro
1761 static bool define_smacro(Context
*ctx
, char *mname
, bool casesense
,
1762 int nparam
, Token
*expansion
)
1764 SMacro
*smac
, **smhead
;
1765 struct hash_table
*smtbl
;
1767 if (smacro_defined(ctx
, mname
, nparam
, &smac
, casesense
)) {
1769 error(ERR_WARNING
|ERR_PASS1
,
1770 "single-line macro `%s' defined both with and"
1771 " without parameters", mname
);
1773 /* Some instances of the old code considered this a failure,
1774 some others didn't. What is the right thing to do here? */
1775 free_tlist(expansion
);
1776 return false; /* Failure */
1779 * We're redefining, so we have to take over an
1780 * existing SMacro structure. This means freeing
1781 * what was already in it.
1783 nasm_free(smac
->name
);
1784 free_tlist(smac
->expansion
);
1787 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
1788 smhead
= (SMacro
**) hash_findi_add(smtbl
, mname
);
1789 smac
= nasm_malloc(sizeof(SMacro
));
1790 smac
->next
= *smhead
;
1793 smac
->name
= nasm_strdup(mname
);
1794 smac
->casesense
= casesense
;
1795 smac
->nparam
= nparam
;
1796 smac
->expansion
= expansion
;
1797 smac
->in_progress
= false;
1798 return true; /* Success */
1802 * Undefine an smacro
1804 static void undef_smacro(Context
*ctx
, const char *mname
)
1806 SMacro
**smhead
, *s
, **sp
;
1807 struct hash_table
*smtbl
;
1809 smtbl
= ctx
? &ctx
->localmac
: &smacros
;
1810 smhead
= (SMacro
**)hash_findi(smtbl
, mname
, NULL
);
1814 * We now have a macro name... go hunt for it.
1817 while ((s
= *sp
) != NULL
) {
1818 if (!mstrcmp(s
->name
, mname
, s
->casesense
)) {
1821 free_tlist(s
->expansion
);
1831 * Parse a mmacro specification.
1833 static bool parse_mmacro_spec(Token
*tline
, MMacro
*def
, const char *directive
)
1837 tline
= tline
->next
;
1839 tline
= expand_id(tline
);
1840 if (!tok_type_(tline
, TOK_ID
)) {
1841 error(ERR_NONFATAL
, "`%s' expects a macro name", directive
);
1845 def
->name
= nasm_strdup(tline
->text
);
1847 def
->nolist
= false;
1848 def
->in_progress
= 0;
1849 def
->rep_nest
= NULL
;
1850 def
->nparam_min
= 0;
1851 def
->nparam_max
= 0;
1853 tline
= expand_smacro(tline
->next
);
1855 if (!tok_type_(tline
, TOK_NUMBER
)) {
1856 error(ERR_NONFATAL
, "`%s' expects a parameter count", directive
);
1858 def
->nparam_min
= def
->nparam_max
=
1859 readnum(tline
->text
, &err
);
1862 "unable to parse parameter count `%s'", tline
->text
);
1864 if (tline
&& tok_is_(tline
->next
, "-")) {
1865 tline
= tline
->next
->next
;
1866 if (tok_is_(tline
, "*")) {
1867 def
->nparam_max
= INT_MAX
;
1868 } else if (!tok_type_(tline
, TOK_NUMBER
)) {
1870 "`%s' expects a parameter count after `-'", directive
);
1872 def
->nparam_max
= readnum(tline
->text
, &err
);
1874 error(ERR_NONFATAL
, "unable to parse parameter count `%s'",
1877 if (def
->nparam_min
> def
->nparam_max
) {
1878 error(ERR_NONFATAL
, "minimum parameter count exceeds maximum");
1882 if (tline
&& tok_is_(tline
->next
, "+")) {
1883 tline
= tline
->next
;
1886 if (tline
&& tok_type_(tline
->next
, TOK_ID
) &&
1887 !nasm_stricmp(tline
->next
->text
, ".nolist")) {
1888 tline
= tline
->next
;
1893 * Handle default parameters.
1895 if (tline
&& tline
->next
) {
1896 def
->dlist
= tline
->next
;
1898 count_mmac_params(def
->dlist
, &def
->ndefs
, &def
->defaults
);
1901 def
->defaults
= NULL
;
1903 def
->expansion
= NULL
;
1906 def
->ndefs
> def
->nparam_max
- def
->nparam_min
&&
1908 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MDP
,
1909 "too many default macro parameters");
1916 * Decode a size directive
1918 static int parse_size(const char *str
) {
1919 static const char *size_names
[] =
1920 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
1921 static const int sizes
[] =
1922 { 0, 1, 4, 16, 8, 10, 2, 32 };
1924 return sizes
[bsii(str
, size_names
, elements(size_names
))+1];
1928 * find and process preprocessor directive in passed line
1929 * Find out if a line contains a preprocessor directive, and deal
1932 * If a directive _is_ found, it is the responsibility of this routine
1933 * (and not the caller) to free_tlist() the line.
1935 * @param tline a pointer to the current tokeninzed line linked list
1936 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
1939 static int do_directive(Token
* tline
)
1941 enum preproc_token i
;
1949 char *p
, *pp
, *mname
;
1953 MMacro
*mmac
, **mmhead
;
1954 Token
*t
, *tt
, *param_start
, *macro_start
, *last
, **tptr
, *origline
;
1956 struct tokenval tokval
;
1958 MMacro
*tmp_defining
; /* Used when manipulating rep_nest */
1966 if (!tline
|| !tok_type_(tline
, TOK_PREPROC_ID
) ||
1967 (tline
->text
[1] == '%' || tline
->text
[1] == '$'
1968 || tline
->text
[1] == '!'))
1969 return NO_DIRECTIVE_FOUND
;
1971 i
= pp_token_hash(tline
->text
);
1974 * If we're in a non-emitting branch of a condition construct,
1975 * or walking to the end of an already terminated %rep block,
1976 * we should ignore all directives except for condition
1979 if (((istk
->conds
&& !emitting(istk
->conds
->state
)) ||
1980 (istk
->mstk
&& !istk
->mstk
->in_progress
)) && !is_condition(i
)) {
1981 return NO_DIRECTIVE_FOUND
;
1985 * If we're defining a macro or reading a %rep block, we should
1986 * ignore all directives except for %macro/%imacro (which nest),
1987 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
1988 * If we're in a %rep block, another %rep nests, so should be let through.
1990 if (defining
&& i
!= PP_MACRO
&& i
!= PP_IMACRO
&&
1991 i
!= PP_ENDMACRO
&& i
!= PP_ENDM
&&
1992 (defining
->name
|| (i
!= PP_ENDREP
&& i
!= PP_REP
))) {
1993 return NO_DIRECTIVE_FOUND
;
1997 if (i
== PP_MACRO
|| i
== PP_IMACRO
) {
1999 return NO_DIRECTIVE_FOUND
;
2000 } else if (nested_mac_count
> 0) {
2001 if (i
== PP_ENDMACRO
) {
2003 return NO_DIRECTIVE_FOUND
;
2006 if (!defining
->name
) {
2009 return NO_DIRECTIVE_FOUND
;
2010 } else if (nested_rep_count
> 0) {
2011 if (i
== PP_ENDREP
) {
2013 return NO_DIRECTIVE_FOUND
;
2021 error(ERR_NONFATAL
, "unknown preprocessor directive `%s'",
2023 return NO_DIRECTIVE_FOUND
; /* didn't get it */
2026 /* Directive to tell NASM what the default stack size is. The
2027 * default is for a 16-bit stack, and this can be overriden with
2029 * the following form:
2031 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2033 tline
= tline
->next
;
2034 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2035 tline
= tline
->next
;
2036 if (!tline
|| tline
->type
!= TOK_ID
) {
2037 error(ERR_NONFATAL
, "`%%stacksize' missing size parameter");
2038 free_tlist(origline
);
2039 return DIRECTIVE_FOUND
;
2041 if (nasm_stricmp(tline
->text
, "flat") == 0) {
2042 /* All subsequent ARG directives are for a 32-bit stack */
2044 StackPointer
= "ebp";
2047 } else if (nasm_stricmp(tline
->text
, "flat64") == 0) {
2048 /* All subsequent ARG directives are for a 64-bit stack */
2050 StackPointer
= "rbp";
2053 } else if (nasm_stricmp(tline
->text
, "large") == 0) {
2054 /* All subsequent ARG directives are for a 16-bit stack,
2055 * far function call.
2058 StackPointer
= "bp";
2061 } else if (nasm_stricmp(tline
->text
, "small") == 0) {
2062 /* All subsequent ARG directives are for a 16-bit stack,
2063 * far function call. We don't support near functions.
2066 StackPointer
= "bp";
2070 error(ERR_NONFATAL
, "`%%stacksize' invalid size type");
2071 free_tlist(origline
);
2072 return DIRECTIVE_FOUND
;
2074 free_tlist(origline
);
2075 return DIRECTIVE_FOUND
;
2078 /* TASM like ARG directive to define arguments to functions, in
2079 * the following form:
2081 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2085 char *arg
, directive
[256];
2086 int size
= StackSize
;
2088 /* Find the argument name */
2089 tline
= tline
->next
;
2090 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2091 tline
= tline
->next
;
2092 if (!tline
|| tline
->type
!= TOK_ID
) {
2093 error(ERR_NONFATAL
, "`%%arg' missing argument parameter");
2094 free_tlist(origline
);
2095 return DIRECTIVE_FOUND
;
2099 /* Find the argument size type */
2100 tline
= tline
->next
;
2101 if (!tline
|| tline
->type
!= TOK_OTHER
2102 || tline
->text
[0] != ':') {
2104 "Syntax error processing `%%arg' directive");
2105 free_tlist(origline
);
2106 return DIRECTIVE_FOUND
;
2108 tline
= tline
->next
;
2109 if (!tline
|| tline
->type
!= TOK_ID
) {
2110 error(ERR_NONFATAL
, "`%%arg' missing size type parameter");
2111 free_tlist(origline
);
2112 return DIRECTIVE_FOUND
;
2115 /* Allow macro expansion of type parameter */
2116 tt
= tokenize(tline
->text
);
2117 tt
= expand_smacro(tt
);
2118 size
= parse_size(tt
->text
);
2121 "Invalid size type for `%%arg' missing directive");
2123 free_tlist(origline
);
2124 return DIRECTIVE_FOUND
;
2128 /* Round up to even stack slots */
2129 size
= (size
+StackSize
-1) & ~(StackSize
-1);
2131 /* Now define the macro for the argument */
2132 snprintf(directive
, sizeof(directive
), "%%define %s (%s+%d)",
2133 arg
, StackPointer
, offset
);
2134 do_directive(tokenize(directive
));
2137 /* Move to the next argument in the list */
2138 tline
= tline
->next
;
2139 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2140 tline
= tline
->next
;
2141 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2143 free_tlist(origline
);
2144 return DIRECTIVE_FOUND
;
2147 /* TASM like LOCAL directive to define local variables for a
2148 * function, in the following form:
2150 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2152 * The '= LocalSize' at the end is ignored by NASM, but is
2153 * required by TASM to define the local parameter size (and used
2154 * by the TASM macro package).
2156 offset
= LocalOffset
;
2158 char *local
, directive
[256];
2159 int size
= StackSize
;
2161 /* Find the argument name */
2162 tline
= tline
->next
;
2163 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2164 tline
= tline
->next
;
2165 if (!tline
|| tline
->type
!= TOK_ID
) {
2167 "`%%local' missing argument parameter");
2168 free_tlist(origline
);
2169 return DIRECTIVE_FOUND
;
2171 local
= tline
->text
;
2173 /* Find the argument size type */
2174 tline
= tline
->next
;
2175 if (!tline
|| tline
->type
!= TOK_OTHER
2176 || tline
->text
[0] != ':') {
2178 "Syntax error processing `%%local' directive");
2179 free_tlist(origline
);
2180 return DIRECTIVE_FOUND
;
2182 tline
= tline
->next
;
2183 if (!tline
|| tline
->type
!= TOK_ID
) {
2185 "`%%local' missing size type parameter");
2186 free_tlist(origline
);
2187 return DIRECTIVE_FOUND
;
2190 /* Allow macro expansion of type parameter */
2191 tt
= tokenize(tline
->text
);
2192 tt
= expand_smacro(tt
);
2193 size
= parse_size(tt
->text
);
2196 "Invalid size type for `%%local' missing directive");
2198 free_tlist(origline
);
2199 return DIRECTIVE_FOUND
;
2203 /* Round up to even stack slots */
2204 size
= (size
+StackSize
-1) & ~(StackSize
-1);
2206 offset
+= size
; /* Negative offset, increment before */
2208 /* Now define the macro for the argument */
2209 snprintf(directive
, sizeof(directive
), "%%define %s (%s-%d)",
2210 local
, StackPointer
, offset
);
2211 do_directive(tokenize(directive
));
2213 /* Now define the assign to setup the enter_c macro correctly */
2214 snprintf(directive
, sizeof(directive
),
2215 "%%assign %%$localsize %%$localsize+%d", size
);
2216 do_directive(tokenize(directive
));
2218 /* Move to the next argument in the list */
2219 tline
= tline
->next
;
2220 if (tline
&& tline
->type
== TOK_WHITESPACE
)
2221 tline
= tline
->next
;
2222 } while (tline
&& tline
->type
== TOK_OTHER
&& tline
->text
[0] == ',');
2223 LocalOffset
= offset
;
2224 free_tlist(origline
);
2225 return DIRECTIVE_FOUND
;
2229 error(ERR_WARNING
|ERR_PASS1
,
2230 "trailing garbage after `%%clear' ignored");
2233 free_tlist(origline
);
2234 return DIRECTIVE_FOUND
;
2237 t
= tline
->next
= expand_smacro(tline
->next
);
2239 if (!t
|| (t
->type
!= TOK_STRING
&&
2240 t
->type
!= TOK_INTERNAL_STRING
)) {
2241 error(ERR_NONFATAL
, "`%%depend' expects a file name");
2242 free_tlist(origline
);
2243 return DIRECTIVE_FOUND
; /* but we did _something_ */
2246 error(ERR_WARNING
|ERR_PASS1
,
2247 "trailing garbage after `%%depend' ignored");
2249 if (t
->type
!= TOK_INTERNAL_STRING
)
2250 nasm_unquote(p
, NULL
);
2251 if (dephead
&& !in_list(*dephead
, p
)) {
2252 StrList
*sl
= nasm_malloc(strlen(p
)+1+sizeof sl
->next
);
2256 deptail
= &sl
->next
;
2258 free_tlist(origline
);
2259 return DIRECTIVE_FOUND
;
2262 t
= tline
->next
= expand_smacro(tline
->next
);
2265 if (!t
|| (t
->type
!= TOK_STRING
&&
2266 t
->type
!= TOK_INTERNAL_STRING
)) {
2267 error(ERR_NONFATAL
, "`%%include' expects a file name");
2268 free_tlist(origline
);
2269 return DIRECTIVE_FOUND
; /* but we did _something_ */
2272 error(ERR_WARNING
|ERR_PASS1
,
2273 "trailing garbage after `%%include' ignored");
2275 if (t
->type
!= TOK_INTERNAL_STRING
)
2276 nasm_unquote(p
, NULL
);
2277 inc
= nasm_malloc(sizeof(Include
));
2280 inc
->fp
= inc_fopen(p
, dephead
, &deptail
, pass
== 0);
2282 /* -MG given but file not found */
2285 inc
->fname
= src_set_fname(nasm_strdup(p
));
2286 inc
->lineno
= src_set_linnum(0);
2288 inc
->expansion
= NULL
;
2291 list
->uplevel(LIST_INCLUDE
);
2293 free_tlist(origline
);
2294 return DIRECTIVE_FOUND
;
2298 static macros_t
*use_pkg
;
2299 const char *pkg_macro
;
2301 t
= tline
->next
= expand_smacro(tline
->next
);
2304 if (!t
|| (t
->type
!= TOK_STRING
&&
2305 t
->type
!= TOK_INTERNAL_STRING
&&
2306 t
->type
!= TOK_ID
)) {
2307 error(ERR_NONFATAL
, "`%%use' expects a package name");
2308 free_tlist(origline
);
2309 return DIRECTIVE_FOUND
; /* but we did _something_ */
2312 error(ERR_WARNING
|ERR_PASS1
,
2313 "trailing garbage after `%%use' ignored");
2314 if (t
->type
== TOK_STRING
)
2315 nasm_unquote(t
->text
, NULL
);
2316 use_pkg
= nasm_stdmac_find_package(t
->text
);
2318 error(ERR_NONFATAL
, "unknown `%%use' package: %s", t
->text
);
2319 /* The first string will be <%define>__USE_*__ */
2320 pkg_macro
= (char *)use_pkg
+ 1;
2321 if (!smacro_defined(NULL
, pkg_macro
, 0, NULL
, true)) {
2322 /* Not already included, go ahead and include it */
2323 stdmacpos
= use_pkg
;
2325 free_tlist(origline
);
2326 return DIRECTIVE_FOUND
;
2329 tline
= tline
->next
;
2331 tline
= expand_id(tline
);
2333 if (!tok_type_(tline
, TOK_ID
)) {
2334 error(ERR_NONFATAL
, "`%%push' expects a context identifier");
2335 free_tlist(origline
);
2336 return DIRECTIVE_FOUND
; /* but we did _something_ */
2339 error(ERR_WARNING
|ERR_PASS1
,
2340 "trailing garbage after `%%push' ignored");
2341 p
= nasm_strdup(tline
->text
);
2343 p
= NULL
; /* Anonymous context */
2345 ctx
= nasm_malloc(sizeof(Context
));
2347 hash_init(&ctx
->localmac
, HASH_SMALL
);
2349 ctx
->number
= unique
++;
2351 free_tlist(origline
);
2352 return DIRECTIVE_FOUND
;
2355 tline
= tline
->next
;
2357 tline
= expand_id(tline
);
2359 if (!tok_type_(tline
, TOK_ID
)) {
2360 error(ERR_NONFATAL
, "`%%repl' expects a context identifier");
2361 free_tlist(origline
);
2362 return DIRECTIVE_FOUND
; /* but we did _something_ */
2365 error(ERR_WARNING
|ERR_PASS1
,
2366 "trailing garbage after `%%repl' ignored");
2367 p
= nasm_strdup(tline
->text
);
2372 error(ERR_NONFATAL
, "`%%repl': context stack is empty");
2374 nasm_free(cstk
->name
);
2377 free_tlist(origline
);
2378 return DIRECTIVE_FOUND
;
2382 error(ERR_WARNING
|ERR_PASS1
,
2383 "trailing garbage after `%%pop' ignored");
2385 error(ERR_NONFATAL
, "`%%pop': context stack is already empty");
2388 free_tlist(origline
);
2389 return DIRECTIVE_FOUND
;
2392 severity
= ERR_FATAL
|ERR_NO_SEVERITY
;
2395 severity
= ERR_NONFATAL
|ERR_NO_SEVERITY
;
2398 severity
= ERR_WARNING
|ERR_NO_SEVERITY
|ERR_WARN_USER
;
2403 /* Only error out if this is the final pass */
2404 if (pass
!= 2 && i
!= PP_FATAL
)
2405 return DIRECTIVE_FOUND
;
2407 tline
->next
= expand_smacro(tline
->next
);
2408 tline
= tline
->next
;
2410 t
= tline
? tline
->next
: NULL
;
2412 if (tok_type_(tline
, TOK_STRING
) && !t
) {
2413 /* The line contains only a quoted string */
2415 nasm_unquote(p
, NULL
);
2416 error(severity
, "%s: %s", pp_directives
[i
], p
);
2418 /* Not a quoted string, or more than a quoted string */
2419 p
= detoken(tline
, false);
2420 error(severity
, "%s: %s", pp_directives
[i
], p
);
2423 free_tlist(origline
);
2424 return DIRECTIVE_FOUND
;
2428 if (istk
->conds
&& !emitting(istk
->conds
->state
))
2431 j
= if_condition(tline
->next
, i
);
2432 tline
->next
= NULL
; /* it got freed */
2433 j
= j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2435 cond
= nasm_malloc(sizeof(Cond
));
2436 cond
->next
= istk
->conds
;
2439 free_tlist(origline
);
2440 return DIRECTIVE_FOUND
;
2444 error(ERR_FATAL
, "`%s': no matching `%%if'", pp_directives
[i
]);
2445 switch(istk
->conds
->state
) {
2447 istk
->conds
->state
= COND_DONE
;
2454 case COND_ELSE_TRUE
:
2455 case COND_ELSE_FALSE
:
2456 error_precond(ERR_WARNING
|ERR_PASS1
,
2457 "`%%elif' after `%%else' ignored");
2458 istk
->conds
->state
= COND_NEVER
;
2463 * IMPORTANT: In the case of %if, we will already have
2464 * called expand_mmac_params(); however, if we're
2465 * processing an %elif we must have been in a
2466 * non-emitting mode, which would have inhibited
2467 * the normal invocation of expand_indirect() and
2468 * expand_mmac_params(). Therefore, we have to do it
2471 t
= expand_indirect(tline
->next
,0);
2472 t
= expand_mmac_params(t
);
2473 j
= if_condition(expand_mmac_params(t
), i
);
2474 tline
->next
= NULL
; /* it got freed */
2475 istk
->conds
->state
=
2476 j
< 0 ? COND_NEVER
: j
? COND_IF_TRUE
: COND_IF_FALSE
;
2479 free_tlist(origline
);
2480 return DIRECTIVE_FOUND
;
2484 error_precond(ERR_WARNING
|ERR_PASS1
,
2485 "trailing garbage after `%%else' ignored");
2487 error(ERR_FATAL
, "`%%else': no matching `%%if'");
2488 switch(istk
->conds
->state
) {
2491 istk
->conds
->state
= COND_ELSE_FALSE
;
2498 istk
->conds
->state
= COND_ELSE_TRUE
;
2501 case COND_ELSE_TRUE
:
2502 case COND_ELSE_FALSE
:
2503 error_precond(ERR_WARNING
|ERR_PASS1
,
2504 "`%%else' after `%%else' ignored.");
2505 istk
->conds
->state
= COND_NEVER
;
2508 free_tlist(origline
);
2509 return DIRECTIVE_FOUND
;
2513 error_precond(ERR_WARNING
|ERR_PASS1
,
2514 "trailing garbage after `%%endif' ignored");
2516 error(ERR_FATAL
, "`%%endif': no matching `%%if'");
2518 istk
->conds
= cond
->next
;
2520 free_tlist(origline
);
2521 return DIRECTIVE_FOUND
;
2527 "`%%%smacro': already defining a macro",
2528 (i
== PP_IMACRO
? "i" : ""));
2529 return DIRECTIVE_FOUND
;
2531 defining
= nasm_malloc(sizeof(MMacro
));
2532 defining
->casesense
= (i
== PP_MACRO
);
2533 if (!parse_mmacro_spec(tline
, defining
, pp_directives
[i
])) {
2534 nasm_free(defining
);
2536 return DIRECTIVE_FOUND
;
2539 mmac
= (MMacro
*) hash_findix(&mmacros
, defining
->name
);
2541 if (!strcmp(mmac
->name
, defining
->name
) &&
2542 (mmac
->nparam_min
<= defining
->nparam_max
2544 && (defining
->nparam_min
<= mmac
->nparam_max
2546 error(ERR_WARNING
|ERR_PASS1
,
2547 "redefining multi-line macro `%s'", defining
->name
);
2548 return DIRECTIVE_FOUND
;
2552 free_tlist(origline
);
2553 return DIRECTIVE_FOUND
;
2557 if (! (defining
&& defining
->name
)) {
2558 error(ERR_NONFATAL
, "`%s': not defining a macro", tline
->text
);
2559 return DIRECTIVE_FOUND
;
2561 mmhead
= (MMacro
**) hash_findi_add(&mmacros
, defining
->name
);
2562 defining
->next
= *mmhead
;
2565 free_tlist(origline
);
2566 return DIRECTIVE_FOUND
;
2574 spec
.casesense
= (i
== PP_UNMACRO
);
2575 if (!parse_mmacro_spec(tline
, &spec
, pp_directives
[i
])) {
2576 return DIRECTIVE_FOUND
;
2578 mmac_p
= (MMacro
**) hash_findi(&mmacros
, spec
.name
, NULL
);
2579 while (mmac_p
&& *mmac_p
) {
2581 if (mmac
->casesense
== spec
.casesense
&&
2582 !mstrcmp(mmac
->name
, spec
.name
, spec
.casesense
) &&
2583 mmac
->nparam_min
== spec
.nparam_min
&&
2584 mmac
->nparam_max
== spec
.nparam_max
&&
2585 mmac
->plus
== spec
.plus
) {
2586 *mmac_p
= mmac
->next
;
2589 mmac_p
= &mmac
->next
;
2592 free_tlist(origline
);
2593 free_tlist(spec
.dlist
);
2594 return DIRECTIVE_FOUND
;
2598 if (tline
->next
&& tline
->next
->type
== TOK_WHITESPACE
)
2599 tline
= tline
->next
;
2600 if (tline
->next
== NULL
) {
2601 free_tlist(origline
);
2602 error(ERR_NONFATAL
, "`%%rotate' missing rotate count");
2603 return DIRECTIVE_FOUND
;
2605 t
= expand_smacro(tline
->next
);
2607 free_tlist(origline
);
2610 tokval
.t_type
= TOKEN_INVALID
;
2612 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2615 return DIRECTIVE_FOUND
;
2617 error(ERR_WARNING
|ERR_PASS1
,
2618 "trailing garbage after expression ignored");
2619 if (!is_simple(evalresult
)) {
2620 error(ERR_NONFATAL
, "non-constant value given to `%%rotate'");
2621 return DIRECTIVE_FOUND
;
2624 while (mmac
&& !mmac
->name
) /* avoid mistaking %reps for macros */
2625 mmac
= mmac
->next_active
;
2627 error(ERR_NONFATAL
, "`%%rotate' invoked outside a macro call");
2628 } else if (mmac
->nparam
== 0) {
2630 "`%%rotate' invoked within macro without parameters");
2632 int rotate
= mmac
->rotate
+ reloc_value(evalresult
);
2634 rotate
%= (int)mmac
->nparam
;
2636 rotate
+= mmac
->nparam
;
2638 mmac
->rotate
= rotate
;
2640 return DIRECTIVE_FOUND
;
2645 tline
= tline
->next
;
2646 } while (tok_type_(tline
, TOK_WHITESPACE
));
2648 if (tok_type_(tline
, TOK_ID
) &&
2649 nasm_stricmp(tline
->text
, ".nolist") == 0) {
2652 tline
= tline
->next
;
2653 } while (tok_type_(tline
, TOK_WHITESPACE
));
2657 t
= expand_smacro(tline
);
2659 tokval
.t_type
= TOKEN_INVALID
;
2661 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
2663 free_tlist(origline
);
2664 return DIRECTIVE_FOUND
;
2667 error(ERR_WARNING
|ERR_PASS1
,
2668 "trailing garbage after expression ignored");
2669 if (!is_simple(evalresult
)) {
2670 error(ERR_NONFATAL
, "non-constant value given to `%%rep'");
2671 return DIRECTIVE_FOUND
;
2673 count
= reloc_value(evalresult
) + 1;
2675 error(ERR_NONFATAL
, "`%%rep' expects a repeat count");
2678 free_tlist(origline
);
2680 tmp_defining
= defining
;
2681 defining
= nasm_malloc(sizeof(MMacro
));
2682 defining
->name
= NULL
; /* flags this macro as a %rep block */
2683 defining
->casesense
= false;
2684 defining
->plus
= false;
2685 defining
->nolist
= nolist
;
2686 defining
->in_progress
= count
;
2687 defining
->nparam_min
= defining
->nparam_max
= 0;
2688 defining
->defaults
= NULL
;
2689 defining
->dlist
= NULL
;
2690 defining
->expansion
= NULL
;
2691 defining
->next_active
= istk
->mstk
;
2692 defining
->rep_nest
= tmp_defining
;
2693 return DIRECTIVE_FOUND
;
2696 if (!defining
|| defining
->name
) {
2697 error(ERR_NONFATAL
, "`%%endrep': no matching `%%rep'");
2698 return DIRECTIVE_FOUND
;
2702 * Now we have a "macro" defined - although it has no name
2703 * and we won't be entering it in the hash tables - we must
2704 * push a macro-end marker for it on to istk->expansion.
2705 * After that, it will take care of propagating itself (a
2706 * macro-end marker line for a macro which is really a %rep
2707 * block will cause the macro to be re-expanded, complete
2708 * with another macro-end marker to ensure the process
2709 * continues) until the whole expansion is forcibly removed
2710 * from istk->expansion by a %exitrep.
2712 l
= nasm_malloc(sizeof(Line
));
2713 l
->next
= istk
->expansion
;
2714 l
->finishes
= defining
;
2716 istk
->expansion
= l
;
2718 istk
->mstk
= defining
;
2720 list
->uplevel(defining
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
2721 tmp_defining
= defining
;
2722 defining
= defining
->rep_nest
;
2723 free_tlist(origline
);
2724 return DIRECTIVE_FOUND
;
2728 * We must search along istk->expansion until we hit a
2729 * macro-end marker for a macro with no name. Then we set
2730 * its `in_progress' flag to 0.
2732 for (l
= istk
->expansion
; l
; l
= l
->next
)
2733 if (l
->finishes
&& !l
->finishes
->name
)
2737 l
->finishes
->in_progress
= 1;
2739 error(ERR_NONFATAL
, "`%%exitrep' not within `%%rep' block");
2740 free_tlist(origline
);
2741 return DIRECTIVE_FOUND
;
2747 casesense
= (i
== PP_DEFINE
|| i
== PP_XDEFINE
);
2749 tline
= tline
->next
;
2751 tline
= expand_id(tline
);
2752 if (!tline
|| (tline
->type
!= TOK_ID
&&
2753 (tline
->type
!= TOK_PREPROC_ID
||
2754 tline
->text
[1] != '$'))) {
2755 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
2757 free_tlist(origline
);
2758 return DIRECTIVE_FOUND
;
2761 ctx
= get_ctx(tline
->text
, false);
2763 mname
= tline
->text
;
2765 param_start
= tline
= tline
->next
;
2768 /* Expand the macro definition now for %xdefine and %ixdefine */
2769 if ((i
== PP_XDEFINE
) || (i
== PP_IXDEFINE
))
2770 tline
= expand_smacro(tline
);
2772 if (tok_is_(tline
, "(")) {
2774 * This macro has parameters.
2777 tline
= tline
->next
;
2781 error(ERR_NONFATAL
, "parameter identifier expected");
2782 free_tlist(origline
);
2783 return DIRECTIVE_FOUND
;
2785 if (tline
->type
!= TOK_ID
) {
2787 "`%s': parameter identifier expected",
2789 free_tlist(origline
);
2790 return DIRECTIVE_FOUND
;
2792 tline
->type
= TOK_SMAC_PARAM
+ nparam
++;
2793 tline
= tline
->next
;
2795 if (tok_is_(tline
, ",")) {
2796 tline
= tline
->next
;
2798 if (!tok_is_(tline
, ")")) {
2800 "`)' expected to terminate macro template");
2801 free_tlist(origline
);
2802 return DIRECTIVE_FOUND
;
2808 tline
= tline
->next
;
2810 if (tok_type_(tline
, TOK_WHITESPACE
))
2811 last
= tline
, tline
= tline
->next
;
2816 if (t
->type
== TOK_ID
) {
2817 for (tt
= param_start
; tt
; tt
= tt
->next
)
2818 if (tt
->type
>= TOK_SMAC_PARAM
&&
2819 !strcmp(tt
->text
, t
->text
))
2823 t
->next
= macro_start
;
2828 * Good. We now have a macro name, a parameter count, and a
2829 * token list (in reverse order) for an expansion. We ought
2830 * to be OK just to create an SMacro, store it, and let
2831 * free_tlist have the rest of the line (which we have
2832 * carefully re-terminated after chopping off the expansion
2835 define_smacro(ctx
, mname
, casesense
, nparam
, macro_start
);
2836 free_tlist(origline
);
2837 return DIRECTIVE_FOUND
;
2840 tline
= tline
->next
;
2842 tline
= expand_id(tline
);
2843 if (!tline
|| (tline
->type
!= TOK_ID
&&
2844 (tline
->type
!= TOK_PREPROC_ID
||
2845 tline
->text
[1] != '$'))) {
2846 error(ERR_NONFATAL
, "`%%undef' expects a macro identifier");
2847 free_tlist(origline
);
2848 return DIRECTIVE_FOUND
;
2851 error(ERR_WARNING
|ERR_PASS1
,
2852 "trailing garbage after macro name ignored");
2855 /* Find the context that symbol belongs to */
2856 ctx
= get_ctx(tline
->text
, false);
2857 undef_smacro(ctx
, tline
->text
);
2858 free_tlist(origline
);
2859 return DIRECTIVE_FOUND
;
2863 casesense
= (i
== PP_DEFSTR
);
2865 tline
= tline
->next
;
2867 tline
= expand_id(tline
);
2868 if (!tline
|| (tline
->type
!= TOK_ID
&&
2869 (tline
->type
!= TOK_PREPROC_ID
||
2870 tline
->text
[1] != '$'))) {
2871 error(ERR_NONFATAL
, "`%s' expects a macro identifier",
2873 free_tlist(origline
);
2874 return DIRECTIVE_FOUND
;
2877 ctx
= get_ctx(tline
->text
, false);
2879 mname
= tline
->text
;
2881 tline
= expand_smacro(tline
->next
);
2884 while (tok_type_(tline
, TOK_WHITESPACE
))
2885 tline
= delete_Token(tline
);
2887 p
= detoken(tline
, false);
2888 macro_start
= nasm_malloc(sizeof(*macro_start
));
2889 macro_start
->next
= NULL
;
2890 macro_start
->text
= nasm_quote(p
, strlen(p
));
2891 macro_start
->type
= TOK_STRING
;
2892 macro_start
->a
.mac
= NULL
;
2896 * We now have a macro name, an implicit parameter count of
2897 * zero, and a string token to use as an expansion. Create
2898 * and store an SMacro.
2900 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2901 free_tlist(origline
);
2902 return DIRECTIVE_FOUND
;
2907 StrList
*xsl
= NULL
;
2908 StrList
**xst
= &xsl
;
2912 tline
= tline
->next
;
2914 tline
= expand_id(tline
);
2915 if (!tline
|| (tline
->type
!= TOK_ID
&&
2916 (tline
->type
!= TOK_PREPROC_ID
||
2917 tline
->text
[1] != '$'))) {
2919 "`%%pathsearch' expects a macro identifier as first parameter");
2920 free_tlist(origline
);
2921 return DIRECTIVE_FOUND
;
2923 ctx
= get_ctx(tline
->text
, false);
2925 mname
= tline
->text
;
2927 tline
= expand_smacro(tline
->next
);
2931 while (tok_type_(t
, TOK_WHITESPACE
))
2934 if (!t
|| (t
->type
!= TOK_STRING
&&
2935 t
->type
!= TOK_INTERNAL_STRING
)) {
2936 error(ERR_NONFATAL
, "`%%pathsearch' expects a file name");
2938 free_tlist(origline
);
2939 return DIRECTIVE_FOUND
; /* but we did _something_ */
2942 error(ERR_WARNING
|ERR_PASS1
,
2943 "trailing garbage after `%%pathsearch' ignored");
2945 if (t
->type
!= TOK_INTERNAL_STRING
)
2946 nasm_unquote(p
, NULL
);
2948 fp
= inc_fopen(p
, &xsl
, &xst
, true);
2951 fclose(fp
); /* Don't actually care about the file */
2953 macro_start
= nasm_malloc(sizeof(*macro_start
));
2954 macro_start
->next
= NULL
;
2955 macro_start
->text
= nasm_quote(p
, strlen(p
));
2956 macro_start
->type
= TOK_STRING
;
2957 macro_start
->a
.mac
= NULL
;
2962 * We now have a macro name, an implicit parameter count of
2963 * zero, and a string token to use as an expansion. Create
2964 * and store an SMacro.
2966 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
2968 free_tlist(origline
);
2969 return DIRECTIVE_FOUND
;
2975 tline
= tline
->next
;
2977 tline
= expand_id(tline
);
2978 if (!tline
|| (tline
->type
!= TOK_ID
&&
2979 (tline
->type
!= TOK_PREPROC_ID
||
2980 tline
->text
[1] != '$'))) {
2982 "`%%strlen' expects a macro identifier as first parameter");
2983 free_tlist(origline
);
2984 return DIRECTIVE_FOUND
;
2986 ctx
= get_ctx(tline
->text
, false);
2988 mname
= tline
->text
;
2990 tline
= expand_smacro(tline
->next
);
2994 while (tok_type_(t
, TOK_WHITESPACE
))
2996 /* t should now point to the string */
2997 if (t
->type
!= TOK_STRING
) {
2999 "`%%strlen` requires string as second parameter");
3001 free_tlist(origline
);
3002 return DIRECTIVE_FOUND
;
3005 macro_start
= nasm_malloc(sizeof(*macro_start
));
3006 macro_start
->next
= NULL
;
3007 make_tok_num(macro_start
, nasm_unquote(t
->text
, NULL
));
3008 macro_start
->a
.mac
= NULL
;
3011 * We now have a macro name, an implicit parameter count of
3012 * zero, and a numeric token to use as an expansion. Create
3013 * and store an SMacro.
3015 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3017 free_tlist(origline
);
3018 return DIRECTIVE_FOUND
;
3023 tline
= tline
->next
;
3025 tline
= expand_id(tline
);
3026 if (!tline
|| (tline
->type
!= TOK_ID
&&
3027 (tline
->type
!= TOK_PREPROC_ID
||
3028 tline
->text
[1] != '$'))) {
3030 "`%%strcat' expects a macro identifier as first parameter");
3031 free_tlist(origline
);
3032 return DIRECTIVE_FOUND
;
3034 ctx
= get_ctx(tline
->text
, false);
3036 mname
= tline
->text
;
3038 tline
= expand_smacro(tline
->next
);
3042 for (t
= tline
; t
; t
= t
->next
) {
3044 case TOK_WHITESPACE
:
3047 len
+= t
->a
.len
= nasm_unquote(t
->text
, NULL
);
3050 if (!strcmp(t
->text
, ",")) /* permit comma separators */
3052 /* else fall through */
3055 "non-string passed to `%%strcat' (%d)", t
->type
);
3057 free_tlist(origline
);
3058 return DIRECTIVE_FOUND
;
3062 p
= pp
= nasm_malloc(len
);
3064 for (t
= tline
; t
; t
= t
->next
) {
3065 if (t
->type
== TOK_STRING
) {
3066 memcpy(p
, t
->text
, t
->a
.len
);
3072 * We now have a macro name, an implicit parameter count of
3073 * zero, and a numeric token to use as an expansion. Create
3074 * and store an SMacro.
3076 macro_start
= new_Token(NULL
, TOK_STRING
, NULL
, 0);
3077 macro_start
->text
= nasm_quote(pp
, len
);
3079 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3081 free_tlist(origline
);
3082 return DIRECTIVE_FOUND
;
3091 tline
= tline
->next
;
3093 tline
= expand_id(tline
);
3094 if (!tline
|| (tline
->type
!= TOK_ID
&&
3095 (tline
->type
!= TOK_PREPROC_ID
||
3096 tline
->text
[1] != '$'))) {
3098 "`%%substr' expects a macro identifier as first parameter");
3099 free_tlist(origline
);
3100 return DIRECTIVE_FOUND
;
3102 ctx
= get_ctx(tline
->text
, false);
3104 mname
= tline
->text
;
3106 tline
= expand_smacro(tline
->next
);
3110 while (tok_type_(t
, TOK_WHITESPACE
))
3113 /* t should now point to the string */
3114 if (t
->type
!= TOK_STRING
) {
3116 "`%%substr` requires string as second parameter");
3118 free_tlist(origline
);
3119 return DIRECTIVE_FOUND
;
3124 tokval
.t_type
= TOKEN_INVALID
;
3125 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
,
3129 free_tlist(origline
);
3130 return DIRECTIVE_FOUND
;
3131 } else if (!is_simple(evalresult
)) {
3132 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
3134 free_tlist(origline
);
3135 return DIRECTIVE_FOUND
;
3137 a1
= evalresult
->value
-1;
3139 while (tok_type_(tt
, TOK_WHITESPACE
))
3142 a2
= 1; /* Backwards compatibility: one character */
3144 tokval
.t_type
= TOKEN_INVALID
;
3145 evalresult
= evaluate(ppscan
, tptr
, &tokval
, NULL
,
3149 free_tlist(origline
);
3150 return DIRECTIVE_FOUND
;
3151 } else if (!is_simple(evalresult
)) {
3152 error(ERR_NONFATAL
, "non-constant value given to `%%substr`");
3154 free_tlist(origline
);
3155 return DIRECTIVE_FOUND
;
3157 a2
= evalresult
->value
;
3160 len
= nasm_unquote(t
->text
, NULL
);
3163 if (a1
+a2
> (int64_t)len
)
3166 macro_start
= nasm_malloc(sizeof(*macro_start
));
3167 macro_start
->next
= NULL
;
3168 macro_start
->text
= nasm_quote((a1
< 0) ? "" : t
->text
+a1
, a2
);
3169 macro_start
->type
= TOK_STRING
;
3170 macro_start
->a
.mac
= NULL
;
3173 * We now have a macro name, an implicit parameter count of
3174 * zero, and a numeric token to use as an expansion. Create
3175 * and store an SMacro.
3177 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3179 free_tlist(origline
);
3180 return DIRECTIVE_FOUND
;
3185 casesense
= (i
== PP_ASSIGN
);
3187 tline
= tline
->next
;
3189 tline
= expand_id(tline
);
3190 if (!tline
|| (tline
->type
!= TOK_ID
&&
3191 (tline
->type
!= TOK_PREPROC_ID
||
3192 tline
->text
[1] != '$'))) {
3194 "`%%%sassign' expects a macro identifier",
3195 (i
== PP_IASSIGN
? "i" : ""));
3196 free_tlist(origline
);
3197 return DIRECTIVE_FOUND
;
3199 ctx
= get_ctx(tline
->text
, false);
3201 mname
= tline
->text
;
3203 tline
= expand_smacro(tline
->next
);
3208 tokval
.t_type
= TOKEN_INVALID
;
3210 evaluate(ppscan
, tptr
, &tokval
, NULL
, pass
, error
, NULL
);
3213 free_tlist(origline
);
3214 return DIRECTIVE_FOUND
;
3218 error(ERR_WARNING
|ERR_PASS1
,
3219 "trailing garbage after expression ignored");
3221 if (!is_simple(evalresult
)) {
3223 "non-constant value given to `%%%sassign'",
3224 (i
== PP_IASSIGN
? "i" : ""));
3225 free_tlist(origline
);
3226 return DIRECTIVE_FOUND
;
3229 macro_start
= nasm_malloc(sizeof(*macro_start
));
3230 macro_start
->next
= NULL
;
3231 make_tok_num(macro_start
, reloc_value(evalresult
));
3232 macro_start
->a
.mac
= NULL
;
3235 * We now have a macro name, an implicit parameter count of
3236 * zero, and a numeric token to use as an expansion. Create
3237 * and store an SMacro.
3239 define_smacro(ctx
, mname
, casesense
, 0, macro_start
);
3240 free_tlist(origline
);
3241 return DIRECTIVE_FOUND
;
3245 * Syntax is `%line nnn[+mmm] [filename]'
3247 tline
= tline
->next
;
3249 if (!tok_type_(tline
, TOK_NUMBER
)) {
3250 error(ERR_NONFATAL
, "`%%line' expects line number");
3251 free_tlist(origline
);
3252 return DIRECTIVE_FOUND
;
3254 k
= readnum(tline
->text
, &err
);
3256 tline
= tline
->next
;
3257 if (tok_is_(tline
, "+")) {
3258 tline
= tline
->next
;
3259 if (!tok_type_(tline
, TOK_NUMBER
)) {
3260 error(ERR_NONFATAL
, "`%%line' expects line increment");
3261 free_tlist(origline
);
3262 return DIRECTIVE_FOUND
;
3264 m
= readnum(tline
->text
, &err
);
3265 tline
= tline
->next
;
3271 nasm_free(src_set_fname(detoken(tline
, false)));
3273 free_tlist(origline
);
3274 return DIRECTIVE_FOUND
;
3278 "preprocessor directive `%s' not yet implemented",
3280 return DIRECTIVE_FOUND
;
3285 * Ensure that a macro parameter contains a condition code and
3286 * nothing else. Return the condition code index if so, or -1
3289 static int find_cc(Token
* t
)
3295 return -1; /* Probably a %+ without a space */
3298 if (t
->type
!= TOK_ID
)
3302 if (tt
&& (tt
->type
!= TOK_OTHER
|| strcmp(tt
->text
, ",")))
3306 j
= elements(conditions
);
3309 m
= nasm_stricmp(t
->text
, conditions
[k
]);
3325 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3326 * %-n) and MMacro-local identifiers (%%foo).
3328 static Token
*expand_mmac_params(Token
* tline
)
3330 Token
*t
, *tt
, **tail
, *thead
;
3336 if (tline
->type
== TOK_PREPROC_ID
&&
3337 (((tline
->text
[1] == '+' || tline
->text
[1] == '-')
3338 && tline
->text
[2]) || tline
->text
[1] == '%'
3339 || (tline
->text
[1] >= '0' && tline
->text
[1] <= '9'))) {
3341 int type
= 0, cc
; /* type = 0 to placate optimisers */
3348 tline
= tline
->next
;
3351 while (mac
&& !mac
->name
) /* avoid mistaking %reps for macros */
3352 mac
= mac
->next_active
;
3354 error(ERR_NONFATAL
, "`%s': not in a macro call", t
->text
);
3356 switch (t
->text
[1]) {
3358 * We have to make a substitution of one of the
3359 * forms %1, %-1, %+1, %%foo, %0.
3363 snprintf(tmpbuf
, sizeof(tmpbuf
), "%d", mac
->nparam
);
3364 text
= nasm_strdup(tmpbuf
);
3368 snprintf(tmpbuf
, sizeof(tmpbuf
), "..@%"PRIu64
".",
3370 text
= nasm_strcat(tmpbuf
, t
->text
+ 2);
3373 n
= atoi(t
->text
+ 2) - 1;
3374 if (n
>= mac
->nparam
)
3377 if (mac
->nparam
> 1)
3378 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3379 tt
= mac
->params
[n
];
3384 "macro parameter %d is not a condition code",
3389 if (inverse_ccs
[cc
] == -1) {
3391 "condition code `%s' is not invertible",
3396 nasm_strdup(conditions
[inverse_ccs
[cc
]]);
3400 n
= atoi(t
->text
+ 2) - 1;
3401 if (n
>= mac
->nparam
)
3404 if (mac
->nparam
> 1)
3405 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3406 tt
= mac
->params
[n
];
3411 "macro parameter %d is not a condition code",
3416 text
= nasm_strdup(conditions
[cc
]);
3420 n
= atoi(t
->text
+ 1) - 1;
3421 if (n
>= mac
->nparam
)
3424 if (mac
->nparam
> 1)
3425 n
= (n
+ mac
->rotate
) % mac
->nparam
;
3426 tt
= mac
->params
[n
];
3429 for (i
= 0; i
< mac
->paramlen
[n
]; i
++) {
3430 *tail
= new_Token(NULL
, tt
->type
, tt
->text
, 0);
3431 tail
= &(*tail
)->next
;
3435 text
= NULL
; /* we've done it here */
3451 tline
= tline
->next
;
3458 for (; t
&& (tt
= t
->next
) != NULL
; t
= t
->next
)
3460 case TOK_WHITESPACE
:
3461 if (tt
->type
== TOK_WHITESPACE
) {
3462 t
->next
= delete_Token(tt
);
3467 if (tt
->type
== t
->type
|| tt
->type
== TOK_NUMBER
) {
3468 char *tmp
= nasm_strcat(t
->text
, tt
->text
);
3471 t
->next
= delete_Token(tt
);
3482 * Expand all single-line macro calls made in the given line.
3483 * Return the expanded version of the line. The original is deemed
3484 * to be destroyed in the process. (In reality we'll just move
3485 * Tokens from input to output a lot of the time, rather than
3486 * actually bothering to destroy and replicate.)
3488 #define DEADMAN_LIMIT (1 << 20)
3490 static Token
*expand_smacro(Token
* tline
)
3492 Token
*t
, *tt
, *mstart
, **tail
, *thead
;
3493 struct hash_table
*smtbl
;
3494 SMacro
*head
= NULL
, *m
;
3497 unsigned int nparam
, sparam
;
3498 int brackets
, rescan
;
3499 Token
*org_tline
= tline
;
3502 int deadman
= DEADMAN_LIMIT
;
3505 * Trick: we should avoid changing the start token pointer since it can
3506 * be contained in "next" field of other token. Because of this
3507 * we allocate a copy of first token and work with it; at the end of
3508 * routine we copy it back
3512 new_Token(org_tline
->next
, org_tline
->type
, org_tline
->text
,
3514 tline
->a
.mac
= org_tline
->a
.mac
;
3515 nasm_free(org_tline
->text
);
3516 org_tline
->text
= NULL
;
3523 while (tline
) { /* main token loop */
3525 error(ERR_NONFATAL
, "interminable macro recursion");
3529 if ((mname
= tline
->text
)) {
3530 /* if this token is a local macro, look in local context */
3533 if (tline
->type
== TOK_ID
|| tline
->type
== TOK_PREPROC_ID
) {
3534 ctx
= get_ctx(mname
, true);
3536 smtbl
= &ctx
->localmac
;
3538 head
= (SMacro
*) hash_findix(smtbl
, mname
);
3541 * We've hit an identifier. As in is_mmacro below, we first
3542 * check whether the identifier is a single-line macro at
3543 * all, then think about checking for parameters if
3546 for (m
= head
; m
; m
= m
->next
)
3547 if (!mstrcmp(m
->name
, mname
, m
->casesense
))
3553 if (m
->nparam
== 0) {
3555 * Simple case: the macro is parameterless. Discard the
3556 * one token that the macro call took, and push the
3557 * expansion back on the to-do stack.
3559 if (!m
->expansion
) {
3560 if (!strcmp("__FILE__", m
->name
)) {
3563 src_get(&num
, &file
);
3564 tline
->text
= nasm_quote(file
, strlen(file
));
3565 tline
->type
= TOK_STRING
;
3569 if (!strcmp("__LINE__", m
->name
)) {
3570 nasm_free(tline
->text
);
3571 make_tok_num(tline
, src_get_linnum());
3574 if (!strcmp("__BITS__", m
->name
)) {
3575 nasm_free(tline
->text
);
3576 make_tok_num(tline
, globalbits
);
3579 tline
= delete_Token(tline
);
3584 * Complicated case: at least one macro with this name
3585 * exists and takes parameters. We must find the
3586 * parameters in the call, count them, find the SMacro
3587 * that corresponds to that form of the macro call, and
3588 * substitute for the parameters when we expand. What a
3591 /*tline = tline->next;
3592 skip_white_(tline); */
3595 while (tok_type_(t
, TOK_SMAC_END
)) {
3596 t
->a
.mac
->in_progress
= false;
3598 t
= tline
->next
= delete_Token(t
);
3601 } while (tok_type_(tline
, TOK_WHITESPACE
));
3602 if (!tok_is_(tline
, "(")) {
3604 * This macro wasn't called with parameters: ignore
3605 * the call. (Behaviour borrowed from gnu cpp.)
3614 sparam
= PARAM_DELTA
;
3615 params
= nasm_malloc(sparam
* sizeof(Token
*));
3616 params
[0] = tline
->next
;
3617 paramsize
= nasm_malloc(sparam
* sizeof(int));
3619 while (true) { /* parameter loop */
3621 * For some unusual expansions
3622 * which concatenates function call
3625 while (tok_type_(t
, TOK_SMAC_END
)) {
3626 t
->a
.mac
->in_progress
= false;
3628 t
= tline
->next
= delete_Token(t
);
3634 "macro call expects terminating `)'");
3637 if (tline
->type
== TOK_WHITESPACE
3639 if (paramsize
[nparam
])
3642 params
[nparam
] = tline
->next
;
3643 continue; /* parameter loop */
3645 if (tline
->type
== TOK_OTHER
3646 && tline
->text
[1] == 0) {
3647 char ch
= tline
->text
[0];
3648 if (ch
== ',' && !paren
&& brackets
<= 0) {
3649 if (++nparam
>= sparam
) {
3650 sparam
+= PARAM_DELTA
;
3651 params
= nasm_realloc(params
,
3656 nasm_realloc(paramsize
,
3660 params
[nparam
] = tline
->next
;
3661 paramsize
[nparam
] = 0;
3663 continue; /* parameter loop */
3666 (brackets
> 0 || (brackets
== 0 &&
3667 !paramsize
[nparam
])))
3669 if (!(brackets
++)) {
3670 params
[nparam
] = tline
->next
;
3671 continue; /* parameter loop */
3674 if (ch
== '}' && brackets
> 0)
3675 if (--brackets
== 0) {
3677 continue; /* parameter loop */
3679 if (ch
== '(' && !brackets
)
3681 if (ch
== ')' && brackets
<= 0)
3687 error(ERR_NONFATAL
, "braces do not "
3688 "enclose all of macro parameter");
3690 paramsize
[nparam
] += white
+ 1;
3692 } /* parameter loop */
3694 while (m
&& (m
->nparam
!= nparam
||
3695 mstrcmp(m
->name
, mname
,
3699 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MNP
,
3700 "macro `%s' exists, "
3701 "but not taking %d parameters",
3702 mstart
->text
, nparam
);
3705 if (m
&& m
->in_progress
)
3707 if (!m
) { /* in progess or didn't find '(' or wrong nparam */
3709 * Design question: should we handle !tline, which
3710 * indicates missing ')' here, or expand those
3711 * macros anyway, which requires the (t) test a few
3715 nasm_free(paramsize
);
3719 * Expand the macro: we are placed on the last token of the
3720 * call, so that we can easily split the call from the
3721 * following tokens. We also start by pushing an SMAC_END
3722 * token for the cycle removal.
3729 tt
= new_Token(tline
, TOK_SMAC_END
, NULL
, 0);
3731 m
->in_progress
= true;
3733 for (t
= m
->expansion
; t
; t
= t
->next
) {
3734 if (t
->type
>= TOK_SMAC_PARAM
) {
3735 Token
*pcopy
= tline
, **ptail
= &pcopy
;
3739 ttt
= params
[t
->type
- TOK_SMAC_PARAM
];
3740 for (i
= paramsize
[t
->type
- TOK_SMAC_PARAM
];
3743 new_Token(tline
, ttt
->type
, ttt
->text
,
3749 } else if (t
->type
== TOK_PREPROC_Q
) {
3750 tt
= new_Token(tline
, TOK_ID
, mname
, 0);
3752 } else if (t
->type
== TOK_PREPROC_QQ
) {
3753 tt
= new_Token(tline
, TOK_ID
, m
->name
, 0);
3756 tt
= new_Token(tline
, t
->type
, t
->text
, 0);
3762 * Having done that, get rid of the macro call, and clean
3763 * up the parameters.
3766 nasm_free(paramsize
);
3768 continue; /* main token loop */
3773 if (tline
->type
== TOK_SMAC_END
) {
3774 tline
->a
.mac
->in_progress
= false;
3775 tline
= delete_Token(tline
);
3778 tline
= tline
->next
;
3786 * Now scan the entire line and look for successive TOK_IDs that resulted
3787 * after expansion (they can't be produced by tokenize()). The successive
3788 * TOK_IDs should be concatenated.
3789 * Also we look for %+ tokens and concatenate the tokens before and after
3790 * them (without white spaces in between).
3795 while (t
&& t
->type
!= TOK_ID
&& t
->type
!= TOK_PREPROC_ID
)
3799 if (t
->next
->type
== TOK_ID
||
3800 t
->next
->type
== TOK_PREPROC_ID
||
3801 t
->next
->type
== TOK_NUMBER
) {
3802 char *p
= nasm_strcat(t
->text
, t
->next
->text
);
3804 t
->next
= delete_Token(t
->next
);
3807 } else if (t
->next
->type
== TOK_WHITESPACE
&& t
->next
->next
&&
3808 t
->next
->next
->type
== TOK_PREPROC_ID
&&
3809 strcmp(t
->next
->next
->text
, "%+") == 0) {
3810 /* free the next whitespace, the %+ token and next whitespace */
3812 for (i
= 1; i
<= 3; i
++) {
3814 || (i
!= 2 && t
->next
->type
!= TOK_WHITESPACE
))
3816 t
->next
= delete_Token(t
->next
);
3821 /* If we concatenaded something, re-scan the line for macros */
3829 *org_tline
= *thead
;
3830 /* since we just gave text to org_line, don't free it */
3832 delete_Token(thead
);
3834 /* the expression expanded to empty line;
3835 we can't return NULL for some reasons
3836 we just set the line to a single WHITESPACE token. */
3837 memset(org_tline
, 0, sizeof(*org_tline
));
3838 org_tline
->text
= NULL
;
3839 org_tline
->type
= TOK_WHITESPACE
;
3848 * Similar to expand_smacro but used exclusively with macro identifiers
3849 * right before they are fetched in. The reason is that there can be
3850 * identifiers consisting of several subparts. We consider that if there
3851 * are more than one element forming the name, user wants a expansion,
3852 * otherwise it will be left as-is. Example:
3856 * the identifier %$abc will be left as-is so that the handler for %define
3857 * will suck it and define the corresponding value. Other case:
3859 * %define _%$abc cde
3861 * In this case user wants name to be expanded *before* %define starts
3862 * working, so we'll expand %$abc into something (if it has a value;
3863 * otherwise it will be left as-is) then concatenate all successive
3866 static Token
*expand_id(Token
* tline
)
3868 Token
*cur
, *oldnext
= NULL
;
3870 if (!tline
|| !tline
->next
)
3875 (cur
->next
->type
== TOK_ID
||
3876 cur
->next
->type
== TOK_PREPROC_ID
3877 || cur
->next
->type
== TOK_NUMBER
))
3880 /* If identifier consists of just one token, don't expand */
3885 oldnext
= cur
->next
; /* Detach the tail past identifier */
3886 cur
->next
= NULL
; /* so that expand_smacro stops here */
3889 tline
= expand_smacro(tline
);
3892 /* expand_smacro possibly changhed tline; re-scan for EOL */
3894 while (cur
&& cur
->next
)
3897 cur
->next
= oldnext
;
3904 * Expand indirect tokens, %[...]. Just like expand_smacro(),
3905 * the input is considered destroyed.
3907 static Token
*expand_indirect(Token
* tline
, int level
)
3909 const int max_indirect_level
= 1000;
3910 Token
*t
, *thead
, **tp
;
3914 if (level
>= max_indirect_level
) {
3915 error(ERR_NONFATAL
, "interminable indirect expansion");
3920 if (t
->type
!= TOK_INDIRECT
) {
3924 it
= tokenize(t
->text
);
3925 it
= expand_indirect(it
, level
+1);
3926 it
= expand_smacro(it
);
3929 switch (thead
? thead
->type
: TOK_NONE
) {
3930 case TOK_WHITESPACE
:
3931 skip
= (it
->type
== TOK_WHITESPACE
);
3935 if (it
->type
== thead
->type
|| it
->type
== TOK_NUMBER
) {
3936 char *tmp
= nasm_strcat(thead
->text
, it
->text
);
3937 nasm_free(thead
->text
);
3946 it
= delete_Token(it
);
3953 *tp
= thead
= t
->next
;
3954 t
= delete_Token(t
);
3962 * Determine whether the given line constitutes a multi-line macro
3963 * call, and return the MMacro structure called if so. Doesn't have
3964 * to check for an initial label - that's taken care of in
3965 * expand_mmacro - but must check numbers of parameters. Guaranteed
3966 * to be called with tline->type == TOK_ID, so the putative macro
3967 * name is easy to find.
3969 static MMacro
*is_mmacro(Token
* tline
, Token
*** params_array
)
3975 head
= (MMacro
*) hash_findix(&mmacros
, tline
->text
);
3978 * Efficiency: first we see if any macro exists with the given
3979 * name. If not, we can return NULL immediately. _Then_ we
3980 * count the parameters, and then we look further along the
3981 * list if necessary to find the proper MMacro.
3983 for (m
= head
; m
; m
= m
->next
)
3984 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
3990 * OK, we have a potential macro. Count and demarcate the
3993 count_mmac_params(tline
->next
, &nparam
, ¶ms
);
3996 * So we know how many parameters we've got. Find the MMacro
3997 * structure that handles this number.
4000 if (m
->nparam_min
<= nparam
4001 && (m
->plus
|| nparam
<= m
->nparam_max
)) {
4003 * This one is right. Just check if cycle removal
4004 * prohibits us using it before we actually celebrate...
4006 if (m
->in_progress
) {
4009 "self-reference in multi-line macro `%s'", m
->name
);
4015 * It's right, and we can use it. Add its default
4016 * parameters to the end of our list if necessary.
4018 if (m
->defaults
&& nparam
< m
->nparam_min
+ m
->ndefs
) {
4020 nasm_realloc(params
,
4021 ((m
->nparam_min
+ m
->ndefs
+
4022 1) * sizeof(*params
)));
4023 while (nparam
< m
->nparam_min
+ m
->ndefs
) {
4024 params
[nparam
] = m
->defaults
[nparam
- m
->nparam_min
];
4029 * If we've gone over the maximum parameter count (and
4030 * we're in Plus mode), ignore parameters beyond
4033 if (m
->plus
&& nparam
> m
->nparam_max
)
4034 nparam
= m
->nparam_max
;
4036 * Then terminate the parameter list, and leave.
4038 if (!params
) { /* need this special case */
4039 params
= nasm_malloc(sizeof(*params
));
4042 params
[nparam
] = NULL
;
4043 *params_array
= params
;
4047 * This one wasn't right: look for the next one with the
4050 for (m
= m
->next
; m
; m
= m
->next
)
4051 if (!mstrcmp(m
->name
, tline
->text
, m
->casesense
))
4056 * After all that, we didn't find one with the right number of
4057 * parameters. Issue a warning, and fail to expand the macro.
4059 error(ERR_WARNING
|ERR_PASS1
|ERR_WARN_MNP
,
4060 "macro `%s' exists, but not taking %d parameters",
4061 tline
->text
, nparam
);
4067 * Expand the multi-line macro call made by the given line, if
4068 * there is one to be expanded. If there is, push the expansion on
4069 * istk->expansion and return 1. Otherwise return 0.
4071 static int expand_mmacro(Token
* tline
)
4073 Token
*startline
= tline
;
4074 Token
*label
= NULL
;
4075 int dont_prepend
= 0;
4076 Token
**params
, *t
, *mtok
, *tt
;
4079 int i
, nparam
, *paramlen
;
4084 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
4085 if (!tok_type_(t
, TOK_ID
) && !tok_type_(t
, TOK_PREPROC_ID
))
4088 m
= is_mmacro(t
, ¶ms
);
4094 * We have an id which isn't a macro call. We'll assume
4095 * it might be a label; we'll also check to see if a
4096 * colon follows it. Then, if there's another id after
4097 * that lot, we'll check it again for macro-hood.
4101 if (tok_type_(t
, TOK_WHITESPACE
))
4102 last
= t
, t
= t
->next
;
4103 if (tok_is_(t
, ":")) {
4105 last
= t
, t
= t
->next
;
4106 if (tok_type_(t
, TOK_WHITESPACE
))
4107 last
= t
, t
= t
->next
;
4109 if (!tok_type_(t
, TOK_ID
) || (m
= is_mmacro(t
, ¶ms
)) == NULL
)
4117 * Fix up the parameters: this involves stripping leading and
4118 * trailing whitespace, then stripping braces if they are
4121 for (nparam
= 0; params
[nparam
]; nparam
++) ;
4122 paramlen
= nparam
? nasm_malloc(nparam
* sizeof(*paramlen
)) : NULL
;
4124 for (i
= 0; params
[i
]; i
++) {
4126 int comma
= (!m
->plus
|| i
< nparam
- 1);
4130 if (tok_is_(t
, "{"))
4131 t
= t
->next
, brace
= true, comma
= false;
4135 if (comma
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, ","))
4136 break; /* ... because we have hit a comma */
4137 if (comma
&& t
->type
== TOK_WHITESPACE
4138 && tok_is_(t
->next
, ","))
4139 break; /* ... or a space then a comma */
4140 if (brace
&& t
->type
== TOK_OTHER
&& !strcmp(t
->text
, "}"))
4141 break; /* ... or a brace */
4148 * OK, we have a MMacro structure together with a set of
4149 * parameters. We must now go through the expansion and push
4150 * copies of each Line on to istk->expansion. Substitution of
4151 * parameter tokens and macro-local tokens doesn't get done
4152 * until the single-line macro substitution process; this is
4153 * because delaying them allows us to change the semantics
4154 * later through %rotate.
4156 * First, push an end marker on to istk->expansion, mark this
4157 * macro as in progress, and set up its invocation-specific
4160 ll
= nasm_malloc(sizeof(Line
));
4161 ll
->next
= istk
->expansion
;
4164 istk
->expansion
= ll
;
4166 m
->in_progress
= true;
4171 m
->paramlen
= paramlen
;
4172 m
->unique
= unique
++;
4175 m
->next_active
= istk
->mstk
;
4178 for (l
= m
->expansion
; l
; l
= l
->next
) {
4181 ll
= nasm_malloc(sizeof(Line
));
4182 ll
->finishes
= NULL
;
4183 ll
->next
= istk
->expansion
;
4184 istk
->expansion
= ll
;
4187 for (t
= l
->first
; t
; t
= t
->next
) {
4191 tt
= *tail
= new_Token(NULL
, TOK_ID
, mname
, 0);
4193 case TOK_PREPROC_QQ
:
4194 tt
= *tail
= new_Token(NULL
, TOK_ID
, m
->name
, 0);
4196 case TOK_PREPROC_ID
:
4197 if (t
->text
[1] == '0' && t
->text
[2] == '0') {
4205 tt
= *tail
= new_Token(NULL
, x
->type
, x
->text
, 0);
4214 * If we had a label, push it on as the first line of
4215 * the macro expansion.
4218 if (dont_prepend
< 0)
4219 free_tlist(startline
);
4221 ll
= nasm_malloc(sizeof(Line
));
4222 ll
->finishes
= NULL
;
4223 ll
->next
= istk
->expansion
;
4224 istk
->expansion
= ll
;
4225 ll
->first
= startline
;
4226 if (!dont_prepend
) {
4228 label
= label
->next
;
4229 label
->next
= tt
= new_Token(NULL
, TOK_OTHER
, ":", 0);
4234 list
->uplevel(m
->nolist
? LIST_MACRO_NOLIST
: LIST_MACRO
);
4239 /* The function that actually does the error reporting */
4240 static void verror(int severity
, const char *fmt
, va_list arg
)
4244 vsnprintf(buff
, sizeof(buff
), fmt
, arg
);
4246 if (istk
&& istk
->mstk
&& istk
->mstk
->name
)
4247 _error(severity
, "(%s:%d) %s", istk
->mstk
->name
,
4248 istk
->mstk
->lineno
, buff
);
4250 _error(severity
, "%s", buff
);
4254 * Since preprocessor always operate only on the line that didn't
4255 * arrived yet, we should always use ERR_OFFBY1.
4257 static void error(int severity
, const char *fmt
, ...)
4261 /* If we're in a dead branch of IF or something like it, ignore the error */
4262 if (istk
&& istk
->conds
&& !emitting(istk
->conds
->state
))
4266 verror(severity
, fmt
, arg
);
4271 * Because %else etc are evaluated in the state context
4272 * of the previous branch, errors might get lost with error():
4273 * %if 0 ... %else trailing garbage ... %endif
4274 * So %else etc should report errors with this function.
4276 static void error_precond(int severity
, const char *fmt
, ...)
4280 /* Only ignore the error if it's really in a dead branch */
4281 if (istk
&& istk
->conds
&& istk
->conds
->state
== COND_NEVER
)
4285 verror(severity
, fmt
, arg
);
4290 pp_reset(char *file
, int apass
, efunc errfunc
, evalfunc eval
,
4291 ListGen
* listgen
, StrList
**deplist
)
4297 istk
= nasm_malloc(sizeof(Include
));
4300 istk
->expansion
= NULL
;
4302 istk
->fp
= fopen(file
, "r");
4304 src_set_fname(nasm_strdup(file
));
4308 error(ERR_FATAL
|ERR_NOFILE
, "unable to open input file `%s'",
4311 nested_mac_count
= 0;
4312 nested_rep_count
= 0;
4315 if (tasm_compatible_mode
) {
4316 stdmacpos
= nasm_stdmac
;
4318 stdmacpos
= nasm_stdmac_after_tasm
;
4320 any_extrastdmac
= extrastdmac
&& *extrastdmac
;
4326 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4327 * The caller, however, will also pass in 3 for preprocess-only so
4328 * we can set __PASS__ accordingly.
4330 pass
= apass
> 2 ? 2 : apass
;
4332 dephead
= deptail
= deplist
;
4334 StrList
*sl
= nasm_malloc(strlen(file
)+1+sizeof sl
->next
);
4336 strcpy(sl
->str
, file
);
4338 deptail
= &sl
->next
;
4342 * Define the __PASS__ macro. This is defined here unlike
4343 * all the other builtins, because it is special -- it varies between
4346 t
= nasm_malloc(sizeof(*t
));
4348 make_tok_num(t
, apass
);
4350 define_smacro(NULL
, "__PASS__", true, 0, t
);
4353 static char *pp_getline(void)
4360 * Fetch a tokenized line, either from the macro-expansion
4361 * buffer or from the input file.
4364 while (istk
->expansion
&& istk
->expansion
->finishes
) {
4365 Line
*l
= istk
->expansion
;
4366 if (!l
->finishes
->name
&& l
->finishes
->in_progress
> 1) {
4370 * This is a macro-end marker for a macro with no
4371 * name, which means it's not really a macro at all
4372 * but a %rep block, and the `in_progress' field is
4373 * more than 1, meaning that we still need to
4374 * repeat. (1 means the natural last repetition; 0
4375 * means termination by %exitrep.) We have
4376 * therefore expanded up to the %endrep, and must
4377 * push the whole block on to the expansion buffer
4378 * again. We don't bother to remove the macro-end
4379 * marker: we'd only have to generate another one
4382 l
->finishes
->in_progress
--;
4383 for (l
= l
->finishes
->expansion
; l
; l
= l
->next
) {
4384 Token
*t
, *tt
, **tail
;
4386 ll
= nasm_malloc(sizeof(Line
));
4387 ll
->next
= istk
->expansion
;
4388 ll
->finishes
= NULL
;
4392 for (t
= l
->first
; t
; t
= t
->next
) {
4393 if (t
->text
|| t
->type
== TOK_WHITESPACE
) {
4395 new_Token(NULL
, t
->type
, t
->text
, 0);
4400 istk
->expansion
= ll
;
4404 * Check whether a `%rep' was started and not ended
4405 * within this macro expansion. This can happen and
4406 * should be detected. It's a fatal error because
4407 * I'm too confused to work out how to recover
4413 "defining with name in expansion");
4414 else if (istk
->mstk
->name
)
4416 "`%%rep' without `%%endrep' within"
4417 " expansion of macro `%s'",
4422 * FIXME: investigate the relationship at this point between
4423 * istk->mstk and l->finishes
4426 MMacro
*m
= istk
->mstk
;
4427 istk
->mstk
= m
->next_active
;
4430 * This was a real macro call, not a %rep, and
4431 * therefore the parameter information needs to
4434 nasm_free(m
->params
);
4435 free_tlist(m
->iline
);
4436 nasm_free(m
->paramlen
);
4437 l
->finishes
->in_progress
= false;
4441 istk
->expansion
= l
->next
;
4443 list
->downlevel(LIST_MACRO
);
4446 while (1) { /* until we get a line we can use */
4448 if (istk
->expansion
) { /* from a macro expansion */
4450 Line
*l
= istk
->expansion
;
4452 istk
->mstk
->lineno
++;
4454 istk
->expansion
= l
->next
;
4456 p
= detoken(tline
, false);
4457 list
->line(LIST_MACRO
, p
);
4462 if (line
) { /* from the current input file */
4463 line
= prepreproc(line
);
4464 tline
= tokenize(line
);
4469 * The current file has ended; work down the istk
4476 "expected `%%endif' before end of file");
4477 /* only set line and file name if there's a next node */
4479 src_set_linnum(i
->lineno
);
4480 nasm_free(src_set_fname(i
->fname
));
4483 list
->downlevel(LIST_INCLUDE
);
4487 if (istk
->expansion
&& istk
->expansion
->finishes
)
4493 * We must expand MMacro parameters and MMacro-local labels
4494 * _before_ we plunge into directive processing, to cope
4495 * with things like `%define something %1' such as STRUC
4496 * uses. Unless we're _defining_ a MMacro, in which case
4497 * those tokens should be left alone to go into the
4498 * definition; and unless we're in a non-emitting
4499 * condition, in which case we don't want to meddle with
4502 if (!defining
&& !(istk
->conds
&& !emitting(istk
->conds
->state
))
4503 && !(istk
->mstk
&& !istk
->mstk
->in_progress
)) {
4504 tline
= expand_indirect(tline
,0);
4505 tline
= expand_mmac_params(tline
);
4509 * Check the line to see if it's a preprocessor directive.
4511 if (do_directive(tline
) == DIRECTIVE_FOUND
) {
4513 } else if (defining
) {
4515 * We're defining a multi-line macro. We emit nothing
4517 * shove the tokenized line on to the macro definition.
4519 Line
*l
= nasm_malloc(sizeof(Line
));
4520 l
->next
= defining
->expansion
;
4523 defining
->expansion
= l
;
4525 } else if (istk
->conds
&& !emitting(istk
->conds
->state
)) {
4527 * We're in a non-emitting branch of a condition block.
4528 * Emit nothing at all, not even a blank line: when we
4529 * emerge from the condition we'll give a line-number
4530 * directive so we keep our place correctly.
4534 } else if (istk
->mstk
&& !istk
->mstk
->in_progress
) {
4536 * We're in a %rep block which has been terminated, so
4537 * we're walking through to the %endrep without
4538 * emitting anything. Emit nothing at all, not even a
4539 * blank line: when we emerge from the %rep block we'll
4540 * give a line-number directive so we keep our place
4546 tline
= expand_smacro(tline
);
4547 if (!expand_mmacro(tline
)) {
4549 * De-tokenize the line again, and emit it.
4551 line
= detoken(tline
, true);
4555 continue; /* expand_mmacro calls free_tlist */
4563 static void pp_cleanup(int pass
)
4566 if(defining
->name
) {
4568 "end of file while still defining macro `%s'",
4571 error(ERR_NONFATAL
, "end of file while still in %%rep");
4574 free_mmacro(defining
);
4583 nasm_free(i
->fname
);
4588 nasm_free(src_set_fname(NULL
));
4593 while ((i
= ipath
)) {
4602 void pp_include_path(char *path
)
4606 i
= nasm_malloc(sizeof(IncPath
));
4607 i
->path
= path
? nasm_strdup(path
) : NULL
;
4610 if (ipath
!= NULL
) {
4612 while (j
->next
!= NULL
)
4620 void pp_pre_include(char *fname
)
4622 Token
*inc
, *space
, *name
;
4625 name
= new_Token(NULL
, TOK_INTERNAL_STRING
, fname
, 0);
4626 space
= new_Token(name
, TOK_WHITESPACE
, NULL
, 0);
4627 inc
= new_Token(space
, TOK_PREPROC_ID
, "%include", 0);
4629 l
= nasm_malloc(sizeof(Line
));
4636 void pp_pre_define(char *definition
)
4642 equals
= strchr(definition
, '=');
4643 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
4644 def
= new_Token(space
, TOK_PREPROC_ID
, "%define", 0);
4647 space
->next
= tokenize(definition
);
4651 l
= nasm_malloc(sizeof(Line
));
4658 void pp_pre_undefine(char *definition
)
4663 space
= new_Token(NULL
, TOK_WHITESPACE
, NULL
, 0);
4664 def
= new_Token(space
, TOK_PREPROC_ID
, "%undef", 0);
4665 space
->next
= tokenize(definition
);
4667 l
= nasm_malloc(sizeof(Line
));
4675 * Added by Keith Kanios:
4677 * This function is used to assist with "runtime" preprocessor
4678 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4680 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4681 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4684 void pp_runtime(char *definition
)
4688 def
= tokenize(definition
);
4689 if(do_directive(def
) == NO_DIRECTIVE_FOUND
)
4694 void pp_extra_stdmac(macros_t
*macros
)
4696 extrastdmac
= macros
;
4699 static void make_tok_num(Token
* tok
, int64_t val
)
4702 snprintf(numbuf
, sizeof(numbuf
), "%"PRId64
"", val
);
4703 tok
->text
= nasm_strdup(numbuf
);
4704 tok
->type
= TOK_NUMBER
;