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 /* additional informations about token */
28 #define TOK_FLAG_BOL 0x0001 /* beginning of line before */
29 #define TOK_FLAG_BOF 0x0002 /* beginning of file before */
30 #define TOK_FLAG_ENDIF 0x0004 /* a endif was found matching starting #ifdef */
31 #define TOK_FLAG_EOF 0x0008 /* end of file */
33 ST_DATA
int parse_flags
;
34 #define PARSE_FLAG_PREPROCESS 0x0001 /* activate preprocessing */
35 #define PARSE_FLAG_TOK_NUM 0x0002 /* return numbers instead of TOK_PPNUM */
36 #define PARSE_FLAG_LINEFEED 0x0004 /* line feed is returned as a
37 token. line feed is also
39 #define PARSE_FLAG_ASM_COMMENTS 0x0008 /* '#' can be used for line comment */
40 #define PARSE_FLAG_SPACES 0x0010 /* next() returns space tokens (for -E) */
42 ST_DATA
struct BufferedFile
*file
;
45 ST_DATA
const int *macro_ptr
;
46 ST_DATA CString tokcstr
; /* current parsed string, if any */
48 /* display benchmark infos */
49 ST_DATA
int total_lines
;
50 ST_DATA
int total_bytes
;
51 ST_DATA
int tok_ident
;
52 ST_DATA TokenSym
**table_ident
;
54 /* ------------------------------------------------------------------------- */
56 static int *macro_ptr_allocated
;
57 static const int *unget_saved_macro_ptr
;
58 static int unget_saved_buffer
[TOK_MAX_SIZE
+ 1];
59 static int unget_buffer_enabled
;
60 static TokenSym
*hash_ident
[TOK_HASH_SIZE
];
61 static char token_buf
[STRING_MAX_SIZE
+ 1];
62 /* true if isid(c) || isnum(c) */
63 static unsigned char isidnum_table
[256-CH_EOF
];
65 static const char tcc_keywords
[] =
66 #define DEF(id, str) str "\0"
71 /* WARNING: the content of this string encodes token numbers */
72 static const unsigned char tok_two_chars
[] =
73 "<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253"
74 "-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\313..\250##\266";
77 struct macro_level
*prev
;
81 ST_FUNC
void next_nomacro(void);
82 static void next_nomacro_spc(void);
83 static void macro_subst(
87 struct macro_level
**can_read_stream
90 ST_FUNC
void skip(int c
)
93 error("'%c' expected (got \"%s\")", c
, get_tok_str(tok
, NULL
));
97 /* ------------------------------------------------------------------------- */
98 /* CString handling */
99 static void cstr_realloc(CString
*cstr
, int new_size
)
104 size
= cstr
->size_allocated
;
106 size
= 8; /* no need to allocate a too small first string */
107 while (size
< new_size
)
109 data
= tcc_realloc(cstr
->data_allocated
, size
);
111 error("memory full");
112 cstr
->data_allocated
= data
;
113 cstr
->size_allocated
= size
;
118 ST_INLN
void cstr_ccat(CString
*cstr
, int ch
)
121 size
= cstr
->size
+ 1;
122 if (size
> cstr
->size_allocated
)
123 cstr_realloc(cstr
, size
);
124 ((unsigned char *)cstr
->data
)[size
- 1] = ch
;
128 ST_FUNC
void cstr_cat(CString
*cstr
, const char *str
)
140 /* add a wide char */
141 ST_FUNC
void cstr_wccat(CString
*cstr
, int ch
)
144 size
= cstr
->size
+ sizeof(nwchar_t
);
145 if (size
> cstr
->size_allocated
)
146 cstr_realloc(cstr
, size
);
147 *(nwchar_t
*)(((unsigned char *)cstr
->data
) + size
- sizeof(nwchar_t
)) = ch
;
151 ST_FUNC
void cstr_new(CString
*cstr
)
153 memset(cstr
, 0, sizeof(CString
));
156 /* free string and reset it to NULL */
157 ST_FUNC
void cstr_free(CString
*cstr
)
159 tcc_free(cstr
->data_allocated
);
164 ST_FUNC
void add_char(CString
*cstr
, int c
)
166 if (c
== '\'' || c
== '\"' || c
== '\\') {
167 /* XXX: could be more precise if char or string */
168 cstr_ccat(cstr
, '\\');
170 if (c
>= 32 && c
<= 126) {
173 cstr_ccat(cstr
, '\\');
175 cstr_ccat(cstr
, 'n');
177 cstr_ccat(cstr
, '0' + ((c
>> 6) & 7));
178 cstr_ccat(cstr
, '0' + ((c
>> 3) & 7));
179 cstr_ccat(cstr
, '0' + (c
& 7));
184 /* ------------------------------------------------------------------------- */
185 /* allocate a new token */
186 static TokenSym
*tok_alloc_new(TokenSym
**pts
, const char *str
, int len
)
188 TokenSym
*ts
, **ptable
;
191 if (tok_ident
>= SYM_FIRST_ANOM
)
192 error("memory full");
194 /* expand token table if needed */
195 i
= tok_ident
- TOK_IDENT
;
196 if ((i
% TOK_ALLOC_INCR
) == 0) {
197 ptable
= tcc_realloc(table_ident
, (i
+ TOK_ALLOC_INCR
) * sizeof(TokenSym
*));
199 error("memory full");
200 table_ident
= ptable
;
203 ts
= tcc_malloc(sizeof(TokenSym
) + len
);
205 ts
->tok
= tok_ident
++;
206 ts
->sym_define
= NULL
;
207 ts
->sym_label
= NULL
;
208 ts
->sym_struct
= NULL
;
209 ts
->sym_identifier
= NULL
;
211 ts
->hash_next
= NULL
;
212 memcpy(ts
->str
, str
, len
);
218 #define TOK_HASH_INIT 1
219 #define TOK_HASH_FUNC(h, c) ((h) * 263 + (c))
221 /* find a token and add it if not found */
222 ST_FUNC TokenSym
*tok_alloc(const char *str
, int len
)
230 h
= TOK_HASH_FUNC(h
, ((unsigned char *)str
)[i
]);
231 h
&= (TOK_HASH_SIZE
- 1);
233 pts
= &hash_ident
[h
];
238 if (ts
->len
== len
&& !memcmp(ts
->str
, str
, len
))
240 pts
= &(ts
->hash_next
);
242 return tok_alloc_new(pts
, str
, len
);
245 /* XXX: buffer overflow */
246 /* XXX: float tokens */
247 ST_FUNC
char *get_tok_str(int v
, CValue
*cv
)
249 static char buf
[STRING_MAX_SIZE
+ 1];
250 static CString cstr_buf
;
255 /* NOTE: to go faster, we give a fixed buffer for small strings */
256 cstr_reset(&cstr_buf
);
258 cstr_buf
.size_allocated
= sizeof(buf
);
264 /* XXX: not quite exact, but only useful for testing */
265 sprintf(p
, "%u", cv
->ui
);
269 /* XXX: not quite exact, but only useful for testing */
271 sprintf(p
, "%u", (unsigned)cv
->ull
);
273 sprintf(p
, "%Lu", cv
->ull
);
277 cstr_ccat(&cstr_buf
, 'L');
279 cstr_ccat(&cstr_buf
, '\'');
280 add_char(&cstr_buf
, cv
->i
);
281 cstr_ccat(&cstr_buf
, '\'');
282 cstr_ccat(&cstr_buf
, '\0');
286 len
= cstr
->size
- 1;
288 add_char(&cstr_buf
, ((unsigned char *)cstr
->data
)[i
]);
289 cstr_ccat(&cstr_buf
, '\0');
292 cstr_ccat(&cstr_buf
, 'L');
295 cstr_ccat(&cstr_buf
, '\"');
297 len
= cstr
->size
- 1;
299 add_char(&cstr_buf
, ((unsigned char *)cstr
->data
)[i
]);
301 len
= (cstr
->size
/ sizeof(nwchar_t
)) - 1;
303 add_char(&cstr_buf
, ((nwchar_t
*)cstr
->data
)[i
]);
305 cstr_ccat(&cstr_buf
, '\"');
306 cstr_ccat(&cstr_buf
, '\0');
315 return strcpy(p
, "...");
317 return strcpy(p
, "<<=");
319 return strcpy(p
, ">>=");
322 /* search in two bytes table */
323 const unsigned char *q
= tok_two_chars
;
336 } else if (v
< tok_ident
) {
337 return table_ident
[v
- TOK_IDENT
]->str
;
338 } else if (v
>= SYM_FIRST_ANOM
) {
339 /* special name for anonymous symbol */
340 sprintf(p
, "L.%u", v
- SYM_FIRST_ANOM
);
342 /* should never happen */
347 return cstr_buf
.data
;
350 /* fill input buffer and peek next char */
351 static int tcc_peekc_slow(BufferedFile
*bf
)
354 /* only tries to read if really end of buffer */
355 if (bf
->buf_ptr
>= bf
->buf_end
) {
357 #if defined(PARSE_DEBUG)
362 len
= read(bf
->fd
, bf
->buffer
, len
);
369 bf
->buf_ptr
= bf
->buffer
;
370 bf
->buf_end
= bf
->buffer
+ len
;
371 *bf
->buf_end
= CH_EOB
;
373 if (bf
->buf_ptr
< bf
->buf_end
) {
374 return bf
->buf_ptr
[0];
376 bf
->buf_ptr
= bf
->buf_end
;
381 /* return the current character, handling end of block if necessary
383 ST_FUNC
int handle_eob(void)
385 return tcc_peekc_slow(file
);
388 /* read next char from current input file and handle end of input buffer */
389 ST_INLN
void inp(void)
391 ch
= *(++(file
->buf_ptr
));
392 /* end of buffer/file handling */
397 /* handle '\[\r]\n' */
398 static int handle_stray_noerror(void)
405 } else if (ch
== '\r') {
419 static void handle_stray(void)
421 if (handle_stray_noerror())
422 error("stray '\\' in program");
425 /* skip the stray and handle the \\n case. Output an error if
426 incorrect char after the stray */
427 static int handle_stray1(uint8_t *p
)
431 if (p
>= file
->buf_end
) {
448 /* handle just the EOB case, but not stray */
449 #define PEEKC_EOB(c, p)\
460 /* handle the complicated stray case */
466 c = handle_stray1(p);\
471 /* input with '\[\r]\n' handling. Note that this function cannot
472 handle other characters after '\', so you cannot call it inside
473 strings or comments */
474 ST_FUNC
void minp(void)
482 /* single line C++ comments */
483 static uint8_t *parse_line_comment(uint8_t *p
)
491 if (c
== '\n' || c
== CH_EOF
) {
493 } else if (c
== '\\') {
502 } else if (c
== '\r') {
520 ST_FUNC
uint8_t *parse_comment(uint8_t *p
)
529 if (c
== '\n' || c
== '*' || c
== '\\')
533 if (c
== '\n' || c
== '*' || c
== '\\')
537 /* now we can handle all the cases */
541 } else if (c
== '*') {
547 } else if (c
== '/') {
549 } else if (c
== '\\') {
554 /* skip '\[\r]\n', otherwise just skip the stray */
560 } else if (c
== '\r') {
577 /* stray, eob or eof */
582 error("unexpected end of file in comment");
583 } else if (c
== '\\') {
595 static inline void skip_spaces(void)
601 static inline int check_space(int t
, int *spc
)
612 /* parse a string without interpreting escapes */
613 static uint8_t *parse_pp_string(uint8_t *p
,
614 int sep
, CString
*str
)
622 } else if (c
== '\\') {
628 /* XXX: indicate line number of start of string */
629 error("missing terminating %c character", sep
);
630 } else if (c
== '\\') {
631 /* escape : just skip \[\r]\n */
636 } else if (c
== '\r') {
639 expect("'\n' after '\r'");
642 } else if (c
== CH_EOF
) {
643 goto unterminated_string
;
646 cstr_ccat(str
, '\\');
652 } else if (c
== '\n') {
655 } else if (c
== '\r') {
659 cstr_ccat(str
, '\r');
675 /* skip block of text until #else, #elif or #endif. skip also pairs of
677 static void preprocess_skip(void)
679 int a
, start_of_line
, c
, in_warn_or_error
;
686 in_warn_or_error
= 0;
707 } else if (c
== '\\') {
708 ch
= file
->buf_ptr
[0];
709 handle_stray_noerror();
716 if (in_warn_or_error
)
718 p
= parse_pp_string(p
, c
, NULL
);
722 if (in_warn_or_error
)
729 p
= parse_comment(p
);
730 } else if (ch
== '/') {
731 p
= parse_line_comment(p
);
741 (tok
== TOK_ELSE
|| tok
== TOK_ELIF
|| tok
== TOK_ENDIF
))
743 if (tok
== TOK_IF
|| tok
== TOK_IFDEF
|| tok
== TOK_IFNDEF
)
745 else if (tok
== TOK_ENDIF
)
747 else if( tok
== TOK_ERROR
|| tok
== TOK_WARNING
)
748 in_warn_or_error
= 1;
762 /* ParseState handling */
764 /* XXX: currently, no include file info is stored. Thus, we cannot display
765 accurate messages if the function or data definition spans multiple
768 /* save current parse state in 's' */
769 ST_FUNC
void save_parse_state(ParseState
*s
)
771 s
->line_num
= file
->line_num
;
772 s
->macro_ptr
= macro_ptr
;
777 /* restore parse state from 's' */
778 ST_FUNC
void restore_parse_state(ParseState
*s
)
780 file
->line_num
= s
->line_num
;
781 macro_ptr
= s
->macro_ptr
;
786 /* return the number of additional 'ints' necessary to store the
788 static inline int tok_ext_size(int t
)
802 error("unsupported token");
809 return LDOUBLE_SIZE
/ 4;
815 /* token string handling */
817 ST_INLN
void tok_str_new(TokenString
*s
)
821 s
->allocated_len
= 0;
822 s
->last_line_num
= -1;
825 ST_FUNC
void tok_str_free(int *str
)
830 static int *tok_str_realloc(TokenString
*s
)
834 if (s
->allocated_len
== 0) {
837 len
= s
->allocated_len
* 2;
839 str
= tcc_realloc(s
->str
, len
* sizeof(int));
841 error("memory full");
842 s
->allocated_len
= len
;
847 ST_FUNC
void tok_str_add(TokenString
*s
, int t
)
853 if (len
>= s
->allocated_len
)
854 str
= tok_str_realloc(s
);
859 static void tok_str_add2(TokenString
*s
, int t
, CValue
*cv
)
866 /* allocate space for worst case */
867 if (len
+ TOK_MAX_SIZE
> s
->allocated_len
)
868 str
= tok_str_realloc(s
);
877 str
[len
++] = cv
->tab
[0];
886 nb_words
= (sizeof(CString
) + cv
->cstr
->size
+ 3) >> 2;
887 while ((len
+ nb_words
) > s
->allocated_len
)
888 str
= tok_str_realloc(s
);
889 cstr
= (CString
*)(str
+ len
);
891 cstr
->size
= cv
->cstr
->size
;
892 cstr
->data_allocated
= NULL
;
893 cstr
->size_allocated
= cstr
->size
;
894 memcpy((char *)cstr
+ sizeof(CString
),
895 cv
->cstr
->data
, cstr
->size
);
902 #if LDOUBLE_SIZE == 8
905 str
[len
++] = cv
->tab
[0];
906 str
[len
++] = cv
->tab
[1];
908 #if LDOUBLE_SIZE == 12
910 str
[len
++] = cv
->tab
[0];
911 str
[len
++] = cv
->tab
[1];
912 str
[len
++] = cv
->tab
[2];
913 #elif LDOUBLE_SIZE == 16
915 str
[len
++] = cv
->tab
[0];
916 str
[len
++] = cv
->tab
[1];
917 str
[len
++] = cv
->tab
[2];
918 str
[len
++] = cv
->tab
[3];
919 #elif LDOUBLE_SIZE != 8
920 #error add long double size support
929 /* add the current parse token in token string 's' */
930 ST_FUNC
void tok_str_add_tok(TokenString
*s
)
934 /* save line number info */
935 if (file
->line_num
!= s
->last_line_num
) {
936 s
->last_line_num
= file
->line_num
;
937 cval
.i
= s
->last_line_num
;
938 tok_str_add2(s
, TOK_LINENUM
, &cval
);
940 tok_str_add2(s
, tok
, &tokc
);
943 /* get a token from an integer array and increment pointer
944 accordingly. we code it as a macro to avoid pointer aliasing. */
945 static inline void TOK_GET(int *t
, const int **pp
, CValue
*cv
)
963 cv
->cstr
= (CString
*)p
;
964 cv
->cstr
->data
= (char *)p
+ sizeof(CString
);
965 p
+= (sizeof(CString
) + cv
->cstr
->size
+ 3) >> 2;
973 #if LDOUBLE_SIZE == 16
975 #elif LDOUBLE_SIZE == 12
977 #elif LDOUBLE_SIZE == 8
980 # error add long double size support
993 static int macro_is_equal(const int *a
, const int *b
)
995 char buf
[STRING_MAX_SIZE
+ 1];
999 TOK_GET(&t
, &a
, &cv
);
1000 pstrcpy(buf
, sizeof buf
, get_tok_str(t
, &cv
));
1001 TOK_GET(&t
, &b
, &cv
);
1002 if (strcmp(buf
, get_tok_str(t
, &cv
)))
1008 /* defines handling */
1009 ST_INLN
void define_push(int v
, int macro_type
, int *str
, Sym
*first_arg
)
1014 if (s
&& !macro_is_equal(s
->d
, str
))
1015 warning("%s redefined", get_tok_str(v
, NULL
));
1017 s
= sym_push2(&define_stack
, v
, macro_type
, 0);
1019 s
->next
= first_arg
;
1020 table_ident
[v
- TOK_IDENT
]->sym_define
= s
;
1023 /* undefined a define symbol. Its name is just set to zero */
1024 ST_FUNC
void define_undef(Sym
*s
)
1028 if (v
>= TOK_IDENT
&& v
< tok_ident
)
1029 table_ident
[v
- TOK_IDENT
]->sym_define
= NULL
;
1033 ST_INLN Sym
*define_find(int v
)
1036 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
1038 return table_ident
[v
]->sym_define
;
1041 /* free define stack until top reaches 'b' */
1042 ST_FUNC
void free_defines(Sym
*b
)
1050 /* do not free args or predefined defines */
1052 tok_str_free(top
->d
);
1054 if (v
>= TOK_IDENT
&& v
< tok_ident
)
1055 table_ident
[v
- TOK_IDENT
]->sym_define
= NULL
;
1063 ST_FUNC Sym
*label_find(int v
)
1066 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
1068 return table_ident
[v
]->sym_label
;
1071 ST_FUNC Sym
*label_push(Sym
**ptop
, int v
, int flags
)
1074 s
= sym_push2(ptop
, v
, 0, 0);
1076 ps
= &table_ident
[v
- TOK_IDENT
]->sym_label
;
1077 if (ptop
== &global_label_stack
) {
1078 /* modify the top most local identifier, so that
1079 sym_identifier will point to 's' when popped */
1081 ps
= &(*ps
)->prev_tok
;
1088 /* pop labels until element last is reached. Look if any labels are
1089 undefined. Define symbols if '&&label' was used. */
1090 ST_FUNC
void label_pop(Sym
**ptop
, Sym
*slast
)
1093 for(s
= *ptop
; s
!= slast
; s
= s1
) {
1095 if (s
->r
== LABEL_DECLARED
) {
1096 warning("label '%s' declared but not used", get_tok_str(s
->v
, NULL
));
1097 } else if (s
->r
== LABEL_FORWARD
) {
1098 error("label '%s' used but not defined",
1099 get_tok_str(s
->v
, NULL
));
1102 /* define corresponding symbol. A size of
1104 put_extern_sym(s
, cur_text_section
, s
->jnext
, 1);
1108 table_ident
[s
->v
- TOK_IDENT
]->sym_label
= s
->prev_tok
;
1114 /* eval an expression for #if/#elif */
1115 static int expr_preprocess(void)
1121 while (tok
!= TOK_LINEFEED
&& tok
!= TOK_EOF
) {
1122 next(); /* do macro subst */
1123 if (tok
== TOK_DEFINED
) {
1128 c
= define_find(tok
) != 0;
1133 } else if (tok
>= TOK_IDENT
) {
1134 /* if undefined macro */
1138 tok_str_add_tok(&str
);
1140 tok_str_add(&str
, -1); /* simulate end of file */
1141 tok_str_add(&str
, 0);
1142 /* now evaluate C constant expression */
1143 macro_ptr
= str
.str
;
1147 tok_str_free(str
.str
);
1151 #if defined(PARSE_DEBUG) || defined(PP_DEBUG)
1152 static void tok_print(int *str
)
1159 TOK_GET(&t
, &str
, &cval
);
1162 printf("%s", get_tok_str(t
, &cval
));
1168 /* parse after #define */
1169 ST_FUNC
void parse_define(void)
1171 Sym
*s
, *first
, **ps
;
1172 int v
, t
, varg
, is_vaargs
, spc
;
1177 error("invalid macro name '%s'", get_tok_str(tok
, &tokc
));
1178 /* XXX: should check if same macro (ANSI) */
1181 /* '(' must be just after macro definition for MACRO_FUNC */
1186 while (tok
!= ')') {
1190 if (varg
== TOK_DOTS
) {
1191 varg
= TOK___VA_ARGS__
;
1193 } else if (tok
== TOK_DOTS
&& gnu_ext
) {
1197 if (varg
< TOK_IDENT
)
1198 error("badly punctuated parameter list");
1199 s
= sym_push2(&define_stack
, varg
| SYM_FIELD
, is_vaargs
, 0);
1212 /* EOF testing necessary for '-D' handling */
1213 while (tok
!= TOK_LINEFEED
&& tok
!= TOK_EOF
) {
1214 /* remove spaces around ## and after '#' */
1215 if (TOK_TWOSHARPS
== tok
) {
1219 } else if ('#' == tok
) {
1221 } else if (check_space(tok
, &spc
)) {
1224 tok_str_add2(&str
, tok
, &tokc
);
1229 --str
.len
; /* remove trailing space */
1230 tok_str_add(&str
, 0);
1232 printf("define %s %d: ", get_tok_str(v
, NULL
), t
);
1235 define_push(v
, t
, str
.str
, first
);
1238 static inline int hash_cached_include(int type
, const char *filename
)
1240 const unsigned char *s
;
1244 h
= TOK_HASH_FUNC(h
, type
);
1247 h
= TOK_HASH_FUNC(h
, *s
);
1250 h
&= (CACHED_INCLUDES_HASH_SIZE
- 1);
1254 /* XXX: use a token or a hash table to accelerate matching ? */
1255 static CachedInclude
*search_cached_include(TCCState
*s1
,
1256 int type
, const char *filename
)
1260 h
= hash_cached_include(type
, filename
);
1261 i
= s1
->cached_includes_hash
[h
];
1265 e
= s1
->cached_includes
[i
- 1];
1266 if (e
->type
== type
&& !PATHCMP(e
->filename
, filename
))
1273 static inline void add_cached_include(TCCState
*s1
, int type
,
1274 const char *filename
, int ifndef_macro
)
1279 if (search_cached_include(s1
, type
, filename
))
1282 printf("adding cached '%s' %s\n", filename
, get_tok_str(ifndef_macro
, NULL
));
1284 e
= tcc_malloc(sizeof(CachedInclude
) + strlen(filename
));
1288 strcpy(e
->filename
, filename
);
1289 e
->ifndef_macro
= ifndef_macro
;
1290 dynarray_add((void ***)&s1
->cached_includes
, &s1
->nb_cached_includes
, e
);
1291 /* add in hash table */
1292 h
= hash_cached_include(type
, filename
);
1293 e
->hash_next
= s1
->cached_includes_hash
[h
];
1294 s1
->cached_includes_hash
[h
] = s1
->nb_cached_includes
;
1297 static void pragma_parse(TCCState
*s1
)
1302 if (tok
== TOK_pack
) {
1305 #pragma pack(1) // set
1306 #pragma pack() // reset to default
1307 #pragma pack(push,1) // push & set
1308 #pragma pack(pop) // restore previous
1312 if (tok
== TOK_ASM_pop
) {
1314 if (s1
->pack_stack_ptr
<= s1
->pack_stack
) {
1316 error("out of pack stack");
1318 s1
->pack_stack_ptr
--;
1322 if (tok
== TOK_ASM_push
) {
1324 if (s1
->pack_stack_ptr
>= s1
->pack_stack
+ PACK_STACK_SIZE
- 1)
1326 s1
->pack_stack_ptr
++;
1329 if (tok
!= TOK_CINT
) {
1331 error("invalid pack pragma");
1334 if (val
< 1 || val
> 16 || (val
& (val
- 1)) != 0)
1338 *s1
->pack_stack_ptr
= val
;
1344 /* is_bof is true if first non space token at beginning of file */
1345 ST_FUNC
void preprocess(int is_bof
)
1347 TCCState
*s1
= tcc_state
;
1348 int i
, c
, n
, saved_parse_flags
;
1352 saved_parse_flags
= parse_flags
;
1353 parse_flags
= PARSE_FLAG_PREPROCESS
| PARSE_FLAG_TOK_NUM
|
1354 PARSE_FLAG_LINEFEED
;
1364 s
= define_find(tok
);
1365 /* undefine symbol by putting an invalid name */
1370 case TOK_INCLUDE_NEXT
:
1371 ch
= file
->buf_ptr
[0];
1372 /* XXX: incorrect if comments : use next_nomacro with a special mode */
1377 } else if (ch
== '\"') {
1382 while (ch
!= c
&& ch
!= '\n' && ch
!= CH_EOF
) {
1383 if ((q
- buf
) < sizeof(buf
) - 1)
1386 if (handle_stray_noerror() == 0)
1394 /* eat all spaces and comments after include */
1395 /* XXX: slightly incorrect */
1396 while (ch1
!= '\n' && ch1
!= CH_EOF
)
1400 /* computed #include : either we have only strings or
1401 we have anything enclosed in '<>' */
1404 if (tok
== TOK_STR
) {
1405 while (tok
!= TOK_LINEFEED
) {
1406 if (tok
!= TOK_STR
) {
1408 error("'#include' expects \"FILENAME\" or <FILENAME>");
1410 pstrcat(buf
, sizeof(buf
), (char *)tokc
.cstr
->data
);
1416 while (tok
!= TOK_LINEFEED
) {
1417 pstrcat(buf
, sizeof(buf
), get_tok_str(tok
, &tokc
));
1421 /* check syntax and remove '<>' */
1422 if (len
< 2 || buf
[0] != '<' || buf
[len
- 1] != '>')
1423 goto include_syntax
;
1424 memmove(buf
, buf
+ 1, len
- 2);
1425 buf
[len
- 2] = '\0';
1430 if (s1
->include_stack_ptr
>= s1
->include_stack
+ INCLUDE_STACK_SIZE
)
1431 error("#include recursion too deep");
1433 n
= s1
->nb_include_paths
+ s1
->nb_sysinclude_paths
;
1434 for (i
= -2; i
< n
; ++i
) {
1435 char buf1
[sizeof file
->filename
];
1442 /* check absolute include path */
1443 if (!IS_ABSPATH(buf
))
1447 } else if (i
== -1) {
1448 /* search in current dir if "header.h" */
1451 size
= tcc_basename(file
->filename
) - file
->filename
;
1452 memcpy(buf1
, file
->filename
, size
);
1456 /* search in all the include paths */
1457 if (i
< s1
->nb_include_paths
)
1458 path
= s1
->include_paths
[i
];
1460 path
= s1
->sysinclude_paths
[i
- s1
->nb_include_paths
];
1461 pstrcpy(buf1
, sizeof(buf1
), path
);
1462 pstrcat(buf1
, sizeof(buf1
), "/");
1465 pstrcat(buf1
, sizeof(buf1
), buf
);
1467 e
= search_cached_include(s1
, c
, buf1
);
1468 if (e
&& define_find(e
->ifndef_macro
)) {
1469 /* no need to parse the include because the 'ifndef macro'
1472 printf("%s: skipping %s\n", file
->filename
, buf
);
1476 f
= tcc_open(s1
, buf1
);
1481 if (tok
== TOK_INCLUDE_NEXT
) {
1492 printf("%s: including %s\n", file
->filename
, buf1
);
1495 /* XXX: fix current line init */
1496 /* push current file in stack */
1497 *s1
->include_stack_ptr
++ = file
;
1499 pstrcpy(f
->inc_filename
, sizeof(f
->inc_filename
), buf1
);
1501 /* add include file debug info */
1503 put_stabs(file
->filename
, N_BINCL
, 0, 0, 0);
1505 tok_flags
|= TOK_FLAG_BOF
| TOK_FLAG_BOL
;
1506 ch
= file
->buf_ptr
[0];
1509 error("include file '%s' not found", buf
);
1516 c
= expr_preprocess();
1522 if (tok
< TOK_IDENT
)
1523 error("invalid argument for '#if%sdef'", c
? "n" : "");
1527 printf("#ifndef %s\n", get_tok_str(tok
, NULL
));
1529 file
->ifndef_macro
= tok
;
1532 c
= (define_find(tok
) != 0) ^ c
;
1534 if (s1
->ifdef_stack_ptr
>= s1
->ifdef_stack
+ IFDEF_STACK_SIZE
)
1535 error("memory full");
1536 *s1
->ifdef_stack_ptr
++ = c
;
1539 if (s1
->ifdef_stack_ptr
== s1
->ifdef_stack
)
1540 error("#else without matching #if");
1541 if (s1
->ifdef_stack_ptr
[-1] & 2)
1542 error("#else after #else");
1543 c
= (s1
->ifdef_stack_ptr
[-1] ^= 3);
1546 if (s1
->ifdef_stack_ptr
== s1
->ifdef_stack
)
1547 error("#elif without matching #if");
1548 c
= s1
->ifdef_stack_ptr
[-1];
1550 error("#elif after #else");
1551 /* last #if/#elif expression was true: we skip */
1554 c
= expr_preprocess();
1555 s1
->ifdef_stack_ptr
[-1] = c
;
1557 if (s1
->ifdef_stack_ptr
== file
->ifdef_stack_ptr
+ 1)
1558 file
->ifndef_macro
= 0;
1568 if (s1
->ifdef_stack_ptr
<= file
->ifdef_stack_ptr
)
1569 error("#endif without matching #if");
1570 s1
->ifdef_stack_ptr
--;
1571 /* '#ifndef macro' was at the start of file. Now we check if
1572 an '#endif' is exactly at the end of file */
1573 if (file
->ifndef_macro
&&
1574 s1
->ifdef_stack_ptr
== file
->ifdef_stack_ptr
) {
1575 file
->ifndef_macro_saved
= file
->ifndef_macro
;
1576 /* need to set to zero to avoid false matches if another
1577 #ifndef at middle of file */
1578 file
->ifndef_macro
= 0;
1579 while (tok
!= TOK_LINEFEED
)
1581 tok_flags
|= TOK_FLAG_ENDIF
;
1587 if (tok
!= TOK_CINT
)
1589 file
->line_num
= tokc
.i
- 1; /* the line number will be incremented after */
1591 if (tok
!= TOK_LINEFEED
) {
1594 pstrcpy(file
->filename
, sizeof(file
->filename
),
1595 (char *)tokc
.cstr
->data
);
1601 ch
= file
->buf_ptr
[0];
1604 while (ch
!= '\n' && ch
!= CH_EOF
) {
1605 if ((q
- buf
) < sizeof(buf
) - 1)
1608 if (handle_stray_noerror() == 0)
1615 error("#error %s", buf
);
1617 warning("#warning %s", buf
);
1623 if (tok
== TOK_LINEFEED
|| tok
== '!' || tok
== TOK_PPNUM
) {
1624 /* '!' is ignored to allow C scripts. numbers are ignored
1625 to emulate cpp behaviour */
1627 if (!(saved_parse_flags
& PARSE_FLAG_ASM_COMMENTS
))
1628 warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok
, &tokc
));
1632 /* ignore other preprocess commands or #! for C scripts */
1633 while (tok
!= TOK_LINEFEED
)
1636 parse_flags
= saved_parse_flags
;
1639 /* evaluate escape codes in a string. */
1640 static void parse_escape_string(CString
*outstr
, const uint8_t *buf
, int is_long
)
1655 case '0': case '1': case '2': case '3':
1656 case '4': case '5': case '6': case '7':
1657 /* at most three octal digits */
1662 n
= n
* 8 + c
- '0';
1666 n
= n
* 8 + c
- '0';
1671 goto add_char_nonext
;
1679 if (c
>= 'a' && c
<= 'f')
1681 else if (c
>= 'A' && c
<= 'F')
1691 goto add_char_nonext
;
1715 goto invalid_escape
;
1725 if (c
>= '!' && c
<= '~')
1726 warning("unknown escape sequence: \'\\%c\'", c
);
1728 warning("unknown escape sequence: \'\\x%x\'", c
);
1735 cstr_ccat(outstr
, c
);
1737 cstr_wccat(outstr
, c
);
1739 /* add a trailing '\0' */
1741 cstr_ccat(outstr
, '\0');
1743 cstr_wccat(outstr
, '\0');
1746 /* we use 64 bit numbers */
1749 /* bn = (bn << shift) | or_val */
1750 static void bn_lshift(unsigned int *bn
, int shift
, int or_val
)
1754 for(i
=0;i
<BN_SIZE
;i
++) {
1756 bn
[i
] = (v
<< shift
) | or_val
;
1757 or_val
= v
>> (32 - shift
);
1761 static void bn_zero(unsigned int *bn
)
1764 for(i
=0;i
<BN_SIZE
;i
++) {
1769 /* parse number in null terminated string 'p' and return it in the
1771 static void parse_number(const char *p
)
1773 int b
, t
, shift
, frac_bits
, s
, exp_val
, ch
;
1775 unsigned int bn
[BN_SIZE
];
1786 goto float_frac_parse
;
1787 } else if (t
== '0') {
1788 if (ch
== 'x' || ch
== 'X') {
1792 } else if (tcc_ext
&& (ch
== 'b' || ch
== 'B')) {
1798 /* parse all digits. cannot check octal numbers at this stage
1799 because of floating point constants */
1801 if (ch
>= 'a' && ch
<= 'f')
1803 else if (ch
>= 'A' && ch
<= 'F')
1811 if (q
>= token_buf
+ STRING_MAX_SIZE
) {
1813 error("number too long");
1819 ((ch
== 'e' || ch
== 'E') && b
== 10) ||
1820 ((ch
== 'p' || ch
== 'P') && (b
== 16 || b
== 2))) {
1822 /* NOTE: strtox should support that for hexa numbers, but
1823 non ISOC99 libcs do not support it, so we prefer to do
1825 /* hexadecimal or binary floats */
1826 /* XXX: handle overflows */
1838 } else if (t
>= 'a') {
1840 } else if (t
>= 'A') {
1845 bn_lshift(bn
, shift
, t
);
1852 if (t
>= 'a' && t
<= 'f') {
1854 } else if (t
>= 'A' && t
<= 'F') {
1856 } else if (t
>= '0' && t
<= '9') {
1862 error("invalid digit");
1863 bn_lshift(bn
, shift
, t
);
1868 if (ch
!= 'p' && ch
!= 'P')
1875 } else if (ch
== '-') {
1879 if (ch
< '0' || ch
> '9')
1880 expect("exponent digits");
1881 while (ch
>= '0' && ch
<= '9') {
1882 exp_val
= exp_val
* 10 + ch
- '0';
1885 exp_val
= exp_val
* s
;
1887 /* now we can generate the number */
1888 /* XXX: should patch directly float number */
1889 d
= (double)bn
[1] * 4294967296.0 + (double)bn
[0];
1890 d
= ldexp(d
, exp_val
- frac_bits
);
1895 /* float : should handle overflow */
1897 } else if (t
== 'L') {
1899 #ifdef TCC_TARGET_PE
1904 /* XXX: not large enough */
1905 tokc
.ld
= (long double)d
;
1912 /* decimal floats */
1914 if (q
>= token_buf
+ STRING_MAX_SIZE
)
1919 while (ch
>= '0' && ch
<= '9') {
1920 if (q
>= token_buf
+ STRING_MAX_SIZE
)
1926 if (ch
== 'e' || ch
== 'E') {
1927 if (q
>= token_buf
+ STRING_MAX_SIZE
)
1931 if (ch
== '-' || ch
== '+') {
1932 if (q
>= token_buf
+ STRING_MAX_SIZE
)
1937 if (ch
< '0' || ch
> '9')
1938 expect("exponent digits");
1939 while (ch
>= '0' && ch
<= '9') {
1940 if (q
>= token_buf
+ STRING_MAX_SIZE
)
1952 tokc
.f
= strtof(token_buf
, NULL
);
1953 } else if (t
== 'L') {
1955 #ifdef TCC_TARGET_PE
1957 tokc
.d
= strtod(token_buf
, NULL
);
1960 tokc
.ld
= strtold(token_buf
, NULL
);
1964 tokc
.d
= strtod(token_buf
, NULL
);
1968 unsigned long long n
, n1
;
1971 /* integer number */
1974 if (b
== 10 && *q
== '0') {
1981 /* no need for checks except for base 10 / 8 errors */
1984 } else if (t
>= 'a') {
1986 } else if (t
>= 'A') {
1991 error("invalid digit");
1995 /* detect overflow */
1996 /* XXX: this test is not reliable */
1998 error("integer constant overflow");
2001 /* XXX: not exactly ANSI compliant */
2002 if ((n
& 0xffffffff00000000LL
) != 0) {
2007 } else if (n
> 0x7fffffff) {
2018 error("three 'l's in integer constant");
2021 if (tok
== TOK_CINT
)
2023 else if (tok
== TOK_CUINT
)
2027 } else if (t
== 'U') {
2029 error("two 'u's in integer constant");
2031 if (tok
== TOK_CINT
)
2033 else if (tok
== TOK_CLLONG
)
2040 if (tok
== TOK_CINT
|| tok
== TOK_CUINT
)
2046 error("invalid number\n");
2050 #define PARSE2(c1, tok1, c2, tok2) \
2061 /* return next token without macro substitution */
2062 static inline void next_nomacro1(void)
2077 goto keep_tok_flags
;
2084 /* first look if it is in fact an end of buffer */
2085 if (p
>= file
->buf_end
) {
2089 if (p
>= file
->buf_end
)
2102 TCCState
*s1
= tcc_state
;
2103 if ((parse_flags
& PARSE_FLAG_LINEFEED
)
2104 && !(tok_flags
& TOK_FLAG_EOF
)) {
2105 tok_flags
|= TOK_FLAG_EOF
;
2107 goto keep_tok_flags
;
2108 } else if (!(parse_flags
& PARSE_FLAG_PREPROCESS
)) {
2110 } else if (s1
->ifdef_stack_ptr
!= file
->ifdef_stack_ptr
) {
2111 error("missing #endif");
2112 } else if (s1
->include_stack_ptr
== s1
->include_stack
) {
2113 /* no include left : end of file. */
2116 tok_flags
&= ~TOK_FLAG_EOF
;
2117 /* pop include file */
2119 /* test if previous '#endif' was after a #ifdef at
2121 if (tok_flags
& TOK_FLAG_ENDIF
) {
2123 printf("#endif %s\n", get_tok_str(file
->ifndef_macro_saved
, NULL
));
2125 add_cached_include(s1
, file
->inc_type
, file
->inc_filename
,
2126 file
->ifndef_macro_saved
);
2129 /* add end of include file debug info */
2130 if (tcc_state
->do_debug
) {
2131 put_stabd(N_EINCL
, 0, 0);
2133 /* pop include stack */
2135 s1
->include_stack_ptr
--;
2136 file
= *s1
->include_stack_ptr
;
2145 tok_flags
|= TOK_FLAG_BOL
;
2148 if (0 == (parse_flags
& PARSE_FLAG_LINEFEED
))
2151 goto keep_tok_flags
;
2156 if ((tok_flags
& TOK_FLAG_BOL
) &&
2157 (parse_flags
& PARSE_FLAG_PREPROCESS
)) {
2159 preprocess(tok_flags
& TOK_FLAG_BOF
);
2165 tok
= TOK_TWOSHARPS
;
2167 if (parse_flags
& PARSE_FLAG_ASM_COMMENTS
) {
2168 p
= parse_line_comment(p
- 1);
2177 case 'a': case 'b': case 'c': case 'd':
2178 case 'e': case 'f': case 'g': case 'h':
2179 case 'i': case 'j': case 'k': case 'l':
2180 case 'm': case 'n': case 'o': case 'p':
2181 case 'q': case 'r': case 's': case 't':
2182 case 'u': case 'v': case 'w': case 'x':
2184 case 'A': case 'B': case 'C': case 'D':
2185 case 'E': case 'F': case 'G': case 'H':
2186 case 'I': case 'J': case 'K':
2187 case 'M': case 'N': case 'O': case 'P':
2188 case 'Q': case 'R': case 'S': case 'T':
2189 case 'U': case 'V': case 'W': case 'X':
2195 h
= TOK_HASH_FUNC(h
, c
);
2199 if (!isidnum_table
[c
-CH_EOF
])
2201 h
= TOK_HASH_FUNC(h
, c
);
2208 /* fast case : no stray found, so we have the full token
2209 and we have already hashed it */
2211 h
&= (TOK_HASH_SIZE
- 1);
2212 pts
= &hash_ident
[h
];
2217 if (ts
->len
== len
&& !memcmp(ts
->str
, p1
, len
))
2219 pts
= &(ts
->hash_next
);
2221 ts
= tok_alloc_new(pts
, p1
, len
);
2225 cstr_reset(&tokcstr
);
2228 cstr_ccat(&tokcstr
, *p1
);
2234 while (isidnum_table
[c
-CH_EOF
]) {
2235 cstr_ccat(&tokcstr
, c
);
2238 ts
= tok_alloc(tokcstr
.data
, tokcstr
.size
);
2244 if (t
!= '\\' && t
!= '\'' && t
!= '\"') {
2246 goto parse_ident_fast
;
2249 if (c
== '\'' || c
== '\"') {
2253 cstr_reset(&tokcstr
);
2254 cstr_ccat(&tokcstr
, 'L');
2255 goto parse_ident_slow
;
2259 case '0': case '1': case '2': case '3':
2260 case '4': case '5': case '6': case '7':
2263 cstr_reset(&tokcstr
);
2264 /* after the first digit, accept digits, alpha, '.' or sign if
2265 prefixed by 'eEpP' */
2269 cstr_ccat(&tokcstr
, c
);
2271 if (!(isnum(c
) || isid(c
) || c
== '.' ||
2272 ((c
== '+' || c
== '-') &&
2273 (t
== 'e' || t
== 'E' || t
== 'p' || t
== 'P'))))
2276 /* We add a trailing '\0' to ease parsing */
2277 cstr_ccat(&tokcstr
, '\0');
2278 tokc
.cstr
= &tokcstr
;
2282 /* special dot handling because it can also start a number */
2285 cstr_reset(&tokcstr
);
2286 cstr_ccat(&tokcstr
, '.');
2288 } else if (c
== '.') {
2308 /* parse the string */
2310 p
= parse_pp_string(p
, sep
, &str
);
2311 cstr_ccat(&str
, '\0');
2313 /* eval the escape (should be done as TOK_PPNUM) */
2314 cstr_reset(&tokcstr
);
2315 parse_escape_string(&tokcstr
, str
.data
, is_long
);
2320 /* XXX: make it portable */
2324 char_size
= sizeof(nwchar_t
);
2325 if (tokcstr
.size
<= char_size
)
2326 error("empty character constant");
2327 if (tokcstr
.size
> 2 * char_size
)
2328 warning("multi-character character constant");
2330 tokc
.i
= *(int8_t *)tokcstr
.data
;
2333 tokc
.i
= *(nwchar_t
*)tokcstr
.data
;
2337 tokc
.cstr
= &tokcstr
;
2351 } else if (c
== '<') {
2369 } else if (c
== '>') {
2387 } else if (c
== '=') {
2400 } else if (c
== '=') {
2413 } else if (c
== '=') {
2426 } else if (c
== '=') {
2429 } else if (c
== '>') {
2437 PARSE2('!', '!', '=', TOK_NE
)
2438 PARSE2('=', '=', '=', TOK_EQ
)
2439 PARSE2('*', '*', '=', TOK_A_MUL
)
2440 PARSE2('%', '%', '=', TOK_A_MOD
)
2441 PARSE2('^', '^', '=', TOK_A_XOR
)
2443 /* comments or operator */
2447 p
= parse_comment(p
);
2449 } else if (c
== '/') {
2450 p
= parse_line_comment(p
);
2452 } else if (c
== '=') {
2472 case '$': /* only used in assembler */
2473 case '@': /* dito */
2478 error("unrecognized character \\x%02x", c
);
2484 #if defined(PARSE_DEBUG)
2485 printf("token = %s\n", get_tok_str(tok
, &tokc
));
2489 /* return next token without macro substitution. Can read input from
2491 static void next_nomacro_spc(void)
2497 TOK_GET(&tok
, ¯o_ptr
, &tokc
);
2498 if (tok
== TOK_LINENUM
) {
2499 file
->line_num
= tokc
.i
;
2508 ST_FUNC
void next_nomacro(void)
2512 } while (is_space(tok
));
2515 /* substitute args in macro_str and return allocated string */
2516 static int *macro_arg_subst(Sym
**nested_list
, const int *macro_str
, Sym
*args
)
2518 int last_tok
, t
, spc
;
2528 TOK_GET(&t
, ¯o_str
, &cval
);
2533 TOK_GET(&t
, ¯o_str
, &cval
);
2536 s
= sym_find2(args
, t
);
2542 TOK_GET(&t
, &st
, &cval
);
2543 if (!check_space(t
, &spc
))
2544 cstr_cat(&cstr
, get_tok_str(t
, &cval
));
2547 cstr_ccat(&cstr
, '\0');
2549 printf("stringize: %s\n", (char *)cstr
.data
);
2553 tok_str_add2(&str
, TOK_STR
, &cval
);
2556 tok_str_add2(&str
, t
, &cval
);
2558 } else if (t
>= TOK_IDENT
) {
2559 s
= sym_find2(args
, t
);
2562 /* if '##' is present before or after, no arg substitution */
2563 if (*macro_str
== TOK_TWOSHARPS
|| last_tok
== TOK_TWOSHARPS
) {
2564 /* special case for var arg macros : ## eats the
2565 ',' if empty VA_ARGS variable. */
2566 /* XXX: test of the ',' is not 100%
2567 reliable. should fix it to avoid security
2569 if (gnu_ext
&& s
->type
.t
&&
2570 last_tok
== TOK_TWOSHARPS
&&
2571 str
.len
>= 2 && str
.str
[str
.len
- 2] == ',') {
2573 /* suppress ',' '##' */
2576 /* suppress '##' and add variable */
2584 TOK_GET(&t1
, &st
, &cval
);
2587 tok_str_add2(&str
, t1
, &cval
);
2591 /* NOTE: the stream cannot be read when macro
2592 substituing an argument */
2593 macro_subst(&str
, nested_list
, st
, NULL
);
2596 tok_str_add(&str
, t
);
2599 tok_str_add2(&str
, t
, &cval
);
2603 tok_str_add(&str
, 0);
2607 static char const ab_month_name
[12][4] =
2609 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2610 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
2613 /* do macro substitution of current token with macro 's' and add
2614 result to (tok_str,tok_len). 'nested_list' is the list of all
2615 macros we got inside to avoid recursing. Return non zero if no
2616 substitution needs to be done */
2617 static int macro_subst_tok(TokenString
*tok_str
,
2618 Sym
**nested_list
, Sym
*s
, struct macro_level
**can_read_stream
)
2620 Sym
*args
, *sa
, *sa1
;
2621 int mstr_allocated
, parlevel
, *mstr
, t
, t1
, spc
;
2629 /* if symbol is a macro, prepare substitution */
2630 /* special macros */
2631 if (tok
== TOK___LINE__
) {
2632 snprintf(buf
, sizeof(buf
), "%d", file
->line_num
);
2636 } else if (tok
== TOK___FILE__
) {
2637 cstrval
= file
->filename
;
2639 } else if (tok
== TOK___DATE__
|| tok
== TOK___TIME__
) {
2644 tm
= localtime(&ti
);
2645 if (tok
== TOK___DATE__
) {
2646 snprintf(buf
, sizeof(buf
), "%s %2d %d",
2647 ab_month_name
[tm
->tm_mon
], tm
->tm_mday
, tm
->tm_year
+ 1900);
2649 snprintf(buf
, sizeof(buf
), "%02d:%02d:%02d",
2650 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
2657 cstr_cat(&cstr
, cstrval
);
2658 cstr_ccat(&cstr
, '\0');
2660 tok_str_add2(tok_str
, t1
, &cval
);
2665 if (s
->type
.t
== MACRO_FUNC
) {
2666 /* NOTE: we do not use next_nomacro to avoid eating the
2667 next token. XXX: find better solution */
2671 while (is_space(t
= *p
) || TOK_LINEFEED
== t
)
2673 if (t
== 0 && can_read_stream
) {
2674 /* end of macro stream: we must look at the token
2675 after in the file */
2676 struct macro_level
*ml
= *can_read_stream
;
2682 *can_read_stream
= ml
-> prev
;
2687 /* XXX: incorrect with comments */
2688 ch
= file
->buf_ptr
[0];
2689 while (is_space(ch
) || ch
== '\n')
2693 if (t
!= '(') /* no macro subst */
2696 /* argument macro */
2701 /* NOTE: empty args are allowed, except if no args */
2703 /* handle '()' case */
2704 if (!args
&& !sa
&& tok
== ')')
2707 error("macro '%s' used with too many args",
2708 get_tok_str(s
->v
, 0));
2711 /* NOTE: non zero sa->t indicates VA_ARGS */
2712 while ((parlevel
> 0 ||
2714 (tok
!= ',' || sa
->type
.t
))) &&
2718 else if (tok
== ')')
2720 if (tok
== TOK_LINEFEED
)
2722 if (!check_space(tok
, &spc
))
2723 tok_str_add2(&str
, tok
, &tokc
);
2727 tok_str_add(&str
, 0);
2728 sa1
= sym_push2(&args
, sa
->v
& ~SYM_FIELD
, sa
->type
.t
, 0);
2732 /* special case for gcc var args: add an empty
2733 var arg argument if it is omitted */
2734 if (sa
&& sa
->type
.t
&& gnu_ext
)
2744 error("macro '%s' used with too few args",
2745 get_tok_str(s
->v
, 0));
2748 /* now subst each arg */
2749 mstr
= macro_arg_subst(nested_list
, mstr
, args
);
2754 tok_str_free(sa
->d
);
2760 sym_push2(nested_list
, s
->v
, 0, 0);
2761 macro_subst(tok_str
, nested_list
, mstr
, can_read_stream
);
2762 /* pop nested defined symbol */
2764 *nested_list
= sa1
->prev
;
2772 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
2773 return the resulting string (which must be freed). */
2774 static inline int *macro_twosharps(const int *macro_str
)
2779 TokenString macro_str1
;
2784 /* we search the first '##' */
2785 for(ptr
= macro_str
;;) {
2786 TOK_GET(&t
, &ptr
, &cval
);
2787 if (t
== TOK_TWOSHARPS
)
2789 /* nothing more to do if end of string */
2794 /* we saw '##', so we need more processing to handle it */
2795 tok_str_new(¯o_str1
);
2796 for(ptr
= macro_str
;;) {
2797 TOK_GET(&tok
, &ptr
, &tokc
);
2800 if (tok
== TOK_TWOSHARPS
)
2802 while (*ptr
== TOK_TWOSHARPS
) {
2804 if (t
&& t
!= TOK_TWOSHARPS
) {
2805 TOK_GET(&t
, &ptr
, &cval
);
2807 /* We concatenate the two tokens */
2809 cstr_cat(&cstr
, get_tok_str(tok
, &tokc
));
2811 cstr_cat(&cstr
, get_tok_str(t
, &cval
));
2812 cstr_ccat(&cstr
, '\0');
2815 file
->buf_ptr
= cstr
.data
;
2818 if (0 == *file
->buf_ptr
)
2820 tok_str_add2(¯o_str1
, tok
, &tokc
);
2822 warning("pasting \"%.*s\" and \"%s\" does not give a valid preprocessing token",
2823 n
, cstr
.data
, (char*)cstr
.data
+ n
);
2829 tok_str_add2(¯o_str1
, tok
, &tokc
);
2831 tok_str_add(¯o_str1
, 0);
2832 return macro_str1
.str
;
2836 /* do macro substitution of macro_str and add result to
2837 (tok_str,tok_len). 'nested_list' is the list of all macros we got
2838 inside to avoid recursing. */
2839 static void macro_subst(TokenString
*tok_str
, Sym
**nested_list
,
2840 const int *macro_str
, struct macro_level
** can_read_stream
)
2847 struct macro_level ml
;
2849 /* first scan for '##' operator handling */
2851 macro_str1
= macro_twosharps(ptr
);
2856 /* NOTE: ptr == NULL can only happen if tokens are read from
2857 file stream due to a macro function call */
2860 TOK_GET(&t
, &ptr
, &cval
);
2865 /* if nested substitution, do nothing */
2866 if (sym_find2(*nested_list
, t
))
2869 if (can_read_stream
)
2870 ml
.prev
= *can_read_stream
, *can_read_stream
= &ml
;
2871 macro_ptr
= (int *)ptr
;
2873 ret
= macro_subst_tok(tok_str
, nested_list
, s
, can_read_stream
);
2874 ptr
= (int *)macro_ptr
;
2876 if (can_read_stream
&& *can_read_stream
== &ml
)
2877 *can_read_stream
= ml
.prev
;
2882 if (!check_space(t
, &spc
))
2883 tok_str_add2(tok_str
, t
, &cval
);
2887 tok_str_free(macro_str1
);
2890 /* return next token with macro substitution */
2891 ST_FUNC
void next(void)
2893 Sym
*nested_list
, *s
;
2895 struct macro_level
*ml
;
2898 if (parse_flags
& PARSE_FLAG_SPACES
)
2903 /* if not reading from macro substituted string, then try
2904 to substitute macros */
2905 if (tok
>= TOK_IDENT
&&
2906 (parse_flags
& PARSE_FLAG_PREPROCESS
)) {
2907 s
= define_find(tok
);
2909 /* we have a macro: we try to substitute */
2913 if (macro_subst_tok(&str
, &nested_list
, s
, &ml
) == 0) {
2914 /* substitution done, NOTE: maybe empty */
2915 tok_str_add(&str
, 0);
2916 macro_ptr
= str
.str
;
2917 macro_ptr_allocated
= str
.str
;
2924 /* end of macro or end of unget buffer */
2925 if (unget_buffer_enabled
) {
2926 macro_ptr
= unget_saved_macro_ptr
;
2927 unget_buffer_enabled
= 0;
2929 /* end of macro string: free it */
2930 tok_str_free(macro_ptr_allocated
);
2931 macro_ptr_allocated
= NULL
;
2938 /* convert preprocessor tokens into C tokens */
2939 if (tok
== TOK_PPNUM
&&
2940 (parse_flags
& PARSE_FLAG_TOK_NUM
)) {
2941 parse_number((char *)tokc
.cstr
->data
);
2945 /* push back current token and set current token to 'last_tok'. Only
2946 identifier case handled for labels. */
2947 ST_INLN
void unget_tok(int last_tok
)
2951 unget_saved_macro_ptr
= macro_ptr
;
2952 unget_buffer_enabled
= 1;
2953 q
= unget_saved_buffer
;
2956 n
= tok_ext_size(tok
) - 1;
2959 *q
= 0; /* end of token string */
2964 /* better than nothing, but needs extension to handle '-E' option
2966 ST_FUNC
void preprocess_init(TCCState
*s1
)
2968 s1
->include_stack_ptr
= s1
->include_stack
;
2969 /* XXX: move that before to avoid having to initialize
2970 file->ifdef_stack_ptr ? */
2971 s1
->ifdef_stack_ptr
= s1
->ifdef_stack
;
2972 file
->ifdef_stack_ptr
= s1
->ifdef_stack_ptr
;
2974 /* XXX: not ANSI compliant: bound checking says error */
2976 s1
->pack_stack
[0] = 0;
2977 s1
->pack_stack_ptr
= s1
->pack_stack
;
2980 ST_FUNC
void preprocess_new()
2986 /* init isid table */
2987 for(i
=CH_EOF
;i
<256;i
++)
2988 isidnum_table
[i
-CH_EOF
] = isid(i
) || isnum(i
);
2990 /* add all tokens */
2992 memset(hash_ident
, 0, TOK_HASH_SIZE
* sizeof(TokenSym
*));
2994 tok_ident
= TOK_IDENT
;
3003 ts
= tok_alloc(p
, r
- p
- 1);
3008 /* Preprocess the current file */
3009 ST_FUNC
int tcc_preprocess(TCCState
*s1
)
3013 BufferedFile
*file_ref
, **iptr
, **iptr_new
;
3014 int token_seen
, line_ref
, d
;
3017 preprocess_init(s1
);
3018 define_start
= define_stack
;
3019 ch
= file
->buf_ptr
[0];
3020 tok_flags
= TOK_FLAG_BOL
| TOK_FLAG_BOF
;
3021 parse_flags
= PARSE_FLAG_ASM_COMMENTS
| PARSE_FLAG_PREPROCESS
|
3022 PARSE_FLAG_LINEFEED
| PARSE_FLAG_SPACES
;
3026 iptr
= s1
->include_stack_ptr
;
3030 if (tok
== TOK_EOF
) {
3032 } else if (file
!= file_ref
) {
3034 } else if (tok
== TOK_LINEFEED
) {
3039 } else if (!token_seen
) {
3040 d
= file
->line_num
- line_ref
;
3041 if (file
!= file_ref
|| d
< 0 || d
>= 8) {
3043 iptr_new
= s1
->include_stack_ptr
;
3044 s
= iptr_new
> iptr
? " 1"
3045 : iptr_new
< iptr
? " 2"
3046 : iptr_new
> s1
->include_stack
? " 3"
3050 fprintf(s1
->outfile
, "# %d \"%s\"%s\n", file
->line_num
, file
->filename
, s
);
3053 fputs("\n", s1
->outfile
), --d
;
3055 line_ref
= (file_ref
= file
)->line_num
;
3056 token_seen
= tok
!= TOK_LINEFEED
;
3060 fputs(get_tok_str(tok
, &tokc
), s1
->outfile
);
3062 free_defines(define_start
);