2 * TCC - Tiny C Compiler
4 * Copyright (c) 2001-2004 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 /********************************************************/
24 /* global variables */
26 ST_DATA
int tok_flags
;
27 ST_DATA
int parse_flags
;
29 ST_DATA
struct BufferedFile
*file
;
32 ST_DATA
const int *macro_ptr
;
33 ST_DATA CString tokcstr
; /* current parsed string, if any */
35 /* display benchmark infos */
36 ST_DATA
int total_lines
;
37 ST_DATA
int total_bytes
;
38 ST_DATA
int tok_ident
;
39 ST_DATA TokenSym
**table_ident
;
41 ST_DATA TinyAlloc
*toksym_alloc
;
42 ST_DATA TinyAlloc
*tokstr_alloc
;
43 ST_DATA TinyAlloc
*cstr_alloc
;
45 /* ------------------------------------------------------------------------- */
47 static TokenSym
*hash_ident
[TOK_HASH_SIZE
];
48 static char token_buf
[STRING_MAX_SIZE
+ 1];
49 static CString cstr_buf
;
50 static TokenString tokstr_buf
;
51 static unsigned char isidnum_table
[256 - CH_EOF
];
52 /* isidnum_table flags: */
57 static TokenString
*macro_stack
;
59 static const char tcc_keywords
[] =
60 #define DEF(id, str) str "\0"
65 /* WARNING: the content of this string encodes token numbers */
66 static const unsigned char tok_two_chars
[] =
68 "<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253"
69 "-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\313..\250##\266";
90 '.','.', 0xa8, // C++ token ?
91 '#','#', TOK_TWOSHARPS
,
95 static void next_nomacro_spc(void);
97 ST_FUNC
void skip(int c
)
100 tcc_error("'%c' expected (got \"%s\")", c
, get_tok_str(tok
, &tokc
));
104 ST_FUNC
void expect(const char *msg
)
106 tcc_error("%s expected", msg
);
109 ST_FUNC
void begin_macro(TokenString
*str
, int alloc
)
112 str
->prev
= macro_stack
;
113 str
->prev_ptr
= macro_ptr
;
114 macro_ptr
= str
->str
;
118 ST_FUNC
void end_macro(void)
120 TokenString
*str
= macro_stack
;
121 macro_stack
= str
->prev
;
122 macro_ptr
= str
->prev_ptr
;
123 if (str
->alloc
== 2) {
124 str
->alloc
= 3; /* just mark as finished */
126 tok_str_free(str
->str
);
132 /* ------------------------------------------------------------------------- */
133 /* Custom allocator for tiny objects */
135 ST_FUNC TinyAlloc
*tal_new(TinyAlloc
**pal
, size_t limit
, size_t size
)
137 TinyAlloc
*al
= tcc_mallocz(sizeof(TinyAlloc
));
138 al
->p
= al
->buffer
= tcc_malloc(size
);
145 ST_FUNC
void tal_delete(TinyAlloc
*al
)
153 fprintf(stderr
, "limit=%5d, size=%5g MB, nb_peak=%6d, nb_total=%8d, nb_missed=%6d, usage=%5.1f%%\n",
154 al
->limit
, al
->size
/ 1024.0 / 1024.0, al
->nb_peak
, al
->nb_total
, al
->nb_missed
,
155 (al
->peak_p
- al
->buffer
) * 100.0 / al
->size
);
158 if (al
->nb_allocs
> 0) {
159 fprintf(stderr
, "TAL_DEBUG: mem leak %d chunks (limit= %d)\n",
160 al
->nb_allocs
, al
->limit
);
161 uint8_t *p
= al
->buffer
;
163 tal_header_t
*header
= (tal_header_t
*)p
;
164 if (header
->line_num
> 0) {
165 fprintf(stderr
, " file %s, line %u: %u bytes\n",
166 header
->file_name
, header
->line_num
, header
->size
);
168 p
+= header
->size
+ sizeof(tal_header_t
);
173 tcc_free(al
->buffer
);
179 ST_FUNC
void tal_free_impl(TinyAlloc
*al
, void *p TAL_DEBUG_PARAMS
)
184 if (al
->buffer
<= (uint8_t *)p
&& (uint8_t *)p
< al
->buffer
+ al
->size
) {
186 tal_header_t
*header
= (((tal_header_t
*)p
) - 1);
187 if (header
->line_num
< 0) {
188 fprintf(stderr
, "TAL_DEBUG: file %s, line %u double frees chunk from\n",
190 fprintf(stderr
, " file %s, line %u: %u bytes\n",
191 header
->file_name
, -header
->line_num
, header
->size
);
193 header
->line_num
= -header
->line_num
;
198 } else if (al
->next
) {
206 ST_FUNC
void *tal_realloc_impl(TinyAlloc
**pal
, void *p
, size_t size TAL_DEBUG_PARAMS
)
208 tal_header_t
*header
;
211 size_t adj_size
= (size
+ 3) & -4;
212 TinyAlloc
*al
= *pal
;
215 is_own
= (al
->buffer
<= (uint8_t *)p
&& (uint8_t *)p
< al
->buffer
+ al
->size
);
216 if ((!p
|| is_own
) && size
<= al
->limit
) {
217 if (al
->p
+ adj_size
+ sizeof(tal_header_t
) < al
->buffer
+ al
->size
) {
218 header
= (tal_header_t
*)al
->p
;
219 header
->size
= adj_size
;
221 int ofs
= strlen(file
) - TAL_DEBUG_FILE_LEN
;
222 strncpy(header
->file_name
, file
+ (ofs
> 0 ? ofs
: 0), TAL_DEBUG_FILE_LEN
);
223 header
->file_name
[TAL_DEBUG_FILE_LEN
] = 0;
224 header
->line_num
= line
;
226 ret
= al
->p
+ sizeof(tal_header_t
);
227 al
->p
+= adj_size
+ sizeof(tal_header_t
);
229 header
= (((tal_header_t
*)p
) - 1);
230 memcpy(ret
, p
, header
->size
);
232 header
->line_num
= -header
->line_num
;
238 if (al
->nb_peak
< al
->nb_allocs
)
239 al
->nb_peak
= al
->nb_allocs
;
240 if (al
->peak_p
< al
->p
)
247 ret
= tal_realloc(*pal
, 0, size
);
248 header
= (((tal_header_t
*)p
) - 1);
249 memcpy(ret
, p
, header
->size
);
251 header
->line_num
= -header
->line_num
;
258 TinyAlloc
*bottom
= al
, *next
= al
->top
? al
->top
: al
;
260 al
= tal_new(pal
, next
->limit
, next
->size
* 2);
268 ret
= tcc_malloc(size
);
269 header
= (((tal_header_t
*)p
) - 1);
270 memcpy(ret
, p
, header
->size
);
272 header
->line_num
= -header
->line_num
;
274 } else if (al
->next
) {
278 ret
= tcc_realloc(p
, size
);
285 /* ------------------------------------------------------------------------- */
286 /* CString handling */
287 static void cstr_realloc(CString
*cstr
, int new_size
)
292 size
= cstr
->size_allocated
;
294 size
= 8; /* no need to allocate a too small first string */
295 while (size
< new_size
)
297 data
= tal_realloc(cstr_alloc
, cstr
->data_allocated
, size
);
298 cstr
->data_allocated
= data
;
299 cstr
->size_allocated
= size
;
304 ST_FUNC
void cstr_ccat(CString
*cstr
, int ch
)
307 size
= cstr
->size
+ 1;
308 if (size
> cstr
->size_allocated
)
309 cstr_realloc(cstr
, size
);
310 ((unsigned char *)cstr
->data
)[size
- 1] = ch
;
314 ST_FUNC
void cstr_cat(CString
*cstr
, const char *str
, int len
)
318 len
= strlen(str
) + 1 + len
;
319 size
= cstr
->size
+ len
;
320 if (size
> cstr
->size_allocated
)
321 cstr_realloc(cstr
, size
);
322 memmove(((unsigned char *)cstr
->data
) + cstr
->size
, str
, len
);
326 /* add a wide char */
327 ST_FUNC
void cstr_wccat(CString
*cstr
, int ch
)
330 size
= cstr
->size
+ sizeof(nwchar_t
);
331 if (size
> cstr
->size_allocated
)
332 cstr_realloc(cstr
, size
);
333 *(nwchar_t
*)(((unsigned char *)cstr
->data
) + size
- sizeof(nwchar_t
)) = ch
;
337 ST_FUNC
void cstr_new(CString
*cstr
)
339 memset(cstr
, 0, sizeof(CString
));
342 /* free string and reset it to NULL */
343 ST_FUNC
void cstr_free(CString
*cstr
)
345 tal_free(cstr_alloc
, cstr
->data_allocated
);
349 /* reset string to empty */
350 ST_FUNC
void cstr_reset(CString
*cstr
)
356 static void add_char(CString
*cstr
, int c
)
358 if (c
== '\'' || c
== '\"' || c
== '\\') {
359 /* XXX: could be more precise if char or string */
360 cstr_ccat(cstr
, '\\');
362 if (c
>= 32 && c
<= 126) {
365 cstr_ccat(cstr
, '\\');
367 cstr_ccat(cstr
, 'n');
369 cstr_ccat(cstr
, '0' + ((c
>> 6) & 7));
370 cstr_ccat(cstr
, '0' + ((c
>> 3) & 7));
371 cstr_ccat(cstr
, '0' + (c
& 7));
376 /* ------------------------------------------------------------------------- */
377 /* allocate a new token */
378 static TokenSym
*tok_alloc_new(TokenSym
**pts
, const char *str
, int len
)
380 TokenSym
*ts
, **ptable
;
383 if (tok_ident
>= SYM_FIRST_ANOM
)
384 tcc_error("memory full (symbols)");
386 /* expand token table if needed */
387 i
= tok_ident
- TOK_IDENT
;
388 if ((i
% TOK_ALLOC_INCR
) == 0) {
389 ptable
= tcc_realloc(table_ident
, (i
+ TOK_ALLOC_INCR
) * sizeof(TokenSym
*));
390 table_ident
= ptable
;
393 ts
= tal_realloc(toksym_alloc
, 0, sizeof(TokenSym
) + len
);
395 ts
->tok
= tok_ident
++;
396 ts
->sym_define
= NULL
;
397 ts
->sym_label
= NULL
;
398 ts
->sym_struct
= NULL
;
399 ts
->sym_identifier
= NULL
;
401 ts
->hash_next
= NULL
;
402 memcpy(ts
->str
, str
, len
);
408 #define TOK_HASH_INIT 1
409 #define TOK_HASH_FUNC(h, c) ((h) + ((h) << 5) + ((h) >> 27) + (c))
412 /* find a token and add it if not found */
413 ST_FUNC TokenSym
*tok_alloc(const char *str
, int len
)
421 h
= TOK_HASH_FUNC(h
, ((unsigned char *)str
)[i
]);
422 h
&= (TOK_HASH_SIZE
- 1);
424 pts
= &hash_ident
[h
];
429 if (ts
->len
== len
&& !memcmp(ts
->str
, str
, len
))
431 pts
= &(ts
->hash_next
);
433 return tok_alloc_new(pts
, str
, len
);
436 /* XXX: buffer overflow */
437 /* XXX: float tokens */
438 ST_FUNC
const char *get_tok_str(int v
, CValue
*cv
)
443 cstr_reset(&cstr_buf
);
449 /* XXX: not quite exact, but only useful for testing */
450 sprintf(p
, "%llu", (unsigned long long)cv
->i
);
454 /* XXX: not quite exact, but only useful for testing */
456 sprintf(p
, "%u", (unsigned)cv
->i
);
458 sprintf(p
, "%llu", (unsigned long long)cv
->i
);
462 cstr_ccat(&cstr_buf
, 'L');
464 cstr_ccat(&cstr_buf
, '\'');
465 add_char(&cstr_buf
, cv
->i
);
466 cstr_ccat(&cstr_buf
, '\'');
467 cstr_ccat(&cstr_buf
, '\0');
471 return (char*)cv
->str
.data
;
473 cstr_ccat(&cstr_buf
, 'L');
475 cstr_ccat(&cstr_buf
, '\"');
477 len
= cv
->str
.size
- 1;
479 add_char(&cstr_buf
, ((unsigned char *)cv
->str
.data
)[i
]);
481 len
= (cv
->str
.size
/ sizeof(nwchar_t
)) - 1;
483 add_char(&cstr_buf
, ((nwchar_t
*)cv
->str
.data
)[i
]);
485 cstr_ccat(&cstr_buf
, '\"');
486 cstr_ccat(&cstr_buf
, '\0');
490 cstr_cat(&cstr_buf
, "<float>", 0);
493 cstr_cat(&cstr_buf
, "<double>", 0);
496 cstr_cat(&cstr_buf
, "<long double>", 0);
499 cstr_cat(&cstr_buf
, "<linenumber>", 0);
502 /* above tokens have value, the ones below don't */
511 return strcpy(p
, "...");
513 return strcpy(p
, "<<=");
515 return strcpy(p
, ">>=");
518 /* search in two bytes table */
519 const unsigned char *q
= tok_two_chars
;
525 return cstr_buf
.data
;
530 sprintf(cstr_buf
.data
, "<%02x>", v
);
531 return cstr_buf
.data
;
536 } else if (v
< tok_ident
) {
537 return table_ident
[v
- TOK_IDENT
]->str
;
538 } else if (v
>= SYM_FIRST_ANOM
) {
539 /* special name for anonymous symbol */
540 sprintf(p
, "L.%u", v
- SYM_FIRST_ANOM
);
542 /* should never happen */
547 return cstr_buf
.data
;
550 /* return the current character, handling end of block if necessary
552 ST_FUNC
int handle_eob(void)
554 BufferedFile
*bf
= file
;
557 /* only tries to read if really end of buffer */
558 if (bf
->buf_ptr
>= bf
->buf_end
) {
560 #if defined(PARSE_DEBUG)
565 len
= read(bf
->fd
, bf
->buffer
, len
);
572 bf
->buf_ptr
= bf
->buffer
;
573 bf
->buf_end
= bf
->buffer
+ len
;
574 *bf
->buf_end
= CH_EOB
;
576 if (bf
->buf_ptr
< bf
->buf_end
) {
577 return bf
->buf_ptr
[0];
579 bf
->buf_ptr
= bf
->buf_end
;
584 /* read next char from current input file and handle end of input buffer */
585 ST_INLN
void inp(void)
587 ch
= *(++(file
->buf_ptr
));
588 /* end of buffer/file handling */
593 /* handle '\[\r]\n' */
594 static int handle_stray_noerror(void)
601 } else if (ch
== '\r') {
615 static void handle_stray(void)
617 if (handle_stray_noerror())
618 tcc_error("stray '\\' in program");
621 /* skip the stray and handle the \\n case. Output an error if
622 incorrect char after the stray */
623 static int handle_stray1(uint8_t *p
)
628 if (p
>= file
->buf_end
) {
635 if (handle_stray_noerror()) {
636 if (!(parse_flags
& PARSE_FLAG_ACCEPT_STRAYS
))
637 tcc_error("stray '\\' in program");
638 *--file
->buf_ptr
= '\\';
645 /* handle just the EOB case, but not stray */
646 #define PEEKC_EOB(c, p)\
657 /* handle the complicated stray case */
663 c = handle_stray1(p);\
668 /* input with '\[\r]\n' handling. Note that this function cannot
669 handle other characters after '\', so you cannot call it inside
670 strings or comments */
671 ST_FUNC
void minp(void)
678 static void pp_line(TCCState
*s1
, BufferedFile
*f
, int level
)
681 int d
= f
->line_num
- f
->line_ref
;
682 if (s1
->Pflag
== LINE_MACRO_OUTPUT_FORMAT_NONE
683 || (level
== 0 && f
->line_ref
&& d
< 8)) {
685 fputs("\n", s1
->ppfp
), --d
;
686 } else if (s1
->Pflag
== LINE_MACRO_OUTPUT_FORMAT_STD
) {
687 fprintf(s1
->ppfp
, "#line %d \"%s\"\n", f
->line_num
, f
->filename
);
689 fprintf(s1
->ppfp
, "# %d \"%s\"%s\n", f
->line_num
, f
->filename
,
690 level
> 0 ? " 1" : level
< 0 ? " 2" : "");
693 f
->line_ref
= f
->line_num
;
696 static uint8_t *parse_print_comment(uint8_t *p
, int is_line_comment
)
699 FILE *ppfp
= tcc_state
->ppfp
;
701 if ((file
->line_num
- file
->line_ref
) > 0) {
704 pp_line(tcc_state
, file
, 0);
707 fputs("//", ppfp
); else
719 if (is_line_comment
) break;
720 tcc_error("unexpected end of file in comment");
725 if ((c
== '/') && star_count
&& !is_line_comment
)
729 if (is_line_comment
) break;
736 if (!is_line_comment
) {
739 file
->line_ref
= file
->line_num
;
744 /* single line C++ comments */
745 static uint8_t *parse_line_comment(uint8_t *p
, int skip
)
750 if (tcc_state
->option_C
&& !skip
)
751 return parse_print_comment(p
, 1);
755 if (c
== '\n' || c
== CH_EOF
) {
757 } else if (c
== '\\') {
766 } else if (c
== '\r') {
784 ST_FUNC
uint8_t *parse_comment(uint8_t *p
, int skip
)
789 if (tcc_state
->option_C
&& !skip
)
790 return parse_print_comment(p
, 0);
795 if (c
== '\n' || c
== '*' || c
== '\\')
799 if (c
== '\n' || c
== '*' || c
== '\\')
803 /* now we can handle all the cases */
807 } else if (c
== '*') {
813 } else if (c
== '/') {
815 } else if (c
== '\\') {
820 tcc_error("unexpected end of file in comment");
822 /* skip '\[\r]\n', otherwise just skip the stray */
828 } else if (c
== '\r') {
845 /* stray, eob or eof */
850 tcc_error("unexpected end of file in comment");
851 } else if (c
== '\\') {
863 static inline void skip_spaces(void)
865 while (isidnum_table
[ch
- CH_EOF
] & IS_SPC
)
869 static inline int check_space(int t
, int *spc
)
871 if (t
< 256 && (isidnum_table
[t
- CH_EOF
] & IS_SPC
)) {
880 /* parse a string without interpreting escapes */
881 static uint8_t *parse_pp_string(uint8_t *p
,
882 int sep
, CString
*str
)
890 } else if (c
== '\\') {
896 /* XXX: indicate line number of start of string */
897 tcc_error("missing terminating %c character", sep
);
898 } else if (c
== '\\') {
899 /* escape : just skip \[\r]\n */
904 } else if (c
== '\r') {
907 expect("'\n' after '\r'");
910 } else if (c
== CH_EOF
) {
911 goto unterminated_string
;
914 cstr_ccat(str
, '\\');
920 } else if (c
== '\n') {
923 } else if (c
== '\r') {
927 cstr_ccat(str
, '\r');
943 /* skip block of text until #else, #elif or #endif. skip also pairs of
945 static void preprocess_skip(void)
947 int a
, start_of_line
, c
, in_warn_or_error
;
954 in_warn_or_error
= 0;
975 } else if (c
== '\\') {
976 ch
= file
->buf_ptr
[0];
977 handle_stray_noerror();
984 if (in_warn_or_error
)
986 p
= parse_pp_string(p
, c
, NULL
);
990 if (in_warn_or_error
)
997 p
= parse_comment(p
,1);
998 } else if (ch
== '/') {
999 p
= parse_line_comment(p
,1);
1004 if (start_of_line
) {
1009 (tok
== TOK_ELSE
|| tok
== TOK_ELIF
|| tok
== TOK_ENDIF
))
1011 if (tok
== TOK_IF
|| tok
== TOK_IFDEF
|| tok
== TOK_IFNDEF
)
1013 else if (tok
== TOK_ENDIF
)
1015 else if( tok
== TOK_ERROR
|| tok
== TOK_WARNING
)
1016 in_warn_or_error
= 1;
1017 else if (tok
== TOK_LINEFEED
)
1019 else if (parse_flags
& PARSE_FLAG_ASM_FILE
)
1020 p
= parse_line_comment(p
,0);
1021 } else if (parse_flags
& PARSE_FLAG_ASM_FILE
)
1022 p
= parse_line_comment(p
,0);
1035 /* ParseState handling */
1037 /* XXX: currently, no include file info is stored. Thus, we cannot display
1038 accurate messages if the function or data definition spans multiple
1041 /* save current parse state in 's' */
1042 ST_FUNC
void save_parse_state(ParseState
*s
)
1044 s
->line_num
= file
->line_num
;
1045 s
->macro_ptr
= macro_ptr
;
1050 /* restore parse state from 's' */
1051 ST_FUNC
void restore_parse_state(ParseState
*s
)
1053 file
->line_num
= s
->line_num
;
1054 macro_ptr
= s
->macro_ptr
;
1059 /* return the number of additional 'ints' necessary to store the
1061 static inline int tok_size(const int *p
)
1076 return 1 + ((sizeof(CString
) + ((CString
*)(p
+1))->size
+ 3) >> 2);
1082 return 1 + LDOUBLE_SIZE
/ 4;
1088 /* token string handling */
1090 ST_INLN
void tok_str_new(TokenString
*s
)
1094 s
->allocated_len
= 0;
1095 s
->last_line_num
= -1;
1098 ST_FUNC
int *tok_str_dup(TokenString
*s
)
1102 str
= tal_realloc(tokstr_alloc
, 0, s
->len
* sizeof(int));
1103 memcpy(str
, s
->str
, s
->len
* sizeof(int));
1107 ST_FUNC
void tok_str_free(int *str
)
1109 tal_free(tokstr_alloc
, str
);
1112 ST_FUNC
int *tok_str_realloc(TokenString
*s
, int new_size
)
1116 size
= s
->allocated_len
;
1119 while (size
< new_size
)
1121 TCC_ASSERT((size
& (size
-1)) == 0);
1122 if (size
> s
->allocated_len
) {
1123 str
= tal_realloc(tokstr_alloc
, s
->str
, size
* sizeof(int));
1124 s
->allocated_len
= size
;
1130 ST_FUNC
void tok_str_add(TokenString
*s
, int t
)
1136 if (len
>= s
->allocated_len
)
1137 str
= tok_str_realloc(s
, len
+ 1);
1142 static void tok_str_add2(TokenString
*s
, int t
, CValue
*cv
)
1149 /* allocate space for worst case */
1150 if (len
+ TOK_MAX_SIZE
>= s
->allocated_len
)
1151 str
= tok_str_realloc(s
, len
+ TOK_MAX_SIZE
+ 1);
1160 str
[len
++] = cv
->tab
[0];
1167 /* Insert the string into the int array. */
1169 1 + (cv
->str
.size
+ sizeof(int) - 1) / sizeof(int);
1170 if (len
+ nb_words
>= s
->allocated_len
)
1171 str
= tok_str_realloc(s
, len
+ nb_words
+ 1);
1172 str
[len
] = cv
->str
.size
;
1173 memcpy(&str
[len
+ 1], cv
->str
.data
, cv
->str
.size
);
1180 #if LDOUBLE_SIZE == 8
1183 str
[len
++] = cv
->tab
[0];
1184 str
[len
++] = cv
->tab
[1];
1186 #if LDOUBLE_SIZE == 12
1188 str
[len
++] = cv
->tab
[0];
1189 str
[len
++] = cv
->tab
[1];
1190 str
[len
++] = cv
->tab
[2];
1191 #elif LDOUBLE_SIZE == 16
1193 str
[len
++] = cv
->tab
[0];
1194 str
[len
++] = cv
->tab
[1];
1195 str
[len
++] = cv
->tab
[2];
1196 str
[len
++] = cv
->tab
[3];
1197 #elif LDOUBLE_SIZE != 8
1198 #error add long double size support
1207 /* add the current parse token in token string 's' */
1208 ST_FUNC
void tok_str_add_tok(TokenString
*s
)
1212 /* save line number info */
1213 if (file
->line_num
!= s
->last_line_num
) {
1214 s
->last_line_num
= file
->line_num
;
1215 cval
.i
= s
->last_line_num
;
1216 tok_str_add2(s
, TOK_LINENUM
, &cval
);
1218 tok_str_add2(s
, tok
, &tokc
);
1221 /* get a token from an integer array and increment pointer
1222 accordingly. we code it as a macro to avoid pointer aliasing. */
1223 static inline void TOK_GET(int *t
, const int **pp
, CValue
*cv
)
1242 cv
->str
.size
= *p
++;
1244 cv
->str
.data_allocated
= 0;
1245 p
+= (cv
->str
.size
+ sizeof(int) - 1) / sizeof(int);
1253 #if LDOUBLE_SIZE == 16
1255 #elif LDOUBLE_SIZE == 12
1257 #elif LDOUBLE_SIZE == 8
1260 # error add long double size support
1273 /* Calling this function is expensive, but it is not possible
1274 to read a token string backwards. */
1275 static int tok_last(const int *str0
, const int *str1
)
1277 const int *str
= str0
;
1282 TOK_GET(&tok
, &str
, &cval
);
1286 static int macro_is_equal(const int *a
, const int *b
)
1291 /* first time preallocate static cstr_buf, next time only reset position to start */
1292 cstr_reset(&cstr_buf
);
1293 TOK_GET(&t
, &a
, &cv
);
1294 cstr_cat(&cstr_buf
, get_tok_str(t
, &cv
), 0);
1295 TOK_GET(&t
, &b
, &cv
);
1296 if (strcmp(cstr_buf
.data
, get_tok_str(t
, &cv
)))
1302 static void tok_print(const char *msg
, const int *str
)
1304 FILE *pr
= tcc_state
->dffp
;
1308 fprintf(pr
, "%s ", msg
);
1310 TOK_GET(&t
, &str
, &cval
);
1313 fprintf(pr
,"%s", get_tok_str(t
, &cval
));
1318 static int define_print_prepared(Sym
*s
)
1320 if (!s
|| !tcc_state
->dffp
|| tcc_state
->dflag
== 0)
1323 if (s
->v
< TOK_IDENT
|| s
->v
>= tok_ident
)
1326 if (file
&& tcc_state
->dflag
== 'D') {
1328 pp_line(tcc_state
, file
, 0);
1329 file
->line_ref
= ++file
->line_num
;
1334 static void define_print(int v
)
1336 FILE *pr
= tcc_state
->dffp
;
1340 if (define_print_prepared(s
) == 0)
1343 fprintf(pr
, "// #define %s", get_tok_str(v
, NULL
));
1344 if (s
->type
.t
== MACRO_FUNC
) {
1349 fprintf(pr
,"%s", get_tok_str(a
->v
& ~SYM_FIELD
, NULL
));
1356 tok_print("", s
->d
);
1359 static void undef_print(int v
)
1361 FILE *pr
= tcc_state
->dffp
;
1365 if (define_print_prepared(s
) == 0)
1368 fprintf(pr
, "// #undef %s\n", get_tok_str(s
->v
, NULL
));
1371 ST_FUNC
void print_defines(void)
1373 Sym
*top
= define_stack
;
1375 define_print(top
->v
);
1380 /* defines handling */
1381 ST_INLN
void define_push(int v
, int macro_type
, TokenString
*str
, Sym
*first_arg
)
1387 if (s
&& !macro_is_equal(s
->d
, str
->str
))
1388 tcc_warning("%s redefined", get_tok_str(v
, NULL
));
1391 s
= sym_push2(&define_stack
, v
, macro_type
, 0);
1392 s
->d
= str
? tok_str_dup(str
) : NULL
;
1393 s
->next
= first_arg
;
1394 table_ident
[v
- TOK_IDENT
]->sym_define
= s
;
1397 /* undefined a define symbol. Its name is just set to zero */
1398 ST_FUNC
void define_undef(Sym
*s
)
1402 if (v
>= TOK_IDENT
&& v
< tok_ident
)
1403 table_ident
[v
- TOK_IDENT
]->sym_define
= NULL
;
1406 ST_INLN Sym
*define_find(int v
)
1409 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
1411 return table_ident
[v
]->sym_define
;
1414 /* free define stack until top reaches 'b' */
1415 ST_FUNC
void free_defines(Sym
*b
)
1423 /* do not free args or predefined defines */
1425 tok_str_free(top
->d
);
1427 if (v
>= TOK_IDENT
&& v
< tok_ident
)
1428 table_ident
[v
- TOK_IDENT
]->sym_define
= NULL
;
1436 ST_FUNC Sym
*label_find(int v
)
1439 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
1441 return table_ident
[v
]->sym_label
;
1444 ST_FUNC Sym
*label_push(Sym
**ptop
, int v
, int flags
)
1447 s
= sym_push2(ptop
, v
, 0, 0);
1449 ps
= &table_ident
[v
- TOK_IDENT
]->sym_label
;
1450 if (ptop
== &global_label_stack
) {
1451 /* modify the top most local identifier, so that
1452 sym_identifier will point to 's' when popped */
1454 ps
= &(*ps
)->prev_tok
;
1461 /* pop labels until element last is reached. Look if any labels are
1462 undefined. Define symbols if '&&label' was used. */
1463 ST_FUNC
void label_pop(Sym
**ptop
, Sym
*slast
)
1466 for(s
= *ptop
; s
!= slast
; s
= s1
) {
1468 if (s
->r
== LABEL_DECLARED
) {
1469 tcc_warning("label '%s' declared but not used", get_tok_str(s
->v
, NULL
));
1470 } else if (s
->r
== LABEL_FORWARD
) {
1471 tcc_error("label '%s' used but not defined",
1472 get_tok_str(s
->v
, NULL
));
1475 /* define corresponding symbol. A size of
1477 put_extern_sym(s
, cur_text_section
, s
->jnext
, 1);
1481 table_ident
[s
->v
- TOK_IDENT
]->sym_label
= s
->prev_tok
;
1487 /* eval an expression for #if/#elif */
1488 static int expr_preprocess(void)
1494 while (tok
!= TOK_LINEFEED
&& tok
!= TOK_EOF
) {
1495 next(); /* do macro subst */
1496 if (tok
== TOK_DEFINED
) {
1501 c
= define_find(tok
) != 0;
1506 } else if (tok
>= TOK_IDENT
) {
1507 /* if undefined macro */
1511 tok_str_add_tok(&str
);
1513 tok_str_add(&str
, -1); /* simulate end of file */
1514 tok_str_add(&str
, 0);
1515 /* now evaluate C constant expression */
1516 begin_macro(&str
, 0);
1524 /* parse after #define */
1525 ST_FUNC
void parse_define(void)
1527 Sym
*s
, *first
, **ps
;
1528 int v
, t
, varg
, is_vaargs
, spc
;
1529 int saved_parse_flags
= parse_flags
;
1533 tcc_error("invalid macro name '%s'", get_tok_str(tok
, &tokc
));
1534 /* XXX: should check if same macro (ANSI) */
1537 /* '(' must be just after macro definition for MACRO_FUNC */
1538 parse_flags
|= PARSE_FLAG_SPACES
;
1541 /* must be able to parse TOK_DOTS (in asm mode '.' can be part of identifier) */
1542 parse_flags
&= ~PARSE_FLAG_ASM_FILE
;
1543 isidnum_table
['.' - CH_EOF
] = 0;
1546 if (tok
!= ')') for (;;) {
1550 if (varg
== TOK_DOTS
) {
1551 varg
= TOK___VA_ARGS__
;
1553 } else if (tok
== TOK_DOTS
&& gnu_ext
) {
1557 if (varg
< TOK_IDENT
)
1559 tcc_error("bad macro parameter list");
1560 s
= sym_push2(&define_stack
, varg
| SYM_FIELD
, is_vaargs
, 0);
1565 if (tok
!= ',' || is_vaargs
)
1571 parse_flags
|= (saved_parse_flags
& PARSE_FLAG_ASM_FILE
);
1572 isidnum_table
['.' - CH_EOF
] =
1573 (parse_flags
& PARSE_FLAG_ASM_FILE
) ? IS_ID
: 0;
1578 parse_flags
|= PARSE_FLAG_ACCEPT_STRAYS
| PARSE_FLAG_SPACES
| PARSE_FLAG_LINEFEED
;
1579 while (tok
!= TOK_LINEFEED
&& tok
!= TOK_EOF
) {
1580 /* remove spaces around ## and after '#' */
1581 if (TOK_TWOSHARPS
== tok
) {
1587 } else if ('#' == tok
) {
1589 } else if (check_space(tok
, &spc
)) {
1592 tok_str_add2(&tokstr_buf
, tok
, &tokc
);
1597 parse_flags
= saved_parse_flags
;
1599 --tokstr_buf
.len
; /* remove trailing space */
1600 tok_str_add(&tokstr_buf
, 0);
1603 tcc_error("'##' cannot appear at either end of macro");
1604 define_push(v
, t
, &tokstr_buf
, first
);
1608 static inline int hash_cached_include(const char *filename
)
1610 const unsigned char *s
;
1614 s
= (unsigned char *) filename
;
1616 h
= TOK_HASH_FUNC(h
, *s
);
1619 h
&= (CACHED_INCLUDES_HASH_SIZE
- 1);
1623 static CachedInclude
*search_cached_include(TCCState
*s1
, const char *filename
)
1627 h
= hash_cached_include(filename
);
1628 i
= s1
->cached_includes_hash
[h
];
1632 e
= s1
->cached_includes
[i
- 1];
1633 if (0 == PATHCMP(e
->filename
, filename
))
1640 static inline void add_cached_include(TCCState
*s1
, const char *filename
, int ifndef_macro
)
1645 if (search_cached_include(s1
, filename
))
1648 printf("adding cached '%s' %s\n", filename
, get_tok_str(ifndef_macro
, NULL
));
1650 e
= tcc_malloc(sizeof(CachedInclude
) + strlen(filename
));
1651 strcpy(e
->filename
, filename
);
1652 e
->ifndef_macro
= ifndef_macro
;
1653 dynarray_add((void ***)&s1
->cached_includes
, &s1
->nb_cached_includes
, e
);
1654 /* add in hash table */
1655 h
= hash_cached_include(filename
);
1656 e
->hash_next
= s1
->cached_includes_hash
[h
];
1657 s1
->cached_includes_hash
[h
] = s1
->nb_cached_includes
;
1660 #define ONCE_PREFIX "#ONCE#"
1662 static void pragma_parse(TCCState
*s1
)
1665 if (tok
== TOK_push_macro
|| tok
== TOK_pop_macro
) {
1669 if (next(), tok
!= '(')
1671 if (next(), tok
!= TOK_STR
)
1673 v
= tok_alloc(tokc
.str
.data
, tokc
.str
.size
- 1)->tok
;
1674 if (next(), tok
!= ')')
1676 if (t
== TOK_push_macro
) {
1677 while (NULL
== (s
= define_find(v
)))
1678 define_push(v
, 0, NULL
, NULL
);
1679 s
->type
.ref
= s
; /* set push boundary */
1681 for (s
= define_stack
; s
; s
= s
->prev
)
1682 if (s
->v
== v
&& s
->type
.ref
== s
) {
1688 table_ident
[v
- TOK_IDENT
]->sym_define
= s
->d
? s
: NULL
;
1690 tcc_warning("unbalanced #pragma pop_macro");
1692 } else if (tok
== TOK_once
) {
1693 char buf1
[sizeof(file
->filename
) + sizeof(ONCE_PREFIX
)];
1694 strcpy(buf1
, ONCE_PREFIX
);
1695 strcat(buf1
, file
->filename
);
1699 add_cached_include(s1
, file
->filename
, tok_alloc(buf1
, strlen(buf1
))->tok
);
1700 } else if (s1
->ppfp
) {
1701 /* tcc -E: keep pragmas below unchanged */
1703 unget_tok(TOK_PRAGMA
);
1705 unget_tok(TOK_LINEFEED
);
1707 } else if (tok
== TOK_pack
) {
1709 #pragma pack(1) // set
1710 #pragma pack() // reset to default
1711 #pragma pack(push,1) // push & set
1712 #pragma pack(pop) // restore previous */
1715 if (tok
== TOK_ASM_pop
) {
1717 if (s1
->pack_stack_ptr
<= s1
->pack_stack
) {
1719 tcc_error("out of pack stack");
1721 s1
->pack_stack_ptr
--;
1725 if (tok
== TOK_ASM_push
) {
1727 if (s1
->pack_stack_ptr
>= s1
->pack_stack
+ PACK_STACK_SIZE
- 1)
1729 s1
->pack_stack_ptr
++;
1732 if (tok
!= TOK_CINT
)
1735 if (val
< 1 || val
> 16 || (val
& (val
- 1)) != 0)
1739 *s1
->pack_stack_ptr
= val
;
1744 } else if (tok
== TOK_comment
) {
1754 file
= tcc_strdup((char *)tokc
.str
.data
);
1755 dynarray_add((void ***)&s1
->pragma_libs
, &s1
->nb_pragma_libs
, file
);
1761 if (s1
->warn_unsupported
)
1762 tcc_warning("#pragma %s is ignored", get_tok_str(tok
, &tokc
));
1767 tcc_error("malformed #pragma directive");
1771 /* is_bof is true if first non space token at beginning of file */
1772 ST_FUNC
void preprocess(int is_bof
)
1774 TCCState
*s1
= tcc_state
;
1775 int i
, c
, n
, saved_parse_flags
;
1779 saved_parse_flags
= parse_flags
;
1780 parse_flags
= PARSE_FLAG_PREPROCESS
1781 | PARSE_FLAG_TOK_NUM
1782 | PARSE_FLAG_TOK_STR
1783 | PARSE_FLAG_LINEFEED
1784 | (parse_flags
& PARSE_FLAG_ASM_FILE
)
1796 s
= define_find(tok
);
1797 /* undefine symbol by putting an invalid name */
1802 case TOK_INCLUDE_NEXT
:
1803 ch
= file
->buf_ptr
[0];
1804 /* XXX: incorrect if comments : use next_nomacro with a special mode */
1809 } else if (ch
== '\"') {
1814 while (ch
!= c
&& ch
!= '\n' && ch
!= CH_EOF
) {
1815 if ((q
- buf
) < sizeof(buf
) - 1)
1818 if (handle_stray_noerror() == 0)
1826 /* eat all spaces and comments after include */
1827 /* XXX: slightly incorrect */
1828 while (ch1
!= '\n' && ch1
!= CH_EOF
)
1832 /* computed #include : either we have only strings or
1833 we have anything enclosed in '<>' */
1836 if (tok
== TOK_STR
) {
1837 while (tok
!= TOK_LINEFEED
) {
1838 if (tok
!= TOK_STR
) {
1840 tcc_error("'#include' expects \"FILENAME\" or <FILENAME>");
1842 pstrcat(buf
, sizeof(buf
), (char *)tokc
.str
.data
);
1848 while (tok
!= TOK_LINEFEED
) {
1849 pstrcat(buf
, sizeof(buf
), get_tok_str(tok
, &tokc
));
1853 /* check syntax and remove '<>' */
1854 if (len
< 2 || buf
[0] != '<' || buf
[len
- 1] != '>')
1855 goto include_syntax
;
1856 memmove(buf
, buf
+ 1, len
- 2);
1857 buf
[len
- 2] = '\0';
1862 if (s1
->include_stack_ptr
>= s1
->include_stack
+ INCLUDE_STACK_SIZE
)
1863 tcc_error("#include recursion too deep");
1864 /* store current file in stack, but increment stack later below */
1865 *s1
->include_stack_ptr
= file
;
1866 i
= tok
== TOK_INCLUDE_NEXT
? file
->include_next_index
: 0;
1867 n
= 2 + s1
->nb_include_paths
+ s1
->nb_sysinclude_paths
;
1868 for (; i
< n
; ++i
) {
1869 char buf1
[sizeof file
->filename
];
1874 /* check absolute include path */
1875 if (!IS_ABSPATH(buf
))
1879 } else if (i
== 1) {
1880 /* search in current dir if "header.h" */
1883 path
= file
->filename
;
1884 pstrncpy(buf1
, path
, tcc_basename(path
) - path
);
1887 /* search in all the include paths */
1888 int j
= i
- 2, k
= j
- s1
->nb_include_paths
;
1889 path
= k
< 0 ? s1
->include_paths
[j
] : s1
->sysinclude_paths
[k
];
1890 if (path
== 0) continue;
1891 pstrcpy(buf1
, sizeof(buf1
), path
);
1892 pstrcat(buf1
, sizeof(buf1
), "/");
1895 pstrcat(buf1
, sizeof(buf1
), buf
);
1896 e
= search_cached_include(s1
, buf1
);
1897 if (e
&& define_find(e
->ifndef_macro
)) {
1898 /* no need to parse the include because the 'ifndef macro'
1901 printf("%s: skipping cached %s\n", file
->filename
, buf1
);
1906 if (tcc_open(s1
, buf1
) < 0)
1909 file
->include_next_index
= i
+ 1;
1911 printf("%s: including %s\n", file
->prev
->filename
, file
->filename
);
1913 /* update target deps */
1914 dynarray_add((void ***)&s1
->target_deps
, &s1
->nb_target_deps
,
1916 /* push current file in stack */
1917 ++s1
->include_stack_ptr
;
1918 /* add include file debug info */
1920 put_stabs(file
->filename
, N_BINCL
, 0, 0, 0);
1921 tok_flags
|= TOK_FLAG_BOF
| TOK_FLAG_BOL
;
1922 ch
= file
->buf_ptr
[0];
1925 tcc_error("include file '%s' not found", buf
);
1932 c
= expr_preprocess();
1938 if (tok
< TOK_IDENT
)
1939 tcc_error("invalid argument for '#if%sdef'", c
? "n" : "");
1943 printf("#ifndef %s\n", get_tok_str(tok
, NULL
));
1945 file
->ifndef_macro
= tok
;
1948 c
= (define_find(tok
) != 0) ^ c
;
1950 if (s1
->ifdef_stack_ptr
>= s1
->ifdef_stack
+ IFDEF_STACK_SIZE
)
1951 tcc_error("memory full (ifdef)");
1952 *s1
->ifdef_stack_ptr
++ = c
;
1955 if (s1
->ifdef_stack_ptr
== s1
->ifdef_stack
)
1956 tcc_error("#else without matching #if");
1957 if (s1
->ifdef_stack_ptr
[-1] & 2)
1958 tcc_error("#else after #else");
1959 c
= (s1
->ifdef_stack_ptr
[-1] ^= 3);
1962 if (s1
->ifdef_stack_ptr
== s1
->ifdef_stack
)
1963 tcc_error("#elif without matching #if");
1964 c
= s1
->ifdef_stack_ptr
[-1];
1966 tcc_error("#elif after #else");
1967 /* last #if/#elif expression was true: we skip */
1970 c
= expr_preprocess();
1971 s1
->ifdef_stack_ptr
[-1] = c
;
1973 if (s1
->ifdef_stack_ptr
== file
->ifdef_stack_ptr
+ 1)
1974 file
->ifndef_macro
= 0;
1984 if (s1
->ifdef_stack_ptr
<= file
->ifdef_stack_ptr
)
1985 tcc_error("#endif without matching #if");
1986 s1
->ifdef_stack_ptr
--;
1987 /* '#ifndef macro' was at the start of file. Now we check if
1988 an '#endif' is exactly at the end of file */
1989 if (file
->ifndef_macro
&&
1990 s1
->ifdef_stack_ptr
== file
->ifdef_stack_ptr
) {
1991 file
->ifndef_macro_saved
= file
->ifndef_macro
;
1992 /* need to set to zero to avoid false matches if another
1993 #ifndef at middle of file */
1994 file
->ifndef_macro
= 0;
1995 while (tok
!= TOK_LINEFEED
)
1997 tok_flags
|= TOK_FLAG_ENDIF
;
2002 n
= strtoul((char*)tokc
.str
.data
, &q
, 10);
2006 if (tok
!= TOK_CINT
)
2008 tcc_error("wrong #line format");
2012 if (tok
!= TOK_LINEFEED
) {
2014 pstrcpy(file
->filename
, sizeof(file
->filename
), (char *)tokc
.str
.data
);
2015 else if (parse_flags
& PARSE_FLAG_ASM_FILE
)
2022 total_lines
+= file
->line_num
- n
;
2025 put_stabs(file
->filename
, N_BINCL
, 0, 0, 0);
2030 ch
= file
->buf_ptr
[0];
2033 while (ch
!= '\n' && ch
!= CH_EOF
) {
2034 if ((q
- buf
) < sizeof(buf
) - 1)
2037 if (handle_stray_noerror() == 0)
2044 tcc_error("#error %s", buf
);
2046 tcc_warning("#warning %s", buf
);
2054 /* ignore gas line comment in an 'S' file. */
2055 if (saved_parse_flags
& PARSE_FLAG_ASM_FILE
)
2057 if (tok
== '!' && is_bof
)
2058 /* '!' is ignored at beginning to allow C scripts. */
2060 tcc_warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok
, &tokc
));
2062 file
->buf_ptr
= parse_line_comment(file
->buf_ptr
,0);
2065 /* ignore other preprocess commands or #! for C scripts */
2066 while (tok
!= TOK_LINEFEED
)
2069 parse_flags
= saved_parse_flags
;
2072 /* evaluate escape codes in a string. */
2073 static void parse_escape_string(CString
*outstr
, const uint8_t *buf
, int is_long
)
2088 case '0': case '1': case '2': case '3':
2089 case '4': case '5': case '6': case '7':
2090 /* at most three octal digits */
2095 n
= n
* 8 + c
- '0';
2099 n
= n
* 8 + c
- '0';
2104 goto add_char_nonext
;
2112 if (c
>= 'a' && c
<= 'f')
2114 else if (c
>= 'A' && c
<= 'F')
2124 goto add_char_nonext
;
2148 goto invalid_escape
;
2158 if (c
>= '!' && c
<= '~')
2159 tcc_warning("unknown escape sequence: \'\\%c\'", c
);
2161 tcc_warning("unknown escape sequence: \'\\x%x\'", c
);
2168 cstr_ccat(outstr
, c
);
2170 cstr_wccat(outstr
, c
);
2172 /* add a trailing '\0' */
2174 cstr_ccat(outstr
, '\0');
2176 cstr_wccat(outstr
, '\0');
2179 void parse_string(const char *s
, int len
)
2181 uint8_t buf
[1000], *p
= buf
;
2184 if ((is_long
= *s
== 'L'))
2188 if (len
>= sizeof buf
)
2189 p
= tcc_malloc(len
+ 1);
2193 cstr_reset(&tokcstr
);
2194 parse_escape_string(&tokcstr
, p
, is_long
);
2200 /* XXX: make it portable */
2204 char_size
= sizeof(nwchar_t
);
2205 if (tokcstr
.size
<= char_size
)
2206 tcc_error("empty character constant");
2207 if (tokcstr
.size
> 2 * char_size
)
2208 tcc_warning("multi-character character constant");
2210 tokc
.i
= *(int8_t *)tokcstr
.data
;
2213 tokc
.i
= *(nwchar_t
*)tokcstr
.data
;
2217 tokc
.str
.size
= tokcstr
.size
;
2218 tokc
.str
.data
= tokcstr
.data
;
2219 tokc
.str
.data_allocated
= tokcstr
.data_allocated
;
2227 /* we use 64 bit numbers */
2230 /* bn = (bn << shift) | or_val */
2231 static void bn_lshift(unsigned int *bn
, int shift
, int or_val
)
2235 for(i
=0;i
<BN_SIZE
;i
++) {
2237 bn
[i
] = (v
<< shift
) | or_val
;
2238 or_val
= v
>> (32 - shift
);
2242 static void bn_zero(unsigned int *bn
)
2245 for(i
=0;i
<BN_SIZE
;i
++) {
2250 /* parse number in null terminated string 'p' and return it in the
2252 static void parse_number(const char *p
)
2254 int b
, t
, shift
, frac_bits
, s
, exp_val
, ch
;
2256 unsigned int bn
[BN_SIZE
];
2267 goto float_frac_parse
;
2268 } else if (t
== '0') {
2269 if (ch
== 'x' || ch
== 'X') {
2273 } else if (tcc_ext
&& (ch
== 'b' || ch
== 'B')) {
2279 /* parse all digits. cannot check octal numbers at this stage
2280 because of floating point constants */
2282 if (ch
>= 'a' && ch
<= 'f')
2284 else if (ch
>= 'A' && ch
<= 'F')
2292 if (q
>= token_buf
+ STRING_MAX_SIZE
) {
2294 tcc_error("number too long");
2300 ((ch
== 'e' || ch
== 'E') && b
== 10) ||
2301 ((ch
== 'p' || ch
== 'P') && (b
== 16 || b
== 2))) {
2303 /* NOTE: strtox should support that for hexa numbers, but
2304 non ISOC99 libcs do not support it, so we prefer to do
2306 /* hexadecimal or binary floats */
2307 /* XXX: handle overflows */
2319 } else if (t
>= 'a') {
2321 } else if (t
>= 'A') {
2326 bn_lshift(bn
, shift
, t
);
2333 if (t
>= 'a' && t
<= 'f') {
2335 } else if (t
>= 'A' && t
<= 'F') {
2337 } else if (t
>= '0' && t
<= '9') {
2343 tcc_error("invalid digit");
2344 bn_lshift(bn
, shift
, t
);
2349 if (ch
!= 'p' && ch
!= 'P')
2356 } else if (ch
== '-') {
2360 if (ch
< '0' || ch
> '9')
2361 expect("exponent digits");
2362 while (ch
>= '0' && ch
<= '9') {
2363 exp_val
= exp_val
* 10 + ch
- '0';
2366 exp_val
= exp_val
* s
;
2368 /* now we can generate the number */
2369 /* XXX: should patch directly float number */
2370 d
= (double)bn
[1] * 4294967296.0 + (double)bn
[0];
2371 d
= ldexp(d
, exp_val
- frac_bits
);
2376 /* float : should handle overflow */
2378 } else if (t
== 'L') {
2380 #ifdef TCC_TARGET_PE
2385 /* XXX: not large enough */
2386 tokc
.ld
= (long double)d
;
2393 /* decimal floats */
2395 if (q
>= token_buf
+ STRING_MAX_SIZE
)
2400 while (ch
>= '0' && ch
<= '9') {
2401 if (q
>= token_buf
+ STRING_MAX_SIZE
)
2407 if (ch
== 'e' || ch
== 'E') {
2408 if (q
>= token_buf
+ STRING_MAX_SIZE
)
2412 if (ch
== '-' || ch
== '+') {
2413 if (q
>= token_buf
+ STRING_MAX_SIZE
)
2418 if (ch
< '0' || ch
> '9')
2419 expect("exponent digits");
2420 while (ch
>= '0' && ch
<= '9') {
2421 if (q
>= token_buf
+ STRING_MAX_SIZE
)
2433 tokc
.f
= strtof(token_buf
, NULL
);
2434 } else if (t
== 'L') {
2436 #ifdef TCC_TARGET_PE
2438 tokc
.d
= strtod(token_buf
, NULL
);
2441 tokc
.ld
= strtold(token_buf
, NULL
);
2445 tokc
.d
= strtod(token_buf
, NULL
);
2449 unsigned long long n
, n1
;
2450 int lcount
, ucount
, must_64bit
;
2453 /* integer number */
2456 if (b
== 10 && *q
== '0') {
2463 /* no need for checks except for base 10 / 8 errors */
2473 tcc_error("invalid digit");
2476 /* detect overflow */
2477 /* XXX: this test is not reliable */
2479 tcc_error("integer constant overflow");
2482 /* Determine the characteristics (unsigned and/or 64bit) the type of
2483 the constant must have according to the constant suffix(es) */
2484 lcount
= ucount
= must_64bit
= 0;
2490 tcc_error("three 'l's in integer constant");
2491 if (lcount
&& *(p
- 1) != ch
)
2492 tcc_error("incorrect integer suffix: %s", p1
);
2494 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
2499 } else if (t
== 'U') {
2501 tcc_error("two 'u's in integer constant");
2509 /* Whether 64 bits are needed to hold the constant's value */
2510 if (n
& 0xffffffff00000000LL
|| must_64bit
) {
2518 /* Whether type must be unsigned to hold the constant's value */
2519 if (ucount
|| ((n1
>> 31) && (b
!= 10))) {
2520 if (tok
== TOK_CLLONG
)
2524 /* If decimal and no unsigned suffix, bump to 64 bits or throw error */
2525 } else if (n1
>> 31) {
2526 if (tok
== TOK_CINT
)
2529 tcc_error("integer constant overflow");
2532 if (tok
== TOK_CINT
|| tok
== TOK_CUINT
)
2538 tcc_error("invalid number\n");
2542 #define PARSE2(c1, tok1, c2, tok2) \
2553 /* return next token without macro substitution */
2554 static inline void next_nomacro1(void)
2556 int t
, c
, is_long
, len
;
2564 #if (__TINYC__ || __GNUC__)
2567 goto parse_ident_fast
;
2574 if (parse_flags
& PARSE_FLAG_SPACES
)
2575 goto keep_tok_flags
;
2576 while (isidnum_table
[*p
- CH_EOF
] & IS_SPC
)
2585 /* first look if it is in fact an end of buffer */
2586 c
= handle_stray1(p
);
2593 TCCState
*s1
= tcc_state
;
2594 if ((parse_flags
& PARSE_FLAG_LINEFEED
)
2595 && !(tok_flags
& TOK_FLAG_EOF
)) {
2596 tok_flags
|= TOK_FLAG_EOF
;
2598 goto keep_tok_flags
;
2599 } else if (!(parse_flags
& PARSE_FLAG_PREPROCESS
)) {
2601 } else if (s1
->ifdef_stack_ptr
!= file
->ifdef_stack_ptr
) {
2602 tcc_error("missing #endif");
2603 } else if (s1
->include_stack_ptr
== s1
->include_stack
) {
2604 /* no include left : end of file. */
2607 tok_flags
&= ~TOK_FLAG_EOF
;
2608 /* pop include file */
2610 /* test if previous '#endif' was after a #ifdef at
2612 if (tok_flags
& TOK_FLAG_ENDIF
) {
2614 printf("#endif %s\n", get_tok_str(file
->ifndef_macro_saved
, NULL
));
2616 add_cached_include(s1
, file
->filename
, file
->ifndef_macro_saved
);
2617 tok_flags
&= ~TOK_FLAG_ENDIF
;
2620 /* add end of include file debug info */
2621 if (tcc_state
->do_debug
) {
2622 put_stabd(N_EINCL
, 0, 0);
2624 /* pop include stack */
2626 s1
->include_stack_ptr
--;
2635 tok_flags
|= TOK_FLAG_BOL
;
2638 if (0 == (parse_flags
& PARSE_FLAG_LINEFEED
))
2641 goto keep_tok_flags
;
2646 if ((tok_flags
& TOK_FLAG_BOL
) &&
2647 (parse_flags
& PARSE_FLAG_PREPROCESS
)) {
2649 preprocess(tok_flags
& TOK_FLAG_BOF
);
2655 tok
= TOK_TWOSHARPS
;
2657 if (parse_flags
& PARSE_FLAG_ASM_FILE
) {
2658 p
= parse_line_comment(p
- 1,0);
2667 /* dollar is allowed to start identifiers when not parsing asm */
2669 if (!(isidnum_table
[c
- CH_EOF
] & IS_ID
)
2670 || (parse_flags
& PARSE_FLAG_ASM_FILE
))
2673 #if (__TINYC__ || __GNUC__)
2680 case 'a': case 'b': case 'c': case 'd':
2681 case 'e': case 'f': case 'g': case 'h':
2682 case 'i': case 'j': case 'k': case 'l':
2683 case 'm': case 'n': case 'o': case 'p':
2684 case 'q': case 'r': case 's': case 't':
2685 case 'u': case 'v': case 'w': case 'x':
2687 case 'A': case 'B': case 'C': case 'D':
2688 case 'E': case 'F': case 'G': case 'H':
2689 case 'I': case 'J': case 'K':
2690 case 'M': case 'N': case 'O': case 'P':
2691 case 'Q': case 'R': case 'S': case 'T':
2692 case 'U': case 'V': case 'W': case 'X':
2699 h
= TOK_HASH_FUNC(h
, c
);
2700 while (c
= *++p
, isidnum_table
[c
- CH_EOF
] & (IS_ID
|IS_NUM
))
2701 h
= TOK_HASH_FUNC(h
, c
);
2706 /* fast case : no stray found, so we have the full token
2707 and we have already hashed it */
2708 h
&= (TOK_HASH_SIZE
- 1);
2709 pts
= &hash_ident
[h
];
2714 if (ts
->len
== len
&& !memcmp(ts
->str
, p1
, len
))
2716 pts
= &(ts
->hash_next
);
2718 ts
= tok_alloc_new(pts
, (char *) p1
, len
);
2722 cstr_reset(&tokcstr
);
2723 cstr_cat(&tokcstr
, p1
, len
);
2727 while (isidnum_table
[c
- CH_EOF
] & (IS_ID
|IS_NUM
))
2729 cstr_ccat(&tokcstr
, c
);
2732 ts
= tok_alloc(tokcstr
.data
, tokcstr
.size
);
2738 if (t
!= '\\' && t
!= '\'' && t
!= '\"') {
2740 goto parse_ident_fast
;
2743 if (c
== '\'' || c
== '\"') {
2747 cstr_reset(&tokcstr
);
2748 cstr_ccat(&tokcstr
, 'L');
2749 goto parse_ident_slow
;
2754 case '0': case '1': case '2': case '3':
2755 case '4': case '5': case '6': case '7':
2757 cstr_reset(&tokcstr
);
2758 /* after the first digit, accept digits, alpha, '.' or sign if
2759 prefixed by 'eEpP' */
2763 cstr_ccat(&tokcstr
, c
);
2765 if (!((isidnum_table
[c
- CH_EOF
] & (IS_ID
|IS_NUM
))
2767 || ((c
== '+' || c
== '-')
2768 && (t
== 'e' || t
== 'E' || t
== 'p' || t
== 'P')
2772 /* We add a trailing '\0' to ease parsing */
2773 cstr_ccat(&tokcstr
, '\0');
2774 tokc
.str
.size
= tokcstr
.size
;
2775 tokc
.str
.data
= tokcstr
.data
;
2776 tokc
.str
.data_allocated
= tokcstr
.data_allocated
;
2781 /* special dot handling because it can also start a number */
2784 cstr_reset(&tokcstr
);
2785 cstr_ccat(&tokcstr
, '.');
2787 } else if ((parse_flags
& PARSE_FLAG_ASM_FILE
)
2788 && (isidnum_table
[c
- CH_EOF
] & (IS_ID
|IS_NUM
))) {
2790 goto parse_ident_fast
;
2791 } else if (c
== '.') {
2797 *--p
= '.'; /* may underflow into file->unget[] */
2808 cstr_reset(&tokcstr
);
2810 cstr_ccat(&tokcstr
, 'L');
2811 cstr_ccat(&tokcstr
, c
);
2812 p
= parse_pp_string(p
, c
, &tokcstr
);
2813 cstr_ccat(&tokcstr
, c
);
2814 cstr_ccat(&tokcstr
, '\0');
2815 tokc
.str
.size
= tokcstr
.size
;
2816 tokc
.str
.data
= tokcstr
.data
;
2817 tokc
.str
.data_allocated
= tokcstr
.data_allocated
;
2826 } else if (c
== '<') {
2843 } else if (c
== '>') {
2861 } else if (c
== '=') {
2874 } else if (c
== '=') {
2887 } else if (c
== '=') {
2900 } else if (c
== '=') {
2903 } else if (c
== '>') {
2911 PARSE2('!', '!', '=', TOK_NE
)
2912 PARSE2('=', '=', '=', TOK_EQ
)
2913 PARSE2('*', '*', '=', TOK_A_MUL
)
2914 PARSE2('%', '%', '=', TOK_A_MOD
)
2915 PARSE2('^', '^', '=', TOK_A_XOR
)
2917 /* comments or operator */
2921 p
= parse_comment(p
,0);
2922 /* comments replaced by a blank */
2924 if (tcc_state
->option_C
)
2926 goto keep_tok_flags
;
2927 } else if (c
== '/') {
2928 p
= parse_line_comment(p
,0);
2930 if (tcc_state
->option_C
)
2932 goto keep_tok_flags
;
2933 } else if (c
== '=') {
2953 case '@': /* only used in assembler */
2959 if (parse_flags
& PARSE_FLAG_ASM_FILE
)
2961 tcc_error("unrecognized character \\x%02x", c
);
2967 #if defined(PARSE_DEBUG)
2968 printf("token = %s\n", get_tok_str(tok
, &tokc
));
2972 /* return next token without macro substitution. Can read input from
2974 static void next_nomacro_spc(void)
2980 TOK_GET(&tok
, ¯o_ptr
, &tokc
);
2981 if (tok
== TOK_LINENUM
) {
2982 file
->line_num
= tokc
.i
;
2991 ST_FUNC
void next_nomacro(void)
2995 } while (tok
< 256 && (isidnum_table
[tok
- CH_EOF
] & IS_SPC
));
2999 static void macro_subst(
3000 TokenString
*tok_str
,
3002 const int *macro_str
,
3006 /* substitute arguments in replacement lists in macro_str by the values in
3007 args (field d) and return allocated string */
3008 static int *macro_arg_subst(Sym
**nested_list
, const int *macro_str
, Sym
*args
)
3020 TOK_GET(&t
, ¯o_str
, &cval
);
3025 TOK_GET(&t
, ¯o_str
, &cval
);
3028 s
= sym_find2(args
, t
);
3031 cstr_ccat(&cstr
, '\"');
3035 TOK_GET(&t
, &st
, &cval
);
3036 if (t
!= TOK_PLCHLDR
3038 && 0 == check_space(t
, &spc
)) {
3039 const char *s
= get_tok_str(t
, &cval
);
3041 if (t
== TOK_PPSTR
&& *s
!= '\'')
3042 add_char(&cstr
, *s
);
3044 cstr_ccat(&cstr
, *s
);
3050 cstr_ccat(&cstr
, '\"');
3051 cstr_ccat(&cstr
, '\0');
3053 printf("\nstringize: <%s>\n", (char *)cstr
.data
);
3056 cval
.str
.size
= cstr
.size
;
3057 cval
.str
.data
= cstr
.data
;
3058 cval
.str
.data_allocated
= cstr
.data_allocated
;
3059 tok_str_add2(&str
, TOK_PPSTR
, &cval
);
3063 expect("macro parameter after '#'");
3065 } else if (t
>= TOK_IDENT
) {
3066 s
= sym_find2(args
, t
);
3070 /* if '##' is present before or after, no arg substitution */
3071 if (*macro_str
== TOK_TWOSHARPS
|| t1
== TOK_TWOSHARPS
) {
3072 /* special case for var arg macros : ## eats the ','
3073 if empty VA_ARGS variable. */
3074 if (t1
== TOK_TWOSHARPS
&& t0
== ',' && gnu_ext
&& s
->type
.t
) {
3076 /* suppress ',' '##' */
3079 /* suppress '##' and add variable */
3086 TOK_GET(&t1
, &st
, &cval
);
3089 tok_str_add2(&str
, t1
, &cval
);
3095 /* NOTE: the stream cannot be read when macro
3096 substituing an argument */
3097 macro_subst(&str
, nested_list
, st
, 0);
3099 if (str
.len
== l0
) /* exanded to empty string */
3100 tok_str_add(&str
, TOK_PLCHLDR
);
3102 tok_str_add(&str
, t
);
3105 tok_str_add2(&str
, t
, &cval
);
3109 tok_str_add(&str
, 0);
3113 static char const ab_month_name
[12][4] =
3115 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
3116 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
3119 /* peek or read [ws_str == NULL] next token from function macro call,
3120 walking up macro levels up to the file if necessary */
3121 static int next_argstream(Sym
**nested_list
, int can_read_stream
, TokenString
*ws_str
)
3129 p
= macro_ptr
, t
= *p
;
3131 while (is_space(t
) || TOK_LINEFEED
== t
)
3132 tok_str_add(ws_str
, t
), t
= *++p
;
3134 if (t
== 0 && can_read_stream
) {
3136 /* also, end of scope for nested defined symbol */
3138 while (sa
&& sa
->v
== -1)
3147 while (is_space(ch
) || ch
== '\n' || ch
== '/') {
3150 uint8_t *p
= file
->buf_ptr
;
3153 p
= parse_comment(p
,0);
3154 file
->buf_ptr
= p
- 1;
3155 } else if (c
== '/') {
3156 p
= parse_line_comment(p
,0);
3157 file
->buf_ptr
= p
- 1;
3162 tok_str_add(ws_str
, ch
);
3176 /* do macro substitution of current token with macro 's' and add
3177 result to (tok_str,tok_len). 'nested_list' is the list of all
3178 macros we got inside to avoid recursing. Return non zero if no
3179 substitution needs to be done */
3180 static int macro_subst_tok(
3181 TokenString
*tok_str
,
3184 int can_read_stream
)
3186 Sym
*args
, *sa
, *sa1
;
3187 int parlevel
, *mstr
, t
, t1
, spc
;
3194 /* if symbol is a macro, prepare substitution */
3195 /* special macros */
3196 if (tok
== TOK___LINE__
) {
3197 snprintf(buf
, sizeof(buf
), "%d", file
->line_num
);
3201 } else if (tok
== TOK___FILE__
) {
3202 cstrval
= file
->filename
;
3204 } else if (tok
== TOK___DATE__
|| tok
== TOK___TIME__
) {
3209 tm
= localtime(&ti
);
3210 if (tok
== TOK___DATE__
) {
3211 snprintf(buf
, sizeof(buf
), "%s %2d %d",
3212 ab_month_name
[tm
->tm_mon
], tm
->tm_mday
, tm
->tm_year
+ 1900);
3214 snprintf(buf
, sizeof(buf
), "%02d:%02d:%02d",
3215 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
3222 cstr_cat(&cstr
, cstrval
, 0);
3223 cval
.str
.size
= cstr
.size
;
3224 cval
.str
.data
= cstr
.data
;
3225 cval
.str
.data_allocated
= cstr
.data_allocated
;
3226 tok_str_add2(tok_str
, t1
, &cval
);
3229 int saved_parse_flags
= parse_flags
;
3232 if (s
->type
.t
== MACRO_FUNC
) {
3233 /* whitespace between macro name and argument list */
3235 tok_str_new(&ws_str
);
3238 parse_flags
|= PARSE_FLAG_SPACES
| PARSE_FLAG_LINEFEED
3239 | PARSE_FLAG_ACCEPT_STRAYS
;
3241 /* get next token from argument stream */
3242 t
= next_argstream(nested_list
, can_read_stream
, &ws_str
);
3244 /* not a macro substitution after all, restore the
3245 * macro token plus all whitespace we've read.
3246 * whitespace is intentionally not merged to preserve
3248 parse_flags
= saved_parse_flags
;
3249 tok_str_add(tok_str
, tok
);
3250 if (parse_flags
& PARSE_FLAG_SPACES
) {
3252 for (i
= 0; i
< ws_str
.len
; i
++)
3253 tok_str_add(tok_str
, ws_str
.str
[i
]);
3255 tok_str_free(ws_str
.str
);
3258 tok_str_free(ws_str
.str
);
3260 next_nomacro(); /* eat '(' */
3262 /* argument macro */
3265 /* NOTE: empty args are allowed, except if no args */
3268 next_argstream(nested_list
, can_read_stream
, NULL
);
3269 } while (is_space(tok
) || TOK_LINEFEED
== tok
);
3271 /* handle '()' case */
3272 if (!args
&& !sa
&& tok
== ')')
3275 tcc_error("macro '%s' used with too many args",
3276 get_tok_str(s
->v
, 0));
3279 /* NOTE: non zero sa->t indicates VA_ARGS */
3280 while ((parlevel
> 0 ||
3282 (tok
!= ',' || sa
->type
.t
)))) {
3283 if (tok
== TOK_EOF
|| tok
== 0)
3287 else if (tok
== ')')
3289 if (tok
== TOK_LINEFEED
)
3291 if (!check_space(tok
, &spc
))
3292 tok_str_add2(&str
, tok
, &tokc
);
3293 next_argstream(nested_list
, can_read_stream
, NULL
);
3298 tok_str_add(&str
, 0);
3299 sa1
= sym_push2(&args
, sa
->v
& ~SYM_FIELD
, sa
->type
.t
, 0);
3303 /* special case for gcc var args: add an empty
3304 var arg argument if it is omitted */
3305 if (sa
&& sa
->type
.t
&& gnu_ext
)
3313 tcc_error("macro '%s' used with too few args",
3314 get_tok_str(s
->v
, 0));
3317 parse_flags
= saved_parse_flags
;
3319 /* now subst each arg */
3320 mstr
= macro_arg_subst(nested_list
, mstr
, args
);
3325 tok_str_free(sa
->d
);
3331 sym_push2(nested_list
, s
->v
, 0, 0);
3332 parse_flags
= saved_parse_flags
;
3333 macro_subst(tok_str
, nested_list
, mstr
, can_read_stream
);
3335 /* pop nested defined symbol */
3337 *nested_list
= sa1
->prev
;
3345 int paste_tokens(int t1
, CValue
*v1
, int t2
, CValue
*v2
)
3351 if (t1
!= TOK_PLCHLDR
)
3352 cstr_cat(&cstr
, get_tok_str(t1
, v1
), -1);
3354 if (t2
!= TOK_PLCHLDR
)
3355 cstr_cat(&cstr
, get_tok_str(t2
, v2
), -1);
3356 cstr_ccat(&cstr
, '\0');
3358 tcc_open_bf(tcc_state
, ":paste:", cstr
.size
);
3359 memcpy(file
->buffer
, cstr
.data
, cstr
.size
);
3362 if (0 == *file
->buf_ptr
)
3366 tcc_warning("pasting <%.*s> and <%s> does not give a valid preprocessing token",
3367 n
, cstr
.data
, (char*)cstr
.data
+ n
);
3372 //printf("paste <%s>\n", (char*)cstr.data);
3377 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
3378 return the resulting string (which must be freed). */
3379 static inline int *macro_twosharps(const int *ptr0
)
3383 TokenString macro_str1
;
3384 int start_of_nosubsts
= -1;
3387 /* we search the first '##' */
3388 for (ptr
= ptr0
;;) {
3389 TOK_GET(&t
, &ptr
, &cval
);
3390 if (t
== TOK_TWOSHARPS
)
3396 tok_str_new(¯o_str1
);
3398 //tok_print(" $$$", ptr0);
3399 for (ptr
= ptr0
;;) {
3400 TOK_GET(&t
, &ptr
, &cval
);
3403 if (t
== TOK_TWOSHARPS
)
3405 while (*ptr
== TOK_TWOSHARPS
) {
3407 /* given 'a##b', remove nosubsts preceding 'a' */
3408 if (start_of_nosubsts
>= 0)
3409 macro_str1
.len
= start_of_nosubsts
;
3410 /* given 'a##b', remove nosubsts preceding 'b' */
3411 while ((t1
= *++ptr
) == TOK_NOSUBST
)
3413 if (t1
&& t1
!= TOK_TWOSHARPS
3414 && t1
!= ':') /* 'a##:' don't build a new token */
3416 TOK_GET(&t1
, &ptr
, &cv1
);
3417 if (t
!= TOK_PLCHLDR
|| t1
!= TOK_PLCHLDR
) {
3418 paste_tokens(t
, &cval
, t1
, &cv1
);
3419 t
= tok
, cval
= tokc
;
3423 if (t
== TOK_NOSUBST
) {
3424 if (start_of_nosubsts
< 0)
3425 start_of_nosubsts
= macro_str1
.len
;
3427 start_of_nosubsts
= -1;
3429 tok_str_add2(¯o_str1
, t
, &cval
);
3431 tok_str_add(¯o_str1
, 0);
3432 //tok_print(" ###", macro_str1.str);
3433 return macro_str1
.str
;
3436 /* do macro substitution of macro_str and add result to
3437 (tok_str,tok_len). 'nested_list' is the list of all macros we got
3438 inside to avoid recursing. */
3439 static void macro_subst(
3440 TokenString
*tok_str
,
3442 const int *macro_str
,
3448 int t
, spc
, nosubst
;
3450 int *macro_str1
= NULL
;
3452 /* first scan for '##' operator handling */
3456 /* first scan for '##' operator handling */
3457 if (can_read_stream
) {
3458 macro_str1
= macro_twosharps(ptr
);
3464 TOK_GET(&t
, &ptr
, &cval
);
3468 if (t
>= TOK_IDENT
&& 0 == nosubst
) {
3473 /* if nested substitution, do nothing */
3474 if (sym_find2(*nested_list
, t
)) {
3475 /* and mark it as TOK_NOSUBST, so it doesn't get subst'd again */
3476 tok_str_add2(tok_str
, TOK_NOSUBST
, NULL
);
3482 str
.str
= (int*)ptr
;
3483 begin_macro(&str
, 2);
3486 macro_subst_tok(tok_str
, nested_list
, s
, can_read_stream
);
3488 if (str
.alloc
== 3) {
3489 /* already finished by reading function macro arguments */
3497 spc
= (tok_str
->len
&&
3498 is_space(tok_last(tok_str
->str
,
3499 tok_str
->str
+ tok_str
->len
)));
3503 if (t
== '\\' && !(parse_flags
& PARSE_FLAG_ACCEPT_STRAYS
))
3504 tcc_error("stray '\\' in program");
3507 if (!check_space(t
, &spc
))
3508 tok_str_add2(tok_str
, t
, &cval
);
3510 if (t
== TOK_NOSUBST
)
3515 tok_str_free(macro_str1
);
3519 /* return next token with macro substitution */
3520 ST_FUNC
void next(void)
3523 if (parse_flags
& PARSE_FLAG_SPACES
)
3529 if (tok
== TOK_NOSUBST
|| tok
== TOK_PLCHLDR
) {
3530 /* discard preprocessor markers */
3532 } else if (tok
== 0) {
3533 /* end of macro or unget token string */
3537 } else if (tok
>= TOK_IDENT
&& (parse_flags
& PARSE_FLAG_PREPROCESS
)) {
3539 /* if reading from file, try to substitute macros */
3540 s
= define_find(tok
);
3542 Sym
*nested_list
= NULL
;
3545 macro_subst_tok(&tokstr_buf
, &nested_list
, s
, 1);
3546 tok_str_add(&tokstr_buf
, 0);
3547 begin_macro(&tokstr_buf
, 2);
3551 /* convert preprocessor tokens into C tokens */
3552 if (tok
== TOK_PPNUM
) {
3553 if (parse_flags
& PARSE_FLAG_TOK_NUM
)
3554 parse_number((char *)tokc
.str
.data
);
3555 } else if (tok
== TOK_PPSTR
) {
3556 if (parse_flags
& PARSE_FLAG_TOK_STR
)
3557 parse_string((char *)tokc
.str
.data
, tokc
.str
.size
- 1);
3561 /* push back current token and set current token to 'last_tok'. Only
3562 identifier case handled for labels. */
3563 ST_INLN
void unget_tok(int last_tok
)
3565 TokenString
*str
= tcc_malloc(sizeof *str
);
3567 tok_str_add2(str
, tok
, &tokc
);
3568 tok_str_add(str
, 0);
3569 begin_macro(str
, 1);
3573 /* better than nothing, but needs extension to handle '-E' option
3575 ST_FUNC
void preprocess_init(TCCState
*s1
)
3577 s1
->include_stack_ptr
= s1
->include_stack
;
3578 /* XXX: move that before to avoid having to initialize
3579 file->ifdef_stack_ptr ? */
3580 s1
->ifdef_stack_ptr
= s1
->ifdef_stack
;
3581 file
->ifdef_stack_ptr
= s1
->ifdef_stack_ptr
;
3583 pvtop
= vtop
= vstack
- 1;
3584 s1
->pack_stack
[0] = 0;
3585 s1
->pack_stack_ptr
= s1
->pack_stack
;
3587 isidnum_table
['$' - CH_EOF
] =
3588 tcc_state
->dollars_in_identifiers
? IS_ID
: 0;
3590 isidnum_table
['.' - CH_EOF
] =
3591 (parse_flags
& PARSE_FLAG_ASM_FILE
) ? IS_ID
: 0;
3594 ST_FUNC
void preprocess_new(void)
3599 /* init isid table */
3600 for(i
= CH_EOF
; i
<128; i
++)
3601 isidnum_table
[i
- CH_EOF
]
3602 = is_space(i
) ? IS_SPC
3607 for(i
= 128; i
<256; i
++)
3608 isidnum_table
[i
- CH_EOF
] = IS_ID
;
3610 /* init allocators */
3611 tal_new(&toksym_alloc
, TOKSYM_TAL_LIMIT
, TOKSYM_TAL_SIZE
);
3612 tal_new(&tokstr_alloc
, TOKSTR_TAL_LIMIT
, TOKSTR_TAL_SIZE
);
3613 tal_new(&cstr_alloc
, CSTR_TAL_LIMIT
, CSTR_TAL_SIZE
);
3615 memset(hash_ident
, 0, TOK_HASH_SIZE
* sizeof(TokenSym
*));
3616 cstr_new(&cstr_buf
);
3617 cstr_realloc(&cstr_buf
, STRING_MAX_SIZE
);
3618 tok_str_new(&tokstr_buf
);
3619 tok_str_realloc(&tokstr_buf
, TOKSTR_MAX_SIZE
);
3621 tok_ident
= TOK_IDENT
;
3630 tok_alloc(p
, r
- p
- 1);
3635 ST_FUNC
void preprocess_delete(void)
3639 /* free -D and compiler defines */
3642 /* cleanup from error/setjmp */
3648 n
= tok_ident
- TOK_IDENT
;
3649 for(i
= 0; i
< n
; i
++)
3650 tal_free(toksym_alloc
, table_ident
[i
]);
3651 tcc_free(table_ident
);
3654 /* free static buffers */
3655 cstr_free(&tokcstr
);
3656 cstr_free(&cstr_buf
);
3657 tok_str_free(tokstr_buf
.str
);
3659 /* free allocators */
3660 tal_delete(toksym_alloc
);
3661 toksym_alloc
= NULL
;
3662 tal_delete(tokstr_alloc
);
3663 tokstr_alloc
= NULL
;
3664 tal_delete(cstr_alloc
);
3668 /* Preprocess the current file */
3669 ST_FUNC
int tcc_preprocess(TCCState
*s1
)
3671 BufferedFile
**iptr
;
3672 int token_seen
, spcs
, level
;
3674 preprocess_init(s1
);
3675 ch
= file
->buf_ptr
[0];
3676 tok_flags
= TOK_FLAG_BOL
| TOK_FLAG_BOF
;
3677 parse_flags
= PARSE_FLAG_PREPROCESS
3678 | (parse_flags
& PARSE_FLAG_ASM_FILE
)
3679 | PARSE_FLAG_LINEFEED
3681 | PARSE_FLAG_ACCEPT_STRAYS
3685 do next(); while (tok
!= TOK_EOF
); return 0;
3688 token_seen
= spcs
= 0;
3689 pp_line(s1
, file
, 0);
3692 iptr
= s1
->include_stack_ptr
;
3696 level
= s1
->include_stack_ptr
- iptr
;
3699 pp_line(s1
, *iptr
, 0);
3700 pp_line(s1
, file
, level
);
3703 if (0 == token_seen
) {
3708 if (tok
== TOK_LINEFEED
) {
3712 pp_line(s1
, file
, 0);
3713 while (s1
->ppfp
&& spcs
> 0)
3714 fputs(" ", s1
->ppfp
), --spcs
;
3718 } else if (tok
== TOK_LINEFEED
) {
3723 fputs(get_tok_str(tok
, &tokc
), s1
->ppfp
);
3729 ST_FUNC
char *trimfront(char *p
)
3731 while (*p
&& (unsigned char)*p
<= ' ')
3736 ST_FUNC
char *trimback(char *a
, char *e
)
3738 while (e
> a
&& (unsigned char)e
[-1] <= ' ')