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 const unsigned char tok_two_chars
[] =
30 "<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253"
31 "-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\313..\250##\266";
33 /* true if isid(c) || isnum(c) */
34 static unsigned char isidnum_table
[256-CH_EOF
];
38 struct macro_level
*prev
;
42 static void next_nomacro(void);
43 static void next_nomacro_spc(void);
44 static void macro_subst(TokenString
*tok_str
, Sym
**nested_list
,
45 const int *macro_str
, struct macro_level
**can_read_stream
);
48 /* allocate a new token */
49 static TokenSym
*tok_alloc_new(TokenSym
**pts
, const char *str
, int len
)
51 TokenSym
*ts
, **ptable
;
54 if (tok_ident
>= SYM_FIRST_ANOM
)
57 /* expand token table if needed */
58 i
= tok_ident
- TOK_IDENT
;
59 if ((i
% TOK_ALLOC_INCR
) == 0) {
60 ptable
= tcc_realloc(table_ident
, (i
+ TOK_ALLOC_INCR
) * sizeof(TokenSym
*));
66 ts
= tcc_malloc(sizeof(TokenSym
) + len
);
68 ts
->tok
= tok_ident
++;
69 ts
->sym_define
= NULL
;
71 ts
->sym_struct
= NULL
;
72 ts
->sym_identifier
= NULL
;
75 memcpy(ts
->str
, str
, len
);
81 #define TOK_HASH_INIT 1
82 #define TOK_HASH_FUNC(h, c) ((h) * 263 + (c))
84 /* find a token and add it if not found */
85 static TokenSym
*tok_alloc(const char *str
, int len
)
93 h
= TOK_HASH_FUNC(h
, ((unsigned char *)str
)[i
]);
94 h
&= (TOK_HASH_SIZE
- 1);
101 if (ts
->len
== len
&& !memcmp(ts
->str
, str
, len
))
103 pts
= &(ts
->hash_next
);
105 return tok_alloc_new(pts
, str
, len
);
108 /* XXX: buffer overflow */
109 /* XXX: float tokens */
110 char *get_tok_str(int v
, CValue
*cv
)
112 static char buf
[STRING_MAX_SIZE
+ 1];
113 static CString cstr_buf
;
118 /* NOTE: to go faster, we give a fixed buffer for small strings */
119 cstr_reset(&cstr_buf
);
121 cstr_buf
.size_allocated
= sizeof(buf
);
127 /* XXX: not quite exact, but only useful for testing */
128 sprintf(p
, "%u", cv
->ui
);
132 /* XXX: not quite exact, but only useful for testing */
134 sprintf(p
, "%u", (unsigned)cv
->ull
);
136 sprintf(p
, "%Lu", cv
->ull
);
140 cstr_ccat(&cstr_buf
, 'L');
142 cstr_ccat(&cstr_buf
, '\'');
143 add_char(&cstr_buf
, cv
->i
);
144 cstr_ccat(&cstr_buf
, '\'');
145 cstr_ccat(&cstr_buf
, '\0');
149 len
= cstr
->size
- 1;
151 add_char(&cstr_buf
, ((unsigned char *)cstr
->data
)[i
]);
152 cstr_ccat(&cstr_buf
, '\0');
155 cstr_ccat(&cstr_buf
, 'L');
158 cstr_ccat(&cstr_buf
, '\"');
160 len
= cstr
->size
- 1;
162 add_char(&cstr_buf
, ((unsigned char *)cstr
->data
)[i
]);
164 len
= (cstr
->size
/ sizeof(nwchar_t
)) - 1;
166 add_char(&cstr_buf
, ((nwchar_t
*)cstr
->data
)[i
]);
168 cstr_ccat(&cstr_buf
, '\"');
169 cstr_ccat(&cstr_buf
, '\0');
178 return strcpy(p
, "...");
180 return strcpy(p
, "<<=");
182 return strcpy(p
, ">>=");
185 /* search in two bytes table */
186 const unsigned char *q
= tok_two_chars
;
199 } else if (v
< tok_ident
) {
200 return table_ident
[v
- TOK_IDENT
]->str
;
201 } else if (v
>= SYM_FIRST_ANOM
) {
202 /* special name for anonymous symbol */
203 sprintf(p
, "L.%u", v
- SYM_FIRST_ANOM
);
205 /* should never happen */
210 return cstr_buf
.data
;
213 /* fill input buffer and peek next char */
214 static int tcc_peekc_slow(BufferedFile
*bf
)
217 /* only tries to read if really end of buffer */
218 if (bf
->buf_ptr
>= bf
->buf_end
) {
220 #if defined(PARSE_DEBUG)
225 len
= read(bf
->fd
, bf
->buffer
, len
);
232 bf
->buf_ptr
= bf
->buffer
;
233 bf
->buf_end
= bf
->buffer
+ len
;
234 *bf
->buf_end
= CH_EOB
;
236 if (bf
->buf_ptr
< bf
->buf_end
) {
237 return bf
->buf_ptr
[0];
239 bf
->buf_ptr
= bf
->buf_end
;
244 /* return the current character, handling end of block if necessary
246 static int handle_eob(void)
248 return tcc_peekc_slow(file
);
251 /* read next char from current input file and handle end of input buffer */
252 static inline void inp(void)
254 ch
= *(++(file
->buf_ptr
));
255 /* end of buffer/file handling */
260 /* handle '\[\r]\n' */
261 static int handle_stray_noerror(void)
268 } else if (ch
== '\r') {
282 static void handle_stray(void)
284 if (handle_stray_noerror())
285 error("stray '\\' in program");
288 /* skip the stray and handle the \\n case. Output an error if
289 incorrect char after the stray */
290 static int handle_stray1(uint8_t *p
)
294 if (p
>= file
->buf_end
) {
311 /* handle just the EOB case, but not stray */
312 #define PEEKC_EOB(c, p)\
323 /* handle the complicated stray case */
329 c = handle_stray1(p);\
334 /* input with '\[\r]\n' handling. Note that this function cannot
335 handle other characters after '\', so you cannot call it inside
336 strings or comments */
337 static void minp(void)
345 /* single line C++ comments */
346 static uint8_t *parse_line_comment(uint8_t *p
)
354 if (c
== '\n' || c
== CH_EOF
) {
356 } else if (c
== '\\') {
365 } else if (c
== '\r') {
383 static uint8_t *parse_comment(uint8_t *p
)
392 if (c
== '\n' || c
== '*' || c
== '\\')
396 if (c
== '\n' || c
== '*' || c
== '\\')
400 /* now we can handle all the cases */
404 } else if (c
== '*') {
410 } else if (c
== '/') {
412 } else if (c
== '\\') {
417 /* skip '\[\r]\n', otherwise just skip the stray */
423 } else if (c
== '\r') {
440 /* stray, eob or eof */
445 error("unexpected end of file in comment");
446 } else if (c
== '\\') {
458 static inline void skip_spaces(void)
464 static inline int check_space(int t
, int *spc
)
475 /* parse a string without interpreting escapes */
476 static uint8_t *parse_pp_string(uint8_t *p
,
477 int sep
, CString
*str
)
485 } else if (c
== '\\') {
491 /* XXX: indicate line number of start of string */
492 error("missing terminating %c character", sep
);
493 } else if (c
== '\\') {
494 /* escape : just skip \[\r]\n */
499 } else if (c
== '\r') {
502 expect("'\n' after '\r'");
505 } else if (c
== CH_EOF
) {
506 goto unterminated_string
;
509 cstr_ccat(str
, '\\');
515 } else if (c
== '\n') {
518 } else if (c
== '\r') {
522 cstr_ccat(str
, '\r');
538 /* skip block of text until #else, #elif or #endif. skip also pairs of
540 void preprocess_skip(void)
542 int a
, start_of_line
, c
, in_warn_or_error
;
549 in_warn_or_error
= 0;
570 } else if (c
== '\\') {
571 ch
= file
->buf_ptr
[0];
572 handle_stray_noerror();
579 if (in_warn_or_error
)
581 p
= parse_pp_string(p
, c
, NULL
);
585 if (in_warn_or_error
)
592 p
= parse_comment(p
);
593 } else if (ch
== '/') {
594 p
= parse_line_comment(p
);
604 (tok
== TOK_ELSE
|| tok
== TOK_ELIF
|| tok
== TOK_ENDIF
))
606 if (tok
== TOK_IF
|| tok
== TOK_IFDEF
|| tok
== TOK_IFNDEF
)
608 else if (tok
== TOK_ENDIF
)
610 else if( tok
== TOK_ERROR
|| tok
== TOK_WARNING
)
611 in_warn_or_error
= 1;
625 /* ParseState handling */
627 /* XXX: currently, no include file info is stored. Thus, we cannot display
628 accurate messages if the function or data definition spans multiple
631 /* save current parse state in 's' */
632 void save_parse_state(ParseState
*s
)
634 s
->line_num
= file
->line_num
;
635 s
->macro_ptr
= macro_ptr
;
640 /* restore parse state from 's' */
641 void restore_parse_state(ParseState
*s
)
643 file
->line_num
= s
->line_num
;
644 macro_ptr
= s
->macro_ptr
;
649 /* return the number of additional 'ints' necessary to store the
651 static inline int tok_ext_size(int t
)
665 error("unsupported token");
672 return LDOUBLE_SIZE
/ 4;
678 /* token string handling */
680 static inline void tok_str_new(TokenString
*s
)
684 s
->allocated_len
= 0;
685 s
->last_line_num
= -1;
688 static void tok_str_free(int *str
)
693 static int *tok_str_realloc(TokenString
*s
)
697 if (s
->allocated_len
== 0) {
700 len
= s
->allocated_len
* 2;
702 str
= tcc_realloc(s
->str
, len
* sizeof(int));
704 error("memory full");
705 s
->allocated_len
= len
;
710 static void tok_str_add(TokenString
*s
, int t
)
716 if (len
>= s
->allocated_len
)
717 str
= tok_str_realloc(s
);
722 static void tok_str_add2(TokenString
*s
, int t
, CValue
*cv
)
729 /* allocate space for worst case */
730 if (len
+ TOK_MAX_SIZE
> s
->allocated_len
)
731 str
= tok_str_realloc(s
);
740 str
[len
++] = cv
->tab
[0];
749 nb_words
= (sizeof(CString
) + cv
->cstr
->size
+ 3) >> 2;
750 while ((len
+ nb_words
) > s
->allocated_len
)
751 str
= tok_str_realloc(s
);
752 cstr
= (CString
*)(str
+ len
);
754 cstr
->size
= cv
->cstr
->size
;
755 cstr
->data_allocated
= NULL
;
756 cstr
->size_allocated
= cstr
->size
;
757 memcpy((char *)cstr
+ sizeof(CString
),
758 cv
->cstr
->data
, cstr
->size
);
765 #if LDOUBLE_SIZE == 8
768 str
[len
++] = cv
->tab
[0];
769 str
[len
++] = cv
->tab
[1];
771 #if LDOUBLE_SIZE == 12
773 str
[len
++] = cv
->tab
[0];
774 str
[len
++] = cv
->tab
[1];
775 str
[len
++] = cv
->tab
[2];
776 #elif LDOUBLE_SIZE == 16
778 str
[len
++] = cv
->tab
[0];
779 str
[len
++] = cv
->tab
[1];
780 str
[len
++] = cv
->tab
[2];
781 str
[len
++] = cv
->tab
[3];
782 #elif LDOUBLE_SIZE != 8
783 #error add long double size support
792 /* add the current parse token in token string 's' */
793 static void tok_str_add_tok(TokenString
*s
)
797 /* save line number info */
798 if (file
->line_num
!= s
->last_line_num
) {
799 s
->last_line_num
= file
->line_num
;
800 cval
.i
= s
->last_line_num
;
801 tok_str_add2(s
, TOK_LINENUM
, &cval
);
803 tok_str_add2(s
, tok
, &tokc
);
806 #if LDOUBLE_SIZE == 16
807 #define LDOUBLE_GET(p, cv) \
812 #elif LDOUBLE_SIZE == 12
813 #define LDOUBLE_GET(p, cv) \
817 #elif LDOUBLE_SIZE == 8
818 #define LDOUBLE_GET(p, cv) \
822 #error add long double size support
826 /* get a token from an integer array and increment pointer
827 accordingly. we code it as a macro to avoid pointer aliasing. */
828 #define TOK_GET(t, p, cv) \
843 cv.cstr = (CString *)p; \
844 cv.cstr->data = (char *)p + sizeof(CString);\
845 p += (sizeof(CString) + cv.cstr->size + 3) >> 2;\
855 LDOUBLE_GET(p, cv); \
856 p += LDOUBLE_SIZE / 4; \
863 /* defines handling */
864 static inline void define_push(int v
, int macro_type
, int *str
, Sym
*first_arg
)
868 s
= sym_push2(&define_stack
, v
, macro_type
, 0);
871 table_ident
[v
- TOK_IDENT
]->sym_define
= s
;
874 /* undefined a define symbol. Its name is just set to zero */
875 static void define_undef(Sym
*s
)
879 if (v
>= TOK_IDENT
&& v
< tok_ident
)
880 table_ident
[v
- TOK_IDENT
]->sym_define
= NULL
;
884 static inline Sym
*define_find(int v
)
887 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
889 return table_ident
[v
]->sym_define
;
892 /* free define stack until top reaches 'b' */
893 static void free_defines(Sym
*b
)
901 /* do not free args or predefined defines */
903 tok_str_free(top
->d
);
905 if (v
>= TOK_IDENT
&& v
< tok_ident
)
906 table_ident
[v
- TOK_IDENT
]->sym_define
= NULL
;
914 static Sym
*label_find(int v
)
917 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
919 return table_ident
[v
]->sym_label
;
922 static Sym
*label_push(Sym
**ptop
, int v
, int flags
)
925 s
= sym_push2(ptop
, v
, 0, 0);
927 ps
= &table_ident
[v
- TOK_IDENT
]->sym_label
;
928 if (ptop
== &global_label_stack
) {
929 /* modify the top most local identifier, so that
930 sym_identifier will point to 's' when popped */
932 ps
= &(*ps
)->prev_tok
;
939 /* pop labels until element last is reached. Look if any labels are
940 undefined. Define symbols if '&&label' was used. */
941 static void label_pop(Sym
**ptop
, Sym
*slast
)
944 for(s
= *ptop
; s
!= slast
; s
= s1
) {
946 if (s
->r
== LABEL_DECLARED
) {
947 warning("label '%s' declared but not used", get_tok_str(s
->v
, NULL
));
948 } else if (s
->r
== LABEL_FORWARD
) {
949 error("label '%s' used but not defined",
950 get_tok_str(s
->v
, NULL
));
953 /* define corresponding symbol. A size of
955 put_extern_sym(s
, cur_text_section
, s
->jnext
, 1);
959 table_ident
[s
->v
- TOK_IDENT
]->sym_label
= s
->prev_tok
;
965 /* eval an expression for #if/#elif */
966 static int expr_preprocess(void)
972 while (tok
!= TOK_LINEFEED
&& tok
!= TOK_EOF
) {
973 next(); /* do macro subst */
974 if (tok
== TOK_DEFINED
) {
979 c
= define_find(tok
) != 0;
984 } else if (tok
>= TOK_IDENT
) {
985 /* if undefined macro */
989 tok_str_add_tok(&str
);
991 tok_str_add(&str
, -1); /* simulate end of file */
992 tok_str_add(&str
, 0);
993 /* now evaluate C constant expression */
998 tok_str_free(str
.str
);
1002 #if defined(PARSE_DEBUG) || defined(PP_DEBUG)
1003 static void tok_print(int *str
)
1010 TOK_GET(t
, str
, cval
);
1013 printf("%s", get_tok_str(t
, &cval
));
1019 /* parse after #define */
1020 static void parse_define(void)
1022 Sym
*s
, *first
, **ps
;
1023 int v
, t
, varg
, is_vaargs
, spc
;
1028 error("invalid macro name '%s'", get_tok_str(tok
, &tokc
));
1029 /* XXX: should check if same macro (ANSI) */
1032 /* '(' must be just after macro definition for MACRO_FUNC */
1037 while (tok
!= ')') {
1041 if (varg
== TOK_DOTS
) {
1042 varg
= TOK___VA_ARGS__
;
1044 } else if (tok
== TOK_DOTS
&& gnu_ext
) {
1048 if (varg
< TOK_IDENT
)
1049 error("badly punctuated parameter list");
1050 s
= sym_push2(&define_stack
, varg
| SYM_FIELD
, is_vaargs
, 0);
1063 /* EOF testing necessary for '-D' handling */
1064 while (tok
!= TOK_LINEFEED
&& tok
!= TOK_EOF
) {
1065 /* remove spaces around ## and after '#' */
1066 if (TOK_TWOSHARPS
== tok
) {
1070 } else if ('#' == tok
) {
1072 } else if (check_space(tok
, &spc
)) {
1075 tok_str_add2(&str
, tok
, &tokc
);
1080 --str
.len
; /* remove trailing space */
1081 tok_str_add(&str
, 0);
1083 printf("define %s %d: ", get_tok_str(v
, NULL
), t
);
1086 define_push(v
, t
, str
.str
, first
);
1089 static inline int hash_cached_include(int type
, const char *filename
)
1091 const unsigned char *s
;
1095 h
= TOK_HASH_FUNC(h
, type
);
1098 h
= TOK_HASH_FUNC(h
, *s
);
1101 h
&= (CACHED_INCLUDES_HASH_SIZE
- 1);
1105 /* XXX: use a token or a hash table to accelerate matching ? */
1106 static CachedInclude
*search_cached_include(TCCState
*s1
,
1107 int type
, const char *filename
)
1111 h
= hash_cached_include(type
, filename
);
1112 i
= s1
->cached_includes_hash
[h
];
1116 e
= s1
->cached_includes
[i
- 1];
1117 if (e
->type
== type
&& !PATHCMP(e
->filename
, filename
))
1124 static inline void add_cached_include(TCCState
*s1
, int type
,
1125 const char *filename
, int ifndef_macro
)
1130 if (search_cached_include(s1
, type
, filename
))
1133 printf("adding cached '%s' %s\n", filename
, get_tok_str(ifndef_macro
, NULL
));
1135 e
= tcc_malloc(sizeof(CachedInclude
) + strlen(filename
));
1139 strcpy(e
->filename
, filename
);
1140 e
->ifndef_macro
= ifndef_macro
;
1141 dynarray_add((void ***)&s1
->cached_includes
, &s1
->nb_cached_includes
, e
);
1142 /* add in hash table */
1143 h
= hash_cached_include(type
, filename
);
1144 e
->hash_next
= s1
->cached_includes_hash
[h
];
1145 s1
->cached_includes_hash
[h
] = s1
->nb_cached_includes
;
1148 static void pragma_parse(TCCState
*s1
)
1153 if (tok
== TOK_pack
) {
1156 #pragma pack(1) // set
1157 #pragma pack() // reset to default
1158 #pragma pack(push,1) // push & set
1159 #pragma pack(pop) // restore previous
1163 if (tok
== TOK_ASM_pop
) {
1165 if (s1
->pack_stack_ptr
<= s1
->pack_stack
) {
1167 error("out of pack stack");
1169 s1
->pack_stack_ptr
--;
1173 if (tok
== TOK_ASM_push
) {
1175 if (s1
->pack_stack_ptr
>= s1
->pack_stack
+ PACK_STACK_SIZE
- 1)
1177 s1
->pack_stack_ptr
++;
1180 if (tok
!= TOK_CINT
) {
1182 error("invalid pack pragma");
1185 if (val
< 1 || val
> 16 || (val
& (val
- 1)) != 0)
1189 *s1
->pack_stack_ptr
= val
;
1195 /* is_bof is true if first non space token at beginning of file */
1196 static void preprocess(int is_bof
)
1198 TCCState
*s1
= tcc_state
;
1199 int i
, c
, n
, saved_parse_flags
;
1203 saved_parse_flags
= parse_flags
;
1204 parse_flags
= PARSE_FLAG_PREPROCESS
| PARSE_FLAG_TOK_NUM
|
1205 PARSE_FLAG_LINEFEED
;
1215 s
= define_find(tok
);
1216 /* undefine symbol by putting an invalid name */
1221 case TOK_INCLUDE_NEXT
:
1222 ch
= file
->buf_ptr
[0];
1223 /* XXX: incorrect if comments : use next_nomacro with a special mode */
1228 } else if (ch
== '\"') {
1233 while (ch
!= c
&& ch
!= '\n' && ch
!= CH_EOF
) {
1234 if ((q
- buf
) < sizeof(buf
) - 1)
1237 if (handle_stray_noerror() == 0)
1245 /* eat all spaces and comments after include */
1246 /* XXX: slightly incorrect */
1247 while (ch1
!= '\n' && ch1
!= CH_EOF
)
1251 /* computed #include : either we have only strings or
1252 we have anything enclosed in '<>' */
1255 if (tok
== TOK_STR
) {
1256 while (tok
!= TOK_LINEFEED
) {
1257 if (tok
!= TOK_STR
) {
1259 error("'#include' expects \"FILENAME\" or <FILENAME>");
1261 pstrcat(buf
, sizeof(buf
), (char *)tokc
.cstr
->data
);
1267 while (tok
!= TOK_LINEFEED
) {
1268 pstrcat(buf
, sizeof(buf
), get_tok_str(tok
, &tokc
));
1272 /* check syntax and remove '<>' */
1273 if (len
< 2 || buf
[0] != '<' || buf
[len
- 1] != '>')
1274 goto include_syntax
;
1275 memmove(buf
, buf
+ 1, len
- 2);
1276 buf
[len
- 2] = '\0';
1281 if (s1
->include_stack_ptr
>= s1
->include_stack
+ INCLUDE_STACK_SIZE
)
1282 error("#include recursion too deep");
1284 n
= s1
->nb_include_paths
+ s1
->nb_sysinclude_paths
;
1285 for (i
= -2; i
< n
; ++i
) {
1286 char buf1
[sizeof file
->filename
];
1293 /* check absolute include path */
1294 if (!IS_ABSPATH(buf
))
1298 } else if (i
== -1) {
1299 /* search in current dir if "header.h" */
1302 size
= tcc_basename(file
->filename
) - file
->filename
;
1303 memcpy(buf1
, file
->filename
, size
);
1307 /* search in all the include paths */
1308 if (i
< s1
->nb_include_paths
)
1309 path
= s1
->include_paths
[i
];
1311 path
= s1
->sysinclude_paths
[i
- s1
->nb_include_paths
];
1312 pstrcpy(buf1
, sizeof(buf1
), path
);
1313 pstrcat(buf1
, sizeof(buf1
), "/");
1316 pstrcat(buf1
, sizeof(buf1
), buf
);
1318 e
= search_cached_include(s1
, c
, buf1
);
1319 if (e
&& define_find(e
->ifndef_macro
)) {
1320 /* no need to parse the include because the 'ifndef macro'
1323 printf("%s: skipping %s\n", file
->filename
, buf
);
1327 f
= tcc_open(s1
, buf1
);
1332 if (tok
== TOK_INCLUDE_NEXT
) {
1343 printf("%s: including %s\n", file
->filename
, buf1
);
1346 /* XXX: fix current line init */
1347 /* push current file in stack */
1348 *s1
->include_stack_ptr
++ = file
;
1350 pstrcpy(f
->inc_filename
, sizeof(f
->inc_filename
), buf1
);
1352 /* add include file debug info */
1353 if (tcc_state
->do_debug
) {
1354 put_stabs(file
->filename
, N_BINCL
, 0, 0, 0);
1356 tok_flags
|= TOK_FLAG_BOF
| TOK_FLAG_BOL
;
1357 ch
= file
->buf_ptr
[0];
1360 error("include file '%s' not found", buf
);
1367 c
= expr_preprocess();
1373 if (tok
< TOK_IDENT
)
1374 error("invalid argument for '#if%sdef'", c
? "n" : "");
1378 printf("#ifndef %s\n", get_tok_str(tok
, NULL
));
1380 file
->ifndef_macro
= tok
;
1383 c
= (define_find(tok
) != 0) ^ c
;
1385 if (s1
->ifdef_stack_ptr
>= s1
->ifdef_stack
+ IFDEF_STACK_SIZE
)
1386 error("memory full");
1387 *s1
->ifdef_stack_ptr
++ = c
;
1390 if (s1
->ifdef_stack_ptr
== s1
->ifdef_stack
)
1391 error("#else without matching #if");
1392 if (s1
->ifdef_stack_ptr
[-1] & 2)
1393 error("#else after #else");
1394 c
= (s1
->ifdef_stack_ptr
[-1] ^= 3);
1397 if (s1
->ifdef_stack_ptr
== s1
->ifdef_stack
)
1398 error("#elif without matching #if");
1399 c
= s1
->ifdef_stack_ptr
[-1];
1401 error("#elif after #else");
1402 /* last #if/#elif expression was true: we skip */
1405 c
= expr_preprocess();
1406 s1
->ifdef_stack_ptr
[-1] = c
;
1408 if (s1
->ifdef_stack_ptr
== file
->ifdef_stack_ptr
+ 1)
1409 file
->ifndef_macro
= 0;
1419 if (s1
->ifdef_stack_ptr
<= file
->ifdef_stack_ptr
)
1420 error("#endif without matching #if");
1421 s1
->ifdef_stack_ptr
--;
1422 /* '#ifndef macro' was at the start of file. Now we check if
1423 an '#endif' is exactly at the end of file */
1424 if (file
->ifndef_macro
&&
1425 s1
->ifdef_stack_ptr
== file
->ifdef_stack_ptr
) {
1426 file
->ifndef_macro_saved
= file
->ifndef_macro
;
1427 /* need to set to zero to avoid false matches if another
1428 #ifndef at middle of file */
1429 file
->ifndef_macro
= 0;
1430 while (tok
!= TOK_LINEFEED
)
1432 tok_flags
|= TOK_FLAG_ENDIF
;
1438 if (tok
!= TOK_CINT
)
1440 file
->line_num
= tokc
.i
- 1; /* the line number will be incremented after */
1442 if (tok
!= TOK_LINEFEED
) {
1445 pstrcpy(file
->filename
, sizeof(file
->filename
),
1446 (char *)tokc
.cstr
->data
);
1452 ch
= file
->buf_ptr
[0];
1455 while (ch
!= '\n' && ch
!= CH_EOF
) {
1456 if ((q
- buf
) < sizeof(buf
) - 1)
1459 if (handle_stray_noerror() == 0)
1466 error("#error %s", buf
);
1468 warning("#warning %s", buf
);
1474 if (tok
== TOK_LINEFEED
|| tok
== '!' || tok
== TOK_CINT
) {
1475 /* '!' is ignored to allow C scripts. numbers are ignored
1476 to emulate cpp behaviour */
1478 if (!(saved_parse_flags
& PARSE_FLAG_ASM_COMMENTS
))
1479 warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok
, &tokc
));
1483 /* ignore other preprocess commands or #! for C scripts */
1484 while (tok
!= TOK_LINEFEED
)
1487 parse_flags
= saved_parse_flags
;
1490 /* evaluate escape codes in a string. */
1491 static void parse_escape_string(CString
*outstr
, const uint8_t *buf
, int is_long
)
1506 case '0': case '1': case '2': case '3':
1507 case '4': case '5': case '6': case '7':
1508 /* at most three octal digits */
1513 n
= n
* 8 + c
- '0';
1517 n
= n
* 8 + c
- '0';
1522 goto add_char_nonext
;
1530 if (c
>= 'a' && c
<= 'f')
1532 else if (c
>= 'A' && c
<= 'F')
1542 goto add_char_nonext
;
1566 goto invalid_escape
;
1576 if (c
>= '!' && c
<= '~')
1577 warning("unknown escape sequence: \'\\%c\'", c
);
1579 warning("unknown escape sequence: \'\\x%x\'", c
);
1586 cstr_ccat(outstr
, c
);
1588 cstr_wccat(outstr
, c
);
1590 /* add a trailing '\0' */
1592 cstr_ccat(outstr
, '\0');
1594 cstr_wccat(outstr
, '\0');
1597 /* we use 64 bit numbers */
1600 /* bn = (bn << shift) | or_val */
1601 void bn_lshift(unsigned int *bn
, int shift
, int or_val
)
1605 for(i
=0;i
<BN_SIZE
;i
++) {
1607 bn
[i
] = (v
<< shift
) | or_val
;
1608 or_val
= v
>> (32 - shift
);
1612 void bn_zero(unsigned int *bn
)
1615 for(i
=0;i
<BN_SIZE
;i
++) {
1620 /* parse number in null terminated string 'p' and return it in the
1622 void parse_number(const char *p
)
1624 int b
, t
, shift
, frac_bits
, s
, exp_val
, ch
;
1626 unsigned int bn
[BN_SIZE
];
1637 goto float_frac_parse
;
1638 } else if (t
== '0') {
1639 if (ch
== 'x' || ch
== 'X') {
1643 } else if (tcc_ext
&& (ch
== 'b' || ch
== 'B')) {
1649 /* parse all digits. cannot check octal numbers at this stage
1650 because of floating point constants */
1652 if (ch
>= 'a' && ch
<= 'f')
1654 else if (ch
>= 'A' && ch
<= 'F')
1662 if (q
>= token_buf
+ STRING_MAX_SIZE
) {
1664 error("number too long");
1670 ((ch
== 'e' || ch
== 'E') && b
== 10) ||
1671 ((ch
== 'p' || ch
== 'P') && (b
== 16 || b
== 2))) {
1673 /* NOTE: strtox should support that for hexa numbers, but
1674 non ISOC99 libcs do not support it, so we prefer to do
1676 /* hexadecimal or binary floats */
1677 /* XXX: handle overflows */
1689 } else if (t
>= 'a') {
1691 } else if (t
>= 'A') {
1696 bn_lshift(bn
, shift
, t
);
1703 if (t
>= 'a' && t
<= 'f') {
1705 } else if (t
>= 'A' && t
<= 'F') {
1707 } else if (t
>= '0' && t
<= '9') {
1713 error("invalid digit");
1714 bn_lshift(bn
, shift
, t
);
1719 if (ch
!= 'p' && ch
!= 'P')
1726 } else if (ch
== '-') {
1730 if (ch
< '0' || ch
> '9')
1731 expect("exponent digits");
1732 while (ch
>= '0' && ch
<= '9') {
1733 exp_val
= exp_val
* 10 + ch
- '0';
1736 exp_val
= exp_val
* s
;
1738 /* now we can generate the number */
1739 /* XXX: should patch directly float number */
1740 d
= (double)bn
[1] * 4294967296.0 + (double)bn
[0];
1741 d
= ldexp(d
, exp_val
- frac_bits
);
1746 /* float : should handle overflow */
1748 } else if (t
== 'L') {
1750 #ifdef TCC_TARGET_PE
1755 /* XXX: not large enough */
1756 tokc
.ld
= (long double)d
;
1763 /* decimal floats */
1765 if (q
>= token_buf
+ STRING_MAX_SIZE
)
1770 while (ch
>= '0' && ch
<= '9') {
1771 if (q
>= token_buf
+ STRING_MAX_SIZE
)
1777 if (ch
== 'e' || ch
== 'E') {
1778 if (q
>= token_buf
+ STRING_MAX_SIZE
)
1782 if (ch
== '-' || ch
== '+') {
1783 if (q
>= token_buf
+ STRING_MAX_SIZE
)
1788 if (ch
< '0' || ch
> '9')
1789 expect("exponent digits");
1790 while (ch
>= '0' && ch
<= '9') {
1791 if (q
>= token_buf
+ STRING_MAX_SIZE
)
1803 tokc
.f
= strtof(token_buf
, NULL
);
1804 } else if (t
== 'L') {
1806 #ifdef TCC_TARGET_PE
1808 tokc
.d
= strtod(token_buf
, NULL
);
1811 tokc
.ld
= strtold(token_buf
, NULL
);
1815 tokc
.d
= strtod(token_buf
, NULL
);
1819 unsigned long long n
, n1
;
1822 /* integer number */
1825 if (b
== 10 && *q
== '0') {
1832 /* no need for checks except for base 10 / 8 errors */
1835 } else if (t
>= 'a') {
1837 } else if (t
>= 'A') {
1842 error("invalid digit");
1846 /* detect overflow */
1847 /* XXX: this test is not reliable */
1849 error("integer constant overflow");
1852 /* XXX: not exactly ANSI compliant */
1853 if ((n
& 0xffffffff00000000LL
) != 0) {
1858 } else if (n
> 0x7fffffff) {
1869 error("three 'l's in integer constant");
1872 if (tok
== TOK_CINT
)
1874 else if (tok
== TOK_CUINT
)
1878 } else if (t
== 'U') {
1880 error("two 'u's in integer constant");
1882 if (tok
== TOK_CINT
)
1884 else if (tok
== TOK_CLLONG
)
1891 if (tok
== TOK_CINT
|| tok
== TOK_CUINT
)
1897 error("invalid number\n");
1901 #define PARSE2(c1, tok1, c2, tok2) \
1912 /* return next token without macro substitution */
1913 static inline void next_nomacro1(void)
1928 goto keep_tok_flags
;
1935 /* first look if it is in fact an end of buffer */
1936 if (p
>= file
->buf_end
) {
1940 if (p
>= file
->buf_end
)
1953 TCCState
*s1
= tcc_state
;
1954 if ((parse_flags
& PARSE_FLAG_LINEFEED
)
1955 && !(tok_flags
& TOK_FLAG_EOF
)) {
1956 tok_flags
|= TOK_FLAG_EOF
;
1958 goto keep_tok_flags
;
1959 } else if (s1
->include_stack_ptr
== s1
->include_stack
||
1960 !(parse_flags
& PARSE_FLAG_PREPROCESS
)) {
1961 /* no include left : end of file. */
1964 tok_flags
&= ~TOK_FLAG_EOF
;
1965 /* pop include file */
1967 /* test if previous '#endif' was after a #ifdef at
1969 if (tok_flags
& TOK_FLAG_ENDIF
) {
1971 printf("#endif %s\n", get_tok_str(file
->ifndef_macro_saved
, NULL
));
1973 add_cached_include(s1
, file
->inc_type
, file
->inc_filename
,
1974 file
->ifndef_macro_saved
);
1977 /* add end of include file debug info */
1978 if (tcc_state
->do_debug
) {
1979 put_stabd(N_EINCL
, 0, 0);
1981 /* pop include stack */
1983 s1
->include_stack_ptr
--;
1984 file
= *s1
->include_stack_ptr
;
1993 tok_flags
|= TOK_FLAG_BOL
;
1996 if (0 == (parse_flags
& PARSE_FLAG_LINEFEED
))
1999 goto keep_tok_flags
;
2004 if ((tok_flags
& TOK_FLAG_BOL
) &&
2005 (parse_flags
& PARSE_FLAG_PREPROCESS
)) {
2007 preprocess(tok_flags
& TOK_FLAG_BOF
);
2013 tok
= TOK_TWOSHARPS
;
2015 if (parse_flags
& PARSE_FLAG_ASM_COMMENTS
) {
2016 p
= parse_line_comment(p
- 1);
2025 case 'a': case 'b': case 'c': case 'd':
2026 case 'e': case 'f': case 'g': case 'h':
2027 case 'i': case 'j': case 'k': case 'l':
2028 case 'm': case 'n': case 'o': case 'p':
2029 case 'q': case 'r': case 's': case 't':
2030 case 'u': case 'v': case 'w': case 'x':
2032 case 'A': case 'B': case 'C': case 'D':
2033 case 'E': case 'F': case 'G': case 'H':
2034 case 'I': case 'J': case 'K':
2035 case 'M': case 'N': case 'O': case 'P':
2036 case 'Q': case 'R': case 'S': case 'T':
2037 case 'U': case 'V': case 'W': case 'X':
2043 h
= TOK_HASH_FUNC(h
, c
);
2047 if (!isidnum_table
[c
-CH_EOF
])
2049 h
= TOK_HASH_FUNC(h
, c
);
2056 /* fast case : no stray found, so we have the full token
2057 and we have already hashed it */
2059 h
&= (TOK_HASH_SIZE
- 1);
2060 pts
= &hash_ident
[h
];
2065 if (ts
->len
== len
&& !memcmp(ts
->str
, p1
, len
))
2067 pts
= &(ts
->hash_next
);
2069 ts
= tok_alloc_new(pts
, p1
, len
);
2073 cstr_reset(&tokcstr
);
2076 cstr_ccat(&tokcstr
, *p1
);
2082 while (isidnum_table
[c
-CH_EOF
]) {
2083 cstr_ccat(&tokcstr
, c
);
2086 ts
= tok_alloc(tokcstr
.data
, tokcstr
.size
);
2092 if (t
!= '\\' && t
!= '\'' && t
!= '\"') {
2094 goto parse_ident_fast
;
2097 if (c
== '\'' || c
== '\"') {
2101 cstr_reset(&tokcstr
);
2102 cstr_ccat(&tokcstr
, 'L');
2103 goto parse_ident_slow
;
2107 case '0': case '1': case '2': case '3':
2108 case '4': case '5': case '6': case '7':
2111 cstr_reset(&tokcstr
);
2112 /* after the first digit, accept digits, alpha, '.' or sign if
2113 prefixed by 'eEpP' */
2117 cstr_ccat(&tokcstr
, c
);
2119 if (!(isnum(c
) || isid(c
) || c
== '.' ||
2120 ((c
== '+' || c
== '-') &&
2121 (t
== 'e' || t
== 'E' || t
== 'p' || t
== 'P'))))
2124 /* We add a trailing '\0' to ease parsing */
2125 cstr_ccat(&tokcstr
, '\0');
2126 tokc
.cstr
= &tokcstr
;
2130 /* special dot handling because it can also start a number */
2133 cstr_reset(&tokcstr
);
2134 cstr_ccat(&tokcstr
, '.');
2136 } else if (c
== '.') {
2156 /* parse the string */
2158 p
= parse_pp_string(p
, sep
, &str
);
2159 cstr_ccat(&str
, '\0');
2161 /* eval the escape (should be done as TOK_PPNUM) */
2162 cstr_reset(&tokcstr
);
2163 parse_escape_string(&tokcstr
, str
.data
, is_long
);
2168 /* XXX: make it portable */
2172 char_size
= sizeof(nwchar_t
);
2173 if (tokcstr
.size
<= char_size
)
2174 error("empty character constant");
2175 if (tokcstr
.size
> 2 * char_size
)
2176 warning("multi-character character constant");
2178 tokc
.i
= *(int8_t *)tokcstr
.data
;
2181 tokc
.i
= *(nwchar_t
*)tokcstr
.data
;
2185 tokc
.cstr
= &tokcstr
;
2199 } else if (c
== '<') {
2217 } else if (c
== '>') {
2235 } else if (c
== '=') {
2248 } else if (c
== '=') {
2261 } else if (c
== '=') {
2274 } else if (c
== '=') {
2277 } else if (c
== '>') {
2285 PARSE2('!', '!', '=', TOK_NE
)
2286 PARSE2('=', '=', '=', TOK_EQ
)
2287 PARSE2('*', '*', '=', TOK_A_MUL
)
2288 PARSE2('%', '%', '=', TOK_A_MOD
)
2289 PARSE2('^', '^', '=', TOK_A_XOR
)
2291 /* comments or operator */
2295 p
= parse_comment(p
);
2297 } else if (c
== '/') {
2298 p
= parse_line_comment(p
);
2300 } else if (c
== '=') {
2320 case '$': /* only used in assembler */
2321 case '@': /* dito */
2326 error("unrecognized character \\x%02x", c
);
2332 #if defined(PARSE_DEBUG)
2333 printf("token = %s\n", get_tok_str(tok
, &tokc
));
2337 /* return next token without macro substitution. Can read input from
2339 static void next_nomacro_spc(void)
2345 TOK_GET(tok
, macro_ptr
, tokc
);
2346 if (tok
== TOK_LINENUM
) {
2347 file
->line_num
= tokc
.i
;
2356 static void next_nomacro(void)
2360 } while (is_space(tok
));
2363 /* substitute args in macro_str and return allocated string */
2364 static int *macro_arg_subst(Sym
**nested_list
, int *macro_str
, Sym
*args
)
2366 int *st
, last_tok
, t
, spc
;
2375 TOK_GET(t
, macro_str
, cval
);
2380 TOK_GET(t
, macro_str
, cval
);
2383 s
= sym_find2(args
, t
);
2389 TOK_GET(t
, st
, cval
);
2390 if (!check_space(t
, &spc
))
2391 cstr_cat(&cstr
, get_tok_str(t
, &cval
));
2394 cstr_ccat(&cstr
, '\0');
2396 printf("stringize: %s\n", (char *)cstr
.data
);
2400 tok_str_add2(&str
, TOK_STR
, &cval
);
2403 tok_str_add2(&str
, t
, &cval
);
2405 } else if (t
>= TOK_IDENT
) {
2406 s
= sym_find2(args
, t
);
2409 /* if '##' is present before or after, no arg substitution */
2410 if (*macro_str
== TOK_TWOSHARPS
|| last_tok
== TOK_TWOSHARPS
) {
2411 /* special case for var arg macros : ## eats the
2412 ',' if empty VA_ARGS variable. */
2413 /* XXX: test of the ',' is not 100%
2414 reliable. should fix it to avoid security
2416 if (gnu_ext
&& s
->type
.t
&&
2417 last_tok
== TOK_TWOSHARPS
&&
2418 str
.len
>= 2 && str
.str
[str
.len
- 2] == ',') {
2420 /* suppress ',' '##' */
2423 /* suppress '##' and add variable */
2431 TOK_GET(t1
, st
, cval
);
2434 tok_str_add2(&str
, t1
, &cval
);
2438 /* NOTE: the stream cannot be read when macro
2439 substituing an argument */
2440 macro_subst(&str
, nested_list
, st
, NULL
);
2443 tok_str_add(&str
, t
);
2446 tok_str_add2(&str
, t
, &cval
);
2450 tok_str_add(&str
, 0);
2454 static char const ab_month_name
[12][4] =
2456 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2457 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
2460 /* do macro substitution of current token with macro 's' and add
2461 result to (tok_str,tok_len). 'nested_list' is the list of all
2462 macros we got inside to avoid recursing. Return non zero if no
2463 substitution needs to be done */
2464 static int macro_subst_tok(TokenString
*tok_str
,
2465 Sym
**nested_list
, Sym
*s
, struct macro_level
**can_read_stream
)
2467 Sym
*args
, *sa
, *sa1
;
2468 int mstr_allocated
, parlevel
, *mstr
, t
, t1
, *p
, spc
;
2475 /* if symbol is a macro, prepare substitution */
2476 /* special macros */
2477 if (tok
== TOK___LINE__
) {
2478 snprintf(buf
, sizeof(buf
), "%d", file
->line_num
);
2482 } else if (tok
== TOK___FILE__
) {
2483 cstrval
= file
->filename
;
2485 } else if (tok
== TOK___DATE__
|| tok
== TOK___TIME__
) {
2490 tm
= localtime(&ti
);
2491 if (tok
== TOK___DATE__
) {
2492 snprintf(buf
, sizeof(buf
), "%s %2d %d",
2493 ab_month_name
[tm
->tm_mon
], tm
->tm_mday
, tm
->tm_year
+ 1900);
2495 snprintf(buf
, sizeof(buf
), "%02d:%02d:%02d",
2496 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
2503 cstr_cat(&cstr
, cstrval
);
2504 cstr_ccat(&cstr
, '\0');
2506 tok_str_add2(tok_str
, t1
, &cval
);
2511 if (s
->type
.t
== MACRO_FUNC
) {
2512 /* NOTE: we do not use next_nomacro to avoid eating the
2513 next token. XXX: find better solution */
2517 while (is_space(t
= *p
) || TOK_LINEFEED
== t
)
2519 if (t
== 0 && can_read_stream
) {
2520 /* end of macro stream: we must look at the token
2521 after in the file */
2522 struct macro_level
*ml
= *can_read_stream
;
2528 *can_read_stream
= ml
-> prev
;
2533 /* XXX: incorrect with comments */
2534 ch
= file
->buf_ptr
[0];
2535 while (is_space(ch
) || ch
== '\n')
2539 if (t
!= '(') /* no macro subst */
2542 /* argument macro */
2547 /* NOTE: empty args are allowed, except if no args */
2549 /* handle '()' case */
2550 if (!args
&& !sa
&& tok
== ')')
2553 error("macro '%s' used with too many args",
2554 get_tok_str(s
->v
, 0));
2557 /* NOTE: non zero sa->t indicates VA_ARGS */
2558 while ((parlevel
> 0 ||
2560 (tok
!= ',' || sa
->type
.t
))) &&
2564 else if (tok
== ')')
2566 if (tok
== TOK_LINEFEED
)
2568 if (!check_space(tok
, &spc
))
2569 tok_str_add2(&str
, tok
, &tokc
);
2573 tok_str_add(&str
, 0);
2574 sa1
= sym_push2(&args
, sa
->v
& ~SYM_FIELD
, sa
->type
.t
, 0);
2578 /* special case for gcc var args: add an empty
2579 var arg argument if it is omitted */
2580 if (sa
&& sa
->type
.t
&& gnu_ext
)
2590 error("macro '%s' used with too few args",
2591 get_tok_str(s
->v
, 0));
2594 /* now subst each arg */
2595 mstr
= macro_arg_subst(nested_list
, mstr
, args
);
2600 tok_str_free(sa
->d
);
2606 sym_push2(nested_list
, s
->v
, 0, 0);
2607 macro_subst(tok_str
, nested_list
, mstr
, can_read_stream
);
2608 /* pop nested defined symbol */
2610 *nested_list
= sa1
->prev
;
2618 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
2619 return the resulting string (which must be freed). */
2620 static inline int *macro_twosharps(const int *macro_str
)
2625 TokenString macro_str1
;
2630 /* we search the first '##' */
2631 for(ptr
= macro_str
;;) {
2632 TOK_GET(t
, ptr
, cval
);
2633 if (t
== TOK_TWOSHARPS
)
2635 /* nothing more to do if end of string */
2640 /* we saw '##', so we need more processing to handle it */
2641 tok_str_new(¯o_str1
);
2642 for(ptr
= macro_str
;;) {
2643 TOK_GET(tok
, ptr
, tokc
);
2646 if (tok
== TOK_TWOSHARPS
)
2648 while (*ptr
== TOK_TWOSHARPS
) {
2650 if (t
&& t
!= TOK_TWOSHARPS
) {
2651 TOK_GET(t
, ptr
, cval
);
2653 /* We concatenate the two tokens */
2655 cstr_cat(&cstr
, get_tok_str(tok
, &tokc
));
2657 cstr_cat(&cstr
, get_tok_str(t
, &cval
));
2658 cstr_ccat(&cstr
, '\0');
2661 file
->buf_ptr
= cstr
.data
;
2664 if (0 == *file
->buf_ptr
)
2666 tok_str_add2(¯o_str1
, tok
, &tokc
);
2668 warning("pasting \"%.*s\" and \"%s\" does not give a valid preprocessing token",
2669 n
, cstr
.data
, (char*)cstr
.data
+ n
);
2675 tok_str_add2(¯o_str1
, tok
, &tokc
);
2677 tok_str_add(¯o_str1
, 0);
2678 return macro_str1
.str
;
2682 /* do macro substitution of macro_str and add result to
2683 (tok_str,tok_len). 'nested_list' is the list of all macros we got
2684 inside to avoid recursing. */
2685 static void macro_subst(TokenString
*tok_str
, Sym
**nested_list
,
2686 const int *macro_str
, struct macro_level
** can_read_stream
)
2693 struct macro_level ml
;
2695 /* first scan for '##' operator handling */
2697 macro_str1
= macro_twosharps(ptr
);
2702 /* NOTE: ptr == NULL can only happen if tokens are read from
2703 file stream due to a macro function call */
2706 TOK_GET(t
, ptr
, cval
);
2711 /* if nested substitution, do nothing */
2712 if (sym_find2(*nested_list
, t
))
2715 if (can_read_stream
)
2716 ml
.prev
= *can_read_stream
, *can_read_stream
= &ml
;
2717 macro_ptr
= (int *)ptr
;
2719 ret
= macro_subst_tok(tok_str
, nested_list
, s
, can_read_stream
);
2720 ptr
= (int *)macro_ptr
;
2722 if (can_read_stream
&& *can_read_stream
== &ml
)
2723 *can_read_stream
= ml
.prev
;
2728 if (!check_space(t
, &spc
))
2729 tok_str_add2(tok_str
, t
, &cval
);
2733 tok_str_free(macro_str1
);
2736 /* return next token with macro substitution */
2737 static void next(void)
2739 Sym
*nested_list
, *s
;
2741 struct macro_level
*ml
;
2744 if (parse_flags
& PARSE_FLAG_SPACES
)
2749 /* if not reading from macro substituted string, then try
2750 to substitute macros */
2751 if (tok
>= TOK_IDENT
&&
2752 (parse_flags
& PARSE_FLAG_PREPROCESS
)) {
2753 s
= define_find(tok
);
2755 /* we have a macro: we try to substitute */
2759 if (macro_subst_tok(&str
, &nested_list
, s
, &ml
) == 0) {
2760 /* substitution done, NOTE: maybe empty */
2761 tok_str_add(&str
, 0);
2762 macro_ptr
= str
.str
;
2763 macro_ptr_allocated
= str
.str
;
2770 /* end of macro or end of unget buffer */
2771 if (unget_buffer_enabled
) {
2772 macro_ptr
= unget_saved_macro_ptr
;
2773 unget_buffer_enabled
= 0;
2775 /* end of macro string: free it */
2776 tok_str_free(macro_ptr_allocated
);
2777 macro_ptr_allocated
= NULL
;
2784 /* convert preprocessor tokens into C tokens */
2785 if (tok
== TOK_PPNUM
&&
2786 (parse_flags
& PARSE_FLAG_TOK_NUM
)) {
2787 parse_number((char *)tokc
.cstr
->data
);
2791 /* push back current token and set current token to 'last_tok'. Only
2792 identifier case handled for labels. */
2793 static inline void unget_tok(int last_tok
)
2797 unget_saved_macro_ptr
= macro_ptr
;
2798 unget_buffer_enabled
= 1;
2799 q
= unget_saved_buffer
;
2802 n
= tok_ext_size(tok
) - 1;
2805 *q
= 0; /* end of token string */
2810 /* better than nothing, but needs extension to handle '-E' option
2812 static void preprocess_init(TCCState
*s1
)
2814 s1
->include_stack_ptr
= s1
->include_stack
;
2815 /* XXX: move that before to avoid having to initialize
2816 file->ifdef_stack_ptr ? */
2817 s1
->ifdef_stack_ptr
= s1
->ifdef_stack
;
2818 file
->ifdef_stack_ptr
= s1
->ifdef_stack_ptr
;
2820 /* XXX: not ANSI compliant: bound checking says error */
2822 s1
->pack_stack
[0] = 0;
2823 s1
->pack_stack_ptr
= s1
->pack_stack
;
2826 void preprocess_new()
2832 /* init isid table */
2833 for(i
=CH_EOF
;i
<256;i
++)
2834 isidnum_table
[i
-CH_EOF
] = isid(i
) || isnum(i
);
2836 /* add all tokens */
2838 memset(hash_ident
, 0, TOK_HASH_SIZE
* sizeof(TokenSym
*));
2840 tok_ident
= TOK_IDENT
;
2849 ts
= tok_alloc(p
, r
- p
- 1);
2854 /* Preprocess the current file */
2855 static int tcc_preprocess(TCCState
*s1
)
2858 BufferedFile
*file_ref
, **iptr
, **iptr_new
;
2859 int token_seen
, line_ref
, d
;
2862 preprocess_init(s1
);
2863 define_start
= define_stack
;
2864 ch
= file
->buf_ptr
[0];
2865 tok_flags
= TOK_FLAG_BOL
| TOK_FLAG_BOF
;
2866 parse_flags
= PARSE_FLAG_ASM_COMMENTS
| PARSE_FLAG_PREPROCESS
|
2867 PARSE_FLAG_LINEFEED
| PARSE_FLAG_SPACES
;
2872 iptr
= s1
->include_stack_ptr
;
2875 if (tok
== TOK_EOF
) {
2877 } else if (file
!= file_ref
) {
2879 } else if (tok
== TOK_LINEFEED
) {
2884 } else if (!token_seen
) {
2885 d
= file
->line_num
- line_ref
;
2886 if (file
!= file_ref
|| d
< 0 || d
>= 8) {
2888 iptr_new
= s1
->include_stack_ptr
;
2889 s
= iptr_new
> iptr
? " 1"
2890 : iptr_new
< iptr
? " 2"
2891 : iptr_new
> s1
->include_stack
? " 3"
2895 fprintf(s1
->outfile
, "# %d \"%s\"%s\n", file
->line_num
, file
->filename
, s
);
2898 fputs("\n", s1
->outfile
), --d
;
2900 line_ref
= (file_ref
= file
)->line_num
;
2901 token_seen
= tok
!= TOK_LINEFEED
;
2905 fputs(get_tok_str(tok
, &tokc
), s1
->outfile
);
2907 free_defines(define_start
);