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 static int pp_debug_tok
, pp_debug_symv
;
54 static void tok_print(const char *msg
, const int *str
);
56 /* isidnum_table flags: */
61 static TokenString
*macro_stack
;
63 static const char tcc_keywords
[] =
64 #define DEF(id, str) str "\0"
69 /* WARNING: the content of this string encodes token numbers */
70 static const unsigned char tok_two_chars
[] =
72 "<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253"
73 "-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\313..\250##\266";
94 '.','.', 0xa8, // C++ token ?
95 '#','#', TOK_TWOSHARPS
,
99 static void next_nomacro_spc(void);
101 ST_FUNC
void skip(int c
)
104 tcc_error("'%c' expected (got \"%s\")", c
, get_tok_str(tok
, &tokc
));
108 ST_FUNC
void expect(const char *msg
)
110 tcc_error("%s expected", msg
);
113 ST_FUNC
void begin_macro(TokenString
*str
, int alloc
)
116 str
->prev
= macro_stack
;
117 str
->prev_ptr
= macro_ptr
;
118 macro_ptr
= str
->str
;
122 ST_FUNC
void end_macro(void)
124 TokenString
*str
= macro_stack
;
125 macro_stack
= str
->prev
;
126 macro_ptr
= str
->prev_ptr
;
127 if (str
->alloc
== 2) {
128 str
->alloc
= 3; /* just mark as finished */
130 tok_str_free(str
->str
);
136 ST_FUNC
char *trimfront(char *p
)
138 while (*p
&& (unsigned char)*p
<= ' ')
143 ST_FUNC
char *trimback(char *a
, char *e
)
145 while (e
> a
&& (unsigned char)e
[-1] <= ' ')
151 /* ------------------------------------------------------------------------- */
152 /* Custom allocator for tiny objects */
154 ST_FUNC TinyAlloc
*tal_new(TinyAlloc
**pal
, size_t limit
, size_t size
)
156 TinyAlloc
*al
= tcc_mallocz(sizeof(TinyAlloc
));
157 al
->p
= al
->buffer
= tcc_malloc(size
);
164 ST_FUNC
void tal_delete(TinyAlloc
*al
)
172 fprintf(stderr
, "limit=%5d, size=%5g MB, nb_peak=%6d, nb_total=%8d, nb_missed=%6d, usage=%5.1f%%\n",
173 al
->limit
, al
->size
/ 1024.0 / 1024.0, al
->nb_peak
, al
->nb_total
, al
->nb_missed
,
174 (al
->peak_p
- al
->buffer
) * 100.0 / al
->size
);
177 if (al
->nb_allocs
> 0) {
178 fprintf(stderr
, "TAL_DEBUG: mem leak %d chunks (limit= %d)\n",
179 al
->nb_allocs
, al
->limit
);
180 uint8_t *p
= al
->buffer
;
182 tal_header_t
*header
= (tal_header_t
*)p
;
183 if (header
->line_num
> 0) {
184 fprintf(stderr
, " file %s, line %u: %u bytes\n",
185 header
->file_name
, header
->line_num
, header
->size
);
187 p
+= header
->size
+ sizeof(tal_header_t
);
192 tcc_free(al
->buffer
);
198 ST_FUNC
void tal_free_impl(TinyAlloc
*al
, void *p TAL_DEBUG_PARAMS
)
203 if (al
->buffer
<= (uint8_t *)p
&& (uint8_t *)p
< al
->buffer
+ al
->size
) {
205 tal_header_t
*header
= (((tal_header_t
*)p
) - 1);
206 if (header
->line_num
< 0) {
207 fprintf(stderr
, "TAL_DEBUG: file %s, line %u double frees chunk from\n",
209 fprintf(stderr
, " file %s, line %u: %u bytes\n",
210 header
->file_name
, -header
->line_num
, header
->size
);
212 header
->line_num
= -header
->line_num
;
217 } else if (al
->next
) {
225 ST_FUNC
void *tal_realloc_impl(TinyAlloc
**pal
, void *p
, size_t size TAL_DEBUG_PARAMS
)
227 tal_header_t
*header
;
230 size_t adj_size
= (size
+ 3) & -4;
231 TinyAlloc
*al
= *pal
;
234 is_own
= (al
->buffer
<= (uint8_t *)p
&& (uint8_t *)p
< al
->buffer
+ al
->size
);
235 if ((!p
|| is_own
) && size
<= al
->limit
) {
236 if (al
->p
+ adj_size
+ sizeof(tal_header_t
) < al
->buffer
+ al
->size
) {
237 header
= (tal_header_t
*)al
->p
;
238 header
->size
= adj_size
;
240 int ofs
= strlen(file
) - TAL_DEBUG_FILE_LEN
;
241 strncpy(header
->file_name
, file
+ (ofs
> 0 ? ofs
: 0), TAL_DEBUG_FILE_LEN
);
242 header
->file_name
[TAL_DEBUG_FILE_LEN
] = 0;
243 header
->line_num
= line
;
245 ret
= al
->p
+ sizeof(tal_header_t
);
246 al
->p
+= adj_size
+ sizeof(tal_header_t
);
248 header
= (((tal_header_t
*)p
) - 1);
249 memcpy(ret
, p
, header
->size
);
251 header
->line_num
= -header
->line_num
;
257 if (al
->nb_peak
< al
->nb_allocs
)
258 al
->nb_peak
= al
->nb_allocs
;
259 if (al
->peak_p
< al
->p
)
266 ret
= tal_realloc(*pal
, 0, size
);
267 header
= (((tal_header_t
*)p
) - 1);
268 memcpy(ret
, p
, header
->size
);
270 header
->line_num
= -header
->line_num
;
277 TinyAlloc
*bottom
= al
, *next
= al
->top
? al
->top
: al
;
279 al
= tal_new(pal
, next
->limit
, next
->size
* 2);
287 ret
= tcc_malloc(size
);
288 header
= (((tal_header_t
*)p
) - 1);
289 memcpy(ret
, p
, header
->size
);
291 header
->line_num
= -header
->line_num
;
293 } else if (al
->next
) {
297 ret
= tcc_realloc(p
, size
);
304 /* ------------------------------------------------------------------------- */
305 /* CString handling */
306 static void cstr_realloc(CString
*cstr
, int new_size
)
311 size
= cstr
->size_allocated
;
313 size
= 8; /* no need to allocate a too small first string */
314 while (size
< new_size
)
316 data
= tal_realloc(cstr_alloc
, cstr
->data_allocated
, size
);
317 cstr
->data_allocated
= data
;
318 cstr
->size_allocated
= size
;
323 ST_FUNC
void cstr_ccat(CString
*cstr
, int ch
)
326 size
= cstr
->size
+ 1;
327 if (size
> cstr
->size_allocated
)
328 cstr_realloc(cstr
, size
);
329 ((unsigned char *)cstr
->data
)[size
- 1] = ch
;
333 ST_FUNC
void cstr_cat(CString
*cstr
, const char *str
, int len
)
337 len
= strlen(str
) + 1 + len
;
338 size
= cstr
->size
+ len
;
339 if (size
> cstr
->size_allocated
)
340 cstr_realloc(cstr
, size
);
341 memmove(((unsigned char *)cstr
->data
) + cstr
->size
, str
, len
);
345 /* add a wide char */
346 ST_FUNC
void cstr_wccat(CString
*cstr
, int ch
)
349 size
= cstr
->size
+ sizeof(nwchar_t
);
350 if (size
> cstr
->size_allocated
)
351 cstr_realloc(cstr
, size
);
352 *(nwchar_t
*)(((unsigned char *)cstr
->data
) + size
- sizeof(nwchar_t
)) = ch
;
356 ST_FUNC
void cstr_new(CString
*cstr
)
358 memset(cstr
, 0, sizeof(CString
));
361 /* free string and reset it to NULL */
362 ST_FUNC
void cstr_free(CString
*cstr
)
364 tal_free(cstr_alloc
, cstr
->data_allocated
);
368 /* reset string to empty */
369 ST_FUNC
void cstr_reset(CString
*cstr
)
375 static void add_char(CString
*cstr
, int c
)
377 if (c
== '\'' || c
== '\"' || c
== '\\') {
378 /* XXX: could be more precise if char or string */
379 cstr_ccat(cstr
, '\\');
381 if (c
>= 32 && c
<= 126) {
384 cstr_ccat(cstr
, '\\');
386 cstr_ccat(cstr
, 'n');
388 cstr_ccat(cstr
, '0' + ((c
>> 6) & 7));
389 cstr_ccat(cstr
, '0' + ((c
>> 3) & 7));
390 cstr_ccat(cstr
, '0' + (c
& 7));
395 /* ------------------------------------------------------------------------- */
396 /* allocate a new token */
397 static TokenSym
*tok_alloc_new(TokenSym
**pts
, const char *str
, int len
)
399 TokenSym
*ts
, **ptable
;
402 if (tok_ident
>= SYM_FIRST_ANOM
)
403 tcc_error("memory full (symbols)");
405 /* expand token table if needed */
406 i
= tok_ident
- TOK_IDENT
;
407 if ((i
% TOK_ALLOC_INCR
) == 0) {
408 ptable
= tcc_realloc(table_ident
, (i
+ TOK_ALLOC_INCR
) * sizeof(TokenSym
*));
409 table_ident
= ptable
;
412 ts
= tal_realloc(toksym_alloc
, 0, sizeof(TokenSym
) + len
);
414 ts
->tok
= tok_ident
++;
415 ts
->sym_define
= NULL
;
416 ts
->sym_label
= NULL
;
417 ts
->sym_struct
= NULL
;
418 ts
->sym_identifier
= NULL
;
420 ts
->hash_next
= NULL
;
421 memcpy(ts
->str
, str
, len
);
427 #define TOK_HASH_INIT 1
428 #define TOK_HASH_FUNC(h, c) ((h) + ((h) << 5) + ((h) >> 27) + (c))
431 /* find a token and add it if not found */
432 ST_FUNC TokenSym
*tok_alloc(const char *str
, int len
)
440 h
= TOK_HASH_FUNC(h
, ((unsigned char *)str
)[i
]);
441 h
&= (TOK_HASH_SIZE
- 1);
443 pts
= &hash_ident
[h
];
448 if (ts
->len
== len
&& !memcmp(ts
->str
, str
, len
))
450 pts
= &(ts
->hash_next
);
452 return tok_alloc_new(pts
, str
, len
);
455 /* XXX: buffer overflow */
456 /* XXX: float tokens */
457 ST_FUNC
const char *get_tok_str(int v
, CValue
*cv
)
462 cstr_reset(&cstr_buf
);
470 /* XXX: not quite exact, but only useful for testing */
472 sprintf(p
, "%u", (unsigned)cv
->i
);
474 sprintf(p
, "%llu", (unsigned long long)cv
->i
);
478 cstr_ccat(&cstr_buf
, 'L');
480 cstr_ccat(&cstr_buf
, '\'');
481 add_char(&cstr_buf
, cv
->i
);
482 cstr_ccat(&cstr_buf
, '\'');
483 cstr_ccat(&cstr_buf
, '\0');
487 return (char*)cv
->str
.data
;
489 cstr_ccat(&cstr_buf
, 'L');
491 cstr_ccat(&cstr_buf
, '\"');
493 len
= cv
->str
.size
- 1;
495 add_char(&cstr_buf
, ((unsigned char *)cv
->str
.data
)[i
]);
497 len
= (cv
->str
.size
/ sizeof(nwchar_t
)) - 1;
499 add_char(&cstr_buf
, ((nwchar_t
*)cv
->str
.data
)[i
]);
501 cstr_ccat(&cstr_buf
, '\"');
502 cstr_ccat(&cstr_buf
, '\0');
506 cstr_cat(&cstr_buf
, "<float>", 0);
509 cstr_cat(&cstr_buf
, "<double>", 0);
512 cstr_cat(&cstr_buf
, "<long double>", 0);
515 cstr_cat(&cstr_buf
, "<linenumber>", 0);
518 /* above tokens have value, the ones below don't */
527 return strcpy(p
, "...");
529 return strcpy(p
, "<<=");
531 return strcpy(p
, ">>=");
534 /* search in two bytes table */
535 const unsigned char *q
= tok_two_chars
;
541 return cstr_buf
.data
;
546 sprintf(cstr_buf
.data
, "<%02x>", v
);
547 return cstr_buf
.data
;
552 } else if (v
< tok_ident
) {
553 return table_ident
[v
- TOK_IDENT
]->str
;
554 } else if (v
>= SYM_FIRST_ANOM
) {
555 /* special name for anonymous symbol */
556 sprintf(p
, "L.%u", v
- SYM_FIRST_ANOM
);
558 /* should never happen */
563 return cstr_buf
.data
;
566 /* return the current character, handling end of block if necessary
568 ST_FUNC
int handle_eob(void)
570 BufferedFile
*bf
= file
;
573 /* only tries to read if really end of buffer */
574 if (bf
->buf_ptr
>= bf
->buf_end
) {
576 #if defined(PARSE_DEBUG)
581 len
= read(bf
->fd
, bf
->buffer
, len
);
588 bf
->buf_ptr
= bf
->buffer
;
589 bf
->buf_end
= bf
->buffer
+ len
;
590 *bf
->buf_end
= CH_EOB
;
592 if (bf
->buf_ptr
< bf
->buf_end
) {
593 return bf
->buf_ptr
[0];
595 bf
->buf_ptr
= bf
->buf_end
;
600 /* read next char from current input file and handle end of input buffer */
601 ST_INLN
void inp(void)
603 ch
= *(++(file
->buf_ptr
));
604 /* end of buffer/file handling */
609 /* handle '\[\r]\n' */
610 static int handle_stray_noerror(void)
617 } else if (ch
== '\r') {
631 static void handle_stray(void)
633 if (handle_stray_noerror())
634 tcc_error("stray '\\' in program");
637 /* skip the stray and handle the \\n case. Output an error if
638 incorrect char after the stray */
639 static int handle_stray1(uint8_t *p
)
644 if (p
>= file
->buf_end
) {
651 if (handle_stray_noerror()) {
652 if (!(parse_flags
& PARSE_FLAG_ACCEPT_STRAYS
))
653 tcc_error("stray '\\' in program");
654 *--file
->buf_ptr
= '\\';
661 /* handle just the EOB case, but not stray */
662 #define PEEKC_EOB(c, p)\
673 /* handle the complicated stray case */
679 c = handle_stray1(p);\
684 /* input with '\[\r]\n' handling. Note that this function cannot
685 handle other characters after '\', so you cannot call it inside
686 strings or comments */
687 ST_FUNC
void minp(void)
694 /* single line C++ comments */
695 static uint8_t *parse_line_comment(uint8_t *p
)
703 if (c
== '\n' || c
== CH_EOF
) {
705 } else if (c
== '\\') {
714 } else if (c
== '\r') {
732 ST_FUNC
uint8_t *parse_comment(uint8_t *p
)
741 if (c
== '\n' || c
== '*' || c
== '\\')
745 if (c
== '\n' || c
== '*' || c
== '\\')
749 /* now we can handle all the cases */
753 } else if (c
== '*') {
759 } else if (c
== '/') {
761 } else if (c
== '\\') {
766 tcc_error("unexpected end of file in comment");
768 /* skip '\[\r]\n', otherwise just skip the stray */
774 } else if (c
== '\r') {
791 /* stray, eob or eof */
796 tcc_error("unexpected end of file in comment");
797 } else if (c
== '\\') {
809 static inline void skip_spaces(void)
811 while (isidnum_table
[ch
- CH_EOF
] & IS_SPC
)
815 static inline int check_space(int t
, int *spc
)
817 if (t
< 256 && (isidnum_table
[t
- CH_EOF
] & IS_SPC
)) {
826 /* parse a string without interpreting escapes */
827 static uint8_t *parse_pp_string(uint8_t *p
,
828 int sep
, CString
*str
)
836 } else if (c
== '\\') {
842 /* XXX: indicate line number of start of string */
843 tcc_error("missing terminating %c character", sep
);
844 } else if (c
== '\\') {
845 /* escape : just skip \[\r]\n */
850 } else if (c
== '\r') {
853 expect("'\n' after '\r'");
856 } else if (c
== CH_EOF
) {
857 goto unterminated_string
;
860 cstr_ccat(str
, '\\');
866 } else if (c
== '\n') {
869 } else if (c
== '\r') {
873 cstr_ccat(str
, '\r');
889 /* skip block of text until #else, #elif or #endif. skip also pairs of
891 static void preprocess_skip(void)
893 int a
, start_of_line
, c
, in_warn_or_error
;
900 in_warn_or_error
= 0;
921 } else if (c
== '\\') {
922 ch
= file
->buf_ptr
[0];
923 handle_stray_noerror();
930 if (in_warn_or_error
)
932 p
= parse_pp_string(p
, c
, NULL
);
936 if (in_warn_or_error
)
943 p
= parse_comment(p
);
944 } else if (ch
== '/') {
945 p
= parse_line_comment(p
);
955 (tok
== TOK_ELSE
|| tok
== TOK_ELIF
|| tok
== TOK_ENDIF
))
957 if (tok
== TOK_IF
|| tok
== TOK_IFDEF
|| tok
== TOK_IFNDEF
)
959 else if (tok
== TOK_ENDIF
)
961 else if( tok
== TOK_ERROR
|| tok
== TOK_WARNING
)
962 in_warn_or_error
= 1;
963 else if (tok
== TOK_LINEFEED
)
965 else if (parse_flags
& PARSE_FLAG_ASM_FILE
)
966 p
= parse_line_comment(p
);
967 } else if (parse_flags
& PARSE_FLAG_ASM_FILE
)
968 p
= parse_line_comment(p
);
981 /* ParseState handling */
983 /* XXX: currently, no include file info is stored. Thus, we cannot display
984 accurate messages if the function or data definition spans multiple
987 /* save current parse state in 's' */
988 ST_FUNC
void save_parse_state(ParseState
*s
)
990 s
->line_num
= file
->line_num
;
991 s
->macro_ptr
= macro_ptr
;
996 /* restore parse state from 's' */
997 ST_FUNC
void restore_parse_state(ParseState
*s
)
999 file
->line_num
= s
->line_num
;
1000 macro_ptr
= s
->macro_ptr
;
1005 /* return the number of additional 'ints' necessary to store the
1007 static inline int tok_size(const int *p
)
1022 return 1 + ((sizeof(CString
) + ((CString
*)(p
+1))->size
+ 3) >> 2);
1028 return 1 + LDOUBLE_SIZE
/ 4;
1034 /* token string handling */
1036 ST_INLN
void tok_str_new(TokenString
*s
)
1040 s
->allocated_len
= 0;
1041 s
->last_line_num
= -1;
1044 ST_FUNC
int *tok_str_dup(TokenString
*s
)
1048 str
= tal_realloc(tokstr_alloc
, 0, s
->len
* sizeof(int));
1049 memcpy(str
, s
->str
, s
->len
* sizeof(int));
1053 ST_FUNC
void tok_str_free(int *str
)
1055 tal_free(tokstr_alloc
, str
);
1058 ST_FUNC
int *tok_str_realloc(TokenString
*s
, int new_size
)
1062 size
= s
->allocated_len
;
1065 while (size
< new_size
)
1067 TCC_ASSERT((size
& (size
-1)) == 0);
1068 if (size
> s
->allocated_len
) {
1069 str
= tal_realloc(tokstr_alloc
, s
->str
, size
* sizeof(int));
1070 s
->allocated_len
= size
;
1076 ST_FUNC
void tok_str_add(TokenString
*s
, int t
)
1082 if (len
>= s
->allocated_len
)
1083 str
= tok_str_realloc(s
, len
+ 1);
1088 static void tok_str_add2(TokenString
*s
, int t
, CValue
*cv
)
1095 /* allocate space for worst case */
1096 if (len
+ TOK_MAX_SIZE
>= s
->allocated_len
)
1097 str
= tok_str_realloc(s
, len
+ TOK_MAX_SIZE
+ 1);
1106 str
[len
++] = cv
->tab
[0];
1113 /* Insert the string into the int array. */
1115 1 + (cv
->str
.size
+ sizeof(int) - 1) / sizeof(int);
1116 if (len
+ nb_words
>= s
->allocated_len
)
1117 str
= tok_str_realloc(s
, len
+ nb_words
+ 1);
1118 str
[len
] = cv
->str
.size
;
1119 memcpy(&str
[len
+ 1], cv
->str
.data
, cv
->str
.size
);
1126 #if LDOUBLE_SIZE == 8
1129 str
[len
++] = cv
->tab
[0];
1130 str
[len
++] = cv
->tab
[1];
1132 #if LDOUBLE_SIZE == 12
1134 str
[len
++] = cv
->tab
[0];
1135 str
[len
++] = cv
->tab
[1];
1136 str
[len
++] = cv
->tab
[2];
1137 #elif LDOUBLE_SIZE == 16
1139 str
[len
++] = cv
->tab
[0];
1140 str
[len
++] = cv
->tab
[1];
1141 str
[len
++] = cv
->tab
[2];
1142 str
[len
++] = cv
->tab
[3];
1143 #elif LDOUBLE_SIZE != 8
1144 #error add long double size support
1153 /* add the current parse token in token string 's' */
1154 ST_FUNC
void tok_str_add_tok(TokenString
*s
)
1158 /* save line number info */
1159 if (file
->line_num
!= s
->last_line_num
) {
1160 s
->last_line_num
= file
->line_num
;
1161 cval
.i
= s
->last_line_num
;
1162 tok_str_add2(s
, TOK_LINENUM
, &cval
);
1164 tok_str_add2(s
, tok
, &tokc
);
1167 /* get a token from an integer array and increment pointer
1168 accordingly. we code it as a macro to avoid pointer aliasing. */
1169 static inline void TOK_GET(int *t
, const int **pp
, CValue
*cv
)
1188 cv
->str
.size
= *p
++;
1190 cv
->str
.data_allocated
= 0;
1191 p
+= (cv
->str
.size
+ sizeof(int) - 1) / sizeof(int);
1199 #if LDOUBLE_SIZE == 16
1201 #elif LDOUBLE_SIZE == 12
1203 #elif LDOUBLE_SIZE == 8
1206 # error add long double size support
1219 /* Calling this function is expensive, but it is not possible
1220 to read a token string backwards. */
1221 static int tok_last(const int *str0
, const int *str1
)
1223 const int *str
= str0
;
1228 TOK_GET(&tok
, &str
, &cval
);
1232 static int macro_is_equal(const int *a
, const int *b
)
1241 /* first time preallocate static cstr_buf, next time only reset position to start */
1242 cstr_reset(&cstr_buf
);
1243 TOK_GET(&t
, &a
, &cv
);
1244 cstr_cat(&cstr_buf
, get_tok_str(t
, &cv
), 0);
1245 TOK_GET(&t
, &b
, &cv
);
1246 if (strcmp(cstr_buf
.data
, get_tok_str(t
, &cv
)))
1252 /* defines handling */
1253 ST_INLN
void define_push(int v
, int macro_type
, TokenString
*str
, Sym
*first_arg
)
1258 s
= sym_push2(&define_stack
, v
, macro_type
, 0);
1259 s
->d
= str
? tok_str_dup(str
) : NULL
;
1260 s
->next
= first_arg
;
1261 table_ident
[v
- TOK_IDENT
]->sym_define
= s
;
1263 if (o
&& !macro_is_equal(o
->d
, s
->d
))
1264 tcc_warning("%s redefined", get_tok_str(v
, NULL
));
1267 /* undefined a define symbol. Its name is just set to zero */
1268 ST_FUNC
void define_undef(Sym
*s
)
1271 if (v
>= TOK_IDENT
&& v
< tok_ident
)
1272 table_ident
[v
- TOK_IDENT
]->sym_define
= NULL
;
1275 ST_INLN Sym
*define_find(int v
)
1278 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
1280 return table_ident
[v
]->sym_define
;
1283 /* free define stack until top reaches 'b' */
1284 ST_FUNC
void free_defines(Sym
*b
)
1286 while (define_stack
!= b
) {
1287 Sym
*top
= define_stack
;
1288 define_stack
= top
->prev
;
1289 tok_str_free(top
->d
);
1294 /* restore remaining (-D or predefined) symbols */
1297 if (v
>= TOK_IDENT
&& v
< tok_ident
) {
1298 Sym
**d
= &table_ident
[v
- TOK_IDENT
]->sym_define
;
1307 ST_FUNC Sym
*label_find(int v
)
1310 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
1312 return table_ident
[v
]->sym_label
;
1315 ST_FUNC Sym
*label_push(Sym
**ptop
, int v
, int flags
)
1318 s
= sym_push2(ptop
, v
, 0, 0);
1320 ps
= &table_ident
[v
- TOK_IDENT
]->sym_label
;
1321 if (ptop
== &global_label_stack
) {
1322 /* modify the top most local identifier, so that
1323 sym_identifier will point to 's' when popped */
1325 ps
= &(*ps
)->prev_tok
;
1332 /* pop labels until element last is reached. Look if any labels are
1333 undefined. Define symbols if '&&label' was used. */
1334 ST_FUNC
void label_pop(Sym
**ptop
, Sym
*slast
)
1337 for(s
= *ptop
; s
!= slast
; s
= s1
) {
1339 if (s
->r
== LABEL_DECLARED
) {
1340 tcc_warning("label '%s' declared but not used", get_tok_str(s
->v
, NULL
));
1341 } else if (s
->r
== LABEL_FORWARD
) {
1342 tcc_error("label '%s' used but not defined",
1343 get_tok_str(s
->v
, NULL
));
1346 /* define corresponding symbol. A size of
1348 put_extern_sym(s
, cur_text_section
, s
->jnext
, 1);
1352 table_ident
[s
->v
- TOK_IDENT
]->sym_label
= s
->prev_tok
;
1358 /* eval an expression for #if/#elif */
1359 static int expr_preprocess(void)
1365 while (tok
!= TOK_LINEFEED
&& tok
!= TOK_EOF
) {
1366 next(); /* do macro subst */
1367 if (tok
== TOK_DEFINED
) {
1372 c
= define_find(tok
) != 0;
1377 } else if (tok
>= TOK_IDENT
) {
1378 /* if undefined macro */
1382 tok_str_add_tok(&str
);
1384 tok_str_add(&str
, -1); /* simulate end of file */
1385 tok_str_add(&str
, 0);
1386 /* now evaluate C constant expression */
1387 begin_macro(&str
, 0);
1395 /* parse after #define */
1396 ST_FUNC
void parse_define(void)
1398 Sym
*s
, *first
, **ps
;
1399 int v
, t
, varg
, is_vaargs
, spc
;
1400 int saved_parse_flags
= parse_flags
;
1404 tcc_error("invalid macro name '%s'", get_tok_str(tok
, &tokc
));
1405 /* XXX: should check if same macro (ANSI) */
1408 /* '(' must be just after macro definition for MACRO_FUNC */
1409 parse_flags
|= PARSE_FLAG_SPACES
;
1412 /* must be able to parse TOK_DOTS (in asm mode '.' can be part of identifier) */
1413 parse_flags
&= ~PARSE_FLAG_ASM_FILE
;
1414 isidnum_table
['.' - CH_EOF
] = 0;
1417 if (tok
!= ')') for (;;) {
1421 if (varg
== TOK_DOTS
) {
1422 varg
= TOK___VA_ARGS__
;
1424 } else if (tok
== TOK_DOTS
&& gnu_ext
) {
1428 if (varg
< TOK_IDENT
)
1430 tcc_error("bad macro parameter list");
1431 s
= sym_push2(&define_stack
, varg
| SYM_FIELD
, is_vaargs
, 0);
1436 if (tok
!= ',' || is_vaargs
)
1442 parse_flags
|= (saved_parse_flags
& PARSE_FLAG_ASM_FILE
);
1443 isidnum_table
['.' - CH_EOF
] =
1444 (parse_flags
& PARSE_FLAG_ASM_FILE
) ? IS_ID
: 0;
1449 parse_flags
|= PARSE_FLAG_ACCEPT_STRAYS
| PARSE_FLAG_SPACES
| PARSE_FLAG_LINEFEED
;
1450 while (tok
!= TOK_LINEFEED
&& tok
!= TOK_EOF
) {
1451 /* remove spaces around ## and after '#' */
1452 if (TOK_TWOSHARPS
== tok
) {
1458 } else if ('#' == tok
) {
1460 } else if (check_space(tok
, &spc
)) {
1463 tok_str_add2(&tokstr_buf
, tok
, &tokc
);
1468 parse_flags
= saved_parse_flags
;
1470 --tokstr_buf
.len
; /* remove trailing space */
1471 tok_str_add(&tokstr_buf
, 0);
1474 tcc_error("'##' cannot appear at either end of macro");
1475 define_push(v
, t
, &tokstr_buf
, first
);
1478 static CachedInclude
*search_cached_include(TCCState
*s1
, const char *filename
, int add
)
1480 const unsigned char *s
;
1486 s
= (unsigned char *) filename
;
1489 h
= TOK_HASH_FUNC(h
, toup(*s
));
1491 h
= TOK_HASH_FUNC(h
, *s
);
1495 h
&= (CACHED_INCLUDES_HASH_SIZE
- 1);
1497 i
= s1
->cached_includes_hash
[h
];
1501 e
= s1
->cached_includes
[i
- 1];
1502 if (0 == PATHCMP(e
->filename
, filename
))
1509 e
= tcc_malloc(sizeof(CachedInclude
) + strlen(filename
));
1510 strcpy(e
->filename
, filename
);
1511 e
->ifndef_macro
= e
->once
= 0;
1512 dynarray_add((void ***)&s1
->cached_includes
, &s1
->nb_cached_includes
, e
);
1513 /* add in hash table */
1514 e
->hash_next
= s1
->cached_includes_hash
[h
];
1515 s1
->cached_includes_hash
[h
] = s1
->nb_cached_includes
;
1517 printf("adding cached '%s'\n", filename
);
1522 static void pragma_parse(TCCState
*s1
)
1525 if (tok
== TOK_push_macro
|| tok
== TOK_pop_macro
) {
1529 if (next(), tok
!= '(')
1531 if (next(), tok
!= TOK_STR
)
1533 v
= tok_alloc(tokc
.str
.data
, tokc
.str
.size
- 1)->tok
;
1534 if (next(), tok
!= ')')
1536 if (t
== TOK_push_macro
) {
1537 while (NULL
== (s
= define_find(v
)))
1538 define_push(v
, 0, NULL
, NULL
);
1539 s
->type
.ref
= s
; /* set push boundary */
1541 for (s
= define_stack
; s
; s
= s
->prev
)
1542 if (s
->v
== v
&& s
->type
.ref
== s
) {
1548 table_ident
[v
- TOK_IDENT
]->sym_define
= s
->d
? s
: NULL
;
1550 tcc_warning("unbalanced #pragma pop_macro");
1551 pp_debug_tok
= t
, pp_debug_symv
= v
;
1553 } else if (tok
== TOK_once
) {
1554 search_cached_include(s1
, file
->filename
, 1)->once
= pp_once
;
1556 } else if (s1
->ppfp
) {
1557 /* tcc -E: keep pragmas below unchanged */
1559 unget_tok(TOK_PRAGMA
);
1561 unget_tok(TOK_LINEFEED
);
1563 } else if (tok
== TOK_pack
) {
1565 #pragma pack(1) // set
1566 #pragma pack() // reset to default
1567 #pragma pack(push,1) // push & set
1568 #pragma pack(pop) // restore previous */
1571 if (tok
== TOK_ASM_pop
) {
1573 if (s1
->pack_stack_ptr
<= s1
->pack_stack
) {
1575 tcc_error("out of pack stack");
1577 s1
->pack_stack_ptr
--;
1581 if (tok
== TOK_ASM_push
) {
1583 if (s1
->pack_stack_ptr
>= s1
->pack_stack
+ PACK_STACK_SIZE
- 1)
1585 s1
->pack_stack_ptr
++;
1588 if (tok
!= TOK_CINT
)
1591 if (val
< 1 || val
> 16 || (val
& (val
- 1)) != 0)
1595 *s1
->pack_stack_ptr
= val
;
1600 } else if (tok
== TOK_comment
) {
1610 file
= tcc_strdup((char *)tokc
.str
.data
);
1611 dynarray_add((void ***)&s1
->pragma_libs
, &s1
->nb_pragma_libs
, file
);
1617 if (s1
->warn_unsupported
)
1618 tcc_warning("#pragma %s is ignored", get_tok_str(tok
, &tokc
));
1623 tcc_error("malformed #pragma directive");
1627 /* is_bof is true if first non space token at beginning of file */
1628 ST_FUNC
void preprocess(int is_bof
)
1630 TCCState
*s1
= tcc_state
;
1631 int i
, c
, n
, saved_parse_flags
;
1635 saved_parse_flags
= parse_flags
;
1636 parse_flags
= PARSE_FLAG_PREPROCESS
1637 | PARSE_FLAG_TOK_NUM
1638 | PARSE_FLAG_TOK_STR
1639 | PARSE_FLAG_LINEFEED
1640 | (parse_flags
& PARSE_FLAG_ASM_FILE
)
1649 pp_debug_symv
= tok
;
1655 pp_debug_symv
= tok
;
1656 s
= define_find(tok
);
1657 /* undefine symbol by putting an invalid name */
1662 case TOK_INCLUDE_NEXT
:
1663 ch
= file
->buf_ptr
[0];
1664 /* XXX: incorrect if comments : use next_nomacro with a special mode */
1669 } else if (ch
== '\"') {
1674 while (ch
!= c
&& ch
!= '\n' && ch
!= CH_EOF
) {
1675 if ((q
- buf
) < sizeof(buf
) - 1)
1678 if (handle_stray_noerror() == 0)
1686 /* eat all spaces and comments after include */
1687 /* XXX: slightly incorrect */
1688 while (ch1
!= '\n' && ch1
!= CH_EOF
)
1692 /* computed #include : either we have only strings or
1693 we have anything enclosed in '<>' */
1696 if (tok
== TOK_STR
) {
1697 while (tok
!= TOK_LINEFEED
) {
1698 if (tok
!= TOK_STR
) {
1700 tcc_error("'#include' expects \"FILENAME\" or <FILENAME>");
1702 pstrcat(buf
, sizeof(buf
), (char *)tokc
.str
.data
);
1708 while (tok
!= TOK_LINEFEED
) {
1709 pstrcat(buf
, sizeof(buf
), get_tok_str(tok
, &tokc
));
1713 /* check syntax and remove '<>' */
1714 if (len
< 2 || buf
[0] != '<' || buf
[len
- 1] != '>')
1715 goto include_syntax
;
1716 memmove(buf
, buf
+ 1, len
- 2);
1717 buf
[len
- 2] = '\0';
1722 if (s1
->include_stack_ptr
>= s1
->include_stack
+ INCLUDE_STACK_SIZE
)
1723 tcc_error("#include recursion too deep");
1724 /* store current file in stack, but increment stack later below */
1725 *s1
->include_stack_ptr
= file
;
1726 i
= tok
== TOK_INCLUDE_NEXT
? file
->include_next_index
: 0;
1727 n
= 2 + s1
->nb_include_paths
+ s1
->nb_sysinclude_paths
;
1728 for (; i
< n
; ++i
) {
1729 char buf1
[sizeof file
->filename
];
1734 /* check absolute include path */
1735 if (!IS_ABSPATH(buf
))
1739 } else if (i
== 1) {
1740 /* search in current dir if "header.h" */
1743 path
= file
->filename
;
1744 pstrncpy(buf1
, path
, tcc_basename(path
) - path
);
1747 /* search in all the include paths */
1748 int j
= i
- 2, k
= j
- s1
->nb_include_paths
;
1749 path
= k
< 0 ? s1
->include_paths
[j
] : s1
->sysinclude_paths
[k
];
1750 if (path
== 0) continue;
1751 pstrcpy(buf1
, sizeof(buf1
), path
);
1752 pstrcat(buf1
, sizeof(buf1
), "/");
1755 pstrcat(buf1
, sizeof(buf1
), buf
);
1756 e
= search_cached_include(s1
, buf1
, 0);
1757 if (e
&& (define_find(e
->ifndef_macro
) || e
->once
== pp_once
)) {
1758 /* no need to parse the include because the 'ifndef macro'
1759 is defined (or had #pragma once) */
1761 printf("%s: skipping cached %s\n", file
->filename
, buf1
);
1766 if (tcc_open(s1
, buf1
) < 0)
1769 file
->include_next_index
= i
+ 1;
1771 printf("%s: including %s\n", file
->prev
->filename
, file
->filename
);
1773 /* update target deps */
1774 dynarray_add((void ***)&s1
->target_deps
, &s1
->nb_target_deps
,
1776 /* push current file in stack */
1777 ++s1
->include_stack_ptr
;
1778 /* add include file debug info */
1780 put_stabs(file
->filename
, N_BINCL
, 0, 0, 0);
1781 tok_flags
|= TOK_FLAG_BOF
| TOK_FLAG_BOL
;
1782 ch
= file
->buf_ptr
[0];
1785 tcc_error("include file '%s' not found", buf
);
1792 c
= expr_preprocess();
1798 if (tok
< TOK_IDENT
)
1799 tcc_error("invalid argument for '#if%sdef'", c
? "n" : "");
1803 printf("#ifndef %s\n", get_tok_str(tok
, NULL
));
1805 file
->ifndef_macro
= tok
;
1808 c
= (define_find(tok
) != 0) ^ c
;
1810 if (s1
->ifdef_stack_ptr
>= s1
->ifdef_stack
+ IFDEF_STACK_SIZE
)
1811 tcc_error("memory full (ifdef)");
1812 *s1
->ifdef_stack_ptr
++ = c
;
1815 if (s1
->ifdef_stack_ptr
== s1
->ifdef_stack
)
1816 tcc_error("#else without matching #if");
1817 if (s1
->ifdef_stack_ptr
[-1] & 2)
1818 tcc_error("#else after #else");
1819 c
= (s1
->ifdef_stack_ptr
[-1] ^= 3);
1822 if (s1
->ifdef_stack_ptr
== s1
->ifdef_stack
)
1823 tcc_error("#elif without matching #if");
1824 c
= s1
->ifdef_stack_ptr
[-1];
1826 tcc_error("#elif after #else");
1827 /* last #if/#elif expression was true: we skip */
1830 c
= expr_preprocess();
1831 s1
->ifdef_stack_ptr
[-1] = c
;
1833 if (s1
->ifdef_stack_ptr
== file
->ifdef_stack_ptr
+ 1)
1834 file
->ifndef_macro
= 0;
1844 if (s1
->ifdef_stack_ptr
<= file
->ifdef_stack_ptr
)
1845 tcc_error("#endif without matching #if");
1846 s1
->ifdef_stack_ptr
--;
1847 /* '#ifndef macro' was at the start of file. Now we check if
1848 an '#endif' is exactly at the end of file */
1849 if (file
->ifndef_macro
&&
1850 s1
->ifdef_stack_ptr
== file
->ifdef_stack_ptr
) {
1851 file
->ifndef_macro_saved
= file
->ifndef_macro
;
1852 /* need to set to zero to avoid false matches if another
1853 #ifndef at middle of file */
1854 file
->ifndef_macro
= 0;
1855 while (tok
!= TOK_LINEFEED
)
1857 tok_flags
|= TOK_FLAG_ENDIF
;
1862 n
= strtoul((char*)tokc
.str
.data
, &q
, 10);
1866 if (tok
!= TOK_CINT
)
1868 tcc_error("wrong #line format");
1872 if (tok
!= TOK_LINEFEED
) {
1874 pstrcpy(file
->filename
, sizeof(file
->filename
), (char *)tokc
.str
.data
);
1875 else if (parse_flags
& PARSE_FLAG_ASM_FILE
)
1882 total_lines
+= file
->line_num
- n
;
1885 put_stabs(file
->filename
, N_BINCL
, 0, 0, 0);
1890 ch
= file
->buf_ptr
[0];
1893 while (ch
!= '\n' && ch
!= CH_EOF
) {
1894 if ((q
- buf
) < sizeof(buf
) - 1)
1897 if (handle_stray_noerror() == 0)
1904 tcc_error("#error %s", buf
);
1906 tcc_warning("#warning %s", buf
);
1914 /* ignore gas line comment in an 'S' file. */
1915 if (saved_parse_flags
& PARSE_FLAG_ASM_FILE
)
1917 if (tok
== '!' && is_bof
)
1918 /* '!' is ignored at beginning to allow C scripts. */
1920 tcc_warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok
, &tokc
));
1922 file
->buf_ptr
= parse_line_comment(file
->buf_ptr
);
1925 /* ignore other preprocess commands or #! for C scripts */
1926 while (tok
!= TOK_LINEFEED
)
1929 parse_flags
= saved_parse_flags
;
1932 /* evaluate escape codes in a string. */
1933 static void parse_escape_string(CString
*outstr
, const uint8_t *buf
, int is_long
)
1948 case '0': case '1': case '2': case '3':
1949 case '4': case '5': case '6': case '7':
1950 /* at most three octal digits */
1955 n
= n
* 8 + c
- '0';
1959 n
= n
* 8 + c
- '0';
1964 goto add_char_nonext
;
1972 if (c
>= 'a' && c
<= 'f')
1974 else if (c
>= 'A' && c
<= 'F')
1984 goto add_char_nonext
;
2008 goto invalid_escape
;
2018 if (c
>= '!' && c
<= '~')
2019 tcc_warning("unknown escape sequence: \'\\%c\'", c
);
2021 tcc_warning("unknown escape sequence: \'\\x%x\'", c
);
2028 cstr_ccat(outstr
, c
);
2030 cstr_wccat(outstr
, c
);
2032 /* add a trailing '\0' */
2034 cstr_ccat(outstr
, '\0');
2036 cstr_wccat(outstr
, '\0');
2039 static void parse_string(const char *s
, int len
)
2041 uint8_t buf
[1000], *p
= buf
;
2044 if ((is_long
= *s
== 'L'))
2048 if (len
>= sizeof buf
)
2049 p
= tcc_malloc(len
+ 1);
2053 cstr_reset(&tokcstr
);
2054 parse_escape_string(&tokcstr
, p
, is_long
);
2060 /* XXX: make it portable */
2064 char_size
= sizeof(nwchar_t
);
2065 if (tokcstr
.size
<= char_size
)
2066 tcc_error("empty character constant");
2067 if (tokcstr
.size
> 2 * char_size
)
2068 tcc_warning("multi-character character constant");
2070 tokc
.i
= *(int8_t *)tokcstr
.data
;
2073 tokc
.i
= *(nwchar_t
*)tokcstr
.data
;
2077 tokc
.str
.size
= tokcstr
.size
;
2078 tokc
.str
.data
= tokcstr
.data
;
2079 tokc
.str
.data_allocated
= tokcstr
.data_allocated
;
2087 /* we use 64 bit numbers */
2090 /* bn = (bn << shift) | or_val */
2091 static void bn_lshift(unsigned int *bn
, int shift
, int or_val
)
2095 for(i
=0;i
<BN_SIZE
;i
++) {
2097 bn
[i
] = (v
<< shift
) | or_val
;
2098 or_val
= v
>> (32 - shift
);
2102 static void bn_zero(unsigned int *bn
)
2105 for(i
=0;i
<BN_SIZE
;i
++) {
2110 /* parse number in null terminated string 'p' and return it in the
2112 static void parse_number(const char *p
)
2114 int b
, t
, shift
, frac_bits
, s
, exp_val
, ch
;
2116 unsigned int bn
[BN_SIZE
];
2127 goto float_frac_parse
;
2128 } else if (t
== '0') {
2129 if (ch
== 'x' || ch
== 'X') {
2133 } else if (tcc_ext
&& (ch
== 'b' || ch
== 'B')) {
2139 /* parse all digits. cannot check octal numbers at this stage
2140 because of floating point constants */
2142 if (ch
>= 'a' && ch
<= 'f')
2144 else if (ch
>= 'A' && ch
<= 'F')
2152 if (q
>= token_buf
+ STRING_MAX_SIZE
) {
2154 tcc_error("number too long");
2160 ((ch
== 'e' || ch
== 'E') && b
== 10) ||
2161 ((ch
== 'p' || ch
== 'P') && (b
== 16 || b
== 2))) {
2163 /* NOTE: strtox should support that for hexa numbers, but
2164 non ISOC99 libcs do not support it, so we prefer to do
2166 /* hexadecimal or binary floats */
2167 /* XXX: handle overflows */
2179 } else if (t
>= 'a') {
2181 } else if (t
>= 'A') {
2186 bn_lshift(bn
, shift
, t
);
2193 if (t
>= 'a' && t
<= 'f') {
2195 } else if (t
>= 'A' && t
<= 'F') {
2197 } else if (t
>= '0' && t
<= '9') {
2203 tcc_error("invalid digit");
2204 bn_lshift(bn
, shift
, t
);
2209 if (ch
!= 'p' && ch
!= 'P')
2216 } else if (ch
== '-') {
2220 if (ch
< '0' || ch
> '9')
2221 expect("exponent digits");
2222 while (ch
>= '0' && ch
<= '9') {
2223 exp_val
= exp_val
* 10 + ch
- '0';
2226 exp_val
= exp_val
* s
;
2228 /* now we can generate the number */
2229 /* XXX: should patch directly float number */
2230 d
= (double)bn
[1] * 4294967296.0 + (double)bn
[0];
2231 d
= ldexp(d
, exp_val
- frac_bits
);
2236 /* float : should handle overflow */
2238 } else if (t
== 'L') {
2240 #ifdef TCC_TARGET_PE
2245 /* XXX: not large enough */
2246 tokc
.ld
= (long double)d
;
2253 /* decimal floats */
2255 if (q
>= token_buf
+ STRING_MAX_SIZE
)
2260 while (ch
>= '0' && ch
<= '9') {
2261 if (q
>= token_buf
+ STRING_MAX_SIZE
)
2267 if (ch
== 'e' || ch
== 'E') {
2268 if (q
>= token_buf
+ STRING_MAX_SIZE
)
2272 if (ch
== '-' || ch
== '+') {
2273 if (q
>= token_buf
+ STRING_MAX_SIZE
)
2278 if (ch
< '0' || ch
> '9')
2279 expect("exponent digits");
2280 while (ch
>= '0' && ch
<= '9') {
2281 if (q
>= token_buf
+ STRING_MAX_SIZE
)
2293 tokc
.f
= strtof(token_buf
, NULL
);
2294 } else if (t
== 'L') {
2296 #ifdef TCC_TARGET_PE
2298 tokc
.d
= strtod(token_buf
, NULL
);
2301 tokc
.ld
= strtold(token_buf
, NULL
);
2305 tokc
.d
= strtod(token_buf
, NULL
);
2309 unsigned long long n
, n1
;
2310 int lcount
, ucount
, must_64bit
;
2313 /* integer number */
2316 if (b
== 10 && *q
== '0') {
2323 /* no need for checks except for base 10 / 8 errors */
2333 tcc_error("invalid digit");
2336 /* detect overflow */
2337 /* XXX: this test is not reliable */
2339 tcc_error("integer constant overflow");
2342 /* Determine the characteristics (unsigned and/or 64bit) the type of
2343 the constant must have according to the constant suffix(es) */
2344 lcount
= ucount
= must_64bit
= 0;
2350 tcc_error("three 'l's in integer constant");
2351 if (lcount
&& *(p
- 1) != ch
)
2352 tcc_error("incorrect integer suffix: %s", p1
);
2354 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
2359 } else if (t
== 'U') {
2361 tcc_error("two 'u's in integer constant");
2369 /* Whether 64 bits are needed to hold the constant's value */
2370 if (n
& 0xffffffff00000000LL
|| must_64bit
) {
2378 /* Whether type must be unsigned to hold the constant's value */
2379 if (ucount
|| ((n1
>> 31) && (b
!= 10))) {
2380 if (tok
== TOK_CLLONG
)
2384 /* If decimal and no unsigned suffix, bump to 64 bits or throw error */
2385 } else if (n1
>> 31) {
2386 if (tok
== TOK_CINT
)
2389 tcc_error("integer constant overflow");
2395 tcc_error("invalid number\n");
2399 #define PARSE2(c1, tok1, c2, tok2) \
2410 /* return next token without macro substitution */
2411 static inline void next_nomacro1(void)
2413 int t
, c
, is_long
, len
;
2421 #if (__TINYC__ || __GNUC__)
2424 goto parse_ident_fast
;
2431 if (parse_flags
& PARSE_FLAG_SPACES
)
2432 goto keep_tok_flags
;
2433 while (isidnum_table
[*p
- CH_EOF
] & IS_SPC
)
2442 /* first look if it is in fact an end of buffer */
2443 c
= handle_stray1(p
);
2450 TCCState
*s1
= tcc_state
;
2451 if ((parse_flags
& PARSE_FLAG_LINEFEED
)
2452 && !(tok_flags
& TOK_FLAG_EOF
)) {
2453 tok_flags
|= TOK_FLAG_EOF
;
2455 goto keep_tok_flags
;
2456 } else if (!(parse_flags
& PARSE_FLAG_PREPROCESS
)) {
2458 } else if (s1
->ifdef_stack_ptr
!= file
->ifdef_stack_ptr
) {
2459 tcc_error("missing #endif");
2460 } else if (s1
->include_stack_ptr
== s1
->include_stack
) {
2461 /* no include left : end of file. */
2464 tok_flags
&= ~TOK_FLAG_EOF
;
2465 /* pop include file */
2467 /* test if previous '#endif' was after a #ifdef at
2469 if (tok_flags
& TOK_FLAG_ENDIF
) {
2471 printf("#endif %s\n", get_tok_str(file
->ifndef_macro_saved
, NULL
));
2473 search_cached_include(s1
, file
->filename
, 1)
2474 ->ifndef_macro
= file
->ifndef_macro_saved
;
2475 tok_flags
&= ~TOK_FLAG_ENDIF
;
2478 /* add end of include file debug info */
2479 if (tcc_state
->do_debug
) {
2480 put_stabd(N_EINCL
, 0, 0);
2482 /* pop include stack */
2484 s1
->include_stack_ptr
--;
2493 tok_flags
|= TOK_FLAG_BOL
;
2496 if (0 == (parse_flags
& PARSE_FLAG_LINEFEED
))
2499 goto keep_tok_flags
;
2504 if ((tok_flags
& TOK_FLAG_BOL
) &&
2505 (parse_flags
& PARSE_FLAG_PREPROCESS
)) {
2507 preprocess(tok_flags
& TOK_FLAG_BOF
);
2513 tok
= TOK_TWOSHARPS
;
2515 if (parse_flags
& PARSE_FLAG_ASM_FILE
) {
2516 p
= parse_line_comment(p
- 1);
2525 /* dollar is allowed to start identifiers when not parsing asm */
2527 if (!(isidnum_table
[c
- CH_EOF
] & IS_ID
)
2528 || (parse_flags
& PARSE_FLAG_ASM_FILE
))
2531 #if (__TINYC__ || __GNUC__)
2538 case 'a': case 'b': case 'c': case 'd':
2539 case 'e': case 'f': case 'g': case 'h':
2540 case 'i': case 'j': case 'k': case 'l':
2541 case 'm': case 'n': case 'o': case 'p':
2542 case 'q': case 'r': case 's': case 't':
2543 case 'u': case 'v': case 'w': case 'x':
2545 case 'A': case 'B': case 'C': case 'D':
2546 case 'E': case 'F': case 'G': case 'H':
2547 case 'I': case 'J': case 'K':
2548 case 'M': case 'N': case 'O': case 'P':
2549 case 'Q': case 'R': case 'S': case 'T':
2550 case 'U': case 'V': case 'W': case 'X':
2557 h
= TOK_HASH_FUNC(h
, c
);
2558 while (c
= *++p
, isidnum_table
[c
- CH_EOF
] & (IS_ID
|IS_NUM
))
2559 h
= TOK_HASH_FUNC(h
, c
);
2564 /* fast case : no stray found, so we have the full token
2565 and we have already hashed it */
2566 h
&= (TOK_HASH_SIZE
- 1);
2567 pts
= &hash_ident
[h
];
2572 if (ts
->len
== len
&& !memcmp(ts
->str
, p1
, len
))
2574 pts
= &(ts
->hash_next
);
2576 ts
= tok_alloc_new(pts
, (char *) p1
, len
);
2580 cstr_reset(&tokcstr
);
2581 cstr_cat(&tokcstr
, p1
, len
);
2585 while (isidnum_table
[c
- CH_EOF
] & (IS_ID
|IS_NUM
))
2587 cstr_ccat(&tokcstr
, c
);
2590 ts
= tok_alloc(tokcstr
.data
, tokcstr
.size
);
2596 if (t
!= '\\' && t
!= '\'' && t
!= '\"') {
2598 goto parse_ident_fast
;
2601 if (c
== '\'' || c
== '\"') {
2605 cstr_reset(&tokcstr
);
2606 cstr_ccat(&tokcstr
, 'L');
2607 goto parse_ident_slow
;
2612 case '0': case '1': case '2': case '3':
2613 case '4': case '5': case '6': case '7':
2615 cstr_reset(&tokcstr
);
2616 /* after the first digit, accept digits, alpha, '.' or sign if
2617 prefixed by 'eEpP' */
2621 cstr_ccat(&tokcstr
, c
);
2623 if (!((isidnum_table
[c
- CH_EOF
] & (IS_ID
|IS_NUM
))
2625 || ((c
== '+' || c
== '-')
2626 && (t
== 'e' || t
== 'E' || t
== 'p' || t
== 'P')
2627 && !(parse_flags
& PARSE_FLAG_ASM_FILE
)
2631 /* We add a trailing '\0' to ease parsing */
2632 cstr_ccat(&tokcstr
, '\0');
2633 tokc
.str
.size
= tokcstr
.size
;
2634 tokc
.str
.data
= tokcstr
.data
;
2635 tokc
.str
.data_allocated
= tokcstr
.data_allocated
;
2640 /* special dot handling because it can also start a number */
2643 cstr_reset(&tokcstr
);
2644 cstr_ccat(&tokcstr
, '.');
2646 } else if ((parse_flags
& PARSE_FLAG_ASM_FILE
)
2647 && (isidnum_table
[c
- CH_EOF
] & (IS_ID
|IS_NUM
))) {
2649 goto parse_ident_fast
;
2650 } else if (c
== '.') {
2656 *--p
= '.'; /* may underflow into file->unget[] */
2667 cstr_reset(&tokcstr
);
2669 cstr_ccat(&tokcstr
, 'L');
2670 cstr_ccat(&tokcstr
, c
);
2671 p
= parse_pp_string(p
, c
, &tokcstr
);
2672 cstr_ccat(&tokcstr
, c
);
2673 cstr_ccat(&tokcstr
, '\0');
2674 tokc
.str
.size
= tokcstr
.size
;
2675 tokc
.str
.data
= tokcstr
.data
;
2676 tokc
.str
.data_allocated
= tokcstr
.data_allocated
;
2685 } else if (c
== '<') {
2702 } else if (c
== '>') {
2720 } else if (c
== '=') {
2733 } else if (c
== '=') {
2746 } else if (c
== '=') {
2759 } else if (c
== '=') {
2762 } else if (c
== '>') {
2770 PARSE2('!', '!', '=', TOK_NE
)
2771 PARSE2('=', '=', '=', TOK_EQ
)
2772 PARSE2('*', '*', '=', TOK_A_MUL
)
2773 PARSE2('%', '%', '=', TOK_A_MOD
)
2774 PARSE2('^', '^', '=', TOK_A_XOR
)
2776 /* comments or operator */
2780 p
= parse_comment(p
);
2781 /* comments replaced by a blank */
2783 goto keep_tok_flags
;
2784 } else if (c
== '/') {
2785 p
= parse_line_comment(p
);
2787 goto keep_tok_flags
;
2788 } else if (c
== '=') {
2808 case '@': /* only used in assembler */
2814 if (parse_flags
& PARSE_FLAG_ASM_FILE
)
2816 tcc_error("unrecognized character \\x%02x", c
);
2822 #if defined(PARSE_DEBUG)
2823 printf("token = %s\n", get_tok_str(tok
, &tokc
));
2827 /* return next token without macro substitution. Can read input from
2829 static void next_nomacro_spc(void)
2835 TOK_GET(&tok
, ¯o_ptr
, &tokc
);
2836 if (tok
== TOK_LINENUM
) {
2837 file
->line_num
= tokc
.i
;
2846 ST_FUNC
void next_nomacro(void)
2850 } while (tok
< 256 && (isidnum_table
[tok
- CH_EOF
] & IS_SPC
));
2854 static void macro_subst(
2855 TokenString
*tok_str
,
2857 const int *macro_str
,
2861 /* substitute arguments in replacement lists in macro_str by the values in
2862 args (field d) and return allocated string */
2863 static int *macro_arg_subst(Sym
**nested_list
, const int *macro_str
, Sym
*args
)
2875 TOK_GET(&t
, ¯o_str
, &cval
);
2880 TOK_GET(&t
, ¯o_str
, &cval
);
2883 s
= sym_find2(args
, t
);
2886 cstr_ccat(&cstr
, '\"');
2890 TOK_GET(&t
, &st
, &cval
);
2891 if (t
!= TOK_PLCHLDR
2893 && 0 == check_space(t
, &spc
)) {
2894 const char *s
= get_tok_str(t
, &cval
);
2896 if (t
== TOK_PPSTR
&& *s
!= '\'')
2897 add_char(&cstr
, *s
);
2899 cstr_ccat(&cstr
, *s
);
2905 cstr_ccat(&cstr
, '\"');
2906 cstr_ccat(&cstr
, '\0');
2908 printf("\nstringize: <%s>\n", (char *)cstr
.data
);
2911 cval
.str
.size
= cstr
.size
;
2912 cval
.str
.data
= cstr
.data
;
2913 cval
.str
.data_allocated
= cstr
.data_allocated
;
2914 tok_str_add2(&str
, TOK_PPSTR
, &cval
);
2918 expect("macro parameter after '#'");
2920 } else if (t
>= TOK_IDENT
) {
2921 s
= sym_find2(args
, t
);
2925 /* if '##' is present before or after, no arg substitution */
2926 if (*macro_str
== TOK_TWOSHARPS
|| t1
== TOK_TWOSHARPS
) {
2927 /* special case for var arg macros : ## eats the ','
2928 if empty VA_ARGS variable. */
2929 if (t1
== TOK_TWOSHARPS
&& t0
== ',' && gnu_ext
&& s
->type
.t
) {
2931 /* suppress ',' '##' */
2934 /* suppress '##' and add variable */
2941 TOK_GET(&t1
, &st
, &cval
);
2944 tok_str_add2(&str
, t1
, &cval
);
2950 /* NOTE: the stream cannot be read when macro
2951 substituing an argument */
2952 macro_subst(&str
, nested_list
, st
, 0);
2954 if (str
.len
== l0
) /* exanded to empty string */
2955 tok_str_add(&str
, TOK_PLCHLDR
);
2957 tok_str_add(&str
, t
);
2960 tok_str_add2(&str
, t
, &cval
);
2964 tok_str_add(&str
, 0);
2968 static char const ab_month_name
[12][4] =
2970 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2971 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
2974 /* peek or read [ws_str == NULL] next token from function macro call,
2975 walking up macro levels up to the file if necessary */
2976 static int next_argstream(Sym
**nested_list
, int can_read_stream
, TokenString
*ws_str
)
2984 p
= macro_ptr
, t
= *p
;
2986 while (is_space(t
) || TOK_LINEFEED
== t
)
2987 tok_str_add(ws_str
, t
), t
= *++p
;
2989 if (t
== 0 && can_read_stream
) {
2991 /* also, end of scope for nested defined symbol */
2993 while (sa
&& sa
->v
== 0)
3002 while (is_space(ch
) || ch
== '\n' || ch
== '/') {
3005 uint8_t *p
= file
->buf_ptr
;
3008 p
= parse_comment(p
);
3009 file
->buf_ptr
= p
- 1;
3010 } else if (c
== '/') {
3011 p
= parse_line_comment(p
);
3012 file
->buf_ptr
= p
- 1;
3017 tok_str_add(ws_str
, ch
);
3031 /* do macro substitution of current token with macro 's' and add
3032 result to (tok_str,tok_len). 'nested_list' is the list of all
3033 macros we got inside to avoid recursing. Return non zero if no
3034 substitution needs to be done */
3035 static int macro_subst_tok(
3036 TokenString
*tok_str
,
3039 int can_read_stream
)
3041 Sym
*args
, *sa
, *sa1
;
3042 int parlevel
, *mstr
, t
, t1
, spc
;
3049 /* if symbol is a macro, prepare substitution */
3050 /* special macros */
3051 if (tok
== TOK___LINE__
) {
3052 snprintf(buf
, sizeof(buf
), "%d", file
->line_num
);
3056 } else if (tok
== TOK___FILE__
) {
3057 cstrval
= file
->filename
;
3059 } else if (tok
== TOK___DATE__
|| tok
== TOK___TIME__
) {
3064 tm
= localtime(&ti
);
3065 if (tok
== TOK___DATE__
) {
3066 snprintf(buf
, sizeof(buf
), "%s %2d %d",
3067 ab_month_name
[tm
->tm_mon
], tm
->tm_mday
, tm
->tm_year
+ 1900);
3069 snprintf(buf
, sizeof(buf
), "%02d:%02d:%02d",
3070 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
3077 cstr_cat(&cstr
, cstrval
, 0);
3078 cval
.str
.size
= cstr
.size
;
3079 cval
.str
.data
= cstr
.data
;
3080 cval
.str
.data_allocated
= cstr
.data_allocated
;
3081 tok_str_add2(tok_str
, t1
, &cval
);
3084 int saved_parse_flags
= parse_flags
;
3087 if (s
->type
.t
== MACRO_FUNC
) {
3088 /* whitespace between macro name and argument list */
3090 tok_str_new(&ws_str
);
3093 parse_flags
|= PARSE_FLAG_SPACES
| PARSE_FLAG_LINEFEED
3094 | PARSE_FLAG_ACCEPT_STRAYS
;
3096 /* get next token from argument stream */
3097 t
= next_argstream(nested_list
, can_read_stream
, &ws_str
);
3099 /* not a macro substitution after all, restore the
3100 * macro token plus all whitespace we've read.
3101 * whitespace is intentionally not merged to preserve
3103 parse_flags
= saved_parse_flags
;
3104 tok_str_add(tok_str
, tok
);
3105 if (parse_flags
& PARSE_FLAG_SPACES
) {
3107 for (i
= 0; i
< ws_str
.len
; i
++)
3108 tok_str_add(tok_str
, ws_str
.str
[i
]);
3110 tok_str_free(ws_str
.str
);
3113 tok_str_free(ws_str
.str
);
3115 next_nomacro(); /* eat '(' */
3117 /* argument macro */
3120 /* NOTE: empty args are allowed, except if no args */
3123 next_argstream(nested_list
, can_read_stream
, NULL
);
3124 } while (is_space(tok
) || TOK_LINEFEED
== tok
);
3126 /* handle '()' case */
3127 if (!args
&& !sa
&& tok
== ')')
3130 tcc_error("macro '%s' used with too many args",
3131 get_tok_str(s
->v
, 0));
3134 /* NOTE: non zero sa->t indicates VA_ARGS */
3135 while ((parlevel
> 0 ||
3137 (tok
!= ',' || sa
->type
.t
)))) {
3138 if (tok
== TOK_EOF
|| tok
== 0)
3142 else if (tok
== ')')
3144 if (tok
== TOK_LINEFEED
)
3146 if (!check_space(tok
, &spc
))
3147 tok_str_add2(&str
, tok
, &tokc
);
3148 next_argstream(nested_list
, can_read_stream
, NULL
);
3153 tok_str_add(&str
, 0);
3154 sa1
= sym_push2(&args
, sa
->v
& ~SYM_FIELD
, sa
->type
.t
, 0);
3158 /* special case for gcc var args: add an empty
3159 var arg argument if it is omitted */
3160 if (sa
&& sa
->type
.t
&& gnu_ext
)
3168 tcc_error("macro '%s' used with too few args",
3169 get_tok_str(s
->v
, 0));
3172 parse_flags
= saved_parse_flags
;
3174 /* now subst each arg */
3175 mstr
= macro_arg_subst(nested_list
, mstr
, args
);
3180 tok_str_free(sa
->d
);
3186 sym_push2(nested_list
, s
->v
, 0, 0);
3187 parse_flags
= saved_parse_flags
;
3188 macro_subst(tok_str
, nested_list
, mstr
, can_read_stream
| 2);
3190 /* pop nested defined symbol */
3192 *nested_list
= sa1
->prev
;
3200 static int paste_tokens(int t1
, CValue
*v1
, int t2
, CValue
*v2
)
3206 if (t1
!= TOK_PLCHLDR
)
3207 cstr_cat(&cstr
, get_tok_str(t1
, v1
), -1);
3209 if (t2
!= TOK_PLCHLDR
)
3210 cstr_cat(&cstr
, get_tok_str(t2
, v2
), -1);
3211 cstr_ccat(&cstr
, '\0');
3213 tcc_open_bf(tcc_state
, ":paste:", cstr
.size
);
3214 memcpy(file
->buffer
, cstr
.data
, cstr
.size
);
3217 if (0 == *file
->buf_ptr
)
3221 tcc_warning("pasting <%.*s> and <%s> does not give a valid preprocessing token",
3222 n
, cstr
.data
, (char*)cstr
.data
+ n
);
3227 //printf("paste <%s>\n", (char*)cstr.data);
3232 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
3233 return the resulting string (which must be freed). */
3234 static inline int *macro_twosharps(const int *ptr0
)
3238 TokenString macro_str1
;
3239 int start_of_nosubsts
= -1;
3242 /* we search the first '##' */
3243 for (ptr
= ptr0
;;) {
3244 TOK_GET(&t
, &ptr
, &cval
);
3245 if (t
== TOK_TWOSHARPS
)
3251 tok_str_new(¯o_str1
);
3253 //tok_print(" $$$", ptr0);
3254 for (ptr
= ptr0
;;) {
3255 TOK_GET(&t
, &ptr
, &cval
);
3258 if (t
== TOK_TWOSHARPS
)
3260 while (*ptr
== TOK_TWOSHARPS
) {
3262 /* given 'a##b', remove nosubsts preceding 'a' */
3263 if (start_of_nosubsts
>= 0)
3264 macro_str1
.len
= start_of_nosubsts
;
3265 /* given 'a##b', remove nosubsts preceding 'b' */
3266 while ((t1
= *++ptr
) == TOK_NOSUBST
)
3268 if (t1
&& t1
!= TOK_TWOSHARPS
3269 && t1
!= ':') /* 'a##:' don't build a new token */
3271 TOK_GET(&t1
, &ptr
, &cv1
);
3272 if (t
!= TOK_PLCHLDR
|| t1
!= TOK_PLCHLDR
) {
3273 paste_tokens(t
, &cval
, t1
, &cv1
);
3274 t
= tok
, cval
= tokc
;
3278 if (t
== TOK_NOSUBST
) {
3279 if (start_of_nosubsts
< 0)
3280 start_of_nosubsts
= macro_str1
.len
;
3282 start_of_nosubsts
= -1;
3284 tok_str_add2(¯o_str1
, t
, &cval
);
3286 tok_str_add(¯o_str1
, 0);
3287 //tok_print(" ###", macro_str1.str);
3288 return macro_str1
.str
;
3291 /* do macro substitution of macro_str and add result to
3292 (tok_str,tok_len). 'nested_list' is the list of all macros we got
3293 inside to avoid recursing. */
3294 static void macro_subst(
3295 TokenString
*tok_str
,
3297 const int *macro_str
,
3303 int t
, spc
, nosubst
;
3305 int *macro_str1
= NULL
;
3307 /* first scan for '##' operator handling */
3311 /* first scan for '##' operator handling */
3312 if (can_read_stream
& 1) {
3313 macro_str1
= macro_twosharps(ptr
);
3319 TOK_GET(&t
, &ptr
, &cval
);
3323 if (t
>= TOK_IDENT
&& 0 == nosubst
) {
3328 /* if nested substitution, do nothing */
3329 if (sym_find2(*nested_list
, t
)) {
3330 /* and mark it as TOK_NOSUBST, so it doesn't get subst'd again */
3331 tok_str_add2(tok_str
, TOK_NOSUBST
, NULL
);
3337 str
.str
= (int*)ptr
;
3338 begin_macro(&str
, 2);
3341 macro_subst_tok(tok_str
, nested_list
, s
, can_read_stream
);
3343 if (str
.alloc
== 3) {
3344 /* already finished by reading function macro arguments */
3352 spc
= (tok_str
->len
&&
3353 is_space(tok_last(tok_str
->str
,
3354 tok_str
->str
+ tok_str
->len
)));
3358 if (t
== '\\' && !(parse_flags
& PARSE_FLAG_ACCEPT_STRAYS
))
3359 tcc_error("stray '\\' in program");
3362 if (!check_space(t
, &spc
))
3363 tok_str_add2(tok_str
, t
, &cval
);
3365 if (t
== TOK_NOSUBST
)
3370 tok_str_free(macro_str1
);
3374 /* return next token with macro substitution */
3375 ST_FUNC
void next(void)
3378 if (parse_flags
& PARSE_FLAG_SPACES
)
3384 if (tok
== TOK_NOSUBST
|| tok
== TOK_PLCHLDR
) {
3385 /* discard preprocessor markers */
3387 } else if (tok
== 0) {
3388 /* end of macro or unget token string */
3392 } else if (tok
>= TOK_IDENT
&& (parse_flags
& PARSE_FLAG_PREPROCESS
)) {
3394 /* if reading from file, try to substitute macros */
3395 s
= define_find(tok
);
3397 Sym
*nested_list
= NULL
;
3400 macro_subst_tok(&tokstr_buf
, &nested_list
, s
, 1);
3401 tok_str_add(&tokstr_buf
, 0);
3402 begin_macro(&tokstr_buf
, 2);
3406 /* convert preprocessor tokens into C tokens */
3407 if (tok
== TOK_PPNUM
) {
3408 if (parse_flags
& PARSE_FLAG_TOK_NUM
)
3409 parse_number((char *)tokc
.str
.data
);
3410 } else if (tok
== TOK_PPSTR
) {
3411 if (parse_flags
& PARSE_FLAG_TOK_STR
)
3412 parse_string((char *)tokc
.str
.data
, tokc
.str
.size
- 1);
3416 /* push back current token and set current token to 'last_tok'. Only
3417 identifier case handled for labels. */
3418 ST_INLN
void unget_tok(int last_tok
)
3420 TokenString
*str
= tcc_malloc(sizeof *str
);
3422 tok_str_add2(str
, tok
, &tokc
);
3423 tok_str_add(str
, 0);
3424 begin_macro(str
, 1);
3428 ST_FUNC
void preprocess_init(TCCState
*s1
)
3430 s1
->include_stack_ptr
= s1
->include_stack
;
3431 /* XXX: move that before to avoid having to initialize
3432 file->ifdef_stack_ptr ? */
3433 s1
->ifdef_stack_ptr
= s1
->ifdef_stack
;
3434 file
->ifdef_stack_ptr
= s1
->ifdef_stack_ptr
;
3437 pvtop
= vtop
= vstack
- 1;
3438 s1
->pack_stack
[0] = 0;
3439 s1
->pack_stack_ptr
= s1
->pack_stack
;
3441 isidnum_table
['$' - CH_EOF
] =
3442 s1
->dollars_in_identifiers
? IS_ID
: 0;
3443 isidnum_table
['.' - CH_EOF
] =
3444 (parse_flags
& PARSE_FLAG_ASM_FILE
) ? IS_ID
: 0;
3447 ST_FUNC
void preprocess_new(void)
3452 /* init isid table */
3453 for(i
= CH_EOF
; i
<128; i
++)
3454 isidnum_table
[i
- CH_EOF
]
3455 = is_space(i
) ? IS_SPC
3460 for(i
= 128; i
<256; i
++)
3461 isidnum_table
[i
- CH_EOF
] = IS_ID
;
3463 /* init allocators */
3464 tal_new(&toksym_alloc
, TOKSYM_TAL_LIMIT
, TOKSYM_TAL_SIZE
);
3465 tal_new(&tokstr_alloc
, TOKSTR_TAL_LIMIT
, TOKSTR_TAL_SIZE
);
3466 tal_new(&cstr_alloc
, CSTR_TAL_LIMIT
, CSTR_TAL_SIZE
);
3468 memset(hash_ident
, 0, TOK_HASH_SIZE
* sizeof(TokenSym
*));
3469 cstr_new(&cstr_buf
);
3470 cstr_realloc(&cstr_buf
, STRING_MAX_SIZE
);
3471 tok_str_new(&tokstr_buf
);
3472 tok_str_realloc(&tokstr_buf
, TOKSTR_MAX_SIZE
);
3474 tok_ident
= TOK_IDENT
;
3483 tok_alloc(p
, r
- p
- 1);
3488 ST_FUNC
void preprocess_delete(void)
3492 /* free -D and compiler defines */
3495 /* cleanup from error/setjmp */
3501 n
= tok_ident
- TOK_IDENT
;
3502 for(i
= 0; i
< n
; i
++)
3503 tal_free(toksym_alloc
, table_ident
[i
]);
3504 tcc_free(table_ident
);
3507 /* free static buffers */
3508 cstr_free(&tokcstr
);
3509 cstr_free(&cstr_buf
);
3510 tok_str_free(tokstr_buf
.str
);
3512 /* free allocators */
3513 tal_delete(toksym_alloc
);
3514 toksym_alloc
= NULL
;
3515 tal_delete(tokstr_alloc
);
3516 tokstr_alloc
= NULL
;
3517 tal_delete(cstr_alloc
);
3521 /* ------------------------------------------------------------------------- */
3522 /* tcc -E [-P[1]] [-dD} support */
3524 static void tok_print(const char *msg
, const int *str
)
3530 fp
= tcc_state
->ppfp
;
3531 if (!fp
|| !tcc_state
->dflag
)
3534 fprintf(fp
, "%s ", msg
);
3536 TOK_GET(&t
, &str
, &cval
);
3539 fprintf(fp
,"%s", get_tok_str(t
, &cval
));
3544 static void pp_line(TCCState
*s1
, BufferedFile
*f
, int level
)
3546 int d
= f
->line_num
- f
->line_ref
;
3551 if (s1
->Pflag
== LINE_MACRO_OUTPUT_FORMAT_NONE
) {
3552 if (level
== 0 && f
->line_ref
&& d
) {
3556 } else if (level
== 0 && f
->line_ref
&& d
< 8) {
3559 fputs("\n", s1
->ppfp
), --d
;
3560 } else if (s1
->Pflag
== LINE_MACRO_OUTPUT_FORMAT_STD
) {
3561 fprintf(s1
->ppfp
, "#line %d \"%s\"\n", f
->line_num
, f
->filename
);
3563 fprintf(s1
->ppfp
, "# %d \"%s\"%s\n", f
->line_num
, f
->filename
,
3564 level
> 0 ? " 1" : level
< 0 ? " 2" : "");
3566 f
->line_ref
= f
->line_num
;
3569 static void define_print(TCCState
*s1
, int v
)
3575 if (NULL
== s
|| NULL
== s
->d
)
3579 fprintf(fp
, "#define %s", get_tok_str(v
, NULL
));
3580 if (s
->type
.t
== MACRO_FUNC
) {
3585 fprintf(fp
,"%s", get_tok_str(a
->v
& ~SYM_FIELD
, NULL
));
3592 tok_print("", s
->d
);
3595 static void pp_debug_defines(TCCState
*s1
)
3606 pp_line(s1
, file
, 0);
3607 file
->line_ref
= ++file
->line_num
;
3611 vs
= get_tok_str(v
, NULL
);
3612 if (t
== TOK_DEFINE
) {
3613 define_print(s1
, v
);
3614 } else if (t
== TOK_UNDEF
) {
3615 fprintf(fp
, "#undef %s\n", vs
);
3616 } else if (t
== TOK_push_macro
) {
3617 fprintf(fp
, "#pragma push_macro(\"%s\")\n", vs
);
3618 } else if (t
== TOK_pop_macro
) {
3619 fprintf(fp
, "#pragma pop_macro(\"%s\")\n", vs
);
3624 static void pp_debug_builtins(TCCState
*s1
)
3627 for (v
= TOK_IDENT
; v
< tok_ident
; ++v
)
3628 define_print(s1
, v
);
3631 static int need_space(int prev_tok
, int tok
, const char *tokstr
)
3633 const char *sp_chars
= "";
3634 if ((prev_tok
>= TOK_IDENT
|| prev_tok
== TOK_PPNUM
) &&
3635 (tok
>= TOK_IDENT
|| tok
== TOK_PPNUM
))
3676 return !!strchr(sp_chars
, tokstr
[0]);
3679 /* Preprocess the current file */
3680 ST_FUNC
int tcc_preprocess(TCCState
*s1
)
3682 BufferedFile
**iptr
;
3683 int token_seen
, spcs
, level
;
3687 preprocess_init(s1
);
3688 ch
= file
->buf_ptr
[0];
3689 tok_flags
= TOK_FLAG_BOL
| TOK_FLAG_BOF
;
3690 parse_flags
= PARSE_FLAG_PREPROCESS
3691 | (parse_flags
& PARSE_FLAG_ASM_FILE
)
3692 | PARSE_FLAG_LINEFEED
3694 | PARSE_FLAG_ACCEPT_STRAYS
3696 define_start
= define_stack
;
3698 /* Credits to Fabrice Bellard's initial revision to demonstrate its
3699 capability to compile and run itself, provided all numbers are
3700 given as decimals. tcc -E -P10 will do. */
3701 if (s1
->Pflag
== 1 + 10)
3702 parse_flags
|= PARSE_FLAG_TOK_NUM
, s1
->Pflag
= 1;
3705 /* for PP benchmarks */
3706 do next(); while (tok
!= TOK_EOF
); return 0;
3709 if (s1
->dflag
& 1) {
3710 pp_debug_builtins(s1
);
3714 token_seen
= TOK_LINEFEED
, spcs
= 0;
3715 pp_line(s1
, file
, 0);
3718 iptr
= s1
->include_stack_ptr
;
3722 level
= s1
->include_stack_ptr
- iptr
;
3725 pp_line(s1
, *iptr
, 0);
3726 pp_line(s1
, file
, level
);
3730 pp_debug_defines(s1
);
3735 if (token_seen
== TOK_LINEFEED
) {
3740 if (tok
== TOK_LINEFEED
) {
3744 pp_line(s1
, file
, 0);
3745 } else if (tok
== TOK_LINEFEED
) {
3749 tokstr
= get_tok_str(tok
, &tokc
);
3750 if (!spcs
&& need_space(token_seen
, tok
, tokstr
))
3753 fputs(" ", s1
->ppfp
), --spcs
;
3754 fputs(tokstr
, s1
->ppfp
);
3758 /* reset define stack, but keep -D and built-ins */
3759 free_defines(define_start
);
3763 /* ------------------------------------------------------------------------- */