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
);
469 /* XXX: not quite exact, but only useful for testing */
471 sprintf(p
, "%u", (unsigned)cv
->i
);
473 sprintf(p
, "%llu", (unsigned long long)cv
->i
);
477 cstr_ccat(&cstr_buf
, 'L');
479 cstr_ccat(&cstr_buf
, '\'');
480 add_char(&cstr_buf
, cv
->i
);
481 cstr_ccat(&cstr_buf
, '\'');
482 cstr_ccat(&cstr_buf
, '\0');
486 return (char*)cv
->str
.data
;
488 cstr_ccat(&cstr_buf
, 'L');
490 cstr_ccat(&cstr_buf
, '\"');
492 len
= cv
->str
.size
- 1;
494 add_char(&cstr_buf
, ((unsigned char *)cv
->str
.data
)[i
]);
496 len
= (cv
->str
.size
/ sizeof(nwchar_t
)) - 1;
498 add_char(&cstr_buf
, ((nwchar_t
*)cv
->str
.data
)[i
]);
500 cstr_ccat(&cstr_buf
, '\"');
501 cstr_ccat(&cstr_buf
, '\0');
505 cstr_cat(&cstr_buf
, "<float>", 0);
508 cstr_cat(&cstr_buf
, "<double>", 0);
511 cstr_cat(&cstr_buf
, "<long double>", 0);
514 cstr_cat(&cstr_buf
, "<linenumber>", 0);
517 /* above tokens have value, the ones below don't */
526 return strcpy(p
, "...");
528 return strcpy(p
, "<<=");
530 return strcpy(p
, ">>=");
533 /* search in two bytes table */
534 const unsigned char *q
= tok_two_chars
;
540 return cstr_buf
.data
;
545 sprintf(cstr_buf
.data
, "<%02x>", v
);
546 return cstr_buf
.data
;
551 } else if (v
< tok_ident
) {
552 return table_ident
[v
- TOK_IDENT
]->str
;
553 } else if (v
>= SYM_FIRST_ANOM
) {
554 /* special name for anonymous symbol */
555 sprintf(p
, "L.%u", v
- SYM_FIRST_ANOM
);
557 /* should never happen */
562 return cstr_buf
.data
;
565 /* return the current character, handling end of block if necessary
567 ST_FUNC
int handle_eob(void)
569 BufferedFile
*bf
= file
;
572 /* only tries to read if really end of buffer */
573 if (bf
->buf_ptr
>= bf
->buf_end
) {
575 #if defined(PARSE_DEBUG)
580 len
= read(bf
->fd
, bf
->buffer
, len
);
587 bf
->buf_ptr
= bf
->buffer
;
588 bf
->buf_end
= bf
->buffer
+ len
;
589 *bf
->buf_end
= CH_EOB
;
591 if (bf
->buf_ptr
< bf
->buf_end
) {
592 return bf
->buf_ptr
[0];
594 bf
->buf_ptr
= bf
->buf_end
;
599 /* read next char from current input file and handle end of input buffer */
600 ST_INLN
void inp(void)
602 ch
= *(++(file
->buf_ptr
));
603 /* end of buffer/file handling */
608 /* handle '\[\r]\n' */
609 static int handle_stray_noerror(void)
616 } else if (ch
== '\r') {
630 static void handle_stray(void)
632 if (handle_stray_noerror())
633 tcc_error("stray '\\' in program");
636 /* skip the stray and handle the \\n case. Output an error if
637 incorrect char after the stray */
638 static int handle_stray1(uint8_t *p
)
643 if (p
>= file
->buf_end
) {
650 if (handle_stray_noerror()) {
651 if (!(parse_flags
& PARSE_FLAG_ACCEPT_STRAYS
))
652 tcc_error("stray '\\' in program");
653 *--file
->buf_ptr
= '\\';
660 /* handle just the EOB case, but not stray */
661 #define PEEKC_EOB(c, p)\
672 /* handle the complicated stray case */
678 c = handle_stray1(p);\
683 /* input with '\[\r]\n' handling. Note that this function cannot
684 handle other characters after '\', so you cannot call it inside
685 strings or comments */
686 ST_FUNC
void minp(void)
693 /* single line C++ comments */
694 static uint8_t *parse_line_comment(uint8_t *p
)
702 if (c
== '\n' || c
== CH_EOF
) {
704 } else if (c
== '\\') {
713 } else if (c
== '\r') {
731 ST_FUNC
uint8_t *parse_comment(uint8_t *p
)
740 if (c
== '\n' || c
== '*' || c
== '\\')
744 if (c
== '\n' || c
== '*' || c
== '\\')
748 /* now we can handle all the cases */
752 } else if (c
== '*') {
758 } else if (c
== '/') {
760 } else if (c
== '\\') {
765 tcc_error("unexpected end of file in comment");
767 /* skip '\[\r]\n', otherwise just skip the stray */
773 } else if (c
== '\r') {
790 /* stray, eob or eof */
795 tcc_error("unexpected end of file in comment");
796 } else if (c
== '\\') {
808 static inline void skip_spaces(void)
810 while (isidnum_table
[ch
- CH_EOF
] & IS_SPC
)
814 static inline int check_space(int t
, int *spc
)
816 if (t
< 256 && (isidnum_table
[t
- CH_EOF
] & IS_SPC
)) {
825 /* parse a string without interpreting escapes */
826 static uint8_t *parse_pp_string(uint8_t *p
,
827 int sep
, CString
*str
)
835 } else if (c
== '\\') {
841 /* XXX: indicate line number of start of string */
842 tcc_error("missing terminating %c character", sep
);
843 } else if (c
== '\\') {
844 /* escape : just skip \[\r]\n */
849 } else if (c
== '\r') {
852 expect("'\n' after '\r'");
855 } else if (c
== CH_EOF
) {
856 goto unterminated_string
;
859 cstr_ccat(str
, '\\');
865 } else if (c
== '\n') {
868 } else if (c
== '\r') {
872 cstr_ccat(str
, '\r');
888 /* skip block of text until #else, #elif or #endif. skip also pairs of
890 static void preprocess_skip(void)
892 int a
, start_of_line
, c
, in_warn_or_error
;
899 in_warn_or_error
= 0;
920 } else if (c
== '\\') {
921 ch
= file
->buf_ptr
[0];
922 handle_stray_noerror();
929 if (in_warn_or_error
)
931 p
= parse_pp_string(p
, c
, NULL
);
935 if (in_warn_or_error
)
942 p
= parse_comment(p
);
943 } else if (ch
== '/') {
944 p
= parse_line_comment(p
);
954 (tok
== TOK_ELSE
|| tok
== TOK_ELIF
|| tok
== TOK_ENDIF
))
956 if (tok
== TOK_IF
|| tok
== TOK_IFDEF
|| tok
== TOK_IFNDEF
)
958 else if (tok
== TOK_ENDIF
)
960 else if( tok
== TOK_ERROR
|| tok
== TOK_WARNING
)
961 in_warn_or_error
= 1;
962 else if (tok
== TOK_LINEFEED
)
964 else if (parse_flags
& PARSE_FLAG_ASM_FILE
)
965 p
= parse_line_comment(p
);
966 } else if (parse_flags
& PARSE_FLAG_ASM_FILE
)
967 p
= parse_line_comment(p
);
980 /* ParseState handling */
982 /* XXX: currently, no include file info is stored. Thus, we cannot display
983 accurate messages if the function or data definition spans multiple
986 /* save current parse state in 's' */
987 ST_FUNC
void save_parse_state(ParseState
*s
)
989 s
->line_num
= file
->line_num
;
990 s
->macro_ptr
= macro_ptr
;
995 /* restore parse state from 's' */
996 ST_FUNC
void restore_parse_state(ParseState
*s
)
998 file
->line_num
= s
->line_num
;
999 macro_ptr
= s
->macro_ptr
;
1004 /* return the number of additional 'ints' necessary to store the
1006 static inline int tok_size(const int *p
)
1021 return 1 + ((sizeof(CString
) + ((CString
*)(p
+1))->size
+ 3) >> 2);
1027 return 1 + LDOUBLE_SIZE
/ 4;
1033 /* token string handling */
1035 ST_INLN
void tok_str_new(TokenString
*s
)
1039 s
->allocated_len
= 0;
1040 s
->last_line_num
= -1;
1043 ST_FUNC
int *tok_str_dup(TokenString
*s
)
1047 str
= tal_realloc(tokstr_alloc
, 0, s
->len
* sizeof(int));
1048 memcpy(str
, s
->str
, s
->len
* sizeof(int));
1052 ST_FUNC
void tok_str_free(int *str
)
1054 tal_free(tokstr_alloc
, str
);
1057 ST_FUNC
int *tok_str_realloc(TokenString
*s
, int new_size
)
1061 size
= s
->allocated_len
;
1064 while (size
< new_size
)
1066 TCC_ASSERT((size
& (size
-1)) == 0);
1067 if (size
> s
->allocated_len
) {
1068 str
= tal_realloc(tokstr_alloc
, s
->str
, size
* sizeof(int));
1069 s
->allocated_len
= size
;
1075 ST_FUNC
void tok_str_add(TokenString
*s
, int t
)
1081 if (len
>= s
->allocated_len
)
1082 str
= tok_str_realloc(s
, len
+ 1);
1087 static void tok_str_add2(TokenString
*s
, int t
, CValue
*cv
)
1094 /* allocate space for worst case */
1095 if (len
+ TOK_MAX_SIZE
>= s
->allocated_len
)
1096 str
= tok_str_realloc(s
, len
+ TOK_MAX_SIZE
+ 1);
1105 str
[len
++] = cv
->tab
[0];
1112 /* Insert the string into the int array. */
1114 1 + (cv
->str
.size
+ sizeof(int) - 1) / sizeof(int);
1115 if (len
+ nb_words
>= s
->allocated_len
)
1116 str
= tok_str_realloc(s
, len
+ nb_words
+ 1);
1117 str
[len
] = cv
->str
.size
;
1118 memcpy(&str
[len
+ 1], cv
->str
.data
, cv
->str
.size
);
1125 #if LDOUBLE_SIZE == 8
1128 str
[len
++] = cv
->tab
[0];
1129 str
[len
++] = cv
->tab
[1];
1131 #if LDOUBLE_SIZE == 12
1133 str
[len
++] = cv
->tab
[0];
1134 str
[len
++] = cv
->tab
[1];
1135 str
[len
++] = cv
->tab
[2];
1136 #elif LDOUBLE_SIZE == 16
1138 str
[len
++] = cv
->tab
[0];
1139 str
[len
++] = cv
->tab
[1];
1140 str
[len
++] = cv
->tab
[2];
1141 str
[len
++] = cv
->tab
[3];
1142 #elif LDOUBLE_SIZE != 8
1143 #error add long double size support
1152 /* add the current parse token in token string 's' */
1153 ST_FUNC
void tok_str_add_tok(TokenString
*s
)
1157 /* save line number info */
1158 if (file
->line_num
!= s
->last_line_num
) {
1159 s
->last_line_num
= file
->line_num
;
1160 cval
.i
= s
->last_line_num
;
1161 tok_str_add2(s
, TOK_LINENUM
, &cval
);
1163 tok_str_add2(s
, tok
, &tokc
);
1166 /* get a token from an integer array and increment pointer
1167 accordingly. we code it as a macro to avoid pointer aliasing. */
1168 static inline void TOK_GET(int *t
, const int **pp
, CValue
*cv
)
1187 cv
->str
.size
= *p
++;
1189 cv
->str
.data_allocated
= 0;
1190 p
+= (cv
->str
.size
+ sizeof(int) - 1) / sizeof(int);
1198 #if LDOUBLE_SIZE == 16
1200 #elif LDOUBLE_SIZE == 12
1202 #elif LDOUBLE_SIZE == 8
1205 # error add long double size support
1218 /* Calling this function is expensive, but it is not possible
1219 to read a token string backwards. */
1220 static int tok_last(const int *str0
, const int *str1
)
1222 const int *str
= str0
;
1227 TOK_GET(&tok
, &str
, &cval
);
1231 static int macro_is_equal(const int *a
, const int *b
)
1240 /* first time preallocate static cstr_buf, next time only reset position to start */
1241 cstr_reset(&cstr_buf
);
1242 TOK_GET(&t
, &a
, &cv
);
1243 cstr_cat(&cstr_buf
, get_tok_str(t
, &cv
), 0);
1244 TOK_GET(&t
, &b
, &cv
);
1245 if (strcmp(cstr_buf
.data
, get_tok_str(t
, &cv
)))
1251 /* defines handling */
1252 ST_INLN
void define_push(int v
, int macro_type
, TokenString
*str
, Sym
*first_arg
)
1257 s
= sym_push2(&define_stack
, v
, macro_type
, 0);
1258 s
->d
= str
? tok_str_dup(str
) : NULL
;
1259 s
->next
= first_arg
;
1260 table_ident
[v
- TOK_IDENT
]->sym_define
= s
;
1262 if (o
&& !macro_is_equal(o
->d
, s
->d
))
1263 tcc_warning("%s redefined", get_tok_str(v
, NULL
));
1266 /* undefined a define symbol. Its name is just set to zero */
1267 ST_FUNC
void define_undef(Sym
*s
)
1270 if (v
>= TOK_IDENT
&& v
< tok_ident
)
1271 table_ident
[v
- TOK_IDENT
]->sym_define
= NULL
;
1274 ST_INLN Sym
*define_find(int v
)
1277 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
1279 return table_ident
[v
]->sym_define
;
1282 /* free define stack until top reaches 'b' */
1283 ST_FUNC
void free_defines(Sym
*b
)
1291 /* do not free args or predefined defines */
1293 tok_str_free(top
->d
);
1295 if (v
>= TOK_IDENT
&& v
< tok_ident
)
1296 table_ident
[v
- TOK_IDENT
]->sym_define
= NULL
;
1304 ST_FUNC Sym
*label_find(int v
)
1307 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
1309 return table_ident
[v
]->sym_label
;
1312 ST_FUNC Sym
*label_push(Sym
**ptop
, int v
, int flags
)
1315 s
= sym_push2(ptop
, v
, 0, 0);
1317 ps
= &table_ident
[v
- TOK_IDENT
]->sym_label
;
1318 if (ptop
== &global_label_stack
) {
1319 /* modify the top most local identifier, so that
1320 sym_identifier will point to 's' when popped */
1322 ps
= &(*ps
)->prev_tok
;
1329 /* pop labels until element last is reached. Look if any labels are
1330 undefined. Define symbols if '&&label' was used. */
1331 ST_FUNC
void label_pop(Sym
**ptop
, Sym
*slast
)
1334 for(s
= *ptop
; s
!= slast
; s
= s1
) {
1336 if (s
->r
== LABEL_DECLARED
) {
1337 tcc_warning("label '%s' declared but not used", get_tok_str(s
->v
, NULL
));
1338 } else if (s
->r
== LABEL_FORWARD
) {
1339 tcc_error("label '%s' used but not defined",
1340 get_tok_str(s
->v
, NULL
));
1343 /* define corresponding symbol. A size of
1345 put_extern_sym(s
, cur_text_section
, s
->jnext
, 1);
1349 table_ident
[s
->v
- TOK_IDENT
]->sym_label
= s
->prev_tok
;
1355 /* eval an expression for #if/#elif */
1356 static int expr_preprocess(void)
1362 while (tok
!= TOK_LINEFEED
&& tok
!= TOK_EOF
) {
1363 next(); /* do macro subst */
1364 if (tok
== TOK_DEFINED
) {
1369 c
= define_find(tok
) != 0;
1374 } else if (tok
>= TOK_IDENT
) {
1375 /* if undefined macro */
1379 tok_str_add_tok(&str
);
1381 tok_str_add(&str
, -1); /* simulate end of file */
1382 tok_str_add(&str
, 0);
1383 /* now evaluate C constant expression */
1384 begin_macro(&str
, 0);
1392 /* parse after #define */
1393 ST_FUNC
void parse_define(void)
1395 Sym
*s
, *first
, **ps
;
1396 int v
, t
, varg
, is_vaargs
, spc
;
1397 int saved_parse_flags
= parse_flags
;
1401 tcc_error("invalid macro name '%s'", get_tok_str(tok
, &tokc
));
1402 /* XXX: should check if same macro (ANSI) */
1405 /* '(' must be just after macro definition for MACRO_FUNC */
1406 parse_flags
|= PARSE_FLAG_SPACES
;
1409 /* must be able to parse TOK_DOTS (in asm mode '.' can be part of identifier) */
1410 parse_flags
&= ~PARSE_FLAG_ASM_FILE
;
1411 isidnum_table
['.' - CH_EOF
] = 0;
1414 if (tok
!= ')') for (;;) {
1418 if (varg
== TOK_DOTS
) {
1419 varg
= TOK___VA_ARGS__
;
1421 } else if (tok
== TOK_DOTS
&& gnu_ext
) {
1425 if (varg
< TOK_IDENT
)
1427 tcc_error("bad macro parameter list");
1428 s
= sym_push2(&define_stack
, varg
| SYM_FIELD
, is_vaargs
, 0);
1433 if (tok
!= ',' || is_vaargs
)
1439 parse_flags
|= (saved_parse_flags
& PARSE_FLAG_ASM_FILE
);
1440 isidnum_table
['.' - CH_EOF
] =
1441 (parse_flags
& PARSE_FLAG_ASM_FILE
) ? IS_ID
: 0;
1446 parse_flags
|= PARSE_FLAG_ACCEPT_STRAYS
| PARSE_FLAG_SPACES
| PARSE_FLAG_LINEFEED
;
1447 while (tok
!= TOK_LINEFEED
&& tok
!= TOK_EOF
) {
1448 /* remove spaces around ## and after '#' */
1449 if (TOK_TWOSHARPS
== tok
) {
1455 } else if ('#' == tok
) {
1457 } else if (check_space(tok
, &spc
)) {
1460 tok_str_add2(&tokstr_buf
, tok
, &tokc
);
1465 parse_flags
= saved_parse_flags
;
1467 --tokstr_buf
.len
; /* remove trailing space */
1468 tok_str_add(&tokstr_buf
, 0);
1471 tcc_error("'##' cannot appear at either end of macro");
1472 define_push(v
, t
, &tokstr_buf
, first
);
1475 static inline int hash_cached_include(const char *filename
)
1477 const unsigned char *s
;
1481 s
= (unsigned char *) filename
;
1483 h
= TOK_HASH_FUNC(h
, *s
);
1486 h
&= (CACHED_INCLUDES_HASH_SIZE
- 1);
1490 static CachedInclude
*search_cached_include(TCCState
*s1
, const char *filename
)
1494 h
= hash_cached_include(filename
);
1495 i
= s1
->cached_includes_hash
[h
];
1499 e
= s1
->cached_includes
[i
- 1];
1500 if (0 == PATHCMP(e
->filename
, filename
))
1507 static inline void add_cached_include(TCCState
*s1
, const char *filename
, int ifndef_macro
)
1512 if (search_cached_include(s1
, filename
))
1515 printf("adding cached '%s' %s\n", filename
, get_tok_str(ifndef_macro
, NULL
));
1517 e
= tcc_malloc(sizeof(CachedInclude
) + strlen(filename
));
1518 strcpy(e
->filename
, filename
);
1519 e
->ifndef_macro
= ifndef_macro
;
1520 dynarray_add((void ***)&s1
->cached_includes
, &s1
->nb_cached_includes
, e
);
1521 /* add in hash table */
1522 h
= hash_cached_include(filename
);
1523 e
->hash_next
= s1
->cached_includes_hash
[h
];
1524 s1
->cached_includes_hash
[h
] = s1
->nb_cached_includes
;
1527 #define ONCE_PREFIX "#ONCE#"
1529 static void pragma_parse(TCCState
*s1
)
1532 if (tok
== TOK_push_macro
|| tok
== TOK_pop_macro
) {
1536 if (next(), tok
!= '(')
1538 if (next(), tok
!= TOK_STR
)
1540 v
= tok_alloc(tokc
.str
.data
, tokc
.str
.size
- 1)->tok
;
1541 if (next(), tok
!= ')')
1543 if (t
== TOK_push_macro
) {
1544 while (NULL
== (s
= define_find(v
)))
1545 define_push(v
, 0, NULL
, NULL
);
1546 s
->type
.ref
= s
; /* set push boundary */
1548 for (s
= define_stack
; s
; s
= s
->prev
)
1549 if (s
->v
== v
&& s
->type
.ref
== s
) {
1555 table_ident
[v
- TOK_IDENT
]->sym_define
= s
->d
? s
: NULL
;
1557 tcc_warning("unbalanced #pragma pop_macro");
1558 pp_debug_tok
= t
, pp_debug_symv
= v
;
1560 } else if (tok
== TOK_once
) {
1561 char buf1
[sizeof(file
->filename
) + sizeof(ONCE_PREFIX
)];
1562 strcpy(buf1
, ONCE_PREFIX
);
1563 strcat(buf1
, file
->filename
);
1567 add_cached_include(s1
, file
->filename
, tok_alloc(buf1
, strlen(buf1
))->tok
);
1568 } else if (s1
->ppfp
) {
1569 /* tcc -E: keep pragmas below unchanged */
1571 unget_tok(TOK_PRAGMA
);
1573 unget_tok(TOK_LINEFEED
);
1575 } else if (tok
== TOK_pack
) {
1577 #pragma pack(1) // set
1578 #pragma pack() // reset to default
1579 #pragma pack(push,1) // push & set
1580 #pragma pack(pop) // restore previous */
1583 if (tok
== TOK_ASM_pop
) {
1585 if (s1
->pack_stack_ptr
<= s1
->pack_stack
) {
1587 tcc_error("out of pack stack");
1589 s1
->pack_stack_ptr
--;
1593 if (tok
== TOK_ASM_push
) {
1595 if (s1
->pack_stack_ptr
>= s1
->pack_stack
+ PACK_STACK_SIZE
- 1)
1597 s1
->pack_stack_ptr
++;
1600 if (tok
!= TOK_CINT
)
1603 if (val
< 1 || val
> 16 || (val
& (val
- 1)) != 0)
1607 *s1
->pack_stack_ptr
= val
;
1612 } else if (tok
== TOK_comment
) {
1622 file
= tcc_strdup((char *)tokc
.str
.data
);
1623 dynarray_add((void ***)&s1
->pragma_libs
, &s1
->nb_pragma_libs
, file
);
1629 if (s1
->warn_unsupported
)
1630 tcc_warning("#pragma %s is ignored", get_tok_str(tok
, &tokc
));
1635 tcc_error("malformed #pragma directive");
1639 /* is_bof is true if first non space token at beginning of file */
1640 ST_FUNC
void preprocess(int is_bof
)
1642 TCCState
*s1
= tcc_state
;
1643 int i
, c
, n
, saved_parse_flags
;
1647 saved_parse_flags
= parse_flags
;
1648 parse_flags
= PARSE_FLAG_PREPROCESS
1649 | PARSE_FLAG_TOK_NUM
1650 | PARSE_FLAG_TOK_STR
1651 | PARSE_FLAG_LINEFEED
1652 | (parse_flags
& PARSE_FLAG_ASM_FILE
)
1661 pp_debug_symv
= tok
;
1667 pp_debug_symv
= tok
;
1668 s
= define_find(tok
);
1669 /* undefine symbol by putting an invalid name */
1674 case TOK_INCLUDE_NEXT
:
1675 ch
= file
->buf_ptr
[0];
1676 /* XXX: incorrect if comments : use next_nomacro with a special mode */
1681 } else if (ch
== '\"') {
1686 while (ch
!= c
&& ch
!= '\n' && ch
!= CH_EOF
) {
1687 if ((q
- buf
) < sizeof(buf
) - 1)
1690 if (handle_stray_noerror() == 0)
1698 /* eat all spaces and comments after include */
1699 /* XXX: slightly incorrect */
1700 while (ch1
!= '\n' && ch1
!= CH_EOF
)
1704 /* computed #include : either we have only strings or
1705 we have anything enclosed in '<>' */
1708 if (tok
== TOK_STR
) {
1709 while (tok
!= TOK_LINEFEED
) {
1710 if (tok
!= TOK_STR
) {
1712 tcc_error("'#include' expects \"FILENAME\" or <FILENAME>");
1714 pstrcat(buf
, sizeof(buf
), (char *)tokc
.str
.data
);
1720 while (tok
!= TOK_LINEFEED
) {
1721 pstrcat(buf
, sizeof(buf
), get_tok_str(tok
, &tokc
));
1725 /* check syntax and remove '<>' */
1726 if (len
< 2 || buf
[0] != '<' || buf
[len
- 1] != '>')
1727 goto include_syntax
;
1728 memmove(buf
, buf
+ 1, len
- 2);
1729 buf
[len
- 2] = '\0';
1734 if (s1
->include_stack_ptr
>= s1
->include_stack
+ INCLUDE_STACK_SIZE
)
1735 tcc_error("#include recursion too deep");
1736 /* store current file in stack, but increment stack later below */
1737 *s1
->include_stack_ptr
= file
;
1738 i
= tok
== TOK_INCLUDE_NEXT
? file
->include_next_index
: 0;
1739 n
= 2 + s1
->nb_include_paths
+ s1
->nb_sysinclude_paths
;
1740 for (; i
< n
; ++i
) {
1741 char buf1
[sizeof file
->filename
];
1746 /* check absolute include path */
1747 if (!IS_ABSPATH(buf
))
1751 } else if (i
== 1) {
1752 /* search in current dir if "header.h" */
1755 path
= file
->filename
;
1756 pstrncpy(buf1
, path
, tcc_basename(path
) - path
);
1759 /* search in all the include paths */
1760 int j
= i
- 2, k
= j
- s1
->nb_include_paths
;
1761 path
= k
< 0 ? s1
->include_paths
[j
] : s1
->sysinclude_paths
[k
];
1762 if (path
== 0) continue;
1763 pstrcpy(buf1
, sizeof(buf1
), path
);
1764 pstrcat(buf1
, sizeof(buf1
), "/");
1767 pstrcat(buf1
, sizeof(buf1
), buf
);
1768 e
= search_cached_include(s1
, buf1
);
1769 if (e
&& define_find(e
->ifndef_macro
)) {
1770 /* no need to parse the include because the 'ifndef macro'
1773 printf("%s: skipping cached %s\n", file
->filename
, buf1
);
1778 if (tcc_open(s1
, buf1
) < 0)
1781 file
->include_next_index
= i
+ 1;
1783 printf("%s: including %s\n", file
->prev
->filename
, file
->filename
);
1785 /* update target deps */
1786 dynarray_add((void ***)&s1
->target_deps
, &s1
->nb_target_deps
,
1788 /* push current file in stack */
1789 ++s1
->include_stack_ptr
;
1790 /* add include file debug info */
1792 put_stabs(file
->filename
, N_BINCL
, 0, 0, 0);
1793 tok_flags
|= TOK_FLAG_BOF
| TOK_FLAG_BOL
;
1794 ch
= file
->buf_ptr
[0];
1797 tcc_error("include file '%s' not found", buf
);
1804 c
= expr_preprocess();
1810 if (tok
< TOK_IDENT
)
1811 tcc_error("invalid argument for '#if%sdef'", c
? "n" : "");
1815 printf("#ifndef %s\n", get_tok_str(tok
, NULL
));
1817 file
->ifndef_macro
= tok
;
1820 c
= (define_find(tok
) != 0) ^ c
;
1822 if (s1
->ifdef_stack_ptr
>= s1
->ifdef_stack
+ IFDEF_STACK_SIZE
)
1823 tcc_error("memory full (ifdef)");
1824 *s1
->ifdef_stack_ptr
++ = c
;
1827 if (s1
->ifdef_stack_ptr
== s1
->ifdef_stack
)
1828 tcc_error("#else without matching #if");
1829 if (s1
->ifdef_stack_ptr
[-1] & 2)
1830 tcc_error("#else after #else");
1831 c
= (s1
->ifdef_stack_ptr
[-1] ^= 3);
1834 if (s1
->ifdef_stack_ptr
== s1
->ifdef_stack
)
1835 tcc_error("#elif without matching #if");
1836 c
= s1
->ifdef_stack_ptr
[-1];
1838 tcc_error("#elif after #else");
1839 /* last #if/#elif expression was true: we skip */
1842 c
= expr_preprocess();
1843 s1
->ifdef_stack_ptr
[-1] = c
;
1845 if (s1
->ifdef_stack_ptr
== file
->ifdef_stack_ptr
+ 1)
1846 file
->ifndef_macro
= 0;
1856 if (s1
->ifdef_stack_ptr
<= file
->ifdef_stack_ptr
)
1857 tcc_error("#endif without matching #if");
1858 s1
->ifdef_stack_ptr
--;
1859 /* '#ifndef macro' was at the start of file. Now we check if
1860 an '#endif' is exactly at the end of file */
1861 if (file
->ifndef_macro
&&
1862 s1
->ifdef_stack_ptr
== file
->ifdef_stack_ptr
) {
1863 file
->ifndef_macro_saved
= file
->ifndef_macro
;
1864 /* need to set to zero to avoid false matches if another
1865 #ifndef at middle of file */
1866 file
->ifndef_macro
= 0;
1867 while (tok
!= TOK_LINEFEED
)
1869 tok_flags
|= TOK_FLAG_ENDIF
;
1874 n
= strtoul((char*)tokc
.str
.data
, &q
, 10);
1878 if (tok
!= TOK_CINT
)
1880 tcc_error("wrong #line format");
1884 if (tok
!= TOK_LINEFEED
) {
1886 pstrcpy(file
->filename
, sizeof(file
->filename
), (char *)tokc
.str
.data
);
1887 else if (parse_flags
& PARSE_FLAG_ASM_FILE
)
1894 total_lines
+= file
->line_num
- n
;
1897 put_stabs(file
->filename
, N_BINCL
, 0, 0, 0);
1902 ch
= file
->buf_ptr
[0];
1905 while (ch
!= '\n' && ch
!= CH_EOF
) {
1906 if ((q
- buf
) < sizeof(buf
) - 1)
1909 if (handle_stray_noerror() == 0)
1916 tcc_error("#error %s", buf
);
1918 tcc_warning("#warning %s", buf
);
1926 /* ignore gas line comment in an 'S' file. */
1927 if (saved_parse_flags
& PARSE_FLAG_ASM_FILE
)
1929 if (tok
== '!' && is_bof
)
1930 /* '!' is ignored at beginning to allow C scripts. */
1932 tcc_warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok
, &tokc
));
1934 file
->buf_ptr
= parse_line_comment(file
->buf_ptr
);
1937 /* ignore other preprocess commands or #! for C scripts */
1938 while (tok
!= TOK_LINEFEED
)
1941 parse_flags
= saved_parse_flags
;
1944 /* evaluate escape codes in a string. */
1945 static void parse_escape_string(CString
*outstr
, const uint8_t *buf
, int is_long
)
1960 case '0': case '1': case '2': case '3':
1961 case '4': case '5': case '6': case '7':
1962 /* at most three octal digits */
1967 n
= n
* 8 + c
- '0';
1971 n
= n
* 8 + c
- '0';
1976 goto add_char_nonext
;
1984 if (c
>= 'a' && c
<= 'f')
1986 else if (c
>= 'A' && c
<= 'F')
1996 goto add_char_nonext
;
2020 goto invalid_escape
;
2030 if (c
>= '!' && c
<= '~')
2031 tcc_warning("unknown escape sequence: \'\\%c\'", c
);
2033 tcc_warning("unknown escape sequence: \'\\x%x\'", c
);
2040 cstr_ccat(outstr
, c
);
2042 cstr_wccat(outstr
, c
);
2044 /* add a trailing '\0' */
2046 cstr_ccat(outstr
, '\0');
2048 cstr_wccat(outstr
, '\0');
2051 void parse_string(const char *s
, int len
)
2053 uint8_t buf
[1000], *p
= buf
;
2056 if ((is_long
= *s
== 'L'))
2060 if (len
>= sizeof buf
)
2061 p
= tcc_malloc(len
+ 1);
2065 cstr_reset(&tokcstr
);
2066 parse_escape_string(&tokcstr
, p
, is_long
);
2072 /* XXX: make it portable */
2076 char_size
= sizeof(nwchar_t
);
2077 if (tokcstr
.size
<= char_size
)
2078 tcc_error("empty character constant");
2079 if (tokcstr
.size
> 2 * char_size
)
2080 tcc_warning("multi-character character constant");
2082 tokc
.i
= *(int8_t *)tokcstr
.data
;
2085 tokc
.i
= *(nwchar_t
*)tokcstr
.data
;
2089 tokc
.str
.size
= tokcstr
.size
;
2090 tokc
.str
.data
= tokcstr
.data
;
2091 tokc
.str
.data_allocated
= tokcstr
.data_allocated
;
2099 /* we use 64 bit numbers */
2102 /* bn = (bn << shift) | or_val */
2103 static void bn_lshift(unsigned int *bn
, int shift
, int or_val
)
2107 for(i
=0;i
<BN_SIZE
;i
++) {
2109 bn
[i
] = (v
<< shift
) | or_val
;
2110 or_val
= v
>> (32 - shift
);
2114 static void bn_zero(unsigned int *bn
)
2117 for(i
=0;i
<BN_SIZE
;i
++) {
2122 /* parse number in null terminated string 'p' and return it in the
2124 static void parse_number(const char *p
)
2126 int b
, t
, shift
, frac_bits
, s
, exp_val
, ch
;
2128 unsigned int bn
[BN_SIZE
];
2139 goto float_frac_parse
;
2140 } else if (t
== '0') {
2141 if (ch
== 'x' || ch
== 'X') {
2145 } else if (tcc_ext
&& (ch
== 'b' || ch
== 'B')) {
2151 /* parse all digits. cannot check octal numbers at this stage
2152 because of floating point constants */
2154 if (ch
>= 'a' && ch
<= 'f')
2156 else if (ch
>= 'A' && ch
<= 'F')
2164 if (q
>= token_buf
+ STRING_MAX_SIZE
) {
2166 tcc_error("number too long");
2172 ((ch
== 'e' || ch
== 'E') && b
== 10) ||
2173 ((ch
== 'p' || ch
== 'P') && (b
== 16 || b
== 2))) {
2175 /* NOTE: strtox should support that for hexa numbers, but
2176 non ISOC99 libcs do not support it, so we prefer to do
2178 /* hexadecimal or binary floats */
2179 /* XXX: handle overflows */
2191 } else if (t
>= 'a') {
2193 } else if (t
>= 'A') {
2198 bn_lshift(bn
, shift
, t
);
2205 if (t
>= 'a' && t
<= 'f') {
2207 } else if (t
>= 'A' && t
<= 'F') {
2209 } else if (t
>= '0' && t
<= '9') {
2215 tcc_error("invalid digit");
2216 bn_lshift(bn
, shift
, t
);
2221 if (ch
!= 'p' && ch
!= 'P')
2228 } else if (ch
== '-') {
2232 if (ch
< '0' || ch
> '9')
2233 expect("exponent digits");
2234 while (ch
>= '0' && ch
<= '9') {
2235 exp_val
= exp_val
* 10 + ch
- '0';
2238 exp_val
= exp_val
* s
;
2240 /* now we can generate the number */
2241 /* XXX: should patch directly float number */
2242 d
= (double)bn
[1] * 4294967296.0 + (double)bn
[0];
2243 d
= ldexp(d
, exp_val
- frac_bits
);
2248 /* float : should handle overflow */
2250 } else if (t
== 'L') {
2252 #ifdef TCC_TARGET_PE
2257 /* XXX: not large enough */
2258 tokc
.ld
= (long double)d
;
2265 /* decimal floats */
2267 if (q
>= token_buf
+ STRING_MAX_SIZE
)
2272 while (ch
>= '0' && ch
<= '9') {
2273 if (q
>= token_buf
+ STRING_MAX_SIZE
)
2279 if (ch
== 'e' || ch
== 'E') {
2280 if (q
>= token_buf
+ STRING_MAX_SIZE
)
2284 if (ch
== '-' || ch
== '+') {
2285 if (q
>= token_buf
+ STRING_MAX_SIZE
)
2290 if (ch
< '0' || ch
> '9')
2291 expect("exponent digits");
2292 while (ch
>= '0' && ch
<= '9') {
2293 if (q
>= token_buf
+ STRING_MAX_SIZE
)
2305 tokc
.f
= strtof(token_buf
, NULL
);
2306 } else if (t
== 'L') {
2308 #ifdef TCC_TARGET_PE
2310 tokc
.d
= strtod(token_buf
, NULL
);
2313 tokc
.ld
= strtold(token_buf
, NULL
);
2317 tokc
.d
= strtod(token_buf
, NULL
);
2321 unsigned long long n
, n1
;
2322 int lcount
, ucount
, must_64bit
;
2325 /* integer number */
2328 if (b
== 10 && *q
== '0') {
2335 /* no need for checks except for base 10 / 8 errors */
2345 tcc_error("invalid digit");
2348 /* detect overflow */
2349 /* XXX: this test is not reliable */
2351 tcc_error("integer constant overflow");
2354 /* Determine the characteristics (unsigned and/or 64bit) the type of
2355 the constant must have according to the constant suffix(es) */
2356 lcount
= ucount
= must_64bit
= 0;
2362 tcc_error("three 'l's in integer constant");
2363 if (lcount
&& *(p
- 1) != ch
)
2364 tcc_error("incorrect integer suffix: %s", p1
);
2366 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
2371 } else if (t
== 'U') {
2373 tcc_error("two 'u's in integer constant");
2381 /* Whether 64 bits are needed to hold the constant's value */
2382 if (n
& 0xffffffff00000000LL
|| must_64bit
) {
2390 /* Whether type must be unsigned to hold the constant's value */
2391 if (ucount
|| ((n1
>> 31) && (b
!= 10))) {
2392 if (tok
== TOK_CLLONG
)
2396 /* If decimal and no unsigned suffix, bump to 64 bits or throw error */
2397 } else if (n1
>> 31) {
2398 if (tok
== TOK_CINT
)
2401 tcc_error("integer constant overflow");
2404 if (tok
== TOK_CINT
|| tok
== TOK_CUINT
)
2410 tcc_error("invalid number\n");
2414 #define PARSE2(c1, tok1, c2, tok2) \
2425 /* return next token without macro substitution */
2426 static inline void next_nomacro1(void)
2428 int t
, c
, is_long
, len
;
2436 #if (__TINYC__ || __GNUC__)
2439 goto parse_ident_fast
;
2446 if (parse_flags
& PARSE_FLAG_SPACES
)
2447 goto keep_tok_flags
;
2448 while (isidnum_table
[*p
- CH_EOF
] & IS_SPC
)
2457 /* first look if it is in fact an end of buffer */
2458 c
= handle_stray1(p
);
2465 TCCState
*s1
= tcc_state
;
2466 if ((parse_flags
& PARSE_FLAG_LINEFEED
)
2467 && !(tok_flags
& TOK_FLAG_EOF
)) {
2468 tok_flags
|= TOK_FLAG_EOF
;
2470 goto keep_tok_flags
;
2471 } else if (!(parse_flags
& PARSE_FLAG_PREPROCESS
)) {
2473 } else if (s1
->ifdef_stack_ptr
!= file
->ifdef_stack_ptr
) {
2474 tcc_error("missing #endif");
2475 } else if (s1
->include_stack_ptr
== s1
->include_stack
) {
2476 /* no include left : end of file. */
2479 tok_flags
&= ~TOK_FLAG_EOF
;
2480 /* pop include file */
2482 /* test if previous '#endif' was after a #ifdef at
2484 if (tok_flags
& TOK_FLAG_ENDIF
) {
2486 printf("#endif %s\n", get_tok_str(file
->ifndef_macro_saved
, NULL
));
2488 add_cached_include(s1
, file
->filename
, file
->ifndef_macro_saved
);
2489 tok_flags
&= ~TOK_FLAG_ENDIF
;
2492 /* add end of include file debug info */
2493 if (tcc_state
->do_debug
) {
2494 put_stabd(N_EINCL
, 0, 0);
2496 /* pop include stack */
2498 s1
->include_stack_ptr
--;
2507 tok_flags
|= TOK_FLAG_BOL
;
2510 if (0 == (parse_flags
& PARSE_FLAG_LINEFEED
))
2513 goto keep_tok_flags
;
2518 if ((tok_flags
& TOK_FLAG_BOL
) &&
2519 (parse_flags
& PARSE_FLAG_PREPROCESS
)) {
2521 preprocess(tok_flags
& TOK_FLAG_BOF
);
2527 tok
= TOK_TWOSHARPS
;
2529 if (parse_flags
& PARSE_FLAG_ASM_FILE
) {
2530 p
= parse_line_comment(p
- 1);
2539 /* dollar is allowed to start identifiers when not parsing asm */
2541 if (!(isidnum_table
[c
- CH_EOF
] & IS_ID
)
2542 || (parse_flags
& PARSE_FLAG_ASM_FILE
))
2545 #if (__TINYC__ || __GNUC__)
2552 case 'a': case 'b': case 'c': case 'd':
2553 case 'e': case 'f': case 'g': case 'h':
2554 case 'i': case 'j': case 'k': case 'l':
2555 case 'm': case 'n': case 'o': case 'p':
2556 case 'q': case 'r': case 's': case 't':
2557 case 'u': case 'v': case 'w': case 'x':
2559 case 'A': case 'B': case 'C': case 'D':
2560 case 'E': case 'F': case 'G': case 'H':
2561 case 'I': case 'J': case 'K':
2562 case 'M': case 'N': case 'O': case 'P':
2563 case 'Q': case 'R': case 'S': case 'T':
2564 case 'U': case 'V': case 'W': case 'X':
2571 h
= TOK_HASH_FUNC(h
, c
);
2572 while (c
= *++p
, isidnum_table
[c
- CH_EOF
] & (IS_ID
|IS_NUM
))
2573 h
= TOK_HASH_FUNC(h
, c
);
2578 /* fast case : no stray found, so we have the full token
2579 and we have already hashed it */
2580 h
&= (TOK_HASH_SIZE
- 1);
2581 pts
= &hash_ident
[h
];
2586 if (ts
->len
== len
&& !memcmp(ts
->str
, p1
, len
))
2588 pts
= &(ts
->hash_next
);
2590 ts
= tok_alloc_new(pts
, (char *) p1
, len
);
2594 cstr_reset(&tokcstr
);
2595 cstr_cat(&tokcstr
, p1
, len
);
2599 while (isidnum_table
[c
- CH_EOF
] & (IS_ID
|IS_NUM
))
2601 cstr_ccat(&tokcstr
, c
);
2604 ts
= tok_alloc(tokcstr
.data
, tokcstr
.size
);
2610 if (t
!= '\\' && t
!= '\'' && t
!= '\"') {
2612 goto parse_ident_fast
;
2615 if (c
== '\'' || c
== '\"') {
2619 cstr_reset(&tokcstr
);
2620 cstr_ccat(&tokcstr
, 'L');
2621 goto parse_ident_slow
;
2626 case '0': case '1': case '2': case '3':
2627 case '4': case '5': case '6': case '7':
2629 cstr_reset(&tokcstr
);
2630 /* after the first digit, accept digits, alpha, '.' or sign if
2631 prefixed by 'eEpP' */
2635 cstr_ccat(&tokcstr
, c
);
2637 if (!((isidnum_table
[c
- CH_EOF
] & (IS_ID
|IS_NUM
))
2639 || ((c
== '+' || c
== '-')
2640 && (t
== 'e' || t
== 'E' || t
== 'p' || t
== 'P')
2641 && !(parse_flags
& PARSE_FLAG_ASM_FILE
)
2645 /* We add a trailing '\0' to ease parsing */
2646 cstr_ccat(&tokcstr
, '\0');
2647 tokc
.str
.size
= tokcstr
.size
;
2648 tokc
.str
.data
= tokcstr
.data
;
2649 tokc
.str
.data_allocated
= tokcstr
.data_allocated
;
2654 /* special dot handling because it can also start a number */
2657 cstr_reset(&tokcstr
);
2658 cstr_ccat(&tokcstr
, '.');
2660 } else if ((parse_flags
& PARSE_FLAG_ASM_FILE
)
2661 && (isidnum_table
[c
- CH_EOF
] & (IS_ID
|IS_NUM
))) {
2663 goto parse_ident_fast
;
2664 } else if (c
== '.') {
2670 *--p
= '.'; /* may underflow into file->unget[] */
2681 cstr_reset(&tokcstr
);
2683 cstr_ccat(&tokcstr
, 'L');
2684 cstr_ccat(&tokcstr
, c
);
2685 p
= parse_pp_string(p
, c
, &tokcstr
);
2686 cstr_ccat(&tokcstr
, c
);
2687 cstr_ccat(&tokcstr
, '\0');
2688 tokc
.str
.size
= tokcstr
.size
;
2689 tokc
.str
.data
= tokcstr
.data
;
2690 tokc
.str
.data_allocated
= tokcstr
.data_allocated
;
2699 } else if (c
== '<') {
2716 } else if (c
== '>') {
2734 } else if (c
== '=') {
2747 } else if (c
== '=') {
2760 } else if (c
== '=') {
2773 } else if (c
== '=') {
2776 } else if (c
== '>') {
2784 PARSE2('!', '!', '=', TOK_NE
)
2785 PARSE2('=', '=', '=', TOK_EQ
)
2786 PARSE2('*', '*', '=', TOK_A_MUL
)
2787 PARSE2('%', '%', '=', TOK_A_MOD
)
2788 PARSE2('^', '^', '=', TOK_A_XOR
)
2790 /* comments or operator */
2794 p
= parse_comment(p
);
2795 /* comments replaced by a blank */
2797 goto keep_tok_flags
;
2798 } else if (c
== '/') {
2799 p
= parse_line_comment(p
);
2801 goto keep_tok_flags
;
2802 } else if (c
== '=') {
2822 case '@': /* only used in assembler */
2828 if (parse_flags
& PARSE_FLAG_ASM_FILE
)
2830 tcc_error("unrecognized character \\x%02x", c
);
2836 #if defined(PARSE_DEBUG)
2837 printf("token = %s\n", get_tok_str(tok
, &tokc
));
2841 /* return next token without macro substitution. Can read input from
2843 static void next_nomacro_spc(void)
2849 TOK_GET(&tok
, ¯o_ptr
, &tokc
);
2850 if (tok
== TOK_LINENUM
) {
2851 file
->line_num
= tokc
.i
;
2860 ST_FUNC
void next_nomacro(void)
2864 } while (tok
< 256 && (isidnum_table
[tok
- CH_EOF
] & IS_SPC
));
2868 static void macro_subst(
2869 TokenString
*tok_str
,
2871 const int *macro_str
,
2875 /* substitute arguments in replacement lists in macro_str by the values in
2876 args (field d) and return allocated string */
2877 static int *macro_arg_subst(Sym
**nested_list
, const int *macro_str
, Sym
*args
)
2889 TOK_GET(&t
, ¯o_str
, &cval
);
2894 TOK_GET(&t
, ¯o_str
, &cval
);
2897 s
= sym_find2(args
, t
);
2900 cstr_ccat(&cstr
, '\"');
2904 TOK_GET(&t
, &st
, &cval
);
2905 if (t
!= TOK_PLCHLDR
2907 && 0 == check_space(t
, &spc
)) {
2908 const char *s
= get_tok_str(t
, &cval
);
2910 if (t
== TOK_PPSTR
&& *s
!= '\'')
2911 add_char(&cstr
, *s
);
2913 cstr_ccat(&cstr
, *s
);
2919 cstr_ccat(&cstr
, '\"');
2920 cstr_ccat(&cstr
, '\0');
2922 printf("\nstringize: <%s>\n", (char *)cstr
.data
);
2925 cval
.str
.size
= cstr
.size
;
2926 cval
.str
.data
= cstr
.data
;
2927 cval
.str
.data_allocated
= cstr
.data_allocated
;
2928 tok_str_add2(&str
, TOK_PPSTR
, &cval
);
2932 expect("macro parameter after '#'");
2934 } else if (t
>= TOK_IDENT
) {
2935 s
= sym_find2(args
, t
);
2939 /* if '##' is present before or after, no arg substitution */
2940 if (*macro_str
== TOK_TWOSHARPS
|| t1
== TOK_TWOSHARPS
) {
2941 /* special case for var arg macros : ## eats the ','
2942 if empty VA_ARGS variable. */
2943 if (t1
== TOK_TWOSHARPS
&& t0
== ',' && gnu_ext
&& s
->type
.t
) {
2945 /* suppress ',' '##' */
2948 /* suppress '##' and add variable */
2955 TOK_GET(&t1
, &st
, &cval
);
2958 tok_str_add2(&str
, t1
, &cval
);
2964 /* NOTE: the stream cannot be read when macro
2965 substituing an argument */
2966 macro_subst(&str
, nested_list
, st
, 0);
2968 if (str
.len
== l0
) /* exanded to empty string */
2969 tok_str_add(&str
, TOK_PLCHLDR
);
2971 tok_str_add(&str
, t
);
2974 tok_str_add2(&str
, t
, &cval
);
2978 tok_str_add(&str
, 0);
2982 static char const ab_month_name
[12][4] =
2984 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2985 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
2988 /* peek or read [ws_str == NULL] next token from function macro call,
2989 walking up macro levels up to the file if necessary */
2990 static int next_argstream(Sym
**nested_list
, int can_read_stream
, TokenString
*ws_str
)
2998 p
= macro_ptr
, t
= *p
;
3000 while (is_space(t
) || TOK_LINEFEED
== t
)
3001 tok_str_add(ws_str
, t
), t
= *++p
;
3003 if (t
== 0 && can_read_stream
) {
3005 /* also, end of scope for nested defined symbol */
3007 while (sa
&& sa
->v
== 0)
3016 while (is_space(ch
) || ch
== '\n' || ch
== '/') {
3019 uint8_t *p
= file
->buf_ptr
;
3022 p
= parse_comment(p
);
3023 file
->buf_ptr
= p
- 1;
3024 } else if (c
== '/') {
3025 p
= parse_line_comment(p
);
3026 file
->buf_ptr
= p
- 1;
3031 tok_str_add(ws_str
, ch
);
3045 /* do macro substitution of current token with macro 's' and add
3046 result to (tok_str,tok_len). 'nested_list' is the list of all
3047 macros we got inside to avoid recursing. Return non zero if no
3048 substitution needs to be done */
3049 static int macro_subst_tok(
3050 TokenString
*tok_str
,
3053 int can_read_stream
)
3055 Sym
*args
, *sa
, *sa1
;
3056 int parlevel
, *mstr
, t
, t1
, spc
;
3063 /* if symbol is a macro, prepare substitution */
3064 /* special macros */
3065 if (tok
== TOK___LINE__
) {
3066 snprintf(buf
, sizeof(buf
), "%d", file
->line_num
);
3070 } else if (tok
== TOK___FILE__
) {
3071 cstrval
= file
->filename
;
3073 } else if (tok
== TOK___DATE__
|| tok
== TOK___TIME__
) {
3078 tm
= localtime(&ti
);
3079 if (tok
== TOK___DATE__
) {
3080 snprintf(buf
, sizeof(buf
), "%s %2d %d",
3081 ab_month_name
[tm
->tm_mon
], tm
->tm_mday
, tm
->tm_year
+ 1900);
3083 snprintf(buf
, sizeof(buf
), "%02d:%02d:%02d",
3084 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
3091 cstr_cat(&cstr
, cstrval
, 0);
3092 cval
.str
.size
= cstr
.size
;
3093 cval
.str
.data
= cstr
.data
;
3094 cval
.str
.data_allocated
= cstr
.data_allocated
;
3095 tok_str_add2(tok_str
, t1
, &cval
);
3098 int saved_parse_flags
= parse_flags
;
3101 if (s
->type
.t
== MACRO_FUNC
) {
3102 /* whitespace between macro name and argument list */
3104 tok_str_new(&ws_str
);
3107 parse_flags
|= PARSE_FLAG_SPACES
| PARSE_FLAG_LINEFEED
3108 | PARSE_FLAG_ACCEPT_STRAYS
;
3110 /* get next token from argument stream */
3111 t
= next_argstream(nested_list
, can_read_stream
, &ws_str
);
3113 /* not a macro substitution after all, restore the
3114 * macro token plus all whitespace we've read.
3115 * whitespace is intentionally not merged to preserve
3117 parse_flags
= saved_parse_flags
;
3118 tok_str_add(tok_str
, tok
);
3119 if (parse_flags
& PARSE_FLAG_SPACES
) {
3121 for (i
= 0; i
< ws_str
.len
; i
++)
3122 tok_str_add(tok_str
, ws_str
.str
[i
]);
3124 tok_str_free(ws_str
.str
);
3127 tok_str_free(ws_str
.str
);
3129 next_nomacro(); /* eat '(' */
3131 /* argument macro */
3134 /* NOTE: empty args are allowed, except if no args */
3137 next_argstream(nested_list
, can_read_stream
, NULL
);
3138 } while (is_space(tok
) || TOK_LINEFEED
== tok
);
3140 /* handle '()' case */
3141 if (!args
&& !sa
&& tok
== ')')
3144 tcc_error("macro '%s' used with too many args",
3145 get_tok_str(s
->v
, 0));
3148 /* NOTE: non zero sa->t indicates VA_ARGS */
3149 while ((parlevel
> 0 ||
3151 (tok
!= ',' || sa
->type
.t
)))) {
3152 if (tok
== TOK_EOF
|| tok
== 0)
3156 else if (tok
== ')')
3158 if (tok
== TOK_LINEFEED
)
3160 if (!check_space(tok
, &spc
))
3161 tok_str_add2(&str
, tok
, &tokc
);
3162 next_argstream(nested_list
, can_read_stream
, NULL
);
3167 tok_str_add(&str
, 0);
3168 sa1
= sym_push2(&args
, sa
->v
& ~SYM_FIELD
, sa
->type
.t
, 0);
3172 /* special case for gcc var args: add an empty
3173 var arg argument if it is omitted */
3174 if (sa
&& sa
->type
.t
&& gnu_ext
)
3182 tcc_error("macro '%s' used with too few args",
3183 get_tok_str(s
->v
, 0));
3186 parse_flags
= saved_parse_flags
;
3188 /* now subst each arg */
3189 mstr
= macro_arg_subst(nested_list
, mstr
, args
);
3194 tok_str_free(sa
->d
);
3200 sym_push2(nested_list
, s
->v
, 0, 0);
3201 parse_flags
= saved_parse_flags
;
3202 macro_subst(tok_str
, nested_list
, mstr
, can_read_stream
| 2);
3204 /* pop nested defined symbol */
3206 *nested_list
= sa1
->prev
;
3214 int paste_tokens(int t1
, CValue
*v1
, int t2
, CValue
*v2
)
3220 if (t1
!= TOK_PLCHLDR
)
3221 cstr_cat(&cstr
, get_tok_str(t1
, v1
), -1);
3223 if (t2
!= TOK_PLCHLDR
)
3224 cstr_cat(&cstr
, get_tok_str(t2
, v2
), -1);
3225 cstr_ccat(&cstr
, '\0');
3227 tcc_open_bf(tcc_state
, ":paste:", cstr
.size
);
3228 memcpy(file
->buffer
, cstr
.data
, cstr
.size
);
3231 if (0 == *file
->buf_ptr
)
3235 tcc_warning("pasting <%.*s> and <%s> does not give a valid preprocessing token",
3236 n
, cstr
.data
, (char*)cstr
.data
+ n
);
3241 //printf("paste <%s>\n", (char*)cstr.data);
3246 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
3247 return the resulting string (which must be freed). */
3248 static inline int *macro_twosharps(const int *ptr0
)
3252 TokenString macro_str1
;
3253 int start_of_nosubsts
= -1;
3256 /* we search the first '##' */
3257 for (ptr
= ptr0
;;) {
3258 TOK_GET(&t
, &ptr
, &cval
);
3259 if (t
== TOK_TWOSHARPS
)
3265 tok_str_new(¯o_str1
);
3267 //tok_print(" $$$", ptr0);
3268 for (ptr
= ptr0
;;) {
3269 TOK_GET(&t
, &ptr
, &cval
);
3272 if (t
== TOK_TWOSHARPS
)
3274 while (*ptr
== TOK_TWOSHARPS
) {
3276 /* given 'a##b', remove nosubsts preceding 'a' */
3277 if (start_of_nosubsts
>= 0)
3278 macro_str1
.len
= start_of_nosubsts
;
3279 /* given 'a##b', remove nosubsts preceding 'b' */
3280 while ((t1
= *++ptr
) == TOK_NOSUBST
)
3282 if (t1
&& t1
!= TOK_TWOSHARPS
3283 && t1
!= ':') /* 'a##:' don't build a new token */
3285 TOK_GET(&t1
, &ptr
, &cv1
);
3286 if (t
!= TOK_PLCHLDR
|| t1
!= TOK_PLCHLDR
) {
3287 paste_tokens(t
, &cval
, t1
, &cv1
);
3288 t
= tok
, cval
= tokc
;
3292 if (t
== TOK_NOSUBST
) {
3293 if (start_of_nosubsts
< 0)
3294 start_of_nosubsts
= macro_str1
.len
;
3296 start_of_nosubsts
= -1;
3298 tok_str_add2(¯o_str1
, t
, &cval
);
3300 tok_str_add(¯o_str1
, 0);
3301 //tok_print(" ###", macro_str1.str);
3302 return macro_str1
.str
;
3305 /* do macro substitution of macro_str and add result to
3306 (tok_str,tok_len). 'nested_list' is the list of all macros we got
3307 inside to avoid recursing. */
3308 static void macro_subst(
3309 TokenString
*tok_str
,
3311 const int *macro_str
,
3317 int t
, spc
, nosubst
;
3319 int *macro_str1
= NULL
;
3321 /* first scan for '##' operator handling */
3325 /* first scan for '##' operator handling */
3326 if (can_read_stream
& 1) {
3327 macro_str1
= macro_twosharps(ptr
);
3333 TOK_GET(&t
, &ptr
, &cval
);
3337 if (t
>= TOK_IDENT
&& 0 == nosubst
) {
3342 /* if nested substitution, do nothing */
3343 if (sym_find2(*nested_list
, t
)) {
3344 /* and mark it as TOK_NOSUBST, so it doesn't get subst'd again */
3345 tok_str_add2(tok_str
, TOK_NOSUBST
, NULL
);
3351 str
.str
= (int*)ptr
;
3352 begin_macro(&str
, 2);
3355 macro_subst_tok(tok_str
, nested_list
, s
, can_read_stream
);
3357 if (str
.alloc
== 3) {
3358 /* already finished by reading function macro arguments */
3366 spc
= (tok_str
->len
&&
3367 is_space(tok_last(tok_str
->str
,
3368 tok_str
->str
+ tok_str
->len
)));
3372 if (t
== '\\' && !(parse_flags
& PARSE_FLAG_ACCEPT_STRAYS
))
3373 tcc_error("stray '\\' in program");
3376 if (!check_space(t
, &spc
))
3377 tok_str_add2(tok_str
, t
, &cval
);
3379 if (t
== TOK_NOSUBST
)
3384 tok_str_free(macro_str1
);
3388 /* return next token with macro substitution */
3389 ST_FUNC
void next(void)
3392 if (parse_flags
& PARSE_FLAG_SPACES
)
3398 if (tok
== TOK_NOSUBST
|| tok
== TOK_PLCHLDR
) {
3399 /* discard preprocessor markers */
3401 } else if (tok
== 0) {
3402 /* end of macro or unget token string */
3406 } else if (tok
>= TOK_IDENT
&& (parse_flags
& PARSE_FLAG_PREPROCESS
)) {
3408 /* if reading from file, try to substitute macros */
3409 s
= define_find(tok
);
3411 Sym
*nested_list
= NULL
;
3414 macro_subst_tok(&tokstr_buf
, &nested_list
, s
, 1);
3415 tok_str_add(&tokstr_buf
, 0);
3416 begin_macro(&tokstr_buf
, 2);
3420 /* convert preprocessor tokens into C tokens */
3421 if (tok
== TOK_PPNUM
) {
3422 if (parse_flags
& PARSE_FLAG_TOK_NUM
)
3423 parse_number((char *)tokc
.str
.data
);
3424 } else if (tok
== TOK_PPSTR
) {
3425 if (parse_flags
& PARSE_FLAG_TOK_STR
)
3426 parse_string((char *)tokc
.str
.data
, tokc
.str
.size
- 1);
3430 /* push back current token and set current token to 'last_tok'. Only
3431 identifier case handled for labels. */
3432 ST_INLN
void unget_tok(int last_tok
)
3434 TokenString
*str
= tcc_malloc(sizeof *str
);
3436 tok_str_add2(str
, tok
, &tokc
);
3437 tok_str_add(str
, 0);
3438 begin_macro(str
, 1);
3442 ST_FUNC
void preprocess_init(TCCState
*s1
)
3444 s1
->include_stack_ptr
= s1
->include_stack
;
3445 /* XXX: move that before to avoid having to initialize
3446 file->ifdef_stack_ptr ? */
3447 s1
->ifdef_stack_ptr
= s1
->ifdef_stack
;
3448 file
->ifdef_stack_ptr
= s1
->ifdef_stack_ptr
;
3450 pvtop
= vtop
= vstack
- 1;
3451 s1
->pack_stack
[0] = 0;
3452 s1
->pack_stack_ptr
= s1
->pack_stack
;
3454 isidnum_table
['$' - CH_EOF
] =
3455 s1
->dollars_in_identifiers
? IS_ID
: 0;
3456 isidnum_table
['.' - CH_EOF
] =
3457 (parse_flags
& PARSE_FLAG_ASM_FILE
) ? IS_ID
: 0;
3460 ST_FUNC
void preprocess_new(void)
3465 /* init isid table */
3466 for(i
= CH_EOF
; i
<128; i
++)
3467 isidnum_table
[i
- CH_EOF
]
3468 = is_space(i
) ? IS_SPC
3473 for(i
= 128; i
<256; i
++)
3474 isidnum_table
[i
- CH_EOF
] = IS_ID
;
3476 /* init allocators */
3477 tal_new(&toksym_alloc
, TOKSYM_TAL_LIMIT
, TOKSYM_TAL_SIZE
);
3478 tal_new(&tokstr_alloc
, TOKSTR_TAL_LIMIT
, TOKSTR_TAL_SIZE
);
3479 tal_new(&cstr_alloc
, CSTR_TAL_LIMIT
, CSTR_TAL_SIZE
);
3481 memset(hash_ident
, 0, TOK_HASH_SIZE
* sizeof(TokenSym
*));
3482 cstr_new(&cstr_buf
);
3483 cstr_realloc(&cstr_buf
, STRING_MAX_SIZE
);
3484 tok_str_new(&tokstr_buf
);
3485 tok_str_realloc(&tokstr_buf
, TOKSTR_MAX_SIZE
);
3487 tok_ident
= TOK_IDENT
;
3496 tok_alloc(p
, r
- p
- 1);
3501 ST_FUNC
void preprocess_delete(void)
3505 /* free -D and compiler defines */
3508 /* cleanup from error/setjmp */
3514 n
= tok_ident
- TOK_IDENT
;
3515 for(i
= 0; i
< n
; i
++)
3516 tal_free(toksym_alloc
, table_ident
[i
]);
3517 tcc_free(table_ident
);
3520 /* free static buffers */
3521 cstr_free(&tokcstr
);
3522 cstr_free(&cstr_buf
);
3523 tok_str_free(tokstr_buf
.str
);
3525 /* free allocators */
3526 tal_delete(toksym_alloc
);
3527 toksym_alloc
= NULL
;
3528 tal_delete(tokstr_alloc
);
3529 tokstr_alloc
= NULL
;
3530 tal_delete(cstr_alloc
);
3534 /* ------------------------------------------------------------------------- */
3535 /* tcc -E [-P[1]] [-dD} support */
3537 static void tok_print(const char *msg
, const int *str
)
3543 fp
= tcc_state
->ppfp
;
3544 if (!fp
|| !tcc_state
->dflag
)
3547 fprintf(fp
, "%s ", msg
);
3549 TOK_GET(&t
, &str
, &cval
);
3552 fprintf(fp
,"%s", get_tok_str(t
, &cval
));
3557 static void pp_line(TCCState
*s1
, BufferedFile
*f
, int level
)
3559 int d
= f
->line_num
- f
->line_ref
;
3564 if (s1
->Pflag
== LINE_MACRO_OUTPUT_FORMAT_NONE
) {
3565 if (level
== 0 && f
->line_ref
&& d
) {
3569 } else if (level
== 0 && f
->line_ref
&& d
< 8) {
3572 fputs("\n", s1
->ppfp
), --d
;
3573 } else if (s1
->Pflag
== LINE_MACRO_OUTPUT_FORMAT_STD
) {
3574 fprintf(s1
->ppfp
, "#line %d \"%s\"\n", f
->line_num
, f
->filename
);
3576 fprintf(s1
->ppfp
, "# %d \"%s\"%s\n", f
->line_num
, f
->filename
,
3577 level
> 0 ? " 1" : level
< 0 ? " 2" : "");
3579 f
->line_ref
= f
->line_num
;
3582 static void define_print(TCCState
*s1
, int v
)
3588 if (NULL
== s
|| NULL
== s
->d
)
3592 fprintf(fp
, "#define %s", get_tok_str(v
, NULL
));
3593 if (s
->type
.t
== MACRO_FUNC
) {
3598 fprintf(fp
,"%s", get_tok_str(a
->v
& ~SYM_FIELD
, NULL
));
3605 tok_print("", s
->d
);
3608 static void pp_debug_defines(TCCState
*s1
)
3619 pp_line(s1
, file
, 0);
3620 file
->line_ref
= ++file
->line_num
;
3624 vs
= get_tok_str(v
, NULL
);
3625 if (t
== TOK_DEFINE
) {
3626 define_print(s1
, v
);
3627 } else if (t
== TOK_UNDEF
) {
3628 fprintf(fp
, "#undef %s\n", vs
);
3629 } else if (t
== TOK_push_macro
) {
3630 fprintf(fp
, "#pragma push_macro(\"%s\")\n", vs
);
3631 } else if (t
== TOK_pop_macro
) {
3632 fprintf(fp
, "#pragma pop_macro(\"%s\")\n", vs
);
3637 static void pp_debug_builtins(TCCState
*s1
)
3640 for (v
= TOK_IDENT
; v
< tok_ident
; ++v
)
3641 define_print(s1
, v
);
3644 static int need_space(int prev_tok
, int tok
, const char *tokstr
)
3646 const char *sp_chars
= "";
3647 if ((prev_tok
>= TOK_IDENT
|| prev_tok
== TOK_PPNUM
) &&
3648 (tok
>= TOK_IDENT
|| tok
== TOK_PPNUM
))
3689 return !!strchr(sp_chars
, tokstr
[0]);
3692 /* Preprocess the current file */
3693 ST_FUNC
int tcc_preprocess(TCCState
*s1
)
3695 BufferedFile
**iptr
;
3696 int token_seen
, spcs
, level
;
3700 preprocess_init(s1
);
3701 ch
= file
->buf_ptr
[0];
3702 tok_flags
= TOK_FLAG_BOL
| TOK_FLAG_BOF
;
3703 parse_flags
= PARSE_FLAG_PREPROCESS
3704 | (parse_flags
& PARSE_FLAG_ASM_FILE
)
3705 | PARSE_FLAG_LINEFEED
3707 | PARSE_FLAG_ACCEPT_STRAYS
3709 define_start
= define_stack
;
3711 /* Credits to Fabrice Bellard's initial revision to demonstrate its
3712 capability to compile and run itself, provided all numbers are
3713 given as decimals. tcc -E -P10 will do. */
3714 if (s1
->Pflag
== 1 + 10)
3715 parse_flags
|= PARSE_FLAG_TOK_NUM
, s1
->Pflag
= 1;
3718 /* for PP benchmarks */
3719 do next(); while (tok
!= TOK_EOF
); return 0;
3722 if (s1
->dflag
& 1) {
3723 pp_debug_builtins(s1
);
3727 token_seen
= TOK_LINEFEED
, spcs
= 0;
3728 pp_line(s1
, file
, 0);
3731 iptr
= s1
->include_stack_ptr
;
3735 level
= s1
->include_stack_ptr
- iptr
;
3738 pp_line(s1
, *iptr
, 0);
3739 pp_line(s1
, file
, level
);
3743 pp_debug_defines(s1
);
3748 if (token_seen
== TOK_LINEFEED
) {
3753 if (tok
== TOK_LINEFEED
) {
3757 pp_line(s1
, file
, 0);
3758 } else if (tok
== TOK_LINEFEED
) {
3762 tokstr
= get_tok_str(tok
, &tokc
);
3763 if (!spcs
&& need_space(token_seen
, tok
, tokstr
))
3766 fputs(" ", s1
->ppfp
), --spcs
;
3767 fputs(tokstr
, s1
->ppfp
);
3771 /* reset define stack, but keep -D and built-ins */
3772 free_defines(define_start
);
3776 /* ------------------------------------------------------------------------- */