added __builtin_constant_p() and __builtin_types_compatible_p() support
[tinycc.git] / tcc.c
blob58ae72a90450f32f1be864d2407b9689aff2f460
1 /*
2 * TCC - Tiny C Compiler
3 *
4 * Copyright (c) 2001, 2002, 2003 Fabrice Bellard
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program 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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #define _GNU_SOURCE
21 #include "config.h"
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <stdarg.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <math.h>
29 #include <unistd.h>
30 #include <signal.h>
31 #include <unistd.h>
32 #include <fcntl.h>
33 #include <setjmp.h>
34 #include <time.h>
35 #ifdef WIN32
36 #include <sys/timeb.h>
37 #endif
38 #ifndef WIN32
39 #include <sys/time.h>
40 #include <sys/ucontext.h>
41 #endif
42 #include "elf.h"
43 #include "stab.h"
44 #ifndef CONFIG_TCC_STATIC
45 #include <dlfcn.h>
46 #endif
48 #include "libtcc.h"
50 /* parser debug */
51 //#define PARSE_DEBUG
52 /* preprocessor debug */
53 //#define PP_DEBUG
54 /* include file debug */
55 //#define INC_DEBUG
57 //#define MEM_DEBUG
59 /* assembler debug */
60 //#define ASM_DEBUG
62 /* target selection */
63 //#define TCC_TARGET_I386 /* i386 code generator */
65 /* default target is I386 */
66 #if !defined(TCC_TARGET_I386)
67 #define TCC_TARGET_I386
68 #endif
70 #if !defined(WIN32) && !defined(TCC_UCLIBC)
71 #define CONFIG_TCC_BCHECK /* enable bound checking code */
72 #endif
74 /* define it to include assembler support */
75 #define CONFIG_TCC_ASM
77 /* path to find crt1.o, crti.o and crtn.o. Only needed when generating
78 executables or dlls */
79 #define CONFIG_TCC_CRT_PREFIX "/usr/lib"
81 #define INCLUDE_STACK_SIZE 32
82 #define IFDEF_STACK_SIZE 64
83 #define VSTACK_SIZE 64
84 #define STRING_MAX_SIZE 1024
86 #define TOK_HASH_SIZE 2048 /* must be a power of two */
87 #define TOK_ALLOC_INCR 512 /* must be a power of two */
88 #define TOK_STR_ALLOC_INCR_BITS 6
89 #define TOK_STR_ALLOC_INCR (1 << TOK_STR_ALLOC_INCR_BITS)
90 #define TOK_MAX_SIZE 4 /* token max size in int unit when stored in string */
92 /* token symbol management */
93 typedef struct TokenSym {
94 struct TokenSym *hash_next;
95 struct Sym *sym_define; /* direct pointer to define */
96 struct Sym *sym_label; /* direct pointer to label */
97 struct Sym *sym_struct; /* direct pointer to structure */
98 struct Sym *sym_identifier; /* direct pointer to identifier */
99 int tok; /* token number */
100 int len;
101 char str[1];
102 } TokenSym;
104 typedef struct CString {
105 int size; /* size in bytes */
106 void *data; /* either 'char *' or 'int *' */
107 int size_allocated;
108 void *data_allocated; /* if non NULL, data has been malloced */
109 } CString;
111 /* type definition */
112 typedef struct CType {
113 int t;
114 struct Sym *ref;
115 } CType;
117 /* constant value */
118 typedef union CValue {
119 long double ld;
120 double d;
121 float f;
122 int i;
123 unsigned int ui;
124 unsigned int ul; /* address (should be unsigned long on 64 bit cpu) */
125 long long ll;
126 unsigned long long ull;
127 struct CString *cstr;
128 void *ptr;
129 int tab[1];
130 } CValue;
132 /* value on stack */
133 typedef struct SValue {
134 CType type; /* type */
135 unsigned short r; /* register + flags */
136 unsigned short r2; /* second register, used for 'long long'
137 type. If not used, set to VT_CONST */
138 CValue c; /* constant, if VT_CONST */
139 struct Sym *sym; /* symbol, if (VT_SYM | VT_CONST) */
140 } SValue;
142 /* symbol management */
143 typedef struct Sym {
144 int v; /* symbol token */
145 int r; /* associated register */
146 int c; /* associated number */
147 CType type; /* associated type */
148 struct Sym *next; /* next related symbol */
149 struct Sym *prev; /* prev symbol in stack */
150 struct Sym *prev_tok; /* previous symbol for this token */
151 } Sym;
153 /* section definition */
154 /* XXX: use directly ELF structure for parameters ? */
155 /* special flag to indicate that the section should not be linked to
156 the other ones */
157 #define SHF_PRIVATE 0x80000000
159 typedef struct Section {
160 unsigned long data_offset; /* current data offset */
161 unsigned char *data; /* section data */
162 unsigned long data_allocated; /* used for realloc() handling */
163 int sh_name; /* elf section name (only used during output) */
164 int sh_num; /* elf section number */
165 int sh_type; /* elf section type */
166 int sh_flags; /* elf section flags */
167 int sh_info; /* elf section info */
168 int sh_addralign; /* elf section alignment */
169 int sh_entsize; /* elf entry size */
170 unsigned long sh_size; /* section size (only used during output) */
171 unsigned long sh_addr; /* address at which the section is relocated */
172 unsigned long sh_offset; /* address at which the section is relocated */
173 int nb_hashed_syms; /* used to resize the hash table */
174 struct Section *link; /* link to another section */
175 struct Section *reloc; /* corresponding section for relocation, if any */
176 struct Section *hash; /* hash table for symbols */
177 struct Section *next;
178 char name[64]; /* section name */
179 } Section;
181 typedef struct DLLReference {
182 int level;
183 char name[1];
184 } DLLReference;
186 /* GNUC attribute definition */
187 typedef struct AttributeDef {
188 int aligned;
189 Section *section;
190 unsigned char func_call; /* FUNC_CDECL or FUNC_STDCALL */
191 } AttributeDef;
193 #define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */
194 #define SYM_FIELD 0x20000000 /* struct/union field symbol space */
195 #define SYM_FIRST_ANOM (1 << (31 - VT_STRUCT_SHIFT)) /* first anonymous sym */
197 /* stored in 'Sym.c' field */
198 #define FUNC_NEW 1 /* ansi function prototype */
199 #define FUNC_OLD 2 /* old function prototype */
200 #define FUNC_ELLIPSIS 3 /* ansi function prototype with ... */
202 /* stored in 'Sym.r' field */
203 #define FUNC_CDECL 0 /* standard c call */
204 #define FUNC_STDCALL 1 /* pascal c call */
206 /* field 'Sym.t' for macros */
207 #define MACRO_OBJ 0 /* object like macro */
208 #define MACRO_FUNC 1 /* function like macro */
210 /* field 'Sym.r' for C labels */
211 #define LABEL_DEFINED 0 /* label is defined */
212 #define LABEL_FORWARD 1 /* label is forward defined */
213 #define LABEL_DECLARED 2 /* label is declared but never used */
215 /* type_decl() types */
216 #define TYPE_ABSTRACT 1 /* type without variable */
217 #define TYPE_DIRECT 2 /* type with variable */
219 #define IO_BUF_SIZE 8192
221 typedef struct BufferedFile {
222 uint8_t *buf_ptr;
223 uint8_t *buf_end;
224 int fd;
225 int line_num; /* current line number - here to simplify code */
226 int ifndef_macro; /* #ifndef macro / #endif search */
227 int ifndef_macro_saved; /* saved ifndef_macro */
228 int *ifdef_stack_ptr; /* ifdef_stack value at the start of the file */
229 char inc_type; /* type of include */
230 char inc_filename[512]; /* filename specified by the user */
231 char filename[1024]; /* current filename - here to simplify code */
232 unsigned char buffer[IO_BUF_SIZE + 1]; /* extra size for CH_EOB char */
233 } BufferedFile;
235 #define CH_EOB '\\' /* end of buffer or '\0' char in file */
236 #define CH_EOF (-1) /* end of file */
238 /* parsing state (used to save parser state to reparse part of the
239 source several times) */
240 typedef struct ParseState {
241 int *macro_ptr;
242 int line_num;
243 int tok;
244 CValue tokc;
245 } ParseState;
247 /* used to record tokens */
248 typedef struct TokenString {
249 int *str;
250 int len;
251 int allocated_len;
252 int last_line_num;
253 } TokenString;
255 /* include file cache, used to find files faster and also to eliminate
256 inclusion if the include file is protected by #ifndef ... #endif */
257 typedef struct CachedInclude {
258 int ifndef_macro;
259 char type; /* '"' or '>' to give include type */
260 char filename[1]; /* path specified in #include */
261 } CachedInclude;
263 /* parser */
264 static struct BufferedFile *file;
265 static int ch, tok;
266 static CValue tokc;
267 static CString tokcstr; /* current parsed string, if any */
268 /* additional informations about token */
269 static int tok_flags;
270 #define TOK_FLAG_BOL 0x0001 /* beginning of line before */
271 #define TOK_FLAG_BOF 0x0002 /* beginning of file before */
272 #define TOK_FLAG_ENDIF 0x0004 /* a endif was found matching starting #ifdef */
274 static int *macro_ptr, *macro_ptr_allocated;
275 static int *unget_saved_macro_ptr;
276 static int unget_saved_buffer[TOK_MAX_SIZE + 1];
277 static int unget_buffer_enabled;
278 static int parse_flags;
279 #define PARSE_FLAG_PREPROCESS 0x0001 /* activate preprocessing */
280 #define PARSE_FLAG_TOK_NUM 0x0002 /* return numbers instead of TOK_PPNUM */
281 #define PARSE_FLAG_LINEFEED 0x0004 /* line feed is returned as a
282 token. line feed is also
283 returned at eof */
285 static Section *text_section, *data_section, *bss_section; /* predefined sections */
286 static Section *cur_text_section; /* current section where function code is
287 generated */
288 /* bound check related sections */
289 static Section *bounds_section; /* contains global data bound description */
290 static Section *lbounds_section; /* contains local data bound description */
291 /* symbol sections */
292 static Section *symtab_section, *strtab_section;
294 /* debug sections */
295 static Section *stab_section, *stabstr_section;
297 /* loc : local variable index
298 ind : output code index
299 rsym: return symbol
300 anon_sym: anonymous symbol index
302 static int rsym, anon_sym, ind, loc;
303 /* expression generation modifiers */
304 static int const_wanted; /* true if constant wanted */
305 static int nocode_wanted; /* true if no code generation wanted for an expression */
306 static int global_expr; /* true if compound literals must be allocated
307 globally (used during initializers parsing */
308 static CType func_vt; /* current function return type (used by return
309 instruction) */
310 static int func_vc;
311 static int last_line_num, last_ind, func_ind; /* debug last line number and pc */
312 static int tok_ident;
313 static TokenSym **table_ident;
314 static TokenSym *hash_ident[TOK_HASH_SIZE];
315 static char token_buf[STRING_MAX_SIZE + 1];
316 static char *funcname;
317 static Sym *global_stack, *local_stack;
318 static Sym *define_stack;
319 static Sym *global_label_stack, *local_label_stack;
321 static SValue vstack[VSTACK_SIZE], *vtop;
322 /* some predefined types */
323 static CType char_pointer_type, func_old_type, int_type;
324 /* true if isid(c) || isnum(c) */
325 static unsigned char isidnum_table[256];
327 /* compile with debug symbol (and use them if error during execution) */
328 static int do_debug = 0;
330 /* compile with built-in memory and bounds checker */
331 static int do_bounds_check = 0;
333 /* display benchmark infos */
334 #if !defined(LIBTCC)
335 static int do_bench = 0;
336 #endif
337 static int total_lines;
338 static int total_bytes;
340 /* use GNU C extensions */
341 static int gnu_ext = 1;
343 /* use Tiny C extensions */
344 static int tcc_ext = 1;
346 /* max number of callers shown if error */
347 static int num_callers = 6;
348 static const char **rt_bound_error_msg;
350 /* XXX: get rid of this ASAP */
351 static struct TCCState *tcc_state;
353 /* give the path of the tcc libraries */
354 static const char *tcc_lib_path = CONFIG_TCC_LIBDIR "/tcc";
356 struct TCCState {
357 int output_type;
359 BufferedFile **include_stack_ptr;
360 int *ifdef_stack_ptr;
362 /* include file handling */
363 char **include_paths;
364 int nb_include_paths;
365 char **sysinclude_paths;
366 int nb_sysinclude_paths;
367 CachedInclude **cached_includes;
368 int nb_cached_includes;
370 char **library_paths;
371 int nb_library_paths;
373 /* array of all loaded dlls (including those referenced by loaded
374 dlls) */
375 DLLReference **loaded_dlls;
376 int nb_loaded_dlls;
378 /* sections */
379 Section **sections;
380 int nb_sections; /* number of sections, including first dummy section */
382 /* got handling */
383 Section *got;
384 Section *plt;
385 unsigned long *got_offsets;
386 int nb_got_offsets;
387 /* give the correspondance from symtab indexes to dynsym indexes */
388 int *symtab_to_dynsym;
390 /* temporary dynamic symbol sections (for dll loading) */
391 Section *dynsymtab_section;
392 /* exported dynamic symbol section */
393 Section *dynsym;
395 /* if true, no standard headers are added */
396 int nostdinc;
398 /* if true, static linking is performed */
399 int static_link;
401 /* warning switches */
402 int warn_write_strings;
403 int warn_unsupported;
404 int warn_error;
406 /* error handling */
407 void *error_opaque;
408 void (*error_func)(void *opaque, const char *msg);
409 int error_set_jmp_enabled;
410 jmp_buf error_jmp_buf;
411 int nb_errors;
413 /* tiny assembler state */
414 Sym *asm_labels;
416 /* see include_stack_ptr */
417 BufferedFile *include_stack[INCLUDE_STACK_SIZE];
419 /* see ifdef_stack_ptr */
420 int ifdef_stack[IFDEF_STACK_SIZE];
423 /* The current value can be: */
424 #define VT_VALMASK 0x00ff
425 #define VT_CONST 0x00f0 /* constant in vc
426 (must be first non register value) */
427 #define VT_LLOCAL 0x00f1 /* lvalue, offset on stack */
428 #define VT_LOCAL 0x00f2 /* offset on stack */
429 #define VT_CMP 0x00f3 /* the value is stored in processor flags (in vc) */
430 #define VT_JMP 0x00f4 /* value is the consequence of jmp true (even) */
431 #define VT_JMPI 0x00f5 /* value is the consequence of jmp false (odd) */
432 #define VT_LVAL 0x0100 /* var is an lvalue */
433 #define VT_SYM 0x0200 /* a symbol value is added */
434 #define VT_MUSTCAST 0x0400 /* value must be casted to be correct (used for
435 char/short stored in integer registers) */
436 #define VT_MUSTBOUND 0x0800 /* bound checking must be done before
437 dereferencing value */
438 #define VT_BOUNDED 0x8000 /* value is bounded. The address of the
439 bounding function call point is in vc */
440 #define VT_LVAL_BYTE 0x1000 /* lvalue is a byte */
441 #define VT_LVAL_SHORT 0x2000 /* lvalue is a short */
442 #define VT_LVAL_UNSIGNED 0x4000 /* lvalue is unsigned */
443 #define VT_LVAL_TYPE (VT_LVAL_BYTE | VT_LVAL_SHORT | VT_LVAL_UNSIGNED)
445 /* types */
446 #define VT_INT 0 /* integer type */
447 #define VT_BYTE 1 /* signed byte type */
448 #define VT_SHORT 2 /* short type */
449 #define VT_VOID 3 /* void type */
450 #define VT_PTR 4 /* pointer */
451 #define VT_ENUM 5 /* enum definition */
452 #define VT_FUNC 6 /* function type */
453 #define VT_STRUCT 7 /* struct/union definition */
454 #define VT_FLOAT 8 /* IEEE float */
455 #define VT_DOUBLE 9 /* IEEE double */
456 #define VT_LDOUBLE 10 /* IEEE long double */
457 #define VT_BOOL 11 /* ISOC99 boolean type */
458 #define VT_LLONG 12 /* 64 bit integer */
459 #define VT_LONG 13 /* long integer (NEVER USED as type, only
460 during parsing) */
461 #define VT_BTYPE 0x000f /* mask for basic type */
462 #define VT_UNSIGNED 0x0010 /* unsigned type */
463 #define VT_ARRAY 0x0020 /* array type (also has VT_PTR) */
464 #define VT_BITFIELD 0x0040 /* bitfield modifier */
465 #define VT_CONSTANT 0x0800 /* const modifier */
466 #define VT_VOLATILE 0x1000 /* volatile modifier */
468 /* storage */
469 #define VT_EXTERN 0x00000080 /* extern definition */
470 #define VT_STATIC 0x00000100 /* static variable */
471 #define VT_TYPEDEF 0x00000200 /* typedef definition */
472 #define VT_INLINE 0x00000400 /* inline definition */
474 #define VT_STRUCT_SHIFT 16 /* shift for bitfield shift values */
476 /* type mask (except storage) */
477 #define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE)
478 #define VT_TYPE (~(VT_STORAGE))
480 /* token values */
482 /* warning: the following compare tokens depend on i386 asm code */
483 #define TOK_ULT 0x92
484 #define TOK_UGE 0x93
485 #define TOK_EQ 0x94
486 #define TOK_NE 0x95
487 #define TOK_ULE 0x96
488 #define TOK_UGT 0x97
489 #define TOK_LT 0x9c
490 #define TOK_GE 0x9d
491 #define TOK_LE 0x9e
492 #define TOK_GT 0x9f
494 #define TOK_LAND 0xa0
495 #define TOK_LOR 0xa1
497 #define TOK_DEC 0xa2
498 #define TOK_MID 0xa3 /* inc/dec, to void constant */
499 #define TOK_INC 0xa4
500 #define TOK_UDIV 0xb0 /* unsigned division */
501 #define TOK_UMOD 0xb1 /* unsigned modulo */
502 #define TOK_PDIV 0xb2 /* fast division with undefined rounding for pointers */
503 #define TOK_CINT 0xb3 /* number in tokc */
504 #define TOK_CCHAR 0xb4 /* char constant in tokc */
505 #define TOK_STR 0xb5 /* pointer to string in tokc */
506 #define TOK_TWOSHARPS 0xb6 /* ## preprocessing token */
507 #define TOK_LCHAR 0xb7
508 #define TOK_LSTR 0xb8
509 #define TOK_CFLOAT 0xb9 /* float constant */
510 #define TOK_LINENUM 0xba /* line number info */
511 #define TOK_CDOUBLE 0xc0 /* double constant */
512 #define TOK_CLDOUBLE 0xc1 /* long double constant */
513 #define TOK_UMULL 0xc2 /* unsigned 32x32 -> 64 mul */
514 #define TOK_ADDC1 0xc3 /* add with carry generation */
515 #define TOK_ADDC2 0xc4 /* add with carry use */
516 #define TOK_SUBC1 0xc5 /* add with carry generation */
517 #define TOK_SUBC2 0xc6 /* add with carry use */
518 #define TOK_CUINT 0xc8 /* unsigned int constant */
519 #define TOK_CLLONG 0xc9 /* long long constant */
520 #define TOK_CULLONG 0xca /* unsigned long long constant */
521 #define TOK_ARROW 0xcb
522 #define TOK_DOTS 0xcc /* three dots */
523 #define TOK_SHR 0xcd /* unsigned shift right */
524 #define TOK_PPNUM 0xce /* preprocessor number */
526 #define TOK_SHL 0x01 /* shift left */
527 #define TOK_SAR 0x02 /* signed shift right */
529 /* assignement operators : normal operator or 0x80 */
530 #define TOK_A_MOD 0xa5
531 #define TOK_A_AND 0xa6
532 #define TOK_A_MUL 0xaa
533 #define TOK_A_ADD 0xab
534 #define TOK_A_SUB 0xad
535 #define TOK_A_DIV 0xaf
536 #define TOK_A_XOR 0xde
537 #define TOK_A_OR 0xfc
538 #define TOK_A_SHL 0x81
539 #define TOK_A_SAR 0x82
541 #ifndef offsetof
542 #define offsetof(type, field) ((size_t) &((type *)0)->field)
543 #endif
545 #ifndef countof
546 #define countof(tab) (sizeof(tab) / sizeof((tab)[0]))
547 #endif
549 /* WARNING: the content of this string encodes token numbers */
550 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";
552 #define TOK_EOF (-1) /* end of file */
553 #define TOK_LINEFEED 10 /* line feed */
555 /* all identificators and strings have token above that */
556 #define TOK_IDENT 256
558 /* only used for i386 asm opcodes definitions */
559 #define DEF_ASM(x) DEF(TOK_ASM_ ## x, #x)
561 #define DEF_BWL(x) \
562 DEF(TOK_ASM_ ## x ## b, #x "b") \
563 DEF(TOK_ASM_ ## x ## w, #x "w") \
564 DEF(TOK_ASM_ ## x ## l, #x "l") \
565 DEF(TOK_ASM_ ## x, #x)
567 #define DEF_WL(x) \
568 DEF(TOK_ASM_ ## x ## w, #x "w") \
569 DEF(TOK_ASM_ ## x ## l, #x "l") \
570 DEF(TOK_ASM_ ## x, #x)
572 #define DEF_FP1(x) \
573 DEF(TOK_ASM_ ## f ## x ## s, "f" #x "s") \
574 DEF(TOK_ASM_ ## fi ## x ## l, "fi" #x "l") \
575 DEF(TOK_ASM_ ## f ## x ## l, "f" #x "l") \
576 DEF(TOK_ASM_ ## fi ## x ## s, "fi" #x "s")
578 #define DEF_FP(x) \
579 DEF(TOK_ASM_ ## f ## x, "f" #x ) \
580 DEF(TOK_ASM_ ## f ## x ## p, "f" #x "p") \
581 DEF_FP1(x)
583 #define DEF_ASMTEST(x) \
584 DEF_ASM(x ## o) \
585 DEF_ASM(x ## no) \
586 DEF_ASM(x ## b) \
587 DEF_ASM(x ## c) \
588 DEF_ASM(x ## nae) \
589 DEF_ASM(x ## nb) \
590 DEF_ASM(x ## nc) \
591 DEF_ASM(x ## ae) \
592 DEF_ASM(x ## e) \
593 DEF_ASM(x ## z) \
594 DEF_ASM(x ## ne) \
595 DEF_ASM(x ## nz) \
596 DEF_ASM(x ## be) \
597 DEF_ASM(x ## na) \
598 DEF_ASM(x ## nbe) \
599 DEF_ASM(x ## a) \
600 DEF_ASM(x ## s) \
601 DEF_ASM(x ## ns) \
602 DEF_ASM(x ## p) \
603 DEF_ASM(x ## pe) \
604 DEF_ASM(x ## np) \
605 DEF_ASM(x ## po) \
606 DEF_ASM(x ## l) \
607 DEF_ASM(x ## nge) \
608 DEF_ASM(x ## nl) \
609 DEF_ASM(x ## ge) \
610 DEF_ASM(x ## le) \
611 DEF_ASM(x ## ng) \
612 DEF_ASM(x ## nle) \
613 DEF_ASM(x ## g)
615 #define TOK_ASM_int TOK_INT
617 enum {
618 TOK_LAST = TOK_IDENT - 1,
619 #define DEF(id, str) id,
620 #include "tcctok.h"
621 #undef DEF
624 static const char tcc_keywords[] =
625 #define DEF(id, str) str "\0"
626 #include "tcctok.h"
627 #undef DEF
630 #define TOK_UIDENT TOK_DEFINE
632 #ifdef WIN32
633 #define snprintf _snprintf
634 #define vsnprintf _vsnprintf
635 #endif
637 #if defined(WIN32) || defined(TCC_UCLIBC) || defined(__FreeBSD__)
638 /* currently incorrect */
639 long double strtold(const char *nptr, char **endptr)
641 return (long double)strtod(nptr, endptr);
643 float strtof(const char *nptr, char **endptr)
645 return (float)strtod(nptr, endptr);
647 #else
648 /* XXX: need to define this to use them in non ISOC99 context */
649 extern float strtof (const char *__nptr, char **__endptr);
650 extern long double strtold (const char *__nptr, char **__endptr);
651 #endif
653 static char *pstrcpy(char *buf, int buf_size, const char *s);
654 static char *pstrcat(char *buf, int buf_size, const char *s);
656 static void next(void);
657 static void next_nomacro(void);
658 static void parse_expr_type(CType *type);
659 static void expr_type(CType *type);
660 static void unary_type(CType *type);
661 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
662 int case_reg, int is_expr);
663 static int expr_const(void);
664 static void expr_eq(void);
665 static void gexpr(void);
666 static void decl(int l);
667 static void decl_initializer(CType *type, Section *sec, unsigned long c,
668 int first, int size_only);
669 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
670 int has_init, int v, int scope);
671 int gv(int rc);
672 void gv2(int rc1, int rc2);
673 void move_reg(int r, int s);
674 void save_regs(int n);
675 void save_reg(int r);
676 void vpop(void);
677 void vswap(void);
678 void vdup(void);
679 int get_reg(int rc);
681 static void macro_subst(TokenString *tok_str,
682 Sym **nested_list, const int *macro_str);
683 int save_reg_forced(int r);
684 void gen_op(int op);
685 void force_charshort_cast(int t);
686 static void gen_cast(CType *type);
687 void vstore(void);
688 static Sym *sym_find(int v);
689 static Sym *sym_push(int v, CType *type, int r, int c);
691 /* type handling */
692 static int type_size(CType *type, int *a);
693 static inline CType *pointed_type(CType *type);
694 static int pointed_size(CType *type);
695 static int lvalue_type(int t);
696 static int parse_btype(CType *type, AttributeDef *ad);
697 static void type_decl(CType *type, AttributeDef *ad, int *v, int td);
698 static int is_compatible_types(CType *type1, CType *type2);
700 void error(const char *fmt, ...);
701 void vpushi(int v);
702 void vset(CType *type, int r, int v);
703 void type_to_str(char *buf, int buf_size,
704 CType *type, const char *varstr);
705 char *get_tok_str(int v, CValue *cv);
706 static Sym *get_sym_ref(CType *type, Section *sec,
707 unsigned long offset, unsigned long size);
708 static Sym *external_global_sym(int v, CType *type, int r);
710 /* section generation */
711 static void section_realloc(Section *sec, unsigned long new_size);
712 static void *section_ptr_add(Section *sec, unsigned long size);
713 static void put_extern_sym(Sym *sym, Section *section,
714 unsigned long value, unsigned long size);
715 static void greloc(Section *s, Sym *sym, unsigned long addr, int type);
716 static int put_elf_str(Section *s, const char *sym);
717 static int put_elf_sym(Section *s,
718 unsigned long value, unsigned long size,
719 int info, int other, int shndx, const char *name);
720 static int add_elf_sym(Section *s, unsigned long value, unsigned long size,
721 int info, int sh_num, const char *name);
722 static void put_elf_reloc(Section *symtab, Section *s, unsigned long offset,
723 int type, int symbol);
724 static void put_stabs(const char *str, int type, int other, int desc,
725 unsigned long value);
726 static void put_stabs_r(const char *str, int type, int other, int desc,
727 unsigned long value, Section *sec, int sym_index);
728 static void put_stabn(int type, int other, int desc, int value);
729 static void put_stabd(int type, int other, int desc);
730 static int tcc_add_dll(TCCState *s, const char *filename, int flags);
732 #define AFF_PRINT_ERROR 0x0001 /* print error if file not found */
733 #define AFF_REFERENCED_DLL 0x0002 /* load a referenced dll from another dll */
734 static int tcc_add_file_internal(TCCState *s, const char *filename, int flags);
736 /* tccasm.c */
738 #ifdef CONFIG_TCC_ASM
740 typedef struct ExprValue {
741 uint32_t v;
742 Sym *sym;
743 } ExprValue;
745 #define MAX_ASM_OPERANDS 30
747 typedef struct ASMOperand {
748 int id; /* GCC 3 optionnal identifier (0 if number only supported */
749 char *constraint;
750 char asm_str[16]; /* computed asm string for operand */
751 SValue *vt; /* C value of the expression */
752 int ref_index; /* if >= 0, gives reference to a output constraint */
753 int priority; /* priority, used to assign registers */
754 int reg; /* if >= 0, register number used for this operand */
755 int is_llong; /* true if double register value */
756 } ASMOperand;
758 static void asm_expr(TCCState *s1, ExprValue *pe);
759 static int asm_int_expr(TCCState *s1);
760 static int find_constraint(ASMOperand *operands, int nb_operands,
761 const char *name, const char **pp);
763 static int tcc_assemble(TCCState *s1, int do_preprocess);
765 #endif
767 static void asm_instr(void);
769 /* true if float/double/long double type */
770 static inline int is_float(int t)
772 int bt;
773 bt = t & VT_BTYPE;
774 return bt == VT_LDOUBLE || bt == VT_DOUBLE || bt == VT_FLOAT;
777 #ifdef TCC_TARGET_I386
778 #include "i386-gen.c"
779 #endif
781 #ifdef CONFIG_TCC_STATIC
783 #define RTLD_LAZY 0x001
784 #define RTLD_NOW 0x002
785 #define RTLD_GLOBAL 0x100
786 #define RTLD_DEFAULT NULL
788 /* dummy function for profiling */
789 void *dlopen(const char *filename, int flag)
791 return NULL;
794 const char *dlerror(void)
796 return "error";
799 typedef struct TCCSyms {
800 char *str;
801 void *ptr;
802 } TCCSyms;
804 #define TCCSYM(a) { #a, &a, },
806 /* add the symbol you want here if no dynamic linking is done */
807 static TCCSyms tcc_syms[] = {
808 TCCSYM(printf)
809 TCCSYM(fprintf)
810 TCCSYM(fopen)
811 TCCSYM(fclose)
812 { NULL, NULL },
815 void *dlsym(void *handle, const char *symbol)
817 TCCSyms *p;
818 p = tcc_syms;
819 while (p->str != NULL) {
820 if (!strcmp(p->str, symbol))
821 return p->ptr;
822 p++;
824 return NULL;
827 #endif
829 /********************************************************/
831 /* we use our own 'finite' function to avoid potential problems with
832 non standard math libs */
833 /* XXX: endianness dependent */
834 int ieee_finite(double d)
836 int *p = (int *)&d;
837 return ((unsigned)((p[1] | 0x800fffff) + 1)) >> 31;
840 /* copy a string and truncate it. */
841 static char *pstrcpy(char *buf, int buf_size, const char *s)
843 char *q, *q_end;
844 int c;
846 if (buf_size > 0) {
847 q = buf;
848 q_end = buf + buf_size - 1;
849 while (q < q_end) {
850 c = *s++;
851 if (c == '\0')
852 break;
853 *q++ = c;
855 *q = '\0';
857 return buf;
860 /* strcat and truncate. */
861 static char *pstrcat(char *buf, int buf_size, const char *s)
863 int len;
864 len = strlen(buf);
865 if (len < buf_size)
866 pstrcpy(buf + len, buf_size - len, s);
867 return buf;
870 /* memory management */
871 #ifdef MEM_DEBUG
872 int mem_cur_size;
873 int mem_max_size;
874 #endif
876 static inline void tcc_free(void *ptr)
878 #ifdef MEM_DEBUG
879 mem_cur_size -= malloc_usable_size(ptr);
880 #endif
881 free(ptr);
884 static void *tcc_malloc(unsigned long size)
886 void *ptr;
887 ptr = malloc(size);
888 if (!ptr && size)
889 error("memory full");
890 #ifdef MEM_DEBUG
891 mem_cur_size += malloc_usable_size(ptr);
892 if (mem_cur_size > mem_max_size)
893 mem_max_size = mem_cur_size;
894 #endif
895 return ptr;
898 static void *tcc_mallocz(unsigned long size)
900 void *ptr;
901 ptr = tcc_malloc(size);
902 memset(ptr, 0, size);
903 return ptr;
906 static inline void *tcc_realloc(void *ptr, unsigned long size)
908 void *ptr1;
909 #ifdef MEM_DEBUG
910 mem_cur_size -= malloc_usable_size(ptr);
911 #endif
912 ptr1 = realloc(ptr, size);
913 #ifdef MEM_DEBUG
914 /* NOTE: count not correct if alloc error, but not critical */
915 mem_cur_size += malloc_usable_size(ptr1);
916 if (mem_cur_size > mem_max_size)
917 mem_max_size = mem_cur_size;
918 #endif
919 return ptr1;
922 static char *tcc_strdup(const char *str)
924 char *ptr;
925 ptr = tcc_malloc(strlen(str) + 1);
926 strcpy(ptr, str);
927 return ptr;
930 #define free(p) use_tcc_free(p)
931 #define malloc(s) use_tcc_malloc(s)
932 #define realloc(p, s) use_tcc_realloc(p, s)
934 static void dynarray_add(void ***ptab, int *nb_ptr, void *data)
936 int nb, nb_alloc;
937 void **pp;
939 nb = *nb_ptr;
940 pp = *ptab;
941 /* every power of two we double array size */
942 if ((nb & (nb - 1)) == 0) {
943 if (!nb)
944 nb_alloc = 1;
945 else
946 nb_alloc = nb * 2;
947 pp = tcc_realloc(pp, nb_alloc * sizeof(void *));
948 if (!pp)
949 error("memory full");
950 *ptab = pp;
952 pp[nb++] = data;
953 *nb_ptr = nb;
956 Section *new_section(TCCState *s1, const char *name, int sh_type, int sh_flags)
958 Section *sec;
960 sec = tcc_mallocz(sizeof(Section));
961 pstrcpy(sec->name, sizeof(sec->name), name);
962 sec->sh_type = sh_type;
963 sec->sh_flags = sh_flags;
964 switch(sh_type) {
965 case SHT_HASH:
966 case SHT_REL:
967 case SHT_DYNSYM:
968 case SHT_SYMTAB:
969 case SHT_DYNAMIC:
970 sec->sh_addralign = 4;
971 break;
972 case SHT_STRTAB:
973 sec->sh_addralign = 1;
974 break;
975 default:
976 sec->sh_addralign = 32; /* default conservative alignment */
977 break;
980 /* only add section if not private */
981 if (!(sh_flags & SHF_PRIVATE)) {
982 sec->sh_num = s1->nb_sections;
983 dynarray_add((void ***)&s1->sections, &s1->nb_sections, sec);
985 return sec;
988 static void free_section(Section *s)
990 tcc_free(s->data);
991 tcc_free(s);
994 /* realloc section and set its content to zero */
995 static void section_realloc(Section *sec, unsigned long new_size)
997 unsigned long size;
998 unsigned char *data;
1000 size = sec->data_allocated;
1001 if (size == 0)
1002 size = 1;
1003 while (size < new_size)
1004 size = size * 2;
1005 data = tcc_realloc(sec->data, size);
1006 if (!data)
1007 error("memory full");
1008 memset(data + sec->data_allocated, 0, size - sec->data_allocated);
1009 sec->data = data;
1010 sec->data_allocated = size;
1013 /* reserve at least 'size' bytes in section 'sec' from
1014 sec->data_offset. */
1015 static void *section_ptr_add(Section *sec, unsigned long size)
1017 unsigned long offset, offset1;
1019 offset = sec->data_offset;
1020 offset1 = offset + size;
1021 if (offset1 > sec->data_allocated)
1022 section_realloc(sec, offset1);
1023 sec->data_offset = offset1;
1024 return sec->data + offset;
1027 /* return a reference to a section, and create it if it does not
1028 exists */
1029 Section *find_section(TCCState *s1, const char *name)
1031 Section *sec;
1032 int i;
1033 for(i = 1; i < s1->nb_sections; i++) {
1034 sec = s1->sections[i];
1035 if (!strcmp(name, sec->name))
1036 return sec;
1038 /* sections are created as PROGBITS */
1039 return new_section(s1, name, SHT_PROGBITS, SHF_ALLOC);
1042 /* update sym->c so that it points to an external symbol in section
1043 'section' with value 'value' */
1044 static void put_extern_sym(Sym *sym, Section *section,
1045 unsigned long value, unsigned long size)
1047 int sym_type, sym_bind, sh_num, info;
1048 Elf32_Sym *esym;
1049 const char *name;
1051 if (section)
1052 sh_num = section->sh_num;
1053 else
1054 sh_num = SHN_UNDEF;
1055 if (!sym->c) {
1056 if ((sym->type.t & VT_BTYPE) == VT_FUNC)
1057 sym_type = STT_FUNC;
1058 else
1059 sym_type = STT_OBJECT;
1060 if (sym->type.t & VT_STATIC)
1061 sym_bind = STB_LOCAL;
1062 else
1063 sym_bind = STB_GLOBAL;
1065 name = get_tok_str(sym->v, NULL);
1066 #ifdef CONFIG_TCC_BCHECK
1067 if (do_bounds_check) {
1068 char buf[32];
1070 /* XXX: avoid doing that for statics ? */
1071 /* if bound checking is activated, we change some function
1072 names by adding the "__bound" prefix */
1073 switch(sym->v) {
1074 #if 0
1075 /* XXX: we rely only on malloc hooks */
1076 case TOK_malloc:
1077 case TOK_free:
1078 case TOK_realloc:
1079 case TOK_memalign:
1080 case TOK_calloc:
1081 #endif
1082 case TOK_memcpy:
1083 case TOK_memmove:
1084 case TOK_memset:
1085 case TOK_strlen:
1086 case TOK_strcpy:
1087 strcpy(buf, "__bound_");
1088 strcat(buf, name);
1089 name = buf;
1090 break;
1093 #endif
1094 info = ELF32_ST_INFO(sym_bind, sym_type);
1095 sym->c = add_elf_sym(symtab_section, value, size, info, sh_num, name);
1096 } else {
1097 esym = &((Elf32_Sym *)symtab_section->data)[sym->c];
1098 esym->st_value = value;
1099 esym->st_size = size;
1100 esym->st_shndx = sh_num;
1104 /* add a new relocation entry to symbol 'sym' in section 's' */
1105 static void greloc(Section *s, Sym *sym, unsigned long offset, int type)
1107 if (!sym->c)
1108 put_extern_sym(sym, NULL, 0, 0);
1109 /* now we can add ELF relocation info */
1110 put_elf_reloc(symtab_section, s, offset, type, sym->c);
1113 static inline int isid(int c)
1115 return (c >= 'a' && c <= 'z') ||
1116 (c >= 'A' && c <= 'Z') ||
1117 c == '_';
1120 static inline int isnum(int c)
1122 return c >= '0' && c <= '9';
1125 static inline int isoct(int c)
1127 return c >= '0' && c <= '7';
1130 static inline int toup(int c)
1132 if (c >= 'a' && c <= 'z')
1133 return c - 'a' + 'A';
1134 else
1135 return c;
1138 static void strcat_vprintf(char *buf, int buf_size, const char *fmt, va_list ap)
1140 int len;
1141 len = strlen(buf);
1142 vsnprintf(buf + len, buf_size - len, fmt, ap);
1145 static void strcat_printf(char *buf, int buf_size, const char *fmt, ...)
1147 va_list ap;
1148 va_start(ap, fmt);
1149 strcat_vprintf(buf, buf_size, fmt, ap);
1150 va_end(ap);
1153 void error1(TCCState *s1, int is_warning, const char *fmt, va_list ap)
1155 char buf[2048];
1156 BufferedFile **f;
1158 buf[0] = '\0';
1159 if (file) {
1160 for(f = s1->include_stack; f < s1->include_stack_ptr; f++)
1161 strcat_printf(buf, sizeof(buf), "In file included from %s:%d:\n",
1162 (*f)->filename, (*f)->line_num);
1163 if (file->line_num > 0) {
1164 strcat_printf(buf, sizeof(buf),
1165 "%s:%d: ", file->filename, file->line_num);
1166 } else {
1167 strcat_printf(buf, sizeof(buf),
1168 "%s: ", file->filename);
1170 } else {
1171 strcat_printf(buf, sizeof(buf),
1172 "tcc: ");
1174 if (is_warning)
1175 strcat_printf(buf, sizeof(buf), "warning: ");
1176 strcat_vprintf(buf, sizeof(buf), fmt, ap);
1178 if (!s1->error_func) {
1179 /* default case: stderr */
1180 fprintf(stderr, "%s\n", buf);
1181 } else {
1182 s1->error_func(s1->error_opaque, buf);
1184 if (!is_warning || s1->warn_error)
1185 s1->nb_errors++;
1188 #ifdef LIBTCC
1189 void tcc_set_error_func(TCCState *s, void *error_opaque,
1190 void (*error_func)(void *opaque, const char *msg))
1192 s->error_opaque = error_opaque;
1193 s->error_func = error_func;
1195 #endif
1197 /* error without aborting current compilation */
1198 void error_noabort(const char *fmt, ...)
1200 TCCState *s1 = tcc_state;
1201 va_list ap;
1203 va_start(ap, fmt);
1204 error1(s1, 0, fmt, ap);
1205 va_end(ap);
1208 void error(const char *fmt, ...)
1210 TCCState *s1 = tcc_state;
1211 va_list ap;
1213 va_start(ap, fmt);
1214 error1(s1, 0, fmt, ap);
1215 va_end(ap);
1216 /* better than nothing: in some cases, we accept to handle errors */
1217 if (s1->error_set_jmp_enabled) {
1218 longjmp(s1->error_jmp_buf, 1);
1219 } else {
1220 /* XXX: eliminate this someday */
1221 exit(1);
1225 void expect(const char *msg)
1227 error("%s expected", msg);
1230 void warning(const char *fmt, ...)
1232 TCCState *s1 = tcc_state;
1233 va_list ap;
1235 va_start(ap, fmt);
1236 error1(s1, 1, fmt, ap);
1237 va_end(ap);
1240 void skip(int c)
1242 if (tok != c)
1243 error("'%c' expected", c);
1244 next();
1247 static void test_lvalue(void)
1249 if (!(vtop->r & VT_LVAL))
1250 expect("lvalue");
1253 /* allocate a new token */
1254 static TokenSym *tok_alloc_new(TokenSym **pts, const char *str, int len)
1256 TokenSym *ts, **ptable;
1257 int i;
1259 if (tok_ident >= SYM_FIRST_ANOM)
1260 error("memory full");
1262 /* expand token table if needed */
1263 i = tok_ident - TOK_IDENT;
1264 if ((i % TOK_ALLOC_INCR) == 0) {
1265 ptable = tcc_realloc(table_ident, (i + TOK_ALLOC_INCR) * sizeof(TokenSym *));
1266 if (!ptable)
1267 error("memory full");
1268 table_ident = ptable;
1271 ts = tcc_malloc(sizeof(TokenSym) + len);
1272 table_ident[i] = ts;
1273 ts->tok = tok_ident++;
1274 ts->sym_define = NULL;
1275 ts->sym_label = NULL;
1276 ts->sym_struct = NULL;
1277 ts->sym_identifier = NULL;
1278 ts->len = len;
1279 ts->hash_next = NULL;
1280 memcpy(ts->str, str, len);
1281 ts->str[len] = '\0';
1282 *pts = ts;
1283 return ts;
1286 #define TOK_HASH_INIT 1
1287 #define TOK_HASH_FUNC(h, c) ((h) * 263 + (c))
1289 /* find a token and add it if not found */
1290 static TokenSym *tok_alloc(const char *str, int len)
1292 TokenSym *ts, **pts;
1293 int i;
1294 unsigned int h;
1296 h = TOK_HASH_INIT;
1297 for(i=0;i<len;i++)
1298 h = TOK_HASH_FUNC(h, ((unsigned char *)str)[i]);
1299 h &= (TOK_HASH_SIZE - 1);
1301 pts = &hash_ident[h];
1302 for(;;) {
1303 ts = *pts;
1304 if (!ts)
1305 break;
1306 if (ts->len == len && !memcmp(ts->str, str, len))
1307 return ts;
1308 pts = &(ts->hash_next);
1310 return tok_alloc_new(pts, str, len);
1313 /* CString handling */
1315 static void cstr_realloc(CString *cstr, int new_size)
1317 int size;
1318 void *data;
1320 size = cstr->size_allocated;
1321 if (size == 0)
1322 size = 8; /* no need to allocate a too small first string */
1323 while (size < new_size)
1324 size = size * 2;
1325 data = tcc_realloc(cstr->data_allocated, size);
1326 if (!data)
1327 error("memory full");
1328 cstr->data_allocated = data;
1329 cstr->size_allocated = size;
1330 cstr->data = data;
1333 /* add a byte */
1334 static void cstr_ccat(CString *cstr, int ch)
1336 int size;
1337 size = cstr->size + 1;
1338 if (size > cstr->size_allocated)
1339 cstr_realloc(cstr, size);
1340 ((unsigned char *)cstr->data)[size - 1] = ch;
1341 cstr->size = size;
1344 static void cstr_cat(CString *cstr, const char *str)
1346 int c;
1347 for(;;) {
1348 c = *str;
1349 if (c == '\0')
1350 break;
1351 cstr_ccat(cstr, c);
1352 str++;
1356 /* add a wide char */
1357 static void cstr_wccat(CString *cstr, int ch)
1359 int size;
1360 size = cstr->size + sizeof(int);
1361 if (size > cstr->size_allocated)
1362 cstr_realloc(cstr, size);
1363 *(int *)(((unsigned char *)cstr->data) + size - sizeof(int)) = ch;
1364 cstr->size = size;
1367 static void cstr_new(CString *cstr)
1369 memset(cstr, 0, sizeof(CString));
1372 /* free string and reset it to NULL */
1373 static void cstr_free(CString *cstr)
1375 tcc_free(cstr->data_allocated);
1376 cstr_new(cstr);
1379 #define cstr_reset(cstr) cstr_free(cstr)
1381 static CString *cstr_dup(CString *cstr1)
1383 CString *cstr;
1384 int size;
1386 cstr = tcc_malloc(sizeof(CString));
1387 size = cstr1->size;
1388 cstr->size = size;
1389 cstr->size_allocated = size;
1390 cstr->data_allocated = tcc_malloc(size);
1391 cstr->data = cstr->data_allocated;
1392 memcpy(cstr->data_allocated, cstr1->data_allocated, size);
1393 return cstr;
1396 /* XXX: unicode ? */
1397 static void add_char(CString *cstr, int c)
1399 if (c == '\'' || c == '\"' || c == '\\') {
1400 /* XXX: could be more precise if char or string */
1401 cstr_ccat(cstr, '\\');
1403 if (c >= 32 && c <= 126) {
1404 cstr_ccat(cstr, c);
1405 } else {
1406 cstr_ccat(cstr, '\\');
1407 if (c == '\n') {
1408 cstr_ccat(cstr, 'n');
1409 } else {
1410 cstr_ccat(cstr, '0' + ((c >> 6) & 7));
1411 cstr_ccat(cstr, '0' + ((c >> 3) & 7));
1412 cstr_ccat(cstr, '0' + (c & 7));
1417 /* XXX: buffer overflow */
1418 /* XXX: float tokens */
1419 char *get_tok_str(int v, CValue *cv)
1421 static char buf[STRING_MAX_SIZE + 1];
1422 static CString cstr_buf;
1423 CString *cstr;
1424 unsigned char *q;
1425 char *p;
1426 int i, len;
1428 /* NOTE: to go faster, we give a fixed buffer for small strings */
1429 cstr_reset(&cstr_buf);
1430 cstr_buf.data = buf;
1431 cstr_buf.size_allocated = sizeof(buf);
1432 p = buf;
1434 switch(v) {
1435 case TOK_CINT:
1436 case TOK_CUINT:
1437 /* XXX: not quite exact, but only useful for testing */
1438 sprintf(p, "%u", cv->ui);
1439 break;
1440 case TOK_CLLONG:
1441 case TOK_CULLONG:
1442 /* XXX: not quite exact, but only useful for testing */
1443 sprintf(p, "%Lu", cv->ull);
1444 break;
1445 case TOK_CCHAR:
1446 case TOK_LCHAR:
1447 cstr_ccat(&cstr_buf, '\'');
1448 add_char(&cstr_buf, cv->i);
1449 cstr_ccat(&cstr_buf, '\'');
1450 cstr_ccat(&cstr_buf, '\0');
1451 break;
1452 case TOK_PPNUM:
1453 cstr = cv->cstr;
1454 len = cstr->size - 1;
1455 for(i=0;i<len;i++)
1456 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
1457 cstr_ccat(&cstr_buf, '\0');
1458 break;
1459 case TOK_STR:
1460 case TOK_LSTR:
1461 cstr = cv->cstr;
1462 cstr_ccat(&cstr_buf, '\"');
1463 if (v == TOK_STR) {
1464 len = cstr->size - 1;
1465 for(i=0;i<len;i++)
1466 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
1467 } else {
1468 len = (cstr->size / sizeof(int)) - 1;
1469 for(i=0;i<len;i++)
1470 add_char(&cstr_buf, ((int *)cstr->data)[i]);
1472 cstr_ccat(&cstr_buf, '\"');
1473 cstr_ccat(&cstr_buf, '\0');
1474 break;
1475 case TOK_LT:
1476 v = '<';
1477 goto addv;
1478 case TOK_GT:
1479 v = '>';
1480 goto addv;
1481 case TOK_A_SHL:
1482 return strcpy(p, "<<=");
1483 case TOK_A_SAR:
1484 return strcpy(p, ">>=");
1485 default:
1486 if (v < TOK_IDENT) {
1487 /* search in two bytes table */
1488 q = tok_two_chars;
1489 while (*q) {
1490 if (q[2] == v) {
1491 *p++ = q[0];
1492 *p++ = q[1];
1493 *p = '\0';
1494 return buf;
1496 q += 3;
1498 addv:
1499 *p++ = v;
1500 *p = '\0';
1501 } else if (v < tok_ident) {
1502 return table_ident[v - TOK_IDENT]->str;
1503 } else if (v >= SYM_FIRST_ANOM) {
1504 /* special name for anonymous symbol */
1505 sprintf(p, "L.%u", v - SYM_FIRST_ANOM);
1506 } else {
1507 /* should never happen */
1508 return NULL;
1510 break;
1512 return cstr_buf.data;
1515 /* push, without hashing */
1516 static Sym *sym_push2(Sym **ps, int v, int t, int c)
1518 Sym *s;
1519 s = tcc_malloc(sizeof(Sym));
1520 s->v = v;
1521 s->type.t = t;
1522 s->c = c;
1523 s->next = NULL;
1524 /* add in stack */
1525 s->prev = *ps;
1526 *ps = s;
1527 return s;
1530 /* find a symbol and return its associated structure. 's' is the top
1531 of the symbol stack */
1532 static Sym *sym_find2(Sym *s, int v)
1534 while (s) {
1535 if (s->v == v)
1536 return s;
1537 s = s->prev;
1539 return NULL;
1542 /* structure lookup */
1543 static inline Sym *struct_find(int v)
1545 v -= TOK_IDENT;
1546 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1547 return NULL;
1548 return table_ident[v]->sym_struct;
1551 /* find an identifier */
1552 static inline Sym *sym_find(int v)
1554 v -= TOK_IDENT;
1555 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1556 return NULL;
1557 return table_ident[v]->sym_identifier;
1560 /* push a given symbol on the symbol stack */
1561 static Sym *sym_push(int v, CType *type, int r, int c)
1563 Sym *s, **ps;
1564 TokenSym *ts;
1566 if (local_stack)
1567 ps = &local_stack;
1568 else
1569 ps = &global_stack;
1570 s = sym_push2(ps, v, type->t, c);
1571 s->type.ref = type->ref;
1572 s->r = r;
1573 /* don't record fields or anonymous symbols */
1574 /* XXX: simplify */
1575 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
1576 /* record symbol in token array */
1577 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
1578 if (v & SYM_STRUCT)
1579 ps = &ts->sym_struct;
1580 else
1581 ps = &ts->sym_identifier;
1582 s->prev_tok = *ps;
1583 *ps = s;
1585 return s;
1588 /* push a global identifier */
1589 static Sym *global_identifier_push(int v, int t, int c)
1591 Sym *s, **ps;
1592 s = sym_push2(&global_stack, v, t, c);
1593 /* don't record anonymous symbol */
1594 if (v < SYM_FIRST_ANOM) {
1595 ps = &table_ident[v - TOK_IDENT]->sym_identifier;
1596 /* modify the top most local identifier, so that
1597 sym_identifier will point to 's' when popped */
1598 while (*ps != NULL)
1599 ps = &(*ps)->prev_tok;
1600 s->prev_tok = NULL;
1601 *ps = s;
1603 return s;
1606 /* pop symbols until top reaches 'b' */
1607 static void sym_pop(Sym **ptop, Sym *b)
1609 Sym *s, *ss, **ps;
1610 TokenSym *ts;
1611 int v;
1613 s = *ptop;
1614 while(s != b) {
1615 ss = s->prev;
1616 v = s->v;
1617 /* remove symbol in token array */
1618 /* XXX: simplify */
1619 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
1620 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
1621 if (v & SYM_STRUCT)
1622 ps = &ts->sym_struct;
1623 else
1624 ps = &ts->sym_identifier;
1625 *ps = s->prev_tok;
1627 tcc_free(s);
1628 s = ss;
1630 *ptop = b;
1633 /* I/O layer */
1635 BufferedFile *tcc_open(TCCState *s1, const char *filename)
1637 int fd;
1638 BufferedFile *bf;
1640 fd = open(filename, O_RDONLY);
1641 if (fd < 0)
1642 return NULL;
1643 bf = tcc_malloc(sizeof(BufferedFile));
1644 if (!bf) {
1645 close(fd);
1646 return NULL;
1648 bf->fd = fd;
1649 bf->buf_ptr = bf->buffer;
1650 bf->buf_end = bf->buffer;
1651 bf->buffer[0] = CH_EOB; /* put eob symbol */
1652 pstrcpy(bf->filename, sizeof(bf->filename), filename);
1653 bf->line_num = 1;
1654 bf->ifndef_macro = 0;
1655 bf->ifdef_stack_ptr = s1->ifdef_stack_ptr;
1656 // printf("opening '%s'\n", filename);
1657 return bf;
1660 void tcc_close(BufferedFile *bf)
1662 total_lines += bf->line_num;
1663 close(bf->fd);
1664 tcc_free(bf);
1667 /* fill input buffer and peek next char */
1668 static int tcc_peekc_slow(BufferedFile *bf)
1670 int len;
1671 /* only tries to read if really end of buffer */
1672 if (bf->buf_ptr >= bf->buf_end) {
1673 if (bf->fd != -1) {
1674 #if defined(PARSE_DEBUG)
1675 len = 8;
1676 #else
1677 len = IO_BUF_SIZE;
1678 #endif
1679 len = read(bf->fd, bf->buffer, len);
1680 if (len < 0)
1681 len = 0;
1682 } else {
1683 len = 0;
1685 total_bytes += len;
1686 bf->buf_ptr = bf->buffer;
1687 bf->buf_end = bf->buffer + len;
1688 *bf->buf_end = CH_EOB;
1690 if (bf->buf_ptr < bf->buf_end) {
1691 return bf->buf_ptr[0];
1692 } else {
1693 bf->buf_ptr = bf->buf_end;
1694 return CH_EOF;
1698 /* return the current character, handling end of block if necessary
1699 (but not stray) */
1700 static int handle_eob(void)
1702 return tcc_peekc_slow(file);
1705 /* read next char from current input file and handle end of input buffer */
1706 static inline void inp(void)
1708 ch = *(++(file->buf_ptr));
1709 /* end of buffer/file handling */
1710 if (ch == CH_EOB)
1711 ch = handle_eob();
1714 /* handle '\[\r]\n' */
1715 static void handle_stray(void)
1717 while (ch == '\\') {
1718 inp();
1719 if (ch == '\n') {
1720 file->line_num++;
1721 inp();
1722 } else if (ch == '\r') {
1723 inp();
1724 if (ch != '\n')
1725 goto fail;
1726 file->line_num++;
1727 inp();
1728 } else {
1729 fail:
1730 error("stray '\\' in program");
1735 /* skip the stray and handle the \\n case. Output an error if
1736 incorrect char after the stray */
1737 static int handle_stray1(uint8_t *p)
1739 int c;
1741 if (p >= file->buf_end) {
1742 file->buf_ptr = p;
1743 c = handle_eob();
1744 p = file->buf_ptr;
1745 if (c == '\\')
1746 goto parse_stray;
1747 } else {
1748 parse_stray:
1749 file->buf_ptr = p;
1750 ch = *p;
1751 handle_stray();
1752 p = file->buf_ptr;
1753 c = *p;
1755 return c;
1758 /* handle just the EOB case, but not stray */
1759 #define PEEKC_EOB(c, p)\
1761 p++;\
1762 c = *p;\
1763 if (c == '\\') {\
1764 file->buf_ptr = p;\
1765 c = handle_eob();\
1766 p = file->buf_ptr;\
1770 /* handle the complicated stray case */
1771 #define PEEKC(c, p)\
1773 p++;\
1774 c = *p;\
1775 if (c == '\\') {\
1776 c = handle_stray1(p);\
1777 p = file->buf_ptr;\
1781 /* input with '\[\r]\n' handling. Note that this function cannot
1782 handle other characters after '\', so you cannot call it inside
1783 strings or comments */
1784 static void minp(void)
1786 inp();
1787 if (ch == '\\')
1788 handle_stray();
1792 /* single line C++ comments */
1793 static uint8_t *parse_line_comment(uint8_t *p)
1795 int c;
1797 p++;
1798 for(;;) {
1799 c = *p;
1800 if (c == '\n' || c == CH_EOF) {
1801 break;
1802 } else if (c == '\\') {
1803 PEEKC_EOB(c, p);
1804 if (c == '\n') {
1805 file->line_num++;
1806 PEEKC_EOB(c, p);
1807 } else if (c == '\r') {
1808 PEEKC_EOB(c, p);
1809 if (c == '\n') {
1810 file->line_num++;
1811 PEEKC_EOB(c, p);
1814 } else {
1815 p++;
1818 return p;
1821 /* C comments */
1822 static uint8_t *parse_comment(uint8_t *p)
1824 int c;
1826 p++;
1827 for(;;) {
1828 /* fast skip loop */
1829 for(;;) {
1830 c = *p;
1831 if (c == '\n' || c == '*' || c == '\\')
1832 break;
1833 p++;
1834 c = *p;
1835 if (c == '\n' || c == '*' || c == '\\')
1836 break;
1837 p++;
1839 /* now we can handle all the cases */
1840 if (c == '\n') {
1841 file->line_num++;
1842 p++;
1843 } else if (c == '*') {
1844 p++;
1845 for(;;) {
1846 c = *p;
1847 if (c == '*') {
1848 p++;
1849 } else if (c == '/') {
1850 goto end_of_comment;
1851 } else if (c == '\\') {
1852 file->buf_ptr = p;
1853 c = handle_eob();
1854 p = file->buf_ptr;
1855 if (c == '\\') {
1856 /* skip '\[\r]\n', otherwise just skip the stray */
1857 while (c == '\\') {
1858 PEEKC_EOB(c, p);
1859 if (c == '\n') {
1860 file->line_num++;
1861 PEEKC_EOB(c, p);
1862 } else if (c == '\r') {
1863 PEEKC_EOB(c, p);
1864 if (c == '\n') {
1865 file->line_num++;
1866 PEEKC_EOB(c, p);
1868 } else {
1869 goto after_star;
1873 } else {
1874 break;
1877 after_star: ;
1878 } else {
1879 /* stray, eob or eof */
1880 file->buf_ptr = p;
1881 c = handle_eob();
1882 p = file->buf_ptr;
1883 if (c == CH_EOF) {
1884 error("unexpected end of file in comment");
1885 } else if (c == '\\') {
1886 p++;
1890 end_of_comment:
1891 p++;
1892 return p;
1895 #define cinp minp
1897 /* space exlcuding newline */
1898 static inline int is_space(int ch)
1900 return ch == ' ' || ch == '\t' || ch == '\v' || ch == '\f' || ch == '\r';
1903 static inline void skip_spaces(void)
1905 while (is_space(ch))
1906 cinp();
1909 /* parse a string without interpreting escapes */
1910 static uint8_t *parse_pp_string(uint8_t *p,
1911 int sep, CString *str)
1913 int c;
1914 p++;
1915 for(;;) {
1916 c = *p;
1917 if (c == sep) {
1918 break;
1919 } else if (c == '\\') {
1920 file->buf_ptr = p;
1921 c = handle_eob();
1922 p = file->buf_ptr;
1923 if (c == CH_EOF) {
1924 unterminated_string:
1925 /* XXX: indicate line number of start of string */
1926 error("missing terminating %c character", sep);
1927 } else if (c == '\\') {
1928 /* escape : just skip \[\r]\n */
1929 PEEKC_EOB(c, p);
1930 if (c == '\n') {
1931 file->line_num++;
1932 p++;
1933 } else if (c == '\r') {
1934 PEEKC_EOB(c, p);
1935 if (c != '\n')
1936 expect("'\n' after '\r'");
1937 file->line_num++;
1938 p++;
1939 } else if (c == CH_EOF) {
1940 goto unterminated_string;
1941 } else {
1942 if (str) {
1943 cstr_ccat(str, '\\');
1944 cstr_ccat(str, c);
1946 p++;
1949 } else if (c == '\n') {
1950 file->line_num++;
1951 goto add_char;
1952 } else if (c == '\r') {
1953 PEEKC_EOB(c, p);
1954 if (c != '\n') {
1955 cstr_ccat(str, '\r');
1956 } else {
1957 file->line_num++;
1958 goto add_char;
1960 } else {
1961 add_char:
1962 if (str)
1963 cstr_ccat(str, c);
1964 p++;
1967 p++;
1968 return p;
1971 /* skip block of text until #else, #elif or #endif. skip also pairs of
1972 #if/#endif */
1973 void preprocess_skip(void)
1975 int a, start_of_line, c;
1976 uint8_t *p;
1978 p = file->buf_ptr;
1979 start_of_line = 1;
1980 a = 0;
1981 for(;;) {
1982 redo_no_start:
1983 c = *p;
1984 switch(c) {
1985 case ' ':
1986 case '\t':
1987 case '\f':
1988 case '\v':
1989 case '\r':
1990 p++;
1991 goto redo_no_start;
1992 case '\n':
1993 start_of_line = 1;
1994 file->line_num++;
1995 p++;
1996 goto redo_no_start;
1997 case '\\':
1998 file->buf_ptr = p;
1999 c = handle_eob();
2000 if (c == CH_EOF) {
2001 expect("#endif");
2002 } else if (c == '\\') {
2003 /* XXX: incorrect: should not give an error */
2004 ch = file->buf_ptr[0];
2005 handle_stray();
2007 p = file->buf_ptr;
2008 goto redo_no_start;
2009 /* skip strings */
2010 case '\"':
2011 case '\'':
2012 p = parse_pp_string(p, c, NULL);
2013 break;
2014 /* skip comments */
2015 case '/':
2016 file->buf_ptr = p;
2017 ch = *p;
2018 minp();
2019 p = file->buf_ptr;
2020 if (ch == '*') {
2021 p = parse_comment(p);
2022 } else if (ch == '/') {
2023 p = parse_line_comment(p);
2025 break;
2027 case '#':
2028 p++;
2029 if (start_of_line) {
2030 file->buf_ptr = p;
2031 next_nomacro();
2032 p = file->buf_ptr;
2033 if (a == 0 &&
2034 (tok == TOK_ELSE || tok == TOK_ELIF || tok == TOK_ENDIF))
2035 goto the_end;
2036 if (tok == TOK_IF || tok == TOK_IFDEF || tok == TOK_IFNDEF)
2037 a++;
2038 else if (tok == TOK_ENDIF)
2039 a--;
2041 break;
2042 default:
2043 p++;
2044 break;
2046 start_of_line = 0;
2048 the_end: ;
2049 file->buf_ptr = p;
2052 /* ParseState handling */
2054 /* XXX: currently, no include file info is stored. Thus, we cannot display
2055 accurate messages if the function or data definition spans multiple
2056 files */
2058 /* save current parse state in 's' */
2059 void save_parse_state(ParseState *s)
2061 s->line_num = file->line_num;
2062 s->macro_ptr = macro_ptr;
2063 s->tok = tok;
2064 s->tokc = tokc;
2067 /* restore parse state from 's' */
2068 void restore_parse_state(ParseState *s)
2070 file->line_num = s->line_num;
2071 macro_ptr = s->macro_ptr;
2072 tok = s->tok;
2073 tokc = s->tokc;
2076 /* return the number of additional 'ints' necessary to store the
2077 token */
2078 static inline int tok_ext_size(int t)
2080 switch(t) {
2081 /* 4 bytes */
2082 case TOK_CINT:
2083 case TOK_CUINT:
2084 case TOK_CCHAR:
2085 case TOK_LCHAR:
2086 case TOK_STR:
2087 case TOK_LSTR:
2088 case TOK_CFLOAT:
2089 case TOK_LINENUM:
2090 case TOK_PPNUM:
2091 return 1;
2092 case TOK_CDOUBLE:
2093 case TOK_CLLONG:
2094 case TOK_CULLONG:
2095 return 2;
2096 case TOK_CLDOUBLE:
2097 return LDOUBLE_SIZE / 4;
2098 default:
2099 return 0;
2103 /* token string handling */
2105 static inline void tok_str_new(TokenString *s)
2107 s->str = NULL;
2108 s->len = 0;
2109 s->allocated_len = 0;
2110 s->last_line_num = -1;
2113 static void tok_str_free(int *str)
2115 const int *p;
2116 CString *cstr;
2117 int t;
2119 p = str;
2120 for(;;) {
2121 t = *p;
2122 /* NOTE: we test zero separately so that GCC can generate a
2123 table for the following switch */
2124 if (t == 0)
2125 break;
2126 switch(t) {
2127 case TOK_CINT:
2128 case TOK_CUINT:
2129 case TOK_CCHAR:
2130 case TOK_LCHAR:
2131 case TOK_CFLOAT:
2132 case TOK_LINENUM:
2133 p += 2;
2134 break;
2135 case TOK_PPNUM:
2136 case TOK_STR:
2137 case TOK_LSTR:
2138 /* XXX: use a macro to be portable on 64 bit ? */
2139 cstr = (CString *)p[1];
2140 cstr_free(cstr);
2141 tcc_free(cstr);
2142 p += 2;
2143 break;
2144 case TOK_CDOUBLE:
2145 case TOK_CLLONG:
2146 case TOK_CULLONG:
2147 p += 3;
2148 break;
2149 case TOK_CLDOUBLE:
2150 p += 1 + (LDOUBLE_SIZE / 4);
2151 break;
2152 default:
2153 p++;
2154 break;
2157 tcc_free(str);
2160 static int *tok_str_realloc(TokenString *s)
2162 int *str, len;
2164 len = s->allocated_len + TOK_STR_ALLOC_INCR;
2165 str = tcc_realloc(s->str, len * sizeof(int));
2166 if (!str)
2167 error("memory full");
2168 s->allocated_len = len;
2169 s->str = str;
2170 return str;
2173 static void tok_str_add(TokenString *s, int t)
2175 int len, *str;
2177 len = s->len;
2178 str = s->str;
2179 if (len >= s->allocated_len)
2180 str = tok_str_realloc(s);
2181 str[len++] = t;
2182 s->len = len;
2185 static void tok_str_add2(TokenString *s, int t, CValue *cv)
2187 int len, *str;
2189 len = s->len;
2190 str = s->str;
2192 /* allocate space for worst case */
2193 if (len + TOK_MAX_SIZE > s->allocated_len)
2194 str = tok_str_realloc(s);
2195 str[len++] = t;
2196 switch(t) {
2197 case TOK_CINT:
2198 case TOK_CUINT:
2199 case TOK_CCHAR:
2200 case TOK_LCHAR:
2201 case TOK_CFLOAT:
2202 case TOK_LINENUM:
2203 str[len++] = cv->tab[0];
2204 break;
2205 case TOK_PPNUM:
2206 case TOK_STR:
2207 case TOK_LSTR:
2208 str[len++] = (int)cstr_dup(cv->cstr);
2209 break;
2210 case TOK_CDOUBLE:
2211 case TOK_CLLONG:
2212 case TOK_CULLONG:
2213 str[len++] = cv->tab[0];
2214 str[len++] = cv->tab[1];
2215 break;
2216 case TOK_CLDOUBLE:
2217 #if LDOUBLE_SIZE == 12
2218 str[len++] = cv->tab[0];
2219 str[len++] = cv->tab[1];
2220 str[len++] = cv->tab[2];
2221 #else
2222 #error add long double size support
2223 #endif
2224 break;
2225 default:
2226 break;
2228 s->len = len;
2231 /* add the current parse token in token string 's' */
2232 static void tok_str_add_tok(TokenString *s)
2234 CValue cval;
2236 /* save line number info */
2237 if (file->line_num != s->last_line_num) {
2238 s->last_line_num = file->line_num;
2239 cval.i = s->last_line_num;
2240 tok_str_add2(s, TOK_LINENUM, &cval);
2242 tok_str_add2(s, tok, &tokc);
2245 #if LDOUBLE_SIZE == 12
2246 #define LDOUBLE_GET(p, cv) \
2247 cv.tab[0] = p[0]; \
2248 cv.tab[1] = p[1]; \
2249 cv.tab[2] = p[2];
2250 #else
2251 #error add long double size support
2252 #endif
2255 /* get a token from an integer array and increment pointer
2256 accordingly. we code it as a macro to avoid pointer aliasing. */
2257 #define TOK_GET(t, p, cv) \
2259 t = *p++; \
2260 switch(t) { \
2261 case TOK_CINT: \
2262 case TOK_CUINT: \
2263 case TOK_CCHAR: \
2264 case TOK_LCHAR: \
2265 case TOK_CFLOAT: \
2266 case TOK_LINENUM: \
2267 case TOK_STR: \
2268 case TOK_LSTR: \
2269 case TOK_PPNUM: \
2270 cv.tab[0] = *p++; \
2271 break; \
2272 case TOK_CDOUBLE: \
2273 case TOK_CLLONG: \
2274 case TOK_CULLONG: \
2275 cv.tab[0] = p[0]; \
2276 cv.tab[1] = p[1]; \
2277 p += 2; \
2278 break; \
2279 case TOK_CLDOUBLE: \
2280 LDOUBLE_GET(p, cv); \
2281 p += LDOUBLE_SIZE / 4; \
2282 break; \
2283 default: \
2284 break; \
2288 /* defines handling */
2289 static inline void define_push(int v, int macro_type, int *str, Sym *first_arg)
2291 Sym *s;
2293 s = sym_push2(&define_stack, v, macro_type, (int)str);
2294 s->next = first_arg;
2295 table_ident[v - TOK_IDENT]->sym_define = s;
2298 /* undefined a define symbol. Its name is just set to zero */
2299 static void define_undef(Sym *s)
2301 int v;
2302 v = s->v;
2303 if (v >= TOK_IDENT && v < tok_ident)
2304 table_ident[v - TOK_IDENT]->sym_define = NULL;
2305 s->v = 0;
2308 static inline Sym *define_find(int v)
2310 v -= TOK_IDENT;
2311 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
2312 return NULL;
2313 return table_ident[v]->sym_define;
2316 /* free define stack until top reaches 'b' */
2317 static void free_defines(Sym *b)
2319 Sym *top, *top1;
2320 int v;
2322 top = define_stack;
2323 while (top != b) {
2324 top1 = top->prev;
2325 /* do not free args or predefined defines */
2326 if (top->c)
2327 tok_str_free((int *)top->c);
2328 v = top->v;
2329 if (v >= TOK_IDENT && v < tok_ident)
2330 table_ident[v - TOK_IDENT]->sym_define = NULL;
2331 tcc_free(top);
2332 top = top1;
2334 define_stack = b;
2337 /* label lookup */
2338 static Sym *label_find(int v)
2340 v -= TOK_IDENT;
2341 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
2342 return NULL;
2343 return table_ident[v]->sym_label;
2346 static Sym *label_push(Sym **ptop, int v, int flags)
2348 Sym *s, **ps;
2349 s = sym_push2(ptop, v, 0, 0);
2350 s->r = flags;
2351 ps = &table_ident[v - TOK_IDENT]->sym_label;
2352 if (ptop == &global_label_stack) {
2353 /* modify the top most local identifier, so that
2354 sym_identifier will point to 's' when popped */
2355 while (*ps != NULL)
2356 ps = &(*ps)->prev_tok;
2358 s->prev_tok = *ps;
2359 *ps = s;
2360 return s;
2363 /* pop labels until element last is reached. Look if any labels are
2364 undefined. Define symbols if '&&label' was used. */
2365 static void label_pop(Sym **ptop, Sym *slast)
2367 Sym *s, *s1;
2368 for(s = *ptop; s != slast; s = s1) {
2369 s1 = s->prev;
2370 if (s->r == LABEL_DECLARED) {
2371 warning("label '%s' declared but not used", get_tok_str(s->v, NULL));
2372 } else if (s->r == LABEL_FORWARD) {
2373 error("label '%s' used but not defined",
2374 get_tok_str(s->v, NULL));
2375 } else {
2376 if (s->c) {
2377 /* define corresponding symbol. A size of
2378 1 is put. */
2379 put_extern_sym(s, cur_text_section, (long)s->next, 1);
2382 /* remove label */
2383 table_ident[s->v - TOK_IDENT]->sym_label = s->prev_tok;
2384 tcc_free(s);
2386 *ptop = slast;
2389 /* eval an expression for #if/#elif */
2390 static int expr_preprocess(void)
2392 int c, t;
2393 TokenString str;
2395 tok_str_new(&str);
2396 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
2397 next(); /* do macro subst */
2398 if (tok == TOK_DEFINED) {
2399 next_nomacro();
2400 t = tok;
2401 if (t == '(')
2402 next_nomacro();
2403 c = define_find(tok) != 0;
2404 if (t == '(')
2405 next_nomacro();
2406 tok = TOK_CINT;
2407 tokc.i = c;
2408 } else if (tok >= TOK_IDENT) {
2409 /* if undefined macro */
2410 tok = TOK_CINT;
2411 tokc.i = 0;
2413 tok_str_add_tok(&str);
2415 tok_str_add(&str, -1); /* simulate end of file */
2416 tok_str_add(&str, 0);
2417 /* now evaluate C constant expression */
2418 macro_ptr = str.str;
2419 next();
2420 c = expr_const();
2421 macro_ptr = NULL;
2422 tok_str_free(str.str);
2423 return c != 0;
2426 #if defined(PARSE_DEBUG) || defined(PP_DEBUG)
2427 static void tok_print(int *str)
2429 int t;
2430 CValue cval;
2432 while (1) {
2433 TOK_GET(t, str, cval);
2434 if (!t)
2435 break;
2436 printf(" %s", get_tok_str(t, &cval));
2438 printf("\n");
2440 #endif
2442 /* parse after #define */
2443 static void parse_define(void)
2445 Sym *s, *first, **ps;
2446 int v, t, varg, is_vaargs, c;
2447 TokenString str;
2449 v = tok;
2450 if (v < TOK_IDENT)
2451 error("invalid macro name '%s'", get_tok_str(tok, &tokc));
2452 /* XXX: should check if same macro (ANSI) */
2453 first = NULL;
2454 t = MACRO_OBJ;
2455 /* '(' must be just after macro definition for MACRO_FUNC */
2456 c = file->buf_ptr[0];
2457 if (c == '\\')
2458 c = handle_stray1(file->buf_ptr);
2459 if (c == '(') {
2460 next_nomacro();
2461 next_nomacro();
2462 ps = &first;
2463 while (tok != ')') {
2464 varg = tok;
2465 next_nomacro();
2466 is_vaargs = 0;
2467 if (varg == TOK_DOTS) {
2468 varg = TOK___VA_ARGS__;
2469 is_vaargs = 1;
2470 } else if (tok == TOK_DOTS && gnu_ext) {
2471 is_vaargs = 1;
2472 next_nomacro();
2474 if (varg < TOK_IDENT)
2475 error("badly punctuated parameter list");
2476 s = sym_push2(&define_stack, varg | SYM_FIELD, is_vaargs, 0);
2477 *ps = s;
2478 ps = &s->next;
2479 if (tok != ',')
2480 break;
2481 next_nomacro();
2483 t = MACRO_FUNC;
2485 tok_str_new(&str);
2486 next_nomacro();
2487 /* EOF testing necessary for '-D' handling */
2488 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
2489 tok_str_add2(&str, tok, &tokc);
2490 next_nomacro();
2492 tok_str_add(&str, 0);
2493 #ifdef PP_DEBUG
2494 printf("define %s %d: ", get_tok_str(v, NULL), t);
2495 tok_print(str.str);
2496 #endif
2497 define_push(v, t, str.str, first);
2500 /* XXX: use a token or a hash table to accelerate matching ? */
2501 static CachedInclude *search_cached_include(TCCState *s1,
2502 int type, const char *filename)
2504 CachedInclude *e;
2505 int i;
2507 for(i = 0;i < s1->nb_cached_includes; i++) {
2508 e = s1->cached_includes[i];
2509 if (e->type == type && !strcmp(e->filename, filename))
2510 return e;
2512 return NULL;
2515 static inline void add_cached_include(TCCState *s1, int type,
2516 const char *filename, int ifndef_macro)
2518 CachedInclude *e;
2520 if (search_cached_include(s1, type, filename))
2521 return;
2522 #ifdef INC_DEBUG
2523 printf("adding cached '%s' %s\n", filename, get_tok_str(ifndef_macro, NULL));
2524 #endif
2525 e = tcc_malloc(sizeof(CachedInclude) + strlen(filename));
2526 if (!e)
2527 return;
2528 e->type = type;
2529 strcpy(e->filename, filename);
2530 e->ifndef_macro = ifndef_macro;
2531 dynarray_add((void ***)&s1->cached_includes, &s1->nb_cached_includes, e);
2534 /* is_bof is true if first non space token at beginning of file */
2535 static void preprocess(int is_bof)
2537 TCCState *s1 = tcc_state;
2538 int size, i, c, n, saved_parse_flags;
2539 char buf[1024], *q, *p;
2540 char buf1[1024];
2541 BufferedFile *f;
2542 Sym *s;
2543 CachedInclude *e;
2545 saved_parse_flags = parse_flags;
2546 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM |
2547 PARSE_FLAG_LINEFEED;
2548 next_nomacro();
2549 redo:
2550 switch(tok) {
2551 case TOK_DEFINE:
2552 next_nomacro();
2553 parse_define();
2554 break;
2555 case TOK_UNDEF:
2556 next_nomacro();
2557 s = define_find(tok);
2558 /* undefine symbol by putting an invalid name */
2559 if (s)
2560 define_undef(s);
2561 break;
2562 case TOK_INCLUDE:
2563 ch = file->buf_ptr[0];
2564 /* XXX: incorrect if comments : use next_nomacro with a special mode */
2565 skip_spaces();
2566 if (ch == '<') {
2567 c = '>';
2568 goto read_name;
2569 } else if (ch == '\"') {
2570 c = ch;
2571 read_name:
2572 /* XXX: better stray handling */
2573 minp();
2574 q = buf;
2575 while (ch != c && ch != '\n' && ch != CH_EOF) {
2576 if ((q - buf) < sizeof(buf) - 1)
2577 *q++ = ch;
2578 minp();
2580 *q = '\0';
2581 minp();
2582 #if 0
2583 /* eat all spaces and comments after include */
2584 /* XXX: slightly incorrect */
2585 while (ch1 != '\n' && ch1 != CH_EOF)
2586 inp();
2587 #endif
2588 } else {
2589 /* computed #include : either we have only strings or
2590 we have anything enclosed in '<>' */
2591 next();
2592 buf[0] = '\0';
2593 if (tok == TOK_STR) {
2594 while (tok != TOK_LINEFEED) {
2595 if (tok != TOK_STR) {
2596 include_syntax:
2597 error("'#include' expects \"FILENAME\" or <FILENAME>");
2599 pstrcat(buf, sizeof(buf), (char *)tokc.cstr->data);
2600 next();
2602 c = '\"';
2603 } else {
2604 int len;
2605 while (tok != TOK_LINEFEED) {
2606 pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc));
2607 next();
2609 len = strlen(buf);
2610 /* check syntax and remove '<>' */
2611 if (len < 2 || buf[0] != '<' || buf[len - 1] != '>')
2612 goto include_syntax;
2613 memmove(buf, buf + 1, len - 2);
2614 buf[len - 2] = '\0';
2615 c = '>';
2619 e = search_cached_include(s1, c, buf);
2620 if (e && define_find(e->ifndef_macro)) {
2621 /* no need to parse the include because the 'ifndef macro'
2622 is defined */
2623 #ifdef INC_DEBUG
2624 printf("%s: skipping %s\n", file->filename, buf);
2625 #endif
2626 } else {
2627 if (c == '\"') {
2628 /* first search in current dir if "header.h" */
2629 size = 0;
2630 p = strrchr(file->filename, '/');
2631 if (p)
2632 size = p + 1 - file->filename;
2633 if (size > sizeof(buf1) - 1)
2634 size = sizeof(buf1) - 1;
2635 memcpy(buf1, file->filename, size);
2636 buf1[size] = '\0';
2637 pstrcat(buf1, sizeof(buf1), buf);
2638 f = tcc_open(s1, buf1);
2639 if (f)
2640 goto found;
2642 if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
2643 error("#include recursion too deep");
2644 /* now search in all the include paths */
2645 n = s1->nb_include_paths + s1->nb_sysinclude_paths;
2646 for(i = 0; i < n; i++) {
2647 const char *path;
2648 if (i < s1->nb_include_paths)
2649 path = s1->include_paths[i];
2650 else
2651 path = s1->sysinclude_paths[i - s1->nb_include_paths];
2652 pstrcpy(buf1, sizeof(buf1), path);
2653 pstrcat(buf1, sizeof(buf1), "/");
2654 pstrcat(buf1, sizeof(buf1), buf);
2655 f = tcc_open(s1, buf1);
2656 if (f)
2657 goto found;
2659 error("include file '%s' not found", buf);
2660 f = NULL;
2661 found:
2662 #ifdef INC_DEBUG
2663 printf("%s: including %s\n", file->filename, buf1);
2664 #endif
2665 f->inc_type = c;
2666 pstrcpy(f->inc_filename, sizeof(f->inc_filename), buf);
2667 /* push current file in stack */
2668 /* XXX: fix current line init */
2669 *s1->include_stack_ptr++ = file;
2670 file = f;
2671 /* add include file debug info */
2672 if (do_debug) {
2673 put_stabs(file->filename, N_BINCL, 0, 0, 0);
2675 tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
2676 ch = file->buf_ptr[0];
2677 goto the_end;
2679 break;
2680 case TOK_IFNDEF:
2681 c = 1;
2682 goto do_ifdef;
2683 case TOK_IF:
2684 c = expr_preprocess();
2685 goto do_if;
2686 case TOK_IFDEF:
2687 c = 0;
2688 do_ifdef:
2689 next_nomacro();
2690 if (tok < TOK_IDENT)
2691 error("invalid argument for '#if%sdef'", c ? "n" : "");
2692 if (is_bof) {
2693 if (c) {
2694 #ifdef INC_DEBUG
2695 printf("#ifndef %s\n", get_tok_str(tok, NULL));
2696 #endif
2697 file->ifndef_macro = tok;
2700 c = (define_find(tok) != 0) ^ c;
2701 do_if:
2702 if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE)
2703 error("memory full");
2704 *s1->ifdef_stack_ptr++ = c;
2705 goto test_skip;
2706 case TOK_ELSE:
2707 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
2708 error("#else without matching #if");
2709 if (s1->ifdef_stack_ptr[-1] & 2)
2710 error("#else after #else");
2711 c = (s1->ifdef_stack_ptr[-1] ^= 3);
2712 goto test_skip;
2713 case TOK_ELIF:
2714 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
2715 error("#elif without matching #if");
2716 c = s1->ifdef_stack_ptr[-1];
2717 if (c > 1)
2718 error("#elif after #else");
2719 /* last #if/#elif expression was true: we skip */
2720 if (c == 1)
2721 goto skip;
2722 c = expr_preprocess();
2723 s1->ifdef_stack_ptr[-1] = c;
2724 test_skip:
2725 if (!(c & 1)) {
2726 skip:
2727 preprocess_skip();
2728 is_bof = 0;
2729 goto redo;
2731 break;
2732 case TOK_ENDIF:
2733 if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)
2734 error("#endif without matching #if");
2735 s1->ifdef_stack_ptr--;
2736 /* '#ifndef macro' was at the start of file. Now we check if
2737 an '#endif' is exactly at the end of file */
2738 if (file->ifndef_macro &&
2739 s1->ifdef_stack_ptr == file->ifdef_stack_ptr) {
2740 file->ifndef_macro_saved = file->ifndef_macro;
2741 /* need to set to zero to avoid false matches if another
2742 #ifndef at middle of file */
2743 file->ifndef_macro = 0;
2744 while (tok != TOK_LINEFEED)
2745 next_nomacro();
2746 tok_flags |= TOK_FLAG_ENDIF;
2747 goto the_end;
2749 break;
2750 case TOK_LINE:
2751 next();
2752 if (tok != TOK_CINT)
2753 error("#line");
2754 file->line_num = tokc.i - 1; /* the line number will be incremented after */
2755 next();
2756 if (tok != TOK_LINEFEED) {
2757 if (tok != TOK_STR)
2758 error("#line");
2759 pstrcpy(file->filename, sizeof(file->filename),
2760 (char *)tokc.cstr->data);
2762 break;
2763 case TOK_ERROR:
2764 case TOK_WARNING:
2765 c = tok;
2766 ch = file->buf_ptr[0];
2767 skip_spaces();
2768 q = buf;
2769 while (ch != '\n' && ch != CH_EOF) {
2770 if ((q - buf) < sizeof(buf) - 1)
2771 *q++ = ch;
2772 minp();
2774 *q = '\0';
2775 if (c == TOK_ERROR)
2776 error("#error %s", buf);
2777 else
2778 warning("#warning %s", buf);
2779 break;
2780 case TOK_PRAGMA:
2781 /* ignored */
2782 break;
2783 default:
2784 if (tok == TOK_LINEFEED || tok == '!' || tok == TOK_CINT) {
2785 /* '!' is ignored to allow C scripts. numbers are ignored
2786 to emulate cpp behaviour */
2787 } else {
2788 error("invalid preprocessing directive #%s", get_tok_str(tok, &tokc));
2790 break;
2792 /* ignore other preprocess commands or #! for C scripts */
2793 while (tok != TOK_LINEFEED)
2794 next_nomacro();
2795 the_end:
2796 parse_flags = saved_parse_flags;
2799 /* evaluate escape codes in a string. */
2800 static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long)
2802 int c, n;
2803 const char *p;
2805 p = buf;
2806 for(;;) {
2807 c = *p;
2808 if (c == '\0')
2809 break;
2810 if (c == '\\') {
2811 p++;
2812 /* escape */
2813 c = *p;
2814 switch(c) {
2815 case '0': case '1': case '2': case '3':
2816 case '4': case '5': case '6': case '7':
2817 /* at most three octal digits */
2818 n = c - '0';
2819 p++;
2820 c = *p;
2821 if (isoct(c)) {
2822 n = n * 8 + c - '0';
2823 p++;
2824 c = *p;
2825 if (isoct(c)) {
2826 n = n * 8 + c - '0';
2827 p++;
2830 c = n;
2831 goto add_char_nonext;
2832 case 'x':
2833 p++;
2834 n = 0;
2835 for(;;) {
2836 c = *p;
2837 if (c >= 'a' && c <= 'f')
2838 c = c - 'a' + 10;
2839 else if (c >= 'A' && c <= 'F')
2840 c = c - 'A' + 10;
2841 else if (isnum(c))
2842 c = c - '0';
2843 else
2844 break;
2845 n = n * 16 + c;
2846 p++;
2848 c = n;
2849 goto add_char_nonext;
2850 case 'a':
2851 c = '\a';
2852 break;
2853 case 'b':
2854 c = '\b';
2855 break;
2856 case 'f':
2857 c = '\f';
2858 break;
2859 case 'n':
2860 c = '\n';
2861 break;
2862 case 'r':
2863 c = '\r';
2864 break;
2865 case 't':
2866 c = '\t';
2867 break;
2868 case 'v':
2869 c = '\v';
2870 break;
2871 case 'e':
2872 if (!gnu_ext)
2873 goto invalid_escape;
2874 c = 27;
2875 break;
2876 case '\'':
2877 case '\"':
2878 case '\\':
2879 case '?':
2880 break;
2881 default:
2882 invalid_escape:
2883 error("invalid escaped char");
2886 p++;
2887 add_char_nonext:
2888 if (!is_long)
2889 cstr_ccat(outstr, c);
2890 else
2891 cstr_wccat(outstr, c);
2893 /* add a trailing '\0' */
2894 if (!is_long)
2895 cstr_ccat(outstr, '\0');
2896 else
2897 cstr_wccat(outstr, '\0');
2900 /* we use 64 bit numbers */
2901 #define BN_SIZE 2
2903 /* bn = (bn << shift) | or_val */
2904 void bn_lshift(unsigned int *bn, int shift, int or_val)
2906 int i;
2907 unsigned int v;
2908 for(i=0;i<BN_SIZE;i++) {
2909 v = bn[i];
2910 bn[i] = (v << shift) | or_val;
2911 or_val = v >> (32 - shift);
2915 void bn_zero(unsigned int *bn)
2917 int i;
2918 for(i=0;i<BN_SIZE;i++) {
2919 bn[i] = 0;
2923 /* parse number in null terminated string 'p' and return it in the
2924 current token */
2925 void parse_number(const char *p)
2927 int b, t, shift, frac_bits, s, exp_val, ch;
2928 char *q;
2929 unsigned int bn[BN_SIZE];
2930 double d;
2932 /* number */
2933 q = token_buf;
2934 ch = *p++;
2935 t = ch;
2936 ch = *p++;
2937 *q++ = t;
2938 b = 10;
2939 if (t == '.') {
2940 goto float_frac_parse;
2941 } else if (t == '0') {
2942 if (ch == 'x' || ch == 'X') {
2943 q--;
2944 ch = *p++;
2945 b = 16;
2946 } else if (tcc_ext && (ch == 'b' || ch == 'B')) {
2947 q--;
2948 ch = *p++;
2949 b = 2;
2952 /* parse all digits. cannot check octal numbers at this stage
2953 because of floating point constants */
2954 while (1) {
2955 if (ch >= 'a' && ch <= 'f')
2956 t = ch - 'a' + 10;
2957 else if (ch >= 'A' && ch <= 'F')
2958 t = ch - 'A' + 10;
2959 else if (isnum(ch))
2960 t = ch - '0';
2961 else
2962 break;
2963 if (t >= b)
2964 break;
2965 if (q >= token_buf + STRING_MAX_SIZE) {
2966 num_too_long:
2967 error("number too long");
2969 *q++ = ch;
2970 ch = *p++;
2972 if (ch == '.' ||
2973 ((ch == 'e' || ch == 'E') && b == 10) ||
2974 ((ch == 'p' || ch == 'P') && (b == 16 || b == 2))) {
2975 if (b != 10) {
2976 /* NOTE: strtox should support that for hexa numbers, but
2977 non ISOC99 libcs do not support it, so we prefer to do
2978 it by hand */
2979 /* hexadecimal or binary floats */
2980 /* XXX: handle overflows */
2981 *q = '\0';
2982 if (b == 16)
2983 shift = 4;
2984 else
2985 shift = 2;
2986 bn_zero(bn);
2987 q = token_buf;
2988 while (1) {
2989 t = *q++;
2990 if (t == '\0') {
2991 break;
2992 } else if (t >= 'a') {
2993 t = t - 'a' + 10;
2994 } else if (t >= 'A') {
2995 t = t - 'A' + 10;
2996 } else {
2997 t = t - '0';
2999 bn_lshift(bn, shift, t);
3001 frac_bits = 0;
3002 if (ch == '.') {
3003 ch = *p++;
3004 while (1) {
3005 t = ch;
3006 if (t >= 'a' && t <= 'f') {
3007 t = t - 'a' + 10;
3008 } else if (t >= 'A' && t <= 'F') {
3009 t = t - 'A' + 10;
3010 } else if (t >= '0' && t <= '9') {
3011 t = t - '0';
3012 } else {
3013 break;
3015 if (t >= b)
3016 error("invalid digit");
3017 bn_lshift(bn, shift, t);
3018 frac_bits += shift;
3019 ch = *p++;
3022 if (ch != 'p' && ch != 'P')
3023 expect("exponent");
3024 ch = *p++;
3025 s = 1;
3026 exp_val = 0;
3027 if (ch == '+') {
3028 ch = *p++;
3029 } else if (ch == '-') {
3030 s = -1;
3031 ch = *p++;
3033 if (ch < '0' || ch > '9')
3034 expect("exponent digits");
3035 while (ch >= '0' && ch <= '9') {
3036 exp_val = exp_val * 10 + ch - '0';
3037 ch = *p++;
3039 exp_val = exp_val * s;
3041 /* now we can generate the number */
3042 /* XXX: should patch directly float number */
3043 d = (double)bn[1] * 4294967296.0 + (double)bn[0];
3044 d = ldexp(d, exp_val - frac_bits);
3045 t = toup(ch);
3046 if (t == 'F') {
3047 ch = *p++;
3048 tok = TOK_CFLOAT;
3049 /* float : should handle overflow */
3050 tokc.f = (float)d;
3051 } else if (t == 'L') {
3052 ch = *p++;
3053 tok = TOK_CLDOUBLE;
3054 /* XXX: not large enough */
3055 tokc.ld = (long double)d;
3056 } else {
3057 tok = TOK_CDOUBLE;
3058 tokc.d = d;
3060 } else {
3061 /* decimal floats */
3062 if (ch == '.') {
3063 if (q >= token_buf + STRING_MAX_SIZE)
3064 goto num_too_long;
3065 *q++ = ch;
3066 ch = *p++;
3067 float_frac_parse:
3068 while (ch >= '0' && ch <= '9') {
3069 if (q >= token_buf + STRING_MAX_SIZE)
3070 goto num_too_long;
3071 *q++ = ch;
3072 ch = *p++;
3075 if (ch == 'e' || ch == 'E') {
3076 if (q >= token_buf + STRING_MAX_SIZE)
3077 goto num_too_long;
3078 *q++ = ch;
3079 ch = *p++;
3080 if (ch == '-' || ch == '+') {
3081 if (q >= token_buf + STRING_MAX_SIZE)
3082 goto num_too_long;
3083 *q++ = ch;
3084 ch = *p++;
3086 if (ch < '0' || ch > '9')
3087 expect("exponent digits");
3088 while (ch >= '0' && ch <= '9') {
3089 if (q >= token_buf + STRING_MAX_SIZE)
3090 goto num_too_long;
3091 *q++ = ch;
3092 ch = *p++;
3095 *q = '\0';
3096 t = toup(ch);
3097 errno = 0;
3098 if (t == 'F') {
3099 ch = *p++;
3100 tok = TOK_CFLOAT;
3101 tokc.f = strtof(token_buf, NULL);
3102 } else if (t == 'L') {
3103 ch = *p++;
3104 tok = TOK_CLDOUBLE;
3105 tokc.ld = strtold(token_buf, NULL);
3106 } else {
3107 tok = TOK_CDOUBLE;
3108 tokc.d = strtod(token_buf, NULL);
3111 } else {
3112 unsigned long long n, n1;
3113 int lcount, ucount;
3115 /* integer number */
3116 *q = '\0';
3117 q = token_buf;
3118 if (b == 10 && *q == '0') {
3119 b = 8;
3120 q++;
3122 n = 0;
3123 while(1) {
3124 t = *q++;
3125 /* no need for checks except for base 10 / 8 errors */
3126 if (t == '\0') {
3127 break;
3128 } else if (t >= 'a') {
3129 t = t - 'a' + 10;
3130 } else if (t >= 'A') {
3131 t = t - 'A' + 10;
3132 } else {
3133 t = t - '0';
3134 if (t >= b)
3135 error("invalid digit");
3137 n1 = n;
3138 n = n * b + t;
3139 /* detect overflow */
3140 /* XXX: this test is not reliable */
3141 if (n < n1)
3142 error("integer constant overflow");
3145 /* XXX: not exactly ANSI compliant */
3146 if ((n & 0xffffffff00000000LL) != 0) {
3147 if ((n >> 63) != 0)
3148 tok = TOK_CULLONG;
3149 else
3150 tok = TOK_CLLONG;
3151 } else if (n > 0x7fffffff) {
3152 tok = TOK_CUINT;
3153 } else {
3154 tok = TOK_CINT;
3156 lcount = 0;
3157 ucount = 0;
3158 for(;;) {
3159 t = toup(ch);
3160 if (t == 'L') {
3161 if (lcount >= 2)
3162 error("three 'l's in integer constant");
3163 lcount++;
3164 if (lcount == 2) {
3165 if (tok == TOK_CINT)
3166 tok = TOK_CLLONG;
3167 else if (tok == TOK_CUINT)
3168 tok = TOK_CULLONG;
3170 ch = *p++;
3171 } else if (t == 'U') {
3172 if (ucount >= 1)
3173 error("two 'u's in integer constant");
3174 ucount++;
3175 if (tok == TOK_CINT)
3176 tok = TOK_CUINT;
3177 else if (tok == TOK_CLLONG)
3178 tok = TOK_CULLONG;
3179 ch = *p++;
3180 } else {
3181 break;
3184 if (tok == TOK_CINT || tok == TOK_CUINT)
3185 tokc.ui = n;
3186 else
3187 tokc.ull = n;
3192 #define PARSE2(c1, tok1, c2, tok2) \
3193 case c1: \
3194 PEEKC(c, p); \
3195 if (c == c2) { \
3196 p++; \
3197 tok = tok2; \
3198 } else { \
3199 tok = tok1; \
3201 break;
3203 /* return next token without macro substitution */
3204 static inline void next_nomacro1(void)
3206 int t, c, is_long;
3207 TokenSym *ts;
3208 uint8_t *p, *p1;
3209 unsigned int h;
3211 p = file->buf_ptr;
3212 redo_no_start:
3213 c = *p;
3214 switch(c) {
3215 case ' ':
3216 case '\t':
3217 case '\f':
3218 case '\v':
3219 case '\r':
3220 p++;
3221 goto redo_no_start;
3223 case '\\':
3224 /* first look if it is in fact an end of buffer */
3225 if (p >= file->buf_end) {
3226 file->buf_ptr = p;
3227 handle_eob();
3228 p = file->buf_ptr;
3229 if (p >= file->buf_end)
3230 goto parse_eof;
3231 else
3232 goto redo_no_start;
3233 } else {
3234 file->buf_ptr = p;
3235 ch = *p;
3236 handle_stray();
3237 p = file->buf_ptr;
3238 goto redo_no_start;
3240 parse_eof:
3242 TCCState *s1 = tcc_state;
3243 if (parse_flags & PARSE_FLAG_LINEFEED) {
3244 tok = TOK_LINEFEED;
3245 } else if (s1->include_stack_ptr == s1->include_stack ||
3246 !(parse_flags & PARSE_FLAG_PREPROCESS)) {
3247 /* no include left : end of file. */
3248 tok = TOK_EOF;
3249 } else {
3250 /* pop include file */
3252 /* test if previous '#endif' was after a #ifdef at
3253 start of file */
3254 if (tok_flags & TOK_FLAG_ENDIF) {
3255 #ifdef INC_DEBUG
3256 printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
3257 #endif
3258 add_cached_include(s1, file->inc_type, file->inc_filename,
3259 file->ifndef_macro_saved);
3262 /* add end of include file debug info */
3263 if (do_debug) {
3264 put_stabd(N_EINCL, 0, 0);
3266 /* pop include stack */
3267 tcc_close(file);
3268 s1->include_stack_ptr--;
3269 file = *s1->include_stack_ptr;
3270 p = file->buf_ptr;
3271 goto redo_no_start;
3274 break;
3276 case '\n':
3277 if (parse_flags & PARSE_FLAG_LINEFEED) {
3278 tok = TOK_LINEFEED;
3279 } else {
3280 file->line_num++;
3281 tok_flags |= TOK_FLAG_BOL;
3282 p++;
3283 goto redo_no_start;
3285 break;
3287 case '#':
3288 /* XXX: simplify */
3289 PEEKC(c, p);
3290 if ((tok_flags & TOK_FLAG_BOL) &&
3291 (parse_flags & PARSE_FLAG_PREPROCESS)) {
3292 file->buf_ptr = p;
3293 preprocess(tok_flags & TOK_FLAG_BOF);
3294 p = file->buf_ptr;
3295 goto redo_no_start;
3296 } else {
3297 if (c == '#') {
3298 p++;
3299 tok = TOK_TWOSHARPS;
3300 } else {
3301 tok = '#';
3304 break;
3306 case 'a': case 'b': case 'c': case 'd':
3307 case 'e': case 'f': case 'g': case 'h':
3308 case 'i': case 'j': case 'k': case 'l':
3309 case 'm': case 'n': case 'o': case 'p':
3310 case 'q': case 'r': case 's': case 't':
3311 case 'u': case 'v': case 'w': case 'x':
3312 case 'y': case 'z':
3313 case 'A': case 'B': case 'C': case 'D':
3314 case 'E': case 'F': case 'G': case 'H':
3315 case 'I': case 'J': case 'K':
3316 case 'M': case 'N': case 'O': case 'P':
3317 case 'Q': case 'R': case 'S': case 'T':
3318 case 'U': case 'V': case 'W': case 'X':
3319 case 'Y': case 'Z':
3320 case '_':
3321 parse_ident_fast:
3322 p1 = p;
3323 h = TOK_HASH_INIT;
3324 h = TOK_HASH_FUNC(h, c);
3325 p++;
3326 for(;;) {
3327 c = *p;
3328 if (!isidnum_table[c])
3329 break;
3330 h = TOK_HASH_FUNC(h, c);
3331 p++;
3333 if (c != '\\') {
3334 TokenSym **pts;
3335 int len;
3337 /* fast case : no stray found, so we have the full token
3338 and we have already hashed it */
3339 len = p - p1;
3340 h &= (TOK_HASH_SIZE - 1);
3341 pts = &hash_ident[h];
3342 for(;;) {
3343 ts = *pts;
3344 if (!ts)
3345 break;
3346 if (ts->len == len && !memcmp(ts->str, p1, len))
3347 goto token_found;
3348 pts = &(ts->hash_next);
3350 ts = tok_alloc_new(pts, p1, len);
3351 token_found: ;
3352 } else {
3353 /* slower case */
3354 cstr_reset(&tokcstr);
3356 while (p1 < p) {
3357 cstr_ccat(&tokcstr, *p1);
3358 p1++;
3360 p--;
3361 PEEKC(c, p);
3362 parse_ident_slow:
3363 while (isidnum_table[c]) {
3364 cstr_ccat(&tokcstr, c);
3365 PEEKC(c, p);
3367 ts = tok_alloc(tokcstr.data, tokcstr.size);
3369 tok = ts->tok;
3370 break;
3371 case 'L':
3372 t = p[1];
3373 if (t != '\\' && t != '\'' && t != '\"') {
3374 /* fast case */
3375 goto parse_ident_fast;
3376 } else {
3377 PEEKC(c, p);
3378 if (c == '\'' || c == '\"') {
3379 is_long = 1;
3380 goto str_const;
3381 } else {
3382 cstr_reset(&tokcstr);
3383 cstr_ccat(&tokcstr, 'L');
3384 goto parse_ident_slow;
3387 break;
3388 case '0': case '1': case '2': case '3':
3389 case '4': case '5': case '6': case '7':
3390 case '8': case '9':
3392 cstr_reset(&tokcstr);
3393 /* after the first digit, accept digits, alpha, '.' or sign if
3394 prefixed by 'eEpP' */
3395 parse_num:
3396 for(;;) {
3397 t = c;
3398 cstr_ccat(&tokcstr, c);
3399 PEEKC(c, p);
3400 if (!(isnum(c) || isid(c) || c == '.' ||
3401 ((c == '+' || c == '-') &&
3402 (t == 'e' || t == 'E' || t == 'p' || t == 'P'))))
3403 break;
3405 /* We add a trailing '\0' to ease parsing */
3406 cstr_ccat(&tokcstr, '\0');
3407 tokc.cstr = &tokcstr;
3408 tok = TOK_PPNUM;
3409 break;
3410 case '.':
3411 /* special dot handling because it can also start a number */
3412 PEEKC(c, p);
3413 if (isnum(c)) {
3414 cstr_reset(&tokcstr);
3415 cstr_ccat(&tokcstr, '.');
3416 goto parse_num;
3417 } else if (c == '.') {
3418 PEEKC(c, p);
3419 if (c != '.')
3420 expect("'.'");
3421 PEEKC(c, p);
3422 tok = TOK_DOTS;
3423 } else {
3424 tok = '.';
3426 break;
3427 case '\'':
3428 case '\"':
3429 is_long = 0;
3430 str_const:
3432 CString str;
3433 int sep;
3435 sep = c;
3437 /* parse the string */
3438 cstr_new(&str);
3439 p = parse_pp_string(p, sep, &str);
3440 cstr_ccat(&str, '\0');
3442 /* eval the escape (should be done as TOK_PPNUM) */
3443 cstr_reset(&tokcstr);
3444 parse_escape_string(&tokcstr, str.data, is_long);
3445 cstr_free(&str);
3447 if (sep == '\'') {
3448 int char_size;
3449 /* XXX: make it portable */
3450 if (!is_long)
3451 char_size = 1;
3452 else
3453 char_size = sizeof(int);
3454 if (tokcstr.size <= char_size)
3455 error("empty character constant");
3456 if (tokcstr.size > 2 * char_size)
3457 warning("multi-character character constant");
3458 if (!is_long) {
3459 tokc.i = *(int8_t *)tokcstr.data;
3460 tok = TOK_CCHAR;
3461 } else {
3462 tokc.i = *(int *)tokcstr.data;
3463 tok = TOK_LCHAR;
3465 } else {
3466 tokc.cstr = &tokcstr;
3467 if (!is_long)
3468 tok = TOK_STR;
3469 else
3470 tok = TOK_LSTR;
3473 break;
3475 case '<':
3476 PEEKC(c, p);
3477 if (c == '=') {
3478 p++;
3479 tok = TOK_LE;
3480 } else if (c == '<') {
3481 PEEKC(c, p);
3482 if (c == '=') {
3483 p++;
3484 tok = TOK_A_SHL;
3485 } else {
3486 tok = TOK_SHL;
3488 } else {
3489 tok = TOK_LT;
3491 break;
3493 case '>':
3494 PEEKC(c, p);
3495 if (c == '=') {
3496 p++;
3497 tok = TOK_GE;
3498 } else if (c == '>') {
3499 PEEKC(c, p);
3500 if (c == '=') {
3501 p++;
3502 tok = TOK_A_SAR;
3503 } else {
3504 tok = TOK_SAR;
3506 } else {
3507 tok = TOK_GT;
3509 break;
3511 case '&':
3512 PEEKC(c, p);
3513 if (c == '&') {
3514 p++;
3515 tok = TOK_LAND;
3516 } else if (c == '=') {
3517 p++;
3518 tok = TOK_A_AND;
3519 } else {
3520 tok = '&';
3522 break;
3524 case '|':
3525 PEEKC(c, p);
3526 if (c == '|') {
3527 p++;
3528 tok = TOK_LOR;
3529 } else if (c == '=') {
3530 p++;
3531 tok = TOK_A_OR;
3532 } else {
3533 tok = '|';
3535 break;
3537 case '+':
3538 PEEKC(c, p);
3539 if (c == '+') {
3540 p++;
3541 tok = TOK_INC;
3542 } else if (c == '=') {
3543 p++;
3544 tok = TOK_A_ADD;
3545 } else {
3546 tok = '+';
3548 break;
3550 case '-':
3551 PEEKC(c, p);
3552 if (c == '-') {
3553 p++;
3554 tok = TOK_DEC;
3555 } else if (c == '=') {
3556 p++;
3557 tok = TOK_A_SUB;
3558 } else if (c == '>') {
3559 p++;
3560 tok = TOK_ARROW;
3561 } else {
3562 tok = '-';
3564 break;
3566 PARSE2('!', '!', '=', TOK_NE)
3567 PARSE2('=', '=', '=', TOK_EQ)
3568 PARSE2('*', '*', '=', TOK_A_MUL)
3569 PARSE2('%', '%', '=', TOK_A_MOD)
3570 PARSE2('^', '^', '=', TOK_A_XOR)
3572 /* comments or operator */
3573 case '/':
3574 PEEKC(c, p);
3575 if (c == '*') {
3576 p = parse_comment(p);
3577 goto redo_no_start;
3578 } else if (c == '/') {
3579 p = parse_line_comment(p);
3580 goto redo_no_start;
3581 } else if (c == '=') {
3582 p++;
3583 tok = TOK_A_DIV;
3584 } else {
3585 tok = '/';
3587 break;
3589 /* simple tokens */
3590 case '(':
3591 case ')':
3592 case '[':
3593 case ']':
3594 case '{':
3595 case '}':
3596 case ',':
3597 case ';':
3598 case ':':
3599 case '?':
3600 case '~':
3601 case '$': /* only used in assembler */
3602 tok = c;
3603 p++;
3604 break;
3605 default:
3606 error("unrecognized character \\x%02x", c);
3607 break;
3609 file->buf_ptr = p;
3610 tok_flags = 0;
3611 #if defined(PARSE_DEBUG)
3612 printf("token = %s\n", get_tok_str(tok, &tokc));
3613 #endif
3616 /* return next token without macro substitution. Can read input from
3617 macro_ptr buffer */
3618 static void next_nomacro(void)
3620 if (macro_ptr) {
3621 redo:
3622 tok = *macro_ptr;
3623 if (tok) {
3624 TOK_GET(tok, macro_ptr, tokc);
3625 if (tok == TOK_LINENUM) {
3626 file->line_num = tokc.i;
3627 goto redo;
3630 } else {
3631 next_nomacro1();
3635 /* substitute args in macro_str and return allocated string */
3636 static int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args)
3638 int *st, last_tok, t, notfirst;
3639 Sym *s;
3640 CValue cval;
3641 TokenString str;
3642 CString cstr;
3644 tok_str_new(&str);
3645 last_tok = 0;
3646 while(1) {
3647 TOK_GET(t, macro_str, cval);
3648 if (!t)
3649 break;
3650 if (t == '#') {
3651 /* stringize */
3652 TOK_GET(t, macro_str, cval);
3653 if (!t)
3654 break;
3655 s = sym_find2(args, t);
3656 if (s) {
3657 cstr_new(&cstr);
3658 st = (int *)s->c;
3659 notfirst = 0;
3660 while (*st) {
3661 if (notfirst)
3662 cstr_ccat(&cstr, ' ');
3663 TOK_GET(t, st, cval);
3664 cstr_cat(&cstr, get_tok_str(t, &cval));
3665 notfirst = 1;
3667 cstr_ccat(&cstr, '\0');
3668 #ifdef PP_DEBUG
3669 printf("stringize: %s\n", (char *)cstr.data);
3670 #endif
3671 /* add string */
3672 cval.cstr = &cstr;
3673 tok_str_add2(&str, TOK_STR, &cval);
3674 cstr_free(&cstr);
3675 } else {
3676 tok_str_add2(&str, t, &cval);
3678 } else if (t >= TOK_IDENT) {
3679 s = sym_find2(args, t);
3680 if (s) {
3681 st = (int *)s->c;
3682 /* if '##' is present before or after, no arg substitution */
3683 if (*macro_str == TOK_TWOSHARPS || last_tok == TOK_TWOSHARPS) {
3684 /* special case for var arg macros : ## eats the
3685 ',' if empty VA_ARGS variable. */
3686 /* XXX: test of the ',' is not 100%
3687 reliable. should fix it to avoid security
3688 problems */
3689 if (gnu_ext && s->type.t &&
3690 last_tok == TOK_TWOSHARPS &&
3691 str.len >= 2 && str.str[str.len - 2] == ',') {
3692 if (*st == 0) {
3693 /* suppress ',' '##' */
3694 str.len -= 2;
3695 } else {
3696 /* suppress '##' and add variable */
3697 str.len--;
3698 goto add_var;
3700 } else {
3701 int t1;
3702 add_var:
3703 for(;;) {
3704 TOK_GET(t1, st, cval);
3705 if (!t1)
3706 break;
3707 tok_str_add2(&str, t1, &cval);
3710 } else {
3711 macro_subst(&str, nested_list, st);
3713 } else {
3714 tok_str_add(&str, t);
3716 } else {
3717 tok_str_add2(&str, t, &cval);
3719 last_tok = t;
3721 tok_str_add(&str, 0);
3722 return str.str;
3725 static char const ab_month_name[12][4] =
3727 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
3728 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
3731 /* do macro substitution of current token with macro 's' and add
3732 result to (tok_str,tok_len). 'nested_list' is the list of all
3733 macros we got inside to avoid recursing. Return non zero if no
3734 substitution needs to be done */
3735 static int macro_subst_tok(TokenString *tok_str,
3736 Sym **nested_list, Sym *s)
3738 Sym *args, *sa, *sa1;
3739 int mstr_allocated, parlevel, *mstr, t;
3740 TokenString str;
3741 char *cstrval;
3742 CValue cval;
3743 CString cstr;
3745 /* if symbol is a macro, prepare substitution */
3747 /* special macros */
3748 if (tok == TOK___LINE__) {
3749 cval.i = file->line_num;
3750 tok_str_add2(tok_str, TOK_CINT, &cval);
3751 } else if (tok == TOK___FILE__) {
3752 cstrval = file->filename;
3753 goto add_cstr;
3754 tok_str_add2(tok_str, TOK_STR, &cval);
3755 } else if (tok == TOK___DATE__ || tok == TOK___TIME__) {
3756 time_t ti;
3757 struct tm *tm;
3758 char buf[64];
3760 time(&ti);
3761 tm = localtime(&ti);
3762 if (tok == TOK___DATE__) {
3763 snprintf(buf, sizeof(buf), "%s %2d %d",
3764 ab_month_name[tm->tm_mon], tm->tm_mday, tm->tm_year + 1900);
3765 } else {
3766 snprintf(buf, sizeof(buf), "%02d:%02d:%02d",
3767 tm->tm_hour, tm->tm_min, tm->tm_sec);
3769 cstrval = buf;
3770 add_cstr:
3771 cstr_new(&cstr);
3772 cstr_cat(&cstr, cstrval);
3773 cstr_ccat(&cstr, '\0');
3774 cval.cstr = &cstr;
3775 tok_str_add2(tok_str, TOK_STR, &cval);
3776 cstr_free(&cstr);
3777 } else {
3778 mstr = (int *)s->c;
3779 mstr_allocated = 0;
3780 if (s->type.t == MACRO_FUNC) {
3781 /* NOTE: we do not use next_nomacro to avoid eating the
3782 next token. XXX: find better solution */
3783 if (macro_ptr) {
3784 t = *macro_ptr;
3785 if (t == 0) {
3786 /* end of macro stream: we must look at the token
3787 after in the file */
3788 macro_ptr = NULL;
3789 goto parse_stream;
3791 } else {
3792 parse_stream:
3793 /* XXX: incorrect with comments */
3794 ch = file->buf_ptr[0];
3795 while (is_space(ch) || ch == '\n')
3796 cinp();
3797 t = ch;
3799 if (t != '(') /* no macro subst */
3800 return -1;
3802 /* argument macro */
3803 next_nomacro();
3804 next_nomacro();
3805 args = NULL;
3806 sa = s->next;
3807 /* NOTE: empty args are allowed, except if no args */
3808 for(;;) {
3809 /* handle '()' case */
3810 if (!args && tok == ')')
3811 break;
3812 if (!sa)
3813 error("macro '%s' used with too many args",
3814 get_tok_str(s->v, 0));
3815 tok_str_new(&str);
3816 parlevel = 0;
3817 /* NOTE: non zero sa->t indicates VA_ARGS */
3818 while ((parlevel > 0 ||
3819 (tok != ')' &&
3820 (tok != ',' || sa->type.t))) &&
3821 tok != -1) {
3822 if (tok == '(')
3823 parlevel++;
3824 else if (tok == ')')
3825 parlevel--;
3826 tok_str_add2(&str, tok, &tokc);
3827 next_nomacro();
3829 tok_str_add(&str, 0);
3830 sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, (int)str.str);
3831 sa = sa->next;
3832 if (tok == ')') {
3833 /* special case for gcc var args: add an empty
3834 var arg argument if it is omitted */
3835 if (sa && sa->type.t && gnu_ext)
3836 continue;
3837 else
3838 break;
3840 if (tok != ',')
3841 expect(",");
3842 next_nomacro();
3844 if (sa) {
3845 error("macro '%s' used with too few args",
3846 get_tok_str(s->v, 0));
3849 /* now subst each arg */
3850 mstr = macro_arg_subst(nested_list, mstr, args);
3851 /* free memory */
3852 sa = args;
3853 while (sa) {
3854 sa1 = sa->prev;
3855 tok_str_free((int *)sa->c);
3856 tcc_free(sa);
3857 sa = sa1;
3859 mstr_allocated = 1;
3861 sym_push2(nested_list, s->v, 0, 0);
3862 macro_subst(tok_str, nested_list, mstr);
3863 /* pop nested defined symbol */
3864 sa1 = *nested_list;
3865 *nested_list = sa1->prev;
3866 tcc_free(sa1);
3867 if (mstr_allocated)
3868 tok_str_free(mstr);
3870 return 0;
3873 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
3874 return the resulting string (which must be freed). */
3875 static inline int *macro_twosharps(const int *macro_str)
3877 TokenSym *ts;
3878 const int *macro_ptr1, *start_macro_ptr, *ptr, *saved_macro_ptr;
3879 int t;
3880 const char *p1, *p2;
3881 CValue cval;
3882 TokenString macro_str1;
3883 CString cstr;
3885 start_macro_ptr = macro_str;
3886 /* we search the first '##' */
3887 for(;;) {
3888 macro_ptr1 = macro_str;
3889 TOK_GET(t, macro_str, cval);
3890 /* nothing more to do if end of string */
3891 if (t == 0)
3892 return NULL;
3893 if (*macro_str == TOK_TWOSHARPS)
3894 break;
3897 /* we saw '##', so we need more processing to handle it */
3898 cstr_new(&cstr);
3899 tok_str_new(&macro_str1);
3900 tok = t;
3901 tokc = cval;
3903 /* add all tokens seen so far */
3904 for(ptr = start_macro_ptr; ptr < macro_ptr1;) {
3905 TOK_GET(t, ptr, cval);
3906 tok_str_add2(&macro_str1, t, &cval);
3908 saved_macro_ptr = macro_ptr;
3909 /* XXX: get rid of the use of macro_ptr here */
3910 macro_ptr = (int *)macro_str;
3911 for(;;) {
3912 while (*macro_ptr == TOK_TWOSHARPS) {
3913 macro_ptr++;
3914 macro_ptr1 = macro_ptr;
3915 t = *macro_ptr;
3916 if (t) {
3917 TOK_GET(t, macro_ptr, cval);
3918 /* We concatenate the two tokens if we have an
3919 identifier or a preprocessing number */
3920 cstr_reset(&cstr);
3921 p1 = get_tok_str(tok, &tokc);
3922 cstr_cat(&cstr, p1);
3923 p2 = get_tok_str(t, &cval);
3924 cstr_cat(&cstr, p2);
3925 cstr_ccat(&cstr, '\0');
3927 if ((tok >= TOK_IDENT || tok == TOK_PPNUM) &&
3928 (t >= TOK_IDENT || t == TOK_PPNUM)) {
3929 if (tok == TOK_PPNUM) {
3930 /* if number, then create a number token */
3931 /* NOTE: no need to allocate because
3932 tok_str_add2() does it */
3933 tokc.cstr = &cstr;
3934 } else {
3935 /* if identifier, we must do a test to
3936 validate we have a correct identifier */
3937 if (t == TOK_PPNUM) {
3938 const char *p;
3939 int c;
3941 p = p2;
3942 for(;;) {
3943 c = *p;
3944 if (c == '\0')
3945 break;
3946 p++;
3947 if (!isnum(c) && !isid(c))
3948 goto error_pasting;
3951 ts = tok_alloc(cstr.data, strlen(cstr.data));
3952 tok = ts->tok; /* modify current token */
3954 } else {
3955 const char *str = cstr.data;
3956 const unsigned char *q;
3958 /* we look for a valid token */
3959 /* XXX: do more extensive checks */
3960 if (!strcmp(str, ">>=")) {
3961 tok = TOK_A_SAR;
3962 } else if (!strcmp(str, "<<=")) {
3963 tok = TOK_A_SHL;
3964 } else if (strlen(str) == 2) {
3965 /* search in two bytes table */
3966 q = tok_two_chars;
3967 for(;;) {
3968 if (!*q)
3969 goto error_pasting;
3970 if (q[0] == str[0] && q[1] == str[1])
3971 break;
3972 q += 3;
3974 tok = q[2];
3975 } else {
3976 error_pasting:
3977 /* NOTE: because get_tok_str use a static buffer,
3978 we must save it */
3979 cstr_reset(&cstr);
3980 p1 = get_tok_str(tok, &tokc);
3981 cstr_cat(&cstr, p1);
3982 cstr_ccat(&cstr, '\0');
3983 p2 = get_tok_str(t, &cval);
3984 warning("pasting \"%s\" and \"%s\" does not give a valid preprocessing token", cstr.data, p2);
3985 /* cannot merge tokens: just add them separately */
3986 tok_str_add2(&macro_str1, tok, &tokc);
3987 /* XXX: free associated memory ? */
3988 tok = t;
3989 tokc = cval;
3994 tok_str_add2(&macro_str1, tok, &tokc);
3995 next_nomacro();
3996 if (tok == 0)
3997 break;
3999 macro_ptr = (int *)saved_macro_ptr;
4000 cstr_free(&cstr);
4001 tok_str_add(&macro_str1, 0);
4002 return macro_str1.str;
4006 /* do macro substitution of macro_str and add result to
4007 (tok_str,tok_len). 'nested_list' is the list of all macros we got
4008 inside to avoid recursing. */
4009 static void macro_subst(TokenString *tok_str,
4010 Sym **nested_list, const int *macro_str)
4012 Sym *s;
4013 int *saved_macro_ptr, *macro_str1;
4014 const int *ptr;
4015 int t, ret;
4016 CValue cval;
4018 /* first scan for '##' operator handling */
4019 ptr = macro_str;
4020 macro_str1 = macro_twosharps(ptr);
4021 if (macro_str1)
4022 ptr = macro_str1;
4023 while (1) {
4024 /* NOTE: ptr == NULL can only happen if tokens are read from
4025 file stream due to a macro function call */
4026 if (ptr == NULL)
4027 break;
4028 TOK_GET(t, ptr, cval);
4029 if (t == 0)
4030 break;
4031 s = define_find(t);
4032 if (s != NULL) {
4033 /* if nested substitution, do nothing */
4034 if (sym_find2(*nested_list, t))
4035 goto no_subst;
4036 saved_macro_ptr = macro_ptr;
4037 macro_ptr = (int *)ptr;
4038 tok = t;
4039 ret = macro_subst_tok(tok_str, nested_list, s);
4040 ptr = (int *)macro_ptr;
4041 macro_ptr = saved_macro_ptr;
4042 if (ret != 0)
4043 goto no_subst;
4044 } else {
4045 no_subst:
4046 tok_str_add2(tok_str, t, &cval);
4049 if (macro_str1)
4050 tok_str_free(macro_str1);
4053 /* return next token with macro substitution */
4054 static void next(void)
4056 Sym *nested_list, *s;
4057 TokenString str;
4059 redo:
4060 next_nomacro();
4061 if (!macro_ptr) {
4062 /* if not reading from macro substituted string, then try
4063 to substitute macros */
4064 if (tok >= TOK_IDENT &&
4065 (parse_flags & PARSE_FLAG_PREPROCESS)) {
4066 s = define_find(tok);
4067 if (s) {
4068 /* we have a macro: we try to substitute */
4069 tok_str_new(&str);
4070 nested_list = NULL;
4071 if (macro_subst_tok(&str, &nested_list, s) == 0) {
4072 /* substitution done, NOTE: maybe empty */
4073 tok_str_add(&str, 0);
4074 macro_ptr = str.str;
4075 macro_ptr_allocated = str.str;
4076 goto redo;
4080 } else {
4081 if (tok == 0) {
4082 /* end of macro or end of unget buffer */
4083 if (unget_buffer_enabled) {
4084 macro_ptr = unget_saved_macro_ptr;
4085 unget_buffer_enabled = 0;
4086 } else {
4087 /* end of macro string: free it */
4088 tok_str_free(macro_ptr_allocated);
4089 macro_ptr = NULL;
4091 goto redo;
4095 /* convert preprocessor tokens into C tokens */
4096 if (tok == TOK_PPNUM &&
4097 (parse_flags & PARSE_FLAG_TOK_NUM)) {
4098 parse_number((char *)tokc.cstr->data);
4102 /* push back current token and set current token to 'last_tok'. Only
4103 identifier case handled for labels. */
4104 static inline void unget_tok(int last_tok)
4106 int i, n;
4107 int *q;
4108 unget_saved_macro_ptr = macro_ptr;
4109 unget_buffer_enabled = 1;
4110 q = unget_saved_buffer;
4111 macro_ptr = q;
4112 *q++ = tok;
4113 n = tok_ext_size(tok) - 1;
4114 for(i=0;i<n;i++)
4115 *q++ = tokc.tab[i];
4116 *q = 0; /* end of token string */
4117 tok = last_tok;
4121 void swap(int *p, int *q)
4123 int t;
4124 t = *p;
4125 *p = *q;
4126 *q = t;
4129 void vsetc(CType *type, int r, CValue *vc)
4131 int v;
4133 if (vtop >= vstack + VSTACK_SIZE)
4134 error("memory full");
4135 /* cannot let cpu flags if other instruction are generated. Also
4136 avoid leaving VT_JMP anywhere except on the top of the stack
4137 because it would complicate the code generator. */
4138 if (vtop >= vstack) {
4139 v = vtop->r & VT_VALMASK;
4140 if (v == VT_CMP || (v & ~1) == VT_JMP)
4141 gv(RC_INT);
4143 vtop++;
4144 vtop->type = *type;
4145 vtop->r = r;
4146 vtop->r2 = VT_CONST;
4147 vtop->c = *vc;
4150 /* push integer constant */
4151 void vpushi(int v)
4153 CValue cval;
4154 cval.i = v;
4155 vsetc(&int_type, VT_CONST, &cval);
4158 /* Return a static symbol pointing to a section */
4159 static Sym *get_sym_ref(CType *type, Section *sec,
4160 unsigned long offset, unsigned long size)
4162 int v;
4163 Sym *sym;
4165 v = anon_sym++;
4166 sym = global_identifier_push(v, type->t | VT_STATIC, 0);
4167 sym->type.ref = type->ref;
4168 sym->r = VT_CONST | VT_SYM;
4169 put_extern_sym(sym, sec, offset, size);
4170 return sym;
4173 /* push a reference to a section offset by adding a dummy symbol */
4174 static void vpush_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
4176 CValue cval;
4178 cval.ul = 0;
4179 vsetc(type, VT_CONST | VT_SYM, &cval);
4180 vtop->sym = get_sym_ref(type, sec, offset, size);
4183 /* define a new external reference to a symbol 'v' of type 'u' */
4184 static Sym *external_global_sym(int v, CType *type, int r)
4186 Sym *s;
4188 s = sym_find(v);
4189 if (!s) {
4190 /* push forward reference */
4191 s = global_identifier_push(v, type->t | VT_EXTERN, 0);
4192 s->type.ref = type->ref;
4193 s->r = r | VT_CONST | VT_SYM;
4195 return s;
4198 /* define a new external reference to a symbol 'v' of type 'u' */
4199 static Sym *external_sym(int v, CType *type, int r)
4201 Sym *s;
4203 s = sym_find(v);
4204 if (!s) {
4205 /* push forward reference */
4206 s = sym_push(v, type, r | VT_CONST | VT_SYM, 0);
4207 s->type.t |= VT_EXTERN;
4208 } else {
4209 if (!is_compatible_types(&s->type, type))
4210 error("incompatible types for redefinition of '%s'",
4211 get_tok_str(v, NULL));
4213 return s;
4216 /* push a reference to global symbol v */
4217 static void vpush_global_sym(CType *type, int v)
4219 Sym *sym;
4220 CValue cval;
4222 sym = external_global_sym(v, type, 0);
4223 cval.ul = 0;
4224 vsetc(type, VT_CONST | VT_SYM, &cval);
4225 vtop->sym = sym;
4228 void vset(CType *type, int r, int v)
4230 CValue cval;
4232 cval.i = v;
4233 vsetc(type, r, &cval);
4236 void vseti(int r, int v)
4238 CType type;
4239 type.t = VT_INT;
4240 vset(&type, r, v);
4243 void vswap(void)
4245 SValue tmp;
4247 tmp = vtop[0];
4248 vtop[0] = vtop[-1];
4249 vtop[-1] = tmp;
4252 void vpushv(SValue *v)
4254 if (vtop >= vstack + VSTACK_SIZE)
4255 error("memory full");
4256 vtop++;
4257 *vtop = *v;
4260 void vdup(void)
4262 vpushv(vtop);
4265 /* save r to the memory stack, and mark it as being free */
4266 void save_reg(int r)
4268 int l, saved, size, align;
4269 SValue *p, sv;
4270 CType *type;
4272 /* modify all stack values */
4273 saved = 0;
4274 l = 0;
4275 for(p=vstack;p<=vtop;p++) {
4276 if ((p->r & VT_VALMASK) == r ||
4277 (p->r2 & VT_VALMASK) == r) {
4278 /* must save value on stack if not already done */
4279 if (!saved) {
4280 /* NOTE: must reload 'r' because r might be equal to r2 */
4281 r = p->r & VT_VALMASK;
4282 /* store register in the stack */
4283 type = &p->type;
4284 if ((p->r & VT_LVAL) ||
4285 (!is_float(type->t) && (type->t & VT_BTYPE) != VT_LLONG))
4286 type = &int_type;
4287 size = type_size(type, &align);
4288 loc = (loc - size) & -align;
4289 sv.type.t = type->t;
4290 sv.r = VT_LOCAL | VT_LVAL;
4291 sv.c.ul = loc;
4292 store(r, &sv);
4293 #ifdef TCC_TARGET_I386
4294 /* x86 specific: need to pop fp register ST0 if saved */
4295 if (r == TREG_ST0) {
4296 o(0xd9dd); /* fstp %st(1) */
4298 #endif
4299 /* special long long case */
4300 if ((type->t & VT_BTYPE) == VT_LLONG) {
4301 sv.c.ul += 4;
4302 store(p->r2, &sv);
4304 l = loc;
4305 saved = 1;
4307 /* mark that stack entry as being saved on the stack */
4308 if (p->r & VT_LVAL) {
4309 /* also clear the bounded flag because the
4310 relocation address of the function was stored in
4311 p->c.ul */
4312 p->r = (p->r & ~(VT_VALMASK | VT_BOUNDED)) | VT_LLOCAL;
4313 } else {
4314 p->r = lvalue_type(p->type.t) | VT_LOCAL;
4316 p->r2 = VT_CONST;
4317 p->c.ul = l;
4322 /* find a free register of class 'rc'. If none, save one register */
4323 int get_reg(int rc)
4325 int r;
4326 SValue *p;
4328 /* find a free register */
4329 for(r=0;r<NB_REGS;r++) {
4330 if (reg_classes[r] & rc) {
4331 for(p=vstack;p<=vtop;p++) {
4332 if ((p->r & VT_VALMASK) == r ||
4333 (p->r2 & VT_VALMASK) == r)
4334 goto notfound;
4336 return r;
4338 notfound: ;
4341 /* no register left : free the first one on the stack (VERY
4342 IMPORTANT to start from the bottom to ensure that we don't
4343 spill registers used in gen_opi()) */
4344 for(p=vstack;p<=vtop;p++) {
4345 r = p->r & VT_VALMASK;
4346 if (r < VT_CONST && (reg_classes[r] & rc))
4347 goto save_found;
4348 /* also look at second register (if long long) */
4349 r = p->r2 & VT_VALMASK;
4350 if (r < VT_CONST && (reg_classes[r] & rc)) {
4351 save_found:
4352 save_reg(r);
4353 return r;
4356 /* Should never comes here */
4357 return -1;
4360 /* save registers up to (vtop - n) stack entry */
4361 void save_regs(int n)
4363 int r;
4364 SValue *p, *p1;
4365 p1 = vtop - n;
4366 for(p = vstack;p <= p1; p++) {
4367 r = p->r & VT_VALMASK;
4368 if (r < VT_CONST) {
4369 save_reg(r);
4374 /* move register 's' to 'r', and flush previous value of r to memory
4375 if needed */
4376 void move_reg(int r, int s)
4378 SValue sv;
4380 if (r != s) {
4381 save_reg(r);
4382 sv.type.t = VT_INT;
4383 sv.r = s;
4384 sv.c.ul = 0;
4385 load(r, &sv);
4389 /* get address of vtop (vtop MUST BE an lvalue) */
4390 void gaddrof(void)
4392 vtop->r &= ~VT_LVAL;
4393 /* tricky: if saved lvalue, then we can go back to lvalue */
4394 if ((vtop->r & VT_VALMASK) == VT_LLOCAL)
4395 vtop->r = (vtop->r & ~(VT_VALMASK | VT_LVAL_TYPE)) | VT_LOCAL | VT_LVAL;
4398 #ifdef CONFIG_TCC_BCHECK
4399 /* generate lvalue bound code */
4400 void gbound(void)
4402 int lval_type;
4403 CType type1;
4405 vtop->r &= ~VT_MUSTBOUND;
4406 /* if lvalue, then use checking code before dereferencing */
4407 if (vtop->r & VT_LVAL) {
4408 /* if not VT_BOUNDED value, then make one */
4409 if (!(vtop->r & VT_BOUNDED)) {
4410 lval_type = vtop->r & (VT_LVAL_TYPE | VT_LVAL);
4411 /* must save type because we must set it to int to get pointer */
4412 type1 = vtop->type;
4413 vtop->type.t = VT_INT;
4414 gaddrof();
4415 vpushi(0);
4416 gen_bounded_ptr_add();
4417 vtop->r |= lval_type;
4418 vtop->type = type1;
4420 /* then check for dereferencing */
4421 gen_bounded_ptr_deref();
4424 #endif
4426 /* store vtop a register belonging to class 'rc'. lvalues are
4427 converted to values. Cannot be used if cannot be converted to
4428 register value (such as structures). */
4429 int gv(int rc)
4431 int r, r2, rc2, bit_pos, bit_size, size, align, i;
4432 unsigned long long ll;
4434 /* NOTE: get_reg can modify vstack[] */
4435 if (vtop->type.t & VT_BITFIELD) {
4436 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
4437 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
4438 /* remove bit field info to avoid loops */
4439 vtop->type.t &= ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
4440 /* generate shifts */
4441 vpushi(32 - (bit_pos + bit_size));
4442 gen_op(TOK_SHL);
4443 vpushi(32 - bit_size);
4444 /* NOTE: transformed to SHR if unsigned */
4445 gen_op(TOK_SAR);
4446 r = gv(rc);
4447 } else {
4448 if (is_float(vtop->type.t) &&
4449 (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
4450 Sym *sym;
4451 int *ptr;
4452 unsigned long offset;
4454 /* XXX: unify with initializers handling ? */
4455 /* CPUs usually cannot use float constants, so we store them
4456 generically in data segment */
4457 size = type_size(&vtop->type, &align);
4458 offset = (data_section->data_offset + align - 1) & -align;
4459 data_section->data_offset = offset;
4460 /* XXX: not portable yet */
4461 ptr = section_ptr_add(data_section, size);
4462 size = size >> 2;
4463 for(i=0;i<size;i++)
4464 ptr[i] = vtop->c.tab[i];
4465 sym = get_sym_ref(&vtop->type, data_section, offset, size << 2);
4466 vtop->r |= VT_LVAL | VT_SYM;
4467 vtop->sym = sym;
4468 vtop->c.ul = 0;
4470 #ifdef CONFIG_TCC_BCHECK
4471 if (vtop->r & VT_MUSTBOUND)
4472 gbound();
4473 #endif
4475 r = vtop->r & VT_VALMASK;
4476 /* need to reload if:
4477 - constant
4478 - lvalue (need to dereference pointer)
4479 - already a register, but not in the right class */
4480 if (r >= VT_CONST ||
4481 (vtop->r & VT_LVAL) ||
4482 !(reg_classes[r] & rc) ||
4483 ((vtop->type.t & VT_BTYPE) == VT_LLONG &&
4484 !(reg_classes[vtop->r2] & rc))) {
4485 r = get_reg(rc);
4486 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
4487 /* two register type load : expand to two words
4488 temporarily */
4489 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
4490 /* load constant */
4491 ll = vtop->c.ull;
4492 vtop->c.ui = ll; /* first word */
4493 load(r, vtop);
4494 vtop->r = r; /* save register value */
4495 vpushi(ll >> 32); /* second word */
4496 } else if (r >= VT_CONST ||
4497 (vtop->r & VT_LVAL)) {
4498 /* load from memory */
4499 load(r, vtop);
4500 vdup();
4501 vtop[-1].r = r; /* save register value */
4502 /* increment pointer to get second word */
4503 vtop->type.t = VT_INT;
4504 gaddrof();
4505 vpushi(4);
4506 gen_op('+');
4507 vtop->r |= VT_LVAL;
4508 } else {
4509 /* move registers */
4510 load(r, vtop);
4511 vdup();
4512 vtop[-1].r = r; /* save register value */
4513 vtop->r = vtop[-1].r2;
4515 /* allocate second register */
4516 rc2 = RC_INT;
4517 if (rc == RC_IRET)
4518 rc2 = RC_LRET;
4519 r2 = get_reg(rc2);
4520 load(r2, vtop);
4521 vpop();
4522 /* write second register */
4523 vtop->r2 = r2;
4524 } else if ((vtop->r & VT_LVAL) && !is_float(vtop->type.t)) {
4525 int t1, t;
4526 /* lvalue of scalar type : need to use lvalue type
4527 because of possible cast */
4528 t = vtop->type.t;
4529 t1 = t;
4530 /* compute memory access type */
4531 if (vtop->r & VT_LVAL_BYTE)
4532 t = VT_BYTE;
4533 else if (vtop->r & VT_LVAL_SHORT)
4534 t = VT_SHORT;
4535 if (vtop->r & VT_LVAL_UNSIGNED)
4536 t |= VT_UNSIGNED;
4537 vtop->type.t = t;
4538 load(r, vtop);
4539 /* restore wanted type */
4540 vtop->type.t = t1;
4541 } else {
4542 /* one register type load */
4543 load(r, vtop);
4546 vtop->r = r;
4548 return r;
4551 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
4552 void gv2(int rc1, int rc2)
4554 int v;
4556 /* generate more generic register first. But VT_JMP or VT_CMP
4557 values must be generated first in all cases to avoid possible
4558 reload errors */
4559 v = vtop[0].r & VT_VALMASK;
4560 if (v != VT_CMP && (v & ~1) != VT_JMP && rc1 <= rc2) {
4561 vswap();
4562 gv(rc1);
4563 vswap();
4564 gv(rc2);
4565 /* test if reload is needed for first register */
4566 if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) {
4567 vswap();
4568 gv(rc1);
4569 vswap();
4571 } else {
4572 gv(rc2);
4573 vswap();
4574 gv(rc1);
4575 vswap();
4576 /* test if reload is needed for first register */
4577 if ((vtop[0].r & VT_VALMASK) >= VT_CONST) {
4578 gv(rc2);
4583 /* expand long long on stack in two int registers */
4584 void lexpand(void)
4586 int u;
4588 u = vtop->type.t & VT_UNSIGNED;
4589 gv(RC_INT);
4590 vdup();
4591 vtop[0].r = vtop[-1].r2;
4592 vtop[0].r2 = VT_CONST;
4593 vtop[-1].r2 = VT_CONST;
4594 vtop[0].type.t = VT_INT | u;
4595 vtop[-1].type.t = VT_INT | u;
4598 /* build a long long from two ints */
4599 void lbuild(int t)
4601 gv2(RC_INT, RC_INT);
4602 vtop[-1].r2 = vtop[0].r;
4603 vtop[-1].type.t = t;
4604 vpop();
4607 /* rotate n first stack elements to the bottom
4608 I1 ... In -> I2 ... In I1 [top is right]
4610 void vrotb(int n)
4612 int i;
4613 SValue tmp;
4615 tmp = vtop[-n + 1];
4616 for(i=-n+1;i!=0;i++)
4617 vtop[i] = vtop[i+1];
4618 vtop[0] = tmp;
4621 /* rotate n first stack elements to the top
4622 I1 ... In -> In I1 ... I(n-1) [top is right]
4624 void vrott(int n)
4626 int i;
4627 SValue tmp;
4629 tmp = vtop[0];
4630 for(i = 0;i < n - 1; i++)
4631 vtop[-i] = vtop[-i - 1];
4632 vtop[-n + 1] = tmp;
4635 /* pop stack value */
4636 void vpop(void)
4638 int v;
4639 v = vtop->r & VT_VALMASK;
4640 #ifdef TCC_TARGET_I386
4641 /* for x86, we need to pop the FP stack */
4642 if (v == TREG_ST0 && !nocode_wanted) {
4643 o(0xd9dd); /* fstp %st(1) */
4644 } else
4645 #endif
4646 if (v == VT_JMP || v == VT_JMPI) {
4647 /* need to put correct jump if && or || without test */
4648 gsym(vtop->c.ul);
4650 vtop--;
4653 /* convert stack entry to register and duplicate its value in another
4654 register */
4655 void gv_dup(void)
4657 int rc, t, r, r1;
4658 SValue sv;
4660 t = vtop->type.t;
4661 if ((t & VT_BTYPE) == VT_LLONG) {
4662 lexpand();
4663 gv_dup();
4664 vswap();
4665 vrotb(3);
4666 gv_dup();
4667 vrotb(4);
4668 /* stack: H L L1 H1 */
4669 lbuild(t);
4670 vrotb(3);
4671 vrotb(3);
4672 vswap();
4673 lbuild(t);
4674 vswap();
4675 } else {
4676 /* duplicate value */
4677 rc = RC_INT;
4678 sv.type.t = VT_INT;
4679 if (is_float(t)) {
4680 rc = RC_FLOAT;
4681 sv.type.t = t;
4683 r = gv(rc);
4684 r1 = get_reg(rc);
4685 sv.r = r;
4686 sv.c.ul = 0;
4687 load(r1, &sv); /* move r to r1 */
4688 vdup();
4689 /* duplicates value */
4690 vtop->r = r1;
4694 /* generate CPU independent (unsigned) long long operations */
4695 void gen_opl(int op)
4697 int t, a, b, op1, c, i;
4698 int func;
4699 SValue tmp;
4701 switch(op) {
4702 case '/':
4703 case TOK_PDIV:
4704 func = TOK___divdi3;
4705 goto gen_func;
4706 case TOK_UDIV:
4707 func = TOK___udivdi3;
4708 goto gen_func;
4709 case '%':
4710 func = TOK___moddi3;
4711 goto gen_func;
4712 case TOK_UMOD:
4713 func = TOK___umoddi3;
4714 gen_func:
4715 /* call generic long long function */
4716 vpush_global_sym(&func_old_type, func);
4717 vrott(3);
4718 gfunc_call(2);
4719 vpushi(0);
4720 vtop->r = REG_IRET;
4721 vtop->r2 = REG_LRET;
4722 break;
4723 case '^':
4724 case '&':
4725 case '|':
4726 case '*':
4727 case '+':
4728 case '-':
4729 t = vtop->type.t;
4730 vswap();
4731 lexpand();
4732 vrotb(3);
4733 lexpand();
4734 /* stack: L1 H1 L2 H2 */
4735 tmp = vtop[0];
4736 vtop[0] = vtop[-3];
4737 vtop[-3] = tmp;
4738 tmp = vtop[-2];
4739 vtop[-2] = vtop[-3];
4740 vtop[-3] = tmp;
4741 vswap();
4742 /* stack: H1 H2 L1 L2 */
4743 if (op == '*') {
4744 vpushv(vtop - 1);
4745 vpushv(vtop - 1);
4746 gen_op(TOK_UMULL);
4747 lexpand();
4748 /* stack: H1 H2 L1 L2 ML MH */
4749 for(i=0;i<4;i++)
4750 vrotb(6);
4751 /* stack: ML MH H1 H2 L1 L2 */
4752 tmp = vtop[0];
4753 vtop[0] = vtop[-2];
4754 vtop[-2] = tmp;
4755 /* stack: ML MH H1 L2 H2 L1 */
4756 gen_op('*');
4757 vrotb(3);
4758 vrotb(3);
4759 gen_op('*');
4760 /* stack: ML MH M1 M2 */
4761 gen_op('+');
4762 gen_op('+');
4763 } else if (op == '+' || op == '-') {
4764 /* XXX: add non carry method too (for MIPS or alpha) */
4765 if (op == '+')
4766 op1 = TOK_ADDC1;
4767 else
4768 op1 = TOK_SUBC1;
4769 gen_op(op1);
4770 /* stack: H1 H2 (L1 op L2) */
4771 vrotb(3);
4772 vrotb(3);
4773 gen_op(op1 + 1); /* TOK_xxxC2 */
4774 } else {
4775 gen_op(op);
4776 /* stack: H1 H2 (L1 op L2) */
4777 vrotb(3);
4778 vrotb(3);
4779 /* stack: (L1 op L2) H1 H2 */
4780 gen_op(op);
4781 /* stack: (L1 op L2) (H1 op H2) */
4783 /* stack: L H */
4784 lbuild(t);
4785 break;
4786 case TOK_SAR:
4787 case TOK_SHR:
4788 case TOK_SHL:
4789 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
4790 t = vtop[-1].type.t;
4791 vswap();
4792 lexpand();
4793 vrotb(3);
4794 /* stack: L H shift */
4795 c = (int)vtop->c.i;
4796 /* constant: simpler */
4797 /* NOTE: all comments are for SHL. the other cases are
4798 done by swaping words */
4799 vpop();
4800 if (op != TOK_SHL)
4801 vswap();
4802 if (c >= 32) {
4803 /* stack: L H */
4804 vpop();
4805 if (c > 32) {
4806 vpushi(c - 32);
4807 gen_op(op);
4809 if (op != TOK_SAR) {
4810 vpushi(0);
4811 } else {
4812 gv_dup();
4813 vpushi(31);
4814 gen_op(TOK_SAR);
4816 vswap();
4817 } else {
4818 vswap();
4819 gv_dup();
4820 /* stack: H L L */
4821 vpushi(c);
4822 gen_op(op);
4823 vswap();
4824 vpushi(32 - c);
4825 if (op == TOK_SHL)
4826 gen_op(TOK_SHR);
4827 else
4828 gen_op(TOK_SHL);
4829 vrotb(3);
4830 /* stack: L L H */
4831 vpushi(c);
4832 if (op == TOK_SHL)
4833 gen_op(TOK_SHL);
4834 else
4835 gen_op(TOK_SHR);
4836 gen_op('|');
4838 if (op != TOK_SHL)
4839 vswap();
4840 lbuild(t);
4841 } else {
4842 /* XXX: should provide a faster fallback on x86 ? */
4843 switch(op) {
4844 case TOK_SAR:
4845 func = TOK___sardi3;
4846 goto gen_func;
4847 case TOK_SHR:
4848 func = TOK___shrdi3;
4849 goto gen_func;
4850 case TOK_SHL:
4851 func = TOK___shldi3;
4852 goto gen_func;
4855 break;
4856 default:
4857 /* compare operations */
4858 t = vtop->type.t;
4859 vswap();
4860 lexpand();
4861 vrotb(3);
4862 lexpand();
4863 /* stack: L1 H1 L2 H2 */
4864 tmp = vtop[-1];
4865 vtop[-1] = vtop[-2];
4866 vtop[-2] = tmp;
4867 /* stack: L1 L2 H1 H2 */
4868 /* compare high */
4869 op1 = op;
4870 /* when values are equal, we need to compare low words. since
4871 the jump is inverted, we invert the test too. */
4872 if (op1 == TOK_LT)
4873 op1 = TOK_LE;
4874 else if (op1 == TOK_GT)
4875 op1 = TOK_GE;
4876 else if (op1 == TOK_ULT)
4877 op1 = TOK_ULE;
4878 else if (op1 == TOK_UGT)
4879 op1 = TOK_UGE;
4880 a = 0;
4881 b = 0;
4882 gen_op(op1);
4883 if (op1 != TOK_NE) {
4884 a = gtst(1, 0);
4886 if (op != TOK_EQ) {
4887 /* generate non equal test */
4888 /* XXX: NOT PORTABLE yet */
4889 if (a == 0) {
4890 b = gtst(0, 0);
4891 } else {
4892 #ifdef TCC_TARGET_I386
4893 b = psym(0x850f, 0);
4894 #else
4895 error("not implemented");
4896 #endif
4899 /* compare low. Always unsigned */
4900 op1 = op;
4901 if (op1 == TOK_LT)
4902 op1 = TOK_ULT;
4903 else if (op1 == TOK_LE)
4904 op1 = TOK_ULE;
4905 else if (op1 == TOK_GT)
4906 op1 = TOK_UGT;
4907 else if (op1 == TOK_GE)
4908 op1 = TOK_UGE;
4909 gen_op(op1);
4910 a = gtst(1, a);
4911 gsym(b);
4912 vseti(VT_JMPI, a);
4913 break;
4917 /* handle integer constant optimizations and various machine
4918 independent opt */
4919 void gen_opic(int op)
4921 int fc, c1, c2, n;
4922 SValue *v1, *v2;
4924 v1 = vtop - 1;
4925 v2 = vtop;
4926 /* currently, we cannot do computations with forward symbols */
4927 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
4928 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
4929 if (c1 && c2) {
4930 fc = v2->c.i;
4931 switch(op) {
4932 case '+': v1->c.i += fc; break;
4933 case '-': v1->c.i -= fc; break;
4934 case '&': v1->c.i &= fc; break;
4935 case '^': v1->c.i ^= fc; break;
4936 case '|': v1->c.i |= fc; break;
4937 case '*': v1->c.i *= fc; break;
4939 case TOK_PDIV:
4940 case '/':
4941 case '%':
4942 case TOK_UDIV:
4943 case TOK_UMOD:
4944 /* if division by zero, generate explicit division */
4945 if (fc == 0) {
4946 if (const_wanted)
4947 error("division by zero in constant");
4948 goto general_case;
4950 switch(op) {
4951 default: v1->c.i /= fc; break;
4952 case '%': v1->c.i %= fc; break;
4953 case TOK_UDIV: v1->c.i = (unsigned)v1->c.i / fc; break;
4954 case TOK_UMOD: v1->c.i = (unsigned)v1->c.i % fc; break;
4956 break;
4957 case TOK_SHL: v1->c.i <<= fc; break;
4958 case TOK_SHR: v1->c.i = (unsigned)v1->c.i >> fc; break;
4959 case TOK_SAR: v1->c.i >>= fc; break;
4960 /* tests */
4961 case TOK_ULT: v1->c.i = (unsigned)v1->c.i < (unsigned)fc; break;
4962 case TOK_UGE: v1->c.i = (unsigned)v1->c.i >= (unsigned)fc; break;
4963 case TOK_EQ: v1->c.i = v1->c.i == fc; break;
4964 case TOK_NE: v1->c.i = v1->c.i != fc; break;
4965 case TOK_ULE: v1->c.i = (unsigned)v1->c.i <= (unsigned)fc; break;
4966 case TOK_UGT: v1->c.i = (unsigned)v1->c.i > (unsigned)fc; break;
4967 case TOK_LT: v1->c.i = v1->c.i < fc; break;
4968 case TOK_GE: v1->c.i = v1->c.i >= fc; break;
4969 case TOK_LE: v1->c.i = v1->c.i <= fc; break;
4970 case TOK_GT: v1->c.i = v1->c.i > fc; break;
4971 /* logical */
4972 case TOK_LAND: v1->c.i = v1->c.i && fc; break;
4973 case TOK_LOR: v1->c.i = v1->c.i || fc; break;
4974 default:
4975 goto general_case;
4977 vtop--;
4978 } else {
4979 /* if commutative ops, put c2 as constant */
4980 if (c1 && (op == '+' || op == '&' || op == '^' ||
4981 op == '|' || op == '*')) {
4982 vswap();
4983 swap(&c1, &c2);
4985 fc = vtop->c.i;
4986 if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV ||
4987 op == TOK_PDIV) &&
4988 fc == 1) ||
4989 ((op == '+' || op == '-' || op == '|' || op == '^' ||
4990 op == TOK_SHL || op == TOK_SHR || op == TOK_SAR) &&
4991 fc == 0) ||
4992 (op == '&' &&
4993 fc == -1))) {
4994 /* nothing to do */
4995 vtop--;
4996 } else if (c2 && (op == '*' || op == TOK_PDIV || op == TOK_UDIV)) {
4997 /* try to use shifts instead of muls or divs */
4998 if (fc > 0 && (fc & (fc - 1)) == 0) {
4999 n = -1;
5000 while (fc) {
5001 fc >>= 1;
5002 n++;
5004 vtop->c.i = n;
5005 if (op == '*')
5006 op = TOK_SHL;
5007 else if (op == TOK_PDIV)
5008 op = TOK_SAR;
5009 else
5010 op = TOK_SHR;
5012 goto general_case;
5013 } else if (c2 && (op == '+' || op == '-') &&
5014 (vtop[-1].r & (VT_VALMASK | VT_LVAL | VT_SYM)) ==
5015 (VT_CONST | VT_SYM)) {
5016 /* symbol + constant case */
5017 if (op == '-')
5018 fc = -fc;
5019 vtop--;
5020 vtop->c.i += fc;
5021 } else {
5022 general_case:
5023 if (!nocode_wanted) {
5024 /* call low level op generator */
5025 gen_opi(op);
5026 } else {
5027 vtop--;
5033 /* generate a floating point operation with constant propagation */
5034 void gen_opif(int op)
5036 int c1, c2;
5037 SValue *v1, *v2;
5038 long double f1, f2;
5040 v1 = vtop - 1;
5041 v2 = vtop;
5042 /* currently, we cannot do computations with forward symbols */
5043 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5044 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5045 if (c1 && c2) {
5046 if (v1->type.t == VT_FLOAT) {
5047 f1 = v1->c.f;
5048 f2 = v2->c.f;
5049 } else if (v1->type.t == VT_DOUBLE) {
5050 f1 = v1->c.d;
5051 f2 = v2->c.d;
5052 } else {
5053 f1 = v1->c.ld;
5054 f2 = v2->c.ld;
5057 /* NOTE: we only do constant propagation if finite number (not
5058 NaN or infinity) (ANSI spec) */
5059 if (!ieee_finite(f1) || !ieee_finite(f2))
5060 goto general_case;
5062 switch(op) {
5063 case '+': f1 += f2; break;
5064 case '-': f1 -= f2; break;
5065 case '*': f1 *= f2; break;
5066 case '/':
5067 if (f2 == 0.0) {
5068 if (const_wanted)
5069 error("division by zero in constant");
5070 goto general_case;
5072 f1 /= f2;
5073 break;
5074 /* XXX: also handles tests ? */
5075 default:
5076 goto general_case;
5078 /* XXX: overflow test ? */
5079 if (v1->type.t == VT_FLOAT) {
5080 v1->c.f = f1;
5081 } else if (v1->type.t == VT_DOUBLE) {
5082 v1->c.d = f1;
5083 } else {
5084 v1->c.ld = f1;
5086 vtop--;
5087 } else {
5088 general_case:
5089 if (!nocode_wanted) {
5090 gen_opf(op);
5091 } else {
5092 vtop--;
5097 static int pointed_size(CType *type)
5099 int align;
5100 return type_size(pointed_type(type), &align);
5103 static inline int is_null_pointer(SValue *p)
5105 if ((p->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
5106 return 0;
5107 return ((p->type.t & VT_BTYPE) == VT_INT && p->c.i == 0) ||
5108 ((p->type.t & VT_BTYPE) == VT_LLONG && p->c.ll == 0);
5111 static inline int is_integer_btype(int bt)
5113 return (bt == VT_BYTE || bt == VT_SHORT ||
5114 bt == VT_INT || bt == VT_LLONG);
5117 /* check types for comparison or substraction of pointers */
5118 static void check_comparison_pointer_types(SValue *p1, SValue *p2, int op)
5120 CType *type1, *type2, tmp_type1, tmp_type2;
5121 int bt1, bt2;
5123 /* null pointers are accepted for all comparisons as gcc */
5124 if (is_null_pointer(p1) || is_null_pointer(p2))
5125 return;
5126 type1 = &p1->type;
5127 type2 = &p2->type;
5128 bt1 = type1->t & VT_BTYPE;
5129 bt2 = type2->t & VT_BTYPE;
5130 /* accept comparison between pointer and integer with a warning */
5131 if ((is_integer_btype(bt1) || is_integer_btype(bt2)) && op != '-') {
5132 warning("comparison between pointer and integer");
5133 return;
5136 /* both must be pointers or implicit function pointers */
5137 if (bt1 == VT_PTR) {
5138 type1 = pointed_type(type1);
5139 } else if (bt1 != VT_FUNC)
5140 goto invalid_operands;
5142 if (bt2 == VT_PTR) {
5143 type2 = pointed_type(type2);
5144 } else if (bt2 != VT_FUNC) {
5145 invalid_operands:
5146 error("invalid operands to binary %s", get_tok_str(op, NULL));
5148 if ((type1->t & VT_BTYPE) == VT_VOID ||
5149 (type2->t & VT_BTYPE) == VT_VOID)
5150 return;
5151 tmp_type1 = *type1;
5152 tmp_type2 = *type2;
5153 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
5154 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
5155 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
5156 /* gcc-like error if '-' is used */
5157 if (op == '-')
5158 goto invalid_operands;
5159 else
5160 warning("comparison of distinct pointer types lacks a cast");
5164 /* generic gen_op: handles types problems */
5165 void gen_op(int op)
5167 int u, t1, t2, bt1, bt2, t;
5168 CType type1;
5170 t1 = vtop[-1].type.t;
5171 t2 = vtop[0].type.t;
5172 bt1 = t1 & VT_BTYPE;
5173 bt2 = t2 & VT_BTYPE;
5175 if (bt1 == VT_PTR || bt2 == VT_PTR) {
5176 /* at least one operand is a pointer */
5177 /* relationnal op: must be both pointers */
5178 if (op >= TOK_ULT && op <= TOK_GT) {
5179 check_comparison_pointer_types(vtop - 1, vtop, op);
5180 /* pointers are handled are unsigned */
5181 t = VT_INT | VT_UNSIGNED;
5182 goto std_op;
5184 /* if both pointers, then it must be the '-' op */
5185 if (bt1 == VT_PTR && bt2 == VT_PTR) {
5186 if (op != '-')
5187 error("cannot use pointers here");
5188 check_comparison_pointer_types(vtop - 1, vtop, op);
5189 /* XXX: check that types are compatible */
5190 u = pointed_size(&vtop[-1].type);
5191 gen_opic(op);
5192 /* set to integer type */
5193 vtop->type.t = VT_INT;
5194 vpushi(u);
5195 gen_op(TOK_PDIV);
5196 } else {
5197 /* exactly one pointer : must be '+' or '-'. */
5198 if (op != '-' && op != '+')
5199 error("cannot use pointers here");
5200 /* Put pointer as first operand */
5201 if (bt2 == VT_PTR) {
5202 vswap();
5203 swap(&t1, &t2);
5205 type1 = vtop[-1].type;
5206 /* XXX: cast to int ? (long long case) */
5207 vpushi(pointed_size(&vtop[-1].type));
5208 gen_op('*');
5209 #ifdef CONFIG_TCC_BCHECK
5210 /* if evaluating constant expression, no code should be
5211 generated, so no bound check */
5212 if (do_bounds_check && !const_wanted) {
5213 /* if bounded pointers, we generate a special code to
5214 test bounds */
5215 if (op == '-') {
5216 vpushi(0);
5217 vswap();
5218 gen_op('-');
5220 gen_bounded_ptr_add();
5221 } else
5222 #endif
5224 gen_opic(op);
5226 /* put again type if gen_opic() swaped operands */
5227 vtop->type = type1;
5229 } else if (is_float(bt1) || is_float(bt2)) {
5230 /* compute bigger type and do implicit casts */
5231 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
5232 t = VT_LDOUBLE;
5233 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
5234 t = VT_DOUBLE;
5235 } else {
5236 t = VT_FLOAT;
5238 /* floats can only be used for a few operations */
5239 if (op != '+' && op != '-' && op != '*' && op != '/' &&
5240 (op < TOK_ULT || op > TOK_GT))
5241 error("invalid operands for binary operation");
5242 goto std_op;
5243 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
5244 /* cast to biggest op */
5245 t = VT_LLONG;
5246 /* convert to unsigned if it does not fit in a long long */
5247 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
5248 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
5249 t |= VT_UNSIGNED;
5250 goto std_op;
5251 } else {
5252 /* integer operations */
5253 t = VT_INT;
5254 /* convert to unsigned if it does not fit in an integer */
5255 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
5256 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
5257 t |= VT_UNSIGNED;
5258 std_op:
5259 /* XXX: currently, some unsigned operations are explicit, so
5260 we modify them here */
5261 if (t & VT_UNSIGNED) {
5262 if (op == TOK_SAR)
5263 op = TOK_SHR;
5264 else if (op == '/')
5265 op = TOK_UDIV;
5266 else if (op == '%')
5267 op = TOK_UMOD;
5268 else if (op == TOK_LT)
5269 op = TOK_ULT;
5270 else if (op == TOK_GT)
5271 op = TOK_UGT;
5272 else if (op == TOK_LE)
5273 op = TOK_ULE;
5274 else if (op == TOK_GE)
5275 op = TOK_UGE;
5277 vswap();
5278 type1.t = t;
5279 gen_cast(&type1);
5280 vswap();
5281 /* special case for shifts and long long: we keep the shift as
5282 an integer */
5283 if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL)
5284 type1.t = VT_INT;
5285 gen_cast(&type1);
5286 if (is_float(t))
5287 gen_opif(op);
5288 else if ((t & VT_BTYPE) == VT_LLONG)
5289 gen_opl(op);
5290 else
5291 gen_opic(op);
5292 if (op >= TOK_ULT && op <= TOK_GT) {
5293 /* relationnal op: the result is an int */
5294 vtop->type.t = VT_INT;
5295 } else {
5296 vtop->type.t = t;
5301 /* generic itof for unsigned long long case */
5302 void gen_cvt_itof1(int t)
5304 if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
5305 (VT_LLONG | VT_UNSIGNED)) {
5307 if (t == VT_FLOAT)
5308 vpush_global_sym(&func_old_type, TOK___ulltof);
5309 else if (t == VT_DOUBLE)
5310 vpush_global_sym(&func_old_type, TOK___ulltod);
5311 else
5312 vpush_global_sym(&func_old_type, TOK___ulltold);
5313 vrott(2);
5314 gfunc_call(1);
5315 vpushi(0);
5316 vtop->r = REG_FRET;
5317 } else {
5318 gen_cvt_itof(t);
5322 /* generic ftoi for unsigned long long case */
5323 void gen_cvt_ftoi1(int t)
5325 int st;
5327 if (t == (VT_LLONG | VT_UNSIGNED)) {
5328 /* not handled natively */
5329 st = vtop->type.t & VT_BTYPE;
5330 if (st == VT_FLOAT)
5331 vpush_global_sym(&func_old_type, TOK___fixunssfdi);
5332 else if (st == VT_DOUBLE)
5333 vpush_global_sym(&func_old_type, TOK___fixunsdfdi);
5334 else
5335 vpush_global_sym(&func_old_type, TOK___fixunsxfdi);
5336 vrott(2);
5337 gfunc_call(1);
5338 vpushi(0);
5339 vtop->r = REG_IRET;
5340 vtop->r2 = REG_LRET;
5341 } else {
5342 gen_cvt_ftoi(t);
5346 /* force char or short cast */
5347 void force_charshort_cast(int t)
5349 int bits, dbt;
5350 dbt = t & VT_BTYPE;
5351 /* XXX: add optimization if lvalue : just change type and offset */
5352 if (dbt == VT_BYTE)
5353 bits = 8;
5354 else
5355 bits = 16;
5356 if (t & VT_UNSIGNED) {
5357 vpushi((1 << bits) - 1);
5358 gen_op('&');
5359 } else {
5360 bits = 32 - bits;
5361 vpushi(bits);
5362 gen_op(TOK_SHL);
5363 vpushi(bits);
5364 gen_op(TOK_SAR);
5368 /* cast 'vtop' to 'type' */
5369 static void gen_cast(CType *type)
5371 int sbt, dbt, sf, df, c;
5373 /* special delayed cast for char/short */
5374 /* XXX: in some cases (multiple cascaded casts), it may still
5375 be incorrect */
5376 if (vtop->r & VT_MUSTCAST) {
5377 vtop->r &= ~VT_MUSTCAST;
5378 force_charshort_cast(vtop->type.t);
5381 dbt = type->t & (VT_BTYPE | VT_UNSIGNED);
5382 sbt = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
5384 if (sbt != dbt && !nocode_wanted) {
5385 sf = is_float(sbt);
5386 df = is_float(dbt);
5387 c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5388 if (sf && df) {
5389 /* convert from fp to fp */
5390 if (c) {
5391 /* constant case: we can do it now */
5392 /* XXX: in ISOC, cannot do it if error in convert */
5393 if (dbt == VT_FLOAT && sbt == VT_DOUBLE)
5394 vtop->c.f = (float)vtop->c.d;
5395 else if (dbt == VT_FLOAT && sbt == VT_LDOUBLE)
5396 vtop->c.f = (float)vtop->c.ld;
5397 else if (dbt == VT_DOUBLE && sbt == VT_FLOAT)
5398 vtop->c.d = (double)vtop->c.f;
5399 else if (dbt == VT_DOUBLE && sbt == VT_LDOUBLE)
5400 vtop->c.d = (double)vtop->c.ld;
5401 else if (dbt == VT_LDOUBLE && sbt == VT_FLOAT)
5402 vtop->c.ld = (long double)vtop->c.f;
5403 else if (dbt == VT_LDOUBLE && sbt == VT_DOUBLE)
5404 vtop->c.ld = (long double)vtop->c.d;
5405 } else {
5406 /* non constant case: generate code */
5407 gen_cvt_ftof(dbt);
5409 } else if (df) {
5410 /* convert int to fp */
5411 if (c) {
5412 switch(sbt) {
5413 case VT_LLONG | VT_UNSIGNED:
5414 case VT_LLONG:
5415 /* XXX: add const cases for long long */
5416 goto do_itof;
5417 case VT_INT | VT_UNSIGNED:
5418 switch(dbt) {
5419 case VT_FLOAT: vtop->c.f = (float)vtop->c.ui; break;
5420 case VT_DOUBLE: vtop->c.d = (double)vtop->c.ui; break;
5421 case VT_LDOUBLE: vtop->c.ld = (long double)vtop->c.ui; break;
5423 break;
5424 default:
5425 switch(dbt) {
5426 case VT_FLOAT: vtop->c.f = (float)vtop->c.i; break;
5427 case VT_DOUBLE: vtop->c.d = (double)vtop->c.i; break;
5428 case VT_LDOUBLE: vtop->c.ld = (long double)vtop->c.i; break;
5430 break;
5432 } else {
5433 do_itof:
5434 gen_cvt_itof1(dbt);
5436 } else if (sf) {
5437 /* convert fp to int */
5438 /* we handle char/short/etc... with generic code */
5439 if (dbt != (VT_INT | VT_UNSIGNED) &&
5440 dbt != (VT_LLONG | VT_UNSIGNED) &&
5441 dbt != VT_LLONG)
5442 dbt = VT_INT;
5443 if (c) {
5444 switch(dbt) {
5445 case VT_LLONG | VT_UNSIGNED:
5446 case VT_LLONG:
5447 /* XXX: add const cases for long long */
5448 goto do_ftoi;
5449 case VT_INT | VT_UNSIGNED:
5450 switch(sbt) {
5451 case VT_FLOAT: vtop->c.ui = (unsigned int)vtop->c.d; break;
5452 case VT_DOUBLE: vtop->c.ui = (unsigned int)vtop->c.d; break;
5453 case VT_LDOUBLE: vtop->c.ui = (unsigned int)vtop->c.d; break;
5455 break;
5456 default:
5457 /* int case */
5458 switch(sbt) {
5459 case VT_FLOAT: vtop->c.i = (int)vtop->c.d; break;
5460 case VT_DOUBLE: vtop->c.i = (int)vtop->c.d; break;
5461 case VT_LDOUBLE: vtop->c.i = (int)vtop->c.d; break;
5463 break;
5465 } else {
5466 do_ftoi:
5467 gen_cvt_ftoi1(dbt);
5469 if (dbt == VT_INT && (type->t & (VT_BTYPE | VT_UNSIGNED)) != dbt) {
5470 /* additional cast for char/short/bool... */
5471 vtop->type.t = dbt;
5472 gen_cast(type);
5474 } else if ((dbt & VT_BTYPE) == VT_LLONG) {
5475 if ((sbt & VT_BTYPE) != VT_LLONG) {
5476 /* scalar to long long */
5477 if (c) {
5478 if (sbt == (VT_INT | VT_UNSIGNED))
5479 vtop->c.ll = vtop->c.ui;
5480 else
5481 vtop->c.ll = vtop->c.i;
5482 } else {
5483 /* machine independent conversion */
5484 gv(RC_INT);
5485 /* generate high word */
5486 if (sbt == (VT_INT | VT_UNSIGNED)) {
5487 vpushi(0);
5488 gv(RC_INT);
5489 } else {
5490 gv_dup();
5491 vpushi(31);
5492 gen_op(TOK_SAR);
5494 /* patch second register */
5495 vtop[-1].r2 = vtop->r;
5496 vpop();
5499 } else if (dbt == VT_BOOL) {
5500 /* scalar to bool */
5501 vpushi(0);
5502 gen_op(TOK_NE);
5503 } else if ((dbt & VT_BTYPE) == VT_BYTE ||
5504 (dbt & VT_BTYPE) == VT_SHORT) {
5505 force_charshort_cast(dbt);
5506 } else if ((dbt & VT_BTYPE) == VT_INT) {
5507 /* scalar to int */
5508 if (sbt == VT_LLONG) {
5509 /* from long long: just take low order word */
5510 lexpand();
5511 vpop();
5513 /* if lvalue and single word type, nothing to do because
5514 the lvalue already contains the real type size (see
5515 VT_LVAL_xxx constants) */
5518 vtop->type = *type;
5521 /* return type size. Put alignment at 'a' */
5522 static int type_size(CType *type, int *a)
5524 Sym *s;
5525 int bt;
5527 bt = type->t & VT_BTYPE;
5528 if (bt == VT_STRUCT) {
5529 /* struct/union */
5530 s = type->ref;
5531 *a = s->r;
5532 return s->c;
5533 } else if (bt == VT_PTR) {
5534 if (type->t & VT_ARRAY) {
5535 s = type->ref;
5536 return type_size(&s->type, a) * s->c;
5537 } else {
5538 *a = PTR_SIZE;
5539 return PTR_SIZE;
5541 } else if (bt == VT_LDOUBLE) {
5542 *a = LDOUBLE_ALIGN;
5543 return LDOUBLE_SIZE;
5544 } else if (bt == VT_DOUBLE || bt == VT_LLONG) {
5545 *a = 4; /* XXX: i386 specific */
5546 return 8;
5547 } else if (bt == VT_INT || bt == VT_ENUM || bt == VT_FLOAT) {
5548 *a = 4;
5549 return 4;
5550 } else if (bt == VT_SHORT) {
5551 *a = 2;
5552 return 2;
5553 } else {
5554 /* char, void, function, _Bool */
5555 *a = 1;
5556 return 1;
5560 /* return the pointed type of t */
5561 static inline CType *pointed_type(CType *type)
5563 return &type->ref->type;
5566 /* modify type so that its it is a pointer to type. */
5567 static void mk_pointer(CType *type)
5569 Sym *s;
5570 s = sym_push(SYM_FIELD, type, 0, -1);
5571 type->t = VT_PTR | (type->t & ~VT_TYPE);
5572 type->ref = s;
5575 /* compare function types. OLD functions match any new functions */
5576 static int is_compatible_func(CType *type1, CType *type2)
5578 Sym *s1, *s2;
5580 s1 = type1->ref;
5581 s2 = type2->ref;
5582 if (!is_compatible_types(&s1->type, &s2->type))
5583 return 0;
5584 /* XXX: not complete */
5585 if (s1->c == FUNC_OLD || s2->c == FUNC_OLD)
5586 return 1;
5587 if (s1->c != s2->c)
5588 return 0;
5589 while (s1 != NULL) {
5590 if (s2 == NULL)
5591 return 0;
5592 if (!is_compatible_types(&s1->type, &s2->type))
5593 return 0;
5594 s1 = s1->next;
5595 s2 = s2->next;
5597 if (s2)
5598 return 0;
5599 return 1;
5602 /* return true if type1 and type2 are exactly the same (including
5603 qualifiers).
5605 - enums are not checked as gcc __builtin_types_compatible_p ()
5607 static int is_compatible_types(CType *type1, CType *type2)
5609 int bt1, t1, t2;
5611 t1 = type1->t & VT_TYPE;
5612 t2 = type2->t & VT_TYPE;
5613 /* XXX: bitfields ? */
5614 if (t1 != t2)
5615 return 0;
5616 /* test more complicated cases */
5617 bt1 = t1 & VT_BTYPE;
5618 if (bt1 == VT_PTR) {
5619 type1 = pointed_type(type1);
5620 type2 = pointed_type(type2);
5621 return is_compatible_types(type1, type2);
5622 } else if (bt1 == VT_STRUCT) {
5623 return (type1->ref == type2->ref);
5624 } else if (bt1 == VT_FUNC) {
5625 return is_compatible_func(type1, type2);
5626 } else {
5627 return 1;
5631 /* print a type. If 'varstr' is not NULL, then the variable is also
5632 printed in the type */
5633 /* XXX: union */
5634 /* XXX: add array and function pointers */
5635 void type_to_str(char *buf, int buf_size,
5636 CType *type, const char *varstr)
5638 int bt, v, t;
5639 Sym *s, *sa;
5640 char buf1[256];
5641 const char *tstr;
5643 t = type->t & VT_TYPE;
5644 bt = t & VT_BTYPE;
5645 buf[0] = '\0';
5646 if (t & VT_CONSTANT)
5647 pstrcat(buf, buf_size, "const ");
5648 if (t & VT_VOLATILE)
5649 pstrcat(buf, buf_size, "volatile ");
5650 if (t & VT_UNSIGNED)
5651 pstrcat(buf, buf_size, "unsigned ");
5652 switch(bt) {
5653 case VT_VOID:
5654 tstr = "void";
5655 goto add_tstr;
5656 case VT_BOOL:
5657 tstr = "_Bool";
5658 goto add_tstr;
5659 case VT_BYTE:
5660 tstr = "char";
5661 goto add_tstr;
5662 case VT_SHORT:
5663 tstr = "short";
5664 goto add_tstr;
5665 case VT_INT:
5666 tstr = "int";
5667 goto add_tstr;
5668 case VT_LONG:
5669 tstr = "long";
5670 goto add_tstr;
5671 case VT_LLONG:
5672 tstr = "long long";
5673 goto add_tstr;
5674 case VT_FLOAT:
5675 tstr = "float";
5676 goto add_tstr;
5677 case VT_DOUBLE:
5678 tstr = "double";
5679 goto add_tstr;
5680 case VT_LDOUBLE:
5681 tstr = "long double";
5682 add_tstr:
5683 pstrcat(buf, buf_size, tstr);
5684 break;
5685 case VT_ENUM:
5686 case VT_STRUCT:
5687 if (bt == VT_STRUCT)
5688 tstr = "struct ";
5689 else
5690 tstr = "enum ";
5691 pstrcat(buf, buf_size, tstr);
5692 v = type->ref->v & ~SYM_STRUCT;
5693 if (v >= SYM_FIRST_ANOM)
5694 pstrcat(buf, buf_size, "<anonymous>");
5695 else
5696 pstrcat(buf, buf_size, get_tok_str(v, NULL));
5697 break;
5698 case VT_FUNC:
5699 s = type->ref;
5700 type_to_str(buf, buf_size, &s->type, varstr);
5701 pstrcat(buf, buf_size, "(");
5702 sa = s->next;
5703 while (sa != NULL) {
5704 type_to_str(buf1, sizeof(buf1), &sa->type, NULL);
5705 pstrcat(buf, buf_size, buf1);
5706 sa = sa->next;
5707 if (sa)
5708 pstrcat(buf, buf_size, ", ");
5710 pstrcat(buf, buf_size, ")");
5711 goto no_var;
5712 case VT_PTR:
5713 s = type->ref;
5714 pstrcpy(buf1, sizeof(buf1), "*");
5715 if (varstr)
5716 pstrcat(buf1, sizeof(buf1), varstr);
5717 type_to_str(buf, buf_size, &s->type, buf1);
5718 goto no_var;
5720 if (varstr) {
5721 pstrcat(buf, buf_size, " ");
5722 pstrcat(buf, buf_size, varstr);
5724 no_var: ;
5727 /* verify type compatibility to store vtop in 'dt' type, and generate
5728 casts if needed. */
5729 static void gen_assign_cast(CType *dt)
5731 CType *st, *type1, *type2, tmp_type1, tmp_type2;
5732 char buf1[256], buf2[256];
5733 int dbt, sbt;
5735 st = &vtop->type; /* source type */
5736 dbt = dt->t & VT_BTYPE;
5737 sbt = st->t & VT_BTYPE;
5738 if (dt->t & VT_CONSTANT)
5739 warning("assignment of read-only location");
5740 switch(dbt) {
5741 case VT_PTR:
5742 /* special cases for pointers */
5743 /* '0' can also be a pointer */
5744 if (is_null_pointer(vtop))
5745 goto type_ok;
5746 /* accept implicit pointer to integer cast with warning */
5747 if (is_integer_btype(sbt)) {
5748 warning("assignment makes pointer from integer without a cast");
5749 goto type_ok;
5751 type1 = pointed_type(dt);
5752 /* a function is implicitely a function pointer */
5753 if (sbt == VT_FUNC) {
5754 if ((type1->t & VT_BTYPE) != VT_VOID &&
5755 !is_compatible_types(pointed_type(dt), st))
5756 goto error;
5757 else
5758 goto type_ok;
5760 if (sbt != VT_PTR)
5761 goto error;
5762 type2 = pointed_type(st);
5763 if ((type1->t & VT_BTYPE) == VT_VOID ||
5764 (type2->t & VT_BTYPE) == VT_VOID) {
5765 /* void * can match anything */
5766 } else {
5767 /* exact type match, except for unsigned */
5768 tmp_type1 = *type1;
5769 tmp_type2 = *type2;
5770 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
5771 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
5772 if (!is_compatible_types(&tmp_type1, &tmp_type2))
5773 goto error;
5775 /* check const and volatile */
5776 if ((!(type1->t & VT_CONSTANT) && (type2->t & VT_CONSTANT)) ||
5777 (!(type1->t & VT_VOLATILE) && (type2->t & VT_VOLATILE)))
5778 warning("assignment discards qualifiers from pointer target type");
5779 break;
5780 case VT_BYTE:
5781 case VT_SHORT:
5782 case VT_INT:
5783 case VT_LLONG:
5784 if (sbt == VT_PTR || sbt == VT_FUNC) {
5785 warning("assignment makes integer from pointer without a cast");
5787 /* XXX: more tests */
5788 break;
5789 case VT_STRUCT:
5790 if (!is_compatible_types(dt, st)) {
5791 error:
5792 type_to_str(buf1, sizeof(buf1), st, NULL);
5793 type_to_str(buf2, sizeof(buf2), dt, NULL);
5794 error("cannot cast '%s' to '%s'", buf1, buf2);
5796 break;
5798 type_ok:
5799 gen_cast(dt);
5802 /* store vtop in lvalue pushed on stack */
5803 void vstore(void)
5805 int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast;
5807 ft = vtop[-1].type.t;
5808 sbt = vtop->type.t & VT_BTYPE;
5809 dbt = ft & VT_BTYPE;
5810 if (((sbt == VT_INT || sbt == VT_SHORT) && dbt == VT_BYTE) ||
5811 (sbt == VT_INT && dbt == VT_SHORT)) {
5812 /* optimize char/short casts */
5813 delayed_cast = VT_MUSTCAST;
5814 vtop->type.t = ft & VT_TYPE;
5815 /* XXX: factorize */
5816 if (ft & VT_CONSTANT)
5817 warning("assignment of read-only location");
5818 } else {
5819 delayed_cast = 0;
5820 gen_assign_cast(&vtop[-1].type);
5823 if (sbt == VT_STRUCT) {
5824 /* if structure, only generate pointer */
5825 /* structure assignment : generate memcpy */
5826 /* XXX: optimize if small size */
5827 if (!nocode_wanted) {
5828 size = type_size(&vtop->type, &align);
5830 vpush_global_sym(&func_old_type, TOK_memcpy);
5832 /* destination */
5833 vpushv(vtop - 2);
5834 vtop->type.t = VT_INT;
5835 gaddrof();
5836 /* source */
5837 vpushv(vtop - 2);
5838 vtop->type.t = VT_INT;
5839 gaddrof();
5840 /* type size */
5841 vpushi(size);
5842 gfunc_call(3);
5844 vswap();
5845 vpop();
5846 } else {
5847 vswap();
5848 vpop();
5850 /* leave source on stack */
5851 } else if (ft & VT_BITFIELD) {
5852 /* bitfield store handling */
5853 bit_pos = (ft >> VT_STRUCT_SHIFT) & 0x3f;
5854 bit_size = (ft >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
5855 /* remove bit field info to avoid loops */
5856 vtop[-1].type.t = ft & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
5858 /* duplicate destination */
5859 vdup();
5860 vtop[-1] = vtop[-2];
5862 /* mask and shift source */
5863 vpushi((1 << bit_size) - 1);
5864 gen_op('&');
5865 vpushi(bit_pos);
5866 gen_op(TOK_SHL);
5867 /* load destination, mask and or with source */
5868 vswap();
5869 vpushi(~(((1 << bit_size) - 1) << bit_pos));
5870 gen_op('&');
5871 gen_op('|');
5872 /* store result */
5873 vstore();
5874 } else {
5875 #ifdef CONFIG_TCC_BCHECK
5876 /* bound check case */
5877 if (vtop[-1].r & VT_MUSTBOUND) {
5878 vswap();
5879 gbound();
5880 vswap();
5882 #endif
5883 if (!nocode_wanted) {
5884 rc = RC_INT;
5885 if (is_float(ft))
5886 rc = RC_FLOAT;
5887 r = gv(rc); /* generate value */
5888 /* if lvalue was saved on stack, must read it */
5889 if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
5890 SValue sv;
5891 t = get_reg(RC_INT);
5892 sv.type.t = VT_INT;
5893 sv.r = VT_LOCAL | VT_LVAL;
5894 sv.c.ul = vtop[-1].c.ul;
5895 load(t, &sv);
5896 vtop[-1].r = t | VT_LVAL;
5898 store(r, vtop - 1);
5899 /* two word case handling : store second register at word + 4 */
5900 if ((ft & VT_BTYPE) == VT_LLONG) {
5901 vswap();
5902 /* convert to int to increment easily */
5903 vtop->type.t = VT_INT;
5904 gaddrof();
5905 vpushi(4);
5906 gen_op('+');
5907 vtop->r |= VT_LVAL;
5908 vswap();
5909 /* XXX: it works because r2 is spilled last ! */
5910 store(vtop->r2, vtop - 1);
5913 vswap();
5914 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
5915 vtop->r |= delayed_cast;
5919 /* post defines POST/PRE add. c is the token ++ or -- */
5920 void inc(int post, int c)
5922 test_lvalue();
5923 vdup(); /* save lvalue */
5924 if (post) {
5925 gv_dup(); /* duplicate value */
5926 vrotb(3);
5927 vrotb(3);
5929 /* add constant */
5930 vpushi(c - TOK_MID);
5931 gen_op('+');
5932 vstore(); /* store value */
5933 if (post)
5934 vpop(); /* if post op, return saved value */
5937 /* Parse GNUC __attribute__ extension. Currently, the following
5938 extensions are recognized:
5939 - aligned(n) : set data/function alignment.
5940 - section(x) : generate data/code in this section.
5941 - unused : currently ignored, but may be used someday.
5943 static void parse_attribute(AttributeDef *ad)
5945 int t, n;
5947 while (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2) {
5948 next();
5949 skip('(');
5950 skip('(');
5951 while (tok != ')') {
5952 if (tok < TOK_IDENT)
5953 expect("attribute name");
5954 t = tok;
5955 next();
5956 switch(t) {
5957 case TOK_SECTION1:
5958 case TOK_SECTION2:
5959 skip('(');
5960 if (tok != TOK_STR)
5961 expect("section name");
5962 ad->section = find_section(tcc_state, (char *)tokc.cstr->data);
5963 next();
5964 skip(')');
5965 break;
5966 case TOK_ALIGNED1:
5967 case TOK_ALIGNED2:
5968 if (tok == '(') {
5969 next();
5970 n = expr_const();
5971 if (n <= 0 || (n & (n - 1)) != 0)
5972 error("alignment must be a positive power of two");
5973 skip(')');
5974 } else {
5975 n = MAX_ALIGN;
5977 ad->aligned = n;
5978 break;
5979 case TOK_UNUSED1:
5980 case TOK_UNUSED2:
5981 /* currently, no need to handle it because tcc does not
5982 track unused objects */
5983 break;
5984 case TOK_NORETURN1:
5985 case TOK_NORETURN2:
5986 /* currently, no need to handle it because tcc does not
5987 track unused objects */
5988 break;
5989 case TOK_CDECL1:
5990 case TOK_CDECL2:
5991 case TOK_CDECL3:
5992 ad->func_call = FUNC_CDECL;
5993 break;
5994 case TOK_STDCALL1:
5995 case TOK_STDCALL2:
5996 case TOK_STDCALL3:
5997 ad->func_call = FUNC_STDCALL;
5998 break;
5999 default:
6000 if (tcc_state->warn_unsupported)
6001 warning("'%s' attribute ignored", get_tok_str(t, NULL));
6002 /* skip parameters */
6003 /* XXX: skip parenthesis too */
6004 if (tok == '(') {
6005 next();
6006 while (tok != ')' && tok != -1)
6007 next();
6008 next();
6010 break;
6012 if (tok != ',')
6013 break;
6014 next();
6016 skip(')');
6017 skip(')');
6021 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
6022 static void struct_decl(CType *type, int u)
6024 int a, v, size, align, maxalign, c, offset;
6025 int bit_size, bit_pos, bsize, bt, lbit_pos;
6026 Sym *s, *ss, **ps;
6027 AttributeDef ad;
6028 CType type1, btype;
6030 a = tok; /* save decl type */
6031 next();
6032 if (tok != '{') {
6033 v = tok;
6034 next();
6035 /* struct already defined ? return it */
6036 if (v < TOK_IDENT)
6037 expect("struct/union/enum name");
6038 s = struct_find(v);
6039 if (s) {
6040 if (s->type.t != a)
6041 error("invalid type");
6042 goto do_decl;
6044 } else {
6045 v = anon_sym++;
6047 type1.t = a;
6048 /* we put an undefined size for struct/union */
6049 s = sym_push(v | SYM_STRUCT, &type1, 0, -1);
6050 s->r = 0; /* default alignment is zero as gcc */
6051 /* put struct/union/enum name in type */
6052 do_decl:
6053 type->t = u;
6054 type->ref = s;
6056 if (tok == '{') {
6057 next();
6058 if (s->c != -1)
6059 error("struct/union/enum already defined");
6060 /* cannot be empty */
6061 c = 0;
6062 /* non empty enums are not allowed */
6063 if (a == TOK_ENUM) {
6064 for(;;) {
6065 v = tok;
6066 if (v < TOK_UIDENT)
6067 expect("identifier");
6068 next();
6069 if (tok == '=') {
6070 next();
6071 c = expr_const();
6073 /* enum symbols have static storage */
6074 ss = sym_push(v, &int_type, VT_CONST, c);
6075 ss->type.t |= VT_STATIC;
6076 if (tok != ',')
6077 break;
6078 next();
6079 c++;
6080 /* NOTE: we accept a trailing comma */
6081 if (tok == '}')
6082 break;
6084 skip('}');
6085 } else {
6086 maxalign = 1;
6087 ps = &s->next;
6088 bit_pos = 0;
6089 offset = 0;
6090 while (tok != '}') {
6091 parse_btype(&btype, &ad);
6092 while (1) {
6093 bit_size = -1;
6094 v = 0;
6095 type1 = btype;
6096 if (tok != ':') {
6097 type_decl(&type1, &ad, &v, TYPE_DIRECT);
6098 if ((type1.t & VT_BTYPE) == VT_FUNC ||
6099 (type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN | VT_INLINE)))
6100 error("invalid type for '%s'",
6101 get_tok_str(v, NULL));
6103 if (tok == ':') {
6104 next();
6105 bit_size = expr_const();
6106 /* XXX: handle v = 0 case for messages */
6107 if (bit_size < 0)
6108 error("negative width in bit-field '%s'",
6109 get_tok_str(v, NULL));
6110 if (v && bit_size == 0)
6111 error("zero width for bit-field '%s'",
6112 get_tok_str(v, NULL));
6114 size = type_size(&type1, &align);
6115 lbit_pos = 0;
6116 if (bit_size >= 0) {
6117 bt = type1.t & VT_BTYPE;
6118 if (bt != VT_INT &&
6119 bt != VT_BYTE &&
6120 bt != VT_SHORT &&
6121 bt != VT_ENUM)
6122 error("bitfields must have scalar type");
6123 bsize = size * 8;
6124 if (bit_size > bsize) {
6125 error("width of '%s' exceeds its type",
6126 get_tok_str(v, NULL));
6127 } else if (bit_size == bsize) {
6128 /* no need for bit fields */
6129 bit_pos = 0;
6130 } else if (bit_size == 0) {
6131 /* XXX: what to do if only padding in a
6132 structure ? */
6133 /* zero size: means to pad */
6134 if (bit_pos > 0)
6135 bit_pos = bsize;
6136 } else {
6137 /* we do not have enough room ? */
6138 if ((bit_pos + bit_size) > bsize)
6139 bit_pos = 0;
6140 lbit_pos = bit_pos;
6141 /* XXX: handle LSB first */
6142 type1.t |= VT_BITFIELD |
6143 (bit_pos << VT_STRUCT_SHIFT) |
6144 (bit_size << (VT_STRUCT_SHIFT + 6));
6145 bit_pos += bit_size;
6147 } else {
6148 bit_pos = 0;
6150 if (v) {
6151 /* add new memory data only if starting
6152 bit field */
6153 if (lbit_pos == 0) {
6154 if (a == TOK_STRUCT) {
6155 c = (c + align - 1) & -align;
6156 offset = c;
6157 c += size;
6158 } else {
6159 offset = 0;
6160 if (size > c)
6161 c = size;
6163 if (align > maxalign)
6164 maxalign = align;
6166 #if 0
6167 printf("add field %s offset=%d",
6168 get_tok_str(v, NULL), offset);
6169 if (type1.t & VT_BITFIELD) {
6170 printf(" pos=%d size=%d",
6171 (type1.t >> VT_STRUCT_SHIFT) & 0x3f,
6172 (type1.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f);
6174 printf("\n");
6175 #endif
6176 ss = sym_push(v | SYM_FIELD, &type1, 0, offset);
6177 *ps = ss;
6178 ps = &ss->next;
6180 if (tok == ';' || tok == TOK_EOF)
6181 break;
6182 skip(',');
6184 skip(';');
6186 skip('}');
6187 /* store size and alignment */
6188 s->c = (c + maxalign - 1) & -maxalign;
6189 s->r = maxalign;
6194 /* return 0 if no type declaration. otherwise, return the basic type
6195 and skip it.
6197 static int parse_btype(CType *type, AttributeDef *ad)
6199 int t, u, type_found;
6200 Sym *s;
6201 CType type1;
6203 memset(ad, 0, sizeof(AttributeDef));
6204 type_found = 0;
6205 t = 0;
6206 while(1) {
6207 switch(tok) {
6208 case TOK_EXTENSION:
6209 /* currently, we really ignore extension */
6210 next();
6211 continue;
6213 /* basic types */
6214 case TOK_CHAR:
6215 u = VT_BYTE;
6216 basic_type:
6217 next();
6218 basic_type1:
6219 if ((t & VT_BTYPE) != 0)
6220 error("too many basic types");
6221 t |= u;
6222 break;
6223 case TOK_VOID:
6224 u = VT_VOID;
6225 goto basic_type;
6226 case TOK_SHORT:
6227 u = VT_SHORT;
6228 goto basic_type;
6229 case TOK_INT:
6230 next();
6231 break;
6232 case TOK_LONG:
6233 next();
6234 if ((t & VT_BTYPE) == VT_DOUBLE) {
6235 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
6236 } else if ((t & VT_BTYPE) == VT_LONG) {
6237 t = (t & ~VT_BTYPE) | VT_LLONG;
6238 } else {
6239 u = VT_LONG;
6240 goto basic_type1;
6242 break;
6243 case TOK_BOOL:
6244 u = VT_BOOL;
6245 goto basic_type;
6246 case TOK_FLOAT:
6247 u = VT_FLOAT;
6248 goto basic_type;
6249 case TOK_DOUBLE:
6250 next();
6251 if ((t & VT_BTYPE) == VT_LONG) {
6252 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
6253 } else {
6254 u = VT_DOUBLE;
6255 goto basic_type1;
6257 break;
6258 case TOK_ENUM:
6259 struct_decl(&type1, VT_ENUM);
6260 basic_type2:
6261 u = type1.t;
6262 type->ref = type1.ref;
6263 goto basic_type1;
6264 case TOK_STRUCT:
6265 case TOK_UNION:
6266 struct_decl(&type1, VT_STRUCT);
6267 goto basic_type2;
6269 /* type modifiers */
6270 case TOK_CONST1:
6271 case TOK_CONST2:
6272 case TOK_CONST3:
6273 t |= VT_CONSTANT;
6274 next();
6275 break;
6276 case TOK_VOLATILE1:
6277 case TOK_VOLATILE2:
6278 case TOK_VOLATILE3:
6279 t |= VT_VOLATILE;
6280 next();
6281 break;
6282 case TOK_REGISTER:
6283 case TOK_SIGNED1:
6284 case TOK_SIGNED2:
6285 case TOK_SIGNED3:
6286 case TOK_AUTO:
6287 case TOK_RESTRICT1:
6288 case TOK_RESTRICT2:
6289 case TOK_RESTRICT3:
6290 next();
6291 break;
6292 case TOK_UNSIGNED:
6293 t |= VT_UNSIGNED;
6294 next();
6295 break;
6297 /* storage */
6298 case TOK_EXTERN:
6299 t |= VT_EXTERN;
6300 next();
6301 break;
6302 case TOK_STATIC:
6303 t |= VT_STATIC;
6304 next();
6305 break;
6306 case TOK_TYPEDEF:
6307 t |= VT_TYPEDEF;
6308 next();
6309 break;
6310 case TOK_INLINE1:
6311 case TOK_INLINE2:
6312 case TOK_INLINE3:
6313 t |= VT_INLINE;
6314 next();
6315 break;
6317 /* GNUC attribute */
6318 case TOK_ATTRIBUTE1:
6319 case TOK_ATTRIBUTE2:
6320 parse_attribute(ad);
6321 break;
6322 /* GNUC typeof */
6323 case TOK_TYPEOF1:
6324 case TOK_TYPEOF2:
6325 case TOK_TYPEOF3:
6326 next();
6327 parse_expr_type(&type1);
6328 goto basic_type2;
6329 default:
6330 s = sym_find(tok);
6331 if (!s || !(s->type.t & VT_TYPEDEF))
6332 goto the_end;
6333 t |= (s->type.t & ~VT_TYPEDEF);
6334 type->ref = s->type.ref;
6335 next();
6336 break;
6338 type_found = 1;
6340 the_end:
6341 /* long is never used as type */
6342 if ((t & VT_BTYPE) == VT_LONG)
6343 t = (t & ~VT_BTYPE) | VT_INT;
6344 type->t = t;
6345 return type_found;
6348 /* convert a function parameter type (array to pointer and function to
6349 function pointer) */
6350 static inline void convert_parameter_type(CType *pt)
6352 /* array must be transformed to pointer according to ANSI C */
6353 pt->t &= ~VT_ARRAY;
6354 if ((pt->t & VT_BTYPE) == VT_FUNC) {
6355 mk_pointer(pt);
6359 static void post_type(CType *type, AttributeDef *ad)
6361 int n, l, t1;
6362 Sym **plast, *s, *first;
6363 AttributeDef ad1;
6364 CType pt;
6366 if (tok == '(') {
6367 /* function declaration */
6368 next();
6369 l = 0;
6370 first = NULL;
6371 plast = &first;
6372 while (tok != ')') {
6373 /* read param name and compute offset */
6374 if (l != FUNC_OLD) {
6375 if (!parse_btype(&pt, &ad1)) {
6376 if (l) {
6377 error("invalid type");
6378 } else {
6379 l = FUNC_OLD;
6380 goto old_proto;
6383 l = FUNC_NEW;
6384 if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
6385 break;
6386 type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
6387 if ((pt.t & VT_BTYPE) == VT_VOID)
6388 error("parameter declared as void");
6389 } else {
6390 old_proto:
6391 n = tok;
6392 pt.t = VT_INT;
6393 next();
6395 convert_parameter_type(&pt);
6396 s = sym_push(n | SYM_FIELD, &pt, 0, 0);
6397 *plast = s;
6398 plast = &s->next;
6399 if (tok == ',') {
6400 next();
6401 if (l == FUNC_NEW && tok == TOK_DOTS) {
6402 l = FUNC_ELLIPSIS;
6403 next();
6404 break;
6408 /* if no parameters, then old type prototype */
6409 if (l == 0)
6410 l = FUNC_OLD;
6411 skip(')');
6412 t1 = type->t & VT_STORAGE;
6413 /* NOTE: const is ignored in returned type as it has a special
6414 meaning in gcc / C++ */
6415 type->t &= ~(VT_STORAGE | VT_CONSTANT);
6416 post_type(type, ad);
6417 /* we push a anonymous symbol which will contain the function prototype */
6418 s = sym_push(SYM_FIELD, type, ad->func_call, l);
6419 s->next = first;
6420 type->t = t1 | VT_FUNC;
6421 type->ref = s;
6422 } else if (tok == '[') {
6423 /* array definition */
6424 next();
6425 n = -1;
6426 if (tok != ']') {
6427 n = expr_const();
6428 if (n < 0)
6429 error("invalid array size");
6431 skip(']');
6432 /* parse next post type */
6433 t1 = type->t & VT_STORAGE;
6434 type->t &= ~VT_STORAGE;
6435 post_type(type, ad);
6437 /* we push a anonymous symbol which will contain the array
6438 element type */
6439 s = sym_push(SYM_FIELD, type, 0, n);
6440 type->t = t1 | VT_ARRAY | VT_PTR;
6441 type->ref = s;
6445 /* Parse a type declaration (except basic type), and return the type
6446 in 'type'. 'td' is a bitmask indicating which kind of type decl is
6447 expected. 'type' should contain the basic type. 'ad' is the
6448 attribute definition of the basic type. It can be modified by
6449 type_decl().
6451 static void type_decl(CType *type, AttributeDef *ad, int *v, int td)
6453 Sym *s;
6454 CType type1, *type2;
6455 int qualifiers;
6457 while (tok == '*') {
6458 qualifiers = 0;
6459 redo:
6460 next();
6461 switch(tok) {
6462 case TOK_CONST1:
6463 case TOK_CONST2:
6464 case TOK_CONST3:
6465 qualifiers |= VT_CONSTANT;
6466 goto redo;
6467 case TOK_VOLATILE1:
6468 case TOK_VOLATILE2:
6469 case TOK_VOLATILE3:
6470 qualifiers |= VT_VOLATILE;
6471 goto redo;
6472 case TOK_RESTRICT1:
6473 case TOK_RESTRICT2:
6474 case TOK_RESTRICT3:
6475 goto redo;
6477 mk_pointer(type);
6478 type->t |= qualifiers;
6481 /* XXX: clarify attribute handling */
6482 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
6483 parse_attribute(ad);
6485 /* recursive type */
6486 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
6487 type1.t = 0; /* XXX: same as int */
6488 if (tok == '(') {
6489 next();
6490 /* XXX: this is not correct to modify 'ad' at this point, but
6491 the syntax is not clear */
6492 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
6493 parse_attribute(ad);
6494 type_decl(&type1, ad, v, td);
6495 skip(')');
6496 } else {
6497 /* type identifier */
6498 if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
6499 *v = tok;
6500 next();
6501 } else {
6502 if (!(td & TYPE_ABSTRACT))
6503 expect("identifier");
6504 *v = 0;
6507 post_type(type, ad);
6508 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
6509 parse_attribute(ad);
6510 if (!type1.t)
6511 return;
6512 /* append type at the end of type1 */
6513 type2 = &type1;
6514 for(;;) {
6515 s = type2->ref;
6516 type2 = &s->type;
6517 if (!type2->t) {
6518 *type2 = *type;
6519 break;
6522 *type = type1;
6525 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
6526 static int lvalue_type(int t)
6528 int bt, r;
6529 r = VT_LVAL;
6530 bt = t & VT_BTYPE;
6531 if (bt == VT_BYTE || bt == VT_BOOL)
6532 r |= VT_LVAL_BYTE;
6533 else if (bt == VT_SHORT)
6534 r |= VT_LVAL_SHORT;
6535 else
6536 return r;
6537 if (t & VT_UNSIGNED)
6538 r |= VT_LVAL_UNSIGNED;
6539 return r;
6542 /* indirection with full error checking and bound check */
6543 static void indir(void)
6545 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
6546 expect("pointer");
6547 if ((vtop->r & VT_LVAL) && !nocode_wanted)
6548 gv(RC_INT);
6549 vtop->type = *pointed_type(&vtop->type);
6550 /* an array is never an lvalue */
6551 if (!(vtop->type.t & VT_ARRAY)) {
6552 vtop->r |= lvalue_type(vtop->type.t);
6553 /* if bound checking, the referenced pointer must be checked */
6554 if (do_bounds_check)
6555 vtop->r |= VT_MUSTBOUND;
6559 /* pass a parameter to a function and do type checking and casting */
6560 static void gfunc_param_typed(Sym *func, Sym *arg)
6562 int func_type;
6563 CType type;
6565 func_type = func->c;
6566 if (func_type == FUNC_OLD ||
6567 (func_type == FUNC_ELLIPSIS && arg == NULL)) {
6568 /* default casting : only need to convert float to double */
6569 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) {
6570 type.t = VT_DOUBLE;
6571 gen_cast(&type);
6573 } else if (arg == NULL) {
6574 error("too many arguments to function");
6575 } else {
6576 gen_assign_cast(&arg->type);
6580 /* parse an expression of the form '(type)' or '(expr)' and return its
6581 type */
6582 static void parse_expr_type(CType *type)
6584 int n;
6585 AttributeDef ad;
6587 skip('(');
6588 if (parse_btype(type, &ad)) {
6589 type_decl(type, &ad, &n, TYPE_ABSTRACT);
6590 } else {
6591 expr_type(type);
6593 skip(')');
6596 static void parse_type(CType *type)
6598 AttributeDef ad;
6599 int n;
6601 if (!parse_btype(type, &ad)) {
6602 expect("type");
6604 type_decl(type, &ad, &n, TYPE_ABSTRACT);
6607 static void vpush_tokc(int t)
6609 CType type;
6610 type.t = t;
6611 vsetc(&type, VT_CONST, &tokc);
6614 static void unary(void)
6616 int n, t, align, size, r;
6617 CType type;
6618 Sym *s;
6619 AttributeDef ad;
6621 /* XXX: GCC 2.95.3 does not generate a table although it should be
6622 better here */
6623 tok_next:
6624 switch(tok) {
6625 case TOK_EXTENSION:
6626 next();
6627 goto tok_next;
6628 case TOK_CINT:
6629 case TOK_CCHAR:
6630 case TOK_LCHAR:
6631 vpushi(tokc.i);
6632 next();
6633 break;
6634 case TOK_CUINT:
6635 vpush_tokc(VT_INT | VT_UNSIGNED);
6636 next();
6637 break;
6638 case TOK_CLLONG:
6639 vpush_tokc(VT_LLONG);
6640 next();
6641 break;
6642 case TOK_CULLONG:
6643 vpush_tokc(VT_LLONG | VT_UNSIGNED);
6644 next();
6645 break;
6646 case TOK_CFLOAT:
6647 vpush_tokc(VT_FLOAT);
6648 next();
6649 break;
6650 case TOK_CDOUBLE:
6651 vpush_tokc(VT_DOUBLE);
6652 next();
6653 break;
6654 case TOK_CLDOUBLE:
6655 vpush_tokc(VT_LDOUBLE);
6656 next();
6657 break;
6658 case TOK___FUNCTION__:
6659 if (!gnu_ext)
6660 goto tok_identifier;
6661 /* fall thru */
6662 case TOK___FUNC__:
6664 void *ptr;
6665 int len;
6666 /* special function name identifier */
6667 len = strlen(funcname) + 1;
6668 /* generate char[len] type */
6669 type.t = VT_BYTE;
6670 mk_pointer(&type);
6671 type.t |= VT_ARRAY;
6672 type.ref->c = len;
6673 vpush_ref(&type, data_section, data_section->data_offset, len);
6674 ptr = section_ptr_add(data_section, len);
6675 memcpy(ptr, funcname, len);
6676 next();
6678 break;
6679 case TOK_LSTR:
6680 t = VT_INT;
6681 goto str_init;
6682 case TOK_STR:
6683 /* string parsing */
6684 t = VT_BYTE;
6685 str_init:
6686 if (tcc_state->warn_write_strings)
6687 t |= VT_CONSTANT;
6688 type.t = t;
6689 mk_pointer(&type);
6690 type.t |= VT_ARRAY;
6691 memset(&ad, 0, sizeof(AttributeDef));
6692 decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, 0);
6693 break;
6694 case '(':
6695 next();
6696 /* cast ? */
6697 if (parse_btype(&type, &ad)) {
6698 type_decl(&type, &ad, &n, TYPE_ABSTRACT);
6699 skip(')');
6700 /* check ISOC99 compound literal */
6701 if (tok == '{') {
6702 /* data is allocated locally by default */
6703 if (global_expr)
6704 r = VT_CONST;
6705 else
6706 r = VT_LOCAL;
6707 /* all except arrays are lvalues */
6708 if (!(type.t & VT_ARRAY))
6709 r |= lvalue_type(type.t);
6710 memset(&ad, 0, sizeof(AttributeDef));
6711 decl_initializer_alloc(&type, &ad, r, 1, 0, 0);
6712 } else {
6713 unary();
6714 gen_cast(&type);
6716 } else if (tok == '{') {
6717 /* save all registers */
6718 save_regs(0);
6719 /* statement expression : we do not accept break/continue
6720 inside as GCC does */
6721 block(NULL, NULL, NULL, NULL, 0, 1);
6722 skip(')');
6723 } else {
6724 gexpr();
6725 skip(')');
6727 break;
6728 case '*':
6729 next();
6730 unary();
6731 indir();
6732 break;
6733 case '&':
6734 next();
6735 unary();
6736 /* functions names must be treated as function pointers,
6737 except for unary '&' and sizeof. Since we consider that
6738 functions are not lvalues, we only have to handle it
6739 there and in function calls. */
6740 /* arrays can also be used although they are not lvalues */
6741 if ((vtop->type.t & VT_BTYPE) != VT_FUNC &&
6742 !(vtop->type.t & VT_ARRAY))
6743 test_lvalue();
6744 mk_pointer(&vtop->type);
6745 gaddrof();
6746 break;
6747 case '!':
6748 next();
6749 unary();
6750 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST)
6751 vtop->c.i = !vtop->c.i;
6752 else if ((vtop->r & VT_VALMASK) == VT_CMP)
6753 vtop->c.i = vtop->c.i ^ 1;
6754 else
6755 vseti(VT_JMP, gtst(1, 0));
6756 break;
6757 case '~':
6758 next();
6759 unary();
6760 vpushi(-1);
6761 gen_op('^');
6762 break;
6763 case '+':
6764 next();
6765 /* in order to force cast, we add zero */
6766 unary();
6767 if ((vtop->type.t & VT_BTYPE) == VT_PTR)
6768 error("pointer not accepted for unary plus");
6769 vpushi(0);
6770 gen_op('+');
6771 break;
6772 case TOK_SIZEOF:
6773 case TOK_ALIGNOF1:
6774 case TOK_ALIGNOF2:
6775 t = tok;
6776 next();
6777 if (tok == '(') {
6778 parse_expr_type(&type);
6779 } else {
6780 unary_type(&type);
6782 size = type_size(&type, &align);
6783 if (t == TOK_SIZEOF) {
6784 if (size < 0)
6785 error("sizeof applied to an incomplete type");
6786 vpushi(size);
6787 } else {
6788 vpushi(align);
6790 break;
6792 case TOK_builtin_types_compatible_p:
6794 CType type1, type2;
6795 next();
6796 skip('(');
6797 parse_type(&type1);
6798 skip(',');
6799 parse_type(&type2);
6800 skip(')');
6801 type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
6802 type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
6803 vpushi(is_compatible_types(&type1, &type2));
6805 break;
6806 case TOK_builtin_constant_p:
6808 int saved_nocode_wanted, res;
6809 next();
6810 skip('(');
6811 saved_nocode_wanted = nocode_wanted;
6812 nocode_wanted = 1;
6813 gexpr();
6814 res = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
6815 vpop();
6816 nocode_wanted = saved_nocode_wanted;
6817 skip(')');
6818 vpushi(res);
6820 break;
6821 case TOK_INC:
6822 case TOK_DEC:
6823 t = tok;
6824 next();
6825 unary();
6826 inc(0, t);
6827 break;
6828 case '-':
6829 next();
6830 vpushi(0);
6831 unary();
6832 gen_op('-');
6833 break;
6834 case TOK_LAND:
6835 if (!gnu_ext)
6836 goto tok_identifier;
6837 next();
6838 /* allow to take the address of a label */
6839 if (tok < TOK_UIDENT)
6840 expect("label identifier");
6841 s = label_find(tok);
6842 if (!s) {
6843 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
6844 } else {
6845 if (s->r == LABEL_DECLARED)
6846 s->r = LABEL_FORWARD;
6848 if (!s->type.t) {
6849 s->type.t = VT_VOID;
6850 mk_pointer(&s->type);
6851 s->type.t |= VT_STATIC;
6853 vset(&s->type, VT_CONST | VT_SYM, 0);
6854 vtop->sym = s;
6855 next();
6856 break;
6857 default:
6858 tok_identifier:
6859 t = tok;
6860 next();
6861 if (t < TOK_UIDENT)
6862 expect("identifier");
6863 s = sym_find(t);
6864 if (!s) {
6865 if (tok != '(')
6866 error("'%s' undeclared", get_tok_str(t, NULL));
6867 /* for simple function calls, we tolerate undeclared
6868 external reference to int() function */
6869 s = external_global_sym(t, &func_old_type, 0);
6871 vset(&s->type, s->r, s->c);
6872 /* if forward reference, we must point to s */
6873 if (vtop->r & VT_SYM) {
6874 vtop->sym = s;
6875 vtop->c.ul = 0;
6877 break;
6880 /* post operations */
6881 while (1) {
6882 if (tok == TOK_INC || tok == TOK_DEC) {
6883 inc(1, tok);
6884 next();
6885 } else if (tok == '.' || tok == TOK_ARROW) {
6886 /* field */
6887 if (tok == TOK_ARROW)
6888 indir();
6889 test_lvalue();
6890 gaddrof();
6891 next();
6892 /* expect pointer on structure */
6893 if ((vtop->type.t & VT_BTYPE) != VT_STRUCT)
6894 expect("struct or union");
6895 s = vtop->type.ref;
6896 /* find field */
6897 tok |= SYM_FIELD;
6898 while ((s = s->next) != NULL) {
6899 if (s->v == tok)
6900 break;
6902 if (!s)
6903 error("field not found");
6904 /* add field offset to pointer */
6905 vtop->type = char_pointer_type; /* change type to 'char *' */
6906 vpushi(s->c);
6907 gen_op('+');
6908 /* change type to field type, and set to lvalue */
6909 vtop->type = s->type;
6910 /* an array is never an lvalue */
6911 if (!(vtop->type.t & VT_ARRAY)) {
6912 vtop->r |= lvalue_type(vtop->type.t);
6913 /* if bound checking, the referenced pointer must be checked */
6914 if (do_bounds_check)
6915 vtop->r |= VT_MUSTBOUND;
6917 next();
6918 } else if (tok == '[') {
6919 next();
6920 gexpr();
6921 gen_op('+');
6922 indir();
6923 skip(']');
6924 } else if (tok == '(') {
6925 SValue ret;
6926 Sym *sa;
6927 int nb_args;
6929 /* function call */
6930 if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
6931 /* pointer test (no array accepted) */
6932 if ((vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
6933 vtop->type = *pointed_type(&vtop->type);
6934 if ((vtop->type.t & VT_BTYPE) != VT_FUNC)
6935 goto error_func;
6936 } else {
6937 error_func:
6938 expect("function pointer");
6940 } else {
6941 vtop->r &= ~VT_LVAL; /* no lvalue */
6943 /* get return type */
6944 s = vtop->type.ref;
6945 next();
6946 sa = s->next; /* first parameter */
6947 nb_args = 0;
6948 /* compute first implicit argument if a structure is returned */
6949 if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
6950 /* get some space for the returned structure */
6951 size = type_size(&s->type, &align);
6952 loc = (loc - size) & -align;
6953 ret.type = s->type;
6954 ret.r = VT_LOCAL | VT_LVAL;
6955 /* pass it as 'int' to avoid structure arg passing
6956 problems */
6957 vseti(VT_LOCAL, loc);
6958 ret.c = vtop->c;
6959 nb_args++;
6960 } else {
6961 ret.type = s->type;
6962 ret.r2 = VT_CONST;
6963 /* return in register */
6964 if (is_float(ret.type.t)) {
6965 ret.r = REG_FRET;
6966 } else {
6967 if ((ret.type.t & VT_BTYPE) == VT_LLONG)
6968 ret.r2 = REG_LRET;
6969 ret.r = REG_IRET;
6971 ret.c.i = 0;
6973 if (tok != ')') {
6974 for(;;) {
6975 expr_eq();
6976 gfunc_param_typed(s, sa);
6977 nb_args++;
6978 if (sa)
6979 sa = sa->next;
6980 if (tok == ')')
6981 break;
6982 skip(',');
6985 if (sa)
6986 error("too few arguments to function");
6987 skip(')');
6988 if (!nocode_wanted) {
6989 gfunc_call(nb_args);
6990 } else {
6991 vtop -= (nb_args + 1);
6993 /* return value */
6994 vsetc(&ret.type, ret.r, &ret.c);
6995 vtop->r2 = ret.r2;
6996 } else {
6997 break;
7002 static void uneq(void)
7004 int t;
7006 unary();
7007 if (tok == '=' ||
7008 (tok >= TOK_A_MOD && tok <= TOK_A_DIV) ||
7009 tok == TOK_A_XOR || tok == TOK_A_OR ||
7010 tok == TOK_A_SHL || tok == TOK_A_SAR) {
7011 test_lvalue();
7012 t = tok;
7013 next();
7014 if (t == '=') {
7015 expr_eq();
7016 } else {
7017 vdup();
7018 expr_eq();
7019 gen_op(t & 0x7f);
7021 vstore();
7025 static void expr_prod(void)
7027 int t;
7029 uneq();
7030 while (tok == '*' || tok == '/' || tok == '%') {
7031 t = tok;
7032 next();
7033 uneq();
7034 gen_op(t);
7038 static void expr_sum(void)
7040 int t;
7042 expr_prod();
7043 while (tok == '+' || tok == '-') {
7044 t = tok;
7045 next();
7046 expr_prod();
7047 gen_op(t);
7051 static void expr_shift(void)
7053 int t;
7055 expr_sum();
7056 while (tok == TOK_SHL || tok == TOK_SAR) {
7057 t = tok;
7058 next();
7059 expr_sum();
7060 gen_op(t);
7064 static void expr_cmp(void)
7066 int t;
7068 expr_shift();
7069 while ((tok >= TOK_ULE && tok <= TOK_GT) ||
7070 tok == TOK_ULT || tok == TOK_UGE) {
7071 t = tok;
7072 next();
7073 expr_shift();
7074 gen_op(t);
7078 static void expr_cmpeq(void)
7080 int t;
7082 expr_cmp();
7083 while (tok == TOK_EQ || tok == TOK_NE) {
7084 t = tok;
7085 next();
7086 expr_cmp();
7087 gen_op(t);
7091 static void expr_and(void)
7093 expr_cmpeq();
7094 while (tok == '&') {
7095 next();
7096 expr_cmpeq();
7097 gen_op('&');
7101 static void expr_xor(void)
7103 expr_and();
7104 while (tok == '^') {
7105 next();
7106 expr_and();
7107 gen_op('^');
7111 static void expr_or(void)
7113 expr_xor();
7114 while (tok == '|') {
7115 next();
7116 expr_xor();
7117 gen_op('|');
7121 /* XXX: fix this mess */
7122 static void expr_land_const(void)
7124 expr_or();
7125 while (tok == TOK_LAND) {
7126 next();
7127 expr_or();
7128 gen_op(TOK_LAND);
7132 /* XXX: fix this mess */
7133 static void expr_lor_const(void)
7135 expr_land_const();
7136 while (tok == TOK_LOR) {
7137 next();
7138 expr_land_const();
7139 gen_op(TOK_LOR);
7143 /* only used if non constant */
7144 static void expr_land(void)
7146 int t;
7148 expr_or();
7149 if (tok == TOK_LAND) {
7150 t = 0;
7151 for(;;) {
7152 t = gtst(1, t);
7153 if (tok != TOK_LAND) {
7154 vseti(VT_JMPI, t);
7155 break;
7157 next();
7158 expr_or();
7163 static void expr_lor(void)
7165 int t;
7167 expr_land();
7168 if (tok == TOK_LOR) {
7169 t = 0;
7170 for(;;) {
7171 t = gtst(0, t);
7172 if (tok != TOK_LOR) {
7173 vseti(VT_JMP, t);
7174 break;
7176 next();
7177 expr_land();
7182 /* XXX: better constant handling */
7183 static void expr_eq(void)
7185 int tt, u, r1, r2, rc, t1, t2, bt1, bt2;
7186 SValue sv;
7187 CType type, type1, type2;
7189 if (const_wanted) {
7190 int c1, c;
7191 expr_lor_const();
7192 if (tok == '?') {
7193 c = vtop->c.i;
7194 vpop();
7195 next();
7196 if (tok == ':' && gnu_ext) {
7197 c1 = c;
7198 } else {
7199 gexpr();
7200 c1 = vtop->c.i;
7201 vpop();
7203 skip(':');
7204 expr_eq();
7205 if (c)
7206 vtop->c.i = c1;
7208 } else {
7209 expr_lor();
7210 if (tok == '?') {
7211 next();
7212 if (vtop != vstack) {
7213 /* needed to avoid having different registers saved in
7214 each branch */
7215 if (is_float(vtop->type.t))
7216 rc = RC_FLOAT;
7217 else
7218 rc = RC_INT;
7219 gv(rc);
7220 save_regs(1);
7222 if (tok == ':' && gnu_ext) {
7223 gv_dup();
7224 tt = gtst(1, 0);
7225 } else {
7226 tt = gtst(1, 0);
7227 gexpr();
7229 type1 = vtop->type;
7230 sv = *vtop; /* save value to handle it later */
7231 vtop--; /* no vpop so that FP stack is not flushed */
7232 skip(':');
7233 u = gjmp(0);
7234 gsym(tt);
7235 expr_eq();
7236 type2 = vtop->type;
7238 t1 = type1.t;
7239 bt1 = t1 & VT_BTYPE;
7240 t2 = type2.t;
7241 bt2 = t2 & VT_BTYPE;
7242 /* cast operands to correct type according to ISOC rules */
7243 if (is_float(bt1) || is_float(bt2)) {
7244 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
7245 type.t = VT_LDOUBLE;
7246 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
7247 type.t = VT_DOUBLE;
7248 } else {
7249 type.t = VT_FLOAT;
7251 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
7252 /* cast to biggest op */
7253 type.t = VT_LLONG;
7254 /* convert to unsigned if it does not fit in a long long */
7255 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
7256 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
7257 type.t |= VT_UNSIGNED;
7258 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
7259 /* XXX: test pointer compatibility */
7260 type = type1;
7261 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
7262 /* XXX: test structure compatibility */
7263 type = type1;
7264 } else if (bt1 == VT_VOID || bt2 == VT_VOID) {
7265 /* NOTE: as an extension, we accept void on only one side */
7266 type.t = VT_VOID;
7267 } else {
7268 /* integer operations */
7269 type.t = VT_INT;
7270 /* convert to unsigned if it does not fit in an integer */
7271 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
7272 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
7273 type.t |= VT_UNSIGNED;
7276 /* now we convert second operand */
7277 gen_cast(&type);
7278 rc = RC_INT;
7279 if (is_float(type.t)) {
7280 rc = RC_FLOAT;
7281 } else if ((type.t & VT_BTYPE) == VT_LLONG) {
7282 /* for long longs, we use fixed registers to avoid having
7283 to handle a complicated move */
7284 rc = RC_IRET;
7287 r2 = gv(rc);
7288 /* this is horrible, but we must also convert first
7289 operand */
7290 tt = gjmp(0);
7291 gsym(u);
7292 /* put again first value and cast it */
7293 *vtop = sv;
7294 gen_cast(&type);
7295 r1 = gv(rc);
7296 move_reg(r2, r1);
7297 vtop->r = r2;
7298 gsym(tt);
7303 static void gexpr(void)
7305 while (1) {
7306 expr_eq();
7307 if (tok != ',')
7308 break;
7309 vpop();
7310 next();
7314 /* parse an expression and return its type without any side effect. */
7315 static void expr_type(CType *type)
7317 int saved_nocode_wanted;
7319 saved_nocode_wanted = nocode_wanted;
7320 nocode_wanted = 1;
7321 gexpr();
7322 *type = vtop->type;
7323 vpop();
7324 nocode_wanted = saved_nocode_wanted;
7327 /* parse a unary expression and return its type without any side
7328 effect. */
7329 static void unary_type(CType *type)
7331 int a;
7333 a = nocode_wanted;
7334 nocode_wanted = 1;
7335 unary();
7336 *type = vtop->type;
7337 vpop();
7338 nocode_wanted = a;
7341 /* parse a constant expression and return value in vtop. */
7342 static void expr_const1(void)
7344 int a;
7345 a = const_wanted;
7346 const_wanted = 1;
7347 expr_eq();
7348 const_wanted = a;
7351 /* parse an integer constant and return its value. */
7352 static int expr_const(void)
7354 int c;
7355 expr_const1();
7356 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
7357 expect("constant expression");
7358 c = vtop->c.i;
7359 vpop();
7360 return c;
7363 /* return the label token if current token is a label, otherwise
7364 return zero */
7365 static int is_label(void)
7367 int last_tok;
7369 /* fast test first */
7370 if (tok < TOK_UIDENT)
7371 return 0;
7372 /* no need to save tokc because tok is an identifier */
7373 last_tok = tok;
7374 next();
7375 if (tok == ':') {
7376 next();
7377 return last_tok;
7378 } else {
7379 unget_tok(last_tok);
7380 return 0;
7384 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
7385 int case_reg, int is_expr)
7387 int a, b, c, d;
7388 Sym *s;
7390 /* generate line number info */
7391 if (do_debug &&
7392 (last_line_num != file->line_num || last_ind != ind)) {
7393 put_stabn(N_SLINE, 0, file->line_num, ind - func_ind);
7394 last_ind = ind;
7395 last_line_num = file->line_num;
7398 if (is_expr) {
7399 /* default return value is (void) */
7400 vpushi(0);
7401 vtop->type.t = VT_VOID;
7404 if (tok == TOK_IF) {
7405 /* if test */
7406 next();
7407 skip('(');
7408 gexpr();
7409 skip(')');
7410 a = gtst(1, 0);
7411 block(bsym, csym, case_sym, def_sym, case_reg, 0);
7412 c = tok;
7413 if (c == TOK_ELSE) {
7414 next();
7415 d = gjmp(0);
7416 gsym(a);
7417 block(bsym, csym, case_sym, def_sym, case_reg, 0);
7418 gsym(d); /* patch else jmp */
7419 } else
7420 gsym(a);
7421 } else if (tok == TOK_WHILE) {
7422 next();
7423 d = ind;
7424 skip('(');
7425 gexpr();
7426 skip(')');
7427 a = gtst(1, 0);
7428 b = 0;
7429 block(&a, &b, case_sym, def_sym, case_reg, 0);
7430 gjmp_addr(d);
7431 gsym(a);
7432 gsym_addr(b, d);
7433 } else if (tok == '{') {
7434 Sym *llabel;
7436 next();
7437 /* record local declaration stack position */
7438 s = local_stack;
7439 llabel = local_label_stack;
7440 /* handle local labels declarations */
7441 if (tok == TOK_LABEL) {
7442 next();
7443 for(;;) {
7444 if (tok < TOK_UIDENT)
7445 expect("label identifier");
7446 label_push(&local_label_stack, tok, LABEL_DECLARED);
7447 next();
7448 if (tok == ',') {
7449 next();
7450 } else {
7451 skip(';');
7452 break;
7456 while (tok != '}') {
7457 decl(VT_LOCAL);
7458 if (tok != '}') {
7459 if (is_expr)
7460 vpop();
7461 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
7464 /* pop locally defined labels */
7465 label_pop(&local_label_stack, llabel);
7466 /* pop locally defined symbols */
7467 sym_pop(&local_stack, s);
7468 next();
7469 } else if (tok == TOK_RETURN) {
7470 next();
7471 if (tok != ';') {
7472 gexpr();
7473 gen_assign_cast(&func_vt);
7474 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
7475 CType type;
7476 /* if returning structure, must copy it to implicit
7477 first pointer arg location */
7478 type = func_vt;
7479 mk_pointer(&type);
7480 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
7481 indir();
7482 vswap();
7483 /* copy structure value to pointer */
7484 vstore();
7485 } else if (is_float(func_vt.t)) {
7486 gv(RC_FRET);
7487 } else {
7488 gv(RC_IRET);
7490 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
7492 skip(';');
7493 rsym = gjmp(rsym); /* jmp */
7494 } else if (tok == TOK_BREAK) {
7495 /* compute jump */
7496 if (!bsym)
7497 error("cannot break");
7498 *bsym = gjmp(*bsym);
7499 next();
7500 skip(';');
7501 } else if (tok == TOK_CONTINUE) {
7502 /* compute jump */
7503 if (!csym)
7504 error("cannot continue");
7505 *csym = gjmp(*csym);
7506 next();
7507 skip(';');
7508 } else if (tok == TOK_FOR) {
7509 int e;
7510 next();
7511 skip('(');
7512 if (tok != ';') {
7513 gexpr();
7514 vpop();
7516 skip(';');
7517 d = ind;
7518 c = ind;
7519 a = 0;
7520 b = 0;
7521 if (tok != ';') {
7522 gexpr();
7523 a = gtst(1, 0);
7525 skip(';');
7526 if (tok != ')') {
7527 e = gjmp(0);
7528 c = ind;
7529 gexpr();
7530 vpop();
7531 gjmp_addr(d);
7532 gsym(e);
7534 skip(')');
7535 block(&a, &b, case_sym, def_sym, case_reg, 0);
7536 gjmp_addr(c);
7537 gsym(a);
7538 gsym_addr(b, c);
7539 } else
7540 if (tok == TOK_DO) {
7541 next();
7542 a = 0;
7543 b = 0;
7544 d = ind;
7545 block(&a, &b, case_sym, def_sym, case_reg, 0);
7546 skip(TOK_WHILE);
7547 skip('(');
7548 gsym(b);
7549 gexpr();
7550 c = gtst(0, 0);
7551 gsym_addr(c, d);
7552 skip(')');
7553 gsym(a);
7554 skip(';');
7555 } else
7556 if (tok == TOK_SWITCH) {
7557 next();
7558 skip('(');
7559 gexpr();
7560 /* XXX: other types than integer */
7561 case_reg = gv(RC_INT);
7562 vpop();
7563 skip(')');
7564 a = 0;
7565 b = gjmp(0); /* jump to first case */
7566 c = 0;
7567 block(&a, csym, &b, &c, case_reg, 0);
7568 /* if no default, jmp after switch */
7569 if (c == 0)
7570 c = ind;
7571 /* default label */
7572 gsym_addr(b, c);
7573 /* break label */
7574 gsym(a);
7575 } else
7576 if (tok == TOK_CASE) {
7577 int v1, v2;
7578 if (!case_sym)
7579 expect("switch");
7580 next();
7581 v1 = expr_const();
7582 v2 = v1;
7583 if (gnu_ext && tok == TOK_DOTS) {
7584 next();
7585 v2 = expr_const();
7586 if (v2 < v1)
7587 warning("empty case range");
7589 /* since a case is like a label, we must skip it with a jmp */
7590 b = gjmp(0);
7591 gsym(*case_sym);
7592 vseti(case_reg, 0);
7593 vpushi(v1);
7594 if (v1 == v2) {
7595 gen_op(TOK_EQ);
7596 *case_sym = gtst(1, 0);
7597 } else {
7598 gen_op(TOK_GE);
7599 *case_sym = gtst(1, 0);
7600 vseti(case_reg, 0);
7601 vpushi(v2);
7602 gen_op(TOK_LE);
7603 *case_sym = gtst(1, *case_sym);
7605 gsym(b);
7606 skip(':');
7607 is_expr = 0;
7608 goto block_after_label;
7609 } else
7610 if (tok == TOK_DEFAULT) {
7611 next();
7612 skip(':');
7613 if (!def_sym)
7614 expect("switch");
7615 if (*def_sym)
7616 error("too many 'default'");
7617 *def_sym = ind;
7618 is_expr = 0;
7619 goto block_after_label;
7620 } else
7621 if (tok == TOK_GOTO) {
7622 next();
7623 if (tok == '*' && gnu_ext) {
7624 /* computed goto */
7625 next();
7626 gexpr();
7627 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
7628 expect("pointer");
7629 ggoto();
7630 } else if (tok >= TOK_UIDENT) {
7631 s = label_find(tok);
7632 /* put forward definition if needed */
7633 if (!s) {
7634 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
7635 } else {
7636 if (s->r == LABEL_DECLARED)
7637 s->r = LABEL_FORWARD;
7639 /* label already defined */
7640 if (s->r & LABEL_FORWARD)
7641 s->next = (void *)gjmp((long)s->next);
7642 else
7643 gjmp_addr((long)s->next);
7644 next();
7645 } else {
7646 expect("label identifier");
7648 skip(';');
7649 } else if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) {
7650 asm_instr();
7651 } else {
7652 b = is_label();
7653 if (b) {
7654 /* label case */
7655 s = label_find(b);
7656 if (s) {
7657 if (s->r == LABEL_DEFINED)
7658 error("duplicate label '%s'", get_tok_str(s->v, NULL));
7659 gsym((long)s->next);
7660 s->r = LABEL_DEFINED;
7661 } else {
7662 s = label_push(&global_label_stack, b, LABEL_DEFINED);
7664 s->next = (void *)ind;
7665 /* we accept this, but it is a mistake */
7666 block_after_label:
7667 if (tok == '}') {
7668 warning("deprecated use of label at end of compound statement");
7669 } else {
7670 if (is_expr)
7671 vpop();
7672 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
7674 } else {
7675 /* expression case */
7676 if (tok != ';') {
7677 if (is_expr) {
7678 vpop();
7679 gexpr();
7680 } else {
7681 gexpr();
7682 vpop();
7685 skip(';');
7690 /* t is the array or struct type. c is the array or struct
7691 address. cur_index/cur_field is the pointer to the current
7692 value. 'size_only' is true if only size info is needed (only used
7693 in arrays) */
7694 static void decl_designator(CType *type, Section *sec, unsigned long c,
7695 int *cur_index, Sym **cur_field,
7696 int size_only)
7698 Sym *s, *f;
7699 int notfirst, index, index_last, align, l, nb_elems, elem_size;
7700 CType type1;
7702 notfirst = 0;
7703 elem_size = 0;
7704 nb_elems = 1;
7705 if (gnu_ext && (l = is_label()) != 0)
7706 goto struct_field;
7707 while (tok == '[' || tok == '.') {
7708 if (tok == '[') {
7709 if (!(type->t & VT_ARRAY))
7710 expect("array type");
7711 s = type->ref;
7712 next();
7713 index = expr_const();
7714 if (index < 0 || (s->c >= 0 && index >= s->c))
7715 expect("invalid index");
7716 if (tok == TOK_DOTS && gnu_ext) {
7717 next();
7718 index_last = expr_const();
7719 if (index_last < 0 ||
7720 (s->c >= 0 && index_last >= s->c) ||
7721 index_last < index)
7722 expect("invalid index");
7723 } else {
7724 index_last = index;
7726 skip(']');
7727 if (!notfirst)
7728 *cur_index = index_last;
7729 type = pointed_type(type);
7730 elem_size = type_size(type, &align);
7731 c += index * elem_size;
7732 /* NOTE: we only support ranges for last designator */
7733 nb_elems = index_last - index + 1;
7734 if (nb_elems != 1) {
7735 notfirst = 1;
7736 break;
7738 } else {
7739 next();
7740 l = tok;
7741 next();
7742 struct_field:
7743 if ((type->t & VT_BTYPE) != VT_STRUCT)
7744 expect("struct/union type");
7745 s = type->ref;
7746 l |= SYM_FIELD;
7747 f = s->next;
7748 while (f) {
7749 if (f->v == l)
7750 break;
7751 f = f->next;
7753 if (!f)
7754 expect("field");
7755 if (!notfirst)
7756 *cur_field = f;
7757 /* XXX: fix this mess by using explicit storage field */
7758 type1 = f->type;
7759 type1.t |= (type->t & ~VT_TYPE);
7760 type = &type1;
7761 c += f->c;
7763 notfirst = 1;
7765 if (notfirst) {
7766 if (tok == '=') {
7767 next();
7768 } else {
7769 if (!gnu_ext)
7770 expect("=");
7772 } else {
7773 if (type->t & VT_ARRAY) {
7774 index = *cur_index;
7775 type = pointed_type(type);
7776 c += index * type_size(type, &align);
7777 } else {
7778 f = *cur_field;
7779 if (!f)
7780 error("too many field init");
7781 /* XXX: fix this mess by using explicit storage field */
7782 type1 = f->type;
7783 type1.t |= (type->t & ~VT_TYPE);
7784 type = &type1;
7785 c += f->c;
7788 decl_initializer(type, sec, c, 0, size_only);
7790 /* XXX: make it more general */
7791 if (!size_only && nb_elems > 1) {
7792 unsigned long c_end;
7793 uint8_t *src, *dst;
7794 int i;
7796 if (!sec)
7797 error("range init not supported yet for dynamic storage");
7798 c_end = c + nb_elems * elem_size;
7799 if (c_end > sec->data_allocated)
7800 section_realloc(sec, c_end);
7801 src = sec->data + c;
7802 dst = src;
7803 for(i = 1; i < nb_elems; i++) {
7804 dst += elem_size;
7805 memcpy(dst, src, elem_size);
7810 #define EXPR_VAL 0
7811 #define EXPR_CONST 1
7812 #define EXPR_ANY 2
7814 /* store a value or an expression directly in global data or in local array */
7815 static void init_putv(CType *type, Section *sec, unsigned long c,
7816 int v, int expr_type)
7818 int saved_global_expr, bt, bit_pos, bit_size;
7819 void *ptr;
7820 unsigned long long bit_mask;
7821 CType dtype;
7823 switch(expr_type) {
7824 case EXPR_VAL:
7825 vpushi(v);
7826 break;
7827 case EXPR_CONST:
7828 /* compound literals must be allocated globally in this case */
7829 saved_global_expr = global_expr;
7830 global_expr = 1;
7831 expr_const1();
7832 global_expr = saved_global_expr;
7833 /* NOTE: symbols are accepted */
7834 if ((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST)
7835 error("initializer element is not constant");
7836 break;
7837 case EXPR_ANY:
7838 expr_eq();
7839 break;
7842 dtype = *type;
7843 dtype.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
7845 if (sec) {
7846 /* XXX: not portable */
7847 /* XXX: generate error if incorrect relocation */
7848 gen_assign_cast(&dtype);
7849 bt = type->t & VT_BTYPE;
7850 ptr = sec->data + c;
7851 /* XXX: make code faster ? */
7852 if (!(type->t & VT_BITFIELD)) {
7853 bit_pos = 0;
7854 bit_size = 32;
7855 bit_mask = -1LL;
7856 } else {
7857 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
7858 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
7859 bit_mask = (1LL << bit_size) - 1;
7861 if ((vtop->r & VT_SYM) &&
7862 (bt == VT_BYTE ||
7863 bt == VT_SHORT ||
7864 bt == VT_DOUBLE ||
7865 bt == VT_LDOUBLE ||
7866 bt == VT_LLONG ||
7867 (bt == VT_INT && bit_size != 32)))
7868 error("initializer element is not computable at load time");
7869 switch(bt) {
7870 case VT_BYTE:
7871 *(char *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
7872 break;
7873 case VT_SHORT:
7874 *(short *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
7875 break;
7876 case VT_DOUBLE:
7877 *(double *)ptr = vtop->c.d;
7878 break;
7879 case VT_LDOUBLE:
7880 *(long double *)ptr = vtop->c.ld;
7881 break;
7882 case VT_LLONG:
7883 *(long long *)ptr |= (vtop->c.ll & bit_mask) << bit_pos;
7884 break;
7885 default:
7886 if (vtop->r & VT_SYM) {
7887 greloc(sec, vtop->sym, c, R_DATA_32);
7889 *(int *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
7890 break;
7892 vtop--;
7893 } else {
7894 vset(&dtype, VT_LOCAL, c);
7895 vswap();
7896 vstore();
7897 vpop();
7901 /* put zeros for variable based init */
7902 static void init_putz(CType *t, Section *sec, unsigned long c, int size)
7904 if (sec) {
7905 /* nothing to do because globals are already set to zero */
7906 } else {
7907 vpush_global_sym(&func_old_type, TOK_memset);
7908 vseti(VT_LOCAL, c);
7909 vpushi(0);
7910 vpushi(size);
7911 gfunc_call(3);
7915 /* 't' contains the type and storage info. 'c' is the offset of the
7916 object in section 'sec'. If 'sec' is NULL, it means stack based
7917 allocation. 'first' is true if array '{' must be read (multi
7918 dimension implicit array init handling). 'size_only' is true if
7919 size only evaluation is wanted (only for arrays). */
7920 static void decl_initializer(CType *type, Section *sec, unsigned long c,
7921 int first, int size_only)
7923 int index, array_length, n, no_oblock, nb, parlevel, i;
7924 int size1, align1, expr_type;
7925 Sym *s, *f;
7926 CType *t1;
7928 if (type->t & VT_ARRAY) {
7929 s = type->ref;
7930 n = s->c;
7931 array_length = 0;
7932 t1 = pointed_type(type);
7933 size1 = type_size(t1, &align1);
7935 no_oblock = 1;
7936 if ((first && tok != TOK_LSTR && tok != TOK_STR) ||
7937 tok == '{') {
7938 skip('{');
7939 no_oblock = 0;
7942 /* only parse strings here if correct type (otherwise: handle
7943 them as ((w)char *) expressions */
7944 if ((tok == TOK_LSTR &&
7945 (t1->t & VT_BTYPE) == VT_INT) ||
7946 (tok == TOK_STR &&
7947 (t1->t & VT_BTYPE) == VT_BYTE)) {
7948 while (tok == TOK_STR || tok == TOK_LSTR) {
7949 int cstr_len, ch;
7950 CString *cstr;
7952 cstr = tokc.cstr;
7953 /* compute maximum number of chars wanted */
7954 if (tok == TOK_STR)
7955 cstr_len = cstr->size;
7956 else
7957 cstr_len = cstr->size / sizeof(int);
7958 cstr_len--;
7959 nb = cstr_len;
7960 if (n >= 0 && nb > (n - array_length))
7961 nb = n - array_length;
7962 if (!size_only) {
7963 if (cstr_len > nb)
7964 warning("initializer-string for array is too long");
7965 /* in order to go faster for common case (char
7966 string in global variable, we handle it
7967 specifically */
7968 if (sec && tok == TOK_STR && size1 == 1) {
7969 memcpy(sec->data + c + array_length, cstr->data, nb);
7970 } else {
7971 for(i=0;i<nb;i++) {
7972 if (tok == TOK_STR)
7973 ch = ((unsigned char *)cstr->data)[i];
7974 else
7975 ch = ((int *)cstr->data)[i];
7976 init_putv(t1, sec, c + (array_length + i) * size1,
7977 ch, EXPR_VAL);
7981 array_length += nb;
7982 next();
7984 /* only add trailing zero if enough storage (no
7985 warning in this case since it is standard) */
7986 if (n < 0 || array_length < n) {
7987 if (!size_only) {
7988 init_putv(t1, sec, c + (array_length * size1), 0, EXPR_VAL);
7990 array_length++;
7992 } else {
7993 index = 0;
7994 while (tok != '}') {
7995 decl_designator(type, sec, c, &index, NULL, size_only);
7996 if (n >= 0 && index >= n)
7997 error("index too large");
7998 /* must put zero in holes (note that doing it that way
7999 ensures that it even works with designators) */
8000 if (!size_only && array_length < index) {
8001 init_putz(t1, sec, c + array_length * size1,
8002 (index - array_length) * size1);
8004 index++;
8005 if (index > array_length)
8006 array_length = index;
8007 /* special test for multi dimensional arrays (may not
8008 be strictly correct if designators are used at the
8009 same time) */
8010 if (index >= n && no_oblock)
8011 break;
8012 if (tok == '}')
8013 break;
8014 skip(',');
8017 if (!no_oblock)
8018 skip('}');
8019 /* put zeros at the end */
8020 if (!size_only && n >= 0 && array_length < n) {
8021 init_putz(t1, sec, c + array_length * size1,
8022 (n - array_length) * size1);
8024 /* patch type size if needed */
8025 if (n < 0)
8026 s->c = array_length;
8027 } else if ((type->t & VT_BTYPE) == VT_STRUCT &&
8028 (sec || !first || tok == '{')) {
8029 int par_count;
8031 /* NOTE: the previous test is a specific case for automatic
8032 struct/union init */
8033 /* XXX: union needs only one init */
8035 /* XXX: this test is incorrect for local initializers
8036 beginning with ( without {. It would be much more difficult
8037 to do it correctly (ideally, the expression parser should
8038 be used in all cases) */
8039 par_count = 0;
8040 if (tok == '(') {
8041 AttributeDef ad1;
8042 CType type1;
8043 next();
8044 while (tok == '(') {
8045 par_count++;
8046 next();
8048 if (!parse_btype(&type1, &ad1))
8049 expect("cast");
8050 type_decl(&type1, &ad1, &n, TYPE_ABSTRACT);
8051 #if 0
8052 if (!is_assignable_types(type, &type1))
8053 error("invalid type for cast");
8054 #endif
8055 skip(')');
8057 no_oblock = 1;
8058 if (first || tok == '{') {
8059 skip('{');
8060 no_oblock = 0;
8062 s = type->ref;
8063 f = s->next;
8064 array_length = 0;
8065 index = 0;
8066 n = s->c;
8067 while (tok != '}') {
8068 decl_designator(type, sec, c, NULL, &f, size_only);
8069 index = f->c;
8070 if (!size_only && array_length < index) {
8071 init_putz(type, sec, c + array_length,
8072 index - array_length);
8074 index = index + type_size(&f->type, &align1);
8075 if (index > array_length)
8076 array_length = index;
8077 f = f->next;
8078 if (no_oblock && f == NULL)
8079 break;
8080 if (tok == '}')
8081 break;
8082 skip(',');
8084 /* put zeros at the end */
8085 if (!size_only && array_length < n) {
8086 init_putz(type, sec, c + array_length,
8087 n - array_length);
8089 if (!no_oblock)
8090 skip('}');
8091 while (par_count) {
8092 skip(')');
8093 par_count--;
8095 } else if (tok == '{') {
8096 next();
8097 decl_initializer(type, sec, c, first, size_only);
8098 skip('}');
8099 } else if (size_only) {
8100 /* just skip expression */
8101 parlevel = 0;
8102 while ((parlevel > 0 || (tok != '}' && tok != ',')) &&
8103 tok != -1) {
8104 if (tok == '(')
8105 parlevel++;
8106 else if (tok == ')')
8107 parlevel--;
8108 next();
8110 } else {
8111 /* currently, we always use constant expression for globals
8112 (may change for scripting case) */
8113 expr_type = EXPR_CONST;
8114 if (!sec)
8115 expr_type = EXPR_ANY;
8116 init_putv(type, sec, c, 0, expr_type);
8120 /* parse an initializer for type 't' if 'has_init' is non zero, and
8121 allocate space in local or global data space ('r' is either
8122 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
8123 variable 'v' of scope 'scope' is declared before initializers are
8124 parsed. If 'v' is zero, then a reference to the new object is put
8125 in the value stack. If 'has_init' is 2, a special parsing is done
8126 to handle string constants. */
8127 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
8128 int has_init, int v, int scope)
8130 int size, align, addr, data_offset;
8131 int level;
8132 ParseState saved_parse_state;
8133 TokenString init_str;
8134 Section *sec;
8136 size = type_size(type, &align);
8137 /* If unknown size, we must evaluate it before
8138 evaluating initializers because
8139 initializers can generate global data too
8140 (e.g. string pointers or ISOC99 compound
8141 literals). It also simplifies local
8142 initializers handling */
8143 tok_str_new(&init_str);
8144 if (size < 0) {
8145 if (!has_init)
8146 error("unknown type size");
8147 /* get all init string */
8148 if (has_init == 2) {
8149 /* only get strings */
8150 while (tok == TOK_STR || tok == TOK_LSTR) {
8151 tok_str_add_tok(&init_str);
8152 next();
8154 } else {
8155 level = 0;
8156 while (level > 0 || (tok != ',' && tok != ';')) {
8157 if (tok < 0)
8158 error("unexpected end of file in initializer");
8159 tok_str_add_tok(&init_str);
8160 if (tok == '{')
8161 level++;
8162 else if (tok == '}') {
8163 if (level == 0)
8164 break;
8165 level--;
8167 next();
8170 tok_str_add(&init_str, -1);
8171 tok_str_add(&init_str, 0);
8173 /* compute size */
8174 save_parse_state(&saved_parse_state);
8176 macro_ptr = init_str.str;
8177 next();
8178 decl_initializer(type, NULL, 0, 1, 1);
8179 /* prepare second initializer parsing */
8180 macro_ptr = init_str.str;
8181 next();
8183 /* if still unknown size, error */
8184 size = type_size(type, &align);
8185 if (size < 0)
8186 error("unknown type size");
8188 /* take into account specified alignment if bigger */
8189 if (ad->aligned > align)
8190 align = ad->aligned;
8191 if ((r & VT_VALMASK) == VT_LOCAL) {
8192 sec = NULL;
8193 if (do_bounds_check && (type->t & VT_ARRAY))
8194 loc--;
8195 loc = (loc - size) & -align;
8196 addr = loc;
8197 /* handles bounds */
8198 /* XXX: currently, since we do only one pass, we cannot track
8199 '&' operators, so we add only arrays */
8200 if (do_bounds_check && (type->t & VT_ARRAY)) {
8201 unsigned long *bounds_ptr;
8202 /* add padding between regions */
8203 loc--;
8204 /* then add local bound info */
8205 bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(unsigned long));
8206 bounds_ptr[0] = addr;
8207 bounds_ptr[1] = size;
8209 if (v) {
8210 /* local variable */
8211 sym_push(v, type, r, addr);
8212 } else {
8213 /* push local reference */
8214 vset(type, r, addr);
8216 } else {
8217 Sym *sym;
8219 sym = NULL;
8220 if (v && scope == VT_CONST) {
8221 /* see if the symbol was already defined */
8222 sym = sym_find(v);
8223 if (sym) {
8224 if (!is_compatible_types(&sym->type, type))
8225 error("incompatible types for redefinition of '%s'",
8226 get_tok_str(v, NULL));
8227 if (sym->type.t & VT_EXTERN) {
8228 /* if the variable is extern, it was not allocated */
8229 sym->type.t &= ~VT_EXTERN;
8230 } else {
8231 /* we accept several definitions of the same
8232 global variable. this is tricky, because we
8233 must play with the SHN_COMMON type of the symbol */
8234 /* XXX: should check if the variable was already
8235 initialized. It is incorrect to initialized it
8236 twice */
8237 /* no init data, we won't add more to the symbol */
8238 if (!has_init)
8239 goto no_alloc;
8244 /* allocate symbol in corresponding section */
8245 sec = ad->section;
8246 if (!sec) {
8247 if (has_init)
8248 sec = data_section;
8250 if (sec) {
8251 data_offset = sec->data_offset;
8252 data_offset = (data_offset + align - 1) & -align;
8253 addr = data_offset;
8254 /* very important to increment global pointer at this time
8255 because initializers themselves can create new initializers */
8256 data_offset += size;
8257 /* add padding if bound check */
8258 if (do_bounds_check)
8259 data_offset++;
8260 sec->data_offset = data_offset;
8261 /* allocate section space to put the data */
8262 if (sec->sh_type != SHT_NOBITS &&
8263 data_offset > sec->data_allocated)
8264 section_realloc(sec, data_offset);
8265 } else {
8266 addr = 0; /* avoid warning */
8269 if (v) {
8270 if (scope == VT_CONST) {
8271 if (!sym)
8272 goto do_def;
8273 } else {
8274 do_def:
8275 sym = sym_push(v, type, r | VT_SYM, 0);
8277 /* update symbol definition */
8278 if (sec) {
8279 put_extern_sym(sym, sec, addr, size);
8280 } else {
8281 Elf32_Sym *esym;
8282 /* put a common area */
8283 put_extern_sym(sym, NULL, align, size);
8284 /* XXX: find a nicer way */
8285 esym = &((Elf32_Sym *)symtab_section->data)[sym->c];
8286 esym->st_shndx = SHN_COMMON;
8288 } else {
8289 CValue cval;
8291 /* push global reference */
8292 sym = get_sym_ref(type, sec, addr, size);
8293 cval.ul = 0;
8294 vsetc(type, VT_CONST | VT_SYM, &cval);
8295 vtop->sym = sym;
8298 /* handles bounds now because the symbol must be defined
8299 before for the relocation */
8300 if (do_bounds_check) {
8301 unsigned long *bounds_ptr;
8303 greloc(bounds_section, sym, bounds_section->data_offset, R_DATA_32);
8304 /* then add global bound info */
8305 bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(long));
8306 bounds_ptr[0] = 0; /* relocated */
8307 bounds_ptr[1] = size;
8310 if (has_init) {
8311 decl_initializer(type, sec, addr, 1, 0);
8312 /* restore parse state if needed */
8313 if (init_str.str) {
8314 tok_str_free(init_str.str);
8315 restore_parse_state(&saved_parse_state);
8318 no_alloc: ;
8321 void put_func_debug(Sym *sym)
8323 char buf[512];
8325 /* stabs info */
8326 /* XXX: we put here a dummy type */
8327 snprintf(buf, sizeof(buf), "%s:%c1",
8328 funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
8329 put_stabs_r(buf, N_FUN, 0, file->line_num, 0,
8330 cur_text_section, sym->c);
8331 last_ind = 0;
8332 last_line_num = 0;
8335 /* not finished : try to put some local vars in registers */
8336 //#define CONFIG_REG_VARS
8338 #ifdef CONFIG_REG_VARS
8339 void add_var_ref(int t)
8341 printf("%s:%d: &%s\n",
8342 file->filename, file->line_num,
8343 get_tok_str(t, NULL));
8346 /* first pass on a function with heuristic to extract variable usage
8347 and pointer references to local variables for register allocation */
8348 void analyse_function(void)
8350 int level, t;
8352 for(;;) {
8353 if (tok == -1)
8354 break;
8355 /* any symbol coming after '&' is considered as being a
8356 variable whose reference is taken. It is highly unaccurate
8357 but it is difficult to do better without a complete parse */
8358 if (tok == '&') {
8359 next();
8360 /* if '& number', then no need to examine next tokens */
8361 if (tok == TOK_CINT ||
8362 tok == TOK_CUINT ||
8363 tok == TOK_CLLONG ||
8364 tok == TOK_CULLONG) {
8365 continue;
8366 } else if (tok >= TOK_UIDENT) {
8367 /* if '& ident [' or '& ident ->', then ident address
8368 is not needed */
8369 t = tok;
8370 next();
8371 if (tok != '[' && tok != TOK_ARROW)
8372 add_var_ref(t);
8373 } else {
8374 level = 0;
8375 while (tok != '}' && tok != ';' &&
8376 !((tok == ',' || tok == ')') && level == 0)) {
8377 if (tok >= TOK_UIDENT) {
8378 add_var_ref(tok);
8379 } else if (tok == '(') {
8380 level++;
8381 } else if (tok == ')') {
8382 level--;
8384 next();
8387 } else {
8388 next();
8392 #endif
8394 /* parse an old style function declaration list */
8395 /* XXX: check multiple parameter */
8396 static void func_decl_list(Sym *func_sym)
8398 AttributeDef ad;
8399 int v;
8400 Sym *s;
8401 CType btype, type;
8403 /* parse each declaration */
8404 while (tok != '{' && tok != ';' && tok != ',' && tok != TOK_EOF) {
8405 if (!parse_btype(&btype, &ad))
8406 expect("declaration list");
8407 if (((btype.t & VT_BTYPE) == VT_ENUM ||
8408 (btype.t & VT_BTYPE) == VT_STRUCT) &&
8409 tok == ';') {
8410 /* we accept no variable after */
8411 } else {
8412 for(;;) {
8413 type = btype;
8414 type_decl(&type, &ad, &v, TYPE_DIRECT);
8415 /* find parameter in function parameter list */
8416 s = func_sym->next;
8417 while (s != NULL) {
8418 if ((s->v & ~SYM_FIELD) == v)
8419 goto found;
8420 s = s->next;
8422 error("declaration for parameter '%s' but no such parameter",
8423 get_tok_str(v, NULL));
8424 found:
8425 /* check that no storage specifier except 'register' was given */
8426 if (type.t & VT_STORAGE)
8427 error("storage class specified for '%s'", get_tok_str(v, NULL));
8428 convert_parameter_type(&type);
8429 /* we can add the type (NOTE: it could be local to the function) */
8430 s->type = type;
8431 /* accept other parameters */
8432 if (tok == ',')
8433 next();
8434 else
8435 break;
8438 skip(';');
8442 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
8443 static void decl(int l)
8445 int v, has_init, r;
8446 CType type, btype;
8447 Sym *sym;
8448 AttributeDef ad;
8450 while (1) {
8451 if (!parse_btype(&btype, &ad)) {
8452 /* skip redundant ';' */
8453 /* XXX: find more elegant solution */
8454 if (tok == ';') {
8455 next();
8456 continue;
8458 /* special test for old K&R protos without explicit int
8459 type. Only accepted when defining global data */
8460 if (l == VT_LOCAL || tok < TOK_DEFINE)
8461 break;
8462 btype.t = VT_INT;
8464 if (((btype.t & VT_BTYPE) == VT_ENUM ||
8465 (btype.t & VT_BTYPE) == VT_STRUCT) &&
8466 tok == ';') {
8467 /* we accept no variable after */
8468 next();
8469 continue;
8471 while (1) { /* iterate thru each declaration */
8472 type = btype;
8473 type_decl(&type, &ad, &v, TYPE_DIRECT);
8474 #if 0
8476 char buf[500];
8477 type_to_str(buf, sizeof(buf), t, get_tok_str(v, NULL));
8478 printf("type = '%s'\n", buf);
8480 #endif
8481 if ((type.t & VT_BTYPE) == VT_FUNC) {
8482 /* if old style function prototype, we accept a
8483 declaration list */
8484 sym = type.ref;
8485 if (sym->c == FUNC_OLD)
8486 func_decl_list(sym);
8489 if (tok == '{') {
8490 #ifdef CONFIG_REG_VARS
8491 TokenString func_str;
8492 ParseState saved_parse_state;
8493 int block_level;
8494 #endif
8496 if (l == VT_LOCAL)
8497 error("cannot use local functions");
8498 if (!(type.t & VT_FUNC))
8499 expect("function definition");
8500 /* XXX: cannot do better now: convert extern line to static inline */
8501 if ((type.t & (VT_EXTERN | VT_INLINE)) == (VT_EXTERN | VT_INLINE))
8502 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
8504 #ifdef CONFIG_REG_VARS
8505 /* parse all function code and record it */
8507 tok_str_new(&func_str);
8509 block_level = 0;
8510 for(;;) {
8511 int t;
8512 if (tok == -1)
8513 error("unexpected end of file");
8514 tok_str_add_tok(&func_str);
8515 t = tok;
8516 next();
8517 if (t == '{') {
8518 block_level++;
8519 } else if (t == '}') {
8520 block_level--;
8521 if (block_level == 0)
8522 break;
8525 tok_str_add(&func_str, -1);
8526 tok_str_add(&func_str, 0);
8528 save_parse_state(&saved_parse_state);
8530 macro_ptr = func_str.str;
8531 next();
8532 analyse_function();
8533 #endif
8535 /* compute text section */
8536 cur_text_section = ad.section;
8537 if (!cur_text_section)
8538 cur_text_section = text_section;
8539 ind = cur_text_section->data_offset;
8540 funcname = get_tok_str(v, NULL);
8541 sym = sym_find(v);
8542 if (sym) {
8543 /* if symbol is already defined, then put complete type */
8544 sym->type = type;
8545 } else {
8546 /* put function symbol */
8547 sym = global_identifier_push(v, type.t, 0);
8548 sym->type.ref = type.ref;
8550 /* NOTE: we patch the symbol size later */
8551 put_extern_sym(sym, cur_text_section, ind, 0);
8552 func_ind = ind;
8553 sym->r = VT_SYM | VT_CONST;
8554 /* put debug symbol */
8555 if (do_debug)
8556 put_func_debug(sym);
8557 /* push a dummy symbol to enable local sym storage */
8558 sym_push2(&local_stack, SYM_FIELD, 0, 0);
8559 gfunc_prolog(&type);
8560 loc = 0;
8561 rsym = 0;
8562 #ifdef CONFIG_REG_VARS
8563 macro_ptr = func_str.str;
8564 next();
8565 #endif
8566 block(NULL, NULL, NULL, NULL, 0, 0);
8567 gsym(rsym);
8568 gfunc_epilog();
8569 cur_text_section->data_offset = ind;
8570 label_pop(&global_label_stack, NULL);
8571 sym_pop(&local_stack, NULL); /* reset local stack */
8572 /* end of function */
8573 /* patch symbol size */
8574 ((Elf32_Sym *)symtab_section->data)[sym->c].st_size =
8575 ind - func_ind;
8576 if (do_debug) {
8577 put_stabn(N_FUN, 0, 0, ind - func_ind);
8579 funcname = ""; /* for safety */
8580 func_vt.t = VT_VOID; /* for safety */
8581 ind = 0; /* for safety */
8583 #ifdef CONFIG_REG_VARS
8584 tok_str_free(func_str.str);
8585 restore_parse_state(&saved_parse_state);
8586 #endif
8587 break;
8588 } else {
8589 if (btype.t & VT_TYPEDEF) {
8590 /* save typedefed type */
8591 /* XXX: test storage specifiers ? */
8592 sym = sym_push(v, &type, 0, 0);
8593 sym->type.t |= VT_TYPEDEF;
8594 } else if ((type.t & VT_BTYPE) == VT_FUNC) {
8595 /* external function definition */
8596 external_sym(v, &type, 0);
8597 } else {
8598 /* not lvalue if array */
8599 r = 0;
8600 if (!(type.t & VT_ARRAY))
8601 r |= lvalue_type(type.t);
8602 has_init = (tok == '=');
8603 if ((btype.t & VT_EXTERN) ||
8604 ((type.t & VT_ARRAY) && (type.t & VT_STATIC) &&
8605 !has_init && l == VT_CONST && type.ref->c < 0)) {
8606 /* external variable */
8607 /* NOTE: as GCC, uninitialized global static
8608 arrays of null size are considered as
8609 extern */
8610 external_sym(v, &type, r);
8611 } else {
8612 if (type.t & VT_STATIC)
8613 r |= VT_CONST;
8614 else
8615 r |= l;
8616 if (has_init)
8617 next();
8618 decl_initializer_alloc(&type, &ad, r,
8619 has_init, v, l);
8622 if (tok != ',') {
8623 skip(';');
8624 break;
8626 next();
8632 /* better than nothing, but needs extension to handle '-E' option
8633 correctly too */
8634 static void preprocess_init(TCCState *s1)
8636 s1->include_stack_ptr = s1->include_stack;
8637 /* XXX: move that before to avoid having to initialize
8638 file->ifdef_stack_ptr ? */
8639 s1->ifdef_stack_ptr = s1->ifdef_stack;
8640 file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
8642 /* XXX: not ANSI compliant: bound checking says error */
8643 vtop = vstack - 1;
8646 /* compile the C file opened in 'file'. Return non zero if errors. */
8647 static int tcc_compile(TCCState *s1)
8649 Sym *define_start;
8650 char buf[512];
8651 volatile int section_sym;
8653 #ifdef INC_DEBUG
8654 printf("%s: **** new file\n", file->filename);
8655 #endif
8656 preprocess_init(s1);
8658 funcname = "";
8659 anon_sym = SYM_FIRST_ANOM;
8661 /* file info: full path + filename */
8662 section_sym = 0; /* avoid warning */
8663 if (do_debug) {
8664 section_sym = put_elf_sym(symtab_section, 0, 0,
8665 ELF32_ST_INFO(STB_LOCAL, STT_SECTION), 0,
8666 text_section->sh_num, NULL);
8667 getcwd(buf, sizeof(buf));
8668 pstrcat(buf, sizeof(buf), "/");
8669 put_stabs_r(buf, N_SO, 0, 0,
8670 text_section->data_offset, text_section, section_sym);
8671 put_stabs_r(file->filename, N_SO, 0, 0,
8672 text_section->data_offset, text_section, section_sym);
8674 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
8675 symbols can be safely used */
8676 put_elf_sym(symtab_section, 0, 0,
8677 ELF32_ST_INFO(STB_LOCAL, STT_FILE), 0,
8678 SHN_ABS, file->filename);
8680 /* define some often used types */
8681 int_type.t = VT_INT;
8683 char_pointer_type.t = VT_BYTE;
8684 mk_pointer(&char_pointer_type);
8686 func_old_type.t = VT_FUNC;
8687 func_old_type.ref = sym_push(SYM_FIELD, &int_type, FUNC_CDECL, FUNC_OLD);
8689 #if 0
8690 /* define 'void *alloca(unsigned int)' builtin function */
8692 Sym *s1;
8694 p = anon_sym++;
8695 sym = sym_push(p, mk_pointer(VT_VOID), FUNC_CDECL, FUNC_NEW);
8696 s1 = sym_push(SYM_FIELD, VT_UNSIGNED | VT_INT, 0, 0);
8697 s1->next = NULL;
8698 sym->next = s1;
8699 sym_push(TOK_alloca, VT_FUNC | (p << VT_STRUCT_SHIFT), VT_CONST, 0);
8701 #endif
8703 define_start = define_stack;
8705 if (setjmp(s1->error_jmp_buf) == 0) {
8706 s1->nb_errors = 0;
8707 s1->error_set_jmp_enabled = 1;
8709 ch = file->buf_ptr[0];
8710 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
8711 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM;
8712 next();
8713 decl(VT_CONST);
8714 if (tok != TOK_EOF)
8715 expect("declaration");
8717 /* end of translation unit info */
8718 if (do_debug) {
8719 put_stabs_r(NULL, N_SO, 0, 0,
8720 text_section->data_offset, text_section, section_sym);
8723 s1->error_set_jmp_enabled = 0;
8725 /* reset define stack, but leave -Dsymbols (may be incorrect if
8726 they are undefined) */
8727 free_defines(define_start);
8729 sym_pop(&global_stack, NULL);
8731 return s1->nb_errors != 0 ? -1 : 0;
8734 #ifdef LIBTCC
8735 int tcc_compile_string(TCCState *s, const char *str)
8737 BufferedFile bf1, *bf = &bf1;
8738 int ret, len;
8739 char *buf;
8741 /* init file structure */
8742 bf->fd = -1;
8743 /* XXX: avoid copying */
8744 len = strlen(str);
8745 buf = tcc_malloc(len + 1);
8746 if (!buf)
8747 return -1;
8748 memcpy(buf, str, len);
8749 buf[len] = CH_EOB;
8750 bf->buf_ptr = buf;
8751 bf->buf_end = buf + len;
8752 pstrcpy(bf->filename, sizeof(bf->filename), "<string>");
8753 bf->line_num = 1;
8754 file = bf;
8756 ret = tcc_compile(s);
8758 tcc_free(buf);
8760 /* currently, no need to close */
8761 return ret;
8763 #endif
8765 /* define a preprocessor symbol. A value can also be provided with the '=' operator */
8766 void tcc_define_symbol(TCCState *s1, const char *sym, const char *value)
8768 BufferedFile bf1, *bf = &bf1;
8770 pstrcpy(bf->buffer, IO_BUF_SIZE, sym);
8771 pstrcat(bf->buffer, IO_BUF_SIZE, " ");
8772 /* default value */
8773 if (!value)
8774 value = "1";
8775 pstrcat(bf->buffer, IO_BUF_SIZE, value);
8777 /* init file structure */
8778 bf->fd = -1;
8779 bf->buf_ptr = bf->buffer;
8780 bf->buf_end = bf->buffer + strlen(bf->buffer);
8781 *bf->buf_end = CH_EOB;
8782 bf->filename[0] = '\0';
8783 bf->line_num = 1;
8784 file = bf;
8786 s1->include_stack_ptr = s1->include_stack;
8788 /* parse with define parser */
8789 ch = file->buf_ptr[0];
8790 next_nomacro();
8791 parse_define();
8792 file = NULL;
8795 /* undefine a preprocessor symbol */
8796 void tcc_undefine_symbol(TCCState *s1, const char *sym)
8798 TokenSym *ts;
8799 Sym *s;
8800 ts = tok_alloc(sym, strlen(sym));
8801 s = define_find(ts->tok);
8802 /* undefine symbol by putting an invalid name */
8803 if (s)
8804 define_undef(s);
8807 #ifdef CONFIG_TCC_ASM
8809 #include "i386-asm.c"
8810 #include "tccasm.c"
8812 #else
8813 static void asm_instr(void)
8815 error("inline asm() not supported");
8817 #endif
8819 #include "tccelf.c"
8821 /* print the position in the source file of PC value 'pc' by reading
8822 the stabs debug information */
8823 static void rt_printline(unsigned long wanted_pc)
8825 Stab_Sym *sym, *sym_end;
8826 char func_name[128], last_func_name[128];
8827 unsigned long func_addr, last_pc, pc;
8828 const char *incl_files[INCLUDE_STACK_SIZE];
8829 int incl_index, len, last_line_num, i;
8830 const char *str, *p;
8832 fprintf(stderr, "0x%08lx:", wanted_pc);
8834 func_name[0] = '\0';
8835 func_addr = 0;
8836 incl_index = 0;
8837 last_func_name[0] = '\0';
8838 last_pc = 0xffffffff;
8839 last_line_num = 1;
8840 sym = (Stab_Sym *)stab_section->data + 1;
8841 sym_end = (Stab_Sym *)(stab_section->data + stab_section->data_offset);
8842 while (sym < sym_end) {
8843 switch(sym->n_type) {
8844 /* function start or end */
8845 case N_FUN:
8846 if (sym->n_strx == 0) {
8847 /* we test if between last line and end of function */
8848 pc = sym->n_value + func_addr;
8849 if (wanted_pc >= last_pc && wanted_pc < pc)
8850 goto found;
8851 func_name[0] = '\0';
8852 func_addr = 0;
8853 } else {
8854 str = stabstr_section->data + sym->n_strx;
8855 p = strchr(str, ':');
8856 if (!p) {
8857 pstrcpy(func_name, sizeof(func_name), str);
8858 } else {
8859 len = p - str;
8860 if (len > sizeof(func_name) - 1)
8861 len = sizeof(func_name) - 1;
8862 memcpy(func_name, str, len);
8863 func_name[len] = '\0';
8865 func_addr = sym->n_value;
8867 break;
8868 /* line number info */
8869 case N_SLINE:
8870 pc = sym->n_value + func_addr;
8871 if (wanted_pc >= last_pc && wanted_pc < pc)
8872 goto found;
8873 last_pc = pc;
8874 last_line_num = sym->n_desc;
8875 /* XXX: slow! */
8876 strcpy(last_func_name, func_name);
8877 break;
8878 /* include files */
8879 case N_BINCL:
8880 str = stabstr_section->data + sym->n_strx;
8881 add_incl:
8882 if (incl_index < INCLUDE_STACK_SIZE) {
8883 incl_files[incl_index++] = str;
8885 break;
8886 case N_EINCL:
8887 if (incl_index > 1)
8888 incl_index--;
8889 break;
8890 case N_SO:
8891 if (sym->n_strx == 0) {
8892 incl_index = 0; /* end of translation unit */
8893 } else {
8894 str = stabstr_section->data + sym->n_strx;
8895 /* do not add path */
8896 len = strlen(str);
8897 if (len > 0 && str[len - 1] != '/')
8898 goto add_incl;
8900 break;
8902 sym++;
8905 /* second pass: we try symtab symbols (no line number info) */
8906 incl_index = 0;
8908 Elf32_Sym *sym, *sym_end;
8909 int type;
8911 sym_end = (Elf32_Sym *)(symtab_section->data + symtab_section->data_offset);
8912 for(sym = (Elf32_Sym *)symtab_section->data + 1;
8913 sym < sym_end;
8914 sym++) {
8915 type = ELF32_ST_TYPE(sym->st_info);
8916 if (type == STT_FUNC) {
8917 if (wanted_pc >= sym->st_value &&
8918 wanted_pc < sym->st_value + sym->st_size) {
8919 pstrcpy(last_func_name, sizeof(last_func_name),
8920 strtab_section->data + sym->st_name);
8921 goto found;
8926 /* did not find any info: */
8927 fprintf(stderr, " ???\n");
8928 return;
8929 found:
8930 if (last_func_name[0] != '\0') {
8931 fprintf(stderr, " %s()", last_func_name);
8933 if (incl_index > 0) {
8934 fprintf(stderr, " (%s:%d",
8935 incl_files[incl_index - 1], last_line_num);
8936 for(i = incl_index - 2; i >= 0; i--)
8937 fprintf(stderr, ", included from %s", incl_files[i]);
8938 fprintf(stderr, ")");
8940 fprintf(stderr, "\n");
8943 #ifndef WIN32
8945 #ifdef __i386__
8947 /* fix for glibc 2.1 */
8948 #ifndef REG_EIP
8949 #define REG_EIP EIP
8950 #define REG_EBP EBP
8951 #endif
8953 /* return the PC at frame level 'level'. Return non zero if not found */
8954 static int rt_get_caller_pc(unsigned long *paddr,
8955 ucontext_t *uc, int level)
8957 unsigned long fp;
8958 int i;
8960 if (level == 0) {
8961 #ifdef __FreeBSD__
8962 *paddr = uc->uc_mcontext.mc_eip;
8963 #else
8964 *paddr = uc->uc_mcontext.gregs[REG_EIP];
8965 #endif
8966 return 0;
8967 } else {
8968 #ifdef __FreeBSD__
8969 fp = uc->uc_mcontext.mc_ebp;
8970 #else
8971 fp = uc->uc_mcontext.gregs[REG_EBP];
8972 #endif
8973 for(i=1;i<level;i++) {
8974 /* XXX: check address validity with program info */
8975 if (fp <= 0x1000 || fp >= 0xc0000000)
8976 return -1;
8977 fp = ((unsigned long *)fp)[0];
8979 *paddr = ((unsigned long *)fp)[1];
8980 return 0;
8983 #else
8984 #error add arch specific rt_get_caller_pc()
8985 #endif
8987 /* emit a run time error at position 'pc' */
8988 void rt_error(ucontext_t *uc, const char *fmt, ...)
8990 va_list ap;
8991 unsigned long pc;
8992 int i;
8994 va_start(ap, fmt);
8995 fprintf(stderr, "Runtime error: ");
8996 vfprintf(stderr, fmt, ap);
8997 fprintf(stderr, "\n");
8998 for(i=0;i<num_callers;i++) {
8999 if (rt_get_caller_pc(&pc, uc, i) < 0)
9000 break;
9001 if (i == 0)
9002 fprintf(stderr, "at ");
9003 else
9004 fprintf(stderr, "by ");
9005 rt_printline(pc);
9007 exit(255);
9008 va_end(ap);
9011 /* signal handler for fatal errors */
9012 static void sig_error(int signum, siginfo_t *siginf, void *puc)
9014 ucontext_t *uc = puc;
9016 switch(signum) {
9017 case SIGFPE:
9018 switch(siginf->si_code) {
9019 case FPE_INTDIV:
9020 case FPE_FLTDIV:
9021 rt_error(uc, "division by zero");
9022 break;
9023 default:
9024 rt_error(uc, "floating point exception");
9025 break;
9027 break;
9028 case SIGBUS:
9029 case SIGSEGV:
9030 if (rt_bound_error_msg && *rt_bound_error_msg)
9031 rt_error(uc, *rt_bound_error_msg);
9032 else
9033 rt_error(uc, "dereferencing invalid pointer");
9034 break;
9035 case SIGILL:
9036 rt_error(uc, "illegal instruction");
9037 break;
9038 case SIGABRT:
9039 rt_error(uc, "abort() called");
9040 break;
9041 default:
9042 rt_error(uc, "caught signal %d", signum);
9043 break;
9045 exit(255);
9047 #endif
9049 /* do all relocations (needed before using tcc_get_symbol()) */
9050 int tcc_relocate(TCCState *s1)
9052 Section *s;
9053 int i;
9055 s1->nb_errors = 0;
9057 tcc_add_runtime(s1);
9059 relocate_common_syms();
9061 /* compute relocation address : section are relocated in place. We
9062 also alloc the bss space */
9063 for(i = 1; i < s1->nb_sections; i++) {
9064 s = s1->sections[i];
9065 if (s->sh_flags & SHF_ALLOC) {
9066 if (s->sh_type == SHT_NOBITS)
9067 s->data = tcc_mallocz(s->data_offset);
9068 s->sh_addr = (unsigned long)s->data;
9072 relocate_syms(s1, 1);
9074 if (s1->nb_errors != 0)
9075 return -1;
9077 /* relocate each section */
9078 for(i = 1; i < s1->nb_sections; i++) {
9079 s = s1->sections[i];
9080 if (s->reloc)
9081 relocate_section(s1, s);
9083 return 0;
9086 /* launch the compiled program with the given arguments */
9087 int tcc_run(TCCState *s1, int argc, char **argv)
9089 int (*prog_main)(int, char **);
9091 if (tcc_relocate(s1) < 0)
9092 return -1;
9094 prog_main = tcc_get_symbol(s1, "main");
9096 if (do_debug) {
9097 #ifdef WIN32
9098 error("debug mode currently not available for Windows");
9099 #else
9100 struct sigaction sigact;
9101 /* install TCC signal handlers to print debug info on fatal
9102 runtime errors */
9103 sigact.sa_flags = SA_SIGINFO | SA_RESETHAND;
9104 sigact.sa_sigaction = sig_error;
9105 sigemptyset(&sigact.sa_mask);
9106 sigaction(SIGFPE, &sigact, NULL);
9107 sigaction(SIGILL, &sigact, NULL);
9108 sigaction(SIGSEGV, &sigact, NULL);
9109 sigaction(SIGBUS, &sigact, NULL);
9110 sigaction(SIGABRT, &sigact, NULL);
9111 #endif
9114 #ifdef CONFIG_TCC_BCHECK
9115 if (do_bounds_check) {
9116 void (*bound_init)(void);
9118 /* set error function */
9119 rt_bound_error_msg = (void *)tcc_get_symbol(s1, "__bound_error_msg");
9121 /* XXX: use .init section so that it also work in binary ? */
9122 bound_init = (void *)tcc_get_symbol(s1, "__bound_init");
9123 bound_init();
9125 #endif
9126 return (*prog_main)(argc, argv);
9129 TCCState *tcc_new(void)
9131 const char *p, *r;
9132 TCCState *s;
9133 TokenSym *ts;
9134 int i, c;
9136 s = tcc_mallocz(sizeof(TCCState));
9137 if (!s)
9138 return NULL;
9139 tcc_state = s;
9140 s->output_type = TCC_OUTPUT_MEMORY;
9142 /* init isid table */
9143 for(i=0;i<256;i++)
9144 isidnum_table[i] = isid(i) || isnum(i);
9146 /* add all tokens */
9147 table_ident = NULL;
9148 memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
9150 tok_ident = TOK_IDENT;
9151 p = tcc_keywords;
9152 while (*p) {
9153 r = p;
9154 for(;;) {
9155 c = *r++;
9156 if (c == '\0')
9157 break;
9159 ts = tok_alloc(p, r - p - 1);
9160 p = r;
9163 /* we add dummy defines for some special macros to speed up tests
9164 and to have working defined() */
9165 define_push(TOK___LINE__, MACRO_OBJ, NULL, NULL);
9166 define_push(TOK___FILE__, MACRO_OBJ, NULL, NULL);
9167 define_push(TOK___DATE__, MACRO_OBJ, NULL, NULL);
9168 define_push(TOK___TIME__, MACRO_OBJ, NULL, NULL);
9170 /* standard defines */
9171 tcc_define_symbol(s, "__STDC__", NULL);
9172 #if defined(TCC_TARGET_I386)
9173 tcc_define_symbol(s, "__i386__", NULL);
9174 #endif
9175 #if defined(linux)
9176 tcc_define_symbol(s, "__linux__", NULL);
9177 tcc_define_symbol(s, "linux", NULL);
9178 #endif
9179 /* tiny C specific defines */
9180 tcc_define_symbol(s, "__TINYC__", NULL);
9182 /* tiny C & gcc defines */
9183 tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned int");
9184 tcc_define_symbol(s, "__PTRDIFF_TYPE__", "int");
9185 tcc_define_symbol(s, "__WCHAR_TYPE__", "int");
9187 /* default library paths */
9188 tcc_add_library_path(s, "/usr/local/lib");
9189 tcc_add_library_path(s, "/usr/lib");
9190 tcc_add_library_path(s, "/lib");
9192 /* no section zero */
9193 dynarray_add((void ***)&s->sections, &s->nb_sections, NULL);
9195 /* create standard sections */
9196 text_section = new_section(s, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR);
9197 data_section = new_section(s, ".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
9198 bss_section = new_section(s, ".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);
9200 /* symbols are always generated for linking stage */
9201 symtab_section = new_symtab(s, ".symtab", SHT_SYMTAB, 0,
9202 ".strtab",
9203 ".hashtab", SHF_PRIVATE);
9204 strtab_section = symtab_section->link;
9206 /* private symbol table for dynamic symbols */
9207 s->dynsymtab_section = new_symtab(s, ".dynsymtab", SHT_SYMTAB, SHF_PRIVATE,
9208 ".dynstrtab",
9209 ".dynhashtab", SHF_PRIVATE);
9210 return s;
9213 void tcc_delete(TCCState *s1)
9215 int i, n;
9217 /* free -D defines */
9218 free_defines(NULL);
9220 /* free tokens */
9221 n = tok_ident - TOK_IDENT;
9222 for(i = 0; i < n; i++)
9223 tcc_free(table_ident[i]);
9224 tcc_free(table_ident);
9226 /* free all sections */
9228 free_section(symtab_section->hash);
9230 free_section(s1->dynsymtab_section->hash);
9231 free_section(s1->dynsymtab_section->link);
9232 free_section(s1->dynsymtab_section);
9234 for(i = 1; i < s1->nb_sections; i++)
9235 free_section(s1->sections[i]);
9236 tcc_free(s1->sections);
9238 /* free loaded dlls array */
9239 for(i = 0; i < s1->nb_loaded_dlls; i++)
9240 tcc_free(s1->loaded_dlls[i]);
9241 tcc_free(s1->loaded_dlls);
9243 /* library paths */
9244 for(i = 0; i < s1->nb_library_paths; i++)
9245 tcc_free(s1->library_paths[i]);
9246 tcc_free(s1->library_paths);
9248 /* cached includes */
9249 for(i = 0; i < s1->nb_cached_includes; i++)
9250 tcc_free(s1->cached_includes[i]);
9251 tcc_free(s1->cached_includes);
9253 for(i = 0; i < s1->nb_include_paths; i++)
9254 tcc_free(s1->include_paths[i]);
9255 tcc_free(s1->include_paths);
9257 for(i = 0; i < s1->nb_sysinclude_paths; i++)
9258 tcc_free(s1->sysinclude_paths[i]);
9259 tcc_free(s1->sysinclude_paths);
9261 tcc_free(s1);
9264 int tcc_add_include_path(TCCState *s1, const char *pathname)
9266 char *pathname1;
9268 pathname1 = tcc_strdup(pathname);
9269 dynarray_add((void ***)&s1->include_paths, &s1->nb_include_paths, pathname1);
9270 return 0;
9273 int tcc_add_sysinclude_path(TCCState *s1, const char *pathname)
9275 char *pathname1;
9277 pathname1 = tcc_strdup(pathname);
9278 dynarray_add((void ***)&s1->sysinclude_paths, &s1->nb_sysinclude_paths, pathname1);
9279 return 0;
9282 static int tcc_add_file_internal(TCCState *s1, const char *filename, int flags)
9284 const char *ext, *filename1;
9285 Elf32_Ehdr ehdr;
9286 int fd, ret;
9287 BufferedFile *saved_file;
9289 /* find source file type with extension */
9290 filename1 = strrchr(filename, '/');
9291 if (filename1)
9292 filename1++;
9293 else
9294 filename1 = filename;
9295 ext = strrchr(filename1, '.');
9296 if (ext)
9297 ext++;
9299 /* open the file */
9300 saved_file = file;
9301 file = tcc_open(s1, filename);
9302 if (!file) {
9303 if (flags & AFF_PRINT_ERROR) {
9304 error_noabort("file '%s' not found", filename);
9306 ret = -1;
9307 goto fail1;
9310 if (!ext || !strcmp(ext, "c")) {
9311 /* C file assumed */
9312 ret = tcc_compile(s1);
9313 } else
9314 #ifdef CONFIG_TCC_ASM
9315 if (!strcmp(ext, "S")) {
9316 /* preprocessed assembler */
9317 ret = tcc_assemble(s1, 1);
9318 } else if (!strcmp(ext, "s")) {
9319 /* non preprocessed assembler */
9320 ret = tcc_assemble(s1, 0);
9321 } else
9322 #endif
9324 fd = file->fd;
9325 /* assume executable format: auto guess file type */
9326 if (read(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr)) {
9327 error_noabort("could not read header");
9328 goto fail;
9330 lseek(fd, 0, SEEK_SET);
9332 if (ehdr.e_ident[0] == ELFMAG0 &&
9333 ehdr.e_ident[1] == ELFMAG1 &&
9334 ehdr.e_ident[2] == ELFMAG2 &&
9335 ehdr.e_ident[3] == ELFMAG3) {
9336 file->line_num = 0; /* do not display line number if error */
9337 if (ehdr.e_type == ET_REL) {
9338 ret = tcc_load_object_file(s1, fd, 0);
9339 } else if (ehdr.e_type == ET_DYN) {
9340 ret = tcc_load_dll(s1, fd, filename,
9341 (flags & AFF_REFERENCED_DLL) != 0);
9342 } else {
9343 error_noabort("unrecognized ELF file");
9344 goto fail;
9346 } else if (memcmp((char *)&ehdr, ARMAG, 8) == 0) {
9347 file->line_num = 0; /* do not display line number if error */
9348 ret = tcc_load_archive(s1, fd);
9349 } else {
9350 /* as GNU ld, consider it is an ld script if not recognized */
9351 ret = tcc_load_ldscript(s1);
9352 if (ret < 0) {
9353 error_noabort("unrecognized file type");
9354 goto fail;
9358 the_end:
9359 tcc_close(file);
9360 fail1:
9361 file = saved_file;
9362 return ret;
9363 fail:
9364 ret = -1;
9365 goto the_end;
9368 int tcc_add_file(TCCState *s, const char *filename)
9370 return tcc_add_file_internal(s, filename, AFF_PRINT_ERROR);
9373 int tcc_add_library_path(TCCState *s, const char *pathname)
9375 char *pathname1;
9377 pathname1 = tcc_strdup(pathname);
9378 dynarray_add((void ***)&s->library_paths, &s->nb_library_paths, pathname1);
9379 return 0;
9382 /* find and load a dll. Return non zero if not found */
9383 /* XXX: add '-rpath' option support ? */
9384 static int tcc_add_dll(TCCState *s, const char *filename, int flags)
9386 char buf[1024];
9387 int i;
9389 for(i = 0; i < s->nb_library_paths; i++) {
9390 snprintf(buf, sizeof(buf), "%s/%s",
9391 s->library_paths[i], filename);
9392 if (tcc_add_file_internal(s, buf, flags) == 0)
9393 return 0;
9395 return -1;
9398 /* the library name is the same as the argument of the '-l' option */
9399 int tcc_add_library(TCCState *s, const char *libraryname)
9401 char buf[1024];
9402 int i;
9403 void *h;
9405 /* first we look for the dynamic library if not static linking */
9406 if (!s->static_link) {
9407 snprintf(buf, sizeof(buf), "lib%s.so", libraryname);
9408 /* if we output to memory, then we simply we dlopen(). */
9409 if (s->output_type == TCC_OUTPUT_MEMORY) {
9410 /* Since the libc is already loaded, we don't need to load it again */
9411 if (!strcmp(libraryname, "c"))
9412 return 0;
9413 h = dlopen(buf, RTLD_GLOBAL | RTLD_LAZY);
9414 if (h)
9415 return 0;
9416 } else {
9417 if (tcc_add_dll(s, buf, 0) == 0)
9418 return 0;
9422 /* then we look for the static library */
9423 for(i = 0; i < s->nb_library_paths; i++) {
9424 snprintf(buf, sizeof(buf), "%s/lib%s.a",
9425 s->library_paths[i], libraryname);
9426 if (tcc_add_file_internal(s, buf, 0) == 0)
9427 return 0;
9429 return -1;
9432 int tcc_add_symbol(TCCState *s, const char *name, unsigned long val)
9434 add_elf_sym(symtab_section, val, 0,
9435 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE),
9436 SHN_ABS, name);
9437 return 0;
9440 int tcc_set_output_type(TCCState *s, int output_type)
9442 char buf[1024];
9444 s->output_type = output_type;
9446 if (!s->nostdinc) {
9447 /* default include paths */
9448 /* XXX: reverse order needed if -isystem support */
9449 tcc_add_sysinclude_path(s, "/usr/local/include");
9450 tcc_add_sysinclude_path(s, "/usr/include");
9451 snprintf(buf, sizeof(buf), "%s/include", tcc_lib_path);
9452 tcc_add_sysinclude_path(s, buf);
9455 /* if bound checking, then add corresponding sections */
9456 #ifdef CONFIG_TCC_BCHECK
9457 if (do_bounds_check) {
9458 /* define symbol */
9459 tcc_define_symbol(s, "__BOUNDS_CHECKING_ON", NULL);
9460 /* create bounds sections */
9461 bounds_section = new_section(s, ".bounds",
9462 SHT_PROGBITS, SHF_ALLOC);
9463 lbounds_section = new_section(s, ".lbounds",
9464 SHT_PROGBITS, SHF_ALLOC);
9466 #endif
9468 /* add debug sections */
9469 if (do_debug) {
9470 /* stab symbols */
9471 stab_section = new_section(s, ".stab", SHT_PROGBITS, 0);
9472 stab_section->sh_entsize = sizeof(Stab_Sym);
9473 stabstr_section = new_section(s, ".stabstr", SHT_STRTAB, 0);
9474 put_elf_str(stabstr_section, "");
9475 stab_section->link = stabstr_section;
9476 /* put first entry */
9477 put_stabs("", 0, 0, 0, 0);
9480 /* add libc crt1/crti objects */
9481 if (output_type == TCC_OUTPUT_EXE ||
9482 output_type == TCC_OUTPUT_DLL) {
9483 if (output_type != TCC_OUTPUT_DLL)
9484 tcc_add_file(s, CONFIG_TCC_CRT_PREFIX "/crt1.o");
9485 tcc_add_file(s, CONFIG_TCC_CRT_PREFIX "/crti.o");
9487 return 0;
9490 #define WD_ALL 0x0001 /* warning is activated when using -Wall */
9492 typedef struct WarningDef {
9493 int offset;
9494 int flags;
9495 const char *name;
9496 } WarningDef;
9498 static const WarningDef warning_defs[] = {
9499 { offsetof(TCCState, warn_unsupported), 0, "unsupported" },
9500 { offsetof(TCCState, warn_write_strings), 0, "write-strings" },
9501 { offsetof(TCCState, warn_error), 0, "error" },
9504 /* set/reset a warning */
9505 int tcc_set_warning(TCCState *s, const char *warning_name, int value)
9507 int i;
9508 const WarningDef *p;
9509 if (!strcmp(warning_name, "all")) {
9510 for(i = 0, p = warning_defs; i < countof(warning_defs); i++, p++) {
9511 if (p->flags & WD_ALL)
9512 *(int *)((uint8_t *)s + p->offset) = 1;
9514 } else {
9515 for(i = 0, p = warning_defs; i < countof(warning_defs); i++, p++) {
9516 if (!strcmp(warning_name, p->name))
9517 goto found;
9519 return -1;
9520 found:
9521 *(int *)((uint8_t *)s + p->offset) = value;
9523 return 0;
9527 #if !defined(LIBTCC)
9529 /* extract the basename of a file */
9530 static const char *tcc_basename(const char *name)
9532 const char *p;
9533 p = strrchr(name, '/');
9534 #ifdef WIN32
9535 if (!p)
9536 p = strrchr(name, '\\');
9537 #endif
9538 if (!p)
9539 p = name;
9540 else
9541 p++;
9542 return p;
9545 static int64_t getclock_us(void)
9547 #ifdef WIN32
9548 struct _timeb tb;
9549 _ftime(&tb);
9550 return (tb.time * 1000LL + tb.millitm) * 1000LL;
9551 #else
9552 struct timeval tv;
9553 gettimeofday(&tv, NULL);
9554 return tv.tv_sec * 1000000LL + tv.tv_usec;
9555 #endif
9558 void help(void)
9560 printf("tcc version " TCC_VERSION " - Tiny C Compiler - Copyright (C) 2001-2003 Fabrice Bellard\n"
9561 "usage: tcc [-v] [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"
9562 " [-Wwarn] [-g] [-b] [-bt N] [-Ldir] [-llib] [-shared] [-static]\n"
9563 " [infile1 infile2...] [-run infile args...]\n"
9564 "\n"
9565 "General options:\n"
9566 " -v display current version\n"
9567 " -c compile only - generate an object file\n"
9568 " -o outfile set output filename\n"
9569 " -Bdir set tcc internal library path\n"
9570 " -bench output compilation statistics\n"
9571 " -run run compiled source\n"
9572 " -Wwarning set or reset (with 'no-' prefix) 'warning'\n"
9573 "Preprocessor options:\n"
9574 " -Idir add include path 'dir'\n"
9575 " -Dsym[=val] define 'sym' with value 'val'\n"
9576 " -Usym undefine 'sym'\n"
9577 "Linker options:\n"
9578 " -Ldir add library path 'dir'\n"
9579 " -llib link with dynamic or static library 'lib'\n"
9580 " -shared generate a shared library\n"
9581 " -static static linking\n"
9582 " -r relocatable output\n"
9583 "Debugger options:\n"
9584 " -g generate runtime debug info\n"
9585 #ifdef CONFIG_TCC_BCHECK
9586 " -b compile with built-in memory and bounds checker (implies -g)\n"
9587 #endif
9588 " -bt N show N callers in stack traces\n"
9592 #define TCC_OPTION_HAS_ARG 0x0001
9593 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
9595 typedef struct TCCOption {
9596 const char *name;
9597 uint16_t index;
9598 uint16_t flags;
9599 } TCCOption;
9601 enum {
9602 TCC_OPTION_HELP,
9603 TCC_OPTION_I,
9604 TCC_OPTION_D,
9605 TCC_OPTION_U,
9606 TCC_OPTION_L,
9607 TCC_OPTION_B,
9608 TCC_OPTION_l,
9609 TCC_OPTION_bench,
9610 TCC_OPTION_bt,
9611 TCC_OPTION_b,
9612 TCC_OPTION_g,
9613 TCC_OPTION_c,
9614 TCC_OPTION_static,
9615 TCC_OPTION_shared,
9616 TCC_OPTION_o,
9617 TCC_OPTION_r,
9618 TCC_OPTION_W,
9619 TCC_OPTION_O,
9620 TCC_OPTION_m,
9621 TCC_OPTION_f,
9622 TCC_OPTION_nostdinc,
9623 TCC_OPTION_print_search_dirs,
9624 TCC_OPTION_rdynamic,
9625 TCC_OPTION_run,
9626 TCC_OPTION_v,
9629 static const TCCOption tcc_options[] = {
9630 { "h", TCC_OPTION_HELP, 0 },
9631 { "?", TCC_OPTION_HELP, 0 },
9632 { "I", TCC_OPTION_I, TCC_OPTION_HAS_ARG },
9633 { "D", TCC_OPTION_D, TCC_OPTION_HAS_ARG },
9634 { "U", TCC_OPTION_U, TCC_OPTION_HAS_ARG },
9635 { "L", TCC_OPTION_L, TCC_OPTION_HAS_ARG },
9636 { "B", TCC_OPTION_B, TCC_OPTION_HAS_ARG },
9637 { "l", TCC_OPTION_l, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
9638 { "bench", TCC_OPTION_bench, 0 },
9639 { "bt", TCC_OPTION_bt, TCC_OPTION_HAS_ARG },
9640 #ifdef CONFIG_TCC_BCHECK
9641 { "b", TCC_OPTION_b, 0 },
9642 #endif
9643 { "g", TCC_OPTION_g, 0 },
9644 { "c", TCC_OPTION_c, 0 },
9645 { "static", TCC_OPTION_static, 0 },
9646 { "shared", TCC_OPTION_shared, 0 },
9647 { "o", TCC_OPTION_o, TCC_OPTION_HAS_ARG },
9648 { "run", TCC_OPTION_run, 0 },
9649 { "rdynamic", TCC_OPTION_rdynamic, 0 }, /* currently ignored */
9650 { "r", TCC_OPTION_r, 0 },
9651 { "W", TCC_OPTION_W, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
9652 { "O", TCC_OPTION_O, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
9653 { "m", TCC_OPTION_m, TCC_OPTION_HAS_ARG },
9654 { "f", TCC_OPTION_f, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
9655 { "nostdinc", TCC_OPTION_nostdinc, 0 },
9656 { "print-search-dirs", TCC_OPTION_print_search_dirs, 0 },
9657 { "v", TCC_OPTION_v, 0 },
9658 { NULL },
9661 int main(int argc, char **argv)
9663 char *r;
9664 int optind, output_type, multiple_files, i, reloc_output;
9665 TCCState *s;
9666 char **files;
9667 int nb_files, nb_libraries, nb_objfiles, dminus, ret;
9668 char objfilename[1024];
9669 int64_t start_time = 0;
9670 const TCCOption *popt;
9671 const char *optarg, *p1, *r1, *outfile;
9672 int print_search_dirs;
9674 s = tcc_new();
9675 output_type = TCC_OUTPUT_EXE;
9677 optind = 1;
9678 outfile = NULL;
9679 multiple_files = 1;
9680 dminus = 0;
9681 files = NULL;
9682 nb_files = 0;
9683 nb_libraries = 0;
9684 reloc_output = 0;
9685 print_search_dirs = 0;
9686 while (1) {
9687 if (optind >= argc) {
9688 if (nb_files == 0 && !print_search_dirs)
9689 goto show_help;
9690 else
9691 break;
9693 r = argv[optind++];
9694 if (r[0] != '-') {
9695 /* add a new file */
9696 dynarray_add((void ***)&files, &nb_files, r);
9697 if (!multiple_files) {
9698 optind--;
9699 /* argv[0] will be this file */
9700 break;
9702 } else {
9703 /* find option in table (match only the first chars */
9704 popt = tcc_options;
9705 for(;;) {
9706 p1 = popt->name;
9707 if (p1 == NULL)
9708 error("invalid option -- '%s'", r);
9709 r1 = r + 1;
9710 for(;;) {
9711 if (*p1 == '\0')
9712 goto option_found;
9713 if (*r1 != *p1)
9714 break;
9715 p1++;
9716 r1++;
9718 popt++;
9720 option_found:
9721 if (popt->flags & TCC_OPTION_HAS_ARG) {
9722 if (*r1 != '\0' || (popt->flags & TCC_OPTION_NOSEP)) {
9723 optarg = r1;
9724 } else {
9725 if (optind >= argc)
9726 error("argument to '%s' is missing", r);
9727 optarg = argv[optind++];
9729 } else {
9730 if (*r1 != '\0')
9731 goto show_help;
9732 optarg = NULL;
9735 switch(popt->index) {
9736 case TCC_OPTION_HELP:
9737 show_help:
9738 help();
9739 return 1;
9740 case TCC_OPTION_I:
9741 if (tcc_add_include_path(s, optarg) < 0)
9742 error("too many include paths");
9743 break;
9744 case TCC_OPTION_D:
9746 char *sym, *value;
9747 sym = (char *)optarg;
9748 value = strchr(sym, '=');
9749 if (value) {
9750 *value = '\0';
9751 value++;
9753 tcc_define_symbol(s, sym, value);
9755 break;
9756 case TCC_OPTION_U:
9757 tcc_undefine_symbol(s, optarg);
9758 break;
9759 case TCC_OPTION_L:
9760 tcc_add_library_path(s, optarg);
9761 break;
9762 case TCC_OPTION_B:
9763 /* set tcc utilities path (mainly for tcc development) */
9764 tcc_lib_path = optarg;
9765 break;
9766 case TCC_OPTION_l:
9767 dynarray_add((void ***)&files, &nb_files, r);
9768 nb_libraries++;
9769 break;
9770 case TCC_OPTION_bench:
9771 do_bench = 1;
9772 break;
9773 case TCC_OPTION_bt:
9774 num_callers = atoi(optarg);
9775 break;
9776 #ifdef CONFIG_TCC_BCHECK
9777 case TCC_OPTION_b:
9778 do_bounds_check = 1;
9779 do_debug = 1;
9780 break;
9781 #endif
9782 case TCC_OPTION_g:
9783 do_debug = 1;
9784 break;
9785 case TCC_OPTION_c:
9786 multiple_files = 1;
9787 output_type = TCC_OUTPUT_OBJ;
9788 break;
9789 case TCC_OPTION_static:
9790 s->static_link = 1;
9791 break;
9792 case TCC_OPTION_shared:
9793 output_type = TCC_OUTPUT_DLL;
9794 break;
9795 case TCC_OPTION_o:
9796 multiple_files = 1;
9797 outfile = optarg;
9798 break;
9799 case TCC_OPTION_r:
9800 /* generate a .o merging several output files */
9801 reloc_output = 1;
9802 output_type = TCC_OUTPUT_OBJ;
9803 break;
9804 case TCC_OPTION_nostdinc:
9805 s->nostdinc = 1;
9806 break;
9807 case TCC_OPTION_print_search_dirs:
9808 print_search_dirs = 1;
9809 break;
9810 case TCC_OPTION_run:
9811 multiple_files = 0;
9812 output_type = TCC_OUTPUT_MEMORY;
9813 break;
9814 case TCC_OPTION_v:
9815 printf("tcc version %s\n", TCC_VERSION);
9816 return 0;
9817 case TCC_OPTION_W:
9819 const char *p = optarg;
9820 int value;
9821 value = 1;
9822 if (p[0] == 'n' && p[1] == 'o' && p[2] == '-') {
9823 p += 2;
9824 value = 0;
9826 if (tcc_set_warning(s, p, value) < 0 && s->warn_unsupported)
9827 goto unsupported_option;
9829 break;
9830 default:
9831 if (s->warn_unsupported) {
9832 unsupported_option:
9833 warning("unsupported option '%s'", r);
9835 break;
9839 if (print_search_dirs) {
9840 /* enough for Linux kernel */
9841 printf("install: %s/\n", tcc_lib_path);
9842 return 0;
9845 nb_objfiles = nb_files - nb_libraries;
9847 /* if outfile provided without other options, we output an
9848 executable */
9849 if (outfile && output_type == TCC_OUTPUT_MEMORY)
9850 output_type = TCC_OUTPUT_EXE;
9852 /* check -c consistency : only single file handled. XXX: checks file type */
9853 if (output_type == TCC_OUTPUT_OBJ && !reloc_output) {
9854 /* accepts only a single input file */
9855 if (nb_objfiles != 1)
9856 error("cannot specify multiple files with -c");
9857 if (nb_libraries != 0)
9858 error("cannot specify libraries with -c");
9861 /* compute default outfile name */
9862 if (output_type != TCC_OUTPUT_MEMORY && !outfile) {
9863 if (output_type == TCC_OUTPUT_OBJ && !reloc_output) {
9864 char *ext;
9865 /* strip path */
9866 pstrcpy(objfilename, sizeof(objfilename) - 1,
9867 tcc_basename(files[0]));
9868 /* add .o extension */
9869 ext = strrchr(objfilename, '.');
9870 if (!ext)
9871 goto default_outfile;
9872 strcpy(ext + 1, "o");
9873 } else {
9874 default_outfile:
9875 pstrcpy(objfilename, sizeof(objfilename), "a.out");
9877 outfile = objfilename;
9880 if (do_bench) {
9881 start_time = getclock_us();
9884 tcc_set_output_type(s, output_type);
9886 /* compile or add each files or library */
9887 for(i = 0;i < nb_files; i++) {
9888 const char *filename;
9890 filename = files[i];
9891 if (filename[0] == '-') {
9892 if (tcc_add_library(s, filename + 2) < 0)
9893 error("cannot find %s", filename);
9894 } else {
9895 if (tcc_add_file(s, filename) < 0) {
9896 ret = 1;
9897 goto the_end;
9902 /* free all files */
9903 tcc_free(files);
9905 if (do_bench) {
9906 double total_time;
9907 total_time = (double)(getclock_us() - start_time) / 1000000.0;
9908 if (total_time < 0.001)
9909 total_time = 0.001;
9910 if (total_bytes < 1)
9911 total_bytes = 1;
9912 printf("%d idents, %d lines, %d bytes, %0.3f s, %d lines/s, %0.1f MB/s\n",
9913 tok_ident - TOK_IDENT, total_lines, total_bytes,
9914 total_time, (int)(total_lines / total_time),
9915 total_bytes / total_time / 1000000.0);
9918 if (s->output_type != TCC_OUTPUT_MEMORY) {
9919 tcc_output_file(s, outfile);
9920 ret = 0;
9921 } else {
9922 ret = tcc_run(s, argc - optind, argv + optind);
9924 the_end:
9925 /* XXX: cannot do it with bound checking because of the malloc hooks */
9926 if (!do_bounds_check)
9927 tcc_delete(s);
9929 #ifdef MEM_DEBUG
9930 if (do_bench) {
9931 printf("memory: %d bytes, max = %d bytes\n", mem_cur_size, mem_max_size);
9933 #endif
9934 return ret;
9937 #endif