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 */
132 sprintf(p
, "%Lu", cv
->ull
);
135 cstr_ccat(&cstr_buf
, 'L');
137 cstr_ccat(&cstr_buf
, '\'');
138 add_char(&cstr_buf
, cv
->i
);
139 cstr_ccat(&cstr_buf
, '\'');
140 cstr_ccat(&cstr_buf
, '\0');
144 len
= cstr
->size
- 1;
146 add_char(&cstr_buf
, ((unsigned char *)cstr
->data
)[i
]);
147 cstr_ccat(&cstr_buf
, '\0');
150 cstr_ccat(&cstr_buf
, 'L');
153 cstr_ccat(&cstr_buf
, '\"');
155 len
= cstr
->size
- 1;
157 add_char(&cstr_buf
, ((unsigned char *)cstr
->data
)[i
]);
159 len
= (cstr
->size
/ sizeof(nwchar_t
)) - 1;
161 add_char(&cstr_buf
, ((nwchar_t
*)cstr
->data
)[i
]);
163 cstr_ccat(&cstr_buf
, '\"');
164 cstr_ccat(&cstr_buf
, '\0');
173 return strcpy(p
, "...");
175 return strcpy(p
, "<<=");
177 return strcpy(p
, ">>=");
180 /* search in two bytes table */
194 } else if (v
< tok_ident
) {
195 return table_ident
[v
- TOK_IDENT
]->str
;
196 } else if (v
>= SYM_FIRST_ANOM
) {
197 /* special name for anonymous symbol */
198 sprintf(p
, "L.%u", v
- SYM_FIRST_ANOM
);
200 /* should never happen */
205 return cstr_buf
.data
;
208 /* fill input buffer and peek next char */
209 static int tcc_peekc_slow(BufferedFile
*bf
)
212 /* only tries to read if really end of buffer */
213 if (bf
->buf_ptr
>= bf
->buf_end
) {
215 #if defined(PARSE_DEBUG)
220 len
= read(bf
->fd
, bf
->buffer
, len
);
227 bf
->buf_ptr
= bf
->buffer
;
228 bf
->buf_end
= bf
->buffer
+ len
;
229 *bf
->buf_end
= CH_EOB
;
231 if (bf
->buf_ptr
< bf
->buf_end
) {
232 return bf
->buf_ptr
[0];
234 bf
->buf_ptr
= bf
->buf_end
;
239 /* return the current character, handling end of block if necessary
241 static int handle_eob(void)
243 return tcc_peekc_slow(file
);
246 /* read next char from current input file and handle end of input buffer */
247 static inline void inp(void)
249 ch
= *(++(file
->buf_ptr
));
250 /* end of buffer/file handling */
255 /* handle '\[\r]\n' */
256 static int handle_stray_noerror(void)
263 } else if (ch
== '\r') {
277 static void handle_stray(void)
279 if (handle_stray_noerror())
280 error("stray '\\' in program");
283 /* skip the stray and handle the \\n case. Output an error if
284 incorrect char after the stray */
285 static int handle_stray1(uint8_t *p
)
289 if (p
>= file
->buf_end
) {
306 /* handle just the EOB case, but not stray */
307 #define PEEKC_EOB(c, p)\
318 /* handle the complicated stray case */
324 c = handle_stray1(p);\
329 /* input with '\[\r]\n' handling. Note that this function cannot
330 handle other characters after '\', so you cannot call it inside
331 strings or comments */
332 static void minp(void)
340 /* single line C++ comments */
341 static uint8_t *parse_line_comment(uint8_t *p
)
349 if (c
== '\n' || c
== CH_EOF
) {
351 } else if (c
== '\\') {
360 } else if (c
== '\r') {
378 static uint8_t *parse_comment(uint8_t *p
)
387 if (c
== '\n' || c
== '*' || c
== '\\')
391 if (c
== '\n' || c
== '*' || c
== '\\')
395 /* now we can handle all the cases */
399 } else if (c
== '*') {
405 } else if (c
== '/') {
407 } else if (c
== '\\') {
412 /* skip '\[\r]\n', otherwise just skip the stray */
418 } else if (c
== '\r') {
435 /* stray, eob or eof */
440 error("unexpected end of file in comment");
441 } else if (c
== '\\') {
453 static inline void skip_spaces(void)
459 static inline int check_space(int t
, int *spc
)
470 /* parse a string without interpreting escapes */
471 static uint8_t *parse_pp_string(uint8_t *p
,
472 int sep
, CString
*str
)
480 } else if (c
== '\\') {
486 /* XXX: indicate line number of start of string */
487 error("missing terminating %c character", sep
);
488 } else if (c
== '\\') {
489 /* escape : just skip \[\r]\n */
494 } else if (c
== '\r') {
497 expect("'\n' after '\r'");
500 } else if (c
== CH_EOF
) {
501 goto unterminated_string
;
504 cstr_ccat(str
, '\\');
510 } else if (c
== '\n') {
513 } else if (c
== '\r') {
517 cstr_ccat(str
, '\r');
533 /* skip block of text until #else, #elif or #endif. skip also pairs of
535 void preprocess_skip(void)
537 int a
, start_of_line
, c
, in_warn_or_error
;
544 in_warn_or_error
= 0;
565 } else if (c
== '\\') {
566 ch
= file
->buf_ptr
[0];
567 handle_stray_noerror();
574 if (in_warn_or_error
)
576 p
= parse_pp_string(p
, c
, NULL
);
580 if (in_warn_or_error
)
587 p
= parse_comment(p
);
588 } else if (ch
== '/') {
589 p
= parse_line_comment(p
);
599 (tok
== TOK_ELSE
|| tok
== TOK_ELIF
|| tok
== TOK_ENDIF
))
601 if (tok
== TOK_IF
|| tok
== TOK_IFDEF
|| tok
== TOK_IFNDEF
)
603 else if (tok
== TOK_ENDIF
)
605 else if( tok
== TOK_ERROR
|| tok
== TOK_WARNING
)
606 in_warn_or_error
= 1;
620 /* ParseState handling */
622 /* XXX: currently, no include file info is stored. Thus, we cannot display
623 accurate messages if the function or data definition spans multiple
626 /* save current parse state in 's' */
627 void save_parse_state(ParseState
*s
)
629 s
->line_num
= file
->line_num
;
630 s
->macro_ptr
= macro_ptr
;
635 /* restore parse state from 's' */
636 void restore_parse_state(ParseState
*s
)
638 file
->line_num
= s
->line_num
;
639 macro_ptr
= s
->macro_ptr
;
644 /* return the number of additional 'ints' necessary to store the
646 static inline int tok_ext_size(int t
)
660 error("unsupported token");
667 return LDOUBLE_SIZE
/ 4;
673 /* token string handling */
675 static inline void tok_str_new(TokenString
*s
)
679 s
->allocated_len
= 0;
680 s
->last_line_num
= -1;
683 static void tok_str_free(int *str
)
688 static int *tok_str_realloc(TokenString
*s
)
692 if (s
->allocated_len
== 0) {
695 len
= s
->allocated_len
* 2;
697 str
= tcc_realloc(s
->str
, len
* sizeof(int));
699 error("memory full");
700 s
->allocated_len
= len
;
705 static void tok_str_add(TokenString
*s
, int t
)
711 if (len
>= s
->allocated_len
)
712 str
= tok_str_realloc(s
);
717 static void tok_str_add2(TokenString
*s
, int t
, CValue
*cv
)
724 /* allocate space for worst case */
725 if (len
+ TOK_MAX_SIZE
> s
->allocated_len
)
726 str
= tok_str_realloc(s
);
735 str
[len
++] = cv
->tab
[0];
744 nb_words
= (sizeof(CString
) + cv
->cstr
->size
+ 3) >> 2;
745 while ((len
+ nb_words
) > s
->allocated_len
)
746 str
= tok_str_realloc(s
);
747 cstr
= (CString
*)(str
+ len
);
749 cstr
->size
= cv
->cstr
->size
;
750 cstr
->data_allocated
= NULL
;
751 cstr
->size_allocated
= cstr
->size
;
752 memcpy((char *)cstr
+ sizeof(CString
),
753 cv
->cstr
->data
, cstr
->size
);
760 #if LDOUBLE_SIZE == 8
763 str
[len
++] = cv
->tab
[0];
764 str
[len
++] = cv
->tab
[1];
766 #if LDOUBLE_SIZE == 12
768 str
[len
++] = cv
->tab
[0];
769 str
[len
++] = cv
->tab
[1];
770 str
[len
++] = cv
->tab
[2];
771 #elif LDOUBLE_SIZE == 16
773 str
[len
++] = cv
->tab
[0];
774 str
[len
++] = cv
->tab
[1];
775 str
[len
++] = cv
->tab
[2];
776 str
[len
++] = cv
->tab
[3];
777 #elif LDOUBLE_SIZE != 8
778 #error add long double size support
787 /* add the current parse token in token string 's' */
788 static void tok_str_add_tok(TokenString
*s
)
792 /* save line number info */
793 if (file
->line_num
!= s
->last_line_num
) {
794 s
->last_line_num
= file
->line_num
;
795 cval
.i
= s
->last_line_num
;
796 tok_str_add2(s
, TOK_LINENUM
, &cval
);
798 tok_str_add2(s
, tok
, &tokc
);
801 #if LDOUBLE_SIZE == 16
802 #define LDOUBLE_GET(p, cv) \
807 #elif LDOUBLE_SIZE == 12
808 #define LDOUBLE_GET(p, cv) \
812 #elif LDOUBLE_SIZE == 8
813 #define LDOUBLE_GET(p, cv) \
817 #error add long double size support
821 /* get a token from an integer array and increment pointer
822 accordingly. we code it as a macro to avoid pointer aliasing. */
823 #define TOK_GET(t, p, cv) \
838 cv.cstr = (CString *)p; \
839 cv.cstr->data = (char *)p + sizeof(CString);\
840 p += (sizeof(CString) + cv.cstr->size + 3) >> 2;\
850 LDOUBLE_GET(p, cv); \
851 p += LDOUBLE_SIZE / 4; \
858 /* defines handling */
859 static inline void define_push(int v
, int macro_type
, int *str
, Sym
*first_arg
)
863 s
= sym_push2(&define_stack
, v
, macro_type
, 0);
866 table_ident
[v
- TOK_IDENT
]->sym_define
= s
;
869 /* undefined a define symbol. Its name is just set to zero */
870 static void define_undef(Sym
*s
)
874 if (v
>= TOK_IDENT
&& v
< tok_ident
)
875 table_ident
[v
- TOK_IDENT
]->sym_define
= NULL
;
879 static inline Sym
*define_find(int v
)
882 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
884 return table_ident
[v
]->sym_define
;
887 /* free define stack until top reaches 'b' */
888 static void free_defines(Sym
*b
)
896 /* do not free args or predefined defines */
898 tok_str_free(top
->d
);
900 if (v
>= TOK_IDENT
&& v
< tok_ident
)
901 table_ident
[v
- TOK_IDENT
]->sym_define
= NULL
;
909 static Sym
*label_find(int v
)
912 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
914 return table_ident
[v
]->sym_label
;
917 static Sym
*label_push(Sym
**ptop
, int v
, int flags
)
920 s
= sym_push2(ptop
, v
, 0, 0);
922 ps
= &table_ident
[v
- TOK_IDENT
]->sym_label
;
923 if (ptop
== &global_label_stack
) {
924 /* modify the top most local identifier, so that
925 sym_identifier will point to 's' when popped */
927 ps
= &(*ps
)->prev_tok
;
934 /* pop labels until element last is reached. Look if any labels are
935 undefined. Define symbols if '&&label' was used. */
936 static void label_pop(Sym
**ptop
, Sym
*slast
)
939 for(s
= *ptop
; s
!= slast
; s
= s1
) {
941 if (s
->r
== LABEL_DECLARED
) {
942 warning("label '%s' declared but not used", get_tok_str(s
->v
, NULL
));
943 } else if (s
->r
== LABEL_FORWARD
) {
944 error("label '%s' used but not defined",
945 get_tok_str(s
->v
, NULL
));
948 /* define corresponding symbol. A size of
950 put_extern_sym(s
, cur_text_section
, (long)s
->next
, 1);
954 table_ident
[s
->v
- TOK_IDENT
]->sym_label
= s
->prev_tok
;
960 /* eval an expression for #if/#elif */
961 static int expr_preprocess(void)
967 while (tok
!= TOK_LINEFEED
&& tok
!= TOK_EOF
) {
968 next(); /* do macro subst */
969 if (tok
== TOK_DEFINED
) {
974 c
= define_find(tok
) != 0;
979 } else if (tok
>= TOK_IDENT
) {
980 /* if undefined macro */
984 tok_str_add_tok(&str
);
986 tok_str_add(&str
, -1); /* simulate end of file */
987 tok_str_add(&str
, 0);
988 /* now evaluate C constant expression */
993 tok_str_free(str
.str
);
997 #if defined(PARSE_DEBUG) || defined(PP_DEBUG)
998 static void tok_print(int *str
)
1005 TOK_GET(t
, str
, cval
);
1008 printf("%s", get_tok_str(t
, &cval
));
1014 /* parse after #define */
1015 static void parse_define(void)
1017 Sym
*s
, *first
, **ps
;
1018 int v
, t
, varg
, is_vaargs
, spc
;
1023 error("invalid macro name '%s'", get_tok_str(tok
, &tokc
));
1024 /* XXX: should check if same macro (ANSI) */
1027 /* '(' must be just after macro definition for MACRO_FUNC */
1032 while (tok
!= ')') {
1036 if (varg
== TOK_DOTS
) {
1037 varg
= TOK___VA_ARGS__
;
1039 } else if (tok
== TOK_DOTS
&& gnu_ext
) {
1043 if (varg
< TOK_IDENT
)
1044 error("badly punctuated parameter list");
1045 s
= sym_push2(&define_stack
, varg
| SYM_FIELD
, is_vaargs
, 0);
1058 /* EOF testing necessary for '-D' handling */
1059 while (tok
!= TOK_LINEFEED
&& tok
!= TOK_EOF
) {
1060 /* remove spaces around ## and after '#' */
1061 if (TOK_TWOSHARPS
== tok
) {
1065 } else if ('#' == tok
) {
1067 } else if (check_space(tok
, &spc
)) {
1070 tok_str_add2(&str
, tok
, &tokc
);
1075 --str
.len
; /* remove trailing space */
1076 tok_str_add(&str
, 0);
1078 printf("define %s %d: ", get_tok_str(v
, NULL
), t
);
1081 define_push(v
, t
, str
.str
, first
);
1084 static inline int hash_cached_include(int type
, const char *filename
)
1086 const unsigned char *s
;
1090 h
= TOK_HASH_FUNC(h
, type
);
1093 h
= TOK_HASH_FUNC(h
, *s
);
1096 h
&= (CACHED_INCLUDES_HASH_SIZE
- 1);
1100 /* XXX: use a token or a hash table to accelerate matching ? */
1101 static CachedInclude
*search_cached_include(TCCState
*s1
,
1102 int type
, const char *filename
)
1106 h
= hash_cached_include(type
, filename
);
1107 i
= s1
->cached_includes_hash
[h
];
1111 e
= s1
->cached_includes
[i
- 1];
1112 if (e
->type
== type
&& !PATHCMP(e
->filename
, filename
))
1119 static inline void add_cached_include(TCCState
*s1
, int type
,
1120 const char *filename
, int ifndef_macro
)
1125 if (search_cached_include(s1
, type
, filename
))
1128 printf("adding cached '%s' %s\n", filename
, get_tok_str(ifndef_macro
, NULL
));
1130 e
= tcc_malloc(sizeof(CachedInclude
) + strlen(filename
));
1134 strcpy(e
->filename
, filename
);
1135 e
->ifndef_macro
= ifndef_macro
;
1136 dynarray_add((void ***)&s1
->cached_includes
, &s1
->nb_cached_includes
, e
);
1137 /* add in hash table */
1138 h
= hash_cached_include(type
, filename
);
1139 e
->hash_next
= s1
->cached_includes_hash
[h
];
1140 s1
->cached_includes_hash
[h
] = s1
->nb_cached_includes
;
1143 static void pragma_parse(TCCState
*s1
)
1148 if (tok
== TOK_pack
) {
1151 #pragma pack(1) // set
1152 #pragma pack() // reset to default
1153 #pragma pack(push,1) // push & set
1154 #pragma pack(pop) // restore previous
1158 if (tok
== TOK_ASM_pop
) {
1160 if (s1
->pack_stack_ptr
<= s1
->pack_stack
) {
1162 error("out of pack stack");
1164 s1
->pack_stack_ptr
--;
1168 if (tok
== TOK_ASM_push
) {
1170 if (s1
->pack_stack_ptr
>= s1
->pack_stack
+ PACK_STACK_SIZE
- 1)
1172 s1
->pack_stack_ptr
++;
1175 if (tok
!= TOK_CINT
) {
1177 error("invalid pack pragma");
1180 if (val
< 1 || val
> 16 || (val
& (val
- 1)) != 0)
1184 *s1
->pack_stack_ptr
= val
;
1190 /* is_bof is true if first non space token at beginning of file */
1191 static void preprocess(int is_bof
)
1193 TCCState
*s1
= tcc_state
;
1194 int i
, c
, n
, saved_parse_flags
;
1198 saved_parse_flags
= parse_flags
;
1199 parse_flags
= PARSE_FLAG_PREPROCESS
| PARSE_FLAG_TOK_NUM
|
1200 PARSE_FLAG_LINEFEED
;
1210 s
= define_find(tok
);
1211 /* undefine symbol by putting an invalid name */
1216 case TOK_INCLUDE_NEXT
:
1217 ch
= file
->buf_ptr
[0];
1218 /* XXX: incorrect if comments : use next_nomacro with a special mode */
1223 } else if (ch
== '\"') {
1228 while (ch
!= c
&& ch
!= '\n' && ch
!= CH_EOF
) {
1229 if ((q
- buf
) < sizeof(buf
) - 1)
1232 if (handle_stray_noerror() == 0)
1240 /* eat all spaces and comments after include */
1241 /* XXX: slightly incorrect */
1242 while (ch1
!= '\n' && ch1
!= CH_EOF
)
1246 /* computed #include : either we have only strings or
1247 we have anything enclosed in '<>' */
1250 if (tok
== TOK_STR
) {
1251 while (tok
!= TOK_LINEFEED
) {
1252 if (tok
!= TOK_STR
) {
1254 error("'#include' expects \"FILENAME\" or <FILENAME>");
1256 pstrcat(buf
, sizeof(buf
), (char *)tokc
.cstr
->data
);
1262 while (tok
!= TOK_LINEFEED
) {
1263 pstrcat(buf
, sizeof(buf
), get_tok_str(tok
, &tokc
));
1267 /* check syntax and remove '<>' */
1268 if (len
< 2 || buf
[0] != '<' || buf
[len
- 1] != '>')
1269 goto include_syntax
;
1270 memmove(buf
, buf
+ 1, len
- 2);
1271 buf
[len
- 2] = '\0';
1276 if (s1
->include_stack_ptr
>= s1
->include_stack
+ INCLUDE_STACK_SIZE
)
1277 error("#include recursion too deep");
1279 n
= s1
->nb_include_paths
+ s1
->nb_sysinclude_paths
;
1280 for (i
= -2; i
< n
; ++i
) {
1281 char buf1
[sizeof file
->filename
];
1288 /* check absolute include path */
1289 if (!IS_ABSPATH(buf
))
1293 } else if (i
== -1) {
1294 /* search in current dir if "header.h" */
1297 size
= tcc_basename(file
->filename
) - file
->filename
;
1298 memcpy(buf1
, file
->filename
, size
);
1302 /* search in all the include paths */
1303 if (i
< s1
->nb_include_paths
)
1304 path
= s1
->include_paths
[i
];
1306 path
= s1
->sysinclude_paths
[i
- s1
->nb_include_paths
];
1307 pstrcpy(buf1
, sizeof(buf1
), path
);
1308 pstrcat(buf1
, sizeof(buf1
), "/");
1311 pstrcat(buf1
, sizeof(buf1
), buf
);
1313 e
= search_cached_include(s1
, c
, buf1
);
1314 if (e
&& define_find(e
->ifndef_macro
)) {
1315 /* no need to parse the include because the 'ifndef macro'
1318 printf("%s: skipping %s\n", file
->filename
, buf
);
1322 f
= tcc_open(s1
, buf1
);
1327 if (tok
== TOK_INCLUDE_NEXT
) {
1338 printf("%s: including %s\n", file
->filename
, buf1
);
1341 /* XXX: fix current line init */
1342 /* push current file in stack */
1343 *s1
->include_stack_ptr
++ = file
;
1345 pstrcpy(f
->inc_filename
, sizeof(f
->inc_filename
), buf1
);
1347 /* add include file debug info */
1348 if (tcc_state
->do_debug
) {
1349 put_stabs(file
->filename
, N_BINCL
, 0, 0, 0);
1351 tok_flags
|= TOK_FLAG_BOF
| TOK_FLAG_BOL
;
1352 ch
= file
->buf_ptr
[0];
1355 error("include file '%s' not found", buf
);
1362 c
= expr_preprocess();
1368 if (tok
< TOK_IDENT
)
1369 error("invalid argument for '#if%sdef'", c
? "n" : "");
1373 printf("#ifndef %s\n", get_tok_str(tok
, NULL
));
1375 file
->ifndef_macro
= tok
;
1378 c
= (define_find(tok
) != 0) ^ c
;
1380 if (s1
->ifdef_stack_ptr
>= s1
->ifdef_stack
+ IFDEF_STACK_SIZE
)
1381 error("memory full");
1382 *s1
->ifdef_stack_ptr
++ = c
;
1385 if (s1
->ifdef_stack_ptr
== s1
->ifdef_stack
)
1386 error("#else without matching #if");
1387 if (s1
->ifdef_stack_ptr
[-1] & 2)
1388 error("#else after #else");
1389 c
= (s1
->ifdef_stack_ptr
[-1] ^= 3);
1392 if (s1
->ifdef_stack_ptr
== s1
->ifdef_stack
)
1393 error("#elif without matching #if");
1394 c
= s1
->ifdef_stack_ptr
[-1];
1396 error("#elif after #else");
1397 /* last #if/#elif expression was true: we skip */
1400 c
= expr_preprocess();
1401 s1
->ifdef_stack_ptr
[-1] = c
;
1411 if (s1
->ifdef_stack_ptr
<= file
->ifdef_stack_ptr
)
1412 error("#endif without matching #if");
1413 s1
->ifdef_stack_ptr
--;
1414 /* '#ifndef macro' was at the start of file. Now we check if
1415 an '#endif' is exactly at the end of file */
1416 if (file
->ifndef_macro
&&
1417 s1
->ifdef_stack_ptr
== file
->ifdef_stack_ptr
) {
1418 file
->ifndef_macro_saved
= file
->ifndef_macro
;
1419 /* need to set to zero to avoid false matches if another
1420 #ifndef at middle of file */
1421 file
->ifndef_macro
= 0;
1422 while (tok
!= TOK_LINEFEED
)
1424 tok_flags
|= TOK_FLAG_ENDIF
;
1430 if (tok
!= TOK_CINT
)
1432 file
->line_num
= tokc
.i
- 1; /* the line number will be incremented after */
1434 if (tok
!= TOK_LINEFEED
) {
1437 pstrcpy(file
->filename
, sizeof(file
->filename
),
1438 (char *)tokc
.cstr
->data
);
1444 ch
= file
->buf_ptr
[0];
1447 while (ch
!= '\n' && ch
!= CH_EOF
) {
1448 if ((q
- buf
) < sizeof(buf
) - 1)
1451 if (handle_stray_noerror() == 0)
1458 error("#error %s", buf
);
1460 warning("#warning %s", buf
);
1466 if (tok
== TOK_LINEFEED
|| tok
== '!' || tok
== TOK_CINT
) {
1467 /* '!' is ignored to allow C scripts. numbers are ignored
1468 to emulate cpp behaviour */
1470 if (!(saved_parse_flags
& PARSE_FLAG_ASM_COMMENTS
))
1471 warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok
, &tokc
));
1475 /* ignore other preprocess commands or #! for C scripts */
1476 while (tok
!= TOK_LINEFEED
)
1479 parse_flags
= saved_parse_flags
;
1482 /* evaluate escape codes in a string. */
1483 static void parse_escape_string(CString
*outstr
, const uint8_t *buf
, int is_long
)
1498 case '0': case '1': case '2': case '3':
1499 case '4': case '5': case '6': case '7':
1500 /* at most three octal digits */
1505 n
= n
* 8 + c
- '0';
1509 n
= n
* 8 + c
- '0';
1514 goto add_char_nonext
;
1522 if (c
>= 'a' && c
<= 'f')
1524 else if (c
>= 'A' && c
<= 'F')
1534 goto add_char_nonext
;
1558 goto invalid_escape
;
1568 if (c
>= '!' && c
<= '~')
1569 warning("unknown escape sequence: \'\\%c\'", c
);
1571 warning("unknown escape sequence: \'\\x%x\'", c
);
1578 cstr_ccat(outstr
, c
);
1580 cstr_wccat(outstr
, c
);
1582 /* add a trailing '\0' */
1584 cstr_ccat(outstr
, '\0');
1586 cstr_wccat(outstr
, '\0');
1589 /* we use 64 bit numbers */
1592 /* bn = (bn << shift) | or_val */
1593 void bn_lshift(unsigned int *bn
, int shift
, int or_val
)
1597 for(i
=0;i
<BN_SIZE
;i
++) {
1599 bn
[i
] = (v
<< shift
) | or_val
;
1600 or_val
= v
>> (32 - shift
);
1604 void bn_zero(unsigned int *bn
)
1607 for(i
=0;i
<BN_SIZE
;i
++) {
1612 /* parse number in null terminated string 'p' and return it in the
1614 void parse_number(const char *p
)
1616 int b
, t
, shift
, frac_bits
, s
, exp_val
, ch
;
1618 unsigned int bn
[BN_SIZE
];
1629 goto float_frac_parse
;
1630 } else if (t
== '0') {
1631 if (ch
== 'x' || ch
== 'X') {
1635 } else if (tcc_ext
&& (ch
== 'b' || ch
== 'B')) {
1641 /* parse all digits. cannot check octal numbers at this stage
1642 because of floating point constants */
1644 if (ch
>= 'a' && ch
<= 'f')
1646 else if (ch
>= 'A' && ch
<= 'F')
1654 if (q
>= token_buf
+ STRING_MAX_SIZE
) {
1656 error("number too long");
1662 ((ch
== 'e' || ch
== 'E') && b
== 10) ||
1663 ((ch
== 'p' || ch
== 'P') && (b
== 16 || b
== 2))) {
1665 /* NOTE: strtox should support that for hexa numbers, but
1666 non ISOC99 libcs do not support it, so we prefer to do
1668 /* hexadecimal or binary floats */
1669 /* XXX: handle overflows */
1681 } else if (t
>= 'a') {
1683 } else if (t
>= 'A') {
1688 bn_lshift(bn
, shift
, t
);
1695 if (t
>= 'a' && t
<= 'f') {
1697 } else if (t
>= 'A' && t
<= 'F') {
1699 } else if (t
>= '0' && t
<= '9') {
1705 error("invalid digit");
1706 bn_lshift(bn
, shift
, t
);
1711 if (ch
!= 'p' && ch
!= 'P')
1718 } else if (ch
== '-') {
1722 if (ch
< '0' || ch
> '9')
1723 expect("exponent digits");
1724 while (ch
>= '0' && ch
<= '9') {
1725 exp_val
= exp_val
* 10 + ch
- '0';
1728 exp_val
= exp_val
* s
;
1730 /* now we can generate the number */
1731 /* XXX: should patch directly float number */
1732 d
= (double)bn
[1] * 4294967296.0 + (double)bn
[0];
1733 d
= ldexp(d
, exp_val
- frac_bits
);
1738 /* float : should handle overflow */
1740 } else if (t
== 'L') {
1743 /* XXX: not large enough */
1744 tokc
.ld
= (long double)d
;
1750 /* decimal floats */
1752 if (q
>= token_buf
+ STRING_MAX_SIZE
)
1757 while (ch
>= '0' && ch
<= '9') {
1758 if (q
>= token_buf
+ STRING_MAX_SIZE
)
1764 if (ch
== 'e' || ch
== 'E') {
1765 if (q
>= token_buf
+ STRING_MAX_SIZE
)
1769 if (ch
== '-' || ch
== '+') {
1770 if (q
>= token_buf
+ STRING_MAX_SIZE
)
1775 if (ch
< '0' || ch
> '9')
1776 expect("exponent digits");
1777 while (ch
>= '0' && ch
<= '9') {
1778 if (q
>= token_buf
+ STRING_MAX_SIZE
)
1790 tokc
.f
= strtof(token_buf
, NULL
);
1791 } else if (t
== 'L') {
1794 tokc
.ld
= strtold(token_buf
, NULL
);
1797 tokc
.d
= strtod(token_buf
, NULL
);
1801 unsigned long long n
, n1
;
1804 /* integer number */
1807 if (b
== 10 && *q
== '0') {
1814 /* no need for checks except for base 10 / 8 errors */
1817 } else if (t
>= 'a') {
1819 } else if (t
>= 'A') {
1824 error("invalid digit");
1828 /* detect overflow */
1829 /* XXX: this test is not reliable */
1831 error("integer constant overflow");
1834 /* XXX: not exactly ANSI compliant */
1835 if ((n
& 0xffffffff00000000LL
) != 0) {
1840 } else if (n
> 0x7fffffff) {
1851 error("three 'l's in integer constant");
1854 if (tok
== TOK_CINT
)
1856 else if (tok
== TOK_CUINT
)
1860 } else if (t
== 'U') {
1862 error("two 'u's in integer constant");
1864 if (tok
== TOK_CINT
)
1866 else if (tok
== TOK_CLLONG
)
1873 if (tok
== TOK_CINT
|| tok
== TOK_CUINT
)
1879 error("invalid number\n");
1883 #define PARSE2(c1, tok1, c2, tok2) \
1894 /* return next token without macro substitution */
1895 static inline void next_nomacro1(void)
1910 goto keep_tok_flags
;
1917 /* first look if it is in fact an end of buffer */
1918 if (p
>= file
->buf_end
) {
1922 if (p
>= file
->buf_end
)
1935 TCCState
*s1
= tcc_state
;
1936 if ((parse_flags
& PARSE_FLAG_LINEFEED
)
1937 && !(tok_flags
& TOK_FLAG_EOF
)) {
1938 tok_flags
|= TOK_FLAG_EOF
;
1940 goto keep_tok_flags
;
1941 } else if (s1
->include_stack_ptr
== s1
->include_stack
||
1942 !(parse_flags
& PARSE_FLAG_PREPROCESS
)) {
1943 /* no include left : end of file. */
1946 tok_flags
&= ~TOK_FLAG_EOF
;
1947 /* pop include file */
1949 /* test if previous '#endif' was after a #ifdef at
1951 if (tok_flags
& TOK_FLAG_ENDIF
) {
1953 printf("#endif %s\n", get_tok_str(file
->ifndef_macro_saved
, NULL
));
1955 add_cached_include(s1
, file
->inc_type
, file
->inc_filename
,
1956 file
->ifndef_macro_saved
);
1959 /* add end of include file debug info */
1960 if (tcc_state
->do_debug
) {
1961 put_stabd(N_EINCL
, 0, 0);
1963 /* pop include stack */
1965 s1
->include_stack_ptr
--;
1966 file
= *s1
->include_stack_ptr
;
1975 tok_flags
|= TOK_FLAG_BOL
;
1978 if (0 == (parse_flags
& PARSE_FLAG_LINEFEED
))
1981 goto keep_tok_flags
;
1986 if ((tok_flags
& TOK_FLAG_BOL
) &&
1987 (parse_flags
& PARSE_FLAG_PREPROCESS
)) {
1989 preprocess(tok_flags
& TOK_FLAG_BOF
);
1995 tok
= TOK_TWOSHARPS
;
1997 if (parse_flags
& PARSE_FLAG_ASM_COMMENTS
) {
1998 p
= parse_line_comment(p
- 1);
2007 case 'a': case 'b': case 'c': case 'd':
2008 case 'e': case 'f': case 'g': case 'h':
2009 case 'i': case 'j': case 'k': case 'l':
2010 case 'm': case 'n': case 'o': case 'p':
2011 case 'q': case 'r': case 's': case 't':
2012 case 'u': case 'v': case 'w': case 'x':
2014 case 'A': case 'B': case 'C': case 'D':
2015 case 'E': case 'F': case 'G': case 'H':
2016 case 'I': case 'J': case 'K':
2017 case 'M': case 'N': case 'O': case 'P':
2018 case 'Q': case 'R': case 'S': case 'T':
2019 case 'U': case 'V': case 'W': case 'X':
2025 h
= TOK_HASH_FUNC(h
, c
);
2029 if (!isidnum_table
[c
-CH_EOF
])
2031 h
= TOK_HASH_FUNC(h
, c
);
2038 /* fast case : no stray found, so we have the full token
2039 and we have already hashed it */
2041 h
&= (TOK_HASH_SIZE
- 1);
2042 pts
= &hash_ident
[h
];
2047 if (ts
->len
== len
&& !memcmp(ts
->str
, p1
, len
))
2049 pts
= &(ts
->hash_next
);
2051 ts
= tok_alloc_new(pts
, p1
, len
);
2055 cstr_reset(&tokcstr
);
2058 cstr_ccat(&tokcstr
, *p1
);
2064 while (isidnum_table
[c
-CH_EOF
]) {
2065 cstr_ccat(&tokcstr
, c
);
2068 ts
= tok_alloc(tokcstr
.data
, tokcstr
.size
);
2074 if (t
!= '\\' && t
!= '\'' && t
!= '\"') {
2076 goto parse_ident_fast
;
2079 if (c
== '\'' || c
== '\"') {
2083 cstr_reset(&tokcstr
);
2084 cstr_ccat(&tokcstr
, 'L');
2085 goto parse_ident_slow
;
2089 case '0': case '1': case '2': case '3':
2090 case '4': case '5': case '6': case '7':
2093 cstr_reset(&tokcstr
);
2094 /* after the first digit, accept digits, alpha, '.' or sign if
2095 prefixed by 'eEpP' */
2099 cstr_ccat(&tokcstr
, c
);
2101 if (!(isnum(c
) || isid(c
) || c
== '.' ||
2102 ((c
== '+' || c
== '-') &&
2103 (t
== 'e' || t
== 'E' || t
== 'p' || t
== 'P'))))
2106 /* We add a trailing '\0' to ease parsing */
2107 cstr_ccat(&tokcstr
, '\0');
2108 tokc
.cstr
= &tokcstr
;
2112 /* special dot handling because it can also start a number */
2115 cstr_reset(&tokcstr
);
2116 cstr_ccat(&tokcstr
, '.');
2118 } else if (c
== '.') {
2138 /* parse the string */
2140 p
= parse_pp_string(p
, sep
, &str
);
2141 cstr_ccat(&str
, '\0');
2143 /* eval the escape (should be done as TOK_PPNUM) */
2144 cstr_reset(&tokcstr
);
2145 parse_escape_string(&tokcstr
, str
.data
, is_long
);
2150 /* XXX: make it portable */
2154 char_size
= sizeof(nwchar_t
);
2155 if (tokcstr
.size
<= char_size
)
2156 error("empty character constant");
2157 if (tokcstr
.size
> 2 * char_size
)
2158 warning("multi-character character constant");
2160 tokc
.i
= *(int8_t *)tokcstr
.data
;
2163 tokc
.i
= *(nwchar_t
*)tokcstr
.data
;
2167 tokc
.cstr
= &tokcstr
;
2181 } else if (c
== '<') {
2199 } else if (c
== '>') {
2217 } else if (c
== '=') {
2230 } else if (c
== '=') {
2243 } else if (c
== '=') {
2256 } else if (c
== '=') {
2259 } else if (c
== '>') {
2267 PARSE2('!', '!', '=', TOK_NE
)
2268 PARSE2('=', '=', '=', TOK_EQ
)
2269 PARSE2('*', '*', '=', TOK_A_MUL
)
2270 PARSE2('%', '%', '=', TOK_A_MOD
)
2271 PARSE2('^', '^', '=', TOK_A_XOR
)
2273 /* comments or operator */
2277 p
= parse_comment(p
);
2279 } else if (c
== '/') {
2280 p
= parse_line_comment(p
);
2282 } else if (c
== '=') {
2302 case '$': /* only used in assembler */
2303 case '@': /* dito */
2308 error("unrecognized character \\x%02x", c
);
2314 #if defined(PARSE_DEBUG)
2315 printf("token = %s\n", get_tok_str(tok
, &tokc
));
2319 /* return next token without macro substitution. Can read input from
2321 static void next_nomacro_spc(void)
2327 TOK_GET(tok
, macro_ptr
, tokc
);
2328 if (tok
== TOK_LINENUM
) {
2329 file
->line_num
= tokc
.i
;
2338 static void next_nomacro(void)
2342 } while (is_space(tok
));
2345 /* substitute args in macro_str and return allocated string */
2346 static int *macro_arg_subst(Sym
**nested_list
, int *macro_str
, Sym
*args
)
2348 int *st
, last_tok
, t
, spc
;
2357 TOK_GET(t
, macro_str
, cval
);
2362 TOK_GET(t
, macro_str
, cval
);
2365 s
= sym_find2(args
, t
);
2371 TOK_GET(t
, st
, cval
);
2372 if (!check_space(t
, &spc
))
2373 cstr_cat(&cstr
, get_tok_str(t
, &cval
));
2376 cstr_ccat(&cstr
, '\0');
2378 printf("stringize: %s\n", (char *)cstr
.data
);
2382 tok_str_add2(&str
, TOK_STR
, &cval
);
2385 tok_str_add2(&str
, t
, &cval
);
2387 } else if (t
>= TOK_IDENT
) {
2388 s
= sym_find2(args
, t
);
2391 /* if '##' is present before or after, no arg substitution */
2392 if (*macro_str
== TOK_TWOSHARPS
|| last_tok
== TOK_TWOSHARPS
) {
2393 /* special case for var arg macros : ## eats the
2394 ',' if empty VA_ARGS variable. */
2395 /* XXX: test of the ',' is not 100%
2396 reliable. should fix it to avoid security
2398 if (gnu_ext
&& s
->type
.t
&&
2399 last_tok
== TOK_TWOSHARPS
&&
2400 str
.len
>= 2 && str
.str
[str
.len
- 2] == ',') {
2402 /* suppress ',' '##' */
2405 /* suppress '##' and add variable */
2413 TOK_GET(t1
, st
, cval
);
2416 tok_str_add2(&str
, t1
, &cval
);
2420 /* NOTE: the stream cannot be read when macro
2421 substituing an argument */
2422 macro_subst(&str
, nested_list
, st
, NULL
);
2425 tok_str_add(&str
, t
);
2428 tok_str_add2(&str
, t
, &cval
);
2432 tok_str_add(&str
, 0);
2436 static char const ab_month_name
[12][4] =
2438 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2439 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
2442 /* do macro substitution of current token with macro 's' and add
2443 result to (tok_str,tok_len). 'nested_list' is the list of all
2444 macros we got inside to avoid recursing. Return non zero if no
2445 substitution needs to be done */
2446 static int macro_subst_tok(TokenString
*tok_str
,
2447 Sym
**nested_list
, Sym
*s
, struct macro_level
**can_read_stream
)
2449 Sym
*args
, *sa
, *sa1
;
2450 int mstr_allocated
, parlevel
, *mstr
, t
, t1
, *p
, spc
;
2457 /* if symbol is a macro, prepare substitution */
2458 /* special macros */
2459 if (tok
== TOK___LINE__
) {
2460 snprintf(buf
, sizeof(buf
), "%d", file
->line_num
);
2464 } else if (tok
== TOK___FILE__
) {
2465 cstrval
= file
->filename
;
2467 } else if (tok
== TOK___DATE__
|| tok
== TOK___TIME__
) {
2472 tm
= localtime(&ti
);
2473 if (tok
== TOK___DATE__
) {
2474 snprintf(buf
, sizeof(buf
), "%s %2d %d",
2475 ab_month_name
[tm
->tm_mon
], tm
->tm_mday
, tm
->tm_year
+ 1900);
2477 snprintf(buf
, sizeof(buf
), "%02d:%02d:%02d",
2478 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
2485 cstr_cat(&cstr
, cstrval
);
2486 cstr_ccat(&cstr
, '\0');
2488 tok_str_add2(tok_str
, t1
, &cval
);
2493 if (s
->type
.t
== MACRO_FUNC
) {
2494 /* NOTE: we do not use next_nomacro to avoid eating the
2495 next token. XXX: find better solution */
2499 while (is_space(t
= *p
) || TOK_LINEFEED
== t
)
2501 if (t
== 0 && can_read_stream
) {
2502 /* end of macro stream: we must look at the token
2503 after in the file */
2504 struct macro_level
*ml
= *can_read_stream
;
2510 *can_read_stream
= ml
-> prev
;
2515 /* XXX: incorrect with comments */
2516 ch
= file
->buf_ptr
[0];
2517 while (is_space(ch
) || ch
== '\n')
2521 if (t
!= '(') /* no macro subst */
2524 /* argument macro */
2529 /* NOTE: empty args are allowed, except if no args */
2531 /* handle '()' case */
2532 if (!args
&& !sa
&& tok
== ')')
2535 error("macro '%s' used with too many args",
2536 get_tok_str(s
->v
, 0));
2539 /* NOTE: non zero sa->t indicates VA_ARGS */
2540 while ((parlevel
> 0 ||
2542 (tok
!= ',' || sa
->type
.t
))) &&
2546 else if (tok
== ')')
2548 if (tok
== TOK_LINEFEED
)
2550 if (!check_space(tok
, &spc
))
2551 tok_str_add2(&str
, tok
, &tokc
);
2555 tok_str_add(&str
, 0);
2556 sa1
= sym_push2(&args
, sa
->v
& ~SYM_FIELD
, sa
->type
.t
, 0);
2560 /* special case for gcc var args: add an empty
2561 var arg argument if it is omitted */
2562 if (sa
&& sa
->type
.t
&& gnu_ext
)
2572 error("macro '%s' used with too few args",
2573 get_tok_str(s
->v
, 0));
2576 /* now subst each arg */
2577 mstr
= macro_arg_subst(nested_list
, mstr
, args
);
2582 tok_str_free(sa
->d
);
2588 sym_push2(nested_list
, s
->v
, 0, 0);
2589 macro_subst(tok_str
, nested_list
, mstr
, can_read_stream
);
2590 /* pop nested defined symbol */
2592 *nested_list
= sa1
->prev
;
2600 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
2601 return the resulting string (which must be freed). */
2602 static inline int *macro_twosharps(const int *macro_str
)
2607 TokenString macro_str1
;
2612 /* we search the first '##' */
2613 for(ptr
= macro_str
;;) {
2614 TOK_GET(t
, ptr
, cval
);
2615 if (t
== TOK_TWOSHARPS
)
2617 /* nothing more to do if end of string */
2622 /* we saw '##', so we need more processing to handle it */
2623 tok_str_new(¯o_str1
);
2624 for(ptr
= macro_str
;;) {
2625 TOK_GET(tok
, ptr
, tokc
);
2628 if (tok
== TOK_TWOSHARPS
)
2630 while (*ptr
== TOK_TWOSHARPS
) {
2632 if (t
&& t
!= TOK_TWOSHARPS
) {
2633 TOK_GET(t
, ptr
, cval
);
2635 /* We concatenate the two tokens */
2637 cstr_cat(&cstr
, get_tok_str(tok
, &tokc
));
2639 cstr_cat(&cstr
, get_tok_str(t
, &cval
));
2640 cstr_ccat(&cstr
, '\0');
2643 file
->buf_ptr
= cstr
.data
;
2646 if (0 == *file
->buf_ptr
)
2648 tok_str_add2(¯o_str1
, tok
, &tokc
);
2650 warning("pasting \"%.*s\" and \"%s\" does not give a valid preprocessing token",
2651 n
, cstr
.data
, (char*)cstr
.data
+ n
);
2657 tok_str_add2(¯o_str1
, tok
, &tokc
);
2659 tok_str_add(¯o_str1
, 0);
2660 return macro_str1
.str
;
2664 /* do macro substitution of macro_str and add result to
2665 (tok_str,tok_len). 'nested_list' is the list of all macros we got
2666 inside to avoid recursing. */
2667 static void macro_subst(TokenString
*tok_str
, Sym
**nested_list
,
2668 const int *macro_str
, struct macro_level
** can_read_stream
)
2675 struct macro_level ml
;
2677 /* first scan for '##' operator handling */
2679 macro_str1
= macro_twosharps(ptr
);
2684 /* NOTE: ptr == NULL can only happen if tokens are read from
2685 file stream due to a macro function call */
2688 TOK_GET(t
, ptr
, cval
);
2693 /* if nested substitution, do nothing */
2694 if (sym_find2(*nested_list
, t
))
2697 if (can_read_stream
)
2698 ml
.prev
= *can_read_stream
, *can_read_stream
= &ml
;
2699 macro_ptr
= (int *)ptr
;
2701 ret
= macro_subst_tok(tok_str
, nested_list
, s
, can_read_stream
);
2702 ptr
= (int *)macro_ptr
;
2704 if (can_read_stream
&& *can_read_stream
== &ml
)
2705 *can_read_stream
= ml
.prev
;
2710 if (!check_space(t
, &spc
))
2711 tok_str_add2(tok_str
, t
, &cval
);
2715 tok_str_free(macro_str1
);
2718 /* return next token with macro substitution */
2719 static void next(void)
2721 Sym
*nested_list
, *s
;
2723 struct macro_level
*ml
;
2726 if (parse_flags
& PARSE_FLAG_SPACES
)
2731 /* if not reading from macro substituted string, then try
2732 to substitute macros */
2733 if (tok
>= TOK_IDENT
&&
2734 (parse_flags
& PARSE_FLAG_PREPROCESS
)) {
2735 s
= define_find(tok
);
2737 /* we have a macro: we try to substitute */
2741 if (macro_subst_tok(&str
, &nested_list
, s
, &ml
) == 0) {
2742 /* substitution done, NOTE: maybe empty */
2743 tok_str_add(&str
, 0);
2744 macro_ptr
= str
.str
;
2745 macro_ptr_allocated
= str
.str
;
2752 /* end of macro or end of unget buffer */
2753 if (unget_buffer_enabled
) {
2754 macro_ptr
= unget_saved_macro_ptr
;
2755 unget_buffer_enabled
= 0;
2757 /* end of macro string: free it */
2758 tok_str_free(macro_ptr_allocated
);
2765 /* convert preprocessor tokens into C tokens */
2766 if (tok
== TOK_PPNUM
&&
2767 (parse_flags
& PARSE_FLAG_TOK_NUM
)) {
2768 parse_number((char *)tokc
.cstr
->data
);
2772 /* push back current token and set current token to 'last_tok'. Only
2773 identifier case handled for labels. */
2774 static inline void unget_tok(int last_tok
)
2778 unget_saved_macro_ptr
= macro_ptr
;
2779 unget_buffer_enabled
= 1;
2780 q
= unget_saved_buffer
;
2783 n
= tok_ext_size(tok
) - 1;
2786 *q
= 0; /* end of token string */
2791 /* better than nothing, but needs extension to handle '-E' option
2793 static void preprocess_init(TCCState
*s1
)
2795 s1
->include_stack_ptr
= s1
->include_stack
;
2796 /* XXX: move that before to avoid having to initialize
2797 file->ifdef_stack_ptr ? */
2798 s1
->ifdef_stack_ptr
= s1
->ifdef_stack
;
2799 file
->ifdef_stack_ptr
= s1
->ifdef_stack_ptr
;
2801 /* XXX: not ANSI compliant: bound checking says error */
2803 s1
->pack_stack
[0] = 0;
2804 s1
->pack_stack_ptr
= s1
->pack_stack
;
2807 void preprocess_new()
2813 /* init isid table */
2814 for(i
=CH_EOF
;i
<256;i
++)
2815 isidnum_table
[i
-CH_EOF
] = isid(i
) || isnum(i
);
2817 /* add all tokens */
2819 memset(hash_ident
, 0, TOK_HASH_SIZE
* sizeof(TokenSym
*));
2821 tok_ident
= TOK_IDENT
;
2830 ts
= tok_alloc(p
, r
- p
- 1);
2835 /* Preprocess the current file */
2836 static int tcc_preprocess(TCCState
*s1
)
2839 BufferedFile
*file_ref
, **iptr
, **iptr_new
;
2840 int token_seen
, line_ref
, d
;
2843 preprocess_init(s1
);
2844 define_start
= define_stack
;
2845 ch
= file
->buf_ptr
[0];
2846 tok_flags
= TOK_FLAG_BOL
| TOK_FLAG_BOF
;
2847 parse_flags
= PARSE_FLAG_ASM_COMMENTS
| PARSE_FLAG_PREPROCESS
|
2848 PARSE_FLAG_LINEFEED
| PARSE_FLAG_SPACES
;
2853 iptr
= s1
->include_stack_ptr
;
2856 if (tok
== TOK_EOF
) {
2858 } else if (file
!= file_ref
) {
2860 } else if (tok
== TOK_LINEFEED
) {
2865 } else if (!token_seen
) {
2866 d
= file
->line_num
- line_ref
;
2867 if (file
!= file_ref
|| d
< 0 || d
>= 8) {
2869 iptr_new
= s1
->include_stack_ptr
;
2870 s
= iptr_new
> iptr
? " 1"
2871 : iptr_new
< iptr
? " 2"
2872 : iptr_new
> s1
->include_stack
? " 3"
2876 fprintf(s1
->outfile
, "# %d \"%s\"%s\n", file
->line_num
, file
->filename
, s
);
2879 fputs("\n", s1
->outfile
), --d
;
2881 line_ref
= (file_ref
= file
)->line_num
;
2882 token_seen
= tok
!= TOK_LINEFEED
;
2886 fputs(get_tok_str(tok
, &tokc
), s1
->outfile
);
2888 free_defines(define_start
);