2 * TCC - Tiny C Compiler
4 * Copyright (c) 2001, 2002, 2003 Fabrice Bellard
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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
[64]; /* 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, only link in referenced objects from archive */
404 /* warning switches */
405 int warn_write_strings
;
406 int warn_unsupported
;
411 void (*error_func
)(void *opaque
, const char *msg
);
412 int error_set_jmp_enabled
;
413 jmp_buf error_jmp_buf
;
416 /* tiny assembler state */
419 /* see include_stack_ptr */
420 BufferedFile
*include_stack
[INCLUDE_STACK_SIZE
];
422 /* see ifdef_stack_ptr */
423 int ifdef_stack
[IFDEF_STACK_SIZE
];
426 /* The current value can be: */
427 #define VT_VALMASK 0x00ff
428 #define VT_CONST 0x00f0 /* constant in vc
429 (must be first non register value) */
430 #define VT_LLOCAL 0x00f1 /* lvalue, offset on stack */
431 #define VT_LOCAL 0x00f2 /* offset on stack */
432 #define VT_CMP 0x00f3 /* the value is stored in processor flags (in vc) */
433 #define VT_JMP 0x00f4 /* value is the consequence of jmp true (even) */
434 #define VT_JMPI 0x00f5 /* value is the consequence of jmp false (odd) */
435 #define VT_LVAL 0x0100 /* var is an lvalue */
436 #define VT_SYM 0x0200 /* a symbol value is added */
437 #define VT_MUSTCAST 0x0400 /* value must be casted to be correct (used for
438 char/short stored in integer registers) */
439 #define VT_MUSTBOUND 0x0800 /* bound checking must be done before
440 dereferencing value */
441 #define VT_BOUNDED 0x8000 /* value is bounded. The address of the
442 bounding function call point is in vc */
443 #define VT_LVAL_BYTE 0x1000 /* lvalue is a byte */
444 #define VT_LVAL_SHORT 0x2000 /* lvalue is a short */
445 #define VT_LVAL_UNSIGNED 0x4000 /* lvalue is unsigned */
446 #define VT_LVAL_TYPE (VT_LVAL_BYTE | VT_LVAL_SHORT | VT_LVAL_UNSIGNED)
449 #define VT_INT 0 /* integer type */
450 #define VT_BYTE 1 /* signed byte type */
451 #define VT_SHORT 2 /* short type */
452 #define VT_VOID 3 /* void type */
453 #define VT_PTR 4 /* pointer */
454 #define VT_ENUM 5 /* enum definition */
455 #define VT_FUNC 6 /* function type */
456 #define VT_STRUCT 7 /* struct/union definition */
457 #define VT_FLOAT 8 /* IEEE float */
458 #define VT_DOUBLE 9 /* IEEE double */
459 #define VT_LDOUBLE 10 /* IEEE long double */
460 #define VT_BOOL 11 /* ISOC99 boolean type */
461 #define VT_LLONG 12 /* 64 bit integer */
462 #define VT_LONG 13 /* long integer (NEVER USED as type, only
464 #define VT_BTYPE 0x000f /* mask for basic type */
465 #define VT_UNSIGNED 0x0010 /* unsigned type */
466 #define VT_ARRAY 0x0020 /* array type (also has VT_PTR) */
467 #define VT_BITFIELD 0x0040 /* bitfield modifier */
468 #define VT_CONSTANT 0x0800 /* const modifier */
469 #define VT_VOLATILE 0x1000 /* volatile modifier */
472 #define VT_EXTERN 0x00000080 /* extern definition */
473 #define VT_STATIC 0x00000100 /* static variable */
474 #define VT_TYPEDEF 0x00000200 /* typedef definition */
475 #define VT_INLINE 0x00000400 /* inline definition */
477 #define VT_STRUCT_SHIFT 16 /* shift for bitfield shift values */
479 /* type mask (except storage) */
480 #define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE)
481 #define VT_TYPE (~(VT_STORAGE))
485 /* warning: the following compare tokens depend on i386 asm code */
497 #define TOK_LAND 0xa0
501 #define TOK_MID 0xa3 /* inc/dec, to void constant */
503 #define TOK_UDIV 0xb0 /* unsigned division */
504 #define TOK_UMOD 0xb1 /* unsigned modulo */
505 #define TOK_PDIV 0xb2 /* fast division with undefined rounding for pointers */
506 #define TOK_CINT 0xb3 /* number in tokc */
507 #define TOK_CCHAR 0xb4 /* char constant in tokc */
508 #define TOK_STR 0xb5 /* pointer to string in tokc */
509 #define TOK_TWOSHARPS 0xb6 /* ## preprocessing token */
510 #define TOK_LCHAR 0xb7
511 #define TOK_LSTR 0xb8
512 #define TOK_CFLOAT 0xb9 /* float constant */
513 #define TOK_LINENUM 0xba /* line number info */
514 #define TOK_CDOUBLE 0xc0 /* double constant */
515 #define TOK_CLDOUBLE 0xc1 /* long double constant */
516 #define TOK_UMULL 0xc2 /* unsigned 32x32 -> 64 mul */
517 #define TOK_ADDC1 0xc3 /* add with carry generation */
518 #define TOK_ADDC2 0xc4 /* add with carry use */
519 #define TOK_SUBC1 0xc5 /* add with carry generation */
520 #define TOK_SUBC2 0xc6 /* add with carry use */
521 #define TOK_CUINT 0xc8 /* unsigned int constant */
522 #define TOK_CLLONG 0xc9 /* long long constant */
523 #define TOK_CULLONG 0xca /* unsigned long long constant */
524 #define TOK_ARROW 0xcb
525 #define TOK_DOTS 0xcc /* three dots */
526 #define TOK_SHR 0xcd /* unsigned shift right */
527 #define TOK_PPNUM 0xce /* preprocessor number */
529 #define TOK_SHL 0x01 /* shift left */
530 #define TOK_SAR 0x02 /* signed shift right */
532 /* assignement operators : normal operator or 0x80 */
533 #define TOK_A_MOD 0xa5
534 #define TOK_A_AND 0xa6
535 #define TOK_A_MUL 0xaa
536 #define TOK_A_ADD 0xab
537 #define TOK_A_SUB 0xad
538 #define TOK_A_DIV 0xaf
539 #define TOK_A_XOR 0xde
540 #define TOK_A_OR 0xfc
541 #define TOK_A_SHL 0x81
542 #define TOK_A_SAR 0x82
545 #define offsetof(type, field) ((size_t) &((type *)0)->field)
549 #define countof(tab) (sizeof(tab) / sizeof((tab)[0]))
552 /* WARNING: the content of this string encodes token numbers */
553 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";
555 #define TOK_EOF (-1) /* end of file */
556 #define TOK_LINEFEED 10 /* line feed */
558 /* all identificators and strings have token above that */
559 #define TOK_IDENT 256
561 /* only used for i386 asm opcodes definitions */
562 #define DEF_ASM(x) DEF(TOK_ASM_ ## x, #x)
565 DEF(TOK_ASM_ ## x ## b, #x "b") \
566 DEF(TOK_ASM_ ## x ## w, #x "w") \
567 DEF(TOK_ASM_ ## x ## l, #x "l") \
568 DEF(TOK_ASM_ ## x, #x)
571 DEF(TOK_ASM_ ## x ## w, #x "w") \
572 DEF(TOK_ASM_ ## x ## l, #x "l") \
573 DEF(TOK_ASM_ ## x, #x)
576 DEF(TOK_ASM_ ## f ## x ## s, "f" #x "s") \
577 DEF(TOK_ASM_ ## fi ## x ## l, "fi" #x "l") \
578 DEF(TOK_ASM_ ## f ## x ## l, "f" #x "l") \
579 DEF(TOK_ASM_ ## fi ## x ## s, "fi" #x "s")
582 DEF(TOK_ASM_ ## f ## x, "f" #x ) \
583 DEF(TOK_ASM_ ## f ## x ## p, "f" #x "p") \
586 #define DEF_ASMTEST(x) \
618 #define TOK_ASM_int TOK_INT
621 TOK_LAST
= TOK_IDENT
- 1,
622 #define DEF(id, str) id,
627 static const char tcc_keywords
[] =
628 #define DEF(id, str) str "\0"
633 #define TOK_UIDENT TOK_DEFINE
636 #define snprintf _snprintf
637 #define vsnprintf _vsnprintf
640 #if defined(WIN32) || defined(TCC_UCLIBC) || defined(__FreeBSD__)
641 /* currently incorrect */
642 long double strtold(const char *nptr
, char **endptr
)
644 return (long double)strtod(nptr
, endptr
);
646 float strtof(const char *nptr
, char **endptr
)
648 return (float)strtod(nptr
, endptr
);
651 /* XXX: need to define this to use them in non ISOC99 context */
652 extern float strtof (const char *__nptr
, char **__endptr
);
653 extern long double strtold (const char *__nptr
, char **__endptr
);
656 static char *pstrcpy(char *buf
, int buf_size
, const char *s
);
657 static char *pstrcat(char *buf
, int buf_size
, const char *s
);
659 static void next(void);
660 static void next_nomacro(void);
661 static void parse_expr_type(CType
*type
);
662 static void expr_type(CType
*type
);
663 static void unary_type(CType
*type
);
664 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
665 int case_reg
, int is_expr
);
666 static int expr_const(void);
667 static void expr_eq(void);
668 static void gexpr(void);
669 static void decl(int l
);
670 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
671 int first
, int size_only
);
672 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
673 int has_init
, int v
, int scope
);
675 void gv2(int rc1
, int rc2
);
676 void move_reg(int r
, int s
);
677 void save_regs(int n
);
678 void save_reg(int r
);
684 static void macro_subst(TokenString
*tok_str
, Sym
**nested_list
,
685 const int *macro_str
, int can_read_stream
);
686 int save_reg_forced(int r
);
688 void force_charshort_cast(int t
);
689 static void gen_cast(CType
*type
);
691 static Sym
*sym_find(int v
);
692 static Sym
*sym_push(int v
, CType
*type
, int r
, int c
);
695 static int type_size(CType
*type
, int *a
);
696 static inline CType
*pointed_type(CType
*type
);
697 static int pointed_size(CType
*type
);
698 static int lvalue_type(int t
);
699 static int parse_btype(CType
*type
, AttributeDef
*ad
);
700 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
);
701 static int is_compatible_types(CType
*type1
, CType
*type2
);
703 void error(const char *fmt
, ...);
705 void vset(CType
*type
, int r
, int v
);
706 void type_to_str(char *buf
, int buf_size
,
707 CType
*type
, const char *varstr
);
708 char *get_tok_str(int v
, CValue
*cv
);
709 static Sym
*get_sym_ref(CType
*type
, Section
*sec
,
710 unsigned long offset
, unsigned long size
);
711 static Sym
*external_global_sym(int v
, CType
*type
, int r
);
713 /* section generation */
714 static void section_realloc(Section
*sec
, unsigned long new_size
);
715 static void *section_ptr_add(Section
*sec
, unsigned long size
);
716 static void put_extern_sym(Sym
*sym
, Section
*section
,
717 unsigned long value
, unsigned long size
);
718 static void greloc(Section
*s
, Sym
*sym
, unsigned long addr
, int type
);
719 static int put_elf_str(Section
*s
, const char *sym
);
720 static int put_elf_sym(Section
*s
,
721 unsigned long value
, unsigned long size
,
722 int info
, int other
, int shndx
, const char *name
);
723 static int add_elf_sym(Section
*s
, unsigned long value
, unsigned long size
,
724 int info
, int sh_num
, const char *name
);
725 static void put_elf_reloc(Section
*symtab
, Section
*s
, unsigned long offset
,
726 int type
, int symbol
);
727 static void put_stabs(const char *str
, int type
, int other
, int desc
,
728 unsigned long value
);
729 static void put_stabs_r(const char *str
, int type
, int other
, int desc
,
730 unsigned long value
, Section
*sec
, int sym_index
);
731 static void put_stabn(int type
, int other
, int desc
, int value
);
732 static void put_stabd(int type
, int other
, int desc
);
733 static int tcc_add_dll(TCCState
*s
, const char *filename
, int flags
);
735 #define AFF_PRINT_ERROR 0x0001 /* print error if file not found */
736 #define AFF_REFERENCED_DLL 0x0002 /* load a referenced dll from another dll */
737 static int tcc_add_file_internal(TCCState
*s
, const char *filename
, int flags
);
741 #ifdef CONFIG_TCC_ASM
743 typedef struct ExprValue
{
748 #define MAX_ASM_OPERANDS 30
750 typedef struct ASMOperand
{
751 int id
; /* GCC 3 optionnal identifier (0 if number only supported */
753 char asm_str
[16]; /* computed asm string for operand */
754 SValue
*vt
; /* C value of the expression */
755 int ref_index
; /* if >= 0, gives reference to a output constraint */
756 int priority
; /* priority, used to assign registers */
757 int reg
; /* if >= 0, register number used for this operand */
758 int is_llong
; /* true if double register value */
761 static void asm_expr(TCCState
*s1
, ExprValue
*pe
);
762 static int asm_int_expr(TCCState
*s1
);
763 static int find_constraint(ASMOperand
*operands
, int nb_operands
,
764 const char *name
, const char **pp
);
766 static int tcc_assemble(TCCState
*s1
, int do_preprocess
);
770 static void asm_instr(void);
772 /* true if float/double/long double type */
773 static inline int is_float(int t
)
777 return bt
== VT_LDOUBLE
|| bt
== VT_DOUBLE
|| bt
== VT_FLOAT
;
780 #ifdef TCC_TARGET_I386
781 #include "i386-gen.c"
784 #ifdef CONFIG_TCC_STATIC
786 #define RTLD_LAZY 0x001
787 #define RTLD_NOW 0x002
788 #define RTLD_GLOBAL 0x100
789 #define RTLD_DEFAULT NULL
791 /* dummy function for profiling */
792 void *dlopen(const char *filename
, int flag
)
797 const char *dlerror(void)
802 typedef struct TCCSyms
{
807 #define TCCSYM(a) { #a, &a, },
809 /* add the symbol you want here if no dynamic linking is done */
810 static TCCSyms tcc_syms
[] = {
818 void *dlsym(void *handle
, const char *symbol
)
822 while (p
->str
!= NULL
) {
823 if (!strcmp(p
->str
, symbol
))
832 /********************************************************/
834 /* we use our own 'finite' function to avoid potential problems with
835 non standard math libs */
836 /* XXX: endianness dependent */
837 int ieee_finite(double d
)
840 return ((unsigned)((p
[1] | 0x800fffff) + 1)) >> 31;
843 /* copy a string and truncate it. */
844 static char *pstrcpy(char *buf
, int buf_size
, const char *s
)
851 q_end
= buf
+ buf_size
- 1;
863 /* strcat and truncate. */
864 static char *pstrcat(char *buf
, int buf_size
, const char *s
)
869 pstrcpy(buf
+ len
, buf_size
- len
, s
);
873 /* memory management */
879 static inline void tcc_free(void *ptr
)
882 mem_cur_size
-= malloc_usable_size(ptr
);
887 static void *tcc_malloc(unsigned long size
)
892 error("memory full");
894 mem_cur_size
+= malloc_usable_size(ptr
);
895 if (mem_cur_size
> mem_max_size
)
896 mem_max_size
= mem_cur_size
;
901 static void *tcc_mallocz(unsigned long size
)
904 ptr
= tcc_malloc(size
);
905 memset(ptr
, 0, size
);
909 static inline void *tcc_realloc(void *ptr
, unsigned long size
)
913 mem_cur_size
-= malloc_usable_size(ptr
);
915 ptr1
= realloc(ptr
, size
);
917 /* NOTE: count not correct if alloc error, but not critical */
918 mem_cur_size
+= malloc_usable_size(ptr1
);
919 if (mem_cur_size
> mem_max_size
)
920 mem_max_size
= mem_cur_size
;
925 static char *tcc_strdup(const char *str
)
928 ptr
= tcc_malloc(strlen(str
) + 1);
933 #define free(p) use_tcc_free(p)
934 #define malloc(s) use_tcc_malloc(s)
935 #define realloc(p, s) use_tcc_realloc(p, s)
937 static void dynarray_add(void ***ptab
, int *nb_ptr
, void *data
)
944 /* every power of two we double array size */
945 if ((nb
& (nb
- 1)) == 0) {
950 pp
= tcc_realloc(pp
, nb_alloc
* sizeof(void *));
952 error("memory full");
959 Section
*new_section(TCCState
*s1
, const char *name
, int sh_type
, int sh_flags
)
963 sec
= tcc_mallocz(sizeof(Section
));
964 pstrcpy(sec
->name
, sizeof(sec
->name
), name
);
965 sec
->sh_type
= sh_type
;
966 sec
->sh_flags
= sh_flags
;
973 sec
->sh_addralign
= 4;
976 sec
->sh_addralign
= 1;
979 sec
->sh_addralign
= 32; /* default conservative alignment */
983 /* only add section if not private */
984 if (!(sh_flags
& SHF_PRIVATE
)) {
985 sec
->sh_num
= s1
->nb_sections
;
986 dynarray_add((void ***)&s1
->sections
, &s1
->nb_sections
, sec
);
991 static void free_section(Section
*s
)
997 /* realloc section and set its content to zero */
998 static void section_realloc(Section
*sec
, unsigned long new_size
)
1001 unsigned char *data
;
1003 size
= sec
->data_allocated
;
1006 while (size
< new_size
)
1008 data
= tcc_realloc(sec
->data
, size
);
1010 error("memory full");
1011 memset(data
+ sec
->data_allocated
, 0, size
- sec
->data_allocated
);
1013 sec
->data_allocated
= size
;
1016 /* reserve at least 'size' bytes in section 'sec' from
1017 sec->data_offset. */
1018 static void *section_ptr_add(Section
*sec
, unsigned long size
)
1020 unsigned long offset
, offset1
;
1022 offset
= sec
->data_offset
;
1023 offset1
= offset
+ size
;
1024 if (offset1
> sec
->data_allocated
)
1025 section_realloc(sec
, offset1
);
1026 sec
->data_offset
= offset1
;
1027 return sec
->data
+ offset
;
1030 /* return a reference to a section, and create it if it does not
1032 Section
*find_section(TCCState
*s1
, const char *name
)
1036 for(i
= 1; i
< s1
->nb_sections
; i
++) {
1037 sec
= s1
->sections
[i
];
1038 if (!strcmp(name
, sec
->name
))
1041 /* sections are created as PROGBITS */
1042 return new_section(s1
, name
, SHT_PROGBITS
, SHF_ALLOC
);
1045 /* update sym->c so that it points to an external symbol in section
1046 'section' with value 'value' */
1047 static void put_extern_sym(Sym
*sym
, Section
*section
,
1048 unsigned long value
, unsigned long size
)
1050 int sym_type
, sym_bind
, sh_num
, info
;
1055 sh_num
= section
->sh_num
;
1059 if ((sym
->type
.t
& VT_BTYPE
) == VT_FUNC
)
1060 sym_type
= STT_FUNC
;
1062 sym_type
= STT_OBJECT
;
1063 if (sym
->type
.t
& VT_STATIC
)
1064 sym_bind
= STB_LOCAL
;
1066 sym_bind
= STB_GLOBAL
;
1068 name
= get_tok_str(sym
->v
, NULL
);
1069 #ifdef CONFIG_TCC_BCHECK
1070 if (do_bounds_check
) {
1073 /* XXX: avoid doing that for statics ? */
1074 /* if bound checking is activated, we change some function
1075 names by adding the "__bound" prefix */
1078 /* XXX: we rely only on malloc hooks */
1090 strcpy(buf
, "__bound_");
1097 info
= ELF32_ST_INFO(sym_bind
, sym_type
);
1098 sym
->c
= add_elf_sym(symtab_section
, value
, size
, info
, sh_num
, name
);
1100 esym
= &((Elf32_Sym
*)symtab_section
->data
)[sym
->c
];
1101 esym
->st_value
= value
;
1102 esym
->st_size
= size
;
1103 esym
->st_shndx
= sh_num
;
1107 /* add a new relocation entry to symbol 'sym' in section 's' */
1108 static void greloc(Section
*s
, Sym
*sym
, unsigned long offset
, int type
)
1111 put_extern_sym(sym
, NULL
, 0, 0);
1112 /* now we can add ELF relocation info */
1113 put_elf_reloc(symtab_section
, s
, offset
, type
, sym
->c
);
1116 static inline int isid(int c
)
1118 return (c
>= 'a' && c
<= 'z') ||
1119 (c
>= 'A' && c
<= 'Z') ||
1123 static inline int isnum(int c
)
1125 return c
>= '0' && c
<= '9';
1128 static inline int isoct(int c
)
1130 return c
>= '0' && c
<= '7';
1133 static inline int toup(int c
)
1135 if (c
>= 'a' && c
<= 'z')
1136 return c
- 'a' + 'A';
1141 static void strcat_vprintf(char *buf
, int buf_size
, const char *fmt
, va_list ap
)
1145 vsnprintf(buf
+ len
, buf_size
- len
, fmt
, ap
);
1148 static void strcat_printf(char *buf
, int buf_size
, const char *fmt
, ...)
1152 strcat_vprintf(buf
, buf_size
, fmt
, ap
);
1156 void error1(TCCState
*s1
, int is_warning
, const char *fmt
, va_list ap
)
1163 for(f
= s1
->include_stack
; f
< s1
->include_stack_ptr
; f
++)
1164 strcat_printf(buf
, sizeof(buf
), "In file included from %s:%d:\n",
1165 (*f
)->filename
, (*f
)->line_num
);
1166 if (file
->line_num
> 0) {
1167 strcat_printf(buf
, sizeof(buf
),
1168 "%s:%d: ", file
->filename
, file
->line_num
);
1170 strcat_printf(buf
, sizeof(buf
),
1171 "%s: ", file
->filename
);
1174 strcat_printf(buf
, sizeof(buf
),
1178 strcat_printf(buf
, sizeof(buf
), "warning: ");
1179 strcat_vprintf(buf
, sizeof(buf
), fmt
, ap
);
1181 if (!s1
->error_func
) {
1182 /* default case: stderr */
1183 fprintf(stderr
, "%s\n", buf
);
1185 s1
->error_func(s1
->error_opaque
, buf
);
1187 if (!is_warning
|| s1
->warn_error
)
1192 void tcc_set_error_func(TCCState
*s
, void *error_opaque
,
1193 void (*error_func
)(void *opaque
, const char *msg
))
1195 s
->error_opaque
= error_opaque
;
1196 s
->error_func
= error_func
;
1200 /* error without aborting current compilation */
1201 void error_noabort(const char *fmt
, ...)
1203 TCCState
*s1
= tcc_state
;
1207 error1(s1
, 0, fmt
, ap
);
1211 void error(const char *fmt
, ...)
1213 TCCState
*s1
= tcc_state
;
1217 error1(s1
, 0, fmt
, ap
);
1219 /* better than nothing: in some cases, we accept to handle errors */
1220 if (s1
->error_set_jmp_enabled
) {
1221 longjmp(s1
->error_jmp_buf
, 1);
1223 /* XXX: eliminate this someday */
1228 void expect(const char *msg
)
1230 error("%s expected", msg
);
1233 void warning(const char *fmt
, ...)
1235 TCCState
*s1
= tcc_state
;
1239 error1(s1
, 1, fmt
, ap
);
1246 error("'%c' expected", c
);
1250 static void test_lvalue(void)
1252 if (!(vtop
->r
& VT_LVAL
))
1256 /* allocate a new token */
1257 static TokenSym
*tok_alloc_new(TokenSym
**pts
, const char *str
, int len
)
1259 TokenSym
*ts
, **ptable
;
1262 if (tok_ident
>= SYM_FIRST_ANOM
)
1263 error("memory full");
1265 /* expand token table if needed */
1266 i
= tok_ident
- TOK_IDENT
;
1267 if ((i
% TOK_ALLOC_INCR
) == 0) {
1268 ptable
= tcc_realloc(table_ident
, (i
+ TOK_ALLOC_INCR
) * sizeof(TokenSym
*));
1270 error("memory full");
1271 table_ident
= ptable
;
1274 ts
= tcc_malloc(sizeof(TokenSym
) + len
);
1275 table_ident
[i
] = ts
;
1276 ts
->tok
= tok_ident
++;
1277 ts
->sym_define
= NULL
;
1278 ts
->sym_label
= NULL
;
1279 ts
->sym_struct
= NULL
;
1280 ts
->sym_identifier
= NULL
;
1282 ts
->hash_next
= NULL
;
1283 memcpy(ts
->str
, str
, len
);
1284 ts
->str
[len
] = '\0';
1289 #define TOK_HASH_INIT 1
1290 #define TOK_HASH_FUNC(h, c) ((h) * 263 + (c))
1292 /* find a token and add it if not found */
1293 static TokenSym
*tok_alloc(const char *str
, int len
)
1295 TokenSym
*ts
, **pts
;
1301 h
= TOK_HASH_FUNC(h
, ((unsigned char *)str
)[i
]);
1302 h
&= (TOK_HASH_SIZE
- 1);
1304 pts
= &hash_ident
[h
];
1309 if (ts
->len
== len
&& !memcmp(ts
->str
, str
, len
))
1311 pts
= &(ts
->hash_next
);
1313 return tok_alloc_new(pts
, str
, len
);
1316 /* CString handling */
1318 static void cstr_realloc(CString
*cstr
, int new_size
)
1323 size
= cstr
->size_allocated
;
1325 size
= 8; /* no need to allocate a too small first string */
1326 while (size
< new_size
)
1328 data
= tcc_realloc(cstr
->data_allocated
, size
);
1330 error("memory full");
1331 cstr
->data_allocated
= data
;
1332 cstr
->size_allocated
= size
;
1337 static void cstr_ccat(CString
*cstr
, int ch
)
1340 size
= cstr
->size
+ 1;
1341 if (size
> cstr
->size_allocated
)
1342 cstr_realloc(cstr
, size
);
1343 ((unsigned char *)cstr
->data
)[size
- 1] = ch
;
1347 static void cstr_cat(CString
*cstr
, const char *str
)
1359 /* add a wide char */
1360 static void cstr_wccat(CString
*cstr
, int ch
)
1363 size
= cstr
->size
+ sizeof(int);
1364 if (size
> cstr
->size_allocated
)
1365 cstr_realloc(cstr
, size
);
1366 *(int *)(((unsigned char *)cstr
->data
) + size
- sizeof(int)) = ch
;
1370 static void cstr_new(CString
*cstr
)
1372 memset(cstr
, 0, sizeof(CString
));
1375 /* free string and reset it to NULL */
1376 static void cstr_free(CString
*cstr
)
1378 tcc_free(cstr
->data_allocated
);
1382 #define cstr_reset(cstr) cstr_free(cstr)
1384 static CString
*cstr_dup(CString
*cstr1
)
1389 cstr
= tcc_malloc(sizeof(CString
));
1392 cstr
->size_allocated
= size
;
1393 cstr
->data_allocated
= tcc_malloc(size
);
1394 cstr
->data
= cstr
->data_allocated
;
1395 memcpy(cstr
->data_allocated
, cstr1
->data_allocated
, size
);
1399 /* XXX: unicode ? */
1400 static void add_char(CString
*cstr
, int c
)
1402 if (c
== '\'' || c
== '\"' || c
== '\\') {
1403 /* XXX: could be more precise if char or string */
1404 cstr_ccat(cstr
, '\\');
1406 if (c
>= 32 && c
<= 126) {
1409 cstr_ccat(cstr
, '\\');
1411 cstr_ccat(cstr
, 'n');
1413 cstr_ccat(cstr
, '0' + ((c
>> 6) & 7));
1414 cstr_ccat(cstr
, '0' + ((c
>> 3) & 7));
1415 cstr_ccat(cstr
, '0' + (c
& 7));
1420 /* XXX: buffer overflow */
1421 /* XXX: float tokens */
1422 char *get_tok_str(int v
, CValue
*cv
)
1424 static char buf
[STRING_MAX_SIZE
+ 1];
1425 static CString cstr_buf
;
1431 /* NOTE: to go faster, we give a fixed buffer for small strings */
1432 cstr_reset(&cstr_buf
);
1433 cstr_buf
.data
= buf
;
1434 cstr_buf
.size_allocated
= sizeof(buf
);
1440 /* XXX: not quite exact, but only useful for testing */
1441 sprintf(p
, "%u", cv
->ui
);
1445 /* XXX: not quite exact, but only useful for testing */
1446 sprintf(p
, "%Lu", cv
->ull
);
1450 cstr_ccat(&cstr_buf
, '\'');
1451 add_char(&cstr_buf
, cv
->i
);
1452 cstr_ccat(&cstr_buf
, '\'');
1453 cstr_ccat(&cstr_buf
, '\0');
1457 len
= cstr
->size
- 1;
1459 add_char(&cstr_buf
, ((unsigned char *)cstr
->data
)[i
]);
1460 cstr_ccat(&cstr_buf
, '\0');
1465 cstr_ccat(&cstr_buf
, '\"');
1467 len
= cstr
->size
- 1;
1469 add_char(&cstr_buf
, ((unsigned char *)cstr
->data
)[i
]);
1471 len
= (cstr
->size
/ sizeof(int)) - 1;
1473 add_char(&cstr_buf
, ((int *)cstr
->data
)[i
]);
1475 cstr_ccat(&cstr_buf
, '\"');
1476 cstr_ccat(&cstr_buf
, '\0');
1485 return strcpy(p
, "<<=");
1487 return strcpy(p
, ">>=");
1489 if (v
< TOK_IDENT
) {
1490 /* search in two bytes table */
1504 } else if (v
< tok_ident
) {
1505 return table_ident
[v
- TOK_IDENT
]->str
;
1506 } else if (v
>= SYM_FIRST_ANOM
) {
1507 /* special name for anonymous symbol */
1508 sprintf(p
, "L.%u", v
- SYM_FIRST_ANOM
);
1510 /* should never happen */
1515 return cstr_buf
.data
;
1518 /* push, without hashing */
1519 static Sym
*sym_push2(Sym
**ps
, int v
, int t
, int c
)
1522 s
= tcc_malloc(sizeof(Sym
));
1533 /* find a symbol and return its associated structure. 's' is the top
1534 of the symbol stack */
1535 static Sym
*sym_find2(Sym
*s
, int v
)
1545 /* structure lookup */
1546 static inline Sym
*struct_find(int v
)
1549 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
1551 return table_ident
[v
]->sym_struct
;
1554 /* find an identifier */
1555 static inline Sym
*sym_find(int v
)
1558 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
1560 return table_ident
[v
]->sym_identifier
;
1563 /* push a given symbol on the symbol stack */
1564 static Sym
*sym_push(int v
, CType
*type
, int r
, int c
)
1573 s
= sym_push2(ps
, v
, type
->t
, c
);
1574 s
->type
.ref
= type
->ref
;
1576 /* don't record fields or anonymous symbols */
1578 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
1579 /* record symbol in token array */
1580 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
1582 ps
= &ts
->sym_struct
;
1584 ps
= &ts
->sym_identifier
;
1591 /* push a global identifier */
1592 static Sym
*global_identifier_push(int v
, int t
, int c
)
1595 s
= sym_push2(&global_stack
, v
, t
, c
);
1596 /* don't record anonymous symbol */
1597 if (v
< SYM_FIRST_ANOM
) {
1598 ps
= &table_ident
[v
- TOK_IDENT
]->sym_identifier
;
1599 /* modify the top most local identifier, so that
1600 sym_identifier will point to 's' when popped */
1602 ps
= &(*ps
)->prev_tok
;
1609 /* pop symbols until top reaches 'b' */
1610 static void sym_pop(Sym
**ptop
, Sym
*b
)
1620 /* remove symbol in token array */
1622 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
1623 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
1625 ps
= &ts
->sym_struct
;
1627 ps
= &ts
->sym_identifier
;
1638 BufferedFile
*tcc_open(TCCState
*s1
, const char *filename
)
1643 fd
= open(filename
, O_RDONLY
);
1646 bf
= tcc_malloc(sizeof(BufferedFile
));
1652 bf
->buf_ptr
= bf
->buffer
;
1653 bf
->buf_end
= bf
->buffer
;
1654 bf
->buffer
[0] = CH_EOB
; /* put eob symbol */
1655 pstrcpy(bf
->filename
, sizeof(bf
->filename
), filename
);
1657 bf
->ifndef_macro
= 0;
1658 bf
->ifdef_stack_ptr
= s1
->ifdef_stack_ptr
;
1659 // printf("opening '%s'\n", filename);
1663 void tcc_close(BufferedFile
*bf
)
1665 total_lines
+= bf
->line_num
;
1670 /* fill input buffer and peek next char */
1671 static int tcc_peekc_slow(BufferedFile
*bf
)
1674 /* only tries to read if really end of buffer */
1675 if (bf
->buf_ptr
>= bf
->buf_end
) {
1677 #if defined(PARSE_DEBUG)
1682 len
= read(bf
->fd
, bf
->buffer
, len
);
1689 bf
->buf_ptr
= bf
->buffer
;
1690 bf
->buf_end
= bf
->buffer
+ len
;
1691 *bf
->buf_end
= CH_EOB
;
1693 if (bf
->buf_ptr
< bf
->buf_end
) {
1694 return bf
->buf_ptr
[0];
1696 bf
->buf_ptr
= bf
->buf_end
;
1701 /* return the current character, handling end of block if necessary
1703 static int handle_eob(void)
1705 return tcc_peekc_slow(file
);
1708 /* read next char from current input file and handle end of input buffer */
1709 static inline void inp(void)
1711 ch
= *(++(file
->buf_ptr
));
1712 /* end of buffer/file handling */
1717 /* handle '\[\r]\n' */
1718 static void handle_stray(void)
1720 while (ch
== '\\') {
1725 } else if (ch
== '\r') {
1733 error("stray '\\' in program");
1738 /* skip the stray and handle the \\n case. Output an error if
1739 incorrect char after the stray */
1740 static int handle_stray1(uint8_t *p
)
1744 if (p
>= file
->buf_end
) {
1761 /* handle just the EOB case, but not stray */
1762 #define PEEKC_EOB(c, p)\
1773 /* handle the complicated stray case */
1774 #define PEEKC(c, p)\
1779 c = handle_stray1(p);\
1784 /* input with '\[\r]\n' handling. Note that this function cannot
1785 handle other characters after '\', so you cannot call it inside
1786 strings or comments */
1787 static void minp(void)
1795 /* single line C++ comments */
1796 static uint8_t *parse_line_comment(uint8_t *p
)
1803 if (c
== '\n' || c
== CH_EOF
) {
1805 } else if (c
== '\\') {
1810 } else if (c
== '\r') {
1825 static uint8_t *parse_comment(uint8_t *p
)
1831 /* fast skip loop */
1834 if (c
== '\n' || c
== '*' || c
== '\\')
1838 if (c
== '\n' || c
== '*' || c
== '\\')
1842 /* now we can handle all the cases */
1846 } else if (c
== '*') {
1852 } else if (c
== '/') {
1853 goto end_of_comment
;
1854 } else if (c
== '\\') {
1859 /* skip '\[\r]\n', otherwise just skip the stray */
1865 } else if (c
== '\r') {
1882 /* stray, eob or eof */
1887 error("unexpected end of file in comment");
1888 } else if (c
== '\\') {
1900 /* space exlcuding newline */
1901 static inline int is_space(int ch
)
1903 return ch
== ' ' || ch
== '\t' || ch
== '\v' || ch
== '\f' || ch
== '\r';
1906 static inline void skip_spaces(void)
1908 while (is_space(ch
))
1912 /* parse a string without interpreting escapes */
1913 static uint8_t *parse_pp_string(uint8_t *p
,
1914 int sep
, CString
*str
)
1922 } else if (c
== '\\') {
1927 unterminated_string
:
1928 /* XXX: indicate line number of start of string */
1929 error("missing terminating %c character", sep
);
1930 } else if (c
== '\\') {
1931 /* escape : just skip \[\r]\n */
1936 } else if (c
== '\r') {
1939 expect("'\n' after '\r'");
1942 } else if (c
== CH_EOF
) {
1943 goto unterminated_string
;
1946 cstr_ccat(str
, '\\');
1952 } else if (c
== '\n') {
1955 } else if (c
== '\r') {
1958 cstr_ccat(str
, '\r');
1974 /* skip block of text until #else, #elif or #endif. skip also pairs of
1976 void preprocess_skip(void)
1978 int a
, start_of_line
, c
;
2005 } else if (c
== '\\') {
2006 /* XXX: incorrect: should not give an error */
2007 ch
= file
->buf_ptr
[0];
2015 p
= parse_pp_string(p
, c
, NULL
);
2024 p
= parse_comment(p
);
2025 } else if (ch
== '/') {
2026 p
= parse_line_comment(p
);
2032 if (start_of_line
) {
2037 (tok
== TOK_ELSE
|| tok
== TOK_ELIF
|| tok
== TOK_ENDIF
))
2039 if (tok
== TOK_IF
|| tok
== TOK_IFDEF
|| tok
== TOK_IFNDEF
)
2041 else if (tok
== TOK_ENDIF
)
2055 /* ParseState handling */
2057 /* XXX: currently, no include file info is stored. Thus, we cannot display
2058 accurate messages if the function or data definition spans multiple
2061 /* save current parse state in 's' */
2062 void save_parse_state(ParseState
*s
)
2064 s
->line_num
= file
->line_num
;
2065 s
->macro_ptr
= macro_ptr
;
2070 /* restore parse state from 's' */
2071 void restore_parse_state(ParseState
*s
)
2073 file
->line_num
= s
->line_num
;
2074 macro_ptr
= s
->macro_ptr
;
2079 /* return the number of additional 'ints' necessary to store the
2081 static inline int tok_ext_size(int t
)
2100 return LDOUBLE_SIZE
/ 4;
2106 /* token string handling */
2108 static inline void tok_str_new(TokenString
*s
)
2112 s
->allocated_len
= 0;
2113 s
->last_line_num
= -1;
2116 static void tok_str_free(int *str
)
2125 /* NOTE: we test zero separately so that GCC can generate a
2126 table for the following switch */
2141 /* XXX: use a macro to be portable on 64 bit ? */
2142 cstr
= (CString
*)p
[1];
2153 p
+= 1 + (LDOUBLE_SIZE
/ 4);
2163 static int *tok_str_realloc(TokenString
*s
)
2167 len
= s
->allocated_len
+ TOK_STR_ALLOC_INCR
;
2168 str
= tcc_realloc(s
->str
, len
* sizeof(int));
2170 error("memory full");
2171 s
->allocated_len
= len
;
2176 static void tok_str_add(TokenString
*s
, int t
)
2182 if (len
>= s
->allocated_len
)
2183 str
= tok_str_realloc(s
);
2188 static void tok_str_add2(TokenString
*s
, int t
, CValue
*cv
)
2195 /* allocate space for worst case */
2196 if (len
+ TOK_MAX_SIZE
> s
->allocated_len
)
2197 str
= tok_str_realloc(s
);
2206 str
[len
++] = cv
->tab
[0];
2211 str
[len
++] = (int)cstr_dup(cv
->cstr
);
2216 str
[len
++] = cv
->tab
[0];
2217 str
[len
++] = cv
->tab
[1];
2220 #if LDOUBLE_SIZE == 12
2221 str
[len
++] = cv
->tab
[0];
2222 str
[len
++] = cv
->tab
[1];
2223 str
[len
++] = cv
->tab
[2];
2225 #error add long double size support
2234 /* add the current parse token in token string 's' */
2235 static void tok_str_add_tok(TokenString
*s
)
2239 /* save line number info */
2240 if (file
->line_num
!= s
->last_line_num
) {
2241 s
->last_line_num
= file
->line_num
;
2242 cval
.i
= s
->last_line_num
;
2243 tok_str_add2(s
, TOK_LINENUM
, &cval
);
2245 tok_str_add2(s
, tok
, &tokc
);
2248 #if LDOUBLE_SIZE == 12
2249 #define LDOUBLE_GET(p, cv) \
2254 #error add long double size support
2258 /* get a token from an integer array and increment pointer
2259 accordingly. we code it as a macro to avoid pointer aliasing. */
2260 #define TOK_GET(t, p, cv) \
2282 case TOK_CLDOUBLE: \
2283 LDOUBLE_GET(p, cv); \
2284 p += LDOUBLE_SIZE / 4; \
2291 /* defines handling */
2292 static inline void define_push(int v
, int macro_type
, int *str
, Sym
*first_arg
)
2296 s
= sym_push2(&define_stack
, v
, macro_type
, (int)str
);
2297 s
->next
= first_arg
;
2298 table_ident
[v
- TOK_IDENT
]->sym_define
= s
;
2301 /* undefined a define symbol. Its name is just set to zero */
2302 static void define_undef(Sym
*s
)
2306 if (v
>= TOK_IDENT
&& v
< tok_ident
)
2307 table_ident
[v
- TOK_IDENT
]->sym_define
= NULL
;
2311 static inline Sym
*define_find(int v
)
2314 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
2316 return table_ident
[v
]->sym_define
;
2319 /* free define stack until top reaches 'b' */
2320 static void free_defines(Sym
*b
)
2328 /* do not free args or predefined defines */
2330 tok_str_free((int *)top
->c
);
2332 if (v
>= TOK_IDENT
&& v
< tok_ident
)
2333 table_ident
[v
- TOK_IDENT
]->sym_define
= NULL
;
2341 static Sym
*label_find(int v
)
2344 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
2346 return table_ident
[v
]->sym_label
;
2349 static Sym
*label_push(Sym
**ptop
, int v
, int flags
)
2352 s
= sym_push2(ptop
, v
, 0, 0);
2354 ps
= &table_ident
[v
- TOK_IDENT
]->sym_label
;
2355 if (ptop
== &global_label_stack
) {
2356 /* modify the top most local identifier, so that
2357 sym_identifier will point to 's' when popped */
2359 ps
= &(*ps
)->prev_tok
;
2366 /* pop labels until element last is reached. Look if any labels are
2367 undefined. Define symbols if '&&label' was used. */
2368 static void label_pop(Sym
**ptop
, Sym
*slast
)
2371 for(s
= *ptop
; s
!= slast
; s
= s1
) {
2373 if (s
->r
== LABEL_DECLARED
) {
2374 warning("label '%s' declared but not used", get_tok_str(s
->v
, NULL
));
2375 } else if (s
->r
== LABEL_FORWARD
) {
2376 error("label '%s' used but not defined",
2377 get_tok_str(s
->v
, NULL
));
2380 /* define corresponding symbol. A size of
2382 put_extern_sym(s
, cur_text_section
, (long)s
->next
, 1);
2386 table_ident
[s
->v
- TOK_IDENT
]->sym_label
= s
->prev_tok
;
2392 /* eval an expression for #if/#elif */
2393 static int expr_preprocess(void)
2399 while (tok
!= TOK_LINEFEED
&& tok
!= TOK_EOF
) {
2400 next(); /* do macro subst */
2401 if (tok
== TOK_DEFINED
) {
2406 c
= define_find(tok
) != 0;
2411 } else if (tok
>= TOK_IDENT
) {
2412 /* if undefined macro */
2416 tok_str_add_tok(&str
);
2418 tok_str_add(&str
, -1); /* simulate end of file */
2419 tok_str_add(&str
, 0);
2420 /* now evaluate C constant expression */
2421 macro_ptr
= str
.str
;
2425 tok_str_free(str
.str
);
2429 #if defined(PARSE_DEBUG) || defined(PP_DEBUG)
2430 static void tok_print(int *str
)
2436 TOK_GET(t
, str
, cval
);
2439 printf(" %s", get_tok_str(t
, &cval
));
2445 /* parse after #define */
2446 static void parse_define(void)
2448 Sym
*s
, *first
, **ps
;
2449 int v
, t
, varg
, is_vaargs
, c
;
2454 error("invalid macro name '%s'", get_tok_str(tok
, &tokc
));
2455 /* XXX: should check if same macro (ANSI) */
2458 /* '(' must be just after macro definition for MACRO_FUNC */
2459 c
= file
->buf_ptr
[0];
2461 c
= handle_stray1(file
->buf_ptr
);
2466 while (tok
!= ')') {
2470 if (varg
== TOK_DOTS
) {
2471 varg
= TOK___VA_ARGS__
;
2473 } else if (tok
== TOK_DOTS
&& gnu_ext
) {
2477 if (varg
< TOK_IDENT
)
2478 error("badly punctuated parameter list");
2479 s
= sym_push2(&define_stack
, varg
| SYM_FIELD
, is_vaargs
, 0);
2490 /* EOF testing necessary for '-D' handling */
2491 while (tok
!= TOK_LINEFEED
&& tok
!= TOK_EOF
) {
2492 tok_str_add2(&str
, tok
, &tokc
);
2495 tok_str_add(&str
, 0);
2497 printf("define %s %d: ", get_tok_str(v
, NULL
), t
);
2500 define_push(v
, t
, str
.str
, first
);
2503 /* XXX: use a token or a hash table to accelerate matching ? */
2504 static CachedInclude
*search_cached_include(TCCState
*s1
,
2505 int type
, const char *filename
)
2510 for(i
= 0;i
< s1
->nb_cached_includes
; i
++) {
2511 e
= s1
->cached_includes
[i
];
2512 if (e
->type
== type
&& !strcmp(e
->filename
, filename
))
2518 static inline void add_cached_include(TCCState
*s1
, int type
,
2519 const char *filename
, int ifndef_macro
)
2523 if (search_cached_include(s1
, type
, filename
))
2526 printf("adding cached '%s' %s\n", filename
, get_tok_str(ifndef_macro
, NULL
));
2528 e
= tcc_malloc(sizeof(CachedInclude
) + strlen(filename
));
2532 strcpy(e
->filename
, filename
);
2533 e
->ifndef_macro
= ifndef_macro
;
2534 dynarray_add((void ***)&s1
->cached_includes
, &s1
->nb_cached_includes
, e
);
2537 /* is_bof is true if first non space token at beginning of file */
2538 static void preprocess(int is_bof
)
2540 TCCState
*s1
= tcc_state
;
2541 int size
, i
, c
, n
, saved_parse_flags
;
2542 char buf
[1024], *q
, *p
;
2548 saved_parse_flags
= parse_flags
;
2549 parse_flags
= PARSE_FLAG_PREPROCESS
| PARSE_FLAG_TOK_NUM
|
2550 PARSE_FLAG_LINEFEED
;
2560 s
= define_find(tok
);
2561 /* undefine symbol by putting an invalid name */
2566 ch
= file
->buf_ptr
[0];
2567 /* XXX: incorrect if comments : use next_nomacro with a special mode */
2572 } else if (ch
== '\"') {
2575 /* XXX: better stray handling */
2578 while (ch
!= c
&& ch
!= '\n' && ch
!= CH_EOF
) {
2579 if ((q
- buf
) < sizeof(buf
) - 1)
2586 /* eat all spaces and comments after include */
2587 /* XXX: slightly incorrect */
2588 while (ch1
!= '\n' && ch1
!= CH_EOF
)
2592 /* computed #include : either we have only strings or
2593 we have anything enclosed in '<>' */
2596 if (tok
== TOK_STR
) {
2597 while (tok
!= TOK_LINEFEED
) {
2598 if (tok
!= TOK_STR
) {
2600 error("'#include' expects \"FILENAME\" or <FILENAME>");
2602 pstrcat(buf
, sizeof(buf
), (char *)tokc
.cstr
->data
);
2608 while (tok
!= TOK_LINEFEED
) {
2609 pstrcat(buf
, sizeof(buf
), get_tok_str(tok
, &tokc
));
2613 /* check syntax and remove '<>' */
2614 if (len
< 2 || buf
[0] != '<' || buf
[len
- 1] != '>')
2615 goto include_syntax
;
2616 memmove(buf
, buf
+ 1, len
- 2);
2617 buf
[len
- 2] = '\0';
2622 e
= search_cached_include(s1
, c
, buf
);
2623 if (e
&& define_find(e
->ifndef_macro
)) {
2624 /* no need to parse the include because the 'ifndef macro'
2627 printf("%s: skipping %s\n", file
->filename
, buf
);
2631 /* first search in current dir if "header.h" */
2633 p
= strrchr(file
->filename
, '/');
2635 size
= p
+ 1 - file
->filename
;
2636 if (size
> sizeof(buf1
) - 1)
2637 size
= sizeof(buf1
) - 1;
2638 memcpy(buf1
, file
->filename
, size
);
2640 pstrcat(buf1
, sizeof(buf1
), buf
);
2641 f
= tcc_open(s1
, buf1
);
2645 if (s1
->include_stack_ptr
>= s1
->include_stack
+ INCLUDE_STACK_SIZE
)
2646 error("#include recursion too deep");
2647 /* now search in all the include paths */
2648 n
= s1
->nb_include_paths
+ s1
->nb_sysinclude_paths
;
2649 for(i
= 0; i
< n
; i
++) {
2651 if (i
< s1
->nb_include_paths
)
2652 path
= s1
->include_paths
[i
];
2654 path
= s1
->sysinclude_paths
[i
- s1
->nb_include_paths
];
2655 pstrcpy(buf1
, sizeof(buf1
), path
);
2656 pstrcat(buf1
, sizeof(buf1
), "/");
2657 pstrcat(buf1
, sizeof(buf1
), buf
);
2658 f
= tcc_open(s1
, buf1
);
2662 error("include file '%s' not found", buf
);
2666 printf("%s: including %s\n", file
->filename
, buf1
);
2669 pstrcpy(f
->inc_filename
, sizeof(f
->inc_filename
), buf
);
2670 /* push current file in stack */
2671 /* XXX: fix current line init */
2672 *s1
->include_stack_ptr
++ = file
;
2674 /* add include file debug info */
2676 put_stabs(file
->filename
, N_BINCL
, 0, 0, 0);
2678 tok_flags
|= TOK_FLAG_BOF
| TOK_FLAG_BOL
;
2679 ch
= file
->buf_ptr
[0];
2687 c
= expr_preprocess();
2693 if (tok
< TOK_IDENT
)
2694 error("invalid argument for '#if%sdef'", c
? "n" : "");
2698 printf("#ifndef %s\n", get_tok_str(tok
, NULL
));
2700 file
->ifndef_macro
= tok
;
2703 c
= (define_find(tok
) != 0) ^ c
;
2705 if (s1
->ifdef_stack_ptr
>= s1
->ifdef_stack
+ IFDEF_STACK_SIZE
)
2706 error("memory full");
2707 *s1
->ifdef_stack_ptr
++ = c
;
2710 if (s1
->ifdef_stack_ptr
== s1
->ifdef_stack
)
2711 error("#else without matching #if");
2712 if (s1
->ifdef_stack_ptr
[-1] & 2)
2713 error("#else after #else");
2714 c
= (s1
->ifdef_stack_ptr
[-1] ^= 3);
2717 if (s1
->ifdef_stack_ptr
== s1
->ifdef_stack
)
2718 error("#elif without matching #if");
2719 c
= s1
->ifdef_stack_ptr
[-1];
2721 error("#elif after #else");
2722 /* last #if/#elif expression was true: we skip */
2725 c
= expr_preprocess();
2726 s1
->ifdef_stack_ptr
[-1] = c
;
2736 if (s1
->ifdef_stack_ptr
<= file
->ifdef_stack_ptr
)
2737 error("#endif without matching #if");
2738 s1
->ifdef_stack_ptr
--;
2739 /* '#ifndef macro' was at the start of file. Now we check if
2740 an '#endif' is exactly at the end of file */
2741 if (file
->ifndef_macro
&&
2742 s1
->ifdef_stack_ptr
== file
->ifdef_stack_ptr
) {
2743 file
->ifndef_macro_saved
= file
->ifndef_macro
;
2744 /* need to set to zero to avoid false matches if another
2745 #ifndef at middle of file */
2746 file
->ifndef_macro
= 0;
2747 while (tok
!= TOK_LINEFEED
)
2749 tok_flags
|= TOK_FLAG_ENDIF
;
2755 if (tok
!= TOK_CINT
)
2757 file
->line_num
= tokc
.i
- 1; /* the line number will be incremented after */
2759 if (tok
!= TOK_LINEFEED
) {
2762 pstrcpy(file
->filename
, sizeof(file
->filename
),
2763 (char *)tokc
.cstr
->data
);
2769 ch
= file
->buf_ptr
[0];
2772 while (ch
!= '\n' && ch
!= CH_EOF
) {
2773 if ((q
- buf
) < sizeof(buf
) - 1)
2779 error("#error %s", buf
);
2781 warning("#warning %s", buf
);
2787 if (tok
== TOK_LINEFEED
|| tok
== '!' || tok
== TOK_CINT
) {
2788 /* '!' is ignored to allow C scripts. numbers are ignored
2789 to emulate cpp behaviour */
2791 error("invalid preprocessing directive #%s", get_tok_str(tok
, &tokc
));
2795 /* ignore other preprocess commands or #! for C scripts */
2796 while (tok
!= TOK_LINEFEED
)
2799 parse_flags
= saved_parse_flags
;
2802 /* evaluate escape codes in a string. */
2803 static void parse_escape_string(CString
*outstr
, const uint8_t *buf
, int is_long
)
2818 case '0': case '1': case '2': case '3':
2819 case '4': case '5': case '6': case '7':
2820 /* at most three octal digits */
2825 n
= n
* 8 + c
- '0';
2829 n
= n
* 8 + c
- '0';
2834 goto add_char_nonext
;
2840 if (c
>= 'a' && c
<= 'f')
2842 else if (c
>= 'A' && c
<= 'F')
2852 goto add_char_nonext
;
2876 goto invalid_escape
;
2886 error("invalid escaped char");
2892 cstr_ccat(outstr
, c
);
2894 cstr_wccat(outstr
, c
);
2896 /* add a trailing '\0' */
2898 cstr_ccat(outstr
, '\0');
2900 cstr_wccat(outstr
, '\0');
2903 /* we use 64 bit numbers */
2906 /* bn = (bn << shift) | or_val */
2907 void bn_lshift(unsigned int *bn
, int shift
, int or_val
)
2911 for(i
=0;i
<BN_SIZE
;i
++) {
2913 bn
[i
] = (v
<< shift
) | or_val
;
2914 or_val
= v
>> (32 - shift
);
2918 void bn_zero(unsigned int *bn
)
2921 for(i
=0;i
<BN_SIZE
;i
++) {
2926 /* parse number in null terminated string 'p' and return it in the
2928 void parse_number(const char *p
)
2930 int b
, t
, shift
, frac_bits
, s
, exp_val
, ch
;
2932 unsigned int bn
[BN_SIZE
];
2943 goto float_frac_parse
;
2944 } else if (t
== '0') {
2945 if (ch
== 'x' || ch
== 'X') {
2949 } else if (tcc_ext
&& (ch
== 'b' || ch
== 'B')) {
2955 /* parse all digits. cannot check octal numbers at this stage
2956 because of floating point constants */
2958 if (ch
>= 'a' && ch
<= 'f')
2960 else if (ch
>= 'A' && ch
<= 'F')
2968 if (q
>= token_buf
+ STRING_MAX_SIZE
) {
2970 error("number too long");
2976 ((ch
== 'e' || ch
== 'E') && b
== 10) ||
2977 ((ch
== 'p' || ch
== 'P') && (b
== 16 || b
== 2))) {
2979 /* NOTE: strtox should support that for hexa numbers, but
2980 non ISOC99 libcs do not support it, so we prefer to do
2982 /* hexadecimal or binary floats */
2983 /* XXX: handle overflows */
2995 } else if (t
>= 'a') {
2997 } else if (t
>= 'A') {
3002 bn_lshift(bn
, shift
, t
);
3009 if (t
>= 'a' && t
<= 'f') {
3011 } else if (t
>= 'A' && t
<= 'F') {
3013 } else if (t
>= '0' && t
<= '9') {
3019 error("invalid digit");
3020 bn_lshift(bn
, shift
, t
);
3025 if (ch
!= 'p' && ch
!= 'P')
3032 } else if (ch
== '-') {
3036 if (ch
< '0' || ch
> '9')
3037 expect("exponent digits");
3038 while (ch
>= '0' && ch
<= '9') {
3039 exp_val
= exp_val
* 10 + ch
- '0';
3042 exp_val
= exp_val
* s
;
3044 /* now we can generate the number */
3045 /* XXX: should patch directly float number */
3046 d
= (double)bn
[1] * 4294967296.0 + (double)bn
[0];
3047 d
= ldexp(d
, exp_val
- frac_bits
);
3052 /* float : should handle overflow */
3054 } else if (t
== 'L') {
3057 /* XXX: not large enough */
3058 tokc
.ld
= (long double)d
;
3064 /* decimal floats */
3066 if (q
>= token_buf
+ STRING_MAX_SIZE
)
3071 while (ch
>= '0' && ch
<= '9') {
3072 if (q
>= token_buf
+ STRING_MAX_SIZE
)
3078 if (ch
== 'e' || ch
== 'E') {
3079 if (q
>= token_buf
+ STRING_MAX_SIZE
)
3083 if (ch
== '-' || ch
== '+') {
3084 if (q
>= token_buf
+ STRING_MAX_SIZE
)
3089 if (ch
< '0' || ch
> '9')
3090 expect("exponent digits");
3091 while (ch
>= '0' && ch
<= '9') {
3092 if (q
>= token_buf
+ STRING_MAX_SIZE
)
3104 tokc
.f
= strtof(token_buf
, NULL
);
3105 } else if (t
== 'L') {
3108 tokc
.ld
= strtold(token_buf
, NULL
);
3111 tokc
.d
= strtod(token_buf
, NULL
);
3115 unsigned long long n
, n1
;
3118 /* integer number */
3121 if (b
== 10 && *q
== '0') {
3128 /* no need for checks except for base 10 / 8 errors */
3131 } else if (t
>= 'a') {
3133 } else if (t
>= 'A') {
3138 error("invalid digit");
3142 /* detect overflow */
3143 /* XXX: this test is not reliable */
3145 error("integer constant overflow");
3148 /* XXX: not exactly ANSI compliant */
3149 if ((n
& 0xffffffff00000000LL
) != 0) {
3154 } else if (n
> 0x7fffffff) {
3165 error("three 'l's in integer constant");
3168 if (tok
== TOK_CINT
)
3170 else if (tok
== TOK_CUINT
)
3174 } else if (t
== 'U') {
3176 error("two 'u's in integer constant");
3178 if (tok
== TOK_CINT
)
3180 else if (tok
== TOK_CLLONG
)
3187 if (tok
== TOK_CINT
|| tok
== TOK_CUINT
)
3195 #define PARSE2(c1, tok1, c2, tok2) \
3206 /* return next token without macro substitution */
3207 static inline void next_nomacro1(void)
3227 /* first look if it is in fact an end of buffer */
3228 if (p
>= file
->buf_end
) {
3232 if (p
>= file
->buf_end
)
3245 TCCState
*s1
= tcc_state
;
3246 if (parse_flags
& PARSE_FLAG_LINEFEED
) {
3248 } else if (s1
->include_stack_ptr
== s1
->include_stack
||
3249 !(parse_flags
& PARSE_FLAG_PREPROCESS
)) {
3250 /* no include left : end of file. */
3253 /* pop include file */
3255 /* test if previous '#endif' was after a #ifdef at
3257 if (tok_flags
& TOK_FLAG_ENDIF
) {
3259 printf("#endif %s\n", get_tok_str(file
->ifndef_macro_saved
, NULL
));
3261 add_cached_include(s1
, file
->inc_type
, file
->inc_filename
,
3262 file
->ifndef_macro_saved
);
3265 /* add end of include file debug info */
3267 put_stabd(N_EINCL
, 0, 0);
3269 /* pop include stack */
3271 s1
->include_stack_ptr
--;
3272 file
= *s1
->include_stack_ptr
;
3280 if (parse_flags
& PARSE_FLAG_LINEFEED
) {
3284 tok_flags
|= TOK_FLAG_BOL
;
3293 if ((tok_flags
& TOK_FLAG_BOL
) &&
3294 (parse_flags
& PARSE_FLAG_PREPROCESS
)) {
3296 preprocess(tok_flags
& TOK_FLAG_BOF
);
3302 tok
= TOK_TWOSHARPS
;
3309 case 'a': case 'b': case 'c': case 'd':
3310 case 'e': case 'f': case 'g': case 'h':
3311 case 'i': case 'j': case 'k': case 'l':
3312 case 'm': case 'n': case 'o': case 'p':
3313 case 'q': case 'r': case 's': case 't':
3314 case 'u': case 'v': case 'w': case 'x':
3316 case 'A': case 'B': case 'C': case 'D':
3317 case 'E': case 'F': case 'G': case 'H':
3318 case 'I': case 'J': case 'K':
3319 case 'M': case 'N': case 'O': case 'P':
3320 case 'Q': case 'R': case 'S': case 'T':
3321 case 'U': case 'V': case 'W': case 'X':
3327 h
= TOK_HASH_FUNC(h
, c
);
3331 if (!isidnum_table
[c
])
3333 h
= TOK_HASH_FUNC(h
, c
);
3340 /* fast case : no stray found, so we have the full token
3341 and we have already hashed it */
3343 h
&= (TOK_HASH_SIZE
- 1);
3344 pts
= &hash_ident
[h
];
3349 if (ts
->len
== len
&& !memcmp(ts
->str
, p1
, len
))
3351 pts
= &(ts
->hash_next
);
3353 ts
= tok_alloc_new(pts
, p1
, len
);
3357 cstr_reset(&tokcstr
);
3360 cstr_ccat(&tokcstr
, *p1
);
3366 while (isidnum_table
[c
]) {
3367 cstr_ccat(&tokcstr
, c
);
3370 ts
= tok_alloc(tokcstr
.data
, tokcstr
.size
);
3376 if (t
!= '\\' && t
!= '\'' && t
!= '\"') {
3378 goto parse_ident_fast
;
3381 if (c
== '\'' || c
== '\"') {
3385 cstr_reset(&tokcstr
);
3386 cstr_ccat(&tokcstr
, 'L');
3387 goto parse_ident_slow
;
3391 case '0': case '1': case '2': case '3':
3392 case '4': case '5': case '6': case '7':
3395 cstr_reset(&tokcstr
);
3396 /* after the first digit, accept digits, alpha, '.' or sign if
3397 prefixed by 'eEpP' */
3401 cstr_ccat(&tokcstr
, c
);
3403 if (!(isnum(c
) || isid(c
) || c
== '.' ||
3404 ((c
== '+' || c
== '-') &&
3405 (t
== 'e' || t
== 'E' || t
== 'p' || t
== 'P'))))
3408 /* We add a trailing '\0' to ease parsing */
3409 cstr_ccat(&tokcstr
, '\0');
3410 tokc
.cstr
= &tokcstr
;
3414 /* special dot handling because it can also start a number */
3417 cstr_reset(&tokcstr
);
3418 cstr_ccat(&tokcstr
, '.');
3420 } else if (c
== '.') {
3440 /* parse the string */
3442 p
= parse_pp_string(p
, sep
, &str
);
3443 cstr_ccat(&str
, '\0');
3445 /* eval the escape (should be done as TOK_PPNUM) */
3446 cstr_reset(&tokcstr
);
3447 parse_escape_string(&tokcstr
, str
.data
, is_long
);
3452 /* XXX: make it portable */
3456 char_size
= sizeof(int);
3457 if (tokcstr
.size
<= char_size
)
3458 error("empty character constant");
3459 if (tokcstr
.size
> 2 * char_size
)
3460 warning("multi-character character constant");
3462 tokc
.i
= *(int8_t *)tokcstr
.data
;
3465 tokc
.i
= *(int *)tokcstr
.data
;
3469 tokc
.cstr
= &tokcstr
;
3483 } else if (c
== '<') {
3501 } else if (c
== '>') {
3519 } else if (c
== '=') {
3532 } else if (c
== '=') {
3545 } else if (c
== '=') {
3558 } else if (c
== '=') {
3561 } else if (c
== '>') {
3569 PARSE2('!', '!', '=', TOK_NE
)
3570 PARSE2('=', '=', '=', TOK_EQ
)
3571 PARSE2('*', '*', '=', TOK_A_MUL
)
3572 PARSE2('%', '%', '=', TOK_A_MOD
)
3573 PARSE2('^', '^', '=', TOK_A_XOR
)
3575 /* comments or operator */
3579 p
= parse_comment(p
);
3581 } else if (c
== '/') {
3582 p
= parse_line_comment(p
);
3584 } else if (c
== '=') {
3604 case '$': /* only used in assembler */
3609 error("unrecognized character \\x%02x", c
);
3614 #if defined(PARSE_DEBUG)
3615 printf("token = %s\n", get_tok_str(tok
, &tokc
));
3619 /* return next token without macro substitution. Can read input from
3621 static void next_nomacro(void)
3627 TOK_GET(tok
, macro_ptr
, tokc
);
3628 if (tok
== TOK_LINENUM
) {
3629 file
->line_num
= tokc
.i
;
3638 /* substitute args in macro_str and return allocated string */
3639 static int *macro_arg_subst(Sym
**nested_list
, int *macro_str
, Sym
*args
)
3641 int *st
, last_tok
, t
, notfirst
;
3650 TOK_GET(t
, macro_str
, cval
);
3655 TOK_GET(t
, macro_str
, cval
);
3658 s
= sym_find2(args
, t
);
3665 cstr_ccat(&cstr
, ' ');
3666 TOK_GET(t
, st
, cval
);
3667 cstr_cat(&cstr
, get_tok_str(t
, &cval
));
3670 cstr_ccat(&cstr
, '\0');
3672 printf("stringize: %s\n", (char *)cstr
.data
);
3676 tok_str_add2(&str
, TOK_STR
, &cval
);
3679 tok_str_add2(&str
, t
, &cval
);
3681 } else if (t
>= TOK_IDENT
) {
3682 s
= sym_find2(args
, t
);
3685 /* if '##' is present before or after, no arg substitution */
3686 if (*macro_str
== TOK_TWOSHARPS
|| last_tok
== TOK_TWOSHARPS
) {
3687 /* special case for var arg macros : ## eats the
3688 ',' if empty VA_ARGS variable. */
3689 /* XXX: test of the ',' is not 100%
3690 reliable. should fix it to avoid security
3692 if (gnu_ext
&& s
->type
.t
&&
3693 last_tok
== TOK_TWOSHARPS
&&
3694 str
.len
>= 2 && str
.str
[str
.len
- 2] == ',') {
3696 /* suppress ',' '##' */
3699 /* suppress '##' and add variable */
3707 TOK_GET(t1
, st
, cval
);
3710 tok_str_add2(&str
, t1
, &cval
);
3714 /* NOTE: the stream cannot be read when macro
3715 substituing an argument */
3716 macro_subst(&str
, nested_list
, st
, 0);
3719 tok_str_add(&str
, t
);
3722 tok_str_add2(&str
, t
, &cval
);
3726 tok_str_add(&str
, 0);
3730 static char const ab_month_name
[12][4] =
3732 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
3733 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
3736 /* do macro substitution of current token with macro 's' and add
3737 result to (tok_str,tok_len). 'nested_list' is the list of all
3738 macros we got inside to avoid recursing. Return non zero if no
3739 substitution needs to be done */
3740 static int macro_subst_tok(TokenString
*tok_str
,
3741 Sym
**nested_list
, Sym
*s
, int can_read_stream
)
3743 Sym
*args
, *sa
, *sa1
;
3744 int mstr_allocated
, parlevel
, *mstr
, t
;
3750 /* if symbol is a macro, prepare substitution */
3752 /* special macros */
3753 if (tok
== TOK___LINE__
) {
3754 cval
.i
= file
->line_num
;
3755 tok_str_add2(tok_str
, TOK_CINT
, &cval
);
3756 } else if (tok
== TOK___FILE__
) {
3757 cstrval
= file
->filename
;
3759 tok_str_add2(tok_str
, TOK_STR
, &cval
);
3760 } else if (tok
== TOK___DATE__
|| tok
== TOK___TIME__
) {
3766 tm
= localtime(&ti
);
3767 if (tok
== TOK___DATE__
) {
3768 snprintf(buf
, sizeof(buf
), "%s %2d %d",
3769 ab_month_name
[tm
->tm_mon
], tm
->tm_mday
, tm
->tm_year
+ 1900);
3771 snprintf(buf
, sizeof(buf
), "%02d:%02d:%02d",
3772 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
3777 cstr_cat(&cstr
, cstrval
);
3778 cstr_ccat(&cstr
, '\0');
3780 tok_str_add2(tok_str
, TOK_STR
, &cval
);
3785 if (s
->type
.t
== MACRO_FUNC
) {
3786 /* NOTE: we do not use next_nomacro to avoid eating the
3787 next token. XXX: find better solution */
3790 if (t
== 0 && can_read_stream
) {
3791 /* end of macro stream: we must look at the token
3792 after in the file */
3798 /* XXX: incorrect with comments */
3799 ch
= file
->buf_ptr
[0];
3800 while (is_space(ch
) || ch
== '\n')
3804 if (t
!= '(') /* no macro subst */
3807 /* argument macro */
3812 /* NOTE: empty args are allowed, except if no args */
3814 /* handle '()' case */
3815 if (!args
&& !sa
&& tok
== ')')
3818 error("macro '%s' used with too many args",
3819 get_tok_str(s
->v
, 0));
3822 /* NOTE: non zero sa->t indicates VA_ARGS */
3823 while ((parlevel
> 0 ||
3825 (tok
!= ',' || sa
->type
.t
))) &&
3829 else if (tok
== ')')
3831 tok_str_add2(&str
, tok
, &tokc
);
3834 tok_str_add(&str
, 0);
3835 sym_push2(&args
, sa
->v
& ~SYM_FIELD
, sa
->type
.t
, (int)str
.str
);
3838 /* special case for gcc var args: add an empty
3839 var arg argument if it is omitted */
3840 if (sa
&& sa
->type
.t
&& gnu_ext
)
3850 error("macro '%s' used with too few args",
3851 get_tok_str(s
->v
, 0));
3854 /* now subst each arg */
3855 mstr
= macro_arg_subst(nested_list
, mstr
, args
);
3860 tok_str_free((int *)sa
->c
);
3866 sym_push2(nested_list
, s
->v
, 0, 0);
3867 macro_subst(tok_str
, nested_list
, mstr
, 1);
3868 /* pop nested defined symbol */
3870 *nested_list
= sa1
->prev
;
3878 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
3879 return the resulting string (which must be freed). */
3880 static inline int *macro_twosharps(const int *macro_str
)
3883 const int *macro_ptr1
, *start_macro_ptr
, *ptr
, *saved_macro_ptr
;
3885 const char *p1
, *p2
;
3887 TokenString macro_str1
;
3890 start_macro_ptr
= macro_str
;
3891 /* we search the first '##' */
3893 macro_ptr1
= macro_str
;
3894 TOK_GET(t
, macro_str
, cval
);
3895 /* nothing more to do if end of string */
3898 if (*macro_str
== TOK_TWOSHARPS
)
3902 /* we saw '##', so we need more processing to handle it */
3904 tok_str_new(¯o_str1
);
3908 /* add all tokens seen so far */
3909 for(ptr
= start_macro_ptr
; ptr
< macro_ptr1
;) {
3910 TOK_GET(t
, ptr
, cval
);
3911 tok_str_add2(¯o_str1
, t
, &cval
);
3913 saved_macro_ptr
= macro_ptr
;
3914 /* XXX: get rid of the use of macro_ptr here */
3915 macro_ptr
= (int *)macro_str
;
3917 while (*macro_ptr
== TOK_TWOSHARPS
) {
3919 macro_ptr1
= macro_ptr
;
3922 TOK_GET(t
, macro_ptr
, cval
);
3923 /* We concatenate the two tokens if we have an
3924 identifier or a preprocessing number */
3926 p1
= get_tok_str(tok
, &tokc
);
3927 cstr_cat(&cstr
, p1
);
3928 p2
= get_tok_str(t
, &cval
);
3929 cstr_cat(&cstr
, p2
);
3930 cstr_ccat(&cstr
, '\0');
3932 if ((tok
>= TOK_IDENT
|| tok
== TOK_PPNUM
) &&
3933 (t
>= TOK_IDENT
|| t
== TOK_PPNUM
)) {
3934 if (tok
== TOK_PPNUM
) {
3935 /* if number, then create a number token */
3936 /* NOTE: no need to allocate because
3937 tok_str_add2() does it */
3940 /* if identifier, we must do a test to
3941 validate we have a correct identifier */
3942 if (t
== TOK_PPNUM
) {
3952 if (!isnum(c
) && !isid(c
))
3956 ts
= tok_alloc(cstr
.data
, strlen(cstr
.data
));
3957 tok
= ts
->tok
; /* modify current token */
3960 const char *str
= cstr
.data
;
3961 const unsigned char *q
;
3963 /* we look for a valid token */
3964 /* XXX: do more extensive checks */
3965 if (!strcmp(str
, ">>=")) {
3967 } else if (!strcmp(str
, "<<=")) {
3969 } else if (strlen(str
) == 2) {
3970 /* search in two bytes table */
3975 if (q
[0] == str
[0] && q
[1] == str
[1])
3982 /* NOTE: because get_tok_str use a static buffer,
3985 p1
= get_tok_str(tok
, &tokc
);
3986 cstr_cat(&cstr
, p1
);
3987 cstr_ccat(&cstr
, '\0');
3988 p2
= get_tok_str(t
, &cval
);
3989 warning("pasting \"%s\" and \"%s\" does not give a valid preprocessing token", cstr
.data
, p2
);
3990 /* cannot merge tokens: just add them separately */
3991 tok_str_add2(¯o_str1
, tok
, &tokc
);
3992 /* XXX: free associated memory ? */
3999 tok_str_add2(¯o_str1
, tok
, &tokc
);
4004 macro_ptr
= (int *)saved_macro_ptr
;
4006 tok_str_add(¯o_str1
, 0);
4007 return macro_str1
.str
;
4011 /* do macro substitution of macro_str and add result to
4012 (tok_str,tok_len). 'nested_list' is the list of all macros we got
4013 inside to avoid recursing. */
4014 static void macro_subst(TokenString
*tok_str
, Sym
**nested_list
,
4015 const int *macro_str
, int can_read_stream
)
4018 int *saved_macro_ptr
, *macro_str1
;
4023 /* first scan for '##' operator handling */
4025 macro_str1
= macro_twosharps(ptr
);
4029 /* NOTE: ptr == NULL can only happen if tokens are read from
4030 file stream due to a macro function call */
4033 TOK_GET(t
, ptr
, cval
);
4038 /* if nested substitution, do nothing */
4039 if (sym_find2(*nested_list
, t
))
4041 saved_macro_ptr
= macro_ptr
;
4042 macro_ptr
= (int *)ptr
;
4044 ret
= macro_subst_tok(tok_str
, nested_list
, s
, can_read_stream
);
4045 ptr
= (int *)macro_ptr
;
4046 macro_ptr
= saved_macro_ptr
;
4051 tok_str_add2(tok_str
, t
, &cval
);
4055 tok_str_free(macro_str1
);
4058 /* return next token with macro substitution */
4059 static void next(void)
4061 Sym
*nested_list
, *s
;
4067 /* if not reading from macro substituted string, then try
4068 to substitute macros */
4069 if (tok
>= TOK_IDENT
&&
4070 (parse_flags
& PARSE_FLAG_PREPROCESS
)) {
4071 s
= define_find(tok
);
4073 /* we have a macro: we try to substitute */
4076 if (macro_subst_tok(&str
, &nested_list
, s
, 1) == 0) {
4077 /* substitution done, NOTE: maybe empty */
4078 tok_str_add(&str
, 0);
4079 macro_ptr
= str
.str
;
4080 macro_ptr_allocated
= str
.str
;
4087 /* end of macro or end of unget buffer */
4088 if (unget_buffer_enabled
) {
4089 macro_ptr
= unget_saved_macro_ptr
;
4090 unget_buffer_enabled
= 0;
4092 /* end of macro string: free it */
4093 tok_str_free(macro_ptr_allocated
);
4100 /* convert preprocessor tokens into C tokens */
4101 if (tok
== TOK_PPNUM
&&
4102 (parse_flags
& PARSE_FLAG_TOK_NUM
)) {
4103 parse_number((char *)tokc
.cstr
->data
);
4107 /* push back current token and set current token to 'last_tok'. Only
4108 identifier case handled for labels. */
4109 static inline void unget_tok(int last_tok
)
4113 unget_saved_macro_ptr
= macro_ptr
;
4114 unget_buffer_enabled
= 1;
4115 q
= unget_saved_buffer
;
4118 n
= tok_ext_size(tok
) - 1;
4121 *q
= 0; /* end of token string */
4126 void swap(int *p
, int *q
)
4134 void vsetc(CType
*type
, int r
, CValue
*vc
)
4138 if (vtop
>= vstack
+ VSTACK_SIZE
)
4139 error("memory full");
4140 /* cannot let cpu flags if other instruction are generated. Also
4141 avoid leaving VT_JMP anywhere except on the top of the stack
4142 because it would complicate the code generator. */
4143 if (vtop
>= vstack
) {
4144 v
= vtop
->r
& VT_VALMASK
;
4145 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
4151 vtop
->r2
= VT_CONST
;
4155 /* push integer constant */
4160 vsetc(&int_type
, VT_CONST
, &cval
);
4163 /* Return a static symbol pointing to a section */
4164 static Sym
*get_sym_ref(CType
*type
, Section
*sec
,
4165 unsigned long offset
, unsigned long size
)
4171 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
4172 sym
->type
.ref
= type
->ref
;
4173 sym
->r
= VT_CONST
| VT_SYM
;
4174 put_extern_sym(sym
, sec
, offset
, size
);
4178 /* push a reference to a section offset by adding a dummy symbol */
4179 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
4184 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
4185 vtop
->sym
= get_sym_ref(type
, sec
, offset
, size
);
4188 /* define a new external reference to a symbol 'v' of type 'u' */
4189 static Sym
*external_global_sym(int v
, CType
*type
, int r
)
4195 /* push forward reference */
4196 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
4197 s
->type
.ref
= type
->ref
;
4198 s
->r
= r
| VT_CONST
| VT_SYM
;
4203 /* define a new external reference to a symbol 'v' of type 'u' */
4204 static Sym
*external_sym(int v
, CType
*type
, int r
)
4210 /* push forward reference */
4211 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
4212 s
->type
.t
|= VT_EXTERN
;
4214 if (!is_compatible_types(&s
->type
, type
))
4215 error("incompatible types for redefinition of '%s'",
4216 get_tok_str(v
, NULL
));
4221 /* push a reference to global symbol v */
4222 static void vpush_global_sym(CType
*type
, int v
)
4227 sym
= external_global_sym(v
, type
, 0);
4229 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
4233 void vset(CType
*type
, int r
, int v
)
4238 vsetc(type
, r
, &cval
);
4241 void vseti(int r
, int v
)
4257 void vpushv(SValue
*v
)
4259 if (vtop
>= vstack
+ VSTACK_SIZE
)
4260 error("memory full");
4270 /* save r to the memory stack, and mark it as being free */
4271 void save_reg(int r
)
4273 int l
, saved
, size
, align
;
4277 /* modify all stack values */
4280 for(p
=vstack
;p
<=vtop
;p
++) {
4281 if ((p
->r
& VT_VALMASK
) == r
||
4282 (p
->r2
& VT_VALMASK
) == r
) {
4283 /* must save value on stack if not already done */
4285 /* NOTE: must reload 'r' because r might be equal to r2 */
4286 r
= p
->r
& VT_VALMASK
;
4287 /* store register in the stack */
4289 if ((p
->r
& VT_LVAL
) ||
4290 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
4292 size
= type_size(type
, &align
);
4293 loc
= (loc
- size
) & -align
;
4294 sv
.type
.t
= type
->t
;
4295 sv
.r
= VT_LOCAL
| VT_LVAL
;
4298 #ifdef TCC_TARGET_I386
4299 /* x86 specific: need to pop fp register ST0 if saved */
4300 if (r
== TREG_ST0
) {
4301 o(0xd9dd); /* fstp %st(1) */
4304 /* special long long case */
4305 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
4312 /* mark that stack entry as being saved on the stack */
4313 if (p
->r
& VT_LVAL
) {
4314 /* also clear the bounded flag because the
4315 relocation address of the function was stored in
4317 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
4319 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
4327 /* find a free register of class 'rc'. If none, save one register */
4333 /* find a free register */
4334 for(r
=0;r
<NB_REGS
;r
++) {
4335 if (reg_classes
[r
] & rc
) {
4336 for(p
=vstack
;p
<=vtop
;p
++) {
4337 if ((p
->r
& VT_VALMASK
) == r
||
4338 (p
->r2
& VT_VALMASK
) == r
)
4346 /* no register left : free the first one on the stack (VERY
4347 IMPORTANT to start from the bottom to ensure that we don't
4348 spill registers used in gen_opi()) */
4349 for(p
=vstack
;p
<=vtop
;p
++) {
4350 r
= p
->r
& VT_VALMASK
;
4351 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
4353 /* also look at second register (if long long) */
4354 r
= p
->r2
& VT_VALMASK
;
4355 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
4361 /* Should never comes here */
4365 /* save registers up to (vtop - n) stack entry */
4366 void save_regs(int n
)
4371 for(p
= vstack
;p
<= p1
; p
++) {
4372 r
= p
->r
& VT_VALMASK
;
4379 /* move register 's' to 'r', and flush previous value of r to memory
4381 void move_reg(int r
, int s
)
4394 /* get address of vtop (vtop MUST BE an lvalue) */
4397 vtop
->r
&= ~VT_LVAL
;
4398 /* tricky: if saved lvalue, then we can go back to lvalue */
4399 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
4400 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
4403 #ifdef CONFIG_TCC_BCHECK
4404 /* generate lvalue bound code */
4410 vtop
->r
&= ~VT_MUSTBOUND
;
4411 /* if lvalue, then use checking code before dereferencing */
4412 if (vtop
->r
& VT_LVAL
) {
4413 /* if not VT_BOUNDED value, then make one */
4414 if (!(vtop
->r
& VT_BOUNDED
)) {
4415 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
4416 /* must save type because we must set it to int to get pointer */
4418 vtop
->type
.t
= VT_INT
;
4421 gen_bounded_ptr_add();
4422 vtop
->r
|= lval_type
;
4425 /* then check for dereferencing */
4426 gen_bounded_ptr_deref();
4431 /* store vtop a register belonging to class 'rc'. lvalues are
4432 converted to values. Cannot be used if cannot be converted to
4433 register value (such as structures). */
4436 int r
, r2
, rc2
, bit_pos
, bit_size
, size
, align
, i
;
4437 unsigned long long ll
;
4439 /* NOTE: get_reg can modify vstack[] */
4440 if (vtop
->type
.t
& VT_BITFIELD
) {
4441 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4442 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
4443 /* remove bit field info to avoid loops */
4444 vtop
->type
.t
&= ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
4445 /* generate shifts */
4446 vpushi(32 - (bit_pos
+ bit_size
));
4448 vpushi(32 - bit_size
);
4449 /* NOTE: transformed to SHR if unsigned */
4453 if (is_float(vtop
->type
.t
) &&
4454 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
4457 unsigned long offset
;
4459 /* XXX: unify with initializers handling ? */
4460 /* CPUs usually cannot use float constants, so we store them
4461 generically in data segment */
4462 size
= type_size(&vtop
->type
, &align
);
4463 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
4464 data_section
->data_offset
= offset
;
4465 /* XXX: not portable yet */
4466 ptr
= section_ptr_add(data_section
, size
);
4469 ptr
[i
] = vtop
->c
.tab
[i
];
4470 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
4471 vtop
->r
|= VT_LVAL
| VT_SYM
;
4475 #ifdef CONFIG_TCC_BCHECK
4476 if (vtop
->r
& VT_MUSTBOUND
)
4480 r
= vtop
->r
& VT_VALMASK
;
4481 /* need to reload if:
4483 - lvalue (need to dereference pointer)
4484 - already a register, but not in the right class */
4485 if (r
>= VT_CONST
||
4486 (vtop
->r
& VT_LVAL
) ||
4487 !(reg_classes
[r
] & rc
) ||
4488 ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&&
4489 !(reg_classes
[vtop
->r2
] & rc
))) {
4491 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
4492 /* two register type load : expand to two words
4494 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
4497 vtop
->c
.ui
= ll
; /* first word */
4499 vtop
->r
= r
; /* save register value */
4500 vpushi(ll
>> 32); /* second word */
4501 } else if (r
>= VT_CONST
||
4502 (vtop
->r
& VT_LVAL
)) {
4503 /* load from memory */
4506 vtop
[-1].r
= r
; /* save register value */
4507 /* increment pointer to get second word */
4508 vtop
->type
.t
= VT_INT
;
4514 /* move registers */
4517 vtop
[-1].r
= r
; /* save register value */
4518 vtop
->r
= vtop
[-1].r2
;
4520 /* allocate second register */
4527 /* write second register */
4529 } else if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
4531 /* lvalue of scalar type : need to use lvalue type
4532 because of possible cast */
4535 /* compute memory access type */
4536 if (vtop
->r
& VT_LVAL_BYTE
)
4538 else if (vtop
->r
& VT_LVAL_SHORT
)
4540 if (vtop
->r
& VT_LVAL_UNSIGNED
)
4544 /* restore wanted type */
4547 /* one register type load */
4556 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
4557 void gv2(int rc1
, int rc2
)
4561 /* generate more generic register first. But VT_JMP or VT_CMP
4562 values must be generated first in all cases to avoid possible
4564 v
= vtop
[0].r
& VT_VALMASK
;
4565 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
4570 /* test if reload is needed for first register */
4571 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
4581 /* test if reload is needed for first register */
4582 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
4588 /* expand long long on stack in two int registers */
4593 u
= vtop
->type
.t
& VT_UNSIGNED
;
4596 vtop
[0].r
= vtop
[-1].r2
;
4597 vtop
[0].r2
= VT_CONST
;
4598 vtop
[-1].r2
= VT_CONST
;
4599 vtop
[0].type
.t
= VT_INT
| u
;
4600 vtop
[-1].type
.t
= VT_INT
| u
;
4603 /* build a long long from two ints */
4606 gv2(RC_INT
, RC_INT
);
4607 vtop
[-1].r2
= vtop
[0].r
;
4608 vtop
[-1].type
.t
= t
;
4612 /* rotate n first stack elements to the bottom
4613 I1 ... In -> I2 ... In I1 [top is right]
4621 for(i
=-n
+1;i
!=0;i
++)
4622 vtop
[i
] = vtop
[i
+1];
4626 /* rotate n first stack elements to the top
4627 I1 ... In -> In I1 ... I(n-1) [top is right]
4635 for(i
= 0;i
< n
- 1; i
++)
4636 vtop
[-i
] = vtop
[-i
- 1];
4640 /* pop stack value */
4644 v
= vtop
->r
& VT_VALMASK
;
4645 #ifdef TCC_TARGET_I386
4646 /* for x86, we need to pop the FP stack */
4647 if (v
== TREG_ST0
&& !nocode_wanted
) {
4648 o(0xd9dd); /* fstp %st(1) */
4651 if (v
== VT_JMP
|| v
== VT_JMPI
) {
4652 /* need to put correct jump if && or || without test */
4658 /* convert stack entry to register and duplicate its value in another
4666 if ((t
& VT_BTYPE
) == VT_LLONG
) {
4673 /* stack: H L L1 H1 */
4681 /* duplicate value */
4692 load(r1
, &sv
); /* move r to r1 */
4694 /* duplicates value */
4699 /* generate CPU independent (unsigned) long long operations */
4700 void gen_opl(int op
)
4702 int t
, a
, b
, op1
, c
, i
;
4709 func
= TOK___divdi3
;
4712 func
= TOK___udivdi3
;
4715 func
= TOK___moddi3
;
4718 func
= TOK___umoddi3
;
4720 /* call generic long long function */
4721 vpush_global_sym(&func_old_type
, func
);
4726 vtop
->r2
= REG_LRET
;
4739 /* stack: L1 H1 L2 H2 */
4744 vtop
[-2] = vtop
[-3];
4747 /* stack: H1 H2 L1 L2 */
4753 /* stack: H1 H2 L1 L2 ML MH */
4756 /* stack: ML MH H1 H2 L1 L2 */
4760 /* stack: ML MH H1 L2 H2 L1 */
4765 /* stack: ML MH M1 M2 */
4768 } else if (op
== '+' || op
== '-') {
4769 /* XXX: add non carry method too (for MIPS or alpha) */
4775 /* stack: H1 H2 (L1 op L2) */
4778 gen_op(op1
+ 1); /* TOK_xxxC2 */
4781 /* stack: H1 H2 (L1 op L2) */
4784 /* stack: (L1 op L2) H1 H2 */
4786 /* stack: (L1 op L2) (H1 op H2) */
4794 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4795 t
= vtop
[-1].type
.t
;
4799 /* stack: L H shift */
4801 /* constant: simpler */
4802 /* NOTE: all comments are for SHL. the other cases are
4803 done by swaping words */
4814 if (op
!= TOK_SAR
) {
4847 /* XXX: should provide a faster fallback on x86 ? */
4850 func
= TOK___sardi3
;
4853 func
= TOK___shrdi3
;
4856 func
= TOK___shldi3
;
4862 /* compare operations */
4868 /* stack: L1 H1 L2 H2 */
4870 vtop
[-1] = vtop
[-2];
4872 /* stack: L1 L2 H1 H2 */
4875 /* when values are equal, we need to compare low words. since
4876 the jump is inverted, we invert the test too. */
4879 else if (op1
== TOK_GT
)
4881 else if (op1
== TOK_ULT
)
4883 else if (op1
== TOK_UGT
)
4888 if (op1
!= TOK_NE
) {
4892 /* generate non equal test */
4893 /* XXX: NOT PORTABLE yet */
4897 #ifdef TCC_TARGET_I386
4898 b
= psym(0x850f, 0);
4900 error("not implemented");
4904 /* compare low. Always unsigned */
4908 else if (op1
== TOK_LE
)
4910 else if (op1
== TOK_GT
)
4912 else if (op1
== TOK_GE
)
4922 /* handle integer constant optimizations and various machine
4924 void gen_opic(int op
)
4931 /* currently, we cannot do computations with forward symbols */
4932 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
4933 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
4937 case '+': v1
->c
.i
+= fc
; break;
4938 case '-': v1
->c
.i
-= fc
; break;
4939 case '&': v1
->c
.i
&= fc
; break;
4940 case '^': v1
->c
.i
^= fc
; break;
4941 case '|': v1
->c
.i
|= fc
; break;
4942 case '*': v1
->c
.i
*= fc
; break;
4949 /* if division by zero, generate explicit division */
4952 error("division by zero in constant");
4956 default: v1
->c
.i
/= fc
; break;
4957 case '%': v1
->c
.i
%= fc
; break;
4958 case TOK_UDIV
: v1
->c
.i
= (unsigned)v1
->c
.i
/ fc
; break;
4959 case TOK_UMOD
: v1
->c
.i
= (unsigned)v1
->c
.i
% fc
; break;
4962 case TOK_SHL
: v1
->c
.i
<<= fc
; break;
4963 case TOK_SHR
: v1
->c
.i
= (unsigned)v1
->c
.i
>> fc
; break;
4964 case TOK_SAR
: v1
->c
.i
>>= fc
; break;
4966 case TOK_ULT
: v1
->c
.i
= (unsigned)v1
->c
.i
< (unsigned)fc
; break;
4967 case TOK_UGE
: v1
->c
.i
= (unsigned)v1
->c
.i
>= (unsigned)fc
; break;
4968 case TOK_EQ
: v1
->c
.i
= v1
->c
.i
== fc
; break;
4969 case TOK_NE
: v1
->c
.i
= v1
->c
.i
!= fc
; break;
4970 case TOK_ULE
: v1
->c
.i
= (unsigned)v1
->c
.i
<= (unsigned)fc
; break;
4971 case TOK_UGT
: v1
->c
.i
= (unsigned)v1
->c
.i
> (unsigned)fc
; break;
4972 case TOK_LT
: v1
->c
.i
= v1
->c
.i
< fc
; break;
4973 case TOK_GE
: v1
->c
.i
= v1
->c
.i
>= fc
; break;
4974 case TOK_LE
: v1
->c
.i
= v1
->c
.i
<= fc
; break;
4975 case TOK_GT
: v1
->c
.i
= v1
->c
.i
> fc
; break;
4977 case TOK_LAND
: v1
->c
.i
= v1
->c
.i
&& fc
; break;
4978 case TOK_LOR
: v1
->c
.i
= v1
->c
.i
|| fc
; break;
4984 /* if commutative ops, put c2 as constant */
4985 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
4986 op
== '|' || op
== '*')) {
4991 if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
4994 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
4995 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
5001 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
5002 /* try to use shifts instead of muls or divs */
5003 if (fc
> 0 && (fc
& (fc
- 1)) == 0) {
5012 else if (op
== TOK_PDIV
)
5018 } else if (c2
&& (op
== '+' || op
== '-') &&
5019 (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) ==
5020 (VT_CONST
| VT_SYM
)) {
5021 /* symbol + constant case */
5028 if (!nocode_wanted
) {
5029 /* call low level op generator */
5038 /* generate a floating point operation with constant propagation */
5039 void gen_opif(int op
)
5047 /* currently, we cannot do computations with forward symbols */
5048 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
5049 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
5051 if (v1
->type
.t
== VT_FLOAT
) {
5054 } else if (v1
->type
.t
== VT_DOUBLE
) {
5062 /* NOTE: we only do constant propagation if finite number (not
5063 NaN or infinity) (ANSI spec) */
5064 if (!ieee_finite(f1
) || !ieee_finite(f2
))
5068 case '+': f1
+= f2
; break;
5069 case '-': f1
-= f2
; break;
5070 case '*': f1
*= f2
; break;
5074 error("division by zero in constant");
5079 /* XXX: also handles tests ? */
5083 /* XXX: overflow test ? */
5084 if (v1
->type
.t
== VT_FLOAT
) {
5086 } else if (v1
->type
.t
== VT_DOUBLE
) {
5094 if (!nocode_wanted
) {
5102 static int pointed_size(CType
*type
)
5105 return type_size(pointed_type(type
), &align
);
5108 static inline int is_null_pointer(SValue
*p
)
5110 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
5112 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& p
->c
.i
== 0) ||
5113 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.ll
== 0);
5116 static inline int is_integer_btype(int bt
)
5118 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
5119 bt
== VT_INT
|| bt
== VT_LLONG
);
5122 /* check types for comparison or substraction of pointers */
5123 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
5125 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
5128 /* null pointers are accepted for all comparisons as gcc */
5129 if (is_null_pointer(p1
) || is_null_pointer(p2
))
5133 bt1
= type1
->t
& VT_BTYPE
;
5134 bt2
= type2
->t
& VT_BTYPE
;
5135 /* accept comparison between pointer and integer with a warning */
5136 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
5137 warning("comparison between pointer and integer");
5141 /* both must be pointers or implicit function pointers */
5142 if (bt1
== VT_PTR
) {
5143 type1
= pointed_type(type1
);
5144 } else if (bt1
!= VT_FUNC
)
5145 goto invalid_operands
;
5147 if (bt2
== VT_PTR
) {
5148 type2
= pointed_type(type2
);
5149 } else if (bt2
!= VT_FUNC
) {
5151 error("invalid operands to binary %s", get_tok_str(op
, NULL
));
5153 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
5154 (type2
->t
& VT_BTYPE
) == VT_VOID
)
5158 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
5159 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
5160 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
5161 /* gcc-like error if '-' is used */
5163 goto invalid_operands
;
5165 warning("comparison of distinct pointer types lacks a cast");
5169 /* generic gen_op: handles types problems */
5172 int u
, t1
, t2
, bt1
, bt2
, t
;
5175 t1
= vtop
[-1].type
.t
;
5176 t2
= vtop
[0].type
.t
;
5177 bt1
= t1
& VT_BTYPE
;
5178 bt2
= t2
& VT_BTYPE
;
5180 if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
5181 /* at least one operand is a pointer */
5182 /* relationnal op: must be both pointers */
5183 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
5184 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
5185 /* pointers are handled are unsigned */
5186 t
= VT_INT
| VT_UNSIGNED
;
5189 /* if both pointers, then it must be the '-' op */
5190 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
5192 error("cannot use pointers here");
5193 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
5194 /* XXX: check that types are compatible */
5195 u
= pointed_size(&vtop
[-1].type
);
5197 /* set to integer type */
5198 vtop
->type
.t
= VT_INT
;
5202 /* exactly one pointer : must be '+' or '-'. */
5203 if (op
!= '-' && op
!= '+')
5204 error("cannot use pointers here");
5205 /* Put pointer as first operand */
5206 if (bt2
== VT_PTR
) {
5210 type1
= vtop
[-1].type
;
5211 /* XXX: cast to int ? (long long case) */
5212 vpushi(pointed_size(&vtop
[-1].type
));
5214 #ifdef CONFIG_TCC_BCHECK
5215 /* if evaluating constant expression, no code should be
5216 generated, so no bound check */
5217 if (do_bounds_check
&& !const_wanted
) {
5218 /* if bounded pointers, we generate a special code to
5225 gen_bounded_ptr_add();
5231 /* put again type if gen_opic() swaped operands */
5234 } else if (is_float(bt1
) || is_float(bt2
)) {
5235 /* compute bigger type and do implicit casts */
5236 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
5238 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
5243 /* floats can only be used for a few operations */
5244 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
5245 (op
< TOK_ULT
|| op
> TOK_GT
))
5246 error("invalid operands for binary operation");
5248 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
5249 /* cast to biggest op */
5251 /* convert to unsigned if it does not fit in a long long */
5252 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
5253 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
5257 /* integer operations */
5259 /* convert to unsigned if it does not fit in an integer */
5260 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
5261 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
5264 /* XXX: currently, some unsigned operations are explicit, so
5265 we modify them here */
5266 if (t
& VT_UNSIGNED
) {
5273 else if (op
== TOK_LT
)
5275 else if (op
== TOK_GT
)
5277 else if (op
== TOK_LE
)
5279 else if (op
== TOK_GE
)
5286 /* special case for shifts and long long: we keep the shift as
5288 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
5293 else if ((t
& VT_BTYPE
) == VT_LLONG
)
5297 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
5298 /* relationnal op: the result is an int */
5299 vtop
->type
.t
= VT_INT
;
5306 /* generic itof for unsigned long long case */
5307 void gen_cvt_itof1(int t
)
5309 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
5310 (VT_LLONG
| VT_UNSIGNED
)) {
5313 vpush_global_sym(&func_old_type
, TOK___ulltof
);
5314 else if (t
== VT_DOUBLE
)
5315 vpush_global_sym(&func_old_type
, TOK___ulltod
);
5317 vpush_global_sym(&func_old_type
, TOK___ulltold
);
5327 /* generic ftoi for unsigned long long case */
5328 void gen_cvt_ftoi1(int t
)
5332 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
5333 /* not handled natively */
5334 st
= vtop
->type
.t
& VT_BTYPE
;
5336 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
5337 else if (st
== VT_DOUBLE
)
5338 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
5340 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
5345 vtop
->r2
= REG_LRET
;
5351 /* force char or short cast */
5352 void force_charshort_cast(int t
)
5356 /* XXX: add optimization if lvalue : just change type and offset */
5361 if (t
& VT_UNSIGNED
) {
5362 vpushi((1 << bits
) - 1);
5373 /* cast 'vtop' to 'type' */
5374 static void gen_cast(CType
*type
)
5376 int sbt
, dbt
, sf
, df
, c
;
5378 /* special delayed cast for char/short */
5379 /* XXX: in some cases (multiple cascaded casts), it may still
5381 if (vtop
->r
& VT_MUSTCAST
) {
5382 vtop
->r
&= ~VT_MUSTCAST
;
5383 force_charshort_cast(vtop
->type
.t
);
5386 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
5387 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
5389 if (sbt
!= dbt
&& !nocode_wanted
) {
5392 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
5394 /* convert from fp to fp */
5396 /* constant case: we can do it now */
5397 /* XXX: in ISOC, cannot do it if error in convert */
5398 if (dbt
== VT_FLOAT
&& sbt
== VT_DOUBLE
)
5399 vtop
->c
.f
= (float)vtop
->c
.d
;
5400 else if (dbt
== VT_FLOAT
&& sbt
== VT_LDOUBLE
)
5401 vtop
->c
.f
= (float)vtop
->c
.ld
;
5402 else if (dbt
== VT_DOUBLE
&& sbt
== VT_FLOAT
)
5403 vtop
->c
.d
= (double)vtop
->c
.f
;
5404 else if (dbt
== VT_DOUBLE
&& sbt
== VT_LDOUBLE
)
5405 vtop
->c
.d
= (double)vtop
->c
.ld
;
5406 else if (dbt
== VT_LDOUBLE
&& sbt
== VT_FLOAT
)
5407 vtop
->c
.ld
= (long double)vtop
->c
.f
;
5408 else if (dbt
== VT_LDOUBLE
&& sbt
== VT_DOUBLE
)
5409 vtop
->c
.ld
= (long double)vtop
->c
.d
;
5411 /* non constant case: generate code */
5415 /* convert int to fp */
5418 case VT_LLONG
| VT_UNSIGNED
:
5420 /* XXX: add const cases for long long */
5422 case VT_INT
| VT_UNSIGNED
:
5424 case VT_FLOAT
: vtop
->c
.f
= (float)vtop
->c
.ui
; break;
5425 case VT_DOUBLE
: vtop
->c
.d
= (double)vtop
->c
.ui
; break;
5426 case VT_LDOUBLE
: vtop
->c
.ld
= (long double)vtop
->c
.ui
; break;
5431 case VT_FLOAT
: vtop
->c
.f
= (float)vtop
->c
.i
; break;
5432 case VT_DOUBLE
: vtop
->c
.d
= (double)vtop
->c
.i
; break;
5433 case VT_LDOUBLE
: vtop
->c
.ld
= (long double)vtop
->c
.i
; break;
5442 /* convert fp to int */
5443 /* we handle char/short/etc... with generic code */
5444 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
5445 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
5450 case VT_LLONG
| VT_UNSIGNED
:
5452 /* XXX: add const cases for long long */
5454 case VT_INT
| VT_UNSIGNED
:
5456 case VT_FLOAT
: vtop
->c
.ui
= (unsigned int)vtop
->c
.d
; break;
5457 case VT_DOUBLE
: vtop
->c
.ui
= (unsigned int)vtop
->c
.d
; break;
5458 case VT_LDOUBLE
: vtop
->c
.ui
= (unsigned int)vtop
->c
.d
; break;
5464 case VT_FLOAT
: vtop
->c
.i
= (int)vtop
->c
.d
; break;
5465 case VT_DOUBLE
: vtop
->c
.i
= (int)vtop
->c
.d
; break;
5466 case VT_LDOUBLE
: vtop
->c
.i
= (int)vtop
->c
.d
; break;
5474 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
5475 /* additional cast for char/short/bool... */
5479 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
5480 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
5481 /* scalar to long long */
5483 if (sbt
== (VT_INT
| VT_UNSIGNED
))
5484 vtop
->c
.ll
= vtop
->c
.ui
;
5486 vtop
->c
.ll
= vtop
->c
.i
;
5488 /* machine independent conversion */
5490 /* generate high word */
5491 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
5499 /* patch second register */
5500 vtop
[-1].r2
= vtop
->r
;
5504 } else if (dbt
== VT_BOOL
) {
5505 /* scalar to bool */
5508 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
5509 (dbt
& VT_BTYPE
) == VT_SHORT
) {
5510 force_charshort_cast(dbt
);
5511 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
5513 if (sbt
== VT_LLONG
) {
5514 /* from long long: just take low order word */
5518 /* if lvalue and single word type, nothing to do because
5519 the lvalue already contains the real type size (see
5520 VT_LVAL_xxx constants) */
5526 /* return type size. Put alignment at 'a' */
5527 static int type_size(CType
*type
, int *a
)
5532 bt
= type
->t
& VT_BTYPE
;
5533 if (bt
== VT_STRUCT
) {
5538 } else if (bt
== VT_PTR
) {
5539 if (type
->t
& VT_ARRAY
) {
5541 return type_size(&s
->type
, a
) * s
->c
;
5546 } else if (bt
== VT_LDOUBLE
) {
5548 return LDOUBLE_SIZE
;
5549 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
5550 *a
= 4; /* XXX: i386 specific */
5552 } else if (bt
== VT_INT
|| bt
== VT_ENUM
|| bt
== VT_FLOAT
) {
5555 } else if (bt
== VT_SHORT
) {
5559 /* char, void, function, _Bool */
5565 /* return the pointed type of t */
5566 static inline CType
*pointed_type(CType
*type
)
5568 return &type
->ref
->type
;
5571 /* modify type so that its it is a pointer to type. */
5572 static void mk_pointer(CType
*type
)
5575 s
= sym_push(SYM_FIELD
, type
, 0, -1);
5576 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
5580 /* compare function types. OLD functions match any new functions */
5581 static int is_compatible_func(CType
*type1
, CType
*type2
)
5587 if (!is_compatible_types(&s1
->type
, &s2
->type
))
5589 /* XXX: not complete */
5590 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
5594 while (s1
!= NULL
) {
5597 if (!is_compatible_types(&s1
->type
, &s2
->type
))
5607 /* return true if type1 and type2 are exactly the same (including
5610 - enums are not checked as gcc __builtin_types_compatible_p ()
5612 static int is_compatible_types(CType
*type1
, CType
*type2
)
5616 t1
= type1
->t
& VT_TYPE
;
5617 t2
= type2
->t
& VT_TYPE
;
5618 /* XXX: bitfields ? */
5621 /* test more complicated cases */
5622 bt1
= t1
& VT_BTYPE
;
5623 if (bt1
== VT_PTR
) {
5624 type1
= pointed_type(type1
);
5625 type2
= pointed_type(type2
);
5626 return is_compatible_types(type1
, type2
);
5627 } else if (bt1
== VT_STRUCT
) {
5628 return (type1
->ref
== type2
->ref
);
5629 } else if (bt1
== VT_FUNC
) {
5630 return is_compatible_func(type1
, type2
);
5636 /* print a type. If 'varstr' is not NULL, then the variable is also
5637 printed in the type */
5639 /* XXX: add array and function pointers */
5640 void type_to_str(char *buf
, int buf_size
,
5641 CType
*type
, const char *varstr
)
5648 t
= type
->t
& VT_TYPE
;
5651 if (t
& VT_CONSTANT
)
5652 pstrcat(buf
, buf_size
, "const ");
5653 if (t
& VT_VOLATILE
)
5654 pstrcat(buf
, buf_size
, "volatile ");
5655 if (t
& VT_UNSIGNED
)
5656 pstrcat(buf
, buf_size
, "unsigned ");
5686 tstr
= "long double";
5688 pstrcat(buf
, buf_size
, tstr
);
5692 if (bt
== VT_STRUCT
)
5696 pstrcat(buf
, buf_size
, tstr
);
5697 v
= type
->ref
->v
& ~SYM_STRUCT
;
5698 if (v
>= SYM_FIRST_ANOM
)
5699 pstrcat(buf
, buf_size
, "<anonymous>");
5701 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
5705 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
5706 pstrcat(buf
, buf_size
, "(");
5708 while (sa
!= NULL
) {
5709 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
5710 pstrcat(buf
, buf_size
, buf1
);
5713 pstrcat(buf
, buf_size
, ", ");
5715 pstrcat(buf
, buf_size
, ")");
5719 pstrcpy(buf1
, sizeof(buf1
), "*");
5721 pstrcat(buf1
, sizeof(buf1
), varstr
);
5722 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
5726 pstrcat(buf
, buf_size
, " ");
5727 pstrcat(buf
, buf_size
, varstr
);
5732 /* verify type compatibility to store vtop in 'dt' type, and generate
5734 static void gen_assign_cast(CType
*dt
)
5736 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
5737 char buf1
[256], buf2
[256];
5740 st
= &vtop
->type
; /* source type */
5741 dbt
= dt
->t
& VT_BTYPE
;
5742 sbt
= st
->t
& VT_BTYPE
;
5743 if (dt
->t
& VT_CONSTANT
)
5744 warning("assignment of read-only location");
5747 /* special cases for pointers */
5748 /* '0' can also be a pointer */
5749 if (is_null_pointer(vtop
))
5751 /* accept implicit pointer to integer cast with warning */
5752 if (is_integer_btype(sbt
)) {
5753 warning("assignment makes pointer from integer without a cast");
5756 type1
= pointed_type(dt
);
5757 /* a function is implicitely a function pointer */
5758 if (sbt
== VT_FUNC
) {
5759 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
5760 !is_compatible_types(pointed_type(dt
), st
))
5767 type2
= pointed_type(st
);
5768 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
5769 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
5770 /* void * can match anything */
5772 /* exact type match, except for unsigned */
5775 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
5776 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
5777 if (!is_compatible_types(&tmp_type1
, &tmp_type2
))
5780 /* check const and volatile */
5781 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
5782 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
5783 warning("assignment discards qualifiers from pointer target type");
5789 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
5790 warning("assignment makes integer from pointer without a cast");
5792 /* XXX: more tests */
5795 if (!is_compatible_types(dt
, st
)) {
5797 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
5798 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
5799 error("cannot cast '%s' to '%s'", buf1
, buf2
);
5807 /* store vtop in lvalue pushed on stack */
5810 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
5812 ft
= vtop
[-1].type
.t
;
5813 sbt
= vtop
->type
.t
& VT_BTYPE
;
5814 dbt
= ft
& VT_BTYPE
;
5815 if (((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
5816 (sbt
== VT_INT
&& dbt
== VT_SHORT
)) {
5817 /* optimize char/short casts */
5818 delayed_cast
= VT_MUSTCAST
;
5819 vtop
->type
.t
= ft
& VT_TYPE
;
5820 /* XXX: factorize */
5821 if (ft
& VT_CONSTANT
)
5822 warning("assignment of read-only location");
5825 gen_assign_cast(&vtop
[-1].type
);
5828 if (sbt
== VT_STRUCT
) {
5829 /* if structure, only generate pointer */
5830 /* structure assignment : generate memcpy */
5831 /* XXX: optimize if small size */
5832 if (!nocode_wanted
) {
5833 size
= type_size(&vtop
->type
, &align
);
5835 vpush_global_sym(&func_old_type
, TOK_memcpy
);
5839 vtop
->type
.t
= VT_INT
;
5843 vtop
->type
.t
= VT_INT
;
5855 /* leave source on stack */
5856 } else if (ft
& VT_BITFIELD
) {
5857 /* bitfield store handling */
5858 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
5859 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
5860 /* remove bit field info to avoid loops */
5861 vtop
[-1].type
.t
= ft
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
5863 /* duplicate destination */
5865 vtop
[-1] = vtop
[-2];
5867 /* mask and shift source */
5868 vpushi((1 << bit_size
) - 1);
5872 /* load destination, mask and or with source */
5874 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
5880 #ifdef CONFIG_TCC_BCHECK
5881 /* bound check case */
5882 if (vtop
[-1].r
& VT_MUSTBOUND
) {
5888 if (!nocode_wanted
) {
5892 r
= gv(rc
); /* generate value */
5893 /* if lvalue was saved on stack, must read it */
5894 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
5896 t
= get_reg(RC_INT
);
5898 sv
.r
= VT_LOCAL
| VT_LVAL
;
5899 sv
.c
.ul
= vtop
[-1].c
.ul
;
5901 vtop
[-1].r
= t
| VT_LVAL
;
5904 /* two word case handling : store second register at word + 4 */
5905 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
5907 /* convert to int to increment easily */
5908 vtop
->type
.t
= VT_INT
;
5914 /* XXX: it works because r2 is spilled last ! */
5915 store(vtop
->r2
, vtop
- 1);
5919 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
5920 vtop
->r
|= delayed_cast
;
5924 /* post defines POST/PRE add. c is the token ++ or -- */
5925 void inc(int post
, int c
)
5928 vdup(); /* save lvalue */
5930 gv_dup(); /* duplicate value */
5935 vpushi(c
- TOK_MID
);
5937 vstore(); /* store value */
5939 vpop(); /* if post op, return saved value */
5942 /* Parse GNUC __attribute__ extension. Currently, the following
5943 extensions are recognized:
5944 - aligned(n) : set data/function alignment.
5945 - section(x) : generate data/code in this section.
5946 - unused : currently ignored, but may be used someday.
5948 static void parse_attribute(AttributeDef
*ad
)
5952 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
5956 while (tok
!= ')') {
5957 if (tok
< TOK_IDENT
)
5958 expect("attribute name");
5966 expect("section name");
5967 ad
->section
= find_section(tcc_state
, (char *)tokc
.cstr
->data
);
5976 if (n
<= 0 || (n
& (n
- 1)) != 0)
5977 error("alignment must be a positive power of two");
5986 /* currently, no need to handle it because tcc does not
5987 track unused objects */
5991 /* currently, no need to handle it because tcc does not
5992 track unused objects */
5997 ad
->func_call
= FUNC_CDECL
;
6002 ad
->func_call
= FUNC_STDCALL
;
6005 if (tcc_state
->warn_unsupported
)
6006 warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
6007 /* skip parameters */
6008 /* XXX: skip parenthesis too */
6011 while (tok
!= ')' && tok
!= -1)
6026 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
6027 static void struct_decl(CType
*type
, int u
)
6029 int a
, v
, size
, align
, maxalign
, c
, offset
;
6030 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
;
6035 a
= tok
; /* save decl type */
6040 /* struct already defined ? return it */
6042 expect("struct/union/enum name");
6046 error("invalid type");
6053 /* we put an undefined size for struct/union */
6054 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
6055 s
->r
= 0; /* default alignment is zero as gcc */
6056 /* put struct/union/enum name in type */
6064 error("struct/union/enum already defined");
6065 /* cannot be empty */
6067 /* non empty enums are not allowed */
6068 if (a
== TOK_ENUM
) {
6072 expect("identifier");
6078 /* enum symbols have static storage */
6079 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
6080 ss
->type
.t
|= VT_STATIC
;
6085 /* NOTE: we accept a trailing comma */
6095 while (tok
!= '}') {
6096 parse_btype(&btype
, &ad
);
6102 type_decl(&type1
, &ad
, &v
, TYPE_DIRECT
);
6103 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
6104 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
6105 error("invalid type for '%s'",
6106 get_tok_str(v
, NULL
));
6110 bit_size
= expr_const();
6111 /* XXX: handle v = 0 case for messages */
6113 error("negative width in bit-field '%s'",
6114 get_tok_str(v
, NULL
));
6115 if (v
&& bit_size
== 0)
6116 error("zero width for bit-field '%s'",
6117 get_tok_str(v
, NULL
));
6119 size
= type_size(&type1
, &align
);
6121 if (bit_size
>= 0) {
6122 bt
= type1
.t
& VT_BTYPE
;
6127 error("bitfields must have scalar type");
6129 if (bit_size
> bsize
) {
6130 error("width of '%s' exceeds its type",
6131 get_tok_str(v
, NULL
));
6132 } else if (bit_size
== bsize
) {
6133 /* no need for bit fields */
6135 } else if (bit_size
== 0) {
6136 /* XXX: what to do if only padding in a
6138 /* zero size: means to pad */
6142 /* we do not have enough room ? */
6143 if ((bit_pos
+ bit_size
) > bsize
)
6146 /* XXX: handle LSB first */
6147 type1
.t
|= VT_BITFIELD
|
6148 (bit_pos
<< VT_STRUCT_SHIFT
) |
6149 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
6150 bit_pos
+= bit_size
;
6156 /* add new memory data only if starting
6158 if (lbit_pos
== 0) {
6159 if (a
== TOK_STRUCT
) {
6160 c
= (c
+ align
- 1) & -align
;
6168 if (align
> maxalign
)
6172 printf("add field %s offset=%d",
6173 get_tok_str(v
, NULL
), offset
);
6174 if (type1
.t
& VT_BITFIELD
) {
6175 printf(" pos=%d size=%d",
6176 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
6177 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
6181 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
6185 if (tok
== ';' || tok
== TOK_EOF
)
6192 /* store size and alignment */
6193 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
6199 /* return 0 if no type declaration. otherwise, return the basic type
6202 static int parse_btype(CType
*type
, AttributeDef
*ad
)
6204 int t
, u
, type_found
;
6208 memset(ad
, 0, sizeof(AttributeDef
));
6214 /* currently, we really ignore extension */
6224 if ((t
& VT_BTYPE
) != 0)
6225 error("too many basic types");
6239 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
6240 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
6241 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
6242 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
6256 if ((t
& VT_BTYPE
) == VT_LONG
) {
6257 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
6264 struct_decl(&type1
, VT_ENUM
);
6267 type
->ref
= type1
.ref
;
6271 struct_decl(&type1
, VT_STRUCT
);
6274 /* type modifiers */
6322 /* GNUC attribute */
6323 case TOK_ATTRIBUTE1
:
6324 case TOK_ATTRIBUTE2
:
6325 parse_attribute(ad
);
6332 parse_expr_type(&type1
);
6336 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
6338 t
|= (s
->type
.t
& ~VT_TYPEDEF
);
6339 type
->ref
= s
->type
.ref
;
6346 /* long is never used as type */
6347 if ((t
& VT_BTYPE
) == VT_LONG
)
6348 t
= (t
& ~VT_BTYPE
) | VT_INT
;
6353 /* convert a function parameter type (array to pointer and function to
6354 function pointer) */
6355 static inline void convert_parameter_type(CType
*pt
)
6357 /* array must be transformed to pointer according to ANSI C */
6359 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
6364 static void post_type(CType
*type
, AttributeDef
*ad
)
6367 Sym
**plast
, *s
, *first
;
6372 /* function declaration */
6377 while (tok
!= ')') {
6378 /* read param name and compute offset */
6379 if (l
!= FUNC_OLD
) {
6380 if (!parse_btype(&pt
, &ad1
)) {
6382 error("invalid type");
6389 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
6391 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
6392 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
6393 error("parameter declared as void");
6400 convert_parameter_type(&pt
);
6401 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
6406 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
6413 /* if no parameters, then old type prototype */
6417 t1
= type
->t
& VT_STORAGE
;
6418 /* NOTE: const is ignored in returned type as it has a special
6419 meaning in gcc / C++ */
6420 type
->t
&= ~(VT_STORAGE
| VT_CONSTANT
);
6421 post_type(type
, ad
);
6422 /* we push a anonymous symbol which will contain the function prototype */
6423 s
= sym_push(SYM_FIELD
, type
, ad
->func_call
, l
);
6425 type
->t
= t1
| VT_FUNC
;
6427 } else if (tok
== '[') {
6428 /* array definition */
6434 error("invalid array size");
6437 /* parse next post type */
6438 t1
= type
->t
& VT_STORAGE
;
6439 type
->t
&= ~VT_STORAGE
;
6440 post_type(type
, ad
);
6442 /* we push a anonymous symbol which will contain the array
6444 s
= sym_push(SYM_FIELD
, type
, 0, n
);
6445 type
->t
= t1
| VT_ARRAY
| VT_PTR
;
6450 /* Parse a type declaration (except basic type), and return the type
6451 in 'type'. 'td' is a bitmask indicating which kind of type decl is
6452 expected. 'type' should contain the basic type. 'ad' is the
6453 attribute definition of the basic type. It can be modified by
6456 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
6459 CType type1
, *type2
;
6462 while (tok
== '*') {
6470 qualifiers
|= VT_CONSTANT
;
6475 qualifiers
|= VT_VOLATILE
;
6483 type
->t
|= qualifiers
;
6486 /* XXX: clarify attribute handling */
6487 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
6488 parse_attribute(ad
);
6490 /* recursive type */
6491 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
6492 type1
.t
= 0; /* XXX: same as int */
6495 /* XXX: this is not correct to modify 'ad' at this point, but
6496 the syntax is not clear */
6497 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
6498 parse_attribute(ad
);
6499 type_decl(&type1
, ad
, v
, td
);
6502 /* type identifier */
6503 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
6507 if (!(td
& TYPE_ABSTRACT
))
6508 expect("identifier");
6512 post_type(type
, ad
);
6513 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
6514 parse_attribute(ad
);
6517 /* append type at the end of type1 */
6530 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
6531 static int lvalue_type(int t
)
6536 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
6538 else if (bt
== VT_SHORT
)
6542 if (t
& VT_UNSIGNED
)
6543 r
|= VT_LVAL_UNSIGNED
;
6547 /* indirection with full error checking and bound check */
6548 static void indir(void)
6550 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
6552 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
6554 vtop
->type
= *pointed_type(&vtop
->type
);
6555 /* an array is never an lvalue */
6556 if (!(vtop
->type
.t
& VT_ARRAY
)) {
6557 vtop
->r
|= lvalue_type(vtop
->type
.t
);
6558 /* if bound checking, the referenced pointer must be checked */
6559 if (do_bounds_check
)
6560 vtop
->r
|= VT_MUSTBOUND
;
6564 /* pass a parameter to a function and do type checking and casting */
6565 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
6570 func_type
= func
->c
;
6571 if (func_type
== FUNC_OLD
||
6572 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
6573 /* default casting : only need to convert float to double */
6574 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
6578 } else if (arg
== NULL
) {
6579 error("too many arguments to function");
6581 gen_assign_cast(&arg
->type
);
6585 /* parse an expression of the form '(type)' or '(expr)' and return its
6587 static void parse_expr_type(CType
*type
)
6593 if (parse_btype(type
, &ad
)) {
6594 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
6601 static void parse_type(CType
*type
)
6606 if (!parse_btype(type
, &ad
)) {
6609 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
6612 static void vpush_tokc(int t
)
6616 vsetc(&type
, VT_CONST
, &tokc
);
6619 static void unary(void)
6621 int n
, t
, align
, size
, r
;
6626 /* XXX: GCC 2.95.3 does not generate a table although it should be
6640 vpush_tokc(VT_INT
| VT_UNSIGNED
);
6644 vpush_tokc(VT_LLONG
);
6648 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
6652 vpush_tokc(VT_FLOAT
);
6656 vpush_tokc(VT_DOUBLE
);
6660 vpush_tokc(VT_LDOUBLE
);
6663 case TOK___FUNCTION__
:
6665 goto tok_identifier
;
6671 /* special function name identifier */
6672 len
= strlen(funcname
) + 1;
6673 /* generate char[len] type */
6678 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
6679 ptr
= section_ptr_add(data_section
, len
);
6680 memcpy(ptr
, funcname
, len
);
6688 /* string parsing */
6691 if (tcc_state
->warn_write_strings
)
6696 memset(&ad
, 0, sizeof(AttributeDef
));
6697 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, 0);
6702 if (parse_btype(&type
, &ad
)) {
6703 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
6705 /* check ISOC99 compound literal */
6707 /* data is allocated locally by default */
6712 /* all except arrays are lvalues */
6713 if (!(type
.t
& VT_ARRAY
))
6714 r
|= lvalue_type(type
.t
);
6715 memset(&ad
, 0, sizeof(AttributeDef
));
6716 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, 0);
6721 } else if (tok
== '{') {
6722 /* save all registers */
6724 /* statement expression : we do not accept break/continue
6725 inside as GCC does */
6726 block(NULL
, NULL
, NULL
, NULL
, 0, 1);
6741 /* functions names must be treated as function pointers,
6742 except for unary '&' and sizeof. Since we consider that
6743 functions are not lvalues, we only have to handle it
6744 there and in function calls. */
6745 /* arrays can also be used although they are not lvalues */
6746 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
6747 !(vtop
->type
.t
& VT_ARRAY
))
6749 mk_pointer(&vtop
->type
);
6755 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
)
6756 vtop
->c
.i
= !vtop
->c
.i
;
6757 else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
6758 vtop
->c
.i
= vtop
->c
.i
^ 1;
6760 vseti(VT_JMP
, gtst(1, 0));
6770 /* in order to force cast, we add zero */
6772 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
6773 error("pointer not accepted for unary plus");
6783 parse_expr_type(&type
);
6787 size
= type_size(&type
, &align
);
6788 if (t
== TOK_SIZEOF
) {
6790 error("sizeof applied to an incomplete type");
6797 case TOK_builtin_types_compatible_p
:
6806 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
6807 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
6808 vpushi(is_compatible_types(&type1
, &type2
));
6811 case TOK_builtin_constant_p
:
6813 int saved_nocode_wanted
, res
;
6816 saved_nocode_wanted
= nocode_wanted
;
6819 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
6821 nocode_wanted
= saved_nocode_wanted
;
6841 goto tok_identifier
;
6843 /* allow to take the address of a label */
6844 if (tok
< TOK_UIDENT
)
6845 expect("label identifier");
6846 s
= label_find(tok
);
6848 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
6850 if (s
->r
== LABEL_DECLARED
)
6851 s
->r
= LABEL_FORWARD
;
6854 s
->type
.t
= VT_VOID
;
6855 mk_pointer(&s
->type
);
6856 s
->type
.t
|= VT_STATIC
;
6858 vset(&s
->type
, VT_CONST
| VT_SYM
, 0);
6867 expect("identifier");
6871 error("'%s' undeclared", get_tok_str(t
, NULL
));
6872 /* for simple function calls, we tolerate undeclared
6873 external reference to int() function */
6874 s
= external_global_sym(t
, &func_old_type
, 0);
6876 vset(&s
->type
, s
->r
, s
->c
);
6877 /* if forward reference, we must point to s */
6878 if (vtop
->r
& VT_SYM
) {
6885 /* post operations */
6887 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
6890 } else if (tok
== '.' || tok
== TOK_ARROW
) {
6892 if (tok
== TOK_ARROW
)
6897 /* expect pointer on structure */
6898 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
6899 expect("struct or union");
6903 while ((s
= s
->next
) != NULL
) {
6908 error("field not found");
6909 /* add field offset to pointer */
6910 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
6913 /* change type to field type, and set to lvalue */
6914 vtop
->type
= s
->type
;
6915 /* an array is never an lvalue */
6916 if (!(vtop
->type
.t
& VT_ARRAY
)) {
6917 vtop
->r
|= lvalue_type(vtop
->type
.t
);
6918 /* if bound checking, the referenced pointer must be checked */
6919 if (do_bounds_check
)
6920 vtop
->r
|= VT_MUSTBOUND
;
6923 } else if (tok
== '[') {
6929 } else if (tok
== '(') {
6935 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
6936 /* pointer test (no array accepted) */
6937 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
6938 vtop
->type
= *pointed_type(&vtop
->type
);
6939 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
6943 expect("function pointer");
6946 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
6948 /* get return type */
6951 sa
= s
->next
; /* first parameter */
6953 /* compute first implicit argument if a structure is returned */
6954 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
6955 /* get some space for the returned structure */
6956 size
= type_size(&s
->type
, &align
);
6957 loc
= (loc
- size
) & -align
;
6959 ret
.r
= VT_LOCAL
| VT_LVAL
;
6960 /* pass it as 'int' to avoid structure arg passing
6962 vseti(VT_LOCAL
, loc
);
6968 /* return in register */
6969 if (is_float(ret
.type
.t
)) {
6972 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
6981 gfunc_param_typed(s
, sa
);
6991 error("too few arguments to function");
6993 if (!nocode_wanted
) {
6994 gfunc_call(nb_args
);
6996 vtop
-= (nb_args
+ 1);
6999 vsetc(&ret
.type
, ret
.r
, &ret
.c
);
7007 static void uneq(void)
7013 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
7014 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
7015 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
7030 static void expr_prod(void)
7035 while (tok
== '*' || tok
== '/' || tok
== '%') {
7043 static void expr_sum(void)
7048 while (tok
== '+' || tok
== '-') {
7056 static void expr_shift(void)
7061 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
7069 static void expr_cmp(void)
7074 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
7075 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
7083 static void expr_cmpeq(void)
7088 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
7096 static void expr_and(void)
7099 while (tok
== '&') {
7106 static void expr_xor(void)
7109 while (tok
== '^') {
7116 static void expr_or(void)
7119 while (tok
== '|') {
7126 /* XXX: fix this mess */
7127 static void expr_land_const(void)
7130 while (tok
== TOK_LAND
) {
7137 /* XXX: fix this mess */
7138 static void expr_lor_const(void)
7141 while (tok
== TOK_LOR
) {
7148 /* only used if non constant */
7149 static void expr_land(void)
7154 if (tok
== TOK_LAND
) {
7158 if (tok
!= TOK_LAND
) {
7168 static void expr_lor(void)
7173 if (tok
== TOK_LOR
) {
7177 if (tok
!= TOK_LOR
) {
7187 /* XXX: better constant handling */
7188 static void expr_eq(void)
7190 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
;
7192 CType type
, type1
, type2
;
7201 if (tok
== ':' && gnu_ext
) {
7217 if (vtop
!= vstack
) {
7218 /* needed to avoid having different registers saved in
7220 if (is_float(vtop
->type
.t
))
7227 if (tok
== ':' && gnu_ext
) {
7235 sv
= *vtop
; /* save value to handle it later */
7236 vtop
--; /* no vpop so that FP stack is not flushed */
7244 bt1
= t1
& VT_BTYPE
;
7246 bt2
= t2
& VT_BTYPE
;
7247 /* cast operands to correct type according to ISOC rules */
7248 if (is_float(bt1
) || is_float(bt2
)) {
7249 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
7250 type
.t
= VT_LDOUBLE
;
7251 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
7256 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
7257 /* cast to biggest op */
7259 /* convert to unsigned if it does not fit in a long long */
7260 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
7261 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
7262 type
.t
|= VT_UNSIGNED
;
7263 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
7264 /* XXX: test pointer compatibility */
7266 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
7267 /* XXX: test structure compatibility */
7269 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
7270 /* NOTE: as an extension, we accept void on only one side */
7273 /* integer operations */
7275 /* convert to unsigned if it does not fit in an integer */
7276 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
7277 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
7278 type
.t
|= VT_UNSIGNED
;
7281 /* now we convert second operand */
7284 if (is_float(type
.t
)) {
7286 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
7287 /* for long longs, we use fixed registers to avoid having
7288 to handle a complicated move */
7293 /* this is horrible, but we must also convert first
7297 /* put again first value and cast it */
7308 static void gexpr(void)
7319 /* parse an expression and return its type without any side effect. */
7320 static void expr_type(CType
*type
)
7322 int saved_nocode_wanted
;
7324 saved_nocode_wanted
= nocode_wanted
;
7329 nocode_wanted
= saved_nocode_wanted
;
7332 /* parse a unary expression and return its type without any side
7334 static void unary_type(CType
*type
)
7346 /* parse a constant expression and return value in vtop. */
7347 static void expr_const1(void)
7356 /* parse an integer constant and return its value. */
7357 static int expr_const(void)
7361 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
7362 expect("constant expression");
7368 /* return the label token if current token is a label, otherwise
7370 static int is_label(void)
7374 /* fast test first */
7375 if (tok
< TOK_UIDENT
)
7377 /* no need to save tokc because tok is an identifier */
7384 unget_tok(last_tok
);
7389 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
7390 int case_reg
, int is_expr
)
7395 /* generate line number info */
7397 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
7398 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
7400 last_line_num
= file
->line_num
;
7404 /* default return value is (void) */
7406 vtop
->type
.t
= VT_VOID
;
7409 if (tok
== TOK_IF
) {
7416 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
7418 if (c
== TOK_ELSE
) {
7422 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
7423 gsym(d
); /* patch else jmp */
7426 } else if (tok
== TOK_WHILE
) {
7434 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
7438 } else if (tok
== '{') {
7442 /* record local declaration stack position */
7444 llabel
= local_label_stack
;
7445 /* handle local labels declarations */
7446 if (tok
== TOK_LABEL
) {
7449 if (tok
< TOK_UIDENT
)
7450 expect("label identifier");
7451 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
7461 while (tok
!= '}') {
7466 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
7469 /* pop locally defined labels */
7470 label_pop(&local_label_stack
, llabel
);
7471 /* pop locally defined symbols */
7472 sym_pop(&local_stack
, s
);
7474 } else if (tok
== TOK_RETURN
) {
7478 gen_assign_cast(&func_vt
);
7479 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
7481 /* if returning structure, must copy it to implicit
7482 first pointer arg location */
7485 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
7488 /* copy structure value to pointer */
7490 } else if (is_float(func_vt
.t
)) {
7495 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
7498 rsym
= gjmp(rsym
); /* jmp */
7499 } else if (tok
== TOK_BREAK
) {
7502 error("cannot break");
7503 *bsym
= gjmp(*bsym
);
7506 } else if (tok
== TOK_CONTINUE
) {
7509 error("cannot continue");
7510 *csym
= gjmp(*csym
);
7513 } else if (tok
== TOK_FOR
) {
7540 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
7545 if (tok
== TOK_DO
) {
7550 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
7561 if (tok
== TOK_SWITCH
) {
7565 /* XXX: other types than integer */
7566 case_reg
= gv(RC_INT
);
7570 b
= gjmp(0); /* jump to first case */
7572 block(&a
, csym
, &b
, &c
, case_reg
, 0);
7573 /* if no default, jmp after switch */
7581 if (tok
== TOK_CASE
) {
7588 if (gnu_ext
&& tok
== TOK_DOTS
) {
7592 warning("empty case range");
7594 /* since a case is like a label, we must skip it with a jmp */
7601 *case_sym
= gtst(1, 0);
7604 *case_sym
= gtst(1, 0);
7608 *case_sym
= gtst(1, *case_sym
);
7613 goto block_after_label
;
7615 if (tok
== TOK_DEFAULT
) {
7621 error("too many 'default'");
7624 goto block_after_label
;
7626 if (tok
== TOK_GOTO
) {
7628 if (tok
== '*' && gnu_ext
) {
7632 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
7635 } else if (tok
>= TOK_UIDENT
) {
7636 s
= label_find(tok
);
7637 /* put forward definition if needed */
7639 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
7641 if (s
->r
== LABEL_DECLARED
)
7642 s
->r
= LABEL_FORWARD
;
7644 /* label already defined */
7645 if (s
->r
& LABEL_FORWARD
)
7646 s
->next
= (void *)gjmp((long)s
->next
);
7648 gjmp_addr((long)s
->next
);
7651 expect("label identifier");
7654 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
7662 if (s
->r
== LABEL_DEFINED
)
7663 error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
7664 gsym((long)s
->next
);
7665 s
->r
= LABEL_DEFINED
;
7667 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
7669 s
->next
= (void *)ind
;
7670 /* we accept this, but it is a mistake */
7673 warning("deprecated use of label at end of compound statement");
7677 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
7680 /* expression case */
7695 /* t is the array or struct type. c is the array or struct
7696 address. cur_index/cur_field is the pointer to the current
7697 value. 'size_only' is true if only size info is needed (only used
7699 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
7700 int *cur_index
, Sym
**cur_field
,
7704 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
7710 if (gnu_ext
&& (l
= is_label()) != 0)
7712 while (tok
== '[' || tok
== '.') {
7714 if (!(type
->t
& VT_ARRAY
))
7715 expect("array type");
7718 index
= expr_const();
7719 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
7720 expect("invalid index");
7721 if (tok
== TOK_DOTS
&& gnu_ext
) {
7723 index_last
= expr_const();
7724 if (index_last
< 0 ||
7725 (s
->c
>= 0 && index_last
>= s
->c
) ||
7727 expect("invalid index");
7733 *cur_index
= index_last
;
7734 type
= pointed_type(type
);
7735 elem_size
= type_size(type
, &align
);
7736 c
+= index
* elem_size
;
7737 /* NOTE: we only support ranges for last designator */
7738 nb_elems
= index_last
- index
+ 1;
7739 if (nb_elems
!= 1) {
7748 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
7749 expect("struct/union type");
7762 /* XXX: fix this mess by using explicit storage field */
7764 type1
.t
|= (type
->t
& ~VT_TYPE
);
7778 if (type
->t
& VT_ARRAY
) {
7780 type
= pointed_type(type
);
7781 c
+= index
* type_size(type
, &align
);
7785 error("too many field init");
7786 /* XXX: fix this mess by using explicit storage field */
7788 type1
.t
|= (type
->t
& ~VT_TYPE
);
7793 decl_initializer(type
, sec
, c
, 0, size_only
);
7795 /* XXX: make it more general */
7796 if (!size_only
&& nb_elems
> 1) {
7797 unsigned long c_end
;
7802 error("range init not supported yet for dynamic storage");
7803 c_end
= c
+ nb_elems
* elem_size
;
7804 if (c_end
> sec
->data_allocated
)
7805 section_realloc(sec
, c_end
);
7806 src
= sec
->data
+ c
;
7808 for(i
= 1; i
< nb_elems
; i
++) {
7810 memcpy(dst
, src
, elem_size
);
7816 #define EXPR_CONST 1
7819 /* store a value or an expression directly in global data or in local array */
7820 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
7821 int v
, int expr_type
)
7823 int saved_global_expr
, bt
, bit_pos
, bit_size
;
7825 unsigned long long bit_mask
;
7833 /* compound literals must be allocated globally in this case */
7834 saved_global_expr
= global_expr
;
7837 global_expr
= saved_global_expr
;
7838 /* NOTE: symbols are accepted */
7839 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
7840 error("initializer element is not constant");
7848 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
7851 /* XXX: not portable */
7852 /* XXX: generate error if incorrect relocation */
7853 gen_assign_cast(&dtype
);
7854 bt
= type
->t
& VT_BTYPE
;
7855 ptr
= sec
->data
+ c
;
7856 /* XXX: make code faster ? */
7857 if (!(type
->t
& VT_BITFIELD
)) {
7862 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
7863 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
7864 bit_mask
= (1LL << bit_size
) - 1;
7866 if ((vtop
->r
& VT_SYM
) &&
7872 (bt
== VT_INT
&& bit_size
!= 32)))
7873 error("initializer element is not computable at load time");
7876 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
7879 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
7882 *(double *)ptr
= vtop
->c
.d
;
7885 *(long double *)ptr
= vtop
->c
.ld
;
7888 *(long long *)ptr
|= (vtop
->c
.ll
& bit_mask
) << bit_pos
;
7891 if (vtop
->r
& VT_SYM
) {
7892 greloc(sec
, vtop
->sym
, c
, R_DATA_32
);
7894 *(int *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
7899 vset(&dtype
, VT_LOCAL
, c
);
7906 /* put zeros for variable based init */
7907 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
7910 /* nothing to do because globals are already set to zero */
7912 vpush_global_sym(&func_old_type
, TOK_memset
);
7920 /* 't' contains the type and storage info. 'c' is the offset of the
7921 object in section 'sec'. If 'sec' is NULL, it means stack based
7922 allocation. 'first' is true if array '{' must be read (multi
7923 dimension implicit array init handling). 'size_only' is true if
7924 size only evaluation is wanted (only for arrays). */
7925 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
7926 int first
, int size_only
)
7928 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, i
;
7929 int size1
, align1
, expr_type
;
7933 if (type
->t
& VT_ARRAY
) {
7937 t1
= pointed_type(type
);
7938 size1
= type_size(t1
, &align1
);
7941 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
7947 /* only parse strings here if correct type (otherwise: handle
7948 them as ((w)char *) expressions */
7949 if ((tok
== TOK_LSTR
&&
7950 (t1
->t
& VT_BTYPE
) == VT_INT
) ||
7952 (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
7953 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
7958 /* compute maximum number of chars wanted */
7960 cstr_len
= cstr
->size
;
7962 cstr_len
= cstr
->size
/ sizeof(int);
7965 if (n
>= 0 && nb
> (n
- array_length
))
7966 nb
= n
- array_length
;
7969 warning("initializer-string for array is too long");
7970 /* in order to go faster for common case (char
7971 string in global variable, we handle it
7973 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
7974 memcpy(sec
->data
+ c
+ array_length
, cstr
->data
, nb
);
7978 ch
= ((unsigned char *)cstr
->data
)[i
];
7980 ch
= ((int *)cstr
->data
)[i
];
7981 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
7989 /* only add trailing zero if enough storage (no
7990 warning in this case since it is standard) */
7991 if (n
< 0 || array_length
< n
) {
7993 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
7999 while (tok
!= '}') {
8000 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
8001 if (n
>= 0 && index
>= n
)
8002 error("index too large");
8003 /* must put zero in holes (note that doing it that way
8004 ensures that it even works with designators) */
8005 if (!size_only
&& array_length
< index
) {
8006 init_putz(t1
, sec
, c
+ array_length
* size1
,
8007 (index
- array_length
) * size1
);
8010 if (index
> array_length
)
8011 array_length
= index
;
8012 /* special test for multi dimensional arrays (may not
8013 be strictly correct if designators are used at the
8015 if (index
>= n
&& no_oblock
)
8024 /* put zeros at the end */
8025 if (!size_only
&& n
>= 0 && array_length
< n
) {
8026 init_putz(t1
, sec
, c
+ array_length
* size1
,
8027 (n
- array_length
) * size1
);
8029 /* patch type size if needed */
8031 s
->c
= array_length
;
8032 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
8033 (sec
|| !first
|| tok
== '{')) {
8036 /* NOTE: the previous test is a specific case for automatic
8037 struct/union init */
8038 /* XXX: union needs only one init */
8040 /* XXX: this test is incorrect for local initializers
8041 beginning with ( without {. It would be much more difficult
8042 to do it correctly (ideally, the expression parser should
8043 be used in all cases) */
8049 while (tok
== '(') {
8053 if (!parse_btype(&type1
, &ad1
))
8055 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
8057 if (!is_assignable_types(type
, &type1
))
8058 error("invalid type for cast");
8063 if (first
|| tok
== '{') {
8072 while (tok
!= '}') {
8073 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
8075 if (!size_only
&& array_length
< index
) {
8076 init_putz(type
, sec
, c
+ array_length
,
8077 index
- array_length
);
8079 index
= index
+ type_size(&f
->type
, &align1
);
8080 if (index
> array_length
)
8081 array_length
= index
;
8083 if (no_oblock
&& f
== NULL
)
8089 /* put zeros at the end */
8090 if (!size_only
&& array_length
< n
) {
8091 init_putz(type
, sec
, c
+ array_length
,
8100 } else if (tok
== '{') {
8102 decl_initializer(type
, sec
, c
, first
, size_only
);
8104 } else if (size_only
) {
8105 /* just skip expression */
8107 while ((parlevel
> 0 || (tok
!= '}' && tok
!= ',')) &&
8111 else if (tok
== ')')
8116 /* currently, we always use constant expression for globals
8117 (may change for scripting case) */
8118 expr_type
= EXPR_CONST
;
8120 expr_type
= EXPR_ANY
;
8121 init_putv(type
, sec
, c
, 0, expr_type
);
8125 /* parse an initializer for type 't' if 'has_init' is non zero, and
8126 allocate space in local or global data space ('r' is either
8127 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
8128 variable 'v' of scope 'scope' is declared before initializers are
8129 parsed. If 'v' is zero, then a reference to the new object is put
8130 in the value stack. If 'has_init' is 2, a special parsing is done
8131 to handle string constants. */
8132 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
8133 int has_init
, int v
, int scope
)
8135 int size
, align
, addr
, data_offset
;
8137 ParseState saved_parse_state
;
8138 TokenString init_str
;
8141 size
= type_size(type
, &align
);
8142 /* If unknown size, we must evaluate it before
8143 evaluating initializers because
8144 initializers can generate global data too
8145 (e.g. string pointers or ISOC99 compound
8146 literals). It also simplifies local
8147 initializers handling */
8148 tok_str_new(&init_str
);
8151 error("unknown type size");
8152 /* get all init string */
8153 if (has_init
== 2) {
8154 /* only get strings */
8155 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
8156 tok_str_add_tok(&init_str
);
8161 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
8163 error("unexpected end of file in initializer");
8164 tok_str_add_tok(&init_str
);
8167 else if (tok
== '}') {
8175 tok_str_add(&init_str
, -1);
8176 tok_str_add(&init_str
, 0);
8179 save_parse_state(&saved_parse_state
);
8181 macro_ptr
= init_str
.str
;
8183 decl_initializer(type
, NULL
, 0, 1, 1);
8184 /* prepare second initializer parsing */
8185 macro_ptr
= init_str
.str
;
8188 /* if still unknown size, error */
8189 size
= type_size(type
, &align
);
8191 error("unknown type size");
8193 /* take into account specified alignment if bigger */
8194 if (ad
->aligned
> align
)
8195 align
= ad
->aligned
;
8196 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
8198 if (do_bounds_check
&& (type
->t
& VT_ARRAY
))
8200 loc
= (loc
- size
) & -align
;
8202 /* handles bounds */
8203 /* XXX: currently, since we do only one pass, we cannot track
8204 '&' operators, so we add only arrays */
8205 if (do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
8206 unsigned long *bounds_ptr
;
8207 /* add padding between regions */
8209 /* then add local bound info */
8210 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(unsigned long));
8211 bounds_ptr
[0] = addr
;
8212 bounds_ptr
[1] = size
;
8215 /* local variable */
8216 sym_push(v
, type
, r
, addr
);
8218 /* push local reference */
8219 vset(type
, r
, addr
);
8225 if (v
&& scope
== VT_CONST
) {
8226 /* see if the symbol was already defined */
8229 if (!is_compatible_types(&sym
->type
, type
))
8230 error("incompatible types for redefinition of '%s'",
8231 get_tok_str(v
, NULL
));
8232 if (sym
->type
.t
& VT_EXTERN
) {
8233 /* if the variable is extern, it was not allocated */
8234 sym
->type
.t
&= ~VT_EXTERN
;
8236 /* we accept several definitions of the same
8237 global variable. this is tricky, because we
8238 must play with the SHN_COMMON type of the symbol */
8239 /* XXX: should check if the variable was already
8240 initialized. It is incorrect to initialized it
8242 /* no init data, we won't add more to the symbol */
8249 /* allocate symbol in corresponding section */
8256 data_offset
= sec
->data_offset
;
8257 data_offset
= (data_offset
+ align
- 1) & -align
;
8259 /* very important to increment global pointer at this time
8260 because initializers themselves can create new initializers */
8261 data_offset
+= size
;
8262 /* add padding if bound check */
8263 if (do_bounds_check
)
8265 sec
->data_offset
= data_offset
;
8266 /* allocate section space to put the data */
8267 if (sec
->sh_type
!= SHT_NOBITS
&&
8268 data_offset
> sec
->data_allocated
)
8269 section_realloc(sec
, data_offset
);
8271 addr
= 0; /* avoid warning */
8275 if (scope
== VT_CONST
) {
8280 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
8282 /* update symbol definition */
8284 put_extern_sym(sym
, sec
, addr
, size
);
8287 /* put a common area */
8288 put_extern_sym(sym
, NULL
, align
, size
);
8289 /* XXX: find a nicer way */
8290 esym
= &((Elf32_Sym
*)symtab_section
->data
)[sym
->c
];
8291 esym
->st_shndx
= SHN_COMMON
;
8296 /* push global reference */
8297 sym
= get_sym_ref(type
, sec
, addr
, size
);
8299 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
8303 /* handles bounds now because the symbol must be defined
8304 before for the relocation */
8305 if (do_bounds_check
) {
8306 unsigned long *bounds_ptr
;
8308 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_32
);
8309 /* then add global bound info */
8310 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(long));
8311 bounds_ptr
[0] = 0; /* relocated */
8312 bounds_ptr
[1] = size
;
8316 decl_initializer(type
, sec
, addr
, 1, 0);
8317 /* restore parse state if needed */
8319 tok_str_free(init_str
.str
);
8320 restore_parse_state(&saved_parse_state
);
8326 void put_func_debug(Sym
*sym
)
8331 /* XXX: we put here a dummy type */
8332 snprintf(buf
, sizeof(buf
), "%s:%c1",
8333 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
8334 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
8335 cur_text_section
, sym
->c
);
8340 /* not finished : try to put some local vars in registers */
8341 //#define CONFIG_REG_VARS
8343 #ifdef CONFIG_REG_VARS
8344 void add_var_ref(int t
)
8346 printf("%s:%d: &%s\n",
8347 file
->filename
, file
->line_num
,
8348 get_tok_str(t
, NULL
));
8351 /* first pass on a function with heuristic to extract variable usage
8352 and pointer references to local variables for register allocation */
8353 void analyse_function(void)
8360 /* any symbol coming after '&' is considered as being a
8361 variable whose reference is taken. It is highly unaccurate
8362 but it is difficult to do better without a complete parse */
8365 /* if '& number', then no need to examine next tokens */
8366 if (tok
== TOK_CINT
||
8368 tok
== TOK_CLLONG
||
8369 tok
== TOK_CULLONG
) {
8371 } else if (tok
>= TOK_UIDENT
) {
8372 /* if '& ident [' or '& ident ->', then ident address
8376 if (tok
!= '[' && tok
!= TOK_ARROW
)
8380 while (tok
!= '}' && tok
!= ';' &&
8381 !((tok
== ',' || tok
== ')') && level
== 0)) {
8382 if (tok
>= TOK_UIDENT
) {
8384 } else if (tok
== '(') {
8386 } else if (tok
== ')') {
8399 /* parse an old style function declaration list */
8400 /* XXX: check multiple parameter */
8401 static void func_decl_list(Sym
*func_sym
)
8408 /* parse each declaration */
8409 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
) {
8410 if (!parse_btype(&btype
, &ad
))
8411 expect("declaration list");
8412 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
8413 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
8415 /* we accept no variable after */
8419 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
8420 /* find parameter in function parameter list */
8423 if ((s
->v
& ~SYM_FIELD
) == v
)
8427 error("declaration for parameter '%s' but no such parameter",
8428 get_tok_str(v
, NULL
));
8430 /* check that no storage specifier except 'register' was given */
8431 if (type
.t
& VT_STORAGE
)
8432 error("storage class specified for '%s'", get_tok_str(v
, NULL
));
8433 convert_parameter_type(&type
);
8434 /* we can add the type (NOTE: it could be local to the function) */
8436 /* accept other parameters */
8447 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
8448 static void decl(int l
)
8456 if (!parse_btype(&btype
, &ad
)) {
8457 /* skip redundant ';' */
8458 /* XXX: find more elegant solution */
8463 /* special test for old K&R protos without explicit int
8464 type. Only accepted when defining global data */
8465 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
8469 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
8470 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
8472 /* we accept no variable after */
8476 while (1) { /* iterate thru each declaration */
8478 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
8482 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
8483 printf("type = '%s'\n", buf
);
8486 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
8487 /* if old style function prototype, we accept a
8490 if (sym
->c
== FUNC_OLD
)
8491 func_decl_list(sym
);
8495 #ifdef CONFIG_REG_VARS
8496 TokenString func_str
;
8497 ParseState saved_parse_state
;
8502 error("cannot use local functions");
8503 if (!(type
.t
& VT_FUNC
))
8504 expect("function definition");
8505 /* XXX: cannot do better now: convert extern line to static inline */
8506 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
8507 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
8509 #ifdef CONFIG_REG_VARS
8510 /* parse all function code and record it */
8512 tok_str_new(&func_str
);
8518 error("unexpected end of file");
8519 tok_str_add_tok(&func_str
);
8524 } else if (t
== '}') {
8526 if (block_level
== 0)
8530 tok_str_add(&func_str
, -1);
8531 tok_str_add(&func_str
, 0);
8533 save_parse_state(&saved_parse_state
);
8535 macro_ptr
= func_str
.str
;
8540 /* compute text section */
8541 cur_text_section
= ad
.section
;
8542 if (!cur_text_section
)
8543 cur_text_section
= text_section
;
8544 ind
= cur_text_section
->data_offset
;
8545 funcname
= get_tok_str(v
, NULL
);
8548 /* if symbol is already defined, then put complete type */
8551 /* put function symbol */
8552 sym
= global_identifier_push(v
, type
.t
, 0);
8553 sym
->type
.ref
= type
.ref
;
8555 /* NOTE: we patch the symbol size later */
8556 put_extern_sym(sym
, cur_text_section
, ind
, 0);
8558 sym
->r
= VT_SYM
| VT_CONST
;
8559 /* put debug symbol */
8561 put_func_debug(sym
);
8562 /* push a dummy symbol to enable local sym storage */
8563 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
8564 gfunc_prolog(&type
);
8567 #ifdef CONFIG_REG_VARS
8568 macro_ptr
= func_str
.str
;
8571 block(NULL
, NULL
, NULL
, NULL
, 0, 0);
8574 cur_text_section
->data_offset
= ind
;
8575 label_pop(&global_label_stack
, NULL
);
8576 sym_pop(&local_stack
, NULL
); /* reset local stack */
8577 /* end of function */
8578 /* patch symbol size */
8579 ((Elf32_Sym
*)symtab_section
->data
)[sym
->c
].st_size
=
8582 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
8584 funcname
= ""; /* for safety */
8585 func_vt
.t
= VT_VOID
; /* for safety */
8586 ind
= 0; /* for safety */
8588 #ifdef CONFIG_REG_VARS
8589 tok_str_free(func_str
.str
);
8590 restore_parse_state(&saved_parse_state
);
8594 if (btype
.t
& VT_TYPEDEF
) {
8595 /* save typedefed type */
8596 /* XXX: test storage specifiers ? */
8597 sym
= sym_push(v
, &type
, 0, 0);
8598 sym
->type
.t
|= VT_TYPEDEF
;
8599 } else if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
8600 /* external function definition */
8601 external_sym(v
, &type
, 0);
8603 /* not lvalue if array */
8605 if (!(type
.t
& VT_ARRAY
))
8606 r
|= lvalue_type(type
.t
);
8607 has_init
= (tok
== '=');
8608 if ((btype
.t
& VT_EXTERN
) ||
8609 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
8610 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
8611 /* external variable */
8612 /* NOTE: as GCC, uninitialized global static
8613 arrays of null size are considered as
8615 external_sym(v
, &type
, r
);
8617 if (type
.t
& VT_STATIC
)
8623 decl_initializer_alloc(&type
, &ad
, r
,
8637 /* better than nothing, but needs extension to handle '-E' option
8639 static void preprocess_init(TCCState
*s1
)
8641 s1
->include_stack_ptr
= s1
->include_stack
;
8642 /* XXX: move that before to avoid having to initialize
8643 file->ifdef_stack_ptr ? */
8644 s1
->ifdef_stack_ptr
= s1
->ifdef_stack
;
8645 file
->ifdef_stack_ptr
= s1
->ifdef_stack_ptr
;
8647 /* XXX: not ANSI compliant: bound checking says error */
8651 /* compile the C file opened in 'file'. Return non zero if errors. */
8652 static int tcc_compile(TCCState
*s1
)
8656 volatile int section_sym
;
8659 printf("%s: **** new file\n", file
->filename
);
8661 preprocess_init(s1
);
8664 anon_sym
= SYM_FIRST_ANOM
;
8666 /* file info: full path + filename */
8667 section_sym
= 0; /* avoid warning */
8669 section_sym
= put_elf_sym(symtab_section
, 0, 0,
8670 ELF32_ST_INFO(STB_LOCAL
, STT_SECTION
), 0,
8671 text_section
->sh_num
, NULL
);
8672 getcwd(buf
, sizeof(buf
));
8673 pstrcat(buf
, sizeof(buf
), "/");
8674 put_stabs_r(buf
, N_SO
, 0, 0,
8675 text_section
->data_offset
, text_section
, section_sym
);
8676 put_stabs_r(file
->filename
, N_SO
, 0, 0,
8677 text_section
->data_offset
, text_section
, section_sym
);
8679 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
8680 symbols can be safely used */
8681 put_elf_sym(symtab_section
, 0, 0,
8682 ELF32_ST_INFO(STB_LOCAL
, STT_FILE
), 0,
8683 SHN_ABS
, file
->filename
);
8685 /* define some often used types */
8686 int_type
.t
= VT_INT
;
8688 char_pointer_type
.t
= VT_BYTE
;
8689 mk_pointer(&char_pointer_type
);
8691 func_old_type
.t
= VT_FUNC
;
8692 func_old_type
.ref
= sym_push(SYM_FIELD
, &int_type
, FUNC_CDECL
, FUNC_OLD
);
8695 /* define 'void *alloca(unsigned int)' builtin function */
8700 sym
= sym_push(p
, mk_pointer(VT_VOID
), FUNC_CDECL
, FUNC_NEW
);
8701 s1
= sym_push(SYM_FIELD
, VT_UNSIGNED
| VT_INT
, 0, 0);
8704 sym_push(TOK_alloca
, VT_FUNC
| (p
<< VT_STRUCT_SHIFT
), VT_CONST
, 0);
8708 define_start
= define_stack
;
8710 if (setjmp(s1
->error_jmp_buf
) == 0) {
8712 s1
->error_set_jmp_enabled
= 1;
8714 ch
= file
->buf_ptr
[0];
8715 tok_flags
= TOK_FLAG_BOL
| TOK_FLAG_BOF
;
8716 parse_flags
= PARSE_FLAG_PREPROCESS
| PARSE_FLAG_TOK_NUM
;
8720 expect("declaration");
8722 /* end of translation unit info */
8724 put_stabs_r(NULL
, N_SO
, 0, 0,
8725 text_section
->data_offset
, text_section
, section_sym
);
8728 s1
->error_set_jmp_enabled
= 0;
8730 /* reset define stack, but leave -Dsymbols (may be incorrect if
8731 they are undefined) */
8732 free_defines(define_start
);
8734 sym_pop(&global_stack
, NULL
);
8736 return s1
->nb_errors
!= 0 ? -1 : 0;
8740 int tcc_compile_string(TCCState
*s
, const char *str
)
8742 BufferedFile bf1
, *bf
= &bf1
;
8746 /* init file structure */
8748 /* XXX: avoid copying */
8750 buf
= tcc_malloc(len
+ 1);
8753 memcpy(buf
, str
, len
);
8756 bf
->buf_end
= buf
+ len
;
8757 pstrcpy(bf
->filename
, sizeof(bf
->filename
), "<string>");
8761 ret
= tcc_compile(s
);
8765 /* currently, no need to close */
8770 /* define a preprocessor symbol. A value can also be provided with the '=' operator */
8771 void tcc_define_symbol(TCCState
*s1
, const char *sym
, const char *value
)
8773 BufferedFile bf1
, *bf
= &bf1
;
8775 pstrcpy(bf
->buffer
, IO_BUF_SIZE
, sym
);
8776 pstrcat(bf
->buffer
, IO_BUF_SIZE
, " ");
8780 pstrcat(bf
->buffer
, IO_BUF_SIZE
, value
);
8782 /* init file structure */
8784 bf
->buf_ptr
= bf
->buffer
;
8785 bf
->buf_end
= bf
->buffer
+ strlen(bf
->buffer
);
8786 *bf
->buf_end
= CH_EOB
;
8787 bf
->filename
[0] = '\0';
8791 s1
->include_stack_ptr
= s1
->include_stack
;
8793 /* parse with define parser */
8794 ch
= file
->buf_ptr
[0];
8800 /* undefine a preprocessor symbol */
8801 void tcc_undefine_symbol(TCCState
*s1
, const char *sym
)
8805 ts
= tok_alloc(sym
, strlen(sym
));
8806 s
= define_find(ts
->tok
);
8807 /* undefine symbol by putting an invalid name */
8812 #ifdef CONFIG_TCC_ASM
8814 #include "i386-asm.c"
8818 static void asm_instr(void)
8820 error("inline asm() not supported");
8826 /* print the position in the source file of PC value 'pc' by reading
8827 the stabs debug information */
8828 static void rt_printline(unsigned long wanted_pc
)
8830 Stab_Sym
*sym
, *sym_end
;
8831 char func_name
[128], last_func_name
[128];
8832 unsigned long func_addr
, last_pc
, pc
;
8833 const char *incl_files
[INCLUDE_STACK_SIZE
];
8834 int incl_index
, len
, last_line_num
, i
;
8835 const char *str
, *p
;
8837 fprintf(stderr
, "0x%08lx:", wanted_pc
);
8839 func_name
[0] = '\0';
8842 last_func_name
[0] = '\0';
8843 last_pc
= 0xffffffff;
8845 sym
= (Stab_Sym
*)stab_section
->data
+ 1;
8846 sym_end
= (Stab_Sym
*)(stab_section
->data
+ stab_section
->data_offset
);
8847 while (sym
< sym_end
) {
8848 switch(sym
->n_type
) {
8849 /* function start or end */
8851 if (sym
->n_strx
== 0) {
8852 /* we test if between last line and end of function */
8853 pc
= sym
->n_value
+ func_addr
;
8854 if (wanted_pc
>= last_pc
&& wanted_pc
< pc
)
8856 func_name
[0] = '\0';
8859 str
= stabstr_section
->data
+ sym
->n_strx
;
8860 p
= strchr(str
, ':');
8862 pstrcpy(func_name
, sizeof(func_name
), str
);
8865 if (len
> sizeof(func_name
) - 1)
8866 len
= sizeof(func_name
) - 1;
8867 memcpy(func_name
, str
, len
);
8868 func_name
[len
] = '\0';
8870 func_addr
= sym
->n_value
;
8873 /* line number info */
8875 pc
= sym
->n_value
+ func_addr
;
8876 if (wanted_pc
>= last_pc
&& wanted_pc
< pc
)
8879 last_line_num
= sym
->n_desc
;
8881 strcpy(last_func_name
, func_name
);
8885 str
= stabstr_section
->data
+ sym
->n_strx
;
8887 if (incl_index
< INCLUDE_STACK_SIZE
) {
8888 incl_files
[incl_index
++] = str
;
8896 if (sym
->n_strx
== 0) {
8897 incl_index
= 0; /* end of translation unit */
8899 str
= stabstr_section
->data
+ sym
->n_strx
;
8900 /* do not add path */
8902 if (len
> 0 && str
[len
- 1] != '/')
8910 /* second pass: we try symtab symbols (no line number info) */
8913 Elf32_Sym
*sym
, *sym_end
;
8916 sym_end
= (Elf32_Sym
*)(symtab_section
->data
+ symtab_section
->data_offset
);
8917 for(sym
= (Elf32_Sym
*)symtab_section
->data
+ 1;
8920 type
= ELF32_ST_TYPE(sym
->st_info
);
8921 if (type
== STT_FUNC
) {
8922 if (wanted_pc
>= sym
->st_value
&&
8923 wanted_pc
< sym
->st_value
+ sym
->st_size
) {
8924 pstrcpy(last_func_name
, sizeof(last_func_name
),
8925 strtab_section
->data
+ sym
->st_name
);
8931 /* did not find any info: */
8932 fprintf(stderr
, " ???\n");
8935 if (last_func_name
[0] != '\0') {
8936 fprintf(stderr
, " %s()", last_func_name
);
8938 if (incl_index
> 0) {
8939 fprintf(stderr
, " (%s:%d",
8940 incl_files
[incl_index
- 1], last_line_num
);
8941 for(i
= incl_index
- 2; i
>= 0; i
--)
8942 fprintf(stderr
, ", included from %s", incl_files
[i
]);
8943 fprintf(stderr
, ")");
8945 fprintf(stderr
, "\n");
8952 /* fix for glibc 2.1 */
8958 /* return the PC at frame level 'level'. Return non zero if not found */
8959 static int rt_get_caller_pc(unsigned long *paddr
,
8960 ucontext_t
*uc
, int level
)
8967 *paddr
= uc
->uc_mcontext
.mc_eip
;
8969 *paddr
= uc
->uc_mcontext
.gregs
[REG_EIP
];
8974 fp
= uc
->uc_mcontext
.mc_ebp
;
8976 fp
= uc
->uc_mcontext
.gregs
[REG_EBP
];
8978 for(i
=1;i
<level
;i
++) {
8979 /* XXX: check address validity with program info */
8980 if (fp
<= 0x1000 || fp
>= 0xc0000000)
8982 fp
= ((unsigned long *)fp
)[0];
8984 *paddr
= ((unsigned long *)fp
)[1];
8989 #error add arch specific rt_get_caller_pc()
8992 /* emit a run time error at position 'pc' */
8993 void rt_error(ucontext_t
*uc
, const char *fmt
, ...)
9000 fprintf(stderr
, "Runtime error: ");
9001 vfprintf(stderr
, fmt
, ap
);
9002 fprintf(stderr
, "\n");
9003 for(i
=0;i
<num_callers
;i
++) {
9004 if (rt_get_caller_pc(&pc
, uc
, i
) < 0)
9007 fprintf(stderr
, "at ");
9009 fprintf(stderr
, "by ");
9016 /* signal handler for fatal errors */
9017 static void sig_error(int signum
, siginfo_t
*siginf
, void *puc
)
9019 ucontext_t
*uc
= puc
;
9023 switch(siginf
->si_code
) {
9026 rt_error(uc
, "division by zero");
9029 rt_error(uc
, "floating point exception");
9035 if (rt_bound_error_msg
&& *rt_bound_error_msg
)
9036 rt_error(uc
, *rt_bound_error_msg
);
9038 rt_error(uc
, "dereferencing invalid pointer");
9041 rt_error(uc
, "illegal instruction");
9044 rt_error(uc
, "abort() called");
9047 rt_error(uc
, "caught signal %d", signum
);
9054 /* do all relocations (needed before using tcc_get_symbol()) */
9055 int tcc_relocate(TCCState
*s1
)
9062 tcc_add_runtime(s1
);
9064 relocate_common_syms();
9066 /* compute relocation address : section are relocated in place. We
9067 also alloc the bss space */
9068 for(i
= 1; i
< s1
->nb_sections
; i
++) {
9069 s
= s1
->sections
[i
];
9070 if (s
->sh_flags
& SHF_ALLOC
) {
9071 if (s
->sh_type
== SHT_NOBITS
)
9072 s
->data
= tcc_mallocz(s
->data_offset
);
9073 s
->sh_addr
= (unsigned long)s
->data
;
9077 relocate_syms(s1
, 1);
9079 if (s1
->nb_errors
!= 0)
9082 /* relocate each section */
9083 for(i
= 1; i
< s1
->nb_sections
; i
++) {
9084 s
= s1
->sections
[i
];
9086 relocate_section(s1
, s
);
9091 /* launch the compiled program with the given arguments */
9092 int tcc_run(TCCState
*s1
, int argc
, char **argv
)
9094 int (*prog_main
)(int, char **);
9096 if (tcc_relocate(s1
) < 0)
9099 prog_main
= tcc_get_symbol(s1
, "main");
9103 error("debug mode currently not available for Windows");
9105 struct sigaction sigact
;
9106 /* install TCC signal handlers to print debug info on fatal
9108 sigact
.sa_flags
= SA_SIGINFO
| SA_RESETHAND
;
9109 sigact
.sa_sigaction
= sig_error
;
9110 sigemptyset(&sigact
.sa_mask
);
9111 sigaction(SIGFPE
, &sigact
, NULL
);
9112 sigaction(SIGILL
, &sigact
, NULL
);
9113 sigaction(SIGSEGV
, &sigact
, NULL
);
9114 sigaction(SIGBUS
, &sigact
, NULL
);
9115 sigaction(SIGABRT
, &sigact
, NULL
);
9119 #ifdef CONFIG_TCC_BCHECK
9120 if (do_bounds_check
) {
9121 void (*bound_init
)(void);
9123 /* set error function */
9124 rt_bound_error_msg
= (void *)tcc_get_symbol(s1
, "__bound_error_msg");
9126 /* XXX: use .init section so that it also work in binary ? */
9127 bound_init
= (void *)tcc_get_symbol(s1
, "__bound_init");
9131 return (*prog_main
)(argc
, argv
);
9134 TCCState
*tcc_new(void)
9141 s
= tcc_mallocz(sizeof(TCCState
));
9145 s
->output_type
= TCC_OUTPUT_MEMORY
;
9147 /* init isid table */
9149 isidnum_table
[i
] = isid(i
) || isnum(i
);
9151 /* add all tokens */
9153 memset(hash_ident
, 0, TOK_HASH_SIZE
* sizeof(TokenSym
*));
9155 tok_ident
= TOK_IDENT
;
9164 ts
= tok_alloc(p
, r
- p
- 1);
9168 /* we add dummy defines for some special macros to speed up tests
9169 and to have working defined() */
9170 define_push(TOK___LINE__
, MACRO_OBJ
, NULL
, NULL
);
9171 define_push(TOK___FILE__
, MACRO_OBJ
, NULL
, NULL
);
9172 define_push(TOK___DATE__
, MACRO_OBJ
, NULL
, NULL
);
9173 define_push(TOK___TIME__
, MACRO_OBJ
, NULL
, NULL
);
9175 /* standard defines */
9176 tcc_define_symbol(s
, "__STDC__", NULL
);
9177 #if defined(TCC_TARGET_I386)
9178 tcc_define_symbol(s
, "__i386__", NULL
);
9181 tcc_define_symbol(s
, "__linux__", NULL
);
9182 tcc_define_symbol(s
, "linux", NULL
);
9184 /* tiny C specific defines */
9185 tcc_define_symbol(s
, "__TINYC__", NULL
);
9187 /* tiny C & gcc defines */
9188 tcc_define_symbol(s
, "__SIZE_TYPE__", "unsigned int");
9189 tcc_define_symbol(s
, "__PTRDIFF_TYPE__", "int");
9190 tcc_define_symbol(s
, "__WCHAR_TYPE__", "int");
9192 /* default library paths */
9193 tcc_add_library_path(s
, "/usr/local/lib");
9194 tcc_add_library_path(s
, "/usr/lib");
9195 tcc_add_library_path(s
, "/lib");
9197 /* no section zero */
9198 dynarray_add((void ***)&s
->sections
, &s
->nb_sections
, NULL
);
9200 /* create standard sections */
9201 text_section
= new_section(s
, ".text", SHT_PROGBITS
, SHF_ALLOC
| SHF_EXECINSTR
);
9202 data_section
= new_section(s
, ".data", SHT_PROGBITS
, SHF_ALLOC
| SHF_WRITE
);
9203 bss_section
= new_section(s
, ".bss", SHT_NOBITS
, SHF_ALLOC
| SHF_WRITE
);
9205 /* symbols are always generated for linking stage */
9206 symtab_section
= new_symtab(s
, ".symtab", SHT_SYMTAB
, 0,
9208 ".hashtab", SHF_PRIVATE
);
9209 strtab_section
= symtab_section
->link
;
9211 /* private symbol table for dynamic symbols */
9212 s
->dynsymtab_section
= new_symtab(s
, ".dynsymtab", SHT_SYMTAB
, SHF_PRIVATE
,
9214 ".dynhashtab", SHF_PRIVATE
);
9215 s
->alacarte_link
= 1;
9219 void tcc_delete(TCCState
*s1
)
9223 /* free -D defines */
9227 n
= tok_ident
- TOK_IDENT
;
9228 for(i
= 0; i
< n
; i
++)
9229 tcc_free(table_ident
[i
]);
9230 tcc_free(table_ident
);
9232 /* free all sections */
9234 free_section(symtab_section
->hash
);
9236 free_section(s1
->dynsymtab_section
->hash
);
9237 free_section(s1
->dynsymtab_section
->link
);
9238 free_section(s1
->dynsymtab_section
);
9240 for(i
= 1; i
< s1
->nb_sections
; i
++)
9241 free_section(s1
->sections
[i
]);
9242 tcc_free(s1
->sections
);
9244 /* free loaded dlls array */
9245 for(i
= 0; i
< s1
->nb_loaded_dlls
; i
++)
9246 tcc_free(s1
->loaded_dlls
[i
]);
9247 tcc_free(s1
->loaded_dlls
);
9250 for(i
= 0; i
< s1
->nb_library_paths
; i
++)
9251 tcc_free(s1
->library_paths
[i
]);
9252 tcc_free(s1
->library_paths
);
9254 /* cached includes */
9255 for(i
= 0; i
< s1
->nb_cached_includes
; i
++)
9256 tcc_free(s1
->cached_includes
[i
]);
9257 tcc_free(s1
->cached_includes
);
9259 for(i
= 0; i
< s1
->nb_include_paths
; i
++)
9260 tcc_free(s1
->include_paths
[i
]);
9261 tcc_free(s1
->include_paths
);
9263 for(i
= 0; i
< s1
->nb_sysinclude_paths
; i
++)
9264 tcc_free(s1
->sysinclude_paths
[i
]);
9265 tcc_free(s1
->sysinclude_paths
);
9270 int tcc_add_include_path(TCCState
*s1
, const char *pathname
)
9274 pathname1
= tcc_strdup(pathname
);
9275 dynarray_add((void ***)&s1
->include_paths
, &s1
->nb_include_paths
, pathname1
);
9279 int tcc_add_sysinclude_path(TCCState
*s1
, const char *pathname
)
9283 pathname1
= tcc_strdup(pathname
);
9284 dynarray_add((void ***)&s1
->sysinclude_paths
, &s1
->nb_sysinclude_paths
, pathname1
);
9288 static int tcc_add_file_internal(TCCState
*s1
, const char *filename
, int flags
)
9290 const char *ext
, *filename1
;
9293 BufferedFile
*saved_file
;
9295 /* find source file type with extension */
9296 filename1
= strrchr(filename
, '/');
9300 filename1
= filename
;
9301 ext
= strrchr(filename1
, '.');
9307 file
= tcc_open(s1
, filename
);
9309 if (flags
& AFF_PRINT_ERROR
) {
9310 error_noabort("file '%s' not found", filename
);
9316 if (!ext
|| !strcmp(ext
, "c")) {
9317 /* C file assumed */
9318 ret
= tcc_compile(s1
);
9320 #ifdef CONFIG_TCC_ASM
9321 if (!strcmp(ext
, "S")) {
9322 /* preprocessed assembler */
9323 ret
= tcc_assemble(s1
, 1);
9324 } else if (!strcmp(ext
, "s")) {
9325 /* non preprocessed assembler */
9326 ret
= tcc_assemble(s1
, 0);
9331 /* assume executable format: auto guess file type */
9332 if (read(fd
, &ehdr
, sizeof(ehdr
)) != sizeof(ehdr
)) {
9333 error_noabort("could not read header");
9336 lseek(fd
, 0, SEEK_SET
);
9338 if (ehdr
.e_ident
[0] == ELFMAG0
&&
9339 ehdr
.e_ident
[1] == ELFMAG1
&&
9340 ehdr
.e_ident
[2] == ELFMAG2
&&
9341 ehdr
.e_ident
[3] == ELFMAG3
) {
9342 file
->line_num
= 0; /* do not display line number if error */
9343 if (ehdr
.e_type
== ET_REL
) {
9344 ret
= tcc_load_object_file(s1
, fd
, 0);
9345 } else if (ehdr
.e_type
== ET_DYN
) {
9346 ret
= tcc_load_dll(s1
, fd
, filename
,
9347 (flags
& AFF_REFERENCED_DLL
) != 0);
9349 error_noabort("unrecognized ELF file");
9352 } else if (memcmp((char *)&ehdr
, ARMAG
, 8) == 0) {
9353 file
->line_num
= 0; /* do not display line number if error */
9354 ret
= tcc_load_archive(s1
, fd
);
9356 /* as GNU ld, consider it is an ld script if not recognized */
9357 ret
= tcc_load_ldscript(s1
);
9359 error_noabort("unrecognized file type");
9374 int tcc_add_file(TCCState
*s
, const char *filename
)
9376 return tcc_add_file_internal(s
, filename
, AFF_PRINT_ERROR
);
9379 int tcc_add_library_path(TCCState
*s
, const char *pathname
)
9383 pathname1
= tcc_strdup(pathname
);
9384 dynarray_add((void ***)&s
->library_paths
, &s
->nb_library_paths
, pathname1
);
9388 /* find and load a dll. Return non zero if not found */
9389 /* XXX: add '-rpath' option support ? */
9390 static int tcc_add_dll(TCCState
*s
, const char *filename
, int flags
)
9395 for(i
= 0; i
< s
->nb_library_paths
; i
++) {
9396 snprintf(buf
, sizeof(buf
), "%s/%s",
9397 s
->library_paths
[i
], filename
);
9398 if (tcc_add_file_internal(s
, buf
, flags
) == 0)
9404 /* the library name is the same as the argument of the '-l' option */
9405 int tcc_add_library(TCCState
*s
, const char *libraryname
)
9411 /* first we look for the dynamic library if not static linking */
9412 if (!s
->static_link
) {
9413 snprintf(buf
, sizeof(buf
), "lib%s.so", libraryname
);
9414 /* if we output to memory, then we simply we dlopen(). */
9415 if (s
->output_type
== TCC_OUTPUT_MEMORY
) {
9416 /* Since the libc is already loaded, we don't need to load it again */
9417 if (!strcmp(libraryname
, "c"))
9419 h
= dlopen(buf
, RTLD_GLOBAL
| RTLD_LAZY
);
9423 if (tcc_add_dll(s
, buf
, 0) == 0)
9428 /* then we look for the static library */
9429 for(i
= 0; i
< s
->nb_library_paths
; i
++) {
9430 snprintf(buf
, sizeof(buf
), "%s/lib%s.a",
9431 s
->library_paths
[i
], libraryname
);
9432 if (tcc_add_file_internal(s
, buf
, 0) == 0)
9438 int tcc_add_symbol(TCCState
*s
, const char *name
, unsigned long val
)
9440 add_elf_sym(symtab_section
, val
, 0,
9441 ELF32_ST_INFO(STB_GLOBAL
, STT_NOTYPE
),
9446 int tcc_set_output_type(TCCState
*s
, int output_type
)
9450 s
->output_type
= output_type
;
9453 /* default include paths */
9454 /* XXX: reverse order needed if -isystem support */
9455 tcc_add_sysinclude_path(s
, "/usr/local/include");
9456 tcc_add_sysinclude_path(s
, "/usr/include");
9457 snprintf(buf
, sizeof(buf
), "%s/include", tcc_lib_path
);
9458 tcc_add_sysinclude_path(s
, buf
);
9461 /* if bound checking, then add corresponding sections */
9462 #ifdef CONFIG_TCC_BCHECK
9463 if (do_bounds_check
) {
9465 tcc_define_symbol(s
, "__BOUNDS_CHECKING_ON", NULL
);
9466 /* create bounds sections */
9467 bounds_section
= new_section(s
, ".bounds",
9468 SHT_PROGBITS
, SHF_ALLOC
);
9469 lbounds_section
= new_section(s
, ".lbounds",
9470 SHT_PROGBITS
, SHF_ALLOC
);
9474 /* add debug sections */
9477 stab_section
= new_section(s
, ".stab", SHT_PROGBITS
, 0);
9478 stab_section
->sh_entsize
= sizeof(Stab_Sym
);
9479 stabstr_section
= new_section(s
, ".stabstr", SHT_STRTAB
, 0);
9480 put_elf_str(stabstr_section
, "");
9481 stab_section
->link
= stabstr_section
;
9482 /* put first entry */
9483 put_stabs("", 0, 0, 0, 0);
9486 /* add libc crt1/crti objects */
9487 if ((output_type
== TCC_OUTPUT_EXE
|| output_type
== TCC_OUTPUT_DLL
) &&
9489 if (output_type
!= TCC_OUTPUT_DLL
)
9490 tcc_add_file(s
, CONFIG_TCC_CRT_PREFIX
"/crt1.o");
9491 tcc_add_file(s
, CONFIG_TCC_CRT_PREFIX
"/crti.o");
9496 #define WD_ALL 0x0001 /* warning is activated when using -Wall */
9498 typedef struct WarningDef
{
9504 static const WarningDef warning_defs
[] = {
9505 { offsetof(TCCState
, warn_unsupported
), 0, "unsupported" },
9506 { offsetof(TCCState
, warn_write_strings
), 0, "write-strings" },
9507 { offsetof(TCCState
, warn_error
), 0, "error" },
9510 /* set/reset a warning */
9511 int tcc_set_warning(TCCState
*s
, const char *warning_name
, int value
)
9514 const WarningDef
*p
;
9515 if (!strcmp(warning_name
, "all")) {
9516 for(i
= 0, p
= warning_defs
; i
< countof(warning_defs
); i
++, p
++) {
9517 if (p
->flags
& WD_ALL
)
9518 *(int *)((uint8_t *)s
+ p
->offset
) = 1;
9521 for(i
= 0, p
= warning_defs
; i
< countof(warning_defs
); i
++, p
++) {
9522 if (!strcmp(warning_name
, p
->name
))
9527 *(int *)((uint8_t *)s
+ p
->offset
) = value
;
9533 #if !defined(LIBTCC)
9535 /* extract the basename of a file */
9536 static const char *tcc_basename(const char *name
)
9539 p
= strrchr(name
, '/');
9542 p
= strrchr(name
, '\\');
9551 static int64_t getclock_us(void)
9556 return (tb
.time
* 1000LL + tb
.millitm
) * 1000LL;
9559 gettimeofday(&tv
, NULL
);
9560 return tv
.tv_sec
* 1000000LL + tv
.tv_usec
;
9566 printf("tcc version " TCC_VERSION
" - Tiny C Compiler - Copyright (C) 2001-2003 Fabrice Bellard\n"
9567 "usage: tcc [-v] [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"
9568 " [-Wwarn] [-g] [-b] [-bt N] [-Ldir] [-llib] [-shared] [-static]\n"
9569 " [infile1 infile2...] [-run infile args...]\n"
9571 "General options:\n"
9572 " -v display current version\n"
9573 " -c compile only - generate an object file\n"
9574 " -o outfile set output filename\n"
9575 " -Bdir set tcc internal library path\n"
9576 " -bench output compilation statistics\n"
9577 " -run run compiled source\n"
9578 " -Wwarning set or reset (with 'no-' prefix) 'warning'\n"
9579 "Preprocessor options:\n"
9580 " -Idir add include path 'dir'\n"
9581 " -Dsym[=val] define 'sym' with value 'val'\n"
9582 " -Usym undefine 'sym'\n"
9584 " -Ldir add library path 'dir'\n"
9585 " -llib link with dynamic or static library 'lib'\n"
9586 " -shared generate a shared library\n"
9587 " -static static linking\n"
9588 " -r relocatable output\n"
9589 "Debugger options:\n"
9590 " -g generate runtime debug info\n"
9591 #ifdef CONFIG_TCC_BCHECK
9592 " -b compile with built-in memory and bounds checker (implies -g)\n"
9594 " -bt N show N callers in stack traces\n"
9598 #define TCC_OPTION_HAS_ARG 0x0001
9599 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
9601 typedef struct TCCOption
{
9628 TCC_OPTION_nostdinc
,
9629 TCC_OPTION_nostdlib
,
9630 TCC_OPTION_print_search_dirs
,
9631 TCC_OPTION_rdynamic
,
9636 static const TCCOption tcc_options
[] = {
9637 { "h", TCC_OPTION_HELP
, 0 },
9638 { "?", TCC_OPTION_HELP
, 0 },
9639 { "I", TCC_OPTION_I
, TCC_OPTION_HAS_ARG
},
9640 { "D", TCC_OPTION_D
, TCC_OPTION_HAS_ARG
},
9641 { "U", TCC_OPTION_U
, TCC_OPTION_HAS_ARG
},
9642 { "L", TCC_OPTION_L
, TCC_OPTION_HAS_ARG
},
9643 { "B", TCC_OPTION_B
, TCC_OPTION_HAS_ARG
},
9644 { "l", TCC_OPTION_l
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
9645 { "bench", TCC_OPTION_bench
, 0 },
9646 { "bt", TCC_OPTION_bt
, TCC_OPTION_HAS_ARG
},
9647 #ifdef CONFIG_TCC_BCHECK
9648 { "b", TCC_OPTION_b
, 0 },
9650 { "g", TCC_OPTION_g
, 0 },
9651 { "c", TCC_OPTION_c
, 0 },
9652 { "static", TCC_OPTION_static
, 0 },
9653 { "shared", TCC_OPTION_shared
, 0 },
9654 { "o", TCC_OPTION_o
, TCC_OPTION_HAS_ARG
},
9655 { "run", TCC_OPTION_run
, 0 },
9656 { "rdynamic", TCC_OPTION_rdynamic
, 0 }, /* currently ignored */
9657 { "r", TCC_OPTION_r
, 0 },
9658 { "W", TCC_OPTION_W
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
9659 { "O", TCC_OPTION_O
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
9660 { "m", TCC_OPTION_m
, TCC_OPTION_HAS_ARG
},
9661 { "f", TCC_OPTION_f
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
9662 { "nostdinc", TCC_OPTION_nostdinc
, 0 },
9663 { "nostdlib", TCC_OPTION_nostdlib
, 0 },
9664 { "print-search-dirs", TCC_OPTION_print_search_dirs
, 0 },
9665 { "v", TCC_OPTION_v
, 0 },
9669 int main(int argc
, char **argv
)
9672 int optind
, output_type
, multiple_files
, i
, reloc_output
;
9675 int nb_files
, nb_libraries
, nb_objfiles
, dminus
, ret
;
9676 char objfilename
[1024];
9677 int64_t start_time
= 0;
9678 const TCCOption
*popt
;
9679 const char *optarg
, *p1
, *r1
, *outfile
;
9680 int print_search_dirs
;
9683 output_type
= TCC_OUTPUT_EXE
;
9693 print_search_dirs
= 0;
9695 if (optind
>= argc
) {
9696 if (nb_files
== 0 && !print_search_dirs
)
9703 /* add a new file */
9704 dynarray_add((void ***)&files
, &nb_files
, r
);
9705 if (!multiple_files
) {
9707 /* argv[0] will be this file */
9711 /* find option in table (match only the first chars */
9716 error("invalid option -- '%s'", r
);
9729 if (popt
->flags
& TCC_OPTION_HAS_ARG
) {
9730 if (*r1
!= '\0' || (popt
->flags
& TCC_OPTION_NOSEP
)) {
9734 error("argument to '%s' is missing", r
);
9735 optarg
= argv
[optind
++];
9743 switch(popt
->index
) {
9744 case TCC_OPTION_HELP
:
9749 if (tcc_add_include_path(s
, optarg
) < 0)
9750 error("too many include paths");
9755 sym
= (char *)optarg
;
9756 value
= strchr(sym
, '=');
9761 tcc_define_symbol(s
, sym
, value
);
9765 tcc_undefine_symbol(s
, optarg
);
9768 tcc_add_library_path(s
, optarg
);
9771 /* set tcc utilities path (mainly for tcc development) */
9772 tcc_lib_path
= optarg
;
9775 dynarray_add((void ***)&files
, &nb_files
, r
);
9778 case TCC_OPTION_bench
:
9782 num_callers
= atoi(optarg
);
9784 #ifdef CONFIG_TCC_BCHECK
9786 do_bounds_check
= 1;
9795 output_type
= TCC_OUTPUT_OBJ
;
9797 case TCC_OPTION_static
:
9800 case TCC_OPTION_shared
:
9801 output_type
= TCC_OUTPUT_DLL
;
9808 /* generate a .o merging several output files */
9810 output_type
= TCC_OUTPUT_OBJ
;
9812 case TCC_OPTION_nostdinc
:
9815 case TCC_OPTION_nostdlib
:
9818 case TCC_OPTION_print_search_dirs
:
9819 print_search_dirs
= 1;
9821 case TCC_OPTION_run
:
9823 output_type
= TCC_OUTPUT_MEMORY
;
9826 printf("tcc version %s\n", TCC_VERSION
);
9830 const char *p
= optarg
;
9833 if (p
[0] == 'n' && p
[1] == 'o' && p
[2] == '-') {
9837 if (tcc_set_warning(s
, p
, value
) < 0 && s
->warn_unsupported
)
9838 goto unsupported_option
;
9842 if (s
->warn_unsupported
) {
9844 warning("unsupported option '%s'", r
);
9850 if (print_search_dirs
) {
9851 /* enough for Linux kernel */
9852 printf("install: %s/\n", tcc_lib_path
);
9856 nb_objfiles
= nb_files
- nb_libraries
;
9858 /* if outfile provided without other options, we output an
9860 if (outfile
&& output_type
== TCC_OUTPUT_MEMORY
)
9861 output_type
= TCC_OUTPUT_EXE
;
9863 /* check -c consistency : only single file handled. XXX: checks file type */
9864 if (output_type
== TCC_OUTPUT_OBJ
&& !reloc_output
) {
9865 /* accepts only a single input file */
9866 if (nb_objfiles
!= 1)
9867 error("cannot specify multiple files with -c");
9868 if (nb_libraries
!= 0)
9869 error("cannot specify libraries with -c");
9872 /* compute default outfile name */
9873 if (output_type
!= TCC_OUTPUT_MEMORY
&& !outfile
) {
9874 if (output_type
== TCC_OUTPUT_OBJ
&& !reloc_output
) {
9877 pstrcpy(objfilename
, sizeof(objfilename
) - 1,
9878 tcc_basename(files
[0]));
9879 /* add .o extension */
9880 ext
= strrchr(objfilename
, '.');
9882 goto default_outfile
;
9883 strcpy(ext
+ 1, "o");
9886 pstrcpy(objfilename
, sizeof(objfilename
), "a.out");
9888 outfile
= objfilename
;
9892 start_time
= getclock_us();
9895 tcc_set_output_type(s
, output_type
);
9897 /* compile or add each files or library */
9898 for(i
= 0;i
< nb_files
; i
++) {
9899 const char *filename
;
9901 filename
= files
[i
];
9902 if (filename
[0] == '-') {
9903 if (tcc_add_library(s
, filename
+ 2) < 0)
9904 error("cannot find %s", filename
);
9906 if (tcc_add_file(s
, filename
) < 0) {
9913 /* free all files */
9918 total_time
= (double)(getclock_us() - start_time
) / 1000000.0;
9919 if (total_time
< 0.001)
9921 if (total_bytes
< 1)
9923 printf("%d idents, %d lines, %d bytes, %0.3f s, %d lines/s, %0.1f MB/s\n",
9924 tok_ident
- TOK_IDENT
, total_lines
, total_bytes
,
9925 total_time
, (int)(total_lines
/ total_time
),
9926 total_bytes
/ total_time
/ 1000000.0);
9929 if (s
->output_type
!= TCC_OUTPUT_MEMORY
) {
9930 tcc_output_file(s
, outfile
);
9933 ret
= tcc_run(s
, argc
- optind
, argv
+ optind
);
9936 /* XXX: cannot do it with bound checking because of the malloc hooks */
9937 if (!do_bounds_check
)
9942 printf("memory: %d bytes, max = %d bytes\n", mem_cur_size
, mem_max_size
);