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 /* true if isid(c) || isnum(c) */
62 static unsigned char isidnum_table
[256-CH_EOF
];
64 static const char tcc_keywords
[] =
65 #define DEF(id, str) str "\0"
70 /* WARNING: the content of this string encodes token numbers */
71 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";
95 '.','.', 0xa8, // C++ token ?
96 '#','#', TOK_TWOSHARPS
,
101 struct macro_level
*prev
;
105 static void next_nomacro_spc(void);
106 static void macro_subst(
107 TokenString
*tok_str
,
109 const int *macro_str
,
110 struct macro_level
**can_read_stream
113 ST_FUNC
void skip(int c
)
116 tcc_error("'%c' expected (got \"%s\")", c
, get_tok_str(tok
, &tokc
));
120 ST_FUNC
void expect(const char *msg
)
122 tcc_error("%s expected", msg
);
125 /* ------------------------------------------------------------------------- */
126 /* CString handling */
127 static void cstr_realloc(CString
*cstr
, int new_size
)
132 size
= cstr
->size_allocated
;
134 size
= 8; /* no need to allocate a too small first string */
135 while (size
< new_size
)
137 data
= tcc_realloc(cstr
->data_allocated
, size
);
138 cstr
->data_allocated
= data
;
139 cstr
->size_allocated
= size
;
144 ST_FUNC
void cstr_ccat(CString
*cstr
, int ch
)
147 size
= cstr
->size
+ 1;
148 if (size
> cstr
->size_allocated
)
149 cstr_realloc(cstr
, size
);
150 ((unsigned char *)cstr
->data
)[size
- 1] = ch
;
154 ST_FUNC
void cstr_cat(CString
*cstr
, const char *str
)
166 /* add a wide char */
167 ST_FUNC
void cstr_wccat(CString
*cstr
, int ch
)
170 size
= cstr
->size
+ sizeof(nwchar_t
);
171 if (size
> cstr
->size_allocated
)
172 cstr_realloc(cstr
, size
);
173 *(nwchar_t
*)(((unsigned char *)cstr
->data
) + size
- sizeof(nwchar_t
)) = ch
;
177 ST_FUNC
void cstr_new(CString
*cstr
)
179 memset(cstr
, 0, sizeof(CString
));
182 /* free string and reset it to NULL */
183 ST_FUNC
void cstr_free(CString
*cstr
)
185 tcc_free(cstr
->data_allocated
);
189 /* reset string to empty */
190 ST_FUNC
void cstr_reset(CString
*cstr
)
196 static void add_char(CString
*cstr
, int c
)
198 if (c
== '\'' || c
== '\"' || c
== '\\') {
199 /* XXX: could be more precise if char or string */
200 cstr_ccat(cstr
, '\\');
202 if (c
>= 32 && c
<= 126) {
205 cstr_ccat(cstr
, '\\');
207 cstr_ccat(cstr
, 'n');
209 cstr_ccat(cstr
, '0' + ((c
>> 6) & 7));
210 cstr_ccat(cstr
, '0' + ((c
>> 3) & 7));
211 cstr_ccat(cstr
, '0' + (c
& 7));
216 /* ------------------------------------------------------------------------- */
217 /* allocate a new token */
218 static TokenSym
*tok_alloc_new(TokenSym
**pts
, const char *str
, int len
)
220 TokenSym
*ts
, **ptable
;
223 if (tok_ident
>= SYM_FIRST_ANOM
)
224 tcc_error("memory full (symbols)");
226 /* expand token table if needed */
227 i
= tok_ident
- TOK_IDENT
;
228 if ((i
% TOK_ALLOC_INCR
) == 0) {
229 ptable
= tcc_realloc(table_ident
, (i
+ TOK_ALLOC_INCR
) * sizeof(TokenSym
*));
230 table_ident
= ptable
;
233 ts
= tcc_malloc(sizeof(TokenSym
) + len
);
235 ts
->tok
= tok_ident
++;
236 ts
->sym_define
= NULL
;
237 ts
->sym_label
= NULL
;
238 ts
->sym_struct
= NULL
;
239 ts
->sym_identifier
= NULL
;
241 ts
->hash_next
= NULL
;
242 memcpy(ts
->str
, str
, len
);
248 #define TOK_HASH_INIT 1
249 #define TOK_HASH_FUNC(h, c) ((h) * 263 + (c))
251 /* find a token and add it if not found */
252 ST_FUNC TokenSym
*tok_alloc(const char *str
, int len
)
260 h
= TOK_HASH_FUNC(h
, ((unsigned char *)str
)[i
]);
261 h
&= (TOK_HASH_SIZE
- 1);
263 pts
= &hash_ident
[h
];
268 if (ts
->len
== len
&& !memcmp(ts
->str
, str
, len
))
270 pts
= &(ts
->hash_next
);
272 return tok_alloc_new(pts
, str
, len
);
275 /* XXX: buffer overflow */
276 /* XXX: float tokens */
277 ST_FUNC
char *get_tok_str(int v
, CValue
*cv
)
279 static char buf
[STRING_MAX_SIZE
+ 1];
280 static CString cstr_buf
;
285 /* NOTE: to go faster, we give a fixed buffer for small strings */
286 cstr_reset(&cstr_buf
);
288 cstr_buf
.size_allocated
= sizeof(buf
);
291 /* just an explanation, should never happen:
292 if (v <= TOK_LINENUM && v >= TOK_CINT && cv == NULL)
293 tcc_error("internal error: get_tok_str"); */
298 /* XXX: not quite exact, but only useful for testing */
299 sprintf(p
, "%u", cv
->ui
);
303 /* XXX: not quite exact, but only useful for testing */
305 sprintf(p
, "%u", (unsigned)cv
->ull
);
307 sprintf(p
, "%llu", cv
->ull
);
311 cstr_ccat(&cstr_buf
, 'L');
313 cstr_ccat(&cstr_buf
, '\'');
314 add_char(&cstr_buf
, cv
->i
);
315 cstr_ccat(&cstr_buf
, '\'');
316 cstr_ccat(&cstr_buf
, '\0');
320 len
= cstr
->size
- 1;
322 add_char(&cstr_buf
, ((unsigned char *)cstr
->data
)[i
]);
323 cstr_ccat(&cstr_buf
, '\0');
326 cstr_ccat(&cstr_buf
, 'L');
329 cstr_ccat(&cstr_buf
, '\"');
331 len
= cstr
->size
- 1;
333 add_char(&cstr_buf
, ((unsigned char *)cstr
->data
)[i
]);
335 len
= (cstr
->size
/ sizeof(nwchar_t
)) - 1;
337 add_char(&cstr_buf
, ((nwchar_t
*)cstr
->data
)[i
]);
339 cstr_ccat(&cstr_buf
, '\"');
340 cstr_ccat(&cstr_buf
, '\0');
347 return NULL
; /* should not happen */
349 /* above tokens have value, the ones below don't */
358 return strcpy(p
, "...");
360 return strcpy(p
, "<<=");
362 return strcpy(p
, ">>=");
365 /* search in two bytes table */
366 const unsigned char *q
= tok_two_chars
;
379 } else if (v
< tok_ident
) {
380 return table_ident
[v
- TOK_IDENT
]->str
;
381 } else if (v
>= SYM_FIRST_ANOM
) {
382 /* special name for anonymous symbol */
383 sprintf(p
, "L.%u", v
- SYM_FIRST_ANOM
);
385 /* should never happen */
390 return cstr_buf
.data
;
393 /* fill input buffer and peek next char */
394 static int tcc_peekc_slow(BufferedFile
*bf
)
397 /* only tries to read if really end of buffer */
398 if (bf
->buf_ptr
>= bf
->buf_end
) {
400 #if defined(PARSE_DEBUG)
405 len
= read(bf
->fd
, bf
->buffer
, len
);
412 bf
->buf_ptr
= bf
->buffer
;
413 bf
->buf_end
= bf
->buffer
+ len
;
414 *bf
->buf_end
= CH_EOB
;
416 if (bf
->buf_ptr
< bf
->buf_end
) {
417 return bf
->buf_ptr
[0];
419 bf
->buf_ptr
= bf
->buf_end
;
424 /* return the current character, handling end of block if necessary
426 ST_FUNC
int handle_eob(void)
428 return tcc_peekc_slow(file
);
431 /* read next char from current input file and handle end of input buffer */
432 ST_INLN
void inp(void)
434 ch
= *(++(file
->buf_ptr
));
435 /* end of buffer/file handling */
440 /* handle '\[\r]\n' */
441 static int handle_stray_noerror(void)
448 } else if (ch
== '\r') {
462 static void handle_stray(void)
464 if (handle_stray_noerror())
465 tcc_error("stray '\\' in program");
468 /* skip the stray and handle the \\n case. Output an error if
469 incorrect char after the stray */
470 static int handle_stray1(uint8_t *p
)
474 if (p
>= file
->buf_end
) {
491 /* handle just the EOB case, but not stray */
492 #define PEEKC_EOB(c, p)\
503 /* handle the complicated stray case */
509 c = handle_stray1(p);\
514 /* input with '\[\r]\n' handling. Note that this function cannot
515 handle other characters after '\', so you cannot call it inside
516 strings or comments */
517 ST_FUNC
void minp(void)
525 /* single line C++ comments */
526 static uint8_t *parse_line_comment(uint8_t *p
)
534 if (c
== '\n' || c
== CH_EOF
) {
536 } else if (c
== '\\') {
545 } else if (c
== '\r') {
563 ST_FUNC
uint8_t *parse_comment(uint8_t *p
)
572 if (c
== '\n' || c
== '*' || c
== '\\')
576 if (c
== '\n' || c
== '*' || c
== '\\')
580 /* now we can handle all the cases */
584 } else if (c
== '*') {
590 } else if (c
== '/') {
592 } else if (c
== '\\') {
597 /* skip '\[\r]\n', otherwise just skip the stray */
603 } else if (c
== '\r') {
620 /* stray, eob or eof */
625 tcc_error("unexpected end of file in comment");
626 } else if (c
== '\\') {
638 static inline void skip_spaces(void)
644 static inline int check_space(int t
, int *spc
)
655 /* parse a string without interpreting escapes */
656 static uint8_t *parse_pp_string(uint8_t *p
,
657 int sep
, CString
*str
)
665 } else if (c
== '\\') {
671 /* XXX: indicate line number of start of string */
672 tcc_error("missing terminating %c character", sep
);
673 } else if (c
== '\\') {
674 /* escape : just skip \[\r]\n */
679 } else if (c
== '\r') {
682 expect("'\n' after '\r'");
685 } else if (c
== CH_EOF
) {
686 goto unterminated_string
;
689 cstr_ccat(str
, '\\');
695 } else if (c
== '\n') {
698 } else if (c
== '\r') {
702 cstr_ccat(str
, '\r');
718 /* skip block of text until #else, #elif or #endif. skip also pairs of
720 static void preprocess_skip(void)
722 int a
, start_of_line
, c
, in_warn_or_error
;
729 in_warn_or_error
= 0;
750 } else if (c
== '\\') {
751 ch
= file
->buf_ptr
[0];
752 handle_stray_noerror();
759 if (in_warn_or_error
)
761 p
= parse_pp_string(p
, c
, NULL
);
765 if (in_warn_or_error
)
772 p
= parse_comment(p
);
773 } else if (ch
== '/') {
774 p
= parse_line_comment(p
);
784 (tok
== TOK_ELSE
|| tok
== TOK_ELIF
|| tok
== TOK_ENDIF
))
786 if (tok
== TOK_IF
|| tok
== TOK_IFDEF
|| tok
== TOK_IFNDEF
)
788 else if (tok
== TOK_ENDIF
)
790 else if( tok
== TOK_ERROR
|| tok
== TOK_WARNING
)
791 in_warn_or_error
= 1;
792 else if (tok
== TOK_LINEFEED
)
807 /* ParseState handling */
809 /* XXX: currently, no include file info is stored. Thus, we cannot display
810 accurate messages if the function or data definition spans multiple
813 /* save current parse state in 's' */
814 ST_FUNC
void save_parse_state(ParseState
*s
)
816 s
->line_num
= file
->line_num
;
817 s
->macro_ptr
= macro_ptr
;
822 /* restore parse state from 's' */
823 ST_FUNC
void restore_parse_state(ParseState
*s
)
825 file
->line_num
= s
->line_num
;
826 macro_ptr
= s
->macro_ptr
;
831 /* return the number of additional 'ints' necessary to store the
833 static inline int tok_ext_size(int t
)
847 tcc_error("unsupported token");
854 return LDOUBLE_SIZE
/ 4;
860 /* token string handling */
862 ST_INLN
void tok_str_new(TokenString
*s
)
866 s
->allocated_len
= 0;
867 s
->last_line_num
= -1;
870 ST_FUNC
void tok_str_free(int *str
)
875 static int *tok_str_realloc(TokenString
*s
)
879 if (s
->allocated_len
== 0) {
882 len
= s
->allocated_len
* 2;
884 str
= tcc_realloc(s
->str
, len
* sizeof(int));
885 s
->allocated_len
= len
;
890 ST_FUNC
void tok_str_add(TokenString
*s
, int t
)
896 if (len
>= s
->allocated_len
)
897 str
= tok_str_realloc(s
);
902 static void tok_str_add2(TokenString
*s
, int t
, CValue
*cv
)
909 /* allocate space for worst case */
910 if (len
+ TOK_MAX_SIZE
> s
->allocated_len
)
911 str
= tok_str_realloc(s
);
920 str
[len
++] = cv
->tab
[0];
929 nb_words
= (sizeof(CString
) + cv
->cstr
->size
+ 3) >> 2;
930 while ((len
+ nb_words
) > s
->allocated_len
)
931 str
= tok_str_realloc(s
);
932 cstr
= (CString
*)(str
+ len
);
934 cstr
->size
= cv
->cstr
->size
;
935 cstr
->data_allocated
= NULL
;
936 cstr
->size_allocated
= cstr
->size
;
937 memcpy((char *)cstr
+ sizeof(CString
),
938 cv
->cstr
->data
, cstr
->size
);
945 #if LDOUBLE_SIZE == 8
948 str
[len
++] = cv
->tab
[0];
949 str
[len
++] = cv
->tab
[1];
951 #if LDOUBLE_SIZE == 12
953 str
[len
++] = cv
->tab
[0];
954 str
[len
++] = cv
->tab
[1];
955 str
[len
++] = cv
->tab
[2];
956 #elif LDOUBLE_SIZE == 16
958 str
[len
++] = cv
->tab
[0];
959 str
[len
++] = cv
->tab
[1];
960 str
[len
++] = cv
->tab
[2];
961 str
[len
++] = cv
->tab
[3];
962 #elif LDOUBLE_SIZE != 8
963 #error add long double size support
972 /* add the current parse token in token string 's' */
973 ST_FUNC
void tok_str_add_tok(TokenString
*s
)
977 /* save line number info */
978 if (file
->line_num
!= s
->last_line_num
) {
979 s
->last_line_num
= file
->line_num
;
980 cval
.i
= s
->last_line_num
;
981 tok_str_add2(s
, TOK_LINENUM
, &cval
);
983 tok_str_add2(s
, tok
, &tokc
);
986 /* get a token from an integer array and increment pointer
987 accordingly. we code it as a macro to avoid pointer aliasing. */
988 static inline void TOK_GET(int *t
, const int **pp
, CValue
*cv
)
1006 cv
->cstr
= (CString
*)p
;
1007 cv
->cstr
->data
= (char *)p
+ sizeof(CString
);
1008 p
+= (sizeof(CString
) + cv
->cstr
->size
+ 3) >> 2;
1016 #if LDOUBLE_SIZE == 16
1018 #elif LDOUBLE_SIZE == 12
1020 #elif LDOUBLE_SIZE == 8
1023 # error add long double size support
1036 static int macro_is_equal(const int *a
, const int *b
)
1038 char buf
[STRING_MAX_SIZE
+ 1];
1042 TOK_GET(&t
, &a
, &cv
);
1043 pstrcpy(buf
, sizeof buf
, get_tok_str(t
, &cv
));
1044 TOK_GET(&t
, &b
, &cv
);
1045 if (strcmp(buf
, get_tok_str(t
, &cv
)))
1051 /* defines handling */
1052 ST_INLN
void define_push(int v
, int macro_type
, int *str
, Sym
*first_arg
)
1057 if (s
&& !macro_is_equal(s
->d
, str
))
1058 tcc_warning("%s redefined", get_tok_str(v
, NULL
));
1060 s
= sym_push2(&define_stack
, v
, macro_type
, 0);
1062 s
->next
= first_arg
;
1063 table_ident
[v
- TOK_IDENT
]->sym_define
= s
;
1066 /* undefined a define symbol. Its name is just set to zero */
1067 ST_FUNC
void define_undef(Sym
*s
)
1071 if (v
>= TOK_IDENT
&& v
< tok_ident
)
1072 table_ident
[v
- TOK_IDENT
]->sym_define
= NULL
;
1076 ST_INLN Sym
*define_find(int v
)
1079 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
1081 return table_ident
[v
]->sym_define
;
1084 /* free define stack until top reaches 'b' */
1085 ST_FUNC
void free_defines(Sym
*b
)
1093 /* do not free args or predefined defines */
1095 tok_str_free(top
->d
);
1097 if (v
>= TOK_IDENT
&& v
< tok_ident
)
1098 table_ident
[v
- TOK_IDENT
]->sym_define
= NULL
;
1106 ST_FUNC Sym
*label_find(int v
)
1109 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
1111 return table_ident
[v
]->sym_label
;
1114 ST_FUNC Sym
*label_push(Sym
**ptop
, int v
, int flags
)
1117 s
= sym_push2(ptop
, v
, 0, 0);
1119 ps
= &table_ident
[v
- TOK_IDENT
]->sym_label
;
1120 if (ptop
== &global_label_stack
) {
1121 /* modify the top most local identifier, so that
1122 sym_identifier will point to 's' when popped */
1124 ps
= &(*ps
)->prev_tok
;
1131 /* pop labels until element last is reached. Look if any labels are
1132 undefined. Define symbols if '&&label' was used. */
1133 ST_FUNC
void label_pop(Sym
**ptop
, Sym
*slast
)
1136 for(s
= *ptop
; s
!= slast
; s
= s1
) {
1138 if (s
->r
== LABEL_DECLARED
) {
1139 tcc_warning("label '%s' declared but not used", get_tok_str(s
->v
, NULL
));
1140 } else if (s
->r
== LABEL_FORWARD
) {
1141 tcc_error("label '%s' used but not defined",
1142 get_tok_str(s
->v
, NULL
));
1145 /* define corresponding symbol. A size of
1147 put_extern_sym(s
, cur_text_section
, s
->jnext
, 1);
1151 table_ident
[s
->v
- TOK_IDENT
]->sym_label
= s
->prev_tok
;
1157 /* eval an expression for #if/#elif */
1158 static int expr_preprocess(void)
1164 while (tok
!= TOK_LINEFEED
&& tok
!= TOK_EOF
) {
1165 next(); /* do macro subst */
1166 if (tok
== TOK_DEFINED
) {
1171 c
= define_find(tok
) != 0;
1176 } else if (tok
>= TOK_IDENT
) {
1177 /* if undefined macro */
1181 tok_str_add_tok(&str
);
1183 tok_str_add(&str
, -1); /* simulate end of file */
1184 tok_str_add(&str
, 0);
1185 /* now evaluate C constant expression */
1186 macro_ptr
= str
.str
;
1190 tok_str_free(str
.str
);
1194 #if defined(PARSE_DEBUG) || defined(PP_DEBUG)
1195 static void tok_print(int *str
)
1202 TOK_GET(&t
, &str
, &cval
);
1205 printf("%s", get_tok_str(t
, &cval
));
1211 /* parse after #define */
1212 ST_FUNC
void parse_define(void)
1214 Sym
*s
, *first
, **ps
;
1215 int v
, t
, varg
, is_vaargs
, spc
, ptok
, macro_list_start
;
1220 tcc_error("invalid macro name '%s'", get_tok_str(tok
, &tokc
));
1221 /* XXX: should check if same macro (ANSI) */
1224 /* '(' must be just after macro definition for MACRO_FUNC */
1229 while (tok
!= ')') {
1233 if (varg
== TOK_DOTS
) {
1234 varg
= TOK___VA_ARGS__
;
1236 } else if (tok
== TOK_DOTS
&& gnu_ext
) {
1240 if (varg
< TOK_IDENT
)
1241 tcc_error("badly punctuated parameter list");
1242 s
= sym_push2(&define_stack
, varg
| SYM_FIELD
, is_vaargs
, 0);
1255 /* EOF testing necessary for '-D' handling */
1257 macro_list_start
= 1;
1258 while (tok
!= TOK_LINEFEED
&& tok
!= TOK_EOF
) {
1259 if (!macro_list_start
&& spc
== 2 && tok
== TOK_TWOSHARPS
)
1260 tcc_error("'##' invalid at start of macro");
1262 /* remove spaces around ## and after '#' */
1263 if (TOK_TWOSHARPS
== tok
) {
1267 } else if ('#' == tok
) {
1269 } else if (check_space(tok
, &spc
)) {
1272 tok_str_add2(&str
, tok
, &tokc
);
1275 macro_list_start
= 0;
1277 if (ptok
== TOK_TWOSHARPS
)
1278 tcc_error("'##' invalid at end of macro");
1280 --str
.len
; /* remove trailing space */
1281 tok_str_add(&str
, 0);
1283 printf("define %s %d: ", get_tok_str(v
, NULL
), t
);
1286 define_push(v
, t
, str
.str
, first
);
1289 static inline int hash_cached_include(const char *filename
)
1291 const unsigned char *s
;
1295 s
= (unsigned char *) filename
;
1297 h
= TOK_HASH_FUNC(h
, *s
);
1300 h
&= (CACHED_INCLUDES_HASH_SIZE
- 1);
1304 static CachedInclude
*search_cached_include(TCCState
*s1
, const char *filename
)
1308 h
= hash_cached_include(filename
);
1309 i
= s1
->cached_includes_hash
[h
];
1313 e
= s1
->cached_includes
[i
- 1];
1314 if (0 == PATHCMP(e
->filename
, filename
))
1321 static inline void add_cached_include(TCCState
*s1
, const char *filename
, int ifndef_macro
)
1326 if (search_cached_include(s1
, filename
))
1329 printf("adding cached '%s' %s\n", filename
, get_tok_str(ifndef_macro
, NULL
));
1331 e
= tcc_malloc(sizeof(CachedInclude
) + strlen(filename
));
1332 strcpy(e
->filename
, filename
);
1333 e
->ifndef_macro
= ifndef_macro
;
1334 dynarray_add((void ***)&s1
->cached_includes
, &s1
->nb_cached_includes
, e
);
1335 /* add in hash table */
1336 h
= hash_cached_include(filename
);
1337 e
->hash_next
= s1
->cached_includes_hash
[h
];
1338 s1
->cached_includes_hash
[h
] = s1
->nb_cached_includes
;
1341 static void pragma_parse(TCCState
*s1
)
1346 if (tok
== TOK_pack
) {
1349 #pragma pack(1) // set
1350 #pragma pack() // reset to default
1351 #pragma pack(push,1) // push & set
1352 #pragma pack(pop) // restore previous
1356 if (tok
== TOK_ASM_pop
) {
1358 if (s1
->pack_stack_ptr
<= s1
->pack_stack
) {
1360 tcc_error("out of pack stack");
1362 s1
->pack_stack_ptr
--;
1366 if (tok
== TOK_ASM_push
) {
1368 if (s1
->pack_stack_ptr
>= s1
->pack_stack
+ PACK_STACK_SIZE
- 1)
1370 s1
->pack_stack_ptr
++;
1373 if (tok
!= TOK_CINT
) {
1375 tcc_error("invalid pack pragma");
1378 if (val
< 1 || val
> 16 || (val
& (val
- 1)) != 0)
1382 *s1
->pack_stack_ptr
= val
;
1388 /* is_bof is true if first non space token at beginning of file */
1389 ST_FUNC
void preprocess(int is_bof
)
1391 TCCState
*s1
= tcc_state
;
1392 int i
, c
, n
, saved_parse_flags
;
1396 saved_parse_flags
= parse_flags
;
1397 parse_flags
= PARSE_FLAG_PREPROCESS
| PARSE_FLAG_TOK_NUM
|
1398 PARSE_FLAG_LINEFEED
;
1408 s
= define_find(tok
);
1409 /* undefine symbol by putting an invalid name */
1414 case TOK_INCLUDE_NEXT
:
1415 ch
= file
->buf_ptr
[0];
1416 /* XXX: incorrect if comments : use next_nomacro with a special mode */
1421 } else if (ch
== '\"') {
1426 while (ch
!= c
&& ch
!= '\n' && ch
!= CH_EOF
) {
1427 if ((q
- buf
) < sizeof(buf
) - 1)
1430 if (handle_stray_noerror() == 0)
1438 /* eat all spaces and comments after include */
1439 /* XXX: slightly incorrect */
1440 while (ch1
!= '\n' && ch1
!= CH_EOF
)
1444 /* computed #include : either we have only strings or
1445 we have anything enclosed in '<>' */
1448 if (tok
== TOK_STR
) {
1449 while (tok
!= TOK_LINEFEED
) {
1450 if (tok
!= TOK_STR
) {
1452 tcc_error("'#include' expects \"FILENAME\" or <FILENAME>");
1454 pstrcat(buf
, sizeof(buf
), (char *)tokc
.cstr
->data
);
1460 while (tok
!= TOK_LINEFEED
) {
1461 pstrcat(buf
, sizeof(buf
), get_tok_str(tok
, &tokc
));
1465 /* check syntax and remove '<>' */
1466 if (len
< 2 || buf
[0] != '<' || buf
[len
- 1] != '>')
1467 goto include_syntax
;
1468 memmove(buf
, buf
+ 1, len
- 2);
1469 buf
[len
- 2] = '\0';
1474 if (s1
->include_stack_ptr
>= s1
->include_stack
+ INCLUDE_STACK_SIZE
)
1475 tcc_error("#include recursion too deep");
1476 /* store current file in stack, but increment stack later below */
1477 *s1
->include_stack_ptr
= file
;
1479 n
= s1
->nb_include_paths
+ s1
->nb_sysinclude_paths
;
1480 for (i
= -2; i
< n
; ++i
) {
1481 char buf1
[sizeof file
->filename
];
1487 /* check absolute include path */
1488 if (!IS_ABSPATH(buf
))
1491 i
= n
; /* force end loop */
1493 } else if (i
== -1) {
1494 /* search in current dir if "header.h" */
1497 path
= file
->filename
;
1498 pstrncpy(buf1
, path
, tcc_basename(path
) - path
);
1501 /* search in all the include paths */
1502 if (i
< s1
->nb_include_paths
)
1503 path
= s1
->include_paths
[i
];
1505 path
= s1
->sysinclude_paths
[i
- s1
->nb_include_paths
];
1506 pstrcpy(buf1
, sizeof(buf1
), path
);
1507 pstrcat(buf1
, sizeof(buf1
), "/");
1510 pstrcat(buf1
, sizeof(buf1
), buf
);
1512 if (tok
== TOK_INCLUDE_NEXT
)
1513 for (f
= s1
->include_stack_ptr
; f
>= s1
->include_stack
; --f
)
1514 if (0 == PATHCMP((*f
)->filename
, buf1
)) {
1516 printf("%s: #include_next skipping %s\n", file
->filename
, buf1
);
1518 goto include_trynext
;
1521 e
= search_cached_include(s1
, buf1
);
1522 if (e
&& define_find(e
->ifndef_macro
)) {
1523 /* no need to parse the include because the 'ifndef macro'
1526 printf("%s: skipping cached %s\n", file
->filename
, buf1
);
1531 if (tcc_open(s1
, buf1
) < 0)
1536 printf("%s: including %s\n", file
->prev
->filename
, file
->filename
);
1538 /* update target deps */
1539 dynarray_add((void ***)&s1
->target_deps
, &s1
->nb_target_deps
,
1541 /* push current file in stack */
1542 ++s1
->include_stack_ptr
;
1543 /* add include file debug info */
1545 put_stabs(file
->filename
, N_BINCL
, 0, 0, 0);
1546 tok_flags
|= TOK_FLAG_BOF
| TOK_FLAG_BOL
;
1547 ch
= file
->buf_ptr
[0];
1550 tcc_error("include file '%s' not found", buf
);
1557 c
= expr_preprocess();
1563 if (tok
< TOK_IDENT
)
1564 tcc_error("invalid argument for '#if%sdef'", c
? "n" : "");
1568 printf("#ifndef %s\n", get_tok_str(tok
, NULL
));
1570 file
->ifndef_macro
= tok
;
1573 c
= (define_find(tok
) != 0) ^ c
;
1575 if (s1
->ifdef_stack_ptr
>= s1
->ifdef_stack
+ IFDEF_STACK_SIZE
)
1576 tcc_error("memory full (ifdef)");
1577 *s1
->ifdef_stack_ptr
++ = c
;
1580 if (s1
->ifdef_stack_ptr
== s1
->ifdef_stack
)
1581 tcc_error("#else without matching #if");
1582 if (s1
->ifdef_stack_ptr
[-1] & 2)
1583 tcc_error("#else after #else");
1584 c
= (s1
->ifdef_stack_ptr
[-1] ^= 3);
1587 if (s1
->ifdef_stack_ptr
== s1
->ifdef_stack
)
1588 tcc_error("#elif without matching #if");
1589 c
= s1
->ifdef_stack_ptr
[-1];
1591 tcc_error("#elif after #else");
1592 /* last #if/#elif expression was true: we skip */
1595 c
= expr_preprocess();
1596 s1
->ifdef_stack_ptr
[-1] = c
;
1598 if (s1
->ifdef_stack_ptr
== file
->ifdef_stack_ptr
+ 1)
1599 file
->ifndef_macro
= 0;
1609 if (s1
->ifdef_stack_ptr
<= file
->ifdef_stack_ptr
)
1610 tcc_error("#endif without matching #if");
1611 s1
->ifdef_stack_ptr
--;
1612 /* '#ifndef macro' was at the start of file. Now we check if
1613 an '#endif' is exactly at the end of file */
1614 if (file
->ifndef_macro
&&
1615 s1
->ifdef_stack_ptr
== file
->ifdef_stack_ptr
) {
1616 file
->ifndef_macro_saved
= file
->ifndef_macro
;
1617 /* need to set to zero to avoid false matches if another
1618 #ifndef at middle of file */
1619 file
->ifndef_macro
= 0;
1620 while (tok
!= TOK_LINEFEED
)
1622 tok_flags
|= TOK_FLAG_ENDIF
;
1628 if (tok
!= TOK_CINT
)
1630 file
->line_num
= tokc
.i
- 1; /* the line number will be incremented after */
1632 if (tok
!= TOK_LINEFEED
) {
1635 pstrcpy(file
->filename
, sizeof(file
->filename
),
1636 (char *)tokc
.cstr
->data
);
1642 ch
= file
->buf_ptr
[0];
1645 while (ch
!= '\n' && ch
!= CH_EOF
) {
1646 if ((q
- buf
) < sizeof(buf
) - 1)
1649 if (handle_stray_noerror() == 0)
1656 tcc_error("#error %s", buf
);
1658 tcc_warning("#warning %s", buf
);
1664 if (tok
== TOK_LINEFEED
|| tok
== '!' || tok
== TOK_PPNUM
) {
1665 /* '!' is ignored to allow C scripts. numbers are ignored
1666 to emulate cpp behaviour */
1668 if (!(saved_parse_flags
& PARSE_FLAG_ASM_COMMENTS
))
1669 tcc_warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok
, &tokc
));
1671 /* this is a gas line comment in an 'S' file. */
1672 file
->buf_ptr
= parse_line_comment(file
->buf_ptr
);
1678 /* ignore other preprocess commands or #! for C scripts */
1679 while (tok
!= TOK_LINEFEED
)
1682 parse_flags
= saved_parse_flags
;
1685 /* evaluate escape codes in a string. */
1686 static void parse_escape_string(CString
*outstr
, const uint8_t *buf
, int is_long
)
1701 case '0': case '1': case '2': case '3':
1702 case '4': case '5': case '6': case '7':
1703 /* at most three octal digits */
1708 n
= n
* 8 + c
- '0';
1712 n
= n
* 8 + c
- '0';
1717 goto add_char_nonext
;
1725 if (c
>= 'a' && c
<= 'f')
1727 else if (c
>= 'A' && c
<= 'F')
1737 goto add_char_nonext
;
1761 goto invalid_escape
;
1771 if (c
>= '!' && c
<= '~')
1772 tcc_warning("unknown escape sequence: \'\\%c\'", c
);
1774 tcc_warning("unknown escape sequence: \'\\x%x\'", c
);
1781 cstr_ccat(outstr
, c
);
1783 cstr_wccat(outstr
, c
);
1785 /* add a trailing '\0' */
1787 cstr_ccat(outstr
, '\0');
1789 cstr_wccat(outstr
, '\0');
1792 /* parse number in null terminated string 'p' and return it in the
1794 static void parse_number(const char *p
)
1803 goto float_frac_parse
;
1806 if (t
== 'x' || t
== 'X') {
1809 } else if (tcc_ext
&& (t
== 'b' || t
== 'B')) {
1817 if(strchr(p
, '.') || (b
== 10 && (strchr(p
,'e') || strchr(p
,'E'))) ||
1818 ((b
== 2 || b
== 16)&& (strchr(p
,'p') || strchr(p
,'P')))){
1819 long double ld
, sh
, fb
;
1821 /* NOTE: strtox should support that for hexa numbers, but
1822 non ISOC99 libcs do not support it, so we prefer to do
1824 /* hexadecimal or binary floats */
1825 /* XXX: handle overflows */
1834 if (c
>= 'a' && c
<= 'f')
1836 else if (c
>= 'A' && c
<= 'F')
1843 tcc_error("invalid digit");
1853 if (c
>= 'a' && c
<= 'f')
1855 else if (c
>= 'A' && c
<= 'F')
1862 if(b
== 10 && (c
== 'e' || c
== 'E' || c
== 'f' || c
== 'F'))
1864 tcc_error("invalid digit");
1871 if ((b
== 16 || b
== 2) && c
!= 'p' && c
!= 'P')
1873 if(((c
== 'e' || c
== 'E') && b
== 10) ||
1874 ((c
== 'p' || c
== 'P') && (b
== 16 || b
== 2))){
1876 if(c
== '+' || c
== '-'){
1883 expect("exponent digits");
1886 exp
= exp
* 10 + c
- '0';
1901 } else if (t
== 'L') {
1903 #ifdef TCC_TARGET_PE
1905 tokc
.d
= (double)ld
;
1912 tokc
.d
= (double)ld
;
1918 if (b
== 10 && c
== '0') {
1924 if (c
>= 'a' && c
<= 'f')
1926 else if (c
>= 'A' && c
<= 'F')
1933 tcc_error("invalid digit");
1936 if (n
< n1
&& warn
){
1937 tcc_warning("integer constant overflow");
1942 /* XXX: not exactly ANSI compliant */
1943 if ((n
& 0xffffffff00000000LL
) != 0) {
1948 } else if (n
> 0x7fffffff) {
1959 tcc_error("three 'l's in integer constant");
1961 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
1964 if (tok
== TOK_CINT
)
1966 else if (tok
== TOK_CUINT
)
1968 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
1972 } else if (t
== 'U') {
1974 tcc_error("two 'u's in integer constant");
1976 if (tok
== TOK_CINT
)
1978 else if (tok
== TOK_CLLONG
)
1985 if (tok
== TOK_CINT
|| tok
== TOK_CUINT
)
1991 tcc_error("invalid number\n");
1995 #define PARSE2(c1, tok1, c2, tok2) \
2006 /* return next token without macro substitution */
2007 static inline void next_nomacro1(void)
2022 goto keep_tok_flags
;
2029 /* first look if it is in fact an end of buffer */
2030 if (p
>= file
->buf_end
) {
2034 if (p
>= file
->buf_end
)
2047 TCCState
*s1
= tcc_state
;
2048 if ((parse_flags
& PARSE_FLAG_LINEFEED
)
2049 && !(tok_flags
& TOK_FLAG_EOF
)) {
2050 tok_flags
|= TOK_FLAG_EOF
;
2052 goto keep_tok_flags
;
2053 } else if (!(parse_flags
& PARSE_FLAG_PREPROCESS
)) {
2055 } else if (s1
->ifdef_stack_ptr
!= file
->ifdef_stack_ptr
) {
2056 tcc_error("missing #endif");
2057 } else if (s1
->include_stack_ptr
== s1
->include_stack
) {
2058 /* no include left : end of file. */
2061 tok_flags
&= ~TOK_FLAG_EOF
;
2062 /* pop include file */
2064 /* test if previous '#endif' was after a #ifdef at
2066 if (tok_flags
& TOK_FLAG_ENDIF
) {
2068 printf("#endif %s\n", get_tok_str(file
->ifndef_macro_saved
, NULL
));
2070 add_cached_include(s1
, file
->filename
, file
->ifndef_macro_saved
);
2071 tok_flags
&= ~TOK_FLAG_ENDIF
;
2074 /* add end of include file debug info */
2075 if (tcc_state
->do_debug
) {
2076 put_stabd(N_EINCL
, 0, 0);
2078 /* pop include stack */
2080 s1
->include_stack_ptr
--;
2089 tok_flags
|= TOK_FLAG_BOL
;
2092 if (0 == (parse_flags
& PARSE_FLAG_LINEFEED
))
2095 goto keep_tok_flags
;
2100 if ((tok_flags
& TOK_FLAG_BOL
) &&
2101 (parse_flags
& PARSE_FLAG_PREPROCESS
)) {
2103 preprocess(tok_flags
& TOK_FLAG_BOF
);
2109 tok
= TOK_TWOSHARPS
;
2111 if (parse_flags
& PARSE_FLAG_ASM_COMMENTS
) {
2112 p
= parse_line_comment(p
- 1);
2121 case 'a': case 'b': case 'c': case 'd':
2122 case 'e': case 'f': case 'g': case 'h':
2123 case 'i': case 'j': case 'k': case 'l':
2124 case 'm': case 'n': case 'o': case 'p':
2125 case 'q': case 'r': case 's': case 't':
2126 case 'u': case 'v': case 'w': case 'x':
2128 case 'A': case 'B': case 'C': case 'D':
2129 case 'E': case 'F': case 'G': case 'H':
2130 case 'I': case 'J': case 'K':
2131 case 'M': case 'N': case 'O': case 'P':
2132 case 'Q': case 'R': case 'S': case 'T':
2133 case 'U': case 'V': case 'W': case 'X':
2139 h
= TOK_HASH_FUNC(h
, c
);
2143 if (!isidnum_table
[c
-CH_EOF
])
2145 h
= TOK_HASH_FUNC(h
, c
);
2152 /* fast case : no stray found, so we have the full token
2153 and we have already hashed it */
2155 h
&= (TOK_HASH_SIZE
- 1);
2156 pts
= &hash_ident
[h
];
2161 if (ts
->len
== len
&& !memcmp(ts
->str
, p1
, len
))
2163 pts
= &(ts
->hash_next
);
2165 ts
= tok_alloc_new(pts
, (char *) p1
, len
);
2169 cstr_reset(&tokcstr
);
2172 cstr_ccat(&tokcstr
, *p1
);
2178 while (isidnum_table
[c
-CH_EOF
]) {
2179 cstr_ccat(&tokcstr
, c
);
2182 ts
= tok_alloc(tokcstr
.data
, tokcstr
.size
);
2188 if (t
!= '\\' && t
!= '\'' && t
!= '\"') {
2190 goto parse_ident_fast
;
2193 if (c
== '\'' || c
== '\"') {
2197 cstr_reset(&tokcstr
);
2198 cstr_ccat(&tokcstr
, 'L');
2199 goto parse_ident_slow
;
2203 case '0': case '1': case '2': case '3':
2204 case '4': case '5': case '6': case '7':
2207 cstr_reset(&tokcstr
);
2208 /* after the first digit, accept digits, alpha, '.' or sign if
2209 prefixed by 'eEpP' */
2213 cstr_ccat(&tokcstr
, c
);
2215 if (!(isnum(c
) || isid(c
) || c
== '.' ||
2216 ((c
== '+' || c
== '-') &&
2217 (t
== 'e' || t
== 'E' || t
== 'p' || t
== 'P'))))
2220 /* We add a trailing '\0' to ease parsing */
2221 cstr_ccat(&tokcstr
, '\0');
2222 tokc
.cstr
= &tokcstr
;
2226 /* special dot handling because it can also start a number */
2229 cstr_reset(&tokcstr
);
2230 cstr_ccat(&tokcstr
, '.');
2232 } else if (c
== '.') {
2252 /* parse the string */
2254 p
= parse_pp_string(p
, sep
, &str
);
2255 cstr_ccat(&str
, '\0');
2257 /* eval the escape (should be done as TOK_PPNUM) */
2258 cstr_reset(&tokcstr
);
2259 parse_escape_string(&tokcstr
, str
.data
, is_long
);
2264 /* XXX: make it portable */
2268 char_size
= sizeof(nwchar_t
);
2269 if (tokcstr
.size
<= char_size
)
2270 tcc_error("empty character constant");
2271 if (tokcstr
.size
> 2 * char_size
)
2272 tcc_warning("multi-character character constant");
2274 tokc
.i
= *(int8_t *)tokcstr
.data
;
2277 tokc
.i
= *(nwchar_t
*)tokcstr
.data
;
2281 tokc
.cstr
= &tokcstr
;
2295 } else if (c
== '<') {
2313 } else if (c
== '>') {
2331 } else if (c
== '=') {
2344 } else if (c
== '=') {
2357 } else if (c
== '=') {
2370 } else if (c
== '=') {
2373 } else if (c
== '>') {
2381 PARSE2('!', '!', '=', TOK_NE
)
2382 PARSE2('=', '=', '=', TOK_EQ
)
2383 PARSE2('*', '*', '=', TOK_A_MUL
)
2384 PARSE2('%', '%', '=', TOK_A_MOD
)
2385 PARSE2('^', '^', '=', TOK_A_XOR
)
2387 /* comments or operator */
2391 p
= parse_comment(p
);
2392 /* comments replaced by a blank */
2394 goto keep_tok_flags
;
2395 } else if (c
== '/') {
2396 p
= parse_line_comment(p
);
2398 goto keep_tok_flags
;
2399 } else if (c
== '=') {
2419 case '$': /* only used in assembler */
2420 case '@': /* dito */
2425 tcc_error("unrecognized character \\x%02x", c
);
2431 #if defined(PARSE_DEBUG)
2432 printf("token = %s\n", get_tok_str(tok
, &tokc
));
2436 /* return next token without macro substitution. Can read input from
2438 static void next_nomacro_spc(void)
2444 TOK_GET(&tok
, ¯o_ptr
, &tokc
);
2445 if (tok
== TOK_LINENUM
) {
2446 file
->line_num
= tokc
.i
;
2455 ST_FUNC
void next_nomacro(void)
2459 } while (is_space(tok
));
2462 /* substitute arguments in replacement lists in macro_str by the values in
2463 args (field d) and return allocated string */
2464 static int *macro_arg_subst(Sym
**nested_list
, const int *macro_str
, Sym
*args
)
2466 int last_tok
, t
, spc
;
2476 TOK_GET(&t
, ¯o_str
, &cval
);
2481 TOK_GET(&t
, ¯o_str
, &cval
);
2484 s
= sym_find2(args
, t
);
2490 TOK_GET(&t
, &st
, &cval
);
2491 if (!check_space(t
, &spc
))
2492 cstr_cat(&cstr
, get_tok_str(t
, &cval
));
2495 cstr_ccat(&cstr
, '\0');
2497 printf("stringize: %s\n", (char *)cstr
.data
);
2501 tok_str_add2(&str
, TOK_STR
, &cval
);
2504 tok_str_add2(&str
, t
, &cval
);
2506 } else if (t
>= TOK_IDENT
) {
2507 s
= sym_find2(args
, t
);
2510 /* if '##' is present before or after, no arg substitution */
2511 if (*macro_str
== TOK_TWOSHARPS
|| last_tok
== TOK_TWOSHARPS
) {
2512 /* special case for var arg macros : ## eats the
2513 ',' if empty VA_ARGS variable. */
2514 /* XXX: test of the ',' is not 100%
2515 reliable. should fix it to avoid security
2517 if (gnu_ext
&& s
->type
.t
&&
2518 last_tok
== TOK_TWOSHARPS
&&
2519 str
.len
>= 2 && str
.str
[str
.len
- 2] == ',') {
2520 if (*st
== TOK_PLCHLDR
) {
2521 /* suppress ',' '##' */
2524 /* suppress '##' and add variable */
2532 TOK_GET(&t1
, &st
, &cval
);
2535 tok_str_add2(&str
, t1
, &cval
);
2539 /* NOTE: the stream cannot be read when macro
2540 substituing an argument */
2541 macro_subst(&str
, nested_list
, st
, NULL
);
2544 tok_str_add(&str
, t
);
2547 tok_str_add2(&str
, t
, &cval
);
2551 tok_str_add(&str
, 0);
2555 static char const ab_month_name
[12][4] =
2557 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2558 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
2561 /* do macro substitution of current token with macro 's' and add
2562 result to (tok_str,tok_len). 'nested_list' is the list of all
2563 macros we got inside to avoid recursing. Return non zero if no
2564 substitution needs to be done */
2565 static int macro_subst_tok(TokenString
*tok_str
,
2566 Sym
**nested_list
, Sym
*s
, struct macro_level
**can_read_stream
)
2568 Sym
*args
, *sa
, *sa1
;
2569 int mstr_allocated
, parlevel
, *mstr
, t
, t1
, spc
;
2577 /* if symbol is a macro, prepare substitution */
2578 /* special macros */
2579 if (tok
== TOK___LINE__
) {
2580 snprintf(buf
, sizeof(buf
), "%d", file
->line_num
);
2584 } else if (tok
== TOK___FILE__
) {
2585 cstrval
= file
->filename
;
2587 } else if (tok
== TOK___DATE__
|| tok
== TOK___TIME__
) {
2592 tm
= localtime(&ti
);
2593 if (tok
== TOK___DATE__
) {
2594 snprintf(buf
, sizeof(buf
), "%s %2d %d",
2595 ab_month_name
[tm
->tm_mon
], tm
->tm_mday
, tm
->tm_year
+ 1900);
2597 snprintf(buf
, sizeof(buf
), "%02d:%02d:%02d",
2598 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
2605 cstr_cat(&cstr
, cstrval
);
2606 cstr_ccat(&cstr
, '\0');
2608 tok_str_add2(tok_str
, t1
, &cval
);
2613 if (s
->type
.t
== MACRO_FUNC
) {
2614 /* NOTE: we do not use next_nomacro to avoid eating the
2615 next token. XXX: find better solution */
2619 while (is_space(t
= *p
) || TOK_LINEFEED
== t
)
2621 if (t
== 0 && can_read_stream
) {
2622 /* end of macro stream: we must look at the token
2623 after in the file */
2624 struct macro_level
*ml
= *can_read_stream
;
2630 *can_read_stream
= ml
-> prev
;
2632 /* also, end of scope for nested defined symbol */
2633 (*nested_list
)->v
= -1;
2637 ch
= file
->buf_ptr
[0];
2638 while (is_space(ch
) || ch
== '\n' || ch
== '/')
2643 uint8_t *p
= file
->buf_ptr
;
2646 p
= parse_comment(p
);
2647 file
->buf_ptr
= p
- 1;
2648 } else if (c
== '/') {
2649 p
= parse_line_comment(p
);
2650 file
->buf_ptr
= p
- 1;
2658 if (t
!= '(') /* no macro subst */
2661 /* argument macro */
2666 /* NOTE: empty args are allowed, except if no args */
2668 /* handle '()' case */
2669 if (!args
&& !sa
&& tok
== ')')
2672 tcc_error("macro '%s' used with too many args",
2673 get_tok_str(s
->v
, 0));
2676 /* NOTE: non zero sa->t indicates VA_ARGS */
2677 while ((parlevel
> 0 ||
2679 (tok
!= ',' || sa
->type
.t
))) &&
2683 else if (tok
== ')')
2685 if (tok
== TOK_LINEFEED
)
2687 if (!check_space(tok
, &spc
))
2688 tok_str_add2(&str
, tok
, &tokc
);
2692 tok_str_add(&str
, TOK_PLCHLDR
);
2694 tok_str_add(&str
, 0);
2695 sa1
= sym_push2(&args
, sa
->v
& ~SYM_FIELD
, sa
->type
.t
, 0);
2699 /* special case for gcc var args: add an empty
2700 var arg argument if it is omitted */
2701 if (sa
&& sa
->type
.t
&& gnu_ext
)
2711 tcc_error("macro '%s' used with too few args",
2712 get_tok_str(s
->v
, 0));
2715 /* now subst each arg */
2716 mstr
= macro_arg_subst(nested_list
, mstr
, args
);
2721 tok_str_free(sa
->d
);
2727 sym_push2(nested_list
, s
->v
, 0, 0);
2728 macro_subst(tok_str
, nested_list
, mstr
, can_read_stream
);
2729 /* pop nested defined symbol */
2731 *nested_list
= sa1
->prev
;
2739 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
2740 return the resulting string (which must be freed). */
2741 static inline int *macro_twosharps(const int *macro_str
)
2745 TokenString macro_str1
;
2747 int n
, start_of_nosubsts
;
2749 /* we search the first '##' */
2750 for(ptr
= macro_str
;;) {
2752 TOK_GET(&t
, &ptr
, &cval
);
2753 if (t
== TOK_TWOSHARPS
)
2755 /* nothing more to do if end of string */
2760 /* we saw '##', so we need more processing to handle it */
2761 start_of_nosubsts
= -1;
2762 tok_str_new(¯o_str1
);
2763 for(ptr
= macro_str
;;) {
2764 TOK_GET(&tok
, &ptr
, &tokc
);
2767 if (tok
== TOK_TWOSHARPS
)
2769 if (tok
== TOK_NOSUBST
&& start_of_nosubsts
< 0)
2770 start_of_nosubsts
= macro_str1
.len
;
2771 while (*ptr
== TOK_TWOSHARPS
) {
2772 /* given 'a##b', remove nosubsts preceding 'a' */
2773 if (start_of_nosubsts
>= 0)
2774 macro_str1
.len
= start_of_nosubsts
;
2775 /* given 'a##b', skip '##' */
2777 /* given 'a##b', remove nosubsts preceding 'b' */
2778 while (t
== TOK_NOSUBST
)
2780 if (t
&& t
!= TOK_TWOSHARPS
) {
2782 TOK_GET(&t
, &ptr
, &cval
);
2783 /* We concatenate the two tokens */
2785 if (tok
!= TOK_PLCHLDR
)
2786 cstr_cat(&cstr
, get_tok_str(tok
, &tokc
));
2788 if (t
!= TOK_PLCHLDR
|| tok
== TOK_PLCHLDR
)
2789 cstr_cat(&cstr
, get_tok_str(t
, &cval
));
2790 cstr_ccat(&cstr
, '\0');
2792 tcc_open_bf(tcc_state
, ":paste:", cstr
.size
);
2793 memcpy(file
->buffer
, cstr
.data
, cstr
.size
);
2796 if (0 == *file
->buf_ptr
)
2798 tok_str_add2(¯o_str1
, tok
, &tokc
);
2799 tcc_warning("pasting \"%.*s\" and \"%s\" does not give a valid preprocessing token",
2800 n
, cstr
.data
, (char*)cstr
.data
+ n
);
2806 if (tok
!= TOK_NOSUBST
) {
2807 tok_str_add2(¯o_str1
, tok
, &tokc
);
2809 start_of_nosubsts
= -1;
2811 tok_str_add2(¯o_str1
, tok
, &tokc
);
2813 tok_str_add(¯o_str1
, 0);
2814 return macro_str1
.str
;
2818 /* do macro substitution of macro_str and add result to
2819 (tok_str,tok_len). 'nested_list' is the list of all macros we got
2820 inside to avoid recursing. */
2821 static void macro_subst(TokenString
*tok_str
, Sym
**nested_list
,
2822 const int *macro_str
, struct macro_level
** can_read_stream
)
2829 struct macro_level ml
;
2832 /* first scan for '##' operator handling */
2834 macro_str1
= macro_twosharps(ptr
);
2842 /* NOTE: ptr == NULL can only happen if tokens are read from
2843 file stream due to a macro function call */
2846 TOK_GET(&t
, &ptr
, &cval
);
2849 if (t
== TOK_NOSUBST
) {
2850 /* following token has already been subst'd. just copy it on */
2851 tok_str_add2(tok_str
, TOK_NOSUBST
, NULL
);
2852 TOK_GET(&t
, &ptr
, &cval
);
2857 /* if nested substitution, do nothing */
2858 if (sym_find2(*nested_list
, t
)) {
2859 /* and mark it as TOK_NOSUBST, so it doesn't get subst'd again */
2860 tok_str_add2(tok_str
, TOK_NOSUBST
, NULL
);
2864 if (can_read_stream
)
2865 ml
.prev
= *can_read_stream
, *can_read_stream
= &ml
;
2866 macro_ptr
= (int *)ptr
;
2868 ret
= macro_subst_tok(tok_str
, nested_list
, s
, can_read_stream
);
2869 ptr
= (int *)macro_ptr
;
2871 if (can_read_stream
&& *can_read_stream
== &ml
)
2872 *can_read_stream
= ml
.prev
;
2875 if (parse_flags
& PARSE_FLAG_SPACES
)
2880 tok_str_add(tok_str
, ' ');
2884 if (!check_space(t
, &spc
))
2885 tok_str_add2(tok_str
, t
, &cval
);
2889 tok_str_free(macro_str1
);
2892 /* return next token with macro substitution */
2893 ST_FUNC
void next(void)
2895 Sym
*nested_list
, *s
;
2897 struct macro_level
*ml
;
2900 if (parse_flags
& PARSE_FLAG_SPACES
)
2905 /* if not reading from macro substituted string, then try
2906 to substitute macros */
2907 if (tok
>= TOK_IDENT
&&
2908 (parse_flags
& PARSE_FLAG_PREPROCESS
)) {
2909 s
= define_find(tok
);
2911 /* we have a macro: we try to substitute */
2915 if (macro_subst_tok(&str
, &nested_list
, s
, &ml
) == 0) {
2916 /* substitution done, NOTE: maybe empty */
2917 tok_str_add(&str
, 0);
2918 macro_ptr
= str
.str
;
2919 macro_ptr_allocated
= str
.str
;
2926 /* end of macro or end of unget buffer */
2927 if (unget_buffer_enabled
) {
2928 macro_ptr
= unget_saved_macro_ptr
;
2929 unget_buffer_enabled
= 0;
2931 /* end of macro string: free it */
2932 tok_str_free(macro_ptr_allocated
);
2933 macro_ptr_allocated
= NULL
;
2937 } else if (tok
== TOK_NOSUBST
) {
2938 /* discard preprocessor's nosubst markers */
2943 /* convert preprocessor tokens into C tokens */
2944 if (tok
== TOK_PPNUM
&&
2945 (parse_flags
& PARSE_FLAG_TOK_NUM
)) {
2946 parse_number((char *)tokc
.cstr
->data
);
2950 /* push back current token and set current token to 'last_tok'. Only
2951 identifier case handled for labels. */
2952 ST_INLN
void unget_tok(int last_tok
)
2956 if (unget_buffer_enabled
)
2958 /* assert(macro_ptr == unget_saved_buffer + 1);
2959 assert(*macro_ptr == 0); */
2963 unget_saved_macro_ptr
= macro_ptr
;
2964 unget_buffer_enabled
= 1;
2966 q
= unget_saved_buffer
;
2969 n
= tok_ext_size(tok
) - 1;
2972 *q
= 0; /* end of token string */
2977 /* better than nothing, but needs extension to handle '-E' option
2979 ST_FUNC
void preprocess_init(TCCState
*s1
)
2981 s1
->include_stack_ptr
= s1
->include_stack
;
2982 /* XXX: move that before to avoid having to initialize
2983 file->ifdef_stack_ptr ? */
2984 s1
->ifdef_stack_ptr
= s1
->ifdef_stack
;
2985 file
->ifdef_stack_ptr
= s1
->ifdef_stack_ptr
;
2988 s1
->pack_stack
[0] = 0;
2989 s1
->pack_stack_ptr
= s1
->pack_stack
;
2992 ST_FUNC
void preprocess_new(void)
2997 /* init isid table */
2998 for(i
=CH_EOF
;i
<256;i
++)
2999 isidnum_table
[i
-CH_EOF
] = isid(i
) || isnum(i
);
3001 /* add all tokens */
3003 memset(hash_ident
, 0, TOK_HASH_SIZE
* sizeof(TokenSym
*));
3005 tok_ident
= TOK_IDENT
;
3014 tok_alloc(p
, r
- p
- 1);
3019 /* Preprocess the current file */
3020 ST_FUNC
int tcc_preprocess(TCCState
*s1
)
3024 BufferedFile
*file_ref
, **iptr
, **iptr_new
;
3025 int token_seen
, line_ref
, d
;
3028 preprocess_init(s1
);
3029 define_start
= define_stack
;
3030 ch
= file
->buf_ptr
[0];
3031 tok_flags
= TOK_FLAG_BOL
| TOK_FLAG_BOF
;
3032 parse_flags
= PARSE_FLAG_ASM_COMMENTS
| PARSE_FLAG_PREPROCESS
|
3033 PARSE_FLAG_LINEFEED
| PARSE_FLAG_SPACES
;
3037 iptr
= s1
->include_stack_ptr
;
3041 if (tok
== TOK_EOF
) {
3043 } else if (file
!= file_ref
) {
3045 } else if (tok
== TOK_LINEFEED
) {
3050 } else if (!token_seen
) {
3051 d
= file
->line_num
- line_ref
;
3052 if (file
!= file_ref
|| d
< 0 || d
>= 8) {
3054 iptr_new
= s1
->include_stack_ptr
;
3055 s
= iptr_new
> iptr
? " 1"
3056 : iptr_new
< iptr
? " 2"
3057 : iptr_new
> s1
->include_stack
? " 3"
3061 fprintf(s1
->ppfp
, "# %d \"%s\"%s\n", file
->line_num
, file
->filename
, s
);
3064 fputs("\n", s1
->ppfp
), --d
;
3066 line_ref
= (file_ref
= file
)->line_num
;
3067 token_seen
= tok
!= TOK_LINEFEED
;
3071 fputs(get_tok_str(tok
, &tokc
), s1
->ppfp
);
3073 free_defines(define_start
);