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 static inline Sym
*define_find(int v
)
830 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
832 return table_ident
[v
]->sym_define
;
835 /* free define stack until top reaches 'b' */
836 void free_defines(Sym
*b
)
844 /* do not free args or predefined defines */
846 tok_str_free(top
->d
);
848 if (v
>= TOK_IDENT
&& v
< tok_ident
)
849 table_ident
[v
- TOK_IDENT
]->sym_define
= NULL
;
857 Sym
*label_find(int v
)
860 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
862 return table_ident
[v
]->sym_label
;
865 Sym
*label_push(Sym
**ptop
, int v
, int flags
)
868 s
= sym_push2(ptop
, v
, 0, 0);
870 ps
= &table_ident
[v
- TOK_IDENT
]->sym_label
;
871 if (ptop
== &global_label_stack
) {
872 /* modify the top most local identifier, so that
873 sym_identifier will point to 's' when popped */
875 ps
= &(*ps
)->prev_tok
;
882 /* pop labels until element last is reached. Look if any labels are
883 undefined. Define symbols if '&&label' was used. */
884 void label_pop(Sym
**ptop
, Sym
*slast
)
887 for(s
= *ptop
; s
!= slast
; s
= s1
) {
889 if (s
->r
== LABEL_DECLARED
) {
890 warning("label '%s' declared but not used", get_tok_str(s
->v
, NULL
));
891 } else if (s
->r
== LABEL_FORWARD
) {
892 error("label '%s' used but not defined",
893 get_tok_str(s
->v
, NULL
));
896 /* define corresponding symbol. A size of
898 put_extern_sym(s
, cur_text_section
, s
->jnext
, 1);
902 table_ident
[s
->v
- TOK_IDENT
]->sym_label
= s
->prev_tok
;
908 /* eval an expression for #if/#elif */
909 static int expr_preprocess(void)
915 while (tok
!= TOK_LINEFEED
&& tok
!= TOK_EOF
) {
916 next(); /* do macro subst */
917 if (tok
== TOK_DEFINED
) {
922 c
= define_find(tok
) != 0;
927 } else if (tok
>= TOK_IDENT
) {
928 /* if undefined macro */
932 tok_str_add_tok(&str
);
934 tok_str_add(&str
, -1); /* simulate end of file */
935 tok_str_add(&str
, 0);
936 /* now evaluate C constant expression */
941 tok_str_free(str
.str
);
945 #if defined(PARSE_DEBUG) || defined(PP_DEBUG)
946 static void tok_print(int *str
)
953 TOK_GET(t
, str
, cval
);
956 printf("%s", get_tok_str(t
, &cval
));
962 /* parse after #define */
963 void parse_define(void)
965 Sym
*s
, *first
, **ps
;
966 int v
, t
, varg
, is_vaargs
, spc
;
971 error("invalid macro name '%s'", get_tok_str(tok
, &tokc
));
972 /* XXX: should check if same macro (ANSI) */
975 /* '(' must be just after macro definition for MACRO_FUNC */
984 if (varg
== TOK_DOTS
) {
985 varg
= TOK___VA_ARGS__
;
987 } else if (tok
== TOK_DOTS
&& gnu_ext
) {
991 if (varg
< TOK_IDENT
)
992 error("badly punctuated parameter list");
993 s
= sym_push2(&define_stack
, varg
| SYM_FIELD
, is_vaargs
, 0);
1006 /* EOF testing necessary for '-D' handling */
1007 while (tok
!= TOK_LINEFEED
&& tok
!= TOK_EOF
) {
1008 /* remove spaces around ## and after '#' */
1009 if (TOK_TWOSHARPS
== tok
) {
1013 } else if ('#' == tok
) {
1015 } else if (check_space(tok
, &spc
)) {
1018 tok_str_add2(&str
, tok
, &tokc
);
1023 --str
.len
; /* remove trailing space */
1024 tok_str_add(&str
, 0);
1026 printf("define %s %d: ", get_tok_str(v
, NULL
), t
);
1029 define_push(v
, t
, str
.str
, first
);
1032 static inline int hash_cached_include(int type
, const char *filename
)
1034 const unsigned char *s
;
1038 h
= TOK_HASH_FUNC(h
, type
);
1041 h
= TOK_HASH_FUNC(h
, *s
);
1044 h
&= (CACHED_INCLUDES_HASH_SIZE
- 1);
1048 /* XXX: use a token or a hash table to accelerate matching ? */
1049 static CachedInclude
*search_cached_include(TCCState
*s1
,
1050 int type
, const char *filename
)
1054 h
= hash_cached_include(type
, filename
);
1055 i
= s1
->cached_includes_hash
[h
];
1059 e
= s1
->cached_includes
[i
- 1];
1060 if (e
->type
== type
&& !PATHCMP(e
->filename
, filename
))
1067 static inline void add_cached_include(TCCState
*s1
, int type
,
1068 const char *filename
, int ifndef_macro
)
1073 if (search_cached_include(s1
, type
, filename
))
1076 printf("adding cached '%s' %s\n", filename
, get_tok_str(ifndef_macro
, NULL
));
1078 e
= tcc_malloc(sizeof(CachedInclude
) + strlen(filename
));
1082 strcpy(e
->filename
, filename
);
1083 e
->ifndef_macro
= ifndef_macro
;
1084 dynarray_add((void ***)&s1
->cached_includes
, &s1
->nb_cached_includes
, e
);
1085 /* add in hash table */
1086 h
= hash_cached_include(type
, filename
);
1087 e
->hash_next
= s1
->cached_includes_hash
[h
];
1088 s1
->cached_includes_hash
[h
] = s1
->nb_cached_includes
;
1091 static void pragma_parse(TCCState
*s1
)
1096 if (tok
== TOK_pack
) {
1099 #pragma pack(1) // set
1100 #pragma pack() // reset to default
1101 #pragma pack(push,1) // push & set
1102 #pragma pack(pop) // restore previous
1106 if (tok
== TOK_ASM_pop
) {
1108 if (s1
->pack_stack_ptr
<= s1
->pack_stack
) {
1110 error("out of pack stack");
1112 s1
->pack_stack_ptr
--;
1116 if (tok
== TOK_ASM_push
) {
1118 if (s1
->pack_stack_ptr
>= s1
->pack_stack
+ PACK_STACK_SIZE
- 1)
1120 s1
->pack_stack_ptr
++;
1123 if (tok
!= TOK_CINT
) {
1125 error("invalid pack pragma");
1128 if (val
< 1 || val
> 16 || (val
& (val
- 1)) != 0)
1132 *s1
->pack_stack_ptr
= val
;
1138 /* is_bof is true if first non space token at beginning of file */
1139 static void preprocess(int is_bof
)
1141 TCCState
*s1
= tcc_state
;
1142 int i
, c
, n
, saved_parse_flags
;
1146 saved_parse_flags
= parse_flags
;
1147 parse_flags
= PARSE_FLAG_PREPROCESS
| PARSE_FLAG_TOK_NUM
|
1148 PARSE_FLAG_LINEFEED
;
1158 s
= define_find(tok
);
1159 /* undefine symbol by putting an invalid name */
1164 case TOK_INCLUDE_NEXT
:
1165 ch
= file
->buf_ptr
[0];
1166 /* XXX: incorrect if comments : use next_nomacro with a special mode */
1171 } else if (ch
== '\"') {
1176 while (ch
!= c
&& ch
!= '\n' && ch
!= CH_EOF
) {
1177 if ((q
- buf
) < sizeof(buf
) - 1)
1180 if (handle_stray_noerror() == 0)
1188 /* eat all spaces and comments after include */
1189 /* XXX: slightly incorrect */
1190 while (ch1
!= '\n' && ch1
!= CH_EOF
)
1194 /* computed #include : either we have only strings or
1195 we have anything enclosed in '<>' */
1198 if (tok
== TOK_STR
) {
1199 while (tok
!= TOK_LINEFEED
) {
1200 if (tok
!= TOK_STR
) {
1202 error("'#include' expects \"FILENAME\" or <FILENAME>");
1204 pstrcat(buf
, sizeof(buf
), (char *)tokc
.cstr
->data
);
1210 while (tok
!= TOK_LINEFEED
) {
1211 pstrcat(buf
, sizeof(buf
), get_tok_str(tok
, &tokc
));
1215 /* check syntax and remove '<>' */
1216 if (len
< 2 || buf
[0] != '<' || buf
[len
- 1] != '>')
1217 goto include_syntax
;
1218 memmove(buf
, buf
+ 1, len
- 2);
1219 buf
[len
- 2] = '\0';
1224 if (s1
->include_stack_ptr
>= s1
->include_stack
+ INCLUDE_STACK_SIZE
)
1225 error("#include recursion too deep");
1227 n
= s1
->nb_include_paths
+ s1
->nb_sysinclude_paths
;
1228 for (i
= -2; i
< n
; ++i
) {
1229 char buf1
[sizeof file
->filename
];
1236 /* check absolute include path */
1237 if (!IS_ABSPATH(buf
))
1241 } else if (i
== -1) {
1242 /* search in current dir if "header.h" */
1245 size
= tcc_basename(file
->filename
) - file
->filename
;
1246 memcpy(buf1
, file
->filename
, size
);
1250 /* search in all the include paths */
1251 if (i
< s1
->nb_include_paths
)
1252 path
= s1
->include_paths
[i
];
1254 path
= s1
->sysinclude_paths
[i
- s1
->nb_include_paths
];
1255 pstrcpy(buf1
, sizeof(buf1
), path
);
1256 pstrcat(buf1
, sizeof(buf1
), "/");
1259 pstrcat(buf1
, sizeof(buf1
), buf
);
1261 e
= search_cached_include(s1
, c
, buf1
);
1262 if (e
&& define_find(e
->ifndef_macro
)) {
1263 /* no need to parse the include because the 'ifndef macro'
1266 printf("%s: skipping %s\n", file
->filename
, buf
);
1270 f
= tcc_open(s1
, buf1
);
1275 if (tok
== TOK_INCLUDE_NEXT
) {
1286 printf("%s: including %s\n", file
->filename
, buf1
);
1289 /* XXX: fix current line init */
1290 /* push current file in stack */
1291 *s1
->include_stack_ptr
++ = file
;
1293 pstrcpy(f
->inc_filename
, sizeof(f
->inc_filename
), buf1
);
1295 /* add include file debug info */
1296 if (tcc_state
->do_debug
) {
1297 put_stabs(file
->filename
, N_BINCL
, 0, 0, 0);
1299 tok_flags
|= TOK_FLAG_BOF
| TOK_FLAG_BOL
;
1300 ch
= file
->buf_ptr
[0];
1303 error("include file '%s' not found", buf
);
1310 c
= expr_preprocess();
1316 if (tok
< TOK_IDENT
)
1317 error("invalid argument for '#if%sdef'", c
? "n" : "");
1321 printf("#ifndef %s\n", get_tok_str(tok
, NULL
));
1323 file
->ifndef_macro
= tok
;
1326 c
= (define_find(tok
) != 0) ^ c
;
1328 if (s1
->ifdef_stack_ptr
>= s1
->ifdef_stack
+ IFDEF_STACK_SIZE
)
1329 error("memory full");
1330 *s1
->ifdef_stack_ptr
++ = c
;
1333 if (s1
->ifdef_stack_ptr
== s1
->ifdef_stack
)
1334 error("#else without matching #if");
1335 if (s1
->ifdef_stack_ptr
[-1] & 2)
1336 error("#else after #else");
1337 c
= (s1
->ifdef_stack_ptr
[-1] ^= 3);
1340 if (s1
->ifdef_stack_ptr
== s1
->ifdef_stack
)
1341 error("#elif without matching #if");
1342 c
= s1
->ifdef_stack_ptr
[-1];
1344 error("#elif after #else");
1345 /* last #if/#elif expression was true: we skip */
1348 c
= expr_preprocess();
1349 s1
->ifdef_stack_ptr
[-1] = c
;
1351 if (s1
->ifdef_stack_ptr
== file
->ifdef_stack_ptr
+ 1)
1352 file
->ifndef_macro
= 0;
1362 if (s1
->ifdef_stack_ptr
<= file
->ifdef_stack_ptr
)
1363 error("#endif without matching #if");
1364 s1
->ifdef_stack_ptr
--;
1365 /* '#ifndef macro' was at the start of file. Now we check if
1366 an '#endif' is exactly at the end of file */
1367 if (file
->ifndef_macro
&&
1368 s1
->ifdef_stack_ptr
== file
->ifdef_stack_ptr
) {
1369 file
->ifndef_macro_saved
= file
->ifndef_macro
;
1370 /* need to set to zero to avoid false matches if another
1371 #ifndef at middle of file */
1372 file
->ifndef_macro
= 0;
1373 while (tok
!= TOK_LINEFEED
)
1375 tok_flags
|= TOK_FLAG_ENDIF
;
1381 if (tok
!= TOK_CINT
)
1383 file
->line_num
= tokc
.i
- 1; /* the line number will be incremented after */
1385 if (tok
!= TOK_LINEFEED
) {
1388 pstrcpy(file
->filename
, sizeof(file
->filename
),
1389 (char *)tokc
.cstr
->data
);
1395 ch
= file
->buf_ptr
[0];
1398 while (ch
!= '\n' && ch
!= CH_EOF
) {
1399 if ((q
- buf
) < sizeof(buf
) - 1)
1402 if (handle_stray_noerror() == 0)
1409 error("#error %s", buf
);
1411 warning("#warning %s", buf
);
1417 if (tok
== TOK_LINEFEED
|| tok
== '!' || tok
== TOK_CINT
) {
1418 /* '!' is ignored to allow C scripts. numbers are ignored
1419 to emulate cpp behaviour */
1421 if (!(saved_parse_flags
& PARSE_FLAG_ASM_COMMENTS
))
1422 warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok
, &tokc
));
1426 /* ignore other preprocess commands or #! for C scripts */
1427 while (tok
!= TOK_LINEFEED
)
1430 parse_flags
= saved_parse_flags
;
1433 /* evaluate escape codes in a string. */
1434 static void parse_escape_string(CString
*outstr
, const uint8_t *buf
, int is_long
)
1449 case '0': case '1': case '2': case '3':
1450 case '4': case '5': case '6': case '7':
1451 /* at most three octal digits */
1456 n
= n
* 8 + c
- '0';
1460 n
= n
* 8 + c
- '0';
1465 goto add_char_nonext
;
1473 if (c
>= 'a' && c
<= 'f')
1475 else if (c
>= 'A' && c
<= 'F')
1485 goto add_char_nonext
;
1509 goto invalid_escape
;
1519 if (c
>= '!' && c
<= '~')
1520 warning("unknown escape sequence: \'\\%c\'", c
);
1522 warning("unknown escape sequence: \'\\x%x\'", c
);
1529 cstr_ccat(outstr
, c
);
1531 cstr_wccat(outstr
, c
);
1533 /* add a trailing '\0' */
1535 cstr_ccat(outstr
, '\0');
1537 cstr_wccat(outstr
, '\0');
1540 /* we use 64 bit numbers */
1543 /* bn = (bn << shift) | or_val */
1544 void bn_lshift(unsigned int *bn
, int shift
, int or_val
)
1548 for(i
=0;i
<BN_SIZE
;i
++) {
1550 bn
[i
] = (v
<< shift
) | or_val
;
1551 or_val
= v
>> (32 - shift
);
1555 void bn_zero(unsigned int *bn
)
1558 for(i
=0;i
<BN_SIZE
;i
++) {
1563 /* parse number in null terminated string 'p' and return it in the
1565 void parse_number(const char *p
)
1567 int b
, t
, shift
, frac_bits
, s
, exp_val
, ch
;
1569 unsigned int bn
[BN_SIZE
];
1580 goto float_frac_parse
;
1581 } else if (t
== '0') {
1582 if (ch
== 'x' || ch
== 'X') {
1586 } else if (tcc_ext
&& (ch
== 'b' || ch
== 'B')) {
1592 /* parse all digits. cannot check octal numbers at this stage
1593 because of floating point constants */
1595 if (ch
>= 'a' && ch
<= 'f')
1597 else if (ch
>= 'A' && ch
<= 'F')
1605 if (q
>= token_buf
+ STRING_MAX_SIZE
) {
1607 error("number too long");
1613 ((ch
== 'e' || ch
== 'E') && b
== 10) ||
1614 ((ch
== 'p' || ch
== 'P') && (b
== 16 || b
== 2))) {
1616 /* NOTE: strtox should support that for hexa numbers, but
1617 non ISOC99 libcs do not support it, so we prefer to do
1619 /* hexadecimal or binary floats */
1620 /* XXX: handle overflows */
1632 } else if (t
>= 'a') {
1634 } else if (t
>= 'A') {
1639 bn_lshift(bn
, shift
, t
);
1646 if (t
>= 'a' && t
<= 'f') {
1648 } else if (t
>= 'A' && t
<= 'F') {
1650 } else if (t
>= '0' && t
<= '9') {
1656 error("invalid digit");
1657 bn_lshift(bn
, shift
, t
);
1662 if (ch
!= 'p' && ch
!= 'P')
1669 } else if (ch
== '-') {
1673 if (ch
< '0' || ch
> '9')
1674 expect("exponent digits");
1675 while (ch
>= '0' && ch
<= '9') {
1676 exp_val
= exp_val
* 10 + ch
- '0';
1679 exp_val
= exp_val
* s
;
1681 /* now we can generate the number */
1682 /* XXX: should patch directly float number */
1683 d
= (double)bn
[1] * 4294967296.0 + (double)bn
[0];
1684 d
= ldexp(d
, exp_val
- frac_bits
);
1689 /* float : should handle overflow */
1691 } else if (t
== 'L') {
1693 #ifdef TCC_TARGET_PE
1698 /* XXX: not large enough */
1699 tokc
.ld
= (long double)d
;
1706 /* decimal floats */
1708 if (q
>= token_buf
+ STRING_MAX_SIZE
)
1713 while (ch
>= '0' && ch
<= '9') {
1714 if (q
>= token_buf
+ STRING_MAX_SIZE
)
1720 if (ch
== 'e' || ch
== 'E') {
1721 if (q
>= token_buf
+ STRING_MAX_SIZE
)
1725 if (ch
== '-' || ch
== '+') {
1726 if (q
>= token_buf
+ STRING_MAX_SIZE
)
1731 if (ch
< '0' || ch
> '9')
1732 expect("exponent digits");
1733 while (ch
>= '0' && ch
<= '9') {
1734 if (q
>= token_buf
+ STRING_MAX_SIZE
)
1746 tokc
.f
= strtof(token_buf
, NULL
);
1747 } else if (t
== 'L') {
1749 #ifdef TCC_TARGET_PE
1751 tokc
.d
= strtod(token_buf
, NULL
);
1754 tokc
.ld
= strtold(token_buf
, NULL
);
1758 tokc
.d
= strtod(token_buf
, NULL
);
1762 unsigned long long n
, n1
;
1765 /* integer number */
1768 if (b
== 10 && *q
== '0') {
1775 /* no need for checks except for base 10 / 8 errors */
1778 } else if (t
>= 'a') {
1780 } else if (t
>= 'A') {
1785 error("invalid digit");
1789 /* detect overflow */
1790 /* XXX: this test is not reliable */
1792 error("integer constant overflow");
1795 /* XXX: not exactly ANSI compliant */
1796 if ((n
& 0xffffffff00000000LL
) != 0) {
1801 } else if (n
> 0x7fffffff) {
1812 error("three 'l's in integer constant");
1815 if (tok
== TOK_CINT
)
1817 else if (tok
== TOK_CUINT
)
1821 } else if (t
== 'U') {
1823 error("two 'u's in integer constant");
1825 if (tok
== TOK_CINT
)
1827 else if (tok
== TOK_CLLONG
)
1834 if (tok
== TOK_CINT
|| tok
== TOK_CUINT
)
1840 error("invalid number\n");
1844 #define PARSE2(c1, tok1, c2, tok2) \
1855 /* return next token without macro substitution */
1856 static inline void next_nomacro1(void)
1871 goto keep_tok_flags
;
1878 /* first look if it is in fact an end of buffer */
1879 if (p
>= file
->buf_end
) {
1883 if (p
>= file
->buf_end
)
1896 TCCState
*s1
= tcc_state
;
1897 if ((parse_flags
& PARSE_FLAG_LINEFEED
)
1898 && !(tok_flags
& TOK_FLAG_EOF
)) {
1899 tok_flags
|= TOK_FLAG_EOF
;
1901 goto keep_tok_flags
;
1902 } else if (s1
->include_stack_ptr
== s1
->include_stack
||
1903 !(parse_flags
& PARSE_FLAG_PREPROCESS
)) {
1904 /* no include left : end of file. */
1907 tok_flags
&= ~TOK_FLAG_EOF
;
1908 /* pop include file */
1910 /* test if previous '#endif' was after a #ifdef at
1912 if (tok_flags
& TOK_FLAG_ENDIF
) {
1914 printf("#endif %s\n", get_tok_str(file
->ifndef_macro_saved
, NULL
));
1916 add_cached_include(s1
, file
->inc_type
, file
->inc_filename
,
1917 file
->ifndef_macro_saved
);
1920 /* add end of include file debug info */
1921 if (tcc_state
->do_debug
) {
1922 put_stabd(N_EINCL
, 0, 0);
1924 /* pop include stack */
1926 s1
->include_stack_ptr
--;
1927 file
= *s1
->include_stack_ptr
;
1936 tok_flags
|= TOK_FLAG_BOL
;
1939 if (0 == (parse_flags
& PARSE_FLAG_LINEFEED
))
1942 goto keep_tok_flags
;
1947 if ((tok_flags
& TOK_FLAG_BOL
) &&
1948 (parse_flags
& PARSE_FLAG_PREPROCESS
)) {
1950 preprocess(tok_flags
& TOK_FLAG_BOF
);
1956 tok
= TOK_TWOSHARPS
;
1958 if (parse_flags
& PARSE_FLAG_ASM_COMMENTS
) {
1959 p
= parse_line_comment(p
- 1);
1968 case 'a': case 'b': case 'c': case 'd':
1969 case 'e': case 'f': case 'g': case 'h':
1970 case 'i': case 'j': case 'k': case 'l':
1971 case 'm': case 'n': case 'o': case 'p':
1972 case 'q': case 'r': case 's': case 't':
1973 case 'u': case 'v': case 'w': case 'x':
1975 case 'A': case 'B': case 'C': case 'D':
1976 case 'E': case 'F': case 'G': case 'H':
1977 case 'I': case 'J': case 'K':
1978 case 'M': case 'N': case 'O': case 'P':
1979 case 'Q': case 'R': case 'S': case 'T':
1980 case 'U': case 'V': case 'W': case 'X':
1986 h
= TOK_HASH_FUNC(h
, c
);
1990 if (!isidnum_table
[c
-CH_EOF
])
1992 h
= TOK_HASH_FUNC(h
, c
);
1999 /* fast case : no stray found, so we have the full token
2000 and we have already hashed it */
2002 h
&= (TOK_HASH_SIZE
- 1);
2003 pts
= &hash_ident
[h
];
2008 if (ts
->len
== len
&& !memcmp(ts
->str
, p1
, len
))
2010 pts
= &(ts
->hash_next
);
2012 ts
= tok_alloc_new(pts
, p1
, len
);
2016 cstr_reset(&tokcstr
);
2019 cstr_ccat(&tokcstr
, *p1
);
2025 while (isidnum_table
[c
-CH_EOF
]) {
2026 cstr_ccat(&tokcstr
, c
);
2029 ts
= tok_alloc(tokcstr
.data
, tokcstr
.size
);
2035 if (t
!= '\\' && t
!= '\'' && t
!= '\"') {
2037 goto parse_ident_fast
;
2040 if (c
== '\'' || c
== '\"') {
2044 cstr_reset(&tokcstr
);
2045 cstr_ccat(&tokcstr
, 'L');
2046 goto parse_ident_slow
;
2050 case '0': case '1': case '2': case '3':
2051 case '4': case '5': case '6': case '7':
2054 cstr_reset(&tokcstr
);
2055 /* after the first digit, accept digits, alpha, '.' or sign if
2056 prefixed by 'eEpP' */
2060 cstr_ccat(&tokcstr
, c
);
2062 if (!(isnum(c
) || isid(c
) || c
== '.' ||
2063 ((c
== '+' || c
== '-') &&
2064 (t
== 'e' || t
== 'E' || t
== 'p' || t
== 'P'))))
2067 /* We add a trailing '\0' to ease parsing */
2068 cstr_ccat(&tokcstr
, '\0');
2069 tokc
.cstr
= &tokcstr
;
2073 /* special dot handling because it can also start a number */
2076 cstr_reset(&tokcstr
);
2077 cstr_ccat(&tokcstr
, '.');
2079 } else if (c
== '.') {
2099 /* parse the string */
2101 p
= parse_pp_string(p
, sep
, &str
);
2102 cstr_ccat(&str
, '\0');
2104 /* eval the escape (should be done as TOK_PPNUM) */
2105 cstr_reset(&tokcstr
);
2106 parse_escape_string(&tokcstr
, str
.data
, is_long
);
2111 /* XXX: make it portable */
2115 char_size
= sizeof(nwchar_t
);
2116 if (tokcstr
.size
<= char_size
)
2117 error("empty character constant");
2118 if (tokcstr
.size
> 2 * char_size
)
2119 warning("multi-character character constant");
2121 tokc
.i
= *(int8_t *)tokcstr
.data
;
2124 tokc
.i
= *(nwchar_t
*)tokcstr
.data
;
2128 tokc
.cstr
= &tokcstr
;
2142 } else if (c
== '<') {
2160 } else if (c
== '>') {
2178 } else if (c
== '=') {
2191 } else if (c
== '=') {
2204 } else if (c
== '=') {
2217 } else if (c
== '=') {
2220 } else if (c
== '>') {
2228 PARSE2('!', '!', '=', TOK_NE
)
2229 PARSE2('=', '=', '=', TOK_EQ
)
2230 PARSE2('*', '*', '=', TOK_A_MUL
)
2231 PARSE2('%', '%', '=', TOK_A_MOD
)
2232 PARSE2('^', '^', '=', TOK_A_XOR
)
2234 /* comments or operator */
2238 p
= parse_comment(p
);
2240 } else if (c
== '/') {
2241 p
= parse_line_comment(p
);
2243 } else if (c
== '=') {
2263 case '$': /* only used in assembler */
2264 case '@': /* dito */
2269 error("unrecognized character \\x%02x", c
);
2275 #if defined(PARSE_DEBUG)
2276 printf("token = %s\n", get_tok_str(tok
, &tokc
));
2280 /* return next token without macro substitution. Can read input from
2282 static void next_nomacro_spc(void)
2288 TOK_GET(tok
, macro_ptr
, tokc
);
2289 if (tok
== TOK_LINENUM
) {
2290 file
->line_num
= tokc
.i
;
2299 void next_nomacro(void)
2303 } while (is_space(tok
));
2306 /* substitute args in macro_str and return allocated string */
2307 static int *macro_arg_subst(Sym
**nested_list
, int *macro_str
, Sym
*args
)
2309 int *st
, last_tok
, t
, spc
;
2318 TOK_GET(t
, macro_str
, cval
);
2323 TOK_GET(t
, macro_str
, cval
);
2326 s
= sym_find2(args
, t
);
2332 TOK_GET(t
, st
, cval
);
2333 if (!check_space(t
, &spc
))
2334 cstr_cat(&cstr
, get_tok_str(t
, &cval
));
2337 cstr_ccat(&cstr
, '\0');
2339 printf("stringize: %s\n", (char *)cstr
.data
);
2343 tok_str_add2(&str
, TOK_STR
, &cval
);
2346 tok_str_add2(&str
, t
, &cval
);
2348 } else if (t
>= TOK_IDENT
) {
2349 s
= sym_find2(args
, t
);
2352 /* if '##' is present before or after, no arg substitution */
2353 if (*macro_str
== TOK_TWOSHARPS
|| last_tok
== TOK_TWOSHARPS
) {
2354 /* special case for var arg macros : ## eats the
2355 ',' if empty VA_ARGS variable. */
2356 /* XXX: test of the ',' is not 100%
2357 reliable. should fix it to avoid security
2359 if (gnu_ext
&& s
->type
.t
&&
2360 last_tok
== TOK_TWOSHARPS
&&
2361 str
.len
>= 2 && str
.str
[str
.len
- 2] == ',') {
2363 /* suppress ',' '##' */
2366 /* suppress '##' and add variable */
2374 TOK_GET(t1
, st
, cval
);
2377 tok_str_add2(&str
, t1
, &cval
);
2381 /* NOTE: the stream cannot be read when macro
2382 substituing an argument */
2383 macro_subst(&str
, nested_list
, st
, NULL
);
2386 tok_str_add(&str
, t
);
2389 tok_str_add2(&str
, t
, &cval
);
2393 tok_str_add(&str
, 0);
2397 static char const ab_month_name
[12][4] =
2399 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2400 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
2403 /* do macro substitution of current token with macro 's' and add
2404 result to (tok_str,tok_len). 'nested_list' is the list of all
2405 macros we got inside to avoid recursing. Return non zero if no
2406 substitution needs to be done */
2407 static int macro_subst_tok(TokenString
*tok_str
,
2408 Sym
**nested_list
, Sym
*s
, struct macro_level
**can_read_stream
)
2410 Sym
*args
, *sa
, *sa1
;
2411 int mstr_allocated
, parlevel
, *mstr
, t
, t1
, *p
, spc
;
2418 /* if symbol is a macro, prepare substitution */
2419 /* special macros */
2420 if (tok
== TOK___LINE__
) {
2421 snprintf(buf
, sizeof(buf
), "%d", file
->line_num
);
2425 } else if (tok
== TOK___FILE__
) {
2426 cstrval
= file
->filename
;
2428 } else if (tok
== TOK___DATE__
|| tok
== TOK___TIME__
) {
2433 tm
= localtime(&ti
);
2434 if (tok
== TOK___DATE__
) {
2435 snprintf(buf
, sizeof(buf
), "%s %2d %d",
2436 ab_month_name
[tm
->tm_mon
], tm
->tm_mday
, tm
->tm_year
+ 1900);
2438 snprintf(buf
, sizeof(buf
), "%02d:%02d:%02d",
2439 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
2446 cstr_cat(&cstr
, cstrval
);
2447 cstr_ccat(&cstr
, '\0');
2449 tok_str_add2(tok_str
, t1
, &cval
);
2454 if (s
->type
.t
== MACRO_FUNC
) {
2455 /* NOTE: we do not use next_nomacro to avoid eating the
2456 next token. XXX: find better solution */
2460 while (is_space(t
= *p
) || TOK_LINEFEED
== t
)
2462 if (t
== 0 && can_read_stream
) {
2463 /* end of macro stream: we must look at the token
2464 after in the file */
2465 struct macro_level
*ml
= *can_read_stream
;
2471 *can_read_stream
= ml
-> prev
;
2476 /* XXX: incorrect with comments */
2477 ch
= file
->buf_ptr
[0];
2478 while (is_space(ch
) || ch
== '\n')
2482 if (t
!= '(') /* no macro subst */
2485 /* argument macro */
2490 /* NOTE: empty args are allowed, except if no args */
2492 /* handle '()' case */
2493 if (!args
&& !sa
&& tok
== ')')
2496 error("macro '%s' used with too many args",
2497 get_tok_str(s
->v
, 0));
2500 /* NOTE: non zero sa->t indicates VA_ARGS */
2501 while ((parlevel
> 0 ||
2503 (tok
!= ',' || sa
->type
.t
))) &&
2507 else if (tok
== ')')
2509 if (tok
== TOK_LINEFEED
)
2511 if (!check_space(tok
, &spc
))
2512 tok_str_add2(&str
, tok
, &tokc
);
2516 tok_str_add(&str
, 0);
2517 sa1
= sym_push2(&args
, sa
->v
& ~SYM_FIELD
, sa
->type
.t
, 0);
2521 /* special case for gcc var args: add an empty
2522 var arg argument if it is omitted */
2523 if (sa
&& sa
->type
.t
&& gnu_ext
)
2533 error("macro '%s' used with too few args",
2534 get_tok_str(s
->v
, 0));
2537 /* now subst each arg */
2538 mstr
= macro_arg_subst(nested_list
, mstr
, args
);
2543 tok_str_free(sa
->d
);
2549 sym_push2(nested_list
, s
->v
, 0, 0);
2550 macro_subst(tok_str
, nested_list
, mstr
, can_read_stream
);
2551 /* pop nested defined symbol */
2553 *nested_list
= sa1
->prev
;
2561 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
2562 return the resulting string (which must be freed). */
2563 static inline int *macro_twosharps(const int *macro_str
)
2568 TokenString macro_str1
;
2573 /* we search the first '##' */
2574 for(ptr
= macro_str
;;) {
2575 TOK_GET(t
, ptr
, cval
);
2576 if (t
== TOK_TWOSHARPS
)
2578 /* nothing more to do if end of string */
2583 /* we saw '##', so we need more processing to handle it */
2584 tok_str_new(¯o_str1
);
2585 for(ptr
= macro_str
;;) {
2586 TOK_GET(tok
, ptr
, tokc
);
2589 if (tok
== TOK_TWOSHARPS
)
2591 while (*ptr
== TOK_TWOSHARPS
) {
2593 if (t
&& t
!= TOK_TWOSHARPS
) {
2594 TOK_GET(t
, ptr
, cval
);
2596 /* We concatenate the two tokens */
2598 cstr_cat(&cstr
, get_tok_str(tok
, &tokc
));
2600 cstr_cat(&cstr
, get_tok_str(t
, &cval
));
2601 cstr_ccat(&cstr
, '\0');
2604 file
->buf_ptr
= cstr
.data
;
2607 if (0 == *file
->buf_ptr
)
2609 tok_str_add2(¯o_str1
, tok
, &tokc
);
2611 warning("pasting \"%.*s\" and \"%s\" does not give a valid preprocessing token",
2612 n
, cstr
.data
, (char*)cstr
.data
+ n
);
2618 tok_str_add2(¯o_str1
, tok
, &tokc
);
2620 tok_str_add(¯o_str1
, 0);
2621 return macro_str1
.str
;
2625 /* do macro substitution of macro_str and add result to
2626 (tok_str,tok_len). 'nested_list' is the list of all macros we got
2627 inside to avoid recursing. */
2628 static void macro_subst(TokenString
*tok_str
, Sym
**nested_list
,
2629 const int *macro_str
, struct macro_level
** can_read_stream
)
2636 struct macro_level ml
;
2638 /* first scan for '##' operator handling */
2640 macro_str1
= macro_twosharps(ptr
);
2645 /* NOTE: ptr == NULL can only happen if tokens are read from
2646 file stream due to a macro function call */
2649 TOK_GET(t
, ptr
, cval
);
2654 /* if nested substitution, do nothing */
2655 if (sym_find2(*nested_list
, t
))
2658 if (can_read_stream
)
2659 ml
.prev
= *can_read_stream
, *can_read_stream
= &ml
;
2660 macro_ptr
= (int *)ptr
;
2662 ret
= macro_subst_tok(tok_str
, nested_list
, s
, can_read_stream
);
2663 ptr
= (int *)macro_ptr
;
2665 if (can_read_stream
&& *can_read_stream
== &ml
)
2666 *can_read_stream
= ml
.prev
;
2671 if (!check_space(t
, &spc
))
2672 tok_str_add2(tok_str
, t
, &cval
);
2676 tok_str_free(macro_str1
);
2679 /* return next token with macro substitution */
2682 Sym
*nested_list
, *s
;
2684 struct macro_level
*ml
;
2687 if (parse_flags
& PARSE_FLAG_SPACES
)
2692 /* if not reading from macro substituted string, then try
2693 to substitute macros */
2694 if (tok
>= TOK_IDENT
&&
2695 (parse_flags
& PARSE_FLAG_PREPROCESS
)) {
2696 s
= define_find(tok
);
2698 /* we have a macro: we try to substitute */
2702 if (macro_subst_tok(&str
, &nested_list
, s
, &ml
) == 0) {
2703 /* substitution done, NOTE: maybe empty */
2704 tok_str_add(&str
, 0);
2705 macro_ptr
= str
.str
;
2706 macro_ptr_allocated
= str
.str
;
2713 /* end of macro or end of unget buffer */
2714 if (unget_buffer_enabled
) {
2715 macro_ptr
= unget_saved_macro_ptr
;
2716 unget_buffer_enabled
= 0;
2718 /* end of macro string: free it */
2719 tok_str_free(macro_ptr_allocated
);
2720 macro_ptr_allocated
= NULL
;
2727 /* convert preprocessor tokens into C tokens */
2728 if (tok
== TOK_PPNUM
&&
2729 (parse_flags
& PARSE_FLAG_TOK_NUM
)) {
2730 parse_number((char *)tokc
.cstr
->data
);
2734 /* better than nothing, but needs extension to handle '-E' option
2736 void preprocess_init(TCCState
*s1
)
2738 s1
->include_stack_ptr
= s1
->include_stack
;
2739 /* XXX: move that before to avoid having to initialize
2740 file->ifdef_stack_ptr ? */
2741 s1
->ifdef_stack_ptr
= s1
->ifdef_stack
;
2742 file
->ifdef_stack_ptr
= s1
->ifdef_stack_ptr
;
2744 /* XXX: not ANSI compliant: bound checking says error */
2746 s1
->pack_stack
[0] = 0;
2747 s1
->pack_stack_ptr
= s1
->pack_stack
;
2750 void preprocess_new()
2756 /* init isid table */
2757 for(i
=CH_EOF
;i
<256;i
++)
2758 isidnum_table
[i
-CH_EOF
] = isid(i
) || isnum(i
);
2760 /* add all tokens */
2762 memset(hash_ident
, 0, TOK_HASH_SIZE
* sizeof(TokenSym
*));
2764 tok_ident
= TOK_IDENT
;
2773 ts
= tok_alloc(p
, r
- p
- 1);
2778 /* Preprocess the current file */
2779 int tcc_preprocess(TCCState
*s1
)
2782 BufferedFile
*file_ref
, **iptr
, **iptr_new
;
2783 int token_seen
, line_ref
, d
;
2786 preprocess_init(s1
);
2787 define_start
= define_stack
;
2788 ch
= file
->buf_ptr
[0];
2789 tok_flags
= TOK_FLAG_BOL
| TOK_FLAG_BOF
;
2790 parse_flags
= PARSE_FLAG_ASM_COMMENTS
| PARSE_FLAG_PREPROCESS
|
2791 PARSE_FLAG_LINEFEED
| PARSE_FLAG_SPACES
;
2796 iptr
= s1
->include_stack_ptr
;
2799 if (tok
== TOK_EOF
) {
2801 } else if (file
!= file_ref
) {
2803 } else if (tok
== TOK_LINEFEED
) {
2808 } else if (!token_seen
) {
2809 d
= file
->line_num
- line_ref
;
2810 if (file
!= file_ref
|| d
< 0 || d
>= 8) {
2812 iptr_new
= s1
->include_stack_ptr
;
2813 s
= iptr_new
> iptr
? " 1"
2814 : iptr_new
< iptr
? " 2"
2815 : iptr_new
> s1
->include_stack
? " 3"
2819 fprintf(s1
->outfile
, "# %d \"%s\"%s\n", file
->line_num
, file
->filename
, s
);
2822 fputs("\n", s1
->outfile
), --d
;
2824 line_ref
= (file_ref
= file
)->line_num
;
2825 token_seen
= tok
!= TOK_LINEFEED
;
2829 fputs(get_tok_str(tok
, &tokc
), s1
->outfile
);
2831 free_defines(define_start
);
2835 /* defines handling */
2836 void define_push(int v
, int macro_type
, int *str
, Sym
*first_arg
)
2840 s
= sym_push2(&define_stack
, v
, macro_type
, 0);
2842 s
->next
= first_arg
;
2843 table_ident
[v
- TOK_IDENT
]->sym_define
= s
;