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
, ex_rc
;
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 ST_FUNC
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 ST_FUNC
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 */
572 for(p
= vstack
; p
<= vtop
; p
++) {
573 #ifdef TCC_TARGET_X86_64
574 if ((p
->r
& VT_VALMASK
) == r
||
575 ((((p
->type
.t
& VT_BTYPE
) == VT_QLONG
) || ((p
->type
.t
& VT_BTYPE
) == VT_QFLOAT
)) &&
576 ((p
->r2
& VT_VALMASK
) == r
)))
578 if ((p
->r
& VT_VALMASK
) == r
|| ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
))
581 /* must save value on stack if not already done */
583 /* NOTE: must reload 'r' because r might be equal to r2 */
584 r
= p
->r
& VT_VALMASK
;
585 /* store register in the stack */
587 if((type
->t
& VT_BTYPE
) == VT_STRUCT
){
590 gfunc_sret(type
, func_var
, &ret
.type
, &ret_align
);
593 if ((p
->r
& VT_LVAL
) || ((type
->t
& VT_BTYPE
) == VT_FUNC
))
594 #ifdef TCC_TARGET_X86_64
595 type
= &char_pointer_type
;
599 size
= type_size(type
, &align
);
600 l
= loc_stack(size
, 1);
601 sv
.r
= VT_LOCAL
| VT_LVAL
;
603 #ifdef TCC_TARGET_X86_64
604 if (((type
->t
& VT_BTYPE
) == VT_QLONG
) || ((type
->t
& VT_BTYPE
) == VT_QFLOAT
))
606 if ((type
->t
& VT_BTYPE
) == VT_LLONG
)
609 #ifdef TCC_TARGET_X86_64
610 int load_size
= 8, load_type
= ((type
->t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
612 int load_size
= 4, load_type
= VT_INT
;
614 sv
.type
.t
= load_type
;
616 sv
.c
.ul
+= load_size
;
622 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
623 /* x86 specific: need to pop fp register ST0 if saved */
625 o(0xd8dd); /* fstp %st(0) */
630 /* mark that stack entry as being saved on the stack */
631 if (p
->r
& VT_LVAL
) {
632 /* also clear the bounded flag because the
633 relocation address of the function was stored in
635 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
637 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
| VT_TMP
;
645 #ifdef TCC_TARGET_ARM
646 /* find a register of class 'rc2' with at most one reference on stack.
647 * If none, call get_reg(rc) */
648 ST_FUNC
int get_reg_ex(int rc
, int rc2
)
653 for(r
=0;r
<NB_REGS
;r
++) {
654 if (reg_classes
[r
] & rc2
) {
657 for(p
= vstack
; p
<= vtop
; p
++) {
658 if ((p
->r
& VT_VALMASK
) == r
||
659 (p
->r2
& VT_VALMASK
) == r
)
670 static int for_reg(int rc
)
675 for(r
= 0; r
< NB_REGS
; r
++) {
676 if (reg_classes
[r
] & rc
) {
677 for(p
= vstack
; p
<= vtop
; p
++) {
678 if ((p
->r
& VT_VALMASK
) == r
|| (p
->r2
& VT_VALMASK
) == r
)
691 /* find a free register of class 'rc'. If none, save one register */
697 /* find a free register */
701 /* no register left : free the first one on the stack (VERY
702 IMPORTANT to start from the bottom to ensure that we don't
703 spill registers used in gen_opi()) */
704 for(p
= vstack
; p
<= vtop
; p
++) {
705 /* look at second register (if long long) */
708 r
= p
->r2
& VT_VALMASK
;
709 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
711 r
= p
->r
& VT_VALMASK
;
712 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
718 /* Should never comes here */
723 /* save registers up to (vtop - n) stack entry */
724 ST_FUNC
void save_regs(int n
)
729 for(p
= vstack
;p
<= p1
; p
++) {
730 r
= p
->r
& VT_VALMASK
;
737 /* move register 's' (of type 't') to 'r', and flush previous value of r to memory
739 static void move_reg(int r
, int s
, int t
)
753 /* get address of vtop (vtop MUST BE an lvalue) */
754 ST_FUNC
void gaddrof(void)
756 if (vtop
->r
& VT_REF
)
759 /* tricky: if saved lvalue, then we can go back to lvalue */
760 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
761 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
| VT_TMP
;
764 #ifdef CONFIG_TCC_BCHECK
765 /* generate lvalue bound code */
766 static void gbound(void)
771 vtop
->r
&= ~VT_MUSTBOUND
;
772 /* if lvalue, then use checking code before dereferencing */
773 if (vtop
->r
& VT_LVAL
) {
774 /* if not VT_BOUNDED value, then make one */
775 if (!(vtop
->r
& VT_BOUNDED
)) {
776 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
777 /* must save type because we must set it to int to get pointer */
779 vtop
->type
.t
= VT_INT
;
782 gen_bounded_ptr_add();
783 vtop
->r
|= lval_type
;
786 /* then check for dereferencing */
787 gen_bounded_ptr_deref();
792 /* store vtop a register belonging to class 'rc'. lvalues are
793 converted to values. Cannot be used if cannot be converted to
794 register value (such as structures). */
795 ST_FUNC
int gv(int rc
)
797 int r
, bit_pos
, bit_size
, size
, align
, i
, ft
, sbt
;
802 /* NOTE: get_reg can modify vstack[] */
803 if (ft
& VT_BITFIELD
) {
806 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
807 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
808 /* remove bit field info to avoid loops */
809 ft
= vtop
->type
.t
&= ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
810 /* cast to int to propagate signedness in following ops */
811 if (sbt
== VT_LLONG
) {
818 if((ft
& VT_UNSIGNED
) || sbt
== VT_BOOL
)
819 type
.t
|= VT_UNSIGNED
;
821 /* generate shifts */
822 vpushi(bits
- (bit_pos
+ bit_size
));
824 vpushi(bits
- bit_size
);
825 /* NOTE: transformed to SHR if unsigned */
829 if (is_float(vtop
->type
.t
) &&
830 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
833 unsigned long offset
;
834 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
838 /* XXX: unify with initializers handling ? */
839 /* CPUs usually cannot use float constants, so we store them
840 generically in data segment */
841 size
= type_size(&vtop
->type
, &align
);
842 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
843 data_section
->data_offset
= offset
;
844 /* XXX: not portable yet */
845 #if defined(__i386__) || defined(__x86_64__)
846 /* Zero pad x87 tenbyte long doubles */
847 if (size
== LDOUBLE_SIZE
) {
848 vtop
->c
.tab
[2] &= 0xffff;
849 #if LDOUBLE_SIZE == 16
854 ptr
= section_ptr_add(data_section
, size
);
856 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
860 ptr
[i
] = vtop
->c
.tab
[size
-1-i
];
864 ptr
[i
] = vtop
->c
.tab
[i
];
865 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
866 vtop
->r
|= VT_LVAL
| VT_SYM
;
868 vtop
->c
.ptr_offset
= 0;
870 #ifdef CONFIG_TCC_BCHECK
871 if (vtop
->r
& VT_MUSTBOUND
)
875 r
= vtop
->r
& VT_VALMASK
;
879 rc2
= (rc
& RC_FLOAT
) ? RC_FLOAT
: RC_INT
;
881 /* need to reload if:
883 - lvalue (need to dereference pointer)
884 - already a register, but not in the right class */
885 if (r
>= VT_CONST
|| (vtop
->r
& VT_LVAL
) || !(reg_classes
[r
] & rc
)
886 #ifdef TCC_TARGET_X86_64
887 || (sbt
== VT_QLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
888 || (sbt
== VT_QFLOAT
&& !(reg_classes
[vtop
->r2
] & rc2
))
890 || (sbt
== VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
895 #ifdef TCC_TARGET_X86_64
896 if ((sbt
== VT_QLONG
) || (sbt
== VT_QFLOAT
))
901 #ifdef TCC_TARGET_X86_64
902 int load_size
= 8, load_type
= (sbt
== VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
904 int load_size
= 4, load_type
= VT_INT
;
905 unsigned long long ll
;
908 /* two register type load : expand to two words
910 #ifndef TCC_TARGET_X86_64
911 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
914 vtop
->c
.ui
= ll
; /* first word */
916 vtop
->r
= r
; /* save register value */
917 vpushi(ll
>> 32); /* second word */
920 /* XXX: test to VT_CONST incorrect ? */
921 if (r
>= VT_CONST
|| (vtop
->r
& VT_LVAL
)) {
922 /* We do not want to modifier the long long
923 pointer here, so the safest (and less
924 efficient) is to save all the other registers
925 in the regs. use VT_TMP XXX: totally inefficient. */
926 /* load from memory */
927 vtop
->type
.t
= load_type
;
930 vtop
[-1].r
= r
| VT_TMP
; /* lock register value */
931 /* increment pointer to get second word */
932 vtop
->type
= char_pointer_type
;
937 vtop
->type
.t
= load_type
;
942 vtop
[-1].r
= r
| VT_TMP
; /* lock register value */
943 vtop
->r
= vtop
[-1].r2
;
945 /* Allocate second register. Here we rely on the fact that
946 get_reg() tries first to free r2 of an SValue. */
950 /* write second register */
954 } else if ((vtop
->r
& VT_LVAL
) && !is_float(ft
)) {
956 /* lvalue of scalar type : need to use lvalue type
957 because of possible cast */
959 /* compute memory access type */
960 if (vtop
->r
& VT_REF
)
961 #ifdef TCC_TARGET_X86_64
966 else if (vtop
->r
& VT_LVAL_BYTE
)
968 else if (vtop
->r
& VT_LVAL_SHORT
)
970 if (vtop
->r
& VT_LVAL_UNSIGNED
)
974 /* restore wanted type */
977 /* one register type load */
981 vtop
->c
.ptr_offset
= 0;
983 #ifdef TCC_TARGET_C67
984 /* uses register pairs for doubles */
985 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
992 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
993 ST_FUNC
void gv2(int rc1
, int rc2
)
995 /* generate more generic register first. But VT_JMP or VT_CMP
996 values must be generated first in all cases to avoid possible
1003 /* test if reload is needed for first register */
1004 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
1014 /* test if reload is needed for first register */
1015 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
1021 /* wrapper around RC_FRET to return a register by type */
1022 static int rc_fret(int t
)
1024 #ifdef TCC_TARGET_X86_64
1025 if (t
== VT_LDOUBLE
) {
1033 /* wrapper around REG_FRET to return a register by type */
1034 static int reg_fret(int t
)
1036 #ifdef TCC_TARGET_X86_64
1037 if (t
== VT_LDOUBLE
) {
1044 /* expand long long on stack in two int registers */
1045 static void lexpand(void)
1049 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
1052 vtop
[0].r
= vtop
[-1].r2
;
1053 vtop
[0].r2
= VT_CONST
;
1054 vtop
[-1].r2
= VT_CONST
;
1055 vtop
[0].type
.t
= VT_INT
| u
;
1056 vtop
[-1].type
.t
= VT_INT
| u
;
1059 #ifdef TCC_TARGET_ARM
1060 /* expand long long on stack */
1061 ST_FUNC
void lexpand_nr(void)
1065 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
1067 vtop
->r2
= VT_CONST
;
1068 vtop
->type
.t
= VT_INT
| u
;
1069 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
1070 if (v
== VT_CONST
) {
1071 vtop
[-1].c
.ui
= vtop
->c
.ull
;
1072 vtop
->c
.ui
= vtop
->c
.ull
>> 32;
1074 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
1076 vtop
->r
= vtop
[-1].r
;
1077 } else if (v
> VT_CONST
) {
1081 vtop
->r
= vtop
[-1].r2
;
1082 vtop
[-1].r2
= VT_CONST
;
1083 vtop
[-1].type
.t
= VT_INT
| u
;
1087 #ifndef TCC_TARGET_X86_64
1088 /* build a long long from two ints */
1089 static void lbuild(int t
)
1091 gv2(RC_INT
, RC_INT
);
1092 vtop
[-1].r2
= vtop
[0].r
;
1093 vtop
[-1].type
.t
= t
;
1098 /* rotate n first stack elements to the bottom
1099 I1 ... In -> I2 ... In I1 [top is right]
1101 ST_FUNC
void vrotb(int n
)
1107 for(i
=-n
+1;i
!=0;i
++)
1108 vtop
[i
] = vtop
[i
+1];
1112 /* rotate the n elements before entry e towards the top
1113 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
1115 ST_FUNC
void vrote(SValue
*e
, int n
)
1121 for(i
= 0;i
< n
- 1; i
++)
1126 /* rotate n first stack elements to the top
1127 I1 ... In -> In I1 ... I(n-1) [top is right]
1129 ST_FUNC
void vrott(int n
)
1134 /* pop stack value */
1135 ST_FUNC
void vpop(void)
1139 v
= fr
& VT_VALMASK
;
1140 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1141 /* for x86, we need to pop the FP stack */
1142 if (v
== TREG_ST0
&& !nocode_wanted
) {
1143 o(0xd8dd); /* fstp %st(0) */
1146 if (v
== VT_JMP
|| v
== VT_JMPI
) {
1147 /* need to put correct jump if && or || without test */
1153 if((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
1156 gfunc_sret(&vtop
->type
, func_var
, &ret
.type
, &align
);
1157 size
= type_size(&ret
.type
, &align
);
1164 /* convert stack entry to register and duplicate its value in another
1166 static void gv_dup(void)
1171 #ifndef TCC_TARGET_X86_64
1172 if ((t
& VT_BTYPE
) == VT_LLONG
) {
1179 /* stack: H L L1 H1 */
1188 /* duplicate value */
1191 #ifdef TCC_TARGET_X86_64
1192 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1203 load(r1
, &sv
); /* move r to r1 */
1205 /* duplicates value */
1210 #ifndef TCC_TARGET_X86_64
1211 /* generate CPU independent (unsigned) long long operations */
1212 static void gen_opl(int op
)
1214 int t
, a
, b
, op1
, c
, i
;
1216 unsigned short reg_iret
= REG_IRET
;
1217 unsigned short reg_lret
= REG_LRET
;
1223 func
= TOK___divdi3
;
1226 func
= TOK___udivdi3
;
1229 func
= TOK___moddi3
;
1232 func
= TOK___umoddi3
;
1239 /* call generic long long function */
1240 vpush_global_sym(&func_old_type
, func
);
1245 vtop
->r2
= reg_lret
;
1258 /* stack: L1 H1 L2 H2 */
1263 vtop
[-2] = vtop
[-3];
1266 /* stack: H1 H2 L1 L2 */
1272 /* stack: H1 H2 L1 L2 ML MH */
1275 /* stack: ML MH H1 H2 L1 L2 */
1279 /* stack: ML MH H1 L2 H2 L1 */
1284 /* stack: ML MH M1 M2 */
1287 } else if (op
== '+' || op
== '-') {
1288 /* XXX: add non carry method too (for MIPS or alpha) */
1294 /* stack: H1 H2 (L1 op L2) */
1297 gen_op(op1
+ 1); /* TOK_xxxC2 */
1300 /* stack: H1 H2 (L1 op L2) */
1303 /* stack: (L1 op L2) H1 H2 */
1305 /* stack: (L1 op L2) (H1 op H2) */
1313 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1314 t
= vtop
[-1].type
.t
;
1318 /* stack: L H shift */
1320 /* constant: simpler */
1321 /* NOTE: all comments are for SHL. the other cases are
1322 done by swaping words */
1333 if (op
!= TOK_SAR
) {
1366 /* XXX: should provide a faster fallback on x86 ? */
1369 func
= TOK___ashrdi3
;
1372 func
= TOK___lshrdi3
;
1375 func
= TOK___ashldi3
;
1381 /* compare operations */
1387 /* stack: L1 H1 L2 H2 */
1389 vtop
[-1] = vtop
[-2];
1391 /* stack: L1 L2 H1 H2 */
1394 /* when values are equal, we need to compare low words. since
1395 the jump is inverted, we invert the test too. */
1398 else if (op1
== TOK_GT
)
1400 else if (op1
== TOK_ULT
)
1402 else if (op1
== TOK_UGT
)
1407 if (op1
!= TOK_NE
) {
1411 /* generate non equal test */
1412 /* XXX: NOT PORTABLE yet */
1416 #if defined(TCC_TARGET_I386)
1417 b
= psym(0x850f, 0);
1418 #elif defined(TCC_TARGET_ARM)
1420 o(0x1A000000 | encbranch(ind
, 0, 1));
1421 #elif defined(TCC_TARGET_C67)
1422 tcc_error("not implemented");
1424 #error not supported
1428 /* compare low. Always unsigned */
1432 else if (op1
== TOK_LE
)
1434 else if (op1
== TOK_GT
)
1436 else if (op1
== TOK_GE
)
1447 /* handle integer constant optimizations and various machine
1449 static void gen_opic(int op
)
1451 int c1
, c2
, t1
, t2
, n
;
1454 typedef unsigned long long U
;
1458 t1
= v1
->type
.t
& VT_BTYPE
;
1459 t2
= v2
->type
.t
& VT_BTYPE
;
1463 else if (v1
->type
.t
& VT_UNSIGNED
)
1470 else if (v2
->type
.t
& VT_UNSIGNED
)
1475 /* currently, we cannot do computations with forward symbols */
1476 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1477 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1480 case '+': l1
+= l2
; break;
1481 case '-': l1
-= l2
; break;
1482 case '&': l1
&= l2
; break;
1483 case '^': l1
^= l2
; break;
1484 case '|': l1
|= l2
; break;
1485 case '*': l1
*= l2
; break;
1492 /* if division by zero, generate explicit division */
1495 tcc_error("division by zero in constant");
1499 default: l1
/= l2
; break;
1500 case '%': l1
%= l2
; break;
1501 case TOK_UDIV
: l1
= (U
)l1
/ l2
; break;
1502 case TOK_UMOD
: l1
= (U
)l1
% l2
; break;
1505 case TOK_SHL
: l1
<<= l2
; break;
1506 case TOK_SHR
: l1
= (U
)l1
>> l2
; break;
1507 case TOK_SAR
: l1
>>= l2
; break;
1509 case TOK_ULT
: l1
= (U
)l1
< (U
)l2
; break;
1510 case TOK_UGE
: l1
= (U
)l1
>= (U
)l2
; break;
1511 case TOK_EQ
: l1
= l1
== l2
; break;
1512 case TOK_NE
: l1
= l1
!= l2
; break;
1513 case TOK_ULE
: l1
= (U
)l1
<= (U
)l2
; break;
1514 case TOK_UGT
: l1
= (U
)l1
> (U
)l2
; break;
1515 case TOK_LT
: l1
= l1
< l2
; break;
1516 case TOK_GE
: l1
= l1
>= l2
; break;
1517 case TOK_LE
: l1
= l1
<= l2
; break;
1518 case TOK_GT
: l1
= l1
> l2
; break;
1520 case TOK_LAND
: l1
= l1
&& l2
; break;
1521 case TOK_LOR
: l1
= l1
|| l2
; break;
1528 /* if commutative ops, put c2 as constant */
1529 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1530 op
== '|' || op
== '*')) {
1532 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1533 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1535 /* Filter out NOP operations like x*1, x-0, x&-1... */
1536 if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1539 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1540 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1546 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1547 /* try to use shifts instead of muls or divs */
1548 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1557 else if (op
== TOK_PDIV
)
1563 } else if (c2
&& (op
== '+' || op
== '-') &&
1564 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
1565 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1566 /* symbol + constant case */
1573 if (!nocode_wanted
) {
1574 /* call low level op generator */
1575 if (t1
== VT_LLONG
|| t2
== VT_LLONG
)
1586 /* generate a floating point operation with constant propagation */
1587 static void gen_opif(int op
)
1595 /* currently, we cannot do computations with forward symbols */
1596 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1597 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1599 if (v1
->type
.t
== VT_FLOAT
) {
1602 } else if (v1
->type
.t
== VT_DOUBLE
) {
1610 /* NOTE: we only do constant propagation if finite number (not
1611 NaN or infinity) (ANSI spec) */
1612 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1616 case '+': f1
+= f2
; break;
1617 case '-': f1
-= f2
; break;
1618 case '*': f1
*= f2
; break;
1622 tcc_error("division by zero in constant");
1627 /* XXX: also handles tests ? */
1631 /* XXX: overflow test ? */
1632 if (v1
->type
.t
== VT_FLOAT
) {
1634 } else if (v1
->type
.t
== VT_DOUBLE
) {
1642 if (!nocode_wanted
) {
1650 static int pointed_size(CType
*type
)
1653 return type_size(pointed_type(type
), &align
);
1656 static void vla_runtime_pointed_size(CType
*type
)
1659 vla_runtime_type_size(pointed_type(type
), &align
);
1662 static inline int is_null_pointer(SValue
*p
)
1664 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1666 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& p
->c
.i
== 0) ||
1667 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.ll
== 0) ||
1668 ((p
->type
.t
& VT_BTYPE
) == VT_PTR
&& p
->c
.ptr_offset
== 0);
1671 static inline int is_integer_btype(int bt
)
1673 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
1674 bt
== VT_INT
|| bt
== VT_LLONG
);
1677 /* check types for comparison or subtraction of pointers */
1678 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
1680 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
1683 /* null pointers are accepted for all comparisons as gcc */
1684 if (is_null_pointer(p1
) || is_null_pointer(p2
))
1688 bt1
= type1
->t
& VT_BTYPE
;
1689 bt2
= type2
->t
& VT_BTYPE
;
1690 /* accept comparison between pointer and integer with a warning */
1691 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
1692 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
1693 tcc_warning("comparison between pointer and integer");
1697 /* both must be pointers or implicit function pointers */
1698 if (bt1
== VT_PTR
) {
1699 type1
= pointed_type(type1
);
1700 } else if (bt1
!= VT_FUNC
)
1701 goto invalid_operands
;
1703 if (bt2
== VT_PTR
) {
1704 type2
= pointed_type(type2
);
1705 } else if (bt2
!= VT_FUNC
) {
1707 tcc_error("invalid operands to binary %s", get_tok_str(op
, NULL
));
1709 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1710 (type2
->t
& VT_BTYPE
) == VT_VOID
)
1714 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1715 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1716 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1717 /* gcc-like error if '-' is used */
1719 goto invalid_operands
;
1721 tcc_warning("comparison of distinct pointer types lacks a cast");
1725 /* generic gen_op: handles types problems */
1726 ST_FUNC
void gen_op(int op
)
1728 int u
, t1
, t2
, bt1
, bt2
, t
;
1731 t1
= vtop
[-1].type
.t
;
1732 t2
= vtop
[0].type
.t
;
1733 bt1
= t1
& VT_BTYPE
;
1734 bt2
= t2
& VT_BTYPE
;
1736 if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
1737 /* at least one operand is a pointer */
1738 /* relationnal op: must be both pointers */
1739 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
1740 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1741 /* pointers are handled are unsigned */
1742 #ifdef TCC_TARGET_X86_64
1743 t
= VT_LLONG
| VT_UNSIGNED
;
1745 t
= VT_INT
| VT_UNSIGNED
;
1749 /* if both pointers, then it must be the '-' op */
1750 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
1752 tcc_error("cannot use pointers here");
1753 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1754 /* XXX: check that types are compatible */
1755 if (vtop
[-1].type
.t
& VT_VLA
) {
1756 vla_runtime_pointed_size(&vtop
[-1].type
);
1758 vpushi(pointed_size(&vtop
[-1].type
));
1762 /* set to integer type */
1763 #ifdef TCC_TARGET_X86_64
1764 vtop
->type
.t
= VT_LLONG
;
1766 vtop
->type
.t
= VT_INT
;
1771 /* exactly one pointer : must be '+' or '-'. */
1772 if (op
!= '-' && op
!= '+')
1773 tcc_error("cannot use pointers here");
1774 /* Put pointer as first operand */
1775 if (bt2
== VT_PTR
) {
1779 type1
= vtop
[-1].type
;
1780 type1
.t
&= ~VT_ARRAY
;
1781 if (vtop
[-1].type
.t
& VT_VLA
)
1782 vla_runtime_pointed_size(&vtop
[-1].type
);
1784 u
= pointed_size(&vtop
[-1].type
);
1786 tcc_error("unknown array element size");
1787 #ifdef TCC_TARGET_X86_64
1790 /* XXX: cast to int ? (long long case) */
1795 #ifdef CONFIG_TCC_BCHECK
1796 /* if evaluating constant expression, no code should be
1797 generated, so no bound check */
1798 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
1799 /* if bounded pointers, we generate a special code to
1806 gen_bounded_ptr_add();
1812 /* put again type if gen_opic() swaped operands */
1815 } else if (is_float(bt1
) || is_float(bt2
)) {
1816 /* compute bigger type and do implicit casts */
1817 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
1819 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
1824 /* floats can only be used for a few operations */
1825 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
1826 (op
< TOK_ULT
|| op
> TOK_GT
))
1827 tcc_error("invalid operands for binary operation");
1829 } else if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
) {
1830 t
= bt1
== VT_LLONG
? VT_LLONG
: VT_INT
;
1831 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (t
| VT_UNSIGNED
))
1834 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
1835 /* cast to biggest op */
1837 /* convert to unsigned if it does not fit in a long long */
1838 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
1839 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
1842 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
1843 tcc_error("comparison of struct");
1845 /* integer operations */
1847 /* convert to unsigned if it does not fit in an integer */
1848 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
1849 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
1852 /* XXX: currently, some unsigned operations are explicit, so
1853 we modify them here */
1854 if (t
& VT_UNSIGNED
) {
1861 else if (op
== TOK_LT
)
1863 else if (op
== TOK_GT
)
1865 else if (op
== TOK_LE
)
1867 else if (op
== TOK_GE
)
1874 /* special case for shifts and long long: we keep the shift as
1876 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
1883 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
1884 /* relationnal op: the result is an int */
1885 vtop
->type
.t
= VT_INT
;
1892 #ifndef TCC_TARGET_ARM
1893 /* generic itof for unsigned long long case */
1894 static void gen_cvt_itof1(int t
)
1896 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
1897 (VT_LLONG
| VT_UNSIGNED
)) {
1900 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
1901 #if LDOUBLE_SIZE != 8
1902 else if (t
== VT_LDOUBLE
)
1903 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
1906 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
1910 vtop
->r
= reg_fret(t
);
1917 /* generic ftoi for unsigned long long case */
1918 static void gen_cvt_ftoi1(int t
)
1922 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
1923 /* not handled natively */
1924 st
= vtop
->type
.t
& VT_BTYPE
;
1926 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
1927 #if LDOUBLE_SIZE != 8
1928 else if (st
== VT_LDOUBLE
)
1929 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
1932 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
1937 vtop
->r2
= REG_LRET
;
1943 /* force char or short cast */
1944 static void force_charshort_cast(int t
)
1948 /* XXX: add optimization if lvalue : just change type and offset */
1953 if (t
& VT_UNSIGNED
) {
1954 vpushi((1 << bits
) - 1);
1960 /* result must be signed or the SAR is converted to an SHL
1961 This was not the case when "t" was a signed short
1962 and the last value on the stack was an unsigned int */
1963 vtop
->type
.t
&= ~VT_UNSIGNED
;
1969 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1970 static void gen_cast(CType
*type
)
1972 int sbt
, dbt
, sf
, df
, c
, p
;
1974 /* special delayed cast for char/short */
1975 /* XXX: in some cases (multiple cascaded casts), it may still
1977 if (vtop
->r
& VT_MUSTCAST
) {
1978 vtop
->r
&= ~VT_MUSTCAST
;
1979 force_charshort_cast(vtop
->type
.t
);
1982 /* bitfields first get cast to ints */
1983 if (vtop
->type
.t
& VT_BITFIELD
) {
1987 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
1988 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
1993 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1994 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
1996 /* constant case: we can do it now */
1997 /* XXX: in ISOC, cannot do it if error in convert */
1998 if (sbt
== VT_FLOAT
)
1999 vtop
->c
.ld
= vtop
->c
.f
;
2000 else if (sbt
== VT_DOUBLE
)
2001 vtop
->c
.ld
= vtop
->c
.d
;
2004 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
2005 if (sbt
& VT_UNSIGNED
)
2006 vtop
->c
.ld
= vtop
->c
.ull
;
2008 vtop
->c
.ld
= vtop
->c
.ll
;
2010 if (sbt
& VT_UNSIGNED
)
2011 vtop
->c
.ld
= vtop
->c
.ui
;
2013 vtop
->c
.ld
= vtop
->c
.i
;
2016 if (dbt
== VT_FLOAT
)
2017 vtop
->c
.f
= (float)vtop
->c
.ld
;
2018 else if (dbt
== VT_DOUBLE
)
2019 vtop
->c
.d
= (double)vtop
->c
.ld
;
2020 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
2021 vtop
->c
.ull
= (unsigned long long)vtop
->c
.ld
;
2022 } else if (sf
&& dbt
== VT_BOOL
) {
2023 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
2026 vtop
->c
.ll
= (long long)vtop
->c
.ld
;
2027 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
2028 vtop
->c
.ll
= vtop
->c
.ull
;
2029 else if (sbt
& VT_UNSIGNED
)
2030 vtop
->c
.ll
= vtop
->c
.ui
;
2031 #ifdef TCC_TARGET_X86_64
2032 else if (sbt
== VT_PTR
)
2035 else if (sbt
!= VT_LLONG
)
2036 vtop
->c
.ll
= vtop
->c
.i
;
2038 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
2039 vtop
->c
.ull
= vtop
->c
.ll
;
2040 else if (dbt
== VT_BOOL
)
2041 vtop
->c
.i
= (vtop
->c
.ll
!= 0);
2042 #ifdef TCC_TARGET_X86_64
2043 else if (dbt
== VT_PTR
)
2046 else if (dbt
!= VT_LLONG
) {
2048 if ((dbt
& VT_BTYPE
) == VT_BYTE
)
2050 else if ((dbt
& VT_BTYPE
) == VT_SHORT
)
2052 if(dbt
& VT_UNSIGNED
)
2053 vtop
->c
.ui
= ((unsigned int)vtop
->c
.ll
<< s
) >> s
;
2055 vtop
->c
.i
= ((int)vtop
->c
.ll
<< s
) >> s
;
2058 } else if (p
&& dbt
== VT_BOOL
) {
2061 } else if (!nocode_wanted
) {
2062 /* non constant case: generate code */
2064 /* convert from fp to fp */
2067 /* convert int to fp */
2070 /* convert fp to int */
2071 if (dbt
== VT_BOOL
) {
2075 /* we handle char/short/etc... with generic code */
2076 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
2077 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
2081 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
2082 /* additional cast for char/short... */
2087 #ifndef TCC_TARGET_X86_64
2088 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
2089 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
2090 /* scalar to long long */
2091 /* machine independent conversion */
2093 /* generate high word */
2094 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
2098 if (sbt
== VT_PTR
) {
2099 /* cast from pointer to int before we apply
2100 shift operation, which pointers don't support*/
2101 gen_cast(&int_type
);
2107 /* patch second register */
2108 vtop
[-1].r2
= vtop
->r
;
2112 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
2113 (dbt
& VT_BTYPE
) == VT_PTR
||
2114 (dbt
& VT_BTYPE
) == VT_FUNC
) {
2115 if ((sbt
& VT_BTYPE
) != VT_LLONG
&&
2116 (sbt
& VT_BTYPE
) != VT_PTR
&&
2117 (sbt
& VT_BTYPE
) != VT_FUNC
) {
2118 /* need to convert from 32bit to 64bit */
2120 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
2121 /* x86_64 specific: movslq */
2123 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
2127 } else if (dbt
== VT_BOOL
) {
2128 /* scalar to bool */
2131 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
2132 (dbt
& VT_BTYPE
) == VT_SHORT
) {
2133 if (sbt
== VT_PTR
) {
2134 vtop
->type
.t
= VT_INT
;
2135 tcc_warning("nonportable conversion from pointer to char/short");
2137 force_charshort_cast(dbt
);
2138 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
2140 if (sbt
== VT_LLONG
) {
2141 /* from long long: just take low order word */
2145 /* if lvalue and single word type, nothing to do because
2146 the lvalue already contains the real type size (see
2147 VT_LVAL_xxx constants) */
2150 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
2151 /* if we are casting between pointer types,
2152 we must update the VT_LVAL_xxx size */
2153 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
2154 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
2159 /* return type size as known at compile time. Put alignment at 'a' */
2160 ST_FUNC
int type_size(CType
*type
, int *a
)
2166 bt
= type
->t
& VT_BTYPE
;
2167 if (bt
== VT_STRUCT
) {
2168 assert(!(type
->t
& VT_VLS
));
2173 } else if (bt
== VT_PTR
) {
2174 if (type
->t
& VT_ARRAY
) {
2177 ts
= type_size(&s
->type
, a
);
2178 if (ts
< 0 && s
->c
< 0)
2180 size
= (size_t)ts
* s
->c
;
2185 } else if (bt
== VT_LDOUBLE
) {
2187 size
= LDOUBLE_SIZE
;
2188 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
2189 #ifdef TCC_TARGET_I386
2190 #ifdef TCC_TARGET_PE
2195 #elif defined(TCC_TARGET_ARM)
2205 } else if (bt
== VT_INT
|| bt
== VT_ENUM
|| bt
== VT_FLOAT
) {
2208 } else if (bt
== VT_SHORT
) {
2211 } else if (bt
== VT_QLONG
|| bt
== VT_QFLOAT
) {
2215 /* char, void, function, _Bool */
2219 assert(size
== (int)size
);
2223 /* push type size as known at runtime time on top of value stack. Put
2225 ST_FUNC
void vla_runtime_type_size(CType
*type
, int *a
)
2227 if (type
->t
& VT_VLA
) {
2228 vset(&int_type
, VT_LOCAL
|VT_LVAL
, type
->ref
->c
);
2230 vpushi(type_size(type
, a
));
2234 static void vla_sp_save(void) {
2235 if (!(vla_flags
& VLA_SP_LOC_SET
)) {
2236 *vla_sp_loc
= loc_stack(PTR_SIZE
, 1);
2237 vla_flags
|= VLA_SP_LOC_SET
;
2239 if (!(vla_flags
& VLA_SP_SAVED
)) {
2240 gen_vla_sp_save(*vla_sp_loc
);
2241 vla_flags
|= VLA_SP_SAVED
;
2245 /* return the pointed type of t */
2246 static inline CType
*pointed_type(CType
*type
)
2248 return &type
->ref
->type
;
2251 /* modify type so that its it is a pointer to type. */
2252 ST_FUNC
void mk_pointer(CType
*type
)
2255 s
= sym_push(SYM_FIELD
, type
, 0, -1);
2256 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
2260 /* compare function types. OLD functions match any new functions */
2261 static int is_compatible_func(CType
*type1
, CType
*type2
)
2267 if (!is_compatible_types(&s1
->type
, &s2
->type
))
2269 /* check func_call */
2270 if (s1
->a
.func_call
!= s2
->a
.func_call
)
2272 /* XXX: not complete */
2273 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
2277 while (s1
!= NULL
) {
2280 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
2290 /* return true if type1 and type2 are the same. If unqualified is
2291 true, qualifiers on the types are ignored.
2293 - enums are not checked as gcc __builtin_types_compatible_p ()
2295 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2299 t1
= type1
->t
& VT_TYPE
;
2300 t2
= type2
->t
& VT_TYPE
;
2302 /* strip qualifiers before comparing */
2303 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2304 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2306 /* Default Vs explicit signedness only matters for char */
2307 if ((t1
& VT_BTYPE
) != VT_BYTE
) {
2311 /* XXX: bitfields ? */
2314 /* test more complicated cases */
2315 bt1
= t1
& VT_BTYPE
;
2316 if (bt1
== VT_PTR
) {
2317 type1
= pointed_type(type1
);
2318 type2
= pointed_type(type2
);
2319 return is_compatible_types(type1
, type2
);
2320 } else if (bt1
== VT_STRUCT
) {
2321 return (type1
->ref
== type2
->ref
);
2322 } else if (bt1
== VT_FUNC
) {
2323 return is_compatible_func(type1
, type2
);
2329 /* return true if type1 and type2 are exactly the same (including
2332 static int is_compatible_types(CType
*type1
, CType
*type2
)
2334 return compare_types(type1
,type2
,0);
2337 /* return true if type1 and type2 are the same (ignoring qualifiers).
2339 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
2341 return compare_types(type1
,type2
,1);
2344 /* print a type. If 'varstr' is not NULL, then the variable is also
2345 printed in the type */
2347 /* XXX: add array and function pointers */
2348 static void type_to_str(char *buf
, int buf_size
,
2349 CType
*type
, const char *varstr
)
2356 t
= type
->t
& VT_TYPE
;
2359 if (t
& VT_CONSTANT
)
2360 pstrcat(buf
, buf_size
, "const ");
2361 if (t
& VT_VOLATILE
)
2362 pstrcat(buf
, buf_size
, "volatile ");
2363 if ((t
& (VT_DEFSIGN
| VT_UNSIGNED
)) == (VT_DEFSIGN
| VT_UNSIGNED
))
2364 pstrcat(buf
, buf_size
, "unsigned ");
2365 else if (t
& VT_DEFSIGN
)
2366 pstrcat(buf
, buf_size
, "signed ");
2396 tstr
= "long double";
2398 pstrcat(buf
, buf_size
, tstr
);
2402 if (bt
== VT_STRUCT
)
2406 pstrcat(buf
, buf_size
, tstr
);
2407 v
= type
->ref
->v
& ~SYM_STRUCT
;
2408 if (v
>= SYM_FIRST_ANOM
)
2409 pstrcat(buf
, buf_size
, "<anonymous>");
2411 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
2415 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
2416 pstrcat(buf
, buf_size
, "(");
2418 while (sa
!= NULL
) {
2419 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
2420 pstrcat(buf
, buf_size
, buf1
);
2423 pstrcat(buf
, buf_size
, ", ");
2425 pstrcat(buf
, buf_size
, ")");
2429 pstrcpy(buf1
, sizeof(buf1
), "*");
2431 pstrcat(buf1
, sizeof(buf1
), varstr
);
2432 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2436 pstrcat(buf
, buf_size
, " ");
2437 pstrcat(buf
, buf_size
, varstr
);
2442 /* verify type compatibility to store vtop in 'dt' type, and generate
2444 static void gen_assign_cast(CType
*dt
)
2446 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
2447 char buf1
[256], buf2
[256];
2450 st
= &vtop
->type
; /* source type */
2451 dbt
= dt
->t
& VT_BTYPE
;
2452 sbt
= st
->t
& VT_BTYPE
;
2453 if (sbt
== VT_VOID
|| dbt
== VT_VOID
)
2454 tcc_error("cannot cast from/to void");
2455 if (dt
->t
& VT_CONSTANT
)
2456 tcc_warning("assignment of read-only location");
2459 /* special cases for pointers */
2460 /* '0' can also be a pointer */
2461 if (is_null_pointer(vtop
))
2463 /* accept implicit pointer to integer cast with warning */
2464 if (is_integer_btype(sbt
)) {
2465 tcc_warning("assignment makes pointer from integer without a cast");
2468 type1
= pointed_type(dt
);
2469 /* a function is implicitely a function pointer */
2470 if (sbt
== VT_FUNC
) {
2471 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
2472 !is_compatible_types(pointed_type(dt
), st
))
2473 tcc_warning("assignment from incompatible pointer type");
2478 type2
= pointed_type(st
);
2479 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2480 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
2481 /* void * can match anything */
2483 /* exact type match, except for unsigned */
2486 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
|
2488 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
|
2490 if (!is_compatible_types(&tmp_type1
, &tmp_type2
))
2491 tcc_warning("assignment from incompatible pointer type");
2493 /* check const and volatile */
2494 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
2495 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
2496 tcc_warning("assignment discards qualifiers from pointer target type");
2502 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
2503 tcc_warning("assignment makes integer from pointer without a cast");
2505 if (sbt
== VT_STRUCT
)
2507 /* XXX: more tests */
2512 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2513 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2514 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2516 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
2517 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
2518 tcc_error("cannot cast '%s' to '%s'", buf1
, buf2
);
2526 static void vstore_im(){
2527 int rc
, ft
, sbt
, dbt
, t
, r
;
2528 ft
= vtop
[-1].type
.t
;
2529 sbt
= vtop
->type
.t
& VT_BTYPE
;
2530 dbt
= ft
& VT_BTYPE
;
2533 #ifdef TCC_TARGET_X86_64
2534 if (dbt
== VT_LDOUBLE
) {
2540 r
= gv(rc
); /* generate value */
2541 /* if lvalue was saved on stack, must read it */
2542 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
2544 t
= get_reg(RC_INT
);
2545 #ifdef TCC_TARGET_X86_64
2550 sv
.r
= VT_LOCAL
| VT_LVAL
| VT_TMP
;
2551 sv
.c
.ul
= vtop
[-1].c
.ul
;
2553 vtop
[-1].r
= t
| VT_LVAL
;
2556 /* two word case handling : store second register at word + 4 */
2557 #ifdef TCC_TARGET_X86_64
2558 if ((dbt
== VT_QLONG
) || (dbt
== VT_QFLOAT
))
2560 if (dbt
== VT_LLONG
)
2563 #ifdef TCC_TARGET_X86_64
2564 int load_size
= 8, load_type
= (sbt
== VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
2566 int load_size
= 4, load_type
= VT_INT
;
2568 vtop
[-1].type
.t
= load_type
;
2571 /* convert to int to increment easily */
2572 vtop
->type
= char_pointer_type
;
2578 vtop
[-1].type
.t
= load_type
;
2579 /* XXX: it works because r2 is spilled last ! */
2580 store(vtop
->r2
, vtop
- 1);
2582 vtop
[-1].type
.t
= ft
;
2588 /* store vtop in lvalue pushed on stack */
2589 ST_FUNC
void vstore(void)
2591 int sbt
, dbt
, ft
, size
, align
, bit_size
, bit_pos
, delayed_cast
;
2593 ft
= vtop
[-1].type
.t
;
2594 sbt
= vtop
->type
.t
& VT_BTYPE
;
2595 dbt
= ft
& VT_BTYPE
;
2596 if ((((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2597 (sbt
== VT_INT
&& dbt
== VT_SHORT
)) && !(vtop
->type
.t
& VT_BITFIELD
)) {
2598 /* optimize char/short casts */
2599 delayed_cast
= VT_MUSTCAST
;
2600 vtop
->type
.t
= ft
& (VT_TYPE
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
)));
2601 /* XXX: factorize */
2602 if (ft
& VT_CONSTANT
)
2603 tcc_warning("assignment of read-only location");
2606 if (!(ft
& VT_BITFIELD
))
2607 gen_assign_cast(&vtop
[-1].type
);
2610 if (sbt
== VT_STRUCT
) {
2611 /* if structure, only generate pointer */
2612 /* structure assignment : generate memcpy */
2613 /* XXX: optimize if small size */
2614 if (!nocode_wanted
) {
2616 int ret_nregs
, ret_align
;
2617 ret_nregs
= gfunc_sret(&vtop
->type
, func_var
, &ret
.type
, &ret_align
);
2621 vtop
[0].type
= ret
.type
;
2622 vtop
[-1].type
= ret
.type
;
2626 size
= type_size(&vtop
->type
, &align
);
2629 vtop
->type
.t
= VT_PTR
;
2632 /* address of memcpy() */
2635 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2636 else if(!(align
& 3))
2637 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2640 vpush_global_sym(&func_old_type
, TOK_memcpy
);
2645 vtop
->type
.t
= VT_PTR
;
2655 /* leave source on stack */
2656 } else if (ft
& VT_BITFIELD
) {
2657 /* bitfield store handling */
2658 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2659 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2660 /* remove bit field info to avoid loops */
2661 vtop
[-1].type
.t
= ft
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
2662 /* duplicate source into other register */
2663 if(dbt
== VT_BOOL
) {
2664 gen_cast(&vtop
[-1].type
);
2665 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2668 /* duplicate destination */
2670 vtop
[-1] = vtop
[-2];
2672 /* mask and shift source */
2673 if(dbt
!= VT_BOOL
) {
2674 if(dbt
== VT_LLONG
) {
2675 vpushll((1ULL << bit_size
) - 1ULL);
2677 vpushi((1 << bit_size
) - 1);
2683 /* load destination, mask and or with source */
2685 if(dbt
== VT_LLONG
) {
2686 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2688 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2695 #ifdef CONFIG_TCC_BCHECK
2696 /* bound check case */
2697 if (vtop
[-1].r
& VT_MUSTBOUND
) {
2703 if (!nocode_wanted
) {
2707 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
2708 vtop
->r
|= delayed_cast
;
2712 /* post defines POST/PRE add. c is the token ++ or -- */
2713 ST_FUNC
void inc(int post
, int c
)
2716 vdup(); /* save lvalue */
2718 gv_dup(); /* duplicate value */
2723 vpushi(c
- TOK_MID
);
2725 vstore(); /* store value */
2727 vpop(); /* if post op, return saved value */
2730 /* Parse GNUC __attribute__ extension. Currently, the following
2731 extensions are recognized:
2732 - aligned(n) : set data/function alignment.
2733 - packed : force data alignment to 1
2734 - section(x) : generate data/code in this section.
2735 - unused : currently ignored, but may be used someday.
2736 - regparm(n) : pass function parameters in registers (i386 only)
2738 static void parse_attribute(AttributeDef
*ad
)
2742 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
2746 while (tok
!= ')') {
2747 if (tok
< TOK_IDENT
)
2748 expect("attribute name");
2756 expect("section name");
2757 ad
->section
= find_section(tcc_state
, (char *)tokc
.cstr
->data
);
2765 expect("alias(\"target\")");
2766 ad
->alias_target
= /* save string as token, for later */
2767 tok_alloc((char*)tokc
.cstr
->data
, tokc
.cstr
->size
-1)->tok
;
2771 case TOK_VISIBILITY1
:
2772 case TOK_VISIBILITY2
:
2775 expect("visibility(\"default|hidden|internal|protected\")");
2776 if (!strcmp (tokc
.cstr
->data
, "default"))
2777 ad
->a
.visibility
= STV_DEFAULT
;
2778 else if (!strcmp (tokc
.cstr
->data
, "hidden"))
2779 ad
->a
.visibility
= STV_HIDDEN
;
2780 else if (!strcmp (tokc
.cstr
->data
, "internal"))
2781 ad
->a
.visibility
= STV_INTERNAL
;
2782 else if (!strcmp (tokc
.cstr
->data
, "protected"))
2783 ad
->a
.visibility
= STV_PROTECTED
;
2785 expect("visibility(\"default|hidden|internal|protected\")");
2794 if (n
<= 0 || (n
& (n
- 1)) != 0)
2795 tcc_error("alignment must be a positive power of two");
2812 /* currently, no need to handle it because tcc does not
2813 track unused objects */
2817 /* currently, no need to handle it because tcc does not
2818 track unused objects */
2823 ad
->a
.func_call
= FUNC_CDECL
;
2828 ad
->a
.func_call
= FUNC_STDCALL
;
2830 #ifdef TCC_TARGET_I386
2840 ad
->a
.func_call
= FUNC_FASTCALL1
+ n
- 1;
2846 ad
->a
.func_call
= FUNC_FASTCALLW
;
2853 ad
->a
.mode
= VT_LLONG
+ 1;
2856 ad
->a
.mode
= VT_SHORT
+ 1;
2859 ad
->a
.mode
= VT_INT
+ 1;
2862 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
2869 ad
->a
.func_export
= 1;
2872 ad
->a
.func_import
= 1;
2875 if (tcc_state
->warn_unsupported
)
2876 tcc_warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
2877 /* skip parameters */
2879 int parenthesis
= 0;
2883 else if (tok
== ')')
2886 } while (parenthesis
&& tok
!= -1);
2899 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2900 static void struct_decl(CType
*type
, int u
, int tdef
)
2902 int a
, v
, size
, align
, maxalign
, c
, offset
, flexible
;
2903 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
, prevbt
;
2904 Sym
*s
, *ss
, *ass
, **ps
;
2908 a
= tok
; /* save decl type */
2913 /* struct already defined ? return it */
2915 expect("struct/union/enum name");
2919 tcc_error("invalid type");
2921 } else if (tok
>= TOK_IDENT
&& !tdef
)
2922 tcc_error("unknown struct/union/enum");
2928 /* we put an undefined size for struct/union */
2929 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
2930 s
->r
= 0; /* default alignment is zero as gcc */
2931 /* put struct/union/enum name in type */
2939 tcc_error("struct/union/enum already defined");
2940 /* cannot be empty */
2942 /* non empty enums are not allowed */
2943 if (a
== TOK_ENUM
) {
2947 expect("identifier");
2949 if (ss
&& !local_stack
)
2950 tcc_error("redefinition of enumerator '%s'",
2951 get_tok_str(v
, NULL
));
2957 /* enum symbols have static storage */
2958 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
2959 ss
->type
.t
|= VT_STATIC
;
2964 /* NOTE: we accept a trailing comma */
2968 s
->c
= type_size(&int_type
, &align
);
2977 while (tok
!= '}') {
2978 parse_btype(&btype
, &ad
);
2981 tcc_error("flexible array member '%s' not at the end of struct",
2982 get_tok_str(v
, NULL
));
2987 type_decl(&type1
, &ad
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
2988 if (v
== 0 && (type1
.t
& VT_BTYPE
) != VT_STRUCT
)
2989 expect("identifier");
2990 if (type_size(&type1
, &align
) < 0) {
2991 if ((a
== TOK_STRUCT
) && (type1
.t
& VT_ARRAY
) && c
)
2994 tcc_error("field '%s' has incomplete type",
2995 get_tok_str(v
, NULL
));
2997 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
2998 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
2999 tcc_error("invalid type for '%s'",
3000 get_tok_str(v
, NULL
));
3004 bit_size
= expr_const();
3005 /* XXX: handle v = 0 case for messages */
3007 tcc_error("negative width in bit-field '%s'",
3008 get_tok_str(v
, NULL
));
3009 if (v
&& bit_size
== 0)
3010 tcc_error("zero width for bit-field '%s'",
3011 get_tok_str(v
, NULL
));
3013 size
= type_size(&type1
, &align
);
3015 if (align
< ad
.a
.aligned
)
3016 align
= ad
.a
.aligned
;
3017 } else if (ad
.a
.packed
) {
3019 } else if (*tcc_state
->pack_stack_ptr
) {
3020 if (align
> *tcc_state
->pack_stack_ptr
)
3021 align
= *tcc_state
->pack_stack_ptr
;
3024 if (bit_size
>= 0) {
3025 bt
= type1
.t
& VT_BTYPE
;
3032 tcc_error("bitfields must have scalar type");
3034 if (bit_size
> bsize
) {
3035 tcc_error("width of '%s' exceeds its type",
3036 get_tok_str(v
, NULL
));
3037 } else if (bit_size
== bsize
) {
3038 /* no need for bit fields */
3040 } else if (bit_size
== 0) {
3041 /* XXX: what to do if only padding in a
3043 /* zero size: means to pad */
3046 /* we do not have enough room ?
3047 did the type change?
3049 if ((bit_pos
+ bit_size
) > bsize
||
3050 bt
!= prevbt
|| a
== TOK_UNION
)
3053 /* XXX: handle LSB first */
3054 type1
.t
|= VT_BITFIELD
|
3055 (bit_pos
<< VT_STRUCT_SHIFT
) |
3056 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
3057 bit_pos
+= bit_size
;
3063 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
3064 /* add new memory data only if starting
3066 if (lbit_pos
== 0) {
3067 if (a
== TOK_STRUCT
) {
3068 c
= (c
+ align
- 1) & -align
;
3077 if (align
> maxalign
)
3081 printf("add field %s offset=%d",
3082 get_tok_str(v
, NULL
), offset
);
3083 if (type1
.t
& VT_BITFIELD
) {
3084 printf(" pos=%d size=%d",
3085 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
3086 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
3091 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
3093 while ((ass
= ass
->next
) != NULL
) {
3094 ss
= sym_push(ass
->v
, &ass
->type
, 0, offset
+ ass
->c
);
3099 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
3103 if (tok
== ';' || tok
== TOK_EOF
)
3110 /* store size and alignment */
3111 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
3117 /* return 1 if basic type is a type size (short, long, long long) */
3118 ST_FUNC
int is_btype_size(int bt
)
3120 return bt
== VT_SHORT
|| bt
== VT_LONG
|| bt
== VT_LLONG
;
3123 /* return 0 if no type declaration. otherwise, return the basic type
3126 static int parse_btype(CType
*type
, AttributeDef
*ad
)
3128 int t
, u
, bt_size
, complete
, type_found
, typespec_found
;
3132 memset(ad
, 0, sizeof(AttributeDef
));
3140 /* currently, we really ignore extension */
3151 tcc_error("too many basic types");
3153 bt_size
= is_btype_size (u
& VT_BTYPE
);
3154 if (u
== VT_INT
|| (!bt_size
&& !(t
& VT_TYPEDEF
)))
3169 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
3170 #ifndef TCC_TARGET_PE
3171 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3173 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
3174 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3188 if ((t
& VT_BTYPE
) == VT_LONG
) {
3189 #ifdef TCC_TARGET_PE
3190 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
3192 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3200 struct_decl(&type1
, VT_ENUM
, t
& VT_TYPEDEF
);
3203 type
->ref
= type1
.ref
;
3207 struct_decl(&type1
, VT_STRUCT
, t
& VT_TYPEDEF
);
3210 /* type modifiers */
3226 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == (VT_DEFSIGN
|VT_UNSIGNED
))
3227 tcc_error("signed and unsigned modifier");
3240 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == VT_DEFSIGN
)
3241 tcc_error("signed and unsigned modifier");
3242 t
|= VT_DEFSIGN
| VT_UNSIGNED
;
3267 /* GNUC attribute */
3268 case TOK_ATTRIBUTE1
:
3269 case TOK_ATTRIBUTE2
:
3270 parse_attribute(ad
);
3273 t
= (t
& ~VT_BTYPE
) | u
;
3281 parse_expr_type(&type1
);
3282 /* remove all storage modifiers except typedef */
3283 type1
.t
&= ~(VT_STORAGE
&~VT_TYPEDEF
);
3289 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
3291 t
|= (s
->type
.t
& ~VT_TYPEDEF
);
3292 type
->ref
= s
->type
.ref
;
3294 /* get attributes from typedef */
3295 if (0 == ad
->a
.aligned
)
3296 ad
->a
.aligned
= s
->a
.aligned
;
3297 if (0 == ad
->a
.func_call
)
3298 ad
->a
.func_call
= s
->a
.func_call
;
3299 ad
->a
.packed
|= s
->a
.packed
;
3308 if (tcc_state
->char_is_unsigned
) {
3309 if ((t
& (VT_DEFSIGN
|VT_BTYPE
)) == VT_BYTE
)
3313 /* long is never used as type */
3314 if ((t
& VT_BTYPE
) == VT_LONG
)
3315 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
3316 t
= (t
& ~VT_BTYPE
) | VT_INT
;
3318 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3324 /* convert a function parameter type (array to pointer and function to
3325 function pointer) */
3326 static inline void convert_parameter_type(CType
*pt
)
3328 /* remove const and volatile qualifiers (XXX: const could be used
3329 to indicate a const function parameter */
3330 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3331 /* array must be transformed to pointer according to ANSI C */
3333 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
3338 ST_FUNC
void parse_asm_str(CString
*astr
)
3341 /* read the string */
3343 expect("string constant");
3345 while (tok
== TOK_STR
) {
3346 /* XXX: add \0 handling too ? */
3347 cstr_cat(astr
, tokc
.cstr
->data
);
3350 cstr_ccat(astr
, '\0');
3353 /* Parse an asm label and return the label
3354 * Don't forget to free the CString in the caller! */
3355 static void asm_label_instr(CString
*astr
)
3358 parse_asm_str(astr
);
3361 printf("asm_alias: \"%s\"\n", (char *)astr
->data
);
3365 static void post_type(CType
*type
, AttributeDef
*ad
)
3367 int n
, l
, t1
, arg_size
, size
, align
;
3368 Sym
**plast
, *s
, *first
;
3373 /* function declaration */
3381 /* read param name and compute offset */
3382 if (l
!= FUNC_OLD
) {
3383 if (!parse_btype(&pt
, &ad1
)) {
3385 tcc_error("invalid type");
3392 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
3394 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3395 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
3396 tcc_error("parameter declared as void");
3397 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
3402 expect("identifier");
3406 convert_parameter_type(&pt
);
3407 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
3413 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
3420 /* if no parameters, then old type prototype */
3424 /* NOTE: const is ignored in returned type as it has a special
3425 meaning in gcc / C++ */
3426 type
->t
&= ~VT_CONSTANT
;
3427 /* some ancient pre-K&R C allows a function to return an array
3428 and the array brackets to be put after the arguments, such
3429 that "int c()[]" means something like "int[] c()" */
3432 skip(']'); /* only handle simple "[]" */
3435 /* we push a anonymous symbol which will contain the function prototype */
3436 ad
->a
.func_args
= arg_size
;
3437 s
= sym_push(SYM_FIELD
, type
, 0, l
);
3442 } else if (tok
== '[') {
3443 /* array definition */
3445 if (tok
== TOK_RESTRICT1
)
3450 if (!local_stack
|| nocode_wanted
)
3451 vpushi(expr_const());
3453 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3456 tcc_error("invalid array size");
3458 if (!is_integer_btype(vtop
->type
.t
& VT_BTYPE
))
3459 tcc_error("size of variable length array should be an integer");
3464 /* parse next post type */
3465 post_type(type
, ad
);
3466 if (type
->t
== VT_FUNC
)
3467 tcc_error("declaration of an array of functions");
3468 t1
|= type
->t
& VT_VLA
;
3471 size
= type_size(&int_type
, &align
);
3472 n
= loc_stack(size
, 1);
3474 vla_runtime_type_size(type
, &align
);
3476 vset(&int_type
, VT_LOCAL
|VT_LVAL
, n
);
3483 /* we push an anonymous symbol which will contain the array
3485 s
= sym_push(SYM_FIELD
, type
, 0, n
);
3486 type
->t
= (t1
? VT_VLA
: VT_ARRAY
) | VT_PTR
;
3491 /* Parse a type declaration (except basic type), and return the type
3492 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3493 expected. 'type' should contain the basic type. 'ad' is the
3494 attribute definition of the basic type. It can be modified by
3497 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
3500 CType type1
, *type2
;
3501 int qualifiers
, storage
;
3503 while (tok
== '*') {
3511 qualifiers
|= VT_CONSTANT
;
3516 qualifiers
|= VT_VOLATILE
;
3524 type
->t
|= qualifiers
;
3527 /* XXX: clarify attribute handling */
3528 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3529 parse_attribute(ad
);
3531 /* recursive type */
3532 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3533 type1
.t
= 0; /* XXX: same as int */
3536 /* XXX: this is not correct to modify 'ad' at this point, but
3537 the syntax is not clear */
3538 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3539 parse_attribute(ad
);
3540 type_decl(&type1
, ad
, v
, td
);
3543 /* type identifier */
3544 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
3548 if (!(td
& TYPE_ABSTRACT
))
3549 expect("identifier");
3553 storage
= type
->t
& VT_STORAGE
;
3554 type
->t
&= ~VT_STORAGE
;
3555 if (storage
& VT_STATIC
) {
3556 int saved_nocode_wanted
= nocode_wanted
;
3558 post_type(type
, ad
);
3559 nocode_wanted
= saved_nocode_wanted
;
3561 post_type(type
, ad
);
3563 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3564 parse_attribute(ad
);
3568 /* append type at the end of type1 */
3581 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3582 ST_FUNC
int lvalue_type(int t
)
3587 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
3589 else if (bt
== VT_SHORT
)
3593 if (t
& VT_UNSIGNED
)
3594 r
|= VT_LVAL_UNSIGNED
;
3598 /* indirection with full error checking and bound check */
3599 ST_FUNC
void indir(void)
3601 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
3602 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
3606 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
3608 vtop
->type
= *pointed_type(&vtop
->type
);
3609 /* Arrays and functions are never lvalues */
3610 if (!(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_VLA
)
3611 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3612 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3613 /* if bound checking, the referenced pointer must be checked */
3614 #ifdef CONFIG_TCC_BCHECK
3615 if (tcc_state
->do_bounds_check
)
3616 vtop
->r
|= VT_MUSTBOUND
;
3621 /* pass a parameter to a function and do type checking and casting */
3622 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
3627 func_type
= func
->c
;
3628 if (func_type
== FUNC_OLD
||
3629 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
3630 /* default casting : only need to convert float to double */
3631 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
3634 } else if (vtop
->type
.t
& VT_BITFIELD
) {
3635 type
.t
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
3638 } else if (arg
== NULL
) {
3639 tcc_error("too many arguments to function");
3642 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
3643 gen_assign_cast(&type
);
3647 /* parse an expression of the form '(type)' or '(expr)' and return its
3649 static void parse_expr_type(CType
*type
)
3655 if (parse_btype(type
, &ad
)) {
3656 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3663 static void parse_type(CType
*type
)
3668 if (!parse_btype(type
, &ad
)) {
3671 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3674 static void vpush_tokc(int t
)
3679 vsetc(&type
, VT_CONST
, &tokc
);
3682 ST_FUNC
void unary(void)
3684 int n
, t
, align
, size
, r
, sizeof_caller
;
3688 static int in_sizeof
= 0;
3690 sizeof_caller
= in_sizeof
;
3692 /* XXX: GCC 2.95.3 does not generate a table although it should be
3706 vpush_tokc(VT_INT
| VT_UNSIGNED
);
3710 vpush_tokc(VT_LLONG
);
3714 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
3718 vpush_tokc(VT_FLOAT
);
3722 vpush_tokc(VT_DOUBLE
);
3726 vpush_tokc(VT_LDOUBLE
);
3729 case TOK___FUNCTION__
:
3731 goto tok_identifier
;
3737 /* special function name identifier */
3738 len
= strlen(funcname
) + 1;
3739 /* generate char[len] type */
3744 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
3745 ptr
= section_ptr_add(data_section
, len
);
3746 memcpy(ptr
, funcname
, len
);
3751 #ifdef TCC_TARGET_PE
3752 t
= VT_SHORT
| VT_UNSIGNED
;
3758 /* string parsing */
3761 if (tcc_state
->warn_write_strings
)
3766 memset(&ad
, 0, sizeof(AttributeDef
));
3767 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, NULL
, 0);
3772 if (parse_btype(&type
, &ad
)) {
3773 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
3775 /* check ISOC99 compound literal */
3777 /* data is allocated locally by default */
3782 /* all except arrays are lvalues */
3783 if (!(type
.t
& VT_ARRAY
))
3784 r
|= lvalue_type(type
.t
);
3785 memset(&ad
, 0, sizeof(AttributeDef
));
3786 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, NULL
, 0);
3788 if (sizeof_caller
) {
3795 } else if (tok
== '{') {
3796 /* save all registers */
3798 /* statement expression : we do not accept break/continue
3799 inside as GCC does */
3800 block(NULL
, NULL
, NULL
, NULL
, 0, 1);
3815 /* functions names must be treated as function pointers,
3816 except for unary '&' and sizeof. Since we consider that
3817 functions are not lvalues, we only have to handle it
3818 there and in function calls. */
3819 /* arrays can also be used although they are not lvalues */
3820 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
3821 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
3823 mk_pointer(&vtop
->type
);
3829 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3831 boolean
.t
= VT_BOOL
;
3833 vtop
->c
.i
= !vtop
->c
.i
;
3834 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
3835 vtop
->c
.i
= vtop
->c
.i
^ 1;
3838 vseti(VT_JMP
, gtst(1, 0));
3850 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
3851 tcc_error("pointer not accepted for unary plus");
3852 /* In order to force cast, we add zero, except for floating point
3853 where we really need an noop (otherwise -0.0 will be transformed
3855 if (!is_float(vtop
->type
.t
)) {
3866 unary_type(&type
); // Perform a in_sizeof = 0;
3867 size
= type_size(&type
, &align
);
3868 if (t
== TOK_SIZEOF
) {
3869 if (!(type
.t
& VT_VLA
)) {
3871 tcc_error("sizeof applied to an incomplete type");
3874 vla_runtime_type_size(&type
, &align
);
3879 vtop
->type
.t
|= VT_UNSIGNED
;
3882 case TOK_builtin_types_compatible_p
:
3891 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3892 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3893 vpushi(is_compatible_types(&type1
, &type2
));
3896 case TOK_builtin_constant_p
:
3898 int saved_nocode_wanted
, res
;
3901 saved_nocode_wanted
= nocode_wanted
;
3904 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
3906 nocode_wanted
= saved_nocode_wanted
;
3911 case TOK_builtin_frame_address
:
3917 if (tok
!= TOK_CINT
|| tokc
.i
< 0) {
3918 tcc_error("__builtin_frame_address only takes positive integers");
3925 vset(&type
, VT_LOCAL
, 0); /* local frame */
3927 mk_pointer(&vtop
->type
);
3928 indir(); /* -> parent frame */
3932 #ifdef TCC_TARGET_X86_64
3933 #ifdef TCC_TARGET_PE
3934 case TOK_builtin_va_start
:
3942 if ((vtop
->r
& VT_VALMASK
) != VT_LOCAL
)
3943 tcc_error("__builtin_va_start expects a local variable");
3944 vtop
->r
&= ~(VT_LVAL
| VT_REF
);
3945 vtop
->type
= char_pointer_type
;
3950 case TOK_builtin_va_arg_types
:
3957 vpushi(classify_x86_64_va_arg(&type
));
3972 t
= vtop
->type
.t
& VT_BTYPE
;
3974 /* In IEEE negate(x) isn't subtract(0,x), but rather
3979 else if (t
== VT_DOUBLE
)
3990 goto tok_identifier
;
3992 /* allow to take the address of a label */
3993 if (tok
< TOK_UIDENT
)
3994 expect("label identifier");
3995 s
= label_find(tok
);
3997 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
3999 if (s
->r
== LABEL_DECLARED
)
4000 s
->r
= LABEL_FORWARD
;
4003 s
->type
.t
= VT_VOID
;
4004 mk_pointer(&s
->type
);
4005 s
->type
.t
|= VT_STATIC
;
4007 vpushsym(&s
->type
, s
);
4011 // special qnan , snan and infinity values
4013 vpush64(VT_DOUBLE
, 0x7ff8000000000000ULL
);
4017 vpush64(VT_DOUBLE
, 0x7ff0000000000001ULL
);
4021 vpush64(VT_DOUBLE
, 0x7ff0000000000000ULL
);
4030 expect("identifier");
4033 const char *name
= get_tok_str(t
, NULL
);
4035 tcc_error("'%s' undeclared", name
);
4036 /* for simple function calls, we tolerate undeclared
4037 external reference to int() function */
4038 if (tcc_state
->warn_implicit_function_declaration
4039 #ifdef TCC_TARGET_PE
4040 /* people must be warned about using undeclared WINAPI functions
4041 (which usually start with uppercase letter) */
4042 || (name
[0] >= 'A' && name
[0] <= 'Z')
4045 tcc_warning("implicit declaration of function '%s'", name
);
4046 s
= external_global_sym(t
, &func_old_type
, 0);
4048 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
4049 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
4050 /* if referencing an inline function, then we generate a
4051 symbol to it if not already done. It will have the
4052 effect to generate code for it at the end of the
4053 compilation unit. Inline function as always
4054 generated in the text section. */
4056 put_extern_sym(s
, text_section
, 0, 0);
4057 r
= VT_SYM
| VT_CONST
;
4061 vset(&s
->type
, r
, s
->c
);
4062 /* if forward reference, we must point to s */
4063 if (vtop
->r
& VT_SYM
) {
4065 vtop
->c
.ptr_offset
= 0;
4070 /* post operations */
4073 int ret_nregs
, ret_align
;
4074 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
4077 } else if (tok
== '.' || tok
== TOK_ARROW
) {
4078 int qualifiers
, add
, is_lval
;
4080 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
4082 if (tok
== TOK_ARROW
)
4086 is_lval
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) >= VT_CONST
;
4090 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
4092 gfunc_sret(&vtop
->type
, func_var
, &ret
.type
, &ret_align
);
4095 /* expect pointer on structure */
4096 if ((type
.t
& VT_BTYPE
) != VT_STRUCT
)
4097 expect("struct or union");
4101 while ((s
= s
->next
) != NULL
) {
4106 tcc_error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, NULL
));
4109 /* change type to field type, and set to lvalue */
4114 type
.t
|= qualifiers
;
4119 /* an array is never an lvalue */
4120 if (!(type
.t
& VT_ARRAY
)) {
4121 vtop
->r
|= lvalue_type(type
.t
);
4122 #ifdef CONFIG_TCC_BCHECK
4123 /* if bound checking, the referenced pointer must be checked */
4124 if (tcc_state
->do_bounds_check
)
4125 vtop
->r
|= VT_MUSTBOUND
;
4129 gfunc_sret(&vtop
->type
, func_var
, &ret
.type
, &ret_align
);
4130 if(is_float(ret
.type
.t
) || (type
.t
& VT_ARRAY
)){
4131 #ifdef TCC_TARGET_X86_64
4132 if((ret
.type
.t
& VT_BTYPE
) != VT_LDOUBLE
)
4138 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
4142 #ifdef TCC_TARGET_X86_64
4147 if(add
& load_size
){
4150 vtop
->r2
= VT_CONST
;
4153 vtop
->type
.t
= VT_LLONG
;
4160 } else if (tok
== '[') {
4166 } else if (tok
== '(') {
4168 int nb_args
, variadic
, addr
;
4171 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
4172 /* pointer test (no array accepted) */
4173 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
4174 vtop
->type
= *pointed_type(&vtop
->type
);
4175 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
4179 expect("function pointer");
4182 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
4184 /* get return type */
4186 sa
= s
->next
; /* first parameter */
4189 /* compute first implicit argument if a structure is returned */
4190 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
4191 variadic
= (s
->c
== FUNC_ELLIPSIS
);
4192 ret_nregs
= gfunc_sret(&s
->type
, variadic
, &ret
.type
,
4195 /* get some space for the returned structure */
4196 size
= type_size(&s
->type
, &align
);
4197 addr
= loc_stack(size
, 1);
4199 ret
.r
= VT_LOCAL
| VT_LVAL
;
4200 /* pass it as 'int' to avoid structure arg passing
4202 vseti(VT_LOCAL
, addr
);
4212 /* return in register */
4213 if (is_float(ret
.type
.t
)) {
4214 ret
.r
= reg_fret(ret
.type
.t
);
4215 #ifdef TCC_TARGET_X86_64
4216 if ((ret
.type
.t
& VT_BTYPE
) == VT_QFLOAT
)
4220 #ifdef TCC_TARGET_X86_64
4221 if ((ret
.type
.t
& VT_BTYPE
) == VT_QLONG
)
4223 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
4234 gfunc_param_typed(s
, sa
);
4244 tcc_error("too few arguments to function");
4246 if (!nocode_wanted
) {
4247 gfunc_call(nb_args
);
4249 vtop
-= (nb_args
+ 1);
4253 for (r
= ret
.r
+ ret_nregs
+ !ret_nregs
; r
-- > ret
.r
;) {
4254 vsetc(&ret
.type
, r
, &ret
.c
);
4255 vtop
->r2
= ret
.r2
; /* Loop only happens when r2 is VT_CONST */
4258 /* handle packed struct return */
4259 if (((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) && ret_nregs
)
4260 vtop
->type
= s
->type
;
4267 ST_FUNC
void expr_prod(void)
4272 while (tok
== '*' || tok
== '/' || tok
== '%') {
4280 ST_FUNC
void expr_sum(void)
4285 while (tok
== '+' || tok
== '-') {
4293 static void expr_shift(void)
4298 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
4306 static void expr_cmp(void)
4311 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
4312 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
4320 static void expr_cmpeq(void)
4325 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
4333 static void expr_and(void)
4336 while (tok
== '&') {
4343 static void expr_xor(void)
4346 while (tok
== '^') {
4353 static void expr_or(void)
4356 while (tok
== '|') {
4363 /* XXX: fix this mess */
4364 static void expr_land_const(void)
4367 while (tok
== TOK_LAND
) {
4374 /* XXX: fix this mess */
4375 static void expr_lor_const(void)
4378 while (tok
== TOK_LOR
) {
4385 /* only used if non constant */
4386 static void expr_land(void)
4391 if (tok
== TOK_LAND
) {
4396 if (tok
!= TOK_LAND
) {
4406 static void expr_lor(void)
4411 if (tok
== TOK_LOR
) {
4416 if (tok
!= TOK_LOR
) {
4426 /* XXX: better constant handling */
4427 static void expr_cond(void)
4429 int tt
, u
, r
, rc
, t1
, t2
, bt1
, bt2
, ret_nregs
, ret_align
;
4431 CType type
, type1
, type2
;
4438 boolean
.t
= VT_BOOL
;
4444 if (tok
!= ':' || !gnu_ext
) {
4459 if (vtop
!= vstack
) {
4460 /* needed to avoid having different registers saved in
4462 if (is_float(vtop
->type
.t
)) {
4464 #ifdef TCC_TARGET_X86_64
4465 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4474 if (tok
== ':' && gnu_ext
) {
4482 sv
= *vtop
; /* save value to handle it later */
4483 vtop
--; /* no vpop so that FP stack is not flushed */
4491 bt1
= t1
& VT_BTYPE
;
4493 bt2
= t2
& VT_BTYPE
;
4494 /* cast operands to correct type according to ISOC rules */
4495 if (is_float(bt1
) || is_float(bt2
)) {
4496 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
4497 type
.t
= VT_LDOUBLE
;
4498 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
4503 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
4504 /* cast to biggest op */
4506 /* convert to unsigned if it does not fit in a long long */
4507 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
4508 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
4509 type
.t
|= VT_UNSIGNED
;
4510 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
4511 /* If one is a null ptr constant the result type is the other. */
4512 if (is_null_pointer (vtop
))
4514 else if (is_null_pointer (&sv
))
4516 /* XXX: test pointer compatibility, C99 has more elaborate rules here. */
4519 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
4520 /* XXX: test function pointer compatibility */
4521 type
= bt1
== VT_FUNC
? type1
: type2
;
4522 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
4523 /* XXX: test structure compatibility */
4524 type
= bt1
== VT_STRUCT
? type1
: type2
;
4525 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
4526 /* NOTE: as an extension, we accept void on only one side */
4529 /* integer operations */
4531 /* convert to unsigned if it does not fit in an integer */
4532 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
4533 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
4534 type
.t
|= VT_UNSIGNED
;
4537 /* now we convert second operand */
4540 if (VT_STRUCT
== (type
.t
& VT_BTYPE
)){
4541 ret_nregs
= gfunc_sret(&type
, func_var
, &ret
.type
, &ret_align
);
4543 vtop
->type
= ret
.type
;
4548 if (is_float(vtop
->type
.t
)) {
4550 #ifdef TCC_TARGET_X86_64
4551 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4558 rc
= reg_classes
[r
] & ~RC_MASK
;
4559 #ifdef TCC_TARGET_X86_64
4560 if (((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
))
4562 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
)
4564 ex_rc
= reg_classes
[vtop
->r2
] & ~RC_MASK
;
4565 /* this is horrible, but we must also convert first
4569 /* put again first value and cast it */
4572 if (VT_STRUCT
== (type
.t
& VT_BTYPE
)){
4574 vtop
->type
= ret
.type
;
4581 if (VT_STRUCT
== (type
.t
& VT_BTYPE
)){
4591 static void expr_eq(void)
4597 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
4598 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
4599 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
4614 ST_FUNC
void gexpr(void)
4625 /* parse an expression and return its type without any side effect. */
4626 static void expr_type(CType
*type
)
4628 int saved_nocode_wanted
;
4630 saved_nocode_wanted
= nocode_wanted
;
4635 nocode_wanted
= saved_nocode_wanted
;
4638 /* parse a unary expression and return its type without any side
4640 static void unary_type(CType
*type
)
4652 /* parse a constant expression and return value in vtop. */
4653 static void expr_const1(void)
4662 /* parse an integer constant and return its value. */
4663 ST_FUNC
int expr_const(void)
4667 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
4668 expect("constant expression");
4674 /* return the label token if current token is a label, otherwise
4676 static int is_label(void)
4680 /* fast test first */
4681 if (tok
< TOK_UIDENT
)
4683 /* no need to save tokc because tok is an identifier */
4690 unget_tok(last_tok
);
4695 static void label_or_decl(int l
)
4699 /* fast test first */
4700 if (tok
>= TOK_UIDENT
)
4702 /* no need to save tokc because tok is an identifier */
4706 unget_tok(last_tok
);
4709 unget_tok(last_tok
);
4714 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
4715 int case_reg
, int is_expr
)
4718 Sym
*s
, *frame_bottom
;
4720 /* generate line number info */
4721 if (tcc_state
->do_debug
&&
4722 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
4723 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
4725 last_line_num
= file
->line_num
;
4729 /* default return value is (void) */
4731 vtop
->type
.t
= VT_VOID
;
4734 if (tok
== TOK_IF
) {
4741 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4743 if (c
== TOK_ELSE
) {
4747 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4748 gsym(d
); /* patch else jmp */
4751 } else if (tok
== TOK_WHILE
) {
4759 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4763 } else if (tok
== '{') {
4765 int block_vla_sp_loc
, *saved_vla_sp_loc
, saved_vla_flags
;
4768 /* record local declaration stack position */
4770 frame_bottom
= sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4771 frame_bottom
->next
= scope_stack_bottom
;
4772 scope_stack_bottom
= frame_bottom
;
4773 llabel
= local_label_stack
;
4775 /* save VLA state */
4776 block_vla_sp_loc
= *(saved_vla_sp_loc
= vla_sp_loc
);
4777 if (saved_vla_sp_loc
!= &vla_sp_root_loc
)
4778 vla_sp_loc
= &block_vla_sp_loc
;
4780 saved_vla_flags
= vla_flags
;
4781 vla_flags
|= VLA_NEED_NEW_FRAME
;
4783 /* handle local labels declarations */
4784 if (tok
== TOK_LABEL
) {
4787 if (tok
< TOK_UIDENT
)
4788 expect("label identifier");
4789 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
4799 while (tok
!= '}') {
4800 label_or_decl(VT_LOCAL
);
4804 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4807 /* pop locally defined labels */
4808 label_pop(&local_label_stack
, llabel
);
4810 /* XXX: this solution makes only valgrind happy...
4811 triggered by gcc.c-torture/execute/20000917-1.c */
4813 switch(vtop
->type
.t
& VT_BTYPE
) {
4818 for(p
=vtop
->type
.ref
;p
;p
=p
->prev
)
4820 tcc_error("unsupported expression type");
4823 /* pop locally defined symbols */
4824 scope_stack_bottom
= scope_stack_bottom
->next
;
4825 sym_pop(&local_stack
, s
);
4827 /* Pop VLA frames and restore stack pointer if required */
4828 if (saved_vla_sp_loc
!= &vla_sp_root_loc
)
4829 *saved_vla_sp_loc
= block_vla_sp_loc
;
4830 if (vla_sp_loc
!= (saved_vla_sp_loc
== &vla_sp_root_loc
? &vla_sp_root_loc
: &block_vla_sp_loc
)) {
4831 vla_sp_loc
= saved_vla_sp_loc
;
4832 gen_vla_sp_restore(*vla_sp_loc
);
4834 vla_flags
= (vla_flags
& ~VLA_SCOPE_FLAGS
) | (saved_vla_flags
& VLA_SCOPE_FLAGS
);
4837 } else if (tok
== TOK_RETURN
) {
4841 gen_assign_cast(&func_vt
);
4842 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
4843 CType type
, ret_type
;
4844 int ret_align
, ret_nregs
;
4845 ret_nregs
= gfunc_sret(&func_vt
, func_var
, &ret_type
,
4847 if (0 == ret_nregs
) {
4848 /* if returning structure, must copy it to implicit
4849 first pointer arg location */
4852 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
4855 /* copy structure value to pointer */
4858 /* returning structure packed into registers */
4860 vtop
->type
= ret_type
;
4861 if (is_float(ret_type
.t
))
4862 rc
= rc_fret(ret_type
.t
);
4870 if (--ret_nregs
== 0)
4872 /* We assume that when a structure is returned in multiple
4873 registers, their classes are consecutive values of the
4876 /* XXX: compatible with arm only: ret_align == register_size */
4877 vtop
->c
.i
+= ret_align
;
4878 vtop
->r
= VT_LOCAL
| VT_LVAL
;
4881 } else if (is_float(func_vt
.t
)) {
4882 gv(rc_fret(func_vt
.t
));
4886 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
4889 rsym
= gjmp(rsym
); /* jmp */
4890 } else if (tok
== TOK_BREAK
) {
4893 tcc_error("cannot break");
4894 *bsym
= gjmp(*bsym
);
4897 } else if (tok
== TOK_CONTINUE
) {
4900 tcc_error("cannot continue");
4901 *csym
= gjmp(*csym
);
4904 } else if (tok
== TOK_FOR
) {
4909 frame_bottom
= sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4910 frame_bottom
->next
= scope_stack_bottom
;
4911 scope_stack_bottom
= frame_bottom
;
4913 /* c99 for-loop init decl? */
4914 if (!decl0(VT_LOCAL
, 1)) {
4915 /* no, regular for-loop init expr */
4939 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4943 scope_stack_bottom
= scope_stack_bottom
->next
;
4944 sym_pop(&local_stack
, s
);
4946 if (tok
== TOK_DO
) {
4951 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4962 if (tok
== TOK_SWITCH
) {
4966 /* XXX: other types than integer */
4967 case_reg
= gv(RC_INT
);
4971 b
= gjmp(0); /* jump to first case */
4973 block(&a
, csym
, &b
, &c
, case_reg
, 0);
4974 /* if no default, jmp after switch */
4982 if (tok
== TOK_CASE
) {
4989 if (gnu_ext
&& tok
== TOK_DOTS
) {
4993 tcc_warning("empty case range");
4995 /* since a case is like a label, we must skip it with a jmp */
5002 *case_sym
= gtst(1, 0);
5005 *case_sym
= gtst(1, 0);
5009 *case_sym
= gtst(1, *case_sym
);
5014 goto block_after_label
;
5016 if (tok
== TOK_DEFAULT
) {
5022 tcc_error("too many 'default'");
5025 goto block_after_label
;
5027 if (tok
== TOK_GOTO
) {
5029 if (tok
== '*' && gnu_ext
) {
5033 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
5036 } else if (tok
>= TOK_UIDENT
) {
5037 s
= label_find(tok
);
5038 /* put forward definition if needed */
5040 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
5042 if (s
->r
== LABEL_DECLARED
)
5043 s
->r
= LABEL_FORWARD
;
5045 /* label already defined */
5046 if (vla_flags
& VLA_IN_SCOPE
) {
5047 /* If VLAs are in use, save the current stack pointer and
5048 reset the stack pointer to what it was at function entry
5049 (label will restore stack pointer in inner scopes) */
5051 gen_vla_sp_restore(vla_sp_root_loc
);
5053 if (s
->r
& LABEL_FORWARD
)
5054 s
->jnext
= gjmp(s
->jnext
);
5056 gjmp_addr(s
->jnext
);
5059 expect("label identifier");
5062 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
5068 if (vla_flags
& VLA_IN_SCOPE
) {
5069 /* save/restore stack pointer across label
5070 this is a no-op when combined with the load immediately
5071 after the label unless we arrive via goto */
5076 if (s
->r
== LABEL_DEFINED
)
5077 tcc_error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
5079 s
->r
= LABEL_DEFINED
;
5081 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
5084 if (vla_flags
& VLA_IN_SCOPE
) {
5085 gen_vla_sp_restore(*vla_sp_loc
);
5086 vla_flags
|= VLA_NEED_NEW_FRAME
;
5088 /* we accept this, but it is a mistake */
5091 tcc_warning("deprecated use of label at end of compound statement");
5095 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
5098 /* expression case */
5113 /* t is the array or struct type. c is the array or struct
5114 address. cur_index/cur_field is the pointer to the current
5115 value. 'size_only' is true if only size info is needed (only used
5117 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
5118 int *cur_index
, Sym
**cur_field
,
5122 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
5128 if (gnu_ext
&& (l
= is_label()) != 0)
5130 while (tok
== '[' || tok
== '.') {
5132 if (!(type
->t
& VT_ARRAY
))
5133 expect("array type");
5136 index
= expr_const();
5137 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
5138 expect("invalid index");
5139 if (tok
== TOK_DOTS
&& gnu_ext
) {
5141 index_last
= expr_const();
5142 if (index_last
< 0 ||
5143 (s
->c
>= 0 && index_last
>= s
->c
) ||
5145 expect("invalid index");
5151 *cur_index
= index_last
;
5152 type
= pointed_type(type
);
5153 elem_size
= type_size(type
, &align
);
5154 c
+= index
* elem_size
;
5155 /* NOTE: we only support ranges for last designator */
5156 nb_elems
= index_last
- index
+ 1;
5157 if (nb_elems
!= 1) {
5166 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
5167 expect("struct/union type");
5180 /* XXX: fix this mess by using explicit storage field */
5182 type1
.t
|= (type
->t
& ~VT_TYPE
);
5196 if (type
->t
& VT_ARRAY
) {
5198 type
= pointed_type(type
);
5199 c
+= index
* type_size(type
, &align
);
5203 tcc_error("too many field init");
5204 /* XXX: fix this mess by using explicit storage field */
5206 type1
.t
|= (type
->t
& ~VT_TYPE
);
5211 decl_initializer(type
, sec
, c
, 0, size_only
);
5213 /* XXX: make it more general */
5214 if (!size_only
&& nb_elems
> 1) {
5215 unsigned long c_end
;
5220 tcc_error("range init not supported yet for dynamic storage");
5221 c_end
= c
+ nb_elems
* elem_size
;
5222 if (c_end
> sec
->data_allocated
)
5223 section_realloc(sec
, c_end
);
5224 src
= sec
->data
+ c
;
5226 for(i
= 1; i
< nb_elems
; i
++) {
5228 memcpy(dst
, src
, elem_size
);
5234 #define EXPR_CONST 1
5237 /* store a value or an expression directly in global data or in local array */
5238 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
5239 int v
, int expr_type
)
5241 int saved_global_expr
, bt
, bit_pos
, bit_size
;
5243 unsigned long long bit_mask
;
5251 /* compound literals must be allocated globally in this case */
5252 saved_global_expr
= global_expr
;
5255 global_expr
= saved_global_expr
;
5256 /* NOTE: symbols are accepted */
5257 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
5258 tcc_error("initializer element is not constant");
5266 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
5269 /* XXX: not portable */
5270 /* XXX: generate error if incorrect relocation */
5271 gen_assign_cast(&dtype
);
5272 bt
= type
->t
& VT_BTYPE
;
5273 /* we'll write at most 12 bytes */
5274 if (c
+ 12 > sec
->data_allocated
) {
5275 section_realloc(sec
, c
+ 12);
5277 ptr
= sec
->data
+ c
;
5278 /* XXX: make code faster ? */
5279 if (!(type
->t
& VT_BITFIELD
)) {
5284 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5285 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
5286 bit_mask
= (1LL << bit_size
) - 1;
5288 if ((vtop
->r
& VT_SYM
) &&
5294 (bt
== VT_INT
&& bit_size
!= 32)))
5295 tcc_error("initializer element is not computable at load time");
5298 vtop
->c
.i
= (vtop
->c
.i
!= 0);
5300 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5303 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5306 *(double *)ptr
= vtop
->c
.d
;
5309 *(long double *)ptr
= vtop
->c
.ld
;
5312 *(long long *)ptr
|= (vtop
->c
.ll
& bit_mask
) << bit_pos
;
5315 if (vtop
->r
& VT_SYM
) {
5316 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
5318 *(addr_t
*)ptr
|= (vtop
->c
.ptr_offset
& bit_mask
) << bit_pos
;
5321 if (vtop
->r
& VT_SYM
) {
5322 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
5324 *(int *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5329 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
5336 /* put zeros for variable based init */
5337 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
5340 /* nothing to do because globals are already set to zero */
5342 vpush_global_sym(&func_old_type
, TOK_memset
);
5344 #ifdef TCC_TARGET_ARM
5355 /* 't' contains the type and storage info. 'c' is the offset of the
5356 object in section 'sec'. If 'sec' is NULL, it means stack based
5357 allocation. 'first' is true if array '{' must be read (multi
5358 dimension implicit array init handling). 'size_only' is true if
5359 size only evaluation is wanted (only for arrays). */
5360 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
5361 int first
, int size_only
)
5363 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, parlevel1
, i
;
5364 int size1
, align1
, expr_type
;
5368 if (type
->t
& VT_VLA
) {
5371 /* save current stack pointer */
5372 if (vla_flags
& VLA_NEED_NEW_FRAME
) {
5374 vla_flags
= VLA_IN_SCOPE
;
5375 vla_sp_loc
= &vla_sp_loc_tmp
;
5378 vla_runtime_type_size(type
, &a
);
5379 gen_vla_alloc(type
, a
);
5380 vset(type
, VT_LOCAL
|VT_LVAL
, c
);
5384 } else if (type
->t
& VT_ARRAY
) {
5388 t1
= pointed_type(type
);
5389 size1
= type_size(t1
, &align1
);
5392 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
5395 tcc_error("character array initializer must be a literal,"
5396 " optionally enclosed in braces");
5401 /* only parse strings here if correct type (otherwise: handle
5402 them as ((w)char *) expressions */
5403 if ((tok
== TOK_LSTR
&&
5404 #ifdef TCC_TARGET_PE
5405 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
5407 (t1
->t
& VT_BTYPE
) == VT_INT
5409 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
5410 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5415 /* compute maximum number of chars wanted */
5417 cstr_len
= cstr
->size
;
5419 cstr_len
= cstr
->size
/ sizeof(nwchar_t
);
5422 if (n
>= 0 && nb
> (n
- array_length
))
5423 nb
= n
- array_length
;
5426 tcc_warning("initializer-string for array is too long");
5427 /* in order to go faster for common case (char
5428 string in global variable, we handle it
5430 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
5431 memcpy(sec
->data
+ c
+ array_length
, cstr
->data
, nb
);
5435 ch
= ((unsigned char *)cstr
->data
)[i
];
5437 ch
= ((nwchar_t
*)cstr
->data
)[i
];
5438 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
5446 /* only add trailing zero if enough storage (no
5447 warning in this case since it is standard) */
5448 if (n
< 0 || array_length
< n
) {
5450 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
5456 while (tok
!= '}') {
5457 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
5458 if (n
>= 0 && index
>= n
)
5459 tcc_error("index too large");
5460 /* must put zero in holes (note that doing it that way
5461 ensures that it even works with designators) */
5462 if (!size_only
&& array_length
< index
) {
5463 init_putz(t1
, sec
, c
+ array_length
* size1
,
5464 (index
- array_length
) * size1
);
5467 if (index
> array_length
)
5468 array_length
= index
;
5469 /* special test for multi dimensional arrays (may not
5470 be strictly correct if designators are used at the
5472 if (index
>= n
&& no_oblock
)
5481 /* put zeros at the end */
5482 if (!size_only
&& n
>= 0 && array_length
< n
) {
5483 init_putz(t1
, sec
, c
+ array_length
* size1
,
5484 (n
- array_length
) * size1
);
5486 /* patch type size if needed */
5488 s
->c
= array_length
;
5489 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
5490 (sec
|| !first
|| tok
== '{')) {
5493 /* NOTE: the previous test is a specific case for automatic
5494 struct/union init */
5495 /* XXX: union needs only one init */
5497 /* XXX: this test is incorrect for local initializers
5498 beginning with ( without {. It would be much more difficult
5499 to do it correctly (ideally, the expression parser should
5500 be used in all cases) */
5506 while (tok
== '(') {
5510 if (!parse_btype(&type1
, &ad1
))
5512 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
5514 if (!is_assignable_types(type
, &type1
))
5515 tcc_error("invalid type for cast");
5520 if (first
|| tok
== '{') {
5529 while (tok
!= '}') {
5530 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
5532 if (!size_only
&& array_length
< index
) {
5533 init_putz(type
, sec
, c
+ array_length
,
5534 index
- array_length
);
5536 index
= index
+ type_size(&f
->type
, &align1
);
5537 if (index
> array_length
)
5538 array_length
= index
;
5540 /* gr: skip fields from same union - ugly. */
5542 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
5543 /* test for same offset */
5544 if (f
->next
->c
!= f
->c
)
5546 /* if yes, test for bitfield shift */
5547 if ((f
->type
.t
& VT_BITFIELD
) && (f
->next
->type
.t
& VT_BITFIELD
)) {
5548 int bit_pos_1
= (f
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5549 int bit_pos_2
= (f
->next
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5550 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
5551 if (bit_pos_1
!= bit_pos_2
)
5558 if (no_oblock
&& f
== NULL
)
5564 /* put zeros at the end */
5565 if (!size_only
&& array_length
< n
) {
5566 init_putz(type
, sec
, c
+ array_length
,
5575 } else if (tok
== '{') {
5577 decl_initializer(type
, sec
, c
, first
, size_only
);
5579 } else if (size_only
) {
5580 /* just skip expression */
5581 parlevel
= parlevel1
= 0;
5582 while ((parlevel
> 0 || parlevel1
> 0 ||
5583 (tok
!= '}' && tok
!= ',')) && tok
!= -1) {
5586 else if (tok
== ')')
5588 else if (tok
== '{')
5590 else if (tok
== '}')
5595 /* currently, we always use constant expression for globals
5596 (may change for scripting case) */
5597 expr_type
= EXPR_CONST
;
5599 expr_type
= EXPR_ANY
;
5600 init_putv(type
, sec
, c
, 0, expr_type
);
5604 /* parse an initializer for type 't' if 'has_init' is non zero, and
5605 allocate space in local or global data space ('r' is either
5606 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5607 variable 'v' with an associated name represented by 'asm_label' of
5608 scope 'scope' is declared before initializers are parsed. If 'v' is
5609 zero, then a reference to the new object is put in the value stack.
5610 If 'has_init' is 2, a special parsing is done to handle string
5612 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
5613 int has_init
, int v
, char *asm_label
,
5616 int size
, align
, addr
, data_offset
;
5618 ParseState saved_parse_state
= {0};
5619 TokenString init_str
;
5621 Sym
*flexible_array
;
5623 flexible_array
= NULL
;
5624 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
5625 Sym
*field
= type
->ref
->next
;
5628 field
= field
->next
;
5629 if (field
->type
.t
& VT_ARRAY
&& field
->type
.ref
->c
< 0)
5630 flexible_array
= field
;
5634 size
= type_size(type
, &align
);
5635 /* If unknown size, we must evaluate it before
5636 evaluating initializers because
5637 initializers can generate global data too
5638 (e.g. string pointers or ISOC99 compound
5639 literals). It also simplifies local
5640 initializers handling */
5641 tok_str_new(&init_str
);
5642 if (size
< 0 || (flexible_array
&& has_init
)) {
5644 tcc_error("unknown type size");
5645 /* get all init string */
5646 if (has_init
== 2) {
5647 /* only get strings */
5648 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5649 tok_str_add_tok(&init_str
);
5654 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
5656 tcc_error("unexpected end of file in initializer");
5657 tok_str_add_tok(&init_str
);
5660 else if (tok
== '}') {
5670 tok_str_add(&init_str
, -1);
5671 tok_str_add(&init_str
, 0);
5674 save_parse_state(&saved_parse_state
);
5676 macro_ptr
= init_str
.str
;
5678 decl_initializer(type
, NULL
, 0, 1, 1);
5679 /* prepare second initializer parsing */
5680 macro_ptr
= init_str
.str
;
5683 /* if still unknown size, error */
5684 size
= type_size(type
, &align
);
5686 tcc_error("unknown type size");
5689 size
+= flexible_array
->type
.ref
->c
* pointed_size(&flexible_array
->type
);
5690 /* take into account specified alignment if bigger */
5691 if (ad
->a
.aligned
) {
5692 if (ad
->a
.aligned
> align
)
5693 align
= ad
->a
.aligned
;
5694 } else if (ad
->a
.packed
) {
5697 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
5699 #ifdef CONFIG_TCC_BCHECK
5700 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5704 addr
= loc_stack(size
, 1);
5705 #ifdef CONFIG_TCC_BCHECK
5706 /* handles bounds */
5707 /* XXX: currently, since we do only one pass, we cannot track
5708 '&' operators, so we add only arrays */
5709 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5710 unsigned long *bounds_ptr
;
5711 /* add padding between regions */
5713 /* then add local bound info */
5714 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(unsigned long));
5715 bounds_ptr
[0] = addr
;
5716 bounds_ptr
[1] = size
;
5720 /* local variable */
5721 sym_push(v
, type
, r
, addr
);
5723 /* push local reference */
5724 vset(type
, r
, addr
);
5730 if (v
&& scope
== VT_CONST
) {
5731 /* see if the symbol was already defined */
5734 if (!is_compatible_types(&sym
->type
, type
))
5735 tcc_error("incompatible types for redefinition of '%s'",
5736 get_tok_str(v
, NULL
));
5737 if (sym
->type
.t
& VT_EXTERN
) {
5738 /* if the variable is extern, it was not allocated */
5739 sym
->type
.t
&= ~VT_EXTERN
;
5740 /* set array size if it was omitted in extern
5742 if ((sym
->type
.t
& VT_ARRAY
) &&
5743 sym
->type
.ref
->c
< 0 &&
5745 sym
->type
.ref
->c
= type
->ref
->c
;
5747 /* we accept several definitions of the same
5748 global variable. this is tricky, because we
5749 must play with the SHN_COMMON type of the symbol */
5750 /* XXX: should check if the variable was already
5751 initialized. It is incorrect to initialized it
5753 /* no init data, we won't add more to the symbol */
5760 /* allocate symbol in corresponding section */
5765 else if (tcc_state
->nocommon
)
5769 data_offset
= sec
->data_offset
;
5770 data_offset
= (data_offset
+ align
- 1) & -align
;
5772 /* very important to increment global pointer at this time
5773 because initializers themselves can create new initializers */
5774 data_offset
+= size
;
5775 #ifdef CONFIG_TCC_BCHECK
5776 /* add padding if bound check */
5777 if (tcc_state
->do_bounds_check
)
5780 sec
->data_offset
= data_offset
;
5781 /* allocate section space to put the data */
5782 if (sec
->sh_type
!= SHT_NOBITS
&&
5783 data_offset
> sec
->data_allocated
)
5784 section_realloc(sec
, data_offset
);
5785 /* align section if needed */
5786 if (align
> sec
->sh_addralign
)
5787 sec
->sh_addralign
= align
;
5789 addr
= 0; /* avoid warning */
5793 if (scope
!= VT_CONST
|| !sym
) {
5794 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
5795 sym
->asm_label
= asm_label
;
5797 /* update symbol definition */
5799 put_extern_sym(sym
, sec
, addr
, size
);
5802 /* put a common area */
5803 put_extern_sym(sym
, NULL
, align
, size
);
5804 /* XXX: find a nicer way */
5805 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
5806 esym
->st_shndx
= SHN_COMMON
;
5809 /* push global reference */
5810 sym
= get_sym_ref(type
, sec
, addr
, size
);
5811 vpushsym(type
, sym
);
5813 /* patch symbol weakness */
5814 if (type
->t
& VT_WEAK
)
5816 apply_visibility(sym
, type
);
5817 #ifdef CONFIG_TCC_BCHECK
5818 /* handles bounds now because the symbol must be defined
5819 before for the relocation */
5820 if (tcc_state
->do_bounds_check
) {
5821 unsigned long *bounds_ptr
;
5823 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
5824 /* then add global bound info */
5825 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(long));
5826 bounds_ptr
[0] = 0; /* relocated */
5827 bounds_ptr
[1] = size
;
5831 if (has_init
|| (type
->t
& VT_VLA
)) {
5832 decl_initializer(type
, sec
, addr
, 1, 0);
5833 /* restore parse state if needed */
5835 tok_str_free(init_str
.str
);
5836 restore_parse_state(&saved_parse_state
);
5838 /* patch flexible array member size back to -1, */
5839 /* for possible subsequent similar declarations */
5841 flexible_array
->type
.ref
->c
= -1;
5846 static void put_func_debug(Sym
*sym
)
5851 /* XXX: we put here a dummy type */
5852 snprintf(buf
, sizeof(buf
), "%s:%c1",
5853 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
5854 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
5855 cur_text_section
, sym
->c
);
5856 /* //gr gdb wants a line at the function */
5857 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
5862 /* parse an old style function declaration list */
5863 /* XXX: check multiple parameter */
5864 static void func_decl_list(Sym
*func_sym
)
5871 /* parse each declaration */
5872 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
&&
5873 tok
!= TOK_ASM1
&& tok
!= TOK_ASM2
&& tok
!= TOK_ASM3
) {
5874 if (!parse_btype(&btype
, &ad
))
5875 expect("declaration list");
5876 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5877 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5879 /* we accept no variable after */
5883 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5884 /* find parameter in function parameter list */
5887 if ((s
->v
& ~SYM_FIELD
) == v
)
5891 tcc_error("declaration for parameter '%s' but no such parameter",
5892 get_tok_str(v
, NULL
));
5894 /* check that no storage specifier except 'register' was given */
5895 if (type
.t
& VT_STORAGE
)
5896 tcc_error("storage class specified for '%s'", get_tok_str(v
, NULL
));
5897 convert_parameter_type(&type
);
5898 /* we can add the type (NOTE: it could be local to the function) */
5900 /* accept other parameters */
5911 /* parse a function defined by symbol 'sym' and generate its code in
5912 'cur_text_section' */
5913 static void gen_function(Sym
*sym
)
5915 int saved_nocode_wanted
= nocode_wanted
;
5917 ind
= cur_text_section
->data_offset
;
5918 /* NOTE: we patch the symbol size later */
5919 put_extern_sym(sym
, cur_text_section
, ind
, 0);
5920 funcname
= get_tok_str(sym
->v
, NULL
);
5922 /* Initialize VLA state */
5923 vla_sp_loc
= &vla_sp_root_loc
;
5924 vla_flags
= VLA_NEED_NEW_FRAME
;
5925 /* put debug symbol */
5926 if (tcc_state
->do_debug
)
5927 put_func_debug(sym
);
5928 /* push a dummy symbol to enable local sym storage */
5929 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
5930 gfunc_prolog(&sym
->type
);
5931 #ifdef CONFIG_TCC_BCHECK
5932 if (tcc_state
->do_bounds_check
5933 && !strcmp(get_tok_str(sym
->v
, NULL
), "main")) {
5937 for (i
= 0, sym
= local_stack
; i
< 2; i
++, sym
= sym
->prev
) {
5938 if (sym
->v
& SYM_FIELD
|| sym
->prev
->v
& SYM_FIELD
)
5940 vpush_global_sym(&func_old_type
, TOK___bound_main_arg
);
5941 vset(&sym
->type
, sym
->r
, sym
->c
);
5947 block(NULL
, NULL
, NULL
, NULL
, 0, 0);
5950 cur_text_section
->data_offset
= ind
;
5951 label_pop(&global_label_stack
, NULL
);
5952 /* reset local stack */
5953 scope_stack_bottom
= NULL
;
5954 sym_pop(&local_stack
, NULL
);
5955 /* end of function */
5956 /* patch symbol size */
5957 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
5959 /* patch symbol weakness (this definition overrules any prototype) */
5960 if (sym
->type
.t
& VT_WEAK
)
5962 apply_visibility(sym
, &sym
->type
);
5963 if (tcc_state
->do_debug
) {
5964 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
5966 /* It's better to crash than to generate wrong code */
5967 cur_text_section
= NULL
;
5968 funcname
= ""; /* for safety */
5969 func_vt
.t
= VT_VOID
; /* for safety */
5970 func_var
= 0; /* for safety */
5971 ind
= 0; /* for safety */
5972 nocode_wanted
= saved_nocode_wanted
;
5975 ST_FUNC
void gen_inline_functions(void)
5978 int *str
, inline_generated
, i
;
5979 struct InlineFunc
*fn
;
5981 /* iterate while inline function are referenced */
5983 inline_generated
= 0;
5984 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5985 fn
= tcc_state
->inline_fns
[i
];
5987 if (sym
&& sym
->c
) {
5988 /* the function was used: generate its code and
5989 convert it to a normal function */
5990 str
= fn
->token_str
;
5993 pstrcpy(file
->filename
, sizeof file
->filename
, fn
->filename
);
5994 sym
->r
= VT_SYM
| VT_CONST
;
5995 sym
->type
.t
&= ~VT_INLINE
;
5999 cur_text_section
= text_section
;
6001 macro_ptr
= NULL
; /* fail safe */
6003 inline_generated
= 1;
6006 if (!inline_generated
)
6009 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
6010 fn
= tcc_state
->inline_fns
[i
];
6011 str
= fn
->token_str
;
6014 dynarray_reset(&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
);
6017 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
6018 static int decl0(int l
, int is_for_loop_init
)
6026 if (!parse_btype(&btype
, &ad
)) {
6027 if (is_for_loop_init
)
6029 /* skip redundant ';' */
6030 /* XXX: find more elegant solution */
6035 if (l
== VT_CONST
&&
6036 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
6037 /* global asm block */
6041 /* special test for old K&R protos without explicit int
6042 type. Only accepted when defining global data */
6043 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
6047 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
6048 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
6050 /* we accept no variable after */
6054 while (1) { /* iterate thru each declaration */
6055 char *asm_label
; // associated asm label
6057 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
6061 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
6062 printf("type = '%s'\n", buf
);
6065 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
6066 if ((type
.t
& VT_STATIC
) && (l
== VT_LOCAL
)) {
6067 tcc_error("function without file scope cannot be static");
6069 /* if old style function prototype, we accept a
6072 if (sym
->c
== FUNC_OLD
)
6073 func_decl_list(sym
);
6077 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
6080 asm_label_instr(&astr
);
6081 asm_label
= tcc_strdup(astr
.data
);
6084 /* parse one last attribute list, after asm label */
6085 parse_attribute(&ad
);
6090 #ifdef TCC_TARGET_PE
6091 if (ad
.a
.func_import
)
6092 type
.t
|= VT_IMPORT
;
6093 if (ad
.a
.func_export
)
6094 type
.t
|= VT_EXPORT
;
6096 type
.t
|= ad
.a
.visibility
<< VT_VIS_SHIFT
;
6100 tcc_error("cannot use local functions");
6101 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
6102 expect("function definition");
6104 /* reject abstract declarators in function definition */
6106 while ((sym
= sym
->next
) != NULL
)
6107 if (!(sym
->v
& ~SYM_FIELD
))
6108 expect("identifier");
6110 /* XXX: cannot do better now: convert extern line to static inline */
6111 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
6112 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
6117 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
6120 ref
= sym
->type
.ref
;
6121 if (0 == ref
->a
.func_proto
)
6122 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
6124 /* use func_call from prototype if not defined */
6125 if (ref
->a
.func_call
!= FUNC_CDECL
6126 && type
.ref
->a
.func_call
== FUNC_CDECL
)
6127 type
.ref
->a
.func_call
= ref
->a
.func_call
;
6129 /* use export from prototype */
6130 if (ref
->a
.func_export
)
6131 type
.ref
->a
.func_export
= 1;
6133 /* use static from prototype */
6134 if (sym
->type
.t
& VT_STATIC
)
6135 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
6137 /* If the definition has no visibility use the
6138 one from prototype. */
6139 if (! (type
.t
& VT_VIS_MASK
))
6140 type
.t
|= sym
->type
.t
& VT_VIS_MASK
;
6142 if (!is_compatible_types(&sym
->type
, &type
)) {
6144 tcc_error("incompatible types for redefinition of '%s'",
6145 get_tok_str(v
, NULL
));
6147 type
.ref
->a
.func_proto
= 0;
6148 /* if symbol is already defined, then put complete type */
6151 /* put function symbol */
6152 sym
= global_identifier_push(v
, type
.t
, 0);
6153 sym
->type
.ref
= type
.ref
;
6156 /* static inline functions are just recorded as a kind
6157 of macro. Their code will be emitted at the end of
6158 the compilation unit only if they are used */
6159 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
6160 (VT_INLINE
| VT_STATIC
)) {
6161 TokenString func_str
;
6163 struct InlineFunc
*fn
;
6164 const char *filename
;
6166 tok_str_new(&func_str
);
6172 tcc_error("unexpected end of file");
6173 tok_str_add_tok(&func_str
);
6178 } else if (t
== '}') {
6180 if (block_level
== 0)
6184 tok_str_add(&func_str
, -1);
6185 tok_str_add(&func_str
, 0);
6186 filename
= file
? file
->filename
: "";
6187 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
6188 strcpy(fn
->filename
, filename
);
6190 fn
->token_str
= func_str
.str
;
6191 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
6194 /* compute text section */
6195 cur_text_section
= ad
.section
;
6196 if (!cur_text_section
)
6197 cur_text_section
= text_section
;
6198 sym
->r
= VT_SYM
| VT_CONST
;
6203 if (btype
.t
& VT_TYPEDEF
) {
6204 /* save typedefed type */
6205 /* XXX: test storage specifiers ? */
6206 sym
= sym_push(v
, &type
, 0, 0);
6208 sym
->type
.t
|= VT_TYPEDEF
;
6211 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
6212 /* external function definition */
6213 /* specific case for func_call attribute */
6214 ad
.a
.func_proto
= 1;
6216 } else if (!(type
.t
& VT_ARRAY
)) {
6217 /* not lvalue if array */
6218 r
|= lvalue_type(type
.t
);
6220 has_init
= (tok
== '=');
6221 if (has_init
&& (type
.t
& VT_VLA
))
6222 tcc_error("Variable length array cannot be initialized");
6223 if ((btype
.t
& VT_EXTERN
) || ((type
.t
& VT_BTYPE
) == VT_FUNC
) ||
6224 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
6225 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
6226 /* external variable or function */
6227 /* NOTE: as GCC, uninitialized global static
6228 arrays of null size are considered as
6230 sym
= external_sym(v
, &type
, r
, asm_label
);
6232 if (ad
.alias_target
) {
6237 alias_target
= sym_find(ad
.alias_target
);
6238 if (!alias_target
|| !alias_target
->c
)
6239 tcc_error("unsupported forward __alias__ attribute");
6240 esym
= &((Elf32_Sym
*)symtab_section
->data
)[alias_target
->c
];
6241 tsec
.sh_num
= esym
->st_shndx
;
6242 put_extern_sym2(sym
, &tsec
, esym
->st_value
, esym
->st_size
, 0);
6245 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
6246 if (type
.t
& VT_STATIC
)
6252 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, asm_label
, l
);
6256 if (is_for_loop_init
)
6269 ST_FUNC
void decl(int l
)