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_spc(void);
43 static void macro_subst(TokenString
*tok_str
, Sym
**nested_list
,
44 const int *macro_str
, struct macro_level
**can_read_stream
);
47 /* allocate a new token */
48 static TokenSym
*tok_alloc_new(TokenSym
**pts
, const char *str
, int len
)
50 TokenSym
*ts
, **ptable
;
53 if (tok_ident
>= SYM_FIRST_ANOM
)
56 /* expand token table if needed */
57 i
= tok_ident
- TOK_IDENT
;
58 if ((i
% TOK_ALLOC_INCR
) == 0) {
59 ptable
= tcc_realloc(table_ident
, (i
+ TOK_ALLOC_INCR
) * sizeof(TokenSym
*));
65 ts
= tcc_malloc(sizeof(TokenSym
) + len
);
67 ts
->tok
= tok_ident
++;
68 ts
->sym_define
= NULL
;
70 ts
->sym_struct
= NULL
;
71 ts
->sym_identifier
= NULL
;
74 memcpy(ts
->str
, str
, len
);
80 #define TOK_HASH_INIT 1
81 #define TOK_HASH_FUNC(h, c) ((h) * 263 + (c))
83 /* find a token and add it if not found */
84 TokenSym
*tok_alloc(const char *str
, int len
)
92 h
= TOK_HASH_FUNC(h
, ((unsigned char *)str
)[i
]);
93 h
&= (TOK_HASH_SIZE
- 1);
100 if (ts
->len
== len
&& !memcmp(ts
->str
, str
, len
))
102 pts
= &(ts
->hash_next
);
104 return tok_alloc_new(pts
, str
, len
);
107 /* XXX: buffer overflow */
108 /* XXX: float tokens */
109 char *get_tok_str(int v
, CValue
*cv
)
111 static char buf
[STRING_MAX_SIZE
+ 1];
112 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 */
185 const unsigned char *q
= tok_two_chars
;
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
247 return tcc_peekc_slow(file
);
250 /* handle '\[\r]\n' */
251 static int handle_stray_noerror(void)
258 } else if (ch
== '\r') {
272 static void handle_stray(void)
274 if (handle_stray_noerror())
275 error("stray '\\' in program");
278 /* skip the stray and handle the \\n case. Output an error if
279 incorrect char after the stray */
280 static int handle_stray1(uint8_t *p
)
284 if (p
>= file
->buf_end
) {
301 /* handle just the EOB case, but not stray */
302 #define PEEKC_EOB(c, p)\
313 /* handle the complicated stray case */
319 c = handle_stray1(p);\
324 /* input with '\[\r]\n' handling. Note that this function cannot
325 handle other characters after '\', so you cannot call it inside
326 strings or comments */
335 /* single line C++ comments */
336 static uint8_t *parse_line_comment(uint8_t *p
)
344 if (c
== '\n' || c
== CH_EOF
) {
346 } else if (c
== '\\') {
355 } else if (c
== '\r') {
373 uint8_t *parse_comment(uint8_t *p
)
382 if (c
== '\n' || c
== '*' || c
== '\\')
386 if (c
== '\n' || c
== '*' || c
== '\\')
390 /* now we can handle all the cases */
394 } else if (c
== '*') {
400 } else if (c
== '/') {
402 } else if (c
== '\\') {
407 /* skip '\[\r]\n', otherwise just skip the stray */
413 } else if (c
== '\r') {
430 /* stray, eob or eof */
435 error("unexpected end of file in comment");
436 } else if (c
== '\\') {
448 static inline void skip_spaces(void)
454 static inline int check_space(int t
, int *spc
)
465 /* parse a string without interpreting escapes */
466 static uint8_t *parse_pp_string(uint8_t *p
,
467 int sep
, CString
*str
)
475 } else if (c
== '\\') {
481 /* XXX: indicate line number of start of string */
482 error("missing terminating %c character", sep
);
483 } else if (c
== '\\') {
484 /* escape : just skip \[\r]\n */
489 } else if (c
== '\r') {
492 expect("'\n' after '\r'");
495 } else if (c
== CH_EOF
) {
496 goto unterminated_string
;
499 cstr_ccat(str
, '\\');
505 } else if (c
== '\n') {
508 } else if (c
== '\r') {
512 cstr_ccat(str
, '\r');
528 /* skip block of text until #else, #elif or #endif. skip also pairs of
530 void preprocess_skip(void)
532 int a
, start_of_line
, c
, in_warn_or_error
;
539 in_warn_or_error
= 0;
560 } else if (c
== '\\') {
561 ch
= file
->buf_ptr
[0];
562 handle_stray_noerror();
569 if (in_warn_or_error
)
571 p
= parse_pp_string(p
, c
, NULL
);
575 if (in_warn_or_error
)
582 p
= parse_comment(p
);
583 } else if (ch
== '/') {
584 p
= parse_line_comment(p
);
594 (tok
== TOK_ELSE
|| tok
== TOK_ELIF
|| tok
== TOK_ENDIF
))
596 if (tok
== TOK_IF
|| tok
== TOK_IFDEF
|| tok
== TOK_IFNDEF
)
598 else if (tok
== TOK_ENDIF
)
600 else if( tok
== TOK_ERROR
|| tok
== TOK_WARNING
)
601 in_warn_or_error
= 1;
615 /* ParseState handling */
617 /* XXX: currently, no include file info is stored. Thus, we cannot display
618 accurate messages if the function or data definition spans multiple
621 /* save current parse state in 's' */
622 void save_parse_state(ParseState
*s
)
624 s
->line_num
= file
->line_num
;
625 s
->macro_ptr
= macro_ptr
;
630 /* restore parse state from 's' */
631 void restore_parse_state(ParseState
*s
)
633 file
->line_num
= s
->line_num
;
634 macro_ptr
= s
->macro_ptr
;
640 void tok_str_free(int *str
)
645 static int *tok_str_realloc(TokenString
*s
)
649 if (s
->allocated_len
== 0) {
652 len
= s
->allocated_len
* 2;
654 str
= tcc_realloc(s
->str
, len
* sizeof(int));
656 error("memory full");
657 s
->allocated_len
= len
;
662 void tok_str_add(TokenString
*s
, int t
)
668 if (len
>= s
->allocated_len
)
669 str
= tok_str_realloc(s
);
674 static void tok_str_add2(TokenString
*s
, int t
, CValue
*cv
)
681 /* allocate space for worst case */
682 if (len
+ TOK_MAX_SIZE
> s
->allocated_len
)
683 str
= tok_str_realloc(s
);
692 str
[len
++] = cv
->tab
[0];
701 nb_words
= (sizeof(CString
) + cv
->cstr
->size
+ 3) >> 2;
702 while ((len
+ nb_words
) > s
->allocated_len
)
703 str
= tok_str_realloc(s
);
704 cstr
= (CString
*)(str
+ len
);
706 cstr
->size
= cv
->cstr
->size
;
707 cstr
->data_allocated
= NULL
;
708 cstr
->size_allocated
= cstr
->size
;
709 memcpy((char *)cstr
+ sizeof(CString
),
710 cv
->cstr
->data
, cstr
->size
);
717 #if LDOUBLE_SIZE == 8
720 str
[len
++] = cv
->tab
[0];
721 str
[len
++] = cv
->tab
[1];
723 #if LDOUBLE_SIZE == 12
725 str
[len
++] = cv
->tab
[0];
726 str
[len
++] = cv
->tab
[1];
727 str
[len
++] = cv
->tab
[2];
728 #elif LDOUBLE_SIZE == 16
730 str
[len
++] = cv
->tab
[0];
731 str
[len
++] = cv
->tab
[1];
732 str
[len
++] = cv
->tab
[2];
733 str
[len
++] = cv
->tab
[3];
734 #elif LDOUBLE_SIZE != 8
735 #error add long double size support
744 /* token string handling */
746 /* add the current parse token in token string 's' */
747 void tok_str_add_tok(TokenString
*s
)
751 /* save line number info */
752 if (file
->line_num
!= s
->last_line_num
) {
753 s
->last_line_num
= file
->line_num
;
754 cval
.i
= s
->last_line_num
;
755 tok_str_add2(s
, TOK_LINENUM
, &cval
);
757 tok_str_add2(s
, tok
, &tokc
);
760 #if LDOUBLE_SIZE == 16
761 #define LDOUBLE_GET(p, cv) \
766 #elif LDOUBLE_SIZE == 12
767 #define LDOUBLE_GET(p, cv) \
771 #elif LDOUBLE_SIZE == 8
772 #define LDOUBLE_GET(p, cv) \
776 #error add long double size support
780 /* get a token from an integer array and increment pointer
781 accordingly. we code it as a macro to avoid pointer aliasing. */
782 #define TOK_GET(t, p, cv) \
797 cv.cstr = (CString *)p; \
798 cv.cstr->data = (char *)p + sizeof(CString);\
799 p += (sizeof(CString) + cv.cstr->size + 3) >> 2;\
809 LDOUBLE_GET(p, cv); \
810 p += LDOUBLE_SIZE / 4; \
817 /* undefined a define symbol. Its name is just set to zero */
818 void define_undef(Sym
*s
)
822 if (v
>= TOK_IDENT
&& v
< tok_ident
)
823 table_ident
[v
- TOK_IDENT
]->sym_define
= NULL
;
827 /* free define stack until top reaches 'b' */
828 void free_defines(Sym
*b
)
836 /* do not free args or predefined defines */
838 tok_str_free(top
->d
);
840 if (v
>= TOK_IDENT
&& v
< tok_ident
)
841 table_ident
[v
- TOK_IDENT
]->sym_define
= NULL
;
849 Sym
*label_find(int v
)
852 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
854 return table_ident
[v
]->sym_label
;
857 Sym
*label_push(Sym
**ptop
, int v
, int flags
)
860 s
= sym_push2(ptop
, v
, 0, 0);
862 ps
= &table_ident
[v
- TOK_IDENT
]->sym_label
;
863 if (ptop
== &global_label_stack
) {
864 /* modify the top most local identifier, so that
865 sym_identifier will point to 's' when popped */
867 ps
= &(*ps
)->prev_tok
;
874 /* pop labels until element last is reached. Look if any labels are
875 undefined. Define symbols if '&&label' was used. */
876 void label_pop(Sym
**ptop
, Sym
*slast
)
879 for(s
= *ptop
; s
!= slast
; s
= s1
) {
881 if (s
->r
== LABEL_DECLARED
) {
882 warning("label '%s' declared but not used", get_tok_str(s
->v
, NULL
));
883 } else if (s
->r
== LABEL_FORWARD
) {
884 error("label '%s' used but not defined",
885 get_tok_str(s
->v
, NULL
));
888 /* define corresponding symbol. A size of
890 put_extern_sym(s
, cur_text_section
, s
->jnext
, 1);
894 table_ident
[s
->v
- TOK_IDENT
]->sym_label
= s
->prev_tok
;
900 /* eval an expression for #if/#elif */
901 static int expr_preprocess(void)
907 while (tok
!= TOK_LINEFEED
&& tok
!= TOK_EOF
) {
908 next(); /* do macro subst */
909 if (tok
== TOK_DEFINED
) {
914 c
= define_find(tok
) != 0;
919 } else if (tok
>= TOK_IDENT
) {
920 /* if undefined macro */
924 tok_str_add_tok(&str
);
926 tok_str_add(&str
, -1); /* simulate end of file */
927 tok_str_add(&str
, 0);
928 /* now evaluate C constant expression */
933 tok_str_free(str
.str
);
937 #if defined(PARSE_DEBUG) || defined(PP_DEBUG)
938 static void tok_print(int *str
)
945 TOK_GET(t
, str
, cval
);
948 printf("%s", get_tok_str(t
, &cval
));
954 /* parse after #define */
955 void parse_define(void)
957 Sym
*s
, *first
, **ps
;
958 int v
, t
, varg
, is_vaargs
, spc
;
963 error("invalid macro name '%s'", get_tok_str(tok
, &tokc
));
964 /* XXX: should check if same macro (ANSI) */
967 /* '(' must be just after macro definition for MACRO_FUNC */
976 if (varg
== TOK_DOTS
) {
977 varg
= TOK___VA_ARGS__
;
979 } else if (tok
== TOK_DOTS
&& gnu_ext
) {
983 if (varg
< TOK_IDENT
)
984 error("badly punctuated parameter list");
985 s
= sym_push2(&define_stack
, varg
| SYM_FIELD
, is_vaargs
, 0);
998 /* EOF testing necessary for '-D' handling */
999 while (tok
!= TOK_LINEFEED
&& tok
!= TOK_EOF
) {
1000 /* remove spaces around ## and after '#' */
1001 if (TOK_TWOSHARPS
== tok
) {
1005 } else if ('#' == tok
) {
1007 } else if (check_space(tok
, &spc
)) {
1010 tok_str_add2(&str
, tok
, &tokc
);
1015 --str
.len
; /* remove trailing space */
1016 tok_str_add(&str
, 0);
1018 printf("define %s %d: ", get_tok_str(v
, NULL
), t
);
1021 define_push(v
, t
, str
.str
, first
);
1024 static inline int hash_cached_include(int type
, const char *filename
)
1026 const unsigned char *s
;
1030 h
= TOK_HASH_FUNC(h
, type
);
1033 h
= TOK_HASH_FUNC(h
, *s
);
1036 h
&= (CACHED_INCLUDES_HASH_SIZE
- 1);
1040 /* XXX: use a token or a hash table to accelerate matching ? */
1041 static CachedInclude
*search_cached_include(TCCState
*s1
,
1042 int type
, const char *filename
)
1046 h
= hash_cached_include(type
, filename
);
1047 i
= s1
->cached_includes_hash
[h
];
1051 e
= s1
->cached_includes
[i
- 1];
1052 if (e
->type
== type
&& !PATHCMP(e
->filename
, filename
))
1059 static inline void add_cached_include(TCCState
*s1
, int type
,
1060 const char *filename
, int ifndef_macro
)
1065 if (search_cached_include(s1
, type
, filename
))
1068 printf("adding cached '%s' %s\n", filename
, get_tok_str(ifndef_macro
, NULL
));
1070 e
= tcc_malloc(sizeof(CachedInclude
) + strlen(filename
));
1074 strcpy(e
->filename
, filename
);
1075 e
->ifndef_macro
= ifndef_macro
;
1076 dynarray_add((void ***)&s1
->cached_includes
, &s1
->nb_cached_includes
, e
);
1077 /* add in hash table */
1078 h
= hash_cached_include(type
, filename
);
1079 e
->hash_next
= s1
->cached_includes_hash
[h
];
1080 s1
->cached_includes_hash
[h
] = s1
->nb_cached_includes
;
1083 static void pragma_parse(TCCState
*s1
)
1088 if (tok
== TOK_pack
) {
1091 #pragma pack(1) // set
1092 #pragma pack() // reset to default
1093 #pragma pack(push,1) // push & set
1094 #pragma pack(pop) // restore previous
1098 if (tok
== TOK_ASM_pop
) {
1100 if (s1
->pack_stack_ptr
<= s1
->pack_stack
) {
1102 error("out of pack stack");
1104 s1
->pack_stack_ptr
--;
1108 if (tok
== TOK_ASM_push
) {
1110 if (s1
->pack_stack_ptr
>= s1
->pack_stack
+ PACK_STACK_SIZE
- 1)
1112 s1
->pack_stack_ptr
++;
1115 if (tok
!= TOK_CINT
) {
1117 error("invalid pack pragma");
1120 if (val
< 1 || val
> 16 || (val
& (val
- 1)) != 0)
1124 *s1
->pack_stack_ptr
= val
;
1130 /* is_bof is true if first non space token at beginning of file */
1131 static void preprocess(int is_bof
)
1133 TCCState
*s1
= tcc_state
;
1134 int i
, c
, n
, saved_parse_flags
;
1138 saved_parse_flags
= parse_flags
;
1139 parse_flags
= PARSE_FLAG_PREPROCESS
| PARSE_FLAG_TOK_NUM
|
1140 PARSE_FLAG_LINEFEED
;
1150 s
= define_find(tok
);
1151 /* undefine symbol by putting an invalid name */
1156 case TOK_INCLUDE_NEXT
:
1157 ch
= file
->buf_ptr
[0];
1158 /* XXX: incorrect if comments : use next_nomacro with a special mode */
1163 } else if (ch
== '\"') {
1168 while (ch
!= c
&& ch
!= '\n' && ch
!= CH_EOF
) {
1169 if ((q
- buf
) < sizeof(buf
) - 1)
1172 if (handle_stray_noerror() == 0)
1180 /* eat all spaces and comments after include */
1181 /* XXX: slightly incorrect */
1182 while (ch1
!= '\n' && ch1
!= CH_EOF
)
1186 /* computed #include : either we have only strings or
1187 we have anything enclosed in '<>' */
1190 if (tok
== TOK_STR
) {
1191 while (tok
!= TOK_LINEFEED
) {
1192 if (tok
!= TOK_STR
) {
1194 error("'#include' expects \"FILENAME\" or <FILENAME>");
1196 pstrcat(buf
, sizeof(buf
), (char *)tokc
.cstr
->data
);
1202 while (tok
!= TOK_LINEFEED
) {
1203 pstrcat(buf
, sizeof(buf
), get_tok_str(tok
, &tokc
));
1207 /* check syntax and remove '<>' */
1208 if (len
< 2 || buf
[0] != '<' || buf
[len
- 1] != '>')
1209 goto include_syntax
;
1210 memmove(buf
, buf
+ 1, len
- 2);
1211 buf
[len
- 2] = '\0';
1216 if (s1
->include_stack_ptr
>= s1
->include_stack
+ INCLUDE_STACK_SIZE
)
1217 error("#include recursion too deep");
1219 n
= s1
->nb_include_paths
+ s1
->nb_sysinclude_paths
;
1220 for (i
= -2; i
< n
; ++i
) {
1221 char buf1
[sizeof file
->filename
];
1228 /* check absolute include path */
1229 if (!IS_ABSPATH(buf
))
1233 } else if (i
== -1) {
1234 /* search in current dir if "header.h" */
1237 size
= tcc_basename(file
->filename
) - file
->filename
;
1238 memcpy(buf1
, file
->filename
, size
);
1242 /* search in all the include paths */
1243 if (i
< s1
->nb_include_paths
)
1244 path
= s1
->include_paths
[i
];
1246 path
= s1
->sysinclude_paths
[i
- s1
->nb_include_paths
];
1247 pstrcpy(buf1
, sizeof(buf1
), path
);
1248 pstrcat(buf1
, sizeof(buf1
), "/");
1251 pstrcat(buf1
, sizeof(buf1
), buf
);
1253 e
= search_cached_include(s1
, c
, buf1
);
1254 if (e
&& define_find(e
->ifndef_macro
)) {
1255 /* no need to parse the include because the 'ifndef macro'
1258 printf("%s: skipping %s\n", file
->filename
, buf
);
1262 f
= tcc_open(s1
, buf1
);
1267 if (tok
== TOK_INCLUDE_NEXT
) {
1278 printf("%s: including %s\n", file
->filename
, buf1
);
1281 /* XXX: fix current line init */
1282 /* push current file in stack */
1283 *s1
->include_stack_ptr
++ = file
;
1285 pstrcpy(f
->inc_filename
, sizeof(f
->inc_filename
), buf1
);
1287 /* add include file debug info */
1288 if (tcc_state
->do_debug
) {
1289 put_stabs(file
->filename
, N_BINCL
, 0, 0, 0);
1291 tok_flags
|= TOK_FLAG_BOF
| TOK_FLAG_BOL
;
1292 ch
= file
->buf_ptr
[0];
1295 error("include file '%s' not found", buf
);
1302 c
= expr_preprocess();
1308 if (tok
< TOK_IDENT
)
1309 error("invalid argument for '#if%sdef'", c
? "n" : "");
1313 printf("#ifndef %s\n", get_tok_str(tok
, NULL
));
1315 file
->ifndef_macro
= tok
;
1318 c
= (define_find(tok
) != 0) ^ c
;
1320 if (s1
->ifdef_stack_ptr
>= s1
->ifdef_stack
+ IFDEF_STACK_SIZE
)
1321 error("memory full");
1322 *s1
->ifdef_stack_ptr
++ = c
;
1325 if (s1
->ifdef_stack_ptr
== s1
->ifdef_stack
)
1326 error("#else without matching #if");
1327 if (s1
->ifdef_stack_ptr
[-1] & 2)
1328 error("#else after #else");
1329 c
= (s1
->ifdef_stack_ptr
[-1] ^= 3);
1332 if (s1
->ifdef_stack_ptr
== s1
->ifdef_stack
)
1333 error("#elif without matching #if");
1334 c
= s1
->ifdef_stack_ptr
[-1];
1336 error("#elif after #else");
1337 /* last #if/#elif expression was true: we skip */
1340 c
= expr_preprocess();
1341 s1
->ifdef_stack_ptr
[-1] = c
;
1343 if (s1
->ifdef_stack_ptr
== file
->ifdef_stack_ptr
+ 1)
1344 file
->ifndef_macro
= 0;
1354 if (s1
->ifdef_stack_ptr
<= file
->ifdef_stack_ptr
)
1355 error("#endif without matching #if");
1356 s1
->ifdef_stack_ptr
--;
1357 /* '#ifndef macro' was at the start of file. Now we check if
1358 an '#endif' is exactly at the end of file */
1359 if (file
->ifndef_macro
&&
1360 s1
->ifdef_stack_ptr
== file
->ifdef_stack_ptr
) {
1361 file
->ifndef_macro_saved
= file
->ifndef_macro
;
1362 /* need to set to zero to avoid false matches if another
1363 #ifndef at middle of file */
1364 file
->ifndef_macro
= 0;
1365 while (tok
!= TOK_LINEFEED
)
1367 tok_flags
|= TOK_FLAG_ENDIF
;
1373 if (tok
!= TOK_CINT
)
1375 file
->line_num
= tokc
.i
- 1; /* the line number will be incremented after */
1377 if (tok
!= TOK_LINEFEED
) {
1380 pstrcpy(file
->filename
, sizeof(file
->filename
),
1381 (char *)tokc
.cstr
->data
);
1387 ch
= file
->buf_ptr
[0];
1390 while (ch
!= '\n' && ch
!= CH_EOF
) {
1391 if ((q
- buf
) < sizeof(buf
) - 1)
1394 if (handle_stray_noerror() == 0)
1401 error("#error %s", buf
);
1403 warning("#warning %s", buf
);
1409 if (tok
== TOK_LINEFEED
|| tok
== '!' || tok
== TOK_CINT
) {
1410 /* '!' is ignored to allow C scripts. numbers are ignored
1411 to emulate cpp behaviour */
1413 if (!(saved_parse_flags
& PARSE_FLAG_ASM_COMMENTS
))
1414 warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok
, &tokc
));
1418 /* ignore other preprocess commands or #! for C scripts */
1419 while (tok
!= TOK_LINEFEED
)
1422 parse_flags
= saved_parse_flags
;
1425 /* evaluate escape codes in a string. */
1426 static void parse_escape_string(CString
*outstr
, const uint8_t *buf
, int is_long
)
1441 case '0': case '1': case '2': case '3':
1442 case '4': case '5': case '6': case '7':
1443 /* at most three octal digits */
1448 n
= n
* 8 + c
- '0';
1452 n
= n
* 8 + c
- '0';
1457 goto add_char_nonext
;
1465 if (c
>= 'a' && c
<= 'f')
1467 else if (c
>= 'A' && c
<= 'F')
1477 goto add_char_nonext
;
1501 goto invalid_escape
;
1511 if (c
>= '!' && c
<= '~')
1512 warning("unknown escape sequence: \'\\%c\'", c
);
1514 warning("unknown escape sequence: \'\\x%x\'", c
);
1521 cstr_ccat(outstr
, c
);
1523 cstr_wccat(outstr
, c
);
1525 /* add a trailing '\0' */
1527 cstr_ccat(outstr
, '\0');
1529 cstr_wccat(outstr
, '\0');
1532 /* we use 64 bit numbers */
1535 /* bn = (bn << shift) | or_val */
1536 void bn_lshift(unsigned int *bn
, int shift
, int or_val
)
1540 for(i
=0;i
<BN_SIZE
;i
++) {
1542 bn
[i
] = (v
<< shift
) | or_val
;
1543 or_val
= v
>> (32 - shift
);
1547 void bn_zero(unsigned int *bn
)
1550 for(i
=0;i
<BN_SIZE
;i
++) {
1555 /* parse number in null terminated string 'p' and return it in the
1557 void parse_number(const char *p
)
1559 int b
, t
, shift
, frac_bits
, s
, exp_val
, ch
;
1561 unsigned int bn
[BN_SIZE
];
1572 goto float_frac_parse
;
1573 } else if (t
== '0') {
1574 if (ch
== 'x' || ch
== 'X') {
1578 } else if (tcc_ext
&& (ch
== 'b' || ch
== 'B')) {
1584 /* parse all digits. cannot check octal numbers at this stage
1585 because of floating point constants */
1587 if (ch
>= 'a' && ch
<= 'f')
1589 else if (ch
>= 'A' && ch
<= 'F')
1597 if (q
>= token_buf
+ STRING_MAX_SIZE
) {
1599 error("number too long");
1605 ((ch
== 'e' || ch
== 'E') && b
== 10) ||
1606 ((ch
== 'p' || ch
== 'P') && (b
== 16 || b
== 2))) {
1608 /* NOTE: strtox should support that for hexa numbers, but
1609 non ISOC99 libcs do not support it, so we prefer to do
1611 /* hexadecimal or binary floats */
1612 /* XXX: handle overflows */
1624 } else if (t
>= 'a') {
1626 } else if (t
>= 'A') {
1631 bn_lshift(bn
, shift
, t
);
1638 if (t
>= 'a' && t
<= 'f') {
1640 } else if (t
>= 'A' && t
<= 'F') {
1642 } else if (t
>= '0' && t
<= '9') {
1648 error("invalid digit");
1649 bn_lshift(bn
, shift
, t
);
1654 if (ch
!= 'p' && ch
!= 'P')
1661 } else if (ch
== '-') {
1665 if (ch
< '0' || ch
> '9')
1666 expect("exponent digits");
1667 while (ch
>= '0' && ch
<= '9') {
1668 exp_val
= exp_val
* 10 + ch
- '0';
1671 exp_val
= exp_val
* s
;
1673 /* now we can generate the number */
1674 /* XXX: should patch directly float number */
1675 d
= (double)bn
[1] * 4294967296.0 + (double)bn
[0];
1676 d
= ldexp(d
, exp_val
- frac_bits
);
1681 /* float : should handle overflow */
1683 } else if (t
== 'L') {
1685 #ifdef TCC_TARGET_PE
1690 /* XXX: not large enough */
1691 tokc
.ld
= (long double)d
;
1698 /* decimal floats */
1700 if (q
>= token_buf
+ STRING_MAX_SIZE
)
1705 while (ch
>= '0' && ch
<= '9') {
1706 if (q
>= token_buf
+ STRING_MAX_SIZE
)
1712 if (ch
== 'e' || ch
== 'E') {
1713 if (q
>= token_buf
+ STRING_MAX_SIZE
)
1717 if (ch
== '-' || ch
== '+') {
1718 if (q
>= token_buf
+ STRING_MAX_SIZE
)
1723 if (ch
< '0' || ch
> '9')
1724 expect("exponent digits");
1725 while (ch
>= '0' && ch
<= '9') {
1726 if (q
>= token_buf
+ STRING_MAX_SIZE
)
1738 tokc
.f
= strtof(token_buf
, NULL
);
1739 } else if (t
== 'L') {
1741 #ifdef TCC_TARGET_PE
1743 tokc
.d
= strtod(token_buf
, NULL
);
1746 tokc
.ld
= strtold(token_buf
, NULL
);
1750 tokc
.d
= strtod(token_buf
, NULL
);
1754 unsigned long long n
, n1
;
1757 /* integer number */
1760 if (b
== 10 && *q
== '0') {
1767 /* no need for checks except for base 10 / 8 errors */
1770 } else if (t
>= 'a') {
1772 } else if (t
>= 'A') {
1777 error("invalid digit");
1781 /* detect overflow */
1782 /* XXX: this test is not reliable */
1784 error("integer constant overflow");
1787 /* XXX: not exactly ANSI compliant */
1788 if ((n
& 0xffffffff00000000LL
) != 0) {
1793 } else if (n
> 0x7fffffff) {
1804 error("three 'l's in integer constant");
1807 if (tok
== TOK_CINT
)
1809 else if (tok
== TOK_CUINT
)
1813 } else if (t
== 'U') {
1815 error("two 'u's in integer constant");
1817 if (tok
== TOK_CINT
)
1819 else if (tok
== TOK_CLLONG
)
1826 if (tok
== TOK_CINT
|| tok
== TOK_CUINT
)
1832 error("invalid number\n");
1836 #define PARSE2(c1, tok1, c2, tok2) \
1847 /* return next token without macro substitution */
1848 static inline void next_nomacro1(void)
1863 goto keep_tok_flags
;
1870 /* first look if it is in fact an end of buffer */
1871 if (p
>= file
->buf_end
) {
1875 if (p
>= file
->buf_end
)
1888 TCCState
*s1
= tcc_state
;
1889 if ((parse_flags
& PARSE_FLAG_LINEFEED
)
1890 && !(tok_flags
& TOK_FLAG_EOF
)) {
1891 tok_flags
|= TOK_FLAG_EOF
;
1893 goto keep_tok_flags
;
1894 } else if (s1
->include_stack_ptr
== s1
->include_stack
||
1895 !(parse_flags
& PARSE_FLAG_PREPROCESS
)) {
1896 /* no include left : end of file. */
1899 tok_flags
&= ~TOK_FLAG_EOF
;
1900 /* pop include file */
1902 /* test if previous '#endif' was after a #ifdef at
1904 if (tok_flags
& TOK_FLAG_ENDIF
) {
1906 printf("#endif %s\n", get_tok_str(file
->ifndef_macro_saved
, NULL
));
1908 add_cached_include(s1
, file
->inc_type
, file
->inc_filename
,
1909 file
->ifndef_macro_saved
);
1912 /* add end of include file debug info */
1913 if (tcc_state
->do_debug
) {
1914 put_stabd(N_EINCL
, 0, 0);
1916 /* pop include stack */
1918 s1
->include_stack_ptr
--;
1919 file
= *s1
->include_stack_ptr
;
1928 tok_flags
|= TOK_FLAG_BOL
;
1931 if (0 == (parse_flags
& PARSE_FLAG_LINEFEED
))
1934 goto keep_tok_flags
;
1939 if ((tok_flags
& TOK_FLAG_BOL
) &&
1940 (parse_flags
& PARSE_FLAG_PREPROCESS
)) {
1942 preprocess(tok_flags
& TOK_FLAG_BOF
);
1948 tok
= TOK_TWOSHARPS
;
1950 if (parse_flags
& PARSE_FLAG_ASM_COMMENTS
) {
1951 p
= parse_line_comment(p
- 1);
1960 case 'a': case 'b': case 'c': case 'd':
1961 case 'e': case 'f': case 'g': case 'h':
1962 case 'i': case 'j': case 'k': case 'l':
1963 case 'm': case 'n': case 'o': case 'p':
1964 case 'q': case 'r': case 's': case 't':
1965 case 'u': case 'v': case 'w': case 'x':
1967 case 'A': case 'B': case 'C': case 'D':
1968 case 'E': case 'F': case 'G': case 'H':
1969 case 'I': case 'J': case 'K':
1970 case 'M': case 'N': case 'O': case 'P':
1971 case 'Q': case 'R': case 'S': case 'T':
1972 case 'U': case 'V': case 'W': case 'X':
1978 h
= TOK_HASH_FUNC(h
, c
);
1982 if (!isidnum_table
[c
-CH_EOF
])
1984 h
= TOK_HASH_FUNC(h
, c
);
1991 /* fast case : no stray found, so we have the full token
1992 and we have already hashed it */
1994 h
&= (TOK_HASH_SIZE
- 1);
1995 pts
= &hash_ident
[h
];
2000 if (ts
->len
== len
&& !memcmp(ts
->str
, p1
, len
))
2002 pts
= &(ts
->hash_next
);
2004 ts
= tok_alloc_new(pts
, p1
, len
);
2008 cstr_reset(&tokcstr
);
2011 cstr_ccat(&tokcstr
, *p1
);
2017 while (isidnum_table
[c
-CH_EOF
]) {
2018 cstr_ccat(&tokcstr
, c
);
2021 ts
= tok_alloc(tokcstr
.data
, tokcstr
.size
);
2027 if (t
!= '\\' && t
!= '\'' && t
!= '\"') {
2029 goto parse_ident_fast
;
2032 if (c
== '\'' || c
== '\"') {
2036 cstr_reset(&tokcstr
);
2037 cstr_ccat(&tokcstr
, 'L');
2038 goto parse_ident_slow
;
2042 case '0': case '1': case '2': case '3':
2043 case '4': case '5': case '6': case '7':
2046 cstr_reset(&tokcstr
);
2047 /* after the first digit, accept digits, alpha, '.' or sign if
2048 prefixed by 'eEpP' */
2052 cstr_ccat(&tokcstr
, c
);
2054 if (!(isnum(c
) || isid(c
) || c
== '.' ||
2055 ((c
== '+' || c
== '-') &&
2056 (t
== 'e' || t
== 'E' || t
== 'p' || t
== 'P'))))
2059 /* We add a trailing '\0' to ease parsing */
2060 cstr_ccat(&tokcstr
, '\0');
2061 tokc
.cstr
= &tokcstr
;
2065 /* special dot handling because it can also start a number */
2068 cstr_reset(&tokcstr
);
2069 cstr_ccat(&tokcstr
, '.');
2071 } else if (c
== '.') {
2091 /* parse the string */
2093 p
= parse_pp_string(p
, sep
, &str
);
2094 cstr_ccat(&str
, '\0');
2096 /* eval the escape (should be done as TOK_PPNUM) */
2097 cstr_reset(&tokcstr
);
2098 parse_escape_string(&tokcstr
, str
.data
, is_long
);
2103 /* XXX: make it portable */
2107 char_size
= sizeof(nwchar_t
);
2108 if (tokcstr
.size
<= char_size
)
2109 error("empty character constant");
2110 if (tokcstr
.size
> 2 * char_size
)
2111 warning("multi-character character constant");
2113 tokc
.i
= *(int8_t *)tokcstr
.data
;
2116 tokc
.i
= *(nwchar_t
*)tokcstr
.data
;
2120 tokc
.cstr
= &tokcstr
;
2134 } else if (c
== '<') {
2152 } else if (c
== '>') {
2170 } else if (c
== '=') {
2183 } else if (c
== '=') {
2196 } else if (c
== '=') {
2209 } else if (c
== '=') {
2212 } else if (c
== '>') {
2220 PARSE2('!', '!', '=', TOK_NE
)
2221 PARSE2('=', '=', '=', TOK_EQ
)
2222 PARSE2('*', '*', '=', TOK_A_MUL
)
2223 PARSE2('%', '%', '=', TOK_A_MOD
)
2224 PARSE2('^', '^', '=', TOK_A_XOR
)
2226 /* comments or operator */
2230 p
= parse_comment(p
);
2232 } else if (c
== '/') {
2233 p
= parse_line_comment(p
);
2235 } else if (c
== '=') {
2255 case '$': /* only used in assembler */
2256 case '@': /* dito */
2261 error("unrecognized character \\x%02x", c
);
2267 #if defined(PARSE_DEBUG)
2268 printf("token = %s\n", get_tok_str(tok
, &tokc
));
2272 /* return next token without macro substitution. Can read input from
2274 static void next_nomacro_spc(void)
2280 TOK_GET(tok
, macro_ptr
, tokc
);
2281 if (tok
== TOK_LINENUM
) {
2282 file
->line_num
= tokc
.i
;
2291 void next_nomacro(void)
2295 } while (is_space(tok
));
2298 /* substitute args in macro_str and return allocated string */
2299 static int *macro_arg_subst(Sym
**nested_list
, int *macro_str
, Sym
*args
)
2301 int *st
, last_tok
, t
, spc
;
2310 TOK_GET(t
, macro_str
, cval
);
2315 TOK_GET(t
, macro_str
, cval
);
2318 s
= sym_find2(args
, t
);
2324 TOK_GET(t
, st
, cval
);
2325 if (!check_space(t
, &spc
))
2326 cstr_cat(&cstr
, get_tok_str(t
, &cval
));
2329 cstr_ccat(&cstr
, '\0');
2331 printf("stringize: %s\n", (char *)cstr
.data
);
2335 tok_str_add2(&str
, TOK_STR
, &cval
);
2338 tok_str_add2(&str
, t
, &cval
);
2340 } else if (t
>= TOK_IDENT
) {
2341 s
= sym_find2(args
, t
);
2344 /* if '##' is present before or after, no arg substitution */
2345 if (*macro_str
== TOK_TWOSHARPS
|| last_tok
== TOK_TWOSHARPS
) {
2346 /* special case for var arg macros : ## eats the
2347 ',' if empty VA_ARGS variable. */
2348 /* XXX: test of the ',' is not 100%
2349 reliable. should fix it to avoid security
2351 if (gnu_ext
&& s
->type
.t
&&
2352 last_tok
== TOK_TWOSHARPS
&&
2353 str
.len
>= 2 && str
.str
[str
.len
- 2] == ',') {
2355 /* suppress ',' '##' */
2358 /* suppress '##' and add variable */
2366 TOK_GET(t1
, st
, cval
);
2369 tok_str_add2(&str
, t1
, &cval
);
2373 /* NOTE: the stream cannot be read when macro
2374 substituing an argument */
2375 macro_subst(&str
, nested_list
, st
, NULL
);
2378 tok_str_add(&str
, t
);
2381 tok_str_add2(&str
, t
, &cval
);
2385 tok_str_add(&str
, 0);
2389 static char const ab_month_name
[12][4] =
2391 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2392 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
2395 /* do macro substitution of current token with macro 's' and add
2396 result to (tok_str,tok_len). 'nested_list' is the list of all
2397 macros we got inside to avoid recursing. Return non zero if no
2398 substitution needs to be done */
2399 static int macro_subst_tok(TokenString
*tok_str
,
2400 Sym
**nested_list
, Sym
*s
, struct macro_level
**can_read_stream
)
2402 Sym
*args
, *sa
, *sa1
;
2403 int mstr_allocated
, parlevel
, *mstr
, t
, t1
, *p
, spc
;
2410 /* if symbol is a macro, prepare substitution */
2411 /* special macros */
2412 if (tok
== TOK___LINE__
) {
2413 snprintf(buf
, sizeof(buf
), "%d", file
->line_num
);
2417 } else if (tok
== TOK___FILE__
) {
2418 cstrval
= file
->filename
;
2420 } else if (tok
== TOK___DATE__
|| tok
== TOK___TIME__
) {
2425 tm
= localtime(&ti
);
2426 if (tok
== TOK___DATE__
) {
2427 snprintf(buf
, sizeof(buf
), "%s %2d %d",
2428 ab_month_name
[tm
->tm_mon
], tm
->tm_mday
, tm
->tm_year
+ 1900);
2430 snprintf(buf
, sizeof(buf
), "%02d:%02d:%02d",
2431 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
2438 cstr_cat(&cstr
, cstrval
);
2439 cstr_ccat(&cstr
, '\0');
2441 tok_str_add2(tok_str
, t1
, &cval
);
2446 if (s
->type
.t
== MACRO_FUNC
) {
2447 /* NOTE: we do not use next_nomacro to avoid eating the
2448 next token. XXX: find better solution */
2452 while (is_space(t
= *p
) || TOK_LINEFEED
== t
)
2454 if (t
== 0 && can_read_stream
) {
2455 /* end of macro stream: we must look at the token
2456 after in the file */
2457 struct macro_level
*ml
= *can_read_stream
;
2463 *can_read_stream
= ml
-> prev
;
2468 /* XXX: incorrect with comments */
2469 ch
= file
->buf_ptr
[0];
2470 while (is_space(ch
) || ch
== '\n')
2474 if (t
!= '(') /* no macro subst */
2477 /* argument macro */
2482 /* NOTE: empty args are allowed, except if no args */
2484 /* handle '()' case */
2485 if (!args
&& !sa
&& tok
== ')')
2488 error("macro '%s' used with too many args",
2489 get_tok_str(s
->v
, 0));
2492 /* NOTE: non zero sa->t indicates VA_ARGS */
2493 while ((parlevel
> 0 ||
2495 (tok
!= ',' || sa
->type
.t
))) &&
2499 else if (tok
== ')')
2501 if (tok
== TOK_LINEFEED
)
2503 if (!check_space(tok
, &spc
))
2504 tok_str_add2(&str
, tok
, &tokc
);
2508 tok_str_add(&str
, 0);
2509 sa1
= sym_push2(&args
, sa
->v
& ~SYM_FIELD
, sa
->type
.t
, 0);
2513 /* special case for gcc var args: add an empty
2514 var arg argument if it is omitted */
2515 if (sa
&& sa
->type
.t
&& gnu_ext
)
2525 error("macro '%s' used with too few args",
2526 get_tok_str(s
->v
, 0));
2529 /* now subst each arg */
2530 mstr
= macro_arg_subst(nested_list
, mstr
, args
);
2535 tok_str_free(sa
->d
);
2541 sym_push2(nested_list
, s
->v
, 0, 0);
2542 macro_subst(tok_str
, nested_list
, mstr
, can_read_stream
);
2543 /* pop nested defined symbol */
2545 *nested_list
= sa1
->prev
;
2553 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
2554 return the resulting string (which must be freed). */
2555 static inline int *macro_twosharps(const int *macro_str
)
2560 TokenString macro_str1
;
2565 /* we search the first '##' */
2566 for(ptr
= macro_str
;;) {
2567 TOK_GET(t
, ptr
, cval
);
2568 if (t
== TOK_TWOSHARPS
)
2570 /* nothing more to do if end of string */
2575 /* we saw '##', so we need more processing to handle it */
2576 tok_str_new(¯o_str1
);
2577 for(ptr
= macro_str
;;) {
2578 TOK_GET(tok
, ptr
, tokc
);
2581 if (tok
== TOK_TWOSHARPS
)
2583 while (*ptr
== TOK_TWOSHARPS
) {
2585 if (t
&& t
!= TOK_TWOSHARPS
) {
2586 TOK_GET(t
, ptr
, cval
);
2588 /* We concatenate the two tokens */
2590 cstr_cat(&cstr
, get_tok_str(tok
, &tokc
));
2592 cstr_cat(&cstr
, get_tok_str(t
, &cval
));
2593 cstr_ccat(&cstr
, '\0');
2596 file
->buf_ptr
= cstr
.data
;
2599 if (0 == *file
->buf_ptr
)
2601 tok_str_add2(¯o_str1
, tok
, &tokc
);
2603 warning("pasting \"%.*s\" and \"%s\" does not give a valid preprocessing token",
2604 n
, cstr
.data
, (char*)cstr
.data
+ n
);
2610 tok_str_add2(¯o_str1
, tok
, &tokc
);
2612 tok_str_add(¯o_str1
, 0);
2613 return macro_str1
.str
;
2617 /* do macro substitution of macro_str and add result to
2618 (tok_str,tok_len). 'nested_list' is the list of all macros we got
2619 inside to avoid recursing. */
2620 static void macro_subst(TokenString
*tok_str
, Sym
**nested_list
,
2621 const int *macro_str
, struct macro_level
** can_read_stream
)
2628 struct macro_level ml
;
2630 /* first scan for '##' operator handling */
2632 macro_str1
= macro_twosharps(ptr
);
2637 /* NOTE: ptr == NULL can only happen if tokens are read from
2638 file stream due to a macro function call */
2641 TOK_GET(t
, ptr
, cval
);
2646 /* if nested substitution, do nothing */
2647 if (sym_find2(*nested_list
, t
))
2650 if (can_read_stream
)
2651 ml
.prev
= *can_read_stream
, *can_read_stream
= &ml
;
2652 macro_ptr
= (int *)ptr
;
2654 ret
= macro_subst_tok(tok_str
, nested_list
, s
, can_read_stream
);
2655 ptr
= (int *)macro_ptr
;
2657 if (can_read_stream
&& *can_read_stream
== &ml
)
2658 *can_read_stream
= ml
.prev
;
2663 if (!check_space(t
, &spc
))
2664 tok_str_add2(tok_str
, t
, &cval
);
2668 tok_str_free(macro_str1
);
2671 /* return next token with macro substitution */
2674 Sym
*nested_list
, *s
;
2676 struct macro_level
*ml
;
2679 if (parse_flags
& PARSE_FLAG_SPACES
)
2684 /* if not reading from macro substituted string, then try
2685 to substitute macros */
2686 if (tok
>= TOK_IDENT
&&
2687 (parse_flags
& PARSE_FLAG_PREPROCESS
)) {
2688 s
= define_find(tok
);
2690 /* we have a macro: we try to substitute */
2694 if (macro_subst_tok(&str
, &nested_list
, s
, &ml
) == 0) {
2695 /* substitution done, NOTE: maybe empty */
2696 tok_str_add(&str
, 0);
2697 macro_ptr
= str
.str
;
2698 macro_ptr_allocated
= str
.str
;
2705 /* end of macro or end of unget buffer */
2706 if (unget_buffer_enabled
) {
2707 macro_ptr
= unget_saved_macro_ptr
;
2708 unget_buffer_enabled
= 0;
2710 /* end of macro string: free it */
2711 tok_str_free(macro_ptr_allocated
);
2712 macro_ptr_allocated
= NULL
;
2719 /* convert preprocessor tokens into C tokens */
2720 if (tok
== TOK_PPNUM
&&
2721 (parse_flags
& PARSE_FLAG_TOK_NUM
)) {
2722 parse_number((char *)tokc
.cstr
->data
);
2726 /* better than nothing, but needs extension to handle '-E' option
2728 void preprocess_init(TCCState
*s1
)
2730 s1
->include_stack_ptr
= s1
->include_stack
;
2731 /* XXX: move that before to avoid having to initialize
2732 file->ifdef_stack_ptr ? */
2733 s1
->ifdef_stack_ptr
= s1
->ifdef_stack
;
2734 file
->ifdef_stack_ptr
= s1
->ifdef_stack_ptr
;
2736 /* XXX: not ANSI compliant: bound checking says error */
2738 s1
->pack_stack
[0] = 0;
2739 s1
->pack_stack_ptr
= s1
->pack_stack
;
2742 void preprocess_new()
2748 /* init isid table */
2749 for(i
=CH_EOF
;i
<256;i
++)
2750 isidnum_table
[i
-CH_EOF
] = isid(i
) || isnum(i
);
2752 /* add all tokens */
2754 memset(hash_ident
, 0, TOK_HASH_SIZE
* sizeof(TokenSym
*));
2756 tok_ident
= TOK_IDENT
;
2765 ts
= tok_alloc(p
, r
- p
- 1);
2770 /* Preprocess the current file */
2771 int tcc_preprocess(TCCState
*s1
)
2774 BufferedFile
*file_ref
, **iptr
, **iptr_new
;
2775 int token_seen
, line_ref
, d
;
2778 preprocess_init(s1
);
2779 define_start
= define_stack
;
2780 ch
= file
->buf_ptr
[0];
2781 tok_flags
= TOK_FLAG_BOL
| TOK_FLAG_BOF
;
2782 parse_flags
= PARSE_FLAG_ASM_COMMENTS
| PARSE_FLAG_PREPROCESS
|
2783 PARSE_FLAG_LINEFEED
| PARSE_FLAG_SPACES
;
2788 iptr
= s1
->include_stack_ptr
;
2791 if (tok
== TOK_EOF
) {
2793 } else if (file
!= file_ref
) {
2795 } else if (tok
== TOK_LINEFEED
) {
2800 } else if (!token_seen
) {
2801 d
= file
->line_num
- line_ref
;
2802 if (file
!= file_ref
|| d
< 0 || d
>= 8) {
2804 iptr_new
= s1
->include_stack_ptr
;
2805 s
= iptr_new
> iptr
? " 1"
2806 : iptr_new
< iptr
? " 2"
2807 : iptr_new
> s1
->include_stack
? " 3"
2811 fprintf(s1
->outfile
, "# %d \"%s\"%s\n", file
->line_num
, file
->filename
, s
);
2814 fputs("\n", s1
->outfile
), --d
;
2816 line_ref
= (file_ref
= file
)->line_num
;
2817 token_seen
= tok
!= TOK_LINEFEED
;
2821 fputs(get_tok_str(tok
, &tokc
), s1
->outfile
);
2823 free_defines(define_start
);
2827 /* defines handling */
2828 void define_push(int v
, int macro_type
, int *str
, Sym
*first_arg
)
2832 s
= sym_push2(&define_stack
, v
, macro_type
, 0);
2834 s
->next
= first_arg
;
2835 table_ident
[v
- TOK_IDENT
]->sym_define
= s
;