2 * TCC - Tiny C Compiler
4 * Copyright (c) 2001-2004 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 /********************************************************/
24 /* global variables */
26 /* loc : local variable index
27 ind : output code index
29 anon_sym: anonymous symbol index
31 ST_DATA
int rsym
, anon_sym
, ind
, loc
;
33 ST_DATA Section
*text_section
, *data_section
, *bss_section
; /* predefined sections */
34 ST_DATA Section
*cur_text_section
; /* current section where function code is generated */
36 ST_DATA Section
*last_text_section
; /* to handle .previous asm directive */
38 #ifdef CONFIG_TCC_BCHECK
39 /* bound check related sections */
40 ST_DATA Section
*bounds_section
; /* contains global data bound description */
41 ST_DATA Section
*lbounds_section
; /* contains local data bound description */
44 ST_DATA Section
*symtab_section
, *strtab_section
;
46 ST_DATA Section
*stab_section
, *stabstr_section
;
47 ST_DATA Sym
*sym_free_first
;
48 ST_DATA
void **sym_pools
;
49 ST_DATA
int nb_sym_pools
;
51 ST_DATA Sym
*global_stack
;
52 ST_DATA Sym
*local_stack
;
53 ST_DATA Sym
*define_stack
;
54 ST_DATA Sym
*global_label_stack
;
55 ST_DATA Sym
*local_label_stack
;
56 static int local_scope
;
58 ST_DATA
int vlas_in_scope
; /* number of VLAs that are currently in scope */
59 ST_DATA
int vla_sp_root_loc
; /* vla_sp_loc for SP before any VLAs were pushed */
60 ST_DATA
int vla_sp_loc
; /* Pointer to variable holding location to store stack pointer on the stack when modifying stack pointer */
62 ST_DATA SValue __vstack
[1+VSTACK_SIZE
], *vtop
, *pvtop
;
64 ST_DATA
int const_wanted
; /* true if constant wanted */
65 ST_DATA
int nocode_wanted
; /* true if no code generation wanted for an expression */
66 ST_DATA
int global_expr
; /* true if compound literals must be allocated globally (used during initializers parsing */
67 ST_DATA CType func_vt
; /* current function return type (used by return instruction) */
68 ST_DATA
int func_var
; /* true if current function is variadic (used by return instruction) */
70 ST_DATA
int last_line_num
, last_ind
, func_ind
; /* debug last line number and pc */
71 ST_DATA
const char *funcname
;
73 ST_DATA CType char_pointer_type
, func_old_type
, int_type
, size_type
;
75 /* ------------------------------------------------------------------------- */
76 static void gen_cast(CType
*type
);
77 static inline CType
*pointed_type(CType
*type
);
78 static int is_compatible_types(CType
*type1
, CType
*type2
);
79 static int parse_btype(CType
*type
, AttributeDef
*ad
);
80 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
);
81 static void parse_expr_type(CType
*type
);
82 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
, int first
, int size_only
);
83 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
, int case_reg
, int is_expr
);
84 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
, int has_init
, int v
, int scope
);
85 static int decl0(int l
, int is_for_loop_init
);
86 static void expr_eq(void);
87 static void expr_lor_const(void);
88 static void unary_type(CType
*type
);
89 static void vla_runtime_type_size(CType
*type
, int *a
);
90 static void vla_sp_restore(void);
91 static void vla_sp_restore_root(void);
92 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
);
93 static void expr_type(CType
*type
);
94 ST_FUNC
void vpush64(int ty
, unsigned long long v
);
95 ST_FUNC
void vpush(CType
*type
);
96 ST_FUNC
int gvtst(int inv
, int t
);
97 ST_FUNC
int is_btype_size(int bt
);
99 ST_INLN
int is_float(int t
)
103 return bt
== VT_LDOUBLE
|| bt
== VT_DOUBLE
|| bt
== VT_FLOAT
|| bt
== VT_QFLOAT
;
106 /* we use our own 'finite' function to avoid potential problems with
107 non standard math libs */
108 /* XXX: endianness dependent */
109 ST_FUNC
int ieee_finite(double d
)
112 memcpy(p
, &d
, sizeof(double));
113 return ((unsigned)((p
[1] | 0x800fffff) + 1)) >> 31;
116 ST_FUNC
void test_lvalue(void)
118 if (!(vtop
->r
& VT_LVAL
))
122 ST_FUNC
void check_vstack(void)
125 tcc_error("internal compiler error: vstack leak (%d)", vtop
- pvtop
);
128 /* ------------------------------------------------------------------------- */
129 /* symbol allocator */
130 static Sym
*__sym_malloc(void)
132 Sym
*sym_pool
, *sym
, *last_sym
;
135 sym_pool
= tcc_malloc(SYM_POOL_NB
* sizeof(Sym
));
136 dynarray_add(&sym_pools
, &nb_sym_pools
, sym_pool
);
138 last_sym
= sym_free_first
;
140 for(i
= 0; i
< SYM_POOL_NB
; i
++) {
141 sym
->next
= last_sym
;
145 sym_free_first
= last_sym
;
149 static inline Sym
*sym_malloc(void)
152 sym
= sym_free_first
;
154 sym
= __sym_malloc();
155 sym_free_first
= sym
->next
;
159 ST_INLN
void sym_free(Sym
*sym
)
161 sym
->next
= sym_free_first
;
162 sym_free_first
= sym
;
165 /* push, without hashing */
166 ST_FUNC Sym
*sym_push2(Sym
**ps
, int v
, int t
, long c
)
186 /* find a symbol and return its associated structure. 's' is the top
187 of the symbol stack */
188 ST_FUNC Sym
*sym_find2(Sym
*s
, int v
)
200 /* structure lookup */
201 ST_INLN Sym
*struct_find(int v
)
204 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
206 return table_ident
[v
]->sym_struct
;
209 /* find an identifier */
210 ST_INLN Sym
*sym_find(int v
)
213 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
215 return table_ident
[v
]->sym_identifier
;
218 /* push a given symbol on the symbol stack */
219 ST_FUNC Sym
*sym_push(int v
, CType
*type
, int r
, int c
)
228 s
= sym_push2(ps
, v
, type
->t
, c
);
229 s
->type
.ref
= type
->ref
;
231 /* don't record fields or anonymous symbols */
233 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
234 /* record symbol in token array */
235 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
237 ps
= &ts
->sym_struct
;
239 ps
= &ts
->sym_identifier
;
243 s
->scope
= local_scope
;
244 if (s
->prev_tok
&& s
->prev_tok
->scope
== s
->scope
)
245 tcc_error("redeclaration of '%s'", get_tok_str(v
& ~SYM_STRUCT
, NULL
));
251 /* push a global identifier */
252 ST_FUNC Sym
*global_identifier_push(int v
, int t
, int c
)
255 s
= sym_push2(&global_stack
, v
, t
, c
);
256 /* don't record anonymous symbol */
257 if (v
< SYM_FIRST_ANOM
) {
258 ps
= &table_ident
[v
- TOK_IDENT
]->sym_identifier
;
259 /* modify the top most local identifier, so that
260 sym_identifier will point to 's' when popped */
262 ps
= &(*ps
)->prev_tok
;
269 /* pop symbols until top reaches 'b' */
270 ST_FUNC
void sym_pop(Sym
**ptop
, Sym
*b
)
280 /* remove symbol in token array */
282 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
283 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
285 ps
= &ts
->sym_struct
;
287 ps
= &ts
->sym_identifier
;
296 static void weaken_symbol(Sym
*sym
)
298 sym
->type
.t
|= VT_WEAK
;
303 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
304 esym_type
= ELFW(ST_TYPE
)(esym
->st_info
);
305 esym
->st_info
= ELFW(ST_INFO
)(STB_WEAK
, esym_type
);
309 static void apply_visibility(Sym
*sym
, CType
*type
)
311 int vis
= sym
->type
.t
& VT_VIS_MASK
;
312 int vis2
= type
->t
& VT_VIS_MASK
;
313 if (vis
== (STV_DEFAULT
<< VT_VIS_SHIFT
))
315 else if (vis2
== (STV_DEFAULT
<< VT_VIS_SHIFT
))
318 vis
= (vis
< vis2
) ? vis
: vis2
;
319 sym
->type
.t
&= ~VT_VIS_MASK
;
325 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
326 vis
>>= VT_VIS_SHIFT
;
327 esym
->st_other
= (esym
->st_other
& ~ELFW(ST_VISIBILITY
)(-1)) | vis
;
331 /* ------------------------------------------------------------------------- */
333 ST_FUNC
void swap(int *p
, int *q
)
341 static void vsetc(CType
*type
, int r
, CValue
*vc
)
345 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
346 tcc_error("memory full (vstack)");
347 /* cannot let cpu flags if other instruction are generated. Also
348 avoid leaving VT_JMP anywhere except on the top of the stack
349 because it would complicate the code generator. */
350 if (vtop
>= vstack
) {
351 v
= vtop
->r
& VT_VALMASK
;
352 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
362 /* push constant of type "type" with useless value */
363 ST_FUNC
void vpush(CType
*type
)
366 vsetc(type
, VT_CONST
, &cval
);
369 /* push integer constant */
370 ST_FUNC
void vpushi(int v
)
374 vsetc(&int_type
, VT_CONST
, &cval
);
377 /* push a pointer sized constant */
378 static void vpushs(addr_t v
)
382 vsetc(&size_type
, VT_CONST
, &cval
);
385 /* push arbitrary 64bit constant */
386 ST_FUNC
void vpush64(int ty
, unsigned long long v
)
393 vsetc(&ctype
, VT_CONST
, &cval
);
396 /* push long long constant */
397 static inline void vpushll(long long v
)
399 vpush64(VT_LLONG
, v
);
402 /* push a symbol value of TYPE */
403 static inline void vpushsym(CType
*type
, Sym
*sym
)
407 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
411 /* Return a static symbol pointing to a section */
412 ST_FUNC Sym
*get_sym_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
418 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
419 sym
->type
.ref
= type
->ref
;
420 sym
->r
= VT_CONST
| VT_SYM
;
421 put_extern_sym(sym
, sec
, offset
, size
);
425 /* push a reference to a section offset by adding a dummy symbol */
426 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
428 vpushsym(type
, get_sym_ref(type
, sec
, offset
, size
));
431 /* define a new external reference to a symbol 'v' of type 'u' */
432 ST_FUNC Sym
*external_global_sym(int v
, CType
*type
, int r
)
438 /* push forward reference */
439 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
440 s
->type
.ref
= type
->ref
;
441 s
->r
= r
| VT_CONST
| VT_SYM
;
446 /* define a new external reference to a symbol 'v' */
447 static Sym
*external_sym(int v
, CType
*type
, int r
)
453 /* push forward reference */
454 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
455 s
->type
.t
|= VT_EXTERN
;
456 } else if (s
->type
.ref
== func_old_type
.ref
) {
457 s
->type
.ref
= type
->ref
;
458 s
->r
= r
| VT_CONST
| VT_SYM
;
459 s
->type
.t
|= VT_EXTERN
;
460 } else if (!is_compatible_types(&s
->type
, type
)) {
461 tcc_error("incompatible types for redefinition of '%s'",
462 get_tok_str(v
, NULL
));
464 /* Merge some storage attributes. */
465 if (type
->t
& VT_WEAK
)
468 if (type
->t
& VT_VIS_MASK
)
469 apply_visibility(s
, type
);
474 /* push a reference to global symbol v */
475 ST_FUNC
void vpush_global_sym(CType
*type
, int v
)
477 vpushsym(type
, external_global_sym(v
, type
, 0));
480 ST_FUNC
void vset(CType
*type
, int r
, int v
)
485 vsetc(type
, r
, &cval
);
488 static void vseti(int r
, int v
)
496 ST_FUNC
void vswap(void)
499 /* cannot let cpu flags if other instruction are generated. Also
500 avoid leaving VT_JMP anywhere except on the top of the stack
501 because it would complicate the code generator. */
502 if (vtop
>= vstack
) {
503 int v
= vtop
->r
& VT_VALMASK
;
504 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
511 /* XXX: +2% overall speed possible with optimized memswap
513 * memswap(&vtop[0], &vtop[1], sizeof *vtop);
517 ST_FUNC
void vpushv(SValue
*v
)
519 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
520 tcc_error("memory full (vstack)");
525 static void vdup(void)
530 /* save r to the memory stack, and mark it as being free */
531 ST_FUNC
void save_reg(int r
)
533 int l
, saved
, size
, align
;
537 /* modify all stack values */
540 for(p
=vstack
;p
<=vtop
;p
++) {
541 if ((p
->r
& VT_VALMASK
) == r
||
542 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
543 /* must save value on stack if not already done */
545 /* NOTE: must reload 'r' because r might be equal to r2 */
546 r
= p
->r
& VT_VALMASK
;
547 /* store register in the stack */
549 if ((p
->r
& VT_LVAL
) ||
550 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
551 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
552 type
= &char_pointer_type
;
556 size
= type_size(type
, &align
);
557 loc
= (loc
- size
) & -align
;
559 sv
.r
= VT_LOCAL
| VT_LVAL
;
562 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
563 /* x86 specific: need to pop fp register ST0 if saved */
565 o(0xd8dd); /* fstp %st(0) */
568 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
569 /* special long long case */
570 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
578 /* mark that stack entry as being saved on the stack */
579 if (p
->r
& VT_LVAL
) {
580 /* also clear the bounded flag because the
581 relocation address of the function was stored in
583 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
585 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
593 #ifdef TCC_TARGET_ARM
594 /* find a register of class 'rc2' with at most one reference on stack.
595 * If none, call get_reg(rc) */
596 ST_FUNC
int get_reg_ex(int rc
, int rc2
)
601 for(r
=0;r
<NB_REGS
;r
++) {
602 if (reg_classes
[r
] & rc2
) {
605 for(p
= vstack
; p
<= vtop
; p
++) {
606 if ((p
->r
& VT_VALMASK
) == r
||
607 (p
->r2
& VT_VALMASK
) == r
)
618 /* find a free register of class 'rc'. If none, save one register */
619 ST_FUNC
int get_reg(int rc
)
624 /* find a free register */
625 for(r
=0;r
<NB_REGS
;r
++) {
626 if (reg_classes
[r
] & rc
) {
627 for(p
=vstack
;p
<=vtop
;p
++) {
628 if ((p
->r
& VT_VALMASK
) == r
||
629 (p
->r2
& VT_VALMASK
) == r
)
637 /* no register left : free the first one on the stack (VERY
638 IMPORTANT to start from the bottom to ensure that we don't
639 spill registers used in gen_opi()) */
640 for(p
=vstack
;p
<=vtop
;p
++) {
641 /* look at second register (if long long) */
642 r
= p
->r2
& VT_VALMASK
;
643 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
645 r
= p
->r
& VT_VALMASK
;
646 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
652 /* Should never comes here */
656 /* save registers up to (vtop - n) stack entry */
657 ST_FUNC
void save_regs(int n
)
662 for(p
= vstack
;p
<= p1
; p
++) {
663 r
= p
->r
& VT_VALMASK
;
670 /* move register 's' (of type 't') to 'r', and flush previous value of r to memory
672 static void move_reg(int r
, int s
, int t
)
686 /* get address of vtop (vtop MUST BE an lvalue) */
687 ST_FUNC
void gaddrof(void)
689 if (vtop
->r
& VT_REF
&& !nocode_wanted
)
692 /* tricky: if saved lvalue, then we can go back to lvalue */
693 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
694 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
699 #ifdef CONFIG_TCC_BCHECK
700 /* generate lvalue bound code */
701 static void gbound(void)
706 vtop
->r
&= ~VT_MUSTBOUND
;
707 /* if lvalue, then use checking code before dereferencing */
708 if (vtop
->r
& VT_LVAL
) {
709 /* if not VT_BOUNDED value, then make one */
710 if (!(vtop
->r
& VT_BOUNDED
)) {
711 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
712 /* must save type because we must set it to int to get pointer */
714 vtop
->type
.t
= VT_PTR
;
717 gen_bounded_ptr_add();
718 vtop
->r
|= lval_type
;
721 /* then check for dereferencing */
722 gen_bounded_ptr_deref();
727 /* store vtop a register belonging to class 'rc'. lvalues are
728 converted to values. Cannot be used if cannot be converted to
729 register value (such as structures). */
730 ST_FUNC
int gv(int rc
)
732 int r
, bit_pos
, bit_size
, size
, align
, i
;
735 /* NOTE: get_reg can modify vstack[] */
736 if (vtop
->type
.t
& VT_BITFIELD
) {
739 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
740 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
741 /* remove bit field info to avoid loops */
742 vtop
->type
.t
&= ~VT_BITFIELD
& ((1 << VT_STRUCT_SHIFT
) - 1);
743 /* cast to int to propagate signedness in following ops */
744 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
749 if((vtop
->type
.t
& VT_UNSIGNED
) ||
750 (vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
751 type
.t
|= VT_UNSIGNED
;
753 /* generate shifts */
754 vpushi(bits
- (bit_pos
+ bit_size
));
756 vpushi(bits
- bit_size
);
757 /* NOTE: transformed to SHR if unsigned */
761 if (is_float(vtop
->type
.t
) &&
762 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
765 unsigned long offset
;
766 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
770 /* XXX: unify with initializers handling ? */
771 /* CPUs usually cannot use float constants, so we store them
772 generically in data segment */
773 size
= type_size(&vtop
->type
, &align
);
774 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
775 data_section
->data_offset
= offset
;
776 /* XXX: not portable yet */
777 #if defined(__i386__) || defined(__x86_64__)
778 /* Zero pad x87 tenbyte long doubles */
779 if (size
== LDOUBLE_SIZE
) {
780 vtop
->c
.tab
[2] &= 0xffff;
781 #if LDOUBLE_SIZE == 16
786 ptr
= section_ptr_add(data_section
, size
);
788 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
792 ptr
[i
] = vtop
->c
.tab
[size
-1-i
];
796 ptr
[i
] = vtop
->c
.tab
[i
];
797 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
798 vtop
->r
|= VT_LVAL
| VT_SYM
;
802 #ifdef CONFIG_TCC_BCHECK
803 if (vtop
->r
& VT_MUSTBOUND
)
807 r
= vtop
->r
& VT_VALMASK
;
808 rc2
= (rc
& RC_FLOAT
) ? RC_FLOAT
: RC_INT
;
809 #ifndef TCC_TARGET_ARM64
812 #ifdef TCC_TARGET_X86_64
813 else if (rc
== RC_FRET
)
818 /* need to reload if:
820 - lvalue (need to dereference pointer)
821 - already a register, but not in the right class */
823 || (vtop
->r
& VT_LVAL
)
824 || !(reg_classes
[r
] & rc
)
825 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
826 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
827 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
&& !(reg_classes
[vtop
->r2
] & rc2
))
829 || ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
834 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
835 if (((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
)) {
836 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
838 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
839 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
840 unsigned long long ll
;
842 int r2
, original_type
;
843 original_type
= vtop
->type
.t
;
844 /* two register type load : expand to two words
846 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
847 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
850 vtop
->c
.i
= ll
; /* first word */
852 vtop
->r
= r
; /* save register value */
853 vpushi(ll
>> 32); /* second word */
856 if (r
>= VT_CONST
|| /* XXX: test to VT_CONST incorrect ? */
857 (vtop
->r
& VT_LVAL
)) {
858 /* We do not want to modifier the long long
859 pointer here, so the safest (and less
860 efficient) is to save all the other registers
861 in the stack. XXX: totally inefficient. */
863 /* load from memory */
864 vtop
->type
.t
= load_type
;
867 vtop
[-1].r
= r
; /* save register value */
868 /* increment pointer to get second word */
869 vtop
->type
.t
= addr_type
;
874 vtop
->type
.t
= load_type
;
879 vtop
[-1].r
= r
; /* save register value */
880 vtop
->r
= vtop
[-1].r2
;
882 /* Allocate second register. Here we rely on the fact that
883 get_reg() tries first to free r2 of an SValue. */
887 /* write second register */
889 vtop
->type
.t
= original_type
;
890 } else if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
892 /* lvalue of scalar type : need to use lvalue type
893 because of possible cast */
896 /* compute memory access type */
897 if (vtop
->r
& VT_REF
)
898 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
903 else if (vtop
->r
& VT_LVAL_BYTE
)
905 else if (vtop
->r
& VT_LVAL_SHORT
)
907 if (vtop
->r
& VT_LVAL_UNSIGNED
)
911 /* restore wanted type */
914 /* one register type load */
919 #ifdef TCC_TARGET_C67
920 /* uses register pairs for doubles */
921 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
928 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
929 ST_FUNC
void gv2(int rc1
, int rc2
)
933 /* generate more generic register first. But VT_JMP or VT_CMP
934 values must be generated first in all cases to avoid possible
936 v
= vtop
[0].r
& VT_VALMASK
;
937 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
942 /* test if reload is needed for first register */
943 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
953 /* test if reload is needed for first register */
954 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
960 #ifndef TCC_TARGET_ARM64
961 /* wrapper around RC_FRET to return a register by type */
962 static int rc_fret(int t
)
964 #ifdef TCC_TARGET_X86_64
965 if (t
== VT_LDOUBLE
) {
973 /* wrapper around REG_FRET to return a register by type */
974 static int reg_fret(int t
)
976 #ifdef TCC_TARGET_X86_64
977 if (t
== VT_LDOUBLE
) {
984 /* expand long long on stack in two int registers */
985 static void lexpand(void)
989 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
992 vtop
[0].r
= vtop
[-1].r2
;
993 vtop
[0].r2
= VT_CONST
;
994 vtop
[-1].r2
= VT_CONST
;
995 vtop
[0].type
.t
= VT_INT
| u
;
996 vtop
[-1].type
.t
= VT_INT
| u
;
999 #ifdef TCC_TARGET_ARM
1000 /* expand long long on stack */
1001 ST_FUNC
void lexpand_nr(void)
1005 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
1007 vtop
->r2
= VT_CONST
;
1008 vtop
->type
.t
= VT_INT
| u
;
1009 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
1010 if (v
== VT_CONST
) {
1011 vtop
[-1].c
.i
= vtop
->c
.i
;
1012 vtop
->c
.i
= vtop
->c
.i
>> 32;
1014 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
1016 vtop
->r
= vtop
[-1].r
;
1017 } else if (v
> VT_CONST
) {
1021 vtop
->r
= vtop
[-1].r2
;
1022 vtop
[-1].r2
= VT_CONST
;
1023 vtop
[-1].type
.t
= VT_INT
| u
;
1027 /* build a long long from two ints */
1028 static void lbuild(int t
)
1030 gv2(RC_INT
, RC_INT
);
1031 vtop
[-1].r2
= vtop
[0].r
;
1032 vtop
[-1].type
.t
= t
;
1036 /* rotate n first stack elements to the bottom
1037 I1 ... In -> I2 ... In I1 [top is right]
1039 ST_FUNC
void vrotb(int n
)
1045 for(i
=-n
+1;i
!=0;i
++)
1046 vtop
[i
] = vtop
[i
+1];
1050 /* rotate the n elements before entry e towards the top
1051 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
1053 ST_FUNC
void vrote(SValue
*e
, int n
)
1059 for(i
= 0;i
< n
- 1; i
++)
1064 /* rotate n first stack elements to the top
1065 I1 ... In -> In I1 ... I(n-1) [top is right]
1067 ST_FUNC
void vrott(int n
)
1072 /* pop stack value */
1073 ST_FUNC
void vpop(void)
1076 v
= vtop
->r
& VT_VALMASK
;
1077 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1078 /* for x86, we need to pop the FP stack */
1079 if (v
== TREG_ST0
&& !nocode_wanted
) {
1080 o(0xd8dd); /* fstp %st(0) */
1083 if (v
== VT_JMP
|| v
== VT_JMPI
) {
1084 /* need to put correct jump if && or || without test */
1090 /* convert stack entry to register and duplicate its value in another
1092 static void gv_dup(void)
1098 if ((t
& VT_BTYPE
) == VT_LLONG
) {
1105 /* stack: H L L1 H1 */
1113 /* duplicate value */
1118 #ifdef TCC_TARGET_X86_64
1119 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1129 load(r1
, &sv
); /* move r to r1 */
1131 /* duplicates value */
1137 /* Generate value test
1139 * Generate a test for any value (jump, comparison and integers) */
1140 ST_FUNC
int gvtst(int inv
, int t
)
1142 int v
= vtop
->r
& VT_VALMASK
;
1143 if (v
!= VT_CMP
&& v
!= VT_JMP
&& v
!= VT_JMPI
) {
1147 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1148 /* constant jmp optimization */
1149 if ((vtop
->c
.i
!= 0) != inv
)
1154 return gtst(inv
, t
);
1157 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
1158 /* generate CPU independent (unsigned) long long operations */
1159 static void gen_opl(int op
)
1161 int t
, a
, b
, op1
, c
, i
;
1163 unsigned short reg_iret
= REG_IRET
;
1164 unsigned short reg_lret
= REG_LRET
;
1170 func
= TOK___divdi3
;
1173 func
= TOK___udivdi3
;
1176 func
= TOK___moddi3
;
1179 func
= TOK___umoddi3
;
1186 /* call generic long long function */
1187 vpush_global_sym(&func_old_type
, func
);
1192 vtop
->r2
= reg_lret
;
1205 /* stack: L1 H1 L2 H2 */
1210 vtop
[-2] = vtop
[-3];
1213 /* stack: H1 H2 L1 L2 */
1219 /* stack: H1 H2 L1 L2 ML MH */
1222 /* stack: ML MH H1 H2 L1 L2 */
1226 /* stack: ML MH H1 L2 H2 L1 */
1231 /* stack: ML MH M1 M2 */
1234 } else if (op
== '+' || op
== '-') {
1235 /* XXX: add non carry method too (for MIPS or alpha) */
1241 /* stack: H1 H2 (L1 op L2) */
1244 gen_op(op1
+ 1); /* TOK_xxxC2 */
1247 /* stack: H1 H2 (L1 op L2) */
1250 /* stack: (L1 op L2) H1 H2 */
1252 /* stack: (L1 op L2) (H1 op H2) */
1260 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1261 t
= vtop
[-1].type
.t
;
1265 /* stack: L H shift */
1267 /* constant: simpler */
1268 /* NOTE: all comments are for SHL. the other cases are
1269 done by swaping words */
1280 if (op
!= TOK_SAR
) {
1313 /* XXX: should provide a faster fallback on x86 ? */
1316 func
= TOK___ashrdi3
;
1319 func
= TOK___lshrdi3
;
1322 func
= TOK___ashldi3
;
1328 /* compare operations */
1334 /* stack: L1 H1 L2 H2 */
1336 vtop
[-1] = vtop
[-2];
1338 /* stack: L1 L2 H1 H2 */
1341 /* when values are equal, we need to compare low words. since
1342 the jump is inverted, we invert the test too. */
1345 else if (op1
== TOK_GT
)
1347 else if (op1
== TOK_ULT
)
1349 else if (op1
== TOK_UGT
)
1354 if (op1
!= TOK_NE
) {
1358 /* generate non equal test */
1359 /* XXX: NOT PORTABLE yet */
1363 #if defined(TCC_TARGET_I386)
1364 b
= psym(0x850f, 0);
1365 #elif defined(TCC_TARGET_ARM)
1367 o(0x1A000000 | encbranch(ind
, 0, 1));
1368 #elif defined(TCC_TARGET_C67) || defined(TCC_TARGET_ARM64)
1369 tcc_error("not implemented");
1371 #error not supported
1375 /* compare low. Always unsigned */
1379 else if (op1
== TOK_LE
)
1381 else if (op1
== TOK_GT
)
1383 else if (op1
== TOK_GE
)
1394 static uint64_t gen_opic_sdiv(uint64_t a
, uint64_t b
)
1396 uint64_t x
= (a
>> 63 ? -a
: a
) / (b
>> 63 ? -b
: b
);
1397 return (a
^ b
) >> 63 ? -x
: x
;
1400 static int gen_opic_lt(uint64_t a
, uint64_t b
)
1402 return (a
^ (uint64_t)1 << 63) < (b
^ (uint64_t)1 << 63);
1405 /* handle integer constant optimizations and various machine
1407 static void gen_opic(int op
)
1409 SValue
*v1
= vtop
- 1;
1411 int t1
= v1
->type
.t
& VT_BTYPE
;
1412 int t2
= v2
->type
.t
& VT_BTYPE
;
1413 int c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1414 int c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1415 uint64_t l1
= c1
? v1
->c
.i
: 0;
1416 uint64_t l2
= c2
? v2
->c
.i
: 0;
1417 int shm
= (t1
== VT_LLONG
) ? 63 : 31;
1420 l1
= ((uint32_t)l1
|
1421 (v1
->type
.t
& VT_UNSIGNED
? 0 : -(l1
& 0x80000000)));
1423 l2
= ((uint32_t)l2
|
1424 (v2
->type
.t
& VT_UNSIGNED
? 0 : -(l2
& 0x80000000)));
1428 case '+': l1
+= l2
; break;
1429 case '-': l1
-= l2
; break;
1430 case '&': l1
&= l2
; break;
1431 case '^': l1
^= l2
; break;
1432 case '|': l1
|= l2
; break;
1433 case '*': l1
*= l2
; break;
1440 /* if division by zero, generate explicit division */
1443 tcc_error("division by zero in constant");
1447 default: l1
= gen_opic_sdiv(l1
, l2
); break;
1448 case '%': l1
= l1
- l2
* gen_opic_sdiv(l1
, l2
); break;
1449 case TOK_UDIV
: l1
= l1
/ l2
; break;
1450 case TOK_UMOD
: l1
= l1
% l2
; break;
1453 case TOK_SHL
: l1
<<= (l2
& shm
); break;
1454 case TOK_SHR
: l1
>>= (l2
& shm
); break;
1456 l1
= (l1
>> 63) ? ~(~l1
>> (l2
& shm
)) : l1
>> (l2
& shm
);
1459 case TOK_ULT
: l1
= l1
< l2
; break;
1460 case TOK_UGE
: l1
= l1
>= l2
; break;
1461 case TOK_EQ
: l1
= l1
== l2
; break;
1462 case TOK_NE
: l1
= l1
!= l2
; break;
1463 case TOK_ULE
: l1
= l1
<= l2
; break;
1464 case TOK_UGT
: l1
= l1
> l2
; break;
1465 case TOK_LT
: l1
= gen_opic_lt(l1
, l2
); break;
1466 case TOK_GE
: l1
= !gen_opic_lt(l1
, l2
); break;
1467 case TOK_LE
: l1
= !gen_opic_lt(l2
, l1
); break;
1468 case TOK_GT
: l1
= gen_opic_lt(l2
, l1
); break;
1470 case TOK_LAND
: l1
= l1
&& l2
; break;
1471 case TOK_LOR
: l1
= l1
|| l2
; break;
1478 /* if commutative ops, put c2 as constant */
1479 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1480 op
== '|' || op
== '*')) {
1482 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1483 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1485 if (!const_wanted
&&
1487 (op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
)) ||
1488 (l1
== -1 && op
== TOK_SAR
))) {
1489 /* treat (0 << x), (0 >> x) and (-1 >> x) as constant */
1491 } else if (!const_wanted
&&
1492 c2
&& ((l2
== 0 && (op
== '&' || op
== '*')) ||
1493 (l2
== -1 && op
== '|') ||
1494 (l2
== 0xffffffff && t2
!= VT_LLONG
&& op
== '|') ||
1495 (l2
== 1 && (op
== '%' || op
== TOK_UMOD
)))) {
1496 /* treat (x & 0), (x * 0), (x | -1) and (x % 1) as constant */
1501 } else if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1504 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1505 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1509 /* filter out NOP operations like x*1, x-0, x&-1... */
1511 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1512 /* try to use shifts instead of muls or divs */
1513 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1522 else if (op
== TOK_PDIV
)
1528 } else if (c2
&& (op
== '+' || op
== '-') &&
1529 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
1530 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1531 /* symbol + constant case */
1538 if (!nocode_wanted
) {
1539 /* call low level op generator */
1540 if (t1
== VT_LLONG
|| t2
== VT_LLONG
||
1541 (PTR_SIZE
== 8 && (t1
== VT_PTR
|| t2
== VT_PTR
)))
1552 /* generate a floating point operation with constant propagation */
1553 static void gen_opif(int op
)
1561 /* currently, we cannot do computations with forward symbols */
1562 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1563 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1565 if (v1
->type
.t
== VT_FLOAT
) {
1568 } else if (v1
->type
.t
== VT_DOUBLE
) {
1576 /* NOTE: we only do constant propagation if finite number (not
1577 NaN or infinity) (ANSI spec) */
1578 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1582 case '+': f1
+= f2
; break;
1583 case '-': f1
-= f2
; break;
1584 case '*': f1
*= f2
; break;
1588 tcc_error("division by zero in constant");
1593 /* XXX: also handles tests ? */
1597 /* XXX: overflow test ? */
1598 if (v1
->type
.t
== VT_FLOAT
) {
1600 } else if (v1
->type
.t
== VT_DOUBLE
) {
1608 if (!nocode_wanted
) {
1616 static int pointed_size(CType
*type
)
1619 return type_size(pointed_type(type
), &align
);
1622 static void vla_runtime_pointed_size(CType
*type
)
1625 vla_runtime_type_size(pointed_type(type
), &align
);
1628 static inline int is_null_pointer(SValue
*p
)
1630 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1632 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& (uint32_t)p
->c
.i
== 0) ||
1633 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.i
== 0) ||
1634 ((p
->type
.t
& VT_BTYPE
) == VT_PTR
&&
1635 (PTR_SIZE
== 4 ? (uint32_t)p
->c
.i
== 0 : p
->c
.i
== 0));
1638 static inline int is_integer_btype(int bt
)
1640 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
1641 bt
== VT_INT
|| bt
== VT_LLONG
);
1644 /* check types for comparison or subtraction of pointers */
1645 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
1647 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
1650 /* null pointers are accepted for all comparisons as gcc */
1651 if (is_null_pointer(p1
) || is_null_pointer(p2
))
1655 bt1
= type1
->t
& VT_BTYPE
;
1656 bt2
= type2
->t
& VT_BTYPE
;
1657 /* accept comparison between pointer and integer with a warning */
1658 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
1659 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
1660 tcc_warning("comparison between pointer and integer");
1664 /* both must be pointers or implicit function pointers */
1665 if (bt1
== VT_PTR
) {
1666 type1
= pointed_type(type1
);
1667 } else if (bt1
!= VT_FUNC
)
1668 goto invalid_operands
;
1670 if (bt2
== VT_PTR
) {
1671 type2
= pointed_type(type2
);
1672 } else if (bt2
!= VT_FUNC
) {
1674 tcc_error("invalid operands to binary %s", get_tok_str(op
, NULL
));
1676 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1677 (type2
->t
& VT_BTYPE
) == VT_VOID
)
1681 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1682 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1683 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1684 /* gcc-like error if '-' is used */
1686 goto invalid_operands
;
1688 tcc_warning("comparison of distinct pointer types lacks a cast");
1692 /* generic gen_op: handles types problems */
1693 ST_FUNC
void gen_op(int op
)
1695 int u
, t1
, t2
, bt1
, bt2
, t
;
1698 t1
= vtop
[-1].type
.t
;
1699 t2
= vtop
[0].type
.t
;
1700 bt1
= t1
& VT_BTYPE
;
1701 bt2
= t2
& VT_BTYPE
;
1703 if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
1704 /* at least one operand is a pointer */
1705 /* relationnal op: must be both pointers */
1706 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
1707 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1708 /* pointers are handled are unsigned */
1709 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1710 t
= VT_LLONG
| VT_UNSIGNED
;
1712 t
= VT_INT
| VT_UNSIGNED
;
1716 /* if both pointers, then it must be the '-' op */
1717 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
1719 tcc_error("cannot use pointers here");
1720 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1721 /* XXX: check that types are compatible */
1722 if (vtop
[-1].type
.t
& VT_VLA
) {
1723 vla_runtime_pointed_size(&vtop
[-1].type
);
1725 vpushi(pointed_size(&vtop
[-1].type
));
1729 /* set to integer type */
1730 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1731 vtop
->type
.t
= VT_LLONG
;
1733 vtop
->type
.t
= VT_INT
;
1738 /* exactly one pointer : must be '+' or '-'. */
1739 if (op
!= '-' && op
!= '+')
1740 tcc_error("cannot use pointers here");
1741 /* Put pointer as first operand */
1742 if (bt2
== VT_PTR
) {
1746 type1
= vtop
[-1].type
;
1747 type1
.t
&= ~VT_ARRAY
;
1748 if (vtop
[-1].type
.t
& VT_VLA
)
1749 vla_runtime_pointed_size(&vtop
[-1].type
);
1751 u
= pointed_size(&vtop
[-1].type
);
1753 tcc_error("unknown array element size");
1754 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1757 /* XXX: cast to int ? (long long case) */
1763 /* #ifdef CONFIG_TCC_BCHECK
1764 The main reason to removing this code:
1771 fprintf(stderr, "v+i-j = %p\n", v+i-j);
1772 fprintf(stderr, "v+(i-j) = %p\n", v+(i-j));
1774 When this code is on. then the output looks like
1776 v+(i-j) = 0xbff84000
1778 /* if evaluating constant expression, no code should be
1779 generated, so no bound check */
1780 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
1781 /* if bounded pointers, we generate a special code to
1788 gen_bounded_ptr_add();
1794 /* put again type if gen_opic() swaped operands */
1797 } else if (is_float(bt1
) || is_float(bt2
)) {
1798 /* compute bigger type and do implicit casts */
1799 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
1801 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
1806 /* floats can only be used for a few operations */
1807 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
1808 (op
< TOK_ULT
|| op
> TOK_GT
))
1809 tcc_error("invalid operands for binary operation");
1811 } else if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
) {
1812 t
= bt1
== VT_LLONG
? VT_LLONG
: VT_INT
;
1813 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (t
| VT_UNSIGNED
))
1816 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
1817 /* cast to biggest op */
1819 /* convert to unsigned if it does not fit in a long long */
1820 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
1821 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
1824 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
1825 tcc_error("comparison of struct");
1827 /* integer operations */
1829 /* convert to unsigned if it does not fit in an integer */
1830 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
1831 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
1834 /* XXX: currently, some unsigned operations are explicit, so
1835 we modify them here */
1836 if (t
& VT_UNSIGNED
) {
1843 else if (op
== TOK_LT
)
1845 else if (op
== TOK_GT
)
1847 else if (op
== TOK_LE
)
1849 else if (op
== TOK_GE
)
1856 /* special case for shifts and long long: we keep the shift as
1858 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
1865 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
1866 /* relationnal op: the result is an int */
1867 vtop
->type
.t
= VT_INT
;
1872 // Make sure that we have converted to an rvalue:
1873 if (vtop
->r
& VT_LVAL
&& !nocode_wanted
)
1874 gv(is_float(vtop
->type
.t
& VT_BTYPE
) ? RC_FLOAT
: RC_INT
);
1877 #ifndef TCC_TARGET_ARM
1878 /* generic itof for unsigned long long case */
1879 static void gen_cvt_itof1(int t
)
1881 #ifdef TCC_TARGET_ARM64
1884 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
1885 (VT_LLONG
| VT_UNSIGNED
)) {
1888 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
1889 #if LDOUBLE_SIZE != 8
1890 else if (t
== VT_LDOUBLE
)
1891 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
1894 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
1898 vtop
->r
= reg_fret(t
);
1906 /* generic ftoi for unsigned long long case */
1907 static void gen_cvt_ftoi1(int t
)
1909 #ifdef TCC_TARGET_ARM64
1914 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
1915 /* not handled natively */
1916 st
= vtop
->type
.t
& VT_BTYPE
;
1918 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
1919 #if LDOUBLE_SIZE != 8
1920 else if (st
== VT_LDOUBLE
)
1921 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
1924 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
1929 vtop
->r2
= REG_LRET
;
1936 /* force char or short cast */
1937 static void force_charshort_cast(int t
)
1941 /* XXX: add optimization if lvalue : just change type and offset */
1946 if (t
& VT_UNSIGNED
) {
1947 vpushi((1 << bits
) - 1);
1953 /* result must be signed or the SAR is converted to an SHL
1954 This was not the case when "t" was a signed short
1955 and the last value on the stack was an unsigned int */
1956 vtop
->type
.t
&= ~VT_UNSIGNED
;
1962 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1963 static void gen_cast(CType
*type
)
1965 int sbt
, dbt
, sf
, df
, c
, p
;
1967 /* special delayed cast for char/short */
1968 /* XXX: in some cases (multiple cascaded casts), it may still
1970 if (vtop
->r
& VT_MUSTCAST
) {
1971 vtop
->r
&= ~VT_MUSTCAST
;
1972 force_charshort_cast(vtop
->type
.t
);
1975 /* bitfields first get cast to ints */
1976 if (vtop
->type
.t
& VT_BITFIELD
&& !nocode_wanted
) {
1980 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
1981 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
1986 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1987 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
1989 /* constant case: we can do it now */
1990 /* XXX: in ISOC, cannot do it if error in convert */
1991 if (sbt
== VT_FLOAT
)
1992 vtop
->c
.ld
= vtop
->c
.f
;
1993 else if (sbt
== VT_DOUBLE
)
1994 vtop
->c
.ld
= vtop
->c
.d
;
1997 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
1998 if ((sbt
& VT_UNSIGNED
) || !(vtop
->c
.i
>> 63))
1999 vtop
->c
.ld
= vtop
->c
.i
;
2001 vtop
->c
.ld
= -(long double)-vtop
->c
.i
;
2003 if ((sbt
& VT_UNSIGNED
) || !(vtop
->c
.i
>> 31))
2004 vtop
->c
.ld
= (uint32_t)vtop
->c
.i
;
2006 vtop
->c
.ld
= -(long double)-(uint32_t)vtop
->c
.i
;
2009 if (dbt
== VT_FLOAT
)
2010 vtop
->c
.f
= (float)vtop
->c
.ld
;
2011 else if (dbt
== VT_DOUBLE
)
2012 vtop
->c
.d
= (double)vtop
->c
.ld
;
2013 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
2014 vtop
->c
.i
= vtop
->c
.ld
;
2015 } else if (sf
&& dbt
== VT_BOOL
) {
2016 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
2019 vtop
->c
.i
= vtop
->c
.ld
;
2020 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
2022 else if (sbt
& VT_UNSIGNED
)
2023 vtop
->c
.i
= (uint32_t)vtop
->c
.i
;
2024 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2025 else if (sbt
== VT_PTR
)
2028 else if (sbt
!= VT_LLONG
)
2029 vtop
->c
.i
= ((uint32_t)vtop
->c
.i
|
2030 -(vtop
->c
.i
& 0x80000000));
2032 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
2034 else if (dbt
== VT_BOOL
)
2035 vtop
->c
.i
= (vtop
->c
.i
!= 0);
2036 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2037 else if (dbt
== VT_PTR
)
2040 else if (dbt
!= VT_LLONG
) {
2041 uint32_t m
= ((dbt
& VT_BTYPE
) == VT_BYTE
? 0xff :
2042 (dbt
& VT_BTYPE
) == VT_SHORT
? 0xffff :
2045 if (!(dbt
& VT_UNSIGNED
))
2046 vtop
->c
.i
|= -(vtop
->c
.i
& ((m
>> 1) + 1));
2049 } else if (p
&& dbt
== VT_BOOL
) {
2052 } else if (!nocode_wanted
) {
2053 /* non constant case: generate code */
2055 /* convert from fp to fp */
2058 /* convert int to fp */
2061 /* convert fp to int */
2062 if (dbt
== VT_BOOL
) {
2066 /* we handle char/short/etc... with generic code */
2067 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
2068 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
2072 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
2073 /* additional cast for char/short... */
2078 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
2079 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
2080 if ((sbt
& VT_BTYPE
) != VT_LLONG
&& !nocode_wanted
) {
2081 /* scalar to long long */
2082 /* machine independent conversion */
2084 /* generate high word */
2085 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
2089 if (sbt
== VT_PTR
) {
2090 /* cast from pointer to int before we apply
2091 shift operation, which pointers don't support*/
2092 gen_cast(&int_type
);
2098 /* patch second register */
2099 vtop
[-1].r2
= vtop
->r
;
2103 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
2104 (dbt
& VT_BTYPE
) == VT_PTR
||
2105 (dbt
& VT_BTYPE
) == VT_FUNC
) {
2106 if ((sbt
& VT_BTYPE
) != VT_LLONG
&&
2107 (sbt
& VT_BTYPE
) != VT_PTR
&&
2108 (sbt
& VT_BTYPE
) != VT_FUNC
&& !nocode_wanted
) {
2109 /* need to convert from 32bit to 64bit */
2111 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
2112 #if defined(TCC_TARGET_ARM64)
2114 #elif defined(TCC_TARGET_X86_64)
2116 /* x86_64 specific: movslq */
2118 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
2125 } else if (dbt
== VT_BOOL
) {
2126 /* scalar to bool */
2129 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
2130 (dbt
& VT_BTYPE
) == VT_SHORT
) {
2131 if (sbt
== VT_PTR
) {
2132 vtop
->type
.t
= VT_INT
;
2133 tcc_warning("nonportable conversion from pointer to char/short");
2135 force_charshort_cast(dbt
);
2136 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
2138 if (sbt
== VT_LLONG
&& !nocode_wanted
) {
2139 /* from long long: just take low order word */
2143 /* if lvalue and single word type, nothing to do because
2144 the lvalue already contains the real type size (see
2145 VT_LVAL_xxx constants) */
2148 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
2149 /* if we are casting between pointer types,
2150 we must update the VT_LVAL_xxx size */
2151 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
2152 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
2157 /* return type size as known at compile time. Put alignment at 'a' */
2158 ST_FUNC
int type_size(CType
*type
, int *a
)
2163 bt
= type
->t
& VT_BTYPE
;
2164 if (bt
== VT_STRUCT
) {
2169 } else if (bt
== VT_PTR
) {
2170 if (type
->t
& VT_ARRAY
) {
2174 ts
= type_size(&s
->type
, a
);
2176 if (ts
< 0 && s
->c
< 0)
2184 } else if (bt
== VT_LDOUBLE
) {
2186 return LDOUBLE_SIZE
;
2187 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
2188 #ifdef TCC_TARGET_I386
2189 #ifdef TCC_TARGET_PE
2194 #elif defined(TCC_TARGET_ARM)
2204 } else if (bt
== VT_INT
|| bt
== VT_FLOAT
) {
2207 } else if (bt
== VT_SHORT
) {
2210 } else if (bt
== VT_QLONG
|| bt
== VT_QFLOAT
) {
2213 } else if (bt
== VT_ENUM
) {
2215 /* Enums might be incomplete, so don't just return '4' here. */
2216 return type
->ref
->c
;
2218 /* char, void, function, _Bool */
2224 /* push type size as known at runtime time on top of value stack. Put
2226 ST_FUNC
void vla_runtime_type_size(CType
*type
, int *a
)
2228 if (type
->t
& VT_VLA
) {
2229 vset(&int_type
, VT_LOCAL
|VT_LVAL
, type
->ref
->c
);
2231 vpushi(type_size(type
, a
));
2235 static void vla_sp_restore(void) {
2236 if (vlas_in_scope
) {
2237 gen_vla_sp_restore(vla_sp_loc
);
2241 static void vla_sp_restore_root(void) {
2242 if (vlas_in_scope
) {
2243 gen_vla_sp_restore(vla_sp_root_loc
);
2247 /* return the pointed type of t */
2248 static inline CType
*pointed_type(CType
*type
)
2250 return &type
->ref
->type
;
2253 /* modify type so that its it is a pointer to type. */
2254 ST_FUNC
void mk_pointer(CType
*type
)
2257 s
= sym_push(SYM_FIELD
, type
, 0, -1);
2258 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
2262 /* compare function types. OLD functions match any new functions */
2263 static int is_compatible_func(CType
*type1
, CType
*type2
)
2269 if (!is_compatible_types(&s1
->type
, &s2
->type
))
2271 /* check func_call */
2272 if (s1
->a
.func_call
!= s2
->a
.func_call
)
2274 /* XXX: not complete */
2275 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
2279 while (s1
!= NULL
) {
2282 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
2292 /* return true if type1 and type2 are the same. If unqualified is
2293 true, qualifiers on the types are ignored.
2295 - enums are not checked as gcc __builtin_types_compatible_p ()
2297 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2301 t1
= type1
->t
& VT_TYPE
;
2302 t2
= type2
->t
& VT_TYPE
;
2304 /* strip qualifiers before comparing */
2305 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2306 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2308 /* Default Vs explicit signedness only matters for char */
2309 if ((t1
& VT_BTYPE
) != VT_BYTE
) {
2313 /* XXX: bitfields ? */
2316 /* test more complicated cases */
2317 bt1
= t1
& VT_BTYPE
;
2318 if (bt1
== VT_PTR
) {
2319 type1
= pointed_type(type1
);
2320 type2
= pointed_type(type2
);
2321 return is_compatible_types(type1
, type2
);
2322 } else if (bt1
== VT_STRUCT
) {
2323 return (type1
->ref
== type2
->ref
);
2324 } else if (bt1
== VT_FUNC
) {
2325 return is_compatible_func(type1
, type2
);
2331 /* return true if type1 and type2 are exactly the same (including
2334 static int is_compatible_types(CType
*type1
, CType
*type2
)
2336 return compare_types(type1
,type2
,0);
2339 /* return true if type1 and type2 are the same (ignoring qualifiers).
2341 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
2343 return compare_types(type1
,type2
,1);
2346 /* print a type. If 'varstr' is not NULL, then the variable is also
2347 printed in the type */
2349 /* XXX: add array and function pointers */
2350 static void type_to_str(char *buf
, int buf_size
,
2351 CType
*type
, const char *varstr
)
2358 t
= type
->t
& VT_TYPE
;
2361 if (t
& VT_CONSTANT
)
2362 pstrcat(buf
, buf_size
, "const ");
2363 if (t
& VT_VOLATILE
)
2364 pstrcat(buf
, buf_size
, "volatile ");
2365 if ((t
& (VT_DEFSIGN
| VT_UNSIGNED
)) == (VT_DEFSIGN
| VT_UNSIGNED
))
2366 pstrcat(buf
, buf_size
, "unsigned ");
2367 else if (t
& VT_DEFSIGN
)
2368 pstrcat(buf
, buf_size
, "signed ");
2398 tstr
= "long double";
2400 pstrcat(buf
, buf_size
, tstr
);
2404 if (bt
== VT_STRUCT
)
2408 pstrcat(buf
, buf_size
, tstr
);
2409 v
= type
->ref
->v
& ~SYM_STRUCT
;
2410 if (v
>= SYM_FIRST_ANOM
)
2411 pstrcat(buf
, buf_size
, "<anonymous>");
2413 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
2417 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
2418 pstrcat(buf
, buf_size
, "(");
2420 while (sa
!= NULL
) {
2421 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
2422 pstrcat(buf
, buf_size
, buf1
);
2425 pstrcat(buf
, buf_size
, ", ");
2427 pstrcat(buf
, buf_size
, ")");
2432 snprintf(buf1
, sizeof(buf1
), "%s[%ld]", varstr
? varstr
: "", s
->c
);
2433 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2436 pstrcpy(buf1
, sizeof(buf1
), "*");
2437 if (t
& VT_CONSTANT
)
2438 pstrcat(buf1
, buf_size
, "const ");
2439 if (t
& VT_VOLATILE
)
2440 pstrcat(buf1
, buf_size
, "volatile ");
2442 pstrcat(buf1
, sizeof(buf1
), varstr
);
2443 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2447 pstrcat(buf
, buf_size
, " ");
2448 pstrcat(buf
, buf_size
, varstr
);
2453 /* verify type compatibility to store vtop in 'dt' type, and generate
2455 static void gen_assign_cast(CType
*dt
)
2457 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
2458 char buf1
[256], buf2
[256];
2461 st
= &vtop
->type
; /* source type */
2462 dbt
= dt
->t
& VT_BTYPE
;
2463 sbt
= st
->t
& VT_BTYPE
;
2464 if (sbt
== VT_VOID
|| dbt
== VT_VOID
) {
2465 if (sbt
== VT_VOID
&& dbt
== VT_VOID
)
2467 It is Ok if both are void
2473 gcc accepts this program
2476 tcc_error("cannot cast from/to void");
2478 if (dt
->t
& VT_CONSTANT
)
2479 tcc_warning("assignment of read-only location");
2482 /* special cases for pointers */
2483 /* '0' can also be a pointer */
2484 if (is_null_pointer(vtop
))
2486 /* accept implicit pointer to integer cast with warning */
2487 if (is_integer_btype(sbt
)) {
2488 tcc_warning("assignment makes pointer from integer without a cast");
2491 type1
= pointed_type(dt
);
2492 /* a function is implicitely a function pointer */
2493 if (sbt
== VT_FUNC
) {
2494 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
2495 !is_compatible_types(pointed_type(dt
), st
))
2496 tcc_warning("assignment from incompatible pointer type");
2501 type2
= pointed_type(st
);
2502 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2503 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
2504 /* void * can match anything */
2506 /* exact type match, except for unsigned */
2509 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
|
2511 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
|
2513 if (!is_compatible_types(&tmp_type1
, &tmp_type2
))
2514 tcc_warning("assignment from incompatible pointer type");
2516 /* check const and volatile */
2517 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
2518 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
2519 tcc_warning("assignment discards qualifiers from pointer target type");
2525 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
2526 tcc_warning("assignment makes integer from pointer without a cast");
2528 /* XXX: more tests */
2533 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2534 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2535 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2537 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
2538 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
2539 tcc_error("cannot cast '%s' to '%s'", buf1
, buf2
);
2547 /* store vtop in lvalue pushed on stack */
2548 ST_FUNC
void vstore(void)
2550 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
2552 ft
= vtop
[-1].type
.t
;
2553 sbt
= vtop
->type
.t
& VT_BTYPE
;
2554 dbt
= ft
& VT_BTYPE
;
2555 if ((((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2556 (sbt
== VT_INT
&& dbt
== VT_SHORT
))
2557 && !(vtop
->type
.t
& VT_BITFIELD
)) {
2558 /* optimize char/short casts */
2559 delayed_cast
= VT_MUSTCAST
;
2560 vtop
->type
.t
= (ft
& VT_TYPE
& ~VT_BITFIELD
&
2561 ((1 << VT_STRUCT_SHIFT
) - 1));
2562 /* XXX: factorize */
2563 if (ft
& VT_CONSTANT
)
2564 tcc_warning("assignment of read-only location");
2567 if (!(ft
& VT_BITFIELD
))
2568 gen_assign_cast(&vtop
[-1].type
);
2571 if (sbt
== VT_STRUCT
) {
2572 /* if structure, only generate pointer */
2573 /* structure assignment : generate memcpy */
2574 /* XXX: optimize if small size */
2575 if (!nocode_wanted
) {
2576 size
= type_size(&vtop
->type
, &align
);
2580 vtop
->type
.t
= VT_PTR
;
2583 /* address of memcpy() */
2586 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2587 else if(!(align
& 3))
2588 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2591 /* Use memmove, rather than memcpy, as dest and src may be same: */
2592 vpush_global_sym(&func_old_type
, TOK_memmove
);
2597 vtop
->type
.t
= VT_PTR
;
2606 /* leave source on stack */
2607 } else if (ft
& VT_BITFIELD
) {
2608 /* bitfield store handling */
2610 /* save lvalue as expression result (example: s.b = s.a = n;) */
2611 vdup(), vtop
[-1] = vtop
[-2];
2613 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2614 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2615 /* remove bit field info to avoid loops */
2616 vtop
[-1].type
.t
= ft
& ~VT_BITFIELD
& ((1 << VT_STRUCT_SHIFT
) - 1);
2618 if((ft
& VT_BTYPE
) == VT_BOOL
) {
2619 gen_cast(&vtop
[-1].type
);
2620 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2623 /* duplicate destination */
2625 vtop
[-1] = vtop
[-2];
2627 /* mask and shift source */
2628 if((ft
& VT_BTYPE
) != VT_BOOL
) {
2629 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2630 vpushll((1ULL << bit_size
) - 1ULL);
2632 vpushi((1 << bit_size
) - 1);
2638 /* load destination, mask and or with source */
2640 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2641 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2643 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2649 /* ... and discard */
2653 if (!nocode_wanted
) {
2654 #ifdef CONFIG_TCC_BCHECK
2655 /* bound check case */
2656 if (vtop
[-1].r
& VT_MUSTBOUND
) {
2665 #ifdef TCC_TARGET_X86_64
2666 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
2668 } else if ((ft
& VT_BTYPE
) == VT_QFLOAT
) {
2673 r
= gv(rc
); /* generate value */
2674 /* if lvalue was saved on stack, must read it */
2675 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
2677 t
= get_reg(RC_INT
);
2678 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2683 sv
.r
= VT_LOCAL
| VT_LVAL
;
2684 sv
.c
.i
= vtop
[-1].c
.i
;
2686 vtop
[-1].r
= t
| VT_LVAL
;
2688 /* two word case handling : store second register at word + 4 (or +8 for x86-64) */
2689 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2690 if (((ft
& VT_BTYPE
) == VT_QLONG
) || ((ft
& VT_BTYPE
) == VT_QFLOAT
)) {
2691 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
2693 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
2694 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
2696 vtop
[-1].type
.t
= load_type
;
2699 /* convert to int to increment easily */
2700 vtop
->type
.t
= addr_type
;
2706 vtop
[-1].type
.t
= load_type
;
2707 /* XXX: it works because r2 is spilled last ! */
2708 store(vtop
->r2
, vtop
- 1);
2714 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
2715 vtop
->r
|= delayed_cast
;
2719 /* post defines POST/PRE add. c is the token ++ or -- */
2720 ST_FUNC
void inc(int post
, int c
)
2723 vdup(); /* save lvalue */
2726 gv_dup(); /* duplicate value */
2728 vdup(); /* duplicate value */
2733 vpushi(c
- TOK_MID
);
2735 vstore(); /* store value */
2737 vpop(); /* if post op, return saved value */
2740 /* Parse GNUC __attribute__ extension. Currently, the following
2741 extensions are recognized:
2742 - aligned(n) : set data/function alignment.
2743 - packed : force data alignment to 1
2744 - section(x) : generate data/code in this section.
2745 - unused : currently ignored, but may be used someday.
2746 - regparm(n) : pass function parameters in registers (i386 only)
2748 static void parse_attribute(AttributeDef
*ad
)
2752 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
2756 while (tok
!= ')') {
2757 if (tok
< TOK_IDENT
)
2758 expect("attribute name");
2766 expect("section name");
2767 ad
->section
= find_section(tcc_state
, (char *)tokc
.str
.data
);
2775 expect("alias(\"target\")");
2776 ad
->alias_target
= /* save string as token, for later */
2777 tok_alloc((char*)tokc
.str
.data
, tokc
.str
.size
-1)->tok
;
2781 case TOK_VISIBILITY1
:
2782 case TOK_VISIBILITY2
:
2785 expect("visibility(\"default|hidden|internal|protected\")");
2786 if (!strcmp (tokc
.str
.data
, "default"))
2787 ad
->a
.visibility
= STV_DEFAULT
;
2788 else if (!strcmp (tokc
.str
.data
, "hidden"))
2789 ad
->a
.visibility
= STV_HIDDEN
;
2790 else if (!strcmp (tokc
.str
.data
, "internal"))
2791 ad
->a
.visibility
= STV_INTERNAL
;
2792 else if (!strcmp (tokc
.str
.data
, "protected"))
2793 ad
->a
.visibility
= STV_PROTECTED
;
2795 expect("visibility(\"default|hidden|internal|protected\")");
2804 if (n
<= 0 || (n
& (n
- 1)) != 0)
2805 tcc_error("alignment must be a positive power of two");
2822 /* currently, no need to handle it because tcc does not
2823 track unused objects */
2827 /* currently, no need to handle it because tcc does not
2828 track unused objects */
2833 ad
->a
.func_call
= FUNC_CDECL
;
2838 ad
->a
.func_call
= FUNC_STDCALL
;
2840 #ifdef TCC_TARGET_I386
2850 ad
->a
.func_call
= FUNC_FASTCALL1
+ n
- 1;
2856 ad
->a
.func_call
= FUNC_FASTCALLW
;
2863 ad
->a
.mode
= VT_LLONG
+ 1;
2866 ad
->a
.mode
= VT_SHORT
+ 1;
2869 ad
->a
.mode
= VT_INT
+ 1;
2872 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
2879 ad
->a
.func_export
= 1;
2882 ad
->a
.func_import
= 1;
2885 if (tcc_state
->warn_unsupported
)
2886 tcc_warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
2887 /* skip parameters */
2889 int parenthesis
= 0;
2893 else if (tok
== ')')
2896 } while (parenthesis
&& tok
!= -1);
2909 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2910 static void struct_decl(CType
*type
, AttributeDef
*ad
, int u
)
2912 int a
, v
, size
, align
, maxalign
, c
, offset
, flexible
;
2913 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
, prevbt
;
2914 Sym
*s
, *ss
, *ass
, **ps
;
2918 a
= tok
; /* save decl type */
2920 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
2921 parse_attribute(ad
);
2927 /* struct already defined ? return it */
2929 expect("struct/union/enum name");
2931 if (s
&& s
->type
.t
== a
) {
2932 if (tok
!= '{' && tok
!= ';')
2933 goto do_decl
; /* variable declaration: 'struct s x;' */
2934 if (s
->scope
== local_scope
&& (s
->c
== -1 || tok
!= '{'))
2935 goto do_decl
; /* at least one must be incomplete type */
2942 /* we put an undefined size for struct/union */
2943 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
2944 s
->r
= 0; /* default alignment is zero as gcc */
2945 /* put struct/union/enum name in type */
2953 tcc_error("struct/union/enum already defined");
2954 /* cannot be empty */
2956 /* non empty enums are not allowed */
2957 if (a
== TOK_ENUM
) {
2961 expect("identifier");
2963 if (ss
&& !local_stack
)
2964 tcc_error("redefinition of enumerator '%s'",
2965 get_tok_str(v
, NULL
));
2971 /* enum symbols have static storage */
2972 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
2973 ss
->type
.t
|= VT_STATIC
;
2978 /* NOTE: we accept a trailing comma */
2982 s
->c
= type_size(&int_type
, &align
);
2991 while (tok
!= '}') {
2992 parse_btype(&btype
, &ad1
);
2995 tcc_error("flexible array member '%s' not at the end of struct",
2996 get_tok_str(v
, NULL
));
3001 type_decl(&type1
, &ad1
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3003 if ((type1
.t
& VT_BTYPE
) != VT_STRUCT
)
3004 expect("identifier");
3006 int v
= btype
.ref
->v
;
3007 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
3008 if (tcc_state
->ms_extensions
== 0)
3009 expect("identifier");
3013 if (type_size(&type1
, &align
) < 0) {
3014 if ((a
== TOK_STRUCT
) && (type1
.t
& VT_ARRAY
) && c
)
3017 tcc_error("field '%s' has incomplete type",
3018 get_tok_str(v
, NULL
));
3020 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
3021 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
3022 tcc_error("invalid type for '%s'",
3023 get_tok_str(v
, NULL
));
3027 bit_size
= expr_const();
3028 /* XXX: handle v = 0 case for messages */
3030 tcc_error("negative width in bit-field '%s'",
3031 get_tok_str(v
, NULL
));
3032 if (v
&& bit_size
== 0)
3033 tcc_error("zero width for bit-field '%s'",
3034 get_tok_str(v
, NULL
));
3036 size
= type_size(&type1
, &align
);
3037 if (ad1
.a
.aligned
) {
3038 if (align
< ad1
.a
.aligned
)
3039 align
= ad1
.a
.aligned
;
3040 } else if (ad1
.a
.packed
) {
3042 } else if (*tcc_state
->pack_stack_ptr
) {
3043 if (align
> *tcc_state
->pack_stack_ptr
)
3044 align
= *tcc_state
->pack_stack_ptr
;
3047 if (bit_size
>= 0) {
3048 bt
= type1
.t
& VT_BTYPE
;
3055 tcc_error("bitfields must have scalar type");
3057 if (bit_size
> bsize
) {
3058 tcc_error("width of '%s' exceeds its type",
3059 get_tok_str(v
, NULL
));
3060 } else if (bit_size
== bsize
) {
3061 /* no need for bit fields */
3063 } else if (bit_size
== 0) {
3064 /* XXX: what to do if only padding in a
3066 /* zero size: means to pad */
3069 /* we do not have enough room ?
3070 did the type change?
3072 if ((bit_pos
+ bit_size
) > bsize
||
3073 bt
!= prevbt
|| a
== TOK_UNION
)
3076 /* XXX: handle LSB first */
3077 type1
.t
|= VT_BITFIELD
|
3078 (bit_pos
<< VT_STRUCT_SHIFT
) |
3079 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
3080 bit_pos
+= bit_size
;
3086 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
3087 /* add new memory data only if starting
3089 if (lbit_pos
== 0) {
3090 if (a
== TOK_STRUCT
) {
3091 c
= (c
+ align
- 1) & -align
;
3100 if (align
> maxalign
)
3104 printf("add field %s offset=%d",
3105 get_tok_str(v
, NULL
), offset
);
3106 if (type1
.t
& VT_BITFIELD
) {
3107 printf(" pos=%d size=%d",
3108 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
3109 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
3114 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
3116 while ((ass
= ass
->next
) != NULL
) {
3117 ss
= sym_push(ass
->v
, &ass
->type
, 0, offset
+ ass
->c
);
3122 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
3126 if (tok
== ';' || tok
== TOK_EOF
)
3133 /* store size and alignment */
3134 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
3140 /* return 1 if basic type is a type size (short, long, long long) */
3141 ST_FUNC
int is_btype_size(int bt
)
3143 return bt
== VT_SHORT
|| bt
== VT_LONG
|| bt
== VT_LLONG
;
3146 /* Add type qualifiers to a type. If the type is an array then the qualifiers
3147 are added to the element type, copied because it could be a typedef. */
3148 static void parse_btype_qualify(CType
*type
, int qualifiers
)
3150 while (type
->t
& VT_ARRAY
) {
3151 type
->ref
= sym_push(SYM_FIELD
, &type
->ref
->type
, 0, type
->ref
->c
);
3152 type
= &type
->ref
->type
;
3154 type
->t
|= qualifiers
;
3157 /* return 0 if no type declaration. otherwise, return the basic type
3160 static int parse_btype(CType
*type
, AttributeDef
*ad
)
3162 int t
, u
, bt_size
, complete
, type_found
, typespec_found
;
3166 memset(ad
, 0, sizeof(AttributeDef
));
3174 /* currently, we really ignore extension */
3185 tcc_error("too many basic types");
3187 bt_size
= is_btype_size (u
& VT_BTYPE
);
3188 if (u
== VT_INT
|| (!bt_size
&& !(t
& VT_TYPEDEF
)))
3203 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
3204 #ifndef TCC_TARGET_PE
3205 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3207 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
3208 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3214 #ifdef TCC_TARGET_ARM64
3216 /* GCC's __uint128_t appears in some Linux header files. Make it a
3217 synonym for long double to get the size and alignment right. */
3229 if ((t
& VT_BTYPE
) == VT_LONG
) {
3230 #ifdef TCC_TARGET_PE
3231 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
3233 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3241 struct_decl(&type1
, ad
, VT_ENUM
);
3244 type
->ref
= type1
.ref
;
3248 struct_decl(&type1
, ad
, VT_STRUCT
);
3251 /* type modifiers */
3256 parse_btype_qualify(type
, VT_CONSTANT
);
3264 parse_btype_qualify(type
, VT_VOLATILE
);
3271 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == (VT_DEFSIGN
|VT_UNSIGNED
))
3272 tcc_error("signed and unsigned modifier");
3285 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == VT_DEFSIGN
)
3286 tcc_error("signed and unsigned modifier");
3287 t
|= VT_DEFSIGN
| VT_UNSIGNED
;
3312 /* GNUC attribute */
3313 case TOK_ATTRIBUTE1
:
3314 case TOK_ATTRIBUTE2
:
3315 parse_attribute(ad
);
3318 t
= (t
& ~VT_BTYPE
) | u
;
3326 parse_expr_type(&type1
);
3327 /* remove all storage modifiers except typedef */
3328 type1
.t
&= ~(VT_STORAGE
&~VT_TYPEDEF
);
3334 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
3337 type
->t
= ((s
->type
.t
& ~VT_TYPEDEF
) |
3338 (t
& ~(VT_CONSTANT
| VT_VOLATILE
)));
3339 type
->ref
= s
->type
.ref
;
3340 if (t
& (VT_CONSTANT
| VT_VOLATILE
))
3341 parse_btype_qualify(type
, t
& (VT_CONSTANT
| VT_VOLATILE
));
3345 /* get attributes from typedef */
3346 if (0 == ad
->a
.aligned
)
3347 ad
->a
.aligned
= s
->a
.aligned
;
3348 if (0 == ad
->a
.func_call
)
3349 ad
->a
.func_call
= s
->a
.func_call
;
3350 ad
->a
.packed
|= s
->a
.packed
;
3359 if (tcc_state
->char_is_unsigned
) {
3360 if ((t
& (VT_DEFSIGN
|VT_BTYPE
)) == VT_BYTE
)
3364 /* long is never used as type */
3365 if ((t
& VT_BTYPE
) == VT_LONG
)
3366 #if (!defined TCC_TARGET_X86_64 && !defined TCC_TARGET_ARM64) || \
3367 defined TCC_TARGET_PE
3368 t
= (t
& ~VT_BTYPE
) | VT_INT
;
3370 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3376 /* convert a function parameter type (array to pointer and function to
3377 function pointer) */
3378 static inline void convert_parameter_type(CType
*pt
)
3380 /* remove const and volatile qualifiers (XXX: const could be used
3381 to indicate a const function parameter */
3382 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3383 /* array must be transformed to pointer according to ANSI C */
3385 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
3390 ST_FUNC
void parse_asm_str(CString
*astr
)
3393 /* read the string */
3395 expect("string constant");
3397 while (tok
== TOK_STR
) {
3398 /* XXX: add \0 handling too ? */
3399 cstr_cat(astr
, tokc
.str
.data
, -1);
3402 cstr_ccat(astr
, '\0');
3405 /* Parse an asm label and return the token */
3406 static int asm_label_instr(void)
3412 parse_asm_str(&astr
);
3415 printf("asm_alias: \"%s\"\n", (char *)astr
.data
);
3417 v
= tok_alloc(astr
.data
, astr
.size
- 1)->tok
;
3422 static void post_type(CType
*type
, AttributeDef
*ad
)
3424 int n
, l
, t1
, arg_size
, align
;
3425 Sym
**plast
, *s
, *first
;
3430 /* function declaration */
3437 int ls
= local_scope
;
3438 local_scope
= 1; /* for struct decl inside function params */
3440 /* read param name and compute offset */
3441 if (l
!= FUNC_OLD
) {
3442 if (!parse_btype(&pt
, &ad1
)) {
3444 tcc_error("invalid type");
3451 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
3453 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3454 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
3455 tcc_error("parameter declared as void");
3456 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
3461 expect("identifier");
3465 convert_parameter_type(&pt
);
3466 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
3472 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
3480 /* if no parameters, then old type prototype */
3484 /* NOTE: const is ignored in returned type as it has a special
3485 meaning in gcc / C++ */
3486 type
->t
&= ~VT_CONSTANT
;
3487 /* some ancient pre-K&R C allows a function to return an array
3488 and the array brackets to be put after the arguments, such
3489 that "int c()[]" means something like "int[] c()" */
3492 skip(']'); /* only handle simple "[]" */
3495 /* we push a anonymous symbol which will contain the function prototype */
3496 ad
->a
.func_args
= arg_size
;
3497 s
= sym_push(SYM_FIELD
, type
, 0, l
);
3502 } else if (tok
== '[') {
3503 /* array definition */
3505 if (tok
== TOK_RESTRICT1
)
3510 if (!local_stack
|| nocode_wanted
)
3511 vpushi(expr_const());
3513 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3516 tcc_error("invalid array size");
3518 if (!is_integer_btype(vtop
->type
.t
& VT_BTYPE
))
3519 tcc_error("size of variable length array should be an integer");
3524 /* parse next post type */
3525 post_type(type
, ad
);
3526 if (type
->t
== VT_FUNC
)
3527 tcc_error("declaration of an array of functions");
3528 t1
|= type
->t
& VT_VLA
;
3531 loc
-= type_size(&int_type
, &align
);
3535 vla_runtime_type_size(type
, &align
);
3537 vset(&int_type
, VT_LOCAL
|VT_LVAL
, n
);
3544 /* we push an anonymous symbol which will contain the array
3546 s
= sym_push(SYM_FIELD
, type
, 0, n
);
3547 type
->t
= (t1
? VT_VLA
: VT_ARRAY
) | VT_PTR
;
3552 /* Parse a type declaration (except basic type), and return the type
3553 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3554 expected. 'type' should contain the basic type. 'ad' is the
3555 attribute definition of the basic type. It can be modified by
3558 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
3561 CType type1
, *type2
;
3562 int qualifiers
, storage
;
3564 while (tok
== '*') {
3572 qualifiers
|= VT_CONSTANT
;
3577 qualifiers
|= VT_VOLATILE
;
3585 type
->t
|= qualifiers
;
3588 /* XXX: clarify attribute handling */
3589 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3590 parse_attribute(ad
);
3592 /* recursive type */
3593 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3594 type1
.t
= 0; /* XXX: same as int */
3597 /* XXX: this is not correct to modify 'ad' at this point, but
3598 the syntax is not clear */
3599 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3600 parse_attribute(ad
);
3601 type_decl(&type1
, ad
, v
, td
);
3604 /* type identifier */
3605 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
3609 if (!(td
& TYPE_ABSTRACT
))
3610 expect("identifier");
3614 storage
= type
->t
& VT_STORAGE
;
3615 type
->t
&= ~VT_STORAGE
;
3616 if (storage
& VT_STATIC
) {
3617 int saved_nocode_wanted
= nocode_wanted
;
3619 post_type(type
, ad
);
3620 nocode_wanted
= saved_nocode_wanted
;
3622 post_type(type
, ad
);
3624 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3625 parse_attribute(ad
);
3629 /* append type at the end of type1 */
3642 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3643 ST_FUNC
int lvalue_type(int t
)
3648 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
3650 else if (bt
== VT_SHORT
)
3654 if (t
& VT_UNSIGNED
)
3655 r
|= VT_LVAL_UNSIGNED
;
3659 /* indirection with full error checking and bound check */
3660 ST_FUNC
void indir(void)
3662 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
3663 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
3667 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
3669 vtop
->type
= *pointed_type(&vtop
->type
);
3670 /* Arrays and functions are never lvalues */
3671 if (!(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_VLA
)
3672 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3673 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3674 /* if bound checking, the referenced pointer must be checked */
3675 #ifdef CONFIG_TCC_BCHECK
3676 if (tcc_state
->do_bounds_check
)
3677 vtop
->r
|= VT_MUSTBOUND
;
3682 /* pass a parameter to a function and do type checking and casting */
3683 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
3688 func_type
= func
->c
;
3689 if (func_type
== FUNC_OLD
||
3690 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
3691 /* default casting : only need to convert float to double */
3692 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
3695 } else if (vtop
->type
.t
& VT_BITFIELD
) {
3696 type
.t
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
3699 } else if (arg
== NULL
) {
3700 tcc_error("too many arguments to function");
3703 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
3704 gen_assign_cast(&type
);
3708 /* parse an expression of the form '(type)' or '(expr)' and return its
3710 static void parse_expr_type(CType
*type
)
3716 if (parse_btype(type
, &ad
)) {
3717 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3724 static void parse_type(CType
*type
)
3729 if (!parse_btype(type
, &ad
)) {
3732 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3735 static void vpush_tokc(int t
)
3740 vsetc(&type
, VT_CONST
, &tokc
);
3743 ST_FUNC
void unary(void)
3745 int n
, t
, align
, size
, r
, sizeof_caller
;
3749 static int in_sizeof
= 0;
3751 sizeof_caller
= in_sizeof
;
3753 /* XXX: GCC 2.95.3 does not generate a table although it should be
3767 vpush_tokc(VT_INT
| VT_UNSIGNED
);
3771 vpush_tokc(VT_LLONG
);
3775 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
3779 vpush_tokc(VT_FLOAT
);
3783 vpush_tokc(VT_DOUBLE
);
3787 vpush_tokc(VT_LDOUBLE
);
3790 case TOK___FUNCTION__
:
3792 goto tok_identifier
;
3798 /* special function name identifier */
3799 len
= strlen(funcname
) + 1;
3800 /* generate char[len] type */
3805 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
3806 ptr
= section_ptr_add(data_section
, len
);
3807 memcpy(ptr
, funcname
, len
);
3812 #ifdef TCC_TARGET_PE
3813 t
= VT_SHORT
| VT_UNSIGNED
;
3819 /* string parsing */
3822 if (tcc_state
->warn_write_strings
)
3827 memset(&ad
, 0, sizeof(AttributeDef
));
3828 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, 0);
3833 if (parse_btype(&type
, &ad
)) {
3834 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
3836 /* check ISOC99 compound literal */
3838 /* data is allocated locally by default */
3843 /* all except arrays are lvalues */
3844 if (!(type
.t
& VT_ARRAY
))
3845 r
|= lvalue_type(type
.t
);
3846 memset(&ad
, 0, sizeof(AttributeDef
));
3847 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, 0);
3849 if (sizeof_caller
) {
3856 } else if (tok
== '{') {
3858 tcc_error("expected constant");
3859 /* save all registers */
3862 /* statement expression : we do not accept break/continue
3863 inside as GCC does */
3864 block(NULL
, NULL
, NULL
, NULL
, 0, 1);
3879 /* functions names must be treated as function pointers,
3880 except for unary '&' and sizeof. Since we consider that
3881 functions are not lvalues, we only have to handle it
3882 there and in function calls. */
3883 /* arrays can also be used although they are not lvalues */
3884 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
3885 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
3887 mk_pointer(&vtop
->type
);
3893 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3895 boolean
.t
= VT_BOOL
;
3897 vtop
->c
.i
= !vtop
->c
.i
;
3898 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
3902 vseti(VT_JMP
, gvtst(1, 0));
3914 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
3915 tcc_error("pointer not accepted for unary plus");
3916 /* In order to force cast, we add zero, except for floating point
3917 where we really need an noop (otherwise -0.0 will be transformed
3919 if (!is_float(vtop
->type
.t
)) {
3930 unary_type(&type
); // Perform a in_sizeof = 0;
3931 size
= type_size(&type
, &align
);
3932 if (t
== TOK_SIZEOF
) {
3933 if (!(type
.t
& VT_VLA
)) {
3935 tcc_error("sizeof applied to an incomplete type");
3938 vla_runtime_type_size(&type
, &align
);
3943 vtop
->type
.t
|= VT_UNSIGNED
;
3946 case TOK_builtin_expect
:
3948 /* __builtin_expect is a no-op for now */
3949 int saved_nocode_wanted
;
3954 saved_nocode_wanted
= nocode_wanted
;
3958 nocode_wanted
= saved_nocode_wanted
;
3962 case TOK_builtin_types_compatible_p
:
3971 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3972 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3973 vpushi(is_compatible_types(&type1
, &type2
));
3976 case TOK_builtin_constant_p
:
3978 int saved_nocode_wanted
, res
;
3981 saved_nocode_wanted
= nocode_wanted
;
3984 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
3986 nocode_wanted
= saved_nocode_wanted
;
3991 case TOK_builtin_frame_address
:
3992 case TOK_builtin_return_address
:
3999 if (tok
!= TOK_CINT
) {
4000 tcc_error("%s only takes positive integers",
4001 tok1
== TOK_builtin_return_address
?
4002 "__builtin_return_address" :
4003 "__builtin_frame_address");
4005 level
= (uint32_t)tokc
.i
;
4010 vset(&type
, VT_LOCAL
, 0); /* local frame */
4012 mk_pointer(&vtop
->type
);
4013 indir(); /* -> parent frame */
4015 if (tok1
== TOK_builtin_return_address
) {
4016 // assume return address is just above frame pointer on stack
4019 mk_pointer(&vtop
->type
);
4024 #ifdef TCC_TARGET_X86_64
4025 #ifdef TCC_TARGET_PE
4026 case TOK_builtin_va_start
:
4034 if ((vtop
->r
& VT_VALMASK
) != VT_LOCAL
)
4035 tcc_error("__builtin_va_start expects a local variable");
4036 vtop
->r
&= ~(VT_LVAL
| VT_REF
);
4037 vtop
->type
= char_pointer_type
;
4042 case TOK_builtin_va_arg_types
:
4049 vpushi(classify_x86_64_va_arg(&type
));
4055 #ifdef TCC_TARGET_ARM64
4056 case TOK___va_start
: {
4058 tcc_error("statement in global scope");
4068 vtop
->type
.t
= VT_VOID
;
4071 case TOK___va_arg
: {
4074 tcc_error("statement in global scope");
4086 case TOK___arm64_clear_cache
: {
4095 vtop
->type
.t
= VT_VOID
;
4099 /* pre operations */
4110 t
= vtop
->type
.t
& VT_BTYPE
;
4112 /* In IEEE negate(x) isn't subtract(0,x), but rather
4117 else if (t
== VT_DOUBLE
)
4128 goto tok_identifier
;
4130 /* allow to take the address of a label */
4131 if (tok
< TOK_UIDENT
)
4132 expect("label identifier");
4133 s
= label_find(tok
);
4135 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4137 if (s
->r
== LABEL_DECLARED
)
4138 s
->r
= LABEL_FORWARD
;
4141 s
->type
.t
= VT_VOID
;
4142 mk_pointer(&s
->type
);
4143 s
->type
.t
|= VT_STATIC
;
4145 vpushsym(&s
->type
, s
);
4149 // special qnan , snan and infinity values
4151 vpush64(VT_DOUBLE
, 0x7ff8000000000000ULL
);
4155 vpush64(VT_DOUBLE
, 0x7ff0000000000001ULL
);
4159 vpush64(VT_DOUBLE
, 0x7ff0000000000000ULL
);
4168 expect("identifier");
4171 const char *name
= get_tok_str(t
, NULL
);
4173 tcc_error("'%s' undeclared", name
);
4174 /* for simple function calls, we tolerate undeclared
4175 external reference to int() function */
4176 if (tcc_state
->warn_implicit_function_declaration
4177 #ifdef TCC_TARGET_PE
4178 /* people must be warned about using undeclared WINAPI functions
4179 (which usually start with uppercase letter) */
4180 || (name
[0] >= 'A' && name
[0] <= 'Z')
4183 tcc_warning("implicit declaration of function '%s'", name
);
4184 s
= external_global_sym(t
, &func_old_type
, 0);
4186 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
4187 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
4188 /* if referencing an inline function, then we generate a
4189 symbol to it if not already done. It will have the
4190 effect to generate code for it at the end of the
4191 compilation unit. Inline function as always
4192 generated in the text section. */
4194 put_extern_sym(s
, text_section
, 0, 0);
4195 r
= VT_SYM
| VT_CONST
;
4199 vset(&s
->type
, r
, s
->c
);
4200 /* if forward reference, we must point to s */
4201 if (vtop
->r
& VT_SYM
) {
4208 /* post operations */
4210 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
4213 } else if (tok
== '.' || tok
== TOK_ARROW
|| tok
== TOK_CDOUBLE
) {
4216 if (tok
== TOK_ARROW
)
4218 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
4221 /* expect pointer on structure */
4222 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
4223 expect("struct or union");
4224 if (tok
== TOK_CDOUBLE
)
4225 expect("field name");
4227 if (tok
== TOK_CINT
|| tok
== TOK_CUINT
)
4228 expect("field name");
4232 while ((s
= s
->next
) != NULL
) {
4237 tcc_error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, &tokc
));
4238 /* add field offset to pointer */
4239 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
4242 /* change type to field type, and set to lvalue */
4243 vtop
->type
= s
->type
;
4244 vtop
->type
.t
|= qualifiers
;
4245 /* an array is never an lvalue */
4246 if (!(vtop
->type
.t
& VT_ARRAY
)) {
4247 vtop
->r
|= lvalue_type(vtop
->type
.t
);
4248 #ifdef CONFIG_TCC_BCHECK
4249 /* if bound checking, the referenced pointer must be checked */
4250 if (tcc_state
->do_bounds_check
)
4251 vtop
->r
|= VT_MUSTBOUND
;
4255 } else if (tok
== '[') {
4261 } else if (tok
== '(') {
4264 int nb_args
, ret_nregs
, ret_align
, regsize
, variadic
;
4267 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
4268 /* pointer test (no array accepted) */
4269 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
4270 vtop
->type
= *pointed_type(&vtop
->type
);
4271 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
4275 expect("function pointer");
4278 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
4280 /* get return type */
4283 sa
= s
->next
; /* first parameter */
4286 /* compute first implicit argument if a structure is returned */
4287 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
4288 variadic
= (s
->c
== FUNC_ELLIPSIS
);
4289 ret_nregs
= gfunc_sret(&s
->type
, variadic
, &ret
.type
,
4290 &ret_align
, ®size
);
4292 /* get some space for the returned structure */
4293 size
= type_size(&s
->type
, &align
);
4294 #ifdef TCC_TARGET_ARM64
4295 /* On arm64, a small struct is return in registers.
4296 It is much easier to write it to memory if we know
4297 that we are allowed to write some extra bytes, so
4298 round the allocated space up to a power of 2: */
4300 while (size
& (size
- 1))
4301 size
= (size
| (size
- 1)) + 1;
4303 loc
= (loc
- size
) & -align
;
4305 ret
.r
= VT_LOCAL
| VT_LVAL
;
4306 /* pass it as 'int' to avoid structure arg passing
4308 vseti(VT_LOCAL
, loc
);
4318 /* return in register */
4319 if (is_float(ret
.type
.t
)) {
4320 ret
.r
= reg_fret(ret
.type
.t
);
4321 #ifdef TCC_TARGET_X86_64
4322 if ((ret
.type
.t
& VT_BTYPE
) == VT_QFLOAT
)
4326 #ifndef TCC_TARGET_ARM64
4327 #ifdef TCC_TARGET_X86_64
4328 if ((ret
.type
.t
& VT_BTYPE
) == VT_QLONG
)
4330 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
4341 gfunc_param_typed(s
, sa
);
4351 tcc_error("too few arguments to function");
4353 if (!nocode_wanted
) {
4354 gfunc_call(nb_args
);
4356 vtop
-= (nb_args
+ 1);
4360 for (r
= ret
.r
+ ret_nregs
+ !ret_nregs
; r
-- > ret
.r
;) {
4361 vsetc(&ret
.type
, r
, &ret
.c
);
4362 vtop
->r2
= ret
.r2
; /* Loop only happens when r2 is VT_CONST */
4365 /* handle packed struct return */
4366 if (((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) && ret_nregs
) {
4369 size
= type_size(&s
->type
, &align
);
4370 /* We're writing whole regs often, make sure there's enough
4371 space. Assume register size is power of 2. */
4372 if (regsize
> align
)
4374 loc
= (loc
- size
) & -align
;
4378 vset(&ret
.type
, VT_LOCAL
| VT_LVAL
, addr
+ offset
);
4382 if (--ret_nregs
== 0)
4386 vset(&s
->type
, VT_LOCAL
| VT_LVAL
, addr
);
4394 ST_FUNC
void expr_prod(void)
4399 while (tok
== '*' || tok
== '/' || tok
== '%') {
4407 ST_FUNC
void expr_sum(void)
4412 while (tok
== '+' || tok
== '-') {
4420 static void expr_shift(void)
4425 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
4433 static void expr_cmp(void)
4438 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
4439 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
4447 static void expr_cmpeq(void)
4452 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
4460 static void expr_and(void)
4463 while (tok
== '&') {
4470 static void expr_xor(void)
4473 while (tok
== '^') {
4480 static void expr_or(void)
4483 while (tok
== '|') {
4490 /* XXX: fix this mess */
4491 static void expr_land_const(void)
4494 while (tok
== TOK_LAND
) {
4500 static void expr_lor_const(void)
4503 while (tok
== TOK_LOR
) {
4510 static void expr_land(void)
4513 if (tok
== TOK_LAND
) {
4514 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4525 int saved_nocode_wanted
= nocode_wanted
;
4529 nocode_wanted
= saved_nocode_wanted
;
4537 if (tok
!= TOK_LAND
) {
4548 static void expr_lor(void)
4551 if (tok
== TOK_LOR
) {
4552 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4559 int saved_nocode_wanted
= nocode_wanted
;
4563 nocode_wanted
= saved_nocode_wanted
;
4575 if (tok
!= TOK_LOR
) {
4586 static void expr_cond(void)
4588 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
, islv
;
4590 CType type
, type1
, type2
;
4595 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4596 int saved_nocode_wanted
= nocode_wanted
;
4599 boolean
.t
= VT_BOOL
;
4605 if (tok
!= ':' || !gnu_ext
) {
4613 nocode_wanted
= saved_nocode_wanted
;
4616 if (tok
!= ':' || !gnu_ext
) {
4620 nocode_wanted
= saved_nocode_wanted
;
4627 if (vtop
!= vstack
) {
4628 /* needed to avoid having different registers saved in
4630 if (is_float(vtop
->type
.t
)) {
4632 #ifdef TCC_TARGET_X86_64
4633 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4643 if (tok
== ':' && gnu_ext
) {
4651 sv
= *vtop
; /* save value to handle it later */
4652 vtop
--; /* no vpop so that FP stack is not flushed */
4660 bt1
= t1
& VT_BTYPE
;
4662 bt2
= t2
& VT_BTYPE
;
4663 /* cast operands to correct type according to ISOC rules */
4664 if (is_float(bt1
) || is_float(bt2
)) {
4665 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
4666 type
.t
= VT_LDOUBLE
;
4667 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
4672 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
4673 /* cast to biggest op */
4675 /* convert to unsigned if it does not fit in a long long */
4676 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
4677 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
4678 type
.t
|= VT_UNSIGNED
;
4679 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
4680 /* If one is a null ptr constant the result type
4682 if (is_null_pointer (vtop
))
4684 else if (is_null_pointer (&sv
))
4686 /* XXX: test pointer compatibility, C99 has more elaborate
4690 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
4691 /* XXX: test function pointer compatibility */
4692 type
= bt1
== VT_FUNC
? type1
: type2
;
4693 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
4694 /* XXX: test structure compatibility */
4695 type
= bt1
== VT_STRUCT
? type1
: type2
;
4696 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
4697 /* NOTE: as an extension, we accept void on only one side */
4700 /* integer operations */
4702 /* convert to unsigned if it does not fit in an integer */
4703 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
4704 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
4705 type
.t
|= VT_UNSIGNED
;
4707 /* keep structs lvalue by transforming `(expr ? a : b)` to `*(expr ? &a : &b)` so
4708 that `(expr ? a : b).mem` does not error with "lvalue expected" */
4709 islv
= (vtop
->r
& VT_LVAL
) && (sv
.r
& VT_LVAL
) && VT_STRUCT
== (type
.t
& VT_BTYPE
);
4711 /* now we convert second operand */
4714 mk_pointer(&vtop
->type
);
4717 else if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4720 if (is_float(type
.t
)) {
4722 #ifdef TCC_TARGET_X86_64
4723 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4727 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
4728 /* for long longs, we use fixed registers to avoid having
4729 to handle a complicated move */
4734 /* this is horrible, but we must also convert first
4738 /* put again first value and cast it */
4742 mk_pointer(&vtop
->type
);
4745 else if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4748 move_reg(r2
, r1
, type
.t
);
4757 static void expr_eq(void)
4763 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
4764 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
4765 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
4780 ST_FUNC
void gexpr(void)
4791 /* parse an expression and return its type without any side effect. */
4792 static void expr_type(CType
*type
)
4794 int saved_nocode_wanted
;
4796 saved_nocode_wanted
= nocode_wanted
;
4801 nocode_wanted
= saved_nocode_wanted
;
4804 /* parse a unary expression and return its type without any side
4806 static void unary_type(CType
*type
)
4818 /* parse a constant expression and return value in vtop. */
4819 static void expr_const1(void)
4828 /* parse an integer constant and return its value. */
4829 ST_FUNC
int expr_const(void)
4833 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
4834 expect("constant expression");
4840 /* return the label token if current token is a label, otherwise
4842 static int is_label(void)
4846 /* fast test first */
4847 if (tok
< TOK_UIDENT
)
4849 /* no need to save tokc because tok is an identifier */
4856 unget_tok(last_tok
);
4861 static void label_or_decl(int l
)
4865 /* fast test first */
4866 if (tok
>= TOK_UIDENT
)
4868 /* no need to save tokc because tok is an identifier */
4872 unget_tok(last_tok
);
4875 unget_tok(last_tok
);
4880 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
4881 int case_reg
, int is_expr
)
4886 /* generate line number info */
4887 if (tcc_state
->do_debug
&&
4888 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
4889 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
4891 last_line_num
= file
->line_num
;
4895 /* default return value is (void) */
4897 vtop
->type
.t
= VT_VOID
;
4900 if (tok
== TOK_IF
) {
4907 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4909 if (c
== TOK_ELSE
) {
4913 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4914 gsym(d
); /* patch else jmp */
4917 } else if (tok
== TOK_WHILE
) {
4926 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4931 } else if (tok
== '{') {
4933 int block_vla_sp_loc
= vla_sp_loc
, saved_vlas_in_scope
= vlas_in_scope
;
4936 /* record local declaration stack position */
4938 llabel
= local_label_stack
;
4941 /* handle local labels declarations */
4942 if (tok
== TOK_LABEL
) {
4945 if (tok
< TOK_UIDENT
)
4946 expect("label identifier");
4947 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
4957 while (tok
!= '}') {
4958 label_or_decl(VT_LOCAL
);
4962 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4965 /* pop locally defined labels */
4966 label_pop(&local_label_stack
, llabel
);
4968 /* XXX: this solution makes only valgrind happy...
4969 triggered by gcc.c-torture/execute/20000917-1.c */
4971 switch(vtop
->type
.t
& VT_BTYPE
) {
4973 /* this breaks a compilation of the linux kernel v2.4.26 */
4974 /* pmd_t *new = ({ __asm__ __volatile__("ud2\n") ; ((pmd_t *)1); }); */
4975 /* Look a commit a80acab: Display error on statement expressions with complex return type */
4976 /* A pointer is not a complex return type */
4980 for(p
=vtop
->type
.ref
;p
;p
=p
->prev
)
4982 tcc_error("unsupported expression type");
4985 /* pop locally defined symbols */
4987 sym_pop(&local_stack
, s
);
4989 /* Pop VLA frames and restore stack pointer if required */
4990 if (vlas_in_scope
> saved_vlas_in_scope
) {
4991 vla_sp_loc
= saved_vlas_in_scope
? block_vla_sp_loc
: vla_sp_root_loc
;
4994 vlas_in_scope
= saved_vlas_in_scope
;
4997 } else if (tok
== TOK_RETURN
) {
5001 gen_assign_cast(&func_vt
);
5002 #ifdef TCC_TARGET_ARM64
5003 // Perhaps it would be better to use this for all backends:
5006 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
5007 CType type
, ret_type
;
5008 int ret_align
, ret_nregs
, regsize
;
5009 ret_nregs
= gfunc_sret(&func_vt
, func_var
, &ret_type
,
5010 &ret_align
, ®size
);
5011 if (0 == ret_nregs
) {
5012 /* if returning structure, must copy it to implicit
5013 first pointer arg location */
5016 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
5019 /* copy structure value to pointer */
5022 /* returning structure packed into registers */
5023 int r
, size
, addr
, align
;
5024 size
= type_size(&func_vt
,&align
);
5025 if ((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) ||
5026 (vtop
->c
.i
& (ret_align
-1)))
5027 && (align
& (ret_align
-1))) {
5028 loc
= (loc
- size
) & -ret_align
;
5031 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
5035 vset(&ret_type
, VT_LOCAL
| VT_LVAL
, addr
);
5037 vtop
->type
= ret_type
;
5038 if (is_float(ret_type
.t
))
5039 r
= rc_fret(ret_type
.t
);
5045 if (--ret_nregs
== 0)
5047 /* We assume that when a structure is returned in multiple
5048 registers, their classes are consecutive values of the
5051 vtop
->c
.i
+= regsize
;
5052 vtop
->r
= VT_LOCAL
| VT_LVAL
;
5055 } else if (is_float(func_vt
.t
)) {
5056 gv(rc_fret(func_vt
.t
));
5061 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
5064 rsym
= gjmp(rsym
); /* jmp */
5065 } else if (tok
== TOK_BREAK
) {
5068 tcc_error("cannot break");
5069 *bsym
= gjmp(*bsym
);
5072 } else if (tok
== TOK_CONTINUE
) {
5075 tcc_error("cannot continue");
5076 vla_sp_restore_root();
5077 *csym
= gjmp(*csym
);
5080 } else if (tok
== TOK_FOR
) {
5087 /* c99 for-loop init decl? */
5088 if (!decl0(VT_LOCAL
, 1)) {
5089 /* no, regular for-loop init expr */
5115 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
5121 sym_pop(&local_stack
, s
);
5124 if (tok
== TOK_DO
) {
5130 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
5141 if (tok
== TOK_SWITCH
) {
5145 /* XXX: other types than integer */
5146 case_reg
= gv(RC_INT
);
5150 b
= gjmp(0); /* jump to first case */
5152 block(&a
, csym
, &b
, &c
, case_reg
, 0);
5153 /* if no default, jmp after switch */
5161 if (tok
== TOK_CASE
) {
5168 if (gnu_ext
&& tok
== TOK_DOTS
) {
5172 tcc_warning("empty case range");
5174 /* since a case is like a label, we must skip it with a jmp */
5182 *case_sym
= gtst(1, 0);
5185 *case_sym
= gtst(1, 0);
5189 *case_sym
= gtst(1, *case_sym
);
5191 case_reg
= gv(RC_INT
);
5196 goto block_after_label
;
5198 if (tok
== TOK_DEFAULT
) {
5204 tcc_error("too many 'default'");
5207 goto block_after_label
;
5209 if (tok
== TOK_GOTO
) {
5211 if (tok
== '*' && gnu_ext
) {
5215 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
5218 } else if (tok
>= TOK_UIDENT
) {
5219 s
= label_find(tok
);
5220 /* put forward definition if needed */
5222 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
5224 if (s
->r
== LABEL_DECLARED
)
5225 s
->r
= LABEL_FORWARD
;
5227 vla_sp_restore_root();
5228 if (s
->r
& LABEL_FORWARD
)
5229 s
->jnext
= gjmp(s
->jnext
);
5231 gjmp_addr(s
->jnext
);
5234 expect("label identifier");
5237 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
5245 if (s
->r
== LABEL_DEFINED
)
5246 tcc_error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
5248 s
->r
= LABEL_DEFINED
;
5250 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
5254 /* we accept this, but it is a mistake */
5257 tcc_warning("deprecated use of label at end of compound statement");
5261 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
5264 /* expression case */
5279 /* t is the array or struct type. c is the array or struct
5280 address. cur_index/cur_field is the pointer to the current
5281 value. 'size_only' is true if only size info is needed (only used
5283 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
5284 int *cur_index
, Sym
**cur_field
,
5288 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
5294 if (gnu_ext
&& (l
= is_label()) != 0)
5296 while (tok
== '[' || tok
== '.') {
5298 if (!(type
->t
& VT_ARRAY
))
5299 expect("array type");
5302 index
= expr_const();
5303 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
5304 expect("invalid index");
5305 if (tok
== TOK_DOTS
&& gnu_ext
) {
5307 index_last
= expr_const();
5308 if (index_last
< 0 ||
5309 (s
->c
>= 0 && index_last
>= s
->c
) ||
5311 expect("invalid index");
5317 *cur_index
= index_last
;
5318 type
= pointed_type(type
);
5319 elem_size
= type_size(type
, &align
);
5320 c
+= index
* elem_size
;
5321 /* NOTE: we only support ranges for last designator */
5322 nb_elems
= index_last
- index
+ 1;
5323 if (nb_elems
!= 1) {
5332 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
5333 expect("struct/union type");
5346 /* XXX: fix this mess by using explicit storage field */
5348 type1
.t
|= (type
->t
& ~VT_TYPE
);
5362 if (type
->t
& VT_ARRAY
) {
5364 type
= pointed_type(type
);
5365 c
+= index
* type_size(type
, &align
);
5369 tcc_error("too many field init");
5370 /* XXX: fix this mess by using explicit storage field */
5372 type1
.t
|= (type
->t
& ~VT_TYPE
);
5377 decl_initializer(type
, sec
, c
, 0, size_only
);
5379 /* XXX: make it more general */
5380 if (!size_only
&& nb_elems
> 1) {
5381 unsigned long c_end
;
5386 tcc_error("range init not supported yet for dynamic storage");
5387 c_end
= c
+ nb_elems
* elem_size
;
5388 if (c_end
> sec
->data_allocated
)
5389 section_realloc(sec
, c_end
);
5390 src
= sec
->data
+ c
;
5392 for(i
= 1; i
< nb_elems
; i
++) {
5394 memcpy(dst
, src
, elem_size
);
5400 #define EXPR_CONST 1
5403 /* store a value or an expression directly in global data or in local array */
5404 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
5405 int v
, int expr_type
)
5407 int saved_global_expr
, bt
, bit_pos
, bit_size
;
5409 unsigned long long bit_mask
;
5417 /* compound literals must be allocated globally in this case */
5418 saved_global_expr
= global_expr
;
5421 global_expr
= saved_global_expr
;
5422 /* NOTE: symbols are accepted */
5423 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
5424 tcc_error("initializer element is not constant");
5432 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
5435 /* XXX: not portable */
5436 /* XXX: generate error if incorrect relocation */
5437 gen_assign_cast(&dtype
);
5438 bt
= type
->t
& VT_BTYPE
;
5439 /* we'll write at most 16 bytes */
5440 if (c
+ 16 > sec
->data_allocated
) {
5441 section_realloc(sec
, c
+ 16);
5443 ptr
= sec
->data
+ c
;
5444 /* XXX: make code faster ? */
5445 if (!(type
->t
& VT_BITFIELD
)) {
5450 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5451 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
5452 bit_mask
= (1LL << bit_size
) - 1;
5454 if ((vtop
->r
& VT_SYM
) &&
5460 (bt
== VT_INT
&& bit_size
!= 32)))
5461 tcc_error("initializer element is not computable at load time");
5463 /* XXX: when cross-compiling we assume that each type has the
5464 same representation on host and target, which is likely to
5465 be wrong in the case of long double */
5467 vtop
->c
.i
= (vtop
->c
.i
!= 0);
5469 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5472 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5475 *(double *)ptr
= vtop
->c
.d
;
5478 *(long double *)ptr
= vtop
->c
.ld
;
5481 *(long long *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5484 addr_t val
= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5485 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
5486 if (vtop
->r
& VT_SYM
)
5487 greloca(sec
, vtop
->sym
, c
, R_DATA_PTR
, val
);
5489 *(addr_t
*)ptr
|= val
;
5491 if (vtop
->r
& VT_SYM
)
5492 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
5493 *(addr_t
*)ptr
|= val
;
5498 int val
= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5499 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
5500 if (vtop
->r
& VT_SYM
)
5501 greloca(sec
, vtop
->sym
, c
, R_DATA_PTR
, val
);
5505 if (vtop
->r
& VT_SYM
)
5506 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
5514 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
5521 /* put zeros for variable based init */
5522 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
5525 /* nothing to do because globals are already set to zero */
5527 vpush_global_sym(&func_old_type
, TOK_memset
);
5529 #ifdef TCC_TARGET_ARM
5540 /* 't' contains the type and storage info. 'c' is the offset of the
5541 object in section 'sec'. If 'sec' is NULL, it means stack based
5542 allocation. 'first' is true if array '{' must be read (multi
5543 dimension implicit array init handling). 'size_only' is true if
5544 size only evaluation is wanted (only for arrays). */
5545 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
5546 int first
, int size_only
)
5548 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, parlevel1
, i
;
5549 int size1
, align1
, expr_type
;
5553 if (type
->t
& VT_VLA
) {
5556 /* save current stack pointer */
5557 if (vlas_in_scope
== 0) {
5558 if (vla_sp_root_loc
== -1)
5559 vla_sp_root_loc
= (loc
-= PTR_SIZE
);
5560 gen_vla_sp_save(vla_sp_root_loc
);
5563 vla_runtime_type_size(type
, &a
);
5564 gen_vla_alloc(type
, a
);
5568 } else if (type
->t
& VT_ARRAY
) {
5572 t1
= pointed_type(type
);
5573 size1
= type_size(t1
, &align1
);
5576 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
5579 tcc_error("character array initializer must be a literal,"
5580 " optionally enclosed in braces");
5585 /* only parse strings here if correct type (otherwise: handle
5586 them as ((w)char *) expressions */
5587 if ((tok
== TOK_LSTR
&&
5588 #ifdef TCC_TARGET_PE
5589 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
5591 (t1
->t
& VT_BTYPE
) == VT_INT
5593 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
5594 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5597 /* compute maximum number of chars wanted */
5599 cstr_len
= tokc
.str
.size
;
5601 cstr_len
= tokc
.str
.size
/ sizeof(nwchar_t
);
5604 if (n
>= 0 && nb
> (n
- array_length
))
5605 nb
= n
- array_length
;
5608 tcc_warning("initializer-string for array is too long");
5609 /* in order to go faster for common case (char
5610 string in global variable, we handle it
5612 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
5613 memcpy(sec
->data
+ c
+ array_length
, tokc
.str
.data
, nb
);
5617 ch
= ((unsigned char *)tokc
.str
.data
)[i
];
5619 ch
= ((nwchar_t
*)tokc
.str
.data
)[i
];
5620 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
5628 /* only add trailing zero if enough storage (no
5629 warning in this case since it is standard) */
5630 if (n
< 0 || array_length
< n
) {
5632 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
5638 while (tok
!= '}') {
5639 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
5640 if (n
>= 0 && index
>= n
)
5641 tcc_error("index too large");
5642 /* must put zero in holes (note that doing it that way
5643 ensures that it even works with designators) */
5644 if (!size_only
&& array_length
< index
) {
5645 init_putz(t1
, sec
, c
+ array_length
* size1
,
5646 (index
- array_length
) * size1
);
5649 if (index
> array_length
)
5650 array_length
= index
;
5651 /* special test for multi dimensional arrays (may not
5652 be strictly correct if designators are used at the
5654 if (index
>= n
&& no_oblock
)
5663 /* put zeros at the end */
5664 if (!size_only
&& n
>= 0 && array_length
< n
) {
5665 init_putz(t1
, sec
, c
+ array_length
* size1
,
5666 (n
- array_length
) * size1
);
5668 /* patch type size if needed */
5670 s
->c
= array_length
;
5671 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
5672 (sec
|| !first
|| tok
== '{')) {
5674 /* NOTE: the previous test is a specific case for automatic
5675 struct/union init */
5676 /* XXX: union needs only one init */
5683 if (tcc_state
->old_struct_init_code
) {
5684 /* an old version of struct initialization.
5685 It have a problems. But with a new version
5686 linux 2.4.26 can't load ramdisk.
5688 while (tok
== '(') {
5692 if (!parse_btype(&type1
, &ad1
))
5694 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
5696 if (!is_assignable_types(type
, &type1
))
5697 tcc_error("invalid type for cast");
5704 if (!parse_btype(&type1
, &ad1
))
5706 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
5708 if (!is_assignable_types(type
, &type1
))
5709 tcc_error("invalid type for cast");
5718 if (first
|| tok
== '{') {
5727 while (tok
!= '}') {
5728 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
5730 if (!size_only
&& array_length
< index
) {
5731 init_putz(type
, sec
, c
+ array_length
,
5732 index
- array_length
);
5734 index
= index
+ type_size(&f
->type
, &align1
);
5735 if (index
> array_length
)
5736 array_length
= index
;
5738 /* gr: skip fields from same union - ugly. */
5741 int f_size
= type_size(&f
->type
, &align
);
5742 int f_type
= (f
->type
.t
& VT_BTYPE
);
5744 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
5745 /* test for same offset */
5746 if (f
->next
->c
!= f
->c
)
5748 if ((f_type
== VT_STRUCT
) && (f_size
== 0)) {
5750 Lets assume a structure of size 0 can't be a member of the union.
5751 This allow to compile the following code from a linux kernel v2.4.26
5752 typedef struct { } rwlock_t;
5758 struct fs_struct init_fs = { { (1) }, (rwlock_t) {}, 0022, };
5759 tcc-0.9.23 can succesfully compile this version of the kernel.
5760 gcc don't have problems with this code too.
5764 /* if yes, test for bitfield shift */
5765 if ((f
->type
.t
& VT_BITFIELD
) && (f
->next
->type
.t
& VT_BITFIELD
)) {
5766 int bit_pos_1
= (f
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5767 int bit_pos_2
= (f
->next
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5768 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
5769 if (bit_pos_1
!= bit_pos_2
)
5776 if (no_oblock
&& f
== NULL
)
5782 /* put zeros at the end */
5783 if (!size_only
&& array_length
< n
) {
5784 init_putz(type
, sec
, c
+ array_length
,
5793 } else if (tok
== '{') {
5795 decl_initializer(type
, sec
, c
, first
, size_only
);
5797 } else if (size_only
) {
5798 /* just skip expression */
5799 parlevel
= parlevel1
= 0;
5800 while ((parlevel
> 0 || parlevel1
> 0 ||
5801 (tok
!= '}' && tok
!= ',')) && tok
!= -1) {
5804 else if (tok
== ')') {
5805 if (parlevel
== 0 && parlevel1
== 0)
5809 else if (tok
== '{')
5811 else if (tok
== '}') {
5812 if (parlevel
== 0 && parlevel1
== 0)
5819 /* currently, we always use constant expression for globals
5820 (may change for scripting case) */
5821 expr_type
= EXPR_CONST
;
5823 expr_type
= EXPR_ANY
;
5824 init_putv(type
, sec
, c
, 0, expr_type
);
5828 /* parse an initializer for type 't' if 'has_init' is non zero, and
5829 allocate space in local or global data space ('r' is either
5830 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5831 variable 'v' of scope 'scope' is declared before initializers
5832 are parsed. If 'v' is zero, then a reference to the new object
5833 is put in the value stack. If 'has_init' is 2, a special parsing
5834 is done to handle string constants. */
5835 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
5836 int has_init
, int v
, int scope
)
5838 int size
, align
, addr
, data_offset
;
5840 ParseState saved_parse_state
= {0};
5841 TokenString init_str
;
5843 Sym
*flexible_array
;
5845 flexible_array
= NULL
;
5846 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
5847 Sym
*field
= type
->ref
->next
;
5850 field
= field
->next
;
5851 if (field
->type
.t
& VT_ARRAY
&& field
->type
.ref
->c
< 0)
5852 flexible_array
= field
;
5856 size
= type_size(type
, &align
);
5857 /* If unknown size, we must evaluate it before
5858 evaluating initializers because
5859 initializers can generate global data too
5860 (e.g. string pointers or ISOC99 compound
5861 literals). It also simplifies local
5862 initializers handling */
5863 tok_str_new(&init_str
);
5864 if (size
< 0 || (flexible_array
&& has_init
)) {
5866 tcc_error("unknown type size");
5867 /* get all init string */
5868 if (has_init
== 2) {
5869 /* only get strings */
5870 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5871 tok_str_add_tok(&init_str
);
5876 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
5878 tcc_error("unexpected end of file in initializer");
5879 tok_str_add_tok(&init_str
);
5882 else if (tok
== '}') {
5892 tok_str_add(&init_str
, -1);
5893 tok_str_add(&init_str
, 0);
5896 save_parse_state(&saved_parse_state
);
5898 begin_macro(&init_str
, 0);
5900 decl_initializer(type
, NULL
, 0, 1, 1);
5901 /* prepare second initializer parsing */
5902 macro_ptr
= init_str
.str
;
5905 /* if still unknown size, error */
5906 size
= type_size(type
, &align
);
5908 tcc_error("unknown type size");
5910 /* If there's a flex member and it was used in the initializer
5912 if (flexible_array
&&
5913 flexible_array
->type
.ref
->c
> 0)
5914 size
+= flexible_array
->type
.ref
->c
5915 * pointed_size(&flexible_array
->type
);
5916 /* take into account specified alignment if bigger */
5917 if (ad
->a
.aligned
) {
5918 if (ad
->a
.aligned
> align
)
5919 align
= ad
->a
.aligned
;
5920 } else if (ad
->a
.packed
) {
5923 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
5925 #ifdef CONFIG_TCC_BCHECK
5926 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5930 loc
= (loc
- size
) & -align
;
5932 #ifdef CONFIG_TCC_BCHECK
5933 /* handles bounds */
5934 /* XXX: currently, since we do only one pass, we cannot track
5935 '&' operators, so we add only arrays */
5936 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5938 /* add padding between regions */
5940 /* then add local bound info */
5941 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(addr_t
));
5942 bounds_ptr
[0] = addr
;
5943 bounds_ptr
[1] = size
;
5947 /* local variable */
5948 sym_push(v
, type
, r
, addr
);
5950 /* push local reference */
5951 vset(type
, r
, addr
);
5957 if (v
&& scope
== VT_CONST
) {
5958 /* see if the symbol was already defined */
5961 if (!is_compatible_types(&sym
->type
, type
))
5962 tcc_error("incompatible types for redefinition of '%s'",
5963 get_tok_str(v
, NULL
));
5964 if (sym
->type
.t
& VT_EXTERN
) {
5965 /* if the variable is extern, it was not allocated */
5966 sym
->type
.t
&= ~VT_EXTERN
;
5967 /* set array size if it was omitted in extern
5969 if ((sym
->type
.t
& VT_ARRAY
) &&
5970 sym
->type
.ref
->c
< 0 &&
5972 sym
->type
.ref
->c
= type
->ref
->c
;
5974 /* we accept several definitions of the same
5975 global variable. this is tricky, because we
5976 must play with the SHN_COMMON type of the symbol */
5977 /* XXX: should check if the variable was already
5978 initialized. It is incorrect to initialized it
5980 /* no init data, we won't add more to the symbol */
5987 /* allocate symbol in corresponding section */
5992 else if (tcc_state
->nocommon
)
5996 data_offset
= sec
->data_offset
;
5997 data_offset
= (data_offset
+ align
- 1) & -align
;
5999 /* very important to increment global pointer at this time
6000 because initializers themselves can create new initializers */
6001 data_offset
+= size
;
6002 #ifdef CONFIG_TCC_BCHECK
6003 /* add padding if bound check */
6004 if (tcc_state
->do_bounds_check
)
6007 sec
->data_offset
= data_offset
;
6008 /* allocate section space to put the data */
6009 if (sec
->sh_type
!= SHT_NOBITS
&&
6010 data_offset
> sec
->data_allocated
)
6011 section_realloc(sec
, data_offset
);
6012 /* align section if needed */
6013 if (align
> sec
->sh_addralign
)
6014 sec
->sh_addralign
= align
;
6016 addr
= 0; /* avoid warning */
6020 if (scope
!= VT_CONST
|| !sym
) {
6021 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
6022 sym
->asm_label
= ad
->asm_label
;
6024 /* update symbol definition */
6026 put_extern_sym(sym
, sec
, addr
, size
);
6029 /* put a common area */
6030 put_extern_sym(sym
, NULL
, align
, size
);
6031 /* XXX: find a nicer way */
6032 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
6033 esym
->st_shndx
= SHN_COMMON
;
6036 /* push global reference */
6037 sym
= get_sym_ref(type
, sec
, addr
, size
);
6038 vpushsym(type
, sym
);
6040 /* patch symbol weakness */
6041 if (type
->t
& VT_WEAK
)
6043 apply_visibility(sym
, type
);
6044 #ifdef CONFIG_TCC_BCHECK
6045 /* handles bounds now because the symbol must be defined
6046 before for the relocation */
6047 if (tcc_state
->do_bounds_check
) {
6050 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
6051 /* then add global bound info */
6052 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(addr_t
));
6053 bounds_ptr
[0] = 0; /* relocated */
6054 bounds_ptr
[1] = size
;
6058 if (has_init
|| (type
->t
& VT_VLA
)) {
6059 decl_initializer(type
, sec
, addr
, 1, 0);
6060 /* restore parse state if needed */
6063 restore_parse_state(&saved_parse_state
);
6065 /* patch flexible array member size back to -1, */
6066 /* for possible subsequent similar declarations */
6068 flexible_array
->type
.ref
->c
= -1;
6073 static void put_func_debug(Sym
*sym
)
6078 /* XXX: we put here a dummy type */
6079 snprintf(buf
, sizeof(buf
), "%s:%c1",
6080 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
6081 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
6082 cur_text_section
, sym
->c
);
6083 /* //gr gdb wants a line at the function */
6084 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
6089 /* parse an old style function declaration list */
6090 /* XXX: check multiple parameter */
6091 static void func_decl_list(Sym
*func_sym
)
6098 /* parse each declaration */
6099 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
&&
6100 tok
!= TOK_ASM1
&& tok
!= TOK_ASM2
&& tok
!= TOK_ASM3
) {
6101 if (!parse_btype(&btype
, &ad
))
6102 expect("declaration list");
6103 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
6104 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
6106 /* we accept no variable after */
6110 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
6111 /* find parameter in function parameter list */
6114 if ((s
->v
& ~SYM_FIELD
) == v
)
6118 tcc_error("declaration for parameter '%s' but no such parameter",
6119 get_tok_str(v
, NULL
));
6121 /* check that no storage specifier except 'register' was given */
6122 if (type
.t
& VT_STORAGE
)
6123 tcc_error("storage class specified for '%s'", get_tok_str(v
, NULL
));
6124 convert_parameter_type(&type
);
6125 /* we can add the type (NOTE: it could be local to the function) */
6127 /* accept other parameters */
6138 /* parse a function defined by symbol 'sym' and generate its code in
6139 'cur_text_section' */
6140 static void gen_function(Sym
*sym
)
6142 int saved_nocode_wanted
= nocode_wanted
;
6145 ind
= cur_text_section
->data_offset
;
6146 /* NOTE: we patch the symbol size later */
6147 put_extern_sym(sym
, cur_text_section
, ind
, 0);
6148 funcname
= get_tok_str(sym
->v
, NULL
);
6150 /* Initialize VLA state */
6152 vla_sp_root_loc
= -1;
6153 /* put debug symbol */
6154 if (tcc_state
->do_debug
)
6155 put_func_debug(sym
);
6157 /* push a dummy symbol to enable local sym storage */
6158 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
6159 local_scope
= 1; /* for function parameters */
6160 gfunc_prolog(&sym
->type
);
6163 #ifdef CONFIG_TCC_BCHECK
6164 if (tcc_state
->do_bounds_check
&& !strcmp(funcname
, "main")) {
6167 for (i
= 0, sym
= local_stack
; i
< 2; i
++, sym
= sym
->prev
) {
6168 if (sym
->v
& SYM_FIELD
|| sym
->prev
->v
& SYM_FIELD
)
6170 vpush_global_sym(&func_old_type
, TOK___bound_main_arg
);
6171 vset(&sym
->type
, sym
->r
, sym
->c
);
6177 block(NULL
, NULL
, NULL
, NULL
, 0, 0);
6180 cur_text_section
->data_offset
= ind
;
6181 label_pop(&global_label_stack
, NULL
);
6182 /* reset local stack */
6184 sym_pop(&local_stack
, NULL
);
6185 /* end of function */
6186 /* patch symbol size */
6187 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
6189 /* patch symbol weakness (this definition overrules any prototype) */
6190 if (sym
->type
.t
& VT_WEAK
)
6192 apply_visibility(sym
, &sym
->type
);
6193 if (tcc_state
->do_debug
) {
6194 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
6196 /* It's better to crash than to generate wrong code */
6197 cur_text_section
= NULL
;
6198 funcname
= ""; /* for safety */
6199 func_vt
.t
= VT_VOID
; /* for safety */
6200 func_var
= 0; /* for safety */
6201 ind
= 0; /* for safety */
6202 nocode_wanted
= saved_nocode_wanted
;
6206 ST_FUNC
void gen_inline_functions(void)
6209 int inline_generated
, i
, ln
;
6210 struct InlineFunc
*fn
;
6212 ln
= file
->line_num
;
6213 /* iterate while inline function are referenced */
6215 inline_generated
= 0;
6216 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
6217 fn
= tcc_state
->inline_fns
[i
];
6219 if (sym
&& sym
->c
) {
6220 /* the function was used: generate its code and
6221 convert it to a normal function */
6224 pstrcpy(file
->filename
, sizeof file
->filename
, fn
->filename
);
6225 sym
->r
= VT_SYM
| VT_CONST
;
6226 sym
->type
.t
&= ~VT_INLINE
;
6228 begin_macro(&fn
->func_str
, 0);
6230 cur_text_section
= text_section
;
6234 inline_generated
= 1;
6237 if (!inline_generated
)
6240 file
->line_num
= ln
;
6241 /* free tokens of unused inline functions */
6242 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
6243 fn
= tcc_state
->inline_fns
[i
];
6245 tok_str_free(fn
->func_str
.str
);
6247 dynarray_reset(&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
);
6250 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
6251 static int decl0(int l
, int is_for_loop_init
)
6259 if (!parse_btype(&btype
, &ad
)) {
6260 if (is_for_loop_init
)
6262 /* skip redundant ';' */
6263 /* XXX: find more elegant solution */
6268 if (l
== VT_CONST
&&
6269 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
6270 /* global asm block */
6274 /* special test for old K&R protos without explicit int
6275 type. Only accepted when defining global data */
6276 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
6280 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
6281 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
6283 if ((btype
.t
& VT_BTYPE
) == VT_STRUCT
) {
6284 int v
= btype
.ref
->v
;
6285 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) >= SYM_FIRST_ANOM
)
6286 tcc_warning("unnamed struct/union that defines no instances");
6291 while (1) { /* iterate thru each declaration */
6293 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
6297 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
6298 printf("type = '%s'\n", buf
);
6301 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
6302 if ((type
.t
& VT_STATIC
) && (l
== VT_LOCAL
)) {
6303 tcc_error("function without file scope cannot be static");
6305 /* if old style function prototype, we accept a
6308 if (sym
->c
== FUNC_OLD
)
6309 func_decl_list(sym
);
6312 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
6313 ad
.asm_label
= asm_label_instr();
6314 /* parse one last attribute list, after asm label */
6315 parse_attribute(&ad
);
6322 #ifdef TCC_TARGET_PE
6323 if (ad
.a
.func_import
)
6324 type
.t
|= VT_IMPORT
;
6325 if (ad
.a
.func_export
)
6326 type
.t
|= VT_EXPORT
;
6328 type
.t
|= ad
.a
.visibility
<< VT_VIS_SHIFT
;
6332 tcc_error("cannot use local functions");
6333 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
6334 expect("function definition");
6336 /* reject abstract declarators in function definition */
6338 while ((sym
= sym
->next
) != NULL
)
6339 if (!(sym
->v
& ~SYM_FIELD
))
6340 expect("identifier");
6342 /* XXX: cannot do better now: convert extern line to static inline */
6343 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
6344 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
6349 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
6352 ref
= sym
->type
.ref
;
6353 if (0 == ref
->a
.func_proto
)
6354 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
6356 /* use func_call from prototype if not defined */
6357 if (ref
->a
.func_call
!= FUNC_CDECL
6358 && type
.ref
->a
.func_call
== FUNC_CDECL
)
6359 type
.ref
->a
.func_call
= ref
->a
.func_call
;
6361 /* use export from prototype */
6362 if (ref
->a
.func_export
)
6363 type
.ref
->a
.func_export
= 1;
6365 /* use static from prototype */
6366 if (sym
->type
.t
& VT_STATIC
)
6367 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
6369 /* If the definition has no visibility use the
6370 one from prototype. */
6371 if (! (type
.t
& VT_VIS_MASK
))
6372 type
.t
|= sym
->type
.t
& VT_VIS_MASK
;
6374 if (!is_compatible_types(&sym
->type
, &type
)) {
6376 tcc_error("incompatible types for redefinition of '%s'",
6377 get_tok_str(v
, NULL
));
6379 type
.ref
->a
.func_proto
= 0;
6380 /* if symbol is already defined, then put complete type */
6383 /* put function symbol */
6384 sym
= global_identifier_push(v
, type
.t
, 0);
6385 sym
->type
.ref
= type
.ref
;
6388 /* static inline functions are just recorded as a kind
6389 of macro. Their code will be emitted at the end of
6390 the compilation unit only if they are used */
6391 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
6392 (VT_INLINE
| VT_STATIC
)) {
6394 struct InlineFunc
*fn
;
6395 const char *filename
;
6397 filename
= file
? file
->filename
: "";
6398 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
6399 strcpy(fn
->filename
, filename
);
6401 tok_str_new(&fn
->func_str
);
6407 tcc_error("unexpected end of file");
6408 tok_str_add_tok(&fn
->func_str
);
6413 } else if (t
== '}') {
6415 if (block_level
== 0)
6419 tok_str_add(&fn
->func_str
, -1);
6420 tok_str_add(&fn
->func_str
, 0);
6421 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
6424 /* compute text section */
6425 cur_text_section
= ad
.section
;
6426 if (!cur_text_section
)
6427 cur_text_section
= text_section
;
6428 sym
->r
= VT_SYM
| VT_CONST
;
6433 if (btype
.t
& VT_TYPEDEF
) {
6434 /* save typedefed type */
6435 /* XXX: test storage specifiers ? */
6436 sym
= sym_push(v
, &type
, 0, 0);
6438 sym
->type
.t
|= VT_TYPEDEF
;
6441 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
6442 /* external function definition */
6443 /* specific case for func_call attribute */
6444 ad
.a
.func_proto
= 1;
6446 } else if (!(type
.t
& VT_ARRAY
)) {
6447 /* not lvalue if array */
6448 r
|= lvalue_type(type
.t
);
6450 has_init
= (tok
== '=');
6451 if (has_init
&& (type
.t
& VT_VLA
))
6452 tcc_error("Variable length array cannot be initialized");
6453 if ((btype
.t
& VT_EXTERN
) || ((type
.t
& VT_BTYPE
) == VT_FUNC
) ||
6454 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
6455 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
6456 /* external variable or function */
6457 /* NOTE: as GCC, uninitialized global static
6458 arrays of null size are considered as
6460 sym
= external_sym(v
, &type
, r
);
6461 sym
->asm_label
= ad
.asm_label
;
6463 if (ad
.alias_target
) {
6468 alias_target
= sym_find(ad
.alias_target
);
6469 if (!alias_target
|| !alias_target
->c
)
6470 tcc_error("unsupported forward __alias__ attribute");
6471 esym
= &((Elf32_Sym
*)symtab_section
->data
)[alias_target
->c
];
6472 tsec
.sh_num
= esym
->st_shndx
;
6473 put_extern_sym2(sym
, &tsec
, esym
->st_value
, esym
->st_size
, 0);
6476 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
6477 if (type
.t
& VT_STATIC
)
6483 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, l
);
6487 if (is_for_loop_init
)
6500 ST_FUNC
void decl(int l
)