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 /* ------------------------------------------------------------------------- */
43 static TokenSym
*hash_ident
[TOK_HASH_SIZE
];
44 static char token_buf
[STRING_MAX_SIZE
+ 1];
45 static CString cstr_buf
;
46 static TokenString tokstr_buf
;
47 static unsigned char isidnum_table
[256 - CH_EOF
];
48 static int pp_debug_tok
, pp_debug_symv
;
50 static void tok_print(const char *msg
, const int *str
);
52 static struct TinyAlloc
*toksym_alloc
;
53 static struct TinyAlloc
*tokstr_alloc
;
54 static struct TinyAlloc
*cstr_alloc
;
56 /* isidnum_table flags: */
61 static TokenString
*macro_stack
;
63 static const char tcc_keywords
[] =
64 #define DEF(id, str) str "\0"
69 /* WARNING: the content of this string encodes token numbers */
70 static const unsigned char tok_two_chars
[] =
72 "<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253"
73 "-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\313..\250##\266";
94 '.','.', 0xa8, // C++ token ?
95 '#','#', TOK_TWOSHARPS
,
99 static void next_nomacro_spc(void);
101 ST_FUNC
void skip(int c
)
104 tcc_error("'%c' expected (got \"%s\")", c
, get_tok_str(tok
, &tokc
));
108 ST_FUNC
void expect(const char *msg
)
110 tcc_error("%s expected", msg
);
113 /* ------------------------------------------------------------------------- */
114 /* Custom allocator for tiny objects */
119 #define tal_free(al, p) tcc_free(p)
120 #define tal_realloc(al, p, size) tcc_realloc(p, size)
121 #define tal_new(a,b,c)
122 #define tal_delete(a)
124 #if !defined(MEM_DEBUG)
125 #define tal_free(al, p) tal_free_impl(al, p)
126 #define tal_realloc(al, p, size) tal_realloc_impl(&al, p, size)
127 #define TAL_DEBUG_PARAMS
130 #define tal_free(al, p) tal_free_impl(al, p, __FILE__, __LINE__)
131 #define tal_realloc(al, p, size) tal_realloc_impl(&al, p, size, __FILE__, __LINE__)
132 #define TAL_DEBUG_PARAMS , const char *file, int line
133 #define TAL_DEBUG_FILE_LEN 15
135 //#define TAL_INFO 1 /* collect and dump allocators stats */
137 typedef struct TinyAlloc
{
143 struct TinyAlloc
*next
, *top
;
152 typedef struct tal_header_t
{
155 int line_num
; /* negative line_num used for double free check */
156 char file_name
[TAL_DEBUG_FILE_LEN
+ 1];
160 /* ------------------------------------------------------------------------- */
162 ST_FUNC TinyAlloc
*tal_new(TinyAlloc
**pal
, size_t limit
, size_t size
)
164 TinyAlloc
*al
= tcc_mallocz(sizeof(TinyAlloc
));
165 al
->p
= al
->buffer
= tcc_malloc(size
);
172 ST_FUNC
void tal_delete(TinyAlloc
*al
)
180 fprintf(stderr
, "limit=%5d, size=%5g MB, nb_peak=%6d, nb_total=%8d, nb_missed=%6d, usage=%5.1f%%\n",
181 al
->limit
, al
->size
/ 1024.0 / 1024.0, al
->nb_peak
, al
->nb_total
, al
->nb_missed
,
182 (al
->peak_p
- al
->buffer
) * 100.0 / al
->size
);
185 if (al
->nb_allocs
> 0) {
187 fprintf(stderr
, "TAL_DEBUG: mem leak %d chunks (limit= %d)\n",
188 al
->nb_allocs
, al
->limit
);
191 tal_header_t
*header
= (tal_header_t
*)p
;
192 if (header
->line_num
> 0) {
193 fprintf(stderr
, " file %s, line %u: %u bytes\n",
194 header
->file_name
, header
->line_num
, header
->size
);
196 p
+= header
->size
+ sizeof(tal_header_t
);
201 tcc_free(al
->buffer
);
207 ST_FUNC
void tal_free_impl(TinyAlloc
*al
, void *p TAL_DEBUG_PARAMS
)
212 if (al
->buffer
<= (uint8_t *)p
&& (uint8_t *)p
< al
->buffer
+ al
->size
) {
214 tal_header_t
*header
= (((tal_header_t
*)p
) - 1);
215 if (header
->line_num
< 0) {
216 fprintf(stderr
, "TAL_DEBUG: file %s, line %u double frees chunk from\n",
218 fprintf(stderr
, " file %s, line %u: %u bytes\n",
219 header
->file_name
, -header
->line_num
, header
->size
);
221 header
->line_num
= -header
->line_num
;
226 } else if (al
->next
) {
234 ST_FUNC
void *tal_realloc_impl(TinyAlloc
**pal
, void *p
, size_t size TAL_DEBUG_PARAMS
)
236 tal_header_t
*header
;
239 size_t adj_size
= (size
+ 3) & -4;
240 TinyAlloc
*al
= *pal
;
243 is_own
= (al
->buffer
<= (uint8_t *)p
&& (uint8_t *)p
< al
->buffer
+ al
->size
);
244 if ((!p
|| is_own
) && size
<= al
->limit
) {
245 if (al
->p
+ adj_size
+ sizeof(tal_header_t
) < al
->buffer
+ al
->size
) {
246 header
= (tal_header_t
*)al
->p
;
247 header
->size
= adj_size
;
249 { int ofs
= strlen(file
) - TAL_DEBUG_FILE_LEN
;
250 strncpy(header
->file_name
, file
+ (ofs
> 0 ? ofs
: 0), TAL_DEBUG_FILE_LEN
);
251 header
->file_name
[TAL_DEBUG_FILE_LEN
] = 0;
252 header
->line_num
= line
; }
254 ret
= al
->p
+ sizeof(tal_header_t
);
255 al
->p
+= adj_size
+ sizeof(tal_header_t
);
257 header
= (((tal_header_t
*)p
) - 1);
258 memcpy(ret
, p
, header
->size
);
260 header
->line_num
= -header
->line_num
;
266 if (al
->nb_peak
< al
->nb_allocs
)
267 al
->nb_peak
= al
->nb_allocs
;
268 if (al
->peak_p
< al
->p
)
275 ret
= tal_realloc(*pal
, 0, size
);
276 header
= (((tal_header_t
*)p
) - 1);
277 memcpy(ret
, p
, header
->size
);
279 header
->line_num
= -header
->line_num
;
286 TinyAlloc
*bottom
= al
, *next
= al
->top
? al
->top
: al
;
288 al
= tal_new(pal
, next
->limit
, next
->size
* 2);
296 ret
= tcc_malloc(size
);
297 header
= (((tal_header_t
*)p
) - 1);
298 memcpy(ret
, p
, header
->size
);
300 header
->line_num
= -header
->line_num
;
302 } else if (al
->next
) {
306 ret
= tcc_realloc(p
, size
);
315 /* ------------------------------------------------------------------------- */
316 /* CString handling */
317 static void cstr_realloc(CString
*cstr
, int new_size
)
321 size
= cstr
->size_allocated
;
323 size
= 8; /* no need to allocate a too small first string */
324 while (size
< new_size
)
326 cstr
->data
= tal_realloc(cstr_alloc
, cstr
->data
, size
);
327 cstr
->size_allocated
= size
;
331 ST_INLN
void cstr_ccat(CString
*cstr
, int ch
)
334 size
= cstr
->size
+ 1;
335 if (size
> cstr
->size_allocated
)
336 cstr_realloc(cstr
, size
);
337 ((unsigned char *)cstr
->data
)[size
- 1] = ch
;
341 ST_FUNC
void cstr_cat(CString
*cstr
, const char *str
, int len
)
345 len
= strlen(str
) + 1 + len
;
346 size
= cstr
->size
+ len
;
347 if (size
> cstr
->size_allocated
)
348 cstr_realloc(cstr
, size
);
349 memmove(((unsigned char *)cstr
->data
) + cstr
->size
, str
, len
);
353 /* add a wide char */
354 ST_FUNC
void cstr_wccat(CString
*cstr
, int ch
)
357 size
= cstr
->size
+ sizeof(nwchar_t
);
358 if (size
> cstr
->size_allocated
)
359 cstr_realloc(cstr
, size
);
360 *(nwchar_t
*)(((unsigned char *)cstr
->data
) + size
- sizeof(nwchar_t
)) = ch
;
364 ST_FUNC
void cstr_new(CString
*cstr
)
366 memset(cstr
, 0, sizeof(CString
));
369 /* free string and reset it to NULL */
370 ST_FUNC
void cstr_free(CString
*cstr
)
372 tal_free(cstr_alloc
, cstr
->data
);
376 /* reset string to empty */
377 ST_FUNC
void cstr_reset(CString
*cstr
)
383 static void add_char(CString
*cstr
, int c
)
385 if (c
== '\'' || c
== '\"' || c
== '\\') {
386 /* XXX: could be more precise if char or string */
387 cstr_ccat(cstr
, '\\');
389 if (c
>= 32 && c
<= 126) {
392 cstr_ccat(cstr
, '\\');
394 cstr_ccat(cstr
, 'n');
396 cstr_ccat(cstr
, '0' + ((c
>> 6) & 7));
397 cstr_ccat(cstr
, '0' + ((c
>> 3) & 7));
398 cstr_ccat(cstr
, '0' + (c
& 7));
403 /* ------------------------------------------------------------------------- */
404 /* allocate a new token */
405 static TokenSym
*tok_alloc_new(TokenSym
**pts
, const char *str
, int len
)
407 TokenSym
*ts
, **ptable
;
410 if (tok_ident
>= SYM_FIRST_ANOM
)
411 tcc_error("memory full (symbols)");
413 /* expand token table if needed */
414 i
= tok_ident
- TOK_IDENT
;
415 if ((i
% TOK_ALLOC_INCR
) == 0) {
416 ptable
= tcc_realloc(table_ident
, (i
+ TOK_ALLOC_INCR
) * sizeof(TokenSym
*));
417 table_ident
= ptable
;
420 ts
= tal_realloc(toksym_alloc
, 0, sizeof(TokenSym
) + len
);
422 ts
->tok
= tok_ident
++;
423 ts
->sym_define
= NULL
;
424 ts
->sym_label
= NULL
;
425 ts
->sym_struct
= NULL
;
426 ts
->sym_identifier
= NULL
;
428 ts
->hash_next
= NULL
;
429 memcpy(ts
->str
, str
, len
);
435 #define TOK_HASH_INIT 1
436 #define TOK_HASH_FUNC(h, c) ((h) + ((h) << 5) + ((h) >> 27) + (c))
439 /* find a token and add it if not found */
440 ST_FUNC TokenSym
*tok_alloc(const char *str
, int len
)
448 h
= TOK_HASH_FUNC(h
, ((unsigned char *)str
)[i
]);
449 h
&= (TOK_HASH_SIZE
- 1);
451 pts
= &hash_ident
[h
];
456 if (ts
->len
== len
&& !memcmp(ts
->str
, str
, len
))
458 pts
= &(ts
->hash_next
);
460 return tok_alloc_new(pts
, str
, len
);
463 /* XXX: buffer overflow */
464 /* XXX: float tokens */
465 ST_FUNC
const char *get_tok_str(int v
, CValue
*cv
)
470 cstr_reset(&cstr_buf
);
478 /* XXX: not quite exact, but only useful for testing */
480 sprintf(p
, "%u", (unsigned)cv
->i
);
482 sprintf(p
, "%llu", (unsigned long long)cv
->i
);
486 cstr_ccat(&cstr_buf
, 'L');
488 cstr_ccat(&cstr_buf
, '\'');
489 add_char(&cstr_buf
, cv
->i
);
490 cstr_ccat(&cstr_buf
, '\'');
491 cstr_ccat(&cstr_buf
, '\0');
495 return (char*)cv
->str
.data
;
497 cstr_ccat(&cstr_buf
, 'L');
499 cstr_ccat(&cstr_buf
, '\"');
501 len
= cv
->str
.size
- 1;
503 add_char(&cstr_buf
, ((unsigned char *)cv
->str
.data
)[i
]);
505 len
= (cv
->str
.size
/ sizeof(nwchar_t
)) - 1;
507 add_char(&cstr_buf
, ((nwchar_t
*)cv
->str
.data
)[i
]);
509 cstr_ccat(&cstr_buf
, '\"');
510 cstr_ccat(&cstr_buf
, '\0');
514 cstr_cat(&cstr_buf
, "<float>", 0);
517 cstr_cat(&cstr_buf
, "<double>", 0);
520 cstr_cat(&cstr_buf
, "<long double>", 0);
523 cstr_cat(&cstr_buf
, "<linenumber>", 0);
526 /* above tokens have value, the ones below don't */
535 return strcpy(p
, "...");
537 return strcpy(p
, "<<=");
539 return strcpy(p
, ">>=");
542 /* search in two bytes table */
543 const unsigned char *q
= tok_two_chars
;
549 return cstr_buf
.data
;
554 sprintf(cstr_buf
.data
, "<%02x>", v
);
555 return cstr_buf
.data
;
560 } else if (v
< tok_ident
) {
561 return table_ident
[v
- TOK_IDENT
]->str
;
562 } else if (v
>= SYM_FIRST_ANOM
) {
563 /* special name for anonymous symbol */
564 sprintf(p
, "L.%u", v
- SYM_FIRST_ANOM
);
566 /* should never happen */
571 return cstr_buf
.data
;
574 /* return the current character, handling end of block if necessary
576 ST_FUNC
int handle_eob(void)
578 BufferedFile
*bf
= file
;
581 /* only tries to read if really end of buffer */
582 if (bf
->buf_ptr
>= bf
->buf_end
) {
584 #if defined(PARSE_DEBUG)
589 len
= read(bf
->fd
, bf
->buffer
, len
);
596 bf
->buf_ptr
= bf
->buffer
;
597 bf
->buf_end
= bf
->buffer
+ len
;
598 *bf
->buf_end
= CH_EOB
;
600 if (bf
->buf_ptr
< bf
->buf_end
) {
601 return bf
->buf_ptr
[0];
603 bf
->buf_ptr
= bf
->buf_end
;
608 /* read next char from current input file and handle end of input buffer */
609 ST_INLN
void inp(void)
611 ch
= *(++(file
->buf_ptr
));
612 /* end of buffer/file handling */
617 /* handle '\[\r]\n' */
618 static int handle_stray_noerror(void)
625 } else if (ch
== '\r') {
639 static void handle_stray(void)
641 if (handle_stray_noerror())
642 tcc_error("stray '\\' in program");
645 /* skip the stray and handle the \\n case. Output an error if
646 incorrect char after the stray */
647 static int handle_stray1(uint8_t *p
)
652 if (p
>= file
->buf_end
) {
659 if (handle_stray_noerror()) {
660 if (!(parse_flags
& PARSE_FLAG_ACCEPT_STRAYS
))
661 tcc_error("stray '\\' in program");
662 *--file
->buf_ptr
= '\\';
669 /* handle just the EOB case, but not stray */
670 #define PEEKC_EOB(c, p)\
681 /* handle the complicated stray case */
687 c = handle_stray1(p);\
692 /* input with '\[\r]\n' handling. Note that this function cannot
693 handle other characters after '\', so you cannot call it inside
694 strings or comments */
695 ST_FUNC
void minp(void)
702 /* single line C++ comments */
703 static uint8_t *parse_line_comment(uint8_t *p
)
711 if (c
== '\n' || c
== CH_EOF
) {
713 } else if (c
== '\\') {
722 } else if (c
== '\r') {
740 ST_FUNC
uint8_t *parse_comment(uint8_t *p
)
749 if (c
== '\n' || c
== '*' || c
== '\\')
753 if (c
== '\n' || c
== '*' || c
== '\\')
757 /* now we can handle all the cases */
761 } else if (c
== '*') {
767 } else if (c
== '/') {
769 } else if (c
== '\\') {
774 tcc_error("unexpected end of file in comment");
776 /* skip '\[\r]\n', otherwise just skip the stray */
782 } else if (c
== '\r') {
799 /* stray, eob or eof */
804 tcc_error("unexpected end of file in comment");
805 } else if (c
== '\\') {
817 static inline void skip_spaces(void)
819 while (isidnum_table
[ch
- CH_EOF
] & IS_SPC
)
823 static inline int check_space(int t
, int *spc
)
825 if (t
< 256 && (isidnum_table
[t
- CH_EOF
] & IS_SPC
)) {
834 /* parse a string without interpreting escapes */
835 static uint8_t *parse_pp_string(uint8_t *p
,
836 int sep
, CString
*str
)
844 } else if (c
== '\\') {
850 /* XXX: indicate line number of start of string */
851 tcc_error("missing terminating %c character", sep
);
852 } else if (c
== '\\') {
853 /* escape : just skip \[\r]\n */
858 } else if (c
== '\r') {
861 expect("'\n' after '\r'");
864 } else if (c
== CH_EOF
) {
865 goto unterminated_string
;
868 cstr_ccat(str
, '\\');
874 } else if (c
== '\n') {
877 } else if (c
== '\r') {
881 cstr_ccat(str
, '\r');
897 /* skip block of text until #else, #elif or #endif. skip also pairs of
899 static void preprocess_skip(void)
901 int a
, start_of_line
, c
, in_warn_or_error
;
908 in_warn_or_error
= 0;
929 } else if (c
== '\\') {
930 ch
= file
->buf_ptr
[0];
931 handle_stray_noerror();
938 if (in_warn_or_error
)
940 p
= parse_pp_string(p
, c
, NULL
);
944 if (in_warn_or_error
)
951 p
= parse_comment(p
);
952 } else if (ch
== '/') {
953 p
= parse_line_comment(p
);
963 (tok
== TOK_ELSE
|| tok
== TOK_ELIF
|| tok
== TOK_ENDIF
))
965 if (tok
== TOK_IF
|| tok
== TOK_IFDEF
|| tok
== TOK_IFNDEF
)
967 else if (tok
== TOK_ENDIF
)
969 else if( tok
== TOK_ERROR
|| tok
== TOK_WARNING
)
970 in_warn_or_error
= 1;
971 else if (tok
== TOK_LINEFEED
)
973 else if (parse_flags
& PARSE_FLAG_ASM_FILE
)
974 p
= parse_line_comment(p
- 1);
975 } else if (parse_flags
& PARSE_FLAG_ASM_FILE
)
976 p
= parse_line_comment(p
- 1);
989 /* ParseState handling */
991 /* XXX: currently, no include file info is stored. Thus, we cannot display
992 accurate messages if the function or data definition spans multiple
995 /* save current parse state in 's' */
996 ST_FUNC
void save_parse_state(ParseState
*s
)
998 s
->line_num
= file
->line_num
;
999 s
->macro_ptr
= macro_ptr
;
1004 /* restore parse state from 's' */
1005 ST_FUNC
void restore_parse_state(ParseState
*s
)
1007 file
->line_num
= s
->line_num
;
1008 macro_ptr
= s
->macro_ptr
;
1013 /* return the number of additional 'ints' necessary to store the
1015 static inline int tok_size(const int *p
)
1030 return 1 + ((sizeof(CString
) + ((CString
*)(p
+1))->size
+ 3) >> 2);
1036 return 1 + LDOUBLE_SIZE
/ 4;
1042 /* token string handling */
1044 ST_INLN
void tok_str_new(TokenString
*s
)
1048 s
->allocated_len
= 0;
1049 s
->last_line_num
= -1;
1052 ST_FUNC TokenString
*tok_str_alloc(void)
1054 TokenString
*str
= tal_realloc(tokstr_alloc
, 0, sizeof *str
);
1059 ST_FUNC
int *tok_str_dup(TokenString
*s
)
1063 str
= tal_realloc(tokstr_alloc
, 0, s
->len
* sizeof(int));
1064 memcpy(str
, s
->str
, s
->len
* sizeof(int));
1068 ST_FUNC
void tok_str_free(int *str
)
1070 tal_free(tokstr_alloc
, str
);
1073 ST_FUNC
int *tok_str_realloc(TokenString
*s
, int new_size
)
1077 size
= s
->allocated_len
;
1080 while (size
< new_size
)
1082 if (size
> s
->allocated_len
) {
1083 str
= tal_realloc(tokstr_alloc
, s
->str
, size
* sizeof(int));
1084 s
->allocated_len
= size
;
1090 ST_FUNC
void tok_str_add(TokenString
*s
, int t
)
1096 if (len
>= s
->allocated_len
)
1097 str
= tok_str_realloc(s
, len
+ 1);
1102 ST_FUNC
void begin_macro(TokenString
*str
, int alloc
)
1105 str
->prev
= macro_stack
;
1106 str
->prev_ptr
= macro_ptr
;
1107 macro_ptr
= str
->str
;
1111 ST_FUNC
void end_macro(void)
1113 TokenString
*str
= macro_stack
;
1114 macro_stack
= str
->prev
;
1115 macro_ptr
= str
->prev_ptr
;
1116 if (str
->alloc
== 2) {
1117 str
->alloc
= 3; /* just mark as finished */
1119 tok_str_free(str
->str
);
1120 if (str
->alloc
== 1)
1121 tal_free(tokstr_alloc
, str
);
1125 static void tok_str_add2(TokenString
*s
, int t
, CValue
*cv
)
1132 /* allocate space for worst case */
1133 if (len
+ TOK_MAX_SIZE
>= s
->allocated_len
)
1134 str
= tok_str_realloc(s
, len
+ TOK_MAX_SIZE
+ 1);
1143 str
[len
++] = cv
->tab
[0];
1150 /* Insert the string into the int array. */
1152 1 + (cv
->str
.size
+ sizeof(int) - 1) / sizeof(int);
1153 if (len
+ nb_words
>= s
->allocated_len
)
1154 str
= tok_str_realloc(s
, len
+ nb_words
+ 1);
1155 str
[len
] = cv
->str
.size
;
1156 memcpy(&str
[len
+ 1], cv
->str
.data
, cv
->str
.size
);
1163 #if LDOUBLE_SIZE == 8
1166 str
[len
++] = cv
->tab
[0];
1167 str
[len
++] = cv
->tab
[1];
1169 #if LDOUBLE_SIZE == 12
1171 str
[len
++] = cv
->tab
[0];
1172 str
[len
++] = cv
->tab
[1];
1173 str
[len
++] = cv
->tab
[2];
1174 #elif LDOUBLE_SIZE == 16
1176 str
[len
++] = cv
->tab
[0];
1177 str
[len
++] = cv
->tab
[1];
1178 str
[len
++] = cv
->tab
[2];
1179 str
[len
++] = cv
->tab
[3];
1180 #elif LDOUBLE_SIZE != 8
1181 #error add long double size support
1190 /* add the current parse token in token string 's' */
1191 ST_FUNC
void tok_str_add_tok(TokenString
*s
)
1195 /* save line number info */
1196 if (file
->line_num
!= s
->last_line_num
) {
1197 s
->last_line_num
= file
->line_num
;
1198 cval
.i
= s
->last_line_num
;
1199 tok_str_add2(s
, TOK_LINENUM
, &cval
);
1201 tok_str_add2(s
, tok
, &tokc
);
1204 /* get a token from an integer array and increment pointer
1205 accordingly. we code it as a macro to avoid pointer aliasing. */
1206 static inline void TOK_GET(int *t
, const int **pp
, CValue
*cv
)
1225 cv
->str
.size
= *p
++;
1227 p
+= (cv
->str
.size
+ sizeof(int) - 1) / sizeof(int);
1235 #if LDOUBLE_SIZE == 16
1237 #elif LDOUBLE_SIZE == 12
1239 #elif LDOUBLE_SIZE == 8
1242 # error add long double size support
1255 /* Calling this function is expensive, but it is not possible
1256 to read a token string backwards. */
1257 static int tok_last(const int *str0
, const int *str1
)
1259 const int *str
= str0
;
1264 TOK_GET(&tok
, &str
, &cval
);
1268 static int macro_is_equal(const int *a
, const int *b
)
1277 /* first time preallocate static cstr_buf, next time only reset position to start */
1278 cstr_reset(&cstr_buf
);
1279 TOK_GET(&t
, &a
, &cv
);
1280 cstr_cat(&cstr_buf
, get_tok_str(t
, &cv
), 0);
1281 TOK_GET(&t
, &b
, &cv
);
1282 if (strcmp(cstr_buf
.data
, get_tok_str(t
, &cv
)))
1288 /* defines handling */
1289 ST_INLN
void define_push(int v
, int macro_type
, int *str
, Sym
*first_arg
)
1294 s
= sym_push2(&define_stack
, v
, macro_type
, 0);
1296 s
->next
= first_arg
;
1297 table_ident
[v
- TOK_IDENT
]->sym_define
= s
;
1299 if (o
&& !macro_is_equal(o
->d
, s
->d
))
1300 tcc_warning("%s redefined", get_tok_str(v
, NULL
));
1303 /* undefined a define symbol. Its name is just set to zero */
1304 ST_FUNC
void define_undef(Sym
*s
)
1307 if (v
>= TOK_IDENT
&& v
< tok_ident
)
1308 table_ident
[v
- TOK_IDENT
]->sym_define
= NULL
;
1311 ST_INLN Sym
*define_find(int v
)
1314 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
1316 return table_ident
[v
]->sym_define
;
1319 /* free define stack until top reaches 'b' */
1320 ST_FUNC
void free_defines(Sym
*b
)
1322 while (define_stack
!= b
) {
1323 Sym
*top
= define_stack
;
1324 define_stack
= top
->prev
;
1325 tok_str_free(top
->d
);
1330 /* restore remaining (-D or predefined) symbols */
1333 if (v
>= TOK_IDENT
&& v
< tok_ident
) {
1334 Sym
**d
= &table_ident
[v
- TOK_IDENT
]->sym_define
;
1343 ST_FUNC Sym
*label_find(int v
)
1346 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
1348 return table_ident
[v
]->sym_label
;
1351 ST_FUNC Sym
*label_push(Sym
**ptop
, int v
, int flags
)
1354 s
= sym_push2(ptop
, v
, 0, 0);
1356 ps
= &table_ident
[v
- TOK_IDENT
]->sym_label
;
1357 if (ptop
== &global_label_stack
) {
1358 /* modify the top most local identifier, so that
1359 sym_identifier will point to 's' when popped */
1361 ps
= &(*ps
)->prev_tok
;
1368 /* pop labels until element last is reached. Look if any labels are
1369 undefined. Define symbols if '&&label' was used. */
1370 ST_FUNC
void label_pop(Sym
**ptop
, Sym
*slast
)
1373 for(s
= *ptop
; s
!= slast
; s
= s1
) {
1375 if (s
->r
== LABEL_DECLARED
) {
1376 tcc_warning("label '%s' declared but not used", get_tok_str(s
->v
, NULL
));
1377 } else if (s
->r
== LABEL_FORWARD
) {
1378 tcc_error("label '%s' used but not defined",
1379 get_tok_str(s
->v
, NULL
));
1382 /* define corresponding symbol. A size of
1384 put_extern_sym(s
, cur_text_section
, s
->jnext
, 1);
1388 table_ident
[s
->v
- TOK_IDENT
]->sym_label
= s
->prev_tok
;
1394 /* eval an expression for #if/#elif */
1395 static int expr_preprocess(void)
1400 str
= tok_str_alloc();
1401 while (tok
!= TOK_LINEFEED
&& tok
!= TOK_EOF
) {
1402 next(); /* do macro subst */
1403 if (tok
== TOK_DEFINED
) {
1408 c
= define_find(tok
) != 0;
1413 } else if (tok
>= TOK_IDENT
) {
1414 /* if undefined macro */
1418 tok_str_add_tok(str
);
1420 tok_str_add(str
, -1); /* simulate end of file */
1421 tok_str_add(str
, 0);
1422 /* now evaluate C constant expression */
1423 begin_macro(str
, 1);
1431 /* parse after #define */
1432 ST_FUNC
void parse_define(void)
1434 Sym
*s
, *first
, **ps
;
1435 int v
, t
, varg
, is_vaargs
, spc
;
1436 int saved_parse_flags
= parse_flags
;
1440 tcc_error("invalid macro name '%s'", get_tok_str(tok
, &tokc
));
1441 /* XXX: should check if same macro (ANSI) */
1444 /* '(' must be just after macro definition for MACRO_FUNC */
1445 parse_flags
|= PARSE_FLAG_SPACES
;
1448 /* must be able to parse TOK_DOTS (in asm mode '.' can be part of identifier) */
1449 parse_flags
&= ~PARSE_FLAG_ASM_FILE
;
1450 isidnum_table
['.' - CH_EOF
] = 0;
1453 if (tok
!= ')') for (;;) {
1457 if (varg
== TOK_DOTS
) {
1458 varg
= TOK___VA_ARGS__
;
1460 } else if (tok
== TOK_DOTS
&& gnu_ext
) {
1464 if (varg
< TOK_IDENT
)
1466 tcc_error("bad macro parameter list");
1467 s
= sym_push2(&define_stack
, varg
| SYM_FIELD
, is_vaargs
, 0);
1472 if (tok
!= ',' || is_vaargs
)
1478 parse_flags
|= (saved_parse_flags
& PARSE_FLAG_ASM_FILE
);
1479 isidnum_table
['.' - CH_EOF
] =
1480 (parse_flags
& PARSE_FLAG_ASM_FILE
) ? IS_ID
: 0;
1485 parse_flags
|= PARSE_FLAG_ACCEPT_STRAYS
| PARSE_FLAG_SPACES
| PARSE_FLAG_LINEFEED
;
1486 while (tok
!= TOK_LINEFEED
&& tok
!= TOK_EOF
) {
1487 /* remove spaces around ## and after '#' */
1488 if (TOK_TWOSHARPS
== tok
) {
1494 } else if ('#' == tok
) {
1496 } else if (check_space(tok
, &spc
)) {
1499 tok_str_add2(&tokstr_buf
, tok
, &tokc
);
1504 parse_flags
= saved_parse_flags
;
1506 --tokstr_buf
.len
; /* remove trailing space */
1507 tok_str_add(&tokstr_buf
, 0);
1510 tcc_error("'##' cannot appear at either end of macro");
1511 define_push(v
, t
, tok_str_dup(&tokstr_buf
), first
);
1514 static CachedInclude
*search_cached_include(TCCState
*s1
, const char *filename
, int add
)
1516 const unsigned char *s
;
1522 s
= (unsigned char *) filename
;
1525 h
= TOK_HASH_FUNC(h
, toup(*s
));
1527 h
= TOK_HASH_FUNC(h
, *s
);
1531 h
&= (CACHED_INCLUDES_HASH_SIZE
- 1);
1533 i
= s1
->cached_includes_hash
[h
];
1537 e
= s1
->cached_includes
[i
- 1];
1538 if (0 == PATHCMP(e
->filename
, filename
))
1545 e
= tcc_malloc(sizeof(CachedInclude
) + strlen(filename
));
1546 strcpy(e
->filename
, filename
);
1547 e
->ifndef_macro
= e
->once
= 0;
1548 dynarray_add((void ***)&s1
->cached_includes
, &s1
->nb_cached_includes
, e
);
1549 /* add in hash table */
1550 e
->hash_next
= s1
->cached_includes_hash
[h
];
1551 s1
->cached_includes_hash
[h
] = s1
->nb_cached_includes
;
1553 printf("adding cached '%s'\n", filename
);
1558 static void pragma_parse(TCCState
*s1
)
1561 if (tok
== TOK_push_macro
|| tok
== TOK_pop_macro
) {
1565 if (next(), tok
!= '(')
1567 if (next(), tok
!= TOK_STR
)
1569 v
= tok_alloc(tokc
.str
.data
, tokc
.str
.size
- 1)->tok
;
1570 if (next(), tok
!= ')')
1572 if (t
== TOK_push_macro
) {
1573 while (NULL
== (s
= define_find(v
)))
1574 define_push(v
, 0, NULL
, NULL
);
1575 s
->type
.ref
= s
; /* set push boundary */
1577 for (s
= define_stack
; s
; s
= s
->prev
)
1578 if (s
->v
== v
&& s
->type
.ref
== s
) {
1584 table_ident
[v
- TOK_IDENT
]->sym_define
= s
->d
? s
: NULL
;
1586 tcc_warning("unbalanced #pragma pop_macro");
1587 pp_debug_tok
= t
, pp_debug_symv
= v
;
1589 } else if (tok
== TOK_once
) {
1590 search_cached_include(s1
, file
->filename
, 1)->once
= pp_once
;
1592 } else if (s1
->ppfp
) {
1593 /* tcc -E: keep pragmas below unchanged */
1595 unget_tok(TOK_PRAGMA
);
1597 unget_tok(TOK_LINEFEED
);
1599 } else if (tok
== TOK_pack
) {
1601 #pragma pack(1) // set
1602 #pragma pack() // reset to default
1603 #pragma pack(push,1) // push & set
1604 #pragma pack(pop) // restore previous */
1607 if (tok
== TOK_ASM_pop
) {
1609 if (s1
->pack_stack_ptr
<= s1
->pack_stack
) {
1611 tcc_error("out of pack stack");
1613 s1
->pack_stack_ptr
--;
1617 if (tok
== TOK_ASM_push
) {
1619 if (s1
->pack_stack_ptr
>= s1
->pack_stack
+ PACK_STACK_SIZE
- 1)
1621 s1
->pack_stack_ptr
++;
1624 if (tok
!= TOK_CINT
)
1627 if (val
< 1 || val
> 16 || (val
& (val
- 1)) != 0)
1631 *s1
->pack_stack_ptr
= val
;
1636 } else if (tok
== TOK_comment
) {
1646 file
= tcc_strdup((char *)tokc
.str
.data
);
1647 dynarray_add((void ***)&s1
->pragma_libs
, &s1
->nb_pragma_libs
, file
);
1653 if (s1
->warn_unsupported
)
1654 tcc_warning("#pragma %s is ignored", get_tok_str(tok
, &tokc
));
1659 tcc_error("malformed #pragma directive");
1663 /* is_bof is true if first non space token at beginning of file */
1664 ST_FUNC
void preprocess(int is_bof
)
1666 TCCState
*s1
= tcc_state
;
1667 int i
, c
, n
, saved_parse_flags
;
1671 saved_parse_flags
= parse_flags
;
1672 parse_flags
= PARSE_FLAG_PREPROCESS
1673 | PARSE_FLAG_TOK_NUM
1674 | PARSE_FLAG_TOK_STR
1675 | PARSE_FLAG_LINEFEED
1676 | (parse_flags
& PARSE_FLAG_ASM_FILE
)
1685 pp_debug_symv
= tok
;
1691 pp_debug_symv
= tok
;
1692 s
= define_find(tok
);
1693 /* undefine symbol by putting an invalid name */
1698 case TOK_INCLUDE_NEXT
:
1699 ch
= file
->buf_ptr
[0];
1700 /* XXX: incorrect if comments : use next_nomacro with a special mode */
1705 } else if (ch
== '\"') {
1710 while (ch
!= c
&& ch
!= '\n' && ch
!= CH_EOF
) {
1711 if ((q
- buf
) < sizeof(buf
) - 1)
1714 if (handle_stray_noerror() == 0)
1722 /* eat all spaces and comments after include */
1723 /* XXX: slightly incorrect */
1724 while (ch1
!= '\n' && ch1
!= CH_EOF
)
1728 /* computed #include : either we have only strings or
1729 we have anything enclosed in '<>' */
1732 if (tok
== TOK_STR
) {
1733 while (tok
!= TOK_LINEFEED
) {
1734 if (tok
!= TOK_STR
) {
1736 tcc_error("'#include' expects \"FILENAME\" or <FILENAME>");
1738 pstrcat(buf
, sizeof(buf
), (char *)tokc
.str
.data
);
1744 while (tok
!= TOK_LINEFEED
) {
1745 pstrcat(buf
, sizeof(buf
), get_tok_str(tok
, &tokc
));
1749 /* check syntax and remove '<>' */
1750 if (len
< 2 || buf
[0] != '<' || buf
[len
- 1] != '>')
1751 goto include_syntax
;
1752 memmove(buf
, buf
+ 1, len
- 2);
1753 buf
[len
- 2] = '\0';
1758 if (s1
->include_stack_ptr
>= s1
->include_stack
+ INCLUDE_STACK_SIZE
)
1759 tcc_error("#include recursion too deep");
1760 /* store current file in stack, but increment stack later below */
1761 *s1
->include_stack_ptr
= file
;
1762 i
= tok
== TOK_INCLUDE_NEXT
? file
->include_next_index
: 0;
1763 n
= 2 + s1
->nb_include_paths
+ s1
->nb_sysinclude_paths
;
1764 for (; i
< n
; ++i
) {
1765 char buf1
[sizeof file
->filename
];
1770 /* check absolute include path */
1771 if (!IS_ABSPATH(buf
))
1775 } else if (i
== 1) {
1776 /* search in current dir if "header.h" */
1779 path
= file
->filename
;
1780 pstrncpy(buf1
, path
, tcc_basename(path
) - path
);
1783 /* search in all the include paths */
1784 int j
= i
- 2, k
= j
- s1
->nb_include_paths
;
1785 path
= k
< 0 ? s1
->include_paths
[j
] : s1
->sysinclude_paths
[k
];
1786 pstrcpy(buf1
, sizeof(buf1
), path
);
1787 pstrcat(buf1
, sizeof(buf1
), "/");
1790 pstrcat(buf1
, sizeof(buf1
), buf
);
1791 e
= search_cached_include(s1
, buf1
, 0);
1792 if (e
&& (define_find(e
->ifndef_macro
) || e
->once
== pp_once
)) {
1793 /* no need to parse the include because the 'ifndef macro'
1794 is defined (or had #pragma once) */
1796 printf("%s: skipping cached %s\n", file
->filename
, buf1
);
1801 if (tcc_open(s1
, buf1
) < 0)
1804 file
->include_next_index
= i
+ 1;
1806 printf("%s: including %s\n", file
->prev
->filename
, file
->filename
);
1808 /* update target deps */
1809 dynarray_add((void ***)&s1
->target_deps
, &s1
->nb_target_deps
,
1811 /* push current file in stack */
1812 ++s1
->include_stack_ptr
;
1813 /* add include file debug info */
1815 put_stabs(file
->filename
, N_BINCL
, 0, 0, 0);
1816 tok_flags
|= TOK_FLAG_BOF
| TOK_FLAG_BOL
;
1817 ch
= file
->buf_ptr
[0];
1820 tcc_error("include file '%s' not found", buf
);
1827 c
= expr_preprocess();
1833 if (tok
< TOK_IDENT
)
1834 tcc_error("invalid argument for '#if%sdef'", c
? "n" : "");
1838 printf("#ifndef %s\n", get_tok_str(tok
, NULL
));
1840 file
->ifndef_macro
= tok
;
1843 c
= (define_find(tok
) != 0) ^ c
;
1845 if (s1
->ifdef_stack_ptr
>= s1
->ifdef_stack
+ IFDEF_STACK_SIZE
)
1846 tcc_error("memory full (ifdef)");
1847 *s1
->ifdef_stack_ptr
++ = c
;
1850 if (s1
->ifdef_stack_ptr
== s1
->ifdef_stack
)
1851 tcc_error("#else without matching #if");
1852 if (s1
->ifdef_stack_ptr
[-1] & 2)
1853 tcc_error("#else after #else");
1854 c
= (s1
->ifdef_stack_ptr
[-1] ^= 3);
1857 if (s1
->ifdef_stack_ptr
== s1
->ifdef_stack
)
1858 tcc_error("#elif without matching #if");
1859 c
= s1
->ifdef_stack_ptr
[-1];
1861 tcc_error("#elif after #else");
1862 /* last #if/#elif expression was true: we skip */
1865 c
= expr_preprocess();
1866 s1
->ifdef_stack_ptr
[-1] = c
;
1868 if (s1
->ifdef_stack_ptr
== file
->ifdef_stack_ptr
+ 1)
1869 file
->ifndef_macro
= 0;
1879 if (s1
->ifdef_stack_ptr
<= file
->ifdef_stack_ptr
)
1880 tcc_error("#endif without matching #if");
1881 s1
->ifdef_stack_ptr
--;
1882 /* '#ifndef macro' was at the start of file. Now we check if
1883 an '#endif' is exactly at the end of file */
1884 if (file
->ifndef_macro
&&
1885 s1
->ifdef_stack_ptr
== file
->ifdef_stack_ptr
) {
1886 file
->ifndef_macro_saved
= file
->ifndef_macro
;
1887 /* need to set to zero to avoid false matches if another
1888 #ifndef at middle of file */
1889 file
->ifndef_macro
= 0;
1890 while (tok
!= TOK_LINEFEED
)
1892 tok_flags
|= TOK_FLAG_ENDIF
;
1897 n
= strtoul((char*)tokc
.str
.data
, &q
, 10);
1901 if (tok
!= TOK_CINT
)
1903 tcc_error("wrong #line format");
1907 if (tok
!= TOK_LINEFEED
) {
1909 pstrcpy(file
->filename
, sizeof(file
->filename
), (char *)tokc
.str
.data
);
1910 else if (parse_flags
& PARSE_FLAG_ASM_FILE
)
1917 total_lines
+= file
->line_num
- n
;
1920 put_stabs(file
->filename
, N_BINCL
, 0, 0, 0);
1925 ch
= file
->buf_ptr
[0];
1928 while (ch
!= '\n' && ch
!= CH_EOF
) {
1929 if ((q
- buf
) < sizeof(buf
) - 1)
1932 if (handle_stray_noerror() == 0)
1939 tcc_error("#error %s", buf
);
1941 tcc_warning("#warning %s", buf
);
1949 /* ignore gas line comment in an 'S' file. */
1950 if (saved_parse_flags
& PARSE_FLAG_ASM_FILE
)
1952 if (tok
== '!' && is_bof
)
1953 /* '!' is ignored at beginning to allow C scripts. */
1955 tcc_warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok
, &tokc
));
1957 file
->buf_ptr
= parse_line_comment(file
->buf_ptr
- 1);
1960 /* ignore other preprocess commands or #! for C scripts */
1961 while (tok
!= TOK_LINEFEED
)
1964 parse_flags
= saved_parse_flags
;
1967 /* evaluate escape codes in a string. */
1968 static void parse_escape_string(CString
*outstr
, const uint8_t *buf
, int is_long
)
1983 case '0': case '1': case '2': case '3':
1984 case '4': case '5': case '6': case '7':
1985 /* at most three octal digits */
1990 n
= n
* 8 + c
- '0';
1994 n
= n
* 8 + c
- '0';
1999 goto add_char_nonext
;
2007 if (c
>= 'a' && c
<= 'f')
2009 else if (c
>= 'A' && c
<= 'F')
2019 goto add_char_nonext
;
2043 goto invalid_escape
;
2053 if (c
>= '!' && c
<= '~')
2054 tcc_warning("unknown escape sequence: \'\\%c\'", c
);
2056 tcc_warning("unknown escape sequence: \'\\x%x\'", c
);
2063 cstr_ccat(outstr
, c
);
2065 cstr_wccat(outstr
, c
);
2067 /* add a trailing '\0' */
2069 cstr_ccat(outstr
, '\0');
2071 cstr_wccat(outstr
, '\0');
2074 static void parse_string(const char *s
, int len
)
2076 uint8_t buf
[1000], *p
= buf
;
2079 if ((is_long
= *s
== 'L'))
2083 if (len
>= sizeof buf
)
2084 p
= tcc_malloc(len
+ 1);
2088 cstr_reset(&tokcstr
);
2089 parse_escape_string(&tokcstr
, p
, is_long
);
2095 /* XXX: make it portable */
2099 char_size
= sizeof(nwchar_t
);
2100 if (tokcstr
.size
<= char_size
)
2101 tcc_error("empty character constant");
2102 if (tokcstr
.size
> 2 * char_size
)
2103 tcc_warning("multi-character character constant");
2105 tokc
.i
= *(int8_t *)tokcstr
.data
;
2108 tokc
.i
= *(nwchar_t
*)tokcstr
.data
;
2112 tokc
.str
.size
= tokcstr
.size
;
2113 tokc
.str
.data
= tokcstr
.data
;
2121 /* we use 64 bit numbers */
2124 /* bn = (bn << shift) | or_val */
2125 static void bn_lshift(unsigned int *bn
, int shift
, int or_val
)
2129 for(i
=0;i
<BN_SIZE
;i
++) {
2131 bn
[i
] = (v
<< shift
) | or_val
;
2132 or_val
= v
>> (32 - shift
);
2136 static void bn_zero(unsigned int *bn
)
2139 for(i
=0;i
<BN_SIZE
;i
++) {
2144 /* parse number in null terminated string 'p' and return it in the
2146 static void parse_number(const char *p
)
2148 int b
, t
, shift
, frac_bits
, s
, exp_val
, ch
;
2150 unsigned int bn
[BN_SIZE
];
2161 goto float_frac_parse
;
2162 } else if (t
== '0') {
2163 if (ch
== 'x' || ch
== 'X') {
2167 } else if (tcc_ext
&& (ch
== 'b' || ch
== 'B')) {
2173 /* parse all digits. cannot check octal numbers at this stage
2174 because of floating point constants */
2176 if (ch
>= 'a' && ch
<= 'f')
2178 else if (ch
>= 'A' && ch
<= 'F')
2186 if (q
>= token_buf
+ STRING_MAX_SIZE
) {
2188 tcc_error("number too long");
2194 ((ch
== 'e' || ch
== 'E') && b
== 10) ||
2195 ((ch
== 'p' || ch
== 'P') && (b
== 16 || b
== 2))) {
2197 /* NOTE: strtox should support that for hexa numbers, but
2198 non ISOC99 libcs do not support it, so we prefer to do
2200 /* hexadecimal or binary floats */
2201 /* XXX: handle overflows */
2213 } else if (t
>= 'a') {
2215 } else if (t
>= 'A') {
2220 bn_lshift(bn
, shift
, t
);
2227 if (t
>= 'a' && t
<= 'f') {
2229 } else if (t
>= 'A' && t
<= 'F') {
2231 } else if (t
>= '0' && t
<= '9') {
2237 tcc_error("invalid digit");
2238 bn_lshift(bn
, shift
, t
);
2243 if (ch
!= 'p' && ch
!= 'P')
2250 } else if (ch
== '-') {
2254 if (ch
< '0' || ch
> '9')
2255 expect("exponent digits");
2256 while (ch
>= '0' && ch
<= '9') {
2257 exp_val
= exp_val
* 10 + ch
- '0';
2260 exp_val
= exp_val
* s
;
2262 /* now we can generate the number */
2263 /* XXX: should patch directly float number */
2264 d
= (double)bn
[1] * 4294967296.0 + (double)bn
[0];
2265 d
= ldexp(d
, exp_val
- frac_bits
);
2270 /* float : should handle overflow */
2272 } else if (t
== 'L') {
2274 #ifdef TCC_TARGET_PE
2279 /* XXX: not large enough */
2280 tokc
.ld
= (long double)d
;
2287 /* decimal floats */
2289 if (q
>= token_buf
+ STRING_MAX_SIZE
)
2294 while (ch
>= '0' && ch
<= '9') {
2295 if (q
>= token_buf
+ STRING_MAX_SIZE
)
2301 if (ch
== 'e' || ch
== 'E') {
2302 if (q
>= token_buf
+ STRING_MAX_SIZE
)
2306 if (ch
== '-' || ch
== '+') {
2307 if (q
>= token_buf
+ STRING_MAX_SIZE
)
2312 if (ch
< '0' || ch
> '9')
2313 expect("exponent digits");
2314 while (ch
>= '0' && ch
<= '9') {
2315 if (q
>= token_buf
+ STRING_MAX_SIZE
)
2327 tokc
.f
= strtof(token_buf
, NULL
);
2328 } else if (t
== 'L') {
2330 #ifdef TCC_TARGET_PE
2332 tokc
.d
= strtod(token_buf
, NULL
);
2335 tokc
.ld
= strtold(token_buf
, NULL
);
2339 tokc
.d
= strtod(token_buf
, NULL
);
2343 unsigned long long n
, n1
;
2344 int lcount
, ucount
, must_64bit
;
2347 /* integer number */
2350 if (b
== 10 && *q
== '0') {
2357 /* no need for checks except for base 10 / 8 errors */
2367 tcc_error("invalid digit");
2370 /* detect overflow */
2371 /* XXX: this test is not reliable */
2373 tcc_error("integer constant overflow");
2376 /* Determine the characteristics (unsigned and/or 64bit) the type of
2377 the constant must have according to the constant suffix(es) */
2378 lcount
= ucount
= must_64bit
= 0;
2384 tcc_error("three 'l's in integer constant");
2385 if (lcount
&& *(p
- 1) != ch
)
2386 tcc_error("incorrect integer suffix: %s", p1
);
2388 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
2393 } else if (t
== 'U') {
2395 tcc_error("two 'u's in integer constant");
2403 /* Whether 64 bits are needed to hold the constant's value */
2404 if (n
& 0xffffffff00000000LL
|| must_64bit
) {
2412 /* Whether type must be unsigned to hold the constant's value */
2413 if (ucount
|| ((n1
>> 31) && (b
!= 10))) {
2414 if (tok
== TOK_CLLONG
)
2418 /* If decimal and no unsigned suffix, bump to 64 bits or throw error */
2419 } else if (n1
>> 31) {
2420 if (tok
== TOK_CINT
)
2423 tcc_error("integer constant overflow");
2429 tcc_error("invalid number\n");
2433 #define PARSE2(c1, tok1, c2, tok2) \
2444 /* return next token without macro substitution */
2445 static inline void next_nomacro1(void)
2447 int t
, c
, is_long
, len
;
2460 if (parse_flags
& PARSE_FLAG_SPACES
)
2461 goto keep_tok_flags
;
2462 while (isidnum_table
[*p
- CH_EOF
] & IS_SPC
)
2471 /* first look if it is in fact an end of buffer */
2472 c
= handle_stray1(p
);
2479 TCCState
*s1
= tcc_state
;
2480 if ((parse_flags
& PARSE_FLAG_LINEFEED
)
2481 && !(tok_flags
& TOK_FLAG_EOF
)) {
2482 tok_flags
|= TOK_FLAG_EOF
;
2484 goto keep_tok_flags
;
2485 } else if (!(parse_flags
& PARSE_FLAG_PREPROCESS
)) {
2487 } else if (s1
->ifdef_stack_ptr
!= file
->ifdef_stack_ptr
) {
2488 tcc_error("missing #endif");
2489 } else if (s1
->include_stack_ptr
== s1
->include_stack
) {
2490 /* no include left : end of file. */
2493 tok_flags
&= ~TOK_FLAG_EOF
;
2494 /* pop include file */
2496 /* test if previous '#endif' was after a #ifdef at
2498 if (tok_flags
& TOK_FLAG_ENDIF
) {
2500 printf("#endif %s\n", get_tok_str(file
->ifndef_macro_saved
, NULL
));
2502 search_cached_include(s1
, file
->filename
, 1)
2503 ->ifndef_macro
= file
->ifndef_macro_saved
;
2504 tok_flags
&= ~TOK_FLAG_ENDIF
;
2507 /* add end of include file debug info */
2508 if (tcc_state
->do_debug
) {
2509 put_stabd(N_EINCL
, 0, 0);
2511 /* pop include stack */
2513 s1
->include_stack_ptr
--;
2522 tok_flags
|= TOK_FLAG_BOL
;
2525 if (0 == (parse_flags
& PARSE_FLAG_LINEFEED
))
2528 goto keep_tok_flags
;
2533 if ((tok_flags
& TOK_FLAG_BOL
) &&
2534 (parse_flags
& PARSE_FLAG_PREPROCESS
)) {
2536 preprocess(tok_flags
& TOK_FLAG_BOF
);
2542 tok
= TOK_TWOSHARPS
;
2544 if (parse_flags
& PARSE_FLAG_ASM_FILE
) {
2545 p
= parse_line_comment(p
- 1);
2554 /* dollar is allowed to start identifiers when not parsing asm */
2556 if (!(isidnum_table
[c
- CH_EOF
] & IS_ID
)
2557 || (parse_flags
& PARSE_FLAG_ASM_FILE
))
2560 case 'a': case 'b': case 'c': case 'd':
2561 case 'e': case 'f': case 'g': case 'h':
2562 case 'i': case 'j': case 'k': case 'l':
2563 case 'm': case 'n': case 'o': case 'p':
2564 case 'q': case 'r': case 's': case 't':
2565 case 'u': case 'v': case 'w': case 'x':
2567 case 'A': case 'B': case 'C': case 'D':
2568 case 'E': case 'F': case 'G': case 'H':
2569 case 'I': case 'J': case 'K':
2570 case 'M': case 'N': case 'O': case 'P':
2571 case 'Q': case 'R': case 'S': case 'T':
2572 case 'U': case 'V': case 'W': case 'X':
2578 h
= TOK_HASH_FUNC(h
, c
);
2579 while (c
= *++p
, isidnum_table
[c
- CH_EOF
] & (IS_ID
|IS_NUM
))
2580 h
= TOK_HASH_FUNC(h
, c
);
2585 /* fast case : no stray found, so we have the full token
2586 and we have already hashed it */
2587 h
&= (TOK_HASH_SIZE
- 1);
2588 pts
= &hash_ident
[h
];
2593 if (ts
->len
== len
&& !memcmp(ts
->str
, p1
, len
))
2595 pts
= &(ts
->hash_next
);
2597 ts
= tok_alloc_new(pts
, (char *) p1
, len
);
2601 cstr_reset(&tokcstr
);
2602 cstr_cat(&tokcstr
, p1
, len
);
2606 while (isidnum_table
[c
- CH_EOF
] & (IS_ID
|IS_NUM
))
2608 cstr_ccat(&tokcstr
, c
);
2611 ts
= tok_alloc(tokcstr
.data
, tokcstr
.size
);
2617 if (t
!= '\\' && t
!= '\'' && t
!= '\"') {
2619 goto parse_ident_fast
;
2622 if (c
== '\'' || c
== '\"') {
2626 cstr_reset(&tokcstr
);
2627 cstr_ccat(&tokcstr
, 'L');
2628 goto parse_ident_slow
;
2633 case '0': case '1': case '2': case '3':
2634 case '4': case '5': case '6': case '7':
2638 /* after the first digit, accept digits, alpha, '.' or sign if
2639 prefixed by 'eEpP' */
2641 cstr_reset(&tokcstr
);
2643 cstr_ccat(&tokcstr
, t
);
2644 if (!((isidnum_table
[c
- CH_EOF
] & (IS_ID
|IS_NUM
))
2646 || ((c
== '+' || c
== '-')
2647 && (((t
== 'e' || t
== 'E')
2648 && !(parse_flags
& PARSE_FLAG_ASM_FILE
2649 /* 0xe+1 is 3 tokens in asm */
2650 && ((char*)tokcstr
.data
)[0] == '0'
2651 && toup(((char*)tokcstr
.data
)[1]) == 'X'))
2652 || t
== 'p' || t
== 'P'))))
2657 /* We add a trailing '\0' to ease parsing */
2658 cstr_ccat(&tokcstr
, '\0');
2659 tokc
.str
.size
= tokcstr
.size
;
2660 tokc
.str
.data
= tokcstr
.data
;
2665 /* special dot handling because it can also start a number */
2670 } else if ((parse_flags
& PARSE_FLAG_ASM_FILE
)
2671 && (isidnum_table
[c
- CH_EOF
] & (IS_ID
|IS_NUM
))) {
2673 goto parse_ident_fast
;
2674 } else if (c
== '.') {
2680 *--p
= '.'; /* may underflow into file->unget[] */
2691 cstr_reset(&tokcstr
);
2693 cstr_ccat(&tokcstr
, 'L');
2694 cstr_ccat(&tokcstr
, c
);
2695 p
= parse_pp_string(p
, c
, &tokcstr
);
2696 cstr_ccat(&tokcstr
, c
);
2697 cstr_ccat(&tokcstr
, '\0');
2698 tokc
.str
.size
= tokcstr
.size
;
2699 tokc
.str
.data
= tokcstr
.data
;
2708 } else if (c
== '<') {
2725 } else if (c
== '>') {
2743 } else if (c
== '=') {
2756 } else if (c
== '=') {
2769 } else if (c
== '=') {
2782 } else if (c
== '=') {
2785 } else if (c
== '>') {
2793 PARSE2('!', '!', '=', TOK_NE
)
2794 PARSE2('=', '=', '=', TOK_EQ
)
2795 PARSE2('*', '*', '=', TOK_A_MUL
)
2796 PARSE2('%', '%', '=', TOK_A_MOD
)
2797 PARSE2('^', '^', '=', TOK_A_XOR
)
2799 /* comments or operator */
2803 p
= parse_comment(p
);
2804 /* comments replaced by a blank */
2806 goto keep_tok_flags
;
2807 } else if (c
== '/') {
2808 p
= parse_line_comment(p
);
2810 goto keep_tok_flags
;
2811 } else if (c
== '=') {
2831 case '@': /* only used in assembler */
2837 if (c
>= 0x80 && c
<= 0xFF) /* utf8 identifiers */
2838 goto parse_ident_fast
;
2839 if (parse_flags
& PARSE_FLAG_ASM_FILE
)
2841 tcc_error("unrecognized character \\x%02x", c
);
2847 #if defined(PARSE_DEBUG)
2848 printf("token = %s\n", get_tok_str(tok
, &tokc
));
2852 /* return next token without macro substitution. Can read input from
2854 static void next_nomacro_spc(void)
2860 TOK_GET(&tok
, ¯o_ptr
, &tokc
);
2861 if (tok
== TOK_LINENUM
) {
2862 file
->line_num
= tokc
.i
;
2871 ST_FUNC
void next_nomacro(void)
2875 } while (tok
< 256 && (isidnum_table
[tok
- CH_EOF
] & IS_SPC
));
2879 static void macro_subst(
2880 TokenString
*tok_str
,
2882 const int *macro_str
,
2886 /* substitute arguments in replacement lists in macro_str by the values in
2887 args (field d) and return allocated string */
2888 static int *macro_arg_subst(Sym
**nested_list
, const int *macro_str
, Sym
*args
)
2900 TOK_GET(&t
, ¯o_str
, &cval
);
2905 TOK_GET(&t
, ¯o_str
, &cval
);
2908 s
= sym_find2(args
, t
);
2911 cstr_ccat(&cstr
, '\"');
2915 TOK_GET(&t
, &st
, &cval
);
2916 if (t
!= TOK_PLCHLDR
2918 && 0 == check_space(t
, &spc
)) {
2919 const char *s
= get_tok_str(t
, &cval
);
2921 if (t
== TOK_PPSTR
&& *s
!= '\'')
2922 add_char(&cstr
, *s
);
2924 cstr_ccat(&cstr
, *s
);
2930 cstr_ccat(&cstr
, '\"');
2931 cstr_ccat(&cstr
, '\0');
2933 printf("\nstringize: <%s>\n", (char *)cstr
.data
);
2936 cval
.str
.size
= cstr
.size
;
2937 cval
.str
.data
= cstr
.data
;
2938 tok_str_add2(&str
, TOK_PPSTR
, &cval
);
2942 expect("macro parameter after '#'");
2944 } else if (t
>= TOK_IDENT
) {
2945 s
= sym_find2(args
, t
);
2949 /* if '##' is present before or after, no arg substitution */
2950 if (*macro_str
== TOK_TWOSHARPS
|| t1
== TOK_TWOSHARPS
) {
2951 /* special case for var arg macros : ## eats the ','
2952 if empty VA_ARGS variable. */
2953 if (t1
== TOK_TWOSHARPS
&& t0
== ',' && gnu_ext
&& s
->type
.t
) {
2955 /* suppress ',' '##' */
2958 /* suppress '##' and add variable */
2965 TOK_GET(&t1
, &st
, &cval
);
2968 tok_str_add2(&str
, t1
, &cval
);
2974 /* NOTE: the stream cannot be read when macro
2975 substituing an argument */
2976 macro_subst(&str
, nested_list
, st
, 0);
2978 if (str
.len
== l0
) /* exanded to empty string */
2979 tok_str_add(&str
, TOK_PLCHLDR
);
2981 tok_str_add(&str
, t
);
2984 tok_str_add2(&str
, t
, &cval
);
2988 tok_str_add(&str
, 0);
2992 static char const ab_month_name
[12][4] =
2994 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2995 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
2998 /* peek or read [ws_str == NULL] next token from function macro call,
2999 walking up macro levels up to the file if necessary */
3000 static int next_argstream(Sym
**nested_list
, int can_read_stream
, TokenString
*ws_str
)
3008 p
= macro_ptr
, t
= *p
;
3010 while (is_space(t
) || TOK_LINEFEED
== t
)
3011 tok_str_add(ws_str
, t
), t
= *++p
;
3013 if (t
== 0 && can_read_stream
) {
3015 /* also, end of scope for nested defined symbol */
3017 while (sa
&& sa
->v
== 0)
3026 while (is_space(ch
) || ch
== '\n' || ch
== '/') {
3029 uint8_t *p
= file
->buf_ptr
;
3032 p
= parse_comment(p
);
3033 file
->buf_ptr
= p
- 1;
3034 } else if (c
== '/') {
3035 p
= parse_line_comment(p
);
3036 file
->buf_ptr
= p
- 1;
3041 tok_str_add(ws_str
, ch
);
3055 /* do macro substitution of current token with macro 's' and add
3056 result to (tok_str,tok_len). 'nested_list' is the list of all
3057 macros we got inside to avoid recursing. Return non zero if no
3058 substitution needs to be done */
3059 static int macro_subst_tok(
3060 TokenString
*tok_str
,
3063 int can_read_stream
)
3065 Sym
*args
, *sa
, *sa1
;
3066 int parlevel
, *mstr
, t
, t1
, spc
;
3073 /* if symbol is a macro, prepare substitution */
3074 /* special macros */
3075 if (tok
== TOK___LINE__
) {
3076 snprintf(buf
, sizeof(buf
), "%d", file
->line_num
);
3080 } else if (tok
== TOK___FILE__
) {
3081 cstrval
= file
->filename
;
3083 } else if (tok
== TOK___DATE__
|| tok
== TOK___TIME__
) {
3088 tm
= localtime(&ti
);
3089 if (tok
== TOK___DATE__
) {
3090 snprintf(buf
, sizeof(buf
), "%s %2d %d",
3091 ab_month_name
[tm
->tm_mon
], tm
->tm_mday
, tm
->tm_year
+ 1900);
3093 snprintf(buf
, sizeof(buf
), "%02d:%02d:%02d",
3094 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
3101 cstr_cat(&cstr
, cstrval
, 0);
3102 cval
.str
.size
= cstr
.size
;
3103 cval
.str
.data
= cstr
.data
;
3104 tok_str_add2(tok_str
, t1
, &cval
);
3107 int saved_parse_flags
= parse_flags
;
3110 if (s
->type
.t
== MACRO_FUNC
) {
3111 /* whitespace between macro name and argument list */
3113 tok_str_new(&ws_str
);
3116 parse_flags
|= PARSE_FLAG_SPACES
| PARSE_FLAG_LINEFEED
3117 | PARSE_FLAG_ACCEPT_STRAYS
;
3119 /* get next token from argument stream */
3120 t
= next_argstream(nested_list
, can_read_stream
, &ws_str
);
3122 /* not a macro substitution after all, restore the
3123 * macro token plus all whitespace we've read.
3124 * whitespace is intentionally not merged to preserve
3126 parse_flags
= saved_parse_flags
;
3127 tok_str_add(tok_str
, tok
);
3128 if (parse_flags
& PARSE_FLAG_SPACES
) {
3130 for (i
= 0; i
< ws_str
.len
; i
++)
3131 tok_str_add(tok_str
, ws_str
.str
[i
]);
3133 tok_str_free(ws_str
.str
);
3136 tok_str_free(ws_str
.str
);
3138 next_nomacro(); /* eat '(' */
3140 /* argument macro */
3143 /* NOTE: empty args are allowed, except if no args */
3146 next_argstream(nested_list
, can_read_stream
, NULL
);
3147 } while (is_space(tok
) || TOK_LINEFEED
== tok
);
3149 /* handle '()' case */
3150 if (!args
&& !sa
&& tok
== ')')
3153 tcc_error("macro '%s' used with too many args",
3154 get_tok_str(s
->v
, 0));
3157 /* NOTE: non zero sa->t indicates VA_ARGS */
3158 while ((parlevel
> 0 ||
3160 (tok
!= ',' || sa
->type
.t
)))) {
3161 if (tok
== TOK_EOF
|| tok
== 0)
3165 else if (tok
== ')')
3167 if (tok
== TOK_LINEFEED
)
3169 if (!check_space(tok
, &spc
))
3170 tok_str_add2(&str
, tok
, &tokc
);
3171 next_argstream(nested_list
, can_read_stream
, NULL
);
3176 tok_str_add(&str
, 0);
3177 sa1
= sym_push2(&args
, sa
->v
& ~SYM_FIELD
, sa
->type
.t
, 0);
3181 /* special case for gcc var args: add an empty
3182 var arg argument if it is omitted */
3183 if (sa
&& sa
->type
.t
&& gnu_ext
)
3191 tcc_error("macro '%s' used with too few args",
3192 get_tok_str(s
->v
, 0));
3195 parse_flags
= saved_parse_flags
;
3197 /* now subst each arg */
3198 mstr
= macro_arg_subst(nested_list
, mstr
, args
);
3203 tok_str_free(sa
->d
);
3209 sym_push2(nested_list
, s
->v
, 0, 0);
3210 parse_flags
= saved_parse_flags
;
3211 macro_subst(tok_str
, nested_list
, mstr
, can_read_stream
| 2);
3213 /* pop nested defined symbol */
3215 *nested_list
= sa1
->prev
;
3223 static int paste_tokens(int t1
, CValue
*v1
, int t2
, CValue
*v2
)
3229 if (t1
!= TOK_PLCHLDR
)
3230 cstr_cat(&cstr
, get_tok_str(t1
, v1
), -1);
3232 if (t2
!= TOK_PLCHLDR
)
3233 cstr_cat(&cstr
, get_tok_str(t2
, v2
), -1);
3234 cstr_ccat(&cstr
, '\0');
3236 tcc_open_bf(tcc_state
, ":paste:", cstr
.size
);
3237 memcpy(file
->buffer
, cstr
.data
, cstr
.size
);
3240 if (0 == *file
->buf_ptr
)
3244 tcc_warning("pasting \"%.*s\" and \"%s\" does not give a valid"
3245 " preprocessing token", n
, cstr
.data
, (char*)cstr
.data
+ n
);
3250 //printf("paste <%s>\n", (char*)cstr.data);
3255 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
3256 return the resulting string (which must be freed). */
3257 static inline int *macro_twosharps(const int *ptr0
)
3261 TokenString macro_str1
;
3262 int start_of_nosubsts
= -1;
3265 /* we search the first '##' */
3266 for (ptr
= ptr0
;;) {
3267 TOK_GET(&t
, &ptr
, &cval
);
3268 if (t
== TOK_TWOSHARPS
)
3274 tok_str_new(¯o_str1
);
3276 //tok_print(" $$$", ptr0);
3277 for (ptr
= ptr0
;;) {
3278 TOK_GET(&t
, &ptr
, &cval
);
3281 if (t
== TOK_TWOSHARPS
)
3283 while (*ptr
== TOK_TWOSHARPS
) {
3285 /* given 'a##b', remove nosubsts preceding 'a' */
3286 if (start_of_nosubsts
>= 0)
3287 macro_str1
.len
= start_of_nosubsts
;
3288 /* given 'a##b', remove nosubsts preceding 'b' */
3289 while ((t1
= *++ptr
) == TOK_NOSUBST
)
3291 if (t1
&& t1
!= TOK_TWOSHARPS
) {
3292 TOK_GET(&t1
, &ptr
, &cv1
);
3293 if (t
!= TOK_PLCHLDR
|| t1
!= TOK_PLCHLDR
) {
3294 if (paste_tokens(t
, &cval
, t1
, &cv1
)) {
3295 t
= tok
, cval
= tokc
;
3297 tok_str_add2(¯o_str1
, t
, &cval
);
3303 if (t
== TOK_NOSUBST
) {
3304 if (start_of_nosubsts
< 0)
3305 start_of_nosubsts
= macro_str1
.len
;
3307 start_of_nosubsts
= -1;
3309 tok_str_add2(¯o_str1
, t
, &cval
);
3311 tok_str_add(¯o_str1
, 0);
3312 //tok_print(" ###", macro_str1.str);
3313 return macro_str1
.str
;
3316 /* do macro substitution of macro_str and add result to
3317 (tok_str,tok_len). 'nested_list' is the list of all macros we got
3318 inside to avoid recursing. */
3319 static void macro_subst(
3320 TokenString
*tok_str
,
3322 const int *macro_str
,
3328 int t
, spc
, nosubst
;
3330 int *macro_str1
= NULL
;
3332 /* first scan for '##' operator handling */
3336 /* first scan for '##' operator handling */
3337 if (can_read_stream
& 1) {
3338 macro_str1
= macro_twosharps(ptr
);
3344 TOK_GET(&t
, &ptr
, &cval
);
3348 if (t
>= TOK_IDENT
&& 0 == nosubst
) {
3353 /* if nested substitution, do nothing */
3354 if (sym_find2(*nested_list
, t
)) {
3355 /* and mark it as TOK_NOSUBST, so it doesn't get subst'd again */
3356 tok_str_add2(tok_str
, TOK_NOSUBST
, NULL
);
3362 str
.str
= (int*)ptr
;
3363 begin_macro(&str
, 2);
3366 macro_subst_tok(tok_str
, nested_list
, s
, can_read_stream
);
3368 if (str
.alloc
== 3) {
3369 /* already finished by reading function macro arguments */
3377 spc
= (tok_str
->len
&&
3378 is_space(tok_last(tok_str
->str
,
3379 tok_str
->str
+ tok_str
->len
)));
3383 if (t
== '\\' && !(parse_flags
& PARSE_FLAG_ACCEPT_STRAYS
))
3384 tcc_error("stray '\\' in program");
3387 if (!check_space(t
, &spc
))
3388 tok_str_add2(tok_str
, t
, &cval
);
3390 if (t
== TOK_NOSUBST
)
3395 tok_str_free(macro_str1
);
3399 /* return next token with macro substitution */
3400 ST_FUNC
void next(void)
3403 if (parse_flags
& PARSE_FLAG_SPACES
)
3409 if (tok
== TOK_NOSUBST
|| tok
== TOK_PLCHLDR
) {
3410 /* discard preprocessor markers */
3412 } else if (tok
== 0) {
3413 /* end of macro or unget token string */
3417 } else if (tok
>= TOK_IDENT
&& (parse_flags
& PARSE_FLAG_PREPROCESS
)) {
3419 /* if reading from file, try to substitute macros */
3420 s
= define_find(tok
);
3422 Sym
*nested_list
= NULL
;
3424 macro_subst_tok(&tokstr_buf
, &nested_list
, s
, 1);
3425 tok_str_add(&tokstr_buf
, 0);
3426 begin_macro(&tokstr_buf
, 2);
3430 /* convert preprocessor tokens into C tokens */
3431 if (tok
== TOK_PPNUM
) {
3432 if (parse_flags
& PARSE_FLAG_TOK_NUM
)
3433 parse_number((char *)tokc
.str
.data
);
3434 } else if (tok
== TOK_PPSTR
) {
3435 if (parse_flags
& PARSE_FLAG_TOK_STR
)
3436 parse_string((char *)tokc
.str
.data
, tokc
.str
.size
- 1);
3440 /* push back current token and set current token to 'last_tok'. Only
3441 identifier case handled for labels. */
3442 ST_INLN
void unget_tok(int last_tok
)
3445 TokenString
*str
= tok_str_alloc();
3446 tok_str_add2(str
, tok
, &tokc
);
3447 tok_str_add(str
, 0);
3448 begin_macro(str
, 1);
3452 ST_FUNC
void preprocess_init(TCCState
*s1
)
3454 s1
->include_stack_ptr
= s1
->include_stack
;
3455 /* XXX: move that before to avoid having to initialize
3456 file->ifdef_stack_ptr ? */
3457 s1
->ifdef_stack_ptr
= s1
->ifdef_stack
;
3458 file
->ifdef_stack_ptr
= s1
->ifdef_stack_ptr
;
3461 pvtop
= vtop
= vstack
- 1;
3462 s1
->pack_stack
[0] = 0;
3463 s1
->pack_stack_ptr
= s1
->pack_stack
;
3465 isidnum_table
['$' - CH_EOF
] =
3466 s1
->dollars_in_identifiers
? IS_ID
: 0;
3467 isidnum_table
['.' - CH_EOF
] =
3468 (parse_flags
& PARSE_FLAG_ASM_FILE
) ? IS_ID
: 0;
3471 ST_FUNC
void preprocess_new(void)
3476 /* init isid table */
3477 for(i
= CH_EOF
; i
<128; i
++)
3478 isidnum_table
[i
- CH_EOF
]
3479 = is_space(i
) ? IS_SPC
3484 for(i
= 128; i
<256; i
++)
3485 isidnum_table
[i
- CH_EOF
] = IS_ID
;
3487 /* init allocators */
3488 tal_new(&toksym_alloc
, TOKSYM_TAL_LIMIT
, TOKSYM_TAL_SIZE
);
3489 tal_new(&tokstr_alloc
, TOKSTR_TAL_LIMIT
, TOKSTR_TAL_SIZE
);
3490 tal_new(&cstr_alloc
, CSTR_TAL_LIMIT
, CSTR_TAL_SIZE
);
3492 memset(hash_ident
, 0, TOK_HASH_SIZE
* sizeof(TokenSym
*));
3493 cstr_new(&cstr_buf
);
3494 cstr_realloc(&cstr_buf
, STRING_MAX_SIZE
);
3495 tok_str_new(&tokstr_buf
);
3496 tok_str_realloc(&tokstr_buf
, TOKSTR_MAX_SIZE
);
3498 tok_ident
= TOK_IDENT
;
3507 tok_alloc(p
, r
- p
- 1);
3512 ST_FUNC
void preprocess_delete(void)
3516 /* free -D and compiler defines */
3519 /* cleanup from error/setjmp */
3525 n
= tok_ident
- TOK_IDENT
;
3526 for(i
= 0; i
< n
; i
++)
3527 tal_free(toksym_alloc
, table_ident
[i
]);
3528 tcc_free(table_ident
);
3531 /* free static buffers */
3532 cstr_free(&tokcstr
);
3533 cstr_free(&cstr_buf
);
3534 tok_str_free(tokstr_buf
.str
);
3536 /* free allocators */
3537 tal_delete(toksym_alloc
);
3538 toksym_alloc
= NULL
;
3539 tal_delete(tokstr_alloc
);
3540 tokstr_alloc
= NULL
;
3541 tal_delete(cstr_alloc
);
3545 /* ------------------------------------------------------------------------- */
3546 /* tcc -E [-P[1]] [-dD} support */
3548 static void tok_print(const char *msg
, const int *str
)
3554 fp
= tcc_state
->ppfp
;
3555 if (!fp
|| !tcc_state
->dflag
)
3558 fprintf(fp
, "%s ", msg
);
3560 TOK_GET(&t
, &str
, &cval
);
3563 fprintf(fp
,"%s", get_tok_str(t
, &cval
));
3568 static void pp_line(TCCState
*s1
, BufferedFile
*f
, int level
)
3570 int d
= f
->line_num
- f
->line_ref
;
3573 if (s1
->Pflag
== LINE_MACRO_OUTPUT_FORMAT_NONE
) {
3574 if (level
== 0 && f
->line_ref
&& d
) {
3578 } else if (level
== 0 && f
->line_ref
&& d
< 8) {
3581 fputs("\n", s1
->ppfp
), --d
;
3582 } else if (s1
->Pflag
== LINE_MACRO_OUTPUT_FORMAT_STD
) {
3583 fprintf(s1
->ppfp
, "#line %d \"%s\"\n", f
->line_num
, f
->filename
);
3585 fprintf(s1
->ppfp
, "# %d \"%s\"%s\n", f
->line_num
, f
->filename
,
3586 level
> 0 ? " 1" : level
< 0 ? " 2" : "");
3588 f
->line_ref
= f
->line_num
;
3591 static void define_print(TCCState
*s1
, int v
)
3597 if (NULL
== s
|| NULL
== s
->d
)
3601 fprintf(fp
, "#define %s", get_tok_str(v
, NULL
));
3602 if (s
->type
.t
== MACRO_FUNC
) {
3607 fprintf(fp
,"%s", get_tok_str(a
->v
& ~SYM_FIELD
, NULL
));
3614 tok_print("", s
->d
);
3617 static void pp_debug_defines(TCCState
*s1
)
3628 pp_line(s1
, file
, 0);
3629 file
->line_ref
= ++file
->line_num
;
3633 vs
= get_tok_str(v
, NULL
);
3634 if (t
== TOK_DEFINE
) {
3635 define_print(s1
, v
);
3636 } else if (t
== TOK_UNDEF
) {
3637 fprintf(fp
, "#undef %s\n", vs
);
3638 } else if (t
== TOK_push_macro
) {
3639 fprintf(fp
, "#pragma push_macro(\"%s\")\n", vs
);
3640 } else if (t
== TOK_pop_macro
) {
3641 fprintf(fp
, "#pragma pop_macro(\"%s\")\n", vs
);
3646 static void pp_debug_builtins(TCCState
*s1
)
3649 for (v
= TOK_IDENT
; v
< tok_ident
; ++v
)
3650 define_print(s1
, v
);
3653 /* Add a space between tokens a and b to avoid unwanted textual pasting */
3654 static int pp_need_space(int a
, int b
)
3656 return 'E' == a
? '+' == b
|| '-' == b
3657 : '+' == a
? TOK_INC
== b
|| '+' == b
3658 : '-' == a
? TOK_DEC
== b
|| '-' == b
3659 : a
>= TOK_IDENT
? b
>= TOK_IDENT
3663 /* maybe hex like 0x1e */
3664 static int pp_check_he0xE(int t
, const char *p
)
3666 if (t
== TOK_PPNUM
&& toup(strchr(p
, 0)[-1]) == 'E')
3671 /* Preprocess the current file */
3672 ST_FUNC
int tcc_preprocess(TCCState
*s1
)
3674 BufferedFile
**iptr
;
3675 int token_seen
, spcs
, level
;
3679 preprocess_init(s1
);
3680 ch
= file
->buf_ptr
[0];
3681 tok_flags
= TOK_FLAG_BOL
| TOK_FLAG_BOF
;
3682 parse_flags
= PARSE_FLAG_PREPROCESS
3683 | (parse_flags
& PARSE_FLAG_ASM_FILE
)
3684 | PARSE_FLAG_LINEFEED
3686 | PARSE_FLAG_ACCEPT_STRAYS
3688 define_start
= define_stack
;
3690 /* Credits to Fabrice Bellard's initial revision to demonstrate its
3691 capability to compile and run itself, provided all numbers are
3692 given as decimals. tcc -E -P10 will do. */
3693 if (s1
->Pflag
== 1 + 10)
3694 parse_flags
|= PARSE_FLAG_TOK_NUM
, s1
->Pflag
= 1;
3697 /* for PP benchmarks */
3698 do next(); while (tok
!= TOK_EOF
); return 0;
3701 if (s1
->dflag
& 1) {
3702 pp_debug_builtins(s1
);
3706 token_seen
= TOK_LINEFEED
, spcs
= 0;
3707 pp_line(s1
, file
, 0);
3710 iptr
= s1
->include_stack_ptr
;
3714 level
= s1
->include_stack_ptr
- iptr
;
3717 pp_line(s1
, *iptr
, 0);
3718 pp_line(s1
, file
, level
);
3722 pp_debug_defines(s1
);
3727 if (token_seen
== TOK_LINEFEED
) {
3732 if (tok
== TOK_LINEFEED
) {
3736 pp_line(s1
, file
, 0);
3737 } else if (tok
== TOK_LINEFEED
) {
3740 spcs
= pp_need_space(token_seen
, tok
);
3744 fputs(" ", s1
->ppfp
), --spcs
;
3745 fputs(p
= get_tok_str(tok
, &tokc
), s1
->ppfp
);
3746 token_seen
= pp_check_he0xE(tok
, p
);;
3748 /* reset define stack, but keep -D and built-ins */
3749 free_defines(define_start
);
3753 /* ------------------------------------------------------------------------- */