2 * TCC - Tiny C Compiler
4 * Copyright (c) 2001-2004 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 /********************************************************/
24 /* global variables */
26 /* loc : local variable index
27 ind : output code index
29 anon_sym: anonymous symbol index
31 ST_DATA
int rsym
, anon_sym
, ind
, loc
;
33 ST_DATA Section
*text_section
, *data_section
, *bss_section
; /* predefined sections */
34 ST_DATA Section
*tdata_section
, *tbss_section
; /* thread-local storage sections */
35 ST_DATA Section
*cur_text_section
; /* current section where function code is generated */
37 ST_DATA Section
*last_text_section
; /* to handle .previous asm directive */
39 #ifdef CONFIG_TCC_BCHECK
40 /* bound check related sections */
41 ST_DATA Section
*bounds_section
; /* contains global data bound description */
42 ST_DATA Section
*lbounds_section
; /* contains local data bound description */
45 ST_DATA Section
*symtab_section
, *strtab_section
;
47 ST_DATA Section
*stab_section
, *stabstr_section
;
48 ST_DATA Sym
*sym_free_first
;
49 ST_DATA
void **sym_pools
;
50 ST_DATA
int nb_sym_pools
;
52 ST_DATA Sym
*global_stack
;
53 ST_DATA Sym
*local_stack
;
54 ST_DATA Sym
*scope_stack_bottom
;
55 ST_DATA Sym
*define_stack
;
56 ST_DATA Sym
*global_label_stack
;
57 ST_DATA Sym
*local_label_stack
;
59 ST_DATA
int vla_sp_loc_tmp
; /* vla_sp_loc is set to this when the value won't be needed later */
60 ST_DATA
int vla_sp_root_loc
; /* vla_sp_loc for SP before any VLAs were pushed */
61 ST_DATA
int *vla_sp_loc
; /* Pointer to variable holding location to store stack pointer on the stack when modifying stack pointer */
62 ST_DATA
int vla_flags
; /* VLA_* flags */
64 ST_DATA SValue __vstack
[1+VSTACK_SIZE
], *vtop
;
66 ST_DATA
int const_wanted
; /* true if constant wanted */
67 ST_DATA
int nocode_wanted
; /* true if no code generation wanted for an expression */
68 ST_DATA
int global_expr
; /* true if compound literals must be allocated globally (used during initializers parsing */
69 ST_DATA CType func_vt
; /* current function return type (used by return instruction) */
71 ST_DATA
int last_line_num
, last_ind
, func_ind
; /* debug last line number and pc */
72 ST_DATA
char *funcname
;
74 ST_DATA CType char_pointer_type
, func_old_type
, int_type
, size_type
;
76 /* ------------------------------------------------------------------------- */
77 static void gen_cast(CType
*type
);
78 static inline CType
*pointed_type(CType
*type
);
79 static int is_compatible_types(CType
*type1
, CType
*type2
);
80 static int parse_btype(CType
*type
, AttributeDef
*ad
);
81 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
);
82 static void parse_expr_type(CType
*type
);
83 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
, int first
, int size_only
);
84 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
, int case_reg
, int is_expr
);
85 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
, int has_init
, int v
, char *asm_label
, int scope
);
86 static int decl0(int l
, int is_for_loop_init
);
87 static void expr_eq(void);
88 static void unary_type(CType
*type
);
89 static void vla_runtime_type_size(CType
*type
, int *a
);
90 static void vla_sp_save(void);
91 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
);
92 static void expr_type(CType
*type
);
94 ST_INLN
int is_float(int t
)
98 return bt
== VT_LDOUBLE
|| bt
== VT_DOUBLE
|| bt
== VT_FLOAT
|| bt
== VT_QFLOAT
;
101 /* we use our own 'finite' function to avoid potential problems with
102 non standard math libs */
103 /* XXX: endianness dependent */
104 ST_FUNC
int ieee_finite(double d
)
107 return ((unsigned)((p
[1] | 0x800fffff) + 1)) >> 31;
110 ST_FUNC
void test_lvalue(void)
112 if (!(vtop
->r
& VT_LVAL
))
116 /* ------------------------------------------------------------------------- */
117 /* symbol allocator */
118 static Sym
*__sym_malloc(void)
120 Sym
*sym_pool
, *sym
, *last_sym
;
123 sym_pool
= tcc_malloc(SYM_POOL_NB
* sizeof(Sym
));
124 dynarray_add(&sym_pools
, &nb_sym_pools
, sym_pool
);
126 last_sym
= sym_free_first
;
128 for(i
= 0; i
< SYM_POOL_NB
; i
++) {
129 sym
->next
= last_sym
;
133 sym_free_first
= last_sym
;
137 static inline Sym
*sym_malloc(void)
140 sym
= sym_free_first
;
142 sym
= __sym_malloc();
143 sym_free_first
= sym
->next
;
147 ST_INLN
void sym_free(Sym
*sym
)
149 sym
->next
= sym_free_first
;
150 tcc_free(sym
->asm_label
);
151 sym_free_first
= sym
;
154 /* push, without hashing */
155 ST_FUNC Sym
*sym_push2(Sym
**ps
, int v
, int t
, long c
)
158 if (ps
== &local_stack
) {
159 for (s
= *ps
; s
&& s
!= scope_stack_bottom
; s
= s
->prev
)
160 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
&& s
->v
== v
)
161 tcc_error("incompatible types for redefinition of '%s'",
162 get_tok_str(v
, NULL
));
181 /* find a symbol and return its associated structure. 's' is the top
182 of the symbol stack */
183 ST_FUNC Sym
*sym_find2(Sym
*s
, int v
)
193 /* structure lookup */
194 ST_INLN Sym
*struct_find(int v
)
197 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
199 return table_ident
[v
]->sym_struct
;
202 /* find an identifier */
203 ST_INLN Sym
*sym_find(int v
)
206 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
208 return table_ident
[v
]->sym_identifier
;
211 /* push a given symbol on the symbol stack */
212 ST_FUNC Sym
*sym_push(int v
, CType
*type
, int r
, int c
)
221 s
= sym_push2(ps
, v
, type
->t
, c
);
222 s
->type
.ref
= type
->ref
;
224 /* don't record fields or anonymous symbols */
226 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
227 /* record symbol in token array */
228 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
230 ps
= &ts
->sym_struct
;
232 ps
= &ts
->sym_identifier
;
239 /* push a global identifier */
240 ST_FUNC Sym
*global_identifier_push(int v
, int t
, int c
)
243 s
= sym_push2(&global_stack
, v
, t
, c
);
244 /* don't record anonymous symbol */
245 if (v
< SYM_FIRST_ANOM
) {
246 ps
= &table_ident
[v
- TOK_IDENT
]->sym_identifier
;
247 /* modify the top most local identifier, so that
248 sym_identifier will point to 's' when popped */
250 ps
= &(*ps
)->prev_tok
;
257 /* pop symbols until top reaches 'b' */
258 ST_FUNC
void sym_pop(Sym
**ptop
, Sym
*b
)
268 /* remove symbol in token array */
270 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
271 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
273 ps
= &ts
->sym_struct
;
275 ps
= &ts
->sym_identifier
;
284 static void weaken_symbol(Sym
*sym
)
286 sym
->type
.t
|= VT_WEAK
;
291 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
292 esym_type
= ELFW(ST_TYPE
)(esym
->st_info
);
293 esym
->st_info
= ELFW(ST_INFO
)(STB_WEAK
, esym_type
);
297 /* ------------------------------------------------------------------------- */
299 ST_FUNC
void swap(int *p
, int *q
)
307 static void vsetc(CType
*type
, int r
, CValue
*vc
)
311 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
312 tcc_error("memory full");
313 /* cannot let cpu flags if other instruction are generated. Also
314 avoid leaving VT_JMP anywhere except on the top of the stack
315 because it would complicate the code generator. */
316 if (vtop
>= vstack
) {
317 v
= vtop
->r
& VT_VALMASK
;
318 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
328 /* push constant of type "type" with useless value */
329 void vpush(CType
*type
)
332 vsetc(type
, VT_CONST
, &cval
);
335 /* push integer constant */
336 ST_FUNC
void vpushi(int v
)
340 vsetc(&int_type
, VT_CONST
, &cval
);
343 /* push a pointer sized constant */
344 static void vpushs(long long v
)
351 vsetc(&size_type
, VT_CONST
, &cval
);
354 /* push arbitrary 64bit constant */
355 void vpush64(int ty
, unsigned long long v
)
362 vsetc(&ctype
, VT_CONST
, &cval
);
365 /* push long long constant */
366 static inline void vpushll(long long v
)
368 vpush64(VT_LLONG
, v
);
371 /* Return a static symbol pointing to a section */
372 ST_FUNC Sym
*get_sym_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
378 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
379 sym
->type
.ref
= type
->ref
;
380 sym
->r
= VT_CONST
| VT_SYM
;
381 put_extern_sym(sym
, sec
, offset
, size
);
385 /* push a reference to a section offset by adding a dummy symbol */
386 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
391 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
392 vtop
->sym
= get_sym_ref(type
, sec
, offset
, size
);
395 /* define a new external reference to a symbol 'v' of type 'u' */
396 ST_FUNC Sym
*external_global_sym(int v
, CType
*type
, int r
)
402 /* push forward reference */
403 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
404 s
->type
.ref
= type
->ref
;
405 s
->r
= r
| VT_CONST
| VT_SYM
;
410 /* define a new external reference to a symbol 'v' with alternate asm
411 name 'asm_label' of type 'u'. 'asm_label' is equal to NULL if there
412 is no alternate name (most cases) */
413 static Sym
*external_sym(int v
, CType
*type
, int r
, char *asm_label
)
419 /* push forward reference */
420 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
421 s
->asm_label
= asm_label
;
422 s
->type
.t
|= VT_EXTERN
;
423 } else if (s
->type
.ref
== func_old_type
.ref
) {
424 s
->type
.ref
= type
->ref
;
425 s
->r
= r
| VT_CONST
| VT_SYM
;
426 s
->type
.t
|= VT_EXTERN
;
427 } else if (!is_compatible_types(&s
->type
, type
)) {
428 tcc_error("incompatible types for redefinition of '%s'",
429 get_tok_str(v
, NULL
));
434 /* push a reference to global symbol v */
435 ST_FUNC
void vpush_global_sym(CType
*type
, int v
)
440 sym
= external_global_sym(v
, type
, 0);
442 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
446 ST_FUNC
void vset(CType
*type
, int r
, int v
)
451 vsetc(type
, r
, &cval
);
454 static void vseti(int r
, int v
)
462 ST_FUNC
void vswap(void)
465 /* cannot let cpu flags if other instruction are generated. Also
466 avoid leaving VT_JMP anywhere except on the top of the stack
467 because it would complicate the code generator. */
468 if (vtop
>= vstack
) {
469 int v
= vtop
->r
& VT_VALMASK
;
470 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
477 /* XXX: +2% overall speed possible with optimized memswap
479 * memswap(&vtop[0], &vtop[1], sizeof *vtop);
483 ST_FUNC
void vpushv(SValue
*v
)
485 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
486 tcc_error("memory full");
491 static void vdup(void)
496 /* save r to the memory stack, and mark it as being free */
497 ST_FUNC
void save_reg(int r
)
499 int l
, saved
, size
, align
;
503 /* modify all stack values */
506 for(p
=vstack
;p
<=vtop
;p
++) {
507 if ((p
->r
& VT_VALMASK
) == r
||
508 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
509 /* must save value on stack if not already done */
511 /* NOTE: must reload 'r' because r might be equal to r2 */
512 r
= p
->r
& VT_VALMASK
;
513 /* store register in the stack */
515 if ((p
->r
& VT_LVAL
) ||
516 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
517 #ifdef TCC_TARGET_X86_64
518 type
= &char_pointer_type
;
522 size
= type_size(type
, &align
);
523 loc
= (loc
- size
) & -align
;
525 sv
.r
= VT_LOCAL
| VT_LVAL
;
528 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
529 /* x86 specific: need to pop fp register ST0 if saved */
531 o(0xd8dd); /* fstp %st(0) */
534 #ifndef TCC_TARGET_X86_64
535 /* special long long case */
536 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
544 /* mark that stack entry as being saved on the stack */
545 if (p
->r
& VT_LVAL
) {
546 /* also clear the bounded flag because the
547 relocation address of the function was stored in
549 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
551 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
559 #ifdef TCC_TARGET_ARM
560 /* find a register of class 'rc2' with at most one reference on stack.
561 * If none, call get_reg(rc) */
562 ST_FUNC
int get_reg_ex(int rc
, int rc2
)
567 for(r
=0;r
<NB_REGS
;r
++) {
568 if (reg_classes
[r
] & rc2
) {
571 for(p
= vstack
; p
<= vtop
; p
++) {
572 if ((p
->r
& VT_VALMASK
) == r
||
573 (p
->r2
& VT_VALMASK
) == r
)
584 /* find a free register of class 'rc'. If none, save one register */
585 ST_FUNC
int get_reg(int rc
)
590 /* find a free register */
591 for(r
=0;r
<NB_REGS
;r
++) {
592 if (reg_classes
[r
] & rc
) {
593 for(p
=vstack
;p
<=vtop
;p
++) {
594 if ((p
->r
& VT_VALMASK
) == r
||
595 (p
->r2
& VT_VALMASK
) == r
)
603 /* no register left : free the first one on the stack (VERY
604 IMPORTANT to start from the bottom to ensure that we don't
605 spill registers used in gen_opi()) */
606 for(p
=vstack
;p
<=vtop
;p
++) {
607 /* look at second register (if long long) */
608 r
= p
->r2
& VT_VALMASK
;
609 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
611 r
= p
->r
& VT_VALMASK
;
612 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
618 /* Should never comes here */
622 /* save registers up to (vtop - n) stack entry */
623 ST_FUNC
void save_regs(int n
)
628 for(p
= vstack
;p
<= p1
; p
++) {
629 r
= p
->r
& VT_VALMASK
;
636 /* move register 's' (of type 't') to 'r', and flush previous value of r to memory
638 static void move_reg(int r
, int s
, int t
)
652 /* get address of vtop (vtop MUST BE an lvalue) */
653 static void gaddrof(void)
655 if (vtop
->r
& VT_REF
)
658 /* tricky: if saved lvalue, then we can go back to lvalue */
659 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
660 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
665 #ifdef CONFIG_TCC_BCHECK
666 /* generate lvalue bound code */
667 static void gbound(void)
672 vtop
->r
&= ~VT_MUSTBOUND
;
673 /* if lvalue, then use checking code before dereferencing */
674 if (vtop
->r
& VT_LVAL
) {
675 /* if not VT_BOUNDED value, then make one */
676 if (!(vtop
->r
& VT_BOUNDED
)) {
677 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
678 /* must save type because we must set it to int to get pointer */
680 vtop
->type
.t
= VT_INT
;
683 gen_bounded_ptr_add();
684 vtop
->r
|= lval_type
;
687 /* then check for dereferencing */
688 gen_bounded_ptr_deref();
693 /* store vtop a register belonging to class 'rc'. lvalues are
694 converted to values. Cannot be used if cannot be converted to
695 register value (such as structures). */
696 ST_FUNC
int gv(int rc
)
698 int r
, bit_pos
, bit_size
, size
, align
, i
;
701 /* NOTE: get_reg can modify vstack[] */
702 if (vtop
->type
.t
& VT_BITFIELD
) {
705 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
706 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
707 /* remove bit field info to avoid loops */
708 vtop
->type
.t
&= ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
709 /* cast to int to propagate signedness in following ops */
710 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
715 if((vtop
->type
.t
& VT_UNSIGNED
) ||
716 (vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
717 type
.t
|= VT_UNSIGNED
;
719 /* generate shifts */
720 vpushi(bits
- (bit_pos
+ bit_size
));
722 vpushi(bits
- bit_size
);
723 /* NOTE: transformed to SHR if unsigned */
727 if (is_float(vtop
->type
.t
) &&
728 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
731 unsigned long offset
;
732 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
736 /* XXX: unify with initializers handling ? */
737 /* CPUs usually cannot use float constants, so we store them
738 generically in data segment */
739 size
= type_size(&vtop
->type
, &align
);
740 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
741 data_section
->data_offset
= offset
;
742 /* XXX: not portable yet */
743 #if defined(__i386__) || defined(__x86_64__)
744 /* Zero pad x87 tenbyte long doubles */
745 if (size
== LDOUBLE_SIZE
) {
746 vtop
->c
.tab
[2] &= 0xffff;
747 #if LDOUBLE_SIZE == 16
752 ptr
= section_ptr_add(data_section
, size
);
754 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
758 ptr
[i
] = vtop
->c
.tab
[size
-1-i
];
762 ptr
[i
] = vtop
->c
.tab
[i
];
763 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
764 vtop
->r
|= VT_LVAL
| VT_SYM
;
768 #ifdef CONFIG_TCC_BCHECK
769 if (vtop
->r
& VT_MUSTBOUND
)
773 r
= vtop
->r
& VT_VALMASK
;
774 rc2
= (rc
& RC_FLOAT
) ? RC_FLOAT
: RC_INT
;
777 #ifdef TCC_TARGET_X86_64
778 else if (rc
== RC_FRET
)
782 /* need to reload if:
784 - lvalue (need to dereference pointer)
785 - already a register, but not in the right class */
787 || (vtop
->r
& VT_LVAL
)
788 || !(reg_classes
[r
] & rc
)
789 #ifdef TCC_TARGET_X86_64
790 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
791 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
&& !(reg_classes
[vtop
->r2
] & rc2
))
793 || ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
798 #ifdef TCC_TARGET_X86_64
799 if (((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
)) {
800 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
802 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
803 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
804 unsigned long long ll
;
806 int r2
, original_type
;
807 original_type
= vtop
->type
.t
;
808 /* two register type load : expand to two words
810 #ifndef TCC_TARGET_X86_64
811 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
814 vtop
->c
.ui
= ll
; /* first word */
816 vtop
->r
= r
; /* save register value */
817 vpushi(ll
>> 32); /* second word */
820 if (r
>= VT_CONST
|| /* XXX: test to VT_CONST incorrect ? */
821 (vtop
->r
& VT_LVAL
)) {
822 /* We do not want to modifier the long long
823 pointer here, so the safest (and less
824 efficient) is to save all the other registers
825 in the stack. XXX: totally inefficient. */
827 /* load from memory */
828 vtop
->type
.t
= load_type
;
831 vtop
[-1].r
= r
; /* save register value */
832 /* increment pointer to get second word */
833 vtop
->type
.t
= addr_type
;
838 vtop
->type
.t
= load_type
;
843 vtop
[-1].r
= r
; /* save register value */
844 vtop
->r
= vtop
[-1].r2
;
846 /* Allocate second register. Here we rely on the fact that
847 get_reg() tries first to free r2 of an SValue. */
851 /* write second register */
853 vtop
->type
.t
= original_type
;
854 } else if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
856 /* lvalue of scalar type : need to use lvalue type
857 because of possible cast */
860 /* compute memory access type */
861 if (vtop
->r
& VT_REF
)
862 #ifdef TCC_TARGET_X86_64
867 else if (vtop
->r
& VT_LVAL_BYTE
)
869 else if (vtop
->r
& VT_LVAL_SHORT
)
871 if (vtop
->r
& VT_LVAL_UNSIGNED
)
875 /* restore wanted type */
878 /* one register type load */
883 #ifdef TCC_TARGET_C67
884 /* uses register pairs for doubles */
885 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
892 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
893 ST_FUNC
void gv2(int rc1
, int rc2
)
897 /* generate more generic register first. But VT_JMP or VT_CMP
898 values must be generated first in all cases to avoid possible
900 v
= vtop
[0].r
& VT_VALMASK
;
901 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
906 /* test if reload is needed for first register */
907 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
917 /* test if reload is needed for first register */
918 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
924 /* wrapper around RC_FRET to return a register by type */
925 static int rc_fret(int t
)
927 #ifdef TCC_TARGET_X86_64
928 if (t
== VT_LDOUBLE
) {
935 /* wrapper around REG_FRET to return a register by type */
936 static int reg_fret(int t
)
938 #ifdef TCC_TARGET_X86_64
939 if (t
== VT_LDOUBLE
) {
946 /* expand long long on stack in two int registers */
947 static void lexpand(void)
951 u
= vtop
->type
.t
& VT_UNSIGNED
;
954 vtop
[0].r
= vtop
[-1].r2
;
955 vtop
[0].r2
= VT_CONST
;
956 vtop
[-1].r2
= VT_CONST
;
957 vtop
[0].type
.t
= VT_INT
| u
;
958 vtop
[-1].type
.t
= VT_INT
| u
;
961 #ifdef TCC_TARGET_ARM
962 /* expand long long on stack */
963 ST_FUNC
void lexpand_nr(void)
967 u
= vtop
->type
.t
& VT_UNSIGNED
;
970 vtop
->type
.t
= VT_INT
| u
;
971 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
973 vtop
[-1].c
.ui
= vtop
->c
.ull
;
974 vtop
->c
.ui
= vtop
->c
.ull
>> 32;
976 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
978 vtop
->r
= vtop
[-1].r
;
979 } else if (v
> VT_CONST
) {
983 vtop
->r
= vtop
[-1].r2
;
984 vtop
[-1].r2
= VT_CONST
;
985 vtop
[-1].type
.t
= VT_INT
| u
;
989 /* build a long long from two ints */
990 static void lbuild(int t
)
993 vtop
[-1].r2
= vtop
[0].r
;
998 /* rotate n first stack elements to the bottom
999 I1 ... In -> I2 ... In I1 [top is right]
1001 ST_FUNC
void vrotb(int n
)
1007 for(i
=-n
+1;i
!=0;i
++)
1008 vtop
[i
] = vtop
[i
+1];
1012 /* rotate the n elements before entry e towards the top
1013 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
1015 ST_FUNC
void vrote(SValue
*e
, int n
)
1021 for(i
= 0;i
< n
- 1; i
++)
1026 /* rotate n first stack elements to the top
1027 I1 ... In -> In I1 ... I(n-1) [top is right]
1029 ST_FUNC
void vrott(int n
)
1034 /* pop stack value */
1035 ST_FUNC
void vpop(void)
1038 v
= vtop
->r
& VT_VALMASK
;
1039 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1040 /* for x86, we need to pop the FP stack */
1041 if (v
== TREG_ST0
&& !nocode_wanted
) {
1042 o(0xd8dd); /* fstp %st(0) */
1045 if (v
== VT_JMP
|| v
== VT_JMPI
) {
1046 /* need to put correct jump if && or || without test */
1052 /* convert stack entry to register and duplicate its value in another
1054 static void gv_dup(void)
1060 if ((t
& VT_BTYPE
) == VT_LLONG
) {
1067 /* stack: H L L1 H1 */
1075 /* duplicate value */
1080 #ifdef TCC_TARGET_X86_64
1081 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1091 load(r1
, &sv
); /* move r to r1 */
1093 /* duplicates value */
1099 #ifndef TCC_TARGET_X86_64
1100 /* generate CPU independent (unsigned) long long operations */
1101 static void gen_opl(int op
)
1103 int t
, a
, b
, op1
, c
, i
;
1105 unsigned short reg_iret
= REG_IRET
;
1106 unsigned short reg_lret
= REG_LRET
;
1112 func
= TOK___divdi3
;
1115 func
= TOK___udivdi3
;
1118 func
= TOK___moddi3
;
1121 func
= TOK___umoddi3
;
1128 /* call generic long long function */
1129 vpush_global_sym(&func_old_type
, func
);
1134 vtop
->r2
= reg_lret
;
1147 /* stack: L1 H1 L2 H2 */
1152 vtop
[-2] = vtop
[-3];
1155 /* stack: H1 H2 L1 L2 */
1161 /* stack: H1 H2 L1 L2 ML MH */
1164 /* stack: ML MH H1 H2 L1 L2 */
1168 /* stack: ML MH H1 L2 H2 L1 */
1173 /* stack: ML MH M1 M2 */
1176 } else if (op
== '+' || op
== '-') {
1177 /* XXX: add non carry method too (for MIPS or alpha) */
1183 /* stack: H1 H2 (L1 op L2) */
1186 gen_op(op1
+ 1); /* TOK_xxxC2 */
1189 /* stack: H1 H2 (L1 op L2) */
1192 /* stack: (L1 op L2) H1 H2 */
1194 /* stack: (L1 op L2) (H1 op H2) */
1202 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1203 t
= vtop
[-1].type
.t
;
1207 /* stack: L H shift */
1209 /* constant: simpler */
1210 /* NOTE: all comments are for SHL. the other cases are
1211 done by swaping words */
1222 if (op
!= TOK_SAR
) {
1255 /* XXX: should provide a faster fallback on x86 ? */
1258 func
= TOK___ashrdi3
;
1261 func
= TOK___lshrdi3
;
1264 func
= TOK___ashldi3
;
1270 /* compare operations */
1276 /* stack: L1 H1 L2 H2 */
1278 vtop
[-1] = vtop
[-2];
1280 /* stack: L1 L2 H1 H2 */
1283 /* when values are equal, we need to compare low words. since
1284 the jump is inverted, we invert the test too. */
1287 else if (op1
== TOK_GT
)
1289 else if (op1
== TOK_ULT
)
1291 else if (op1
== TOK_UGT
)
1296 if (op1
!= TOK_NE
) {
1300 /* generate non equal test */
1301 /* XXX: NOT PORTABLE yet */
1305 #if defined(TCC_TARGET_I386)
1306 b
= psym(0x850f, 0);
1307 #elif defined(TCC_TARGET_ARM)
1309 o(0x1A000000 | encbranch(ind
, 0, 1));
1310 #elif defined(TCC_TARGET_C67)
1311 tcc_error("not implemented");
1313 #error not supported
1317 /* compare low. Always unsigned */
1321 else if (op1
== TOK_LE
)
1323 else if (op1
== TOK_GT
)
1325 else if (op1
== TOK_GE
)
1336 /* handle integer constant optimizations and various machine
1338 static void gen_opic(int op
)
1340 int c1
, c2
, t1
, t2
, n
;
1343 typedef unsigned long long U
;
1347 t1
= v1
->type
.t
& VT_BTYPE
;
1348 t2
= v2
->type
.t
& VT_BTYPE
;
1352 else if (v1
->type
.t
& VT_UNSIGNED
)
1359 else if (v2
->type
.t
& VT_UNSIGNED
)
1364 /* currently, we cannot do computations with forward symbols */
1365 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1366 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1369 case '+': l1
+= l2
; break;
1370 case '-': l1
-= l2
; break;
1371 case '&': l1
&= l2
; break;
1372 case '^': l1
^= l2
; break;
1373 case '|': l1
|= l2
; break;
1374 case '*': l1
*= l2
; break;
1381 /* if division by zero, generate explicit division */
1384 tcc_error("division by zero in constant");
1388 default: l1
/= l2
; break;
1389 case '%': l1
%= l2
; break;
1390 case TOK_UDIV
: l1
= (U
)l1
/ l2
; break;
1391 case TOK_UMOD
: l1
= (U
)l1
% l2
; break;
1394 case TOK_SHL
: l1
<<= l2
; break;
1395 case TOK_SHR
: l1
= (U
)l1
>> l2
; break;
1396 case TOK_SAR
: l1
>>= l2
; break;
1398 case TOK_ULT
: l1
= (U
)l1
< (U
)l2
; break;
1399 case TOK_UGE
: l1
= (U
)l1
>= (U
)l2
; break;
1400 case TOK_EQ
: l1
= l1
== l2
; break;
1401 case TOK_NE
: l1
= l1
!= l2
; break;
1402 case TOK_ULE
: l1
= (U
)l1
<= (U
)l2
; break;
1403 case TOK_UGT
: l1
= (U
)l1
> (U
)l2
; break;
1404 case TOK_LT
: l1
= l1
< l2
; break;
1405 case TOK_GE
: l1
= l1
>= l2
; break;
1406 case TOK_LE
: l1
= l1
<= l2
; break;
1407 case TOK_GT
: l1
= l1
> l2
; break;
1409 case TOK_LAND
: l1
= l1
&& l2
; break;
1410 case TOK_LOR
: l1
= l1
|| l2
; break;
1417 /* if commutative ops, put c2 as constant */
1418 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1419 op
== '|' || op
== '*')) {
1421 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1422 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1424 /* Filter out NOP operations like x*1, x-0, x&-1... */
1425 if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1428 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1429 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1435 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1436 /* try to use shifts instead of muls or divs */
1437 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1446 else if (op
== TOK_PDIV
)
1452 } else if (c2
&& (op
== '+' || op
== '-') &&
1453 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
1454 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1455 /* symbol + constant case */
1462 if (!nocode_wanted
) {
1463 /* call low level op generator */
1464 if (t1
== VT_LLONG
|| t2
== VT_LLONG
)
1475 /* generate a floating point operation with constant propagation */
1476 static void gen_opif(int op
)
1484 /* currently, we cannot do computations with forward symbols */
1485 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1486 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1488 if (v1
->type
.t
== VT_FLOAT
) {
1491 } else if (v1
->type
.t
== VT_DOUBLE
) {
1499 /* NOTE: we only do constant propagation if finite number (not
1500 NaN or infinity) (ANSI spec) */
1501 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1505 case '+': f1
+= f2
; break;
1506 case '-': f1
-= f2
; break;
1507 case '*': f1
*= f2
; break;
1511 tcc_error("division by zero in constant");
1516 /* XXX: also handles tests ? */
1520 /* XXX: overflow test ? */
1521 if (v1
->type
.t
== VT_FLOAT
) {
1523 } else if (v1
->type
.t
== VT_DOUBLE
) {
1531 if (!nocode_wanted
) {
1539 static int pointed_size(CType
*type
)
1542 return type_size(pointed_type(type
), &align
);
1545 static void vla_runtime_pointed_size(CType
*type
)
1548 vla_runtime_type_size(pointed_type(type
), &align
);
1551 static inline int is_null_pointer(SValue
*p
)
1553 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1555 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& p
->c
.i
== 0) ||
1556 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.ll
== 0) ||
1557 ((p
->type
.t
& VT_BTYPE
) == VT_PTR
&& p
->c
.ptr
== 0);
1560 static inline int is_integer_btype(int bt
)
1562 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
1563 bt
== VT_INT
|| bt
== VT_LLONG
);
1566 /* check types for comparison or substraction of pointers */
1567 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
1569 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
1572 /* null pointers are accepted for all comparisons as gcc */
1573 if (is_null_pointer(p1
) || is_null_pointer(p2
))
1577 bt1
= type1
->t
& VT_BTYPE
;
1578 bt2
= type2
->t
& VT_BTYPE
;
1579 /* accept comparison between pointer and integer with a warning */
1580 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
1581 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
1582 tcc_warning("comparison between pointer and integer");
1586 /* both must be pointers or implicit function pointers */
1587 if (bt1
== VT_PTR
) {
1588 type1
= pointed_type(type1
);
1589 } else if (bt1
!= VT_FUNC
)
1590 goto invalid_operands
;
1592 if (bt2
== VT_PTR
) {
1593 type2
= pointed_type(type2
);
1594 } else if (bt2
!= VT_FUNC
) {
1596 tcc_error("invalid operands to binary %s", get_tok_str(op
, NULL
));
1598 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1599 (type2
->t
& VT_BTYPE
) == VT_VOID
)
1603 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1604 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1605 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1606 /* gcc-like error if '-' is used */
1608 goto invalid_operands
;
1610 tcc_warning("comparison of distinct pointer types lacks a cast");
1614 /* generic gen_op: handles types problems */
1615 ST_FUNC
void gen_op(int op
)
1617 int u
, t1
, t2
, bt1
, bt2
, t
;
1620 t1
= vtop
[-1].type
.t
;
1621 t2
= vtop
[0].type
.t
;
1622 bt1
= t1
& VT_BTYPE
;
1623 bt2
= t2
& VT_BTYPE
;
1625 if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
1626 /* at least one operand is a pointer */
1627 /* relationnal op: must be both pointers */
1628 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
1629 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1630 /* pointers are handled are unsigned */
1631 #ifdef TCC_TARGET_X86_64
1632 t
= VT_LLONG
| VT_UNSIGNED
;
1634 t
= VT_INT
| VT_UNSIGNED
;
1638 /* if both pointers, then it must be the '-' op */
1639 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
1641 tcc_error("cannot use pointers here");
1642 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1643 /* XXX: check that types are compatible */
1644 if (vtop
[-1].type
.t
& VT_VLA
) {
1645 vla_runtime_pointed_size(&vtop
[-1].type
);
1647 vpushi(pointed_size(&vtop
[-1].type
));
1651 /* set to integer type */
1652 #ifdef TCC_TARGET_X86_64
1653 vtop
->type
.t
= VT_LLONG
;
1655 vtop
->type
.t
= VT_INT
;
1660 /* exactly one pointer : must be '+' or '-'. */
1661 if (op
!= '-' && op
!= '+')
1662 tcc_error("cannot use pointers here");
1663 /* Put pointer as first operand */
1664 if (bt2
== VT_PTR
) {
1668 type1
= vtop
[-1].type
;
1669 type1
.t
&= ~VT_ARRAY
;
1670 if (vtop
[-1].type
.t
& VT_VLA
)
1671 vla_runtime_pointed_size(&vtop
[-1].type
);
1673 u
= pointed_size(&vtop
[-1].type
);
1675 tcc_error("unknown array element size");
1676 #ifdef TCC_TARGET_X86_64
1679 /* XXX: cast to int ? (long long case) */
1684 #ifdef CONFIG_TCC_BCHECK
1685 /* if evaluating constant expression, no code should be
1686 generated, so no bound check */
1687 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
1688 /* if bounded pointers, we generate a special code to
1695 gen_bounded_ptr_add();
1701 /* put again type if gen_opic() swaped operands */
1704 } else if (is_float(bt1
) || is_float(bt2
)) {
1705 /* compute bigger type and do implicit casts */
1706 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
1708 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
1713 /* floats can only be used for a few operations */
1714 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
1715 (op
< TOK_ULT
|| op
> TOK_GT
))
1716 tcc_error("invalid operands for binary operation");
1718 } else if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
) {
1719 t
= bt1
== VT_LLONG
? VT_LLONG
: VT_INT
;
1720 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (t
| VT_UNSIGNED
))
1723 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
1724 /* cast to biggest op */
1726 /* convert to unsigned if it does not fit in a long long */
1727 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
1728 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
1731 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
1732 tcc_error("comparison of struct");
1734 /* integer operations */
1736 /* convert to unsigned if it does not fit in an integer */
1737 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
1738 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
1741 /* XXX: currently, some unsigned operations are explicit, so
1742 we modify them here */
1743 if (t
& VT_UNSIGNED
) {
1750 else if (op
== TOK_LT
)
1752 else if (op
== TOK_GT
)
1754 else if (op
== TOK_LE
)
1756 else if (op
== TOK_GE
)
1763 /* special case for shifts and long long: we keep the shift as
1765 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
1772 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
1773 /* relationnal op: the result is an int */
1774 vtop
->type
.t
= VT_INT
;
1781 #ifndef TCC_TARGET_ARM
1782 /* generic itof for unsigned long long case */
1783 static void gen_cvt_itof1(int t
)
1785 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
1786 (VT_LLONG
| VT_UNSIGNED
)) {
1789 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
1790 #if LDOUBLE_SIZE != 8
1791 else if (t
== VT_LDOUBLE
)
1792 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
1795 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
1799 vtop
->r
= reg_fret(t
);
1806 /* generic ftoi for unsigned long long case */
1807 static void gen_cvt_ftoi1(int t
)
1811 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
1812 /* not handled natively */
1813 st
= vtop
->type
.t
& VT_BTYPE
;
1815 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
1816 #if LDOUBLE_SIZE != 8
1817 else if (st
== VT_LDOUBLE
)
1818 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
1821 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
1826 vtop
->r2
= REG_LRET
;
1832 /* force char or short cast */
1833 static void force_charshort_cast(int t
)
1837 /* XXX: add optimization if lvalue : just change type and offset */
1842 if (t
& VT_UNSIGNED
) {
1843 vpushi((1 << bits
) - 1);
1849 /* result must be signed or the SAR is converted to an SHL
1850 This was not the case when "t" was a signed short
1851 and the last value on the stack was an unsigned int */
1852 vtop
->type
.t
&= ~VT_UNSIGNED
;
1858 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1859 static void gen_cast(CType
*type
)
1861 int sbt
, dbt
, sf
, df
, c
, p
;
1863 /* special delayed cast for char/short */
1864 /* XXX: in some cases (multiple cascaded casts), it may still
1866 if (vtop
->r
& VT_MUSTCAST
) {
1867 vtop
->r
&= ~VT_MUSTCAST
;
1868 force_charshort_cast(vtop
->type
.t
);
1871 /* bitfields first get cast to ints */
1872 if (vtop
->type
.t
& VT_BITFIELD
) {
1876 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
1877 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
1882 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1883 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
1885 /* constant case: we can do it now */
1886 /* XXX: in ISOC, cannot do it if error in convert */
1887 if (sbt
== VT_FLOAT
)
1888 vtop
->c
.ld
= vtop
->c
.f
;
1889 else if (sbt
== VT_DOUBLE
)
1890 vtop
->c
.ld
= vtop
->c
.d
;
1893 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
1894 if (sbt
& VT_UNSIGNED
)
1895 vtop
->c
.ld
= vtop
->c
.ull
;
1897 vtop
->c
.ld
= vtop
->c
.ll
;
1899 if (sbt
& VT_UNSIGNED
)
1900 vtop
->c
.ld
= vtop
->c
.ui
;
1902 vtop
->c
.ld
= vtop
->c
.i
;
1905 if (dbt
== VT_FLOAT
)
1906 vtop
->c
.f
= (float)vtop
->c
.ld
;
1907 else if (dbt
== VT_DOUBLE
)
1908 vtop
->c
.d
= (double)vtop
->c
.ld
;
1909 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
1910 vtop
->c
.ull
= (unsigned long long)vtop
->c
.ld
;
1911 } else if (sf
&& dbt
== VT_BOOL
) {
1912 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
1915 vtop
->c
.ll
= (long long)vtop
->c
.ld
;
1916 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
1917 vtop
->c
.ll
= vtop
->c
.ull
;
1918 else if (sbt
& VT_UNSIGNED
)
1919 vtop
->c
.ll
= vtop
->c
.ui
;
1920 #ifdef TCC_TARGET_X86_64
1921 else if (sbt
== VT_PTR
)
1924 else if (sbt
!= VT_LLONG
)
1925 vtop
->c
.ll
= vtop
->c
.i
;
1927 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
1928 vtop
->c
.ull
= vtop
->c
.ll
;
1929 else if (dbt
== VT_BOOL
)
1930 vtop
->c
.i
= (vtop
->c
.ll
!= 0);
1931 else if (dbt
!= VT_LLONG
) {
1933 if ((dbt
& VT_BTYPE
) == VT_BYTE
)
1935 else if ((dbt
& VT_BTYPE
) == VT_SHORT
)
1938 if(dbt
& VT_UNSIGNED
)
1939 vtop
->c
.ui
= ((unsigned int)vtop
->c
.ll
<< s
) >> s
;
1941 vtop
->c
.i
= ((int)vtop
->c
.ll
<< s
) >> s
;
1944 } else if (p
&& dbt
== VT_BOOL
) {
1947 } else if (!nocode_wanted
) {
1948 /* non constant case: generate code */
1950 /* convert from fp to fp */
1953 /* convert int to fp */
1956 /* convert fp to int */
1957 if (dbt
== VT_BOOL
) {
1961 /* we handle char/short/etc... with generic code */
1962 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
1963 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
1967 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
1968 /* additional cast for char/short... */
1973 #ifndef TCC_TARGET_X86_64
1974 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
1975 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
1976 /* scalar to long long */
1977 /* machine independent conversion */
1979 /* generate high word */
1980 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
1984 if (sbt
== VT_PTR
) {
1985 /* cast from pointer to int before we apply
1986 shift operation, which pointers don't support*/
1987 gen_cast(&int_type
);
1993 /* patch second register */
1994 vtop
[-1].r2
= vtop
->r
;
1998 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
1999 (dbt
& VT_BTYPE
) == VT_PTR
||
2000 (dbt
& VT_BTYPE
) == VT_FUNC
) {
2001 if ((sbt
& VT_BTYPE
) != VT_LLONG
&&
2002 (sbt
& VT_BTYPE
) != VT_PTR
&&
2003 (sbt
& VT_BTYPE
) != VT_FUNC
) {
2004 /* need to convert from 32bit to 64bit */
2006 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
2007 /* x86_64 specific: movslq */
2009 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
2013 } else if (dbt
== VT_BOOL
) {
2014 /* scalar to bool */
2017 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
2018 (dbt
& VT_BTYPE
) == VT_SHORT
) {
2019 if (sbt
== VT_PTR
) {
2020 vtop
->type
.t
= VT_INT
;
2021 tcc_warning("nonportable conversion from pointer to char/short");
2023 force_charshort_cast(dbt
);
2024 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
2026 if (sbt
== VT_LLONG
) {
2027 /* from long long: just take low order word */
2031 /* if lvalue and single word type, nothing to do because
2032 the lvalue already contains the real type size (see
2033 VT_LVAL_xxx constants) */
2036 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
2037 /* if we are casting between pointer types,
2038 we must update the VT_LVAL_xxx size */
2039 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
2040 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
2045 /* return type size as known at compile time. Put alignment at 'a' */
2046 ST_FUNC
int type_size(CType
*type
, int *a
)
2051 bt
= type
->t
& VT_BTYPE
;
2052 if (bt
== VT_STRUCT
) {
2057 } else if (bt
== VT_PTR
) {
2058 if (type
->t
& VT_ARRAY
) {
2062 ts
= type_size(&s
->type
, a
);
2064 if (ts
< 0 && s
->c
< 0)
2072 } else if (bt
== VT_LDOUBLE
) {
2074 return LDOUBLE_SIZE
;
2075 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
2076 #ifdef TCC_TARGET_I386
2077 #ifdef TCC_TARGET_PE
2082 #elif defined(TCC_TARGET_ARM)
2092 } else if (bt
== VT_INT
|| bt
== VT_ENUM
|| bt
== VT_FLOAT
) {
2095 } else if (bt
== VT_SHORT
) {
2098 } else if (bt
== VT_QLONG
|| bt
== VT_QFLOAT
) {
2102 /* char, void, function, _Bool */
2108 /* push type size as known at runtime time on top of value stack. Put
2110 ST_FUNC
void vla_runtime_type_size(CType
*type
, int *a
)
2112 if (type
->t
& VT_VLA
) {
2113 vset(&int_type
, VT_LOCAL
|VT_LVAL
, type
->ref
->c
);
2115 vpushi(type_size(type
, a
));
2119 static void vla_sp_save(void) {
2120 if (!(vla_flags
& VLA_SP_LOC_SET
)) {
2121 *vla_sp_loc
= (loc
-= PTR_SIZE
);
2122 vla_flags
|= VLA_SP_LOC_SET
;
2124 if (!(vla_flags
& VLA_SP_SAVED
)) {
2125 gen_vla_sp_save(*vla_sp_loc
);
2126 vla_flags
|= VLA_SP_SAVED
;
2130 /* return the pointed type of t */
2131 static inline CType
*pointed_type(CType
*type
)
2133 return &type
->ref
->type
;
2136 /* modify type so that its it is a pointer to type. */
2137 ST_FUNC
void mk_pointer(CType
*type
)
2140 s
= sym_push(SYM_FIELD
, type
, 0, -1);
2141 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
2145 /* compare function types. OLD functions match any new functions */
2146 static int is_compatible_func(CType
*type1
, CType
*type2
)
2152 if (!is_compatible_types(&s1
->type
, &s2
->type
))
2154 /* check func_call */
2155 if (FUNC_CALL(s1
->r
) != FUNC_CALL(s2
->r
))
2157 /* XXX: not complete */
2158 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
2162 while (s1
!= NULL
) {
2165 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
2175 /* return true if type1 and type2 are the same. If unqualified is
2176 true, qualifiers on the types are ignored.
2178 - enums are not checked as gcc __builtin_types_compatible_p ()
2180 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2184 t1
= type1
->t
& VT_TYPE
;
2185 t2
= type2
->t
& VT_TYPE
;
2187 /* strip qualifiers before comparing */
2188 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2189 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2191 /* XXX: bitfields ? */
2194 /* test more complicated cases */
2195 bt1
= t1
& VT_BTYPE
;
2196 if (bt1
== VT_PTR
) {
2197 type1
= pointed_type(type1
);
2198 type2
= pointed_type(type2
);
2199 return is_compatible_types(type1
, type2
);
2200 } else if (bt1
== VT_STRUCT
) {
2201 return (type1
->ref
== type2
->ref
);
2202 } else if (bt1
== VT_FUNC
) {
2203 return is_compatible_func(type1
, type2
);
2209 /* return true if type1 and type2 are exactly the same (including
2212 static int is_compatible_types(CType
*type1
, CType
*type2
)
2214 return compare_types(type1
,type2
,0);
2217 /* return true if type1 and type2 are the same (ignoring qualifiers).
2219 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
2221 return compare_types(type1
,type2
,1);
2224 /* print a type. If 'varstr' is not NULL, then the variable is also
2225 printed in the type */
2227 /* XXX: add array and function pointers */
2228 static void type_to_str(char *buf
, int buf_size
,
2229 CType
*type
, const char *varstr
)
2236 t
= type
->t
& VT_TYPE
;
2239 if (t
& VT_CONSTANT
)
2240 pstrcat(buf
, buf_size
, "const ");
2241 if (t
& VT_VOLATILE
)
2242 pstrcat(buf
, buf_size
, "volatile ");
2243 if (t
& VT_UNSIGNED
)
2244 pstrcat(buf
, buf_size
, "unsigned ");
2274 tstr
= "long double";
2276 pstrcat(buf
, buf_size
, tstr
);
2280 if (bt
== VT_STRUCT
)
2284 pstrcat(buf
, buf_size
, tstr
);
2285 v
= type
->ref
->v
& ~SYM_STRUCT
;
2286 if (v
>= SYM_FIRST_ANOM
)
2287 pstrcat(buf
, buf_size
, "<anonymous>");
2289 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
2293 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
2294 pstrcat(buf
, buf_size
, "(");
2296 while (sa
!= NULL
) {
2297 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
2298 pstrcat(buf
, buf_size
, buf1
);
2301 pstrcat(buf
, buf_size
, ", ");
2303 pstrcat(buf
, buf_size
, ")");
2307 pstrcpy(buf1
, sizeof(buf1
), "*");
2309 pstrcat(buf1
, sizeof(buf1
), varstr
);
2310 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2314 pstrcat(buf
, buf_size
, " ");
2315 pstrcat(buf
, buf_size
, varstr
);
2320 /* verify type compatibility to store vtop in 'dt' type, and generate
2322 static void gen_assign_cast(CType
*dt
)
2324 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
2325 char buf1
[256], buf2
[256];
2328 st
= &vtop
->type
; /* source type */
2329 dbt
= dt
->t
& VT_BTYPE
;
2330 sbt
= st
->t
& VT_BTYPE
;
2332 tcc_error("Cannot assign void value");
2333 if (dt
->t
& VT_CONSTANT
)
2334 tcc_warning("assignment of read-only location");
2337 /* special cases for pointers */
2338 /* '0' can also be a pointer */
2339 if (is_null_pointer(vtop
))
2341 /* accept implicit pointer to integer cast with warning */
2342 if (is_integer_btype(sbt
)) {
2343 tcc_warning("assignment makes pointer from integer without a cast");
2346 type1
= pointed_type(dt
);
2347 /* a function is implicitely a function pointer */
2348 if (sbt
== VT_FUNC
) {
2349 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
2350 !is_compatible_types(pointed_type(dt
), st
))
2351 tcc_warning("assignment from incompatible pointer type");
2356 type2
= pointed_type(st
);
2357 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2358 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
2359 /* void * can match anything */
2361 /* exact type match, except for unsigned */
2364 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2365 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2366 if (!is_compatible_types(&tmp_type1
, &tmp_type2
))
2367 tcc_warning("assignment from incompatible pointer type");
2369 /* check const and volatile */
2370 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
2371 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
2372 tcc_warning("assignment discards qualifiers from pointer target type");
2378 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
2379 tcc_warning("assignment makes integer from pointer without a cast");
2381 /* XXX: more tests */
2386 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2387 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2388 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2390 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
2391 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
2392 tcc_error("cannot cast '%s' to '%s'", buf1
, buf2
);
2400 /* store vtop in lvalue pushed on stack */
2401 ST_FUNC
void vstore(void)
2403 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
2405 ft
= vtop
[-1].type
.t
;
2406 sbt
= vtop
->type
.t
& VT_BTYPE
;
2407 dbt
= ft
& VT_BTYPE
;
2408 if ((((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2409 (sbt
== VT_INT
&& dbt
== VT_SHORT
))
2410 && !(vtop
->type
.t
& VT_BITFIELD
)) {
2411 /* optimize char/short casts */
2412 delayed_cast
= VT_MUSTCAST
;
2413 vtop
->type
.t
= ft
& (VT_TYPE
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
)));
2414 /* XXX: factorize */
2415 if (ft
& VT_CONSTANT
)
2416 tcc_warning("assignment of read-only location");
2419 if (!(ft
& VT_BITFIELD
))
2420 gen_assign_cast(&vtop
[-1].type
);
2423 if (sbt
== VT_STRUCT
) {
2424 /* if structure, only generate pointer */
2425 /* structure assignment : generate memcpy */
2426 /* XXX: optimize if small size */
2427 if (!nocode_wanted
) {
2428 size
= type_size(&vtop
->type
, &align
);
2432 vtop
->type
.t
= VT_PTR
;
2435 /* address of memcpy() */
2438 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2439 else if(!(align
& 3))
2440 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2443 vpush_global_sym(&func_old_type
, TOK_memcpy
);
2448 vtop
->type
.t
= VT_PTR
;
2457 /* leave source on stack */
2458 } else if (ft
& VT_BITFIELD
) {
2459 /* bitfield store handling */
2460 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2461 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2462 /* remove bit field info to avoid loops */
2463 vtop
[-1].type
.t
= ft
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
2465 /* duplicate source into other register */
2470 if((ft
& VT_BTYPE
) == VT_BOOL
) {
2471 gen_cast(&vtop
[-1].type
);
2472 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2475 /* duplicate destination */
2477 vtop
[-1] = vtop
[-2];
2479 /* mask and shift source */
2480 if((ft
& VT_BTYPE
) != VT_BOOL
) {
2481 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2482 vpushll((1ULL << bit_size
) - 1ULL);
2484 vpushi((1 << bit_size
) - 1);
2490 /* load destination, mask and or with source */
2492 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2493 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2495 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2502 /* pop off shifted source from "duplicate source..." above */
2506 #ifdef CONFIG_TCC_BCHECK
2507 /* bound check case */
2508 if (vtop
[-1].r
& VT_MUSTBOUND
) {
2514 if (!nocode_wanted
) {
2518 #ifdef TCC_TARGET_X86_64
2519 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
2521 } else if ((ft
& VT_BTYPE
) == VT_QFLOAT
) {
2526 r
= gv(rc
); /* generate value */
2527 /* if lvalue was saved on stack, must read it */
2528 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
2530 t
= get_reg(RC_INT
);
2531 #ifdef TCC_TARGET_X86_64
2536 sv
.r
= VT_LOCAL
| VT_LVAL
;
2537 sv
.c
.ul
= vtop
[-1].c
.ul
;
2539 vtop
[-1].r
= t
| VT_LVAL
;
2541 /* two word case handling : store second register at word + 4 (or +8 for x86-64) */
2542 #ifdef TCC_TARGET_X86_64
2543 if (((ft
& VT_BTYPE
) == VT_QLONG
) || ((ft
& VT_BTYPE
) == VT_QFLOAT
)) {
2544 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
2546 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
2547 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
2549 vtop
[-1].type
.t
= load_type
;
2552 /* convert to int to increment easily */
2553 vtop
->type
.t
= addr_type
;
2559 vtop
[-1].type
.t
= load_type
;
2560 /* XXX: it works because r2 is spilled last ! */
2561 store(vtop
->r2
, vtop
- 1);
2567 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
2568 vtop
->r
|= delayed_cast
;
2572 /* post defines POST/PRE add. c is the token ++ or -- */
2573 ST_FUNC
void inc(int post
, int c
)
2576 vdup(); /* save lvalue */
2578 gv_dup(); /* duplicate value */
2583 vpushi(c
- TOK_MID
);
2585 vstore(); /* store value */
2587 vpop(); /* if post op, return saved value */
2590 /* Parse GNUC __attribute__ extension. Currently, the following
2591 extensions are recognized:
2592 - aligned(n) : set data/function alignment.
2593 - packed : force data alignment to 1
2594 - section(x) : generate data/code in this section.
2595 - unused : currently ignored, but may be used someday.
2596 - regparm(n) : pass function parameters in registers (i386 only)
2598 static void parse_attribute(AttributeDef
*ad
)
2602 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
2606 while (tok
!= ')') {
2607 if (tok
< TOK_IDENT
)
2608 expect("attribute name");
2616 expect("section name");
2617 ad
->section
= find_section(tcc_state
, (char *)tokc
.cstr
->data
);
2625 expect("alias(\"target\")");
2626 ad
->alias_target
= /* save string as token, for later */
2627 tok_alloc((char*)tokc
.cstr
->data
, tokc
.cstr
->size
-1)->tok
;
2636 if (n
<= 0 || (n
& (n
- 1)) != 0)
2637 tcc_error("alignment must be a positive power of two");
2654 /* currently, no need to handle it because tcc does not
2655 track unused objects */
2659 /* currently, no need to handle it because tcc does not
2660 track unused objects */
2665 ad
->func_call
= FUNC_CDECL
;
2670 ad
->func_call
= FUNC_STDCALL
;
2672 #ifdef TCC_TARGET_I386
2682 ad
->func_call
= FUNC_FASTCALL1
+ n
- 1;
2688 ad
->func_call
= FUNC_FASTCALLW
;
2695 ad
->mode
= VT_LLONG
+ 1;
2698 ad
->mode
= VT_SHORT
+ 1;
2701 ad
->mode
= VT_INT
+ 1;
2704 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
2711 ad
->func_export
= 1;
2714 ad
->func_import
= 1;
2717 if (tcc_state
->warn_unsupported
)
2718 tcc_warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
2719 /* skip parameters */
2721 int parenthesis
= 0;
2725 else if (tok
== ')')
2728 } while (parenthesis
&& tok
!= -1);
2741 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2742 static void struct_decl(CType
*type
, int u
, int tdef
)
2744 int a
, v
, size
, align
, maxalign
, c
, offset
, flexible
;
2745 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
, prevbt
;
2746 Sym
*s
, *ss
, *ass
, **ps
;
2750 a
= tok
; /* save decl type */
2755 /* struct already defined ? return it */
2757 expect("struct/union/enum name");
2761 tcc_error("invalid type");
2763 } else if (tok
>= TOK_IDENT
&& !tdef
)
2764 tcc_error("unknown struct/union/enum");
2770 /* we put an undefined size for struct/union */
2771 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
2772 s
->r
= 0; /* default alignment is zero as gcc */
2773 /* put struct/union/enum name in type */
2781 tcc_error("struct/union/enum already defined");
2782 /* cannot be empty */
2784 /* non empty enums are not allowed */
2785 if (a
== TOK_ENUM
) {
2789 expect("identifier");
2792 tcc_error("redefinition of enumerator '%s'",
2793 get_tok_str(v
, NULL
));
2799 /* enum symbols have static storage */
2800 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
2801 ss
->type
.t
|= VT_STATIC
;
2806 /* NOTE: we accept a trailing comma */
2810 s
->c
= type_size(&int_type
, &align
);
2819 while (tok
!= '}') {
2820 parse_btype(&btype
, &ad
);
2823 tcc_error("flexible array member '%s' not at the end of struct",
2824 get_tok_str(v
, NULL
));
2829 type_decl(&type1
, &ad
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
2830 if (v
== 0 && (type1
.t
& VT_BTYPE
) != VT_STRUCT
)
2831 expect("identifier");
2832 if (type_size(&type1
, &align
) < 0) {
2833 if ((a
== TOK_STRUCT
) && (type1
.t
& VT_ARRAY
) && c
)
2836 tcc_error("field '%s' has incomplete type",
2837 get_tok_str(v
, NULL
));
2839 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
2840 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
2841 tcc_error("invalid type for '%s'",
2842 get_tok_str(v
, NULL
));
2846 bit_size
= expr_const();
2847 /* XXX: handle v = 0 case for messages */
2849 tcc_error("negative width in bit-field '%s'",
2850 get_tok_str(v
, NULL
));
2851 if (v
&& bit_size
== 0)
2852 tcc_error("zero width for bit-field '%s'",
2853 get_tok_str(v
, NULL
));
2855 size
= type_size(&type1
, &align
);
2857 if (align
< ad
.aligned
)
2859 } else if (ad
.packed
) {
2861 } else if (*tcc_state
->pack_stack_ptr
) {
2862 if (align
> *tcc_state
->pack_stack_ptr
)
2863 align
= *tcc_state
->pack_stack_ptr
;
2866 if (bit_size
>= 0) {
2867 bt
= type1
.t
& VT_BTYPE
;
2874 tcc_error("bitfields must have scalar type");
2876 if (bit_size
> bsize
) {
2877 tcc_error("width of '%s' exceeds its type",
2878 get_tok_str(v
, NULL
));
2879 } else if (bit_size
== bsize
) {
2880 /* no need for bit fields */
2882 } else if (bit_size
== 0) {
2883 /* XXX: what to do if only padding in a
2885 /* zero size: means to pad */
2888 /* we do not have enough room ?
2889 did the type change?
2891 if ((bit_pos
+ bit_size
) > bsize
||
2892 bt
!= prevbt
|| a
== TOK_UNION
)
2895 /* XXX: handle LSB first */
2896 type1
.t
|= VT_BITFIELD
|
2897 (bit_pos
<< VT_STRUCT_SHIFT
) |
2898 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
2899 bit_pos
+= bit_size
;
2905 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2906 /* add new memory data only if starting
2908 if (lbit_pos
== 0) {
2909 if (a
== TOK_STRUCT
) {
2910 c
= (c
+ align
- 1) & -align
;
2919 if (align
> maxalign
)
2923 printf("add field %s offset=%d",
2924 get_tok_str(v
, NULL
), offset
);
2925 if (type1
.t
& VT_BITFIELD
) {
2926 printf(" pos=%d size=%d",
2927 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
2928 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
2933 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2935 while ((ass
= ass
->next
) != NULL
) {
2936 ss
= sym_push(ass
->v
, &ass
->type
, 0, offset
+ ass
->c
);
2941 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
2945 if (tok
== ';' || tok
== TOK_EOF
)
2952 /* store size and alignment */
2953 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
2959 /* return 0 if no type declaration. otherwise, return the basic type
2962 static int parse_btype(CType
*type
, AttributeDef
*ad
)
2964 int t
, u
, type_found
, typespec_found
, typedef_found
;
2968 memset(ad
, 0, sizeof(AttributeDef
));
2976 /* currently, we really ignore extension */
2986 if ((t
& VT_BTYPE
) != 0)
2987 tcc_error("too many basic types");
3003 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
3004 #ifndef TCC_TARGET_PE
3005 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3007 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
3008 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3022 if ((t
& VT_BTYPE
) == VT_LONG
) {
3023 #ifdef TCC_TARGET_PE
3024 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
3026 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3034 struct_decl(&type1
, VT_ENUM
, t
& VT_TYPEDEF
);
3037 type
->ref
= type1
.ref
;
3041 struct_decl(&type1
, VT_STRUCT
, t
& VT_TYPEDEF
);
3044 /* type modifiers */
3101 /* GNUC attribute */
3102 case TOK_ATTRIBUTE1
:
3103 case TOK_ATTRIBUTE2
:
3104 parse_attribute(ad
);
3107 t
= (t
& ~VT_BTYPE
) | u
;
3115 parse_expr_type(&type1
);
3116 /* remove all storage modifiers except typedef */
3117 type1
.t
&= ~(VT_STORAGE
&~VT_TYPEDEF
);
3120 if (typespec_found
|| typedef_found
)
3123 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
3126 t
|= (s
->type
.t
& ~VT_TYPEDEF
);
3127 type
->ref
= s
->type
.ref
;
3129 /* get attributes from typedef */
3130 if (0 == ad
->aligned
)
3131 ad
->aligned
= FUNC_ALIGN(s
->r
);
3132 if (0 == ad
->func_call
)
3133 ad
->func_call
= FUNC_CALL(s
->r
);
3134 ad
->packed
|= FUNC_PACKED(s
->r
);
3143 if ((t
& (VT_SIGNED
|VT_UNSIGNED
)) == (VT_SIGNED
|VT_UNSIGNED
))
3144 tcc_error("signed and unsigned modifier");
3145 if (tcc_state
->char_is_unsigned
) {
3146 if ((t
& (VT_SIGNED
|VT_UNSIGNED
|VT_BTYPE
)) == VT_BYTE
)
3151 /* long is never used as type */
3152 if ((t
& VT_BTYPE
) == VT_LONG
)
3153 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
3154 t
= (t
& ~VT_BTYPE
) | VT_INT
;
3156 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3162 /* convert a function parameter type (array to pointer and function to
3163 function pointer) */
3164 static inline void convert_parameter_type(CType
*pt
)
3166 /* remove const and volatile qualifiers (XXX: const could be used
3167 to indicate a const function parameter */
3168 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3169 /* array must be transformed to pointer according to ANSI C */
3171 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
3176 ST_FUNC
void parse_asm_str(CString
*astr
)
3179 /* read the string */
3181 expect("string constant");
3183 while (tok
== TOK_STR
) {
3184 /* XXX: add \0 handling too ? */
3185 cstr_cat(astr
, tokc
.cstr
->data
);
3188 cstr_ccat(astr
, '\0');
3191 /* Parse an asm label and return the label
3192 * Don't forget to free the CString in the caller! */
3193 static void asm_label_instr(CString
*astr
)
3196 parse_asm_str(astr
);
3199 printf("asm_alias: \"%s\"\n", (char *)astr
->data
);
3203 static void post_type(CType
*type
, AttributeDef
*ad
)
3205 int n
, l
, t1
, arg_size
, align
;
3206 Sym
**plast
, *s
, *first
;
3211 /* function declaration */
3219 /* read param name and compute offset */
3220 if (l
!= FUNC_OLD
) {
3221 if (!parse_btype(&pt
, &ad1
)) {
3223 tcc_error("invalid type");
3230 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
3232 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3233 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
3234 tcc_error("parameter declared as void");
3235 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
3240 expect("identifier");
3244 convert_parameter_type(&pt
);
3245 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
3251 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
3258 /* if no parameters, then old type prototype */
3262 /* NOTE: const is ignored in returned type as it has a special
3263 meaning in gcc / C++ */
3264 type
->t
&= ~VT_CONSTANT
;
3265 /* some ancient pre-K&R C allows a function to return an array
3266 and the array brackets to be put after the arguments, such
3267 that "int c()[]" means something like "int[] c()" */
3270 skip(']'); /* only handle simple "[]" */
3273 /* we push a anonymous symbol which will contain the function prototype */
3274 ad
->func_args
= arg_size
;
3275 s
= sym_push(SYM_FIELD
, type
, INT_ATTR(ad
), l
);
3279 } else if (tok
== '[') {
3280 /* array definition */
3282 if (tok
== TOK_RESTRICT1
)
3287 if (!local_stack
|| nocode_wanted
)
3288 vpushi(expr_const());
3290 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3293 tcc_error("invalid array size");
3295 if (!is_integer_btype(vtop
->type
.t
& VT_BTYPE
))
3296 tcc_error("size of variable length array should be an integer");
3301 /* parse next post type */
3302 post_type(type
, ad
);
3303 if (type
->t
== VT_FUNC
)
3304 tcc_error("declaration of an array of functions");
3305 t1
|= type
->t
& VT_VLA
;
3308 loc
-= type_size(&int_type
, &align
);
3312 vla_runtime_type_size(type
, &align
);
3314 vset(&int_type
, VT_LOCAL
|VT_LVAL
, loc
);
3321 /* we push an anonymous symbol which will contain the array
3323 s
= sym_push(SYM_FIELD
, type
, 0, n
);
3324 type
->t
= (t1
? VT_VLA
: VT_ARRAY
) | VT_PTR
;
3329 /* Parse a type declaration (except basic type), and return the type
3330 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3331 expected. 'type' should contain the basic type. 'ad' is the
3332 attribute definition of the basic type. It can be modified by
3335 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
3338 CType type1
, *type2
;
3339 int qualifiers
, storage
;
3341 while (tok
== '*') {
3349 qualifiers
|= VT_CONSTANT
;
3354 qualifiers
|= VT_VOLATILE
;
3362 type
->t
|= qualifiers
;
3365 /* XXX: clarify attribute handling */
3366 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3367 parse_attribute(ad
);
3369 /* recursive type */
3370 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3371 type1
.t
= 0; /* XXX: same as int */
3374 /* XXX: this is not correct to modify 'ad' at this point, but
3375 the syntax is not clear */
3376 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3377 parse_attribute(ad
);
3378 type_decl(&type1
, ad
, v
, td
);
3381 /* type identifier */
3382 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
3386 if (!(td
& TYPE_ABSTRACT
))
3387 expect("identifier");
3391 storage
= type
->t
& VT_STORAGE
;
3392 type
->t
&= ~VT_STORAGE
;
3393 if (storage
& VT_STATIC
) {
3394 int saved_nocode_wanted
= nocode_wanted
;
3396 post_type(type
, ad
);
3397 nocode_wanted
= saved_nocode_wanted
;
3399 post_type(type
, ad
);
3401 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3402 parse_attribute(ad
);
3406 /* append type at the end of type1 */
3419 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3420 ST_FUNC
int lvalue_type(int t
)
3425 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
3427 else if (bt
== VT_SHORT
)
3431 if (t
& VT_UNSIGNED
)
3432 r
|= VT_LVAL_UNSIGNED
;
3436 /* indirection with full error checking and bound check */
3437 ST_FUNC
void indir(void)
3439 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
3440 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
3444 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
3446 vtop
->type
= *pointed_type(&vtop
->type
);
3447 /* Arrays and functions are never lvalues */
3448 if (!(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_VLA
)
3449 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3450 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3451 /* if bound checking, the referenced pointer must be checked */
3452 #ifdef CONFIG_TCC_BCHECK
3453 if (tcc_state
->do_bounds_check
)
3454 vtop
->r
|= VT_MUSTBOUND
;
3459 /* pass a parameter to a function and do type checking and casting */
3460 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
3465 func_type
= func
->c
;
3466 if (func_type
== FUNC_OLD
||
3467 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
3468 /* default casting : only need to convert float to double */
3469 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
3473 } else if (arg
== NULL
) {
3474 tcc_error("too many arguments to function");
3477 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
3478 gen_assign_cast(&type
);
3482 /* parse an expression of the form '(type)' or '(expr)' and return its
3484 static void parse_expr_type(CType
*type
)
3490 if (parse_btype(type
, &ad
)) {
3491 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3498 static void parse_type(CType
*type
)
3503 if (!parse_btype(type
, &ad
)) {
3506 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3509 static void vpush_tokc(int t
)
3514 vsetc(&type
, VT_CONST
, &tokc
);
3517 ST_FUNC
void unary(void)
3519 int n
, t
, align
, size
, r
, sizeof_caller
;
3523 static int in_sizeof
= 0;
3525 sizeof_caller
= in_sizeof
;
3527 /* XXX: GCC 2.95.3 does not generate a table although it should be
3541 vpush_tokc(VT_INT
| VT_UNSIGNED
);
3545 vpush_tokc(VT_LLONG
);
3549 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
3553 vpush_tokc(VT_FLOAT
);
3557 vpush_tokc(VT_DOUBLE
);
3561 vpush_tokc(VT_LDOUBLE
);
3564 case TOK___FUNCTION__
:
3566 goto tok_identifier
;
3572 /* special function name identifier */
3573 len
= strlen(funcname
) + 1;
3574 /* generate char[len] type */
3579 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
3580 ptr
= section_ptr_add(data_section
, len
);
3581 memcpy(ptr
, funcname
, len
);
3586 #ifdef TCC_TARGET_PE
3587 t
= VT_SHORT
| VT_UNSIGNED
;
3593 /* string parsing */
3596 if (tcc_state
->warn_write_strings
)
3601 memset(&ad
, 0, sizeof(AttributeDef
));
3602 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, NULL
, 0);
3607 if (parse_btype(&type
, &ad
)) {
3608 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
3610 /* check ISOC99 compound literal */
3612 /* data is allocated locally by default */
3617 /* all except arrays are lvalues */
3618 if (!(type
.t
& VT_ARRAY
))
3619 r
|= lvalue_type(type
.t
);
3620 memset(&ad
, 0, sizeof(AttributeDef
));
3621 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, NULL
, 0);
3623 if (sizeof_caller
) {
3630 } else if (tok
== '{') {
3631 /* save all registers */
3633 /* statement expression : we do not accept break/continue
3634 inside as GCC does */
3635 block(NULL
, NULL
, NULL
, NULL
, 0, 1);
3650 /* functions names must be treated as function pointers,
3651 except for unary '&' and sizeof. Since we consider that
3652 functions are not lvalues, we only have to handle it
3653 there and in function calls. */
3654 /* arrays can also be used although they are not lvalues */
3655 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
3656 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
3658 mk_pointer(&vtop
->type
);
3664 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3666 boolean
.t
= VT_BOOL
;
3668 vtop
->c
.i
= !vtop
->c
.i
;
3669 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
3670 vtop
->c
.i
= vtop
->c
.i
^ 1;
3673 vseti(VT_JMP
, gtst(1, 0));
3684 /* in order to force cast, we add zero */
3686 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
3687 tcc_error("pointer not accepted for unary plus");
3697 unary_type(&type
); // Perform a in_sizeof = 0;
3698 size
= type_size(&type
, &align
);
3699 if (t
== TOK_SIZEOF
) {
3700 if (!(type
.t
& VT_VLA
)) {
3702 tcc_error("sizeof applied to an incomplete type");
3705 vla_runtime_type_size(&type
, &align
);
3710 vtop
->type
.t
|= VT_UNSIGNED
;
3713 case TOK_builtin_types_compatible_p
:
3722 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3723 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3724 vpushi(is_compatible_types(&type1
, &type2
));
3727 case TOK_builtin_constant_p
:
3729 int saved_nocode_wanted
, res
;
3732 saved_nocode_wanted
= nocode_wanted
;
3735 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
3737 nocode_wanted
= saved_nocode_wanted
;
3742 case TOK_builtin_frame_address
:
3748 if (tok
!= TOK_CINT
|| tokc
.i
< 0) {
3749 tcc_error("__builtin_frame_address only takes positive integers");
3756 vset(&type
, VT_LOCAL
, 0); /* local frame */
3758 mk_pointer(&vtop
->type
);
3759 indir(); /* -> parent frame */
3763 #ifdef TCC_TARGET_X86_64
3764 #ifdef TCC_TARGET_PE
3765 case TOK_builtin_va_start
:
3773 if ((vtop
->r
& VT_VALMASK
) != VT_LOCAL
)
3774 tcc_error("__builtin_va_start expects a local variable");
3775 vtop
->r
&= ~(VT_LVAL
| VT_REF
);
3776 vtop
->type
= char_pointer_type
;
3781 case TOK_builtin_va_arg_types
:
3788 vpushi(classify_x86_64_va_arg(&type
));
3808 goto tok_identifier
;
3810 /* allow to take the address of a label */
3811 if (tok
< TOK_UIDENT
)
3812 expect("label identifier");
3813 s
= label_find(tok
);
3815 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
3817 if (s
->r
== LABEL_DECLARED
)
3818 s
->r
= LABEL_FORWARD
;
3821 s
->type
.t
= VT_VOID
;
3822 mk_pointer(&s
->type
);
3823 s
->type
.t
|= VT_STATIC
;
3825 vset(&s
->type
, VT_CONST
| VT_SYM
, 0);
3830 // special qnan , snan and infinity values
3832 vpush64(VT_DOUBLE
, 0x7ff8000000000000ULL
);
3836 vpush64(VT_DOUBLE
, 0x7ff0000000000001ULL
);
3840 vpush64(VT_DOUBLE
, 0x7ff0000000000000ULL
);
3849 expect("identifier");
3853 tcc_error("'%s' undeclared", get_tok_str(t
, NULL
));
3854 /* for simple function calls, we tolerate undeclared
3855 external reference to int() function */
3856 if (tcc_state
->warn_implicit_function_declaration
)
3857 tcc_warning("implicit declaration of function '%s'",
3858 get_tok_str(t
, NULL
));
3859 s
= external_global_sym(t
, &func_old_type
, 0);
3861 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
3862 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
3863 /* if referencing an inline function, then we generate a
3864 symbol to it if not already done. It will have the
3865 effect to generate code for it at the end of the
3866 compilation unit. Inline function as always
3867 generated in the text section. */
3869 put_extern_sym(s
, text_section
, 0, 0);
3870 r
= VT_SYM
| VT_CONST
;
3874 vset(&s
->type
, r
, s
->c
);
3875 /* if forward reference, we must point to s */
3876 if (vtop
->r
& VT_SYM
) {
3883 /* post operations */
3885 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
3888 } else if (tok
== '.' || tok
== TOK_ARROW
) {
3891 if (tok
== TOK_ARROW
)
3893 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
3897 /* expect pointer on structure */
3898 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
3899 expect("struct or union");
3903 while ((s
= s
->next
) != NULL
) {
3908 tcc_error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, NULL
));
3909 /* add field offset to pointer */
3910 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
3913 /* change type to field type, and set to lvalue */
3914 vtop
->type
= s
->type
;
3915 vtop
->type
.t
|= qualifiers
;
3916 /* an array is never an lvalue */
3917 if (!(vtop
->type
.t
& VT_ARRAY
)) {
3918 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3919 #ifdef CONFIG_TCC_BCHECK
3920 /* if bound checking, the referenced pointer must be checked */
3921 if (tcc_state
->do_bounds_check
)
3922 vtop
->r
|= VT_MUSTBOUND
;
3926 } else if (tok
== '[') {
3932 } else if (tok
== '(') {
3938 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3939 /* pointer test (no array accepted) */
3940 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
3941 vtop
->type
= *pointed_type(&vtop
->type
);
3942 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
3946 expect("function pointer");
3949 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
3951 /* get return type */
3954 sa
= s
->next
; /* first parameter */
3957 /* compute first implicit argument if a structure is returned */
3958 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
3960 sret
= gfunc_sret(&s
->type
, &ret
.type
, &ret_align
);
3962 /* get some space for the returned structure */
3963 size
= type_size(&s
->type
, &align
);
3964 loc
= (loc
- size
) & -align
;
3966 ret
.r
= VT_LOCAL
| VT_LVAL
;
3967 /* pass it as 'int' to avoid structure arg passing
3969 vseti(VT_LOCAL
, loc
);
3979 /* return in register */
3980 if (is_float(ret
.type
.t
)) {
3981 ret
.r
= reg_fret(ret
.type
.t
);
3982 #ifdef TCC_TARGET_X86_64
3983 if ((ret
.type
.t
& VT_BTYPE
) == VT_QFLOAT
)
3987 #ifdef TCC_TARGET_X86_64
3988 if ((ret
.type
.t
& VT_BTYPE
) == VT_QLONG
)
3990 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
4000 gfunc_param_typed(s
, sa
);
4010 tcc_error("too few arguments to function");
4012 if (!nocode_wanted
) {
4013 gfunc_call(nb_args
);
4015 vtop
-= (nb_args
+ 1);
4018 vsetc(&ret
.type
, ret
.r
, &ret
.c
);
4020 /* handle packed struct return */
4021 if (((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) && !sret
) {
4023 size
= type_size(&s
->type
, &align
);
4024 loc
= (loc
- size
) & -align
;
4026 vset(&ret
.type
, VT_LOCAL
| VT_LVAL
, addr
);
4030 vset(&s
->type
, VT_LOCAL
| VT_LVAL
, addr
);
4038 ST_FUNC
void expr_prod(void)
4043 while (tok
== '*' || tok
== '/' || tok
== '%') {
4051 ST_FUNC
void expr_sum(void)
4056 while (tok
== '+' || tok
== '-') {
4064 static void expr_shift(void)
4069 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
4077 static void expr_cmp(void)
4082 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
4083 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
4091 static void expr_cmpeq(void)
4096 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
4104 static void expr_and(void)
4107 while (tok
== '&') {
4114 static void expr_xor(void)
4117 while (tok
== '^') {
4124 static void expr_or(void)
4127 while (tok
== '|') {
4134 /* XXX: fix this mess */
4135 static void expr_land_const(void)
4138 while (tok
== TOK_LAND
) {
4145 /* XXX: fix this mess */
4146 static void expr_lor_const(void)
4149 while (tok
== TOK_LOR
) {
4156 /* only used if non constant */
4157 static void expr_land(void)
4162 if (tok
== TOK_LAND
) {
4167 if (tok
!= TOK_LAND
) {
4177 static void expr_lor(void)
4182 if (tok
== TOK_LOR
) {
4187 if (tok
!= TOK_LOR
) {
4197 /* XXX: better constant handling */
4198 static void expr_cond(void)
4200 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
;
4202 CType type
, type1
, type2
;
4209 boolean
.t
= VT_BOOL
;
4215 if (tok
!= ':' || !gnu_ext
) {
4230 if (vtop
!= vstack
) {
4231 /* needed to avoid having different registers saved in
4233 if (is_float(vtop
->type
.t
)) {
4235 #ifdef TCC_TARGET_X86_64
4236 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4246 if (tok
== ':' && gnu_ext
) {
4254 sv
= *vtop
; /* save value to handle it later */
4255 vtop
--; /* no vpop so that FP stack is not flushed */
4263 bt1
= t1
& VT_BTYPE
;
4265 bt2
= t2
& VT_BTYPE
;
4266 /* cast operands to correct type according to ISOC rules */
4267 if (is_float(bt1
) || is_float(bt2
)) {
4268 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
4269 type
.t
= VT_LDOUBLE
;
4270 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
4275 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
4276 /* cast to biggest op */
4278 /* convert to unsigned if it does not fit in a long long */
4279 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
4280 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
4281 type
.t
|= VT_UNSIGNED
;
4282 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
4283 /* If one is a null ptr constant the result type
4285 if (is_null_pointer (vtop
))
4287 else if (is_null_pointer (&sv
))
4289 /* XXX: test pointer compatibility, C99 has more elaborate
4293 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
4294 /* XXX: test function pointer compatibility */
4295 type
= bt1
== VT_FUNC
? type1
: type2
;
4296 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
4297 /* XXX: test structure compatibility */
4298 type
= bt1
== VT_STRUCT
? type1
: type2
;
4299 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
4300 /* NOTE: as an extension, we accept void on only one side */
4303 /* integer operations */
4305 /* convert to unsigned if it does not fit in an integer */
4306 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
4307 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
4308 type
.t
|= VT_UNSIGNED
;
4311 /* now we convert second operand */
4313 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4316 if (is_float(type
.t
)) {
4318 #ifdef TCC_TARGET_X86_64
4319 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4323 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
4324 /* for long longs, we use fixed registers to avoid having
4325 to handle a complicated move */
4330 /* this is horrible, but we must also convert first
4334 /* put again first value and cast it */
4337 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4340 move_reg(r2
, r1
, type
.t
);
4347 static void expr_eq(void)
4353 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
4354 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
4355 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
4370 ST_FUNC
void gexpr(void)
4381 /* parse an expression and return its type without any side effect. */
4382 static void expr_type(CType
*type
)
4384 int saved_nocode_wanted
;
4386 saved_nocode_wanted
= nocode_wanted
;
4391 nocode_wanted
= saved_nocode_wanted
;
4394 /* parse a unary expression and return its type without any side
4396 static void unary_type(CType
*type
)
4408 /* parse a constant expression and return value in vtop. */
4409 static void expr_const1(void)
4418 /* parse an integer constant and return its value. */
4419 ST_FUNC
int expr_const(void)
4423 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
4424 expect("constant expression");
4430 /* return the label token if current token is a label, otherwise
4432 static int is_label(void)
4436 /* fast test first */
4437 if (tok
< TOK_UIDENT
)
4439 /* no need to save tokc because tok is an identifier */
4446 unget_tok(last_tok
);
4451 static void label_or_decl(int l
)
4455 /* fast test first */
4456 if (tok
>= TOK_UIDENT
)
4458 /* no need to save tokc because tok is an identifier */
4462 unget_tok(last_tok
);
4465 unget_tok(last_tok
);
4470 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
4471 int case_reg
, int is_expr
)
4474 Sym
*s
, *frame_bottom
;
4476 /* generate line number info */
4477 if (tcc_state
->do_debug
&&
4478 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
4479 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
4481 last_line_num
= file
->line_num
;
4485 /* default return value is (void) */
4487 vtop
->type
.t
= VT_VOID
;
4490 if (tok
== TOK_IF
) {
4497 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4499 if (c
== TOK_ELSE
) {
4503 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4504 gsym(d
); /* patch else jmp */
4507 } else if (tok
== TOK_WHILE
) {
4515 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4519 } else if (tok
== '{') {
4521 int block_vla_sp_loc
, *saved_vla_sp_loc
, saved_vla_flags
;
4524 /* record local declaration stack position */
4526 frame_bottom
= sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4527 frame_bottom
->next
= scope_stack_bottom
;
4528 scope_stack_bottom
= frame_bottom
;
4529 llabel
= local_label_stack
;
4531 /* save VLA state */
4532 block_vla_sp_loc
= *(saved_vla_sp_loc
= vla_sp_loc
);
4533 if (saved_vla_sp_loc
!= &vla_sp_root_loc
)
4534 vla_sp_loc
= &block_vla_sp_loc
;
4536 saved_vla_flags
= vla_flags
;
4537 vla_flags
|= VLA_NEED_NEW_FRAME
;
4539 /* handle local labels declarations */
4540 if (tok
== TOK_LABEL
) {
4543 if (tok
< TOK_UIDENT
)
4544 expect("label identifier");
4545 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
4555 while (tok
!= '}') {
4556 label_or_decl(VT_LOCAL
);
4560 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4563 /* pop locally defined labels */
4564 label_pop(&local_label_stack
, llabel
);
4566 /* XXX: this solution makes only valgrind happy...
4567 triggered by gcc.c-torture/execute/20000917-1.c */
4569 switch(vtop
->type
.t
& VT_BTYPE
) {
4574 for(p
=vtop
->type
.ref
;p
;p
=p
->prev
)
4576 tcc_error("unsupported expression type");
4579 /* pop locally defined symbols */
4580 scope_stack_bottom
= scope_stack_bottom
->next
;
4581 sym_pop(&local_stack
, s
);
4583 /* Pop VLA frames and restore stack pointer if required */
4584 if (saved_vla_sp_loc
!= &vla_sp_root_loc
)
4585 *saved_vla_sp_loc
= block_vla_sp_loc
;
4586 if (vla_sp_loc
!= (saved_vla_sp_loc
== &vla_sp_root_loc
? &vla_sp_root_loc
: &block_vla_sp_loc
)) {
4587 vla_sp_loc
= saved_vla_sp_loc
;
4588 gen_vla_sp_restore(*vla_sp_loc
);
4590 vla_flags
= (vla_flags
& ~VLA_SCOPE_FLAGS
) | (saved_vla_flags
& VLA_SCOPE_FLAGS
);
4593 } else if (tok
== TOK_RETURN
) {
4597 gen_assign_cast(&func_vt
);
4598 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
4599 CType type
, ret_type
;
4601 if (gfunc_sret(&func_vt
, &ret_type
, &ret_align
)) {
4602 /* if returning structure, must copy it to implicit
4603 first pointer arg location */
4606 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
4609 /* copy structure value to pointer */
4612 /* returning structure packed into registers */
4613 int size
, addr
, align
;
4614 size
= type_size(&func_vt
,&align
);
4615 if ((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) || (vtop
->c
.i
& (ret_align
-1)))
4616 && (align
& (ret_align
-1))) {
4617 loc
= (loc
- size
) & -align
;
4620 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
4623 vset(&ret_type
, VT_LOCAL
| VT_LVAL
, addr
);
4625 vtop
->type
= ret_type
;
4626 if (is_float(ret_type
.t
))
4627 gv(rc_fret(ret_type
.t
));
4631 } else if (is_float(func_vt
.t
)) {
4632 gv(rc_fret(func_vt
.t
));
4636 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
4639 rsym
= gjmp(rsym
); /* jmp */
4640 } else if (tok
== TOK_BREAK
) {
4643 tcc_error("cannot break");
4644 *bsym
= gjmp(*bsym
);
4647 } else if (tok
== TOK_CONTINUE
) {
4650 tcc_error("cannot continue");
4651 *csym
= gjmp(*csym
);
4654 } else if (tok
== TOK_FOR
) {
4659 frame_bottom
= sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4660 frame_bottom
->next
= scope_stack_bottom
;
4661 scope_stack_bottom
= frame_bottom
;
4663 /* c99 for-loop init decl? */
4664 if (!decl0(VT_LOCAL
, 1)) {
4665 /* no, regular for-loop init expr */
4689 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4693 scope_stack_bottom
= scope_stack_bottom
->next
;
4694 sym_pop(&local_stack
, s
);
4696 if (tok
== TOK_DO
) {
4701 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4712 if (tok
== TOK_SWITCH
) {
4716 /* XXX: other types than integer */
4717 case_reg
= gv(RC_INT
);
4721 b
= gjmp(0); /* jump to first case */
4723 block(&a
, csym
, &b
, &c
, case_reg
, 0);
4724 /* if no default, jmp after switch */
4732 if (tok
== TOK_CASE
) {
4739 if (gnu_ext
&& tok
== TOK_DOTS
) {
4743 tcc_warning("empty case range");
4745 /* since a case is like a label, we must skip it with a jmp */
4752 *case_sym
= gtst(1, 0);
4755 *case_sym
= gtst(1, 0);
4759 *case_sym
= gtst(1, *case_sym
);
4764 goto block_after_label
;
4766 if (tok
== TOK_DEFAULT
) {
4772 tcc_error("too many 'default'");
4775 goto block_after_label
;
4777 if (tok
== TOK_GOTO
) {
4779 if (tok
== '*' && gnu_ext
) {
4783 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
4786 } else if (tok
>= TOK_UIDENT
) {
4787 s
= label_find(tok
);
4788 /* put forward definition if needed */
4790 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4792 if (s
->r
== LABEL_DECLARED
)
4793 s
->r
= LABEL_FORWARD
;
4795 /* label already defined */
4796 if (vla_flags
& VLA_IN_SCOPE
) {
4797 /* If VLAs are in use, save the current stack pointer and
4798 reset the stack pointer to what it was at function entry
4799 (label will restore stack pointer in inner scopes) */
4801 gen_vla_sp_restore(vla_sp_root_loc
);
4803 if (s
->r
& LABEL_FORWARD
)
4804 s
->jnext
= gjmp(s
->jnext
);
4806 gjmp_addr(s
->jnext
);
4809 expect("label identifier");
4812 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
4818 if (vla_flags
& VLA_IN_SCOPE
) {
4819 /* save/restore stack pointer across label
4820 this is a no-op when combined with the load immediately
4821 after the label unless we arrive via goto */
4826 if (s
->r
== LABEL_DEFINED
)
4827 tcc_error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
4829 s
->r
= LABEL_DEFINED
;
4831 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
4834 if (vla_flags
& VLA_IN_SCOPE
) {
4835 gen_vla_sp_restore(*vla_sp_loc
);
4836 vla_flags
|= VLA_NEED_NEW_FRAME
;
4838 /* we accept this, but it is a mistake */
4841 tcc_warning("deprecated use of label at end of compound statement");
4845 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4848 /* expression case */
4863 /* t is the array or struct type. c is the array or struct
4864 address. cur_index/cur_field is the pointer to the current
4865 value. 'size_only' is true if only size info is needed (only used
4867 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
4868 int *cur_index
, Sym
**cur_field
,
4872 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
4878 if (gnu_ext
&& (l
= is_label()) != 0)
4880 while (tok
== '[' || tok
== '.') {
4882 if (!(type
->t
& VT_ARRAY
))
4883 expect("array type");
4886 index
= expr_const();
4887 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
4888 expect("invalid index");
4889 if (tok
== TOK_DOTS
&& gnu_ext
) {
4891 index_last
= expr_const();
4892 if (index_last
< 0 ||
4893 (s
->c
>= 0 && index_last
>= s
->c
) ||
4895 expect("invalid index");
4901 *cur_index
= index_last
;
4902 type
= pointed_type(type
);
4903 elem_size
= type_size(type
, &align
);
4904 c
+= index
* elem_size
;
4905 /* NOTE: we only support ranges for last designator */
4906 nb_elems
= index_last
- index
+ 1;
4907 if (nb_elems
!= 1) {
4916 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
4917 expect("struct/union type");
4930 /* XXX: fix this mess by using explicit storage field */
4932 type1
.t
|= (type
->t
& ~VT_TYPE
);
4946 if (type
->t
& VT_ARRAY
) {
4948 type
= pointed_type(type
);
4949 c
+= index
* type_size(type
, &align
);
4953 tcc_error("too many field init");
4954 /* XXX: fix this mess by using explicit storage field */
4956 type1
.t
|= (type
->t
& ~VT_TYPE
);
4961 decl_initializer(type
, sec
, c
, 0, size_only
);
4963 /* XXX: make it more general */
4964 if (!size_only
&& nb_elems
> 1) {
4965 unsigned long c_end
;
4970 tcc_error("range init not supported yet for dynamic storage");
4971 c_end
= c
+ nb_elems
* elem_size
;
4972 if (c_end
> sec
->data_allocated
)
4973 section_realloc(sec
, c_end
);
4974 src
= sec
->data
+ c
;
4976 for(i
= 1; i
< nb_elems
; i
++) {
4978 memcpy(dst
, src
, elem_size
);
4984 #define EXPR_CONST 1
4987 /* store a value or an expression directly in global data or in local array */
4988 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
4989 int v
, int expr_type
)
4991 int saved_global_expr
, bt
, bit_pos
, bit_size
;
4993 unsigned long long bit_mask
;
5001 /* compound literals must be allocated globally in this case */
5002 saved_global_expr
= global_expr
;
5005 global_expr
= saved_global_expr
;
5006 /* NOTE: symbols are accepted */
5007 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
5008 tcc_error("initializer element is not constant");
5016 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
5019 /* XXX: not portable */
5020 /* XXX: generate error if incorrect relocation */
5021 gen_assign_cast(&dtype
);
5022 bt
= type
->t
& VT_BTYPE
;
5023 /* we'll write at most 12 bytes */
5024 if (c
+ 12 > sec
->data_allocated
) {
5025 section_realloc(sec
, c
+ 12);
5027 ptr
= sec
->data
+ c
;
5028 /* XXX: make code faster ? */
5029 if (!(type
->t
& VT_BITFIELD
)) {
5034 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5035 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
5036 bit_mask
= (1LL << bit_size
) - 1;
5038 if ((vtop
->r
& VT_SYM
) &&
5044 (bt
== VT_INT
&& bit_size
!= 32)))
5045 tcc_error("initializer element is not computable at load time");
5048 vtop
->c
.i
= (vtop
->c
.i
!= 0);
5050 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5053 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5056 *(double *)ptr
= vtop
->c
.d
;
5059 *(long double *)ptr
= vtop
->c
.ld
;
5062 *(long long *)ptr
|= (vtop
->c
.ll
& bit_mask
) << bit_pos
;
5065 if (vtop
->r
& VT_SYM
) {
5066 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
5068 *(int *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5073 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
5080 /* put zeros for variable based init */
5081 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
5084 /* nothing to do because globals are already set to zero */
5086 vpush_global_sym(&func_old_type
, TOK_memset
);
5094 /* 't' contains the type and storage info. 'c' is the offset of the
5095 object in section 'sec'. If 'sec' is NULL, it means stack based
5096 allocation. 'first' is true if array '{' must be read (multi
5097 dimension implicit array init handling). 'size_only' is true if
5098 size only evaluation is wanted (only for arrays). */
5099 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
5100 int first
, int size_only
)
5102 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, parlevel1
, i
;
5103 int size1
, align1
, expr_type
;
5107 if (type
->t
& VT_VLA
) {
5110 /* save current stack pointer */
5111 if (vla_flags
& VLA_NEED_NEW_FRAME
) {
5113 vla_flags
= VLA_IN_SCOPE
;
5114 vla_sp_loc
= &vla_sp_loc_tmp
;
5117 vla_runtime_type_size(type
, &a
);
5118 gen_vla_alloc(type
, a
);
5119 vset(type
, VT_LOCAL
|VT_LVAL
, c
);
5123 } else if (type
->t
& VT_ARRAY
) {
5127 t1
= pointed_type(type
);
5128 size1
= type_size(t1
, &align1
);
5131 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
5134 tcc_error("character array initializer must be a literal,"
5135 " optionally enclosed in braces");
5140 /* only parse strings here if correct type (otherwise: handle
5141 them as ((w)char *) expressions */
5142 if ((tok
== TOK_LSTR
&&
5143 #ifdef TCC_TARGET_PE
5144 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
5146 (t1
->t
& VT_BTYPE
) == VT_INT
5148 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
5149 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5154 /* compute maximum number of chars wanted */
5156 cstr_len
= cstr
->size
;
5158 cstr_len
= cstr
->size
/ sizeof(nwchar_t
);
5161 if (n
>= 0 && nb
> (n
- array_length
))
5162 nb
= n
- array_length
;
5165 tcc_warning("initializer-string for array is too long");
5166 /* in order to go faster for common case (char
5167 string in global variable, we handle it
5169 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
5170 memcpy(sec
->data
+ c
+ array_length
, cstr
->data
, nb
);
5174 ch
= ((unsigned char *)cstr
->data
)[i
];
5176 ch
= ((nwchar_t
*)cstr
->data
)[i
];
5177 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
5185 /* only add trailing zero if enough storage (no
5186 warning in this case since it is standard) */
5187 if (n
< 0 || array_length
< n
) {
5189 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
5195 while (tok
!= '}') {
5196 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
5197 if (n
>= 0 && index
>= n
)
5198 tcc_error("index too large");
5199 /* must put zero in holes (note that doing it that way
5200 ensures that it even works with designators) */
5201 if (!size_only
&& array_length
< index
) {
5202 init_putz(t1
, sec
, c
+ array_length
* size1
,
5203 (index
- array_length
) * size1
);
5206 if (index
> array_length
)
5207 array_length
= index
;
5208 /* special test for multi dimensional arrays (may not
5209 be strictly correct if designators are used at the
5211 if (index
>= n
&& no_oblock
)
5220 /* put zeros at the end */
5221 if (!size_only
&& n
>= 0 && array_length
< n
) {
5222 init_putz(t1
, sec
, c
+ array_length
* size1
,
5223 (n
- array_length
) * size1
);
5225 /* patch type size if needed */
5227 s
->c
= array_length
;
5228 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
5229 (sec
|| !first
|| tok
== '{')) {
5232 /* NOTE: the previous test is a specific case for automatic
5233 struct/union init */
5234 /* XXX: union needs only one init */
5236 /* XXX: this test is incorrect for local initializers
5237 beginning with ( without {. It would be much more difficult
5238 to do it correctly (ideally, the expression parser should
5239 be used in all cases) */
5245 while (tok
== '(') {
5249 if (!parse_btype(&type1
, &ad1
))
5251 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
5253 if (!is_assignable_types(type
, &type1
))
5254 tcc_error("invalid type for cast");
5259 if (first
|| tok
== '{') {
5268 while (tok
!= '}') {
5269 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
5271 if (!size_only
&& array_length
< index
) {
5272 init_putz(type
, sec
, c
+ array_length
,
5273 index
- array_length
);
5275 index
= index
+ type_size(&f
->type
, &align1
);
5276 if (index
> array_length
)
5277 array_length
= index
;
5279 /* gr: skip fields from same union - ugly. */
5281 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
5282 /* test for same offset */
5283 if (f
->next
->c
!= f
->c
)
5285 /* if yes, test for bitfield shift */
5286 if ((f
->type
.t
& VT_BITFIELD
) && (f
->next
->type
.t
& VT_BITFIELD
)) {
5287 int bit_pos_1
= (f
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5288 int bit_pos_2
= (f
->next
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5289 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
5290 if (bit_pos_1
!= bit_pos_2
)
5297 if (no_oblock
&& f
== NULL
)
5303 /* put zeros at the end */
5304 if (!size_only
&& array_length
< n
) {
5305 init_putz(type
, sec
, c
+ array_length
,
5314 } else if (tok
== '{') {
5316 decl_initializer(type
, sec
, c
, first
, size_only
);
5318 } else if (size_only
) {
5319 /* just skip expression */
5320 parlevel
= parlevel1
= 0;
5321 while ((parlevel
> 0 || parlevel1
> 0 ||
5322 (tok
!= '}' && tok
!= ',')) && tok
!= -1) {
5325 else if (tok
== ')')
5327 else if (tok
== '{')
5329 else if (tok
== '}')
5334 /* currently, we always use constant expression for globals
5335 (may change for scripting case) */
5336 expr_type
= EXPR_CONST
;
5338 expr_type
= EXPR_ANY
;
5339 init_putv(type
, sec
, c
, 0, expr_type
);
5343 /* parse an initializer for type 't' if 'has_init' is non zero, and
5344 allocate space in local or global data space ('r' is either
5345 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5346 variable 'v' with an associated name represented by 'asm_label' of
5347 scope 'scope' is declared before initializers are parsed. If 'v' is
5348 zero, then a reference to the new object is put in the value stack.
5349 If 'has_init' is 2, a special parsing is done to handle string
5351 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
5352 int has_init
, int v
, char *asm_label
,
5355 int size
, align
, addr
, data_offset
;
5357 ParseState saved_parse_state
= {0};
5358 TokenString init_str
;
5360 Sym
*flexible_array
;
5362 flexible_array
= NULL
;
5363 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
5364 Sym
*field
= type
->ref
->next
;
5367 field
= field
->next
;
5368 if (field
->type
.t
& VT_ARRAY
&& field
->type
.ref
->c
< 0)
5369 flexible_array
= field
;
5373 size
= type_size(type
, &align
);
5374 /* If unknown size, we must evaluate it before
5375 evaluating initializers because
5376 initializers can generate global data too
5377 (e.g. string pointers or ISOC99 compound
5378 literals). It also simplifies local
5379 initializers handling */
5380 tok_str_new(&init_str
);
5381 if (size
< 0 || (flexible_array
&& has_init
)) {
5383 tcc_error("unknown type size");
5384 /* get all init string */
5385 if (has_init
== 2) {
5386 /* only get strings */
5387 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5388 tok_str_add_tok(&init_str
);
5393 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
5395 tcc_error("unexpected end of file in initializer");
5396 tok_str_add_tok(&init_str
);
5399 else if (tok
== '}') {
5409 tok_str_add(&init_str
, -1);
5410 tok_str_add(&init_str
, 0);
5413 save_parse_state(&saved_parse_state
);
5415 macro_ptr
= init_str
.str
;
5417 decl_initializer(type
, NULL
, 0, 1, 1);
5418 /* prepare second initializer parsing */
5419 macro_ptr
= init_str
.str
;
5422 /* if still unknown size, error */
5423 size
= type_size(type
, &align
);
5425 tcc_error("unknown type size");
5428 size
+= flexible_array
->type
.ref
->c
* pointed_size(&flexible_array
->type
);
5429 /* take into account specified alignment if bigger */
5431 if (ad
->aligned
> align
)
5432 align
= ad
->aligned
;
5433 } else if (ad
->packed
) {
5436 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
5438 #ifdef CONFIG_TCC_BCHECK
5439 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5443 loc
= (loc
- size
) & -align
;
5445 #ifdef CONFIG_TCC_BCHECK
5446 /* handles bounds */
5447 /* XXX: currently, since we do only one pass, we cannot track
5448 '&' operators, so we add only arrays */
5449 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5450 unsigned long *bounds_ptr
;
5451 /* add padding between regions */
5453 /* then add local bound info */
5454 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(unsigned long));
5455 bounds_ptr
[0] = addr
;
5456 bounds_ptr
[1] = size
;
5460 /* local variable */
5461 sym_push(v
, type
, r
, addr
);
5463 /* push local reference */
5464 vset(type
, r
, addr
);
5470 if (v
&& scope
== VT_CONST
) {
5471 /* see if the symbol was already defined */
5474 if (!is_compatible_types(&sym
->type
, type
))
5475 tcc_error("incompatible types for redefinition of '%s'",
5476 get_tok_str(v
, NULL
));
5477 if (sym
->type
.t
& VT_EXTERN
) {
5478 /* if the variable is extern, it was not allocated */
5479 sym
->type
.t
&= ~VT_EXTERN
;
5480 /* set array size if it was ommited in extern
5482 if ((sym
->type
.t
& VT_ARRAY
) &&
5483 sym
->type
.ref
->c
< 0 &&
5485 sym
->type
.ref
->c
= type
->ref
->c
;
5487 /* we accept several definitions of the same
5488 global variable. this is tricky, because we
5489 must play with the SHN_COMMON type of the symbol */
5490 /* XXX: should check if the variable was already
5491 initialized. It is incorrect to initialized it
5493 /* no init data, we won't add more to the symbol */
5500 /* allocate symbol in corresponding section */
5504 if (type
->t
& VT_TLS
) {
5506 tdata_section
= new_section(tcc_state
, ".tdata",
5508 SHF_ALLOC
| SHF_WRITE
| SHF_TLS
);
5509 sec
= tdata_section
;
5513 else if (tcc_state
->nocommon
) {
5514 if (type
->t
& VT_TLS
) {
5516 tbss_section
= new_section(tcc_state
, ".tbss",
5518 SHF_ALLOC
| SHF_WRITE
| SHF_TLS
);
5525 data_offset
= sec
->data_offset
;
5526 data_offset
= (data_offset
+ align
- 1) & -align
;
5528 /* very important to increment global pointer at this time
5529 because initializers themselves can create new initializers */
5530 data_offset
+= size
;
5531 #ifdef CONFIG_TCC_BCHECK
5532 /* add padding if bound check */
5533 if (tcc_state
->do_bounds_check
)
5536 sec
->data_offset
= data_offset
;
5537 /* allocate section space to put the data */
5538 if (sec
->sh_type
!= SHT_NOBITS
&&
5539 data_offset
> sec
->data_allocated
)
5540 section_realloc(sec
, data_offset
);
5541 /* align section if needed */
5542 if (align
> sec
->sh_addralign
)
5543 sec
->sh_addralign
= align
;
5545 addr
= 0; /* avoid warning */
5549 if (scope
!= VT_CONST
|| !sym
) {
5550 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
5551 sym
->asm_label
= asm_label
;
5553 /* update symbol definition */
5555 put_extern_sym(sym
, sec
, addr
, size
);
5558 /* put a common area */
5559 put_extern_sym(sym
, NULL
, align
, size
);
5560 /* XXX: find a nicer way */
5561 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
5562 esym
->st_shndx
= SHN_COMMON
;
5567 /* push global reference */
5568 sym
= get_sym_ref(type
, sec
, addr
, size
);
5570 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
5573 /* patch symbol weakness */
5574 if (type
->t
& VT_WEAK
)
5576 #ifdef CONFIG_TCC_BCHECK
5577 /* handles bounds now because the symbol must be defined
5578 before for the relocation */
5579 if (tcc_state
->do_bounds_check
) {
5580 unsigned long *bounds_ptr
;
5582 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
5583 /* then add global bound info */
5584 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(long));
5585 bounds_ptr
[0] = 0; /* relocated */
5586 bounds_ptr
[1] = size
;
5590 if (has_init
|| (type
->t
& VT_VLA
)) {
5591 decl_initializer(type
, sec
, addr
, 1, 0);
5592 /* restore parse state if needed */
5594 tok_str_free(init_str
.str
);
5595 restore_parse_state(&saved_parse_state
);
5597 /* patch flexible array member size back to -1, */
5598 /* for possible subsequent similar declarations */
5600 flexible_array
->type
.ref
->c
= -1;
5605 static void put_func_debug(Sym
*sym
)
5610 /* XXX: we put here a dummy type */
5611 snprintf(buf
, sizeof(buf
), "%s:%c1",
5612 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
5613 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
5614 cur_text_section
, sym
->c
);
5615 /* //gr gdb wants a line at the function */
5616 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
5621 /* parse an old style function declaration list */
5622 /* XXX: check multiple parameter */
5623 static void func_decl_list(Sym
*func_sym
)
5630 /* parse each declaration */
5631 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
&&
5632 tok
!= TOK_ASM1
&& tok
!= TOK_ASM2
&& tok
!= TOK_ASM3
) {
5633 if (!parse_btype(&btype
, &ad
))
5634 expect("declaration list");
5635 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5636 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5638 /* we accept no variable after */
5642 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5643 /* find parameter in function parameter list */
5646 if ((s
->v
& ~SYM_FIELD
) == v
)
5650 tcc_error("declaration for parameter '%s' but no such parameter",
5651 get_tok_str(v
, NULL
));
5653 /* check that no storage specifier except 'register' was given */
5654 if (type
.t
& VT_STORAGE
)
5655 tcc_error("storage class specified for '%s'", get_tok_str(v
, NULL
));
5656 convert_parameter_type(&type
);
5657 /* we can add the type (NOTE: it could be local to the function) */
5659 /* accept other parameters */
5670 /* parse a function defined by symbol 'sym' and generate its code in
5671 'cur_text_section' */
5672 static void gen_function(Sym
*sym
)
5674 int saved_nocode_wanted
= nocode_wanted
;
5676 ind
= cur_text_section
->data_offset
;
5677 /* NOTE: we patch the symbol size later */
5678 put_extern_sym(sym
, cur_text_section
, ind
, 0);
5679 funcname
= get_tok_str(sym
->v
, NULL
);
5681 /* Initialize VLA state */
5682 vla_sp_loc
= &vla_sp_root_loc
;
5683 vla_flags
= VLA_NEED_NEW_FRAME
;
5684 /* put debug symbol */
5685 if (tcc_state
->do_debug
)
5686 put_func_debug(sym
);
5687 /* push a dummy symbol to enable local sym storage */
5688 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
5689 gfunc_prolog(&sym
->type
);
5691 block(NULL
, NULL
, NULL
, NULL
, 0, 0);
5694 cur_text_section
->data_offset
= ind
;
5695 label_pop(&global_label_stack
, NULL
);
5696 /* reset local stack */
5697 scope_stack_bottom
= NULL
;
5698 sym_pop(&local_stack
, NULL
);
5699 /* end of function */
5700 /* patch symbol size */
5701 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
5703 /* patch symbol weakness (this definition overrules any prototype) */
5704 if (sym
->type
.t
& VT_WEAK
)
5706 if (tcc_state
->do_debug
) {
5707 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
5709 /* It's better to crash than to generate wrong code */
5710 cur_text_section
= NULL
;
5711 funcname
= ""; /* for safety */
5712 func_vt
.t
= VT_VOID
; /* for safety */
5713 ind
= 0; /* for safety */
5714 nocode_wanted
= saved_nocode_wanted
;
5717 ST_FUNC
void gen_inline_functions(void)
5720 int *str
, inline_generated
, i
;
5721 struct InlineFunc
*fn
;
5723 /* iterate while inline function are referenced */
5725 inline_generated
= 0;
5726 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5727 fn
= tcc_state
->inline_fns
[i
];
5729 if (sym
&& sym
->c
) {
5730 /* the function was used: generate its code and
5731 convert it to a normal function */
5732 str
= fn
->token_str
;
5735 pstrcpy(file
->filename
, sizeof file
->filename
, fn
->filename
);
5736 sym
->r
= VT_SYM
| VT_CONST
;
5737 sym
->type
.t
&= ~VT_INLINE
;
5741 cur_text_section
= text_section
;
5743 macro_ptr
= NULL
; /* fail safe */
5745 inline_generated
= 1;
5748 if (!inline_generated
)
5751 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5752 fn
= tcc_state
->inline_fns
[i
];
5753 str
= fn
->token_str
;
5756 dynarray_reset(&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
);
5759 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
5760 static int decl0(int l
, int is_for_loop_init
)
5768 if (!parse_btype(&btype
, &ad
)) {
5769 if (is_for_loop_init
)
5771 /* skip redundant ';' */
5772 /* XXX: find more elegant solution */
5777 if (l
== VT_CONST
&&
5778 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5779 /* global asm block */
5783 /* special test for old K&R protos without explicit int
5784 type. Only accepted when defining global data */
5785 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
5789 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5790 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5792 /* we accept no variable after */
5796 while (1) { /* iterate thru each declaration */
5797 char *asm_label
; // associated asm label
5799 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5803 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
5804 printf("type = '%s'\n", buf
);
5807 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5808 if ((type
.t
& VT_STATIC
) && (l
== VT_LOCAL
)) {
5809 tcc_error("function without file scope cannot be static");
5811 /* if old style function prototype, we accept a
5814 if (sym
->c
== FUNC_OLD
)
5815 func_decl_list(sym
);
5819 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5822 asm_label_instr(&astr
);
5823 asm_label
= tcc_strdup(astr
.data
);
5826 /* parse one last attribute list, after asm label */
5827 parse_attribute(&ad
);
5832 #ifdef TCC_TARGET_PE
5834 type
.t
|= VT_IMPORT
;
5836 type
.t
|= VT_EXPORT
;
5840 tcc_error("cannot use local functions");
5841 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
5842 expect("function definition");
5844 /* reject abstract declarators in function definition */
5846 while ((sym
= sym
->next
) != NULL
)
5847 if (!(sym
->v
& ~SYM_FIELD
))
5848 expect("identifier");
5850 /* XXX: cannot do better now: convert extern line to static inline */
5851 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
5852 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5856 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
5859 r
= sym
->type
.ref
->r
;
5862 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
5864 /* use func_call from prototype if not defined */
5865 if (FUNC_CALL(r
) != FUNC_CDECL
5866 && FUNC_CALL(type
.ref
->r
) == FUNC_CDECL
)
5867 FUNC_CALL(type
.ref
->r
) = FUNC_CALL(r
);
5869 /* use export from prototype */
5871 FUNC_EXPORT(type
.ref
->r
) = 1;
5873 /* use static from prototype */
5874 if (sym
->type
.t
& VT_STATIC
)
5875 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5877 if (!is_compatible_types(&sym
->type
, &type
)) {
5879 tcc_error("incompatible types for redefinition of '%s'",
5880 get_tok_str(v
, NULL
));
5882 FUNC_PROTO(type
.ref
->r
) = 0;
5883 /* if symbol is already defined, then put complete type */
5886 /* put function symbol */
5887 sym
= global_identifier_push(v
, type
.t
, 0);
5888 sym
->type
.ref
= type
.ref
;
5891 /* static inline functions are just recorded as a kind
5892 of macro. Their code will be emitted at the end of
5893 the compilation unit only if they are used */
5894 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
5895 (VT_INLINE
| VT_STATIC
)) {
5896 TokenString func_str
;
5898 struct InlineFunc
*fn
;
5899 const char *filename
;
5901 tok_str_new(&func_str
);
5907 tcc_error("unexpected end of file");
5908 tok_str_add_tok(&func_str
);
5913 } else if (t
== '}') {
5915 if (block_level
== 0)
5919 tok_str_add(&func_str
, -1);
5920 tok_str_add(&func_str
, 0);
5921 filename
= file
? file
->filename
: "";
5922 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
5923 strcpy(fn
->filename
, filename
);
5925 fn
->token_str
= func_str
.str
;
5926 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
5929 /* compute text section */
5930 cur_text_section
= ad
.section
;
5931 if (!cur_text_section
)
5932 cur_text_section
= text_section
;
5933 sym
->r
= VT_SYM
| VT_CONST
;
5938 if (btype
.t
& VT_TYPEDEF
) {
5939 /* save typedefed type */
5940 /* XXX: test storage specifiers ? */
5941 sym
= sym_push(v
, &type
, INT_ATTR(&ad
), 0);
5942 sym
->type
.t
|= VT_TYPEDEF
;
5945 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5946 /* external function definition */
5947 /* specific case for func_call attribute */
5949 type
.ref
->r
= INT_ATTR(&ad
);
5950 } else if (!(type
.t
& VT_ARRAY
)) {
5951 /* not lvalue if array */
5952 r
|= lvalue_type(type
.t
);
5954 has_init
= (tok
== '=');
5955 if (has_init
&& (type
.t
& VT_VLA
))
5956 tcc_error("Variable length array cannot be initialized");
5957 if ((btype
.t
& VT_EXTERN
) || ((type
.t
& VT_BTYPE
) == VT_FUNC
) ||
5958 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
5959 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
5960 /* external variable or function */
5961 /* NOTE: as GCC, uninitialized global static
5962 arrays of null size are considered as
5964 sym
= external_sym(v
, &type
, r
, asm_label
);
5966 if (type
.t
& VT_WEAK
)
5969 if (ad
.alias_target
) {
5974 alias_target
= sym_find(ad
.alias_target
);
5975 if (!alias_target
|| !alias_target
->c
)
5976 tcc_error("unsupported forward __alias__ attribute");
5977 esym
= &((Elf32_Sym
*)symtab_section
->data
)[alias_target
->c
];
5978 tsec
.sh_num
= esym
->st_shndx
;
5979 put_extern_sym2(sym
, &tsec
, esym
->st_value
, esym
->st_size
, 0);
5982 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
5983 if (type
.t
& VT_STATIC
)
5989 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, asm_label
, l
);
5993 if (is_for_loop_init
)
6006 ST_FUNC
void decl(int l
)