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
;
59 ST_DATA
int vlas_in_scope
; /* number of VLAs that are currently in scope */
60 ST_DATA
int vla_sp_root_loc
; /* vla_sp_loc for SP before any VLAs were pushed */
61 ST_DATA
int vla_sp_loc
; /* Pointer to variable holding location to store stack pointer on the stack when modifying stack pointer */
63 ST_DATA SValue __vstack
[1+VSTACK_SIZE
], *vtop
, *pvtop
;
65 ST_DATA
int const_wanted
; /* true if constant wanted */
66 ST_DATA
int nocode_wanted
; /* true if no code generation wanted for an expression */
67 ST_DATA
int global_expr
; /* true if compound literals must be allocated globally (used during initializers parsing */
68 ST_DATA CType func_vt
; /* current function return type (used by return instruction) */
69 ST_DATA
int func_var
; /* true if current function is variadic (used by return instruction) */
71 ST_DATA
int last_line_num
, last_ind
, func_ind
; /* debug last line number and pc */
72 ST_DATA
const char *funcname
;
74 ST_DATA CType char_pointer_type
, func_old_type
, int_type
, size_type
;
76 ST_DATA
struct switch_t
{
79 } **p
; int n
; /* list of case ranges */
80 int def_sym
; /* default symbol */
81 } *cur_switch
; /* current switch */
83 /* ------------------------------------------------------------------------- */
84 static void gen_cast(CType
*type
);
85 static inline CType
*pointed_type(CType
*type
);
86 static int is_compatible_types(CType
*type1
, CType
*type2
);
87 static int parse_btype(CType
*type
, AttributeDef
*ad
);
88 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
);
89 static void parse_expr_type(CType
*type
);
90 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
, int first
, int size_only
);
91 static void block(int *bsym
, int *csym
, int is_expr
);
92 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
, int has_init
, int v
, int scope
);
93 static int decl0(int l
, int is_for_loop_init
);
94 static void expr_eq(void);
95 static void expr_lor_const(void);
96 static void unary_type(CType
*type
);
97 static void vla_runtime_type_size(CType
*type
, int *a
);
98 static void vla_sp_restore(void);
99 static void vla_sp_restore_root(void);
100 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
);
101 static void expr_type(CType
*type
);
102 ST_FUNC
void vpush64(int ty
, unsigned long long v
);
103 ST_FUNC
void vpush(CType
*type
);
104 ST_FUNC
int gvtst(int inv
, int t
);
105 ST_FUNC
int is_btype_size(int bt
);
107 ST_INLN
int is_float(int t
)
111 return bt
== VT_LDOUBLE
|| bt
== VT_DOUBLE
|| bt
== VT_FLOAT
|| bt
== VT_QFLOAT
;
114 /* we use our own 'finite' function to avoid potential problems with
115 non standard math libs */
116 /* XXX: endianness dependent */
117 ST_FUNC
int ieee_finite(double d
)
120 memcpy(p
, &d
, sizeof(double));
121 return ((unsigned)((p
[1] | 0x800fffff) + 1)) >> 31;
124 ST_FUNC
void test_lvalue(void)
126 if (!(vtop
->r
& VT_LVAL
))
130 ST_FUNC
void check_vstack(void)
133 tcc_error("internal compiler error: vstack leak (%d)", vtop
- pvtop
);
136 /* ------------------------------------------------------------------------- */
137 /* symbol allocator */
138 static Sym
*__sym_malloc(void)
140 Sym
*sym_pool
, *sym
, *last_sym
;
143 sym_pool
= tcc_malloc(SYM_POOL_NB
* sizeof(Sym
));
144 dynarray_add(&sym_pools
, &nb_sym_pools
, sym_pool
);
146 last_sym
= sym_free_first
;
148 for(i
= 0; i
< SYM_POOL_NB
; i
++) {
149 sym
->next
= last_sym
;
153 sym_free_first
= last_sym
;
157 static inline Sym
*sym_malloc(void)
160 sym
= sym_free_first
;
162 sym
= __sym_malloc();
163 sym_free_first
= sym
->next
;
167 ST_INLN
void sym_free(Sym
*sym
)
169 sym
->next
= sym_free_first
;
170 sym_free_first
= sym
;
173 /* push, without hashing */
174 ST_FUNC Sym
*sym_push2(Sym
**ps
, int v
, int t
, long c
)
194 /* find a symbol and return its associated structure. 's' is the top
195 of the symbol stack */
196 ST_FUNC Sym
*sym_find2(Sym
*s
, int v
)
208 /* structure lookup */
209 ST_INLN Sym
*struct_find(int v
)
212 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
214 return table_ident
[v
]->sym_struct
;
217 /* find an identifier */
218 ST_INLN Sym
*sym_find(int v
)
221 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
223 return table_ident
[v
]->sym_identifier
;
226 /* push a given symbol on the symbol stack */
227 ST_FUNC Sym
*sym_push(int v
, CType
*type
, int r
, int c
)
236 s
= sym_push2(ps
, v
, type
->t
, c
);
237 s
->type
.ref
= type
->ref
;
239 /* don't record fields or anonymous symbols */
241 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
242 /* record symbol in token array */
243 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
245 ps
= &ts
->sym_struct
;
247 ps
= &ts
->sym_identifier
;
250 s
->scope
= local_scope
;
251 if (s
->prev_tok
&& s
->prev_tok
->scope
== s
->scope
)
252 tcc_error("redeclaration of '%s'",
253 get_tok_str(v
& ~SYM_STRUCT
, NULL
));
258 /* push a global identifier */
259 ST_FUNC Sym
*global_identifier_push(int v
, int t
, int c
)
262 s
= sym_push2(&global_stack
, v
, t
, c
);
263 /* don't record anonymous symbol */
264 if (v
< SYM_FIRST_ANOM
) {
265 ps
= &table_ident
[v
- TOK_IDENT
]->sym_identifier
;
266 /* modify the top most local identifier, so that
267 sym_identifier will point to 's' when popped */
269 ps
= &(*ps
)->prev_tok
;
276 /* pop symbols until top reaches 'b' */
277 ST_FUNC
void sym_pop(Sym
**ptop
, Sym
*b
)
287 /* remove symbol in token array */
289 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
290 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
292 ps
= &ts
->sym_struct
;
294 ps
= &ts
->sym_identifier
;
303 static void weaken_symbol(Sym
*sym
)
305 sym
->type
.t
|= VT_WEAK
;
310 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
311 esym_type
= ELFW(ST_TYPE
)(esym
->st_info
);
312 esym
->st_info
= ELFW(ST_INFO
)(STB_WEAK
, esym_type
);
316 static void apply_visibility(Sym
*sym
, CType
*type
)
318 int vis
= sym
->type
.t
& VT_VIS_MASK
;
319 int vis2
= type
->t
& VT_VIS_MASK
;
320 if (vis
== (STV_DEFAULT
<< VT_VIS_SHIFT
))
322 else if (vis2
== (STV_DEFAULT
<< VT_VIS_SHIFT
))
325 vis
= (vis
< vis2
) ? vis
: vis2
;
326 sym
->type
.t
&= ~VT_VIS_MASK
;
332 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
333 vis
>>= VT_VIS_SHIFT
;
334 esym
->st_other
= (esym
->st_other
& ~ELFW(ST_VISIBILITY
)(-1)) | vis
;
338 /* ------------------------------------------------------------------------- */
340 ST_FUNC
void swap(int *p
, int *q
)
348 static void vsetc(CType
*type
, int r
, CValue
*vc
)
352 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
353 tcc_error("memory full (vstack)");
354 /* cannot let cpu flags if other instruction are generated. Also
355 avoid leaving VT_JMP anywhere except on the top of the stack
356 because it would complicate the code generator. */
357 if (vtop
>= vstack
) {
358 v
= vtop
->r
& VT_VALMASK
;
359 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
369 /* push constant of type "type" with useless value */
370 ST_FUNC
void vpush(CType
*type
)
373 vsetc(type
, VT_CONST
, &cval
);
376 /* push integer constant */
377 ST_FUNC
void vpushi(int v
)
381 vsetc(&int_type
, VT_CONST
, &cval
);
384 /* push a pointer sized constant */
385 static void vpushs(addr_t v
)
389 vsetc(&size_type
, VT_CONST
, &cval
);
392 /* push arbitrary 64bit constant */
393 ST_FUNC
void vpush64(int ty
, unsigned long long v
)
400 vsetc(&ctype
, VT_CONST
, &cval
);
403 /* push long long constant */
404 static inline void vpushll(long long v
)
406 vpush64(VT_LLONG
, v
);
409 /* push a symbol value of TYPE */
410 static inline void vpushsym(CType
*type
, Sym
*sym
)
414 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
418 /* Return a static symbol pointing to a section */
419 ST_FUNC Sym
*get_sym_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
425 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
426 sym
->type
.ref
= type
->ref
;
427 sym
->r
= VT_CONST
| VT_SYM
;
428 put_extern_sym(sym
, sec
, offset
, size
);
432 /* push a reference to a section offset by adding a dummy symbol */
433 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
435 vpushsym(type
, get_sym_ref(type
, sec
, offset
, size
));
438 /* define a new external reference to a symbol 'v' of type 'u' */
439 ST_FUNC Sym
*external_global_sym(int v
, CType
*type
, int r
)
445 /* push forward reference */
446 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
447 s
->type
.ref
= type
->ref
;
448 s
->r
= r
| VT_CONST
| VT_SYM
;
453 /* define a new external reference to a symbol 'v' */
454 static Sym
*external_sym(int v
, CType
*type
, int r
)
460 /* push forward reference */
461 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
462 s
->type
.t
|= VT_EXTERN
;
463 } else if (s
->type
.ref
== func_old_type
.ref
) {
464 s
->type
.ref
= type
->ref
;
465 s
->r
= r
| VT_CONST
| VT_SYM
;
466 s
->type
.t
|= VT_EXTERN
;
467 } else if (!is_compatible_types(&s
->type
, type
)) {
468 tcc_error("incompatible types for redefinition of '%s'",
469 get_tok_str(v
, NULL
));
471 /* Merge some storage attributes. */
472 if (type
->t
& VT_WEAK
)
475 if (type
->t
& VT_VIS_MASK
)
476 apply_visibility(s
, type
);
481 /* push a reference to global symbol v */
482 ST_FUNC
void vpush_global_sym(CType
*type
, int v
)
484 vpushsym(type
, external_global_sym(v
, type
, 0));
487 ST_FUNC
void vset(CType
*type
, int r
, int v
)
492 vsetc(type
, r
, &cval
);
495 static void vseti(int r
, int v
)
503 ST_FUNC
void vswap(void)
506 /* cannot let cpu flags if other instruction are generated. Also
507 avoid leaving VT_JMP anywhere except on the top of the stack
508 because it would complicate the code generator. */
509 if (vtop
>= vstack
) {
510 int v
= vtop
->r
& VT_VALMASK
;
511 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
518 /* XXX: +2% overall speed possible with optimized memswap
520 * memswap(&vtop[0], &vtop[1], sizeof *vtop);
524 ST_FUNC
void vpushv(SValue
*v
)
526 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
527 tcc_error("memory full (vstack)");
532 static void vdup(void)
537 /* save registers up to (vtop - n) stack entry */
538 ST_FUNC
void save_regs(int n
)
541 for(p
= vstack
, p1
= vtop
- n
; p
<= p1
; p
++)
545 /* save r to the memory stack, and mark it as being free */
546 ST_FUNC
void save_reg(int r
)
548 save_reg_upstack(r
, 0);
551 /* save r to the memory stack, and mark it as being free,
552 if seen up to (vtop - n) stack entry */
553 ST_FUNC
void save_reg_upstack(int r
, int n
)
555 int l
, saved
, size
, align
;
559 if ((r
&= VT_VALMASK
) >= VT_CONST
)
562 /* modify all stack values */
565 for(p
= vstack
, p1
= vtop
- n
; p
<= p1
; p
++) {
566 if ((p
->r
& VT_VALMASK
) == r
||
567 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
568 /* must save value on stack if not already done */
570 /* NOTE: must reload 'r' because r might be equal to r2 */
571 r
= p
->r
& VT_VALMASK
;
572 /* store register in the stack */
574 if ((p
->r
& VT_LVAL
) ||
575 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
576 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
577 type
= &char_pointer_type
;
581 size
= type_size(type
, &align
);
582 loc
= (loc
- size
) & -align
;
584 sv
.r
= VT_LOCAL
| VT_LVAL
;
587 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
588 /* x86 specific: need to pop fp register ST0 if saved */
590 o(0xd8dd); /* fstp %st(0) */
593 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
594 /* special long long case */
595 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
603 /* mark that stack entry as being saved on the stack */
604 if (p
->r
& VT_LVAL
) {
605 /* also clear the bounded flag because the
606 relocation address of the function was stored in
608 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
610 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
618 #ifdef TCC_TARGET_ARM
619 /* find a register of class 'rc2' with at most one reference on stack.
620 * If none, call get_reg(rc) */
621 ST_FUNC
int get_reg_ex(int rc
, int rc2
)
626 for(r
=0;r
<NB_REGS
;r
++) {
627 if (reg_classes
[r
] & rc2
) {
630 for(p
= vstack
; p
<= vtop
; p
++) {
631 if ((p
->r
& VT_VALMASK
) == r
||
632 (p
->r2
& VT_VALMASK
) == r
)
643 /* find a free register of class 'rc'. If none, save one register */
644 ST_FUNC
int get_reg(int rc
)
649 /* find a free register */
650 for(r
=0;r
<NB_REGS
;r
++) {
651 if (reg_classes
[r
] & rc
) {
652 for(p
=vstack
;p
<=vtop
;p
++) {
653 if ((p
->r
& VT_VALMASK
) == r
||
654 (p
->r2
& VT_VALMASK
) == r
)
662 /* no register left : free the first one on the stack (VERY
663 IMPORTANT to start from the bottom to ensure that we don't
664 spill registers used in gen_opi()) */
665 for(p
=vstack
;p
<=vtop
;p
++) {
666 /* look at second register (if long long) */
667 r
= p
->r2
& VT_VALMASK
;
668 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
670 r
= p
->r
& VT_VALMASK
;
671 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
677 /* Should never comes here */
681 /* move register 's' (of type 't') to 'r', and flush previous value of r to memory
683 static void move_reg(int r
, int s
, int t
)
697 /* get address of vtop (vtop MUST BE an lvalue) */
698 ST_FUNC
void gaddrof(void)
700 if (vtop
->r
& VT_REF
&& !nocode_wanted
)
703 /* tricky: if saved lvalue, then we can go back to lvalue */
704 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
705 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
710 #ifdef CONFIG_TCC_BCHECK
711 /* generate lvalue bound code */
712 static void gbound(void)
717 vtop
->r
&= ~VT_MUSTBOUND
;
718 /* if lvalue, then use checking code before dereferencing */
719 if (vtop
->r
& VT_LVAL
) {
720 /* if not VT_BOUNDED value, then make one */
721 if (!(vtop
->r
& VT_BOUNDED
)) {
722 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
723 /* must save type because we must set it to int to get pointer */
725 vtop
->type
.t
= VT_PTR
;
728 gen_bounded_ptr_add();
729 vtop
->r
|= lval_type
;
732 /* then check for dereferencing */
733 gen_bounded_ptr_deref();
738 /* store vtop a register belonging to class 'rc'. lvalues are
739 converted to values. Cannot be used if cannot be converted to
740 register value (such as structures). */
741 ST_FUNC
int gv(int rc
)
743 int r
, bit_pos
, bit_size
, size
, align
, i
;
746 /* NOTE: get_reg can modify vstack[] */
747 if (vtop
->type
.t
& VT_BITFIELD
) {
750 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
751 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
752 /* remove bit field info to avoid loops */
753 vtop
->type
.t
&= ~VT_BITFIELD
& ((1 << VT_STRUCT_SHIFT
) - 1);
754 /* cast to int to propagate signedness in following ops */
755 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
760 if((vtop
->type
.t
& VT_UNSIGNED
) ||
761 (vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
762 type
.t
|= VT_UNSIGNED
;
764 /* generate shifts */
765 vpushi(bits
- (bit_pos
+ bit_size
));
767 vpushi(bits
- bit_size
);
768 /* NOTE: transformed to SHR if unsigned */
772 if (is_float(vtop
->type
.t
) &&
773 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
776 unsigned long offset
;
777 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
781 /* XXX: unify with initializers handling ? */
782 /* CPUs usually cannot use float constants, so we store them
783 generically in data segment */
784 size
= type_size(&vtop
->type
, &align
);
785 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
786 data_section
->data_offset
= offset
;
787 /* XXX: not portable yet */
788 #if defined(__i386__) || defined(__x86_64__)
789 /* Zero pad x87 tenbyte long doubles */
790 if (size
== LDOUBLE_SIZE
) {
791 vtop
->c
.tab
[2] &= 0xffff;
792 #if LDOUBLE_SIZE == 16
797 ptr
= section_ptr_add(data_section
, size
);
799 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
803 ptr
[i
] = vtop
->c
.tab
[size
-1-i
];
807 ptr
[i
] = vtop
->c
.tab
[i
];
808 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
809 vtop
->r
|= VT_LVAL
| VT_SYM
;
813 #ifdef CONFIG_TCC_BCHECK
814 if (vtop
->r
& VT_MUSTBOUND
)
818 r
= vtop
->r
& VT_VALMASK
;
819 rc2
= (rc
& RC_FLOAT
) ? RC_FLOAT
: RC_INT
;
820 #ifndef TCC_TARGET_ARM64
823 #ifdef TCC_TARGET_X86_64
824 else if (rc
== RC_FRET
)
829 /* need to reload if:
831 - lvalue (need to dereference pointer)
832 - already a register, but not in the right class */
834 || (vtop
->r
& VT_LVAL
)
835 || !(reg_classes
[r
] & rc
)
836 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
837 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
838 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
&& !(reg_classes
[vtop
->r2
] & rc2
))
840 || ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
845 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
846 if (((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
)) {
847 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
849 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
850 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
851 unsigned long long ll
;
853 int r2
, original_type
;
854 original_type
= vtop
->type
.t
;
855 /* two register type load : expand to two words
857 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
858 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
861 vtop
->c
.i
= ll
; /* first word */
863 vtop
->r
= r
; /* save register value */
864 vpushi(ll
>> 32); /* second word */
867 if (vtop
->r
& VT_LVAL
) {
868 /* We do not want to modifier the long long
869 pointer here, so the safest (and less
870 efficient) is to save all the other registers
871 in the stack. XXX: totally inefficient. */
875 /* lvalue_save: save only if used further down the stack */
876 save_reg_upstack(vtop
->r
, 1);
878 /* load from memory */
879 vtop
->type
.t
= load_type
;
882 vtop
[-1].r
= r
; /* save register value */
883 /* increment pointer to get second word */
884 vtop
->type
.t
= addr_type
;
889 vtop
->type
.t
= load_type
;
894 vtop
[-1].r
= r
; /* save register value */
895 vtop
->r
= vtop
[-1].r2
;
897 /* Allocate second register. Here we rely on the fact that
898 get_reg() tries first to free r2 of an SValue. */
902 /* write second register */
904 vtop
->type
.t
= original_type
;
905 } else if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
907 /* lvalue of scalar type : need to use lvalue type
908 because of possible cast */
911 /* compute memory access type */
912 if (vtop
->r
& VT_REF
)
913 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
918 else if (vtop
->r
& VT_LVAL_BYTE
)
920 else if (vtop
->r
& VT_LVAL_SHORT
)
922 if (vtop
->r
& VT_LVAL_UNSIGNED
)
926 /* restore wanted type */
929 /* one register type load */
934 #ifdef TCC_TARGET_C67
935 /* uses register pairs for doubles */
936 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
943 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
944 ST_FUNC
void gv2(int rc1
, int rc2
)
948 /* generate more generic register first. But VT_JMP or VT_CMP
949 values must be generated first in all cases to avoid possible
951 v
= vtop
[0].r
& VT_VALMASK
;
952 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
957 /* test if reload is needed for first register */
958 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
968 /* test if reload is needed for first register */
969 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
975 #ifndef TCC_TARGET_ARM64
976 /* wrapper around RC_FRET to return a register by type */
977 static int rc_fret(int t
)
979 #ifdef TCC_TARGET_X86_64
980 if (t
== VT_LDOUBLE
) {
988 /* wrapper around REG_FRET to return a register by type */
989 static int reg_fret(int t
)
991 #ifdef TCC_TARGET_X86_64
992 if (t
== VT_LDOUBLE
) {
999 /* expand long long on stack in two int registers */
1000 static void lexpand(void)
1004 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
1007 vtop
[0].r
= vtop
[-1].r2
;
1008 vtop
[0].r2
= VT_CONST
;
1009 vtop
[-1].r2
= VT_CONST
;
1010 vtop
[0].type
.t
= VT_INT
| u
;
1011 vtop
[-1].type
.t
= VT_INT
| u
;
1014 #ifdef TCC_TARGET_ARM
1015 /* expand long long on stack */
1016 ST_FUNC
void lexpand_nr(void)
1020 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
1022 vtop
->r2
= VT_CONST
;
1023 vtop
->type
.t
= VT_INT
| u
;
1024 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
1025 if (v
== VT_CONST
) {
1026 vtop
[-1].c
.i
= vtop
->c
.i
;
1027 vtop
->c
.i
= vtop
->c
.i
>> 32;
1029 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
1031 vtop
->r
= vtop
[-1].r
;
1032 } else if (v
> VT_CONST
) {
1036 vtop
->r
= vtop
[-1].r2
;
1037 vtop
[-1].r2
= VT_CONST
;
1038 vtop
[-1].type
.t
= VT_INT
| u
;
1042 /* build a long long from two ints */
1043 static void lbuild(int t
)
1045 gv2(RC_INT
, RC_INT
);
1046 vtop
[-1].r2
= vtop
[0].r
;
1047 vtop
[-1].type
.t
= t
;
1051 /* rotate n first stack elements to the bottom
1052 I1 ... In -> I2 ... In I1 [top is right]
1054 ST_FUNC
void vrotb(int n
)
1060 for(i
=-n
+1;i
!=0;i
++)
1061 vtop
[i
] = vtop
[i
+1];
1065 /* rotate the n elements before entry e towards the top
1066 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
1068 ST_FUNC
void vrote(SValue
*e
, int n
)
1074 for(i
= 0;i
< n
- 1; i
++)
1079 /* rotate n first stack elements to the top
1080 I1 ... In -> In I1 ... I(n-1) [top is right]
1082 ST_FUNC
void vrott(int n
)
1087 /* pop stack value */
1088 ST_FUNC
void vpop(void)
1091 v
= vtop
->r
& VT_VALMASK
;
1092 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1093 /* for x86, we need to pop the FP stack */
1094 if (v
== TREG_ST0
&& !nocode_wanted
) {
1095 o(0xd8dd); /* fstp %st(0) */
1098 if (v
== VT_JMP
|| v
== VT_JMPI
) {
1099 /* need to put correct jump if && or || without test */
1105 /* convert stack entry to register and duplicate its value in another
1107 static void gv_dup(void)
1113 if ((t
& VT_BTYPE
) == VT_LLONG
) {
1120 /* stack: H L L1 H1 */
1128 /* duplicate value */
1133 #ifdef TCC_TARGET_X86_64
1134 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1144 load(r1
, &sv
); /* move r to r1 */
1146 /* duplicates value */
1152 /* Generate value test
1154 * Generate a test for any value (jump, comparison and integers) */
1155 ST_FUNC
int gvtst(int inv
, int t
)
1157 int v
= vtop
->r
& VT_VALMASK
;
1158 if (v
!= VT_CMP
&& v
!= VT_JMP
&& v
!= VT_JMPI
) {
1162 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1163 /* constant jmp optimization */
1164 if ((vtop
->c
.i
!= 0) != inv
)
1169 return gtst(inv
, t
);
1172 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
1173 /* generate CPU independent (unsigned) long long operations */
1174 static void gen_opl(int op
)
1176 int t
, a
, b
, op1
, c
, i
;
1178 unsigned short reg_iret
= REG_IRET
;
1179 unsigned short reg_lret
= REG_LRET
;
1185 func
= TOK___divdi3
;
1188 func
= TOK___udivdi3
;
1191 func
= TOK___moddi3
;
1194 func
= TOK___umoddi3
;
1201 /* call generic long long function */
1202 vpush_global_sym(&func_old_type
, func
);
1207 vtop
->r2
= reg_lret
;
1220 /* stack: L1 H1 L2 H2 */
1225 vtop
[-2] = vtop
[-3];
1228 /* stack: H1 H2 L1 L2 */
1234 /* stack: H1 H2 L1 L2 ML MH */
1237 /* stack: ML MH H1 H2 L1 L2 */
1241 /* stack: ML MH H1 L2 H2 L1 */
1246 /* stack: ML MH M1 M2 */
1249 } else if (op
== '+' || op
== '-') {
1250 /* XXX: add non carry method too (for MIPS or alpha) */
1256 /* stack: H1 H2 (L1 op L2) */
1259 gen_op(op1
+ 1); /* TOK_xxxC2 */
1262 /* stack: H1 H2 (L1 op L2) */
1265 /* stack: (L1 op L2) H1 H2 */
1267 /* stack: (L1 op L2) (H1 op H2) */
1275 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1276 t
= vtop
[-1].type
.t
;
1280 /* stack: L H shift */
1282 /* constant: simpler */
1283 /* NOTE: all comments are for SHL. the other cases are
1284 done by swaping words */
1295 if (op
!= TOK_SAR
) {
1328 /* XXX: should provide a faster fallback on x86 ? */
1331 func
= TOK___ashrdi3
;
1334 func
= TOK___lshrdi3
;
1337 func
= TOK___ashldi3
;
1343 /* compare operations */
1349 /* stack: L1 H1 L2 H2 */
1351 vtop
[-1] = vtop
[-2];
1353 /* stack: L1 L2 H1 H2 */
1356 /* when values are equal, we need to compare low words. since
1357 the jump is inverted, we invert the test too. */
1360 else if (op1
== TOK_GT
)
1362 else if (op1
== TOK_ULT
)
1364 else if (op1
== TOK_UGT
)
1369 if (op1
!= TOK_NE
) {
1373 /* generate non equal test */
1374 /* XXX: NOT PORTABLE yet */
1378 #if defined(TCC_TARGET_I386)
1379 b
= psym(0x850f, 0);
1380 #elif defined(TCC_TARGET_ARM)
1382 o(0x1A000000 | encbranch(ind
, 0, 1));
1383 #elif defined(TCC_TARGET_C67) || defined(TCC_TARGET_ARM64)
1384 tcc_error("not implemented");
1386 #error not supported
1390 /* compare low. Always unsigned */
1394 else if (op1
== TOK_LE
)
1396 else if (op1
== TOK_GT
)
1398 else if (op1
== TOK_GE
)
1409 static uint64_t gen_opic_sdiv(uint64_t a
, uint64_t b
)
1411 uint64_t x
= (a
>> 63 ? -a
: a
) / (b
>> 63 ? -b
: b
);
1412 return (a
^ b
) >> 63 ? -x
: x
;
1415 static int gen_opic_lt(uint64_t a
, uint64_t b
)
1417 return (a
^ (uint64_t)1 << 63) < (b
^ (uint64_t)1 << 63);
1420 /* handle integer constant optimizations and various machine
1422 static void gen_opic(int op
)
1424 SValue
*v1
= vtop
- 1;
1426 int t1
= v1
->type
.t
& VT_BTYPE
;
1427 int t2
= v2
->type
.t
& VT_BTYPE
;
1428 int c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1429 int c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1430 uint64_t l1
= c1
? v1
->c
.i
: 0;
1431 uint64_t l2
= c2
? v2
->c
.i
: 0;
1432 int shm
= (t1
== VT_LLONG
) ? 63 : 31;
1435 l1
= ((uint32_t)l1
|
1436 (v1
->type
.t
& VT_UNSIGNED
? 0 : -(l1
& 0x80000000)));
1438 l2
= ((uint32_t)l2
|
1439 (v2
->type
.t
& VT_UNSIGNED
? 0 : -(l2
& 0x80000000)));
1443 case '+': l1
+= l2
; break;
1444 case '-': l1
-= l2
; break;
1445 case '&': l1
&= l2
; break;
1446 case '^': l1
^= l2
; break;
1447 case '|': l1
|= l2
; break;
1448 case '*': l1
*= l2
; break;
1455 /* if division by zero, generate explicit division */
1458 tcc_error("division by zero in constant");
1462 default: l1
= gen_opic_sdiv(l1
, l2
); break;
1463 case '%': l1
= l1
- l2
* gen_opic_sdiv(l1
, l2
); break;
1464 case TOK_UDIV
: l1
= l1
/ l2
; break;
1465 case TOK_UMOD
: l1
= l1
% l2
; break;
1468 case TOK_SHL
: l1
<<= (l2
& shm
); break;
1469 case TOK_SHR
: l1
>>= (l2
& shm
); break;
1471 l1
= (l1
>> 63) ? ~(~l1
>> (l2
& shm
)) : l1
>> (l2
& shm
);
1474 case TOK_ULT
: l1
= l1
< l2
; break;
1475 case TOK_UGE
: l1
= l1
>= l2
; break;
1476 case TOK_EQ
: l1
= l1
== l2
; break;
1477 case TOK_NE
: l1
= l1
!= l2
; break;
1478 case TOK_ULE
: l1
= l1
<= l2
; break;
1479 case TOK_UGT
: l1
= l1
> l2
; break;
1480 case TOK_LT
: l1
= gen_opic_lt(l1
, l2
); break;
1481 case TOK_GE
: l1
= !gen_opic_lt(l1
, l2
); break;
1482 case TOK_LE
: l1
= !gen_opic_lt(l2
, l1
); break;
1483 case TOK_GT
: l1
= gen_opic_lt(l2
, l1
); break;
1485 case TOK_LAND
: l1
= l1
&& l2
; break;
1486 case TOK_LOR
: l1
= l1
|| l2
; break;
1493 /* if commutative ops, put c2 as constant */
1494 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1495 op
== '|' || op
== '*')) {
1497 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1498 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1500 if (!const_wanted
&&
1502 (op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
)) ||
1503 (l1
== -1 && op
== TOK_SAR
))) {
1504 /* treat (0 << x), (0 >> x) and (-1 >> x) as constant */
1506 } else if (!const_wanted
&&
1507 c2
&& ((l2
== 0 && (op
== '&' || op
== '*')) ||
1508 (l2
== -1 && op
== '|') ||
1509 (l2
== 0xffffffff && t2
!= VT_LLONG
&& op
== '|') ||
1510 (l2
== 1 && (op
== '%' || op
== TOK_UMOD
)))) {
1511 /* treat (x & 0), (x * 0), (x | -1) and (x % 1) as constant */
1516 } else if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1519 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1520 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1524 /* filter out NOP operations like x*1, x-0, x&-1... */
1526 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1527 /* try to use shifts instead of muls or divs */
1528 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1537 else if (op
== TOK_PDIV
)
1543 } else if (c2
&& (op
== '+' || op
== '-') &&
1544 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
1545 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1546 /* symbol + constant case */
1553 if (!nocode_wanted
) {
1554 /* call low level op generator */
1555 if (t1
== VT_LLONG
|| t2
== VT_LLONG
||
1556 (PTR_SIZE
== 8 && (t1
== VT_PTR
|| t2
== VT_PTR
)))
1567 /* generate a floating point operation with constant propagation */
1568 static void gen_opif(int op
)
1576 /* currently, we cannot do computations with forward symbols */
1577 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1578 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1580 if (v1
->type
.t
== VT_FLOAT
) {
1583 } else if (v1
->type
.t
== VT_DOUBLE
) {
1591 /* NOTE: we only do constant propagation if finite number (not
1592 NaN or infinity) (ANSI spec) */
1593 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1597 case '+': f1
+= f2
; break;
1598 case '-': f1
-= f2
; break;
1599 case '*': f1
*= f2
; break;
1603 tcc_error("division by zero in constant");
1608 /* XXX: also handles tests ? */
1612 /* XXX: overflow test ? */
1613 if (v1
->type
.t
== VT_FLOAT
) {
1615 } else if (v1
->type
.t
== VT_DOUBLE
) {
1623 if (!nocode_wanted
) {
1631 static int pointed_size(CType
*type
)
1634 return type_size(pointed_type(type
), &align
);
1637 static void vla_runtime_pointed_size(CType
*type
)
1640 vla_runtime_type_size(pointed_type(type
), &align
);
1643 static inline int is_null_pointer(SValue
*p
)
1645 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1647 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& (uint32_t)p
->c
.i
== 0) ||
1648 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.i
== 0) ||
1649 ((p
->type
.t
& VT_BTYPE
) == VT_PTR
&&
1650 (PTR_SIZE
== 4 ? (uint32_t)p
->c
.i
== 0 : p
->c
.i
== 0));
1653 static inline int is_integer_btype(int bt
)
1655 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
1656 bt
== VT_INT
|| bt
== VT_LLONG
);
1659 /* check types for comparison or subtraction of pointers */
1660 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
1662 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
1665 /* null pointers are accepted for all comparisons as gcc */
1666 if (is_null_pointer(p1
) || is_null_pointer(p2
))
1670 bt1
= type1
->t
& VT_BTYPE
;
1671 bt2
= type2
->t
& VT_BTYPE
;
1672 /* accept comparison between pointer and integer with a warning */
1673 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
1674 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
1675 tcc_warning("comparison between pointer and integer");
1679 /* both must be pointers or implicit function pointers */
1680 if (bt1
== VT_PTR
) {
1681 type1
= pointed_type(type1
);
1682 } else if (bt1
!= VT_FUNC
)
1683 goto invalid_operands
;
1685 if (bt2
== VT_PTR
) {
1686 type2
= pointed_type(type2
);
1687 } else if (bt2
!= VT_FUNC
) {
1689 tcc_error("invalid operands to binary %s", get_tok_str(op
, NULL
));
1691 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1692 (type2
->t
& VT_BTYPE
) == VT_VOID
)
1696 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1697 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1698 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1699 /* gcc-like error if '-' is used */
1701 goto invalid_operands
;
1703 tcc_warning("comparison of distinct pointer types lacks a cast");
1707 /* generic gen_op: handles types problems */
1708 ST_FUNC
void gen_op(int op
)
1710 int u
, t1
, t2
, bt1
, bt2
, t
;
1713 t1
= vtop
[-1].type
.t
;
1714 t2
= vtop
[0].type
.t
;
1715 bt1
= t1
& VT_BTYPE
;
1716 bt2
= t2
& VT_BTYPE
;
1718 if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
1719 tcc_error("operation on a struct");
1720 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
1721 /* at least one operand is a pointer */
1722 /* relationnal op: must be both pointers */
1723 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
1724 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1725 /* pointers are handled are unsigned */
1726 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1727 t
= VT_LLONG
| VT_UNSIGNED
;
1729 t
= VT_INT
| VT_UNSIGNED
;
1733 /* if both pointers, then it must be the '-' op */
1734 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
1736 tcc_error("cannot use pointers here");
1737 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1738 /* XXX: check that types are compatible */
1739 if (vtop
[-1].type
.t
& VT_VLA
) {
1740 vla_runtime_pointed_size(&vtop
[-1].type
);
1742 vpushi(pointed_size(&vtop
[-1].type
));
1746 /* set to integer type */
1747 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1748 vtop
->type
.t
= VT_LLONG
;
1750 vtop
->type
.t
= VT_INT
;
1755 /* exactly one pointer : must be '+' or '-'. */
1756 if (op
!= '-' && op
!= '+')
1757 tcc_error("cannot use pointers here");
1758 /* Put pointer as first operand */
1759 if (bt2
== VT_PTR
) {
1763 type1
= vtop
[-1].type
;
1764 type1
.t
&= ~VT_ARRAY
;
1765 if (vtop
[-1].type
.t
& VT_VLA
)
1766 vla_runtime_pointed_size(&vtop
[-1].type
);
1768 u
= pointed_size(&vtop
[-1].type
);
1770 tcc_error("unknown array element size");
1771 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1774 /* XXX: cast to int ? (long long case) */
1780 /* #ifdef CONFIG_TCC_BCHECK
1781 The main reason to removing this code:
1788 fprintf(stderr, "v+i-j = %p\n", v+i-j);
1789 fprintf(stderr, "v+(i-j) = %p\n", v+(i-j));
1791 When this code is on. then the output looks like
1793 v+(i-j) = 0xbff84000
1795 /* if evaluating constant expression, no code should be
1796 generated, so no bound check */
1797 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
1798 /* if bounded pointers, we generate a special code to
1805 gen_bounded_ptr_add();
1811 /* put again type if gen_opic() swaped operands */
1814 } else if (is_float(bt1
) || is_float(bt2
)) {
1815 /* compute bigger type and do implicit casts */
1816 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
1818 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
1823 /* floats can only be used for a few operations */
1824 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
1825 (op
< TOK_ULT
|| op
> TOK_GT
))
1826 tcc_error("invalid operands for binary operation");
1828 } else if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
) {
1829 t
= bt1
== VT_LLONG
? VT_LLONG
: VT_INT
;
1830 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (t
| VT_UNSIGNED
))
1833 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
1834 /* cast to biggest op */
1836 /* convert to unsigned if it does not fit in a long long */
1837 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
1838 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
1842 /* integer operations */
1844 /* convert to unsigned if it does not fit in an integer */
1845 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
1846 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
1849 /* XXX: currently, some unsigned operations are explicit, so
1850 we modify them here */
1851 if (t
& VT_UNSIGNED
) {
1858 else if (op
== TOK_LT
)
1860 else if (op
== TOK_GT
)
1862 else if (op
== TOK_LE
)
1864 else if (op
== TOK_GE
)
1871 /* special case for shifts and long long: we keep the shift as
1873 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
1880 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
1881 /* relationnal op: the result is an int */
1882 vtop
->type
.t
= VT_INT
;
1887 // Make sure that we have converted to an rvalue:
1888 if (vtop
->r
& VT_LVAL
&& !nocode_wanted
)
1889 gv(is_float(vtop
->type
.t
& VT_BTYPE
) ? RC_FLOAT
: RC_INT
);
1892 #ifndef TCC_TARGET_ARM
1893 /* generic itof for unsigned long long case */
1894 static void gen_cvt_itof1(int t
)
1896 #ifdef TCC_TARGET_ARM64
1899 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
1900 (VT_LLONG
| VT_UNSIGNED
)) {
1903 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
1904 #if LDOUBLE_SIZE != 8
1905 else if (t
== VT_LDOUBLE
)
1906 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
1909 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
1913 vtop
->r
= reg_fret(t
);
1921 /* generic ftoi for unsigned long long case */
1922 static void gen_cvt_ftoi1(int t
)
1924 #ifdef TCC_TARGET_ARM64
1929 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
1930 /* not handled natively */
1931 st
= vtop
->type
.t
& VT_BTYPE
;
1933 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
1934 #if LDOUBLE_SIZE != 8
1935 else if (st
== VT_LDOUBLE
)
1936 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
1939 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
1944 vtop
->r2
= REG_LRET
;
1951 /* force char or short cast */
1952 static void force_charshort_cast(int t
)
1956 /* XXX: add optimization if lvalue : just change type and offset */
1961 if (t
& VT_UNSIGNED
) {
1962 vpushi((1 << bits
) - 1);
1965 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
)
1971 /* result must be signed or the SAR is converted to an SHL
1972 This was not the case when "t" was a signed short
1973 and the last value on the stack was an unsigned int */
1974 vtop
->type
.t
&= ~VT_UNSIGNED
;
1980 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1981 static void gen_cast(CType
*type
)
1983 int sbt
, dbt
, sf
, df
, c
, p
;
1985 /* special delayed cast for char/short */
1986 /* XXX: in some cases (multiple cascaded casts), it may still
1988 if (vtop
->r
& VT_MUSTCAST
) {
1989 vtop
->r
&= ~VT_MUSTCAST
;
1990 force_charshort_cast(vtop
->type
.t
);
1993 /* bitfields first get cast to ints */
1994 if (vtop
->type
.t
& VT_BITFIELD
&& !nocode_wanted
) {
1998 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
1999 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
2004 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
2005 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
2007 /* constant case: we can do it now */
2008 /* XXX: in ISOC, cannot do it if error in convert */
2009 if (sbt
== VT_FLOAT
)
2010 vtop
->c
.ld
= vtop
->c
.f
;
2011 else if (sbt
== VT_DOUBLE
)
2012 vtop
->c
.ld
= vtop
->c
.d
;
2015 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
2016 if ((sbt
& VT_UNSIGNED
) || !(vtop
->c
.i
>> 63))
2017 vtop
->c
.ld
= vtop
->c
.i
;
2019 vtop
->c
.ld
= -(long double)-vtop
->c
.i
;
2021 if ((sbt
& VT_UNSIGNED
) || !(vtop
->c
.i
>> 31))
2022 vtop
->c
.ld
= (uint32_t)vtop
->c
.i
;
2024 vtop
->c
.ld
= -(long double)-(uint32_t)vtop
->c
.i
;
2027 if (dbt
== VT_FLOAT
)
2028 vtop
->c
.f
= (float)vtop
->c
.ld
;
2029 else if (dbt
== VT_DOUBLE
)
2030 vtop
->c
.d
= (double)vtop
->c
.ld
;
2031 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
2032 vtop
->c
.i
= vtop
->c
.ld
;
2033 } else if (sf
&& dbt
== VT_BOOL
) {
2034 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
2037 vtop
->c
.i
= vtop
->c
.ld
;
2038 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
2040 else if (sbt
& VT_UNSIGNED
)
2041 vtop
->c
.i
= (uint32_t)vtop
->c
.i
;
2042 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2043 else if (sbt
== VT_PTR
)
2046 else if (sbt
!= VT_LLONG
)
2047 vtop
->c
.i
= ((uint32_t)vtop
->c
.i
|
2048 -(vtop
->c
.i
& 0x80000000));
2050 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
2052 else if (dbt
== VT_BOOL
)
2053 vtop
->c
.i
= (vtop
->c
.i
!= 0);
2054 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2055 else if (dbt
== VT_PTR
)
2058 else if (dbt
!= VT_LLONG
) {
2059 uint32_t m
= ((dbt
& VT_BTYPE
) == VT_BYTE
? 0xff :
2060 (dbt
& VT_BTYPE
) == VT_SHORT
? 0xffff :
2063 if (!(dbt
& VT_UNSIGNED
))
2064 vtop
->c
.i
|= -(vtop
->c
.i
& ((m
>> 1) + 1));
2067 } else if (p
&& dbt
== VT_BOOL
) {
2070 } else if (!nocode_wanted
) {
2071 /* non constant case: generate code */
2073 /* convert from fp to fp */
2076 /* convert int to fp */
2079 /* convert fp to int */
2080 if (dbt
== VT_BOOL
) {
2084 /* we handle char/short/etc... with generic code */
2085 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
2086 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
2090 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
2091 /* additional cast for char/short... */
2096 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
2097 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
2098 if ((sbt
& VT_BTYPE
) != VT_LLONG
&& !nocode_wanted
) {
2099 /* scalar to long long */
2100 /* machine independent conversion */
2102 /* generate high word */
2103 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
2107 if (sbt
== VT_PTR
) {
2108 /* cast from pointer to int before we apply
2109 shift operation, which pointers don't support*/
2110 gen_cast(&int_type
);
2116 /* patch second register */
2117 vtop
[-1].r2
= vtop
->r
;
2121 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
2122 (dbt
& VT_BTYPE
) == VT_PTR
||
2123 (dbt
& VT_BTYPE
) == VT_FUNC
) {
2124 if ((sbt
& VT_BTYPE
) != VT_LLONG
&&
2125 (sbt
& VT_BTYPE
) != VT_PTR
&&
2126 (sbt
& VT_BTYPE
) != VT_FUNC
&& !nocode_wanted
) {
2127 /* need to convert from 32bit to 64bit */
2129 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
2130 #if defined(TCC_TARGET_ARM64)
2132 #elif defined(TCC_TARGET_X86_64)
2134 /* x86_64 specific: movslq */
2136 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
2143 } else if (dbt
== VT_BOOL
) {
2144 /* scalar to bool */
2147 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
2148 (dbt
& VT_BTYPE
) == VT_SHORT
) {
2149 if (sbt
== VT_PTR
) {
2150 vtop
->type
.t
= VT_INT
;
2151 tcc_warning("nonportable conversion from pointer to char/short");
2153 force_charshort_cast(dbt
);
2154 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
2156 if (sbt
== VT_LLONG
&& !nocode_wanted
) {
2157 /* from long long: just take low order word */
2161 /* if lvalue and single word type, nothing to do because
2162 the lvalue already contains the real type size (see
2163 VT_LVAL_xxx constants) */
2166 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
2167 /* if we are casting between pointer types,
2168 we must update the VT_LVAL_xxx size */
2169 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
2170 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
2175 /* return type size as known at compile time. Put alignment at 'a' */
2176 ST_FUNC
int type_size(CType
*type
, int *a
)
2181 bt
= type
->t
& VT_BTYPE
;
2182 if (bt
== VT_STRUCT
) {
2187 } else if (bt
== VT_PTR
) {
2188 if (type
->t
& VT_ARRAY
) {
2192 ts
= type_size(&s
->type
, a
);
2194 if (ts
< 0 && s
->c
< 0)
2202 } else if (bt
== VT_LDOUBLE
) {
2204 return LDOUBLE_SIZE
;
2205 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
2206 #ifdef TCC_TARGET_I386
2207 #ifdef TCC_TARGET_PE
2212 #elif defined(TCC_TARGET_ARM)
2222 } else if (bt
== VT_INT
|| bt
== VT_FLOAT
) {
2225 } else if (bt
== VT_SHORT
) {
2228 } else if (bt
== VT_QLONG
|| bt
== VT_QFLOAT
) {
2231 } else if (bt
== VT_ENUM
) {
2233 /* Enums might be incomplete, so don't just return '4' here. */
2234 return type
->ref
->c
;
2236 /* char, void, function, _Bool */
2242 /* push type size as known at runtime time on top of value stack. Put
2244 ST_FUNC
void vla_runtime_type_size(CType
*type
, int *a
)
2246 if (type
->t
& VT_VLA
) {
2247 type_size(&type
->ref
->type
, a
);
2248 vset(&int_type
, VT_LOCAL
|VT_LVAL
, type
->ref
->c
);
2250 vpushi(type_size(type
, a
));
2254 static void vla_sp_restore(void) {
2255 if (vlas_in_scope
) {
2256 gen_vla_sp_restore(vla_sp_loc
);
2260 static void vla_sp_restore_root(void) {
2261 if (vlas_in_scope
) {
2262 gen_vla_sp_restore(vla_sp_root_loc
);
2266 /* return the pointed type of t */
2267 static inline CType
*pointed_type(CType
*type
)
2269 return &type
->ref
->type
;
2272 /* modify type so that its it is a pointer to type. */
2273 ST_FUNC
void mk_pointer(CType
*type
)
2276 s
= sym_push(SYM_FIELD
, type
, 0, -1);
2277 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
2281 /* compare function types. OLD functions match any new functions */
2282 static int is_compatible_func(CType
*type1
, CType
*type2
)
2288 if (!is_compatible_types(&s1
->type
, &s2
->type
))
2290 /* check func_call */
2291 if (s1
->a
.func_call
!= s2
->a
.func_call
)
2293 /* XXX: not complete */
2294 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
2298 while (s1
!= NULL
) {
2301 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
2311 /* return true if type1 and type2 are the same. If unqualified is
2312 true, qualifiers on the types are ignored.
2314 - enums are not checked as gcc __builtin_types_compatible_p ()
2316 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2320 t1
= type1
->t
& VT_TYPE
;
2321 t2
= type2
->t
& VT_TYPE
;
2323 /* strip qualifiers before comparing */
2324 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2325 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2327 /* Default Vs explicit signedness only matters for char */
2328 if ((t1
& VT_BTYPE
) != VT_BYTE
) {
2332 /* XXX: bitfields ? */
2335 /* test more complicated cases */
2336 bt1
= t1
& VT_BTYPE
;
2337 if (bt1
== VT_PTR
) {
2338 type1
= pointed_type(type1
);
2339 type2
= pointed_type(type2
);
2340 return is_compatible_types(type1
, type2
);
2341 } else if (bt1
== VT_STRUCT
) {
2342 return (type1
->ref
== type2
->ref
);
2343 } else if (bt1
== VT_FUNC
) {
2344 return is_compatible_func(type1
, type2
);
2350 /* return true if type1 and type2 are exactly the same (including
2353 static int is_compatible_types(CType
*type1
, CType
*type2
)
2355 return compare_types(type1
,type2
,0);
2358 /* return true if type1 and type2 are the same (ignoring qualifiers).
2360 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
2362 return compare_types(type1
,type2
,1);
2365 /* print a type. If 'varstr' is not NULL, then the variable is also
2366 printed in the type */
2368 /* XXX: add array and function pointers */
2369 static void type_to_str(char *buf
, int buf_size
,
2370 CType
*type
, const char *varstr
)
2377 t
= type
->t
& VT_TYPE
;
2380 if (t
& VT_CONSTANT
)
2381 pstrcat(buf
, buf_size
, "const ");
2382 if (t
& VT_VOLATILE
)
2383 pstrcat(buf
, buf_size
, "volatile ");
2384 if ((t
& (VT_DEFSIGN
| VT_UNSIGNED
)) == (VT_DEFSIGN
| VT_UNSIGNED
))
2385 pstrcat(buf
, buf_size
, "unsigned ");
2386 else if (t
& VT_DEFSIGN
)
2387 pstrcat(buf
, buf_size
, "signed ");
2417 tstr
= "long double";
2419 pstrcat(buf
, buf_size
, tstr
);
2423 if (bt
== VT_STRUCT
)
2427 pstrcat(buf
, buf_size
, tstr
);
2428 v
= type
->ref
->v
& ~SYM_STRUCT
;
2429 if (v
>= SYM_FIRST_ANOM
)
2430 pstrcat(buf
, buf_size
, "<anonymous>");
2432 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
2436 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
2437 pstrcat(buf
, buf_size
, "(");
2439 while (sa
!= NULL
) {
2440 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
2441 pstrcat(buf
, buf_size
, buf1
);
2444 pstrcat(buf
, buf_size
, ", ");
2446 pstrcat(buf
, buf_size
, ")");
2451 snprintf(buf1
, sizeof(buf1
), "%s[%ld]", varstr
? varstr
: "", s
->c
);
2452 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2455 pstrcpy(buf1
, sizeof(buf1
), "*");
2456 if (t
& VT_CONSTANT
)
2457 pstrcat(buf1
, buf_size
, "const ");
2458 if (t
& VT_VOLATILE
)
2459 pstrcat(buf1
, buf_size
, "volatile ");
2461 pstrcat(buf1
, sizeof(buf1
), varstr
);
2462 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2466 pstrcat(buf
, buf_size
, " ");
2467 pstrcat(buf
, buf_size
, varstr
);
2472 /* verify type compatibility to store vtop in 'dt' type, and generate
2474 static void gen_assign_cast(CType
*dt
)
2476 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
2477 char buf1
[256], buf2
[256];
2480 st
= &vtop
->type
; /* source type */
2481 dbt
= dt
->t
& VT_BTYPE
;
2482 sbt
= st
->t
& VT_BTYPE
;
2483 if (sbt
== VT_VOID
|| dbt
== VT_VOID
) {
2484 if (sbt
== VT_VOID
&& dbt
== VT_VOID
)
2486 It is Ok if both are void
2492 gcc accepts this program
2495 tcc_error("cannot cast from/to void");
2497 if (dt
->t
& VT_CONSTANT
)
2498 tcc_warning("assignment of read-only location");
2501 /* special cases for pointers */
2502 /* '0' can also be a pointer */
2503 if (is_null_pointer(vtop
))
2505 /* accept implicit pointer to integer cast with warning */
2506 if (is_integer_btype(sbt
)) {
2507 tcc_warning("assignment makes pointer from integer without a cast");
2510 type1
= pointed_type(dt
);
2511 /* a function is implicitely a function pointer */
2512 if (sbt
== VT_FUNC
) {
2513 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
2514 !is_compatible_types(pointed_type(dt
), st
))
2515 tcc_warning("assignment from incompatible pointer type");
2520 type2
= pointed_type(st
);
2521 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2522 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
2523 /* void * can match anything */
2525 /* exact type match, except for unsigned */
2528 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
|
2530 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
|
2532 if (!is_compatible_types(&tmp_type1
, &tmp_type2
))
2533 tcc_warning("assignment from incompatible pointer type");
2535 /* check const and volatile */
2536 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
2537 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
2538 tcc_warning("assignment discards qualifiers from pointer target type");
2544 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
2545 tcc_warning("assignment makes integer from pointer without a cast");
2546 } else if (sbt
== VT_STRUCT
) {
2547 goto case_VT_STRUCT
;
2549 /* XXX: more tests */
2555 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2556 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2557 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2559 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
2560 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
2561 tcc_error("cannot cast '%s' to '%s'", buf1
, buf2
);
2569 /* store vtop in lvalue pushed on stack */
2570 ST_FUNC
void vstore(void)
2572 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
2574 ft
= vtop
[-1].type
.t
;
2575 sbt
= vtop
->type
.t
& VT_BTYPE
;
2576 dbt
= ft
& VT_BTYPE
;
2577 if ((((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2578 (sbt
== VT_INT
&& dbt
== VT_SHORT
))
2579 && !(vtop
->type
.t
& VT_BITFIELD
)) {
2580 /* optimize char/short casts */
2581 delayed_cast
= VT_MUSTCAST
;
2582 vtop
->type
.t
= (ft
& VT_TYPE
& ~VT_BITFIELD
&
2583 ((1 << VT_STRUCT_SHIFT
) - 1));
2584 /* XXX: factorize */
2585 if (ft
& VT_CONSTANT
)
2586 tcc_warning("assignment of read-only location");
2589 if (!(ft
& VT_BITFIELD
))
2590 gen_assign_cast(&vtop
[-1].type
);
2593 if (sbt
== VT_STRUCT
) {
2594 /* if structure, only generate pointer */
2595 /* structure assignment : generate memcpy */
2596 /* XXX: optimize if small size */
2597 if (!nocode_wanted
) {
2598 size
= type_size(&vtop
->type
, &align
);
2602 vtop
->type
.t
= VT_PTR
;
2605 /* address of memcpy() */
2608 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2609 else if(!(align
& 3))
2610 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2613 /* Use memmove, rather than memcpy, as dest and src may be same: */
2614 vpush_global_sym(&func_old_type
, TOK_memmove
);
2619 vtop
->type
.t
= VT_PTR
;
2628 /* leave source on stack */
2629 } else if (ft
& VT_BITFIELD
) {
2630 /* bitfield store handling */
2632 /* save lvalue as expression result (example: s.b = s.a = n;) */
2633 vdup(), vtop
[-1] = vtop
[-2];
2635 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2636 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2637 /* remove bit field info to avoid loops */
2638 vtop
[-1].type
.t
= ft
& ~VT_BITFIELD
& ((1 << VT_STRUCT_SHIFT
) - 1);
2640 if((ft
& VT_BTYPE
) == VT_BOOL
) {
2641 gen_cast(&vtop
[-1].type
);
2642 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2645 /* duplicate destination */
2647 vtop
[-1] = vtop
[-2];
2649 /* mask and shift source */
2650 if((ft
& VT_BTYPE
) != VT_BOOL
) {
2651 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2652 vpushll((1ULL << bit_size
) - 1ULL);
2654 vpushi((1 << bit_size
) - 1);
2660 /* load destination, mask and or with source */
2662 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2663 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2665 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2671 /* ... and discard */
2675 if (!nocode_wanted
) {
2676 #ifdef CONFIG_TCC_BCHECK
2677 /* bound check case */
2678 if (vtop
[-1].r
& VT_MUSTBOUND
) {
2687 #ifdef TCC_TARGET_X86_64
2688 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
2690 } else if ((ft
& VT_BTYPE
) == VT_QFLOAT
) {
2695 r
= gv(rc
); /* generate value */
2696 /* if lvalue was saved on stack, must read it */
2697 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
2699 t
= get_reg(RC_INT
);
2700 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2705 sv
.r
= VT_LOCAL
| VT_LVAL
;
2706 sv
.c
.i
= vtop
[-1].c
.i
;
2708 vtop
[-1].r
= t
| VT_LVAL
;
2710 /* two word case handling : store second register at word + 4 (or +8 for x86-64) */
2711 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2712 if (((ft
& VT_BTYPE
) == VT_QLONG
) || ((ft
& VT_BTYPE
) == VT_QFLOAT
)) {
2713 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
2715 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
2716 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
2718 vtop
[-1].type
.t
= load_type
;
2721 /* convert to int to increment easily */
2722 vtop
->type
.t
= addr_type
;
2728 vtop
[-1].type
.t
= load_type
;
2729 /* XXX: it works because r2 is spilled last ! */
2730 store(vtop
->r2
, vtop
- 1);
2736 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
2737 vtop
->r
|= delayed_cast
;
2741 /* post defines POST/PRE add. c is the token ++ or -- */
2742 ST_FUNC
void inc(int post
, int c
)
2745 vdup(); /* save lvalue */
2748 gv_dup(); /* duplicate value */
2750 vdup(); /* duplicate value */
2755 vpushi(c
- TOK_MID
);
2757 vstore(); /* store value */
2759 vpop(); /* if post op, return saved value */
2762 /* Parse GNUC __attribute__ extension. Currently, the following
2763 extensions are recognized:
2764 - aligned(n) : set data/function alignment.
2765 - packed : force data alignment to 1
2766 - section(x) : generate data/code in this section.
2767 - unused : currently ignored, but may be used someday.
2768 - regparm(n) : pass function parameters in registers (i386 only)
2770 static void parse_attribute(AttributeDef
*ad
)
2774 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
2778 while (tok
!= ')') {
2779 if (tok
< TOK_IDENT
)
2780 expect("attribute name");
2788 expect("section name");
2789 ad
->section
= find_section(tcc_state
, (char *)tokc
.str
.data
);
2797 expect("alias(\"target\")");
2798 ad
->alias_target
= /* save string as token, for later */
2799 tok_alloc((char*)tokc
.str
.data
, tokc
.str
.size
-1)->tok
;
2803 case TOK_VISIBILITY1
:
2804 case TOK_VISIBILITY2
:
2807 expect("visibility(\"default|hidden|internal|protected\")");
2808 if (!strcmp (tokc
.str
.data
, "default"))
2809 ad
->a
.visibility
= STV_DEFAULT
;
2810 else if (!strcmp (tokc
.str
.data
, "hidden"))
2811 ad
->a
.visibility
= STV_HIDDEN
;
2812 else if (!strcmp (tokc
.str
.data
, "internal"))
2813 ad
->a
.visibility
= STV_INTERNAL
;
2814 else if (!strcmp (tokc
.str
.data
, "protected"))
2815 ad
->a
.visibility
= STV_PROTECTED
;
2817 expect("visibility(\"default|hidden|internal|protected\")");
2826 if (n
<= 0 || (n
& (n
- 1)) != 0)
2827 tcc_error("alignment must be a positive power of two");
2844 /* currently, no need to handle it because tcc does not
2845 track unused objects */
2849 /* currently, no need to handle it because tcc does not
2850 track unused objects */
2855 ad
->a
.func_call
= FUNC_CDECL
;
2860 ad
->a
.func_call
= FUNC_STDCALL
;
2862 #ifdef TCC_TARGET_I386
2872 ad
->a
.func_call
= FUNC_FASTCALL1
+ n
- 1;
2878 ad
->a
.func_call
= FUNC_FASTCALLW
;
2885 ad
->a
.mode
= VT_LLONG
+ 1;
2888 ad
->a
.mode
= VT_SHORT
+ 1;
2891 ad
->a
.mode
= VT_INT
+ 1;
2894 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
2901 ad
->a
.func_export
= 1;
2904 ad
->a
.func_import
= 1;
2907 if (tcc_state
->warn_unsupported
)
2908 tcc_warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
2909 /* skip parameters */
2911 int parenthesis
= 0;
2915 else if (tok
== ')')
2918 } while (parenthesis
&& tok
!= -1);
2931 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2932 static void struct_decl(CType
*type
, AttributeDef
*ad
, int u
)
2934 int a
, v
, size
, align
, maxalign
, c
, offset
, flexible
;
2935 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
, prevbt
;
2936 Sym
*s
, *ss
, *ass
, **ps
;
2940 a
= tok
; /* save decl type */
2942 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
2943 parse_attribute(ad
);
2949 /* struct already defined ? return it */
2951 expect("struct/union/enum name");
2953 if (s
&& (s
->scope
== local_scope
|| (tok
!= '{' && tok
!= ';'))) {
2955 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
2963 /* we put an undefined size for struct/union */
2964 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
2965 s
->r
= 0; /* default alignment is zero as gcc */
2966 /* put struct/union/enum name in type */
2974 tcc_error("struct/union/enum already defined");
2975 /* cannot be empty */
2977 /* non empty enums are not allowed */
2978 if (a
== TOK_ENUM
) {
2982 expect("identifier");
2984 if (ss
&& !local_stack
)
2985 tcc_error("redefinition of enumerator '%s'",
2986 get_tok_str(v
, NULL
));
2992 /* enum symbols have static storage */
2993 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
2994 ss
->type
.t
|= VT_STATIC
;
2999 /* NOTE: we accept a trailing comma */
3003 s
->c
= type_size(&int_type
, &align
);
3012 while (tok
!= '}') {
3013 parse_btype(&btype
, &ad1
);
3016 tcc_error("flexible array member '%s' not at the end of struct",
3017 get_tok_str(v
, NULL
));
3022 type_decl(&type1
, &ad1
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3024 if ((type1
.t
& VT_BTYPE
) != VT_STRUCT
)
3025 expect("identifier");
3027 int v
= btype
.ref
->v
;
3028 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
3029 if (tcc_state
->ms_extensions
== 0)
3030 expect("identifier");
3034 if (type_size(&type1
, &align
) < 0) {
3035 if ((a
== TOK_STRUCT
) && (type1
.t
& VT_ARRAY
) && c
)
3038 tcc_error("field '%s' has incomplete type",
3039 get_tok_str(v
, NULL
));
3041 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
3042 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
3043 tcc_error("invalid type for '%s'",
3044 get_tok_str(v
, NULL
));
3048 bit_size
= expr_const();
3049 /* XXX: handle v = 0 case for messages */
3051 tcc_error("negative width in bit-field '%s'",
3052 get_tok_str(v
, NULL
));
3053 if (v
&& bit_size
== 0)
3054 tcc_error("zero width for bit-field '%s'",
3055 get_tok_str(v
, NULL
));
3057 size
= type_size(&type1
, &align
);
3058 if (ad1
.a
.aligned
) {
3059 if (align
< ad1
.a
.aligned
)
3060 align
= ad1
.a
.aligned
;
3061 } else if (ad1
.a
.packed
) {
3063 } else if (*tcc_state
->pack_stack_ptr
) {
3064 if (align
> *tcc_state
->pack_stack_ptr
)
3065 align
= *tcc_state
->pack_stack_ptr
;
3068 if (bit_size
>= 0) {
3069 bt
= type1
.t
& VT_BTYPE
;
3076 tcc_error("bitfields must have scalar type");
3078 if (bit_size
> bsize
) {
3079 tcc_error("width of '%s' exceeds its type",
3080 get_tok_str(v
, NULL
));
3081 } else if (bit_size
== bsize
) {
3082 /* no need for bit fields */
3084 } else if (bit_size
== 0) {
3085 /* XXX: what to do if only padding in a
3087 /* zero size: means to pad */
3090 /* we do not have enough room ?
3091 did the type change?
3093 if ((bit_pos
+ bit_size
) > bsize
||
3094 bt
!= prevbt
|| a
== TOK_UNION
)
3097 /* XXX: handle LSB first */
3098 type1
.t
|= VT_BITFIELD
|
3099 (bit_pos
<< VT_STRUCT_SHIFT
) |
3100 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
3101 bit_pos
+= bit_size
;
3107 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
3108 /* add new memory data only if starting
3110 if (lbit_pos
== 0) {
3111 if (a
== TOK_STRUCT
) {
3112 c
= (c
+ align
- 1) & -align
;
3121 if (align
> maxalign
)
3125 printf("add field %s offset=%d",
3126 get_tok_str(v
, NULL
), offset
);
3127 if (type1
.t
& VT_BITFIELD
) {
3128 printf(" pos=%d size=%d",
3129 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
3130 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
3135 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
3137 while ((ass
= ass
->next
) != NULL
) {
3138 ss
= sym_push(ass
->v
, &ass
->type
, 0, offset
+ ass
->c
);
3143 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
3147 if (tok
== ';' || tok
== TOK_EOF
)
3154 /* store size and alignment */
3155 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
3161 /* return 1 if basic type is a type size (short, long, long long) */
3162 ST_FUNC
int is_btype_size(int bt
)
3164 return bt
== VT_SHORT
|| bt
== VT_LONG
|| bt
== VT_LLONG
;
3167 /* Add type qualifiers to a type. If the type is an array then the qualifiers
3168 are added to the element type, copied because it could be a typedef. */
3169 static void parse_btype_qualify(CType
*type
, int qualifiers
)
3171 while (type
->t
& VT_ARRAY
) {
3172 type
->ref
= sym_push(SYM_FIELD
, &type
->ref
->type
, 0, type
->ref
->c
);
3173 type
= &type
->ref
->type
;
3175 type
->t
|= qualifiers
;
3178 /* return 0 if no type declaration. otherwise, return the basic type
3181 static int parse_btype(CType
*type
, AttributeDef
*ad
)
3183 int t
, u
, bt_size
, complete
, type_found
, typespec_found
;
3187 memset(ad
, 0, sizeof(AttributeDef
));
3195 /* currently, we really ignore extension */
3206 tcc_error("too many basic types");
3208 bt_size
= is_btype_size (u
& VT_BTYPE
);
3209 if (u
== VT_INT
|| (!bt_size
&& !(t
& VT_TYPEDEF
)))
3224 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
3225 #ifndef TCC_TARGET_PE
3226 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3228 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
3229 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3235 #ifdef TCC_TARGET_ARM64
3237 /* GCC's __uint128_t appears in some Linux header files. Make it a
3238 synonym for long double to get the size and alignment right. */
3250 if ((t
& VT_BTYPE
) == VT_LONG
) {
3251 #ifdef TCC_TARGET_PE
3252 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
3254 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3262 struct_decl(&type1
, ad
, VT_ENUM
);
3265 type
->ref
= type1
.ref
;
3269 struct_decl(&type1
, ad
, VT_STRUCT
);
3272 /* type modifiers */
3277 parse_btype_qualify(type
, VT_CONSTANT
);
3285 parse_btype_qualify(type
, VT_VOLATILE
);
3292 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == (VT_DEFSIGN
|VT_UNSIGNED
))
3293 tcc_error("signed and unsigned modifier");
3306 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == VT_DEFSIGN
)
3307 tcc_error("signed and unsigned modifier");
3308 t
|= VT_DEFSIGN
| VT_UNSIGNED
;
3333 /* GNUC attribute */
3334 case TOK_ATTRIBUTE1
:
3335 case TOK_ATTRIBUTE2
:
3336 parse_attribute(ad
);
3339 t
= (t
& ~VT_BTYPE
) | u
;
3347 parse_expr_type(&type1
);
3348 /* remove all storage modifiers except typedef */
3349 type1
.t
&= ~(VT_STORAGE
&~VT_TYPEDEF
);
3355 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
3358 type
->t
= ((s
->type
.t
& ~VT_TYPEDEF
) |
3359 (t
& ~(VT_CONSTANT
| VT_VOLATILE
)));
3360 type
->ref
= s
->type
.ref
;
3361 if (t
& (VT_CONSTANT
| VT_VOLATILE
))
3362 parse_btype_qualify(type
, t
& (VT_CONSTANT
| VT_VOLATILE
));
3366 /* get attributes from typedef */
3367 if (0 == ad
->a
.aligned
)
3368 ad
->a
.aligned
= s
->a
.aligned
;
3369 if (0 == ad
->a
.func_call
)
3370 ad
->a
.func_call
= s
->a
.func_call
;
3371 ad
->a
.packed
|= s
->a
.packed
;
3380 if (tcc_state
->char_is_unsigned
) {
3381 if ((t
& (VT_DEFSIGN
|VT_BTYPE
)) == VT_BYTE
)
3385 /* long is never used as type */
3386 if ((t
& VT_BTYPE
) == VT_LONG
)
3387 #if (!defined TCC_TARGET_X86_64 && !defined TCC_TARGET_ARM64) || \
3388 defined TCC_TARGET_PE
3389 t
= (t
& ~VT_BTYPE
) | VT_INT
;
3391 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3397 /* convert a function parameter type (array to pointer and function to
3398 function pointer) */
3399 static inline void convert_parameter_type(CType
*pt
)
3401 /* remove const and volatile qualifiers (XXX: const could be used
3402 to indicate a const function parameter */
3403 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3404 /* array must be transformed to pointer according to ANSI C */
3406 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
3411 ST_FUNC
void parse_asm_str(CString
*astr
)
3414 /* read the string */
3416 expect("string constant");
3418 while (tok
== TOK_STR
) {
3419 /* XXX: add \0 handling too ? */
3420 cstr_cat(astr
, tokc
.str
.data
, -1);
3423 cstr_ccat(astr
, '\0');
3426 /* Parse an asm label and return the token */
3427 static int asm_label_instr(void)
3433 parse_asm_str(&astr
);
3436 printf("asm_alias: \"%s\"\n", (char *)astr
.data
);
3438 v
= tok_alloc(astr
.data
, astr
.size
- 1)->tok
;
3443 static void post_type(CType
*type
, AttributeDef
*ad
)
3445 int n
, l
, t1
, arg_size
, align
;
3446 Sym
**plast
, *s
, *first
;
3451 /* function declaration */
3459 /* read param name and compute offset */
3460 if (l
!= FUNC_OLD
) {
3461 if (!parse_btype(&pt
, &ad1
)) {
3463 tcc_error("invalid type");
3470 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
3472 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3473 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
3474 tcc_error("parameter declared as void");
3475 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
3480 expect("identifier");
3484 convert_parameter_type(&pt
);
3485 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
3491 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
3498 /* if no parameters, then old type prototype */
3502 /* NOTE: const is ignored in returned type as it has a special
3503 meaning in gcc / C++ */
3504 type
->t
&= ~VT_CONSTANT
;
3505 /* some ancient pre-K&R C allows a function to return an array
3506 and the array brackets to be put after the arguments, such
3507 that "int c()[]" means something like "int[] c()" */
3510 skip(']'); /* only handle simple "[]" */
3513 /* we push a anonymous symbol which will contain the function prototype */
3514 ad
->a
.func_args
= arg_size
;
3515 s
= sym_push(SYM_FIELD
, type
, 0, l
);
3520 } else if (tok
== '[') {
3521 /* array definition */
3523 if (tok
== TOK_RESTRICT1
)
3528 if (!local_stack
|| nocode_wanted
)
3529 vpushi(expr_const());
3531 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3534 tcc_error("invalid array size");
3536 if (!is_integer_btype(vtop
->type
.t
& VT_BTYPE
))
3537 tcc_error("size of variable length array should be an integer");
3542 /* parse next post type */
3543 post_type(type
, ad
);
3544 if (type
->t
== VT_FUNC
)
3545 tcc_error("declaration of an array of functions");
3546 t1
|= type
->t
& VT_VLA
;
3549 loc
-= type_size(&int_type
, &align
);
3553 vla_runtime_type_size(type
, &align
);
3555 vset(&int_type
, VT_LOCAL
|VT_LVAL
, n
);
3562 /* we push an anonymous symbol which will contain the array
3564 s
= sym_push(SYM_FIELD
, type
, 0, n
);
3565 type
->t
= (t1
? VT_VLA
: VT_ARRAY
) | VT_PTR
;
3570 /* Parse a type declaration (except basic type), and return the type
3571 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3572 expected. 'type' should contain the basic type. 'ad' is the
3573 attribute definition of the basic type. It can be modified by
3576 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
3579 CType type1
, *type2
;
3580 int qualifiers
, storage
;
3582 while (tok
== '*') {
3590 qualifiers
|= VT_CONSTANT
;
3595 qualifiers
|= VT_VOLATILE
;
3603 type
->t
|= qualifiers
;
3606 /* XXX: clarify attribute handling */
3607 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3608 parse_attribute(ad
);
3610 /* recursive type */
3611 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3612 type1
.t
= 0; /* XXX: same as int */
3615 /* XXX: this is not correct to modify 'ad' at this point, but
3616 the syntax is not clear */
3617 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3618 parse_attribute(ad
);
3619 type_decl(&type1
, ad
, v
, td
);
3622 /* type identifier */
3623 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
3627 if (!(td
& TYPE_ABSTRACT
))
3628 expect("identifier");
3632 storage
= type
->t
& VT_STORAGE
;
3633 type
->t
&= ~VT_STORAGE
;
3634 if (storage
& VT_STATIC
) {
3635 int saved_nocode_wanted
= nocode_wanted
;
3637 post_type(type
, ad
);
3638 nocode_wanted
= saved_nocode_wanted
;
3640 post_type(type
, ad
);
3642 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3643 parse_attribute(ad
);
3647 /* append type at the end of type1 */
3660 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3661 ST_FUNC
int lvalue_type(int t
)
3666 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
3668 else if (bt
== VT_SHORT
)
3672 if (t
& VT_UNSIGNED
)
3673 r
|= VT_LVAL_UNSIGNED
;
3677 /* indirection with full error checking and bound check */
3678 ST_FUNC
void indir(void)
3680 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
3681 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
3685 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
3687 vtop
->type
= *pointed_type(&vtop
->type
);
3688 /* Arrays and functions are never lvalues */
3689 if (!(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_VLA
)
3690 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3691 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3692 /* if bound checking, the referenced pointer must be checked */
3693 #ifdef CONFIG_TCC_BCHECK
3694 if (tcc_state
->do_bounds_check
)
3695 vtop
->r
|= VT_MUSTBOUND
;
3700 /* pass a parameter to a function and do type checking and casting */
3701 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
3706 func_type
= func
->c
;
3707 if (func_type
== FUNC_OLD
||
3708 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
3709 /* default casting : only need to convert float to double */
3710 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
3713 } else if (vtop
->type
.t
& VT_BITFIELD
) {
3714 type
.t
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
3717 } else if (arg
== NULL
) {
3718 tcc_error("too many arguments to function");
3721 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
3722 gen_assign_cast(&type
);
3726 /* parse an expression of the form '(type)' or '(expr)' and return its
3728 static void parse_expr_type(CType
*type
)
3734 if (parse_btype(type
, &ad
)) {
3735 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3742 static void parse_type(CType
*type
)
3747 if (!parse_btype(type
, &ad
)) {
3750 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3753 static void vpush_tokc(int t
)
3758 vsetc(&type
, VT_CONST
, &tokc
);
3761 ST_FUNC
void unary(void)
3763 int n
, t
, align
, size
, r
, sizeof_caller
;
3768 sizeof_caller
= in_sizeof
;
3770 /* XXX: GCC 2.95.3 does not generate a table although it should be
3784 vpush_tokc(VT_INT
| VT_UNSIGNED
);
3788 vpush_tokc(VT_LLONG
);
3792 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
3796 vpush_tokc(VT_FLOAT
);
3800 vpush_tokc(VT_DOUBLE
);
3804 vpush_tokc(VT_LDOUBLE
);
3807 case TOK___FUNCTION__
:
3809 goto tok_identifier
;
3815 /* special function name identifier */
3816 len
= strlen(funcname
) + 1;
3817 /* generate char[len] type */
3822 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
3823 ptr
= section_ptr_add(data_section
, len
);
3824 memcpy(ptr
, funcname
, len
);
3829 #ifdef TCC_TARGET_PE
3830 t
= VT_SHORT
| VT_UNSIGNED
;
3836 /* string parsing */
3839 if (tcc_state
->warn_write_strings
)
3844 memset(&ad
, 0, sizeof(AttributeDef
));
3845 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, 0);
3850 if (parse_btype(&type
, &ad
)) {
3851 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
3853 /* check ISOC99 compound literal */
3855 /* data is allocated locally by default */
3860 /* all except arrays are lvalues */
3861 if (!(type
.t
& VT_ARRAY
))
3862 r
|= lvalue_type(type
.t
);
3863 memset(&ad
, 0, sizeof(AttributeDef
));
3864 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, 0);
3866 if (sizeof_caller
) {
3873 } else if (tok
== '{') {
3875 tcc_error("expected constant");
3876 /* save all registers */
3879 /* statement expression : we do not accept break/continue
3880 inside as GCC does */
3881 block(NULL
, NULL
, 1);
3896 /* functions names must be treated as function pointers,
3897 except for unary '&' and sizeof. Since we consider that
3898 functions are not lvalues, we only have to handle it
3899 there and in function calls. */
3900 /* arrays can also be used although they are not lvalues */
3901 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
3902 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
3904 mk_pointer(&vtop
->type
);
3910 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3912 boolean
.t
= VT_BOOL
;
3914 vtop
->c
.i
= !vtop
->c
.i
;
3915 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
3919 vseti(VT_JMP
, gvtst(1, 0));
3931 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
3932 tcc_error("pointer not accepted for unary plus");
3933 /* In order to force cast, we add zero, except for floating point
3934 where we really need an noop (otherwise -0.0 will be transformed
3936 if (!is_float(vtop
->type
.t
)) {
3947 unary_type(&type
); // Perform a in_sizeof = 0;
3948 size
= type_size(&type
, &align
);
3949 if (t
== TOK_SIZEOF
) {
3950 if (!(type
.t
& VT_VLA
)) {
3952 tcc_error("sizeof applied to an incomplete type");
3955 vla_runtime_type_size(&type
, &align
);
3960 vtop
->type
.t
|= VT_UNSIGNED
;
3963 case TOK_builtin_expect
:
3965 /* __builtin_expect is a no-op for now */
3966 int saved_nocode_wanted
;
3971 saved_nocode_wanted
= nocode_wanted
;
3975 nocode_wanted
= saved_nocode_wanted
;
3979 case TOK_builtin_types_compatible_p
:
3988 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3989 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3990 vpushi(is_compatible_types(&type1
, &type2
));
3993 case TOK_builtin_constant_p
:
3995 int saved_nocode_wanted
, res
;
3998 saved_nocode_wanted
= nocode_wanted
;
4001 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
4003 nocode_wanted
= saved_nocode_wanted
;
4008 case TOK_builtin_frame_address
:
4009 case TOK_builtin_return_address
:
4016 if (tok
!= TOK_CINT
) {
4017 tcc_error("%s only takes positive integers",
4018 tok1
== TOK_builtin_return_address
?
4019 "__builtin_return_address" :
4020 "__builtin_frame_address");
4022 level
= (uint32_t)tokc
.i
;
4027 vset(&type
, VT_LOCAL
, 0); /* local frame */
4029 mk_pointer(&vtop
->type
);
4030 indir(); /* -> parent frame */
4032 if (tok1
== TOK_builtin_return_address
) {
4033 // assume return address is just above frame pointer on stack
4036 mk_pointer(&vtop
->type
);
4041 #ifdef TCC_TARGET_X86_64
4042 #ifdef TCC_TARGET_PE
4043 case TOK_builtin_va_start
:
4051 if ((vtop
->r
& VT_VALMASK
) != VT_LOCAL
)
4052 tcc_error("__builtin_va_start expects a local variable");
4053 vtop
->r
&= ~(VT_LVAL
| VT_REF
);
4054 vtop
->type
= char_pointer_type
;
4060 case TOK_builtin_va_arg_types
:
4067 vpushi(classify_x86_64_va_arg(&type
));
4073 #ifdef TCC_TARGET_ARM64
4074 case TOK___va_start
: {
4076 tcc_error("statement in global scope");
4086 vtop
->type
.t
= VT_VOID
;
4089 case TOK___va_arg
: {
4092 tcc_error("statement in global scope");
4104 case TOK___arm64_clear_cache
: {
4113 vtop
->type
.t
= VT_VOID
;
4117 /* pre operations */
4128 t
= vtop
->type
.t
& VT_BTYPE
;
4130 /* In IEEE negate(x) isn't subtract(0,x), but rather
4135 else if (t
== VT_DOUBLE
)
4146 goto tok_identifier
;
4148 /* allow to take the address of a label */
4149 if (tok
< TOK_UIDENT
)
4150 expect("label identifier");
4151 s
= label_find(tok
);
4153 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4155 if (s
->r
== LABEL_DECLARED
)
4156 s
->r
= LABEL_FORWARD
;
4159 s
->type
.t
= VT_VOID
;
4160 mk_pointer(&s
->type
);
4161 s
->type
.t
|= VT_STATIC
;
4163 vpushsym(&s
->type
, s
);
4167 // special qnan , snan and infinity values
4169 vpush64(VT_DOUBLE
, 0x7ff8000000000000ULL
);
4173 vpush64(VT_DOUBLE
, 0x7ff0000000000001ULL
);
4177 vpush64(VT_DOUBLE
, 0x7ff0000000000000ULL
);
4186 expect("identifier");
4189 const char *name
= get_tok_str(t
, NULL
);
4191 tcc_error("'%s' undeclared", name
);
4192 /* for simple function calls, we tolerate undeclared
4193 external reference to int() function */
4194 if (tcc_state
->warn_implicit_function_declaration
4195 #ifdef TCC_TARGET_PE
4196 /* people must be warned about using undeclared WINAPI functions
4197 (which usually start with uppercase letter) */
4198 || (name
[0] >= 'A' && name
[0] <= 'Z')
4201 tcc_warning("implicit declaration of function '%s'", name
);
4202 s
= external_global_sym(t
, &func_old_type
, 0);
4204 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
4205 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
4206 /* if referencing an inline function, then we generate a
4207 symbol to it if not already done. It will have the
4208 effect to generate code for it at the end of the
4209 compilation unit. Inline function as always
4210 generated in the text section. */
4212 put_extern_sym(s
, text_section
, 0, 0);
4213 r
= VT_SYM
| VT_CONST
;
4217 vset(&s
->type
, r
, s
->c
);
4218 /* if forward reference, we must point to s */
4219 if (vtop
->r
& VT_SYM
) {
4226 /* post operations */
4228 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
4231 } else if (tok
== '.' || tok
== TOK_ARROW
|| tok
== TOK_CDOUBLE
) {
4234 if (tok
== TOK_ARROW
)
4236 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
4239 /* expect pointer on structure */
4240 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
4241 expect("struct or union");
4242 if (tok
== TOK_CDOUBLE
)
4243 expect("field name");
4245 if (tok
== TOK_CINT
|| tok
== TOK_CUINT
)
4246 expect("field name");
4250 while ((s
= s
->next
) != NULL
) {
4255 tcc_error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, &tokc
));
4256 /* add field offset to pointer */
4257 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
4260 /* change type to field type, and set to lvalue */
4261 vtop
->type
= s
->type
;
4262 vtop
->type
.t
|= qualifiers
;
4263 /* an array is never an lvalue */
4264 if (!(vtop
->type
.t
& VT_ARRAY
)) {
4265 vtop
->r
|= lvalue_type(vtop
->type
.t
);
4266 #ifdef CONFIG_TCC_BCHECK
4267 /* if bound checking, the referenced pointer must be checked */
4268 if (tcc_state
->do_bounds_check
)
4269 vtop
->r
|= VT_MUSTBOUND
;
4273 } else if (tok
== '[') {
4279 } else if (tok
== '(') {
4282 int nb_args
, ret_nregs
, ret_align
, regsize
, variadic
;
4285 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
4286 /* pointer test (no array accepted) */
4287 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
4288 vtop
->type
= *pointed_type(&vtop
->type
);
4289 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
4293 expect("function pointer");
4296 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
4298 /* get return type */
4301 sa
= s
->next
; /* first parameter */
4304 /* compute first implicit argument if a structure is returned */
4305 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
4306 variadic
= (s
->c
== FUNC_ELLIPSIS
);
4307 ret_nregs
= gfunc_sret(&s
->type
, variadic
, &ret
.type
,
4308 &ret_align
, ®size
);
4310 /* get some space for the returned structure */
4311 size
= type_size(&s
->type
, &align
);
4312 #ifdef TCC_TARGET_ARM64
4313 /* On arm64, a small struct is return in registers.
4314 It is much easier to write it to memory if we know
4315 that we are allowed to write some extra bytes, so
4316 round the allocated space up to a power of 2: */
4318 while (size
& (size
- 1))
4319 size
= (size
| (size
- 1)) + 1;
4321 loc
= (loc
- size
) & -align
;
4323 ret
.r
= VT_LOCAL
| VT_LVAL
;
4324 /* pass it as 'int' to avoid structure arg passing
4326 vseti(VT_LOCAL
, loc
);
4336 /* return in register */
4337 if (is_float(ret
.type
.t
)) {
4338 ret
.r
= reg_fret(ret
.type
.t
);
4339 #ifdef TCC_TARGET_X86_64
4340 if ((ret
.type
.t
& VT_BTYPE
) == VT_QFLOAT
)
4344 #ifndef TCC_TARGET_ARM64
4345 #ifdef TCC_TARGET_X86_64
4346 if ((ret
.type
.t
& VT_BTYPE
) == VT_QLONG
)
4348 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
4359 gfunc_param_typed(s
, sa
);
4369 tcc_error("too few arguments to function");
4371 if (!nocode_wanted
) {
4372 gfunc_call(nb_args
);
4374 vtop
-= (nb_args
+ 1);
4378 for (r
= ret
.r
+ ret_nregs
+ !ret_nregs
; r
-- > ret
.r
;) {
4379 vsetc(&ret
.type
, r
, &ret
.c
);
4380 vtop
->r2
= ret
.r2
; /* Loop only happens when r2 is VT_CONST */
4383 /* handle packed struct return */
4384 if (((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) && ret_nregs
) {
4387 size
= type_size(&s
->type
, &align
);
4388 /* We're writing whole regs often, make sure there's enough
4389 space. Assume register size is power of 2. */
4390 if (regsize
> align
)
4392 loc
= (loc
- size
) & -align
;
4396 vset(&ret
.type
, VT_LOCAL
| VT_LVAL
, addr
+ offset
);
4400 if (--ret_nregs
== 0)
4404 vset(&s
->type
, VT_LOCAL
| VT_LVAL
, addr
);
4412 ST_FUNC
void expr_prod(void)
4417 while (tok
== '*' || tok
== '/' || tok
== '%') {
4425 ST_FUNC
void expr_sum(void)
4430 while (tok
== '+' || tok
== '-') {
4438 static void expr_shift(void)
4443 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
4451 static void expr_cmp(void)
4456 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
4457 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
4465 static void expr_cmpeq(void)
4470 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
4478 static void expr_and(void)
4481 while (tok
== '&') {
4488 static void expr_xor(void)
4491 while (tok
== '^') {
4498 static void expr_or(void)
4501 while (tok
== '|') {
4508 /* XXX: fix this mess */
4509 static void expr_land_const(void)
4512 while (tok
== TOK_LAND
) {
4518 static void expr_lor_const(void)
4521 while (tok
== TOK_LOR
) {
4528 static void expr_land(void)
4531 if (tok
== TOK_LAND
) {
4532 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4543 int saved_nocode_wanted
= nocode_wanted
;
4547 nocode_wanted
= saved_nocode_wanted
;
4555 if (tok
!= TOK_LAND
) {
4566 static void expr_lor(void)
4569 if (tok
== TOK_LOR
) {
4570 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4577 int saved_nocode_wanted
= nocode_wanted
;
4581 nocode_wanted
= saved_nocode_wanted
;
4593 if (tok
!= TOK_LOR
) {
4604 static void expr_cond(void)
4606 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
, islv
;
4608 CType type
, type1
, type2
;
4613 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4614 int saved_nocode_wanted
= nocode_wanted
;
4617 boolean
.t
= VT_BOOL
;
4623 if (tok
!= ':' || !gnu_ext
) {
4631 nocode_wanted
= saved_nocode_wanted
;
4634 if (tok
!= ':' || !gnu_ext
) {
4638 nocode_wanted
= saved_nocode_wanted
;
4645 if (vtop
!= vstack
) {
4646 /* needed to avoid having different registers saved in
4648 if (is_float(vtop
->type
.t
)) {
4650 #ifdef TCC_TARGET_X86_64
4651 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4661 if (tok
== ':' && gnu_ext
) {
4669 sv
= *vtop
; /* save value to handle it later */
4670 vtop
--; /* no vpop so that FP stack is not flushed */
4678 bt1
= t1
& VT_BTYPE
;
4680 bt2
= t2
& VT_BTYPE
;
4681 /* cast operands to correct type according to ISOC rules */
4682 if (is_float(bt1
) || is_float(bt2
)) {
4683 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
4684 type
.t
= VT_LDOUBLE
;
4685 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
4690 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
4691 /* cast to biggest op */
4693 /* convert to unsigned if it does not fit in a long long */
4694 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
4695 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
4696 type
.t
|= VT_UNSIGNED
;
4697 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
4698 /* If one is a null ptr constant the result type
4700 if (is_null_pointer (vtop
))
4702 else if (is_null_pointer (&sv
))
4704 /* XXX: test pointer compatibility, C99 has more elaborate
4708 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
4709 /* XXX: test function pointer compatibility */
4710 type
= bt1
== VT_FUNC
? type1
: type2
;
4711 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
4712 /* XXX: test structure compatibility */
4713 type
= bt1
== VT_STRUCT
? type1
: type2
;
4714 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
4715 /* NOTE: as an extension, we accept void on only one side */
4718 /* integer operations */
4720 /* convert to unsigned if it does not fit in an integer */
4721 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
4722 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
4723 type
.t
|= VT_UNSIGNED
;
4725 /* keep structs lvalue by transforming `(expr ? a : b)` to `*(expr ? &a : &b)` so
4726 that `(expr ? a : b).mem` does not error with "lvalue expected" */
4727 islv
= (vtop
->r
& VT_LVAL
) && (sv
.r
& VT_LVAL
) && VT_STRUCT
== (type
.t
& VT_BTYPE
);
4729 /* now we convert second operand */
4732 mk_pointer(&vtop
->type
);
4735 else if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4738 if (is_float(type
.t
)) {
4740 #ifdef TCC_TARGET_X86_64
4741 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4745 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
4746 /* for long longs, we use fixed registers to avoid having
4747 to handle a complicated move */
4752 /* this is horrible, but we must also convert first
4756 /* put again first value and cast it */
4760 mk_pointer(&vtop
->type
);
4763 else if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4766 move_reg(r2
, r1
, type
.t
);
4775 static void expr_eq(void)
4781 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
4782 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
4783 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
4798 ST_FUNC
void gexpr(void)
4809 /* parse an expression and return its type without any side effect. */
4810 static void expr_type(CType
*type
)
4812 int saved_nocode_wanted
;
4814 saved_nocode_wanted
= nocode_wanted
;
4819 nocode_wanted
= saved_nocode_wanted
;
4822 /* parse a unary expression and return its type without any side
4824 static void unary_type(CType
*type
)
4836 /* parse a constant expression and return value in vtop. */
4837 static void expr_const1(void)
4846 /* parse an integer constant and return its value. */
4847 ST_FUNC
int expr_const(void)
4851 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
4852 expect("constant expression");
4858 /* return the label token if current token is a label, otherwise
4860 static int is_label(void)
4864 /* fast test first */
4865 if (tok
< TOK_UIDENT
)
4867 /* no need to save tokc because tok is an identifier */
4874 unget_tok(last_tok
);
4879 static void label_or_decl(int l
)
4883 /* fast test first */
4884 if (tok
>= TOK_UIDENT
)
4886 /* no need to save tokc because tok is an identifier */
4890 unget_tok(last_tok
);
4893 unget_tok(last_tok
);
4898 static int case_cmp(const void *pa
, const void *pb
)
4900 int a
= (*(struct case_t
**) pa
)->v1
;
4901 int b
= (*(struct case_t
**) pb
)->v1
;
4902 return a
< b
? -1 : a
> b
;
4905 static int gcase(struct case_t
**base
, int len
, int case_reg
, int *bsym
)
4917 case_reg
= gv(RC_INT
);
4923 gtst_addr(0, p
->sym
); /* v1 <= x <= v2 */
4924 case_reg
= gv(RC_INT
);
4927 case_reg
= gcase(base
, len
/2, case_reg
, bsym
);
4928 if (cur_switch
->def_sym
)
4929 gjmp_addr(cur_switch
->def_sym
);
4931 *bsym
= gjmp(*bsym
);
4935 base
+= e
; len
-= e
;
4943 if (p
->v1
== p
->v2
) {
4945 gtst_addr(0, p
->sym
);
4949 case_reg
= gv(RC_INT
);
4955 gtst_addr(0, p
->sym
);
4958 case_reg
= gv(RC_INT
);
4964 static void block(int *bsym
, int *csym
, int is_expr
)
4969 /* generate line number info */
4970 if (tcc_state
->do_debug
&&
4971 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
4972 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
4974 last_line_num
= file
->line_num
;
4978 /* default return value is (void) */
4980 vtop
->type
.t
= VT_VOID
;
4983 if (tok
== TOK_IF
) {
4990 block(bsym
, csym
, 0);
4992 if (c
== TOK_ELSE
) {
4996 block(bsym
, csym
, 0);
4997 gsym(d
); /* patch else jmp */
5000 } else if (tok
== TOK_WHILE
) {
5016 } else if (tok
== '{') {
5018 int block_vla_sp_loc
= vla_sp_loc
, saved_vlas_in_scope
= vlas_in_scope
;
5021 /* record local declaration stack position */
5023 llabel
= local_label_stack
;
5026 /* handle local labels declarations */
5027 if (tok
== TOK_LABEL
) {
5030 if (tok
< TOK_UIDENT
)
5031 expect("label identifier");
5032 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
5042 while (tok
!= '}') {
5043 label_or_decl(VT_LOCAL
);
5047 block(bsym
, csym
, is_expr
);
5050 /* pop locally defined labels */
5051 label_pop(&local_label_stack
, llabel
);
5053 /* XXX: this solution makes only valgrind happy...
5054 triggered by gcc.c-torture/execute/20000917-1.c */
5056 switch(vtop
->type
.t
& VT_BTYPE
) {
5058 /* this breaks a compilation of the linux kernel v2.4.26 */
5059 /* pmd_t *new = ({ __asm__ __volatile__("ud2\n") ; ((pmd_t *)1); }); */
5060 /* Look a commit a80acab: Display error on statement expressions with complex return type */
5061 /* A pointer is not a complex return type */
5065 for(p
=vtop
->type
.ref
;p
;p
=p
->prev
)
5067 tcc_error("unsupported expression type");
5070 /* pop locally defined symbols */
5072 sym_pop(&local_stack
, s
);
5074 /* Pop VLA frames and restore stack pointer if required */
5075 if (vlas_in_scope
> saved_vlas_in_scope
) {
5076 vla_sp_loc
= saved_vlas_in_scope
? block_vla_sp_loc
: vla_sp_root_loc
;
5079 vlas_in_scope
= saved_vlas_in_scope
;
5082 } else if (tok
== TOK_RETURN
) {
5086 gen_assign_cast(&func_vt
);
5087 #ifdef TCC_TARGET_ARM64
5088 // Perhaps it would be better to use this for all backends:
5091 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
5092 CType type
, ret_type
;
5093 int ret_align
, ret_nregs
, regsize
;
5094 ret_nregs
= gfunc_sret(&func_vt
, func_var
, &ret_type
,
5095 &ret_align
, ®size
);
5096 if (0 == ret_nregs
) {
5097 /* if returning structure, must copy it to implicit
5098 first pointer arg location */
5101 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
5104 /* copy structure value to pointer */
5107 /* returning structure packed into registers */
5108 int r
, size
, addr
, align
;
5109 size
= type_size(&func_vt
,&align
);
5110 if ((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) ||
5111 (vtop
->c
.i
& (ret_align
-1)))
5112 && (align
& (ret_align
-1))) {
5113 loc
= (loc
- size
) & -ret_align
;
5116 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
5120 vset(&ret_type
, VT_LOCAL
| VT_LVAL
, addr
);
5122 vtop
->type
= ret_type
;
5123 if (is_float(ret_type
.t
))
5124 r
= rc_fret(ret_type
.t
);
5135 if (--ret_nregs
== 0)
5137 /* We assume that when a structure is returned in multiple
5138 registers, their classes are consecutive values of the
5141 vtop
->c
.i
+= regsize
;
5145 } else if (is_float(func_vt
.t
)) {
5146 gv(rc_fret(func_vt
.t
));
5151 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
5154 /* jump unless last stmt in top-level block */
5155 if (tok
!= '}' || local_scope
!= 1)
5157 } else if (tok
== TOK_BREAK
) {
5160 tcc_error("cannot break");
5161 *bsym
= gjmp(*bsym
);
5164 } else if (tok
== TOK_CONTINUE
) {
5167 tcc_error("cannot continue");
5168 vla_sp_restore_root();
5169 *csym
= gjmp(*csym
);
5172 } else if (tok
== TOK_FOR
) {
5179 /* c99 for-loop init decl? */
5180 if (!decl0(VT_LOCAL
, 1)) {
5181 /* no, regular for-loop init expr */
5213 sym_pop(&local_stack
, s
);
5216 if (tok
== TOK_DO
) {
5233 if (tok
== TOK_SWITCH
) {
5234 struct switch_t
*saved
, sw
;
5238 /* XXX: other types than integer */
5243 b
= gjmp(0); /* jump to first case */
5244 sw
.p
= NULL
; sw
.n
= 0; sw
.def_sym
= 0;
5248 a
= gjmp(a
); /* add implicit break */
5251 qsort(sw
.p
, sw
.n
, sizeof(void*), case_cmp
);
5252 for (b
= 1; b
< sw
.n
; b
++)
5253 if (sw
.p
[b
- 1]->v2
>= sw
.p
[b
]->v1
)
5254 tcc_error("duplicate case value");
5255 gcase(sw
.p
, sw
.n
, c
, &a
);
5257 gjmp_addr(sw
.def_sym
);
5258 dynarray_reset(&sw
.p
, &sw
.n
);
5263 if (tok
== TOK_CASE
) {
5264 struct case_t
*cr
= tcc_malloc(sizeof(struct case_t
));
5268 cr
->v1
= cr
->v2
= expr_const();
5269 if (gnu_ext
&& tok
== TOK_DOTS
) {
5271 cr
->v2
= expr_const();
5272 if (cr
->v2
< cr
->v1
)
5273 tcc_warning("empty case range");
5276 dynarray_add((void***) &cur_switch
->p
, &cur_switch
->n
, cr
);
5279 goto block_after_label
;
5281 if (tok
== TOK_DEFAULT
) {
5286 if (cur_switch
->def_sym
)
5287 tcc_error("too many 'default'");
5288 cur_switch
->def_sym
= ind
;
5290 goto block_after_label
;
5292 if (tok
== TOK_GOTO
) {
5294 if (tok
== '*' && gnu_ext
) {
5298 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
5301 } else if (tok
>= TOK_UIDENT
) {
5302 s
= label_find(tok
);
5303 /* put forward definition if needed */
5305 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
5307 if (s
->r
== LABEL_DECLARED
)
5308 s
->r
= LABEL_FORWARD
;
5310 vla_sp_restore_root();
5311 if (s
->r
& LABEL_FORWARD
)
5312 s
->jnext
= gjmp(s
->jnext
);
5314 gjmp_addr(s
->jnext
);
5317 expect("label identifier");
5320 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
5328 if (s
->r
== LABEL_DEFINED
)
5329 tcc_error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
5331 s
->r
= LABEL_DEFINED
;
5333 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
5337 /* we accept this, but it is a mistake */
5340 tcc_warning("deprecated use of label at end of compound statement");
5344 block(bsym
, csym
, is_expr
);
5347 /* expression case */
5362 /* t is the array or struct type. c is the array or struct
5363 address. cur_index/cur_field is the pointer to the current
5364 value. 'size_only' is true if only size info is needed (only used
5366 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
5367 int *cur_index
, Sym
**cur_field
,
5371 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
5377 if (gnu_ext
&& (l
= is_label()) != 0)
5379 while (tok
== '[' || tok
== '.') {
5381 if (!(type
->t
& VT_ARRAY
))
5382 expect("array type");
5385 index
= expr_const();
5386 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
5387 expect("invalid index");
5388 if (tok
== TOK_DOTS
&& gnu_ext
) {
5390 index_last
= expr_const();
5391 if (index_last
< 0 ||
5392 (s
->c
>= 0 && index_last
>= s
->c
) ||
5394 expect("invalid index");
5400 *cur_index
= index_last
;
5401 type
= pointed_type(type
);
5402 elem_size
= type_size(type
, &align
);
5403 c
+= index
* elem_size
;
5404 /* NOTE: we only support ranges for last designator */
5405 nb_elems
= index_last
- index
+ 1;
5406 if (nb_elems
!= 1) {
5415 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
5416 expect("struct/union type");
5429 /* XXX: fix this mess by using explicit storage field */
5431 type1
.t
|= (type
->t
& ~VT_TYPE
);
5445 if (type
->t
& VT_ARRAY
) {
5447 type
= pointed_type(type
);
5448 c
+= index
* type_size(type
, &align
);
5452 tcc_error("too many field init");
5453 /* XXX: fix this mess by using explicit storage field */
5455 type1
.t
|= (type
->t
& ~VT_TYPE
);
5460 decl_initializer(type
, sec
, c
, 0, size_only
);
5462 /* XXX: make it more general */
5463 if (!size_only
&& nb_elems
> 1) {
5464 unsigned long c_end
;
5469 tcc_error("range init not supported yet for dynamic storage");
5470 c_end
= c
+ nb_elems
* elem_size
;
5471 if (c_end
> sec
->data_allocated
)
5472 section_realloc(sec
, c_end
);
5473 src
= sec
->data
+ c
;
5475 for(i
= 1; i
< nb_elems
; i
++) {
5477 memcpy(dst
, src
, elem_size
);
5483 #define EXPR_CONST 1
5486 /* store a value or an expression directly in global data or in local array */
5487 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
5488 int v
, int expr_type
)
5490 int saved_global_expr
, bt
, bit_pos
, bit_size
;
5492 unsigned long long bit_mask
;
5500 /* compound literals must be allocated globally in this case */
5501 saved_global_expr
= global_expr
;
5504 global_expr
= saved_global_expr
;
5505 /* NOTE: symbols are accepted */
5506 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
5507 tcc_error("initializer element is not constant");
5515 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
5518 /* XXX: not portable */
5519 /* XXX: generate error if incorrect relocation */
5520 gen_assign_cast(&dtype
);
5521 bt
= type
->t
& VT_BTYPE
;
5522 /* we'll write at most 16 bytes */
5523 if (c
+ 16 > sec
->data_allocated
) {
5524 section_realloc(sec
, c
+ 16);
5526 ptr
= sec
->data
+ c
;
5527 /* XXX: make code faster ? */
5528 if (!(type
->t
& VT_BITFIELD
)) {
5533 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5534 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
5535 bit_mask
= (1LL << bit_size
) - 1;
5537 if ((vtop
->r
& VT_SYM
) &&
5543 (bt
== VT_INT
&& bit_size
!= 32)))
5544 tcc_error("initializer element is not computable at load time");
5546 /* XXX: when cross-compiling we assume that each type has the
5547 same representation on host and target, which is likely to
5548 be wrong in the case of long double */
5550 vtop
->c
.i
= (vtop
->c
.i
!= 0);
5552 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5555 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5558 *(double *)ptr
= vtop
->c
.d
;
5561 if (sizeof(long double) == LDOUBLE_SIZE
)
5562 *(long double *)ptr
= vtop
->c
.ld
;
5563 else if (sizeof(double) == LDOUBLE_SIZE
)
5564 *(double *)ptr
= vtop
->c
.ld
;
5566 tcc_error("can't cross compile long double constants");
5569 *(long long *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5572 addr_t val
= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5573 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
5574 if (vtop
->r
& VT_SYM
)
5575 greloca(sec
, vtop
->sym
, c
, R_DATA_PTR
, val
);
5577 *(addr_t
*)ptr
|= val
;
5579 if (vtop
->r
& VT_SYM
)
5580 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
5581 *(addr_t
*)ptr
|= val
;
5586 int val
= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5587 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
5588 if (vtop
->r
& VT_SYM
)
5589 greloca(sec
, vtop
->sym
, c
, R_DATA_PTR
, val
);
5593 if (vtop
->r
& VT_SYM
)
5594 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
5602 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
5609 /* put zeros for variable based init */
5610 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
5613 /* nothing to do because globals are already set to zero */
5615 vpush_global_sym(&func_old_type
, TOK_memset
);
5617 #ifdef TCC_TARGET_ARM
5628 /* 't' contains the type and storage info. 'c' is the offset of the
5629 object in section 'sec'. If 'sec' is NULL, it means stack based
5630 allocation. 'first' is true if array '{' must be read (multi
5631 dimension implicit array init handling). 'size_only' is true if
5632 size only evaluation is wanted (only for arrays). */
5633 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
5634 int first
, int size_only
)
5636 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, parlevel1
, i
;
5637 int size1
, align1
, expr_type
;
5641 if (type
->t
& VT_VLA
) {
5644 /* save current stack pointer */
5645 if (vlas_in_scope
== 0) {
5646 if (vla_sp_root_loc
== -1)
5647 vla_sp_root_loc
= (loc
-= PTR_SIZE
);
5648 gen_vla_sp_save(vla_sp_root_loc
);
5651 vla_runtime_type_size(type
, &a
);
5652 gen_vla_alloc(type
, a
);
5656 } else if (type
->t
& VT_ARRAY
) {
5660 t1
= pointed_type(type
);
5661 size1
= type_size(t1
, &align1
);
5664 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
5667 tcc_error("character array initializer must be a literal,"
5668 " optionally enclosed in braces");
5673 /* only parse strings here if correct type (otherwise: handle
5674 them as ((w)char *) expressions */
5675 if ((tok
== TOK_LSTR
&&
5676 #ifdef TCC_TARGET_PE
5677 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
5679 (t1
->t
& VT_BTYPE
) == VT_INT
5681 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
5682 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5685 /* compute maximum number of chars wanted */
5687 cstr_len
= tokc
.str
.size
;
5689 cstr_len
= tokc
.str
.size
/ sizeof(nwchar_t
);
5692 if (n
>= 0 && nb
> (n
- array_length
))
5693 nb
= n
- array_length
;
5696 tcc_warning("initializer-string for array is too long");
5697 /* in order to go faster for common case (char
5698 string in global variable, we handle it
5700 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
5701 memcpy(sec
->data
+ c
+ array_length
, tokc
.str
.data
, nb
);
5705 ch
= ((unsigned char *)tokc
.str
.data
)[i
];
5707 ch
= ((nwchar_t
*)tokc
.str
.data
)[i
];
5708 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
5716 /* only add trailing zero if enough storage (no
5717 warning in this case since it is standard) */
5718 if (n
< 0 || array_length
< n
) {
5720 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
5726 while (tok
!= '}') {
5727 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
5728 if (n
>= 0 && index
>= n
)
5729 tcc_error("index too large");
5730 /* must put zero in holes (note that doing it that way
5731 ensures that it even works with designators) */
5732 if (!size_only
&& array_length
< index
) {
5733 init_putz(t1
, sec
, c
+ array_length
* size1
,
5734 (index
- array_length
) * size1
);
5737 if (index
> array_length
)
5738 array_length
= index
;
5739 /* special test for multi dimensional arrays (may not
5740 be strictly correct if designators are used at the
5742 if (index
>= n
&& no_oblock
)
5751 /* put zeros at the end */
5752 if (!size_only
&& n
>= 0 && array_length
< n
) {
5753 init_putz(t1
, sec
, c
+ array_length
* size1
,
5754 (n
- array_length
) * size1
);
5756 /* patch type size if needed */
5758 s
->c
= array_length
;
5759 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
5760 (sec
|| !first
|| tok
== '{')) {
5762 /* NOTE: the previous test is a specific case for automatic
5763 struct/union init */
5764 /* XXX: union needs only one init */
5771 if (tcc_state
->old_struct_init_code
) {
5772 /* an old version of struct initialization.
5773 It have a problems. But with a new version
5774 linux 2.4.26 can't load ramdisk.
5776 while (tok
== '(') {
5780 if (!parse_btype(&type1
, &ad1
))
5782 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
5784 if (!is_assignable_types(type
, &type1
))
5785 tcc_error("invalid type for cast");
5792 if (!parse_btype(&type1
, &ad1
))
5794 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
5796 if (!is_assignable_types(type
, &type1
))
5797 tcc_error("invalid type for cast");
5806 if (first
|| tok
== '{') {
5815 while (tok
!= '}') {
5816 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
5818 if (!size_only
&& array_length
< index
) {
5819 init_putz(type
, sec
, c
+ array_length
,
5820 index
- array_length
);
5822 index
= index
+ type_size(&f
->type
, &align1
);
5823 if (index
> array_length
)
5824 array_length
= index
;
5826 /* gr: skip fields from same union - ugly. */
5829 int f_size
= type_size(&f
->type
, &align
);
5830 int f_type
= (f
->type
.t
& VT_BTYPE
);
5832 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
5833 /* test for same offset */
5834 if (f
->next
->c
!= f
->c
)
5836 if ((f_type
== VT_STRUCT
) && (f_size
== 0)) {
5838 Lets assume a structure of size 0 can't be a member of the union.
5839 This allow to compile the following code from a linux kernel v2.4.26
5840 typedef struct { } rwlock_t;
5846 struct fs_struct init_fs = { { (1) }, (rwlock_t) {}, 0022, };
5847 tcc-0.9.23 can succesfully compile this version of the kernel.
5848 gcc don't have problems with this code too.
5852 /* if yes, test for bitfield shift */
5853 if ((f
->type
.t
& VT_BITFIELD
) && (f
->next
->type
.t
& VT_BITFIELD
)) {
5854 int bit_pos_1
= (f
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5855 int bit_pos_2
= (f
->next
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5856 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
5857 if (bit_pos_1
!= bit_pos_2
)
5864 if (no_oblock
&& f
== NULL
)
5870 /* put zeros at the end */
5871 if (!size_only
&& array_length
< n
) {
5872 init_putz(type
, sec
, c
+ array_length
,
5881 } else if (tok
== '{') {
5883 decl_initializer(type
, sec
, c
, first
, size_only
);
5885 } else if (size_only
) {
5886 /* just skip expression */
5887 parlevel
= parlevel1
= 0;
5888 while ((parlevel
> 0 || parlevel1
> 0 ||
5889 (tok
!= '}' && tok
!= ',')) && tok
!= -1) {
5892 else if (tok
== ')') {
5893 if (parlevel
== 0 && parlevel1
== 0)
5897 else if (tok
== '{')
5899 else if (tok
== '}') {
5900 if (parlevel
== 0 && parlevel1
== 0)
5907 /* currently, we always use constant expression for globals
5908 (may change for scripting case) */
5909 expr_type
= EXPR_CONST
;
5911 expr_type
= EXPR_ANY
;
5912 init_putv(type
, sec
, c
, 0, expr_type
);
5916 /* parse an initializer for type 't' if 'has_init' is non zero, and
5917 allocate space in local or global data space ('r' is either
5918 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5919 variable 'v' of scope 'scope' is declared before initializers
5920 are parsed. If 'v' is zero, then a reference to the new object
5921 is put in the value stack. If 'has_init' is 2, a special parsing
5922 is done to handle string constants. */
5923 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
5924 int has_init
, int v
, int scope
)
5926 int size
, align
, addr
, data_offset
;
5928 ParseState saved_parse_state
= {0};
5929 TokenString
*init_str
= NULL
;
5931 Sym
*flexible_array
;
5933 flexible_array
= NULL
;
5934 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
5935 Sym
*field
= type
->ref
->next
;
5938 field
= field
->next
;
5939 if (field
->type
.t
& VT_ARRAY
&& field
->type
.ref
->c
< 0)
5940 flexible_array
= field
;
5944 size
= type_size(type
, &align
);
5945 /* If unknown size, we must evaluate it before
5946 evaluating initializers because
5947 initializers can generate global data too
5948 (e.g. string pointers or ISOC99 compound
5949 literals). It also simplifies local
5950 initializers handling */
5951 if (size
< 0 || (flexible_array
&& has_init
)) {
5953 tcc_error("unknown type size");
5954 /* get all init string */
5955 init_str
= tok_str_alloc();
5956 if (has_init
== 2) {
5957 /* only get strings */
5958 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5959 tok_str_add_tok(init_str
);
5964 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
5966 tcc_error("unexpected end of file in initializer");
5967 tok_str_add_tok(init_str
);
5970 else if (tok
== '}') {
5980 tok_str_add(init_str
, -1);
5981 tok_str_add(init_str
, 0);
5984 save_parse_state(&saved_parse_state
);
5986 begin_macro(init_str
, 1);
5988 decl_initializer(type
, NULL
, 0, 1, 1);
5989 /* prepare second initializer parsing */
5990 macro_ptr
= init_str
->str
;
5993 /* if still unknown size, error */
5994 size
= type_size(type
, &align
);
5996 tcc_error("unknown type size");
5998 /* If there's a flex member and it was used in the initializer
6000 if (flexible_array
&&
6001 flexible_array
->type
.ref
->c
> 0)
6002 size
+= flexible_array
->type
.ref
->c
6003 * pointed_size(&flexible_array
->type
);
6004 /* take into account specified alignment if bigger */
6005 if (ad
->a
.aligned
) {
6006 if (ad
->a
.aligned
> align
)
6007 align
= ad
->a
.aligned
;
6008 } else if (ad
->a
.packed
) {
6011 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
6013 #ifdef CONFIG_TCC_BCHECK
6014 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
6018 loc
= (loc
- size
) & -align
;
6020 #ifdef CONFIG_TCC_BCHECK
6021 /* handles bounds */
6022 /* XXX: currently, since we do only one pass, we cannot track
6023 '&' operators, so we add only arrays */
6024 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
6026 /* add padding between regions */
6028 /* then add local bound info */
6029 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(addr_t
));
6030 bounds_ptr
[0] = addr
;
6031 bounds_ptr
[1] = size
;
6035 /* local variable */
6036 sym_push(v
, type
, r
, addr
);
6038 /* push local reference */
6039 vset(type
, r
, addr
);
6045 if (v
&& scope
== VT_CONST
) {
6046 /* see if the symbol was already defined */
6049 if (!is_compatible_types(&sym
->type
, type
))
6050 tcc_error("incompatible types for redefinition of '%s'",
6051 get_tok_str(v
, NULL
));
6052 if (sym
->type
.t
& VT_EXTERN
) {
6053 /* if the variable is extern, it was not allocated */
6054 sym
->type
.t
&= ~VT_EXTERN
;
6055 /* set array size if it was omitted in extern
6057 if ((sym
->type
.t
& VT_ARRAY
) &&
6058 sym
->type
.ref
->c
< 0 &&
6060 sym
->type
.ref
->c
= type
->ref
->c
;
6062 /* we accept several definitions of the same
6063 global variable. this is tricky, because we
6064 must play with the SHN_COMMON type of the symbol */
6065 /* XXX: should check if the variable was already
6066 initialized. It is incorrect to initialized it
6068 /* no init data, we won't add more to the symbol */
6075 /* allocate symbol in corresponding section */
6080 else if (tcc_state
->nocommon
)
6084 data_offset
= sec
->data_offset
;
6085 data_offset
= (data_offset
+ align
- 1) & -align
;
6087 /* very important to increment global pointer at this time
6088 because initializers themselves can create new initializers */
6089 data_offset
+= size
;
6090 #ifdef CONFIG_TCC_BCHECK
6091 /* add padding if bound check */
6092 if (tcc_state
->do_bounds_check
)
6095 sec
->data_offset
= data_offset
;
6096 /* allocate section space to put the data */
6097 if (sec
->sh_type
!= SHT_NOBITS
&&
6098 data_offset
> sec
->data_allocated
)
6099 section_realloc(sec
, data_offset
);
6100 /* align section if needed */
6101 if (align
> sec
->sh_addralign
)
6102 sec
->sh_addralign
= align
;
6104 addr
= 0; /* avoid warning */
6108 if (scope
!= VT_CONST
|| !sym
) {
6109 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
6110 sym
->asm_label
= ad
->asm_label
;
6112 /* update symbol definition */
6114 put_extern_sym(sym
, sec
, addr
, size
);
6117 /* put a common area */
6118 put_extern_sym(sym
, NULL
, align
, size
);
6119 /* XXX: find a nicer way */
6120 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
6121 esym
->st_shndx
= SHN_COMMON
;
6124 /* push global reference */
6125 sym
= get_sym_ref(type
, sec
, addr
, size
);
6126 vpushsym(type
, sym
);
6128 /* patch symbol weakness */
6129 if (type
->t
& VT_WEAK
)
6131 apply_visibility(sym
, type
);
6132 #ifdef CONFIG_TCC_BCHECK
6133 /* handles bounds now because the symbol must be defined
6134 before for the relocation */
6135 if (tcc_state
->do_bounds_check
) {
6138 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
6139 /* then add global bound info */
6140 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(addr_t
));
6141 bounds_ptr
[0] = 0; /* relocated */
6142 bounds_ptr
[1] = size
;
6146 if (has_init
|| (type
->t
& VT_VLA
)) {
6147 decl_initializer(type
, sec
, addr
, 1, 0);
6148 /* patch flexible array member size back to -1, */
6149 /* for possible subsequent similar declarations */
6151 flexible_array
->type
.ref
->c
= -1;
6154 /* restore parse state if needed */
6157 restore_parse_state(&saved_parse_state
);
6161 static void put_func_debug(Sym
*sym
)
6166 /* XXX: we put here a dummy type */
6167 snprintf(buf
, sizeof(buf
), "%s:%c1",
6168 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
6169 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
6170 cur_text_section
, sym
->c
);
6171 /* //gr gdb wants a line at the function */
6172 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
6177 /* parse an old style function declaration list */
6178 /* XXX: check multiple parameter */
6179 static void func_decl_list(Sym
*func_sym
)
6186 /* parse each declaration */
6187 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
&&
6188 tok
!= TOK_ASM1
&& tok
!= TOK_ASM2
&& tok
!= TOK_ASM3
) {
6189 if (!parse_btype(&btype
, &ad
))
6190 expect("declaration list");
6191 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
6192 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
6194 /* we accept no variable after */
6198 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
6199 /* find parameter in function parameter list */
6202 if ((s
->v
& ~SYM_FIELD
) == v
)
6206 tcc_error("declaration for parameter '%s' but no such parameter",
6207 get_tok_str(v
, NULL
));
6209 /* check that no storage specifier except 'register' was given */
6210 if (type
.t
& VT_STORAGE
)
6211 tcc_error("storage class specified for '%s'", get_tok_str(v
, NULL
));
6212 convert_parameter_type(&type
);
6213 /* we can add the type (NOTE: it could be local to the function) */
6215 /* accept other parameters */
6226 /* parse a function defined by symbol 'sym' and generate its code in
6227 'cur_text_section' */
6228 static void gen_function(Sym
*sym
)
6230 int saved_nocode_wanted
= nocode_wanted
;
6233 ind
= cur_text_section
->data_offset
;
6234 /* NOTE: we patch the symbol size later */
6235 put_extern_sym(sym
, cur_text_section
, ind
, 0);
6236 funcname
= get_tok_str(sym
->v
, NULL
);
6238 /* Initialize VLA state */
6240 vla_sp_root_loc
= -1;
6241 /* put debug symbol */
6242 if (tcc_state
->do_debug
)
6243 put_func_debug(sym
);
6245 /* push a dummy symbol to enable local sym storage */
6246 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
6247 local_scope
= 1; /* for function parameters */
6248 gfunc_prolog(&sym
->type
);
6252 block(NULL
, NULL
, 0);
6255 cur_text_section
->data_offset
= ind
;
6256 label_pop(&global_label_stack
, NULL
);
6257 /* reset local stack */
6259 sym_pop(&local_stack
, NULL
);
6260 /* end of function */
6261 /* patch symbol size */
6262 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
6264 /* patch symbol weakness (this definition overrules any prototype) */
6265 if (sym
->type
.t
& VT_WEAK
)
6267 apply_visibility(sym
, &sym
->type
);
6268 if (tcc_state
->do_debug
) {
6269 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
6271 /* It's better to crash than to generate wrong code */
6272 cur_text_section
= NULL
;
6273 funcname
= ""; /* for safety */
6274 func_vt
.t
= VT_VOID
; /* for safety */
6275 func_var
= 0; /* for safety */
6276 ind
= 0; /* for safety */
6277 nocode_wanted
= saved_nocode_wanted
;
6281 ST_FUNC
void gen_inline_functions(void)
6284 int inline_generated
, i
, ln
;
6285 struct InlineFunc
*fn
;
6287 ln
= file
->line_num
;
6288 /* iterate while inline function are referenced */
6290 inline_generated
= 0;
6291 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
6292 fn
= tcc_state
->inline_fns
[i
];
6294 if (sym
&& sym
->c
) {
6295 /* the function was used: generate its code and
6296 convert it to a normal function */
6299 pstrcpy(file
->filename
, sizeof file
->filename
, fn
->filename
);
6300 sym
->r
= VT_SYM
| VT_CONST
;
6301 sym
->type
.t
&= ~VT_INLINE
;
6303 begin_macro(&fn
->func_str
, 0);
6305 cur_text_section
= text_section
;
6309 inline_generated
= 1;
6312 if (!inline_generated
)
6315 file
->line_num
= ln
;
6316 /* free tokens of unused inline functions */
6317 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
6318 fn
= tcc_state
->inline_fns
[i
];
6320 tok_str_free(fn
->func_str
.str
);
6322 dynarray_reset(&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
);
6325 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
6326 static int decl0(int l
, int is_for_loop_init
)
6334 if (!parse_btype(&btype
, &ad
)) {
6335 if (is_for_loop_init
)
6337 /* skip redundant ';' */
6338 /* XXX: find more elegant solution */
6343 if (l
== VT_CONST
&&
6344 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
6345 /* global asm block */
6349 /* special test for old K&R protos without explicit int
6350 type. Only accepted when defining global data */
6351 if (l
== VT_LOCAL
|| tok
< TOK_UIDENT
)
6355 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
6356 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
6358 if ((btype
.t
& VT_BTYPE
) == VT_STRUCT
) {
6359 int v
= btype
.ref
->v
;
6360 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) >= SYM_FIRST_ANOM
)
6361 tcc_warning("unnamed struct/union that defines no instances");
6366 while (1) { /* iterate thru each declaration */
6368 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
6372 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
6373 printf("type = '%s'\n", buf
);
6376 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
6377 if ((type
.t
& VT_STATIC
) && (l
== VT_LOCAL
)) {
6378 tcc_error("function without file scope cannot be static");
6380 /* if old style function prototype, we accept a
6383 if (sym
->c
== FUNC_OLD
)
6384 func_decl_list(sym
);
6387 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
6388 ad
.asm_label
= asm_label_instr();
6389 /* parse one last attribute list, after asm label */
6390 parse_attribute(&ad
);
6397 #ifdef TCC_TARGET_PE
6398 if (ad
.a
.func_import
)
6399 type
.t
|= VT_IMPORT
;
6400 if (ad
.a
.func_export
)
6401 type
.t
|= VT_EXPORT
;
6403 type
.t
|= ad
.a
.visibility
<< VT_VIS_SHIFT
;
6407 tcc_error("cannot use local functions");
6408 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
6409 expect("function definition");
6411 /* reject abstract declarators in function definition */
6413 while ((sym
= sym
->next
) != NULL
)
6414 if (!(sym
->v
& ~SYM_FIELD
))
6415 expect("identifier");
6417 /* XXX: cannot do better now: convert extern line to static inline */
6418 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
6419 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
6424 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
6427 ref
= sym
->type
.ref
;
6428 if (0 == ref
->a
.func_proto
)
6429 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
6431 /* use func_call from prototype if not defined */
6432 if (ref
->a
.func_call
!= FUNC_CDECL
6433 && type
.ref
->a
.func_call
== FUNC_CDECL
)
6434 type
.ref
->a
.func_call
= ref
->a
.func_call
;
6436 /* use export from prototype */
6437 if (ref
->a
.func_export
)
6438 type
.ref
->a
.func_export
= 1;
6440 /* use static from prototype */
6441 if (sym
->type
.t
& VT_STATIC
)
6442 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
6444 /* If the definition has no visibility use the
6445 one from prototype. */
6446 if (! (type
.t
& VT_VIS_MASK
))
6447 type
.t
|= sym
->type
.t
& VT_VIS_MASK
;
6449 if (!is_compatible_types(&sym
->type
, &type
)) {
6451 tcc_error("incompatible types for redefinition of '%s'",
6452 get_tok_str(v
, NULL
));
6454 type
.ref
->a
.func_proto
= 0;
6455 /* if symbol is already defined, then put complete type */
6458 /* put function symbol */
6459 sym
= global_identifier_push(v
, type
.t
, 0);
6460 sym
->type
.ref
= type
.ref
;
6463 /* static inline functions are just recorded as a kind
6464 of macro. Their code will be emitted at the end of
6465 the compilation unit only if they are used */
6466 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
6467 (VT_INLINE
| VT_STATIC
)) {
6469 struct InlineFunc
*fn
;
6470 const char *filename
;
6472 filename
= file
? file
->filename
: "";
6473 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
6474 strcpy(fn
->filename
, filename
);
6476 tok_str_new(&fn
->func_str
);
6482 tcc_error("unexpected end of file");
6483 tok_str_add_tok(&fn
->func_str
);
6488 } else if (t
== '}') {
6490 if (block_level
== 0)
6494 tok_str_add(&fn
->func_str
, -1);
6495 tok_str_add(&fn
->func_str
, 0);
6496 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
6499 /* compute text section */
6500 cur_text_section
= ad
.section
;
6501 if (!cur_text_section
)
6502 cur_text_section
= text_section
;
6503 sym
->r
= VT_SYM
| VT_CONST
;
6508 if (btype
.t
& VT_TYPEDEF
) {
6509 /* save typedefed type */
6510 /* XXX: test storage specifiers ? */
6512 if (sym
&& sym
->scope
== local_scope
) {
6513 if (!is_compatible_types(&sym
->type
, &type
)
6514 || !(sym
->type
.t
& VT_TYPEDEF
))
6515 tcc_error("incompatible redefinition of '%s'",
6516 get_tok_str(v
, NULL
));
6519 sym
= sym_push(v
, &type
, 0, 0);
6522 sym
->type
.t
|= VT_TYPEDEF
;
6525 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
6526 /* external function definition */
6527 /* specific case for func_call attribute */
6528 ad
.a
.func_proto
= 1;
6530 } else if (!(type
.t
& VT_ARRAY
)) {
6531 /* not lvalue if array */
6532 r
|= lvalue_type(type
.t
);
6534 has_init
= (tok
== '=');
6535 if (has_init
&& (type
.t
& VT_VLA
))
6536 tcc_error("Variable length array cannot be initialized");
6537 if ((btype
.t
& VT_EXTERN
) || ((type
.t
& VT_BTYPE
) == VT_FUNC
) ||
6538 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
6539 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
6540 /* external variable or function */
6541 /* NOTE: as GCC, uninitialized global static
6542 arrays of null size are considered as
6544 sym
= external_sym(v
, &type
, r
);
6545 sym
->asm_label
= ad
.asm_label
;
6547 if (ad
.alias_target
) {
6552 alias_target
= sym_find(ad
.alias_target
);
6553 if (!alias_target
|| !alias_target
->c
)
6554 tcc_error("unsupported forward __alias__ attribute");
6555 esym
= &((Elf32_Sym
*)symtab_section
->data
)[alias_target
->c
];
6556 tsec
.sh_num
= esym
->st_shndx
;
6557 put_extern_sym2(sym
, &tsec
, esym
->st_value
, esym
->st_size
, 0);
6560 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
6561 if (type
.t
& VT_STATIC
)
6567 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, l
);
6571 if (is_for_loop_init
)
6584 ST_FUNC
void decl(int l
)