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
);
93 ST_FUNC
void vpush64(int ty
, unsigned long long v
);
94 ST_FUNC
void vpush(CType
*type
);
95 ST_FUNC
int gvtst(int inv
, int t
);
96 ST_FUNC
int is_btype_size(int bt
);
98 ST_INLN
int is_float(int t
)
102 return bt
== VT_LDOUBLE
|| bt
== VT_DOUBLE
|| bt
== VT_FLOAT
|| bt
== VT_QFLOAT
;
105 /* we use our own 'finite' function to avoid potential problems with
106 non standard math libs */
107 /* XXX: endianness dependent */
108 ST_FUNC
int ieee_finite(double d
)
111 memcpy(p
, &d
, sizeof(double));
112 return ((unsigned)((p
[1] | 0x800fffff) + 1)) >> 31;
115 ST_FUNC
void test_lvalue(void)
117 if (!(vtop
->r
& VT_LVAL
))
121 /* ------------------------------------------------------------------------- */
122 /* symbol allocator */
123 static Sym
*__sym_malloc(void)
125 Sym
*sym_pool
, *sym
, *last_sym
;
128 sym_pool
= tcc_malloc(SYM_POOL_NB
* sizeof(Sym
));
129 dynarray_add(&sym_pools
, &nb_sym_pools
, sym_pool
);
131 last_sym
= sym_free_first
;
133 for(i
= 0; i
< SYM_POOL_NB
; i
++) {
134 sym
->next
= last_sym
;
138 sym_free_first
= last_sym
;
142 static inline Sym
*sym_malloc(void)
145 sym
= sym_free_first
;
147 sym
= __sym_malloc();
148 sym_free_first
= sym
->next
;
152 ST_INLN
void sym_free(Sym
*sym
)
154 sym
->next
= sym_free_first
;
155 tcc_free(sym
->asm_label
);
156 sym_free_first
= sym
;
159 /* push, without hashing */
160 ST_FUNC Sym
*sym_push2(Sym
**ps
, int v
, int t
, long c
)
163 if (ps
== &local_stack
) {
164 for (s
= *ps
; s
&& s
!= scope_stack_bottom
; s
= s
->prev
)
165 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
&& s
->v
== v
)
166 tcc_error("incompatible types for redefinition of '%s'",
167 get_tok_str(v
, NULL
));
185 /* find a symbol and return its associated structure. 's' is the top
186 of the symbol stack */
187 ST_FUNC Sym
*sym_find2(Sym
*s
, int v
)
199 /* structure lookup */
200 ST_INLN Sym
*struct_find(int v
)
203 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
205 return table_ident
[v
]->sym_struct
;
208 /* find an identifier */
209 ST_INLN Sym
*sym_find(int v
)
212 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
214 return table_ident
[v
]->sym_identifier
;
217 /* push a given symbol on the symbol stack */
218 ST_FUNC Sym
*sym_push(int v
, CType
*type
, int r
, int c
)
227 s
= sym_push2(ps
, v
, type
->t
, c
);
228 s
->type
.ref
= type
->ref
;
230 /* don't record fields or anonymous symbols */
232 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
233 /* record symbol in token array */
234 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
236 ps
= &ts
->sym_struct
;
238 ps
= &ts
->sym_identifier
;
245 /* push a global identifier */
246 ST_FUNC Sym
*global_identifier_push(int v
, int t
, int c
)
249 s
= sym_push2(&global_stack
, v
, t
, c
);
250 /* don't record anonymous symbol */
251 if (v
< SYM_FIRST_ANOM
) {
252 ps
= &table_ident
[v
- TOK_IDENT
]->sym_identifier
;
253 /* modify the top most local identifier, so that
254 sym_identifier will point to 's' when popped */
256 ps
= &(*ps
)->prev_tok
;
263 /* pop symbols until top reaches 'b' */
264 ST_FUNC
void sym_pop(Sym
**ptop
, Sym
*b
)
274 /* remove symbol in token array */
276 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
277 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
279 ps
= &ts
->sym_struct
;
281 ps
= &ts
->sym_identifier
;
290 static void weaken_symbol(Sym
*sym
)
292 sym
->type
.t
|= VT_WEAK
;
297 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
298 esym_type
= ELFW(ST_TYPE
)(esym
->st_info
);
299 esym
->st_info
= ELFW(ST_INFO
)(STB_WEAK
, esym_type
);
303 static void apply_visibility(Sym
*sym
, CType
*type
)
305 int vis
= sym
->type
.t
& VT_VIS_MASK
;
306 int vis2
= type
->t
& VT_VIS_MASK
;
307 if (vis
== (STV_DEFAULT
<< VT_VIS_SHIFT
))
309 else if (vis2
== (STV_DEFAULT
<< VT_VIS_SHIFT
))
312 vis
= (vis
< vis2
) ? vis
: vis2
;
313 sym
->type
.t
&= ~VT_VIS_MASK
;
319 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
320 vis
>>= VT_VIS_SHIFT
;
321 esym
->st_other
= (esym
->st_other
& ~ELFW(ST_VISIBILITY
)(-1)) | vis
;
325 /* ------------------------------------------------------------------------- */
327 ST_FUNC
void swap(int *p
, int *q
)
335 static void vsetc(CType
*type
, int r
, CValue
*vc
)
339 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
340 tcc_error("memory full (vstack)");
341 /* cannot let cpu flags if other instruction are generated. Also
342 avoid leaving VT_JMP anywhere except on the top of the stack
343 because it would complicate the code generator. */
344 if (vtop
>= vstack
) {
345 v
= vtop
->r
& VT_VALMASK
;
346 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
356 /* push constant of type "type" with useless value */
357 ST_FUNC
void vpush(CType
*type
)
360 vsetc(type
, VT_CONST
, &cval
);
363 /* push integer constant */
364 ST_FUNC
void vpushi(int v
)
368 vsetc(&int_type
, VT_CONST
, &cval
);
371 /* push a pointer sized constant */
372 static void vpushs(addr_t v
)
376 vsetc(&size_type
, VT_CONST
, &cval
);
379 /* push arbitrary 64bit constant */
380 ST_FUNC
void vpush64(int ty
, unsigned long long v
)
387 vsetc(&ctype
, VT_CONST
, &cval
);
390 /* push long long constant */
391 static inline void vpushll(long long v
)
393 vpush64(VT_LLONG
, v
);
396 /* push a symbol value of TYPE */
397 static inline void vpushsym(CType
*type
, Sym
*sym
)
401 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
405 /* Return a static symbol pointing to a section */
406 ST_FUNC Sym
*get_sym_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
412 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
413 sym
->type
.ref
= type
->ref
;
414 sym
->r
= VT_CONST
| VT_SYM
;
415 put_extern_sym(sym
, sec
, offset
, size
);
419 /* push a reference to a section offset by adding a dummy symbol */
420 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
422 vpushsym(type
, get_sym_ref(type
, sec
, offset
, size
));
425 /* define a new external reference to a symbol 'v' of type 'u' */
426 ST_FUNC Sym
*external_global_sym(int v
, CType
*type
, int r
)
432 /* push forward reference */
433 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
434 s
->type
.ref
= type
->ref
;
435 s
->r
= r
| VT_CONST
| VT_SYM
;
440 /* define a new external reference to a symbol 'v' with alternate asm
441 name 'asm_label' of type 'u'. 'asm_label' is equal to NULL if there
442 is no alternate name (most cases) */
443 static Sym
*external_sym(int v
, CType
*type
, int r
, char *asm_label
)
449 /* push forward reference */
450 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
451 s
->asm_label
= asm_label
;
452 s
->type
.t
|= VT_EXTERN
;
453 } else if (s
->type
.ref
== func_old_type
.ref
) {
454 s
->type
.ref
= type
->ref
;
455 s
->r
= r
| VT_CONST
| VT_SYM
;
456 s
->type
.t
|= VT_EXTERN
;
457 } else if (!is_compatible_types(&s
->type
, type
)) {
458 tcc_error("incompatible types for redefinition of '%s'",
459 get_tok_str(v
, NULL
));
461 /* Merge some storage attributes. */
462 if (type
->t
& VT_WEAK
)
465 if (type
->t
& VT_VIS_MASK
)
466 apply_visibility(s
, type
);
471 /* push a reference to global symbol v */
472 ST_FUNC
void vpush_global_sym(CType
*type
, int v
)
474 vpushsym(type
, external_global_sym(v
, type
, 0));
477 ST_FUNC
void vset(CType
*type
, int r
, int v
)
482 vsetc(type
, r
, &cval
);
485 static void vseti(int r
, int v
)
493 ST_FUNC
void vswap(void)
496 /* cannot let cpu flags if other instruction are generated. Also
497 avoid leaving VT_JMP anywhere except on the top of the stack
498 because it would complicate the code generator. */
499 if (vtop
>= vstack
) {
500 int v
= vtop
->r
& VT_VALMASK
;
501 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
508 /* XXX: +2% overall speed possible with optimized memswap
510 * memswap(&vtop[0], &vtop[1], sizeof *vtop);
514 ST_FUNC
void vpushv(SValue
*v
)
516 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
517 tcc_error("memory full (vstack)");
522 static void vdup(void)
527 /* save r to the memory stack, and mark it as being free */
528 ST_FUNC
void save_reg(int r
)
530 int l
, saved
, size
, align
;
534 /* modify all stack values */
537 for(p
=vstack
;p
<=vtop
;p
++) {
538 if ((p
->r
& VT_VALMASK
) == r
||
539 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
540 /* must save value on stack if not already done */
542 /* NOTE: must reload 'r' because r might be equal to r2 */
543 r
= p
->r
& VT_VALMASK
;
544 /* store register in the stack */
546 if ((p
->r
& VT_LVAL
) ||
547 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
548 #ifdef TCC_TARGET_X86_64
549 type
= &char_pointer_type
;
553 size
= type_size(type
, &align
);
554 loc
= (loc
- size
) & -align
;
556 sv
.r
= VT_LOCAL
| VT_LVAL
;
559 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
560 /* x86 specific: need to pop fp register ST0 if saved */
562 o(0xd8dd); /* fstp %st(0) */
565 #ifndef TCC_TARGET_X86_64
566 /* special long long case */
567 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
575 /* mark that stack entry as being saved on the stack */
576 if (p
->r
& VT_LVAL
) {
577 /* also clear the bounded flag because the
578 relocation address of the function was stored in
580 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
582 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
590 #ifdef TCC_TARGET_ARM
591 /* find a register of class 'rc2' with at most one reference on stack.
592 * If none, call get_reg(rc) */
593 ST_FUNC
int get_reg_ex(int rc
, int rc2
)
598 for(r
=0;r
<NB_REGS
;r
++) {
599 if (reg_classes
[r
] & rc2
) {
602 for(p
= vstack
; p
<= vtop
; p
++) {
603 if ((p
->r
& VT_VALMASK
) == r
||
604 (p
->r2
& VT_VALMASK
) == r
)
615 /* find a free register of class 'rc'. If none, save one register */
616 ST_FUNC
int get_reg(int rc
)
621 /* find a free register */
622 for(r
=0;r
<NB_REGS
;r
++) {
623 if (reg_classes
[r
] & rc
) {
624 for(p
=vstack
;p
<=vtop
;p
++) {
625 if ((p
->r
& VT_VALMASK
) == r
||
626 (p
->r2
& VT_VALMASK
) == r
)
634 /* no register left : free the first one on the stack (VERY
635 IMPORTANT to start from the bottom to ensure that we don't
636 spill registers used in gen_opi()) */
637 for(p
=vstack
;p
<=vtop
;p
++) {
638 /* look at second register (if long long) */
639 r
= p
->r2
& VT_VALMASK
;
640 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
642 r
= p
->r
& VT_VALMASK
;
643 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
649 /* Should never comes here */
653 /* save registers up to (vtop - n) stack entry */
654 ST_FUNC
void save_regs(int n
)
659 for(p
= vstack
;p
<= p1
; p
++) {
660 r
= p
->r
& VT_VALMASK
;
667 /* move register 's' (of type 't') to 'r', and flush previous value of r to memory
669 static void move_reg(int r
, int s
, int t
)
683 /* get address of vtop (vtop MUST BE an lvalue) */
684 static void gaddrof(void)
686 if (vtop
->r
& VT_REF
)
689 /* tricky: if saved lvalue, then we can go back to lvalue */
690 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
691 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
696 #ifdef CONFIG_TCC_BCHECK
697 /* generate lvalue bound code */
698 static void gbound(void)
703 vtop
->r
&= ~VT_MUSTBOUND
;
704 /* if lvalue, then use checking code before dereferencing */
705 if (vtop
->r
& VT_LVAL
) {
706 /* if not VT_BOUNDED value, then make one */
707 if (!(vtop
->r
& VT_BOUNDED
)) {
708 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
709 /* must save type because we must set it to int to get pointer */
711 vtop
->type
.t
= VT_INT
;
714 gen_bounded_ptr_add();
715 vtop
->r
|= lval_type
;
718 /* then check for dereferencing */
719 gen_bounded_ptr_deref();
724 /* store vtop a register belonging to class 'rc'. lvalues are
725 converted to values. Cannot be used if cannot be converted to
726 register value (such as structures). */
727 ST_FUNC
int gv(int rc
)
729 int r
, bit_pos
, bit_size
, size
, align
, i
;
732 /* NOTE: get_reg can modify vstack[] */
733 if (vtop
->type
.t
& VT_BITFIELD
) {
736 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
737 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
738 /* remove bit field info to avoid loops */
739 vtop
->type
.t
&= ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
740 /* cast to int to propagate signedness in following ops */
741 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
746 if((vtop
->type
.t
& VT_UNSIGNED
) ||
747 (vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
748 type
.t
|= VT_UNSIGNED
;
750 /* generate shifts */
751 vpushi(bits
- (bit_pos
+ bit_size
));
753 vpushi(bits
- bit_size
);
754 /* NOTE: transformed to SHR if unsigned */
758 if (is_float(vtop
->type
.t
) &&
759 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
762 unsigned long offset
;
763 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
767 /* XXX: unify with initializers handling ? */
768 /* CPUs usually cannot use float constants, so we store them
769 generically in data segment */
770 size
= type_size(&vtop
->type
, &align
);
771 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
772 data_section
->data_offset
= offset
;
773 /* XXX: not portable yet */
774 #if defined(__i386__) || defined(__x86_64__)
775 /* Zero pad x87 tenbyte long doubles */
776 if (size
== LDOUBLE_SIZE
) {
777 vtop
->c
.tab
[2] &= 0xffff;
778 #if LDOUBLE_SIZE == 16
783 ptr
= section_ptr_add(data_section
, size
);
785 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
789 ptr
[i
] = vtop
->c
.tab
[size
-1-i
];
793 ptr
[i
] = vtop
->c
.tab
[i
];
794 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
795 vtop
->r
|= VT_LVAL
| VT_SYM
;
797 vtop
->c
.ptr_offset
= 0;
799 #ifdef CONFIG_TCC_BCHECK
800 if (vtop
->r
& VT_MUSTBOUND
)
804 r
= vtop
->r
& VT_VALMASK
;
805 rc2
= (rc
& RC_FLOAT
) ? RC_FLOAT
: RC_INT
;
808 #ifdef TCC_TARGET_X86_64
809 else if (rc
== RC_FRET
)
813 /* need to reload if:
815 - lvalue (need to dereference pointer)
816 - already a register, but not in the right class */
818 || (vtop
->r
& VT_LVAL
)
819 || !(reg_classes
[r
] & rc
)
820 #ifdef TCC_TARGET_X86_64
821 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
822 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
&& !(reg_classes
[vtop
->r2
] & rc2
))
824 || ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
829 #ifdef TCC_TARGET_X86_64
830 if (((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
)) {
831 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
833 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
834 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
835 unsigned long long ll
;
837 int r2
, original_type
;
838 original_type
= vtop
->type
.t
;
839 /* two register type load : expand to two words
841 #ifndef TCC_TARGET_X86_64
842 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
845 vtop
->c
.ui
= ll
; /* first word */
847 vtop
->r
= r
; /* save register value */
848 vpushi(ll
>> 32); /* second word */
851 if (r
>= VT_CONST
|| /* XXX: test to VT_CONST incorrect ? */
852 (vtop
->r
& VT_LVAL
)) {
853 /* We do not want to modifier the long long
854 pointer here, so the safest (and less
855 efficient) is to save all the other registers
856 in the stack. XXX: totally inefficient. */
858 /* load from memory */
859 vtop
->type
.t
= load_type
;
862 vtop
[-1].r
= r
; /* save register value */
863 /* increment pointer to get second word */
864 vtop
->type
.t
= addr_type
;
869 vtop
->type
.t
= load_type
;
874 vtop
[-1].r
= r
; /* save register value */
875 vtop
->r
= vtop
[-1].r2
;
877 /* Allocate second register. Here we rely on the fact that
878 get_reg() tries first to free r2 of an SValue. */
882 /* write second register */
884 vtop
->type
.t
= original_type
;
885 } else if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
887 /* lvalue of scalar type : need to use lvalue type
888 because of possible cast */
891 /* compute memory access type */
892 if (vtop
->r
& VT_REF
)
893 #ifdef TCC_TARGET_X86_64
898 else if (vtop
->r
& VT_LVAL_BYTE
)
900 else if (vtop
->r
& VT_LVAL_SHORT
)
902 if (vtop
->r
& VT_LVAL_UNSIGNED
)
906 /* restore wanted type */
909 /* one register type load */
914 #ifdef TCC_TARGET_C67
915 /* uses register pairs for doubles */
916 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
923 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
924 ST_FUNC
void gv2(int rc1
, int rc2
)
928 /* generate more generic register first. But VT_JMP or VT_CMP
929 values must be generated first in all cases to avoid possible
931 v
= vtop
[0].r
& VT_VALMASK
;
932 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
937 /* test if reload is needed for first register */
938 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
948 /* test if reload is needed for first register */
949 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
955 /* wrapper around RC_FRET to return a register by type */
956 static int rc_fret(int t
)
958 #ifdef TCC_TARGET_X86_64
959 if (t
== VT_LDOUBLE
) {
966 /* wrapper around REG_FRET to return a register by type */
967 static int reg_fret(int t
)
969 #ifdef TCC_TARGET_X86_64
970 if (t
== VT_LDOUBLE
) {
977 /* expand long long on stack in two int registers */
978 static void lexpand(void)
982 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
985 vtop
[0].r
= vtop
[-1].r2
;
986 vtop
[0].r2
= VT_CONST
;
987 vtop
[-1].r2
= VT_CONST
;
988 vtop
[0].type
.t
= VT_INT
| u
;
989 vtop
[-1].type
.t
= VT_INT
| u
;
992 #ifdef TCC_TARGET_ARM
993 /* expand long long on stack */
994 ST_FUNC
void lexpand_nr(void)
998 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
1000 vtop
->r2
= VT_CONST
;
1001 vtop
->type
.t
= VT_INT
| u
;
1002 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
1003 if (v
== VT_CONST
) {
1004 vtop
[-1].c
.ui
= vtop
->c
.ull
;
1005 vtop
->c
.ui
= vtop
->c
.ull
>> 32;
1007 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
1009 vtop
->r
= vtop
[-1].r
;
1010 } else if (v
> VT_CONST
) {
1014 vtop
->r
= vtop
[-1].r2
;
1015 vtop
[-1].r2
= VT_CONST
;
1016 vtop
[-1].type
.t
= VT_INT
| u
;
1020 /* build a long long from two ints */
1021 static void lbuild(int t
)
1023 gv2(RC_INT
, RC_INT
);
1024 vtop
[-1].r2
= vtop
[0].r
;
1025 vtop
[-1].type
.t
= t
;
1029 /* rotate n first stack elements to the bottom
1030 I1 ... In -> I2 ... In I1 [top is right]
1032 ST_FUNC
void vrotb(int n
)
1038 for(i
=-n
+1;i
!=0;i
++)
1039 vtop
[i
] = vtop
[i
+1];
1043 /* rotate the n elements before entry e towards the top
1044 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
1046 ST_FUNC
void vrote(SValue
*e
, int n
)
1052 for(i
= 0;i
< n
- 1; i
++)
1057 /* rotate n first stack elements to the top
1058 I1 ... In -> In I1 ... I(n-1) [top is right]
1060 ST_FUNC
void vrott(int n
)
1065 /* pop stack value */
1066 ST_FUNC
void vpop(void)
1069 v
= vtop
->r
& VT_VALMASK
;
1070 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1071 /* for x86, we need to pop the FP stack */
1072 if (v
== TREG_ST0
&& !nocode_wanted
) {
1073 o(0xd8dd); /* fstp %st(0) */
1076 if (v
== VT_JMP
|| v
== VT_JMPI
) {
1077 /* need to put correct jump if && or || without test */
1083 /* convert stack entry to register and duplicate its value in another
1085 static void gv_dup(void)
1091 if ((t
& VT_BTYPE
) == VT_LLONG
) {
1098 /* stack: H L L1 H1 */
1106 /* duplicate value */
1111 #ifdef TCC_TARGET_X86_64
1112 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1122 load(r1
, &sv
); /* move r to r1 */
1124 /* duplicates value */
1130 /* Generate value test
1132 * Generate a test for any value (jump, comparison and integers) */
1133 ST_FUNC
int gvtst(int inv
, int t
)
1135 int v
= vtop
->r
& VT_VALMASK
;
1136 if (v
!= VT_CMP
&& v
!= VT_JMP
&& v
!= VT_JMPI
) {
1140 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1141 /* constant jmp optimization */
1142 if ((vtop
->c
.i
!= 0) != inv
)
1147 return gtst(inv
, t
);
1150 #ifndef TCC_TARGET_X86_64
1151 /* generate CPU independent (unsigned) long long operations */
1152 static void gen_opl(int op
)
1154 int t
, a
, b
, op1
, c
, i
;
1156 unsigned short reg_iret
= REG_IRET
;
1157 unsigned short reg_lret
= REG_LRET
;
1163 func
= TOK___divdi3
;
1166 func
= TOK___udivdi3
;
1169 func
= TOK___moddi3
;
1172 func
= TOK___umoddi3
;
1179 /* call generic long long function */
1180 vpush_global_sym(&func_old_type
, func
);
1185 vtop
->r2
= reg_lret
;
1198 /* stack: L1 H1 L2 H2 */
1203 vtop
[-2] = vtop
[-3];
1206 /* stack: H1 H2 L1 L2 */
1212 /* stack: H1 H2 L1 L2 ML MH */
1215 /* stack: ML MH H1 H2 L1 L2 */
1219 /* stack: ML MH H1 L2 H2 L1 */
1224 /* stack: ML MH M1 M2 */
1227 } else if (op
== '+' || op
== '-') {
1228 /* XXX: add non carry method too (for MIPS or alpha) */
1234 /* stack: H1 H2 (L1 op L2) */
1237 gen_op(op1
+ 1); /* TOK_xxxC2 */
1240 /* stack: H1 H2 (L1 op L2) */
1243 /* stack: (L1 op L2) H1 H2 */
1245 /* stack: (L1 op L2) (H1 op H2) */
1253 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1254 t
= vtop
[-1].type
.t
;
1258 /* stack: L H shift */
1260 /* constant: simpler */
1261 /* NOTE: all comments are for SHL. the other cases are
1262 done by swaping words */
1273 if (op
!= TOK_SAR
) {
1306 /* XXX: should provide a faster fallback on x86 ? */
1309 func
= TOK___ashrdi3
;
1312 func
= TOK___lshrdi3
;
1315 func
= TOK___ashldi3
;
1321 /* compare operations */
1327 /* stack: L1 H1 L2 H2 */
1329 vtop
[-1] = vtop
[-2];
1331 /* stack: L1 L2 H1 H2 */
1334 /* when values are equal, we need to compare low words. since
1335 the jump is inverted, we invert the test too. */
1338 else if (op1
== TOK_GT
)
1340 else if (op1
== TOK_ULT
)
1342 else if (op1
== TOK_UGT
)
1347 if (op1
!= TOK_NE
) {
1351 /* generate non equal test */
1352 /* XXX: NOT PORTABLE yet */
1356 #if defined(TCC_TARGET_I386)
1357 b
= psym(0x850f, 0);
1358 #elif defined(TCC_TARGET_ARM)
1360 o(0x1A000000 | encbranch(ind
, 0, 1));
1361 #elif defined(TCC_TARGET_C67)
1362 tcc_error("not implemented");
1364 #error not supported
1368 /* compare low. Always unsigned */
1372 else if (op1
== TOK_LE
)
1374 else if (op1
== TOK_GT
)
1376 else if (op1
== TOK_GE
)
1387 /* handle integer constant optimizations and various machine
1389 static void gen_opic(int op
)
1391 int c1
, c2
, t1
, t2
, n
;
1394 typedef unsigned long long U
;
1398 t1
= v1
->type
.t
& VT_BTYPE
;
1399 t2
= v2
->type
.t
& VT_BTYPE
;
1403 else if (v1
->type
.t
& VT_UNSIGNED
)
1410 else if (v2
->type
.t
& VT_UNSIGNED
)
1415 /* currently, we cannot do computations with forward symbols */
1416 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1417 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1420 case '+': l1
+= l2
; break;
1421 case '-': l1
-= l2
; break;
1422 case '&': l1
&= l2
; break;
1423 case '^': l1
^= l2
; break;
1424 case '|': l1
|= l2
; break;
1425 case '*': l1
*= l2
; break;
1432 /* if division by zero, generate explicit division */
1435 tcc_error("division by zero in constant");
1439 default: l1
/= l2
; break;
1440 case '%': l1
%= l2
; break;
1441 case TOK_UDIV
: l1
= (U
)l1
/ l2
; break;
1442 case TOK_UMOD
: l1
= (U
)l1
% l2
; break;
1445 case TOK_SHL
: l1
<<= l2
; break;
1446 case TOK_SHR
: l1
= (U
)l1
>> l2
; break;
1447 case TOK_SAR
: l1
>>= l2
; break;
1449 case TOK_ULT
: l1
= (U
)l1
< (U
)l2
; break;
1450 case TOK_UGE
: l1
= (U
)l1
>= (U
)l2
; break;
1451 case TOK_EQ
: l1
= l1
== l2
; break;
1452 case TOK_NE
: l1
= l1
!= l2
; break;
1453 case TOK_ULE
: l1
= (U
)l1
<= (U
)l2
; break;
1454 case TOK_UGT
: l1
= (U
)l1
> (U
)l2
; break;
1455 case TOK_LT
: l1
= l1
< l2
; break;
1456 case TOK_GE
: l1
= l1
>= l2
; break;
1457 case TOK_LE
: l1
= l1
<= l2
; break;
1458 case TOK_GT
: l1
= l1
> l2
; break;
1460 case TOK_LAND
: l1
= l1
&& l2
; break;
1461 case TOK_LOR
: l1
= l1
|| l2
; break;
1468 /* if commutative ops, put c2 as constant */
1469 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1470 op
== '|' || op
== '*')) {
1472 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1473 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1475 /* Filter out NOP operations like x*1, x-0, x&-1... */
1476 if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1479 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1480 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1486 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1487 /* try to use shifts instead of muls or divs */
1488 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1497 else if (op
== TOK_PDIV
)
1503 } else if (c2
&& (op
== '+' || op
== '-') &&
1504 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
1505 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1506 /* symbol + constant case */
1513 if (!nocode_wanted
) {
1514 /* call low level op generator */
1515 if (t1
== VT_LLONG
|| t2
== VT_LLONG
)
1526 /* generate a floating point operation with constant propagation */
1527 static void gen_opif(int op
)
1535 /* currently, we cannot do computations with forward symbols */
1536 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1537 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1539 if (v1
->type
.t
== VT_FLOAT
) {
1542 } else if (v1
->type
.t
== VT_DOUBLE
) {
1550 /* NOTE: we only do constant propagation if finite number (not
1551 NaN or infinity) (ANSI spec) */
1552 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1556 case '+': f1
+= f2
; break;
1557 case '-': f1
-= f2
; break;
1558 case '*': f1
*= f2
; break;
1562 tcc_error("division by zero in constant");
1567 /* XXX: also handles tests ? */
1571 /* XXX: overflow test ? */
1572 if (v1
->type
.t
== VT_FLOAT
) {
1574 } else if (v1
->type
.t
== VT_DOUBLE
) {
1582 if (!nocode_wanted
) {
1590 static int pointed_size(CType
*type
)
1593 return type_size(pointed_type(type
), &align
);
1596 static void vla_runtime_pointed_size(CType
*type
)
1599 vla_runtime_type_size(pointed_type(type
), &align
);
1602 static inline int is_null_pointer(SValue
*p
)
1604 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1606 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& p
->c
.i
== 0) ||
1607 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.ll
== 0) ||
1608 ((p
->type
.t
& VT_BTYPE
) == VT_PTR
&& p
->c
.ptr_offset
== 0);
1611 static inline int is_integer_btype(int bt
)
1613 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
1614 bt
== VT_INT
|| bt
== VT_LLONG
);
1617 /* check types for comparison or subtraction of pointers */
1618 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
1620 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
1623 /* null pointers are accepted for all comparisons as gcc */
1624 if (is_null_pointer(p1
) || is_null_pointer(p2
))
1628 bt1
= type1
->t
& VT_BTYPE
;
1629 bt2
= type2
->t
& VT_BTYPE
;
1630 /* accept comparison between pointer and integer with a warning */
1631 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
1632 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
1633 tcc_warning("comparison between pointer and integer");
1637 /* both must be pointers or implicit function pointers */
1638 if (bt1
== VT_PTR
) {
1639 type1
= pointed_type(type1
);
1640 } else if (bt1
!= VT_FUNC
)
1641 goto invalid_operands
;
1643 if (bt2
== VT_PTR
) {
1644 type2
= pointed_type(type2
);
1645 } else if (bt2
!= VT_FUNC
) {
1647 tcc_error("invalid operands to binary %s", get_tok_str(op
, NULL
));
1649 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1650 (type2
->t
& VT_BTYPE
) == VT_VOID
)
1654 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1655 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1656 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1657 /* gcc-like error if '-' is used */
1659 goto invalid_operands
;
1661 tcc_warning("comparison of distinct pointer types lacks a cast");
1665 /* generic gen_op: handles types problems */
1666 ST_FUNC
void gen_op(int op
)
1668 int u
, t1
, t2
, bt1
, bt2
, t
;
1671 t1
= vtop
[-1].type
.t
;
1672 t2
= vtop
[0].type
.t
;
1673 bt1
= t1
& VT_BTYPE
;
1674 bt2
= t2
& VT_BTYPE
;
1676 if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
1677 /* at least one operand is a pointer */
1678 /* relationnal op: must be both pointers */
1679 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
1680 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1681 /* pointers are handled are unsigned */
1682 #ifdef TCC_TARGET_X86_64
1683 t
= VT_LLONG
| VT_UNSIGNED
;
1685 t
= VT_INT
| VT_UNSIGNED
;
1689 /* if both pointers, then it must be the '-' op */
1690 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
1692 tcc_error("cannot use pointers here");
1693 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1694 /* XXX: check that types are compatible */
1695 if (vtop
[-1].type
.t
& VT_VLA
) {
1696 vla_runtime_pointed_size(&vtop
[-1].type
);
1698 vpushi(pointed_size(&vtop
[-1].type
));
1702 /* set to integer type */
1703 #ifdef TCC_TARGET_X86_64
1704 vtop
->type
.t
= VT_LLONG
;
1706 vtop
->type
.t
= VT_INT
;
1711 /* exactly one pointer : must be '+' or '-'. */
1712 if (op
!= '-' && op
!= '+')
1713 tcc_error("cannot use pointers here");
1714 /* Put pointer as first operand */
1715 if (bt2
== VT_PTR
) {
1719 type1
= vtop
[-1].type
;
1720 type1
.t
&= ~VT_ARRAY
;
1721 if (vtop
[-1].type
.t
& VT_VLA
)
1722 vla_runtime_pointed_size(&vtop
[-1].type
);
1724 u
= pointed_size(&vtop
[-1].type
);
1726 tcc_error("unknown array element size");
1727 #ifdef TCC_TARGET_X86_64
1730 /* XXX: cast to int ? (long long case) */
1735 #ifdef CONFIG_TCC_BCHECK
1736 /* if evaluating constant expression, no code should be
1737 generated, so no bound check */
1738 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
1739 /* if bounded pointers, we generate a special code to
1746 gen_bounded_ptr_add();
1752 /* put again type if gen_opic() swaped operands */
1755 } else if (is_float(bt1
) || is_float(bt2
)) {
1756 /* compute bigger type and do implicit casts */
1757 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
1759 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
1764 /* floats can only be used for a few operations */
1765 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
1766 (op
< TOK_ULT
|| op
> TOK_GT
))
1767 tcc_error("invalid operands for binary operation");
1769 } else if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
) {
1770 t
= bt1
== VT_LLONG
? VT_LLONG
: VT_INT
;
1771 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (t
| VT_UNSIGNED
))
1774 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
1775 /* cast to biggest op */
1777 /* convert to unsigned if it does not fit in a long long */
1778 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
1779 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
1782 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
1783 tcc_error("comparison of struct");
1785 /* integer operations */
1787 /* convert to unsigned if it does not fit in an integer */
1788 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
1789 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
1792 /* XXX: currently, some unsigned operations are explicit, so
1793 we modify them here */
1794 if (t
& VT_UNSIGNED
) {
1801 else if (op
== TOK_LT
)
1803 else if (op
== TOK_GT
)
1805 else if (op
== TOK_LE
)
1807 else if (op
== TOK_GE
)
1814 /* special case for shifts and long long: we keep the shift as
1816 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
1823 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
1824 /* relationnal op: the result is an int */
1825 vtop
->type
.t
= VT_INT
;
1832 #ifndef TCC_TARGET_ARM
1833 /* generic itof for unsigned long long case */
1834 static void gen_cvt_itof1(int t
)
1836 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
1837 (VT_LLONG
| VT_UNSIGNED
)) {
1840 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
1841 #if LDOUBLE_SIZE != 8
1842 else if (t
== VT_LDOUBLE
)
1843 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
1846 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
1850 vtop
->r
= reg_fret(t
);
1857 /* generic ftoi for unsigned long long case */
1858 static void gen_cvt_ftoi1(int t
)
1862 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
1863 /* not handled natively */
1864 st
= vtop
->type
.t
& VT_BTYPE
;
1866 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
1867 #if LDOUBLE_SIZE != 8
1868 else if (st
== VT_LDOUBLE
)
1869 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
1872 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
1877 vtop
->r2
= REG_LRET
;
1883 /* force char or short cast */
1884 static void force_charshort_cast(int t
)
1888 /* XXX: add optimization if lvalue : just change type and offset */
1893 if (t
& VT_UNSIGNED
) {
1894 vpushi((1 << bits
) - 1);
1900 /* result must be signed or the SAR is converted to an SHL
1901 This was not the case when "t" was a signed short
1902 and the last value on the stack was an unsigned int */
1903 vtop
->type
.t
&= ~VT_UNSIGNED
;
1909 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1910 static void gen_cast(CType
*type
)
1912 int sbt
, dbt
, sf
, df
, c
, p
;
1914 /* special delayed cast for char/short */
1915 /* XXX: in some cases (multiple cascaded casts), it may still
1917 if (vtop
->r
& VT_MUSTCAST
) {
1918 vtop
->r
&= ~VT_MUSTCAST
;
1919 force_charshort_cast(vtop
->type
.t
);
1922 /* bitfields first get cast to ints */
1923 if (vtop
->type
.t
& VT_BITFIELD
) {
1927 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
1928 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
1933 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1934 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
1936 /* constant case: we can do it now */
1937 /* XXX: in ISOC, cannot do it if error in convert */
1938 if (sbt
== VT_FLOAT
)
1939 vtop
->c
.ld
= vtop
->c
.f
;
1940 else if (sbt
== VT_DOUBLE
)
1941 vtop
->c
.ld
= vtop
->c
.d
;
1944 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
1945 if (sbt
& VT_UNSIGNED
)
1946 vtop
->c
.ld
= vtop
->c
.ull
;
1948 vtop
->c
.ld
= vtop
->c
.ll
;
1950 if (sbt
& VT_UNSIGNED
)
1951 vtop
->c
.ld
= vtop
->c
.ui
;
1953 vtop
->c
.ld
= vtop
->c
.i
;
1956 if (dbt
== VT_FLOAT
)
1957 vtop
->c
.f
= (float)vtop
->c
.ld
;
1958 else if (dbt
== VT_DOUBLE
)
1959 vtop
->c
.d
= (double)vtop
->c
.ld
;
1960 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
1961 vtop
->c
.ull
= (unsigned long long)vtop
->c
.ld
;
1962 } else if (sf
&& dbt
== VT_BOOL
) {
1963 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
1966 vtop
->c
.ll
= (long long)vtop
->c
.ld
;
1967 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
1968 vtop
->c
.ll
= vtop
->c
.ull
;
1969 else if (sbt
& VT_UNSIGNED
)
1970 vtop
->c
.ll
= vtop
->c
.ui
;
1971 #ifdef TCC_TARGET_X86_64
1972 else if (sbt
== VT_PTR
)
1975 else if (sbt
!= VT_LLONG
)
1976 vtop
->c
.ll
= vtop
->c
.i
;
1978 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
1979 vtop
->c
.ull
= vtop
->c
.ll
;
1980 else if (dbt
== VT_BOOL
)
1981 vtop
->c
.i
= (vtop
->c
.ll
!= 0);
1982 #ifdef TCC_TARGET_X86_64
1983 else if (dbt
== VT_PTR
)
1986 else if (dbt
!= VT_LLONG
) {
1988 if ((dbt
& VT_BTYPE
) == VT_BYTE
)
1990 else if ((dbt
& VT_BTYPE
) == VT_SHORT
)
1992 if(dbt
& VT_UNSIGNED
)
1993 vtop
->c
.ui
= ((unsigned int)vtop
->c
.ll
<< s
) >> s
;
1995 vtop
->c
.i
= ((int)vtop
->c
.ll
<< s
) >> s
;
1998 } else if (p
&& dbt
== VT_BOOL
) {
2001 } else if (!nocode_wanted
) {
2002 /* non constant case: generate code */
2004 /* convert from fp to fp */
2007 /* convert int to fp */
2010 /* convert fp to int */
2011 if (dbt
== VT_BOOL
) {
2015 /* we handle char/short/etc... with generic code */
2016 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
2017 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
2021 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
2022 /* additional cast for char/short... */
2027 #ifndef TCC_TARGET_X86_64
2028 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
2029 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
2030 /* scalar to long long */
2031 /* machine independent conversion */
2033 /* generate high word */
2034 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
2038 if (sbt
== VT_PTR
) {
2039 /* cast from pointer to int before we apply
2040 shift operation, which pointers don't support*/
2041 gen_cast(&int_type
);
2047 /* patch second register */
2048 vtop
[-1].r2
= vtop
->r
;
2052 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
2053 (dbt
& VT_BTYPE
) == VT_PTR
||
2054 (dbt
& VT_BTYPE
) == VT_FUNC
) {
2055 if ((sbt
& VT_BTYPE
) != VT_LLONG
&&
2056 (sbt
& VT_BTYPE
) != VT_PTR
&&
2057 (sbt
& VT_BTYPE
) != VT_FUNC
) {
2058 /* need to convert from 32bit to 64bit */
2060 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
2061 /* x86_64 specific: movslq */
2063 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
2067 } else if (dbt
== VT_BOOL
) {
2068 /* scalar to bool */
2071 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
2072 (dbt
& VT_BTYPE
) == VT_SHORT
) {
2073 if (sbt
== VT_PTR
) {
2074 vtop
->type
.t
= VT_INT
;
2075 tcc_warning("nonportable conversion from pointer to char/short");
2077 force_charshort_cast(dbt
);
2078 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
2080 if (sbt
== VT_LLONG
) {
2081 /* from long long: just take low order word */
2085 /* if lvalue and single word type, nothing to do because
2086 the lvalue already contains the real type size (see
2087 VT_LVAL_xxx constants) */
2090 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
2091 /* if we are casting between pointer types,
2092 we must update the VT_LVAL_xxx size */
2093 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
2094 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
2099 /* return type size as known at compile time. Put alignment at 'a' */
2100 ST_FUNC
int type_size(CType
*type
, int *a
)
2105 bt
= type
->t
& VT_BTYPE
;
2106 if (bt
== VT_STRUCT
) {
2111 } else if (bt
== VT_PTR
) {
2112 if (type
->t
& VT_ARRAY
) {
2116 ts
= type_size(&s
->type
, a
);
2118 if (ts
< 0 && s
->c
< 0)
2126 } else if (bt
== VT_LDOUBLE
) {
2128 return LDOUBLE_SIZE
;
2129 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
2130 #ifdef TCC_TARGET_I386
2131 #ifdef TCC_TARGET_PE
2136 #elif defined(TCC_TARGET_ARM)
2146 } else if (bt
== VT_INT
|| bt
== VT_ENUM
|| bt
== VT_FLOAT
) {
2149 } else if (bt
== VT_SHORT
) {
2152 } else if (bt
== VT_QLONG
|| bt
== VT_QFLOAT
) {
2156 /* char, void, function, _Bool */
2162 /* push type size as known at runtime time on top of value stack. Put
2164 ST_FUNC
void vla_runtime_type_size(CType
*type
, int *a
)
2166 if (type
->t
& VT_VLA
) {
2167 vset(&int_type
, VT_LOCAL
|VT_LVAL
, type
->ref
->c
);
2169 vpushi(type_size(type
, a
));
2173 static void vla_sp_save(void) {
2174 if (!(vla_flags
& VLA_SP_LOC_SET
)) {
2175 *vla_sp_loc
= (loc
-= PTR_SIZE
);
2176 vla_flags
|= VLA_SP_LOC_SET
;
2178 if (!(vla_flags
& VLA_SP_SAVED
)) {
2179 gen_vla_sp_save(*vla_sp_loc
);
2180 vla_flags
|= VLA_SP_SAVED
;
2184 /* return the pointed type of t */
2185 static inline CType
*pointed_type(CType
*type
)
2187 return &type
->ref
->type
;
2190 /* modify type so that its it is a pointer to type. */
2191 ST_FUNC
void mk_pointer(CType
*type
)
2194 s
= sym_push(SYM_FIELD
, type
, 0, -1);
2195 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
2199 /* compare function types. OLD functions match any new functions */
2200 static int is_compatible_func(CType
*type1
, CType
*type2
)
2206 if (!is_compatible_types(&s1
->type
, &s2
->type
))
2208 /* check func_call */
2209 if (s1
->a
.func_call
!= s2
->a
.func_call
)
2211 /* XXX: not complete */
2212 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
2216 while (s1
!= NULL
) {
2219 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
2229 /* return true if type1 and type2 are the same. If unqualified is
2230 true, qualifiers on the types are ignored.
2232 - enums are not checked as gcc __builtin_types_compatible_p ()
2234 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2238 t1
= type1
->t
& VT_TYPE
;
2239 t2
= type2
->t
& VT_TYPE
;
2241 /* strip qualifiers before comparing */
2242 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2243 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2245 /* Default Vs explicit signedness only matters for char */
2246 if ((t1
& VT_BTYPE
) != VT_BYTE
) {
2250 /* XXX: bitfields ? */
2253 /* test more complicated cases */
2254 bt1
= t1
& VT_BTYPE
;
2255 if (bt1
== VT_PTR
) {
2256 type1
= pointed_type(type1
);
2257 type2
= pointed_type(type2
);
2258 return is_compatible_types(type1
, type2
);
2259 } else if (bt1
== VT_STRUCT
) {
2260 return (type1
->ref
== type2
->ref
);
2261 } else if (bt1
== VT_FUNC
) {
2262 return is_compatible_func(type1
, type2
);
2268 /* return true if type1 and type2 are exactly the same (including
2271 static int is_compatible_types(CType
*type1
, CType
*type2
)
2273 return compare_types(type1
,type2
,0);
2276 /* return true if type1 and type2 are the same (ignoring qualifiers).
2278 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
2280 return compare_types(type1
,type2
,1);
2283 /* print a type. If 'varstr' is not NULL, then the variable is also
2284 printed in the type */
2286 /* XXX: add array and function pointers */
2287 static void type_to_str(char *buf
, int buf_size
,
2288 CType
*type
, const char *varstr
)
2295 t
= type
->t
& VT_TYPE
;
2298 if (t
& VT_CONSTANT
)
2299 pstrcat(buf
, buf_size
, "const ");
2300 if (t
& VT_VOLATILE
)
2301 pstrcat(buf
, buf_size
, "volatile ");
2302 if ((t
& (VT_DEFSIGN
| VT_UNSIGNED
)) == (VT_DEFSIGN
| VT_UNSIGNED
))
2303 pstrcat(buf
, buf_size
, "unsigned ");
2304 else if (t
& VT_DEFSIGN
)
2305 pstrcat(buf
, buf_size
, "signed ");
2335 tstr
= "long double";
2337 pstrcat(buf
, buf_size
, tstr
);
2341 if (bt
== VT_STRUCT
)
2345 pstrcat(buf
, buf_size
, tstr
);
2346 v
= type
->ref
->v
& ~SYM_STRUCT
;
2347 if (v
>= SYM_FIRST_ANOM
)
2348 pstrcat(buf
, buf_size
, "<anonymous>");
2350 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
2354 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
2355 pstrcat(buf
, buf_size
, "(");
2357 while (sa
!= NULL
) {
2358 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
2359 pstrcat(buf
, buf_size
, buf1
);
2362 pstrcat(buf
, buf_size
, ", ");
2364 pstrcat(buf
, buf_size
, ")");
2368 pstrcpy(buf1
, sizeof(buf1
), "*");
2370 pstrcat(buf1
, sizeof(buf1
), varstr
);
2371 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2375 pstrcat(buf
, buf_size
, " ");
2376 pstrcat(buf
, buf_size
, varstr
);
2381 /* verify type compatibility to store vtop in 'dt' type, and generate
2383 static void gen_assign_cast(CType
*dt
)
2385 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
2386 char buf1
[256], buf2
[256];
2389 st
= &vtop
->type
; /* source type */
2390 dbt
= dt
->t
& VT_BTYPE
;
2391 sbt
= st
->t
& VT_BTYPE
;
2392 if (sbt
== VT_VOID
|| dbt
== VT_VOID
)
2393 tcc_error("cannot cast from/to void");
2394 if (dt
->t
& VT_CONSTANT
)
2395 tcc_warning("assignment of read-only location");
2398 /* special cases for pointers */
2399 /* '0' can also be a pointer */
2400 if (is_null_pointer(vtop
))
2402 /* accept implicit pointer to integer cast with warning */
2403 if (is_integer_btype(sbt
)) {
2404 tcc_warning("assignment makes pointer from integer without a cast");
2407 type1
= pointed_type(dt
);
2408 /* a function is implicitely a function pointer */
2409 if (sbt
== VT_FUNC
) {
2410 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
2411 !is_compatible_types(pointed_type(dt
), st
))
2412 tcc_warning("assignment from incompatible pointer type");
2417 type2
= pointed_type(st
);
2418 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2419 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
2420 /* void * can match anything */
2422 /* exact type match, except for unsigned */
2425 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
|
2427 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
|
2429 if (!is_compatible_types(&tmp_type1
, &tmp_type2
))
2430 tcc_warning("assignment from incompatible pointer type");
2432 /* check const and volatile */
2433 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
2434 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
2435 tcc_warning("assignment discards qualifiers from pointer target type");
2441 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
2442 tcc_warning("assignment makes integer from pointer without a cast");
2444 /* XXX: more tests */
2449 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2450 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2451 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2453 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
2454 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
2455 tcc_error("cannot cast '%s' to '%s'", buf1
, buf2
);
2463 /* store vtop in lvalue pushed on stack */
2464 ST_FUNC
void vstore(void)
2466 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
2468 ft
= vtop
[-1].type
.t
;
2469 sbt
= vtop
->type
.t
& VT_BTYPE
;
2470 dbt
= ft
& VT_BTYPE
;
2471 if ((((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2472 (sbt
== VT_INT
&& dbt
== VT_SHORT
))
2473 && !(vtop
->type
.t
& VT_BITFIELD
)) {
2474 /* optimize char/short casts */
2475 delayed_cast
= VT_MUSTCAST
;
2476 vtop
->type
.t
= ft
& (VT_TYPE
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
)));
2477 /* XXX: factorize */
2478 if (ft
& VT_CONSTANT
)
2479 tcc_warning("assignment of read-only location");
2482 if (!(ft
& VT_BITFIELD
))
2483 gen_assign_cast(&vtop
[-1].type
);
2486 if (sbt
== VT_STRUCT
) {
2487 /* if structure, only generate pointer */
2488 /* structure assignment : generate memcpy */
2489 /* XXX: optimize if small size */
2490 if (!nocode_wanted
) {
2491 size
= type_size(&vtop
->type
, &align
);
2495 vtop
->type
.t
= VT_PTR
;
2498 /* address of memcpy() */
2501 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2502 else if(!(align
& 3))
2503 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2506 vpush_global_sym(&func_old_type
, TOK_memcpy
);
2511 vtop
->type
.t
= VT_PTR
;
2520 /* leave source on stack */
2521 } else if (ft
& VT_BITFIELD
) {
2522 /* bitfield store handling */
2523 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2524 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2525 /* remove bit field info to avoid loops */
2526 vtop
[-1].type
.t
= ft
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
2528 /* duplicate source into other register */
2533 if((ft
& VT_BTYPE
) == VT_BOOL
) {
2534 gen_cast(&vtop
[-1].type
);
2535 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2538 /* duplicate destination */
2540 vtop
[-1] = vtop
[-2];
2542 /* mask and shift source */
2543 if((ft
& VT_BTYPE
) != VT_BOOL
) {
2544 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2545 vpushll((1ULL << bit_size
) - 1ULL);
2547 vpushi((1 << bit_size
) - 1);
2553 /* load destination, mask and or with source */
2555 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2556 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2558 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2565 /* pop off shifted source from "duplicate source..." above */
2569 #ifdef CONFIG_TCC_BCHECK
2570 /* bound check case */
2571 if (vtop
[-1].r
& VT_MUSTBOUND
) {
2577 if (!nocode_wanted
) {
2581 #ifdef TCC_TARGET_X86_64
2582 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
2584 } else if ((ft
& VT_BTYPE
) == VT_QFLOAT
) {
2589 r
= gv(rc
); /* generate value */
2590 /* if lvalue was saved on stack, must read it */
2591 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
2593 t
= get_reg(RC_INT
);
2594 #ifdef TCC_TARGET_X86_64
2599 sv
.r
= VT_LOCAL
| VT_LVAL
;
2600 sv
.c
.ul
= vtop
[-1].c
.ul
;
2602 vtop
[-1].r
= t
| VT_LVAL
;
2604 /* two word case handling : store second register at word + 4 (or +8 for x86-64) */
2605 #ifdef TCC_TARGET_X86_64
2606 if (((ft
& VT_BTYPE
) == VT_QLONG
) || ((ft
& VT_BTYPE
) == VT_QFLOAT
)) {
2607 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
2609 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
2610 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
2612 vtop
[-1].type
.t
= load_type
;
2615 /* convert to int to increment easily */
2616 vtop
->type
.t
= addr_type
;
2622 vtop
[-1].type
.t
= load_type
;
2623 /* XXX: it works because r2 is spilled last ! */
2624 store(vtop
->r2
, vtop
- 1);
2630 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
2631 vtop
->r
|= delayed_cast
;
2635 /* post defines POST/PRE add. c is the token ++ or -- */
2636 ST_FUNC
void inc(int post
, int c
)
2639 vdup(); /* save lvalue */
2641 gv_dup(); /* duplicate value */
2646 vpushi(c
- TOK_MID
);
2648 vstore(); /* store value */
2650 vpop(); /* if post op, return saved value */
2653 /* Parse GNUC __attribute__ extension. Currently, the following
2654 extensions are recognized:
2655 - aligned(n) : set data/function alignment.
2656 - packed : force data alignment to 1
2657 - section(x) : generate data/code in this section.
2658 - unused : currently ignored, but may be used someday.
2659 - regparm(n) : pass function parameters in registers (i386 only)
2661 static void parse_attribute(AttributeDef
*ad
)
2665 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
2669 while (tok
!= ')') {
2670 if (tok
< TOK_IDENT
)
2671 expect("attribute name");
2679 expect("section name");
2680 ad
->section
= find_section(tcc_state
, (char *)tokc
.cstr
->data
);
2688 expect("alias(\"target\")");
2689 ad
->alias_target
= /* save string as token, for later */
2690 tok_alloc((char*)tokc
.cstr
->data
, tokc
.cstr
->size
-1)->tok
;
2694 case TOK_VISIBILITY1
:
2695 case TOK_VISIBILITY2
:
2698 expect("visibility(\"default|hidden|internal|protected\")");
2699 if (!strcmp (tokc
.cstr
->data
, "default"))
2700 ad
->a
.visibility
= STV_DEFAULT
;
2701 else if (!strcmp (tokc
.cstr
->data
, "hidden"))
2702 ad
->a
.visibility
= STV_HIDDEN
;
2703 else if (!strcmp (tokc
.cstr
->data
, "internal"))
2704 ad
->a
.visibility
= STV_INTERNAL
;
2705 else if (!strcmp (tokc
.cstr
->data
, "protected"))
2706 ad
->a
.visibility
= STV_PROTECTED
;
2708 expect("visibility(\"default|hidden|internal|protected\")");
2717 if (n
<= 0 || (n
& (n
- 1)) != 0)
2718 tcc_error("alignment must be a positive power of two");
2735 /* currently, no need to handle it because tcc does not
2736 track unused objects */
2740 /* currently, no need to handle it because tcc does not
2741 track unused objects */
2746 ad
->a
.func_call
= FUNC_CDECL
;
2751 ad
->a
.func_call
= FUNC_STDCALL
;
2753 #ifdef TCC_TARGET_I386
2763 ad
->a
.func_call
= FUNC_FASTCALL1
+ n
- 1;
2769 ad
->a
.func_call
= FUNC_FASTCALLW
;
2776 ad
->a
.mode
= VT_LLONG
+ 1;
2779 ad
->a
.mode
= VT_SHORT
+ 1;
2782 ad
->a
.mode
= VT_INT
+ 1;
2785 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
2792 ad
->a
.func_export
= 1;
2795 ad
->a
.func_import
= 1;
2798 if (tcc_state
->warn_unsupported
)
2799 tcc_warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
2800 /* skip parameters */
2802 int parenthesis
= 0;
2806 else if (tok
== ')')
2809 } while (parenthesis
&& tok
!= -1);
2822 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2823 static void struct_decl(CType
*type
, int u
, int tdef
)
2825 int a
, v
, size
, align
, maxalign
, c
, offset
, flexible
;
2826 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
, prevbt
;
2827 Sym
*s
, *ss
, *ass
, **ps
;
2831 a
= tok
; /* save decl type */
2836 /* struct already defined ? return it */
2838 expect("struct/union/enum name");
2842 tcc_error("invalid type: '%s'", get_tok_str(v
, NULL
));
2844 } else if (tok
>= TOK_IDENT
&& !tdef
)
2845 tcc_error("unknown struct/union/enum");
2851 /* we put an undefined size for struct/union */
2852 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
2853 s
->r
= 0; /* default alignment is zero as gcc */
2854 /* put struct/union/enum name in type */
2862 tcc_error("struct/union/enum already defined");
2863 /* cannot be empty */
2865 /* non empty enums are not allowed */
2866 if (a
== TOK_ENUM
) {
2870 expect("identifier");
2872 if (ss
&& !local_stack
)
2873 tcc_error("redefinition of enumerator '%s'",
2874 get_tok_str(v
, NULL
));
2880 /* enum symbols have static storage */
2881 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
2882 ss
->type
.t
|= VT_STATIC
;
2887 /* NOTE: we accept a trailing comma */
2891 s
->c
= type_size(&int_type
, &align
);
2900 while (tok
!= '}') {
2901 parse_btype(&btype
, &ad
);
2904 tcc_error("flexible array member '%s' not at the end of struct",
2905 get_tok_str(v
, NULL
));
2910 type_decl(&type1
, &ad
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
2911 if (v
== 0 && (type1
.t
& VT_BTYPE
) != VT_STRUCT
)
2912 expect("identifier");
2913 if (type_size(&type1
, &align
) < 0) {
2914 if ((a
== TOK_STRUCT
) && (type1
.t
& VT_ARRAY
))
2917 tcc_error("field '%s' has incomplete type",
2918 get_tok_str(v
, NULL
));
2920 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
2921 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
2922 tcc_error("invalid type for '%s'",
2923 get_tok_str(v
, NULL
));
2927 bit_size
= expr_const();
2928 /* XXX: handle v = 0 case for messages */
2930 tcc_error("negative width in bit-field '%s'",
2931 get_tok_str(v
, NULL
));
2932 if (v
&& bit_size
== 0)
2933 tcc_error("zero width for bit-field '%s'",
2934 get_tok_str(v
, NULL
));
2936 size
= type_size(&type1
, &align
);
2938 if (align
< ad
.a
.aligned
)
2939 align
= ad
.a
.aligned
;
2940 } else if (ad
.a
.packed
) {
2942 } else if (*tcc_state
->pack_stack_ptr
) {
2943 if (align
> *tcc_state
->pack_stack_ptr
)
2944 align
= *tcc_state
->pack_stack_ptr
;
2947 if (bit_size
>= 0) {
2948 bt
= type1
.t
& VT_BTYPE
;
2955 tcc_error("bitfields must have scalar type");
2957 if (bit_size
> bsize
) {
2958 tcc_error("width of '%s' exceeds its type",
2959 get_tok_str(v
, NULL
));
2960 } else if (bit_size
== bsize
) {
2961 /* no need for bit fields */
2963 } else if (bit_size
== 0) {
2964 /* XXX: what to do if only padding in a
2966 /* zero size: means to pad */
2969 /* we do not have enough room ?
2970 did the type change?
2972 if ((bit_pos
+ bit_size
) > bsize
||
2973 bt
!= prevbt
|| a
== TOK_UNION
)
2976 /* XXX: handle LSB first */
2977 type1
.t
|= VT_BITFIELD
|
2978 (bit_pos
<< VT_STRUCT_SHIFT
) |
2979 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
2980 bit_pos
+= bit_size
;
2986 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2987 /* add new memory data only if starting
2989 if (lbit_pos
== 0) {
2990 if (a
== TOK_STRUCT
) {
2991 c
= (c
+ align
- 1) & -align
;
3000 if (align
> maxalign
)
3004 printf("add field %s offset=%d",
3005 get_tok_str(v
, NULL
), offset
);
3006 if (type1
.t
& VT_BITFIELD
) {
3007 printf(" pos=%d size=%d",
3008 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
3009 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
3014 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
3016 while ((ass
= ass
->next
) != NULL
) {
3017 ss
= sym_push(ass
->v
, &ass
->type
, 0, offset
+ ass
->c
);
3022 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
3026 if (tok
== ';' || tok
== TOK_EOF
)
3034 tcc_error("flexible array member '%s' in otherwise empty struct", get_tok_str(v
, NULL
));
3035 /* store size and alignment */
3036 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
3042 /* return 1 if basic type is a type size (short, long, long long) */
3043 ST_FUNC
int is_btype_size(int bt
)
3045 return bt
== VT_SHORT
|| bt
== VT_LONG
|| bt
== VT_LLONG
;
3048 /* return 0 if no type declaration. otherwise, return the basic type
3051 static int parse_btype(CType
*type
, AttributeDef
*ad
)
3053 int t
, u
, bt_size
, complete
, type_found
, typespec_found
;
3057 memset(ad
, 0, sizeof(AttributeDef
));
3065 /* currently, we really ignore extension */
3076 tcc_error("too many basic types");
3078 bt_size
= is_btype_size (u
& VT_BTYPE
);
3079 if (u
== VT_INT
|| (!bt_size
&& !(t
& VT_TYPEDEF
)))
3094 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
3095 #ifndef TCC_TARGET_PE
3096 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3098 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
3099 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3113 if ((t
& VT_BTYPE
) == VT_LONG
) {
3114 #ifdef TCC_TARGET_PE
3115 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
3117 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3125 struct_decl(&type1
, VT_ENUM
, t
& VT_TYPEDEF
);
3128 type
->ref
= type1
.ref
;
3132 struct_decl(&type1
, VT_STRUCT
, t
& VT_TYPEDEF
);
3135 /* type modifiers */
3151 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == (VT_DEFSIGN
|VT_UNSIGNED
))
3152 tcc_error("signed and unsigned modifier");
3165 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == VT_DEFSIGN
)
3166 tcc_error("signed and unsigned modifier");
3167 t
|= VT_DEFSIGN
| VT_UNSIGNED
;
3192 /* GNUC attribute */
3193 case TOK_ATTRIBUTE1
:
3194 case TOK_ATTRIBUTE2
:
3195 parse_attribute(ad
);
3198 t
= (t
& ~VT_BTYPE
) | u
;
3206 parse_expr_type(&type1
);
3207 /* remove all storage modifiers except typedef */
3208 type1
.t
&= ~(VT_STORAGE
&~VT_TYPEDEF
);
3214 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
3216 t
|= (s
->type
.t
& ~VT_TYPEDEF
);
3217 type
->ref
= s
->type
.ref
;
3219 /* get attributes from typedef */
3220 if (0 == ad
->a
.aligned
)
3221 ad
->a
.aligned
= s
->a
.aligned
;
3222 if (0 == ad
->a
.func_call
)
3223 ad
->a
.func_call
= s
->a
.func_call
;
3224 ad
->a
.packed
|= s
->a
.packed
;
3233 if (tcc_state
->char_is_unsigned
) {
3234 if ((t
& (VT_DEFSIGN
|VT_BTYPE
)) == VT_BYTE
)
3238 /* long is never used as type */
3239 if ((t
& VT_BTYPE
) == VT_LONG
)
3240 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
3241 t
= (t
& ~VT_BTYPE
) | VT_INT
;
3243 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3249 /* convert a function parameter type (array to pointer and function to
3250 function pointer) */
3251 static inline void convert_parameter_type(CType
*pt
)
3253 /* remove const and volatile qualifiers (XXX: const could be used
3254 to indicate a const function parameter */
3255 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3256 /* array must be transformed to pointer according to ANSI C */
3258 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
3263 ST_FUNC
void parse_asm_str(CString
*astr
)
3266 /* read the string */
3268 expect("string constant");
3270 while (tok
== TOK_STR
) {
3271 /* XXX: add \0 handling too ? */
3272 cstr_cat(astr
, tokc
.cstr
->data
);
3275 cstr_ccat(astr
, '\0');
3278 /* Parse an asm label and return the label
3279 * Don't forget to free the CString in the caller! */
3280 static void asm_label_instr(CString
*astr
)
3283 parse_asm_str(astr
);
3286 printf("asm_alias: \"%s\"\n", (char *)astr
->data
);
3290 static void post_type(CType
*type
, AttributeDef
*ad
)
3292 int n
, l
, t1
, arg_size
, align
;
3293 Sym
**plast
, *s
, *first
;
3298 /* function declaration */
3306 /* read param name and compute offset */
3307 if (l
!= FUNC_OLD
) {
3308 if (!parse_btype(&pt
, &ad1
)) {
3310 tcc_error("invalid type");
3317 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
3319 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3320 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
3321 tcc_error("parameter declared as void");
3322 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
3327 expect("identifier");
3331 convert_parameter_type(&pt
);
3332 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
3338 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
3345 /* if no parameters, then old type prototype */
3349 /* NOTE: const is ignored in returned type as it has a special
3350 meaning in gcc / C++ */
3351 type
->t
&= ~VT_CONSTANT
;
3352 /* some ancient pre-K&R C allows a function to return an array
3353 and the array brackets to be put after the arguments, such
3354 that "int c()[]" means something like "int[] c()" */
3357 skip(']'); /* only handle simple "[]" */
3360 /* we push a anonymous symbol which will contain the function prototype */
3361 ad
->a
.func_args
= arg_size
;
3362 s
= sym_push(SYM_FIELD
, type
, 0, l
);
3367 } else if (tok
== '[') {
3368 /* array definition */
3370 if (tok
== TOK_RESTRICT1
)
3375 if (!local_stack
|| nocode_wanted
)
3376 vpushi(expr_const());
3378 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3381 tcc_error("invalid array size");
3383 if (!is_integer_btype(vtop
->type
.t
& VT_BTYPE
))
3384 tcc_error("size of variable length array should be an integer");
3389 /* parse next post type */
3390 post_type(type
, ad
);
3391 if (type
->t
== VT_FUNC
)
3392 tcc_error("declaration of an array of functions");
3393 t1
|= type
->t
& VT_VLA
;
3396 loc
-= type_size(&int_type
, &align
);
3400 vla_runtime_type_size(type
, &align
);
3402 vset(&int_type
, VT_LOCAL
|VT_LVAL
, loc
);
3409 /* we push an anonymous symbol which will contain the array
3411 s
= sym_push(SYM_FIELD
, type
, 0, n
);
3412 type
->t
= (t1
? VT_VLA
: VT_ARRAY
) | VT_PTR
;
3417 /* Parse a type declaration (except basic type), and return the type
3418 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3419 expected. 'type' should contain the basic type. 'ad' is the
3420 attribute definition of the basic type. It can be modified by
3423 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
3426 CType type1
, *type2
;
3427 int qualifiers
, storage
;
3429 while (tok
== '*') {
3437 qualifiers
|= VT_CONSTANT
;
3442 qualifiers
|= VT_VOLATILE
;
3450 type
->t
|= qualifiers
;
3453 /* XXX: clarify attribute handling */
3454 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3455 parse_attribute(ad
);
3457 /* recursive type */
3458 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3459 type1
.t
= 0; /* XXX: same as int */
3462 /* XXX: this is not correct to modify 'ad' at this point, but
3463 the syntax is not clear */
3464 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3465 parse_attribute(ad
);
3466 type_decl(&type1
, ad
, v
, td
);
3469 /* type identifier */
3470 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
3474 if (!(td
& TYPE_ABSTRACT
))
3475 expect("identifier");
3479 storage
= type
->t
& VT_STORAGE
;
3480 type
->t
&= ~VT_STORAGE
;
3481 if (storage
& VT_STATIC
) {
3482 int saved_nocode_wanted
= nocode_wanted
;
3484 post_type(type
, ad
);
3485 nocode_wanted
= saved_nocode_wanted
;
3487 post_type(type
, ad
);
3489 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3490 parse_attribute(ad
);
3494 /* append type at the end of type1 */
3507 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3508 ST_FUNC
int lvalue_type(int t
)
3513 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
3515 else if (bt
== VT_SHORT
)
3519 if (t
& VT_UNSIGNED
)
3520 r
|= VT_LVAL_UNSIGNED
;
3524 /* indirection with full error checking and bound check */
3525 ST_FUNC
void indir(void)
3527 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
3528 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
3532 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
3534 vtop
->type
= *pointed_type(&vtop
->type
);
3535 /* Arrays and functions are never lvalues */
3536 if (!(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_VLA
)
3537 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3538 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3539 /* if bound checking, the referenced pointer must be checked */
3540 #ifdef CONFIG_TCC_BCHECK
3541 if (tcc_state
->do_bounds_check
)
3542 vtop
->r
|= VT_MUSTBOUND
;
3547 /* pass a parameter to a function and do type checking and casting */
3548 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
3553 func_type
= func
->c
;
3554 if (func_type
== FUNC_OLD
||
3555 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
3556 /* default casting : only need to convert float to double */
3557 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
3560 } else if (vtop
->type
.t
& VT_BITFIELD
) {
3561 type
.t
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
3564 } else if (arg
== NULL
) {
3565 tcc_error("too many arguments to function");
3568 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
3569 gen_assign_cast(&type
);
3573 /* parse an expression of the form '(type)' or '(expr)' and return its
3575 static void parse_expr_type(CType
*type
)
3581 if (parse_btype(type
, &ad
)) {
3582 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3589 static void parse_type(CType
*type
)
3594 if (!parse_btype(type
, &ad
)) {
3597 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3600 static void vpush_tokc(int t
)
3605 vsetc(&type
, VT_CONST
, &tokc
);
3608 ST_FUNC
void unary(void)
3610 int n
, t
, align
, size
, r
, sizeof_caller
;
3614 static int in_sizeof
= 0;
3616 sizeof_caller
= in_sizeof
;
3618 /* XXX: GCC 2.95.3 does not generate a table although it should be
3632 vpush_tokc(VT_INT
| VT_UNSIGNED
);
3636 vpush_tokc(VT_LLONG
);
3640 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
3644 vpush_tokc(VT_FLOAT
);
3648 vpush_tokc(VT_DOUBLE
);
3652 vpush_tokc(VT_LDOUBLE
);
3655 case TOK___FUNCTION__
:
3657 goto tok_identifier
;
3663 /* special function name identifier */
3664 len
= strlen(funcname
) + 1;
3665 /* generate char[len] type */
3670 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
3671 ptr
= section_ptr_add(data_section
, len
);
3672 memcpy(ptr
, funcname
, len
);
3677 #ifdef TCC_TARGET_PE
3678 t
= VT_SHORT
| VT_UNSIGNED
;
3684 /* string parsing */
3687 if (tcc_state
->warn_write_strings
)
3692 memset(&ad
, 0, sizeof(AttributeDef
));
3693 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, NULL
, 0);
3698 if (parse_btype(&type
, &ad
)) {
3699 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
3701 /* check ISOC99 compound literal */
3703 /* data is allocated locally by default */
3708 /* all except arrays are lvalues */
3709 if (!(type
.t
& VT_ARRAY
))
3710 r
|= lvalue_type(type
.t
);
3711 memset(&ad
, 0, sizeof(AttributeDef
));
3712 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, NULL
, 0);
3714 if (sizeof_caller
) {
3721 } else if (tok
== '{') {
3722 /* save all registers */
3724 /* statement expression : we do not accept break/continue
3725 inside as GCC does */
3726 block(NULL
, NULL
, NULL
, NULL
, 0, 1);
3741 /* functions names must be treated as function pointers,
3742 except for unary '&' and sizeof. Since we consider that
3743 functions are not lvalues, we only have to handle it
3744 there and in function calls. */
3745 /* arrays can also be used although they are not lvalues */
3746 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
3747 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
3749 mk_pointer(&vtop
->type
);
3755 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3757 boolean
.t
= VT_BOOL
;
3759 vtop
->c
.i
= !vtop
->c
.i
;
3760 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
3761 vtop
->c
.i
= vtop
->c
.i
^ 1;
3764 vseti(VT_JMP
, gvtst(1, 0));
3776 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
3777 tcc_error("pointer not accepted for unary plus");
3778 /* In order to force cast, we add zero, except for floating point
3779 where we really need an noop (otherwise -0.0 will be transformed
3781 if (!is_float(vtop
->type
.t
)) {
3792 unary_type(&type
); // Perform a in_sizeof = 0;
3793 size
= type_size(&type
, &align
);
3794 if (t
== TOK_SIZEOF
) {
3795 if (!(type
.t
& VT_VLA
)) {
3797 tcc_error("sizeof applied to an incomplete type");
3798 if(type
.t
& VT_BITFIELD
)
3799 tcc_error("'%s' applied to a bit-field", get_tok_str(t
, NULL
));
3802 vla_runtime_type_size(&type
, &align
);
3807 vtop
->type
.t
|= VT_UNSIGNED
;
3810 case TOK_builtin_types_compatible_p
:
3819 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3820 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3821 vpushi(is_compatible_types(&type1
, &type2
));
3824 case TOK_builtin_constant_p
:
3826 int saved_nocode_wanted
, res
;
3829 saved_nocode_wanted
= nocode_wanted
;
3832 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
3834 nocode_wanted
= saved_nocode_wanted
;
3839 case TOK_builtin_frame_address
:
3845 if (tok
!= TOK_CINT
|| tokc
.i
< 0) {
3846 tcc_error("__builtin_frame_address only takes positive integers");
3853 vset(&type
, VT_LOCAL
, 0); /* local frame */
3855 mk_pointer(&vtop
->type
);
3856 indir(); /* -> parent frame */
3860 #ifdef TCC_TARGET_X86_64
3861 #ifdef TCC_TARGET_PE
3862 case TOK_builtin_va_start
:
3870 if ((vtop
->r
& VT_VALMASK
) != VT_LOCAL
)
3871 tcc_error("__builtin_va_start expects a local variable");
3872 vtop
->r
&= ~(VT_LVAL
| VT_REF
);
3873 vtop
->type
= char_pointer_type
;
3878 case TOK_builtin_va_arg_types
:
3885 vpushi(classify_x86_64_va_arg(&type
));
3900 t
= vtop
->type
.t
& VT_BTYPE
;
3902 /* In IEEE negate(x) isn't subtract(0,x), but rather
3907 else if (t
== VT_DOUBLE
)
3918 goto tok_identifier
;
3920 /* allow to take the address of a label */
3921 if (tok
< TOK_UIDENT
)
3922 expect("label identifier");
3923 s
= label_find(tok
);
3925 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
3927 if (s
->r
== LABEL_DECLARED
)
3928 s
->r
= LABEL_FORWARD
;
3931 s
->type
.t
= VT_VOID
;
3932 mk_pointer(&s
->type
);
3933 s
->type
.t
|= VT_STATIC
;
3935 vpushsym(&s
->type
, s
);
3939 // special qnan , snan and infinity values
3941 vpush64(VT_DOUBLE
, 0x7ff8000000000000ULL
);
3945 vpush64(VT_DOUBLE
, 0x7ff0000000000001ULL
);
3949 vpush64(VT_DOUBLE
, 0x7ff0000000000000ULL
);
3958 expect("identifier");
3961 const char *name
= get_tok_str(t
, NULL
);
3963 tcc_error("'%s' undeclared", name
);
3964 /* for simple function calls, we tolerate undeclared
3965 external reference to int() function */
3966 if (tcc_state
->warn_implicit_function_declaration
3967 #ifdef TCC_TARGET_PE
3968 /* people must be warned about using undeclared WINAPI functions
3969 (which usually start with uppercase letter) */
3970 || (name
[0] >= 'A' && name
[0] <= 'Z')
3973 tcc_warning("implicit declaration of function '%s'", name
);
3974 s
= external_global_sym(t
, &func_old_type
, 0);
3976 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
3977 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
3978 /* if referencing an inline function, then we generate a
3979 symbol to it if not already done. It will have the
3980 effect to generate code for it at the end of the
3981 compilation unit. Inline function as always
3982 generated in the text section. */
3984 put_extern_sym(s
, text_section
, 0, 0);
3985 r
= VT_SYM
| VT_CONST
;
3989 vset(&s
->type
, r
, s
->c
);
3990 /* if forward reference, we must point to s */
3991 if (vtop
->r
& VT_SYM
) {
3993 vtop
->c
.ptr_offset
= 0;
3998 /* post operations */
4000 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
4003 } else if (tok
== '.' || tok
== TOK_ARROW
) {
4006 if (tok
== TOK_ARROW
)
4008 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
4012 /* expect pointer on structure */
4013 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
4014 expect("struct or union");
4018 while ((s
= s
->next
) != NULL
) {
4023 tcc_error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, NULL
));
4024 /* add field offset to pointer */
4025 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
4028 /* change type to field type, and set to lvalue */
4029 vtop
->type
= s
->type
;
4030 vtop
->type
.t
|= qualifiers
;
4031 /* an array is never an lvalue */
4032 if (!(vtop
->type
.t
& VT_ARRAY
)) {
4033 vtop
->r
|= lvalue_type(vtop
->type
.t
);
4034 #ifdef CONFIG_TCC_BCHECK
4035 /* if bound checking, the referenced pointer must be checked */
4036 if (tcc_state
->do_bounds_check
)
4037 vtop
->r
|= VT_MUSTBOUND
;
4041 } else if (tok
== '[') {
4047 } else if (tok
== '(') {
4050 int nb_args
, ret_nregs
, ret_align
, variadic
;
4053 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
4054 /* pointer test (no array accepted) */
4055 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
4056 vtop
->type
= *pointed_type(&vtop
->type
);
4057 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
4061 expect("function pointer");
4064 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
4066 /* get return type */
4069 sa
= s
->next
; /* first parameter */
4072 /* compute first implicit argument if a structure is returned */
4073 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
4074 variadic
= (s
->c
== FUNC_ELLIPSIS
);
4075 ret_nregs
= gfunc_sret(&s
->type
, variadic
, &ret
.type
,
4078 /* get some space for the returned structure */
4079 size
= type_size(&s
->type
, &align
);
4080 loc
= (loc
- size
) & -align
;
4082 ret
.r
= VT_LOCAL
| VT_LVAL
;
4083 /* pass it as 'int' to avoid structure arg passing
4085 vseti(VT_LOCAL
, loc
);
4095 /* return in register */
4096 if (is_float(ret
.type
.t
)) {
4097 ret
.r
= reg_fret(ret
.type
.t
);
4098 #ifdef TCC_TARGET_X86_64
4099 if ((ret
.type
.t
& VT_BTYPE
) == VT_QFLOAT
)
4103 #ifdef TCC_TARGET_X86_64
4104 if ((ret
.type
.t
& VT_BTYPE
) == VT_QLONG
)
4106 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
4116 gfunc_param_typed(s
, sa
);
4126 tcc_error("too few arguments to function");
4128 if (!nocode_wanted
) {
4129 gfunc_call(nb_args
);
4131 vtop
-= (nb_args
+ 1);
4135 for (r
= ret
.r
+ ret_nregs
+ !ret_nregs
; r
-- > ret
.r
;) {
4136 vsetc(&ret
.type
, r
, &ret
.c
);
4137 vtop
->r2
= ret
.r2
; /* Loop only happens when r2 is VT_CONST */
4140 /* handle packed struct return */
4141 if (((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) && ret_nregs
) {
4144 size
= type_size(&s
->type
, &align
);
4145 loc
= (loc
- size
) & -align
;
4149 vset(&ret
.type
, VT_LOCAL
| VT_LVAL
, addr
+ offset
);
4153 if (--ret_nregs
== 0)
4155 /* XXX: compatible with arm only: ret_align == register_size */
4156 offset
+= ret_align
;
4158 vset(&s
->type
, VT_LOCAL
| VT_LVAL
, addr
);
4166 ST_FUNC
void expr_prod(void)
4171 while (tok
== '*' || tok
== '/' || tok
== '%') {
4179 ST_FUNC
void expr_sum(void)
4184 while (tok
== '+' || tok
== '-') {
4192 static void expr_shift(void)
4197 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
4205 static void expr_cmp(void)
4210 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
4211 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
4219 static void expr_cmpeq(void)
4224 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
4232 static void expr_and(void)
4235 while (tok
== '&') {
4242 static void expr_xor(void)
4245 while (tok
== '^') {
4252 static void expr_or(void)
4255 while (tok
== '|') {
4262 /* XXX: fix this mess */
4263 static void expr_land_const(void)
4266 while (tok
== TOK_LAND
) {
4273 /* XXX: fix this mess */
4274 static void expr_lor_const(void)
4277 while (tok
== TOK_LOR
) {
4284 /* only used if non constant */
4285 static void expr_land(void)
4290 if (tok
== TOK_LAND
) {
4295 if (tok
!= TOK_LAND
) {
4305 static void expr_lor(void)
4310 if (tok
== TOK_LOR
) {
4315 if (tok
!= TOK_LOR
) {
4325 /* XXX: better constant handling */
4326 static void expr_cond(void)
4328 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
;
4330 CType type
, type1
, type2
;
4337 boolean
.t
= VT_BOOL
;
4343 if (tok
!= ':' || !gnu_ext
) {
4358 if (vtop
!= vstack
) {
4359 /* needed to avoid having different registers saved in
4361 if (is_float(vtop
->type
.t
)) {
4363 #ifdef TCC_TARGET_X86_64
4364 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4374 if (tok
== ':' && gnu_ext
) {
4382 sv
= *vtop
; /* save value to handle it later */
4383 vtop
--; /* no vpop so that FP stack is not flushed */
4391 bt1
= t1
& VT_BTYPE
;
4393 bt2
= t2
& VT_BTYPE
;
4394 /* cast operands to correct type according to ISOC rules */
4395 if (is_float(bt1
) || is_float(bt2
)) {
4396 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
4397 type
.t
= VT_LDOUBLE
;
4398 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
4403 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
4404 /* cast to biggest op */
4406 /* convert to unsigned if it does not fit in a long long */
4407 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
4408 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
4409 type
.t
|= VT_UNSIGNED
;
4410 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
4411 /* If one is a null ptr constant the result type
4413 if (is_null_pointer (vtop
))
4415 else if (is_null_pointer (&sv
))
4417 /* XXX: test pointer compatibility, C99 has more elaborate
4421 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
4422 /* XXX: test function pointer compatibility */
4423 type
= bt1
== VT_FUNC
? type1
: type2
;
4424 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
4425 /* XXX: test structure compatibility */
4426 type
= bt1
== VT_STRUCT
? type1
: type2
;
4427 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
4428 /* NOTE: as an extension, we accept void on only one side */
4431 /* integer operations */
4433 /* convert to unsigned if it does not fit in an integer */
4434 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
4435 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
4436 type
.t
|= VT_UNSIGNED
;
4439 /* now we convert second operand */
4441 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4444 if (is_float(type
.t
)) {
4446 #ifdef TCC_TARGET_X86_64
4447 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4451 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
4452 /* for long longs, we use fixed registers to avoid having
4453 to handle a complicated move */
4458 /* this is horrible, but we must also convert first
4462 /* put again first value and cast it */
4465 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4468 move_reg(r2
, r1
, type
.t
);
4475 static void expr_eq(void)
4481 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
4482 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
4483 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
4498 ST_FUNC
void gexpr(void)
4509 /* parse an expression and return its type without any side effect. */
4510 static void expr_type(CType
*type
)
4512 int saved_nocode_wanted
;
4514 saved_nocode_wanted
= nocode_wanted
;
4519 nocode_wanted
= saved_nocode_wanted
;
4522 /* parse a unary expression and return its type without any side
4524 static void unary_type(CType
*type
)
4536 /* parse a constant expression and return value in vtop. */
4537 static void expr_const1(void)
4546 /* parse an integer constant and return its value. */
4547 ST_FUNC
int expr_const(void)
4551 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
4552 expect("constant expression");
4558 /* return the label token if current token is a label, otherwise
4560 static int is_label(void)
4564 /* fast test first */
4565 if (tok
< TOK_UIDENT
)
4567 /* no need to save tokc because tok is an identifier */
4574 unget_tok(last_tok
);
4579 static void label_or_decl(int l
)
4583 /* fast test first */
4584 if (tok
>= TOK_UIDENT
)
4586 /* no need to save tokc because tok is an identifier */
4590 unget_tok(last_tok
);
4593 unget_tok(last_tok
);
4598 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
4599 int case_reg
, int is_expr
)
4602 Sym
*s
, *frame_bottom
;
4604 /* generate line number info */
4605 if (tcc_state
->do_debug
&&
4606 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
4607 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
4609 last_line_num
= file
->line_num
;
4613 /* default return value is (void) */
4615 vtop
->type
.t
= VT_VOID
;
4618 if (tok
== TOK_IF
) {
4625 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4627 if (c
== TOK_ELSE
) {
4631 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4632 gsym(d
); /* patch else jmp */
4635 } else if (tok
== TOK_WHILE
) {
4643 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4647 } else if (tok
== '{') {
4649 int block_vla_sp_loc
, *saved_vla_sp_loc
, saved_vla_flags
;
4652 /* record local declaration stack position */
4654 frame_bottom
= sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4655 frame_bottom
->next
= scope_stack_bottom
;
4656 scope_stack_bottom
= frame_bottom
;
4657 llabel
= local_label_stack
;
4659 /* save VLA state */
4660 block_vla_sp_loc
= *(saved_vla_sp_loc
= vla_sp_loc
);
4661 if (saved_vla_sp_loc
!= &vla_sp_root_loc
)
4662 vla_sp_loc
= &block_vla_sp_loc
;
4664 saved_vla_flags
= vla_flags
;
4665 vla_flags
|= VLA_NEED_NEW_FRAME
;
4667 /* handle local labels declarations */
4668 if (tok
== TOK_LABEL
) {
4671 if (tok
< TOK_UIDENT
)
4672 expect("label identifier");
4673 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
4683 while (tok
!= '}') {
4684 label_or_decl(VT_LOCAL
);
4688 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4691 /* pop locally defined labels */
4692 label_pop(&local_label_stack
, llabel
);
4694 /* XXX: this solution makes only valgrind happy...
4695 triggered by gcc.c-torture/execute/20000917-1.c */
4697 switch(vtop
->type
.t
& VT_BTYPE
) {
4702 for(p
=vtop
->type
.ref
;p
;p
=p
->prev
)
4704 tcc_error("unsupported expression type");
4707 /* pop locally defined symbols */
4708 scope_stack_bottom
= scope_stack_bottom
->next
;
4709 sym_pop(&local_stack
, s
);
4711 /* Pop VLA frames and restore stack pointer if required */
4712 if (saved_vla_sp_loc
!= &vla_sp_root_loc
)
4713 *saved_vla_sp_loc
= block_vla_sp_loc
;
4714 if (vla_sp_loc
!= (saved_vla_sp_loc
== &vla_sp_root_loc
? &vla_sp_root_loc
: &block_vla_sp_loc
)) {
4715 vla_sp_loc
= saved_vla_sp_loc
;
4716 gen_vla_sp_restore(*vla_sp_loc
);
4718 vla_flags
= (vla_flags
& ~VLA_SCOPE_FLAGS
) | (saved_vla_flags
& VLA_SCOPE_FLAGS
);
4721 } else if (tok
== TOK_RETURN
) {
4725 gen_assign_cast(&func_vt
);
4726 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
4727 CType type
, ret_type
;
4728 int ret_align
, ret_nregs
;
4729 ret_nregs
= gfunc_sret(&func_vt
, func_var
, &ret_type
,
4731 if (0 == ret_nregs
) {
4732 /* if returning structure, must copy it to implicit
4733 first pointer arg location */
4736 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
4739 /* copy structure value to pointer */
4742 /* returning structure packed into registers */
4743 int r
, size
, addr
, align
;
4744 size
= type_size(&func_vt
,&align
);
4745 if ((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) || (vtop
->c
.i
& (ret_align
-1)))
4746 && (align
& (ret_align
-1))) {
4747 loc
= (loc
- size
) & -align
;
4750 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
4753 vset(&ret_type
, VT_LOCAL
| VT_LVAL
, addr
);
4755 vtop
->type
= ret_type
;
4756 if (is_float(ret_type
.t
))
4757 r
= rc_fret(ret_type
.t
);
4763 if (--ret_nregs
== 0)
4765 /* We assume that when a structure is returned in multiple
4766 registers, their classes are consecutive values of the
4769 /* XXX: compatible with arm only: ret_align == register_size */
4770 vtop
->c
.i
+= ret_align
;
4771 vtop
->r
= VT_LOCAL
| VT_LVAL
;
4774 } else if (is_float(func_vt
.t
)) {
4775 gv(rc_fret(func_vt
.t
));
4779 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
4782 rsym
= gjmp(rsym
); /* jmp */
4783 } else if (tok
== TOK_BREAK
) {
4786 tcc_error("cannot break");
4787 *bsym
= gjmp(*bsym
);
4790 } else if (tok
== TOK_CONTINUE
) {
4793 tcc_error("cannot continue");
4794 *csym
= gjmp(*csym
);
4797 } else if (tok
== TOK_FOR
) {
4802 frame_bottom
= sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4803 frame_bottom
->next
= scope_stack_bottom
;
4804 scope_stack_bottom
= frame_bottom
;
4806 /* c99 for-loop init decl? */
4807 if (!decl0(VT_LOCAL
, 1)) {
4808 /* no, regular for-loop init expr */
4832 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4836 scope_stack_bottom
= scope_stack_bottom
->next
;
4837 sym_pop(&local_stack
, s
);
4839 if (tok
== TOK_DO
) {
4844 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4855 if (tok
== TOK_SWITCH
) {
4859 /* XXX: other types than integer */
4860 case_reg
= gv(RC_INT
);
4864 b
= gjmp(0); /* jump to first case */
4866 block(&a
, csym
, &b
, &c
, case_reg
, 0);
4867 /* if no default, jmp after switch */
4875 if (tok
== TOK_CASE
) {
4882 if (gnu_ext
&& tok
== TOK_DOTS
) {
4886 tcc_warning("empty case range");
4888 /* since a case is like a label, we must skip it with a jmp */
4895 *case_sym
= gtst(1, 0);
4898 *case_sym
= gtst(1, 0);
4902 *case_sym
= gtst(1, *case_sym
);
4907 goto block_after_label
;
4909 if (tok
== TOK_DEFAULT
) {
4915 tcc_error("too many 'default'");
4918 goto block_after_label
;
4920 if (tok
== TOK_GOTO
) {
4922 if (tok
== '*' && gnu_ext
) {
4926 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
4929 } else if (tok
>= TOK_UIDENT
) {
4930 s
= label_find(tok
);
4931 /* put forward definition if needed */
4933 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4935 if (s
->r
== LABEL_DECLARED
)
4936 s
->r
= LABEL_FORWARD
;
4938 /* label already defined */
4939 if (vla_flags
& VLA_IN_SCOPE
) {
4940 /* If VLAs are in use, save the current stack pointer and
4941 reset the stack pointer to what it was at function entry
4942 (label will restore stack pointer in inner scopes) */
4944 gen_vla_sp_restore(vla_sp_root_loc
);
4946 if (s
->r
& LABEL_FORWARD
)
4947 s
->jnext
= gjmp(s
->jnext
);
4949 gjmp_addr(s
->jnext
);
4952 expect("label identifier");
4955 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
4961 if (vla_flags
& VLA_IN_SCOPE
) {
4962 /* save/restore stack pointer across label
4963 this is a no-op when combined with the load immediately
4964 after the label unless we arrive via goto */
4969 if (s
->r
== LABEL_DEFINED
)
4970 tcc_error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
4972 s
->r
= LABEL_DEFINED
;
4974 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
4977 if (vla_flags
& VLA_IN_SCOPE
) {
4978 gen_vla_sp_restore(*vla_sp_loc
);
4979 vla_flags
|= VLA_NEED_NEW_FRAME
;
4981 /* we accept this, but it is a mistake */
4984 tcc_warning("deprecated use of label at end of compound statement");
4988 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4991 /* expression case */
5006 /* t is the array or struct type. c is the array or struct
5007 address. cur_index/cur_field is the pointer to the current
5008 value. 'size_only' is true if only size info is needed (only used
5010 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
5011 int *cur_index
, Sym
**cur_field
,
5015 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
5021 if (gnu_ext
&& (l
= is_label()) != 0)
5023 while (tok
== '[' || tok
== '.') {
5025 if (!(type
->t
& VT_ARRAY
))
5026 expect("array type");
5029 index
= expr_const();
5030 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
5031 expect("invalid index");
5032 if (tok
== TOK_DOTS
&& gnu_ext
) {
5034 index_last
= expr_const();
5035 if (index_last
< 0 ||
5036 (s
->c
>= 0 && index_last
>= s
->c
) ||
5038 expect("invalid index");
5044 *cur_index
= index_last
;
5045 type
= pointed_type(type
);
5046 elem_size
= type_size(type
, &align
);
5047 c
+= index
* elem_size
;
5048 /* NOTE: we only support ranges for last designator */
5049 nb_elems
= index_last
- index
+ 1;
5050 if (nb_elems
!= 1) {
5059 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
5060 expect("struct/union type");
5073 /* XXX: fix this mess by using explicit storage field */
5075 type1
.t
|= (type
->t
& ~VT_TYPE
);
5089 if (type
->t
& VT_ARRAY
) {
5091 type
= pointed_type(type
);
5092 c
+= index
* type_size(type
, &align
);
5096 tcc_error("too many field init");
5097 /* XXX: fix this mess by using explicit storage field */
5099 type1
.t
|= (type
->t
& ~VT_TYPE
);
5104 decl_initializer(type
, sec
, c
, 0, size_only
);
5106 /* XXX: make it more general */
5107 if (!size_only
&& nb_elems
> 1) {
5108 unsigned long c_end
;
5113 tcc_error("range init not supported yet for dynamic storage");
5114 c_end
= c
+ nb_elems
* elem_size
;
5115 if (c_end
> sec
->data_allocated
)
5116 section_realloc(sec
, c_end
);
5117 src
= sec
->data
+ c
;
5119 for(i
= 1; i
< nb_elems
; i
++) {
5121 memcpy(dst
, src
, elem_size
);
5127 #define EXPR_CONST 1
5130 /* store a value or an expression directly in global data or in local array */
5131 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
5132 int v
, int expr_type
)
5134 int saved_global_expr
, bt
, bit_pos
, bit_size
;
5136 unsigned long long bit_mask
;
5144 /* compound literals must be allocated globally in this case */
5145 saved_global_expr
= global_expr
;
5148 global_expr
= saved_global_expr
;
5149 /* NOTE: symbols are accepted */
5150 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
5151 tcc_error("initializer element is not constant");
5159 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
5162 /* XXX: not portable */
5163 /* XXX: generate error if incorrect relocation */
5164 gen_assign_cast(&dtype
);
5165 bt
= type
->t
& VT_BTYPE
;
5166 /* we'll write at most 12 bytes */
5167 if (c
+ 12 > sec
->data_allocated
) {
5168 section_realloc(sec
, c
+ 12);
5170 ptr
= sec
->data
+ c
;
5171 /* XXX: make code faster ? */
5172 if (!(type
->t
& VT_BITFIELD
)) {
5177 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5178 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
5179 bit_mask
= (1LL << bit_size
) - 1;
5181 if ((vtop
->r
& VT_SYM
) &&
5187 (bt
== VT_INT
&& bit_size
!= 32)))
5188 tcc_error("initializer element is not computable at load time");
5191 vtop
->c
.i
= (vtop
->c
.i
!= 0);
5193 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5196 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5199 *(double *)ptr
= vtop
->c
.d
;
5202 *(long double *)ptr
= vtop
->c
.ld
;
5205 *(long long *)ptr
|= (vtop
->c
.ll
& bit_mask
) << bit_pos
;
5208 if (vtop
->r
& VT_SYM
) {
5209 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
5211 *(addr_t
*)ptr
|= (vtop
->c
.ptr_offset
& bit_mask
) << bit_pos
;
5214 if (vtop
->r
& VT_SYM
) {
5215 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
5217 *(int *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5222 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
5229 /* put zeros for variable based init */
5230 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
5233 /* nothing to do because globals are already set to zero */
5235 vpush_global_sym(&func_old_type
, TOK_memset
);
5237 #ifdef TCC_TARGET_ARM
5248 /* 't' contains the type and storage info. 'c' is the offset of the
5249 object in section 'sec'. If 'sec' is NULL, it means stack based
5250 allocation. 'first' is true if array '{' must be read (multi
5251 dimension implicit array init handling). 'size_only' is true if
5252 size only evaluation is wanted (only for arrays). */
5253 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
5254 int first
, int size_only
)
5256 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, parlevel1
, i
;
5257 int size1
, align1
, expr_type
;
5261 if (type
->t
& VT_VLA
) {
5264 /* save current stack pointer */
5265 if (vla_flags
& VLA_NEED_NEW_FRAME
) {
5267 vla_flags
= VLA_IN_SCOPE
;
5268 vla_sp_loc
= &vla_sp_loc_tmp
;
5271 vla_runtime_type_size(type
, &a
);
5272 gen_vla_alloc(type
, a
);
5273 vset(type
, VT_LOCAL
|VT_LVAL
, c
);
5277 } else if (type
->t
& VT_ARRAY
) {
5281 t1
= pointed_type(type
);
5282 size1
= type_size(t1
, &align1
);
5285 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
5288 tcc_error("character array initializer must be a literal,"
5289 " optionally enclosed in braces");
5294 /* only parse strings here if correct type (otherwise: handle
5295 them as ((w)char *) expressions */
5296 if ((tok
== TOK_LSTR
&&
5297 #ifdef TCC_TARGET_PE
5298 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
5300 (t1
->t
& VT_BTYPE
) == VT_INT
5302 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
5303 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5308 /* compute maximum number of chars wanted */
5310 cstr_len
= cstr
->size
;
5312 cstr_len
= cstr
->size
/ sizeof(nwchar_t
);
5315 if (n
>= 0 && nb
> (n
- array_length
))
5316 nb
= n
- array_length
;
5319 tcc_warning("initializer-string for array is too long");
5320 /* in order to go faster for common case (char
5321 string in global variable, we handle it
5323 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
5324 memcpy(sec
->data
+ c
+ array_length
, cstr
->data
, nb
);
5328 ch
= ((unsigned char *)cstr
->data
)[i
];
5330 ch
= ((nwchar_t
*)cstr
->data
)[i
];
5331 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
5339 /* only add trailing zero if enough storage (no
5340 warning in this case since it is standard) */
5341 if (n
< 0 || array_length
< n
) {
5343 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
5349 while (tok
!= '}') {
5350 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
5351 if (n
>= 0 && index
>= n
)
5352 tcc_error("index too large");
5353 /* must put zero in holes (note that doing it that way
5354 ensures that it even works with designators) */
5355 if (!size_only
&& array_length
< index
) {
5356 init_putz(t1
, sec
, c
+ array_length
* size1
,
5357 (index
- array_length
) * size1
);
5360 if (index
> array_length
)
5361 array_length
= index
;
5362 /* special test for multi dimensional arrays (may not
5363 be strictly correct if designators are used at the
5365 if (index
>= n
&& no_oblock
)
5374 /* put zeros at the end */
5375 if (!size_only
&& n
>= 0 && array_length
< n
) {
5376 init_putz(t1
, sec
, c
+ array_length
* size1
,
5377 (n
- array_length
) * size1
);
5379 /* patch type size if needed */
5381 s
->c
= array_length
;
5382 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
5383 (sec
|| !first
|| tok
== '{')) {
5386 /* NOTE: the previous test is a specific case for automatic
5387 struct/union init */
5388 /* XXX: union needs only one init */
5390 /* XXX: this test is incorrect for local initializers
5391 beginning with ( without {. It would be much more difficult
5392 to do it correctly (ideally, the expression parser should
5393 be used in all cases) */
5399 while (tok
== '(') {
5403 if (!parse_btype(&type1
, &ad1
))
5405 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
5407 if (!is_assignable_types(type
, &type1
))
5408 tcc_error("invalid type for cast");
5413 if (first
|| tok
== '{') {
5422 while (tok
!= '}') {
5423 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
5425 if (!size_only
&& array_length
< index
) {
5426 init_putz(type
, sec
, c
+ array_length
,
5427 index
- array_length
);
5429 index
= index
+ type_size(&f
->type
, &align1
);
5430 if (index
> array_length
)
5431 array_length
= index
;
5433 /* gr: skip fields from same union - ugly. */
5435 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
5436 /* test for same offset */
5437 if (f
->next
->c
!= f
->c
)
5439 /* if yes, test for bitfield shift */
5440 if ((f
->type
.t
& VT_BITFIELD
) && (f
->next
->type
.t
& VT_BITFIELD
)) {
5441 int bit_pos_1
= (f
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5442 int bit_pos_2
= (f
->next
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5443 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
5444 if (bit_pos_1
!= bit_pos_2
)
5451 if (no_oblock
&& f
== NULL
)
5457 /* put zeros at the end */
5458 if (!size_only
&& array_length
< n
) {
5459 init_putz(type
, sec
, c
+ array_length
,
5468 } else if (tok
== '{') {
5470 decl_initializer(type
, sec
, c
, first
, size_only
);
5472 } else if (size_only
) {
5473 /* just skip expression */
5474 parlevel
= parlevel1
= 0;
5475 while ((parlevel
> 0 || parlevel1
> 0 ||
5476 (tok
!= '}' && tok
!= ',')) && tok
!= -1) {
5479 else if (tok
== ')')
5481 else if (tok
== '{')
5483 else if (tok
== '}')
5488 /* currently, we always use constant expression for globals
5489 (may change for scripting case) */
5490 expr_type
= EXPR_CONST
;
5492 expr_type
= EXPR_ANY
;
5493 init_putv(type
, sec
, c
, 0, expr_type
);
5497 /* parse an initializer for type 't' if 'has_init' is non zero, and
5498 allocate space in local or global data space ('r' is either
5499 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5500 variable 'v' with an associated name represented by 'asm_label' of
5501 scope 'scope' is declared before initializers are parsed. If 'v' is
5502 zero, then a reference to the new object is put in the value stack.
5503 If 'has_init' is 2, a special parsing is done to handle string
5505 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
5506 int has_init
, int v
, char *asm_label
,
5509 int size
, align
, addr
, data_offset
;
5511 ParseState saved_parse_state
= {0};
5512 TokenString init_str
;
5514 Sym
*flexible_array
;
5516 flexible_array
= NULL
;
5517 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
5518 Sym
*field
= type
->ref
->next
;
5521 field
= field
->next
;
5522 if (field
->type
.t
& VT_ARRAY
&& field
->type
.ref
->c
< 0)
5523 flexible_array
= field
;
5527 size
= type_size(type
, &align
);
5528 /* If unknown size, we must evaluate it before
5529 evaluating initializers because
5530 initializers can generate global data too
5531 (e.g. string pointers or ISOC99 compound
5532 literals). It also simplifies local
5533 initializers handling */
5534 tok_str_new(&init_str
);
5535 if (size
< 0 || (flexible_array
&& has_init
)) {
5537 tcc_error("unknown type size");
5538 /* get all init string */
5539 if (has_init
== 2) {
5540 /* only get strings */
5541 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5542 tok_str_add_tok(&init_str
);
5547 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
5549 tcc_error("unexpected end of file in initializer");
5550 tok_str_add_tok(&init_str
);
5553 else if (tok
== '}') {
5563 tok_str_add(&init_str
, -1);
5564 tok_str_add(&init_str
, 0);
5567 save_parse_state(&saved_parse_state
);
5569 macro_ptr
= init_str
.str
;
5571 decl_initializer(type
, NULL
, 0, 1, 1);
5572 /* prepare second initializer parsing */
5573 macro_ptr
= init_str
.str
;
5576 /* if still unknown size, error */
5577 size
= type_size(type
, &align
);
5579 tcc_error("unknown type size");
5582 size
+= flexible_array
->type
.ref
->c
* pointed_size(&flexible_array
->type
);
5583 /* take into account specified alignment if bigger */
5584 if (ad
->a
.aligned
) {
5585 if (ad
->a
.aligned
> align
)
5586 align
= ad
->a
.aligned
;
5587 } else if (ad
->a
.packed
) {
5590 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
5592 #ifdef CONFIG_TCC_BCHECK
5593 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5597 loc
= (loc
- size
) & -align
;
5599 #ifdef CONFIG_TCC_BCHECK
5600 /* handles bounds */
5601 /* XXX: currently, since we do only one pass, we cannot track
5602 '&' operators, so we add only arrays */
5603 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5604 unsigned long *bounds_ptr
;
5605 /* add padding between regions */
5607 /* then add local bound info */
5608 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(unsigned long));
5609 bounds_ptr
[0] = addr
;
5610 bounds_ptr
[1] = size
;
5614 /* local variable */
5615 sym_push(v
, type
, r
, addr
);
5617 /* push local reference */
5618 vset(type
, r
, addr
);
5624 if (v
&& scope
== VT_CONST
) {
5625 /* see if the symbol was already defined */
5628 if (!is_compatible_types(&sym
->type
, type
))
5629 tcc_error("incompatible types for redefinition of '%s'",
5630 get_tok_str(v
, NULL
));
5631 if (sym
->type
.t
& VT_EXTERN
) {
5632 /* if the variable is extern, it was not allocated */
5633 sym
->type
.t
&= ~VT_EXTERN
;
5634 /* set array size if it was omitted in extern
5636 if ((sym
->type
.t
& VT_ARRAY
) &&
5637 sym
->type
.ref
->c
< 0 &&
5639 sym
->type
.ref
->c
= type
->ref
->c
;
5641 /* we accept several definitions of the same
5642 global variable. this is tricky, because we
5643 must play with the SHN_COMMON type of the symbol */
5644 /* XXX: should check if the variable was already
5645 initialized. It is incorrect to initialized it
5647 /* no init data, we won't add more to the symbol */
5654 /* allocate symbol in corresponding section */
5659 else if (tcc_state
->nocommon
)
5663 data_offset
= sec
->data_offset
;
5664 data_offset
= (data_offset
+ align
- 1) & -align
;
5666 /* very important to increment global pointer at this time
5667 because initializers themselves can create new initializers */
5668 data_offset
+= size
;
5669 #ifdef CONFIG_TCC_BCHECK
5670 /* add padding if bound check */
5671 if (tcc_state
->do_bounds_check
)
5674 sec
->data_offset
= data_offset
;
5675 /* allocate section space to put the data */
5676 if (sec
->sh_type
!= SHT_NOBITS
&&
5677 data_offset
> sec
->data_allocated
)
5678 section_realloc(sec
, data_offset
);
5679 /* align section if needed */
5680 if (align
> sec
->sh_addralign
)
5681 sec
->sh_addralign
= align
;
5683 addr
= 0; /* avoid warning */
5687 if (scope
!= VT_CONST
|| !sym
) {
5688 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
5689 sym
->asm_label
= asm_label
;
5691 /* update symbol definition */
5693 put_extern_sym(sym
, sec
, addr
, size
);
5696 /* put a common area */
5697 put_extern_sym(sym
, NULL
, align
, size
);
5698 /* XXX: find a nicer way */
5699 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
5700 esym
->st_shndx
= SHN_COMMON
;
5703 /* push global reference */
5704 sym
= get_sym_ref(type
, sec
, addr
, size
);
5705 vpushsym(type
, sym
);
5707 /* patch symbol weakness */
5708 if (type
->t
& VT_WEAK
)
5710 apply_visibility(sym
, type
);
5711 #ifdef CONFIG_TCC_BCHECK
5712 /* handles bounds now because the symbol must be defined
5713 before for the relocation */
5714 if (tcc_state
->do_bounds_check
) {
5715 unsigned long *bounds_ptr
;
5717 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
5718 /* then add global bound info */
5719 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(long));
5720 bounds_ptr
[0] = 0; /* relocated */
5721 bounds_ptr
[1] = size
;
5725 if (has_init
|| (type
->t
& VT_VLA
)) {
5726 decl_initializer(type
, sec
, addr
, 1, 0);
5727 /* restore parse state if needed */
5729 tok_str_free(init_str
.str
);
5730 restore_parse_state(&saved_parse_state
);
5732 /* patch flexible array member size back to -1, */
5733 /* for possible subsequent similar declarations */
5735 flexible_array
->type
.ref
->c
= -1;
5740 static void put_func_debug(Sym
*sym
)
5745 /* XXX: we put here a dummy type */
5746 snprintf(buf
, sizeof(buf
), "%s:%c1",
5747 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
5748 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
5749 cur_text_section
, sym
->c
);
5750 /* //gr gdb wants a line at the function */
5751 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
5756 /* parse an old style function declaration list */
5757 /* XXX: check multiple parameter */
5758 static void func_decl_list(Sym
*func_sym
)
5765 /* parse each declaration */
5766 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
&&
5767 tok
!= TOK_ASM1
&& tok
!= TOK_ASM2
&& tok
!= TOK_ASM3
) {
5768 if (!parse_btype(&btype
, &ad
))
5769 expect("declaration list");
5770 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5771 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5773 /* we accept no variable after */
5777 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5778 /* find parameter in function parameter list */
5781 if ((s
->v
& ~SYM_FIELD
) == v
)
5785 tcc_error("declaration for parameter '%s' but no such parameter",
5786 get_tok_str(v
, NULL
));
5788 /* check that no storage specifier except 'register' was given */
5789 if (type
.t
& VT_STORAGE
)
5790 tcc_error("storage class specified for '%s'", get_tok_str(v
, NULL
));
5791 convert_parameter_type(&type
);
5792 /* we can add the type (NOTE: it could be local to the function) */
5794 /* accept other parameters */
5805 /* parse a function defined by symbol 'sym' and generate its code in
5806 'cur_text_section' */
5807 static void gen_function(Sym
*sym
)
5809 int saved_nocode_wanted
= nocode_wanted
;
5811 ind
= cur_text_section
->data_offset
;
5812 /* NOTE: we patch the symbol size later */
5813 put_extern_sym(sym
, cur_text_section
, ind
, 0);
5814 funcname
= get_tok_str(sym
->v
, NULL
);
5816 /* Initialize VLA state */
5817 vla_sp_loc
= &vla_sp_root_loc
;
5818 vla_flags
= VLA_NEED_NEW_FRAME
;
5819 /* put debug symbol */
5820 if (tcc_state
->do_debug
)
5821 put_func_debug(sym
);
5822 /* push a dummy symbol to enable local sym storage */
5823 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
5824 gfunc_prolog(&sym
->type
);
5825 #ifdef CONFIG_TCC_BCHECK
5826 if (tcc_state
->do_bounds_check
5827 && !strcmp(get_tok_str(sym
->v
, NULL
), "main")) {
5831 for (i
= 0, sym
= local_stack
; i
< 2; i
++, sym
= sym
->prev
) {
5832 if (sym
->v
& SYM_FIELD
|| sym
->prev
->v
& SYM_FIELD
)
5834 vpush_global_sym(&func_old_type
, TOK___bound_main_arg
);
5835 vset(&sym
->type
, sym
->r
, sym
->c
);
5841 block(NULL
, NULL
, NULL
, NULL
, 0, 0);
5844 cur_text_section
->data_offset
= ind
;
5845 label_pop(&global_label_stack
, NULL
);
5846 /* reset local stack */
5847 scope_stack_bottom
= NULL
;
5848 sym_pop(&local_stack
, NULL
);
5849 /* end of function */
5850 /* patch symbol size */
5851 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
5853 /* patch symbol weakness (this definition overrules any prototype) */
5854 if (sym
->type
.t
& VT_WEAK
)
5856 apply_visibility(sym
, &sym
->type
);
5857 if (tcc_state
->do_debug
) {
5858 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
5860 /* It's better to crash than to generate wrong code */
5861 cur_text_section
= NULL
;
5862 funcname
= ""; /* for safety */
5863 func_vt
.t
= VT_VOID
; /* for safety */
5864 func_var
= 0; /* for safety */
5865 ind
= 0; /* for safety */
5866 nocode_wanted
= saved_nocode_wanted
;
5869 ST_FUNC
void gen_inline_functions(void)
5872 int *str
, inline_generated
, i
;
5873 struct InlineFunc
*fn
;
5875 /* iterate while inline function are referenced */
5877 inline_generated
= 0;
5878 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5879 fn
= tcc_state
->inline_fns
[i
];
5881 if (sym
&& sym
->c
) {
5882 /* the function was used: generate its code and
5883 convert it to a normal function */
5884 str
= fn
->token_str
;
5887 pstrcpy(file
->filename
, sizeof file
->filename
, fn
->filename
);
5888 sym
->r
= VT_SYM
| VT_CONST
;
5889 sym
->type
.t
&= ~VT_INLINE
;
5893 cur_text_section
= text_section
;
5895 macro_ptr
= NULL
; /* fail safe */
5897 inline_generated
= 1;
5900 if (!inline_generated
)
5903 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5904 fn
= tcc_state
->inline_fns
[i
];
5905 str
= fn
->token_str
;
5908 dynarray_reset(&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
);
5911 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
5912 static int decl0(int l
, int is_for_loop_init
)
5920 if (!parse_btype(&btype
, &ad
)) {
5921 if (is_for_loop_init
)
5923 /* skip redundant ';' */
5924 /* XXX: find more elegant solution */
5929 if (l
== VT_CONST
&&
5930 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5931 /* global asm block */
5935 /* special test for old K&R protos without explicit int
5936 type. Only accepted when defining global data */
5937 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
5941 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5942 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5944 /* we accept no variable after */
5948 while (1) { /* iterate thru each declaration */
5949 char *asm_label
; // associated asm label
5951 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5955 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
5956 printf("type = '%s'\n", buf
);
5959 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5960 if ((type
.t
& VT_STATIC
) && (l
== VT_LOCAL
)) {
5961 tcc_error("function without file scope cannot be static");
5963 /* if old style function prototype, we accept a
5966 if (sym
->c
== FUNC_OLD
)
5967 func_decl_list(sym
);
5971 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5974 asm_label_instr(&astr
);
5975 asm_label
= tcc_strdup(astr
.data
);
5978 /* parse one last attribute list, after asm label */
5979 parse_attribute(&ad
);
5984 #ifdef TCC_TARGET_PE
5985 if (ad
.a
.func_import
)
5986 type
.t
|= VT_IMPORT
;
5987 if (ad
.a
.func_export
)
5988 type
.t
|= VT_EXPORT
;
5990 type
.t
|= ad
.a
.visibility
<< VT_VIS_SHIFT
;
5994 tcc_error("cannot use local functions");
5995 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
5996 expect("function definition");
5998 /* reject abstract declarators in function definition */
6000 while ((sym
= sym
->next
) != NULL
)
6001 if (!(sym
->v
& ~SYM_FIELD
))
6002 expect("identifier");
6004 /* XXX: cannot do better now: convert extern line to static inline */
6005 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
6006 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
6011 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
6014 ref
= sym
->type
.ref
;
6015 if (0 == ref
->a
.func_proto
)
6016 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
6018 /* use func_call from prototype if not defined */
6019 if (ref
->a
.func_call
!= FUNC_CDECL
6020 && type
.ref
->a
.func_call
== FUNC_CDECL
)
6021 type
.ref
->a
.func_call
= ref
->a
.func_call
;
6023 /* use export from prototype */
6024 if (ref
->a
.func_export
)
6025 type
.ref
->a
.func_export
= 1;
6027 /* use static from prototype */
6028 if (sym
->type
.t
& VT_STATIC
)
6029 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
6031 /* If the definition has no visibility use the
6032 one from prototype. */
6033 if (! (type
.t
& VT_VIS_MASK
))
6034 type
.t
|= sym
->type
.t
& VT_VIS_MASK
;
6036 if (!is_compatible_types(&sym
->type
, &type
)) {
6038 tcc_error("incompatible types for redefinition of '%s'",
6039 get_tok_str(v
, NULL
));
6041 type
.ref
->a
.func_proto
= 0;
6042 /* if symbol is already defined, then put complete type */
6045 /* put function symbol */
6046 sym
= global_identifier_push(v
, type
.t
, 0);
6047 sym
->type
.ref
= type
.ref
;
6050 /* static inline functions are just recorded as a kind
6051 of macro. Their code will be emitted at the end of
6052 the compilation unit only if they are used */
6053 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
6054 (VT_INLINE
| VT_STATIC
)) {
6055 TokenString func_str
;
6057 struct InlineFunc
*fn
;
6058 const char *filename
;
6060 tok_str_new(&func_str
);
6066 tcc_error("unexpected end of file");
6067 tok_str_add_tok(&func_str
);
6072 } else if (t
== '}') {
6074 if (block_level
== 0)
6078 tok_str_add(&func_str
, -1);
6079 tok_str_add(&func_str
, 0);
6080 filename
= file
? file
->filename
: "";
6081 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
6082 strcpy(fn
->filename
, filename
);
6084 fn
->token_str
= func_str
.str
;
6085 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
6088 /* compute text section */
6089 cur_text_section
= ad
.section
;
6090 if (!cur_text_section
)
6091 cur_text_section
= text_section
;
6092 sym
->r
= VT_SYM
| VT_CONST
;
6097 if (btype
.t
& VT_TYPEDEF
) {
6098 /* save typedefed type */
6099 /* XXX: test storage specifiers ? */
6100 sym
= sym_push(v
, &type
, 0, 0);
6102 sym
->type
.t
|= VT_TYPEDEF
;
6105 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
6106 /* external function definition */
6107 /* specific case for func_call attribute */
6108 ad
.a
.func_proto
= 1;
6110 } else if (!(type
.t
& VT_ARRAY
)) {
6111 /* not lvalue if array */
6112 r
|= lvalue_type(type
.t
);
6114 has_init
= (tok
== '=');
6115 if (has_init
&& (type
.t
& VT_VLA
))
6116 tcc_error("Variable length array cannot be initialized");
6117 if ((btype
.t
& VT_EXTERN
) || ((type
.t
& VT_BTYPE
) == VT_FUNC
) ||
6118 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
6119 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
6120 /* external variable or function */
6121 /* NOTE: as GCC, uninitialized global static
6122 arrays of null size are considered as
6124 sym
= external_sym(v
, &type
, r
, asm_label
);
6126 if (ad
.alias_target
) {
6131 alias_target
= sym_find(ad
.alias_target
);
6132 if (!alias_target
|| !alias_target
->c
)
6133 tcc_error("unsupported forward __alias__ attribute");
6134 esym
= &((Elf32_Sym
*)symtab_section
->data
)[alias_target
->c
];
6135 tsec
.sh_num
= esym
->st_shndx
;
6136 put_extern_sym2(sym
, &tsec
, esym
->st_value
, esym
->st_size
, 0);
6139 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
6140 if (type
.t
& VT_STATIC
)
6146 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, asm_label
, l
);
6150 if (is_for_loop_init
)
6163 ST_FUNC
void decl(int l
)