2 * TCC - Tiny C Compiler
4 * Copyright (c) 2001-2004 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 /********************************************************/
24 /* global variables */
26 /* loc : local variable index
27 ind : output code index
29 anon_sym: anonymous symbol index
31 ST_DATA
int rsym
, anon_sym
, ind
, loc
;
33 ST_DATA Section
*text_section
, *data_section
, *bss_section
; /* predefined sections */
34 ST_DATA Section
*cur_text_section
; /* current section where function code is generated */
36 ST_DATA Section
*last_text_section
; /* to handle .previous asm directive */
38 #ifdef CONFIG_TCC_BCHECK
39 /* bound check related sections */
40 ST_DATA Section
*bounds_section
; /* contains global data bound description */
41 ST_DATA Section
*lbounds_section
; /* contains local data bound description */
44 ST_DATA Section
*symtab_section
, *strtab_section
;
46 ST_DATA Section
*stab_section
, *stabstr_section
;
47 ST_DATA Sym
*sym_free_first
;
48 ST_DATA
void **sym_pools
;
49 ST_DATA
int nb_sym_pools
;
51 ST_DATA Sym
*global_stack
;
52 ST_DATA Sym
*local_stack
;
53 ST_DATA Sym
*scope_stack_bottom
;
54 ST_DATA Sym
*define_stack
;
55 ST_DATA Sym
*global_label_stack
;
56 ST_DATA Sym
*local_label_stack
;
58 ST_DATA
int vla_sp_loc_tmp
; /* vla_sp_loc is set to this when the value won't be needed later */
59 ST_DATA
int vla_sp_root_loc
; /* vla_sp_loc for SP before any VLAs were pushed */
60 ST_DATA
int *vla_sp_loc
; /* Pointer to variable holding location to store stack pointer on the stack when modifying stack pointer */
61 ST_DATA
int vla_flags
; /* VLA_* flags */
63 ST_DATA SValue __vstack
[1+VSTACK_SIZE
], *vtop
;
65 ST_DATA
int const_wanted
; /* true if constant wanted */
66 ST_DATA
int nocode_wanted
; /* true if no code generation wanted for an expression */
67 ST_DATA
int global_expr
; /* true if compound literals must be allocated globally (used during initializers parsing */
68 ST_DATA CType func_vt
; /* current function return type (used by return instruction) */
69 ST_DATA
int func_var
; /* true if current function is variadic (used by return instruction) */
71 ST_DATA
int last_line_num
, last_ind
, func_ind
; /* debug last line number and pc */
72 ST_DATA
char *funcname
;
73 ST_DATA
int pop_stack
;
75 ST_DATA CType char_pointer_type
, func_old_type
, int_type
, size_type
;
77 /* ------------------------------------------------------------------------- */
78 static void gen_cast(CType
*type
);
79 static inline CType
*pointed_type(CType
*type
);
80 static int is_compatible_types(CType
*type1
, CType
*type2
);
81 static int parse_btype(CType
*type
, AttributeDef
*ad
);
82 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
);
83 static void parse_expr_type(CType
*type
);
84 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
, int first
, int size_only
);
85 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
, int case_reg
, int is_expr
);
86 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
, int has_init
, int v
, char *asm_label
, int scope
);
87 static int decl0(int l
, int is_for_loop_init
);
88 static void expr_eq(void);
89 static void unary_type(CType
*type
);
90 static void vla_runtime_type_size(CType
*type
, int *a
);
91 static void vla_sp_save(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 gtst(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 /* ------------------------------------------------------------------------- */
123 /* symbol allocator */
124 static Sym
*__sym_malloc(void)
126 Sym
*sym_pool
, *sym
, *last_sym
;
129 sym_pool
= tcc_malloc(SYM_POOL_NB
* sizeof(Sym
));
130 dynarray_add(&sym_pools
, &nb_sym_pools
, sym_pool
);
132 last_sym
= sym_free_first
;
134 for(i
= 0; i
< SYM_POOL_NB
; i
++) {
135 sym
->next
= last_sym
;
139 sym_free_first
= last_sym
;
143 static inline Sym
*sym_malloc(void)
146 sym
= sym_free_first
;
148 sym
= __sym_malloc();
149 sym_free_first
= sym
->next
;
153 ST_INLN
void sym_free(Sym
*sym
)
155 sym
->next
= sym_free_first
;
156 tcc_free(sym
->asm_label
);
157 sym_free_first
= sym
;
160 /* push, without hashing */
161 ST_FUNC Sym
*sym_push2(Sym
**ps
, int v
, int t
, long c
)
164 if (ps
== &local_stack
) {
165 for (s
= *ps
; s
&& s
!= scope_stack_bottom
; s
= s
->prev
)
166 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
&& s
->v
== v
)
167 tcc_error("incompatible types for redefinition of '%s'",
168 get_tok_str(v
, NULL
));
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
;
246 /* push a global identifier */
247 ST_FUNC Sym
*global_identifier_push(int v
, int t
, int c
)
250 s
= sym_push2(&global_stack
, v
, t
, c
);
251 /* don't record anonymous symbol */
252 if (v
< SYM_FIRST_ANOM
) {
253 ps
= &table_ident
[v
- TOK_IDENT
]->sym_identifier
;
254 /* modify the top most local identifier, so that
255 sym_identifier will point to 's' when popped */
257 ps
= &(*ps
)->prev_tok
;
264 /* pop symbols until top reaches 'b' */
265 ST_FUNC
void sym_pop(Sym
**ptop
, Sym
*b
)
275 /* remove symbol in token array */
277 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
278 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
280 ps
= &ts
->sym_struct
;
282 ps
= &ts
->sym_identifier
;
291 static void weaken_symbol(Sym
*sym
)
293 sym
->type
.t
|= VT_WEAK
;
298 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
299 esym_type
= ELFW(ST_TYPE
)(esym
->st_info
);
300 esym
->st_info
= ELFW(ST_INFO
)(STB_WEAK
, esym_type
);
304 static void apply_visibility(Sym
*sym
, CType
*type
)
306 int vis
= sym
->type
.t
& VT_VIS_MASK
;
307 int vis2
= type
->t
& VT_VIS_MASK
;
308 if (vis
== (STV_DEFAULT
<< VT_VIS_SHIFT
))
310 else if (vis2
== (STV_DEFAULT
<< VT_VIS_SHIFT
))
313 vis
= (vis
< vis2
) ? vis
: vis2
;
314 sym
->type
.t
&= ~VT_VIS_MASK
;
320 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
321 vis
>>= VT_VIS_SHIFT
;
322 esym
->st_other
= (esym
->st_other
& ~ELFW(ST_VISIBILITY
)(-1)) | vis
;
326 /* ------------------------------------------------------------------------- */
328 ST_FUNC
void swap(int *p
, int *q
)
336 static void vsetc(CType
*type
, int r
, CValue
*vc
)
340 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
341 tcc_error("memory full (vstack)");
342 /* cannot let cpu flags if other instruction are generated. Also
343 avoid leaving VT_JMP anywhere except on the top of the stack
344 because it would complicate the code generator. */
345 if (vtop
>= vstack
) {
346 v
= vtop
->r
& VT_VALMASK
;
347 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
357 /* push constant of type "type" with useless value */
358 ST_FUNC
void vpush(CType
*type
)
361 vsetc(type
, VT_CONST
, &cval
);
364 /* push integer constant */
365 ST_FUNC
void vpushi(int v
)
369 vsetc(&int_type
, VT_CONST
, &cval
);
372 /* push a pointer sized constant */
373 static void vpushs(addr_t v
)
377 vsetc(&size_type
, VT_CONST
, &cval
);
380 /* push arbitrary 64bit constant */
381 ST_FUNC
void vpush64(int ty
, unsigned long long v
)
388 vsetc(&ctype
, VT_CONST
, &cval
);
391 /* push long long constant */
392 static inline void vpushll(long long v
)
394 vpush64(VT_LLONG
, v
);
397 /* push a symbol value of TYPE */
398 static inline void vpushsym(CType
*type
, Sym
*sym
)
402 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
406 /* Return a static symbol pointing to a section */
407 ST_FUNC Sym
*get_sym_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
413 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
414 sym
->type
.ref
= type
->ref
;
415 sym
->r
= VT_CONST
| VT_SYM
;
416 put_extern_sym(sym
, sec
, offset
, size
);
420 /* push a reference to a section offset by adding a dummy symbol */
421 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
423 vpushsym(type
, get_sym_ref(type
, sec
, offset
, size
));
426 /* define a new external reference to a symbol 'v' of type 'u' */
427 ST_FUNC Sym
*external_global_sym(int v
, CType
*type
, int r
)
433 /* push forward reference */
434 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
435 s
->type
.ref
= type
->ref
;
436 s
->r
= r
| VT_CONST
| VT_SYM
;
441 /* define a new external reference to a symbol 'v' with alternate asm
442 name 'asm_label' of type 'u'. 'asm_label' is equal to NULL if there
443 is no alternate name (most cases) */
444 static Sym
*external_sym(int v
, CType
*type
, int r
, char *asm_label
)
450 /* push forward reference */
451 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
452 s
->asm_label
= asm_label
;
453 s
->type
.t
|= VT_EXTERN
;
454 } else if (s
->type
.ref
== func_old_type
.ref
) {
455 s
->type
.ref
= type
->ref
;
456 s
->r
= r
| VT_CONST
| VT_SYM
;
457 s
->type
.t
|= VT_EXTERN
;
458 } else if (!is_compatible_types(&s
->type
, type
)) {
459 tcc_error("incompatible types for redefinition of '%s'",
460 get_tok_str(v
, NULL
));
462 /* Merge some storage attributes. */
463 if (type
->t
& VT_WEAK
)
466 if (type
->t
& VT_VIS_MASK
)
467 apply_visibility(s
, type
);
472 /* push a reference to global symbol v */
473 ST_FUNC
void vpush_global_sym(CType
*type
, int v
)
475 vpushsym(type
, external_global_sym(v
, type
, 0));
478 ST_FUNC
void vset(CType
*type
, int r
, int v
)
483 vsetc(type
, r
, &cval
);
486 static void vseti(int r
, int v
)
494 ST_FUNC
void vswap(void)
497 /* cannot let cpu flags if other instruction are generated. Also
498 avoid leaving VT_JMP anywhere except on the top of the stack
499 because it would complicate the code generator. */
500 if (vtop
>= vstack
) {
501 int v
= vtop
->r
& VT_VALMASK
;
502 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
509 /* XXX: +2% overall speed possible with optimized memswap
511 * memswap(&vtop[0], &vtop[1], sizeof *vtop);
515 ST_FUNC
void vpushv(SValue
*v
)
517 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
518 tcc_error("memory full (vstack)");
523 static void vdup(void)
528 static int align_size(int size
)
530 #ifdef TCC_TARGET_X86_64
543 int loc_stack(int size
, int is_sub
){
545 align
= align_size(size
);
546 size
= (size
+ align
- 1) & - align
;
563 /* save r to the memory stack, and mark it as being free */
564 ST_FUNC
void save_reg(int r
)
566 int l
, saved
, size
, align
;
570 /* modify all stack values */
573 for(p
=vstack
;p
<=vtop
;p
++) {
574 if ((p
->r
& VT_VALMASK
) == r
||
575 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
576 /* must save value on stack if not already done */
578 /* NOTE: must reload 'r' because r might be equal to r2 */
579 r
= p
->r
& VT_VALMASK
;
580 /* store register in the stack */
582 if ((p
->r
& VT_LVAL
) ||
583 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
584 #ifdef TCC_TARGET_X86_64
585 type
= &char_pointer_type
;
589 size
= type_size(type
, &align
);
590 loc
= (loc
- size
) & -align
;
592 sv
.r
= VT_LOCAL
| VT_LVAL
;
595 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
596 /* x86 specific: need to pop fp register ST0 if saved */
598 o(0xd8dd); /* fstp %st(0) */
601 #ifndef TCC_TARGET_X86_64
602 /* special long long case */
603 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
611 /* mark that stack entry as being saved on the stack */
612 if (p
->r
& VT_LVAL
) {
613 /* also clear the bounded flag because the
614 relocation address of the function was stored in
616 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
618 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
626 #ifdef TCC_TARGET_ARM
627 /* find a register of class 'rc2' with at most one reference on stack.
628 * If none, call get_reg(rc) */
629 ST_FUNC
int get_reg_ex(int rc
, int rc2
)
634 for(r
=0;r
<NB_REGS
;r
++) {
635 if (reg_classes
[r
] & rc2
) {
638 for(p
= vstack
; p
<= vtop
; p
++) {
639 if ((p
->r
& VT_VALMASK
) == r
||
640 (p
->r2
& VT_VALMASK
) == r
)
651 /* find a free register of class 'rc'. If none, save one register */
652 ST_FUNC
int get_reg(int rc
)
657 /* find a free register */
658 for(r
=0;r
<NB_REGS
;r
++) {
659 if (reg_classes
[r
] & rc
) {
660 for(p
=vstack
;p
<=vtop
;p
++) {
661 if ((p
->r
& VT_VALMASK
) == r
||
662 (p
->r2
& VT_VALMASK
) == r
)
670 /* no register left : free the first one on the stack (VERY
671 IMPORTANT to start from the bottom to ensure that we don't
672 spill registers used in gen_opi()) */
673 for(p
=vstack
;p
<=vtop
;p
++) {
674 /* look at second register (if long long) */
675 r
= p
->r2
& VT_VALMASK
;
676 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
678 r
= p
->r
& VT_VALMASK
;
679 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
685 /* Should never comes here */
689 /* save registers up to (vtop - n) stack entry */
690 ST_FUNC
void save_regs(int n
)
695 for(p
= vstack
;p
<= p1
; p
++) {
696 r
= p
->r
& VT_VALMASK
;
703 /* move register 's' (of type 't') to 'r', and flush previous value of r to memory
705 static void move_reg(int r
, int s
, int t
)
719 /* get address of vtop (vtop MUST BE an lvalue) */
720 static void gaddrof(void)
722 if (vtop
->r
& VT_REF
)
725 /* tricky: if saved lvalue, then we can go back to lvalue */
726 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
727 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
732 #ifdef CONFIG_TCC_BCHECK
733 /* generate lvalue bound code */
734 static void gbound(void)
739 vtop
->r
&= ~VT_MUSTBOUND
;
740 /* if lvalue, then use checking code before dereferencing */
741 if (vtop
->r
& VT_LVAL
) {
742 /* if not VT_BOUNDED value, then make one */
743 if (!(vtop
->r
& VT_BOUNDED
)) {
744 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
745 /* must save type because we must set it to int to get pointer */
747 vtop
->type
.t
= VT_INT
;
750 gen_bounded_ptr_add();
751 vtop
->r
|= lval_type
;
754 /* then check for dereferencing */
755 gen_bounded_ptr_deref();
760 /* store vtop a register belonging to class 'rc'. lvalues are
761 converted to values. Cannot be used if cannot be converted to
762 register value (such as structures). */
763 ST_FUNC
int gv(int rc
)
765 int r
, bit_pos
, bit_size
, size
, align
, i
;
768 /* NOTE: get_reg can modify vstack[] */
769 if (vtop
->type
.t
& VT_BITFIELD
) {
772 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
773 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
774 /* remove bit field info to avoid loops */
775 vtop
->type
.t
&= ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
776 /* cast to int to propagate signedness in following ops */
777 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
782 if((vtop
->type
.t
& VT_UNSIGNED
) ||
783 (vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
784 type
.t
|= VT_UNSIGNED
;
786 /* generate shifts */
787 vpushi(bits
- (bit_pos
+ bit_size
));
789 vpushi(bits
- bit_size
);
790 /* NOTE: transformed to SHR if unsigned */
794 if (is_float(vtop
->type
.t
) &&
795 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
798 unsigned long offset
;
799 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
803 /* XXX: unify with initializers handling ? */
804 /* CPUs usually cannot use float constants, so we store them
805 generically in data segment */
806 size
= type_size(&vtop
->type
, &align
);
807 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
808 data_section
->data_offset
= offset
;
809 /* XXX: not portable yet */
810 #if defined(__i386__) || defined(__x86_64__)
811 /* Zero pad x87 tenbyte long doubles */
812 if (size
== LDOUBLE_SIZE
) {
813 vtop
->c
.tab
[2] &= 0xffff;
814 #if LDOUBLE_SIZE == 16
819 ptr
= section_ptr_add(data_section
, size
);
821 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
825 ptr
[i
] = vtop
->c
.tab
[size
-1-i
];
829 ptr
[i
] = vtop
->c
.tab
[i
];
830 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
831 vtop
->r
|= VT_LVAL
| VT_SYM
;
833 vtop
->c
.ptr_offset
= 0;
835 #ifdef CONFIG_TCC_BCHECK
836 if (vtop
->r
& VT_MUSTBOUND
)
840 r
= vtop
->r
& VT_VALMASK
;
841 rc2
= (rc
& RC_FLOAT
) ? RC_FLOAT
: RC_INT
;
844 #ifdef TCC_TARGET_X86_64
845 else if (rc
== RC_FRET
)
849 /* need to reload if:
851 - lvalue (need to dereference pointer)
852 - already a register, but not in the right class */
854 || (vtop
->r
& VT_LVAL
)
855 || !(reg_classes
[r
] & rc
)
856 #ifdef TCC_TARGET_X86_64
857 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
858 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
&& !(reg_classes
[vtop
->r2
] & rc2
))
860 || ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
865 #ifdef TCC_TARGET_X86_64
866 if (((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
)) {
867 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
869 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
870 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
871 unsigned long long ll
;
873 int r2
, original_type
;
874 original_type
= vtop
->type
.t
;
875 /* two register type load : expand to two words
877 #ifndef TCC_TARGET_X86_64
878 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
881 vtop
->c
.ui
= ll
; /* first word */
883 vtop
->r
= r
; /* save register value */
884 vpushi(ll
>> 32); /* second word */
887 if (r
>= VT_CONST
|| /* XXX: test to VT_CONST incorrect ? */
888 (vtop
->r
& VT_LVAL
)) {
889 /* We do not want to modifier the long long
890 pointer here, so the safest (and less
891 efficient) is to save all the other registers
892 in the stack. XXX: totally inefficient. */
894 /* load from memory */
895 vtop
->type
.t
= load_type
;
898 vtop
[-1].r
= r
; /* save register value */
899 /* increment pointer to get second word */
900 vtop
->type
.t
= addr_type
;
905 vtop
->type
.t
= load_type
;
910 vtop
[-1].r
= r
; /* save register value */
911 vtop
->r
= vtop
[-1].r2
;
913 /* Allocate second register. Here we rely on the fact that
914 get_reg() tries first to free r2 of an SValue. */
918 /* write second register */
920 vtop
->type
.t
= original_type
;
921 } else if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
923 /* lvalue of scalar type : need to use lvalue type
924 because of possible cast */
927 /* compute memory access type */
928 if (vtop
->r
& VT_REF
)
929 #ifdef TCC_TARGET_X86_64
934 else if (vtop
->r
& VT_LVAL_BYTE
)
936 else if (vtop
->r
& VT_LVAL_SHORT
)
938 if (vtop
->r
& VT_LVAL_UNSIGNED
)
942 /* restore wanted type */
945 /* one register type load */
949 vtop
->c
.ptr_offset
= 0;
951 #ifdef TCC_TARGET_C67
952 /* uses register pairs for doubles */
953 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
960 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
961 ST_FUNC
void gv2(int rc1
, int rc2
)
963 /* generate more generic register first. But VT_JMP or VT_CMP
964 values must be generated first in all cases to avoid possible
971 /* test if reload is needed for first register */
972 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
982 /* test if reload is needed for first register */
983 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
989 /* wrapper around RC_FRET to return a register by type */
990 static int rc_fret(int t
)
992 #ifdef TCC_TARGET_X86_64
993 if (t
== VT_LDOUBLE
) {
1000 /* wrapper around REG_FRET to return a register by type */
1001 static int reg_fret(int t
)
1003 #ifdef TCC_TARGET_X86_64
1004 if (t
== VT_LDOUBLE
) {
1011 /* expand long long on stack in two int registers */
1012 static void lexpand(void)
1016 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
1019 vtop
[0].r
= vtop
[-1].r2
;
1020 vtop
[0].r2
= VT_CONST
;
1021 vtop
[-1].r2
= VT_CONST
;
1022 vtop
[0].type
.t
= VT_INT
| u
;
1023 vtop
[-1].type
.t
= VT_INT
| u
;
1026 #ifdef TCC_TARGET_ARM
1027 /* expand long long on stack */
1028 ST_FUNC
void lexpand_nr(void)
1032 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
1034 vtop
->r2
= VT_CONST
;
1035 vtop
->type
.t
= VT_INT
| u
;
1036 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
1037 if (v
== VT_CONST
) {
1038 vtop
[-1].c
.ui
= vtop
->c
.ull
;
1039 vtop
->c
.ui
= vtop
->c
.ull
>> 32;
1041 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
1043 vtop
->r
= vtop
[-1].r
;
1044 } else if (v
> VT_CONST
) {
1048 vtop
->r
= vtop
[-1].r2
;
1049 vtop
[-1].r2
= VT_CONST
;
1050 vtop
[-1].type
.t
= VT_INT
| u
;
1054 #ifndef TCC_TARGET_X86_64
1055 /* build a long long from two ints */
1056 static void lbuild(int t
)
1058 gv2(RC_INT
, RC_INT
);
1059 vtop
[-1].r2
= vtop
[0].r
;
1060 vtop
[-1].type
.t
= t
;
1065 /* rotate n first stack elements to the bottom
1066 I1 ... In -> I2 ... In I1 [top is right]
1068 ST_FUNC
void vrotb(int n
)
1074 for(i
=-n
+1;i
!=0;i
++)
1075 vtop
[i
] = vtop
[i
+1];
1079 /* rotate the n elements before entry e towards the top
1080 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
1082 ST_FUNC
void vrote(SValue
*e
, int n
)
1088 for(i
= 0;i
< n
- 1; i
++)
1093 /* rotate n first stack elements to the top
1094 I1 ... In -> In I1 ... I(n-1) [top is right]
1096 ST_FUNC
void vrott(int n
)
1101 /* pop stack value */
1102 ST_FUNC
void vpop(void)
1105 v
= vtop
->r
& VT_VALMASK
;
1106 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1107 /* for x86, we need to pop the FP stack */
1108 if (v
== TREG_ST0
&& !nocode_wanted
) {
1109 o(0xd8dd); /* fstp %st(0) */
1112 if (v
== VT_JMP
|| v
== VT_JMPI
) {
1113 /* need to put correct jump if && or || without test */
1119 /* convert stack entry to register and duplicate its value in another
1121 static void gv_dup(void)
1126 #ifndef TCC_TARGET_X86_64
1127 if ((t
& VT_BTYPE
) == VT_LLONG
) {
1134 /* stack: H L L1 H1 */
1143 /* duplicate value */
1146 #ifdef TCC_TARGET_X86_64
1147 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1158 load(r1
, &sv
); /* move r to r1 */
1160 /* duplicates value */
1165 #ifndef TCC_TARGET_X86_64
1166 /* generate CPU independent (unsigned) long long operations */
1167 static void gen_opl(int op
)
1169 int t
, a
, b
, op1
, c
, i
;
1171 unsigned short reg_iret
= REG_IRET
;
1172 unsigned short reg_lret
= REG_LRET
;
1178 func
= TOK___divdi3
;
1181 func
= TOK___udivdi3
;
1184 func
= TOK___moddi3
;
1187 func
= TOK___umoddi3
;
1194 /* call generic long long function */
1195 vpush_global_sym(&func_old_type
, func
);
1200 vtop
->r2
= reg_lret
;
1213 /* stack: L1 H1 L2 H2 */
1218 vtop
[-2] = vtop
[-3];
1221 /* stack: H1 H2 L1 L2 */
1227 /* stack: H1 H2 L1 L2 ML MH */
1230 /* stack: ML MH H1 H2 L1 L2 */
1234 /* stack: ML MH H1 L2 H2 L1 */
1239 /* stack: ML MH M1 M2 */
1242 } else if (op
== '+' || op
== '-') {
1243 /* XXX: add non carry method too (for MIPS or alpha) */
1249 /* stack: H1 H2 (L1 op L2) */
1252 gen_op(op1
+ 1); /* TOK_xxxC2 */
1255 /* stack: H1 H2 (L1 op L2) */
1258 /* stack: (L1 op L2) H1 H2 */
1260 /* stack: (L1 op L2) (H1 op H2) */
1268 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1269 t
= vtop
[-1].type
.t
;
1273 /* stack: L H shift */
1275 /* constant: simpler */
1276 /* NOTE: all comments are for SHL. the other cases are
1277 done by swaping words */
1288 if (op
!= TOK_SAR
) {
1321 /* XXX: should provide a faster fallback on x86 ? */
1324 func
= TOK___ashrdi3
;
1327 func
= TOK___lshrdi3
;
1330 func
= TOK___ashldi3
;
1336 /* compare operations */
1342 /* stack: L1 H1 L2 H2 */
1344 vtop
[-1] = vtop
[-2];
1346 /* stack: L1 L2 H1 H2 */
1349 /* when values are equal, we need to compare low words. since
1350 the jump is inverted, we invert the test too. */
1353 else if (op1
== TOK_GT
)
1355 else if (op1
== TOK_ULT
)
1357 else if (op1
== TOK_UGT
)
1362 if (op1
!= TOK_NE
) {
1366 /* generate non equal test */
1367 /* XXX: NOT PORTABLE yet */
1371 #if defined(TCC_TARGET_I386)
1372 b
= psym(0x850f, 0);
1373 #elif defined(TCC_TARGET_ARM)
1375 o(0x1A000000 | encbranch(ind
, 0, 1));
1376 #elif defined(TCC_TARGET_C67)
1377 tcc_error("not implemented");
1379 #error not supported
1383 /* compare low. Always unsigned */
1387 else if (op1
== TOK_LE
)
1389 else if (op1
== TOK_GT
)
1391 else if (op1
== TOK_GE
)
1402 /* handle integer constant optimizations and various machine
1404 static void gen_opic(int op
)
1406 int c1
, c2
, t1
, t2
, n
;
1409 typedef unsigned long long U
;
1413 t1
= v1
->type
.t
& VT_BTYPE
;
1414 t2
= v2
->type
.t
& VT_BTYPE
;
1418 else if (v1
->type
.t
& VT_UNSIGNED
)
1425 else if (v2
->type
.t
& VT_UNSIGNED
)
1430 /* currently, we cannot do computations with forward symbols */
1431 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1432 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1435 case '+': l1
+= l2
; break;
1436 case '-': l1
-= l2
; break;
1437 case '&': l1
&= l2
; break;
1438 case '^': l1
^= l2
; break;
1439 case '|': l1
|= l2
; break;
1440 case '*': l1
*= l2
; break;
1447 /* if division by zero, generate explicit division */
1450 tcc_error("division by zero in constant");
1454 default: l1
/= l2
; break;
1455 case '%': l1
%= l2
; break;
1456 case TOK_UDIV
: l1
= (U
)l1
/ l2
; break;
1457 case TOK_UMOD
: l1
= (U
)l1
% l2
; break;
1460 case TOK_SHL
: l1
<<= l2
; break;
1461 case TOK_SHR
: l1
= (U
)l1
>> l2
; break;
1462 case TOK_SAR
: l1
>>= l2
; break;
1464 case TOK_ULT
: l1
= (U
)l1
< (U
)l2
; break;
1465 case TOK_UGE
: l1
= (U
)l1
>= (U
)l2
; break;
1466 case TOK_EQ
: l1
= l1
== l2
; break;
1467 case TOK_NE
: l1
= l1
!= l2
; break;
1468 case TOK_ULE
: l1
= (U
)l1
<= (U
)l2
; break;
1469 case TOK_UGT
: l1
= (U
)l1
> (U
)l2
; break;
1470 case TOK_LT
: l1
= l1
< l2
; break;
1471 case TOK_GE
: l1
= l1
>= l2
; break;
1472 case TOK_LE
: l1
= l1
<= l2
; break;
1473 case TOK_GT
: l1
= l1
> l2
; break;
1475 case TOK_LAND
: l1
= l1
&& l2
; break;
1476 case TOK_LOR
: l1
= l1
|| l2
; break;
1483 /* if commutative ops, put c2 as constant */
1484 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1485 op
== '|' || op
== '*')) {
1487 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1488 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1490 /* Filter out NOP operations like x*1, x-0, x&-1... */
1491 if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1494 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1495 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1501 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1502 /* try to use shifts instead of muls or divs */
1503 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1512 else if (op
== TOK_PDIV
)
1518 } else if (c2
&& (op
== '+' || op
== '-') &&
1519 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
1520 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1521 /* symbol + constant case */
1528 if (!nocode_wanted
) {
1529 /* call low level op generator */
1530 if (t1
== VT_LLONG
|| t2
== VT_LLONG
)
1541 /* generate a floating point operation with constant propagation */
1542 static void gen_opif(int op
)
1550 /* currently, we cannot do computations with forward symbols */
1551 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1552 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1554 if (v1
->type
.t
== VT_FLOAT
) {
1557 } else if (v1
->type
.t
== VT_DOUBLE
) {
1565 /* NOTE: we only do constant propagation if finite number (not
1566 NaN or infinity) (ANSI spec) */
1567 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1571 case '+': f1
+= f2
; break;
1572 case '-': f1
-= f2
; break;
1573 case '*': f1
*= f2
; break;
1577 tcc_error("division by zero in constant");
1582 /* XXX: also handles tests ? */
1586 /* XXX: overflow test ? */
1587 if (v1
->type
.t
== VT_FLOAT
) {
1589 } else if (v1
->type
.t
== VT_DOUBLE
) {
1597 if (!nocode_wanted
) {
1605 static int pointed_size(CType
*type
)
1608 return type_size(pointed_type(type
), &align
);
1611 static void vla_runtime_pointed_size(CType
*type
)
1614 vla_runtime_type_size(pointed_type(type
), &align
);
1617 static inline int is_null_pointer(SValue
*p
)
1619 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1621 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& p
->c
.i
== 0) ||
1622 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.ll
== 0) ||
1623 ((p
->type
.t
& VT_BTYPE
) == VT_PTR
&& p
->c
.ptr_offset
== 0);
1626 static inline int is_integer_btype(int bt
)
1628 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
1629 bt
== VT_INT
|| bt
== VT_LLONG
);
1632 /* check types for comparison or subtraction of pointers */
1633 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
1635 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
1638 /* null pointers are accepted for all comparisons as gcc */
1639 if (is_null_pointer(p1
) || is_null_pointer(p2
))
1643 bt1
= type1
->t
& VT_BTYPE
;
1644 bt2
= type2
->t
& VT_BTYPE
;
1645 /* accept comparison between pointer and integer with a warning */
1646 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
1647 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
1648 tcc_warning("comparison between pointer and integer");
1652 /* both must be pointers or implicit function pointers */
1653 if (bt1
== VT_PTR
) {
1654 type1
= pointed_type(type1
);
1655 } else if (bt1
!= VT_FUNC
)
1656 goto invalid_operands
;
1658 if (bt2
== VT_PTR
) {
1659 type2
= pointed_type(type2
);
1660 } else if (bt2
!= VT_FUNC
) {
1662 tcc_error("invalid operands to binary %s", get_tok_str(op
, NULL
));
1664 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1665 (type2
->t
& VT_BTYPE
) == VT_VOID
)
1669 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1670 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1671 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1672 /* gcc-like error if '-' is used */
1674 goto invalid_operands
;
1676 tcc_warning("comparison of distinct pointer types lacks a cast");
1680 /* generic gen_op: handles types problems */
1681 ST_FUNC
void gen_op(int op
)
1683 int u
, t1
, t2
, bt1
, bt2
, t
;
1686 t1
= vtop
[-1].type
.t
;
1687 t2
= vtop
[0].type
.t
;
1688 bt1
= t1
& VT_BTYPE
;
1689 bt2
= t2
& VT_BTYPE
;
1691 if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
1692 /* at least one operand is a pointer */
1693 /* relationnal op: must be both pointers */
1694 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
1695 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1696 /* pointers are handled are unsigned */
1697 #ifdef TCC_TARGET_X86_64
1698 t
= VT_LLONG
| VT_UNSIGNED
;
1700 t
= VT_INT
| VT_UNSIGNED
;
1704 /* if both pointers, then it must be the '-' op */
1705 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
1707 tcc_error("cannot use pointers here");
1708 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1709 /* XXX: check that types are compatible */
1710 if (vtop
[-1].type
.t
& VT_VLA
) {
1711 vla_runtime_pointed_size(&vtop
[-1].type
);
1713 vpushi(pointed_size(&vtop
[-1].type
));
1717 /* set to integer type */
1718 #ifdef TCC_TARGET_X86_64
1719 vtop
->type
.t
= VT_LLONG
;
1721 vtop
->type
.t
= VT_INT
;
1726 /* exactly one pointer : must be '+' or '-'. */
1727 if (op
!= '-' && op
!= '+')
1728 tcc_error("cannot use pointers here");
1729 /* Put pointer as first operand */
1730 if (bt2
== VT_PTR
) {
1734 type1
= vtop
[-1].type
;
1735 type1
.t
&= ~VT_ARRAY
;
1736 if (vtop
[-1].type
.t
& VT_VLA
)
1737 vla_runtime_pointed_size(&vtop
[-1].type
);
1739 u
= pointed_size(&vtop
[-1].type
);
1741 tcc_error("unknown array element size");
1742 #ifdef TCC_TARGET_X86_64
1745 /* XXX: cast to int ? (long long case) */
1750 #ifdef CONFIG_TCC_BCHECK
1751 /* if evaluating constant expression, no code should be
1752 generated, so no bound check */
1753 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
1754 /* if bounded pointers, we generate a special code to
1761 gen_bounded_ptr_add();
1767 /* put again type if gen_opic() swaped operands */
1770 } else if (is_float(bt1
) || is_float(bt2
)) {
1771 /* compute bigger type and do implicit casts */
1772 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
1774 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
1779 /* floats can only be used for a few operations */
1780 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
1781 (op
< TOK_ULT
|| op
> TOK_GT
))
1782 tcc_error("invalid operands for binary operation");
1784 } else if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
) {
1785 t
= bt1
== VT_LLONG
? VT_LLONG
: VT_INT
;
1786 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (t
| VT_UNSIGNED
))
1789 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
1790 /* cast to biggest op */
1792 /* convert to unsigned if it does not fit in a long long */
1793 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
1794 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
1797 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
1798 tcc_error("comparison of struct");
1800 /* integer operations */
1802 /* convert to unsigned if it does not fit in an integer */
1803 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
1804 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
1807 /* XXX: currently, some unsigned operations are explicit, so
1808 we modify them here */
1809 if (t
& VT_UNSIGNED
) {
1816 else if (op
== TOK_LT
)
1818 else if (op
== TOK_GT
)
1820 else if (op
== TOK_LE
)
1822 else if (op
== TOK_GE
)
1829 /* special case for shifts and long long: we keep the shift as
1831 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
1838 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
1839 /* relationnal op: the result is an int */
1840 vtop
->type
.t
= VT_INT
;
1847 #ifndef TCC_TARGET_ARM
1848 /* generic itof for unsigned long long case */
1849 static void gen_cvt_itof1(int t
)
1851 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
1852 (VT_LLONG
| VT_UNSIGNED
)) {
1855 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
1856 #if LDOUBLE_SIZE != 8
1857 else if (t
== VT_LDOUBLE
)
1858 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
1861 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
1865 vtop
->r
= reg_fret(t
);
1872 /* generic ftoi for unsigned long long case */
1873 static void gen_cvt_ftoi1(int t
)
1877 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
1878 /* not handled natively */
1879 st
= vtop
->type
.t
& VT_BTYPE
;
1881 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
1882 #if LDOUBLE_SIZE != 8
1883 else if (st
== VT_LDOUBLE
)
1884 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
1887 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
1892 vtop
->r2
= REG_LRET
;
1898 /* force char or short cast */
1899 static void force_charshort_cast(int t
)
1903 /* XXX: add optimization if lvalue : just change type and offset */
1908 if (t
& VT_UNSIGNED
) {
1909 vpushi((1 << bits
) - 1);
1915 /* result must be signed or the SAR is converted to an SHL
1916 This was not the case when "t" was a signed short
1917 and the last value on the stack was an unsigned int */
1918 vtop
->type
.t
&= ~VT_UNSIGNED
;
1924 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1925 static void gen_cast(CType
*type
)
1927 int sbt
, dbt
, sf
, df
, c
, p
;
1929 /* special delayed cast for char/short */
1930 /* XXX: in some cases (multiple cascaded casts), it may still
1932 if (vtop
->r
& VT_MUSTCAST
) {
1933 vtop
->r
&= ~VT_MUSTCAST
;
1934 force_charshort_cast(vtop
->type
.t
);
1937 /* bitfields first get cast to ints */
1938 if (vtop
->type
.t
& VT_BITFIELD
) {
1942 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
1943 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
1948 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1949 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
1951 /* constant case: we can do it now */
1952 /* XXX: in ISOC, cannot do it if error in convert */
1953 if (sbt
== VT_FLOAT
)
1954 vtop
->c
.ld
= vtop
->c
.f
;
1955 else if (sbt
== VT_DOUBLE
)
1956 vtop
->c
.ld
= vtop
->c
.d
;
1959 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
1960 if (sbt
& VT_UNSIGNED
)
1961 vtop
->c
.ld
= vtop
->c
.ull
;
1963 vtop
->c
.ld
= vtop
->c
.ll
;
1965 if (sbt
& VT_UNSIGNED
)
1966 vtop
->c
.ld
= vtop
->c
.ui
;
1968 vtop
->c
.ld
= vtop
->c
.i
;
1971 if (dbt
== VT_FLOAT
)
1972 vtop
->c
.f
= (float)vtop
->c
.ld
;
1973 else if (dbt
== VT_DOUBLE
)
1974 vtop
->c
.d
= (double)vtop
->c
.ld
;
1975 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
1976 vtop
->c
.ull
= (unsigned long long)vtop
->c
.ld
;
1977 } else if (sf
&& dbt
== VT_BOOL
) {
1978 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
1981 vtop
->c
.ll
= (long long)vtop
->c
.ld
;
1982 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
1983 vtop
->c
.ll
= vtop
->c
.ull
;
1984 else if (sbt
& VT_UNSIGNED
)
1985 vtop
->c
.ll
= vtop
->c
.ui
;
1986 #ifdef TCC_TARGET_X86_64
1987 else if (sbt
== VT_PTR
)
1990 else if (sbt
!= VT_LLONG
)
1991 vtop
->c
.ll
= vtop
->c
.i
;
1993 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
1994 vtop
->c
.ull
= vtop
->c
.ll
;
1995 else if (dbt
== VT_BOOL
)
1996 vtop
->c
.i
= (vtop
->c
.ll
!= 0);
1997 #ifdef TCC_TARGET_X86_64
1998 else if (dbt
== VT_PTR
)
2001 else if (dbt
!= VT_LLONG
) {
2003 if ((dbt
& VT_BTYPE
) == VT_BYTE
)
2005 else if ((dbt
& VT_BTYPE
) == VT_SHORT
)
2007 if(dbt
& VT_UNSIGNED
)
2008 vtop
->c
.ui
= ((unsigned int)vtop
->c
.ll
<< s
) >> s
;
2010 vtop
->c
.i
= ((int)vtop
->c
.ll
<< s
) >> s
;
2013 } else if (p
&& dbt
== VT_BOOL
) {
2016 } else if (!nocode_wanted
) {
2017 /* non constant case: generate code */
2019 /* convert from fp to fp */
2022 /* convert int to fp */
2025 /* convert fp to int */
2026 if (dbt
== VT_BOOL
) {
2030 /* we handle char/short/etc... with generic code */
2031 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
2032 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
2036 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
2037 /* additional cast for char/short... */
2042 #ifndef TCC_TARGET_X86_64
2043 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
2044 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
2045 /* scalar to long long */
2046 /* machine independent conversion */
2048 /* generate high word */
2049 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
2053 if (sbt
== VT_PTR
) {
2054 /* cast from pointer to int before we apply
2055 shift operation, which pointers don't support*/
2056 gen_cast(&int_type
);
2062 /* patch second register */
2063 vtop
[-1].r2
= vtop
->r
;
2067 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
2068 (dbt
& VT_BTYPE
) == VT_PTR
||
2069 (dbt
& VT_BTYPE
) == VT_FUNC
) {
2070 if ((sbt
& VT_BTYPE
) != VT_LLONG
&&
2071 (sbt
& VT_BTYPE
) != VT_PTR
&&
2072 (sbt
& VT_BTYPE
) != VT_FUNC
) {
2073 /* need to convert from 32bit to 64bit */
2075 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
2076 /* x86_64 specific: movslq */
2078 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
2082 } else if (dbt
== VT_BOOL
) {
2083 /* scalar to bool */
2086 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
2087 (dbt
& VT_BTYPE
) == VT_SHORT
) {
2088 if (sbt
== VT_PTR
) {
2089 vtop
->type
.t
= VT_INT
;
2090 tcc_warning("nonportable conversion from pointer to char/short");
2092 force_charshort_cast(dbt
);
2093 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
2095 if (sbt
== VT_LLONG
) {
2096 /* from long long: just take low order word */
2100 /* if lvalue and single word type, nothing to do because
2101 the lvalue already contains the real type size (see
2102 VT_LVAL_xxx constants) */
2105 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
2106 /* if we are casting between pointer types,
2107 we must update the VT_LVAL_xxx size */
2108 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
2109 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
2114 /* return type size as known at compile time. Put alignment at 'a' */
2115 ST_FUNC
int type_size(CType
*type
, int *a
)
2120 bt
= type
->t
& VT_BTYPE
;
2121 if (bt
== VT_STRUCT
) {
2126 } else if (bt
== VT_PTR
) {
2127 if (type
->t
& VT_ARRAY
) {
2131 ts
= type_size(&s
->type
, a
);
2133 if (ts
< 0 && s
->c
< 0)
2141 } else if (bt
== VT_LDOUBLE
) {
2143 return LDOUBLE_SIZE
;
2144 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
2145 #ifdef TCC_TARGET_I386
2146 #ifdef TCC_TARGET_PE
2151 #elif defined(TCC_TARGET_ARM)
2161 } else if (bt
== VT_INT
|| bt
== VT_ENUM
|| bt
== VT_FLOAT
) {
2164 } else if (bt
== VT_SHORT
) {
2167 } else if (bt
== VT_QLONG
|| bt
== VT_QFLOAT
) {
2171 /* char, void, function, _Bool */
2177 /* push type size as known at runtime time on top of value stack. Put
2179 ST_FUNC
void vla_runtime_type_size(CType
*type
, int *a
)
2181 if (type
->t
& VT_VLA
) {
2182 vset(&int_type
, VT_LOCAL
|VT_LVAL
, type
->ref
->c
);
2184 vpushi(type_size(type
, a
));
2188 static void vla_sp_save(void) {
2189 if (!(vla_flags
& VLA_SP_LOC_SET
)) {
2190 *vla_sp_loc
= (loc
-= PTR_SIZE
);
2191 vla_flags
|= VLA_SP_LOC_SET
;
2193 if (!(vla_flags
& VLA_SP_SAVED
)) {
2194 gen_vla_sp_save(*vla_sp_loc
);
2195 vla_flags
|= VLA_SP_SAVED
;
2199 /* return the pointed type of t */
2200 static inline CType
*pointed_type(CType
*type
)
2202 return &type
->ref
->type
;
2205 /* modify type so that its it is a pointer to type. */
2206 ST_FUNC
void mk_pointer(CType
*type
)
2209 s
= sym_push(SYM_FIELD
, type
, 0, -1);
2210 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
2214 /* compare function types. OLD functions match any new functions */
2215 static int is_compatible_func(CType
*type1
, CType
*type2
)
2221 if (!is_compatible_types(&s1
->type
, &s2
->type
))
2223 /* check func_call */
2224 if (s1
->a
.func_call
!= s2
->a
.func_call
)
2226 /* XXX: not complete */
2227 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
2231 while (s1
!= NULL
) {
2234 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
2244 /* return true if type1 and type2 are the same. If unqualified is
2245 true, qualifiers on the types are ignored.
2247 - enums are not checked as gcc __builtin_types_compatible_p ()
2249 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2253 t1
= type1
->t
& VT_TYPE
;
2254 t2
= type2
->t
& VT_TYPE
;
2256 /* strip qualifiers before comparing */
2257 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2258 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2260 /* Default Vs explicit signedness only matters for char */
2261 if ((t1
& VT_BTYPE
) != VT_BYTE
) {
2265 /* XXX: bitfields ? */
2268 /* test more complicated cases */
2269 bt1
= t1
& VT_BTYPE
;
2270 if (bt1
== VT_PTR
) {
2271 type1
= pointed_type(type1
);
2272 type2
= pointed_type(type2
);
2273 return is_compatible_types(type1
, type2
);
2274 } else if (bt1
== VT_STRUCT
) {
2275 return (type1
->ref
== type2
->ref
);
2276 } else if (bt1
== VT_FUNC
) {
2277 return is_compatible_func(type1
, type2
);
2283 /* return true if type1 and type2 are exactly the same (including
2286 static int is_compatible_types(CType
*type1
, CType
*type2
)
2288 return compare_types(type1
,type2
,0);
2291 /* return true if type1 and type2 are the same (ignoring qualifiers).
2293 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
2295 return compare_types(type1
,type2
,1);
2298 /* print a type. If 'varstr' is not NULL, then the variable is also
2299 printed in the type */
2301 /* XXX: add array and function pointers */
2302 static void type_to_str(char *buf
, int buf_size
,
2303 CType
*type
, const char *varstr
)
2310 t
= type
->t
& VT_TYPE
;
2313 if (t
& VT_CONSTANT
)
2314 pstrcat(buf
, buf_size
, "const ");
2315 if (t
& VT_VOLATILE
)
2316 pstrcat(buf
, buf_size
, "volatile ");
2317 if ((t
& (VT_DEFSIGN
| VT_UNSIGNED
)) == (VT_DEFSIGN
| VT_UNSIGNED
))
2318 pstrcat(buf
, buf_size
, "unsigned ");
2319 else if (t
& VT_DEFSIGN
)
2320 pstrcat(buf
, buf_size
, "signed ");
2350 tstr
= "long double";
2352 pstrcat(buf
, buf_size
, tstr
);
2356 if (bt
== VT_STRUCT
)
2360 pstrcat(buf
, buf_size
, tstr
);
2361 v
= type
->ref
->v
& ~SYM_STRUCT
;
2362 if (v
>= SYM_FIRST_ANOM
)
2363 pstrcat(buf
, buf_size
, "<anonymous>");
2365 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
2369 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
2370 pstrcat(buf
, buf_size
, "(");
2372 while (sa
!= NULL
) {
2373 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
2374 pstrcat(buf
, buf_size
, buf1
);
2377 pstrcat(buf
, buf_size
, ", ");
2379 pstrcat(buf
, buf_size
, ")");
2383 pstrcpy(buf1
, sizeof(buf1
), "*");
2385 pstrcat(buf1
, sizeof(buf1
), varstr
);
2386 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2390 pstrcat(buf
, buf_size
, " ");
2391 pstrcat(buf
, buf_size
, varstr
);
2396 /* verify type compatibility to store vtop in 'dt' type, and generate
2398 static void gen_assign_cast(CType
*dt
)
2400 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
2401 char buf1
[256], buf2
[256];
2404 st
= &vtop
->type
; /* source type */
2405 dbt
= dt
->t
& VT_BTYPE
;
2406 sbt
= st
->t
& VT_BTYPE
;
2407 if (sbt
== VT_VOID
|| dbt
== VT_VOID
)
2408 tcc_error("cannot cast from/to void");
2409 if (dt
->t
& VT_CONSTANT
)
2410 tcc_warning("assignment of read-only location");
2413 /* special cases for pointers */
2414 /* '0' can also be a pointer */
2415 if (is_null_pointer(vtop
))
2417 /* accept implicit pointer to integer cast with warning */
2418 if (is_integer_btype(sbt
)) {
2419 tcc_warning("assignment makes pointer from integer without a cast");
2422 type1
= pointed_type(dt
);
2423 /* a function is implicitely a function pointer */
2424 if (sbt
== VT_FUNC
) {
2425 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
2426 !is_compatible_types(pointed_type(dt
), st
))
2427 tcc_warning("assignment from incompatible pointer type");
2432 type2
= pointed_type(st
);
2433 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2434 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
2435 /* void * can match anything */
2437 /* exact type match, except for unsigned */
2440 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
|
2442 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
|
2444 if (!is_compatible_types(&tmp_type1
, &tmp_type2
))
2445 tcc_warning("assignment from incompatible pointer type");
2447 /* check const and volatile */
2448 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
2449 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
2450 tcc_warning("assignment discards qualifiers from pointer target type");
2456 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
2457 tcc_warning("assignment makes integer from pointer without a cast");
2459 /* XXX: more tests */
2464 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2465 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2466 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2468 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
2469 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
2470 tcc_error("cannot cast '%s' to '%s'", buf1
, buf2
);
2478 static void vstore_im(){
2479 int rc
, ft
, sbt
, dbt
, t
, r
;
2480 ft
= vtop
[-1].type
.t
;
2481 sbt
= vtop
->type
.t
& VT_BTYPE
;
2482 dbt
= ft
& VT_BTYPE
;
2485 #ifdef TCC_TARGET_X86_64
2486 if (dbt
== VT_LDOUBLE
) {
2492 r
= gv(rc
); /* generate value */
2493 /* if lvalue was saved on stack, must read it */
2494 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
2496 t
= get_reg(RC_INT
);
2497 #ifdef TCC_TARGET_X86_64
2502 sv
.r
= VT_LOCAL
| VT_LVAL
;
2503 sv
.c
.ul
= vtop
[-1].c
.ul
;
2505 vtop
[-1].r
= t
| VT_LVAL
;
2508 /* two word case handling : store second register at word + 4 */
2509 #ifdef TCC_TARGET_X86_64
2510 if ((dbt
== VT_QLONG
) || (dbt
== VT_QFLOAT
))
2512 if (dbt
== VT_LLONG
)
2515 #ifdef TCC_TARGET_X86_64
2516 int load_size
= 8, load_type
= (sbt
== VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
2518 int load_size
= 4, load_type
= VT_INT
;
2520 vtop
[-1].type
.t
= load_type
;
2523 /* convert to int to increment easily */
2524 vtop
->type
= char_pointer_type
;
2530 vtop
[-1].type
.t
= load_type
;
2531 /* XXX: it works because r2 is spilled last ! */
2532 store(vtop
->r2
, vtop
- 1);
2534 vtop
[-1].type
.t
= ft
;
2540 /* store vtop in lvalue pushed on stack */
2541 ST_FUNC
void vstore(void)
2543 int sbt
, dbt
, ft
, size
, align
, bit_size
, bit_pos
, delayed_cast
;
2545 ft
= vtop
[-1].type
.t
;
2546 sbt
= vtop
->type
.t
& VT_BTYPE
;
2547 dbt
= ft
& VT_BTYPE
;
2548 if ((((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2549 (sbt
== VT_INT
&& dbt
== VT_SHORT
)) && !(vtop
->type
.t
& VT_BITFIELD
)) {
2550 /* optimize char/short casts */
2551 delayed_cast
= VT_MUSTCAST
;
2552 vtop
->type
.t
= ft
& (VT_TYPE
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
)));
2553 /* XXX: factorize */
2554 if (ft
& VT_CONSTANT
)
2555 tcc_warning("assignment of read-only location");
2558 if (!(ft
& VT_BITFIELD
))
2559 gen_assign_cast(&vtop
[-1].type
);
2562 if (sbt
== VT_STRUCT
) {
2563 /* if structure, only generate pointer */
2564 /* structure assignment : generate memcpy */
2565 /* XXX: optimize if small size */
2566 if (!nocode_wanted
) {
2567 size
= type_size(&vtop
->type
, &align
);
2571 vtop
->type
.t
= VT_PTR
;
2574 /* address of memcpy() */
2577 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2578 else if(!(align
& 3))
2579 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2582 vpush_global_sym(&func_old_type
, TOK_memcpy
);
2587 vtop
->type
.t
= VT_PTR
;
2596 /* leave source on stack */
2597 } else if (ft
& VT_BITFIELD
) {
2598 /* bitfield store handling */
2599 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2600 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2601 /* remove bit field info to avoid loops */
2602 vtop
[-1].type
.t
= ft
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
2604 /* duplicate source into other register */
2605 if((ft
& VT_BTYPE
) == VT_BOOL
) {
2606 gen_cast(&vtop
[-1].type
);
2607 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2610 /* duplicate destination */
2612 vtop
[-1] = vtop
[-2];
2614 /* duplicate destination */
2616 vtop
[-1] = vtop
[-2];
2618 /* mask and shift source */
2619 if((ft
& VT_BTYPE
) != VT_BOOL
) {
2620 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2621 vpushll((1ULL << bit_size
) - 1ULL);
2623 vpushi((1 << bit_size
) - 1);
2629 /* load destination, mask and or with source */
2631 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2632 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2634 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2641 /* pop off shifted source from "duplicate source..." above */
2645 #ifdef CONFIG_TCC_BCHECK
2646 /* bound check case */
2647 if (vtop
[-1].r
& VT_MUSTBOUND
) {
2653 if (!nocode_wanted
) {
2657 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
2658 vtop
->r
|= delayed_cast
;
2662 /* post defines POST/PRE add. c is the token ++ or -- */
2663 ST_FUNC
void inc(int post
, int c
)
2666 vdup(); /* save lvalue */
2668 gv_dup(); /* duplicate value */
2673 vpushi(c
- TOK_MID
);
2675 vstore(); /* store value */
2677 vpop(); /* if post op, return saved value */
2680 /* Parse GNUC __attribute__ extension. Currently, the following
2681 extensions are recognized:
2682 - aligned(n) : set data/function alignment.
2683 - packed : force data alignment to 1
2684 - section(x) : generate data/code in this section.
2685 - unused : currently ignored, but may be used someday.
2686 - regparm(n) : pass function parameters in registers (i386 only)
2688 static void parse_attribute(AttributeDef
*ad
)
2692 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
2696 while (tok
!= ')') {
2697 if (tok
< TOK_IDENT
)
2698 expect("attribute name");
2706 expect("section name");
2707 ad
->section
= find_section(tcc_state
, (char *)tokc
.cstr
->data
);
2715 expect("alias(\"target\")");
2716 ad
->alias_target
= /* save string as token, for later */
2717 tok_alloc((char*)tokc
.cstr
->data
, tokc
.cstr
->size
-1)->tok
;
2721 case TOK_VISIBILITY1
:
2722 case TOK_VISIBILITY2
:
2725 expect("visibility(\"default|hidden|internal|protected\")");
2726 if (!strcmp (tokc
.cstr
->data
, "default"))
2727 ad
->a
.visibility
= STV_DEFAULT
;
2728 else if (!strcmp (tokc
.cstr
->data
, "hidden"))
2729 ad
->a
.visibility
= STV_HIDDEN
;
2730 else if (!strcmp (tokc
.cstr
->data
, "internal"))
2731 ad
->a
.visibility
= STV_INTERNAL
;
2732 else if (!strcmp (tokc
.cstr
->data
, "protected"))
2733 ad
->a
.visibility
= STV_PROTECTED
;
2735 expect("visibility(\"default|hidden|internal|protected\")");
2744 if (n
<= 0 || (n
& (n
- 1)) != 0)
2745 tcc_error("alignment must be a positive power of two");
2762 /* currently, no need to handle it because tcc does not
2763 track unused objects */
2767 /* currently, no need to handle it because tcc does not
2768 track unused objects */
2773 ad
->a
.func_call
= FUNC_CDECL
;
2778 ad
->a
.func_call
= FUNC_STDCALL
;
2780 #ifdef TCC_TARGET_I386
2790 ad
->a
.func_call
= FUNC_FASTCALL1
+ n
- 1;
2796 ad
->a
.func_call
= FUNC_FASTCALLW
;
2803 ad
->a
.mode
= VT_LLONG
+ 1;
2806 ad
->a
.mode
= VT_SHORT
+ 1;
2809 ad
->a
.mode
= VT_INT
+ 1;
2812 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
2819 ad
->a
.func_export
= 1;
2822 ad
->a
.func_import
= 1;
2825 if (tcc_state
->warn_unsupported
)
2826 tcc_warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
2827 /* skip parameters */
2829 int parenthesis
= 0;
2833 else if (tok
== ')')
2836 } while (parenthesis
&& tok
!= -1);
2849 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2850 static void struct_decl(CType
*type
, int u
, int tdef
)
2852 int a
, v
, size
, align
, maxalign
, c
, offset
, flexible
;
2853 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
, prevbt
;
2854 Sym
*s
, *ss
, *ass
, **ps
;
2858 a
= tok
; /* save decl type */
2863 /* struct already defined ? return it */
2865 expect("struct/union/enum name");
2869 tcc_error("invalid type");
2871 } else if (tok
>= TOK_IDENT
&& !tdef
)
2872 tcc_error("unknown struct/union/enum");
2878 /* we put an undefined size for struct/union */
2879 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
2880 s
->r
= 0; /* default alignment is zero as gcc */
2881 /* put struct/union/enum name in type */
2889 tcc_error("struct/union/enum already defined");
2890 /* cannot be empty */
2892 /* non empty enums are not allowed */
2893 if (a
== TOK_ENUM
) {
2897 expect("identifier");
2899 if (ss
&& !local_stack
)
2900 tcc_error("redefinition of enumerator '%s'",
2901 get_tok_str(v
, NULL
));
2907 /* enum symbols have static storage */
2908 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
2909 ss
->type
.t
|= VT_STATIC
;
2914 /* NOTE: we accept a trailing comma */
2918 s
->c
= type_size(&int_type
, &align
);
2927 while (tok
!= '}') {
2928 parse_btype(&btype
, &ad
);
2931 tcc_error("flexible array member '%s' not at the end of struct",
2932 get_tok_str(v
, NULL
));
2937 type_decl(&type1
, &ad
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
2938 if (v
== 0 && (type1
.t
& VT_BTYPE
) != VT_STRUCT
)
2939 expect("identifier");
2940 if (type_size(&type1
, &align
) < 0) {
2941 if ((a
== TOK_STRUCT
) && (type1
.t
& VT_ARRAY
) && c
)
2944 tcc_error("field '%s' has incomplete type",
2945 get_tok_str(v
, NULL
));
2947 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
2948 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
2949 tcc_error("invalid type for '%s'",
2950 get_tok_str(v
, NULL
));
2954 bit_size
= expr_const();
2955 /* XXX: handle v = 0 case for messages */
2957 tcc_error("negative width in bit-field '%s'",
2958 get_tok_str(v
, NULL
));
2959 if (v
&& bit_size
== 0)
2960 tcc_error("zero width for bit-field '%s'",
2961 get_tok_str(v
, NULL
));
2963 size
= type_size(&type1
, &align
);
2965 if (align
< ad
.a
.aligned
)
2966 align
= ad
.a
.aligned
;
2967 } else if (ad
.a
.packed
) {
2969 } else if (*tcc_state
->pack_stack_ptr
) {
2970 if (align
> *tcc_state
->pack_stack_ptr
)
2971 align
= *tcc_state
->pack_stack_ptr
;
2974 if (bit_size
>= 0) {
2975 bt
= type1
.t
& VT_BTYPE
;
2982 tcc_error("bitfields must have scalar type");
2984 if (bit_size
> bsize
) {
2985 tcc_error("width of '%s' exceeds its type",
2986 get_tok_str(v
, NULL
));
2987 } else if (bit_size
== bsize
) {
2988 /* no need for bit fields */
2990 } else if (bit_size
== 0) {
2991 /* XXX: what to do if only padding in a
2993 /* zero size: means to pad */
2996 /* we do not have enough room ?
2997 did the type change?
2999 if ((bit_pos
+ bit_size
) > bsize
||
3000 bt
!= prevbt
|| a
== TOK_UNION
)
3003 /* XXX: handle LSB first */
3004 type1
.t
|= VT_BITFIELD
|
3005 (bit_pos
<< VT_STRUCT_SHIFT
) |
3006 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
3007 bit_pos
+= bit_size
;
3013 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
3014 /* add new memory data only if starting
3016 if (lbit_pos
== 0) {
3017 if (a
== TOK_STRUCT
) {
3018 c
= (c
+ align
- 1) & -align
;
3027 if (align
> maxalign
)
3031 printf("add field %s offset=%d",
3032 get_tok_str(v
, NULL
), offset
);
3033 if (type1
.t
& VT_BITFIELD
) {
3034 printf(" pos=%d size=%d",
3035 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
3036 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
3041 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
3043 while ((ass
= ass
->next
) != NULL
) {
3044 ss
= sym_push(ass
->v
, &ass
->type
, 0, offset
+ ass
->c
);
3049 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
3053 if (tok
== ';' || tok
== TOK_EOF
)
3060 /* store size and alignment */
3061 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
3067 /* return 1 if basic type is a type size (short, long, long long) */
3068 ST_FUNC
int is_btype_size(int bt
)
3070 return bt
== VT_SHORT
|| bt
== VT_LONG
|| bt
== VT_LLONG
;
3073 /* return 0 if no type declaration. otherwise, return the basic type
3076 static int parse_btype(CType
*type
, AttributeDef
*ad
)
3078 int t
, u
, bt_size
, complete
, type_found
, typespec_found
;
3082 memset(ad
, 0, sizeof(AttributeDef
));
3090 /* currently, we really ignore extension */
3101 tcc_error("too many basic types");
3103 bt_size
= is_btype_size (u
& VT_BTYPE
);
3104 if (u
== VT_INT
|| (!bt_size
&& !(t
& VT_TYPEDEF
)))
3119 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
3120 #ifndef TCC_TARGET_PE
3121 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3123 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
3124 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3138 if ((t
& VT_BTYPE
) == VT_LONG
) {
3139 #ifdef TCC_TARGET_PE
3140 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
3142 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3150 struct_decl(&type1
, VT_ENUM
, t
& VT_TYPEDEF
);
3153 type
->ref
= type1
.ref
;
3157 struct_decl(&type1
, VT_STRUCT
, t
& VT_TYPEDEF
);
3160 /* type modifiers */
3176 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == (VT_DEFSIGN
|VT_UNSIGNED
))
3177 tcc_error("signed and unsigned modifier");
3190 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == VT_DEFSIGN
)
3191 tcc_error("signed and unsigned modifier");
3192 t
|= VT_DEFSIGN
| VT_UNSIGNED
;
3217 /* GNUC attribute */
3218 case TOK_ATTRIBUTE1
:
3219 case TOK_ATTRIBUTE2
:
3220 parse_attribute(ad
);
3223 t
= (t
& ~VT_BTYPE
) | u
;
3231 parse_expr_type(&type1
);
3232 /* remove all storage modifiers except typedef */
3233 type1
.t
&= ~(VT_STORAGE
&~VT_TYPEDEF
);
3239 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
3241 t
|= (s
->type
.t
& ~VT_TYPEDEF
);
3242 type
->ref
= s
->type
.ref
;
3244 /* get attributes from typedef */
3245 if (0 == ad
->a
.aligned
)
3246 ad
->a
.aligned
= s
->a
.aligned
;
3247 if (0 == ad
->a
.func_call
)
3248 ad
->a
.func_call
= s
->a
.func_call
;
3249 ad
->a
.packed
|= s
->a
.packed
;
3258 if (tcc_state
->char_is_unsigned
) {
3259 if ((t
& (VT_DEFSIGN
|VT_BTYPE
)) == VT_BYTE
)
3263 /* long is never used as type */
3264 if ((t
& VT_BTYPE
) == VT_LONG
)
3265 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
3266 t
= (t
& ~VT_BTYPE
) | VT_INT
;
3268 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3274 /* convert a function parameter type (array to pointer and function to
3275 function pointer) */
3276 static inline void convert_parameter_type(CType
*pt
)
3278 /* remove const and volatile qualifiers (XXX: const could be used
3279 to indicate a const function parameter */
3280 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3281 /* array must be transformed to pointer according to ANSI C */
3283 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
3288 ST_FUNC
void parse_asm_str(CString
*astr
)
3291 /* read the string */
3293 expect("string constant");
3295 while (tok
== TOK_STR
) {
3296 /* XXX: add \0 handling too ? */
3297 cstr_cat(astr
, tokc
.cstr
->data
);
3300 cstr_ccat(astr
, '\0');
3303 /* Parse an asm label and return the label
3304 * Don't forget to free the CString in the caller! */
3305 static void asm_label_instr(CString
*astr
)
3308 parse_asm_str(astr
);
3311 printf("asm_alias: \"%s\"\n", (char *)astr
->data
);
3315 static void post_type(CType
*type
, AttributeDef
*ad
)
3317 int n
, l
, t1
, arg_size
, align
;
3318 Sym
**plast
, *s
, *first
;
3323 /* function declaration */
3331 /* read param name and compute offset */
3332 if (l
!= FUNC_OLD
) {
3333 if (!parse_btype(&pt
, &ad1
)) {
3335 tcc_error("invalid type");
3342 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
3344 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3345 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
3346 tcc_error("parameter declared as void");
3347 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
3352 expect("identifier");
3356 convert_parameter_type(&pt
);
3357 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
3363 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
3370 /* if no parameters, then old type prototype */
3374 /* NOTE: const is ignored in returned type as it has a special
3375 meaning in gcc / C++ */
3376 type
->t
&= ~VT_CONSTANT
;
3377 /* some ancient pre-K&R C allows a function to return an array
3378 and the array brackets to be put after the arguments, such
3379 that "int c()[]" means something like "int[] c()" */
3382 skip(']'); /* only handle simple "[]" */
3385 /* we push a anonymous symbol which will contain the function prototype */
3386 ad
->a
.func_args
= arg_size
;
3387 s
= sym_push(SYM_FIELD
, type
, 0, l
);
3392 } else if (tok
== '[') {
3393 /* array definition */
3395 if (tok
== TOK_RESTRICT1
)
3400 if (!local_stack
|| nocode_wanted
)
3401 vpushi(expr_const());
3403 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3406 tcc_error("invalid array size");
3408 if (!is_integer_btype(vtop
->type
.t
& VT_BTYPE
))
3409 tcc_error("size of variable length array should be an integer");
3414 /* parse next post type */
3415 post_type(type
, ad
);
3416 if (type
->t
== VT_FUNC
)
3417 tcc_error("declaration of an array of functions");
3418 t1
|= type
->t
& VT_VLA
;
3421 loc
-= type_size(&int_type
, &align
);
3425 vla_runtime_type_size(type
, &align
);
3427 vset(&int_type
, VT_LOCAL
|VT_LVAL
, loc
);
3434 /* we push an anonymous symbol which will contain the array
3436 s
= sym_push(SYM_FIELD
, type
, 0, n
);
3437 type
->t
= (t1
? VT_VLA
: VT_ARRAY
) | VT_PTR
;
3442 /* Parse a type declaration (except basic type), and return the type
3443 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3444 expected. 'type' should contain the basic type. 'ad' is the
3445 attribute definition of the basic type. It can be modified by
3448 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
3451 CType type1
, *type2
;
3452 int qualifiers
, storage
;
3454 while (tok
== '*') {
3462 qualifiers
|= VT_CONSTANT
;
3467 qualifiers
|= VT_VOLATILE
;
3475 type
->t
|= qualifiers
;
3478 /* XXX: clarify attribute handling */
3479 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3480 parse_attribute(ad
);
3482 /* recursive type */
3483 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3484 type1
.t
= 0; /* XXX: same as int */
3487 /* XXX: this is not correct to modify 'ad' at this point, but
3488 the syntax is not clear */
3489 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3490 parse_attribute(ad
);
3491 type_decl(&type1
, ad
, v
, td
);
3494 /* type identifier */
3495 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
3499 if (!(td
& TYPE_ABSTRACT
))
3500 expect("identifier");
3504 storage
= type
->t
& VT_STORAGE
;
3505 type
->t
&= ~VT_STORAGE
;
3506 if (storage
& VT_STATIC
) {
3507 int saved_nocode_wanted
= nocode_wanted
;
3509 post_type(type
, ad
);
3510 nocode_wanted
= saved_nocode_wanted
;
3512 post_type(type
, ad
);
3514 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3515 parse_attribute(ad
);
3519 /* append type at the end of type1 */
3532 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3533 ST_FUNC
int lvalue_type(int t
)
3538 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
3540 else if (bt
== VT_SHORT
)
3544 if (t
& VT_UNSIGNED
)
3545 r
|= VT_LVAL_UNSIGNED
;
3549 /* indirection with full error checking and bound check */
3550 ST_FUNC
void indir(void)
3552 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
3553 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
3557 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
3559 vtop
->type
= *pointed_type(&vtop
->type
);
3560 /* Arrays and functions are never lvalues */
3561 if (!(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_VLA
)
3562 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3563 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3564 /* if bound checking, the referenced pointer must be checked */
3565 #ifdef CONFIG_TCC_BCHECK
3566 if (tcc_state
->do_bounds_check
)
3567 vtop
->r
|= VT_MUSTBOUND
;
3572 /* pass a parameter to a function and do type checking and casting */
3573 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
3578 func_type
= func
->c
;
3579 if (func_type
== FUNC_OLD
||
3580 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
3581 /* default casting : only need to convert float to double */
3582 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
3585 } else if (vtop
->type
.t
& VT_BITFIELD
) {
3586 type
.t
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
3589 } else if (arg
== NULL
) {
3590 tcc_error("too many arguments to function");
3593 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
3594 gen_assign_cast(&type
);
3598 /* parse an expression of the form '(type)' or '(expr)' and return its
3600 static void parse_expr_type(CType
*type
)
3606 if (parse_btype(type
, &ad
)) {
3607 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3614 static void parse_type(CType
*type
)
3619 if (!parse_btype(type
, &ad
)) {
3622 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3625 static void vpush_tokc(int t
)
3630 vsetc(&type
, VT_CONST
, &tokc
);
3633 ST_FUNC
void unary(void)
3635 int n
, t
, align
, size
, r
, sizeof_caller
;
3639 static int in_sizeof
= 0;
3641 sizeof_caller
= in_sizeof
;
3643 /* XXX: GCC 2.95.3 does not generate a table although it should be
3657 vpush_tokc(VT_INT
| VT_UNSIGNED
);
3661 vpush_tokc(VT_LLONG
);
3665 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
3669 vpush_tokc(VT_FLOAT
);
3673 vpush_tokc(VT_DOUBLE
);
3677 vpush_tokc(VT_LDOUBLE
);
3680 case TOK___FUNCTION__
:
3682 goto tok_identifier
;
3688 /* special function name identifier */
3689 len
= strlen(funcname
) + 1;
3690 /* generate char[len] type */
3695 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
3696 ptr
= section_ptr_add(data_section
, len
);
3697 memcpy(ptr
, funcname
, len
);
3702 #ifdef TCC_TARGET_PE
3703 t
= VT_SHORT
| VT_UNSIGNED
;
3709 /* string parsing */
3712 if (tcc_state
->warn_write_strings
)
3717 memset(&ad
, 0, sizeof(AttributeDef
));
3718 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, NULL
, 0);
3723 if (parse_btype(&type
, &ad
)) {
3724 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
3726 /* check ISOC99 compound literal */
3728 /* data is allocated locally by default */
3733 /* all except arrays are lvalues */
3734 if (!(type
.t
& VT_ARRAY
))
3735 r
|= lvalue_type(type
.t
);
3736 memset(&ad
, 0, sizeof(AttributeDef
));
3737 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, NULL
, 0);
3739 if (sizeof_caller
) {
3746 } else if (tok
== '{') {
3747 /* save all registers */
3749 /* statement expression : we do not accept break/continue
3750 inside as GCC does */
3751 block(NULL
, NULL
, NULL
, NULL
, 0, 1);
3766 /* functions names must be treated as function pointers,
3767 except for unary '&' and sizeof. Since we consider that
3768 functions are not lvalues, we only have to handle it
3769 there and in function calls. */
3770 /* arrays can also be used although they are not lvalues */
3771 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
3772 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
3774 mk_pointer(&vtop
->type
);
3780 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3782 boolean
.t
= VT_BOOL
;
3784 vtop
->c
.i
= !vtop
->c
.i
;
3785 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
3786 vtop
->c
.i
= vtop
->c
.i
^ 1;
3789 vseti(VT_JMP
, gtst(1, 0));
3801 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
3802 tcc_error("pointer not accepted for unary plus");
3803 /* In order to force cast, we add zero, except for floating point
3804 where we really need an noop (otherwise -0.0 will be transformed
3806 if (!is_float(vtop
->type
.t
)) {
3817 unary_type(&type
); // Perform a in_sizeof = 0;
3818 size
= type_size(&type
, &align
);
3819 if (t
== TOK_SIZEOF
) {
3820 if (!(type
.t
& VT_VLA
)) {
3822 tcc_error("sizeof applied to an incomplete type");
3825 vla_runtime_type_size(&type
, &align
);
3830 vtop
->type
.t
|= VT_UNSIGNED
;
3833 case TOK_builtin_types_compatible_p
:
3842 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3843 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3844 vpushi(is_compatible_types(&type1
, &type2
));
3847 case TOK_builtin_constant_p
:
3849 int saved_nocode_wanted
, res
;
3852 saved_nocode_wanted
= nocode_wanted
;
3855 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
3857 nocode_wanted
= saved_nocode_wanted
;
3862 case TOK_builtin_frame_address
:
3868 if (tok
!= TOK_CINT
|| tokc
.i
< 0) {
3869 tcc_error("__builtin_frame_address only takes positive integers");
3876 vset(&type
, VT_LOCAL
, 0); /* local frame */
3878 mk_pointer(&vtop
->type
);
3879 indir(); /* -> parent frame */
3883 #ifdef TCC_TARGET_X86_64
3884 #ifdef TCC_TARGET_PE
3885 case TOK_builtin_va_start
:
3893 if ((vtop
->r
& VT_VALMASK
) != VT_LOCAL
)
3894 tcc_error("__builtin_va_start expects a local variable");
3895 vtop
->r
&= ~(VT_LVAL
| VT_REF
);
3896 vtop
->type
= char_pointer_type
;
3901 case TOK_builtin_va_arg_types
:
3908 vpushi(classify_x86_64_va_arg(&type
));
3923 t
= vtop
->type
.t
& VT_BTYPE
;
3925 /* In IEEE negate(x) isn't subtract(0,x), but rather
3930 else if (t
== VT_DOUBLE
)
3941 goto tok_identifier
;
3943 /* allow to take the address of a label */
3944 if (tok
< TOK_UIDENT
)
3945 expect("label identifier");
3946 s
= label_find(tok
);
3948 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
3950 if (s
->r
== LABEL_DECLARED
)
3951 s
->r
= LABEL_FORWARD
;
3954 s
->type
.t
= VT_VOID
;
3955 mk_pointer(&s
->type
);
3956 s
->type
.t
|= VT_STATIC
;
3958 vpushsym(&s
->type
, s
);
3962 // special qnan , snan and infinity values
3964 vpush64(VT_DOUBLE
, 0x7ff8000000000000ULL
);
3968 vpush64(VT_DOUBLE
, 0x7ff0000000000001ULL
);
3972 vpush64(VT_DOUBLE
, 0x7ff0000000000000ULL
);
3981 expect("identifier");
3984 const char *name
= get_tok_str(t
, NULL
);
3986 tcc_error("'%s' undeclared", name
);
3987 /* for simple function calls, we tolerate undeclared
3988 external reference to int() function */
3989 if (tcc_state
->warn_implicit_function_declaration
3990 #ifdef TCC_TARGET_PE
3991 /* people must be warned about using undeclared WINAPI functions
3992 (which usually start with uppercase letter) */
3993 || (name
[0] >= 'A' && name
[0] <= 'Z')
3996 tcc_warning("implicit declaration of function '%s'", name
);
3997 s
= external_global_sym(t
, &func_old_type
, 0);
3999 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
4000 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
4001 /* if referencing an inline function, then we generate a
4002 symbol to it if not already done. It will have the
4003 effect to generate code for it at the end of the
4004 compilation unit. Inline function as always
4005 generated in the text section. */
4007 put_extern_sym(s
, text_section
, 0, 0);
4008 r
= VT_SYM
| VT_CONST
;
4012 vset(&s
->type
, r
, s
->c
);
4013 /* if forward reference, we must point to s */
4014 if (vtop
->r
& VT_SYM
) {
4016 vtop
->c
.ptr_offset
= 0;
4021 /* post operations */
4023 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
4026 } else if (tok
== '.' || tok
== TOK_ARROW
) {
4029 if (tok
== TOK_ARROW
)
4031 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
4035 /* expect pointer on structure */
4036 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
4037 expect("struct or union");
4041 while ((s
= s
->next
) != NULL
) {
4046 tcc_error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, NULL
));
4047 /* add field offset to pointer */
4048 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
4051 /* change type to field type, and set to lvalue */
4052 vtop
->type
= s
->type
;
4053 vtop
->type
.t
|= qualifiers
;
4054 /* an array is never an lvalue */
4055 if (!(vtop
->type
.t
& VT_ARRAY
)) {
4056 vtop
->r
|= lvalue_type(vtop
->type
.t
);
4057 #ifdef CONFIG_TCC_BCHECK
4058 /* if bound checking, the referenced pointer must be checked */
4059 if (tcc_state
->do_bounds_check
)
4060 vtop
->r
|= VT_MUSTBOUND
;
4064 } else if (tok
== '[') {
4070 } else if (tok
== '(') {
4073 int nb_args
, ret_nregs
, ret_align
, variadic
;
4076 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
4077 /* pointer test (no array accepted) */
4078 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
4079 vtop
->type
= *pointed_type(&vtop
->type
);
4080 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
4084 expect("function pointer");
4087 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
4089 /* get return type */
4092 sa
= s
->next
; /* first parameter */
4095 /* compute first implicit argument if a structure is returned */
4096 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
4097 variadic
= (s
->c
== FUNC_ELLIPSIS
);
4098 ret_nregs
= gfunc_sret(&s
->type
, variadic
, &ret
.type
,
4101 /* get some space for the returned structure */
4102 size
= type_size(&s
->type
, &align
);
4103 loc
= (loc
- size
) & -align
;
4105 ret
.r
= VT_LOCAL
| VT_LVAL
;
4106 /* pass it as 'int' to avoid structure arg passing
4108 vseti(VT_LOCAL
, loc
);
4118 /* return in register */
4119 if (is_float(ret
.type
.t
)) {
4120 ret
.r
= reg_fret(ret
.type
.t
);
4121 #ifdef TCC_TARGET_X86_64
4122 if ((ret
.type
.t
& VT_BTYPE
) == VT_QFLOAT
)
4126 #ifdef TCC_TARGET_X86_64
4127 if ((ret
.type
.t
& VT_BTYPE
) == VT_QLONG
)
4129 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
4139 gfunc_param_typed(s
, sa
);
4149 tcc_error("too few arguments to function");
4151 if (!nocode_wanted
) {
4152 gfunc_call(nb_args
);
4154 vtop
-= (nb_args
+ 1);
4158 for (r
= ret
.r
+ ret_nregs
+ !ret_nregs
; r
-- > ret
.r
;) {
4159 vsetc(&ret
.type
, r
, &ret
.c
);
4160 vtop
->r2
= ret
.r2
; /* Loop only happens when r2 is VT_CONST */
4163 /* handle packed struct return */
4164 if (((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) && ret_nregs
) {
4167 size
= type_size(&s
->type
, &align
);
4168 loc
= (loc
- size
) & -align
;
4172 vset(&ret
.type
, VT_LOCAL
| VT_LVAL
, addr
+ offset
);
4176 if (--ret_nregs
== 0)
4178 /* XXX: compatible with arm only: ret_align == register_size */
4179 offset
+= ret_align
;
4181 vset(&s
->type
, VT_LOCAL
| VT_LVAL
, addr
);
4189 ST_FUNC
void expr_prod(void)
4194 while (tok
== '*' || tok
== '/' || tok
== '%') {
4202 ST_FUNC
void expr_sum(void)
4207 while (tok
== '+' || tok
== '-') {
4215 static void expr_shift(void)
4220 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
4228 static void expr_cmp(void)
4233 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
4234 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
4242 static void expr_cmpeq(void)
4247 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
4255 static void expr_and(void)
4258 while (tok
== '&') {
4265 static void expr_xor(void)
4268 while (tok
== '^') {
4275 static void expr_or(void)
4278 while (tok
== '|') {
4285 /* XXX: fix this mess */
4286 static void expr_land_const(void)
4289 while (tok
== TOK_LAND
) {
4296 /* XXX: fix this mess */
4297 static void expr_lor_const(void)
4300 while (tok
== TOK_LOR
) {
4307 /* only used if non constant */
4308 static void expr_land(void)
4313 if (tok
== TOK_LAND
) {
4318 if (tok
!= TOK_LAND
) {
4328 static void expr_lor(void)
4333 if (tok
== TOK_LOR
) {
4338 if (tok
!= TOK_LOR
) {
4348 /* XXX: better constant handling */
4349 static void expr_cond(void)
4351 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
;
4353 CType type
, type1
, type2
;
4360 boolean
.t
= VT_BOOL
;
4366 if (tok
!= ':' || !gnu_ext
) {
4381 if (vtop
!= vstack
) {
4382 /* needed to avoid having different registers saved in
4384 if (is_float(vtop
->type
.t
)) {
4386 #ifdef TCC_TARGET_X86_64
4387 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4397 if (tok
== ':' && gnu_ext
) {
4405 sv
= *vtop
; /* save value to handle it later */
4406 vtop
--; /* no vpop so that FP stack is not flushed */
4414 bt1
= t1
& VT_BTYPE
;
4416 bt2
= t2
& VT_BTYPE
;
4417 /* cast operands to correct type according to ISOC rules */
4418 if (is_float(bt1
) || is_float(bt2
)) {
4419 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
4420 type
.t
= VT_LDOUBLE
;
4421 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
4426 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
4427 /* cast to biggest op */
4429 /* convert to unsigned if it does not fit in a long long */
4430 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
4431 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
4432 type
.t
|= VT_UNSIGNED
;
4433 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
4434 /* If one is a null ptr constant the result type
4436 if (is_null_pointer (vtop
))
4438 else if (is_null_pointer (&sv
))
4440 /* XXX: test pointer compatibility, C99 has more elaborate
4444 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
4445 /* XXX: test function pointer compatibility */
4446 type
= bt1
== VT_FUNC
? type1
: type2
;
4447 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
4448 /* XXX: test structure compatibility */
4449 type
= bt1
== VT_STRUCT
? type1
: type2
;
4450 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
4451 /* NOTE: as an extension, we accept void on only one side */
4454 /* integer operations */
4456 /* convert to unsigned if it does not fit in an integer */
4457 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
4458 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
4459 type
.t
|= VT_UNSIGNED
;
4462 /* now we convert second operand */
4464 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4467 if (is_float(type
.t
)) {
4469 #ifdef TCC_TARGET_X86_64
4470 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4474 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
4475 /* for long longs, we use fixed registers to avoid having
4476 to handle a complicated move */
4481 /* this is horrible, but we must also convert first
4485 /* put again first value and cast it */
4488 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4491 move_reg(r2
, r1
, type
.t
);
4498 static void expr_eq(void)
4504 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
4505 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
4506 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
4521 ST_FUNC
void gexpr(void)
4532 /* parse an expression and return its type without any side effect. */
4533 static void expr_type(CType
*type
)
4535 int saved_nocode_wanted
;
4537 saved_nocode_wanted
= nocode_wanted
;
4542 nocode_wanted
= saved_nocode_wanted
;
4545 /* parse a unary expression and return its type without any side
4547 static void unary_type(CType
*type
)
4559 /* parse a constant expression and return value in vtop. */
4560 static void expr_const1(void)
4569 /* parse an integer constant and return its value. */
4570 ST_FUNC
int expr_const(void)
4574 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
4575 expect("constant expression");
4581 /* return the label token if current token is a label, otherwise
4583 static int is_label(void)
4587 /* fast test first */
4588 if (tok
< TOK_UIDENT
)
4590 /* no need to save tokc because tok is an identifier */
4597 unget_tok(last_tok
);
4602 static void label_or_decl(int l
)
4606 /* fast test first */
4607 if (tok
>= TOK_UIDENT
)
4609 /* no need to save tokc because tok is an identifier */
4613 unget_tok(last_tok
);
4616 unget_tok(last_tok
);
4621 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
4622 int case_reg
, int is_expr
)
4625 Sym
*s
, *frame_bottom
;
4627 /* generate line number info */
4628 if (tcc_state
->do_debug
&&
4629 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
4630 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
4632 last_line_num
= file
->line_num
;
4636 /* default return value is (void) */
4638 vtop
->type
.t
= VT_VOID
;
4641 if (tok
== TOK_IF
) {
4648 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4650 if (c
== TOK_ELSE
) {
4654 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4655 gsym(d
); /* patch else jmp */
4658 } else if (tok
== TOK_WHILE
) {
4666 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4670 } else if (tok
== '{') {
4672 int saved_loc
, saved_pop_stack
, size
;
4673 int block_vla_sp_loc
, *saved_vla_sp_loc
, saved_vla_flags
;
4676 /* record local declaration stack position */
4678 frame_bottom
= sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4679 frame_bottom
->next
= scope_stack_bottom
;
4680 scope_stack_bottom
= frame_bottom
;
4681 llabel
= local_label_stack
;
4684 saved_pop_stack
= pop_stack
;
4686 /* save VLA state */
4687 block_vla_sp_loc
= *(saved_vla_sp_loc
= vla_sp_loc
);
4688 if (saved_vla_sp_loc
!= &vla_sp_root_loc
)
4689 vla_sp_loc
= &block_vla_sp_loc
;
4691 saved_vla_flags
= vla_flags
;
4692 vla_flags
|= VLA_NEED_NEW_FRAME
;
4694 /* handle local labels declarations */
4695 if (tok
== TOK_LABEL
) {
4698 if (tok
< TOK_UIDENT
)
4699 expect("label identifier");
4700 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
4710 while (tok
!= '}') {
4711 label_or_decl(VT_LOCAL
);
4715 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4718 /* pop locally defined labels */
4719 label_pop(&local_label_stack
, llabel
);
4721 /* XXX: this solution makes only valgrind happy...
4722 triggered by gcc.c-torture/execute/20000917-1.c */
4724 switch(vtop
->type
.t
& VT_BTYPE
) {
4729 for(p
=vtop
->type
.ref
;p
;p
=p
->prev
)
4731 tcc_error("unsupported expression type");
4734 /* pop locally defined symbols */
4735 scope_stack_bottom
= scope_stack_bottom
->next
;
4736 sym_pop(&local_stack
, s
);
4738 size
= -(loc
- saved_loc
);
4739 pop_stack
= saved_pop_stack
;
4743 /* Pop VLA frames and restore stack pointer if required */
4744 if (saved_vla_sp_loc
!= &vla_sp_root_loc
)
4745 *saved_vla_sp_loc
= block_vla_sp_loc
;
4746 if (vla_sp_loc
!= (saved_vla_sp_loc
== &vla_sp_root_loc
? &vla_sp_root_loc
: &block_vla_sp_loc
)) {
4747 vla_sp_loc
= saved_vla_sp_loc
;
4748 gen_vla_sp_restore(*vla_sp_loc
);
4750 vla_flags
= (vla_flags
& ~VLA_SCOPE_FLAGS
) | (saved_vla_flags
& VLA_SCOPE_FLAGS
);
4753 } else if (tok
== TOK_RETURN
) {
4757 gen_assign_cast(&func_vt
);
4758 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
4759 CType type
, ret_type
;
4760 int ret_align
, ret_nregs
;
4761 ret_nregs
= gfunc_sret(&func_vt
, func_var
, &ret_type
,
4763 if (0 == ret_nregs
) {
4764 /* if returning structure, must copy it to implicit
4765 first pointer arg location */
4768 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
4771 /* copy structure value to pointer */
4774 /* returning structure packed into registers */
4775 int r
, size
, addr
, align
;
4776 size
= type_size(&func_vt
,&align
);
4777 if ((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) || (vtop
->c
.i
& (ret_align
-1)))
4778 && (align
& (ret_align
-1))) {
4779 loc
= (loc
- size
) & -align
;
4782 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
4785 vset(&ret_type
, VT_LOCAL
| VT_LVAL
, addr
);
4787 vtop
->type
= ret_type
;
4788 if (is_float(ret_type
.t
))
4789 r
= rc_fret(ret_type
.t
);
4795 if (--ret_nregs
== 0)
4797 /* We assume that when a structure is returned in multiple
4798 registers, their classes are consecutive values of the
4801 /* XXX: compatible with arm only: ret_align == register_size */
4802 vtop
->c
.i
+= ret_align
;
4803 vtop
->r
= VT_LOCAL
| VT_LVAL
;
4806 } else if (is_float(func_vt
.t
)) {
4807 gv(rc_fret(func_vt
.t
));
4811 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
4814 rsym
= gjmp(rsym
); /* jmp */
4815 } else if (tok
== TOK_BREAK
) {
4818 tcc_error("cannot break");
4819 *bsym
= gjmp(*bsym
);
4822 } else if (tok
== TOK_CONTINUE
) {
4825 tcc_error("cannot continue");
4826 *csym
= gjmp(*csym
);
4829 } else if (tok
== TOK_FOR
) {
4834 frame_bottom
= sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4835 frame_bottom
->next
= scope_stack_bottom
;
4836 scope_stack_bottom
= frame_bottom
;
4838 /* c99 for-loop init decl? */
4839 if (!decl0(VT_LOCAL
, 1)) {
4840 /* no, regular for-loop init expr */
4864 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4868 scope_stack_bottom
= scope_stack_bottom
->next
;
4869 sym_pop(&local_stack
, s
);
4871 if (tok
== TOK_DO
) {
4876 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4887 if (tok
== TOK_SWITCH
) {
4891 /* XXX: other types than integer */
4892 case_reg
= gv(RC_INT
);
4896 b
= gjmp(0); /* jump to first case */
4898 block(&a
, csym
, &b
, &c
, case_reg
, 0);
4899 /* if no default, jmp after switch */
4907 if (tok
== TOK_CASE
) {
4914 if (gnu_ext
&& tok
== TOK_DOTS
) {
4918 tcc_warning("empty case range");
4920 /* since a case is like a label, we must skip it with a jmp */
4927 *case_sym
= gtst(1, 0);
4930 *case_sym
= gtst(1, 0);
4934 *case_sym
= gtst(1, *case_sym
);
4939 goto block_after_label
;
4941 if (tok
== TOK_DEFAULT
) {
4947 tcc_error("too many 'default'");
4950 goto block_after_label
;
4952 if (tok
== TOK_GOTO
) {
4954 if (tok
== '*' && gnu_ext
) {
4958 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
4961 } else if (tok
>= TOK_UIDENT
) {
4962 s
= label_find(tok
);
4963 /* put forward definition if needed */
4965 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4967 if (s
->r
== LABEL_DECLARED
)
4968 s
->r
= LABEL_FORWARD
;
4970 /* label already defined */
4971 if (vla_flags
& VLA_IN_SCOPE
) {
4972 /* If VLAs are in use, save the current stack pointer and
4973 reset the stack pointer to what it was at function entry
4974 (label will restore stack pointer in inner scopes) */
4976 gen_vla_sp_restore(vla_sp_root_loc
);
4978 if (s
->r
& LABEL_FORWARD
)
4979 s
->jnext
= gjmp(s
->jnext
);
4981 gjmp_addr(s
->jnext
);
4984 expect("label identifier");
4987 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
4993 if (vla_flags
& VLA_IN_SCOPE
) {
4994 /* save/restore stack pointer across label
4995 this is a no-op when combined with the load immediately
4996 after the label unless we arrive via goto */
5001 if (s
->r
== LABEL_DEFINED
)
5002 tcc_error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
5004 s
->r
= LABEL_DEFINED
;
5006 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
5009 if (vla_flags
& VLA_IN_SCOPE
) {
5010 gen_vla_sp_restore(*vla_sp_loc
);
5011 vla_flags
|= VLA_NEED_NEW_FRAME
;
5013 /* we accept this, but it is a mistake */
5016 tcc_warning("deprecated use of label at end of compound statement");
5020 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
5023 /* expression case */
5038 /* t is the array or struct type. c is the array or struct
5039 address. cur_index/cur_field is the pointer to the current
5040 value. 'size_only' is true if only size info is needed (only used
5042 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
5043 int *cur_index
, Sym
**cur_field
,
5047 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
5053 if (gnu_ext
&& (l
= is_label()) != 0)
5055 while (tok
== '[' || tok
== '.') {
5057 if (!(type
->t
& VT_ARRAY
))
5058 expect("array type");
5061 index
= expr_const();
5062 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
5063 expect("invalid index");
5064 if (tok
== TOK_DOTS
&& gnu_ext
) {
5066 index_last
= expr_const();
5067 if (index_last
< 0 ||
5068 (s
->c
>= 0 && index_last
>= s
->c
) ||
5070 expect("invalid index");
5076 *cur_index
= index_last
;
5077 type
= pointed_type(type
);
5078 elem_size
= type_size(type
, &align
);
5079 c
+= index
* elem_size
;
5080 /* NOTE: we only support ranges for last designator */
5081 nb_elems
= index_last
- index
+ 1;
5082 if (nb_elems
!= 1) {
5091 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
5092 expect("struct/union type");
5105 /* XXX: fix this mess by using explicit storage field */
5107 type1
.t
|= (type
->t
& ~VT_TYPE
);
5121 if (type
->t
& VT_ARRAY
) {
5123 type
= pointed_type(type
);
5124 c
+= index
* type_size(type
, &align
);
5128 tcc_error("too many field init");
5129 /* XXX: fix this mess by using explicit storage field */
5131 type1
.t
|= (type
->t
& ~VT_TYPE
);
5136 decl_initializer(type
, sec
, c
, 0, size_only
);
5138 /* XXX: make it more general */
5139 if (!size_only
&& nb_elems
> 1) {
5140 unsigned long c_end
;
5145 tcc_error("range init not supported yet for dynamic storage");
5146 c_end
= c
+ nb_elems
* elem_size
;
5147 if (c_end
> sec
->data_allocated
)
5148 section_realloc(sec
, c_end
);
5149 src
= sec
->data
+ c
;
5151 for(i
= 1; i
< nb_elems
; i
++) {
5153 memcpy(dst
, src
, elem_size
);
5159 #define EXPR_CONST 1
5162 /* store a value or an expression directly in global data or in local array */
5163 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
5164 int v
, int expr_type
)
5166 int saved_global_expr
, bt
, bit_pos
, bit_size
;
5168 unsigned long long bit_mask
;
5176 /* compound literals must be allocated globally in this case */
5177 saved_global_expr
= global_expr
;
5180 global_expr
= saved_global_expr
;
5181 /* NOTE: symbols are accepted */
5182 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
5183 tcc_error("initializer element is not constant");
5191 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
5194 /* XXX: not portable */
5195 /* XXX: generate error if incorrect relocation */
5196 gen_assign_cast(&dtype
);
5197 bt
= type
->t
& VT_BTYPE
;
5198 /* we'll write at most 12 bytes */
5199 if (c
+ 12 > sec
->data_allocated
) {
5200 section_realloc(sec
, c
+ 12);
5202 ptr
= sec
->data
+ c
;
5203 /* XXX: make code faster ? */
5204 if (!(type
->t
& VT_BITFIELD
)) {
5209 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5210 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
5211 bit_mask
= (1LL << bit_size
) - 1;
5213 if ((vtop
->r
& VT_SYM
) &&
5219 (bt
== VT_INT
&& bit_size
!= 32)))
5220 tcc_error("initializer element is not computable at load time");
5223 vtop
->c
.i
= (vtop
->c
.i
!= 0);
5225 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5228 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5231 *(double *)ptr
= vtop
->c
.d
;
5234 *(long double *)ptr
= vtop
->c
.ld
;
5237 *(long long *)ptr
|= (vtop
->c
.ll
& bit_mask
) << bit_pos
;
5240 if (vtop
->r
& VT_SYM
) {
5241 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
5243 *(addr_t
*)ptr
|= (vtop
->c
.ptr_offset
& bit_mask
) << bit_pos
;
5246 if (vtop
->r
& VT_SYM
) {
5247 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
5249 *(int *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5254 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
5261 /* put zeros for variable based init */
5262 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
5265 /* nothing to do because globals are already set to zero */
5267 vpush_global_sym(&func_old_type
, TOK_memset
);
5269 #ifdef TCC_TARGET_ARM
5280 /* 't' contains the type and storage info. 'c' is the offset of the
5281 object in section 'sec'. If 'sec' is NULL, it means stack based
5282 allocation. 'first' is true if array '{' must be read (multi
5283 dimension implicit array init handling). 'size_only' is true if
5284 size only evaluation is wanted (only for arrays). */
5285 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
5286 int first
, int size_only
)
5288 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, parlevel1
, i
;
5289 int size1
, align1
, expr_type
;
5293 if (type
->t
& VT_VLA
) {
5296 /* save current stack pointer */
5297 if (vla_flags
& VLA_NEED_NEW_FRAME
) {
5299 vla_flags
= VLA_IN_SCOPE
;
5300 vla_sp_loc
= &vla_sp_loc_tmp
;
5303 vla_runtime_type_size(type
, &a
);
5304 gen_vla_alloc(type
, a
);
5305 vset(type
, VT_LOCAL
|VT_LVAL
, c
);
5309 } else if (type
->t
& VT_ARRAY
) {
5313 t1
= pointed_type(type
);
5314 size1
= type_size(t1
, &align1
);
5317 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
5320 tcc_error("character array initializer must be a literal,"
5321 " optionally enclosed in braces");
5326 /* only parse strings here if correct type (otherwise: handle
5327 them as ((w)char *) expressions */
5328 if ((tok
== TOK_LSTR
&&
5329 #ifdef TCC_TARGET_PE
5330 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
5332 (t1
->t
& VT_BTYPE
) == VT_INT
5334 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
5335 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5340 /* compute maximum number of chars wanted */
5342 cstr_len
= cstr
->size
;
5344 cstr_len
= cstr
->size
/ sizeof(nwchar_t
);
5347 if (n
>= 0 && nb
> (n
- array_length
))
5348 nb
= n
- array_length
;
5351 tcc_warning("initializer-string for array is too long");
5352 /* in order to go faster for common case (char
5353 string in global variable, we handle it
5355 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
5356 memcpy(sec
->data
+ c
+ array_length
, cstr
->data
, nb
);
5360 ch
= ((unsigned char *)cstr
->data
)[i
];
5362 ch
= ((nwchar_t
*)cstr
->data
)[i
];
5363 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
5371 /* only add trailing zero if enough storage (no
5372 warning in this case since it is standard) */
5373 if (n
< 0 || array_length
< n
) {
5375 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
5381 while (tok
!= '}') {
5382 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
5383 if (n
>= 0 && index
>= n
)
5384 tcc_error("index too large");
5385 /* must put zero in holes (note that doing it that way
5386 ensures that it even works with designators) */
5387 if (!size_only
&& array_length
< index
) {
5388 init_putz(t1
, sec
, c
+ array_length
* size1
,
5389 (index
- array_length
) * size1
);
5392 if (index
> array_length
)
5393 array_length
= index
;
5394 /* special test for multi dimensional arrays (may not
5395 be strictly correct if designators are used at the
5397 if (index
>= n
&& no_oblock
)
5406 /* put zeros at the end */
5407 if (!size_only
&& n
>= 0 && array_length
< n
) {
5408 init_putz(t1
, sec
, c
+ array_length
* size1
,
5409 (n
- array_length
) * size1
);
5411 /* patch type size if needed */
5413 s
->c
= array_length
;
5414 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
5415 (sec
|| !first
|| tok
== '{')) {
5418 /* NOTE: the previous test is a specific case for automatic
5419 struct/union init */
5420 /* XXX: union needs only one init */
5422 /* XXX: this test is incorrect for local initializers
5423 beginning with ( without {. It would be much more difficult
5424 to do it correctly (ideally, the expression parser should
5425 be used in all cases) */
5431 while (tok
== '(') {
5435 if (!parse_btype(&type1
, &ad1
))
5437 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
5439 if (!is_assignable_types(type
, &type1
))
5440 tcc_error("invalid type for cast");
5445 if (first
|| tok
== '{') {
5454 while (tok
!= '}') {
5455 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
5457 if (!size_only
&& array_length
< index
) {
5458 init_putz(type
, sec
, c
+ array_length
,
5459 index
- array_length
);
5461 index
= index
+ type_size(&f
->type
, &align1
);
5462 if (index
> array_length
)
5463 array_length
= index
;
5465 /* gr: skip fields from same union - ugly. */
5467 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
5468 /* test for same offset */
5469 if (f
->next
->c
!= f
->c
)
5471 /* if yes, test for bitfield shift */
5472 if ((f
->type
.t
& VT_BITFIELD
) && (f
->next
->type
.t
& VT_BITFIELD
)) {
5473 int bit_pos_1
= (f
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5474 int bit_pos_2
= (f
->next
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5475 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
5476 if (bit_pos_1
!= bit_pos_2
)
5483 if (no_oblock
&& f
== NULL
)
5489 /* put zeros at the end */
5490 if (!size_only
&& array_length
< n
) {
5491 init_putz(type
, sec
, c
+ array_length
,
5500 } else if (tok
== '{') {
5502 decl_initializer(type
, sec
, c
, first
, size_only
);
5504 } else if (size_only
) {
5505 /* just skip expression */
5506 parlevel
= parlevel1
= 0;
5507 while ((parlevel
> 0 || parlevel1
> 0 ||
5508 (tok
!= '}' && tok
!= ',')) && tok
!= -1) {
5511 else if (tok
== ')')
5513 else if (tok
== '{')
5515 else if (tok
== '}')
5520 /* currently, we always use constant expression for globals
5521 (may change for scripting case) */
5522 expr_type
= EXPR_CONST
;
5524 expr_type
= EXPR_ANY
;
5525 init_putv(type
, sec
, c
, 0, expr_type
);
5529 /* parse an initializer for type 't' if 'has_init' is non zero, and
5530 allocate space in local or global data space ('r' is either
5531 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5532 variable 'v' with an associated name represented by 'asm_label' of
5533 scope 'scope' is declared before initializers are parsed. If 'v' is
5534 zero, then a reference to the new object is put in the value stack.
5535 If 'has_init' is 2, a special parsing is done to handle string
5537 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
5538 int has_init
, int v
, char *asm_label
,
5541 int size
, align
, addr
, data_offset
;
5543 ParseState saved_parse_state
= {0};
5544 TokenString init_str
;
5546 Sym
*flexible_array
;
5548 flexible_array
= NULL
;
5549 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
5550 Sym
*field
= type
->ref
->next
;
5553 field
= field
->next
;
5554 if (field
->type
.t
& VT_ARRAY
&& field
->type
.ref
->c
< 0)
5555 flexible_array
= field
;
5559 size
= type_size(type
, &align
);
5560 /* If unknown size, we must evaluate it before
5561 evaluating initializers because
5562 initializers can generate global data too
5563 (e.g. string pointers or ISOC99 compound
5564 literals). It also simplifies local
5565 initializers handling */
5566 tok_str_new(&init_str
);
5567 if (size
< 0 || (flexible_array
&& has_init
)) {
5569 tcc_error("unknown type size");
5570 /* get all init string */
5571 if (has_init
== 2) {
5572 /* only get strings */
5573 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5574 tok_str_add_tok(&init_str
);
5579 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
5581 tcc_error("unexpected end of file in initializer");
5582 tok_str_add_tok(&init_str
);
5585 else if (tok
== '}') {
5595 tok_str_add(&init_str
, -1);
5596 tok_str_add(&init_str
, 0);
5599 save_parse_state(&saved_parse_state
);
5601 macro_ptr
= init_str
.str
;
5603 decl_initializer(type
, NULL
, 0, 1, 1);
5604 /* prepare second initializer parsing */
5605 macro_ptr
= init_str
.str
;
5608 /* if still unknown size, error */
5609 size
= type_size(type
, &align
);
5611 tcc_error("unknown type size");
5614 size
+= flexible_array
->type
.ref
->c
* pointed_size(&flexible_array
->type
);
5615 /* take into account specified alignment if bigger */
5616 if (ad
->a
.aligned
) {
5617 if (ad
->a
.aligned
> align
)
5618 align
= ad
->a
.aligned
;
5619 } else if (ad
->a
.packed
) {
5622 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
5624 #ifdef CONFIG_TCC_BCHECK
5625 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5629 loc
= (loc
- size
) & -align
;
5631 #ifdef CONFIG_TCC_BCHECK
5632 /* handles bounds */
5633 /* XXX: currently, since we do only one pass, we cannot track
5634 '&' operators, so we add only arrays */
5635 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5636 unsigned long *bounds_ptr
;
5637 /* add padding between regions */
5639 /* then add local bound info */
5640 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(unsigned long));
5641 bounds_ptr
[0] = addr
;
5642 bounds_ptr
[1] = size
;
5646 /* local variable */
5647 sym_push(v
, type
, r
, addr
);
5649 /* push local reference */
5650 vset(type
, r
, addr
);
5656 if (v
&& scope
== VT_CONST
) {
5657 /* see if the symbol was already defined */
5660 if (!is_compatible_types(&sym
->type
, type
))
5661 tcc_error("incompatible types for redefinition of '%s'",
5662 get_tok_str(v
, NULL
));
5663 if (sym
->type
.t
& VT_EXTERN
) {
5664 /* if the variable is extern, it was not allocated */
5665 sym
->type
.t
&= ~VT_EXTERN
;
5666 /* set array size if it was omitted in extern
5668 if ((sym
->type
.t
& VT_ARRAY
) &&
5669 sym
->type
.ref
->c
< 0 &&
5671 sym
->type
.ref
->c
= type
->ref
->c
;
5673 /* we accept several definitions of the same
5674 global variable. this is tricky, because we
5675 must play with the SHN_COMMON type of the symbol */
5676 /* XXX: should check if the variable was already
5677 initialized. It is incorrect to initialized it
5679 /* no init data, we won't add more to the symbol */
5686 /* allocate symbol in corresponding section */
5691 else if (tcc_state
->nocommon
)
5695 data_offset
= sec
->data_offset
;
5696 data_offset
= (data_offset
+ align
- 1) & -align
;
5698 /* very important to increment global pointer at this time
5699 because initializers themselves can create new initializers */
5700 data_offset
+= size
;
5701 #ifdef CONFIG_TCC_BCHECK
5702 /* add padding if bound check */
5703 if (tcc_state
->do_bounds_check
)
5706 sec
->data_offset
= data_offset
;
5707 /* allocate section space to put the data */
5708 if (sec
->sh_type
!= SHT_NOBITS
&&
5709 data_offset
> sec
->data_allocated
)
5710 section_realloc(sec
, data_offset
);
5711 /* align section if needed */
5712 if (align
> sec
->sh_addralign
)
5713 sec
->sh_addralign
= align
;
5715 addr
= 0; /* avoid warning */
5719 if (scope
!= VT_CONST
|| !sym
) {
5720 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
5721 sym
->asm_label
= asm_label
;
5723 /* update symbol definition */
5725 put_extern_sym(sym
, sec
, addr
, size
);
5728 /* put a common area */
5729 put_extern_sym(sym
, NULL
, align
, size
);
5730 /* XXX: find a nicer way */
5731 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
5732 esym
->st_shndx
= SHN_COMMON
;
5735 /* push global reference */
5736 sym
= get_sym_ref(type
, sec
, addr
, size
);
5737 vpushsym(type
, sym
);
5739 /* patch symbol weakness */
5740 if (type
->t
& VT_WEAK
)
5742 apply_visibility(sym
, type
);
5743 #ifdef CONFIG_TCC_BCHECK
5744 /* handles bounds now because the symbol must be defined
5745 before for the relocation */
5746 if (tcc_state
->do_bounds_check
) {
5747 unsigned long *bounds_ptr
;
5749 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
5750 /* then add global bound info */
5751 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(long));
5752 bounds_ptr
[0] = 0; /* relocated */
5753 bounds_ptr
[1] = size
;
5757 if (has_init
|| (type
->t
& VT_VLA
)) {
5758 decl_initializer(type
, sec
, addr
, 1, 0);
5759 /* restore parse state if needed */
5761 tok_str_free(init_str
.str
);
5762 restore_parse_state(&saved_parse_state
);
5764 /* patch flexible array member size back to -1, */
5765 /* for possible subsequent similar declarations */
5767 flexible_array
->type
.ref
->c
= -1;
5772 static void put_func_debug(Sym
*sym
)
5777 /* XXX: we put here a dummy type */
5778 snprintf(buf
, sizeof(buf
), "%s:%c1",
5779 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
5780 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
5781 cur_text_section
, sym
->c
);
5782 /* //gr gdb wants a line at the function */
5783 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
5788 /* parse an old style function declaration list */
5789 /* XXX: check multiple parameter */
5790 static void func_decl_list(Sym
*func_sym
)
5797 /* parse each declaration */
5798 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
&&
5799 tok
!= TOK_ASM1
&& tok
!= TOK_ASM2
&& tok
!= TOK_ASM3
) {
5800 if (!parse_btype(&btype
, &ad
))
5801 expect("declaration list");
5802 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5803 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5805 /* we accept no variable after */
5809 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5810 /* find parameter in function parameter list */
5813 if ((s
->v
& ~SYM_FIELD
) == v
)
5817 tcc_error("declaration for parameter '%s' but no such parameter",
5818 get_tok_str(v
, NULL
));
5820 /* check that no storage specifier except 'register' was given */
5821 if (type
.t
& VT_STORAGE
)
5822 tcc_error("storage class specified for '%s'", get_tok_str(v
, NULL
));
5823 convert_parameter_type(&type
);
5824 /* we can add the type (NOTE: it could be local to the function) */
5826 /* accept other parameters */
5837 /* parse a function defined by symbol 'sym' and generate its code in
5838 'cur_text_section' */
5839 static void gen_function(Sym
*sym
)
5841 int saved_nocode_wanted
= nocode_wanted
;
5843 ind
= cur_text_section
->data_offset
;
5844 /* NOTE: we patch the symbol size later */
5845 put_extern_sym(sym
, cur_text_section
, ind
, 0);
5846 funcname
= get_tok_str(sym
->v
, NULL
);
5848 /* Initialize VLA state */
5849 vla_sp_loc
= &vla_sp_root_loc
;
5850 vla_flags
= VLA_NEED_NEW_FRAME
;
5851 /* put debug symbol */
5852 if (tcc_state
->do_debug
)
5853 put_func_debug(sym
);
5854 /* push a dummy symbol to enable local sym storage */
5855 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
5856 gfunc_prolog(&sym
->type
);
5857 #ifdef CONFIG_TCC_BCHECK
5858 if (tcc_state
->do_bounds_check
5859 && !strcmp(get_tok_str(sym
->v
, NULL
), "main")) {
5863 for (i
= 0, sym
= local_stack
; i
< 2; i
++, sym
= sym
->prev
) {
5864 if (sym
->v
& SYM_FIELD
|| sym
->prev
->v
& SYM_FIELD
)
5866 vpush_global_sym(&func_old_type
, TOK___bound_main_arg
);
5867 vset(&sym
->type
, sym
->r
, sym
->c
);
5873 block(NULL
, NULL
, NULL
, NULL
, 0, 0);
5876 cur_text_section
->data_offset
= ind
;
5877 label_pop(&global_label_stack
, NULL
);
5878 /* reset local stack */
5879 scope_stack_bottom
= NULL
;
5880 sym_pop(&local_stack
, NULL
);
5881 /* end of function */
5882 /* patch symbol size */
5883 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
5885 /* patch symbol weakness (this definition overrules any prototype) */
5886 if (sym
->type
.t
& VT_WEAK
)
5888 apply_visibility(sym
, &sym
->type
);
5889 if (tcc_state
->do_debug
) {
5890 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
5892 /* It's better to crash than to generate wrong code */
5893 cur_text_section
= NULL
;
5894 funcname
= ""; /* for safety */
5895 func_vt
.t
= VT_VOID
; /* for safety */
5896 func_var
= 0; /* for safety */
5897 ind
= 0; /* for safety */
5898 nocode_wanted
= saved_nocode_wanted
;
5901 ST_FUNC
void gen_inline_functions(void)
5904 int *str
, inline_generated
, i
;
5905 struct InlineFunc
*fn
;
5907 /* iterate while inline function are referenced */
5909 inline_generated
= 0;
5910 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5911 fn
= tcc_state
->inline_fns
[i
];
5913 if (sym
&& sym
->c
) {
5914 /* the function was used: generate its code and
5915 convert it to a normal function */
5916 str
= fn
->token_str
;
5919 pstrcpy(file
->filename
, sizeof file
->filename
, fn
->filename
);
5920 sym
->r
= VT_SYM
| VT_CONST
;
5921 sym
->type
.t
&= ~VT_INLINE
;
5925 cur_text_section
= text_section
;
5927 macro_ptr
= NULL
; /* fail safe */
5929 inline_generated
= 1;
5932 if (!inline_generated
)
5935 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5936 fn
= tcc_state
->inline_fns
[i
];
5937 str
= fn
->token_str
;
5940 dynarray_reset(&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
);
5943 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
5944 static int decl0(int l
, int is_for_loop_init
)
5952 if (!parse_btype(&btype
, &ad
)) {
5953 if (is_for_loop_init
)
5955 /* skip redundant ';' */
5956 /* XXX: find more elegant solution */
5961 if (l
== VT_CONST
&&
5962 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5963 /* global asm block */
5967 /* special test for old K&R protos without explicit int
5968 type. Only accepted when defining global data */
5969 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
5973 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5974 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5976 /* we accept no variable after */
5980 while (1) { /* iterate thru each declaration */
5981 char *asm_label
; // associated asm label
5983 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5987 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
5988 printf("type = '%s'\n", buf
);
5991 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5992 if ((type
.t
& VT_STATIC
) && (l
== VT_LOCAL
)) {
5993 tcc_error("function without file scope cannot be static");
5995 /* if old style function prototype, we accept a
5998 if (sym
->c
== FUNC_OLD
)
5999 func_decl_list(sym
);
6003 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
6006 asm_label_instr(&astr
);
6007 asm_label
= tcc_strdup(astr
.data
);
6010 /* parse one last attribute list, after asm label */
6011 parse_attribute(&ad
);
6016 #ifdef TCC_TARGET_PE
6017 if (ad
.a
.func_import
)
6018 type
.t
|= VT_IMPORT
;
6019 if (ad
.a
.func_export
)
6020 type
.t
|= VT_EXPORT
;
6022 type
.t
|= ad
.a
.visibility
<< VT_VIS_SHIFT
;
6026 tcc_error("cannot use local functions");
6027 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
6028 expect("function definition");
6030 /* reject abstract declarators in function definition */
6032 while ((sym
= sym
->next
) != NULL
)
6033 if (!(sym
->v
& ~SYM_FIELD
))
6034 expect("identifier");
6036 /* XXX: cannot do better now: convert extern line to static inline */
6037 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
6038 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
6043 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
6046 ref
= sym
->type
.ref
;
6047 if (0 == ref
->a
.func_proto
)
6048 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
6050 /* use func_call from prototype if not defined */
6051 if (ref
->a
.func_call
!= FUNC_CDECL
6052 && type
.ref
->a
.func_call
== FUNC_CDECL
)
6053 type
.ref
->a
.func_call
= ref
->a
.func_call
;
6055 /* use export from prototype */
6056 if (ref
->a
.func_export
)
6057 type
.ref
->a
.func_export
= 1;
6059 /* use static from prototype */
6060 if (sym
->type
.t
& VT_STATIC
)
6061 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
6063 /* If the definition has no visibility use the
6064 one from prototype. */
6065 if (! (type
.t
& VT_VIS_MASK
))
6066 type
.t
|= sym
->type
.t
& VT_VIS_MASK
;
6068 if (!is_compatible_types(&sym
->type
, &type
)) {
6070 tcc_error("incompatible types for redefinition of '%s'",
6071 get_tok_str(v
, NULL
));
6073 type
.ref
->a
.func_proto
= 0;
6074 /* if symbol is already defined, then put complete type */
6077 /* put function symbol */
6078 sym
= global_identifier_push(v
, type
.t
, 0);
6079 sym
->type
.ref
= type
.ref
;
6082 /* static inline functions are just recorded as a kind
6083 of macro. Their code will be emitted at the end of
6084 the compilation unit only if they are used */
6085 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
6086 (VT_INLINE
| VT_STATIC
)) {
6087 TokenString func_str
;
6089 struct InlineFunc
*fn
;
6090 const char *filename
;
6092 tok_str_new(&func_str
);
6098 tcc_error("unexpected end of file");
6099 tok_str_add_tok(&func_str
);
6104 } else if (t
== '}') {
6106 if (block_level
== 0)
6110 tok_str_add(&func_str
, -1);
6111 tok_str_add(&func_str
, 0);
6112 filename
= file
? file
->filename
: "";
6113 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
6114 strcpy(fn
->filename
, filename
);
6116 fn
->token_str
= func_str
.str
;
6117 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
6120 /* compute text section */
6121 cur_text_section
= ad
.section
;
6122 if (!cur_text_section
)
6123 cur_text_section
= text_section
;
6124 sym
->r
= VT_SYM
| VT_CONST
;
6129 if (btype
.t
& VT_TYPEDEF
) {
6130 /* save typedefed type */
6131 /* XXX: test storage specifiers ? */
6132 sym
= sym_push(v
, &type
, 0, 0);
6134 sym
->type
.t
|= VT_TYPEDEF
;
6137 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
6138 /* external function definition */
6139 /* specific case for func_call attribute */
6140 ad
.a
.func_proto
= 1;
6142 } else if (!(type
.t
& VT_ARRAY
)) {
6143 /* not lvalue if array */
6144 r
|= lvalue_type(type
.t
);
6146 has_init
= (tok
== '=');
6147 if (has_init
&& (type
.t
& VT_VLA
))
6148 tcc_error("Variable length array cannot be initialized");
6149 if ((btype
.t
& VT_EXTERN
) || ((type
.t
& VT_BTYPE
) == VT_FUNC
) ||
6150 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
6151 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
6152 /* external variable or function */
6153 /* NOTE: as GCC, uninitialized global static
6154 arrays of null size are considered as
6156 sym
= external_sym(v
, &type
, r
, asm_label
);
6158 if (ad
.alias_target
) {
6163 alias_target
= sym_find(ad
.alias_target
);
6164 if (!alias_target
|| !alias_target
->c
)
6165 tcc_error("unsupported forward __alias__ attribute");
6166 esym
= &((Elf32_Sym
*)symtab_section
->data
)[alias_target
->c
];
6167 tsec
.sh_num
= esym
->st_shndx
;
6168 put_extern_sym2(sym
, &tsec
, esym
->st_value
, esym
->st_size
, 0);
6171 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
6172 if (type
.t
& VT_STATIC
)
6178 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, asm_label
, l
);
6182 if (is_for_loop_init
)
6195 ST_FUNC
void decl(int l
)