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
22 static const char tcc_keywords
[] =
23 #define DEF(id, str) str "\0"
28 /* WARNING: the content of this string encodes token numbers */
29 static char tok_two_chars
[] = "<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\313..\250##\266";
31 /* true if isid(c) || isnum(c) */
32 static unsigned char isidnum_table
[256-CH_EOF
];
36 struct macro_level
*prev
;
40 static void next_nomacro(void);
41 static void next_nomacro_spc(void);
42 static void macro_subst(TokenString
*tok_str
, Sym
**nested_list
,
43 const int *macro_str
, struct macro_level
**can_read_stream
);
46 /* allocate a new token */
47 static TokenSym
*tok_alloc_new(TokenSym
**pts
, const char *str
, int len
)
49 TokenSym
*ts
, **ptable
;
52 if (tok_ident
>= SYM_FIRST_ANOM
)
55 /* expand token table if needed */
56 i
= tok_ident
- TOK_IDENT
;
57 if ((i
% TOK_ALLOC_INCR
) == 0) {
58 ptable
= tcc_realloc(table_ident
, (i
+ TOK_ALLOC_INCR
) * sizeof(TokenSym
*));
64 ts
= tcc_malloc(sizeof(TokenSym
) + len
);
66 ts
->tok
= tok_ident
++;
67 ts
->sym_define
= NULL
;
69 ts
->sym_struct
= NULL
;
70 ts
->sym_identifier
= NULL
;
73 memcpy(ts
->str
, str
, len
);
79 #define TOK_HASH_INIT 1
80 #define TOK_HASH_FUNC(h, c) ((h) * 263 + (c))
82 /* find a token and add it if not found */
83 static TokenSym
*tok_alloc(const char *str
, int len
)
91 h
= TOK_HASH_FUNC(h
, ((unsigned char *)str
)[i
]);
92 h
&= (TOK_HASH_SIZE
- 1);
99 if (ts
->len
== len
&& !memcmp(ts
->str
, str
, len
))
101 pts
= &(ts
->hash_next
);
103 return tok_alloc_new(pts
, str
, len
);
106 /* XXX: buffer overflow */
107 /* XXX: float tokens */
108 char *get_tok_str(int v
, CValue
*cv
)
110 static char buf
[STRING_MAX_SIZE
+ 1];
111 static CString cstr_buf
;
117 /* NOTE: to go faster, we give a fixed buffer for small strings */
118 cstr_reset(&cstr_buf
);
120 cstr_buf
.size_allocated
= sizeof(buf
);
126 /* XXX: not quite exact, but only useful for testing */
127 sprintf(p
, "%u", cv
->ui
);
131 /* XXX: not quite exact, but only useful for testing */
133 sprintf(p
, "%u", (unsigned)cv
->ull
);
135 sprintf(p
, "%Lu", cv
->ull
);
139 cstr_ccat(&cstr_buf
, 'L');
141 cstr_ccat(&cstr_buf
, '\'');
142 add_char(&cstr_buf
, cv
->i
);
143 cstr_ccat(&cstr_buf
, '\'');
144 cstr_ccat(&cstr_buf
, '\0');
148 len
= cstr
->size
- 1;
150 add_char(&cstr_buf
, ((unsigned char *)cstr
->data
)[i
]);
151 cstr_ccat(&cstr_buf
, '\0');
154 cstr_ccat(&cstr_buf
, 'L');
157 cstr_ccat(&cstr_buf
, '\"');
159 len
= cstr
->size
- 1;
161 add_char(&cstr_buf
, ((unsigned char *)cstr
->data
)[i
]);
163 len
= (cstr
->size
/ sizeof(nwchar_t
)) - 1;
165 add_char(&cstr_buf
, ((nwchar_t
*)cstr
->data
)[i
]);
167 cstr_ccat(&cstr_buf
, '\"');
168 cstr_ccat(&cstr_buf
, '\0');
177 return strcpy(p
, "...");
179 return strcpy(p
, "<<=");
181 return strcpy(p
, ">>=");
184 /* search in two bytes table */
198 } else if (v
< tok_ident
) {
199 return table_ident
[v
- TOK_IDENT
]->str
;
200 } else if (v
>= SYM_FIRST_ANOM
) {
201 /* special name for anonymous symbol */
202 sprintf(p
, "L.%u", v
- SYM_FIRST_ANOM
);
204 /* should never happen */
209 return cstr_buf
.data
;
212 /* fill input buffer and peek next char */
213 static int tcc_peekc_slow(BufferedFile
*bf
)
216 /* only tries to read if really end of buffer */
217 if (bf
->buf_ptr
>= bf
->buf_end
) {
219 #if defined(PARSE_DEBUG)
224 len
= read(bf
->fd
, bf
->buffer
, len
);
231 bf
->buf_ptr
= bf
->buffer
;
232 bf
->buf_end
= bf
->buffer
+ len
;
233 *bf
->buf_end
= CH_EOB
;
235 if (bf
->buf_ptr
< bf
->buf_end
) {
236 return bf
->buf_ptr
[0];
238 bf
->buf_ptr
= bf
->buf_end
;
243 /* return the current character, handling end of block if necessary
245 static int handle_eob(void)
247 return tcc_peekc_slow(file
);
250 /* read next char from current input file and handle end of input buffer */
251 static inline void inp(void)
253 ch
= *(++(file
->buf_ptr
));
254 /* end of buffer/file handling */
259 /* handle '\[\r]\n' */
260 static int handle_stray_noerror(void)
267 } else if (ch
== '\r') {
281 static void handle_stray(void)
283 if (handle_stray_noerror())
284 error("stray '\\' in program");
287 /* skip the stray and handle the \\n case. Output an error if
288 incorrect char after the stray */
289 static int handle_stray1(uint8_t *p
)
293 if (p
>= file
->buf_end
) {
310 /* handle just the EOB case, but not stray */
311 #define PEEKC_EOB(c, p)\
322 /* handle the complicated stray case */
328 c = handle_stray1(p);\
333 /* input with '\[\r]\n' handling. Note that this function cannot
334 handle other characters after '\', so you cannot call it inside
335 strings or comments */
336 static void minp(void)
344 /* single line C++ comments */
345 static uint8_t *parse_line_comment(uint8_t *p
)
353 if (c
== '\n' || c
== CH_EOF
) {
355 } else if (c
== '\\') {
364 } else if (c
== '\r') {
382 static uint8_t *parse_comment(uint8_t *p
)
391 if (c
== '\n' || c
== '*' || c
== '\\')
395 if (c
== '\n' || c
== '*' || c
== '\\')
399 /* now we can handle all the cases */
403 } else if (c
== '*') {
409 } else if (c
== '/') {
411 } else if (c
== '\\') {
416 /* skip '\[\r]\n', otherwise just skip the stray */
422 } else if (c
== '\r') {
439 /* stray, eob or eof */
444 error("unexpected end of file in comment");
445 } else if (c
== '\\') {
457 static inline void skip_spaces(void)
463 static inline int check_space(int t
, int *spc
)
474 /* parse a string without interpreting escapes */
475 static uint8_t *parse_pp_string(uint8_t *p
,
476 int sep
, CString
*str
)
484 } else if (c
== '\\') {
490 /* XXX: indicate line number of start of string */
491 error("missing terminating %c character", sep
);
492 } else if (c
== '\\') {
493 /* escape : just skip \[\r]\n */
498 } else if (c
== '\r') {
501 expect("'\n' after '\r'");
504 } else if (c
== CH_EOF
) {
505 goto unterminated_string
;
508 cstr_ccat(str
, '\\');
514 } else if (c
== '\n') {
517 } else if (c
== '\r') {
521 cstr_ccat(str
, '\r');
537 /* skip block of text until #else, #elif or #endif. skip also pairs of
539 void preprocess_skip(void)
541 int a
, start_of_line
, c
, in_warn_or_error
;
548 in_warn_or_error
= 0;
569 } else if (c
== '\\') {
570 ch
= file
->buf_ptr
[0];
571 handle_stray_noerror();
578 if (in_warn_or_error
)
580 p
= parse_pp_string(p
, c
, NULL
);
584 if (in_warn_or_error
)
591 p
= parse_comment(p
);
592 } else if (ch
== '/') {
593 p
= parse_line_comment(p
);
603 (tok
== TOK_ELSE
|| tok
== TOK_ELIF
|| tok
== TOK_ENDIF
))
605 if (tok
== TOK_IF
|| tok
== TOK_IFDEF
|| tok
== TOK_IFNDEF
)
607 else if (tok
== TOK_ENDIF
)
609 else if( tok
== TOK_ERROR
|| tok
== TOK_WARNING
)
610 in_warn_or_error
= 1;
624 /* ParseState handling */
626 /* XXX: currently, no include file info is stored. Thus, we cannot display
627 accurate messages if the function or data definition spans multiple
630 /* save current parse state in 's' */
631 void save_parse_state(ParseState
*s
)
633 s
->line_num
= file
->line_num
;
634 s
->macro_ptr
= macro_ptr
;
639 /* restore parse state from 's' */
640 void restore_parse_state(ParseState
*s
)
642 file
->line_num
= s
->line_num
;
643 macro_ptr
= s
->macro_ptr
;
648 /* return the number of additional 'ints' necessary to store the
650 static inline int tok_ext_size(int t
)
664 error("unsupported token");
671 return LDOUBLE_SIZE
/ 4;
677 /* token string handling */
679 static inline void tok_str_new(TokenString
*s
)
683 s
->allocated_len
= 0;
684 s
->last_line_num
= -1;
687 static void tok_str_free(int *str
)
692 static int *tok_str_realloc(TokenString
*s
)
696 if (s
->allocated_len
== 0) {
699 len
= s
->allocated_len
* 2;
701 str
= tcc_realloc(s
->str
, len
* sizeof(int));
703 error("memory full");
704 s
->allocated_len
= len
;
709 static void tok_str_add(TokenString
*s
, int t
)
715 if (len
>= s
->allocated_len
)
716 str
= tok_str_realloc(s
);
721 static void tok_str_add2(TokenString
*s
, int t
, CValue
*cv
)
728 /* allocate space for worst case */
729 if (len
+ TOK_MAX_SIZE
> s
->allocated_len
)
730 str
= tok_str_realloc(s
);
739 str
[len
++] = cv
->tab
[0];
748 nb_words
= (sizeof(CString
) + cv
->cstr
->size
+ 3) >> 2;
749 while ((len
+ nb_words
) > s
->allocated_len
)
750 str
= tok_str_realloc(s
);
751 cstr
= (CString
*)(str
+ len
);
753 cstr
->size
= cv
->cstr
->size
;
754 cstr
->data_allocated
= NULL
;
755 cstr
->size_allocated
= cstr
->size
;
756 memcpy((char *)cstr
+ sizeof(CString
),
757 cv
->cstr
->data
, cstr
->size
);
764 #if LDOUBLE_SIZE == 8
767 str
[len
++] = cv
->tab
[0];
768 str
[len
++] = cv
->tab
[1];
770 #if LDOUBLE_SIZE == 12
772 str
[len
++] = cv
->tab
[0];
773 str
[len
++] = cv
->tab
[1];
774 str
[len
++] = cv
->tab
[2];
775 #elif LDOUBLE_SIZE == 16
777 str
[len
++] = cv
->tab
[0];
778 str
[len
++] = cv
->tab
[1];
779 str
[len
++] = cv
->tab
[2];
780 str
[len
++] = cv
->tab
[3];
781 #elif LDOUBLE_SIZE != 8
782 #error add long double size support
791 /* add the current parse token in token string 's' */
792 static void tok_str_add_tok(TokenString
*s
)
796 /* save line number info */
797 if (file
->line_num
!= s
->last_line_num
) {
798 s
->last_line_num
= file
->line_num
;
799 cval
.i
= s
->last_line_num
;
800 tok_str_add2(s
, TOK_LINENUM
, &cval
);
802 tok_str_add2(s
, tok
, &tokc
);
805 #if LDOUBLE_SIZE == 16
806 #define LDOUBLE_GET(p, cv) \
811 #elif LDOUBLE_SIZE == 12
812 #define LDOUBLE_GET(p, cv) \
816 #elif LDOUBLE_SIZE == 8
817 #define LDOUBLE_GET(p, cv) \
821 #error add long double size support
825 /* get a token from an integer array and increment pointer
826 accordingly. we code it as a macro to avoid pointer aliasing. */
827 #define TOK_GET(t, p, cv) \
842 cv.cstr = (CString *)p; \
843 cv.cstr->data = (char *)p + sizeof(CString);\
844 p += (sizeof(CString) + cv.cstr->size + 3) >> 2;\
854 LDOUBLE_GET(p, cv); \
855 p += LDOUBLE_SIZE / 4; \
862 /* defines handling */
863 static inline void define_push(int v
, int macro_type
, int *str
, Sym
*first_arg
)
867 s
= sym_push2(&define_stack
, v
, macro_type
, 0);
870 table_ident
[v
- TOK_IDENT
]->sym_define
= s
;
873 /* undefined a define symbol. Its name is just set to zero */
874 static void define_undef(Sym
*s
)
878 if (v
>= TOK_IDENT
&& v
< tok_ident
)
879 table_ident
[v
- TOK_IDENT
]->sym_define
= NULL
;
883 static inline Sym
*define_find(int v
)
886 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
888 return table_ident
[v
]->sym_define
;
891 /* free define stack until top reaches 'b' */
892 static void free_defines(Sym
*b
)
900 /* do not free args or predefined defines */
902 tok_str_free(top
->d
);
904 if (v
>= TOK_IDENT
&& v
< tok_ident
)
905 table_ident
[v
- TOK_IDENT
]->sym_define
= NULL
;
913 static Sym
*label_find(int v
)
916 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
918 return table_ident
[v
]->sym_label
;
921 static Sym
*label_push(Sym
**ptop
, int v
, int flags
)
924 s
= sym_push2(ptop
, v
, 0, 0);
926 ps
= &table_ident
[v
- TOK_IDENT
]->sym_label
;
927 if (ptop
== &global_label_stack
) {
928 /* modify the top most local identifier, so that
929 sym_identifier will point to 's' when popped */
931 ps
= &(*ps
)->prev_tok
;
938 /* pop labels until element last is reached. Look if any labels are
939 undefined. Define symbols if '&&label' was used. */
940 static void label_pop(Sym
**ptop
, Sym
*slast
)
943 for(s
= *ptop
; s
!= slast
; s
= s1
) {
945 if (s
->r
== LABEL_DECLARED
) {
946 warning("label '%s' declared but not used", get_tok_str(s
->v
, NULL
));
947 } else if (s
->r
== LABEL_FORWARD
) {
948 error("label '%s' used but not defined",
949 get_tok_str(s
->v
, NULL
));
952 /* define corresponding symbol. A size of
954 put_extern_sym(s
, cur_text_section
, s
->jnext
, 1);
958 table_ident
[s
->v
- TOK_IDENT
]->sym_label
= s
->prev_tok
;
964 /* eval an expression for #if/#elif */
965 static int expr_preprocess(void)
971 while (tok
!= TOK_LINEFEED
&& tok
!= TOK_EOF
) {
972 next(); /* do macro subst */
973 if (tok
== TOK_DEFINED
) {
978 c
= define_find(tok
) != 0;
983 } else if (tok
>= TOK_IDENT
) {
984 /* if undefined macro */
988 tok_str_add_tok(&str
);
990 tok_str_add(&str
, -1); /* simulate end of file */
991 tok_str_add(&str
, 0);
992 /* now evaluate C constant expression */
997 tok_str_free(str
.str
);
1001 #if defined(PARSE_DEBUG) || defined(PP_DEBUG)
1002 static void tok_print(int *str
)
1009 TOK_GET(t
, str
, cval
);
1012 printf("%s", get_tok_str(t
, &cval
));
1018 /* parse after #define */
1019 static void parse_define(void)
1021 Sym
*s
, *first
, **ps
;
1022 int v
, t
, varg
, is_vaargs
, spc
;
1027 error("invalid macro name '%s'", get_tok_str(tok
, &tokc
));
1028 /* XXX: should check if same macro (ANSI) */
1031 /* '(' must be just after macro definition for MACRO_FUNC */
1036 while (tok
!= ')') {
1040 if (varg
== TOK_DOTS
) {
1041 varg
= TOK___VA_ARGS__
;
1043 } else if (tok
== TOK_DOTS
&& gnu_ext
) {
1047 if (varg
< TOK_IDENT
)
1048 error("badly punctuated parameter list");
1049 s
= sym_push2(&define_stack
, varg
| SYM_FIELD
, is_vaargs
, 0);
1062 /* EOF testing necessary for '-D' handling */
1063 while (tok
!= TOK_LINEFEED
&& tok
!= TOK_EOF
) {
1064 /* remove spaces around ## and after '#' */
1065 if (TOK_TWOSHARPS
== tok
) {
1069 } else if ('#' == tok
) {
1071 } else if (check_space(tok
, &spc
)) {
1074 tok_str_add2(&str
, tok
, &tokc
);
1079 --str
.len
; /* remove trailing space */
1080 tok_str_add(&str
, 0);
1082 printf("define %s %d: ", get_tok_str(v
, NULL
), t
);
1085 define_push(v
, t
, str
.str
, first
);
1088 static inline int hash_cached_include(int type
, const char *filename
)
1090 const unsigned char *s
;
1094 h
= TOK_HASH_FUNC(h
, type
);
1097 h
= TOK_HASH_FUNC(h
, *s
);
1100 h
&= (CACHED_INCLUDES_HASH_SIZE
- 1);
1104 /* XXX: use a token or a hash table to accelerate matching ? */
1105 static CachedInclude
*search_cached_include(TCCState
*s1
,
1106 int type
, const char *filename
)
1110 h
= hash_cached_include(type
, filename
);
1111 i
= s1
->cached_includes_hash
[h
];
1115 e
= s1
->cached_includes
[i
- 1];
1116 if (e
->type
== type
&& !PATHCMP(e
->filename
, filename
))
1123 static inline void add_cached_include(TCCState
*s1
, int type
,
1124 const char *filename
, int ifndef_macro
)
1129 if (search_cached_include(s1
, type
, filename
))
1132 printf("adding cached '%s' %s\n", filename
, get_tok_str(ifndef_macro
, NULL
));
1134 e
= tcc_malloc(sizeof(CachedInclude
) + strlen(filename
));
1138 strcpy(e
->filename
, filename
);
1139 e
->ifndef_macro
= ifndef_macro
;
1140 dynarray_add((void ***)&s1
->cached_includes
, &s1
->nb_cached_includes
, e
);
1141 /* add in hash table */
1142 h
= hash_cached_include(type
, filename
);
1143 e
->hash_next
= s1
->cached_includes_hash
[h
];
1144 s1
->cached_includes_hash
[h
] = s1
->nb_cached_includes
;
1147 static void pragma_parse(TCCState
*s1
)
1152 if (tok
== TOK_pack
) {
1155 #pragma pack(1) // set
1156 #pragma pack() // reset to default
1157 #pragma pack(push,1) // push & set
1158 #pragma pack(pop) // restore previous
1162 if (tok
== TOK_ASM_pop
) {
1164 if (s1
->pack_stack_ptr
<= s1
->pack_stack
) {
1166 error("out of pack stack");
1168 s1
->pack_stack_ptr
--;
1172 if (tok
== TOK_ASM_push
) {
1174 if (s1
->pack_stack_ptr
>= s1
->pack_stack
+ PACK_STACK_SIZE
- 1)
1176 s1
->pack_stack_ptr
++;
1179 if (tok
!= TOK_CINT
) {
1181 error("invalid pack pragma");
1184 if (val
< 1 || val
> 16 || (val
& (val
- 1)) != 0)
1188 *s1
->pack_stack_ptr
= val
;
1194 /* is_bof is true if first non space token at beginning of file */
1195 static void preprocess(int is_bof
)
1197 TCCState
*s1
= tcc_state
;
1198 int i
, c
, n
, saved_parse_flags
;
1202 saved_parse_flags
= parse_flags
;
1203 parse_flags
= PARSE_FLAG_PREPROCESS
| PARSE_FLAG_TOK_NUM
|
1204 PARSE_FLAG_LINEFEED
;
1214 s
= define_find(tok
);
1215 /* undefine symbol by putting an invalid name */
1220 case TOK_INCLUDE_NEXT
:
1221 ch
= file
->buf_ptr
[0];
1222 /* XXX: incorrect if comments : use next_nomacro with a special mode */
1227 } else if (ch
== '\"') {
1232 while (ch
!= c
&& ch
!= '\n' && ch
!= CH_EOF
) {
1233 if ((q
- buf
) < sizeof(buf
) - 1)
1236 if (handle_stray_noerror() == 0)
1244 /* eat all spaces and comments after include */
1245 /* XXX: slightly incorrect */
1246 while (ch1
!= '\n' && ch1
!= CH_EOF
)
1250 /* computed #include : either we have only strings or
1251 we have anything enclosed in '<>' */
1254 if (tok
== TOK_STR
) {
1255 while (tok
!= TOK_LINEFEED
) {
1256 if (tok
!= TOK_STR
) {
1258 error("'#include' expects \"FILENAME\" or <FILENAME>");
1260 pstrcat(buf
, sizeof(buf
), (char *)tokc
.cstr
->data
);
1266 while (tok
!= TOK_LINEFEED
) {
1267 pstrcat(buf
, sizeof(buf
), get_tok_str(tok
, &tokc
));
1271 /* check syntax and remove '<>' */
1272 if (len
< 2 || buf
[0] != '<' || buf
[len
- 1] != '>')
1273 goto include_syntax
;
1274 memmove(buf
, buf
+ 1, len
- 2);
1275 buf
[len
- 2] = '\0';
1280 if (s1
->include_stack_ptr
>= s1
->include_stack
+ INCLUDE_STACK_SIZE
)
1281 error("#include recursion too deep");
1283 n
= s1
->nb_include_paths
+ s1
->nb_sysinclude_paths
;
1284 for (i
= -2; i
< n
; ++i
) {
1285 char buf1
[sizeof file
->filename
];
1292 /* check absolute include path */
1293 if (!IS_ABSPATH(buf
))
1297 } else if (i
== -1) {
1298 /* search in current dir if "header.h" */
1301 size
= tcc_basename(file
->filename
) - file
->filename
;
1302 memcpy(buf1
, file
->filename
, size
);
1306 /* search in all the include paths */
1307 if (i
< s1
->nb_include_paths
)
1308 path
= s1
->include_paths
[i
];
1310 path
= s1
->sysinclude_paths
[i
- s1
->nb_include_paths
];
1311 pstrcpy(buf1
, sizeof(buf1
), path
);
1312 pstrcat(buf1
, sizeof(buf1
), "/");
1315 pstrcat(buf1
, sizeof(buf1
), buf
);
1317 e
= search_cached_include(s1
, c
, buf1
);
1318 if (e
&& define_find(e
->ifndef_macro
)) {
1319 /* no need to parse the include because the 'ifndef macro'
1322 printf("%s: skipping %s\n", file
->filename
, buf
);
1326 f
= tcc_open(s1
, buf1
);
1331 if (tok
== TOK_INCLUDE_NEXT
) {
1342 printf("%s: including %s\n", file
->filename
, buf1
);
1345 /* XXX: fix current line init */
1346 /* push current file in stack */
1347 *s1
->include_stack_ptr
++ = file
;
1349 pstrcpy(f
->inc_filename
, sizeof(f
->inc_filename
), buf1
);
1351 /* add include file debug info */
1352 if (tcc_state
->do_debug
) {
1353 put_stabs(file
->filename
, N_BINCL
, 0, 0, 0);
1355 tok_flags
|= TOK_FLAG_BOF
| TOK_FLAG_BOL
;
1356 ch
= file
->buf_ptr
[0];
1359 error("include file '%s' not found", buf
);
1366 c
= expr_preprocess();
1372 if (tok
< TOK_IDENT
)
1373 error("invalid argument for '#if%sdef'", c
? "n" : "");
1377 printf("#ifndef %s\n", get_tok_str(tok
, NULL
));
1379 file
->ifndef_macro
= tok
;
1382 c
= (define_find(tok
) != 0) ^ c
;
1384 if (s1
->ifdef_stack_ptr
>= s1
->ifdef_stack
+ IFDEF_STACK_SIZE
)
1385 error("memory full");
1386 *s1
->ifdef_stack_ptr
++ = c
;
1389 if (s1
->ifdef_stack_ptr
== s1
->ifdef_stack
)
1390 error("#else without matching #if");
1391 if (s1
->ifdef_stack_ptr
[-1] & 2)
1392 error("#else after #else");
1393 c
= (s1
->ifdef_stack_ptr
[-1] ^= 3);
1396 if (s1
->ifdef_stack_ptr
== s1
->ifdef_stack
)
1397 error("#elif without matching #if");
1398 c
= s1
->ifdef_stack_ptr
[-1];
1400 error("#elif after #else");
1401 /* last #if/#elif expression was true: we skip */
1404 c
= expr_preprocess();
1405 s1
->ifdef_stack_ptr
[-1] = c
;
1415 if (s1
->ifdef_stack_ptr
<= file
->ifdef_stack_ptr
)
1416 error("#endif without matching #if");
1417 s1
->ifdef_stack_ptr
--;
1418 /* '#ifndef macro' was at the start of file. Now we check if
1419 an '#endif' is exactly at the end of file */
1420 if (file
->ifndef_macro
&&
1421 s1
->ifdef_stack_ptr
== file
->ifdef_stack_ptr
) {
1422 file
->ifndef_macro_saved
= file
->ifndef_macro
;
1423 /* need to set to zero to avoid false matches if another
1424 #ifndef at middle of file */
1425 file
->ifndef_macro
= 0;
1426 while (tok
!= TOK_LINEFEED
)
1428 tok_flags
|= TOK_FLAG_ENDIF
;
1434 if (tok
!= TOK_CINT
)
1436 file
->line_num
= tokc
.i
- 1; /* the line number will be incremented after */
1438 if (tok
!= TOK_LINEFEED
) {
1441 pstrcpy(file
->filename
, sizeof(file
->filename
),
1442 (char *)tokc
.cstr
->data
);
1448 ch
= file
->buf_ptr
[0];
1451 while (ch
!= '\n' && ch
!= CH_EOF
) {
1452 if ((q
- buf
) < sizeof(buf
) - 1)
1455 if (handle_stray_noerror() == 0)
1462 error("#error %s", buf
);
1464 warning("#warning %s", buf
);
1470 if (tok
== TOK_LINEFEED
|| tok
== '!' || tok
== TOK_CINT
) {
1471 /* '!' is ignored to allow C scripts. numbers are ignored
1472 to emulate cpp behaviour */
1474 if (!(saved_parse_flags
& PARSE_FLAG_ASM_COMMENTS
))
1475 warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok
, &tokc
));
1479 /* ignore other preprocess commands or #! for C scripts */
1480 while (tok
!= TOK_LINEFEED
)
1483 parse_flags
= saved_parse_flags
;
1486 /* evaluate escape codes in a string. */
1487 static void parse_escape_string(CString
*outstr
, const uint8_t *buf
, int is_long
)
1502 case '0': case '1': case '2': case '3':
1503 case '4': case '5': case '6': case '7':
1504 /* at most three octal digits */
1509 n
= n
* 8 + c
- '0';
1513 n
= n
* 8 + c
- '0';
1518 goto add_char_nonext
;
1526 if (c
>= 'a' && c
<= 'f')
1528 else if (c
>= 'A' && c
<= 'F')
1538 goto add_char_nonext
;
1562 goto invalid_escape
;
1572 if (c
>= '!' && c
<= '~')
1573 warning("unknown escape sequence: \'\\%c\'", c
);
1575 warning("unknown escape sequence: \'\\x%x\'", c
);
1582 cstr_ccat(outstr
, c
);
1584 cstr_wccat(outstr
, c
);
1586 /* add a trailing '\0' */
1588 cstr_ccat(outstr
, '\0');
1590 cstr_wccat(outstr
, '\0');
1593 /* we use 64 bit numbers */
1596 /* bn = (bn << shift) | or_val */
1597 void bn_lshift(unsigned int *bn
, int shift
, int or_val
)
1601 for(i
=0;i
<BN_SIZE
;i
++) {
1603 bn
[i
] = (v
<< shift
) | or_val
;
1604 or_val
= v
>> (32 - shift
);
1608 void bn_zero(unsigned int *bn
)
1611 for(i
=0;i
<BN_SIZE
;i
++) {
1616 /* parse number in null terminated string 'p' and return it in the
1618 void parse_number(const char *p
)
1620 int b
, t
, shift
, frac_bits
, s
, exp_val
, ch
;
1622 unsigned int bn
[BN_SIZE
];
1633 goto float_frac_parse
;
1634 } else if (t
== '0') {
1635 if (ch
== 'x' || ch
== 'X') {
1639 } else if (tcc_ext
&& (ch
== 'b' || ch
== 'B')) {
1645 /* parse all digits. cannot check octal numbers at this stage
1646 because of floating point constants */
1648 if (ch
>= 'a' && ch
<= 'f')
1650 else if (ch
>= 'A' && ch
<= 'F')
1658 if (q
>= token_buf
+ STRING_MAX_SIZE
) {
1660 error("number too long");
1666 ((ch
== 'e' || ch
== 'E') && b
== 10) ||
1667 ((ch
== 'p' || ch
== 'P') && (b
== 16 || b
== 2))) {
1669 /* NOTE: strtox should support that for hexa numbers, but
1670 non ISOC99 libcs do not support it, so we prefer to do
1672 /* hexadecimal or binary floats */
1673 /* XXX: handle overflows */
1685 } else if (t
>= 'a') {
1687 } else if (t
>= 'A') {
1692 bn_lshift(bn
, shift
, t
);
1699 if (t
>= 'a' && t
<= 'f') {
1701 } else if (t
>= 'A' && t
<= 'F') {
1703 } else if (t
>= '0' && t
<= '9') {
1709 error("invalid digit");
1710 bn_lshift(bn
, shift
, t
);
1715 if (ch
!= 'p' && ch
!= 'P')
1722 } else if (ch
== '-') {
1726 if (ch
< '0' || ch
> '9')
1727 expect("exponent digits");
1728 while (ch
>= '0' && ch
<= '9') {
1729 exp_val
= exp_val
* 10 + ch
- '0';
1732 exp_val
= exp_val
* s
;
1734 /* now we can generate the number */
1735 /* XXX: should patch directly float number */
1736 d
= (double)bn
[1] * 4294967296.0 + (double)bn
[0];
1737 d
= ldexp(d
, exp_val
- frac_bits
);
1742 /* float : should handle overflow */
1744 } else if (t
== 'L') {
1746 #ifdef TCC_TARGET_PE
1751 /* XXX: not large enough */
1752 tokc
.ld
= (long double)d
;
1759 /* decimal floats */
1761 if (q
>= token_buf
+ STRING_MAX_SIZE
)
1766 while (ch
>= '0' && ch
<= '9') {
1767 if (q
>= token_buf
+ STRING_MAX_SIZE
)
1773 if (ch
== 'e' || ch
== 'E') {
1774 if (q
>= token_buf
+ STRING_MAX_SIZE
)
1778 if (ch
== '-' || ch
== '+') {
1779 if (q
>= token_buf
+ STRING_MAX_SIZE
)
1784 if (ch
< '0' || ch
> '9')
1785 expect("exponent digits");
1786 while (ch
>= '0' && ch
<= '9') {
1787 if (q
>= token_buf
+ STRING_MAX_SIZE
)
1799 tokc
.f
= strtof(token_buf
, NULL
);
1800 } else if (t
== 'L') {
1802 #ifdef TCC_TARGET_PE
1804 tokc
.d
= strtod(token_buf
, NULL
);
1807 tokc
.ld
= strtold(token_buf
, NULL
);
1811 tokc
.d
= strtod(token_buf
, NULL
);
1815 unsigned long long n
, n1
;
1818 /* integer number */
1821 if (b
== 10 && *q
== '0') {
1828 /* no need for checks except for base 10 / 8 errors */
1831 } else if (t
>= 'a') {
1833 } else if (t
>= 'A') {
1838 error("invalid digit");
1842 /* detect overflow */
1843 /* XXX: this test is not reliable */
1845 error("integer constant overflow");
1848 /* XXX: not exactly ANSI compliant */
1849 if ((n
& 0xffffffff00000000LL
) != 0) {
1854 } else if (n
> 0x7fffffff) {
1865 error("three 'l's in integer constant");
1868 if (tok
== TOK_CINT
)
1870 else if (tok
== TOK_CUINT
)
1874 } else if (t
== 'U') {
1876 error("two 'u's in integer constant");
1878 if (tok
== TOK_CINT
)
1880 else if (tok
== TOK_CLLONG
)
1887 if (tok
== TOK_CINT
|| tok
== TOK_CUINT
)
1893 error("invalid number\n");
1897 #define PARSE2(c1, tok1, c2, tok2) \
1908 /* return next token without macro substitution */
1909 static inline void next_nomacro1(void)
1924 goto keep_tok_flags
;
1931 /* first look if it is in fact an end of buffer */
1932 if (p
>= file
->buf_end
) {
1936 if (p
>= file
->buf_end
)
1949 TCCState
*s1
= tcc_state
;
1950 if ((parse_flags
& PARSE_FLAG_LINEFEED
)
1951 && !(tok_flags
& TOK_FLAG_EOF
)) {
1952 tok_flags
|= TOK_FLAG_EOF
;
1954 goto keep_tok_flags
;
1955 } else if (s1
->include_stack_ptr
== s1
->include_stack
||
1956 !(parse_flags
& PARSE_FLAG_PREPROCESS
)) {
1957 /* no include left : end of file. */
1960 tok_flags
&= ~TOK_FLAG_EOF
;
1961 /* pop include file */
1963 /* test if previous '#endif' was after a #ifdef at
1965 if (tok_flags
& TOK_FLAG_ENDIF
) {
1967 printf("#endif %s\n", get_tok_str(file
->ifndef_macro_saved
, NULL
));
1969 add_cached_include(s1
, file
->inc_type
, file
->inc_filename
,
1970 file
->ifndef_macro_saved
);
1973 /* add end of include file debug info */
1974 if (tcc_state
->do_debug
) {
1975 put_stabd(N_EINCL
, 0, 0);
1977 /* pop include stack */
1979 s1
->include_stack_ptr
--;
1980 file
= *s1
->include_stack_ptr
;
1989 tok_flags
|= TOK_FLAG_BOL
;
1992 if (0 == (parse_flags
& PARSE_FLAG_LINEFEED
))
1995 goto keep_tok_flags
;
2000 if ((tok_flags
& TOK_FLAG_BOL
) &&
2001 (parse_flags
& PARSE_FLAG_PREPROCESS
)) {
2003 preprocess(tok_flags
& TOK_FLAG_BOF
);
2009 tok
= TOK_TWOSHARPS
;
2011 if (parse_flags
& PARSE_FLAG_ASM_COMMENTS
) {
2012 p
= parse_line_comment(p
- 1);
2021 case 'a': case 'b': case 'c': case 'd':
2022 case 'e': case 'f': case 'g': case 'h':
2023 case 'i': case 'j': case 'k': case 'l':
2024 case 'm': case 'n': case 'o': case 'p':
2025 case 'q': case 'r': case 's': case 't':
2026 case 'u': case 'v': case 'w': case 'x':
2028 case 'A': case 'B': case 'C': case 'D':
2029 case 'E': case 'F': case 'G': case 'H':
2030 case 'I': case 'J': case 'K':
2031 case 'M': case 'N': case 'O': case 'P':
2032 case 'Q': case 'R': case 'S': case 'T':
2033 case 'U': case 'V': case 'W': case 'X':
2039 h
= TOK_HASH_FUNC(h
, c
);
2043 if (!isidnum_table
[c
-CH_EOF
])
2045 h
= TOK_HASH_FUNC(h
, c
);
2052 /* fast case : no stray found, so we have the full token
2053 and we have already hashed it */
2055 h
&= (TOK_HASH_SIZE
- 1);
2056 pts
= &hash_ident
[h
];
2061 if (ts
->len
== len
&& !memcmp(ts
->str
, p1
, len
))
2063 pts
= &(ts
->hash_next
);
2065 ts
= tok_alloc_new(pts
, p1
, len
);
2069 cstr_reset(&tokcstr
);
2072 cstr_ccat(&tokcstr
, *p1
);
2078 while (isidnum_table
[c
-CH_EOF
]) {
2079 cstr_ccat(&tokcstr
, c
);
2082 ts
= tok_alloc(tokcstr
.data
, tokcstr
.size
);
2088 if (t
!= '\\' && t
!= '\'' && t
!= '\"') {
2090 goto parse_ident_fast
;
2093 if (c
== '\'' || c
== '\"') {
2097 cstr_reset(&tokcstr
);
2098 cstr_ccat(&tokcstr
, 'L');
2099 goto parse_ident_slow
;
2103 case '0': case '1': case '2': case '3':
2104 case '4': case '5': case '6': case '7':
2107 cstr_reset(&tokcstr
);
2108 /* after the first digit, accept digits, alpha, '.' or sign if
2109 prefixed by 'eEpP' */
2113 cstr_ccat(&tokcstr
, c
);
2115 if (!(isnum(c
) || isid(c
) || c
== '.' ||
2116 ((c
== '+' || c
== '-') &&
2117 (t
== 'e' || t
== 'E' || t
== 'p' || t
== 'P'))))
2120 /* We add a trailing '\0' to ease parsing */
2121 cstr_ccat(&tokcstr
, '\0');
2122 tokc
.cstr
= &tokcstr
;
2126 /* special dot handling because it can also start a number */
2129 cstr_reset(&tokcstr
);
2130 cstr_ccat(&tokcstr
, '.');
2132 } else if (c
== '.') {
2152 /* parse the string */
2154 p
= parse_pp_string(p
, sep
, &str
);
2155 cstr_ccat(&str
, '\0');
2157 /* eval the escape (should be done as TOK_PPNUM) */
2158 cstr_reset(&tokcstr
);
2159 parse_escape_string(&tokcstr
, str
.data
, is_long
);
2164 /* XXX: make it portable */
2168 char_size
= sizeof(nwchar_t
);
2169 if (tokcstr
.size
<= char_size
)
2170 error("empty character constant");
2171 if (tokcstr
.size
> 2 * char_size
)
2172 warning("multi-character character constant");
2174 tokc
.i
= *(int8_t *)tokcstr
.data
;
2177 tokc
.i
= *(nwchar_t
*)tokcstr
.data
;
2181 tokc
.cstr
= &tokcstr
;
2195 } else if (c
== '<') {
2213 } else if (c
== '>') {
2231 } else if (c
== '=') {
2244 } else if (c
== '=') {
2257 } else if (c
== '=') {
2270 } else if (c
== '=') {
2273 } else if (c
== '>') {
2281 PARSE2('!', '!', '=', TOK_NE
)
2282 PARSE2('=', '=', '=', TOK_EQ
)
2283 PARSE2('*', '*', '=', TOK_A_MUL
)
2284 PARSE2('%', '%', '=', TOK_A_MOD
)
2285 PARSE2('^', '^', '=', TOK_A_XOR
)
2287 /* comments or operator */
2291 p
= parse_comment(p
);
2293 } else if (c
== '/') {
2294 p
= parse_line_comment(p
);
2296 } else if (c
== '=') {
2316 case '$': /* only used in assembler */
2317 case '@': /* dito */
2322 error("unrecognized character \\x%02x", c
);
2328 #if defined(PARSE_DEBUG)
2329 printf("token = %s\n", get_tok_str(tok
, &tokc
));
2333 /* return next token without macro substitution. Can read input from
2335 static void next_nomacro_spc(void)
2341 TOK_GET(tok
, macro_ptr
, tokc
);
2342 if (tok
== TOK_LINENUM
) {
2343 file
->line_num
= tokc
.i
;
2352 static void next_nomacro(void)
2356 } while (is_space(tok
));
2359 /* substitute args in macro_str and return allocated string */
2360 static int *macro_arg_subst(Sym
**nested_list
, int *macro_str
, Sym
*args
)
2362 int *st
, last_tok
, t
, spc
;
2371 TOK_GET(t
, macro_str
, cval
);
2376 TOK_GET(t
, macro_str
, cval
);
2379 s
= sym_find2(args
, t
);
2385 TOK_GET(t
, st
, cval
);
2386 if (!check_space(t
, &spc
))
2387 cstr_cat(&cstr
, get_tok_str(t
, &cval
));
2390 cstr_ccat(&cstr
, '\0');
2392 printf("stringize: %s\n", (char *)cstr
.data
);
2396 tok_str_add2(&str
, TOK_STR
, &cval
);
2399 tok_str_add2(&str
, t
, &cval
);
2401 } else if (t
>= TOK_IDENT
) {
2402 s
= sym_find2(args
, t
);
2405 /* if '##' is present before or after, no arg substitution */
2406 if (*macro_str
== TOK_TWOSHARPS
|| last_tok
== TOK_TWOSHARPS
) {
2407 /* special case for var arg macros : ## eats the
2408 ',' if empty VA_ARGS variable. */
2409 /* XXX: test of the ',' is not 100%
2410 reliable. should fix it to avoid security
2412 if (gnu_ext
&& s
->type
.t
&&
2413 last_tok
== TOK_TWOSHARPS
&&
2414 str
.len
>= 2 && str
.str
[str
.len
- 2] == ',') {
2416 /* suppress ',' '##' */
2419 /* suppress '##' and add variable */
2427 TOK_GET(t1
, st
, cval
);
2430 tok_str_add2(&str
, t1
, &cval
);
2434 /* NOTE: the stream cannot be read when macro
2435 substituing an argument */
2436 macro_subst(&str
, nested_list
, st
, NULL
);
2439 tok_str_add(&str
, t
);
2442 tok_str_add2(&str
, t
, &cval
);
2446 tok_str_add(&str
, 0);
2450 static char const ab_month_name
[12][4] =
2452 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2453 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
2456 /* do macro substitution of current token with macro 's' and add
2457 result to (tok_str,tok_len). 'nested_list' is the list of all
2458 macros we got inside to avoid recursing. Return non zero if no
2459 substitution needs to be done */
2460 static int macro_subst_tok(TokenString
*tok_str
,
2461 Sym
**nested_list
, Sym
*s
, struct macro_level
**can_read_stream
)
2463 Sym
*args
, *sa
, *sa1
;
2464 int mstr_allocated
, parlevel
, *mstr
, t
, t1
, *p
, spc
;
2471 /* if symbol is a macro, prepare substitution */
2472 /* special macros */
2473 if (tok
== TOK___LINE__
) {
2474 snprintf(buf
, sizeof(buf
), "%d", file
->line_num
);
2478 } else if (tok
== TOK___FILE__
) {
2479 cstrval
= file
->filename
;
2481 } else if (tok
== TOK___DATE__
|| tok
== TOK___TIME__
) {
2486 tm
= localtime(&ti
);
2487 if (tok
== TOK___DATE__
) {
2488 snprintf(buf
, sizeof(buf
), "%s %2d %d",
2489 ab_month_name
[tm
->tm_mon
], tm
->tm_mday
, tm
->tm_year
+ 1900);
2491 snprintf(buf
, sizeof(buf
), "%02d:%02d:%02d",
2492 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
2499 cstr_cat(&cstr
, cstrval
);
2500 cstr_ccat(&cstr
, '\0');
2502 tok_str_add2(tok_str
, t1
, &cval
);
2507 if (s
->type
.t
== MACRO_FUNC
) {
2508 /* NOTE: we do not use next_nomacro to avoid eating the
2509 next token. XXX: find better solution */
2513 while (is_space(t
= *p
) || TOK_LINEFEED
== t
)
2515 if (t
== 0 && can_read_stream
) {
2516 /* end of macro stream: we must look at the token
2517 after in the file */
2518 struct macro_level
*ml
= *can_read_stream
;
2524 *can_read_stream
= ml
-> prev
;
2529 /* XXX: incorrect with comments */
2530 ch
= file
->buf_ptr
[0];
2531 while (is_space(ch
) || ch
== '\n')
2535 if (t
!= '(') /* no macro subst */
2538 /* argument macro */
2543 /* NOTE: empty args are allowed, except if no args */
2545 /* handle '()' case */
2546 if (!args
&& !sa
&& tok
== ')')
2549 error("macro '%s' used with too many args",
2550 get_tok_str(s
->v
, 0));
2553 /* NOTE: non zero sa->t indicates VA_ARGS */
2554 while ((parlevel
> 0 ||
2556 (tok
!= ',' || sa
->type
.t
))) &&
2560 else if (tok
== ')')
2562 if (tok
== TOK_LINEFEED
)
2564 if (!check_space(tok
, &spc
))
2565 tok_str_add2(&str
, tok
, &tokc
);
2569 tok_str_add(&str
, 0);
2570 sa1
= sym_push2(&args
, sa
->v
& ~SYM_FIELD
, sa
->type
.t
, 0);
2574 /* special case for gcc var args: add an empty
2575 var arg argument if it is omitted */
2576 if (sa
&& sa
->type
.t
&& gnu_ext
)
2586 error("macro '%s' used with too few args",
2587 get_tok_str(s
->v
, 0));
2590 /* now subst each arg */
2591 mstr
= macro_arg_subst(nested_list
, mstr
, args
);
2596 tok_str_free(sa
->d
);
2602 sym_push2(nested_list
, s
->v
, 0, 0);
2603 macro_subst(tok_str
, nested_list
, mstr
, can_read_stream
);
2604 /* pop nested defined symbol */
2606 *nested_list
= sa1
->prev
;
2614 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
2615 return the resulting string (which must be freed). */
2616 static inline int *macro_twosharps(const int *macro_str
)
2621 TokenString macro_str1
;
2626 /* we search the first '##' */
2627 for(ptr
= macro_str
;;) {
2628 TOK_GET(t
, ptr
, cval
);
2629 if (t
== TOK_TWOSHARPS
)
2631 /* nothing more to do if end of string */
2636 /* we saw '##', so we need more processing to handle it */
2637 tok_str_new(¯o_str1
);
2638 for(ptr
= macro_str
;;) {
2639 TOK_GET(tok
, ptr
, tokc
);
2642 if (tok
== TOK_TWOSHARPS
)
2644 while (*ptr
== TOK_TWOSHARPS
) {
2646 if (t
&& t
!= TOK_TWOSHARPS
) {
2647 TOK_GET(t
, ptr
, cval
);
2649 /* We concatenate the two tokens */
2651 cstr_cat(&cstr
, get_tok_str(tok
, &tokc
));
2653 cstr_cat(&cstr
, get_tok_str(t
, &cval
));
2654 cstr_ccat(&cstr
, '\0');
2657 file
->buf_ptr
= cstr
.data
;
2660 if (0 == *file
->buf_ptr
)
2662 tok_str_add2(¯o_str1
, tok
, &tokc
);
2664 warning("pasting \"%.*s\" and \"%s\" does not give a valid preprocessing token",
2665 n
, cstr
.data
, (char*)cstr
.data
+ n
);
2671 tok_str_add2(¯o_str1
, tok
, &tokc
);
2673 tok_str_add(¯o_str1
, 0);
2674 return macro_str1
.str
;
2678 /* do macro substitution of macro_str and add result to
2679 (tok_str,tok_len). 'nested_list' is the list of all macros we got
2680 inside to avoid recursing. */
2681 static void macro_subst(TokenString
*tok_str
, Sym
**nested_list
,
2682 const int *macro_str
, struct macro_level
** can_read_stream
)
2689 struct macro_level ml
;
2691 /* first scan for '##' operator handling */
2693 macro_str1
= macro_twosharps(ptr
);
2698 /* NOTE: ptr == NULL can only happen if tokens are read from
2699 file stream due to a macro function call */
2702 TOK_GET(t
, ptr
, cval
);
2707 /* if nested substitution, do nothing */
2708 if (sym_find2(*nested_list
, t
))
2711 if (can_read_stream
)
2712 ml
.prev
= *can_read_stream
, *can_read_stream
= &ml
;
2713 macro_ptr
= (int *)ptr
;
2715 ret
= macro_subst_tok(tok_str
, nested_list
, s
, can_read_stream
);
2716 ptr
= (int *)macro_ptr
;
2718 if (can_read_stream
&& *can_read_stream
== &ml
)
2719 *can_read_stream
= ml
.prev
;
2724 if (!check_space(t
, &spc
))
2725 tok_str_add2(tok_str
, t
, &cval
);
2729 tok_str_free(macro_str1
);
2732 /* return next token with macro substitution */
2733 static void next(void)
2735 Sym
*nested_list
, *s
;
2737 struct macro_level
*ml
;
2740 if (parse_flags
& PARSE_FLAG_SPACES
)
2745 /* if not reading from macro substituted string, then try
2746 to substitute macros */
2747 if (tok
>= TOK_IDENT
&&
2748 (parse_flags
& PARSE_FLAG_PREPROCESS
)) {
2749 s
= define_find(tok
);
2751 /* we have a macro: we try to substitute */
2755 if (macro_subst_tok(&str
, &nested_list
, s
, &ml
) == 0) {
2756 /* substitution done, NOTE: maybe empty */
2757 tok_str_add(&str
, 0);
2758 macro_ptr
= str
.str
;
2759 macro_ptr_allocated
= str
.str
;
2766 /* end of macro or end of unget buffer */
2767 if (unget_buffer_enabled
) {
2768 macro_ptr
= unget_saved_macro_ptr
;
2769 unget_buffer_enabled
= 0;
2771 /* end of macro string: free it */
2772 tok_str_free(macro_ptr_allocated
);
2779 /* convert preprocessor tokens into C tokens */
2780 if (tok
== TOK_PPNUM
&&
2781 (parse_flags
& PARSE_FLAG_TOK_NUM
)) {
2782 parse_number((char *)tokc
.cstr
->data
);
2786 /* push back current token and set current token to 'last_tok'. Only
2787 identifier case handled for labels. */
2788 static inline void unget_tok(int last_tok
)
2792 unget_saved_macro_ptr
= macro_ptr
;
2793 unget_buffer_enabled
= 1;
2794 q
= unget_saved_buffer
;
2797 n
= tok_ext_size(tok
) - 1;
2800 *q
= 0; /* end of token string */
2805 /* better than nothing, but needs extension to handle '-E' option
2807 static void preprocess_init(TCCState
*s1
)
2809 s1
->include_stack_ptr
= s1
->include_stack
;
2810 /* XXX: move that before to avoid having to initialize
2811 file->ifdef_stack_ptr ? */
2812 s1
->ifdef_stack_ptr
= s1
->ifdef_stack
;
2813 file
->ifdef_stack_ptr
= s1
->ifdef_stack_ptr
;
2815 /* XXX: not ANSI compliant: bound checking says error */
2817 s1
->pack_stack
[0] = 0;
2818 s1
->pack_stack_ptr
= s1
->pack_stack
;
2821 void preprocess_new()
2827 /* init isid table */
2828 for(i
=CH_EOF
;i
<256;i
++)
2829 isidnum_table
[i
-CH_EOF
] = isid(i
) || isnum(i
);
2831 /* add all tokens */
2833 memset(hash_ident
, 0, TOK_HASH_SIZE
* sizeof(TokenSym
*));
2835 tok_ident
= TOK_IDENT
;
2844 ts
= tok_alloc(p
, r
- p
- 1);
2849 /* Preprocess the current file */
2850 static int tcc_preprocess(TCCState
*s1
)
2853 BufferedFile
*file_ref
, **iptr
, **iptr_new
;
2854 int token_seen
, line_ref
, d
;
2857 preprocess_init(s1
);
2858 define_start
= define_stack
;
2859 ch
= file
->buf_ptr
[0];
2860 tok_flags
= TOK_FLAG_BOL
| TOK_FLAG_BOF
;
2861 parse_flags
= PARSE_FLAG_ASM_COMMENTS
| PARSE_FLAG_PREPROCESS
|
2862 PARSE_FLAG_LINEFEED
| PARSE_FLAG_SPACES
;
2867 iptr
= s1
->include_stack_ptr
;
2870 if (tok
== TOK_EOF
) {
2872 } else if (file
!= file_ref
) {
2874 } else if (tok
== TOK_LINEFEED
) {
2879 } else if (!token_seen
) {
2880 d
= file
->line_num
- line_ref
;
2881 if (file
!= file_ref
|| d
< 0 || d
>= 8) {
2883 iptr_new
= s1
->include_stack_ptr
;
2884 s
= iptr_new
> iptr
? " 1"
2885 : iptr_new
< iptr
? " 2"
2886 : iptr_new
> s1
->include_stack
? " 3"
2890 fprintf(s1
->outfile
, "# %d \"%s\"%s\n", file
->line_num
, file
->filename
, s
);
2893 fputs("\n", s1
->outfile
), --d
;
2895 line_ref
= (file_ref
= file
)->line_num
;
2896 token_seen
= tok
!= TOK_LINEFEED
;
2900 fputs(get_tok_str(tok
, &tokc
), s1
->outfile
);
2902 free_defines(define_start
);