2 * TCC - Tiny C Compiler
4 * Copyright (c) 2001-2004 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 /********************************************************/
24 /* global variables */
26 /* loc : local variable index
27 ind : output code index
29 anon_sym: anonymous symbol index
31 ST_DATA
int rsym
, anon_sym
, ind
, loc
;
33 ST_DATA Section
*text_section
, *data_section
, *bss_section
; /* predefined sections */
34 ST_DATA Section
*cur_text_section
; /* current section where function code is generated */
36 ST_DATA Section
*last_text_section
; /* to handle .previous asm directive */
38 #ifdef CONFIG_TCC_BCHECK
39 /* bound check related sections */
40 ST_DATA Section
*bounds_section
; /* contains global data bound description */
41 ST_DATA Section
*lbounds_section
; /* contains local data bound description */
44 ST_DATA Section
*symtab_section
, *strtab_section
;
46 ST_DATA Section
*stab_section
, *stabstr_section
;
47 ST_DATA Sym
*sym_free_first
;
48 ST_DATA
void **sym_pools
;
49 ST_DATA
int nb_sym_pools
;
51 ST_DATA Sym
*global_stack
;
52 ST_DATA Sym
*local_stack
;
53 ST_DATA Sym
*scope_stack_bottom
;
54 ST_DATA Sym
*define_stack
;
55 ST_DATA Sym
*global_label_stack
;
56 ST_DATA Sym
*local_label_stack
;
58 ST_DATA
int vlas_in_scope
; /* number of VLAs that are currently in scope */
59 ST_DATA
int vla_sp_root_loc
; /* vla_sp_loc for SP before any VLAs were pushed */
60 ST_DATA
int vla_sp_loc
; /* Pointer to variable holding location to store stack pointer on the stack when modifying stack pointer */
62 ST_DATA SValue __vstack
[1+VSTACK_SIZE
], *vtop
, *pvtop
;
64 ST_DATA
int const_wanted
; /* true if constant wanted */
65 ST_DATA
int nocode_wanted
; /* true if no code generation wanted for an expression */
66 ST_DATA
int global_expr
; /* true if compound literals must be allocated globally (used during initializers parsing */
67 ST_DATA CType func_vt
; /* current function return type (used by return instruction) */
68 ST_DATA
int func_var
; /* true if current function is variadic (used by return instruction) */
70 ST_DATA
int last_line_num
, last_ind
, func_ind
; /* debug last line number and pc */
71 ST_DATA
const char *funcname
;
73 ST_DATA CType char_pointer_type
, func_old_type
, int_type
, size_type
;
75 /* ------------------------------------------------------------------------- */
76 static void gen_cast(CType
*type
);
77 static inline CType
*pointed_type(CType
*type
);
78 static int is_compatible_types(CType
*type1
, CType
*type2
);
79 static int parse_btype(CType
*type
, AttributeDef
*ad
);
80 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
);
81 static void parse_expr_type(CType
*type
);
82 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
, int first
, int size_only
);
83 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
, int case_reg
, int is_expr
);
84 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
, int has_init
, int v
, char *asm_label
, int scope
);
85 static int decl0(int l
, int is_for_loop_init
);
86 static void expr_eq(void);
87 static void unary_type(CType
*type
);
88 static void vla_runtime_type_size(CType
*type
, int *a
);
89 static void vla_sp_restore(void);
90 static void vla_sp_restore_root(void);
91 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
);
92 static void expr_type(CType
*type
);
93 ST_FUNC
void vpush64(int ty
, unsigned long long v
);
94 ST_FUNC
void vpush(CType
*type
);
95 ST_FUNC
int gvtst(int inv
, int t
);
96 ST_FUNC
int is_btype_size(int bt
);
98 ST_INLN
int is_float(int t
)
102 return bt
== VT_LDOUBLE
|| bt
== VT_DOUBLE
|| bt
== VT_FLOAT
|| bt
== VT_QFLOAT
;
105 /* we use our own 'finite' function to avoid potential problems with
106 non standard math libs */
107 /* XXX: endianness dependent */
108 ST_FUNC
int ieee_finite(double d
)
111 memcpy(p
, &d
, sizeof(double));
112 return ((unsigned)((p
[1] | 0x800fffff) + 1)) >> 31;
115 ST_FUNC
void test_lvalue(void)
117 if (!(vtop
->r
& VT_LVAL
))
121 ST_FUNC
void check_vstack(void)
124 tcc_error("internal compiler error: vstack leak (%d)", vtop
- pvtop
);
127 /* ------------------------------------------------------------------------- */
128 /* symbol allocator */
129 static Sym
*__sym_malloc(void)
131 Sym
*sym_pool
, *sym
, *last_sym
;
134 sym_pool
= tcc_malloc(SYM_POOL_NB
* sizeof(Sym
));
135 dynarray_add(&sym_pools
, &nb_sym_pools
, sym_pool
);
137 last_sym
= sym_free_first
;
139 for(i
= 0; i
< SYM_POOL_NB
; i
++) {
140 sym
->next
= last_sym
;
144 sym_free_first
= last_sym
;
148 static inline Sym
*sym_malloc(void)
151 sym
= sym_free_first
;
153 sym
= __sym_malloc();
154 sym_free_first
= sym
->next
;
158 ST_INLN
void sym_free(Sym
*sym
)
160 sym
->next
= sym_free_first
;
161 tcc_free(sym
->asm_label
);
162 sym_free_first
= sym
;
165 /* push, without hashing */
166 ST_FUNC Sym
*sym_push2(Sym
**ps
, int v
, int t
, long c
)
169 if (ps
== &local_stack
) {
170 for (s
= *ps
; s
&& s
!= scope_stack_bottom
; s
= s
->prev
)
171 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
&& s
->v
== v
)
172 tcc_error("incompatible types for redefinition of '%s'",
173 get_tok_str(v
, NULL
));
191 /* find a symbol and return its associated structure. 's' is the top
192 of the symbol stack */
193 ST_FUNC Sym
*sym_find2(Sym
*s
, int v
)
205 /* structure lookup */
206 ST_INLN Sym
*struct_find(int v
)
209 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
211 return table_ident
[v
]->sym_struct
;
214 /* find an identifier */
215 ST_INLN Sym
*sym_find(int v
)
218 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
220 return table_ident
[v
]->sym_identifier
;
223 /* push a given symbol on the symbol stack */
224 ST_FUNC Sym
*sym_push(int v
, CType
*type
, int r
, int c
)
233 s
= sym_push2(ps
, v
, type
->t
, c
);
234 s
->type
.ref
= type
->ref
;
236 /* don't record fields or anonymous symbols */
238 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
239 /* record symbol in token array */
240 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
242 ps
= &ts
->sym_struct
;
244 ps
= &ts
->sym_identifier
;
251 /* push a global identifier */
252 ST_FUNC Sym
*global_identifier_push(int v
, int t
, int c
)
255 s
= sym_push2(&global_stack
, v
, t
, c
);
256 /* don't record anonymous symbol */
257 if (v
< SYM_FIRST_ANOM
) {
258 ps
= &table_ident
[v
- TOK_IDENT
]->sym_identifier
;
259 /* modify the top most local identifier, so that
260 sym_identifier will point to 's' when popped */
262 ps
= &(*ps
)->prev_tok
;
269 /* pop symbols until top reaches 'b' */
270 ST_FUNC
void sym_pop(Sym
**ptop
, Sym
*b
)
280 /* remove symbol in token array */
282 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
283 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
285 ps
= &ts
->sym_struct
;
287 ps
= &ts
->sym_identifier
;
296 static void weaken_symbol(Sym
*sym
)
298 sym
->type
.t
|= VT_WEAK
;
303 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
304 esym_type
= ELFW(ST_TYPE
)(esym
->st_info
);
305 esym
->st_info
= ELFW(ST_INFO
)(STB_WEAK
, esym_type
);
309 static void apply_visibility(Sym
*sym
, CType
*type
)
311 int vis
= sym
->type
.t
& VT_VIS_MASK
;
312 int vis2
= type
->t
& VT_VIS_MASK
;
313 if (vis
== (STV_DEFAULT
<< VT_VIS_SHIFT
))
315 else if (vis2
== (STV_DEFAULT
<< VT_VIS_SHIFT
))
318 vis
= (vis
< vis2
) ? vis
: vis2
;
319 sym
->type
.t
&= ~VT_VIS_MASK
;
325 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
326 vis
>>= VT_VIS_SHIFT
;
327 esym
->st_other
= (esym
->st_other
& ~ELFW(ST_VISIBILITY
)(-1)) | vis
;
331 /* ------------------------------------------------------------------------- */
333 ST_FUNC
void swap(int *p
, int *q
)
341 static void vsetc(CType
*type
, int r
, CValue
*vc
)
345 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
346 tcc_error("memory full (vstack)");
347 /* cannot let cpu flags if other instruction are generated. Also
348 avoid leaving VT_JMP anywhere except on the top of the stack
349 because it would complicate the code generator. */
350 if (vtop
>= vstack
) {
351 v
= vtop
->r
& VT_VALMASK
;
352 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
362 /* push constant of type "type" with useless value */
363 ST_FUNC
void vpush(CType
*type
)
366 vsetc(type
, VT_CONST
, &cval
);
369 /* push integer constant */
370 ST_FUNC
void vpushi(int v
)
374 vsetc(&int_type
, VT_CONST
, &cval
);
377 /* push a pointer sized constant */
378 static void vpushs(addr_t v
)
382 vsetc(&size_type
, VT_CONST
, &cval
);
385 /* push arbitrary 64bit constant */
386 ST_FUNC
void vpush64(int ty
, unsigned long long v
)
393 vsetc(&ctype
, VT_CONST
, &cval
);
396 /* push long long constant */
397 static inline void vpushll(long long v
)
399 vpush64(VT_LLONG
, v
);
402 /* push a symbol value of TYPE */
403 static inline void vpushsym(CType
*type
, Sym
*sym
)
407 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
411 /* Return a static symbol pointing to a section */
412 ST_FUNC Sym
*get_sym_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
418 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
419 sym
->type
.ref
= type
->ref
;
420 sym
->r
= VT_CONST
| VT_SYM
;
421 put_extern_sym(sym
, sec
, offset
, size
);
425 /* push a reference to a section offset by adding a dummy symbol */
426 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
428 vpushsym(type
, get_sym_ref(type
, sec
, offset
, size
));
431 /* define a new external reference to a symbol 'v' of type 'u' */
432 ST_FUNC Sym
*external_global_sym(int v
, CType
*type
, int r
)
438 /* push forward reference */
439 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
440 s
->type
.ref
= type
->ref
;
441 s
->r
= r
| VT_CONST
| VT_SYM
;
446 /* define a new external reference to a symbol 'v' with alternate asm
447 name 'asm_label' of type 'u'. 'asm_label' is equal to NULL if there
448 is no alternate name (most cases) */
449 static Sym
*external_sym(int v
, CType
*type
, int r
, char *asm_label
)
455 /* push forward reference */
456 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
457 s
->asm_label
= asm_label
? tcc_strdup(asm_label
) : 0;
458 s
->type
.t
|= VT_EXTERN
;
459 } else if (s
->type
.ref
== func_old_type
.ref
) {
460 s
->type
.ref
= type
->ref
;
461 s
->r
= r
| VT_CONST
| VT_SYM
;
462 s
->type
.t
|= VT_EXTERN
;
463 } else if (!is_compatible_types(&s
->type
, type
)) {
464 tcc_error("incompatible types for redefinition of '%s'",
465 get_tok_str(v
, NULL
));
467 /* Merge some storage attributes. */
468 if (type
->t
& VT_WEAK
)
471 if (type
->t
& VT_VIS_MASK
)
472 apply_visibility(s
, type
);
477 /* push a reference to global symbol v */
478 ST_FUNC
void vpush_global_sym(CType
*type
, int v
)
480 vpushsym(type
, external_global_sym(v
, type
, 0));
483 ST_FUNC
void vset(CType
*type
, int r
, int v
)
488 vsetc(type
, r
, &cval
);
491 static void vseti(int r
, int v
)
499 ST_FUNC
void vswap(void)
502 /* cannot let cpu flags if other instruction are generated. Also
503 avoid leaving VT_JMP anywhere except on the top of the stack
504 because it would complicate the code generator. */
505 if (vtop
>= vstack
) {
506 int v
= vtop
->r
& VT_VALMASK
;
507 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
514 /* XXX: +2% overall speed possible with optimized memswap
516 * memswap(&vtop[0], &vtop[1], sizeof *vtop);
520 ST_FUNC
void vpushv(SValue
*v
)
522 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
523 tcc_error("memory full (vstack)");
528 static void vdup(void)
533 /* save r to the memory stack, and mark it as being free */
534 ST_FUNC
void save_reg(int r
)
536 int l
, saved
, size
, align
;
540 /* modify all stack values */
543 for(p
=vstack
;p
<=vtop
;p
++) {
544 if ((p
->r
& VT_VALMASK
) == r
||
545 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
546 /* must save value on stack if not already done */
548 /* NOTE: must reload 'r' because r might be equal to r2 */
549 r
= p
->r
& VT_VALMASK
;
550 /* store register in the stack */
552 if ((p
->r
& VT_LVAL
) ||
553 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
554 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
555 type
= &char_pointer_type
;
559 size
= type_size(type
, &align
);
560 loc
= (loc
- size
) & -align
;
562 sv
.r
= VT_LOCAL
| VT_LVAL
;
565 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
566 /* x86 specific: need to pop fp register ST0 if saved */
568 o(0xd8dd); /* fstp %st(0) */
571 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
572 /* special long long case */
573 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
581 /* mark that stack entry as being saved on the stack */
582 if (p
->r
& VT_LVAL
) {
583 /* also clear the bounded flag because the
584 relocation address of the function was stored in
586 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
588 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
596 #ifdef TCC_TARGET_ARM
597 /* find a register of class 'rc2' with at most one reference on stack.
598 * If none, call get_reg(rc) */
599 ST_FUNC
int get_reg_ex(int rc
, int rc2
)
604 for(r
=0;r
<NB_REGS
;r
++) {
605 if (reg_classes
[r
] & rc2
) {
608 for(p
= vstack
; p
<= vtop
; p
++) {
609 if ((p
->r
& VT_VALMASK
) == r
||
610 (p
->r2
& VT_VALMASK
) == r
)
621 /* find a free register of class 'rc'. If none, save one register */
622 ST_FUNC
int get_reg(int rc
)
627 /* find a free register */
628 for(r
=0;r
<NB_REGS
;r
++) {
629 if (reg_classes
[r
] & rc
) {
630 for(p
=vstack
;p
<=vtop
;p
++) {
631 if ((p
->r
& VT_VALMASK
) == r
||
632 (p
->r2
& VT_VALMASK
) == r
)
640 /* no register left : free the first one on the stack (VERY
641 IMPORTANT to start from the bottom to ensure that we don't
642 spill registers used in gen_opi()) */
643 for(p
=vstack
;p
<=vtop
;p
++) {
644 /* look at second register (if long long) */
645 r
= p
->r2
& VT_VALMASK
;
646 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
648 r
= p
->r
& VT_VALMASK
;
649 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
655 /* Should never comes here */
659 /* save registers up to (vtop - n) stack entry */
660 ST_FUNC
void save_regs(int n
)
665 for(p
= vstack
;p
<= p1
; p
++) {
666 r
= p
->r
& VT_VALMASK
;
673 /* move register 's' (of type 't') to 'r', and flush previous value of r to memory
675 static void move_reg(int r
, int s
, int t
)
689 /* get address of vtop (vtop MUST BE an lvalue) */
690 ST_FUNC
void gaddrof(void)
692 if (vtop
->r
& VT_REF
&& !nocode_wanted
)
695 /* tricky: if saved lvalue, then we can go back to lvalue */
696 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
697 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
702 #ifdef CONFIG_TCC_BCHECK
703 /* generate lvalue bound code */
704 static void gbound(void)
709 vtop
->r
&= ~VT_MUSTBOUND
;
710 /* if lvalue, then use checking code before dereferencing */
711 if (vtop
->r
& VT_LVAL
) {
712 /* if not VT_BOUNDED value, then make one */
713 if (!(vtop
->r
& VT_BOUNDED
)) {
714 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
715 /* must save type because we must set it to int to get pointer */
717 vtop
->type
.t
= VT_PTR
;
720 gen_bounded_ptr_add();
721 vtop
->r
|= lval_type
;
724 /* then check for dereferencing */
725 gen_bounded_ptr_deref();
730 /* store vtop a register belonging to class 'rc'. lvalues are
731 converted to values. Cannot be used if cannot be converted to
732 register value (such as structures). */
733 ST_FUNC
int gv(int rc
)
735 int r
, bit_pos
, bit_size
, size
, align
, i
;
738 /* NOTE: get_reg can modify vstack[] */
739 if (vtop
->type
.t
& VT_BITFIELD
) {
742 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
743 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
744 /* remove bit field info to avoid loops */
745 vtop
->type
.t
&= ~VT_BITFIELD
& ((1 << VT_STRUCT_SHIFT
) - 1);
746 /* cast to int to propagate signedness in following ops */
747 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
752 if((vtop
->type
.t
& VT_UNSIGNED
) ||
753 (vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
754 type
.t
|= VT_UNSIGNED
;
756 /* generate shifts */
757 vpushi(bits
- (bit_pos
+ bit_size
));
759 vpushi(bits
- bit_size
);
760 /* NOTE: transformed to SHR if unsigned */
764 if (is_float(vtop
->type
.t
) &&
765 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
768 unsigned long offset
;
769 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
773 /* XXX: unify with initializers handling ? */
774 /* CPUs usually cannot use float constants, so we store them
775 generically in data segment */
776 size
= type_size(&vtop
->type
, &align
);
777 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
778 data_section
->data_offset
= offset
;
779 /* XXX: not portable yet */
780 #if defined(__i386__) || defined(__x86_64__)
781 /* Zero pad x87 tenbyte long doubles */
782 if (size
== LDOUBLE_SIZE
) {
783 vtop
->c
.tab
[2] &= 0xffff;
784 #if LDOUBLE_SIZE == 16
789 ptr
= section_ptr_add(data_section
, size
);
791 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
795 ptr
[i
] = vtop
->c
.tab
[size
-1-i
];
799 ptr
[i
] = vtop
->c
.tab
[i
];
800 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
801 vtop
->r
|= VT_LVAL
| VT_SYM
;
803 vtop
->c
.ptr_offset
= 0;
805 #ifdef CONFIG_TCC_BCHECK
806 if (vtop
->r
& VT_MUSTBOUND
)
810 r
= vtop
->r
& VT_VALMASK
;
811 rc2
= (rc
& RC_FLOAT
) ? RC_FLOAT
: RC_INT
;
812 #ifndef TCC_TARGET_ARM64
815 #ifdef TCC_TARGET_X86_64
816 else if (rc
== RC_FRET
)
821 /* need to reload if:
823 - lvalue (need to dereference pointer)
824 - already a register, but not in the right class */
826 || (vtop
->r
& VT_LVAL
)
827 || !(reg_classes
[r
] & rc
)
828 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
829 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
830 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
&& !(reg_classes
[vtop
->r2
] & rc2
))
832 || ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
837 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
838 if (((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
)) {
839 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
841 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
842 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
843 unsigned long long ll
;
845 int r2
, original_type
;
846 original_type
= vtop
->type
.t
;
847 /* two register type load : expand to two words
849 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
850 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
853 vtop
->c
.ui
= ll
; /* first word */
855 vtop
->r
= r
; /* save register value */
856 vpushi(ll
>> 32); /* second word */
859 if (r
>= VT_CONST
|| /* XXX: test to VT_CONST incorrect ? */
860 (vtop
->r
& VT_LVAL
)) {
861 /* We do not want to modifier the long long
862 pointer here, so the safest (and less
863 efficient) is to save all the other registers
864 in the stack. XXX: totally inefficient. */
866 /* load from memory */
867 vtop
->type
.t
= load_type
;
870 vtop
[-1].r
= r
; /* save register value */
871 /* increment pointer to get second word */
872 vtop
->type
.t
= addr_type
;
877 vtop
->type
.t
= load_type
;
882 vtop
[-1].r
= r
; /* save register value */
883 vtop
->r
= vtop
[-1].r2
;
885 /* Allocate second register. Here we rely on the fact that
886 get_reg() tries first to free r2 of an SValue. */
890 /* write second register */
892 vtop
->type
.t
= original_type
;
893 } else if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
895 /* lvalue of scalar type : need to use lvalue type
896 because of possible cast */
899 /* compute memory access type */
900 if (vtop
->r
& VT_REF
)
901 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
906 else if (vtop
->r
& VT_LVAL_BYTE
)
908 else if (vtop
->r
& VT_LVAL_SHORT
)
910 if (vtop
->r
& VT_LVAL_UNSIGNED
)
914 /* restore wanted type */
917 /* one register type load */
922 #ifdef TCC_TARGET_C67
923 /* uses register pairs for doubles */
924 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
931 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
932 ST_FUNC
void gv2(int rc1
, int rc2
)
936 /* generate more generic register first. But VT_JMP or VT_CMP
937 values must be generated first in all cases to avoid possible
939 v
= vtop
[0].r
& VT_VALMASK
;
940 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
945 /* test if reload is needed for first register */
946 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
956 /* test if reload is needed for first register */
957 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
963 #ifndef TCC_TARGET_ARM64
964 /* wrapper around RC_FRET to return a register by type */
965 static int rc_fret(int t
)
967 #ifdef TCC_TARGET_X86_64
968 if (t
== VT_LDOUBLE
) {
976 /* wrapper around REG_FRET to return a register by type */
977 static int reg_fret(int t
)
979 #ifdef TCC_TARGET_X86_64
980 if (t
== VT_LDOUBLE
) {
987 /* expand long long on stack in two int registers */
988 static void lexpand(void)
992 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
995 vtop
[0].r
= vtop
[-1].r2
;
996 vtop
[0].r2
= VT_CONST
;
997 vtop
[-1].r2
= VT_CONST
;
998 vtop
[0].type
.t
= VT_INT
| u
;
999 vtop
[-1].type
.t
= VT_INT
| u
;
1002 #ifdef TCC_TARGET_ARM
1003 /* expand long long on stack */
1004 ST_FUNC
void lexpand_nr(void)
1008 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
1010 vtop
->r2
= VT_CONST
;
1011 vtop
->type
.t
= VT_INT
| u
;
1012 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
1013 if (v
== VT_CONST
) {
1014 vtop
[-1].c
.ui
= vtop
->c
.ull
;
1015 vtop
->c
.ui
= vtop
->c
.ull
>> 32;
1017 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
1019 vtop
->r
= vtop
[-1].r
;
1020 } else if (v
> VT_CONST
) {
1024 vtop
->r
= vtop
[-1].r2
;
1025 vtop
[-1].r2
= VT_CONST
;
1026 vtop
[-1].type
.t
= VT_INT
| u
;
1030 /* build a long long from two ints */
1031 static void lbuild(int t
)
1033 gv2(RC_INT
, RC_INT
);
1034 vtop
[-1].r2
= vtop
[0].r
;
1035 vtop
[-1].type
.t
= t
;
1039 /* rotate n first stack elements to the bottom
1040 I1 ... In -> I2 ... In I1 [top is right]
1042 ST_FUNC
void vrotb(int n
)
1048 for(i
=-n
+1;i
!=0;i
++)
1049 vtop
[i
] = vtop
[i
+1];
1053 /* rotate the n elements before entry e towards the top
1054 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
1056 ST_FUNC
void vrote(SValue
*e
, int n
)
1062 for(i
= 0;i
< n
- 1; i
++)
1067 /* rotate n first stack elements to the top
1068 I1 ... In -> In I1 ... I(n-1) [top is right]
1070 ST_FUNC
void vrott(int n
)
1075 /* pop stack value */
1076 ST_FUNC
void vpop(void)
1079 v
= vtop
->r
& VT_VALMASK
;
1080 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1081 /* for x86, we need to pop the FP stack */
1082 if (v
== TREG_ST0
&& !nocode_wanted
) {
1083 o(0xd8dd); /* fstp %st(0) */
1086 if (v
== VT_JMP
|| v
== VT_JMPI
) {
1087 /* need to put correct jump if && or || without test */
1093 /* convert stack entry to register and duplicate its value in another
1095 static void gv_dup(void)
1101 if ((t
& VT_BTYPE
) == VT_LLONG
) {
1108 /* stack: H L L1 H1 */
1116 /* duplicate value */
1121 #ifdef TCC_TARGET_X86_64
1122 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1132 load(r1
, &sv
); /* move r to r1 */
1134 /* duplicates value */
1140 /* Generate value test
1142 * Generate a test for any value (jump, comparison and integers) */
1143 ST_FUNC
int gvtst(int inv
, int t
)
1145 int v
= vtop
->r
& VT_VALMASK
;
1146 if (v
!= VT_CMP
&& v
!= VT_JMP
&& v
!= VT_JMPI
) {
1150 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1151 /* constant jmp optimization */
1152 if ((vtop
->c
.i
!= 0) != inv
)
1157 return gtst(inv
, t
);
1160 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
1161 /* generate CPU independent (unsigned) long long operations */
1162 static void gen_opl(int op
)
1164 int t
, a
, b
, op1
, c
, i
;
1166 unsigned short reg_iret
= REG_IRET
;
1167 unsigned short reg_lret
= REG_LRET
;
1173 func
= TOK___divdi3
;
1176 func
= TOK___udivdi3
;
1179 func
= TOK___moddi3
;
1182 func
= TOK___umoddi3
;
1189 /* call generic long long function */
1190 vpush_global_sym(&func_old_type
, func
);
1195 vtop
->r2
= reg_lret
;
1208 /* stack: L1 H1 L2 H2 */
1213 vtop
[-2] = vtop
[-3];
1216 /* stack: H1 H2 L1 L2 */
1222 /* stack: H1 H2 L1 L2 ML MH */
1225 /* stack: ML MH H1 H2 L1 L2 */
1229 /* stack: ML MH H1 L2 H2 L1 */
1234 /* stack: ML MH M1 M2 */
1237 } else if (op
== '+' || op
== '-') {
1238 /* XXX: add non carry method too (for MIPS or alpha) */
1244 /* stack: H1 H2 (L1 op L2) */
1247 gen_op(op1
+ 1); /* TOK_xxxC2 */
1250 /* stack: H1 H2 (L1 op L2) */
1253 /* stack: (L1 op L2) H1 H2 */
1255 /* stack: (L1 op L2) (H1 op H2) */
1263 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1264 t
= vtop
[-1].type
.t
;
1268 /* stack: L H shift */
1270 /* constant: simpler */
1271 /* NOTE: all comments are for SHL. the other cases are
1272 done by swaping words */
1283 if (op
!= TOK_SAR
) {
1316 /* XXX: should provide a faster fallback on x86 ? */
1319 func
= TOK___ashrdi3
;
1322 func
= TOK___lshrdi3
;
1325 func
= TOK___ashldi3
;
1331 /* compare operations */
1337 /* stack: L1 H1 L2 H2 */
1339 vtop
[-1] = vtop
[-2];
1341 /* stack: L1 L2 H1 H2 */
1344 /* when values are equal, we need to compare low words. since
1345 the jump is inverted, we invert the test too. */
1348 else if (op1
== TOK_GT
)
1350 else if (op1
== TOK_ULT
)
1352 else if (op1
== TOK_UGT
)
1357 if (op1
!= TOK_NE
) {
1361 /* generate non equal test */
1362 /* XXX: NOT PORTABLE yet */
1366 #if defined(TCC_TARGET_I386)
1367 b
= psym(0x850f, 0);
1368 #elif defined(TCC_TARGET_ARM)
1370 o(0x1A000000 | encbranch(ind
, 0, 1));
1371 #elif defined(TCC_TARGET_C67) || defined(TCC_TARGET_ARM64)
1372 tcc_error("not implemented");
1374 #error not supported
1378 /* compare low. Always unsigned */
1382 else if (op1
== TOK_LE
)
1384 else if (op1
== TOK_GT
)
1386 else if (op1
== TOK_GE
)
1397 /* handle integer constant optimizations and various machine
1399 static void gen_opic(int op
)
1401 int c1
, c2
, t1
, t2
, n
;
1404 typedef unsigned long long U
;
1408 t1
= v1
->type
.t
& VT_BTYPE
;
1409 t2
= v2
->type
.t
& VT_BTYPE
;
1413 else if (v1
->type
.t
& VT_UNSIGNED
)
1420 else if (v2
->type
.t
& VT_UNSIGNED
)
1425 /* currently, we cannot do computations with forward symbols */
1426 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1427 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1430 case '+': l1
+= l2
; break;
1431 case '-': l1
-= l2
; break;
1432 case '&': l1
&= l2
; break;
1433 case '^': l1
^= l2
; break;
1434 case '|': l1
|= l2
; break;
1435 case '*': l1
*= l2
; break;
1442 /* if division by zero, generate explicit division */
1445 tcc_error("division by zero in constant");
1449 default: l1
/= l2
; break;
1450 case '%': l1
%= l2
; break;
1451 case TOK_UDIV
: l1
= (U
)l1
/ l2
; break;
1452 case TOK_UMOD
: l1
= (U
)l1
% l2
; break;
1455 case TOK_SHL
: l1
<<= l2
; break;
1456 case TOK_SHR
: l1
= (U
)l1
>> l2
; break;
1457 case TOK_SAR
: l1
>>= l2
; break;
1459 case TOK_ULT
: l1
= (U
)l1
< (U
)l2
; break;
1460 case TOK_UGE
: l1
= (U
)l1
>= (U
)l2
; break;
1461 case TOK_EQ
: l1
= l1
== l2
; break;
1462 case TOK_NE
: l1
= l1
!= l2
; break;
1463 case TOK_ULE
: l1
= (U
)l1
<= (U
)l2
; break;
1464 case TOK_UGT
: l1
= (U
)l1
> (U
)l2
; break;
1465 case TOK_LT
: l1
= l1
< l2
; break;
1466 case TOK_GE
: l1
= l1
>= l2
; break;
1467 case TOK_LE
: l1
= l1
<= l2
; break;
1468 case TOK_GT
: l1
= l1
> l2
; break;
1470 case TOK_LAND
: l1
= l1
&& l2
; break;
1471 case TOK_LOR
: l1
= l1
|| l2
; break;
1478 /* if commutative ops, put c2 as constant */
1479 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1480 op
== '|' || op
== '*')) {
1482 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1483 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1485 if (!const_wanted
&&
1487 (op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
)) ||
1488 (l1
== -1 && op
== TOK_SAR
))) {
1489 /* treat (0 << x), (0 >> x) and (-1 >> x) as constant */
1491 } else if (!const_wanted
&&
1492 c2
&& ((l2
== 0 && (op
== '&' || op
== '*')) ||
1493 (l2
== -1 && op
== '|') ||
1494 (l2
== 0xffffffff && t2
!= VT_LLONG
&& op
== '|') ||
1495 (l2
== 1 && (op
== '%' || op
== TOK_UMOD
)))) {
1496 /* treat (x & 0), (x * 0), (x | -1) and (x % 1) as constant */
1501 } else if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1504 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1505 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1509 /* filter out NOP operations like x*1, x-0, x&-1... */
1511 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1512 /* try to use shifts instead of muls or divs */
1513 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1522 else if (op
== TOK_PDIV
)
1528 } else if (c2
&& (op
== '+' || op
== '-') &&
1529 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
1530 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1531 /* symbol + constant case */
1538 if (!nocode_wanted
) {
1539 /* call low level op generator */
1540 if (t1
== VT_LLONG
|| t2
== VT_LLONG
||
1541 (PTR_SIZE
== 8 && (t1
== VT_PTR
|| t2
== VT_PTR
)))
1552 /* generate a floating point operation with constant propagation */
1553 static void gen_opif(int op
)
1561 /* currently, we cannot do computations with forward symbols */
1562 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1563 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1565 if (v1
->type
.t
== VT_FLOAT
) {
1568 } else if (v1
->type
.t
== VT_DOUBLE
) {
1576 /* NOTE: we only do constant propagation if finite number (not
1577 NaN or infinity) (ANSI spec) */
1578 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1582 case '+': f1
+= f2
; break;
1583 case '-': f1
-= f2
; break;
1584 case '*': f1
*= f2
; break;
1588 tcc_error("division by zero in constant");
1593 /* XXX: also handles tests ? */
1597 /* XXX: overflow test ? */
1598 if (v1
->type
.t
== VT_FLOAT
) {
1600 } else if (v1
->type
.t
== VT_DOUBLE
) {
1608 if (!nocode_wanted
) {
1616 static int pointed_size(CType
*type
)
1619 return type_size(pointed_type(type
), &align
);
1622 static void vla_runtime_pointed_size(CType
*type
)
1625 vla_runtime_type_size(pointed_type(type
), &align
);
1628 static inline int is_null_pointer(SValue
*p
)
1630 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1632 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& p
->c
.i
== 0) ||
1633 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.ll
== 0) ||
1634 ((p
->type
.t
& VT_BTYPE
) == VT_PTR
&& p
->c
.ptr_offset
== 0);
1637 static inline int is_integer_btype(int bt
)
1639 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
1640 bt
== VT_INT
|| bt
== VT_LLONG
);
1643 /* check types for comparison or subtraction of pointers */
1644 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
1646 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
1649 /* null pointers are accepted for all comparisons as gcc */
1650 if (is_null_pointer(p1
) || is_null_pointer(p2
))
1654 bt1
= type1
->t
& VT_BTYPE
;
1655 bt2
= type2
->t
& VT_BTYPE
;
1656 /* accept comparison between pointer and integer with a warning */
1657 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
1658 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
1659 tcc_warning("comparison between pointer and integer");
1663 /* both must be pointers or implicit function pointers */
1664 if (bt1
== VT_PTR
) {
1665 type1
= pointed_type(type1
);
1666 } else if (bt1
!= VT_FUNC
)
1667 goto invalid_operands
;
1669 if (bt2
== VT_PTR
) {
1670 type2
= pointed_type(type2
);
1671 } else if (bt2
!= VT_FUNC
) {
1673 tcc_error("invalid operands to binary %s", get_tok_str(op
, NULL
));
1675 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1676 (type2
->t
& VT_BTYPE
) == VT_VOID
)
1680 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1681 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1682 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1683 /* gcc-like error if '-' is used */
1685 goto invalid_operands
;
1687 tcc_warning("comparison of distinct pointer types lacks a cast");
1691 /* generic gen_op: handles types problems */
1692 ST_FUNC
void gen_op(int op
)
1694 int u
, t1
, t2
, bt1
, bt2
, t
;
1697 t1
= vtop
[-1].type
.t
;
1698 t2
= vtop
[0].type
.t
;
1699 bt1
= t1
& VT_BTYPE
;
1700 bt2
= t2
& VT_BTYPE
;
1702 if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
1703 /* at least one operand is a pointer */
1704 /* relationnal op: must be both pointers */
1705 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
1706 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1707 /* pointers are handled are unsigned */
1708 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1709 t
= VT_LLONG
| VT_UNSIGNED
;
1711 t
= VT_INT
| VT_UNSIGNED
;
1715 /* if both pointers, then it must be the '-' op */
1716 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
1718 tcc_error("cannot use pointers here");
1719 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1720 /* XXX: check that types are compatible */
1721 if (vtop
[-1].type
.t
& VT_VLA
) {
1722 vla_runtime_pointed_size(&vtop
[-1].type
);
1724 vpushi(pointed_size(&vtop
[-1].type
));
1728 /* set to integer type */
1729 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1730 vtop
->type
.t
= VT_LLONG
;
1732 vtop
->type
.t
= VT_INT
;
1737 /* exactly one pointer : must be '+' or '-'. */
1738 if (op
!= '-' && op
!= '+')
1739 tcc_error("cannot use pointers here");
1740 /* Put pointer as first operand */
1741 if (bt2
== VT_PTR
) {
1745 type1
= vtop
[-1].type
;
1746 type1
.t
&= ~VT_ARRAY
;
1747 if (vtop
[-1].type
.t
& VT_VLA
)
1748 vla_runtime_pointed_size(&vtop
[-1].type
);
1750 u
= pointed_size(&vtop
[-1].type
);
1752 tcc_error("unknown array element size");
1753 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1756 /* XXX: cast to int ? (long long case) */
1762 /* #ifdef CONFIG_TCC_BCHECK
1763 The main reason to removing this code:
1770 fprintf(stderr, "v+i-j = %p\n", v+i-j);
1771 fprintf(stderr, "v+(i-j) = %p\n", v+(i-j));
1773 When this code is on. then the output looks like
1775 v+(i-j) = 0xbff84000
1777 /* if evaluating constant expression, no code should be
1778 generated, so no bound check */
1779 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
1780 /* if bounded pointers, we generate a special code to
1787 gen_bounded_ptr_add();
1793 /* put again type if gen_opic() swaped operands */
1796 } else if (is_float(bt1
) || is_float(bt2
)) {
1797 /* compute bigger type and do implicit casts */
1798 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
1800 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
1805 /* floats can only be used for a few operations */
1806 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
1807 (op
< TOK_ULT
|| op
> TOK_GT
))
1808 tcc_error("invalid operands for binary operation");
1810 } else if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
) {
1811 t
= bt1
== VT_LLONG
? VT_LLONG
: VT_INT
;
1812 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (t
| VT_UNSIGNED
))
1815 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
1816 /* cast to biggest op */
1818 /* convert to unsigned if it does not fit in a long long */
1819 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
1820 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
1823 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
1824 tcc_error("comparison of struct");
1826 /* integer operations */
1828 /* convert to unsigned if it does not fit in an integer */
1829 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
1830 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
1833 /* XXX: currently, some unsigned operations are explicit, so
1834 we modify them here */
1835 if (t
& VT_UNSIGNED
) {
1842 else if (op
== TOK_LT
)
1844 else if (op
== TOK_GT
)
1846 else if (op
== TOK_LE
)
1848 else if (op
== TOK_GE
)
1855 /* special case for shifts and long long: we keep the shift as
1857 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
1864 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
1865 /* relationnal op: the result is an int */
1866 vtop
->type
.t
= VT_INT
;
1871 // Make sure that we have converted to an rvalue:
1872 if (vtop
->r
& VT_LVAL
&& !nocode_wanted
)
1873 gv(is_float(vtop
->type
.t
& VT_BTYPE
) ? RC_FLOAT
: RC_INT
);
1876 #ifndef TCC_TARGET_ARM
1877 /* generic itof for unsigned long long case */
1878 static void gen_cvt_itof1(int t
)
1880 #ifdef TCC_TARGET_ARM64
1883 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
1884 (VT_LLONG
| VT_UNSIGNED
)) {
1887 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
1888 #if LDOUBLE_SIZE != 8
1889 else if (t
== VT_LDOUBLE
)
1890 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
1893 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
1897 vtop
->r
= reg_fret(t
);
1905 /* generic ftoi for unsigned long long case */
1906 static void gen_cvt_ftoi1(int t
)
1908 #ifdef TCC_TARGET_ARM64
1913 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
1914 /* not handled natively */
1915 st
= vtop
->type
.t
& VT_BTYPE
;
1917 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
1918 #if LDOUBLE_SIZE != 8
1919 else if (st
== VT_LDOUBLE
)
1920 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
1923 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
1928 vtop
->r2
= REG_LRET
;
1935 /* force char or short cast */
1936 static void force_charshort_cast(int t
)
1940 /* XXX: add optimization if lvalue : just change type and offset */
1945 if (t
& VT_UNSIGNED
) {
1946 vpushi((1 << bits
) - 1);
1952 /* result must be signed or the SAR is converted to an SHL
1953 This was not the case when "t" was a signed short
1954 and the last value on the stack was an unsigned int */
1955 vtop
->type
.t
&= ~VT_UNSIGNED
;
1961 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1962 static void gen_cast(CType
*type
)
1964 int sbt
, dbt
, sf
, df
, c
, p
;
1966 /* special delayed cast for char/short */
1967 /* XXX: in some cases (multiple cascaded casts), it may still
1969 if (vtop
->r
& VT_MUSTCAST
) {
1970 vtop
->r
&= ~VT_MUSTCAST
;
1971 force_charshort_cast(vtop
->type
.t
);
1974 /* bitfields first get cast to ints */
1975 if (vtop
->type
.t
& VT_BITFIELD
&& !nocode_wanted
) {
1979 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
1980 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
1985 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1986 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
1988 /* constant case: we can do it now */
1989 /* XXX: in ISOC, cannot do it if error in convert */
1990 if (sbt
== VT_FLOAT
)
1991 vtop
->c
.ld
= vtop
->c
.f
;
1992 else if (sbt
== VT_DOUBLE
)
1993 vtop
->c
.ld
= vtop
->c
.d
;
1996 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
1997 if (sbt
& VT_UNSIGNED
)
1998 vtop
->c
.ld
= vtop
->c
.ull
;
2000 vtop
->c
.ld
= vtop
->c
.ll
;
2002 if (sbt
& VT_UNSIGNED
)
2003 vtop
->c
.ld
= vtop
->c
.ui
;
2005 vtop
->c
.ld
= vtop
->c
.i
;
2008 if (dbt
== VT_FLOAT
)
2009 vtop
->c
.f
= (float)vtop
->c
.ld
;
2010 else if (dbt
== VT_DOUBLE
)
2011 vtop
->c
.d
= (double)vtop
->c
.ld
;
2012 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
2013 vtop
->c
.ull
= (unsigned long long)vtop
->c
.ld
;
2014 } else if (sf
&& dbt
== VT_BOOL
) {
2015 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
2018 vtop
->c
.ll
= (long long)vtop
->c
.ld
;
2019 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
2020 vtop
->c
.ll
= vtop
->c
.ull
;
2021 else if (sbt
& VT_UNSIGNED
)
2022 vtop
->c
.ll
= vtop
->c
.ui
;
2023 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2024 else if (sbt
== VT_PTR
)
2027 else if (sbt
!= VT_LLONG
)
2028 vtop
->c
.ll
= vtop
->c
.i
;
2030 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
2031 vtop
->c
.ull
= vtop
->c
.ll
;
2032 else if (dbt
== VT_BOOL
)
2033 vtop
->c
.i
= (vtop
->c
.ll
!= 0);
2034 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2035 else if (dbt
== VT_PTR
)
2038 else if (dbt
!= VT_LLONG
) {
2040 if ((dbt
& VT_BTYPE
) == VT_BYTE
)
2042 else if ((dbt
& VT_BTYPE
) == VT_SHORT
)
2044 if(dbt
& VT_UNSIGNED
)
2045 vtop
->c
.ui
= ((unsigned int)vtop
->c
.ll
<< s
) >> s
;
2047 vtop
->c
.i
= ((int)vtop
->c
.ll
<< s
) >> s
;
2050 } else if (p
&& dbt
== VT_BOOL
) {
2053 } else if (!nocode_wanted
) {
2054 /* non constant case: generate code */
2056 /* convert from fp to fp */
2059 /* convert int to fp */
2062 /* convert fp to int */
2063 if (dbt
== VT_BOOL
) {
2067 /* we handle char/short/etc... with generic code */
2068 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
2069 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
2073 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
2074 /* additional cast for char/short... */
2079 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
2080 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
2081 if ((sbt
& VT_BTYPE
) != VT_LLONG
&& !nocode_wanted
) {
2082 /* scalar to long long */
2083 /* machine independent conversion */
2085 /* generate high word */
2086 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
2090 if (sbt
== VT_PTR
) {
2091 /* cast from pointer to int before we apply
2092 shift operation, which pointers don't support*/
2093 gen_cast(&int_type
);
2099 /* patch second register */
2100 vtop
[-1].r2
= vtop
->r
;
2104 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
2105 (dbt
& VT_BTYPE
) == VT_PTR
||
2106 (dbt
& VT_BTYPE
) == VT_FUNC
) {
2107 if ((sbt
& VT_BTYPE
) != VT_LLONG
&&
2108 (sbt
& VT_BTYPE
) != VT_PTR
&&
2109 (sbt
& VT_BTYPE
) != VT_FUNC
&& !nocode_wanted
) {
2110 /* need to convert from 32bit to 64bit */
2112 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
2113 #if defined(TCC_TARGET_ARM64)
2115 #elif defined(TCC_TARGET_X86_64)
2117 /* x86_64 specific: movslq */
2119 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
2126 } else if (dbt
== VT_BOOL
) {
2127 /* scalar to bool */
2130 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
2131 (dbt
& VT_BTYPE
) == VT_SHORT
) {
2132 if (sbt
== VT_PTR
) {
2133 vtop
->type
.t
= VT_INT
;
2134 tcc_warning("nonportable conversion from pointer to char/short");
2136 force_charshort_cast(dbt
);
2137 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
2139 if (sbt
== VT_LLONG
&& !nocode_wanted
) {
2140 /* from long long: just take low order word */
2144 /* if lvalue and single word type, nothing to do because
2145 the lvalue already contains the real type size (see
2146 VT_LVAL_xxx constants) */
2149 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
2150 /* if we are casting between pointer types,
2151 we must update the VT_LVAL_xxx size */
2152 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
2153 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
2158 /* return type size as known at compile time. Put alignment at 'a' */
2159 ST_FUNC
int type_size(CType
*type
, int *a
)
2164 bt
= type
->t
& VT_BTYPE
;
2165 if (bt
== VT_STRUCT
) {
2170 } else if (bt
== VT_PTR
) {
2171 if (type
->t
& VT_ARRAY
) {
2175 ts
= type_size(&s
->type
, a
);
2177 if (ts
< 0 && s
->c
< 0)
2185 } else if (bt
== VT_LDOUBLE
) {
2187 return LDOUBLE_SIZE
;
2188 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
2189 #ifdef TCC_TARGET_I386
2190 #ifdef TCC_TARGET_PE
2195 #elif defined(TCC_TARGET_ARM)
2205 } else if (bt
== VT_INT
|| bt
== VT_ENUM
|| bt
== VT_FLOAT
) {
2208 } else if (bt
== VT_SHORT
) {
2211 } else if (bt
== VT_QLONG
|| bt
== VT_QFLOAT
) {
2215 /* char, void, function, _Bool */
2221 /* push type size as known at runtime time on top of value stack. Put
2223 ST_FUNC
void vla_runtime_type_size(CType
*type
, int *a
)
2225 if (type
->t
& VT_VLA
) {
2226 vset(&int_type
, VT_LOCAL
|VT_LVAL
, type
->ref
->c
);
2228 vpushi(type_size(type
, a
));
2232 static void vla_sp_restore(void) {
2233 if (vlas_in_scope
) {
2234 gen_vla_sp_restore(vla_sp_loc
);
2238 static void vla_sp_restore_root(void) {
2239 if (vlas_in_scope
) {
2240 gen_vla_sp_restore(vla_sp_root_loc
);
2244 /* return the pointed type of t */
2245 static inline CType
*pointed_type(CType
*type
)
2247 return &type
->ref
->type
;
2250 /* modify type so that its it is a pointer to type. */
2251 ST_FUNC
void mk_pointer(CType
*type
)
2254 s
= sym_push(SYM_FIELD
, type
, 0, -1);
2255 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
2259 /* compare function types. OLD functions match any new functions */
2260 static int is_compatible_func(CType
*type1
, CType
*type2
)
2266 if (!is_compatible_types(&s1
->type
, &s2
->type
))
2268 /* check func_call */
2269 if (s1
->a
.func_call
!= s2
->a
.func_call
)
2271 /* XXX: not complete */
2272 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
2276 while (s1
!= NULL
) {
2279 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
2289 /* return true if type1 and type2 are the same. If unqualified is
2290 true, qualifiers on the types are ignored.
2292 - enums are not checked as gcc __builtin_types_compatible_p ()
2294 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2298 t1
= type1
->t
& VT_TYPE
;
2299 t2
= type2
->t
& VT_TYPE
;
2301 /* strip qualifiers before comparing */
2302 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2303 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2305 /* Default Vs explicit signedness only matters for char */
2306 if ((t1
& VT_BTYPE
) != VT_BYTE
) {
2310 /* XXX: bitfields ? */
2313 /* test more complicated cases */
2314 bt1
= t1
& VT_BTYPE
;
2315 if (bt1
== VT_PTR
) {
2316 type1
= pointed_type(type1
);
2317 type2
= pointed_type(type2
);
2318 return is_compatible_types(type1
, type2
);
2319 } else if (bt1
== VT_STRUCT
) {
2320 return (type1
->ref
== type2
->ref
);
2321 } else if (bt1
== VT_FUNC
) {
2322 return is_compatible_func(type1
, type2
);
2328 /* return true if type1 and type2 are exactly the same (including
2331 static int is_compatible_types(CType
*type1
, CType
*type2
)
2333 return compare_types(type1
,type2
,0);
2336 /* return true if type1 and type2 are the same (ignoring qualifiers).
2338 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
2340 return compare_types(type1
,type2
,1);
2343 /* print a type. If 'varstr' is not NULL, then the variable is also
2344 printed in the type */
2346 /* XXX: add array and function pointers */
2347 static void type_to_str(char *buf
, int buf_size
,
2348 CType
*type
, const char *varstr
)
2355 t
= type
->t
& VT_TYPE
;
2358 if (t
& VT_CONSTANT
)
2359 pstrcat(buf
, buf_size
, "const ");
2360 if (t
& VT_VOLATILE
)
2361 pstrcat(buf
, buf_size
, "volatile ");
2362 if ((t
& (VT_DEFSIGN
| VT_UNSIGNED
)) == (VT_DEFSIGN
| VT_UNSIGNED
))
2363 pstrcat(buf
, buf_size
, "unsigned ");
2364 else if (t
& VT_DEFSIGN
)
2365 pstrcat(buf
, buf_size
, "signed ");
2395 tstr
= "long double";
2397 pstrcat(buf
, buf_size
, tstr
);
2401 if (bt
== VT_STRUCT
)
2405 pstrcat(buf
, buf_size
, tstr
);
2406 v
= type
->ref
->v
& ~SYM_STRUCT
;
2407 if (v
>= SYM_FIRST_ANOM
)
2408 pstrcat(buf
, buf_size
, "<anonymous>");
2410 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
2414 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
2415 pstrcat(buf
, buf_size
, "(");
2417 while (sa
!= NULL
) {
2418 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
2419 pstrcat(buf
, buf_size
, buf1
);
2422 pstrcat(buf
, buf_size
, ", ");
2424 pstrcat(buf
, buf_size
, ")");
2428 pstrcpy(buf1
, sizeof(buf1
), "*");
2430 pstrcat(buf1
, sizeof(buf1
), varstr
);
2431 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2435 pstrcat(buf
, buf_size
, " ");
2436 pstrcat(buf
, buf_size
, varstr
);
2441 /* verify type compatibility to store vtop in 'dt' type, and generate
2443 static void gen_assign_cast(CType
*dt
)
2445 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
2446 char buf1
[256], buf2
[256];
2449 st
= &vtop
->type
; /* source type */
2450 dbt
= dt
->t
& VT_BTYPE
;
2451 sbt
= st
->t
& VT_BTYPE
;
2452 if (sbt
== VT_VOID
|| dbt
== VT_VOID
) {
2453 if (sbt
== VT_VOID
&& dbt
== VT_VOID
)
2455 It is Ok if both are void
2461 gcc accepts this program
2464 tcc_error("cannot cast from/to void");
2466 if (dt
->t
& VT_CONSTANT
)
2467 tcc_warning("assignment of read-only location");
2470 /* special cases for pointers */
2471 /* '0' can also be a pointer */
2472 if (is_null_pointer(vtop
))
2474 /* accept implicit pointer to integer cast with warning */
2475 if (is_integer_btype(sbt
)) {
2476 tcc_warning("assignment makes pointer from integer without a cast");
2479 type1
= pointed_type(dt
);
2480 /* a function is implicitely a function pointer */
2481 if (sbt
== VT_FUNC
) {
2482 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
2483 !is_compatible_types(pointed_type(dt
), st
))
2484 tcc_warning("assignment from incompatible pointer type");
2489 type2
= pointed_type(st
);
2490 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2491 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
2492 /* void * can match anything */
2494 /* exact type match, except for unsigned */
2497 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
|
2499 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
|
2501 if (!is_compatible_types(&tmp_type1
, &tmp_type2
))
2502 tcc_warning("assignment from incompatible pointer type");
2504 /* check const and volatile */
2505 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
2506 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
2507 tcc_warning("assignment discards qualifiers from pointer target type");
2513 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
2514 tcc_warning("assignment makes integer from pointer without a cast");
2516 /* XXX: more tests */
2521 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2522 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2523 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2525 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
2526 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
2527 tcc_error("cannot cast '%s' to '%s'", buf1
, buf2
);
2535 /* store vtop in lvalue pushed on stack */
2536 ST_FUNC
void vstore(void)
2538 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
2540 ft
= vtop
[-1].type
.t
;
2541 sbt
= vtop
->type
.t
& VT_BTYPE
;
2542 dbt
= ft
& VT_BTYPE
;
2543 if ((((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2544 (sbt
== VT_INT
&& dbt
== VT_SHORT
))
2545 && !(vtop
->type
.t
& VT_BITFIELD
)) {
2546 /* optimize char/short casts */
2547 delayed_cast
= VT_MUSTCAST
;
2548 vtop
->type
.t
= (ft
& VT_TYPE
& ~VT_BITFIELD
&
2549 ((1 << VT_STRUCT_SHIFT
) - 1));
2550 /* XXX: factorize */
2551 if (ft
& VT_CONSTANT
)
2552 tcc_warning("assignment of read-only location");
2555 if (!(ft
& VT_BITFIELD
))
2556 gen_assign_cast(&vtop
[-1].type
);
2559 if (sbt
== VT_STRUCT
) {
2560 /* if structure, only generate pointer */
2561 /* structure assignment : generate memcpy */
2562 /* XXX: optimize if small size */
2563 if (!nocode_wanted
) {
2564 size
= type_size(&vtop
->type
, &align
);
2568 vtop
->type
.t
= VT_PTR
;
2571 /* address of memcpy() */
2574 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2575 else if(!(align
& 3))
2576 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2579 /* Use memmove, rather than memcpy, as dest and src may be same: */
2580 vpush_global_sym(&func_old_type
, TOK_memmove
);
2585 vtop
->type
.t
= VT_PTR
;
2594 /* leave source on stack */
2595 } else if (ft
& VT_BITFIELD
) {
2596 /* bitfield store handling */
2598 /* save lvalue as expression result (example: s.b = s.a = n;) */
2599 vdup(), vtop
[-1] = vtop
[-2];
2601 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2602 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2603 /* remove bit field info to avoid loops */
2604 vtop
[-1].type
.t
= ft
& ~VT_BITFIELD
& ((1 << VT_STRUCT_SHIFT
) - 1);
2606 if((ft
& VT_BTYPE
) == VT_BOOL
) {
2607 gen_cast(&vtop
[-1].type
);
2608 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2611 /* duplicate destination */
2613 vtop
[-1] = vtop
[-2];
2615 /* mask and shift source */
2616 if((ft
& VT_BTYPE
) != VT_BOOL
) {
2617 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2618 vpushll((1ULL << bit_size
) - 1ULL);
2620 vpushi((1 << bit_size
) - 1);
2626 /* load destination, mask and or with source */
2628 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2629 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2631 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2637 /* ... and discard */
2641 if (!nocode_wanted
) {
2642 #ifdef CONFIG_TCC_BCHECK
2643 /* bound check case */
2644 if (vtop
[-1].r
& VT_MUSTBOUND
) {
2653 #ifdef TCC_TARGET_X86_64
2654 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
2656 } else if ((ft
& VT_BTYPE
) == VT_QFLOAT
) {
2661 r
= gv(rc
); /* generate value */
2662 /* if lvalue was saved on stack, must read it */
2663 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
2665 t
= get_reg(RC_INT
);
2666 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2671 sv
.r
= VT_LOCAL
| VT_LVAL
;
2672 sv
.c
.ul
= vtop
[-1].c
.ul
;
2674 vtop
[-1].r
= t
| VT_LVAL
;
2676 /* two word case handling : store second register at word + 4 (or +8 for x86-64) */
2677 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2678 if (((ft
& VT_BTYPE
) == VT_QLONG
) || ((ft
& VT_BTYPE
) == VT_QFLOAT
)) {
2679 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
2681 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
2682 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
2684 vtop
[-1].type
.t
= load_type
;
2687 /* convert to int to increment easily */
2688 vtop
->type
.t
= addr_type
;
2694 vtop
[-1].type
.t
= load_type
;
2695 /* XXX: it works because r2 is spilled last ! */
2696 store(vtop
->r2
, vtop
- 1);
2702 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
2703 vtop
->r
|= delayed_cast
;
2707 /* post defines POST/PRE add. c is the token ++ or -- */
2708 ST_FUNC
void inc(int post
, int c
)
2711 vdup(); /* save lvalue */
2714 gv_dup(); /* duplicate value */
2716 vdup(); /* duplicate value */
2721 vpushi(c
- TOK_MID
);
2723 vstore(); /* store value */
2725 vpop(); /* if post op, return saved value */
2728 /* Parse GNUC __attribute__ extension. Currently, the following
2729 extensions are recognized:
2730 - aligned(n) : set data/function alignment.
2731 - packed : force data alignment to 1
2732 - section(x) : generate data/code in this section.
2733 - unused : currently ignored, but may be used someday.
2734 - regparm(n) : pass function parameters in registers (i386 only)
2736 static void parse_attribute(AttributeDef
*ad
)
2740 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
2744 while (tok
!= ')') {
2745 if (tok
< TOK_IDENT
)
2746 expect("attribute name");
2754 expect("section name");
2755 ad
->section
= find_section(tcc_state
, (char *)tokc
.cstr
->data
);
2763 expect("alias(\"target\")");
2764 ad
->alias_target
= /* save string as token, for later */
2765 tok_alloc((char*)tokc
.cstr
->data
, tokc
.cstr
->size
-1)->tok
;
2769 case TOK_VISIBILITY1
:
2770 case TOK_VISIBILITY2
:
2773 expect("visibility(\"default|hidden|internal|protected\")");
2774 if (!strcmp (tokc
.cstr
->data
, "default"))
2775 ad
->a
.visibility
= STV_DEFAULT
;
2776 else if (!strcmp (tokc
.cstr
->data
, "hidden"))
2777 ad
->a
.visibility
= STV_HIDDEN
;
2778 else if (!strcmp (tokc
.cstr
->data
, "internal"))
2779 ad
->a
.visibility
= STV_INTERNAL
;
2780 else if (!strcmp (tokc
.cstr
->data
, "protected"))
2781 ad
->a
.visibility
= STV_PROTECTED
;
2783 expect("visibility(\"default|hidden|internal|protected\")");
2792 if (n
<= 0 || (n
& (n
- 1)) != 0)
2793 tcc_error("alignment must be a positive power of two");
2810 /* currently, no need to handle it because tcc does not
2811 track unused objects */
2815 /* currently, no need to handle it because tcc does not
2816 track unused objects */
2821 ad
->a
.func_call
= FUNC_CDECL
;
2826 ad
->a
.func_call
= FUNC_STDCALL
;
2828 #ifdef TCC_TARGET_I386
2838 ad
->a
.func_call
= FUNC_FASTCALL1
+ n
- 1;
2844 ad
->a
.func_call
= FUNC_FASTCALLW
;
2851 ad
->a
.mode
= VT_LLONG
+ 1;
2854 ad
->a
.mode
= VT_SHORT
+ 1;
2857 ad
->a
.mode
= VT_INT
+ 1;
2860 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
2867 ad
->a
.func_export
= 1;
2870 ad
->a
.func_import
= 1;
2873 if (tcc_state
->warn_unsupported
)
2874 tcc_warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
2875 /* skip parameters */
2877 int parenthesis
= 0;
2881 else if (tok
== ')')
2884 } while (parenthesis
&& tok
!= -1);
2897 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2898 static void struct_decl(CType
*type
, int u
, int tdef
)
2900 int a
, v
, size
, align
, maxalign
, c
, offset
, flexible
;
2901 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
, prevbt
;
2902 Sym
*s
, *ss
, *ass
, **ps
;
2906 a
= tok
; /* save decl type */
2911 /* struct already defined ? return it */
2913 expect("struct/union/enum name");
2917 tcc_error("invalid type");
2919 } else if (tok
>= TOK_IDENT
&& !tdef
)
2920 tcc_error("unknown struct/union/enum");
2926 /* we put an undefined size for struct/union */
2927 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
2928 s
->r
= 0; /* default alignment is zero as gcc */
2929 /* put struct/union/enum name in type */
2937 tcc_error("struct/union/enum already defined");
2938 /* cannot be empty */
2940 /* non empty enums are not allowed */
2941 if (a
== TOK_ENUM
) {
2945 expect("identifier");
2947 if (ss
&& !local_stack
)
2948 tcc_error("redefinition of enumerator '%s'",
2949 get_tok_str(v
, NULL
));
2955 /* enum symbols have static storage */
2956 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
2957 ss
->type
.t
|= VT_STATIC
;
2962 /* NOTE: we accept a trailing comma */
2966 s
->c
= type_size(&int_type
, &align
);
2975 while (tok
!= '}') {
2976 parse_btype(&btype
, &ad
);
2979 tcc_error("flexible array member '%s' not at the end of struct",
2980 get_tok_str(v
, NULL
));
2985 type_decl(&type1
, &ad
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
2987 if ((type1
.t
& VT_BTYPE
) != VT_STRUCT
)
2988 expect("identifier");
2990 int v
= btype
.ref
->v
;
2991 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
2992 if (tcc_state
->ms_extensions
== 0)
2993 expect("identifier");
2997 if (type_size(&type1
, &align
) < 0) {
2998 if ((a
== TOK_STRUCT
) && (type1
.t
& VT_ARRAY
) && c
)
3001 tcc_error("field '%s' has incomplete type",
3002 get_tok_str(v
, NULL
));
3004 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
3005 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
3006 tcc_error("invalid type for '%s'",
3007 get_tok_str(v
, NULL
));
3011 bit_size
= expr_const();
3012 /* XXX: handle v = 0 case for messages */
3014 tcc_error("negative width in bit-field '%s'",
3015 get_tok_str(v
, NULL
));
3016 if (v
&& bit_size
== 0)
3017 tcc_error("zero width for bit-field '%s'",
3018 get_tok_str(v
, NULL
));
3020 size
= type_size(&type1
, &align
);
3022 if (align
< ad
.a
.aligned
)
3023 align
= ad
.a
.aligned
;
3024 } else if (ad
.a
.packed
) {
3026 } else if (*tcc_state
->pack_stack_ptr
) {
3027 if (align
> *tcc_state
->pack_stack_ptr
)
3028 align
= *tcc_state
->pack_stack_ptr
;
3031 if (bit_size
>= 0) {
3032 bt
= type1
.t
& VT_BTYPE
;
3039 tcc_error("bitfields must have scalar type");
3041 if (bit_size
> bsize
) {
3042 tcc_error("width of '%s' exceeds its type",
3043 get_tok_str(v
, NULL
));
3044 } else if (bit_size
== bsize
) {
3045 /* no need for bit fields */
3047 } else if (bit_size
== 0) {
3048 /* XXX: what to do if only padding in a
3050 /* zero size: means to pad */
3053 /* we do not have enough room ?
3054 did the type change?
3056 if ((bit_pos
+ bit_size
) > bsize
||
3057 bt
!= prevbt
|| a
== TOK_UNION
)
3060 /* XXX: handle LSB first */
3061 type1
.t
|= VT_BITFIELD
|
3062 (bit_pos
<< VT_STRUCT_SHIFT
) |
3063 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
3064 bit_pos
+= bit_size
;
3070 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
3071 /* add new memory data only if starting
3073 if (lbit_pos
== 0) {
3074 if (a
== TOK_STRUCT
) {
3075 c
= (c
+ align
- 1) & -align
;
3084 if (align
> maxalign
)
3088 printf("add field %s offset=%d",
3089 get_tok_str(v
, NULL
), offset
);
3090 if (type1
.t
& VT_BITFIELD
) {
3091 printf(" pos=%d size=%d",
3092 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
3093 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
3098 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
3100 while ((ass
= ass
->next
) != NULL
) {
3101 ss
= sym_push(ass
->v
, &ass
->type
, 0, offset
+ ass
->c
);
3106 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
3110 if (tok
== ';' || tok
== TOK_EOF
)
3117 /* store size and alignment */
3118 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
3124 /* return 1 if basic type is a type size (short, long, long long) */
3125 ST_FUNC
int is_btype_size(int bt
)
3127 return bt
== VT_SHORT
|| bt
== VT_LONG
|| bt
== VT_LLONG
;
3130 /* return 0 if no type declaration. otherwise, return the basic type
3133 static int parse_btype(CType
*type
, AttributeDef
*ad
)
3135 int t
, u
, bt_size
, complete
, type_found
, typespec_found
;
3139 memset(ad
, 0, sizeof(AttributeDef
));
3147 /* currently, we really ignore extension */
3158 tcc_error("too many basic types");
3160 bt_size
= is_btype_size (u
& VT_BTYPE
);
3161 if (u
== VT_INT
|| (!bt_size
&& !(t
& VT_TYPEDEF
)))
3176 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
3177 #ifndef TCC_TARGET_PE
3178 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3180 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
3181 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3187 #ifdef TCC_TARGET_ARM64
3189 /* GCC's __uint128_t appears in some Linux header files. Make it a
3190 synonym for long double to get the size and alignment right. */
3202 if ((t
& VT_BTYPE
) == VT_LONG
) {
3203 #ifdef TCC_TARGET_PE
3204 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
3206 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3214 struct_decl(&type1
, VT_ENUM
, t
& (VT_TYPEDEF
| VT_EXTERN
));
3217 type
->ref
= type1
.ref
;
3221 struct_decl(&type1
, VT_STRUCT
, t
& (VT_TYPEDEF
| VT_EXTERN
));
3224 /* type modifiers */
3240 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == (VT_DEFSIGN
|VT_UNSIGNED
))
3241 tcc_error("signed and unsigned modifier");
3254 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == VT_DEFSIGN
)
3255 tcc_error("signed and unsigned modifier");
3256 t
|= VT_DEFSIGN
| VT_UNSIGNED
;
3281 /* GNUC attribute */
3282 case TOK_ATTRIBUTE1
:
3283 case TOK_ATTRIBUTE2
:
3284 parse_attribute(ad
);
3287 t
= (t
& ~VT_BTYPE
) | u
;
3295 parse_expr_type(&type1
);
3296 /* remove all storage modifiers except typedef */
3297 type1
.t
&= ~(VT_STORAGE
&~VT_TYPEDEF
);
3303 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
3305 t
|= (s
->type
.t
& ~VT_TYPEDEF
);
3306 type
->ref
= s
->type
.ref
;
3308 /* get attributes from typedef */
3309 if (0 == ad
->a
.aligned
)
3310 ad
->a
.aligned
= s
->a
.aligned
;
3311 if (0 == ad
->a
.func_call
)
3312 ad
->a
.func_call
= s
->a
.func_call
;
3313 ad
->a
.packed
|= s
->a
.packed
;
3322 if (tcc_state
->char_is_unsigned
) {
3323 if ((t
& (VT_DEFSIGN
|VT_BTYPE
)) == VT_BYTE
)
3327 /* long is never used as type */
3328 if ((t
& VT_BTYPE
) == VT_LONG
)
3329 #if (!defined TCC_TARGET_X86_64 && !defined TCC_TARGET_ARM64) || \
3330 defined TCC_TARGET_PE
3331 t
= (t
& ~VT_BTYPE
) | VT_INT
;
3333 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3339 /* convert a function parameter type (array to pointer and function to
3340 function pointer) */
3341 static inline void convert_parameter_type(CType
*pt
)
3343 /* remove const and volatile qualifiers (XXX: const could be used
3344 to indicate a const function parameter */
3345 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3346 /* array must be transformed to pointer according to ANSI C */
3348 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
3353 ST_FUNC
void parse_asm_str(CString
*astr
)
3356 /* read the string */
3358 expect("string constant");
3360 while (tok
== TOK_STR
) {
3361 /* XXX: add \0 handling too ? */
3362 cstr_cat(astr
, tokc
.cstr
->data
);
3365 cstr_ccat(astr
, '\0');
3368 /* Parse an asm label and return the label
3369 * Don't forget to free the CString in the caller! */
3370 static void asm_label_instr(CString
*astr
)
3373 parse_asm_str(astr
);
3376 printf("asm_alias: \"%s\"\n", (char *)astr
->data
);
3380 static void post_type(CType
*type
, AttributeDef
*ad
)
3382 int n
, l
, t1
, arg_size
, align
;
3383 Sym
**plast
, *s
, *first
;
3388 /* function declaration */
3396 /* read param name and compute offset */
3397 if (l
!= FUNC_OLD
) {
3398 if (!parse_btype(&pt
, &ad1
)) {
3400 tcc_error("invalid type");
3407 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
3409 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3410 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
3411 tcc_error("parameter declared as void");
3412 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
3417 expect("identifier");
3421 convert_parameter_type(&pt
);
3422 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
3428 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
3435 /* if no parameters, then old type prototype */
3439 /* NOTE: const is ignored in returned type as it has a special
3440 meaning in gcc / C++ */
3441 type
->t
&= ~VT_CONSTANT
;
3442 /* some ancient pre-K&R C allows a function to return an array
3443 and the array brackets to be put after the arguments, such
3444 that "int c()[]" means something like "int[] c()" */
3447 skip(']'); /* only handle simple "[]" */
3450 /* we push a anonymous symbol which will contain the function prototype */
3451 ad
->a
.func_args
= arg_size
;
3452 s
= sym_push(SYM_FIELD
, type
, 0, l
);
3457 } else if (tok
== '[') {
3458 /* array definition */
3460 if (tok
== TOK_RESTRICT1
)
3465 if (!local_stack
|| nocode_wanted
)
3466 vpushi(expr_const());
3468 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3471 tcc_error("invalid array size");
3473 if (!is_integer_btype(vtop
->type
.t
& VT_BTYPE
))
3474 tcc_error("size of variable length array should be an integer");
3479 /* parse next post type */
3480 post_type(type
, ad
);
3481 if (type
->t
== VT_FUNC
)
3482 tcc_error("declaration of an array of functions");
3483 t1
|= type
->t
& VT_VLA
;
3486 loc
-= type_size(&int_type
, &align
);
3490 vla_runtime_type_size(type
, &align
);
3492 vset(&int_type
, VT_LOCAL
|VT_LVAL
, n
);
3499 /* we push an anonymous symbol which will contain the array
3501 s
= sym_push(SYM_FIELD
, type
, 0, n
);
3502 type
->t
= (t1
? VT_VLA
: VT_ARRAY
) | VT_PTR
;
3507 /* Parse a type declaration (except basic type), and return the type
3508 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3509 expected. 'type' should contain the basic type. 'ad' is the
3510 attribute definition of the basic type. It can be modified by
3513 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
3516 CType type1
, *type2
;
3517 int qualifiers
, storage
;
3519 while (tok
== '*') {
3527 qualifiers
|= VT_CONSTANT
;
3532 qualifiers
|= VT_VOLATILE
;
3540 type
->t
|= qualifiers
;
3543 /* XXX: clarify attribute handling */
3544 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3545 parse_attribute(ad
);
3547 /* recursive type */
3548 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3549 type1
.t
= 0; /* XXX: same as int */
3552 /* XXX: this is not correct to modify 'ad' at this point, but
3553 the syntax is not clear */
3554 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3555 parse_attribute(ad
);
3556 type_decl(&type1
, ad
, v
, td
);
3559 /* type identifier */
3560 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
3564 if (!(td
& TYPE_ABSTRACT
))
3565 expect("identifier");
3569 storage
= type
->t
& VT_STORAGE
;
3570 type
->t
&= ~VT_STORAGE
;
3571 if (storage
& VT_STATIC
) {
3572 int saved_nocode_wanted
= nocode_wanted
;
3574 post_type(type
, ad
);
3575 nocode_wanted
= saved_nocode_wanted
;
3577 post_type(type
, ad
);
3579 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3580 parse_attribute(ad
);
3584 /* append type at the end of type1 */
3597 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3598 ST_FUNC
int lvalue_type(int t
)
3603 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
3605 else if (bt
== VT_SHORT
)
3609 if (t
& VT_UNSIGNED
)
3610 r
|= VT_LVAL_UNSIGNED
;
3614 /* indirection with full error checking and bound check */
3615 ST_FUNC
void indir(void)
3617 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
3618 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
3622 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
3624 vtop
->type
= *pointed_type(&vtop
->type
);
3625 /* Arrays and functions are never lvalues */
3626 if (!(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_VLA
)
3627 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3628 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3629 /* if bound checking, the referenced pointer must be checked */
3630 #ifdef CONFIG_TCC_BCHECK
3631 if (tcc_state
->do_bounds_check
)
3632 vtop
->r
|= VT_MUSTBOUND
;
3637 /* pass a parameter to a function and do type checking and casting */
3638 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
3643 func_type
= func
->c
;
3644 if (func_type
== FUNC_OLD
||
3645 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
3646 /* default casting : only need to convert float to double */
3647 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
3650 } else if (vtop
->type
.t
& VT_BITFIELD
) {
3651 type
.t
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
3654 } else if (arg
== NULL
) {
3655 tcc_error("too many arguments to function");
3658 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
3659 gen_assign_cast(&type
);
3663 /* parse an expression of the form '(type)' or '(expr)' and return its
3665 static void parse_expr_type(CType
*type
)
3671 if (parse_btype(type
, &ad
)) {
3672 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3679 static void parse_type(CType
*type
)
3684 if (!parse_btype(type
, &ad
)) {
3687 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3690 static void vpush_tokc(int t
)
3695 vsetc(&type
, VT_CONST
, &tokc
);
3698 ST_FUNC
void unary(void)
3700 int n
, t
, align
, size
, r
, sizeof_caller
;
3704 static int in_sizeof
= 0;
3706 sizeof_caller
= in_sizeof
;
3708 /* XXX: GCC 2.95.3 does not generate a table although it should be
3722 vpush_tokc(VT_INT
| VT_UNSIGNED
);
3726 vpush_tokc(VT_LLONG
);
3730 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
3734 vpush_tokc(VT_FLOAT
);
3738 vpush_tokc(VT_DOUBLE
);
3742 vpush_tokc(VT_LDOUBLE
);
3745 case TOK___FUNCTION__
:
3747 goto tok_identifier
;
3753 /* special function name identifier */
3754 len
= strlen(funcname
) + 1;
3755 /* generate char[len] type */
3760 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
3761 ptr
= section_ptr_add(data_section
, len
);
3762 memcpy(ptr
, funcname
, len
);
3767 #ifdef TCC_TARGET_PE
3768 t
= VT_SHORT
| VT_UNSIGNED
;
3774 /* string parsing */
3777 if (tcc_state
->warn_write_strings
)
3782 memset(&ad
, 0, sizeof(AttributeDef
));
3783 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, NULL
, 0);
3788 if (parse_btype(&type
, &ad
)) {
3789 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
3791 /* check ISOC99 compound literal */
3793 /* data is allocated locally by default */
3798 /* all except arrays are lvalues */
3799 if (!(type
.t
& VT_ARRAY
))
3800 r
|= lvalue_type(type
.t
);
3801 memset(&ad
, 0, sizeof(AttributeDef
));
3802 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, NULL
, 0);
3804 if (sizeof_caller
) {
3811 } else if (tok
== '{') {
3814 tcc_error("statement expression in global scope"); */
3815 /* this check breaks compilation of the linux 2.4.26 with the meesage:
3816 linux/include/net/tcp.h:945: error: statement expression in global scope */
3818 /* save all registers */
3820 /* statement expression : we do not accept break/continue
3821 inside as GCC does */
3822 block(NULL
, NULL
, NULL
, NULL
, 0, 1);
3837 /* functions names must be treated as function pointers,
3838 except for unary '&' and sizeof. Since we consider that
3839 functions are not lvalues, we only have to handle it
3840 there and in function calls. */
3841 /* arrays can also be used although they are not lvalues */
3842 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
3843 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
3845 mk_pointer(&vtop
->type
);
3851 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3853 boolean
.t
= VT_BOOL
;
3855 vtop
->c
.i
= !vtop
->c
.i
;
3856 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
3857 vtop
->c
.i
= vtop
->c
.i
^ 1;
3858 else if (!nocode_wanted
) {
3860 vseti(VT_JMP
, gvtst(1, 0));
3874 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
3875 tcc_error("pointer not accepted for unary plus");
3876 /* In order to force cast, we add zero, except for floating point
3877 where we really need an noop (otherwise -0.0 will be transformed
3879 if (!is_float(vtop
->type
.t
)) {
3890 unary_type(&type
); // Perform a in_sizeof = 0;
3891 size
= type_size(&type
, &align
);
3892 if (t
== TOK_SIZEOF
) {
3893 if (!(type
.t
& VT_VLA
)) {
3895 tcc_error("sizeof applied to an incomplete type");
3898 vla_runtime_type_size(&type
, &align
);
3903 vtop
->type
.t
|= VT_UNSIGNED
;
3906 case TOK_builtin_types_compatible_p
:
3915 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3916 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3917 vpushi(is_compatible_types(&type1
, &type2
));
3920 case TOK_builtin_constant_p
:
3922 int saved_nocode_wanted
, res
;
3925 saved_nocode_wanted
= nocode_wanted
;
3928 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
3930 nocode_wanted
= saved_nocode_wanted
;
3935 case TOK_builtin_frame_address
:
3936 case TOK_builtin_return_address
:
3943 if (tok
!= TOK_CINT
|| tokc
.i
< 0) {
3944 tcc_error("%s only takes positive integers",
3945 tok1
== TOK_builtin_return_address
?
3946 "__builtin_return_address" :
3947 "__builtin_frame_address");
3954 vset(&type
, VT_LOCAL
, 0); /* local frame */
3956 mk_pointer(&vtop
->type
);
3957 indir(); /* -> parent frame */
3959 if (tok1
== TOK_builtin_return_address
) {
3960 // assume return address is just above frame pointer on stack
3963 mk_pointer(&vtop
->type
);
3968 #ifdef TCC_TARGET_X86_64
3969 #ifdef TCC_TARGET_PE
3970 case TOK_builtin_va_start
:
3978 if ((vtop
->r
& VT_VALMASK
) != VT_LOCAL
)
3979 tcc_error("__builtin_va_start expects a local variable");
3980 vtop
->r
&= ~(VT_LVAL
| VT_REF
);
3981 vtop
->type
= char_pointer_type
;
3986 case TOK_builtin_va_arg_types
:
3993 vpushi(classify_x86_64_va_arg(&type
));
3999 #ifdef TCC_TARGET_ARM64
4000 case TOK___va_start
: {
4002 tcc_error("statement in global scope");
4012 vtop
->type
.t
= VT_VOID
;
4015 case TOK___va_arg
: {
4018 tcc_error("statement in global scope");
4030 case TOK___arm64_clear_cache
: {
4039 vtop
->type
.t
= VT_VOID
;
4043 /* pre operations */
4054 t
= vtop
->type
.t
& VT_BTYPE
;
4056 /* In IEEE negate(x) isn't subtract(0,x), but rather
4061 else if (t
== VT_DOUBLE
)
4072 goto tok_identifier
;
4074 /* allow to take the address of a label */
4075 if (tok
< TOK_UIDENT
)
4076 expect("label identifier");
4077 s
= label_find(tok
);
4079 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4081 if (s
->r
== LABEL_DECLARED
)
4082 s
->r
= LABEL_FORWARD
;
4085 s
->type
.t
= VT_VOID
;
4086 mk_pointer(&s
->type
);
4087 s
->type
.t
|= VT_STATIC
;
4089 vpushsym(&s
->type
, s
);
4093 // special qnan , snan and infinity values
4095 vpush64(VT_DOUBLE
, 0x7ff8000000000000ULL
);
4099 vpush64(VT_DOUBLE
, 0x7ff0000000000001ULL
);
4103 vpush64(VT_DOUBLE
, 0x7ff0000000000000ULL
);
4112 expect("identifier");
4115 const char *name
= get_tok_str(t
, NULL
);
4117 tcc_error("'%s' undeclared", name
);
4118 /* for simple function calls, we tolerate undeclared
4119 external reference to int() function */
4120 if (tcc_state
->warn_implicit_function_declaration
4121 #ifdef TCC_TARGET_PE
4122 /* people must be warned about using undeclared WINAPI functions
4123 (which usually start with uppercase letter) */
4124 || (name
[0] >= 'A' && name
[0] <= 'Z')
4127 tcc_warning("implicit declaration of function '%s'", name
);
4128 s
= external_global_sym(t
, &func_old_type
, 0);
4130 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
4131 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
4132 /* if referencing an inline function, then we generate a
4133 symbol to it if not already done. It will have the
4134 effect to generate code for it at the end of the
4135 compilation unit. Inline function as always
4136 generated in the text section. */
4138 put_extern_sym(s
, text_section
, 0, 0);
4139 r
= VT_SYM
| VT_CONST
;
4143 vset(&s
->type
, r
, s
->c
);
4144 /* if forward reference, we must point to s */
4145 if (vtop
->r
& VT_SYM
) {
4147 vtop
->c
.ptr_offset
= 0;
4152 /* post operations */
4154 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
4157 } else if (tok
== '.' || tok
== TOK_ARROW
|| tok
== TOK_CDOUBLE
) {
4160 if (tok
== TOK_ARROW
)
4162 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
4165 /* expect pointer on structure */
4166 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
4167 expect("struct or union");
4168 if (tok
== TOK_CDOUBLE
)
4169 expect("field name");
4171 if (tok
== TOK_CINT
|| tok
== TOK_CUINT
)
4172 expect("field name");
4176 while ((s
= s
->next
) != NULL
) {
4181 tcc_error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, NULL
));
4182 /* add field offset to pointer */
4183 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
4186 /* change type to field type, and set to lvalue */
4187 vtop
->type
= s
->type
;
4188 vtop
->type
.t
|= qualifiers
;
4189 /* an array is never an lvalue */
4190 if (!(vtop
->type
.t
& VT_ARRAY
)) {
4191 vtop
->r
|= lvalue_type(vtop
->type
.t
);
4192 #ifdef CONFIG_TCC_BCHECK
4193 /* if bound checking, the referenced pointer must be checked */
4194 if (tcc_state
->do_bounds_check
)
4195 vtop
->r
|= VT_MUSTBOUND
;
4199 } else if (tok
== '[') {
4205 } else if (tok
== '(') {
4208 int nb_args
, ret_nregs
, ret_align
, regsize
, variadic
;
4211 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
4212 /* pointer test (no array accepted) */
4213 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
4214 vtop
->type
= *pointed_type(&vtop
->type
);
4215 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
4219 expect("function pointer");
4222 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
4224 /* get return type */
4227 sa
= s
->next
; /* first parameter */
4230 /* compute first implicit argument if a structure is returned */
4231 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
4232 variadic
= (s
->c
== FUNC_ELLIPSIS
);
4233 ret_nregs
= gfunc_sret(&s
->type
, variadic
, &ret
.type
,
4234 &ret_align
, ®size
);
4236 /* get some space for the returned structure */
4237 size
= type_size(&s
->type
, &align
);
4238 #ifdef TCC_TARGET_ARM64
4239 /* On arm64, a small struct is return in registers.
4240 It is much easier to write it to memory if we know
4241 that we are allowed to write some extra bytes, so
4242 round the allocated space up to a power of 2: */
4244 while (size
& (size
- 1))
4245 size
= (size
| (size
- 1)) + 1;
4247 loc
= (loc
- size
) & -align
;
4249 ret
.r
= VT_LOCAL
| VT_LVAL
;
4250 /* pass it as 'int' to avoid structure arg passing
4252 vseti(VT_LOCAL
, loc
);
4262 /* return in register */
4263 if (is_float(ret
.type
.t
)) {
4264 ret
.r
= reg_fret(ret
.type
.t
);
4265 #ifdef TCC_TARGET_X86_64
4266 if ((ret
.type
.t
& VT_BTYPE
) == VT_QFLOAT
)
4270 #ifndef TCC_TARGET_ARM64
4271 #ifdef TCC_TARGET_X86_64
4272 if ((ret
.type
.t
& VT_BTYPE
) == VT_QLONG
)
4274 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
4285 gfunc_param_typed(s
, sa
);
4295 tcc_error("too few arguments to function");
4297 if (!nocode_wanted
) {
4298 gfunc_call(nb_args
);
4300 vtop
-= (nb_args
+ 1);
4304 for (r
= ret
.r
+ ret_nregs
+ !ret_nregs
; r
-- > ret
.r
;) {
4305 vsetc(&ret
.type
, r
, &ret
.c
);
4306 vtop
->r2
= ret
.r2
; /* Loop only happens when r2 is VT_CONST */
4309 /* handle packed struct return */
4310 if (((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) && ret_nregs
) {
4313 size
= type_size(&s
->type
, &align
);
4314 /* We're writing whole regs often, make sure there's enough
4315 space. Assume register size is power of 2. */
4316 if (regsize
> align
)
4318 loc
= (loc
- size
) & -align
;
4322 vset(&ret
.type
, VT_LOCAL
| VT_LVAL
, addr
+ offset
);
4326 if (--ret_nregs
== 0)
4330 vset(&s
->type
, VT_LOCAL
| VT_LVAL
, addr
);
4338 ST_FUNC
void expr_prod(void)
4343 while (tok
== '*' || tok
== '/' || tok
== '%') {
4351 ST_FUNC
void expr_sum(void)
4356 while (tok
== '+' || tok
== '-') {
4364 static void expr_shift(void)
4369 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
4377 static void expr_cmp(void)
4382 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
4383 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
4391 static void expr_cmpeq(void)
4396 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
4404 static void expr_and(void)
4407 while (tok
== '&') {
4414 static void expr_xor(void)
4417 while (tok
== '^') {
4424 static void expr_or(void)
4427 while (tok
== '|') {
4434 /* XXX: fix this mess */
4435 static void expr_land_const(void)
4438 while (tok
== TOK_LAND
) {
4445 /* XXX: fix this mess */
4446 static void expr_lor_const(void)
4449 while (tok
== TOK_LOR
) {
4456 /* only used if non constant */
4457 static void expr_land(void)
4462 if (tok
== TOK_LAND
) {
4467 if (tok
!= TOK_LAND
) {
4477 static void expr_lor(void)
4482 if (tok
== TOK_LOR
) {
4487 if (tok
!= TOK_LOR
) {
4497 /* XXX: better constant handling */
4498 static void expr_cond(void)
4500 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
;
4502 CType type
, type1
, type2
;
4509 boolean
.t
= VT_BOOL
;
4515 if (tok
!= ':' || !gnu_ext
) {
4530 if (vtop
!= vstack
) {
4531 /* needed to avoid having different registers saved in
4533 if (is_float(vtop
->type
.t
)) {
4535 #ifdef TCC_TARGET_X86_64
4536 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4546 if (tok
== ':' && gnu_ext
) {
4554 sv
= *vtop
; /* save value to handle it later */
4555 vtop
--; /* no vpop so that FP stack is not flushed */
4563 bt1
= t1
& VT_BTYPE
;
4565 bt2
= t2
& VT_BTYPE
;
4566 /* cast operands to correct type according to ISOC rules */
4567 if (is_float(bt1
) || is_float(bt2
)) {
4568 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
4569 type
.t
= VT_LDOUBLE
;
4570 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
4575 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
4576 /* cast to biggest op */
4578 /* convert to unsigned if it does not fit in a long long */
4579 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
4580 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
4581 type
.t
|= VT_UNSIGNED
;
4582 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
4583 /* If one is a null ptr constant the result type
4585 if (is_null_pointer (vtop
))
4587 else if (is_null_pointer (&sv
))
4589 /* XXX: test pointer compatibility, C99 has more elaborate
4593 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
4594 /* XXX: test function pointer compatibility */
4595 type
= bt1
== VT_FUNC
? type1
: type2
;
4596 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
4597 /* XXX: test structure compatibility */
4598 type
= bt1
== VT_STRUCT
? type1
: type2
;
4599 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
4600 /* NOTE: as an extension, we accept void on only one side */
4603 /* integer operations */
4605 /* convert to unsigned if it does not fit in an integer */
4606 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
4607 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
4608 type
.t
|= VT_UNSIGNED
;
4611 /* now we convert second operand */
4613 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4616 if (is_float(type
.t
)) {
4618 #ifdef TCC_TARGET_X86_64
4619 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4623 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
4624 /* for long longs, we use fixed registers to avoid having
4625 to handle a complicated move */
4630 /* this is horrible, but we must also convert first
4634 /* put again first value and cast it */
4637 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4640 move_reg(r2
, r1
, type
.t
);
4647 static void expr_eq(void)
4653 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
4654 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
4655 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
4670 ST_FUNC
void gexpr(void)
4681 /* parse an expression and return its type without any side effect. */
4682 static void expr_type(CType
*type
)
4684 int saved_nocode_wanted
;
4686 saved_nocode_wanted
= nocode_wanted
;
4691 nocode_wanted
= saved_nocode_wanted
;
4694 /* parse a unary expression and return its type without any side
4696 static void unary_type(CType
*type
)
4708 /* parse a constant expression and return value in vtop. */
4709 static void expr_const1(void)
4718 /* parse an integer constant and return its value. */
4719 ST_FUNC
int expr_const(void)
4723 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
4724 expect("constant expression");
4730 /* return the label token if current token is a label, otherwise
4732 static int is_label(void)
4736 /* fast test first */
4737 if (tok
< TOK_UIDENT
)
4739 /* no need to save tokc because tok is an identifier */
4746 unget_tok(last_tok
);
4751 static void label_or_decl(int l
)
4755 /* fast test first */
4756 if (tok
>= TOK_UIDENT
)
4758 /* no need to save tokc because tok is an identifier */
4762 unget_tok(last_tok
);
4765 unget_tok(last_tok
);
4770 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
4771 int case_reg
, int is_expr
)
4774 Sym
*s
, *frame_bottom
;
4776 /* generate line number info */
4777 if (tcc_state
->do_debug
&&
4778 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
4779 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
4781 last_line_num
= file
->line_num
;
4785 /* default return value is (void) */
4787 vtop
->type
.t
= VT_VOID
;
4790 if (tok
== TOK_IF
) {
4797 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4799 if (c
== TOK_ELSE
) {
4803 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4804 gsym(d
); /* patch else jmp */
4807 } else if (tok
== TOK_WHILE
) {
4816 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4820 } else if (tok
== '{') {
4822 int block_vla_sp_loc
= vla_sp_loc
, saved_vlas_in_scope
= vlas_in_scope
;
4825 /* record local declaration stack position */
4827 frame_bottom
= sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4828 frame_bottom
->next
= scope_stack_bottom
;
4829 scope_stack_bottom
= frame_bottom
;
4830 llabel
= local_label_stack
;
4832 /* handle local labels declarations */
4833 if (tok
== TOK_LABEL
) {
4836 if (tok
< TOK_UIDENT
)
4837 expect("label identifier");
4838 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
4848 while (tok
!= '}') {
4849 label_or_decl(VT_LOCAL
);
4853 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4856 /* pop locally defined labels */
4857 label_pop(&local_label_stack
, llabel
);
4859 /* XXX: this solution makes only valgrind happy...
4860 triggered by gcc.c-torture/execute/20000917-1.c */
4862 switch(vtop
->type
.t
& VT_BTYPE
) {
4864 /* this breaks a compilation of the linux kernel v2.4.26 */
4865 /* pmd_t *new = ({ __asm__ __volatile__("ud2\n") ; ((pmd_t *)1); }); */
4866 /* Look a commit a80acab: Display error on statement expressions with complex return type */
4867 /* A pointer is not a complex return type */
4871 for(p
=vtop
->type
.ref
;p
;p
=p
->prev
)
4873 tcc_error("unsupported expression type");
4876 /* pop locally defined symbols */
4877 scope_stack_bottom
= scope_stack_bottom
->next
;
4878 sym_pop(&local_stack
, s
);
4880 /* Pop VLA frames and restore stack pointer if required */
4881 if (vlas_in_scope
> saved_vlas_in_scope
) {
4882 vla_sp_loc
= saved_vlas_in_scope
? block_vla_sp_loc
: vla_sp_root_loc
;
4885 vlas_in_scope
= saved_vlas_in_scope
;
4888 } else if (tok
== TOK_RETURN
) {
4892 gen_assign_cast(&func_vt
);
4893 #ifdef TCC_TARGET_ARM64
4894 // Perhaps it would be better to use this for all backends:
4897 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
4898 CType type
, ret_type
;
4899 int ret_align
, ret_nregs
, regsize
;
4900 ret_nregs
= gfunc_sret(&func_vt
, func_var
, &ret_type
,
4901 &ret_align
, ®size
);
4902 if (0 == ret_nregs
) {
4903 /* if returning structure, must copy it to implicit
4904 first pointer arg location */
4907 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
4910 /* copy structure value to pointer */
4913 /* returning structure packed into registers */
4914 int r
, size
, addr
, align
;
4915 size
= type_size(&func_vt
,&align
);
4916 if ((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) || (vtop
->c
.i
& (ret_align
-1)))
4917 && (align
& (ret_align
-1))) {
4918 loc
= (loc
- size
) & -ret_align
;
4921 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
4925 vset(&ret_type
, VT_LOCAL
| VT_LVAL
, addr
);
4927 vtop
->type
= ret_type
;
4928 if (is_float(ret_type
.t
))
4929 r
= rc_fret(ret_type
.t
);
4935 if (--ret_nregs
== 0)
4937 /* We assume that when a structure is returned in multiple
4938 registers, their classes are consecutive values of the
4941 vtop
->c
.i
+= regsize
;
4942 vtop
->r
= VT_LOCAL
| VT_LVAL
;
4945 } else if (is_float(func_vt
.t
)) {
4946 gv(rc_fret(func_vt
.t
));
4951 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
4954 rsym
= gjmp(rsym
); /* jmp */
4955 } else if (tok
== TOK_BREAK
) {
4958 tcc_error("cannot break");
4959 *bsym
= gjmp(*bsym
);
4962 } else if (tok
== TOK_CONTINUE
) {
4965 tcc_error("cannot continue");
4966 vla_sp_restore_root();
4967 *csym
= gjmp(*csym
);
4970 } else if (tok
== TOK_FOR
) {
4975 frame_bottom
= sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4976 frame_bottom
->next
= scope_stack_bottom
;
4977 scope_stack_bottom
= frame_bottom
;
4979 /* c99 for-loop init decl? */
4980 if (!decl0(VT_LOCAL
, 1)) {
4981 /* no, regular for-loop init expr */
5007 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
5011 scope_stack_bottom
= scope_stack_bottom
->next
;
5012 sym_pop(&local_stack
, s
);
5014 if (tok
== TOK_DO
) {
5020 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
5031 if (tok
== TOK_SWITCH
) {
5035 /* XXX: other types than integer */
5036 case_reg
= gv(RC_INT
);
5040 b
= gjmp(0); /* jump to first case */
5042 block(&a
, csym
, &b
, &c
, case_reg
, 0);
5043 /* if no default, jmp after switch */
5051 if (tok
== TOK_CASE
) {
5058 if (gnu_ext
&& tok
== TOK_DOTS
) {
5062 tcc_warning("empty case range");
5064 /* since a case is like a label, we must skip it with a jmp */
5072 *case_sym
= gtst(1, 0);
5075 *case_sym
= gtst(1, 0);
5079 *case_sym
= gtst(1, *case_sym
);
5081 case_reg
= gv(RC_INT
);
5086 goto block_after_label
;
5088 if (tok
== TOK_DEFAULT
) {
5094 tcc_error("too many 'default'");
5097 goto block_after_label
;
5099 if (tok
== TOK_GOTO
) {
5101 if (tok
== '*' && gnu_ext
) {
5105 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
5108 } else if (tok
>= TOK_UIDENT
) {
5109 s
= label_find(tok
);
5110 /* put forward definition if needed */
5112 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
5114 if (s
->r
== LABEL_DECLARED
)
5115 s
->r
= LABEL_FORWARD
;
5117 vla_sp_restore_root();
5118 if (s
->r
& LABEL_FORWARD
)
5119 s
->jnext
= gjmp(s
->jnext
);
5121 gjmp_addr(s
->jnext
);
5124 expect("label identifier");
5127 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
5135 if (s
->r
== LABEL_DEFINED
)
5136 tcc_error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
5138 s
->r
= LABEL_DEFINED
;
5140 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
5144 /* we accept this, but it is a mistake */
5147 tcc_warning("deprecated use of label at end of compound statement");
5151 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
5154 /* expression case */
5169 /* t is the array or struct type. c is the array or struct
5170 address. cur_index/cur_field is the pointer to the current
5171 value. 'size_only' is true if only size info is needed (only used
5173 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
5174 int *cur_index
, Sym
**cur_field
,
5178 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
5184 if (gnu_ext
&& (l
= is_label()) != 0)
5186 while (tok
== '[' || tok
== '.') {
5188 if (!(type
->t
& VT_ARRAY
))
5189 expect("array type");
5192 index
= expr_const();
5193 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
5194 expect("invalid index");
5195 if (tok
== TOK_DOTS
&& gnu_ext
) {
5197 index_last
= expr_const();
5198 if (index_last
< 0 ||
5199 (s
->c
>= 0 && index_last
>= s
->c
) ||
5201 expect("invalid index");
5207 *cur_index
= index_last
;
5208 type
= pointed_type(type
);
5209 elem_size
= type_size(type
, &align
);
5210 c
+= index
* elem_size
;
5211 /* NOTE: we only support ranges for last designator */
5212 nb_elems
= index_last
- index
+ 1;
5213 if (nb_elems
!= 1) {
5222 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
5223 expect("struct/union type");
5236 /* XXX: fix this mess by using explicit storage field */
5238 type1
.t
|= (type
->t
& ~VT_TYPE
);
5252 if (type
->t
& VT_ARRAY
) {
5254 type
= pointed_type(type
);
5255 c
+= index
* type_size(type
, &align
);
5259 tcc_error("too many field init");
5260 /* XXX: fix this mess by using explicit storage field */
5262 type1
.t
|= (type
->t
& ~VT_TYPE
);
5267 decl_initializer(type
, sec
, c
, 0, size_only
);
5269 /* XXX: make it more general */
5270 if (!size_only
&& nb_elems
> 1) {
5271 unsigned long c_end
;
5276 tcc_error("range init not supported yet for dynamic storage");
5277 c_end
= c
+ nb_elems
* elem_size
;
5278 if (c_end
> sec
->data_allocated
)
5279 section_realloc(sec
, c_end
);
5280 src
= sec
->data
+ c
;
5282 for(i
= 1; i
< nb_elems
; i
++) {
5284 memcpy(dst
, src
, elem_size
);
5290 #define EXPR_CONST 1
5293 /* store a value or an expression directly in global data or in local array */
5294 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
5295 int v
, int expr_type
)
5297 int saved_global_expr
, bt
, bit_pos
, bit_size
;
5299 unsigned long long bit_mask
;
5307 /* compound literals must be allocated globally in this case */
5308 saved_global_expr
= global_expr
;
5311 global_expr
= saved_global_expr
;
5312 /* NOTE: symbols are accepted */
5313 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
5314 tcc_error("initializer element is not constant");
5322 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
5325 /* XXX: not portable */
5326 /* XXX: generate error if incorrect relocation */
5327 gen_assign_cast(&dtype
);
5328 bt
= type
->t
& VT_BTYPE
;
5329 /* we'll write at most 16 bytes */
5330 if (c
+ 16 > sec
->data_allocated
) {
5331 section_realloc(sec
, c
+ 16);
5333 ptr
= sec
->data
+ c
;
5334 /* XXX: make code faster ? */
5335 if (!(type
->t
& VT_BITFIELD
)) {
5340 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5341 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
5342 bit_mask
= (1LL << bit_size
) - 1;
5344 if ((vtop
->r
& VT_SYM
) &&
5350 (bt
== VT_INT
&& bit_size
!= 32)))
5351 tcc_error("initializer element is not computable at load time");
5353 /* XXX: when cross-compiling we assume that each type has the
5354 same representation on host and target, which is likely to
5355 be wrong in the case of long double */
5357 vtop
->c
.i
= (vtop
->c
.i
!= 0);
5359 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5362 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5365 *(double *)ptr
= vtop
->c
.d
;
5368 *(long double *)ptr
= vtop
->c
.ld
;
5371 *(long long *)ptr
|= (vtop
->c
.ll
& bit_mask
) << bit_pos
;
5374 addr_t val
= (vtop
->c
.ptr_offset
& bit_mask
) << bit_pos
;
5375 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
5376 if (vtop
->r
& VT_SYM
)
5377 greloca(sec
, vtop
->sym
, c
, R_DATA_PTR
, val
);
5379 *(addr_t
*)ptr
|= val
;
5381 if (vtop
->r
& VT_SYM
)
5382 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
5383 *(addr_t
*)ptr
|= val
;
5388 int val
= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5389 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
5390 if (vtop
->r
& VT_SYM
)
5391 greloca(sec
, vtop
->sym
, c
, R_DATA_PTR
, val
);
5395 if (vtop
->r
& VT_SYM
)
5396 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
5404 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
5411 /* put zeros for variable based init */
5412 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
5415 /* nothing to do because globals are already set to zero */
5417 vpush_global_sym(&func_old_type
, TOK_memset
);
5419 #ifdef TCC_TARGET_ARM
5430 /* 't' contains the type and storage info. 'c' is the offset of the
5431 object in section 'sec'. If 'sec' is NULL, it means stack based
5432 allocation. 'first' is true if array '{' must be read (multi
5433 dimension implicit array init handling). 'size_only' is true if
5434 size only evaluation is wanted (only for arrays). */
5435 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
5436 int first
, int size_only
)
5438 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, parlevel1
, i
;
5439 int size1
, align1
, expr_type
;
5443 if (type
->t
& VT_VLA
) {
5446 /* save current stack pointer */
5447 if (vlas_in_scope
== 0) {
5448 if (vla_sp_root_loc
== -1)
5449 vla_sp_root_loc
= (loc
-= PTR_SIZE
);
5450 gen_vla_sp_save(vla_sp_root_loc
);
5453 vla_runtime_type_size(type
, &a
);
5454 gen_vla_alloc(type
, a
);
5458 } else if (type
->t
& VT_ARRAY
) {
5462 t1
= pointed_type(type
);
5463 size1
= type_size(t1
, &align1
);
5466 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
5469 tcc_error("character array initializer must be a literal,"
5470 " optionally enclosed in braces");
5475 /* only parse strings here if correct type (otherwise: handle
5476 them as ((w)char *) expressions */
5477 if ((tok
== TOK_LSTR
&&
5478 #ifdef TCC_TARGET_PE
5479 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
5481 (t1
->t
& VT_BTYPE
) == VT_INT
5483 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
5484 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5489 /* compute maximum number of chars wanted */
5491 cstr_len
= cstr
->size
;
5493 cstr_len
= cstr
->size
/ sizeof(nwchar_t
);
5496 if (n
>= 0 && nb
> (n
- array_length
))
5497 nb
= n
- array_length
;
5500 tcc_warning("initializer-string for array is too long");
5501 /* in order to go faster for common case (char
5502 string in global variable, we handle it
5504 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
5505 memcpy(sec
->data
+ c
+ array_length
, cstr
->data
, nb
);
5509 ch
= ((unsigned char *)cstr
->data
)[i
];
5511 ch
= ((nwchar_t
*)cstr
->data
)[i
];
5512 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
5520 /* only add trailing zero if enough storage (no
5521 warning in this case since it is standard) */
5522 if (n
< 0 || array_length
< n
) {
5524 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
5530 while (tok
!= '}') {
5531 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
5532 if (n
>= 0 && index
>= n
)
5533 tcc_error("index too large");
5534 /* must put zero in holes (note that doing it that way
5535 ensures that it even works with designators) */
5536 if (!size_only
&& array_length
< index
) {
5537 init_putz(t1
, sec
, c
+ array_length
* size1
,
5538 (index
- array_length
) * size1
);
5541 if (index
> array_length
)
5542 array_length
= index
;
5543 /* special test for multi dimensional arrays (may not
5544 be strictly correct if designators are used at the
5546 if (index
>= n
&& no_oblock
)
5555 /* put zeros at the end */
5556 if (!size_only
&& n
>= 0 && array_length
< n
) {
5557 init_putz(t1
, sec
, c
+ array_length
* size1
,
5558 (n
- array_length
) * size1
);
5560 /* patch type size if needed */
5562 s
->c
= array_length
;
5563 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
5564 (sec
|| !first
|| tok
== '{')) {
5566 /* NOTE: the previous test is a specific case for automatic
5567 struct/union init */
5568 /* XXX: union needs only one init */
5575 if (tcc_state
->old_struct_init_code
) {
5576 /* an old version of struct initialization.
5577 It have a problems. But with a new version
5578 linux 2.4.26 can't load ramdisk.
5580 while (tok
== '(') {
5584 if (!parse_btype(&type1
, &ad1
))
5586 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
5588 if (!is_assignable_types(type
, &type1
))
5589 tcc_error("invalid type for cast");
5596 if (!parse_btype(&type1
, &ad1
))
5598 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
5600 if (!is_assignable_types(type
, &type1
))
5601 tcc_error("invalid type for cast");
5610 if (first
|| tok
== '{') {
5619 while (tok
!= '}') {
5620 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
5622 if (!size_only
&& array_length
< index
) {
5623 init_putz(type
, sec
, c
+ array_length
,
5624 index
- array_length
);
5626 index
= index
+ type_size(&f
->type
, &align1
);
5627 if (index
> array_length
)
5628 array_length
= index
;
5630 /* gr: skip fields from same union - ugly. */
5633 int f_size
= type_size(&f
->type
, &align
);
5634 int f_type
= (f
->type
.t
& VT_BTYPE
);
5636 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
5637 /* test for same offset */
5638 if (f
->next
->c
!= f
->c
)
5640 if ((f_type
== VT_STRUCT
) && (f_size
== 0)) {
5642 Lets assume a structure of size 0 can't be a member of the union.
5643 This allow to compile the following code from a linux kernel v2.4.26
5644 typedef struct { } rwlock_t;
5650 struct fs_struct init_fs = { { (1) }, (rwlock_t) {}, 0022, };
5651 tcc-0.9.23 can succesfully compile this version of the kernel.
5652 gcc don't have problems with this code too.
5656 /* if yes, test for bitfield shift */
5657 if ((f
->type
.t
& VT_BITFIELD
) && (f
->next
->type
.t
& VT_BITFIELD
)) {
5658 int bit_pos_1
= (f
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5659 int bit_pos_2
= (f
->next
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5660 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
5661 if (bit_pos_1
!= bit_pos_2
)
5668 if (no_oblock
&& f
== NULL
)
5674 /* put zeros at the end */
5675 if (!size_only
&& array_length
< n
) {
5676 init_putz(type
, sec
, c
+ array_length
,
5685 } else if (tok
== '{') {
5687 decl_initializer(type
, sec
, c
, first
, size_only
);
5689 } else if (size_only
) {
5690 /* just skip expression */
5691 parlevel
= parlevel1
= 0;
5692 while ((parlevel
> 0 || parlevel1
> 0 ||
5693 (tok
!= '}' && tok
!= ',')) && tok
!= -1) {
5696 else if (tok
== ')') {
5697 if (parlevel
== 0 && parlevel1
== 0)
5701 else if (tok
== '{')
5703 else if (tok
== '}') {
5704 if (parlevel
== 0 && parlevel1
== 0)
5711 /* currently, we always use constant expression for globals
5712 (may change for scripting case) */
5713 expr_type
= EXPR_CONST
;
5715 expr_type
= EXPR_ANY
;
5716 init_putv(type
, sec
, c
, 0, expr_type
);
5720 /* parse an initializer for type 't' if 'has_init' is non zero, and
5721 allocate space in local or global data space ('r' is either
5722 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5723 variable 'v' with an associated name represented by 'asm_label' of
5724 scope 'scope' is declared before initializers are parsed. If 'v' is
5725 zero, then a reference to the new object is put in the value stack.
5726 If 'has_init' is 2, a special parsing is done to handle string
5728 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
5729 int has_init
, int v
, char *asm_label
,
5732 int size
, align
, addr
, data_offset
;
5734 ParseState saved_parse_state
= {0};
5735 TokenString init_str
;
5737 Sym
*flexible_array
;
5739 flexible_array
= NULL
;
5740 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
5741 Sym
*field
= type
->ref
->next
;
5744 field
= field
->next
;
5745 if (field
->type
.t
& VT_ARRAY
&& field
->type
.ref
->c
< 0)
5746 flexible_array
= field
;
5750 size
= type_size(type
, &align
);
5751 /* If unknown size, we must evaluate it before
5752 evaluating initializers because
5753 initializers can generate global data too
5754 (e.g. string pointers or ISOC99 compound
5755 literals). It also simplifies local
5756 initializers handling */
5757 tok_str_new(&init_str
);
5758 if (size
< 0 || (flexible_array
&& has_init
)) {
5760 tcc_error("unknown type size");
5761 /* get all init string */
5762 if (has_init
== 2) {
5763 /* only get strings */
5764 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5765 tok_str_add_tok(&init_str
);
5770 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
5772 tcc_error("unexpected end of file in initializer");
5773 tok_str_add_tok(&init_str
);
5776 else if (tok
== '}') {
5786 tok_str_add(&init_str
, -1);
5787 tok_str_add(&init_str
, 0);
5790 save_parse_state(&saved_parse_state
);
5792 begin_macro(&init_str
, 0);
5794 decl_initializer(type
, NULL
, 0, 1, 1);
5795 /* prepare second initializer parsing */
5796 macro_ptr
= init_str
.str
;
5799 /* if still unknown size, error */
5800 size
= type_size(type
, &align
);
5802 tcc_error("unknown type size");
5805 size
+= flexible_array
->type
.ref
->c
* pointed_size(&flexible_array
->type
);
5806 /* take into account specified alignment if bigger */
5807 if (ad
->a
.aligned
) {
5808 if (ad
->a
.aligned
> align
)
5809 align
= ad
->a
.aligned
;
5810 } else if (ad
->a
.packed
) {
5813 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
5815 #ifdef CONFIG_TCC_BCHECK
5816 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5820 loc
= (loc
- size
) & -align
;
5822 #ifdef CONFIG_TCC_BCHECK
5823 /* handles bounds */
5824 /* XXX: currently, since we do only one pass, we cannot track
5825 '&' operators, so we add only arrays */
5826 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5828 /* add padding between regions */
5830 /* then add local bound info */
5831 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(addr_t
));
5832 bounds_ptr
[0] = addr
;
5833 bounds_ptr
[1] = size
;
5837 /* local variable */
5838 sym_push(v
, type
, r
, addr
);
5840 /* push local reference */
5841 vset(type
, r
, addr
);
5847 if (v
&& scope
== VT_CONST
) {
5848 /* see if the symbol was already defined */
5851 if (!is_compatible_types(&sym
->type
, type
))
5852 tcc_error("incompatible types for redefinition of '%s'",
5853 get_tok_str(v
, NULL
));
5854 if (sym
->type
.t
& VT_EXTERN
) {
5855 /* if the variable is extern, it was not allocated */
5856 sym
->type
.t
&= ~VT_EXTERN
;
5857 /* set array size if it was omitted in extern
5859 if ((sym
->type
.t
& VT_ARRAY
) &&
5860 sym
->type
.ref
->c
< 0 &&
5862 sym
->type
.ref
->c
= type
->ref
->c
;
5864 /* we accept several definitions of the same
5865 global variable. this is tricky, because we
5866 must play with the SHN_COMMON type of the symbol */
5867 /* XXX: should check if the variable was already
5868 initialized. It is incorrect to initialized it
5870 /* no init data, we won't add more to the symbol */
5877 /* allocate symbol in corresponding section */
5882 else if (tcc_state
->nocommon
)
5886 data_offset
= sec
->data_offset
;
5887 data_offset
= (data_offset
+ align
- 1) & -align
;
5889 /* very important to increment global pointer at this time
5890 because initializers themselves can create new initializers */
5891 data_offset
+= size
;
5892 #ifdef CONFIG_TCC_BCHECK
5893 /* add padding if bound check */
5894 if (tcc_state
->do_bounds_check
)
5897 sec
->data_offset
= data_offset
;
5898 /* allocate section space to put the data */
5899 if (sec
->sh_type
!= SHT_NOBITS
&&
5900 data_offset
> sec
->data_allocated
)
5901 section_realloc(sec
, data_offset
);
5902 /* align section if needed */
5903 if (align
> sec
->sh_addralign
)
5904 sec
->sh_addralign
= align
;
5906 addr
= 0; /* avoid warning */
5910 if (scope
!= VT_CONST
|| !sym
) {
5911 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
5912 sym
->asm_label
= asm_label
? tcc_strdup(asm_label
) : 0;
5914 /* update symbol definition */
5916 put_extern_sym(sym
, sec
, addr
, size
);
5919 /* put a common area */
5920 put_extern_sym(sym
, NULL
, align
, size
);
5921 /* XXX: find a nicer way */
5922 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
5923 esym
->st_shndx
= SHN_COMMON
;
5926 /* push global reference */
5927 sym
= get_sym_ref(type
, sec
, addr
, size
);
5928 vpushsym(type
, sym
);
5930 /* patch symbol weakness */
5931 if (type
->t
& VT_WEAK
)
5933 apply_visibility(sym
, type
);
5934 #ifdef CONFIG_TCC_BCHECK
5935 /* handles bounds now because the symbol must be defined
5936 before for the relocation */
5937 if (tcc_state
->do_bounds_check
) {
5940 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
5941 /* then add global bound info */
5942 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(addr_t
));
5943 bounds_ptr
[0] = 0; /* relocated */
5944 bounds_ptr
[1] = size
;
5948 if (has_init
|| (type
->t
& VT_VLA
)) {
5949 decl_initializer(type
, sec
, addr
, 1, 0);
5950 /* restore parse state if needed */
5953 restore_parse_state(&saved_parse_state
);
5955 /* patch flexible array member size back to -1, */
5956 /* for possible subsequent similar declarations */
5958 flexible_array
->type
.ref
->c
= -1;
5963 static void put_func_debug(Sym
*sym
)
5968 /* XXX: we put here a dummy type */
5969 snprintf(buf
, sizeof(buf
), "%s:%c1",
5970 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
5971 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
5972 cur_text_section
, sym
->c
);
5973 /* //gr gdb wants a line at the function */
5974 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
5979 /* parse an old style function declaration list */
5980 /* XXX: check multiple parameter */
5981 static void func_decl_list(Sym
*func_sym
)
5988 /* parse each declaration */
5989 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
&&
5990 tok
!= TOK_ASM1
&& tok
!= TOK_ASM2
&& tok
!= TOK_ASM3
) {
5991 if (!parse_btype(&btype
, &ad
))
5992 expect("declaration list");
5993 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5994 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5996 /* we accept no variable after */
6000 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
6001 /* find parameter in function parameter list */
6004 if ((s
->v
& ~SYM_FIELD
) == v
)
6008 tcc_error("declaration for parameter '%s' but no such parameter",
6009 get_tok_str(v
, NULL
));
6011 /* check that no storage specifier except 'register' was given */
6012 if (type
.t
& VT_STORAGE
)
6013 tcc_error("storage class specified for '%s'", get_tok_str(v
, NULL
));
6014 convert_parameter_type(&type
);
6015 /* we can add the type (NOTE: it could be local to the function) */
6017 /* accept other parameters */
6028 /* parse a function defined by symbol 'sym' and generate its code in
6029 'cur_text_section' */
6030 static void gen_function(Sym
*sym
)
6032 int saved_nocode_wanted
= nocode_wanted
;
6035 ind
= cur_text_section
->data_offset
;
6036 /* NOTE: we patch the symbol size later */
6037 put_extern_sym(sym
, cur_text_section
, ind
, 0);
6038 funcname
= get_tok_str(sym
->v
, NULL
);
6040 /* Initialize VLA state */
6042 vla_sp_root_loc
= -1;
6043 /* put debug symbol */
6044 if (tcc_state
->do_debug
)
6045 put_func_debug(sym
);
6046 /* push a dummy symbol to enable local sym storage */
6047 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
6048 gfunc_prolog(&sym
->type
);
6049 #ifdef CONFIG_TCC_BCHECK
6050 if (tcc_state
->do_bounds_check
&& !strcmp(funcname
, "main")) {
6053 for (i
= 0, sym
= local_stack
; i
< 2; i
++, sym
= sym
->prev
) {
6054 if (sym
->v
& SYM_FIELD
|| sym
->prev
->v
& SYM_FIELD
)
6056 vpush_global_sym(&func_old_type
, TOK___bound_main_arg
);
6057 vset(&sym
->type
, sym
->r
, sym
->c
);
6063 block(NULL
, NULL
, NULL
, NULL
, 0, 0);
6066 cur_text_section
->data_offset
= ind
;
6067 label_pop(&global_label_stack
, NULL
);
6068 /* reset local stack */
6069 scope_stack_bottom
= NULL
;
6070 sym_pop(&local_stack
, NULL
);
6071 /* end of function */
6072 /* patch symbol size */
6073 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
6075 /* patch symbol weakness (this definition overrules any prototype) */
6076 if (sym
->type
.t
& VT_WEAK
)
6078 apply_visibility(sym
, &sym
->type
);
6079 if (tcc_state
->do_debug
) {
6080 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
6082 /* It's better to crash than to generate wrong code */
6083 cur_text_section
= NULL
;
6084 funcname
= ""; /* for safety */
6085 func_vt
.t
= VT_VOID
; /* for safety */
6086 func_var
= 0; /* for safety */
6087 ind
= 0; /* for safety */
6088 nocode_wanted
= saved_nocode_wanted
;
6092 ST_FUNC
void gen_inline_functions(void)
6095 int inline_generated
, i
, ln
;
6096 struct InlineFunc
*fn
;
6098 ln
= file
->line_num
;
6099 /* iterate while inline function are referenced */
6101 inline_generated
= 0;
6102 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
6103 fn
= tcc_state
->inline_fns
[i
];
6105 if (sym
&& sym
->c
) {
6106 /* the function was used: generate its code and
6107 convert it to a normal function */
6110 pstrcpy(file
->filename
, sizeof file
->filename
, fn
->filename
);
6111 sym
->r
= VT_SYM
| VT_CONST
;
6112 sym
->type
.t
&= ~VT_INLINE
;
6114 begin_macro(&fn
->func_str
, 0);
6116 cur_text_section
= text_section
;
6120 inline_generated
= 1;
6123 if (!inline_generated
)
6126 file
->line_num
= ln
;
6127 /* free tokens of unused inline functions */
6128 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
6129 fn
= tcc_state
->inline_fns
[i
];
6131 tok_str_free(fn
->func_str
.str
);
6133 dynarray_reset(&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
);
6136 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
6137 static int decl0(int l
, int is_for_loop_init
)
6143 char *asm_label
= 0; // associated asm label
6146 if (!parse_btype(&btype
, &ad
)) {
6147 if (is_for_loop_init
)
6149 /* skip redundant ';' */
6150 /* XXX: find more elegant solution */
6155 if (l
== VT_CONST
&&
6156 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
6157 /* global asm block */
6161 /* special test for old K&R protos without explicit int
6162 type. Only accepted when defining global data */
6163 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
6167 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
6168 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
6170 if ((btype
.t
& VT_BTYPE
) == VT_STRUCT
) {
6171 int v
= btype
.ref
->v
;
6172 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) >= SYM_FIRST_ANOM
)
6173 tcc_warning("unnamed struct/union that defines no instances");
6178 while (1) { /* iterate thru each declaration */
6180 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
6184 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
6185 printf("type = '%s'\n", buf
);
6188 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
6189 if ((type
.t
& VT_STATIC
) && (l
== VT_LOCAL
)) {
6190 tcc_error("function without file scope cannot be static");
6192 /* if old style function prototype, we accept a
6195 if (sym
->c
== FUNC_OLD
)
6196 func_decl_list(sym
);
6199 tcc_free(asm_label
);
6201 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
6204 asm_label_instr(&astr
);
6205 asm_label
= tcc_strdup(astr
.data
);
6208 /* parse one last attribute list, after asm label */
6209 parse_attribute(&ad
);
6214 #ifdef TCC_TARGET_PE
6215 if (ad
.a
.func_import
)
6216 type
.t
|= VT_IMPORT
;
6217 if (ad
.a
.func_export
)
6218 type
.t
|= VT_EXPORT
;
6220 type
.t
|= ad
.a
.visibility
<< VT_VIS_SHIFT
;
6224 tcc_error("cannot use local functions");
6225 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
6226 expect("function definition");
6228 /* reject abstract declarators in function definition */
6230 while ((sym
= sym
->next
) != NULL
)
6231 if (!(sym
->v
& ~SYM_FIELD
))
6232 expect("identifier");
6234 /* XXX: cannot do better now: convert extern line to static inline */
6235 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
6236 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
6241 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
6244 ref
= sym
->type
.ref
;
6245 if (0 == ref
->a
.func_proto
)
6246 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
6248 /* use func_call from prototype if not defined */
6249 if (ref
->a
.func_call
!= FUNC_CDECL
6250 && type
.ref
->a
.func_call
== FUNC_CDECL
)
6251 type
.ref
->a
.func_call
= ref
->a
.func_call
;
6253 /* use export from prototype */
6254 if (ref
->a
.func_export
)
6255 type
.ref
->a
.func_export
= 1;
6257 /* use static from prototype */
6258 if (sym
->type
.t
& VT_STATIC
)
6259 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
6261 /* If the definition has no visibility use the
6262 one from prototype. */
6263 if (! (type
.t
& VT_VIS_MASK
))
6264 type
.t
|= sym
->type
.t
& VT_VIS_MASK
;
6266 if (!is_compatible_types(&sym
->type
, &type
)) {
6268 tcc_error("incompatible types for redefinition of '%s'",
6269 get_tok_str(v
, NULL
));
6271 type
.ref
->a
.func_proto
= 0;
6272 /* if symbol is already defined, then put complete type */
6275 /* put function symbol */
6276 sym
= global_identifier_push(v
, type
.t
, 0);
6277 sym
->type
.ref
= type
.ref
;
6280 /* static inline functions are just recorded as a kind
6281 of macro. Their code will be emitted at the end of
6282 the compilation unit only if they are used */
6283 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
6284 (VT_INLINE
| VT_STATIC
)) {
6286 struct InlineFunc
*fn
;
6287 const char *filename
;
6289 filename
= file
? file
->filename
: "";
6290 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
6291 strcpy(fn
->filename
, filename
);
6293 tok_str_new(&fn
->func_str
);
6299 tcc_error("unexpected end of file");
6300 tok_str_add_tok(&fn
->func_str
);
6305 } else if (t
== '}') {
6307 if (block_level
== 0)
6311 tok_str_add(&fn
->func_str
, -1);
6312 tok_str_add(&fn
->func_str
, 0);
6313 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
6316 /* compute text section */
6317 cur_text_section
= ad
.section
;
6318 if (!cur_text_section
)
6319 cur_text_section
= text_section
;
6320 sym
->r
= VT_SYM
| VT_CONST
;
6325 if (btype
.t
& VT_TYPEDEF
) {
6326 /* save typedefed type */
6327 /* XXX: test storage specifiers ? */
6328 sym
= sym_push(v
, &type
, 0, 0);
6330 sym
->type
.t
|= VT_TYPEDEF
;
6333 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
6334 /* external function definition */
6335 /* specific case for func_call attribute */
6336 ad
.a
.func_proto
= 1;
6338 } else if (!(type
.t
& VT_ARRAY
)) {
6339 /* not lvalue if array */
6340 r
|= lvalue_type(type
.t
);
6342 has_init
= (tok
== '=');
6343 if (has_init
&& (type
.t
& VT_VLA
))
6344 tcc_error("Variable length array cannot be initialized");
6345 if ((btype
.t
& VT_EXTERN
) || ((type
.t
& VT_BTYPE
) == VT_FUNC
) ||
6346 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
6347 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
6348 /* external variable or function */
6349 /* NOTE: as GCC, uninitialized global static
6350 arrays of null size are considered as
6352 sym
= external_sym(v
, &type
, r
, asm_label
);
6354 if (ad
.alias_target
) {
6359 alias_target
= sym_find(ad
.alias_target
);
6360 if (!alias_target
|| !alias_target
->c
)
6361 tcc_error("unsupported forward __alias__ attribute");
6362 esym
= &((Elf32_Sym
*)symtab_section
->data
)[alias_target
->c
];
6363 tsec
.sh_num
= esym
->st_shndx
;
6364 put_extern_sym2(sym
, &tsec
, esym
->st_value
, esym
->st_size
, 0);
6367 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
6368 if (type
.t
& VT_STATIC
)
6374 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, asm_label
, l
);
6378 if (is_for_loop_init
) {
6379 tcc_free(asm_label
);
6390 tcc_free(asm_label
);
6394 ST_FUNC
void decl(int l
)