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
;
876 if((rc
& ~RC_MASK
) && (rc
!= RC_ST0
))
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
);
2627 #ifndef TCC_TARGET_X86_64
2630 vtop
->type
.t
= VT_PTR
;
2633 /* address of memcpy() */
2634 # ifdef TCC_ARM_EABI
2636 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2637 else if(!(align
& 3))
2638 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2641 vpush_global_sym(&func_old_type
, TOK_memcpy
);
2646 vtop
->type
.t
= VT_PTR
;
2654 vtop
->type
.t
= VT_PTR
;
2658 vtop
->type
.t
= VT_PTR
;
2662 struct_copy(&vtop
[-2], &vtop
[-1], &vtop
[0]);
2670 /* leave source on stack */
2671 } else if (ft
& VT_BITFIELD
) {
2672 /* bitfield store handling */
2673 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2674 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2675 /* remove bit field info to avoid loops */
2676 vtop
[-1].type
.t
= ft
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
2677 /* duplicate source into other register */
2678 if(dbt
== VT_BOOL
) {
2679 gen_cast(&vtop
[-1].type
);
2680 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2683 /* duplicate destination */
2685 vtop
[-1] = vtop
[-2];
2687 /* mask and shift source */
2688 if(dbt
!= VT_BOOL
) {
2689 if(dbt
== VT_LLONG
) {
2690 vpushll((1ULL << bit_size
) - 1ULL);
2692 vpushi((1 << bit_size
) - 1);
2698 /* load destination, mask and or with source */
2700 if(dbt
== VT_LLONG
) {
2701 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2703 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2710 #ifdef CONFIG_TCC_BCHECK
2711 /* bound check case */
2712 if (vtop
[-1].r
& VT_MUSTBOUND
) {
2718 if (!nocode_wanted
) {
2722 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
2723 vtop
->r
|= delayed_cast
;
2727 /* post defines POST/PRE add. c is the token ++ or -- */
2728 ST_FUNC
void inc(int post
, int c
)
2731 vdup(); /* save lvalue */
2733 gv_dup(); /* duplicate value */
2738 vpushi(c
- TOK_MID
);
2740 vstore(); /* store value */
2742 vpop(); /* if post op, return saved value */
2745 /* Parse GNUC __attribute__ extension. Currently, the following
2746 extensions are recognized:
2747 - aligned(n) : set data/function alignment.
2748 - packed : force data alignment to 1
2749 - section(x) : generate data/code in this section.
2750 - unused : currently ignored, but may be used someday.
2751 - regparm(n) : pass function parameters in registers (i386 only)
2753 static void parse_attribute(AttributeDef
*ad
)
2757 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
2761 while (tok
!= ')') {
2762 if (tok
< TOK_IDENT
)
2763 expect("attribute name");
2771 expect("section name");
2772 ad
->section
= find_section(tcc_state
, (char *)tokc
.cstr
->data
);
2780 expect("alias(\"target\")");
2781 ad
->alias_target
= /* save string as token, for later */
2782 tok_alloc((char*)tokc
.cstr
->data
, tokc
.cstr
->size
-1)->tok
;
2786 case TOK_VISIBILITY1
:
2787 case TOK_VISIBILITY2
:
2790 expect("visibility(\"default|hidden|internal|protected\")");
2791 if (!strcmp (tokc
.cstr
->data
, "default"))
2792 ad
->a
.visibility
= STV_DEFAULT
;
2793 else if (!strcmp (tokc
.cstr
->data
, "hidden"))
2794 ad
->a
.visibility
= STV_HIDDEN
;
2795 else if (!strcmp (tokc
.cstr
->data
, "internal"))
2796 ad
->a
.visibility
= STV_INTERNAL
;
2797 else if (!strcmp (tokc
.cstr
->data
, "protected"))
2798 ad
->a
.visibility
= STV_PROTECTED
;
2800 expect("visibility(\"default|hidden|internal|protected\")");
2809 if (n
<= 0 || (n
& (n
- 1)) != 0)
2810 tcc_error("alignment must be a positive power of two");
2827 /* currently, no need to handle it because tcc does not
2828 track unused objects */
2832 /* currently, no need to handle it because tcc does not
2833 track unused objects */
2838 ad
->a
.func_call
= FUNC_CDECL
;
2843 ad
->a
.func_call
= FUNC_STDCALL
;
2845 #ifdef TCC_TARGET_I386
2855 ad
->a
.func_call
= FUNC_FASTCALL1
+ n
- 1;
2861 ad
->a
.func_call
= FUNC_FASTCALLW
;
2868 ad
->a
.mode
= VT_LLONG
+ 1;
2871 ad
->a
.mode
= VT_SHORT
+ 1;
2874 ad
->a
.mode
= VT_INT
+ 1;
2877 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
2884 ad
->a
.func_export
= 1;
2887 ad
->a
.func_import
= 1;
2890 if (tcc_state
->warn_unsupported
)
2891 tcc_warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
2892 /* skip parameters */
2894 int parenthesis
= 0;
2898 else if (tok
== ')')
2901 } while (parenthesis
&& tok
!= -1);
2914 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2915 static void struct_decl(CType
*type
, int u
, int tdef
)
2917 int a
, v
, size
, align
, maxalign
, c
, offset
, flexible
;
2918 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
, prevbt
;
2919 Sym
*s
, *ss
, *ass
, **ps
;
2923 a
= tok
; /* save decl type */
2928 /* struct already defined ? return it */
2930 expect("struct/union/enum name");
2934 tcc_error("invalid type");
2936 } else if (tok
>= TOK_IDENT
&& !tdef
)
2937 tcc_error("unknown struct/union/enum");
2943 /* we put an undefined size for struct/union */
2944 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
2945 s
->r
= 0; /* default alignment is zero as gcc */
2946 /* put struct/union/enum name in type */
2954 tcc_error("struct/union/enum already defined");
2955 /* cannot be empty */
2957 /* non empty enums are not allowed */
2958 if (a
== TOK_ENUM
) {
2962 expect("identifier");
2964 if (ss
&& !local_stack
)
2965 tcc_error("redefinition of enumerator '%s'",
2966 get_tok_str(v
, NULL
));
2972 /* enum symbols have static storage */
2973 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
2974 ss
->type
.t
|= VT_STATIC
;
2979 /* NOTE: we accept a trailing comma */
2983 s
->c
= type_size(&int_type
, &align
);
2992 while (tok
!= '}') {
2993 parse_btype(&btype
, &ad
);
2996 tcc_error("flexible array member '%s' not at the end of struct",
2997 get_tok_str(v
, NULL
));
3002 type_decl(&type1
, &ad
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3003 if (v
== 0 && (type1
.t
& VT_BTYPE
) != VT_STRUCT
)
3004 expect("identifier");
3005 if (type_size(&type1
, &align
) < 0) {
3006 if ((a
== TOK_STRUCT
) && (type1
.t
& VT_ARRAY
) && c
)
3009 tcc_error("field '%s' has incomplete type",
3010 get_tok_str(v
, NULL
));
3012 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
3013 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
3014 tcc_error("invalid type for '%s'",
3015 get_tok_str(v
, NULL
));
3019 bit_size
= expr_const();
3020 /* XXX: handle v = 0 case for messages */
3022 tcc_error("negative width in bit-field '%s'",
3023 get_tok_str(v
, NULL
));
3024 if (v
&& bit_size
== 0)
3025 tcc_error("zero width for bit-field '%s'",
3026 get_tok_str(v
, NULL
));
3028 size
= type_size(&type1
, &align
);
3030 if (align
< ad
.a
.aligned
)
3031 align
= ad
.a
.aligned
;
3032 } else if (ad
.a
.packed
) {
3034 } else if (*tcc_state
->pack_stack_ptr
) {
3035 if (align
> *tcc_state
->pack_stack_ptr
)
3036 align
= *tcc_state
->pack_stack_ptr
;
3039 if (bit_size
>= 0) {
3040 bt
= type1
.t
& VT_BTYPE
;
3047 tcc_error("bitfields must have scalar type");
3049 if (bit_size
> bsize
) {
3050 tcc_error("width of '%s' exceeds its type",
3051 get_tok_str(v
, NULL
));
3052 } else if (bit_size
== bsize
) {
3053 /* no need for bit fields */
3055 } else if (bit_size
== 0) {
3056 /* XXX: what to do if only padding in a
3058 /* zero size: means to pad */
3061 /* we do not have enough room ?
3062 did the type change?
3064 if ((bit_pos
+ bit_size
) > bsize
||
3065 bt
!= prevbt
|| a
== TOK_UNION
)
3068 /* XXX: handle LSB first */
3069 type1
.t
|= VT_BITFIELD
|
3070 (bit_pos
<< VT_STRUCT_SHIFT
) |
3071 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
3072 bit_pos
+= bit_size
;
3078 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
3079 /* add new memory data only if starting
3081 if (lbit_pos
== 0) {
3082 if (a
== TOK_STRUCT
) {
3083 c
= (c
+ align
- 1) & -align
;
3092 if (align
> maxalign
)
3096 printf("add field %s offset=%d",
3097 get_tok_str(v
, NULL
), offset
);
3098 if (type1
.t
& VT_BITFIELD
) {
3099 printf(" pos=%d size=%d",
3100 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
3101 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
3106 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
3108 while ((ass
= ass
->next
) != NULL
) {
3109 ss
= sym_push(ass
->v
, &ass
->type
, 0, offset
+ ass
->c
);
3114 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
3118 if (tok
== ';' || tok
== TOK_EOF
)
3125 /* store size and alignment */
3126 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
3132 /* return 1 if basic type is a type size (short, long, long long) */
3133 ST_FUNC
int is_btype_size(int bt
)
3135 return bt
== VT_SHORT
|| bt
== VT_LONG
|| bt
== VT_LLONG
;
3138 /* return 0 if no type declaration. otherwise, return the basic type
3141 static int parse_btype(CType
*type
, AttributeDef
*ad
)
3143 int t
, u
, bt_size
, complete
, type_found
, typespec_found
;
3147 memset(ad
, 0, sizeof(AttributeDef
));
3155 /* currently, we really ignore extension */
3166 tcc_error("too many basic types");
3168 bt_size
= is_btype_size (u
& VT_BTYPE
);
3169 if (u
== VT_INT
|| (!bt_size
&& !(t
& VT_TYPEDEF
)))
3184 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
3185 #ifndef TCC_TARGET_PE
3186 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3188 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
3189 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3203 if ((t
& VT_BTYPE
) == VT_LONG
) {
3204 #ifdef TCC_TARGET_PE
3205 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
3207 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3215 struct_decl(&type1
, VT_ENUM
, t
& VT_TYPEDEF
);
3218 type
->ref
= type1
.ref
;
3222 struct_decl(&type1
, VT_STRUCT
, t
& VT_TYPEDEF
);
3225 /* type modifiers */
3241 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == (VT_DEFSIGN
|VT_UNSIGNED
))
3242 tcc_error("signed and unsigned modifier");
3255 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == VT_DEFSIGN
)
3256 tcc_error("signed and unsigned modifier");
3257 t
|= VT_DEFSIGN
| VT_UNSIGNED
;
3282 /* GNUC attribute */
3283 case TOK_ATTRIBUTE1
:
3284 case TOK_ATTRIBUTE2
:
3285 parse_attribute(ad
);
3288 t
= (t
& ~VT_BTYPE
) | u
;
3296 parse_expr_type(&type1
);
3297 /* remove all storage modifiers except typedef */
3298 type1
.t
&= ~(VT_STORAGE
&~VT_TYPEDEF
);
3304 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
3306 t
|= (s
->type
.t
& ~VT_TYPEDEF
);
3307 type
->ref
= s
->type
.ref
;
3309 /* get attributes from typedef */
3310 if (0 == ad
->a
.aligned
)
3311 ad
->a
.aligned
= s
->a
.aligned
;
3312 if (0 == ad
->a
.func_call
)
3313 ad
->a
.func_call
= s
->a
.func_call
;
3314 ad
->a
.packed
|= s
->a
.packed
;
3323 if (tcc_state
->char_is_unsigned
) {
3324 if ((t
& (VT_DEFSIGN
|VT_BTYPE
)) == VT_BYTE
)
3328 /* long is never used as type */
3329 if ((t
& VT_BTYPE
) == VT_LONG
)
3330 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
3331 t
= (t
& ~VT_BTYPE
) | VT_INT
;
3333 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3339 /* convert a function parameter type (array to pointer and function to
3340 function pointer) */
3341 static inline void convert_parameter_type(CType
*pt
)
3343 /* remove const and volatile qualifiers (XXX: const could be used
3344 to indicate a const function parameter */
3345 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3346 /* array must be transformed to pointer according to ANSI C */
3348 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
3353 ST_FUNC
void parse_asm_str(CString
*astr
)
3356 /* read the string */
3358 expect("string constant");
3360 while (tok
== TOK_STR
) {
3361 /* XXX: add \0 handling too ? */
3362 cstr_cat(astr
, tokc
.cstr
->data
);
3365 cstr_ccat(astr
, '\0');
3368 /* Parse an asm label and return the label
3369 * Don't forget to free the CString in the caller! */
3370 static void asm_label_instr(CString
*astr
)
3373 parse_asm_str(astr
);
3376 printf("asm_alias: \"%s\"\n", (char *)astr
->data
);
3380 static void post_type(CType
*type
, AttributeDef
*ad
)
3382 int n
, l
, t1
, arg_size
, size
, align
;
3383 Sym
**plast
, *s
, *first
;
3388 /* function declaration */
3396 /* read param name and compute offset */
3397 if (l
!= FUNC_OLD
) {
3398 if (!parse_btype(&pt
, &ad1
)) {
3400 tcc_error("invalid type");
3407 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
3409 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3410 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
3411 tcc_error("parameter declared as void");
3412 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
3417 expect("identifier");
3421 convert_parameter_type(&pt
);
3422 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
3428 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
3435 /* if no parameters, then old type prototype */
3439 /* NOTE: const is ignored in returned type as it has a special
3440 meaning in gcc / C++ */
3441 type
->t
&= ~VT_CONSTANT
;
3442 /* some ancient pre-K&R C allows a function to return an array
3443 and the array brackets to be put after the arguments, such
3444 that "int c()[]" means something like "int[] c()" */
3447 skip(']'); /* only handle simple "[]" */
3450 /* we push a anonymous symbol which will contain the function prototype */
3451 ad
->a
.func_args
= arg_size
;
3452 s
= sym_push(SYM_FIELD
, type
, 0, l
);
3457 } else if (tok
== '[') {
3458 /* array definition */
3460 if (tok
== TOK_RESTRICT1
)
3465 if (!local_stack
|| nocode_wanted
)
3466 vpushi(expr_const());
3468 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3471 tcc_error("invalid array size");
3473 if (!is_integer_btype(vtop
->type
.t
& VT_BTYPE
))
3474 tcc_error("size of variable length array should be an integer");
3479 /* parse next post type */
3480 post_type(type
, ad
);
3481 if (type
->t
== VT_FUNC
)
3482 tcc_error("declaration of an array of functions");
3483 t1
|= type
->t
& VT_VLA
;
3486 size
= type_size(&int_type
, &align
);
3487 n
= loc_stack(size
, 1);
3489 vla_runtime_type_size(type
, &align
);
3491 vset(&int_type
, VT_LOCAL
|VT_LVAL
, n
);
3498 /* we push an anonymous symbol which will contain the array
3500 s
= sym_push(SYM_FIELD
, type
, 0, n
);
3501 type
->t
= (t1
? VT_VLA
: VT_ARRAY
) | VT_PTR
;
3506 /* Parse a type declaration (except basic type), and return the type
3507 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3508 expected. 'type' should contain the basic type. 'ad' is the
3509 attribute definition of the basic type. It can be modified by
3512 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
3515 CType type1
, *type2
;
3516 int qualifiers
, storage
;
3518 while (tok
== '*') {
3526 qualifiers
|= VT_CONSTANT
;
3531 qualifiers
|= VT_VOLATILE
;
3539 type
->t
|= qualifiers
;
3542 /* XXX: clarify attribute handling */
3543 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3544 parse_attribute(ad
);
3546 /* recursive type */
3547 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3548 type1
.t
= 0; /* XXX: same as int */
3551 /* XXX: this is not correct to modify 'ad' at this point, but
3552 the syntax is not clear */
3553 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3554 parse_attribute(ad
);
3555 type_decl(&type1
, ad
, v
, td
);
3558 /* type identifier */
3559 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
3563 if (!(td
& TYPE_ABSTRACT
))
3564 expect("identifier");
3568 storage
= type
->t
& VT_STORAGE
;
3569 type
->t
&= ~VT_STORAGE
;
3570 if (storage
& VT_STATIC
) {
3571 int saved_nocode_wanted
= nocode_wanted
;
3573 post_type(type
, ad
);
3574 nocode_wanted
= saved_nocode_wanted
;
3576 post_type(type
, ad
);
3578 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3579 parse_attribute(ad
);
3583 /* append type at the end of type1 */
3596 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3597 ST_FUNC
int lvalue_type(int t
)
3602 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
3604 else if (bt
== VT_SHORT
)
3608 if (t
& VT_UNSIGNED
)
3609 r
|= VT_LVAL_UNSIGNED
;
3613 /* indirection with full error checking and bound check */
3614 ST_FUNC
void indir(void)
3616 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
3617 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
3621 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
3623 vtop
->type
= *pointed_type(&vtop
->type
);
3624 /* Arrays and functions are never lvalues */
3625 if (!(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_VLA
)
3626 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3627 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3628 /* if bound checking, the referenced pointer must be checked */
3629 #ifdef CONFIG_TCC_BCHECK
3630 if (tcc_state
->do_bounds_check
)
3631 vtop
->r
|= VT_MUSTBOUND
;
3636 /* pass a parameter to a function and do type checking and casting */
3637 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
3642 func_type
= func
->c
;
3643 if (func_type
== FUNC_OLD
||
3644 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
3645 /* default casting : only need to convert float to double */
3646 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
3649 } else if (vtop
->type
.t
& VT_BITFIELD
) {
3650 type
.t
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
3653 } else if (arg
== NULL
) {
3654 tcc_error("too many arguments to function");
3657 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
3658 gen_assign_cast(&type
);
3662 /* parse an expression of the form '(type)' or '(expr)' and return its
3664 static void parse_expr_type(CType
*type
)
3670 if (parse_btype(type
, &ad
)) {
3671 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3678 static void parse_type(CType
*type
)
3683 if (!parse_btype(type
, &ad
)) {
3686 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3689 static void vpush_tokc(int t
)
3694 vsetc(&type
, VT_CONST
, &tokc
);
3697 ST_FUNC
void unary(void)
3699 int n
, t
, align
, size
, r
, sizeof_caller
;
3703 static int in_sizeof
= 0;
3705 sizeof_caller
= in_sizeof
;
3707 /* XXX: GCC 2.95.3 does not generate a table although it should be
3721 vpush_tokc(VT_INT
| VT_UNSIGNED
);
3725 vpush_tokc(VT_LLONG
);
3729 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
3733 vpush_tokc(VT_FLOAT
);
3737 vpush_tokc(VT_DOUBLE
);
3741 vpush_tokc(VT_LDOUBLE
);
3744 case TOK___FUNCTION__
:
3746 goto tok_identifier
;
3752 /* special function name identifier */
3753 len
= strlen(funcname
) + 1;
3754 /* generate char[len] type */
3759 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
3760 ptr
= section_ptr_add(data_section
, len
);
3761 memcpy(ptr
, funcname
, len
);
3766 #ifdef TCC_TARGET_PE
3767 t
= VT_SHORT
| VT_UNSIGNED
;
3773 /* string parsing */
3776 if (tcc_state
->warn_write_strings
)
3781 memset(&ad
, 0, sizeof(AttributeDef
));
3782 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, NULL
, 0);
3787 if (parse_btype(&type
, &ad
)) {
3788 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
3790 /* check ISOC99 compound literal */
3792 /* data is allocated locally by default */
3797 /* all except arrays are lvalues */
3798 if (!(type
.t
& VT_ARRAY
))
3799 r
|= lvalue_type(type
.t
);
3800 memset(&ad
, 0, sizeof(AttributeDef
));
3801 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, NULL
, 0);
3803 if (sizeof_caller
) {
3810 } else if (tok
== '{') {
3811 /* save all registers */
3813 /* statement expression : we do not accept break/continue
3814 inside as GCC does */
3815 block(NULL
, NULL
, NULL
, NULL
, 0, 1);
3830 /* functions names must be treated as function pointers,
3831 except for unary '&' and sizeof. Since we consider that
3832 functions are not lvalues, we only have to handle it
3833 there and in function calls. */
3834 /* arrays can also be used although they are not lvalues */
3835 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
3836 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
3838 mk_pointer(&vtop
->type
);
3844 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3846 boolean
.t
= VT_BOOL
;
3848 vtop
->c
.i
= !vtop
->c
.i
;
3849 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
3850 vtop
->c
.i
= vtop
->c
.i
^ 1;
3853 vseti(VT_JMP
, gtst(1, 0));
3865 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
3866 tcc_error("pointer not accepted for unary plus");
3867 /* In order to force cast, we add zero, except for floating point
3868 where we really need an noop (otherwise -0.0 will be transformed
3870 if (!is_float(vtop
->type
.t
)) {
3881 unary_type(&type
); // Perform a in_sizeof = 0;
3882 size
= type_size(&type
, &align
);
3883 if (t
== TOK_SIZEOF
) {
3884 if (!(type
.t
& VT_VLA
)) {
3886 tcc_error("sizeof applied to an incomplete type");
3889 vla_runtime_type_size(&type
, &align
);
3894 vtop
->type
.t
|= VT_UNSIGNED
;
3897 case TOK_builtin_types_compatible_p
:
3906 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3907 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3908 vpushi(is_compatible_types(&type1
, &type2
));
3911 case TOK_builtin_constant_p
:
3913 int saved_nocode_wanted
, res
;
3916 saved_nocode_wanted
= nocode_wanted
;
3919 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
3921 nocode_wanted
= saved_nocode_wanted
;
3926 case TOK_builtin_frame_address
:
3932 if (tok
!= TOK_CINT
|| tokc
.i
< 0) {
3933 tcc_error("__builtin_frame_address only takes positive integers");
3940 vset(&type
, VT_LOCAL
, 0); /* local frame */
3942 mk_pointer(&vtop
->type
);
3943 indir(); /* -> parent frame */
3947 #ifdef TCC_TARGET_X86_64
3948 #ifdef TCC_TARGET_PE
3949 case TOK_builtin_va_start
:
3957 if ((vtop
->r
& VT_VALMASK
) != VT_LOCAL
)
3958 tcc_error("__builtin_va_start expects a local variable");
3959 vtop
->r
&= ~(VT_LVAL
| VT_REF
);
3960 vtop
->type
= char_pointer_type
;
3965 case TOK_builtin_va_arg_types
:
3972 vpushi(classify_x86_64_va_arg(&type
));
3987 t
= vtop
->type
.t
& VT_BTYPE
;
3989 /* In IEEE negate(x) isn't subtract(0,x), but rather
3994 else if (t
== VT_DOUBLE
)
4005 goto tok_identifier
;
4007 /* allow to take the address of a label */
4008 if (tok
< TOK_UIDENT
)
4009 expect("label identifier");
4010 s
= label_find(tok
);
4012 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4014 if (s
->r
== LABEL_DECLARED
)
4015 s
->r
= LABEL_FORWARD
;
4018 s
->type
.t
= VT_VOID
;
4019 mk_pointer(&s
->type
);
4020 s
->type
.t
|= VT_STATIC
;
4022 vpushsym(&s
->type
, s
);
4026 // special qnan , snan and infinity values
4028 vpush64(VT_DOUBLE
, 0x7ff8000000000000ULL
);
4032 vpush64(VT_DOUBLE
, 0x7ff0000000000001ULL
);
4036 vpush64(VT_DOUBLE
, 0x7ff0000000000000ULL
);
4045 expect("identifier");
4048 const char *name
= get_tok_str(t
, NULL
);
4050 tcc_error("'%s' undeclared", name
);
4051 /* for simple function calls, we tolerate undeclared
4052 external reference to int() function */
4053 if (tcc_state
->warn_implicit_function_declaration
4054 #ifdef TCC_TARGET_PE
4055 /* people must be warned about using undeclared WINAPI functions
4056 (which usually start with uppercase letter) */
4057 || (name
[0] >= 'A' && name
[0] <= 'Z')
4060 tcc_warning("implicit declaration of function '%s'", name
);
4061 s
= external_global_sym(t
, &func_old_type
, 0);
4063 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
4064 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
4065 /* if referencing an inline function, then we generate a
4066 symbol to it if not already done. It will have the
4067 effect to generate code for it at the end of the
4068 compilation unit. Inline function as always
4069 generated in the text section. */
4071 put_extern_sym(s
, text_section
, 0, 0);
4072 r
= VT_SYM
| VT_CONST
;
4076 vset(&s
->type
, r
, s
->c
);
4077 /* if forward reference, we must point to s */
4078 if (vtop
->r
& VT_SYM
) {
4080 vtop
->c
.ptr_offset
= 0;
4085 /* post operations */
4088 int ret_nregs
, ret_align
;
4089 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
4092 } else if (tok
== '.' || tok
== TOK_ARROW
) {
4093 int qualifiers
, add
, is_lval
;
4095 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
4097 if (tok
== TOK_ARROW
)
4101 is_lval
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) >= VT_CONST
;
4105 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
4107 gfunc_sret(&vtop
->type
, func_var
, &ret
.type
, &ret_align
);
4110 /* expect pointer on structure */
4111 if ((type
.t
& VT_BTYPE
) != VT_STRUCT
)
4112 expect("struct or union");
4116 while ((s
= s
->next
) != NULL
) {
4121 tcc_error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, NULL
));
4124 /* change type to field type, and set to lvalue */
4129 type
.t
|= qualifiers
;
4134 /* an array is never an lvalue */
4135 if (!(type
.t
& VT_ARRAY
)) {
4136 vtop
->r
|= lvalue_type(type
.t
);
4137 #ifdef CONFIG_TCC_BCHECK
4138 /* if bound checking, the referenced pointer must be checked */
4139 if (tcc_state
->do_bounds_check
)
4140 vtop
->r
|= VT_MUSTBOUND
;
4144 gfunc_sret(&vtop
->type
, func_var
, &ret
.type
, &ret_align
);
4145 if(is_float(ret
.type
.t
) || (type
.t
& VT_ARRAY
)){
4146 #ifdef TCC_TARGET_X86_64
4147 if((ret
.type
.t
& VT_BTYPE
) != VT_LDOUBLE
)
4153 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
4157 #ifdef TCC_TARGET_X86_64
4162 if(add
& load_size
){
4165 vtop
->r2
= VT_CONST
;
4168 vtop
->type
.t
= VT_LLONG
;
4175 } else if (tok
== '[') {
4181 } else if (tok
== '(') {
4183 int nb_args
, variadic
, addr
;
4186 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
4187 /* pointer test (no array accepted) */
4188 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
4189 vtop
->type
= *pointed_type(&vtop
->type
);
4190 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
4194 expect("function pointer");
4197 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
4199 /* get return type */
4201 sa
= s
->next
; /* first parameter */
4204 /* compute first implicit argument if a structure is returned */
4205 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
4206 variadic
= (s
->c
== FUNC_ELLIPSIS
);
4207 ret_nregs
= gfunc_sret(&s
->type
, variadic
, &ret
.type
,
4210 /* get some space for the returned structure */
4211 size
= type_size(&s
->type
, &align
);
4212 addr
= loc_stack(size
, 1);
4214 ret
.r
= VT_LOCAL
| VT_LVAL
;
4215 /* pass it as 'int' to avoid structure arg passing
4217 vseti(VT_LOCAL
, addr
);
4227 /* return in register */
4228 if (is_float(ret
.type
.t
)) {
4229 ret
.r
= reg_fret(ret
.type
.t
);
4230 #ifdef TCC_TARGET_X86_64
4231 if ((ret
.type
.t
& VT_BTYPE
) == VT_QFLOAT
)
4235 #ifdef TCC_TARGET_X86_64
4236 if ((ret
.type
.t
& VT_BTYPE
) == VT_QLONG
)
4238 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
4249 gfunc_param_typed(s
, sa
);
4259 tcc_error("too few arguments to function");
4261 if (!nocode_wanted
) {
4262 gfunc_call(nb_args
);
4264 vtop
-= (nb_args
+ 1);
4268 for (r
= ret
.r
+ ret_nregs
+ !ret_nregs
; r
-- > ret
.r
;) {
4269 vsetc(&ret
.type
, r
, &ret
.c
);
4270 vtop
->r2
= ret
.r2
; /* Loop only happens when r2 is VT_CONST */
4273 /* handle packed struct return */
4274 if (((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) && ret_nregs
)
4275 vtop
->type
= s
->type
;
4282 ST_FUNC
void expr_prod(void)
4287 while (tok
== '*' || tok
== '/' || tok
== '%') {
4295 ST_FUNC
void expr_sum(void)
4300 while (tok
== '+' || tok
== '-') {
4308 static void expr_shift(void)
4313 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
4321 static void expr_cmp(void)
4326 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
4327 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
4335 static void expr_cmpeq(void)
4340 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
4348 static void expr_and(void)
4351 while (tok
== '&') {
4358 static void expr_xor(void)
4361 while (tok
== '^') {
4368 static void expr_or(void)
4371 while (tok
== '|') {
4378 /* XXX: fix this mess */
4379 static void expr_land_const(void)
4382 while (tok
== TOK_LAND
) {
4389 /* XXX: fix this mess */
4390 static void expr_lor_const(void)
4393 while (tok
== TOK_LOR
) {
4400 /* only used if non constant */
4401 static void expr_land(void)
4406 if (tok
== TOK_LAND
) {
4411 if (tok
!= TOK_LAND
) {
4421 static void expr_lor(void)
4426 if (tok
== TOK_LOR
) {
4431 if (tok
!= TOK_LOR
) {
4441 /* XXX: better constant handling */
4442 static void expr_cond(void)
4444 int tt
, u
, r
, rc
, t1
, t2
, bt1
, bt2
, ret_nregs
, ret_align
;
4446 CType type
, type1
, type2
;
4453 boolean
.t
= VT_BOOL
;
4459 if (tok
!= ':' || !gnu_ext
) {
4474 if (vtop
!= vstack
) {
4475 /* needed to avoid having different registers saved in
4477 if (is_float(vtop
->type
.t
)) {
4479 #ifdef TCC_TARGET_X86_64
4480 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4489 if (tok
== ':' && gnu_ext
) {
4497 sv
= *vtop
; /* save value to handle it later */
4498 vtop
--; /* no vpop so that FP stack is not flushed */
4506 bt1
= t1
& VT_BTYPE
;
4508 bt2
= t2
& VT_BTYPE
;
4509 /* cast operands to correct type according to ISOC rules */
4510 if (is_float(bt1
) || is_float(bt2
)) {
4511 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
4512 type
.t
= VT_LDOUBLE
;
4513 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
4518 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
4519 /* cast to biggest op */
4521 /* convert to unsigned if it does not fit in a long long */
4522 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
4523 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
4524 type
.t
|= VT_UNSIGNED
;
4525 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
4526 /* If one is a null ptr constant the result type is the other. */
4527 if (is_null_pointer (vtop
))
4529 else if (is_null_pointer (&sv
))
4531 /* XXX: test pointer compatibility, C99 has more elaborate rules here. */
4534 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
4535 /* XXX: test function pointer compatibility */
4536 type
= bt1
== VT_FUNC
? type1
: type2
;
4537 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
4538 /* XXX: test structure compatibility */
4539 type
= bt1
== VT_STRUCT
? type1
: type2
;
4540 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
4541 /* NOTE: as an extension, we accept void on only one side */
4544 /* integer operations */
4546 /* convert to unsigned if it does not fit in an integer */
4547 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
4548 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
4549 type
.t
|= VT_UNSIGNED
;
4552 /* now we convert second operand */
4555 if (VT_STRUCT
== (type
.t
& VT_BTYPE
)){
4556 ret_nregs
= gfunc_sret(&type
, func_var
, &ret
.type
, &ret_align
);
4558 vtop
->type
= ret
.type
;
4563 if (is_float(vtop
->type
.t
)) {
4565 #ifdef TCC_TARGET_X86_64
4566 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4573 rc
= reg_classes
[r
] & ~RC_MASK
;
4574 #ifdef TCC_TARGET_X86_64
4575 if (((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
))
4577 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
)
4579 ex_rc
= reg_classes
[vtop
->r2
] & ~RC_MASK
;
4580 /* this is horrible, but we must also convert first
4584 /* put again first value and cast it */
4587 if (VT_STRUCT
== (type
.t
& VT_BTYPE
)){
4589 vtop
->type
= ret
.type
;
4596 if (VT_STRUCT
== (type
.t
& VT_BTYPE
)){
4606 static void expr_eq(void)
4612 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
4613 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
4614 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
4629 ST_FUNC
void gexpr(void)
4640 /* parse an expression and return its type without any side effect. */
4641 static void expr_type(CType
*type
)
4643 int saved_nocode_wanted
;
4645 saved_nocode_wanted
= nocode_wanted
;
4650 nocode_wanted
= saved_nocode_wanted
;
4653 /* parse a unary expression and return its type without any side
4655 static void unary_type(CType
*type
)
4667 /* parse a constant expression and return value in vtop. */
4668 static void expr_const1(void)
4677 /* parse an integer constant and return its value. */
4678 ST_FUNC
int expr_const(void)
4682 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
4683 expect("constant expression");
4689 /* return the label token if current token is a label, otherwise
4691 static int is_label(void)
4695 /* fast test first */
4696 if (tok
< TOK_UIDENT
)
4698 /* no need to save tokc because tok is an identifier */
4705 unget_tok(last_tok
);
4710 static void label_or_decl(int l
)
4714 /* fast test first */
4715 if (tok
>= TOK_UIDENT
)
4717 /* no need to save tokc because tok is an identifier */
4721 unget_tok(last_tok
);
4724 unget_tok(last_tok
);
4729 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
4730 int case_reg
, int is_expr
)
4733 Sym
*s
, *frame_bottom
;
4735 /* generate line number info */
4736 if (tcc_state
->do_debug
&&
4737 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
4738 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
4740 last_line_num
= file
->line_num
;
4744 /* default return value is (void) */
4746 vtop
->type
.t
= VT_VOID
;
4749 if (tok
== TOK_IF
) {
4756 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4758 if (c
== TOK_ELSE
) {
4762 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4763 gsym(d
); /* patch else jmp */
4766 } else if (tok
== TOK_WHILE
) {
4774 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4778 } else if (tok
== '{') {
4780 int block_vla_sp_loc
, *saved_vla_sp_loc
, saved_vla_flags
;
4783 /* record local declaration stack position */
4785 frame_bottom
= sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4786 frame_bottom
->next
= scope_stack_bottom
;
4787 scope_stack_bottom
= frame_bottom
;
4788 llabel
= local_label_stack
;
4790 /* save VLA state */
4791 block_vla_sp_loc
= *(saved_vla_sp_loc
= vla_sp_loc
);
4792 if (saved_vla_sp_loc
!= &vla_sp_root_loc
)
4793 vla_sp_loc
= &block_vla_sp_loc
;
4795 saved_vla_flags
= vla_flags
;
4796 vla_flags
|= VLA_NEED_NEW_FRAME
;
4798 /* handle local labels declarations */
4799 if (tok
== TOK_LABEL
) {
4802 if (tok
< TOK_UIDENT
)
4803 expect("label identifier");
4804 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
4814 while (tok
!= '}') {
4815 label_or_decl(VT_LOCAL
);
4819 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4822 /* pop locally defined labels */
4823 label_pop(&local_label_stack
, llabel
);
4825 /* XXX: this solution makes only valgrind happy...
4826 triggered by gcc.c-torture/execute/20000917-1.c */
4828 switch(vtop
->type
.t
& VT_BTYPE
) {
4833 for(p
=vtop
->type
.ref
;p
;p
=p
->prev
)
4835 tcc_error("unsupported expression type");
4838 /* pop locally defined symbols */
4839 scope_stack_bottom
= scope_stack_bottom
->next
;
4840 sym_pop(&local_stack
, s
);
4842 /* Pop VLA frames and restore stack pointer if required */
4843 if (saved_vla_sp_loc
!= &vla_sp_root_loc
)
4844 *saved_vla_sp_loc
= block_vla_sp_loc
;
4845 if (vla_sp_loc
!= (saved_vla_sp_loc
== &vla_sp_root_loc
? &vla_sp_root_loc
: &block_vla_sp_loc
)) {
4846 vla_sp_loc
= saved_vla_sp_loc
;
4847 gen_vla_sp_restore(*vla_sp_loc
);
4849 vla_flags
= (vla_flags
& ~VLA_SCOPE_FLAGS
) | (saved_vla_flags
& VLA_SCOPE_FLAGS
);
4852 } else if (tok
== TOK_RETURN
) {
4856 gen_assign_cast(&func_vt
);
4857 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
4858 CType type
, ret_type
;
4859 int ret_align
, ret_nregs
;
4860 ret_nregs
= gfunc_sret(&func_vt
, func_var
, &ret_type
,
4862 if (0 == ret_nregs
) {
4863 /* if returning structure, must copy it to implicit
4864 first pointer arg location */
4867 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
4870 /* copy structure value to pointer */
4873 /* returning structure packed into registers */
4875 vtop
->type
= ret_type
;
4876 if (is_float(ret_type
.t
))
4877 rc
= rc_fret(ret_type
.t
);
4885 if (--ret_nregs
== 0)
4887 /* We assume that when a structure is returned in multiple
4888 registers, their classes are consecutive values of the
4891 /* XXX: compatible with arm only: ret_align == register_size */
4892 vtop
->c
.i
+= ret_align
;
4893 vtop
->r
= VT_LOCAL
| VT_LVAL
;
4896 } else if (is_float(func_vt
.t
)) {
4897 gv(rc_fret(func_vt
.t
));
4901 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
4904 rsym
= gjmp(rsym
); /* jmp */
4905 } else if (tok
== TOK_BREAK
) {
4908 tcc_error("cannot break");
4909 *bsym
= gjmp(*bsym
);
4912 } else if (tok
== TOK_CONTINUE
) {
4915 tcc_error("cannot continue");
4916 *csym
= gjmp(*csym
);
4919 } else if (tok
== TOK_FOR
) {
4924 frame_bottom
= sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4925 frame_bottom
->next
= scope_stack_bottom
;
4926 scope_stack_bottom
= frame_bottom
;
4928 /* c99 for-loop init decl? */
4929 if (!decl0(VT_LOCAL
, 1)) {
4930 /* no, regular for-loop init expr */
4954 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4958 scope_stack_bottom
= scope_stack_bottom
->next
;
4959 sym_pop(&local_stack
, s
);
4961 if (tok
== TOK_DO
) {
4966 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4977 if (tok
== TOK_SWITCH
) {
4981 /* XXX: other types than integer */
4982 case_reg
= gv(RC_INT
);
4986 b
= gjmp(0); /* jump to first case */
4988 block(&a
, csym
, &b
, &c
, case_reg
, 0);
4989 /* if no default, jmp after switch */
4997 if (tok
== TOK_CASE
) {
5004 if (gnu_ext
&& tok
== TOK_DOTS
) {
5008 tcc_warning("empty case range");
5010 /* since a case is like a label, we must skip it with a jmp */
5017 *case_sym
= gtst(1, 0);
5020 *case_sym
= gtst(1, 0);
5024 *case_sym
= gtst(1, *case_sym
);
5029 goto block_after_label
;
5031 if (tok
== TOK_DEFAULT
) {
5037 tcc_error("too many 'default'");
5040 goto block_after_label
;
5042 if (tok
== TOK_GOTO
) {
5044 if (tok
== '*' && gnu_ext
) {
5048 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
5051 } else if (tok
>= TOK_UIDENT
) {
5052 s
= label_find(tok
);
5053 /* put forward definition if needed */
5055 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
5057 if (s
->r
== LABEL_DECLARED
)
5058 s
->r
= LABEL_FORWARD
;
5060 /* label already defined */
5061 if (vla_flags
& VLA_IN_SCOPE
) {
5062 /* If VLAs are in use, save the current stack pointer and
5063 reset the stack pointer to what it was at function entry
5064 (label will restore stack pointer in inner scopes) */
5066 gen_vla_sp_restore(vla_sp_root_loc
);
5068 if (s
->r
& LABEL_FORWARD
)
5069 s
->jnext
= gjmp(s
->jnext
);
5071 gjmp_addr(s
->jnext
);
5074 expect("label identifier");
5077 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
5083 if (vla_flags
& VLA_IN_SCOPE
) {
5084 /* save/restore stack pointer across label
5085 this is a no-op when combined with the load immediately
5086 after the label unless we arrive via goto */
5091 if (s
->r
== LABEL_DEFINED
)
5092 tcc_error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
5094 s
->r
= LABEL_DEFINED
;
5096 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
5099 if (vla_flags
& VLA_IN_SCOPE
) {
5100 gen_vla_sp_restore(*vla_sp_loc
);
5101 vla_flags
|= VLA_NEED_NEW_FRAME
;
5103 /* we accept this, but it is a mistake */
5106 tcc_warning("deprecated use of label at end of compound statement");
5110 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
5113 /* expression case */
5128 /* t is the array or struct type. c is the array or struct
5129 address. cur_index/cur_field is the pointer to the current
5130 value. 'size_only' is true if only size info is needed (only used
5132 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
5133 int *cur_index
, Sym
**cur_field
,
5137 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
5143 if (gnu_ext
&& (l
= is_label()) != 0)
5145 while (tok
== '[' || tok
== '.') {
5147 if (!(type
->t
& VT_ARRAY
))
5148 expect("array type");
5151 index
= expr_const();
5152 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
5153 expect("invalid index");
5154 if (tok
== TOK_DOTS
&& gnu_ext
) {
5156 index_last
= expr_const();
5157 if (index_last
< 0 ||
5158 (s
->c
>= 0 && index_last
>= s
->c
) ||
5160 expect("invalid index");
5166 *cur_index
= index_last
;
5167 type
= pointed_type(type
);
5168 elem_size
= type_size(type
, &align
);
5169 c
+= index
* elem_size
;
5170 /* NOTE: we only support ranges for last designator */
5171 nb_elems
= index_last
- index
+ 1;
5172 if (nb_elems
!= 1) {
5181 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
5182 expect("struct/union type");
5195 /* XXX: fix this mess by using explicit storage field */
5197 type1
.t
|= (type
->t
& ~VT_TYPE
);
5211 if (type
->t
& VT_ARRAY
) {
5213 type
= pointed_type(type
);
5214 c
+= index
* type_size(type
, &align
);
5218 tcc_error("too many field init");
5219 /* XXX: fix this mess by using explicit storage field */
5221 type1
.t
|= (type
->t
& ~VT_TYPE
);
5226 decl_initializer(type
, sec
, c
, 0, size_only
);
5228 /* XXX: make it more general */
5229 if (!size_only
&& nb_elems
> 1) {
5230 unsigned long c_end
;
5235 tcc_error("range init not supported yet for dynamic storage");
5236 c_end
= c
+ nb_elems
* elem_size
;
5237 if (c_end
> sec
->data_allocated
)
5238 section_realloc(sec
, c_end
);
5239 src
= sec
->data
+ c
;
5241 for(i
= 1; i
< nb_elems
; i
++) {
5243 memcpy(dst
, src
, elem_size
);
5249 #define EXPR_CONST 1
5252 /* store a value or an expression directly in global data or in local array */
5253 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
5254 int v
, int expr_type
)
5256 int saved_global_expr
, bt
, bit_pos
, bit_size
;
5258 unsigned long long bit_mask
;
5266 /* compound literals must be allocated globally in this case */
5267 saved_global_expr
= global_expr
;
5270 global_expr
= saved_global_expr
;
5271 /* NOTE: symbols are accepted */
5272 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
5273 tcc_error("initializer element is not constant");
5281 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
5284 /* XXX: not portable */
5285 /* XXX: generate error if incorrect relocation */
5286 gen_assign_cast(&dtype
);
5287 bt
= type
->t
& VT_BTYPE
;
5288 /* we'll write at most 12 bytes */
5289 if (c
+ 12 > sec
->data_allocated
) {
5290 section_realloc(sec
, c
+ 12);
5292 ptr
= sec
->data
+ c
;
5293 /* XXX: make code faster ? */
5294 if (!(type
->t
& VT_BITFIELD
)) {
5299 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5300 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
5301 bit_mask
= (1LL << bit_size
) - 1;
5303 if ((vtop
->r
& VT_SYM
) &&
5309 (bt
== VT_INT
&& bit_size
!= 32)))
5310 tcc_error("initializer element is not computable at load time");
5313 vtop
->c
.i
= (vtop
->c
.i
!= 0);
5315 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5318 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5321 *(double *)ptr
= vtop
->c
.d
;
5324 *(long double *)ptr
= vtop
->c
.ld
;
5327 *(long long *)ptr
|= (vtop
->c
.ll
& bit_mask
) << bit_pos
;
5330 if (vtop
->r
& VT_SYM
) {
5331 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
5333 *(addr_t
*)ptr
|= (vtop
->c
.ptr_offset
& bit_mask
) << bit_pos
;
5336 if (vtop
->r
& VT_SYM
) {
5337 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
5339 *(int *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5344 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
5351 /* put zeros for variable based init */
5352 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
5355 /* nothing to do because globals are already set to zero */
5357 #ifndef TCC_TARGET_X86_64
5358 vpush_global_sym(&func_old_type
, TOK_memset
);
5360 # ifdef TCC_TARGET_ARM
5370 gen_putz(vtop
, size
);
5376 /* 't' contains the type and storage info. 'c' is the offset of the
5377 object in section 'sec'. If 'sec' is NULL, it means stack based
5378 allocation. 'first' is true if array '{' must be read (multi
5379 dimension implicit array init handling). 'size_only' is true if
5380 size only evaluation is wanted (only for arrays). */
5381 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
5382 int first
, int size_only
)
5384 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, parlevel1
, i
;
5385 int size1
, align1
, expr_type
;
5389 if (type
->t
& VT_VLA
) {
5392 /* save current stack pointer */
5393 if (vla_flags
& VLA_NEED_NEW_FRAME
) {
5395 vla_flags
= VLA_IN_SCOPE
;
5396 vla_sp_loc
= &vla_sp_loc_tmp
;
5399 vla_runtime_type_size(type
, &a
);
5400 gen_vla_alloc(type
, a
);
5401 vset(type
, VT_LOCAL
|VT_LVAL
, c
);
5405 } else if (type
->t
& VT_ARRAY
) {
5409 t1
= pointed_type(type
);
5410 size1
= type_size(t1
, &align1
);
5413 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
5416 tcc_error("character array initializer must be a literal,"
5417 " optionally enclosed in braces");
5422 /* only parse strings here if correct type (otherwise: handle
5423 them as ((w)char *) expressions */
5424 if ((tok
== TOK_LSTR
&&
5425 #ifdef TCC_TARGET_PE
5426 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
5428 (t1
->t
& VT_BTYPE
) == VT_INT
5430 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
5431 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5436 /* compute maximum number of chars wanted */
5438 cstr_len
= cstr
->size
;
5440 cstr_len
= cstr
->size
/ sizeof(nwchar_t
);
5443 if (n
>= 0 && nb
> (n
- array_length
))
5444 nb
= n
- array_length
;
5447 tcc_warning("initializer-string for array is too long");
5448 /* in order to go faster for common case (char
5449 string in global variable, we handle it
5451 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
5452 memcpy(sec
->data
+ c
+ array_length
, cstr
->data
, nb
);
5456 ch
= ((unsigned char *)cstr
->data
)[i
];
5458 ch
= ((nwchar_t
*)cstr
->data
)[i
];
5459 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
5467 /* only add trailing zero if enough storage (no
5468 warning in this case since it is standard) */
5469 if (n
< 0 || array_length
< n
) {
5471 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
5477 while (tok
!= '}') {
5478 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
5479 if (n
>= 0 && index
>= n
)
5480 tcc_error("index too large");
5481 /* must put zero in holes (note that doing it that way
5482 ensures that it even works with designators) */
5483 if (!size_only
&& array_length
< index
) {
5484 init_putz(t1
, sec
, c
+ array_length
* size1
,
5485 (index
- array_length
) * size1
);
5488 if (index
> array_length
)
5489 array_length
= index
;
5490 /* special test for multi dimensional arrays (may not
5491 be strictly correct if designators are used at the
5493 if (index
>= n
&& no_oblock
)
5502 /* put zeros at the end */
5503 if (!size_only
&& n
>= 0 && array_length
< n
) {
5504 init_putz(t1
, sec
, c
+ array_length
* size1
,
5505 (n
- array_length
) * size1
);
5507 /* patch type size if needed */
5509 s
->c
= array_length
;
5510 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
5511 (sec
|| !first
|| tok
== '{')) {
5514 /* NOTE: the previous test is a specific case for automatic
5515 struct/union init */
5516 /* XXX: union needs only one init */
5518 /* XXX: this test is incorrect for local initializers
5519 beginning with ( without {. It would be much more difficult
5520 to do it correctly (ideally, the expression parser should
5521 be used in all cases) */
5527 while (tok
== '(') {
5531 if (!parse_btype(&type1
, &ad1
))
5533 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
5535 if (!is_assignable_types(type
, &type1
))
5536 tcc_error("invalid type for cast");
5541 if (first
|| tok
== '{') {
5550 while (tok
!= '}') {
5551 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
5553 if (!size_only
&& array_length
< index
) {
5554 init_putz(type
, sec
, c
+ array_length
,
5555 index
- array_length
);
5557 index
= index
+ type_size(&f
->type
, &align1
);
5558 if (index
> array_length
)
5559 array_length
= index
;
5561 /* gr: skip fields from same union - ugly. */
5563 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
5564 /* test for same offset */
5565 if (f
->next
->c
!= f
->c
)
5567 /* if yes, test for bitfield shift */
5568 if ((f
->type
.t
& VT_BITFIELD
) && (f
->next
->type
.t
& VT_BITFIELD
)) {
5569 int bit_pos_1
= (f
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5570 int bit_pos_2
= (f
->next
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5571 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
5572 if (bit_pos_1
!= bit_pos_2
)
5579 if (no_oblock
&& f
== NULL
)
5585 /* put zeros at the end */
5586 if (!size_only
&& array_length
< n
) {
5587 init_putz(type
, sec
, c
+ array_length
,
5596 } else if (tok
== '{') {
5598 decl_initializer(type
, sec
, c
, first
, size_only
);
5600 } else if (size_only
) {
5601 /* just skip expression */
5602 parlevel
= parlevel1
= 0;
5603 while ((parlevel
> 0 || parlevel1
> 0 ||
5604 (tok
!= '}' && tok
!= ',')) && tok
!= -1) {
5607 else if (tok
== ')')
5609 else if (tok
== '{')
5611 else if (tok
== '}')
5616 /* currently, we always use constant expression for globals
5617 (may change for scripting case) */
5618 expr_type
= EXPR_CONST
;
5620 expr_type
= EXPR_ANY
;
5621 init_putv(type
, sec
, c
, 0, expr_type
);
5625 /* parse an initializer for type 't' if 'has_init' is non zero, and
5626 allocate space in local or global data space ('r' is either
5627 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5628 variable 'v' with an associated name represented by 'asm_label' of
5629 scope 'scope' is declared before initializers are parsed. If 'v' is
5630 zero, then a reference to the new object is put in the value stack.
5631 If 'has_init' is 2, a special parsing is done to handle string
5633 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
5634 int has_init
, int v
, char *asm_label
,
5637 int size
, align
, addr
, data_offset
;
5639 ParseState saved_parse_state
= {0};
5640 TokenString init_str
;
5642 Sym
*flexible_array
;
5644 flexible_array
= NULL
;
5645 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
5646 Sym
*field
= type
->ref
->next
;
5649 field
= field
->next
;
5650 if (field
->type
.t
& VT_ARRAY
&& field
->type
.ref
->c
< 0)
5651 flexible_array
= field
;
5655 size
= type_size(type
, &align
);
5656 /* If unknown size, we must evaluate it before
5657 evaluating initializers because
5658 initializers can generate global data too
5659 (e.g. string pointers or ISOC99 compound
5660 literals). It also simplifies local
5661 initializers handling */
5662 tok_str_new(&init_str
);
5663 if (size
< 0 || (flexible_array
&& has_init
)) {
5665 tcc_error("unknown type size");
5666 /* get all init string */
5667 if (has_init
== 2) {
5668 /* only get strings */
5669 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5670 tok_str_add_tok(&init_str
);
5675 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
5677 tcc_error("unexpected end of file in initializer");
5678 tok_str_add_tok(&init_str
);
5681 else if (tok
== '}') {
5691 tok_str_add(&init_str
, -1);
5692 tok_str_add(&init_str
, 0);
5695 save_parse_state(&saved_parse_state
);
5697 macro_ptr
= init_str
.str
;
5699 decl_initializer(type
, NULL
, 0, 1, 1);
5700 /* prepare second initializer parsing */
5701 macro_ptr
= init_str
.str
;
5704 /* if still unknown size, error */
5705 size
= type_size(type
, &align
);
5707 tcc_error("unknown type size");
5710 size
+= flexible_array
->type
.ref
->c
* pointed_size(&flexible_array
->type
);
5711 /* take into account specified alignment if bigger */
5712 if (ad
->a
.aligned
) {
5713 if (ad
->a
.aligned
> align
)
5714 align
= ad
->a
.aligned
;
5715 } else if (ad
->a
.packed
) {
5718 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
5720 #ifdef CONFIG_TCC_BCHECK
5721 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5725 addr
= loc_stack(size
, 1);
5726 #ifdef CONFIG_TCC_BCHECK
5727 /* handles bounds */
5728 /* XXX: currently, since we do only one pass, we cannot track
5729 '&' operators, so we add only arrays */
5730 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5731 unsigned long *bounds_ptr
;
5732 /* add padding between regions */
5734 /* then add local bound info */
5735 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(unsigned long));
5736 bounds_ptr
[0] = addr
;
5737 bounds_ptr
[1] = size
;
5741 /* local variable */
5742 sym_push(v
, type
, r
, addr
);
5744 /* push local reference */
5745 vset(type
, r
, addr
);
5751 if (v
&& scope
== VT_CONST
) {
5752 /* see if the symbol was already defined */
5755 if (!is_compatible_types(&sym
->type
, type
))
5756 tcc_error("incompatible types for redefinition of '%s'",
5757 get_tok_str(v
, NULL
));
5758 if (sym
->type
.t
& VT_EXTERN
) {
5759 /* if the variable is extern, it was not allocated */
5760 sym
->type
.t
&= ~VT_EXTERN
;
5761 /* set array size if it was omitted in extern
5763 if ((sym
->type
.t
& VT_ARRAY
) &&
5764 sym
->type
.ref
->c
< 0 &&
5766 sym
->type
.ref
->c
= type
->ref
->c
;
5768 /* we accept several definitions of the same
5769 global variable. this is tricky, because we
5770 must play with the SHN_COMMON type of the symbol */
5771 /* XXX: should check if the variable was already
5772 initialized. It is incorrect to initialized it
5774 /* no init data, we won't add more to the symbol */
5781 /* allocate symbol in corresponding section */
5786 else if (tcc_state
->nocommon
)
5790 data_offset
= sec
->data_offset
;
5791 data_offset
= (data_offset
+ align
- 1) & -align
;
5793 /* very important to increment global pointer at this time
5794 because initializers themselves can create new initializers */
5795 data_offset
+= size
;
5796 #ifdef CONFIG_TCC_BCHECK
5797 /* add padding if bound check */
5798 if (tcc_state
->do_bounds_check
)
5801 sec
->data_offset
= data_offset
;
5802 /* allocate section space to put the data */
5803 if (sec
->sh_type
!= SHT_NOBITS
&&
5804 data_offset
> sec
->data_allocated
)
5805 section_realloc(sec
, data_offset
);
5806 /* align section if needed */
5807 if (align
> sec
->sh_addralign
)
5808 sec
->sh_addralign
= align
;
5810 addr
= 0; /* avoid warning */
5814 if (scope
!= VT_CONST
|| !sym
) {
5815 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
5816 sym
->asm_label
= asm_label
;
5818 /* update symbol definition */
5820 put_extern_sym(sym
, sec
, addr
, size
);
5823 /* put a common area */
5824 put_extern_sym(sym
, NULL
, align
, size
);
5825 /* XXX: find a nicer way */
5826 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
5827 esym
->st_shndx
= SHN_COMMON
;
5830 /* push global reference */
5831 sym
= get_sym_ref(type
, sec
, addr
, size
);
5832 vpushsym(type
, sym
);
5834 /* patch symbol weakness */
5835 if (type
->t
& VT_WEAK
)
5837 apply_visibility(sym
, type
);
5838 #ifdef CONFIG_TCC_BCHECK
5839 /* handles bounds now because the symbol must be defined
5840 before for the relocation */
5841 if (tcc_state
->do_bounds_check
) {
5842 unsigned long *bounds_ptr
;
5844 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
5845 /* then add global bound info */
5846 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(long));
5847 bounds_ptr
[0] = 0; /* relocated */
5848 bounds_ptr
[1] = size
;
5852 if (has_init
|| (type
->t
& VT_VLA
)) {
5853 decl_initializer(type
, sec
, addr
, 1, 0);
5854 /* restore parse state if needed */
5856 tok_str_free(init_str
.str
);
5857 restore_parse_state(&saved_parse_state
);
5859 /* patch flexible array member size back to -1, */
5860 /* for possible subsequent similar declarations */
5862 flexible_array
->type
.ref
->c
= -1;
5867 static void put_func_debug(Sym
*sym
)
5872 /* XXX: we put here a dummy type */
5873 snprintf(buf
, sizeof(buf
), "%s:%c1",
5874 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
5875 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
5876 cur_text_section
, sym
->c
);
5877 /* //gr gdb wants a line at the function */
5878 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
5883 /* parse an old style function declaration list */
5884 /* XXX: check multiple parameter */
5885 static void func_decl_list(Sym
*func_sym
)
5892 /* parse each declaration */
5893 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
&&
5894 tok
!= TOK_ASM1
&& tok
!= TOK_ASM2
&& tok
!= TOK_ASM3
) {
5895 if (!parse_btype(&btype
, &ad
))
5896 expect("declaration list");
5897 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5898 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5900 /* we accept no variable after */
5904 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5905 /* find parameter in function parameter list */
5908 if ((s
->v
& ~SYM_FIELD
) == v
)
5912 tcc_error("declaration for parameter '%s' but no such parameter",
5913 get_tok_str(v
, NULL
));
5915 /* check that no storage specifier except 'register' was given */
5916 if (type
.t
& VT_STORAGE
)
5917 tcc_error("storage class specified for '%s'", get_tok_str(v
, NULL
));
5918 convert_parameter_type(&type
);
5919 /* we can add the type (NOTE: it could be local to the function) */
5921 /* accept other parameters */
5932 /* parse a function defined by symbol 'sym' and generate its code in
5933 'cur_text_section' */
5934 static void gen_function(Sym
*sym
)
5936 int saved_nocode_wanted
= nocode_wanted
;
5938 ind
= cur_text_section
->data_offset
;
5939 /* NOTE: we patch the symbol size later */
5940 put_extern_sym(sym
, cur_text_section
, ind
, 0);
5941 funcname
= get_tok_str(sym
->v
, NULL
);
5943 /* Initialize VLA state */
5944 vla_sp_loc
= &vla_sp_root_loc
;
5945 vla_flags
= VLA_NEED_NEW_FRAME
;
5946 /* put debug symbol */
5947 if (tcc_state
->do_debug
)
5948 put_func_debug(sym
);
5949 /* push a dummy symbol to enable local sym storage */
5950 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
5951 gfunc_prolog(&sym
->type
);
5952 #ifdef CONFIG_TCC_BCHECK
5953 if (tcc_state
->do_bounds_check
5954 && !strcmp(get_tok_str(sym
->v
, NULL
), "main")) {
5958 for (i
= 0, sym
= local_stack
; i
< 2; i
++, sym
= sym
->prev
) {
5959 if (sym
->v
& SYM_FIELD
|| sym
->prev
->v
& SYM_FIELD
)
5961 vpush_global_sym(&func_old_type
, TOK___bound_main_arg
);
5962 vset(&sym
->type
, sym
->r
, sym
->c
);
5968 block(NULL
, NULL
, NULL
, NULL
, 0, 0);
5971 cur_text_section
->data_offset
= ind
;
5972 label_pop(&global_label_stack
, NULL
);
5973 /* reset local stack */
5974 scope_stack_bottom
= NULL
;
5975 sym_pop(&local_stack
, NULL
);
5976 /* end of function */
5977 /* patch symbol size */
5978 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
5980 /* patch symbol weakness (this definition overrules any prototype) */
5981 if (sym
->type
.t
& VT_WEAK
)
5983 apply_visibility(sym
, &sym
->type
);
5984 if (tcc_state
->do_debug
) {
5985 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
5987 /* It's better to crash than to generate wrong code */
5988 cur_text_section
= NULL
;
5989 funcname
= ""; /* for safety */
5990 func_vt
.t
= VT_VOID
; /* for safety */
5991 func_var
= 0; /* for safety */
5992 ind
= 0; /* for safety */
5993 nocode_wanted
= saved_nocode_wanted
;
5996 ST_FUNC
void gen_inline_functions(void)
5999 int *str
, inline_generated
, i
;
6000 struct InlineFunc
*fn
;
6002 /* iterate while inline function are referenced */
6004 inline_generated
= 0;
6005 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
6006 fn
= tcc_state
->inline_fns
[i
];
6008 if (sym
&& sym
->c
) {
6009 /* the function was used: generate its code and
6010 convert it to a normal function */
6011 str
= fn
->token_str
;
6014 pstrcpy(file
->filename
, sizeof file
->filename
, fn
->filename
);
6015 sym
->r
= VT_SYM
| VT_CONST
;
6016 sym
->type
.t
&= ~VT_INLINE
;
6020 cur_text_section
= text_section
;
6022 macro_ptr
= NULL
; /* fail safe */
6024 inline_generated
= 1;
6027 if (!inline_generated
)
6030 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
6031 fn
= tcc_state
->inline_fns
[i
];
6032 str
= fn
->token_str
;
6035 dynarray_reset(&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
);
6038 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
6039 static int decl0(int l
, int is_for_loop_init
)
6047 if (!parse_btype(&btype
, &ad
)) {
6048 if (is_for_loop_init
)
6050 /* skip redundant ';' */
6051 /* XXX: find more elegant solution */
6056 if (l
== VT_CONST
&&
6057 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
6058 /* global asm block */
6062 /* special test for old K&R protos without explicit int
6063 type. Only accepted when defining global data */
6064 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
6068 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
6069 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
6071 /* we accept no variable after */
6075 while (1) { /* iterate thru each declaration */
6076 char *asm_label
; // associated asm label
6078 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
6082 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
6083 printf("type = '%s'\n", buf
);
6086 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
6087 if ((type
.t
& VT_STATIC
) && (l
== VT_LOCAL
)) {
6088 tcc_error("function without file scope cannot be static");
6090 /* if old style function prototype, we accept a
6093 if (sym
->c
== FUNC_OLD
)
6094 func_decl_list(sym
);
6098 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
6101 asm_label_instr(&astr
);
6102 asm_label
= tcc_strdup(astr
.data
);
6105 /* parse one last attribute list, after asm label */
6106 parse_attribute(&ad
);
6111 #ifdef TCC_TARGET_PE
6112 if (ad
.a
.func_import
)
6113 type
.t
|= VT_IMPORT
;
6114 if (ad
.a
.func_export
)
6115 type
.t
|= VT_EXPORT
;
6117 type
.t
|= ad
.a
.visibility
<< VT_VIS_SHIFT
;
6121 tcc_error("cannot use local functions");
6122 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
6123 expect("function definition");
6125 /* reject abstract declarators in function definition */
6127 while ((sym
= sym
->next
) != NULL
)
6128 if (!(sym
->v
& ~SYM_FIELD
))
6129 expect("identifier");
6131 /* XXX: cannot do better now: convert extern line to static inline */
6132 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
6133 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
6138 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
6141 ref
= sym
->type
.ref
;
6142 if (0 == ref
->a
.func_proto
)
6143 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
6145 /* use func_call from prototype if not defined */
6146 if (ref
->a
.func_call
!= FUNC_CDECL
6147 && type
.ref
->a
.func_call
== FUNC_CDECL
)
6148 type
.ref
->a
.func_call
= ref
->a
.func_call
;
6150 /* use export from prototype */
6151 if (ref
->a
.func_export
)
6152 type
.ref
->a
.func_export
= 1;
6154 /* use static from prototype */
6155 if (sym
->type
.t
& VT_STATIC
)
6156 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
6158 /* If the definition has no visibility use the
6159 one from prototype. */
6160 if (! (type
.t
& VT_VIS_MASK
))
6161 type
.t
|= sym
->type
.t
& VT_VIS_MASK
;
6163 if (!is_compatible_types(&sym
->type
, &type
)) {
6165 tcc_error("incompatible types for redefinition of '%s'",
6166 get_tok_str(v
, NULL
));
6168 type
.ref
->a
.func_proto
= 0;
6169 /* if symbol is already defined, then put complete type */
6172 /* put function symbol */
6173 sym
= global_identifier_push(v
, type
.t
, 0);
6174 sym
->type
.ref
= type
.ref
;
6177 /* static inline functions are just recorded as a kind
6178 of macro. Their code will be emitted at the end of
6179 the compilation unit only if they are used */
6180 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
6181 (VT_INLINE
| VT_STATIC
)) {
6182 TokenString func_str
;
6184 struct InlineFunc
*fn
;
6185 const char *filename
;
6187 tok_str_new(&func_str
);
6193 tcc_error("unexpected end of file");
6194 tok_str_add_tok(&func_str
);
6199 } else if (t
== '}') {
6201 if (block_level
== 0)
6205 tok_str_add(&func_str
, -1);
6206 tok_str_add(&func_str
, 0);
6207 filename
= file
? file
->filename
: "";
6208 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
6209 strcpy(fn
->filename
, filename
);
6211 fn
->token_str
= func_str
.str
;
6212 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
6215 /* compute text section */
6216 cur_text_section
= ad
.section
;
6217 if (!cur_text_section
)
6218 cur_text_section
= text_section
;
6219 sym
->r
= VT_SYM
| VT_CONST
;
6224 if (btype
.t
& VT_TYPEDEF
) {
6225 /* save typedefed type */
6226 /* XXX: test storage specifiers ? */
6227 sym
= sym_push(v
, &type
, 0, 0);
6229 sym
->type
.t
|= VT_TYPEDEF
;
6232 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
6233 /* external function definition */
6234 /* specific case for func_call attribute */
6235 ad
.a
.func_proto
= 1;
6237 } else if (!(type
.t
& VT_ARRAY
)) {
6238 /* not lvalue if array */
6239 r
|= lvalue_type(type
.t
);
6241 has_init
= (tok
== '=');
6242 if (has_init
&& (type
.t
& VT_VLA
))
6243 tcc_error("Variable length array cannot be initialized");
6244 if ((btype
.t
& VT_EXTERN
) || ((type
.t
& VT_BTYPE
) == VT_FUNC
) ||
6245 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
6246 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
6247 /* external variable or function */
6248 /* NOTE: as GCC, uninitialized global static
6249 arrays of null size are considered as
6251 sym
= external_sym(v
, &type
, r
, asm_label
);
6253 if (ad
.alias_target
) {
6258 alias_target
= sym_find(ad
.alias_target
);
6259 if (!alias_target
|| !alias_target
->c
)
6260 tcc_error("unsupported forward __alias__ attribute");
6261 esym
= &((Elf32_Sym
*)symtab_section
->data
)[alias_target
->c
];
6262 tsec
.sh_num
= esym
->st_shndx
;
6263 put_extern_sym2(sym
, &tsec
, esym
->st_value
, esym
->st_size
, 0);
6266 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
6267 if (type
.t
& VT_STATIC
)
6273 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, asm_label
, l
);
6277 if (is_for_loop_init
)
6290 ST_FUNC
void decl(int l
)