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
21 /* fill input buffer and peek next char */
22 static int tcc_peekc_slow(BufferedFile
*bf
)
25 /* only tries to read if really end of buffer */
26 if (bf
->buf_ptr
>= bf
->buf_end
) {
28 #if defined(PARSE_DEBUG)
33 len
= read(bf
->fd
, bf
->buffer
, len
);
40 bf
->buf_ptr
= bf
->buffer
;
41 bf
->buf_end
= bf
->buffer
+ len
;
42 *bf
->buf_end
= CH_EOB
;
44 if (bf
->buf_ptr
< bf
->buf_end
) {
45 return bf
->buf_ptr
[0];
47 bf
->buf_ptr
= bf
->buf_end
;
52 /* return the current character, handling end of block if necessary
54 static int handle_eob(void)
56 return tcc_peekc_slow(file
);
59 /* read next char from current input file and handle end of input buffer */
60 static inline void inp(void)
62 ch
= *(++(file
->buf_ptr
));
63 /* end of buffer/file handling */
68 /* handle '\[\r]\n' */
69 static int handle_stray_noerror(void)
76 } else if (ch
== '\r') {
90 static void handle_stray(void)
92 if (handle_stray_noerror())
93 error("stray '\\' in program");
96 /* skip the stray and handle the \\n case. Output an error if
97 incorrect char after the stray */
98 static int handle_stray1(uint8_t *p
)
102 if (p
>= file
->buf_end
) {
119 /* handle just the EOB case, but not stray */
120 #define PEEKC_EOB(c, p)\
131 /* handle the complicated stray case */
137 c = handle_stray1(p);\
142 /* input with '\[\r]\n' handling. Note that this function cannot
143 handle other characters after '\', so you cannot call it inside
144 strings or comments */
145 static void minp(void)
153 /* single line C++ comments */
154 static uint8_t *parse_line_comment(uint8_t *p
)
162 if (c
== '\n' || c
== CH_EOF
) {
164 } else if (c
== '\\') {
173 } else if (c
== '\r') {
191 static uint8_t *parse_comment(uint8_t *p
)
200 if (c
== '\n' || c
== '*' || c
== '\\')
204 if (c
== '\n' || c
== '*' || c
== '\\')
208 /* now we can handle all the cases */
212 } else if (c
== '*') {
218 } else if (c
== '/') {
220 } else if (c
== '\\') {
225 /* skip '\[\r]\n', otherwise just skip the stray */
231 } else if (c
== '\r') {
248 /* stray, eob or eof */
253 error("unexpected end of file in comment");
254 } else if (c
== '\\') {
266 /* space exlcuding newline */
267 static inline int is_space(int ch
)
269 return ch
== ' ' || ch
== '\t' || ch
== '\v' || ch
== '\f' || ch
== '\r';
272 static inline void skip_spaces(void)
278 static inline int check_space(int t
, int *spc
)
289 /* parse a string without interpreting escapes */
290 static uint8_t *parse_pp_string(uint8_t *p
,
291 int sep
, CString
*str
)
299 } else if (c
== '\\') {
305 /* XXX: indicate line number of start of string */
306 error("missing terminating %c character", sep
);
307 } else if (c
== '\\') {
308 /* escape : just skip \[\r]\n */
313 } else if (c
== '\r') {
316 expect("'\n' after '\r'");
319 } else if (c
== CH_EOF
) {
320 goto unterminated_string
;
323 cstr_ccat(str
, '\\');
329 } else if (c
== '\n') {
332 } else if (c
== '\r') {
336 cstr_ccat(str
, '\r');
352 /* skip block of text until #else, #elif or #endif. skip also pairs of
354 void preprocess_skip(void)
356 int a
, start_of_line
, c
, in_warn_or_error
;
363 in_warn_or_error
= 0;
384 } else if (c
== '\\') {
385 ch
= file
->buf_ptr
[0];
386 handle_stray_noerror();
393 if (in_warn_or_error
)
395 p
= parse_pp_string(p
, c
, NULL
);
399 if (in_warn_or_error
)
406 p
= parse_comment(p
);
407 } else if (ch
== '/') {
408 p
= parse_line_comment(p
);
418 (tok
== TOK_ELSE
|| tok
== TOK_ELIF
|| tok
== TOK_ENDIF
))
420 if (tok
== TOK_IF
|| tok
== TOK_IFDEF
|| tok
== TOK_IFNDEF
)
422 else if (tok
== TOK_ENDIF
)
424 else if( tok
== TOK_ERROR
|| tok
== TOK_WARNING
)
425 in_warn_or_error
= 1;
439 /* ParseState handling */
441 /* XXX: currently, no include file info is stored. Thus, we cannot display
442 accurate messages if the function or data definition spans multiple
445 /* save current parse state in 's' */
446 void save_parse_state(ParseState
*s
)
448 s
->line_num
= file
->line_num
;
449 s
->macro_ptr
= macro_ptr
;
454 /* restore parse state from 's' */
455 void restore_parse_state(ParseState
*s
)
457 file
->line_num
= s
->line_num
;
458 macro_ptr
= s
->macro_ptr
;
463 /* return the number of additional 'ints' necessary to store the
465 static inline int tok_ext_size(int t
)
479 error("unsupported token");
486 return LDOUBLE_SIZE
/ 4;
492 /* token string handling */
494 static inline void tok_str_new(TokenString
*s
)
498 s
->allocated_len
= 0;
499 s
->last_line_num
= -1;
502 static void tok_str_free(int *str
)
507 static int *tok_str_realloc(TokenString
*s
)
511 if (s
->allocated_len
== 0) {
514 len
= s
->allocated_len
* 2;
516 str
= tcc_realloc(s
->str
, len
* sizeof(int));
518 error("memory full");
519 s
->allocated_len
= len
;
524 static void tok_str_add(TokenString
*s
, int t
)
530 if (len
>= s
->allocated_len
)
531 str
= tok_str_realloc(s
);
536 static void tok_str_add2(TokenString
*s
, int t
, CValue
*cv
)
543 /* allocate space for worst case */
544 if (len
+ TOK_MAX_SIZE
> s
->allocated_len
)
545 str
= tok_str_realloc(s
);
554 str
[len
++] = cv
->tab
[0];
563 nb_words
= (sizeof(CString
) + cv
->cstr
->size
+ 3) >> 2;
564 while ((len
+ nb_words
) > s
->allocated_len
)
565 str
= tok_str_realloc(s
);
566 cstr
= (CString
*)(str
+ len
);
568 cstr
->size
= cv
->cstr
->size
;
569 cstr
->data_allocated
= NULL
;
570 cstr
->size_allocated
= cstr
->size
;
571 memcpy((char *)cstr
+ sizeof(CString
),
572 cv
->cstr
->data
, cstr
->size
);
579 #if LDOUBLE_SIZE == 8
582 str
[len
++] = cv
->tab
[0];
583 str
[len
++] = cv
->tab
[1];
585 #if LDOUBLE_SIZE == 12
587 str
[len
++] = cv
->tab
[0];
588 str
[len
++] = cv
->tab
[1];
589 str
[len
++] = cv
->tab
[2];
590 #elif LDOUBLE_SIZE == 16
592 str
[len
++] = cv
->tab
[0];
593 str
[len
++] = cv
->tab
[1];
594 str
[len
++] = cv
->tab
[2];
595 str
[len
++] = cv
->tab
[3];
596 #elif LDOUBLE_SIZE != 8
597 #error add long double size support
606 /* add the current parse token in token string 's' */
607 static void tok_str_add_tok(TokenString
*s
)
611 /* save line number info */
612 if (file
->line_num
!= s
->last_line_num
) {
613 s
->last_line_num
= file
->line_num
;
614 cval
.i
= s
->last_line_num
;
615 tok_str_add2(s
, TOK_LINENUM
, &cval
);
617 tok_str_add2(s
, tok
, &tokc
);
620 #if LDOUBLE_SIZE == 16
621 #define LDOUBLE_GET(p, cv) \
626 #elif LDOUBLE_SIZE == 12
627 #define LDOUBLE_GET(p, cv) \
631 #elif LDOUBLE_SIZE == 8
632 #define LDOUBLE_GET(p, cv) \
636 #error add long double size support
640 /* get a token from an integer array and increment pointer
641 accordingly. we code it as a macro to avoid pointer aliasing. */
642 #define TOK_GET(t, p, cv) \
657 cv.cstr = (CString *)p; \
658 cv.cstr->data = (char *)p + sizeof(CString);\
659 p += (sizeof(CString) + cv.cstr->size + 3) >> 2;\
669 LDOUBLE_GET(p, cv); \
670 p += LDOUBLE_SIZE / 4; \
677 /* defines handling */
678 static inline void define_push(int v
, int macro_type
, int *str
, Sym
*first_arg
)
682 s
= sym_push2(&define_stack
, v
, macro_type
, (long)str
);
684 table_ident
[v
- TOK_IDENT
]->sym_define
= s
;
687 /* undefined a define symbol. Its name is just set to zero */
688 static void define_undef(Sym
*s
)
692 if (v
>= TOK_IDENT
&& v
< tok_ident
)
693 table_ident
[v
- TOK_IDENT
]->sym_define
= NULL
;
697 static inline Sym
*define_find(int v
)
700 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
702 return table_ident
[v
]->sym_define
;
705 /* free define stack until top reaches 'b' */
706 static void free_defines(Sym
*b
)
714 /* do not free args or predefined defines */
716 tok_str_free((int *)top
->c
);
718 if (v
>= TOK_IDENT
&& v
< tok_ident
)
719 table_ident
[v
- TOK_IDENT
]->sym_define
= NULL
;
727 static Sym
*label_find(int v
)
730 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
732 return table_ident
[v
]->sym_label
;
735 static Sym
*label_push(Sym
**ptop
, int v
, int flags
)
738 s
= sym_push2(ptop
, v
, 0, 0);
740 ps
= &table_ident
[v
- TOK_IDENT
]->sym_label
;
741 if (ptop
== &global_label_stack
) {
742 /* modify the top most local identifier, so that
743 sym_identifier will point to 's' when popped */
745 ps
= &(*ps
)->prev_tok
;
752 /* pop labels until element last is reached. Look if any labels are
753 undefined. Define symbols if '&&label' was used. */
754 static void label_pop(Sym
**ptop
, Sym
*slast
)
757 for(s
= *ptop
; s
!= slast
; s
= s1
) {
759 if (s
->r
== LABEL_DECLARED
) {
760 warning("label '%s' declared but not used", get_tok_str(s
->v
, NULL
));
761 } else if (s
->r
== LABEL_FORWARD
) {
762 error("label '%s' used but not defined",
763 get_tok_str(s
->v
, NULL
));
766 /* define corresponding symbol. A size of
768 put_extern_sym(s
, cur_text_section
, (long)s
->next
, 1);
772 table_ident
[s
->v
- TOK_IDENT
]->sym_label
= s
->prev_tok
;
778 /* eval an expression for #if/#elif */
779 static int expr_preprocess(void)
785 while (tok
!= TOK_LINEFEED
&& tok
!= TOK_EOF
) {
786 next(); /* do macro subst */
787 if (tok
== TOK_DEFINED
) {
792 c
= define_find(tok
) != 0;
797 } else if (tok
>= TOK_IDENT
) {
798 /* if undefined macro */
802 tok_str_add_tok(&str
);
804 tok_str_add(&str
, -1); /* simulate end of file */
805 tok_str_add(&str
, 0);
806 /* now evaluate C constant expression */
811 tok_str_free(str
.str
);
815 #if defined(PARSE_DEBUG) || defined(PP_DEBUG)
816 static void tok_print(int *str
)
823 TOK_GET(t
, str
, cval
);
826 printf("%s", get_tok_str(t
, &cval
));
832 /* parse after #define */
833 static void parse_define(void)
835 Sym
*s
, *first
, **ps
;
836 int v
, t
, varg
, is_vaargs
, spc
;
841 error("invalid macro name '%s'", get_tok_str(tok
, &tokc
));
842 /* XXX: should check if same macro (ANSI) */
845 /* '(' must be just after macro definition for MACRO_FUNC */
854 if (varg
== TOK_DOTS
) {
855 varg
= TOK___VA_ARGS__
;
857 } else if (tok
== TOK_DOTS
&& gnu_ext
) {
861 if (varg
< TOK_IDENT
)
862 error("badly punctuated parameter list");
863 s
= sym_push2(&define_stack
, varg
| SYM_FIELD
, is_vaargs
, 0);
876 /* EOF testing necessary for '-D' handling */
877 while (tok
!= TOK_LINEFEED
&& tok
!= TOK_EOF
) {
878 /* remove spaces around ## and after '#' */
879 if (TOK_TWOSHARPS
== tok
) {
883 } else if ('#' == tok
) {
885 } else if (check_space(tok
, &spc
)) {
888 tok_str_add2(&str
, tok
, &tokc
);
893 --str
.len
; /* remove trailing space */
894 tok_str_add(&str
, 0);
896 printf("define %s %d: ", get_tok_str(v
, NULL
), t
);
899 define_push(v
, t
, str
.str
, first
);
902 static inline int hash_cached_include(int type
, const char *filename
)
904 const unsigned char *s
;
908 h
= TOK_HASH_FUNC(h
, type
);
911 h
= TOK_HASH_FUNC(h
, *s
);
914 h
&= (CACHED_INCLUDES_HASH_SIZE
- 1);
918 /* XXX: use a token or a hash table to accelerate matching ? */
919 static CachedInclude
*search_cached_include(TCCState
*s1
,
920 int type
, const char *filename
)
924 h
= hash_cached_include(type
, filename
);
925 i
= s1
->cached_includes_hash
[h
];
929 e
= s1
->cached_includes
[i
- 1];
930 if (e
->type
== type
&& !PATHCMP(e
->filename
, filename
))
937 static inline void add_cached_include(TCCState
*s1
, int type
,
938 const char *filename
, int ifndef_macro
)
943 if (search_cached_include(s1
, type
, filename
))
946 printf("adding cached '%s' %s\n", filename
, get_tok_str(ifndef_macro
, NULL
));
948 e
= tcc_malloc(sizeof(CachedInclude
) + strlen(filename
));
952 strcpy(e
->filename
, filename
);
953 e
->ifndef_macro
= ifndef_macro
;
954 dynarray_add((void ***)&s1
->cached_includes
, &s1
->nb_cached_includes
, e
);
955 /* add in hash table */
956 h
= hash_cached_include(type
, filename
);
957 e
->hash_next
= s1
->cached_includes_hash
[h
];
958 s1
->cached_includes_hash
[h
] = s1
->nb_cached_includes
;
961 static void pragma_parse(TCCState
*s1
)
966 if (tok
== TOK_pack
) {
969 #pragma pack(1) // set
970 #pragma pack() // reset to default
971 #pragma pack(push,1) // push & set
972 #pragma pack(pop) // restore previous
976 if (tok
== TOK_ASM_pop
) {
978 if (s1
->pack_stack_ptr
<= s1
->pack_stack
) {
980 error("out of pack stack");
982 s1
->pack_stack_ptr
--;
986 if (tok
== TOK_ASM_push
) {
988 if (s1
->pack_stack_ptr
>= s1
->pack_stack
+ PACK_STACK_SIZE
- 1)
990 s1
->pack_stack_ptr
++;
993 if (tok
!= TOK_CINT
) {
995 error("invalid pack pragma");
998 if (val
< 1 || val
> 16 || (val
& (val
- 1)) != 0)
1002 *s1
->pack_stack_ptr
= val
;
1008 /* is_bof is true if first non space token at beginning of file */
1009 static void preprocess(int is_bof
)
1011 TCCState
*s1
= tcc_state
;
1012 int size
, i
, c
, n
, saved_parse_flags
;
1019 saved_parse_flags
= parse_flags
;
1020 parse_flags
= PARSE_FLAG_PREPROCESS
| PARSE_FLAG_TOK_NUM
|
1021 PARSE_FLAG_LINEFEED
;
1031 s
= define_find(tok
);
1032 /* undefine symbol by putting an invalid name */
1037 case TOK_INCLUDE_NEXT
:
1038 ch
= file
->buf_ptr
[0];
1039 /* XXX: incorrect if comments : use next_nomacro with a special mode */
1044 } else if (ch
== '\"') {
1049 while (ch
!= c
&& ch
!= '\n' && ch
!= CH_EOF
) {
1050 if ((q
- buf
) < sizeof(buf
) - 1)
1053 if (handle_stray_noerror() == 0)
1061 /* eat all spaces and comments after include */
1062 /* XXX: slightly incorrect */
1063 while (ch1
!= '\n' && ch1
!= CH_EOF
)
1067 /* computed #include : either we have only strings or
1068 we have anything enclosed in '<>' */
1071 if (tok
== TOK_STR
) {
1072 while (tok
!= TOK_LINEFEED
) {
1073 if (tok
!= TOK_STR
) {
1075 error("'#include' expects \"FILENAME\" or <FILENAME>");
1077 pstrcat(buf
, sizeof(buf
), (char *)tokc
.cstr
->data
);
1083 while (tok
!= TOK_LINEFEED
) {
1084 pstrcat(buf
, sizeof(buf
), get_tok_str(tok
, &tokc
));
1088 /* check syntax and remove '<>' */
1089 if (len
< 2 || buf
[0] != '<' || buf
[len
- 1] != '>')
1090 goto include_syntax
;
1091 memmove(buf
, buf
+ 1, len
- 2);
1092 buf
[len
- 2] = '\0';
1097 e
= search_cached_include(s1
, c
, buf
);
1098 if (e
&& define_find(e
->ifndef_macro
)) {
1099 /* no need to parse the include because the 'ifndef macro'
1102 printf("%s: skipping %s\n", file
->filename
, buf
);
1105 if (s1
->include_stack_ptr
>= s1
->include_stack
+ INCLUDE_STACK_SIZE
)
1106 error("#include recursion too deep");
1107 /* push current file in stack */
1108 /* XXX: fix current line init */
1109 *s1
->include_stack_ptr
++ = file
;
1111 /* check absolute include path */
1112 if (IS_ABSPATH(buf
)) {
1113 f
= tcc_open(s1
, buf
);
1118 /* first search in current dir if "header.h" */
1119 size
= tcc_basename(file
->filename
) - file
->filename
;
1120 if (size
> sizeof(buf1
) - 1)
1121 size
= sizeof(buf1
) - 1;
1122 memcpy(buf1
, file
->filename
, size
);
1124 pstrcat(buf1
, sizeof(buf1
), buf
);
1125 f
= tcc_open(s1
, buf1
);
1127 if (tok
== TOK_INCLUDE_NEXT
)
1133 /* now search in all the include paths */
1134 n
= s1
->nb_include_paths
+ s1
->nb_sysinclude_paths
;
1135 for(i
= 0; i
< n
; i
++) {
1137 if (i
< s1
->nb_include_paths
)
1138 path
= s1
->include_paths
[i
];
1140 path
= s1
->sysinclude_paths
[i
- s1
->nb_include_paths
];
1141 pstrcpy(buf1
, sizeof(buf1
), path
);
1142 pstrcat(buf1
, sizeof(buf1
), "/");
1143 pstrcat(buf1
, sizeof(buf1
), buf
);
1144 f
= tcc_open(s1
, buf1
);
1146 if (tok
== TOK_INCLUDE_NEXT
)
1152 --s1
->include_stack_ptr
;
1153 error("include file '%s' not found", buf
);
1157 printf("%s: including %s\n", file
->filename
, buf1
);
1160 pstrcpy(f
->inc_filename
, sizeof(f
->inc_filename
), buf
);
1162 /* add include file debug info */
1164 put_stabs(file
->filename
, N_BINCL
, 0, 0, 0);
1166 tok_flags
|= TOK_FLAG_BOF
| TOK_FLAG_BOL
;
1167 ch
= file
->buf_ptr
[0];
1175 c
= expr_preprocess();
1181 if (tok
< TOK_IDENT
)
1182 error("invalid argument for '#if%sdef'", c
? "n" : "");
1186 printf("#ifndef %s\n", get_tok_str(tok
, NULL
));
1188 file
->ifndef_macro
= tok
;
1191 c
= (define_find(tok
) != 0) ^ c
;
1193 if (s1
->ifdef_stack_ptr
>= s1
->ifdef_stack
+ IFDEF_STACK_SIZE
)
1194 error("memory full");
1195 *s1
->ifdef_stack_ptr
++ = c
;
1198 if (s1
->ifdef_stack_ptr
== s1
->ifdef_stack
)
1199 error("#else without matching #if");
1200 if (s1
->ifdef_stack_ptr
[-1] & 2)
1201 error("#else after #else");
1202 c
= (s1
->ifdef_stack_ptr
[-1] ^= 3);
1205 if (s1
->ifdef_stack_ptr
== s1
->ifdef_stack
)
1206 error("#elif without matching #if");
1207 c
= s1
->ifdef_stack_ptr
[-1];
1209 error("#elif after #else");
1210 /* last #if/#elif expression was true: we skip */
1213 c
= expr_preprocess();
1214 s1
->ifdef_stack_ptr
[-1] = c
;
1224 if (s1
->ifdef_stack_ptr
<= file
->ifdef_stack_ptr
)
1225 error("#endif without matching #if");
1226 s1
->ifdef_stack_ptr
--;
1227 /* '#ifndef macro' was at the start of file. Now we check if
1228 an '#endif' is exactly at the end of file */
1229 if (file
->ifndef_macro
&&
1230 s1
->ifdef_stack_ptr
== file
->ifdef_stack_ptr
) {
1231 file
->ifndef_macro_saved
= file
->ifndef_macro
;
1232 /* need to set to zero to avoid false matches if another
1233 #ifndef at middle of file */
1234 file
->ifndef_macro
= 0;
1235 while (tok
!= TOK_LINEFEED
)
1237 tok_flags
|= TOK_FLAG_ENDIF
;
1243 if (tok
!= TOK_CINT
)
1245 file
->line_num
= tokc
.i
- 1; /* the line number will be incremented after */
1247 if (tok
!= TOK_LINEFEED
) {
1250 pstrcpy(file
->filename
, sizeof(file
->filename
),
1251 (char *)tokc
.cstr
->data
);
1257 ch
= file
->buf_ptr
[0];
1260 while (ch
!= '\n' && ch
!= CH_EOF
) {
1261 if ((q
- buf
) < sizeof(buf
) - 1)
1264 if (handle_stray_noerror() == 0)
1271 error("#error %s", buf
);
1273 warning("#warning %s", buf
);
1279 if (tok
== TOK_LINEFEED
|| tok
== '!' || tok
== TOK_CINT
) {
1280 /* '!' is ignored to allow C scripts. numbers are ignored
1281 to emulate cpp behaviour */
1283 if (!(saved_parse_flags
& PARSE_FLAG_ASM_COMMENTS
))
1284 warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok
, &tokc
));
1288 /* ignore other preprocess commands or #! for C scripts */
1289 while (tok
!= TOK_LINEFEED
)
1292 parse_flags
= saved_parse_flags
;
1295 /* evaluate escape codes in a string. */
1296 static void parse_escape_string(CString
*outstr
, const uint8_t *buf
, int is_long
)
1311 case '0': case '1': case '2': case '3':
1312 case '4': case '5': case '6': case '7':
1313 /* at most three octal digits */
1318 n
= n
* 8 + c
- '0';
1322 n
= n
* 8 + c
- '0';
1327 goto add_char_nonext
;
1335 if (c
>= 'a' && c
<= 'f')
1337 else if (c
>= 'A' && c
<= 'F')
1347 goto add_char_nonext
;
1371 goto invalid_escape
;
1381 if (c
>= '!' && c
<= '~')
1382 warning("unknown escape sequence: \'\\%c\'", c
);
1384 warning("unknown escape sequence: \'\\x%x\'", c
);
1391 cstr_ccat(outstr
, c
);
1393 cstr_wccat(outstr
, c
);
1395 /* add a trailing '\0' */
1397 cstr_ccat(outstr
, '\0');
1399 cstr_wccat(outstr
, '\0');
1402 /* we use 64 bit numbers */
1405 /* bn = (bn << shift) | or_val */
1406 void bn_lshift(unsigned int *bn
, int shift
, int or_val
)
1410 for(i
=0;i
<BN_SIZE
;i
++) {
1412 bn
[i
] = (v
<< shift
) | or_val
;
1413 or_val
= v
>> (32 - shift
);
1417 void bn_zero(unsigned int *bn
)
1420 for(i
=0;i
<BN_SIZE
;i
++) {
1425 /* parse number in null terminated string 'p' and return it in the
1427 void parse_number(const char *p
)
1429 int b
, t
, shift
, frac_bits
, s
, exp_val
, ch
;
1431 unsigned int bn
[BN_SIZE
];
1442 goto float_frac_parse
;
1443 } else if (t
== '0') {
1444 if (ch
== 'x' || ch
== 'X') {
1448 } else if (tcc_ext
&& (ch
== 'b' || ch
== 'B')) {
1454 /* parse all digits. cannot check octal numbers at this stage
1455 because of floating point constants */
1457 if (ch
>= 'a' && ch
<= 'f')
1459 else if (ch
>= 'A' && ch
<= 'F')
1467 if (q
>= token_buf
+ STRING_MAX_SIZE
) {
1469 error("number too long");
1475 ((ch
== 'e' || ch
== 'E') && b
== 10) ||
1476 ((ch
== 'p' || ch
== 'P') && (b
== 16 || b
== 2))) {
1478 /* NOTE: strtox should support that for hexa numbers, but
1479 non ISOC99 libcs do not support it, so we prefer to do
1481 /* hexadecimal or binary floats */
1482 /* XXX: handle overflows */
1494 } else if (t
>= 'a') {
1496 } else if (t
>= 'A') {
1501 bn_lshift(bn
, shift
, t
);
1508 if (t
>= 'a' && t
<= 'f') {
1510 } else if (t
>= 'A' && t
<= 'F') {
1512 } else if (t
>= '0' && t
<= '9') {
1518 error("invalid digit");
1519 bn_lshift(bn
, shift
, t
);
1524 if (ch
!= 'p' && ch
!= 'P')
1531 } else if (ch
== '-') {
1535 if (ch
< '0' || ch
> '9')
1536 expect("exponent digits");
1537 while (ch
>= '0' && ch
<= '9') {
1538 exp_val
= exp_val
* 10 + ch
- '0';
1541 exp_val
= exp_val
* s
;
1543 /* now we can generate the number */
1544 /* XXX: should patch directly float number */
1545 d
= (double)bn
[1] * 4294967296.0 + (double)bn
[0];
1546 d
= ldexp(d
, exp_val
- frac_bits
);
1551 /* float : should handle overflow */
1553 } else if (t
== 'L') {
1556 /* XXX: not large enough */
1557 tokc
.ld
= (long double)d
;
1563 /* decimal floats */
1565 if (q
>= token_buf
+ STRING_MAX_SIZE
)
1570 while (ch
>= '0' && ch
<= '9') {
1571 if (q
>= token_buf
+ STRING_MAX_SIZE
)
1577 if (ch
== 'e' || ch
== 'E') {
1578 if (q
>= token_buf
+ STRING_MAX_SIZE
)
1582 if (ch
== '-' || ch
== '+') {
1583 if (q
>= token_buf
+ STRING_MAX_SIZE
)
1588 if (ch
< '0' || ch
> '9')
1589 expect("exponent digits");
1590 while (ch
>= '0' && ch
<= '9') {
1591 if (q
>= token_buf
+ STRING_MAX_SIZE
)
1603 tokc
.f
= strtof(token_buf
, NULL
);
1604 } else if (t
== 'L') {
1607 tokc
.ld
= strtold(token_buf
, NULL
);
1610 tokc
.d
= strtod(token_buf
, NULL
);
1614 unsigned long long n
, n1
;
1617 /* integer number */
1620 if (b
== 10 && *q
== '0') {
1627 /* no need for checks except for base 10 / 8 errors */
1630 } else if (t
>= 'a') {
1632 } else if (t
>= 'A') {
1637 error("invalid digit");
1641 /* detect overflow */
1642 /* XXX: this test is not reliable */
1644 error("integer constant overflow");
1647 /* XXX: not exactly ANSI compliant */
1648 if ((n
& 0xffffffff00000000LL
) != 0) {
1653 } else if (n
> 0x7fffffff) {
1664 error("three 'l's in integer constant");
1667 if (tok
== TOK_CINT
)
1669 else if (tok
== TOK_CUINT
)
1673 } else if (t
== 'U') {
1675 error("two 'u's in integer constant");
1677 if (tok
== TOK_CINT
)
1679 else if (tok
== TOK_CLLONG
)
1686 if (tok
== TOK_CINT
|| tok
== TOK_CUINT
)
1692 error("invalid number\n");
1696 #define PARSE2(c1, tok1, c2, tok2) \
1707 /* return next token without macro substitution */
1708 static inline void next_nomacro1(void)
1723 goto keep_tok_flags
;
1730 /* first look if it is in fact an end of buffer */
1731 if (p
>= file
->buf_end
) {
1735 if (p
>= file
->buf_end
)
1748 TCCState
*s1
= tcc_state
;
1749 if ((parse_flags
& PARSE_FLAG_LINEFEED
)
1750 && !(tok_flags
& TOK_FLAG_EOF
)) {
1751 tok_flags
|= TOK_FLAG_EOF
;
1753 goto keep_tok_flags
;
1754 } else if (s1
->include_stack_ptr
== s1
->include_stack
||
1755 !(parse_flags
& PARSE_FLAG_PREPROCESS
)) {
1756 /* no include left : end of file. */
1759 tok_flags
&= ~TOK_FLAG_EOF
;
1760 /* pop include file */
1762 /* test if previous '#endif' was after a #ifdef at
1764 if (tok_flags
& TOK_FLAG_ENDIF
) {
1766 printf("#endif %s\n", get_tok_str(file
->ifndef_macro_saved
, NULL
));
1768 add_cached_include(s1
, file
->inc_type
, file
->inc_filename
,
1769 file
->ifndef_macro_saved
);
1772 /* add end of include file debug info */
1774 put_stabd(N_EINCL
, 0, 0);
1776 /* pop include stack */
1778 s1
->include_stack_ptr
--;
1779 file
= *s1
->include_stack_ptr
;
1788 tok_flags
|= TOK_FLAG_BOL
;
1790 if (0 == (parse_flags
& PARSE_FLAG_LINEFEED
))
1793 goto keep_tok_flags
;
1798 if ((tok_flags
& TOK_FLAG_BOL
) &&
1799 (parse_flags
& PARSE_FLAG_PREPROCESS
)) {
1801 preprocess(tok_flags
& TOK_FLAG_BOF
);
1807 tok
= TOK_TWOSHARPS
;
1809 if (parse_flags
& PARSE_FLAG_ASM_COMMENTS
) {
1810 p
= parse_line_comment(p
- 1);
1819 case 'a': case 'b': case 'c': case 'd':
1820 case 'e': case 'f': case 'g': case 'h':
1821 case 'i': case 'j': case 'k': case 'l':
1822 case 'm': case 'n': case 'o': case 'p':
1823 case 'q': case 'r': case 's': case 't':
1824 case 'u': case 'v': case 'w': case 'x':
1826 case 'A': case 'B': case 'C': case 'D':
1827 case 'E': case 'F': case 'G': case 'H':
1828 case 'I': case 'J': case 'K':
1829 case 'M': case 'N': case 'O': case 'P':
1830 case 'Q': case 'R': case 'S': case 'T':
1831 case 'U': case 'V': case 'W': case 'X':
1837 h
= TOK_HASH_FUNC(h
, c
);
1841 if (!isidnum_table
[c
-CH_EOF
])
1843 h
= TOK_HASH_FUNC(h
, c
);
1850 /* fast case : no stray found, so we have the full token
1851 and we have already hashed it */
1853 h
&= (TOK_HASH_SIZE
- 1);
1854 pts
= &hash_ident
[h
];
1859 if (ts
->len
== len
&& !memcmp(ts
->str
, p1
, len
))
1861 pts
= &(ts
->hash_next
);
1863 ts
= tok_alloc_new(pts
, p1
, len
);
1867 cstr_reset(&tokcstr
);
1870 cstr_ccat(&tokcstr
, *p1
);
1876 while (isidnum_table
[c
-CH_EOF
]) {
1877 cstr_ccat(&tokcstr
, c
);
1880 ts
= tok_alloc(tokcstr
.data
, tokcstr
.size
);
1886 if (t
!= '\\' && t
!= '\'' && t
!= '\"') {
1888 goto parse_ident_fast
;
1891 if (c
== '\'' || c
== '\"') {
1895 cstr_reset(&tokcstr
);
1896 cstr_ccat(&tokcstr
, 'L');
1897 goto parse_ident_slow
;
1901 case '0': case '1': case '2': case '3':
1902 case '4': case '5': case '6': case '7':
1905 cstr_reset(&tokcstr
);
1906 /* after the first digit, accept digits, alpha, '.' or sign if
1907 prefixed by 'eEpP' */
1911 cstr_ccat(&tokcstr
, c
);
1913 if (!(isnum(c
) || isid(c
) || c
== '.' ||
1914 ((c
== '+' || c
== '-') &&
1915 (t
== 'e' || t
== 'E' || t
== 'p' || t
== 'P'))))
1918 /* We add a trailing '\0' to ease parsing */
1919 cstr_ccat(&tokcstr
, '\0');
1920 tokc
.cstr
= &tokcstr
;
1924 /* special dot handling because it can also start a number */
1927 cstr_reset(&tokcstr
);
1928 cstr_ccat(&tokcstr
, '.');
1930 } else if (c
== '.') {
1950 /* parse the string */
1952 p
= parse_pp_string(p
, sep
, &str
);
1953 cstr_ccat(&str
, '\0');
1955 /* eval the escape (should be done as TOK_PPNUM) */
1956 cstr_reset(&tokcstr
);
1957 parse_escape_string(&tokcstr
, str
.data
, is_long
);
1962 /* XXX: make it portable */
1966 char_size
= sizeof(nwchar_t
);
1967 if (tokcstr
.size
<= char_size
)
1968 error("empty character constant");
1969 if (tokcstr
.size
> 2 * char_size
)
1970 warning("multi-character character constant");
1972 tokc
.i
= *(int8_t *)tokcstr
.data
;
1975 tokc
.i
= *(nwchar_t
*)tokcstr
.data
;
1979 tokc
.cstr
= &tokcstr
;
1993 } else if (c
== '<') {
2011 } else if (c
== '>') {
2029 } else if (c
== '=') {
2042 } else if (c
== '=') {
2055 } else if (c
== '=') {
2068 } else if (c
== '=') {
2071 } else if (c
== '>') {
2079 PARSE2('!', '!', '=', TOK_NE
)
2080 PARSE2('=', '=', '=', TOK_EQ
)
2081 PARSE2('*', '*', '=', TOK_A_MUL
)
2082 PARSE2('%', '%', '=', TOK_A_MOD
)
2083 PARSE2('^', '^', '=', TOK_A_XOR
)
2085 /* comments or operator */
2089 p
= parse_comment(p
);
2091 } else if (c
== '/') {
2092 p
= parse_line_comment(p
);
2094 } else if (c
== '=') {
2114 case '$': /* only used in assembler */
2115 case '@': /* dito */
2120 error("unrecognized character \\x%02x", c
);
2126 #if defined(PARSE_DEBUG)
2127 printf("token = %s\n", get_tok_str(tok
, &tokc
));
2131 /* return next token without macro substitution. Can read input from
2133 static void next_nomacro_spc(void)
2139 TOK_GET(tok
, macro_ptr
, tokc
);
2140 if (tok
== TOK_LINENUM
) {
2141 file
->line_num
= tokc
.i
;
2150 static void next_nomacro(void)
2154 } while (is_space(tok
));
2157 /* substitute args in macro_str and return allocated string */
2158 static int *macro_arg_subst(Sym
**nested_list
, int *macro_str
, Sym
*args
)
2160 int *st
, last_tok
, t
, spc
;
2169 TOK_GET(t
, macro_str
, cval
);
2174 TOK_GET(t
, macro_str
, cval
);
2177 s
= sym_find2(args
, t
);
2183 TOK_GET(t
, st
, cval
);
2184 if (!check_space(t
, &spc
))
2185 cstr_cat(&cstr
, get_tok_str(t
, &cval
));
2188 cstr_ccat(&cstr
, '\0');
2190 printf("stringize: %s\n", (char *)cstr
.data
);
2194 tok_str_add2(&str
, TOK_STR
, &cval
);
2197 tok_str_add2(&str
, t
, &cval
);
2199 } else if (t
>= TOK_IDENT
) {
2200 s
= sym_find2(args
, t
);
2203 /* if '##' is present before or after, no arg substitution */
2204 if (*macro_str
== TOK_TWOSHARPS
|| last_tok
== TOK_TWOSHARPS
) {
2205 /* special case for var arg macros : ## eats the
2206 ',' if empty VA_ARGS variable. */
2207 /* XXX: test of the ',' is not 100%
2208 reliable. should fix it to avoid security
2210 if (gnu_ext
&& s
->type
.t
&&
2211 last_tok
== TOK_TWOSHARPS
&&
2212 str
.len
>= 2 && str
.str
[str
.len
- 2] == ',') {
2214 /* suppress ',' '##' */
2217 /* suppress '##' and add variable */
2225 TOK_GET(t1
, st
, cval
);
2228 tok_str_add2(&str
, t1
, &cval
);
2232 /* NOTE: the stream cannot be read when macro
2233 substituing an argument */
2234 macro_subst(&str
, nested_list
, st
, NULL
);
2237 tok_str_add(&str
, t
);
2240 tok_str_add2(&str
, t
, &cval
);
2244 tok_str_add(&str
, 0);
2248 static char const ab_month_name
[12][4] =
2250 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2251 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
2254 /* do macro substitution of current token with macro 's' and add
2255 result to (tok_str,tok_len). 'nested_list' is the list of all
2256 macros we got inside to avoid recursing. Return non zero if no
2257 substitution needs to be done */
2258 static int macro_subst_tok(TokenString
*tok_str
,
2259 Sym
**nested_list
, Sym
*s
, struct macro_level
**can_read_stream
)
2261 Sym
*args
, *sa
, *sa1
;
2262 int mstr_allocated
, parlevel
, *mstr
, t
, t1
, *p
, spc
;
2269 /* if symbol is a macro, prepare substitution */
2270 /* special macros */
2271 if (tok
== TOK___LINE__
) {
2272 snprintf(buf
, sizeof(buf
), "%d", file
->line_num
);
2276 } else if (tok
== TOK___FILE__
) {
2277 cstrval
= file
->filename
;
2279 } else if (tok
== TOK___DATE__
|| tok
== TOK___TIME__
) {
2284 tm
= localtime(&ti
);
2285 if (tok
== TOK___DATE__
) {
2286 snprintf(buf
, sizeof(buf
), "%s %2d %d",
2287 ab_month_name
[tm
->tm_mon
], tm
->tm_mday
, tm
->tm_year
+ 1900);
2289 snprintf(buf
, sizeof(buf
), "%02d:%02d:%02d",
2290 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
2297 cstr_cat(&cstr
, cstrval
);
2298 cstr_ccat(&cstr
, '\0');
2300 tok_str_add2(tok_str
, t1
, &cval
);
2305 if (s
->type
.t
== MACRO_FUNC
) {
2306 /* NOTE: we do not use next_nomacro to avoid eating the
2307 next token. XXX: find better solution */
2311 while (is_space(t
= *p
) || TOK_LINEFEED
== t
)
2313 if (t
== 0 && can_read_stream
) {
2314 /* end of macro stream: we must look at the token
2315 after in the file */
2316 struct macro_level
*ml
= *can_read_stream
;
2322 *can_read_stream
= ml
-> prev
;
2327 /* XXX: incorrect with comments */
2328 ch
= file
->buf_ptr
[0];
2329 while (is_space(ch
) || ch
== '\n')
2333 if (t
!= '(') /* no macro subst */
2336 /* argument macro */
2341 /* NOTE: empty args are allowed, except if no args */
2343 /* handle '()' case */
2344 if (!args
&& !sa
&& tok
== ')')
2347 error("macro '%s' used with too many args",
2348 get_tok_str(s
->v
, 0));
2351 /* NOTE: non zero sa->t indicates VA_ARGS */
2352 while ((parlevel
> 0 ||
2354 (tok
!= ',' || sa
->type
.t
))) &&
2358 else if (tok
== ')')
2360 if (tok
== TOK_LINEFEED
)
2362 if (!check_space(tok
, &spc
))
2363 tok_str_add2(&str
, tok
, &tokc
);
2367 tok_str_add(&str
, 0);
2368 sym_push2(&args
, sa
->v
& ~SYM_FIELD
, sa
->type
.t
, (long)str
.str
);
2371 /* special case for gcc var args: add an empty
2372 var arg argument if it is omitted */
2373 if (sa
&& sa
->type
.t
&& gnu_ext
)
2383 error("macro '%s' used with too few args",
2384 get_tok_str(s
->v
, 0));
2387 /* now subst each arg */
2388 mstr
= macro_arg_subst(nested_list
, mstr
, args
);
2393 tok_str_free((int *)sa
->c
);
2399 sym_push2(nested_list
, s
->v
, 0, 0);
2400 macro_subst(tok_str
, nested_list
, mstr
, can_read_stream
);
2401 /* pop nested defined symbol */
2403 *nested_list
= sa1
->prev
;
2411 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
2412 return the resulting string (which must be freed). */
2413 static inline int *macro_twosharps(const int *macro_str
)
2416 const int *ptr
, *saved_macro_ptr
;
2418 const char *p1
, *p2
;
2420 TokenString macro_str1
;
2423 /* we search the first '##' */
2424 for(ptr
= macro_str
;;) {
2425 TOK_GET(t
, ptr
, cval
);
2426 if (t
== TOK_TWOSHARPS
)
2428 /* nothing more to do if end of string */
2433 /* we saw '##', so we need more processing to handle it */
2435 tok_str_new(¯o_str1
);
2436 saved_macro_ptr
= macro_ptr
;
2437 /* XXX: get rid of the use of macro_ptr here */
2438 macro_ptr
= (int *)macro_str
;
2443 if (tok
== TOK_TWOSHARPS
)
2445 while (*macro_ptr
== TOK_TWOSHARPS
) {
2447 if (t
&& t
!= TOK_TWOSHARPS
) {
2448 TOK_GET(t
, macro_ptr
, cval
);
2449 /* We concatenate the two tokens if we have an
2450 identifier or a preprocessing number */
2452 p1
= get_tok_str(tok
, &tokc
);
2453 cstr_cat(&cstr
, p1
);
2454 p2
= get_tok_str(t
, &cval
);
2455 cstr_cat(&cstr
, p2
);
2456 cstr_ccat(&cstr
, '\0');
2458 if ((tok
>= TOK_IDENT
|| tok
== TOK_PPNUM
) &&
2459 (t
>= TOK_IDENT
|| t
== TOK_PPNUM
)) {
2460 if (tok
== TOK_PPNUM
) {
2461 /* if number, then create a number token */
2462 /* NOTE: no need to allocate because
2463 tok_str_add2() does it */
2464 cstr_reset(&tokcstr
);
2467 tokc
.cstr
= &tokcstr
;
2469 /* if identifier, we must do a test to
2470 validate we have a correct identifier */
2471 if (t
== TOK_PPNUM
) {
2481 if (!isnum(c
) && !isid(c
))
2485 ts
= tok_alloc(cstr
.data
, strlen(cstr
.data
));
2486 tok
= ts
->tok
; /* modify current token */
2489 const char *str
= cstr
.data
;
2490 const unsigned char *q
;
2492 /* we look for a valid token */
2493 /* XXX: do more extensive checks */
2494 if (!strcmp(str
, ">>=")) {
2496 } else if (!strcmp(str
, "<<=")) {
2498 } else if (strlen(str
) == 2) {
2499 /* search in two bytes table */
2504 if (q
[0] == str
[0] && q
[1] == str
[1])
2511 /* NOTE: because get_tok_str use a static buffer,
2514 p1
= get_tok_str(tok
, &tokc
);
2515 cstr_cat(&cstr
, p1
);
2516 cstr_ccat(&cstr
, '\0');
2517 p2
= get_tok_str(t
, &cval
);
2518 warning("pasting \"%s\" and \"%s\" does not give a valid preprocessing token", cstr
.data
, p2
);
2519 /* cannot merge tokens: just add them separately */
2520 tok_str_add2(¯o_str1
, tok
, &tokc
);
2521 /* XXX: free associated memory ? */
2528 tok_str_add2(¯o_str1
, tok
, &tokc
);
2530 macro_ptr
= (int *)saved_macro_ptr
;
2532 tok_str_add(¯o_str1
, 0);
2533 return macro_str1
.str
;
2537 /* do macro substitution of macro_str and add result to
2538 (tok_str,tok_len). 'nested_list' is the list of all macros we got
2539 inside to avoid recursing. */
2540 static void macro_subst(TokenString
*tok_str
, Sym
**nested_list
,
2541 const int *macro_str
, struct macro_level
** can_read_stream
)
2548 struct macro_level ml
;
2550 /* first scan for '##' operator handling */
2552 macro_str1
= macro_twosharps(ptr
);
2557 /* NOTE: ptr == NULL can only happen if tokens are read from
2558 file stream due to a macro function call */
2561 TOK_GET(t
, ptr
, cval
);
2566 /* if nested substitution, do nothing */
2567 if (sym_find2(*nested_list
, t
))
2570 if (can_read_stream
)
2571 ml
.prev
= *can_read_stream
, *can_read_stream
= &ml
;
2572 macro_ptr
= (int *)ptr
;
2574 ret
= macro_subst_tok(tok_str
, nested_list
, s
, can_read_stream
);
2575 ptr
= (int *)macro_ptr
;
2577 if (can_read_stream
&& *can_read_stream
== &ml
)
2578 *can_read_stream
= ml
.prev
;
2583 if (!check_space(t
, &spc
))
2584 tok_str_add2(tok_str
, t
, &cval
);
2588 tok_str_free(macro_str1
);
2591 /* return next token with macro substitution */
2592 static void next(void)
2594 Sym
*nested_list
, *s
;
2596 struct macro_level
*ml
;
2599 if (parse_flags
& PARSE_FLAG_SPACES
)
2604 /* if not reading from macro substituted string, then try
2605 to substitute macros */
2606 if (tok
>= TOK_IDENT
&&
2607 (parse_flags
& PARSE_FLAG_PREPROCESS
)) {
2608 s
= define_find(tok
);
2610 /* we have a macro: we try to substitute */
2614 if (macro_subst_tok(&str
, &nested_list
, s
, &ml
) == 0) {
2615 /* substitution done, NOTE: maybe empty */
2616 tok_str_add(&str
, 0);
2617 macro_ptr
= str
.str
;
2618 macro_ptr_allocated
= str
.str
;
2625 /* end of macro or end of unget buffer */
2626 if (unget_buffer_enabled
) {
2627 macro_ptr
= unget_saved_macro_ptr
;
2628 unget_buffer_enabled
= 0;
2630 /* end of macro string: free it */
2631 tok_str_free(macro_ptr_allocated
);
2638 /* convert preprocessor tokens into C tokens */
2639 if (tok
== TOK_PPNUM
&&
2640 (parse_flags
& PARSE_FLAG_TOK_NUM
)) {
2641 parse_number((char *)tokc
.cstr
->data
);
2645 /* push back current token and set current token to 'last_tok'. Only
2646 identifier case handled for labels. */
2647 static inline void unget_tok(int last_tok
)
2651 unget_saved_macro_ptr
= macro_ptr
;
2652 unget_buffer_enabled
= 1;
2653 q
= unget_saved_buffer
;
2656 n
= tok_ext_size(tok
) - 1;
2659 *q
= 0; /* end of token string */