2 * TCC - Tiny C Compiler
4 * Copyright (c) 2001, 2002, 2003 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
36 #include <sys/timeb.h>
40 #include <sys/ucontext.h>
44 #ifndef CONFIG_TCC_STATIC
52 /* preprocessor debug */
54 /* include file 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
70 #if !defined(WIN32) && !defined(TCC_UCLIBC)
71 #define CONFIG_TCC_BCHECK /* enable bound checking code */
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 */
104 typedef struct CString
{
105 int size
; /* size in bytes */
106 void *data
; /* either 'char *' or 'int *' */
108 void *data_allocated
; /* if non NULL, data has been malloced */
111 /* type definition */
112 typedef struct CType
{
118 typedef union CValue
{
124 unsigned int ul
; /* address (should be unsigned long on 64 bit cpu) */
126 unsigned long long ull
;
127 struct CString
*cstr
;
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) */
142 /* symbol management */
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 */
153 /* section definition */
154 /* XXX: use directly ELF structure for parameters ? */
155 /* special flag to indicate that the section should not be linked to
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
[1]; /* section name */
181 typedef struct DLLReference
{
186 /* GNUC attribute definition */
187 typedef struct AttributeDef
{
190 unsigned char func_call
; /* FUNC_CDECL or FUNC_STDCALL */
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
{
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 */
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
{
247 /* used to record tokens */
248 typedef struct 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
{
259 char type
; /* '"' or '>' to give include type */
260 char filename
[1]; /* path specified in #include */
264 static struct BufferedFile
*file
;
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
285 static Section
*text_section
, *data_section
, *bss_section
; /* predefined sections */
286 static Section
*cur_text_section
; /* current section where function code is
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
;
295 static Section
*stab_section
, *stabstr_section
;
297 /* loc : local variable index
298 ind : output code index
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
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 */
335 static int do_bench
= 0;
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";
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
375 DLLReference
**loaded_dlls
;
380 int nb_sections
; /* number of sections, including first dummy section */
385 unsigned long *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 */
395 int nostdinc
; /* if true, no standard headers are added */
396 int nostdlib
; /* if true, no standard libraries are added */
398 /* if true, static linking is performed */
401 /* if true, all symbols are exported */
404 /* if true, only link in referenced objects from archive */
407 /* warning switches */
408 int warn_write_strings
;
409 int warn_unsupported
;
415 void (*error_func
)(void *opaque
, const char *msg
);
416 int error_set_jmp_enabled
;
417 jmp_buf error_jmp_buf
;
420 /* tiny assembler state */
423 /* see include_stack_ptr */
424 BufferedFile
*include_stack
[INCLUDE_STACK_SIZE
];
426 /* see ifdef_stack_ptr */
427 int ifdef_stack
[IFDEF_STACK_SIZE
];
430 /* The current value can be: */
431 #define VT_VALMASK 0x00ff
432 #define VT_CONST 0x00f0 /* constant in vc
433 (must be first non register value) */
434 #define VT_LLOCAL 0x00f1 /* lvalue, offset on stack */
435 #define VT_LOCAL 0x00f2 /* offset on stack */
436 #define VT_CMP 0x00f3 /* the value is stored in processor flags (in vc) */
437 #define VT_JMP 0x00f4 /* value is the consequence of jmp true (even) */
438 #define VT_JMPI 0x00f5 /* value is the consequence of jmp false (odd) */
439 #define VT_LVAL 0x0100 /* var is an lvalue */
440 #define VT_SYM 0x0200 /* a symbol value is added */
441 #define VT_MUSTCAST 0x0400 /* value must be casted to be correct (used for
442 char/short stored in integer registers) */
443 #define VT_MUSTBOUND 0x0800 /* bound checking must be done before
444 dereferencing value */
445 #define VT_BOUNDED 0x8000 /* value is bounded. The address of the
446 bounding function call point is in vc */
447 #define VT_LVAL_BYTE 0x1000 /* lvalue is a byte */
448 #define VT_LVAL_SHORT 0x2000 /* lvalue is a short */
449 #define VT_LVAL_UNSIGNED 0x4000 /* lvalue is unsigned */
450 #define VT_LVAL_TYPE (VT_LVAL_BYTE | VT_LVAL_SHORT | VT_LVAL_UNSIGNED)
453 #define VT_INT 0 /* integer type */
454 #define VT_BYTE 1 /* signed byte type */
455 #define VT_SHORT 2 /* short type */
456 #define VT_VOID 3 /* void type */
457 #define VT_PTR 4 /* pointer */
458 #define VT_ENUM 5 /* enum definition */
459 #define VT_FUNC 6 /* function type */
460 #define VT_STRUCT 7 /* struct/union definition */
461 #define VT_FLOAT 8 /* IEEE float */
462 #define VT_DOUBLE 9 /* IEEE double */
463 #define VT_LDOUBLE 10 /* IEEE long double */
464 #define VT_BOOL 11 /* ISOC99 boolean type */
465 #define VT_LLONG 12 /* 64 bit integer */
466 #define VT_LONG 13 /* long integer (NEVER USED as type, only
468 #define VT_BTYPE 0x000f /* mask for basic type */
469 #define VT_UNSIGNED 0x0010 /* unsigned type */
470 #define VT_ARRAY 0x0020 /* array type (also has VT_PTR) */
471 #define VT_BITFIELD 0x0040 /* bitfield modifier */
472 #define VT_CONSTANT 0x0800 /* const modifier */
473 #define VT_VOLATILE 0x1000 /* volatile modifier */
476 #define VT_EXTERN 0x00000080 /* extern definition */
477 #define VT_STATIC 0x00000100 /* static variable */
478 #define VT_TYPEDEF 0x00000200 /* typedef definition */
479 #define VT_INLINE 0x00000400 /* inline definition */
481 #define VT_STRUCT_SHIFT 16 /* shift for bitfield shift values */
483 /* type mask (except storage) */
484 #define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE)
485 #define VT_TYPE (~(VT_STORAGE))
489 /* warning: the following compare tokens depend on i386 asm code */
501 #define TOK_LAND 0xa0
505 #define TOK_MID 0xa3 /* inc/dec, to void constant */
507 #define TOK_UDIV 0xb0 /* unsigned division */
508 #define TOK_UMOD 0xb1 /* unsigned modulo */
509 #define TOK_PDIV 0xb2 /* fast division with undefined rounding for pointers */
510 #define TOK_CINT 0xb3 /* number in tokc */
511 #define TOK_CCHAR 0xb4 /* char constant in tokc */
512 #define TOK_STR 0xb5 /* pointer to string in tokc */
513 #define TOK_TWOSHARPS 0xb6 /* ## preprocessing token */
514 #define TOK_LCHAR 0xb7
515 #define TOK_LSTR 0xb8
516 #define TOK_CFLOAT 0xb9 /* float constant */
517 #define TOK_LINENUM 0xba /* line number info */
518 #define TOK_CDOUBLE 0xc0 /* double constant */
519 #define TOK_CLDOUBLE 0xc1 /* long double constant */
520 #define TOK_UMULL 0xc2 /* unsigned 32x32 -> 64 mul */
521 #define TOK_ADDC1 0xc3 /* add with carry generation */
522 #define TOK_ADDC2 0xc4 /* add with carry use */
523 #define TOK_SUBC1 0xc5 /* add with carry generation */
524 #define TOK_SUBC2 0xc6 /* add with carry use */
525 #define TOK_CUINT 0xc8 /* unsigned int constant */
526 #define TOK_CLLONG 0xc9 /* long long constant */
527 #define TOK_CULLONG 0xca /* unsigned long long constant */
528 #define TOK_ARROW 0xcb
529 #define TOK_DOTS 0xcc /* three dots */
530 #define TOK_SHR 0xcd /* unsigned shift right */
531 #define TOK_PPNUM 0xce /* preprocessor number */
533 #define TOK_SHL 0x01 /* shift left */
534 #define TOK_SAR 0x02 /* signed shift right */
536 /* assignement operators : normal operator or 0x80 */
537 #define TOK_A_MOD 0xa5
538 #define TOK_A_AND 0xa6
539 #define TOK_A_MUL 0xaa
540 #define TOK_A_ADD 0xab
541 #define TOK_A_SUB 0xad
542 #define TOK_A_DIV 0xaf
543 #define TOK_A_XOR 0xde
544 #define TOK_A_OR 0xfc
545 #define TOK_A_SHL 0x81
546 #define TOK_A_SAR 0x82
549 #define offsetof(type, field) ((size_t) &((type *)0)->field)
553 #define countof(tab) (sizeof(tab) / sizeof((tab)[0]))
556 /* WARNING: the content of this string encodes token numbers */
557 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";
559 #define TOK_EOF (-1) /* end of file */
560 #define TOK_LINEFEED 10 /* line feed */
562 /* all identificators and strings have token above that */
563 #define TOK_IDENT 256
565 /* only used for i386 asm opcodes definitions */
566 #define DEF_ASM(x) DEF(TOK_ASM_ ## x, #x)
569 DEF(TOK_ASM_ ## x ## b, #x "b") \
570 DEF(TOK_ASM_ ## x ## w, #x "w") \
571 DEF(TOK_ASM_ ## x ## l, #x "l") \
572 DEF(TOK_ASM_ ## x, #x)
575 DEF(TOK_ASM_ ## x ## w, #x "w") \
576 DEF(TOK_ASM_ ## x ## l, #x "l") \
577 DEF(TOK_ASM_ ## x, #x)
580 DEF(TOK_ASM_ ## f ## x ## s, "f" #x "s") \
581 DEF(TOK_ASM_ ## fi ## x ## l, "fi" #x "l") \
582 DEF(TOK_ASM_ ## f ## x ## l, "f" #x "l") \
583 DEF(TOK_ASM_ ## fi ## x ## s, "fi" #x "s")
586 DEF(TOK_ASM_ ## f ## x, "f" #x ) \
587 DEF(TOK_ASM_ ## f ## x ## p, "f" #x "p") \
590 #define DEF_ASMTEST(x) \
622 #define TOK_ASM_int TOK_INT
625 TOK_LAST
= TOK_IDENT
- 1,
626 #define DEF(id, str) id,
631 static const char tcc_keywords
[] =
632 #define DEF(id, str) str "\0"
637 #define TOK_UIDENT TOK_DEFINE
640 #define snprintf _snprintf
641 #define vsnprintf _vsnprintf
644 #if defined(WIN32) || defined(TCC_UCLIBC) || defined(__FreeBSD__)
645 /* currently incorrect */
646 long double strtold(const char *nptr
, char **endptr
)
648 return (long double)strtod(nptr
, endptr
);
650 float strtof(const char *nptr
, char **endptr
)
652 return (float)strtod(nptr
, endptr
);
655 /* XXX: need to define this to use them in non ISOC99 context */
656 extern float strtof (const char *__nptr
, char **__endptr
);
657 extern long double strtold (const char *__nptr
, char **__endptr
);
660 static char *pstrcpy(char *buf
, int buf_size
, const char *s
);
661 static char *pstrcat(char *buf
, int buf_size
, const char *s
);
663 static void next(void);
664 static void next_nomacro(void);
665 static void parse_expr_type(CType
*type
);
666 static void expr_type(CType
*type
);
667 static void unary_type(CType
*type
);
668 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
669 int case_reg
, int is_expr
);
670 static int expr_const(void);
671 static void expr_eq(void);
672 static void gexpr(void);
673 static void decl(int l
);
674 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
675 int first
, int size_only
);
676 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
677 int has_init
, int v
, int scope
);
679 void gv2(int rc1
, int rc2
);
680 void move_reg(int r
, int s
);
681 void save_regs(int n
);
682 void save_reg(int r
);
688 static void macro_subst(TokenString
*tok_str
, Sym
**nested_list
,
689 const int *macro_str
, int can_read_stream
);
690 int save_reg_forced(int r
);
692 void force_charshort_cast(int t
);
693 static void gen_cast(CType
*type
);
695 static Sym
*sym_find(int v
);
696 static Sym
*sym_push(int v
, CType
*type
, int r
, int c
);
699 static int type_size(CType
*type
, int *a
);
700 static inline CType
*pointed_type(CType
*type
);
701 static int pointed_size(CType
*type
);
702 static int lvalue_type(int t
);
703 static int parse_btype(CType
*type
, AttributeDef
*ad
);
704 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
);
705 static int is_compatible_types(CType
*type1
, CType
*type2
);
707 void error(const char *fmt
, ...);
709 void vset(CType
*type
, int r
, int v
);
710 void type_to_str(char *buf
, int buf_size
,
711 CType
*type
, const char *varstr
);
712 char *get_tok_str(int v
, CValue
*cv
);
713 static Sym
*get_sym_ref(CType
*type
, Section
*sec
,
714 unsigned long offset
, unsigned long size
);
715 static Sym
*external_global_sym(int v
, CType
*type
, int r
);
717 /* section generation */
718 static void section_realloc(Section
*sec
, unsigned long new_size
);
719 static void *section_ptr_add(Section
*sec
, unsigned long size
);
720 static void put_extern_sym(Sym
*sym
, Section
*section
,
721 unsigned long value
, unsigned long size
);
722 static void greloc(Section
*s
, Sym
*sym
, unsigned long addr
, int type
);
723 static int put_elf_str(Section
*s
, const char *sym
);
724 static int put_elf_sym(Section
*s
,
725 unsigned long value
, unsigned long size
,
726 int info
, int other
, int shndx
, const char *name
);
727 static int add_elf_sym(Section
*s
, unsigned long value
, unsigned long size
,
728 int info
, int sh_num
, const char *name
);
729 static void put_elf_reloc(Section
*symtab
, Section
*s
, unsigned long offset
,
730 int type
, int symbol
);
731 static void put_stabs(const char *str
, int type
, int other
, int desc
,
732 unsigned long value
);
733 static void put_stabs_r(const char *str
, int type
, int other
, int desc
,
734 unsigned long value
, Section
*sec
, int sym_index
);
735 static void put_stabn(int type
, int other
, int desc
, int value
);
736 static void put_stabd(int type
, int other
, int desc
);
737 static int tcc_add_dll(TCCState
*s
, const char *filename
, int flags
);
739 #define AFF_PRINT_ERROR 0x0001 /* print error if file not found */
740 #define AFF_REFERENCED_DLL 0x0002 /* load a referenced dll from another dll */
741 static int tcc_add_file_internal(TCCState
*s
, const char *filename
, int flags
);
745 #ifdef CONFIG_TCC_ASM
747 typedef struct ExprValue
{
752 #define MAX_ASM_OPERANDS 30
754 typedef struct ASMOperand
{
755 int id
; /* GCC 3 optionnal identifier (0 if number only supported */
757 char asm_str
[16]; /* computed asm string for operand */
758 SValue
*vt
; /* C value of the expression */
759 int ref_index
; /* if >= 0, gives reference to a output constraint */
760 int priority
; /* priority, used to assign registers */
761 int reg
; /* if >= 0, register number used for this operand */
762 int is_llong
; /* true if double register value */
765 static void asm_expr(TCCState
*s1
, ExprValue
*pe
);
766 static int asm_int_expr(TCCState
*s1
);
767 static int find_constraint(ASMOperand
*operands
, int nb_operands
,
768 const char *name
, const char **pp
);
770 static int tcc_assemble(TCCState
*s1
, int do_preprocess
);
774 static void asm_instr(void);
776 /* true if float/double/long double type */
777 static inline int is_float(int t
)
781 return bt
== VT_LDOUBLE
|| bt
== VT_DOUBLE
|| bt
== VT_FLOAT
;
784 #ifdef TCC_TARGET_I386
785 #include "i386-gen.c"
788 #ifdef CONFIG_TCC_STATIC
790 #define RTLD_LAZY 0x001
791 #define RTLD_NOW 0x002
792 #define RTLD_GLOBAL 0x100
793 #define RTLD_DEFAULT NULL
795 /* dummy function for profiling */
796 void *dlopen(const char *filename
, int flag
)
801 const char *dlerror(void)
806 typedef struct TCCSyms
{
811 #define TCCSYM(a) { #a, &a, },
813 /* add the symbol you want here if no dynamic linking is done */
814 static TCCSyms tcc_syms
[] = {
822 void *dlsym(void *handle
, const char *symbol
)
826 while (p
->str
!= NULL
) {
827 if (!strcmp(p
->str
, symbol
))
836 /********************************************************/
838 /* we use our own 'finite' function to avoid potential problems with
839 non standard math libs */
840 /* XXX: endianness dependent */
841 int ieee_finite(double d
)
844 return ((unsigned)((p
[1] | 0x800fffff) + 1)) >> 31;
847 /* copy a string and truncate it. */
848 static char *pstrcpy(char *buf
, int buf_size
, const char *s
)
855 q_end
= buf
+ buf_size
- 1;
867 /* strcat and truncate. */
868 static char *pstrcat(char *buf
, int buf_size
, const char *s
)
873 pstrcpy(buf
+ len
, buf_size
- len
, s
);
877 /* memory management */
883 static inline void tcc_free(void *ptr
)
886 mem_cur_size
-= malloc_usable_size(ptr
);
891 static void *tcc_malloc(unsigned long size
)
896 error("memory full");
898 mem_cur_size
+= malloc_usable_size(ptr
);
899 if (mem_cur_size
> mem_max_size
)
900 mem_max_size
= mem_cur_size
;
905 static void *tcc_mallocz(unsigned long size
)
908 ptr
= tcc_malloc(size
);
909 memset(ptr
, 0, size
);
913 static inline void *tcc_realloc(void *ptr
, unsigned long size
)
917 mem_cur_size
-= malloc_usable_size(ptr
);
919 ptr1
= realloc(ptr
, size
);
921 /* NOTE: count not correct if alloc error, but not critical */
922 mem_cur_size
+= malloc_usable_size(ptr1
);
923 if (mem_cur_size
> mem_max_size
)
924 mem_max_size
= mem_cur_size
;
929 static char *tcc_strdup(const char *str
)
932 ptr
= tcc_malloc(strlen(str
) + 1);
937 #define free(p) use_tcc_free(p)
938 #define malloc(s) use_tcc_malloc(s)
939 #define realloc(p, s) use_tcc_realloc(p, s)
941 static void dynarray_add(void ***ptab
, int *nb_ptr
, void *data
)
948 /* every power of two we double array size */
949 if ((nb
& (nb
- 1)) == 0) {
954 pp
= tcc_realloc(pp
, nb_alloc
* sizeof(void *));
956 error("memory full");
963 Section
*new_section(TCCState
*s1
, const char *name
, int sh_type
, int sh_flags
)
967 sec
= tcc_mallocz(sizeof(Section
) + strlen(name
));
968 strcpy(sec
->name
, name
);
969 sec
->sh_type
= sh_type
;
970 sec
->sh_flags
= sh_flags
;
977 sec
->sh_addralign
= 4;
980 sec
->sh_addralign
= 1;
983 sec
->sh_addralign
= 32; /* default conservative alignment */
987 /* only add section if not private */
988 if (!(sh_flags
& SHF_PRIVATE
)) {
989 sec
->sh_num
= s1
->nb_sections
;
990 dynarray_add((void ***)&s1
->sections
, &s1
->nb_sections
, sec
);
995 static void free_section(Section
*s
)
1001 /* realloc section and set its content to zero */
1002 static void section_realloc(Section
*sec
, unsigned long new_size
)
1005 unsigned char *data
;
1007 size
= sec
->data_allocated
;
1010 while (size
< new_size
)
1012 data
= tcc_realloc(sec
->data
, size
);
1014 error("memory full");
1015 memset(data
+ sec
->data_allocated
, 0, size
- sec
->data_allocated
);
1017 sec
->data_allocated
= size
;
1020 /* reserve at least 'size' bytes in section 'sec' from
1021 sec->data_offset. */
1022 static void *section_ptr_add(Section
*sec
, unsigned long size
)
1024 unsigned long offset
, offset1
;
1026 offset
= sec
->data_offset
;
1027 offset1
= offset
+ size
;
1028 if (offset1
> sec
->data_allocated
)
1029 section_realloc(sec
, offset1
);
1030 sec
->data_offset
= offset1
;
1031 return sec
->data
+ offset
;
1034 /* return a reference to a section, and create it if it does not
1036 Section
*find_section(TCCState
*s1
, const char *name
)
1040 for(i
= 1; i
< s1
->nb_sections
; i
++) {
1041 sec
= s1
->sections
[i
];
1042 if (!strcmp(name
, sec
->name
))
1045 /* sections are created as PROGBITS */
1046 return new_section(s1
, name
, SHT_PROGBITS
, SHF_ALLOC
);
1049 /* update sym->c so that it points to an external symbol in section
1050 'section' with value 'value' */
1051 static void put_extern_sym(Sym
*sym
, Section
*section
,
1052 unsigned long value
, unsigned long size
)
1054 int sym_type
, sym_bind
, sh_num
, info
;
1059 sh_num
= section
->sh_num
;
1063 if ((sym
->type
.t
& VT_BTYPE
) == VT_FUNC
)
1064 sym_type
= STT_FUNC
;
1066 sym_type
= STT_OBJECT
;
1067 if (sym
->type
.t
& VT_STATIC
)
1068 sym_bind
= STB_LOCAL
;
1070 sym_bind
= STB_GLOBAL
;
1072 name
= get_tok_str(sym
->v
, NULL
);
1073 #ifdef CONFIG_TCC_BCHECK
1074 if (do_bounds_check
) {
1077 /* XXX: avoid doing that for statics ? */
1078 /* if bound checking is activated, we change some function
1079 names by adding the "__bound" prefix */
1082 /* XXX: we rely only on malloc hooks */
1094 strcpy(buf
, "__bound_");
1101 info
= ELF32_ST_INFO(sym_bind
, sym_type
);
1102 sym
->c
= add_elf_sym(symtab_section
, value
, size
, info
, sh_num
, name
);
1104 esym
= &((Elf32_Sym
*)symtab_section
->data
)[sym
->c
];
1105 esym
->st_value
= value
;
1106 esym
->st_size
= size
;
1107 esym
->st_shndx
= sh_num
;
1111 /* add a new relocation entry to symbol 'sym' in section 's' */
1112 static void greloc(Section
*s
, Sym
*sym
, unsigned long offset
, int type
)
1115 put_extern_sym(sym
, NULL
, 0, 0);
1116 /* now we can add ELF relocation info */
1117 put_elf_reloc(symtab_section
, s
, offset
, type
, sym
->c
);
1120 static inline int isid(int c
)
1122 return (c
>= 'a' && c
<= 'z') ||
1123 (c
>= 'A' && c
<= 'Z') ||
1127 static inline int isnum(int c
)
1129 return c
>= '0' && c
<= '9';
1132 static inline int isoct(int c
)
1134 return c
>= '0' && c
<= '7';
1137 static inline int toup(int c
)
1139 if (c
>= 'a' && c
<= 'z')
1140 return c
- 'a' + 'A';
1145 static void strcat_vprintf(char *buf
, int buf_size
, const char *fmt
, va_list ap
)
1149 vsnprintf(buf
+ len
, buf_size
- len
, fmt
, ap
);
1152 static void strcat_printf(char *buf
, int buf_size
, const char *fmt
, ...)
1156 strcat_vprintf(buf
, buf_size
, fmt
, ap
);
1160 void error1(TCCState
*s1
, int is_warning
, const char *fmt
, va_list ap
)
1167 for(f
= s1
->include_stack
; f
< s1
->include_stack_ptr
; f
++)
1168 strcat_printf(buf
, sizeof(buf
), "In file included from %s:%d:\n",
1169 (*f
)->filename
, (*f
)->line_num
);
1170 if (file
->line_num
> 0) {
1171 strcat_printf(buf
, sizeof(buf
),
1172 "%s:%d: ", file
->filename
, file
->line_num
);
1174 strcat_printf(buf
, sizeof(buf
),
1175 "%s: ", file
->filename
);
1178 strcat_printf(buf
, sizeof(buf
),
1182 strcat_printf(buf
, sizeof(buf
), "warning: ");
1183 strcat_vprintf(buf
, sizeof(buf
), fmt
, ap
);
1185 if (!s1
->error_func
) {
1186 /* default case: stderr */
1187 fprintf(stderr
, "%s\n", buf
);
1189 s1
->error_func(s1
->error_opaque
, buf
);
1191 if (!is_warning
|| s1
->warn_error
)
1196 void tcc_set_error_func(TCCState
*s
, void *error_opaque
,
1197 void (*error_func
)(void *opaque
, const char *msg
))
1199 s
->error_opaque
= error_opaque
;
1200 s
->error_func
= error_func
;
1204 /* error without aborting current compilation */
1205 void error_noabort(const char *fmt
, ...)
1207 TCCState
*s1
= tcc_state
;
1211 error1(s1
, 0, fmt
, ap
);
1215 void error(const char *fmt
, ...)
1217 TCCState
*s1
= tcc_state
;
1221 error1(s1
, 0, fmt
, ap
);
1223 /* better than nothing: in some cases, we accept to handle errors */
1224 if (s1
->error_set_jmp_enabled
) {
1225 longjmp(s1
->error_jmp_buf
, 1);
1227 /* XXX: eliminate this someday */
1232 void expect(const char *msg
)
1234 error("%s expected", msg
);
1237 void warning(const char *fmt
, ...)
1239 TCCState
*s1
= tcc_state
;
1246 error1(s1
, 1, fmt
, ap
);
1253 error("'%c' expected", c
);
1257 static void test_lvalue(void)
1259 if (!(vtop
->r
& VT_LVAL
))
1263 /* allocate a new token */
1264 static TokenSym
*tok_alloc_new(TokenSym
**pts
, const char *str
, int len
)
1266 TokenSym
*ts
, **ptable
;
1269 if (tok_ident
>= SYM_FIRST_ANOM
)
1270 error("memory full");
1272 /* expand token table if needed */
1273 i
= tok_ident
- TOK_IDENT
;
1274 if ((i
% TOK_ALLOC_INCR
) == 0) {
1275 ptable
= tcc_realloc(table_ident
, (i
+ TOK_ALLOC_INCR
) * sizeof(TokenSym
*));
1277 error("memory full");
1278 table_ident
= ptable
;
1281 ts
= tcc_malloc(sizeof(TokenSym
) + len
);
1282 table_ident
[i
] = ts
;
1283 ts
->tok
= tok_ident
++;
1284 ts
->sym_define
= NULL
;
1285 ts
->sym_label
= NULL
;
1286 ts
->sym_struct
= NULL
;
1287 ts
->sym_identifier
= NULL
;
1289 ts
->hash_next
= NULL
;
1290 memcpy(ts
->str
, str
, len
);
1291 ts
->str
[len
] = '\0';
1296 #define TOK_HASH_INIT 1
1297 #define TOK_HASH_FUNC(h, c) ((h) * 263 + (c))
1299 /* find a token and add it if not found */
1300 static TokenSym
*tok_alloc(const char *str
, int len
)
1302 TokenSym
*ts
, **pts
;
1308 h
= TOK_HASH_FUNC(h
, ((unsigned char *)str
)[i
]);
1309 h
&= (TOK_HASH_SIZE
- 1);
1311 pts
= &hash_ident
[h
];
1316 if (ts
->len
== len
&& !memcmp(ts
->str
, str
, len
))
1318 pts
= &(ts
->hash_next
);
1320 return tok_alloc_new(pts
, str
, len
);
1323 /* CString handling */
1325 static void cstr_realloc(CString
*cstr
, int new_size
)
1330 size
= cstr
->size_allocated
;
1332 size
= 8; /* no need to allocate a too small first string */
1333 while (size
< new_size
)
1335 data
= tcc_realloc(cstr
->data_allocated
, size
);
1337 error("memory full");
1338 cstr
->data_allocated
= data
;
1339 cstr
->size_allocated
= size
;
1344 static void cstr_ccat(CString
*cstr
, int ch
)
1347 size
= cstr
->size
+ 1;
1348 if (size
> cstr
->size_allocated
)
1349 cstr_realloc(cstr
, size
);
1350 ((unsigned char *)cstr
->data
)[size
- 1] = ch
;
1354 static void cstr_cat(CString
*cstr
, const char *str
)
1366 /* add a wide char */
1367 static void cstr_wccat(CString
*cstr
, int ch
)
1370 size
= cstr
->size
+ sizeof(int);
1371 if (size
> cstr
->size_allocated
)
1372 cstr_realloc(cstr
, size
);
1373 *(int *)(((unsigned char *)cstr
->data
) + size
- sizeof(int)) = ch
;
1377 static void cstr_new(CString
*cstr
)
1379 memset(cstr
, 0, sizeof(CString
));
1382 /* free string and reset it to NULL */
1383 static void cstr_free(CString
*cstr
)
1385 tcc_free(cstr
->data_allocated
);
1389 #define cstr_reset(cstr) cstr_free(cstr)
1391 static CString
*cstr_dup(CString
*cstr1
)
1396 cstr
= tcc_malloc(sizeof(CString
));
1399 cstr
->size_allocated
= size
;
1400 cstr
->data_allocated
= tcc_malloc(size
);
1401 cstr
->data
= cstr
->data_allocated
;
1402 memcpy(cstr
->data_allocated
, cstr1
->data_allocated
, size
);
1406 /* XXX: unicode ? */
1407 static void add_char(CString
*cstr
, int c
)
1409 if (c
== '\'' || c
== '\"' || c
== '\\') {
1410 /* XXX: could be more precise if char or string */
1411 cstr_ccat(cstr
, '\\');
1413 if (c
>= 32 && c
<= 126) {
1416 cstr_ccat(cstr
, '\\');
1418 cstr_ccat(cstr
, 'n');
1420 cstr_ccat(cstr
, '0' + ((c
>> 6) & 7));
1421 cstr_ccat(cstr
, '0' + ((c
>> 3) & 7));
1422 cstr_ccat(cstr
, '0' + (c
& 7));
1427 /* XXX: buffer overflow */
1428 /* XXX: float tokens */
1429 char *get_tok_str(int v
, CValue
*cv
)
1431 static char buf
[STRING_MAX_SIZE
+ 1];
1432 static CString cstr_buf
;
1438 /* NOTE: to go faster, we give a fixed buffer for small strings */
1439 cstr_reset(&cstr_buf
);
1440 cstr_buf
.data
= buf
;
1441 cstr_buf
.size_allocated
= sizeof(buf
);
1447 /* XXX: not quite exact, but only useful for testing */
1448 sprintf(p
, "%u", cv
->ui
);
1452 /* XXX: not quite exact, but only useful for testing */
1453 sprintf(p
, "%Lu", cv
->ull
);
1457 cstr_ccat(&cstr_buf
, '\'');
1458 add_char(&cstr_buf
, cv
->i
);
1459 cstr_ccat(&cstr_buf
, '\'');
1460 cstr_ccat(&cstr_buf
, '\0');
1464 len
= cstr
->size
- 1;
1466 add_char(&cstr_buf
, ((unsigned char *)cstr
->data
)[i
]);
1467 cstr_ccat(&cstr_buf
, '\0');
1472 cstr_ccat(&cstr_buf
, '\"');
1474 len
= cstr
->size
- 1;
1476 add_char(&cstr_buf
, ((unsigned char *)cstr
->data
)[i
]);
1478 len
= (cstr
->size
/ sizeof(int)) - 1;
1480 add_char(&cstr_buf
, ((int *)cstr
->data
)[i
]);
1482 cstr_ccat(&cstr_buf
, '\"');
1483 cstr_ccat(&cstr_buf
, '\0');
1492 return strcpy(p
, "<<=");
1494 return strcpy(p
, ">>=");
1496 if (v
< TOK_IDENT
) {
1497 /* search in two bytes table */
1511 } else if (v
< tok_ident
) {
1512 return table_ident
[v
- TOK_IDENT
]->str
;
1513 } else if (v
>= SYM_FIRST_ANOM
) {
1514 /* special name for anonymous symbol */
1515 sprintf(p
, "L.%u", v
- SYM_FIRST_ANOM
);
1517 /* should never happen */
1522 return cstr_buf
.data
;
1525 /* push, without hashing */
1526 static Sym
*sym_push2(Sym
**ps
, int v
, int t
, int c
)
1529 s
= tcc_malloc(sizeof(Sym
));
1540 /* find a symbol and return its associated structure. 's' is the top
1541 of the symbol stack */
1542 static Sym
*sym_find2(Sym
*s
, int v
)
1552 /* structure lookup */
1553 static inline Sym
*struct_find(int v
)
1556 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
1558 return table_ident
[v
]->sym_struct
;
1561 /* find an identifier */
1562 static inline Sym
*sym_find(int v
)
1565 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
1567 return table_ident
[v
]->sym_identifier
;
1570 /* push a given symbol on the symbol stack */
1571 static Sym
*sym_push(int v
, CType
*type
, int r
, int c
)
1580 s
= sym_push2(ps
, v
, type
->t
, c
);
1581 s
->type
.ref
= type
->ref
;
1583 /* don't record fields or anonymous symbols */
1585 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
1586 /* record symbol in token array */
1587 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
1589 ps
= &ts
->sym_struct
;
1591 ps
= &ts
->sym_identifier
;
1598 /* push a global identifier */
1599 static Sym
*global_identifier_push(int v
, int t
, int c
)
1602 s
= sym_push2(&global_stack
, v
, t
, c
);
1603 /* don't record anonymous symbol */
1604 if (v
< SYM_FIRST_ANOM
) {
1605 ps
= &table_ident
[v
- TOK_IDENT
]->sym_identifier
;
1606 /* modify the top most local identifier, so that
1607 sym_identifier will point to 's' when popped */
1609 ps
= &(*ps
)->prev_tok
;
1616 /* pop symbols until top reaches 'b' */
1617 static void sym_pop(Sym
**ptop
, Sym
*b
)
1627 /* remove symbol in token array */
1629 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
1630 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
1632 ps
= &ts
->sym_struct
;
1634 ps
= &ts
->sym_identifier
;
1645 BufferedFile
*tcc_open(TCCState
*s1
, const char *filename
)
1650 fd
= open(filename
, O_RDONLY
);
1653 bf
= tcc_malloc(sizeof(BufferedFile
));
1659 bf
->buf_ptr
= bf
->buffer
;
1660 bf
->buf_end
= bf
->buffer
;
1661 bf
->buffer
[0] = CH_EOB
; /* put eob symbol */
1662 pstrcpy(bf
->filename
, sizeof(bf
->filename
), filename
);
1664 bf
->ifndef_macro
= 0;
1665 bf
->ifdef_stack_ptr
= s1
->ifdef_stack_ptr
;
1666 // printf("opening '%s'\n", filename);
1670 void tcc_close(BufferedFile
*bf
)
1672 total_lines
+= bf
->line_num
;
1677 /* fill input buffer and peek next char */
1678 static int tcc_peekc_slow(BufferedFile
*bf
)
1681 /* only tries to read if really end of buffer */
1682 if (bf
->buf_ptr
>= bf
->buf_end
) {
1684 #if defined(PARSE_DEBUG)
1689 len
= read(bf
->fd
, bf
->buffer
, len
);
1696 bf
->buf_ptr
= bf
->buffer
;
1697 bf
->buf_end
= bf
->buffer
+ len
;
1698 *bf
->buf_end
= CH_EOB
;
1700 if (bf
->buf_ptr
< bf
->buf_end
) {
1701 return bf
->buf_ptr
[0];
1703 bf
->buf_ptr
= bf
->buf_end
;
1708 /* return the current character, handling end of block if necessary
1710 static int handle_eob(void)
1712 return tcc_peekc_slow(file
);
1715 /* read next char from current input file and handle end of input buffer */
1716 static inline void inp(void)
1718 ch
= *(++(file
->buf_ptr
));
1719 /* end of buffer/file handling */
1724 /* handle '\[\r]\n' */
1725 static void handle_stray(void)
1727 while (ch
== '\\') {
1732 } else if (ch
== '\r') {
1740 error("stray '\\' in program");
1745 /* skip the stray and handle the \\n case. Output an error if
1746 incorrect char after the stray */
1747 static int handle_stray1(uint8_t *p
)
1751 if (p
>= file
->buf_end
) {
1768 /* handle just the EOB case, but not stray */
1769 #define PEEKC_EOB(c, p)\
1780 /* handle the complicated stray case */
1781 #define PEEKC(c, p)\
1786 c = handle_stray1(p);\
1791 /* input with '\[\r]\n' handling. Note that this function cannot
1792 handle other characters after '\', so you cannot call it inside
1793 strings or comments */
1794 static void minp(void)
1802 /* single line C++ comments */
1803 static uint8_t *parse_line_comment(uint8_t *p
)
1810 if (c
== '\n' || c
== CH_EOF
) {
1812 } else if (c
== '\\') {
1817 } else if (c
== '\r') {
1832 static uint8_t *parse_comment(uint8_t *p
)
1838 /* fast skip loop */
1841 if (c
== '\n' || c
== '*' || c
== '\\')
1845 if (c
== '\n' || c
== '*' || c
== '\\')
1849 /* now we can handle all the cases */
1853 } else if (c
== '*') {
1859 } else if (c
== '/') {
1860 goto end_of_comment
;
1861 } else if (c
== '\\') {
1866 /* skip '\[\r]\n', otherwise just skip the stray */
1872 } else if (c
== '\r') {
1889 /* stray, eob or eof */
1894 error("unexpected end of file in comment");
1895 } else if (c
== '\\') {
1907 /* space exlcuding newline */
1908 static inline int is_space(int ch
)
1910 return ch
== ' ' || ch
== '\t' || ch
== '\v' || ch
== '\f' || ch
== '\r';
1913 static inline void skip_spaces(void)
1915 while (is_space(ch
))
1919 /* parse a string without interpreting escapes */
1920 static uint8_t *parse_pp_string(uint8_t *p
,
1921 int sep
, CString
*str
)
1929 } else if (c
== '\\') {
1934 unterminated_string
:
1935 /* XXX: indicate line number of start of string */
1936 error("missing terminating %c character", sep
);
1937 } else if (c
== '\\') {
1938 /* escape : just skip \[\r]\n */
1943 } else if (c
== '\r') {
1946 expect("'\n' after '\r'");
1949 } else if (c
== CH_EOF
) {
1950 goto unterminated_string
;
1953 cstr_ccat(str
, '\\');
1959 } else if (c
== '\n') {
1962 } else if (c
== '\r') {
1965 cstr_ccat(str
, '\r');
1981 /* skip block of text until #else, #elif or #endif. skip also pairs of
1983 void preprocess_skip(void)
1985 int a
, start_of_line
, c
;
2012 } else if (c
== '\\') {
2013 /* XXX: incorrect: should not give an error */
2014 ch
= file
->buf_ptr
[0];
2022 p
= parse_pp_string(p
, c
, NULL
);
2031 p
= parse_comment(p
);
2032 } else if (ch
== '/') {
2033 p
= parse_line_comment(p
);
2039 if (start_of_line
) {
2044 (tok
== TOK_ELSE
|| tok
== TOK_ELIF
|| tok
== TOK_ENDIF
))
2046 if (tok
== TOK_IF
|| tok
== TOK_IFDEF
|| tok
== TOK_IFNDEF
)
2048 else if (tok
== TOK_ENDIF
)
2062 /* ParseState handling */
2064 /* XXX: currently, no include file info is stored. Thus, we cannot display
2065 accurate messages if the function or data definition spans multiple
2068 /* save current parse state in 's' */
2069 void save_parse_state(ParseState
*s
)
2071 s
->line_num
= file
->line_num
;
2072 s
->macro_ptr
= macro_ptr
;
2077 /* restore parse state from 's' */
2078 void restore_parse_state(ParseState
*s
)
2080 file
->line_num
= s
->line_num
;
2081 macro_ptr
= s
->macro_ptr
;
2086 /* return the number of additional 'ints' necessary to store the
2088 static inline int tok_ext_size(int t
)
2107 return LDOUBLE_SIZE
/ 4;
2113 /* token string handling */
2115 static inline void tok_str_new(TokenString
*s
)
2119 s
->allocated_len
= 0;
2120 s
->last_line_num
= -1;
2123 static void tok_str_free(int *str
)
2132 /* NOTE: we test zero separately so that GCC can generate a
2133 table for the following switch */
2148 /* XXX: use a macro to be portable on 64 bit ? */
2149 cstr
= (CString
*)p
[1];
2160 p
+= 1 + (LDOUBLE_SIZE
/ 4);
2170 static int *tok_str_realloc(TokenString
*s
)
2174 len
= s
->allocated_len
+ TOK_STR_ALLOC_INCR
;
2175 str
= tcc_realloc(s
->str
, len
* sizeof(int));
2177 error("memory full");
2178 s
->allocated_len
= len
;
2183 static void tok_str_add(TokenString
*s
, int t
)
2189 if (len
>= s
->allocated_len
)
2190 str
= tok_str_realloc(s
);
2195 static void tok_str_add2(TokenString
*s
, int t
, CValue
*cv
)
2202 /* allocate space for worst case */
2203 if (len
+ TOK_MAX_SIZE
> s
->allocated_len
)
2204 str
= tok_str_realloc(s
);
2213 str
[len
++] = cv
->tab
[0];
2218 str
[len
++] = (int)cstr_dup(cv
->cstr
);
2223 str
[len
++] = cv
->tab
[0];
2224 str
[len
++] = cv
->tab
[1];
2227 #if LDOUBLE_SIZE == 12
2228 str
[len
++] = cv
->tab
[0];
2229 str
[len
++] = cv
->tab
[1];
2230 str
[len
++] = cv
->tab
[2];
2232 #error add long double size support
2241 /* add the current parse token in token string 's' */
2242 static void tok_str_add_tok(TokenString
*s
)
2246 /* save line number info */
2247 if (file
->line_num
!= s
->last_line_num
) {
2248 s
->last_line_num
= file
->line_num
;
2249 cval
.i
= s
->last_line_num
;
2250 tok_str_add2(s
, TOK_LINENUM
, &cval
);
2252 tok_str_add2(s
, tok
, &tokc
);
2255 #if LDOUBLE_SIZE == 12
2256 #define LDOUBLE_GET(p, cv) \
2261 #error add long double size support
2265 /* get a token from an integer array and increment pointer
2266 accordingly. we code it as a macro to avoid pointer aliasing. */
2267 #define TOK_GET(t, p, cv) \
2289 case TOK_CLDOUBLE: \
2290 LDOUBLE_GET(p, cv); \
2291 p += LDOUBLE_SIZE / 4; \
2298 /* defines handling */
2299 static inline void define_push(int v
, int macro_type
, int *str
, Sym
*first_arg
)
2303 s
= sym_push2(&define_stack
, v
, macro_type
, (int)str
);
2304 s
->next
= first_arg
;
2305 table_ident
[v
- TOK_IDENT
]->sym_define
= s
;
2308 /* undefined a define symbol. Its name is just set to zero */
2309 static void define_undef(Sym
*s
)
2313 if (v
>= TOK_IDENT
&& v
< tok_ident
)
2314 table_ident
[v
- TOK_IDENT
]->sym_define
= NULL
;
2318 static inline Sym
*define_find(int v
)
2321 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
2323 return table_ident
[v
]->sym_define
;
2326 /* free define stack until top reaches 'b' */
2327 static void free_defines(Sym
*b
)
2335 /* do not free args or predefined defines */
2337 tok_str_free((int *)top
->c
);
2339 if (v
>= TOK_IDENT
&& v
< tok_ident
)
2340 table_ident
[v
- TOK_IDENT
]->sym_define
= NULL
;
2348 static Sym
*label_find(int v
)
2351 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
2353 return table_ident
[v
]->sym_label
;
2356 static Sym
*label_push(Sym
**ptop
, int v
, int flags
)
2359 s
= sym_push2(ptop
, v
, 0, 0);
2361 ps
= &table_ident
[v
- TOK_IDENT
]->sym_label
;
2362 if (ptop
== &global_label_stack
) {
2363 /* modify the top most local identifier, so that
2364 sym_identifier will point to 's' when popped */
2366 ps
= &(*ps
)->prev_tok
;
2373 /* pop labels until element last is reached. Look if any labels are
2374 undefined. Define symbols if '&&label' was used. */
2375 static void label_pop(Sym
**ptop
, Sym
*slast
)
2378 for(s
= *ptop
; s
!= slast
; s
= s1
) {
2380 if (s
->r
== LABEL_DECLARED
) {
2381 warning("label '%s' declared but not used", get_tok_str(s
->v
, NULL
));
2382 } else if (s
->r
== LABEL_FORWARD
) {
2383 error("label '%s' used but not defined",
2384 get_tok_str(s
->v
, NULL
));
2387 /* define corresponding symbol. A size of
2389 put_extern_sym(s
, cur_text_section
, (long)s
->next
, 1);
2393 table_ident
[s
->v
- TOK_IDENT
]->sym_label
= s
->prev_tok
;
2399 /* eval an expression for #if/#elif */
2400 static int expr_preprocess(void)
2406 while (tok
!= TOK_LINEFEED
&& tok
!= TOK_EOF
) {
2407 next(); /* do macro subst */
2408 if (tok
== TOK_DEFINED
) {
2413 c
= define_find(tok
) != 0;
2418 } else if (tok
>= TOK_IDENT
) {
2419 /* if undefined macro */
2423 tok_str_add_tok(&str
);
2425 tok_str_add(&str
, -1); /* simulate end of file */
2426 tok_str_add(&str
, 0);
2427 /* now evaluate C constant expression */
2428 macro_ptr
= str
.str
;
2432 tok_str_free(str
.str
);
2436 #if defined(PARSE_DEBUG) || defined(PP_DEBUG)
2437 static void tok_print(int *str
)
2443 TOK_GET(t
, str
, cval
);
2446 printf(" %s", get_tok_str(t
, &cval
));
2452 /* parse after #define */
2453 static void parse_define(void)
2455 Sym
*s
, *first
, **ps
;
2456 int v
, t
, varg
, is_vaargs
, c
;
2461 error("invalid macro name '%s'", get_tok_str(tok
, &tokc
));
2462 /* XXX: should check if same macro (ANSI) */
2465 /* '(' must be just after macro definition for MACRO_FUNC */
2466 c
= file
->buf_ptr
[0];
2468 c
= handle_stray1(file
->buf_ptr
);
2473 while (tok
!= ')') {
2477 if (varg
== TOK_DOTS
) {
2478 varg
= TOK___VA_ARGS__
;
2480 } else if (tok
== TOK_DOTS
&& gnu_ext
) {
2484 if (varg
< TOK_IDENT
)
2485 error("badly punctuated parameter list");
2486 s
= sym_push2(&define_stack
, varg
| SYM_FIELD
, is_vaargs
, 0);
2497 /* EOF testing necessary for '-D' handling */
2498 while (tok
!= TOK_LINEFEED
&& tok
!= TOK_EOF
) {
2499 tok_str_add2(&str
, tok
, &tokc
);
2502 tok_str_add(&str
, 0);
2504 printf("define %s %d: ", get_tok_str(v
, NULL
), t
);
2507 define_push(v
, t
, str
.str
, first
);
2510 /* XXX: use a token or a hash table to accelerate matching ? */
2511 static CachedInclude
*search_cached_include(TCCState
*s1
,
2512 int type
, const char *filename
)
2517 for(i
= 0;i
< s1
->nb_cached_includes
; i
++) {
2518 e
= s1
->cached_includes
[i
];
2519 if (e
->type
== type
&& !strcmp(e
->filename
, filename
))
2525 static inline void add_cached_include(TCCState
*s1
, int type
,
2526 const char *filename
, int ifndef_macro
)
2530 if (search_cached_include(s1
, type
, filename
))
2533 printf("adding cached '%s' %s\n", filename
, get_tok_str(ifndef_macro
, NULL
));
2535 e
= tcc_malloc(sizeof(CachedInclude
) + strlen(filename
));
2539 strcpy(e
->filename
, filename
);
2540 e
->ifndef_macro
= ifndef_macro
;
2541 dynarray_add((void ***)&s1
->cached_includes
, &s1
->nb_cached_includes
, e
);
2544 /* is_bof is true if first non space token at beginning of file */
2545 static void preprocess(int is_bof
)
2547 TCCState
*s1
= tcc_state
;
2548 int size
, i
, c
, n
, saved_parse_flags
;
2549 char buf
[1024], *q
, *p
;
2555 saved_parse_flags
= parse_flags
;
2556 parse_flags
= PARSE_FLAG_PREPROCESS
| PARSE_FLAG_TOK_NUM
|
2557 PARSE_FLAG_LINEFEED
;
2567 s
= define_find(tok
);
2568 /* undefine symbol by putting an invalid name */
2573 ch
= file
->buf_ptr
[0];
2574 /* XXX: incorrect if comments : use next_nomacro with a special mode */
2579 } else if (ch
== '\"') {
2582 /* XXX: better stray handling */
2585 while (ch
!= c
&& ch
!= '\n' && ch
!= CH_EOF
) {
2586 if ((q
- buf
) < sizeof(buf
) - 1)
2593 /* eat all spaces and comments after include */
2594 /* XXX: slightly incorrect */
2595 while (ch1
!= '\n' && ch1
!= CH_EOF
)
2599 /* computed #include : either we have only strings or
2600 we have anything enclosed in '<>' */
2603 if (tok
== TOK_STR
) {
2604 while (tok
!= TOK_LINEFEED
) {
2605 if (tok
!= TOK_STR
) {
2607 error("'#include' expects \"FILENAME\" or <FILENAME>");
2609 pstrcat(buf
, sizeof(buf
), (char *)tokc
.cstr
->data
);
2615 while (tok
!= TOK_LINEFEED
) {
2616 pstrcat(buf
, sizeof(buf
), get_tok_str(tok
, &tokc
));
2620 /* check syntax and remove '<>' */
2621 if (len
< 2 || buf
[0] != '<' || buf
[len
- 1] != '>')
2622 goto include_syntax
;
2623 memmove(buf
, buf
+ 1, len
- 2);
2624 buf
[len
- 2] = '\0';
2629 e
= search_cached_include(s1
, c
, buf
);
2630 if (e
&& define_find(e
->ifndef_macro
)) {
2631 /* no need to parse the include because the 'ifndef macro'
2634 printf("%s: skipping %s\n", file
->filename
, buf
);
2638 /* first search in current dir if "header.h" */
2640 p
= strrchr(file
->filename
, '/');
2642 size
= p
+ 1 - file
->filename
;
2643 if (size
> sizeof(buf1
) - 1)
2644 size
= sizeof(buf1
) - 1;
2645 memcpy(buf1
, file
->filename
, size
);
2647 pstrcat(buf1
, sizeof(buf1
), buf
);
2648 f
= tcc_open(s1
, buf1
);
2652 if (s1
->include_stack_ptr
>= s1
->include_stack
+ INCLUDE_STACK_SIZE
)
2653 error("#include recursion too deep");
2654 /* now search in all the include paths */
2655 n
= s1
->nb_include_paths
+ s1
->nb_sysinclude_paths
;
2656 for(i
= 0; i
< n
; i
++) {
2658 if (i
< s1
->nb_include_paths
)
2659 path
= s1
->include_paths
[i
];
2661 path
= s1
->sysinclude_paths
[i
- s1
->nb_include_paths
];
2662 pstrcpy(buf1
, sizeof(buf1
), path
);
2663 pstrcat(buf1
, sizeof(buf1
), "/");
2664 pstrcat(buf1
, sizeof(buf1
), buf
);
2665 f
= tcc_open(s1
, buf1
);
2669 error("include file '%s' not found", buf
);
2673 printf("%s: including %s\n", file
->filename
, buf1
);
2676 pstrcpy(f
->inc_filename
, sizeof(f
->inc_filename
), buf
);
2677 /* push current file in stack */
2678 /* XXX: fix current line init */
2679 *s1
->include_stack_ptr
++ = file
;
2681 /* add include file debug info */
2683 put_stabs(file
->filename
, N_BINCL
, 0, 0, 0);
2685 tok_flags
|= TOK_FLAG_BOF
| TOK_FLAG_BOL
;
2686 ch
= file
->buf_ptr
[0];
2694 c
= expr_preprocess();
2700 if (tok
< TOK_IDENT
)
2701 error("invalid argument for '#if%sdef'", c
? "n" : "");
2705 printf("#ifndef %s\n", get_tok_str(tok
, NULL
));
2707 file
->ifndef_macro
= tok
;
2710 c
= (define_find(tok
) != 0) ^ c
;
2712 if (s1
->ifdef_stack_ptr
>= s1
->ifdef_stack
+ IFDEF_STACK_SIZE
)
2713 error("memory full");
2714 *s1
->ifdef_stack_ptr
++ = c
;
2717 if (s1
->ifdef_stack_ptr
== s1
->ifdef_stack
)
2718 error("#else without matching #if");
2719 if (s1
->ifdef_stack_ptr
[-1] & 2)
2720 error("#else after #else");
2721 c
= (s1
->ifdef_stack_ptr
[-1] ^= 3);
2724 if (s1
->ifdef_stack_ptr
== s1
->ifdef_stack
)
2725 error("#elif without matching #if");
2726 c
= s1
->ifdef_stack_ptr
[-1];
2728 error("#elif after #else");
2729 /* last #if/#elif expression was true: we skip */
2732 c
= expr_preprocess();
2733 s1
->ifdef_stack_ptr
[-1] = c
;
2743 if (s1
->ifdef_stack_ptr
<= file
->ifdef_stack_ptr
)
2744 error("#endif without matching #if");
2745 s1
->ifdef_stack_ptr
--;
2746 /* '#ifndef macro' was at the start of file. Now we check if
2747 an '#endif' is exactly at the end of file */
2748 if (file
->ifndef_macro
&&
2749 s1
->ifdef_stack_ptr
== file
->ifdef_stack_ptr
) {
2750 file
->ifndef_macro_saved
= file
->ifndef_macro
;
2751 /* need to set to zero to avoid false matches if another
2752 #ifndef at middle of file */
2753 file
->ifndef_macro
= 0;
2754 while (tok
!= TOK_LINEFEED
)
2756 tok_flags
|= TOK_FLAG_ENDIF
;
2762 if (tok
!= TOK_CINT
)
2764 file
->line_num
= tokc
.i
- 1; /* the line number will be incremented after */
2766 if (tok
!= TOK_LINEFEED
) {
2769 pstrcpy(file
->filename
, sizeof(file
->filename
),
2770 (char *)tokc
.cstr
->data
);
2776 ch
= file
->buf_ptr
[0];
2779 while (ch
!= '\n' && ch
!= CH_EOF
) {
2780 if ((q
- buf
) < sizeof(buf
) - 1)
2786 error("#error %s", buf
);
2788 warning("#warning %s", buf
);
2794 if (tok
== TOK_LINEFEED
|| tok
== '!' || tok
== TOK_CINT
) {
2795 /* '!' is ignored to allow C scripts. numbers are ignored
2796 to emulate cpp behaviour */
2798 error("invalid preprocessing directive #%s", get_tok_str(tok
, &tokc
));
2802 /* ignore other preprocess commands or #! for C scripts */
2803 while (tok
!= TOK_LINEFEED
)
2806 parse_flags
= saved_parse_flags
;
2809 /* evaluate escape codes in a string. */
2810 static void parse_escape_string(CString
*outstr
, const uint8_t *buf
, int is_long
)
2825 case '0': case '1': case '2': case '3':
2826 case '4': case '5': case '6': case '7':
2827 /* at most three octal digits */
2832 n
= n
* 8 + c
- '0';
2836 n
= n
* 8 + c
- '0';
2841 goto add_char_nonext
;
2847 if (c
>= 'a' && c
<= 'f')
2849 else if (c
>= 'A' && c
<= 'F')
2859 goto add_char_nonext
;
2883 goto invalid_escape
;
2893 if (c
>= '!' && c
<= '~')
2894 warning("unknown escape sequence: \'\\%c\'", c
);
2896 warning("unknown escape sequence: \'\\x%x\'", c
);
2903 cstr_ccat(outstr
, c
);
2905 cstr_wccat(outstr
, c
);
2907 /* add a trailing '\0' */
2909 cstr_ccat(outstr
, '\0');
2911 cstr_wccat(outstr
, '\0');
2914 /* we use 64 bit numbers */
2917 /* bn = (bn << shift) | or_val */
2918 void bn_lshift(unsigned int *bn
, int shift
, int or_val
)
2922 for(i
=0;i
<BN_SIZE
;i
++) {
2924 bn
[i
] = (v
<< shift
) | or_val
;
2925 or_val
= v
>> (32 - shift
);
2929 void bn_zero(unsigned int *bn
)
2932 for(i
=0;i
<BN_SIZE
;i
++) {
2937 /* parse number in null terminated string 'p' and return it in the
2939 void parse_number(const char *p
)
2941 int b
, t
, shift
, frac_bits
, s
, exp_val
, ch
;
2943 unsigned int bn
[BN_SIZE
];
2954 goto float_frac_parse
;
2955 } else if (t
== '0') {
2956 if (ch
== 'x' || ch
== 'X') {
2960 } else if (tcc_ext
&& (ch
== 'b' || ch
== 'B')) {
2966 /* parse all digits. cannot check octal numbers at this stage
2967 because of floating point constants */
2969 if (ch
>= 'a' && ch
<= 'f')
2971 else if (ch
>= 'A' && ch
<= 'F')
2979 if (q
>= token_buf
+ STRING_MAX_SIZE
) {
2981 error("number too long");
2987 ((ch
== 'e' || ch
== 'E') && b
== 10) ||
2988 ((ch
== 'p' || ch
== 'P') && (b
== 16 || b
== 2))) {
2990 /* NOTE: strtox should support that for hexa numbers, but
2991 non ISOC99 libcs do not support it, so we prefer to do
2993 /* hexadecimal or binary floats */
2994 /* XXX: handle overflows */
3006 } else if (t
>= 'a') {
3008 } else if (t
>= 'A') {
3013 bn_lshift(bn
, shift
, t
);
3020 if (t
>= 'a' && t
<= 'f') {
3022 } else if (t
>= 'A' && t
<= 'F') {
3024 } else if (t
>= '0' && t
<= '9') {
3030 error("invalid digit");
3031 bn_lshift(bn
, shift
, t
);
3036 if (ch
!= 'p' && ch
!= 'P')
3043 } else if (ch
== '-') {
3047 if (ch
< '0' || ch
> '9')
3048 expect("exponent digits");
3049 while (ch
>= '0' && ch
<= '9') {
3050 exp_val
= exp_val
* 10 + ch
- '0';
3053 exp_val
= exp_val
* s
;
3055 /* now we can generate the number */
3056 /* XXX: should patch directly float number */
3057 d
= (double)bn
[1] * 4294967296.0 + (double)bn
[0];
3058 d
= ldexp(d
, exp_val
- frac_bits
);
3063 /* float : should handle overflow */
3065 } else if (t
== 'L') {
3068 /* XXX: not large enough */
3069 tokc
.ld
= (long double)d
;
3075 /* decimal floats */
3077 if (q
>= token_buf
+ STRING_MAX_SIZE
)
3082 while (ch
>= '0' && ch
<= '9') {
3083 if (q
>= token_buf
+ STRING_MAX_SIZE
)
3089 if (ch
== 'e' || ch
== 'E') {
3090 if (q
>= token_buf
+ STRING_MAX_SIZE
)
3094 if (ch
== '-' || ch
== '+') {
3095 if (q
>= token_buf
+ STRING_MAX_SIZE
)
3100 if (ch
< '0' || ch
> '9')
3101 expect("exponent digits");
3102 while (ch
>= '0' && ch
<= '9') {
3103 if (q
>= token_buf
+ STRING_MAX_SIZE
)
3115 tokc
.f
= strtof(token_buf
, NULL
);
3116 } else if (t
== 'L') {
3119 tokc
.ld
= strtold(token_buf
, NULL
);
3122 tokc
.d
= strtod(token_buf
, NULL
);
3126 unsigned long long n
, n1
;
3129 /* integer number */
3132 if (b
== 10 && *q
== '0') {
3139 /* no need for checks except for base 10 / 8 errors */
3142 } else if (t
>= 'a') {
3144 } else if (t
>= 'A') {
3149 error("invalid digit");
3153 /* detect overflow */
3154 /* XXX: this test is not reliable */
3156 error("integer constant overflow");
3159 /* XXX: not exactly ANSI compliant */
3160 if ((n
& 0xffffffff00000000LL
) != 0) {
3165 } else if (n
> 0x7fffffff) {
3176 error("three 'l's in integer constant");
3179 if (tok
== TOK_CINT
)
3181 else if (tok
== TOK_CUINT
)
3185 } else if (t
== 'U') {
3187 error("two 'u's in integer constant");
3189 if (tok
== TOK_CINT
)
3191 else if (tok
== TOK_CLLONG
)
3198 if (tok
== TOK_CINT
|| tok
== TOK_CUINT
)
3206 #define PARSE2(c1, tok1, c2, tok2) \
3217 /* return next token without macro substitution */
3218 static inline void next_nomacro1(void)
3238 /* first look if it is in fact an end of buffer */
3239 if (p
>= file
->buf_end
) {
3243 if (p
>= file
->buf_end
)
3256 TCCState
*s1
= tcc_state
;
3257 if (parse_flags
& PARSE_FLAG_LINEFEED
) {
3259 } else if (s1
->include_stack_ptr
== s1
->include_stack
||
3260 !(parse_flags
& PARSE_FLAG_PREPROCESS
)) {
3261 /* no include left : end of file. */
3264 /* pop include file */
3266 /* test if previous '#endif' was after a #ifdef at
3268 if (tok_flags
& TOK_FLAG_ENDIF
) {
3270 printf("#endif %s\n", get_tok_str(file
->ifndef_macro_saved
, NULL
));
3272 add_cached_include(s1
, file
->inc_type
, file
->inc_filename
,
3273 file
->ifndef_macro_saved
);
3276 /* add end of include file debug info */
3278 put_stabd(N_EINCL
, 0, 0);
3280 /* pop include stack */
3282 s1
->include_stack_ptr
--;
3283 file
= *s1
->include_stack_ptr
;
3291 if (parse_flags
& PARSE_FLAG_LINEFEED
) {
3295 tok_flags
|= TOK_FLAG_BOL
;
3304 if ((tok_flags
& TOK_FLAG_BOL
) &&
3305 (parse_flags
& PARSE_FLAG_PREPROCESS
)) {
3307 preprocess(tok_flags
& TOK_FLAG_BOF
);
3313 tok
= TOK_TWOSHARPS
;
3320 case 'a': case 'b': case 'c': case 'd':
3321 case 'e': case 'f': case 'g': case 'h':
3322 case 'i': case 'j': case 'k': case 'l':
3323 case 'm': case 'n': case 'o': case 'p':
3324 case 'q': case 'r': case 's': case 't':
3325 case 'u': case 'v': case 'w': case 'x':
3327 case 'A': case 'B': case 'C': case 'D':
3328 case 'E': case 'F': case 'G': case 'H':
3329 case 'I': case 'J': case 'K':
3330 case 'M': case 'N': case 'O': case 'P':
3331 case 'Q': case 'R': case 'S': case 'T':
3332 case 'U': case 'V': case 'W': case 'X':
3338 h
= TOK_HASH_FUNC(h
, c
);
3342 if (!isidnum_table
[c
])
3344 h
= TOK_HASH_FUNC(h
, c
);
3351 /* fast case : no stray found, so we have the full token
3352 and we have already hashed it */
3354 h
&= (TOK_HASH_SIZE
- 1);
3355 pts
= &hash_ident
[h
];
3360 if (ts
->len
== len
&& !memcmp(ts
->str
, p1
, len
))
3362 pts
= &(ts
->hash_next
);
3364 ts
= tok_alloc_new(pts
, p1
, len
);
3368 cstr_reset(&tokcstr
);
3371 cstr_ccat(&tokcstr
, *p1
);
3377 while (isidnum_table
[c
]) {
3378 cstr_ccat(&tokcstr
, c
);
3381 ts
= tok_alloc(tokcstr
.data
, tokcstr
.size
);
3387 if (t
!= '\\' && t
!= '\'' && t
!= '\"') {
3389 goto parse_ident_fast
;
3392 if (c
== '\'' || c
== '\"') {
3396 cstr_reset(&tokcstr
);
3397 cstr_ccat(&tokcstr
, 'L');
3398 goto parse_ident_slow
;
3402 case '0': case '1': case '2': case '3':
3403 case '4': case '5': case '6': case '7':
3406 cstr_reset(&tokcstr
);
3407 /* after the first digit, accept digits, alpha, '.' or sign if
3408 prefixed by 'eEpP' */
3412 cstr_ccat(&tokcstr
, c
);
3414 if (!(isnum(c
) || isid(c
) || c
== '.' ||
3415 ((c
== '+' || c
== '-') &&
3416 (t
== 'e' || t
== 'E' || t
== 'p' || t
== 'P'))))
3419 /* We add a trailing '\0' to ease parsing */
3420 cstr_ccat(&tokcstr
, '\0');
3421 tokc
.cstr
= &tokcstr
;
3425 /* special dot handling because it can also start a number */
3428 cstr_reset(&tokcstr
);
3429 cstr_ccat(&tokcstr
, '.');
3431 } else if (c
== '.') {
3451 /* parse the string */
3453 p
= parse_pp_string(p
, sep
, &str
);
3454 cstr_ccat(&str
, '\0');
3456 /* eval the escape (should be done as TOK_PPNUM) */
3457 cstr_reset(&tokcstr
);
3458 parse_escape_string(&tokcstr
, str
.data
, is_long
);
3463 /* XXX: make it portable */
3467 char_size
= sizeof(int);
3468 if (tokcstr
.size
<= char_size
)
3469 error("empty character constant");
3470 if (tokcstr
.size
> 2 * char_size
)
3471 warning("multi-character character constant");
3473 tokc
.i
= *(int8_t *)tokcstr
.data
;
3476 tokc
.i
= *(int *)tokcstr
.data
;
3480 tokc
.cstr
= &tokcstr
;
3494 } else if (c
== '<') {
3512 } else if (c
== '>') {
3530 } else if (c
== '=') {
3543 } else if (c
== '=') {
3556 } else if (c
== '=') {
3569 } else if (c
== '=') {
3572 } else if (c
== '>') {
3580 PARSE2('!', '!', '=', TOK_NE
)
3581 PARSE2('=', '=', '=', TOK_EQ
)
3582 PARSE2('*', '*', '=', TOK_A_MUL
)
3583 PARSE2('%', '%', '=', TOK_A_MOD
)
3584 PARSE2('^', '^', '=', TOK_A_XOR
)
3586 /* comments or operator */
3590 p
= parse_comment(p
);
3592 } else if (c
== '/') {
3593 p
= parse_line_comment(p
);
3595 } else if (c
== '=') {
3615 case '$': /* only used in assembler */
3620 error("unrecognized character \\x%02x", c
);
3625 #if defined(PARSE_DEBUG)
3626 printf("token = %s\n", get_tok_str(tok
, &tokc
));
3630 /* return next token without macro substitution. Can read input from
3632 static void next_nomacro(void)
3638 TOK_GET(tok
, macro_ptr
, tokc
);
3639 if (tok
== TOK_LINENUM
) {
3640 file
->line_num
= tokc
.i
;
3649 /* substitute args in macro_str and return allocated string */
3650 static int *macro_arg_subst(Sym
**nested_list
, int *macro_str
, Sym
*args
)
3652 int *st
, last_tok
, t
, notfirst
;
3661 TOK_GET(t
, macro_str
, cval
);
3666 TOK_GET(t
, macro_str
, cval
);
3669 s
= sym_find2(args
, t
);
3676 cstr_ccat(&cstr
, ' ');
3677 TOK_GET(t
, st
, cval
);
3678 cstr_cat(&cstr
, get_tok_str(t
, &cval
));
3681 cstr_ccat(&cstr
, '\0');
3683 printf("stringize: %s\n", (char *)cstr
.data
);
3687 tok_str_add2(&str
, TOK_STR
, &cval
);
3690 tok_str_add2(&str
, t
, &cval
);
3692 } else if (t
>= TOK_IDENT
) {
3693 s
= sym_find2(args
, t
);
3696 /* if '##' is present before or after, no arg substitution */
3697 if (*macro_str
== TOK_TWOSHARPS
|| last_tok
== TOK_TWOSHARPS
) {
3698 /* special case for var arg macros : ## eats the
3699 ',' if empty VA_ARGS variable. */
3700 /* XXX: test of the ',' is not 100%
3701 reliable. should fix it to avoid security
3703 if (gnu_ext
&& s
->type
.t
&&
3704 last_tok
== TOK_TWOSHARPS
&&
3705 str
.len
>= 2 && str
.str
[str
.len
- 2] == ',') {
3707 /* suppress ',' '##' */
3710 /* suppress '##' and add variable */
3718 TOK_GET(t1
, st
, cval
);
3721 tok_str_add2(&str
, t1
, &cval
);
3725 /* NOTE: the stream cannot be read when macro
3726 substituing an argument */
3727 macro_subst(&str
, nested_list
, st
, 0);
3730 tok_str_add(&str
, t
);
3733 tok_str_add2(&str
, t
, &cval
);
3737 tok_str_add(&str
, 0);
3741 static char const ab_month_name
[12][4] =
3743 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
3744 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
3747 /* do macro substitution of current token with macro 's' and add
3748 result to (tok_str,tok_len). 'nested_list' is the list of all
3749 macros we got inside to avoid recursing. Return non zero if no
3750 substitution needs to be done */
3751 static int macro_subst_tok(TokenString
*tok_str
,
3752 Sym
**nested_list
, Sym
*s
, int can_read_stream
)
3754 Sym
*args
, *sa
, *sa1
;
3755 int mstr_allocated
, parlevel
, *mstr
, t
;
3761 /* if symbol is a macro, prepare substitution */
3763 /* special macros */
3764 if (tok
== TOK___LINE__
) {
3765 cval
.i
= file
->line_num
;
3766 tok_str_add2(tok_str
, TOK_CINT
, &cval
);
3767 } else if (tok
== TOK___FILE__
) {
3768 cstrval
= file
->filename
;
3770 tok_str_add2(tok_str
, TOK_STR
, &cval
);
3771 } else if (tok
== TOK___DATE__
|| tok
== TOK___TIME__
) {
3777 tm
= localtime(&ti
);
3778 if (tok
== TOK___DATE__
) {
3779 snprintf(buf
, sizeof(buf
), "%s %2d %d",
3780 ab_month_name
[tm
->tm_mon
], tm
->tm_mday
, tm
->tm_year
+ 1900);
3782 snprintf(buf
, sizeof(buf
), "%02d:%02d:%02d",
3783 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
3788 cstr_cat(&cstr
, cstrval
);
3789 cstr_ccat(&cstr
, '\0');
3791 tok_str_add2(tok_str
, TOK_STR
, &cval
);
3796 if (s
->type
.t
== MACRO_FUNC
) {
3797 /* NOTE: we do not use next_nomacro to avoid eating the
3798 next token. XXX: find better solution */
3801 if (t
== 0 && can_read_stream
) {
3802 /* end of macro stream: we must look at the token
3803 after in the file */
3809 /* XXX: incorrect with comments */
3810 ch
= file
->buf_ptr
[0];
3811 while (is_space(ch
) || ch
== '\n')
3815 if (t
!= '(') /* no macro subst */
3818 /* argument macro */
3823 /* NOTE: empty args are allowed, except if no args */
3825 /* handle '()' case */
3826 if (!args
&& !sa
&& tok
== ')')
3829 error("macro '%s' used with too many args",
3830 get_tok_str(s
->v
, 0));
3833 /* NOTE: non zero sa->t indicates VA_ARGS */
3834 while ((parlevel
> 0 ||
3836 (tok
!= ',' || sa
->type
.t
))) &&
3840 else if (tok
== ')')
3842 tok_str_add2(&str
, tok
, &tokc
);
3845 tok_str_add(&str
, 0);
3846 sym_push2(&args
, sa
->v
& ~SYM_FIELD
, sa
->type
.t
, (int)str
.str
);
3849 /* special case for gcc var args: add an empty
3850 var arg argument if it is omitted */
3851 if (sa
&& sa
->type
.t
&& gnu_ext
)
3861 error("macro '%s' used with too few args",
3862 get_tok_str(s
->v
, 0));
3865 /* now subst each arg */
3866 mstr
= macro_arg_subst(nested_list
, mstr
, args
);
3871 tok_str_free((int *)sa
->c
);
3877 sym_push2(nested_list
, s
->v
, 0, 0);
3878 macro_subst(tok_str
, nested_list
, mstr
, 1);
3879 /* pop nested defined symbol */
3881 *nested_list
= sa1
->prev
;
3889 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
3890 return the resulting string (which must be freed). */
3891 static inline int *macro_twosharps(const int *macro_str
)
3894 const int *macro_ptr1
, *start_macro_ptr
, *ptr
, *saved_macro_ptr
;
3896 const char *p1
, *p2
;
3898 TokenString macro_str1
;
3901 start_macro_ptr
= macro_str
;
3902 /* we search the first '##' */
3904 macro_ptr1
= macro_str
;
3905 TOK_GET(t
, macro_str
, cval
);
3906 /* nothing more to do if end of string */
3909 if (*macro_str
== TOK_TWOSHARPS
)
3913 /* we saw '##', so we need more processing to handle it */
3915 tok_str_new(¯o_str1
);
3919 /* add all tokens seen so far */
3920 for(ptr
= start_macro_ptr
; ptr
< macro_ptr1
;) {
3921 TOK_GET(t
, ptr
, cval
);
3922 tok_str_add2(¯o_str1
, t
, &cval
);
3924 saved_macro_ptr
= macro_ptr
;
3925 /* XXX: get rid of the use of macro_ptr here */
3926 macro_ptr
= (int *)macro_str
;
3928 while (*macro_ptr
== TOK_TWOSHARPS
) {
3930 macro_ptr1
= macro_ptr
;
3933 TOK_GET(t
, macro_ptr
, cval
);
3934 /* We concatenate the two tokens if we have an
3935 identifier or a preprocessing number */
3937 p1
= get_tok_str(tok
, &tokc
);
3938 cstr_cat(&cstr
, p1
);
3939 p2
= get_tok_str(t
, &cval
);
3940 cstr_cat(&cstr
, p2
);
3941 cstr_ccat(&cstr
, '\0');
3943 if ((tok
>= TOK_IDENT
|| tok
== TOK_PPNUM
) &&
3944 (t
>= TOK_IDENT
|| t
== TOK_PPNUM
)) {
3945 if (tok
== TOK_PPNUM
) {
3946 /* if number, then create a number token */
3947 /* NOTE: no need to allocate because
3948 tok_str_add2() does it */
3951 /* if identifier, we must do a test to
3952 validate we have a correct identifier */
3953 if (t
== TOK_PPNUM
) {
3963 if (!isnum(c
) && !isid(c
))
3967 ts
= tok_alloc(cstr
.data
, strlen(cstr
.data
));
3968 tok
= ts
->tok
; /* modify current token */
3971 const char *str
= cstr
.data
;
3972 const unsigned char *q
;
3974 /* we look for a valid token */
3975 /* XXX: do more extensive checks */
3976 if (!strcmp(str
, ">>=")) {
3978 } else if (!strcmp(str
, "<<=")) {
3980 } else if (strlen(str
) == 2) {
3981 /* search in two bytes table */
3986 if (q
[0] == str
[0] && q
[1] == str
[1])
3993 /* NOTE: because get_tok_str use a static buffer,
3996 p1
= get_tok_str(tok
, &tokc
);
3997 cstr_cat(&cstr
, p1
);
3998 cstr_ccat(&cstr
, '\0');
3999 p2
= get_tok_str(t
, &cval
);
4000 warning("pasting \"%s\" and \"%s\" does not give a valid preprocessing token", cstr
.data
, p2
);
4001 /* cannot merge tokens: just add them separately */
4002 tok_str_add2(¯o_str1
, tok
, &tokc
);
4003 /* XXX: free associated memory ? */
4010 tok_str_add2(¯o_str1
, tok
, &tokc
);
4015 macro_ptr
= (int *)saved_macro_ptr
;
4017 tok_str_add(¯o_str1
, 0);
4018 return macro_str1
.str
;
4022 /* do macro substitution of macro_str and add result to
4023 (tok_str,tok_len). 'nested_list' is the list of all macros we got
4024 inside to avoid recursing. */
4025 static void macro_subst(TokenString
*tok_str
, Sym
**nested_list
,
4026 const int *macro_str
, int can_read_stream
)
4029 int *saved_macro_ptr
, *macro_str1
;
4034 /* first scan for '##' operator handling */
4036 macro_str1
= macro_twosharps(ptr
);
4040 /* NOTE: ptr == NULL can only happen if tokens are read from
4041 file stream due to a macro function call */
4044 TOK_GET(t
, ptr
, cval
);
4049 /* if nested substitution, do nothing */
4050 if (sym_find2(*nested_list
, t
))
4052 saved_macro_ptr
= macro_ptr
;
4053 macro_ptr
= (int *)ptr
;
4055 ret
= macro_subst_tok(tok_str
, nested_list
, s
, can_read_stream
);
4056 ptr
= (int *)macro_ptr
;
4057 macro_ptr
= saved_macro_ptr
;
4062 tok_str_add2(tok_str
, t
, &cval
);
4066 tok_str_free(macro_str1
);
4069 /* return next token with macro substitution */
4070 static void next(void)
4072 Sym
*nested_list
, *s
;
4078 /* if not reading from macro substituted string, then try
4079 to substitute macros */
4080 if (tok
>= TOK_IDENT
&&
4081 (parse_flags
& PARSE_FLAG_PREPROCESS
)) {
4082 s
= define_find(tok
);
4084 /* we have a macro: we try to substitute */
4087 if (macro_subst_tok(&str
, &nested_list
, s
, 1) == 0) {
4088 /* substitution done, NOTE: maybe empty */
4089 tok_str_add(&str
, 0);
4090 macro_ptr
= str
.str
;
4091 macro_ptr_allocated
= str
.str
;
4098 /* end of macro or end of unget buffer */
4099 if (unget_buffer_enabled
) {
4100 macro_ptr
= unget_saved_macro_ptr
;
4101 unget_buffer_enabled
= 0;
4103 /* end of macro string: free it */
4104 tok_str_free(macro_ptr_allocated
);
4111 /* convert preprocessor tokens into C tokens */
4112 if (tok
== TOK_PPNUM
&&
4113 (parse_flags
& PARSE_FLAG_TOK_NUM
)) {
4114 parse_number((char *)tokc
.cstr
->data
);
4118 /* push back current token and set current token to 'last_tok'. Only
4119 identifier case handled for labels. */
4120 static inline void unget_tok(int last_tok
)
4124 unget_saved_macro_ptr
= macro_ptr
;
4125 unget_buffer_enabled
= 1;
4126 q
= unget_saved_buffer
;
4129 n
= tok_ext_size(tok
) - 1;
4132 *q
= 0; /* end of token string */
4137 void swap(int *p
, int *q
)
4145 void vsetc(CType
*type
, int r
, CValue
*vc
)
4149 if (vtop
>= vstack
+ VSTACK_SIZE
)
4150 error("memory full");
4151 /* cannot let cpu flags if other instruction are generated. Also
4152 avoid leaving VT_JMP anywhere except on the top of the stack
4153 because it would complicate the code generator. */
4154 if (vtop
>= vstack
) {
4155 v
= vtop
->r
& VT_VALMASK
;
4156 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
4162 vtop
->r2
= VT_CONST
;
4166 /* push integer constant */
4171 vsetc(&int_type
, VT_CONST
, &cval
);
4174 /* Return a static symbol pointing to a section */
4175 static Sym
*get_sym_ref(CType
*type
, Section
*sec
,
4176 unsigned long offset
, unsigned long size
)
4182 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
4183 sym
->type
.ref
= type
->ref
;
4184 sym
->r
= VT_CONST
| VT_SYM
;
4185 put_extern_sym(sym
, sec
, offset
, size
);
4189 /* push a reference to a section offset by adding a dummy symbol */
4190 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
4195 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
4196 vtop
->sym
= get_sym_ref(type
, sec
, offset
, size
);
4199 /* define a new external reference to a symbol 'v' of type 'u' */
4200 static Sym
*external_global_sym(int v
, CType
*type
, int r
)
4206 /* push forward reference */
4207 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
4208 s
->type
.ref
= type
->ref
;
4209 s
->r
= r
| VT_CONST
| VT_SYM
;
4214 /* define a new external reference to a symbol 'v' of type 'u' */
4215 static Sym
*external_sym(int v
, CType
*type
, int r
)
4221 /* push forward reference */
4222 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
4223 s
->type
.t
|= VT_EXTERN
;
4225 if (!is_compatible_types(&s
->type
, type
))
4226 error("incompatible types for redefinition of '%s'",
4227 get_tok_str(v
, NULL
));
4232 /* push a reference to global symbol v */
4233 static void vpush_global_sym(CType
*type
, int v
)
4238 sym
= external_global_sym(v
, type
, 0);
4240 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
4244 void vset(CType
*type
, int r
, int v
)
4249 vsetc(type
, r
, &cval
);
4252 void vseti(int r
, int v
)
4268 void vpushv(SValue
*v
)
4270 if (vtop
>= vstack
+ VSTACK_SIZE
)
4271 error("memory full");
4281 /* save r to the memory stack, and mark it as being free */
4282 void save_reg(int r
)
4284 int l
, saved
, size
, align
;
4288 /* modify all stack values */
4291 for(p
=vstack
;p
<=vtop
;p
++) {
4292 if ((p
->r
& VT_VALMASK
) == r
||
4293 (p
->r2
& VT_VALMASK
) == r
) {
4294 /* must save value on stack if not already done */
4296 /* NOTE: must reload 'r' because r might be equal to r2 */
4297 r
= p
->r
& VT_VALMASK
;
4298 /* store register in the stack */
4300 if ((p
->r
& VT_LVAL
) ||
4301 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
4303 size
= type_size(type
, &align
);
4304 loc
= (loc
- size
) & -align
;
4305 sv
.type
.t
= type
->t
;
4306 sv
.r
= VT_LOCAL
| VT_LVAL
;
4309 #ifdef TCC_TARGET_I386
4310 /* x86 specific: need to pop fp register ST0 if saved */
4311 if (r
== TREG_ST0
) {
4312 o(0xd9dd); /* fstp %st(1) */
4315 /* special long long case */
4316 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
4323 /* mark that stack entry as being saved on the stack */
4324 if (p
->r
& VT_LVAL
) {
4325 /* also clear the bounded flag because the
4326 relocation address of the function was stored in
4328 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
4330 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
4338 /* find a free register of class 'rc'. If none, save one register */
4344 /* find a free register */
4345 for(r
=0;r
<NB_REGS
;r
++) {
4346 if (reg_classes
[r
] & rc
) {
4347 for(p
=vstack
;p
<=vtop
;p
++) {
4348 if ((p
->r
& VT_VALMASK
) == r
||
4349 (p
->r2
& VT_VALMASK
) == r
)
4357 /* no register left : free the first one on the stack (VERY
4358 IMPORTANT to start from the bottom to ensure that we don't
4359 spill registers used in gen_opi()) */
4360 for(p
=vstack
;p
<=vtop
;p
++) {
4361 r
= p
->r
& VT_VALMASK
;
4362 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
4364 /* also look at second register (if long long) */
4365 r
= p
->r2
& VT_VALMASK
;
4366 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
4372 /* Should never comes here */
4376 /* save registers up to (vtop - n) stack entry */
4377 void save_regs(int n
)
4382 for(p
= vstack
;p
<= p1
; p
++) {
4383 r
= p
->r
& VT_VALMASK
;
4390 /* move register 's' to 'r', and flush previous value of r to memory
4392 void move_reg(int r
, int s
)
4405 /* get address of vtop (vtop MUST BE an lvalue) */
4408 vtop
->r
&= ~VT_LVAL
;
4409 /* tricky: if saved lvalue, then we can go back to lvalue */
4410 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
4411 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
4414 #ifdef CONFIG_TCC_BCHECK
4415 /* generate lvalue bound code */
4421 vtop
->r
&= ~VT_MUSTBOUND
;
4422 /* if lvalue, then use checking code before dereferencing */
4423 if (vtop
->r
& VT_LVAL
) {
4424 /* if not VT_BOUNDED value, then make one */
4425 if (!(vtop
->r
& VT_BOUNDED
)) {
4426 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
4427 /* must save type because we must set it to int to get pointer */
4429 vtop
->type
.t
= VT_INT
;
4432 gen_bounded_ptr_add();
4433 vtop
->r
|= lval_type
;
4436 /* then check for dereferencing */
4437 gen_bounded_ptr_deref();
4442 /* store vtop a register belonging to class 'rc'. lvalues are
4443 converted to values. Cannot be used if cannot be converted to
4444 register value (such as structures). */
4447 int r
, r2
, rc2
, bit_pos
, bit_size
, size
, align
, i
;
4448 unsigned long long ll
;
4450 /* NOTE: get_reg can modify vstack[] */
4451 if (vtop
->type
.t
& VT_BITFIELD
) {
4452 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4453 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
4454 /* remove bit field info to avoid loops */
4455 vtop
->type
.t
&= ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
4456 /* generate shifts */
4457 vpushi(32 - (bit_pos
+ bit_size
));
4459 vpushi(32 - bit_size
);
4460 /* NOTE: transformed to SHR if unsigned */
4464 if (is_float(vtop
->type
.t
) &&
4465 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
4468 unsigned long offset
;
4470 /* XXX: unify with initializers handling ? */
4471 /* CPUs usually cannot use float constants, so we store them
4472 generically in data segment */
4473 size
= type_size(&vtop
->type
, &align
);
4474 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
4475 data_section
->data_offset
= offset
;
4476 /* XXX: not portable yet */
4477 ptr
= section_ptr_add(data_section
, size
);
4480 ptr
[i
] = vtop
->c
.tab
[i
];
4481 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
4482 vtop
->r
|= VT_LVAL
| VT_SYM
;
4486 #ifdef CONFIG_TCC_BCHECK
4487 if (vtop
->r
& VT_MUSTBOUND
)
4491 r
= vtop
->r
& VT_VALMASK
;
4492 /* need to reload if:
4494 - lvalue (need to dereference pointer)
4495 - already a register, but not in the right class */
4496 if (r
>= VT_CONST
||
4497 (vtop
->r
& VT_LVAL
) ||
4498 !(reg_classes
[r
] & rc
) ||
4499 ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&&
4500 !(reg_classes
[vtop
->r2
] & rc
))) {
4502 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
4503 /* two register type load : expand to two words
4505 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
4508 vtop
->c
.ui
= ll
; /* first word */
4510 vtop
->r
= r
; /* save register value */
4511 vpushi(ll
>> 32); /* second word */
4512 } else if (r
>= VT_CONST
||
4513 (vtop
->r
& VT_LVAL
)) {
4514 /* load from memory */
4517 vtop
[-1].r
= r
; /* save register value */
4518 /* increment pointer to get second word */
4519 vtop
->type
.t
= VT_INT
;
4525 /* move registers */
4528 vtop
[-1].r
= r
; /* save register value */
4529 vtop
->r
= vtop
[-1].r2
;
4531 /* allocate second register */
4538 /* write second register */
4540 } else if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
4542 /* lvalue of scalar type : need to use lvalue type
4543 because of possible cast */
4546 /* compute memory access type */
4547 if (vtop
->r
& VT_LVAL_BYTE
)
4549 else if (vtop
->r
& VT_LVAL_SHORT
)
4551 if (vtop
->r
& VT_LVAL_UNSIGNED
)
4555 /* restore wanted type */
4558 /* one register type load */
4567 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
4568 void gv2(int rc1
, int rc2
)
4572 /* generate more generic register first. But VT_JMP or VT_CMP
4573 values must be generated first in all cases to avoid possible
4575 v
= vtop
[0].r
& VT_VALMASK
;
4576 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
4581 /* test if reload is needed for first register */
4582 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
4592 /* test if reload is needed for first register */
4593 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
4599 /* expand long long on stack in two int registers */
4604 u
= vtop
->type
.t
& VT_UNSIGNED
;
4607 vtop
[0].r
= vtop
[-1].r2
;
4608 vtop
[0].r2
= VT_CONST
;
4609 vtop
[-1].r2
= VT_CONST
;
4610 vtop
[0].type
.t
= VT_INT
| u
;
4611 vtop
[-1].type
.t
= VT_INT
| u
;
4614 /* build a long long from two ints */
4617 gv2(RC_INT
, RC_INT
);
4618 vtop
[-1].r2
= vtop
[0].r
;
4619 vtop
[-1].type
.t
= t
;
4623 /* rotate n first stack elements to the bottom
4624 I1 ... In -> I2 ... In I1 [top is right]
4632 for(i
=-n
+1;i
!=0;i
++)
4633 vtop
[i
] = vtop
[i
+1];
4637 /* rotate n first stack elements to the top
4638 I1 ... In -> In I1 ... I(n-1) [top is right]
4646 for(i
= 0;i
< n
- 1; i
++)
4647 vtop
[-i
] = vtop
[-i
- 1];
4651 /* pop stack value */
4655 v
= vtop
->r
& VT_VALMASK
;
4656 #ifdef TCC_TARGET_I386
4657 /* for x86, we need to pop the FP stack */
4658 if (v
== TREG_ST0
&& !nocode_wanted
) {
4659 o(0xd9dd); /* fstp %st(1) */
4662 if (v
== VT_JMP
|| v
== VT_JMPI
) {
4663 /* need to put correct jump if && or || without test */
4669 /* convert stack entry to register and duplicate its value in another
4677 if ((t
& VT_BTYPE
) == VT_LLONG
) {
4684 /* stack: H L L1 H1 */
4692 /* duplicate value */
4703 load(r1
, &sv
); /* move r to r1 */
4705 /* duplicates value */
4710 /* generate CPU independent (unsigned) long long operations */
4711 void gen_opl(int op
)
4713 int t
, a
, b
, op1
, c
, i
;
4720 func
= TOK___divdi3
;
4723 func
= TOK___udivdi3
;
4726 func
= TOK___moddi3
;
4729 func
= TOK___umoddi3
;
4731 /* call generic long long function */
4732 vpush_global_sym(&func_old_type
, func
);
4737 vtop
->r2
= REG_LRET
;
4750 /* stack: L1 H1 L2 H2 */
4755 vtop
[-2] = vtop
[-3];
4758 /* stack: H1 H2 L1 L2 */
4764 /* stack: H1 H2 L1 L2 ML MH */
4767 /* stack: ML MH H1 H2 L1 L2 */
4771 /* stack: ML MH H1 L2 H2 L1 */
4776 /* stack: ML MH M1 M2 */
4779 } else if (op
== '+' || op
== '-') {
4780 /* XXX: add non carry method too (for MIPS or alpha) */
4786 /* stack: H1 H2 (L1 op L2) */
4789 gen_op(op1
+ 1); /* TOK_xxxC2 */
4792 /* stack: H1 H2 (L1 op L2) */
4795 /* stack: (L1 op L2) H1 H2 */
4797 /* stack: (L1 op L2) (H1 op H2) */
4805 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4806 t
= vtop
[-1].type
.t
;
4810 /* stack: L H shift */
4812 /* constant: simpler */
4813 /* NOTE: all comments are for SHL. the other cases are
4814 done by swaping words */
4825 if (op
!= TOK_SAR
) {
4858 /* XXX: should provide a faster fallback on x86 ? */
4861 func
= TOK___sardi3
;
4864 func
= TOK___shrdi3
;
4867 func
= TOK___shldi3
;
4873 /* compare operations */
4879 /* stack: L1 H1 L2 H2 */
4881 vtop
[-1] = vtop
[-2];
4883 /* stack: L1 L2 H1 H2 */
4886 /* when values are equal, we need to compare low words. since
4887 the jump is inverted, we invert the test too. */
4890 else if (op1
== TOK_GT
)
4892 else if (op1
== TOK_ULT
)
4894 else if (op1
== TOK_UGT
)
4899 if (op1
!= TOK_NE
) {
4903 /* generate non equal test */
4904 /* XXX: NOT PORTABLE yet */
4908 #ifdef TCC_TARGET_I386
4909 b
= psym(0x850f, 0);
4911 error("not implemented");
4915 /* compare low. Always unsigned */
4919 else if (op1
== TOK_LE
)
4921 else if (op1
== TOK_GT
)
4923 else if (op1
== TOK_GE
)
4933 /* handle integer constant optimizations and various machine
4935 void gen_opic(int op
)
4942 /* currently, we cannot do computations with forward symbols */
4943 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
4944 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
4948 case '+': v1
->c
.i
+= fc
; break;
4949 case '-': v1
->c
.i
-= fc
; break;
4950 case '&': v1
->c
.i
&= fc
; break;
4951 case '^': v1
->c
.i
^= fc
; break;
4952 case '|': v1
->c
.i
|= fc
; break;
4953 case '*': v1
->c
.i
*= fc
; break;
4960 /* if division by zero, generate explicit division */
4963 error("division by zero in constant");
4967 default: v1
->c
.i
/= fc
; break;
4968 case '%': v1
->c
.i
%= fc
; break;
4969 case TOK_UDIV
: v1
->c
.i
= (unsigned)v1
->c
.i
/ fc
; break;
4970 case TOK_UMOD
: v1
->c
.i
= (unsigned)v1
->c
.i
% fc
; break;
4973 case TOK_SHL
: v1
->c
.i
<<= fc
; break;
4974 case TOK_SHR
: v1
->c
.i
= (unsigned)v1
->c
.i
>> fc
; break;
4975 case TOK_SAR
: v1
->c
.i
>>= fc
; break;
4977 case TOK_ULT
: v1
->c
.i
= (unsigned)v1
->c
.i
< (unsigned)fc
; break;
4978 case TOK_UGE
: v1
->c
.i
= (unsigned)v1
->c
.i
>= (unsigned)fc
; break;
4979 case TOK_EQ
: v1
->c
.i
= v1
->c
.i
== fc
; break;
4980 case TOK_NE
: v1
->c
.i
= v1
->c
.i
!= fc
; break;
4981 case TOK_ULE
: v1
->c
.i
= (unsigned)v1
->c
.i
<= (unsigned)fc
; break;
4982 case TOK_UGT
: v1
->c
.i
= (unsigned)v1
->c
.i
> (unsigned)fc
; break;
4983 case TOK_LT
: v1
->c
.i
= v1
->c
.i
< fc
; break;
4984 case TOK_GE
: v1
->c
.i
= v1
->c
.i
>= fc
; break;
4985 case TOK_LE
: v1
->c
.i
= v1
->c
.i
<= fc
; break;
4986 case TOK_GT
: v1
->c
.i
= v1
->c
.i
> fc
; break;
4988 case TOK_LAND
: v1
->c
.i
= v1
->c
.i
&& fc
; break;
4989 case TOK_LOR
: v1
->c
.i
= v1
->c
.i
|| fc
; break;
4995 /* if commutative ops, put c2 as constant */
4996 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
4997 op
== '|' || op
== '*')) {
5002 if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
5005 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
5006 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
5012 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
5013 /* try to use shifts instead of muls or divs */
5014 if (fc
> 0 && (fc
& (fc
- 1)) == 0) {
5023 else if (op
== TOK_PDIV
)
5029 } else if (c2
&& (op
== '+' || op
== '-') &&
5030 (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) ==
5031 (VT_CONST
| VT_SYM
)) {
5032 /* symbol + constant case */
5039 if (!nocode_wanted
) {
5040 /* call low level op generator */
5049 /* generate a floating point operation with constant propagation */
5050 void gen_opif(int op
)
5058 /* currently, we cannot do computations with forward symbols */
5059 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
5060 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
5062 if (v1
->type
.t
== VT_FLOAT
) {
5065 } else if (v1
->type
.t
== VT_DOUBLE
) {
5073 /* NOTE: we only do constant propagation if finite number (not
5074 NaN or infinity) (ANSI spec) */
5075 if (!ieee_finite(f1
) || !ieee_finite(f2
))
5079 case '+': f1
+= f2
; break;
5080 case '-': f1
-= f2
; break;
5081 case '*': f1
*= f2
; break;
5085 error("division by zero in constant");
5090 /* XXX: also handles tests ? */
5094 /* XXX: overflow test ? */
5095 if (v1
->type
.t
== VT_FLOAT
) {
5097 } else if (v1
->type
.t
== VT_DOUBLE
) {
5105 if (!nocode_wanted
) {
5113 static int pointed_size(CType
*type
)
5116 return type_size(pointed_type(type
), &align
);
5119 static inline int is_null_pointer(SValue
*p
)
5121 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
5123 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& p
->c
.i
== 0) ||
5124 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.ll
== 0);
5127 static inline int is_integer_btype(int bt
)
5129 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
5130 bt
== VT_INT
|| bt
== VT_LLONG
);
5133 /* check types for comparison or substraction of pointers */
5134 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
5136 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
5139 /* null pointers are accepted for all comparisons as gcc */
5140 if (is_null_pointer(p1
) || is_null_pointer(p2
))
5144 bt1
= type1
->t
& VT_BTYPE
;
5145 bt2
= type2
->t
& VT_BTYPE
;
5146 /* accept comparison between pointer and integer with a warning */
5147 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
5148 warning("comparison between pointer and integer");
5152 /* both must be pointers or implicit function pointers */
5153 if (bt1
== VT_PTR
) {
5154 type1
= pointed_type(type1
);
5155 } else if (bt1
!= VT_FUNC
)
5156 goto invalid_operands
;
5158 if (bt2
== VT_PTR
) {
5159 type2
= pointed_type(type2
);
5160 } else if (bt2
!= VT_FUNC
) {
5162 error("invalid operands to binary %s", get_tok_str(op
, NULL
));
5164 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
5165 (type2
->t
& VT_BTYPE
) == VT_VOID
)
5169 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
5170 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
5171 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
5172 /* gcc-like error if '-' is used */
5174 goto invalid_operands
;
5176 warning("comparison of distinct pointer types lacks a cast");
5180 /* generic gen_op: handles types problems */
5183 int u
, t1
, t2
, bt1
, bt2
, t
;
5186 t1
= vtop
[-1].type
.t
;
5187 t2
= vtop
[0].type
.t
;
5188 bt1
= t1
& VT_BTYPE
;
5189 bt2
= t2
& VT_BTYPE
;
5191 if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
5192 /* at least one operand is a pointer */
5193 /* relationnal op: must be both pointers */
5194 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
5195 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
5196 /* pointers are handled are unsigned */
5197 t
= VT_INT
| VT_UNSIGNED
;
5200 /* if both pointers, then it must be the '-' op */
5201 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
5203 error("cannot use pointers here");
5204 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
5205 /* XXX: check that types are compatible */
5206 u
= pointed_size(&vtop
[-1].type
);
5208 /* set to integer type */
5209 vtop
->type
.t
= VT_INT
;
5213 /* exactly one pointer : must be '+' or '-'. */
5214 if (op
!= '-' && op
!= '+')
5215 error("cannot use pointers here");
5216 /* Put pointer as first operand */
5217 if (bt2
== VT_PTR
) {
5221 type1
= vtop
[-1].type
;
5222 /* XXX: cast to int ? (long long case) */
5223 vpushi(pointed_size(&vtop
[-1].type
));
5225 #ifdef CONFIG_TCC_BCHECK
5226 /* if evaluating constant expression, no code should be
5227 generated, so no bound check */
5228 if (do_bounds_check
&& !const_wanted
) {
5229 /* if bounded pointers, we generate a special code to
5236 gen_bounded_ptr_add();
5242 /* put again type if gen_opic() swaped operands */
5245 } else if (is_float(bt1
) || is_float(bt2
)) {
5246 /* compute bigger type and do implicit casts */
5247 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
5249 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
5254 /* floats can only be used for a few operations */
5255 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
5256 (op
< TOK_ULT
|| op
> TOK_GT
))
5257 error("invalid operands for binary operation");
5259 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
5260 /* cast to biggest op */
5262 /* convert to unsigned if it does not fit in a long long */
5263 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
5264 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
5268 /* integer operations */
5270 /* convert to unsigned if it does not fit in an integer */
5271 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
5272 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
5275 /* XXX: currently, some unsigned operations are explicit, so
5276 we modify them here */
5277 if (t
& VT_UNSIGNED
) {
5284 else if (op
== TOK_LT
)
5286 else if (op
== TOK_GT
)
5288 else if (op
== TOK_LE
)
5290 else if (op
== TOK_GE
)
5297 /* special case for shifts and long long: we keep the shift as
5299 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
5304 else if ((t
& VT_BTYPE
) == VT_LLONG
)
5308 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
5309 /* relationnal op: the result is an int */
5310 vtop
->type
.t
= VT_INT
;
5317 /* generic itof for unsigned long long case */
5318 void gen_cvt_itof1(int t
)
5320 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
5321 (VT_LLONG
| VT_UNSIGNED
)) {
5324 vpush_global_sym(&func_old_type
, TOK___ulltof
);
5325 else if (t
== VT_DOUBLE
)
5326 vpush_global_sym(&func_old_type
, TOK___ulltod
);
5328 vpush_global_sym(&func_old_type
, TOK___ulltold
);
5338 /* generic ftoi for unsigned long long case */
5339 void gen_cvt_ftoi1(int t
)
5343 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
5344 /* not handled natively */
5345 st
= vtop
->type
.t
& VT_BTYPE
;
5347 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
5348 else if (st
== VT_DOUBLE
)
5349 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
5351 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
5356 vtop
->r2
= REG_LRET
;
5362 /* force char or short cast */
5363 void force_charshort_cast(int t
)
5367 /* XXX: add optimization if lvalue : just change type and offset */
5372 if (t
& VT_UNSIGNED
) {
5373 vpushi((1 << bits
) - 1);
5384 /* cast 'vtop' to 'type' */
5385 static void gen_cast(CType
*type
)
5387 int sbt
, dbt
, sf
, df
, c
;
5389 /* special delayed cast for char/short */
5390 /* XXX: in some cases (multiple cascaded casts), it may still
5392 if (vtop
->r
& VT_MUSTCAST
) {
5393 vtop
->r
&= ~VT_MUSTCAST
;
5394 force_charshort_cast(vtop
->type
.t
);
5397 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
5398 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
5400 if (sbt
!= dbt
&& !nocode_wanted
) {
5403 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
5405 /* convert from fp to fp */
5407 /* constant case: we can do it now */
5408 /* XXX: in ISOC, cannot do it if error in convert */
5409 if (dbt
== VT_FLOAT
&& sbt
== VT_DOUBLE
)
5410 vtop
->c
.f
= (float)vtop
->c
.d
;
5411 else if (dbt
== VT_FLOAT
&& sbt
== VT_LDOUBLE
)
5412 vtop
->c
.f
= (float)vtop
->c
.ld
;
5413 else if (dbt
== VT_DOUBLE
&& sbt
== VT_FLOAT
)
5414 vtop
->c
.d
= (double)vtop
->c
.f
;
5415 else if (dbt
== VT_DOUBLE
&& sbt
== VT_LDOUBLE
)
5416 vtop
->c
.d
= (double)vtop
->c
.ld
;
5417 else if (dbt
== VT_LDOUBLE
&& sbt
== VT_FLOAT
)
5418 vtop
->c
.ld
= (long double)vtop
->c
.f
;
5419 else if (dbt
== VT_LDOUBLE
&& sbt
== VT_DOUBLE
)
5420 vtop
->c
.ld
= (long double)vtop
->c
.d
;
5422 /* non constant case: generate code */
5426 /* convert int to fp */
5429 case VT_LLONG
| VT_UNSIGNED
:
5431 /* XXX: add const cases for long long */
5433 case VT_INT
| VT_UNSIGNED
:
5435 case VT_FLOAT
: vtop
->c
.f
= (float)vtop
->c
.ui
; break;
5436 case VT_DOUBLE
: vtop
->c
.d
= (double)vtop
->c
.ui
; break;
5437 case VT_LDOUBLE
: vtop
->c
.ld
= (long double)vtop
->c
.ui
; break;
5442 case VT_FLOAT
: vtop
->c
.f
= (float)vtop
->c
.i
; break;
5443 case VT_DOUBLE
: vtop
->c
.d
= (double)vtop
->c
.i
; break;
5444 case VT_LDOUBLE
: vtop
->c
.ld
= (long double)vtop
->c
.i
; break;
5453 /* convert fp to int */
5454 /* we handle char/short/etc... with generic code */
5455 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
5456 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
5461 case VT_LLONG
| VT_UNSIGNED
:
5463 /* XXX: add const cases for long long */
5465 case VT_INT
| VT_UNSIGNED
:
5467 case VT_FLOAT
: vtop
->c
.ui
= (unsigned int)vtop
->c
.d
; break;
5468 case VT_DOUBLE
: vtop
->c
.ui
= (unsigned int)vtop
->c
.d
; break;
5469 case VT_LDOUBLE
: vtop
->c
.ui
= (unsigned int)vtop
->c
.d
; break;
5475 case VT_FLOAT
: vtop
->c
.i
= (int)vtop
->c
.d
; break;
5476 case VT_DOUBLE
: vtop
->c
.i
= (int)vtop
->c
.d
; break;
5477 case VT_LDOUBLE
: vtop
->c
.i
= (int)vtop
->c
.d
; break;
5485 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
5486 /* additional cast for char/short/bool... */
5490 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
5491 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
5492 /* scalar to long long */
5494 if (sbt
== (VT_INT
| VT_UNSIGNED
))
5495 vtop
->c
.ll
= vtop
->c
.ui
;
5497 vtop
->c
.ll
= vtop
->c
.i
;
5499 /* machine independent conversion */
5501 /* generate high word */
5502 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
5510 /* patch second register */
5511 vtop
[-1].r2
= vtop
->r
;
5515 } else if (dbt
== VT_BOOL
) {
5516 /* scalar to bool */
5519 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
5520 (dbt
& VT_BTYPE
) == VT_SHORT
) {
5521 force_charshort_cast(dbt
);
5522 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
5524 if (sbt
== VT_LLONG
) {
5525 /* from long long: just take low order word */
5529 /* if lvalue and single word type, nothing to do because
5530 the lvalue already contains the real type size (see
5531 VT_LVAL_xxx constants) */
5537 /* return type size. Put alignment at 'a' */
5538 static int type_size(CType
*type
, int *a
)
5543 bt
= type
->t
& VT_BTYPE
;
5544 if (bt
== VT_STRUCT
) {
5549 } else if (bt
== VT_PTR
) {
5550 if (type
->t
& VT_ARRAY
) {
5552 return type_size(&s
->type
, a
) * s
->c
;
5557 } else if (bt
== VT_LDOUBLE
) {
5559 return LDOUBLE_SIZE
;
5560 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
5561 *a
= 4; /* XXX: i386 specific */
5563 } else if (bt
== VT_INT
|| bt
== VT_ENUM
|| bt
== VT_FLOAT
) {
5566 } else if (bt
== VT_SHORT
) {
5570 /* char, void, function, _Bool */
5576 /* return the pointed type of t */
5577 static inline CType
*pointed_type(CType
*type
)
5579 return &type
->ref
->type
;
5582 /* modify type so that its it is a pointer to type. */
5583 static void mk_pointer(CType
*type
)
5586 s
= sym_push(SYM_FIELD
, type
, 0, -1);
5587 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
5591 /* compare function types. OLD functions match any new functions */
5592 static int is_compatible_func(CType
*type1
, CType
*type2
)
5598 if (!is_compatible_types(&s1
->type
, &s2
->type
))
5600 /* XXX: not complete */
5601 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
5605 while (s1
!= NULL
) {
5608 if (!is_compatible_types(&s1
->type
, &s2
->type
))
5618 /* return true if type1 and type2 are exactly the same (including
5621 - enums are not checked as gcc __builtin_types_compatible_p ()
5623 static int is_compatible_types(CType
*type1
, CType
*type2
)
5627 t1
= type1
->t
& VT_TYPE
;
5628 t2
= type2
->t
& VT_TYPE
;
5629 /* XXX: bitfields ? */
5632 /* test more complicated cases */
5633 bt1
= t1
& VT_BTYPE
;
5634 if (bt1
== VT_PTR
) {
5635 type1
= pointed_type(type1
);
5636 type2
= pointed_type(type2
);
5637 return is_compatible_types(type1
, type2
);
5638 } else if (bt1
== VT_STRUCT
) {
5639 return (type1
->ref
== type2
->ref
);
5640 } else if (bt1
== VT_FUNC
) {
5641 return is_compatible_func(type1
, type2
);
5647 /* print a type. If 'varstr' is not NULL, then the variable is also
5648 printed in the type */
5650 /* XXX: add array and function pointers */
5651 void type_to_str(char *buf
, int buf_size
,
5652 CType
*type
, const char *varstr
)
5659 t
= type
->t
& VT_TYPE
;
5662 if (t
& VT_CONSTANT
)
5663 pstrcat(buf
, buf_size
, "const ");
5664 if (t
& VT_VOLATILE
)
5665 pstrcat(buf
, buf_size
, "volatile ");
5666 if (t
& VT_UNSIGNED
)
5667 pstrcat(buf
, buf_size
, "unsigned ");
5697 tstr
= "long double";
5699 pstrcat(buf
, buf_size
, tstr
);
5703 if (bt
== VT_STRUCT
)
5707 pstrcat(buf
, buf_size
, tstr
);
5708 v
= type
->ref
->v
& ~SYM_STRUCT
;
5709 if (v
>= SYM_FIRST_ANOM
)
5710 pstrcat(buf
, buf_size
, "<anonymous>");
5712 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
5716 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
5717 pstrcat(buf
, buf_size
, "(");
5719 while (sa
!= NULL
) {
5720 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
5721 pstrcat(buf
, buf_size
, buf1
);
5724 pstrcat(buf
, buf_size
, ", ");
5726 pstrcat(buf
, buf_size
, ")");
5730 pstrcpy(buf1
, sizeof(buf1
), "*");
5732 pstrcat(buf1
, sizeof(buf1
), varstr
);
5733 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
5737 pstrcat(buf
, buf_size
, " ");
5738 pstrcat(buf
, buf_size
, varstr
);
5743 /* verify type compatibility to store vtop in 'dt' type, and generate
5745 static void gen_assign_cast(CType
*dt
)
5747 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
5748 char buf1
[256], buf2
[256];
5751 st
= &vtop
->type
; /* source type */
5752 dbt
= dt
->t
& VT_BTYPE
;
5753 sbt
= st
->t
& VT_BTYPE
;
5754 if (dt
->t
& VT_CONSTANT
)
5755 warning("assignment of read-only location");
5758 /* special cases for pointers */
5759 /* '0' can also be a pointer */
5760 if (is_null_pointer(vtop
))
5762 /* accept implicit pointer to integer cast with warning */
5763 if (is_integer_btype(sbt
)) {
5764 warning("assignment makes pointer from integer without a cast");
5767 type1
= pointed_type(dt
);
5768 /* a function is implicitely a function pointer */
5769 if (sbt
== VT_FUNC
) {
5770 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
5771 !is_compatible_types(pointed_type(dt
), st
))
5778 type2
= pointed_type(st
);
5779 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
5780 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
5781 /* void * can match anything */
5783 /* exact type match, except for unsigned */
5786 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
5787 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
5788 if (!is_compatible_types(&tmp_type1
, &tmp_type2
))
5791 /* check const and volatile */
5792 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
5793 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
5794 warning("assignment discards qualifiers from pointer target type");
5800 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
5801 warning("assignment makes integer from pointer without a cast");
5803 /* XXX: more tests */
5806 if (!is_compatible_types(dt
, st
)) {
5808 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
5809 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
5810 error("cannot cast '%s' to '%s'", buf1
, buf2
);
5818 /* store vtop in lvalue pushed on stack */
5821 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
5823 ft
= vtop
[-1].type
.t
;
5824 sbt
= vtop
->type
.t
& VT_BTYPE
;
5825 dbt
= ft
& VT_BTYPE
;
5826 if (((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
5827 (sbt
== VT_INT
&& dbt
== VT_SHORT
)) {
5828 /* optimize char/short casts */
5829 delayed_cast
= VT_MUSTCAST
;
5830 vtop
->type
.t
= ft
& VT_TYPE
;
5831 /* XXX: factorize */
5832 if (ft
& VT_CONSTANT
)
5833 warning("assignment of read-only location");
5836 gen_assign_cast(&vtop
[-1].type
);
5839 if (sbt
== VT_STRUCT
) {
5840 /* if structure, only generate pointer */
5841 /* structure assignment : generate memcpy */
5842 /* XXX: optimize if small size */
5843 if (!nocode_wanted
) {
5844 size
= type_size(&vtop
->type
, &align
);
5846 vpush_global_sym(&func_old_type
, TOK_memcpy
);
5850 vtop
->type
.t
= VT_INT
;
5854 vtop
->type
.t
= VT_INT
;
5866 /* leave source on stack */
5867 } else if (ft
& VT_BITFIELD
) {
5868 /* bitfield store handling */
5869 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
5870 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
5871 /* remove bit field info to avoid loops */
5872 vtop
[-1].type
.t
= ft
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
5874 /* duplicate destination */
5876 vtop
[-1] = vtop
[-2];
5878 /* mask and shift source */
5879 vpushi((1 << bit_size
) - 1);
5883 /* load destination, mask and or with source */
5885 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
5891 #ifdef CONFIG_TCC_BCHECK
5892 /* bound check case */
5893 if (vtop
[-1].r
& VT_MUSTBOUND
) {
5899 if (!nocode_wanted
) {
5903 r
= gv(rc
); /* generate value */
5904 /* if lvalue was saved on stack, must read it */
5905 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
5907 t
= get_reg(RC_INT
);
5909 sv
.r
= VT_LOCAL
| VT_LVAL
;
5910 sv
.c
.ul
= vtop
[-1].c
.ul
;
5912 vtop
[-1].r
= t
| VT_LVAL
;
5915 /* two word case handling : store second register at word + 4 */
5916 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
5918 /* convert to int to increment easily */
5919 vtop
->type
.t
= VT_INT
;
5925 /* XXX: it works because r2 is spilled last ! */
5926 store(vtop
->r2
, vtop
- 1);
5930 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
5931 vtop
->r
|= delayed_cast
;
5935 /* post defines POST/PRE add. c is the token ++ or -- */
5936 void inc(int post
, int c
)
5939 vdup(); /* save lvalue */
5941 gv_dup(); /* duplicate value */
5946 vpushi(c
- TOK_MID
);
5948 vstore(); /* store value */
5950 vpop(); /* if post op, return saved value */
5953 /* Parse GNUC __attribute__ extension. Currently, the following
5954 extensions are recognized:
5955 - aligned(n) : set data/function alignment.
5956 - section(x) : generate data/code in this section.
5957 - unused : currently ignored, but may be used someday.
5959 static void parse_attribute(AttributeDef
*ad
)
5963 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
5967 while (tok
!= ')') {
5968 if (tok
< TOK_IDENT
)
5969 expect("attribute name");
5977 expect("section name");
5978 ad
->section
= find_section(tcc_state
, (char *)tokc
.cstr
->data
);
5987 if (n
<= 0 || (n
& (n
- 1)) != 0)
5988 error("alignment must be a positive power of two");
5997 /* currently, no need to handle it because tcc does not
5998 track unused objects */
6002 /* currently, no need to handle it because tcc does not
6003 track unused objects */
6008 ad
->func_call
= FUNC_CDECL
;
6013 ad
->func_call
= FUNC_STDCALL
;
6016 if (tcc_state
->warn_unsupported
)
6017 warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
6018 /* skip parameters */
6019 /* XXX: skip parenthesis too */
6022 while (tok
!= ')' && tok
!= -1)
6037 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
6038 static void struct_decl(CType
*type
, int u
)
6040 int a
, v
, size
, align
, maxalign
, c
, offset
;
6041 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
;
6046 a
= tok
; /* save decl type */
6051 /* struct already defined ? return it */
6053 expect("struct/union/enum name");
6057 error("invalid type");
6064 /* we put an undefined size for struct/union */
6065 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
6066 s
->r
= 0; /* default alignment is zero as gcc */
6067 /* put struct/union/enum name in type */
6075 error("struct/union/enum already defined");
6076 /* cannot be empty */
6078 /* non empty enums are not allowed */
6079 if (a
== TOK_ENUM
) {
6083 expect("identifier");
6089 /* enum symbols have static storage */
6090 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
6091 ss
->type
.t
|= VT_STATIC
;
6096 /* NOTE: we accept a trailing comma */
6106 while (tok
!= '}') {
6107 parse_btype(&btype
, &ad
);
6113 type_decl(&type1
, &ad
, &v
, TYPE_DIRECT
);
6114 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
6115 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
6116 error("invalid type for '%s'",
6117 get_tok_str(v
, NULL
));
6121 bit_size
= expr_const();
6122 /* XXX: handle v = 0 case for messages */
6124 error("negative width in bit-field '%s'",
6125 get_tok_str(v
, NULL
));
6126 if (v
&& bit_size
== 0)
6127 error("zero width for bit-field '%s'",
6128 get_tok_str(v
, NULL
));
6130 size
= type_size(&type1
, &align
);
6132 if (bit_size
>= 0) {
6133 bt
= type1
.t
& VT_BTYPE
;
6138 error("bitfields must have scalar type");
6140 if (bit_size
> bsize
) {
6141 error("width of '%s' exceeds its type",
6142 get_tok_str(v
, NULL
));
6143 } else if (bit_size
== bsize
) {
6144 /* no need for bit fields */
6146 } else if (bit_size
== 0) {
6147 /* XXX: what to do if only padding in a
6149 /* zero size: means to pad */
6153 /* we do not have enough room ? */
6154 if ((bit_pos
+ bit_size
) > bsize
)
6157 /* XXX: handle LSB first */
6158 type1
.t
|= VT_BITFIELD
|
6159 (bit_pos
<< VT_STRUCT_SHIFT
) |
6160 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
6161 bit_pos
+= bit_size
;
6167 /* add new memory data only if starting
6169 if (lbit_pos
== 0) {
6170 if (a
== TOK_STRUCT
) {
6171 c
= (c
+ align
- 1) & -align
;
6179 if (align
> maxalign
)
6183 printf("add field %s offset=%d",
6184 get_tok_str(v
, NULL
), offset
);
6185 if (type1
.t
& VT_BITFIELD
) {
6186 printf(" pos=%d size=%d",
6187 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
6188 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
6192 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
6196 if (tok
== ';' || tok
== TOK_EOF
)
6203 /* store size and alignment */
6204 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
6210 /* return 0 if no type declaration. otherwise, return the basic type
6213 static int parse_btype(CType
*type
, AttributeDef
*ad
)
6215 int t
, u
, type_found
, typespec_found
;
6219 memset(ad
, 0, sizeof(AttributeDef
));
6226 /* currently, we really ignore extension */
6236 if ((t
& VT_BTYPE
) != 0)
6237 error("too many basic types");
6253 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
6254 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
6255 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
6256 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
6270 if ((t
& VT_BTYPE
) == VT_LONG
) {
6271 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
6278 struct_decl(&type1
, VT_ENUM
);
6281 type
->ref
= type1
.ref
;
6285 struct_decl(&type1
, VT_STRUCT
);
6288 /* type modifiers */
6338 /* GNUC attribute */
6339 case TOK_ATTRIBUTE1
:
6340 case TOK_ATTRIBUTE2
:
6341 parse_attribute(ad
);
6348 parse_expr_type(&type1
);
6354 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
6356 t
|= (s
->type
.t
& ~VT_TYPEDEF
);
6357 type
->ref
= s
->type
.ref
;
6364 /* long is never used as type */
6365 if ((t
& VT_BTYPE
) == VT_LONG
)
6366 t
= (t
& ~VT_BTYPE
) | VT_INT
;
6371 /* convert a function parameter type (array to pointer and function to
6372 function pointer) */
6373 static inline void convert_parameter_type(CType
*pt
)
6375 /* array must be transformed to pointer according to ANSI C */
6377 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
6382 static void post_type(CType
*type
, AttributeDef
*ad
)
6385 Sym
**plast
, *s
, *first
;
6390 /* function declaration */
6395 while (tok
!= ')') {
6396 /* read param name and compute offset */
6397 if (l
!= FUNC_OLD
) {
6398 if (!parse_btype(&pt
, &ad1
)) {
6400 error("invalid type");
6407 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
6409 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
6410 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
6411 error("parameter declared as void");
6418 convert_parameter_type(&pt
);
6419 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
6424 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
6431 /* if no parameters, then old type prototype */
6435 t1
= type
->t
& VT_STORAGE
;
6436 /* NOTE: const is ignored in returned type as it has a special
6437 meaning in gcc / C++ */
6438 type
->t
&= ~(VT_STORAGE
| VT_CONSTANT
);
6439 post_type(type
, ad
);
6440 /* we push a anonymous symbol which will contain the function prototype */
6441 s
= sym_push(SYM_FIELD
, type
, ad
->func_call
, l
);
6443 type
->t
= t1
| VT_FUNC
;
6445 } else if (tok
== '[') {
6446 /* array definition */
6452 error("invalid array size");
6455 /* parse next post type */
6456 t1
= type
->t
& VT_STORAGE
;
6457 type
->t
&= ~VT_STORAGE
;
6458 post_type(type
, ad
);
6460 /* we push a anonymous symbol which will contain the array
6462 s
= sym_push(SYM_FIELD
, type
, 0, n
);
6463 type
->t
= t1
| VT_ARRAY
| VT_PTR
;
6468 /* Parse a type declaration (except basic type), and return the type
6469 in 'type'. 'td' is a bitmask indicating which kind of type decl is
6470 expected. 'type' should contain the basic type. 'ad' is the
6471 attribute definition of the basic type. It can be modified by
6474 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
6477 CType type1
, *type2
;
6480 while (tok
== '*') {
6488 qualifiers
|= VT_CONSTANT
;
6493 qualifiers
|= VT_VOLATILE
;
6501 type
->t
|= qualifiers
;
6504 /* XXX: clarify attribute handling */
6505 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
6506 parse_attribute(ad
);
6508 /* recursive type */
6509 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
6510 type1
.t
= 0; /* XXX: same as int */
6513 /* XXX: this is not correct to modify 'ad' at this point, but
6514 the syntax is not clear */
6515 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
6516 parse_attribute(ad
);
6517 type_decl(&type1
, ad
, v
, td
);
6520 /* type identifier */
6521 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
6525 if (!(td
& TYPE_ABSTRACT
))
6526 expect("identifier");
6530 post_type(type
, ad
);
6531 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
6532 parse_attribute(ad
);
6535 /* append type at the end of type1 */
6548 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
6549 static int lvalue_type(int t
)
6554 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
6556 else if (bt
== VT_SHORT
)
6560 if (t
& VT_UNSIGNED
)
6561 r
|= VT_LVAL_UNSIGNED
;
6565 /* indirection with full error checking and bound check */
6566 static void indir(void)
6568 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
6570 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
6572 vtop
->type
= *pointed_type(&vtop
->type
);
6573 /* an array is never an lvalue */
6574 if (!(vtop
->type
.t
& VT_ARRAY
)) {
6575 vtop
->r
|= lvalue_type(vtop
->type
.t
);
6576 /* if bound checking, the referenced pointer must be checked */
6577 if (do_bounds_check
)
6578 vtop
->r
|= VT_MUSTBOUND
;
6582 /* pass a parameter to a function and do type checking and casting */
6583 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
6588 func_type
= func
->c
;
6589 if (func_type
== FUNC_OLD
||
6590 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
6591 /* default casting : only need to convert float to double */
6592 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
6596 } else if (arg
== NULL
) {
6597 error("too many arguments to function");
6600 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
6601 gen_assign_cast(&type
);
6605 /* parse an expression of the form '(type)' or '(expr)' and return its
6607 static void parse_expr_type(CType
*type
)
6613 if (parse_btype(type
, &ad
)) {
6614 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
6621 static void parse_type(CType
*type
)
6626 if (!parse_btype(type
, &ad
)) {
6629 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
6632 static void vpush_tokc(int t
)
6636 vsetc(&type
, VT_CONST
, &tokc
);
6639 static void unary(void)
6641 int n
, t
, align
, size
, r
;
6646 /* XXX: GCC 2.95.3 does not generate a table although it should be
6660 vpush_tokc(VT_INT
| VT_UNSIGNED
);
6664 vpush_tokc(VT_LLONG
);
6668 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
6672 vpush_tokc(VT_FLOAT
);
6676 vpush_tokc(VT_DOUBLE
);
6680 vpush_tokc(VT_LDOUBLE
);
6683 case TOK___FUNCTION__
:
6685 goto tok_identifier
;
6691 /* special function name identifier */
6692 len
= strlen(funcname
) + 1;
6693 /* generate char[len] type */
6698 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
6699 ptr
= section_ptr_add(data_section
, len
);
6700 memcpy(ptr
, funcname
, len
);
6708 /* string parsing */
6711 if (tcc_state
->warn_write_strings
)
6716 memset(&ad
, 0, sizeof(AttributeDef
));
6717 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, 0);
6722 if (parse_btype(&type
, &ad
)) {
6723 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
6725 /* check ISOC99 compound literal */
6727 /* data is allocated locally by default */
6732 /* all except arrays are lvalues */
6733 if (!(type
.t
& VT_ARRAY
))
6734 r
|= lvalue_type(type
.t
);
6735 memset(&ad
, 0, sizeof(AttributeDef
));
6736 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, 0);
6741 } else if (tok
== '{') {
6742 /* save all registers */
6744 /* statement expression : we do not accept break/continue
6745 inside as GCC does */
6746 block(NULL
, NULL
, NULL
, NULL
, 0, 1);
6761 /* functions names must be treated as function pointers,
6762 except for unary '&' and sizeof. Since we consider that
6763 functions are not lvalues, we only have to handle it
6764 there and in function calls. */
6765 /* arrays can also be used although they are not lvalues */
6766 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
6767 !(vtop
->type
.t
& VT_ARRAY
))
6769 mk_pointer(&vtop
->type
);
6775 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
)
6776 vtop
->c
.i
= !vtop
->c
.i
;
6777 else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
6778 vtop
->c
.i
= vtop
->c
.i
^ 1;
6780 vseti(VT_JMP
, gtst(1, 0));
6790 /* in order to force cast, we add zero */
6792 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
6793 error("pointer not accepted for unary plus");
6803 parse_expr_type(&type
);
6807 size
= type_size(&type
, &align
);
6808 if (t
== TOK_SIZEOF
) {
6810 error("sizeof applied to an incomplete type");
6817 case TOK_builtin_types_compatible_p
:
6826 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
6827 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
6828 vpushi(is_compatible_types(&type1
, &type2
));
6831 case TOK_builtin_constant_p
:
6833 int saved_nocode_wanted
, res
;
6836 saved_nocode_wanted
= nocode_wanted
;
6839 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
6841 nocode_wanted
= saved_nocode_wanted
;
6861 goto tok_identifier
;
6863 /* allow to take the address of a label */
6864 if (tok
< TOK_UIDENT
)
6865 expect("label identifier");
6866 s
= label_find(tok
);
6868 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
6870 if (s
->r
== LABEL_DECLARED
)
6871 s
->r
= LABEL_FORWARD
;
6874 s
->type
.t
= VT_VOID
;
6875 mk_pointer(&s
->type
);
6876 s
->type
.t
|= VT_STATIC
;
6878 vset(&s
->type
, VT_CONST
| VT_SYM
, 0);
6887 expect("identifier");
6891 error("'%s' undeclared", get_tok_str(t
, NULL
));
6892 /* for simple function calls, we tolerate undeclared
6893 external reference to int() function */
6894 s
= external_global_sym(t
, &func_old_type
, 0);
6896 vset(&s
->type
, s
->r
, s
->c
);
6897 /* if forward reference, we must point to s */
6898 if (vtop
->r
& VT_SYM
) {
6905 /* post operations */
6907 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
6910 } else if (tok
== '.' || tok
== TOK_ARROW
) {
6912 if (tok
== TOK_ARROW
)
6917 /* expect pointer on structure */
6918 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
6919 expect("struct or union");
6923 while ((s
= s
->next
) != NULL
) {
6928 error("field not found");
6929 /* add field offset to pointer */
6930 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
6933 /* change type to field type, and set to lvalue */
6934 vtop
->type
= s
->type
;
6935 /* an array is never an lvalue */
6936 if (!(vtop
->type
.t
& VT_ARRAY
)) {
6937 vtop
->r
|= lvalue_type(vtop
->type
.t
);
6938 /* if bound checking, the referenced pointer must be checked */
6939 if (do_bounds_check
)
6940 vtop
->r
|= VT_MUSTBOUND
;
6943 } else if (tok
== '[') {
6949 } else if (tok
== '(') {
6955 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
6956 /* pointer test (no array accepted) */
6957 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
6958 vtop
->type
= *pointed_type(&vtop
->type
);
6959 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
6963 expect("function pointer");
6966 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
6968 /* get return type */
6971 sa
= s
->next
; /* first parameter */
6973 /* compute first implicit argument if a structure is returned */
6974 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
6975 /* get some space for the returned structure */
6976 size
= type_size(&s
->type
, &align
);
6977 loc
= (loc
- size
) & -align
;
6979 ret
.r
= VT_LOCAL
| VT_LVAL
;
6980 /* pass it as 'int' to avoid structure arg passing
6982 vseti(VT_LOCAL
, loc
);
6988 /* return in register */
6989 if (is_float(ret
.type
.t
)) {
6992 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
7001 gfunc_param_typed(s
, sa
);
7011 error("too few arguments to function");
7013 if (!nocode_wanted
) {
7014 gfunc_call(nb_args
);
7016 vtop
-= (nb_args
+ 1);
7019 vsetc(&ret
.type
, ret
.r
, &ret
.c
);
7027 static void uneq(void)
7033 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
7034 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
7035 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
7050 static void expr_prod(void)
7055 while (tok
== '*' || tok
== '/' || tok
== '%') {
7063 static void expr_sum(void)
7068 while (tok
== '+' || tok
== '-') {
7076 static void expr_shift(void)
7081 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
7089 static void expr_cmp(void)
7094 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
7095 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
7103 static void expr_cmpeq(void)
7108 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
7116 static void expr_and(void)
7119 while (tok
== '&') {
7126 static void expr_xor(void)
7129 while (tok
== '^') {
7136 static void expr_or(void)
7139 while (tok
== '|') {
7146 /* XXX: fix this mess */
7147 static void expr_land_const(void)
7150 while (tok
== TOK_LAND
) {
7157 /* XXX: fix this mess */
7158 static void expr_lor_const(void)
7161 while (tok
== TOK_LOR
) {
7168 /* only used if non constant */
7169 static void expr_land(void)
7174 if (tok
== TOK_LAND
) {
7178 if (tok
!= TOK_LAND
) {
7188 static void expr_lor(void)
7193 if (tok
== TOK_LOR
) {
7197 if (tok
!= TOK_LOR
) {
7207 /* XXX: better constant handling */
7208 static void expr_eq(void)
7210 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
;
7212 CType type
, type1
, type2
;
7221 if (tok
== ':' && gnu_ext
) {
7237 if (vtop
!= vstack
) {
7238 /* needed to avoid having different registers saved in
7240 if (is_float(vtop
->type
.t
))
7247 if (tok
== ':' && gnu_ext
) {
7255 sv
= *vtop
; /* save value to handle it later */
7256 vtop
--; /* no vpop so that FP stack is not flushed */
7264 bt1
= t1
& VT_BTYPE
;
7266 bt2
= t2
& VT_BTYPE
;
7267 /* cast operands to correct type according to ISOC rules */
7268 if (is_float(bt1
) || is_float(bt2
)) {
7269 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
7270 type
.t
= VT_LDOUBLE
;
7271 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
7276 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
7277 /* cast to biggest op */
7279 /* convert to unsigned if it does not fit in a long long */
7280 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
7281 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
7282 type
.t
|= VT_UNSIGNED
;
7283 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
7284 /* XXX: test pointer compatibility */
7286 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
7287 /* XXX: test structure compatibility */
7289 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
7290 /* NOTE: as an extension, we accept void on only one side */
7293 /* integer operations */
7295 /* convert to unsigned if it does not fit in an integer */
7296 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
7297 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
7298 type
.t
|= VT_UNSIGNED
;
7301 /* now we convert second operand */
7304 if (is_float(type
.t
)) {
7306 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
7307 /* for long longs, we use fixed registers to avoid having
7308 to handle a complicated move */
7313 /* this is horrible, but we must also convert first
7317 /* put again first value and cast it */
7328 static void gexpr(void)
7339 /* parse an expression and return its type without any side effect. */
7340 static void expr_type(CType
*type
)
7342 int saved_nocode_wanted
;
7344 saved_nocode_wanted
= nocode_wanted
;
7349 nocode_wanted
= saved_nocode_wanted
;
7352 /* parse a unary expression and return its type without any side
7354 static void unary_type(CType
*type
)
7366 /* parse a constant expression and return value in vtop. */
7367 static void expr_const1(void)
7376 /* parse an integer constant and return its value. */
7377 static int expr_const(void)
7381 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
7382 expect("constant expression");
7388 /* return the label token if current token is a label, otherwise
7390 static int is_label(void)
7394 /* fast test first */
7395 if (tok
< TOK_UIDENT
)
7397 /* no need to save tokc because tok is an identifier */
7404 unget_tok(last_tok
);
7409 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
7410 int case_reg
, int is_expr
)
7415 /* generate line number info */
7417 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
7418 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
7420 last_line_num
= file
->line_num
;
7424 /* default return value is (void) */
7426 vtop
->type
.t
= VT_VOID
;
7429 if (tok
== TOK_IF
) {
7436 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
7438 if (c
== TOK_ELSE
) {
7442 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
7443 gsym(d
); /* patch else jmp */
7446 } else if (tok
== TOK_WHILE
) {
7454 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
7458 } else if (tok
== '{') {
7462 /* record local declaration stack position */
7464 llabel
= local_label_stack
;
7465 /* handle local labels declarations */
7466 if (tok
== TOK_LABEL
) {
7469 if (tok
< TOK_UIDENT
)
7470 expect("label identifier");
7471 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
7481 while (tok
!= '}') {
7486 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
7489 /* pop locally defined labels */
7490 label_pop(&local_label_stack
, llabel
);
7491 /* pop locally defined symbols */
7492 sym_pop(&local_stack
, s
);
7494 } else if (tok
== TOK_RETURN
) {
7498 gen_assign_cast(&func_vt
);
7499 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
7501 /* if returning structure, must copy it to implicit
7502 first pointer arg location */
7505 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
7508 /* copy structure value to pointer */
7510 } else if (is_float(func_vt
.t
)) {
7515 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
7518 rsym
= gjmp(rsym
); /* jmp */
7519 } else if (tok
== TOK_BREAK
) {
7522 error("cannot break");
7523 *bsym
= gjmp(*bsym
);
7526 } else if (tok
== TOK_CONTINUE
) {
7529 error("cannot continue");
7530 *csym
= gjmp(*csym
);
7533 } else if (tok
== TOK_FOR
) {
7560 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
7565 if (tok
== TOK_DO
) {
7570 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
7581 if (tok
== TOK_SWITCH
) {
7585 /* XXX: other types than integer */
7586 case_reg
= gv(RC_INT
);
7590 b
= gjmp(0); /* jump to first case */
7592 block(&a
, csym
, &b
, &c
, case_reg
, 0);
7593 /* if no default, jmp after switch */
7601 if (tok
== TOK_CASE
) {
7608 if (gnu_ext
&& tok
== TOK_DOTS
) {
7612 warning("empty case range");
7614 /* since a case is like a label, we must skip it with a jmp */
7621 *case_sym
= gtst(1, 0);
7624 *case_sym
= gtst(1, 0);
7628 *case_sym
= gtst(1, *case_sym
);
7633 goto block_after_label
;
7635 if (tok
== TOK_DEFAULT
) {
7641 error("too many 'default'");
7644 goto block_after_label
;
7646 if (tok
== TOK_GOTO
) {
7648 if (tok
== '*' && gnu_ext
) {
7652 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
7655 } else if (tok
>= TOK_UIDENT
) {
7656 s
= label_find(tok
);
7657 /* put forward definition if needed */
7659 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
7661 if (s
->r
== LABEL_DECLARED
)
7662 s
->r
= LABEL_FORWARD
;
7664 /* label already defined */
7665 if (s
->r
& LABEL_FORWARD
)
7666 s
->next
= (void *)gjmp((long)s
->next
);
7668 gjmp_addr((long)s
->next
);
7671 expect("label identifier");
7674 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
7682 if (s
->r
== LABEL_DEFINED
)
7683 error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
7684 gsym((long)s
->next
);
7685 s
->r
= LABEL_DEFINED
;
7687 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
7689 s
->next
= (void *)ind
;
7690 /* we accept this, but it is a mistake */
7693 warning("deprecated use of label at end of compound statement");
7697 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
7700 /* expression case */
7715 /* t is the array or struct type. c is the array or struct
7716 address. cur_index/cur_field is the pointer to the current
7717 value. 'size_only' is true if only size info is needed (only used
7719 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
7720 int *cur_index
, Sym
**cur_field
,
7724 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
7730 if (gnu_ext
&& (l
= is_label()) != 0)
7732 while (tok
== '[' || tok
== '.') {
7734 if (!(type
->t
& VT_ARRAY
))
7735 expect("array type");
7738 index
= expr_const();
7739 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
7740 expect("invalid index");
7741 if (tok
== TOK_DOTS
&& gnu_ext
) {
7743 index_last
= expr_const();
7744 if (index_last
< 0 ||
7745 (s
->c
>= 0 && index_last
>= s
->c
) ||
7747 expect("invalid index");
7753 *cur_index
= index_last
;
7754 type
= pointed_type(type
);
7755 elem_size
= type_size(type
, &align
);
7756 c
+= index
* elem_size
;
7757 /* NOTE: we only support ranges for last designator */
7758 nb_elems
= index_last
- index
+ 1;
7759 if (nb_elems
!= 1) {
7768 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
7769 expect("struct/union type");
7782 /* XXX: fix this mess by using explicit storage field */
7784 type1
.t
|= (type
->t
& ~VT_TYPE
);
7798 if (type
->t
& VT_ARRAY
) {
7800 type
= pointed_type(type
);
7801 c
+= index
* type_size(type
, &align
);
7805 error("too many field init");
7806 /* XXX: fix this mess by using explicit storage field */
7808 type1
.t
|= (type
->t
& ~VT_TYPE
);
7813 decl_initializer(type
, sec
, c
, 0, size_only
);
7815 /* XXX: make it more general */
7816 if (!size_only
&& nb_elems
> 1) {
7817 unsigned long c_end
;
7822 error("range init not supported yet for dynamic storage");
7823 c_end
= c
+ nb_elems
* elem_size
;
7824 if (c_end
> sec
->data_allocated
)
7825 section_realloc(sec
, c_end
);
7826 src
= sec
->data
+ c
;
7828 for(i
= 1; i
< nb_elems
; i
++) {
7830 memcpy(dst
, src
, elem_size
);
7836 #define EXPR_CONST 1
7839 /* store a value or an expression directly in global data or in local array */
7840 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
7841 int v
, int expr_type
)
7843 int saved_global_expr
, bt
, bit_pos
, bit_size
;
7845 unsigned long long bit_mask
;
7853 /* compound literals must be allocated globally in this case */
7854 saved_global_expr
= global_expr
;
7857 global_expr
= saved_global_expr
;
7858 /* NOTE: symbols are accepted */
7859 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
7860 error("initializer element is not constant");
7868 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
7871 /* XXX: not portable */
7872 /* XXX: generate error if incorrect relocation */
7873 gen_assign_cast(&dtype
);
7874 bt
= type
->t
& VT_BTYPE
;
7875 ptr
= sec
->data
+ c
;
7876 /* XXX: make code faster ? */
7877 if (!(type
->t
& VT_BITFIELD
)) {
7882 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
7883 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
7884 bit_mask
= (1LL << bit_size
) - 1;
7886 if ((vtop
->r
& VT_SYM
) &&
7892 (bt
== VT_INT
&& bit_size
!= 32)))
7893 error("initializer element is not computable at load time");
7896 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
7899 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
7902 *(double *)ptr
= vtop
->c
.d
;
7905 *(long double *)ptr
= vtop
->c
.ld
;
7908 *(long long *)ptr
|= (vtop
->c
.ll
& bit_mask
) << bit_pos
;
7911 if (vtop
->r
& VT_SYM
) {
7912 greloc(sec
, vtop
->sym
, c
, R_DATA_32
);
7914 *(int *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
7919 vset(&dtype
, VT_LOCAL
, c
);
7926 /* put zeros for variable based init */
7927 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
7930 /* nothing to do because globals are already set to zero */
7932 vpush_global_sym(&func_old_type
, TOK_memset
);
7940 /* 't' contains the type and storage info. 'c' is the offset of the
7941 object in section 'sec'. If 'sec' is NULL, it means stack based
7942 allocation. 'first' is true if array '{' must be read (multi
7943 dimension implicit array init handling). 'size_only' is true if
7944 size only evaluation is wanted (only for arrays). */
7945 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
7946 int first
, int size_only
)
7948 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, i
;
7949 int size1
, align1
, expr_type
;
7953 if (type
->t
& VT_ARRAY
) {
7957 t1
= pointed_type(type
);
7958 size1
= type_size(t1
, &align1
);
7961 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
7967 /* only parse strings here if correct type (otherwise: handle
7968 them as ((w)char *) expressions */
7969 if ((tok
== TOK_LSTR
&&
7970 (t1
->t
& VT_BTYPE
) == VT_INT
) ||
7972 (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
7973 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
7978 /* compute maximum number of chars wanted */
7980 cstr_len
= cstr
->size
;
7982 cstr_len
= cstr
->size
/ sizeof(int);
7985 if (n
>= 0 && nb
> (n
- array_length
))
7986 nb
= n
- array_length
;
7989 warning("initializer-string for array is too long");
7990 /* in order to go faster for common case (char
7991 string in global variable, we handle it
7993 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
7994 memcpy(sec
->data
+ c
+ array_length
, cstr
->data
, nb
);
7998 ch
= ((unsigned char *)cstr
->data
)[i
];
8000 ch
= ((int *)cstr
->data
)[i
];
8001 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
8009 /* only add trailing zero if enough storage (no
8010 warning in this case since it is standard) */
8011 if (n
< 0 || array_length
< n
) {
8013 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
8019 while (tok
!= '}') {
8020 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
8021 if (n
>= 0 && index
>= n
)
8022 error("index too large");
8023 /* must put zero in holes (note that doing it that way
8024 ensures that it even works with designators) */
8025 if (!size_only
&& array_length
< index
) {
8026 init_putz(t1
, sec
, c
+ array_length
* size1
,
8027 (index
- array_length
) * size1
);
8030 if (index
> array_length
)
8031 array_length
= index
;
8032 /* special test for multi dimensional arrays (may not
8033 be strictly correct if designators are used at the
8035 if (index
>= n
&& no_oblock
)
8044 /* put zeros at the end */
8045 if (!size_only
&& n
>= 0 && array_length
< n
) {
8046 init_putz(t1
, sec
, c
+ array_length
* size1
,
8047 (n
- array_length
) * size1
);
8049 /* patch type size if needed */
8051 s
->c
= array_length
;
8052 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
8053 (sec
|| !first
|| tok
== '{')) {
8056 /* NOTE: the previous test is a specific case for automatic
8057 struct/union init */
8058 /* XXX: union needs only one init */
8060 /* XXX: this test is incorrect for local initializers
8061 beginning with ( without {. It would be much more difficult
8062 to do it correctly (ideally, the expression parser should
8063 be used in all cases) */
8069 while (tok
== '(') {
8073 if (!parse_btype(&type1
, &ad1
))
8075 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
8077 if (!is_assignable_types(type
, &type1
))
8078 error("invalid type for cast");
8083 if (first
|| tok
== '{') {
8092 while (tok
!= '}') {
8093 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
8095 if (!size_only
&& array_length
< index
) {
8096 init_putz(type
, sec
, c
+ array_length
,
8097 index
- array_length
);
8099 index
= index
+ type_size(&f
->type
, &align1
);
8100 if (index
> array_length
)
8101 array_length
= index
;
8103 if (no_oblock
&& f
== NULL
)
8109 /* put zeros at the end */
8110 if (!size_only
&& array_length
< n
) {
8111 init_putz(type
, sec
, c
+ array_length
,
8120 } else if (tok
== '{') {
8122 decl_initializer(type
, sec
, c
, first
, size_only
);
8124 } else if (size_only
) {
8125 /* just skip expression */
8127 while ((parlevel
> 0 || (tok
!= '}' && tok
!= ',')) &&
8131 else if (tok
== ')')
8136 /* currently, we always use constant expression for globals
8137 (may change for scripting case) */
8138 expr_type
= EXPR_CONST
;
8140 expr_type
= EXPR_ANY
;
8141 init_putv(type
, sec
, c
, 0, expr_type
);
8145 /* parse an initializer for type 't' if 'has_init' is non zero, and
8146 allocate space in local or global data space ('r' is either
8147 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
8148 variable 'v' of scope 'scope' is declared before initializers are
8149 parsed. If 'v' is zero, then a reference to the new object is put
8150 in the value stack. If 'has_init' is 2, a special parsing is done
8151 to handle string constants. */
8152 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
8153 int has_init
, int v
, int scope
)
8155 int size
, align
, addr
, data_offset
;
8157 ParseState saved_parse_state
;
8158 TokenString init_str
;
8161 size
= type_size(type
, &align
);
8162 /* If unknown size, we must evaluate it before
8163 evaluating initializers because
8164 initializers can generate global data too
8165 (e.g. string pointers or ISOC99 compound
8166 literals). It also simplifies local
8167 initializers handling */
8168 tok_str_new(&init_str
);
8171 error("unknown type size");
8172 /* get all init string */
8173 if (has_init
== 2) {
8174 /* only get strings */
8175 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
8176 tok_str_add_tok(&init_str
);
8181 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
8183 error("unexpected end of file in initializer");
8184 tok_str_add_tok(&init_str
);
8187 else if (tok
== '}') {
8195 tok_str_add(&init_str
, -1);
8196 tok_str_add(&init_str
, 0);
8199 save_parse_state(&saved_parse_state
);
8201 macro_ptr
= init_str
.str
;
8203 decl_initializer(type
, NULL
, 0, 1, 1);
8204 /* prepare second initializer parsing */
8205 macro_ptr
= init_str
.str
;
8208 /* if still unknown size, error */
8209 size
= type_size(type
, &align
);
8211 error("unknown type size");
8213 /* take into account specified alignment if bigger */
8214 if (ad
->aligned
> align
)
8215 align
= ad
->aligned
;
8216 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
8218 if (do_bounds_check
&& (type
->t
& VT_ARRAY
))
8220 loc
= (loc
- size
) & -align
;
8222 /* handles bounds */
8223 /* XXX: currently, since we do only one pass, we cannot track
8224 '&' operators, so we add only arrays */
8225 if (do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
8226 unsigned long *bounds_ptr
;
8227 /* add padding between regions */
8229 /* then add local bound info */
8230 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(unsigned long));
8231 bounds_ptr
[0] = addr
;
8232 bounds_ptr
[1] = size
;
8235 /* local variable */
8236 sym_push(v
, type
, r
, addr
);
8238 /* push local reference */
8239 vset(type
, r
, addr
);
8245 if (v
&& scope
== VT_CONST
) {
8246 /* see if the symbol was already defined */
8249 if (!is_compatible_types(&sym
->type
, type
))
8250 error("incompatible types for redefinition of '%s'",
8251 get_tok_str(v
, NULL
));
8252 if (sym
->type
.t
& VT_EXTERN
) {
8253 /* if the variable is extern, it was not allocated */
8254 sym
->type
.t
&= ~VT_EXTERN
;
8256 /* we accept several definitions of the same
8257 global variable. this is tricky, because we
8258 must play with the SHN_COMMON type of the symbol */
8259 /* XXX: should check if the variable was already
8260 initialized. It is incorrect to initialized it
8262 /* no init data, we won't add more to the symbol */
8269 /* allocate symbol in corresponding section */
8276 data_offset
= sec
->data_offset
;
8277 data_offset
= (data_offset
+ align
- 1) & -align
;
8279 /* very important to increment global pointer at this time
8280 because initializers themselves can create new initializers */
8281 data_offset
+= size
;
8282 /* add padding if bound check */
8283 if (do_bounds_check
)
8285 sec
->data_offset
= data_offset
;
8286 /* allocate section space to put the data */
8287 if (sec
->sh_type
!= SHT_NOBITS
&&
8288 data_offset
> sec
->data_allocated
)
8289 section_realloc(sec
, data_offset
);
8291 addr
= 0; /* avoid warning */
8295 if (scope
== VT_CONST
) {
8300 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
8302 /* update symbol definition */
8304 put_extern_sym(sym
, sec
, addr
, size
);
8307 /* put a common area */
8308 put_extern_sym(sym
, NULL
, align
, size
);
8309 /* XXX: find a nicer way */
8310 esym
= &((Elf32_Sym
*)symtab_section
->data
)[sym
->c
];
8311 esym
->st_shndx
= SHN_COMMON
;
8316 /* push global reference */
8317 sym
= get_sym_ref(type
, sec
, addr
, size
);
8319 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
8323 /* handles bounds now because the symbol must be defined
8324 before for the relocation */
8325 if (do_bounds_check
) {
8326 unsigned long *bounds_ptr
;
8328 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_32
);
8329 /* then add global bound info */
8330 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(long));
8331 bounds_ptr
[0] = 0; /* relocated */
8332 bounds_ptr
[1] = size
;
8336 decl_initializer(type
, sec
, addr
, 1, 0);
8337 /* restore parse state if needed */
8339 tok_str_free(init_str
.str
);
8340 restore_parse_state(&saved_parse_state
);
8346 void put_func_debug(Sym
*sym
)
8351 /* XXX: we put here a dummy type */
8352 snprintf(buf
, sizeof(buf
), "%s:%c1",
8353 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
8354 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
8355 cur_text_section
, sym
->c
);
8360 /* not finished : try to put some local vars in registers */
8361 //#define CONFIG_REG_VARS
8363 #ifdef CONFIG_REG_VARS
8364 void add_var_ref(int t
)
8366 printf("%s:%d: &%s\n",
8367 file
->filename
, file
->line_num
,
8368 get_tok_str(t
, NULL
));
8371 /* first pass on a function with heuristic to extract variable usage
8372 and pointer references to local variables for register allocation */
8373 void analyse_function(void)
8380 /* any symbol coming after '&' is considered as being a
8381 variable whose reference is taken. It is highly unaccurate
8382 but it is difficult to do better without a complete parse */
8385 /* if '& number', then no need to examine next tokens */
8386 if (tok
== TOK_CINT
||
8388 tok
== TOK_CLLONG
||
8389 tok
== TOK_CULLONG
) {
8391 } else if (tok
>= TOK_UIDENT
) {
8392 /* if '& ident [' or '& ident ->', then ident address
8396 if (tok
!= '[' && tok
!= TOK_ARROW
)
8400 while (tok
!= '}' && tok
!= ';' &&
8401 !((tok
== ',' || tok
== ')') && level
== 0)) {
8402 if (tok
>= TOK_UIDENT
) {
8404 } else if (tok
== '(') {
8406 } else if (tok
== ')') {
8419 /* parse an old style function declaration list */
8420 /* XXX: check multiple parameter */
8421 static void func_decl_list(Sym
*func_sym
)
8428 /* parse each declaration */
8429 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
) {
8430 if (!parse_btype(&btype
, &ad
))
8431 expect("declaration list");
8432 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
8433 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
8435 /* we accept no variable after */
8439 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
8440 /* find parameter in function parameter list */
8443 if ((s
->v
& ~SYM_FIELD
) == v
)
8447 error("declaration for parameter '%s' but no such parameter",
8448 get_tok_str(v
, NULL
));
8450 /* check that no storage specifier except 'register' was given */
8451 if (type
.t
& VT_STORAGE
)
8452 error("storage class specified for '%s'", get_tok_str(v
, NULL
));
8453 convert_parameter_type(&type
);
8454 /* we can add the type (NOTE: it could be local to the function) */
8456 /* accept other parameters */
8467 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
8468 static void decl(int l
)
8476 if (!parse_btype(&btype
, &ad
)) {
8477 /* skip redundant ';' */
8478 /* XXX: find more elegant solution */
8483 /* special test for old K&R protos without explicit int
8484 type. Only accepted when defining global data */
8485 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
8489 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
8490 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
8492 /* we accept no variable after */
8496 while (1) { /* iterate thru each declaration */
8498 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
8502 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
8503 printf("type = '%s'\n", buf
);
8506 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
8507 /* if old style function prototype, we accept a
8510 if (sym
->c
== FUNC_OLD
)
8511 func_decl_list(sym
);
8515 #ifdef CONFIG_REG_VARS
8516 TokenString func_str
;
8517 ParseState saved_parse_state
;
8522 error("cannot use local functions");
8523 if (!(type
.t
& VT_FUNC
))
8524 expect("function definition");
8526 /* reject abstract declarators in function definition */
8528 while ((sym
= sym
->next
) != NULL
)
8529 if (!(sym
->v
& ~SYM_FIELD
))
8530 expect("identifier");
8532 /* XXX: cannot do better now: convert extern line to static inline */
8533 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
8534 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
8536 #ifdef CONFIG_REG_VARS
8537 /* parse all function code and record it */
8539 tok_str_new(&func_str
);
8545 error("unexpected end of file");
8546 tok_str_add_tok(&func_str
);
8551 } else if (t
== '}') {
8553 if (block_level
== 0)
8557 tok_str_add(&func_str
, -1);
8558 tok_str_add(&func_str
, 0);
8560 save_parse_state(&saved_parse_state
);
8562 macro_ptr
= func_str
.str
;
8567 /* compute text section */
8568 cur_text_section
= ad
.section
;
8569 if (!cur_text_section
)
8570 cur_text_section
= text_section
;
8571 ind
= cur_text_section
->data_offset
;
8572 funcname
= get_tok_str(v
, NULL
);
8575 /* if symbol is already defined, then put complete type */
8578 /* put function symbol */
8579 sym
= global_identifier_push(v
, type
.t
, 0);
8580 sym
->type
.ref
= type
.ref
;
8582 /* NOTE: we patch the symbol size later */
8583 put_extern_sym(sym
, cur_text_section
, ind
, 0);
8585 sym
->r
= VT_SYM
| VT_CONST
;
8586 /* put debug symbol */
8588 put_func_debug(sym
);
8589 /* push a dummy symbol to enable local sym storage */
8590 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
8591 gfunc_prolog(&type
);
8594 #ifdef CONFIG_REG_VARS
8595 macro_ptr
= func_str
.str
;
8598 block(NULL
, NULL
, NULL
, NULL
, 0, 0);
8601 cur_text_section
->data_offset
= ind
;
8602 label_pop(&global_label_stack
, NULL
);
8603 sym_pop(&local_stack
, NULL
); /* reset local stack */
8604 /* end of function */
8605 /* patch symbol size */
8606 ((Elf32_Sym
*)symtab_section
->data
)[sym
->c
].st_size
=
8609 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
8611 funcname
= ""; /* for safety */
8612 func_vt
.t
= VT_VOID
; /* for safety */
8613 ind
= 0; /* for safety */
8615 #ifdef CONFIG_REG_VARS
8616 tok_str_free(func_str
.str
);
8617 restore_parse_state(&saved_parse_state
);
8621 if (btype
.t
& VT_TYPEDEF
) {
8622 /* save typedefed type */
8623 /* XXX: test storage specifiers ? */
8624 sym
= sym_push(v
, &type
, 0, 0);
8625 sym
->type
.t
|= VT_TYPEDEF
;
8626 } else if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
8627 /* external function definition */
8628 external_sym(v
, &type
, 0);
8630 /* not lvalue if array */
8632 if (!(type
.t
& VT_ARRAY
))
8633 r
|= lvalue_type(type
.t
);
8634 has_init
= (tok
== '=');
8635 if ((btype
.t
& VT_EXTERN
) ||
8636 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
8637 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
8638 /* external variable */
8639 /* NOTE: as GCC, uninitialized global static
8640 arrays of null size are considered as
8642 external_sym(v
, &type
, r
);
8644 if (type
.t
& VT_STATIC
)
8650 decl_initializer_alloc(&type
, &ad
, r
,
8664 /* better than nothing, but needs extension to handle '-E' option
8666 static void preprocess_init(TCCState
*s1
)
8668 s1
->include_stack_ptr
= s1
->include_stack
;
8669 /* XXX: move that before to avoid having to initialize
8670 file->ifdef_stack_ptr ? */
8671 s1
->ifdef_stack_ptr
= s1
->ifdef_stack
;
8672 file
->ifdef_stack_ptr
= s1
->ifdef_stack_ptr
;
8674 /* XXX: not ANSI compliant: bound checking says error */
8678 /* compile the C file opened in 'file'. Return non zero if errors. */
8679 static int tcc_compile(TCCState
*s1
)
8683 volatile int section_sym
;
8686 printf("%s: **** new file\n", file
->filename
);
8688 preprocess_init(s1
);
8691 anon_sym
= SYM_FIRST_ANOM
;
8693 /* file info: full path + filename */
8694 section_sym
= 0; /* avoid warning */
8696 section_sym
= put_elf_sym(symtab_section
, 0, 0,
8697 ELF32_ST_INFO(STB_LOCAL
, STT_SECTION
), 0,
8698 text_section
->sh_num
, NULL
);
8699 getcwd(buf
, sizeof(buf
));
8700 pstrcat(buf
, sizeof(buf
), "/");
8701 put_stabs_r(buf
, N_SO
, 0, 0,
8702 text_section
->data_offset
, text_section
, section_sym
);
8703 put_stabs_r(file
->filename
, N_SO
, 0, 0,
8704 text_section
->data_offset
, text_section
, section_sym
);
8706 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
8707 symbols can be safely used */
8708 put_elf_sym(symtab_section
, 0, 0,
8709 ELF32_ST_INFO(STB_LOCAL
, STT_FILE
), 0,
8710 SHN_ABS
, file
->filename
);
8712 /* define some often used types */
8713 int_type
.t
= VT_INT
;
8715 char_pointer_type
.t
= VT_BYTE
;
8716 mk_pointer(&char_pointer_type
);
8718 func_old_type
.t
= VT_FUNC
;
8719 func_old_type
.ref
= sym_push(SYM_FIELD
, &int_type
, FUNC_CDECL
, FUNC_OLD
);
8722 /* define 'void *alloca(unsigned int)' builtin function */
8727 sym
= sym_push(p
, mk_pointer(VT_VOID
), FUNC_CDECL
, FUNC_NEW
);
8728 s1
= sym_push(SYM_FIELD
, VT_UNSIGNED
| VT_INT
, 0, 0);
8731 sym_push(TOK_alloca
, VT_FUNC
| (p
<< VT_STRUCT_SHIFT
), VT_CONST
, 0);
8735 define_start
= define_stack
;
8737 if (setjmp(s1
->error_jmp_buf
) == 0) {
8739 s1
->error_set_jmp_enabled
= 1;
8741 ch
= file
->buf_ptr
[0];
8742 tok_flags
= TOK_FLAG_BOL
| TOK_FLAG_BOF
;
8743 parse_flags
= PARSE_FLAG_PREPROCESS
| PARSE_FLAG_TOK_NUM
;
8747 expect("declaration");
8749 /* end of translation unit info */
8751 put_stabs_r(NULL
, N_SO
, 0, 0,
8752 text_section
->data_offset
, text_section
, section_sym
);
8755 s1
->error_set_jmp_enabled
= 0;
8757 /* reset define stack, but leave -Dsymbols (may be incorrect if
8758 they are undefined) */
8759 free_defines(define_start
);
8761 sym_pop(&global_stack
, NULL
);
8763 return s1
->nb_errors
!= 0 ? -1 : 0;
8767 int tcc_compile_string(TCCState
*s
, const char *str
)
8769 BufferedFile bf1
, *bf
= &bf1
;
8773 /* init file structure */
8775 /* XXX: avoid copying */
8777 buf
= tcc_malloc(len
+ 1);
8780 memcpy(buf
, str
, len
);
8783 bf
->buf_end
= buf
+ len
;
8784 pstrcpy(bf
->filename
, sizeof(bf
->filename
), "<string>");
8788 ret
= tcc_compile(s
);
8792 /* currently, no need to close */
8797 /* define a preprocessor symbol. A value can also be provided with the '=' operator */
8798 void tcc_define_symbol(TCCState
*s1
, const char *sym
, const char *value
)
8800 BufferedFile bf1
, *bf
= &bf1
;
8802 pstrcpy(bf
->buffer
, IO_BUF_SIZE
, sym
);
8803 pstrcat(bf
->buffer
, IO_BUF_SIZE
, " ");
8807 pstrcat(bf
->buffer
, IO_BUF_SIZE
, value
);
8809 /* init file structure */
8811 bf
->buf_ptr
= bf
->buffer
;
8812 bf
->buf_end
= bf
->buffer
+ strlen(bf
->buffer
);
8813 *bf
->buf_end
= CH_EOB
;
8814 bf
->filename
[0] = '\0';
8818 s1
->include_stack_ptr
= s1
->include_stack
;
8820 /* parse with define parser */
8821 ch
= file
->buf_ptr
[0];
8827 /* undefine a preprocessor symbol */
8828 void tcc_undefine_symbol(TCCState
*s1
, const char *sym
)
8832 ts
= tok_alloc(sym
, strlen(sym
));
8833 s
= define_find(ts
->tok
);
8834 /* undefine symbol by putting an invalid name */
8839 #ifdef CONFIG_TCC_ASM
8841 #include "i386-asm.c"
8845 static void asm_instr(void)
8847 error("inline asm() not supported");
8853 /* print the position in the source file of PC value 'pc' by reading
8854 the stabs debug information */
8855 static void rt_printline(unsigned long wanted_pc
)
8857 Stab_Sym
*sym
, *sym_end
;
8858 char func_name
[128], last_func_name
[128];
8859 unsigned long func_addr
, last_pc
, pc
;
8860 const char *incl_files
[INCLUDE_STACK_SIZE
];
8861 int incl_index
, len
, last_line_num
, i
;
8862 const char *str
, *p
;
8864 fprintf(stderr
, "0x%08lx:", wanted_pc
);
8866 func_name
[0] = '\0';
8869 last_func_name
[0] = '\0';
8870 last_pc
= 0xffffffff;
8872 sym
= (Stab_Sym
*)stab_section
->data
+ 1;
8873 sym_end
= (Stab_Sym
*)(stab_section
->data
+ stab_section
->data_offset
);
8874 while (sym
< sym_end
) {
8875 switch(sym
->n_type
) {
8876 /* function start or end */
8878 if (sym
->n_strx
== 0) {
8879 /* we test if between last line and end of function */
8880 pc
= sym
->n_value
+ func_addr
;
8881 if (wanted_pc
>= last_pc
&& wanted_pc
< pc
)
8883 func_name
[0] = '\0';
8886 str
= stabstr_section
->data
+ sym
->n_strx
;
8887 p
= strchr(str
, ':');
8889 pstrcpy(func_name
, sizeof(func_name
), str
);
8892 if (len
> sizeof(func_name
) - 1)
8893 len
= sizeof(func_name
) - 1;
8894 memcpy(func_name
, str
, len
);
8895 func_name
[len
] = '\0';
8897 func_addr
= sym
->n_value
;
8900 /* line number info */
8902 pc
= sym
->n_value
+ func_addr
;
8903 if (wanted_pc
>= last_pc
&& wanted_pc
< pc
)
8906 last_line_num
= sym
->n_desc
;
8908 strcpy(last_func_name
, func_name
);
8912 str
= stabstr_section
->data
+ sym
->n_strx
;
8914 if (incl_index
< INCLUDE_STACK_SIZE
) {
8915 incl_files
[incl_index
++] = str
;
8923 if (sym
->n_strx
== 0) {
8924 incl_index
= 0; /* end of translation unit */
8926 str
= stabstr_section
->data
+ sym
->n_strx
;
8927 /* do not add path */
8929 if (len
> 0 && str
[len
- 1] != '/')
8937 /* second pass: we try symtab symbols (no line number info) */
8940 Elf32_Sym
*sym
, *sym_end
;
8943 sym_end
= (Elf32_Sym
*)(symtab_section
->data
+ symtab_section
->data_offset
);
8944 for(sym
= (Elf32_Sym
*)symtab_section
->data
+ 1;
8947 type
= ELF32_ST_TYPE(sym
->st_info
);
8948 if (type
== STT_FUNC
) {
8949 if (wanted_pc
>= sym
->st_value
&&
8950 wanted_pc
< sym
->st_value
+ sym
->st_size
) {
8951 pstrcpy(last_func_name
, sizeof(last_func_name
),
8952 strtab_section
->data
+ sym
->st_name
);
8958 /* did not find any info: */
8959 fprintf(stderr
, " ???\n");
8962 if (last_func_name
[0] != '\0') {
8963 fprintf(stderr
, " %s()", last_func_name
);
8965 if (incl_index
> 0) {
8966 fprintf(stderr
, " (%s:%d",
8967 incl_files
[incl_index
- 1], last_line_num
);
8968 for(i
= incl_index
- 2; i
>= 0; i
--)
8969 fprintf(stderr
, ", included from %s", incl_files
[i
]);
8970 fprintf(stderr
, ")");
8972 fprintf(stderr
, "\n");
8979 /* fix for glibc 2.1 */
8985 /* return the PC at frame level 'level'. Return non zero if not found */
8986 static int rt_get_caller_pc(unsigned long *paddr
,
8987 ucontext_t
*uc
, int level
)
8994 *paddr
= uc
->uc_mcontext
.mc_eip
;
8996 *paddr
= uc
->uc_mcontext
.gregs
[REG_EIP
];
9001 fp
= uc
->uc_mcontext
.mc_ebp
;
9003 fp
= uc
->uc_mcontext
.gregs
[REG_EBP
];
9005 for(i
=1;i
<level
;i
++) {
9006 /* XXX: check address validity with program info */
9007 if (fp
<= 0x1000 || fp
>= 0xc0000000)
9009 fp
= ((unsigned long *)fp
)[0];
9011 *paddr
= ((unsigned long *)fp
)[1];
9016 #error add arch specific rt_get_caller_pc()
9019 /* emit a run time error at position 'pc' */
9020 void rt_error(ucontext_t
*uc
, const char *fmt
, ...)
9027 fprintf(stderr
, "Runtime error: ");
9028 vfprintf(stderr
, fmt
, ap
);
9029 fprintf(stderr
, "\n");
9030 for(i
=0;i
<num_callers
;i
++) {
9031 if (rt_get_caller_pc(&pc
, uc
, i
) < 0)
9034 fprintf(stderr
, "at ");
9036 fprintf(stderr
, "by ");
9043 /* signal handler for fatal errors */
9044 static void sig_error(int signum
, siginfo_t
*siginf
, void *puc
)
9046 ucontext_t
*uc
= puc
;
9050 switch(siginf
->si_code
) {
9053 rt_error(uc
, "division by zero");
9056 rt_error(uc
, "floating point exception");
9062 if (rt_bound_error_msg
&& *rt_bound_error_msg
)
9063 rt_error(uc
, *rt_bound_error_msg
);
9065 rt_error(uc
, "dereferencing invalid pointer");
9068 rt_error(uc
, "illegal instruction");
9071 rt_error(uc
, "abort() called");
9074 rt_error(uc
, "caught signal %d", signum
);
9081 /* do all relocations (needed before using tcc_get_symbol()) */
9082 int tcc_relocate(TCCState
*s1
)
9089 tcc_add_runtime(s1
);
9091 build_got_entries(s1
);
9093 relocate_common_syms();
9095 /* compute relocation address : section are relocated in place. We
9096 also alloc the bss space */
9097 for(i
= 1; i
< s1
->nb_sections
; i
++) {
9098 s
= s1
->sections
[i
];
9099 if (s
->sh_flags
& SHF_ALLOC
) {
9100 if (s
->sh_type
== SHT_NOBITS
)
9101 s
->data
= tcc_mallocz(s
->data_offset
);
9102 s
->sh_addr
= (unsigned long)s
->data
;
9106 relocate_syms(s1
, 1);
9108 if (s1
->nb_errors
!= 0)
9111 /* relocate each section */
9112 for(i
= 1; i
< s1
->nb_sections
; i
++) {
9113 s
= s1
->sections
[i
];
9115 relocate_section(s1
, s
);
9120 /* launch the compiled program with the given arguments */
9121 int tcc_run(TCCState
*s1
, int argc
, char **argv
)
9123 int (*prog_main
)(int, char **);
9125 if (tcc_relocate(s1
) < 0)
9128 prog_main
= tcc_get_symbol_err(s1
, "main");
9132 error("debug mode currently not available for Windows");
9134 struct sigaction sigact
;
9135 /* install TCC signal handlers to print debug info on fatal
9137 sigact
.sa_flags
= SA_SIGINFO
| SA_RESETHAND
;
9138 sigact
.sa_sigaction
= sig_error
;
9139 sigemptyset(&sigact
.sa_mask
);
9140 sigaction(SIGFPE
, &sigact
, NULL
);
9141 sigaction(SIGILL
, &sigact
, NULL
);
9142 sigaction(SIGSEGV
, &sigact
, NULL
);
9143 sigaction(SIGBUS
, &sigact
, NULL
);
9144 sigaction(SIGABRT
, &sigact
, NULL
);
9148 #ifdef CONFIG_TCC_BCHECK
9149 if (do_bounds_check
) {
9150 void (*bound_init
)(void);
9152 /* set error function */
9153 rt_bound_error_msg
= (void *)tcc_get_symbol_err(s1
,
9154 "__bound_error_msg");
9156 /* XXX: use .init section so that it also work in binary ? */
9157 bound_init
= (void *)tcc_get_symbol_err(s1
, "__bound_init");
9161 return (*prog_main
)(argc
, argv
);
9164 TCCState
*tcc_new(void)
9171 s
= tcc_mallocz(sizeof(TCCState
));
9175 s
->output_type
= TCC_OUTPUT_MEMORY
;
9177 /* init isid table */
9179 isidnum_table
[i
] = isid(i
) || isnum(i
);
9181 /* add all tokens */
9183 memset(hash_ident
, 0, TOK_HASH_SIZE
* sizeof(TokenSym
*));
9185 tok_ident
= TOK_IDENT
;
9194 ts
= tok_alloc(p
, r
- p
- 1);
9198 /* we add dummy defines for some special macros to speed up tests
9199 and to have working defined() */
9200 define_push(TOK___LINE__
, MACRO_OBJ
, NULL
, NULL
);
9201 define_push(TOK___FILE__
, MACRO_OBJ
, NULL
, NULL
);
9202 define_push(TOK___DATE__
, MACRO_OBJ
, NULL
, NULL
);
9203 define_push(TOK___TIME__
, MACRO_OBJ
, NULL
, NULL
);
9205 /* standard defines */
9206 tcc_define_symbol(s
, "__STDC__", NULL
);
9207 #if defined(TCC_TARGET_I386)
9208 tcc_define_symbol(s
, "__i386__", NULL
);
9211 tcc_define_symbol(s
, "__linux__", NULL
);
9212 tcc_define_symbol(s
, "linux", NULL
);
9214 /* tiny C specific defines */
9215 tcc_define_symbol(s
, "__TINYC__", NULL
);
9217 /* tiny C & gcc defines */
9218 tcc_define_symbol(s
, "__SIZE_TYPE__", "unsigned int");
9219 tcc_define_symbol(s
, "__PTRDIFF_TYPE__", "int");
9220 tcc_define_symbol(s
, "__WCHAR_TYPE__", "int");
9222 /* default library paths */
9223 tcc_add_library_path(s
, "/usr/local/lib");
9224 tcc_add_library_path(s
, "/usr/lib");
9225 tcc_add_library_path(s
, "/lib");
9227 /* no section zero */
9228 dynarray_add((void ***)&s
->sections
, &s
->nb_sections
, NULL
);
9230 /* create standard sections */
9231 text_section
= new_section(s
, ".text", SHT_PROGBITS
, SHF_ALLOC
| SHF_EXECINSTR
);
9232 data_section
= new_section(s
, ".data", SHT_PROGBITS
, SHF_ALLOC
| SHF_WRITE
);
9233 bss_section
= new_section(s
, ".bss", SHT_NOBITS
, SHF_ALLOC
| SHF_WRITE
);
9235 /* symbols are always generated for linking stage */
9236 symtab_section
= new_symtab(s
, ".symtab", SHT_SYMTAB
, 0,
9238 ".hashtab", SHF_PRIVATE
);
9239 strtab_section
= symtab_section
->link
;
9241 /* private symbol table for dynamic symbols */
9242 s
->dynsymtab_section
= new_symtab(s
, ".dynsymtab", SHT_SYMTAB
, SHF_PRIVATE
,
9244 ".dynhashtab", SHF_PRIVATE
);
9245 s
->alacarte_link
= 1;
9249 void tcc_delete(TCCState
*s1
)
9253 /* free -D defines */
9257 n
= tok_ident
- TOK_IDENT
;
9258 for(i
= 0; i
< n
; i
++)
9259 tcc_free(table_ident
[i
]);
9260 tcc_free(table_ident
);
9262 /* free all sections */
9264 free_section(symtab_section
->hash
);
9266 free_section(s1
->dynsymtab_section
->hash
);
9267 free_section(s1
->dynsymtab_section
->link
);
9268 free_section(s1
->dynsymtab_section
);
9270 for(i
= 1; i
< s1
->nb_sections
; i
++)
9271 free_section(s1
->sections
[i
]);
9272 tcc_free(s1
->sections
);
9274 /* free loaded dlls array */
9275 for(i
= 0; i
< s1
->nb_loaded_dlls
; i
++)
9276 tcc_free(s1
->loaded_dlls
[i
]);
9277 tcc_free(s1
->loaded_dlls
);
9280 for(i
= 0; i
< s1
->nb_library_paths
; i
++)
9281 tcc_free(s1
->library_paths
[i
]);
9282 tcc_free(s1
->library_paths
);
9284 /* cached includes */
9285 for(i
= 0; i
< s1
->nb_cached_includes
; i
++)
9286 tcc_free(s1
->cached_includes
[i
]);
9287 tcc_free(s1
->cached_includes
);
9289 for(i
= 0; i
< s1
->nb_include_paths
; i
++)
9290 tcc_free(s1
->include_paths
[i
]);
9291 tcc_free(s1
->include_paths
);
9293 for(i
= 0; i
< s1
->nb_sysinclude_paths
; i
++)
9294 tcc_free(s1
->sysinclude_paths
[i
]);
9295 tcc_free(s1
->sysinclude_paths
);
9300 int tcc_add_include_path(TCCState
*s1
, const char *pathname
)
9304 pathname1
= tcc_strdup(pathname
);
9305 dynarray_add((void ***)&s1
->include_paths
, &s1
->nb_include_paths
, pathname1
);
9309 int tcc_add_sysinclude_path(TCCState
*s1
, const char *pathname
)
9313 pathname1
= tcc_strdup(pathname
);
9314 dynarray_add((void ***)&s1
->sysinclude_paths
, &s1
->nb_sysinclude_paths
, pathname1
);
9318 static int tcc_add_file_internal(TCCState
*s1
, const char *filename
, int flags
)
9320 const char *ext
, *filename1
;
9323 BufferedFile
*saved_file
;
9325 /* find source file type with extension */
9326 filename1
= strrchr(filename
, '/');
9330 filename1
= filename
;
9331 ext
= strrchr(filename1
, '.');
9337 file
= tcc_open(s1
, filename
);
9339 if (flags
& AFF_PRINT_ERROR
) {
9340 error_noabort("file '%s' not found", filename
);
9346 if (!ext
|| !strcmp(ext
, "c")) {
9347 /* C file assumed */
9348 ret
= tcc_compile(s1
);
9350 #ifdef CONFIG_TCC_ASM
9351 if (!strcmp(ext
, "S")) {
9352 /* preprocessed assembler */
9353 ret
= tcc_assemble(s1
, 1);
9354 } else if (!strcmp(ext
, "s")) {
9355 /* non preprocessed assembler */
9356 ret
= tcc_assemble(s1
, 0);
9361 /* assume executable format: auto guess file type */
9362 if (read(fd
, &ehdr
, sizeof(ehdr
)) != sizeof(ehdr
)) {
9363 error_noabort("could not read header");
9366 lseek(fd
, 0, SEEK_SET
);
9368 if (ehdr
.e_ident
[0] == ELFMAG0
&&
9369 ehdr
.e_ident
[1] == ELFMAG1
&&
9370 ehdr
.e_ident
[2] == ELFMAG2
&&
9371 ehdr
.e_ident
[3] == ELFMAG3
) {
9372 file
->line_num
= 0; /* do not display line number if error */
9373 if (ehdr
.e_type
== ET_REL
) {
9374 ret
= tcc_load_object_file(s1
, fd
, 0);
9375 } else if (ehdr
.e_type
== ET_DYN
) {
9376 if (s1
->output_type
== TCC_OUTPUT_MEMORY
) {
9378 h
= dlopen(filename
, RTLD_GLOBAL
| RTLD_LAZY
);
9384 ret
= tcc_load_dll(s1
, fd
, filename
,
9385 (flags
& AFF_REFERENCED_DLL
) != 0);
9388 error_noabort("unrecognized ELF file");
9391 } else if (memcmp((char *)&ehdr
, ARMAG
, 8) == 0) {
9392 file
->line_num
= 0; /* do not display line number if error */
9393 ret
= tcc_load_archive(s1
, fd
);
9395 /* as GNU ld, consider it is an ld script if not recognized */
9396 ret
= tcc_load_ldscript(s1
);
9398 error_noabort("unrecognized file type");
9413 int tcc_add_file(TCCState
*s
, const char *filename
)
9415 return tcc_add_file_internal(s
, filename
, AFF_PRINT_ERROR
);
9418 int tcc_add_library_path(TCCState
*s
, const char *pathname
)
9422 pathname1
= tcc_strdup(pathname
);
9423 dynarray_add((void ***)&s
->library_paths
, &s
->nb_library_paths
, pathname1
);
9427 /* find and load a dll. Return non zero if not found */
9428 /* XXX: add '-rpath' option support ? */
9429 static int tcc_add_dll(TCCState
*s
, const char *filename
, int flags
)
9434 for(i
= 0; i
< s
->nb_library_paths
; i
++) {
9435 snprintf(buf
, sizeof(buf
), "%s/%s",
9436 s
->library_paths
[i
], filename
);
9437 if (tcc_add_file_internal(s
, buf
, flags
) == 0)
9443 /* the library name is the same as the argument of the '-l' option */
9444 int tcc_add_library(TCCState
*s
, const char *libraryname
)
9449 /* first we look for the dynamic library if not static linking */
9450 if (!s
->static_link
) {
9451 snprintf(buf
, sizeof(buf
), "lib%s.so", libraryname
);
9452 if (tcc_add_dll(s
, buf
, 0) == 0)
9456 /* then we look for the static library */
9457 for(i
= 0; i
< s
->nb_library_paths
; i
++) {
9458 snprintf(buf
, sizeof(buf
), "%s/lib%s.a",
9459 s
->library_paths
[i
], libraryname
);
9460 if (tcc_add_file_internal(s
, buf
, 0) == 0)
9466 int tcc_add_symbol(TCCState
*s
, const char *name
, unsigned long val
)
9468 add_elf_sym(symtab_section
, val
, 0,
9469 ELF32_ST_INFO(STB_GLOBAL
, STT_NOTYPE
),
9474 int tcc_set_output_type(TCCState
*s
, int output_type
)
9478 s
->output_type
= output_type
;
9481 /* default include paths */
9482 /* XXX: reverse order needed if -isystem support */
9483 tcc_add_sysinclude_path(s
, "/usr/local/include");
9484 tcc_add_sysinclude_path(s
, "/usr/include");
9485 snprintf(buf
, sizeof(buf
), "%s/include", tcc_lib_path
);
9486 tcc_add_sysinclude_path(s
, buf
);
9489 /* if bound checking, then add corresponding sections */
9490 #ifdef CONFIG_TCC_BCHECK
9491 if (do_bounds_check
) {
9493 tcc_define_symbol(s
, "__BOUNDS_CHECKING_ON", NULL
);
9494 /* create bounds sections */
9495 bounds_section
= new_section(s
, ".bounds",
9496 SHT_PROGBITS
, SHF_ALLOC
);
9497 lbounds_section
= new_section(s
, ".lbounds",
9498 SHT_PROGBITS
, SHF_ALLOC
);
9502 /* add debug sections */
9505 stab_section
= new_section(s
, ".stab", SHT_PROGBITS
, 0);
9506 stab_section
->sh_entsize
= sizeof(Stab_Sym
);
9507 stabstr_section
= new_section(s
, ".stabstr", SHT_STRTAB
, 0);
9508 put_elf_str(stabstr_section
, "");
9509 stab_section
->link
= stabstr_section
;
9510 /* put first entry */
9511 put_stabs("", 0, 0, 0, 0);
9514 /* add libc crt1/crti objects */
9515 if ((output_type
== TCC_OUTPUT_EXE
|| output_type
== TCC_OUTPUT_DLL
) &&
9517 if (output_type
!= TCC_OUTPUT_DLL
)
9518 tcc_add_file(s
, CONFIG_TCC_CRT_PREFIX
"/crt1.o");
9519 tcc_add_file(s
, CONFIG_TCC_CRT_PREFIX
"/crti.o");
9524 #define WD_ALL 0x0001 /* warning is activated when using -Wall */
9526 typedef struct WarningDef
{
9532 static const WarningDef warning_defs
[] = {
9533 { offsetof(TCCState
, warn_unsupported
), 0, "unsupported" },
9534 { offsetof(TCCState
, warn_write_strings
), 0, "write-strings" },
9535 { offsetof(TCCState
, warn_error
), 0, "error" },
9538 /* set/reset a warning */
9539 int tcc_set_warning(TCCState
*s
, const char *warning_name
, int value
)
9542 const WarningDef
*p
;
9543 if (!strcmp(warning_name
, "all")) {
9544 for(i
= 0, p
= warning_defs
; i
< countof(warning_defs
); i
++, p
++) {
9545 if (p
->flags
& WD_ALL
)
9546 *(int *)((uint8_t *)s
+ p
->offset
) = 1;
9549 for(i
= 0, p
= warning_defs
; i
< countof(warning_defs
); i
++, p
++) {
9550 if (!strcmp(warning_name
, p
->name
))
9555 *(int *)((uint8_t *)s
+ p
->offset
) = value
;
9560 #if !defined(LIBTCC)
9562 /* extract the basename of a file */
9563 static const char *tcc_basename(const char *name
)
9566 p
= strrchr(name
, '/');
9569 p
= strrchr(name
, '\\');
9578 static int64_t getclock_us(void)
9583 return (tb
.time
* 1000LL + tb
.millitm
) * 1000LL;
9586 gettimeofday(&tv
, NULL
);
9587 return tv
.tv_sec
* 1000000LL + tv
.tv_usec
;
9593 printf("tcc version " TCC_VERSION
" - Tiny C Compiler - Copyright (C) 2001-2003 Fabrice Bellard\n"
9594 "usage: tcc [-v] [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"
9595 " [-Wwarn] [-g] [-b] [-bt N] [-Ldir] [-llib] [-shared] [-static]\n"
9596 " [infile1 infile2...] [-run infile args...]\n"
9598 "General options:\n"
9599 " -v display current version\n"
9600 " -c compile only - generate an object file\n"
9601 " -o outfile set output filename\n"
9602 " -Bdir set tcc internal library path\n"
9603 " -bench output compilation statistics\n"
9604 " -run run compiled source\n"
9605 " -Wwarning set or reset (with 'no-' prefix) 'warning'\n"
9606 " -w disable all warnings\n"
9607 "Preprocessor options:\n"
9608 " -Idir add include path 'dir'\n"
9609 " -Dsym[=val] define 'sym' with value 'val'\n"
9610 " -Usym undefine 'sym'\n"
9612 " -Ldir add library path 'dir'\n"
9613 " -llib link with dynamic or static library 'lib'\n"
9614 " -shared generate a shared library\n"
9615 " -static static linking\n"
9616 " -rdynamic export all global symbols to dynamic linker\n"
9617 " -r relocatable output\n"
9618 "Debugger options:\n"
9619 " -g generate runtime debug info\n"
9620 #ifdef CONFIG_TCC_BCHECK
9621 " -b compile with built-in memory and bounds checker (implies -g)\n"
9623 " -bt N show N callers in stack traces\n"
9627 #define TCC_OPTION_HAS_ARG 0x0001
9628 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
9630 typedef struct TCCOption
{
9657 TCC_OPTION_nostdinc
,
9658 TCC_OPTION_nostdlib
,
9659 TCC_OPTION_print_search_dirs
,
9660 TCC_OPTION_rdynamic
,
9666 static const TCCOption tcc_options
[] = {
9667 { "h", TCC_OPTION_HELP
, 0 },
9668 { "?", TCC_OPTION_HELP
, 0 },
9669 { "I", TCC_OPTION_I
, TCC_OPTION_HAS_ARG
},
9670 { "D", TCC_OPTION_D
, TCC_OPTION_HAS_ARG
},
9671 { "U", TCC_OPTION_U
, TCC_OPTION_HAS_ARG
},
9672 { "L", TCC_OPTION_L
, TCC_OPTION_HAS_ARG
},
9673 { "B", TCC_OPTION_B
, TCC_OPTION_HAS_ARG
},
9674 { "l", TCC_OPTION_l
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
9675 { "bench", TCC_OPTION_bench
, 0 },
9676 { "bt", TCC_OPTION_bt
, TCC_OPTION_HAS_ARG
},
9677 #ifdef CONFIG_TCC_BCHECK
9678 { "b", TCC_OPTION_b
, 0 },
9680 { "g", TCC_OPTION_g
, 0 },
9681 { "c", TCC_OPTION_c
, 0 },
9682 { "static", TCC_OPTION_static
, 0 },
9683 { "shared", TCC_OPTION_shared
, 0 },
9684 { "o", TCC_OPTION_o
, TCC_OPTION_HAS_ARG
},
9685 { "run", TCC_OPTION_run
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
9686 { "rdynamic", TCC_OPTION_rdynamic
, 0 },
9687 { "r", TCC_OPTION_r
, 0 },
9688 { "W", TCC_OPTION_W
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
9689 { "O", TCC_OPTION_O
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
9690 { "m", TCC_OPTION_m
, TCC_OPTION_HAS_ARG
},
9691 { "f", TCC_OPTION_f
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
9692 { "nostdinc", TCC_OPTION_nostdinc
, 0 },
9693 { "nostdlib", TCC_OPTION_nostdlib
, 0 },
9694 { "print-search-dirs", TCC_OPTION_print_search_dirs
, 0 },
9695 { "v", TCC_OPTION_v
, 0 },
9696 { "w", TCC_OPTION_w
, 0 },
9700 /* convert 'str' into an array of space separated strings */
9701 static int expand_args(char ***pargv
, const char *str
)
9710 while (is_space(*str
))
9715 while (*str
!= '\0' && !is_space(*str
))
9718 arg
= tcc_malloc(len
+ 1);
9719 memcpy(arg
, s1
, len
);
9721 dynarray_add((void ***)&argv
, &argc
, arg
);
9727 static char **files
;
9728 static int nb_files
, nb_libraries
;
9729 static int multiple_files
;
9730 static int print_search_dirs
;
9731 static int output_type
;
9732 static int reloc_output
;
9733 static const char *outfile
;
9735 int parse_args(TCCState
*s
, int argc
, char **argv
)
9738 const TCCOption
*popt
;
9739 const char *optarg
, *p1
, *r1
;
9744 if (optind
>= argc
) {
9745 if (nb_files
== 0 && !print_search_dirs
)
9752 /* add a new file */
9753 dynarray_add((void ***)&files
, &nb_files
, r
);
9754 if (!multiple_files
) {
9756 /* argv[0] will be this file */
9760 /* find option in table (match only the first chars */
9765 error("invalid option -- '%s'", r
);
9778 if (popt
->flags
& TCC_OPTION_HAS_ARG
) {
9779 if (*r1
!= '\0' || (popt
->flags
& TCC_OPTION_NOSEP
)) {
9783 error("argument to '%s' is missing", r
);
9784 optarg
= argv
[optind
++];
9792 switch(popt
->index
) {
9793 case TCC_OPTION_HELP
:
9798 if (tcc_add_include_path(s
, optarg
) < 0)
9799 error("too many include paths");
9804 sym
= (char *)optarg
;
9805 value
= strchr(sym
, '=');
9810 tcc_define_symbol(s
, sym
, value
);
9814 tcc_undefine_symbol(s
, optarg
);
9817 tcc_add_library_path(s
, optarg
);
9820 /* set tcc utilities path (mainly for tcc development) */
9821 tcc_lib_path
= optarg
;
9824 dynarray_add((void ***)&files
, &nb_files
, r
);
9827 case TCC_OPTION_bench
:
9831 num_callers
= atoi(optarg
);
9833 #ifdef CONFIG_TCC_BCHECK
9835 do_bounds_check
= 1;
9844 output_type
= TCC_OUTPUT_OBJ
;
9846 case TCC_OPTION_static
:
9849 case TCC_OPTION_shared
:
9850 output_type
= TCC_OUTPUT_DLL
;
9857 /* generate a .o merging several output files */
9859 output_type
= TCC_OUTPUT_OBJ
;
9861 case TCC_OPTION_nostdinc
:
9864 case TCC_OPTION_nostdlib
:
9867 case TCC_OPTION_print_search_dirs
:
9868 print_search_dirs
= 1;
9870 case TCC_OPTION_run
:
9874 argc1
= expand_args(&argv1
, optarg
);
9876 parse_args(s
, argc1
, argv1
);
9879 output_type
= TCC_OUTPUT_MEMORY
;
9883 printf("tcc version %s\n", TCC_VERSION
);
9887 const char *p
= optarg
;
9890 if (p
[0] == 'n' && p
[1] == 'o' && p
[2] == '-') {
9894 if (tcc_set_warning(s
, p
, value
) < 0 && s
->warn_unsupported
)
9895 goto unsupported_option
;
9901 case TCC_OPTION_rdynamic
:
9905 if (s
->warn_unsupported
) {
9907 warning("unsupported option '%s'", r
);
9916 int main(int argc
, char **argv
)
9920 int nb_objfiles
, ret
, optind
;
9921 char objfilename
[1024];
9922 int64_t start_time
= 0;
9925 output_type
= TCC_OUTPUT_EXE
;
9932 print_search_dirs
= 0;
9934 optind
= parse_args(s
, argc
- 1, argv
+ 1) + 1;
9936 if (print_search_dirs
) {
9937 /* enough for Linux kernel */
9938 printf("install: %s/\n", tcc_lib_path
);
9942 nb_objfiles
= nb_files
- nb_libraries
;
9944 /* if outfile provided without other options, we output an
9946 if (outfile
&& output_type
== TCC_OUTPUT_MEMORY
)
9947 output_type
= TCC_OUTPUT_EXE
;
9949 /* check -c consistency : only single file handled. XXX: checks file type */
9950 if (output_type
== TCC_OUTPUT_OBJ
&& !reloc_output
) {
9951 /* accepts only a single input file */
9952 if (nb_objfiles
!= 1)
9953 error("cannot specify multiple files with -c");
9954 if (nb_libraries
!= 0)
9955 error("cannot specify libraries with -c");
9958 /* compute default outfile name */
9959 if (output_type
!= TCC_OUTPUT_MEMORY
&& !outfile
) {
9960 if (output_type
== TCC_OUTPUT_OBJ
&& !reloc_output
) {
9963 pstrcpy(objfilename
, sizeof(objfilename
) - 1,
9964 tcc_basename(files
[0]));
9965 /* add .o extension */
9966 ext
= strrchr(objfilename
, '.');
9968 goto default_outfile
;
9969 strcpy(ext
+ 1, "o");
9972 pstrcpy(objfilename
, sizeof(objfilename
), "a.out");
9974 outfile
= objfilename
;
9978 start_time
= getclock_us();
9981 tcc_set_output_type(s
, output_type
);
9983 /* compile or add each files or library */
9984 for(i
= 0;i
< nb_files
; i
++) {
9985 const char *filename
;
9987 filename
= files
[i
];
9988 if (filename
[0] == '-') {
9989 if (tcc_add_library(s
, filename
+ 2) < 0)
9990 error("cannot find %s", filename
);
9992 if (tcc_add_file(s
, filename
) < 0) {
9999 /* free all files */
10004 total_time
= (double)(getclock_us() - start_time
) / 1000000.0;
10005 if (total_time
< 0.001)
10006 total_time
= 0.001;
10007 if (total_bytes
< 1)
10009 printf("%d idents, %d lines, %d bytes, %0.3f s, %d lines/s, %0.1f MB/s\n",
10010 tok_ident
- TOK_IDENT
, total_lines
, total_bytes
,
10011 total_time
, (int)(total_lines
/ total_time
),
10012 total_bytes
/ total_time
/ 1000000.0);
10015 if (s
->output_type
!= TCC_OUTPUT_MEMORY
) {
10016 tcc_output_file(s
, outfile
);
10019 ret
= tcc_run(s
, argc
- optind
, argv
+ optind
);
10022 /* XXX: cannot do it with bound checking because of the malloc hooks */
10023 if (!do_bounds_check
)
10028 printf("memory: %d bytes, max = %d bytes\n", mem_cur_size
, mem_max_size
);