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 vla_sp_loc_tmp
; /* vla_sp_loc is set to this when the value won't be needed later */
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 */
61 ST_DATA
int vla_flags
; /* VLA_* flags */
63 ST_DATA SValue __vstack
[1+VSTACK_SIZE
], *vtop
;
65 ST_DATA
int const_wanted
; /* true if constant wanted */
66 ST_DATA
int nocode_wanted
; /* true if no code generation wanted for an expression */
67 ST_DATA
int global_expr
; /* true if compound literals must be allocated globally (used during initializers parsing */
68 ST_DATA CType func_vt
; /* current function return type (used by return instruction) */
69 ST_DATA
int func_var
; /* true if current function is variadic (used by return instruction) */
71 ST_DATA
int last_line_num
, last_ind
, func_ind
; /* debug last line number and pc */
72 ST_DATA
char *funcname
;
74 ST_DATA CType char_pointer_type
, func_old_type
, int_type
, size_type
;
76 /* ------------------------------------------------------------------------- */
77 static void gen_cast(CType
*type
);
78 static inline CType
*pointed_type(CType
*type
);
79 static int is_compatible_types(CType
*type1
, CType
*type2
);
80 static int parse_btype(CType
*type
, AttributeDef
*ad
);
81 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
);
82 static void parse_expr_type(CType
*type
);
83 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
, int first
, int size_only
, int *par_count_p
);
84 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
, int case_reg
, int is_expr
);
85 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
, int has_init
, int v
, char *asm_label
, int scope
);
86 static int decl0(int l
, int is_for_loop_init
);
87 static void expr_eq(void);
88 static void unary_type(CType
*type
);
89 static void vla_runtime_type_size(CType
*type
, int *a
);
90 static void vla_sp_save(void);
91 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
);
92 static void expr_type(CType
*type
);
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 /* ------------------------------------------------------------------------- */
122 /* symbol allocator */
123 static Sym
*__sym_malloc(void)
125 Sym
*sym_pool
, *sym
, *last_sym
;
128 sym_pool
= tcc_malloc(SYM_POOL_NB
* sizeof(Sym
));
129 dynarray_add(&sym_pools
, &nb_sym_pools
, sym_pool
);
131 last_sym
= sym_free_first
;
133 for(i
= 0; i
< SYM_POOL_NB
; i
++) {
134 sym
->next
= last_sym
;
138 sym_free_first
= last_sym
;
142 static inline Sym
*sym_malloc(void)
145 sym
= sym_free_first
;
147 sym
= __sym_malloc();
148 sym_free_first
= sym
->next
;
152 ST_INLN
void sym_free(Sym
*sym
)
154 sym
->next
= sym_free_first
;
155 tcc_free(sym
->asm_label
);
156 sym_free_first
= sym
;
159 /* push, without hashing */
160 ST_FUNC Sym
*sym_push2(Sym
**ps
, int v
, int t
, long c
)
163 if (ps
== &local_stack
) {
164 for (s
= *ps
; s
&& s
!= scope_stack_bottom
; s
= s
->prev
)
165 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
&& s
->v
== v
)
166 tcc_error("incompatible types for redefinition of '%s'",
167 get_tok_str(v
, NULL
));
185 /* find a symbol and return its associated structure. 's' is the top
186 of the symbol stack */
187 ST_FUNC Sym
*sym_find2(Sym
*s
, int v
)
199 /* structure lookup */
200 ST_INLN Sym
*struct_find(int v
)
203 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
205 return table_ident
[v
]->sym_struct
;
208 /* find an identifier */
209 ST_INLN Sym
*sym_find(int v
)
212 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
214 return table_ident
[v
]->sym_identifier
;
217 /* push a given symbol on the symbol stack */
218 ST_FUNC Sym
*sym_push(int v
, CType
*type
, int r
, int c
)
227 s
= sym_push2(ps
, v
, type
->t
, c
);
228 s
->type
.ref
= type
->ref
;
230 /* don't record fields or anonymous symbols */
232 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
233 /* record symbol in token array */
234 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
236 ps
= &ts
->sym_struct
;
238 ps
= &ts
->sym_identifier
;
245 /* push a global identifier */
246 ST_FUNC Sym
*global_identifier_push(int v
, int t
, int c
)
249 s
= sym_push2(&global_stack
, v
, t
, c
);
250 /* don't record anonymous symbol */
251 if (v
< SYM_FIRST_ANOM
) {
252 ps
= &table_ident
[v
- TOK_IDENT
]->sym_identifier
;
253 /* modify the top most local identifier, so that
254 sym_identifier will point to 's' when popped */
256 ps
= &(*ps
)->prev_tok
;
263 /* pop symbols until top reaches 'b' */
264 ST_FUNC
void sym_pop(Sym
**ptop
, Sym
*b
)
274 /* remove symbol in token array */
276 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
277 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
279 ps
= &ts
->sym_struct
;
281 ps
= &ts
->sym_identifier
;
290 static void weaken_symbol(Sym
*sym
)
292 sym
->type
.t
|= VT_WEAK
;
297 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
298 esym_type
= ELFW(ST_TYPE
)(esym
->st_info
);
299 esym
->st_info
= ELFW(ST_INFO
)(STB_WEAK
, esym_type
);
303 static void apply_visibility(Sym
*sym
, CType
*type
)
305 int vis
= sym
->type
.t
& VT_VIS_MASK
;
306 int vis2
= type
->t
& VT_VIS_MASK
;
307 if (vis
== (STV_DEFAULT
<< VT_VIS_SHIFT
))
309 else if (vis2
== (STV_DEFAULT
<< VT_VIS_SHIFT
))
312 vis
= (vis
< vis2
) ? vis
: vis2
;
313 sym
->type
.t
&= ~VT_VIS_MASK
;
319 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
320 vis
>>= VT_VIS_SHIFT
;
321 esym
->st_other
= (esym
->st_other
& ~ELFW(ST_VISIBILITY
)(-1)) | vis
;
325 /* ------------------------------------------------------------------------- */
327 ST_FUNC
void swap(int *p
, int *q
)
335 static void vsetc(CType
*type
, int r
, CValue
*vc
)
339 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
340 tcc_error("memory full (vstack)");
341 /* cannot let cpu flags if other instruction are generated. Also
342 avoid leaving VT_JMP anywhere except on the top of the stack
343 because it would complicate the code generator. */
344 if (vtop
>= vstack
) {
345 v
= vtop
->r
& VT_VALMASK
;
346 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
356 /* push constant of type "type" with useless value */
357 ST_FUNC
void vpush(CType
*type
)
360 vsetc(type
, VT_CONST
, &cval
);
363 /* push integer constant */
364 ST_FUNC
void vpushi(int v
)
368 vsetc(&int_type
, VT_CONST
, &cval
);
371 /* push a pointer sized constant */
372 static void vpushs(addr_t v
)
376 vsetc(&size_type
, VT_CONST
, &cval
);
379 /* push arbitrary 64bit constant */
380 ST_FUNC
void vpush64(int ty
, unsigned long long v
)
387 vsetc(&ctype
, VT_CONST
, &cval
);
390 /* push long long constant */
391 static inline void vpushll(long long v
)
393 vpush64(VT_LLONG
, v
);
396 /* push a symbol value of TYPE */
397 static inline void vpushsym(CType
*type
, Sym
*sym
)
401 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
405 /* Return a static symbol pointing to a section */
406 ST_FUNC Sym
*get_sym_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
412 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
413 sym
->type
.ref
= type
->ref
;
414 sym
->r
= VT_CONST
| VT_SYM
;
415 put_extern_sym(sym
, sec
, offset
, size
);
419 /* push a reference to a section offset by adding a dummy symbol */
420 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
422 vpushsym(type
, get_sym_ref(type
, sec
, offset
, size
));
425 /* define a new external reference to a symbol 'v' of type 'u' */
426 ST_FUNC Sym
*external_global_sym(int v
, CType
*type
, int r
)
432 /* push forward reference */
433 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
434 s
->type
.ref
= type
->ref
;
435 s
->r
= r
| VT_CONST
| VT_SYM
;
440 /* define a new external reference to a symbol 'v' with alternate asm
441 name 'asm_label' of type 'u'. 'asm_label' is equal to NULL if there
442 is no alternate name (most cases) */
443 static Sym
*external_sym(int v
, CType
*type
, int r
, char *asm_label
)
449 /* push forward reference */
450 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
451 s
->asm_label
= asm_label
;
452 s
->type
.t
|= VT_EXTERN
;
453 } else if (s
->type
.ref
== func_old_type
.ref
) {
454 s
->type
.ref
= type
->ref
;
455 s
->r
= r
| VT_CONST
| VT_SYM
;
456 s
->type
.t
|= VT_EXTERN
;
457 } else if (!is_compatible_types(&s
->type
, type
)) {
458 tcc_error("incompatible types for redefinition of '%s'",
459 get_tok_str(v
, NULL
));
461 /* Merge some storage attributes. */
462 if (type
->t
& VT_WEAK
)
465 if (type
->t
& VT_VIS_MASK
)
466 apply_visibility(s
, type
);
471 /* push a reference to global symbol v */
472 ST_FUNC
void vpush_global_sym(CType
*type
, int v
)
474 vpushsym(type
, external_global_sym(v
, type
, 0));
477 ST_FUNC
void vset(CType
*type
, int r
, int v
)
482 vsetc(type
, r
, &cval
);
485 static void vseti(int r
, int v
)
493 ST_FUNC
void vswap(void)
496 /* cannot let cpu flags if other instruction are generated. Also
497 avoid leaving VT_JMP anywhere except on the top of the stack
498 because it would complicate the code generator. */
499 if (vtop
>= vstack
) {
500 int v
= vtop
->r
& VT_VALMASK
;
501 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
508 /* XXX: +2% overall speed possible with optimized memswap
510 * memswap(&vtop[0], &vtop[1], sizeof *vtop);
514 ST_FUNC
void vpushv(SValue
*v
)
516 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
517 tcc_error("memory full (vstack)");
522 static void vdup(void)
527 /* save r to the memory stack, and mark it as being free */
528 ST_FUNC
void save_reg(int r
)
530 int l
, saved
, size
, align
;
534 /* modify all stack values */
537 for(p
=vstack
;p
<=vtop
;p
++) {
538 if ((p
->r
& VT_VALMASK
) == r
||
539 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
540 /* must save value on stack if not already done */
542 /* NOTE: must reload 'r' because r might be equal to r2 */
543 r
= p
->r
& VT_VALMASK
;
544 /* store register in the stack */
546 if ((p
->r
& VT_LVAL
) ||
547 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
548 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
549 type
= &char_pointer_type
;
553 size
= type_size(type
, &align
);
554 loc
= (loc
- size
) & -align
;
556 sv
.r
= VT_LOCAL
| VT_LVAL
;
559 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
560 /* x86 specific: need to pop fp register ST0 if saved */
562 o(0xd8dd); /* fstp %st(0) */
565 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
566 /* special long long case */
567 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
575 /* mark that stack entry as being saved on the stack */
576 if (p
->r
& VT_LVAL
) {
577 /* also clear the bounded flag because the
578 relocation address of the function was stored in
580 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
582 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
590 #ifdef TCC_TARGET_ARM
591 /* find a register of class 'rc2' with at most one reference on stack.
592 * If none, call get_reg(rc) */
593 ST_FUNC
int get_reg_ex(int rc
, int rc2
)
598 for(r
=0;r
<NB_REGS
;r
++) {
599 if (reg_classes
[r
] & rc2
) {
602 for(p
= vstack
; p
<= vtop
; p
++) {
603 if ((p
->r
& VT_VALMASK
) == r
||
604 (p
->r2
& VT_VALMASK
) == r
)
615 /* find a free register of class 'rc'. If none, save one register */
616 ST_FUNC
int get_reg(int rc
)
621 /* find a free register */
622 for(r
=0;r
<NB_REGS
;r
++) {
623 if (reg_classes
[r
] & rc
) {
624 for(p
=vstack
;p
<=vtop
;p
++) {
625 if ((p
->r
& VT_VALMASK
) == r
||
626 (p
->r2
& VT_VALMASK
) == r
)
634 /* no register left : free the first one on the stack (VERY
635 IMPORTANT to start from the bottom to ensure that we don't
636 spill registers used in gen_opi()) */
637 for(p
=vstack
;p
<=vtop
;p
++) {
638 /* look at second register (if long long) */
639 r
= p
->r2
& VT_VALMASK
;
640 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
642 r
= p
->r
& VT_VALMASK
;
643 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
649 /* Should never comes here */
653 /* save registers up to (vtop - n) stack entry */
654 ST_FUNC
void save_regs(int n
)
659 for(p
= vstack
;p
<= p1
; p
++) {
660 r
= p
->r
& VT_VALMASK
;
667 /* move register 's' (of type 't') to 'r', and flush previous value of r to memory
669 static void move_reg(int r
, int s
, int t
)
683 /* get address of vtop (vtop MUST BE an lvalue) */
684 ST_FUNC
void gaddrof(void)
686 if (vtop
->r
& VT_REF
&& !nocode_wanted
)
689 /* tricky: if saved lvalue, then we can go back to lvalue */
690 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
691 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
696 #ifdef CONFIG_TCC_BCHECK
697 /* generate lvalue bound code */
698 static void gbound(void)
703 vtop
->r
&= ~VT_MUSTBOUND
;
704 /* if lvalue, then use checking code before dereferencing */
705 if (vtop
->r
& VT_LVAL
) {
706 /* if not VT_BOUNDED value, then make one */
707 if (!(vtop
->r
& VT_BOUNDED
)) {
708 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
709 /* must save type because we must set it to int to get pointer */
711 vtop
->type
.t
= VT_INT
;
714 gen_bounded_ptr_add();
715 vtop
->r
|= lval_type
;
718 /* then check for dereferencing */
719 gen_bounded_ptr_deref();
724 /* store vtop a register belonging to class 'rc'. lvalues are
725 converted to values. Cannot be used if cannot be converted to
726 register value (such as structures). */
727 ST_FUNC
int gv(int rc
)
729 int r
, bit_pos
, bit_size
, size
, align
, i
;
732 /* NOTE: get_reg can modify vstack[] */
733 if (vtop
->type
.t
& VT_BITFIELD
) {
736 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
737 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
738 /* remove bit field info to avoid loops */
739 vtop
->type
.t
&= ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
740 /* cast to int to propagate signedness in following ops */
741 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
746 if((vtop
->type
.t
& VT_UNSIGNED
) ||
747 (vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
748 type
.t
|= VT_UNSIGNED
;
750 /* generate shifts */
751 vpushi(bits
- (bit_pos
+ bit_size
));
753 vpushi(bits
- bit_size
);
754 /* NOTE: transformed to SHR if unsigned */
758 if (is_float(vtop
->type
.t
) &&
759 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
762 unsigned long offset
;
763 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
767 /* XXX: unify with initializers handling ? */
768 /* CPUs usually cannot use float constants, so we store them
769 generically in data segment */
770 size
= type_size(&vtop
->type
, &align
);
771 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
772 data_section
->data_offset
= offset
;
773 /* XXX: not portable yet */
774 #if defined(__i386__) || defined(__x86_64__)
775 /* Zero pad x87 tenbyte long doubles */
776 if (size
== LDOUBLE_SIZE
) {
777 vtop
->c
.tab
[2] &= 0xffff;
778 #if LDOUBLE_SIZE == 16
783 ptr
= section_ptr_add(data_section
, size
);
785 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
789 ptr
[i
] = vtop
->c
.tab
[size
-1-i
];
793 ptr
[i
] = vtop
->c
.tab
[i
];
794 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
795 vtop
->r
|= VT_LVAL
| VT_SYM
;
797 vtop
->c
.ptr_offset
= 0;
799 #ifdef CONFIG_TCC_BCHECK
800 if (vtop
->r
& VT_MUSTBOUND
)
804 r
= vtop
->r
& VT_VALMASK
;
805 rc2
= (rc
& RC_FLOAT
) ? RC_FLOAT
: RC_INT
;
806 #ifndef TCC_TARGET_ARM64
809 #ifdef TCC_TARGET_X86_64
810 else if (rc
== RC_FRET
)
815 /* need to reload if:
817 - lvalue (need to dereference pointer)
818 - already a register, but not in the right class */
820 || (vtop
->r
& VT_LVAL
)
821 || !(reg_classes
[r
] & rc
)
822 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
823 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
824 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
&& !(reg_classes
[vtop
->r2
] & rc2
))
826 || ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
831 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
832 if (((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
)) {
833 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
835 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
836 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
837 unsigned long long ll
;
839 int r2
, original_type
;
840 original_type
= vtop
->type
.t
;
841 /* two register type load : expand to two words
843 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
844 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
847 vtop
->c
.ui
= ll
; /* first word */
849 vtop
->r
= r
; /* save register value */
850 vpushi(ll
>> 32); /* second word */
853 if (r
>= VT_CONST
|| /* XXX: test to VT_CONST incorrect ? */
854 (vtop
->r
& VT_LVAL
)) {
855 /* We do not want to modifier the long long
856 pointer here, so the safest (and less
857 efficient) is to save all the other registers
858 in the stack. XXX: totally inefficient. */
860 /* load from memory */
861 vtop
->type
.t
= load_type
;
864 vtop
[-1].r
= r
; /* save register value */
865 /* increment pointer to get second word */
866 vtop
->type
.t
= addr_type
;
871 vtop
->type
.t
= load_type
;
876 vtop
[-1].r
= r
; /* save register value */
877 vtop
->r
= vtop
[-1].r2
;
879 /* Allocate second register. Here we rely on the fact that
880 get_reg() tries first to free r2 of an SValue. */
884 /* write second register */
886 vtop
->type
.t
= original_type
;
887 } else if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
889 /* lvalue of scalar type : need to use lvalue type
890 because of possible cast */
893 /* compute memory access type */
894 if (vtop
->r
& VT_REF
)
895 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
900 else if (vtop
->r
& VT_LVAL_BYTE
)
902 else if (vtop
->r
& VT_LVAL_SHORT
)
904 if (vtop
->r
& VT_LVAL_UNSIGNED
)
908 /* restore wanted type */
911 /* one register type load */
916 #ifdef TCC_TARGET_C67
917 /* uses register pairs for doubles */
918 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
925 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
926 ST_FUNC
void gv2(int rc1
, int rc2
)
930 /* generate more generic register first. But VT_JMP or VT_CMP
931 values must be generated first in all cases to avoid possible
933 v
= vtop
[0].r
& VT_VALMASK
;
934 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
939 /* test if reload is needed for first register */
940 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
950 /* test if reload is needed for first register */
951 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
957 #ifndef TCC_TARGET_ARM64
958 /* wrapper around RC_FRET to return a register by type */
959 static int rc_fret(int t
)
961 #ifdef TCC_TARGET_X86_64
962 if (t
== VT_LDOUBLE
) {
970 /* wrapper around REG_FRET to return a register by type */
971 static int reg_fret(int t
)
973 #ifdef TCC_TARGET_X86_64
974 if (t
== VT_LDOUBLE
) {
981 /* expand long long on stack in two int registers */
982 static void lexpand(void)
986 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
989 vtop
[0].r
= vtop
[-1].r2
;
990 vtop
[0].r2
= VT_CONST
;
991 vtop
[-1].r2
= VT_CONST
;
992 vtop
[0].type
.t
= VT_INT
| u
;
993 vtop
[-1].type
.t
= VT_INT
| u
;
996 #ifdef TCC_TARGET_ARM
997 /* expand long long on stack */
998 ST_FUNC
void lexpand_nr(void)
1002 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
1004 vtop
->r2
= VT_CONST
;
1005 vtop
->type
.t
= VT_INT
| u
;
1006 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
1007 if (v
== VT_CONST
) {
1008 vtop
[-1].c
.ui
= vtop
->c
.ull
;
1009 vtop
->c
.ui
= vtop
->c
.ull
>> 32;
1011 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
1013 vtop
->r
= vtop
[-1].r
;
1014 } else if (v
> VT_CONST
) {
1018 vtop
->r
= vtop
[-1].r2
;
1019 vtop
[-1].r2
= VT_CONST
;
1020 vtop
[-1].type
.t
= VT_INT
| u
;
1024 /* build a long long from two ints */
1025 static void lbuild(int t
)
1027 gv2(RC_INT
, RC_INT
);
1028 vtop
[-1].r2
= vtop
[0].r
;
1029 vtop
[-1].type
.t
= t
;
1033 /* rotate n first stack elements to the bottom
1034 I1 ... In -> I2 ... In I1 [top is right]
1036 ST_FUNC
void vrotb(int n
)
1042 for(i
=-n
+1;i
!=0;i
++)
1043 vtop
[i
] = vtop
[i
+1];
1047 /* rotate the n elements before entry e towards the top
1048 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
1050 ST_FUNC
void vrote(SValue
*e
, int n
)
1056 for(i
= 0;i
< n
- 1; i
++)
1061 /* rotate n first stack elements to the top
1062 I1 ... In -> In I1 ... I(n-1) [top is right]
1064 ST_FUNC
void vrott(int n
)
1069 /* pop stack value */
1070 ST_FUNC
void vpop(void)
1073 v
= vtop
->r
& VT_VALMASK
;
1074 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1075 /* for x86, we need to pop the FP stack */
1076 if (v
== TREG_ST0
&& !nocode_wanted
) {
1077 o(0xd8dd); /* fstp %st(0) */
1080 if (v
== VT_JMP
|| v
== VT_JMPI
) {
1081 /* need to put correct jump if && or || without test */
1087 /* convert stack entry to register and duplicate its value in another
1089 static void gv_dup(void)
1095 if ((t
& VT_BTYPE
) == VT_LLONG
) {
1102 /* stack: H L L1 H1 */
1110 /* duplicate value */
1115 #ifdef TCC_TARGET_X86_64
1116 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1126 load(r1
, &sv
); /* move r to r1 */
1128 /* duplicates value */
1134 /* Generate value test
1136 * Generate a test for any value (jump, comparison and integers) */
1137 ST_FUNC
int gvtst(int inv
, int t
)
1139 int v
= vtop
->r
& VT_VALMASK
;
1140 if (v
!= VT_CMP
&& v
!= VT_JMP
&& v
!= VT_JMPI
) {
1144 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1145 /* constant jmp optimization */
1146 if ((vtop
->c
.i
!= 0) != inv
)
1151 return gtst(inv
, t
);
1154 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
1155 /* generate CPU independent (unsigned) long long operations */
1156 static void gen_opl(int op
)
1158 int t
, a
, b
, op1
, c
, i
;
1160 unsigned short reg_iret
= REG_IRET
;
1161 unsigned short reg_lret
= REG_LRET
;
1167 func
= TOK___divdi3
;
1170 func
= TOK___udivdi3
;
1173 func
= TOK___moddi3
;
1176 func
= TOK___umoddi3
;
1183 /* call generic long long function */
1184 vpush_global_sym(&func_old_type
, func
);
1189 vtop
->r2
= reg_lret
;
1202 /* stack: L1 H1 L2 H2 */
1207 vtop
[-2] = vtop
[-3];
1210 /* stack: H1 H2 L1 L2 */
1216 /* stack: H1 H2 L1 L2 ML MH */
1219 /* stack: ML MH H1 H2 L1 L2 */
1223 /* stack: ML MH H1 L2 H2 L1 */
1228 /* stack: ML MH M1 M2 */
1231 } else if (op
== '+' || op
== '-') {
1232 /* XXX: add non carry method too (for MIPS or alpha) */
1238 /* stack: H1 H2 (L1 op L2) */
1241 gen_op(op1
+ 1); /* TOK_xxxC2 */
1244 /* stack: H1 H2 (L1 op L2) */
1247 /* stack: (L1 op L2) H1 H2 */
1249 /* stack: (L1 op L2) (H1 op H2) */
1257 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1258 t
= vtop
[-1].type
.t
;
1262 /* stack: L H shift */
1264 /* constant: simpler */
1265 /* NOTE: all comments are for SHL. the other cases are
1266 done by swaping words */
1277 if (op
!= TOK_SAR
) {
1310 /* XXX: should provide a faster fallback on x86 ? */
1313 func
= TOK___ashrdi3
;
1316 func
= TOK___lshrdi3
;
1319 func
= TOK___ashldi3
;
1325 /* compare operations */
1331 /* stack: L1 H1 L2 H2 */
1333 vtop
[-1] = vtop
[-2];
1335 /* stack: L1 L2 H1 H2 */
1338 /* when values are equal, we need to compare low words. since
1339 the jump is inverted, we invert the test too. */
1342 else if (op1
== TOK_GT
)
1344 else if (op1
== TOK_ULT
)
1346 else if (op1
== TOK_UGT
)
1351 if (op1
!= TOK_NE
) {
1355 /* generate non equal test */
1356 /* XXX: NOT PORTABLE yet */
1360 #if defined(TCC_TARGET_I386)
1361 b
= psym(0x850f, 0);
1362 #elif defined(TCC_TARGET_ARM)
1364 o(0x1A000000 | encbranch(ind
, 0, 1));
1365 #elif defined(TCC_TARGET_C67) || defined(TCC_TARGET_ARM64)
1366 tcc_error("not implemented");
1368 #error not supported
1372 /* compare low. Always unsigned */
1376 else if (op1
== TOK_LE
)
1378 else if (op1
== TOK_GT
)
1380 else if (op1
== TOK_GE
)
1391 /* handle integer constant optimizations and various machine
1393 static void gen_opic(int op
)
1395 int c1
, c2
, t1
, t2
, n
;
1398 typedef unsigned long long U
;
1402 t1
= v1
->type
.t
& VT_BTYPE
;
1403 t2
= v2
->type
.t
& VT_BTYPE
;
1407 else if (v1
->type
.t
& VT_UNSIGNED
)
1414 else if (v2
->type
.t
& VT_UNSIGNED
)
1419 /* currently, we cannot do computations with forward symbols */
1420 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1421 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1424 case '+': l1
+= l2
; break;
1425 case '-': l1
-= l2
; break;
1426 case '&': l1
&= l2
; break;
1427 case '^': l1
^= l2
; break;
1428 case '|': l1
|= l2
; break;
1429 case '*': l1
*= l2
; break;
1436 /* if division by zero, generate explicit division */
1439 tcc_error("division by zero in constant");
1443 default: l1
/= l2
; break;
1444 case '%': l1
%= l2
; break;
1445 case TOK_UDIV
: l1
= (U
)l1
/ l2
; break;
1446 case TOK_UMOD
: l1
= (U
)l1
% l2
; break;
1449 case TOK_SHL
: l1
<<= l2
; break;
1450 case TOK_SHR
: l1
= (U
)l1
>> l2
; break;
1451 case TOK_SAR
: l1
>>= l2
; break;
1453 case TOK_ULT
: l1
= (U
)l1
< (U
)l2
; break;
1454 case TOK_UGE
: l1
= (U
)l1
>= (U
)l2
; break;
1455 case TOK_EQ
: l1
= l1
== l2
; break;
1456 case TOK_NE
: l1
= l1
!= l2
; break;
1457 case TOK_ULE
: l1
= (U
)l1
<= (U
)l2
; break;
1458 case TOK_UGT
: l1
= (U
)l1
> (U
)l2
; break;
1459 case TOK_LT
: l1
= l1
< l2
; break;
1460 case TOK_GE
: l1
= l1
>= l2
; break;
1461 case TOK_LE
: l1
= l1
<= l2
; break;
1462 case TOK_GT
: l1
= l1
> l2
; break;
1464 case TOK_LAND
: l1
= l1
&& l2
; break;
1465 case TOK_LOR
: l1
= l1
|| l2
; break;
1472 /* if commutative ops, put c2 as constant */
1473 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1474 op
== '|' || op
== '*')) {
1476 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1477 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1479 if (!const_wanted
&&
1481 (op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
)) ||
1482 (l1
== -1 && op
== TOK_SAR
))) {
1483 /* treat (0 << x), (0 >> x) and (-1 >> x) as constant */
1485 } else if (!const_wanted
&&
1486 c2
&& ((l2
== 0 && (op
== '&' || op
== '*')) ||
1487 (l2
== -1 && op
== '|') ||
1488 (l2
== 0xffffffff && t2
!= VT_LLONG
&& op
== '|') ||
1489 (l2
== 1 && (op
== '%' || op
== TOK_UMOD
)))) {
1490 /* treat (x & 0), (x * 0), (x | -1) and (x % 1) as constant */
1495 } else if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1498 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1499 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1503 /* filter out NOP operations like x*1, x-0, x&-1... */
1505 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1506 /* try to use shifts instead of muls or divs */
1507 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1516 else if (op
== TOK_PDIV
)
1522 } else if (c2
&& (op
== '+' || op
== '-') &&
1523 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
1524 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1525 /* symbol + constant case */
1532 if (!nocode_wanted
) {
1533 /* call low level op generator */
1534 if (t1
== VT_LLONG
|| t2
== VT_LLONG
||
1535 (PTR_SIZE
== 8 && (t1
== VT_PTR
|| t2
== VT_PTR
)))
1546 /* generate a floating point operation with constant propagation */
1547 static void gen_opif(int op
)
1555 /* currently, we cannot do computations with forward symbols */
1556 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1557 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1559 if (v1
->type
.t
== VT_FLOAT
) {
1562 } else if (v1
->type
.t
== VT_DOUBLE
) {
1570 /* NOTE: we only do constant propagation if finite number (not
1571 NaN or infinity) (ANSI spec) */
1572 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1576 case '+': f1
+= f2
; break;
1577 case '-': f1
-= f2
; break;
1578 case '*': f1
*= f2
; break;
1582 tcc_error("division by zero in constant");
1587 /* XXX: also handles tests ? */
1591 /* XXX: overflow test ? */
1592 if (v1
->type
.t
== VT_FLOAT
) {
1594 } else if (v1
->type
.t
== VT_DOUBLE
) {
1602 if (!nocode_wanted
) {
1610 static int pointed_size(CType
*type
)
1613 return type_size(pointed_type(type
), &align
);
1616 static void vla_runtime_pointed_size(CType
*type
)
1619 vla_runtime_type_size(pointed_type(type
), &align
);
1622 static inline int is_null_pointer(SValue
*p
)
1624 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1626 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& p
->c
.i
== 0) ||
1627 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.ll
== 0) ||
1628 ((p
->type
.t
& VT_BTYPE
) == VT_PTR
&& p
->c
.ptr_offset
== 0);
1631 static inline int is_integer_btype(int bt
)
1633 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
1634 bt
== VT_INT
|| bt
== VT_LLONG
);
1637 /* check types for comparison or subtraction of pointers */
1638 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
1640 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
1643 /* null pointers are accepted for all comparisons as gcc */
1644 if (is_null_pointer(p1
) || is_null_pointer(p2
))
1648 bt1
= type1
->t
& VT_BTYPE
;
1649 bt2
= type2
->t
& VT_BTYPE
;
1650 /* accept comparison between pointer and integer with a warning */
1651 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
1652 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
1653 tcc_warning("comparison between pointer and integer");
1657 /* both must be pointers or implicit function pointers */
1658 if (bt1
== VT_PTR
) {
1659 type1
= pointed_type(type1
);
1660 } else if (bt1
!= VT_FUNC
)
1661 goto invalid_operands
;
1663 if (bt2
== VT_PTR
) {
1664 type2
= pointed_type(type2
);
1665 } else if (bt2
!= VT_FUNC
) {
1667 tcc_error("invalid operands to binary %s", get_tok_str(op
, NULL
));
1669 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1670 (type2
->t
& VT_BTYPE
) == VT_VOID
)
1674 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1675 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1676 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1677 /* gcc-like error if '-' is used */
1679 goto invalid_operands
;
1681 tcc_warning("comparison of distinct pointer types lacks a cast");
1685 /* generic gen_op: handles types problems */
1686 ST_FUNC
void gen_op(int op
)
1688 int u
, t1
, t2
, bt1
, bt2
, t
;
1691 t1
= vtop
[-1].type
.t
;
1692 t2
= vtop
[0].type
.t
;
1693 bt1
= t1
& VT_BTYPE
;
1694 bt2
= t2
& VT_BTYPE
;
1696 if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
1697 /* at least one operand is a pointer */
1698 /* relationnal op: must be both pointers */
1699 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
1700 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1701 /* pointers are handled are unsigned */
1702 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1703 t
= VT_LLONG
| VT_UNSIGNED
;
1705 t
= VT_INT
| VT_UNSIGNED
;
1709 /* if both pointers, then it must be the '-' op */
1710 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
1712 tcc_error("cannot use pointers here");
1713 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1714 /* XXX: check that types are compatible */
1715 if (vtop
[-1].type
.t
& VT_VLA
) {
1716 vla_runtime_pointed_size(&vtop
[-1].type
);
1718 vpushi(pointed_size(&vtop
[-1].type
));
1722 /* set to integer type */
1723 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1724 vtop
->type
.t
= VT_LLONG
;
1726 vtop
->type
.t
= VT_INT
;
1731 /* exactly one pointer : must be '+' or '-'. */
1732 if (op
!= '-' && op
!= '+')
1733 tcc_error("cannot use pointers here");
1734 /* Put pointer as first operand */
1735 if (bt2
== VT_PTR
) {
1739 type1
= vtop
[-1].type
;
1740 type1
.t
&= ~VT_ARRAY
;
1741 if (vtop
[-1].type
.t
& VT_VLA
)
1742 vla_runtime_pointed_size(&vtop
[-1].type
);
1744 u
= pointed_size(&vtop
[-1].type
);
1746 tcc_error("unknown array element size");
1747 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1750 /* XXX: cast to int ? (long long case) */
1755 #ifdef CONFIG_TCC_BCHECK
1756 /* if evaluating constant expression, no code should be
1757 generated, so no bound check */
1758 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
1759 /* if bounded pointers, we generate a special code to
1766 gen_bounded_ptr_add();
1772 /* put again type if gen_opic() swaped operands */
1775 } else if (is_float(bt1
) || is_float(bt2
)) {
1776 /* compute bigger type and do implicit casts */
1777 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
1779 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
1784 /* floats can only be used for a few operations */
1785 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
1786 (op
< TOK_ULT
|| op
> TOK_GT
))
1787 tcc_error("invalid operands for binary operation");
1789 } else if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
) {
1790 t
= bt1
== VT_LLONG
? VT_LLONG
: VT_INT
;
1791 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (t
| VT_UNSIGNED
))
1794 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
1795 /* cast to biggest op */
1797 /* convert to unsigned if it does not fit in a long long */
1798 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
1799 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
1802 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
1803 tcc_error("comparison of struct");
1805 /* integer operations */
1807 /* convert to unsigned if it does not fit in an integer */
1808 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
1809 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
1812 /* XXX: currently, some unsigned operations are explicit, so
1813 we modify them here */
1814 if (t
& VT_UNSIGNED
) {
1821 else if (op
== TOK_LT
)
1823 else if (op
== TOK_GT
)
1825 else if (op
== TOK_LE
)
1827 else if (op
== TOK_GE
)
1834 /* special case for shifts and long long: we keep the shift as
1836 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
1843 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
1844 /* relationnal op: the result is an int */
1845 vtop
->type
.t
= VT_INT
;
1850 // Make sure that we have converted to an rvalue:
1851 if (vtop
->r
& VT_LVAL
&& !nocode_wanted
)
1852 gv(is_float(vtop
->type
.t
& VT_BTYPE
) ? RC_FLOAT
: RC_INT
);
1855 #ifndef TCC_TARGET_ARM
1856 /* generic itof for unsigned long long case */
1857 static void gen_cvt_itof1(int t
)
1859 #ifdef TCC_TARGET_ARM64
1862 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
1863 (VT_LLONG
| VT_UNSIGNED
)) {
1866 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
1867 #if LDOUBLE_SIZE != 8
1868 else if (t
== VT_LDOUBLE
)
1869 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
1872 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
1876 vtop
->r
= reg_fret(t
);
1884 /* generic ftoi for unsigned long long case */
1885 static void gen_cvt_ftoi1(int t
)
1887 #ifdef TCC_TARGET_ARM64
1892 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
1893 /* not handled natively */
1894 st
= vtop
->type
.t
& VT_BTYPE
;
1896 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
1897 #if LDOUBLE_SIZE != 8
1898 else if (st
== VT_LDOUBLE
)
1899 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
1902 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
1907 vtop
->r2
= REG_LRET
;
1914 /* force char or short cast */
1915 static void force_charshort_cast(int t
)
1919 /* XXX: add optimization if lvalue : just change type and offset */
1924 if (t
& VT_UNSIGNED
) {
1925 vpushi((1 << bits
) - 1);
1931 /* result must be signed or the SAR is converted to an SHL
1932 This was not the case when "t" was a signed short
1933 and the last value on the stack was an unsigned int */
1934 vtop
->type
.t
&= ~VT_UNSIGNED
;
1940 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1941 static void gen_cast(CType
*type
)
1943 int sbt
, dbt
, sf
, df
, c
, p
;
1945 /* special delayed cast for char/short */
1946 /* XXX: in some cases (multiple cascaded casts), it may still
1948 if (vtop
->r
& VT_MUSTCAST
) {
1949 vtop
->r
&= ~VT_MUSTCAST
;
1950 force_charshort_cast(vtop
->type
.t
);
1953 /* bitfields first get cast to ints */
1954 if (vtop
->type
.t
& VT_BITFIELD
&& !nocode_wanted
) {
1958 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
1959 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
1964 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1965 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
1967 /* constant case: we can do it now */
1968 /* XXX: in ISOC, cannot do it if error in convert */
1969 if (sbt
== VT_FLOAT
)
1970 vtop
->c
.ld
= vtop
->c
.f
;
1971 else if (sbt
== VT_DOUBLE
)
1972 vtop
->c
.ld
= vtop
->c
.d
;
1975 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
1976 if (sbt
& VT_UNSIGNED
)
1977 vtop
->c
.ld
= vtop
->c
.ull
;
1979 vtop
->c
.ld
= vtop
->c
.ll
;
1981 if (sbt
& VT_UNSIGNED
)
1982 vtop
->c
.ld
= vtop
->c
.ui
;
1984 vtop
->c
.ld
= vtop
->c
.i
;
1987 if (dbt
== VT_FLOAT
)
1988 vtop
->c
.f
= (float)vtop
->c
.ld
;
1989 else if (dbt
== VT_DOUBLE
)
1990 vtop
->c
.d
= (double)vtop
->c
.ld
;
1991 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
1992 vtop
->c
.ull
= (unsigned long long)vtop
->c
.ld
;
1993 } else if (sf
&& dbt
== VT_BOOL
) {
1994 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
1997 vtop
->c
.ll
= (long long)vtop
->c
.ld
;
1998 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
1999 vtop
->c
.ll
= vtop
->c
.ull
;
2000 else if (sbt
& VT_UNSIGNED
)
2001 vtop
->c
.ll
= vtop
->c
.ui
;
2002 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2003 else if (sbt
== VT_PTR
)
2006 else if (sbt
!= VT_LLONG
)
2007 vtop
->c
.ll
= vtop
->c
.i
;
2009 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
2010 vtop
->c
.ull
= vtop
->c
.ll
;
2011 else if (dbt
== VT_BOOL
)
2012 vtop
->c
.i
= (vtop
->c
.ll
!= 0);
2013 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2014 else if (dbt
== VT_PTR
)
2017 else if (dbt
!= VT_LLONG
) {
2019 if ((dbt
& VT_BTYPE
) == VT_BYTE
)
2021 else if ((dbt
& VT_BTYPE
) == VT_SHORT
)
2023 if(dbt
& VT_UNSIGNED
)
2024 vtop
->c
.ui
= ((unsigned int)vtop
->c
.ll
<< s
) >> s
;
2026 vtop
->c
.i
= ((int)vtop
->c
.ll
<< s
) >> s
;
2029 } else if (p
&& dbt
== VT_BOOL
) {
2032 } else if (!nocode_wanted
) {
2033 /* non constant case: generate code */
2035 /* convert from fp to fp */
2038 /* convert int to fp */
2041 /* convert fp to int */
2042 if (dbt
== VT_BOOL
) {
2046 /* we handle char/short/etc... with generic code */
2047 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
2048 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
2052 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
2053 /* additional cast for char/short... */
2058 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
2059 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
2060 if ((sbt
& VT_BTYPE
) != VT_LLONG
&& !nocode_wanted
) {
2061 /* scalar to long long */
2062 /* machine independent conversion */
2064 /* generate high word */
2065 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
2069 if (sbt
== VT_PTR
) {
2070 /* cast from pointer to int before we apply
2071 shift operation, which pointers don't support*/
2072 gen_cast(&int_type
);
2078 /* patch second register */
2079 vtop
[-1].r2
= vtop
->r
;
2083 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
2084 (dbt
& VT_BTYPE
) == VT_PTR
||
2085 (dbt
& VT_BTYPE
) == VT_FUNC
) {
2086 if ((sbt
& VT_BTYPE
) != VT_LLONG
&&
2087 (sbt
& VT_BTYPE
) != VT_PTR
&&
2088 (sbt
& VT_BTYPE
) != VT_FUNC
&& !nocode_wanted
) {
2089 /* need to convert from 32bit to 64bit */
2091 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
2092 #if defined(TCC_TARGET_ARM64)
2094 #elif defined(TCC_TARGET_X86_64)
2096 /* x86_64 specific: movslq */
2098 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
2105 } else if (dbt
== VT_BOOL
) {
2106 /* scalar to bool */
2109 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
2110 (dbt
& VT_BTYPE
) == VT_SHORT
) {
2111 if (sbt
== VT_PTR
) {
2112 vtop
->type
.t
= VT_INT
;
2113 tcc_warning("nonportable conversion from pointer to char/short");
2115 force_charshort_cast(dbt
);
2116 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
2118 if (sbt
== VT_LLONG
&& !nocode_wanted
) {
2119 /* from long long: just take low order word */
2123 /* if lvalue and single word type, nothing to do because
2124 the lvalue already contains the real type size (see
2125 VT_LVAL_xxx constants) */
2128 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
2129 /* if we are casting between pointer types,
2130 we must update the VT_LVAL_xxx size */
2131 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
2132 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
2137 /* return type size as known at compile time. Put alignment at 'a' */
2138 ST_FUNC
int type_size(CType
*type
, int *a
)
2143 bt
= type
->t
& VT_BTYPE
;
2144 if (bt
== VT_STRUCT
) {
2149 } else if (bt
== VT_PTR
) {
2150 if (type
->t
& VT_ARRAY
) {
2154 ts
= type_size(&s
->type
, a
);
2156 if (ts
< 0 && s
->c
< 0)
2164 } else if (bt
== VT_LDOUBLE
) {
2166 return LDOUBLE_SIZE
;
2167 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
2168 #ifdef TCC_TARGET_I386
2169 #ifdef TCC_TARGET_PE
2174 #elif defined(TCC_TARGET_ARM)
2184 } else if (bt
== VT_INT
|| bt
== VT_ENUM
|| bt
== VT_FLOAT
) {
2187 } else if (bt
== VT_SHORT
) {
2190 } else if (bt
== VT_QLONG
|| bt
== VT_QFLOAT
) {
2194 /* char, void, function, _Bool */
2200 /* push type size as known at runtime time on top of value stack. Put
2202 ST_FUNC
void vla_runtime_type_size(CType
*type
, int *a
)
2204 if (type
->t
& VT_VLA
) {
2205 vset(&int_type
, VT_LOCAL
|VT_LVAL
, type
->ref
->c
);
2207 vpushi(type_size(type
, a
));
2211 static void vla_sp_save(void) {
2212 if (!(vla_flags
& VLA_SP_LOC_SET
)) {
2213 *vla_sp_loc
= (loc
-= PTR_SIZE
);
2214 vla_flags
|= VLA_SP_LOC_SET
;
2216 if (!(vla_flags
& VLA_SP_SAVED
)) {
2217 gen_vla_sp_save(*vla_sp_loc
);
2218 vla_flags
|= VLA_SP_SAVED
;
2222 /* return the pointed type of t */
2223 static inline CType
*pointed_type(CType
*type
)
2225 return &type
->ref
->type
;
2228 /* modify type so that its it is a pointer to type. */
2229 ST_FUNC
void mk_pointer(CType
*type
)
2232 s
= sym_push(SYM_FIELD
, type
, 0, -1);
2233 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
2237 /* compare function types. OLD functions match any new functions */
2238 static int is_compatible_func(CType
*type1
, CType
*type2
)
2244 if (!is_compatible_types(&s1
->type
, &s2
->type
))
2246 /* check func_call */
2247 if (s1
->a
.func_call
!= s2
->a
.func_call
)
2249 /* XXX: not complete */
2250 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
2254 while (s1
!= NULL
) {
2257 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
2267 /* return true if type1 and type2 are the same. If unqualified is
2268 true, qualifiers on the types are ignored.
2270 - enums are not checked as gcc __builtin_types_compatible_p ()
2272 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2276 t1
= type1
->t
& VT_TYPE
;
2277 t2
= type2
->t
& VT_TYPE
;
2279 /* strip qualifiers before comparing */
2280 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2281 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2283 /* Default Vs explicit signedness only matters for char */
2284 if ((t1
& VT_BTYPE
) != VT_BYTE
) {
2288 /* XXX: bitfields ? */
2291 /* test more complicated cases */
2292 bt1
= t1
& VT_BTYPE
;
2293 if (bt1
== VT_PTR
) {
2294 type1
= pointed_type(type1
);
2295 type2
= pointed_type(type2
);
2296 return is_compatible_types(type1
, type2
);
2297 } else if (bt1
== VT_STRUCT
) {
2298 return (type1
->ref
== type2
->ref
);
2299 } else if (bt1
== VT_FUNC
) {
2300 return is_compatible_func(type1
, type2
);
2306 /* return true if type1 and type2 are exactly the same (including
2309 static int is_compatible_types(CType
*type1
, CType
*type2
)
2311 return compare_types(type1
,type2
,0);
2314 /* return true if type1 and type2 are the same (ignoring qualifiers).
2316 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
2318 return compare_types(type1
,type2
,1);
2321 /* print a type. If 'varstr' is not NULL, then the variable is also
2322 printed in the type */
2324 /* XXX: add array and function pointers */
2325 static void type_to_str(char *buf
, int buf_size
,
2326 CType
*type
, const char *varstr
)
2333 t
= type
->t
& VT_TYPE
;
2336 if (t
& VT_CONSTANT
)
2337 pstrcat(buf
, buf_size
, "const ");
2338 if (t
& VT_VOLATILE
)
2339 pstrcat(buf
, buf_size
, "volatile ");
2340 if ((t
& (VT_DEFSIGN
| VT_UNSIGNED
)) == (VT_DEFSIGN
| VT_UNSIGNED
))
2341 pstrcat(buf
, buf_size
, "unsigned ");
2342 else if (t
& VT_DEFSIGN
)
2343 pstrcat(buf
, buf_size
, "signed ");
2373 tstr
= "long double";
2375 pstrcat(buf
, buf_size
, tstr
);
2379 if (bt
== VT_STRUCT
)
2383 pstrcat(buf
, buf_size
, tstr
);
2384 v
= type
->ref
->v
& ~SYM_STRUCT
;
2385 if (v
>= SYM_FIRST_ANOM
)
2386 pstrcat(buf
, buf_size
, "<anonymous>");
2388 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
2392 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
2393 pstrcat(buf
, buf_size
, "(");
2395 while (sa
!= NULL
) {
2396 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
2397 pstrcat(buf
, buf_size
, buf1
);
2400 pstrcat(buf
, buf_size
, ", ");
2402 pstrcat(buf
, buf_size
, ")");
2406 pstrcpy(buf1
, sizeof(buf1
), "*");
2408 pstrcat(buf1
, sizeof(buf1
), varstr
);
2409 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2413 pstrcat(buf
, buf_size
, " ");
2414 pstrcat(buf
, buf_size
, varstr
);
2419 /* verify type compatibility to store vtop in 'dt' type, and generate
2421 static void gen_assign_cast(CType
*dt
)
2423 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
2424 char buf1
[256], buf2
[256];
2427 st
= &vtop
->type
; /* source type */
2428 dbt
= dt
->t
& VT_BTYPE
;
2429 sbt
= st
->t
& VT_BTYPE
;
2430 if (sbt
== VT_VOID
|| dbt
== VT_VOID
) {
2431 if (sbt
== VT_VOID
&& dbt
== VT_VOID
)
2433 It is Ok if both are void
2439 gcc accepts this program
2442 tcc_error("cannot cast from/to void");
2444 if (dt
->t
& VT_CONSTANT
)
2445 tcc_warning("assignment of read-only location");
2448 /* special cases for pointers */
2449 /* '0' can also be a pointer */
2450 if (is_null_pointer(vtop
))
2452 /* accept implicit pointer to integer cast with warning */
2453 if (is_integer_btype(sbt
)) {
2454 tcc_warning("assignment makes pointer from integer without a cast");
2457 type1
= pointed_type(dt
);
2458 /* a function is implicitely a function pointer */
2459 if (sbt
== VT_FUNC
) {
2460 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
2461 !is_compatible_types(pointed_type(dt
), st
))
2462 tcc_warning("assignment from incompatible pointer type");
2467 type2
= pointed_type(st
);
2468 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2469 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
2470 /* void * can match anything */
2472 /* exact type match, except for unsigned */
2475 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
|
2477 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
|
2479 if (!is_compatible_types(&tmp_type1
, &tmp_type2
))
2480 tcc_warning("assignment from incompatible pointer type");
2482 /* check const and volatile */
2483 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
2484 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
2485 tcc_warning("assignment discards qualifiers from pointer target type");
2491 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
2492 tcc_warning("assignment makes integer from pointer without a cast");
2494 /* XXX: more tests */
2499 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2500 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2501 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2503 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
2504 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
2505 tcc_error("cannot cast '%s' to '%s'", buf1
, buf2
);
2513 /* store vtop in lvalue pushed on stack */
2514 ST_FUNC
void vstore(void)
2516 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
2518 ft
= vtop
[-1].type
.t
;
2519 sbt
= vtop
->type
.t
& VT_BTYPE
;
2520 dbt
= ft
& VT_BTYPE
;
2521 if ((((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2522 (sbt
== VT_INT
&& dbt
== VT_SHORT
))
2523 && !(vtop
->type
.t
& VT_BITFIELD
)) {
2524 /* optimize char/short casts */
2525 delayed_cast
= VT_MUSTCAST
;
2526 vtop
->type
.t
= ft
& (VT_TYPE
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
)));
2527 /* XXX: factorize */
2528 if (ft
& VT_CONSTANT
)
2529 tcc_warning("assignment of read-only location");
2532 if (!(ft
& VT_BITFIELD
))
2533 gen_assign_cast(&vtop
[-1].type
);
2536 if (sbt
== VT_STRUCT
) {
2537 /* if structure, only generate pointer */
2538 /* structure assignment : generate memcpy */
2539 /* XXX: optimize if small size */
2540 if (!nocode_wanted
) {
2541 size
= type_size(&vtop
->type
, &align
);
2545 vtop
->type
.t
= VT_PTR
;
2548 /* address of memcpy() */
2551 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2552 else if(!(align
& 3))
2553 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2556 vpush_global_sym(&func_old_type
, TOK_memcpy
);
2561 vtop
->type
.t
= VT_PTR
;
2570 /* leave source on stack */
2571 } else if (ft
& VT_BITFIELD
) {
2572 /* bitfield store handling */
2574 /* save lvalue as expression result (example: s.b = s.a = n;) */
2575 vdup(), vtop
[-1] = vtop
[-2];
2577 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2578 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2579 /* remove bit field info to avoid loops */
2580 vtop
[-1].type
.t
= ft
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
2582 if((ft
& VT_BTYPE
) == VT_BOOL
) {
2583 gen_cast(&vtop
[-1].type
);
2584 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2587 /* duplicate destination */
2589 vtop
[-1] = vtop
[-2];
2591 /* mask and shift source */
2592 if((ft
& VT_BTYPE
) != VT_BOOL
) {
2593 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2594 vpushll((1ULL << bit_size
) - 1ULL);
2596 vpushi((1 << bit_size
) - 1);
2602 /* load destination, mask and or with source */
2604 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2605 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2607 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2613 /* ... and discard */
2617 if (!nocode_wanted
) {
2618 #ifdef CONFIG_TCC_BCHECK
2619 /* bound check case */
2620 if (vtop
[-1].r
& VT_MUSTBOUND
) {
2629 #ifdef TCC_TARGET_X86_64
2630 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
2632 } else if ((ft
& VT_BTYPE
) == VT_QFLOAT
) {
2637 r
= gv(rc
); /* generate value */
2638 /* if lvalue was saved on stack, must read it */
2639 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
2641 t
= get_reg(RC_INT
);
2642 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2647 sv
.r
= VT_LOCAL
| VT_LVAL
;
2648 sv
.c
.ul
= vtop
[-1].c
.ul
;
2650 vtop
[-1].r
= t
| VT_LVAL
;
2652 /* two word case handling : store second register at word + 4 (or +8 for x86-64) */
2653 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2654 if (((ft
& VT_BTYPE
) == VT_QLONG
) || ((ft
& VT_BTYPE
) == VT_QFLOAT
)) {
2655 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
2657 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
2658 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
2660 vtop
[-1].type
.t
= load_type
;
2663 /* convert to int to increment easily */
2664 vtop
->type
.t
= addr_type
;
2670 vtop
[-1].type
.t
= load_type
;
2671 /* XXX: it works because r2 is spilled last ! */
2672 store(vtop
->r2
, vtop
- 1);
2678 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
2679 vtop
->r
|= delayed_cast
;
2683 /* post defines POST/PRE add. c is the token ++ or -- */
2684 ST_FUNC
void inc(int post
, int c
)
2687 vdup(); /* save lvalue */
2690 gv_dup(); /* duplicate value */
2692 vdup(); /* duplicate value */
2697 vpushi(c
- TOK_MID
);
2699 vstore(); /* store value */
2701 vpop(); /* if post op, return saved value */
2704 /* Parse GNUC __attribute__ extension. Currently, the following
2705 extensions are recognized:
2706 - aligned(n) : set data/function alignment.
2707 - packed : force data alignment to 1
2708 - section(x) : generate data/code in this section.
2709 - unused : currently ignored, but may be used someday.
2710 - regparm(n) : pass function parameters in registers (i386 only)
2712 static void parse_attribute(AttributeDef
*ad
)
2716 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
2720 while (tok
!= ')') {
2721 if (tok
< TOK_IDENT
)
2722 expect("attribute name");
2730 expect("section name");
2731 ad
->section
= find_section(tcc_state
, (char *)tokc
.cstr
->data
);
2739 expect("alias(\"target\")");
2740 ad
->alias_target
= /* save string as token, for later */
2741 tok_alloc((char*)tokc
.cstr
->data
, tokc
.cstr
->size
-1)->tok
;
2745 case TOK_VISIBILITY1
:
2746 case TOK_VISIBILITY2
:
2749 expect("visibility(\"default|hidden|internal|protected\")");
2750 if (!strcmp (tokc
.cstr
->data
, "default"))
2751 ad
->a
.visibility
= STV_DEFAULT
;
2752 else if (!strcmp (tokc
.cstr
->data
, "hidden"))
2753 ad
->a
.visibility
= STV_HIDDEN
;
2754 else if (!strcmp (tokc
.cstr
->data
, "internal"))
2755 ad
->a
.visibility
= STV_INTERNAL
;
2756 else if (!strcmp (tokc
.cstr
->data
, "protected"))
2757 ad
->a
.visibility
= STV_PROTECTED
;
2759 expect("visibility(\"default|hidden|internal|protected\")");
2768 if (n
<= 0 || (n
& (n
- 1)) != 0)
2769 tcc_error("alignment must be a positive power of two");
2786 /* currently, no need to handle it because tcc does not
2787 track unused objects */
2791 /* currently, no need to handle it because tcc does not
2792 track unused objects */
2797 ad
->a
.func_call
= FUNC_CDECL
;
2802 ad
->a
.func_call
= FUNC_STDCALL
;
2804 #ifdef TCC_TARGET_I386
2814 ad
->a
.func_call
= FUNC_FASTCALL1
+ n
- 1;
2820 ad
->a
.func_call
= FUNC_FASTCALLW
;
2827 ad
->a
.mode
= VT_LLONG
+ 1;
2830 ad
->a
.mode
= VT_SHORT
+ 1;
2833 ad
->a
.mode
= VT_INT
+ 1;
2836 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
2843 ad
->a
.func_export
= 1;
2846 ad
->a
.func_import
= 1;
2849 if (tcc_state
->warn_unsupported
)
2850 tcc_warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
2851 /* skip parameters */
2853 int parenthesis
= 0;
2857 else if (tok
== ')')
2860 } while (parenthesis
&& tok
!= -1);
2873 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2874 static void struct_decl(CType
*type
, int u
, int tdef
)
2876 int a
, v
, size
, align
, maxalign
, c
, offset
, flexible
;
2877 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
, prevbt
;
2878 Sym
*s
, *ss
, *ass
, **ps
;
2882 a
= tok
; /* save decl type */
2887 /* struct already defined ? return it */
2889 expect("struct/union/enum name");
2893 tcc_error("invalid type");
2895 } else if (tok
>= TOK_IDENT
&& !tdef
)
2896 tcc_error("unknown struct/union/enum");
2902 /* we put an undefined size for struct/union */
2903 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
2904 s
->r
= 0; /* default alignment is zero as gcc */
2905 /* put struct/union/enum name in type */
2913 tcc_error("struct/union/enum already defined");
2914 /* cannot be empty */
2916 /* non empty enums are not allowed */
2917 if (a
== TOK_ENUM
) {
2921 expect("identifier");
2923 if (ss
&& !local_stack
)
2924 tcc_error("redefinition of enumerator '%s'",
2925 get_tok_str(v
, NULL
));
2931 /* enum symbols have static storage */
2932 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
2933 ss
->type
.t
|= VT_STATIC
;
2938 /* NOTE: we accept a trailing comma */
2942 s
->c
= type_size(&int_type
, &align
);
2951 while (tok
!= '}') {
2952 parse_btype(&btype
, &ad
);
2955 tcc_error("flexible array member '%s' not at the end of struct",
2956 get_tok_str(v
, NULL
));
2961 type_decl(&type1
, &ad
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
2962 if (v
== 0 && (type1
.t
& VT_BTYPE
) != VT_STRUCT
)
2963 expect("identifier");
2964 if (type_size(&type1
, &align
) < 0) {
2965 if ((a
== TOK_STRUCT
) && (type1
.t
& VT_ARRAY
) && c
)
2968 tcc_error("field '%s' has incomplete type",
2969 get_tok_str(v
, NULL
));
2971 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
2972 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
2973 tcc_error("invalid type for '%s'",
2974 get_tok_str(v
, NULL
));
2978 bit_size
= expr_const();
2979 /* XXX: handle v = 0 case for messages */
2981 tcc_error("negative width in bit-field '%s'",
2982 get_tok_str(v
, NULL
));
2983 if (v
&& bit_size
== 0)
2984 tcc_error("zero width for bit-field '%s'",
2985 get_tok_str(v
, NULL
));
2987 size
= type_size(&type1
, &align
);
2989 if (align
< ad
.a
.aligned
)
2990 align
= ad
.a
.aligned
;
2991 } else if (ad
.a
.packed
) {
2993 } else if (*tcc_state
->pack_stack_ptr
) {
2994 if (align
> *tcc_state
->pack_stack_ptr
)
2995 align
= *tcc_state
->pack_stack_ptr
;
2998 if (bit_size
>= 0) {
2999 bt
= type1
.t
& VT_BTYPE
;
3006 tcc_error("bitfields must have scalar type");
3008 if (bit_size
> bsize
) {
3009 tcc_error("width of '%s' exceeds its type",
3010 get_tok_str(v
, NULL
));
3011 } else if (bit_size
== bsize
) {
3012 /* no need for bit fields */
3014 } else if (bit_size
== 0) {
3015 /* XXX: what to do if only padding in a
3017 /* zero size: means to pad */
3020 /* we do not have enough room ?
3021 did the type change?
3023 if ((bit_pos
+ bit_size
) > bsize
||
3024 bt
!= prevbt
|| a
== TOK_UNION
)
3027 /* XXX: handle LSB first */
3028 type1
.t
|= VT_BITFIELD
|
3029 (bit_pos
<< VT_STRUCT_SHIFT
) |
3030 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
3031 bit_pos
+= bit_size
;
3037 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
3038 /* add new memory data only if starting
3040 if (lbit_pos
== 0) {
3041 if (a
== TOK_STRUCT
) {
3042 c
= (c
+ align
- 1) & -align
;
3051 if (align
> maxalign
)
3055 printf("add field %s offset=%d",
3056 get_tok_str(v
, NULL
), offset
);
3057 if (type1
.t
& VT_BITFIELD
) {
3058 printf(" pos=%d size=%d",
3059 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
3060 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
3065 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
3067 while ((ass
= ass
->next
) != NULL
) {
3068 ss
= sym_push(ass
->v
, &ass
->type
, 0, offset
+ ass
->c
);
3073 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
3077 if (tok
== ';' || tok
== TOK_EOF
)
3084 /* store size and alignment */
3085 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
3091 /* return 1 if basic type is a type size (short, long, long long) */
3092 ST_FUNC
int is_btype_size(int bt
)
3094 return bt
== VT_SHORT
|| bt
== VT_LONG
|| bt
== VT_LLONG
;
3097 /* return 0 if no type declaration. otherwise, return the basic type
3100 static int parse_btype(CType
*type
, AttributeDef
*ad
)
3102 int t
, u
, bt_size
, complete
, type_found
, typespec_found
;
3106 memset(ad
, 0, sizeof(AttributeDef
));
3114 /* currently, we really ignore extension */
3125 tcc_error("too many basic types");
3127 bt_size
= is_btype_size (u
& VT_BTYPE
);
3128 if (u
== VT_INT
|| (!bt_size
&& !(t
& VT_TYPEDEF
)))
3143 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
3144 #ifndef TCC_TARGET_PE
3145 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3147 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
3148 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3154 #ifdef TCC_TARGET_ARM64
3156 /* GCC's __uint128_t appears in some Linux header files. Make it a
3157 synonym for long double to get the size and alignment right. */
3169 if ((t
& VT_BTYPE
) == VT_LONG
) {
3170 #ifdef TCC_TARGET_PE
3171 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
3173 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3181 struct_decl(&type1
, VT_ENUM
, t
& (VT_TYPEDEF
| VT_EXTERN
));
3184 type
->ref
= type1
.ref
;
3188 struct_decl(&type1
, VT_STRUCT
, t
& (VT_TYPEDEF
| VT_EXTERN
));
3191 /* type modifiers */
3207 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == (VT_DEFSIGN
|VT_UNSIGNED
))
3208 tcc_error("signed and unsigned modifier");
3221 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == VT_DEFSIGN
)
3222 tcc_error("signed and unsigned modifier");
3223 t
|= VT_DEFSIGN
| VT_UNSIGNED
;
3248 /* GNUC attribute */
3249 case TOK_ATTRIBUTE1
:
3250 case TOK_ATTRIBUTE2
:
3251 parse_attribute(ad
);
3254 t
= (t
& ~VT_BTYPE
) | u
;
3262 parse_expr_type(&type1
);
3263 /* remove all storage modifiers except typedef */
3264 type1
.t
&= ~(VT_STORAGE
&~VT_TYPEDEF
);
3270 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
3272 t
|= (s
->type
.t
& ~VT_TYPEDEF
);
3273 type
->ref
= s
->type
.ref
;
3275 /* get attributes from typedef */
3276 if (0 == ad
->a
.aligned
)
3277 ad
->a
.aligned
= s
->a
.aligned
;
3278 if (0 == ad
->a
.func_call
)
3279 ad
->a
.func_call
= s
->a
.func_call
;
3280 ad
->a
.packed
|= s
->a
.packed
;
3289 if (tcc_state
->char_is_unsigned
) {
3290 if ((t
& (VT_DEFSIGN
|VT_BTYPE
)) == VT_BYTE
)
3294 /* long is never used as type */
3295 if ((t
& VT_BTYPE
) == VT_LONG
)
3296 #if (!defined TCC_TARGET_X86_64 && !defined TCC_TARGET_ARM64) || \
3297 defined TCC_TARGET_PE
3298 t
= (t
& ~VT_BTYPE
) | VT_INT
;
3300 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3306 /* convert a function parameter type (array to pointer and function to
3307 function pointer) */
3308 static inline void convert_parameter_type(CType
*pt
)
3310 /* remove const and volatile qualifiers (XXX: const could be used
3311 to indicate a const function parameter */
3312 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3313 /* array must be transformed to pointer according to ANSI C */
3315 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
3320 ST_FUNC
void parse_asm_str(CString
*astr
)
3323 /* read the string */
3325 expect("string constant");
3327 while (tok
== TOK_STR
) {
3328 /* XXX: add \0 handling too ? */
3329 cstr_cat(astr
, tokc
.cstr
->data
);
3332 cstr_ccat(astr
, '\0');
3335 /* Parse an asm label and return the label
3336 * Don't forget to free the CString in the caller! */
3337 static void asm_label_instr(CString
*astr
)
3340 parse_asm_str(astr
);
3343 printf("asm_alias: \"%s\"\n", (char *)astr
->data
);
3347 static void post_type(CType
*type
, AttributeDef
*ad
)
3349 int n
, l
, t1
, arg_size
, align
;
3350 Sym
**plast
, *s
, *first
;
3355 /* function declaration */
3363 /* read param name and compute offset */
3364 if (l
!= FUNC_OLD
) {
3365 if (!parse_btype(&pt
, &ad1
)) {
3367 tcc_error("invalid type");
3374 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
3376 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3377 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
3378 tcc_error("parameter declared as void");
3379 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
3384 expect("identifier");
3388 convert_parameter_type(&pt
);
3389 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
3395 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
3402 /* if no parameters, then old type prototype */
3406 /* NOTE: const is ignored in returned type as it has a special
3407 meaning in gcc / C++ */
3408 type
->t
&= ~VT_CONSTANT
;
3409 /* some ancient pre-K&R C allows a function to return an array
3410 and the array brackets to be put after the arguments, such
3411 that "int c()[]" means something like "int[] c()" */
3414 skip(']'); /* only handle simple "[]" */
3417 /* we push a anonymous symbol which will contain the function prototype */
3418 ad
->a
.func_args
= arg_size
;
3419 s
= sym_push(SYM_FIELD
, type
, 0, l
);
3424 } else if (tok
== '[') {
3425 /* array definition */
3427 if (tok
== TOK_RESTRICT1
)
3432 if (!local_stack
|| nocode_wanted
)
3433 vpushi(expr_const());
3435 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3438 tcc_error("invalid array size");
3440 if (!is_integer_btype(vtop
->type
.t
& VT_BTYPE
))
3441 tcc_error("size of variable length array should be an integer");
3446 /* parse next post type */
3447 post_type(type
, ad
);
3448 if (type
->t
== VT_FUNC
)
3449 tcc_error("declaration of an array of functions");
3450 t1
|= type
->t
& VT_VLA
;
3453 loc
-= type_size(&int_type
, &align
);
3457 vla_runtime_type_size(type
, &align
);
3459 vset(&int_type
, VT_LOCAL
|VT_LVAL
, loc
);
3466 /* we push an anonymous symbol which will contain the array
3468 s
= sym_push(SYM_FIELD
, type
, 0, n
);
3469 type
->t
= (t1
? VT_VLA
: VT_ARRAY
) | VT_PTR
;
3474 /* Parse a type declaration (except basic type), and return the type
3475 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3476 expected. 'type' should contain the basic type. 'ad' is the
3477 attribute definition of the basic type. It can be modified by
3480 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
3483 CType type1
, *type2
;
3484 int qualifiers
, storage
;
3486 while (tok
== '*') {
3494 qualifiers
|= VT_CONSTANT
;
3499 qualifiers
|= VT_VOLATILE
;
3507 type
->t
|= qualifiers
;
3510 /* XXX: clarify attribute handling */
3511 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3512 parse_attribute(ad
);
3514 /* recursive type */
3515 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3516 type1
.t
= 0; /* XXX: same as int */
3519 /* XXX: this is not correct to modify 'ad' at this point, but
3520 the syntax is not clear */
3521 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3522 parse_attribute(ad
);
3523 type_decl(&type1
, ad
, v
, td
);
3526 /* type identifier */
3527 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
3531 if (!(td
& TYPE_ABSTRACT
))
3532 expect("identifier");
3536 storage
= type
->t
& VT_STORAGE
;
3537 type
->t
&= ~VT_STORAGE
;
3538 if (storage
& VT_STATIC
) {
3539 int saved_nocode_wanted
= nocode_wanted
;
3541 post_type(type
, ad
);
3542 nocode_wanted
= saved_nocode_wanted
;
3544 post_type(type
, ad
);
3546 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3547 parse_attribute(ad
);
3551 /* append type at the end of type1 */
3564 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3565 ST_FUNC
int lvalue_type(int t
)
3570 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
3572 else if (bt
== VT_SHORT
)
3576 if (t
& VT_UNSIGNED
)
3577 r
|= VT_LVAL_UNSIGNED
;
3581 /* indirection with full error checking and bound check */
3582 ST_FUNC
void indir(void)
3584 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
3585 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
3589 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
3591 vtop
->type
= *pointed_type(&vtop
->type
);
3592 /* Arrays and functions are never lvalues */
3593 if (!(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_VLA
)
3594 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3595 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3596 /* if bound checking, the referenced pointer must be checked */
3597 #ifdef CONFIG_TCC_BCHECK
3598 if (tcc_state
->do_bounds_check
)
3599 vtop
->r
|= VT_MUSTBOUND
;
3604 /* pass a parameter to a function and do type checking and casting */
3605 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
3610 func_type
= func
->c
;
3611 if (func_type
== FUNC_OLD
||
3612 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
3613 /* default casting : only need to convert float to double */
3614 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
3617 } else if (vtop
->type
.t
& VT_BITFIELD
) {
3618 type
.t
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
3621 } else if (arg
== NULL
) {
3622 tcc_error("too many arguments to function");
3625 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
3626 gen_assign_cast(&type
);
3630 /* parse an expression of the form '(type)' or '(expr)' and return its
3632 static void parse_expr_type(CType
*type
)
3638 if (parse_btype(type
, &ad
)) {
3639 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3646 static void parse_type(CType
*type
)
3651 if (!parse_btype(type
, &ad
)) {
3654 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3657 static void vpush_tokc(int t
)
3662 vsetc(&type
, VT_CONST
, &tokc
);
3665 ST_FUNC
void unary(void)
3667 int n
, t
, align
, size
, r
, sizeof_caller
;
3671 static int in_sizeof
= 0;
3673 sizeof_caller
= in_sizeof
;
3675 /* XXX: GCC 2.95.3 does not generate a table although it should be
3689 vpush_tokc(VT_INT
| VT_UNSIGNED
);
3693 vpush_tokc(VT_LLONG
);
3697 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
3701 vpush_tokc(VT_FLOAT
);
3705 vpush_tokc(VT_DOUBLE
);
3709 vpush_tokc(VT_LDOUBLE
);
3712 case TOK___FUNCTION__
:
3714 goto tok_identifier
;
3720 /* special function name identifier */
3721 len
= strlen(funcname
) + 1;
3722 /* generate char[len] type */
3727 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
3728 ptr
= section_ptr_add(data_section
, len
);
3729 memcpy(ptr
, funcname
, len
);
3734 #ifdef TCC_TARGET_PE
3735 t
= VT_SHORT
| VT_UNSIGNED
;
3741 /* string parsing */
3744 if (tcc_state
->warn_write_strings
)
3749 memset(&ad
, 0, sizeof(AttributeDef
));
3750 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, NULL
, 0);
3755 if (parse_btype(&type
, &ad
)) {
3756 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
3758 /* check ISOC99 compound literal */
3760 /* data is allocated locally by default */
3765 /* all except arrays are lvalues */
3766 if (!(type
.t
& VT_ARRAY
))
3767 r
|= lvalue_type(type
.t
);
3768 memset(&ad
, 0, sizeof(AttributeDef
));
3769 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, NULL
, 0);
3771 if (sizeof_caller
) {
3778 } else if (tok
== '{') {
3780 tcc_error("statement expression in global scope");
3781 /* save all registers */
3783 /* statement expression : we do not accept break/continue
3784 inside as GCC does */
3785 block(NULL
, NULL
, NULL
, NULL
, 0, 1);
3800 /* functions names must be treated as function pointers,
3801 except for unary '&' and sizeof. Since we consider that
3802 functions are not lvalues, we only have to handle it
3803 there and in function calls. */
3804 /* arrays can also be used although they are not lvalues */
3805 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
3806 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
3808 mk_pointer(&vtop
->type
);
3814 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3816 boolean
.t
= VT_BOOL
;
3818 vtop
->c
.i
= !vtop
->c
.i
;
3819 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
3820 vtop
->c
.i
= vtop
->c
.i
^ 1;
3821 else if (!nocode_wanted
) {
3823 vseti(VT_JMP
, gvtst(1, 0));
3837 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
3838 tcc_error("pointer not accepted for unary plus");
3839 /* In order to force cast, we add zero, except for floating point
3840 where we really need an noop (otherwise -0.0 will be transformed
3842 if (!is_float(vtop
->type
.t
)) {
3853 unary_type(&type
); // Perform a in_sizeof = 0;
3854 size
= type_size(&type
, &align
);
3855 if (t
== TOK_SIZEOF
) {
3856 if (!(type
.t
& VT_VLA
)) {
3858 tcc_error("sizeof applied to an incomplete type");
3861 vla_runtime_type_size(&type
, &align
);
3866 vtop
->type
.t
|= VT_UNSIGNED
;
3869 case TOK_builtin_types_compatible_p
:
3878 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3879 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3880 vpushi(is_compatible_types(&type1
, &type2
));
3883 case TOK_builtin_constant_p
:
3885 int saved_nocode_wanted
, res
;
3888 saved_nocode_wanted
= nocode_wanted
;
3891 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
3893 nocode_wanted
= saved_nocode_wanted
;
3898 case TOK_builtin_frame_address
:
3899 case TOK_builtin_return_address
:
3906 if (tok
!= TOK_CINT
|| tokc
.i
< 0) {
3907 tcc_error("%s only takes positive integers",
3908 tok1
== TOK_builtin_return_address
?
3909 "__builtin_return_address" :
3910 "__builtin_frame_address");
3917 vset(&type
, VT_LOCAL
, 0); /* local frame */
3919 mk_pointer(&vtop
->type
);
3920 indir(); /* -> parent frame */
3922 if (tok1
== TOK_builtin_return_address
) {
3923 // assume return address is just above frame pointer on stack
3926 mk_pointer(&vtop
->type
);
3931 #ifdef TCC_TARGET_X86_64
3932 #ifdef TCC_TARGET_PE
3933 case TOK_builtin_va_start
:
3941 if ((vtop
->r
& VT_VALMASK
) != VT_LOCAL
)
3942 tcc_error("__builtin_va_start expects a local variable");
3943 vtop
->r
&= ~(VT_LVAL
| VT_REF
);
3944 vtop
->type
= char_pointer_type
;
3949 case TOK_builtin_va_arg_types
:
3956 vpushi(classify_x86_64_va_arg(&type
));
3962 #ifdef TCC_TARGET_ARM64
3963 case TOK___va_start
: {
3965 tcc_error("statement in global scope");
3975 vtop
->type
.t
= VT_VOID
;
3978 case TOK___va_arg
: {
3980 tcc_error("statement in global scope");
3993 case TOK___arm64_clear_cache
: {
4002 vtop
->type
.t
= VT_VOID
;
4006 /* pre operations */
4017 t
= vtop
->type
.t
& VT_BTYPE
;
4019 /* In IEEE negate(x) isn't subtract(0,x), but rather
4024 else if (t
== VT_DOUBLE
)
4035 goto tok_identifier
;
4037 /* allow to take the address of a label */
4038 if (tok
< TOK_UIDENT
)
4039 expect("label identifier");
4040 s
= label_find(tok
);
4042 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4044 if (s
->r
== LABEL_DECLARED
)
4045 s
->r
= LABEL_FORWARD
;
4048 s
->type
.t
= VT_VOID
;
4049 mk_pointer(&s
->type
);
4050 s
->type
.t
|= VT_STATIC
;
4052 vpushsym(&s
->type
, s
);
4056 // special qnan , snan and infinity values
4058 vpush64(VT_DOUBLE
, 0x7ff8000000000000ULL
);
4062 vpush64(VT_DOUBLE
, 0x7ff0000000000001ULL
);
4066 vpush64(VT_DOUBLE
, 0x7ff0000000000000ULL
);
4075 expect("identifier");
4078 const char *name
= get_tok_str(t
, NULL
);
4080 tcc_error("'%s' undeclared", name
);
4081 /* for simple function calls, we tolerate undeclared
4082 external reference to int() function */
4083 if (tcc_state
->warn_implicit_function_declaration
4084 #ifdef TCC_TARGET_PE
4085 /* people must be warned about using undeclared WINAPI functions
4086 (which usually start with uppercase letter) */
4087 || (name
[0] >= 'A' && name
[0] <= 'Z')
4090 tcc_warning("implicit declaration of function '%s'", name
);
4091 s
= external_global_sym(t
, &func_old_type
, 0);
4093 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
4094 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
4095 /* if referencing an inline function, then we generate a
4096 symbol to it if not already done. It will have the
4097 effect to generate code for it at the end of the
4098 compilation unit. Inline function as always
4099 generated in the text section. */
4101 put_extern_sym(s
, text_section
, 0, 0);
4102 r
= VT_SYM
| VT_CONST
;
4106 vset(&s
->type
, r
, s
->c
);
4107 /* if forward reference, we must point to s */
4108 if (vtop
->r
& VT_SYM
) {
4110 vtop
->c
.ptr_offset
= 0;
4115 /* post operations */
4117 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
4120 } else if (tok
== '.' || tok
== TOK_ARROW
) {
4123 if (tok
== TOK_ARROW
)
4125 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
4129 /* expect pointer on structure */
4130 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
4131 expect("struct or union");
4135 while ((s
= s
->next
) != NULL
) {
4140 tcc_error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, NULL
));
4141 /* add field offset to pointer */
4142 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
4145 /* change type to field type, and set to lvalue */
4146 vtop
->type
= s
->type
;
4147 vtop
->type
.t
|= qualifiers
;
4148 /* an array is never an lvalue */
4149 if (!(vtop
->type
.t
& VT_ARRAY
)) {
4150 vtop
->r
|= lvalue_type(vtop
->type
.t
);
4151 #ifdef CONFIG_TCC_BCHECK
4152 /* if bound checking, the referenced pointer must be checked */
4153 if (tcc_state
->do_bounds_check
)
4154 vtop
->r
|= VT_MUSTBOUND
;
4158 } else if (tok
== '[') {
4164 } else if (tok
== '(') {
4167 int nb_args
, ret_nregs
, ret_align
, regsize
, variadic
;
4170 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
4171 /* pointer test (no array accepted) */
4172 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
4173 vtop
->type
= *pointed_type(&vtop
->type
);
4174 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
4178 expect("function pointer");
4181 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
4183 /* get return type */
4186 sa
= s
->next
; /* first parameter */
4189 /* compute first implicit argument if a structure is returned */
4190 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
4191 variadic
= (s
->c
== FUNC_ELLIPSIS
);
4192 ret_nregs
= gfunc_sret(&s
->type
, variadic
, &ret
.type
,
4193 &ret_align
, ®size
);
4195 /* get some space for the returned structure */
4196 size
= type_size(&s
->type
, &align
);
4197 #ifdef TCC_TARGET_ARM64
4198 /* On arm64, a small struct is return in registers.
4199 It is much easier to write it to memory if we know
4200 that we are allowed to write some extra bytes, so
4201 round the allocated space up to a power of 2: */
4203 while (size
& (size
- 1))
4204 size
= (size
| (size
- 1)) + 1;
4206 loc
= (loc
- size
) & -align
;
4208 ret
.r
= VT_LOCAL
| VT_LVAL
;
4209 /* pass it as 'int' to avoid structure arg passing
4211 vseti(VT_LOCAL
, loc
);
4221 /* return in register */
4222 if (is_float(ret
.type
.t
)) {
4223 ret
.r
= reg_fret(ret
.type
.t
);
4224 #ifdef TCC_TARGET_X86_64
4225 if ((ret
.type
.t
& VT_BTYPE
) == VT_QFLOAT
)
4229 #ifndef TCC_TARGET_ARM64
4230 #ifdef TCC_TARGET_X86_64
4231 if ((ret
.type
.t
& VT_BTYPE
) == VT_QLONG
)
4233 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
4244 gfunc_param_typed(s
, sa
);
4254 tcc_error("too few arguments to function");
4256 if (!nocode_wanted
) {
4257 gfunc_call(nb_args
);
4259 vtop
-= (nb_args
+ 1);
4263 for (r
= ret
.r
+ ret_nregs
+ !ret_nregs
; r
-- > ret
.r
;) {
4264 vsetc(&ret
.type
, r
, &ret
.c
);
4265 vtop
->r2
= ret
.r2
; /* Loop only happens when r2 is VT_CONST */
4268 /* handle packed struct return */
4269 if (((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) && ret_nregs
) {
4272 size
= type_size(&s
->type
, &align
);
4273 /* We're writing whole regs often, make sure there's enough
4274 space. Assume register size is power of 2. */
4275 if (regsize
> align
)
4277 loc
= (loc
- size
) & -align
;
4281 vset(&ret
.type
, VT_LOCAL
| VT_LVAL
, addr
+ offset
);
4285 if (--ret_nregs
== 0)
4289 vset(&s
->type
, VT_LOCAL
| VT_LVAL
, addr
);
4297 ST_FUNC
void expr_prod(void)
4302 while (tok
== '*' || tok
== '/' || tok
== '%') {
4310 ST_FUNC
void expr_sum(void)
4315 while (tok
== '+' || tok
== '-') {
4323 static void expr_shift(void)
4328 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
4336 static void expr_cmp(void)
4341 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
4342 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
4350 static void expr_cmpeq(void)
4355 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
4363 static void expr_and(void)
4366 while (tok
== '&') {
4373 static void expr_xor(void)
4376 while (tok
== '^') {
4383 static void expr_or(void)
4386 while (tok
== '|') {
4393 /* XXX: fix this mess */
4394 static void expr_land_const(void)
4397 while (tok
== TOK_LAND
) {
4404 /* XXX: fix this mess */
4405 static void expr_lor_const(void)
4408 while (tok
== TOK_LOR
) {
4415 /* only used if non constant */
4416 static void expr_land(void)
4421 if (tok
== TOK_LAND
) {
4426 if (tok
!= TOK_LAND
) {
4436 static void expr_lor(void)
4441 if (tok
== TOK_LOR
) {
4446 if (tok
!= TOK_LOR
) {
4456 /* XXX: better constant handling */
4457 static void expr_cond(void)
4459 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
;
4461 CType type
, type1
, type2
;
4468 boolean
.t
= VT_BOOL
;
4474 if (tok
!= ':' || !gnu_ext
) {
4489 if (vtop
!= vstack
) {
4490 /* needed to avoid having different registers saved in
4492 if (is_float(vtop
->type
.t
)) {
4494 #ifdef TCC_TARGET_X86_64
4495 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4505 if (tok
== ':' && gnu_ext
) {
4513 sv
= *vtop
; /* save value to handle it later */
4514 vtop
--; /* no vpop so that FP stack is not flushed */
4522 bt1
= t1
& VT_BTYPE
;
4524 bt2
= t2
& VT_BTYPE
;
4525 /* cast operands to correct type according to ISOC rules */
4526 if (is_float(bt1
) || is_float(bt2
)) {
4527 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
4528 type
.t
= VT_LDOUBLE
;
4529 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
4534 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
4535 /* cast to biggest op */
4537 /* convert to unsigned if it does not fit in a long long */
4538 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
4539 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
4540 type
.t
|= VT_UNSIGNED
;
4541 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
4542 /* If one is a null ptr constant the result type
4544 if (is_null_pointer (vtop
))
4546 else if (is_null_pointer (&sv
))
4548 /* XXX: test pointer compatibility, C99 has more elaborate
4552 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
4553 /* XXX: test function pointer compatibility */
4554 type
= bt1
== VT_FUNC
? type1
: type2
;
4555 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
4556 /* XXX: test structure compatibility */
4557 type
= bt1
== VT_STRUCT
? type1
: type2
;
4558 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
4559 /* NOTE: as an extension, we accept void on only one side */
4562 /* integer operations */
4564 /* convert to unsigned if it does not fit in an integer */
4565 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
4566 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
4567 type
.t
|= VT_UNSIGNED
;
4570 /* now we convert second operand */
4572 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4575 if (is_float(type
.t
)) {
4577 #ifdef TCC_TARGET_X86_64
4578 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4582 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
4583 /* for long longs, we use fixed registers to avoid having
4584 to handle a complicated move */
4589 /* this is horrible, but we must also convert first
4593 /* put again first value and cast it */
4596 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4599 move_reg(r2
, r1
, type
.t
);
4606 static void expr_eq(void)
4612 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
4613 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
4614 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
4629 ST_FUNC
void gexpr(void)
4640 /* parse an expression and return its type without any side effect. */
4641 static void expr_type(CType
*type
)
4643 int saved_nocode_wanted
;
4645 saved_nocode_wanted
= nocode_wanted
;
4650 nocode_wanted
= saved_nocode_wanted
;
4653 /* parse a unary expression and return its type without any side
4655 static void unary_type(CType
*type
)
4667 /* parse a constant expression and return value in vtop. */
4668 static void expr_const1(void)
4677 /* parse an integer constant and return its value. */
4678 ST_FUNC
int expr_const(void)
4682 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
4683 expect("constant expression");
4689 /* return the label token if current token is a label, otherwise
4691 static int is_label(void)
4695 /* fast test first */
4696 if (tok
< TOK_UIDENT
)
4698 /* no need to save tokc because tok is an identifier */
4705 unget_tok(last_tok
);
4710 static void label_or_decl(int l
)
4714 /* fast test first */
4715 if (tok
>= TOK_UIDENT
)
4717 /* no need to save tokc because tok is an identifier */
4721 unget_tok(last_tok
);
4724 unget_tok(last_tok
);
4729 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
4730 int case_reg
, int is_expr
)
4733 Sym
*s
, *frame_bottom
;
4735 /* generate line number info */
4736 if (tcc_state
->do_debug
&&
4737 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
4738 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
4740 last_line_num
= file
->line_num
;
4744 /* default return value is (void) */
4746 vtop
->type
.t
= VT_VOID
;
4749 if (tok
== TOK_IF
) {
4756 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4758 if (c
== TOK_ELSE
) {
4762 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4763 gsym(d
); /* patch else jmp */
4766 } else if (tok
== TOK_WHILE
) {
4774 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4778 } else if (tok
== '{') {
4780 int block_vla_sp_loc
, *saved_vla_sp_loc
, saved_vla_flags
;
4783 /* record local declaration stack position */
4785 frame_bottom
= sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4786 frame_bottom
->next
= scope_stack_bottom
;
4787 scope_stack_bottom
= frame_bottom
;
4788 llabel
= local_label_stack
;
4790 /* save VLA state */
4791 block_vla_sp_loc
= *(saved_vla_sp_loc
= vla_sp_loc
);
4792 if (saved_vla_sp_loc
!= &vla_sp_root_loc
)
4793 vla_sp_loc
= &block_vla_sp_loc
;
4795 saved_vla_flags
= vla_flags
;
4796 vla_flags
|= VLA_NEED_NEW_FRAME
;
4798 /* handle local labels declarations */
4799 if (tok
== TOK_LABEL
) {
4802 if (tok
< TOK_UIDENT
)
4803 expect("label identifier");
4804 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
4814 while (tok
!= '}') {
4815 label_or_decl(VT_LOCAL
);
4819 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4822 /* pop locally defined labels */
4823 label_pop(&local_label_stack
, llabel
);
4825 /* XXX: this solution makes only valgrind happy...
4826 triggered by gcc.c-torture/execute/20000917-1.c */
4828 switch(vtop
->type
.t
& VT_BTYPE
) {
4830 /* this breaks a compilation of the linux kernel v2.4.26 */
4831 /* pmd_t *new = ({ __asm__ __volatile__("ud2\n") ; ((pmd_t *)1); }); */
4832 /* Look a commit a80acab: Display error on statement expressions with complex return type */
4833 /* A pointer is not a complex return type */
4837 for(p
=vtop
->type
.ref
;p
;p
=p
->prev
)
4839 tcc_error("unsupported expression type");
4842 /* pop locally defined symbols */
4843 scope_stack_bottom
= scope_stack_bottom
->next
;
4844 sym_pop(&local_stack
, s
);
4846 /* Pop VLA frames and restore stack pointer if required */
4847 if (saved_vla_sp_loc
!= &vla_sp_root_loc
)
4848 *saved_vla_sp_loc
= block_vla_sp_loc
;
4849 if (vla_sp_loc
!= (saved_vla_sp_loc
== &vla_sp_root_loc
? &vla_sp_root_loc
: &block_vla_sp_loc
)) {
4850 vla_sp_loc
= saved_vla_sp_loc
;
4851 gen_vla_sp_restore(*vla_sp_loc
);
4853 vla_flags
= (vla_flags
& ~VLA_SCOPE_FLAGS
) | (saved_vla_flags
& VLA_SCOPE_FLAGS
);
4856 } else if (tok
== TOK_RETURN
) {
4860 gen_assign_cast(&func_vt
);
4861 #ifdef TCC_TARGET_ARM64
4862 // Perhaps it would be better to use this for all backends:
4865 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
4866 CType type
, ret_type
;
4867 int ret_align
, ret_nregs
, regsize
;
4868 ret_nregs
= gfunc_sret(&func_vt
, func_var
, &ret_type
,
4869 &ret_align
, ®size
);
4870 if (0 == ret_nregs
) {
4871 /* if returning structure, must copy it to implicit
4872 first pointer arg location */
4875 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
4878 /* copy structure value to pointer */
4881 /* returning structure packed into registers */
4882 int r
, size
, addr
, align
;
4883 size
= type_size(&func_vt
,&align
);
4884 if ((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) || (vtop
->c
.i
& (ret_align
-1)))
4885 && (align
& (ret_align
-1))) {
4886 loc
= (loc
- size
) & -align
;
4889 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
4892 vset(&ret_type
, VT_LOCAL
| VT_LVAL
, addr
);
4894 vtop
->type
= ret_type
;
4895 if (is_float(ret_type
.t
))
4896 r
= rc_fret(ret_type
.t
);
4902 if (--ret_nregs
== 0)
4904 /* We assume that when a structure is returned in multiple
4905 registers, their classes are consecutive values of the
4908 vtop
->c
.i
+= regsize
;
4909 vtop
->r
= VT_LOCAL
| VT_LVAL
;
4912 } else if (is_float(func_vt
.t
)) {
4913 gv(rc_fret(func_vt
.t
));
4918 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
4921 rsym
= gjmp(rsym
); /* jmp */
4922 } else if (tok
== TOK_BREAK
) {
4925 tcc_error("cannot break");
4926 *bsym
= gjmp(*bsym
);
4929 } else if (tok
== TOK_CONTINUE
) {
4932 tcc_error("cannot continue");
4933 *csym
= gjmp(*csym
);
4936 } else if (tok
== TOK_FOR
) {
4941 frame_bottom
= sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4942 frame_bottom
->next
= scope_stack_bottom
;
4943 scope_stack_bottom
= frame_bottom
;
4945 /* c99 for-loop init decl? */
4946 if (!decl0(VT_LOCAL
, 1)) {
4947 /* no, regular for-loop init expr */
4971 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4975 scope_stack_bottom
= scope_stack_bottom
->next
;
4976 sym_pop(&local_stack
, s
);
4978 if (tok
== TOK_DO
) {
4983 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4994 if (tok
== TOK_SWITCH
) {
4998 /* XXX: other types than integer */
4999 case_reg
= gv(RC_INT
);
5003 b
= gjmp(0); /* jump to first case */
5005 block(&a
, csym
, &b
, &c
, case_reg
, 0);
5006 /* if no default, jmp after switch */
5014 if (tok
== TOK_CASE
) {
5021 if (gnu_ext
&& tok
== TOK_DOTS
) {
5025 tcc_warning("empty case range");
5027 /* since a case is like a label, we must skip it with a jmp */
5035 *case_sym
= gtst(1, 0);
5038 *case_sym
= gtst(1, 0);
5042 *case_sym
= gtst(1, *case_sym
);
5044 case_reg
= gv(RC_INT
);
5049 goto block_after_label
;
5051 if (tok
== TOK_DEFAULT
) {
5057 tcc_error("too many 'default'");
5060 goto block_after_label
;
5062 if (tok
== TOK_GOTO
) {
5064 if (tok
== '*' && gnu_ext
) {
5068 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
5071 } else if (tok
>= TOK_UIDENT
) {
5072 s
= label_find(tok
);
5073 /* put forward definition if needed */
5075 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
5077 if (s
->r
== LABEL_DECLARED
)
5078 s
->r
= LABEL_FORWARD
;
5080 /* label already defined */
5081 if (vla_flags
& VLA_IN_SCOPE
) {
5082 /* If VLAs are in use, save the current stack pointer and
5083 reset the stack pointer to what it was at function entry
5084 (label will restore stack pointer in inner scopes) */
5086 gen_vla_sp_restore(vla_sp_root_loc
);
5088 if (s
->r
& LABEL_FORWARD
)
5089 s
->jnext
= gjmp(s
->jnext
);
5091 gjmp_addr(s
->jnext
);
5094 expect("label identifier");
5097 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
5103 if (vla_flags
& VLA_IN_SCOPE
) {
5104 /* save/restore stack pointer across label
5105 this is a no-op when combined with the load immediately
5106 after the label unless we arrive via goto */
5111 if (s
->r
== LABEL_DEFINED
)
5112 tcc_error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
5114 s
->r
= LABEL_DEFINED
;
5116 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
5119 if (vla_flags
& VLA_IN_SCOPE
) {
5120 gen_vla_sp_restore(*vla_sp_loc
);
5121 vla_flags
|= VLA_NEED_NEW_FRAME
;
5123 /* we accept this, but it is a mistake */
5126 tcc_warning("deprecated use of label at end of compound statement");
5130 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
5133 /* expression case */
5148 /* t is the array or struct type. c is the array or struct
5149 address. cur_index/cur_field is the pointer to the current
5150 value. 'size_only' is true if only size info is needed (only used
5152 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
5153 int *cur_index
, Sym
**cur_field
,
5154 int size_only
, int *par_count_p
)
5157 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
5163 if (gnu_ext
&& (l
= is_label()) != 0)
5165 while (tok
== '[' || tok
== '.') {
5167 if (!(type
->t
& VT_ARRAY
))
5168 expect("array type");
5171 index
= expr_const();
5172 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
5173 expect("invalid index");
5174 if (tok
== TOK_DOTS
&& gnu_ext
) {
5176 index_last
= expr_const();
5177 if (index_last
< 0 ||
5178 (s
->c
>= 0 && index_last
>= s
->c
) ||
5180 expect("invalid index");
5186 *cur_index
= index_last
;
5187 type
= pointed_type(type
);
5188 elem_size
= type_size(type
, &align
);
5189 c
+= index
* elem_size
;
5190 /* NOTE: we only support ranges for last designator */
5191 nb_elems
= index_last
- index
+ 1;
5192 if (nb_elems
!= 1) {
5201 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
5202 expect("struct/union type");
5215 /* XXX: fix this mess by using explicit storage field */
5217 type1
.t
|= (type
->t
& ~VT_TYPE
);
5231 if (type
->t
& VT_ARRAY
) {
5233 type
= pointed_type(type
);
5234 c
+= index
* type_size(type
, &align
);
5238 tcc_error("too many field init");
5239 /* XXX: fix this mess by using explicit storage field */
5241 type1
.t
|= (type
->t
& ~VT_TYPE
);
5246 decl_initializer(type
, sec
, c
, 0, size_only
, par_count_p
);
5248 /* XXX: make it more general */
5249 if (!size_only
&& nb_elems
> 1) {
5250 unsigned long c_end
;
5255 tcc_error("range init not supported yet for dynamic storage");
5256 c_end
= c
+ nb_elems
* elem_size
;
5257 if (c_end
> sec
->data_allocated
)
5258 section_realloc(sec
, c_end
);
5259 src
= sec
->data
+ c
;
5261 for(i
= 1; i
< nb_elems
; i
++) {
5263 memcpy(dst
, src
, elem_size
);
5269 #define EXPR_CONST 1
5272 /* store a value or an expression directly in global data or in local array */
5273 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
5274 int v
, int expr_type
)
5276 int saved_global_expr
, bt
, bit_pos
, bit_size
;
5278 unsigned long long bit_mask
;
5286 /* compound literals must be allocated globally in this case */
5287 saved_global_expr
= global_expr
;
5290 global_expr
= saved_global_expr
;
5291 /* NOTE: symbols are accepted */
5292 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
5293 tcc_error("initializer element is not constant");
5301 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
5304 /* XXX: not portable */
5305 /* XXX: generate error if incorrect relocation */
5306 gen_assign_cast(&dtype
);
5307 bt
= type
->t
& VT_BTYPE
;
5308 /* we'll write at most 16 bytes */
5309 if (c
+ 16 > sec
->data_allocated
) {
5310 section_realloc(sec
, c
+ 16);
5312 ptr
= sec
->data
+ c
;
5313 /* XXX: make code faster ? */
5314 if (!(type
->t
& VT_BITFIELD
)) {
5319 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5320 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
5321 bit_mask
= (1LL << bit_size
) - 1;
5323 if ((vtop
->r
& VT_SYM
) &&
5329 (bt
== VT_INT
&& bit_size
!= 32)))
5330 tcc_error("initializer element is not computable at load time");
5332 /* XXX: when cross-compiling we assume that each type has the
5333 same representation on host and target, which is likely to
5334 be wrong in the case of long double */
5336 vtop
->c
.i
= (vtop
->c
.i
!= 0);
5338 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5341 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5344 *(double *)ptr
= vtop
->c
.d
;
5347 *(long double *)ptr
= vtop
->c
.ld
;
5350 *(long long *)ptr
|= (vtop
->c
.ll
& bit_mask
) << bit_pos
;
5353 addr_t val
= (vtop
->c
.ptr_offset
& bit_mask
) << bit_pos
;
5354 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
5355 if (vtop
->r
& VT_SYM
)
5356 greloca(sec
, vtop
->sym
, c
, R_DATA_PTR
, val
);
5358 *(addr_t
*)ptr
|= val
;
5360 if (vtop
->r
& VT_SYM
)
5361 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
5362 *(addr_t
*)ptr
|= val
;
5367 int val
= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5368 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
5369 if (vtop
->r
& VT_SYM
)
5370 greloca(sec
, vtop
->sym
, c
, R_DATA_PTR
, val
);
5374 if (vtop
->r
& VT_SYM
)
5375 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
5383 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
5390 /* put zeros for variable based init */
5391 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
5394 /* nothing to do because globals are already set to zero */
5396 vpush_global_sym(&func_old_type
, TOK_memset
);
5398 #ifdef TCC_TARGET_ARM
5409 /* 't' contains the type and storage info. 'c' is the offset of the
5410 object in section 'sec'. If 'sec' is NULL, it means stack based
5411 allocation. 'first' is true if array '{' must be read (multi
5412 dimension implicit array init handling). 'size_only' is true if
5413 size only evaluation is wanted (only for arrays). */
5414 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
5415 int first
, int size_only
, int *par_count_p
)
5417 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, parlevel1
, i
;
5418 int size1
, align1
, expr_type
;
5422 if (type
->t
& VT_VLA
) {
5425 /* save current stack pointer */
5426 if (vla_flags
& VLA_NEED_NEW_FRAME
) {
5428 vla_flags
= VLA_IN_SCOPE
;
5429 vla_sp_loc
= &vla_sp_loc_tmp
;
5432 vla_runtime_type_size(type
, &a
);
5433 gen_vla_alloc(type
, a
);
5434 vset(type
, VT_LOCAL
|VT_LVAL
, c
);
5438 } else if (type
->t
& VT_ARRAY
) {
5442 t1
= pointed_type(type
);
5443 size1
= type_size(t1
, &align1
);
5446 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
5449 tcc_error("character array initializer must be a literal,"
5450 " optionally enclosed in braces");
5455 /* only parse strings here if correct type (otherwise: handle
5456 them as ((w)char *) expressions */
5457 if ((tok
== TOK_LSTR
&&
5458 #ifdef TCC_TARGET_PE
5459 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
5461 (t1
->t
& VT_BTYPE
) == VT_INT
5463 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
5464 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5469 /* compute maximum number of chars wanted */
5471 cstr_len
= cstr
->size
;
5473 cstr_len
= cstr
->size
/ sizeof(nwchar_t
);
5476 if (n
>= 0 && nb
> (n
- array_length
))
5477 nb
= n
- array_length
;
5480 tcc_warning("initializer-string for array is too long");
5481 /* in order to go faster for common case (char
5482 string in global variable, we handle it
5484 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
5485 memcpy(sec
->data
+ c
+ array_length
, cstr
->data
, nb
);
5489 ch
= ((unsigned char *)cstr
->data
)[i
];
5491 ch
= ((nwchar_t
*)cstr
->data
)[i
];
5492 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
5500 /* only add trailing zero if enough storage (no
5501 warning in this case since it is standard) */
5502 if (n
< 0 || array_length
< n
) {
5504 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
5510 while (tok
!= '}') {
5511 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
, par_count_p
);
5512 if (n
>= 0 && index
>= n
)
5513 tcc_error("index too large");
5514 /* must put zero in holes (note that doing it that way
5515 ensures that it even works with designators) */
5516 if (!size_only
&& array_length
< index
) {
5517 init_putz(t1
, sec
, c
+ array_length
* size1
,
5518 (index
- array_length
) * size1
);
5521 if (index
> array_length
)
5522 array_length
= index
;
5523 /* special test for multi dimensional arrays (may not
5524 be strictly correct if designators are used at the
5526 if (index
>= n
&& no_oblock
)
5528 if (*par_count_p
!= 0 && tok
== ')') {
5539 /* put zeros at the end */
5540 if (!size_only
&& n
>= 0 && array_length
< n
) {
5541 init_putz(t1
, sec
, c
+ array_length
* size1
,
5542 (n
- array_length
) * size1
);
5544 /* patch type size if needed */
5546 s
->c
= array_length
;
5547 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
5548 (sec
|| !first
|| tok
== '{')) {
5551 /* NOTE: the previous test is a specific case for automatic
5552 struct/union init */
5553 /* XXX: union needs only one init */
5555 /* XXX: this test is incorrect for local initializers
5556 beginning with ( without {. It would be much more difficult
5557 to do it correctly (ideally, the expression parser should
5558 be used in all cases) */
5564 while (tok
== '(') {
5568 if (!parse_btype(&type1
, &ad1
))
5570 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
5572 if (!is_assignable_types(type
, &type1
))
5573 tcc_error("invalid type for cast");
5578 if (first
|| tok
== '{') {
5587 while (tok
!= '}') {
5588 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
, &par_count
);
5590 if (!size_only
&& array_length
< index
) {
5591 init_putz(type
, sec
, c
+ array_length
,
5592 index
- array_length
);
5594 index
= index
+ type_size(&f
->type
, &align1
);
5595 if (index
> array_length
)
5596 array_length
= index
;
5598 /* gr: skip fields from same union - ugly. */
5601 int f_size
= type_size(&f
->type
, &align
);
5602 int f_type
= (f
->type
.t
& VT_BTYPE
);
5604 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
5605 /* test for same offset */
5606 if (f
->next
->c
!= f
->c
)
5608 if ((f_type
== VT_STRUCT
) && (f_size
== 0)) {
5610 Lets assume a structure of size 0 can't be a member of the union.
5611 This allow to compile the following code from a linux kernel v2.4.26
5612 typedef struct { } rwlock_t;
5618 struct fs_struct init_fs = { { (1) }, (rwlock_t) {}, 0022, };
5619 tcc-0.9.23 can succesfully compile this version of the kernel.
5620 gcc don't have problems with this code too.
5624 /* if yes, test for bitfield shift */
5625 if ((f
->type
.t
& VT_BITFIELD
) && (f
->next
->type
.t
& VT_BITFIELD
)) {
5626 int bit_pos_1
= (f
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5627 int bit_pos_2
= (f
->next
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5628 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
5629 if (bit_pos_1
!= bit_pos_2
)
5636 if (no_oblock
&& f
== NULL
)
5642 /* put zeros at the end */
5643 if (!size_only
&& array_length
< n
) {
5644 init_putz(type
, sec
, c
+ array_length
,
5653 } else if (tok
== '{') {
5656 decl_initializer(type
, sec
, c
, first
, size_only
, &par_count
);
5658 } else if (size_only
) {
5659 /* just skip expression */
5660 parlevel
= parlevel1
= 0;
5661 while ((parlevel
> 0 || parlevel1
> 0 ||
5662 (tok
!= '}' && tok
!= ',')) && tok
!= -1) {
5665 else if (tok
== ')') {
5666 if (parlevel
== 0 && parlevel1
== 0)
5670 else if (tok
== '{')
5672 else if (tok
== '}') {
5673 if (parlevel
== 0 && parlevel1
== 0)
5680 /* currently, we always use constant expression for globals
5681 (may change for scripting case) */
5682 expr_type
= EXPR_CONST
;
5684 expr_type
= EXPR_ANY
;
5685 init_putv(type
, sec
, c
, 0, expr_type
);
5689 /* parse an initializer for type 't' if 'has_init' is non zero, and
5690 allocate space in local or global data space ('r' is either
5691 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5692 variable 'v' with an associated name represented by 'asm_label' of
5693 scope 'scope' is declared before initializers are parsed. If 'v' is
5694 zero, then a reference to the new object is put in the value stack.
5695 If 'has_init' is 2, a special parsing is done to handle string
5697 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
5698 int has_init
, int v
, char *asm_label
,
5701 int size
, align
, addr
, data_offset
;
5703 ParseState saved_parse_state
= {0};
5704 TokenString init_str
;
5706 Sym
*flexible_array
;
5708 flexible_array
= NULL
;
5709 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
5710 Sym
*field
= type
->ref
->next
;
5713 field
= field
->next
;
5714 if (field
->type
.t
& VT_ARRAY
&& field
->type
.ref
->c
< 0)
5715 flexible_array
= field
;
5719 size
= type_size(type
, &align
);
5720 /* If unknown size, we must evaluate it before
5721 evaluating initializers because
5722 initializers can generate global data too
5723 (e.g. string pointers or ISOC99 compound
5724 literals). It also simplifies local
5725 initializers handling */
5726 tok_str_new(&init_str
);
5727 if (size
< 0 || (flexible_array
&& has_init
)) {
5730 tcc_error("unknown type size");
5731 /* get all init string */
5732 if (has_init
== 2) {
5733 /* only get strings */
5734 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5735 tok_str_add_tok(&init_str
);
5740 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
5742 tcc_error("unexpected end of file in initializer");
5743 tok_str_add_tok(&init_str
);
5746 else if (tok
== '}') {
5756 tok_str_add(&init_str
, -1);
5757 tok_str_add(&init_str
, 0);
5760 save_parse_state(&saved_parse_state
);
5762 macro_ptr
= init_str
.str
;
5764 decl_initializer(type
, NULL
, 0, 1, 1, &par_count
);
5765 /* prepare second initializer parsing */
5766 macro_ptr
= init_str
.str
;
5769 /* if still unknown size, error */
5770 size
= type_size(type
, &align
);
5772 tcc_error("unknown type size");
5775 size
+= flexible_array
->type
.ref
->c
* pointed_size(&flexible_array
->type
);
5776 /* take into account specified alignment if bigger */
5777 if (ad
->a
.aligned
) {
5778 if (ad
->a
.aligned
> align
)
5779 align
= ad
->a
.aligned
;
5780 } else if (ad
->a
.packed
) {
5783 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
5785 #ifdef CONFIG_TCC_BCHECK
5786 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5790 loc
= (loc
- size
) & -align
;
5792 #ifdef CONFIG_TCC_BCHECK
5793 /* handles bounds */
5794 /* XXX: currently, since we do only one pass, we cannot track
5795 '&' operators, so we add only arrays */
5796 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5797 unsigned long *bounds_ptr
;
5798 /* add padding between regions */
5800 /* then add local bound info */
5801 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(unsigned long));
5802 bounds_ptr
[0] = addr
;
5803 bounds_ptr
[1] = size
;
5807 /* local variable */
5808 sym_push(v
, type
, r
, addr
);
5810 /* push local reference */
5811 vset(type
, r
, addr
);
5817 if (v
&& scope
== VT_CONST
) {
5818 /* see if the symbol was already defined */
5821 if (!is_compatible_types(&sym
->type
, type
))
5822 tcc_error("incompatible types for redefinition of '%s'",
5823 get_tok_str(v
, NULL
));
5824 if (sym
->type
.t
& VT_EXTERN
) {
5825 /* if the variable is extern, it was not allocated */
5826 sym
->type
.t
&= ~VT_EXTERN
;
5827 /* set array size if it was omitted in extern
5829 if ((sym
->type
.t
& VT_ARRAY
) &&
5830 sym
->type
.ref
->c
< 0 &&
5832 sym
->type
.ref
->c
= type
->ref
->c
;
5834 /* we accept several definitions of the same
5835 global variable. this is tricky, because we
5836 must play with the SHN_COMMON type of the symbol */
5837 /* XXX: should check if the variable was already
5838 initialized. It is incorrect to initialized it
5840 /* no init data, we won't add more to the symbol */
5847 /* allocate symbol in corresponding section */
5852 else if (tcc_state
->nocommon
)
5856 data_offset
= sec
->data_offset
;
5857 data_offset
= (data_offset
+ align
- 1) & -align
;
5859 /* very important to increment global pointer at this time
5860 because initializers themselves can create new initializers */
5861 data_offset
+= size
;
5862 #ifdef CONFIG_TCC_BCHECK
5863 /* add padding if bound check */
5864 if (tcc_state
->do_bounds_check
)
5867 sec
->data_offset
= data_offset
;
5868 /* allocate section space to put the data */
5869 if (sec
->sh_type
!= SHT_NOBITS
&&
5870 data_offset
> sec
->data_allocated
)
5871 section_realloc(sec
, data_offset
);
5872 /* align section if needed */
5873 if (align
> sec
->sh_addralign
)
5874 sec
->sh_addralign
= align
;
5876 addr
= 0; /* avoid warning */
5880 if (scope
!= VT_CONST
|| !sym
) {
5881 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
5882 sym
->asm_label
= asm_label
;
5884 /* update symbol definition */
5886 put_extern_sym(sym
, sec
, addr
, size
);
5889 /* put a common area */
5890 put_extern_sym(sym
, NULL
, align
, size
);
5891 /* XXX: find a nicer way */
5892 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
5893 esym
->st_shndx
= SHN_COMMON
;
5896 /* push global reference */
5897 sym
= get_sym_ref(type
, sec
, addr
, size
);
5898 vpushsym(type
, sym
);
5900 /* patch symbol weakness */
5901 if (type
->t
& VT_WEAK
)
5903 apply_visibility(sym
, type
);
5904 #ifdef CONFIG_TCC_BCHECK
5905 /* handles bounds now because the symbol must be defined
5906 before for the relocation */
5907 if (tcc_state
->do_bounds_check
) {
5908 unsigned long *bounds_ptr
;
5910 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
5911 /* then add global bound info */
5912 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(long));
5913 bounds_ptr
[0] = 0; /* relocated */
5914 bounds_ptr
[1] = size
;
5918 if (has_init
|| (type
->t
& VT_VLA
)) {
5920 decl_initializer(type
, sec
, addr
, 1, 0, &par_count
);
5921 /* restore parse state if needed */
5923 tok_str_free(init_str
.str
);
5924 restore_parse_state(&saved_parse_state
);
5926 /* patch flexible array member size back to -1, */
5927 /* for possible subsequent similar declarations */
5929 flexible_array
->type
.ref
->c
= -1;
5934 static void put_func_debug(Sym
*sym
)
5939 /* XXX: we put here a dummy type */
5940 snprintf(buf
, sizeof(buf
), "%s:%c1",
5941 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
5942 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
5943 cur_text_section
, sym
->c
);
5944 /* //gr gdb wants a line at the function */
5945 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
5950 /* parse an old style function declaration list */
5951 /* XXX: check multiple parameter */
5952 static void func_decl_list(Sym
*func_sym
)
5959 /* parse each declaration */
5960 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
&&
5961 tok
!= TOK_ASM1
&& tok
!= TOK_ASM2
&& tok
!= TOK_ASM3
) {
5962 if (!parse_btype(&btype
, &ad
))
5963 expect("declaration list");
5964 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5965 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5967 /* we accept no variable after */
5971 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5972 /* find parameter in function parameter list */
5975 if ((s
->v
& ~SYM_FIELD
) == v
)
5979 tcc_error("declaration for parameter '%s' but no such parameter",
5980 get_tok_str(v
, NULL
));
5982 /* check that no storage specifier except 'register' was given */
5983 if (type
.t
& VT_STORAGE
)
5984 tcc_error("storage class specified for '%s'", get_tok_str(v
, NULL
));
5985 convert_parameter_type(&type
);
5986 /* we can add the type (NOTE: it could be local to the function) */
5988 /* accept other parameters */
5999 /* parse a function defined by symbol 'sym' and generate its code in
6000 'cur_text_section' */
6001 static void gen_function(Sym
*sym
)
6003 int saved_nocode_wanted
= nocode_wanted
;
6005 ind
= cur_text_section
->data_offset
;
6006 /* NOTE: we patch the symbol size later */
6007 put_extern_sym(sym
, cur_text_section
, ind
, 0);
6008 funcname
= get_tok_str(sym
->v
, NULL
);
6010 /* Initialize VLA state */
6011 vla_sp_loc
= &vla_sp_root_loc
;
6012 vla_flags
= VLA_NEED_NEW_FRAME
;
6013 /* put debug symbol */
6014 if (tcc_state
->do_debug
)
6015 put_func_debug(sym
);
6016 /* push a dummy symbol to enable local sym storage */
6017 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
6018 gfunc_prolog(&sym
->type
);
6019 #ifdef CONFIG_TCC_BCHECK
6020 if (tcc_state
->do_bounds_check
6021 && !strcmp(get_tok_str(sym
->v
, NULL
), "main")) {
6025 for (i
= 0, sym
= local_stack
; i
< 2; i
++, sym
= sym
->prev
) {
6026 if (sym
->v
& SYM_FIELD
|| sym
->prev
->v
& SYM_FIELD
)
6028 vpush_global_sym(&func_old_type
, TOK___bound_main_arg
);
6029 vset(&sym
->type
, sym
->r
, sym
->c
);
6035 block(NULL
, NULL
, NULL
, NULL
, 0, 0);
6038 cur_text_section
->data_offset
= ind
;
6039 label_pop(&global_label_stack
, NULL
);
6040 /* reset local stack */
6041 scope_stack_bottom
= NULL
;
6042 sym_pop(&local_stack
, NULL
);
6043 /* end of function */
6044 /* patch symbol size */
6045 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
6047 /* patch symbol weakness (this definition overrules any prototype) */
6048 if (sym
->type
.t
& VT_WEAK
)
6050 apply_visibility(sym
, &sym
->type
);
6051 if (tcc_state
->do_debug
) {
6052 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
6054 /* It's better to crash than to generate wrong code */
6055 cur_text_section
= NULL
;
6056 funcname
= ""; /* for safety */
6057 func_vt
.t
= VT_VOID
; /* for safety */
6058 func_var
= 0; /* for safety */
6059 ind
= 0; /* for safety */
6060 nocode_wanted
= saved_nocode_wanted
;
6063 ST_FUNC
void gen_inline_functions(void)
6066 int *str
, inline_generated
, i
;
6067 struct InlineFunc
*fn
;
6069 /* iterate while inline function are referenced */
6071 inline_generated
= 0;
6072 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
6073 fn
= tcc_state
->inline_fns
[i
];
6075 if (sym
&& sym
->c
) {
6076 /* the function was used: generate its code and
6077 convert it to a normal function */
6078 str
= fn
->token_str
;
6081 pstrcpy(file
->filename
, sizeof file
->filename
, fn
->filename
);
6082 sym
->r
= VT_SYM
| VT_CONST
;
6083 sym
->type
.t
&= ~VT_INLINE
;
6087 cur_text_section
= text_section
;
6089 macro_ptr
= NULL
; /* fail safe */
6091 inline_generated
= 1;
6094 if (!inline_generated
)
6097 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
6098 fn
= tcc_state
->inline_fns
[i
];
6099 str
= fn
->token_str
;
6102 dynarray_reset(&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
);
6105 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
6106 static int decl0(int l
, int is_for_loop_init
)
6114 if (!parse_btype(&btype
, &ad
)) {
6115 if (is_for_loop_init
)
6117 /* skip redundant ';' */
6118 /* XXX: find more elegant solution */
6123 if (l
== VT_CONST
&&
6124 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
6125 /* global asm block */
6129 /* special test for old K&R protos without explicit int
6130 type. Only accepted when defining global data */
6131 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
6135 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
6136 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
6138 /* we accept no variable after */
6142 while (1) { /* iterate thru each declaration */
6143 char *asm_label
; // associated asm label
6145 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
6149 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
6150 printf("type = '%s'\n", buf
);
6153 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
6154 if ((type
.t
& VT_STATIC
) && (l
== VT_LOCAL
)) {
6155 tcc_error("function without file scope cannot be static");
6157 /* if old style function prototype, we accept a
6160 if (sym
->c
== FUNC_OLD
)
6161 func_decl_list(sym
);
6165 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
6168 asm_label_instr(&astr
);
6169 asm_label
= tcc_strdup(astr
.data
);
6172 /* parse one last attribute list, after asm label */
6173 parse_attribute(&ad
);
6178 #ifdef TCC_TARGET_PE
6179 if (ad
.a
.func_import
)
6180 type
.t
|= VT_IMPORT
;
6181 if (ad
.a
.func_export
)
6182 type
.t
|= VT_EXPORT
;
6184 type
.t
|= ad
.a
.visibility
<< VT_VIS_SHIFT
;
6188 tcc_error("cannot use local functions");
6189 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
6190 expect("function definition");
6192 /* reject abstract declarators in function definition */
6194 while ((sym
= sym
->next
) != NULL
)
6195 if (!(sym
->v
& ~SYM_FIELD
))
6196 expect("identifier");
6198 /* XXX: cannot do better now: convert extern line to static inline */
6199 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
6200 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
6205 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
6208 ref
= sym
->type
.ref
;
6209 if (0 == ref
->a
.func_proto
)
6210 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
6212 /* use func_call from prototype if not defined */
6213 if (ref
->a
.func_call
!= FUNC_CDECL
6214 && type
.ref
->a
.func_call
== FUNC_CDECL
)
6215 type
.ref
->a
.func_call
= ref
->a
.func_call
;
6217 /* use export from prototype */
6218 if (ref
->a
.func_export
)
6219 type
.ref
->a
.func_export
= 1;
6221 /* use static from prototype */
6222 if (sym
->type
.t
& VT_STATIC
)
6223 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
6225 /* If the definition has no visibility use the
6226 one from prototype. */
6227 if (! (type
.t
& VT_VIS_MASK
))
6228 type
.t
|= sym
->type
.t
& VT_VIS_MASK
;
6230 if (!is_compatible_types(&sym
->type
, &type
)) {
6232 tcc_error("incompatible types for redefinition of '%s'",
6233 get_tok_str(v
, NULL
));
6235 type
.ref
->a
.func_proto
= 0;
6236 /* if symbol is already defined, then put complete type */
6239 /* put function symbol */
6240 sym
= global_identifier_push(v
, type
.t
, 0);
6241 sym
->type
.ref
= type
.ref
;
6244 /* static inline functions are just recorded as a kind
6245 of macro. Their code will be emitted at the end of
6246 the compilation unit only if they are used */
6247 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
6248 (VT_INLINE
| VT_STATIC
)) {
6249 TokenString func_str
;
6251 struct InlineFunc
*fn
;
6252 const char *filename
;
6254 tok_str_new(&func_str
);
6260 tcc_error("unexpected end of file");
6261 tok_str_add_tok(&func_str
);
6266 } else if (t
== '}') {
6268 if (block_level
== 0)
6272 tok_str_add(&func_str
, -1);
6273 tok_str_add(&func_str
, 0);
6274 filename
= file
? file
->filename
: "";
6275 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
6276 strcpy(fn
->filename
, filename
);
6278 fn
->token_str
= func_str
.str
;
6279 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
6282 /* compute text section */
6283 cur_text_section
= ad
.section
;
6284 if (!cur_text_section
)
6285 cur_text_section
= text_section
;
6286 sym
->r
= VT_SYM
| VT_CONST
;
6291 if (btype
.t
& VT_TYPEDEF
) {
6292 /* save typedefed type */
6293 /* XXX: test storage specifiers ? */
6294 sym
= sym_push(v
, &type
, 0, 0);
6296 sym
->type
.t
|= VT_TYPEDEF
;
6299 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
6300 /* external function definition */
6301 /* specific case for func_call attribute */
6302 ad
.a
.func_proto
= 1;
6304 } else if (!(type
.t
& VT_ARRAY
)) {
6305 /* not lvalue if array */
6306 r
|= lvalue_type(type
.t
);
6308 has_init
= (tok
== '=');
6309 if (has_init
&& (type
.t
& VT_VLA
))
6310 tcc_error("Variable length array cannot be initialized");
6311 if ((btype
.t
& VT_EXTERN
) || ((type
.t
& VT_BTYPE
) == VT_FUNC
) ||
6312 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
6313 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
6314 /* external variable or function */
6315 /* NOTE: as GCC, uninitialized global static
6316 arrays of null size are considered as
6318 sym
= external_sym(v
, &type
, r
, asm_label
);
6320 if (ad
.alias_target
) {
6325 alias_target
= sym_find(ad
.alias_target
);
6326 if (!alias_target
|| !alias_target
->c
)
6327 tcc_error("unsupported forward __alias__ attribute");
6328 esym
= &((Elf32_Sym
*)symtab_section
->data
)[alias_target
->c
];
6329 tsec
.sh_num
= esym
->st_shndx
;
6330 put_extern_sym2(sym
, &tsec
, esym
->st_value
, esym
->st_size
, 0);
6333 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
6334 if (type
.t
& VT_STATIC
)
6340 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, asm_label
, l
);
6344 if (is_for_loop_init
)
6357 ST_FUNC
void decl(int l
)