2 * TCC - Tiny C Compiler
4 * Copyright (c) 2001-2004 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 static const char tcc_keywords
[] =
23 #define DEF(id, str) str "\0"
28 /* WARNING: the content of this string encodes token numbers */
29 static char tok_two_chars
[] = "<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\313..\250##\266";
31 /* true if isid(c) || isnum(c) */
32 static unsigned char isidnum_table
[256-CH_EOF
];
35 /* allocate a new token */
36 static TokenSym
*tok_alloc_new(TokenSym
**pts
, const char *str
, int len
)
38 TokenSym
*ts
, **ptable
;
41 if (tok_ident
>= SYM_FIRST_ANOM
)
44 /* expand token table if needed */
45 i
= tok_ident
- TOK_IDENT
;
46 if ((i
% TOK_ALLOC_INCR
) == 0) {
47 ptable
= tcc_realloc(table_ident
, (i
+ TOK_ALLOC_INCR
) * sizeof(TokenSym
*));
53 ts
= tcc_malloc(sizeof(TokenSym
) + len
);
55 ts
->tok
= tok_ident
++;
56 ts
->sym_define
= NULL
;
58 ts
->sym_struct
= NULL
;
59 ts
->sym_identifier
= NULL
;
62 memcpy(ts
->str
, str
, len
);
68 #define TOK_HASH_INIT 1
69 #define TOK_HASH_FUNC(h, c) ((h) * 263 + (c))
71 /* find a token and add it if not found */
72 static TokenSym
*tok_alloc(const char *str
, int len
)
80 h
= TOK_HASH_FUNC(h
, ((unsigned char *)str
)[i
]);
81 h
&= (TOK_HASH_SIZE
- 1);
88 if (ts
->len
== len
&& !memcmp(ts
->str
, str
, len
))
90 pts
= &(ts
->hash_next
);
92 return tok_alloc_new(pts
, str
, len
);
95 /* XXX: buffer overflow */
96 /* XXX: float tokens */
97 char *get_tok_str(int v
, CValue
*cv
)
99 static char buf
[STRING_MAX_SIZE
+ 1];
100 static CString cstr_buf
;
106 /* NOTE: to go faster, we give a fixed buffer for small strings */
107 cstr_reset(&cstr_buf
);
109 cstr_buf
.size_allocated
= sizeof(buf
);
115 /* XXX: not quite exact, but only useful for testing */
116 sprintf(p
, "%u", cv
->ui
);
120 /* XXX: not quite exact, but only useful for testing */
121 sprintf(p
, "%Lu", cv
->ull
);
124 cstr_ccat(&cstr_buf
, 'L');
126 cstr_ccat(&cstr_buf
, '\'');
127 add_char(&cstr_buf
, cv
->i
);
128 cstr_ccat(&cstr_buf
, '\'');
129 cstr_ccat(&cstr_buf
, '\0');
133 len
= cstr
->size
- 1;
135 add_char(&cstr_buf
, ((unsigned char *)cstr
->data
)[i
]);
136 cstr_ccat(&cstr_buf
, '\0');
139 cstr_ccat(&cstr_buf
, 'L');
142 cstr_ccat(&cstr_buf
, '\"');
144 len
= cstr
->size
- 1;
146 add_char(&cstr_buf
, ((unsigned char *)cstr
->data
)[i
]);
148 len
= (cstr
->size
/ sizeof(nwchar_t
)) - 1;
150 add_char(&cstr_buf
, ((nwchar_t
*)cstr
->data
)[i
]);
152 cstr_ccat(&cstr_buf
, '\"');
153 cstr_ccat(&cstr_buf
, '\0');
162 return strcpy(p
, "...");
164 return strcpy(p
, "<<=");
166 return strcpy(p
, ">>=");
169 /* search in two bytes table */
183 } else if (v
< tok_ident
) {
184 return table_ident
[v
- TOK_IDENT
]->str
;
185 } else if (v
>= SYM_FIRST_ANOM
) {
186 /* special name for anonymous symbol */
187 sprintf(p
, "L.%u", v
- SYM_FIRST_ANOM
);
189 /* should never happen */
194 return cstr_buf
.data
;
197 /* fill input buffer and peek next char */
198 static int tcc_peekc_slow(BufferedFile
*bf
)
201 /* only tries to read if really end of buffer */
202 if (bf
->buf_ptr
>= bf
->buf_end
) {
204 #if defined(PARSE_DEBUG)
209 len
= read(bf
->fd
, bf
->buffer
, len
);
216 bf
->buf_ptr
= bf
->buffer
;
217 bf
->buf_end
= bf
->buffer
+ len
;
218 *bf
->buf_end
= CH_EOB
;
220 if (bf
->buf_ptr
< bf
->buf_end
) {
221 return bf
->buf_ptr
[0];
223 bf
->buf_ptr
= bf
->buf_end
;
228 /* return the current character, handling end of block if necessary
230 static int handle_eob(void)
232 return tcc_peekc_slow(file
);
235 /* read next char from current input file and handle end of input buffer */
236 static inline void inp(void)
238 ch
= *(++(file
->buf_ptr
));
239 /* end of buffer/file handling */
244 /* handle '\[\r]\n' */
245 static int handle_stray_noerror(void)
252 } else if (ch
== '\r') {
266 static void handle_stray(void)
268 if (handle_stray_noerror())
269 error("stray '\\' in program");
272 /* skip the stray and handle the \\n case. Output an error if
273 incorrect char after the stray */
274 static int handle_stray1(uint8_t *p
)
278 if (p
>= file
->buf_end
) {
295 /* handle just the EOB case, but not stray */
296 #define PEEKC_EOB(c, p)\
307 /* handle the complicated stray case */
313 c = handle_stray1(p);\
318 /* input with '\[\r]\n' handling. Note that this function cannot
319 handle other characters after '\', so you cannot call it inside
320 strings or comments */
321 static void minp(void)
329 /* single line C++ comments */
330 static uint8_t *parse_line_comment(uint8_t *p
)
338 if (c
== '\n' || c
== CH_EOF
) {
340 } else if (c
== '\\') {
349 } else if (c
== '\r') {
367 static uint8_t *parse_comment(uint8_t *p
)
376 if (c
== '\n' || c
== '*' || c
== '\\')
380 if (c
== '\n' || c
== '*' || c
== '\\')
384 /* now we can handle all the cases */
388 } else if (c
== '*') {
394 } else if (c
== '/') {
396 } else if (c
== '\\') {
401 /* skip '\[\r]\n', otherwise just skip the stray */
407 } else if (c
== '\r') {
424 /* stray, eob or eof */
429 error("unexpected end of file in comment");
430 } else if (c
== '\\') {
442 /* space exlcuding newline */
443 static inline int is_space(int ch
)
445 return ch
== ' ' || ch
== '\t' || ch
== '\v' || ch
== '\f' || ch
== '\r';
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
;
639 /* return the number of additional 'ints' necessary to store the
641 static inline int tok_ext_size(int t
)
655 error("unsupported token");
662 return LDOUBLE_SIZE
/ 4;
668 /* token string handling */
670 static inline void tok_str_new(TokenString
*s
)
674 s
->allocated_len
= 0;
675 s
->last_line_num
= -1;
678 static void tok_str_free(int *str
)
683 static int *tok_str_realloc(TokenString
*s
)
687 if (s
->allocated_len
== 0) {
690 len
= s
->allocated_len
* 2;
692 str
= tcc_realloc(s
->str
, len
* sizeof(int));
694 error("memory full");
695 s
->allocated_len
= len
;
700 static void tok_str_add(TokenString
*s
, int t
)
706 if (len
>= s
->allocated_len
)
707 str
= tok_str_realloc(s
);
712 static void tok_str_add2(TokenString
*s
, int t
, CValue
*cv
)
719 /* allocate space for worst case */
720 if (len
+ TOK_MAX_SIZE
> s
->allocated_len
)
721 str
= tok_str_realloc(s
);
730 str
[len
++] = cv
->tab
[0];
739 nb_words
= (sizeof(CString
) + cv
->cstr
->size
+ 3) >> 2;
740 while ((len
+ nb_words
) > s
->allocated_len
)
741 str
= tok_str_realloc(s
);
742 cstr
= (CString
*)(str
+ len
);
744 cstr
->size
= cv
->cstr
->size
;
745 cstr
->data_allocated
= NULL
;
746 cstr
->size_allocated
= cstr
->size
;
747 memcpy((char *)cstr
+ sizeof(CString
),
748 cv
->cstr
->data
, cstr
->size
);
755 #if LDOUBLE_SIZE == 8
758 str
[len
++] = cv
->tab
[0];
759 str
[len
++] = cv
->tab
[1];
761 #if LDOUBLE_SIZE == 12
763 str
[len
++] = cv
->tab
[0];
764 str
[len
++] = cv
->tab
[1];
765 str
[len
++] = cv
->tab
[2];
766 #elif LDOUBLE_SIZE == 16
768 str
[len
++] = cv
->tab
[0];
769 str
[len
++] = cv
->tab
[1];
770 str
[len
++] = cv
->tab
[2];
771 str
[len
++] = cv
->tab
[3];
772 #elif LDOUBLE_SIZE != 8
773 #error add long double size support
782 /* add the current parse token in token string 's' */
783 static void tok_str_add_tok(TokenString
*s
)
787 /* save line number info */
788 if (file
->line_num
!= s
->last_line_num
) {
789 s
->last_line_num
= file
->line_num
;
790 cval
.i
= s
->last_line_num
;
791 tok_str_add2(s
, TOK_LINENUM
, &cval
);
793 tok_str_add2(s
, tok
, &tokc
);
796 #if LDOUBLE_SIZE == 16
797 #define LDOUBLE_GET(p, cv) \
802 #elif LDOUBLE_SIZE == 12
803 #define LDOUBLE_GET(p, cv) \
807 #elif LDOUBLE_SIZE == 8
808 #define LDOUBLE_GET(p, cv) \
812 #error add long double size support
816 /* get a token from an integer array and increment pointer
817 accordingly. we code it as a macro to avoid pointer aliasing. */
818 #define TOK_GET(t, p, cv) \
833 cv.cstr = (CString *)p; \
834 cv.cstr->data = (char *)p + sizeof(CString);\
835 p += (sizeof(CString) + cv.cstr->size + 3) >> 2;\
845 LDOUBLE_GET(p, cv); \
846 p += LDOUBLE_SIZE / 4; \
853 /* defines handling */
854 static inline void define_push(int v
, int macro_type
, int *str
, Sym
*first_arg
)
858 s
= sym_push2(&define_stack
, v
, macro_type
, (long)str
);
860 table_ident
[v
- TOK_IDENT
]->sym_define
= s
;
863 /* undefined a define symbol. Its name is just set to zero */
864 static void define_undef(Sym
*s
)
868 if (v
>= TOK_IDENT
&& v
< tok_ident
)
869 table_ident
[v
- TOK_IDENT
]->sym_define
= NULL
;
873 static inline Sym
*define_find(int v
)
876 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
878 return table_ident
[v
]->sym_define
;
881 /* free define stack until top reaches 'b' */
882 static void free_defines(Sym
*b
)
890 /* do not free args or predefined defines */
892 tok_str_free((int *)top
->c
);
894 if (v
>= TOK_IDENT
&& v
< tok_ident
)
895 table_ident
[v
- TOK_IDENT
]->sym_define
= NULL
;
903 static Sym
*label_find(int v
)
906 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
908 return table_ident
[v
]->sym_label
;
911 static Sym
*label_push(Sym
**ptop
, int v
, int flags
)
914 s
= sym_push2(ptop
, v
, 0, 0);
916 ps
= &table_ident
[v
- TOK_IDENT
]->sym_label
;
917 if (ptop
== &global_label_stack
) {
918 /* modify the top most local identifier, so that
919 sym_identifier will point to 's' when popped */
921 ps
= &(*ps
)->prev_tok
;
928 /* pop labels until element last is reached. Look if any labels are
929 undefined. Define symbols if '&&label' was used. */
930 static void label_pop(Sym
**ptop
, Sym
*slast
)
933 for(s
= *ptop
; s
!= slast
; s
= s1
) {
935 if (s
->r
== LABEL_DECLARED
) {
936 warning("label '%s' declared but not used", get_tok_str(s
->v
, NULL
));
937 } else if (s
->r
== LABEL_FORWARD
) {
938 error("label '%s' used but not defined",
939 get_tok_str(s
->v
, NULL
));
942 /* define corresponding symbol. A size of
944 put_extern_sym(s
, cur_text_section
, (long)s
->next
, 1);
948 table_ident
[s
->v
- TOK_IDENT
]->sym_label
= s
->prev_tok
;
954 /* eval an expression for #if/#elif */
955 static int expr_preprocess(void)
961 while (tok
!= TOK_LINEFEED
&& tok
!= TOK_EOF
) {
962 next(); /* do macro subst */
963 if (tok
== TOK_DEFINED
) {
968 c
= define_find(tok
) != 0;
973 } else if (tok
>= TOK_IDENT
) {
974 /* if undefined macro */
978 tok_str_add_tok(&str
);
980 tok_str_add(&str
, -1); /* simulate end of file */
981 tok_str_add(&str
, 0);
982 /* now evaluate C constant expression */
987 tok_str_free(str
.str
);
991 #if defined(PARSE_DEBUG) || defined(PP_DEBUG)
992 static void tok_print(int *str
)
999 TOK_GET(t
, str
, cval
);
1002 printf("%s", get_tok_str(t
, &cval
));
1008 /* parse after #define */
1009 static void parse_define(void)
1011 Sym
*s
, *first
, **ps
;
1012 int v
, t
, varg
, is_vaargs
, spc
;
1017 error("invalid macro name '%s'", get_tok_str(tok
, &tokc
));
1018 /* XXX: should check if same macro (ANSI) */
1021 /* '(' must be just after macro definition for MACRO_FUNC */
1026 while (tok
!= ')') {
1030 if (varg
== TOK_DOTS
) {
1031 varg
= TOK___VA_ARGS__
;
1033 } else if (tok
== TOK_DOTS
&& gnu_ext
) {
1037 if (varg
< TOK_IDENT
)
1038 error("badly punctuated parameter list");
1039 s
= sym_push2(&define_stack
, varg
| SYM_FIELD
, is_vaargs
, 0);
1052 /* EOF testing necessary for '-D' handling */
1053 while (tok
!= TOK_LINEFEED
&& tok
!= TOK_EOF
) {
1054 /* remove spaces around ## and after '#' */
1055 if (TOK_TWOSHARPS
== tok
) {
1059 } else if ('#' == tok
) {
1061 } else if (check_space(tok
, &spc
)) {
1064 tok_str_add2(&str
, tok
, &tokc
);
1069 --str
.len
; /* remove trailing space */
1070 tok_str_add(&str
, 0);
1072 printf("define %s %d: ", get_tok_str(v
, NULL
), t
);
1075 define_push(v
, t
, str
.str
, first
);
1078 static inline int hash_cached_include(int type
, const char *filename
)
1080 const unsigned char *s
;
1084 h
= TOK_HASH_FUNC(h
, type
);
1087 h
= TOK_HASH_FUNC(h
, *s
);
1090 h
&= (CACHED_INCLUDES_HASH_SIZE
- 1);
1094 /* XXX: use a token or a hash table to accelerate matching ? */
1095 static CachedInclude
*search_cached_include(TCCState
*s1
,
1096 int type
, const char *filename
)
1100 h
= hash_cached_include(type
, filename
);
1101 i
= s1
->cached_includes_hash
[h
];
1105 e
= s1
->cached_includes
[i
- 1];
1106 if (e
->type
== type
&& !PATHCMP(e
->filename
, filename
))
1113 static inline void add_cached_include(TCCState
*s1
, int type
,
1114 const char *filename
, int ifndef_macro
)
1119 if (search_cached_include(s1
, type
, filename
))
1122 printf("adding cached '%s' %s\n", filename
, get_tok_str(ifndef_macro
, NULL
));
1124 e
= tcc_malloc(sizeof(CachedInclude
) + strlen(filename
));
1128 strcpy(e
->filename
, filename
);
1129 e
->ifndef_macro
= ifndef_macro
;
1130 dynarray_add((void ***)&s1
->cached_includes
, &s1
->nb_cached_includes
, e
);
1131 /* add in hash table */
1132 h
= hash_cached_include(type
, filename
);
1133 e
->hash_next
= s1
->cached_includes_hash
[h
];
1134 s1
->cached_includes_hash
[h
] = s1
->nb_cached_includes
;
1137 static void pragma_parse(TCCState
*s1
)
1142 if (tok
== TOK_pack
) {
1145 #pragma pack(1) // set
1146 #pragma pack() // reset to default
1147 #pragma pack(push,1) // push & set
1148 #pragma pack(pop) // restore previous
1152 if (tok
== TOK_ASM_pop
) {
1154 if (s1
->pack_stack_ptr
<= s1
->pack_stack
) {
1156 error("out of pack stack");
1158 s1
->pack_stack_ptr
--;
1162 if (tok
== TOK_ASM_push
) {
1164 if (s1
->pack_stack_ptr
>= s1
->pack_stack
+ PACK_STACK_SIZE
- 1)
1166 s1
->pack_stack_ptr
++;
1169 if (tok
!= TOK_CINT
) {
1171 error("invalid pack pragma");
1174 if (val
< 1 || val
> 16 || (val
& (val
- 1)) != 0)
1178 *s1
->pack_stack_ptr
= val
;
1184 /* is_bof is true if first non space token at beginning of file */
1185 static void preprocess(int is_bof
)
1187 TCCState
*s1
= tcc_state
;
1188 int size
, i
, c
, n
, saved_parse_flags
;
1195 saved_parse_flags
= parse_flags
;
1196 parse_flags
= PARSE_FLAG_PREPROCESS
| PARSE_FLAG_TOK_NUM
|
1197 PARSE_FLAG_LINEFEED
;
1207 s
= define_find(tok
);
1208 /* undefine symbol by putting an invalid name */
1213 case TOK_INCLUDE_NEXT
:
1214 ch
= file
->buf_ptr
[0];
1215 /* XXX: incorrect if comments : use next_nomacro with a special mode */
1220 } else if (ch
== '\"') {
1225 while (ch
!= c
&& ch
!= '\n' && ch
!= CH_EOF
) {
1226 if ((q
- buf
) < sizeof(buf
) - 1)
1229 if (handle_stray_noerror() == 0)
1237 /* eat all spaces and comments after include */
1238 /* XXX: slightly incorrect */
1239 while (ch1
!= '\n' && ch1
!= CH_EOF
)
1243 /* computed #include : either we have only strings or
1244 we have anything enclosed in '<>' */
1247 if (tok
== TOK_STR
) {
1248 while (tok
!= TOK_LINEFEED
) {
1249 if (tok
!= TOK_STR
) {
1251 error("'#include' expects \"FILENAME\" or <FILENAME>");
1253 pstrcat(buf
, sizeof(buf
), (char *)tokc
.cstr
->data
);
1259 while (tok
!= TOK_LINEFEED
) {
1260 pstrcat(buf
, sizeof(buf
), get_tok_str(tok
, &tokc
));
1264 /* check syntax and remove '<>' */
1265 if (len
< 2 || buf
[0] != '<' || buf
[len
- 1] != '>')
1266 goto include_syntax
;
1267 memmove(buf
, buf
+ 1, len
- 2);
1268 buf
[len
- 2] = '\0';
1273 e
= search_cached_include(s1
, c
, buf
);
1274 if (e
&& define_find(e
->ifndef_macro
)) {
1275 /* no need to parse the include because the 'ifndef macro'
1278 printf("%s: skipping %s\n", file
->filename
, buf
);
1281 if (s1
->include_stack_ptr
>= s1
->include_stack
+ INCLUDE_STACK_SIZE
)
1282 error("#include recursion too deep");
1283 /* push current file in stack */
1284 /* XXX: fix current line init */
1285 *s1
->include_stack_ptr
++ = file
;
1287 /* check absolute include path */
1288 if (IS_ABSPATH(buf
)) {
1289 f
= tcc_open(s1
, buf
);
1294 /* first search in current dir if "header.h" */
1295 size
= tcc_basename(file
->filename
) - file
->filename
;
1296 if (size
> sizeof(buf1
) - 1)
1297 size
= sizeof(buf1
) - 1;
1298 memcpy(buf1
, file
->filename
, size
);
1300 pstrcat(buf1
, sizeof(buf1
), buf
);
1301 f
= tcc_open(s1
, buf1
);
1303 if (tok
== TOK_INCLUDE_NEXT
)
1309 /* now search in all the include paths */
1310 n
= s1
->nb_include_paths
+ s1
->nb_sysinclude_paths
;
1311 for(i
= 0; i
< n
; i
++) {
1313 if (i
< s1
->nb_include_paths
)
1314 path
= s1
->include_paths
[i
];
1316 path
= s1
->sysinclude_paths
[i
- s1
->nb_include_paths
];
1317 pstrcpy(buf1
, sizeof(buf1
), path
);
1318 pstrcat(buf1
, sizeof(buf1
), "/");
1319 pstrcat(buf1
, sizeof(buf1
), buf
);
1320 f
= tcc_open(s1
, buf1
);
1322 if (tok
== TOK_INCLUDE_NEXT
)
1328 --s1
->include_stack_ptr
;
1329 error("include file '%s' not found", buf
);
1333 printf("%s: including %s\n", file
->filename
, buf1
);
1336 pstrcpy(f
->inc_filename
, sizeof(f
->inc_filename
), buf
);
1338 /* add include file debug info */
1340 put_stabs(file
->filename
, N_BINCL
, 0, 0, 0);
1342 tok_flags
|= TOK_FLAG_BOF
| TOK_FLAG_BOL
;
1343 ch
= file
->buf_ptr
[0];
1351 c
= expr_preprocess();
1357 if (tok
< TOK_IDENT
)
1358 error("invalid argument for '#if%sdef'", c
? "n" : "");
1362 printf("#ifndef %s\n", get_tok_str(tok
, NULL
));
1364 file
->ifndef_macro
= tok
;
1367 c
= (define_find(tok
) != 0) ^ c
;
1369 if (s1
->ifdef_stack_ptr
>= s1
->ifdef_stack
+ IFDEF_STACK_SIZE
)
1370 error("memory full");
1371 *s1
->ifdef_stack_ptr
++ = c
;
1374 if (s1
->ifdef_stack_ptr
== s1
->ifdef_stack
)
1375 error("#else without matching #if");
1376 if (s1
->ifdef_stack_ptr
[-1] & 2)
1377 error("#else after #else");
1378 c
= (s1
->ifdef_stack_ptr
[-1] ^= 3);
1381 if (s1
->ifdef_stack_ptr
== s1
->ifdef_stack
)
1382 error("#elif without matching #if");
1383 c
= s1
->ifdef_stack_ptr
[-1];
1385 error("#elif after #else");
1386 /* last #if/#elif expression was true: we skip */
1389 c
= expr_preprocess();
1390 s1
->ifdef_stack_ptr
[-1] = c
;
1400 if (s1
->ifdef_stack_ptr
<= file
->ifdef_stack_ptr
)
1401 error("#endif without matching #if");
1402 s1
->ifdef_stack_ptr
--;
1403 /* '#ifndef macro' was at the start of file. Now we check if
1404 an '#endif' is exactly at the end of file */
1405 if (file
->ifndef_macro
&&
1406 s1
->ifdef_stack_ptr
== file
->ifdef_stack_ptr
) {
1407 file
->ifndef_macro_saved
= file
->ifndef_macro
;
1408 /* need to set to zero to avoid false matches if another
1409 #ifndef at middle of file */
1410 file
->ifndef_macro
= 0;
1411 while (tok
!= TOK_LINEFEED
)
1413 tok_flags
|= TOK_FLAG_ENDIF
;
1419 if (tok
!= TOK_CINT
)
1421 file
->line_num
= tokc
.i
- 1; /* the line number will be incremented after */
1423 if (tok
!= TOK_LINEFEED
) {
1426 pstrcpy(file
->filename
, sizeof(file
->filename
),
1427 (char *)tokc
.cstr
->data
);
1433 ch
= file
->buf_ptr
[0];
1436 while (ch
!= '\n' && ch
!= CH_EOF
) {
1437 if ((q
- buf
) < sizeof(buf
) - 1)
1440 if (handle_stray_noerror() == 0)
1447 error("#error %s", buf
);
1449 warning("#warning %s", buf
);
1455 if (tok
== TOK_LINEFEED
|| tok
== '!' || tok
== TOK_CINT
) {
1456 /* '!' is ignored to allow C scripts. numbers are ignored
1457 to emulate cpp behaviour */
1459 if (!(saved_parse_flags
& PARSE_FLAG_ASM_COMMENTS
))
1460 warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok
, &tokc
));
1464 /* ignore other preprocess commands or #! for C scripts */
1465 while (tok
!= TOK_LINEFEED
)
1468 parse_flags
= saved_parse_flags
;
1471 /* evaluate escape codes in a string. */
1472 static void parse_escape_string(CString
*outstr
, const uint8_t *buf
, int is_long
)
1487 case '0': case '1': case '2': case '3':
1488 case '4': case '5': case '6': case '7':
1489 /* at most three octal digits */
1494 n
= n
* 8 + c
- '0';
1498 n
= n
* 8 + c
- '0';
1503 goto add_char_nonext
;
1511 if (c
>= 'a' && c
<= 'f')
1513 else if (c
>= 'A' && c
<= 'F')
1523 goto add_char_nonext
;
1547 goto invalid_escape
;
1557 if (c
>= '!' && c
<= '~')
1558 warning("unknown escape sequence: \'\\%c\'", c
);
1560 warning("unknown escape sequence: \'\\x%x\'", c
);
1567 cstr_ccat(outstr
, c
);
1569 cstr_wccat(outstr
, c
);
1571 /* add a trailing '\0' */
1573 cstr_ccat(outstr
, '\0');
1575 cstr_wccat(outstr
, '\0');
1578 /* we use 64 bit numbers */
1581 /* bn = (bn << shift) | or_val */
1582 void bn_lshift(unsigned int *bn
, int shift
, int or_val
)
1586 for(i
=0;i
<BN_SIZE
;i
++) {
1588 bn
[i
] = (v
<< shift
) | or_val
;
1589 or_val
= v
>> (32 - shift
);
1593 void bn_zero(unsigned int *bn
)
1596 for(i
=0;i
<BN_SIZE
;i
++) {
1601 /* parse number in null terminated string 'p' and return it in the
1603 void parse_number(const char *p
)
1605 int b
, t
, shift
, frac_bits
, s
, exp_val
, ch
;
1607 unsigned int bn
[BN_SIZE
];
1618 goto float_frac_parse
;
1619 } else if (t
== '0') {
1620 if (ch
== 'x' || ch
== 'X') {
1624 } else if (tcc_ext
&& (ch
== 'b' || ch
== 'B')) {
1630 /* parse all digits. cannot check octal numbers at this stage
1631 because of floating point constants */
1633 if (ch
>= 'a' && ch
<= 'f')
1635 else if (ch
>= 'A' && ch
<= 'F')
1643 if (q
>= token_buf
+ STRING_MAX_SIZE
) {
1645 error("number too long");
1651 ((ch
== 'e' || ch
== 'E') && b
== 10) ||
1652 ((ch
== 'p' || ch
== 'P') && (b
== 16 || b
== 2))) {
1654 /* NOTE: strtox should support that for hexa numbers, but
1655 non ISOC99 libcs do not support it, so we prefer to do
1657 /* hexadecimal or binary floats */
1658 /* XXX: handle overflows */
1670 } else if (t
>= 'a') {
1672 } else if (t
>= 'A') {
1677 bn_lshift(bn
, shift
, t
);
1684 if (t
>= 'a' && t
<= 'f') {
1686 } else if (t
>= 'A' && t
<= 'F') {
1688 } else if (t
>= '0' && t
<= '9') {
1694 error("invalid digit");
1695 bn_lshift(bn
, shift
, t
);
1700 if (ch
!= 'p' && ch
!= 'P')
1707 } else if (ch
== '-') {
1711 if (ch
< '0' || ch
> '9')
1712 expect("exponent digits");
1713 while (ch
>= '0' && ch
<= '9') {
1714 exp_val
= exp_val
* 10 + ch
- '0';
1717 exp_val
= exp_val
* s
;
1719 /* now we can generate the number */
1720 /* XXX: should patch directly float number */
1721 d
= (double)bn
[1] * 4294967296.0 + (double)bn
[0];
1722 d
= ldexp(d
, exp_val
- frac_bits
);
1727 /* float : should handle overflow */
1729 } else if (t
== 'L') {
1732 /* XXX: not large enough */
1733 tokc
.ld
= (long double)d
;
1739 /* decimal floats */
1741 if (q
>= token_buf
+ STRING_MAX_SIZE
)
1746 while (ch
>= '0' && ch
<= '9') {
1747 if (q
>= token_buf
+ STRING_MAX_SIZE
)
1753 if (ch
== 'e' || ch
== 'E') {
1754 if (q
>= token_buf
+ STRING_MAX_SIZE
)
1758 if (ch
== '-' || ch
== '+') {
1759 if (q
>= token_buf
+ STRING_MAX_SIZE
)
1764 if (ch
< '0' || ch
> '9')
1765 expect("exponent digits");
1766 while (ch
>= '0' && ch
<= '9') {
1767 if (q
>= token_buf
+ STRING_MAX_SIZE
)
1779 tokc
.f
= strtof(token_buf
, NULL
);
1780 } else if (t
== 'L') {
1783 tokc
.ld
= strtold(token_buf
, NULL
);
1786 tokc
.d
= strtod(token_buf
, NULL
);
1790 unsigned long long n
, n1
;
1793 /* integer number */
1796 if (b
== 10 && *q
== '0') {
1803 /* no need for checks except for base 10 / 8 errors */
1806 } else if (t
>= 'a') {
1808 } else if (t
>= 'A') {
1813 error("invalid digit");
1817 /* detect overflow */
1818 /* XXX: this test is not reliable */
1820 error("integer constant overflow");
1823 /* XXX: not exactly ANSI compliant */
1824 if ((n
& 0xffffffff00000000LL
) != 0) {
1829 } else if (n
> 0x7fffffff) {
1840 error("three 'l's in integer constant");
1843 if (tok
== TOK_CINT
)
1845 else if (tok
== TOK_CUINT
)
1849 } else if (t
== 'U') {
1851 error("two 'u's in integer constant");
1853 if (tok
== TOK_CINT
)
1855 else if (tok
== TOK_CLLONG
)
1862 if (tok
== TOK_CINT
|| tok
== TOK_CUINT
)
1868 error("invalid number\n");
1872 #define PARSE2(c1, tok1, c2, tok2) \
1883 /* return next token without macro substitution */
1884 static inline void next_nomacro1(void)
1899 goto keep_tok_flags
;
1906 /* first look if it is in fact an end of buffer */
1907 if (p
>= file
->buf_end
) {
1911 if (p
>= file
->buf_end
)
1924 TCCState
*s1
= tcc_state
;
1925 if ((parse_flags
& PARSE_FLAG_LINEFEED
)
1926 && !(tok_flags
& TOK_FLAG_EOF
)) {
1927 tok_flags
|= TOK_FLAG_EOF
;
1929 goto keep_tok_flags
;
1930 } else if (s1
->include_stack_ptr
== s1
->include_stack
||
1931 !(parse_flags
& PARSE_FLAG_PREPROCESS
)) {
1932 /* no include left : end of file. */
1935 tok_flags
&= ~TOK_FLAG_EOF
;
1936 /* pop include file */
1938 /* test if previous '#endif' was after a #ifdef at
1940 if (tok_flags
& TOK_FLAG_ENDIF
) {
1942 printf("#endif %s\n", get_tok_str(file
->ifndef_macro_saved
, NULL
));
1944 add_cached_include(s1
, file
->inc_type
, file
->inc_filename
,
1945 file
->ifndef_macro_saved
);
1948 /* add end of include file debug info */
1950 put_stabd(N_EINCL
, 0, 0);
1952 /* pop include stack */
1954 s1
->include_stack_ptr
--;
1955 file
= *s1
->include_stack_ptr
;
1964 tok_flags
|= TOK_FLAG_BOL
;
1966 if (0 == (parse_flags
& PARSE_FLAG_LINEFEED
))
1969 goto keep_tok_flags
;
1974 if ((tok_flags
& TOK_FLAG_BOL
) &&
1975 (parse_flags
& PARSE_FLAG_PREPROCESS
)) {
1977 preprocess(tok_flags
& TOK_FLAG_BOF
);
1983 tok
= TOK_TWOSHARPS
;
1985 if (parse_flags
& PARSE_FLAG_ASM_COMMENTS
) {
1986 p
= parse_line_comment(p
- 1);
1995 case 'a': case 'b': case 'c': case 'd':
1996 case 'e': case 'f': case 'g': case 'h':
1997 case 'i': case 'j': case 'k': case 'l':
1998 case 'm': case 'n': case 'o': case 'p':
1999 case 'q': case 'r': case 's': case 't':
2000 case 'u': case 'v': case 'w': case 'x':
2002 case 'A': case 'B': case 'C': case 'D':
2003 case 'E': case 'F': case 'G': case 'H':
2004 case 'I': case 'J': case 'K':
2005 case 'M': case 'N': case 'O': case 'P':
2006 case 'Q': case 'R': case 'S': case 'T':
2007 case 'U': case 'V': case 'W': case 'X':
2013 h
= TOK_HASH_FUNC(h
, c
);
2017 if (!isidnum_table
[c
-CH_EOF
])
2019 h
= TOK_HASH_FUNC(h
, c
);
2026 /* fast case : no stray found, so we have the full token
2027 and we have already hashed it */
2029 h
&= (TOK_HASH_SIZE
- 1);
2030 pts
= &hash_ident
[h
];
2035 if (ts
->len
== len
&& !memcmp(ts
->str
, p1
, len
))
2037 pts
= &(ts
->hash_next
);
2039 ts
= tok_alloc_new(pts
, p1
, len
);
2043 cstr_reset(&tokcstr
);
2046 cstr_ccat(&tokcstr
, *p1
);
2052 while (isidnum_table
[c
-CH_EOF
]) {
2053 cstr_ccat(&tokcstr
, c
);
2056 ts
= tok_alloc(tokcstr
.data
, tokcstr
.size
);
2062 if (t
!= '\\' && t
!= '\'' && t
!= '\"') {
2064 goto parse_ident_fast
;
2067 if (c
== '\'' || c
== '\"') {
2071 cstr_reset(&tokcstr
);
2072 cstr_ccat(&tokcstr
, 'L');
2073 goto parse_ident_slow
;
2077 case '0': case '1': case '2': case '3':
2078 case '4': case '5': case '6': case '7':
2081 cstr_reset(&tokcstr
);
2082 /* after the first digit, accept digits, alpha, '.' or sign if
2083 prefixed by 'eEpP' */
2087 cstr_ccat(&tokcstr
, c
);
2089 if (!(isnum(c
) || isid(c
) || c
== '.' ||
2090 ((c
== '+' || c
== '-') &&
2091 (t
== 'e' || t
== 'E' || t
== 'p' || t
== 'P'))))
2094 /* We add a trailing '\0' to ease parsing */
2095 cstr_ccat(&tokcstr
, '\0');
2096 tokc
.cstr
= &tokcstr
;
2100 /* special dot handling because it can also start a number */
2103 cstr_reset(&tokcstr
);
2104 cstr_ccat(&tokcstr
, '.');
2106 } else if (c
== '.') {
2126 /* parse the string */
2128 p
= parse_pp_string(p
, sep
, &str
);
2129 cstr_ccat(&str
, '\0');
2131 /* eval the escape (should be done as TOK_PPNUM) */
2132 cstr_reset(&tokcstr
);
2133 parse_escape_string(&tokcstr
, str
.data
, is_long
);
2138 /* XXX: make it portable */
2142 char_size
= sizeof(nwchar_t
);
2143 if (tokcstr
.size
<= char_size
)
2144 error("empty character constant");
2145 if (tokcstr
.size
> 2 * char_size
)
2146 warning("multi-character character constant");
2148 tokc
.i
= *(int8_t *)tokcstr
.data
;
2151 tokc
.i
= *(nwchar_t
*)tokcstr
.data
;
2155 tokc
.cstr
= &tokcstr
;
2169 } else if (c
== '<') {
2187 } else if (c
== '>') {
2205 } else if (c
== '=') {
2218 } else if (c
== '=') {
2231 } else if (c
== '=') {
2244 } else if (c
== '=') {
2247 } else if (c
== '>') {
2255 PARSE2('!', '!', '=', TOK_NE
)
2256 PARSE2('=', '=', '=', TOK_EQ
)
2257 PARSE2('*', '*', '=', TOK_A_MUL
)
2258 PARSE2('%', '%', '=', TOK_A_MOD
)
2259 PARSE2('^', '^', '=', TOK_A_XOR
)
2261 /* comments or operator */
2265 p
= parse_comment(p
);
2267 } else if (c
== '/') {
2268 p
= parse_line_comment(p
);
2270 } else if (c
== '=') {
2290 case '$': /* only used in assembler */
2291 case '@': /* dito */
2296 error("unrecognized character \\x%02x", c
);
2302 #if defined(PARSE_DEBUG)
2303 printf("token = %s\n", get_tok_str(tok
, &tokc
));
2307 /* return next token without macro substitution. Can read input from
2309 static void next_nomacro_spc(void)
2315 TOK_GET(tok
, macro_ptr
, tokc
);
2316 if (tok
== TOK_LINENUM
) {
2317 file
->line_num
= tokc
.i
;
2326 static void next_nomacro(void)
2330 } while (is_space(tok
));
2333 /* substitute args in macro_str and return allocated string */
2334 static int *macro_arg_subst(Sym
**nested_list
, int *macro_str
, Sym
*args
)
2336 int *st
, last_tok
, t
, spc
;
2345 TOK_GET(t
, macro_str
, cval
);
2350 TOK_GET(t
, macro_str
, cval
);
2353 s
= sym_find2(args
, t
);
2359 TOK_GET(t
, st
, cval
);
2360 if (!check_space(t
, &spc
))
2361 cstr_cat(&cstr
, get_tok_str(t
, &cval
));
2364 cstr_ccat(&cstr
, '\0');
2366 printf("stringize: %s\n", (char *)cstr
.data
);
2370 tok_str_add2(&str
, TOK_STR
, &cval
);
2373 tok_str_add2(&str
, t
, &cval
);
2375 } else if (t
>= TOK_IDENT
) {
2376 s
= sym_find2(args
, t
);
2379 /* if '##' is present before or after, no arg substitution */
2380 if (*macro_str
== TOK_TWOSHARPS
|| last_tok
== TOK_TWOSHARPS
) {
2381 /* special case for var arg macros : ## eats the
2382 ',' if empty VA_ARGS variable. */
2383 /* XXX: test of the ',' is not 100%
2384 reliable. should fix it to avoid security
2386 if (gnu_ext
&& s
->type
.t
&&
2387 last_tok
== TOK_TWOSHARPS
&&
2388 str
.len
>= 2 && str
.str
[str
.len
- 2] == ',') {
2390 /* suppress ',' '##' */
2393 /* suppress '##' and add variable */
2401 TOK_GET(t1
, st
, cval
);
2404 tok_str_add2(&str
, t1
, &cval
);
2408 /* NOTE: the stream cannot be read when macro
2409 substituing an argument */
2410 macro_subst(&str
, nested_list
, st
, NULL
);
2413 tok_str_add(&str
, t
);
2416 tok_str_add2(&str
, t
, &cval
);
2420 tok_str_add(&str
, 0);
2424 static char const ab_month_name
[12][4] =
2426 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2427 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
2430 /* do macro substitution of current token with macro 's' and add
2431 result to (tok_str,tok_len). 'nested_list' is the list of all
2432 macros we got inside to avoid recursing. Return non zero if no
2433 substitution needs to be done */
2434 static int macro_subst_tok(TokenString
*tok_str
,
2435 Sym
**nested_list
, Sym
*s
, struct macro_level
**can_read_stream
)
2437 Sym
*args
, *sa
, *sa1
;
2438 int mstr_allocated
, parlevel
, *mstr
, t
, t1
, *p
, spc
;
2445 /* if symbol is a macro, prepare substitution */
2446 /* special macros */
2447 if (tok
== TOK___LINE__
) {
2448 snprintf(buf
, sizeof(buf
), "%d", file
->line_num
);
2452 } else if (tok
== TOK___FILE__
) {
2453 cstrval
= file
->filename
;
2455 } else if (tok
== TOK___DATE__
|| tok
== TOK___TIME__
) {
2460 tm
= localtime(&ti
);
2461 if (tok
== TOK___DATE__
) {
2462 snprintf(buf
, sizeof(buf
), "%s %2d %d",
2463 ab_month_name
[tm
->tm_mon
], tm
->tm_mday
, tm
->tm_year
+ 1900);
2465 snprintf(buf
, sizeof(buf
), "%02d:%02d:%02d",
2466 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
2473 cstr_cat(&cstr
, cstrval
);
2474 cstr_ccat(&cstr
, '\0');
2476 tok_str_add2(tok_str
, t1
, &cval
);
2481 if (s
->type
.t
== MACRO_FUNC
) {
2482 /* NOTE: we do not use next_nomacro to avoid eating the
2483 next token. XXX: find better solution */
2487 while (is_space(t
= *p
) || TOK_LINEFEED
== t
)
2489 if (t
== 0 && can_read_stream
) {
2490 /* end of macro stream: we must look at the token
2491 after in the file */
2492 struct macro_level
*ml
= *can_read_stream
;
2498 *can_read_stream
= ml
-> prev
;
2503 /* XXX: incorrect with comments */
2504 ch
= file
->buf_ptr
[0];
2505 while (is_space(ch
) || ch
== '\n')
2509 if (t
!= '(') /* no macro subst */
2512 /* argument macro */
2517 /* NOTE: empty args are allowed, except if no args */
2519 /* handle '()' case */
2520 if (!args
&& !sa
&& tok
== ')')
2523 error("macro '%s' used with too many args",
2524 get_tok_str(s
->v
, 0));
2527 /* NOTE: non zero sa->t indicates VA_ARGS */
2528 while ((parlevel
> 0 ||
2530 (tok
!= ',' || sa
->type
.t
))) &&
2534 else if (tok
== ')')
2536 if (tok
== TOK_LINEFEED
)
2538 if (!check_space(tok
, &spc
))
2539 tok_str_add2(&str
, tok
, &tokc
);
2543 tok_str_add(&str
, 0);
2544 sym_push2(&args
, sa
->v
& ~SYM_FIELD
, sa
->type
.t
, (long)str
.str
);
2547 /* special case for gcc var args: add an empty
2548 var arg argument if it is omitted */
2549 if (sa
&& sa
->type
.t
&& gnu_ext
)
2559 error("macro '%s' used with too few args",
2560 get_tok_str(s
->v
, 0));
2563 /* now subst each arg */
2564 mstr
= macro_arg_subst(nested_list
, mstr
, args
);
2569 tok_str_free((int *)sa
->c
);
2575 sym_push2(nested_list
, s
->v
, 0, 0);
2576 macro_subst(tok_str
, nested_list
, mstr
, can_read_stream
);
2577 /* pop nested defined symbol */
2579 *nested_list
= sa1
->prev
;
2587 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
2588 return the resulting string (which must be freed). */
2589 static inline int *macro_twosharps(const int *macro_str
)
2592 const int *ptr
, *saved_macro_ptr
;
2594 const char *p1
, *p2
;
2596 TokenString macro_str1
;
2599 /* we search the first '##' */
2600 for(ptr
= macro_str
;;) {
2601 TOK_GET(t
, ptr
, cval
);
2602 if (t
== TOK_TWOSHARPS
)
2604 /* nothing more to do if end of string */
2609 /* we saw '##', so we need more processing to handle it */
2611 tok_str_new(¯o_str1
);
2612 saved_macro_ptr
= macro_ptr
;
2613 /* XXX: get rid of the use of macro_ptr here */
2614 macro_ptr
= (int *)macro_str
;
2619 if (tok
== TOK_TWOSHARPS
)
2621 while (*macro_ptr
== TOK_TWOSHARPS
) {
2623 if (t
&& t
!= TOK_TWOSHARPS
) {
2624 TOK_GET(t
, macro_ptr
, cval
);
2625 /* We concatenate the two tokens if we have an
2626 identifier or a preprocessing number */
2628 p1
= get_tok_str(tok
, &tokc
);
2629 cstr_cat(&cstr
, p1
);
2630 p2
= get_tok_str(t
, &cval
);
2631 cstr_cat(&cstr
, p2
);
2632 cstr_ccat(&cstr
, '\0');
2634 if ((tok
>= TOK_IDENT
|| tok
== TOK_PPNUM
) &&
2635 (t
>= TOK_IDENT
|| t
== TOK_PPNUM
)) {
2636 if (tok
== TOK_PPNUM
) {
2637 /* if number, then create a number token */
2638 /* NOTE: no need to allocate because
2639 tok_str_add2() does it */
2640 cstr_reset(&tokcstr
);
2643 tokc
.cstr
= &tokcstr
;
2645 /* if identifier, we must do a test to
2646 validate we have a correct identifier */
2647 if (t
== TOK_PPNUM
) {
2657 if (!isnum(c
) && !isid(c
))
2661 ts
= tok_alloc(cstr
.data
, strlen(cstr
.data
));
2662 tok
= ts
->tok
; /* modify current token */
2665 const char *str
= cstr
.data
;
2666 const unsigned char *q
;
2668 /* we look for a valid token */
2669 /* XXX: do more extensive checks */
2670 if (!strcmp(str
, ">>=")) {
2672 } else if (!strcmp(str
, "<<=")) {
2674 } else if (strlen(str
) == 2) {
2675 /* search in two bytes table */
2680 if (q
[0] == str
[0] && q
[1] == str
[1])
2687 /* NOTE: because get_tok_str use a static buffer,
2690 p1
= get_tok_str(tok
, &tokc
);
2691 cstr_cat(&cstr
, p1
);
2692 cstr_ccat(&cstr
, '\0');
2693 p2
= get_tok_str(t
, &cval
);
2694 warning("pasting \"%s\" and \"%s\" does not give a valid preprocessing token", cstr
.data
, p2
);
2695 /* cannot merge tokens: just add them separately */
2696 tok_str_add2(¯o_str1
, tok
, &tokc
);
2697 /* XXX: free associated memory ? */
2704 tok_str_add2(¯o_str1
, tok
, &tokc
);
2706 macro_ptr
= (int *)saved_macro_ptr
;
2708 tok_str_add(¯o_str1
, 0);
2709 return macro_str1
.str
;
2713 /* do macro substitution of macro_str and add result to
2714 (tok_str,tok_len). 'nested_list' is the list of all macros we got
2715 inside to avoid recursing. */
2716 static void macro_subst(TokenString
*tok_str
, Sym
**nested_list
,
2717 const int *macro_str
, struct macro_level
** can_read_stream
)
2724 struct macro_level ml
;
2726 /* first scan for '##' operator handling */
2728 macro_str1
= macro_twosharps(ptr
);
2733 /* NOTE: ptr == NULL can only happen if tokens are read from
2734 file stream due to a macro function call */
2737 TOK_GET(t
, ptr
, cval
);
2742 /* if nested substitution, do nothing */
2743 if (sym_find2(*nested_list
, t
))
2746 if (can_read_stream
)
2747 ml
.prev
= *can_read_stream
, *can_read_stream
= &ml
;
2748 macro_ptr
= (int *)ptr
;
2750 ret
= macro_subst_tok(tok_str
, nested_list
, s
, can_read_stream
);
2751 ptr
= (int *)macro_ptr
;
2753 if (can_read_stream
&& *can_read_stream
== &ml
)
2754 *can_read_stream
= ml
.prev
;
2759 if (!check_space(t
, &spc
))
2760 tok_str_add2(tok_str
, t
, &cval
);
2764 tok_str_free(macro_str1
);
2767 /* return next token with macro substitution */
2768 static void next(void)
2770 Sym
*nested_list
, *s
;
2772 struct macro_level
*ml
;
2775 if (parse_flags
& PARSE_FLAG_SPACES
)
2780 /* if not reading from macro substituted string, then try
2781 to substitute macros */
2782 if (tok
>= TOK_IDENT
&&
2783 (parse_flags
& PARSE_FLAG_PREPROCESS
)) {
2784 s
= define_find(tok
);
2786 /* we have a macro: we try to substitute */
2790 if (macro_subst_tok(&str
, &nested_list
, s
, &ml
) == 0) {
2791 /* substitution done, NOTE: maybe empty */
2792 tok_str_add(&str
, 0);
2793 macro_ptr
= str
.str
;
2794 macro_ptr_allocated
= str
.str
;
2801 /* end of macro or end of unget buffer */
2802 if (unget_buffer_enabled
) {
2803 macro_ptr
= unget_saved_macro_ptr
;
2804 unget_buffer_enabled
= 0;
2806 /* end of macro string: free it */
2807 tok_str_free(macro_ptr_allocated
);
2814 /* convert preprocessor tokens into C tokens */
2815 if (tok
== TOK_PPNUM
&&
2816 (parse_flags
& PARSE_FLAG_TOK_NUM
)) {
2817 parse_number((char *)tokc
.cstr
->data
);
2821 /* push back current token and set current token to 'last_tok'. Only
2822 identifier case handled for labels. */
2823 static inline void unget_tok(int last_tok
)
2827 unget_saved_macro_ptr
= macro_ptr
;
2828 unget_buffer_enabled
= 1;
2829 q
= unget_saved_buffer
;
2832 n
= tok_ext_size(tok
) - 1;
2835 *q
= 0; /* end of token string */
2840 /* better than nothing, but needs extension to handle '-E' option
2842 static void preprocess_init(TCCState
*s1
)
2844 s1
->include_stack_ptr
= s1
->include_stack
;
2845 /* XXX: move that before to avoid having to initialize
2846 file->ifdef_stack_ptr ? */
2847 s1
->ifdef_stack_ptr
= s1
->ifdef_stack
;
2848 file
->ifdef_stack_ptr
= s1
->ifdef_stack_ptr
;
2850 /* XXX: not ANSI compliant: bound checking says error */
2852 s1
->pack_stack
[0] = 0;
2853 s1
->pack_stack_ptr
= s1
->pack_stack
;
2856 void preprocess_new()
2862 /* init isid table */
2863 for(i
=CH_EOF
;i
<256;i
++)
2864 isidnum_table
[i
-CH_EOF
] = isid(i
) || isnum(i
);
2866 /* add all tokens */
2868 memset(hash_ident
, 0, TOK_HASH_SIZE
* sizeof(TokenSym
*));
2870 tok_ident
= TOK_IDENT
;
2879 ts
= tok_alloc(p
, r
- p
- 1);
2884 /* Preprocess the current file */
2885 static int tcc_preprocess(TCCState
*s1
)
2888 BufferedFile
*file_ref
;
2889 int token_seen
, line_ref
;
2891 preprocess_init(s1
);
2892 define_start
= define_stack
;
2893 ch
= file
->buf_ptr
[0];
2894 tok_flags
= TOK_FLAG_BOL
| TOK_FLAG_BOF
;
2895 parse_flags
= PARSE_FLAG_ASM_COMMENTS
| PARSE_FLAG_PREPROCESS
|
2896 PARSE_FLAG_LINEFEED
| PARSE_FLAG_SPACES
;
2903 if (tok
== TOK_EOF
) {
2905 } else if (tok
== TOK_LINEFEED
) {
2910 } else if (!token_seen
) {
2911 int d
= file
->line_num
- line_ref
;
2912 if (file
!= file_ref
|| d
< 0 || d
>= 8)
2913 fprintf(s1
->outfile
, "# %d \"%s\"\n", file
->line_num
, file
->filename
);
2916 fputs("\n", s1
->outfile
), --d
;
2917 line_ref
= (file_ref
= file
)->line_num
;
2920 fputs(get_tok_str(tok
, &tokc
), s1
->outfile
);
2922 free_defines(define_start
);