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
;
53 static void tok_print(const char *msg
, const int *str
);
55 /* isidnum_table flags: */
60 static TokenString
*macro_stack
;
62 static const char tcc_keywords
[] =
63 #define DEF(id, str) str "\0"
68 /* WARNING: the content of this string encodes token numbers */
69 static const unsigned char tok_two_chars
[] =
71 "<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253"
72 "-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\313..\250##\266";
93 '.','.', 0xa8, // C++ token ?
94 '#','#', TOK_TWOSHARPS
,
98 static void next_nomacro_spc(void);
100 ST_FUNC
void skip(int c
)
103 tcc_error("'%c' expected (got \"%s\")", c
, get_tok_str(tok
, &tokc
));
107 ST_FUNC
void expect(const char *msg
)
109 tcc_error("%s expected", msg
);
112 ST_FUNC
void begin_macro(TokenString
*str
, int alloc
)
115 str
->prev
= macro_stack
;
116 str
->prev_ptr
= macro_ptr
;
117 macro_ptr
= str
->str
;
121 ST_FUNC
void end_macro(void)
123 TokenString
*str
= macro_stack
;
124 macro_stack
= str
->prev
;
125 macro_ptr
= str
->prev_ptr
;
126 if (str
->alloc
== 2) {
127 str
->alloc
= 3; /* just mark as finished */
129 tok_str_free(str
->str
);
135 ST_FUNC
char *trimfront(char *p
)
137 while (*p
&& (unsigned char)*p
<= ' ')
142 ST_FUNC
char *trimback(char *a
, char *e
)
144 while (e
> a
&& (unsigned char)e
[-1] <= ' ')
150 /* ------------------------------------------------------------------------- */
151 /* Custom allocator for tiny objects */
153 ST_FUNC TinyAlloc
*tal_new(TinyAlloc
**pal
, size_t limit
, size_t size
)
155 TinyAlloc
*al
= tcc_mallocz(sizeof(TinyAlloc
));
156 al
->p
= al
->buffer
= tcc_malloc(size
);
163 ST_FUNC
void tal_delete(TinyAlloc
*al
)
171 fprintf(stderr
, "limit=%5d, size=%5g MB, nb_peak=%6d, nb_total=%8d, nb_missed=%6d, usage=%5.1f%%\n",
172 al
->limit
, al
->size
/ 1024.0 / 1024.0, al
->nb_peak
, al
->nb_total
, al
->nb_missed
,
173 (al
->peak_p
- al
->buffer
) * 100.0 / al
->size
);
176 if (al
->nb_allocs
> 0) {
177 fprintf(stderr
, "TAL_DEBUG: mem leak %d chunks (limit= %d)\n",
178 al
->nb_allocs
, al
->limit
);
179 uint8_t *p
= al
->buffer
;
181 tal_header_t
*header
= (tal_header_t
*)p
;
182 if (header
->line_num
> 0) {
183 fprintf(stderr
, " file %s, line %u: %u bytes\n",
184 header
->file_name
, header
->line_num
, header
->size
);
186 p
+= header
->size
+ sizeof(tal_header_t
);
191 tcc_free(al
->buffer
);
197 ST_FUNC
void tal_free_impl(TinyAlloc
*al
, void *p TAL_DEBUG_PARAMS
)
202 if (al
->buffer
<= (uint8_t *)p
&& (uint8_t *)p
< al
->buffer
+ al
->size
) {
204 tal_header_t
*header
= (((tal_header_t
*)p
) - 1);
205 if (header
->line_num
< 0) {
206 fprintf(stderr
, "TAL_DEBUG: file %s, line %u double frees chunk from\n",
208 fprintf(stderr
, " file %s, line %u: %u bytes\n",
209 header
->file_name
, -header
->line_num
, header
->size
);
211 header
->line_num
= -header
->line_num
;
216 } else if (al
->next
) {
224 ST_FUNC
void *tal_realloc_impl(TinyAlloc
**pal
, void *p
, size_t size TAL_DEBUG_PARAMS
)
226 tal_header_t
*header
;
229 size_t adj_size
= (size
+ 3) & -4;
230 TinyAlloc
*al
= *pal
;
233 is_own
= (al
->buffer
<= (uint8_t *)p
&& (uint8_t *)p
< al
->buffer
+ al
->size
);
234 if ((!p
|| is_own
) && size
<= al
->limit
) {
235 if (al
->p
+ adj_size
+ sizeof(tal_header_t
) < al
->buffer
+ al
->size
) {
236 header
= (tal_header_t
*)al
->p
;
237 header
->size
= adj_size
;
239 int ofs
= strlen(file
) - TAL_DEBUG_FILE_LEN
;
240 strncpy(header
->file_name
, file
+ (ofs
> 0 ? ofs
: 0), TAL_DEBUG_FILE_LEN
);
241 header
->file_name
[TAL_DEBUG_FILE_LEN
] = 0;
242 header
->line_num
= line
;
244 ret
= al
->p
+ sizeof(tal_header_t
);
245 al
->p
+= adj_size
+ sizeof(tal_header_t
);
247 header
= (((tal_header_t
*)p
) - 1);
248 memcpy(ret
, p
, header
->size
);
250 header
->line_num
= -header
->line_num
;
256 if (al
->nb_peak
< al
->nb_allocs
)
257 al
->nb_peak
= al
->nb_allocs
;
258 if (al
->peak_p
< al
->p
)
265 ret
= tal_realloc(*pal
, 0, size
);
266 header
= (((tal_header_t
*)p
) - 1);
267 memcpy(ret
, p
, header
->size
);
269 header
->line_num
= -header
->line_num
;
276 TinyAlloc
*bottom
= al
, *next
= al
->top
? al
->top
: al
;
278 al
= tal_new(pal
, next
->limit
, next
->size
* 2);
286 ret
= tcc_malloc(size
);
287 header
= (((tal_header_t
*)p
) - 1);
288 memcpy(ret
, p
, header
->size
);
290 header
->line_num
= -header
->line_num
;
292 } else if (al
->next
) {
296 ret
= tcc_realloc(p
, size
);
303 /* ------------------------------------------------------------------------- */
304 /* CString handling */
305 static void cstr_realloc(CString
*cstr
, int new_size
)
310 size
= cstr
->size_allocated
;
312 size
= 8; /* no need to allocate a too small first string */
313 while (size
< new_size
)
315 data
= tal_realloc(cstr_alloc
, cstr
->data_allocated
, size
);
316 cstr
->data_allocated
= data
;
317 cstr
->size_allocated
= size
;
322 ST_FUNC
void cstr_ccat(CString
*cstr
, int ch
)
325 size
= cstr
->size
+ 1;
326 if (size
> cstr
->size_allocated
)
327 cstr_realloc(cstr
, size
);
328 ((unsigned char *)cstr
->data
)[size
- 1] = ch
;
332 ST_FUNC
void cstr_cat(CString
*cstr
, const char *str
, int len
)
336 len
= strlen(str
) + 1 + len
;
337 size
= cstr
->size
+ len
;
338 if (size
> cstr
->size_allocated
)
339 cstr_realloc(cstr
, size
);
340 memmove(((unsigned char *)cstr
->data
) + cstr
->size
, str
, len
);
344 /* add a wide char */
345 ST_FUNC
void cstr_wccat(CString
*cstr
, int ch
)
348 size
= cstr
->size
+ sizeof(nwchar_t
);
349 if (size
> cstr
->size_allocated
)
350 cstr_realloc(cstr
, size
);
351 *(nwchar_t
*)(((unsigned char *)cstr
->data
) + size
- sizeof(nwchar_t
)) = ch
;
355 ST_FUNC
void cstr_new(CString
*cstr
)
357 memset(cstr
, 0, sizeof(CString
));
360 /* free string and reset it to NULL */
361 ST_FUNC
void cstr_free(CString
*cstr
)
363 tal_free(cstr_alloc
, cstr
->data_allocated
);
367 /* reset string to empty */
368 ST_FUNC
void cstr_reset(CString
*cstr
)
374 static void add_char(CString
*cstr
, int c
)
376 if (c
== '\'' || c
== '\"' || c
== '\\') {
377 /* XXX: could be more precise if char or string */
378 cstr_ccat(cstr
, '\\');
380 if (c
>= 32 && c
<= 126) {
383 cstr_ccat(cstr
, '\\');
385 cstr_ccat(cstr
, 'n');
387 cstr_ccat(cstr
, '0' + ((c
>> 6) & 7));
388 cstr_ccat(cstr
, '0' + ((c
>> 3) & 7));
389 cstr_ccat(cstr
, '0' + (c
& 7));
394 /* ------------------------------------------------------------------------- */
395 /* allocate a new token */
396 static TokenSym
*tok_alloc_new(TokenSym
**pts
, const char *str
, int len
)
398 TokenSym
*ts
, **ptable
;
401 if (tok_ident
>= SYM_FIRST_ANOM
)
402 tcc_error("memory full (symbols)");
404 /* expand token table if needed */
405 i
= tok_ident
- TOK_IDENT
;
406 if ((i
% TOK_ALLOC_INCR
) == 0) {
407 ptable
= tcc_realloc(table_ident
, (i
+ TOK_ALLOC_INCR
) * sizeof(TokenSym
*));
408 table_ident
= ptable
;
411 ts
= tal_realloc(toksym_alloc
, 0, sizeof(TokenSym
) + len
);
413 ts
->tok
= tok_ident
++;
414 ts
->sym_define
= NULL
;
415 ts
->sym_label
= NULL
;
416 ts
->sym_struct
= NULL
;
417 ts
->sym_identifier
= NULL
;
419 ts
->hash_next
= NULL
;
420 memcpy(ts
->str
, str
, len
);
426 #define TOK_HASH_INIT 1
427 #define TOK_HASH_FUNC(h, c) ((h) + ((h) << 5) + ((h) >> 27) + (c))
430 /* find a token and add it if not found */
431 ST_FUNC TokenSym
*tok_alloc(const char *str
, int len
)
439 h
= TOK_HASH_FUNC(h
, ((unsigned char *)str
)[i
]);
440 h
&= (TOK_HASH_SIZE
- 1);
442 pts
= &hash_ident
[h
];
447 if (ts
->len
== len
&& !memcmp(ts
->str
, str
, len
))
449 pts
= &(ts
->hash_next
);
451 return tok_alloc_new(pts
, str
, len
);
454 /* XXX: buffer overflow */
455 /* XXX: float tokens */
456 ST_FUNC
const char *get_tok_str(int v
, CValue
*cv
)
461 cstr_reset(&cstr_buf
);
467 /* XXX: not quite exact, but only useful for testing */
468 sprintf(p
, "%llu", (unsigned long long)cv
->i
);
472 /* XXX: not quite exact, but only useful for testing */
474 sprintf(p
, "%u", (unsigned)cv
->i
);
476 sprintf(p
, "%llu", (unsigned long long)cv
->i
);
480 cstr_ccat(&cstr_buf
, 'L');
482 cstr_ccat(&cstr_buf
, '\'');
483 add_char(&cstr_buf
, cv
->i
);
484 cstr_ccat(&cstr_buf
, '\'');
485 cstr_ccat(&cstr_buf
, '\0');
489 return (char*)cv
->str
.data
;
491 cstr_ccat(&cstr_buf
, 'L');
493 cstr_ccat(&cstr_buf
, '\"');
495 len
= cv
->str
.size
- 1;
497 add_char(&cstr_buf
, ((unsigned char *)cv
->str
.data
)[i
]);
499 len
= (cv
->str
.size
/ sizeof(nwchar_t
)) - 1;
501 add_char(&cstr_buf
, ((nwchar_t
*)cv
->str
.data
)[i
]);
503 cstr_ccat(&cstr_buf
, '\"');
504 cstr_ccat(&cstr_buf
, '\0');
508 cstr_cat(&cstr_buf
, "<float>", 0);
511 cstr_cat(&cstr_buf
, "<double>", 0);
514 cstr_cat(&cstr_buf
, "<long double>", 0);
517 cstr_cat(&cstr_buf
, "<linenumber>", 0);
520 /* above tokens have value, the ones below don't */
529 return strcpy(p
, "...");
531 return strcpy(p
, "<<=");
533 return strcpy(p
, ">>=");
536 /* search in two bytes table */
537 const unsigned char *q
= tok_two_chars
;
543 return cstr_buf
.data
;
548 sprintf(cstr_buf
.data
, "<%02x>", v
);
549 return cstr_buf
.data
;
554 } else if (v
< tok_ident
) {
555 return table_ident
[v
- TOK_IDENT
]->str
;
556 } else if (v
>= SYM_FIRST_ANOM
) {
557 /* special name for anonymous symbol */
558 sprintf(p
, "L.%u", v
- SYM_FIRST_ANOM
);
560 /* should never happen */
565 return cstr_buf
.data
;
568 /* return the current character, handling end of block if necessary
570 ST_FUNC
int handle_eob(void)
572 BufferedFile
*bf
= file
;
575 /* only tries to read if really end of buffer */
576 if (bf
->buf_ptr
>= bf
->buf_end
) {
578 #if defined(PARSE_DEBUG)
583 len
= read(bf
->fd
, bf
->buffer
, len
);
590 bf
->buf_ptr
= bf
->buffer
;
591 bf
->buf_end
= bf
->buffer
+ len
;
592 *bf
->buf_end
= CH_EOB
;
594 if (bf
->buf_ptr
< bf
->buf_end
) {
595 return bf
->buf_ptr
[0];
597 bf
->buf_ptr
= bf
->buf_end
;
602 /* read next char from current input file and handle end of input buffer */
603 ST_INLN
void inp(void)
605 ch
= *(++(file
->buf_ptr
));
606 /* end of buffer/file handling */
611 /* handle '\[\r]\n' */
612 static int handle_stray_noerror(void)
619 } else if (ch
== '\r') {
633 static void handle_stray(void)
635 if (handle_stray_noerror())
636 tcc_error("stray '\\' in program");
639 /* skip the stray and handle the \\n case. Output an error if
640 incorrect char after the stray */
641 static int handle_stray1(uint8_t *p
)
646 if (p
>= file
->buf_end
) {
653 if (handle_stray_noerror()) {
654 if (!(parse_flags
& PARSE_FLAG_ACCEPT_STRAYS
))
655 tcc_error("stray '\\' in program");
656 *--file
->buf_ptr
= '\\';
663 /* handle just the EOB case, but not stray */
664 #define PEEKC_EOB(c, p)\
675 /* handle the complicated stray case */
681 c = handle_stray1(p);\
686 /* input with '\[\r]\n' handling. Note that this function cannot
687 handle other characters after '\', so you cannot call it inside
688 strings or comments */
689 ST_FUNC
void minp(void)
696 /* single line C++ comments */
697 static uint8_t *parse_line_comment(uint8_t *p
)
705 if (c
== '\n' || c
== CH_EOF
) {
707 } else if (c
== '\\') {
716 } else if (c
== '\r') {
734 ST_FUNC
uint8_t *parse_comment(uint8_t *p
)
743 if (c
== '\n' || c
== '*' || c
== '\\')
747 if (c
== '\n' || c
== '*' || c
== '\\')
751 /* now we can handle all the cases */
755 } else if (c
== '*') {
761 } else if (c
== '/') {
763 } else if (c
== '\\') {
768 tcc_error("unexpected end of file in comment");
770 /* skip '\[\r]\n', otherwise just skip the stray */
776 } else if (c
== '\r') {
793 /* stray, eob or eof */
798 tcc_error("unexpected end of file in comment");
799 } else if (c
== '\\') {
811 static inline void skip_spaces(void)
813 while (isidnum_table
[ch
- CH_EOF
] & IS_SPC
)
817 static inline int check_space(int t
, int *spc
)
819 if (t
< 256 && (isidnum_table
[t
- CH_EOF
] & IS_SPC
)) {
828 /* parse a string without interpreting escapes */
829 static uint8_t *parse_pp_string(uint8_t *p
,
830 int sep
, CString
*str
)
838 } else if (c
== '\\') {
844 /* XXX: indicate line number of start of string */
845 tcc_error("missing terminating %c character", sep
);
846 } else if (c
== '\\') {
847 /* escape : just skip \[\r]\n */
852 } else if (c
== '\r') {
855 expect("'\n' after '\r'");
858 } else if (c
== CH_EOF
) {
859 goto unterminated_string
;
862 cstr_ccat(str
, '\\');
868 } else if (c
== '\n') {
871 } else if (c
== '\r') {
875 cstr_ccat(str
, '\r');
891 /* skip block of text until #else, #elif or #endif. skip also pairs of
893 static void preprocess_skip(void)
895 int a
, start_of_line
, c
, in_warn_or_error
;
902 in_warn_or_error
= 0;
923 } else if (c
== '\\') {
924 ch
= file
->buf_ptr
[0];
925 handle_stray_noerror();
932 if (in_warn_or_error
)
934 p
= parse_pp_string(p
, c
, NULL
);
938 if (in_warn_or_error
)
945 p
= parse_comment(p
);
946 } else if (ch
== '/') {
947 p
= parse_line_comment(p
);
957 (tok
== TOK_ELSE
|| tok
== TOK_ELIF
|| tok
== TOK_ENDIF
))
959 if (tok
== TOK_IF
|| tok
== TOK_IFDEF
|| tok
== TOK_IFNDEF
)
961 else if (tok
== TOK_ENDIF
)
963 else if( tok
== TOK_ERROR
|| tok
== TOK_WARNING
)
964 in_warn_or_error
= 1;
965 else if (tok
== TOK_LINEFEED
)
967 else if (parse_flags
& PARSE_FLAG_ASM_FILE
)
968 p
= parse_line_comment(p
);
969 } else if (parse_flags
& PARSE_FLAG_ASM_FILE
)
970 p
= parse_line_comment(p
);
983 /* ParseState handling */
985 /* XXX: currently, no include file info is stored. Thus, we cannot display
986 accurate messages if the function or data definition spans multiple
989 /* save current parse state in 's' */
990 ST_FUNC
void save_parse_state(ParseState
*s
)
992 s
->line_num
= file
->line_num
;
993 s
->macro_ptr
= macro_ptr
;
998 /* restore parse state from 's' */
999 ST_FUNC
void restore_parse_state(ParseState
*s
)
1001 file
->line_num
= s
->line_num
;
1002 macro_ptr
= s
->macro_ptr
;
1007 /* return the number of additional 'ints' necessary to store the
1009 static inline int tok_size(const int *p
)
1024 return 1 + ((sizeof(CString
) + ((CString
*)(p
+1))->size
+ 3) >> 2);
1030 return 1 + LDOUBLE_SIZE
/ 4;
1036 /* token string handling */
1038 ST_INLN
void tok_str_new(TokenString
*s
)
1042 s
->allocated_len
= 0;
1043 s
->last_line_num
= -1;
1046 ST_FUNC
int *tok_str_dup(TokenString
*s
)
1050 str
= tal_realloc(tokstr_alloc
, 0, s
->len
* sizeof(int));
1051 memcpy(str
, s
->str
, s
->len
* sizeof(int));
1055 ST_FUNC
void tok_str_free(int *str
)
1057 tal_free(tokstr_alloc
, str
);
1060 ST_FUNC
int *tok_str_realloc(TokenString
*s
, int new_size
)
1064 size
= s
->allocated_len
;
1067 while (size
< new_size
)
1069 TCC_ASSERT((size
& (size
-1)) == 0);
1070 if (size
> s
->allocated_len
) {
1071 str
= tal_realloc(tokstr_alloc
, s
->str
, size
* sizeof(int));
1072 s
->allocated_len
= size
;
1078 ST_FUNC
void tok_str_add(TokenString
*s
, int t
)
1084 if (len
>= s
->allocated_len
)
1085 str
= tok_str_realloc(s
, len
+ 1);
1090 static void tok_str_add2(TokenString
*s
, int t
, CValue
*cv
)
1097 /* allocate space for worst case */
1098 if (len
+ TOK_MAX_SIZE
>= s
->allocated_len
)
1099 str
= tok_str_realloc(s
, len
+ TOK_MAX_SIZE
+ 1);
1108 str
[len
++] = cv
->tab
[0];
1115 /* Insert the string into the int array. */
1117 1 + (cv
->str
.size
+ sizeof(int) - 1) / sizeof(int);
1118 if (len
+ nb_words
>= s
->allocated_len
)
1119 str
= tok_str_realloc(s
, len
+ nb_words
+ 1);
1120 str
[len
] = cv
->str
.size
;
1121 memcpy(&str
[len
+ 1], cv
->str
.data
, cv
->str
.size
);
1128 #if LDOUBLE_SIZE == 8
1131 str
[len
++] = cv
->tab
[0];
1132 str
[len
++] = cv
->tab
[1];
1134 #if LDOUBLE_SIZE == 12
1136 str
[len
++] = cv
->tab
[0];
1137 str
[len
++] = cv
->tab
[1];
1138 str
[len
++] = cv
->tab
[2];
1139 #elif LDOUBLE_SIZE == 16
1141 str
[len
++] = cv
->tab
[0];
1142 str
[len
++] = cv
->tab
[1];
1143 str
[len
++] = cv
->tab
[2];
1144 str
[len
++] = cv
->tab
[3];
1145 #elif LDOUBLE_SIZE != 8
1146 #error add long double size support
1155 /* add the current parse token in token string 's' */
1156 ST_FUNC
void tok_str_add_tok(TokenString
*s
)
1160 /* save line number info */
1161 if (file
->line_num
!= s
->last_line_num
) {
1162 s
->last_line_num
= file
->line_num
;
1163 cval
.i
= s
->last_line_num
;
1164 tok_str_add2(s
, TOK_LINENUM
, &cval
);
1166 tok_str_add2(s
, tok
, &tokc
);
1169 /* get a token from an integer array and increment pointer
1170 accordingly. we code it as a macro to avoid pointer aliasing. */
1171 static inline void TOK_GET(int *t
, const int **pp
, CValue
*cv
)
1190 cv
->str
.size
= *p
++;
1192 cv
->str
.data_allocated
= 0;
1193 p
+= (cv
->str
.size
+ sizeof(int) - 1) / sizeof(int);
1201 #if LDOUBLE_SIZE == 16
1203 #elif LDOUBLE_SIZE == 12
1205 #elif LDOUBLE_SIZE == 8
1208 # error add long double size support
1221 /* Calling this function is expensive, but it is not possible
1222 to read a token string backwards. */
1223 static int tok_last(const int *str0
, const int *str1
)
1225 const int *str
= str0
;
1230 TOK_GET(&tok
, &str
, &cval
);
1234 static int macro_is_equal(const int *a
, const int *b
)
1243 /* first time preallocate static cstr_buf, next time only reset position to start */
1244 cstr_reset(&cstr_buf
);
1245 TOK_GET(&t
, &a
, &cv
);
1246 cstr_cat(&cstr_buf
, get_tok_str(t
, &cv
), 0);
1247 TOK_GET(&t
, &b
, &cv
);
1248 if (strcmp(cstr_buf
.data
, get_tok_str(t
, &cv
)))
1254 /* defines handling */
1255 ST_INLN
void define_push(int v
, int macro_type
, TokenString
*str
, Sym
*first_arg
)
1260 s
= sym_push2(&define_stack
, v
, macro_type
, 0);
1261 s
->d
= str
? tok_str_dup(str
) : NULL
;
1262 s
->next
= first_arg
;
1263 table_ident
[v
- TOK_IDENT
]->sym_define
= s
;
1265 if (o
&& !macro_is_equal(o
->d
, s
->d
))
1266 tcc_warning("%s redefined", get_tok_str(v
, NULL
));
1269 /* undefined a define symbol. Its name is just set to zero */
1270 ST_FUNC
void define_undef(Sym
*s
)
1273 if (v
>= TOK_IDENT
&& v
< tok_ident
)
1274 table_ident
[v
- TOK_IDENT
]->sym_define
= NULL
;
1277 ST_INLN Sym
*define_find(int v
)
1280 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
1282 return table_ident
[v
]->sym_define
;
1285 /* free define stack until top reaches 'b' */
1286 ST_FUNC
void free_defines(Sym
*b
)
1294 /* do not free args or predefined defines */
1296 tok_str_free(top
->d
);
1298 if (v
>= TOK_IDENT
&& v
< tok_ident
)
1299 table_ident
[v
- TOK_IDENT
]->sym_define
= NULL
;
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 inline int hash_cached_include(const char *filename
)
1480 const unsigned char *s
;
1484 s
= (unsigned char *) filename
;
1486 h
= TOK_HASH_FUNC(h
, *s
);
1489 h
&= (CACHED_INCLUDES_HASH_SIZE
- 1);
1493 static CachedInclude
*search_cached_include(TCCState
*s1
, const char *filename
)
1497 h
= hash_cached_include(filename
);
1498 i
= s1
->cached_includes_hash
[h
];
1502 e
= s1
->cached_includes
[i
- 1];
1503 if (0 == PATHCMP(e
->filename
, filename
))
1510 static inline void add_cached_include(TCCState
*s1
, const char *filename
, int ifndef_macro
)
1515 if (search_cached_include(s1
, filename
))
1518 printf("adding cached '%s' %s\n", filename
, get_tok_str(ifndef_macro
, NULL
));
1520 e
= tcc_malloc(sizeof(CachedInclude
) + strlen(filename
));
1521 strcpy(e
->filename
, filename
);
1522 e
->ifndef_macro
= ifndef_macro
;
1523 dynarray_add((void ***)&s1
->cached_includes
, &s1
->nb_cached_includes
, e
);
1524 /* add in hash table */
1525 h
= hash_cached_include(filename
);
1526 e
->hash_next
= s1
->cached_includes_hash
[h
];
1527 s1
->cached_includes_hash
[h
] = s1
->nb_cached_includes
;
1530 #define ONCE_PREFIX "#ONCE#"
1532 static void pragma_parse(TCCState
*s1
)
1535 if (tok
== TOK_push_macro
|| tok
== TOK_pop_macro
) {
1539 if (next(), tok
!= '(')
1541 if (next(), tok
!= TOK_STR
)
1543 v
= tok_alloc(tokc
.str
.data
, tokc
.str
.size
- 1)->tok
;
1544 if (next(), tok
!= ')')
1546 if (t
== TOK_push_macro
) {
1547 while (NULL
== (s
= define_find(v
)))
1548 define_push(v
, 0, NULL
, NULL
);
1549 s
->type
.ref
= s
; /* set push boundary */
1551 for (s
= define_stack
; s
; s
= s
->prev
)
1552 if (s
->v
== v
&& s
->type
.ref
== s
) {
1558 table_ident
[v
- TOK_IDENT
]->sym_define
= s
->d
? s
: NULL
;
1560 tcc_warning("unbalanced #pragma pop_macro");
1561 pp_debug_tok
= t
, pp_debug_symv
= v
;
1563 } else if (tok
== TOK_once
) {
1564 char buf1
[sizeof(file
->filename
) + sizeof(ONCE_PREFIX
)];
1565 strcpy(buf1
, ONCE_PREFIX
);
1566 strcat(buf1
, file
->filename
);
1570 add_cached_include(s1
, file
->filename
, tok_alloc(buf1
, strlen(buf1
))->tok
);
1571 } else if (s1
->ppfp
) {
1572 /* tcc -E: keep pragmas below unchanged */
1574 unget_tok(TOK_PRAGMA
);
1576 unget_tok(TOK_LINEFEED
);
1578 } else if (tok
== TOK_pack
) {
1580 #pragma pack(1) // set
1581 #pragma pack() // reset to default
1582 #pragma pack(push,1) // push & set
1583 #pragma pack(pop) // restore previous */
1586 if (tok
== TOK_ASM_pop
) {
1588 if (s1
->pack_stack_ptr
<= s1
->pack_stack
) {
1590 tcc_error("out of pack stack");
1592 s1
->pack_stack_ptr
--;
1596 if (tok
== TOK_ASM_push
) {
1598 if (s1
->pack_stack_ptr
>= s1
->pack_stack
+ PACK_STACK_SIZE
- 1)
1600 s1
->pack_stack_ptr
++;
1603 if (tok
!= TOK_CINT
)
1606 if (val
< 1 || val
> 16 || (val
& (val
- 1)) != 0)
1610 *s1
->pack_stack_ptr
= val
;
1615 } else if (tok
== TOK_comment
) {
1625 file
= tcc_strdup((char *)tokc
.str
.data
);
1626 dynarray_add((void ***)&s1
->pragma_libs
, &s1
->nb_pragma_libs
, file
);
1632 if (s1
->warn_unsupported
)
1633 tcc_warning("#pragma %s is ignored", get_tok_str(tok
, &tokc
));
1638 tcc_error("malformed #pragma directive");
1642 /* is_bof is true if first non space token at beginning of file */
1643 ST_FUNC
void preprocess(int is_bof
)
1645 TCCState
*s1
= tcc_state
;
1646 int i
, c
, n
, saved_parse_flags
;
1650 saved_parse_flags
= parse_flags
;
1651 parse_flags
= PARSE_FLAG_PREPROCESS
1652 | PARSE_FLAG_TOK_NUM
1653 | PARSE_FLAG_TOK_STR
1654 | PARSE_FLAG_LINEFEED
1655 | (parse_flags
& PARSE_FLAG_ASM_FILE
)
1664 pp_debug_symv
= tok
;
1670 pp_debug_symv
= tok
;
1671 s
= define_find(tok
);
1672 /* undefine symbol by putting an invalid name */
1677 case TOK_INCLUDE_NEXT
:
1678 ch
= file
->buf_ptr
[0];
1679 /* XXX: incorrect if comments : use next_nomacro with a special mode */
1684 } else if (ch
== '\"') {
1689 while (ch
!= c
&& ch
!= '\n' && ch
!= CH_EOF
) {
1690 if ((q
- buf
) < sizeof(buf
) - 1)
1693 if (handle_stray_noerror() == 0)
1701 /* eat all spaces and comments after include */
1702 /* XXX: slightly incorrect */
1703 while (ch1
!= '\n' && ch1
!= CH_EOF
)
1707 /* computed #include : either we have only strings or
1708 we have anything enclosed in '<>' */
1711 if (tok
== TOK_STR
) {
1712 while (tok
!= TOK_LINEFEED
) {
1713 if (tok
!= TOK_STR
) {
1715 tcc_error("'#include' expects \"FILENAME\" or <FILENAME>");
1717 pstrcat(buf
, sizeof(buf
), (char *)tokc
.str
.data
);
1723 while (tok
!= TOK_LINEFEED
) {
1724 pstrcat(buf
, sizeof(buf
), get_tok_str(tok
, &tokc
));
1728 /* check syntax and remove '<>' */
1729 if (len
< 2 || buf
[0] != '<' || buf
[len
- 1] != '>')
1730 goto include_syntax
;
1731 memmove(buf
, buf
+ 1, len
- 2);
1732 buf
[len
- 2] = '\0';
1737 if (s1
->include_stack_ptr
>= s1
->include_stack
+ INCLUDE_STACK_SIZE
)
1738 tcc_error("#include recursion too deep");
1739 /* store current file in stack, but increment stack later below */
1740 *s1
->include_stack_ptr
= file
;
1741 i
= tok
== TOK_INCLUDE_NEXT
? file
->include_next_index
: 0;
1742 n
= 2 + s1
->nb_include_paths
+ s1
->nb_sysinclude_paths
;
1743 for (; i
< n
; ++i
) {
1744 char buf1
[sizeof file
->filename
];
1749 /* check absolute include path */
1750 if (!IS_ABSPATH(buf
))
1754 } else if (i
== 1) {
1755 /* search in current dir if "header.h" */
1758 path
= file
->filename
;
1759 pstrncpy(buf1
, path
, tcc_basename(path
) - path
);
1762 /* search in all the include paths */
1763 int j
= i
- 2, k
= j
- s1
->nb_include_paths
;
1764 path
= k
< 0 ? s1
->include_paths
[j
] : s1
->sysinclude_paths
[k
];
1765 if (path
== 0) continue;
1766 pstrcpy(buf1
, sizeof(buf1
), path
);
1767 pstrcat(buf1
, sizeof(buf1
), "/");
1770 pstrcat(buf1
, sizeof(buf1
), buf
);
1771 e
= search_cached_include(s1
, buf1
);
1772 if (e
&& define_find(e
->ifndef_macro
)) {
1773 /* no need to parse the include because the 'ifndef macro'
1776 printf("%s: skipping cached %s\n", file
->filename
, buf1
);
1781 if (tcc_open(s1
, buf1
) < 0)
1784 file
->include_next_index
= i
+ 1;
1786 printf("%s: including %s\n", file
->prev
->filename
, file
->filename
);
1788 /* update target deps */
1789 dynarray_add((void ***)&s1
->target_deps
, &s1
->nb_target_deps
,
1791 /* push current file in stack */
1792 ++s1
->include_stack_ptr
;
1793 /* add include file debug info */
1795 put_stabs(file
->filename
, N_BINCL
, 0, 0, 0);
1796 tok_flags
|= TOK_FLAG_BOF
| TOK_FLAG_BOL
;
1797 ch
= file
->buf_ptr
[0];
1800 tcc_error("include file '%s' not found", buf
);
1807 c
= expr_preprocess();
1813 if (tok
< TOK_IDENT
)
1814 tcc_error("invalid argument for '#if%sdef'", c
? "n" : "");
1818 printf("#ifndef %s\n", get_tok_str(tok
, NULL
));
1820 file
->ifndef_macro
= tok
;
1823 c
= (define_find(tok
) != 0) ^ c
;
1825 if (s1
->ifdef_stack_ptr
>= s1
->ifdef_stack
+ IFDEF_STACK_SIZE
)
1826 tcc_error("memory full (ifdef)");
1827 *s1
->ifdef_stack_ptr
++ = c
;
1830 if (s1
->ifdef_stack_ptr
== s1
->ifdef_stack
)
1831 tcc_error("#else without matching #if");
1832 if (s1
->ifdef_stack_ptr
[-1] & 2)
1833 tcc_error("#else after #else");
1834 c
= (s1
->ifdef_stack_ptr
[-1] ^= 3);
1837 if (s1
->ifdef_stack_ptr
== s1
->ifdef_stack
)
1838 tcc_error("#elif without matching #if");
1839 c
= s1
->ifdef_stack_ptr
[-1];
1841 tcc_error("#elif after #else");
1842 /* last #if/#elif expression was true: we skip */
1845 c
= expr_preprocess();
1846 s1
->ifdef_stack_ptr
[-1] = c
;
1848 if (s1
->ifdef_stack_ptr
== file
->ifdef_stack_ptr
+ 1)
1849 file
->ifndef_macro
= 0;
1859 if (s1
->ifdef_stack_ptr
<= file
->ifdef_stack_ptr
)
1860 tcc_error("#endif without matching #if");
1861 s1
->ifdef_stack_ptr
--;
1862 /* '#ifndef macro' was at the start of file. Now we check if
1863 an '#endif' is exactly at the end of file */
1864 if (file
->ifndef_macro
&&
1865 s1
->ifdef_stack_ptr
== file
->ifdef_stack_ptr
) {
1866 file
->ifndef_macro_saved
= file
->ifndef_macro
;
1867 /* need to set to zero to avoid false matches if another
1868 #ifndef at middle of file */
1869 file
->ifndef_macro
= 0;
1870 while (tok
!= TOK_LINEFEED
)
1872 tok_flags
|= TOK_FLAG_ENDIF
;
1877 n
= strtoul((char*)tokc
.str
.data
, &q
, 10);
1881 if (tok
!= TOK_CINT
)
1883 tcc_error("wrong #line format");
1887 if (tok
!= TOK_LINEFEED
) {
1889 pstrcpy(file
->filename
, sizeof(file
->filename
), (char *)tokc
.str
.data
);
1890 else if (parse_flags
& PARSE_FLAG_ASM_FILE
)
1897 total_lines
+= file
->line_num
- n
;
1900 put_stabs(file
->filename
, N_BINCL
, 0, 0, 0);
1905 ch
= file
->buf_ptr
[0];
1908 while (ch
!= '\n' && ch
!= CH_EOF
) {
1909 if ((q
- buf
) < sizeof(buf
) - 1)
1912 if (handle_stray_noerror() == 0)
1919 tcc_error("#error %s", buf
);
1921 tcc_warning("#warning %s", buf
);
1929 /* ignore gas line comment in an 'S' file. */
1930 if (saved_parse_flags
& PARSE_FLAG_ASM_FILE
)
1932 if (tok
== '!' && is_bof
)
1933 /* '!' is ignored at beginning to allow C scripts. */
1935 tcc_warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok
, &tokc
));
1937 file
->buf_ptr
= parse_line_comment(file
->buf_ptr
);
1940 /* ignore other preprocess commands or #! for C scripts */
1941 while (tok
!= TOK_LINEFEED
)
1944 parse_flags
= saved_parse_flags
;
1947 /* evaluate escape codes in a string. */
1948 static void parse_escape_string(CString
*outstr
, const uint8_t *buf
, int is_long
)
1963 case '0': case '1': case '2': case '3':
1964 case '4': case '5': case '6': case '7':
1965 /* at most three octal digits */
1970 n
= n
* 8 + c
- '0';
1974 n
= n
* 8 + c
- '0';
1979 goto add_char_nonext
;
1987 if (c
>= 'a' && c
<= 'f')
1989 else if (c
>= 'A' && c
<= 'F')
1999 goto add_char_nonext
;
2023 goto invalid_escape
;
2033 if (c
>= '!' && c
<= '~')
2034 tcc_warning("unknown escape sequence: \'\\%c\'", c
);
2036 tcc_warning("unknown escape sequence: \'\\x%x\'", c
);
2043 cstr_ccat(outstr
, c
);
2045 cstr_wccat(outstr
, c
);
2047 /* add a trailing '\0' */
2049 cstr_ccat(outstr
, '\0');
2051 cstr_wccat(outstr
, '\0');
2054 void parse_string(const char *s
, int len
)
2056 uint8_t buf
[1000], *p
= buf
;
2059 if ((is_long
= *s
== 'L'))
2063 if (len
>= sizeof buf
)
2064 p
= tcc_malloc(len
+ 1);
2068 cstr_reset(&tokcstr
);
2069 parse_escape_string(&tokcstr
, p
, is_long
);
2075 /* XXX: make it portable */
2079 char_size
= sizeof(nwchar_t
);
2080 if (tokcstr
.size
<= char_size
)
2081 tcc_error("empty character constant");
2082 if (tokcstr
.size
> 2 * char_size
)
2083 tcc_warning("multi-character character constant");
2085 tokc
.i
= *(int8_t *)tokcstr
.data
;
2088 tokc
.i
= *(nwchar_t
*)tokcstr
.data
;
2092 tokc
.str
.size
= tokcstr
.size
;
2093 tokc
.str
.data
= tokcstr
.data
;
2094 tokc
.str
.data_allocated
= tokcstr
.data_allocated
;
2102 /* we use 64 bit numbers */
2105 /* bn = (bn << shift) | or_val */
2106 static void bn_lshift(unsigned int *bn
, int shift
, int or_val
)
2110 for(i
=0;i
<BN_SIZE
;i
++) {
2112 bn
[i
] = (v
<< shift
) | or_val
;
2113 or_val
= v
>> (32 - shift
);
2117 static void bn_zero(unsigned int *bn
)
2120 for(i
=0;i
<BN_SIZE
;i
++) {
2125 /* parse number in null terminated string 'p' and return it in the
2127 static void parse_number(const char *p
)
2129 int b
, t
, shift
, frac_bits
, s
, exp_val
, ch
;
2131 unsigned int bn
[BN_SIZE
];
2142 goto float_frac_parse
;
2143 } else if (t
== '0') {
2144 if (ch
== 'x' || ch
== 'X') {
2148 } else if (tcc_ext
&& (ch
== 'b' || ch
== 'B')) {
2154 /* parse all digits. cannot check octal numbers at this stage
2155 because of floating point constants */
2157 if (ch
>= 'a' && ch
<= 'f')
2159 else if (ch
>= 'A' && ch
<= 'F')
2167 if (q
>= token_buf
+ STRING_MAX_SIZE
) {
2169 tcc_error("number too long");
2175 ((ch
== 'e' || ch
== 'E') && b
== 10) ||
2176 ((ch
== 'p' || ch
== 'P') && (b
== 16 || b
== 2))) {
2178 /* NOTE: strtox should support that for hexa numbers, but
2179 non ISOC99 libcs do not support it, so we prefer to do
2181 /* hexadecimal or binary floats */
2182 /* XXX: handle overflows */
2194 } else if (t
>= 'a') {
2196 } else if (t
>= 'A') {
2201 bn_lshift(bn
, shift
, t
);
2208 if (t
>= 'a' && t
<= 'f') {
2210 } else if (t
>= 'A' && t
<= 'F') {
2212 } else if (t
>= '0' && t
<= '9') {
2218 tcc_error("invalid digit");
2219 bn_lshift(bn
, shift
, t
);
2224 if (ch
!= 'p' && ch
!= 'P')
2231 } else if (ch
== '-') {
2235 if (ch
< '0' || ch
> '9')
2236 expect("exponent digits");
2237 while (ch
>= '0' && ch
<= '9') {
2238 exp_val
= exp_val
* 10 + ch
- '0';
2241 exp_val
= exp_val
* s
;
2243 /* now we can generate the number */
2244 /* XXX: should patch directly float number */
2245 d
= (double)bn
[1] * 4294967296.0 + (double)bn
[0];
2246 d
= ldexp(d
, exp_val
- frac_bits
);
2251 /* float : should handle overflow */
2253 } else if (t
== 'L') {
2255 #ifdef TCC_TARGET_PE
2260 /* XXX: not large enough */
2261 tokc
.ld
= (long double)d
;
2268 /* decimal floats */
2270 if (q
>= token_buf
+ STRING_MAX_SIZE
)
2275 while (ch
>= '0' && ch
<= '9') {
2276 if (q
>= token_buf
+ STRING_MAX_SIZE
)
2282 if (ch
== 'e' || ch
== 'E') {
2283 if (q
>= token_buf
+ STRING_MAX_SIZE
)
2287 if (ch
== '-' || ch
== '+') {
2288 if (q
>= token_buf
+ STRING_MAX_SIZE
)
2293 if (ch
< '0' || ch
> '9')
2294 expect("exponent digits");
2295 while (ch
>= '0' && ch
<= '9') {
2296 if (q
>= token_buf
+ STRING_MAX_SIZE
)
2308 tokc
.f
= strtof(token_buf
, NULL
);
2309 } else if (t
== 'L') {
2311 #ifdef TCC_TARGET_PE
2313 tokc
.d
= strtod(token_buf
, NULL
);
2316 tokc
.ld
= strtold(token_buf
, NULL
);
2320 tokc
.d
= strtod(token_buf
, NULL
);
2324 unsigned long long n
, n1
;
2325 int lcount
, ucount
, must_64bit
;
2328 /* integer number */
2331 if (b
== 10 && *q
== '0') {
2338 /* no need for checks except for base 10 / 8 errors */
2348 tcc_error("invalid digit");
2351 /* detect overflow */
2352 /* XXX: this test is not reliable */
2354 tcc_error("integer constant overflow");
2357 /* Determine the characteristics (unsigned and/or 64bit) the type of
2358 the constant must have according to the constant suffix(es) */
2359 lcount
= ucount
= must_64bit
= 0;
2365 tcc_error("three 'l's in integer constant");
2366 if (lcount
&& *(p
- 1) != ch
)
2367 tcc_error("incorrect integer suffix: %s", p1
);
2369 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
2374 } else if (t
== 'U') {
2376 tcc_error("two 'u's in integer constant");
2384 /* Whether 64 bits are needed to hold the constant's value */
2385 if (n
& 0xffffffff00000000LL
|| must_64bit
) {
2393 /* Whether type must be unsigned to hold the constant's value */
2394 if (ucount
|| ((n1
>> 31) && (b
!= 10))) {
2395 if (tok
== TOK_CLLONG
)
2399 /* If decimal and no unsigned suffix, bump to 64 bits or throw error */
2400 } else if (n1
>> 31) {
2401 if (tok
== TOK_CINT
)
2404 tcc_error("integer constant overflow");
2407 if (tok
== TOK_CINT
|| tok
== TOK_CUINT
)
2413 tcc_error("invalid number\n");
2417 #define PARSE2(c1, tok1, c2, tok2) \
2428 /* return next token without macro substitution */
2429 static inline void next_nomacro1(void)
2431 int t
, c
, is_long
, len
;
2439 #if (__TINYC__ || __GNUC__)
2442 goto parse_ident_fast
;
2449 if (parse_flags
& PARSE_FLAG_SPACES
)
2450 goto keep_tok_flags
;
2451 while (isidnum_table
[*p
- CH_EOF
] & IS_SPC
)
2460 /* first look if it is in fact an end of buffer */
2461 c
= handle_stray1(p
);
2468 TCCState
*s1
= tcc_state
;
2469 if ((parse_flags
& PARSE_FLAG_LINEFEED
)
2470 && !(tok_flags
& TOK_FLAG_EOF
)) {
2471 tok_flags
|= TOK_FLAG_EOF
;
2473 goto keep_tok_flags
;
2474 } else if (!(parse_flags
& PARSE_FLAG_PREPROCESS
)) {
2476 } else if (s1
->ifdef_stack_ptr
!= file
->ifdef_stack_ptr
) {
2477 tcc_error("missing #endif");
2478 } else if (s1
->include_stack_ptr
== s1
->include_stack
) {
2479 /* no include left : end of file. */
2482 tok_flags
&= ~TOK_FLAG_EOF
;
2483 /* pop include file */
2485 /* test if previous '#endif' was after a #ifdef at
2487 if (tok_flags
& TOK_FLAG_ENDIF
) {
2489 printf("#endif %s\n", get_tok_str(file
->ifndef_macro_saved
, NULL
));
2491 add_cached_include(s1
, file
->filename
, file
->ifndef_macro_saved
);
2492 tok_flags
&= ~TOK_FLAG_ENDIF
;
2495 /* add end of include file debug info */
2496 if (tcc_state
->do_debug
) {
2497 put_stabd(N_EINCL
, 0, 0);
2499 /* pop include stack */
2501 s1
->include_stack_ptr
--;
2510 tok_flags
|= TOK_FLAG_BOL
;
2513 if (0 == (parse_flags
& PARSE_FLAG_LINEFEED
))
2516 goto keep_tok_flags
;
2521 if ((tok_flags
& TOK_FLAG_BOL
) &&
2522 (parse_flags
& PARSE_FLAG_PREPROCESS
)) {
2524 preprocess(tok_flags
& TOK_FLAG_BOF
);
2530 tok
= TOK_TWOSHARPS
;
2532 if (parse_flags
& PARSE_FLAG_ASM_FILE
) {
2533 p
= parse_line_comment(p
- 1);
2542 /* dollar is allowed to start identifiers when not parsing asm */
2544 if (!(isidnum_table
[c
- CH_EOF
] & IS_ID
)
2545 || (parse_flags
& PARSE_FLAG_ASM_FILE
))
2548 #if (__TINYC__ || __GNUC__)
2555 case 'a': case 'b': case 'c': case 'd':
2556 case 'e': case 'f': case 'g': case 'h':
2557 case 'i': case 'j': case 'k': case 'l':
2558 case 'm': case 'n': case 'o': case 'p':
2559 case 'q': case 'r': case 's': case 't':
2560 case 'u': case 'v': case 'w': case 'x':
2562 case 'A': case 'B': case 'C': case 'D':
2563 case 'E': case 'F': case 'G': case 'H':
2564 case 'I': case 'J': case 'K':
2565 case 'M': case 'N': case 'O': case 'P':
2566 case 'Q': case 'R': case 'S': case 'T':
2567 case 'U': case 'V': case 'W': case 'X':
2574 h
= TOK_HASH_FUNC(h
, c
);
2575 while (c
= *++p
, isidnum_table
[c
- CH_EOF
] & (IS_ID
|IS_NUM
))
2576 h
= TOK_HASH_FUNC(h
, c
);
2581 /* fast case : no stray found, so we have the full token
2582 and we have already hashed it */
2583 h
&= (TOK_HASH_SIZE
- 1);
2584 pts
= &hash_ident
[h
];
2589 if (ts
->len
== len
&& !memcmp(ts
->str
, p1
, len
))
2591 pts
= &(ts
->hash_next
);
2593 ts
= tok_alloc_new(pts
, (char *) p1
, len
);
2597 cstr_reset(&tokcstr
);
2598 cstr_cat(&tokcstr
, p1
, len
);
2602 while (isidnum_table
[c
- CH_EOF
] & (IS_ID
|IS_NUM
))
2604 cstr_ccat(&tokcstr
, c
);
2607 ts
= tok_alloc(tokcstr
.data
, tokcstr
.size
);
2613 if (t
!= '\\' && t
!= '\'' && t
!= '\"') {
2615 goto parse_ident_fast
;
2618 if (c
== '\'' || c
== '\"') {
2622 cstr_reset(&tokcstr
);
2623 cstr_ccat(&tokcstr
, 'L');
2624 goto parse_ident_slow
;
2629 case '0': case '1': case '2': case '3':
2630 case '4': case '5': case '6': case '7':
2632 cstr_reset(&tokcstr
);
2633 /* after the first digit, accept digits, alpha, '.' or sign if
2634 prefixed by 'eEpP' */
2638 cstr_ccat(&tokcstr
, c
);
2640 if (!((isidnum_table
[c
- CH_EOF
] & (IS_ID
|IS_NUM
))
2642 || ((c
== '+' || c
== '-')
2643 && (t
== 'e' || t
== 'E' || t
== 'p' || t
== 'P')
2644 && !(parse_flags
& PARSE_FLAG_ASM_FILE
)
2648 /* We add a trailing '\0' to ease parsing */
2649 cstr_ccat(&tokcstr
, '\0');
2650 tokc
.str
.size
= tokcstr
.size
;
2651 tokc
.str
.data
= tokcstr
.data
;
2652 tokc
.str
.data_allocated
= tokcstr
.data_allocated
;
2657 /* special dot handling because it can also start a number */
2660 cstr_reset(&tokcstr
);
2661 cstr_ccat(&tokcstr
, '.');
2663 } else if ((parse_flags
& PARSE_FLAG_ASM_FILE
)
2664 && (isidnum_table
[c
- CH_EOF
] & (IS_ID
|IS_NUM
))) {
2666 goto parse_ident_fast
;
2667 } else if (c
== '.') {
2673 *--p
= '.'; /* may underflow into file->unget[] */
2684 cstr_reset(&tokcstr
);
2686 cstr_ccat(&tokcstr
, 'L');
2687 cstr_ccat(&tokcstr
, c
);
2688 p
= parse_pp_string(p
, c
, &tokcstr
);
2689 cstr_ccat(&tokcstr
, c
);
2690 cstr_ccat(&tokcstr
, '\0');
2691 tokc
.str
.size
= tokcstr
.size
;
2692 tokc
.str
.data
= tokcstr
.data
;
2693 tokc
.str
.data_allocated
= tokcstr
.data_allocated
;
2702 } else if (c
== '<') {
2719 } else if (c
== '>') {
2737 } else if (c
== '=') {
2750 } else if (c
== '=') {
2763 } else if (c
== '=') {
2776 } else if (c
== '=') {
2779 } else if (c
== '>') {
2787 PARSE2('!', '!', '=', TOK_NE
)
2788 PARSE2('=', '=', '=', TOK_EQ
)
2789 PARSE2('*', '*', '=', TOK_A_MUL
)
2790 PARSE2('%', '%', '=', TOK_A_MOD
)
2791 PARSE2('^', '^', '=', TOK_A_XOR
)
2793 /* comments or operator */
2797 p
= parse_comment(p
);
2798 /* comments replaced by a blank */
2800 goto keep_tok_flags
;
2801 } else if (c
== '/') {
2802 p
= parse_line_comment(p
);
2804 goto keep_tok_flags
;
2805 } else if (c
== '=') {
2825 case '@': /* only used in assembler */
2831 if (parse_flags
& PARSE_FLAG_ASM_FILE
)
2833 tcc_error("unrecognized character \\x%02x", c
);
2839 #if defined(PARSE_DEBUG)
2840 printf("token = %s\n", get_tok_str(tok
, &tokc
));
2844 /* return next token without macro substitution. Can read input from
2846 static void next_nomacro_spc(void)
2852 TOK_GET(&tok
, ¯o_ptr
, &tokc
);
2853 if (tok
== TOK_LINENUM
) {
2854 file
->line_num
= tokc
.i
;
2863 ST_FUNC
void next_nomacro(void)
2867 } while (tok
< 256 && (isidnum_table
[tok
- CH_EOF
] & IS_SPC
));
2871 static void macro_subst(
2872 TokenString
*tok_str
,
2874 const int *macro_str
,
2878 /* substitute arguments in replacement lists in macro_str by the values in
2879 args (field d) and return allocated string */
2880 static int *macro_arg_subst(Sym
**nested_list
, const int *macro_str
, Sym
*args
)
2892 TOK_GET(&t
, ¯o_str
, &cval
);
2897 TOK_GET(&t
, ¯o_str
, &cval
);
2900 s
= sym_find2(args
, t
);
2903 cstr_ccat(&cstr
, '\"');
2907 TOK_GET(&t
, &st
, &cval
);
2908 if (t
!= TOK_PLCHLDR
2910 && 0 == check_space(t
, &spc
)) {
2911 const char *s
= get_tok_str(t
, &cval
);
2913 if (t
== TOK_PPSTR
&& *s
!= '\'')
2914 add_char(&cstr
, *s
);
2916 cstr_ccat(&cstr
, *s
);
2922 cstr_ccat(&cstr
, '\"');
2923 cstr_ccat(&cstr
, '\0');
2925 printf("\nstringize: <%s>\n", (char *)cstr
.data
);
2928 cval
.str
.size
= cstr
.size
;
2929 cval
.str
.data
= cstr
.data
;
2930 cval
.str
.data_allocated
= cstr
.data_allocated
;
2931 tok_str_add2(&str
, TOK_PPSTR
, &cval
);
2935 expect("macro parameter after '#'");
2937 } else if (t
>= TOK_IDENT
) {
2938 s
= sym_find2(args
, t
);
2942 /* if '##' is present before or after, no arg substitution */
2943 if (*macro_str
== TOK_TWOSHARPS
|| t1
== TOK_TWOSHARPS
) {
2944 /* special case for var arg macros : ## eats the ','
2945 if empty VA_ARGS variable. */
2946 if (t1
== TOK_TWOSHARPS
&& t0
== ',' && gnu_ext
&& s
->type
.t
) {
2948 /* suppress ',' '##' */
2951 /* suppress '##' and add variable */
2958 TOK_GET(&t1
, &st
, &cval
);
2961 tok_str_add2(&str
, t1
, &cval
);
2967 /* NOTE: the stream cannot be read when macro
2968 substituing an argument */
2969 macro_subst(&str
, nested_list
, st
, 0);
2971 if (str
.len
== l0
) /* exanded to empty string */
2972 tok_str_add(&str
, TOK_PLCHLDR
);
2974 tok_str_add(&str
, t
);
2977 tok_str_add2(&str
, t
, &cval
);
2981 tok_str_add(&str
, 0);
2985 static char const ab_month_name
[12][4] =
2987 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2988 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
2991 /* peek or read [ws_str == NULL] next token from function macro call,
2992 walking up macro levels up to the file if necessary */
2993 static int next_argstream(Sym
**nested_list
, int can_read_stream
, TokenString
*ws_str
)
3001 p
= macro_ptr
, t
= *p
;
3003 while (is_space(t
) || TOK_LINEFEED
== t
)
3004 tok_str_add(ws_str
, t
), t
= *++p
;
3006 if (t
== 0 && can_read_stream
) {
3008 /* also, end of scope for nested defined symbol */
3010 while (sa
&& sa
->v
== 0)
3019 while (is_space(ch
) || ch
== '\n' || ch
== '/') {
3022 uint8_t *p
= file
->buf_ptr
;
3025 p
= parse_comment(p
);
3026 file
->buf_ptr
= p
- 1;
3027 } else if (c
== '/') {
3028 p
= parse_line_comment(p
);
3029 file
->buf_ptr
= p
- 1;
3034 tok_str_add(ws_str
, ch
);
3048 /* do macro substitution of current token with macro 's' and add
3049 result to (tok_str,tok_len). 'nested_list' is the list of all
3050 macros we got inside to avoid recursing. Return non zero if no
3051 substitution needs to be done */
3052 static int macro_subst_tok(
3053 TokenString
*tok_str
,
3056 int can_read_stream
)
3058 Sym
*args
, *sa
, *sa1
;
3059 int parlevel
, *mstr
, t
, t1
, spc
;
3066 /* if symbol is a macro, prepare substitution */
3067 /* special macros */
3068 if (tok
== TOK___LINE__
) {
3069 snprintf(buf
, sizeof(buf
), "%d", file
->line_num
);
3073 } else if (tok
== TOK___FILE__
) {
3074 cstrval
= file
->filename
;
3076 } else if (tok
== TOK___DATE__
|| tok
== TOK___TIME__
) {
3081 tm
= localtime(&ti
);
3082 if (tok
== TOK___DATE__
) {
3083 snprintf(buf
, sizeof(buf
), "%s %2d %d",
3084 ab_month_name
[tm
->tm_mon
], tm
->tm_mday
, tm
->tm_year
+ 1900);
3086 snprintf(buf
, sizeof(buf
), "%02d:%02d:%02d",
3087 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
3094 cstr_cat(&cstr
, cstrval
, 0);
3095 cval
.str
.size
= cstr
.size
;
3096 cval
.str
.data
= cstr
.data
;
3097 cval
.str
.data_allocated
= cstr
.data_allocated
;
3098 tok_str_add2(tok_str
, t1
, &cval
);
3101 int saved_parse_flags
= parse_flags
;
3104 if (s
->type
.t
== MACRO_FUNC
) {
3105 /* whitespace between macro name and argument list */
3107 tok_str_new(&ws_str
);
3110 parse_flags
|= PARSE_FLAG_SPACES
| PARSE_FLAG_LINEFEED
3111 | PARSE_FLAG_ACCEPT_STRAYS
;
3113 /* get next token from argument stream */
3114 t
= next_argstream(nested_list
, can_read_stream
, &ws_str
);
3116 /* not a macro substitution after all, restore the
3117 * macro token plus all whitespace we've read.
3118 * whitespace is intentionally not merged to preserve
3120 parse_flags
= saved_parse_flags
;
3121 tok_str_add(tok_str
, tok
);
3122 if (parse_flags
& PARSE_FLAG_SPACES
) {
3124 for (i
= 0; i
< ws_str
.len
; i
++)
3125 tok_str_add(tok_str
, ws_str
.str
[i
]);
3127 tok_str_free(ws_str
.str
);
3130 tok_str_free(ws_str
.str
);
3132 next_nomacro(); /* eat '(' */
3134 /* argument macro */
3137 /* NOTE: empty args are allowed, except if no args */
3140 next_argstream(nested_list
, can_read_stream
, NULL
);
3141 } while (is_space(tok
) || TOK_LINEFEED
== tok
);
3143 /* handle '()' case */
3144 if (!args
&& !sa
&& tok
== ')')
3147 tcc_error("macro '%s' used with too many args",
3148 get_tok_str(s
->v
, 0));
3151 /* NOTE: non zero sa->t indicates VA_ARGS */
3152 while ((parlevel
> 0 ||
3154 (tok
!= ',' || sa
->type
.t
)))) {
3155 if (tok
== TOK_EOF
|| tok
== 0)
3159 else if (tok
== ')')
3161 if (tok
== TOK_LINEFEED
)
3163 if (!check_space(tok
, &spc
))
3164 tok_str_add2(&str
, tok
, &tokc
);
3165 next_argstream(nested_list
, can_read_stream
, NULL
);
3170 tok_str_add(&str
, 0);
3171 sa1
= sym_push2(&args
, sa
->v
& ~SYM_FIELD
, sa
->type
.t
, 0);
3175 /* special case for gcc var args: add an empty
3176 var arg argument if it is omitted */
3177 if (sa
&& sa
->type
.t
&& gnu_ext
)
3185 tcc_error("macro '%s' used with too few args",
3186 get_tok_str(s
->v
, 0));
3189 parse_flags
= saved_parse_flags
;
3191 /* now subst each arg */
3192 mstr
= macro_arg_subst(nested_list
, mstr
, args
);
3197 tok_str_free(sa
->d
);
3203 sym_push2(nested_list
, s
->v
, 0, 0);
3204 parse_flags
= saved_parse_flags
;
3205 macro_subst(tok_str
, nested_list
, mstr
, can_read_stream
| 2);
3207 /* pop nested defined symbol */
3209 *nested_list
= sa1
->prev
;
3217 int paste_tokens(int t1
, CValue
*v1
, int t2
, CValue
*v2
)
3223 if (t1
!= TOK_PLCHLDR
)
3224 cstr_cat(&cstr
, get_tok_str(t1
, v1
), -1);
3226 if (t2
!= TOK_PLCHLDR
)
3227 cstr_cat(&cstr
, get_tok_str(t2
, v2
), -1);
3228 cstr_ccat(&cstr
, '\0');
3230 tcc_open_bf(tcc_state
, ":paste:", cstr
.size
);
3231 memcpy(file
->buffer
, cstr
.data
, cstr
.size
);
3234 if (0 == *file
->buf_ptr
)
3238 tcc_warning("pasting <%.*s> and <%s> does not give a valid preprocessing token",
3239 n
, cstr
.data
, (char*)cstr
.data
+ n
);
3244 //printf("paste <%s>\n", (char*)cstr.data);
3249 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
3250 return the resulting string (which must be freed). */
3251 static inline int *macro_twosharps(const int *ptr0
)
3255 TokenString macro_str1
;
3256 int start_of_nosubsts
= -1;
3259 /* we search the first '##' */
3260 for (ptr
= ptr0
;;) {
3261 TOK_GET(&t
, &ptr
, &cval
);
3262 if (t
== TOK_TWOSHARPS
)
3268 tok_str_new(¯o_str1
);
3270 //tok_print(" $$$", ptr0);
3271 for (ptr
= ptr0
;;) {
3272 TOK_GET(&t
, &ptr
, &cval
);
3275 if (t
== TOK_TWOSHARPS
)
3277 while (*ptr
== TOK_TWOSHARPS
) {
3279 /* given 'a##b', remove nosubsts preceding 'a' */
3280 if (start_of_nosubsts
>= 0)
3281 macro_str1
.len
= start_of_nosubsts
;
3282 /* given 'a##b', remove nosubsts preceding 'b' */
3283 while ((t1
= *++ptr
) == TOK_NOSUBST
)
3285 if (t1
&& t1
!= TOK_TWOSHARPS
3286 && t1
!= ':') /* 'a##:' don't build a new token */
3288 TOK_GET(&t1
, &ptr
, &cv1
);
3289 if (t
!= TOK_PLCHLDR
|| t1
!= TOK_PLCHLDR
) {
3290 paste_tokens(t
, &cval
, t1
, &cv1
);
3291 t
= tok
, cval
= tokc
;
3295 if (t
== TOK_NOSUBST
) {
3296 if (start_of_nosubsts
< 0)
3297 start_of_nosubsts
= macro_str1
.len
;
3299 start_of_nosubsts
= -1;
3301 tok_str_add2(¯o_str1
, t
, &cval
);
3303 tok_str_add(¯o_str1
, 0);
3304 //tok_print(" ###", macro_str1.str);
3305 return macro_str1
.str
;
3308 /* do macro substitution of macro_str and add result to
3309 (tok_str,tok_len). 'nested_list' is the list of all macros we got
3310 inside to avoid recursing. */
3311 static void macro_subst(
3312 TokenString
*tok_str
,
3314 const int *macro_str
,
3320 int t
, spc
, nosubst
;
3322 int *macro_str1
= NULL
;
3324 /* first scan for '##' operator handling */
3328 /* first scan for '##' operator handling */
3329 if (can_read_stream
& 1) {
3330 macro_str1
= macro_twosharps(ptr
);
3336 TOK_GET(&t
, &ptr
, &cval
);
3340 if (t
>= TOK_IDENT
&& 0 == nosubst
) {
3345 /* if nested substitution, do nothing */
3346 if (sym_find2(*nested_list
, t
)) {
3347 /* and mark it as TOK_NOSUBST, so it doesn't get subst'd again */
3348 tok_str_add2(tok_str
, TOK_NOSUBST
, NULL
);
3354 str
.str
= (int*)ptr
;
3355 begin_macro(&str
, 2);
3358 macro_subst_tok(tok_str
, nested_list
, s
, can_read_stream
);
3360 if (str
.alloc
== 3) {
3361 /* already finished by reading function macro arguments */
3369 spc
= (tok_str
->len
&&
3370 is_space(tok_last(tok_str
->str
,
3371 tok_str
->str
+ tok_str
->len
)));
3375 if (t
== '\\' && !(parse_flags
& PARSE_FLAG_ACCEPT_STRAYS
))
3376 tcc_error("stray '\\' in program");
3379 if (!check_space(t
, &spc
))
3380 tok_str_add2(tok_str
, t
, &cval
);
3382 if (t
== TOK_NOSUBST
)
3387 tok_str_free(macro_str1
);
3391 /* return next token with macro substitution */
3392 ST_FUNC
void next(void)
3395 if (parse_flags
& PARSE_FLAG_SPACES
)
3401 if (tok
== TOK_NOSUBST
|| tok
== TOK_PLCHLDR
) {
3402 /* discard preprocessor markers */
3404 } else if (tok
== 0) {
3405 /* end of macro or unget token string */
3409 } else if (tok
>= TOK_IDENT
&& (parse_flags
& PARSE_FLAG_PREPROCESS
)) {
3411 /* if reading from file, try to substitute macros */
3412 s
= define_find(tok
);
3414 Sym
*nested_list
= NULL
;
3417 macro_subst_tok(&tokstr_buf
, &nested_list
, s
, 1);
3418 tok_str_add(&tokstr_buf
, 0);
3419 begin_macro(&tokstr_buf
, 2);
3423 /* convert preprocessor tokens into C tokens */
3424 if (tok
== TOK_PPNUM
) {
3425 if (parse_flags
& PARSE_FLAG_TOK_NUM
)
3426 parse_number((char *)tokc
.str
.data
);
3427 } else if (tok
== TOK_PPSTR
) {
3428 if (parse_flags
& PARSE_FLAG_TOK_STR
)
3429 parse_string((char *)tokc
.str
.data
, tokc
.str
.size
- 1);
3433 /* push back current token and set current token to 'last_tok'. Only
3434 identifier case handled for labels. */
3435 ST_INLN
void unget_tok(int last_tok
)
3437 TokenString
*str
= tcc_malloc(sizeof *str
);
3439 tok_str_add2(str
, tok
, &tokc
);
3440 tok_str_add(str
, 0);
3441 begin_macro(str
, 1);
3445 ST_FUNC
void preprocess_init(TCCState
*s1
)
3447 s1
->include_stack_ptr
= s1
->include_stack
;
3448 /* XXX: move that before to avoid having to initialize
3449 file->ifdef_stack_ptr ? */
3450 s1
->ifdef_stack_ptr
= s1
->ifdef_stack
;
3451 file
->ifdef_stack_ptr
= s1
->ifdef_stack_ptr
;
3453 pvtop
= vtop
= vstack
- 1;
3454 s1
->pack_stack
[0] = 0;
3455 s1
->pack_stack_ptr
= s1
->pack_stack
;
3457 isidnum_table
['$' - CH_EOF
] =
3458 s1
->dollars_in_identifiers
? IS_ID
: 0;
3459 isidnum_table
['.' - CH_EOF
] =
3460 (parse_flags
& PARSE_FLAG_ASM_FILE
) ? IS_ID
: 0;
3463 ST_FUNC
void preprocess_new(void)
3468 /* init isid table */
3469 for(i
= CH_EOF
; i
<128; i
++)
3470 isidnum_table
[i
- CH_EOF
]
3471 = is_space(i
) ? IS_SPC
3476 for(i
= 128; i
<256; i
++)
3477 isidnum_table
[i
- CH_EOF
] = IS_ID
;
3479 /* init allocators */
3480 tal_new(&toksym_alloc
, TOKSYM_TAL_LIMIT
, TOKSYM_TAL_SIZE
);
3481 tal_new(&tokstr_alloc
, TOKSTR_TAL_LIMIT
, TOKSTR_TAL_SIZE
);
3482 tal_new(&cstr_alloc
, CSTR_TAL_LIMIT
, CSTR_TAL_SIZE
);
3484 memset(hash_ident
, 0, TOK_HASH_SIZE
* sizeof(TokenSym
*));
3485 cstr_new(&cstr_buf
);
3486 cstr_realloc(&cstr_buf
, STRING_MAX_SIZE
);
3487 tok_str_new(&tokstr_buf
);
3488 tok_str_realloc(&tokstr_buf
, TOKSTR_MAX_SIZE
);
3490 tok_ident
= TOK_IDENT
;
3499 tok_alloc(p
, r
- p
- 1);
3504 ST_FUNC
void preprocess_delete(void)
3508 /* free -D and compiler defines */
3511 /* cleanup from error/setjmp */
3517 n
= tok_ident
- TOK_IDENT
;
3518 for(i
= 0; i
< n
; i
++)
3519 tal_free(toksym_alloc
, table_ident
[i
]);
3520 tcc_free(table_ident
);
3523 /* free static buffers */
3524 cstr_free(&tokcstr
);
3525 cstr_free(&cstr_buf
);
3526 tok_str_free(tokstr_buf
.str
);
3528 /* free allocators */
3529 tal_delete(toksym_alloc
);
3530 toksym_alloc
= NULL
;
3531 tal_delete(tokstr_alloc
);
3532 tokstr_alloc
= NULL
;
3533 tal_delete(cstr_alloc
);
3537 /* ------------------------------------------------------------------------- */
3538 /* tcc -E [-P[1]] [-dD} support */
3540 static void tok_print(const char *msg
, const int *str
)
3546 fp
= tcc_state
->ppfp
;
3547 if (!fp
|| !tcc_state
->dflag
)
3550 fprintf(fp
, "%s ", msg
);
3552 TOK_GET(&t
, &str
, &cval
);
3555 fprintf(fp
,"%s", get_tok_str(t
, &cval
));
3560 static void pp_line(TCCState
*s1
, BufferedFile
*f
, int level
)
3562 int d
= f
->line_num
- f
->line_ref
;
3567 if (s1
->Pflag
== LINE_MACRO_OUTPUT_FORMAT_NONE
) {
3568 if (level
== 0 && f
->line_ref
&& d
) {
3572 } else if (level
== 0 && f
->line_ref
&& d
< 8) {
3575 fputs("\n", s1
->ppfp
), --d
;
3576 } else if (s1
->Pflag
== LINE_MACRO_OUTPUT_FORMAT_STD
) {
3577 fprintf(s1
->ppfp
, "#line %d \"%s\"\n", f
->line_num
, f
->filename
);
3579 fprintf(s1
->ppfp
, "# %d \"%s\"%s\n", f
->line_num
, f
->filename
,
3580 level
> 0 ? " 1" : level
< 0 ? " 2" : "");
3582 f
->line_ref
= f
->line_num
;
3585 static void define_print(TCCState
*s1
, int v
)
3591 if (NULL
== s
|| NULL
== s
->d
)
3595 fprintf(fp
, "#define %s", get_tok_str(v
, NULL
));
3596 if (s
->type
.t
== MACRO_FUNC
) {
3601 fprintf(fp
,"%s", get_tok_str(a
->v
& ~SYM_FIELD
, NULL
));
3608 tok_print("", s
->d
);
3611 static void pp_debug_defines(TCCState
*s1
)
3622 pp_line(s1
, file
, 0);
3623 file
->line_ref
= ++file
->line_num
;
3627 vs
= get_tok_str(v
, NULL
);
3628 if (t
== TOK_DEFINE
) {
3629 define_print(s1
, v
);
3630 } else if (t
== TOK_UNDEF
) {
3631 fprintf(fp
, "#undef %s\n", vs
);
3632 } else if (t
== TOK_push_macro
) {
3633 fprintf(fp
, "#pragma push_macro(\"%s\")\n", vs
);
3634 } else if (t
== TOK_pop_macro
) {
3635 fprintf(fp
, "#pragma pop_macro(\"%s\")\n", vs
);
3640 static void pp_debug_builtins(TCCState
*s1
)
3643 for (v
= TOK_IDENT
; v
< tok_ident
; ++v
)
3644 define_print(s1
, v
);
3647 static int need_space(int prev_tok
, int tok
, const char *tokstr
)
3649 const char *sp_chars
= "";
3650 if ((prev_tok
>= TOK_IDENT
|| prev_tok
== TOK_PPNUM
) &&
3651 (tok
>= TOK_IDENT
|| tok
== TOK_PPNUM
))
3692 return !!strchr(sp_chars
, tokstr
[0]);
3695 /* Preprocess the current file */
3696 ST_FUNC
int tcc_preprocess(TCCState
*s1
)
3698 BufferedFile
**iptr
;
3699 int token_seen
, spcs
, level
;
3703 preprocess_init(s1
);
3704 ch
= file
->buf_ptr
[0];
3705 tok_flags
= TOK_FLAG_BOL
| TOK_FLAG_BOF
;
3706 parse_flags
= PARSE_FLAG_PREPROCESS
3707 | (parse_flags
& PARSE_FLAG_ASM_FILE
)
3708 | PARSE_FLAG_LINEFEED
3710 | PARSE_FLAG_ACCEPT_STRAYS
3712 define_start
= define_stack
;
3714 /* Credits to Fabrice Bellard's initial revision to demonstrate its
3715 capability to compile and run itself, provided all numbers are
3716 given as decimals. tcc -E -P10 will do. */
3717 if (s1
->Pflag
== 1 + 10)
3718 parse_flags
|= PARSE_FLAG_TOK_NUM
, s1
->Pflag
= 1;
3721 /* for PP benchmarks */
3722 do next(); while (tok
!= TOK_EOF
); return 0;
3725 if (s1
->dflag
& 1) {
3726 pp_debug_builtins(s1
);
3730 token_seen
= TOK_LINEFEED
, spcs
= 0;
3731 pp_line(s1
, file
, 0);
3734 iptr
= s1
->include_stack_ptr
;
3738 level
= s1
->include_stack_ptr
- iptr
;
3741 pp_line(s1
, *iptr
, 0);
3742 pp_line(s1
, file
, level
);
3746 pp_debug_defines(s1
);
3751 if (token_seen
== TOK_LINEFEED
) {
3756 if (tok
== TOK_LINEFEED
) {
3760 pp_line(s1
, file
, 0);
3761 } else if (tok
== TOK_LINEFEED
) {
3765 tokstr
= get_tok_str(tok
, &tokc
);
3766 if (!spcs
&& need_space(token_seen
, tok
, tokstr
))
3769 fputs(" ", s1
->ppfp
), --spcs
;
3770 fputs(tokstr
, s1
->ppfp
);
3774 /* reset define stack, but keep -D and built-ins */
3775 free_defines(define_start
);
3779 /* ------------------------------------------------------------------------- */