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
*define_stack
;
54 ST_DATA Sym
*global_label_stack
;
55 ST_DATA Sym
*local_label_stack
;
56 static int local_scope
;
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
, int scope
);
85 static int decl0(int l
, int is_for_loop_init
);
86 static void expr_eq(void);
87 static void expr_lor_const(void);
88 static void unary_type(CType
*type
);
89 static void vla_runtime_type_size(CType
*type
, int *a
);
90 static void vla_sp_restore(void);
91 static void vla_sp_restore_root(void);
92 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
);
93 static void expr_type(CType
*type
);
94 ST_FUNC
void vpush64(int ty
, unsigned long long v
);
95 ST_FUNC
void vpush(CType
*type
);
96 ST_FUNC
int gvtst(int inv
, int t
);
97 ST_FUNC
int is_btype_size(int bt
);
99 ST_INLN
int is_float(int t
)
103 return bt
== VT_LDOUBLE
|| bt
== VT_DOUBLE
|| bt
== VT_FLOAT
|| bt
== VT_QFLOAT
;
106 /* we use our own 'finite' function to avoid potential problems with
107 non standard math libs */
108 /* XXX: endianness dependent */
109 ST_FUNC
int ieee_finite(double d
)
112 memcpy(p
, &d
, sizeof(double));
113 return ((unsigned)((p
[1] | 0x800fffff) + 1)) >> 31;
116 ST_FUNC
void test_lvalue(void)
118 if (!(vtop
->r
& VT_LVAL
))
122 ST_FUNC
void check_vstack(void)
125 tcc_error("internal compiler error: vstack leak (%d)", vtop
- pvtop
);
128 /* ------------------------------------------------------------------------- */
129 /* symbol allocator */
130 static Sym
*__sym_malloc(void)
132 Sym
*sym_pool
, *sym
, *last_sym
;
135 sym_pool
= tcc_malloc(SYM_POOL_NB
* sizeof(Sym
));
136 dynarray_add(&sym_pools
, &nb_sym_pools
, sym_pool
);
138 last_sym
= sym_free_first
;
140 for(i
= 0; i
< SYM_POOL_NB
; i
++) {
141 sym
->next
= last_sym
;
145 sym_free_first
= last_sym
;
149 static inline Sym
*sym_malloc(void)
152 sym
= sym_free_first
;
154 sym
= __sym_malloc();
155 sym_free_first
= sym
->next
;
159 ST_INLN
void sym_free(Sym
*sym
)
161 sym
->next
= sym_free_first
;
162 sym_free_first
= sym
;
165 /* push, without hashing */
166 ST_FUNC Sym
*sym_push2(Sym
**ps
, int v
, int t
, long c
)
186 /* find a symbol and return its associated structure. 's' is the top
187 of the symbol stack */
188 ST_FUNC Sym
*sym_find2(Sym
*s
, int v
)
200 /* structure lookup */
201 ST_INLN Sym
*struct_find(int v
)
204 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
206 return table_ident
[v
]->sym_struct
;
209 /* find an identifier */
210 ST_INLN Sym
*sym_find(int v
)
213 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
215 return table_ident
[v
]->sym_identifier
;
218 /* push a given symbol on the symbol stack */
219 ST_FUNC Sym
*sym_push(int v
, CType
*type
, int r
, int c
)
228 s
= sym_push2(ps
, v
, type
->t
, c
);
229 s
->type
.ref
= type
->ref
;
231 /* don't record fields or anonymous symbols */
233 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
234 /* record symbol in token array */
235 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
237 ps
= &ts
->sym_struct
;
239 ps
= &ts
->sym_identifier
;
242 s
->scope
= local_scope
;
243 if (s
->prev_tok
&& s
->prev_tok
->scope
== s
->scope
)
244 tcc_error("redeclaration of '%s'",
245 get_tok_str(v
& ~SYM_STRUCT
, NULL
));
250 /* push a global identifier */
251 ST_FUNC Sym
*global_identifier_push(int v
, int t
, int c
)
254 s
= sym_push2(&global_stack
, v
, t
, c
);
255 /* don't record anonymous symbol */
256 if (v
< SYM_FIRST_ANOM
) {
257 ps
= &table_ident
[v
- TOK_IDENT
]->sym_identifier
;
258 /* modify the top most local identifier, so that
259 sym_identifier will point to 's' when popped */
261 ps
= &(*ps
)->prev_tok
;
268 /* pop symbols until top reaches 'b' */
269 ST_FUNC
void sym_pop(Sym
**ptop
, Sym
*b
)
279 /* remove symbol in token array */
281 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
282 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
284 ps
= &ts
->sym_struct
;
286 ps
= &ts
->sym_identifier
;
295 static void weaken_symbol(Sym
*sym
)
297 sym
->type
.t
|= VT_WEAK
;
302 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
303 esym_type
= ELFW(ST_TYPE
)(esym
->st_info
);
304 esym
->st_info
= ELFW(ST_INFO
)(STB_WEAK
, esym_type
);
308 static void apply_visibility(Sym
*sym
, CType
*type
)
310 int vis
= sym
->type
.t
& VT_VIS_MASK
;
311 int vis2
= type
->t
& VT_VIS_MASK
;
312 if (vis
== (STV_DEFAULT
<< VT_VIS_SHIFT
))
314 else if (vis2
== (STV_DEFAULT
<< VT_VIS_SHIFT
))
317 vis
= (vis
< vis2
) ? vis
: vis2
;
318 sym
->type
.t
&= ~VT_VIS_MASK
;
324 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
325 vis
>>= VT_VIS_SHIFT
;
326 esym
->st_other
= (esym
->st_other
& ~ELFW(ST_VISIBILITY
)(-1)) | vis
;
330 /* ------------------------------------------------------------------------- */
332 ST_FUNC
void swap(int *p
, int *q
)
340 static void vsetc(CType
*type
, int r
, CValue
*vc
)
344 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
345 tcc_error("memory full (vstack)");
346 /* cannot let cpu flags if other instruction are generated. Also
347 avoid leaving VT_JMP anywhere except on the top of the stack
348 because it would complicate the code generator. */
349 if (vtop
>= vstack
) {
350 v
= vtop
->r
& VT_VALMASK
;
351 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
361 /* push constant of type "type" with useless value */
362 ST_FUNC
void vpush(CType
*type
)
365 vsetc(type
, VT_CONST
, &cval
);
368 /* push integer constant */
369 ST_FUNC
void vpushi(int v
)
373 vsetc(&int_type
, VT_CONST
, &cval
);
376 /* push a pointer sized constant */
377 static void vpushs(addr_t v
)
381 vsetc(&size_type
, VT_CONST
, &cval
);
384 /* push arbitrary 64bit constant */
385 ST_FUNC
void vpush64(int ty
, unsigned long long v
)
392 vsetc(&ctype
, VT_CONST
, &cval
);
395 /* push long long constant */
396 static inline void vpushll(long long v
)
398 vpush64(VT_LLONG
, v
);
401 /* push a symbol value of TYPE */
402 static inline void vpushsym(CType
*type
, Sym
*sym
)
406 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
410 /* Return a static symbol pointing to a section */
411 ST_FUNC Sym
*get_sym_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
417 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
418 sym
->type
.ref
= type
->ref
;
419 sym
->r
= VT_CONST
| VT_SYM
;
420 put_extern_sym(sym
, sec
, offset
, size
);
424 /* push a reference to a section offset by adding a dummy symbol */
425 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
427 vpushsym(type
, get_sym_ref(type
, sec
, offset
, size
));
430 /* define a new external reference to a symbol 'v' of type 'u' */
431 ST_FUNC Sym
*external_global_sym(int v
, CType
*type
, int r
)
437 /* push forward reference */
438 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
439 s
->type
.ref
= type
->ref
;
440 s
->r
= r
| VT_CONST
| VT_SYM
;
445 /* define a new external reference to a symbol 'v' */
446 static Sym
*external_sym(int v
, CType
*type
, int r
)
452 /* push forward reference */
453 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
454 s
->type
.t
|= VT_EXTERN
;
455 } else if (s
->type
.ref
== func_old_type
.ref
) {
456 s
->type
.ref
= type
->ref
;
457 s
->r
= r
| VT_CONST
| VT_SYM
;
458 s
->type
.t
|= VT_EXTERN
;
459 } else if (!is_compatible_types(&s
->type
, type
)) {
460 tcc_error("incompatible types for redefinition of '%s'",
461 get_tok_str(v
, NULL
));
463 /* Merge some storage attributes. */
464 if (type
->t
& VT_WEAK
)
467 if (type
->t
& VT_VIS_MASK
)
468 apply_visibility(s
, type
);
473 /* push a reference to global symbol v */
474 ST_FUNC
void vpush_global_sym(CType
*type
, int v
)
476 vpushsym(type
, external_global_sym(v
, type
, 0));
479 ST_FUNC
void vset(CType
*type
, int r
, int v
)
484 vsetc(type
, r
, &cval
);
487 static void vseti(int r
, int v
)
495 ST_FUNC
void vswap(void)
498 /* cannot let cpu flags if other instruction are generated. Also
499 avoid leaving VT_JMP anywhere except on the top of the stack
500 because it would complicate the code generator. */
501 if (vtop
>= vstack
) {
502 int v
= vtop
->r
& VT_VALMASK
;
503 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
510 /* XXX: +2% overall speed possible with optimized memswap
512 * memswap(&vtop[0], &vtop[1], sizeof *vtop);
516 ST_FUNC
void vpushv(SValue
*v
)
518 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
519 tcc_error("memory full (vstack)");
524 static void vdup(void)
529 /* save r to the memory stack, and mark it as being free */
530 ST_FUNC
void save_reg(int r
)
532 int l
, saved
, size
, align
;
536 /* modify all stack values */
539 for(p
=vstack
;p
<=vtop
;p
++) {
540 if ((p
->r
& VT_VALMASK
) == r
||
541 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
542 /* must save value on stack if not already done */
544 /* NOTE: must reload 'r' because r might be equal to r2 */
545 r
= p
->r
& VT_VALMASK
;
546 /* store register in the stack */
548 if ((p
->r
& VT_LVAL
) ||
549 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
550 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
551 type
= &char_pointer_type
;
555 size
= type_size(type
, &align
);
556 loc
= (loc
- size
) & -align
;
558 sv
.r
= VT_LOCAL
| VT_LVAL
;
561 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
562 /* x86 specific: need to pop fp register ST0 if saved */
564 o(0xd8dd); /* fstp %st(0) */
567 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
568 /* special long long case */
569 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
577 /* mark that stack entry as being saved on the stack */
578 if (p
->r
& VT_LVAL
) {
579 /* also clear the bounded flag because the
580 relocation address of the function was stored in
582 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
584 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
592 #ifdef TCC_TARGET_ARM
593 /* find a register of class 'rc2' with at most one reference on stack.
594 * If none, call get_reg(rc) */
595 ST_FUNC
int get_reg_ex(int rc
, int rc2
)
600 for(r
=0;r
<NB_REGS
;r
++) {
601 if (reg_classes
[r
] & rc2
) {
604 for(p
= vstack
; p
<= vtop
; p
++) {
605 if ((p
->r
& VT_VALMASK
) == r
||
606 (p
->r2
& VT_VALMASK
) == r
)
617 /* find a free register of class 'rc'. If none, save one register */
618 ST_FUNC
int get_reg(int rc
)
623 /* find a free register */
624 for(r
=0;r
<NB_REGS
;r
++) {
625 if (reg_classes
[r
] & rc
) {
626 for(p
=vstack
;p
<=vtop
;p
++) {
627 if ((p
->r
& VT_VALMASK
) == r
||
628 (p
->r2
& VT_VALMASK
) == r
)
636 /* no register left : free the first one on the stack (VERY
637 IMPORTANT to start from the bottom to ensure that we don't
638 spill registers used in gen_opi()) */
639 for(p
=vstack
;p
<=vtop
;p
++) {
640 /* look at second register (if long long) */
641 r
= p
->r2
& VT_VALMASK
;
642 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
644 r
= p
->r
& VT_VALMASK
;
645 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
651 /* Should never comes here */
655 /* save registers up to (vtop - n) stack entry */
656 ST_FUNC
void save_regs(int n
)
661 for(p
= vstack
;p
<= p1
; p
++) {
662 r
= p
->r
& VT_VALMASK
;
669 /* move register 's' (of type 't') to 'r', and flush previous value of r to memory
671 static void move_reg(int r
, int s
, int t
)
685 /* get address of vtop (vtop MUST BE an lvalue) */
686 ST_FUNC
void gaddrof(void)
688 if (vtop
->r
& VT_REF
&& !nocode_wanted
)
691 /* tricky: if saved lvalue, then we can go back to lvalue */
692 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
693 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
698 #ifdef CONFIG_TCC_BCHECK
699 /* generate lvalue bound code */
700 static void gbound(void)
705 vtop
->r
&= ~VT_MUSTBOUND
;
706 /* if lvalue, then use checking code before dereferencing */
707 if (vtop
->r
& VT_LVAL
) {
708 /* if not VT_BOUNDED value, then make one */
709 if (!(vtop
->r
& VT_BOUNDED
)) {
710 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
711 /* must save type because we must set it to int to get pointer */
713 vtop
->type
.t
= VT_PTR
;
716 gen_bounded_ptr_add();
717 vtop
->r
|= lval_type
;
720 /* then check for dereferencing */
721 gen_bounded_ptr_deref();
726 /* store vtop a register belonging to class 'rc'. lvalues are
727 converted to values. Cannot be used if cannot be converted to
728 register value (such as structures). */
729 ST_FUNC
int gv(int rc
)
731 int r
, bit_pos
, bit_size
, size
, align
, i
;
734 /* NOTE: get_reg can modify vstack[] */
735 if (vtop
->type
.t
& VT_BITFIELD
) {
738 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
739 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
740 /* remove bit field info to avoid loops */
741 vtop
->type
.t
&= ~VT_BITFIELD
& ((1 << VT_STRUCT_SHIFT
) - 1);
742 /* cast to int to propagate signedness in following ops */
743 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
748 if((vtop
->type
.t
& VT_UNSIGNED
) ||
749 (vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
750 type
.t
|= VT_UNSIGNED
;
752 /* generate shifts */
753 vpushi(bits
- (bit_pos
+ bit_size
));
755 vpushi(bits
- bit_size
);
756 /* NOTE: transformed to SHR if unsigned */
760 if (is_float(vtop
->type
.t
) &&
761 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
764 unsigned long offset
;
765 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
769 /* XXX: unify with initializers handling ? */
770 /* CPUs usually cannot use float constants, so we store them
771 generically in data segment */
772 size
= type_size(&vtop
->type
, &align
);
773 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
774 data_section
->data_offset
= offset
;
775 /* XXX: not portable yet */
776 #if defined(__i386__) || defined(__x86_64__)
777 /* Zero pad x87 tenbyte long doubles */
778 if (size
== LDOUBLE_SIZE
) {
779 vtop
->c
.tab
[2] &= 0xffff;
780 #if LDOUBLE_SIZE == 16
785 ptr
= section_ptr_add(data_section
, size
);
787 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
791 ptr
[i
] = vtop
->c
.tab
[size
-1-i
];
795 ptr
[i
] = vtop
->c
.tab
[i
];
796 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
797 vtop
->r
|= VT_LVAL
| VT_SYM
;
801 #ifdef CONFIG_TCC_BCHECK
802 if (vtop
->r
& VT_MUSTBOUND
)
806 r
= vtop
->r
& VT_VALMASK
;
807 rc2
= (rc
& RC_FLOAT
) ? RC_FLOAT
: RC_INT
;
808 #ifndef TCC_TARGET_ARM64
811 #ifdef TCC_TARGET_X86_64
812 else if (rc
== RC_FRET
)
817 /* need to reload if:
819 - lvalue (need to dereference pointer)
820 - already a register, but not in the right class */
822 || (vtop
->r
& VT_LVAL
)
823 || !(reg_classes
[r
] & rc
)
824 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
825 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
826 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
&& !(reg_classes
[vtop
->r2
] & rc2
))
828 || ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
833 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
834 if (((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
)) {
835 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
837 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
838 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
839 unsigned long long ll
;
841 int r2
, original_type
;
842 original_type
= vtop
->type
.t
;
843 /* two register type load : expand to two words
845 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
846 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
849 vtop
->c
.i
= ll
; /* first word */
851 vtop
->r
= r
; /* save register value */
852 vpushi(ll
>> 32); /* second word */
855 if (r
>= VT_CONST
|| /* XXX: test to VT_CONST incorrect ? */
856 (vtop
->r
& VT_LVAL
)) {
857 /* We do not want to modifier the long long
858 pointer here, so the safest (and less
859 efficient) is to save all the other registers
860 in the stack. XXX: totally inefficient. */
862 /* load from memory */
863 vtop
->type
.t
= load_type
;
866 vtop
[-1].r
= r
; /* save register value */
867 /* increment pointer to get second word */
868 vtop
->type
.t
= addr_type
;
873 vtop
->type
.t
= load_type
;
878 vtop
[-1].r
= r
; /* save register value */
879 vtop
->r
= vtop
[-1].r2
;
881 /* Allocate second register. Here we rely on the fact that
882 get_reg() tries first to free r2 of an SValue. */
886 /* write second register */
888 vtop
->type
.t
= original_type
;
889 } else if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
891 /* lvalue of scalar type : need to use lvalue type
892 because of possible cast */
895 /* compute memory access type */
896 if (vtop
->r
& VT_REF
)
897 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
902 else if (vtop
->r
& VT_LVAL_BYTE
)
904 else if (vtop
->r
& VT_LVAL_SHORT
)
906 if (vtop
->r
& VT_LVAL_UNSIGNED
)
910 /* restore wanted type */
913 /* one register type load */
918 #ifdef TCC_TARGET_C67
919 /* uses register pairs for doubles */
920 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
927 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
928 ST_FUNC
void gv2(int rc1
, int rc2
)
932 /* generate more generic register first. But VT_JMP or VT_CMP
933 values must be generated first in all cases to avoid possible
935 v
= vtop
[0].r
& VT_VALMASK
;
936 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
941 /* test if reload is needed for first register */
942 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
952 /* test if reload is needed for first register */
953 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
959 #ifndef TCC_TARGET_ARM64
960 /* wrapper around RC_FRET to return a register by type */
961 static int rc_fret(int t
)
963 #ifdef TCC_TARGET_X86_64
964 if (t
== VT_LDOUBLE
) {
972 /* wrapper around REG_FRET to return a register by type */
973 static int reg_fret(int t
)
975 #ifdef TCC_TARGET_X86_64
976 if (t
== VT_LDOUBLE
) {
983 /* expand long long on stack in two int registers */
984 static void lexpand(void)
988 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
991 vtop
[0].r
= vtop
[-1].r2
;
992 vtop
[0].r2
= VT_CONST
;
993 vtop
[-1].r2
= VT_CONST
;
994 vtop
[0].type
.t
= VT_INT
| u
;
995 vtop
[-1].type
.t
= VT_INT
| u
;
998 #ifdef TCC_TARGET_ARM
999 /* expand long long on stack */
1000 ST_FUNC
void lexpand_nr(void)
1004 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
1006 vtop
->r2
= VT_CONST
;
1007 vtop
->type
.t
= VT_INT
| u
;
1008 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
1009 if (v
== VT_CONST
) {
1010 vtop
[-1].c
.i
= vtop
->c
.i
;
1011 vtop
->c
.i
= vtop
->c
.i
>> 32;
1013 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
1015 vtop
->r
= vtop
[-1].r
;
1016 } else if (v
> VT_CONST
) {
1020 vtop
->r
= vtop
[-1].r2
;
1021 vtop
[-1].r2
= VT_CONST
;
1022 vtop
[-1].type
.t
= VT_INT
| u
;
1026 /* build a long long from two ints */
1027 static void lbuild(int t
)
1029 gv2(RC_INT
, RC_INT
);
1030 vtop
[-1].r2
= vtop
[0].r
;
1031 vtop
[-1].type
.t
= t
;
1035 /* rotate n first stack elements to the bottom
1036 I1 ... In -> I2 ... In I1 [top is right]
1038 ST_FUNC
void vrotb(int n
)
1044 for(i
=-n
+1;i
!=0;i
++)
1045 vtop
[i
] = vtop
[i
+1];
1049 /* rotate the n elements before entry e towards the top
1050 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
1052 ST_FUNC
void vrote(SValue
*e
, int n
)
1058 for(i
= 0;i
< n
- 1; i
++)
1063 /* rotate n first stack elements to the top
1064 I1 ... In -> In I1 ... I(n-1) [top is right]
1066 ST_FUNC
void vrott(int n
)
1071 /* pop stack value */
1072 ST_FUNC
void vpop(void)
1075 v
= vtop
->r
& VT_VALMASK
;
1076 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1077 /* for x86, we need to pop the FP stack */
1078 if (v
== TREG_ST0
&& !nocode_wanted
) {
1079 o(0xd8dd); /* fstp %st(0) */
1082 if (v
== VT_JMP
|| v
== VT_JMPI
) {
1083 /* need to put correct jump if && or || without test */
1089 /* convert stack entry to register and duplicate its value in another
1091 static void gv_dup(void)
1097 if ((t
& VT_BTYPE
) == VT_LLONG
) {
1104 /* stack: H L L1 H1 */
1112 /* duplicate value */
1117 #ifdef TCC_TARGET_X86_64
1118 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1128 load(r1
, &sv
); /* move r to r1 */
1130 /* duplicates value */
1136 /* Generate value test
1138 * Generate a test for any value (jump, comparison and integers) */
1139 ST_FUNC
int gvtst(int inv
, int t
)
1141 int v
= vtop
->r
& VT_VALMASK
;
1142 if (v
!= VT_CMP
&& v
!= VT_JMP
&& v
!= VT_JMPI
) {
1146 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1147 /* constant jmp optimization */
1148 if ((vtop
->c
.i
!= 0) != inv
)
1153 return gtst(inv
, t
);
1156 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
1157 /* generate CPU independent (unsigned) long long operations */
1158 static void gen_opl(int op
)
1160 int t
, a
, b
, op1
, c
, i
;
1162 unsigned short reg_iret
= REG_IRET
;
1163 unsigned short reg_lret
= REG_LRET
;
1169 func
= TOK___divdi3
;
1172 func
= TOK___udivdi3
;
1175 func
= TOK___moddi3
;
1178 func
= TOK___umoddi3
;
1185 /* call generic long long function */
1186 vpush_global_sym(&func_old_type
, func
);
1191 vtop
->r2
= reg_lret
;
1204 /* stack: L1 H1 L2 H2 */
1209 vtop
[-2] = vtop
[-3];
1212 /* stack: H1 H2 L1 L2 */
1218 /* stack: H1 H2 L1 L2 ML MH */
1221 /* stack: ML MH H1 H2 L1 L2 */
1225 /* stack: ML MH H1 L2 H2 L1 */
1230 /* stack: ML MH M1 M2 */
1233 } else if (op
== '+' || op
== '-') {
1234 /* XXX: add non carry method too (for MIPS or alpha) */
1240 /* stack: H1 H2 (L1 op L2) */
1243 gen_op(op1
+ 1); /* TOK_xxxC2 */
1246 /* stack: H1 H2 (L1 op L2) */
1249 /* stack: (L1 op L2) H1 H2 */
1251 /* stack: (L1 op L2) (H1 op H2) */
1259 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1260 t
= vtop
[-1].type
.t
;
1264 /* stack: L H shift */
1266 /* constant: simpler */
1267 /* NOTE: all comments are for SHL. the other cases are
1268 done by swaping words */
1279 if (op
!= TOK_SAR
) {
1312 /* XXX: should provide a faster fallback on x86 ? */
1315 func
= TOK___ashrdi3
;
1318 func
= TOK___lshrdi3
;
1321 func
= TOK___ashldi3
;
1327 /* compare operations */
1333 /* stack: L1 H1 L2 H2 */
1335 vtop
[-1] = vtop
[-2];
1337 /* stack: L1 L2 H1 H2 */
1340 /* when values are equal, we need to compare low words. since
1341 the jump is inverted, we invert the test too. */
1344 else if (op1
== TOK_GT
)
1346 else if (op1
== TOK_ULT
)
1348 else if (op1
== TOK_UGT
)
1353 if (op1
!= TOK_NE
) {
1357 /* generate non equal test */
1358 /* XXX: NOT PORTABLE yet */
1362 #if defined(TCC_TARGET_I386)
1363 b
= psym(0x850f, 0);
1364 #elif defined(TCC_TARGET_ARM)
1366 o(0x1A000000 | encbranch(ind
, 0, 1));
1367 #elif defined(TCC_TARGET_C67) || defined(TCC_TARGET_ARM64)
1368 tcc_error("not implemented");
1370 #error not supported
1374 /* compare low. Always unsigned */
1378 else if (op1
== TOK_LE
)
1380 else if (op1
== TOK_GT
)
1382 else if (op1
== TOK_GE
)
1393 static uint64_t gen_opic_sdiv(uint64_t a
, uint64_t b
)
1395 uint64_t x
= (a
>> 63 ? -a
: a
) / (b
>> 63 ? -b
: b
);
1396 return (a
^ b
) >> 63 ? -x
: x
;
1399 static int gen_opic_lt(uint64_t a
, uint64_t b
)
1401 return (a
^ (uint64_t)1 << 63) < (b
^ (uint64_t)1 << 63);
1404 /* handle integer constant optimizations and various machine
1406 static void gen_opic(int op
)
1408 SValue
*v1
= vtop
- 1;
1410 int t1
= v1
->type
.t
& VT_BTYPE
;
1411 int t2
= v2
->type
.t
& VT_BTYPE
;
1412 int c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1413 int c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1414 uint64_t l1
= c1
? v1
->c
.i
: 0;
1415 uint64_t l2
= c2
? v2
->c
.i
: 0;
1416 int shm
= (t1
== VT_LLONG
) ? 63 : 31;
1419 l1
= ((uint32_t)l1
|
1420 (v1
->type
.t
& VT_UNSIGNED
? 0 : -(l1
& 0x80000000)));
1422 l2
= ((uint32_t)l2
|
1423 (v2
->type
.t
& VT_UNSIGNED
? 0 : -(l2
& 0x80000000)));
1427 case '+': l1
+= l2
; break;
1428 case '-': l1
-= l2
; break;
1429 case '&': l1
&= l2
; break;
1430 case '^': l1
^= l2
; break;
1431 case '|': l1
|= l2
; break;
1432 case '*': l1
*= l2
; break;
1439 /* if division by zero, generate explicit division */
1442 tcc_error("division by zero in constant");
1446 default: l1
= gen_opic_sdiv(l1
, l2
); break;
1447 case '%': l1
= l1
- l2
* gen_opic_sdiv(l1
, l2
); break;
1448 case TOK_UDIV
: l1
= l1
/ l2
; break;
1449 case TOK_UMOD
: l1
= l1
% l2
; break;
1452 case TOK_SHL
: l1
<<= (l2
& shm
); break;
1453 case TOK_SHR
: l1
>>= (l2
& shm
); break;
1455 l1
= (l1
>> 63) ? ~(~l1
>> (l2
& shm
)) : l1
>> (l2
& shm
);
1458 case TOK_ULT
: l1
= l1
< l2
; break;
1459 case TOK_UGE
: l1
= l1
>= l2
; break;
1460 case TOK_EQ
: l1
= l1
== l2
; break;
1461 case TOK_NE
: l1
= l1
!= l2
; break;
1462 case TOK_ULE
: l1
= l1
<= l2
; break;
1463 case TOK_UGT
: l1
= l1
> l2
; break;
1464 case TOK_LT
: l1
= gen_opic_lt(l1
, l2
); break;
1465 case TOK_GE
: l1
= !gen_opic_lt(l1
, l2
); break;
1466 case TOK_LE
: l1
= !gen_opic_lt(l2
, l1
); break;
1467 case TOK_GT
: l1
= gen_opic_lt(l2
, l1
); break;
1469 case TOK_LAND
: l1
= l1
&& l2
; break;
1470 case TOK_LOR
: l1
= l1
|| l2
; break;
1477 /* if commutative ops, put c2 as constant */
1478 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1479 op
== '|' || op
== '*')) {
1481 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1482 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1484 if (!const_wanted
&&
1486 (op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
)) ||
1487 (l1
== -1 && op
== TOK_SAR
))) {
1488 /* treat (0 << x), (0 >> x) and (-1 >> x) as constant */
1490 } else if (!const_wanted
&&
1491 c2
&& ((l2
== 0 && (op
== '&' || op
== '*')) ||
1492 (l2
== -1 && op
== '|') ||
1493 (l2
== 0xffffffff && t2
!= VT_LLONG
&& op
== '|') ||
1494 (l2
== 1 && (op
== '%' || op
== TOK_UMOD
)))) {
1495 /* treat (x & 0), (x * 0), (x | -1) and (x % 1) as constant */
1500 } else if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1503 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1504 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1508 /* filter out NOP operations like x*1, x-0, x&-1... */
1510 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1511 /* try to use shifts instead of muls or divs */
1512 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1521 else if (op
== TOK_PDIV
)
1527 } else if (c2
&& (op
== '+' || op
== '-') &&
1528 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
1529 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1530 /* symbol + constant case */
1537 if (!nocode_wanted
) {
1538 /* call low level op generator */
1539 if (t1
== VT_LLONG
|| t2
== VT_LLONG
||
1540 (PTR_SIZE
== 8 && (t1
== VT_PTR
|| t2
== VT_PTR
)))
1551 /* generate a floating point operation with constant propagation */
1552 static void gen_opif(int op
)
1560 /* currently, we cannot do computations with forward symbols */
1561 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1562 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1564 if (v1
->type
.t
== VT_FLOAT
) {
1567 } else if (v1
->type
.t
== VT_DOUBLE
) {
1575 /* NOTE: we only do constant propagation if finite number (not
1576 NaN or infinity) (ANSI spec) */
1577 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1581 case '+': f1
+= f2
; break;
1582 case '-': f1
-= f2
; break;
1583 case '*': f1
*= f2
; break;
1587 tcc_error("division by zero in constant");
1592 /* XXX: also handles tests ? */
1596 /* XXX: overflow test ? */
1597 if (v1
->type
.t
== VT_FLOAT
) {
1599 } else if (v1
->type
.t
== VT_DOUBLE
) {
1607 if (!nocode_wanted
) {
1615 static int pointed_size(CType
*type
)
1618 return type_size(pointed_type(type
), &align
);
1621 static void vla_runtime_pointed_size(CType
*type
)
1624 vla_runtime_type_size(pointed_type(type
), &align
);
1627 static inline int is_null_pointer(SValue
*p
)
1629 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1631 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& (uint32_t)p
->c
.i
== 0) ||
1632 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.i
== 0) ||
1633 ((p
->type
.t
& VT_BTYPE
) == VT_PTR
&&
1634 (PTR_SIZE
== 4 ? (uint32_t)p
->c
.i
== 0 : p
->c
.i
== 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_STRUCT
|| bt2
== VT_STRUCT
) {
1703 tcc_error("operation on a struct");
1704 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
1705 /* at least one operand is a pointer */
1706 /* relationnal op: must be both pointers */
1707 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
1708 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1709 /* pointers are handled are unsigned */
1710 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1711 t
= VT_LLONG
| VT_UNSIGNED
;
1713 t
= VT_INT
| VT_UNSIGNED
;
1717 /* if both pointers, then it must be the '-' op */
1718 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
1720 tcc_error("cannot use pointers here");
1721 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1722 /* XXX: check that types are compatible */
1723 if (vtop
[-1].type
.t
& VT_VLA
) {
1724 vla_runtime_pointed_size(&vtop
[-1].type
);
1726 vpushi(pointed_size(&vtop
[-1].type
));
1730 /* set to integer type */
1731 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1732 vtop
->type
.t
= VT_LLONG
;
1734 vtop
->type
.t
= VT_INT
;
1739 /* exactly one pointer : must be '+' or '-'. */
1740 if (op
!= '-' && op
!= '+')
1741 tcc_error("cannot use pointers here");
1742 /* Put pointer as first operand */
1743 if (bt2
== VT_PTR
) {
1747 type1
= vtop
[-1].type
;
1748 type1
.t
&= ~VT_ARRAY
;
1749 if (vtop
[-1].type
.t
& VT_VLA
)
1750 vla_runtime_pointed_size(&vtop
[-1].type
);
1752 u
= pointed_size(&vtop
[-1].type
);
1754 tcc_error("unknown array element size");
1755 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1758 /* XXX: cast to int ? (long long case) */
1764 /* #ifdef CONFIG_TCC_BCHECK
1765 The main reason to removing this code:
1772 fprintf(stderr, "v+i-j = %p\n", v+i-j);
1773 fprintf(stderr, "v+(i-j) = %p\n", v+(i-j));
1775 When this code is on. then the output looks like
1777 v+(i-j) = 0xbff84000
1779 /* if evaluating constant expression, no code should be
1780 generated, so no bound check */
1781 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
1782 /* if bounded pointers, we generate a special code to
1789 gen_bounded_ptr_add();
1795 /* put again type if gen_opic() swaped operands */
1798 } else if (is_float(bt1
) || is_float(bt2
)) {
1799 /* compute bigger type and do implicit casts */
1800 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
1802 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
1807 /* floats can only be used for a few operations */
1808 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
1809 (op
< TOK_ULT
|| op
> TOK_GT
))
1810 tcc_error("invalid operands for binary operation");
1812 } else if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
) {
1813 t
= bt1
== VT_LLONG
? VT_LLONG
: VT_INT
;
1814 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (t
| VT_UNSIGNED
))
1817 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
1818 /* cast to biggest op */
1820 /* convert to unsigned if it does not fit in a long long */
1821 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
1822 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
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
) || !(vtop
->c
.i
>> 63))
1998 vtop
->c
.ld
= vtop
->c
.i
;
2000 vtop
->c
.ld
= -(long double)-vtop
->c
.i
;
2002 if ((sbt
& VT_UNSIGNED
) || !(vtop
->c
.i
>> 31))
2003 vtop
->c
.ld
= (uint32_t)vtop
->c
.i
;
2005 vtop
->c
.ld
= -(long double)-(uint32_t)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
.i
= vtop
->c
.ld
;
2014 } else if (sf
&& dbt
== VT_BOOL
) {
2015 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
2018 vtop
->c
.i
= vtop
->c
.ld
;
2019 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
2021 else if (sbt
& VT_UNSIGNED
)
2022 vtop
->c
.i
= (uint32_t)vtop
->c
.i
;
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
.i
= ((uint32_t)vtop
->c
.i
|
2029 -(vtop
->c
.i
& 0x80000000));
2031 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
2033 else if (dbt
== VT_BOOL
)
2034 vtop
->c
.i
= (vtop
->c
.i
!= 0);
2035 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2036 else if (dbt
== VT_PTR
)
2039 else if (dbt
!= VT_LLONG
) {
2040 uint32_t m
= ((dbt
& VT_BTYPE
) == VT_BYTE
? 0xff :
2041 (dbt
& VT_BTYPE
) == VT_SHORT
? 0xffff :
2044 if (!(dbt
& VT_UNSIGNED
))
2045 vtop
->c
.i
|= -(vtop
->c
.i
& ((m
>> 1) + 1));
2048 } else if (p
&& dbt
== VT_BOOL
) {
2051 } else if (!nocode_wanted
) {
2052 /* non constant case: generate code */
2054 /* convert from fp to fp */
2057 /* convert int to fp */
2060 /* convert fp to int */
2061 if (dbt
== VT_BOOL
) {
2065 /* we handle char/short/etc... with generic code */
2066 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
2067 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
2071 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
2072 /* additional cast for char/short... */
2077 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
2078 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
2079 if ((sbt
& VT_BTYPE
) != VT_LLONG
&& !nocode_wanted
) {
2080 /* scalar to long long */
2081 /* machine independent conversion */
2083 /* generate high word */
2084 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
2088 if (sbt
== VT_PTR
) {
2089 /* cast from pointer to int before we apply
2090 shift operation, which pointers don't support*/
2091 gen_cast(&int_type
);
2097 /* patch second register */
2098 vtop
[-1].r2
= vtop
->r
;
2102 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
2103 (dbt
& VT_BTYPE
) == VT_PTR
||
2104 (dbt
& VT_BTYPE
) == VT_FUNC
) {
2105 if ((sbt
& VT_BTYPE
) != VT_LLONG
&&
2106 (sbt
& VT_BTYPE
) != VT_PTR
&&
2107 (sbt
& VT_BTYPE
) != VT_FUNC
&& !nocode_wanted
) {
2108 /* need to convert from 32bit to 64bit */
2110 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
2111 #if defined(TCC_TARGET_ARM64)
2113 #elif defined(TCC_TARGET_X86_64)
2115 /* x86_64 specific: movslq */
2117 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
2124 } else if (dbt
== VT_BOOL
) {
2125 /* scalar to bool */
2128 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
2129 (dbt
& VT_BTYPE
) == VT_SHORT
) {
2130 if (sbt
== VT_PTR
) {
2131 vtop
->type
.t
= VT_INT
;
2132 tcc_warning("nonportable conversion from pointer to char/short");
2134 force_charshort_cast(dbt
);
2135 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
2137 if (sbt
== VT_LLONG
&& !nocode_wanted
) {
2138 /* from long long: just take low order word */
2142 /* if lvalue and single word type, nothing to do because
2143 the lvalue already contains the real type size (see
2144 VT_LVAL_xxx constants) */
2147 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
2148 /* if we are casting between pointer types,
2149 we must update the VT_LVAL_xxx size */
2150 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
2151 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
2156 /* return type size as known at compile time. Put alignment at 'a' */
2157 ST_FUNC
int type_size(CType
*type
, int *a
)
2162 bt
= type
->t
& VT_BTYPE
;
2163 if (bt
== VT_STRUCT
) {
2168 } else if (bt
== VT_PTR
) {
2169 if (type
->t
& VT_ARRAY
) {
2173 ts
= type_size(&s
->type
, a
);
2175 if (ts
< 0 && s
->c
< 0)
2183 } else if (bt
== VT_LDOUBLE
) {
2185 return LDOUBLE_SIZE
;
2186 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
2187 #ifdef TCC_TARGET_I386
2188 #ifdef TCC_TARGET_PE
2193 #elif defined(TCC_TARGET_ARM)
2203 } else if (bt
== VT_INT
|| bt
== VT_FLOAT
) {
2206 } else if (bt
== VT_SHORT
) {
2209 } else if (bt
== VT_QLONG
|| bt
== VT_QFLOAT
) {
2212 } else if (bt
== VT_ENUM
) {
2214 /* Enums might be incomplete, so don't just return '4' here. */
2215 return type
->ref
->c
;
2217 /* char, void, function, _Bool */
2223 /* push type size as known at runtime time on top of value stack. Put
2225 ST_FUNC
void vla_runtime_type_size(CType
*type
, int *a
)
2227 if (type
->t
& VT_VLA
) {
2228 vset(&int_type
, VT_LOCAL
|VT_LVAL
, type
->ref
->c
);
2230 vpushi(type_size(type
, a
));
2234 static void vla_sp_restore(void) {
2235 if (vlas_in_scope
) {
2236 gen_vla_sp_restore(vla_sp_loc
);
2240 static void vla_sp_restore_root(void) {
2241 if (vlas_in_scope
) {
2242 gen_vla_sp_restore(vla_sp_root_loc
);
2246 /* return the pointed type of t */
2247 static inline CType
*pointed_type(CType
*type
)
2249 return &type
->ref
->type
;
2252 /* modify type so that its it is a pointer to type. */
2253 ST_FUNC
void mk_pointer(CType
*type
)
2256 s
= sym_push(SYM_FIELD
, type
, 0, -1);
2257 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
2261 /* compare function types. OLD functions match any new functions */
2262 static int is_compatible_func(CType
*type1
, CType
*type2
)
2268 if (!is_compatible_types(&s1
->type
, &s2
->type
))
2270 /* check func_call */
2271 if (s1
->a
.func_call
!= s2
->a
.func_call
)
2273 /* XXX: not complete */
2274 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
2278 while (s1
!= NULL
) {
2281 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
2291 /* return true if type1 and type2 are the same. If unqualified is
2292 true, qualifiers on the types are ignored.
2294 - enums are not checked as gcc __builtin_types_compatible_p ()
2296 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2300 t1
= type1
->t
& VT_TYPE
;
2301 t2
= type2
->t
& VT_TYPE
;
2303 /* strip qualifiers before comparing */
2304 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2305 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2307 /* Default Vs explicit signedness only matters for char */
2308 if ((t1
& VT_BTYPE
) != VT_BYTE
) {
2312 /* XXX: bitfields ? */
2315 /* test more complicated cases */
2316 bt1
= t1
& VT_BTYPE
;
2317 if (bt1
== VT_PTR
) {
2318 type1
= pointed_type(type1
);
2319 type2
= pointed_type(type2
);
2320 return is_compatible_types(type1
, type2
);
2321 } else if (bt1
== VT_STRUCT
) {
2322 return (type1
->ref
== type2
->ref
);
2323 } else if (bt1
== VT_FUNC
) {
2324 return is_compatible_func(type1
, type2
);
2330 /* return true if type1 and type2 are exactly the same (including
2333 static int is_compatible_types(CType
*type1
, CType
*type2
)
2335 return compare_types(type1
,type2
,0);
2338 /* return true if type1 and type2 are the same (ignoring qualifiers).
2340 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
2342 return compare_types(type1
,type2
,1);
2345 /* print a type. If 'varstr' is not NULL, then the variable is also
2346 printed in the type */
2348 /* XXX: add array and function pointers */
2349 static void type_to_str(char *buf
, int buf_size
,
2350 CType
*type
, const char *varstr
)
2357 t
= type
->t
& VT_TYPE
;
2360 if (t
& VT_CONSTANT
)
2361 pstrcat(buf
, buf_size
, "const ");
2362 if (t
& VT_VOLATILE
)
2363 pstrcat(buf
, buf_size
, "volatile ");
2364 if ((t
& (VT_DEFSIGN
| VT_UNSIGNED
)) == (VT_DEFSIGN
| VT_UNSIGNED
))
2365 pstrcat(buf
, buf_size
, "unsigned ");
2366 else if (t
& VT_DEFSIGN
)
2367 pstrcat(buf
, buf_size
, "signed ");
2397 tstr
= "long double";
2399 pstrcat(buf
, buf_size
, tstr
);
2403 if (bt
== VT_STRUCT
)
2407 pstrcat(buf
, buf_size
, tstr
);
2408 v
= type
->ref
->v
& ~SYM_STRUCT
;
2409 if (v
>= SYM_FIRST_ANOM
)
2410 pstrcat(buf
, buf_size
, "<anonymous>");
2412 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
2416 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
2417 pstrcat(buf
, buf_size
, "(");
2419 while (sa
!= NULL
) {
2420 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
2421 pstrcat(buf
, buf_size
, buf1
);
2424 pstrcat(buf
, buf_size
, ", ");
2426 pstrcat(buf
, buf_size
, ")");
2431 snprintf(buf1
, sizeof(buf1
), "%s[%ld]", varstr
? varstr
: "", s
->c
);
2432 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2435 pstrcpy(buf1
, sizeof(buf1
), "*");
2436 if (t
& VT_CONSTANT
)
2437 pstrcat(buf1
, buf_size
, "const ");
2438 if (t
& VT_VOLATILE
)
2439 pstrcat(buf1
, buf_size
, "volatile ");
2441 pstrcat(buf1
, sizeof(buf1
), varstr
);
2442 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2446 pstrcat(buf
, buf_size
, " ");
2447 pstrcat(buf
, buf_size
, varstr
);
2452 /* verify type compatibility to store vtop in 'dt' type, and generate
2454 static void gen_assign_cast(CType
*dt
)
2456 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
2457 char buf1
[256], buf2
[256];
2460 st
= &vtop
->type
; /* source type */
2461 dbt
= dt
->t
& VT_BTYPE
;
2462 sbt
= st
->t
& VT_BTYPE
;
2463 if (sbt
== VT_VOID
|| dbt
== VT_VOID
) {
2464 if (sbt
== VT_VOID
&& dbt
== VT_VOID
)
2466 It is Ok if both are void
2472 gcc accepts this program
2475 tcc_error("cannot cast from/to void");
2477 if (dt
->t
& VT_CONSTANT
)
2478 tcc_warning("assignment of read-only location");
2481 /* special cases for pointers */
2482 /* '0' can also be a pointer */
2483 if (is_null_pointer(vtop
))
2485 /* accept implicit pointer to integer cast with warning */
2486 if (is_integer_btype(sbt
)) {
2487 tcc_warning("assignment makes pointer from integer without a cast");
2490 type1
= pointed_type(dt
);
2491 /* a function is implicitely a function pointer */
2492 if (sbt
== VT_FUNC
) {
2493 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
2494 !is_compatible_types(pointed_type(dt
), st
))
2495 tcc_warning("assignment from incompatible pointer type");
2500 type2
= pointed_type(st
);
2501 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2502 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
2503 /* void * can match anything */
2505 /* exact type match, except for unsigned */
2508 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
|
2510 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
|
2512 if (!is_compatible_types(&tmp_type1
, &tmp_type2
))
2513 tcc_warning("assignment from incompatible pointer type");
2515 /* check const and volatile */
2516 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
2517 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
2518 tcc_warning("assignment discards qualifiers from pointer target type");
2524 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
2525 tcc_warning("assignment makes integer from pointer without a cast");
2526 } else if (sbt
== VT_STRUCT
) {
2527 goto case_VT_STRUCT
;
2529 /* XXX: more tests */
2535 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2536 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2537 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2539 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
2540 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
2541 tcc_error("cannot cast '%s' to '%s'", buf1
, buf2
);
2549 /* store vtop in lvalue pushed on stack */
2550 ST_FUNC
void vstore(void)
2552 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
2554 ft
= vtop
[-1].type
.t
;
2555 sbt
= vtop
->type
.t
& VT_BTYPE
;
2556 dbt
= ft
& VT_BTYPE
;
2557 if ((((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2558 (sbt
== VT_INT
&& dbt
== VT_SHORT
))
2559 && !(vtop
->type
.t
& VT_BITFIELD
)) {
2560 /* optimize char/short casts */
2561 delayed_cast
= VT_MUSTCAST
;
2562 vtop
->type
.t
= (ft
& VT_TYPE
& ~VT_BITFIELD
&
2563 ((1 << VT_STRUCT_SHIFT
) - 1));
2564 /* XXX: factorize */
2565 if (ft
& VT_CONSTANT
)
2566 tcc_warning("assignment of read-only location");
2569 if (!(ft
& VT_BITFIELD
))
2570 gen_assign_cast(&vtop
[-1].type
);
2573 if (sbt
== VT_STRUCT
) {
2574 /* if structure, only generate pointer */
2575 /* structure assignment : generate memcpy */
2576 /* XXX: optimize if small size */
2577 if (!nocode_wanted
) {
2578 size
= type_size(&vtop
->type
, &align
);
2582 vtop
->type
.t
= VT_PTR
;
2585 /* address of memcpy() */
2588 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2589 else if(!(align
& 3))
2590 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2593 /* Use memmove, rather than memcpy, as dest and src may be same: */
2594 vpush_global_sym(&func_old_type
, TOK_memmove
);
2599 vtop
->type
.t
= VT_PTR
;
2608 /* leave source on stack */
2609 } else if (ft
& VT_BITFIELD
) {
2610 /* bitfield store handling */
2612 /* save lvalue as expression result (example: s.b = s.a = n;) */
2613 vdup(), vtop
[-1] = vtop
[-2];
2615 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2616 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2617 /* remove bit field info to avoid loops */
2618 vtop
[-1].type
.t
= ft
& ~VT_BITFIELD
& ((1 << VT_STRUCT_SHIFT
) - 1);
2620 if((ft
& VT_BTYPE
) == VT_BOOL
) {
2621 gen_cast(&vtop
[-1].type
);
2622 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2625 /* duplicate destination */
2627 vtop
[-1] = vtop
[-2];
2629 /* mask and shift source */
2630 if((ft
& VT_BTYPE
) != VT_BOOL
) {
2631 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2632 vpushll((1ULL << bit_size
) - 1ULL);
2634 vpushi((1 << bit_size
) - 1);
2640 /* load destination, mask and or with source */
2642 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2643 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2645 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2651 /* ... and discard */
2655 if (!nocode_wanted
) {
2656 #ifdef CONFIG_TCC_BCHECK
2657 /* bound check case */
2658 if (vtop
[-1].r
& VT_MUSTBOUND
) {
2667 #ifdef TCC_TARGET_X86_64
2668 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
2670 } else if ((ft
& VT_BTYPE
) == VT_QFLOAT
) {
2675 r
= gv(rc
); /* generate value */
2676 /* if lvalue was saved on stack, must read it */
2677 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
2679 t
= get_reg(RC_INT
);
2680 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2685 sv
.r
= VT_LOCAL
| VT_LVAL
;
2686 sv
.c
.i
= vtop
[-1].c
.i
;
2688 vtop
[-1].r
= t
| VT_LVAL
;
2690 /* two word case handling : store second register at word + 4 (or +8 for x86-64) */
2691 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2692 if (((ft
& VT_BTYPE
) == VT_QLONG
) || ((ft
& VT_BTYPE
) == VT_QFLOAT
)) {
2693 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
2695 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
2696 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
2698 vtop
[-1].type
.t
= load_type
;
2701 /* convert to int to increment easily */
2702 vtop
->type
.t
= addr_type
;
2708 vtop
[-1].type
.t
= load_type
;
2709 /* XXX: it works because r2 is spilled last ! */
2710 store(vtop
->r2
, vtop
- 1);
2716 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
2717 vtop
->r
|= delayed_cast
;
2721 /* post defines POST/PRE add. c is the token ++ or -- */
2722 ST_FUNC
void inc(int post
, int c
)
2725 vdup(); /* save lvalue */
2728 gv_dup(); /* duplicate value */
2730 vdup(); /* duplicate value */
2735 vpushi(c
- TOK_MID
);
2737 vstore(); /* store value */
2739 vpop(); /* if post op, return saved value */
2742 /* Parse GNUC __attribute__ extension. Currently, the following
2743 extensions are recognized:
2744 - aligned(n) : set data/function alignment.
2745 - packed : force data alignment to 1
2746 - section(x) : generate data/code in this section.
2747 - unused : currently ignored, but may be used someday.
2748 - regparm(n) : pass function parameters in registers (i386 only)
2750 static void parse_attribute(AttributeDef
*ad
)
2754 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
2758 while (tok
!= ')') {
2759 if (tok
< TOK_IDENT
)
2760 expect("attribute name");
2768 expect("section name");
2769 ad
->section
= find_section(tcc_state
, (char *)tokc
.str
.data
);
2777 expect("alias(\"target\")");
2778 ad
->alias_target
= /* save string as token, for later */
2779 tok_alloc((char*)tokc
.str
.data
, tokc
.str
.size
-1)->tok
;
2783 case TOK_VISIBILITY1
:
2784 case TOK_VISIBILITY2
:
2787 expect("visibility(\"default|hidden|internal|protected\")");
2788 if (!strcmp (tokc
.str
.data
, "default"))
2789 ad
->a
.visibility
= STV_DEFAULT
;
2790 else if (!strcmp (tokc
.str
.data
, "hidden"))
2791 ad
->a
.visibility
= STV_HIDDEN
;
2792 else if (!strcmp (tokc
.str
.data
, "internal"))
2793 ad
->a
.visibility
= STV_INTERNAL
;
2794 else if (!strcmp (tokc
.str
.data
, "protected"))
2795 ad
->a
.visibility
= STV_PROTECTED
;
2797 expect("visibility(\"default|hidden|internal|protected\")");
2806 if (n
<= 0 || (n
& (n
- 1)) != 0)
2807 tcc_error("alignment must be a positive power of two");
2824 /* currently, no need to handle it because tcc does not
2825 track unused objects */
2829 /* currently, no need to handle it because tcc does not
2830 track unused objects */
2835 ad
->a
.func_call
= FUNC_CDECL
;
2840 ad
->a
.func_call
= FUNC_STDCALL
;
2842 #ifdef TCC_TARGET_I386
2852 ad
->a
.func_call
= FUNC_FASTCALL1
+ n
- 1;
2858 ad
->a
.func_call
= FUNC_FASTCALLW
;
2865 ad
->a
.mode
= VT_LLONG
+ 1;
2868 ad
->a
.mode
= VT_SHORT
+ 1;
2871 ad
->a
.mode
= VT_INT
+ 1;
2874 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
2881 ad
->a
.func_export
= 1;
2884 ad
->a
.func_import
= 1;
2887 if (tcc_state
->warn_unsupported
)
2888 tcc_warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
2889 /* skip parameters */
2891 int parenthesis
= 0;
2895 else if (tok
== ')')
2898 } while (parenthesis
&& tok
!= -1);
2911 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2912 static void struct_decl(CType
*type
, AttributeDef
*ad
, int u
)
2914 int a
, v
, size
, align
, maxalign
, c
, offset
, flexible
;
2915 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
, prevbt
;
2916 Sym
*s
, *ss
, *ass
, **ps
;
2920 a
= tok
; /* save decl type */
2922 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
2923 parse_attribute(ad
);
2929 /* struct already defined ? return it */
2931 expect("struct/union/enum name");
2933 if (s
&& (s
->scope
== local_scope
|| (tok
!= '{' && tok
!= ';'))) {
2935 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
2943 /* we put an undefined size for struct/union */
2944 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
2945 s
->r
= 0; /* default alignment is zero as gcc */
2946 /* put struct/union/enum name in type */
2954 tcc_error("struct/union/enum already defined");
2955 /* cannot be empty */
2957 /* non empty enums are not allowed */
2958 if (a
== TOK_ENUM
) {
2962 expect("identifier");
2964 if (ss
&& !local_stack
)
2965 tcc_error("redefinition of enumerator '%s'",
2966 get_tok_str(v
, NULL
));
2972 /* enum symbols have static storage */
2973 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
2974 ss
->type
.t
|= VT_STATIC
;
2979 /* NOTE: we accept a trailing comma */
2983 s
->c
= type_size(&int_type
, &align
);
2992 while (tok
!= '}') {
2993 parse_btype(&btype
, &ad1
);
2996 tcc_error("flexible array member '%s' not at the end of struct",
2997 get_tok_str(v
, NULL
));
3002 type_decl(&type1
, &ad1
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3004 if ((type1
.t
& VT_BTYPE
) != VT_STRUCT
)
3005 expect("identifier");
3007 int v
= btype
.ref
->v
;
3008 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
3009 if (tcc_state
->ms_extensions
== 0)
3010 expect("identifier");
3014 if (type_size(&type1
, &align
) < 0) {
3015 if ((a
== TOK_STRUCT
) && (type1
.t
& VT_ARRAY
) && c
)
3018 tcc_error("field '%s' has incomplete type",
3019 get_tok_str(v
, NULL
));
3021 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
3022 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
3023 tcc_error("invalid type for '%s'",
3024 get_tok_str(v
, NULL
));
3028 bit_size
= expr_const();
3029 /* XXX: handle v = 0 case for messages */
3031 tcc_error("negative width in bit-field '%s'",
3032 get_tok_str(v
, NULL
));
3033 if (v
&& bit_size
== 0)
3034 tcc_error("zero width for bit-field '%s'",
3035 get_tok_str(v
, NULL
));
3037 size
= type_size(&type1
, &align
);
3038 if (ad1
.a
.aligned
) {
3039 if (align
< ad1
.a
.aligned
)
3040 align
= ad1
.a
.aligned
;
3041 } else if (ad1
.a
.packed
) {
3043 } else if (*tcc_state
->pack_stack_ptr
) {
3044 if (align
> *tcc_state
->pack_stack_ptr
)
3045 align
= *tcc_state
->pack_stack_ptr
;
3048 if (bit_size
>= 0) {
3049 bt
= type1
.t
& VT_BTYPE
;
3056 tcc_error("bitfields must have scalar type");
3058 if (bit_size
> bsize
) {
3059 tcc_error("width of '%s' exceeds its type",
3060 get_tok_str(v
, NULL
));
3061 } else if (bit_size
== bsize
) {
3062 /* no need for bit fields */
3064 } else if (bit_size
== 0) {
3065 /* XXX: what to do if only padding in a
3067 /* zero size: means to pad */
3070 /* we do not have enough room ?
3071 did the type change?
3073 if ((bit_pos
+ bit_size
) > bsize
||
3074 bt
!= prevbt
|| a
== TOK_UNION
)
3077 /* XXX: handle LSB first */
3078 type1
.t
|= VT_BITFIELD
|
3079 (bit_pos
<< VT_STRUCT_SHIFT
) |
3080 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
3081 bit_pos
+= bit_size
;
3087 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
3088 /* add new memory data only if starting
3090 if (lbit_pos
== 0) {
3091 if (a
== TOK_STRUCT
) {
3092 c
= (c
+ align
- 1) & -align
;
3101 if (align
> maxalign
)
3105 printf("add field %s offset=%d",
3106 get_tok_str(v
, NULL
), offset
);
3107 if (type1
.t
& VT_BITFIELD
) {
3108 printf(" pos=%d size=%d",
3109 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
3110 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
3115 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
3117 while ((ass
= ass
->next
) != NULL
) {
3118 ss
= sym_push(ass
->v
, &ass
->type
, 0, offset
+ ass
->c
);
3123 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
3127 if (tok
== ';' || tok
== TOK_EOF
)
3134 /* store size and alignment */
3135 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
3141 /* return 1 if basic type is a type size (short, long, long long) */
3142 ST_FUNC
int is_btype_size(int bt
)
3144 return bt
== VT_SHORT
|| bt
== VT_LONG
|| bt
== VT_LLONG
;
3147 /* Add type qualifiers to a type. If the type is an array then the qualifiers
3148 are added to the element type, copied because it could be a typedef. */
3149 static void parse_btype_qualify(CType
*type
, int qualifiers
)
3151 while (type
->t
& VT_ARRAY
) {
3152 type
->ref
= sym_push(SYM_FIELD
, &type
->ref
->type
, 0, type
->ref
->c
);
3153 type
= &type
->ref
->type
;
3155 type
->t
|= qualifiers
;
3158 /* return 0 if no type declaration. otherwise, return the basic type
3161 static int parse_btype(CType
*type
, AttributeDef
*ad
)
3163 int t
, u
, bt_size
, complete
, type_found
, typespec_found
;
3167 memset(ad
, 0, sizeof(AttributeDef
));
3175 /* currently, we really ignore extension */
3186 tcc_error("too many basic types");
3188 bt_size
= is_btype_size (u
& VT_BTYPE
);
3189 if (u
== VT_INT
|| (!bt_size
&& !(t
& VT_TYPEDEF
)))
3204 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
3205 #ifndef TCC_TARGET_PE
3206 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3208 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
3209 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3215 #ifdef TCC_TARGET_ARM64
3217 /* GCC's __uint128_t appears in some Linux header files. Make it a
3218 synonym for long double to get the size and alignment right. */
3230 if ((t
& VT_BTYPE
) == VT_LONG
) {
3231 #ifdef TCC_TARGET_PE
3232 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
3234 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3242 struct_decl(&type1
, ad
, VT_ENUM
);
3245 type
->ref
= type1
.ref
;
3249 struct_decl(&type1
, ad
, VT_STRUCT
);
3252 /* type modifiers */
3257 parse_btype_qualify(type
, VT_CONSTANT
);
3265 parse_btype_qualify(type
, VT_VOLATILE
);
3272 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == (VT_DEFSIGN
|VT_UNSIGNED
))
3273 tcc_error("signed and unsigned modifier");
3286 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == VT_DEFSIGN
)
3287 tcc_error("signed and unsigned modifier");
3288 t
|= VT_DEFSIGN
| VT_UNSIGNED
;
3313 /* GNUC attribute */
3314 case TOK_ATTRIBUTE1
:
3315 case TOK_ATTRIBUTE2
:
3316 parse_attribute(ad
);
3319 t
= (t
& ~VT_BTYPE
) | u
;
3327 parse_expr_type(&type1
);
3328 /* remove all storage modifiers except typedef */
3329 type1
.t
&= ~(VT_STORAGE
&~VT_TYPEDEF
);
3335 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
3338 type
->t
= ((s
->type
.t
& ~VT_TYPEDEF
) |
3339 (t
& ~(VT_CONSTANT
| VT_VOLATILE
)));
3340 type
->ref
= s
->type
.ref
;
3341 if (t
& (VT_CONSTANT
| VT_VOLATILE
))
3342 parse_btype_qualify(type
, t
& (VT_CONSTANT
| VT_VOLATILE
));
3346 /* get attributes from typedef */
3347 if (0 == ad
->a
.aligned
)
3348 ad
->a
.aligned
= s
->a
.aligned
;
3349 if (0 == ad
->a
.func_call
)
3350 ad
->a
.func_call
= s
->a
.func_call
;
3351 ad
->a
.packed
|= s
->a
.packed
;
3360 if (tcc_state
->char_is_unsigned
) {
3361 if ((t
& (VT_DEFSIGN
|VT_BTYPE
)) == VT_BYTE
)
3365 /* long is never used as type */
3366 if ((t
& VT_BTYPE
) == VT_LONG
)
3367 #if (!defined TCC_TARGET_X86_64 && !defined TCC_TARGET_ARM64) || \
3368 defined TCC_TARGET_PE
3369 t
= (t
& ~VT_BTYPE
) | VT_INT
;
3371 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3377 /* convert a function parameter type (array to pointer and function to
3378 function pointer) */
3379 static inline void convert_parameter_type(CType
*pt
)
3381 /* remove const and volatile qualifiers (XXX: const could be used
3382 to indicate a const function parameter */
3383 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3384 /* array must be transformed to pointer according to ANSI C */
3386 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
3391 ST_FUNC
void parse_asm_str(CString
*astr
)
3394 /* read the string */
3396 expect("string constant");
3398 while (tok
== TOK_STR
) {
3399 /* XXX: add \0 handling too ? */
3400 cstr_cat(astr
, tokc
.str
.data
, -1);
3403 cstr_ccat(astr
, '\0');
3406 /* Parse an asm label and return the token */
3407 static int asm_label_instr(void)
3413 parse_asm_str(&astr
);
3416 printf("asm_alias: \"%s\"\n", (char *)astr
.data
);
3418 v
= tok_alloc(astr
.data
, astr
.size
- 1)->tok
;
3423 static void post_type(CType
*type
, AttributeDef
*ad
)
3425 int n
, l
, t1
, arg_size
, align
;
3426 Sym
**plast
, *s
, *first
;
3431 /* function declaration */
3439 /* read param name and compute offset */
3440 if (l
!= FUNC_OLD
) {
3441 if (!parse_btype(&pt
, &ad1
)) {
3443 tcc_error("invalid type");
3450 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
3452 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3453 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
3454 tcc_error("parameter declared as void");
3455 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
3460 expect("identifier");
3464 convert_parameter_type(&pt
);
3465 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
3471 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
3478 /* if no parameters, then old type prototype */
3482 /* NOTE: const is ignored in returned type as it has a special
3483 meaning in gcc / C++ */
3484 type
->t
&= ~VT_CONSTANT
;
3485 /* some ancient pre-K&R C allows a function to return an array
3486 and the array brackets to be put after the arguments, such
3487 that "int c()[]" means something like "int[] c()" */
3490 skip(']'); /* only handle simple "[]" */
3493 /* we push a anonymous symbol which will contain the function prototype */
3494 ad
->a
.func_args
= arg_size
;
3495 s
= sym_push(SYM_FIELD
, type
, 0, l
);
3500 } else if (tok
== '[') {
3501 /* array definition */
3503 if (tok
== TOK_RESTRICT1
)
3508 if (!local_stack
|| nocode_wanted
)
3509 vpushi(expr_const());
3511 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3514 tcc_error("invalid array size");
3516 if (!is_integer_btype(vtop
->type
.t
& VT_BTYPE
))
3517 tcc_error("size of variable length array should be an integer");
3522 /* parse next post type */
3523 post_type(type
, ad
);
3524 if (type
->t
== VT_FUNC
)
3525 tcc_error("declaration of an array of functions");
3526 t1
|= type
->t
& VT_VLA
;
3529 loc
-= type_size(&int_type
, &align
);
3533 vla_runtime_type_size(type
, &align
);
3535 vset(&int_type
, VT_LOCAL
|VT_LVAL
, n
);
3542 /* we push an anonymous symbol which will contain the array
3544 s
= sym_push(SYM_FIELD
, type
, 0, n
);
3545 type
->t
= (t1
? VT_VLA
: VT_ARRAY
) | VT_PTR
;
3550 /* Parse a type declaration (except basic type), and return the type
3551 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3552 expected. 'type' should contain the basic type. 'ad' is the
3553 attribute definition of the basic type. It can be modified by
3556 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
3559 CType type1
, *type2
;
3560 int qualifiers
, storage
;
3562 while (tok
== '*') {
3570 qualifiers
|= VT_CONSTANT
;
3575 qualifiers
|= VT_VOLATILE
;
3583 type
->t
|= qualifiers
;
3586 /* XXX: clarify attribute handling */
3587 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3588 parse_attribute(ad
);
3590 /* recursive type */
3591 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3592 type1
.t
= 0; /* XXX: same as int */
3595 /* XXX: this is not correct to modify 'ad' at this point, but
3596 the syntax is not clear */
3597 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3598 parse_attribute(ad
);
3599 type_decl(&type1
, ad
, v
, td
);
3602 /* type identifier */
3603 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
3607 if (!(td
& TYPE_ABSTRACT
))
3608 expect("identifier");
3612 storage
= type
->t
& VT_STORAGE
;
3613 type
->t
&= ~VT_STORAGE
;
3614 if (storage
& VT_STATIC
) {
3615 int saved_nocode_wanted
= nocode_wanted
;
3617 post_type(type
, ad
);
3618 nocode_wanted
= saved_nocode_wanted
;
3620 post_type(type
, ad
);
3622 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3623 parse_attribute(ad
);
3627 /* append type at the end of type1 */
3640 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3641 ST_FUNC
int lvalue_type(int t
)
3646 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
3648 else if (bt
== VT_SHORT
)
3652 if (t
& VT_UNSIGNED
)
3653 r
|= VT_LVAL_UNSIGNED
;
3657 /* indirection with full error checking and bound check */
3658 ST_FUNC
void indir(void)
3660 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
3661 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
3665 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
3667 vtop
->type
= *pointed_type(&vtop
->type
);
3668 /* Arrays and functions are never lvalues */
3669 if (!(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_VLA
)
3670 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3671 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3672 /* if bound checking, the referenced pointer must be checked */
3673 #ifdef CONFIG_TCC_BCHECK
3674 if (tcc_state
->do_bounds_check
)
3675 vtop
->r
|= VT_MUSTBOUND
;
3680 /* pass a parameter to a function and do type checking and casting */
3681 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
3686 func_type
= func
->c
;
3687 if (func_type
== FUNC_OLD
||
3688 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
3689 /* default casting : only need to convert float to double */
3690 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
3693 } else if (vtop
->type
.t
& VT_BITFIELD
) {
3694 type
.t
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
3697 } else if (arg
== NULL
) {
3698 tcc_error("too many arguments to function");
3701 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
3702 gen_assign_cast(&type
);
3706 /* parse an expression of the form '(type)' or '(expr)' and return its
3708 static void parse_expr_type(CType
*type
)
3714 if (parse_btype(type
, &ad
)) {
3715 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3722 static void parse_type(CType
*type
)
3727 if (!parse_btype(type
, &ad
)) {
3730 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3733 static void vpush_tokc(int t
)
3738 vsetc(&type
, VT_CONST
, &tokc
);
3741 ST_FUNC
void unary(void)
3743 int n
, t
, align
, size
, r
, sizeof_caller
;
3747 static int in_sizeof
= 0;
3749 sizeof_caller
= in_sizeof
;
3751 /* XXX: GCC 2.95.3 does not generate a table although it should be
3765 vpush_tokc(VT_INT
| VT_UNSIGNED
);
3769 vpush_tokc(VT_LLONG
);
3773 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
3777 vpush_tokc(VT_FLOAT
);
3781 vpush_tokc(VT_DOUBLE
);
3785 vpush_tokc(VT_LDOUBLE
);
3788 case TOK___FUNCTION__
:
3790 goto tok_identifier
;
3796 /* special function name identifier */
3797 len
= strlen(funcname
) + 1;
3798 /* generate char[len] type */
3803 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
3804 ptr
= section_ptr_add(data_section
, len
);
3805 memcpy(ptr
, funcname
, len
);
3810 #ifdef TCC_TARGET_PE
3811 t
= VT_SHORT
| VT_UNSIGNED
;
3817 /* string parsing */
3820 if (tcc_state
->warn_write_strings
)
3825 memset(&ad
, 0, sizeof(AttributeDef
));
3826 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, 0);
3831 if (parse_btype(&type
, &ad
)) {
3832 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
3834 /* check ISOC99 compound literal */
3836 /* data is allocated locally by default */
3841 /* all except arrays are lvalues */
3842 if (!(type
.t
& VT_ARRAY
))
3843 r
|= lvalue_type(type
.t
);
3844 memset(&ad
, 0, sizeof(AttributeDef
));
3845 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, 0);
3847 if (sizeof_caller
) {
3854 } else if (tok
== '{') {
3856 tcc_error("expected constant");
3857 /* save all registers */
3860 /* statement expression : we do not accept break/continue
3861 inside as GCC does */
3862 block(NULL
, NULL
, NULL
, NULL
, 0, 1);
3877 /* functions names must be treated as function pointers,
3878 except for unary '&' and sizeof. Since we consider that
3879 functions are not lvalues, we only have to handle it
3880 there and in function calls. */
3881 /* arrays can also be used although they are not lvalues */
3882 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
3883 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
3885 mk_pointer(&vtop
->type
);
3891 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3893 boolean
.t
= VT_BOOL
;
3895 vtop
->c
.i
= !vtop
->c
.i
;
3896 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
3900 vseti(VT_JMP
, gvtst(1, 0));
3912 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
3913 tcc_error("pointer not accepted for unary plus");
3914 /* In order to force cast, we add zero, except for floating point
3915 where we really need an noop (otherwise -0.0 will be transformed
3917 if (!is_float(vtop
->type
.t
)) {
3928 unary_type(&type
); // Perform a in_sizeof = 0;
3929 size
= type_size(&type
, &align
);
3930 if (t
== TOK_SIZEOF
) {
3931 if (!(type
.t
& VT_VLA
)) {
3933 tcc_error("sizeof applied to an incomplete type");
3936 vla_runtime_type_size(&type
, &align
);
3941 vtop
->type
.t
|= VT_UNSIGNED
;
3944 case TOK_builtin_expect
:
3946 /* __builtin_expect is a no-op for now */
3947 int saved_nocode_wanted
;
3952 saved_nocode_wanted
= nocode_wanted
;
3956 nocode_wanted
= saved_nocode_wanted
;
3960 case TOK_builtin_types_compatible_p
:
3969 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3970 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3971 vpushi(is_compatible_types(&type1
, &type2
));
3974 case TOK_builtin_constant_p
:
3976 int saved_nocode_wanted
, res
;
3979 saved_nocode_wanted
= nocode_wanted
;
3982 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
3984 nocode_wanted
= saved_nocode_wanted
;
3989 case TOK_builtin_frame_address
:
3990 case TOK_builtin_return_address
:
3997 if (tok
!= TOK_CINT
) {
3998 tcc_error("%s only takes positive integers",
3999 tok1
== TOK_builtin_return_address
?
4000 "__builtin_return_address" :
4001 "__builtin_frame_address");
4003 level
= (uint32_t)tokc
.i
;
4008 vset(&type
, VT_LOCAL
, 0); /* local frame */
4010 mk_pointer(&vtop
->type
);
4011 indir(); /* -> parent frame */
4013 if (tok1
== TOK_builtin_return_address
) {
4014 // assume return address is just above frame pointer on stack
4017 mk_pointer(&vtop
->type
);
4022 #ifdef TCC_TARGET_X86_64
4023 #ifdef TCC_TARGET_PE
4024 case TOK_builtin_va_start
:
4032 if ((vtop
->r
& VT_VALMASK
) != VT_LOCAL
)
4033 tcc_error("__builtin_va_start expects a local variable");
4034 vtop
->r
&= ~(VT_LVAL
| VT_REF
);
4035 vtop
->type
= char_pointer_type
;
4041 case TOK_builtin_va_arg_types
:
4048 vpushi(classify_x86_64_va_arg(&type
));
4054 #ifdef TCC_TARGET_ARM64
4055 case TOK___va_start
: {
4057 tcc_error("statement in global scope");
4067 vtop
->type
.t
= VT_VOID
;
4070 case TOK___va_arg
: {
4073 tcc_error("statement in global scope");
4085 case TOK___arm64_clear_cache
: {
4094 vtop
->type
.t
= VT_VOID
;
4098 /* pre operations */
4109 t
= vtop
->type
.t
& VT_BTYPE
;
4111 /* In IEEE negate(x) isn't subtract(0,x), but rather
4116 else if (t
== VT_DOUBLE
)
4127 goto tok_identifier
;
4129 /* allow to take the address of a label */
4130 if (tok
< TOK_UIDENT
)
4131 expect("label identifier");
4132 s
= label_find(tok
);
4134 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4136 if (s
->r
== LABEL_DECLARED
)
4137 s
->r
= LABEL_FORWARD
;
4140 s
->type
.t
= VT_VOID
;
4141 mk_pointer(&s
->type
);
4142 s
->type
.t
|= VT_STATIC
;
4144 vpushsym(&s
->type
, s
);
4148 // special qnan , snan and infinity values
4150 vpush64(VT_DOUBLE
, 0x7ff8000000000000ULL
);
4154 vpush64(VT_DOUBLE
, 0x7ff0000000000001ULL
);
4158 vpush64(VT_DOUBLE
, 0x7ff0000000000000ULL
);
4167 expect("identifier");
4170 const char *name
= get_tok_str(t
, NULL
);
4172 tcc_error("'%s' undeclared", name
);
4173 /* for simple function calls, we tolerate undeclared
4174 external reference to int() function */
4175 if (tcc_state
->warn_implicit_function_declaration
4176 #ifdef TCC_TARGET_PE
4177 /* people must be warned about using undeclared WINAPI functions
4178 (which usually start with uppercase letter) */
4179 || (name
[0] >= 'A' && name
[0] <= 'Z')
4182 tcc_warning("implicit declaration of function '%s'", name
);
4183 s
= external_global_sym(t
, &func_old_type
, 0);
4185 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
4186 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
4187 /* if referencing an inline function, then we generate a
4188 symbol to it if not already done. It will have the
4189 effect to generate code for it at the end of the
4190 compilation unit. Inline function as always
4191 generated in the text section. */
4193 put_extern_sym(s
, text_section
, 0, 0);
4194 r
= VT_SYM
| VT_CONST
;
4198 vset(&s
->type
, r
, s
->c
);
4199 /* if forward reference, we must point to s */
4200 if (vtop
->r
& VT_SYM
) {
4207 /* post operations */
4209 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
4212 } else if (tok
== '.' || tok
== TOK_ARROW
|| tok
== TOK_CDOUBLE
) {
4215 if (tok
== TOK_ARROW
)
4217 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
4220 /* expect pointer on structure */
4221 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
4222 expect("struct or union");
4223 if (tok
== TOK_CDOUBLE
)
4224 expect("field name");
4226 if (tok
== TOK_CINT
|| tok
== TOK_CUINT
)
4227 expect("field name");
4231 while ((s
= s
->next
) != NULL
) {
4236 tcc_error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, &tokc
));
4237 /* add field offset to pointer */
4238 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
4241 /* change type to field type, and set to lvalue */
4242 vtop
->type
= s
->type
;
4243 vtop
->type
.t
|= qualifiers
;
4244 /* an array is never an lvalue */
4245 if (!(vtop
->type
.t
& VT_ARRAY
)) {
4246 vtop
->r
|= lvalue_type(vtop
->type
.t
);
4247 #ifdef CONFIG_TCC_BCHECK
4248 /* if bound checking, the referenced pointer must be checked */
4249 if (tcc_state
->do_bounds_check
)
4250 vtop
->r
|= VT_MUSTBOUND
;
4254 } else if (tok
== '[') {
4260 } else if (tok
== '(') {
4263 int nb_args
, ret_nregs
, ret_align
, regsize
, variadic
;
4266 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
4267 /* pointer test (no array accepted) */
4268 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
4269 vtop
->type
= *pointed_type(&vtop
->type
);
4270 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
4274 expect("function pointer");
4277 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
4279 /* get return type */
4282 sa
= s
->next
; /* first parameter */
4285 /* compute first implicit argument if a structure is returned */
4286 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
4287 variadic
= (s
->c
== FUNC_ELLIPSIS
);
4288 ret_nregs
= gfunc_sret(&s
->type
, variadic
, &ret
.type
,
4289 &ret_align
, ®size
);
4291 /* get some space for the returned structure */
4292 size
= type_size(&s
->type
, &align
);
4293 #ifdef TCC_TARGET_ARM64
4294 /* On arm64, a small struct is return in registers.
4295 It is much easier to write it to memory if we know
4296 that we are allowed to write some extra bytes, so
4297 round the allocated space up to a power of 2: */
4299 while (size
& (size
- 1))
4300 size
= (size
| (size
- 1)) + 1;
4302 loc
= (loc
- size
) & -align
;
4304 ret
.r
= VT_LOCAL
| VT_LVAL
;
4305 /* pass it as 'int' to avoid structure arg passing
4307 vseti(VT_LOCAL
, loc
);
4317 /* return in register */
4318 if (is_float(ret
.type
.t
)) {
4319 ret
.r
= reg_fret(ret
.type
.t
);
4320 #ifdef TCC_TARGET_X86_64
4321 if ((ret
.type
.t
& VT_BTYPE
) == VT_QFLOAT
)
4325 #ifndef TCC_TARGET_ARM64
4326 #ifdef TCC_TARGET_X86_64
4327 if ((ret
.type
.t
& VT_BTYPE
) == VT_QLONG
)
4329 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
4340 gfunc_param_typed(s
, sa
);
4350 tcc_error("too few arguments to function");
4352 if (!nocode_wanted
) {
4353 gfunc_call(nb_args
);
4355 vtop
-= (nb_args
+ 1);
4359 for (r
= ret
.r
+ ret_nregs
+ !ret_nregs
; r
-- > ret
.r
;) {
4360 vsetc(&ret
.type
, r
, &ret
.c
);
4361 vtop
->r2
= ret
.r2
; /* Loop only happens when r2 is VT_CONST */
4364 /* handle packed struct return */
4365 if (((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) && ret_nregs
) {
4368 size
= type_size(&s
->type
, &align
);
4369 /* We're writing whole regs often, make sure there's enough
4370 space. Assume register size is power of 2. */
4371 if (regsize
> align
)
4373 loc
= (loc
- size
) & -align
;
4377 vset(&ret
.type
, VT_LOCAL
| VT_LVAL
, addr
+ offset
);
4381 if (--ret_nregs
== 0)
4385 vset(&s
->type
, VT_LOCAL
| VT_LVAL
, addr
);
4393 ST_FUNC
void expr_prod(void)
4398 while (tok
== '*' || tok
== '/' || tok
== '%') {
4406 ST_FUNC
void expr_sum(void)
4411 while (tok
== '+' || tok
== '-') {
4419 static void expr_shift(void)
4424 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
4432 static void expr_cmp(void)
4437 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
4438 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
4446 static void expr_cmpeq(void)
4451 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
4459 static void expr_and(void)
4462 while (tok
== '&') {
4469 static void expr_xor(void)
4472 while (tok
== '^') {
4479 static void expr_or(void)
4482 while (tok
== '|') {
4489 /* XXX: fix this mess */
4490 static void expr_land_const(void)
4493 while (tok
== TOK_LAND
) {
4499 static void expr_lor_const(void)
4502 while (tok
== TOK_LOR
) {
4509 static void expr_land(void)
4512 if (tok
== TOK_LAND
) {
4513 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4524 int saved_nocode_wanted
= nocode_wanted
;
4528 nocode_wanted
= saved_nocode_wanted
;
4536 if (tok
!= TOK_LAND
) {
4547 static void expr_lor(void)
4550 if (tok
== TOK_LOR
) {
4551 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4558 int saved_nocode_wanted
= nocode_wanted
;
4562 nocode_wanted
= saved_nocode_wanted
;
4574 if (tok
!= TOK_LOR
) {
4585 static void expr_cond(void)
4587 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
, islv
;
4589 CType type
, type1
, type2
;
4594 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4595 int saved_nocode_wanted
= nocode_wanted
;
4598 boolean
.t
= VT_BOOL
;
4604 if (tok
!= ':' || !gnu_ext
) {
4612 nocode_wanted
= saved_nocode_wanted
;
4615 if (tok
!= ':' || !gnu_ext
) {
4619 nocode_wanted
= saved_nocode_wanted
;
4626 if (vtop
!= vstack
) {
4627 /* needed to avoid having different registers saved in
4629 if (is_float(vtop
->type
.t
)) {
4631 #ifdef TCC_TARGET_X86_64
4632 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4642 if (tok
== ':' && gnu_ext
) {
4650 sv
= *vtop
; /* save value to handle it later */
4651 vtop
--; /* no vpop so that FP stack is not flushed */
4659 bt1
= t1
& VT_BTYPE
;
4661 bt2
= t2
& VT_BTYPE
;
4662 /* cast operands to correct type according to ISOC rules */
4663 if (is_float(bt1
) || is_float(bt2
)) {
4664 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
4665 type
.t
= VT_LDOUBLE
;
4666 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
4671 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
4672 /* cast to biggest op */
4674 /* convert to unsigned if it does not fit in a long long */
4675 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
4676 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
4677 type
.t
|= VT_UNSIGNED
;
4678 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
4679 /* If one is a null ptr constant the result type
4681 if (is_null_pointer (vtop
))
4683 else if (is_null_pointer (&sv
))
4685 /* XXX: test pointer compatibility, C99 has more elaborate
4689 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
4690 /* XXX: test function pointer compatibility */
4691 type
= bt1
== VT_FUNC
? type1
: type2
;
4692 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
4693 /* XXX: test structure compatibility */
4694 type
= bt1
== VT_STRUCT
? type1
: type2
;
4695 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
4696 /* NOTE: as an extension, we accept void on only one side */
4699 /* integer operations */
4701 /* convert to unsigned if it does not fit in an integer */
4702 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
4703 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
4704 type
.t
|= VT_UNSIGNED
;
4706 /* keep structs lvalue by transforming `(expr ? a : b)` to `*(expr ? &a : &b)` so
4707 that `(expr ? a : b).mem` does not error with "lvalue expected" */
4708 islv
= (vtop
->r
& VT_LVAL
) && (sv
.r
& VT_LVAL
) && VT_STRUCT
== (type
.t
& VT_BTYPE
);
4710 /* now we convert second operand */
4713 mk_pointer(&vtop
->type
);
4716 else if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4719 if (is_float(type
.t
)) {
4721 #ifdef TCC_TARGET_X86_64
4722 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4726 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
4727 /* for long longs, we use fixed registers to avoid having
4728 to handle a complicated move */
4733 /* this is horrible, but we must also convert first
4737 /* put again first value and cast it */
4741 mk_pointer(&vtop
->type
);
4744 else if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4747 move_reg(r2
, r1
, type
.t
);
4756 static void expr_eq(void)
4762 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
4763 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
4764 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
4779 ST_FUNC
void gexpr(void)
4790 /* parse an expression and return its type without any side effect. */
4791 static void expr_type(CType
*type
)
4793 int saved_nocode_wanted
;
4795 saved_nocode_wanted
= nocode_wanted
;
4800 nocode_wanted
= saved_nocode_wanted
;
4803 /* parse a unary expression and return its type without any side
4805 static void unary_type(CType
*type
)
4817 /* parse a constant expression and return value in vtop. */
4818 static void expr_const1(void)
4827 /* parse an integer constant and return its value. */
4828 ST_FUNC
int expr_const(void)
4832 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
4833 expect("constant expression");
4839 /* return the label token if current token is a label, otherwise
4841 static int is_label(void)
4845 /* fast test first */
4846 if (tok
< TOK_UIDENT
)
4848 /* no need to save tokc because tok is an identifier */
4855 unget_tok(last_tok
);
4860 static void label_or_decl(int l
)
4864 /* fast test first */
4865 if (tok
>= TOK_UIDENT
)
4867 /* no need to save tokc because tok is an identifier */
4871 unget_tok(last_tok
);
4874 unget_tok(last_tok
);
4879 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
4880 int case_reg
, int is_expr
)
4885 /* generate line number info */
4886 if (tcc_state
->do_debug
&&
4887 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
4888 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
4890 last_line_num
= file
->line_num
;
4894 /* default return value is (void) */
4896 vtop
->type
.t
= VT_VOID
;
4899 if (tok
== TOK_IF
) {
4906 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4908 if (c
== TOK_ELSE
) {
4912 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4913 gsym(d
); /* patch else jmp */
4916 } else if (tok
== TOK_WHILE
) {
4926 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4932 } else if (tok
== '{') {
4934 int block_vla_sp_loc
= vla_sp_loc
, saved_vlas_in_scope
= vlas_in_scope
;
4937 /* record local declaration stack position */
4939 llabel
= local_label_stack
;
4942 /* handle local labels declarations */
4943 if (tok
== TOK_LABEL
) {
4946 if (tok
< TOK_UIDENT
)
4947 expect("label identifier");
4948 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
4958 while (tok
!= '}') {
4959 label_or_decl(VT_LOCAL
);
4963 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4966 /* pop locally defined labels */
4967 label_pop(&local_label_stack
, llabel
);
4969 /* XXX: this solution makes only valgrind happy...
4970 triggered by gcc.c-torture/execute/20000917-1.c */
4972 switch(vtop
->type
.t
& VT_BTYPE
) {
4974 /* this breaks a compilation of the linux kernel v2.4.26 */
4975 /* pmd_t *new = ({ __asm__ __volatile__("ud2\n") ; ((pmd_t *)1); }); */
4976 /* Look a commit a80acab: Display error on statement expressions with complex return type */
4977 /* A pointer is not a complex return type */
4981 for(p
=vtop
->type
.ref
;p
;p
=p
->prev
)
4983 tcc_error("unsupported expression type");
4986 /* pop locally defined symbols */
4988 sym_pop(&local_stack
, s
);
4990 /* Pop VLA frames and restore stack pointer if required */
4991 if (vlas_in_scope
> saved_vlas_in_scope
) {
4992 vla_sp_loc
= saved_vlas_in_scope
? block_vla_sp_loc
: vla_sp_root_loc
;
4995 vlas_in_scope
= saved_vlas_in_scope
;
4998 } else if (tok
== TOK_RETURN
) {
5002 gen_assign_cast(&func_vt
);
5003 #ifdef TCC_TARGET_ARM64
5004 // Perhaps it would be better to use this for all backends:
5007 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
5008 CType type
, ret_type
;
5009 int ret_align
, ret_nregs
, regsize
;
5010 ret_nregs
= gfunc_sret(&func_vt
, func_var
, &ret_type
,
5011 &ret_align
, ®size
);
5012 if (0 == ret_nregs
) {
5013 /* if returning structure, must copy it to implicit
5014 first pointer arg location */
5017 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
5020 /* copy structure value to pointer */
5023 /* returning structure packed into registers */
5024 int r
, size
, addr
, align
;
5025 size
= type_size(&func_vt
,&align
);
5026 if ((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) ||
5027 (vtop
->c
.i
& (ret_align
-1)))
5028 && (align
& (ret_align
-1))) {
5029 loc
= (loc
- size
) & -ret_align
;
5032 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
5036 vset(&ret_type
, VT_LOCAL
| VT_LVAL
, addr
);
5038 vtop
->type
= ret_type
;
5039 if (is_float(ret_type
.t
))
5040 r
= rc_fret(ret_type
.t
);
5046 if (--ret_nregs
== 0)
5048 /* We assume that when a structure is returned in multiple
5049 registers, their classes are consecutive values of the
5052 vtop
->c
.i
+= regsize
;
5053 vtop
->r
= VT_LOCAL
| VT_LVAL
;
5056 } else if (is_float(func_vt
.t
)) {
5057 gv(rc_fret(func_vt
.t
));
5062 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
5065 /* jump unless last stmt in top-level block */
5066 if (tok
!= '}' || local_scope
!= 1)
5068 } else if (tok
== TOK_BREAK
) {
5071 tcc_error("cannot break");
5072 *bsym
= gjmp(*bsym
);
5075 } else if (tok
== TOK_CONTINUE
) {
5078 tcc_error("cannot continue");
5079 vla_sp_restore_root();
5080 *csym
= gjmp(*csym
);
5083 } else if (tok
== TOK_FOR
) {
5090 /* c99 for-loop init decl? */
5091 if (!decl0(VT_LOCAL
, 1)) {
5092 /* no, regular for-loop init expr */
5118 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
5124 sym_pop(&local_stack
, s
);
5127 if (tok
== TOK_DO
) {
5133 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
5144 if (tok
== TOK_SWITCH
) {
5148 /* XXX: other types than integer */
5149 case_reg
= gv(RC_INT
);
5153 b
= gjmp(0); /* jump to first case */
5155 block(&a
, csym
, &b
, &c
, case_reg
, 0);
5156 /* if no default, jmp after switch */
5164 if (tok
== TOK_CASE
) {
5171 if (gnu_ext
&& tok
== TOK_DOTS
) {
5175 tcc_warning("empty case range");
5177 /* since a case is like a label, we must skip it with a jmp */
5185 *case_sym
= gtst(1, 0);
5188 *case_sym
= gtst(1, 0);
5192 *case_sym
= gtst(1, *case_sym
);
5194 case_reg
= gv(RC_INT
);
5199 goto block_after_label
;
5201 if (tok
== TOK_DEFAULT
) {
5207 tcc_error("too many 'default'");
5210 goto block_after_label
;
5212 if (tok
== TOK_GOTO
) {
5214 if (tok
== '*' && gnu_ext
) {
5218 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
5221 } else if (tok
>= TOK_UIDENT
) {
5222 s
= label_find(tok
);
5223 /* put forward definition if needed */
5225 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
5227 if (s
->r
== LABEL_DECLARED
)
5228 s
->r
= LABEL_FORWARD
;
5230 vla_sp_restore_root();
5231 if (s
->r
& LABEL_FORWARD
)
5232 s
->jnext
= gjmp(s
->jnext
);
5234 gjmp_addr(s
->jnext
);
5237 expect("label identifier");
5240 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
5248 if (s
->r
== LABEL_DEFINED
)
5249 tcc_error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
5251 s
->r
= LABEL_DEFINED
;
5253 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
5257 /* we accept this, but it is a mistake */
5260 tcc_warning("deprecated use of label at end of compound statement");
5264 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
5267 /* expression case */
5282 /* t is the array or struct type. c is the array or struct
5283 address. cur_index/cur_field is the pointer to the current
5284 value. 'size_only' is true if only size info is needed (only used
5286 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
5287 int *cur_index
, Sym
**cur_field
,
5291 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
5297 if (gnu_ext
&& (l
= is_label()) != 0)
5299 while (tok
== '[' || tok
== '.') {
5301 if (!(type
->t
& VT_ARRAY
))
5302 expect("array type");
5305 index
= expr_const();
5306 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
5307 expect("invalid index");
5308 if (tok
== TOK_DOTS
&& gnu_ext
) {
5310 index_last
= expr_const();
5311 if (index_last
< 0 ||
5312 (s
->c
>= 0 && index_last
>= s
->c
) ||
5314 expect("invalid index");
5320 *cur_index
= index_last
;
5321 type
= pointed_type(type
);
5322 elem_size
= type_size(type
, &align
);
5323 c
+= index
* elem_size
;
5324 /* NOTE: we only support ranges for last designator */
5325 nb_elems
= index_last
- index
+ 1;
5326 if (nb_elems
!= 1) {
5335 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
5336 expect("struct/union type");
5349 /* XXX: fix this mess by using explicit storage field */
5351 type1
.t
|= (type
->t
& ~VT_TYPE
);
5365 if (type
->t
& VT_ARRAY
) {
5367 type
= pointed_type(type
);
5368 c
+= index
* type_size(type
, &align
);
5372 tcc_error("too many field init");
5373 /* XXX: fix this mess by using explicit storage field */
5375 type1
.t
|= (type
->t
& ~VT_TYPE
);
5380 decl_initializer(type
, sec
, c
, 0, size_only
);
5382 /* XXX: make it more general */
5383 if (!size_only
&& nb_elems
> 1) {
5384 unsigned long c_end
;
5389 tcc_error("range init not supported yet for dynamic storage");
5390 c_end
= c
+ nb_elems
* elem_size
;
5391 if (c_end
> sec
->data_allocated
)
5392 section_realloc(sec
, c_end
);
5393 src
= sec
->data
+ c
;
5395 for(i
= 1; i
< nb_elems
; i
++) {
5397 memcpy(dst
, src
, elem_size
);
5403 #define EXPR_CONST 1
5406 /* store a value or an expression directly in global data or in local array */
5407 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
5408 int v
, int expr_type
)
5410 int saved_global_expr
, bt
, bit_pos
, bit_size
;
5412 unsigned long long bit_mask
;
5420 /* compound literals must be allocated globally in this case */
5421 saved_global_expr
= global_expr
;
5424 global_expr
= saved_global_expr
;
5425 /* NOTE: symbols are accepted */
5426 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
5427 tcc_error("initializer element is not constant");
5435 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
5438 /* XXX: not portable */
5439 /* XXX: generate error if incorrect relocation */
5440 gen_assign_cast(&dtype
);
5441 bt
= type
->t
& VT_BTYPE
;
5442 /* we'll write at most 16 bytes */
5443 if (c
+ 16 > sec
->data_allocated
) {
5444 section_realloc(sec
, c
+ 16);
5446 ptr
= sec
->data
+ c
;
5447 /* XXX: make code faster ? */
5448 if (!(type
->t
& VT_BITFIELD
)) {
5453 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5454 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
5455 bit_mask
= (1LL << bit_size
) - 1;
5457 if ((vtop
->r
& VT_SYM
) &&
5463 (bt
== VT_INT
&& bit_size
!= 32)))
5464 tcc_error("initializer element is not computable at load time");
5466 /* XXX: when cross-compiling we assume that each type has the
5467 same representation on host and target, which is likely to
5468 be wrong in the case of long double */
5470 vtop
->c
.i
= (vtop
->c
.i
!= 0);
5472 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5475 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5478 *(double *)ptr
= vtop
->c
.d
;
5481 *(long double *)ptr
= vtop
->c
.ld
;
5484 *(long long *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5487 addr_t val
= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5488 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
5489 if (vtop
->r
& VT_SYM
)
5490 greloca(sec
, vtop
->sym
, c
, R_DATA_PTR
, val
);
5492 *(addr_t
*)ptr
|= val
;
5494 if (vtop
->r
& VT_SYM
)
5495 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
5496 *(addr_t
*)ptr
|= val
;
5501 int val
= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5502 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
5503 if (vtop
->r
& VT_SYM
)
5504 greloca(sec
, vtop
->sym
, c
, R_DATA_PTR
, val
);
5508 if (vtop
->r
& VT_SYM
)
5509 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
5517 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
5524 /* put zeros for variable based init */
5525 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
5528 /* nothing to do because globals are already set to zero */
5530 vpush_global_sym(&func_old_type
, TOK_memset
);
5532 #ifdef TCC_TARGET_ARM
5543 /* 't' contains the type and storage info. 'c' is the offset of the
5544 object in section 'sec'. If 'sec' is NULL, it means stack based
5545 allocation. 'first' is true if array '{' must be read (multi
5546 dimension implicit array init handling). 'size_only' is true if
5547 size only evaluation is wanted (only for arrays). */
5548 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
5549 int first
, int size_only
)
5551 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, parlevel1
, i
;
5552 int size1
, align1
, expr_type
;
5556 if (type
->t
& VT_VLA
) {
5559 /* save current stack pointer */
5560 if (vlas_in_scope
== 0) {
5561 if (vla_sp_root_loc
== -1)
5562 vla_sp_root_loc
= (loc
-= PTR_SIZE
);
5563 gen_vla_sp_save(vla_sp_root_loc
);
5566 vla_runtime_type_size(type
, &a
);
5567 gen_vla_alloc(type
, a
);
5571 } else if (type
->t
& VT_ARRAY
) {
5575 t1
= pointed_type(type
);
5576 size1
= type_size(t1
, &align1
);
5579 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
5582 tcc_error("character array initializer must be a literal,"
5583 " optionally enclosed in braces");
5588 /* only parse strings here if correct type (otherwise: handle
5589 them as ((w)char *) expressions */
5590 if ((tok
== TOK_LSTR
&&
5591 #ifdef TCC_TARGET_PE
5592 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
5594 (t1
->t
& VT_BTYPE
) == VT_INT
5596 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
5597 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5600 /* compute maximum number of chars wanted */
5602 cstr_len
= tokc
.str
.size
;
5604 cstr_len
= tokc
.str
.size
/ sizeof(nwchar_t
);
5607 if (n
>= 0 && nb
> (n
- array_length
))
5608 nb
= n
- array_length
;
5611 tcc_warning("initializer-string for array is too long");
5612 /* in order to go faster for common case (char
5613 string in global variable, we handle it
5615 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
5616 memcpy(sec
->data
+ c
+ array_length
, tokc
.str
.data
, nb
);
5620 ch
= ((unsigned char *)tokc
.str
.data
)[i
];
5622 ch
= ((nwchar_t
*)tokc
.str
.data
)[i
];
5623 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
5631 /* only add trailing zero if enough storage (no
5632 warning in this case since it is standard) */
5633 if (n
< 0 || array_length
< n
) {
5635 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
5641 while (tok
!= '}') {
5642 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
5643 if (n
>= 0 && index
>= n
)
5644 tcc_error("index too large");
5645 /* must put zero in holes (note that doing it that way
5646 ensures that it even works with designators) */
5647 if (!size_only
&& array_length
< index
) {
5648 init_putz(t1
, sec
, c
+ array_length
* size1
,
5649 (index
- array_length
) * size1
);
5652 if (index
> array_length
)
5653 array_length
= index
;
5654 /* special test for multi dimensional arrays (may not
5655 be strictly correct if designators are used at the
5657 if (index
>= n
&& no_oblock
)
5666 /* put zeros at the end */
5667 if (!size_only
&& n
>= 0 && array_length
< n
) {
5668 init_putz(t1
, sec
, c
+ array_length
* size1
,
5669 (n
- array_length
) * size1
);
5671 /* patch type size if needed */
5673 s
->c
= array_length
;
5674 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
5675 (sec
|| !first
|| tok
== '{')) {
5677 /* NOTE: the previous test is a specific case for automatic
5678 struct/union init */
5679 /* XXX: union needs only one init */
5686 if (tcc_state
->old_struct_init_code
) {
5687 /* an old version of struct initialization.
5688 It have a problems. But with a new version
5689 linux 2.4.26 can't load ramdisk.
5691 while (tok
== '(') {
5695 if (!parse_btype(&type1
, &ad1
))
5697 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
5699 if (!is_assignable_types(type
, &type1
))
5700 tcc_error("invalid type for cast");
5707 if (!parse_btype(&type1
, &ad1
))
5709 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
5711 if (!is_assignable_types(type
, &type1
))
5712 tcc_error("invalid type for cast");
5721 if (first
|| tok
== '{') {
5730 while (tok
!= '}') {
5731 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
5733 if (!size_only
&& array_length
< index
) {
5734 init_putz(type
, sec
, c
+ array_length
,
5735 index
- array_length
);
5737 index
= index
+ type_size(&f
->type
, &align1
);
5738 if (index
> array_length
)
5739 array_length
= index
;
5741 /* gr: skip fields from same union - ugly. */
5744 int f_size
= type_size(&f
->type
, &align
);
5745 int f_type
= (f
->type
.t
& VT_BTYPE
);
5747 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
5748 /* test for same offset */
5749 if (f
->next
->c
!= f
->c
)
5751 if ((f_type
== VT_STRUCT
) && (f_size
== 0)) {
5753 Lets assume a structure of size 0 can't be a member of the union.
5754 This allow to compile the following code from a linux kernel v2.4.26
5755 typedef struct { } rwlock_t;
5761 struct fs_struct init_fs = { { (1) }, (rwlock_t) {}, 0022, };
5762 tcc-0.9.23 can succesfully compile this version of the kernel.
5763 gcc don't have problems with this code too.
5767 /* if yes, test for bitfield shift */
5768 if ((f
->type
.t
& VT_BITFIELD
) && (f
->next
->type
.t
& VT_BITFIELD
)) {
5769 int bit_pos_1
= (f
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5770 int bit_pos_2
= (f
->next
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5771 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
5772 if (bit_pos_1
!= bit_pos_2
)
5779 if (no_oblock
&& f
== NULL
)
5785 /* put zeros at the end */
5786 if (!size_only
&& array_length
< n
) {
5787 init_putz(type
, sec
, c
+ array_length
,
5796 } else if (tok
== '{') {
5798 decl_initializer(type
, sec
, c
, first
, size_only
);
5800 } else if (size_only
) {
5801 /* just skip expression */
5802 parlevel
= parlevel1
= 0;
5803 while ((parlevel
> 0 || parlevel1
> 0 ||
5804 (tok
!= '}' && tok
!= ',')) && tok
!= -1) {
5807 else if (tok
== ')') {
5808 if (parlevel
== 0 && parlevel1
== 0)
5812 else if (tok
== '{')
5814 else if (tok
== '}') {
5815 if (parlevel
== 0 && parlevel1
== 0)
5822 /* currently, we always use constant expression for globals
5823 (may change for scripting case) */
5824 expr_type
= EXPR_CONST
;
5826 expr_type
= EXPR_ANY
;
5827 init_putv(type
, sec
, c
, 0, expr_type
);
5831 /* parse an initializer for type 't' if 'has_init' is non zero, and
5832 allocate space in local or global data space ('r' is either
5833 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5834 variable 'v' of scope 'scope' is declared before initializers
5835 are parsed. If 'v' is zero, then a reference to the new object
5836 is put in the value stack. If 'has_init' is 2, a special parsing
5837 is done to handle string constants. */
5838 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
5839 int has_init
, int v
, int scope
)
5841 int size
, align
, addr
, data_offset
;
5843 ParseState saved_parse_state
= {0};
5844 TokenString init_str
;
5846 Sym
*flexible_array
;
5848 flexible_array
= NULL
;
5849 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
5850 Sym
*field
= type
->ref
->next
;
5853 field
= field
->next
;
5854 if (field
->type
.t
& VT_ARRAY
&& field
->type
.ref
->c
< 0)
5855 flexible_array
= field
;
5859 size
= type_size(type
, &align
);
5860 /* If unknown size, we must evaluate it before
5861 evaluating initializers because
5862 initializers can generate global data too
5863 (e.g. string pointers or ISOC99 compound
5864 literals). It also simplifies local
5865 initializers handling */
5866 tok_str_new(&init_str
);
5867 if (size
< 0 || (flexible_array
&& has_init
)) {
5869 tcc_error("unknown type size");
5870 /* get all init string */
5871 if (has_init
== 2) {
5872 /* only get strings */
5873 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5874 tok_str_add_tok(&init_str
);
5879 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
5881 tcc_error("unexpected end of file in initializer");
5882 tok_str_add_tok(&init_str
);
5885 else if (tok
== '}') {
5895 tok_str_add(&init_str
, -1);
5896 tok_str_add(&init_str
, 0);
5899 save_parse_state(&saved_parse_state
);
5901 begin_macro(&init_str
, 0);
5903 decl_initializer(type
, NULL
, 0, 1, 1);
5904 /* prepare second initializer parsing */
5905 macro_ptr
= init_str
.str
;
5908 /* if still unknown size, error */
5909 size
= type_size(type
, &align
);
5911 tcc_error("unknown type size");
5913 /* If there's a flex member and it was used in the initializer
5915 if (flexible_array
&&
5916 flexible_array
->type
.ref
->c
> 0)
5917 size
+= flexible_array
->type
.ref
->c
5918 * pointed_size(&flexible_array
->type
);
5919 /* take into account specified alignment if bigger */
5920 if (ad
->a
.aligned
) {
5921 if (ad
->a
.aligned
> align
)
5922 align
= ad
->a
.aligned
;
5923 } else if (ad
->a
.packed
) {
5926 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
5928 #ifdef CONFIG_TCC_BCHECK
5929 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5933 loc
= (loc
- size
) & -align
;
5935 #ifdef CONFIG_TCC_BCHECK
5936 /* handles bounds */
5937 /* XXX: currently, since we do only one pass, we cannot track
5938 '&' operators, so we add only arrays */
5939 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5941 /* add padding between regions */
5943 /* then add local bound info */
5944 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(addr_t
));
5945 bounds_ptr
[0] = addr
;
5946 bounds_ptr
[1] = size
;
5950 /* local variable */
5951 sym_push(v
, type
, r
, addr
);
5953 /* push local reference */
5954 vset(type
, r
, addr
);
5960 if (v
&& scope
== VT_CONST
) {
5961 /* see if the symbol was already defined */
5964 if (!is_compatible_types(&sym
->type
, type
))
5965 tcc_error("incompatible types for redefinition of '%s'",
5966 get_tok_str(v
, NULL
));
5967 if (sym
->type
.t
& VT_EXTERN
) {
5968 /* if the variable is extern, it was not allocated */
5969 sym
->type
.t
&= ~VT_EXTERN
;
5970 /* set array size if it was omitted in extern
5972 if ((sym
->type
.t
& VT_ARRAY
) &&
5973 sym
->type
.ref
->c
< 0 &&
5975 sym
->type
.ref
->c
= type
->ref
->c
;
5977 /* we accept several definitions of the same
5978 global variable. this is tricky, because we
5979 must play with the SHN_COMMON type of the symbol */
5980 /* XXX: should check if the variable was already
5981 initialized. It is incorrect to initialized it
5983 /* no init data, we won't add more to the symbol */
5990 /* allocate symbol in corresponding section */
5995 else if (tcc_state
->nocommon
)
5999 data_offset
= sec
->data_offset
;
6000 data_offset
= (data_offset
+ align
- 1) & -align
;
6002 /* very important to increment global pointer at this time
6003 because initializers themselves can create new initializers */
6004 data_offset
+= size
;
6005 #ifdef CONFIG_TCC_BCHECK
6006 /* add padding if bound check */
6007 if (tcc_state
->do_bounds_check
)
6010 sec
->data_offset
= data_offset
;
6011 /* allocate section space to put the data */
6012 if (sec
->sh_type
!= SHT_NOBITS
&&
6013 data_offset
> sec
->data_allocated
)
6014 section_realloc(sec
, data_offset
);
6015 /* align section if needed */
6016 if (align
> sec
->sh_addralign
)
6017 sec
->sh_addralign
= align
;
6019 addr
= 0; /* avoid warning */
6023 if (scope
!= VT_CONST
|| !sym
) {
6024 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
6025 sym
->asm_label
= ad
->asm_label
;
6027 /* update symbol definition */
6029 put_extern_sym(sym
, sec
, addr
, size
);
6032 /* put a common area */
6033 put_extern_sym(sym
, NULL
, align
, size
);
6034 /* XXX: find a nicer way */
6035 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
6036 esym
->st_shndx
= SHN_COMMON
;
6039 /* push global reference */
6040 sym
= get_sym_ref(type
, sec
, addr
, size
);
6041 vpushsym(type
, sym
);
6043 /* patch symbol weakness */
6044 if (type
->t
& VT_WEAK
)
6046 apply_visibility(sym
, type
);
6047 #ifdef CONFIG_TCC_BCHECK
6048 /* handles bounds now because the symbol must be defined
6049 before for the relocation */
6050 if (tcc_state
->do_bounds_check
) {
6053 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
6054 /* then add global bound info */
6055 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(addr_t
));
6056 bounds_ptr
[0] = 0; /* relocated */
6057 bounds_ptr
[1] = size
;
6061 if (has_init
|| (type
->t
& VT_VLA
)) {
6062 decl_initializer(type
, sec
, addr
, 1, 0);
6063 /* restore parse state if needed */
6066 restore_parse_state(&saved_parse_state
);
6068 /* patch flexible array member size back to -1, */
6069 /* for possible subsequent similar declarations */
6071 flexible_array
->type
.ref
->c
= -1;
6076 static void put_func_debug(Sym
*sym
)
6081 /* XXX: we put here a dummy type */
6082 snprintf(buf
, sizeof(buf
), "%s:%c1",
6083 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
6084 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
6085 cur_text_section
, sym
->c
);
6086 /* //gr gdb wants a line at the function */
6087 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
6092 /* parse an old style function declaration list */
6093 /* XXX: check multiple parameter */
6094 static void func_decl_list(Sym
*func_sym
)
6101 /* parse each declaration */
6102 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
&&
6103 tok
!= TOK_ASM1
&& tok
!= TOK_ASM2
&& tok
!= TOK_ASM3
) {
6104 if (!parse_btype(&btype
, &ad
))
6105 expect("declaration list");
6106 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
6107 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
6109 /* we accept no variable after */
6113 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
6114 /* find parameter in function parameter list */
6117 if ((s
->v
& ~SYM_FIELD
) == v
)
6121 tcc_error("declaration for parameter '%s' but no such parameter",
6122 get_tok_str(v
, NULL
));
6124 /* check that no storage specifier except 'register' was given */
6125 if (type
.t
& VT_STORAGE
)
6126 tcc_error("storage class specified for '%s'", get_tok_str(v
, NULL
));
6127 convert_parameter_type(&type
);
6128 /* we can add the type (NOTE: it could be local to the function) */
6130 /* accept other parameters */
6141 /* parse a function defined by symbol 'sym' and generate its code in
6142 'cur_text_section' */
6143 static void gen_function(Sym
*sym
)
6145 int saved_nocode_wanted
= nocode_wanted
;
6148 ind
= cur_text_section
->data_offset
;
6149 /* NOTE: we patch the symbol size later */
6150 put_extern_sym(sym
, cur_text_section
, ind
, 0);
6151 funcname
= get_tok_str(sym
->v
, NULL
);
6153 /* Initialize VLA state */
6155 vla_sp_root_loc
= -1;
6156 /* put debug symbol */
6157 if (tcc_state
->do_debug
)
6158 put_func_debug(sym
);
6160 /* push a dummy symbol to enable local sym storage */
6161 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
6162 local_scope
= 1; /* for function parameters */
6163 gfunc_prolog(&sym
->type
);
6166 #ifdef CONFIG_TCC_BCHECK
6167 if (tcc_state
->do_bounds_check
&& !strcmp(funcname
, "main")) {
6170 for (i
= 0, sym
= local_stack
; i
< 2; i
++, sym
= sym
->prev
) {
6171 if (sym
->v
& SYM_FIELD
|| sym
->prev
->v
& SYM_FIELD
)
6173 vpush_global_sym(&func_old_type
, TOK___bound_main_arg
);
6174 vset(&sym
->type
, sym
->r
, sym
->c
);
6180 block(NULL
, NULL
, NULL
, NULL
, 0, 0);
6183 cur_text_section
->data_offset
= ind
;
6184 label_pop(&global_label_stack
, NULL
);
6185 /* reset local stack */
6187 sym_pop(&local_stack
, NULL
);
6188 /* end of function */
6189 /* patch symbol size */
6190 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
6192 /* patch symbol weakness (this definition overrules any prototype) */
6193 if (sym
->type
.t
& VT_WEAK
)
6195 apply_visibility(sym
, &sym
->type
);
6196 if (tcc_state
->do_debug
) {
6197 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
6199 /* It's better to crash than to generate wrong code */
6200 cur_text_section
= NULL
;
6201 funcname
= ""; /* for safety */
6202 func_vt
.t
= VT_VOID
; /* for safety */
6203 func_var
= 0; /* for safety */
6204 ind
= 0; /* for safety */
6205 nocode_wanted
= saved_nocode_wanted
;
6209 ST_FUNC
void gen_inline_functions(void)
6212 int inline_generated
, i
, ln
;
6213 struct InlineFunc
*fn
;
6215 ln
= file
->line_num
;
6216 /* iterate while inline function are referenced */
6218 inline_generated
= 0;
6219 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
6220 fn
= tcc_state
->inline_fns
[i
];
6222 if (sym
&& sym
->c
) {
6223 /* the function was used: generate its code and
6224 convert it to a normal function */
6227 pstrcpy(file
->filename
, sizeof file
->filename
, fn
->filename
);
6228 sym
->r
= VT_SYM
| VT_CONST
;
6229 sym
->type
.t
&= ~VT_INLINE
;
6231 begin_macro(&fn
->func_str
, 0);
6233 cur_text_section
= text_section
;
6237 inline_generated
= 1;
6240 if (!inline_generated
)
6243 file
->line_num
= ln
;
6244 /* free tokens of unused inline functions */
6245 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
6246 fn
= tcc_state
->inline_fns
[i
];
6248 tok_str_free(fn
->func_str
.str
);
6250 dynarray_reset(&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
);
6253 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
6254 static int decl0(int l
, int is_for_loop_init
)
6262 if (!parse_btype(&btype
, &ad
)) {
6263 if (is_for_loop_init
)
6265 /* skip redundant ';' */
6266 /* XXX: find more elegant solution */
6271 if (l
== VT_CONST
&&
6272 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
6273 /* global asm block */
6277 /* special test for old K&R protos without explicit int
6278 type. Only accepted when defining global data */
6279 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
6283 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
6284 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
6286 if ((btype
.t
& VT_BTYPE
) == VT_STRUCT
) {
6287 int v
= btype
.ref
->v
;
6288 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) >= SYM_FIRST_ANOM
)
6289 tcc_warning("unnamed struct/union that defines no instances");
6294 while (1) { /* iterate thru each declaration */
6296 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
6300 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
6301 printf("type = '%s'\n", buf
);
6304 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
6305 if ((type
.t
& VT_STATIC
) && (l
== VT_LOCAL
)) {
6306 tcc_error("function without file scope cannot be static");
6308 /* if old style function prototype, we accept a
6311 if (sym
->c
== FUNC_OLD
)
6312 func_decl_list(sym
);
6315 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
6316 ad
.asm_label
= asm_label_instr();
6317 /* parse one last attribute list, after asm label */
6318 parse_attribute(&ad
);
6325 #ifdef TCC_TARGET_PE
6326 if (ad
.a
.func_import
)
6327 type
.t
|= VT_IMPORT
;
6328 if (ad
.a
.func_export
)
6329 type
.t
|= VT_EXPORT
;
6331 type
.t
|= ad
.a
.visibility
<< VT_VIS_SHIFT
;
6335 tcc_error("cannot use local functions");
6336 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
6337 expect("function definition");
6339 /* reject abstract declarators in function definition */
6341 while ((sym
= sym
->next
) != NULL
)
6342 if (!(sym
->v
& ~SYM_FIELD
))
6343 expect("identifier");
6345 /* XXX: cannot do better now: convert extern line to static inline */
6346 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
6347 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
6352 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
6355 ref
= sym
->type
.ref
;
6356 if (0 == ref
->a
.func_proto
)
6357 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
6359 /* use func_call from prototype if not defined */
6360 if (ref
->a
.func_call
!= FUNC_CDECL
6361 && type
.ref
->a
.func_call
== FUNC_CDECL
)
6362 type
.ref
->a
.func_call
= ref
->a
.func_call
;
6364 /* use export from prototype */
6365 if (ref
->a
.func_export
)
6366 type
.ref
->a
.func_export
= 1;
6368 /* use static from prototype */
6369 if (sym
->type
.t
& VT_STATIC
)
6370 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
6372 /* If the definition has no visibility use the
6373 one from prototype. */
6374 if (! (type
.t
& VT_VIS_MASK
))
6375 type
.t
|= sym
->type
.t
& VT_VIS_MASK
;
6377 if (!is_compatible_types(&sym
->type
, &type
)) {
6379 tcc_error("incompatible types for redefinition of '%s'",
6380 get_tok_str(v
, NULL
));
6382 type
.ref
->a
.func_proto
= 0;
6383 /* if symbol is already defined, then put complete type */
6386 /* put function symbol */
6387 sym
= global_identifier_push(v
, type
.t
, 0);
6388 sym
->type
.ref
= type
.ref
;
6391 /* static inline functions are just recorded as a kind
6392 of macro. Their code will be emitted at the end of
6393 the compilation unit only if they are used */
6394 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
6395 (VT_INLINE
| VT_STATIC
)) {
6397 struct InlineFunc
*fn
;
6398 const char *filename
;
6400 filename
= file
? file
->filename
: "";
6401 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
6402 strcpy(fn
->filename
, filename
);
6404 tok_str_new(&fn
->func_str
);
6410 tcc_error("unexpected end of file");
6411 tok_str_add_tok(&fn
->func_str
);
6416 } else if (t
== '}') {
6418 if (block_level
== 0)
6422 tok_str_add(&fn
->func_str
, -1);
6423 tok_str_add(&fn
->func_str
, 0);
6424 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
6427 /* compute text section */
6428 cur_text_section
= ad
.section
;
6429 if (!cur_text_section
)
6430 cur_text_section
= text_section
;
6431 sym
->r
= VT_SYM
| VT_CONST
;
6436 if (btype
.t
& VT_TYPEDEF
) {
6437 /* save typedefed type */
6438 /* XXX: test storage specifiers ? */
6440 if (sym
&& sym
->scope
== local_scope
) {
6441 if (!is_compatible_types(&sym
->type
, &type
)
6442 || !(sym
->type
.t
& VT_TYPEDEF
))
6443 tcc_error("incompatible redefinition of '%s'",
6444 get_tok_str(v
, NULL
));
6447 sym
= sym_push(v
, &type
, 0, 0);
6450 sym
->type
.t
|= VT_TYPEDEF
;
6453 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
6454 /* external function definition */
6455 /* specific case for func_call attribute */
6456 ad
.a
.func_proto
= 1;
6458 } else if (!(type
.t
& VT_ARRAY
)) {
6459 /* not lvalue if array */
6460 r
|= lvalue_type(type
.t
);
6462 has_init
= (tok
== '=');
6463 if (has_init
&& (type
.t
& VT_VLA
))
6464 tcc_error("Variable length array cannot be initialized");
6465 if ((btype
.t
& VT_EXTERN
) || ((type
.t
& VT_BTYPE
) == VT_FUNC
) ||
6466 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
6467 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
6468 /* external variable or function */
6469 /* NOTE: as GCC, uninitialized global static
6470 arrays of null size are considered as
6472 sym
= external_sym(v
, &type
, r
);
6473 sym
->asm_label
= ad
.asm_label
;
6475 if (ad
.alias_target
) {
6480 alias_target
= sym_find(ad
.alias_target
);
6481 if (!alias_target
|| !alias_target
->c
)
6482 tcc_error("unsupported forward __alias__ attribute");
6483 esym
= &((Elf32_Sym
*)symtab_section
->data
)[alias_target
->c
];
6484 tsec
.sh_num
= esym
->st_shndx
;
6485 put_extern_sym2(sym
, &tsec
, esym
->st_value
, esym
->st_size
, 0);
6488 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
6489 if (type
.t
& VT_STATIC
)
6495 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, l
);
6499 if (is_for_loop_init
)
6512 ST_FUNC
void decl(int l
)