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
, &tokc
));
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;
749 else if (tok
== TOK_LINEFEED
)
764 /* ParseState handling */
766 /* XXX: currently, no include file info is stored. Thus, we cannot display
767 accurate messages if the function or data definition spans multiple
770 /* save current parse state in 's' */
771 ST_FUNC
void save_parse_state(ParseState
*s
)
773 s
->line_num
= file
->line_num
;
774 s
->macro_ptr
= macro_ptr
;
779 /* restore parse state from 's' */
780 ST_FUNC
void restore_parse_state(ParseState
*s
)
782 file
->line_num
= s
->line_num
;
783 macro_ptr
= s
->macro_ptr
;
788 /* return the number of additional 'ints' necessary to store the
790 static inline int tok_ext_size(int t
)
804 error("unsupported token");
811 return LDOUBLE_SIZE
/ 4;
817 /* token string handling */
819 ST_INLN
void tok_str_new(TokenString
*s
)
823 s
->allocated_len
= 0;
824 s
->last_line_num
= -1;
827 ST_FUNC
void tok_str_free(int *str
)
832 static int *tok_str_realloc(TokenString
*s
)
836 if (s
->allocated_len
== 0) {
839 len
= s
->allocated_len
* 2;
841 str
= tcc_realloc(s
->str
, len
* sizeof(int));
843 error("memory full");
844 s
->allocated_len
= len
;
849 ST_FUNC
void tok_str_add(TokenString
*s
, int t
)
855 if (len
>= s
->allocated_len
)
856 str
= tok_str_realloc(s
);
861 static void tok_str_add2(TokenString
*s
, int t
, CValue
*cv
)
868 /* allocate space for worst case */
869 if (len
+ TOK_MAX_SIZE
> s
->allocated_len
)
870 str
= tok_str_realloc(s
);
879 str
[len
++] = cv
->tab
[0];
888 nb_words
= (sizeof(CString
) + cv
->cstr
->size
+ 3) >> 2;
889 while ((len
+ nb_words
) > s
->allocated_len
)
890 str
= tok_str_realloc(s
);
891 cstr
= (CString
*)(str
+ len
);
893 cstr
->size
= cv
->cstr
->size
;
894 cstr
->data_allocated
= NULL
;
895 cstr
->size_allocated
= cstr
->size
;
896 memcpy((char *)cstr
+ sizeof(CString
),
897 cv
->cstr
->data
, cstr
->size
);
904 #if LDOUBLE_SIZE == 8
907 str
[len
++] = cv
->tab
[0];
908 str
[len
++] = cv
->tab
[1];
910 #if LDOUBLE_SIZE == 12
912 str
[len
++] = cv
->tab
[0];
913 str
[len
++] = cv
->tab
[1];
914 str
[len
++] = cv
->tab
[2];
915 #elif LDOUBLE_SIZE == 16
917 str
[len
++] = cv
->tab
[0];
918 str
[len
++] = cv
->tab
[1];
919 str
[len
++] = cv
->tab
[2];
920 str
[len
++] = cv
->tab
[3];
921 #elif LDOUBLE_SIZE != 8
922 #error add long double size support
931 /* add the current parse token in token string 's' */
932 ST_FUNC
void tok_str_add_tok(TokenString
*s
)
936 /* save line number info */
937 if (file
->line_num
!= s
->last_line_num
) {
938 s
->last_line_num
= file
->line_num
;
939 cval
.i
= s
->last_line_num
;
940 tok_str_add2(s
, TOK_LINENUM
, &cval
);
942 tok_str_add2(s
, tok
, &tokc
);
945 /* get a token from an integer array and increment pointer
946 accordingly. we code it as a macro to avoid pointer aliasing. */
947 static inline void TOK_GET(int *t
, const int **pp
, CValue
*cv
)
965 cv
->cstr
= (CString
*)p
;
966 cv
->cstr
->data
= (char *)p
+ sizeof(CString
);
967 p
+= (sizeof(CString
) + cv
->cstr
->size
+ 3) >> 2;
975 #if LDOUBLE_SIZE == 16
977 #elif LDOUBLE_SIZE == 12
979 #elif LDOUBLE_SIZE == 8
982 # error add long double size support
995 static int macro_is_equal(const int *a
, const int *b
)
997 char buf
[STRING_MAX_SIZE
+ 1];
1001 TOK_GET(&t
, &a
, &cv
);
1002 pstrcpy(buf
, sizeof buf
, get_tok_str(t
, &cv
));
1003 TOK_GET(&t
, &b
, &cv
);
1004 if (strcmp(buf
, get_tok_str(t
, &cv
)))
1010 /* defines handling */
1011 ST_INLN
void define_push(int v
, int macro_type
, int *str
, Sym
*first_arg
)
1016 if (s
&& !macro_is_equal(s
->d
, str
))
1017 warning("%s redefined", get_tok_str(v
, NULL
));
1019 s
= sym_push2(&define_stack
, v
, macro_type
, 0);
1021 s
->next
= first_arg
;
1022 table_ident
[v
- TOK_IDENT
]->sym_define
= s
;
1025 /* undefined a define symbol. Its name is just set to zero */
1026 ST_FUNC
void define_undef(Sym
*s
)
1030 if (v
>= TOK_IDENT
&& v
< tok_ident
)
1031 table_ident
[v
- TOK_IDENT
]->sym_define
= NULL
;
1035 ST_INLN Sym
*define_find(int v
)
1038 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
1040 return table_ident
[v
]->sym_define
;
1043 /* free define stack until top reaches 'b' */
1044 ST_FUNC
void free_defines(Sym
*b
)
1052 /* do not free args or predefined defines */
1054 tok_str_free(top
->d
);
1056 if (v
>= TOK_IDENT
&& v
< tok_ident
)
1057 table_ident
[v
- TOK_IDENT
]->sym_define
= NULL
;
1065 ST_FUNC Sym
*label_find(int v
)
1068 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
1070 return table_ident
[v
]->sym_label
;
1073 ST_FUNC Sym
*label_push(Sym
**ptop
, int v
, int flags
)
1076 s
= sym_push2(ptop
, v
, 0, 0);
1078 ps
= &table_ident
[v
- TOK_IDENT
]->sym_label
;
1079 if (ptop
== &global_label_stack
) {
1080 /* modify the top most local identifier, so that
1081 sym_identifier will point to 's' when popped */
1083 ps
= &(*ps
)->prev_tok
;
1090 /* pop labels until element last is reached. Look if any labels are
1091 undefined. Define symbols if '&&label' was used. */
1092 ST_FUNC
void label_pop(Sym
**ptop
, Sym
*slast
)
1095 for(s
= *ptop
; s
!= slast
; s
= s1
) {
1097 if (s
->r
== LABEL_DECLARED
) {
1098 warning("label '%s' declared but not used", get_tok_str(s
->v
, NULL
));
1099 } else if (s
->r
== LABEL_FORWARD
) {
1100 error("label '%s' used but not defined",
1101 get_tok_str(s
->v
, NULL
));
1104 /* define corresponding symbol. A size of
1106 put_extern_sym(s
, cur_text_section
, s
->jnext
, 1);
1110 table_ident
[s
->v
- TOK_IDENT
]->sym_label
= s
->prev_tok
;
1116 /* eval an expression for #if/#elif */
1117 static int expr_preprocess(void)
1123 while (tok
!= TOK_LINEFEED
&& tok
!= TOK_EOF
) {
1124 next(); /* do macro subst */
1125 if (tok
== TOK_DEFINED
) {
1130 c
= define_find(tok
) != 0;
1135 } else if (tok
>= TOK_IDENT
) {
1136 /* if undefined macro */
1140 tok_str_add_tok(&str
);
1142 tok_str_add(&str
, -1); /* simulate end of file */
1143 tok_str_add(&str
, 0);
1144 /* now evaluate C constant expression */
1145 macro_ptr
= str
.str
;
1149 tok_str_free(str
.str
);
1153 #if defined(PARSE_DEBUG) || defined(PP_DEBUG)
1154 static void tok_print(int *str
)
1161 TOK_GET(&t
, &str
, &cval
);
1164 printf("%s", get_tok_str(t
, &cval
));
1170 /* parse after #define */
1171 ST_FUNC
void parse_define(void)
1173 Sym
*s
, *first
, **ps
;
1174 int v
, t
, varg
, is_vaargs
, spc
;
1179 error("invalid macro name '%s'", get_tok_str(tok
, &tokc
));
1180 /* XXX: should check if same macro (ANSI) */
1183 /* '(' must be just after macro definition for MACRO_FUNC */
1188 while (tok
!= ')') {
1192 if (varg
== TOK_DOTS
) {
1193 varg
= TOK___VA_ARGS__
;
1195 } else if (tok
== TOK_DOTS
&& gnu_ext
) {
1199 if (varg
< TOK_IDENT
)
1200 error("badly punctuated parameter list");
1201 s
= sym_push2(&define_stack
, varg
| SYM_FIELD
, is_vaargs
, 0);
1214 /* EOF testing necessary for '-D' handling */
1215 while (tok
!= TOK_LINEFEED
&& tok
!= TOK_EOF
) {
1216 /* remove spaces around ## and after '#' */
1217 if (TOK_TWOSHARPS
== tok
) {
1221 } else if ('#' == tok
) {
1223 } else if (check_space(tok
, &spc
)) {
1226 tok_str_add2(&str
, tok
, &tokc
);
1231 --str
.len
; /* remove trailing space */
1232 tok_str_add(&str
, 0);
1234 printf("define %s %d: ", get_tok_str(v
, NULL
), t
);
1237 define_push(v
, t
, str
.str
, first
);
1240 static inline int hash_cached_include(int type
, const char *filename
)
1242 const unsigned char *s
;
1246 h
= TOK_HASH_FUNC(h
, type
);
1249 h
= TOK_HASH_FUNC(h
, *s
);
1252 h
&= (CACHED_INCLUDES_HASH_SIZE
- 1);
1256 /* XXX: use a token or a hash table to accelerate matching ? */
1257 static CachedInclude
*search_cached_include(TCCState
*s1
,
1258 int type
, const char *filename
)
1262 h
= hash_cached_include(type
, filename
);
1263 i
= s1
->cached_includes_hash
[h
];
1267 e
= s1
->cached_includes
[i
- 1];
1268 if (e
->type
== type
&& !PATHCMP(e
->filename
, filename
))
1275 static inline void add_cached_include(TCCState
*s1
, int type
,
1276 const char *filename
, int ifndef_macro
)
1281 if (search_cached_include(s1
, type
, filename
))
1284 printf("adding cached '%s' %s\n", filename
, get_tok_str(ifndef_macro
, NULL
));
1286 e
= tcc_malloc(sizeof(CachedInclude
) + strlen(filename
));
1290 strcpy(e
->filename
, filename
);
1291 e
->ifndef_macro
= ifndef_macro
;
1292 dynarray_add((void ***)&s1
->cached_includes
, &s1
->nb_cached_includes
, e
);
1293 /* add in hash table */
1294 h
= hash_cached_include(type
, filename
);
1295 e
->hash_next
= s1
->cached_includes_hash
[h
];
1296 s1
->cached_includes_hash
[h
] = s1
->nb_cached_includes
;
1299 static void pragma_parse(TCCState
*s1
)
1304 if (tok
== TOK_pack
) {
1307 #pragma pack(1) // set
1308 #pragma pack() // reset to default
1309 #pragma pack(push,1) // push & set
1310 #pragma pack(pop) // restore previous
1314 if (tok
== TOK_ASM_pop
) {
1316 if (s1
->pack_stack_ptr
<= s1
->pack_stack
) {
1318 error("out of pack stack");
1320 s1
->pack_stack_ptr
--;
1324 if (tok
== TOK_ASM_push
) {
1326 if (s1
->pack_stack_ptr
>= s1
->pack_stack
+ PACK_STACK_SIZE
- 1)
1328 s1
->pack_stack_ptr
++;
1331 if (tok
!= TOK_CINT
) {
1333 error("invalid pack pragma");
1336 if (val
< 1 || val
> 16 || (val
& (val
- 1)) != 0)
1340 *s1
->pack_stack_ptr
= val
;
1346 /* is_bof is true if first non space token at beginning of file */
1347 ST_FUNC
void preprocess(int is_bof
)
1349 TCCState
*s1
= tcc_state
;
1350 int i
, c
, n
, saved_parse_flags
;
1354 saved_parse_flags
= parse_flags
;
1355 parse_flags
= PARSE_FLAG_PREPROCESS
| PARSE_FLAG_TOK_NUM
|
1356 PARSE_FLAG_LINEFEED
;
1366 s
= define_find(tok
);
1367 /* undefine symbol by putting an invalid name */
1372 case TOK_INCLUDE_NEXT
:
1373 ch
= file
->buf_ptr
[0];
1374 /* XXX: incorrect if comments : use next_nomacro with a special mode */
1379 } else if (ch
== '\"') {
1384 while (ch
!= c
&& ch
!= '\n' && ch
!= CH_EOF
) {
1385 if ((q
- buf
) < sizeof(buf
) - 1)
1388 if (handle_stray_noerror() == 0)
1396 /* eat all spaces and comments after include */
1397 /* XXX: slightly incorrect */
1398 while (ch1
!= '\n' && ch1
!= CH_EOF
)
1402 /* computed #include : either we have only strings or
1403 we have anything enclosed in '<>' */
1406 if (tok
== TOK_STR
) {
1407 while (tok
!= TOK_LINEFEED
) {
1408 if (tok
!= TOK_STR
) {
1410 error("'#include' expects \"FILENAME\" or <FILENAME>");
1412 pstrcat(buf
, sizeof(buf
), (char *)tokc
.cstr
->data
);
1418 while (tok
!= TOK_LINEFEED
) {
1419 pstrcat(buf
, sizeof(buf
), get_tok_str(tok
, &tokc
));
1423 /* check syntax and remove '<>' */
1424 if (len
< 2 || buf
[0] != '<' || buf
[len
- 1] != '>')
1425 goto include_syntax
;
1426 memmove(buf
, buf
+ 1, len
- 2);
1427 buf
[len
- 2] = '\0';
1432 if (s1
->include_stack_ptr
>= s1
->include_stack
+ INCLUDE_STACK_SIZE
)
1433 error("#include recursion too deep");
1435 n
= s1
->nb_include_paths
+ s1
->nb_sysinclude_paths
;
1436 for (i
= -2; i
< n
; ++i
) {
1437 char buf1
[sizeof file
->filename
];
1443 /* check absolute include path */
1444 if (!IS_ABSPATH(buf
))
1448 } else if (i
== -1) {
1449 /* search in current dir if "header.h" */
1452 size
= tcc_basename(file
->filename
) - file
->filename
;
1453 memcpy(buf1
, file
->filename
, size
);
1457 /* search in all the include paths */
1458 if (i
< s1
->nb_include_paths
)
1459 path
= s1
->include_paths
[i
];
1461 path
= s1
->sysinclude_paths
[i
- s1
->nb_include_paths
];
1462 pstrcpy(buf1
, sizeof(buf1
), path
);
1463 pstrcat(buf1
, sizeof(buf1
), "/");
1466 pstrcat(buf1
, sizeof(buf1
), buf
);
1468 e
= search_cached_include(s1
, c
, buf1
);
1469 if (e
&& define_find(e
->ifndef_macro
)) {
1470 /* no need to parse the include because the 'ifndef macro'
1473 printf("%s: skipping %s\n", file
->filename
, buf
);
1477 fd
= tcc_open(s1
, buf1
);
1482 if (tok
== TOK_INCLUDE_NEXT
) {
1493 printf("%s: including %s\n", file
->filename
, buf1
);
1495 /* update target deps */
1496 dynarray_add((void ***)&s1
->target_deps
, &s1
->nb_target_deps
,
1498 /* XXX: fix current line init */
1499 /* push current file in stack */
1500 *s1
->include_stack_ptr
++ = file
->prev
;
1502 pstrcpy(file
->inc_filename
, sizeof(file
->inc_filename
), buf1
);
1503 /* add include file debug info */
1505 put_stabs(file
->filename
, N_BINCL
, 0, 0, 0);
1506 tok_flags
|= TOK_FLAG_BOF
| TOK_FLAG_BOL
;
1507 ch
= file
->buf_ptr
[0];
1510 error("include file '%s' not found", buf
);
1517 c
= expr_preprocess();
1523 if (tok
< TOK_IDENT
)
1524 error("invalid argument for '#if%sdef'", c
? "n" : "");
1528 printf("#ifndef %s\n", get_tok_str(tok
, NULL
));
1530 file
->ifndef_macro
= tok
;
1533 c
= (define_find(tok
) != 0) ^ c
;
1535 if (s1
->ifdef_stack_ptr
>= s1
->ifdef_stack
+ IFDEF_STACK_SIZE
)
1536 error("memory full");
1537 *s1
->ifdef_stack_ptr
++ = c
;
1540 if (s1
->ifdef_stack_ptr
== s1
->ifdef_stack
)
1541 error("#else without matching #if");
1542 if (s1
->ifdef_stack_ptr
[-1] & 2)
1543 error("#else after #else");
1544 c
= (s1
->ifdef_stack_ptr
[-1] ^= 3);
1547 if (s1
->ifdef_stack_ptr
== s1
->ifdef_stack
)
1548 error("#elif without matching #if");
1549 c
= s1
->ifdef_stack_ptr
[-1];
1551 error("#elif after #else");
1552 /* last #if/#elif expression was true: we skip */
1555 c
= expr_preprocess();
1556 s1
->ifdef_stack_ptr
[-1] = c
;
1558 if (s1
->ifdef_stack_ptr
== file
->ifdef_stack_ptr
+ 1)
1559 file
->ifndef_macro
= 0;
1569 if (s1
->ifdef_stack_ptr
<= file
->ifdef_stack_ptr
)
1570 error("#endif without matching #if");
1571 s1
->ifdef_stack_ptr
--;
1572 /* '#ifndef macro' was at the start of file. Now we check if
1573 an '#endif' is exactly at the end of file */
1574 if (file
->ifndef_macro
&&
1575 s1
->ifdef_stack_ptr
== file
->ifdef_stack_ptr
) {
1576 file
->ifndef_macro_saved
= file
->ifndef_macro
;
1577 /* need to set to zero to avoid false matches if another
1578 #ifndef at middle of file */
1579 file
->ifndef_macro
= 0;
1580 while (tok
!= TOK_LINEFEED
)
1582 tok_flags
|= TOK_FLAG_ENDIF
;
1588 if (tok
!= TOK_CINT
)
1590 file
->line_num
= tokc
.i
- 1; /* the line number will be incremented after */
1592 if (tok
!= TOK_LINEFEED
) {
1595 pstrcpy(file
->filename
, sizeof(file
->filename
),
1596 (char *)tokc
.cstr
->data
);
1602 ch
= file
->buf_ptr
[0];
1605 while (ch
!= '\n' && ch
!= CH_EOF
) {
1606 if ((q
- buf
) < sizeof(buf
) - 1)
1609 if (handle_stray_noerror() == 0)
1616 error("#error %s", buf
);
1618 warning("#warning %s", buf
);
1624 if (tok
== TOK_LINEFEED
|| tok
== '!' || tok
== TOK_PPNUM
) {
1625 /* '!' is ignored to allow C scripts. numbers are ignored
1626 to emulate cpp behaviour */
1628 if (!(saved_parse_flags
& PARSE_FLAG_ASM_COMMENTS
))
1629 warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok
, &tokc
));
1631 /* this is a gas line comment in an 'S' file. */
1632 file
->buf_ptr
= parse_line_comment(file
->buf_ptr
);
1638 /* ignore other preprocess commands or #! for C scripts */
1639 while (tok
!= TOK_LINEFEED
)
1642 parse_flags
= saved_parse_flags
;
1645 /* evaluate escape codes in a string. */
1646 static void parse_escape_string(CString
*outstr
, const uint8_t *buf
, int is_long
)
1661 case '0': case '1': case '2': case '3':
1662 case '4': case '5': case '6': case '7':
1663 /* at most three octal digits */
1668 n
= n
* 8 + c
- '0';
1672 n
= n
* 8 + c
- '0';
1677 goto add_char_nonext
;
1685 if (c
>= 'a' && c
<= 'f')
1687 else if (c
>= 'A' && c
<= 'F')
1697 goto add_char_nonext
;
1721 goto invalid_escape
;
1731 if (c
>= '!' && c
<= '~')
1732 warning("unknown escape sequence: \'\\%c\'", c
);
1734 warning("unknown escape sequence: \'\\x%x\'", c
);
1741 cstr_ccat(outstr
, c
);
1743 cstr_wccat(outstr
, c
);
1745 /* add a trailing '\0' */
1747 cstr_ccat(outstr
, '\0');
1749 cstr_wccat(outstr
, '\0');
1752 /* we use 64 bit numbers */
1755 /* bn = (bn << shift) | or_val */
1756 static void bn_lshift(unsigned int *bn
, int shift
, int or_val
)
1760 for(i
=0;i
<BN_SIZE
;i
++) {
1762 bn
[i
] = (v
<< shift
) | or_val
;
1763 or_val
= v
>> (32 - shift
);
1767 static void bn_zero(unsigned int *bn
)
1770 for(i
=0;i
<BN_SIZE
;i
++) {
1775 /* parse number in null terminated string 'p' and return it in the
1777 static void parse_number(const char *p
)
1779 int b
, t
, shift
, frac_bits
, s
, exp_val
, ch
;
1781 unsigned int bn
[BN_SIZE
];
1792 goto float_frac_parse
;
1793 } else if (t
== '0') {
1794 if (ch
== 'x' || ch
== 'X') {
1798 } else if (tcc_ext
&& (ch
== 'b' || ch
== 'B')) {
1804 /* parse all digits. cannot check octal numbers at this stage
1805 because of floating point constants */
1807 if (ch
>= 'a' && ch
<= 'f')
1809 else if (ch
>= 'A' && ch
<= 'F')
1817 if (q
>= token_buf
+ STRING_MAX_SIZE
) {
1819 error("number too long");
1825 ((ch
== 'e' || ch
== 'E') && b
== 10) ||
1826 ((ch
== 'p' || ch
== 'P') && (b
== 16 || b
== 2))) {
1828 /* NOTE: strtox should support that for hexa numbers, but
1829 non ISOC99 libcs do not support it, so we prefer to do
1831 /* hexadecimal or binary floats */
1832 /* XXX: handle overflows */
1844 } else if (t
>= 'a') {
1846 } else if (t
>= 'A') {
1851 bn_lshift(bn
, shift
, t
);
1858 if (t
>= 'a' && t
<= 'f') {
1860 } else if (t
>= 'A' && t
<= 'F') {
1862 } else if (t
>= '0' && t
<= '9') {
1868 error("invalid digit");
1869 bn_lshift(bn
, shift
, t
);
1874 if (ch
!= 'p' && ch
!= 'P')
1881 } else if (ch
== '-') {
1885 if (ch
< '0' || ch
> '9')
1886 expect("exponent digits");
1887 while (ch
>= '0' && ch
<= '9') {
1888 exp_val
= exp_val
* 10 + ch
- '0';
1891 exp_val
= exp_val
* s
;
1893 /* now we can generate the number */
1894 /* XXX: should patch directly float number */
1895 d
= (double)bn
[1] * 4294967296.0 + (double)bn
[0];
1896 d
= ldexp(d
, exp_val
- frac_bits
);
1901 /* float : should handle overflow */
1903 } else if (t
== 'L') {
1905 #ifdef TCC_TARGET_PE
1910 /* XXX: not large enough */
1911 tokc
.ld
= (long double)d
;
1918 /* decimal floats */
1920 if (q
>= token_buf
+ STRING_MAX_SIZE
)
1925 while (ch
>= '0' && ch
<= '9') {
1926 if (q
>= token_buf
+ STRING_MAX_SIZE
)
1932 if (ch
== 'e' || ch
== 'E') {
1933 if (q
>= token_buf
+ STRING_MAX_SIZE
)
1937 if (ch
== '-' || ch
== '+') {
1938 if (q
>= token_buf
+ STRING_MAX_SIZE
)
1943 if (ch
< '0' || ch
> '9')
1944 expect("exponent digits");
1945 while (ch
>= '0' && ch
<= '9') {
1946 if (q
>= token_buf
+ STRING_MAX_SIZE
)
1958 tokc
.f
= strtof(token_buf
, NULL
);
1959 } else if (t
== 'L') {
1961 #ifdef TCC_TARGET_PE
1963 tokc
.d
= strtod(token_buf
, NULL
);
1966 tokc
.ld
= strtold(token_buf
, NULL
);
1970 tokc
.d
= strtod(token_buf
, NULL
);
1974 unsigned long long n
, n1
;
1977 /* integer number */
1980 if (b
== 10 && *q
== '0') {
1987 /* no need for checks except for base 10 / 8 errors */
1990 } else if (t
>= 'a') {
1992 } else if (t
>= 'A') {
1997 error("invalid digit");
2001 /* detect overflow */
2002 /* XXX: this test is not reliable */
2004 error("integer constant overflow");
2007 /* XXX: not exactly ANSI compliant */
2008 if ((n
& 0xffffffff00000000LL
) != 0) {
2013 } else if (n
> 0x7fffffff) {
2024 error("three 'l's in integer constant");
2027 if (tok
== TOK_CINT
)
2029 else if (tok
== TOK_CUINT
)
2033 } else if (t
== 'U') {
2035 error("two 'u's in integer constant");
2037 if (tok
== TOK_CINT
)
2039 else if (tok
== TOK_CLLONG
)
2046 if (tok
== TOK_CINT
|| tok
== TOK_CUINT
)
2052 error("invalid number\n");
2056 #define PARSE2(c1, tok1, c2, tok2) \
2067 /* return next token without macro substitution */
2068 static inline void next_nomacro1(void)
2083 goto keep_tok_flags
;
2090 /* first look if it is in fact an end of buffer */
2091 if (p
>= file
->buf_end
) {
2095 if (p
>= file
->buf_end
)
2108 TCCState
*s1
= tcc_state
;
2109 if ((parse_flags
& PARSE_FLAG_LINEFEED
)
2110 && !(tok_flags
& TOK_FLAG_EOF
)) {
2111 tok_flags
|= TOK_FLAG_EOF
;
2113 goto keep_tok_flags
;
2114 } else if (!(parse_flags
& PARSE_FLAG_PREPROCESS
)) {
2116 } else if (s1
->ifdef_stack_ptr
!= file
->ifdef_stack_ptr
) {
2117 error("missing #endif");
2118 } else if (s1
->include_stack_ptr
== s1
->include_stack
) {
2119 /* no include left : end of file. */
2122 tok_flags
&= ~TOK_FLAG_EOF
;
2123 /* pop include file */
2125 /* test if previous '#endif' was after a #ifdef at
2127 if (tok_flags
& TOK_FLAG_ENDIF
) {
2129 printf("#endif %s\n", get_tok_str(file
->ifndef_macro_saved
, NULL
));
2131 add_cached_include(s1
, file
->inc_type
, file
->inc_filename
,
2132 file
->ifndef_macro_saved
);
2135 /* add end of include file debug info */
2136 if (tcc_state
->do_debug
) {
2137 put_stabd(N_EINCL
, 0, 0);
2139 /* pop include stack */
2141 s1
->include_stack_ptr
--;
2150 tok_flags
|= TOK_FLAG_BOL
;
2153 if (0 == (parse_flags
& PARSE_FLAG_LINEFEED
))
2156 goto keep_tok_flags
;
2161 if ((tok_flags
& TOK_FLAG_BOL
) &&
2162 (parse_flags
& PARSE_FLAG_PREPROCESS
)) {
2164 preprocess(tok_flags
& TOK_FLAG_BOF
);
2170 tok
= TOK_TWOSHARPS
;
2172 if (parse_flags
& PARSE_FLAG_ASM_COMMENTS
) {
2173 p
= parse_line_comment(p
- 1);
2182 case 'a': case 'b': case 'c': case 'd':
2183 case 'e': case 'f': case 'g': case 'h':
2184 case 'i': case 'j': case 'k': case 'l':
2185 case 'm': case 'n': case 'o': case 'p':
2186 case 'q': case 'r': case 's': case 't':
2187 case 'u': case 'v': case 'w': case 'x':
2189 case 'A': case 'B': case 'C': case 'D':
2190 case 'E': case 'F': case 'G': case 'H':
2191 case 'I': case 'J': case 'K':
2192 case 'M': case 'N': case 'O': case 'P':
2193 case 'Q': case 'R': case 'S': case 'T':
2194 case 'U': case 'V': case 'W': case 'X':
2200 h
= TOK_HASH_FUNC(h
, c
);
2204 if (!isidnum_table
[c
-CH_EOF
])
2206 h
= TOK_HASH_FUNC(h
, c
);
2213 /* fast case : no stray found, so we have the full token
2214 and we have already hashed it */
2216 h
&= (TOK_HASH_SIZE
- 1);
2217 pts
= &hash_ident
[h
];
2222 if (ts
->len
== len
&& !memcmp(ts
->str
, p1
, len
))
2224 pts
= &(ts
->hash_next
);
2226 ts
= tok_alloc_new(pts
, p1
, len
);
2230 cstr_reset(&tokcstr
);
2233 cstr_ccat(&tokcstr
, *p1
);
2239 while (isidnum_table
[c
-CH_EOF
]) {
2240 cstr_ccat(&tokcstr
, c
);
2243 ts
= tok_alloc(tokcstr
.data
, tokcstr
.size
);
2249 if (t
!= '\\' && t
!= '\'' && t
!= '\"') {
2251 goto parse_ident_fast
;
2254 if (c
== '\'' || c
== '\"') {
2258 cstr_reset(&tokcstr
);
2259 cstr_ccat(&tokcstr
, 'L');
2260 goto parse_ident_slow
;
2264 case '0': case '1': case '2': case '3':
2265 case '4': case '5': case '6': case '7':
2268 cstr_reset(&tokcstr
);
2269 /* after the first digit, accept digits, alpha, '.' or sign if
2270 prefixed by 'eEpP' */
2274 cstr_ccat(&tokcstr
, c
);
2276 if (!(isnum(c
) || isid(c
) || c
== '.' ||
2277 ((c
== '+' || c
== '-') &&
2278 (t
== 'e' || t
== 'E' || t
== 'p' || t
== 'P'))))
2281 /* We add a trailing '\0' to ease parsing */
2282 cstr_ccat(&tokcstr
, '\0');
2283 tokc
.cstr
= &tokcstr
;
2287 /* special dot handling because it can also start a number */
2290 cstr_reset(&tokcstr
);
2291 cstr_ccat(&tokcstr
, '.');
2293 } else if (c
== '.') {
2313 /* parse the string */
2315 p
= parse_pp_string(p
, sep
, &str
);
2316 cstr_ccat(&str
, '\0');
2318 /* eval the escape (should be done as TOK_PPNUM) */
2319 cstr_reset(&tokcstr
);
2320 parse_escape_string(&tokcstr
, str
.data
, is_long
);
2325 /* XXX: make it portable */
2329 char_size
= sizeof(nwchar_t
);
2330 if (tokcstr
.size
<= char_size
)
2331 error("empty character constant");
2332 if (tokcstr
.size
> 2 * char_size
)
2333 warning("multi-character character constant");
2335 tokc
.i
= *(int8_t *)tokcstr
.data
;
2338 tokc
.i
= *(nwchar_t
*)tokcstr
.data
;
2342 tokc
.cstr
= &tokcstr
;
2356 } else if (c
== '<') {
2374 } else if (c
== '>') {
2392 } else if (c
== '=') {
2405 } else if (c
== '=') {
2418 } else if (c
== '=') {
2431 } else if (c
== '=') {
2434 } else if (c
== '>') {
2442 PARSE2('!', '!', '=', TOK_NE
)
2443 PARSE2('=', '=', '=', TOK_EQ
)
2444 PARSE2('*', '*', '=', TOK_A_MUL
)
2445 PARSE2('%', '%', '=', TOK_A_MOD
)
2446 PARSE2('^', '^', '=', TOK_A_XOR
)
2448 /* comments or operator */
2452 p
= parse_comment(p
);
2453 /* comments replaced by a blank */
2455 goto keep_tok_flags
;
2456 } else if (c
== '/') {
2457 p
= parse_line_comment(p
);
2459 goto keep_tok_flags
;
2460 } else if (c
== '=') {
2480 case '$': /* only used in assembler */
2481 case '@': /* dito */
2486 error("unrecognized character \\x%02x", c
);
2492 #if defined(PARSE_DEBUG)
2493 printf("token = %s\n", get_tok_str(tok
, &tokc
));
2497 /* return next token without macro substitution. Can read input from
2499 static void next_nomacro_spc(void)
2505 TOK_GET(&tok
, ¯o_ptr
, &tokc
);
2506 if (tok
== TOK_LINENUM
) {
2507 file
->line_num
= tokc
.i
;
2516 ST_FUNC
void next_nomacro(void)
2520 } while (is_space(tok
));
2523 /* substitute args in macro_str and return allocated string */
2524 static int *macro_arg_subst(Sym
**nested_list
, const int *macro_str
, Sym
*args
)
2526 int last_tok
, t
, spc
;
2536 TOK_GET(&t
, ¯o_str
, &cval
);
2541 TOK_GET(&t
, ¯o_str
, &cval
);
2544 s
= sym_find2(args
, t
);
2550 TOK_GET(&t
, &st
, &cval
);
2551 if (!check_space(t
, &spc
))
2552 cstr_cat(&cstr
, get_tok_str(t
, &cval
));
2555 cstr_ccat(&cstr
, '\0');
2557 printf("stringize: %s\n", (char *)cstr
.data
);
2561 tok_str_add2(&str
, TOK_STR
, &cval
);
2564 tok_str_add2(&str
, t
, &cval
);
2566 } else if (t
>= TOK_IDENT
) {
2567 s
= sym_find2(args
, t
);
2570 /* if '##' is present before or after, no arg substitution */
2571 if (*macro_str
== TOK_TWOSHARPS
|| last_tok
== TOK_TWOSHARPS
) {
2572 /* special case for var arg macros : ## eats the
2573 ',' if empty VA_ARGS variable. */
2574 /* XXX: test of the ',' is not 100%
2575 reliable. should fix it to avoid security
2577 if (gnu_ext
&& s
->type
.t
&&
2578 last_tok
== TOK_TWOSHARPS
&&
2579 str
.len
>= 2 && str
.str
[str
.len
- 2] == ',') {
2581 /* suppress ',' '##' */
2584 /* suppress '##' and add variable */
2592 TOK_GET(&t1
, &st
, &cval
);
2595 tok_str_add2(&str
, t1
, &cval
);
2599 /* NOTE: the stream cannot be read when macro
2600 substituing an argument */
2601 macro_subst(&str
, nested_list
, st
, NULL
);
2604 tok_str_add(&str
, t
);
2607 tok_str_add2(&str
, t
, &cval
);
2611 tok_str_add(&str
, 0);
2615 static char const ab_month_name
[12][4] =
2617 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2618 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
2621 /* do macro substitution of current token with macro 's' and add
2622 result to (tok_str,tok_len). 'nested_list' is the list of all
2623 macros we got inside to avoid recursing. Return non zero if no
2624 substitution needs to be done */
2625 static int macro_subst_tok(TokenString
*tok_str
,
2626 Sym
**nested_list
, Sym
*s
, struct macro_level
**can_read_stream
)
2628 Sym
*args
, *sa
, *sa1
;
2629 int mstr_allocated
, parlevel
, *mstr
, t
, t1
, spc
;
2637 /* if symbol is a macro, prepare substitution */
2638 /* special macros */
2639 if (tok
== TOK___LINE__
) {
2640 snprintf(buf
, sizeof(buf
), "%d", file
->line_num
);
2644 } else if (tok
== TOK___FILE__
) {
2645 cstrval
= file
->filename
;
2647 } else if (tok
== TOK___DATE__
|| tok
== TOK___TIME__
) {
2652 tm
= localtime(&ti
);
2653 if (tok
== TOK___DATE__
) {
2654 snprintf(buf
, sizeof(buf
), "%s %2d %d",
2655 ab_month_name
[tm
->tm_mon
], tm
->tm_mday
, tm
->tm_year
+ 1900);
2657 snprintf(buf
, sizeof(buf
), "%02d:%02d:%02d",
2658 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
2665 cstr_cat(&cstr
, cstrval
);
2666 cstr_ccat(&cstr
, '\0');
2668 tok_str_add2(tok_str
, t1
, &cval
);
2673 if (s
->type
.t
== MACRO_FUNC
) {
2674 /* NOTE: we do not use next_nomacro to avoid eating the
2675 next token. XXX: find better solution */
2679 while (is_space(t
= *p
) || TOK_LINEFEED
== t
)
2681 if (t
== 0 && can_read_stream
) {
2682 /* end of macro stream: we must look at the token
2683 after in the file */
2684 struct macro_level
*ml
= *can_read_stream
;
2690 *can_read_stream
= ml
-> prev
;
2695 /* XXX: incorrect with comments */
2696 ch
= file
->buf_ptr
[0];
2697 while (is_space(ch
) || ch
== '\n')
2701 if (t
!= '(') /* no macro subst */
2704 /* argument macro */
2709 /* NOTE: empty args are allowed, except if no args */
2711 /* handle '()' case */
2712 if (!args
&& !sa
&& tok
== ')')
2715 error("macro '%s' used with too many args",
2716 get_tok_str(s
->v
, 0));
2719 /* NOTE: non zero sa->t indicates VA_ARGS */
2720 while ((parlevel
> 0 ||
2722 (tok
!= ',' || sa
->type
.t
))) &&
2726 else if (tok
== ')')
2728 if (tok
== TOK_LINEFEED
)
2730 if (!check_space(tok
, &spc
))
2731 tok_str_add2(&str
, tok
, &tokc
);
2735 tok_str_add(&str
, 0);
2736 sa1
= sym_push2(&args
, sa
->v
& ~SYM_FIELD
, sa
->type
.t
, 0);
2740 /* special case for gcc var args: add an empty
2741 var arg argument if it is omitted */
2742 if (sa
&& sa
->type
.t
&& gnu_ext
)
2752 error("macro '%s' used with too few args",
2753 get_tok_str(s
->v
, 0));
2756 /* now subst each arg */
2757 mstr
= macro_arg_subst(nested_list
, mstr
, args
);
2762 tok_str_free(sa
->d
);
2768 sym_push2(nested_list
, s
->v
, 0, 0);
2769 macro_subst(tok_str
, nested_list
, mstr
, can_read_stream
);
2770 /* pop nested defined symbol */
2772 *nested_list
= sa1
->prev
;
2780 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
2781 return the resulting string (which must be freed). */
2782 static inline int *macro_twosharps(const int *macro_str
)
2787 TokenString macro_str1
;
2791 /* we search the first '##' */
2792 for(ptr
= macro_str
;;) {
2793 TOK_GET(&t
, &ptr
, &cval
);
2794 if (t
== TOK_TWOSHARPS
)
2796 /* nothing more to do if end of string */
2801 /* we saw '##', so we need more processing to handle it */
2802 tok_str_new(¯o_str1
);
2803 for(ptr
= macro_str
;;) {
2804 TOK_GET(&tok
, &ptr
, &tokc
);
2807 if (tok
== TOK_TWOSHARPS
)
2809 while (*ptr
== TOK_TWOSHARPS
) {
2810 do { t
= *++ptr
; } while (t
== TOK_NOSUBST
);
2812 if (t
&& t
!= TOK_TWOSHARPS
) {
2813 TOK_GET(&t
, &ptr
, &cval
);
2815 /* We concatenate the two tokens */
2817 cstr_cat(&cstr
, get_tok_str(tok
, &tokc
));
2819 cstr_cat(&cstr
, get_tok_str(t
, &cval
));
2820 cstr_ccat(&cstr
, '\0');
2822 tcc_open_bf(tcc_state
, "<paste>", cstr
.size
);
2823 memcpy(file
->buffer
, cstr
.data
, cstr
.size
);
2826 if (0 == *file
->buf_ptr
)
2828 tok_str_add2(¯o_str1
, tok
, &tokc
);
2829 warning("pasting \"%.*s\" and \"%s\" does not give a valid preprocessing token",
2830 n
, cstr
.data
, (char*)cstr
.data
+ n
);
2836 tok_str_add2(¯o_str1
, tok
, &tokc
);
2838 tok_str_add(¯o_str1
, 0);
2839 return macro_str1
.str
;
2843 /* do macro substitution of macro_str and add result to
2844 (tok_str,tok_len). 'nested_list' is the list of all macros we got
2845 inside to avoid recursing. */
2846 static void macro_subst(TokenString
*tok_str
, Sym
**nested_list
,
2847 const int *macro_str
, struct macro_level
** can_read_stream
)
2854 struct macro_level ml
;
2857 /* first scan for '##' operator handling */
2859 macro_str1
= macro_twosharps(ptr
);
2867 /* NOTE: ptr == NULL can only happen if tokens are read from
2868 file stream due to a macro function call */
2871 TOK_GET(&t
, &ptr
, &cval
);
2874 if (t
== TOK_NOSUBST
) {
2875 /* following token has already been subst'd. just copy it on */
2876 tok_str_add2(tok_str
, TOK_NOSUBST
, NULL
);
2877 TOK_GET(&t
, &ptr
, &cval
);
2882 /* if nested substitution, do nothing */
2883 if (sym_find2(*nested_list
, t
)) {
2884 /* and mark it as TOK_NOSUBST, so it doesn't get subst'd again */
2885 tok_str_add2(tok_str
, TOK_NOSUBST
, NULL
);
2889 if (can_read_stream
)
2890 ml
.prev
= *can_read_stream
, *can_read_stream
= &ml
;
2891 macro_ptr
= (int *)ptr
;
2893 ret
= macro_subst_tok(tok_str
, nested_list
, s
, can_read_stream
);
2894 ptr
= (int *)macro_ptr
;
2896 if (can_read_stream
&& *can_read_stream
== &ml
)
2897 *can_read_stream
= ml
.prev
;
2900 if (parse_flags
& PARSE_FLAG_SPACES
)
2905 tok_str_add(tok_str
, ' ');
2909 if (!check_space(t
, &spc
))
2910 tok_str_add2(tok_str
, t
, &cval
);
2914 tok_str_free(macro_str1
);
2917 /* return next token with macro substitution */
2918 ST_FUNC
void next(void)
2920 Sym
*nested_list
, *s
;
2922 struct macro_level
*ml
;
2925 if (parse_flags
& PARSE_FLAG_SPACES
)
2930 /* if not reading from macro substituted string, then try
2931 to substitute macros */
2932 if (tok
>= TOK_IDENT
&&
2933 (parse_flags
& PARSE_FLAG_PREPROCESS
)) {
2934 s
= define_find(tok
);
2936 /* we have a macro: we try to substitute */
2940 if (macro_subst_tok(&str
, &nested_list
, s
, &ml
) == 0) {
2941 /* substitution done, NOTE: maybe empty */
2942 tok_str_add(&str
, 0);
2943 macro_ptr
= str
.str
;
2944 macro_ptr_allocated
= str
.str
;
2951 /* end of macro or end of unget buffer */
2952 if (unget_buffer_enabled
) {
2953 macro_ptr
= unget_saved_macro_ptr
;
2954 unget_buffer_enabled
= 0;
2956 /* end of macro string: free it */
2957 tok_str_free(macro_ptr_allocated
);
2958 macro_ptr_allocated
= NULL
;
2962 } else if (tok
== TOK_NOSUBST
) {
2963 /* discard preprocessor's nosubst markers */
2968 /* convert preprocessor tokens into C tokens */
2969 if (tok
== TOK_PPNUM
&&
2970 (parse_flags
& PARSE_FLAG_TOK_NUM
)) {
2971 parse_number((char *)tokc
.cstr
->data
);
2975 /* push back current token and set current token to 'last_tok'. Only
2976 identifier case handled for labels. */
2977 ST_INLN
void unget_tok(int last_tok
)
2981 unget_saved_macro_ptr
= macro_ptr
;
2982 unget_buffer_enabled
= 1;
2983 q
= unget_saved_buffer
;
2986 n
= tok_ext_size(tok
) - 1;
2989 *q
= 0; /* end of token string */
2994 /* better than nothing, but needs extension to handle '-E' option
2996 ST_FUNC
void preprocess_init(TCCState
*s1
)
2998 s1
->include_stack_ptr
= s1
->include_stack
;
2999 /* XXX: move that before to avoid having to initialize
3000 file->ifdef_stack_ptr ? */
3001 s1
->ifdef_stack_ptr
= s1
->ifdef_stack
;
3002 file
->ifdef_stack_ptr
= s1
->ifdef_stack_ptr
;
3004 /* XXX: not ANSI compliant: bound checking says error */
3006 s1
->pack_stack
[0] = 0;
3007 s1
->pack_stack_ptr
= s1
->pack_stack
;
3010 ST_FUNC
void preprocess_new()
3016 /* init isid table */
3017 for(i
=CH_EOF
;i
<256;i
++)
3018 isidnum_table
[i
-CH_EOF
] = isid(i
) || isnum(i
);
3020 /* add all tokens */
3022 memset(hash_ident
, 0, TOK_HASH_SIZE
* sizeof(TokenSym
*));
3024 tok_ident
= TOK_IDENT
;
3033 ts
= tok_alloc(p
, r
- p
- 1);
3038 /* Preprocess the current file */
3039 ST_FUNC
int tcc_preprocess(TCCState
*s1
)
3043 BufferedFile
*file_ref
, **iptr
, **iptr_new
;
3044 int token_seen
, line_ref
, d
;
3047 preprocess_init(s1
);
3048 define_start
= define_stack
;
3049 ch
= file
->buf_ptr
[0];
3050 tok_flags
= TOK_FLAG_BOL
| TOK_FLAG_BOF
;
3051 parse_flags
= PARSE_FLAG_ASM_COMMENTS
| PARSE_FLAG_PREPROCESS
|
3052 PARSE_FLAG_LINEFEED
| PARSE_FLAG_SPACES
;
3056 iptr
= s1
->include_stack_ptr
;
3060 if (tok
== TOK_EOF
) {
3062 } else if (file
!= file_ref
) {
3064 } else if (tok
== TOK_LINEFEED
) {
3069 } else if (!token_seen
) {
3070 d
= file
->line_num
- line_ref
;
3071 if (file
!= file_ref
|| d
< 0 || d
>= 8) {
3073 iptr_new
= s1
->include_stack_ptr
;
3074 s
= iptr_new
> iptr
? " 1"
3075 : iptr_new
< iptr
? " 2"
3076 : iptr_new
> s1
->include_stack
? " 3"
3080 fprintf(s1
->outfile
, "# %d \"%s\"%s\n", file
->line_num
, file
->filename
, s
);
3083 fputs("\n", s1
->outfile
), --d
;
3085 line_ref
= (file_ref
= file
)->line_num
;
3086 token_seen
= tok
!= TOK_LINEFEED
;
3090 fputs(get_tok_str(tok
, &tokc
), s1
->outfile
);
3092 free_defines(define_start
);