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
);
94 ST_INLN
int is_float(int t
)
98 return bt
== VT_LDOUBLE
|| bt
== VT_DOUBLE
|| bt
== VT_FLOAT
|| bt
== VT_QFLOAT
;
101 /* we use our own 'finite' function to avoid potential problems with
102 non standard math libs */
103 /* XXX: endianness dependent */
104 ST_FUNC
int ieee_finite(double d
)
107 memcpy(p
, &d
, sizeof(double));
108 return ((unsigned)((p
[1] | 0x800fffff) + 1)) >> 31;
111 ST_FUNC
void test_lvalue(void)
113 if (!(vtop
->r
& VT_LVAL
))
117 /* ------------------------------------------------------------------------- */
118 /* symbol allocator */
119 static Sym
*__sym_malloc(void)
121 Sym
*sym_pool
, *sym
, *last_sym
;
124 sym_pool
= tcc_malloc(SYM_POOL_NB
* sizeof(Sym
));
125 dynarray_add(&sym_pools
, &nb_sym_pools
, sym_pool
);
127 last_sym
= sym_free_first
;
129 for(i
= 0; i
< SYM_POOL_NB
; i
++) {
130 sym
->next
= last_sym
;
134 sym_free_first
= last_sym
;
138 static inline Sym
*sym_malloc(void)
141 sym
= sym_free_first
;
143 sym
= __sym_malloc();
144 sym_free_first
= sym
->next
;
148 ST_INLN
void sym_free(Sym
*sym
)
150 sym
->next
= sym_free_first
;
151 tcc_free(sym
->asm_label
);
152 sym_free_first
= sym
;
155 /* push, without hashing */
156 ST_FUNC Sym
*sym_push2(Sym
**ps
, int v
, int t
, long c
)
159 if (ps
== &local_stack
) {
160 for (s
= *ps
; s
&& s
!= scope_stack_bottom
; s
= s
->prev
)
161 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
&& s
->v
== v
)
162 tcc_error("incompatible types for redefinition of '%s'",
163 get_tok_str(v
, NULL
));
181 /* find a symbol and return its associated structure. 's' is the top
182 of the symbol stack */
183 ST_FUNC Sym
*sym_find2(Sym
*s
, int v
)
193 /* structure lookup */
194 ST_INLN Sym
*struct_find(int v
)
197 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
199 return table_ident
[v
]->sym_struct
;
202 /* find an identifier */
203 ST_INLN Sym
*sym_find(int v
)
206 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
208 return table_ident
[v
]->sym_identifier
;
211 /* push a given symbol on the symbol stack */
212 ST_FUNC Sym
*sym_push(int v
, CType
*type
, int r
, int c
)
221 s
= sym_push2(ps
, v
, type
->t
, c
);
222 s
->type
.ref
= type
->ref
;
224 /* don't record fields or anonymous symbols */
226 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
227 /* record symbol in token array */
228 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
230 ps
= &ts
->sym_struct
;
232 ps
= &ts
->sym_identifier
;
239 /* push a global identifier */
240 ST_FUNC Sym
*global_identifier_push(int v
, int t
, int c
)
243 s
= sym_push2(&global_stack
, v
, t
, c
);
244 /* don't record anonymous symbol */
245 if (v
< SYM_FIRST_ANOM
) {
246 ps
= &table_ident
[v
- TOK_IDENT
]->sym_identifier
;
247 /* modify the top most local identifier, so that
248 sym_identifier will point to 's' when popped */
250 ps
= &(*ps
)->prev_tok
;
257 /* pop symbols until top reaches 'b' */
258 ST_FUNC
void sym_pop(Sym
**ptop
, Sym
*b
)
268 /* remove symbol in token array */
270 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
271 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
273 ps
= &ts
->sym_struct
;
275 ps
= &ts
->sym_identifier
;
284 static void weaken_symbol(Sym
*sym
)
286 sym
->type
.t
|= VT_WEAK
;
291 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
292 esym_type
= ELFW(ST_TYPE
)(esym
->st_info
);
293 esym
->st_info
= ELFW(ST_INFO
)(STB_WEAK
, esym_type
);
297 /* ------------------------------------------------------------------------- */
299 ST_FUNC
void swap(int *p
, int *q
)
307 static void vsetc(CType
*type
, int r
, CValue
*vc
)
311 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
312 tcc_error("memory full (vstack)");
313 /* cannot let cpu flags if other instruction are generated. Also
314 avoid leaving VT_JMP anywhere except on the top of the stack
315 because it would complicate the code generator. */
316 if (vtop
>= vstack
) {
317 v
= vtop
->r
& VT_VALMASK
;
318 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
328 /* push constant of type "type" with useless value */
329 void vpush(CType
*type
)
332 memset(&cval
, 0, sizeof(CValue
));
333 vsetc(type
, VT_CONST
, &cval
);
336 /* push integer constant */
337 ST_FUNC
void vpushi(int v
)
340 memset(&cval
, 0, sizeof(CValue
));
342 vsetc(&int_type
, VT_CONST
, &cval
);
345 /* push a pointer sized constant */
346 static void vpushs(long long v
)
349 memset(&cval
, 0, sizeof(CValue
));
354 vsetc(&size_type
, VT_CONST
, &cval
);
357 /* push arbitrary 64bit constant */
358 void vpush64(int ty
, unsigned long long v
)
362 memset(&cval
, 0, sizeof(CValue
));
366 vsetc(&ctype
, VT_CONST
, &cval
);
369 /* push long long constant */
370 static inline void vpushll(long long v
)
372 vpush64(VT_LLONG
, v
);
375 /* push a symbol value of TYPE */
376 static inline void vpushsym(CType
*type
, Sym
*sym
)
379 memset(&cval
, 0, sizeof(CValue
));
382 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
386 /* Return a static symbol pointing to a section */
387 ST_FUNC Sym
*get_sym_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
393 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
394 sym
->type
.ref
= type
->ref
;
395 sym
->r
= VT_CONST
| VT_SYM
;
396 put_extern_sym(sym
, sec
, offset
, size
);
400 /* push a reference to a section offset by adding a dummy symbol */
401 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
403 vpushsym(type
, get_sym_ref(type
, sec
, offset
, size
));
406 /* define a new external reference to a symbol 'v' of type 'u' */
407 ST_FUNC Sym
*external_global_sym(int v
, CType
*type
, int r
)
413 /* push forward reference */
414 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
415 s
->type
.ref
= type
->ref
;
416 s
->r
= r
| VT_CONST
| VT_SYM
;
421 /* define a new external reference to a symbol 'v' with alternate asm
422 name 'asm_label' of type 'u'. 'asm_label' is equal to NULL if there
423 is no alternate name (most cases) */
424 static Sym
*external_sym(int v
, CType
*type
, int r
, char *asm_label
)
430 /* push forward reference */
431 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
432 s
->asm_label
= asm_label
;
433 s
->type
.t
|= VT_EXTERN
;
434 } else if (s
->type
.ref
== func_old_type
.ref
) {
435 s
->type
.ref
= type
->ref
;
436 s
->r
= r
| VT_CONST
| VT_SYM
;
437 s
->type
.t
|= VT_EXTERN
;
438 } else if (!is_compatible_types(&s
->type
, type
)) {
439 tcc_error("incompatible types for redefinition of '%s'",
440 get_tok_str(v
, NULL
));
445 /* push a reference to global symbol v */
446 ST_FUNC
void vpush_global_sym(CType
*type
, int v
)
448 vpushsym(type
, external_global_sym(v
, type
, 0));
451 ST_FUNC
void vset(CType
*type
, int r
, int v
)
454 memset(&cval
, 0, sizeof(CValue
));
457 vsetc(type
, r
, &cval
);
460 static void vseti(int r
, int v
)
468 ST_FUNC
void vswap(void)
471 /* cannot let cpu flags if other instruction are generated. Also
472 avoid leaving VT_JMP anywhere except on the top of the stack
473 because it would complicate the code generator. */
474 if (vtop
>= vstack
) {
475 int v
= vtop
->r
& VT_VALMASK
;
476 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
483 /* XXX: +2% overall speed possible with optimized memswap
485 * memswap(&vtop[0], &vtop[1], sizeof *vtop);
489 ST_FUNC
void vpushv(SValue
*v
)
491 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
492 tcc_error("memory full (vstack)");
497 static void vdup(void)
502 /* save r to the memory stack, and mark it as being free */
503 ST_FUNC
void save_reg(int r
)
505 int l
, saved
, size
, align
;
509 /* modify all stack values */
512 for(p
=vstack
;p
<=vtop
;p
++) {
513 if ((p
->r
& VT_VALMASK
) == r
||
514 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
515 /* must save value on stack if not already done */
517 /* NOTE: must reload 'r' because r might be equal to r2 */
518 r
= p
->r
& VT_VALMASK
;
519 /* store register in the stack */
521 if ((p
->r
& VT_LVAL
) ||
522 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
523 #ifdef TCC_TARGET_X86_64
524 type
= &char_pointer_type
;
528 size
= type_size(type
, &align
);
529 loc
= (loc
- size
) & -align
;
531 sv
.r
= VT_LOCAL
| VT_LVAL
;
534 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
535 /* x86 specific: need to pop fp register ST0 if saved */
537 o(0xd8dd); /* fstp %st(0) */
540 #ifndef TCC_TARGET_X86_64
541 /* special long long case */
542 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
550 /* mark that stack entry as being saved on the stack */
551 if (p
->r
& VT_LVAL
) {
552 /* also clear the bounded flag because the
553 relocation address of the function was stored in
555 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
557 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
565 #ifdef TCC_TARGET_ARM
566 /* find a register of class 'rc2' with at most one reference on stack.
567 * If none, call get_reg(rc) */
568 ST_FUNC
int get_reg_ex(int rc
, int rc2
)
573 for(r
=0;r
<NB_REGS
;r
++) {
574 if (reg_classes
[r
] & rc2
) {
577 for(p
= vstack
; p
<= vtop
; p
++) {
578 if ((p
->r
& VT_VALMASK
) == r
||
579 (p
->r2
& VT_VALMASK
) == r
)
590 /* find a free register of class 'rc'. If none, save one register */
591 ST_FUNC
int get_reg(int rc
)
596 /* find a free register */
597 for(r
=0;r
<NB_REGS
;r
++) {
598 if (reg_classes
[r
] & rc
) {
599 for(p
=vstack
;p
<=vtop
;p
++) {
600 if ((p
->r
& VT_VALMASK
) == r
||
601 (p
->r2
& VT_VALMASK
) == r
)
609 /* no register left : free the first one on the stack (VERY
610 IMPORTANT to start from the bottom to ensure that we don't
611 spill registers used in gen_opi()) */
612 for(p
=vstack
;p
<=vtop
;p
++) {
613 /* look at second register (if long long) */
614 r
= p
->r2
& VT_VALMASK
;
615 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
617 r
= p
->r
& VT_VALMASK
;
618 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
624 /* Should never comes here */
628 /* save registers up to (vtop - n) stack entry */
629 ST_FUNC
void save_regs(int n
)
634 for(p
= vstack
;p
<= p1
; p
++) {
635 r
= p
->r
& VT_VALMASK
;
642 /* move register 's' (of type 't') to 'r', and flush previous value of r to memory
644 static void move_reg(int r
, int s
, int t
)
658 /* get address of vtop (vtop MUST BE an lvalue) */
659 static void gaddrof(void)
661 if (vtop
->r
& VT_REF
)
664 /* tricky: if saved lvalue, then we can go back to lvalue */
665 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
666 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
671 #ifdef CONFIG_TCC_BCHECK
672 /* generate lvalue bound code */
673 static void gbound(void)
678 vtop
->r
&= ~VT_MUSTBOUND
;
679 /* if lvalue, then use checking code before dereferencing */
680 if (vtop
->r
& VT_LVAL
) {
681 /* if not VT_BOUNDED value, then make one */
682 if (!(vtop
->r
& VT_BOUNDED
)) {
683 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
684 /* must save type because we must set it to int to get pointer */
686 vtop
->type
.t
= VT_INT
;
689 gen_bounded_ptr_add();
690 vtop
->r
|= lval_type
;
693 /* then check for dereferencing */
694 gen_bounded_ptr_deref();
699 /* store vtop a register belonging to class 'rc'. lvalues are
700 converted to values. Cannot be used if cannot be converted to
701 register value (such as structures). */
702 ST_FUNC
int gv(int rc
)
704 int r
, bit_pos
, bit_size
, size
, align
, i
;
707 /* NOTE: get_reg can modify vstack[] */
708 if (vtop
->type
.t
& VT_BITFIELD
) {
711 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
712 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
713 /* remove bit field info to avoid loops */
714 vtop
->type
.t
&= ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
715 /* cast to int to propagate signedness in following ops */
716 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
721 if((vtop
->type
.t
& VT_UNSIGNED
) ||
722 (vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
723 type
.t
|= VT_UNSIGNED
;
725 /* generate shifts */
726 vpushi(bits
- (bit_pos
+ bit_size
));
728 vpushi(bits
- bit_size
);
729 /* NOTE: transformed to SHR if unsigned */
733 if (is_float(vtop
->type
.t
) &&
734 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
737 unsigned long offset
;
738 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
740 memset(&check
, 0, sizeof(CValue
));
743 /* XXX: unify with initializers handling ? */
744 /* CPUs usually cannot use float constants, so we store them
745 generically in data segment */
746 size
= type_size(&vtop
->type
, &align
);
747 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
748 data_section
->data_offset
= offset
;
749 /* XXX: not portable yet */
750 #if defined(__i386__) || defined(__x86_64__)
751 /* Zero pad x87 tenbyte long doubles */
752 if (size
== LDOUBLE_SIZE
) {
753 vtop
->c
.tab
[2] &= 0xffff;
754 #if LDOUBLE_SIZE == 16
759 ptr
= section_ptr_add(data_section
, size
);
761 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
765 ptr
[i
] = vtop
->c
.tab
[size
-1-i
];
769 ptr
[i
] = vtop
->c
.tab
[i
];
770 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
771 vtop
->r
|= VT_LVAL
| VT_SYM
;
775 #ifdef CONFIG_TCC_BCHECK
776 if (vtop
->r
& VT_MUSTBOUND
)
780 r
= vtop
->r
& VT_VALMASK
;
781 rc2
= (rc
& RC_FLOAT
) ? RC_FLOAT
: RC_INT
;
784 #ifdef TCC_TARGET_X86_64
785 else if (rc
== RC_FRET
)
789 /* need to reload if:
791 - lvalue (need to dereference pointer)
792 - already a register, but not in the right class */
794 || (vtop
->r
& VT_LVAL
)
795 || !(reg_classes
[r
] & rc
)
796 #ifdef TCC_TARGET_X86_64
797 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
798 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
&& !(reg_classes
[vtop
->r2
] & rc2
))
800 || ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
805 #ifdef TCC_TARGET_X86_64
806 if (((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
)) {
807 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
809 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
810 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
811 unsigned long long ll
;
813 int r2
, original_type
;
814 original_type
= vtop
->type
.t
;
815 /* two register type load : expand to two words
817 #ifndef TCC_TARGET_X86_64
818 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
821 vtop
->c
.ui
= ll
; /* first word */
823 vtop
->r
= r
; /* save register value */
824 vpushi(ll
>> 32); /* second word */
827 if (r
>= VT_CONST
|| /* XXX: test to VT_CONST incorrect ? */
828 (vtop
->r
& VT_LVAL
)) {
829 /* We do not want to modifier the long long
830 pointer here, so the safest (and less
831 efficient) is to save all the other registers
832 in the stack. XXX: totally inefficient. */
834 /* load from memory */
835 vtop
->type
.t
= load_type
;
838 vtop
[-1].r
= r
; /* save register value */
839 /* increment pointer to get second word */
840 vtop
->type
.t
= addr_type
;
845 vtop
->type
.t
= load_type
;
850 vtop
[-1].r
= r
; /* save register value */
851 vtop
->r
= vtop
[-1].r2
;
853 /* Allocate second register. Here we rely on the fact that
854 get_reg() tries first to free r2 of an SValue. */
858 /* write second register */
860 vtop
->type
.t
= original_type
;
861 } else if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
863 /* lvalue of scalar type : need to use lvalue type
864 because of possible cast */
867 /* compute memory access type */
868 if (vtop
->r
& VT_REF
)
869 #ifdef TCC_TARGET_X86_64
874 else if (vtop
->r
& VT_LVAL_BYTE
)
876 else if (vtop
->r
& VT_LVAL_SHORT
)
878 if (vtop
->r
& VT_LVAL_UNSIGNED
)
882 /* restore wanted type */
885 /* one register type load */
890 #ifdef TCC_TARGET_C67
891 /* uses register pairs for doubles */
892 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
899 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
900 ST_FUNC
void gv2(int rc1
, int rc2
)
904 /* generate more generic register first. But VT_JMP or VT_CMP
905 values must be generated first in all cases to avoid possible
907 v
= vtop
[0].r
& VT_VALMASK
;
908 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
913 /* test if reload is needed for first register */
914 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
924 /* test if reload is needed for first register */
925 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
931 /* wrapper around RC_FRET to return a register by type */
932 static int rc_fret(int t
)
934 #ifdef TCC_TARGET_X86_64
935 if (t
== VT_LDOUBLE
) {
942 /* wrapper around REG_FRET to return a register by type */
943 static int reg_fret(int t
)
945 #ifdef TCC_TARGET_X86_64
946 if (t
== VT_LDOUBLE
) {
953 /* expand long long on stack in two int registers */
954 static void lexpand(void)
958 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
961 vtop
[0].r
= vtop
[-1].r2
;
962 vtop
[0].r2
= VT_CONST
;
963 vtop
[-1].r2
= VT_CONST
;
964 vtop
[0].type
.t
= VT_INT
| u
;
965 vtop
[-1].type
.t
= VT_INT
| u
;
968 #ifdef TCC_TARGET_ARM
969 /* expand long long on stack */
970 ST_FUNC
void lexpand_nr(void)
974 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
977 vtop
->type
.t
= VT_INT
| u
;
978 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
980 vtop
[-1].c
.ui
= vtop
->c
.ull
;
981 vtop
->c
.ui
= vtop
->c
.ull
>> 32;
983 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
985 vtop
->r
= vtop
[-1].r
;
986 } else if (v
> VT_CONST
) {
990 vtop
->r
= vtop
[-1].r2
;
991 vtop
[-1].r2
= VT_CONST
;
992 vtop
[-1].type
.t
= VT_INT
| u
;
996 /* build a long long from two ints */
997 static void lbuild(int t
)
1000 vtop
[-1].r2
= vtop
[0].r
;
1001 vtop
[-1].type
.t
= t
;
1005 /* rotate n first stack elements to the bottom
1006 I1 ... In -> I2 ... In I1 [top is right]
1008 ST_FUNC
void vrotb(int n
)
1014 for(i
=-n
+1;i
!=0;i
++)
1015 vtop
[i
] = vtop
[i
+1];
1019 /* rotate the n elements before entry e towards the top
1020 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
1022 ST_FUNC
void vrote(SValue
*e
, int n
)
1028 for(i
= 0;i
< n
- 1; i
++)
1033 /* rotate n first stack elements to the top
1034 I1 ... In -> In I1 ... I(n-1) [top is right]
1036 ST_FUNC
void vrott(int n
)
1041 /* pop stack value */
1042 ST_FUNC
void vpop(void)
1045 v
= vtop
->r
& VT_VALMASK
;
1046 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1047 /* for x86, we need to pop the FP stack */
1048 if (v
== TREG_ST0
&& !nocode_wanted
) {
1049 o(0xd8dd); /* fstp %st(0) */
1052 if (v
== VT_JMP
|| v
== VT_JMPI
) {
1053 /* need to put correct jump if && or || without test */
1059 /* convert stack entry to register and duplicate its value in another
1061 static void gv_dup(void)
1067 if ((t
& VT_BTYPE
) == VT_LLONG
) {
1074 /* stack: H L L1 H1 */
1082 /* duplicate value */
1087 #ifdef TCC_TARGET_X86_64
1088 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1098 load(r1
, &sv
); /* move r to r1 */
1100 /* duplicates value */
1106 /* Generate value test
1108 * Generate a test for any value (jump, comparison and integers) */
1109 int gvtst(int inv
, int t
)
1111 int v
= vtop
->r
& VT_VALMASK
;
1112 if (v
!= VT_CMP
&& v
!= VT_JMP
&& v
!= VT_JMPI
) {
1116 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1117 /* constant jmp optimization */
1118 if ((vtop
->c
.i
!= 0) != inv
)
1123 return gtst(inv
, t
);
1126 #ifndef TCC_TARGET_X86_64
1127 /* generate CPU independent (unsigned) long long operations */
1128 static void gen_opl(int op
)
1130 int t
, a
, b
, op1
, c
, i
;
1132 unsigned short reg_iret
= REG_IRET
;
1133 unsigned short reg_lret
= REG_LRET
;
1139 func
= TOK___divdi3
;
1142 func
= TOK___udivdi3
;
1145 func
= TOK___moddi3
;
1148 func
= TOK___umoddi3
;
1155 /* call generic long long function */
1156 vpush_global_sym(&func_old_type
, func
);
1161 vtop
->r2
= reg_lret
;
1174 /* stack: L1 H1 L2 H2 */
1179 vtop
[-2] = vtop
[-3];
1182 /* stack: H1 H2 L1 L2 */
1188 /* stack: H1 H2 L1 L2 ML MH */
1191 /* stack: ML MH H1 H2 L1 L2 */
1195 /* stack: ML MH H1 L2 H2 L1 */
1200 /* stack: ML MH M1 M2 */
1203 } else if (op
== '+' || op
== '-') {
1204 /* XXX: add non carry method too (for MIPS or alpha) */
1210 /* stack: H1 H2 (L1 op L2) */
1213 gen_op(op1
+ 1); /* TOK_xxxC2 */
1216 /* stack: H1 H2 (L1 op L2) */
1219 /* stack: (L1 op L2) H1 H2 */
1221 /* stack: (L1 op L2) (H1 op H2) */
1229 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1230 t
= vtop
[-1].type
.t
;
1234 /* stack: L H shift */
1236 /* constant: simpler */
1237 /* NOTE: all comments are for SHL. the other cases are
1238 done by swaping words */
1249 if (op
!= TOK_SAR
) {
1282 /* XXX: should provide a faster fallback on x86 ? */
1285 func
= TOK___ashrdi3
;
1288 func
= TOK___lshrdi3
;
1291 func
= TOK___ashldi3
;
1297 /* compare operations */
1303 /* stack: L1 H1 L2 H2 */
1305 vtop
[-1] = vtop
[-2];
1307 /* stack: L1 L2 H1 H2 */
1310 /* when values are equal, we need to compare low words. since
1311 the jump is inverted, we invert the test too. */
1314 else if (op1
== TOK_GT
)
1316 else if (op1
== TOK_ULT
)
1318 else if (op1
== TOK_UGT
)
1323 if (op1
!= TOK_NE
) {
1327 /* generate non equal test */
1328 /* XXX: NOT PORTABLE yet */
1332 #if defined(TCC_TARGET_I386)
1333 b
= psym(0x850f, 0);
1334 #elif defined(TCC_TARGET_ARM)
1336 o(0x1A000000 | encbranch(ind
, 0, 1));
1337 #elif defined(TCC_TARGET_C67)
1338 tcc_error("not implemented");
1340 #error not supported
1344 /* compare low. Always unsigned */
1348 else if (op1
== TOK_LE
)
1350 else if (op1
== TOK_GT
)
1352 else if (op1
== TOK_GE
)
1363 /* handle integer constant optimizations and various machine
1365 static void gen_opic(int op
)
1367 int c1
, c2
, t1
, t2
, n
;
1370 typedef unsigned long long U
;
1374 t1
= v1
->type
.t
& VT_BTYPE
;
1375 t2
= v2
->type
.t
& VT_BTYPE
;
1379 else if (v1
->type
.t
& VT_UNSIGNED
)
1386 else if (v2
->type
.t
& VT_UNSIGNED
)
1391 /* currently, we cannot do computations with forward symbols */
1392 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1393 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1396 case '+': l1
+= l2
; break;
1397 case '-': l1
-= l2
; break;
1398 case '&': l1
&= l2
; break;
1399 case '^': l1
^= l2
; break;
1400 case '|': l1
|= l2
; break;
1401 case '*': l1
*= l2
; break;
1408 /* if division by zero, generate explicit division */
1411 tcc_error("division by zero in constant");
1415 default: l1
/= l2
; break;
1416 case '%': l1
%= l2
; break;
1417 case TOK_UDIV
: l1
= (U
)l1
/ l2
; break;
1418 case TOK_UMOD
: l1
= (U
)l1
% l2
; break;
1421 case TOK_SHL
: l1
<<= l2
; break;
1422 case TOK_SHR
: l1
= (U
)l1
>> l2
; break;
1423 case TOK_SAR
: l1
>>= l2
; break;
1425 case TOK_ULT
: l1
= (U
)l1
< (U
)l2
; break;
1426 case TOK_UGE
: l1
= (U
)l1
>= (U
)l2
; break;
1427 case TOK_EQ
: l1
= l1
== l2
; break;
1428 case TOK_NE
: l1
= l1
!= l2
; break;
1429 case TOK_ULE
: l1
= (U
)l1
<= (U
)l2
; break;
1430 case TOK_UGT
: l1
= (U
)l1
> (U
)l2
; break;
1431 case TOK_LT
: l1
= l1
< l2
; break;
1432 case TOK_GE
: l1
= l1
>= l2
; break;
1433 case TOK_LE
: l1
= l1
<= l2
; break;
1434 case TOK_GT
: l1
= l1
> l2
; break;
1436 case TOK_LAND
: l1
= l1
&& l2
; break;
1437 case TOK_LOR
: l1
= l1
|| l2
; break;
1444 /* if commutative ops, put c2 as constant */
1445 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1446 op
== '|' || op
== '*')) {
1448 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1449 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1451 /* Filter out NOP operations like x*1, x-0, x&-1... */
1452 if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1455 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1456 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1462 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1463 /* try to use shifts instead of muls or divs */
1464 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1473 else if (op
== TOK_PDIV
)
1479 } else if (c2
&& (op
== '+' || op
== '-') &&
1480 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
1481 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1482 /* symbol + constant case */
1489 if (!nocode_wanted
) {
1490 /* call low level op generator */
1491 if (t1
== VT_LLONG
|| t2
== VT_LLONG
)
1502 /* generate a floating point operation with constant propagation */
1503 static void gen_opif(int op
)
1511 /* currently, we cannot do computations with forward symbols */
1512 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1513 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1515 if (v1
->type
.t
== VT_FLOAT
) {
1518 } else if (v1
->type
.t
== VT_DOUBLE
) {
1526 /* NOTE: we only do constant propagation if finite number (not
1527 NaN or infinity) (ANSI spec) */
1528 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1532 case '+': f1
+= f2
; break;
1533 case '-': f1
-= f2
; break;
1534 case '*': f1
*= f2
; break;
1538 tcc_error("division by zero in constant");
1543 /* XXX: also handles tests ? */
1547 /* XXX: overflow test ? */
1548 if (v1
->type
.t
== VT_FLOAT
) {
1550 } else if (v1
->type
.t
== VT_DOUBLE
) {
1558 if (!nocode_wanted
) {
1566 static int pointed_size(CType
*type
)
1569 return type_size(pointed_type(type
), &align
);
1572 static void vla_runtime_pointed_size(CType
*type
)
1575 vla_runtime_type_size(pointed_type(type
), &align
);
1578 static inline int is_null_pointer(SValue
*p
)
1580 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1582 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& p
->c
.i
== 0) ||
1583 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.ll
== 0) ||
1584 ((p
->type
.t
& VT_BTYPE
) == VT_PTR
&& p
->c
.ptr
== 0);
1587 static inline int is_integer_btype(int bt
)
1589 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
1590 bt
== VT_INT
|| bt
== VT_LLONG
);
1593 /* check types for comparison or substraction of pointers */
1594 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
1596 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
1599 /* null pointers are accepted for all comparisons as gcc */
1600 if (is_null_pointer(p1
) || is_null_pointer(p2
))
1604 bt1
= type1
->t
& VT_BTYPE
;
1605 bt2
= type2
->t
& VT_BTYPE
;
1606 /* accept comparison between pointer and integer with a warning */
1607 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
1608 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
1609 tcc_warning("comparison between pointer and integer");
1613 /* both must be pointers or implicit function pointers */
1614 if (bt1
== VT_PTR
) {
1615 type1
= pointed_type(type1
);
1616 } else if (bt1
!= VT_FUNC
)
1617 goto invalid_operands
;
1619 if (bt2
== VT_PTR
) {
1620 type2
= pointed_type(type2
);
1621 } else if (bt2
!= VT_FUNC
) {
1623 tcc_error("invalid operands to binary %s", get_tok_str(op
, NULL
));
1625 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1626 (type2
->t
& VT_BTYPE
) == VT_VOID
)
1630 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1631 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1632 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1633 /* gcc-like error if '-' is used */
1635 goto invalid_operands
;
1637 tcc_warning("comparison of distinct pointer types lacks a cast");
1641 /* generic gen_op: handles types problems */
1642 ST_FUNC
void gen_op(int op
)
1644 int u
, t1
, t2
, bt1
, bt2
, t
;
1647 t1
= vtop
[-1].type
.t
;
1648 t2
= vtop
[0].type
.t
;
1649 bt1
= t1
& VT_BTYPE
;
1650 bt2
= t2
& VT_BTYPE
;
1652 if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
1653 /* at least one operand is a pointer */
1654 /* relationnal op: must be both pointers */
1655 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
1656 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1657 /* pointers are handled are unsigned */
1658 #ifdef TCC_TARGET_X86_64
1659 t
= VT_LLONG
| VT_UNSIGNED
;
1661 t
= VT_INT
| VT_UNSIGNED
;
1665 /* if both pointers, then it must be the '-' op */
1666 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
1668 tcc_error("cannot use pointers here");
1669 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1670 /* XXX: check that types are compatible */
1671 if (vtop
[-1].type
.t
& VT_VLA
) {
1672 vla_runtime_pointed_size(&vtop
[-1].type
);
1674 vpushi(pointed_size(&vtop
[-1].type
));
1678 /* set to integer type */
1679 #ifdef TCC_TARGET_X86_64
1680 vtop
->type
.t
= VT_LLONG
;
1682 vtop
->type
.t
= VT_INT
;
1687 /* exactly one pointer : must be '+' or '-'. */
1688 if (op
!= '-' && op
!= '+')
1689 tcc_error("cannot use pointers here");
1690 /* Put pointer as first operand */
1691 if (bt2
== VT_PTR
) {
1695 type1
= vtop
[-1].type
;
1696 type1
.t
&= ~VT_ARRAY
;
1697 if (vtop
[-1].type
.t
& VT_VLA
)
1698 vla_runtime_pointed_size(&vtop
[-1].type
);
1700 u
= pointed_size(&vtop
[-1].type
);
1702 tcc_error("unknown array element size");
1703 #ifdef TCC_TARGET_X86_64
1706 /* XXX: cast to int ? (long long case) */
1711 #ifdef CONFIG_TCC_BCHECK
1712 /* if evaluating constant expression, no code should be
1713 generated, so no bound check */
1714 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
1715 /* if bounded pointers, we generate a special code to
1722 gen_bounded_ptr_add();
1728 /* put again type if gen_opic() swaped operands */
1731 } else if (is_float(bt1
) || is_float(bt2
)) {
1732 /* compute bigger type and do implicit casts */
1733 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
1735 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
1740 /* floats can only be used for a few operations */
1741 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
1742 (op
< TOK_ULT
|| op
> TOK_GT
))
1743 tcc_error("invalid operands for binary operation");
1745 } else if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
) {
1746 t
= bt1
== VT_LLONG
? VT_LLONG
: VT_INT
;
1747 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (t
| VT_UNSIGNED
))
1750 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
1751 /* cast to biggest op */
1753 /* convert to unsigned if it does not fit in a long long */
1754 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
1755 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
1758 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
1759 tcc_error("comparison of struct");
1761 /* integer operations */
1763 /* convert to unsigned if it does not fit in an integer */
1764 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
1765 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
1768 /* XXX: currently, some unsigned operations are explicit, so
1769 we modify them here */
1770 if (t
& VT_UNSIGNED
) {
1777 else if (op
== TOK_LT
)
1779 else if (op
== TOK_GT
)
1781 else if (op
== TOK_LE
)
1783 else if (op
== TOK_GE
)
1790 /* special case for shifts and long long: we keep the shift as
1792 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
1799 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
1800 /* relationnal op: the result is an int */
1801 vtop
->type
.t
= VT_INT
;
1808 #ifndef TCC_TARGET_ARM
1809 /* generic itof for unsigned long long case */
1810 static void gen_cvt_itof1(int t
)
1812 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
1813 (VT_LLONG
| VT_UNSIGNED
)) {
1816 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
1817 #if LDOUBLE_SIZE != 8
1818 else if (t
== VT_LDOUBLE
)
1819 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
1822 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
1826 vtop
->r
= reg_fret(t
);
1833 /* generic ftoi for unsigned long long case */
1834 static void gen_cvt_ftoi1(int t
)
1838 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
1839 /* not handled natively */
1840 st
= vtop
->type
.t
& VT_BTYPE
;
1842 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
1843 #if LDOUBLE_SIZE != 8
1844 else if (st
== VT_LDOUBLE
)
1845 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
1848 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
1853 vtop
->r2
= REG_LRET
;
1859 /* force char or short cast */
1860 static void force_charshort_cast(int t
)
1864 /* XXX: add optimization if lvalue : just change type and offset */
1869 if (t
& VT_UNSIGNED
) {
1870 vpushi((1 << bits
) - 1);
1876 /* result must be signed or the SAR is converted to an SHL
1877 This was not the case when "t" was a signed short
1878 and the last value on the stack was an unsigned int */
1879 vtop
->type
.t
&= ~VT_UNSIGNED
;
1885 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1886 static void gen_cast(CType
*type
)
1888 int sbt
, dbt
, sf
, df
, c
, p
;
1890 /* special delayed cast for char/short */
1891 /* XXX: in some cases (multiple cascaded casts), it may still
1893 if (vtop
->r
& VT_MUSTCAST
) {
1894 vtop
->r
&= ~VT_MUSTCAST
;
1895 force_charshort_cast(vtop
->type
.t
);
1898 /* bitfields first get cast to ints */
1899 if (vtop
->type
.t
& VT_BITFIELD
) {
1903 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
1904 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
1909 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1910 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
1912 /* constant case: we can do it now */
1913 /* XXX: in ISOC, cannot do it if error in convert */
1914 if (sbt
== VT_FLOAT
)
1915 vtop
->c
.ld
= vtop
->c
.f
;
1916 else if (sbt
== VT_DOUBLE
)
1917 vtop
->c
.ld
= vtop
->c
.d
;
1920 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
1921 if (sbt
& VT_UNSIGNED
)
1922 vtop
->c
.ld
= vtop
->c
.ull
;
1924 vtop
->c
.ld
= vtop
->c
.ll
;
1926 if (sbt
& VT_UNSIGNED
)
1927 vtop
->c
.ld
= vtop
->c
.ui
;
1929 vtop
->c
.ld
= vtop
->c
.i
;
1932 if (dbt
== VT_FLOAT
)
1933 vtop
->c
.f
= (float)vtop
->c
.ld
;
1934 else if (dbt
== VT_DOUBLE
)
1935 vtop
->c
.d
= (double)vtop
->c
.ld
;
1936 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
1937 vtop
->c
.ull
= (unsigned long long)vtop
->c
.ld
;
1938 } else if (sf
&& dbt
== VT_BOOL
) {
1939 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
1942 vtop
->c
.ll
= (long long)vtop
->c
.ld
;
1943 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
1944 vtop
->c
.ll
= vtop
->c
.ull
;
1945 else if (sbt
& VT_UNSIGNED
)
1946 vtop
->c
.ll
= vtop
->c
.ui
;
1947 #ifdef TCC_TARGET_X86_64
1948 else if (sbt
== VT_PTR
)
1951 else if (sbt
!= VT_LLONG
)
1952 vtop
->c
.ll
= vtop
->c
.i
;
1954 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
1955 vtop
->c
.ull
= vtop
->c
.ll
;
1956 else if (dbt
== VT_BOOL
)
1957 vtop
->c
.i
= (vtop
->c
.ll
!= 0);
1958 #ifdef TCC_TARGET_X86_64
1959 else if (dbt
== VT_PTR
)
1962 else if (dbt
!= VT_LLONG
) {
1964 if ((dbt
& VT_BTYPE
) == VT_BYTE
)
1966 else if ((dbt
& VT_BTYPE
) == VT_SHORT
)
1968 if(dbt
& VT_UNSIGNED
)
1969 vtop
->c
.ui
= ((unsigned int)vtop
->c
.ll
<< s
) >> s
;
1971 vtop
->c
.i
= ((int)vtop
->c
.ll
<< s
) >> s
;
1974 } else if (p
&& dbt
== VT_BOOL
) {
1977 } else if (!nocode_wanted
) {
1978 /* non constant case: generate code */
1980 /* convert from fp to fp */
1983 /* convert int to fp */
1986 /* convert fp to int */
1987 if (dbt
== VT_BOOL
) {
1991 /* we handle char/short/etc... with generic code */
1992 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
1993 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
1997 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
1998 /* additional cast for char/short... */
2003 #ifndef TCC_TARGET_X86_64
2004 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
2005 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
2006 /* scalar to long long */
2007 /* machine independent conversion */
2009 /* generate high word */
2010 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
2014 if (sbt
== VT_PTR
) {
2015 /* cast from pointer to int before we apply
2016 shift operation, which pointers don't support*/
2017 gen_cast(&int_type
);
2023 /* patch second register */
2024 vtop
[-1].r2
= vtop
->r
;
2028 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
2029 (dbt
& VT_BTYPE
) == VT_PTR
||
2030 (dbt
& VT_BTYPE
) == VT_FUNC
) {
2031 if ((sbt
& VT_BTYPE
) != VT_LLONG
&&
2032 (sbt
& VT_BTYPE
) != VT_PTR
&&
2033 (sbt
& VT_BTYPE
) != VT_FUNC
) {
2034 /* need to convert from 32bit to 64bit */
2036 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
2037 /* x86_64 specific: movslq */
2039 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
2043 } else if (dbt
== VT_BOOL
) {
2044 /* scalar to bool */
2047 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
2048 (dbt
& VT_BTYPE
) == VT_SHORT
) {
2049 if (sbt
== VT_PTR
) {
2050 vtop
->type
.t
= VT_INT
;
2051 tcc_warning("nonportable conversion from pointer to char/short");
2053 force_charshort_cast(dbt
);
2054 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
2056 if (sbt
== VT_LLONG
) {
2057 /* from long long: just take low order word */
2061 /* if lvalue and single word type, nothing to do because
2062 the lvalue already contains the real type size (see
2063 VT_LVAL_xxx constants) */
2066 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
2067 /* if we are casting between pointer types,
2068 we must update the VT_LVAL_xxx size */
2069 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
2070 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
2075 /* return type size as known at compile time. Put alignment at 'a' */
2076 ST_FUNC
int type_size(CType
*type
, int *a
)
2081 bt
= type
->t
& VT_BTYPE
;
2082 if (bt
== VT_STRUCT
) {
2087 } else if (bt
== VT_PTR
) {
2088 if (type
->t
& VT_ARRAY
) {
2092 ts
= type_size(&s
->type
, a
);
2094 if (ts
< 0 && s
->c
< 0)
2102 } else if (bt
== VT_LDOUBLE
) {
2104 return LDOUBLE_SIZE
;
2105 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
2106 #ifdef TCC_TARGET_I386
2107 #ifdef TCC_TARGET_PE
2112 #elif defined(TCC_TARGET_ARM)
2122 } else if (bt
== VT_INT
|| bt
== VT_ENUM
|| bt
== VT_FLOAT
) {
2125 } else if (bt
== VT_SHORT
) {
2128 } else if (bt
== VT_QLONG
|| bt
== VT_QFLOAT
) {
2132 /* char, void, function, _Bool */
2138 /* push type size as known at runtime time on top of value stack. Put
2140 ST_FUNC
void vla_runtime_type_size(CType
*type
, int *a
)
2142 if (type
->t
& VT_VLA
) {
2143 vset(&int_type
, VT_LOCAL
|VT_LVAL
, type
->ref
->c
);
2145 vpushi(type_size(type
, a
));
2149 static void vla_sp_save(void) {
2150 if (!(vla_flags
& VLA_SP_LOC_SET
)) {
2151 *vla_sp_loc
= (loc
-= PTR_SIZE
);
2152 vla_flags
|= VLA_SP_LOC_SET
;
2154 if (!(vla_flags
& VLA_SP_SAVED
)) {
2155 gen_vla_sp_save(*vla_sp_loc
);
2156 vla_flags
|= VLA_SP_SAVED
;
2160 /* return the pointed type of t */
2161 static inline CType
*pointed_type(CType
*type
)
2163 return &type
->ref
->type
;
2166 /* modify type so that its it is a pointer to type. */
2167 ST_FUNC
void mk_pointer(CType
*type
)
2170 s
= sym_push(SYM_FIELD
, type
, 0, -1);
2171 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
2175 /* compare function types. OLD functions match any new functions */
2176 static int is_compatible_func(CType
*type1
, CType
*type2
)
2182 if (!is_compatible_types(&s1
->type
, &s2
->type
))
2184 /* check func_call */
2185 if (s1
->a
.func_call
!= s2
->a
.func_call
)
2187 /* XXX: not complete */
2188 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
2192 while (s1
!= NULL
) {
2195 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
2205 /* return true if type1 and type2 are the same. If unqualified is
2206 true, qualifiers on the types are ignored.
2208 - enums are not checked as gcc __builtin_types_compatible_p ()
2210 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2214 t1
= type1
->t
& VT_TYPE
;
2215 t2
= type2
->t
& VT_TYPE
;
2217 /* strip qualifiers before comparing */
2218 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2219 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2221 /* Default Vs explicit signedness only matters for char */
2222 if ((t1
& VT_BTYPE
) != VT_BYTE
) {
2226 /* XXX: bitfields ? */
2229 /* test more complicated cases */
2230 bt1
= t1
& VT_BTYPE
;
2231 if (bt1
== VT_PTR
) {
2232 type1
= pointed_type(type1
);
2233 type2
= pointed_type(type2
);
2234 return is_compatible_types(type1
, type2
);
2235 } else if (bt1
== VT_STRUCT
) {
2236 return (type1
->ref
== type2
->ref
);
2237 } else if (bt1
== VT_FUNC
) {
2238 return is_compatible_func(type1
, type2
);
2244 /* return true if type1 and type2 are exactly the same (including
2247 static int is_compatible_types(CType
*type1
, CType
*type2
)
2249 return compare_types(type1
,type2
,0);
2252 /* return true if type1 and type2 are the same (ignoring qualifiers).
2254 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
2256 return compare_types(type1
,type2
,1);
2259 /* print a type. If 'varstr' is not NULL, then the variable is also
2260 printed in the type */
2262 /* XXX: add array and function pointers */
2263 static void type_to_str(char *buf
, int buf_size
,
2264 CType
*type
, const char *varstr
)
2271 t
= type
->t
& VT_TYPE
;
2274 if (t
& VT_CONSTANT
)
2275 pstrcat(buf
, buf_size
, "const ");
2276 if (t
& VT_VOLATILE
)
2277 pstrcat(buf
, buf_size
, "volatile ");
2278 if ((t
& (VT_DEFSIGN
| VT_UNSIGNED
)) == (VT_DEFSIGN
| VT_UNSIGNED
))
2279 pstrcat(buf
, buf_size
, "unsigned ");
2280 else if (t
& VT_DEFSIGN
)
2281 pstrcat(buf
, buf_size
, "signed ");
2311 tstr
= "long double";
2313 pstrcat(buf
, buf_size
, tstr
);
2317 if (bt
== VT_STRUCT
)
2321 pstrcat(buf
, buf_size
, tstr
);
2322 v
= type
->ref
->v
& ~SYM_STRUCT
;
2323 if (v
>= SYM_FIRST_ANOM
)
2324 pstrcat(buf
, buf_size
, "<anonymous>");
2326 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
2330 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
2331 pstrcat(buf
, buf_size
, "(");
2333 while (sa
!= NULL
) {
2334 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
2335 pstrcat(buf
, buf_size
, buf1
);
2338 pstrcat(buf
, buf_size
, ", ");
2340 pstrcat(buf
, buf_size
, ")");
2344 pstrcpy(buf1
, sizeof(buf1
), "*");
2346 pstrcat(buf1
, sizeof(buf1
), varstr
);
2347 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2351 pstrcat(buf
, buf_size
, " ");
2352 pstrcat(buf
, buf_size
, varstr
);
2357 /* verify type compatibility to store vtop in 'dt' type, and generate
2359 static void gen_assign_cast(CType
*dt
)
2361 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
2362 char buf1
[256], buf2
[256];
2365 st
= &vtop
->type
; /* source type */
2366 dbt
= dt
->t
& VT_BTYPE
;
2367 sbt
= st
->t
& VT_BTYPE
;
2368 if (sbt
== VT_VOID
|| dbt
== VT_VOID
)
2369 tcc_error("cannot cast from/to void");
2370 if (dt
->t
& VT_CONSTANT
)
2371 tcc_warning("assignment of read-only location");
2374 /* special cases for pointers */
2375 /* '0' can also be a pointer */
2376 if (is_null_pointer(vtop
))
2378 /* accept implicit pointer to integer cast with warning */
2379 if (is_integer_btype(sbt
)) {
2380 tcc_warning("assignment makes pointer from integer without a cast");
2383 type1
= pointed_type(dt
);
2384 /* a function is implicitely a function pointer */
2385 if (sbt
== VT_FUNC
) {
2386 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
2387 !is_compatible_types(pointed_type(dt
), st
))
2388 tcc_warning("assignment from incompatible pointer type");
2393 type2
= pointed_type(st
);
2394 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2395 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
2396 /* void * can match anything */
2398 /* exact type match, except for unsigned */
2401 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
|
2403 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
|
2405 if (!is_compatible_types(&tmp_type1
, &tmp_type2
))
2406 tcc_warning("assignment from incompatible pointer type");
2408 /* check const and volatile */
2409 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
2410 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
2411 tcc_warning("assignment discards qualifiers from pointer target type");
2417 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
2418 tcc_warning("assignment makes integer from pointer without a cast");
2420 /* XXX: more tests */
2425 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2426 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2427 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2429 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
2430 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
2431 tcc_error("cannot cast '%s' to '%s'", buf1
, buf2
);
2439 /* store vtop in lvalue pushed on stack */
2440 ST_FUNC
void vstore(void)
2442 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
2444 ft
= vtop
[-1].type
.t
;
2445 sbt
= vtop
->type
.t
& VT_BTYPE
;
2446 dbt
= ft
& VT_BTYPE
;
2447 if ((((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2448 (sbt
== VT_INT
&& dbt
== VT_SHORT
))
2449 && !(vtop
->type
.t
& VT_BITFIELD
)) {
2450 /* optimize char/short casts */
2451 delayed_cast
= VT_MUSTCAST
;
2452 vtop
->type
.t
= ft
& (VT_TYPE
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
)));
2453 /* XXX: factorize */
2454 if (ft
& VT_CONSTANT
)
2455 tcc_warning("assignment of read-only location");
2458 if (!(ft
& VT_BITFIELD
))
2459 gen_assign_cast(&vtop
[-1].type
);
2462 if (sbt
== VT_STRUCT
) {
2463 /* if structure, only generate pointer */
2464 /* structure assignment : generate memcpy */
2465 /* XXX: optimize if small size */
2466 if (!nocode_wanted
) {
2467 size
= type_size(&vtop
->type
, &align
);
2471 vtop
->type
.t
= VT_PTR
;
2474 /* address of memcpy() */
2477 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2478 else if(!(align
& 3))
2479 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2482 vpush_global_sym(&func_old_type
, TOK_memcpy
);
2487 vtop
->type
.t
= VT_PTR
;
2496 /* leave source on stack */
2497 } else if (ft
& VT_BITFIELD
) {
2498 /* bitfield store handling */
2499 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2500 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2501 /* remove bit field info to avoid loops */
2502 vtop
[-1].type
.t
= ft
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
2504 /* duplicate source into other register */
2509 if((ft
& VT_BTYPE
) == VT_BOOL
) {
2510 gen_cast(&vtop
[-1].type
);
2511 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2514 /* duplicate destination */
2516 vtop
[-1] = vtop
[-2];
2518 /* mask and shift source */
2519 if((ft
& VT_BTYPE
) != VT_BOOL
) {
2520 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2521 vpushll((1ULL << bit_size
) - 1ULL);
2523 vpushi((1 << bit_size
) - 1);
2529 /* load destination, mask and or with source */
2531 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2532 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2534 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2541 /* pop off shifted source from "duplicate source..." above */
2545 #ifdef CONFIG_TCC_BCHECK
2546 /* bound check case */
2547 if (vtop
[-1].r
& VT_MUSTBOUND
) {
2553 if (!nocode_wanted
) {
2557 #ifdef TCC_TARGET_X86_64
2558 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
2560 } else if ((ft
& VT_BTYPE
) == VT_QFLOAT
) {
2565 r
= gv(rc
); /* generate value */
2566 /* if lvalue was saved on stack, must read it */
2567 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
2569 t
= get_reg(RC_INT
);
2570 #ifdef TCC_TARGET_X86_64
2575 sv
.r
= VT_LOCAL
| VT_LVAL
;
2576 sv
.c
.ul
= vtop
[-1].c
.ul
;
2578 vtop
[-1].r
= t
| VT_LVAL
;
2580 /* two word case handling : store second register at word + 4 (or +8 for x86-64) */
2581 #ifdef TCC_TARGET_X86_64
2582 if (((ft
& VT_BTYPE
) == VT_QLONG
) || ((ft
& VT_BTYPE
) == VT_QFLOAT
)) {
2583 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
2585 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
2586 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
2588 vtop
[-1].type
.t
= load_type
;
2591 /* convert to int to increment easily */
2592 vtop
->type
.t
= addr_type
;
2598 vtop
[-1].type
.t
= load_type
;
2599 /* XXX: it works because r2 is spilled last ! */
2600 store(vtop
->r2
, vtop
- 1);
2606 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
2607 vtop
->r
|= delayed_cast
;
2611 /* post defines POST/PRE add. c is the token ++ or -- */
2612 ST_FUNC
void inc(int post
, int c
)
2615 vdup(); /* save lvalue */
2617 gv_dup(); /* duplicate value */
2622 vpushi(c
- TOK_MID
);
2624 vstore(); /* store value */
2626 vpop(); /* if post op, return saved value */
2629 /* Parse GNUC __attribute__ extension. Currently, the following
2630 extensions are recognized:
2631 - aligned(n) : set data/function alignment.
2632 - packed : force data alignment to 1
2633 - section(x) : generate data/code in this section.
2634 - unused : currently ignored, but may be used someday.
2635 - regparm(n) : pass function parameters in registers (i386 only)
2637 static void parse_attribute(AttributeDef
*ad
)
2641 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
2645 while (tok
!= ')') {
2646 if (tok
< TOK_IDENT
)
2647 expect("attribute name");
2655 expect("section name");
2656 ad
->section
= find_section(tcc_state
, (char *)tokc
.cstr
->data
);
2664 expect("alias(\"target\")");
2665 ad
->alias_target
= /* save string as token, for later */
2666 tok_alloc((char*)tokc
.cstr
->data
, tokc
.cstr
->size
-1)->tok
;
2675 if (n
<= 0 || (n
& (n
- 1)) != 0)
2676 tcc_error("alignment must be a positive power of two");
2693 /* currently, no need to handle it because tcc does not
2694 track unused objects */
2698 /* currently, no need to handle it because tcc does not
2699 track unused objects */
2704 ad
->a
.func_call
= FUNC_CDECL
;
2709 ad
->a
.func_call
= FUNC_STDCALL
;
2711 #ifdef TCC_TARGET_I386
2721 ad
->a
.func_call
= FUNC_FASTCALL1
+ n
- 1;
2727 ad
->a
.func_call
= FUNC_FASTCALLW
;
2734 ad
->a
.mode
= VT_LLONG
+ 1;
2737 ad
->a
.mode
= VT_SHORT
+ 1;
2740 ad
->a
.mode
= VT_INT
+ 1;
2743 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
2750 ad
->a
.func_export
= 1;
2753 ad
->a
.func_import
= 1;
2756 if (tcc_state
->warn_unsupported
)
2757 tcc_warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
2758 /* skip parameters */
2760 int parenthesis
= 0;
2764 else if (tok
== ')')
2767 } while (parenthesis
&& tok
!= -1);
2780 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2781 static void struct_decl(CType
*type
, int u
, int tdef
)
2783 int a
, v
, size
, align
, maxalign
, c
, offset
, flexible
;
2784 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
, prevbt
;
2785 Sym
*s
, *ss
, *ass
, **ps
;
2789 a
= tok
; /* save decl type */
2794 /* struct already defined ? return it */
2796 expect("struct/union/enum name");
2800 tcc_error("invalid type");
2802 } else if (tok
>= TOK_IDENT
&& !tdef
)
2803 tcc_error("unknown struct/union/enum");
2809 /* we put an undefined size for struct/union */
2810 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
2811 s
->r
= 0; /* default alignment is zero as gcc */
2812 /* put struct/union/enum name in type */
2820 tcc_error("struct/union/enum already defined");
2821 /* cannot be empty */
2823 /* non empty enums are not allowed */
2824 if (a
== TOK_ENUM
) {
2828 expect("identifier");
2830 if (ss
&& !local_stack
)
2831 tcc_error("redefinition of enumerator '%s'",
2832 get_tok_str(v
, NULL
));
2838 /* enum symbols have static storage */
2839 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
2840 ss
->type
.t
|= VT_STATIC
;
2845 /* NOTE: we accept a trailing comma */
2849 s
->c
= type_size(&int_type
, &align
);
2858 while (tok
!= '}') {
2859 parse_btype(&btype
, &ad
);
2862 tcc_error("flexible array member '%s' not at the end of struct",
2863 get_tok_str(v
, NULL
));
2868 type_decl(&type1
, &ad
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
2869 if (v
== 0 && (type1
.t
& VT_BTYPE
) != VT_STRUCT
)
2870 expect("identifier");
2871 if (type_size(&type1
, &align
) < 0) {
2872 if ((a
== TOK_STRUCT
) && (type1
.t
& VT_ARRAY
) && c
)
2875 tcc_error("field '%s' has incomplete type",
2876 get_tok_str(v
, NULL
));
2878 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
2879 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
2880 tcc_error("invalid type for '%s'",
2881 get_tok_str(v
, NULL
));
2885 bit_size
= expr_const();
2886 /* XXX: handle v = 0 case for messages */
2888 tcc_error("negative width in bit-field '%s'",
2889 get_tok_str(v
, NULL
));
2890 if (v
&& bit_size
== 0)
2891 tcc_error("zero width for bit-field '%s'",
2892 get_tok_str(v
, NULL
));
2894 size
= type_size(&type1
, &align
);
2896 if (align
< ad
.a
.aligned
)
2897 align
= ad
.a
.aligned
;
2898 } else if (ad
.a
.packed
) {
2900 } else if (*tcc_state
->pack_stack_ptr
) {
2901 if (align
> *tcc_state
->pack_stack_ptr
)
2902 align
= *tcc_state
->pack_stack_ptr
;
2905 if (bit_size
>= 0) {
2906 bt
= type1
.t
& VT_BTYPE
;
2913 tcc_error("bitfields must have scalar type");
2915 if (bit_size
> bsize
) {
2916 tcc_error("width of '%s' exceeds its type",
2917 get_tok_str(v
, NULL
));
2918 } else if (bit_size
== bsize
) {
2919 /* no need for bit fields */
2921 } else if (bit_size
== 0) {
2922 /* XXX: what to do if only padding in a
2924 /* zero size: means to pad */
2927 /* we do not have enough room ?
2928 did the type change?
2930 if ((bit_pos
+ bit_size
) > bsize
||
2931 bt
!= prevbt
|| a
== TOK_UNION
)
2934 /* XXX: handle LSB first */
2935 type1
.t
|= VT_BITFIELD
|
2936 (bit_pos
<< VT_STRUCT_SHIFT
) |
2937 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
2938 bit_pos
+= bit_size
;
2944 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2945 /* add new memory data only if starting
2947 if (lbit_pos
== 0) {
2948 if (a
== TOK_STRUCT
) {
2949 c
= (c
+ align
- 1) & -align
;
2958 if (align
> maxalign
)
2962 printf("add field %s offset=%d",
2963 get_tok_str(v
, NULL
), offset
);
2964 if (type1
.t
& VT_BITFIELD
) {
2965 printf(" pos=%d size=%d",
2966 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
2967 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
2972 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2974 while ((ass
= ass
->next
) != NULL
) {
2975 ss
= sym_push(ass
->v
, &ass
->type
, 0, offset
+ ass
->c
);
2980 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
2984 if (tok
== ';' || tok
== TOK_EOF
)
2991 /* store size and alignment */
2992 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
2998 /* return 1 if basic type is a type size (short, long, long long) */
2999 int is_btype_size (int bt
)
3001 return bt
== VT_SHORT
|| bt
== VT_LONG
|| bt
== VT_LLONG
;
3004 /* return 0 if no type declaration. otherwise, return the basic type
3007 static int parse_btype(CType
*type
, AttributeDef
*ad
)
3009 int t
, u
, bt_size
, complete
, type_found
, typespec_found
;
3013 memset(ad
, 0, sizeof(AttributeDef
));
3021 /* currently, we really ignore extension */
3032 tcc_error("too many basic types");
3034 bt_size
= is_btype_size (u
& VT_BTYPE
);
3035 if (u
== VT_INT
|| (!bt_size
&& !(t
& VT_TYPEDEF
)))
3050 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
3051 #ifndef TCC_TARGET_PE
3052 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3054 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
3055 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3069 if ((t
& VT_BTYPE
) == VT_LONG
) {
3070 #ifdef TCC_TARGET_PE
3071 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
3073 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3081 struct_decl(&type1
, VT_ENUM
, t
& VT_TYPEDEF
);
3084 type
->ref
= type1
.ref
;
3088 struct_decl(&type1
, VT_STRUCT
, t
& VT_TYPEDEF
);
3091 /* type modifiers */
3107 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == (VT_DEFSIGN
|VT_UNSIGNED
))
3108 tcc_error("signed and unsigned modifier");
3121 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == VT_DEFSIGN
)
3122 tcc_error("signed and unsigned modifier");
3123 t
|= VT_DEFSIGN
| VT_UNSIGNED
;
3148 /* GNUC attribute */
3149 case TOK_ATTRIBUTE1
:
3150 case TOK_ATTRIBUTE2
:
3151 parse_attribute(ad
);
3154 t
= (t
& ~VT_BTYPE
) | u
;
3162 parse_expr_type(&type1
);
3163 /* remove all storage modifiers except typedef */
3164 type1
.t
&= ~(VT_STORAGE
&~VT_TYPEDEF
);
3170 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
3172 t
|= (s
->type
.t
& ~VT_TYPEDEF
);
3173 type
->ref
= s
->type
.ref
;
3175 /* get attributes from typedef */
3176 if (0 == ad
->a
.aligned
)
3177 ad
->a
.aligned
= s
->a
.aligned
;
3178 if (0 == ad
->a
.func_call
)
3179 ad
->a
.func_call
= s
->a
.func_call
;
3180 ad
->a
.packed
|= s
->a
.packed
;
3189 if (tcc_state
->char_is_unsigned
) {
3190 if ((t
& (VT_DEFSIGN
|VT_BTYPE
)) == VT_BYTE
)
3194 /* long is never used as type */
3195 if ((t
& VT_BTYPE
) == VT_LONG
)
3196 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
3197 t
= (t
& ~VT_BTYPE
) | VT_INT
;
3199 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3205 /* convert a function parameter type (array to pointer and function to
3206 function pointer) */
3207 static inline void convert_parameter_type(CType
*pt
)
3209 /* remove const and volatile qualifiers (XXX: const could be used
3210 to indicate a const function parameter */
3211 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3212 /* array must be transformed to pointer according to ANSI C */
3214 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
3219 ST_FUNC
void parse_asm_str(CString
*astr
)
3222 /* read the string */
3224 expect("string constant");
3226 while (tok
== TOK_STR
) {
3227 /* XXX: add \0 handling too ? */
3228 cstr_cat(astr
, tokc
.cstr
->data
);
3231 cstr_ccat(astr
, '\0');
3234 /* Parse an asm label and return the label
3235 * Don't forget to free the CString in the caller! */
3236 static void asm_label_instr(CString
*astr
)
3239 parse_asm_str(astr
);
3242 printf("asm_alias: \"%s\"\n", (char *)astr
->data
);
3246 static void post_type(CType
*type
, AttributeDef
*ad
)
3248 int n
, l
, t1
, arg_size
, align
;
3249 Sym
**plast
, *s
, *first
;
3254 /* function declaration */
3262 /* read param name and compute offset */
3263 if (l
!= FUNC_OLD
) {
3264 if (!parse_btype(&pt
, &ad1
)) {
3266 tcc_error("invalid type");
3273 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
3275 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3276 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
3277 tcc_error("parameter declared as void");
3278 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
3283 expect("identifier");
3287 convert_parameter_type(&pt
);
3288 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
3294 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
3301 /* if no parameters, then old type prototype */
3305 /* NOTE: const is ignored in returned type as it has a special
3306 meaning in gcc / C++ */
3307 type
->t
&= ~VT_CONSTANT
;
3308 /* some ancient pre-K&R C allows a function to return an array
3309 and the array brackets to be put after the arguments, such
3310 that "int c()[]" means something like "int[] c()" */
3313 skip(']'); /* only handle simple "[]" */
3316 /* we push a anonymous symbol which will contain the function prototype */
3317 ad
->a
.func_args
= arg_size
;
3318 s
= sym_push(SYM_FIELD
, type
, 0, l
);
3323 } else if (tok
== '[') {
3324 /* array definition */
3326 if (tok
== TOK_RESTRICT1
)
3331 if (!local_stack
|| nocode_wanted
)
3332 vpushi(expr_const());
3334 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3337 tcc_error("invalid array size");
3339 if (!is_integer_btype(vtop
->type
.t
& VT_BTYPE
))
3340 tcc_error("size of variable length array should be an integer");
3345 /* parse next post type */
3346 post_type(type
, ad
);
3347 if (type
->t
== VT_FUNC
)
3348 tcc_error("declaration of an array of functions");
3349 t1
|= type
->t
& VT_VLA
;
3352 loc
-= type_size(&int_type
, &align
);
3356 vla_runtime_type_size(type
, &align
);
3358 vset(&int_type
, VT_LOCAL
|VT_LVAL
, loc
);
3365 /* we push an anonymous symbol which will contain the array
3367 s
= sym_push(SYM_FIELD
, type
, 0, n
);
3368 type
->t
= (t1
? VT_VLA
: VT_ARRAY
) | VT_PTR
;
3373 /* Parse a type declaration (except basic type), and return the type
3374 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3375 expected. 'type' should contain the basic type. 'ad' is the
3376 attribute definition of the basic type. It can be modified by
3379 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
3382 CType type1
, *type2
;
3383 int qualifiers
, storage
;
3385 while (tok
== '*') {
3393 qualifiers
|= VT_CONSTANT
;
3398 qualifiers
|= VT_VOLATILE
;
3406 type
->t
|= qualifiers
;
3409 /* XXX: clarify attribute handling */
3410 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3411 parse_attribute(ad
);
3413 /* recursive type */
3414 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3415 type1
.t
= 0; /* XXX: same as int */
3418 /* XXX: this is not correct to modify 'ad' at this point, but
3419 the syntax is not clear */
3420 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3421 parse_attribute(ad
);
3422 type_decl(&type1
, ad
, v
, td
);
3425 /* type identifier */
3426 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
3430 if (!(td
& TYPE_ABSTRACT
))
3431 expect("identifier");
3435 storage
= type
->t
& VT_STORAGE
;
3436 type
->t
&= ~VT_STORAGE
;
3437 if (storage
& VT_STATIC
) {
3438 int saved_nocode_wanted
= nocode_wanted
;
3440 post_type(type
, ad
);
3441 nocode_wanted
= saved_nocode_wanted
;
3443 post_type(type
, ad
);
3445 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3446 parse_attribute(ad
);
3450 /* append type at the end of type1 */
3463 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3464 ST_FUNC
int lvalue_type(int t
)
3469 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
3471 else if (bt
== VT_SHORT
)
3475 if (t
& VT_UNSIGNED
)
3476 r
|= VT_LVAL_UNSIGNED
;
3480 /* indirection with full error checking and bound check */
3481 ST_FUNC
void indir(void)
3483 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
3484 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
3488 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
3490 vtop
->type
= *pointed_type(&vtop
->type
);
3491 /* Arrays and functions are never lvalues */
3492 if (!(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_VLA
)
3493 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3494 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3495 /* if bound checking, the referenced pointer must be checked */
3496 #ifdef CONFIG_TCC_BCHECK
3497 if (tcc_state
->do_bounds_check
)
3498 vtop
->r
|= VT_MUSTBOUND
;
3503 /* pass a parameter to a function and do type checking and casting */
3504 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
3509 func_type
= func
->c
;
3510 if (func_type
== FUNC_OLD
||
3511 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
3512 /* default casting : only need to convert float to double */
3513 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
3516 } else if (vtop
->type
.t
& VT_BITFIELD
) {
3517 type
.t
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
3520 } else if (arg
== NULL
) {
3521 tcc_error("too many arguments to function");
3524 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
3525 gen_assign_cast(&type
);
3529 /* parse an expression of the form '(type)' or '(expr)' and return its
3531 static void parse_expr_type(CType
*type
)
3537 if (parse_btype(type
, &ad
)) {
3538 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3545 static void parse_type(CType
*type
)
3550 if (!parse_btype(type
, &ad
)) {
3553 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3556 static void vpush_tokc(int t
)
3561 vsetc(&type
, VT_CONST
, &tokc
);
3564 ST_FUNC
void unary(void)
3566 int n
, t
, align
, size
, r
, sizeof_caller
;
3570 static int in_sizeof
= 0;
3572 sizeof_caller
= in_sizeof
;
3574 /* XXX: GCC 2.95.3 does not generate a table although it should be
3588 vpush_tokc(VT_INT
| VT_UNSIGNED
);
3592 vpush_tokc(VT_LLONG
);
3596 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
3600 vpush_tokc(VT_FLOAT
);
3604 vpush_tokc(VT_DOUBLE
);
3608 vpush_tokc(VT_LDOUBLE
);
3611 case TOK___FUNCTION__
:
3613 goto tok_identifier
;
3619 /* special function name identifier */
3620 len
= strlen(funcname
) + 1;
3621 /* generate char[len] type */
3626 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
3627 ptr
= section_ptr_add(data_section
, len
);
3628 memcpy(ptr
, funcname
, len
);
3633 #ifdef TCC_TARGET_PE
3634 t
= VT_SHORT
| VT_UNSIGNED
;
3640 /* string parsing */
3643 if (tcc_state
->warn_write_strings
)
3648 memset(&ad
, 0, sizeof(AttributeDef
));
3649 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, NULL
, 0);
3654 if (parse_btype(&type
, &ad
)) {
3655 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
3657 /* check ISOC99 compound literal */
3659 /* data is allocated locally by default */
3664 /* all except arrays are lvalues */
3665 if (!(type
.t
& VT_ARRAY
))
3666 r
|= lvalue_type(type
.t
);
3667 memset(&ad
, 0, sizeof(AttributeDef
));
3668 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, NULL
, 0);
3670 if (sizeof_caller
) {
3677 } else if (tok
== '{') {
3678 /* save all registers */
3680 /* statement expression : we do not accept break/continue
3681 inside as GCC does */
3682 block(NULL
, NULL
, NULL
, NULL
, 0, 1);
3697 /* functions names must be treated as function pointers,
3698 except for unary '&' and sizeof. Since we consider that
3699 functions are not lvalues, we only have to handle it
3700 there and in function calls. */
3701 /* arrays can also be used although they are not lvalues */
3702 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
3703 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
3705 mk_pointer(&vtop
->type
);
3711 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3713 boolean
.t
= VT_BOOL
;
3715 vtop
->c
.i
= !vtop
->c
.i
;
3716 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
3717 vtop
->c
.i
= vtop
->c
.i
^ 1;
3720 vseti(VT_JMP
, gvtst(1, 0));
3732 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
3733 tcc_error("pointer not accepted for unary plus");
3734 /* In order to force cast, we add zero, except for floating point
3735 where we really need an noop (otherwise -0.0 will be transformed
3737 if (!is_float(vtop
->type
.t
)) {
3748 unary_type(&type
); // Perform a in_sizeof = 0;
3749 size
= type_size(&type
, &align
);
3750 if (t
== TOK_SIZEOF
) {
3751 if (!(type
.t
& VT_VLA
)) {
3753 tcc_error("sizeof applied to an incomplete type");
3756 vla_runtime_type_size(&type
, &align
);
3761 vtop
->type
.t
|= VT_UNSIGNED
;
3764 case TOK_builtin_types_compatible_p
:
3773 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3774 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3775 vpushi(is_compatible_types(&type1
, &type2
));
3778 case TOK_builtin_constant_p
:
3780 int saved_nocode_wanted
, res
;
3783 saved_nocode_wanted
= nocode_wanted
;
3786 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
3788 nocode_wanted
= saved_nocode_wanted
;
3793 case TOK_builtin_frame_address
:
3799 if (tok
!= TOK_CINT
|| tokc
.i
< 0) {
3800 tcc_error("__builtin_frame_address only takes positive integers");
3807 vset(&type
, VT_LOCAL
, 0); /* local frame */
3809 mk_pointer(&vtop
->type
);
3810 indir(); /* -> parent frame */
3814 #ifdef TCC_TARGET_X86_64
3815 #ifdef TCC_TARGET_PE
3816 case TOK_builtin_va_start
:
3824 if ((vtop
->r
& VT_VALMASK
) != VT_LOCAL
)
3825 tcc_error("__builtin_va_start expects a local variable");
3826 vtop
->r
&= ~(VT_LVAL
| VT_REF
);
3827 vtop
->type
= char_pointer_type
;
3832 case TOK_builtin_va_arg_types
:
3839 vpushi(classify_x86_64_va_arg(&type
));
3854 t
= vtop
->type
.t
& VT_BTYPE
;
3856 /* In IEEE negate(x) isn't subtract(0,x), but rather
3861 else if (t
== VT_DOUBLE
)
3872 goto tok_identifier
;
3874 /* allow to take the address of a label */
3875 if (tok
< TOK_UIDENT
)
3876 expect("label identifier");
3877 s
= label_find(tok
);
3879 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
3881 if (s
->r
== LABEL_DECLARED
)
3882 s
->r
= LABEL_FORWARD
;
3885 s
->type
.t
= VT_VOID
;
3886 mk_pointer(&s
->type
);
3887 s
->type
.t
|= VT_STATIC
;
3889 vset(&s
->type
, VT_CONST
| VT_SYM
, 0);
3894 // special qnan , snan and infinity values
3896 vpush64(VT_DOUBLE
, 0x7ff8000000000000ULL
);
3900 vpush64(VT_DOUBLE
, 0x7ff0000000000001ULL
);
3904 vpush64(VT_DOUBLE
, 0x7ff0000000000000ULL
);
3913 expect("identifier");
3917 tcc_error("'%s' undeclared", get_tok_str(t
, NULL
));
3918 /* for simple function calls, we tolerate undeclared
3919 external reference to int() function */
3920 if (tcc_state
->warn_implicit_function_declaration
)
3921 tcc_warning("implicit declaration of function '%s'",
3922 get_tok_str(t
, NULL
));
3923 s
= external_global_sym(t
, &func_old_type
, 0);
3925 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
3926 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
3927 /* if referencing an inline function, then we generate a
3928 symbol to it if not already done. It will have the
3929 effect to generate code for it at the end of the
3930 compilation unit. Inline function as always
3931 generated in the text section. */
3933 put_extern_sym(s
, text_section
, 0, 0);
3934 r
= VT_SYM
| VT_CONST
;
3938 vset(&s
->type
, r
, s
->c
);
3939 /* if forward reference, we must point to s */
3940 if (vtop
->r
& VT_SYM
) {
3947 /* post operations */
3949 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
3952 } else if (tok
== '.' || tok
== TOK_ARROW
) {
3955 if (tok
== TOK_ARROW
)
3957 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
3961 /* expect pointer on structure */
3962 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
3963 expect("struct or union");
3967 while ((s
= s
->next
) != NULL
) {
3972 tcc_error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, NULL
));
3973 /* add field offset to pointer */
3974 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
3977 /* change type to field type, and set to lvalue */
3978 vtop
->type
= s
->type
;
3979 vtop
->type
.t
|= qualifiers
;
3980 /* an array is never an lvalue */
3981 if (!(vtop
->type
.t
& VT_ARRAY
)) {
3982 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3983 #ifdef CONFIG_TCC_BCHECK
3984 /* if bound checking, the referenced pointer must be checked */
3985 if (tcc_state
->do_bounds_check
)
3986 vtop
->r
|= VT_MUSTBOUND
;
3990 } else if (tok
== '[') {
3996 } else if (tok
== '(') {
3999 int nb_args
, ret_nregs
, ret_align
, variadic
;
4002 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
4003 /* pointer test (no array accepted) */
4004 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
4005 vtop
->type
= *pointed_type(&vtop
->type
);
4006 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
4010 expect("function pointer");
4013 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
4015 /* get return type */
4018 sa
= s
->next
; /* first parameter */
4021 /* compute first implicit argument if a structure is returned */
4022 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
4023 variadic
= (s
->c
== FUNC_ELLIPSIS
);
4024 ret_nregs
= gfunc_sret(&s
->type
, variadic
, &ret
.type
,
4027 /* get some space for the returned structure */
4028 size
= type_size(&s
->type
, &align
);
4029 loc
= (loc
- size
) & -align
;
4031 ret
.r
= VT_LOCAL
| VT_LVAL
;
4032 /* pass it as 'int' to avoid structure arg passing
4034 vseti(VT_LOCAL
, loc
);
4044 /* return in register */
4045 if (is_float(ret
.type
.t
)) {
4046 ret
.r
= reg_fret(ret
.type
.t
);
4047 #ifdef TCC_TARGET_X86_64
4048 if ((ret
.type
.t
& VT_BTYPE
) == VT_QFLOAT
)
4052 #ifdef TCC_TARGET_X86_64
4053 if ((ret
.type
.t
& VT_BTYPE
) == VT_QLONG
)
4055 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
4065 gfunc_param_typed(s
, sa
);
4075 tcc_error("too few arguments to function");
4077 if (!nocode_wanted
) {
4078 gfunc_call(nb_args
);
4080 vtop
-= (nb_args
+ 1);
4084 for (r
= ret
.r
+ ret_nregs
+ !ret_nregs
; r
-- > ret
.r
;) {
4085 vsetc(&ret
.type
, r
, &ret
.c
);
4086 vtop
->r2
= ret
.r2
; /* Loop only happens when r2 is VT_CONST */
4089 /* handle packed struct return */
4090 if (((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) && ret_nregs
) {
4093 size
= type_size(&s
->type
, &align
);
4094 loc
= (loc
- size
) & -align
;
4098 vset(&ret
.type
, VT_LOCAL
| VT_LVAL
, addr
+ offset
);
4102 if (--ret_nregs
== 0)
4104 /* XXX: compatible with arm only: ret_align == register_size */
4105 offset
+= ret_align
;
4107 vset(&s
->type
, VT_LOCAL
| VT_LVAL
, addr
);
4115 ST_FUNC
void expr_prod(void)
4120 while (tok
== '*' || tok
== '/' || tok
== '%') {
4128 ST_FUNC
void expr_sum(void)
4133 while (tok
== '+' || tok
== '-') {
4141 static void expr_shift(void)
4146 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
4154 static void expr_cmp(void)
4159 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
4160 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
4168 static void expr_cmpeq(void)
4173 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
4181 static void expr_and(void)
4184 while (tok
== '&') {
4191 static void expr_xor(void)
4194 while (tok
== '^') {
4201 static void expr_or(void)
4204 while (tok
== '|') {
4211 /* XXX: fix this mess */
4212 static void expr_land_const(void)
4215 while (tok
== TOK_LAND
) {
4222 /* XXX: fix this mess */
4223 static void expr_lor_const(void)
4226 while (tok
== TOK_LOR
) {
4233 /* only used if non constant */
4234 static void expr_land(void)
4239 if (tok
== TOK_LAND
) {
4244 if (tok
!= TOK_LAND
) {
4254 static void expr_lor(void)
4259 if (tok
== TOK_LOR
) {
4264 if (tok
!= TOK_LOR
) {
4274 /* XXX: better constant handling */
4275 static void expr_cond(void)
4277 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
;
4279 CType type
, type1
, type2
;
4286 boolean
.t
= VT_BOOL
;
4292 if (tok
!= ':' || !gnu_ext
) {
4307 if (vtop
!= vstack
) {
4308 /* needed to avoid having different registers saved in
4310 if (is_float(vtop
->type
.t
)) {
4312 #ifdef TCC_TARGET_X86_64
4313 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4323 if (tok
== ':' && gnu_ext
) {
4331 sv
= *vtop
; /* save value to handle it later */
4332 vtop
--; /* no vpop so that FP stack is not flushed */
4340 bt1
= t1
& VT_BTYPE
;
4342 bt2
= t2
& VT_BTYPE
;
4343 /* cast operands to correct type according to ISOC rules */
4344 if (is_float(bt1
) || is_float(bt2
)) {
4345 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
4346 type
.t
= VT_LDOUBLE
;
4347 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
4352 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
4353 /* cast to biggest op */
4355 /* convert to unsigned if it does not fit in a long long */
4356 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
4357 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
4358 type
.t
|= VT_UNSIGNED
;
4359 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
4360 /* If one is a null ptr constant the result type
4362 if (is_null_pointer (vtop
))
4364 else if (is_null_pointer (&sv
))
4366 /* XXX: test pointer compatibility, C99 has more elaborate
4370 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
4371 /* XXX: test function pointer compatibility */
4372 type
= bt1
== VT_FUNC
? type1
: type2
;
4373 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
4374 /* XXX: test structure compatibility */
4375 type
= bt1
== VT_STRUCT
? type1
: type2
;
4376 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
4377 /* NOTE: as an extension, we accept void on only one side */
4380 /* integer operations */
4382 /* convert to unsigned if it does not fit in an integer */
4383 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
4384 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
4385 type
.t
|= VT_UNSIGNED
;
4388 /* now we convert second operand */
4390 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4393 if (is_float(type
.t
)) {
4395 #ifdef TCC_TARGET_X86_64
4396 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4400 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
4401 /* for long longs, we use fixed registers to avoid having
4402 to handle a complicated move */
4407 /* this is horrible, but we must also convert first
4411 /* put again first value and cast it */
4414 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4417 move_reg(r2
, r1
, type
.t
);
4424 static void expr_eq(void)
4430 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
4431 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
4432 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
4447 ST_FUNC
void gexpr(void)
4458 /* parse an expression and return its type without any side effect. */
4459 static void expr_type(CType
*type
)
4461 int saved_nocode_wanted
;
4463 saved_nocode_wanted
= nocode_wanted
;
4468 nocode_wanted
= saved_nocode_wanted
;
4471 /* parse a unary expression and return its type without any side
4473 static void unary_type(CType
*type
)
4485 /* parse a constant expression and return value in vtop. */
4486 static void expr_const1(void)
4495 /* parse an integer constant and return its value. */
4496 ST_FUNC
int expr_const(void)
4500 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
4501 expect("constant expression");
4507 /* return the label token if current token is a label, otherwise
4509 static int is_label(void)
4513 /* fast test first */
4514 if (tok
< TOK_UIDENT
)
4516 /* no need to save tokc because tok is an identifier */
4523 unget_tok(last_tok
);
4528 static void label_or_decl(int l
)
4532 /* fast test first */
4533 if (tok
>= TOK_UIDENT
)
4535 /* no need to save tokc because tok is an identifier */
4539 unget_tok(last_tok
);
4542 unget_tok(last_tok
);
4547 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
4548 int case_reg
, int is_expr
)
4551 Sym
*s
, *frame_bottom
;
4553 /* generate line number info */
4554 if (tcc_state
->do_debug
&&
4555 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
4556 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
4558 last_line_num
= file
->line_num
;
4562 /* default return value is (void) */
4564 vtop
->type
.t
= VT_VOID
;
4567 if (tok
== TOK_IF
) {
4574 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4576 if (c
== TOK_ELSE
) {
4580 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4581 gsym(d
); /* patch else jmp */
4584 } else if (tok
== TOK_WHILE
) {
4592 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4596 } else if (tok
== '{') {
4598 int block_vla_sp_loc
, *saved_vla_sp_loc
, saved_vla_flags
;
4601 /* record local declaration stack position */
4603 frame_bottom
= sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4604 frame_bottom
->next
= scope_stack_bottom
;
4605 scope_stack_bottom
= frame_bottom
;
4606 llabel
= local_label_stack
;
4608 /* save VLA state */
4609 block_vla_sp_loc
= *(saved_vla_sp_loc
= vla_sp_loc
);
4610 if (saved_vla_sp_loc
!= &vla_sp_root_loc
)
4611 vla_sp_loc
= &block_vla_sp_loc
;
4613 saved_vla_flags
= vla_flags
;
4614 vla_flags
|= VLA_NEED_NEW_FRAME
;
4616 /* handle local labels declarations */
4617 if (tok
== TOK_LABEL
) {
4620 if (tok
< TOK_UIDENT
)
4621 expect("label identifier");
4622 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
4632 while (tok
!= '}') {
4633 label_or_decl(VT_LOCAL
);
4637 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4640 /* pop locally defined labels */
4641 label_pop(&local_label_stack
, llabel
);
4643 /* XXX: this solution makes only valgrind happy...
4644 triggered by gcc.c-torture/execute/20000917-1.c */
4646 switch(vtop
->type
.t
& VT_BTYPE
) {
4651 for(p
=vtop
->type
.ref
;p
;p
=p
->prev
)
4653 tcc_error("unsupported expression type");
4656 /* pop locally defined symbols */
4657 scope_stack_bottom
= scope_stack_bottom
->next
;
4658 sym_pop(&local_stack
, s
);
4660 /* Pop VLA frames and restore stack pointer if required */
4661 if (saved_vla_sp_loc
!= &vla_sp_root_loc
)
4662 *saved_vla_sp_loc
= block_vla_sp_loc
;
4663 if (vla_sp_loc
!= (saved_vla_sp_loc
== &vla_sp_root_loc
? &vla_sp_root_loc
: &block_vla_sp_loc
)) {
4664 vla_sp_loc
= saved_vla_sp_loc
;
4665 gen_vla_sp_restore(*vla_sp_loc
);
4667 vla_flags
= (vla_flags
& ~VLA_SCOPE_FLAGS
) | (saved_vla_flags
& VLA_SCOPE_FLAGS
);
4670 } else if (tok
== TOK_RETURN
) {
4674 gen_assign_cast(&func_vt
);
4675 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
4676 CType type
, ret_type
;
4677 int ret_align
, ret_nregs
;
4678 ret_nregs
= gfunc_sret(&func_vt
, func_var
, &ret_type
,
4680 if (0 == ret_nregs
) {
4681 /* if returning structure, must copy it to implicit
4682 first pointer arg location */
4685 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
4688 /* copy structure value to pointer */
4691 /* returning structure packed into registers */
4692 int r
, size
, addr
, align
;
4693 size
= type_size(&func_vt
,&align
);
4694 if ((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) || (vtop
->c
.i
& (ret_align
-1)))
4695 && (align
& (ret_align
-1))) {
4696 loc
= (loc
- size
) & -align
;
4699 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
4702 vset(&ret_type
, VT_LOCAL
| VT_LVAL
, addr
);
4704 vtop
->type
= ret_type
;
4705 if (is_float(ret_type
.t
))
4706 r
= rc_fret(ret_type
.t
);
4712 if (--ret_nregs
== 0)
4714 /* We assume that when a structure is returned in multiple
4715 registers, their classes are consecutive values of the
4718 /* XXX: compatible with arm only: ret_align == register_size */
4719 vtop
->c
.i
+= ret_align
;
4720 vtop
->r
= VT_LOCAL
| VT_LVAL
;
4723 } else if (is_float(func_vt
.t
)) {
4724 gv(rc_fret(func_vt
.t
));
4728 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
4731 rsym
= gjmp(rsym
); /* jmp */
4732 } else if (tok
== TOK_BREAK
) {
4735 tcc_error("cannot break");
4736 *bsym
= gjmp(*bsym
);
4739 } else if (tok
== TOK_CONTINUE
) {
4742 tcc_error("cannot continue");
4743 *csym
= gjmp(*csym
);
4746 } else if (tok
== TOK_FOR
) {
4751 frame_bottom
= sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4752 frame_bottom
->next
= scope_stack_bottom
;
4753 scope_stack_bottom
= frame_bottom
;
4755 /* c99 for-loop init decl? */
4756 if (!decl0(VT_LOCAL
, 1)) {
4757 /* no, regular for-loop init expr */
4781 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4785 scope_stack_bottom
= scope_stack_bottom
->next
;
4786 sym_pop(&local_stack
, s
);
4788 if (tok
== TOK_DO
) {
4793 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4804 if (tok
== TOK_SWITCH
) {
4808 /* XXX: other types than integer */
4809 case_reg
= gv(RC_INT
);
4813 b
= gjmp(0); /* jump to first case */
4815 block(&a
, csym
, &b
, &c
, case_reg
, 0);
4816 /* if no default, jmp after switch */
4824 if (tok
== TOK_CASE
) {
4831 if (gnu_ext
&& tok
== TOK_DOTS
) {
4835 tcc_warning("empty case range");
4837 /* since a case is like a label, we must skip it with a jmp */
4844 *case_sym
= gtst(1, 0);
4847 *case_sym
= gtst(1, 0);
4851 *case_sym
= gtst(1, *case_sym
);
4856 goto block_after_label
;
4858 if (tok
== TOK_DEFAULT
) {
4864 tcc_error("too many 'default'");
4867 goto block_after_label
;
4869 if (tok
== TOK_GOTO
) {
4871 if (tok
== '*' && gnu_ext
) {
4875 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
4878 } else if (tok
>= TOK_UIDENT
) {
4879 s
= label_find(tok
);
4880 /* put forward definition if needed */
4882 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4884 if (s
->r
== LABEL_DECLARED
)
4885 s
->r
= LABEL_FORWARD
;
4887 /* label already defined */
4888 if (vla_flags
& VLA_IN_SCOPE
) {
4889 /* If VLAs are in use, save the current stack pointer and
4890 reset the stack pointer to what it was at function entry
4891 (label will restore stack pointer in inner scopes) */
4893 gen_vla_sp_restore(vla_sp_root_loc
);
4895 if (s
->r
& LABEL_FORWARD
)
4896 s
->jnext
= gjmp(s
->jnext
);
4898 gjmp_addr(s
->jnext
);
4901 expect("label identifier");
4904 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
4910 if (vla_flags
& VLA_IN_SCOPE
) {
4911 /* save/restore stack pointer across label
4912 this is a no-op when combined with the load immediately
4913 after the label unless we arrive via goto */
4918 if (s
->r
== LABEL_DEFINED
)
4919 tcc_error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
4921 s
->r
= LABEL_DEFINED
;
4923 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
4926 if (vla_flags
& VLA_IN_SCOPE
) {
4927 gen_vla_sp_restore(*vla_sp_loc
);
4928 vla_flags
|= VLA_NEED_NEW_FRAME
;
4930 /* we accept this, but it is a mistake */
4933 tcc_warning("deprecated use of label at end of compound statement");
4937 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4940 /* expression case */
4955 /* t is the array or struct type. c is the array or struct
4956 address. cur_index/cur_field is the pointer to the current
4957 value. 'size_only' is true if only size info is needed (only used
4959 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
4960 int *cur_index
, Sym
**cur_field
,
4964 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
4970 if (gnu_ext
&& (l
= is_label()) != 0)
4972 while (tok
== '[' || tok
== '.') {
4974 if (!(type
->t
& VT_ARRAY
))
4975 expect("array type");
4978 index
= expr_const();
4979 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
4980 expect("invalid index");
4981 if (tok
== TOK_DOTS
&& gnu_ext
) {
4983 index_last
= expr_const();
4984 if (index_last
< 0 ||
4985 (s
->c
>= 0 && index_last
>= s
->c
) ||
4987 expect("invalid index");
4993 *cur_index
= index_last
;
4994 type
= pointed_type(type
);
4995 elem_size
= type_size(type
, &align
);
4996 c
+= index
* elem_size
;
4997 /* NOTE: we only support ranges for last designator */
4998 nb_elems
= index_last
- index
+ 1;
4999 if (nb_elems
!= 1) {
5008 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
5009 expect("struct/union type");
5022 /* XXX: fix this mess by using explicit storage field */
5024 type1
.t
|= (type
->t
& ~VT_TYPE
);
5038 if (type
->t
& VT_ARRAY
) {
5040 type
= pointed_type(type
);
5041 c
+= index
* type_size(type
, &align
);
5045 tcc_error("too many field init");
5046 /* XXX: fix this mess by using explicit storage field */
5048 type1
.t
|= (type
->t
& ~VT_TYPE
);
5053 decl_initializer(type
, sec
, c
, 0, size_only
);
5055 /* XXX: make it more general */
5056 if (!size_only
&& nb_elems
> 1) {
5057 unsigned long c_end
;
5062 tcc_error("range init not supported yet for dynamic storage");
5063 c_end
= c
+ nb_elems
* elem_size
;
5064 if (c_end
> sec
->data_allocated
)
5065 section_realloc(sec
, c_end
);
5066 src
= sec
->data
+ c
;
5068 for(i
= 1; i
< nb_elems
; i
++) {
5070 memcpy(dst
, src
, elem_size
);
5076 #define EXPR_CONST 1
5079 /* store a value or an expression directly in global data or in local array */
5080 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
5081 int v
, int expr_type
)
5083 int saved_global_expr
, bt
, bit_pos
, bit_size
;
5085 unsigned long long bit_mask
;
5093 /* compound literals must be allocated globally in this case */
5094 saved_global_expr
= global_expr
;
5097 global_expr
= saved_global_expr
;
5098 /* NOTE: symbols are accepted */
5099 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
5100 tcc_error("initializer element is not constant");
5108 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
5111 /* XXX: not portable */
5112 /* XXX: generate error if incorrect relocation */
5113 gen_assign_cast(&dtype
);
5114 bt
= type
->t
& VT_BTYPE
;
5115 /* we'll write at most 12 bytes */
5116 if (c
+ 12 > sec
->data_allocated
) {
5117 section_realloc(sec
, c
+ 12);
5119 ptr
= sec
->data
+ c
;
5120 /* XXX: make code faster ? */
5121 if (!(type
->t
& VT_BITFIELD
)) {
5126 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5127 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
5128 bit_mask
= (1LL << bit_size
) - 1;
5130 if ((vtop
->r
& VT_SYM
) &&
5136 (bt
== VT_INT
&& bit_size
!= 32)))
5137 tcc_error("initializer element is not computable at load time");
5140 vtop
->c
.i
= (vtop
->c
.i
!= 0);
5142 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5145 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5148 *(double *)ptr
= vtop
->c
.d
;
5151 *(long double *)ptr
= vtop
->c
.ld
;
5154 *(long long *)ptr
|= (vtop
->c
.ll
& bit_mask
) << bit_pos
;
5157 if (vtop
->r
& VT_SYM
) {
5158 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
5160 *(addr_t
*)ptr
|= (vtop
->c
.ull
& bit_mask
) << bit_pos
;
5163 if (vtop
->r
& VT_SYM
) {
5164 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
5166 *(int *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5171 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
5178 /* put zeros for variable based init */
5179 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
5182 /* nothing to do because globals are already set to zero */
5184 vpush_global_sym(&func_old_type
, TOK_memset
);
5186 #ifdef TCC_TARGET_ARM
5197 /* 't' contains the type and storage info. 'c' is the offset of the
5198 object in section 'sec'. If 'sec' is NULL, it means stack based
5199 allocation. 'first' is true if array '{' must be read (multi
5200 dimension implicit array init handling). 'size_only' is true if
5201 size only evaluation is wanted (only for arrays). */
5202 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
5203 int first
, int size_only
)
5205 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, parlevel1
, i
;
5206 int size1
, align1
, expr_type
;
5210 if (type
->t
& VT_VLA
) {
5213 /* save current stack pointer */
5214 if (vla_flags
& VLA_NEED_NEW_FRAME
) {
5216 vla_flags
= VLA_IN_SCOPE
;
5217 vla_sp_loc
= &vla_sp_loc_tmp
;
5220 vla_runtime_type_size(type
, &a
);
5221 gen_vla_alloc(type
, a
);
5222 vset(type
, VT_LOCAL
|VT_LVAL
, c
);
5226 } else if (type
->t
& VT_ARRAY
) {
5230 t1
= pointed_type(type
);
5231 size1
= type_size(t1
, &align1
);
5234 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
5237 tcc_error("character array initializer must be a literal,"
5238 " optionally enclosed in braces");
5243 /* only parse strings here if correct type (otherwise: handle
5244 them as ((w)char *) expressions */
5245 if ((tok
== TOK_LSTR
&&
5246 #ifdef TCC_TARGET_PE
5247 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
5249 (t1
->t
& VT_BTYPE
) == VT_INT
5251 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
5252 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5257 /* compute maximum number of chars wanted */
5259 cstr_len
= cstr
->size
;
5261 cstr_len
= cstr
->size
/ sizeof(nwchar_t
);
5264 if (n
>= 0 && nb
> (n
- array_length
))
5265 nb
= n
- array_length
;
5268 tcc_warning("initializer-string for array is too long");
5269 /* in order to go faster for common case (char
5270 string in global variable, we handle it
5272 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
5273 memcpy(sec
->data
+ c
+ array_length
, cstr
->data
, nb
);
5277 ch
= ((unsigned char *)cstr
->data
)[i
];
5279 ch
= ((nwchar_t
*)cstr
->data
)[i
];
5280 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
5288 /* only add trailing zero if enough storage (no
5289 warning in this case since it is standard) */
5290 if (n
< 0 || array_length
< n
) {
5292 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
5298 while (tok
!= '}') {
5299 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
5300 if (n
>= 0 && index
>= n
)
5301 tcc_error("index too large");
5302 /* must put zero in holes (note that doing it that way
5303 ensures that it even works with designators) */
5304 if (!size_only
&& array_length
< index
) {
5305 init_putz(t1
, sec
, c
+ array_length
* size1
,
5306 (index
- array_length
) * size1
);
5309 if (index
> array_length
)
5310 array_length
= index
;
5311 /* special test for multi dimensional arrays (may not
5312 be strictly correct if designators are used at the
5314 if (index
>= n
&& no_oblock
)
5323 /* put zeros at the end */
5324 if (!size_only
&& n
>= 0 && array_length
< n
) {
5325 init_putz(t1
, sec
, c
+ array_length
* size1
,
5326 (n
- array_length
) * size1
);
5328 /* patch type size if needed */
5330 s
->c
= array_length
;
5331 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
5332 (sec
|| !first
|| tok
== '{')) {
5335 /* NOTE: the previous test is a specific case for automatic
5336 struct/union init */
5337 /* XXX: union needs only one init */
5339 /* XXX: this test is incorrect for local initializers
5340 beginning with ( without {. It would be much more difficult
5341 to do it correctly (ideally, the expression parser should
5342 be used in all cases) */
5348 while (tok
== '(') {
5352 if (!parse_btype(&type1
, &ad1
))
5354 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
5356 if (!is_assignable_types(type
, &type1
))
5357 tcc_error("invalid type for cast");
5362 if (first
|| tok
== '{') {
5371 while (tok
!= '}') {
5372 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
5374 if (!size_only
&& array_length
< index
) {
5375 init_putz(type
, sec
, c
+ array_length
,
5376 index
- array_length
);
5378 index
= index
+ type_size(&f
->type
, &align1
);
5379 if (index
> array_length
)
5380 array_length
= index
;
5382 /* gr: skip fields from same union - ugly. */
5384 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
5385 /* test for same offset */
5386 if (f
->next
->c
!= f
->c
)
5388 /* if yes, test for bitfield shift */
5389 if ((f
->type
.t
& VT_BITFIELD
) && (f
->next
->type
.t
& VT_BITFIELD
)) {
5390 int bit_pos_1
= (f
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5391 int bit_pos_2
= (f
->next
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5392 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
5393 if (bit_pos_1
!= bit_pos_2
)
5400 if (no_oblock
&& f
== NULL
)
5406 /* put zeros at the end */
5407 if (!size_only
&& array_length
< n
) {
5408 init_putz(type
, sec
, c
+ array_length
,
5417 } else if (tok
== '{') {
5419 decl_initializer(type
, sec
, c
, first
, size_only
);
5421 } else if (size_only
) {
5422 /* just skip expression */
5423 parlevel
= parlevel1
= 0;
5424 while ((parlevel
> 0 || parlevel1
> 0 ||
5425 (tok
!= '}' && tok
!= ',')) && tok
!= -1) {
5428 else if (tok
== ')')
5430 else if (tok
== '{')
5432 else if (tok
== '}')
5437 /* currently, we always use constant expression for globals
5438 (may change for scripting case) */
5439 expr_type
= EXPR_CONST
;
5441 expr_type
= EXPR_ANY
;
5442 init_putv(type
, sec
, c
, 0, expr_type
);
5446 /* parse an initializer for type 't' if 'has_init' is non zero, and
5447 allocate space in local or global data space ('r' is either
5448 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5449 variable 'v' with an associated name represented by 'asm_label' of
5450 scope 'scope' is declared before initializers are parsed. If 'v' is
5451 zero, then a reference to the new object is put in the value stack.
5452 If 'has_init' is 2, a special parsing is done to handle string
5454 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
5455 int has_init
, int v
, char *asm_label
,
5458 int size
, align
, addr
, data_offset
;
5460 ParseState saved_parse_state
= {0};
5461 TokenString init_str
;
5463 Sym
*flexible_array
;
5465 flexible_array
= NULL
;
5466 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
5467 Sym
*field
= type
->ref
->next
;
5470 field
= field
->next
;
5471 if (field
->type
.t
& VT_ARRAY
&& field
->type
.ref
->c
< 0)
5472 flexible_array
= field
;
5476 size
= type_size(type
, &align
);
5477 /* If unknown size, we must evaluate it before
5478 evaluating initializers because
5479 initializers can generate global data too
5480 (e.g. string pointers or ISOC99 compound
5481 literals). It also simplifies local
5482 initializers handling */
5483 tok_str_new(&init_str
);
5484 if (size
< 0 || (flexible_array
&& has_init
)) {
5486 tcc_error("unknown type size");
5487 /* get all init string */
5488 if (has_init
== 2) {
5489 /* only get strings */
5490 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5491 tok_str_add_tok(&init_str
);
5496 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
5498 tcc_error("unexpected end of file in initializer");
5499 tok_str_add_tok(&init_str
);
5502 else if (tok
== '}') {
5512 tok_str_add(&init_str
, -1);
5513 tok_str_add(&init_str
, 0);
5516 save_parse_state(&saved_parse_state
);
5518 macro_ptr
= init_str
.str
;
5520 decl_initializer(type
, NULL
, 0, 1, 1);
5521 /* prepare second initializer parsing */
5522 macro_ptr
= init_str
.str
;
5525 /* if still unknown size, error */
5526 size
= type_size(type
, &align
);
5528 tcc_error("unknown type size");
5531 size
+= flexible_array
->type
.ref
->c
* pointed_size(&flexible_array
->type
);
5532 /* take into account specified alignment if bigger */
5533 if (ad
->a
.aligned
) {
5534 if (ad
->a
.aligned
> align
)
5535 align
= ad
->a
.aligned
;
5536 } else if (ad
->a
.packed
) {
5539 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
5541 #ifdef CONFIG_TCC_BCHECK
5542 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5546 loc
= (loc
- size
) & -align
;
5548 #ifdef CONFIG_TCC_BCHECK
5549 /* handles bounds */
5550 /* XXX: currently, since we do only one pass, we cannot track
5551 '&' operators, so we add only arrays */
5552 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5553 unsigned long *bounds_ptr
;
5554 /* add padding between regions */
5556 /* then add local bound info */
5557 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(unsigned long));
5558 bounds_ptr
[0] = addr
;
5559 bounds_ptr
[1] = size
;
5563 /* local variable */
5564 sym_push(v
, type
, r
, addr
);
5566 /* push local reference */
5567 vset(type
, r
, addr
);
5573 if (v
&& scope
== VT_CONST
) {
5574 /* see if the symbol was already defined */
5577 if (!is_compatible_types(&sym
->type
, type
))
5578 tcc_error("incompatible types for redefinition of '%s'",
5579 get_tok_str(v
, NULL
));
5580 if (sym
->type
.t
& VT_EXTERN
) {
5581 /* if the variable is extern, it was not allocated */
5582 sym
->type
.t
&= ~VT_EXTERN
;
5583 /* set array size if it was ommited in extern
5585 if ((sym
->type
.t
& VT_ARRAY
) &&
5586 sym
->type
.ref
->c
< 0 &&
5588 sym
->type
.ref
->c
= type
->ref
->c
;
5590 /* we accept several definitions of the same
5591 global variable. this is tricky, because we
5592 must play with the SHN_COMMON type of the symbol */
5593 /* XXX: should check if the variable was already
5594 initialized. It is incorrect to initialized it
5596 /* no init data, we won't add more to the symbol */
5603 /* allocate symbol in corresponding section */
5608 else if (tcc_state
->nocommon
)
5612 data_offset
= sec
->data_offset
;
5613 data_offset
= (data_offset
+ align
- 1) & -align
;
5615 /* very important to increment global pointer at this time
5616 because initializers themselves can create new initializers */
5617 data_offset
+= size
;
5618 #ifdef CONFIG_TCC_BCHECK
5619 /* add padding if bound check */
5620 if (tcc_state
->do_bounds_check
)
5623 sec
->data_offset
= data_offset
;
5624 /* allocate section space to put the data */
5625 if (sec
->sh_type
!= SHT_NOBITS
&&
5626 data_offset
> sec
->data_allocated
)
5627 section_realloc(sec
, data_offset
);
5628 /* align section if needed */
5629 if (align
> sec
->sh_addralign
)
5630 sec
->sh_addralign
= align
;
5632 addr
= 0; /* avoid warning */
5636 if (scope
!= VT_CONST
|| !sym
) {
5637 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
5638 sym
->asm_label
= asm_label
;
5640 /* update symbol definition */
5642 put_extern_sym(sym
, sec
, addr
, size
);
5645 /* put a common area */
5646 put_extern_sym(sym
, NULL
, align
, size
);
5647 /* XXX: find a nicer way */
5648 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
5649 esym
->st_shndx
= SHN_COMMON
;
5652 /* push global reference */
5653 sym
= get_sym_ref(type
, sec
, addr
, size
);
5654 vpushsym(type
, sym
);
5656 /* patch symbol weakness */
5657 if (type
->t
& VT_WEAK
)
5659 #ifdef CONFIG_TCC_BCHECK
5660 /* handles bounds now because the symbol must be defined
5661 before for the relocation */
5662 if (tcc_state
->do_bounds_check
) {
5663 unsigned long *bounds_ptr
;
5665 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
5666 /* then add global bound info */
5667 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(long));
5668 bounds_ptr
[0] = 0; /* relocated */
5669 bounds_ptr
[1] = size
;
5673 if (has_init
|| (type
->t
& VT_VLA
)) {
5674 decl_initializer(type
, sec
, addr
, 1, 0);
5675 /* restore parse state if needed */
5677 tok_str_free(init_str
.str
);
5678 restore_parse_state(&saved_parse_state
);
5680 /* patch flexible array member size back to -1, */
5681 /* for possible subsequent similar declarations */
5683 flexible_array
->type
.ref
->c
= -1;
5688 static void put_func_debug(Sym
*sym
)
5693 /* XXX: we put here a dummy type */
5694 snprintf(buf
, sizeof(buf
), "%s:%c1",
5695 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
5696 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
5697 cur_text_section
, sym
->c
);
5698 /* //gr gdb wants a line at the function */
5699 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
5704 /* parse an old style function declaration list */
5705 /* XXX: check multiple parameter */
5706 static void func_decl_list(Sym
*func_sym
)
5713 /* parse each declaration */
5714 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
&&
5715 tok
!= TOK_ASM1
&& tok
!= TOK_ASM2
&& tok
!= TOK_ASM3
) {
5716 if (!parse_btype(&btype
, &ad
))
5717 expect("declaration list");
5718 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5719 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5721 /* we accept no variable after */
5725 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5726 /* find parameter in function parameter list */
5729 if ((s
->v
& ~SYM_FIELD
) == v
)
5733 tcc_error("declaration for parameter '%s' but no such parameter",
5734 get_tok_str(v
, NULL
));
5736 /* check that no storage specifier except 'register' was given */
5737 if (type
.t
& VT_STORAGE
)
5738 tcc_error("storage class specified for '%s'", get_tok_str(v
, NULL
));
5739 convert_parameter_type(&type
);
5740 /* we can add the type (NOTE: it could be local to the function) */
5742 /* accept other parameters */
5753 /* parse a function defined by symbol 'sym' and generate its code in
5754 'cur_text_section' */
5755 static void gen_function(Sym
*sym
)
5757 int saved_nocode_wanted
= nocode_wanted
;
5759 ind
= cur_text_section
->data_offset
;
5760 /* NOTE: we patch the symbol size later */
5761 put_extern_sym(sym
, cur_text_section
, ind
, 0);
5762 funcname
= get_tok_str(sym
->v
, NULL
);
5764 /* Initialize VLA state */
5765 vla_sp_loc
= &vla_sp_root_loc
;
5766 vla_flags
= VLA_NEED_NEW_FRAME
;
5767 /* put debug symbol */
5768 if (tcc_state
->do_debug
)
5769 put_func_debug(sym
);
5770 /* push a dummy symbol to enable local sym storage */
5771 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
5772 gfunc_prolog(&sym
->type
);
5773 #ifdef CONFIG_TCC_BCHECK
5774 if (tcc_state
->do_bounds_check
5775 && !strcmp(get_tok_str(sym
->v
, NULL
), "main")) {
5779 for (i
= 0, sym
= local_stack
; i
< 2; i
++, sym
= sym
->prev
) {
5780 if (sym
->v
& SYM_FIELD
|| sym
->prev
->v
& SYM_FIELD
)
5782 vpush_global_sym(&func_old_type
, TOK___bound_main_arg
);
5783 vset(&sym
->type
, sym
->r
, sym
->c
);
5789 block(NULL
, NULL
, NULL
, NULL
, 0, 0);
5792 cur_text_section
->data_offset
= ind
;
5793 label_pop(&global_label_stack
, NULL
);
5794 /* reset local stack */
5795 scope_stack_bottom
= NULL
;
5796 sym_pop(&local_stack
, NULL
);
5797 /* end of function */
5798 /* patch symbol size */
5799 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
5801 /* patch symbol weakness (this definition overrules any prototype) */
5802 if (sym
->type
.t
& VT_WEAK
)
5804 if (tcc_state
->do_debug
) {
5805 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
5807 /* It's better to crash than to generate wrong code */
5808 cur_text_section
= NULL
;
5809 funcname
= ""; /* for safety */
5810 func_vt
.t
= VT_VOID
; /* for safety */
5811 func_var
= 0; /* for safety */
5812 ind
= 0; /* for safety */
5813 nocode_wanted
= saved_nocode_wanted
;
5816 ST_FUNC
void gen_inline_functions(void)
5819 int *str
, inline_generated
, i
;
5820 struct InlineFunc
*fn
;
5822 /* iterate while inline function are referenced */
5824 inline_generated
= 0;
5825 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5826 fn
= tcc_state
->inline_fns
[i
];
5828 if (sym
&& sym
->c
) {
5829 /* the function was used: generate its code and
5830 convert it to a normal function */
5831 str
= fn
->token_str
;
5834 pstrcpy(file
->filename
, sizeof file
->filename
, fn
->filename
);
5835 sym
->r
= VT_SYM
| VT_CONST
;
5836 sym
->type
.t
&= ~VT_INLINE
;
5840 cur_text_section
= text_section
;
5842 macro_ptr
= NULL
; /* fail safe */
5844 inline_generated
= 1;
5847 if (!inline_generated
)
5850 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5851 fn
= tcc_state
->inline_fns
[i
];
5852 str
= fn
->token_str
;
5855 dynarray_reset(&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
);
5858 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
5859 static int decl0(int l
, int is_for_loop_init
)
5867 if (!parse_btype(&btype
, &ad
)) {
5868 if (is_for_loop_init
)
5870 /* skip redundant ';' */
5871 /* XXX: find more elegant solution */
5876 if (l
== VT_CONST
&&
5877 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5878 /* global asm block */
5882 /* special test for old K&R protos without explicit int
5883 type. Only accepted when defining global data */
5884 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
5888 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5889 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5891 /* we accept no variable after */
5895 while (1) { /* iterate thru each declaration */
5896 char *asm_label
; // associated asm label
5898 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5902 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
5903 printf("type = '%s'\n", buf
);
5906 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5907 if ((type
.t
& VT_STATIC
) && (l
== VT_LOCAL
)) {
5908 tcc_error("function without file scope cannot be static");
5910 /* if old style function prototype, we accept a
5913 if (sym
->c
== FUNC_OLD
)
5914 func_decl_list(sym
);
5918 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5921 asm_label_instr(&astr
);
5922 asm_label
= tcc_strdup(astr
.data
);
5925 /* parse one last attribute list, after asm label */
5926 parse_attribute(&ad
);
5931 #ifdef TCC_TARGET_PE
5932 if (ad
.a
.func_import
)
5933 type
.t
|= VT_IMPORT
;
5934 if (ad
.a
.func_export
)
5935 type
.t
|= VT_EXPORT
;
5939 tcc_error("cannot use local functions");
5940 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
5941 expect("function definition");
5943 /* reject abstract declarators in function definition */
5945 while ((sym
= sym
->next
) != NULL
)
5946 if (!(sym
->v
& ~SYM_FIELD
))
5947 expect("identifier");
5949 /* XXX: cannot do better now: convert extern line to static inline */
5950 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
5951 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5956 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
5959 ref
= sym
->type
.ref
;
5960 if (0 == ref
->a
.func_proto
)
5961 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
5963 /* use func_call from prototype if not defined */
5964 if (ref
->a
.func_call
!= FUNC_CDECL
5965 && type
.ref
->a
.func_call
== FUNC_CDECL
)
5966 type
.ref
->a
.func_call
= ref
->a
.func_call
;
5968 /* use export from prototype */
5969 if (ref
->a
.func_export
)
5970 type
.ref
->a
.func_export
= 1;
5972 /* use static from prototype */
5973 if (sym
->type
.t
& VT_STATIC
)
5974 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5976 if (!is_compatible_types(&sym
->type
, &type
)) {
5978 tcc_error("incompatible types for redefinition of '%s'",
5979 get_tok_str(v
, NULL
));
5981 type
.ref
->a
.func_proto
= 0;
5982 /* if symbol is already defined, then put complete type */
5985 /* put function symbol */
5986 sym
= global_identifier_push(v
, type
.t
, 0);
5987 sym
->type
.ref
= type
.ref
;
5990 /* static inline functions are just recorded as a kind
5991 of macro. Their code will be emitted at the end of
5992 the compilation unit only if they are used */
5993 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
5994 (VT_INLINE
| VT_STATIC
)) {
5995 TokenString func_str
;
5997 struct InlineFunc
*fn
;
5998 const char *filename
;
6000 tok_str_new(&func_str
);
6006 tcc_error("unexpected end of file");
6007 tok_str_add_tok(&func_str
);
6012 } else if (t
== '}') {
6014 if (block_level
== 0)
6018 tok_str_add(&func_str
, -1);
6019 tok_str_add(&func_str
, 0);
6020 filename
= file
? file
->filename
: "";
6021 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
6022 strcpy(fn
->filename
, filename
);
6024 fn
->token_str
= func_str
.str
;
6025 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
6028 /* compute text section */
6029 cur_text_section
= ad
.section
;
6030 if (!cur_text_section
)
6031 cur_text_section
= text_section
;
6032 sym
->r
= VT_SYM
| VT_CONST
;
6037 if (btype
.t
& VT_TYPEDEF
) {
6038 /* save typedefed type */
6039 /* XXX: test storage specifiers ? */
6040 sym
= sym_push(v
, &type
, 0, 0);
6042 sym
->type
.t
|= VT_TYPEDEF
;
6045 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
6046 /* external function definition */
6047 /* specific case for func_call attribute */
6048 ad
.a
.func_proto
= 1;
6050 } else if (!(type
.t
& VT_ARRAY
)) {
6051 /* not lvalue if array */
6052 r
|= lvalue_type(type
.t
);
6054 has_init
= (tok
== '=');
6055 if (has_init
&& (type
.t
& VT_VLA
))
6056 tcc_error("Variable length array cannot be initialized");
6057 if ((btype
.t
& VT_EXTERN
) || ((type
.t
& VT_BTYPE
) == VT_FUNC
) ||
6058 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
6059 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
6060 /* external variable or function */
6061 /* NOTE: as GCC, uninitialized global static
6062 arrays of null size are considered as
6064 sym
= external_sym(v
, &type
, r
, asm_label
);
6066 if (type
.t
& VT_WEAK
)
6069 if (ad
.alias_target
) {
6074 alias_target
= sym_find(ad
.alias_target
);
6075 if (!alias_target
|| !alias_target
->c
)
6076 tcc_error("unsupported forward __alias__ attribute");
6077 esym
= &((Elf32_Sym
*)symtab_section
->data
)[alias_target
->c
];
6078 tsec
.sh_num
= esym
->st_shndx
;
6079 put_extern_sym2(sym
, &tsec
, esym
->st_value
, esym
->st_size
, 0);
6082 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
6083 if (type
.t
& VT_STATIC
)
6089 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, asm_label
, l
);
6093 if (is_for_loop_init
)
6106 ST_FUNC
void decl(int l
)