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 tcc_error("'%c' expected (got \"%s\")", c
, get_tok_str(tok
, &tokc
));
97 ST_FUNC
void expect(const char *msg
)
99 tcc_error("%s expected", msg
);
102 /* ------------------------------------------------------------------------- */
103 /* CString handling */
104 static void cstr_realloc(CString
*cstr
, int new_size
)
109 size
= cstr
->size_allocated
;
111 size
= 8; /* no need to allocate a too small first string */
112 while (size
< new_size
)
114 data
= tcc_realloc(cstr
->data_allocated
, size
);
115 cstr
->data_allocated
= data
;
116 cstr
->size_allocated
= size
;
121 PUB_FUNC
void cstr_ccat(CString
*cstr
, int ch
)
124 size
= cstr
->size
+ 1;
125 if (size
> cstr
->size_allocated
)
126 cstr_realloc(cstr
, size
);
127 ((unsigned char *)cstr
->data
)[size
- 1] = ch
;
131 PUB_FUNC
void cstr_cat(CString
*cstr
, const char *str
)
143 /* add a wide char */
144 PUB_FUNC
void cstr_wccat(CString
*cstr
, int ch
)
147 size
= cstr
->size
+ sizeof(nwchar_t
);
148 if (size
> cstr
->size_allocated
)
149 cstr_realloc(cstr
, size
);
150 *(nwchar_t
*)(((unsigned char *)cstr
->data
) + size
- sizeof(nwchar_t
)) = ch
;
154 PUB_FUNC
void cstr_new(CString
*cstr
)
156 memset(cstr
, 0, sizeof(CString
));
159 /* free string and reset it to NULL */
160 PUB_FUNC
void cstr_free(CString
*cstr
)
162 tcc_free(cstr
->data_allocated
);
167 static void add_char(CString
*cstr
, int c
)
169 if (c
== '\'' || c
== '\"' || c
== '\\') {
170 /* XXX: could be more precise if char or string */
171 cstr_ccat(cstr
, '\\');
173 if (c
>= 32 && c
<= 126) {
176 cstr_ccat(cstr
, '\\');
178 cstr_ccat(cstr
, 'n');
180 cstr_ccat(cstr
, '0' + ((c
>> 6) & 7));
181 cstr_ccat(cstr
, '0' + ((c
>> 3) & 7));
182 cstr_ccat(cstr
, '0' + (c
& 7));
187 /* ------------------------------------------------------------------------- */
188 /* allocate a new token */
189 static TokenSym
*tok_alloc_new(TokenSym
**pts
, const char *str
, int len
)
191 TokenSym
*ts
, **ptable
;
194 if (tok_ident
>= SYM_FIRST_ANOM
)
195 tcc_error("memory full");
197 /* expand token table if needed */
198 i
= tok_ident
- TOK_IDENT
;
199 if ((i
% TOK_ALLOC_INCR
) == 0) {
200 ptable
= tcc_realloc(table_ident
, (i
+ TOK_ALLOC_INCR
) * sizeof(TokenSym
*));
201 table_ident
= ptable
;
204 ts
= tcc_malloc(sizeof(TokenSym
) + len
);
206 ts
->tok
= tok_ident
++;
207 ts
->sym_define
= NULL
;
208 ts
->sym_label
= NULL
;
209 ts
->sym_struct
= NULL
;
210 ts
->sym_identifier
= NULL
;
212 ts
->hash_next
= NULL
;
213 memcpy(ts
->str
, str
, len
);
219 #define TOK_HASH_INIT 1
220 #define TOK_HASH_FUNC(h, c) ((h) * 263 + (c))
222 /* find a token and add it if not found */
223 ST_FUNC TokenSym
*tok_alloc(const char *str
, int len
)
231 h
= TOK_HASH_FUNC(h
, ((unsigned char *)str
)[i
]);
232 h
&= (TOK_HASH_SIZE
- 1);
234 pts
= &hash_ident
[h
];
239 if (ts
->len
== len
&& !memcmp(ts
->str
, str
, len
))
241 pts
= &(ts
->hash_next
);
243 return tok_alloc_new(pts
, str
, len
);
246 /* XXX: buffer overflow */
247 /* XXX: float tokens */
248 ST_FUNC
char *get_tok_str(int v
, CValue
*cv
)
250 static char buf
[STRING_MAX_SIZE
+ 1];
251 static CString cstr_buf
;
256 /* NOTE: to go faster, we give a fixed buffer for small strings */
257 cstr_reset(&cstr_buf
);
259 cstr_buf
.size_allocated
= sizeof(buf
);
265 /* XXX: not quite exact, but only useful for testing */
266 sprintf(p
, "%u", cv
->ui
);
270 /* XXX: not quite exact, but only useful for testing */
272 sprintf(p
, "%u", (unsigned)cv
->ull
);
274 sprintf(p
, "%Lu", cv
->ull
);
278 cstr_ccat(&cstr_buf
, 'L');
280 cstr_ccat(&cstr_buf
, '\'');
281 add_char(&cstr_buf
, cv
->i
);
282 cstr_ccat(&cstr_buf
, '\'');
283 cstr_ccat(&cstr_buf
, '\0');
287 len
= cstr
->size
- 1;
289 add_char(&cstr_buf
, ((unsigned char *)cstr
->data
)[i
]);
290 cstr_ccat(&cstr_buf
, '\0');
293 cstr_ccat(&cstr_buf
, 'L');
296 cstr_ccat(&cstr_buf
, '\"');
298 len
= cstr
->size
- 1;
300 add_char(&cstr_buf
, ((unsigned char *)cstr
->data
)[i
]);
302 len
= (cstr
->size
/ sizeof(nwchar_t
)) - 1;
304 add_char(&cstr_buf
, ((nwchar_t
*)cstr
->data
)[i
]);
306 cstr_ccat(&cstr_buf
, '\"');
307 cstr_ccat(&cstr_buf
, '\0');
316 return strcpy(p
, "...");
318 return strcpy(p
, "<<=");
320 return strcpy(p
, ">>=");
323 /* search in two bytes table */
324 const unsigned char *q
= tok_two_chars
;
337 } else if (v
< tok_ident
) {
338 return table_ident
[v
- TOK_IDENT
]->str
;
339 } else if (v
>= SYM_FIRST_ANOM
) {
340 /* special name for anonymous symbol */
341 sprintf(p
, "L.%u", v
- SYM_FIRST_ANOM
);
343 /* should never happen */
348 return cstr_buf
.data
;
351 /* fill input buffer and peek next char */
352 static int tcc_peekc_slow(BufferedFile
*bf
)
355 /* only tries to read if really end of buffer */
356 if (bf
->buf_ptr
>= bf
->buf_end
) {
358 #if defined(PARSE_DEBUG)
363 len
= read(bf
->fd
, bf
->buffer
, len
);
370 bf
->buf_ptr
= bf
->buffer
;
371 bf
->buf_end
= bf
->buffer
+ len
;
372 *bf
->buf_end
= CH_EOB
;
374 if (bf
->buf_ptr
< bf
->buf_end
) {
375 return bf
->buf_ptr
[0];
377 bf
->buf_ptr
= bf
->buf_end
;
382 /* return the current character, handling end of block if necessary
384 ST_FUNC
int handle_eob(void)
386 return tcc_peekc_slow(file
);
389 /* read next char from current input file and handle end of input buffer */
390 ST_INLN
void inp(void)
392 ch
= *(++(file
->buf_ptr
));
393 /* end of buffer/file handling */
398 /* handle '\[\r]\n' */
399 static int handle_stray_noerror(void)
406 } else if (ch
== '\r') {
420 static void handle_stray(void)
422 if (handle_stray_noerror())
423 tcc_error("stray '\\' in program");
426 /* skip the stray and handle the \\n case. Output an error if
427 incorrect char after the stray */
428 static int handle_stray1(uint8_t *p
)
432 if (p
>= file
->buf_end
) {
449 /* handle just the EOB case, but not stray */
450 #define PEEKC_EOB(c, p)\
461 /* handle the complicated stray case */
467 c = handle_stray1(p);\
472 /* input with '\[\r]\n' handling. Note that this function cannot
473 handle other characters after '\', so you cannot call it inside
474 strings or comments */
475 ST_FUNC
void minp(void)
483 /* single line C++ comments */
484 static uint8_t *parse_line_comment(uint8_t *p
)
492 if (c
== '\n' || c
== CH_EOF
) {
494 } else if (c
== '\\') {
503 } else if (c
== '\r') {
521 ST_FUNC
uint8_t *parse_comment(uint8_t *p
)
530 if (c
== '\n' || c
== '*' || c
== '\\')
534 if (c
== '\n' || c
== '*' || c
== '\\')
538 /* now we can handle all the cases */
542 } else if (c
== '*') {
548 } else if (c
== '/') {
550 } else if (c
== '\\') {
555 /* skip '\[\r]\n', otherwise just skip the stray */
561 } else if (c
== '\r') {
578 /* stray, eob or eof */
583 tcc_error("unexpected end of file in comment");
584 } else if (c
== '\\') {
596 static inline void skip_spaces(void)
602 static inline int check_space(int t
, int *spc
)
613 /* parse a string without interpreting escapes */
614 static uint8_t *parse_pp_string(uint8_t *p
,
615 int sep
, CString
*str
)
623 } else if (c
== '\\') {
629 /* XXX: indicate line number of start of string */
630 tcc_error("missing terminating %c character", sep
);
631 } else if (c
== '\\') {
632 /* escape : just skip \[\r]\n */
637 } else if (c
== '\r') {
640 expect("'\n' after '\r'");
643 } else if (c
== CH_EOF
) {
644 goto unterminated_string
;
647 cstr_ccat(str
, '\\');
653 } else if (c
== '\n') {
656 } else if (c
== '\r') {
660 cstr_ccat(str
, '\r');
676 /* skip block of text until #else, #elif or #endif. skip also pairs of
678 static void preprocess_skip(void)
680 int a
, start_of_line
, c
, in_warn_or_error
;
687 in_warn_or_error
= 0;
708 } else if (c
== '\\') {
709 ch
= file
->buf_ptr
[0];
710 handle_stray_noerror();
717 if (in_warn_or_error
)
719 p
= parse_pp_string(p
, c
, NULL
);
723 if (in_warn_or_error
)
730 p
= parse_comment(p
);
731 } else if (ch
== '/') {
732 p
= parse_line_comment(p
);
742 (tok
== TOK_ELSE
|| tok
== TOK_ELIF
|| tok
== TOK_ENDIF
))
744 if (tok
== TOK_IF
|| tok
== TOK_IFDEF
|| tok
== TOK_IFNDEF
)
746 else if (tok
== TOK_ENDIF
)
748 else if( tok
== TOK_ERROR
|| tok
== TOK_WARNING
)
749 in_warn_or_error
= 1;
750 else if (tok
== TOK_LINEFEED
)
765 /* ParseState handling */
767 /* XXX: currently, no include file info is stored. Thus, we cannot display
768 accurate messages if the function or data definition spans multiple
771 /* save current parse state in 's' */
772 ST_FUNC
void save_parse_state(ParseState
*s
)
774 s
->line_num
= file
->line_num
;
775 s
->macro_ptr
= macro_ptr
;
780 /* restore parse state from 's' */
781 ST_FUNC
void restore_parse_state(ParseState
*s
)
783 file
->line_num
= s
->line_num
;
784 macro_ptr
= s
->macro_ptr
;
789 /* return the number of additional 'ints' necessary to store the
791 static inline int tok_ext_size(int t
)
805 tcc_error("unsupported token");
812 return LDOUBLE_SIZE
/ 4;
818 /* token string handling */
820 ST_INLN
void tok_str_new(TokenString
*s
)
824 s
->allocated_len
= 0;
825 s
->last_line_num
= -1;
828 ST_FUNC
void tok_str_free(int *str
)
833 static int *tok_str_realloc(TokenString
*s
)
837 if (s
->allocated_len
== 0) {
840 len
= s
->allocated_len
* 2;
842 str
= tcc_realloc(s
->str
, len
* sizeof(int));
843 s
->allocated_len
= len
;
848 ST_FUNC
void tok_str_add(TokenString
*s
, int t
)
854 if (len
>= s
->allocated_len
)
855 str
= tok_str_realloc(s
);
860 static void tok_str_add2(TokenString
*s
, int t
, CValue
*cv
)
867 /* allocate space for worst case */
868 if (len
+ TOK_MAX_SIZE
> s
->allocated_len
)
869 str
= tok_str_realloc(s
);
878 str
[len
++] = cv
->tab
[0];
887 nb_words
= (sizeof(CString
) + cv
->cstr
->size
+ 3) >> 2;
888 while ((len
+ nb_words
) > s
->allocated_len
)
889 str
= tok_str_realloc(s
);
890 cstr
= (CString
*)(str
+ len
);
892 cstr
->size
= cv
->cstr
->size
;
893 cstr
->data_allocated
= NULL
;
894 cstr
->size_allocated
= cstr
->size
;
895 memcpy((char *)cstr
+ sizeof(CString
),
896 cv
->cstr
->data
, cstr
->size
);
903 #if LDOUBLE_SIZE == 8
906 str
[len
++] = cv
->tab
[0];
907 str
[len
++] = cv
->tab
[1];
909 #if LDOUBLE_SIZE == 12
911 str
[len
++] = cv
->tab
[0];
912 str
[len
++] = cv
->tab
[1];
913 str
[len
++] = cv
->tab
[2];
914 #elif LDOUBLE_SIZE == 16
916 str
[len
++] = cv
->tab
[0];
917 str
[len
++] = cv
->tab
[1];
918 str
[len
++] = cv
->tab
[2];
919 str
[len
++] = cv
->tab
[3];
920 #elif LDOUBLE_SIZE != 8
921 #error add long double size support
930 /* add the current parse token in token string 's' */
931 ST_FUNC
void tok_str_add_tok(TokenString
*s
)
935 /* save line number info */
936 if (file
->line_num
!= s
->last_line_num
) {
937 s
->last_line_num
= file
->line_num
;
938 cval
.i
= s
->last_line_num
;
939 tok_str_add2(s
, TOK_LINENUM
, &cval
);
941 tok_str_add2(s
, tok
, &tokc
);
944 /* get a token from an integer array and increment pointer
945 accordingly. we code it as a macro to avoid pointer aliasing. */
946 static inline void TOK_GET(int *t
, const int **pp
, CValue
*cv
)
964 cv
->cstr
= (CString
*)p
;
965 cv
->cstr
->data
= (char *)p
+ sizeof(CString
);
966 p
+= (sizeof(CString
) + cv
->cstr
->size
+ 3) >> 2;
974 #if LDOUBLE_SIZE == 16
976 #elif LDOUBLE_SIZE == 12
978 #elif LDOUBLE_SIZE == 8
981 # error add long double size support
994 static int macro_is_equal(const int *a
, const int *b
)
996 char buf
[STRING_MAX_SIZE
+ 1];
1000 TOK_GET(&t
, &a
, &cv
);
1001 pstrcpy(buf
, sizeof buf
, get_tok_str(t
, &cv
));
1002 TOK_GET(&t
, &b
, &cv
);
1003 if (strcmp(buf
, get_tok_str(t
, &cv
)))
1009 /* defines handling */
1010 ST_INLN
void define_push(int v
, int macro_type
, int *str
, Sym
*first_arg
)
1015 if (s
&& !macro_is_equal(s
->d
, str
))
1016 tcc_warning("%s redefined", get_tok_str(v
, NULL
));
1018 s
= sym_push2(&define_stack
, v
, macro_type
, 0);
1020 s
->next
= first_arg
;
1021 table_ident
[v
- TOK_IDENT
]->sym_define
= s
;
1024 /* undefined a define symbol. Its name is just set to zero */
1025 ST_FUNC
void define_undef(Sym
*s
)
1029 if (v
>= TOK_IDENT
&& v
< tok_ident
)
1030 table_ident
[v
- TOK_IDENT
]->sym_define
= NULL
;
1034 ST_INLN Sym
*define_find(int v
)
1037 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
1039 return table_ident
[v
]->sym_define
;
1042 /* free define stack until top reaches 'b' */
1043 ST_FUNC
void free_defines(Sym
*b
)
1051 /* do not free args or predefined defines */
1053 tok_str_free(top
->d
);
1055 if (v
>= TOK_IDENT
&& v
< tok_ident
)
1056 table_ident
[v
- TOK_IDENT
]->sym_define
= NULL
;
1064 ST_FUNC Sym
*label_find(int v
)
1067 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
1069 return table_ident
[v
]->sym_label
;
1072 ST_FUNC Sym
*label_push(Sym
**ptop
, int v
, int flags
)
1075 s
= sym_push2(ptop
, v
, 0, 0);
1077 ps
= &table_ident
[v
- TOK_IDENT
]->sym_label
;
1078 if (ptop
== &global_label_stack
) {
1079 /* modify the top most local identifier, so that
1080 sym_identifier will point to 's' when popped */
1082 ps
= &(*ps
)->prev_tok
;
1089 /* pop labels until element last is reached. Look if any labels are
1090 undefined. Define symbols if '&&label' was used. */
1091 ST_FUNC
void label_pop(Sym
**ptop
, Sym
*slast
)
1094 for(s
= *ptop
; s
!= slast
; s
= s1
) {
1096 if (s
->r
== LABEL_DECLARED
) {
1097 tcc_warning("label '%s' declared but not used", get_tok_str(s
->v
, NULL
));
1098 } else if (s
->r
== LABEL_FORWARD
) {
1099 tcc_error("label '%s' used but not defined",
1100 get_tok_str(s
->v
, NULL
));
1103 /* define corresponding symbol. A size of
1105 put_extern_sym(s
, cur_text_section
, s
->jnext
, 1);
1109 table_ident
[s
->v
- TOK_IDENT
]->sym_label
= s
->prev_tok
;
1115 /* eval an expression for #if/#elif */
1116 static int expr_preprocess(void)
1122 while (tok
!= TOK_LINEFEED
&& tok
!= TOK_EOF
) {
1123 next(); /* do macro subst */
1124 if (tok
== TOK_DEFINED
) {
1129 c
= define_find(tok
) != 0;
1134 } else if (tok
>= TOK_IDENT
) {
1135 /* if undefined macro */
1139 tok_str_add_tok(&str
);
1141 tok_str_add(&str
, -1); /* simulate end of file */
1142 tok_str_add(&str
, 0);
1143 /* now evaluate C constant expression */
1144 macro_ptr
= str
.str
;
1148 tok_str_free(str
.str
);
1152 #if defined(PARSE_DEBUG) || defined(PP_DEBUG)
1153 static void tok_print(int *str
)
1160 TOK_GET(&t
, &str
, &cval
);
1163 printf("%s", get_tok_str(t
, &cval
));
1169 /* parse after #define */
1170 ST_FUNC
void parse_define(void)
1172 Sym
*s
, *first
, **ps
;
1173 int v
, t
, varg
, is_vaargs
, spc
;
1178 tcc_error("invalid macro name '%s'", get_tok_str(tok
, &tokc
));
1179 /* XXX: should check if same macro (ANSI) */
1182 /* '(' must be just after macro definition for MACRO_FUNC */
1187 while (tok
!= ')') {
1191 if (varg
== TOK_DOTS
) {
1192 varg
= TOK___VA_ARGS__
;
1194 } else if (tok
== TOK_DOTS
&& gnu_ext
) {
1198 if (varg
< TOK_IDENT
)
1199 tcc_error("badly punctuated parameter list");
1200 s
= sym_push2(&define_stack
, varg
| SYM_FIELD
, is_vaargs
, 0);
1213 /* EOF testing necessary for '-D' handling */
1214 while (tok
!= TOK_LINEFEED
&& tok
!= TOK_EOF
) {
1215 /* remove spaces around ## and after '#' */
1216 if (TOK_TWOSHARPS
== tok
) {
1220 } else if ('#' == tok
) {
1222 } else if (check_space(tok
, &spc
)) {
1225 tok_str_add2(&str
, tok
, &tokc
);
1230 --str
.len
; /* remove trailing space */
1231 tok_str_add(&str
, 0);
1233 printf("define %s %d: ", get_tok_str(v
, NULL
), t
);
1236 define_push(v
, t
, str
.str
, first
);
1239 static inline int hash_cached_include(int type
, const char *filename
)
1241 const unsigned char *s
;
1245 h
= TOK_HASH_FUNC(h
, type
);
1248 h
= TOK_HASH_FUNC(h
, *s
);
1251 h
&= (CACHED_INCLUDES_HASH_SIZE
- 1);
1255 /* XXX: use a token or a hash table to accelerate matching ? */
1256 static CachedInclude
*search_cached_include(TCCState
*s1
,
1257 int type
, const char *filename
)
1261 h
= hash_cached_include(type
, filename
);
1262 i
= s1
->cached_includes_hash
[h
];
1266 e
= s1
->cached_includes
[i
- 1];
1267 if (e
->type
== type
&& !PATHCMP(e
->filename
, filename
))
1274 static inline void add_cached_include(TCCState
*s1
, int type
,
1275 const char *filename
, int ifndef_macro
)
1280 if (search_cached_include(s1
, type
, filename
))
1283 printf("adding cached '%s' %s\n", filename
, get_tok_str(ifndef_macro
, NULL
));
1285 e
= tcc_malloc(sizeof(CachedInclude
) + strlen(filename
));
1289 strcpy(e
->filename
, filename
);
1290 e
->ifndef_macro
= ifndef_macro
;
1291 dynarray_add((void ***)&s1
->cached_includes
, &s1
->nb_cached_includes
, e
);
1292 /* add in hash table */
1293 h
= hash_cached_include(type
, filename
);
1294 e
->hash_next
= s1
->cached_includes_hash
[h
];
1295 s1
->cached_includes_hash
[h
] = s1
->nb_cached_includes
;
1298 static void pragma_parse(TCCState
*s1
)
1303 if (tok
== TOK_pack
) {
1306 #pragma pack(1) // set
1307 #pragma pack() // reset to default
1308 #pragma pack(push,1) // push & set
1309 #pragma pack(pop) // restore previous
1313 if (tok
== TOK_ASM_pop
) {
1315 if (s1
->pack_stack_ptr
<= s1
->pack_stack
) {
1317 tcc_error("out of pack stack");
1319 s1
->pack_stack_ptr
--;
1323 if (tok
== TOK_ASM_push
) {
1325 if (s1
->pack_stack_ptr
>= s1
->pack_stack
+ PACK_STACK_SIZE
- 1)
1327 s1
->pack_stack_ptr
++;
1330 if (tok
!= TOK_CINT
) {
1332 tcc_error("invalid pack pragma");
1335 if (val
< 1 || val
> 16 || (val
& (val
- 1)) != 0)
1339 *s1
->pack_stack_ptr
= val
;
1345 /* is_bof is true if first non space token at beginning of file */
1346 ST_FUNC
void preprocess(int is_bof
)
1348 TCCState
*s1
= tcc_state
;
1349 int i
, c
, n
, saved_parse_flags
;
1353 saved_parse_flags
= parse_flags
;
1354 parse_flags
= PARSE_FLAG_PREPROCESS
| PARSE_FLAG_TOK_NUM
|
1355 PARSE_FLAG_LINEFEED
;
1365 s
= define_find(tok
);
1366 /* undefine symbol by putting an invalid name */
1371 case TOK_INCLUDE_NEXT
:
1372 ch
= file
->buf_ptr
[0];
1373 /* XXX: incorrect if comments : use next_nomacro with a special mode */
1378 } else if (ch
== '\"') {
1383 while (ch
!= c
&& ch
!= '\n' && ch
!= CH_EOF
) {
1384 if ((q
- buf
) < sizeof(buf
) - 1)
1387 if (handle_stray_noerror() == 0)
1395 /* eat all spaces and comments after include */
1396 /* XXX: slightly incorrect */
1397 while (ch1
!= '\n' && ch1
!= CH_EOF
)
1401 /* computed #include : either we have only strings or
1402 we have anything enclosed in '<>' */
1405 if (tok
== TOK_STR
) {
1406 while (tok
!= TOK_LINEFEED
) {
1407 if (tok
!= TOK_STR
) {
1409 tcc_error("'#include' expects \"FILENAME\" or <FILENAME>");
1411 pstrcat(buf
, sizeof(buf
), (char *)tokc
.cstr
->data
);
1417 while (tok
!= TOK_LINEFEED
) {
1418 pstrcat(buf
, sizeof(buf
), get_tok_str(tok
, &tokc
));
1422 /* check syntax and remove '<>' */
1423 if (len
< 2 || buf
[0] != '<' || buf
[len
- 1] != '>')
1424 goto include_syntax
;
1425 memmove(buf
, buf
+ 1, len
- 2);
1426 buf
[len
- 2] = '\0';
1431 if (s1
->include_stack_ptr
>= s1
->include_stack
+ INCLUDE_STACK_SIZE
)
1432 tcc_error("#include recursion too deep");
1434 n
= s1
->nb_include_paths
+ s1
->nb_sysinclude_paths
;
1435 for (i
= -2; i
< n
; ++i
) {
1436 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 fd
= tcc_open(s1
, buf1
);
1481 if (tok
== TOK_INCLUDE_NEXT
) {
1492 printf("%s: including %s\n", file
->filename
, buf1
);
1494 /* update target deps */
1495 dynarray_add((void ***)&s1
->target_deps
, &s1
->nb_target_deps
,
1497 /* XXX: fix current line init */
1498 /* push current file in stack */
1499 *s1
->include_stack_ptr
++ = file
->prev
;
1501 pstrcpy(file
->inc_filename
, sizeof(file
->inc_filename
), buf1
);
1502 /* add include file debug info */
1504 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 tcc_error("include file '%s' not found", buf
);
1516 c
= expr_preprocess();
1522 if (tok
< TOK_IDENT
)
1523 tcc_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 tcc_error("memory full");
1536 *s1
->ifdef_stack_ptr
++ = c
;
1539 if (s1
->ifdef_stack_ptr
== s1
->ifdef_stack
)
1540 tcc_error("#else without matching #if");
1541 if (s1
->ifdef_stack_ptr
[-1] & 2)
1542 tcc_error("#else after #else");
1543 c
= (s1
->ifdef_stack_ptr
[-1] ^= 3);
1546 if (s1
->ifdef_stack_ptr
== s1
->ifdef_stack
)
1547 tcc_error("#elif without matching #if");
1548 c
= s1
->ifdef_stack_ptr
[-1];
1550 tcc_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 tcc_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 tcc_error("#error %s", buf
);
1617 tcc_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 tcc_warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok
, &tokc
));
1630 /* this is a gas line comment in an 'S' file. */
1631 file
->buf_ptr
= parse_line_comment(file
->buf_ptr
);
1637 /* ignore other preprocess commands or #! for C scripts */
1638 while (tok
!= TOK_LINEFEED
)
1641 parse_flags
= saved_parse_flags
;
1644 /* evaluate escape codes in a string. */
1645 static void parse_escape_string(CString
*outstr
, const uint8_t *buf
, int is_long
)
1660 case '0': case '1': case '2': case '3':
1661 case '4': case '5': case '6': case '7':
1662 /* at most three octal digits */
1667 n
= n
* 8 + c
- '0';
1671 n
= n
* 8 + c
- '0';
1676 goto add_char_nonext
;
1684 if (c
>= 'a' && c
<= 'f')
1686 else if (c
>= 'A' && c
<= 'F')
1696 goto add_char_nonext
;
1720 goto invalid_escape
;
1730 if (c
>= '!' && c
<= '~')
1731 tcc_warning("unknown escape sequence: \'\\%c\'", c
);
1733 tcc_warning("unknown escape sequence: \'\\x%x\'", c
);
1740 cstr_ccat(outstr
, c
);
1742 cstr_wccat(outstr
, c
);
1744 /* add a trailing '\0' */
1746 cstr_ccat(outstr
, '\0');
1748 cstr_wccat(outstr
, '\0');
1751 /* we use 64 bit numbers */
1754 /* bn = (bn << shift) | or_val */
1755 static void bn_lshift(unsigned int *bn
, int shift
, int or_val
)
1759 for(i
=0;i
<BN_SIZE
;i
++) {
1761 bn
[i
] = (v
<< shift
) | or_val
;
1762 or_val
= v
>> (32 - shift
);
1766 static void bn_zero(unsigned int *bn
)
1769 for(i
=0;i
<BN_SIZE
;i
++) {
1774 /* parse number in null terminated string 'p' and return it in the
1776 static void parse_number(const char *p
)
1778 int b
, t
, shift
, frac_bits
, s
, exp_val
, ch
;
1780 unsigned int bn
[BN_SIZE
];
1791 goto float_frac_parse
;
1792 } else if (t
== '0') {
1793 if (ch
== 'x' || ch
== 'X') {
1797 } else if (tcc_ext
&& (ch
== 'b' || ch
== 'B')) {
1803 /* parse all digits. cannot check octal numbers at this stage
1804 because of floating point constants */
1806 if (ch
>= 'a' && ch
<= 'f')
1808 else if (ch
>= 'A' && ch
<= 'F')
1816 if (q
>= token_buf
+ STRING_MAX_SIZE
) {
1818 tcc_error("number too long");
1824 ((ch
== 'e' || ch
== 'E') && b
== 10) ||
1825 ((ch
== 'p' || ch
== 'P') && (b
== 16 || b
== 2))) {
1827 /* NOTE: strtox should support that for hexa numbers, but
1828 non ISOC99 libcs do not support it, so we prefer to do
1830 /* hexadecimal or binary floats */
1831 /* XXX: handle overflows */
1843 } else if (t
>= 'a') {
1845 } else if (t
>= 'A') {
1850 bn_lshift(bn
, shift
, t
);
1857 if (t
>= 'a' && t
<= 'f') {
1859 } else if (t
>= 'A' && t
<= 'F') {
1861 } else if (t
>= '0' && t
<= '9') {
1867 tcc_error("invalid digit");
1868 bn_lshift(bn
, shift
, t
);
1873 if (ch
!= 'p' && ch
!= 'P')
1880 } else if (ch
== '-') {
1884 if (ch
< '0' || ch
> '9')
1885 expect("exponent digits");
1886 while (ch
>= '0' && ch
<= '9') {
1887 exp_val
= exp_val
* 10 + ch
- '0';
1890 exp_val
= exp_val
* s
;
1892 /* now we can generate the number */
1893 /* XXX: should patch directly float number */
1894 d
= (double)bn
[1] * 4294967296.0 + (double)bn
[0];
1895 d
= ldexp(d
, exp_val
- frac_bits
);
1900 /* float : should handle overflow */
1902 } else if (t
== 'L') {
1904 #ifdef TCC_TARGET_PE
1909 /* XXX: not large enough */
1910 tokc
.ld
= (long double)d
;
1917 /* decimal floats */
1919 if (q
>= token_buf
+ STRING_MAX_SIZE
)
1924 while (ch
>= '0' && ch
<= '9') {
1925 if (q
>= token_buf
+ STRING_MAX_SIZE
)
1931 if (ch
== 'e' || ch
== 'E') {
1932 if (q
>= token_buf
+ STRING_MAX_SIZE
)
1936 if (ch
== '-' || ch
== '+') {
1937 if (q
>= token_buf
+ STRING_MAX_SIZE
)
1942 if (ch
< '0' || ch
> '9')
1943 expect("exponent digits");
1944 while (ch
>= '0' && ch
<= '9') {
1945 if (q
>= token_buf
+ STRING_MAX_SIZE
)
1957 tokc
.f
= strtof(token_buf
, NULL
);
1958 } else if (t
== 'L') {
1960 #ifdef TCC_TARGET_PE
1962 tokc
.d
= strtod(token_buf
, NULL
);
1965 tokc
.ld
= strtold(token_buf
, NULL
);
1969 tokc
.d
= strtod(token_buf
, NULL
);
1973 unsigned long long n
, n1
;
1976 /* integer number */
1979 if (b
== 10 && *q
== '0') {
1986 /* no need for checks except for base 10 / 8 errors */
1989 } else if (t
>= 'a') {
1991 } else if (t
>= 'A') {
1996 tcc_error("invalid digit");
2000 /* detect overflow */
2001 /* XXX: this test is not reliable */
2003 tcc_error("integer constant overflow");
2006 /* XXX: not exactly ANSI compliant */
2007 if ((n
& 0xffffffff00000000LL
) != 0) {
2012 } else if (n
> 0x7fffffff) {
2023 tcc_error("three 'l's in integer constant");
2025 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
2028 if (tok
== TOK_CINT
)
2030 else if (tok
== TOK_CUINT
)
2032 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
2036 } else if (t
== 'U') {
2038 tcc_error("two 'u's in integer constant");
2040 if (tok
== TOK_CINT
)
2042 else if (tok
== TOK_CLLONG
)
2049 if (tok
== TOK_CINT
|| tok
== TOK_CUINT
)
2055 tcc_error("invalid number\n");
2059 #define PARSE2(c1, tok1, c2, tok2) \
2070 /* return next token without macro substitution */
2071 static inline void next_nomacro1(void)
2086 goto keep_tok_flags
;
2093 /* first look if it is in fact an end of buffer */
2094 if (p
>= file
->buf_end
) {
2098 if (p
>= file
->buf_end
)
2111 TCCState
*s1
= tcc_state
;
2112 if ((parse_flags
& PARSE_FLAG_LINEFEED
)
2113 && !(tok_flags
& TOK_FLAG_EOF
)) {
2114 tok_flags
|= TOK_FLAG_EOF
;
2116 goto keep_tok_flags
;
2117 } else if (!(parse_flags
& PARSE_FLAG_PREPROCESS
)) {
2119 } else if (s1
->ifdef_stack_ptr
!= file
->ifdef_stack_ptr
) {
2120 tcc_error("missing #endif");
2121 } else if (s1
->include_stack_ptr
== s1
->include_stack
) {
2122 /* no include left : end of file. */
2125 tok_flags
&= ~TOK_FLAG_EOF
;
2126 /* pop include file */
2128 /* test if previous '#endif' was after a #ifdef at
2130 if (tok_flags
& TOK_FLAG_ENDIF
) {
2132 printf("#endif %s\n", get_tok_str(file
->ifndef_macro_saved
, NULL
));
2134 add_cached_include(s1
, file
->inc_type
, file
->inc_filename
,
2135 file
->ifndef_macro_saved
);
2138 /* add end of include file debug info */
2139 if (tcc_state
->do_debug
) {
2140 put_stabd(N_EINCL
, 0, 0);
2142 /* pop include stack */
2144 s1
->include_stack_ptr
--;
2153 tok_flags
|= TOK_FLAG_BOL
;
2156 if (0 == (parse_flags
& PARSE_FLAG_LINEFEED
))
2159 goto keep_tok_flags
;
2164 if ((tok_flags
& TOK_FLAG_BOL
) &&
2165 (parse_flags
& PARSE_FLAG_PREPROCESS
)) {
2167 preprocess(tok_flags
& TOK_FLAG_BOF
);
2173 tok
= TOK_TWOSHARPS
;
2175 if (parse_flags
& PARSE_FLAG_ASM_COMMENTS
) {
2176 p
= parse_line_comment(p
- 1);
2185 case 'a': case 'b': case 'c': case 'd':
2186 case 'e': case 'f': case 'g': case 'h':
2187 case 'i': case 'j': case 'k': case 'l':
2188 case 'm': case 'n': case 'o': case 'p':
2189 case 'q': case 'r': case 's': case 't':
2190 case 'u': case 'v': case 'w': case 'x':
2192 case 'A': case 'B': case 'C': case 'D':
2193 case 'E': case 'F': case 'G': case 'H':
2194 case 'I': case 'J': case 'K':
2195 case 'M': case 'N': case 'O': case 'P':
2196 case 'Q': case 'R': case 'S': case 'T':
2197 case 'U': case 'V': case 'W': case 'X':
2203 h
= TOK_HASH_FUNC(h
, c
);
2207 if (!isidnum_table
[c
-CH_EOF
])
2209 h
= TOK_HASH_FUNC(h
, c
);
2216 /* fast case : no stray found, so we have the full token
2217 and we have already hashed it */
2219 h
&= (TOK_HASH_SIZE
- 1);
2220 pts
= &hash_ident
[h
];
2225 if (ts
->len
== len
&& !memcmp(ts
->str
, p1
, len
))
2227 pts
= &(ts
->hash_next
);
2229 ts
= tok_alloc_new(pts
, p1
, len
);
2233 cstr_reset(&tokcstr
);
2236 cstr_ccat(&tokcstr
, *p1
);
2242 while (isidnum_table
[c
-CH_EOF
]) {
2243 cstr_ccat(&tokcstr
, c
);
2246 ts
= tok_alloc(tokcstr
.data
, tokcstr
.size
);
2252 if (t
!= '\\' && t
!= '\'' && t
!= '\"') {
2254 goto parse_ident_fast
;
2257 if (c
== '\'' || c
== '\"') {
2261 cstr_reset(&tokcstr
);
2262 cstr_ccat(&tokcstr
, 'L');
2263 goto parse_ident_slow
;
2267 case '0': case '1': case '2': case '3':
2268 case '4': case '5': case '6': case '7':
2271 cstr_reset(&tokcstr
);
2272 /* after the first digit, accept digits, alpha, '.' or sign if
2273 prefixed by 'eEpP' */
2277 cstr_ccat(&tokcstr
, c
);
2279 if (!(isnum(c
) || isid(c
) || c
== '.' ||
2280 ((c
== '+' || c
== '-') &&
2281 (t
== 'e' || t
== 'E' || t
== 'p' || t
== 'P'))))
2284 /* We add a trailing '\0' to ease parsing */
2285 cstr_ccat(&tokcstr
, '\0');
2286 tokc
.cstr
= &tokcstr
;
2290 /* special dot handling because it can also start a number */
2293 cstr_reset(&tokcstr
);
2294 cstr_ccat(&tokcstr
, '.');
2296 } else if (c
== '.') {
2316 /* parse the string */
2318 p
= parse_pp_string(p
, sep
, &str
);
2319 cstr_ccat(&str
, '\0');
2321 /* eval the escape (should be done as TOK_PPNUM) */
2322 cstr_reset(&tokcstr
);
2323 parse_escape_string(&tokcstr
, str
.data
, is_long
);
2328 /* XXX: make it portable */
2332 char_size
= sizeof(nwchar_t
);
2333 if (tokcstr
.size
<= char_size
)
2334 tcc_error("empty character constant");
2335 if (tokcstr
.size
> 2 * char_size
)
2336 tcc_warning("multi-character character constant");
2338 tokc
.i
= *(int8_t *)tokcstr
.data
;
2341 tokc
.i
= *(nwchar_t
*)tokcstr
.data
;
2345 tokc
.cstr
= &tokcstr
;
2359 } else if (c
== '<') {
2377 } else if (c
== '>') {
2395 } else if (c
== '=') {
2408 } else if (c
== '=') {
2421 } else if (c
== '=') {
2434 } else if (c
== '=') {
2437 } else if (c
== '>') {
2445 PARSE2('!', '!', '=', TOK_NE
)
2446 PARSE2('=', '=', '=', TOK_EQ
)
2447 PARSE2('*', '*', '=', TOK_A_MUL
)
2448 PARSE2('%', '%', '=', TOK_A_MOD
)
2449 PARSE2('^', '^', '=', TOK_A_XOR
)
2451 /* comments or operator */
2455 p
= parse_comment(p
);
2456 /* comments replaced by a blank */
2458 goto keep_tok_flags
;
2459 } else if (c
== '/') {
2460 p
= parse_line_comment(p
);
2462 goto keep_tok_flags
;
2463 } else if (c
== '=') {
2483 case '$': /* only used in assembler */
2484 case '@': /* dito */
2489 tcc_error("unrecognized character \\x%02x", c
);
2495 #if defined(PARSE_DEBUG)
2496 printf("token = %s\n", get_tok_str(tok
, &tokc
));
2500 /* return next token without macro substitution. Can read input from
2502 static void next_nomacro_spc(void)
2508 TOK_GET(&tok
, ¯o_ptr
, &tokc
);
2509 if (tok
== TOK_LINENUM
) {
2510 file
->line_num
= tokc
.i
;
2519 ST_FUNC
void next_nomacro(void)
2523 } while (is_space(tok
));
2526 /* substitute args in macro_str and return allocated string */
2527 static int *macro_arg_subst(Sym
**nested_list
, const int *macro_str
, Sym
*args
)
2529 int last_tok
, t
, spc
;
2539 TOK_GET(&t
, ¯o_str
, &cval
);
2544 TOK_GET(&t
, ¯o_str
, &cval
);
2547 s
= sym_find2(args
, t
);
2553 TOK_GET(&t
, &st
, &cval
);
2554 if (!check_space(t
, &spc
))
2555 cstr_cat(&cstr
, get_tok_str(t
, &cval
));
2558 cstr_ccat(&cstr
, '\0');
2560 printf("stringize: %s\n", (char *)cstr
.data
);
2564 tok_str_add2(&str
, TOK_STR
, &cval
);
2567 tok_str_add2(&str
, t
, &cval
);
2569 } else if (t
>= TOK_IDENT
) {
2570 s
= sym_find2(args
, t
);
2573 /* if '##' is present before or after, no arg substitution */
2574 if (*macro_str
== TOK_TWOSHARPS
|| last_tok
== TOK_TWOSHARPS
) {
2575 /* special case for var arg macros : ## eats the
2576 ',' if empty VA_ARGS variable. */
2577 /* XXX: test of the ',' is not 100%
2578 reliable. should fix it to avoid security
2580 if (gnu_ext
&& s
->type
.t
&&
2581 last_tok
== TOK_TWOSHARPS
&&
2582 str
.len
>= 2 && str
.str
[str
.len
- 2] == ',') {
2584 /* suppress ',' '##' */
2587 /* suppress '##' and add variable */
2595 TOK_GET(&t1
, &st
, &cval
);
2598 tok_str_add2(&str
, t1
, &cval
);
2602 /* NOTE: the stream cannot be read when macro
2603 substituing an argument */
2604 macro_subst(&str
, nested_list
, st
, NULL
);
2607 tok_str_add(&str
, t
);
2610 tok_str_add2(&str
, t
, &cval
);
2614 tok_str_add(&str
, 0);
2618 static char const ab_month_name
[12][4] =
2620 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2621 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
2624 /* do macro substitution of current token with macro 's' and add
2625 result to (tok_str,tok_len). 'nested_list' is the list of all
2626 macros we got inside to avoid recursing. Return non zero if no
2627 substitution needs to be done */
2628 static int macro_subst_tok(TokenString
*tok_str
,
2629 Sym
**nested_list
, Sym
*s
, struct macro_level
**can_read_stream
)
2631 Sym
*args
, *sa
, *sa1
;
2632 int mstr_allocated
, parlevel
, *mstr
, t
, t1
, spc
;
2640 /* if symbol is a macro, prepare substitution */
2641 /* special macros */
2642 if (tok
== TOK___LINE__
) {
2643 snprintf(buf
, sizeof(buf
), "%d", file
->line_num
);
2647 } else if (tok
== TOK___FILE__
) {
2648 cstrval
= file
->filename
;
2650 } else if (tok
== TOK___DATE__
|| tok
== TOK___TIME__
) {
2655 tm
= localtime(&ti
);
2656 if (tok
== TOK___DATE__
) {
2657 snprintf(buf
, sizeof(buf
), "%s %2d %d",
2658 ab_month_name
[tm
->tm_mon
], tm
->tm_mday
, tm
->tm_year
+ 1900);
2660 snprintf(buf
, sizeof(buf
), "%02d:%02d:%02d",
2661 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
2668 cstr_cat(&cstr
, cstrval
);
2669 cstr_ccat(&cstr
, '\0');
2671 tok_str_add2(tok_str
, t1
, &cval
);
2676 if (s
->type
.t
== MACRO_FUNC
) {
2677 /* NOTE: we do not use next_nomacro to avoid eating the
2678 next token. XXX: find better solution */
2682 while (is_space(t
= *p
) || TOK_LINEFEED
== t
)
2684 if (t
== 0 && can_read_stream
) {
2685 /* end of macro stream: we must look at the token
2686 after in the file */
2687 struct macro_level
*ml
= *can_read_stream
;
2693 *can_read_stream
= ml
-> prev
;
2695 /* also, end of scope for nested defined symbol */
2696 (*nested_list
)->v
= -1;
2700 ch
= file
->buf_ptr
[0];
2701 while (is_space(ch
) || ch
== '\n' || ch
== '/')
2706 uint8_t *p
= file
->buf_ptr
;
2709 p
= parse_comment(p
);
2710 file
->buf_ptr
= p
- 1;
2711 } else if (c
== '/') {
2712 p
= parse_line_comment(p
);
2713 file
->buf_ptr
= p
- 1;
2721 if (t
!= '(') /* no macro subst */
2724 /* argument macro */
2729 /* NOTE: empty args are allowed, except if no args */
2731 /* handle '()' case */
2732 if (!args
&& !sa
&& tok
== ')')
2735 tcc_error("macro '%s' used with too many args",
2736 get_tok_str(s
->v
, 0));
2739 /* NOTE: non zero sa->t indicates VA_ARGS */
2740 while ((parlevel
> 0 ||
2742 (tok
!= ',' || sa
->type
.t
))) &&
2746 else if (tok
== ')')
2748 if (tok
== TOK_LINEFEED
)
2750 if (!check_space(tok
, &spc
))
2751 tok_str_add2(&str
, tok
, &tokc
);
2755 tok_str_add(&str
, 0);
2756 sa1
= sym_push2(&args
, sa
->v
& ~SYM_FIELD
, sa
->type
.t
, 0);
2760 /* special case for gcc var args: add an empty
2761 var arg argument if it is omitted */
2762 if (sa
&& sa
->type
.t
&& gnu_ext
)
2772 tcc_error("macro '%s' used with too few args",
2773 get_tok_str(s
->v
, 0));
2776 /* now subst each arg */
2777 mstr
= macro_arg_subst(nested_list
, mstr
, args
);
2782 tok_str_free(sa
->d
);
2788 sym_push2(nested_list
, s
->v
, 0, 0);
2789 macro_subst(tok_str
, nested_list
, mstr
, can_read_stream
);
2790 /* pop nested defined symbol */
2792 *nested_list
= sa1
->prev
;
2800 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
2801 return the resulting string (which must be freed). */
2802 static inline int *macro_twosharps(const int *macro_str
)
2807 TokenString macro_str1
;
2809 int n
, start_of_nosubsts
;
2811 /* we search the first '##' */
2812 for(ptr
= macro_str
;;) {
2813 TOK_GET(&t
, &ptr
, &cval
);
2814 if (t
== TOK_TWOSHARPS
)
2816 /* nothing more to do if end of string */
2821 /* we saw '##', so we need more processing to handle it */
2822 start_of_nosubsts
= -1;
2823 tok_str_new(¯o_str1
);
2824 for(ptr
= macro_str
;;) {
2825 TOK_GET(&tok
, &ptr
, &tokc
);
2828 if (tok
== TOK_TWOSHARPS
)
2830 if (tok
== TOK_NOSUBST
&& start_of_nosubsts
< 0)
2831 start_of_nosubsts
= macro_str1
.len
;
2832 while (*ptr
== TOK_TWOSHARPS
) {
2833 /* given 'a##b', remove nosubsts preceding 'a' */
2834 if (start_of_nosubsts
>= 0)
2835 macro_str1
.len
= start_of_nosubsts
;
2836 /* given 'a##b', skip '##' */
2838 /* given 'a##b', remove nosubsts preceding 'b' */
2839 while (t
== TOK_NOSUBST
)
2842 if (t
&& t
!= TOK_TWOSHARPS
) {
2843 TOK_GET(&t
, &ptr
, &cval
);
2845 /* We concatenate the two tokens */
2847 cstr_cat(&cstr
, get_tok_str(tok
, &tokc
));
2849 cstr_cat(&cstr
, get_tok_str(t
, &cval
));
2850 cstr_ccat(&cstr
, '\0');
2852 tcc_open_bf(tcc_state
, "<paste>", cstr
.size
);
2853 memcpy(file
->buffer
, cstr
.data
, cstr
.size
);
2856 if (0 == *file
->buf_ptr
)
2858 tok_str_add2(¯o_str1
, tok
, &tokc
);
2859 tcc_warning("pasting \"%.*s\" and \"%s\" does not give a valid preprocessing token",
2860 n
, cstr
.data
, (char*)cstr
.data
+ n
);
2866 if (tok
!= TOK_NOSUBST
)
2867 start_of_nosubsts
= -1;
2868 tok_str_add2(¯o_str1
, tok
, &tokc
);
2870 tok_str_add(¯o_str1
, 0);
2871 return macro_str1
.str
;
2875 /* do macro substitution of macro_str and add result to
2876 (tok_str,tok_len). 'nested_list' is the list of all macros we got
2877 inside to avoid recursing. */
2878 static void macro_subst(TokenString
*tok_str
, Sym
**nested_list
,
2879 const int *macro_str
, struct macro_level
** can_read_stream
)
2886 struct macro_level ml
;
2889 /* first scan for '##' operator handling */
2891 macro_str1
= macro_twosharps(ptr
);
2899 /* NOTE: ptr == NULL can only happen if tokens are read from
2900 file stream due to a macro function call */
2903 TOK_GET(&t
, &ptr
, &cval
);
2906 if (t
== TOK_NOSUBST
) {
2907 /* following token has already been subst'd. just copy it on */
2908 tok_str_add2(tok_str
, TOK_NOSUBST
, NULL
);
2909 TOK_GET(&t
, &ptr
, &cval
);
2914 /* if nested substitution, do nothing */
2915 if (sym_find2(*nested_list
, t
)) {
2916 /* and mark it as TOK_NOSUBST, so it doesn't get subst'd again */
2917 tok_str_add2(tok_str
, TOK_NOSUBST
, NULL
);
2921 if (can_read_stream
)
2922 ml
.prev
= *can_read_stream
, *can_read_stream
= &ml
;
2923 macro_ptr
= (int *)ptr
;
2925 ret
= macro_subst_tok(tok_str
, nested_list
, s
, can_read_stream
);
2926 ptr
= (int *)macro_ptr
;
2928 if (can_read_stream
&& *can_read_stream
== &ml
)
2929 *can_read_stream
= ml
.prev
;
2932 if (parse_flags
& PARSE_FLAG_SPACES
)
2937 tok_str_add(tok_str
, ' ');
2941 if (!check_space(t
, &spc
))
2942 tok_str_add2(tok_str
, t
, &cval
);
2946 tok_str_free(macro_str1
);
2949 /* return next token with macro substitution */
2950 ST_FUNC
void next(void)
2952 Sym
*nested_list
, *s
;
2954 struct macro_level
*ml
;
2957 if (parse_flags
& PARSE_FLAG_SPACES
)
2962 /* if not reading from macro substituted string, then try
2963 to substitute macros */
2964 if (tok
>= TOK_IDENT
&&
2965 (parse_flags
& PARSE_FLAG_PREPROCESS
)) {
2966 s
= define_find(tok
);
2968 /* we have a macro: we try to substitute */
2972 if (macro_subst_tok(&str
, &nested_list
, s
, &ml
) == 0) {
2973 /* substitution done, NOTE: maybe empty */
2974 tok_str_add(&str
, 0);
2975 macro_ptr
= str
.str
;
2976 macro_ptr_allocated
= str
.str
;
2983 /* end of macro or end of unget buffer */
2984 if (unget_buffer_enabled
) {
2985 macro_ptr
= unget_saved_macro_ptr
;
2986 unget_buffer_enabled
= 0;
2988 /* end of macro string: free it */
2989 tok_str_free(macro_ptr_allocated
);
2990 macro_ptr_allocated
= NULL
;
2994 } else if (tok
== TOK_NOSUBST
) {
2995 /* discard preprocessor's nosubst markers */
3000 /* convert preprocessor tokens into C tokens */
3001 if (tok
== TOK_PPNUM
&&
3002 (parse_flags
& PARSE_FLAG_TOK_NUM
)) {
3003 parse_number((char *)tokc
.cstr
->data
);
3007 /* push back current token and set current token to 'last_tok'. Only
3008 identifier case handled for labels. */
3009 ST_INLN
void unget_tok(int last_tok
)
3013 if (unget_buffer_enabled
)
3015 /* assert(macro_ptr == unget_saved_buffer + 1);
3016 assert(*macro_ptr == 0); */
3020 unget_saved_macro_ptr
= macro_ptr
;
3021 unget_buffer_enabled
= 1;
3023 q
= unget_saved_buffer
;
3026 n
= tok_ext_size(tok
) - 1;
3029 *q
= 0; /* end of token string */
3034 /* better than nothing, but needs extension to handle '-E' option
3036 ST_FUNC
void preprocess_init(TCCState
*s1
)
3038 s1
->include_stack_ptr
= s1
->include_stack
;
3039 /* XXX: move that before to avoid having to initialize
3040 file->ifdef_stack_ptr ? */
3041 s1
->ifdef_stack_ptr
= s1
->ifdef_stack
;
3042 file
->ifdef_stack_ptr
= s1
->ifdef_stack_ptr
;
3044 /* XXX: not ANSI compliant: bound checking says error */
3046 s1
->pack_stack
[0] = 0;
3047 s1
->pack_stack_ptr
= s1
->pack_stack
;
3050 ST_FUNC
void preprocess_new()
3055 /* init isid table */
3056 for(i
=CH_EOF
;i
<256;i
++)
3057 isidnum_table
[i
-CH_EOF
] = isid(i
) || isnum(i
);
3059 /* add all tokens */
3061 memset(hash_ident
, 0, TOK_HASH_SIZE
* sizeof(TokenSym
*));
3063 tok_ident
= TOK_IDENT
;
3072 tok_alloc(p
, r
- p
- 1);
3077 /* Preprocess the current file */
3078 ST_FUNC
int tcc_preprocess(TCCState
*s1
)
3082 BufferedFile
*file_ref
, **iptr
, **iptr_new
;
3083 int token_seen
, line_ref
, d
;
3086 preprocess_init(s1
);
3087 define_start
= define_stack
;
3088 ch
= file
->buf_ptr
[0];
3089 tok_flags
= TOK_FLAG_BOL
| TOK_FLAG_BOF
;
3090 parse_flags
= PARSE_FLAG_ASM_COMMENTS
| PARSE_FLAG_PREPROCESS
|
3091 PARSE_FLAG_LINEFEED
| PARSE_FLAG_SPACES
;
3095 iptr
= s1
->include_stack_ptr
;
3099 if (tok
== TOK_EOF
) {
3101 } else if (file
!= file_ref
) {
3103 } else if (tok
== TOK_LINEFEED
) {
3108 } else if (!token_seen
) {
3109 d
= file
->line_num
- line_ref
;
3110 if (file
!= file_ref
|| d
< 0 || d
>= 8) {
3112 iptr_new
= s1
->include_stack_ptr
;
3113 s
= iptr_new
> iptr
? " 1"
3114 : iptr_new
< iptr
? " 2"
3115 : iptr_new
> s1
->include_stack
? " 3"
3119 fprintf(s1
->outfile
, "# %d \"%s\"%s\n", file
->line_num
, file
->filename
, s
);
3122 fputs("\n", s1
->outfile
), --d
;
3124 line_ref
= (file_ref
= file
)->line_num
;
3125 token_seen
= tok
!= TOK_LINEFEED
;
3129 fputs(get_tok_str(tok
, &tokc
), s1
->outfile
);
3131 free_defines(define_start
);