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
);
975 } else if (parse_flags
& PARSE_FLAG_ASM_FILE
)
976 p
= parse_line_comment(p
);
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 if (path
== 0) continue;
1787 pstrcpy(buf1
, sizeof(buf1
), path
);
1788 pstrcat(buf1
, sizeof(buf1
), "/");
1791 pstrcat(buf1
, sizeof(buf1
), buf
);
1792 e
= search_cached_include(s1
, buf1
, 0);
1793 if (e
&& (define_find(e
->ifndef_macro
) || e
->once
== pp_once
)) {
1794 /* no need to parse the include because the 'ifndef macro'
1795 is defined (or had #pragma once) */
1797 printf("%s: skipping cached %s\n", file
->filename
, buf1
);
1802 if (tcc_open(s1
, buf1
) < 0)
1805 file
->include_next_index
= i
+ 1;
1807 printf("%s: including %s\n", file
->prev
->filename
, file
->filename
);
1809 /* update target deps */
1810 dynarray_add((void ***)&s1
->target_deps
, &s1
->nb_target_deps
,
1812 /* push current file in stack */
1813 ++s1
->include_stack_ptr
;
1814 /* add include file debug info */
1816 put_stabs(file
->filename
, N_BINCL
, 0, 0, 0);
1817 tok_flags
|= TOK_FLAG_BOF
| TOK_FLAG_BOL
;
1818 ch
= file
->buf_ptr
[0];
1821 tcc_error("include file '%s' not found", buf
);
1828 c
= expr_preprocess();
1834 if (tok
< TOK_IDENT
)
1835 tcc_error("invalid argument for '#if%sdef'", c
? "n" : "");
1839 printf("#ifndef %s\n", get_tok_str(tok
, NULL
));
1841 file
->ifndef_macro
= tok
;
1844 c
= (define_find(tok
) != 0) ^ c
;
1846 if (s1
->ifdef_stack_ptr
>= s1
->ifdef_stack
+ IFDEF_STACK_SIZE
)
1847 tcc_error("memory full (ifdef)");
1848 *s1
->ifdef_stack_ptr
++ = c
;
1851 if (s1
->ifdef_stack_ptr
== s1
->ifdef_stack
)
1852 tcc_error("#else without matching #if");
1853 if (s1
->ifdef_stack_ptr
[-1] & 2)
1854 tcc_error("#else after #else");
1855 c
= (s1
->ifdef_stack_ptr
[-1] ^= 3);
1858 if (s1
->ifdef_stack_ptr
== s1
->ifdef_stack
)
1859 tcc_error("#elif without matching #if");
1860 c
= s1
->ifdef_stack_ptr
[-1];
1862 tcc_error("#elif after #else");
1863 /* last #if/#elif expression was true: we skip */
1866 c
= expr_preprocess();
1867 s1
->ifdef_stack_ptr
[-1] = c
;
1869 if (s1
->ifdef_stack_ptr
== file
->ifdef_stack_ptr
+ 1)
1870 file
->ifndef_macro
= 0;
1880 if (s1
->ifdef_stack_ptr
<= file
->ifdef_stack_ptr
)
1881 tcc_error("#endif without matching #if");
1882 s1
->ifdef_stack_ptr
--;
1883 /* '#ifndef macro' was at the start of file. Now we check if
1884 an '#endif' is exactly at the end of file */
1885 if (file
->ifndef_macro
&&
1886 s1
->ifdef_stack_ptr
== file
->ifdef_stack_ptr
) {
1887 file
->ifndef_macro_saved
= file
->ifndef_macro
;
1888 /* need to set to zero to avoid false matches if another
1889 #ifndef at middle of file */
1890 file
->ifndef_macro
= 0;
1891 while (tok
!= TOK_LINEFEED
)
1893 tok_flags
|= TOK_FLAG_ENDIF
;
1898 n
= strtoul((char*)tokc
.str
.data
, &q
, 10);
1902 if (tok
!= TOK_CINT
)
1904 tcc_error("wrong #line format");
1908 if (tok
!= TOK_LINEFEED
) {
1910 pstrcpy(file
->filename
, sizeof(file
->filename
), (char *)tokc
.str
.data
);
1911 else if (parse_flags
& PARSE_FLAG_ASM_FILE
)
1918 total_lines
+= file
->line_num
- n
;
1921 put_stabs(file
->filename
, N_BINCL
, 0, 0, 0);
1926 ch
= file
->buf_ptr
[0];
1929 while (ch
!= '\n' && ch
!= CH_EOF
) {
1930 if ((q
- buf
) < sizeof(buf
) - 1)
1933 if (handle_stray_noerror() == 0)
1940 tcc_error("#error %s", buf
);
1942 tcc_warning("#warning %s", buf
);
1950 /* ignore gas line comment in an 'S' file. */
1951 if (saved_parse_flags
& PARSE_FLAG_ASM_FILE
)
1953 if (tok
== '!' && is_bof
)
1954 /* '!' is ignored at beginning to allow C scripts. */
1956 tcc_warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok
, &tokc
));
1958 file
->buf_ptr
= parse_line_comment(file
->buf_ptr
);
1961 /* ignore other preprocess commands or #! for C scripts */
1962 while (tok
!= TOK_LINEFEED
)
1965 parse_flags
= saved_parse_flags
;
1968 /* evaluate escape codes in a string. */
1969 static void parse_escape_string(CString
*outstr
, const uint8_t *buf
, int is_long
)
1984 case '0': case '1': case '2': case '3':
1985 case '4': case '5': case '6': case '7':
1986 /* at most three octal digits */
1991 n
= n
* 8 + c
- '0';
1995 n
= n
* 8 + c
- '0';
2000 goto add_char_nonext
;
2008 if (c
>= 'a' && c
<= 'f')
2010 else if (c
>= 'A' && c
<= 'F')
2020 goto add_char_nonext
;
2044 goto invalid_escape
;
2054 if (c
>= '!' && c
<= '~')
2055 tcc_warning("unknown escape sequence: \'\\%c\'", c
);
2057 tcc_warning("unknown escape sequence: \'\\x%x\'", c
);
2064 cstr_ccat(outstr
, c
);
2066 cstr_wccat(outstr
, c
);
2068 /* add a trailing '\0' */
2070 cstr_ccat(outstr
, '\0');
2072 cstr_wccat(outstr
, '\0');
2075 static void parse_string(const char *s
, int len
)
2077 uint8_t buf
[1000], *p
= buf
;
2080 if ((is_long
= *s
== 'L'))
2084 if (len
>= sizeof buf
)
2085 p
= tcc_malloc(len
+ 1);
2089 cstr_reset(&tokcstr
);
2090 parse_escape_string(&tokcstr
, p
, is_long
);
2096 /* XXX: make it portable */
2100 char_size
= sizeof(nwchar_t
);
2101 if (tokcstr
.size
<= char_size
)
2102 tcc_error("empty character constant");
2103 if (tokcstr
.size
> 2 * char_size
)
2104 tcc_warning("multi-character character constant");
2106 tokc
.i
= *(int8_t *)tokcstr
.data
;
2109 tokc
.i
= *(nwchar_t
*)tokcstr
.data
;
2113 tokc
.str
.size
= tokcstr
.size
;
2114 tokc
.str
.data
= tokcstr
.data
;
2122 /* we use 64 bit numbers */
2125 /* bn = (bn << shift) | or_val */
2126 static void bn_lshift(unsigned int *bn
, int shift
, int or_val
)
2130 for(i
=0;i
<BN_SIZE
;i
++) {
2132 bn
[i
] = (v
<< shift
) | or_val
;
2133 or_val
= v
>> (32 - shift
);
2137 static void bn_zero(unsigned int *bn
)
2140 for(i
=0;i
<BN_SIZE
;i
++) {
2145 /* parse number in null terminated string 'p' and return it in the
2147 static void parse_number(const char *p
)
2149 int b
, t
, shift
, frac_bits
, s
, exp_val
, ch
;
2151 unsigned int bn
[BN_SIZE
];
2162 goto float_frac_parse
;
2163 } else if (t
== '0') {
2164 if (ch
== 'x' || ch
== 'X') {
2168 } else if (tcc_ext
&& (ch
== 'b' || ch
== 'B')) {
2174 /* parse all digits. cannot check octal numbers at this stage
2175 because of floating point constants */
2177 if (ch
>= 'a' && ch
<= 'f')
2179 else if (ch
>= 'A' && ch
<= 'F')
2187 if (q
>= token_buf
+ STRING_MAX_SIZE
) {
2189 tcc_error("number too long");
2195 ((ch
== 'e' || ch
== 'E') && b
== 10) ||
2196 ((ch
== 'p' || ch
== 'P') && (b
== 16 || b
== 2))) {
2198 /* NOTE: strtox should support that for hexa numbers, but
2199 non ISOC99 libcs do not support it, so we prefer to do
2201 /* hexadecimal or binary floats */
2202 /* XXX: handle overflows */
2214 } else if (t
>= 'a') {
2216 } else if (t
>= 'A') {
2221 bn_lshift(bn
, shift
, t
);
2228 if (t
>= 'a' && t
<= 'f') {
2230 } else if (t
>= 'A' && t
<= 'F') {
2232 } else if (t
>= '0' && t
<= '9') {
2238 tcc_error("invalid digit");
2239 bn_lshift(bn
, shift
, t
);
2244 if (ch
!= 'p' && ch
!= 'P')
2251 } else if (ch
== '-') {
2255 if (ch
< '0' || ch
> '9')
2256 expect("exponent digits");
2257 while (ch
>= '0' && ch
<= '9') {
2258 exp_val
= exp_val
* 10 + ch
- '0';
2261 exp_val
= exp_val
* s
;
2263 /* now we can generate the number */
2264 /* XXX: should patch directly float number */
2265 d
= (double)bn
[1] * 4294967296.0 + (double)bn
[0];
2266 d
= ldexp(d
, exp_val
- frac_bits
);
2271 /* float : should handle overflow */
2273 } else if (t
== 'L') {
2275 #ifdef TCC_TARGET_PE
2280 /* XXX: not large enough */
2281 tokc
.ld
= (long double)d
;
2288 /* decimal floats */
2290 if (q
>= token_buf
+ STRING_MAX_SIZE
)
2295 while (ch
>= '0' && ch
<= '9') {
2296 if (q
>= token_buf
+ STRING_MAX_SIZE
)
2302 if (ch
== 'e' || ch
== 'E') {
2303 if (q
>= token_buf
+ STRING_MAX_SIZE
)
2307 if (ch
== '-' || ch
== '+') {
2308 if (q
>= token_buf
+ STRING_MAX_SIZE
)
2313 if (ch
< '0' || ch
> '9')
2314 expect("exponent digits");
2315 while (ch
>= '0' && ch
<= '9') {
2316 if (q
>= token_buf
+ STRING_MAX_SIZE
)
2328 tokc
.f
= strtof(token_buf
, NULL
);
2329 } else if (t
== 'L') {
2331 #ifdef TCC_TARGET_PE
2333 tokc
.d
= strtod(token_buf
, NULL
);
2336 tokc
.ld
= strtold(token_buf
, NULL
);
2340 tokc
.d
= strtod(token_buf
, NULL
);
2344 unsigned long long n
, n1
;
2345 int lcount
, ucount
, must_64bit
;
2348 /* integer number */
2351 if (b
== 10 && *q
== '0') {
2358 /* no need for checks except for base 10 / 8 errors */
2368 tcc_error("invalid digit");
2371 /* detect overflow */
2372 /* XXX: this test is not reliable */
2374 tcc_error("integer constant overflow");
2377 /* Determine the characteristics (unsigned and/or 64bit) the type of
2378 the constant must have according to the constant suffix(es) */
2379 lcount
= ucount
= must_64bit
= 0;
2385 tcc_error("three 'l's in integer constant");
2386 if (lcount
&& *(p
- 1) != ch
)
2387 tcc_error("incorrect integer suffix: %s", p1
);
2389 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
2394 } else if (t
== 'U') {
2396 tcc_error("two 'u's in integer constant");
2404 /* Whether 64 bits are needed to hold the constant's value */
2405 if (n
& 0xffffffff00000000LL
|| must_64bit
) {
2413 /* Whether type must be unsigned to hold the constant's value */
2414 if (ucount
|| ((n1
>> 31) && (b
!= 10))) {
2415 if (tok
== TOK_CLLONG
)
2419 /* If decimal and no unsigned suffix, bump to 64 bits or throw error */
2420 } else if (n1
>> 31) {
2421 if (tok
== TOK_CINT
)
2424 tcc_error("integer constant overflow");
2430 tcc_error("invalid number\n");
2434 #define PARSE2(c1, tok1, c2, tok2) \
2445 /* return next token without macro substitution */
2446 static inline void next_nomacro1(void)
2448 int t
, c
, is_long
, len
;
2461 if (parse_flags
& PARSE_FLAG_SPACES
)
2462 goto keep_tok_flags
;
2463 while (isidnum_table
[*p
- CH_EOF
] & IS_SPC
)
2472 /* first look if it is in fact an end of buffer */
2473 c
= handle_stray1(p
);
2480 TCCState
*s1
= tcc_state
;
2481 if ((parse_flags
& PARSE_FLAG_LINEFEED
)
2482 && !(tok_flags
& TOK_FLAG_EOF
)) {
2483 tok_flags
|= TOK_FLAG_EOF
;
2485 goto keep_tok_flags
;
2486 } else if (!(parse_flags
& PARSE_FLAG_PREPROCESS
)) {
2488 } else if (s1
->ifdef_stack_ptr
!= file
->ifdef_stack_ptr
) {
2489 tcc_error("missing #endif");
2490 } else if (s1
->include_stack_ptr
== s1
->include_stack
) {
2491 /* no include left : end of file. */
2494 tok_flags
&= ~TOK_FLAG_EOF
;
2495 /* pop include file */
2497 /* test if previous '#endif' was after a #ifdef at
2499 if (tok_flags
& TOK_FLAG_ENDIF
) {
2501 printf("#endif %s\n", get_tok_str(file
->ifndef_macro_saved
, NULL
));
2503 search_cached_include(s1
, file
->filename
, 1)
2504 ->ifndef_macro
= file
->ifndef_macro_saved
;
2505 tok_flags
&= ~TOK_FLAG_ENDIF
;
2508 /* add end of include file debug info */
2509 if (tcc_state
->do_debug
) {
2510 put_stabd(N_EINCL
, 0, 0);
2512 /* pop include stack */
2514 s1
->include_stack_ptr
--;
2523 tok_flags
|= TOK_FLAG_BOL
;
2526 if (0 == (parse_flags
& PARSE_FLAG_LINEFEED
))
2529 goto keep_tok_flags
;
2534 if ((tok_flags
& TOK_FLAG_BOL
) &&
2535 (parse_flags
& PARSE_FLAG_PREPROCESS
)) {
2537 preprocess(tok_flags
& TOK_FLAG_BOF
);
2543 tok
= TOK_TWOSHARPS
;
2545 if (parse_flags
& PARSE_FLAG_ASM_FILE
) {
2546 p
= parse_line_comment(p
- 1);
2555 /* dollar is allowed to start identifiers when not parsing asm */
2557 if (!(isidnum_table
[c
- CH_EOF
] & IS_ID
)
2558 || (parse_flags
& PARSE_FLAG_ASM_FILE
))
2561 case 'a': case 'b': case 'c': case 'd':
2562 case 'e': case 'f': case 'g': case 'h':
2563 case 'i': case 'j': case 'k': case 'l':
2564 case 'm': case 'n': case 'o': case 'p':
2565 case 'q': case 'r': case 's': case 't':
2566 case 'u': case 'v': case 'w': case 'x':
2568 case 'A': case 'B': case 'C': case 'D':
2569 case 'E': case 'F': case 'G': case 'H':
2570 case 'I': case 'J': case 'K':
2571 case 'M': case 'N': case 'O': case 'P':
2572 case 'Q': case 'R': case 'S': case 'T':
2573 case 'U': case 'V': case 'W': case 'X':
2579 h
= TOK_HASH_FUNC(h
, c
);
2580 while (c
= *++p
, isidnum_table
[c
- CH_EOF
] & (IS_ID
|IS_NUM
))
2581 h
= TOK_HASH_FUNC(h
, c
);
2586 /* fast case : no stray found, so we have the full token
2587 and we have already hashed it */
2588 h
&= (TOK_HASH_SIZE
- 1);
2589 pts
= &hash_ident
[h
];
2594 if (ts
->len
== len
&& !memcmp(ts
->str
, p1
, len
))
2596 pts
= &(ts
->hash_next
);
2598 ts
= tok_alloc_new(pts
, (char *) p1
, len
);
2602 cstr_reset(&tokcstr
);
2603 cstr_cat(&tokcstr
, p1
, len
);
2607 while (isidnum_table
[c
- CH_EOF
] & (IS_ID
|IS_NUM
))
2609 cstr_ccat(&tokcstr
, c
);
2612 ts
= tok_alloc(tokcstr
.data
, tokcstr
.size
);
2618 if (t
!= '\\' && t
!= '\'' && t
!= '\"') {
2620 goto parse_ident_fast
;
2623 if (c
== '\'' || c
== '\"') {
2627 cstr_reset(&tokcstr
);
2628 cstr_ccat(&tokcstr
, 'L');
2629 goto parse_ident_slow
;
2634 case '0': case '1': case '2': case '3':
2635 case '4': case '5': case '6': case '7':
2637 cstr_reset(&tokcstr
);
2638 /* after the first digit, accept digits, alpha, '.' or sign if
2639 prefixed by 'eEpP' */
2643 cstr_ccat(&tokcstr
, c
);
2645 if (!((isidnum_table
[c
- CH_EOF
] & (IS_ID
|IS_NUM
))
2647 || ((c
== '+' || c
== '-')
2648 && (t
== 'e' || t
== 'E' || t
== 'p' || t
== 'P')
2649 && !(parse_flags
& PARSE_FLAG_ASM_FILE
)
2653 /* We add a trailing '\0' to ease parsing */
2654 cstr_ccat(&tokcstr
, '\0');
2655 tokc
.str
.size
= tokcstr
.size
;
2656 tokc
.str
.data
= tokcstr
.data
;
2661 /* special dot handling because it can also start a number */
2664 cstr_reset(&tokcstr
);
2665 cstr_ccat(&tokcstr
, '.');
2667 } else if ((parse_flags
& PARSE_FLAG_ASM_FILE
)
2668 && (isidnum_table
[c
- CH_EOF
] & (IS_ID
|IS_NUM
))) {
2670 goto parse_ident_fast
;
2671 } else if (c
== '.') {
2677 *--p
= '.'; /* may underflow into file->unget[] */
2688 cstr_reset(&tokcstr
);
2690 cstr_ccat(&tokcstr
, 'L');
2691 cstr_ccat(&tokcstr
, c
);
2692 p
= parse_pp_string(p
, c
, &tokcstr
);
2693 cstr_ccat(&tokcstr
, c
);
2694 cstr_ccat(&tokcstr
, '\0');
2695 tokc
.str
.size
= tokcstr
.size
;
2696 tokc
.str
.data
= tokcstr
.data
;
2705 } else if (c
== '<') {
2722 } else if (c
== '>') {
2740 } else if (c
== '=') {
2753 } else if (c
== '=') {
2766 } else if (c
== '=') {
2779 } else if (c
== '=') {
2782 } else if (c
== '>') {
2790 PARSE2('!', '!', '=', TOK_NE
)
2791 PARSE2('=', '=', '=', TOK_EQ
)
2792 PARSE2('*', '*', '=', TOK_A_MUL
)
2793 PARSE2('%', '%', '=', TOK_A_MOD
)
2794 PARSE2('^', '^', '=', TOK_A_XOR
)
2796 /* comments or operator */
2800 p
= parse_comment(p
);
2801 /* comments replaced by a blank */
2803 goto keep_tok_flags
;
2804 } else if (c
== '/') {
2805 p
= parse_line_comment(p
);
2807 goto keep_tok_flags
;
2808 } else if (c
== '=') {
2828 case '@': /* only used in assembler */
2834 if (c
>= 0x80 && c
<= 0xFF) /* utf8 identifiers */
2835 goto parse_ident_fast
;
2836 if (parse_flags
& PARSE_FLAG_ASM_FILE
)
2838 tcc_error("unrecognized character \\x%02x", c
);
2844 #if defined(PARSE_DEBUG)
2845 printf("token = %s\n", get_tok_str(tok
, &tokc
));
2849 /* return next token without macro substitution. Can read input from
2851 static void next_nomacro_spc(void)
2857 TOK_GET(&tok
, ¯o_ptr
, &tokc
);
2858 if (tok
== TOK_LINENUM
) {
2859 file
->line_num
= tokc
.i
;
2868 ST_FUNC
void next_nomacro(void)
2872 } while (tok
< 256 && (isidnum_table
[tok
- CH_EOF
] & IS_SPC
));
2876 static void macro_subst(
2877 TokenString
*tok_str
,
2879 const int *macro_str
,
2883 /* substitute arguments in replacement lists in macro_str by the values in
2884 args (field d) and return allocated string */
2885 static int *macro_arg_subst(Sym
**nested_list
, const int *macro_str
, Sym
*args
)
2897 TOK_GET(&t
, ¯o_str
, &cval
);
2902 TOK_GET(&t
, ¯o_str
, &cval
);
2905 s
= sym_find2(args
, t
);
2908 cstr_ccat(&cstr
, '\"');
2912 TOK_GET(&t
, &st
, &cval
);
2913 if (t
!= TOK_PLCHLDR
2915 && 0 == check_space(t
, &spc
)) {
2916 const char *s
= get_tok_str(t
, &cval
);
2918 if (t
== TOK_PPSTR
&& *s
!= '\'')
2919 add_char(&cstr
, *s
);
2921 cstr_ccat(&cstr
, *s
);
2927 cstr_ccat(&cstr
, '\"');
2928 cstr_ccat(&cstr
, '\0');
2930 printf("\nstringize: <%s>\n", (char *)cstr
.data
);
2933 cval
.str
.size
= cstr
.size
;
2934 cval
.str
.data
= cstr
.data
;
2935 tok_str_add2(&str
, TOK_PPSTR
, &cval
);
2939 expect("macro parameter after '#'");
2941 } else if (t
>= TOK_IDENT
) {
2942 s
= sym_find2(args
, t
);
2946 /* if '##' is present before or after, no arg substitution */
2947 if (*macro_str
== TOK_TWOSHARPS
|| t1
== TOK_TWOSHARPS
) {
2948 /* special case for var arg macros : ## eats the ','
2949 if empty VA_ARGS variable. */
2950 if (t1
== TOK_TWOSHARPS
&& t0
== ',' && gnu_ext
&& s
->type
.t
) {
2952 /* suppress ',' '##' */
2955 /* suppress '##' and add variable */
2962 TOK_GET(&t1
, &st
, &cval
);
2965 tok_str_add2(&str
, t1
, &cval
);
2971 /* NOTE: the stream cannot be read when macro
2972 substituing an argument */
2973 macro_subst(&str
, nested_list
, st
, 0);
2975 if (str
.len
== l0
) /* exanded to empty string */
2976 tok_str_add(&str
, TOK_PLCHLDR
);
2978 tok_str_add(&str
, t
);
2981 tok_str_add2(&str
, t
, &cval
);
2985 tok_str_add(&str
, 0);
2989 static char const ab_month_name
[12][4] =
2991 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2992 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
2995 /* peek or read [ws_str == NULL] next token from function macro call,
2996 walking up macro levels up to the file if necessary */
2997 static int next_argstream(Sym
**nested_list
, int can_read_stream
, TokenString
*ws_str
)
3005 p
= macro_ptr
, t
= *p
;
3007 while (is_space(t
) || TOK_LINEFEED
== t
)
3008 tok_str_add(ws_str
, t
), t
= *++p
;
3010 if (t
== 0 && can_read_stream
) {
3012 /* also, end of scope for nested defined symbol */
3014 while (sa
&& sa
->v
== 0)
3023 while (is_space(ch
) || ch
== '\n' || ch
== '/') {
3026 uint8_t *p
= file
->buf_ptr
;
3029 p
= parse_comment(p
);
3030 file
->buf_ptr
= p
- 1;
3031 } else if (c
== '/') {
3032 p
= parse_line_comment(p
);
3033 file
->buf_ptr
= p
- 1;
3038 tok_str_add(ws_str
, ch
);
3052 /* do macro substitution of current token with macro 's' and add
3053 result to (tok_str,tok_len). 'nested_list' is the list of all
3054 macros we got inside to avoid recursing. Return non zero if no
3055 substitution needs to be done */
3056 static int macro_subst_tok(
3057 TokenString
*tok_str
,
3060 int can_read_stream
)
3062 Sym
*args
, *sa
, *sa1
;
3063 int parlevel
, *mstr
, t
, t1
, spc
;
3070 /* if symbol is a macro, prepare substitution */
3071 /* special macros */
3072 if (tok
== TOK___LINE__
) {
3073 snprintf(buf
, sizeof(buf
), "%d", file
->line_num
);
3077 } else if (tok
== TOK___FILE__
) {
3078 cstrval
= file
->filename
;
3080 } else if (tok
== TOK___DATE__
|| tok
== TOK___TIME__
) {
3085 tm
= localtime(&ti
);
3086 if (tok
== TOK___DATE__
) {
3087 snprintf(buf
, sizeof(buf
), "%s %2d %d",
3088 ab_month_name
[tm
->tm_mon
], tm
->tm_mday
, tm
->tm_year
+ 1900);
3090 snprintf(buf
, sizeof(buf
), "%02d:%02d:%02d",
3091 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
3098 cstr_cat(&cstr
, cstrval
, 0);
3099 cval
.str
.size
= cstr
.size
;
3100 cval
.str
.data
= cstr
.data
;
3101 tok_str_add2(tok_str
, t1
, &cval
);
3104 int saved_parse_flags
= parse_flags
;
3107 if (s
->type
.t
== MACRO_FUNC
) {
3108 /* whitespace between macro name and argument list */
3110 tok_str_new(&ws_str
);
3113 parse_flags
|= PARSE_FLAG_SPACES
| PARSE_FLAG_LINEFEED
3114 | PARSE_FLAG_ACCEPT_STRAYS
;
3116 /* get next token from argument stream */
3117 t
= next_argstream(nested_list
, can_read_stream
, &ws_str
);
3119 /* not a macro substitution after all, restore the
3120 * macro token plus all whitespace we've read.
3121 * whitespace is intentionally not merged to preserve
3123 parse_flags
= saved_parse_flags
;
3124 tok_str_add(tok_str
, tok
);
3125 if (parse_flags
& PARSE_FLAG_SPACES
) {
3127 for (i
= 0; i
< ws_str
.len
; i
++)
3128 tok_str_add(tok_str
, ws_str
.str
[i
]);
3130 tok_str_free(ws_str
.str
);
3133 tok_str_free(ws_str
.str
);
3135 next_nomacro(); /* eat '(' */
3137 /* argument macro */
3140 /* NOTE: empty args are allowed, except if no args */
3143 next_argstream(nested_list
, can_read_stream
, NULL
);
3144 } while (is_space(tok
) || TOK_LINEFEED
== tok
);
3146 /* handle '()' case */
3147 if (!args
&& !sa
&& tok
== ')')
3150 tcc_error("macro '%s' used with too many args",
3151 get_tok_str(s
->v
, 0));
3154 /* NOTE: non zero sa->t indicates VA_ARGS */
3155 while ((parlevel
> 0 ||
3157 (tok
!= ',' || sa
->type
.t
)))) {
3158 if (tok
== TOK_EOF
|| tok
== 0)
3162 else if (tok
== ')')
3164 if (tok
== TOK_LINEFEED
)
3166 if (!check_space(tok
, &spc
))
3167 tok_str_add2(&str
, tok
, &tokc
);
3168 next_argstream(nested_list
, can_read_stream
, NULL
);
3173 tok_str_add(&str
, 0);
3174 sa1
= sym_push2(&args
, sa
->v
& ~SYM_FIELD
, sa
->type
.t
, 0);
3178 /* special case for gcc var args: add an empty
3179 var arg argument if it is omitted */
3180 if (sa
&& sa
->type
.t
&& gnu_ext
)
3188 tcc_error("macro '%s' used with too few args",
3189 get_tok_str(s
->v
, 0));
3192 parse_flags
= saved_parse_flags
;
3194 /* now subst each arg */
3195 mstr
= macro_arg_subst(nested_list
, mstr
, args
);
3200 tok_str_free(sa
->d
);
3206 sym_push2(nested_list
, s
->v
, 0, 0);
3207 parse_flags
= saved_parse_flags
;
3208 macro_subst(tok_str
, nested_list
, mstr
, can_read_stream
| 2);
3210 /* pop nested defined symbol */
3212 *nested_list
= sa1
->prev
;
3220 static int paste_tokens(int t1
, CValue
*v1
, int t2
, CValue
*v2
)
3226 if (t1
!= TOK_PLCHLDR
)
3227 cstr_cat(&cstr
, get_tok_str(t1
, v1
), -1);
3229 if (t2
!= TOK_PLCHLDR
)
3230 cstr_cat(&cstr
, get_tok_str(t2
, v2
), -1);
3231 cstr_ccat(&cstr
, '\0');
3233 tcc_open_bf(tcc_state
, ":paste:", cstr
.size
);
3234 memcpy(file
->buffer
, cstr
.data
, cstr
.size
);
3237 if (0 == *file
->buf_ptr
)
3241 tcc_warning("pasting <%.*s> and <%s> does not give a valid preprocessing token",
3242 n
, cstr
.data
, (char*)cstr
.data
+ n
);
3247 //printf("paste <%s>\n", (char*)cstr.data);
3252 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
3253 return the resulting string (which must be freed). */
3254 static inline int *macro_twosharps(const int *ptr0
)
3258 TokenString macro_str1
;
3259 int start_of_nosubsts
= -1;
3262 /* we search the first '##' */
3263 for (ptr
= ptr0
;;) {
3264 TOK_GET(&t
, &ptr
, &cval
);
3265 if (t
== TOK_TWOSHARPS
)
3271 tok_str_new(¯o_str1
);
3273 //tok_print(" $$$", ptr0);
3274 for (ptr
= ptr0
;;) {
3275 TOK_GET(&t
, &ptr
, &cval
);
3278 if (t
== TOK_TWOSHARPS
)
3280 while (*ptr
== TOK_TWOSHARPS
) {
3282 /* given 'a##b', remove nosubsts preceding 'a' */
3283 if (start_of_nosubsts
>= 0)
3284 macro_str1
.len
= start_of_nosubsts
;
3285 /* given 'a##b', remove nosubsts preceding 'b' */
3286 while ((t1
= *++ptr
) == TOK_NOSUBST
)
3288 if (t1
&& t1
!= TOK_TWOSHARPS
3289 && t1
!= ':') /* 'a##:' don't build a new token */
3291 TOK_GET(&t1
, &ptr
, &cv1
);
3292 if (t
!= TOK_PLCHLDR
|| t1
!= TOK_PLCHLDR
) {
3293 paste_tokens(t
, &cval
, t1
, &cv1
);
3294 t
= tok
, cval
= tokc
;
3298 if (t
== TOK_NOSUBST
) {
3299 if (start_of_nosubsts
< 0)
3300 start_of_nosubsts
= macro_str1
.len
;
3302 start_of_nosubsts
= -1;
3304 tok_str_add2(¯o_str1
, t
, &cval
);
3306 tok_str_add(¯o_str1
, 0);
3307 //tok_print(" ###", macro_str1.str);
3308 return macro_str1
.str
;
3311 /* do macro substitution of macro_str and add result to
3312 (tok_str,tok_len). 'nested_list' is the list of all macros we got
3313 inside to avoid recursing. */
3314 static void macro_subst(
3315 TokenString
*tok_str
,
3317 const int *macro_str
,
3323 int t
, spc
, nosubst
;
3325 int *macro_str1
= NULL
;
3327 /* first scan for '##' operator handling */
3331 /* first scan for '##' operator handling */
3332 if (can_read_stream
& 1) {
3333 macro_str1
= macro_twosharps(ptr
);
3339 TOK_GET(&t
, &ptr
, &cval
);
3343 if (t
>= TOK_IDENT
&& 0 == nosubst
) {
3348 /* if nested substitution, do nothing */
3349 if (sym_find2(*nested_list
, t
)) {
3350 /* and mark it as TOK_NOSUBST, so it doesn't get subst'd again */
3351 tok_str_add2(tok_str
, TOK_NOSUBST
, NULL
);
3357 str
.str
= (int*)ptr
;
3358 begin_macro(&str
, 2);
3361 macro_subst_tok(tok_str
, nested_list
, s
, can_read_stream
);
3363 if (str
.alloc
== 3) {
3364 /* already finished by reading function macro arguments */
3372 spc
= (tok_str
->len
&&
3373 is_space(tok_last(tok_str
->str
,
3374 tok_str
->str
+ tok_str
->len
)));
3378 if (t
== '\\' && !(parse_flags
& PARSE_FLAG_ACCEPT_STRAYS
))
3379 tcc_error("stray '\\' in program");
3382 if (!check_space(t
, &spc
))
3383 tok_str_add2(tok_str
, t
, &cval
);
3385 if (t
== TOK_NOSUBST
)
3390 tok_str_free(macro_str1
);
3394 /* return next token with macro substitution */
3395 ST_FUNC
void next(void)
3398 if (parse_flags
& PARSE_FLAG_SPACES
)
3404 if (tok
== TOK_NOSUBST
|| tok
== TOK_PLCHLDR
) {
3405 /* discard preprocessor markers */
3407 } else if (tok
== 0) {
3408 /* end of macro or unget token string */
3412 } else if (tok
>= TOK_IDENT
&& (parse_flags
& PARSE_FLAG_PREPROCESS
)) {
3414 /* if reading from file, try to substitute macros */
3415 s
= define_find(tok
);
3417 Sym
*nested_list
= NULL
;
3419 macro_subst_tok(&tokstr_buf
, &nested_list
, s
, 1);
3420 tok_str_add(&tokstr_buf
, 0);
3421 begin_macro(&tokstr_buf
, 2);
3425 /* convert preprocessor tokens into C tokens */
3426 if (tok
== TOK_PPNUM
) {
3427 if (parse_flags
& PARSE_FLAG_TOK_NUM
)
3428 parse_number((char *)tokc
.str
.data
);
3429 } else if (tok
== TOK_PPSTR
) {
3430 if (parse_flags
& PARSE_FLAG_TOK_STR
)
3431 parse_string((char *)tokc
.str
.data
, tokc
.str
.size
- 1);
3435 /* push back current token and set current token to 'last_tok'. Only
3436 identifier case handled for labels. */
3437 ST_INLN
void unget_tok(int last_tok
)
3440 TokenString
*str
= tok_str_alloc();
3441 tok_str_add2(str
, tok
, &tokc
);
3442 tok_str_add(str
, 0);
3443 begin_macro(str
, 1);
3447 ST_FUNC
void preprocess_init(TCCState
*s1
)
3449 s1
->include_stack_ptr
= s1
->include_stack
;
3450 /* XXX: move that before to avoid having to initialize
3451 file->ifdef_stack_ptr ? */
3452 s1
->ifdef_stack_ptr
= s1
->ifdef_stack
;
3453 file
->ifdef_stack_ptr
= s1
->ifdef_stack_ptr
;
3456 pvtop
= vtop
= vstack
- 1;
3457 s1
->pack_stack
[0] = 0;
3458 s1
->pack_stack_ptr
= s1
->pack_stack
;
3460 isidnum_table
['$' - CH_EOF
] =
3461 s1
->dollars_in_identifiers
? IS_ID
: 0;
3462 isidnum_table
['.' - CH_EOF
] =
3463 (parse_flags
& PARSE_FLAG_ASM_FILE
) ? IS_ID
: 0;
3466 ST_FUNC
void preprocess_new(void)
3471 /* init isid table */
3472 for(i
= CH_EOF
; i
<128; i
++)
3473 isidnum_table
[i
- CH_EOF
]
3474 = is_space(i
) ? IS_SPC
3479 for(i
= 128; i
<256; i
++)
3480 isidnum_table
[i
- CH_EOF
] = IS_ID
;
3482 /* init allocators */
3483 tal_new(&toksym_alloc
, TOKSYM_TAL_LIMIT
, TOKSYM_TAL_SIZE
);
3484 tal_new(&tokstr_alloc
, TOKSTR_TAL_LIMIT
, TOKSTR_TAL_SIZE
);
3485 tal_new(&cstr_alloc
, CSTR_TAL_LIMIT
, CSTR_TAL_SIZE
);
3487 memset(hash_ident
, 0, TOK_HASH_SIZE
* sizeof(TokenSym
*));
3488 cstr_new(&cstr_buf
);
3489 cstr_realloc(&cstr_buf
, STRING_MAX_SIZE
);
3490 tok_str_new(&tokstr_buf
);
3491 tok_str_realloc(&tokstr_buf
, TOKSTR_MAX_SIZE
);
3493 tok_ident
= TOK_IDENT
;
3502 tok_alloc(p
, r
- p
- 1);
3507 ST_FUNC
void preprocess_delete(void)
3511 /* free -D and compiler defines */
3514 /* cleanup from error/setjmp */
3520 n
= tok_ident
- TOK_IDENT
;
3521 for(i
= 0; i
< n
; i
++)
3522 tal_free(toksym_alloc
, table_ident
[i
]);
3523 tcc_free(table_ident
);
3526 /* free static buffers */
3527 cstr_free(&tokcstr
);
3528 cstr_free(&cstr_buf
);
3529 tok_str_free(tokstr_buf
.str
);
3531 /* free allocators */
3532 tal_delete(toksym_alloc
);
3533 toksym_alloc
= NULL
;
3534 tal_delete(tokstr_alloc
);
3535 tokstr_alloc
= NULL
;
3536 tal_delete(cstr_alloc
);
3540 /* ------------------------------------------------------------------------- */
3541 /* tcc -E [-P[1]] [-dD} support */
3543 static void tok_print(const char *msg
, const int *str
)
3549 fp
= tcc_state
->ppfp
;
3550 if (!fp
|| !tcc_state
->dflag
)
3553 fprintf(fp
, "%s ", msg
);
3555 TOK_GET(&t
, &str
, &cval
);
3558 fprintf(fp
,"%s", get_tok_str(t
, &cval
));
3563 static void pp_line(TCCState
*s1
, BufferedFile
*f
, int level
)
3565 int d
= f
->line_num
- f
->line_ref
;
3568 if (s1
->Pflag
== LINE_MACRO_OUTPUT_FORMAT_NONE
) {
3569 if (level
== 0 && f
->line_ref
&& d
) {
3573 } else if (level
== 0 && f
->line_ref
&& d
< 8) {
3576 fputs("\n", s1
->ppfp
), --d
;
3577 } else if (s1
->Pflag
== LINE_MACRO_OUTPUT_FORMAT_STD
) {
3578 fprintf(s1
->ppfp
, "#line %d \"%s\"\n", f
->line_num
, f
->filename
);
3580 fprintf(s1
->ppfp
, "# %d \"%s\"%s\n", f
->line_num
, f
->filename
,
3581 level
> 0 ? " 1" : level
< 0 ? " 2" : "");
3583 f
->line_ref
= f
->line_num
;
3586 static void define_print(TCCState
*s1
, int v
)
3592 if (NULL
== s
|| NULL
== s
->d
)
3596 fprintf(fp
, "#define %s", get_tok_str(v
, NULL
));
3597 if (s
->type
.t
== MACRO_FUNC
) {
3602 fprintf(fp
,"%s", get_tok_str(a
->v
& ~SYM_FIELD
, NULL
));
3609 tok_print("", s
->d
);
3612 static void pp_debug_defines(TCCState
*s1
)
3623 pp_line(s1
, file
, 0);
3624 file
->line_ref
= ++file
->line_num
;
3628 vs
= get_tok_str(v
, NULL
);
3629 if (t
== TOK_DEFINE
) {
3630 define_print(s1
, v
);
3631 } else if (t
== TOK_UNDEF
) {
3632 fprintf(fp
, "#undef %s\n", vs
);
3633 } else if (t
== TOK_push_macro
) {
3634 fprintf(fp
, "#pragma push_macro(\"%s\")\n", vs
);
3635 } else if (t
== TOK_pop_macro
) {
3636 fprintf(fp
, "#pragma pop_macro(\"%s\")\n", vs
);
3641 static void pp_debug_builtins(TCCState
*s1
)
3644 for (v
= TOK_IDENT
; v
< tok_ident
; ++v
)
3645 define_print(s1
, v
);
3648 static int need_space(int prev_tok
, int tok
, const char *tokstr
)
3650 const char *sp_chars
= "";
3651 if ((prev_tok
>= TOK_IDENT
|| prev_tok
== TOK_PPNUM
) &&
3652 (tok
>= TOK_IDENT
|| tok
== TOK_PPNUM
))
3693 return !!strchr(sp_chars
, tokstr
[0]);
3696 /* Preprocess the current file */
3697 ST_FUNC
int tcc_preprocess(TCCState
*s1
)
3699 BufferedFile
**iptr
;
3700 int token_seen
, spcs
, level
;
3704 preprocess_init(s1
);
3705 ch
= file
->buf_ptr
[0];
3706 tok_flags
= TOK_FLAG_BOL
| TOK_FLAG_BOF
;
3707 parse_flags
= PARSE_FLAG_PREPROCESS
3708 | (parse_flags
& PARSE_FLAG_ASM_FILE
)
3709 | PARSE_FLAG_LINEFEED
3711 | PARSE_FLAG_ACCEPT_STRAYS
3713 define_start
= define_stack
;
3715 /* Credits to Fabrice Bellard's initial revision to demonstrate its
3716 capability to compile and run itself, provided all numbers are
3717 given as decimals. tcc -E -P10 will do. */
3718 if (s1
->Pflag
== 1 + 10)
3719 parse_flags
|= PARSE_FLAG_TOK_NUM
, s1
->Pflag
= 1;
3722 /* for PP benchmarks */
3723 do next(); while (tok
!= TOK_EOF
); return 0;
3726 if (s1
->dflag
& 1) {
3727 pp_debug_builtins(s1
);
3731 token_seen
= TOK_LINEFEED
, spcs
= 0;
3732 pp_line(s1
, file
, 0);
3735 iptr
= s1
->include_stack_ptr
;
3739 level
= s1
->include_stack_ptr
- iptr
;
3742 pp_line(s1
, *iptr
, 0);
3743 pp_line(s1
, file
, level
);
3747 pp_debug_defines(s1
);
3752 if (token_seen
== TOK_LINEFEED
) {
3757 if (tok
== TOK_LINEFEED
) {
3761 pp_line(s1
, file
, 0);
3762 } else if (tok
== TOK_LINEFEED
) {
3766 tokstr
= get_tok_str(tok
, &tokc
);
3767 if (!spcs
&& need_space(token_seen
, tok
, tokstr
))
3770 fputs(" ", s1
->ppfp
), --spcs
;
3771 fputs(tokstr
, s1
->ppfp
);
3775 /* reset define stack, but keep -D and built-ins */
3776 free_defines(define_start
);
3780 /* ------------------------------------------------------------------------- */