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
*cur_text_section
; /* current section where function code is generated */
36 ST_DATA Section
*last_text_section
; /* to handle .previous asm directive */
38 #ifdef CONFIG_TCC_BCHECK
39 /* bound check related sections */
40 ST_DATA Section
*bounds_section
; /* contains global data bound description */
41 ST_DATA Section
*lbounds_section
; /* contains local data bound description */
44 ST_DATA Section
*symtab_section
, *strtab_section
;
46 ST_DATA Section
*stab_section
, *stabstr_section
;
47 ST_DATA Sym
*sym_free_first
;
48 ST_DATA
void **sym_pools
;
49 ST_DATA
int nb_sym_pools
;
51 ST_DATA Sym
*global_stack
;
52 ST_DATA Sym
*local_stack
;
53 ST_DATA Sym
*scope_stack_bottom
;
54 ST_DATA Sym
*define_stack
;
55 ST_DATA Sym
*global_label_stack
;
56 ST_DATA Sym
*local_label_stack
;
58 ST_DATA
int vlas_in_scope
; /* number of VLAs that are currently in scope */
59 ST_DATA
int vla_sp_root_loc
; /* vla_sp_loc for SP before any VLAs were pushed */
60 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 SValue __vstack
[1+VSTACK_SIZE
], *vtop
, *pvtop
;
64 ST_DATA
int const_wanted
; /* true if constant wanted */
65 ST_DATA
int nocode_wanted
; /* true if no code generation wanted for an expression */
66 ST_DATA
int global_expr
; /* true if compound literals must be allocated globally (used during initializers parsing */
67 ST_DATA CType func_vt
; /* current function return type (used by return instruction) */
68 ST_DATA
int func_var
; /* true if current function is variadic (used by return instruction) */
70 ST_DATA
int last_line_num
, last_ind
, func_ind
; /* debug last line number and pc */
71 ST_DATA
const char *funcname
;
73 ST_DATA CType char_pointer_type
, func_old_type
, int_type
, size_type
;
75 /* ------------------------------------------------------------------------- */
76 static void gen_cast(CType
*type
);
77 static inline CType
*pointed_type(CType
*type
);
78 static int is_compatible_types(CType
*type1
, CType
*type2
);
79 static int parse_btype(CType
*type
, AttributeDef
*ad
);
80 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
);
81 static void parse_expr_type(CType
*type
);
82 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
, int first
, int size_only
);
83 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
, int case_reg
, int is_expr
);
84 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
, int has_init
, int v
, char *asm_label
, int scope
);
85 static int decl0(int l
, int is_for_loop_init
);
86 static void expr_eq(void);
87 static void unary_type(CType
*type
);
88 static void vla_runtime_type_size(CType
*type
, int *a
);
89 static void vla_sp_restore(void);
90 static void vla_sp_restore_root(void);
91 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
);
92 static void expr_type(CType
*type
);
93 ST_FUNC
void vpush64(int ty
, unsigned long long v
);
94 ST_FUNC
void vpush(CType
*type
);
95 ST_FUNC
int gvtst(int inv
, int t
);
96 ST_FUNC
int is_btype_size(int bt
);
98 ST_INLN
int is_float(int t
)
102 return bt
== VT_LDOUBLE
|| bt
== VT_DOUBLE
|| bt
== VT_FLOAT
|| bt
== VT_QFLOAT
;
105 /* we use our own 'finite' function to avoid potential problems with
106 non standard math libs */
107 /* XXX: endianness dependent */
108 ST_FUNC
int ieee_finite(double d
)
111 memcpy(p
, &d
, sizeof(double));
112 return ((unsigned)((p
[1] | 0x800fffff) + 1)) >> 31;
115 ST_FUNC
void test_lvalue(void)
117 if (!(vtop
->r
& VT_LVAL
))
121 ST_FUNC
void check_vstack(void)
124 tcc_error("internal compiler error: vstack leak (%d)", vtop
- pvtop
);
127 /* ------------------------------------------------------------------------- */
128 /* symbol allocator */
129 static Sym
*__sym_malloc(void)
131 Sym
*sym_pool
, *sym
, *last_sym
;
134 sym_pool
= tcc_malloc(SYM_POOL_NB
* sizeof(Sym
));
135 dynarray_add(&sym_pools
, &nb_sym_pools
, sym_pool
);
137 last_sym
= sym_free_first
;
139 for(i
= 0; i
< SYM_POOL_NB
; i
++) {
140 sym
->next
= last_sym
;
144 sym_free_first
= last_sym
;
148 static inline Sym
*sym_malloc(void)
151 sym
= sym_free_first
;
153 sym
= __sym_malloc();
154 sym_free_first
= sym
->next
;
158 ST_INLN
void sym_free(Sym
*sym
)
160 sym
->next
= sym_free_first
;
161 tcc_free(sym
->asm_label
);
162 sym_free_first
= sym
;
165 /* push, without hashing */
166 ST_FUNC Sym
*sym_push2(Sym
**ps
, int v
, int t
, long c
)
169 if (ps
== &local_stack
) {
170 for (s
= *ps
; s
&& s
!= scope_stack_bottom
; s
= s
->prev
)
171 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
&& s
->v
== v
)
172 tcc_error("incompatible types for redefinition of '%s'",
173 get_tok_str(v
, NULL
));
191 /* find a symbol and return its associated structure. 's' is the top
192 of the symbol stack */
193 ST_FUNC Sym
*sym_find2(Sym
*s
, int v
)
205 /* structure lookup */
206 ST_INLN Sym
*struct_find(int v
)
209 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
211 return table_ident
[v
]->sym_struct
;
214 /* find an identifier */
215 ST_INLN Sym
*sym_find(int v
)
218 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
220 return table_ident
[v
]->sym_identifier
;
223 /* push a given symbol on the symbol stack */
224 ST_FUNC Sym
*sym_push(int v
, CType
*type
, int r
, int c
)
233 s
= sym_push2(ps
, v
, type
->t
, c
);
234 s
->type
.ref
= type
->ref
;
236 /* don't record fields or anonymous symbols */
238 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
239 /* record symbol in token array */
240 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
242 ps
= &ts
->sym_struct
;
244 ps
= &ts
->sym_identifier
;
251 /* push a global identifier */
252 ST_FUNC Sym
*global_identifier_push(int v
, int t
, int c
)
255 s
= sym_push2(&global_stack
, v
, t
, c
);
256 /* don't record anonymous symbol */
257 if (v
< SYM_FIRST_ANOM
) {
258 ps
= &table_ident
[v
- TOK_IDENT
]->sym_identifier
;
259 /* modify the top most local identifier, so that
260 sym_identifier will point to 's' when popped */
262 ps
= &(*ps
)->prev_tok
;
269 /* pop symbols until top reaches 'b' */
270 ST_FUNC
void sym_pop(Sym
**ptop
, Sym
*b
)
280 /* remove symbol in token array */
282 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
283 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
285 ps
= &ts
->sym_struct
;
287 ps
= &ts
->sym_identifier
;
296 static void weaken_symbol(Sym
*sym
)
298 sym
->type
.t
|= VT_WEAK
;
303 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
304 esym_type
= ELFW(ST_TYPE
)(esym
->st_info
);
305 esym
->st_info
= ELFW(ST_INFO
)(STB_WEAK
, esym_type
);
309 static void apply_visibility(Sym
*sym
, CType
*type
)
311 int vis
= sym
->type
.t
& VT_VIS_MASK
;
312 int vis2
= type
->t
& VT_VIS_MASK
;
313 if (vis
== (STV_DEFAULT
<< VT_VIS_SHIFT
))
315 else if (vis2
== (STV_DEFAULT
<< VT_VIS_SHIFT
))
318 vis
= (vis
< vis2
) ? vis
: vis2
;
319 sym
->type
.t
&= ~VT_VIS_MASK
;
325 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
326 vis
>>= VT_VIS_SHIFT
;
327 esym
->st_other
= (esym
->st_other
& ~ELFW(ST_VISIBILITY
)(-1)) | vis
;
331 /* ------------------------------------------------------------------------- */
333 ST_FUNC
void swap(int *p
, int *q
)
341 static void vsetc(CType
*type
, int r
, CValue
*vc
)
345 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
346 tcc_error("memory full (vstack)");
347 /* cannot let cpu flags if other instruction are generated. Also
348 avoid leaving VT_JMP anywhere except on the top of the stack
349 because it would complicate the code generator. */
350 if (vtop
>= vstack
) {
351 v
= vtop
->r
& VT_VALMASK
;
352 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
362 /* push constant of type "type" with useless value */
363 ST_FUNC
void vpush(CType
*type
)
366 vsetc(type
, VT_CONST
, &cval
);
369 /* push integer constant */
370 ST_FUNC
void vpushi(int v
)
374 vsetc(&int_type
, VT_CONST
, &cval
);
377 /* push a pointer sized constant */
378 static void vpushs(addr_t v
)
382 vsetc(&size_type
, VT_CONST
, &cval
);
385 /* push arbitrary 64bit constant */
386 ST_FUNC
void vpush64(int ty
, unsigned long long v
)
393 vsetc(&ctype
, VT_CONST
, &cval
);
396 /* push long long constant */
397 static inline void vpushll(long long v
)
399 vpush64(VT_LLONG
, v
);
402 /* push a symbol value of TYPE */
403 static inline void vpushsym(CType
*type
, Sym
*sym
)
407 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
411 /* Return a static symbol pointing to a section */
412 ST_FUNC Sym
*get_sym_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
418 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
419 sym
->type
.ref
= type
->ref
;
420 sym
->r
= VT_CONST
| VT_SYM
;
421 put_extern_sym(sym
, sec
, offset
, size
);
425 /* push a reference to a section offset by adding a dummy symbol */
426 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
428 vpushsym(type
, get_sym_ref(type
, sec
, offset
, size
));
431 /* define a new external reference to a symbol 'v' of type 'u' */
432 ST_FUNC Sym
*external_global_sym(int v
, CType
*type
, int r
)
438 /* push forward reference */
439 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
440 s
->type
.ref
= type
->ref
;
441 s
->r
= r
| VT_CONST
| VT_SYM
;
446 /* define a new external reference to a symbol 'v' with alternate asm
447 name 'asm_label' of type 'u'. 'asm_label' is equal to NULL if there
448 is no alternate name (most cases) */
449 static Sym
*external_sym(int v
, CType
*type
, int r
, char *asm_label
)
455 /* push forward reference */
456 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
457 s
->asm_label
= asm_label
? tcc_strdup(asm_label
) : 0;
458 s
->type
.t
|= VT_EXTERN
;
459 } else if (s
->type
.ref
== func_old_type
.ref
) {
460 s
->type
.ref
= type
->ref
;
461 s
->r
= r
| VT_CONST
| VT_SYM
;
462 s
->type
.t
|= VT_EXTERN
;
463 } else if (!is_compatible_types(&s
->type
, type
)) {
464 tcc_error("incompatible types for redefinition of '%s'",
465 get_tok_str(v
, NULL
));
467 /* Merge some storage attributes. */
468 if (type
->t
& VT_WEAK
)
471 if (type
->t
& VT_VIS_MASK
)
472 apply_visibility(s
, type
);
477 /* push a reference to global symbol v */
478 ST_FUNC
void vpush_global_sym(CType
*type
, int v
)
480 vpushsym(type
, external_global_sym(v
, type
, 0));
483 ST_FUNC
void vset(CType
*type
, int r
, int v
)
488 vsetc(type
, r
, &cval
);
491 static void vseti(int r
, int v
)
499 ST_FUNC
void vswap(void)
502 /* cannot let cpu flags if other instruction are generated. Also
503 avoid leaving VT_JMP anywhere except on the top of the stack
504 because it would complicate the code generator. */
505 if (vtop
>= vstack
) {
506 int v
= vtop
->r
& VT_VALMASK
;
507 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
514 /* XXX: +2% overall speed possible with optimized memswap
516 * memswap(&vtop[0], &vtop[1], sizeof *vtop);
520 ST_FUNC
void vpushv(SValue
*v
)
522 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
523 tcc_error("memory full (vstack)");
528 static void vdup(void)
533 /* save r to the memory stack, and mark it as being free */
534 ST_FUNC
void save_reg(int r
)
536 int l
, saved
, size
, align
;
540 /* modify all stack values */
543 for(p
=vstack
;p
<=vtop
;p
++) {
544 if ((p
->r
& VT_VALMASK
) == r
||
545 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
546 /* must save value on stack if not already done */
548 /* NOTE: must reload 'r' because r might be equal to r2 */
549 r
= p
->r
& VT_VALMASK
;
550 /* store register in the stack */
552 if ((p
->r
& VT_LVAL
) ||
553 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
554 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
555 type
= &char_pointer_type
;
559 size
= type_size(type
, &align
);
560 loc
= (loc
- size
) & -align
;
562 sv
.r
= VT_LOCAL
| VT_LVAL
;
565 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
566 /* x86 specific: need to pop fp register ST0 if saved */
568 o(0xd8dd); /* fstp %st(0) */
571 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
572 /* special long long case */
573 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
581 /* mark that stack entry as being saved on the stack */
582 if (p
->r
& VT_LVAL
) {
583 /* also clear the bounded flag because the
584 relocation address of the function was stored in
586 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
588 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
596 #ifdef TCC_TARGET_ARM
597 /* find a register of class 'rc2' with at most one reference on stack.
598 * If none, call get_reg(rc) */
599 ST_FUNC
int get_reg_ex(int rc
, int rc2
)
604 for(r
=0;r
<NB_REGS
;r
++) {
605 if (reg_classes
[r
] & rc2
) {
608 for(p
= vstack
; p
<= vtop
; p
++) {
609 if ((p
->r
& VT_VALMASK
) == r
||
610 (p
->r2
& VT_VALMASK
) == r
)
621 /* find a free register of class 'rc'. If none, save one register */
622 ST_FUNC
int get_reg(int rc
)
627 /* find a free register */
628 for(r
=0;r
<NB_REGS
;r
++) {
629 if (reg_classes
[r
] & rc
) {
630 for(p
=vstack
;p
<=vtop
;p
++) {
631 if ((p
->r
& VT_VALMASK
) == r
||
632 (p
->r2
& VT_VALMASK
) == r
)
640 /* no register left : free the first one on the stack (VERY
641 IMPORTANT to start from the bottom to ensure that we don't
642 spill registers used in gen_opi()) */
643 for(p
=vstack
;p
<=vtop
;p
++) {
644 /* look at second register (if long long) */
645 r
= p
->r2
& VT_VALMASK
;
646 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
648 r
= p
->r
& VT_VALMASK
;
649 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
655 /* Should never comes here */
659 /* save registers up to (vtop - n) stack entry */
660 ST_FUNC
void save_regs(int n
)
665 for(p
= vstack
;p
<= p1
; p
++) {
666 r
= p
->r
& VT_VALMASK
;
673 /* move register 's' (of type 't') to 'r', and flush previous value of r to memory
675 static void move_reg(int r
, int s
, int t
)
689 /* get address of vtop (vtop MUST BE an lvalue) */
690 ST_FUNC
void gaddrof(void)
692 if (vtop
->r
& VT_REF
&& !nocode_wanted
)
695 /* tricky: if saved lvalue, then we can go back to lvalue */
696 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
697 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
702 #ifdef CONFIG_TCC_BCHECK
703 /* generate lvalue bound code */
704 static void gbound(void)
709 vtop
->r
&= ~VT_MUSTBOUND
;
710 /* if lvalue, then use checking code before dereferencing */
711 if (vtop
->r
& VT_LVAL
) {
712 /* if not VT_BOUNDED value, then make one */
713 if (!(vtop
->r
& VT_BOUNDED
)) {
714 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
715 /* must save type because we must set it to int to get pointer */
717 vtop
->type
.t
= VT_PTR
;
720 gen_bounded_ptr_add();
721 vtop
->r
|= lval_type
;
724 /* then check for dereferencing */
725 gen_bounded_ptr_deref();
730 /* store vtop a register belonging to class 'rc'. lvalues are
731 converted to values. Cannot be used if cannot be converted to
732 register value (such as structures). */
733 ST_FUNC
int gv(int rc
)
735 int r
, bit_pos
, bit_size
, size
, align
, i
;
738 /* NOTE: get_reg can modify vstack[] */
739 if (vtop
->type
.t
& VT_BITFIELD
) {
742 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
743 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
744 /* remove bit field info to avoid loops */
745 vtop
->type
.t
&= ~VT_BITFIELD
& ((1 << VT_STRUCT_SHIFT
) - 1);
746 /* cast to int to propagate signedness in following ops */
747 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
752 if((vtop
->type
.t
& VT_UNSIGNED
) ||
753 (vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
754 type
.t
|= VT_UNSIGNED
;
756 /* generate shifts */
757 vpushi(bits
- (bit_pos
+ bit_size
));
759 vpushi(bits
- bit_size
);
760 /* NOTE: transformed to SHR if unsigned */
764 if (is_float(vtop
->type
.t
) &&
765 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
768 unsigned long offset
;
769 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
773 /* XXX: unify with initializers handling ? */
774 /* CPUs usually cannot use float constants, so we store them
775 generically in data segment */
776 size
= type_size(&vtop
->type
, &align
);
777 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
778 data_section
->data_offset
= offset
;
779 /* XXX: not portable yet */
780 #if defined(__i386__) || defined(__x86_64__)
781 /* Zero pad x87 tenbyte long doubles */
782 if (size
== LDOUBLE_SIZE
) {
783 vtop
->c
.tab
[2] &= 0xffff;
784 #if LDOUBLE_SIZE == 16
789 ptr
= section_ptr_add(data_section
, size
);
791 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
795 ptr
[i
] = vtop
->c
.tab
[size
-1-i
];
799 ptr
[i
] = vtop
->c
.tab
[i
];
800 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
801 vtop
->r
|= VT_LVAL
| VT_SYM
;
805 #ifdef CONFIG_TCC_BCHECK
806 if (vtop
->r
& VT_MUSTBOUND
)
810 r
= vtop
->r
& VT_VALMASK
;
811 rc2
= (rc
& RC_FLOAT
) ? RC_FLOAT
: RC_INT
;
812 #ifndef TCC_TARGET_ARM64
815 #ifdef TCC_TARGET_X86_64
816 else if (rc
== RC_FRET
)
821 /* need to reload if:
823 - lvalue (need to dereference pointer)
824 - already a register, but not in the right class */
826 || (vtop
->r
& VT_LVAL
)
827 || !(reg_classes
[r
] & rc
)
828 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
829 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
830 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
&& !(reg_classes
[vtop
->r2
] & rc2
))
832 || ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
837 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
838 if (((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
)) {
839 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
841 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
842 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
843 unsigned long long ll
;
845 int r2
, original_type
;
846 original_type
= vtop
->type
.t
;
847 /* two register type load : expand to two words
849 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
850 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
853 vtop
->c
.i
= ll
; /* first word */
855 vtop
->r
= r
; /* save register value */
856 vpushi(ll
>> 32); /* second word */
859 if (r
>= VT_CONST
|| /* XXX: test to VT_CONST incorrect ? */
860 (vtop
->r
& VT_LVAL
)) {
861 /* We do not want to modifier the long long
862 pointer here, so the safest (and less
863 efficient) is to save all the other registers
864 in the stack. XXX: totally inefficient. */
866 /* load from memory */
867 vtop
->type
.t
= load_type
;
870 vtop
[-1].r
= r
; /* save register value */
871 /* increment pointer to get second word */
872 vtop
->type
.t
= addr_type
;
877 vtop
->type
.t
= load_type
;
882 vtop
[-1].r
= r
; /* save register value */
883 vtop
->r
= vtop
[-1].r2
;
885 /* Allocate second register. Here we rely on the fact that
886 get_reg() tries first to free r2 of an SValue. */
890 /* write second register */
892 vtop
->type
.t
= original_type
;
893 } else if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
895 /* lvalue of scalar type : need to use lvalue type
896 because of possible cast */
899 /* compute memory access type */
900 if (vtop
->r
& VT_REF
)
901 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
906 else if (vtop
->r
& VT_LVAL_BYTE
)
908 else if (vtop
->r
& VT_LVAL_SHORT
)
910 if (vtop
->r
& VT_LVAL_UNSIGNED
)
914 /* restore wanted type */
917 /* one register type load */
922 #ifdef TCC_TARGET_C67
923 /* uses register pairs for doubles */
924 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
931 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
932 ST_FUNC
void gv2(int rc1
, int rc2
)
936 /* generate more generic register first. But VT_JMP or VT_CMP
937 values must be generated first in all cases to avoid possible
939 v
= vtop
[0].r
& VT_VALMASK
;
940 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
945 /* test if reload is needed for first register */
946 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
956 /* test if reload is needed for first register */
957 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
963 #ifndef TCC_TARGET_ARM64
964 /* wrapper around RC_FRET to return a register by type */
965 static int rc_fret(int t
)
967 #ifdef TCC_TARGET_X86_64
968 if (t
== VT_LDOUBLE
) {
976 /* wrapper around REG_FRET to return a register by type */
977 static int reg_fret(int t
)
979 #ifdef TCC_TARGET_X86_64
980 if (t
== VT_LDOUBLE
) {
987 /* expand long long on stack in two int registers */
988 static void lexpand(void)
992 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
995 vtop
[0].r
= vtop
[-1].r2
;
996 vtop
[0].r2
= VT_CONST
;
997 vtop
[-1].r2
= VT_CONST
;
998 vtop
[0].type
.t
= VT_INT
| u
;
999 vtop
[-1].type
.t
= VT_INT
| u
;
1002 #ifdef TCC_TARGET_ARM
1003 /* expand long long on stack */
1004 ST_FUNC
void lexpand_nr(void)
1008 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
1010 vtop
->r2
= VT_CONST
;
1011 vtop
->type
.t
= VT_INT
| u
;
1012 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
1013 if (v
== VT_CONST
) {
1014 vtop
[-1].c
.i
= vtop
->c
.i
;
1015 vtop
->c
.i
= vtop
->c
.i
>> 32;
1017 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
1019 vtop
->r
= vtop
[-1].r
;
1020 } else if (v
> VT_CONST
) {
1024 vtop
->r
= vtop
[-1].r2
;
1025 vtop
[-1].r2
= VT_CONST
;
1026 vtop
[-1].type
.t
= VT_INT
| u
;
1030 /* build a long long from two ints */
1031 static void lbuild(int t
)
1033 gv2(RC_INT
, RC_INT
);
1034 vtop
[-1].r2
= vtop
[0].r
;
1035 vtop
[-1].type
.t
= t
;
1039 /* rotate n first stack elements to the bottom
1040 I1 ... In -> I2 ... In I1 [top is right]
1042 ST_FUNC
void vrotb(int n
)
1048 for(i
=-n
+1;i
!=0;i
++)
1049 vtop
[i
] = vtop
[i
+1];
1053 /* rotate the n elements before entry e towards the top
1054 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
1056 ST_FUNC
void vrote(SValue
*e
, int n
)
1062 for(i
= 0;i
< n
- 1; i
++)
1067 /* rotate n first stack elements to the top
1068 I1 ... In -> In I1 ... I(n-1) [top is right]
1070 ST_FUNC
void vrott(int n
)
1075 /* pop stack value */
1076 ST_FUNC
void vpop(void)
1079 v
= vtop
->r
& VT_VALMASK
;
1080 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1081 /* for x86, we need to pop the FP stack */
1082 if (v
== TREG_ST0
&& !nocode_wanted
) {
1083 o(0xd8dd); /* fstp %st(0) */
1086 if (v
== VT_JMP
|| v
== VT_JMPI
) {
1087 /* need to put correct jump if && or || without test */
1093 /* convert stack entry to register and duplicate its value in another
1095 static void gv_dup(void)
1101 if ((t
& VT_BTYPE
) == VT_LLONG
) {
1108 /* stack: H L L1 H1 */
1116 /* duplicate value */
1121 #ifdef TCC_TARGET_X86_64
1122 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1132 load(r1
, &sv
); /* move r to r1 */
1134 /* duplicates value */
1140 /* Generate value test
1142 * Generate a test for any value (jump, comparison and integers) */
1143 ST_FUNC
int gvtst(int inv
, int t
)
1145 int v
= vtop
->r
& VT_VALMASK
;
1146 if (v
!= VT_CMP
&& v
!= VT_JMP
&& v
!= VT_JMPI
) {
1150 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1151 /* constant jmp optimization */
1152 if ((vtop
->c
.i
!= 0) != inv
)
1157 return gtst(inv
, t
);
1160 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
1161 /* generate CPU independent (unsigned) long long operations */
1162 static void gen_opl(int op
)
1164 int t
, a
, b
, op1
, c
, i
;
1166 unsigned short reg_iret
= REG_IRET
;
1167 unsigned short reg_lret
= REG_LRET
;
1173 func
= TOK___divdi3
;
1176 func
= TOK___udivdi3
;
1179 func
= TOK___moddi3
;
1182 func
= TOK___umoddi3
;
1189 /* call generic long long function */
1190 vpush_global_sym(&func_old_type
, func
);
1195 vtop
->r2
= reg_lret
;
1208 /* stack: L1 H1 L2 H2 */
1213 vtop
[-2] = vtop
[-3];
1216 /* stack: H1 H2 L1 L2 */
1222 /* stack: H1 H2 L1 L2 ML MH */
1225 /* stack: ML MH H1 H2 L1 L2 */
1229 /* stack: ML MH H1 L2 H2 L1 */
1234 /* stack: ML MH M1 M2 */
1237 } else if (op
== '+' || op
== '-') {
1238 /* XXX: add non carry method too (for MIPS or alpha) */
1244 /* stack: H1 H2 (L1 op L2) */
1247 gen_op(op1
+ 1); /* TOK_xxxC2 */
1250 /* stack: H1 H2 (L1 op L2) */
1253 /* stack: (L1 op L2) H1 H2 */
1255 /* stack: (L1 op L2) (H1 op H2) */
1263 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1264 t
= vtop
[-1].type
.t
;
1268 /* stack: L H shift */
1270 /* constant: simpler */
1271 /* NOTE: all comments are for SHL. the other cases are
1272 done by swaping words */
1283 if (op
!= TOK_SAR
) {
1316 /* XXX: should provide a faster fallback on x86 ? */
1319 func
= TOK___ashrdi3
;
1322 func
= TOK___lshrdi3
;
1325 func
= TOK___ashldi3
;
1331 /* compare operations */
1337 /* stack: L1 H1 L2 H2 */
1339 vtop
[-1] = vtop
[-2];
1341 /* stack: L1 L2 H1 H2 */
1344 /* when values are equal, we need to compare low words. since
1345 the jump is inverted, we invert the test too. */
1348 else if (op1
== TOK_GT
)
1350 else if (op1
== TOK_ULT
)
1352 else if (op1
== TOK_UGT
)
1357 if (op1
!= TOK_NE
) {
1361 /* generate non equal test */
1362 /* XXX: NOT PORTABLE yet */
1366 #if defined(TCC_TARGET_I386)
1367 b
= psym(0x850f, 0);
1368 #elif defined(TCC_TARGET_ARM)
1370 o(0x1A000000 | encbranch(ind
, 0, 1));
1371 #elif defined(TCC_TARGET_C67) || defined(TCC_TARGET_ARM64)
1372 tcc_error("not implemented");
1374 #error not supported
1378 /* compare low. Always unsigned */
1382 else if (op1
== TOK_LE
)
1384 else if (op1
== TOK_GT
)
1386 else if (op1
== TOK_GE
)
1397 static uint64_t gen_opic_sdiv(uint64_t a
, uint64_t b
)
1399 uint64_t x
= (a
>> 63 ? -a
: a
) / (b
>> 63 ? -b
: b
);
1400 return (a
^ b
) >> 63 ? -x
: x
;
1403 static int gen_opic_lt(uint64_t a
, uint64_t b
)
1405 return (a
^ (uint64_t)1 << 63) < (b
^ (uint64_t)1 << 63);
1408 /* handle integer constant optimizations and various machine
1410 static void gen_opic(int op
)
1412 SValue
*v1
= vtop
- 1;
1414 int t1
= v1
->type
.t
& VT_BTYPE
;
1415 int t2
= v2
->type
.t
& VT_BTYPE
;
1416 int c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1417 int c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1418 uint64_t l1
= c1
? v1
->c
.i
: 0;
1419 uint64_t l2
= c2
? v2
->c
.i
: 0;
1420 int shm
= (t1
== VT_LLONG
) ? 63 : 31;
1423 l1
= ((uint32_t)l1
|
1424 (v1
->type
.t
& VT_UNSIGNED
? 0 : -(l1
& 0x80000000)));
1426 l2
= ((uint32_t)l2
|
1427 (v2
->type
.t
& VT_UNSIGNED
? 0 : -(l2
& 0x80000000)));
1431 case '+': l1
+= l2
; break;
1432 case '-': l1
-= l2
; break;
1433 case '&': l1
&= l2
; break;
1434 case '^': l1
^= l2
; break;
1435 case '|': l1
|= l2
; break;
1436 case '*': l1
*= l2
; break;
1443 /* if division by zero, generate explicit division */
1446 tcc_error("division by zero in constant");
1450 default: l1
= gen_opic_sdiv(l1
, l2
); break;
1451 case '%': l1
= l1
- l2
* gen_opic_sdiv(l1
, l2
); break;
1452 case TOK_UDIV
: l1
= l1
/ l2
; break;
1453 case TOK_UMOD
: l1
= l1
% l2
; break;
1456 case TOK_SHL
: l1
<<= (l2
& shm
); break;
1457 case TOK_SHR
: l1
>>= (l2
& shm
); break;
1459 l1
= (l1
>> 63) ? ~(~l1
>> (l2
& shm
)) : l1
>> (l2
& shm
);
1462 case TOK_ULT
: l1
= l1
< l2
; break;
1463 case TOK_UGE
: l1
= l1
>= l2
; break;
1464 case TOK_EQ
: l1
= l1
== l2
; break;
1465 case TOK_NE
: l1
= l1
!= l2
; break;
1466 case TOK_ULE
: l1
= l1
<= l2
; break;
1467 case TOK_UGT
: l1
= l1
> l2
; break;
1468 case TOK_LT
: l1
= gen_opic_lt(l1
, l2
); break;
1469 case TOK_GE
: l1
= !gen_opic_lt(l1
, l2
); break;
1470 case TOK_LE
: l1
= !gen_opic_lt(l2
, l1
); break;
1471 case TOK_GT
: l1
= gen_opic_lt(l2
, l1
); break;
1473 case TOK_LAND
: l1
= l1
&& l2
; break;
1474 case TOK_LOR
: l1
= l1
|| l2
; break;
1481 /* if commutative ops, put c2 as constant */
1482 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1483 op
== '|' || op
== '*')) {
1485 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1486 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1488 if (!const_wanted
&&
1490 (op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
)) ||
1491 (l1
== -1 && op
== TOK_SAR
))) {
1492 /* treat (0 << x), (0 >> x) and (-1 >> x) as constant */
1494 } else if (!const_wanted
&&
1495 c2
&& ((l2
== 0 && (op
== '&' || op
== '*')) ||
1496 (l2
== -1 && op
== '|') ||
1497 (l2
== 0xffffffff && t2
!= VT_LLONG
&& op
== '|') ||
1498 (l2
== 1 && (op
== '%' || op
== TOK_UMOD
)))) {
1499 /* treat (x & 0), (x * 0), (x | -1) and (x % 1) as constant */
1504 } else if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1507 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1508 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1512 /* filter out NOP operations like x*1, x-0, x&-1... */
1514 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1515 /* try to use shifts instead of muls or divs */
1516 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1525 else if (op
== TOK_PDIV
)
1531 } else if (c2
&& (op
== '+' || op
== '-') &&
1532 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
1533 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1534 /* symbol + constant case */
1541 if (!nocode_wanted
) {
1542 /* call low level op generator */
1543 if (t1
== VT_LLONG
|| t2
== VT_LLONG
||
1544 (PTR_SIZE
== 8 && (t1
== VT_PTR
|| t2
== VT_PTR
)))
1555 /* generate a floating point operation with constant propagation */
1556 static void gen_opif(int op
)
1564 /* currently, we cannot do computations with forward symbols */
1565 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1566 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1568 if (v1
->type
.t
== VT_FLOAT
) {
1571 } else if (v1
->type
.t
== VT_DOUBLE
) {
1579 /* NOTE: we only do constant propagation if finite number (not
1580 NaN or infinity) (ANSI spec) */
1581 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1585 case '+': f1
+= f2
; break;
1586 case '-': f1
-= f2
; break;
1587 case '*': f1
*= f2
; break;
1591 tcc_error("division by zero in constant");
1596 /* XXX: also handles tests ? */
1600 /* XXX: overflow test ? */
1601 if (v1
->type
.t
== VT_FLOAT
) {
1603 } else if (v1
->type
.t
== VT_DOUBLE
) {
1611 if (!nocode_wanted
) {
1619 static int pointed_size(CType
*type
)
1622 return type_size(pointed_type(type
), &align
);
1625 static void vla_runtime_pointed_size(CType
*type
)
1628 vla_runtime_type_size(pointed_type(type
), &align
);
1631 static inline int is_null_pointer(SValue
*p
)
1633 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1635 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& (uint32_t)p
->c
.i
== 0) ||
1636 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.i
== 0) ||
1637 ((p
->type
.t
& VT_BTYPE
) == VT_PTR
&&
1638 (PTR_SIZE
== 4 ? (uint32_t)p
->c
.i
== 0 : p
->c
.i
== 0));
1641 static inline int is_integer_btype(int bt
)
1643 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
1644 bt
== VT_INT
|| bt
== VT_LLONG
);
1647 /* check types for comparison or subtraction of pointers */
1648 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
1650 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
1653 /* null pointers are accepted for all comparisons as gcc */
1654 if (is_null_pointer(p1
) || is_null_pointer(p2
))
1658 bt1
= type1
->t
& VT_BTYPE
;
1659 bt2
= type2
->t
& VT_BTYPE
;
1660 /* accept comparison between pointer and integer with a warning */
1661 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
1662 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
1663 tcc_warning("comparison between pointer and integer");
1667 /* both must be pointers or implicit function pointers */
1668 if (bt1
== VT_PTR
) {
1669 type1
= pointed_type(type1
);
1670 } else if (bt1
!= VT_FUNC
)
1671 goto invalid_operands
;
1673 if (bt2
== VT_PTR
) {
1674 type2
= pointed_type(type2
);
1675 } else if (bt2
!= VT_FUNC
) {
1677 tcc_error("invalid operands to binary %s", get_tok_str(op
, NULL
));
1679 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1680 (type2
->t
& VT_BTYPE
) == VT_VOID
)
1684 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1685 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1686 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1687 /* gcc-like error if '-' is used */
1689 goto invalid_operands
;
1691 tcc_warning("comparison of distinct pointer types lacks a cast");
1695 /* generic gen_op: handles types problems */
1696 ST_FUNC
void gen_op(int op
)
1698 int u
, t1
, t2
, bt1
, bt2
, t
;
1701 t1
= vtop
[-1].type
.t
;
1702 t2
= vtop
[0].type
.t
;
1703 bt1
= t1
& VT_BTYPE
;
1704 bt2
= t2
& VT_BTYPE
;
1706 if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
1707 /* at least one operand is a pointer */
1708 /* relationnal op: must be both pointers */
1709 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
1710 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1711 /* pointers are handled are unsigned */
1712 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1713 t
= VT_LLONG
| VT_UNSIGNED
;
1715 t
= VT_INT
| VT_UNSIGNED
;
1719 /* if both pointers, then it must be the '-' op */
1720 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
1722 tcc_error("cannot use pointers here");
1723 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1724 /* XXX: check that types are compatible */
1725 if (vtop
[-1].type
.t
& VT_VLA
) {
1726 vla_runtime_pointed_size(&vtop
[-1].type
);
1728 vpushi(pointed_size(&vtop
[-1].type
));
1732 /* set to integer type */
1733 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1734 vtop
->type
.t
= VT_LLONG
;
1736 vtop
->type
.t
= VT_INT
;
1741 /* exactly one pointer : must be '+' or '-'. */
1742 if (op
!= '-' && op
!= '+')
1743 tcc_error("cannot use pointers here");
1744 /* Put pointer as first operand */
1745 if (bt2
== VT_PTR
) {
1749 type1
= vtop
[-1].type
;
1750 type1
.t
&= ~VT_ARRAY
;
1751 if (vtop
[-1].type
.t
& VT_VLA
)
1752 vla_runtime_pointed_size(&vtop
[-1].type
);
1754 u
= pointed_size(&vtop
[-1].type
);
1756 tcc_error("unknown array element size");
1757 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1760 /* XXX: cast to int ? (long long case) */
1766 /* #ifdef CONFIG_TCC_BCHECK
1767 The main reason to removing this code:
1774 fprintf(stderr, "v+i-j = %p\n", v+i-j);
1775 fprintf(stderr, "v+(i-j) = %p\n", v+(i-j));
1777 When this code is on. then the output looks like
1779 v+(i-j) = 0xbff84000
1781 /* if evaluating constant expression, no code should be
1782 generated, so no bound check */
1783 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
1784 /* if bounded pointers, we generate a special code to
1791 gen_bounded_ptr_add();
1797 /* put again type if gen_opic() swaped operands */
1800 } else if (is_float(bt1
) || is_float(bt2
)) {
1801 /* compute bigger type and do implicit casts */
1802 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
1804 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
1809 /* floats can only be used for a few operations */
1810 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
1811 (op
< TOK_ULT
|| op
> TOK_GT
))
1812 tcc_error("invalid operands for binary operation");
1814 } else if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
) {
1815 t
= bt1
== VT_LLONG
? VT_LLONG
: VT_INT
;
1816 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (t
| VT_UNSIGNED
))
1819 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
1820 /* cast to biggest op */
1822 /* convert to unsigned if it does not fit in a long long */
1823 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
1824 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
1827 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
1828 tcc_error("comparison of struct");
1830 /* integer operations */
1832 /* convert to unsigned if it does not fit in an integer */
1833 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
1834 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
1837 /* XXX: currently, some unsigned operations are explicit, so
1838 we modify them here */
1839 if (t
& VT_UNSIGNED
) {
1846 else if (op
== TOK_LT
)
1848 else if (op
== TOK_GT
)
1850 else if (op
== TOK_LE
)
1852 else if (op
== TOK_GE
)
1859 /* special case for shifts and long long: we keep the shift as
1861 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
1868 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
1869 /* relationnal op: the result is an int */
1870 vtop
->type
.t
= VT_INT
;
1875 // Make sure that we have converted to an rvalue:
1876 if (vtop
->r
& VT_LVAL
&& !nocode_wanted
)
1877 gv(is_float(vtop
->type
.t
& VT_BTYPE
) ? RC_FLOAT
: RC_INT
);
1880 #ifndef TCC_TARGET_ARM
1881 /* generic itof for unsigned long long case */
1882 static void gen_cvt_itof1(int t
)
1884 #ifdef TCC_TARGET_ARM64
1887 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
1888 (VT_LLONG
| VT_UNSIGNED
)) {
1891 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
1892 #if LDOUBLE_SIZE != 8
1893 else if (t
== VT_LDOUBLE
)
1894 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
1897 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
1901 vtop
->r
= reg_fret(t
);
1909 /* generic ftoi for unsigned long long case */
1910 static void gen_cvt_ftoi1(int t
)
1912 #ifdef TCC_TARGET_ARM64
1917 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
1918 /* not handled natively */
1919 st
= vtop
->type
.t
& VT_BTYPE
;
1921 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
1922 #if LDOUBLE_SIZE != 8
1923 else if (st
== VT_LDOUBLE
)
1924 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
1927 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
1932 vtop
->r2
= REG_LRET
;
1939 /* force char or short cast */
1940 static void force_charshort_cast(int t
)
1944 /* XXX: add optimization if lvalue : just change type and offset */
1949 if (t
& VT_UNSIGNED
) {
1950 vpushi((1 << bits
) - 1);
1956 /* result must be signed or the SAR is converted to an SHL
1957 This was not the case when "t" was a signed short
1958 and the last value on the stack was an unsigned int */
1959 vtop
->type
.t
&= ~VT_UNSIGNED
;
1965 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1966 static void gen_cast(CType
*type
)
1968 int sbt
, dbt
, sf
, df
, c
, p
;
1970 /* special delayed cast for char/short */
1971 /* XXX: in some cases (multiple cascaded casts), it may still
1973 if (vtop
->r
& VT_MUSTCAST
) {
1974 vtop
->r
&= ~VT_MUSTCAST
;
1975 force_charshort_cast(vtop
->type
.t
);
1978 /* bitfields first get cast to ints */
1979 if (vtop
->type
.t
& VT_BITFIELD
&& !nocode_wanted
) {
1983 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
1984 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
1989 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1990 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
1992 /* constant case: we can do it now */
1993 /* XXX: in ISOC, cannot do it if error in convert */
1994 if (sbt
== VT_FLOAT
)
1995 vtop
->c
.ld
= vtop
->c
.f
;
1996 else if (sbt
== VT_DOUBLE
)
1997 vtop
->c
.ld
= vtop
->c
.d
;
2000 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
2001 if ((sbt
& VT_UNSIGNED
) || !(vtop
->c
.i
>> 63))
2002 vtop
->c
.ld
= vtop
->c
.i
;
2004 vtop
->c
.ld
= -(long double)-vtop
->c
.i
;
2006 if ((sbt
& VT_UNSIGNED
) || !(vtop
->c
.i
>> 31))
2007 vtop
->c
.ld
= (uint32_t)vtop
->c
.i
;
2009 vtop
->c
.ld
= -(long double)-(uint32_t)vtop
->c
.i
;
2012 if (dbt
== VT_FLOAT
)
2013 vtop
->c
.f
= (float)vtop
->c
.ld
;
2014 else if (dbt
== VT_DOUBLE
)
2015 vtop
->c
.d
= (double)vtop
->c
.ld
;
2016 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
2017 vtop
->c
.i
= vtop
->c
.ld
;
2018 } else if (sf
&& dbt
== VT_BOOL
) {
2019 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
2022 vtop
->c
.i
= vtop
->c
.ld
;
2023 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
2025 else if (sbt
& VT_UNSIGNED
)
2026 vtop
->c
.i
= (uint32_t)vtop
->c
.i
;
2027 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2028 else if (sbt
== VT_PTR
)
2031 else if (sbt
!= VT_LLONG
)
2032 vtop
->c
.i
= ((uint32_t)vtop
->c
.i
|
2033 -(vtop
->c
.i
& 0x80000000));
2035 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
2037 else if (dbt
== VT_BOOL
)
2038 vtop
->c
.i
= (vtop
->c
.i
!= 0);
2039 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2040 else if (dbt
== VT_PTR
)
2043 else if (dbt
!= VT_LLONG
) {
2044 uint32_t m
= ((dbt
& VT_BTYPE
) == VT_BYTE
? 0xff :
2045 (dbt
& VT_BTYPE
) == VT_SHORT
? 0xffff :
2048 if (!(dbt
& VT_UNSIGNED
))
2049 vtop
->c
.i
|= -(vtop
->c
.i
& ((m
>> 1) + 1));
2052 } else if (p
&& dbt
== VT_BOOL
) {
2055 } else if (!nocode_wanted
) {
2056 /* non constant case: generate code */
2058 /* convert from fp to fp */
2061 /* convert int to fp */
2064 /* convert fp to int */
2065 if (dbt
== VT_BOOL
) {
2069 /* we handle char/short/etc... with generic code */
2070 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
2071 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
2075 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
2076 /* additional cast for char/short... */
2081 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
2082 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
2083 if ((sbt
& VT_BTYPE
) != VT_LLONG
&& !nocode_wanted
) {
2084 /* scalar to long long */
2085 /* machine independent conversion */
2087 /* generate high word */
2088 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
2092 if (sbt
== VT_PTR
) {
2093 /* cast from pointer to int before we apply
2094 shift operation, which pointers don't support*/
2095 gen_cast(&int_type
);
2101 /* patch second register */
2102 vtop
[-1].r2
= vtop
->r
;
2106 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
2107 (dbt
& VT_BTYPE
) == VT_PTR
||
2108 (dbt
& VT_BTYPE
) == VT_FUNC
) {
2109 if ((sbt
& VT_BTYPE
) != VT_LLONG
&&
2110 (sbt
& VT_BTYPE
) != VT_PTR
&&
2111 (sbt
& VT_BTYPE
) != VT_FUNC
&& !nocode_wanted
) {
2112 /* need to convert from 32bit to 64bit */
2114 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
2115 #if defined(TCC_TARGET_ARM64)
2117 #elif defined(TCC_TARGET_X86_64)
2119 /* x86_64 specific: movslq */
2121 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
2128 } else if (dbt
== VT_BOOL
) {
2129 /* scalar to bool */
2132 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
2133 (dbt
& VT_BTYPE
) == VT_SHORT
) {
2134 if (sbt
== VT_PTR
) {
2135 vtop
->type
.t
= VT_INT
;
2136 tcc_warning("nonportable conversion from pointer to char/short");
2138 force_charshort_cast(dbt
);
2139 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
2141 if (sbt
== VT_LLONG
&& !nocode_wanted
) {
2142 /* from long long: just take low order word */
2146 /* if lvalue and single word type, nothing to do because
2147 the lvalue already contains the real type size (see
2148 VT_LVAL_xxx constants) */
2151 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
2152 /* if we are casting between pointer types,
2153 we must update the VT_LVAL_xxx size */
2154 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
2155 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
2160 /* return type size as known at compile time. Put alignment at 'a' */
2161 ST_FUNC
int type_size(CType
*type
, int *a
)
2166 bt
= type
->t
& VT_BTYPE
;
2167 if (bt
== VT_STRUCT
) {
2172 } else if (bt
== VT_PTR
) {
2173 if (type
->t
& VT_ARRAY
) {
2177 ts
= type_size(&s
->type
, a
);
2179 if (ts
< 0 && s
->c
< 0)
2187 } else if (bt
== VT_LDOUBLE
) {
2189 return LDOUBLE_SIZE
;
2190 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
2191 #ifdef TCC_TARGET_I386
2192 #ifdef TCC_TARGET_PE
2197 #elif defined(TCC_TARGET_ARM)
2207 } else if (bt
== VT_INT
|| bt
== VT_ENUM
|| bt
== VT_FLOAT
) {
2210 } else if (bt
== VT_SHORT
) {
2213 } else if (bt
== VT_QLONG
|| bt
== VT_QFLOAT
) {
2217 /* char, void, function, _Bool */
2223 /* push type size as known at runtime time on top of value stack. Put
2225 ST_FUNC
void vla_runtime_type_size(CType
*type
, int *a
)
2227 if (type
->t
& VT_VLA
) {
2228 vset(&int_type
, VT_LOCAL
|VT_LVAL
, type
->ref
->c
);
2230 vpushi(type_size(type
, a
));
2234 static void vla_sp_restore(void) {
2235 if (vlas_in_scope
) {
2236 gen_vla_sp_restore(vla_sp_loc
);
2240 static void vla_sp_restore_root(void) {
2241 if (vlas_in_scope
) {
2242 gen_vla_sp_restore(vla_sp_root_loc
);
2246 /* return the pointed type of t */
2247 static inline CType
*pointed_type(CType
*type
)
2249 return &type
->ref
->type
;
2252 /* modify type so that its it is a pointer to type. */
2253 ST_FUNC
void mk_pointer(CType
*type
)
2256 s
= sym_push(SYM_FIELD
, type
, 0, -1);
2257 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
2261 /* compare function types. OLD functions match any new functions */
2262 static int is_compatible_func(CType
*type1
, CType
*type2
)
2268 if (!is_compatible_types(&s1
->type
, &s2
->type
))
2270 /* check func_call */
2271 if (s1
->a
.func_call
!= s2
->a
.func_call
)
2273 /* XXX: not complete */
2274 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
2278 while (s1
!= NULL
) {
2281 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
2291 /* return true if type1 and type2 are the same. If unqualified is
2292 true, qualifiers on the types are ignored.
2294 - enums are not checked as gcc __builtin_types_compatible_p ()
2296 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2300 t1
= type1
->t
& VT_TYPE
;
2301 t2
= type2
->t
& VT_TYPE
;
2303 /* strip qualifiers before comparing */
2304 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2305 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2307 /* Default Vs explicit signedness only matters for char */
2308 if ((t1
& VT_BTYPE
) != VT_BYTE
) {
2312 /* XXX: bitfields ? */
2315 /* test more complicated cases */
2316 bt1
= t1
& VT_BTYPE
;
2317 if (bt1
== VT_PTR
) {
2318 type1
= pointed_type(type1
);
2319 type2
= pointed_type(type2
);
2320 return is_compatible_types(type1
, type2
);
2321 } else if (bt1
== VT_STRUCT
) {
2322 return (type1
->ref
== type2
->ref
);
2323 } else if (bt1
== VT_FUNC
) {
2324 return is_compatible_func(type1
, type2
);
2330 /* return true if type1 and type2 are exactly the same (including
2333 static int is_compatible_types(CType
*type1
, CType
*type2
)
2335 return compare_types(type1
,type2
,0);
2338 /* return true if type1 and type2 are the same (ignoring qualifiers).
2340 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
2342 return compare_types(type1
,type2
,1);
2345 /* print a type. If 'varstr' is not NULL, then the variable is also
2346 printed in the type */
2348 /* XXX: add array and function pointers */
2349 static void type_to_str(char *buf
, int buf_size
,
2350 CType
*type
, const char *varstr
)
2357 t
= type
->t
& VT_TYPE
;
2360 if (t
& VT_CONSTANT
)
2361 pstrcat(buf
, buf_size
, "const ");
2362 if (t
& VT_VOLATILE
)
2363 pstrcat(buf
, buf_size
, "volatile ");
2364 if ((t
& (VT_DEFSIGN
| VT_UNSIGNED
)) == (VT_DEFSIGN
| VT_UNSIGNED
))
2365 pstrcat(buf
, buf_size
, "unsigned ");
2366 else if (t
& VT_DEFSIGN
)
2367 pstrcat(buf
, buf_size
, "signed ");
2397 tstr
= "long double";
2399 pstrcat(buf
, buf_size
, tstr
);
2403 if (bt
== VT_STRUCT
)
2407 pstrcat(buf
, buf_size
, tstr
);
2408 v
= type
->ref
->v
& ~SYM_STRUCT
;
2409 if (v
>= SYM_FIRST_ANOM
)
2410 pstrcat(buf
, buf_size
, "<anonymous>");
2412 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
2416 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
2417 pstrcat(buf
, buf_size
, "(");
2419 while (sa
!= NULL
) {
2420 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
2421 pstrcat(buf
, buf_size
, buf1
);
2424 pstrcat(buf
, buf_size
, ", ");
2426 pstrcat(buf
, buf_size
, ")");
2431 snprintf(buf1
, sizeof(buf1
), "%s[%ld]", varstr
? varstr
: "", s
->c
);
2432 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2435 pstrcpy(buf1
, sizeof(buf1
), "*");
2436 if (t
& VT_CONSTANT
)
2437 pstrcat(buf1
, buf_size
, "const ");
2438 if (t
& VT_VOLATILE
)
2439 pstrcat(buf1
, buf_size
, "volatile ");
2441 pstrcat(buf1
, sizeof(buf1
), varstr
);
2442 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2446 pstrcat(buf
, buf_size
, " ");
2447 pstrcat(buf
, buf_size
, varstr
);
2452 /* verify type compatibility to store vtop in 'dt' type, and generate
2454 static void gen_assign_cast(CType
*dt
)
2456 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
2457 char buf1
[256], buf2
[256];
2460 st
= &vtop
->type
; /* source type */
2461 dbt
= dt
->t
& VT_BTYPE
;
2462 sbt
= st
->t
& VT_BTYPE
;
2463 if (sbt
== VT_VOID
|| dbt
== VT_VOID
) {
2464 if (sbt
== VT_VOID
&& dbt
== VT_VOID
)
2466 It is Ok if both are void
2472 gcc accepts this program
2475 tcc_error("cannot cast from/to void");
2477 if (dt
->t
& VT_CONSTANT
)
2478 tcc_warning("assignment of read-only location");
2481 /* special cases for pointers */
2482 /* '0' can also be a pointer */
2483 if (is_null_pointer(vtop
))
2485 /* accept implicit pointer to integer cast with warning */
2486 if (is_integer_btype(sbt
)) {
2487 tcc_warning("assignment makes pointer from integer without a cast");
2490 type1
= pointed_type(dt
);
2491 /* a function is implicitely a function pointer */
2492 if (sbt
== VT_FUNC
) {
2493 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
2494 !is_compatible_types(pointed_type(dt
), st
))
2495 tcc_warning("assignment from incompatible pointer type");
2500 type2
= pointed_type(st
);
2501 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2502 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
2503 /* void * can match anything */
2505 /* exact type match, except for unsigned */
2508 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
|
2510 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
|
2512 if (!is_compatible_types(&tmp_type1
, &tmp_type2
))
2513 tcc_warning("assignment from incompatible pointer type");
2515 /* check const and volatile */
2516 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
2517 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
2518 tcc_warning("assignment discards qualifiers from pointer target type");
2524 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
2525 tcc_warning("assignment makes integer from pointer without a cast");
2527 /* XXX: more tests */
2532 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2533 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2534 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2536 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
2537 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
2538 tcc_error("cannot cast '%s' to '%s'", buf1
, buf2
);
2546 /* store vtop in lvalue pushed on stack */
2547 ST_FUNC
void vstore(void)
2549 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
2551 ft
= vtop
[-1].type
.t
;
2552 sbt
= vtop
->type
.t
& VT_BTYPE
;
2553 dbt
= ft
& VT_BTYPE
;
2554 if ((((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2555 (sbt
== VT_INT
&& dbt
== VT_SHORT
))
2556 && !(vtop
->type
.t
& VT_BITFIELD
)) {
2557 /* optimize char/short casts */
2558 delayed_cast
= VT_MUSTCAST
;
2559 vtop
->type
.t
= (ft
& VT_TYPE
& ~VT_BITFIELD
&
2560 ((1 << VT_STRUCT_SHIFT
) - 1));
2561 /* XXX: factorize */
2562 if (ft
& VT_CONSTANT
)
2563 tcc_warning("assignment of read-only location");
2566 if (!(ft
& VT_BITFIELD
))
2567 gen_assign_cast(&vtop
[-1].type
);
2570 if (sbt
== VT_STRUCT
) {
2571 /* if structure, only generate pointer */
2572 /* structure assignment : generate memcpy */
2573 /* XXX: optimize if small size */
2574 if (!nocode_wanted
) {
2575 size
= type_size(&vtop
->type
, &align
);
2579 vtop
->type
.t
= VT_PTR
;
2582 /* address of memcpy() */
2585 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2586 else if(!(align
& 3))
2587 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2590 /* Use memmove, rather than memcpy, as dest and src may be same: */
2591 vpush_global_sym(&func_old_type
, TOK_memmove
);
2596 vtop
->type
.t
= VT_PTR
;
2605 /* leave source on stack */
2606 } else if (ft
& VT_BITFIELD
) {
2607 /* bitfield store handling */
2609 /* save lvalue as expression result (example: s.b = s.a = n;) */
2610 vdup(), vtop
[-1] = vtop
[-2];
2612 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2613 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2614 /* remove bit field info to avoid loops */
2615 vtop
[-1].type
.t
= ft
& ~VT_BITFIELD
& ((1 << VT_STRUCT_SHIFT
) - 1);
2617 if((ft
& VT_BTYPE
) == VT_BOOL
) {
2618 gen_cast(&vtop
[-1].type
);
2619 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2622 /* duplicate destination */
2624 vtop
[-1] = vtop
[-2];
2626 /* mask and shift source */
2627 if((ft
& VT_BTYPE
) != VT_BOOL
) {
2628 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2629 vpushll((1ULL << bit_size
) - 1ULL);
2631 vpushi((1 << bit_size
) - 1);
2637 /* load destination, mask and or with source */
2639 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2640 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2642 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2648 /* ... and discard */
2652 if (!nocode_wanted
) {
2653 #ifdef CONFIG_TCC_BCHECK
2654 /* bound check case */
2655 if (vtop
[-1].r
& VT_MUSTBOUND
) {
2664 #ifdef TCC_TARGET_X86_64
2665 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
2667 } else if ((ft
& VT_BTYPE
) == VT_QFLOAT
) {
2672 r
= gv(rc
); /* generate value */
2673 /* if lvalue was saved on stack, must read it */
2674 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
2676 t
= get_reg(RC_INT
);
2677 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2682 sv
.r
= VT_LOCAL
| VT_LVAL
;
2683 sv
.c
.i
= vtop
[-1].c
.i
;
2685 vtop
[-1].r
= t
| VT_LVAL
;
2687 /* two word case handling : store second register at word + 4 (or +8 for x86-64) */
2688 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2689 if (((ft
& VT_BTYPE
) == VT_QLONG
) || ((ft
& VT_BTYPE
) == VT_QFLOAT
)) {
2690 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
2692 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
2693 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
2695 vtop
[-1].type
.t
= load_type
;
2698 /* convert to int to increment easily */
2699 vtop
->type
.t
= addr_type
;
2705 vtop
[-1].type
.t
= load_type
;
2706 /* XXX: it works because r2 is spilled last ! */
2707 store(vtop
->r2
, vtop
- 1);
2713 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
2714 vtop
->r
|= delayed_cast
;
2718 /* post defines POST/PRE add. c is the token ++ or -- */
2719 ST_FUNC
void inc(int post
, int c
)
2722 vdup(); /* save lvalue */
2725 gv_dup(); /* duplicate value */
2727 vdup(); /* duplicate value */
2732 vpushi(c
- TOK_MID
);
2734 vstore(); /* store value */
2736 vpop(); /* if post op, return saved value */
2739 /* Parse GNUC __attribute__ extension. Currently, the following
2740 extensions are recognized:
2741 - aligned(n) : set data/function alignment.
2742 - packed : force data alignment to 1
2743 - section(x) : generate data/code in this section.
2744 - unused : currently ignored, but may be used someday.
2745 - regparm(n) : pass function parameters in registers (i386 only)
2747 static void parse_attribute(AttributeDef
*ad
)
2751 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
2755 while (tok
!= ')') {
2756 if (tok
< TOK_IDENT
)
2757 expect("attribute name");
2765 expect("section name");
2766 ad
->section
= find_section(tcc_state
, (char *)tokc
.cstr
->data
);
2774 expect("alias(\"target\")");
2775 ad
->alias_target
= /* save string as token, for later */
2776 tok_alloc((char*)tokc
.cstr
->data
, tokc
.cstr
->size
-1)->tok
;
2780 case TOK_VISIBILITY1
:
2781 case TOK_VISIBILITY2
:
2784 expect("visibility(\"default|hidden|internal|protected\")");
2785 if (!strcmp (tokc
.cstr
->data
, "default"))
2786 ad
->a
.visibility
= STV_DEFAULT
;
2787 else if (!strcmp (tokc
.cstr
->data
, "hidden"))
2788 ad
->a
.visibility
= STV_HIDDEN
;
2789 else if (!strcmp (tokc
.cstr
->data
, "internal"))
2790 ad
->a
.visibility
= STV_INTERNAL
;
2791 else if (!strcmp (tokc
.cstr
->data
, "protected"))
2792 ad
->a
.visibility
= STV_PROTECTED
;
2794 expect("visibility(\"default|hidden|internal|protected\")");
2803 if (n
<= 0 || (n
& (n
- 1)) != 0)
2804 tcc_error("alignment must be a positive power of two");
2821 /* currently, no need to handle it because tcc does not
2822 track unused objects */
2826 /* currently, no need to handle it because tcc does not
2827 track unused objects */
2832 ad
->a
.func_call
= FUNC_CDECL
;
2837 ad
->a
.func_call
= FUNC_STDCALL
;
2839 #ifdef TCC_TARGET_I386
2849 ad
->a
.func_call
= FUNC_FASTCALL1
+ n
- 1;
2855 ad
->a
.func_call
= FUNC_FASTCALLW
;
2862 ad
->a
.mode
= VT_LLONG
+ 1;
2865 ad
->a
.mode
= VT_SHORT
+ 1;
2868 ad
->a
.mode
= VT_INT
+ 1;
2871 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
2878 ad
->a
.func_export
= 1;
2881 ad
->a
.func_import
= 1;
2884 if (tcc_state
->warn_unsupported
)
2885 tcc_warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
2886 /* skip parameters */
2888 int parenthesis
= 0;
2892 else if (tok
== ')')
2895 } while (parenthesis
&& tok
!= -1);
2908 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2909 static void struct_decl(CType
*type
, int u
, int tdef
)
2911 int a
, v
, size
, align
, maxalign
, c
, offset
, flexible
;
2912 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
, prevbt
;
2913 Sym
*s
, *ss
, *ass
, **ps
;
2917 a
= tok
; /* save decl type */
2922 /* struct already defined ? return it */
2924 expect("struct/union/enum name");
2928 tcc_error("invalid type");
2930 } else if (tok
>= TOK_IDENT
&& !tdef
)
2931 tcc_error("unknown struct/union/enum");
2937 /* we put an undefined size for struct/union */
2938 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
2939 s
->r
= 0; /* default alignment is zero as gcc */
2940 /* put struct/union/enum name in type */
2948 tcc_error("struct/union/enum already defined");
2949 /* cannot be empty */
2951 /* non empty enums are not allowed */
2952 if (a
== TOK_ENUM
) {
2956 expect("identifier");
2958 if (ss
&& !local_stack
)
2959 tcc_error("redefinition of enumerator '%s'",
2960 get_tok_str(v
, NULL
));
2966 /* enum symbols have static storage */
2967 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
2968 ss
->type
.t
|= VT_STATIC
;
2973 /* NOTE: we accept a trailing comma */
2977 s
->c
= type_size(&int_type
, &align
);
2986 while (tok
!= '}') {
2987 parse_btype(&btype
, &ad
);
2990 tcc_error("flexible array member '%s' not at the end of struct",
2991 get_tok_str(v
, NULL
));
2996 type_decl(&type1
, &ad
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
2998 if ((type1
.t
& VT_BTYPE
) != VT_STRUCT
)
2999 expect("identifier");
3001 int v
= btype
.ref
->v
;
3002 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
3003 if (tcc_state
->ms_extensions
== 0)
3004 expect("identifier");
3008 if (type_size(&type1
, &align
) < 0) {
3009 if ((a
== TOK_STRUCT
) && (type1
.t
& VT_ARRAY
) && c
)
3012 tcc_error("field '%s' has incomplete type",
3013 get_tok_str(v
, NULL
));
3015 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
3016 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
3017 tcc_error("invalid type for '%s'",
3018 get_tok_str(v
, NULL
));
3022 bit_size
= expr_const();
3023 /* XXX: handle v = 0 case for messages */
3025 tcc_error("negative width in bit-field '%s'",
3026 get_tok_str(v
, NULL
));
3027 if (v
&& bit_size
== 0)
3028 tcc_error("zero width for bit-field '%s'",
3029 get_tok_str(v
, NULL
));
3031 size
= type_size(&type1
, &align
);
3033 if (align
< ad
.a
.aligned
)
3034 align
= ad
.a
.aligned
;
3035 } else if (ad
.a
.packed
) {
3037 } else if (*tcc_state
->pack_stack_ptr
) {
3038 if (align
> *tcc_state
->pack_stack_ptr
)
3039 align
= *tcc_state
->pack_stack_ptr
;
3042 if (bit_size
>= 0) {
3043 bt
= type1
.t
& VT_BTYPE
;
3050 tcc_error("bitfields must have scalar type");
3052 if (bit_size
> bsize
) {
3053 tcc_error("width of '%s' exceeds its type",
3054 get_tok_str(v
, NULL
));
3055 } else if (bit_size
== bsize
) {
3056 /* no need for bit fields */
3058 } else if (bit_size
== 0) {
3059 /* XXX: what to do if only padding in a
3061 /* zero size: means to pad */
3064 /* we do not have enough room ?
3065 did the type change?
3067 if ((bit_pos
+ bit_size
) > bsize
||
3068 bt
!= prevbt
|| a
== TOK_UNION
)
3071 /* XXX: handle LSB first */
3072 type1
.t
|= VT_BITFIELD
|
3073 (bit_pos
<< VT_STRUCT_SHIFT
) |
3074 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
3075 bit_pos
+= bit_size
;
3081 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
3082 /* add new memory data only if starting
3084 if (lbit_pos
== 0) {
3085 if (a
== TOK_STRUCT
) {
3086 c
= (c
+ align
- 1) & -align
;
3095 if (align
> maxalign
)
3099 printf("add field %s offset=%d",
3100 get_tok_str(v
, NULL
), offset
);
3101 if (type1
.t
& VT_BITFIELD
) {
3102 printf(" pos=%d size=%d",
3103 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
3104 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
3109 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
3111 while ((ass
= ass
->next
) != NULL
) {
3112 ss
= sym_push(ass
->v
, &ass
->type
, 0, offset
+ ass
->c
);
3117 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
3121 if (tok
== ';' || tok
== TOK_EOF
)
3128 /* store size and alignment */
3129 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
3135 /* return 1 if basic type is a type size (short, long, long long) */
3136 ST_FUNC
int is_btype_size(int bt
)
3138 return bt
== VT_SHORT
|| bt
== VT_LONG
|| bt
== VT_LLONG
;
3141 /* return 0 if no type declaration. otherwise, return the basic type
3144 static int parse_btype(CType
*type
, AttributeDef
*ad
)
3146 int t
, u
, bt_size
, complete
, type_found
, typespec_found
;
3150 memset(ad
, 0, sizeof(AttributeDef
));
3158 /* currently, we really ignore extension */
3169 tcc_error("too many basic types");
3171 bt_size
= is_btype_size (u
& VT_BTYPE
);
3172 if (u
== VT_INT
|| (!bt_size
&& !(t
& VT_TYPEDEF
)))
3187 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
3188 #ifndef TCC_TARGET_PE
3189 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3191 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
3192 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3198 #ifdef TCC_TARGET_ARM64
3200 /* GCC's __uint128_t appears in some Linux header files. Make it a
3201 synonym for long double to get the size and alignment right. */
3213 if ((t
& VT_BTYPE
) == VT_LONG
) {
3214 #ifdef TCC_TARGET_PE
3215 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
3217 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3225 struct_decl(&type1
, VT_ENUM
, t
& (VT_TYPEDEF
| VT_EXTERN
));
3228 type
->ref
= type1
.ref
;
3232 struct_decl(&type1
, VT_STRUCT
, t
& (VT_TYPEDEF
| VT_EXTERN
));
3235 /* type modifiers */
3251 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == (VT_DEFSIGN
|VT_UNSIGNED
))
3252 tcc_error("signed and unsigned modifier");
3265 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == VT_DEFSIGN
)
3266 tcc_error("signed and unsigned modifier");
3267 t
|= VT_DEFSIGN
| VT_UNSIGNED
;
3292 /* GNUC attribute */
3293 case TOK_ATTRIBUTE1
:
3294 case TOK_ATTRIBUTE2
:
3295 parse_attribute(ad
);
3298 t
= (t
& ~VT_BTYPE
) | u
;
3306 parse_expr_type(&type1
);
3307 /* remove all storage modifiers except typedef */
3308 type1
.t
&= ~(VT_STORAGE
&~VT_TYPEDEF
);
3314 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
3316 t
|= (s
->type
.t
& ~VT_TYPEDEF
);
3317 if ((t
& VT_ARRAY
) &&
3318 t
& (VT_CONSTANT
| VT_VOLATILE
) & ~s
->type
.ref
->type
.t
) {
3319 /* This is a case like "typedef int T[1]; const T x;"
3320 in which which we must make a copy of the typedef
3321 type so that we can add the type qualifiers to it. */
3322 type
->ref
= sym_push(SYM_FIELD
, &s
->type
.ref
->type
,
3324 type
->ref
->type
.t
|= t
& (VT_CONSTANT
| VT_VOLATILE
);
3327 type
->ref
= s
->type
.ref
;
3329 /* get attributes from typedef */
3330 if (0 == ad
->a
.aligned
)
3331 ad
->a
.aligned
= s
->a
.aligned
;
3332 if (0 == ad
->a
.func_call
)
3333 ad
->a
.func_call
= s
->a
.func_call
;
3334 ad
->a
.packed
|= s
->a
.packed
;
3343 if (tcc_state
->char_is_unsigned
) {
3344 if ((t
& (VT_DEFSIGN
|VT_BTYPE
)) == VT_BYTE
)
3348 /* long is never used as type */
3349 if ((t
& VT_BTYPE
) == VT_LONG
)
3350 #if (!defined TCC_TARGET_X86_64 && !defined TCC_TARGET_ARM64) || \
3351 defined TCC_TARGET_PE
3352 t
= (t
& ~VT_BTYPE
) | VT_INT
;
3354 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3360 /* convert a function parameter type (array to pointer and function to
3361 function pointer) */
3362 static inline void convert_parameter_type(CType
*pt
)
3364 /* remove const and volatile qualifiers (XXX: const could be used
3365 to indicate a const function parameter */
3366 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3367 /* array must be transformed to pointer according to ANSI C */
3369 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
3374 ST_FUNC
void parse_asm_str(CString
*astr
)
3377 /* read the string */
3379 expect("string constant");
3381 while (tok
== TOK_STR
) {
3382 /* XXX: add \0 handling too ? */
3383 cstr_cat(astr
, tokc
.cstr
->data
);
3386 cstr_ccat(astr
, '\0');
3389 /* Parse an asm label and return the label
3390 * Don't forget to free the CString in the caller! */
3391 static void asm_label_instr(CString
*astr
)
3394 parse_asm_str(astr
);
3397 printf("asm_alias: \"%s\"\n", (char *)astr
->data
);
3401 static void post_type(CType
*type
, AttributeDef
*ad
)
3403 int n
, l
, t1
, arg_size
, align
;
3404 Sym
**plast
, *s
, *first
;
3409 /* function declaration */
3417 /* read param name and compute offset */
3418 if (l
!= FUNC_OLD
) {
3419 if (!parse_btype(&pt
, &ad1
)) {
3421 tcc_error("invalid type");
3428 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
3430 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3431 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
3432 tcc_error("parameter declared as void");
3433 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
3438 expect("identifier");
3442 convert_parameter_type(&pt
);
3443 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
3449 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
3456 /* if no parameters, then old type prototype */
3460 /* NOTE: const is ignored in returned type as it has a special
3461 meaning in gcc / C++ */
3462 type
->t
&= ~VT_CONSTANT
;
3463 /* some ancient pre-K&R C allows a function to return an array
3464 and the array brackets to be put after the arguments, such
3465 that "int c()[]" means something like "int[] c()" */
3468 skip(']'); /* only handle simple "[]" */
3471 /* we push a anonymous symbol which will contain the function prototype */
3472 ad
->a
.func_args
= arg_size
;
3473 s
= sym_push(SYM_FIELD
, type
, 0, l
);
3478 } else if (tok
== '[') {
3479 /* array definition */
3481 if (tok
== TOK_RESTRICT1
)
3486 if (!local_stack
|| nocode_wanted
)
3487 vpushi(expr_const());
3489 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3492 tcc_error("invalid array size");
3494 if (!is_integer_btype(vtop
->type
.t
& VT_BTYPE
))
3495 tcc_error("size of variable length array should be an integer");
3500 /* parse next post type */
3501 post_type(type
, ad
);
3502 if (type
->t
== VT_FUNC
)
3503 tcc_error("declaration of an array of functions");
3504 t1
|= type
->t
& VT_VLA
;
3507 loc
-= type_size(&int_type
, &align
);
3511 vla_runtime_type_size(type
, &align
);
3513 vset(&int_type
, VT_LOCAL
|VT_LVAL
, n
);
3520 /* we push an anonymous symbol which will contain the array
3522 s
= sym_push(SYM_FIELD
, type
, 0, n
);
3523 type
->t
= (t1
? VT_VLA
: VT_ARRAY
) | VT_PTR
;
3528 /* Parse a type declaration (except basic type), and return the type
3529 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3530 expected. 'type' should contain the basic type. 'ad' is the
3531 attribute definition of the basic type. It can be modified by
3534 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
3537 CType type1
, *type2
;
3538 int qualifiers
, storage
;
3540 while (tok
== '*') {
3548 qualifiers
|= VT_CONSTANT
;
3553 qualifiers
|= VT_VOLATILE
;
3561 type
->t
|= qualifiers
;
3564 /* XXX: clarify attribute handling */
3565 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3566 parse_attribute(ad
);
3568 /* recursive type */
3569 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3570 type1
.t
= 0; /* XXX: same as int */
3573 /* XXX: this is not correct to modify 'ad' at this point, but
3574 the syntax is not clear */
3575 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3576 parse_attribute(ad
);
3577 type_decl(&type1
, ad
, v
, td
);
3580 /* type identifier */
3581 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
3585 if (!(td
& TYPE_ABSTRACT
))
3586 expect("identifier");
3590 storage
= type
->t
& VT_STORAGE
;
3591 type
->t
&= ~VT_STORAGE
;
3592 if (storage
& VT_STATIC
) {
3593 int saved_nocode_wanted
= nocode_wanted
;
3595 post_type(type
, ad
);
3596 nocode_wanted
= saved_nocode_wanted
;
3598 post_type(type
, ad
);
3600 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3601 parse_attribute(ad
);
3605 /* append type at the end of type1 */
3618 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3619 ST_FUNC
int lvalue_type(int t
)
3624 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
3626 else if (bt
== VT_SHORT
)
3630 if (t
& VT_UNSIGNED
)
3631 r
|= VT_LVAL_UNSIGNED
;
3635 /* indirection with full error checking and bound check */
3636 ST_FUNC
void indir(void)
3638 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
3639 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
3643 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
3645 vtop
->type
= *pointed_type(&vtop
->type
);
3646 /* Arrays and functions are never lvalues */
3647 if (!(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_VLA
)
3648 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3649 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3650 /* if bound checking, the referenced pointer must be checked */
3651 #ifdef CONFIG_TCC_BCHECK
3652 if (tcc_state
->do_bounds_check
)
3653 vtop
->r
|= VT_MUSTBOUND
;
3658 /* pass a parameter to a function and do type checking and casting */
3659 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
3664 func_type
= func
->c
;
3665 if (func_type
== FUNC_OLD
||
3666 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
3667 /* default casting : only need to convert float to double */
3668 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
3671 } else if (vtop
->type
.t
& VT_BITFIELD
) {
3672 type
.t
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
3675 } else if (arg
== NULL
) {
3676 tcc_error("too many arguments to function");
3679 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
3680 gen_assign_cast(&type
);
3684 /* parse an expression of the form '(type)' or '(expr)' and return its
3686 static void parse_expr_type(CType
*type
)
3692 if (parse_btype(type
, &ad
)) {
3693 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3700 static void parse_type(CType
*type
)
3705 if (!parse_btype(type
, &ad
)) {
3708 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3711 static void vpush_tokc(int t
)
3716 vsetc(&type
, VT_CONST
, &tokc
);
3719 ST_FUNC
void unary(void)
3721 int n
, t
, align
, size
, r
, sizeof_caller
;
3725 static int in_sizeof
= 0;
3727 sizeof_caller
= in_sizeof
;
3729 /* XXX: GCC 2.95.3 does not generate a table although it should be
3743 vpush_tokc(VT_INT
| VT_UNSIGNED
);
3747 vpush_tokc(VT_LLONG
);
3751 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
3755 vpush_tokc(VT_FLOAT
);
3759 vpush_tokc(VT_DOUBLE
);
3763 vpush_tokc(VT_LDOUBLE
);
3766 case TOK___FUNCTION__
:
3768 goto tok_identifier
;
3774 /* special function name identifier */
3775 len
= strlen(funcname
) + 1;
3776 /* generate char[len] type */
3781 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
3782 ptr
= section_ptr_add(data_section
, len
);
3783 memcpy(ptr
, funcname
, len
);
3788 #ifdef TCC_TARGET_PE
3789 t
= VT_SHORT
| VT_UNSIGNED
;
3795 /* string parsing */
3798 if (tcc_state
->warn_write_strings
)
3803 memset(&ad
, 0, sizeof(AttributeDef
));
3804 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, NULL
, 0);
3809 if (parse_btype(&type
, &ad
)) {
3810 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
3812 /* check ISOC99 compound literal */
3814 /* data is allocated locally by default */
3819 /* all except arrays are lvalues */
3820 if (!(type
.t
& VT_ARRAY
))
3821 r
|= lvalue_type(type
.t
);
3822 memset(&ad
, 0, sizeof(AttributeDef
));
3823 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, NULL
, 0);
3825 if (sizeof_caller
) {
3832 } else if (tok
== '{') {
3835 tcc_error("statement expression in global scope"); */
3836 /* this check breaks compilation of the linux 2.4.26 with the meesage:
3837 linux/include/net/tcp.h:945: error: statement expression in global scope */
3839 /* save all registers */
3841 /* statement expression : we do not accept break/continue
3842 inside as GCC does */
3843 block(NULL
, NULL
, NULL
, NULL
, 0, 1);
3858 /* functions names must be treated as function pointers,
3859 except for unary '&' and sizeof. Since we consider that
3860 functions are not lvalues, we only have to handle it
3861 there and in function calls. */
3862 /* arrays can also be used although they are not lvalues */
3863 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
3864 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
3866 mk_pointer(&vtop
->type
);
3872 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3874 boolean
.t
= VT_BOOL
;
3876 vtop
->c
.i
= !vtop
->c
.i
;
3877 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
3879 else if (!nocode_wanted
) {
3881 vseti(VT_JMP
, gvtst(1, 0));
3895 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
3896 tcc_error("pointer not accepted for unary plus");
3897 /* In order to force cast, we add zero, except for floating point
3898 where we really need an noop (otherwise -0.0 will be transformed
3900 if (!is_float(vtop
->type
.t
)) {
3911 unary_type(&type
); // Perform a in_sizeof = 0;
3912 size
= type_size(&type
, &align
);
3913 if (t
== TOK_SIZEOF
) {
3914 if (!(type
.t
& VT_VLA
)) {
3916 tcc_error("sizeof applied to an incomplete type");
3919 vla_runtime_type_size(&type
, &align
);
3924 vtop
->type
.t
|= VT_UNSIGNED
;
3927 case TOK_builtin_types_compatible_p
:
3936 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3937 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3938 vpushi(is_compatible_types(&type1
, &type2
));
3941 case TOK_builtin_constant_p
:
3943 int saved_nocode_wanted
, res
;
3946 saved_nocode_wanted
= nocode_wanted
;
3949 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
3951 nocode_wanted
= saved_nocode_wanted
;
3956 case TOK_builtin_frame_address
:
3957 case TOK_builtin_return_address
:
3964 if (tok
!= TOK_CINT
) {
3965 tcc_error("%s only takes positive integers",
3966 tok1
== TOK_builtin_return_address
?
3967 "__builtin_return_address" :
3968 "__builtin_frame_address");
3970 level
= (uint32_t)tokc
.i
;
3975 vset(&type
, VT_LOCAL
, 0); /* local frame */
3977 mk_pointer(&vtop
->type
);
3978 indir(); /* -> parent frame */
3980 if (tok1
== TOK_builtin_return_address
) {
3981 // assume return address is just above frame pointer on stack
3984 mk_pointer(&vtop
->type
);
3989 #ifdef TCC_TARGET_X86_64
3990 #ifdef TCC_TARGET_PE
3991 case TOK_builtin_va_start
:
3999 if ((vtop
->r
& VT_VALMASK
) != VT_LOCAL
)
4000 tcc_error("__builtin_va_start expects a local variable");
4001 vtop
->r
&= ~(VT_LVAL
| VT_REF
);
4002 vtop
->type
= char_pointer_type
;
4007 case TOK_builtin_va_arg_types
:
4014 vpushi(classify_x86_64_va_arg(&type
));
4020 #ifdef TCC_TARGET_ARM64
4021 case TOK___va_start
: {
4023 tcc_error("statement in global scope");
4033 vtop
->type
.t
= VT_VOID
;
4036 case TOK___va_arg
: {
4039 tcc_error("statement in global scope");
4051 case TOK___arm64_clear_cache
: {
4060 vtop
->type
.t
= VT_VOID
;
4064 /* pre operations */
4075 t
= vtop
->type
.t
& VT_BTYPE
;
4077 /* In IEEE negate(x) isn't subtract(0,x), but rather
4082 else if (t
== VT_DOUBLE
)
4093 goto tok_identifier
;
4095 /* allow to take the address of a label */
4096 if (tok
< TOK_UIDENT
)
4097 expect("label identifier");
4098 s
= label_find(tok
);
4100 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4102 if (s
->r
== LABEL_DECLARED
)
4103 s
->r
= LABEL_FORWARD
;
4106 s
->type
.t
= VT_VOID
;
4107 mk_pointer(&s
->type
);
4108 s
->type
.t
|= VT_STATIC
;
4110 vpushsym(&s
->type
, s
);
4114 // special qnan , snan and infinity values
4116 vpush64(VT_DOUBLE
, 0x7ff8000000000000ULL
);
4120 vpush64(VT_DOUBLE
, 0x7ff0000000000001ULL
);
4124 vpush64(VT_DOUBLE
, 0x7ff0000000000000ULL
);
4133 expect("identifier");
4136 const char *name
= get_tok_str(t
, NULL
);
4138 tcc_error("'%s' undeclared", name
);
4139 /* for simple function calls, we tolerate undeclared
4140 external reference to int() function */
4141 if (tcc_state
->warn_implicit_function_declaration
4142 #ifdef TCC_TARGET_PE
4143 /* people must be warned about using undeclared WINAPI functions
4144 (which usually start with uppercase letter) */
4145 || (name
[0] >= 'A' && name
[0] <= 'Z')
4148 tcc_warning("implicit declaration of function '%s'", name
);
4149 s
= external_global_sym(t
, &func_old_type
, 0);
4151 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
4152 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
4153 /* if referencing an inline function, then we generate a
4154 symbol to it if not already done. It will have the
4155 effect to generate code for it at the end of the
4156 compilation unit. Inline function as always
4157 generated in the text section. */
4159 put_extern_sym(s
, text_section
, 0, 0);
4160 r
= VT_SYM
| VT_CONST
;
4164 vset(&s
->type
, r
, s
->c
);
4165 /* if forward reference, we must point to s */
4166 if (vtop
->r
& VT_SYM
) {
4173 /* post operations */
4175 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
4178 } else if (tok
== '.' || tok
== TOK_ARROW
|| tok
== TOK_CDOUBLE
) {
4181 if (tok
== TOK_ARROW
)
4183 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
4186 /* expect pointer on structure */
4187 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
4188 expect("struct or union");
4189 if (tok
== TOK_CDOUBLE
)
4190 expect("field name");
4192 if (tok
== TOK_CINT
|| tok
== TOK_CUINT
)
4193 expect("field name");
4197 while ((s
= s
->next
) != NULL
) {
4202 tcc_error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, NULL
));
4203 /* add field offset to pointer */
4204 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
4207 /* change type to field type, and set to lvalue */
4208 vtop
->type
= s
->type
;
4209 vtop
->type
.t
|= qualifiers
;
4210 /* an array is never an lvalue */
4211 if (!(vtop
->type
.t
& VT_ARRAY
)) {
4212 vtop
->r
|= lvalue_type(vtop
->type
.t
);
4213 #ifdef CONFIG_TCC_BCHECK
4214 /* if bound checking, the referenced pointer must be checked */
4215 if (tcc_state
->do_bounds_check
)
4216 vtop
->r
|= VT_MUSTBOUND
;
4220 } else if (tok
== '[') {
4226 } else if (tok
== '(') {
4229 int nb_args
, ret_nregs
, ret_align
, regsize
, variadic
;
4232 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
4233 /* pointer test (no array accepted) */
4234 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
4235 vtop
->type
= *pointed_type(&vtop
->type
);
4236 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
4240 expect("function pointer");
4243 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
4245 /* get return type */
4248 sa
= s
->next
; /* first parameter */
4251 /* compute first implicit argument if a structure is returned */
4252 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
4253 variadic
= (s
->c
== FUNC_ELLIPSIS
);
4254 ret_nregs
= gfunc_sret(&s
->type
, variadic
, &ret
.type
,
4255 &ret_align
, ®size
);
4257 /* get some space for the returned structure */
4258 size
= type_size(&s
->type
, &align
);
4259 #ifdef TCC_TARGET_ARM64
4260 /* On arm64, a small struct is return in registers.
4261 It is much easier to write it to memory if we know
4262 that we are allowed to write some extra bytes, so
4263 round the allocated space up to a power of 2: */
4265 while (size
& (size
- 1))
4266 size
= (size
| (size
- 1)) + 1;
4268 loc
= (loc
- size
) & -align
;
4270 ret
.r
= VT_LOCAL
| VT_LVAL
;
4271 /* pass it as 'int' to avoid structure arg passing
4273 vseti(VT_LOCAL
, loc
);
4283 /* return in register */
4284 if (is_float(ret
.type
.t
)) {
4285 ret
.r
= reg_fret(ret
.type
.t
);
4286 #ifdef TCC_TARGET_X86_64
4287 if ((ret
.type
.t
& VT_BTYPE
) == VT_QFLOAT
)
4291 #ifndef TCC_TARGET_ARM64
4292 #ifdef TCC_TARGET_X86_64
4293 if ((ret
.type
.t
& VT_BTYPE
) == VT_QLONG
)
4295 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
4306 gfunc_param_typed(s
, sa
);
4316 tcc_error("too few arguments to function");
4318 if (!nocode_wanted
) {
4319 gfunc_call(nb_args
);
4321 vtop
-= (nb_args
+ 1);
4325 for (r
= ret
.r
+ ret_nregs
+ !ret_nregs
; r
-- > ret
.r
;) {
4326 vsetc(&ret
.type
, r
, &ret
.c
);
4327 vtop
->r2
= ret
.r2
; /* Loop only happens when r2 is VT_CONST */
4330 /* handle packed struct return */
4331 if (((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) && ret_nregs
) {
4334 size
= type_size(&s
->type
, &align
);
4335 /* We're writing whole regs often, make sure there's enough
4336 space. Assume register size is power of 2. */
4337 if (regsize
> align
)
4339 loc
= (loc
- size
) & -align
;
4343 vset(&ret
.type
, VT_LOCAL
| VT_LVAL
, addr
+ offset
);
4347 if (--ret_nregs
== 0)
4351 vset(&s
->type
, VT_LOCAL
| VT_LVAL
, addr
);
4359 ST_FUNC
void expr_prod(void)
4364 while (tok
== '*' || tok
== '/' || tok
== '%') {
4372 ST_FUNC
void expr_sum(void)
4377 while (tok
== '+' || tok
== '-') {
4385 static void expr_shift(void)
4390 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
4398 static void expr_cmp(void)
4403 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
4404 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
4412 static void expr_cmpeq(void)
4417 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
4425 static void expr_and(void)
4428 while (tok
== '&') {
4435 static void expr_xor(void)
4438 while (tok
== '^') {
4445 static void expr_or(void)
4448 while (tok
== '|') {
4455 /* XXX: fix this mess */
4456 static void expr_land_const(void)
4459 while (tok
== TOK_LAND
) {
4466 /* XXX: fix this mess */
4467 static void expr_lor_const(void)
4470 while (tok
== TOK_LOR
) {
4477 /* only used if non constant */
4478 static void expr_land(void)
4483 if (tok
== TOK_LAND
) {
4488 if (tok
!= TOK_LAND
) {
4498 static void expr_lor(void)
4503 if (tok
== TOK_LOR
) {
4508 if (tok
!= TOK_LOR
) {
4518 /* XXX: better constant handling */
4519 static void expr_cond(void)
4521 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
;
4523 CType type
, type1
, type2
;
4531 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4534 boolean
.t
= VT_BOOL
;
4539 if (tok
!= ':' || !gnu_ext
) {
4551 if (vtop
!= vstack
) {
4552 /* needed to avoid having different registers saved in
4554 if (is_float(vtop
->type
.t
)) {
4556 #ifdef TCC_TARGET_X86_64
4557 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4567 if (tok
== ':' && gnu_ext
) {
4575 sv
= *vtop
; /* save value to handle it later */
4576 vtop
--; /* no vpop so that FP stack is not flushed */
4584 bt1
= t1
& VT_BTYPE
;
4586 bt2
= t2
& VT_BTYPE
;
4587 /* cast operands to correct type according to ISOC rules */
4588 if (is_float(bt1
) || is_float(bt2
)) {
4589 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
4590 type
.t
= VT_LDOUBLE
;
4591 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
4596 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
4597 /* cast to biggest op */
4599 /* convert to unsigned if it does not fit in a long long */
4600 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
4601 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
4602 type
.t
|= VT_UNSIGNED
;
4603 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
4604 /* If one is a null ptr constant the result type
4606 if (is_null_pointer (vtop
))
4608 else if (is_null_pointer (&sv
))
4610 /* XXX: test pointer compatibility, C99 has more elaborate
4614 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
4615 /* XXX: test function pointer compatibility */
4616 type
= bt1
== VT_FUNC
? type1
: type2
;
4617 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
4618 /* XXX: test structure compatibility */
4619 type
= bt1
== VT_STRUCT
? type1
: type2
;
4620 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
4621 /* NOTE: as an extension, we accept void on only one side */
4624 /* integer operations */
4626 /* convert to unsigned if it does not fit in an integer */
4627 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
4628 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
4629 type
.t
|= VT_UNSIGNED
;
4632 /* now we convert second operand */
4634 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4637 if (is_float(type
.t
)) {
4639 #ifdef TCC_TARGET_X86_64
4640 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4644 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
4645 /* for long longs, we use fixed registers to avoid having
4646 to handle a complicated move */
4651 /* this is horrible, but we must also convert first
4655 /* put again first value and cast it */
4658 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4661 move_reg(r2
, r1
, type
.t
);
4668 static void expr_eq(void)
4674 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
4675 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
4676 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
4691 ST_FUNC
void gexpr(void)
4702 /* parse an expression and return its type without any side effect. */
4703 static void expr_type(CType
*type
)
4705 int saved_nocode_wanted
;
4707 saved_nocode_wanted
= nocode_wanted
;
4712 nocode_wanted
= saved_nocode_wanted
;
4715 /* parse a unary expression and return its type without any side
4717 static void unary_type(CType
*type
)
4729 /* parse a constant expression and return value in vtop. */
4730 static void expr_const1(void)
4739 /* parse an integer constant and return its value. */
4740 ST_FUNC
int expr_const(void)
4744 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
4745 expect("constant expression");
4751 /* return the label token if current token is a label, otherwise
4753 static int is_label(void)
4757 /* fast test first */
4758 if (tok
< TOK_UIDENT
)
4760 /* no need to save tokc because tok is an identifier */
4767 unget_tok(last_tok
);
4772 static void label_or_decl(int l
)
4776 /* fast test first */
4777 if (tok
>= TOK_UIDENT
)
4779 /* no need to save tokc because tok is an identifier */
4783 unget_tok(last_tok
);
4786 unget_tok(last_tok
);
4791 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
4792 int case_reg
, int is_expr
)
4795 Sym
*s
, *frame_bottom
;
4797 /* generate line number info */
4798 if (tcc_state
->do_debug
&&
4799 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
4800 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
4802 last_line_num
= file
->line_num
;
4806 /* default return value is (void) */
4808 vtop
->type
.t
= VT_VOID
;
4811 if (tok
== TOK_IF
) {
4818 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4820 if (c
== TOK_ELSE
) {
4824 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4825 gsym(d
); /* patch else jmp */
4828 } else if (tok
== TOK_WHILE
) {
4837 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4841 } else if (tok
== '{') {
4843 int block_vla_sp_loc
= vla_sp_loc
, saved_vlas_in_scope
= vlas_in_scope
;
4846 /* record local declaration stack position */
4848 frame_bottom
= sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4849 frame_bottom
->next
= scope_stack_bottom
;
4850 scope_stack_bottom
= frame_bottom
;
4851 llabel
= local_label_stack
;
4853 /* handle local labels declarations */
4854 if (tok
== TOK_LABEL
) {
4857 if (tok
< TOK_UIDENT
)
4858 expect("label identifier");
4859 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
4869 while (tok
!= '}') {
4870 label_or_decl(VT_LOCAL
);
4874 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4877 /* pop locally defined labels */
4878 label_pop(&local_label_stack
, llabel
);
4880 /* XXX: this solution makes only valgrind happy...
4881 triggered by gcc.c-torture/execute/20000917-1.c */
4883 switch(vtop
->type
.t
& VT_BTYPE
) {
4885 /* this breaks a compilation of the linux kernel v2.4.26 */
4886 /* pmd_t *new = ({ __asm__ __volatile__("ud2\n") ; ((pmd_t *)1); }); */
4887 /* Look a commit a80acab: Display error on statement expressions with complex return type */
4888 /* A pointer is not a complex return type */
4892 for(p
=vtop
->type
.ref
;p
;p
=p
->prev
)
4894 tcc_error("unsupported expression type");
4897 /* pop locally defined symbols */
4898 scope_stack_bottom
= scope_stack_bottom
->next
;
4899 sym_pop(&local_stack
, s
);
4901 /* Pop VLA frames and restore stack pointer if required */
4902 if (vlas_in_scope
> saved_vlas_in_scope
) {
4903 vla_sp_loc
= saved_vlas_in_scope
? block_vla_sp_loc
: vla_sp_root_loc
;
4906 vlas_in_scope
= saved_vlas_in_scope
;
4909 } else if (tok
== TOK_RETURN
) {
4913 gen_assign_cast(&func_vt
);
4914 #ifdef TCC_TARGET_ARM64
4915 // Perhaps it would be better to use this for all backends:
4918 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
4919 CType type
, ret_type
;
4920 int ret_align
, ret_nregs
, regsize
;
4921 ret_nregs
= gfunc_sret(&func_vt
, func_var
, &ret_type
,
4922 &ret_align
, ®size
);
4923 if (0 == ret_nregs
) {
4924 /* if returning structure, must copy it to implicit
4925 first pointer arg location */
4928 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
4931 /* copy structure value to pointer */
4934 /* returning structure packed into registers */
4935 int r
, size
, addr
, align
;
4936 size
= type_size(&func_vt
,&align
);
4937 if ((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) ||
4938 (vtop
->c
.i
& (ret_align
-1)))
4939 && (align
& (ret_align
-1))) {
4940 loc
= (loc
- size
) & -ret_align
;
4943 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
4947 vset(&ret_type
, VT_LOCAL
| VT_LVAL
, addr
);
4949 vtop
->type
= ret_type
;
4950 if (is_float(ret_type
.t
))
4951 r
= rc_fret(ret_type
.t
);
4957 if (--ret_nregs
== 0)
4959 /* We assume that when a structure is returned in multiple
4960 registers, their classes are consecutive values of the
4963 vtop
->c
.i
+= regsize
;
4964 vtop
->r
= VT_LOCAL
| VT_LVAL
;
4967 } else if (is_float(func_vt
.t
)) {
4968 gv(rc_fret(func_vt
.t
));
4973 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
4976 rsym
= gjmp(rsym
); /* jmp */
4977 } else if (tok
== TOK_BREAK
) {
4980 tcc_error("cannot break");
4981 *bsym
= gjmp(*bsym
);
4984 } else if (tok
== TOK_CONTINUE
) {
4987 tcc_error("cannot continue");
4988 vla_sp_restore_root();
4989 *csym
= gjmp(*csym
);
4992 } else if (tok
== TOK_FOR
) {
4997 frame_bottom
= sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4998 frame_bottom
->next
= scope_stack_bottom
;
4999 scope_stack_bottom
= frame_bottom
;
5001 /* c99 for-loop init decl? */
5002 if (!decl0(VT_LOCAL
, 1)) {
5003 /* no, regular for-loop init expr */
5029 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
5033 scope_stack_bottom
= scope_stack_bottom
->next
;
5034 sym_pop(&local_stack
, s
);
5036 if (tok
== TOK_DO
) {
5042 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
5053 if (tok
== TOK_SWITCH
) {
5057 /* XXX: other types than integer */
5058 case_reg
= gv(RC_INT
);
5062 b
= gjmp(0); /* jump to first case */
5064 block(&a
, csym
, &b
, &c
, case_reg
, 0);
5065 /* if no default, jmp after switch */
5073 if (tok
== TOK_CASE
) {
5080 if (gnu_ext
&& tok
== TOK_DOTS
) {
5084 tcc_warning("empty case range");
5086 /* since a case is like a label, we must skip it with a jmp */
5094 *case_sym
= gtst(1, 0);
5097 *case_sym
= gtst(1, 0);
5101 *case_sym
= gtst(1, *case_sym
);
5103 case_reg
= gv(RC_INT
);
5108 goto block_after_label
;
5110 if (tok
== TOK_DEFAULT
) {
5116 tcc_error("too many 'default'");
5119 goto block_after_label
;
5121 if (tok
== TOK_GOTO
) {
5123 if (tok
== '*' && gnu_ext
) {
5127 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
5130 } else if (tok
>= TOK_UIDENT
) {
5131 s
= label_find(tok
);
5132 /* put forward definition if needed */
5134 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
5136 if (s
->r
== LABEL_DECLARED
)
5137 s
->r
= LABEL_FORWARD
;
5139 vla_sp_restore_root();
5140 if (s
->r
& LABEL_FORWARD
)
5141 s
->jnext
= gjmp(s
->jnext
);
5143 gjmp_addr(s
->jnext
);
5146 expect("label identifier");
5149 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
5157 if (s
->r
== LABEL_DEFINED
)
5158 tcc_error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
5160 s
->r
= LABEL_DEFINED
;
5162 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
5166 /* we accept this, but it is a mistake */
5169 tcc_warning("deprecated use of label at end of compound statement");
5173 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
5176 /* expression case */
5191 /* t is the array or struct type. c is the array or struct
5192 address. cur_index/cur_field is the pointer to the current
5193 value. 'size_only' is true if only size info is needed (only used
5195 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
5196 int *cur_index
, Sym
**cur_field
,
5200 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
5206 if (gnu_ext
&& (l
= is_label()) != 0)
5208 while (tok
== '[' || tok
== '.') {
5210 if (!(type
->t
& VT_ARRAY
))
5211 expect("array type");
5214 index
= expr_const();
5215 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
5216 expect("invalid index");
5217 if (tok
== TOK_DOTS
&& gnu_ext
) {
5219 index_last
= expr_const();
5220 if (index_last
< 0 ||
5221 (s
->c
>= 0 && index_last
>= s
->c
) ||
5223 expect("invalid index");
5229 *cur_index
= index_last
;
5230 type
= pointed_type(type
);
5231 elem_size
= type_size(type
, &align
);
5232 c
+= index
* elem_size
;
5233 /* NOTE: we only support ranges for last designator */
5234 nb_elems
= index_last
- index
+ 1;
5235 if (nb_elems
!= 1) {
5244 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
5245 expect("struct/union type");
5258 /* XXX: fix this mess by using explicit storage field */
5260 type1
.t
|= (type
->t
& ~VT_TYPE
);
5274 if (type
->t
& VT_ARRAY
) {
5276 type
= pointed_type(type
);
5277 c
+= index
* type_size(type
, &align
);
5281 tcc_error("too many field init");
5282 /* XXX: fix this mess by using explicit storage field */
5284 type1
.t
|= (type
->t
& ~VT_TYPE
);
5289 decl_initializer(type
, sec
, c
, 0, size_only
);
5291 /* XXX: make it more general */
5292 if (!size_only
&& nb_elems
> 1) {
5293 unsigned long c_end
;
5298 tcc_error("range init not supported yet for dynamic storage");
5299 c_end
= c
+ nb_elems
* elem_size
;
5300 if (c_end
> sec
->data_allocated
)
5301 section_realloc(sec
, c_end
);
5302 src
= sec
->data
+ c
;
5304 for(i
= 1; i
< nb_elems
; i
++) {
5306 memcpy(dst
, src
, elem_size
);
5312 #define EXPR_CONST 1
5315 /* store a value or an expression directly in global data or in local array */
5316 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
5317 int v
, int expr_type
)
5319 int saved_global_expr
, bt
, bit_pos
, bit_size
;
5321 unsigned long long bit_mask
;
5329 /* compound literals must be allocated globally in this case */
5330 saved_global_expr
= global_expr
;
5333 global_expr
= saved_global_expr
;
5334 /* NOTE: symbols are accepted */
5335 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
5336 tcc_error("initializer element is not constant");
5344 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
5347 /* XXX: not portable */
5348 /* XXX: generate error if incorrect relocation */
5349 gen_assign_cast(&dtype
);
5350 bt
= type
->t
& VT_BTYPE
;
5351 /* we'll write at most 16 bytes */
5352 if (c
+ 16 > sec
->data_allocated
) {
5353 section_realloc(sec
, c
+ 16);
5355 ptr
= sec
->data
+ c
;
5356 /* XXX: make code faster ? */
5357 if (!(type
->t
& VT_BITFIELD
)) {
5362 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5363 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
5364 bit_mask
= (1LL << bit_size
) - 1;
5366 if ((vtop
->r
& VT_SYM
) &&
5372 (bt
== VT_INT
&& bit_size
!= 32)))
5373 tcc_error("initializer element is not computable at load time");
5375 /* XXX: when cross-compiling we assume that each type has the
5376 same representation on host and target, which is likely to
5377 be wrong in the case of long double */
5379 vtop
->c
.i
= (vtop
->c
.i
!= 0);
5381 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5384 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5387 *(double *)ptr
= vtop
->c
.d
;
5390 *(long double *)ptr
= vtop
->c
.ld
;
5393 *(long long *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5396 addr_t val
= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5397 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
5398 if (vtop
->r
& VT_SYM
)
5399 greloca(sec
, vtop
->sym
, c
, R_DATA_PTR
, val
);
5401 *(addr_t
*)ptr
|= val
;
5403 if (vtop
->r
& VT_SYM
)
5404 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
5405 *(addr_t
*)ptr
|= val
;
5410 int val
= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5411 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
5412 if (vtop
->r
& VT_SYM
)
5413 greloca(sec
, vtop
->sym
, c
, R_DATA_PTR
, val
);
5417 if (vtop
->r
& VT_SYM
)
5418 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
5426 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
5433 /* put zeros for variable based init */
5434 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
5437 /* nothing to do because globals are already set to zero */
5439 vpush_global_sym(&func_old_type
, TOK_memset
);
5441 #ifdef TCC_TARGET_ARM
5452 /* 't' contains the type and storage info. 'c' is the offset of the
5453 object in section 'sec'. If 'sec' is NULL, it means stack based
5454 allocation. 'first' is true if array '{' must be read (multi
5455 dimension implicit array init handling). 'size_only' is true if
5456 size only evaluation is wanted (only for arrays). */
5457 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
5458 int first
, int size_only
)
5460 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, parlevel1
, i
;
5461 int size1
, align1
, expr_type
;
5465 if (type
->t
& VT_VLA
) {
5468 /* save current stack pointer */
5469 if (vlas_in_scope
== 0) {
5470 if (vla_sp_root_loc
== -1)
5471 vla_sp_root_loc
= (loc
-= PTR_SIZE
);
5472 gen_vla_sp_save(vla_sp_root_loc
);
5475 vla_runtime_type_size(type
, &a
);
5476 gen_vla_alloc(type
, a
);
5480 } else if (type
->t
& VT_ARRAY
) {
5484 t1
= pointed_type(type
);
5485 size1
= type_size(t1
, &align1
);
5488 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
5491 tcc_error("character array initializer must be a literal,"
5492 " optionally enclosed in braces");
5497 /* only parse strings here if correct type (otherwise: handle
5498 them as ((w)char *) expressions */
5499 if ((tok
== TOK_LSTR
&&
5500 #ifdef TCC_TARGET_PE
5501 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
5503 (t1
->t
& VT_BTYPE
) == VT_INT
5505 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
5506 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5511 /* compute maximum number of chars wanted */
5513 cstr_len
= cstr
->size
;
5515 cstr_len
= cstr
->size
/ sizeof(nwchar_t
);
5518 if (n
>= 0 && nb
> (n
- array_length
))
5519 nb
= n
- array_length
;
5522 tcc_warning("initializer-string for array is too long");
5523 /* in order to go faster for common case (char
5524 string in global variable, we handle it
5526 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
5527 memcpy(sec
->data
+ c
+ array_length
, cstr
->data
, nb
);
5531 ch
= ((unsigned char *)cstr
->data
)[i
];
5533 ch
= ((nwchar_t
*)cstr
->data
)[i
];
5534 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
5542 /* only add trailing zero if enough storage (no
5543 warning in this case since it is standard) */
5544 if (n
< 0 || array_length
< n
) {
5546 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
5552 while (tok
!= '}') {
5553 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
5554 if (n
>= 0 && index
>= n
)
5555 tcc_error("index too large");
5556 /* must put zero in holes (note that doing it that way
5557 ensures that it even works with designators) */
5558 if (!size_only
&& array_length
< index
) {
5559 init_putz(t1
, sec
, c
+ array_length
* size1
,
5560 (index
- array_length
) * size1
);
5563 if (index
> array_length
)
5564 array_length
= index
;
5565 /* special test for multi dimensional arrays (may not
5566 be strictly correct if designators are used at the
5568 if (index
>= n
&& no_oblock
)
5577 /* put zeros at the end */
5578 if (!size_only
&& n
>= 0 && array_length
< n
) {
5579 init_putz(t1
, sec
, c
+ array_length
* size1
,
5580 (n
- array_length
) * size1
);
5582 /* patch type size if needed */
5584 s
->c
= array_length
;
5585 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
5586 (sec
|| !first
|| tok
== '{')) {
5588 /* NOTE: the previous test is a specific case for automatic
5589 struct/union init */
5590 /* XXX: union needs only one init */
5597 if (tcc_state
->old_struct_init_code
) {
5598 /* an old version of struct initialization.
5599 It have a problems. But with a new version
5600 linux 2.4.26 can't load ramdisk.
5602 while (tok
== '(') {
5606 if (!parse_btype(&type1
, &ad1
))
5608 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
5610 if (!is_assignable_types(type
, &type1
))
5611 tcc_error("invalid type for cast");
5618 if (!parse_btype(&type1
, &ad1
))
5620 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
5622 if (!is_assignable_types(type
, &type1
))
5623 tcc_error("invalid type for cast");
5632 if (first
|| tok
== '{') {
5641 while (tok
!= '}') {
5642 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
5644 if (!size_only
&& array_length
< index
) {
5645 init_putz(type
, sec
, c
+ array_length
,
5646 index
- array_length
);
5648 index
= index
+ type_size(&f
->type
, &align1
);
5649 if (index
> array_length
)
5650 array_length
= index
;
5652 /* gr: skip fields from same union - ugly. */
5655 int f_size
= type_size(&f
->type
, &align
);
5656 int f_type
= (f
->type
.t
& VT_BTYPE
);
5658 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
5659 /* test for same offset */
5660 if (f
->next
->c
!= f
->c
)
5662 if ((f_type
== VT_STRUCT
) && (f_size
== 0)) {
5664 Lets assume a structure of size 0 can't be a member of the union.
5665 This allow to compile the following code from a linux kernel v2.4.26
5666 typedef struct { } rwlock_t;
5672 struct fs_struct init_fs = { { (1) }, (rwlock_t) {}, 0022, };
5673 tcc-0.9.23 can succesfully compile this version of the kernel.
5674 gcc don't have problems with this code too.
5678 /* if yes, test for bitfield shift */
5679 if ((f
->type
.t
& VT_BITFIELD
) && (f
->next
->type
.t
& VT_BITFIELD
)) {
5680 int bit_pos_1
= (f
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5681 int bit_pos_2
= (f
->next
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5682 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
5683 if (bit_pos_1
!= bit_pos_2
)
5690 if (no_oblock
&& f
== NULL
)
5696 /* put zeros at the end */
5697 if (!size_only
&& array_length
< n
) {
5698 init_putz(type
, sec
, c
+ array_length
,
5707 } else if (tok
== '{') {
5709 decl_initializer(type
, sec
, c
, first
, size_only
);
5711 } else if (size_only
) {
5712 /* just skip expression */
5713 parlevel
= parlevel1
= 0;
5714 while ((parlevel
> 0 || parlevel1
> 0 ||
5715 (tok
!= '}' && tok
!= ',')) && tok
!= -1) {
5718 else if (tok
== ')') {
5719 if (parlevel
== 0 && parlevel1
== 0)
5723 else if (tok
== '{')
5725 else if (tok
== '}') {
5726 if (parlevel
== 0 && parlevel1
== 0)
5733 /* currently, we always use constant expression for globals
5734 (may change for scripting case) */
5735 expr_type
= EXPR_CONST
;
5737 expr_type
= EXPR_ANY
;
5738 init_putv(type
, sec
, c
, 0, expr_type
);
5742 /* parse an initializer for type 't' if 'has_init' is non zero, and
5743 allocate space in local or global data space ('r' is either
5744 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5745 variable 'v' with an associated name represented by 'asm_label' of
5746 scope 'scope' is declared before initializers are parsed. If 'v' is
5747 zero, then a reference to the new object is put in the value stack.
5748 If 'has_init' is 2, a special parsing is done to handle string
5750 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
5751 int has_init
, int v
, char *asm_label
,
5754 int size
, align
, addr
, data_offset
;
5756 ParseState saved_parse_state
= {0};
5757 TokenString init_str
;
5759 Sym
*flexible_array
;
5761 flexible_array
= NULL
;
5762 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
5763 Sym
*field
= type
->ref
->next
;
5766 field
= field
->next
;
5767 if (field
->type
.t
& VT_ARRAY
&& field
->type
.ref
->c
< 0)
5768 flexible_array
= field
;
5772 size
= type_size(type
, &align
);
5773 /* If unknown size, we must evaluate it before
5774 evaluating initializers because
5775 initializers can generate global data too
5776 (e.g. string pointers or ISOC99 compound
5777 literals). It also simplifies local
5778 initializers handling */
5779 tok_str_new(&init_str
);
5780 if (size
< 0 || (flexible_array
&& has_init
)) {
5782 tcc_error("unknown type size");
5783 /* get all init string */
5784 if (has_init
== 2) {
5785 /* only get strings */
5786 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5787 tok_str_add_tok(&init_str
);
5792 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
5794 tcc_error("unexpected end of file in initializer");
5795 tok_str_add_tok(&init_str
);
5798 else if (tok
== '}') {
5808 tok_str_add(&init_str
, -1);
5809 tok_str_add(&init_str
, 0);
5812 save_parse_state(&saved_parse_state
);
5814 begin_macro(&init_str
, 0);
5816 decl_initializer(type
, NULL
, 0, 1, 1);
5817 /* prepare second initializer parsing */
5818 macro_ptr
= init_str
.str
;
5821 /* if still unknown size, error */
5822 size
= type_size(type
, &align
);
5824 tcc_error("unknown type size");
5827 size
+= flexible_array
->type
.ref
->c
* pointed_size(&flexible_array
->type
);
5828 /* take into account specified alignment if bigger */
5829 if (ad
->a
.aligned
) {
5830 if (ad
->a
.aligned
> align
)
5831 align
= ad
->a
.aligned
;
5832 } else if (ad
->a
.packed
) {
5835 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
5837 #ifdef CONFIG_TCC_BCHECK
5838 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5842 loc
= (loc
- size
) & -align
;
5844 #ifdef CONFIG_TCC_BCHECK
5845 /* handles bounds */
5846 /* XXX: currently, since we do only one pass, we cannot track
5847 '&' operators, so we add only arrays */
5848 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5850 /* add padding between regions */
5852 /* then add local bound info */
5853 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(addr_t
));
5854 bounds_ptr
[0] = addr
;
5855 bounds_ptr
[1] = size
;
5859 /* local variable */
5860 sym_push(v
, type
, r
, addr
);
5862 /* push local reference */
5863 vset(type
, r
, addr
);
5869 if (v
&& scope
== VT_CONST
) {
5870 /* see if the symbol was already defined */
5873 if (!is_compatible_types(&sym
->type
, type
))
5874 tcc_error("incompatible types for redefinition of '%s'",
5875 get_tok_str(v
, NULL
));
5876 if (sym
->type
.t
& VT_EXTERN
) {
5877 /* if the variable is extern, it was not allocated */
5878 sym
->type
.t
&= ~VT_EXTERN
;
5879 /* set array size if it was omitted in extern
5881 if ((sym
->type
.t
& VT_ARRAY
) &&
5882 sym
->type
.ref
->c
< 0 &&
5884 sym
->type
.ref
->c
= type
->ref
->c
;
5886 /* we accept several definitions of the same
5887 global variable. this is tricky, because we
5888 must play with the SHN_COMMON type of the symbol */
5889 /* XXX: should check if the variable was already
5890 initialized. It is incorrect to initialized it
5892 /* no init data, we won't add more to the symbol */
5899 /* allocate symbol in corresponding section */
5904 else if (tcc_state
->nocommon
)
5908 data_offset
= sec
->data_offset
;
5909 data_offset
= (data_offset
+ align
- 1) & -align
;
5911 /* very important to increment global pointer at this time
5912 because initializers themselves can create new initializers */
5913 data_offset
+= size
;
5914 #ifdef CONFIG_TCC_BCHECK
5915 /* add padding if bound check */
5916 if (tcc_state
->do_bounds_check
)
5919 sec
->data_offset
= data_offset
;
5920 /* allocate section space to put the data */
5921 if (sec
->sh_type
!= SHT_NOBITS
&&
5922 data_offset
> sec
->data_allocated
)
5923 section_realloc(sec
, data_offset
);
5924 /* align section if needed */
5925 if (align
> sec
->sh_addralign
)
5926 sec
->sh_addralign
= align
;
5928 addr
= 0; /* avoid warning */
5932 if (scope
!= VT_CONST
|| !sym
) {
5933 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
5934 sym
->asm_label
= asm_label
? tcc_strdup(asm_label
) : 0;
5936 /* update symbol definition */
5938 put_extern_sym(sym
, sec
, addr
, size
);
5941 /* put a common area */
5942 put_extern_sym(sym
, NULL
, align
, size
);
5943 /* XXX: find a nicer way */
5944 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
5945 esym
->st_shndx
= SHN_COMMON
;
5948 /* push global reference */
5949 sym
= get_sym_ref(type
, sec
, addr
, size
);
5950 vpushsym(type
, sym
);
5952 /* patch symbol weakness */
5953 if (type
->t
& VT_WEAK
)
5955 apply_visibility(sym
, type
);
5956 #ifdef CONFIG_TCC_BCHECK
5957 /* handles bounds now because the symbol must be defined
5958 before for the relocation */
5959 if (tcc_state
->do_bounds_check
) {
5962 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
5963 /* then add global bound info */
5964 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(addr_t
));
5965 bounds_ptr
[0] = 0; /* relocated */
5966 bounds_ptr
[1] = size
;
5970 if (has_init
|| (type
->t
& VT_VLA
)) {
5971 decl_initializer(type
, sec
, addr
, 1, 0);
5972 /* restore parse state if needed */
5975 restore_parse_state(&saved_parse_state
);
5977 /* patch flexible array member size back to -1, */
5978 /* for possible subsequent similar declarations */
5980 flexible_array
->type
.ref
->c
= -1;
5985 static void put_func_debug(Sym
*sym
)
5990 /* XXX: we put here a dummy type */
5991 snprintf(buf
, sizeof(buf
), "%s:%c1",
5992 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
5993 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
5994 cur_text_section
, sym
->c
);
5995 /* //gr gdb wants a line at the function */
5996 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
6001 /* parse an old style function declaration list */
6002 /* XXX: check multiple parameter */
6003 static void func_decl_list(Sym
*func_sym
)
6010 /* parse each declaration */
6011 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
&&
6012 tok
!= TOK_ASM1
&& tok
!= TOK_ASM2
&& tok
!= TOK_ASM3
) {
6013 if (!parse_btype(&btype
, &ad
))
6014 expect("declaration list");
6015 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
6016 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
6018 /* we accept no variable after */
6022 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
6023 /* find parameter in function parameter list */
6026 if ((s
->v
& ~SYM_FIELD
) == v
)
6030 tcc_error("declaration for parameter '%s' but no such parameter",
6031 get_tok_str(v
, NULL
));
6033 /* check that no storage specifier except 'register' was given */
6034 if (type
.t
& VT_STORAGE
)
6035 tcc_error("storage class specified for '%s'", get_tok_str(v
, NULL
));
6036 convert_parameter_type(&type
);
6037 /* we can add the type (NOTE: it could be local to the function) */
6039 /* accept other parameters */
6050 /* parse a function defined by symbol 'sym' and generate its code in
6051 'cur_text_section' */
6052 static void gen_function(Sym
*sym
)
6054 int saved_nocode_wanted
= nocode_wanted
;
6057 ind
= cur_text_section
->data_offset
;
6058 /* NOTE: we patch the symbol size later */
6059 put_extern_sym(sym
, cur_text_section
, ind
, 0);
6060 funcname
= get_tok_str(sym
->v
, NULL
);
6062 /* Initialize VLA state */
6064 vla_sp_root_loc
= -1;
6065 /* put debug symbol */
6066 if (tcc_state
->do_debug
)
6067 put_func_debug(sym
);
6068 /* push a dummy symbol to enable local sym storage */
6069 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
6070 gfunc_prolog(&sym
->type
);
6071 #ifdef CONFIG_TCC_BCHECK
6072 if (tcc_state
->do_bounds_check
&& !strcmp(funcname
, "main")) {
6075 for (i
= 0, sym
= local_stack
; i
< 2; i
++, sym
= sym
->prev
) {
6076 if (sym
->v
& SYM_FIELD
|| sym
->prev
->v
& SYM_FIELD
)
6078 vpush_global_sym(&func_old_type
, TOK___bound_main_arg
);
6079 vset(&sym
->type
, sym
->r
, sym
->c
);
6085 block(NULL
, NULL
, NULL
, NULL
, 0, 0);
6088 cur_text_section
->data_offset
= ind
;
6089 label_pop(&global_label_stack
, NULL
);
6090 /* reset local stack */
6091 scope_stack_bottom
= NULL
;
6092 sym_pop(&local_stack
, NULL
);
6093 /* end of function */
6094 /* patch symbol size */
6095 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
6097 /* patch symbol weakness (this definition overrules any prototype) */
6098 if (sym
->type
.t
& VT_WEAK
)
6100 apply_visibility(sym
, &sym
->type
);
6101 if (tcc_state
->do_debug
) {
6102 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
6104 /* It's better to crash than to generate wrong code */
6105 cur_text_section
= NULL
;
6106 funcname
= ""; /* for safety */
6107 func_vt
.t
= VT_VOID
; /* for safety */
6108 func_var
= 0; /* for safety */
6109 ind
= 0; /* for safety */
6110 nocode_wanted
= saved_nocode_wanted
;
6114 ST_FUNC
void gen_inline_functions(void)
6117 int inline_generated
, i
, ln
;
6118 struct InlineFunc
*fn
;
6120 ln
= file
->line_num
;
6121 /* iterate while inline function are referenced */
6123 inline_generated
= 0;
6124 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
6125 fn
= tcc_state
->inline_fns
[i
];
6127 if (sym
&& sym
->c
) {
6128 /* the function was used: generate its code and
6129 convert it to a normal function */
6132 pstrcpy(file
->filename
, sizeof file
->filename
, fn
->filename
);
6133 sym
->r
= VT_SYM
| VT_CONST
;
6134 sym
->type
.t
&= ~VT_INLINE
;
6136 begin_macro(&fn
->func_str
, 0);
6138 cur_text_section
= text_section
;
6142 inline_generated
= 1;
6145 if (!inline_generated
)
6148 file
->line_num
= ln
;
6149 /* free tokens of unused inline functions */
6150 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
6151 fn
= tcc_state
->inline_fns
[i
];
6153 tok_str_free(fn
->func_str
.str
);
6155 dynarray_reset(&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
);
6158 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
6159 static int decl0(int l
, int is_for_loop_init
)
6165 char *asm_label
= 0; // associated asm label
6168 if (!parse_btype(&btype
, &ad
)) {
6169 if (is_for_loop_init
)
6171 /* skip redundant ';' */
6172 /* XXX: find more elegant solution */
6177 if (l
== VT_CONST
&&
6178 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
6179 /* global asm block */
6183 /* special test for old K&R protos without explicit int
6184 type. Only accepted when defining global data */
6185 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
6189 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
6190 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
6192 if ((btype
.t
& VT_BTYPE
) == VT_STRUCT
) {
6193 int v
= btype
.ref
->v
;
6194 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) >= SYM_FIRST_ANOM
)
6195 tcc_warning("unnamed struct/union that defines no instances");
6200 while (1) { /* iterate thru each declaration */
6202 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
6206 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
6207 printf("type = '%s'\n", buf
);
6210 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
6211 if ((type
.t
& VT_STATIC
) && (l
== VT_LOCAL
)) {
6212 tcc_error("function without file scope cannot be static");
6214 /* if old style function prototype, we accept a
6217 if (sym
->c
== FUNC_OLD
)
6218 func_decl_list(sym
);
6221 tcc_free(asm_label
);
6223 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
6226 asm_label_instr(&astr
);
6227 asm_label
= tcc_strdup(astr
.data
);
6230 /* parse one last attribute list, after asm label */
6231 parse_attribute(&ad
);
6236 #ifdef TCC_TARGET_PE
6237 if (ad
.a
.func_import
)
6238 type
.t
|= VT_IMPORT
;
6239 if (ad
.a
.func_export
)
6240 type
.t
|= VT_EXPORT
;
6242 type
.t
|= ad
.a
.visibility
<< VT_VIS_SHIFT
;
6246 tcc_error("cannot use local functions");
6247 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
6248 expect("function definition");
6250 /* reject abstract declarators in function definition */
6252 while ((sym
= sym
->next
) != NULL
)
6253 if (!(sym
->v
& ~SYM_FIELD
))
6254 expect("identifier");
6256 /* XXX: cannot do better now: convert extern line to static inline */
6257 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
6258 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
6263 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
6266 ref
= sym
->type
.ref
;
6267 if (0 == ref
->a
.func_proto
)
6268 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
6270 /* use func_call from prototype if not defined */
6271 if (ref
->a
.func_call
!= FUNC_CDECL
6272 && type
.ref
->a
.func_call
== FUNC_CDECL
)
6273 type
.ref
->a
.func_call
= ref
->a
.func_call
;
6275 /* use export from prototype */
6276 if (ref
->a
.func_export
)
6277 type
.ref
->a
.func_export
= 1;
6279 /* use static from prototype */
6280 if (sym
->type
.t
& VT_STATIC
)
6281 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
6283 /* If the definition has no visibility use the
6284 one from prototype. */
6285 if (! (type
.t
& VT_VIS_MASK
))
6286 type
.t
|= sym
->type
.t
& VT_VIS_MASK
;
6288 if (!is_compatible_types(&sym
->type
, &type
)) {
6290 tcc_error("incompatible types for redefinition of '%s'",
6291 get_tok_str(v
, NULL
));
6293 type
.ref
->a
.func_proto
= 0;
6294 /* if symbol is already defined, then put complete type */
6297 /* put function symbol */
6298 sym
= global_identifier_push(v
, type
.t
, 0);
6299 sym
->type
.ref
= type
.ref
;
6302 /* static inline functions are just recorded as a kind
6303 of macro. Their code will be emitted at the end of
6304 the compilation unit only if they are used */
6305 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
6306 (VT_INLINE
| VT_STATIC
)) {
6308 struct InlineFunc
*fn
;
6309 const char *filename
;
6311 filename
= file
? file
->filename
: "";
6312 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
6313 strcpy(fn
->filename
, filename
);
6315 tok_str_new(&fn
->func_str
);
6321 tcc_error("unexpected end of file");
6322 tok_str_add_tok(&fn
->func_str
);
6327 } else if (t
== '}') {
6329 if (block_level
== 0)
6333 tok_str_add(&fn
->func_str
, -1);
6334 tok_str_add(&fn
->func_str
, 0);
6335 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
6338 /* compute text section */
6339 cur_text_section
= ad
.section
;
6340 if (!cur_text_section
)
6341 cur_text_section
= text_section
;
6342 sym
->r
= VT_SYM
| VT_CONST
;
6347 if (btype
.t
& VT_TYPEDEF
) {
6348 /* save typedefed type */
6349 /* XXX: test storage specifiers ? */
6350 sym
= sym_push(v
, &type
, 0, 0);
6352 sym
->type
.t
|= VT_TYPEDEF
;
6355 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
6356 /* external function definition */
6357 /* specific case for func_call attribute */
6358 ad
.a
.func_proto
= 1;
6360 } else if (!(type
.t
& VT_ARRAY
)) {
6361 /* not lvalue if array */
6362 r
|= lvalue_type(type
.t
);
6364 has_init
= (tok
== '=');
6365 if (has_init
&& (type
.t
& VT_VLA
))
6366 tcc_error("Variable length array cannot be initialized");
6367 if ((btype
.t
& VT_EXTERN
) || ((type
.t
& VT_BTYPE
) == VT_FUNC
) ||
6368 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
6369 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
6370 /* external variable or function */
6371 /* NOTE: as GCC, uninitialized global static
6372 arrays of null size are considered as
6374 sym
= external_sym(v
, &type
, r
, asm_label
);
6376 if (ad
.alias_target
) {
6381 alias_target
= sym_find(ad
.alias_target
);
6382 if (!alias_target
|| !alias_target
->c
)
6383 tcc_error("unsupported forward __alias__ attribute");
6384 esym
= &((Elf32_Sym
*)symtab_section
->data
)[alias_target
->c
];
6385 tsec
.sh_num
= esym
->st_shndx
;
6386 put_extern_sym2(sym
, &tsec
, esym
->st_value
, esym
->st_size
, 0);
6389 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
6390 if (type
.t
& VT_STATIC
)
6396 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, asm_label
, l
);
6400 if (is_for_loop_init
) {
6401 tcc_free(asm_label
);
6412 tcc_free(asm_label
);
6416 ST_FUNC
void decl(int l
)