2 * TCC - Tiny C Compiler
4 * Copyright (c) 2001-2004 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 /********************************************************/
24 /* global variables */
26 /* loc : local variable index
27 ind : output code index
29 anon_sym: anonymous symbol index
31 ST_DATA
int rsym
, anon_sym
, ind
, loc
;
33 ST_DATA Section
*text_section
, *data_section
, *bss_section
; /* predefined sections */
34 ST_DATA Section
*cur_text_section
; /* current section where function code is generated */
36 ST_DATA Section
*last_text_section
; /* to handle .previous asm directive */
38 #ifdef CONFIG_TCC_BCHECK
39 /* bound check related sections */
40 ST_DATA Section
*bounds_section
; /* contains global data bound description */
41 ST_DATA Section
*lbounds_section
; /* contains local data bound description */
44 ST_DATA Section
*symtab_section
, *strtab_section
;
46 ST_DATA Section
*stab_section
, *stabstr_section
;
47 ST_DATA Sym
*sym_free_first
;
48 ST_DATA
void **sym_pools
;
49 ST_DATA
int nb_sym_pools
;
51 ST_DATA Sym
*global_stack
;
52 ST_DATA Sym
*local_stack
;
53 ST_DATA Sym
*scope_stack_bottom
;
54 ST_DATA Sym
*define_stack
;
55 ST_DATA Sym
*global_label_stack
;
56 ST_DATA Sym
*local_label_stack
;
58 ST_DATA
int vla_sp_loc_tmp
; /* vla_sp_loc is set to this when the value won't be needed later */
59 ST_DATA
int vla_sp_root_loc
; /* vla_sp_loc for SP before any VLAs were pushed */
60 ST_DATA
int *vla_sp_loc
; /* Pointer to variable holding location to store stack pointer on the stack when modifying stack pointer */
61 ST_DATA
int vla_flags
; /* VLA_* flags */
63 ST_DATA SValue __vstack
[1+VSTACK_SIZE
], *vtop
;
65 ST_DATA
int const_wanted
; /* true if constant wanted */
66 ST_DATA
int nocode_wanted
; /* true if no code generation wanted for an expression */
67 ST_DATA
int global_expr
; /* true if compound literals must be allocated globally (used during initializers parsing */
68 ST_DATA CType func_vt
; /* current function return type (used by return instruction) */
69 ST_DATA
int func_var
; /* true if current function is variadic (used by return instruction) */
71 ST_DATA
int last_line_num
, last_ind
, func_ind
; /* debug last line number and pc */
72 ST_DATA
char *funcname
;
74 ST_DATA CType char_pointer_type
, func_old_type
, int_type
, size_type
;
76 /* ------------------------------------------------------------------------- */
77 static void gen_cast(CType
*type
);
78 static inline CType
*pointed_type(CType
*type
);
79 static int is_compatible_types(CType
*type1
, CType
*type2
);
80 static int parse_btype(CType
*type
, AttributeDef
*ad
);
81 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
);
82 static void parse_expr_type(CType
*type
);
83 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
, int first
, int size_only
);
84 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
, int case_reg
, int is_expr
);
85 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
, int has_init
, int v
, char *asm_label
, int scope
);
86 static int decl0(int l
, int is_for_loop_init
);
87 static void expr_eq(void);
88 static void unary_type(CType
*type
);
89 static void vla_runtime_type_size(CType
*type
, int *a
);
90 static void vla_sp_save(void);
91 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
);
92 static void expr_type(CType
*type
);
95 ST_FUNC
void vpush64(int ty
, unsigned long long v
);
96 ST_FUNC
void vpush(CType
*type
);
97 ST_FUNC
int gvtst(int inv
, int t
);
98 ST_FUNC
int is_btype_size(int bt
);
100 ST_INLN
int is_float(int t
)
104 return bt
== VT_LDOUBLE
|| bt
== VT_DOUBLE
|| bt
== VT_FLOAT
|| bt
== VT_QFLOAT
;
107 /* we use our own 'finite' function to avoid potential problems with
108 non standard math libs */
109 /* XXX: endianness dependent */
110 ST_FUNC
int ieee_finite(double d
)
113 memcpy(p
, &d
, sizeof(double));
114 return ((unsigned)((p
[1] | 0x800fffff) + 1)) >> 31;
117 ST_FUNC
void test_lvalue(void)
119 if (!(vtop
->r
& VT_LVAL
))
123 /* ------------------------------------------------------------------------- */
124 /* symbol allocator */
125 static Sym
*__sym_malloc(void)
127 Sym
*sym_pool
, *sym
, *last_sym
;
130 sym_pool
= tcc_malloc(SYM_POOL_NB
* sizeof(Sym
));
131 dynarray_add(&sym_pools
, &nb_sym_pools
, sym_pool
);
133 last_sym
= sym_free_first
;
135 for(i
= 0; i
< SYM_POOL_NB
; i
++) {
136 sym
->next
= last_sym
;
140 sym_free_first
= last_sym
;
144 static inline Sym
*sym_malloc(void)
147 sym
= sym_free_first
;
149 sym
= __sym_malloc();
150 sym_free_first
= sym
->next
;
154 ST_INLN
void sym_free(Sym
*sym
)
156 sym
->next
= sym_free_first
;
157 tcc_free(sym
->asm_label
);
158 sym_free_first
= sym
;
161 /* push, without hashing */
162 ST_FUNC Sym
*sym_push2(Sym
**ps
, int v
, int t
, long c
)
165 if (ps
== &local_stack
) {
166 for (s
= *ps
; s
&& s
!= scope_stack_bottom
; s
= s
->prev
)
167 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
&& s
->v
== v
)
168 tcc_error("incompatible types for redefinition of '%s'",
169 get_tok_str(v
, NULL
));
187 /* find a symbol and return its associated structure. 's' is the top
188 of the symbol stack */
189 ST_FUNC Sym
*sym_find2(Sym
*s
, int v
)
201 /* structure lookup */
202 ST_INLN Sym
*struct_find(int v
)
205 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
207 return table_ident
[v
]->sym_struct
;
210 /* find an identifier */
211 ST_INLN Sym
*sym_find(int v
)
214 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
216 return table_ident
[v
]->sym_identifier
;
219 /* push a given symbol on the symbol stack */
220 ST_FUNC Sym
*sym_push(int v
, CType
*type
, int r
, int c
)
229 s
= sym_push2(ps
, v
, type
->t
, c
);
230 s
->type
.ref
= type
->ref
;
232 /* don't record fields or anonymous symbols */
234 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
235 /* record symbol in token array */
236 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
238 ps
= &ts
->sym_struct
;
240 ps
= &ts
->sym_identifier
;
247 /* push a global identifier */
248 ST_FUNC Sym
*global_identifier_push(int v
, int t
, int c
)
251 s
= sym_push2(&global_stack
, v
, t
, c
);
252 /* don't record anonymous symbol */
253 if (v
< SYM_FIRST_ANOM
) {
254 ps
= &table_ident
[v
- TOK_IDENT
]->sym_identifier
;
255 /* modify the top most local identifier, so that
256 sym_identifier will point to 's' when popped */
258 ps
= &(*ps
)->prev_tok
;
265 /* pop symbols until top reaches 'b' */
266 ST_FUNC
void sym_pop(Sym
**ptop
, Sym
*b
)
276 /* remove symbol in token array */
278 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
279 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
281 ps
= &ts
->sym_struct
;
283 ps
= &ts
->sym_identifier
;
292 static void weaken_symbol(Sym
*sym
)
294 sym
->type
.t
|= VT_WEAK
;
299 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
300 esym_type
= ELFW(ST_TYPE
)(esym
->st_info
);
301 esym
->st_info
= ELFW(ST_INFO
)(STB_WEAK
, esym_type
);
305 static void apply_visibility(Sym
*sym
, CType
*type
)
307 int vis
= sym
->type
.t
& VT_VIS_MASK
;
308 int vis2
= type
->t
& VT_VIS_MASK
;
309 if (vis
== (STV_DEFAULT
<< VT_VIS_SHIFT
))
311 else if (vis2
== (STV_DEFAULT
<< VT_VIS_SHIFT
))
314 vis
= (vis
< vis2
) ? vis
: vis2
;
315 sym
->type
.t
&= ~VT_VIS_MASK
;
321 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
322 vis
>>= VT_VIS_SHIFT
;
323 esym
->st_other
= (esym
->st_other
& ~ELFW(ST_VISIBILITY
)(-1)) | vis
;
327 /* ------------------------------------------------------------------------- */
329 ST_FUNC
void swap(int *p
, int *q
)
337 static void vsetc(CType
*type
, int r
, CValue
*vc
)
341 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
342 tcc_error("memory full (vstack)");
343 /* cannot let cpu flags if other instruction are generated. Also
344 avoid leaving VT_JMP anywhere except on the top of the stack
345 because it would complicate the code generator. */
346 if (vtop
>= vstack
) {
347 v
= vtop
->r
& VT_VALMASK
;
348 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
358 /* push constant of type "type" with useless value */
359 ST_FUNC
void vpush(CType
*type
)
362 vsetc(type
, VT_CONST
, &cval
);
365 /* push integer constant */
366 ST_FUNC
void vpushi(int v
)
370 vsetc(&int_type
, VT_CONST
, &cval
);
373 /* push a pointer sized constant */
374 static void vpushs(addr_t v
)
378 vsetc(&size_type
, VT_CONST
, &cval
);
381 /* push arbitrary 64bit constant */
382 ST_FUNC
void vpush64(int ty
, unsigned long long v
)
389 vsetc(&ctype
, VT_CONST
, &cval
);
392 /* push long long constant */
393 static inline void vpushll(long long v
)
395 vpush64(VT_LLONG
, v
);
398 /* push a symbol value of TYPE */
399 static inline void vpushsym(CType
*type
, Sym
*sym
)
403 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
407 /* Return a static symbol pointing to a section */
408 ST_FUNC Sym
*get_sym_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
414 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
415 sym
->type
.ref
= type
->ref
;
416 sym
->r
= VT_CONST
| VT_SYM
;
417 put_extern_sym(sym
, sec
, offset
, size
);
421 /* push a reference to a section offset by adding a dummy symbol */
422 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
424 vpushsym(type
, get_sym_ref(type
, sec
, offset
, size
));
427 /* define a new external reference to a symbol 'v' of type 'u' */
428 ST_FUNC Sym
*external_global_sym(int v
, CType
*type
, int r
)
434 /* push forward reference */
435 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
436 s
->type
.ref
= type
->ref
;
437 s
->r
= r
| VT_CONST
| VT_SYM
;
442 /* define a new external reference to a symbol 'v' with alternate asm
443 name 'asm_label' of type 'u'. 'asm_label' is equal to NULL if there
444 is no alternate name (most cases) */
445 static Sym
*external_sym(int v
, CType
*type
, int r
, char *asm_label
)
451 /* push forward reference */
452 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
453 s
->asm_label
= asm_label
;
454 s
->type
.t
|= VT_EXTERN
;
455 } else if (s
->type
.ref
== func_old_type
.ref
) {
456 s
->type
.ref
= type
->ref
;
457 s
->r
= r
| VT_CONST
| VT_SYM
;
458 s
->type
.t
|= VT_EXTERN
;
459 } else if (!is_compatible_types(&s
->type
, type
)) {
460 tcc_error("incompatible types for redefinition of '%s'",
461 get_tok_str(v
, NULL
));
463 /* Merge some storage attributes. */
464 if (type
->t
& VT_WEAK
)
467 if (type
->t
& VT_VIS_MASK
)
468 apply_visibility(s
, type
);
473 /* push a reference to global symbol v */
474 ST_FUNC
void vpush_global_sym(CType
*type
, int v
)
476 vpushsym(type
, external_global_sym(v
, type
, 0));
479 ST_FUNC
void vset(CType
*type
, int r
, int v
)
484 vsetc(type
, r
, &cval
);
487 static void vseti(int r
, int v
)
495 ST_FUNC
void vswap(void)
498 /* cannot let cpu flags if other instruction are generated. Also
499 avoid leaving VT_JMP anywhere except on the top of the stack
500 because it would complicate the code generator. */
501 if (vtop
>= vstack
) {
502 int v
= vtop
->r
& VT_VALMASK
;
503 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
510 /* XXX: +2% overall speed possible with optimized memswap
512 * memswap(&vtop[0], &vtop[1], sizeof *vtop);
516 ST_FUNC
void vpushv(SValue
*v
)
518 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
519 tcc_error("memory full (vstack)");
524 static void vdup(void)
529 /* save r to the memory stack, and mark it as being free */
530 ST_FUNC
void save_reg(int r
)
532 int l
, saved
, size
, align
;
536 /* modify all stack values */
539 for(p
=vstack
;p
<=vtop
;p
++) {
540 if ((p
->r
& VT_VALMASK
) == r
||
541 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
542 /* must save value on stack if not already done */
544 /* NOTE: must reload 'r' because r might be equal to r2 */
545 r
= p
->r
& VT_VALMASK
;
546 /* store register in the stack */
548 if ((p
->r
& VT_LVAL
) ||
549 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
550 #ifdef TCC_TARGET_X86_64
551 type
= &char_pointer_type
;
555 size
= type_size(type
, &align
);
556 loc
= (loc
- size
) & -align
;
558 sv
.r
= VT_LOCAL
| VT_LVAL
;
561 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
562 /* x86 specific: need to pop fp register ST0 if saved */
564 o(0xd8dd); /* fstp %st(0) */
567 #ifndef TCC_TARGET_X86_64
568 /* special long long case */
569 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
577 /* mark that stack entry as being saved on the stack */
578 if (p
->r
& VT_LVAL
) {
579 /* also clear the bounded flag because the
580 relocation address of the function was stored in
582 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
584 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
592 #ifdef TCC_TARGET_ARM
593 /* find a register of class 'rc2' with at most one reference on stack.
594 * If none, call get_reg(rc) */
595 ST_FUNC
int get_reg_ex(int rc
, int rc2
)
600 for(r
=0;r
<NB_REGS
;r
++) {
601 if (reg_classes
[r
] & rc2
) {
604 for(p
= vstack
; p
<= vtop
; p
++) {
605 if ((p
->r
& VT_VALMASK
) == r
||
606 (p
->r2
& VT_VALMASK
) == r
)
617 /* find a free register of class 'rc'. If none, save one register */
618 ST_FUNC
int get_reg(int rc
)
623 /* find a free register */
624 for(r
=0;r
<NB_REGS
;r
++) {
625 if (reg_classes
[r
] & rc
) {
626 for(p
=vstack
;p
<=vtop
;p
++) {
627 if ((p
->r
& VT_VALMASK
) == r
||
628 (p
->r2
& VT_VALMASK
) == r
)
636 /* no register left : free the first one on the stack (VERY
637 IMPORTANT to start from the bottom to ensure that we don't
638 spill registers used in gen_opi()) */
639 for(p
=vstack
;p
<=vtop
;p
++) {
640 /* look at second register (if long long) */
641 r
= p
->r2
& VT_VALMASK
;
642 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
644 r
= p
->r
& VT_VALMASK
;
645 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
651 /* Should never comes here */
655 /* save registers up to (vtop - n) stack entry */
656 ST_FUNC
void save_regs(int n
)
661 for(p
= vstack
;p
<= p1
; p
++) {
662 r
= p
->r
& VT_VALMASK
;
669 /* move register 's' (of type 't') to 'r', and flush previous value of r to memory
671 static void move_reg(int r
, int s
, int t
)
685 /* get address of vtop (vtop MUST BE an lvalue) */
686 static void gaddrof(void)
688 if (vtop
->r
& VT_REF
)
691 /* tricky: if saved lvalue, then we can go back to lvalue */
692 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
693 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
698 #ifdef CONFIG_TCC_BCHECK
699 /* generate lvalue bound code */
700 static void gbound(void)
705 vtop
->r
&= ~VT_MUSTBOUND
;
706 /* if lvalue, then use checking code before dereferencing */
707 if (vtop
->r
& VT_LVAL
) {
708 /* if not VT_BOUNDED value, then make one */
709 if (!(vtop
->r
& VT_BOUNDED
)) {
710 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
711 /* must save type because we must set it to int to get pointer */
713 vtop
->type
.t
= VT_INT
;
716 gen_bounded_ptr_add();
717 vtop
->r
|= lval_type
;
720 /* then check for dereferencing */
721 gen_bounded_ptr_deref();
726 /* store vtop a register belonging to class 'rc'. lvalues are
727 converted to values. Cannot be used if cannot be converted to
728 register value (such as structures). */
729 ST_FUNC
int gv(int rc
)
731 int r
, bit_pos
, bit_size
, size
, align
, i
;
734 /* NOTE: get_reg can modify vstack[] */
735 if (vtop
->type
.t
& VT_BITFIELD
) {
738 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
739 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
740 /* remove bit field info to avoid loops */
741 vtop
->type
.t
&= ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
742 /* cast to int to propagate signedness in following ops */
743 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
748 if((vtop
->type
.t
& VT_UNSIGNED
) ||
749 (vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
750 type
.t
|= VT_UNSIGNED
;
752 /* generate shifts */
753 vpushi(bits
- (bit_pos
+ bit_size
));
755 vpushi(bits
- bit_size
);
756 /* NOTE: transformed to SHR if unsigned */
760 if (is_float(vtop
->type
.t
) &&
761 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
764 unsigned long offset
;
765 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
769 /* XXX: unify with initializers handling ? */
770 /* CPUs usually cannot use float constants, so we store them
771 generically in data segment */
772 size
= type_size(&vtop
->type
, &align
);
773 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
774 data_section
->data_offset
= offset
;
775 /* XXX: not portable yet */
776 #if defined(__i386__) || defined(__x86_64__)
777 /* Zero pad x87 tenbyte long doubles */
778 if (size
== LDOUBLE_SIZE
) {
779 vtop
->c
.tab
[2] &= 0xffff;
780 #if LDOUBLE_SIZE == 16
785 ptr
= section_ptr_add(data_section
, size
);
787 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
791 ptr
[i
] = vtop
->c
.tab
[size
-1-i
];
795 ptr
[i
] = vtop
->c
.tab
[i
];
796 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
797 vtop
->r
|= VT_LVAL
| VT_SYM
;
799 vtop
->c
.ptr_offset
= 0;
801 #ifdef CONFIG_TCC_BCHECK
802 if (vtop
->r
& VT_MUSTBOUND
)
806 r
= vtop
->r
& VT_VALMASK
;
807 rc2
= (rc
& RC_FLOAT
) ? RC_FLOAT
: RC_INT
;
810 #ifdef TCC_TARGET_X86_64
811 else if (rc
== RC_FRET
)
815 /* need to reload if:
817 - lvalue (need to dereference pointer)
818 - already a register, but not in the right class */
820 || (vtop
->r
& VT_LVAL
)
821 || !(reg_classes
[r
] & rc
)
822 #ifdef TCC_TARGET_X86_64
823 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
824 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
&& !(reg_classes
[vtop
->r2
] & rc2
))
826 || ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
831 #ifdef TCC_TARGET_X86_64
832 if (((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
)) {
833 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
835 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
836 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
837 unsigned long long ll
;
839 int r2
, original_type
;
840 original_type
= vtop
->type
.t
;
841 /* two register type load : expand to two words
843 #ifndef TCC_TARGET_X86_64
844 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
847 vtop
->c
.ui
= ll
; /* first word */
849 vtop
->r
= r
; /* save register value */
850 vpushi(ll
>> 32); /* second word */
853 if (r
>= VT_CONST
|| /* XXX: test to VT_CONST incorrect ? */
854 (vtop
->r
& VT_LVAL
)) {
855 /* We do not want to modifier the long long
856 pointer here, so the safest (and less
857 efficient) is to save all the other registers
858 in the stack. XXX: totally inefficient. */
860 /* load from memory */
861 vtop
->type
.t
= load_type
;
864 vtop
[-1].r
= r
; /* save register value */
865 /* increment pointer to get second word */
866 vtop
->type
.t
= addr_type
;
871 vtop
->type
.t
= load_type
;
876 vtop
[-1].r
= r
; /* save register value */
877 vtop
->r
= vtop
[-1].r2
;
879 /* Allocate second register. Here we rely on the fact that
880 get_reg() tries first to free r2 of an SValue. */
884 /* write second register */
886 vtop
->type
.t
= original_type
;
887 } else if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
889 /* lvalue of scalar type : need to use lvalue type
890 because of possible cast */
893 /* compute memory access type */
894 if (vtop
->r
& VT_REF
)
895 #ifdef TCC_TARGET_X86_64
900 else if (vtop
->r
& VT_LVAL_BYTE
)
902 else if (vtop
->r
& VT_LVAL_SHORT
)
904 if (vtop
->r
& VT_LVAL_UNSIGNED
)
908 /* restore wanted type */
911 /* one register type load */
916 #ifdef TCC_TARGET_C67
917 /* uses register pairs for doubles */
918 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
925 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
926 ST_FUNC
void gv2(int rc1
, int rc2
)
930 /* generate more generic register first. But VT_JMP or VT_CMP
931 values must be generated first in all cases to avoid possible
933 v
= vtop
[0].r
& VT_VALMASK
;
934 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
939 /* test if reload is needed for first register */
940 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
950 /* test if reload is needed for first register */
951 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
957 /* wrapper around RC_FRET to return a register by type */
958 static int rc_fret(int t
)
960 #ifdef TCC_TARGET_X86_64
961 if (t
== VT_LDOUBLE
) {
968 /* wrapper around REG_FRET to return a register by type */
969 static int reg_fret(int t
)
971 #ifdef TCC_TARGET_X86_64
972 if (t
== VT_LDOUBLE
) {
979 /* expand long long on stack in two int registers */
980 static void lexpand(void)
984 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
987 vtop
[0].r
= vtop
[-1].r2
;
988 vtop
[0].r2
= VT_CONST
;
989 vtop
[-1].r2
= VT_CONST
;
990 vtop
[0].type
.t
= VT_INT
| u
;
991 vtop
[-1].type
.t
= VT_INT
| u
;
994 #ifdef TCC_TARGET_ARM
995 /* expand long long on stack */
996 ST_FUNC
void lexpand_nr(void)
1000 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
1002 vtop
->r2
= VT_CONST
;
1003 vtop
->type
.t
= VT_INT
| u
;
1004 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
1005 if (v
== VT_CONST
) {
1006 vtop
[-1].c
.ui
= vtop
->c
.ull
;
1007 vtop
->c
.ui
= vtop
->c
.ull
>> 32;
1009 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
1011 vtop
->r
= vtop
[-1].r
;
1012 } else if (v
> VT_CONST
) {
1016 vtop
->r
= vtop
[-1].r2
;
1017 vtop
[-1].r2
= VT_CONST
;
1018 vtop
[-1].type
.t
= VT_INT
| u
;
1022 /* build a long long from two ints */
1023 static void lbuild(int t
)
1025 gv2(RC_INT
, RC_INT
);
1026 vtop
[-1].r2
= vtop
[0].r
;
1027 vtop
[-1].type
.t
= t
;
1031 /* rotate n first stack elements to the bottom
1032 I1 ... In -> I2 ... In I1 [top is right]
1034 ST_FUNC
void vrotb(int n
)
1040 for(i
=-n
+1;i
!=0;i
++)
1041 vtop
[i
] = vtop
[i
+1];
1045 /* rotate the n elements before entry e towards the top
1046 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
1048 ST_FUNC
void vrote(SValue
*e
, int n
)
1054 for(i
= 0;i
< n
- 1; i
++)
1059 /* rotate n first stack elements to the top
1060 I1 ... In -> In I1 ... I(n-1) [top is right]
1062 ST_FUNC
void vrott(int n
)
1067 /* pop stack value */
1068 ST_FUNC
void vpop(void)
1071 v
= vtop
->r
& VT_VALMASK
;
1072 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1073 /* for x86, we need to pop the FP stack */
1074 if (v
== TREG_ST0
&& !nocode_wanted
) {
1075 o(0xd8dd); /* fstp %st(0) */
1078 if (v
== VT_JMP
|| v
== VT_JMPI
) {
1079 /* need to put correct jump if && or || without test */
1085 /* convert stack entry to register and duplicate its value in another
1087 static void gv_dup(void)
1093 if ((t
& VT_BTYPE
) == VT_LLONG
) {
1100 /* stack: H L L1 H1 */
1108 /* duplicate value */
1113 #ifdef TCC_TARGET_X86_64
1114 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1124 load(r1
, &sv
); /* move r to r1 */
1126 /* duplicates value */
1132 /* Generate value test
1134 * Generate a test for any value (jump, comparison and integers) */
1135 ST_FUNC
int gvtst(int inv
, int t
)
1137 int v
= vtop
->r
& VT_VALMASK
;
1138 if (v
!= VT_CMP
&& v
!= VT_JMP
&& v
!= VT_JMPI
) {
1142 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1143 /* constant jmp optimization */
1144 if ((vtop
->c
.i
!= 0) != inv
)
1149 return gtst(inv
, t
);
1152 #ifndef TCC_TARGET_X86_64
1153 /* generate CPU independent (unsigned) long long operations */
1154 static void gen_opl(int op
)
1156 int t
, a
, b
, op1
, c
, i
;
1158 unsigned short reg_iret
= REG_IRET
;
1159 unsigned short reg_lret
= REG_LRET
;
1165 func
= TOK___divdi3
;
1168 func
= TOK___udivdi3
;
1171 func
= TOK___moddi3
;
1174 func
= TOK___umoddi3
;
1181 /* call generic long long function */
1182 vpush_global_sym(&func_old_type
, func
);
1187 vtop
->r2
= reg_lret
;
1200 /* stack: L1 H1 L2 H2 */
1205 vtop
[-2] = vtop
[-3];
1208 /* stack: H1 H2 L1 L2 */
1214 /* stack: H1 H2 L1 L2 ML MH */
1217 /* stack: ML MH H1 H2 L1 L2 */
1221 /* stack: ML MH H1 L2 H2 L1 */
1226 /* stack: ML MH M1 M2 */
1229 } else if (op
== '+' || op
== '-') {
1230 /* XXX: add non carry method too (for MIPS or alpha) */
1236 /* stack: H1 H2 (L1 op L2) */
1239 gen_op(op1
+ 1); /* TOK_xxxC2 */
1242 /* stack: H1 H2 (L1 op L2) */
1245 /* stack: (L1 op L2) H1 H2 */
1247 /* stack: (L1 op L2) (H1 op H2) */
1255 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1256 t
= vtop
[-1].type
.t
;
1260 /* stack: L H shift */
1262 /* constant: simpler */
1263 /* NOTE: all comments are for SHL. the other cases are
1264 done by swaping words */
1275 if (op
!= TOK_SAR
) {
1308 /* XXX: should provide a faster fallback on x86 ? */
1311 func
= TOK___ashrdi3
;
1314 func
= TOK___lshrdi3
;
1317 func
= TOK___ashldi3
;
1323 /* compare operations */
1329 /* stack: L1 H1 L2 H2 */
1331 vtop
[-1] = vtop
[-2];
1333 /* stack: L1 L2 H1 H2 */
1336 /* when values are equal, we need to compare low words. since
1337 the jump is inverted, we invert the test too. */
1340 else if (op1
== TOK_GT
)
1342 else if (op1
== TOK_ULT
)
1344 else if (op1
== TOK_UGT
)
1349 if (op1
!= TOK_NE
) {
1353 /* generate non equal test */
1354 /* XXX: NOT PORTABLE yet */
1358 #if defined(TCC_TARGET_I386)
1359 b
= psym(0x850f, 0);
1360 #elif defined(TCC_TARGET_ARM)
1362 o(0x1A000000 | encbranch(ind
, 0, 1));
1363 #elif defined(TCC_TARGET_C67)
1364 tcc_error("not implemented");
1366 #error not supported
1370 /* compare low. Always unsigned */
1374 else if (op1
== TOK_LE
)
1376 else if (op1
== TOK_GT
)
1378 else if (op1
== TOK_GE
)
1389 /* handle integer constant optimizations and various machine
1391 static void gen_opic(int op
)
1393 int c1
, c2
, t1
, t2
, n
;
1396 typedef unsigned long long U
;
1400 t1
= v1
->type
.t
& VT_BTYPE
;
1401 t2
= v2
->type
.t
& VT_BTYPE
;
1405 else if (v1
->type
.t
& VT_UNSIGNED
)
1412 else if (v2
->type
.t
& VT_UNSIGNED
)
1417 /* currently, we cannot do computations with forward symbols */
1418 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1419 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1422 case '+': l1
+= l2
; break;
1423 case '-': l1
-= l2
; break;
1424 case '&': l1
&= l2
; break;
1425 case '^': l1
^= l2
; break;
1426 case '|': l1
|= l2
; break;
1427 case '*': l1
*= l2
; break;
1434 /* if division by zero, generate explicit division */
1437 tcc_error("division by zero in constant");
1441 default: l1
/= l2
; break;
1442 case '%': l1
%= l2
; break;
1443 case TOK_UDIV
: l1
= (U
)l1
/ l2
; break;
1444 case TOK_UMOD
: l1
= (U
)l1
% l2
; break;
1447 case TOK_SHL
: l1
<<= l2
; break;
1448 case TOK_SHR
: l1
= (U
)l1
>> l2
; break;
1449 case TOK_SAR
: l1
>>= l2
; break;
1451 case TOK_ULT
: l1
= (U
)l1
< (U
)l2
; break;
1452 case TOK_UGE
: l1
= (U
)l1
>= (U
)l2
; break;
1453 case TOK_EQ
: l1
= l1
== l2
; break;
1454 case TOK_NE
: l1
= l1
!= l2
; break;
1455 case TOK_ULE
: l1
= (U
)l1
<= (U
)l2
; break;
1456 case TOK_UGT
: l1
= (U
)l1
> (U
)l2
; break;
1457 case TOK_LT
: l1
= l1
< l2
; break;
1458 case TOK_GE
: l1
= l1
>= l2
; break;
1459 case TOK_LE
: l1
= l1
<= l2
; break;
1460 case TOK_GT
: l1
= l1
> l2
; break;
1462 case TOK_LAND
: l1
= l1
&& l2
; break;
1463 case TOK_LOR
: l1
= l1
|| l2
; break;
1470 /* if commutative ops, put c2 as constant */
1471 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1472 op
== '|' || op
== '*')) {
1474 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1475 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1477 /* Filter out NOP operations like x*1, x-0, x&-1... */
1478 if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1481 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1482 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1488 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1489 /* try to use shifts instead of muls or divs */
1490 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1499 else if (op
== TOK_PDIV
)
1505 } else if (c2
&& (op
== '+' || op
== '-') &&
1506 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
1507 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1508 /* symbol + constant case */
1515 if (!nocode_wanted
) {
1516 /* call low level op generator */
1517 if (t1
== VT_LLONG
|| t2
== VT_LLONG
)
1528 /* generate a floating point operation with constant propagation */
1529 static void gen_opif(int op
)
1537 /* currently, we cannot do computations with forward symbols */
1538 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1539 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1541 if (v1
->type
.t
== VT_FLOAT
) {
1544 } else if (v1
->type
.t
== VT_DOUBLE
) {
1552 /* NOTE: we only do constant propagation if finite number (not
1553 NaN or infinity) (ANSI spec) */
1554 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1558 case '+': f1
+= f2
; break;
1559 case '-': f1
-= f2
; break;
1560 case '*': f1
*= f2
; break;
1564 tcc_error("division by zero in constant");
1569 /* XXX: also handles tests ? */
1573 /* XXX: overflow test ? */
1574 if (v1
->type
.t
== VT_FLOAT
) {
1576 } else if (v1
->type
.t
== VT_DOUBLE
) {
1584 if (!nocode_wanted
) {
1592 static int pointed_size(CType
*type
)
1595 return type_size(pointed_type(type
), &align
);
1598 static void vla_runtime_pointed_size(CType
*type
)
1601 vla_runtime_type_size(pointed_type(type
), &align
);
1604 static inline int is_null_pointer(SValue
*p
)
1606 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1608 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& p
->c
.i
== 0) ||
1609 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.ll
== 0) ||
1610 ((p
->type
.t
& VT_BTYPE
) == VT_PTR
&& p
->c
.ptr_offset
== 0);
1613 static inline int is_integer_btype(int bt
)
1615 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
1616 bt
== VT_INT
|| bt
== VT_LLONG
);
1619 /* check types for comparison or subtraction of pointers */
1620 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
1622 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
1625 /* null pointers are accepted for all comparisons as gcc */
1626 if (is_null_pointer(p1
) || is_null_pointer(p2
))
1630 bt1
= type1
->t
& VT_BTYPE
;
1631 bt2
= type2
->t
& VT_BTYPE
;
1632 /* accept comparison between pointer and integer with a warning */
1633 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
1634 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
1635 tcc_warning("comparison between pointer and integer");
1639 /* both must be pointers or implicit function pointers */
1640 if (bt1
== VT_PTR
) {
1641 type1
= pointed_type(type1
);
1642 } else if (bt1
!= VT_FUNC
)
1643 goto invalid_operands
;
1645 if (bt2
== VT_PTR
) {
1646 type2
= pointed_type(type2
);
1647 } else if (bt2
!= VT_FUNC
) {
1649 tcc_error("invalid operands to binary %s", get_tok_str(op
, NULL
));
1651 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1652 (type2
->t
& VT_BTYPE
) == VT_VOID
)
1656 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1657 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1658 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1659 /* gcc-like error if '-' is used */
1661 goto invalid_operands
;
1663 tcc_warning("comparison of distinct pointer types lacks a cast");
1667 /* generic gen_op: handles types problems */
1668 ST_FUNC
void gen_op(int op
)
1670 int u
, t1
, t2
, bt1
, bt2
, t
;
1673 t1
= vtop
[-1].type
.t
;
1674 t2
= vtop
[0].type
.t
;
1675 bt1
= t1
& VT_BTYPE
;
1676 bt2
= t2
& VT_BTYPE
;
1678 if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
1679 /* at least one operand is a pointer */
1680 /* relationnal op: must be both pointers */
1681 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
1682 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1683 /* pointers are handled are unsigned */
1684 #ifdef TCC_TARGET_X86_64
1685 t
= VT_LLONG
| VT_UNSIGNED
;
1687 t
= VT_INT
| VT_UNSIGNED
;
1691 /* if both pointers, then it must be the '-' op */
1692 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
1694 tcc_error("cannot use pointers here");
1695 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1696 /* XXX: check that types are compatible */
1697 if (vtop
[-1].type
.t
& VT_VLA
) {
1698 vla_runtime_pointed_size(&vtop
[-1].type
);
1700 vpushi(pointed_size(&vtop
[-1].type
));
1704 /* set to integer type */
1705 #ifdef TCC_TARGET_X86_64
1706 vtop
->type
.t
= VT_LLONG
;
1708 vtop
->type
.t
= VT_INT
;
1713 /* exactly one pointer : must be '+' or '-'. */
1714 if (op
!= '-' && op
!= '+')
1715 tcc_error("cannot use pointers here");
1716 /* Put pointer as first operand */
1717 if (bt2
== VT_PTR
) {
1721 type1
= vtop
[-1].type
;
1722 type1
.t
&= ~VT_ARRAY
;
1723 if (vtop
[-1].type
.t
& VT_VLA
)
1724 vla_runtime_pointed_size(&vtop
[-1].type
);
1726 u
= pointed_size(&vtop
[-1].type
);
1728 tcc_error("unknown array element size");
1729 #ifdef TCC_TARGET_X86_64
1732 /* XXX: cast to int ? (long long case) */
1737 #ifdef CONFIG_TCC_BCHECK
1738 /* if evaluating constant expression, no code should be
1739 generated, so no bound check */
1740 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
1741 /* if bounded pointers, we generate a special code to
1748 gen_bounded_ptr_add();
1754 /* put again type if gen_opic() swaped operands */
1757 } else if (is_float(bt1
) || is_float(bt2
)) {
1758 /* compute bigger type and do implicit casts */
1759 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
1761 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
1766 /* floats can only be used for a few operations */
1767 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
1768 (op
< TOK_ULT
|| op
> TOK_GT
))
1769 tcc_error("invalid operands for binary operation");
1771 } else if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
) {
1772 t
= bt1
== VT_LLONG
? VT_LLONG
: VT_INT
;
1773 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (t
| VT_UNSIGNED
))
1776 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
1777 /* cast to biggest op */
1779 /* convert to unsigned if it does not fit in a long long */
1780 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
1781 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
1784 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
1785 tcc_error("comparison of struct");
1787 /* integer operations */
1789 /* convert to unsigned if it does not fit in an integer */
1790 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
1791 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
1794 /* XXX: currently, some unsigned operations are explicit, so
1795 we modify them here */
1796 if (t
& VT_UNSIGNED
) {
1803 else if (op
== TOK_LT
)
1805 else if (op
== TOK_GT
)
1807 else if (op
== TOK_LE
)
1809 else if (op
== TOK_GE
)
1816 /* special case for shifts and long long: we keep the shift as
1818 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
1825 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
1826 /* relationnal op: the result is an int */
1827 vtop
->type
.t
= VT_INT
;
1834 #ifndef TCC_TARGET_ARM
1835 /* generic itof for unsigned long long case */
1836 static void gen_cvt_itof1(int t
)
1838 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
1839 (VT_LLONG
| VT_UNSIGNED
)) {
1842 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
1843 #if LDOUBLE_SIZE != 8
1844 else if (t
== VT_LDOUBLE
)
1845 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
1848 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
1852 vtop
->r
= reg_fret(t
);
1859 /* generic ftoi for unsigned long long case */
1860 static void gen_cvt_ftoi1(int t
)
1864 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
1865 /* not handled natively */
1866 st
= vtop
->type
.t
& VT_BTYPE
;
1868 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
1869 #if LDOUBLE_SIZE != 8
1870 else if (st
== VT_LDOUBLE
)
1871 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
1874 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
1879 vtop
->r2
= REG_LRET
;
1885 /* force char or short cast */
1886 static void force_charshort_cast(int t
)
1890 /* XXX: add optimization if lvalue : just change type and offset */
1895 if (t
& VT_UNSIGNED
) {
1896 vpushi((1 << bits
) - 1);
1902 /* result must be signed or the SAR is converted to an SHL
1903 This was not the case when "t" was a signed short
1904 and the last value on the stack was an unsigned int */
1905 vtop
->type
.t
&= ~VT_UNSIGNED
;
1911 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1912 static void gen_cast(CType
*type
)
1914 int sbt
, dbt
, sf
, df
, c
, p
;
1916 /* special delayed cast for char/short */
1917 /* XXX: in some cases (multiple cascaded casts), it may still
1919 if (vtop
->r
& VT_MUSTCAST
) {
1920 vtop
->r
&= ~VT_MUSTCAST
;
1921 force_charshort_cast(vtop
->type
.t
);
1924 /* bitfields first get cast to ints */
1925 if (vtop
->type
.t
& VT_BITFIELD
) {
1929 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
1930 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
1935 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1936 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
1938 /* constant case: we can do it now */
1939 /* XXX: in ISOC, cannot do it if error in convert */
1940 if (sbt
== VT_FLOAT
)
1941 vtop
->c
.ld
= vtop
->c
.f
;
1942 else if (sbt
== VT_DOUBLE
)
1943 vtop
->c
.ld
= vtop
->c
.d
;
1946 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
1947 if (sbt
& VT_UNSIGNED
)
1948 vtop
->c
.ld
= vtop
->c
.ull
;
1950 vtop
->c
.ld
= vtop
->c
.ll
;
1952 if (sbt
& VT_UNSIGNED
)
1953 vtop
->c
.ld
= vtop
->c
.ui
;
1955 vtop
->c
.ld
= vtop
->c
.i
;
1958 if (dbt
== VT_FLOAT
)
1959 vtop
->c
.f
= (float)vtop
->c
.ld
;
1960 else if (dbt
== VT_DOUBLE
)
1961 vtop
->c
.d
= (double)vtop
->c
.ld
;
1962 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
1963 vtop
->c
.ull
= (unsigned long long)vtop
->c
.ld
;
1964 } else if (sf
&& dbt
== VT_BOOL
) {
1965 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
1968 vtop
->c
.ll
= (long long)vtop
->c
.ld
;
1969 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
1970 vtop
->c
.ll
= vtop
->c
.ull
;
1971 else if (sbt
& VT_UNSIGNED
)
1972 vtop
->c
.ll
= vtop
->c
.ui
;
1973 #ifdef TCC_TARGET_X86_64
1974 else if (sbt
== VT_PTR
)
1977 else if (sbt
!= VT_LLONG
)
1978 vtop
->c
.ll
= vtop
->c
.i
;
1980 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
1981 vtop
->c
.ull
= vtop
->c
.ll
;
1982 else if (dbt
== VT_BOOL
)
1983 vtop
->c
.i
= (vtop
->c
.ll
!= 0);
1984 #ifdef TCC_TARGET_X86_64
1985 else if (dbt
== VT_PTR
)
1988 else if (dbt
!= VT_LLONG
) {
1989 int s
, dt
, warr
= 0;
1991 dt
= dbt
& VT_BTYPE
;
1994 if((ll
!= (unsigned char)ll
) && (ll
!= (char)ll
))
1997 }else if (dt
== VT_SHORT
){
1998 if((ll
!= (unsigned short)ll
) && (ll
!= (short)ll
))
2002 if((ll
!= (unsigned int)ll
) && (ll
!= (int)ll
))
2006 if(warr
&& !is_force
){
2008 tcc_warning("large integer implicitly truncated to unsigned type");
2011 tcc_warning("overflow in implicit constant conversion");
2013 if(dbt
& VT_UNSIGNED
)
2014 vtop
->c
.ui
= ((unsigned int)vtop
->c
.ll
<< s
) >> s
;
2016 vtop
->c
.i
= ((int)vtop
->c
.ll
<< s
) >> s
;
2019 } else if (p
&& dbt
== VT_BOOL
) {
2022 } else if (!nocode_wanted
) {
2023 /* non constant case: generate code */
2025 /* convert from fp to fp */
2028 /* convert int to fp */
2031 /* convert fp to int */
2032 if (dbt
== VT_BOOL
) {
2036 /* we handle char/short/etc... with generic code */
2037 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
2038 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
2042 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
2043 /* additional cast for char/short... */
2048 #ifndef TCC_TARGET_X86_64
2049 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
2050 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
2051 /* scalar to long long */
2052 /* machine independent conversion */
2054 /* generate high word */
2055 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
2059 if (sbt
== VT_PTR
) {
2060 /* cast from pointer to int before we apply
2061 shift operation, which pointers don't support*/
2062 gen_cast(&int_type
);
2068 /* patch second register */
2069 vtop
[-1].r2
= vtop
->r
;
2073 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
2074 (dbt
& VT_BTYPE
) == VT_PTR
||
2075 (dbt
& VT_BTYPE
) == VT_FUNC
) {
2076 if ((sbt
& VT_BTYPE
) != VT_LLONG
&&
2077 (sbt
& VT_BTYPE
) != VT_PTR
&&
2078 (sbt
& VT_BTYPE
) != VT_FUNC
) {
2079 /* need to convert from 32bit to 64bit */
2081 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
2082 /* x86_64 specific: movslq */
2084 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
2088 } else if (dbt
== VT_BOOL
) {
2089 /* scalar to bool */
2092 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
2093 (dbt
& VT_BTYPE
) == VT_SHORT
) {
2094 if (sbt
== VT_PTR
) {
2095 vtop
->type
.t
= VT_INT
;
2096 tcc_warning("nonportable conversion from pointer to char/short");
2098 force_charshort_cast(dbt
);
2099 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
2101 if (sbt
== VT_LLONG
) {
2102 /* from long long: just take low order word */
2106 /* if lvalue and single word type, nothing to do because
2107 the lvalue already contains the real type size (see
2108 VT_LVAL_xxx constants) */
2111 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
2112 /* if we are casting between pointer types,
2113 we must update the VT_LVAL_xxx size */
2114 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
2115 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
2120 /* return type size as known at compile time. Put alignment at 'a' */
2121 ST_FUNC
int type_size(CType
*type
, int *a
)
2126 bt
= type
->t
& VT_BTYPE
;
2127 if (bt
== VT_STRUCT
) {
2132 } else if (bt
== VT_PTR
) {
2133 if (type
->t
& VT_ARRAY
) {
2137 ts
= type_size(&s
->type
, a
);
2139 if (ts
< 0 && s
->c
< 0)
2147 } else if (bt
== VT_LDOUBLE
) {
2149 return LDOUBLE_SIZE
;
2150 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
2151 #ifdef TCC_TARGET_I386
2152 #ifdef TCC_TARGET_PE
2157 #elif defined(TCC_TARGET_ARM)
2167 } else if (bt
== VT_INT
|| bt
== VT_ENUM
|| bt
== VT_FLOAT
) {
2170 } else if (bt
== VT_SHORT
) {
2173 } else if (bt
== VT_QLONG
|| bt
== VT_QFLOAT
) {
2177 /* char, void, function, _Bool */
2183 /* push type size as known at runtime time on top of value stack. Put
2185 ST_FUNC
void vla_runtime_type_size(CType
*type
, int *a
)
2187 if (type
->t
& VT_VLA
) {
2188 vset(&int_type
, VT_LOCAL
|VT_LVAL
, type
->ref
->c
);
2190 vpushi(type_size(type
, a
));
2194 static void vla_sp_save(void) {
2195 if (!(vla_flags
& VLA_SP_LOC_SET
)) {
2196 *vla_sp_loc
= (loc
-= PTR_SIZE
);
2197 vla_flags
|= VLA_SP_LOC_SET
;
2199 if (!(vla_flags
& VLA_SP_SAVED
)) {
2200 gen_vla_sp_save(*vla_sp_loc
);
2201 vla_flags
|= VLA_SP_SAVED
;
2205 /* return the pointed type of t */
2206 static inline CType
*pointed_type(CType
*type
)
2208 return &type
->ref
->type
;
2211 /* modify type so that its it is a pointer to type. */
2212 ST_FUNC
void mk_pointer(CType
*type
)
2215 s
= sym_push(SYM_FIELD
, type
, 0, -1);
2216 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
2220 /* compare function types. OLD functions match any new functions */
2221 static int is_compatible_func(CType
*type1
, CType
*type2
)
2227 if (!is_compatible_types(&s1
->type
, &s2
->type
))
2229 /* check func_call */
2230 if (s1
->a
.func_call
!= s2
->a
.func_call
)
2232 /* XXX: not complete */
2233 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
2237 while (s1
!= NULL
) {
2240 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
2250 /* return true if type1 and type2 are the same. If unqualified is
2251 true, qualifiers on the types are ignored.
2253 - enums are not checked as gcc __builtin_types_compatible_p ()
2255 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2259 t1
= type1
->t
& VT_TYPE
;
2260 t2
= type2
->t
& VT_TYPE
;
2262 /* strip qualifiers before comparing */
2263 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2264 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2266 /* Default Vs explicit signedness only matters for char */
2267 if ((t1
& VT_BTYPE
) != VT_BYTE
) {
2271 /* XXX: bitfields ? */
2274 /* test more complicated cases */
2275 bt1
= t1
& VT_BTYPE
;
2276 if (bt1
== VT_PTR
) {
2277 type1
= pointed_type(type1
);
2278 type2
= pointed_type(type2
);
2279 return is_compatible_types(type1
, type2
);
2280 } else if (bt1
== VT_STRUCT
) {
2281 return (type1
->ref
== type2
->ref
);
2282 } else if (bt1
== VT_FUNC
) {
2283 return is_compatible_func(type1
, type2
);
2289 /* return true if type1 and type2 are exactly the same (including
2292 static int is_compatible_types(CType
*type1
, CType
*type2
)
2294 return compare_types(type1
,type2
,0);
2297 /* return true if type1 and type2 are the same (ignoring qualifiers).
2299 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
2301 return compare_types(type1
,type2
,1);
2304 /* print a type. If 'varstr' is not NULL, then the variable is also
2305 printed in the type */
2307 /* XXX: add array and function pointers */
2308 static void type_to_str(char *buf
, int buf_size
,
2309 CType
*type
, const char *varstr
)
2316 t
= type
->t
& VT_TYPE
;
2319 if (t
& VT_CONSTANT
)
2320 pstrcat(buf
, buf_size
, "const ");
2321 if (t
& VT_VOLATILE
)
2322 pstrcat(buf
, buf_size
, "volatile ");
2323 if ((t
& (VT_DEFSIGN
| VT_UNSIGNED
)) == (VT_DEFSIGN
| VT_UNSIGNED
))
2324 pstrcat(buf
, buf_size
, "unsigned ");
2325 else if (t
& VT_DEFSIGN
)
2326 pstrcat(buf
, buf_size
, "signed ");
2356 tstr
= "long double";
2358 pstrcat(buf
, buf_size
, tstr
);
2362 if (bt
== VT_STRUCT
)
2366 pstrcat(buf
, buf_size
, tstr
);
2367 v
= type
->ref
->v
& ~SYM_STRUCT
;
2368 if (v
>= SYM_FIRST_ANOM
)
2369 pstrcat(buf
, buf_size
, "<anonymous>");
2371 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
2375 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
2376 pstrcat(buf
, buf_size
, "(");
2378 while (sa
!= NULL
) {
2379 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
2380 pstrcat(buf
, buf_size
, buf1
);
2383 pstrcat(buf
, buf_size
, ", ");
2385 pstrcat(buf
, buf_size
, ")");
2389 pstrcpy(buf1
, sizeof(buf1
), "*");
2391 pstrcat(buf1
, sizeof(buf1
), varstr
);
2392 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2396 pstrcat(buf
, buf_size
, " ");
2397 pstrcat(buf
, buf_size
, varstr
);
2402 /* verify type compatibility to store vtop in 'dt' type, and generate
2404 static void gen_assign_cast(CType
*dt
)
2406 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
2407 char buf1
[256], buf2
[256];
2410 st
= &vtop
->type
; /* source type */
2411 dbt
= dt
->t
& VT_BTYPE
;
2412 sbt
= st
->t
& VT_BTYPE
;
2413 if (sbt
== VT_VOID
|| dbt
== VT_VOID
)
2414 tcc_error("cannot cast from/to void");
2415 if (dt
->t
& VT_CONSTANT
)
2416 tcc_warning("assignment of read-only location");
2419 /* special cases for pointers */
2420 /* '0' can also be a pointer */
2421 if (is_null_pointer(vtop
))
2423 /* accept implicit pointer to integer cast with warning */
2424 if (is_integer_btype(sbt
)) {
2425 tcc_warning("assignment makes pointer from integer without a cast");
2428 type1
= pointed_type(dt
);
2429 /* a function is implicitely a function pointer */
2430 if (sbt
== VT_FUNC
) {
2431 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
2432 !is_compatible_types(pointed_type(dt
), st
))
2433 tcc_warning("assignment from incompatible pointer type");
2438 type2
= pointed_type(st
);
2439 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2440 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
2441 /* void * can match anything */
2443 /* exact type match, except for unsigned */
2446 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
|
2448 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
|
2450 if (!is_compatible_types(&tmp_type1
, &tmp_type2
))
2451 tcc_warning("assignment from incompatible pointer type");
2453 /* check const and volatile */
2454 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
2455 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
2456 tcc_warning("assignment discards qualifiers from pointer target type");
2462 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
2463 tcc_warning("assignment makes integer from pointer without a cast");
2465 if (sbt
== VT_STRUCT
)
2467 /* XXX: more tests */
2472 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2473 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2474 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2476 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
2477 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
2478 tcc_error("cannot cast '%s' to '%s'", buf1
, buf2
);
2486 /* store vtop in lvalue pushed on stack */
2487 ST_FUNC
void vstore(void)
2489 int sbt
, dbt
, ft
, cc
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
2491 ft
= vtop
[-1].type
.t
;
2492 sbt
= vtop
->type
.t
& VT_BTYPE
;
2493 dbt
= ft
& VT_BTYPE
;
2494 cc
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
2495 if ((((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2496 (sbt
== VT_INT
&& dbt
== VT_SHORT
))
2497 && !(vtop
->type
.t
& VT_BITFIELD
) && !cc
) {
2498 /* optimize char/short casts */
2499 delayed_cast
= VT_MUSTCAST
;
2500 vtop
->type
.t
= ft
& (VT_TYPE
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
)));
2501 /* XXX: factorize */
2502 if (ft
& VT_CONSTANT
)
2503 tcc_warning("assignment of read-only location");
2506 if (!(ft
& VT_BITFIELD
))
2507 gen_assign_cast(&vtop
[-1].type
);
2510 if (sbt
== VT_STRUCT
) {
2511 /* if structure, only generate pointer */
2512 /* structure assignment : generate memcpy */
2513 /* XXX: optimize if small size */
2514 if (!nocode_wanted
) {
2515 size
= type_size(&vtop
->type
, &align
);
2519 vtop
->type
.t
= VT_PTR
;
2522 /* address of memcpy() */
2525 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2526 else if(!(align
& 3))
2527 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2530 vpush_global_sym(&func_old_type
, TOK_memcpy
);
2535 vtop
->type
.t
= VT_PTR
;
2544 /* leave source on stack */
2545 } else if (ft
& VT_BITFIELD
) {
2546 /* bitfield store handling */
2547 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2548 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2549 /* remove bit field info to avoid loops */
2550 vtop
[-1].type
.t
= ft
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
2552 /* duplicate source into other register */
2557 if((ft
& VT_BTYPE
) == VT_BOOL
) {
2558 gen_cast(&vtop
[-1].type
);
2559 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2562 /* duplicate destination */
2564 vtop
[-1] = vtop
[-2];
2566 /* mask and shift source */
2567 if((ft
& VT_BTYPE
) != VT_BOOL
) {
2568 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2569 vpushll((1ULL << bit_size
) - 1ULL);
2571 vpushi((1 << bit_size
) - 1);
2577 /* load destination, mask and or with source */
2579 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2580 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2582 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2589 /* pop off shifted source from "duplicate source..." above */
2593 #ifdef CONFIG_TCC_BCHECK
2594 /* bound check case */
2595 if (vtop
[-1].r
& VT_MUSTBOUND
) {
2601 if (!nocode_wanted
) {
2605 #ifdef TCC_TARGET_X86_64
2606 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
2608 } else if ((ft
& VT_BTYPE
) == VT_QFLOAT
) {
2613 r
= gv(rc
); /* generate value */
2614 /* if lvalue was saved on stack, must read it */
2615 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
2617 t
= get_reg(RC_INT
);
2618 #ifdef TCC_TARGET_X86_64
2623 sv
.r
= VT_LOCAL
| VT_LVAL
;
2624 sv
.c
.ul
= vtop
[-1].c
.ul
;
2626 vtop
[-1].r
= t
| VT_LVAL
;
2628 /* two word case handling : store second register at word + 4 (or +8 for x86-64) */
2629 #ifdef TCC_TARGET_X86_64
2630 if (((ft
& VT_BTYPE
) == VT_QLONG
) || ((ft
& VT_BTYPE
) == VT_QFLOAT
)) {
2631 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
2633 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
2634 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
2636 vtop
[-1].type
.t
= load_type
;
2639 /* convert to int to increment easily */
2640 vtop
->type
.t
= addr_type
;
2646 vtop
[-1].type
.t
= load_type
;
2647 /* XXX: it works because r2 is spilled last ! */
2648 store(vtop
->r2
, vtop
- 1);
2654 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
2655 vtop
->r
|= delayed_cast
;
2659 /* post defines POST/PRE add. c is the token ++ or -- */
2660 ST_FUNC
void inc(int post
, int c
)
2663 vdup(); /* save lvalue */
2665 gv_dup(); /* duplicate value */
2670 vpushi(c
- TOK_MID
);
2672 vstore(); /* store value */
2674 vpop(); /* if post op, return saved value */
2677 /* Parse GNUC __attribute__ extension. Currently, the following
2678 extensions are recognized:
2679 - aligned(n) : set data/function alignment.
2680 - packed : force data alignment to 1
2681 - section(x) : generate data/code in this section.
2682 - unused : currently ignored, but may be used someday.
2683 - regparm(n) : pass function parameters in registers (i386 only)
2685 static void parse_attribute(AttributeDef
*ad
)
2689 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
2693 while (tok
!= ')') {
2694 if (tok
< TOK_IDENT
)
2695 expect("attribute name");
2703 expect("section name");
2704 ad
->section
= find_section(tcc_state
, (char *)tokc
.cstr
->data
);
2712 expect("alias(\"target\")");
2713 ad
->alias_target
= /* save string as token, for later */
2714 tok_alloc((char*)tokc
.cstr
->data
, tokc
.cstr
->size
-1)->tok
;
2718 case TOK_VISIBILITY1
:
2719 case TOK_VISIBILITY2
:
2722 expect("visibility(\"default|hidden|internal|protected\")");
2723 if (!strcmp (tokc
.cstr
->data
, "default"))
2724 ad
->a
.visibility
= STV_DEFAULT
;
2725 else if (!strcmp (tokc
.cstr
->data
, "hidden"))
2726 ad
->a
.visibility
= STV_HIDDEN
;
2727 else if (!strcmp (tokc
.cstr
->data
, "internal"))
2728 ad
->a
.visibility
= STV_INTERNAL
;
2729 else if (!strcmp (tokc
.cstr
->data
, "protected"))
2730 ad
->a
.visibility
= STV_PROTECTED
;
2732 expect("visibility(\"default|hidden|internal|protected\")");
2741 if (n
<= 0 || (n
& (n
- 1)) != 0)
2742 tcc_error("alignment must be a positive power of two");
2759 /* currently, no need to handle it because tcc does not
2760 track unused objects */
2764 /* currently, no need to handle it because tcc does not
2765 track unused objects */
2770 ad
->a
.func_call
= FUNC_CDECL
;
2775 ad
->a
.func_call
= FUNC_STDCALL
;
2777 #ifdef TCC_TARGET_I386
2787 ad
->a
.func_call
= FUNC_FASTCALL1
+ n
- 1;
2793 ad
->a
.func_call
= FUNC_FASTCALLW
;
2800 ad
->a
.mode
= VT_LLONG
+ 1;
2803 ad
->a
.mode
= VT_SHORT
+ 1;
2806 ad
->a
.mode
= VT_INT
+ 1;
2809 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
2816 ad
->a
.func_export
= 1;
2819 ad
->a
.func_import
= 1;
2822 if (tcc_state
->warn_unsupported
)
2823 tcc_warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
2824 /* skip parameters */
2826 int parenthesis
= 0;
2830 else if (tok
== ')')
2833 } while (parenthesis
&& tok
!= -1);
2846 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2847 static void struct_decl(CType
*type
, int u
, int tdef
)
2849 int a
, v
, size
, align
, maxalign
, c
, offset
, flexible
;
2850 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
, prevbt
;
2851 Sym
*s
, *ss
, *ass
, **ps
;
2855 a
= tok
; /* save decl type */
2860 /* struct already defined ? return it */
2862 expect("struct/union/enum name");
2866 tcc_error("invalid type: '%s'", get_tok_str(v
, NULL
));
2868 } else if (tok
>= TOK_IDENT
&& !tdef
)
2869 tcc_error("unknown struct/union/enum");
2875 /* we put an undefined size for struct/union */
2876 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
2877 s
->r
= 0; /* default alignment is zero as gcc */
2878 /* put struct/union/enum name in type */
2886 tcc_error("struct/union/enum already defined");
2887 /* cannot be empty */
2889 /* non empty enums are not allowed */
2890 if (a
== TOK_ENUM
) {
2894 expect("identifier");
2896 if (ss
&& !local_stack
)
2897 tcc_error("redefinition of enumerator '%s'",
2898 get_tok_str(v
, NULL
));
2904 /* enum symbols have static storage */
2905 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
2906 ss
->type
.t
|= VT_STATIC
;
2911 /* NOTE: we accept a trailing comma */
2915 s
->c
= type_size(&int_type
, &align
);
2924 while (tok
!= '}') {
2925 parse_btype(&btype
, &ad
);
2928 tcc_error("flexible array member '%s' not at the end of struct",
2929 get_tok_str(v
, NULL
));
2934 type_decl(&type1
, &ad
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
2935 if (v
== 0 && (type1
.t
& VT_BTYPE
) != VT_STRUCT
)
2936 expect("identifier");
2937 if (type_size(&type1
, &align
) < 0) {
2938 if ((a
== TOK_STRUCT
) && (type1
.t
& VT_ARRAY
))
2941 tcc_error("field '%s' has incomplete type",
2942 get_tok_str(v
, NULL
));
2944 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
2945 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
2946 tcc_error("invalid type for '%s'",
2947 get_tok_str(v
, NULL
));
2951 bit_size
= expr_const();
2952 /* XXX: handle v = 0 case for messages */
2954 tcc_error("negative width in bit-field '%s'",
2955 get_tok_str(v
, NULL
));
2956 if (v
&& bit_size
== 0)
2957 tcc_error("zero width for bit-field '%s'",
2958 get_tok_str(v
, NULL
));
2960 size
= type_size(&type1
, &align
);
2962 if (align
< ad
.a
.aligned
)
2963 align
= ad
.a
.aligned
;
2964 } else if (ad
.a
.packed
) {
2966 } else if (*tcc_state
->pack_stack_ptr
) {
2967 if (align
> *tcc_state
->pack_stack_ptr
)
2968 align
= *tcc_state
->pack_stack_ptr
;
2971 if (bit_size
>= 0) {
2972 bt
= type1
.t
& VT_BTYPE
;
2979 tcc_error("bitfields must have scalar type");
2981 if (bit_size
> bsize
) {
2982 tcc_error("width of '%s' exceeds its type",
2983 get_tok_str(v
, NULL
));
2984 } else if (bit_size
== bsize
) {
2985 /* no need for bit fields */
2987 } else if (bit_size
== 0) {
2988 /* XXX: what to do if only padding in a
2990 /* zero size: means to pad */
2993 /* we do not have enough room ?
2994 did the type change?
2996 if ((bit_pos
+ bit_size
) > bsize
||
2997 bt
!= prevbt
|| a
== TOK_UNION
)
3000 /* XXX: handle LSB first */
3001 type1
.t
|= VT_BITFIELD
|
3002 (bit_pos
<< VT_STRUCT_SHIFT
) |
3003 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
3004 bit_pos
+= bit_size
;
3010 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
3011 /* add new memory data only if starting
3013 if (lbit_pos
== 0) {
3014 if (a
== TOK_STRUCT
) {
3015 c
= (c
+ align
- 1) & -align
;
3024 if (align
> maxalign
)
3028 printf("add field %s offset=%d",
3029 get_tok_str(v
, NULL
), offset
);
3030 if (type1
.t
& VT_BITFIELD
) {
3031 printf(" pos=%d size=%d",
3032 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
3033 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
3038 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
3040 while ((ass
= ass
->next
) != NULL
) {
3041 ss
= sym_push(ass
->v
, &ass
->type
, 0, offset
+ ass
->c
);
3046 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
3050 if (tok
== ';' || tok
== TOK_EOF
)
3058 tcc_error("flexible array member '%s' in otherwise empty struct", get_tok_str(v
, NULL
));
3059 /* store size and alignment */
3060 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
3066 /* return 1 if basic type is a type size (short, long, long long) */
3067 ST_FUNC
int is_btype_size(int bt
)
3069 return bt
== VT_SHORT
|| bt
== VT_LONG
|| bt
== VT_LLONG
;
3072 /* return 0 if no type declaration. otherwise, return the basic type
3075 static int parse_btype(CType
*type
, AttributeDef
*ad
)
3077 int t
, u
, bt_size
, complete
, type_found
, typespec_found
;
3081 memset(ad
, 0, sizeof(AttributeDef
));
3089 /* currently, we really ignore extension */
3100 tcc_error("too many basic types");
3102 bt_size
= is_btype_size (u
& VT_BTYPE
);
3103 if (u
== VT_INT
|| (!bt_size
&& !(t
& VT_TYPEDEF
)))
3118 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
3119 #ifndef TCC_TARGET_PE
3120 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3122 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
3123 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3137 if ((t
& VT_BTYPE
) == VT_LONG
) {
3138 #ifdef TCC_TARGET_PE
3139 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
3141 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3149 struct_decl(&type1
, VT_ENUM
, t
& VT_TYPEDEF
);
3152 type
->ref
= type1
.ref
;
3156 struct_decl(&type1
, VT_STRUCT
, t
& VT_TYPEDEF
);
3159 /* type modifiers */
3175 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == (VT_DEFSIGN
|VT_UNSIGNED
))
3176 tcc_error("signed and unsigned modifier");
3189 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == VT_DEFSIGN
)
3190 tcc_error("signed and unsigned modifier");
3191 t
|= VT_DEFSIGN
| VT_UNSIGNED
;
3216 /* GNUC attribute */
3217 case TOK_ATTRIBUTE1
:
3218 case TOK_ATTRIBUTE2
:
3219 parse_attribute(ad
);
3222 t
= (t
& ~VT_BTYPE
) | u
;
3230 parse_expr_type(&type1
);
3231 /* remove all storage modifiers except typedef */
3232 type1
.t
&= ~(VT_STORAGE
&~VT_TYPEDEF
);
3238 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
3240 t
|= (s
->type
.t
& ~VT_TYPEDEF
);
3241 type
->ref
= s
->type
.ref
;
3243 /* get attributes from typedef */
3244 if (0 == ad
->a
.aligned
)
3245 ad
->a
.aligned
= s
->a
.aligned
;
3246 if (0 == ad
->a
.func_call
)
3247 ad
->a
.func_call
= s
->a
.func_call
;
3248 ad
->a
.packed
|= s
->a
.packed
;
3257 if (tcc_state
->char_is_unsigned
) {
3258 if ((t
& (VT_DEFSIGN
|VT_BTYPE
)) == VT_BYTE
)
3262 /* long is never used as type */
3263 if ((t
& VT_BTYPE
) == VT_LONG
)
3264 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
3265 t
= (t
& ~VT_BTYPE
) | VT_INT
;
3267 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3273 /* convert a function parameter type (array to pointer and function to
3274 function pointer) */
3275 static inline void convert_parameter_type(CType
*pt
)
3277 /* remove const and volatile qualifiers (XXX: const could be used
3278 to indicate a const function parameter */
3279 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3280 /* array must be transformed to pointer according to ANSI C */
3282 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
3287 ST_FUNC
void parse_asm_str(CString
*astr
)
3290 /* read the string */
3292 expect("string constant");
3294 while (tok
== TOK_STR
) {
3295 /* XXX: add \0 handling too ? */
3296 cstr_cat(astr
, tokc
.cstr
->data
);
3299 cstr_ccat(astr
, '\0');
3302 /* Parse an asm label and return the label
3303 * Don't forget to free the CString in the caller! */
3304 static void asm_label_instr(CString
*astr
)
3307 parse_asm_str(astr
);
3310 printf("asm_alias: \"%s\"\n", (char *)astr
->data
);
3314 static void post_type(CType
*type
, AttributeDef
*ad
)
3316 int n
, l
, t1
, arg_size
, align
;
3317 Sym
**plast
, *s
, *first
;
3322 /* function declaration */
3330 /* read param name and compute offset */
3331 if (l
!= FUNC_OLD
) {
3332 if (!parse_btype(&pt
, &ad1
)) {
3334 tcc_error("invalid type");
3341 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
3343 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3344 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
3345 tcc_error("parameter declared as void");
3346 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
3351 expect("identifier");
3355 convert_parameter_type(&pt
);
3356 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
3362 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
3369 /* if no parameters, then old type prototype */
3373 /* NOTE: const is ignored in returned type as it has a special
3374 meaning in gcc / C++ */
3375 type
->t
&= ~VT_CONSTANT
;
3376 /* some ancient pre-K&R C allows a function to return an array
3377 and the array brackets to be put after the arguments, such
3378 that "int c()[]" means something like "int[] c()" */
3381 skip(']'); /* only handle simple "[]" */
3384 /* we push a anonymous symbol which will contain the function prototype */
3385 ad
->a
.func_args
= arg_size
;
3386 s
= sym_push(SYM_FIELD
, type
, 0, l
);
3391 } else if (tok
== '[') {
3392 /* array definition */
3394 if (tok
== TOK_RESTRICT1
)
3399 if (!local_stack
|| nocode_wanted
)
3400 vpushi(expr_const());
3402 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3405 tcc_error("invalid array size");
3407 if (!is_integer_btype(vtop
->type
.t
& VT_BTYPE
))
3408 tcc_error("size of variable length array should be an integer");
3413 /* parse next post type */
3414 post_type(type
, ad
);
3415 if (type
->t
== VT_FUNC
)
3416 tcc_error("declaration of an array of functions");
3417 t1
|= type
->t
& VT_VLA
;
3420 loc
-= type_size(&int_type
, &align
);
3424 vla_runtime_type_size(type
, &align
);
3426 vset(&int_type
, VT_LOCAL
|VT_LVAL
, loc
);
3433 /* we push an anonymous symbol which will contain the array
3435 s
= sym_push(SYM_FIELD
, type
, 0, n
);
3436 type
->t
= (t1
? VT_VLA
: VT_ARRAY
) | VT_PTR
;
3441 /* Parse a type declaration (except basic type), and return the type
3442 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3443 expected. 'type' should contain the basic type. 'ad' is the
3444 attribute definition of the basic type. It can be modified by
3447 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
3450 CType type1
, *type2
;
3451 int qualifiers
, storage
;
3453 while (tok
== '*') {
3461 qualifiers
|= VT_CONSTANT
;
3466 qualifiers
|= VT_VOLATILE
;
3474 type
->t
|= qualifiers
;
3477 /* XXX: clarify attribute handling */
3478 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3479 parse_attribute(ad
);
3481 /* recursive type */
3482 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3483 type1
.t
= 0; /* XXX: same as int */
3486 /* XXX: this is not correct to modify 'ad' at this point, but
3487 the syntax is not clear */
3488 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3489 parse_attribute(ad
);
3490 type_decl(&type1
, ad
, v
, td
);
3493 /* type identifier */
3494 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
3498 if (!(td
& TYPE_ABSTRACT
))
3499 expect("identifier");
3503 storage
= type
->t
& VT_STORAGE
;
3504 type
->t
&= ~VT_STORAGE
;
3505 if (storage
& VT_STATIC
) {
3506 int saved_nocode_wanted
= nocode_wanted
;
3508 post_type(type
, ad
);
3509 nocode_wanted
= saved_nocode_wanted
;
3511 post_type(type
, ad
);
3513 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3514 parse_attribute(ad
);
3518 /* append type at the end of type1 */
3531 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3532 ST_FUNC
int lvalue_type(int t
)
3537 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
3539 else if (bt
== VT_SHORT
)
3543 if (t
& VT_UNSIGNED
)
3544 r
|= VT_LVAL_UNSIGNED
;
3548 /* indirection with full error checking and bound check */
3549 ST_FUNC
void indir(void)
3551 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
3552 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
3556 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
3558 vtop
->type
= *pointed_type(&vtop
->type
);
3559 /* Arrays and functions are never lvalues */
3560 if (!(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_VLA
)
3561 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3562 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3563 /* if bound checking, the referenced pointer must be checked */
3564 #ifdef CONFIG_TCC_BCHECK
3565 if (tcc_state
->do_bounds_check
)
3566 vtop
->r
|= VT_MUSTBOUND
;
3571 /* pass a parameter to a function and do type checking and casting */
3572 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
3577 func_type
= func
->c
;
3578 if (func_type
== FUNC_OLD
||
3579 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
3580 /* default casting : only need to convert float to double */
3581 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
3584 } else if (vtop
->type
.t
& VT_BITFIELD
) {
3585 type
.t
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
3588 } else if (arg
== NULL
) {
3589 tcc_error("too many arguments to function");
3592 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
3593 gen_assign_cast(&type
);
3597 /* parse an expression of the form '(type)' or '(expr)' and return its
3599 static void parse_expr_type(CType
*type
)
3605 if (parse_btype(type
, &ad
)) {
3606 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3613 static void parse_type(CType
*type
)
3618 if (!parse_btype(type
, &ad
)) {
3621 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3624 static void vpush_tokc(int t
)
3629 vsetc(&type
, VT_CONST
, &tokc
);
3632 ST_FUNC
void unary(void)
3634 int n
, t
, align
, size
, r
, sizeof_caller
;
3638 static int in_sizeof
= 0;
3640 sizeof_caller
= in_sizeof
;
3642 /* XXX: GCC 2.95.3 does not generate a table although it should be
3656 vpush_tokc(VT_INT
| VT_UNSIGNED
);
3660 vpush_tokc(VT_LLONG
);
3664 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
3668 vpush_tokc(VT_FLOAT
);
3672 vpush_tokc(VT_DOUBLE
);
3676 vpush_tokc(VT_LDOUBLE
);
3679 case TOK___FUNCTION__
:
3681 goto tok_identifier
;
3687 /* special function name identifier */
3688 len
= strlen(funcname
) + 1;
3689 /* generate char[len] type */
3694 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
3695 ptr
= section_ptr_add(data_section
, len
);
3696 memcpy(ptr
, funcname
, len
);
3701 #ifdef TCC_TARGET_PE
3702 t
= VT_SHORT
| VT_UNSIGNED
;
3708 /* string parsing */
3711 if (tcc_state
->warn_write_strings
)
3716 memset(&ad
, 0, sizeof(AttributeDef
));
3717 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, NULL
, 0);
3722 if (parse_btype(&type
, &ad
)) {
3723 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
3725 /* check ISOC99 compound literal */
3727 /* data is allocated locally by default */
3732 /* all except arrays are lvalues */
3733 if (!(type
.t
& VT_ARRAY
))
3734 r
|= lvalue_type(type
.t
);
3735 memset(&ad
, 0, sizeof(AttributeDef
));
3736 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, NULL
, 0);
3738 if (sizeof_caller
) {
3747 } else if (tok
== '{') {
3748 /* save all registers */
3750 /* statement expression : we do not accept break/continue
3751 inside as GCC does */
3752 block(NULL
, NULL
, NULL
, NULL
, 0, 1);
3767 /* functions names must be treated as function pointers,
3768 except for unary '&' and sizeof. Since we consider that
3769 functions are not lvalues, we only have to handle it
3770 there and in function calls. */
3771 /* arrays can also be used although they are not lvalues */
3772 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
3773 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
3775 mk_pointer(&vtop
->type
);
3781 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3783 boolean
.t
= VT_BOOL
;
3785 vtop
->c
.i
= !vtop
->c
.i
;
3786 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
3787 vtop
->c
.i
= vtop
->c
.i
^ 1;
3790 vseti(VT_JMP
, gvtst(1, 0));
3802 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
3803 tcc_error("pointer not accepted for unary plus");
3804 /* In order to force cast, we add zero, except for floating point
3805 where we really need an noop (otherwise -0.0 will be transformed
3807 if (!is_float(vtop
->type
.t
)) {
3818 unary_type(&type
); // Perform a in_sizeof = 0;
3819 size
= type_size(&type
, &align
);
3820 if (t
== TOK_SIZEOF
) {
3821 if (!(type
.t
& VT_VLA
)) {
3823 tcc_error("sizeof applied to an incomplete type");
3824 if(type
.t
& VT_BITFIELD
)
3825 tcc_error("'%s' applied to a bit-field", get_tok_str(t
, NULL
));
3828 vla_runtime_type_size(&type
, &align
);
3833 vtop
->type
.t
|= VT_UNSIGNED
;
3836 case TOK_builtin_types_compatible_p
:
3845 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3846 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3847 vpushi(is_compatible_types(&type1
, &type2
));
3850 case TOK_builtin_constant_p
:
3852 int saved_nocode_wanted
, res
;
3855 saved_nocode_wanted
= nocode_wanted
;
3858 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
3860 nocode_wanted
= saved_nocode_wanted
;
3865 case TOK_builtin_frame_address
:
3871 if (tok
!= TOK_CINT
|| tokc
.i
< 0) {
3872 tcc_error("__builtin_frame_address only takes positive integers");
3879 vset(&type
, VT_LOCAL
, 0); /* local frame */
3881 mk_pointer(&vtop
->type
);
3882 indir(); /* -> parent frame */
3886 #ifdef TCC_TARGET_X86_64
3887 #ifdef TCC_TARGET_PE
3888 case TOK_builtin_va_start
:
3896 if ((vtop
->r
& VT_VALMASK
) != VT_LOCAL
)
3897 tcc_error("__builtin_va_start expects a local variable");
3898 vtop
->r
&= ~(VT_LVAL
| VT_REF
);
3899 vtop
->type
= char_pointer_type
;
3904 case TOK_builtin_va_arg_types
:
3911 vpushi(classify_x86_64_va_arg(&type
));
3926 t
= vtop
->type
.t
& VT_BTYPE
;
3928 /* In IEEE negate(x) isn't subtract(0,x), but rather
3933 else if (t
== VT_DOUBLE
)
3944 goto tok_identifier
;
3946 /* allow to take the address of a label */
3947 if (tok
< TOK_UIDENT
)
3948 expect("label identifier");
3949 s
= label_find(tok
);
3951 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
3953 if (s
->r
== LABEL_DECLARED
)
3954 s
->r
= LABEL_FORWARD
;
3957 s
->type
.t
= VT_VOID
;
3958 mk_pointer(&s
->type
);
3959 s
->type
.t
|= VT_STATIC
;
3961 vpushsym(&s
->type
, s
);
3965 // special qnan , snan and infinity values
3967 vpush64(VT_DOUBLE
, 0x7ff8000000000000ULL
);
3971 vpush64(VT_DOUBLE
, 0x7ff0000000000001ULL
);
3975 vpush64(VT_DOUBLE
, 0x7ff0000000000000ULL
);
3984 expect("identifier");
3987 const char *name
= get_tok_str(t
, NULL
);
3989 tcc_error("'%s' undeclared", name
);
3990 /* for simple function calls, we tolerate undeclared
3991 external reference to int() function */
3992 if (tcc_state
->warn_implicit_function_declaration
3993 #ifdef TCC_TARGET_PE
3994 /* people must be warned about using undeclared WINAPI functions
3995 (which usually start with uppercase letter) */
3996 || (name
[0] >= 'A' && name
[0] <= 'Z')
3999 tcc_warning("implicit declaration of function '%s'", name
);
4000 s
= external_global_sym(t
, &func_old_type
, 0);
4002 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
4003 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
4004 /* if referencing an inline function, then we generate a
4005 symbol to it if not already done. It will have the
4006 effect to generate code for it at the end of the
4007 compilation unit. Inline function as always
4008 generated in the text section. */
4010 put_extern_sym(s
, text_section
, 0, 0);
4011 r
= VT_SYM
| VT_CONST
;
4015 vset(&s
->type
, r
, s
->c
);
4016 /* if forward reference, we must point to s */
4017 if (vtop
->r
& VT_SYM
) {
4019 vtop
->c
.ptr_offset
= 0;
4024 /* post operations */
4026 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
4029 } else if (tok
== '.' || tok
== TOK_ARROW
) {
4032 if (tok
== TOK_ARROW
)
4034 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
4038 /* expect pointer on structure */
4039 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
4040 expect("struct or union");
4044 while ((s
= s
->next
) != NULL
) {
4049 tcc_error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, NULL
));
4050 /* add field offset to pointer */
4051 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
4054 /* change type to field type, and set to lvalue */
4055 vtop
->type
= s
->type
;
4056 vtop
->type
.t
|= qualifiers
;
4057 /* an array is never an lvalue */
4058 if (!(vtop
->type
.t
& VT_ARRAY
)) {
4059 vtop
->r
|= lvalue_type(vtop
->type
.t
);
4060 #ifdef CONFIG_TCC_BCHECK
4061 /* if bound checking, the referenced pointer must be checked */
4062 if (tcc_state
->do_bounds_check
)
4063 vtop
->r
|= VT_MUSTBOUND
;
4067 } else if (tok
== '[') {
4073 } else if (tok
== '(') {
4076 int nb_args
, ret_nregs
, ret_align
, variadic
;
4079 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
4080 /* pointer test (no array accepted) */
4081 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
4082 vtop
->type
= *pointed_type(&vtop
->type
);
4083 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
4087 expect("function pointer");
4090 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
4092 /* get return type */
4095 sa
= s
->next
; /* first parameter */
4098 /* compute first implicit argument if a structure is returned */
4099 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
4100 variadic
= (s
->c
== FUNC_ELLIPSIS
);
4101 ret_nregs
= gfunc_sret(&s
->type
, variadic
, &ret
.type
,
4104 /* get some space for the returned structure */
4105 size
= type_size(&s
->type
, &align
);
4106 loc
= (loc
- size
) & -align
;
4108 ret
.r
= VT_LOCAL
| VT_LVAL
;
4109 /* pass it as 'int' to avoid structure arg passing
4111 vseti(VT_LOCAL
, loc
);
4121 /* return in register */
4122 if (is_float(ret
.type
.t
)) {
4123 ret
.r
= reg_fret(ret
.type
.t
);
4124 #ifdef TCC_TARGET_X86_64
4125 if ((ret
.type
.t
& VT_BTYPE
) == VT_QFLOAT
)
4129 #ifdef TCC_TARGET_X86_64
4130 if ((ret
.type
.t
& VT_BTYPE
) == VT_QLONG
)
4132 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
4142 gfunc_param_typed(s
, sa
);
4152 tcc_error("too few arguments to function");
4154 if (!nocode_wanted
) {
4155 gfunc_call(nb_args
);
4157 vtop
-= (nb_args
+ 1);
4161 for (r
= ret
.r
+ ret_nregs
+ !ret_nregs
; r
-- > ret
.r
;) {
4162 vsetc(&ret
.type
, r
, &ret
.c
);
4163 vtop
->r2
= ret
.r2
; /* Loop only happens when r2 is VT_CONST */
4166 /* handle packed struct return */
4167 if (((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) && ret_nregs
) {
4170 size
= type_size(&s
->type
, &align
);
4171 loc
= (loc
- size
) & -align
;
4175 vset(&ret
.type
, VT_LOCAL
| VT_LVAL
, addr
+ offset
);
4179 if (--ret_nregs
== 0)
4181 /* XXX: compatible with arm only: ret_align == register_size */
4182 offset
+= ret_align
;
4184 vset(&s
->type
, VT_LOCAL
| VT_LVAL
, addr
);
4192 ST_FUNC
void expr_prod(void)
4197 while (tok
== '*' || tok
== '/' || tok
== '%') {
4205 ST_FUNC
void expr_sum(void)
4210 while (tok
== '+' || tok
== '-') {
4218 static void expr_shift(void)
4223 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
4231 static void expr_cmp(void)
4236 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
4237 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
4245 static void expr_cmpeq(void)
4250 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
4258 static void expr_and(void)
4261 while (tok
== '&') {
4268 static void expr_xor(void)
4271 while (tok
== '^') {
4278 static void expr_or(void)
4281 while (tok
== '|') {
4288 /* XXX: fix this mess */
4289 static void expr_land_const(void)
4292 while (tok
== TOK_LAND
) {
4299 /* XXX: fix this mess */
4300 static void expr_lor_const(void)
4303 while (tok
== TOK_LOR
) {
4310 /* only used if non constant */
4311 static void expr_land(void)
4316 if (tok
== TOK_LAND
) {
4321 if (tok
!= TOK_LAND
) {
4331 static void expr_lor(void)
4336 if (tok
== TOK_LOR
) {
4341 if (tok
!= TOK_LOR
) {
4351 /* XXX: better constant handling */
4352 static void expr_cond(void)
4354 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
;
4356 CType type
, type1
, type2
;
4363 boolean
.t
= VT_BOOL
;
4369 if (tok
!= ':' || !gnu_ext
) {
4384 if (vtop
!= vstack
) {
4385 /* needed to avoid having different registers saved in
4387 if (is_float(vtop
->type
.t
)) {
4389 #ifdef TCC_TARGET_X86_64
4390 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4400 if (tok
== ':' && gnu_ext
) {
4408 sv
= *vtop
; /* save value to handle it later */
4409 vtop
--; /* no vpop so that FP stack is not flushed */
4417 bt1
= t1
& VT_BTYPE
;
4419 bt2
= t2
& VT_BTYPE
;
4420 /* cast operands to correct type according to ISOC rules */
4421 if (is_float(bt1
) || is_float(bt2
)) {
4422 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
4423 type
.t
= VT_LDOUBLE
;
4424 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
4429 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
4430 /* cast to biggest op */
4432 /* convert to unsigned if it does not fit in a long long */
4433 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
4434 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
4435 type
.t
|= VT_UNSIGNED
;
4436 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
4437 /* If one is a null ptr constant the result type
4439 if (is_null_pointer (vtop
))
4441 else if (is_null_pointer (&sv
))
4443 /* XXX: test pointer compatibility, C99 has more elaborate
4447 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
4448 /* XXX: test function pointer compatibility */
4449 type
= bt1
== VT_FUNC
? type1
: type2
;
4450 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
4451 /* XXX: test structure compatibility */
4452 type
= bt1
== VT_STRUCT
? type1
: type2
;
4453 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
4454 /* NOTE: as an extension, we accept void on only one side */
4457 /* integer operations */
4459 /* convert to unsigned if it does not fit in an integer */
4460 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
4461 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
4462 type
.t
|= VT_UNSIGNED
;
4465 /* now we convert second operand */
4467 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4470 if (is_float(type
.t
)) {
4472 #ifdef TCC_TARGET_X86_64
4473 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4477 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
4478 /* for long longs, we use fixed registers to avoid having
4479 to handle a complicated move */
4484 /* this is horrible, but we must also convert first
4488 /* put again first value and cast it */
4491 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4494 move_reg(r2
, r1
, type
.t
);
4501 static void expr_eq(void)
4507 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
4508 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
4509 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
4524 ST_FUNC
void gexpr(void)
4535 /* parse an expression and return its type without any side effect. */
4536 static void expr_type(CType
*type
)
4538 int saved_nocode_wanted
;
4540 saved_nocode_wanted
= nocode_wanted
;
4545 nocode_wanted
= saved_nocode_wanted
;
4548 /* parse a unary expression and return its type without any side
4550 static void unary_type(CType
*type
)
4562 /* parse a constant expression and return value in vtop. */
4563 static void expr_const1(void)
4572 /* parse an integer constant and return its value. */
4573 ST_FUNC
int expr_const(void)
4577 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
4578 expect("constant expression");
4584 /* return the label token if current token is a label, otherwise
4586 static int is_label(void)
4590 /* fast test first */
4591 if (tok
< TOK_UIDENT
)
4593 /* no need to save tokc because tok is an identifier */
4600 unget_tok(last_tok
);
4605 static void label_or_decl(int l
)
4609 /* fast test first */
4610 if (tok
>= TOK_UIDENT
)
4612 /* no need to save tokc because tok is an identifier */
4616 unget_tok(last_tok
);
4619 unget_tok(last_tok
);
4624 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
4625 int case_reg
, int is_expr
)
4628 Sym
*s
, *frame_bottom
;
4630 /* generate line number info */
4631 if (tcc_state
->do_debug
&&
4632 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
4633 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
4635 last_line_num
= file
->line_num
;
4639 /* default return value is (void) */
4641 vtop
->type
.t
= VT_VOID
;
4644 if (tok
== TOK_IF
) {
4651 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4653 if (c
== TOK_ELSE
) {
4657 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4658 gsym(d
); /* patch else jmp */
4661 } else if (tok
== TOK_WHILE
) {
4669 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4673 } else if (tok
== '{') {
4675 int block_vla_sp_loc
, *saved_vla_sp_loc
, saved_vla_flags
;
4678 /* record local declaration stack position */
4680 frame_bottom
= sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4681 frame_bottom
->next
= scope_stack_bottom
;
4682 scope_stack_bottom
= frame_bottom
;
4683 llabel
= local_label_stack
;
4685 /* save VLA state */
4686 block_vla_sp_loc
= *(saved_vla_sp_loc
= vla_sp_loc
);
4687 if (saved_vla_sp_loc
!= &vla_sp_root_loc
)
4688 vla_sp_loc
= &block_vla_sp_loc
;
4690 saved_vla_flags
= vla_flags
;
4691 vla_flags
|= VLA_NEED_NEW_FRAME
;
4693 /* handle local labels declarations */
4694 if (tok
== TOK_LABEL
) {
4697 if (tok
< TOK_UIDENT
)
4698 expect("label identifier");
4699 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
4709 while (tok
!= '}') {
4710 label_or_decl(VT_LOCAL
);
4714 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4717 /* pop locally defined labels */
4718 label_pop(&local_label_stack
, llabel
);
4720 /* XXX: this solution makes only valgrind happy...
4721 triggered by gcc.c-torture/execute/20000917-1.c */
4723 switch(vtop
->type
.t
& VT_BTYPE
) {
4728 for(p
=vtop
->type
.ref
;p
;p
=p
->prev
)
4730 tcc_error("unsupported expression type");
4733 /* pop locally defined symbols */
4734 scope_stack_bottom
= scope_stack_bottom
->next
;
4735 sym_pop(&local_stack
, s
);
4737 /* Pop VLA frames and restore stack pointer if required */
4738 if (saved_vla_sp_loc
!= &vla_sp_root_loc
)
4739 *saved_vla_sp_loc
= block_vla_sp_loc
;
4740 if (vla_sp_loc
!= (saved_vla_sp_loc
== &vla_sp_root_loc
? &vla_sp_root_loc
: &block_vla_sp_loc
)) {
4741 vla_sp_loc
= saved_vla_sp_loc
;
4742 gen_vla_sp_restore(*vla_sp_loc
);
4744 vla_flags
= (vla_flags
& ~VLA_SCOPE_FLAGS
) | (saved_vla_flags
& VLA_SCOPE_FLAGS
);
4747 } else if (tok
== TOK_RETURN
) {
4751 gen_assign_cast(&func_vt
);
4752 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
4753 CType type
, ret_type
;
4754 int ret_align
, ret_nregs
;
4755 ret_nregs
= gfunc_sret(&func_vt
, func_var
, &ret_type
,
4757 if (0 == ret_nregs
) {
4758 /* if returning structure, must copy it to implicit
4759 first pointer arg location */
4762 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
4765 /* copy structure value to pointer */
4768 /* returning structure packed into registers */
4769 int r
, size
, addr
, align
;
4770 size
= type_size(&func_vt
,&align
);
4771 if ((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) || (vtop
->c
.i
& (ret_align
-1)))
4772 && (align
& (ret_align
-1))) {
4773 loc
= (loc
- size
) & -align
;
4776 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
4779 vset(&ret_type
, VT_LOCAL
| VT_LVAL
, addr
);
4781 vtop
->type
= ret_type
;
4782 if (is_float(ret_type
.t
))
4783 r
= rc_fret(ret_type
.t
);
4789 if (--ret_nregs
== 0)
4791 /* We assume that when a structure is returned in multiple
4792 registers, their classes are consecutive values of the
4795 /* XXX: compatible with arm only: ret_align == register_size */
4796 vtop
->c
.i
+= ret_align
;
4797 vtop
->r
= VT_LOCAL
| VT_LVAL
;
4800 } else if (is_float(func_vt
.t
)) {
4801 gv(rc_fret(func_vt
.t
));
4805 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
4808 rsym
= gjmp(rsym
); /* jmp */
4809 } else if (tok
== TOK_BREAK
) {
4812 tcc_error("cannot break");
4813 *bsym
= gjmp(*bsym
);
4816 } else if (tok
== TOK_CONTINUE
) {
4819 tcc_error("cannot continue");
4820 *csym
= gjmp(*csym
);
4823 } else if (tok
== TOK_FOR
) {
4828 frame_bottom
= sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4829 frame_bottom
->next
= scope_stack_bottom
;
4830 scope_stack_bottom
= frame_bottom
;
4832 /* c99 for-loop init decl? */
4833 if (!decl0(VT_LOCAL
, 1)) {
4834 /* no, regular for-loop init expr */
4858 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4862 scope_stack_bottom
= scope_stack_bottom
->next
;
4863 sym_pop(&local_stack
, s
);
4865 if (tok
== TOK_DO
) {
4870 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4881 if (tok
== TOK_SWITCH
) {
4885 /* XXX: other types than integer */
4886 case_reg
= gv(RC_INT
);
4890 b
= gjmp(0); /* jump to first case */
4892 block(&a
, csym
, &b
, &c
, case_reg
, 0);
4893 /* if no default, jmp after switch */
4901 if (tok
== TOK_CASE
) {
4908 if (gnu_ext
&& tok
== TOK_DOTS
) {
4912 tcc_warning("empty case range");
4914 /* since a case is like a label, we must skip it with a jmp */
4921 *case_sym
= gtst(1, 0);
4924 *case_sym
= gtst(1, 0);
4928 *case_sym
= gtst(1, *case_sym
);
4933 goto block_after_label
;
4935 if (tok
== TOK_DEFAULT
) {
4941 tcc_error("too many 'default'");
4944 goto block_after_label
;
4946 if (tok
== TOK_GOTO
) {
4948 if (tok
== '*' && gnu_ext
) {
4952 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
4955 } else if (tok
>= TOK_UIDENT
) {
4956 s
= label_find(tok
);
4957 /* put forward definition if needed */
4959 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4961 if (s
->r
== LABEL_DECLARED
)
4962 s
->r
= LABEL_FORWARD
;
4964 /* label already defined */
4965 if (vla_flags
& VLA_IN_SCOPE
) {
4966 /* If VLAs are in use, save the current stack pointer and
4967 reset the stack pointer to what it was at function entry
4968 (label will restore stack pointer in inner scopes) */
4970 gen_vla_sp_restore(vla_sp_root_loc
);
4972 if (s
->r
& LABEL_FORWARD
)
4973 s
->jnext
= gjmp(s
->jnext
);
4975 gjmp_addr(s
->jnext
);
4978 expect("label identifier");
4981 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
4987 if (vla_flags
& VLA_IN_SCOPE
) {
4988 /* save/restore stack pointer across label
4989 this is a no-op when combined with the load immediately
4990 after the label unless we arrive via goto */
4995 if (s
->r
== LABEL_DEFINED
)
4996 tcc_error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
4998 s
->r
= LABEL_DEFINED
;
5000 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
5003 if (vla_flags
& VLA_IN_SCOPE
) {
5004 gen_vla_sp_restore(*vla_sp_loc
);
5005 vla_flags
|= VLA_NEED_NEW_FRAME
;
5007 /* we accept this, but it is a mistake */
5010 tcc_warning("deprecated use of label at end of compound statement");
5014 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
5017 /* expression case */
5032 /* t is the array or struct type. c is the array or struct
5033 address. cur_index/cur_field is the pointer to the current
5034 value. 'size_only' is true if only size info is needed (only used
5036 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
5037 int *cur_index
, Sym
**cur_field
,
5041 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
5047 if (gnu_ext
&& (l
= is_label()) != 0)
5049 while (tok
== '[' || tok
== '.') {
5051 if (!(type
->t
& VT_ARRAY
))
5052 expect("array type");
5055 index
= expr_const();
5056 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
5057 expect("invalid index");
5058 if (tok
== TOK_DOTS
&& gnu_ext
) {
5060 index_last
= expr_const();
5061 if (index_last
< 0 ||
5062 (s
->c
>= 0 && index_last
>= s
->c
) ||
5064 expect("invalid index");
5070 *cur_index
= index_last
;
5071 type
= pointed_type(type
);
5072 elem_size
= type_size(type
, &align
);
5073 c
+= index
* elem_size
;
5074 /* NOTE: we only support ranges for last designator */
5075 nb_elems
= index_last
- index
+ 1;
5076 if (nb_elems
!= 1) {
5085 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
5086 expect("struct/union type");
5099 /* XXX: fix this mess by using explicit storage field */
5101 type1
.t
|= (type
->t
& ~VT_TYPE
);
5115 if (type
->t
& VT_ARRAY
) {
5117 type
= pointed_type(type
);
5118 c
+= index
* type_size(type
, &align
);
5122 tcc_error("too many field init");
5123 /* XXX: fix this mess by using explicit storage field */
5125 type1
.t
|= (type
->t
& ~VT_TYPE
);
5130 decl_initializer(type
, sec
, c
, 0, size_only
);
5132 /* XXX: make it more general */
5133 if (!size_only
&& nb_elems
> 1) {
5134 unsigned long c_end
;
5139 tcc_error("range init not supported yet for dynamic storage");
5140 c_end
= c
+ nb_elems
* elem_size
;
5141 if (c_end
> sec
->data_allocated
)
5142 section_realloc(sec
, c_end
);
5143 src
= sec
->data
+ c
;
5145 for(i
= 1; i
< nb_elems
; i
++) {
5147 memcpy(dst
, src
, elem_size
);
5153 #define EXPR_CONST 1
5156 /* store a value or an expression directly in global data or in local array */
5157 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
5158 int v
, int expr_type
)
5160 int saved_global_expr
, bt
, bit_pos
, bit_size
;
5162 unsigned long long bit_mask
;
5170 /* compound literals must be allocated globally in this case */
5171 saved_global_expr
= global_expr
;
5174 global_expr
= saved_global_expr
;
5175 /* NOTE: symbols are accepted */
5176 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
5177 tcc_error("initializer element is not constant");
5185 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
5188 /* XXX: not portable */
5189 /* XXX: generate error if incorrect relocation */
5190 gen_assign_cast(&dtype
);
5191 bt
= type
->t
& VT_BTYPE
;
5192 /* we'll write at most 12 bytes */
5193 if (c
+ 12 > sec
->data_allocated
) {
5194 section_realloc(sec
, c
+ 12);
5196 ptr
= sec
->data
+ c
;
5197 /* XXX: make code faster ? */
5198 if (!(type
->t
& VT_BITFIELD
)) {
5203 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5204 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
5205 bit_mask
= (1LL << bit_size
) - 1;
5207 if ((vtop
->r
& VT_SYM
) &&
5213 (bt
== VT_INT
&& bit_size
!= 32)))
5214 tcc_error("initializer element is not computable at load time");
5217 vtop
->c
.i
= (vtop
->c
.i
!= 0);
5219 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5222 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5225 *(double *)ptr
= vtop
->c
.d
;
5228 *(long double *)ptr
= vtop
->c
.ld
;
5231 *(long long *)ptr
|= (vtop
->c
.ll
& bit_mask
) << bit_pos
;
5234 if (vtop
->r
& VT_SYM
) {
5235 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
5237 *(addr_t
*)ptr
|= (vtop
->c
.ptr_offset
& bit_mask
) << bit_pos
;
5240 if (vtop
->r
& VT_SYM
) {
5241 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
5243 *(int *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5248 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
5255 /* put zeros for variable based init */
5256 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
5259 /* nothing to do because globals are already set to zero */
5261 vpush_global_sym(&func_old_type
, TOK_memset
);
5263 #ifdef TCC_TARGET_ARM
5274 /* 't' contains the type and storage info. 'c' is the offset of the
5275 object in section 'sec'. If 'sec' is NULL, it means stack based
5276 allocation. 'first' is true if array '{' must be read (multi
5277 dimension implicit array init handling). 'size_only' is true if
5278 size only evaluation is wanted (only for arrays). */
5279 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
5280 int first
, int size_only
)
5282 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, parlevel1
, i
;
5283 int size1
, align1
, expr_type
;
5287 if (type
->t
& VT_VLA
) {
5290 /* save current stack pointer */
5291 if (vla_flags
& VLA_NEED_NEW_FRAME
) {
5293 vla_flags
= VLA_IN_SCOPE
;
5294 vla_sp_loc
= &vla_sp_loc_tmp
;
5297 vla_runtime_type_size(type
, &a
);
5298 gen_vla_alloc(type
, a
);
5299 vset(type
, VT_LOCAL
|VT_LVAL
, c
);
5303 } else if (type
->t
& VT_ARRAY
) {
5307 t1
= pointed_type(type
);
5308 size1
= type_size(t1
, &align1
);
5311 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
5314 tcc_error("character array initializer must be a literal,"
5315 " optionally enclosed in braces");
5320 /* only parse strings here if correct type (otherwise: handle
5321 them as ((w)char *) expressions */
5322 if ((tok
== TOK_LSTR
&&
5323 #ifdef TCC_TARGET_PE
5324 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
5326 (t1
->t
& VT_BTYPE
) == VT_INT
5328 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
5329 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5334 /* compute maximum number of chars wanted */
5336 cstr_len
= cstr
->size
;
5338 cstr_len
= cstr
->size
/ sizeof(nwchar_t
);
5341 if (n
>= 0 && nb
> (n
- array_length
))
5342 nb
= n
- array_length
;
5345 tcc_warning("initializer-string for array is too long");
5346 /* in order to go faster for common case (char
5347 string in global variable, we handle it
5349 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
5350 memcpy(sec
->data
+ c
+ array_length
, cstr
->data
, nb
);
5354 ch
= ((unsigned char *)cstr
->data
)[i
];
5356 ch
= ((nwchar_t
*)cstr
->data
)[i
];
5357 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
5365 /* only add trailing zero if enough storage (no
5366 warning in this case since it is standard) */
5367 if (n
< 0 || array_length
< n
) {
5369 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
5375 while (tok
!= '}') {
5376 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
5377 if (n
>= 0 && index
>= n
)
5378 tcc_error("index too large");
5379 /* must put zero in holes (note that doing it that way
5380 ensures that it even works with designators) */
5381 if (!size_only
&& array_length
< index
) {
5382 init_putz(t1
, sec
, c
+ array_length
* size1
,
5383 (index
- array_length
) * size1
);
5386 if (index
> array_length
)
5387 array_length
= index
;
5388 /* special test for multi dimensional arrays (may not
5389 be strictly correct if designators are used at the
5391 if (index
>= n
&& no_oblock
)
5400 /* put zeros at the end */
5401 if (!size_only
&& n
>= 0 && array_length
< n
) {
5402 init_putz(t1
, sec
, c
+ array_length
* size1
,
5403 (n
- array_length
) * size1
);
5405 /* patch type size if needed */
5407 s
->c
= array_length
;
5408 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
5409 (sec
|| !first
|| tok
== '{')) {
5412 /* NOTE: the previous test is a specific case for automatic
5413 struct/union init */
5414 /* XXX: union needs only one init */
5416 /* XXX: this test is incorrect for local initializers
5417 beginning with ( without {. It would be much more difficult
5418 to do it correctly (ideally, the expression parser should
5419 be used in all cases) */
5425 while (tok
== '(') {
5429 if (!parse_btype(&type1
, &ad1
))
5431 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
5433 if (!is_assignable_types(type
, &type1
))
5434 tcc_error("invalid type for cast");
5439 if (first
|| tok
== '{') {
5448 while (tok
!= '}') {
5449 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
5451 if (!size_only
&& array_length
< index
) {
5452 init_putz(type
, sec
, c
+ array_length
,
5453 index
- array_length
);
5455 index
= index
+ type_size(&f
->type
, &align1
);
5456 if (index
> array_length
)
5457 array_length
= index
;
5459 /* gr: skip fields from same union - ugly. */
5461 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
5462 /* test for same offset */
5463 if (f
->next
->c
!= f
->c
)
5465 /* if yes, test for bitfield shift */
5466 if ((f
->type
.t
& VT_BITFIELD
) && (f
->next
->type
.t
& VT_BITFIELD
)) {
5467 int bit_pos_1
= (f
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5468 int bit_pos_2
= (f
->next
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5469 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
5470 if (bit_pos_1
!= bit_pos_2
)
5477 if (no_oblock
&& f
== NULL
)
5483 /* put zeros at the end */
5484 if (!size_only
&& array_length
< n
) {
5485 init_putz(type
, sec
, c
+ array_length
,
5494 } else if (tok
== '{') {
5496 decl_initializer(type
, sec
, c
, first
, size_only
);
5498 } else if (size_only
) {
5499 /* just skip expression */
5500 parlevel
= parlevel1
= 0;
5501 while ((parlevel
> 0 || parlevel1
> 0 ||
5502 (tok
!= '}' && tok
!= ',')) && tok
!= -1) {
5505 else if (tok
== ')')
5507 else if (tok
== '{')
5509 else if (tok
== '}')
5514 /* currently, we always use constant expression for globals
5515 (may change for scripting case) */
5516 expr_type
= EXPR_CONST
;
5518 expr_type
= EXPR_ANY
;
5519 init_putv(type
, sec
, c
, 0, expr_type
);
5523 /* parse an initializer for type 't' if 'has_init' is non zero, and
5524 allocate space in local or global data space ('r' is either
5525 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5526 variable 'v' with an associated name represented by 'asm_label' of
5527 scope 'scope' is declared before initializers are parsed. If 'v' is
5528 zero, then a reference to the new object is put in the value stack.
5529 If 'has_init' is 2, a special parsing is done to handle string
5531 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
5532 int has_init
, int v
, char *asm_label
,
5535 int size
, align
, addr
, data_offset
;
5537 ParseState saved_parse_state
= {0};
5538 TokenString init_str
;
5540 Sym
*flexible_array
;
5542 flexible_array
= NULL
;
5543 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
5544 Sym
*field
= type
->ref
->next
;
5547 field
= field
->next
;
5548 if (field
->type
.t
& VT_ARRAY
&& field
->type
.ref
->c
< 0)
5549 flexible_array
= field
;
5553 size
= type_size(type
, &align
);
5554 /* If unknown size, we must evaluate it before
5555 evaluating initializers because
5556 initializers can generate global data too
5557 (e.g. string pointers or ISOC99 compound
5558 literals). It also simplifies local
5559 initializers handling */
5560 tok_str_new(&init_str
);
5561 if (size
< 0 || (flexible_array
&& has_init
)) {
5563 tcc_error("unknown type size");
5564 /* get all init string */
5565 if (has_init
== 2) {
5566 /* only get strings */
5567 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5568 tok_str_add_tok(&init_str
);
5573 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
5575 tcc_error("unexpected end of file in initializer");
5576 tok_str_add_tok(&init_str
);
5579 else if (tok
== '}') {
5589 tok_str_add(&init_str
, -1);
5590 tok_str_add(&init_str
, 0);
5593 save_parse_state(&saved_parse_state
);
5595 macro_ptr
= init_str
.str
;
5597 decl_initializer(type
, NULL
, 0, 1, 1);
5598 /* prepare second initializer parsing */
5599 macro_ptr
= init_str
.str
;
5602 /* if still unknown size, error */
5603 size
= type_size(type
, &align
);
5605 tcc_error("unknown type size");
5608 size
+= flexible_array
->type
.ref
->c
* pointed_size(&flexible_array
->type
);
5609 /* take into account specified alignment if bigger */
5610 if (ad
->a
.aligned
) {
5611 if (ad
->a
.aligned
> align
)
5612 align
= ad
->a
.aligned
;
5613 } else if (ad
->a
.packed
) {
5616 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
5618 #ifdef CONFIG_TCC_BCHECK
5619 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5623 loc
= (loc
- size
) & -align
;
5625 #ifdef CONFIG_TCC_BCHECK
5626 /* handles bounds */
5627 /* XXX: currently, since we do only one pass, we cannot track
5628 '&' operators, so we add only arrays */
5629 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5630 unsigned long *bounds_ptr
;
5631 /* add padding between regions */
5633 /* then add local bound info */
5634 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(unsigned long));
5635 bounds_ptr
[0] = addr
;
5636 bounds_ptr
[1] = size
;
5640 /* local variable */
5641 sym_push(v
, type
, r
, addr
);
5643 /* push local reference */
5644 vset(type
, r
, addr
);
5650 if (v
&& scope
== VT_CONST
) {
5651 /* see if the symbol was already defined */
5654 if (!is_compatible_types(&sym
->type
, type
))
5655 tcc_error("incompatible types for redefinition of '%s'",
5656 get_tok_str(v
, NULL
));
5657 if (sym
->type
.t
& VT_EXTERN
) {
5658 /* if the variable is extern, it was not allocated */
5659 sym
->type
.t
&= ~VT_EXTERN
;
5660 /* set array size if it was omitted in extern
5662 if ((sym
->type
.t
& VT_ARRAY
) &&
5663 sym
->type
.ref
->c
< 0 &&
5665 sym
->type
.ref
->c
= type
->ref
->c
;
5667 /* we accept several definitions of the same
5668 global variable. this is tricky, because we
5669 must play with the SHN_COMMON type of the symbol */
5670 /* XXX: should check if the variable was already
5671 initialized. It is incorrect to initialized it
5673 /* no init data, we won't add more to the symbol */
5680 /* allocate symbol in corresponding section */
5685 else if (tcc_state
->nocommon
)
5689 data_offset
= sec
->data_offset
;
5690 data_offset
= (data_offset
+ align
- 1) & -align
;
5692 /* very important to increment global pointer at this time
5693 because initializers themselves can create new initializers */
5694 data_offset
+= size
;
5695 #ifdef CONFIG_TCC_BCHECK
5696 /* add padding if bound check */
5697 if (tcc_state
->do_bounds_check
)
5700 sec
->data_offset
= data_offset
;
5701 /* allocate section space to put the data */
5702 if (sec
->sh_type
!= SHT_NOBITS
&&
5703 data_offset
> sec
->data_allocated
)
5704 section_realloc(sec
, data_offset
);
5705 /* align section if needed */
5706 if (align
> sec
->sh_addralign
)
5707 sec
->sh_addralign
= align
;
5709 addr
= 0; /* avoid warning */
5713 if (scope
!= VT_CONST
|| !sym
) {
5714 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
5715 sym
->asm_label
= asm_label
;
5717 /* update symbol definition */
5719 put_extern_sym(sym
, sec
, addr
, size
);
5722 /* put a common area */
5723 put_extern_sym(sym
, NULL
, align
, size
);
5724 /* XXX: find a nicer way */
5725 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
5726 esym
->st_shndx
= SHN_COMMON
;
5729 /* push global reference */
5730 sym
= get_sym_ref(type
, sec
, addr
, size
);
5731 vpushsym(type
, sym
);
5733 /* patch symbol weakness */
5734 if (type
->t
& VT_WEAK
)
5736 apply_visibility(sym
, type
);
5737 #ifdef CONFIG_TCC_BCHECK
5738 /* handles bounds now because the symbol must be defined
5739 before for the relocation */
5740 if (tcc_state
->do_bounds_check
) {
5741 unsigned long *bounds_ptr
;
5743 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
5744 /* then add global bound info */
5745 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(long));
5746 bounds_ptr
[0] = 0; /* relocated */
5747 bounds_ptr
[1] = size
;
5751 if (has_init
|| (type
->t
& VT_VLA
)) {
5752 decl_initializer(type
, sec
, addr
, 1, 0);
5753 /* restore parse state if needed */
5755 tok_str_free(init_str
.str
);
5756 restore_parse_state(&saved_parse_state
);
5758 /* patch flexible array member size back to -1, */
5759 /* for possible subsequent similar declarations */
5761 flexible_array
->type
.ref
->c
= -1;
5766 static void put_func_debug(Sym
*sym
)
5771 /* XXX: we put here a dummy type */
5772 snprintf(buf
, sizeof(buf
), "%s:%c1",
5773 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
5774 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
5775 cur_text_section
, sym
->c
);
5776 /* //gr gdb wants a line at the function */
5777 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
5782 /* parse an old style function declaration list */
5783 /* XXX: check multiple parameter */
5784 static void func_decl_list(Sym
*func_sym
)
5791 /* parse each declaration */
5792 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
&&
5793 tok
!= TOK_ASM1
&& tok
!= TOK_ASM2
&& tok
!= TOK_ASM3
) {
5794 if (!parse_btype(&btype
, &ad
))
5795 expect("declaration list");
5796 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5797 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5799 /* we accept no variable after */
5803 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5804 /* find parameter in function parameter list */
5807 if ((s
->v
& ~SYM_FIELD
) == v
)
5811 tcc_error("declaration for parameter '%s' but no such parameter",
5812 get_tok_str(v
, NULL
));
5814 /* check that no storage specifier except 'register' was given */
5815 if (type
.t
& VT_STORAGE
)
5816 tcc_error("storage class specified for '%s'", get_tok_str(v
, NULL
));
5817 convert_parameter_type(&type
);
5818 /* we can add the type (NOTE: it could be local to the function) */
5820 /* accept other parameters */
5831 /* parse a function defined by symbol 'sym' and generate its code in
5832 'cur_text_section' */
5833 static void gen_function(Sym
*sym
)
5835 int saved_nocode_wanted
= nocode_wanted
;
5837 ind
= cur_text_section
->data_offset
;
5838 /* NOTE: we patch the symbol size later */
5839 put_extern_sym(sym
, cur_text_section
, ind
, 0);
5840 funcname
= get_tok_str(sym
->v
, NULL
);
5842 /* Initialize VLA state */
5843 vla_sp_loc
= &vla_sp_root_loc
;
5844 vla_flags
= VLA_NEED_NEW_FRAME
;
5845 /* put debug symbol */
5846 if (tcc_state
->do_debug
)
5847 put_func_debug(sym
);
5848 /* push a dummy symbol to enable local sym storage */
5849 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
5850 gfunc_prolog(&sym
->type
);
5851 #ifdef CONFIG_TCC_BCHECK
5852 if (tcc_state
->do_bounds_check
5853 && !strcmp(get_tok_str(sym
->v
, NULL
), "main")) {
5857 for (i
= 0, sym
= local_stack
; i
< 2; i
++, sym
= sym
->prev
) {
5858 if (sym
->v
& SYM_FIELD
|| sym
->prev
->v
& SYM_FIELD
)
5860 vpush_global_sym(&func_old_type
, TOK___bound_main_arg
);
5861 vset(&sym
->type
, sym
->r
, sym
->c
);
5867 block(NULL
, NULL
, NULL
, NULL
, 0, 0);
5870 cur_text_section
->data_offset
= ind
;
5871 label_pop(&global_label_stack
, NULL
);
5872 /* reset local stack */
5873 scope_stack_bottom
= NULL
;
5874 sym_pop(&local_stack
, NULL
);
5875 /* end of function */
5876 /* patch symbol size */
5877 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
5879 /* patch symbol weakness (this definition overrules any prototype) */
5880 if (sym
->type
.t
& VT_WEAK
)
5882 apply_visibility(sym
, &sym
->type
);
5883 if (tcc_state
->do_debug
) {
5884 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
5886 /* It's better to crash than to generate wrong code */
5887 cur_text_section
= NULL
;
5888 funcname
= ""; /* for safety */
5889 func_vt
.t
= VT_VOID
; /* for safety */
5890 func_var
= 0; /* for safety */
5891 ind
= 0; /* for safety */
5892 nocode_wanted
= saved_nocode_wanted
;
5895 ST_FUNC
void gen_inline_functions(void)
5898 int *str
, inline_generated
, i
;
5899 struct InlineFunc
*fn
;
5901 /* iterate while inline function are referenced */
5903 inline_generated
= 0;
5904 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5905 fn
= tcc_state
->inline_fns
[i
];
5907 if (sym
&& sym
->c
) {
5908 /* the function was used: generate its code and
5909 convert it to a normal function */
5910 str
= fn
->token_str
;
5913 pstrcpy(file
->filename
, sizeof file
->filename
, fn
->filename
);
5914 sym
->r
= VT_SYM
| VT_CONST
;
5915 sym
->type
.t
&= ~VT_INLINE
;
5919 cur_text_section
= text_section
;
5921 macro_ptr
= NULL
; /* fail safe */
5923 inline_generated
= 1;
5926 if (!inline_generated
)
5929 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5930 fn
= tcc_state
->inline_fns
[i
];
5931 str
= fn
->token_str
;
5934 dynarray_reset(&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
);
5937 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
5938 static int decl0(int l
, int is_for_loop_init
)
5946 if (!parse_btype(&btype
, &ad
)) {
5947 if (is_for_loop_init
)
5949 /* skip redundant ';' */
5950 /* XXX: find more elegant solution */
5955 if (l
== VT_CONST
&&
5956 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5957 /* global asm block */
5961 /* special test for old K&R protos without explicit int
5962 type. Only accepted when defining global data */
5963 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
5967 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5968 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5970 /* we accept no variable after */
5974 while (1) { /* iterate thru each declaration */
5975 char *asm_label
; // associated asm label
5977 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5981 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
5982 printf("type = '%s'\n", buf
);
5985 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5986 if ((type
.t
& VT_STATIC
) && (l
== VT_LOCAL
)) {
5987 tcc_error("function without file scope cannot be static");
5989 /* if old style function prototype, we accept a
5992 if (sym
->c
== FUNC_OLD
)
5993 func_decl_list(sym
);
5997 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
6000 asm_label_instr(&astr
);
6001 asm_label
= tcc_strdup(astr
.data
);
6004 /* parse one last attribute list, after asm label */
6005 parse_attribute(&ad
);
6010 #ifdef TCC_TARGET_PE
6011 if (ad
.a
.func_import
)
6012 type
.t
|= VT_IMPORT
;
6013 if (ad
.a
.func_export
)
6014 type
.t
|= VT_EXPORT
;
6016 type
.t
|= ad
.a
.visibility
<< VT_VIS_SHIFT
;
6020 tcc_error("cannot use local functions");
6021 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
6022 expect("function definition");
6024 /* reject abstract declarators in function definition */
6026 while ((sym
= sym
->next
) != NULL
)
6027 if (!(sym
->v
& ~SYM_FIELD
))
6028 expect("identifier");
6030 /* XXX: cannot do better now: convert extern line to static inline */
6031 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
6032 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
6037 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
6040 ref
= sym
->type
.ref
;
6041 if (0 == ref
->a
.func_proto
)
6042 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
6044 /* use func_call from prototype if not defined */
6045 if (ref
->a
.func_call
!= FUNC_CDECL
6046 && type
.ref
->a
.func_call
== FUNC_CDECL
)
6047 type
.ref
->a
.func_call
= ref
->a
.func_call
;
6049 /* use export from prototype */
6050 if (ref
->a
.func_export
)
6051 type
.ref
->a
.func_export
= 1;
6053 /* use static from prototype */
6054 if (sym
->type
.t
& VT_STATIC
)
6055 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
6057 /* If the definition has no visibility use the
6058 one from prototype. */
6059 if (! (type
.t
& VT_VIS_MASK
))
6060 type
.t
|= sym
->type
.t
& VT_VIS_MASK
;
6062 if (!is_compatible_types(&sym
->type
, &type
)) {
6064 tcc_error("incompatible types for redefinition of '%s'",
6065 get_tok_str(v
, NULL
));
6067 type
.ref
->a
.func_proto
= 0;
6068 /* if symbol is already defined, then put complete type */
6071 /* put function symbol */
6072 sym
= global_identifier_push(v
, type
.t
, 0);
6073 sym
->type
.ref
= type
.ref
;
6076 /* static inline functions are just recorded as a kind
6077 of macro. Their code will be emitted at the end of
6078 the compilation unit only if they are used */
6079 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
6080 (VT_INLINE
| VT_STATIC
)) {
6081 TokenString func_str
;
6083 struct InlineFunc
*fn
;
6084 const char *filename
;
6086 tok_str_new(&func_str
);
6092 tcc_error("unexpected end of file");
6093 tok_str_add_tok(&func_str
);
6098 } else if (t
== '}') {
6100 if (block_level
== 0)
6104 tok_str_add(&func_str
, -1);
6105 tok_str_add(&func_str
, 0);
6106 filename
= file
? file
->filename
: "";
6107 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
6108 strcpy(fn
->filename
, filename
);
6110 fn
->token_str
= func_str
.str
;
6111 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
6114 /* compute text section */
6115 cur_text_section
= ad
.section
;
6116 if (!cur_text_section
)
6117 cur_text_section
= text_section
;
6118 sym
->r
= VT_SYM
| VT_CONST
;
6123 if (btype
.t
& VT_TYPEDEF
) {
6124 /* save typedefed type */
6125 /* XXX: test storage specifiers ? */
6126 sym
= sym_push(v
, &type
, 0, 0);
6128 sym
->type
.t
|= VT_TYPEDEF
;
6131 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
6132 /* external function definition */
6133 /* specific case for func_call attribute */
6134 ad
.a
.func_proto
= 1;
6136 } else if (!(type
.t
& VT_ARRAY
)) {
6137 /* not lvalue if array */
6138 r
|= lvalue_type(type
.t
);
6140 has_init
= (tok
== '=');
6141 if (has_init
&& (type
.t
& VT_VLA
))
6142 tcc_error("Variable length array cannot be initialized");
6143 if ((btype
.t
& VT_EXTERN
) || ((type
.t
& VT_BTYPE
) == VT_FUNC
) ||
6144 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
6145 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
6146 /* external variable or function */
6147 /* NOTE: as GCC, uninitialized global static
6148 arrays of null size are considered as
6150 sym
= external_sym(v
, &type
, r
, asm_label
);
6152 if (ad
.alias_target
) {
6157 alias_target
= sym_find(ad
.alias_target
);
6158 if (!alias_target
|| !alias_target
->c
)
6159 tcc_error("unsupported forward __alias__ attribute");
6160 esym
= &((Elf32_Sym
*)symtab_section
->data
)[alias_target
->c
];
6161 tsec
.sh_num
= esym
->st_shndx
;
6162 put_extern_sym2(sym
, &tsec
, esym
->st_value
, esym
->st_size
, 0);
6165 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
6166 if (type
.t
& VT_STATIC
)
6172 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, asm_label
, l
);
6176 if (is_for_loop_init
)
6189 ST_FUNC
void decl(int l
)