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 #ifndef TCC_TARGET_X86_64
1023 /* build a long long from two ints */
1024 static void lbuild(int t
)
1026 gv2(RC_INT
, RC_INT
);
1027 vtop
[-1].r2
= vtop
[0].r
;
1028 vtop
[-1].type
.t
= t
;
1033 /* rotate n first stack elements to the bottom
1034 I1 ... In -> I2 ... In I1 [top is right]
1036 ST_FUNC
void vrotb(int n
)
1042 for(i
=-n
+1;i
!=0;i
++)
1043 vtop
[i
] = vtop
[i
+1];
1047 /* rotate the n elements before entry e towards the top
1048 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
1050 ST_FUNC
void vrote(SValue
*e
, int n
)
1056 for(i
= 0;i
< n
- 1; i
++)
1061 /* rotate n first stack elements to the top
1062 I1 ... In -> In I1 ... I(n-1) [top is right]
1064 ST_FUNC
void vrott(int n
)
1069 /* pop stack value */
1070 ST_FUNC
void vpop(void)
1073 v
= vtop
->r
& VT_VALMASK
;
1074 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1075 /* for x86, we need to pop the FP stack */
1076 if (v
== TREG_ST0
&& !nocode_wanted
) {
1077 o(0xd8dd); /* fstp %st(0) */
1080 if (v
== VT_JMP
|| v
== VT_JMPI
) {
1081 /* need to put correct jump if && or || without test */
1087 /* convert stack entry to register and duplicate its value in another
1089 static void gv_dup(void)
1094 #ifndef TCC_TARGET_X86_64
1095 if ((t
& VT_BTYPE
) == VT_LLONG
) {
1102 /* stack: H L L1 H1 */
1111 /* duplicate value */
1114 #ifdef TCC_TARGET_X86_64
1115 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1126 load(r1
, &sv
); /* move r to r1 */
1128 /* duplicates value */
1134 /* Generate value test
1136 * Generate a test for any value (jump, comparison and integers) */
1137 ST_FUNC
int gvtst(int inv
, int t
)
1139 int v
= vtop
->r
& VT_VALMASK
;
1140 if (v
!= VT_CMP
&& v
!= VT_JMP
&& v
!= VT_JMPI
) {
1144 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1145 /* constant jmp optimization */
1146 if ((vtop
->c
.i
!= 0) != inv
)
1151 return gtst(inv
, t
);
1154 #ifndef TCC_TARGET_X86_64
1155 /* generate CPU independent (unsigned) long long operations */
1156 static void gen_opl(int op
)
1158 int t
, a
, b
, op1
, c
, i
;
1160 unsigned short reg_iret
= REG_IRET
;
1161 unsigned short reg_lret
= REG_LRET
;
1167 func
= TOK___divdi3
;
1170 func
= TOK___udivdi3
;
1173 func
= TOK___moddi3
;
1176 func
= TOK___umoddi3
;
1183 /* call generic long long function */
1184 vpush_global_sym(&func_old_type
, func
);
1189 vtop
->r2
= reg_lret
;
1202 /* stack: L1 H1 L2 H2 */
1207 vtop
[-2] = vtop
[-3];
1210 /* stack: H1 H2 L1 L2 */
1216 /* stack: H1 H2 L1 L2 ML MH */
1219 /* stack: ML MH H1 H2 L1 L2 */
1223 /* stack: ML MH H1 L2 H2 L1 */
1228 /* stack: ML MH M1 M2 */
1231 } else if (op
== '+' || op
== '-') {
1232 /* XXX: add non carry method too (for MIPS or alpha) */
1238 /* stack: H1 H2 (L1 op L2) */
1241 gen_op(op1
+ 1); /* TOK_xxxC2 */
1244 /* stack: H1 H2 (L1 op L2) */
1247 /* stack: (L1 op L2) H1 H2 */
1249 /* stack: (L1 op L2) (H1 op H2) */
1257 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1258 t
= vtop
[-1].type
.t
;
1262 /* stack: L H shift */
1264 /* constant: simpler */
1265 /* NOTE: all comments are for SHL. the other cases are
1266 done by swaping words */
1277 if (op
!= TOK_SAR
) {
1310 /* XXX: should provide a faster fallback on x86 ? */
1313 func
= TOK___ashrdi3
;
1316 func
= TOK___lshrdi3
;
1319 func
= TOK___ashldi3
;
1325 /* compare operations */
1331 /* stack: L1 H1 L2 H2 */
1333 vtop
[-1] = vtop
[-2];
1335 /* stack: L1 L2 H1 H2 */
1338 /* when values are equal, we need to compare low words. since
1339 the jump is inverted, we invert the test too. */
1342 else if (op1
== TOK_GT
)
1344 else if (op1
== TOK_ULT
)
1346 else if (op1
== TOK_UGT
)
1351 if (op1
!= TOK_NE
) {
1355 /* generate non equal test */
1356 /* XXX: NOT PORTABLE yet */
1360 #if defined(TCC_TARGET_I386)
1361 b
= psym(0x850f, 0);
1362 #elif defined(TCC_TARGET_ARM)
1364 o(0x1A000000 | encbranch(ind
, 0, 1));
1365 #elif defined(TCC_TARGET_C67)
1366 tcc_error("not implemented");
1368 #error not supported
1372 /* compare low. Always unsigned */
1376 else if (op1
== TOK_LE
)
1378 else if (op1
== TOK_GT
)
1380 else if (op1
== TOK_GE
)
1391 /* handle integer constant optimizations and various machine
1393 static void gen_opic(int op
)
1395 int c1
, c2
, t1
, t2
, n
;
1398 typedef unsigned long long U
;
1402 t1
= v1
->type
.t
& VT_BTYPE
;
1403 t2
= v2
->type
.t
& VT_BTYPE
;
1407 else if (v1
->type
.t
& VT_UNSIGNED
)
1414 else if (v2
->type
.t
& VT_UNSIGNED
)
1419 /* currently, we cannot do computations with forward symbols */
1420 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1421 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1424 case '+': l1
+= l2
; break;
1425 case '-': l1
-= l2
; break;
1426 case '&': l1
&= l2
; break;
1427 case '^': l1
^= l2
; break;
1428 case '|': l1
|= l2
; break;
1429 case '*': l1
*= l2
; break;
1436 /* if division by zero, generate explicit division */
1439 tcc_error("division by zero in constant");
1443 default: l1
/= l2
; break;
1444 case '%': l1
%= l2
; break;
1445 case TOK_UDIV
: l1
= (U
)l1
/ l2
; break;
1446 case TOK_UMOD
: l1
= (U
)l1
% l2
; break;
1449 case TOK_SHL
: l1
<<= l2
; break;
1450 case TOK_SHR
: l1
= (U
)l1
>> l2
; break;
1451 case TOK_SAR
: l1
>>= l2
; break;
1453 case TOK_ULT
: l1
= (U
)l1
< (U
)l2
; break;
1454 case TOK_UGE
: l1
= (U
)l1
>= (U
)l2
; break;
1455 case TOK_EQ
: l1
= l1
== l2
; break;
1456 case TOK_NE
: l1
= l1
!= l2
; break;
1457 case TOK_ULE
: l1
= (U
)l1
<= (U
)l2
; break;
1458 case TOK_UGT
: l1
= (U
)l1
> (U
)l2
; break;
1459 case TOK_LT
: l1
= l1
< l2
; break;
1460 case TOK_GE
: l1
= l1
>= l2
; break;
1461 case TOK_LE
: l1
= l1
<= l2
; break;
1462 case TOK_GT
: l1
= l1
> l2
; break;
1464 case TOK_LAND
: l1
= l1
&& l2
; break;
1465 case TOK_LOR
: l1
= l1
|| l2
; break;
1472 /* if commutative ops, put c2 as constant */
1473 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1474 op
== '|' || op
== '*')) {
1476 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1477 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1479 /* Filter out NOP operations like x*1, x-0, x&-1... */
1480 if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1483 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1484 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1490 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1491 /* try to use shifts instead of muls or divs */
1492 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1501 else if (op
== TOK_PDIV
)
1507 } else if (c2
&& (op
== '+' || op
== '-') &&
1508 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
1509 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1510 /* symbol + constant case */
1517 if (!nocode_wanted
) {
1518 /* call low level op generator */
1519 if (t1
== VT_LLONG
|| t2
== VT_LLONG
)
1530 /* generate a floating point operation with constant propagation */
1531 static void gen_opif(int op
)
1539 /* currently, we cannot do computations with forward symbols */
1540 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1541 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1543 if (v1
->type
.t
== VT_FLOAT
) {
1546 } else if (v1
->type
.t
== VT_DOUBLE
) {
1554 /* NOTE: we only do constant propagation if finite number (not
1555 NaN or infinity) (ANSI spec) */
1556 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1560 case '+': f1
+= f2
; break;
1561 case '-': f1
-= f2
; break;
1562 case '*': f1
*= f2
; break;
1566 tcc_error("division by zero in constant");
1571 /* XXX: also handles tests ? */
1575 /* XXX: overflow test ? */
1576 if (v1
->type
.t
== VT_FLOAT
) {
1578 } else if (v1
->type
.t
== VT_DOUBLE
) {
1586 if (!nocode_wanted
) {
1594 static int pointed_size(CType
*type
)
1597 return type_size(pointed_type(type
), &align
);
1600 static void vla_runtime_pointed_size(CType
*type
)
1603 vla_runtime_type_size(pointed_type(type
), &align
);
1606 static inline int is_null_pointer(SValue
*p
)
1608 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1610 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& p
->c
.i
== 0) ||
1611 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.ll
== 0) ||
1612 ((p
->type
.t
& VT_BTYPE
) == VT_PTR
&& p
->c
.ptr_offset
== 0);
1615 static inline int is_integer_btype(int bt
)
1617 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
1618 bt
== VT_INT
|| bt
== VT_LLONG
);
1621 /* check types for comparison or subtraction of pointers */
1622 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
1624 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
1627 /* null pointers are accepted for all comparisons as gcc */
1628 if (is_null_pointer(p1
) || is_null_pointer(p2
))
1632 bt1
= type1
->t
& VT_BTYPE
;
1633 bt2
= type2
->t
& VT_BTYPE
;
1634 /* accept comparison between pointer and integer with a warning */
1635 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
1636 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
1637 tcc_warning("comparison between pointer and integer");
1641 /* both must be pointers or implicit function pointers */
1642 if (bt1
== VT_PTR
) {
1643 type1
= pointed_type(type1
);
1644 } else if (bt1
!= VT_FUNC
)
1645 goto invalid_operands
;
1647 if (bt2
== VT_PTR
) {
1648 type2
= pointed_type(type2
);
1649 } else if (bt2
!= VT_FUNC
) {
1651 tcc_error("invalid operands to binary %s", get_tok_str(op
, NULL
));
1653 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1654 (type2
->t
& VT_BTYPE
) == VT_VOID
)
1658 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1659 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1660 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1661 /* gcc-like error if '-' is used */
1663 goto invalid_operands
;
1665 tcc_warning("comparison of distinct pointer types lacks a cast");
1669 /* generic gen_op: handles types problems */
1670 ST_FUNC
void gen_op(int op
)
1672 int u
, t1
, t2
, bt1
, bt2
, t
;
1675 t1
= vtop
[-1].type
.t
;
1676 t2
= vtop
[0].type
.t
;
1677 bt1
= t1
& VT_BTYPE
;
1678 bt2
= t2
& VT_BTYPE
;
1680 if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
1681 /* at least one operand is a pointer */
1682 /* relationnal op: must be both pointers */
1683 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
1684 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1685 /* pointers are handled are unsigned */
1686 #ifdef TCC_TARGET_X86_64
1687 t
= VT_LLONG
| VT_UNSIGNED
;
1689 t
= VT_INT
| VT_UNSIGNED
;
1693 /* if both pointers, then it must be the '-' op */
1694 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
1696 tcc_error("cannot use pointers here");
1697 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1698 /* XXX: check that types are compatible */
1699 if (vtop
[-1].type
.t
& VT_VLA
) {
1700 vla_runtime_pointed_size(&vtop
[-1].type
);
1702 vpushi(pointed_size(&vtop
[-1].type
));
1706 /* set to integer type */
1707 #ifdef TCC_TARGET_X86_64
1708 vtop
->type
.t
= VT_LLONG
;
1710 vtop
->type
.t
= VT_INT
;
1715 /* exactly one pointer : must be '+' or '-'. */
1716 if (op
!= '-' && op
!= '+')
1717 tcc_error("cannot use pointers here");
1718 /* Put pointer as first operand */
1719 if (bt2
== VT_PTR
) {
1723 type1
= vtop
[-1].type
;
1724 type1
.t
&= ~VT_ARRAY
;
1725 if (vtop
[-1].type
.t
& VT_VLA
)
1726 vla_runtime_pointed_size(&vtop
[-1].type
);
1728 u
= pointed_size(&vtop
[-1].type
);
1730 tcc_error("unknown array element size");
1731 #ifdef TCC_TARGET_X86_64
1734 /* XXX: cast to int ? (long long case) */
1739 #ifdef CONFIG_TCC_BCHECK
1740 /* if evaluating constant expression, no code should be
1741 generated, so no bound check */
1742 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
1743 /* if bounded pointers, we generate a special code to
1750 gen_bounded_ptr_add();
1756 /* put again type if gen_opic() swaped operands */
1759 } else if (is_float(bt1
) || is_float(bt2
)) {
1760 /* compute bigger type and do implicit casts */
1761 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
1763 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
1768 /* floats can only be used for a few operations */
1769 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
1770 (op
< TOK_ULT
|| op
> TOK_GT
))
1771 tcc_error("invalid operands for binary operation");
1773 } else if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
) {
1774 t
= bt1
== VT_LLONG
? VT_LLONG
: VT_INT
;
1775 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (t
| VT_UNSIGNED
))
1778 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
1779 /* cast to biggest op */
1781 /* convert to unsigned if it does not fit in a long long */
1782 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
1783 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
1786 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
1787 tcc_error("comparison of struct");
1789 /* integer operations */
1791 /* convert to unsigned if it does not fit in an integer */
1792 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
1793 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
1796 /* XXX: currently, some unsigned operations are explicit, so
1797 we modify them here */
1798 if (t
& VT_UNSIGNED
) {
1805 else if (op
== TOK_LT
)
1807 else if (op
== TOK_GT
)
1809 else if (op
== TOK_LE
)
1811 else if (op
== TOK_GE
)
1818 /* special case for shifts and long long: we keep the shift as
1820 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
1827 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
1828 /* relationnal op: the result is an int */
1829 vtop
->type
.t
= VT_INT
;
1836 #ifndef TCC_TARGET_ARM
1837 /* generic itof for unsigned long long case */
1838 static void gen_cvt_itof1(int t
)
1840 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
1841 (VT_LLONG
| VT_UNSIGNED
)) {
1844 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
1845 #if LDOUBLE_SIZE != 8
1846 else if (t
== VT_LDOUBLE
)
1847 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
1850 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
1854 vtop
->r
= reg_fret(t
);
1861 /* generic ftoi for unsigned long long case */
1862 static void gen_cvt_ftoi1(int t
)
1866 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
1867 /* not handled natively */
1868 st
= vtop
->type
.t
& VT_BTYPE
;
1870 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
1871 #if LDOUBLE_SIZE != 8
1872 else if (st
== VT_LDOUBLE
)
1873 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
1876 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
1881 vtop
->r2
= REG_LRET
;
1887 /* force char or short cast */
1888 static void force_charshort_cast(int t
)
1892 /* XXX: add optimization if lvalue : just change type and offset */
1897 if (t
& VT_UNSIGNED
) {
1898 vpushi((1 << bits
) - 1);
1904 /* result must be signed or the SAR is converted to an SHL
1905 This was not the case when "t" was a signed short
1906 and the last value on the stack was an unsigned int */
1907 vtop
->type
.t
&= ~VT_UNSIGNED
;
1913 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1914 static void gen_cast(CType
*type
)
1916 int sbt
, dbt
, sf
, df
, c
, p
;
1918 /* special delayed cast for char/short */
1919 /* XXX: in some cases (multiple cascaded casts), it may still
1921 if (vtop
->r
& VT_MUSTCAST
) {
1922 vtop
->r
&= ~VT_MUSTCAST
;
1923 force_charshort_cast(vtop
->type
.t
);
1926 /* bitfields first get cast to ints */
1927 if (vtop
->type
.t
& VT_BITFIELD
) {
1931 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
1932 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
1937 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1938 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
1940 /* constant case: we can do it now */
1941 /* XXX: in ISOC, cannot do it if error in convert */
1942 if (sbt
== VT_FLOAT
)
1943 vtop
->c
.ld
= vtop
->c
.f
;
1944 else if (sbt
== VT_DOUBLE
)
1945 vtop
->c
.ld
= vtop
->c
.d
;
1948 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
1949 if (sbt
& VT_UNSIGNED
)
1950 vtop
->c
.ld
= vtop
->c
.ull
;
1952 vtop
->c
.ld
= vtop
->c
.ll
;
1954 if (sbt
& VT_UNSIGNED
)
1955 vtop
->c
.ld
= vtop
->c
.ui
;
1957 vtop
->c
.ld
= vtop
->c
.i
;
1960 if (dbt
== VT_FLOAT
)
1961 vtop
->c
.f
= (float)vtop
->c
.ld
;
1962 else if (dbt
== VT_DOUBLE
)
1963 vtop
->c
.d
= (double)vtop
->c
.ld
;
1964 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
1965 vtop
->c
.ull
= (unsigned long long)vtop
->c
.ld
;
1966 } else if (sf
&& dbt
== VT_BOOL
) {
1967 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
1970 vtop
->c
.ll
= (long long)vtop
->c
.ld
;
1971 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
1972 vtop
->c
.ll
= vtop
->c
.ull
;
1973 else if (sbt
& VT_UNSIGNED
)
1974 vtop
->c
.ll
= vtop
->c
.ui
;
1975 #ifdef TCC_TARGET_X86_64
1976 else if (sbt
== VT_PTR
)
1979 else if (sbt
!= VT_LLONG
)
1980 vtop
->c
.ll
= vtop
->c
.i
;
1982 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
1983 vtop
->c
.ull
= vtop
->c
.ll
;
1984 else if (dbt
== VT_BOOL
)
1985 vtop
->c
.i
= (vtop
->c
.ll
!= 0);
1986 #ifdef TCC_TARGET_X86_64
1987 else if (dbt
== VT_PTR
)
1990 else if (dbt
!= VT_LLONG
) {
1991 int s
, dt
, warr
= 0;
1993 dt
= dbt
& VT_BTYPE
;
1996 if((ll
!= (unsigned char)ll
) && (ll
!= (char)ll
))
1999 }else if (dt
== VT_SHORT
){
2000 if((ll
!= (unsigned short)ll
) && (ll
!= (short)ll
))
2004 if((ll
!= (unsigned int)ll
) && (ll
!= (int)ll
))
2008 if(warr
&& !is_force
){
2010 tcc_warning("large integer implicitly truncated to unsigned type");
2013 tcc_warning("overflow in implicit constant conversion");
2015 if(dbt
& VT_UNSIGNED
)
2016 vtop
->c
.ui
= ((unsigned int)vtop
->c
.ll
<< s
) >> s
;
2018 vtop
->c
.i
= ((int)vtop
->c
.ll
<< s
) >> s
;
2021 } else if (p
&& dbt
== VT_BOOL
) {
2024 } else if (!nocode_wanted
) {
2025 /* non constant case: generate code */
2027 /* convert from fp to fp */
2030 /* convert int to fp */
2033 /* convert fp to int */
2034 if (dbt
== VT_BOOL
) {
2038 /* we handle char/short/etc... with generic code */
2039 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
2040 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
2044 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
2045 /* additional cast for char/short... */
2050 #ifndef TCC_TARGET_X86_64
2051 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
2052 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
2053 /* scalar to long long */
2054 /* machine independent conversion */
2056 /* generate high word */
2057 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
2061 if (sbt
== VT_PTR
) {
2062 /* cast from pointer to int before we apply
2063 shift operation, which pointers don't support*/
2064 gen_cast(&int_type
);
2070 /* patch second register */
2071 vtop
[-1].r2
= vtop
->r
;
2075 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
2076 (dbt
& VT_BTYPE
) == VT_PTR
||
2077 (dbt
& VT_BTYPE
) == VT_FUNC
) {
2078 if ((sbt
& VT_BTYPE
) != VT_LLONG
&&
2079 (sbt
& VT_BTYPE
) != VT_PTR
&&
2080 (sbt
& VT_BTYPE
) != VT_FUNC
) {
2081 /* need to convert from 32bit to 64bit */
2083 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
2084 /* x86_64 specific: movslq */
2086 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
2090 } else if (dbt
== VT_BOOL
) {
2091 /* scalar to bool */
2094 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
2095 (dbt
& VT_BTYPE
) == VT_SHORT
) {
2096 if (sbt
== VT_PTR
) {
2097 vtop
->type
.t
= VT_INT
;
2098 tcc_warning("nonportable conversion from pointer to char/short");
2100 force_charshort_cast(dbt
);
2101 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
2103 if (sbt
== VT_LLONG
) {
2104 /* from long long: just take low order word */
2108 /* if lvalue and single word type, nothing to do because
2109 the lvalue already contains the real type size (see
2110 VT_LVAL_xxx constants) */
2113 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
2114 /* if we are casting between pointer types,
2115 we must update the VT_LVAL_xxx size */
2116 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
2117 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
2122 /* return type size as known at compile time. Put alignment at 'a' */
2123 ST_FUNC
int type_size(CType
*type
, int *a
)
2128 bt
= type
->t
& VT_BTYPE
;
2129 if (bt
== VT_STRUCT
) {
2134 } else if (bt
== VT_PTR
) {
2135 if (type
->t
& VT_ARRAY
) {
2139 ts
= type_size(&s
->type
, a
);
2141 if (ts
< 0 && s
->c
< 0)
2149 } else if (bt
== VT_LDOUBLE
) {
2151 return LDOUBLE_SIZE
;
2152 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
2153 #ifdef TCC_TARGET_I386
2154 #ifdef TCC_TARGET_PE
2159 #elif defined(TCC_TARGET_ARM)
2169 } else if (bt
== VT_INT
|| bt
== VT_ENUM
|| bt
== VT_FLOAT
) {
2172 } else if (bt
== VT_SHORT
) {
2175 } else if (bt
== VT_QLONG
|| bt
== VT_QFLOAT
) {
2179 /* char, void, function, _Bool */
2185 /* push type size as known at runtime time on top of value stack. Put
2187 ST_FUNC
void vla_runtime_type_size(CType
*type
, int *a
)
2189 if (type
->t
& VT_VLA
) {
2190 vset(&int_type
, VT_LOCAL
|VT_LVAL
, type
->ref
->c
);
2192 vpushi(type_size(type
, a
));
2196 static void vla_sp_save(void) {
2197 if (!(vla_flags
& VLA_SP_LOC_SET
)) {
2198 *vla_sp_loc
= (loc
-= PTR_SIZE
);
2199 vla_flags
|= VLA_SP_LOC_SET
;
2201 if (!(vla_flags
& VLA_SP_SAVED
)) {
2202 gen_vla_sp_save(*vla_sp_loc
);
2203 vla_flags
|= VLA_SP_SAVED
;
2207 /* return the pointed type of t */
2208 static inline CType
*pointed_type(CType
*type
)
2210 return &type
->ref
->type
;
2213 /* modify type so that its it is a pointer to type. */
2214 ST_FUNC
void mk_pointer(CType
*type
)
2217 s
= sym_push(SYM_FIELD
, type
, 0, -1);
2218 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
2222 /* compare function types. OLD functions match any new functions */
2223 static int is_compatible_func(CType
*type1
, CType
*type2
)
2229 if (!is_compatible_types(&s1
->type
, &s2
->type
))
2231 /* check func_call */
2232 if (s1
->a
.func_call
!= s2
->a
.func_call
)
2234 /* XXX: not complete */
2235 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
2239 while (s1
!= NULL
) {
2242 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
2252 /* return true if type1 and type2 are the same. If unqualified is
2253 true, qualifiers on the types are ignored.
2255 - enums are not checked as gcc __builtin_types_compatible_p ()
2257 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2261 t1
= type1
->t
& VT_TYPE
;
2262 t2
= type2
->t
& VT_TYPE
;
2264 /* strip qualifiers before comparing */
2265 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2266 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2268 /* Default Vs explicit signedness only matters for char */
2269 if ((t1
& VT_BTYPE
) != VT_BYTE
) {
2273 /* XXX: bitfields ? */
2276 /* test more complicated cases */
2277 bt1
= t1
& VT_BTYPE
;
2278 if (bt1
== VT_PTR
) {
2279 type1
= pointed_type(type1
);
2280 type2
= pointed_type(type2
);
2281 return is_compatible_types(type1
, type2
);
2282 } else if (bt1
== VT_STRUCT
) {
2283 return (type1
->ref
== type2
->ref
);
2284 } else if (bt1
== VT_FUNC
) {
2285 return is_compatible_func(type1
, type2
);
2291 /* return true if type1 and type2 are exactly the same (including
2294 static int is_compatible_types(CType
*type1
, CType
*type2
)
2296 return compare_types(type1
,type2
,0);
2299 /* return true if type1 and type2 are the same (ignoring qualifiers).
2301 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
2303 return compare_types(type1
,type2
,1);
2306 /* print a type. If 'varstr' is not NULL, then the variable is also
2307 printed in the type */
2309 /* XXX: add array and function pointers */
2310 static void type_to_str(char *buf
, int buf_size
,
2311 CType
*type
, const char *varstr
)
2318 t
= type
->t
& VT_TYPE
;
2321 if (t
& VT_CONSTANT
)
2322 pstrcat(buf
, buf_size
, "const ");
2323 if (t
& VT_VOLATILE
)
2324 pstrcat(buf
, buf_size
, "volatile ");
2325 if ((t
& (VT_DEFSIGN
| VT_UNSIGNED
)) == (VT_DEFSIGN
| VT_UNSIGNED
))
2326 pstrcat(buf
, buf_size
, "unsigned ");
2327 else if (t
& VT_DEFSIGN
)
2328 pstrcat(buf
, buf_size
, "signed ");
2358 tstr
= "long double";
2360 pstrcat(buf
, buf_size
, tstr
);
2364 if (bt
== VT_STRUCT
)
2368 pstrcat(buf
, buf_size
, tstr
);
2369 v
= type
->ref
->v
& ~SYM_STRUCT
;
2370 if (v
>= SYM_FIRST_ANOM
)
2371 pstrcat(buf
, buf_size
, "<anonymous>");
2373 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
2377 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
2378 pstrcat(buf
, buf_size
, "(");
2380 while (sa
!= NULL
) {
2381 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
2382 pstrcat(buf
, buf_size
, buf1
);
2385 pstrcat(buf
, buf_size
, ", ");
2387 pstrcat(buf
, buf_size
, ")");
2391 pstrcpy(buf1
, sizeof(buf1
), "*");
2393 pstrcat(buf1
, sizeof(buf1
), varstr
);
2394 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2398 pstrcat(buf
, buf_size
, " ");
2399 pstrcat(buf
, buf_size
, varstr
);
2404 /* verify type compatibility to store vtop in 'dt' type, and generate
2406 static void gen_assign_cast(CType
*dt
)
2408 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
2409 char buf1
[256], buf2
[256];
2412 st
= &vtop
->type
; /* source type */
2413 dbt
= dt
->t
& VT_BTYPE
;
2414 sbt
= st
->t
& VT_BTYPE
;
2415 if (sbt
== VT_VOID
|| dbt
== VT_VOID
)
2416 tcc_error("cannot cast from/to void");
2417 if (dt
->t
& VT_CONSTANT
)
2418 tcc_warning("assignment of read-only location");
2421 /* special cases for pointers */
2422 /* '0' can also be a pointer */
2423 if (is_null_pointer(vtop
))
2425 /* accept implicit pointer to integer cast with warning */
2426 if (is_integer_btype(sbt
)) {
2427 tcc_warning("assignment makes pointer from integer without a cast");
2430 type1
= pointed_type(dt
);
2431 /* a function is implicitely a function pointer */
2432 if (sbt
== VT_FUNC
) {
2433 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
2434 !is_compatible_types(pointed_type(dt
), st
))
2435 tcc_warning("assignment from incompatible pointer type");
2440 type2
= pointed_type(st
);
2441 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2442 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
2443 /* void * can match anything */
2445 /* exact type match, except for unsigned */
2448 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
|
2450 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
|
2452 if (!is_compatible_types(&tmp_type1
, &tmp_type2
))
2453 tcc_warning("assignment from incompatible pointer type");
2455 /* check const and volatile */
2456 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
2457 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
2458 tcc_warning("assignment discards qualifiers from pointer target type");
2464 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
2465 tcc_warning("assignment makes integer from pointer without a cast");
2467 if (sbt
== VT_STRUCT
)
2469 /* XXX: more tests */
2474 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2475 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2476 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2478 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
2479 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
2480 tcc_error("cannot cast '%s' to '%s'", buf1
, buf2
);
2488 /* store vtop in lvalue pushed on stack */
2489 ST_FUNC
void vstore(void)
2491 int sbt
, dbt
, ft
, cc
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
2493 ft
= vtop
[-1].type
.t
;
2494 sbt
= vtop
->type
.t
& VT_BTYPE
;
2495 dbt
= ft
& VT_BTYPE
;
2496 cc
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
2497 if ((((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2498 (sbt
== VT_INT
&& dbt
== VT_SHORT
))
2499 && !(vtop
->type
.t
& VT_BITFIELD
) && !cc
) {
2500 /* optimize char/short casts */
2501 delayed_cast
= VT_MUSTCAST
;
2502 vtop
->type
.t
= ft
& (VT_TYPE
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
)));
2503 /* XXX: factorize */
2504 if (ft
& VT_CONSTANT
)
2505 tcc_warning("assignment of read-only location");
2508 if (!(ft
& VT_BITFIELD
))
2509 gen_assign_cast(&vtop
[-1].type
);
2512 if (sbt
== VT_STRUCT
) {
2513 /* if structure, only generate pointer */
2514 /* structure assignment : generate memcpy */
2515 /* XXX: optimize if small size */
2516 if (!nocode_wanted
) {
2517 size
= type_size(&vtop
->type
, &align
);
2521 vtop
->type
.t
= VT_PTR
;
2524 /* address of memcpy() */
2527 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2528 else if(!(align
& 3))
2529 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2532 vpush_global_sym(&func_old_type
, TOK_memcpy
);
2537 vtop
->type
.t
= VT_PTR
;
2546 /* leave source on stack */
2547 } else if (ft
& VT_BITFIELD
) {
2548 /* bitfield store handling */
2549 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2550 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2551 /* remove bit field info to avoid loops */
2552 vtop
[-1].type
.t
= ft
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
2554 if((ft
& VT_BTYPE
) == VT_BOOL
) {
2555 gen_cast(&vtop
[-1].type
);
2556 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2559 /* duplicate destination */
2561 vtop
[-1] = vtop
[-2];
2563 /* mask and shift source */
2564 if((ft
& VT_BTYPE
) != VT_BOOL
) {
2565 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2566 vpushll((1ULL << bit_size
) - 1ULL);
2568 vpushi((1 << bit_size
) - 1);
2574 /* load destination, mask and or with source */
2576 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2577 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2579 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2586 #ifdef CONFIG_TCC_BCHECK
2587 /* bound check case */
2588 if (vtop
[-1].r
& VT_MUSTBOUND
) {
2594 if (!nocode_wanted
) {
2598 #ifdef TCC_TARGET_X86_64
2599 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
2601 } else if ((ft
& VT_BTYPE
) == VT_QFLOAT
) {
2606 r
= gv(rc
); /* generate value */
2607 /* if lvalue was saved on stack, must read it */
2608 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
2610 t
= get_reg(RC_INT
);
2611 #ifdef TCC_TARGET_X86_64
2616 sv
.r
= VT_LOCAL
| VT_LVAL
;
2617 sv
.c
.ul
= vtop
[-1].c
.ul
;
2619 vtop
[-1].r
= t
| VT_LVAL
;
2621 /* two word case handling : store second register at word + 4 (or +8 for x86-64) */
2622 #ifdef TCC_TARGET_X86_64
2623 if (((ft
& VT_BTYPE
) == VT_QLONG
) || ((ft
& VT_BTYPE
) == VT_QFLOAT
)) {
2624 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
2626 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
2627 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
2629 vtop
[-1].type
.t
= load_type
;
2632 /* convert to int to increment easily */
2633 vtop
->type
.t
= addr_type
;
2639 vtop
[-1].type
.t
= load_type
;
2640 /* XXX: it works because r2 is spilled last ! */
2641 store(vtop
->r2
, vtop
- 1);
2647 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
2648 vtop
->r
|= delayed_cast
;
2652 /* post defines POST/PRE add. c is the token ++ or -- */
2653 ST_FUNC
void inc(int post
, int c
)
2656 vdup(); /* save lvalue */
2658 gv_dup(); /* duplicate value */
2663 vpushi(c
- TOK_MID
);
2665 vstore(); /* store value */
2667 vpop(); /* if post op, return saved value */
2670 /* Parse GNUC __attribute__ extension. Currently, the following
2671 extensions are recognized:
2672 - aligned(n) : set data/function alignment.
2673 - packed : force data alignment to 1
2674 - section(x) : generate data/code in this section.
2675 - unused : currently ignored, but may be used someday.
2676 - regparm(n) : pass function parameters in registers (i386 only)
2678 static void parse_attribute(AttributeDef
*ad
)
2682 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
2686 while (tok
!= ')') {
2687 if (tok
< TOK_IDENT
)
2688 expect("attribute name");
2696 expect("section name");
2697 ad
->section
= find_section(tcc_state
, (char *)tokc
.cstr
->data
);
2705 expect("alias(\"target\")");
2706 ad
->alias_target
= /* save string as token, for later */
2707 tok_alloc((char*)tokc
.cstr
->data
, tokc
.cstr
->size
-1)->tok
;
2711 case TOK_VISIBILITY1
:
2712 case TOK_VISIBILITY2
:
2715 expect("visibility(\"default|hidden|internal|protected\")");
2716 if (!strcmp (tokc
.cstr
->data
, "default"))
2717 ad
->a
.visibility
= STV_DEFAULT
;
2718 else if (!strcmp (tokc
.cstr
->data
, "hidden"))
2719 ad
->a
.visibility
= STV_HIDDEN
;
2720 else if (!strcmp (tokc
.cstr
->data
, "internal"))
2721 ad
->a
.visibility
= STV_INTERNAL
;
2722 else if (!strcmp (tokc
.cstr
->data
, "protected"))
2723 ad
->a
.visibility
= STV_PROTECTED
;
2725 expect("visibility(\"default|hidden|internal|protected\")");
2734 if (n
<= 0 || (n
& (n
- 1)) != 0)
2735 tcc_error("alignment must be a positive power of two");
2752 /* currently, no need to handle it because tcc does not
2753 track unused objects */
2757 /* currently, no need to handle it because tcc does not
2758 track unused objects */
2763 ad
->a
.func_call
= FUNC_CDECL
;
2768 ad
->a
.func_call
= FUNC_STDCALL
;
2770 #ifdef TCC_TARGET_I386
2780 ad
->a
.func_call
= FUNC_FASTCALL1
+ n
- 1;
2786 ad
->a
.func_call
= FUNC_FASTCALLW
;
2793 ad
->a
.mode
= VT_LLONG
+ 1;
2796 ad
->a
.mode
= VT_SHORT
+ 1;
2799 ad
->a
.mode
= VT_INT
+ 1;
2802 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
2809 ad
->a
.func_export
= 1;
2812 ad
->a
.func_import
= 1;
2815 if (tcc_state
->warn_unsupported
)
2816 tcc_warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
2817 /* skip parameters */
2819 int parenthesis
= 0;
2823 else if (tok
== ')')
2826 } while (parenthesis
&& tok
!= -1);
2839 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2840 static void struct_decl(CType
*type
, int u
, int tdef
)
2842 int a
, v
, size
, align
, maxalign
, c
, offset
, flexible
;
2843 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
, prevbt
;
2844 Sym
*s
, *ss
, *ass
, **ps
;
2848 a
= tok
; /* save decl type */
2853 /* struct already defined ? return it */
2855 expect("struct/union/enum name");
2859 tcc_error("invalid type: '%s'", get_tok_str(v
, NULL
));
2861 } else if (tok
>= TOK_IDENT
&& !tdef
)
2862 tcc_error("unknown struct/union/enum");
2868 /* we put an undefined size for struct/union */
2869 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
2870 s
->r
= 0; /* default alignment is zero as gcc */
2871 /* put struct/union/enum name in type */
2879 tcc_error("struct/union/enum already defined");
2880 /* cannot be empty */
2882 /* non empty enums are not allowed */
2883 if (a
== TOK_ENUM
) {
2887 expect("identifier");
2889 if (ss
&& !local_stack
)
2890 tcc_error("redefinition of enumerator '%s'",
2891 get_tok_str(v
, NULL
));
2897 /* enum symbols have static storage */
2898 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
2899 ss
->type
.t
|= VT_STATIC
;
2904 /* NOTE: we accept a trailing comma */
2908 s
->c
= type_size(&int_type
, &align
);
2917 while (tok
!= '}') {
2918 parse_btype(&btype
, &ad
);
2921 tcc_error("flexible array member '%s' not at the end of struct",
2922 get_tok_str(v
, NULL
));
2927 type_decl(&type1
, &ad
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
2928 if (v
== 0 && (type1
.t
& VT_BTYPE
) != VT_STRUCT
)
2929 expect("identifier");
2930 if (type_size(&type1
, &align
) < 0) {
2931 if ((a
== TOK_STRUCT
) && (type1
.t
& VT_ARRAY
))
2934 tcc_error("field '%s' has incomplete type",
2935 get_tok_str(v
, NULL
));
2937 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
2938 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
2939 tcc_error("invalid type for '%s'",
2940 get_tok_str(v
, NULL
));
2944 bit_size
= expr_const();
2945 /* XXX: handle v = 0 case for messages */
2947 tcc_error("negative width in bit-field '%s'",
2948 get_tok_str(v
, NULL
));
2949 if (v
&& bit_size
== 0)
2950 tcc_error("zero width for bit-field '%s'",
2951 get_tok_str(v
, NULL
));
2953 size
= type_size(&type1
, &align
);
2955 if (align
< ad
.a
.aligned
)
2956 align
= ad
.a
.aligned
;
2957 } else if (ad
.a
.packed
) {
2959 } else if (*tcc_state
->pack_stack_ptr
) {
2960 if (align
> *tcc_state
->pack_stack_ptr
)
2961 align
= *tcc_state
->pack_stack_ptr
;
2964 if (bit_size
>= 0) {
2965 bt
= type1
.t
& VT_BTYPE
;
2972 tcc_error("bitfields must have scalar type");
2974 if (bit_size
> bsize
) {
2975 tcc_error("width of '%s' exceeds its type",
2976 get_tok_str(v
, NULL
));
2977 } else if (bit_size
== bsize
) {
2978 /* no need for bit fields */
2980 } else if (bit_size
== 0) {
2981 /* XXX: what to do if only padding in a
2983 /* zero size: means to pad */
2986 /* we do not have enough room ?
2987 did the type change?
2989 if ((bit_pos
+ bit_size
) > bsize
||
2990 bt
!= prevbt
|| a
== TOK_UNION
)
2993 /* XXX: handle LSB first */
2994 type1
.t
|= VT_BITFIELD
|
2995 (bit_pos
<< VT_STRUCT_SHIFT
) |
2996 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
2997 bit_pos
+= bit_size
;
3003 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
3004 /* add new memory data only if starting
3006 if (lbit_pos
== 0) {
3007 if (a
== TOK_STRUCT
) {
3008 c
= (c
+ align
- 1) & -align
;
3017 if (align
> maxalign
)
3021 printf("add field %s offset=%d",
3022 get_tok_str(v
, NULL
), offset
);
3023 if (type1
.t
& VT_BITFIELD
) {
3024 printf(" pos=%d size=%d",
3025 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
3026 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
3031 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
3033 while ((ass
= ass
->next
) != NULL
) {
3034 ss
= sym_push(ass
->v
, &ass
->type
, 0, offset
+ ass
->c
);
3039 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
3043 if (tok
== ';' || tok
== TOK_EOF
)
3051 tcc_error("flexible array member '%s' in otherwise empty struct", get_tok_str(v
, NULL
));
3052 /* store size and alignment */
3053 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
3059 /* return 1 if basic type is a type size (short, long, long long) */
3060 ST_FUNC
int is_btype_size(int bt
)
3062 return bt
== VT_SHORT
|| bt
== VT_LONG
|| bt
== VT_LLONG
;
3065 /* return 0 if no type declaration. otherwise, return the basic type
3068 static int parse_btype(CType
*type
, AttributeDef
*ad
)
3070 int t
, u
, bt_size
, complete
, type_found
, typespec_found
;
3074 memset(ad
, 0, sizeof(AttributeDef
));
3082 /* currently, we really ignore extension */
3093 tcc_error("too many basic types");
3095 bt_size
= is_btype_size (u
& VT_BTYPE
);
3096 if (u
== VT_INT
|| (!bt_size
&& !(t
& VT_TYPEDEF
)))
3111 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
3112 #ifndef TCC_TARGET_PE
3113 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3115 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
3116 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3130 if ((t
& VT_BTYPE
) == VT_LONG
) {
3131 #ifdef TCC_TARGET_PE
3132 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
3134 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3142 struct_decl(&type1
, VT_ENUM
, t
& VT_TYPEDEF
);
3145 type
->ref
= type1
.ref
;
3149 struct_decl(&type1
, VT_STRUCT
, t
& VT_TYPEDEF
);
3152 /* type modifiers */
3168 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == (VT_DEFSIGN
|VT_UNSIGNED
))
3169 tcc_error("signed and unsigned modifier");
3182 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == VT_DEFSIGN
)
3183 tcc_error("signed and unsigned modifier");
3184 t
|= VT_DEFSIGN
| VT_UNSIGNED
;
3209 /* GNUC attribute */
3210 case TOK_ATTRIBUTE1
:
3211 case TOK_ATTRIBUTE2
:
3212 parse_attribute(ad
);
3215 t
= (t
& ~VT_BTYPE
) | u
;
3223 parse_expr_type(&type1
);
3224 /* remove all storage modifiers except typedef */
3225 type1
.t
&= ~(VT_STORAGE
&~VT_TYPEDEF
);
3231 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
3233 t
|= (s
->type
.t
& ~VT_TYPEDEF
);
3234 type
->ref
= s
->type
.ref
;
3236 /* get attributes from typedef */
3237 if (0 == ad
->a
.aligned
)
3238 ad
->a
.aligned
= s
->a
.aligned
;
3239 if (0 == ad
->a
.func_call
)
3240 ad
->a
.func_call
= s
->a
.func_call
;
3241 ad
->a
.packed
|= s
->a
.packed
;
3250 if (tcc_state
->char_is_unsigned
) {
3251 if ((t
& (VT_DEFSIGN
|VT_BTYPE
)) == VT_BYTE
)
3255 /* long is never used as type */
3256 if ((t
& VT_BTYPE
) == VT_LONG
)
3257 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
3258 t
= (t
& ~VT_BTYPE
) | VT_INT
;
3260 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3266 /* convert a function parameter type (array to pointer and function to
3267 function pointer) */
3268 static inline void convert_parameter_type(CType
*pt
)
3270 /* remove const and volatile qualifiers (XXX: const could be used
3271 to indicate a const function parameter */
3272 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3273 /* array must be transformed to pointer according to ANSI C */
3275 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
3280 ST_FUNC
void parse_asm_str(CString
*astr
)
3283 /* read the string */
3285 expect("string constant");
3287 while (tok
== TOK_STR
) {
3288 /* XXX: add \0 handling too ? */
3289 cstr_cat(astr
, tokc
.cstr
->data
);
3292 cstr_ccat(astr
, '\0');
3295 /* Parse an asm label and return the label
3296 * Don't forget to free the CString in the caller! */
3297 static void asm_label_instr(CString
*astr
)
3300 parse_asm_str(astr
);
3303 printf("asm_alias: \"%s\"\n", (char *)astr
->data
);
3307 static void post_type(CType
*type
, AttributeDef
*ad
)
3309 int n
, l
, t1
, arg_size
, align
;
3310 Sym
**plast
, *s
, *first
;
3315 /* function declaration */
3323 /* read param name and compute offset */
3324 if (l
!= FUNC_OLD
) {
3325 if (!parse_btype(&pt
, &ad1
)) {
3327 tcc_error("invalid type");
3334 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
3336 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3337 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
3338 tcc_error("parameter declared as void");
3339 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
3344 expect("identifier");
3348 convert_parameter_type(&pt
);
3349 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
3355 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
3362 /* if no parameters, then old type prototype */
3366 /* NOTE: const is ignored in returned type as it has a special
3367 meaning in gcc / C++ */
3368 type
->t
&= ~VT_CONSTANT
;
3369 /* some ancient pre-K&R C allows a function to return an array
3370 and the array brackets to be put after the arguments, such
3371 that "int c()[]" means something like "int[] c()" */
3374 skip(']'); /* only handle simple "[]" */
3377 /* we push a anonymous symbol which will contain the function prototype */
3378 ad
->a
.func_args
= arg_size
;
3379 s
= sym_push(SYM_FIELD
, type
, 0, l
);
3384 } else if (tok
== '[') {
3385 /* array definition */
3387 if (tok
== TOK_RESTRICT1
)
3392 if (!local_stack
|| nocode_wanted
)
3393 vpushi(expr_const());
3395 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3398 tcc_error("invalid array size");
3400 if (!is_integer_btype(vtop
->type
.t
& VT_BTYPE
))
3401 tcc_error("size of variable length array should be an integer");
3406 /* parse next post type */
3407 post_type(type
, ad
);
3408 if (type
->t
== VT_FUNC
)
3409 tcc_error("declaration of an array of functions");
3410 t1
|= type
->t
& VT_VLA
;
3413 loc
-= type_size(&int_type
, &align
);
3417 vla_runtime_type_size(type
, &align
);
3419 vset(&int_type
, VT_LOCAL
|VT_LVAL
, loc
);
3426 /* we push an anonymous symbol which will contain the array
3428 s
= sym_push(SYM_FIELD
, type
, 0, n
);
3429 type
->t
= (t1
? VT_VLA
: VT_ARRAY
) | VT_PTR
;
3434 /* Parse a type declaration (except basic type), and return the type
3435 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3436 expected. 'type' should contain the basic type. 'ad' is the
3437 attribute definition of the basic type. It can be modified by
3440 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
3443 CType type1
, *type2
;
3444 int qualifiers
, storage
;
3446 while (tok
== '*') {
3454 qualifiers
|= VT_CONSTANT
;
3459 qualifiers
|= VT_VOLATILE
;
3467 type
->t
|= qualifiers
;
3470 /* XXX: clarify attribute handling */
3471 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3472 parse_attribute(ad
);
3474 /* recursive type */
3475 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3476 type1
.t
= 0; /* XXX: same as int */
3479 /* XXX: this is not correct to modify 'ad' at this point, but
3480 the syntax is not clear */
3481 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3482 parse_attribute(ad
);
3483 type_decl(&type1
, ad
, v
, td
);
3486 /* type identifier */
3487 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
3491 if (!(td
& TYPE_ABSTRACT
))
3492 expect("identifier");
3496 storage
= type
->t
& VT_STORAGE
;
3497 type
->t
&= ~VT_STORAGE
;
3498 if (storage
& VT_STATIC
) {
3499 int saved_nocode_wanted
= nocode_wanted
;
3501 post_type(type
, ad
);
3502 nocode_wanted
= saved_nocode_wanted
;
3504 post_type(type
, ad
);
3506 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3507 parse_attribute(ad
);
3511 /* append type at the end of type1 */
3524 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3525 ST_FUNC
int lvalue_type(int t
)
3530 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
3532 else if (bt
== VT_SHORT
)
3536 if (t
& VT_UNSIGNED
)
3537 r
|= VT_LVAL_UNSIGNED
;
3541 /* indirection with full error checking and bound check */
3542 ST_FUNC
void indir(void)
3544 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
3545 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
3549 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
3551 vtop
->type
= *pointed_type(&vtop
->type
);
3552 /* Arrays and functions are never lvalues */
3553 if (!(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_VLA
)
3554 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3555 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3556 /* if bound checking, the referenced pointer must be checked */
3557 #ifdef CONFIG_TCC_BCHECK
3558 if (tcc_state
->do_bounds_check
)
3559 vtop
->r
|= VT_MUSTBOUND
;
3564 /* pass a parameter to a function and do type checking and casting */
3565 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
3570 func_type
= func
->c
;
3571 if (func_type
== FUNC_OLD
||
3572 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
3573 /* default casting : only need to convert float to double */
3574 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
3577 } else if (vtop
->type
.t
& VT_BITFIELD
) {
3578 type
.t
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
3581 } else if (arg
== NULL
) {
3582 tcc_error("too many arguments to function");
3585 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
3586 gen_assign_cast(&type
);
3590 /* parse an expression of the form '(type)' or '(expr)' and return its
3592 static void parse_expr_type(CType
*type
)
3598 if (parse_btype(type
, &ad
)) {
3599 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3606 static void parse_type(CType
*type
)
3611 if (!parse_btype(type
, &ad
)) {
3614 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3617 static void vpush_tokc(int t
)
3622 vsetc(&type
, VT_CONST
, &tokc
);
3625 ST_FUNC
void unary(void)
3627 int n
, t
, align
, size
, r
, sizeof_caller
;
3631 static int in_sizeof
= 0;
3633 sizeof_caller
= in_sizeof
;
3635 /* XXX: GCC 2.95.3 does not generate a table although it should be
3649 vpush_tokc(VT_INT
| VT_UNSIGNED
);
3653 vpush_tokc(VT_LLONG
);
3657 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
3661 vpush_tokc(VT_FLOAT
);
3665 vpush_tokc(VT_DOUBLE
);
3669 vpush_tokc(VT_LDOUBLE
);
3672 case TOK___FUNCTION__
:
3674 goto tok_identifier
;
3680 /* special function name identifier */
3681 len
= strlen(funcname
) + 1;
3682 /* generate char[len] type */
3687 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
3688 ptr
= section_ptr_add(data_section
, len
);
3689 memcpy(ptr
, funcname
, len
);
3694 #ifdef TCC_TARGET_PE
3695 t
= VT_SHORT
| VT_UNSIGNED
;
3701 /* string parsing */
3704 if (tcc_state
->warn_write_strings
)
3709 memset(&ad
, 0, sizeof(AttributeDef
));
3710 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, NULL
, 0);
3715 if (parse_btype(&type
, &ad
)) {
3716 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
3718 /* check ISOC99 compound literal */
3720 /* data is allocated locally by default */
3725 /* all except arrays are lvalues */
3726 if (!(type
.t
& VT_ARRAY
))
3727 r
|= lvalue_type(type
.t
);
3728 memset(&ad
, 0, sizeof(AttributeDef
));
3729 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, NULL
, 0);
3731 if (sizeof_caller
) {
3740 } else if (tok
== '{') {
3741 /* save all registers */
3743 /* statement expression : we do not accept break/continue
3744 inside as GCC does */
3745 block(NULL
, NULL
, NULL
, NULL
, 0, 1);
3760 /* functions names must be treated as function pointers,
3761 except for unary '&' and sizeof. Since we consider that
3762 functions are not lvalues, we only have to handle it
3763 there and in function calls. */
3764 /* arrays can also be used although they are not lvalues */
3765 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
3766 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
3768 mk_pointer(&vtop
->type
);
3774 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3776 boolean
.t
= VT_BOOL
;
3778 vtop
->c
.i
= !vtop
->c
.i
;
3779 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
3780 vtop
->c
.i
= vtop
->c
.i
^ 1;
3783 vseti(VT_JMP
, gvtst(1, 0));
3795 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
3796 tcc_error("pointer not accepted for unary plus");
3797 /* In order to force cast, we add zero, except for floating point
3798 where we really need an noop (otherwise -0.0 will be transformed
3800 if (!is_float(vtop
->type
.t
)) {
3811 unary_type(&type
); // Perform a in_sizeof = 0;
3812 size
= type_size(&type
, &align
);
3813 if (t
== TOK_SIZEOF
) {
3814 if (!(type
.t
& VT_VLA
)) {
3816 tcc_error("sizeof applied to an incomplete type");
3817 if(type
.t
& VT_BITFIELD
)
3818 tcc_error("'%s' applied to a bit-field", get_tok_str(t
, NULL
));
3821 vla_runtime_type_size(&type
, &align
);
3826 vtop
->type
.t
|= VT_UNSIGNED
;
3829 case TOK_builtin_types_compatible_p
:
3838 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3839 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3840 vpushi(is_compatible_types(&type1
, &type2
));
3843 case TOK_builtin_constant_p
:
3845 int saved_nocode_wanted
, res
;
3848 saved_nocode_wanted
= nocode_wanted
;
3851 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
3853 nocode_wanted
= saved_nocode_wanted
;
3858 case TOK_builtin_frame_address
:
3864 if (tok
!= TOK_CINT
|| tokc
.i
< 0) {
3865 tcc_error("__builtin_frame_address only takes positive integers");
3872 vset(&type
, VT_LOCAL
, 0); /* local frame */
3874 mk_pointer(&vtop
->type
);
3875 indir(); /* -> parent frame */
3879 #ifdef TCC_TARGET_X86_64
3880 #ifdef TCC_TARGET_PE
3881 case TOK_builtin_va_start
:
3889 if ((vtop
->r
& VT_VALMASK
) != VT_LOCAL
)
3890 tcc_error("__builtin_va_start expects a local variable");
3891 vtop
->r
&= ~(VT_LVAL
| VT_REF
);
3892 vtop
->type
= char_pointer_type
;
3897 case TOK_builtin_va_arg_types
:
3904 vpushi(classify_x86_64_va_arg(&type
));
3919 t
= vtop
->type
.t
& VT_BTYPE
;
3921 /* In IEEE negate(x) isn't subtract(0,x), but rather
3926 else if (t
== VT_DOUBLE
)
3937 goto tok_identifier
;
3939 /* allow to take the address of a label */
3940 if (tok
< TOK_UIDENT
)
3941 expect("label identifier");
3942 s
= label_find(tok
);
3944 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
3946 if (s
->r
== LABEL_DECLARED
)
3947 s
->r
= LABEL_FORWARD
;
3950 s
->type
.t
= VT_VOID
;
3951 mk_pointer(&s
->type
);
3952 s
->type
.t
|= VT_STATIC
;
3954 vpushsym(&s
->type
, s
);
3958 // special qnan , snan and infinity values
3960 vpush64(VT_DOUBLE
, 0x7ff8000000000000ULL
);
3964 vpush64(VT_DOUBLE
, 0x7ff0000000000001ULL
);
3968 vpush64(VT_DOUBLE
, 0x7ff0000000000000ULL
);
3977 expect("identifier");
3980 const char *name
= get_tok_str(t
, NULL
);
3982 tcc_error("'%s' undeclared", name
);
3983 /* for simple function calls, we tolerate undeclared
3984 external reference to int() function */
3985 if (tcc_state
->warn_implicit_function_declaration
3986 #ifdef TCC_TARGET_PE
3987 /* people must be warned about using undeclared WINAPI functions
3988 (which usually start with uppercase letter) */
3989 || (name
[0] >= 'A' && name
[0] <= 'Z')
3992 tcc_warning("implicit declaration of function '%s'", name
);
3993 s
= external_global_sym(t
, &func_old_type
, 0);
3995 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
3996 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
3997 /* if referencing an inline function, then we generate a
3998 symbol to it if not already done. It will have the
3999 effect to generate code for it at the end of the
4000 compilation unit. Inline function as always
4001 generated in the text section. */
4003 put_extern_sym(s
, text_section
, 0, 0);
4004 r
= VT_SYM
| VT_CONST
;
4008 vset(&s
->type
, r
, s
->c
);
4009 /* if forward reference, we must point to s */
4010 if (vtop
->r
& VT_SYM
) {
4012 vtop
->c
.ptr_offset
= 0;
4017 /* post operations */
4019 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
4022 } else if (tok
== '.' || tok
== TOK_ARROW
) {
4025 if (tok
== TOK_ARROW
)
4027 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
4031 /* expect pointer on structure */
4032 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
4033 expect("struct or union");
4037 while ((s
= s
->next
) != NULL
) {
4042 tcc_error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, NULL
));
4043 /* add field offset to pointer */
4044 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
4047 /* change type to field type, and set to lvalue */
4048 vtop
->type
= s
->type
;
4049 vtop
->type
.t
|= qualifiers
;
4050 /* an array is never an lvalue */
4051 if (!(vtop
->type
.t
& VT_ARRAY
)) {
4052 vtop
->r
|= lvalue_type(vtop
->type
.t
);
4053 #ifdef CONFIG_TCC_BCHECK
4054 /* if bound checking, the referenced pointer must be checked */
4055 if (tcc_state
->do_bounds_check
)
4056 vtop
->r
|= VT_MUSTBOUND
;
4060 } else if (tok
== '[') {
4066 } else if (tok
== '(') {
4069 int nb_args
, ret_nregs
, ret_align
, variadic
;
4072 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
4073 /* pointer test (no array accepted) */
4074 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
4075 vtop
->type
= *pointed_type(&vtop
->type
);
4076 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
4080 expect("function pointer");
4083 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
4085 /* get return type */
4088 sa
= s
->next
; /* first parameter */
4091 /* compute first implicit argument if a structure is returned */
4092 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
4093 variadic
= (s
->c
== FUNC_ELLIPSIS
);
4094 ret_nregs
= gfunc_sret(&s
->type
, variadic
, &ret
.type
,
4097 /* get some space for the returned structure */
4098 size
= type_size(&s
->type
, &align
);
4099 loc
= (loc
- size
) & -align
;
4101 ret
.r
= VT_LOCAL
| VT_LVAL
;
4102 /* pass it as 'int' to avoid structure arg passing
4104 vseti(VT_LOCAL
, loc
);
4114 /* return in register */
4115 if (is_float(ret
.type
.t
)) {
4116 ret
.r
= reg_fret(ret
.type
.t
);
4117 #ifdef TCC_TARGET_X86_64
4118 if ((ret
.type
.t
& VT_BTYPE
) == VT_QFLOAT
)
4122 #ifdef TCC_TARGET_X86_64
4123 if ((ret
.type
.t
& VT_BTYPE
) == VT_QLONG
)
4125 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
4135 gfunc_param_typed(s
, sa
);
4145 tcc_error("too few arguments to function");
4147 if (!nocode_wanted
) {
4148 gfunc_call(nb_args
);
4150 vtop
-= (nb_args
+ 1);
4154 for (r
= ret
.r
+ ret_nregs
+ !ret_nregs
; r
-- > ret
.r
;) {
4155 vsetc(&ret
.type
, r
, &ret
.c
);
4156 vtop
->r2
= ret
.r2
; /* Loop only happens when r2 is VT_CONST */
4159 /* handle packed struct return */
4160 if (((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) && ret_nregs
) {
4163 size
= type_size(&s
->type
, &align
);
4164 loc
= (loc
- size
) & -align
;
4168 vset(&ret
.type
, VT_LOCAL
| VT_LVAL
, addr
+ offset
);
4172 if (--ret_nregs
== 0)
4174 /* XXX: compatible with arm only: ret_align == register_size */
4175 offset
+= ret_align
;
4177 vset(&s
->type
, VT_LOCAL
| VT_LVAL
, addr
);
4185 ST_FUNC
void expr_prod(void)
4190 while (tok
== '*' || tok
== '/' || tok
== '%') {
4198 ST_FUNC
void expr_sum(void)
4203 while (tok
== '+' || tok
== '-') {
4211 static void expr_shift(void)
4216 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
4224 static void expr_cmp(void)
4229 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
4230 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
4238 static void expr_cmpeq(void)
4243 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
4251 static void expr_and(void)
4254 while (tok
== '&') {
4261 static void expr_xor(void)
4264 while (tok
== '^') {
4271 static void expr_or(void)
4274 while (tok
== '|') {
4281 /* XXX: fix this mess */
4282 static void expr_land_const(void)
4285 while (tok
== TOK_LAND
) {
4292 /* XXX: fix this mess */
4293 static void expr_lor_const(void)
4296 while (tok
== TOK_LOR
) {
4303 /* only used if non constant */
4304 static void expr_land(void)
4309 if (tok
== TOK_LAND
) {
4314 if (tok
!= TOK_LAND
) {
4324 static void expr_lor(void)
4329 if (tok
== TOK_LOR
) {
4334 if (tok
!= TOK_LOR
) {
4344 /* XXX: better constant handling */
4345 static void expr_cond(void)
4347 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
;
4349 CType type
, type1
, type2
;
4356 boolean
.t
= VT_BOOL
;
4362 if (tok
!= ':' || !gnu_ext
) {
4377 if (vtop
!= vstack
) {
4378 /* needed to avoid having different registers saved in
4380 if (is_float(vtop
->type
.t
)) {
4382 #ifdef TCC_TARGET_X86_64
4383 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4393 if (tok
== ':' && gnu_ext
) {
4401 sv
= *vtop
; /* save value to handle it later */
4402 vtop
--; /* no vpop so that FP stack is not flushed */
4410 bt1
= t1
& VT_BTYPE
;
4412 bt2
= t2
& VT_BTYPE
;
4413 /* cast operands to correct type according to ISOC rules */
4414 if (is_float(bt1
) || is_float(bt2
)) {
4415 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
4416 type
.t
= VT_LDOUBLE
;
4417 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
4422 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
4423 /* cast to biggest op */
4425 /* convert to unsigned if it does not fit in a long long */
4426 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
4427 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
4428 type
.t
|= VT_UNSIGNED
;
4429 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
4430 /* If one is a null ptr constant the result type
4432 if (is_null_pointer (vtop
))
4434 else if (is_null_pointer (&sv
))
4436 /* XXX: test pointer compatibility, C99 has more elaborate
4440 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
4441 /* XXX: test function pointer compatibility */
4442 type
= bt1
== VT_FUNC
? type1
: type2
;
4443 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
4444 /* XXX: test structure compatibility */
4445 type
= bt1
== VT_STRUCT
? type1
: type2
;
4446 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
4447 /* NOTE: as an extension, we accept void on only one side */
4450 /* integer operations */
4452 /* convert to unsigned if it does not fit in an integer */
4453 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
4454 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
4455 type
.t
|= VT_UNSIGNED
;
4458 /* now we convert second operand */
4460 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4463 if (is_float(type
.t
)) {
4465 #ifdef TCC_TARGET_X86_64
4466 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4470 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
4471 /* for long longs, we use fixed registers to avoid having
4472 to handle a complicated move */
4477 /* this is horrible, but we must also convert first
4481 /* put again first value and cast it */
4484 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4487 move_reg(r2
, r1
, type
.t
);
4494 static void expr_eq(void)
4500 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
4501 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
4502 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
4517 ST_FUNC
void gexpr(void)
4528 /* parse an expression and return its type without any side effect. */
4529 static void expr_type(CType
*type
)
4531 int saved_nocode_wanted
;
4533 saved_nocode_wanted
= nocode_wanted
;
4538 nocode_wanted
= saved_nocode_wanted
;
4541 /* parse a unary expression and return its type without any side
4543 static void unary_type(CType
*type
)
4555 /* parse a constant expression and return value in vtop. */
4556 static void expr_const1(void)
4565 /* parse an integer constant and return its value. */
4566 ST_FUNC
int expr_const(void)
4570 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
4571 expect("constant expression");
4577 /* return the label token if current token is a label, otherwise
4579 static int is_label(void)
4583 /* fast test first */
4584 if (tok
< TOK_UIDENT
)
4586 /* no need to save tokc because tok is an identifier */
4593 unget_tok(last_tok
);
4598 static void label_or_decl(int l
)
4602 /* fast test first */
4603 if (tok
>= TOK_UIDENT
)
4605 /* no need to save tokc because tok is an identifier */
4609 unget_tok(last_tok
);
4612 unget_tok(last_tok
);
4617 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
4618 int case_reg
, int is_expr
)
4621 Sym
*s
, *frame_bottom
;
4623 /* generate line number info */
4624 if (tcc_state
->do_debug
&&
4625 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
4626 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
4628 last_line_num
= file
->line_num
;
4632 /* default return value is (void) */
4634 vtop
->type
.t
= VT_VOID
;
4637 if (tok
== TOK_IF
) {
4644 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4646 if (c
== TOK_ELSE
) {
4650 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4651 gsym(d
); /* patch else jmp */
4654 } else if (tok
== TOK_WHILE
) {
4662 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4666 } else if (tok
== '{') {
4668 int block_vla_sp_loc
, *saved_vla_sp_loc
, saved_vla_flags
;
4671 /* record local declaration stack position */
4673 frame_bottom
= sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4674 frame_bottom
->next
= scope_stack_bottom
;
4675 scope_stack_bottom
= frame_bottom
;
4676 llabel
= local_label_stack
;
4678 /* save VLA state */
4679 block_vla_sp_loc
= *(saved_vla_sp_loc
= vla_sp_loc
);
4680 if (saved_vla_sp_loc
!= &vla_sp_root_loc
)
4681 vla_sp_loc
= &block_vla_sp_loc
;
4683 saved_vla_flags
= vla_flags
;
4684 vla_flags
|= VLA_NEED_NEW_FRAME
;
4686 /* handle local labels declarations */
4687 if (tok
== TOK_LABEL
) {
4690 if (tok
< TOK_UIDENT
)
4691 expect("label identifier");
4692 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
4702 while (tok
!= '}') {
4703 label_or_decl(VT_LOCAL
);
4707 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4710 /* pop locally defined labels */
4711 label_pop(&local_label_stack
, llabel
);
4713 /* XXX: this solution makes only valgrind happy...
4714 triggered by gcc.c-torture/execute/20000917-1.c */
4716 switch(vtop
->type
.t
& VT_BTYPE
) {
4721 for(p
=vtop
->type
.ref
;p
;p
=p
->prev
)
4723 tcc_error("unsupported expression type");
4726 /* pop locally defined symbols */
4727 scope_stack_bottom
= scope_stack_bottom
->next
;
4728 sym_pop(&local_stack
, s
);
4730 /* Pop VLA frames and restore stack pointer if required */
4731 if (saved_vla_sp_loc
!= &vla_sp_root_loc
)
4732 *saved_vla_sp_loc
= block_vla_sp_loc
;
4733 if (vla_sp_loc
!= (saved_vla_sp_loc
== &vla_sp_root_loc
? &vla_sp_root_loc
: &block_vla_sp_loc
)) {
4734 vla_sp_loc
= saved_vla_sp_loc
;
4735 gen_vla_sp_restore(*vla_sp_loc
);
4737 vla_flags
= (vla_flags
& ~VLA_SCOPE_FLAGS
) | (saved_vla_flags
& VLA_SCOPE_FLAGS
);
4740 } else if (tok
== TOK_RETURN
) {
4744 gen_assign_cast(&func_vt
);
4745 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
4746 CType type
, ret_type
;
4747 int ret_align
, ret_nregs
;
4748 ret_nregs
= gfunc_sret(&func_vt
, func_var
, &ret_type
,
4750 if (0 == ret_nregs
) {
4751 /* if returning structure, must copy it to implicit
4752 first pointer arg location */
4755 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
4758 /* copy structure value to pointer */
4761 /* returning structure packed into registers */
4762 int r
, size
, addr
, align
;
4763 size
= type_size(&func_vt
,&align
);
4764 if ((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) || (vtop
->c
.i
& (ret_align
-1)))
4765 && (align
& (ret_align
-1))) {
4766 loc
= (loc
- size
) & -align
;
4769 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
4772 vset(&ret_type
, VT_LOCAL
| VT_LVAL
, addr
);
4774 vtop
->type
= ret_type
;
4775 if (is_float(ret_type
.t
))
4776 r
= rc_fret(ret_type
.t
);
4782 if (--ret_nregs
== 0)
4784 /* We assume that when a structure is returned in multiple
4785 registers, their classes are consecutive values of the
4788 /* XXX: compatible with arm only: ret_align == register_size */
4789 vtop
->c
.i
+= ret_align
;
4790 vtop
->r
= VT_LOCAL
| VT_LVAL
;
4793 } else if (is_float(func_vt
.t
)) {
4794 gv(rc_fret(func_vt
.t
));
4798 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
4801 rsym
= gjmp(rsym
); /* jmp */
4802 } else if (tok
== TOK_BREAK
) {
4805 tcc_error("cannot break");
4806 *bsym
= gjmp(*bsym
);
4809 } else if (tok
== TOK_CONTINUE
) {
4812 tcc_error("cannot continue");
4813 *csym
= gjmp(*csym
);
4816 } else if (tok
== TOK_FOR
) {
4821 frame_bottom
= sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4822 frame_bottom
->next
= scope_stack_bottom
;
4823 scope_stack_bottom
= frame_bottom
;
4825 /* c99 for-loop init decl? */
4826 if (!decl0(VT_LOCAL
, 1)) {
4827 /* no, regular for-loop init expr */
4851 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4855 scope_stack_bottom
= scope_stack_bottom
->next
;
4856 sym_pop(&local_stack
, s
);
4858 if (tok
== TOK_DO
) {
4863 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4874 if (tok
== TOK_SWITCH
) {
4878 /* XXX: other types than integer */
4879 case_reg
= gv(RC_INT
);
4883 b
= gjmp(0); /* jump to first case */
4885 block(&a
, csym
, &b
, &c
, case_reg
, 0);
4886 /* if no default, jmp after switch */
4894 if (tok
== TOK_CASE
) {
4901 if (gnu_ext
&& tok
== TOK_DOTS
) {
4905 tcc_warning("empty case range");
4907 /* since a case is like a label, we must skip it with a jmp */
4914 *case_sym
= gtst(1, 0);
4917 *case_sym
= gtst(1, 0);
4921 *case_sym
= gtst(1, *case_sym
);
4926 goto block_after_label
;
4928 if (tok
== TOK_DEFAULT
) {
4934 tcc_error("too many 'default'");
4937 goto block_after_label
;
4939 if (tok
== TOK_GOTO
) {
4941 if (tok
== '*' && gnu_ext
) {
4945 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
4948 } else if (tok
>= TOK_UIDENT
) {
4949 s
= label_find(tok
);
4950 /* put forward definition if needed */
4952 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4954 if (s
->r
== LABEL_DECLARED
)
4955 s
->r
= LABEL_FORWARD
;
4957 /* label already defined */
4958 if (vla_flags
& VLA_IN_SCOPE
) {
4959 /* If VLAs are in use, save the current stack pointer and
4960 reset the stack pointer to what it was at function entry
4961 (label will restore stack pointer in inner scopes) */
4963 gen_vla_sp_restore(vla_sp_root_loc
);
4965 if (s
->r
& LABEL_FORWARD
)
4966 s
->jnext
= gjmp(s
->jnext
);
4968 gjmp_addr(s
->jnext
);
4971 expect("label identifier");
4974 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
4980 if (vla_flags
& VLA_IN_SCOPE
) {
4981 /* save/restore stack pointer across label
4982 this is a no-op when combined with the load immediately
4983 after the label unless we arrive via goto */
4988 if (s
->r
== LABEL_DEFINED
)
4989 tcc_error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
4991 s
->r
= LABEL_DEFINED
;
4993 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
4996 if (vla_flags
& VLA_IN_SCOPE
) {
4997 gen_vla_sp_restore(*vla_sp_loc
);
4998 vla_flags
|= VLA_NEED_NEW_FRAME
;
5000 /* we accept this, but it is a mistake */
5003 tcc_warning("deprecated use of label at end of compound statement");
5007 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
5010 /* expression case */
5025 /* t is the array or struct type. c is the array or struct
5026 address. cur_index/cur_field is the pointer to the current
5027 value. 'size_only' is true if only size info is needed (only used
5029 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
5030 int *cur_index
, Sym
**cur_field
,
5034 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
5040 if (gnu_ext
&& (l
= is_label()) != 0)
5042 while (tok
== '[' || tok
== '.') {
5044 if (!(type
->t
& VT_ARRAY
))
5045 expect("array type");
5048 index
= expr_const();
5049 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
5050 expect("invalid index");
5051 if (tok
== TOK_DOTS
&& gnu_ext
) {
5053 index_last
= expr_const();
5054 if (index_last
< 0 ||
5055 (s
->c
>= 0 && index_last
>= s
->c
) ||
5057 expect("invalid index");
5063 *cur_index
= index_last
;
5064 type
= pointed_type(type
);
5065 elem_size
= type_size(type
, &align
);
5066 c
+= index
* elem_size
;
5067 /* NOTE: we only support ranges for last designator */
5068 nb_elems
= index_last
- index
+ 1;
5069 if (nb_elems
!= 1) {
5078 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
5079 expect("struct/union type");
5092 /* XXX: fix this mess by using explicit storage field */
5094 type1
.t
|= (type
->t
& ~VT_TYPE
);
5108 if (type
->t
& VT_ARRAY
) {
5110 type
= pointed_type(type
);
5111 c
+= index
* type_size(type
, &align
);
5115 tcc_error("too many field init");
5116 /* XXX: fix this mess by using explicit storage field */
5118 type1
.t
|= (type
->t
& ~VT_TYPE
);
5123 decl_initializer(type
, sec
, c
, 0, size_only
);
5125 /* XXX: make it more general */
5126 if (!size_only
&& nb_elems
> 1) {
5127 unsigned long c_end
;
5132 tcc_error("range init not supported yet for dynamic storage");
5133 c_end
= c
+ nb_elems
* elem_size
;
5134 if (c_end
> sec
->data_allocated
)
5135 section_realloc(sec
, c_end
);
5136 src
= sec
->data
+ c
;
5138 for(i
= 1; i
< nb_elems
; i
++) {
5140 memcpy(dst
, src
, elem_size
);
5146 #define EXPR_CONST 1
5149 /* store a value or an expression directly in global data or in local array */
5150 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
5151 int v
, int expr_type
)
5153 int saved_global_expr
, bt
, bit_pos
, bit_size
;
5155 unsigned long long bit_mask
;
5163 /* compound literals must be allocated globally in this case */
5164 saved_global_expr
= global_expr
;
5167 global_expr
= saved_global_expr
;
5168 /* NOTE: symbols are accepted */
5169 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
5170 tcc_error("initializer element is not constant");
5178 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
5181 /* XXX: not portable */
5182 /* XXX: generate error if incorrect relocation */
5183 gen_assign_cast(&dtype
);
5184 bt
= type
->t
& VT_BTYPE
;
5185 /* we'll write at most 12 bytes */
5186 if (c
+ 12 > sec
->data_allocated
) {
5187 section_realloc(sec
, c
+ 12);
5189 ptr
= sec
->data
+ c
;
5190 /* XXX: make code faster ? */
5191 if (!(type
->t
& VT_BITFIELD
)) {
5196 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5197 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
5198 bit_mask
= (1LL << bit_size
) - 1;
5200 if ((vtop
->r
& VT_SYM
) &&
5206 (bt
== VT_INT
&& bit_size
!= 32)))
5207 tcc_error("initializer element is not computable at load time");
5210 vtop
->c
.i
= (vtop
->c
.i
!= 0);
5212 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5215 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5218 *(double *)ptr
= vtop
->c
.d
;
5221 *(long double *)ptr
= vtop
->c
.ld
;
5224 *(long long *)ptr
|= (vtop
->c
.ll
& bit_mask
) << bit_pos
;
5227 if (vtop
->r
& VT_SYM
) {
5228 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
5230 *(addr_t
*)ptr
|= (vtop
->c
.ptr_offset
& bit_mask
) << bit_pos
;
5233 if (vtop
->r
& VT_SYM
) {
5234 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
5236 *(int *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5241 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
5248 /* put zeros for variable based init */
5249 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
5252 /* nothing to do because globals are already set to zero */
5254 vpush_global_sym(&func_old_type
, TOK_memset
);
5256 #ifdef TCC_TARGET_ARM
5267 /* 't' contains the type and storage info. 'c' is the offset of the
5268 object in section 'sec'. If 'sec' is NULL, it means stack based
5269 allocation. 'first' is true if array '{' must be read (multi
5270 dimension implicit array init handling). 'size_only' is true if
5271 size only evaluation is wanted (only for arrays). */
5272 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
5273 int first
, int size_only
)
5275 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, parlevel1
, i
;
5276 int size1
, align1
, expr_type
;
5280 if (type
->t
& VT_VLA
) {
5283 /* save current stack pointer */
5284 if (vla_flags
& VLA_NEED_NEW_FRAME
) {
5286 vla_flags
= VLA_IN_SCOPE
;
5287 vla_sp_loc
= &vla_sp_loc_tmp
;
5290 vla_runtime_type_size(type
, &a
);
5291 gen_vla_alloc(type
, a
);
5292 vset(type
, VT_LOCAL
|VT_LVAL
, c
);
5296 } else if (type
->t
& VT_ARRAY
) {
5300 t1
= pointed_type(type
);
5301 size1
= type_size(t1
, &align1
);
5304 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
5307 tcc_error("character array initializer must be a literal,"
5308 " optionally enclosed in braces");
5313 /* only parse strings here if correct type (otherwise: handle
5314 them as ((w)char *) expressions */
5315 if ((tok
== TOK_LSTR
&&
5316 #ifdef TCC_TARGET_PE
5317 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
5319 (t1
->t
& VT_BTYPE
) == VT_INT
5321 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
5322 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5327 /* compute maximum number of chars wanted */
5329 cstr_len
= cstr
->size
;
5331 cstr_len
= cstr
->size
/ sizeof(nwchar_t
);
5334 if (n
>= 0 && nb
> (n
- array_length
))
5335 nb
= n
- array_length
;
5338 tcc_warning("initializer-string for array is too long");
5339 /* in order to go faster for common case (char
5340 string in global variable, we handle it
5342 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
5343 memcpy(sec
->data
+ c
+ array_length
, cstr
->data
, nb
);
5347 ch
= ((unsigned char *)cstr
->data
)[i
];
5349 ch
= ((nwchar_t
*)cstr
->data
)[i
];
5350 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
5358 /* only add trailing zero if enough storage (no
5359 warning in this case since it is standard) */
5360 if (n
< 0 || array_length
< n
) {
5362 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
5368 while (tok
!= '}') {
5369 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
5370 if (n
>= 0 && index
>= n
)
5371 tcc_error("index too large");
5372 /* must put zero in holes (note that doing it that way
5373 ensures that it even works with designators) */
5374 if (!size_only
&& array_length
< index
) {
5375 init_putz(t1
, sec
, c
+ array_length
* size1
,
5376 (index
- array_length
) * size1
);
5379 if (index
> array_length
)
5380 array_length
= index
;
5381 /* special test for multi dimensional arrays (may not
5382 be strictly correct if designators are used at the
5384 if (index
>= n
&& no_oblock
)
5393 /* put zeros at the end */
5394 if (!size_only
&& n
>= 0 && array_length
< n
) {
5395 init_putz(t1
, sec
, c
+ array_length
* size1
,
5396 (n
- array_length
) * size1
);
5398 /* patch type size if needed */
5400 s
->c
= array_length
;
5401 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
5402 (sec
|| !first
|| tok
== '{')) {
5405 /* NOTE: the previous test is a specific case for automatic
5406 struct/union init */
5407 /* XXX: union needs only one init */
5409 /* XXX: this test is incorrect for local initializers
5410 beginning with ( without {. It would be much more difficult
5411 to do it correctly (ideally, the expression parser should
5412 be used in all cases) */
5418 while (tok
== '(') {
5422 if (!parse_btype(&type1
, &ad1
))
5424 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
5426 if (!is_assignable_types(type
, &type1
))
5427 tcc_error("invalid type for cast");
5432 if (first
|| tok
== '{') {
5441 while (tok
!= '}') {
5442 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
5444 if (!size_only
&& array_length
< index
) {
5445 init_putz(type
, sec
, c
+ array_length
,
5446 index
- array_length
);
5448 index
= index
+ type_size(&f
->type
, &align1
);
5449 if (index
> array_length
)
5450 array_length
= index
;
5452 /* gr: skip fields from same union - ugly. */
5454 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
5455 /* test for same offset */
5456 if (f
->next
->c
!= f
->c
)
5458 /* if yes, test for bitfield shift */
5459 if ((f
->type
.t
& VT_BITFIELD
) && (f
->next
->type
.t
& VT_BITFIELD
)) {
5460 int bit_pos_1
= (f
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5461 int bit_pos_2
= (f
->next
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5462 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
5463 if (bit_pos_1
!= bit_pos_2
)
5470 if (no_oblock
&& f
== NULL
)
5476 /* put zeros at the end */
5477 if (!size_only
&& array_length
< n
) {
5478 init_putz(type
, sec
, c
+ array_length
,
5487 } else if (tok
== '{') {
5489 decl_initializer(type
, sec
, c
, first
, size_only
);
5491 } else if (size_only
) {
5492 /* just skip expression */
5493 parlevel
= parlevel1
= 0;
5494 while ((parlevel
> 0 || parlevel1
> 0 ||
5495 (tok
!= '}' && tok
!= ',')) && tok
!= -1) {
5498 else if (tok
== ')')
5500 else if (tok
== '{')
5502 else if (tok
== '}')
5507 /* currently, we always use constant expression for globals
5508 (may change for scripting case) */
5509 expr_type
= EXPR_CONST
;
5511 expr_type
= EXPR_ANY
;
5512 init_putv(type
, sec
, c
, 0, expr_type
);
5516 /* parse an initializer for type 't' if 'has_init' is non zero, and
5517 allocate space in local or global data space ('r' is either
5518 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5519 variable 'v' with an associated name represented by 'asm_label' of
5520 scope 'scope' is declared before initializers are parsed. If 'v' is
5521 zero, then a reference to the new object is put in the value stack.
5522 If 'has_init' is 2, a special parsing is done to handle string
5524 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
5525 int has_init
, int v
, char *asm_label
,
5528 int size
, align
, addr
, data_offset
;
5530 ParseState saved_parse_state
= {0};
5531 TokenString init_str
;
5533 Sym
*flexible_array
;
5535 flexible_array
= NULL
;
5536 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
5537 Sym
*field
= type
->ref
->next
;
5540 field
= field
->next
;
5541 if (field
->type
.t
& VT_ARRAY
&& field
->type
.ref
->c
< 0)
5542 flexible_array
= field
;
5546 size
= type_size(type
, &align
);
5547 /* If unknown size, we must evaluate it before
5548 evaluating initializers because
5549 initializers can generate global data too
5550 (e.g. string pointers or ISOC99 compound
5551 literals). It also simplifies local
5552 initializers handling */
5553 tok_str_new(&init_str
);
5554 if (size
< 0 || (flexible_array
&& has_init
)) {
5556 tcc_error("unknown type size");
5557 /* get all init string */
5558 if (has_init
== 2) {
5559 /* only get strings */
5560 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5561 tok_str_add_tok(&init_str
);
5566 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
5568 tcc_error("unexpected end of file in initializer");
5569 tok_str_add_tok(&init_str
);
5572 else if (tok
== '}') {
5582 tok_str_add(&init_str
, -1);
5583 tok_str_add(&init_str
, 0);
5586 save_parse_state(&saved_parse_state
);
5588 macro_ptr
= init_str
.str
;
5590 decl_initializer(type
, NULL
, 0, 1, 1);
5591 /* prepare second initializer parsing */
5592 macro_ptr
= init_str
.str
;
5595 /* if still unknown size, error */
5596 size
= type_size(type
, &align
);
5598 tcc_error("unknown type size");
5601 size
+= flexible_array
->type
.ref
->c
* pointed_size(&flexible_array
->type
);
5602 /* take into account specified alignment if bigger */
5603 if (ad
->a
.aligned
) {
5604 if (ad
->a
.aligned
> align
)
5605 align
= ad
->a
.aligned
;
5606 } else if (ad
->a
.packed
) {
5609 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
5611 #ifdef CONFIG_TCC_BCHECK
5612 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5616 loc
= (loc
- size
) & -align
;
5618 #ifdef CONFIG_TCC_BCHECK
5619 /* handles bounds */
5620 /* XXX: currently, since we do only one pass, we cannot track
5621 '&' operators, so we add only arrays */
5622 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5623 unsigned long *bounds_ptr
;
5624 /* add padding between regions */
5626 /* then add local bound info */
5627 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(unsigned long));
5628 bounds_ptr
[0] = addr
;
5629 bounds_ptr
[1] = size
;
5633 /* local variable */
5634 sym_push(v
, type
, r
, addr
);
5636 /* push local reference */
5637 vset(type
, r
, addr
);
5643 if (v
&& scope
== VT_CONST
) {
5644 /* see if the symbol was already defined */
5647 if (!is_compatible_types(&sym
->type
, type
))
5648 tcc_error("incompatible types for redefinition of '%s'",
5649 get_tok_str(v
, NULL
));
5650 if (sym
->type
.t
& VT_EXTERN
) {
5651 /* if the variable is extern, it was not allocated */
5652 sym
->type
.t
&= ~VT_EXTERN
;
5653 /* set array size if it was omitted in extern
5655 if ((sym
->type
.t
& VT_ARRAY
) &&
5656 sym
->type
.ref
->c
< 0 &&
5658 sym
->type
.ref
->c
= type
->ref
->c
;
5660 /* we accept several definitions of the same
5661 global variable. this is tricky, because we
5662 must play with the SHN_COMMON type of the symbol */
5663 /* XXX: should check if the variable was already
5664 initialized. It is incorrect to initialized it
5666 /* no init data, we won't add more to the symbol */
5673 /* allocate symbol in corresponding section */
5678 else if (tcc_state
->nocommon
)
5682 data_offset
= sec
->data_offset
;
5683 data_offset
= (data_offset
+ align
- 1) & -align
;
5685 /* very important to increment global pointer at this time
5686 because initializers themselves can create new initializers */
5687 data_offset
+= size
;
5688 #ifdef CONFIG_TCC_BCHECK
5689 /* add padding if bound check */
5690 if (tcc_state
->do_bounds_check
)
5693 sec
->data_offset
= data_offset
;
5694 /* allocate section space to put the data */
5695 if (sec
->sh_type
!= SHT_NOBITS
&&
5696 data_offset
> sec
->data_allocated
)
5697 section_realloc(sec
, data_offset
);
5698 /* align section if needed */
5699 if (align
> sec
->sh_addralign
)
5700 sec
->sh_addralign
= align
;
5702 addr
= 0; /* avoid warning */
5706 if (scope
!= VT_CONST
|| !sym
) {
5707 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
5708 sym
->asm_label
= asm_label
;
5710 /* update symbol definition */
5712 put_extern_sym(sym
, sec
, addr
, size
);
5715 /* put a common area */
5716 put_extern_sym(sym
, NULL
, align
, size
);
5717 /* XXX: find a nicer way */
5718 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
5719 esym
->st_shndx
= SHN_COMMON
;
5722 /* push global reference */
5723 sym
= get_sym_ref(type
, sec
, addr
, size
);
5724 vpushsym(type
, sym
);
5726 /* patch symbol weakness */
5727 if (type
->t
& VT_WEAK
)
5729 apply_visibility(sym
, type
);
5730 #ifdef CONFIG_TCC_BCHECK
5731 /* handles bounds now because the symbol must be defined
5732 before for the relocation */
5733 if (tcc_state
->do_bounds_check
) {
5734 unsigned long *bounds_ptr
;
5736 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
5737 /* then add global bound info */
5738 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(long));
5739 bounds_ptr
[0] = 0; /* relocated */
5740 bounds_ptr
[1] = size
;
5744 if (has_init
|| (type
->t
& VT_VLA
)) {
5745 decl_initializer(type
, sec
, addr
, 1, 0);
5746 /* restore parse state if needed */
5748 tok_str_free(init_str
.str
);
5749 restore_parse_state(&saved_parse_state
);
5751 /* patch flexible array member size back to -1, */
5752 /* for possible subsequent similar declarations */
5754 flexible_array
->type
.ref
->c
= -1;
5759 static void put_func_debug(Sym
*sym
)
5764 /* XXX: we put here a dummy type */
5765 snprintf(buf
, sizeof(buf
), "%s:%c1",
5766 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
5767 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
5768 cur_text_section
, sym
->c
);
5769 /* //gr gdb wants a line at the function */
5770 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
5775 /* parse an old style function declaration list */
5776 /* XXX: check multiple parameter */
5777 static void func_decl_list(Sym
*func_sym
)
5784 /* parse each declaration */
5785 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
&&
5786 tok
!= TOK_ASM1
&& tok
!= TOK_ASM2
&& tok
!= TOK_ASM3
) {
5787 if (!parse_btype(&btype
, &ad
))
5788 expect("declaration list");
5789 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5790 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5792 /* we accept no variable after */
5796 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5797 /* find parameter in function parameter list */
5800 if ((s
->v
& ~SYM_FIELD
) == v
)
5804 tcc_error("declaration for parameter '%s' but no such parameter",
5805 get_tok_str(v
, NULL
));
5807 /* check that no storage specifier except 'register' was given */
5808 if (type
.t
& VT_STORAGE
)
5809 tcc_error("storage class specified for '%s'", get_tok_str(v
, NULL
));
5810 convert_parameter_type(&type
);
5811 /* we can add the type (NOTE: it could be local to the function) */
5813 /* accept other parameters */
5824 /* parse a function defined by symbol 'sym' and generate its code in
5825 'cur_text_section' */
5826 static void gen_function(Sym
*sym
)
5828 int saved_nocode_wanted
= nocode_wanted
;
5830 ind
= cur_text_section
->data_offset
;
5831 /* NOTE: we patch the symbol size later */
5832 put_extern_sym(sym
, cur_text_section
, ind
, 0);
5833 funcname
= get_tok_str(sym
->v
, NULL
);
5835 /* Initialize VLA state */
5836 vla_sp_loc
= &vla_sp_root_loc
;
5837 vla_flags
= VLA_NEED_NEW_FRAME
;
5838 /* put debug symbol */
5839 if (tcc_state
->do_debug
)
5840 put_func_debug(sym
);
5841 /* push a dummy symbol to enable local sym storage */
5842 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
5843 gfunc_prolog(&sym
->type
);
5844 #ifdef CONFIG_TCC_BCHECK
5845 if (tcc_state
->do_bounds_check
5846 && !strcmp(get_tok_str(sym
->v
, NULL
), "main")) {
5850 for (i
= 0, sym
= local_stack
; i
< 2; i
++, sym
= sym
->prev
) {
5851 if (sym
->v
& SYM_FIELD
|| sym
->prev
->v
& SYM_FIELD
)
5853 vpush_global_sym(&func_old_type
, TOK___bound_main_arg
);
5854 vset(&sym
->type
, sym
->r
, sym
->c
);
5860 block(NULL
, NULL
, NULL
, NULL
, 0, 0);
5863 cur_text_section
->data_offset
= ind
;
5864 label_pop(&global_label_stack
, NULL
);
5865 /* reset local stack */
5866 scope_stack_bottom
= NULL
;
5867 sym_pop(&local_stack
, NULL
);
5868 /* end of function */
5869 /* patch symbol size */
5870 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
5872 /* patch symbol weakness (this definition overrules any prototype) */
5873 if (sym
->type
.t
& VT_WEAK
)
5875 apply_visibility(sym
, &sym
->type
);
5876 if (tcc_state
->do_debug
) {
5877 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
5879 /* It's better to crash than to generate wrong code */
5880 cur_text_section
= NULL
;
5881 funcname
= ""; /* for safety */
5882 func_vt
.t
= VT_VOID
; /* for safety */
5883 func_var
= 0; /* for safety */
5884 ind
= 0; /* for safety */
5885 nocode_wanted
= saved_nocode_wanted
;
5888 ST_FUNC
void gen_inline_functions(void)
5891 int *str
, inline_generated
, i
;
5892 struct InlineFunc
*fn
;
5894 /* iterate while inline function are referenced */
5896 inline_generated
= 0;
5897 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5898 fn
= tcc_state
->inline_fns
[i
];
5900 if (sym
&& sym
->c
) {
5901 /* the function was used: generate its code and
5902 convert it to a normal function */
5903 str
= fn
->token_str
;
5906 pstrcpy(file
->filename
, sizeof file
->filename
, fn
->filename
);
5907 sym
->r
= VT_SYM
| VT_CONST
;
5908 sym
->type
.t
&= ~VT_INLINE
;
5912 cur_text_section
= text_section
;
5914 macro_ptr
= NULL
; /* fail safe */
5916 inline_generated
= 1;
5919 if (!inline_generated
)
5922 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5923 fn
= tcc_state
->inline_fns
[i
];
5924 str
= fn
->token_str
;
5927 dynarray_reset(&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
);
5930 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
5931 static int decl0(int l
, int is_for_loop_init
)
5939 if (!parse_btype(&btype
, &ad
)) {
5940 if (is_for_loop_init
)
5942 /* skip redundant ';' */
5943 /* XXX: find more elegant solution */
5948 if (l
== VT_CONST
&&
5949 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5950 /* global asm block */
5954 /* special test for old K&R protos without explicit int
5955 type. Only accepted when defining global data */
5956 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
5960 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5961 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5963 /* we accept no variable after */
5967 while (1) { /* iterate thru each declaration */
5968 char *asm_label
; // associated asm label
5970 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5974 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
5975 printf("type = '%s'\n", buf
);
5978 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5979 if ((type
.t
& VT_STATIC
) && (l
== VT_LOCAL
)) {
5980 tcc_error("function without file scope cannot be static");
5982 /* if old style function prototype, we accept a
5985 if (sym
->c
== FUNC_OLD
)
5986 func_decl_list(sym
);
5990 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5993 asm_label_instr(&astr
);
5994 asm_label
= tcc_strdup(astr
.data
);
5997 /* parse one last attribute list, after asm label */
5998 parse_attribute(&ad
);
6003 #ifdef TCC_TARGET_PE
6004 if (ad
.a
.func_import
)
6005 type
.t
|= VT_IMPORT
;
6006 if (ad
.a
.func_export
)
6007 type
.t
|= VT_EXPORT
;
6009 type
.t
|= ad
.a
.visibility
<< VT_VIS_SHIFT
;
6013 tcc_error("cannot use local functions");
6014 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
6015 expect("function definition");
6017 /* reject abstract declarators in function definition */
6019 while ((sym
= sym
->next
) != NULL
)
6020 if (!(sym
->v
& ~SYM_FIELD
))
6021 expect("identifier");
6023 /* XXX: cannot do better now: convert extern line to static inline */
6024 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
6025 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
6030 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
6033 ref
= sym
->type
.ref
;
6034 if (0 == ref
->a
.func_proto
)
6035 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
6037 /* use func_call from prototype if not defined */
6038 if (ref
->a
.func_call
!= FUNC_CDECL
6039 && type
.ref
->a
.func_call
== FUNC_CDECL
)
6040 type
.ref
->a
.func_call
= ref
->a
.func_call
;
6042 /* use export from prototype */
6043 if (ref
->a
.func_export
)
6044 type
.ref
->a
.func_export
= 1;
6046 /* use static from prototype */
6047 if (sym
->type
.t
& VT_STATIC
)
6048 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
6050 /* If the definition has no visibility use the
6051 one from prototype. */
6052 if (! (type
.t
& VT_VIS_MASK
))
6053 type
.t
|= sym
->type
.t
& VT_VIS_MASK
;
6055 if (!is_compatible_types(&sym
->type
, &type
)) {
6057 tcc_error("incompatible types for redefinition of '%s'",
6058 get_tok_str(v
, NULL
));
6060 type
.ref
->a
.func_proto
= 0;
6061 /* if symbol is already defined, then put complete type */
6064 /* put function symbol */
6065 sym
= global_identifier_push(v
, type
.t
, 0);
6066 sym
->type
.ref
= type
.ref
;
6069 /* static inline functions are just recorded as a kind
6070 of macro. Their code will be emitted at the end of
6071 the compilation unit only if they are used */
6072 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
6073 (VT_INLINE
| VT_STATIC
)) {
6074 TokenString func_str
;
6076 struct InlineFunc
*fn
;
6077 const char *filename
;
6079 tok_str_new(&func_str
);
6085 tcc_error("unexpected end of file");
6086 tok_str_add_tok(&func_str
);
6091 } else if (t
== '}') {
6093 if (block_level
== 0)
6097 tok_str_add(&func_str
, -1);
6098 tok_str_add(&func_str
, 0);
6099 filename
= file
? file
->filename
: "";
6100 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
6101 strcpy(fn
->filename
, filename
);
6103 fn
->token_str
= func_str
.str
;
6104 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
6107 /* compute text section */
6108 cur_text_section
= ad
.section
;
6109 if (!cur_text_section
)
6110 cur_text_section
= text_section
;
6111 sym
->r
= VT_SYM
| VT_CONST
;
6116 if (btype
.t
& VT_TYPEDEF
) {
6117 /* save typedefed type */
6118 /* XXX: test storage specifiers ? */
6119 sym
= sym_push(v
, &type
, 0, 0);
6121 sym
->type
.t
|= VT_TYPEDEF
;
6124 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
6125 /* external function definition */
6126 /* specific case for func_call attribute */
6127 ad
.a
.func_proto
= 1;
6129 } else if (!(type
.t
& VT_ARRAY
)) {
6130 /* not lvalue if array */
6131 r
|= lvalue_type(type
.t
);
6133 has_init
= (tok
== '=');
6134 if (has_init
&& (type
.t
& VT_VLA
))
6135 tcc_error("Variable length array cannot be initialized");
6136 if ((btype
.t
& VT_EXTERN
) || ((type
.t
& VT_BTYPE
) == VT_FUNC
) ||
6137 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
6138 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
6139 /* external variable or function */
6140 /* NOTE: as GCC, uninitialized global static
6141 arrays of null size are considered as
6143 sym
= external_sym(v
, &type
, r
, asm_label
);
6145 if (ad
.alias_target
) {
6150 alias_target
= sym_find(ad
.alias_target
);
6151 if (!alias_target
|| !alias_target
->c
)
6152 tcc_error("unsupported forward __alias__ attribute");
6153 esym
= &((Elf32_Sym
*)symtab_section
->data
)[alias_target
->c
];
6154 tsec
.sh_num
= esym
->st_shndx
;
6155 put_extern_sym2(sym
, &tsec
, esym
->st_value
, esym
->st_size
, 0);
6158 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
6159 if (type
.t
& VT_STATIC
)
6165 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, asm_label
, l
);
6169 if (is_for_loop_init
)
6182 ST_FUNC
void decl(int l
)