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 vsetc(type
, VT_CONST
, &cval
);
335 /* push integer constant */
336 ST_FUNC
void vpushi(int v
)
340 vsetc(&int_type
, VT_CONST
, &cval
);
343 /* push a pointer sized constant */
344 static void vpushs(long long v
)
351 vsetc(&size_type
, VT_CONST
, &cval
);
354 /* push arbitrary 64bit constant */
355 void vpush64(int ty
, unsigned long long v
)
362 vsetc(&ctype
, VT_CONST
, &cval
);
365 /* push long long constant */
366 static inline void vpushll(long long v
)
368 vpush64(VT_LLONG
, v
);
371 /* push a symbol value of TYPE */
372 static inline void vpushsym(CType
*type
, Sym
*sym
)
377 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
381 /* Return a static symbol pointing to a section */
382 ST_FUNC Sym
*get_sym_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
388 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
389 sym
->type
.ref
= type
->ref
;
390 sym
->r
= VT_CONST
| VT_SYM
;
391 put_extern_sym(sym
, sec
, offset
, size
);
395 /* push a reference to a section offset by adding a dummy symbol */
396 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
398 vpushsym(type
, get_sym_ref(type
, sec
, offset
, size
));
401 /* define a new external reference to a symbol 'v' of type 'u' */
402 ST_FUNC Sym
*external_global_sym(int v
, CType
*type
, int r
)
408 /* push forward reference */
409 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
410 s
->type
.ref
= type
->ref
;
411 s
->r
= r
| VT_CONST
| VT_SYM
;
416 /* define a new external reference to a symbol 'v' with alternate asm
417 name 'asm_label' of type 'u'. 'asm_label' is equal to NULL if there
418 is no alternate name (most cases) */
419 static Sym
*external_sym(int v
, CType
*type
, int r
, char *asm_label
)
425 /* push forward reference */
426 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
427 s
->asm_label
= asm_label
;
428 s
->type
.t
|= VT_EXTERN
;
429 } else if (s
->type
.ref
== func_old_type
.ref
) {
430 s
->type
.ref
= type
->ref
;
431 s
->r
= r
| VT_CONST
| VT_SYM
;
432 s
->type
.t
|= VT_EXTERN
;
433 } else if (!is_compatible_types(&s
->type
, type
)) {
434 tcc_error("incompatible types for redefinition of '%s'",
435 get_tok_str(v
, NULL
));
440 /* push a reference to global symbol v */
441 ST_FUNC
void vpush_global_sym(CType
*type
, int v
)
443 vpushsym(type
, external_global_sym(v
, type
, 0));
446 ST_FUNC
void vset(CType
*type
, int r
, int v
)
451 vsetc(type
, r
, &cval
);
454 static void vseti(int r
, int v
)
462 ST_FUNC
void vswap(void)
465 /* cannot let cpu flags if other instruction are generated. Also
466 avoid leaving VT_JMP anywhere except on the top of the stack
467 because it would complicate the code generator. */
468 if (vtop
>= vstack
) {
469 int v
= vtop
->r
& VT_VALMASK
;
470 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
477 /* XXX: +2% overall speed possible with optimized memswap
479 * memswap(&vtop[0], &vtop[1], sizeof *vtop);
483 ST_FUNC
void vpushv(SValue
*v
)
485 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
486 tcc_error("memory full (vstack)");
491 static void vdup(void)
496 /* save r to the memory stack, and mark it as being free */
497 ST_FUNC
void save_reg(int r
)
499 int l
, saved
, size
, align
;
503 /* modify all stack values */
506 for(p
=vstack
;p
<=vtop
;p
++) {
507 if ((p
->r
& VT_VALMASK
) == r
||
508 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
509 /* must save value on stack if not already done */
511 /* NOTE: must reload 'r' because r might be equal to r2 */
512 r
= p
->r
& VT_VALMASK
;
513 /* store register in the stack */
515 if ((p
->r
& VT_LVAL
) ||
516 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
517 #ifdef TCC_TARGET_X86_64
518 type
= &char_pointer_type
;
522 size
= type_size(type
, &align
);
523 loc
= (loc
- size
) & -align
;
525 sv
.r
= VT_LOCAL
| VT_LVAL
;
528 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
529 /* x86 specific: need to pop fp register ST0 if saved */
531 o(0xd8dd); /* fstp %st(0) */
534 #ifndef TCC_TARGET_X86_64
535 /* special long long case */
536 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
544 /* mark that stack entry as being saved on the stack */
545 if (p
->r
& VT_LVAL
) {
546 /* also clear the bounded flag because the
547 relocation address of the function was stored in
549 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
551 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
559 #ifdef TCC_TARGET_ARM
560 /* find a register of class 'rc2' with at most one reference on stack.
561 * If none, call get_reg(rc) */
562 ST_FUNC
int get_reg_ex(int rc
, int rc2
)
567 for(r
=0;r
<NB_REGS
;r
++) {
568 if (reg_classes
[r
] & rc2
) {
571 for(p
= vstack
; p
<= vtop
; p
++) {
572 if ((p
->r
& VT_VALMASK
) == r
||
573 (p
->r2
& VT_VALMASK
) == r
)
584 /* find a free register of class 'rc'. If none, save one register */
585 ST_FUNC
int get_reg(int rc
)
590 /* find a free register */
591 for(r
=0;r
<NB_REGS
;r
++) {
592 if (reg_classes
[r
] & rc
) {
593 for(p
=vstack
;p
<=vtop
;p
++) {
594 if ((p
->r
& VT_VALMASK
) == r
||
595 (p
->r2
& VT_VALMASK
) == r
)
603 /* no register left : free the first one on the stack (VERY
604 IMPORTANT to start from the bottom to ensure that we don't
605 spill registers used in gen_opi()) */
606 for(p
=vstack
;p
<=vtop
;p
++) {
607 /* look at second register (if long long) */
608 r
= p
->r2
& VT_VALMASK
;
609 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
611 r
= p
->r
& VT_VALMASK
;
612 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
618 /* Should never comes here */
622 /* save registers up to (vtop - n) stack entry */
623 ST_FUNC
void save_regs(int n
)
628 for(p
= vstack
;p
<= p1
; p
++) {
629 r
= p
->r
& VT_VALMASK
;
636 /* move register 's' (of type 't') to 'r', and flush previous value of r to memory
638 static void move_reg(int r
, int s
, int t
)
652 /* get address of vtop (vtop MUST BE an lvalue) */
653 static void gaddrof(void)
655 if (vtop
->r
& VT_REF
)
658 /* tricky: if saved lvalue, then we can go back to lvalue */
659 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
660 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
665 #ifdef CONFIG_TCC_BCHECK
666 /* generate lvalue bound code */
667 static void gbound(void)
672 vtop
->r
&= ~VT_MUSTBOUND
;
673 /* if lvalue, then use checking code before dereferencing */
674 if (vtop
->r
& VT_LVAL
) {
675 /* if not VT_BOUNDED value, then make one */
676 if (!(vtop
->r
& VT_BOUNDED
)) {
677 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
678 /* must save type because we must set it to int to get pointer */
680 vtop
->type
.t
= VT_INT
;
683 gen_bounded_ptr_add();
684 vtop
->r
|= lval_type
;
687 /* then check for dereferencing */
688 gen_bounded_ptr_deref();
693 /* store vtop a register belonging to class 'rc'. lvalues are
694 converted to values. Cannot be used if cannot be converted to
695 register value (such as structures). */
696 ST_FUNC
int gv(int rc
)
698 int r
, bit_pos
, bit_size
, size
, align
, i
;
701 /* NOTE: get_reg can modify vstack[] */
702 if (vtop
->type
.t
& VT_BITFIELD
) {
705 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
706 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
707 /* remove bit field info to avoid loops */
708 vtop
->type
.t
&= ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
709 /* cast to int to propagate signedness in following ops */
710 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
715 if((vtop
->type
.t
& VT_UNSIGNED
) ||
716 (vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
717 type
.t
|= VT_UNSIGNED
;
719 /* generate shifts */
720 vpushi(bits
- (bit_pos
+ bit_size
));
722 vpushi(bits
- bit_size
);
723 /* NOTE: transformed to SHR if unsigned */
727 if (is_float(vtop
->type
.t
) &&
728 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
731 unsigned long offset
;
732 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
736 /* XXX: unify with initializers handling ? */
737 /* CPUs usually cannot use float constants, so we store them
738 generically in data segment */
739 size
= type_size(&vtop
->type
, &align
);
740 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
741 data_section
->data_offset
= offset
;
742 /* XXX: not portable yet */
743 #if defined(__i386__) || defined(__x86_64__)
744 /* Zero pad x87 tenbyte long doubles */
745 if (size
== LDOUBLE_SIZE
) {
746 vtop
->c
.tab
[2] &= 0xffff;
747 #if LDOUBLE_SIZE == 16
752 ptr
= section_ptr_add(data_section
, size
);
754 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
758 ptr
[i
] = vtop
->c
.tab
[size
-1-i
];
762 ptr
[i
] = vtop
->c
.tab
[i
];
763 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
764 vtop
->r
|= VT_LVAL
| VT_SYM
;
768 #ifdef CONFIG_TCC_BCHECK
769 if (vtop
->r
& VT_MUSTBOUND
)
773 r
= vtop
->r
& VT_VALMASK
;
774 rc2
= (rc
& RC_FLOAT
) ? RC_FLOAT
: RC_INT
;
777 #ifdef TCC_TARGET_X86_64
778 else if (rc
== RC_FRET
)
782 /* need to reload if:
784 - lvalue (need to dereference pointer)
785 - already a register, but not in the right class */
787 || (vtop
->r
& VT_LVAL
)
788 || !(reg_classes
[r
] & rc
)
789 #ifdef TCC_TARGET_X86_64
790 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
791 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
&& !(reg_classes
[vtop
->r2
] & rc2
))
793 || ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
798 #ifdef TCC_TARGET_X86_64
799 if (((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
)) {
800 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
802 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
803 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
804 unsigned long long ll
;
806 int r2
, original_type
;
807 original_type
= vtop
->type
.t
;
808 /* two register type load : expand to two words
810 #ifndef TCC_TARGET_X86_64
811 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
814 vtop
->c
.ui
= ll
; /* first word */
816 vtop
->r
= r
; /* save register value */
817 vpushi(ll
>> 32); /* second word */
820 if (r
>= VT_CONST
|| /* XXX: test to VT_CONST incorrect ? */
821 (vtop
->r
& VT_LVAL
)) {
822 /* We do not want to modifier the long long
823 pointer here, so the safest (and less
824 efficient) is to save all the other registers
825 in the stack. XXX: totally inefficient. */
827 /* load from memory */
828 vtop
->type
.t
= load_type
;
831 vtop
[-1].r
= r
; /* save register value */
832 /* increment pointer to get second word */
833 vtop
->type
.t
= addr_type
;
838 vtop
->type
.t
= load_type
;
843 vtop
[-1].r
= r
; /* save register value */
844 vtop
->r
= vtop
[-1].r2
;
846 /* Allocate second register. Here we rely on the fact that
847 get_reg() tries first to free r2 of an SValue. */
851 /* write second register */
853 vtop
->type
.t
= original_type
;
854 } else if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
856 /* lvalue of scalar type : need to use lvalue type
857 because of possible cast */
860 /* compute memory access type */
861 if (vtop
->r
& VT_REF
)
862 #ifdef TCC_TARGET_X86_64
867 else if (vtop
->r
& VT_LVAL_BYTE
)
869 else if (vtop
->r
& VT_LVAL_SHORT
)
871 if (vtop
->r
& VT_LVAL_UNSIGNED
)
875 /* restore wanted type */
878 /* one register type load */
883 #ifdef TCC_TARGET_C67
884 /* uses register pairs for doubles */
885 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
892 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
893 ST_FUNC
void gv2(int rc1
, int rc2
)
897 /* generate more generic register first. But VT_JMP or VT_CMP
898 values must be generated first in all cases to avoid possible
900 v
= vtop
[0].r
& VT_VALMASK
;
901 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
906 /* test if reload is needed for first register */
907 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
917 /* test if reload is needed for first register */
918 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
924 /* wrapper around RC_FRET to return a register by type */
925 static int rc_fret(int t
)
927 #ifdef TCC_TARGET_X86_64
928 if (t
== VT_LDOUBLE
) {
935 /* wrapper around REG_FRET to return a register by type */
936 static int reg_fret(int t
)
938 #ifdef TCC_TARGET_X86_64
939 if (t
== VT_LDOUBLE
) {
946 /* expand long long on stack in two int registers */
947 static void lexpand(void)
951 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
954 vtop
[0].r
= vtop
[-1].r2
;
955 vtop
[0].r2
= VT_CONST
;
956 vtop
[-1].r2
= VT_CONST
;
957 vtop
[0].type
.t
= VT_INT
| u
;
958 vtop
[-1].type
.t
= VT_INT
| u
;
961 #ifdef TCC_TARGET_ARM
962 /* expand long long on stack */
963 ST_FUNC
void lexpand_nr(void)
967 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
970 vtop
->type
.t
= VT_INT
| u
;
971 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
973 vtop
[-1].c
.ui
= vtop
->c
.ull
;
974 vtop
->c
.ui
= vtop
->c
.ull
>> 32;
976 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
978 vtop
->r
= vtop
[-1].r
;
979 } else if (v
> VT_CONST
) {
983 vtop
->r
= vtop
[-1].r2
;
984 vtop
[-1].r2
= VT_CONST
;
985 vtop
[-1].type
.t
= VT_INT
| u
;
989 /* build a long long from two ints */
990 static void lbuild(int t
)
993 vtop
[-1].r2
= vtop
[0].r
;
998 /* rotate n first stack elements to the bottom
999 I1 ... In -> I2 ... In I1 [top is right]
1001 ST_FUNC
void vrotb(int n
)
1007 for(i
=-n
+1;i
!=0;i
++)
1008 vtop
[i
] = vtop
[i
+1];
1012 /* rotate the n elements before entry e towards the top
1013 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
1015 ST_FUNC
void vrote(SValue
*e
, int n
)
1021 for(i
= 0;i
< n
- 1; i
++)
1026 /* rotate n first stack elements to the top
1027 I1 ... In -> In I1 ... I(n-1) [top is right]
1029 ST_FUNC
void vrott(int n
)
1034 /* pop stack value */
1035 ST_FUNC
void vpop(void)
1038 v
= vtop
->r
& VT_VALMASK
;
1039 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1040 /* for x86, we need to pop the FP stack */
1041 if (v
== TREG_ST0
&& !nocode_wanted
) {
1042 o(0xd8dd); /* fstp %st(0) */
1045 if (v
== VT_JMP
|| v
== VT_JMPI
) {
1046 /* need to put correct jump if && or || without test */
1052 /* convert stack entry to register and duplicate its value in another
1054 static void gv_dup(void)
1060 if ((t
& VT_BTYPE
) == VT_LLONG
) {
1067 /* stack: H L L1 H1 */
1075 /* duplicate value */
1080 #ifdef TCC_TARGET_X86_64
1081 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1091 load(r1
, &sv
); /* move r to r1 */
1093 /* duplicates value */
1099 /* Generate value test
1101 * Generate a test for any value (jump, comparison and integers) */
1102 int gvtst(int inv
, int t
)
1104 int v
= vtop
->r
& VT_VALMASK
;
1105 if (v
!= VT_CMP
&& v
!= VT_JMP
&& v
!= VT_JMPI
) {
1109 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1110 /* constant jmp optimization */
1111 if ((vtop
->c
.i
!= 0) != inv
)
1116 return gtst(inv
, t
);
1119 #ifndef TCC_TARGET_X86_64
1120 /* generate CPU independent (unsigned) long long operations */
1121 static void gen_opl(int op
)
1123 int t
, a
, b
, op1
, c
, i
;
1125 unsigned short reg_iret
= REG_IRET
;
1126 unsigned short reg_lret
= REG_LRET
;
1132 func
= TOK___divdi3
;
1135 func
= TOK___udivdi3
;
1138 func
= TOK___moddi3
;
1141 func
= TOK___umoddi3
;
1148 /* call generic long long function */
1149 vpush_global_sym(&func_old_type
, func
);
1154 vtop
->r2
= reg_lret
;
1167 /* stack: L1 H1 L2 H2 */
1172 vtop
[-2] = vtop
[-3];
1175 /* stack: H1 H2 L1 L2 */
1181 /* stack: H1 H2 L1 L2 ML MH */
1184 /* stack: ML MH H1 H2 L1 L2 */
1188 /* stack: ML MH H1 L2 H2 L1 */
1193 /* stack: ML MH M1 M2 */
1196 } else if (op
== '+' || op
== '-') {
1197 /* XXX: add non carry method too (for MIPS or alpha) */
1203 /* stack: H1 H2 (L1 op L2) */
1206 gen_op(op1
+ 1); /* TOK_xxxC2 */
1209 /* stack: H1 H2 (L1 op L2) */
1212 /* stack: (L1 op L2) H1 H2 */
1214 /* stack: (L1 op L2) (H1 op H2) */
1222 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1223 t
= vtop
[-1].type
.t
;
1227 /* stack: L H shift */
1229 /* constant: simpler */
1230 /* NOTE: all comments are for SHL. the other cases are
1231 done by swaping words */
1242 if (op
!= TOK_SAR
) {
1275 /* XXX: should provide a faster fallback on x86 ? */
1278 func
= TOK___ashrdi3
;
1281 func
= TOK___lshrdi3
;
1284 func
= TOK___ashldi3
;
1290 /* compare operations */
1296 /* stack: L1 H1 L2 H2 */
1298 vtop
[-1] = vtop
[-2];
1300 /* stack: L1 L2 H1 H2 */
1303 /* when values are equal, we need to compare low words. since
1304 the jump is inverted, we invert the test too. */
1307 else if (op1
== TOK_GT
)
1309 else if (op1
== TOK_ULT
)
1311 else if (op1
== TOK_UGT
)
1316 if (op1
!= TOK_NE
) {
1320 /* generate non equal test */
1321 /* XXX: NOT PORTABLE yet */
1325 #if defined(TCC_TARGET_I386)
1326 b
= psym(0x850f, 0);
1327 #elif defined(TCC_TARGET_ARM)
1329 o(0x1A000000 | encbranch(ind
, 0, 1));
1330 #elif defined(TCC_TARGET_C67)
1331 tcc_error("not implemented");
1333 #error not supported
1337 /* compare low. Always unsigned */
1341 else if (op1
== TOK_LE
)
1343 else if (op1
== TOK_GT
)
1345 else if (op1
== TOK_GE
)
1356 /* handle integer constant optimizations and various machine
1358 static void gen_opic(int op
)
1360 int c1
, c2
, t1
, t2
, n
;
1363 typedef unsigned long long U
;
1367 t1
= v1
->type
.t
& VT_BTYPE
;
1368 t2
= v2
->type
.t
& VT_BTYPE
;
1372 else if (v1
->type
.t
& VT_UNSIGNED
)
1379 else if (v2
->type
.t
& VT_UNSIGNED
)
1384 /* currently, we cannot do computations with forward symbols */
1385 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1386 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1389 case '+': l1
+= l2
; break;
1390 case '-': l1
-= l2
; break;
1391 case '&': l1
&= l2
; break;
1392 case '^': l1
^= l2
; break;
1393 case '|': l1
|= l2
; break;
1394 case '*': l1
*= l2
; break;
1401 /* if division by zero, generate explicit division */
1404 tcc_error("division by zero in constant");
1408 default: l1
/= l2
; break;
1409 case '%': l1
%= l2
; break;
1410 case TOK_UDIV
: l1
= (U
)l1
/ l2
; break;
1411 case TOK_UMOD
: l1
= (U
)l1
% l2
; break;
1414 case TOK_SHL
: l1
<<= l2
; break;
1415 case TOK_SHR
: l1
= (U
)l1
>> l2
; break;
1416 case TOK_SAR
: l1
>>= l2
; break;
1418 case TOK_ULT
: l1
= (U
)l1
< (U
)l2
; break;
1419 case TOK_UGE
: l1
= (U
)l1
>= (U
)l2
; break;
1420 case TOK_EQ
: l1
= l1
== l2
; break;
1421 case TOK_NE
: l1
= l1
!= l2
; break;
1422 case TOK_ULE
: l1
= (U
)l1
<= (U
)l2
; break;
1423 case TOK_UGT
: l1
= (U
)l1
> (U
)l2
; break;
1424 case TOK_LT
: l1
= l1
< l2
; break;
1425 case TOK_GE
: l1
= l1
>= l2
; break;
1426 case TOK_LE
: l1
= l1
<= l2
; break;
1427 case TOK_GT
: l1
= l1
> l2
; break;
1429 case TOK_LAND
: l1
= l1
&& l2
; break;
1430 case TOK_LOR
: l1
= l1
|| l2
; break;
1437 /* if commutative ops, put c2 as constant */
1438 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1439 op
== '|' || op
== '*')) {
1441 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1442 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1444 /* Filter out NOP operations like x*1, x-0, x&-1... */
1445 if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1448 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1449 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1455 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1456 /* try to use shifts instead of muls or divs */
1457 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1466 else if (op
== TOK_PDIV
)
1472 } else if (c2
&& (op
== '+' || op
== '-') &&
1473 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
1474 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1475 /* symbol + constant case */
1482 if (!nocode_wanted
) {
1483 /* call low level op generator */
1484 if (t1
== VT_LLONG
|| t2
== VT_LLONG
)
1495 /* generate a floating point operation with constant propagation */
1496 static void gen_opif(int op
)
1504 /* currently, we cannot do computations with forward symbols */
1505 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1506 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1508 if (v1
->type
.t
== VT_FLOAT
) {
1511 } else if (v1
->type
.t
== VT_DOUBLE
) {
1519 /* NOTE: we only do constant propagation if finite number (not
1520 NaN or infinity) (ANSI spec) */
1521 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1525 case '+': f1
+= f2
; break;
1526 case '-': f1
-= f2
; break;
1527 case '*': f1
*= f2
; break;
1531 tcc_error("division by zero in constant");
1536 /* XXX: also handles tests ? */
1540 /* XXX: overflow test ? */
1541 if (v1
->type
.t
== VT_FLOAT
) {
1543 } else if (v1
->type
.t
== VT_DOUBLE
) {
1551 if (!nocode_wanted
) {
1559 static int pointed_size(CType
*type
)
1562 return type_size(pointed_type(type
), &align
);
1565 static void vla_runtime_pointed_size(CType
*type
)
1568 vla_runtime_type_size(pointed_type(type
), &align
);
1571 static inline int is_null_pointer(SValue
*p
)
1573 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1575 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& p
->c
.i
== 0) ||
1576 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.ll
== 0) ||
1577 ((p
->type
.t
& VT_BTYPE
) == VT_PTR
&& p
->c
.ptr
== 0);
1580 static inline int is_integer_btype(int bt
)
1582 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
1583 bt
== VT_INT
|| bt
== VT_LLONG
);
1586 /* check types for comparison or substraction of pointers */
1587 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
1589 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
1592 /* null pointers are accepted for all comparisons as gcc */
1593 if (is_null_pointer(p1
) || is_null_pointer(p2
))
1597 bt1
= type1
->t
& VT_BTYPE
;
1598 bt2
= type2
->t
& VT_BTYPE
;
1599 /* accept comparison between pointer and integer with a warning */
1600 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
1601 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
1602 tcc_warning("comparison between pointer and integer");
1606 /* both must be pointers or implicit function pointers */
1607 if (bt1
== VT_PTR
) {
1608 type1
= pointed_type(type1
);
1609 } else if (bt1
!= VT_FUNC
)
1610 goto invalid_operands
;
1612 if (bt2
== VT_PTR
) {
1613 type2
= pointed_type(type2
);
1614 } else if (bt2
!= VT_FUNC
) {
1616 tcc_error("invalid operands to binary %s", get_tok_str(op
, NULL
));
1618 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1619 (type2
->t
& VT_BTYPE
) == VT_VOID
)
1623 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1624 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1625 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1626 /* gcc-like error if '-' is used */
1628 goto invalid_operands
;
1630 tcc_warning("comparison of distinct pointer types lacks a cast");
1634 /* generic gen_op: handles types problems */
1635 ST_FUNC
void gen_op(int op
)
1637 int u
, t1
, t2
, bt1
, bt2
, t
;
1640 t1
= vtop
[-1].type
.t
;
1641 t2
= vtop
[0].type
.t
;
1642 bt1
= t1
& VT_BTYPE
;
1643 bt2
= t2
& VT_BTYPE
;
1645 if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
1646 /* at least one operand is a pointer */
1647 /* relationnal op: must be both pointers */
1648 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
1649 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1650 /* pointers are handled are unsigned */
1651 #ifdef TCC_TARGET_X86_64
1652 t
= VT_LLONG
| VT_UNSIGNED
;
1654 t
= VT_INT
| VT_UNSIGNED
;
1658 /* if both pointers, then it must be the '-' op */
1659 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
1661 tcc_error("cannot use pointers here");
1662 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1663 /* XXX: check that types are compatible */
1664 if (vtop
[-1].type
.t
& VT_VLA
) {
1665 vla_runtime_pointed_size(&vtop
[-1].type
);
1667 vpushi(pointed_size(&vtop
[-1].type
));
1671 /* set to integer type */
1672 #ifdef TCC_TARGET_X86_64
1673 vtop
->type
.t
= VT_LLONG
;
1675 vtop
->type
.t
= VT_INT
;
1680 /* exactly one pointer : must be '+' or '-'. */
1681 if (op
!= '-' && op
!= '+')
1682 tcc_error("cannot use pointers here");
1683 /* Put pointer as first operand */
1684 if (bt2
== VT_PTR
) {
1688 type1
= vtop
[-1].type
;
1689 type1
.t
&= ~VT_ARRAY
;
1690 if (vtop
[-1].type
.t
& VT_VLA
)
1691 vla_runtime_pointed_size(&vtop
[-1].type
);
1693 u
= pointed_size(&vtop
[-1].type
);
1695 tcc_error("unknown array element size");
1696 #ifdef TCC_TARGET_X86_64
1699 /* XXX: cast to int ? (long long case) */
1704 #ifdef CONFIG_TCC_BCHECK
1705 /* if evaluating constant expression, no code should be
1706 generated, so no bound check */
1707 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
1708 /* if bounded pointers, we generate a special code to
1715 gen_bounded_ptr_add();
1721 /* put again type if gen_opic() swaped operands */
1724 } else if (is_float(bt1
) || is_float(bt2
)) {
1725 /* compute bigger type and do implicit casts */
1726 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
1728 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
1733 /* floats can only be used for a few operations */
1734 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
1735 (op
< TOK_ULT
|| op
> TOK_GT
))
1736 tcc_error("invalid operands for binary operation");
1738 } else if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
) {
1739 t
= bt1
== VT_LLONG
? VT_LLONG
: VT_INT
;
1740 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (t
| VT_UNSIGNED
))
1743 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
1744 /* cast to biggest op */
1746 /* convert to unsigned if it does not fit in a long long */
1747 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
1748 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
1751 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
1752 tcc_error("comparison of struct");
1754 /* integer operations */
1756 /* convert to unsigned if it does not fit in an integer */
1757 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
1758 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
1761 /* XXX: currently, some unsigned operations are explicit, so
1762 we modify them here */
1763 if (t
& VT_UNSIGNED
) {
1770 else if (op
== TOK_LT
)
1772 else if (op
== TOK_GT
)
1774 else if (op
== TOK_LE
)
1776 else if (op
== TOK_GE
)
1783 /* special case for shifts and long long: we keep the shift as
1785 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
1792 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
1793 /* relationnal op: the result is an int */
1794 vtop
->type
.t
= VT_INT
;
1801 #ifndef TCC_TARGET_ARM
1802 /* generic itof for unsigned long long case */
1803 static void gen_cvt_itof1(int t
)
1805 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
1806 (VT_LLONG
| VT_UNSIGNED
)) {
1809 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
1810 #if LDOUBLE_SIZE != 8
1811 else if (t
== VT_LDOUBLE
)
1812 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
1815 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
1819 vtop
->r
= reg_fret(t
);
1826 /* generic ftoi for unsigned long long case */
1827 static void gen_cvt_ftoi1(int t
)
1831 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
1832 /* not handled natively */
1833 st
= vtop
->type
.t
& VT_BTYPE
;
1835 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
1836 #if LDOUBLE_SIZE != 8
1837 else if (st
== VT_LDOUBLE
)
1838 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
1841 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
1846 vtop
->r2
= REG_LRET
;
1852 /* force char or short cast */
1853 static void force_charshort_cast(int t
)
1857 /* XXX: add optimization if lvalue : just change type and offset */
1862 if (t
& VT_UNSIGNED
) {
1863 vpushi((1 << bits
) - 1);
1869 /* result must be signed or the SAR is converted to an SHL
1870 This was not the case when "t" was a signed short
1871 and the last value on the stack was an unsigned int */
1872 vtop
->type
.t
&= ~VT_UNSIGNED
;
1878 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1879 static void gen_cast(CType
*type
)
1881 int sbt
, dbt
, sf
, df
, c
, p
;
1883 /* special delayed cast for char/short */
1884 /* XXX: in some cases (multiple cascaded casts), it may still
1886 if (vtop
->r
& VT_MUSTCAST
) {
1887 vtop
->r
&= ~VT_MUSTCAST
;
1888 force_charshort_cast(vtop
->type
.t
);
1891 /* bitfields first get cast to ints */
1892 if (vtop
->type
.t
& VT_BITFIELD
) {
1896 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
1897 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
1902 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1903 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
1905 /* constant case: we can do it now */
1906 /* XXX: in ISOC, cannot do it if error in convert */
1907 if (sbt
== VT_FLOAT
)
1908 vtop
->c
.ld
= vtop
->c
.f
;
1909 else if (sbt
== VT_DOUBLE
)
1910 vtop
->c
.ld
= vtop
->c
.d
;
1913 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
1914 if (sbt
& VT_UNSIGNED
)
1915 vtop
->c
.ld
= vtop
->c
.ull
;
1917 vtop
->c
.ld
= vtop
->c
.ll
;
1919 if (sbt
& VT_UNSIGNED
)
1920 vtop
->c
.ld
= vtop
->c
.ui
;
1922 vtop
->c
.ld
= vtop
->c
.i
;
1925 if (dbt
== VT_FLOAT
)
1926 vtop
->c
.f
= (float)vtop
->c
.ld
;
1927 else if (dbt
== VT_DOUBLE
)
1928 vtop
->c
.d
= (double)vtop
->c
.ld
;
1929 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
1930 vtop
->c
.ull
= (unsigned long long)vtop
->c
.ld
;
1931 } else if (sf
&& dbt
== VT_BOOL
) {
1932 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
1935 vtop
->c
.ll
= (long long)vtop
->c
.ld
;
1936 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
1937 vtop
->c
.ll
= vtop
->c
.ull
;
1938 else if (sbt
& VT_UNSIGNED
)
1939 vtop
->c
.ll
= vtop
->c
.ui
;
1940 #ifdef TCC_TARGET_X86_64
1941 else if (sbt
== VT_PTR
)
1944 else if (sbt
!= VT_LLONG
)
1945 vtop
->c
.ll
= vtop
->c
.i
;
1947 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
1948 vtop
->c
.ull
= vtop
->c
.ll
;
1949 else if (dbt
== VT_BOOL
)
1950 vtop
->c
.i
= (vtop
->c
.ll
!= 0);
1951 #ifdef TCC_TARGET_X86_64
1952 else if (dbt
== VT_PTR
)
1955 else if (dbt
!= VT_LLONG
) {
1957 if ((dbt
& VT_BTYPE
) == VT_BYTE
)
1959 else if ((dbt
& VT_BTYPE
) == VT_SHORT
)
1961 if(dbt
& VT_UNSIGNED
)
1962 vtop
->c
.ui
= ((unsigned int)vtop
->c
.ll
<< s
) >> s
;
1964 vtop
->c
.i
= ((int)vtop
->c
.ll
<< s
) >> s
;
1967 } else if (p
&& dbt
== VT_BOOL
) {
1970 } else if (!nocode_wanted
) {
1971 /* non constant case: generate code */
1973 /* convert from fp to fp */
1976 /* convert int to fp */
1979 /* convert fp to int */
1980 if (dbt
== VT_BOOL
) {
1984 /* we handle char/short/etc... with generic code */
1985 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
1986 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
1990 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
1991 /* additional cast for char/short... */
1996 #ifndef TCC_TARGET_X86_64
1997 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
1998 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
1999 /* scalar to long long */
2000 /* machine independent conversion */
2002 /* generate high word */
2003 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
2007 if (sbt
== VT_PTR
) {
2008 /* cast from pointer to int before we apply
2009 shift operation, which pointers don't support*/
2010 gen_cast(&int_type
);
2016 /* patch second register */
2017 vtop
[-1].r2
= vtop
->r
;
2021 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
2022 (dbt
& VT_BTYPE
) == VT_PTR
||
2023 (dbt
& VT_BTYPE
) == VT_FUNC
) {
2024 if ((sbt
& VT_BTYPE
) != VT_LLONG
&&
2025 (sbt
& VT_BTYPE
) != VT_PTR
&&
2026 (sbt
& VT_BTYPE
) != VT_FUNC
) {
2027 /* need to convert from 32bit to 64bit */
2029 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
2030 /* x86_64 specific: movslq */
2032 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
2036 } else if (dbt
== VT_BOOL
) {
2037 /* scalar to bool */
2040 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
2041 (dbt
& VT_BTYPE
) == VT_SHORT
) {
2042 if (sbt
== VT_PTR
) {
2043 vtop
->type
.t
= VT_INT
;
2044 tcc_warning("nonportable conversion from pointer to char/short");
2046 force_charshort_cast(dbt
);
2047 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
2049 if (sbt
== VT_LLONG
) {
2050 /* from long long: just take low order word */
2054 /* if lvalue and single word type, nothing to do because
2055 the lvalue already contains the real type size (see
2056 VT_LVAL_xxx constants) */
2059 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
2060 /* if we are casting between pointer types,
2061 we must update the VT_LVAL_xxx size */
2062 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
2063 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
2068 /* return type size as known at compile time. Put alignment at 'a' */
2069 ST_FUNC
int type_size(CType
*type
, int *a
)
2074 bt
= type
->t
& VT_BTYPE
;
2075 if (bt
== VT_STRUCT
) {
2080 } else if (bt
== VT_PTR
) {
2081 if (type
->t
& VT_ARRAY
) {
2085 ts
= type_size(&s
->type
, a
);
2087 if (ts
< 0 && s
->c
< 0)
2095 } else if (bt
== VT_LDOUBLE
) {
2097 return LDOUBLE_SIZE
;
2098 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
2099 #ifdef TCC_TARGET_I386
2100 #ifdef TCC_TARGET_PE
2105 #elif defined(TCC_TARGET_ARM)
2115 } else if (bt
== VT_INT
|| bt
== VT_ENUM
|| bt
== VT_FLOAT
) {
2118 } else if (bt
== VT_SHORT
) {
2121 } else if (bt
== VT_QLONG
|| bt
== VT_QFLOAT
) {
2125 /* char, void, function, _Bool */
2131 /* push type size as known at runtime time on top of value stack. Put
2133 ST_FUNC
void vla_runtime_type_size(CType
*type
, int *a
)
2135 if (type
->t
& VT_VLA
) {
2136 vset(&int_type
, VT_LOCAL
|VT_LVAL
, type
->ref
->c
);
2138 vpushi(type_size(type
, a
));
2142 static void vla_sp_save(void) {
2143 if (!(vla_flags
& VLA_SP_LOC_SET
)) {
2144 *vla_sp_loc
= (loc
-= PTR_SIZE
);
2145 vla_flags
|= VLA_SP_LOC_SET
;
2147 if (!(vla_flags
& VLA_SP_SAVED
)) {
2148 gen_vla_sp_save(*vla_sp_loc
);
2149 vla_flags
|= VLA_SP_SAVED
;
2153 /* return the pointed type of t */
2154 static inline CType
*pointed_type(CType
*type
)
2156 return &type
->ref
->type
;
2159 /* modify type so that its it is a pointer to type. */
2160 ST_FUNC
void mk_pointer(CType
*type
)
2163 s
= sym_push(SYM_FIELD
, type
, 0, -1);
2164 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
2168 /* compare function types. OLD functions match any new functions */
2169 static int is_compatible_func(CType
*type1
, CType
*type2
)
2175 if (!is_compatible_types(&s1
->type
, &s2
->type
))
2177 /* check func_call */
2178 if (s1
->a
.func_call
!= s2
->a
.func_call
)
2180 /* XXX: not complete */
2181 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
2185 while (s1
!= NULL
) {
2188 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
2198 /* return true if type1 and type2 are the same. If unqualified is
2199 true, qualifiers on the types are ignored.
2201 - enums are not checked as gcc __builtin_types_compatible_p ()
2203 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2207 t1
= type1
->t
& VT_TYPE
;
2208 t2
= type2
->t
& VT_TYPE
;
2210 /* strip qualifiers before comparing */
2211 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2212 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2214 /* Default Vs explicit signedness only matters for char */
2215 if ((t1
& VT_BTYPE
) != VT_BYTE
) {
2219 /* XXX: bitfields ? */
2222 /* test more complicated cases */
2223 bt1
= t1
& VT_BTYPE
;
2224 if (bt1
== VT_PTR
) {
2225 type1
= pointed_type(type1
);
2226 type2
= pointed_type(type2
);
2227 return is_compatible_types(type1
, type2
);
2228 } else if (bt1
== VT_STRUCT
) {
2229 return (type1
->ref
== type2
->ref
);
2230 } else if (bt1
== VT_FUNC
) {
2231 return is_compatible_func(type1
, type2
);
2237 /* return true if type1 and type2 are exactly the same (including
2240 static int is_compatible_types(CType
*type1
, CType
*type2
)
2242 return compare_types(type1
,type2
,0);
2245 /* return true if type1 and type2 are the same (ignoring qualifiers).
2247 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
2249 return compare_types(type1
,type2
,1);
2252 /* print a type. If 'varstr' is not NULL, then the variable is also
2253 printed in the type */
2255 /* XXX: add array and function pointers */
2256 static void type_to_str(char *buf
, int buf_size
,
2257 CType
*type
, const char *varstr
)
2264 t
= type
->t
& VT_TYPE
;
2267 if (t
& VT_CONSTANT
)
2268 pstrcat(buf
, buf_size
, "const ");
2269 if (t
& VT_VOLATILE
)
2270 pstrcat(buf
, buf_size
, "volatile ");
2271 if (t
& (VT_DEFSIGN
| VT_UNSIGNED
))
2272 pstrcat(buf
, buf_size
, "unsigned ");
2273 else if (t
& VT_DEFSIGN
)
2274 pstrcat(buf
, buf_size
, "signed ");
2304 tstr
= "long double";
2306 pstrcat(buf
, buf_size
, tstr
);
2310 if (bt
== VT_STRUCT
)
2314 pstrcat(buf
, buf_size
, tstr
);
2315 v
= type
->ref
->v
& ~SYM_STRUCT
;
2316 if (v
>= SYM_FIRST_ANOM
)
2317 pstrcat(buf
, buf_size
, "<anonymous>");
2319 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
2323 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
2324 pstrcat(buf
, buf_size
, "(");
2326 while (sa
!= NULL
) {
2327 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
2328 pstrcat(buf
, buf_size
, buf1
);
2331 pstrcat(buf
, buf_size
, ", ");
2333 pstrcat(buf
, buf_size
, ")");
2337 pstrcpy(buf1
, sizeof(buf1
), "*");
2339 pstrcat(buf1
, sizeof(buf1
), varstr
);
2340 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2344 pstrcat(buf
, buf_size
, " ");
2345 pstrcat(buf
, buf_size
, varstr
);
2350 /* verify type compatibility to store vtop in 'dt' type, and generate
2352 static void gen_assign_cast(CType
*dt
)
2354 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
2355 char buf1
[256], buf2
[256];
2358 st
= &vtop
->type
; /* source type */
2359 dbt
= dt
->t
& VT_BTYPE
;
2360 sbt
= st
->t
& VT_BTYPE
;
2361 if (sbt
== VT_VOID
|| dbt
== VT_VOID
)
2362 tcc_error("cannot cast from/to void");
2363 if (dt
->t
& VT_CONSTANT
)
2364 tcc_warning("assignment of read-only location");
2367 /* special cases for pointers */
2368 /* '0' can also be a pointer */
2369 if (is_null_pointer(vtop
))
2371 /* accept implicit pointer to integer cast with warning */
2372 if (is_integer_btype(sbt
)) {
2373 tcc_warning("assignment makes pointer from integer without a cast");
2376 type1
= pointed_type(dt
);
2377 /* a function is implicitely a function pointer */
2378 if (sbt
== VT_FUNC
) {
2379 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
2380 !is_compatible_types(pointed_type(dt
), st
))
2381 tcc_warning("assignment from incompatible pointer type");
2386 type2
= pointed_type(st
);
2387 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2388 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
2389 /* void * can match anything */
2391 /* exact type match, except for unsigned */
2394 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
|
2396 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
|
2398 if (!is_compatible_types(&tmp_type1
, &tmp_type2
))
2399 tcc_warning("assignment from incompatible pointer type");
2401 /* check const and volatile */
2402 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
2403 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
2404 tcc_warning("assignment discards qualifiers from pointer target type");
2410 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
2411 tcc_warning("assignment makes integer from pointer without a cast");
2413 /* XXX: more tests */
2418 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2419 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2420 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2422 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
2423 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
2424 tcc_error("cannot cast '%s' to '%s'", buf1
, buf2
);
2432 /* store vtop in lvalue pushed on stack */
2433 ST_FUNC
void vstore(void)
2435 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
2437 ft
= vtop
[-1].type
.t
;
2438 sbt
= vtop
->type
.t
& VT_BTYPE
;
2439 dbt
= ft
& VT_BTYPE
;
2440 if ((((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2441 (sbt
== VT_INT
&& dbt
== VT_SHORT
))
2442 && !(vtop
->type
.t
& VT_BITFIELD
)) {
2443 /* optimize char/short casts */
2444 delayed_cast
= VT_MUSTCAST
;
2445 vtop
->type
.t
= ft
& (VT_TYPE
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
)));
2446 /* XXX: factorize */
2447 if (ft
& VT_CONSTANT
)
2448 tcc_warning("assignment of read-only location");
2451 if (!(ft
& VT_BITFIELD
))
2452 gen_assign_cast(&vtop
[-1].type
);
2455 if (sbt
== VT_STRUCT
) {
2456 /* if structure, only generate pointer */
2457 /* structure assignment : generate memcpy */
2458 /* XXX: optimize if small size */
2459 if (!nocode_wanted
) {
2460 size
= type_size(&vtop
->type
, &align
);
2464 vtop
->type
.t
= VT_PTR
;
2467 /* address of memcpy() */
2470 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2471 else if(!(align
& 3))
2472 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2475 vpush_global_sym(&func_old_type
, TOK_memcpy
);
2480 vtop
->type
.t
= VT_PTR
;
2489 /* leave source on stack */
2490 } else if (ft
& VT_BITFIELD
) {
2491 /* bitfield store handling */
2492 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2493 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2494 /* remove bit field info to avoid loops */
2495 vtop
[-1].type
.t
= ft
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
2497 /* duplicate source into other register */
2502 if((ft
& VT_BTYPE
) == VT_BOOL
) {
2503 gen_cast(&vtop
[-1].type
);
2504 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2507 /* duplicate destination */
2509 vtop
[-1] = vtop
[-2];
2511 /* mask and shift source */
2512 if((ft
& VT_BTYPE
) != VT_BOOL
) {
2513 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2514 vpushll((1ULL << bit_size
) - 1ULL);
2516 vpushi((1 << bit_size
) - 1);
2522 /* load destination, mask and or with source */
2524 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2525 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2527 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2534 /* pop off shifted source from "duplicate source..." above */
2538 #ifdef CONFIG_TCC_BCHECK
2539 /* bound check case */
2540 if (vtop
[-1].r
& VT_MUSTBOUND
) {
2546 if (!nocode_wanted
) {
2550 #ifdef TCC_TARGET_X86_64
2551 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
2553 } else if ((ft
& VT_BTYPE
) == VT_QFLOAT
) {
2558 r
= gv(rc
); /* generate value */
2559 /* if lvalue was saved on stack, must read it */
2560 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
2562 t
= get_reg(RC_INT
);
2563 #ifdef TCC_TARGET_X86_64
2568 sv
.r
= VT_LOCAL
| VT_LVAL
;
2569 sv
.c
.ul
= vtop
[-1].c
.ul
;
2571 vtop
[-1].r
= t
| VT_LVAL
;
2573 /* two word case handling : store second register at word + 4 (or +8 for x86-64) */
2574 #ifdef TCC_TARGET_X86_64
2575 if (((ft
& VT_BTYPE
) == VT_QLONG
) || ((ft
& VT_BTYPE
) == VT_QFLOAT
)) {
2576 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
2578 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
2579 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
2581 vtop
[-1].type
.t
= load_type
;
2584 /* convert to int to increment easily */
2585 vtop
->type
.t
= addr_type
;
2591 vtop
[-1].type
.t
= load_type
;
2592 /* XXX: it works because r2 is spilled last ! */
2593 store(vtop
->r2
, vtop
- 1);
2599 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
2600 vtop
->r
|= delayed_cast
;
2604 /* post defines POST/PRE add. c is the token ++ or -- */
2605 ST_FUNC
void inc(int post
, int c
)
2608 vdup(); /* save lvalue */
2610 gv_dup(); /* duplicate value */
2615 vpushi(c
- TOK_MID
);
2617 vstore(); /* store value */
2619 vpop(); /* if post op, return saved value */
2622 /* Parse GNUC __attribute__ extension. Currently, the following
2623 extensions are recognized:
2624 - aligned(n) : set data/function alignment.
2625 - packed : force data alignment to 1
2626 - section(x) : generate data/code in this section.
2627 - unused : currently ignored, but may be used someday.
2628 - regparm(n) : pass function parameters in registers (i386 only)
2630 static void parse_attribute(AttributeDef
*ad
)
2634 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
2638 while (tok
!= ')') {
2639 if (tok
< TOK_IDENT
)
2640 expect("attribute name");
2648 expect("section name");
2649 ad
->section
= find_section(tcc_state
, (char *)tokc
.cstr
->data
);
2657 expect("alias(\"target\")");
2658 ad
->alias_target
= /* save string as token, for later */
2659 tok_alloc((char*)tokc
.cstr
->data
, tokc
.cstr
->size
-1)->tok
;
2668 if (n
<= 0 || (n
& (n
- 1)) != 0)
2669 tcc_error("alignment must be a positive power of two");
2686 /* currently, no need to handle it because tcc does not
2687 track unused objects */
2691 /* currently, no need to handle it because tcc does not
2692 track unused objects */
2697 ad
->a
.func_call
= FUNC_CDECL
;
2702 ad
->a
.func_call
= FUNC_STDCALL
;
2704 #ifdef TCC_TARGET_I386
2714 ad
->a
.func_call
= FUNC_FASTCALL1
+ n
- 1;
2720 ad
->a
.func_call
= FUNC_FASTCALLW
;
2727 ad
->a
.mode
= VT_LLONG
+ 1;
2730 ad
->a
.mode
= VT_SHORT
+ 1;
2733 ad
->a
.mode
= VT_INT
+ 1;
2736 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
2743 ad
->a
.func_export
= 1;
2746 ad
->a
.func_import
= 1;
2749 if (tcc_state
->warn_unsupported
)
2750 tcc_warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
2751 /* skip parameters */
2753 int parenthesis
= 0;
2757 else if (tok
== ')')
2760 } while (parenthesis
&& tok
!= -1);
2773 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2774 static void struct_decl(CType
*type
, int u
, int tdef
)
2776 int a
, v
, size
, align
, maxalign
, c
, offset
, flexible
;
2777 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
, prevbt
;
2778 Sym
*s
, *ss
, *ass
, **ps
;
2782 a
= tok
; /* save decl type */
2787 /* struct already defined ? return it */
2789 expect("struct/union/enum name");
2793 tcc_error("invalid type");
2795 } else if (tok
>= TOK_IDENT
&& !tdef
)
2796 tcc_error("unknown struct/union/enum");
2802 /* we put an undefined size for struct/union */
2803 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
2804 s
->r
= 0; /* default alignment is zero as gcc */
2805 /* put struct/union/enum name in type */
2813 tcc_error("struct/union/enum already defined");
2814 /* cannot be empty */
2816 /* non empty enums are not allowed */
2817 if (a
== TOK_ENUM
) {
2821 expect("identifier");
2824 tcc_error("redefinition of enumerator '%s'",
2825 get_tok_str(v
, NULL
));
2831 /* enum symbols have static storage */
2832 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
2833 ss
->type
.t
|= VT_STATIC
;
2838 /* NOTE: we accept a trailing comma */
2842 s
->c
= type_size(&int_type
, &align
);
2851 while (tok
!= '}') {
2852 parse_btype(&btype
, &ad
);
2855 tcc_error("flexible array member '%s' not at the end of struct",
2856 get_tok_str(v
, NULL
));
2861 type_decl(&type1
, &ad
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
2862 if (v
== 0 && (type1
.t
& VT_BTYPE
) != VT_STRUCT
)
2863 expect("identifier");
2864 if (type_size(&type1
, &align
) < 0) {
2865 if ((a
== TOK_STRUCT
) && (type1
.t
& VT_ARRAY
) && c
)
2868 tcc_error("field '%s' has incomplete type",
2869 get_tok_str(v
, NULL
));
2871 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
2872 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
2873 tcc_error("invalid type for '%s'",
2874 get_tok_str(v
, NULL
));
2878 bit_size
= expr_const();
2879 /* XXX: handle v = 0 case for messages */
2881 tcc_error("negative width in bit-field '%s'",
2882 get_tok_str(v
, NULL
));
2883 if (v
&& bit_size
== 0)
2884 tcc_error("zero width for bit-field '%s'",
2885 get_tok_str(v
, NULL
));
2887 size
= type_size(&type1
, &align
);
2889 if (align
< ad
.a
.aligned
)
2890 align
= ad
.a
.aligned
;
2891 } else if (ad
.a
.packed
) {
2893 } else if (*tcc_state
->pack_stack_ptr
) {
2894 if (align
> *tcc_state
->pack_stack_ptr
)
2895 align
= *tcc_state
->pack_stack_ptr
;
2898 if (bit_size
>= 0) {
2899 bt
= type1
.t
& VT_BTYPE
;
2906 tcc_error("bitfields must have scalar type");
2908 if (bit_size
> bsize
) {
2909 tcc_error("width of '%s' exceeds its type",
2910 get_tok_str(v
, NULL
));
2911 } else if (bit_size
== bsize
) {
2912 /* no need for bit fields */
2914 } else if (bit_size
== 0) {
2915 /* XXX: what to do if only padding in a
2917 /* zero size: means to pad */
2920 /* we do not have enough room ?
2921 did the type change?
2923 if ((bit_pos
+ bit_size
) > bsize
||
2924 bt
!= prevbt
|| a
== TOK_UNION
)
2927 /* XXX: handle LSB first */
2928 type1
.t
|= VT_BITFIELD
|
2929 (bit_pos
<< VT_STRUCT_SHIFT
) |
2930 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
2931 bit_pos
+= bit_size
;
2937 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2938 /* add new memory data only if starting
2940 if (lbit_pos
== 0) {
2941 if (a
== TOK_STRUCT
) {
2942 c
= (c
+ align
- 1) & -align
;
2951 if (align
> maxalign
)
2955 printf("add field %s offset=%d",
2956 get_tok_str(v
, NULL
), offset
);
2957 if (type1
.t
& VT_BITFIELD
) {
2958 printf(" pos=%d size=%d",
2959 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
2960 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
2965 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2967 while ((ass
= ass
->next
) != NULL
) {
2968 ss
= sym_push(ass
->v
, &ass
->type
, 0, offset
+ ass
->c
);
2973 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
2977 if (tok
== ';' || tok
== TOK_EOF
)
2984 /* store size and alignment */
2985 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
2991 /* return 0 if no type declaration. otherwise, return the basic type
2994 static int parse_btype(CType
*type
, AttributeDef
*ad
)
2996 int t
, u
, type_found
, typespec_found
, typedef_found
;
3000 memset(ad
, 0, sizeof(AttributeDef
));
3008 /* currently, we really ignore extension */
3018 if ((t
& VT_BTYPE
) != 0)
3019 tcc_error("too many basic types");
3035 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
3036 #ifndef TCC_TARGET_PE
3037 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3039 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
3040 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3054 if ((t
& VT_BTYPE
) == VT_LONG
) {
3055 #ifdef TCC_TARGET_PE
3056 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
3058 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3066 struct_decl(&type1
, VT_ENUM
, t
& VT_TYPEDEF
);
3069 type
->ref
= type1
.ref
;
3073 struct_decl(&type1
, VT_STRUCT
, t
& VT_TYPEDEF
);
3076 /* type modifiers */
3092 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == (VT_DEFSIGN
|VT_UNSIGNED
))
3093 tcc_error("signed and unsigned modifier");
3106 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == VT_DEFSIGN
)
3107 tcc_error("signed and unsigned modifier");
3108 t
|= VT_DEFSIGN
| VT_UNSIGNED
;
3133 /* GNUC attribute */
3134 case TOK_ATTRIBUTE1
:
3135 case TOK_ATTRIBUTE2
:
3136 parse_attribute(ad
);
3139 t
= (t
& ~VT_BTYPE
) | u
;
3147 parse_expr_type(&type1
);
3148 /* remove all storage modifiers except typedef */
3149 type1
.t
&= ~(VT_STORAGE
&~VT_TYPEDEF
);
3152 if (typespec_found
|| typedef_found
)
3155 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
3158 t
|= (s
->type
.t
& ~VT_TYPEDEF
);
3159 type
->ref
= s
->type
.ref
;
3161 /* get attributes from typedef */
3162 if (0 == ad
->a
.aligned
)
3163 ad
->a
.aligned
= s
->a
.aligned
;
3164 if (0 == ad
->a
.func_call
)
3165 ad
->a
.func_call
= s
->a
.func_call
;
3166 ad
->a
.packed
|= s
->a
.packed
;
3175 if (tcc_state
->char_is_unsigned
) {
3176 if ((t
& (VT_DEFSIGN
|VT_BTYPE
)) == VT_BYTE
)
3180 /* long is never used as type */
3181 if ((t
& VT_BTYPE
) == VT_LONG
)
3182 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
3183 t
= (t
& ~VT_BTYPE
) | VT_INT
;
3185 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3191 /* convert a function parameter type (array to pointer and function to
3192 function pointer) */
3193 static inline void convert_parameter_type(CType
*pt
)
3195 /* remove const and volatile qualifiers (XXX: const could be used
3196 to indicate a const function parameter */
3197 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3198 /* array must be transformed to pointer according to ANSI C */
3200 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
3205 ST_FUNC
void parse_asm_str(CString
*astr
)
3208 /* read the string */
3210 expect("string constant");
3212 while (tok
== TOK_STR
) {
3213 /* XXX: add \0 handling too ? */
3214 cstr_cat(astr
, tokc
.cstr
->data
);
3217 cstr_ccat(astr
, '\0');
3220 /* Parse an asm label and return the label
3221 * Don't forget to free the CString in the caller! */
3222 static void asm_label_instr(CString
*astr
)
3225 parse_asm_str(astr
);
3228 printf("asm_alias: \"%s\"\n", (char *)astr
->data
);
3232 static void post_type(CType
*type
, AttributeDef
*ad
)
3234 int n
, l
, t1
, arg_size
, align
;
3235 Sym
**plast
, *s
, *first
;
3240 /* function declaration */
3248 /* read param name and compute offset */
3249 if (l
!= FUNC_OLD
) {
3250 if (!parse_btype(&pt
, &ad1
)) {
3252 tcc_error("invalid type");
3259 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
3261 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3262 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
3263 tcc_error("parameter declared as void");
3264 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
3269 expect("identifier");
3273 convert_parameter_type(&pt
);
3274 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
3280 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
3287 /* if no parameters, then old type prototype */
3291 /* NOTE: const is ignored in returned type as it has a special
3292 meaning in gcc / C++ */
3293 type
->t
&= ~VT_CONSTANT
;
3294 /* some ancient pre-K&R C allows a function to return an array
3295 and the array brackets to be put after the arguments, such
3296 that "int c()[]" means something like "int[] c()" */
3299 skip(']'); /* only handle simple "[]" */
3302 /* we push a anonymous symbol which will contain the function prototype */
3303 ad
->a
.func_args
= arg_size
;
3304 s
= sym_push(SYM_FIELD
, type
, 0, l
);
3309 } else if (tok
== '[') {
3310 /* array definition */
3312 if (tok
== TOK_RESTRICT1
)
3317 if (!local_stack
|| nocode_wanted
)
3318 vpushi(expr_const());
3320 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3323 tcc_error("invalid array size");
3325 if (!is_integer_btype(vtop
->type
.t
& VT_BTYPE
))
3326 tcc_error("size of variable length array should be an integer");
3331 /* parse next post type */
3332 post_type(type
, ad
);
3333 if (type
->t
== VT_FUNC
)
3334 tcc_error("declaration of an array of functions");
3335 t1
|= type
->t
& VT_VLA
;
3338 loc
-= type_size(&int_type
, &align
);
3342 vla_runtime_type_size(type
, &align
);
3344 vset(&int_type
, VT_LOCAL
|VT_LVAL
, loc
);
3351 /* we push an anonymous symbol which will contain the array
3353 s
= sym_push(SYM_FIELD
, type
, 0, n
);
3354 type
->t
= (t1
? VT_VLA
: VT_ARRAY
) | VT_PTR
;
3359 /* Parse a type declaration (except basic type), and return the type
3360 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3361 expected. 'type' should contain the basic type. 'ad' is the
3362 attribute definition of the basic type. It can be modified by
3365 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
3368 CType type1
, *type2
;
3369 int qualifiers
, storage
;
3371 while (tok
== '*') {
3379 qualifiers
|= VT_CONSTANT
;
3384 qualifiers
|= VT_VOLATILE
;
3392 type
->t
|= qualifiers
;
3395 /* XXX: clarify attribute handling */
3396 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3397 parse_attribute(ad
);
3399 /* recursive type */
3400 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3401 type1
.t
= 0; /* XXX: same as int */
3404 /* XXX: this is not correct to modify 'ad' at this point, but
3405 the syntax is not clear */
3406 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3407 parse_attribute(ad
);
3408 type_decl(&type1
, ad
, v
, td
);
3411 /* type identifier */
3412 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
3416 if (!(td
& TYPE_ABSTRACT
))
3417 expect("identifier");
3421 storage
= type
->t
& VT_STORAGE
;
3422 type
->t
&= ~VT_STORAGE
;
3423 if (storage
& VT_STATIC
) {
3424 int saved_nocode_wanted
= nocode_wanted
;
3426 post_type(type
, ad
);
3427 nocode_wanted
= saved_nocode_wanted
;
3429 post_type(type
, ad
);
3431 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3432 parse_attribute(ad
);
3436 /* append type at the end of type1 */
3449 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3450 ST_FUNC
int lvalue_type(int t
)
3455 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
3457 else if (bt
== VT_SHORT
)
3461 if (t
& VT_UNSIGNED
)
3462 r
|= VT_LVAL_UNSIGNED
;
3466 /* indirection with full error checking and bound check */
3467 ST_FUNC
void indir(void)
3469 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
3470 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
3474 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
3476 vtop
->type
= *pointed_type(&vtop
->type
);
3477 /* Arrays and functions are never lvalues */
3478 if (!(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_VLA
)
3479 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3480 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3481 /* if bound checking, the referenced pointer must be checked */
3482 #ifdef CONFIG_TCC_BCHECK
3483 if (tcc_state
->do_bounds_check
)
3484 vtop
->r
|= VT_MUSTBOUND
;
3489 /* pass a parameter to a function and do type checking and casting */
3490 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
3495 func_type
= func
->c
;
3496 if (func_type
== FUNC_OLD
||
3497 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
3498 /* default casting : only need to convert float to double */
3499 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
3502 } else if (vtop
->type
.t
& VT_BITFIELD
) {
3503 type
.t
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
3506 } else if (arg
== NULL
) {
3507 tcc_error("too many arguments to function");
3510 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
3511 gen_assign_cast(&type
);
3515 /* parse an expression of the form '(type)' or '(expr)' and return its
3517 static void parse_expr_type(CType
*type
)
3523 if (parse_btype(type
, &ad
)) {
3524 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3531 static void parse_type(CType
*type
)
3536 if (!parse_btype(type
, &ad
)) {
3539 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3542 static void vpush_tokc(int t
)
3547 vsetc(&type
, VT_CONST
, &tokc
);
3550 ST_FUNC
void unary(void)
3552 int n
, t
, align
, size
, r
, sizeof_caller
;
3556 static int in_sizeof
= 0;
3558 sizeof_caller
= in_sizeof
;
3560 /* XXX: GCC 2.95.3 does not generate a table although it should be
3574 vpush_tokc(VT_INT
| VT_UNSIGNED
);
3578 vpush_tokc(VT_LLONG
);
3582 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
3586 vpush_tokc(VT_FLOAT
);
3590 vpush_tokc(VT_DOUBLE
);
3594 vpush_tokc(VT_LDOUBLE
);
3597 case TOK___FUNCTION__
:
3599 goto tok_identifier
;
3605 /* special function name identifier */
3606 len
= strlen(funcname
) + 1;
3607 /* generate char[len] type */
3612 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
3613 ptr
= section_ptr_add(data_section
, len
);
3614 memcpy(ptr
, funcname
, len
);
3619 #ifdef TCC_TARGET_PE
3620 t
= VT_SHORT
| VT_UNSIGNED
;
3626 /* string parsing */
3629 if (tcc_state
->warn_write_strings
)
3634 memset(&ad
, 0, sizeof(AttributeDef
));
3635 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, NULL
, 0);
3640 if (parse_btype(&type
, &ad
)) {
3641 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
3643 /* check ISOC99 compound literal */
3645 /* data is allocated locally by default */
3650 /* all except arrays are lvalues */
3651 if (!(type
.t
& VT_ARRAY
))
3652 r
|= lvalue_type(type
.t
);
3653 memset(&ad
, 0, sizeof(AttributeDef
));
3654 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, NULL
, 0);
3656 if (sizeof_caller
) {
3663 } else if (tok
== '{') {
3664 /* save all registers */
3666 /* statement expression : we do not accept break/continue
3667 inside as GCC does */
3668 block(NULL
, NULL
, NULL
, NULL
, 0, 1);
3683 /* functions names must be treated as function pointers,
3684 except for unary '&' and sizeof. Since we consider that
3685 functions are not lvalues, we only have to handle it
3686 there and in function calls. */
3687 /* arrays can also be used although they are not lvalues */
3688 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
3689 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
3691 mk_pointer(&vtop
->type
);
3697 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3699 boolean
.t
= VT_BOOL
;
3701 vtop
->c
.i
= !vtop
->c
.i
;
3702 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
3703 vtop
->c
.i
= vtop
->c
.i
^ 1;
3706 vseti(VT_JMP
, gvtst(1, 0));
3718 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
3719 tcc_error("pointer not accepted for unary plus");
3720 /* In order to force cast, we add zero, except for floating point
3721 where we really need an noop (otherwise -0.0 will be transformed
3723 if (!is_float(vtop
->type
.t
)) {
3734 unary_type(&type
); // Perform a in_sizeof = 0;
3735 size
= type_size(&type
, &align
);
3736 if (t
== TOK_SIZEOF
) {
3737 if (!(type
.t
& VT_VLA
)) {
3739 tcc_error("sizeof applied to an incomplete type");
3742 vla_runtime_type_size(&type
, &align
);
3747 vtop
->type
.t
|= VT_UNSIGNED
;
3750 case TOK_builtin_types_compatible_p
:
3759 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3760 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3761 vpushi(is_compatible_types(&type1
, &type2
));
3764 case TOK_builtin_constant_p
:
3766 int saved_nocode_wanted
, res
;
3769 saved_nocode_wanted
= nocode_wanted
;
3772 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
3774 nocode_wanted
= saved_nocode_wanted
;
3779 case TOK_builtin_frame_address
:
3785 if (tok
!= TOK_CINT
|| tokc
.i
< 0) {
3786 tcc_error("__builtin_frame_address only takes positive integers");
3793 vset(&type
, VT_LOCAL
, 0); /* local frame */
3795 mk_pointer(&vtop
->type
);
3796 indir(); /* -> parent frame */
3800 #ifdef TCC_TARGET_X86_64
3801 #ifdef TCC_TARGET_PE
3802 case TOK_builtin_va_start
:
3810 if ((vtop
->r
& VT_VALMASK
) != VT_LOCAL
)
3811 tcc_error("__builtin_va_start expects a local variable");
3812 vtop
->r
&= ~(VT_LVAL
| VT_REF
);
3813 vtop
->type
= char_pointer_type
;
3818 case TOK_builtin_va_arg_types
:
3825 vpushi(classify_x86_64_va_arg(&type
));
3840 t
= vtop
->type
.t
& VT_BTYPE
;
3842 /* In IEEE negate(x) isn't subtract(0,x), but rather
3847 else if (t
== VT_DOUBLE
)
3858 goto tok_identifier
;
3860 /* allow to take the address of a label */
3861 if (tok
< TOK_UIDENT
)
3862 expect("label identifier");
3863 s
= label_find(tok
);
3865 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
3867 if (s
->r
== LABEL_DECLARED
)
3868 s
->r
= LABEL_FORWARD
;
3871 s
->type
.t
= VT_VOID
;
3872 mk_pointer(&s
->type
);
3873 s
->type
.t
|= VT_STATIC
;
3875 vset(&s
->type
, VT_CONST
| VT_SYM
, 0);
3880 // special qnan , snan and infinity values
3882 vpush64(VT_DOUBLE
, 0x7ff8000000000000ULL
);
3886 vpush64(VT_DOUBLE
, 0x7ff0000000000001ULL
);
3890 vpush64(VT_DOUBLE
, 0x7ff0000000000000ULL
);
3899 expect("identifier");
3903 tcc_error("'%s' undeclared", get_tok_str(t
, NULL
));
3904 /* for simple function calls, we tolerate undeclared
3905 external reference to int() function */
3906 if (tcc_state
->warn_implicit_function_declaration
)
3907 tcc_warning("implicit declaration of function '%s'",
3908 get_tok_str(t
, NULL
));
3909 s
= external_global_sym(t
, &func_old_type
, 0);
3911 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
3912 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
3913 /* if referencing an inline function, then we generate a
3914 symbol to it if not already done. It will have the
3915 effect to generate code for it at the end of the
3916 compilation unit. Inline function as always
3917 generated in the text section. */
3919 put_extern_sym(s
, text_section
, 0, 0);
3920 r
= VT_SYM
| VT_CONST
;
3924 vset(&s
->type
, r
, s
->c
);
3925 /* if forward reference, we must point to s */
3926 if (vtop
->r
& VT_SYM
) {
3933 /* post operations */
3935 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
3938 } else if (tok
== '.' || tok
== TOK_ARROW
) {
3941 if (tok
== TOK_ARROW
)
3943 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
3947 /* expect pointer on structure */
3948 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
3949 expect("struct or union");
3953 while ((s
= s
->next
) != NULL
) {
3958 tcc_error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, NULL
));
3959 /* add field offset to pointer */
3960 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
3963 /* change type to field type, and set to lvalue */
3964 vtop
->type
= s
->type
;
3965 vtop
->type
.t
|= qualifiers
;
3966 /* an array is never an lvalue */
3967 if (!(vtop
->type
.t
& VT_ARRAY
)) {
3968 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3969 #ifdef CONFIG_TCC_BCHECK
3970 /* if bound checking, the referenced pointer must be checked */
3971 if (tcc_state
->do_bounds_check
)
3972 vtop
->r
|= VT_MUSTBOUND
;
3976 } else if (tok
== '[') {
3982 } else if (tok
== '(') {
3985 int nb_args
, ret_nregs
, ret_align
, variadic
;
3988 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3989 /* pointer test (no array accepted) */
3990 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
3991 vtop
->type
= *pointed_type(&vtop
->type
);
3992 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
3996 expect("function pointer");
3999 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
4001 /* get return type */
4004 sa
= s
->next
; /* first parameter */
4007 /* compute first implicit argument if a structure is returned */
4008 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
4009 variadic
= (s
->c
== FUNC_ELLIPSIS
);
4010 ret_nregs
= gfunc_sret(&s
->type
, variadic
, &ret
.type
,
4013 /* get some space for the returned structure */
4014 size
= type_size(&s
->type
, &align
);
4015 loc
= (loc
- size
) & -align
;
4017 ret
.r
= VT_LOCAL
| VT_LVAL
;
4018 /* pass it as 'int' to avoid structure arg passing
4020 vseti(VT_LOCAL
, loc
);
4030 /* return in register */
4031 if (is_float(ret
.type
.t
)) {
4032 ret
.r
= reg_fret(ret
.type
.t
);
4033 #ifdef TCC_TARGET_X86_64
4034 if ((ret
.type
.t
& VT_BTYPE
) == VT_QFLOAT
)
4038 #ifdef TCC_TARGET_X86_64
4039 if ((ret
.type
.t
& VT_BTYPE
) == VT_QLONG
)
4041 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
4051 gfunc_param_typed(s
, sa
);
4061 tcc_error("too few arguments to function");
4063 if (!nocode_wanted
) {
4064 gfunc_call(nb_args
);
4066 vtop
-= (nb_args
+ 1);
4070 for (r
= ret
.r
+ ret_nregs
+ !ret_nregs
; r
-- > ret
.r
;) {
4071 vsetc(&ret
.type
, r
, &ret
.c
);
4072 vtop
->r2
= ret
.r2
; /* Loop only happens when r2 is VT_CONST */
4075 /* handle packed struct return */
4076 if (((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) && ret_nregs
) {
4079 size
= type_size(&s
->type
, &align
);
4080 loc
= (loc
- size
) & -align
;
4084 vset(&ret
.type
, VT_LOCAL
| VT_LVAL
, addr
+ offset
);
4088 if (--ret_nregs
== 0)
4090 /* XXX: compatible with arm only: ret_align == register_size */
4091 offset
+= ret_align
;
4093 vset(&s
->type
, VT_LOCAL
| VT_LVAL
, addr
);
4101 ST_FUNC
void expr_prod(void)
4106 while (tok
== '*' || tok
== '/' || tok
== '%') {
4114 ST_FUNC
void expr_sum(void)
4119 while (tok
== '+' || tok
== '-') {
4127 static void expr_shift(void)
4132 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
4140 static void expr_cmp(void)
4145 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
4146 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
4154 static void expr_cmpeq(void)
4159 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
4167 static void expr_and(void)
4170 while (tok
== '&') {
4177 static void expr_xor(void)
4180 while (tok
== '^') {
4187 static void expr_or(void)
4190 while (tok
== '|') {
4197 /* XXX: fix this mess */
4198 static void expr_land_const(void)
4201 while (tok
== TOK_LAND
) {
4208 /* XXX: fix this mess */
4209 static void expr_lor_const(void)
4212 while (tok
== TOK_LOR
) {
4219 /* only used if non constant */
4220 static void expr_land(void)
4225 if (tok
== TOK_LAND
) {
4230 if (tok
!= TOK_LAND
) {
4240 static void expr_lor(void)
4245 if (tok
== TOK_LOR
) {
4250 if (tok
!= TOK_LOR
) {
4260 /* XXX: better constant handling */
4261 static void expr_cond(void)
4263 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
;
4265 CType type
, type1
, type2
;
4272 boolean
.t
= VT_BOOL
;
4278 if (tok
!= ':' || !gnu_ext
) {
4293 if (vtop
!= vstack
) {
4294 /* needed to avoid having different registers saved in
4296 if (is_float(vtop
->type
.t
)) {
4298 #ifdef TCC_TARGET_X86_64
4299 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4309 if (tok
== ':' && gnu_ext
) {
4317 sv
= *vtop
; /* save value to handle it later */
4318 vtop
--; /* no vpop so that FP stack is not flushed */
4326 bt1
= t1
& VT_BTYPE
;
4328 bt2
= t2
& VT_BTYPE
;
4329 /* cast operands to correct type according to ISOC rules */
4330 if (is_float(bt1
) || is_float(bt2
)) {
4331 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
4332 type
.t
= VT_LDOUBLE
;
4333 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
4338 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
4339 /* cast to biggest op */
4341 /* convert to unsigned if it does not fit in a long long */
4342 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
4343 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
4344 type
.t
|= VT_UNSIGNED
;
4345 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
4346 /* If one is a null ptr constant the result type
4348 if (is_null_pointer (vtop
))
4350 else if (is_null_pointer (&sv
))
4352 /* XXX: test pointer compatibility, C99 has more elaborate
4356 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
4357 /* XXX: test function pointer compatibility */
4358 type
= bt1
== VT_FUNC
? type1
: type2
;
4359 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
4360 /* XXX: test structure compatibility */
4361 type
= bt1
== VT_STRUCT
? type1
: type2
;
4362 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
4363 /* NOTE: as an extension, we accept void on only one side */
4366 /* integer operations */
4368 /* convert to unsigned if it does not fit in an integer */
4369 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
4370 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
4371 type
.t
|= VT_UNSIGNED
;
4374 /* now we convert second operand */
4376 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4379 if (is_float(type
.t
)) {
4381 #ifdef TCC_TARGET_X86_64
4382 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4386 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
4387 /* for long longs, we use fixed registers to avoid having
4388 to handle a complicated move */
4393 /* this is horrible, but we must also convert first
4397 /* put again first value and cast it */
4400 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4403 move_reg(r2
, r1
, type
.t
);
4410 static void expr_eq(void)
4416 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
4417 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
4418 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
4433 ST_FUNC
void gexpr(void)
4444 /* parse an expression and return its type without any side effect. */
4445 static void expr_type(CType
*type
)
4447 int saved_nocode_wanted
;
4449 saved_nocode_wanted
= nocode_wanted
;
4454 nocode_wanted
= saved_nocode_wanted
;
4457 /* parse a unary expression and return its type without any side
4459 static void unary_type(CType
*type
)
4471 /* parse a constant expression and return value in vtop. */
4472 static void expr_const1(void)
4481 /* parse an integer constant and return its value. */
4482 ST_FUNC
int expr_const(void)
4486 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
4487 expect("constant expression");
4493 /* return the label token if current token is a label, otherwise
4495 static int is_label(void)
4499 /* fast test first */
4500 if (tok
< TOK_UIDENT
)
4502 /* no need to save tokc because tok is an identifier */
4509 unget_tok(last_tok
);
4514 static void label_or_decl(int l
)
4518 /* fast test first */
4519 if (tok
>= TOK_UIDENT
)
4521 /* no need to save tokc because tok is an identifier */
4525 unget_tok(last_tok
);
4528 unget_tok(last_tok
);
4533 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
4534 int case_reg
, int is_expr
)
4537 Sym
*s
, *frame_bottom
;
4539 /* generate line number info */
4540 if (tcc_state
->do_debug
&&
4541 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
4542 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
4544 last_line_num
= file
->line_num
;
4548 /* default return value is (void) */
4550 vtop
->type
.t
= VT_VOID
;
4553 if (tok
== TOK_IF
) {
4560 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4562 if (c
== TOK_ELSE
) {
4566 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4567 gsym(d
); /* patch else jmp */
4570 } else if (tok
== TOK_WHILE
) {
4578 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4582 } else if (tok
== '{') {
4584 int block_vla_sp_loc
, *saved_vla_sp_loc
, saved_vla_flags
;
4587 /* record local declaration stack position */
4589 frame_bottom
= sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4590 frame_bottom
->next
= scope_stack_bottom
;
4591 scope_stack_bottom
= frame_bottom
;
4592 llabel
= local_label_stack
;
4594 /* save VLA state */
4595 block_vla_sp_loc
= *(saved_vla_sp_loc
= vla_sp_loc
);
4596 if (saved_vla_sp_loc
!= &vla_sp_root_loc
)
4597 vla_sp_loc
= &block_vla_sp_loc
;
4599 saved_vla_flags
= vla_flags
;
4600 vla_flags
|= VLA_NEED_NEW_FRAME
;
4602 /* handle local labels declarations */
4603 if (tok
== TOK_LABEL
) {
4606 if (tok
< TOK_UIDENT
)
4607 expect("label identifier");
4608 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
4618 while (tok
!= '}') {
4619 label_or_decl(VT_LOCAL
);
4623 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4626 /* pop locally defined labels */
4627 label_pop(&local_label_stack
, llabel
);
4629 /* XXX: this solution makes only valgrind happy...
4630 triggered by gcc.c-torture/execute/20000917-1.c */
4632 switch(vtop
->type
.t
& VT_BTYPE
) {
4637 for(p
=vtop
->type
.ref
;p
;p
=p
->prev
)
4639 tcc_error("unsupported expression type");
4642 /* pop locally defined symbols */
4643 scope_stack_bottom
= scope_stack_bottom
->next
;
4644 sym_pop(&local_stack
, s
);
4646 /* Pop VLA frames and restore stack pointer if required */
4647 if (saved_vla_sp_loc
!= &vla_sp_root_loc
)
4648 *saved_vla_sp_loc
= block_vla_sp_loc
;
4649 if (vla_sp_loc
!= (saved_vla_sp_loc
== &vla_sp_root_loc
? &vla_sp_root_loc
: &block_vla_sp_loc
)) {
4650 vla_sp_loc
= saved_vla_sp_loc
;
4651 gen_vla_sp_restore(*vla_sp_loc
);
4653 vla_flags
= (vla_flags
& ~VLA_SCOPE_FLAGS
) | (saved_vla_flags
& VLA_SCOPE_FLAGS
);
4656 } else if (tok
== TOK_RETURN
) {
4660 gen_assign_cast(&func_vt
);
4661 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
4662 CType type
, ret_type
;
4663 int ret_align
, ret_nregs
;
4664 ret_nregs
= gfunc_sret(&func_vt
, func_var
, &ret_type
,
4666 if (0 == ret_nregs
) {
4667 /* if returning structure, must copy it to implicit
4668 first pointer arg location */
4671 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
4674 /* copy structure value to pointer */
4677 /* returning structure packed into registers */
4678 int r
, size
, addr
, align
;
4679 size
= type_size(&func_vt
,&align
);
4680 if ((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) || (vtop
->c
.i
& (ret_align
-1)))
4681 && (align
& (ret_align
-1))) {
4682 loc
= (loc
- size
) & -align
;
4685 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
4688 vset(&ret_type
, VT_LOCAL
| VT_LVAL
, addr
);
4690 vtop
->type
= ret_type
;
4691 if (is_float(ret_type
.t
))
4692 r
= rc_fret(ret_type
.t
);
4698 if (--ret_nregs
== 0)
4700 /* We assume that when a structure is returned in multiple
4701 registers, their classes are consecutive values of the
4704 /* XXX: compatible with arm only: ret_align == register_size */
4705 vtop
->c
.i
+= ret_align
;
4706 vtop
->r
= VT_LOCAL
| VT_LVAL
;
4709 } else if (is_float(func_vt
.t
)) {
4710 gv(rc_fret(func_vt
.t
));
4714 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
4717 rsym
= gjmp(rsym
); /* jmp */
4718 } else if (tok
== TOK_BREAK
) {
4721 tcc_error("cannot break");
4722 *bsym
= gjmp(*bsym
);
4725 } else if (tok
== TOK_CONTINUE
) {
4728 tcc_error("cannot continue");
4729 *csym
= gjmp(*csym
);
4732 } else if (tok
== TOK_FOR
) {
4737 frame_bottom
= sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4738 frame_bottom
->next
= scope_stack_bottom
;
4739 scope_stack_bottom
= frame_bottom
;
4741 /* c99 for-loop init decl? */
4742 if (!decl0(VT_LOCAL
, 1)) {
4743 /* no, regular for-loop init expr */
4767 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4771 scope_stack_bottom
= scope_stack_bottom
->next
;
4772 sym_pop(&local_stack
, s
);
4774 if (tok
== TOK_DO
) {
4779 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4790 if (tok
== TOK_SWITCH
) {
4794 /* XXX: other types than integer */
4795 case_reg
= gv(RC_INT
);
4799 b
= gjmp(0); /* jump to first case */
4801 block(&a
, csym
, &b
, &c
, case_reg
, 0);
4802 /* if no default, jmp after switch */
4810 if (tok
== TOK_CASE
) {
4817 if (gnu_ext
&& tok
== TOK_DOTS
) {
4821 tcc_warning("empty case range");
4823 /* since a case is like a label, we must skip it with a jmp */
4830 *case_sym
= gtst(1, 0);
4833 *case_sym
= gtst(1, 0);
4837 *case_sym
= gtst(1, *case_sym
);
4842 goto block_after_label
;
4844 if (tok
== TOK_DEFAULT
) {
4850 tcc_error("too many 'default'");
4853 goto block_after_label
;
4855 if (tok
== TOK_GOTO
) {
4857 if (tok
== '*' && gnu_ext
) {
4861 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
4864 } else if (tok
>= TOK_UIDENT
) {
4865 s
= label_find(tok
);
4866 /* put forward definition if needed */
4868 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4870 if (s
->r
== LABEL_DECLARED
)
4871 s
->r
= LABEL_FORWARD
;
4873 /* label already defined */
4874 if (vla_flags
& VLA_IN_SCOPE
) {
4875 /* If VLAs are in use, save the current stack pointer and
4876 reset the stack pointer to what it was at function entry
4877 (label will restore stack pointer in inner scopes) */
4879 gen_vla_sp_restore(vla_sp_root_loc
);
4881 if (s
->r
& LABEL_FORWARD
)
4882 s
->jnext
= gjmp(s
->jnext
);
4884 gjmp_addr(s
->jnext
);
4887 expect("label identifier");
4890 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
4896 if (vla_flags
& VLA_IN_SCOPE
) {
4897 /* save/restore stack pointer across label
4898 this is a no-op when combined with the load immediately
4899 after the label unless we arrive via goto */
4904 if (s
->r
== LABEL_DEFINED
)
4905 tcc_error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
4907 s
->r
= LABEL_DEFINED
;
4909 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
4912 if (vla_flags
& VLA_IN_SCOPE
) {
4913 gen_vla_sp_restore(*vla_sp_loc
);
4914 vla_flags
|= VLA_NEED_NEW_FRAME
;
4916 /* we accept this, but it is a mistake */
4919 tcc_warning("deprecated use of label at end of compound statement");
4923 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4926 /* expression case */
4941 /* t is the array or struct type. c is the array or struct
4942 address. cur_index/cur_field is the pointer to the current
4943 value. 'size_only' is true if only size info is needed (only used
4945 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
4946 int *cur_index
, Sym
**cur_field
,
4950 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
4956 if (gnu_ext
&& (l
= is_label()) != 0)
4958 while (tok
== '[' || tok
== '.') {
4960 if (!(type
->t
& VT_ARRAY
))
4961 expect("array type");
4964 index
= expr_const();
4965 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
4966 expect("invalid index");
4967 if (tok
== TOK_DOTS
&& gnu_ext
) {
4969 index_last
= expr_const();
4970 if (index_last
< 0 ||
4971 (s
->c
>= 0 && index_last
>= s
->c
) ||
4973 expect("invalid index");
4979 *cur_index
= index_last
;
4980 type
= pointed_type(type
);
4981 elem_size
= type_size(type
, &align
);
4982 c
+= index
* elem_size
;
4983 /* NOTE: we only support ranges for last designator */
4984 nb_elems
= index_last
- index
+ 1;
4985 if (nb_elems
!= 1) {
4994 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
4995 expect("struct/union type");
5008 /* XXX: fix this mess by using explicit storage field */
5010 type1
.t
|= (type
->t
& ~VT_TYPE
);
5024 if (type
->t
& VT_ARRAY
) {
5026 type
= pointed_type(type
);
5027 c
+= index
* type_size(type
, &align
);
5031 tcc_error("too many field init");
5032 /* XXX: fix this mess by using explicit storage field */
5034 type1
.t
|= (type
->t
& ~VT_TYPE
);
5039 decl_initializer(type
, sec
, c
, 0, size_only
);
5041 /* XXX: make it more general */
5042 if (!size_only
&& nb_elems
> 1) {
5043 unsigned long c_end
;
5048 tcc_error("range init not supported yet for dynamic storage");
5049 c_end
= c
+ nb_elems
* elem_size
;
5050 if (c_end
> sec
->data_allocated
)
5051 section_realloc(sec
, c_end
);
5052 src
= sec
->data
+ c
;
5054 for(i
= 1; i
< nb_elems
; i
++) {
5056 memcpy(dst
, src
, elem_size
);
5062 #define EXPR_CONST 1
5065 /* store a value or an expression directly in global data or in local array */
5066 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
5067 int v
, int expr_type
)
5069 int saved_global_expr
, bt
, bit_pos
, bit_size
;
5071 unsigned long long bit_mask
;
5079 /* compound literals must be allocated globally in this case */
5080 saved_global_expr
= global_expr
;
5083 global_expr
= saved_global_expr
;
5084 /* NOTE: symbols are accepted */
5085 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
5086 tcc_error("initializer element is not constant");
5094 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
5097 /* XXX: not portable */
5098 /* XXX: generate error if incorrect relocation */
5099 gen_assign_cast(&dtype
);
5100 bt
= type
->t
& VT_BTYPE
;
5101 /* we'll write at most 12 bytes */
5102 if (c
+ 12 > sec
->data_allocated
) {
5103 section_realloc(sec
, c
+ 12);
5105 ptr
= sec
->data
+ c
;
5106 /* XXX: make code faster ? */
5107 if (!(type
->t
& VT_BITFIELD
)) {
5112 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5113 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
5114 bit_mask
= (1LL << bit_size
) - 1;
5116 if ((vtop
->r
& VT_SYM
) &&
5122 (bt
== VT_INT
&& bit_size
!= 32)))
5123 tcc_error("initializer element is not computable at load time");
5126 vtop
->c
.i
= (vtop
->c
.i
!= 0);
5128 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5131 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5134 *(double *)ptr
= vtop
->c
.d
;
5137 *(long double *)ptr
= vtop
->c
.ld
;
5140 *(long long *)ptr
|= (vtop
->c
.ll
& bit_mask
) << bit_pos
;
5143 if (vtop
->r
& VT_SYM
) {
5144 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
5146 *(addr_t
*)ptr
|= (vtop
->c
.ull
& bit_mask
) << bit_pos
;
5149 if (vtop
->r
& VT_SYM
) {
5150 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
5152 *(int *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5157 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
5164 /* put zeros for variable based init */
5165 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
5168 /* nothing to do because globals are already set to zero */
5170 vpush_global_sym(&func_old_type
, TOK_memset
);
5172 #ifdef TCC_TARGET_ARM
5183 /* 't' contains the type and storage info. 'c' is the offset of the
5184 object in section 'sec'. If 'sec' is NULL, it means stack based
5185 allocation. 'first' is true if array '{' must be read (multi
5186 dimension implicit array init handling). 'size_only' is true if
5187 size only evaluation is wanted (only for arrays). */
5188 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
5189 int first
, int size_only
)
5191 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, parlevel1
, i
;
5192 int size1
, align1
, expr_type
;
5196 if (type
->t
& VT_VLA
) {
5199 /* save current stack pointer */
5200 if (vla_flags
& VLA_NEED_NEW_FRAME
) {
5202 vla_flags
= VLA_IN_SCOPE
;
5203 vla_sp_loc
= &vla_sp_loc_tmp
;
5206 vla_runtime_type_size(type
, &a
);
5207 gen_vla_alloc(type
, a
);
5208 vset(type
, VT_LOCAL
|VT_LVAL
, c
);
5212 } else if (type
->t
& VT_ARRAY
) {
5216 t1
= pointed_type(type
);
5217 size1
= type_size(t1
, &align1
);
5220 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
5223 tcc_error("character array initializer must be a literal,"
5224 " optionally enclosed in braces");
5229 /* only parse strings here if correct type (otherwise: handle
5230 them as ((w)char *) expressions */
5231 if ((tok
== TOK_LSTR
&&
5232 #ifdef TCC_TARGET_PE
5233 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
5235 (t1
->t
& VT_BTYPE
) == VT_INT
5237 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
5238 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5243 /* compute maximum number of chars wanted */
5245 cstr_len
= cstr
->size
;
5247 cstr_len
= cstr
->size
/ sizeof(nwchar_t
);
5250 if (n
>= 0 && nb
> (n
- array_length
))
5251 nb
= n
- array_length
;
5254 tcc_warning("initializer-string for array is too long");
5255 /* in order to go faster for common case (char
5256 string in global variable, we handle it
5258 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
5259 memcpy(sec
->data
+ c
+ array_length
, cstr
->data
, nb
);
5263 ch
= ((unsigned char *)cstr
->data
)[i
];
5265 ch
= ((nwchar_t
*)cstr
->data
)[i
];
5266 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
5274 /* only add trailing zero if enough storage (no
5275 warning in this case since it is standard) */
5276 if (n
< 0 || array_length
< n
) {
5278 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
5284 while (tok
!= '}') {
5285 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
5286 if (n
>= 0 && index
>= n
)
5287 tcc_error("index too large");
5288 /* must put zero in holes (note that doing it that way
5289 ensures that it even works with designators) */
5290 if (!size_only
&& array_length
< index
) {
5291 init_putz(t1
, sec
, c
+ array_length
* size1
,
5292 (index
- array_length
) * size1
);
5295 if (index
> array_length
)
5296 array_length
= index
;
5297 /* special test for multi dimensional arrays (may not
5298 be strictly correct if designators are used at the
5300 if (index
>= n
&& no_oblock
)
5309 /* put zeros at the end */
5310 if (!size_only
&& n
>= 0 && array_length
< n
) {
5311 init_putz(t1
, sec
, c
+ array_length
* size1
,
5312 (n
- array_length
) * size1
);
5314 /* patch type size if needed */
5316 s
->c
= array_length
;
5317 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
5318 (sec
|| !first
|| tok
== '{')) {
5321 /* NOTE: the previous test is a specific case for automatic
5322 struct/union init */
5323 /* XXX: union needs only one init */
5325 /* XXX: this test is incorrect for local initializers
5326 beginning with ( without {. It would be much more difficult
5327 to do it correctly (ideally, the expression parser should
5328 be used in all cases) */
5334 while (tok
== '(') {
5338 if (!parse_btype(&type1
, &ad1
))
5340 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
5342 if (!is_assignable_types(type
, &type1
))
5343 tcc_error("invalid type for cast");
5348 if (first
|| tok
== '{') {
5357 while (tok
!= '}') {
5358 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
5360 if (!size_only
&& array_length
< index
) {
5361 init_putz(type
, sec
, c
+ array_length
,
5362 index
- array_length
);
5364 index
= index
+ type_size(&f
->type
, &align1
);
5365 if (index
> array_length
)
5366 array_length
= index
;
5368 /* gr: skip fields from same union - ugly. */
5370 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
5371 /* test for same offset */
5372 if (f
->next
->c
!= f
->c
)
5374 /* if yes, test for bitfield shift */
5375 if ((f
->type
.t
& VT_BITFIELD
) && (f
->next
->type
.t
& VT_BITFIELD
)) {
5376 int bit_pos_1
= (f
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5377 int bit_pos_2
= (f
->next
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5378 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
5379 if (bit_pos_1
!= bit_pos_2
)
5386 if (no_oblock
&& f
== NULL
)
5392 /* put zeros at the end */
5393 if (!size_only
&& array_length
< n
) {
5394 init_putz(type
, sec
, c
+ array_length
,
5403 } else if (tok
== '{') {
5405 decl_initializer(type
, sec
, c
, first
, size_only
);
5407 } else if (size_only
) {
5408 /* just skip expression */
5409 parlevel
= parlevel1
= 0;
5410 while ((parlevel
> 0 || parlevel1
> 0 ||
5411 (tok
!= '}' && tok
!= ',')) && tok
!= -1) {
5414 else if (tok
== ')')
5416 else if (tok
== '{')
5418 else if (tok
== '}')
5423 /* currently, we always use constant expression for globals
5424 (may change for scripting case) */
5425 expr_type
= EXPR_CONST
;
5427 expr_type
= EXPR_ANY
;
5428 init_putv(type
, sec
, c
, 0, expr_type
);
5432 /* parse an initializer for type 't' if 'has_init' is non zero, and
5433 allocate space in local or global data space ('r' is either
5434 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5435 variable 'v' with an associated name represented by 'asm_label' of
5436 scope 'scope' is declared before initializers are parsed. If 'v' is
5437 zero, then a reference to the new object is put in the value stack.
5438 If 'has_init' is 2, a special parsing is done to handle string
5440 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
5441 int has_init
, int v
, char *asm_label
,
5444 int size
, align
, addr
, data_offset
;
5446 ParseState saved_parse_state
= {0};
5447 TokenString init_str
;
5449 Sym
*flexible_array
;
5451 flexible_array
= NULL
;
5452 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
5453 Sym
*field
= type
->ref
->next
;
5456 field
= field
->next
;
5457 if (field
->type
.t
& VT_ARRAY
&& field
->type
.ref
->c
< 0)
5458 flexible_array
= field
;
5462 size
= type_size(type
, &align
);
5463 /* If unknown size, we must evaluate it before
5464 evaluating initializers because
5465 initializers can generate global data too
5466 (e.g. string pointers or ISOC99 compound
5467 literals). It also simplifies local
5468 initializers handling */
5469 tok_str_new(&init_str
);
5470 if (size
< 0 || (flexible_array
&& has_init
)) {
5472 tcc_error("unknown type size");
5473 /* get all init string */
5474 if (has_init
== 2) {
5475 /* only get strings */
5476 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5477 tok_str_add_tok(&init_str
);
5482 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
5484 tcc_error("unexpected end of file in initializer");
5485 tok_str_add_tok(&init_str
);
5488 else if (tok
== '}') {
5498 tok_str_add(&init_str
, -1);
5499 tok_str_add(&init_str
, 0);
5502 save_parse_state(&saved_parse_state
);
5504 macro_ptr
= init_str
.str
;
5506 decl_initializer(type
, NULL
, 0, 1, 1);
5507 /* prepare second initializer parsing */
5508 macro_ptr
= init_str
.str
;
5511 /* if still unknown size, error */
5512 size
= type_size(type
, &align
);
5514 tcc_error("unknown type size");
5517 size
+= flexible_array
->type
.ref
->c
* pointed_size(&flexible_array
->type
);
5518 /* take into account specified alignment if bigger */
5519 if (ad
->a
.aligned
) {
5520 if (ad
->a
.aligned
> align
)
5521 align
= ad
->a
.aligned
;
5522 } else if (ad
->a
.packed
) {
5525 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
5527 #ifdef CONFIG_TCC_BCHECK
5528 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5532 loc
= (loc
- size
) & -align
;
5534 #ifdef CONFIG_TCC_BCHECK
5535 /* handles bounds */
5536 /* XXX: currently, since we do only one pass, we cannot track
5537 '&' operators, so we add only arrays */
5538 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5539 unsigned long *bounds_ptr
;
5540 /* add padding between regions */
5542 /* then add local bound info */
5543 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(unsigned long));
5544 bounds_ptr
[0] = addr
;
5545 bounds_ptr
[1] = size
;
5549 /* local variable */
5550 sym_push(v
, type
, r
, addr
);
5552 /* push local reference */
5553 vset(type
, r
, addr
);
5559 if (v
&& scope
== VT_CONST
) {
5560 /* see if the symbol was already defined */
5563 if (!is_compatible_types(&sym
->type
, type
))
5564 tcc_error("incompatible types for redefinition of '%s'",
5565 get_tok_str(v
, NULL
));
5566 if (sym
->type
.t
& VT_EXTERN
) {
5567 /* if the variable is extern, it was not allocated */
5568 sym
->type
.t
&= ~VT_EXTERN
;
5569 /* set array size if it was ommited in extern
5571 if ((sym
->type
.t
& VT_ARRAY
) &&
5572 sym
->type
.ref
->c
< 0 &&
5574 sym
->type
.ref
->c
= type
->ref
->c
;
5576 /* we accept several definitions of the same
5577 global variable. this is tricky, because we
5578 must play with the SHN_COMMON type of the symbol */
5579 /* XXX: should check if the variable was already
5580 initialized. It is incorrect to initialized it
5582 /* no init data, we won't add more to the symbol */
5589 /* allocate symbol in corresponding section */
5594 else if (tcc_state
->nocommon
)
5598 data_offset
= sec
->data_offset
;
5599 data_offset
= (data_offset
+ align
- 1) & -align
;
5601 /* very important to increment global pointer at this time
5602 because initializers themselves can create new initializers */
5603 data_offset
+= size
;
5604 #ifdef CONFIG_TCC_BCHECK
5605 /* add padding if bound check */
5606 if (tcc_state
->do_bounds_check
)
5609 sec
->data_offset
= data_offset
;
5610 /* allocate section space to put the data */
5611 if (sec
->sh_type
!= SHT_NOBITS
&&
5612 data_offset
> sec
->data_allocated
)
5613 section_realloc(sec
, data_offset
);
5614 /* align section if needed */
5615 if (align
> sec
->sh_addralign
)
5616 sec
->sh_addralign
= align
;
5618 addr
= 0; /* avoid warning */
5622 if (scope
!= VT_CONST
|| !sym
) {
5623 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
5624 sym
->asm_label
= asm_label
;
5626 /* update symbol definition */
5628 put_extern_sym(sym
, sec
, addr
, size
);
5631 /* put a common area */
5632 put_extern_sym(sym
, NULL
, align
, size
);
5633 /* XXX: find a nicer way */
5634 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
5635 esym
->st_shndx
= SHN_COMMON
;
5638 /* push global reference */
5639 sym
= get_sym_ref(type
, sec
, addr
, size
);
5640 vpushsym(type
, sym
);
5642 /* patch symbol weakness */
5643 if (type
->t
& VT_WEAK
)
5645 #ifdef CONFIG_TCC_BCHECK
5646 /* handles bounds now because the symbol must be defined
5647 before for the relocation */
5648 if (tcc_state
->do_bounds_check
) {
5649 unsigned long *bounds_ptr
;
5651 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
5652 /* then add global bound info */
5653 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(long));
5654 bounds_ptr
[0] = 0; /* relocated */
5655 bounds_ptr
[1] = size
;
5659 if (has_init
|| (type
->t
& VT_VLA
)) {
5660 decl_initializer(type
, sec
, addr
, 1, 0);
5661 /* restore parse state if needed */
5663 tok_str_free(init_str
.str
);
5664 restore_parse_state(&saved_parse_state
);
5666 /* patch flexible array member size back to -1, */
5667 /* for possible subsequent similar declarations */
5669 flexible_array
->type
.ref
->c
= -1;
5674 static void put_func_debug(Sym
*sym
)
5679 /* XXX: we put here a dummy type */
5680 snprintf(buf
, sizeof(buf
), "%s:%c1",
5681 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
5682 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
5683 cur_text_section
, sym
->c
);
5684 /* //gr gdb wants a line at the function */
5685 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
5690 /* parse an old style function declaration list */
5691 /* XXX: check multiple parameter */
5692 static void func_decl_list(Sym
*func_sym
)
5699 /* parse each declaration */
5700 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
&&
5701 tok
!= TOK_ASM1
&& tok
!= TOK_ASM2
&& tok
!= TOK_ASM3
) {
5702 if (!parse_btype(&btype
, &ad
))
5703 expect("declaration list");
5704 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5705 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5707 /* we accept no variable after */
5711 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5712 /* find parameter in function parameter list */
5715 if ((s
->v
& ~SYM_FIELD
) == v
)
5719 tcc_error("declaration for parameter '%s' but no such parameter",
5720 get_tok_str(v
, NULL
));
5722 /* check that no storage specifier except 'register' was given */
5723 if (type
.t
& VT_STORAGE
)
5724 tcc_error("storage class specified for '%s'", get_tok_str(v
, NULL
));
5725 convert_parameter_type(&type
);
5726 /* we can add the type (NOTE: it could be local to the function) */
5728 /* accept other parameters */
5739 /* parse a function defined by symbol 'sym' and generate its code in
5740 'cur_text_section' */
5741 static void gen_function(Sym
*sym
)
5743 int saved_nocode_wanted
= nocode_wanted
;
5745 ind
= cur_text_section
->data_offset
;
5746 /* NOTE: we patch the symbol size later */
5747 put_extern_sym(sym
, cur_text_section
, ind
, 0);
5748 funcname
= get_tok_str(sym
->v
, NULL
);
5750 /* Initialize VLA state */
5751 vla_sp_loc
= &vla_sp_root_loc
;
5752 vla_flags
= VLA_NEED_NEW_FRAME
;
5753 /* put debug symbol */
5754 if (tcc_state
->do_debug
)
5755 put_func_debug(sym
);
5756 /* push a dummy symbol to enable local sym storage */
5757 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
5758 gfunc_prolog(&sym
->type
);
5760 block(NULL
, NULL
, NULL
, NULL
, 0, 0);
5763 cur_text_section
->data_offset
= ind
;
5764 label_pop(&global_label_stack
, NULL
);
5765 /* reset local stack */
5766 scope_stack_bottom
= NULL
;
5767 sym_pop(&local_stack
, NULL
);
5768 /* end of function */
5769 /* patch symbol size */
5770 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
5772 /* patch symbol weakness (this definition overrules any prototype) */
5773 if (sym
->type
.t
& VT_WEAK
)
5775 if (tcc_state
->do_debug
) {
5776 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
5778 /* It's better to crash than to generate wrong code */
5779 cur_text_section
= NULL
;
5780 funcname
= ""; /* for safety */
5781 func_vt
.t
= VT_VOID
; /* for safety */
5782 func_var
= 0; /* for safety */
5783 ind
= 0; /* for safety */
5784 nocode_wanted
= saved_nocode_wanted
;
5787 ST_FUNC
void gen_inline_functions(void)
5790 int *str
, inline_generated
, i
;
5791 struct InlineFunc
*fn
;
5793 /* iterate while inline function are referenced */
5795 inline_generated
= 0;
5796 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5797 fn
= tcc_state
->inline_fns
[i
];
5799 if (sym
&& sym
->c
) {
5800 /* the function was used: generate its code and
5801 convert it to a normal function */
5802 str
= fn
->token_str
;
5805 pstrcpy(file
->filename
, sizeof file
->filename
, fn
->filename
);
5806 sym
->r
= VT_SYM
| VT_CONST
;
5807 sym
->type
.t
&= ~VT_INLINE
;
5811 cur_text_section
= text_section
;
5813 macro_ptr
= NULL
; /* fail safe */
5815 inline_generated
= 1;
5818 if (!inline_generated
)
5821 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5822 fn
= tcc_state
->inline_fns
[i
];
5823 str
= fn
->token_str
;
5826 dynarray_reset(&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
);
5829 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
5830 static int decl0(int l
, int is_for_loop_init
)
5838 if (!parse_btype(&btype
, &ad
)) {
5839 if (is_for_loop_init
)
5841 /* skip redundant ';' */
5842 /* XXX: find more elegant solution */
5847 if (l
== VT_CONST
&&
5848 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5849 /* global asm block */
5853 /* special test for old K&R protos without explicit int
5854 type. Only accepted when defining global data */
5855 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
5859 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5860 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5862 /* we accept no variable after */
5866 while (1) { /* iterate thru each declaration */
5867 char *asm_label
; // associated asm label
5869 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5873 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
5874 printf("type = '%s'\n", buf
);
5877 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5878 if ((type
.t
& VT_STATIC
) && (l
== VT_LOCAL
)) {
5879 tcc_error("function without file scope cannot be static");
5881 /* if old style function prototype, we accept a
5884 if (sym
->c
== FUNC_OLD
)
5885 func_decl_list(sym
);
5889 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5892 asm_label_instr(&astr
);
5893 asm_label
= tcc_strdup(astr
.data
);
5896 /* parse one last attribute list, after asm label */
5897 parse_attribute(&ad
);
5902 #ifdef TCC_TARGET_PE
5903 if (ad
.a
.func_import
)
5904 type
.t
|= VT_IMPORT
;
5905 if (ad
.a
.func_export
)
5906 type
.t
|= VT_EXPORT
;
5910 tcc_error("cannot use local functions");
5911 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
5912 expect("function definition");
5914 /* reject abstract declarators in function definition */
5916 while ((sym
= sym
->next
) != NULL
)
5917 if (!(sym
->v
& ~SYM_FIELD
))
5918 expect("identifier");
5920 /* XXX: cannot do better now: convert extern line to static inline */
5921 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
5922 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5927 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
5930 ref
= sym
->type
.ref
;
5931 if (0 == ref
->a
.func_proto
)
5932 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
5934 /* use func_call from prototype if not defined */
5935 if (ref
->a
.func_call
!= FUNC_CDECL
5936 && type
.ref
->a
.func_call
== FUNC_CDECL
)
5937 type
.ref
->a
.func_call
= ref
->a
.func_call
;
5939 /* use export from prototype */
5940 if (ref
->a
.func_export
)
5941 type
.ref
->a
.func_export
= 1;
5943 /* use static from prototype */
5944 if (sym
->type
.t
& VT_STATIC
)
5945 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5947 if (!is_compatible_types(&sym
->type
, &type
)) {
5949 tcc_error("incompatible types for redefinition of '%s'",
5950 get_tok_str(v
, NULL
));
5952 type
.ref
->a
.func_proto
= 0;
5953 /* if symbol is already defined, then put complete type */
5956 /* put function symbol */
5957 sym
= global_identifier_push(v
, type
.t
, 0);
5958 sym
->type
.ref
= type
.ref
;
5961 /* static inline functions are just recorded as a kind
5962 of macro. Their code will be emitted at the end of
5963 the compilation unit only if they are used */
5964 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
5965 (VT_INLINE
| VT_STATIC
)) {
5966 TokenString func_str
;
5968 struct InlineFunc
*fn
;
5969 const char *filename
;
5971 tok_str_new(&func_str
);
5977 tcc_error("unexpected end of file");
5978 tok_str_add_tok(&func_str
);
5983 } else if (t
== '}') {
5985 if (block_level
== 0)
5989 tok_str_add(&func_str
, -1);
5990 tok_str_add(&func_str
, 0);
5991 filename
= file
? file
->filename
: "";
5992 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
5993 strcpy(fn
->filename
, filename
);
5995 fn
->token_str
= func_str
.str
;
5996 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
5999 /* compute text section */
6000 cur_text_section
= ad
.section
;
6001 if (!cur_text_section
)
6002 cur_text_section
= text_section
;
6003 sym
->r
= VT_SYM
| VT_CONST
;
6008 if (btype
.t
& VT_TYPEDEF
) {
6009 /* save typedefed type */
6010 /* XXX: test storage specifiers ? */
6011 sym
= sym_push(v
, &type
, 0, 0);
6013 sym
->type
.t
|= VT_TYPEDEF
;
6016 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
6017 /* external function definition */
6018 /* specific case for func_call attribute */
6019 ad
.a
.func_proto
= 1;
6021 } else if (!(type
.t
& VT_ARRAY
)) {
6022 /* not lvalue if array */
6023 r
|= lvalue_type(type
.t
);
6025 has_init
= (tok
== '=');
6026 if (has_init
&& (type
.t
& VT_VLA
))
6027 tcc_error("Variable length array cannot be initialized");
6028 if ((btype
.t
& VT_EXTERN
) || ((type
.t
& VT_BTYPE
) == VT_FUNC
) ||
6029 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
6030 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
6031 /* external variable or function */
6032 /* NOTE: as GCC, uninitialized global static
6033 arrays of null size are considered as
6035 sym
= external_sym(v
, &type
, r
, asm_label
);
6037 if (type
.t
& VT_WEAK
)
6040 if (ad
.alias_target
) {
6045 alias_target
= sym_find(ad
.alias_target
);
6046 if (!alias_target
|| !alias_target
->c
)
6047 tcc_error("unsupported forward __alias__ attribute");
6048 esym
= &((Elf32_Sym
*)symtab_section
->data
)[alias_target
->c
];
6049 tsec
.sh_num
= esym
->st_shndx
;
6050 put_extern_sym2(sym
, &tsec
, esym
->st_value
, esym
->st_size
, 0);
6053 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
6054 if (type
.t
& VT_STATIC
)
6060 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, asm_label
, l
);
6064 if (is_for_loop_init
)
6077 ST_FUNC
void decl(int l
)