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(addr_t v
)
348 vsetc(&size_type
, VT_CONST
, &cval
);
351 /* push arbitrary 64bit constant */
352 void vpush64(int ty
, unsigned long long v
)
359 vsetc(&ctype
, VT_CONST
, &cval
);
362 /* push long long constant */
363 static inline void vpushll(long long v
)
365 vpush64(VT_LLONG
, v
);
368 /* push a symbol value of TYPE */
369 static inline void vpushsym(CType
*type
, Sym
*sym
)
373 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
377 /* Return a static symbol pointing to a section */
378 ST_FUNC Sym
*get_sym_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
384 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
385 sym
->type
.ref
= type
->ref
;
386 sym
->r
= VT_CONST
| VT_SYM
;
387 put_extern_sym(sym
, sec
, offset
, size
);
391 /* push a reference to a section offset by adding a dummy symbol */
392 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
394 vpushsym(type
, get_sym_ref(type
, sec
, offset
, size
));
397 /* define a new external reference to a symbol 'v' of type 'u' */
398 ST_FUNC Sym
*external_global_sym(int v
, CType
*type
, int r
)
404 /* push forward reference */
405 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
406 s
->type
.ref
= type
->ref
;
407 s
->r
= r
| VT_CONST
| VT_SYM
;
412 /* define a new external reference to a symbol 'v' with alternate asm
413 name 'asm_label' of type 'u'. 'asm_label' is equal to NULL if there
414 is no alternate name (most cases) */
415 static Sym
*external_sym(int v
, CType
*type
, int r
, char *asm_label
)
421 /* push forward reference */
422 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
423 s
->asm_label
= asm_label
;
424 s
->type
.t
|= VT_EXTERN
;
425 } else if (s
->type
.ref
== func_old_type
.ref
) {
426 s
->type
.ref
= type
->ref
;
427 s
->r
= r
| VT_CONST
| VT_SYM
;
428 s
->type
.t
|= VT_EXTERN
;
429 } else if (!is_compatible_types(&s
->type
, type
)) {
430 tcc_error("incompatible types for redefinition of '%s'",
431 get_tok_str(v
, NULL
));
436 /* push a reference to global symbol v */
437 ST_FUNC
void vpush_global_sym(CType
*type
, int v
)
439 vpushsym(type
, external_global_sym(v
, type
, 0));
442 ST_FUNC
void vset(CType
*type
, int r
, int v
)
447 vsetc(type
, r
, &cval
);
450 static void vseti(int r
, int v
)
458 ST_FUNC
void vswap(void)
461 /* cannot let cpu flags if other instruction are generated. Also
462 avoid leaving VT_JMP anywhere except on the top of the stack
463 because it would complicate the code generator. */
464 if (vtop
>= vstack
) {
465 int v
= vtop
->r
& VT_VALMASK
;
466 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
473 /* XXX: +2% overall speed possible with optimized memswap
475 * memswap(&vtop[0], &vtop[1], sizeof *vtop);
479 ST_FUNC
void vpushv(SValue
*v
)
481 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
482 tcc_error("memory full (vstack)");
487 static void vdup(void)
492 /* save r to the memory stack, and mark it as being free */
493 ST_FUNC
void save_reg(int r
)
495 int l
, saved
, size
, align
;
499 /* modify all stack values */
502 for(p
=vstack
;p
<=vtop
;p
++) {
503 if ((p
->r
& VT_VALMASK
) == r
||
504 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
505 /* must save value on stack if not already done */
507 /* NOTE: must reload 'r' because r might be equal to r2 */
508 r
= p
->r
& VT_VALMASK
;
509 /* store register in the stack */
511 if ((p
->r
& VT_LVAL
) ||
512 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
513 #ifdef TCC_TARGET_X86_64
514 type
= &char_pointer_type
;
518 size
= type_size(type
, &align
);
519 loc
= (loc
- size
) & -align
;
521 sv
.r
= VT_LOCAL
| VT_LVAL
;
524 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
525 /* x86 specific: need to pop fp register ST0 if saved */
527 o(0xd8dd); /* fstp %st(0) */
530 #ifndef TCC_TARGET_X86_64
531 /* special long long case */
532 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
540 /* mark that stack entry as being saved on the stack */
541 if (p
->r
& VT_LVAL
) {
542 /* also clear the bounded flag because the
543 relocation address of the function was stored in
545 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
547 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
555 #ifdef TCC_TARGET_ARM
556 /* find a register of class 'rc2' with at most one reference on stack.
557 * If none, call get_reg(rc) */
558 ST_FUNC
int get_reg_ex(int rc
, int rc2
)
563 for(r
=0;r
<NB_REGS
;r
++) {
564 if (reg_classes
[r
] & rc2
) {
567 for(p
= vstack
; p
<= vtop
; p
++) {
568 if ((p
->r
& VT_VALMASK
) == r
||
569 (p
->r2
& VT_VALMASK
) == r
)
580 /* find a free register of class 'rc'. If none, save one register */
581 ST_FUNC
int get_reg(int rc
)
586 /* find a free register */
587 for(r
=0;r
<NB_REGS
;r
++) {
588 if (reg_classes
[r
] & rc
) {
589 for(p
=vstack
;p
<=vtop
;p
++) {
590 if ((p
->r
& VT_VALMASK
) == r
||
591 (p
->r2
& VT_VALMASK
) == r
)
599 /* no register left : free the first one on the stack (VERY
600 IMPORTANT to start from the bottom to ensure that we don't
601 spill registers used in gen_opi()) */
602 for(p
=vstack
;p
<=vtop
;p
++) {
603 /* look at second register (if long long) */
604 r
= p
->r2
& VT_VALMASK
;
605 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
607 r
= p
->r
& VT_VALMASK
;
608 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
614 /* Should never comes here */
618 /* save registers up to (vtop - n) stack entry */
619 ST_FUNC
void save_regs(int n
)
624 for(p
= vstack
;p
<= p1
; p
++) {
625 r
= p
->r
& VT_VALMASK
;
632 /* move register 's' (of type 't') to 'r', and flush previous value of r to memory
634 static void move_reg(int r
, int s
, int t
)
648 /* get address of vtop (vtop MUST BE an lvalue) */
649 static void gaddrof(void)
651 if (vtop
->r
& VT_REF
)
654 /* tricky: if saved lvalue, then we can go back to lvalue */
655 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
656 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
661 #ifdef CONFIG_TCC_BCHECK
662 /* generate lvalue bound code */
663 static void gbound(void)
668 vtop
->r
&= ~VT_MUSTBOUND
;
669 /* if lvalue, then use checking code before dereferencing */
670 if (vtop
->r
& VT_LVAL
) {
671 /* if not VT_BOUNDED value, then make one */
672 if (!(vtop
->r
& VT_BOUNDED
)) {
673 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
674 /* must save type because we must set it to int to get pointer */
676 vtop
->type
.t
= VT_INT
;
679 gen_bounded_ptr_add();
680 vtop
->r
|= lval_type
;
683 /* then check for dereferencing */
684 gen_bounded_ptr_deref();
689 /* store vtop a register belonging to class 'rc'. lvalues are
690 converted to values. Cannot be used if cannot be converted to
691 register value (such as structures). */
692 ST_FUNC
int gv(int rc
)
694 int r
, bit_pos
, bit_size
, size
, align
, i
;
697 /* NOTE: get_reg can modify vstack[] */
698 if (vtop
->type
.t
& VT_BITFIELD
) {
701 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
702 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
703 /* remove bit field info to avoid loops */
704 vtop
->type
.t
&= ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
705 /* cast to int to propagate signedness in following ops */
706 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
711 if((vtop
->type
.t
& VT_UNSIGNED
) ||
712 (vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
713 type
.t
|= VT_UNSIGNED
;
715 /* generate shifts */
716 vpushi(bits
- (bit_pos
+ bit_size
));
718 vpushi(bits
- bit_size
);
719 /* NOTE: transformed to SHR if unsigned */
723 if (is_float(vtop
->type
.t
) &&
724 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
727 unsigned long offset
;
728 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
732 /* XXX: unify with initializers handling ? */
733 /* CPUs usually cannot use float constants, so we store them
734 generically in data segment */
735 size
= type_size(&vtop
->type
, &align
);
736 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
737 data_section
->data_offset
= offset
;
738 /* XXX: not portable yet */
739 #if defined(__i386__) || defined(__x86_64__)
740 /* Zero pad x87 tenbyte long doubles */
741 if (size
== LDOUBLE_SIZE
) {
742 vtop
->c
.tab
[2] &= 0xffff;
743 #if LDOUBLE_SIZE == 16
748 ptr
= section_ptr_add(data_section
, size
);
750 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
754 ptr
[i
] = vtop
->c
.tab
[size
-1-i
];
758 ptr
[i
] = vtop
->c
.tab
[i
];
759 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
760 vtop
->r
|= VT_LVAL
| VT_SYM
;
762 vtop
->c
.ptr_offset
= 0;
764 #ifdef CONFIG_TCC_BCHECK
765 if (vtop
->r
& VT_MUSTBOUND
)
769 r
= vtop
->r
& VT_VALMASK
;
770 rc2
= (rc
& RC_FLOAT
) ? RC_FLOAT
: RC_INT
;
773 #ifdef TCC_TARGET_X86_64
774 else if (rc
== RC_FRET
)
778 /* need to reload if:
780 - lvalue (need to dereference pointer)
781 - already a register, but not in the right class */
783 || (vtop
->r
& VT_LVAL
)
784 || !(reg_classes
[r
] & rc
)
785 #ifdef TCC_TARGET_X86_64
786 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
787 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
&& !(reg_classes
[vtop
->r2
] & rc2
))
789 || ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
794 #ifdef TCC_TARGET_X86_64
795 if (((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
)) {
796 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
798 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
799 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
800 unsigned long long ll
;
802 int r2
, original_type
;
803 original_type
= vtop
->type
.t
;
804 /* two register type load : expand to two words
806 #ifndef TCC_TARGET_X86_64
807 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
810 vtop
->c
.ui
= ll
; /* first word */
812 vtop
->r
= r
; /* save register value */
813 vpushi(ll
>> 32); /* second word */
816 if (r
>= VT_CONST
|| /* XXX: test to VT_CONST incorrect ? */
817 (vtop
->r
& VT_LVAL
)) {
818 /* We do not want to modifier the long long
819 pointer here, so the safest (and less
820 efficient) is to save all the other registers
821 in the stack. XXX: totally inefficient. */
823 /* load from memory */
824 vtop
->type
.t
= load_type
;
827 vtop
[-1].r
= r
; /* save register value */
828 /* increment pointer to get second word */
829 vtop
->type
.t
= addr_type
;
834 vtop
->type
.t
= load_type
;
839 vtop
[-1].r
= r
; /* save register value */
840 vtop
->r
= vtop
[-1].r2
;
842 /* Allocate second register. Here we rely on the fact that
843 get_reg() tries first to free r2 of an SValue. */
847 /* write second register */
849 vtop
->type
.t
= original_type
;
850 } else if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
852 /* lvalue of scalar type : need to use lvalue type
853 because of possible cast */
856 /* compute memory access type */
857 if (vtop
->r
& VT_REF
)
858 #ifdef TCC_TARGET_X86_64
863 else if (vtop
->r
& VT_LVAL_BYTE
)
865 else if (vtop
->r
& VT_LVAL_SHORT
)
867 if (vtop
->r
& VT_LVAL_UNSIGNED
)
871 /* restore wanted type */
874 /* one register type load */
879 #ifdef TCC_TARGET_C67
880 /* uses register pairs for doubles */
881 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
888 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
889 ST_FUNC
void gv2(int rc1
, int rc2
)
893 /* generate more generic register first. But VT_JMP or VT_CMP
894 values must be generated first in all cases to avoid possible
896 v
= vtop
[0].r
& VT_VALMASK
;
897 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
902 /* test if reload is needed for first register */
903 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
913 /* test if reload is needed for first register */
914 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
920 /* wrapper around RC_FRET to return a register by type */
921 static int rc_fret(int t
)
923 #ifdef TCC_TARGET_X86_64
924 if (t
== VT_LDOUBLE
) {
931 /* wrapper around REG_FRET to return a register by type */
932 static int reg_fret(int t
)
934 #ifdef TCC_TARGET_X86_64
935 if (t
== VT_LDOUBLE
) {
942 /* expand long long on stack in two int registers */
943 static void lexpand(void)
947 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
950 vtop
[0].r
= vtop
[-1].r2
;
951 vtop
[0].r2
= VT_CONST
;
952 vtop
[-1].r2
= VT_CONST
;
953 vtop
[0].type
.t
= VT_INT
| u
;
954 vtop
[-1].type
.t
= VT_INT
| u
;
957 #ifdef TCC_TARGET_ARM
958 /* expand long long on stack */
959 ST_FUNC
void lexpand_nr(void)
963 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
966 vtop
->type
.t
= VT_INT
| u
;
967 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
969 vtop
[-1].c
.ui
= vtop
->c
.ull
;
970 vtop
->c
.ui
= vtop
->c
.ull
>> 32;
972 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
974 vtop
->r
= vtop
[-1].r
;
975 } else if (v
> VT_CONST
) {
979 vtop
->r
= vtop
[-1].r2
;
980 vtop
[-1].r2
= VT_CONST
;
981 vtop
[-1].type
.t
= VT_INT
| u
;
985 /* build a long long from two ints */
986 static void lbuild(int t
)
989 vtop
[-1].r2
= vtop
[0].r
;
994 /* rotate n first stack elements to the bottom
995 I1 ... In -> I2 ... In I1 [top is right]
997 ST_FUNC
void vrotb(int n
)
1003 for(i
=-n
+1;i
!=0;i
++)
1004 vtop
[i
] = vtop
[i
+1];
1008 /* rotate the n elements before entry e towards the top
1009 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
1011 ST_FUNC
void vrote(SValue
*e
, int n
)
1017 for(i
= 0;i
< n
- 1; i
++)
1022 /* rotate n first stack elements to the top
1023 I1 ... In -> In I1 ... I(n-1) [top is right]
1025 ST_FUNC
void vrott(int n
)
1030 /* pop stack value */
1031 ST_FUNC
void vpop(void)
1034 v
= vtop
->r
& VT_VALMASK
;
1035 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1036 /* for x86, we need to pop the FP stack */
1037 if (v
== TREG_ST0
&& !nocode_wanted
) {
1038 o(0xd8dd); /* fstp %st(0) */
1041 if (v
== VT_JMP
|| v
== VT_JMPI
) {
1042 /* need to put correct jump if && or || without test */
1048 /* convert stack entry to register and duplicate its value in another
1050 static void gv_dup(void)
1056 if ((t
& VT_BTYPE
) == VT_LLONG
) {
1063 /* stack: H L L1 H1 */
1071 /* duplicate value */
1076 #ifdef TCC_TARGET_X86_64
1077 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1087 load(r1
, &sv
); /* move r to r1 */
1089 /* duplicates value */
1095 /* Generate value test
1097 * Generate a test for any value (jump, comparison and integers) */
1098 int gvtst(int inv
, int t
)
1100 int v
= vtop
->r
& VT_VALMASK
;
1101 if (v
!= VT_CMP
&& v
!= VT_JMP
&& v
!= VT_JMPI
) {
1105 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1106 /* constant jmp optimization */
1107 if ((vtop
->c
.i
!= 0) != inv
)
1112 return gtst(inv
, t
);
1115 #ifndef TCC_TARGET_X86_64
1116 /* generate CPU independent (unsigned) long long operations */
1117 static void gen_opl(int op
)
1119 int t
, a
, b
, op1
, c
, i
;
1121 unsigned short reg_iret
= REG_IRET
;
1122 unsigned short reg_lret
= REG_LRET
;
1128 func
= TOK___divdi3
;
1131 func
= TOK___udivdi3
;
1134 func
= TOK___moddi3
;
1137 func
= TOK___umoddi3
;
1144 /* call generic long long function */
1145 vpush_global_sym(&func_old_type
, func
);
1150 vtop
->r2
= reg_lret
;
1163 /* stack: L1 H1 L2 H2 */
1168 vtop
[-2] = vtop
[-3];
1171 /* stack: H1 H2 L1 L2 */
1177 /* stack: H1 H2 L1 L2 ML MH */
1180 /* stack: ML MH H1 H2 L1 L2 */
1184 /* stack: ML MH H1 L2 H2 L1 */
1189 /* stack: ML MH M1 M2 */
1192 } else if (op
== '+' || op
== '-') {
1193 /* XXX: add non carry method too (for MIPS or alpha) */
1199 /* stack: H1 H2 (L1 op L2) */
1202 gen_op(op1
+ 1); /* TOK_xxxC2 */
1205 /* stack: H1 H2 (L1 op L2) */
1208 /* stack: (L1 op L2) H1 H2 */
1210 /* stack: (L1 op L2) (H1 op H2) */
1218 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1219 t
= vtop
[-1].type
.t
;
1223 /* stack: L H shift */
1225 /* constant: simpler */
1226 /* NOTE: all comments are for SHL. the other cases are
1227 done by swaping words */
1238 if (op
!= TOK_SAR
) {
1271 /* XXX: should provide a faster fallback on x86 ? */
1274 func
= TOK___ashrdi3
;
1277 func
= TOK___lshrdi3
;
1280 func
= TOK___ashldi3
;
1286 /* compare operations */
1292 /* stack: L1 H1 L2 H2 */
1294 vtop
[-1] = vtop
[-2];
1296 /* stack: L1 L2 H1 H2 */
1299 /* when values are equal, we need to compare low words. since
1300 the jump is inverted, we invert the test too. */
1303 else if (op1
== TOK_GT
)
1305 else if (op1
== TOK_ULT
)
1307 else if (op1
== TOK_UGT
)
1312 if (op1
!= TOK_NE
) {
1316 /* generate non equal test */
1317 /* XXX: NOT PORTABLE yet */
1321 #if defined(TCC_TARGET_I386)
1322 b
= psym(0x850f, 0);
1323 #elif defined(TCC_TARGET_ARM)
1325 o(0x1A000000 | encbranch(ind
, 0, 1));
1326 #elif defined(TCC_TARGET_C67)
1327 tcc_error("not implemented");
1329 #error not supported
1333 /* compare low. Always unsigned */
1337 else if (op1
== TOK_LE
)
1339 else if (op1
== TOK_GT
)
1341 else if (op1
== TOK_GE
)
1352 /* handle integer constant optimizations and various machine
1354 static void gen_opic(int op
)
1356 int c1
, c2
, t1
, t2
, n
;
1359 typedef unsigned long long U
;
1363 t1
= v1
->type
.t
& VT_BTYPE
;
1364 t2
= v2
->type
.t
& VT_BTYPE
;
1368 else if (v1
->type
.t
& VT_UNSIGNED
)
1375 else if (v2
->type
.t
& VT_UNSIGNED
)
1380 /* currently, we cannot do computations with forward symbols */
1381 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1382 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1385 case '+': l1
+= l2
; break;
1386 case '-': l1
-= l2
; break;
1387 case '&': l1
&= l2
; break;
1388 case '^': l1
^= l2
; break;
1389 case '|': l1
|= l2
; break;
1390 case '*': l1
*= l2
; break;
1397 /* if division by zero, generate explicit division */
1400 tcc_error("division by zero in constant");
1404 default: l1
/= l2
; break;
1405 case '%': l1
%= l2
; break;
1406 case TOK_UDIV
: l1
= (U
)l1
/ l2
; break;
1407 case TOK_UMOD
: l1
= (U
)l1
% l2
; break;
1410 case TOK_SHL
: l1
<<= l2
; break;
1411 case TOK_SHR
: l1
= (U
)l1
>> l2
; break;
1412 case TOK_SAR
: l1
>>= l2
; break;
1414 case TOK_ULT
: l1
= (U
)l1
< (U
)l2
; break;
1415 case TOK_UGE
: l1
= (U
)l1
>= (U
)l2
; break;
1416 case TOK_EQ
: l1
= l1
== l2
; break;
1417 case TOK_NE
: l1
= l1
!= l2
; break;
1418 case TOK_ULE
: l1
= (U
)l1
<= (U
)l2
; break;
1419 case TOK_UGT
: l1
= (U
)l1
> (U
)l2
; break;
1420 case TOK_LT
: l1
= l1
< l2
; break;
1421 case TOK_GE
: l1
= l1
>= l2
; break;
1422 case TOK_LE
: l1
= l1
<= l2
; break;
1423 case TOK_GT
: l1
= l1
> l2
; break;
1425 case TOK_LAND
: l1
= l1
&& l2
; break;
1426 case TOK_LOR
: l1
= l1
|| l2
; break;
1433 /* if commutative ops, put c2 as constant */
1434 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1435 op
== '|' || op
== '*')) {
1437 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1438 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1440 /* Filter out NOP operations like x*1, x-0, x&-1... */
1441 if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1444 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1445 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1451 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1452 /* try to use shifts instead of muls or divs */
1453 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1462 else if (op
== TOK_PDIV
)
1468 } else if (c2
&& (op
== '+' || op
== '-') &&
1469 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
1470 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1471 /* symbol + constant case */
1478 if (!nocode_wanted
) {
1479 /* call low level op generator */
1480 if (t1
== VT_LLONG
|| t2
== VT_LLONG
)
1491 /* generate a floating point operation with constant propagation */
1492 static void gen_opif(int op
)
1500 /* currently, we cannot do computations with forward symbols */
1501 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1502 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1504 if (v1
->type
.t
== VT_FLOAT
) {
1507 } else if (v1
->type
.t
== VT_DOUBLE
) {
1515 /* NOTE: we only do constant propagation if finite number (not
1516 NaN or infinity) (ANSI spec) */
1517 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1521 case '+': f1
+= f2
; break;
1522 case '-': f1
-= f2
; break;
1523 case '*': f1
*= f2
; break;
1527 tcc_error("division by zero in constant");
1532 /* XXX: also handles tests ? */
1536 /* XXX: overflow test ? */
1537 if (v1
->type
.t
== VT_FLOAT
) {
1539 } else if (v1
->type
.t
== VT_DOUBLE
) {
1547 if (!nocode_wanted
) {
1555 static int pointed_size(CType
*type
)
1558 return type_size(pointed_type(type
), &align
);
1561 static void vla_runtime_pointed_size(CType
*type
)
1564 vla_runtime_type_size(pointed_type(type
), &align
);
1567 static inline int is_null_pointer(SValue
*p
)
1569 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1571 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& p
->c
.i
== 0) ||
1572 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.ll
== 0) ||
1573 ((p
->type
.t
& VT_BTYPE
) == VT_PTR
&& p
->c
.ptr_offset
== 0);
1576 static inline int is_integer_btype(int bt
)
1578 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
1579 bt
== VT_INT
|| bt
== VT_LLONG
);
1582 /* check types for comparison or substraction of pointers */
1583 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
1585 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
1588 /* null pointers are accepted for all comparisons as gcc */
1589 if (is_null_pointer(p1
) || is_null_pointer(p2
))
1593 bt1
= type1
->t
& VT_BTYPE
;
1594 bt2
= type2
->t
& VT_BTYPE
;
1595 /* accept comparison between pointer and integer with a warning */
1596 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
1597 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
1598 tcc_warning("comparison between pointer and integer");
1602 /* both must be pointers or implicit function pointers */
1603 if (bt1
== VT_PTR
) {
1604 type1
= pointed_type(type1
);
1605 } else if (bt1
!= VT_FUNC
)
1606 goto invalid_operands
;
1608 if (bt2
== VT_PTR
) {
1609 type2
= pointed_type(type2
);
1610 } else if (bt2
!= VT_FUNC
) {
1612 tcc_error("invalid operands to binary %s", get_tok_str(op
, NULL
));
1614 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1615 (type2
->t
& VT_BTYPE
) == VT_VOID
)
1619 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1620 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1621 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1622 /* gcc-like error if '-' is used */
1624 goto invalid_operands
;
1626 tcc_warning("comparison of distinct pointer types lacks a cast");
1630 /* generic gen_op: handles types problems */
1631 ST_FUNC
void gen_op(int op
)
1633 int u
, t1
, t2
, bt1
, bt2
, t
;
1636 t1
= vtop
[-1].type
.t
;
1637 t2
= vtop
[0].type
.t
;
1638 bt1
= t1
& VT_BTYPE
;
1639 bt2
= t2
& VT_BTYPE
;
1641 if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
1642 /* at least one operand is a pointer */
1643 /* relationnal op: must be both pointers */
1644 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
1645 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1646 /* pointers are handled are unsigned */
1647 #ifdef TCC_TARGET_X86_64
1648 t
= VT_LLONG
| VT_UNSIGNED
;
1650 t
= VT_INT
| VT_UNSIGNED
;
1654 /* if both pointers, then it must be the '-' op */
1655 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
1657 tcc_error("cannot use pointers here");
1658 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1659 /* XXX: check that types are compatible */
1660 if (vtop
[-1].type
.t
& VT_VLA
) {
1661 vla_runtime_pointed_size(&vtop
[-1].type
);
1663 vpushi(pointed_size(&vtop
[-1].type
));
1667 /* set to integer type */
1668 #ifdef TCC_TARGET_X86_64
1669 vtop
->type
.t
= VT_LLONG
;
1671 vtop
->type
.t
= VT_INT
;
1676 /* exactly one pointer : must be '+' or '-'. */
1677 if (op
!= '-' && op
!= '+')
1678 tcc_error("cannot use pointers here");
1679 /* Put pointer as first operand */
1680 if (bt2
== VT_PTR
) {
1684 type1
= vtop
[-1].type
;
1685 type1
.t
&= ~VT_ARRAY
;
1686 if (vtop
[-1].type
.t
& VT_VLA
)
1687 vla_runtime_pointed_size(&vtop
[-1].type
);
1689 u
= pointed_size(&vtop
[-1].type
);
1691 tcc_error("unknown array element size");
1692 #ifdef TCC_TARGET_X86_64
1695 /* XXX: cast to int ? (long long case) */
1700 #ifdef CONFIG_TCC_BCHECK
1701 /* if evaluating constant expression, no code should be
1702 generated, so no bound check */
1703 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
1704 /* if bounded pointers, we generate a special code to
1711 gen_bounded_ptr_add();
1717 /* put again type if gen_opic() swaped operands */
1720 } else if (is_float(bt1
) || is_float(bt2
)) {
1721 /* compute bigger type and do implicit casts */
1722 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
1724 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
1729 /* floats can only be used for a few operations */
1730 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
1731 (op
< TOK_ULT
|| op
> TOK_GT
))
1732 tcc_error("invalid operands for binary operation");
1734 } else if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
) {
1735 t
= bt1
== VT_LLONG
? VT_LLONG
: VT_INT
;
1736 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (t
| VT_UNSIGNED
))
1739 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
1740 /* cast to biggest op */
1742 /* convert to unsigned if it does not fit in a long long */
1743 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
1744 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
1747 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
1748 tcc_error("comparison of struct");
1750 /* integer operations */
1752 /* convert to unsigned if it does not fit in an integer */
1753 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
1754 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
1757 /* XXX: currently, some unsigned operations are explicit, so
1758 we modify them here */
1759 if (t
& VT_UNSIGNED
) {
1766 else if (op
== TOK_LT
)
1768 else if (op
== TOK_GT
)
1770 else if (op
== TOK_LE
)
1772 else if (op
== TOK_GE
)
1779 /* special case for shifts and long long: we keep the shift as
1781 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
1788 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
1789 /* relationnal op: the result is an int */
1790 vtop
->type
.t
= VT_INT
;
1797 #ifndef TCC_TARGET_ARM
1798 /* generic itof for unsigned long long case */
1799 static void gen_cvt_itof1(int t
)
1801 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
1802 (VT_LLONG
| VT_UNSIGNED
)) {
1805 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
1806 #if LDOUBLE_SIZE != 8
1807 else if (t
== VT_LDOUBLE
)
1808 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
1811 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
1815 vtop
->r
= reg_fret(t
);
1822 /* generic ftoi for unsigned long long case */
1823 static void gen_cvt_ftoi1(int t
)
1827 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
1828 /* not handled natively */
1829 st
= vtop
->type
.t
& VT_BTYPE
;
1831 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
1832 #if LDOUBLE_SIZE != 8
1833 else if (st
== VT_LDOUBLE
)
1834 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
1837 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
1842 vtop
->r2
= REG_LRET
;
1848 /* force char or short cast */
1849 static void force_charshort_cast(int t
)
1853 /* XXX: add optimization if lvalue : just change type and offset */
1858 if (t
& VT_UNSIGNED
) {
1859 vpushi((1 << bits
) - 1);
1865 /* result must be signed or the SAR is converted to an SHL
1866 This was not the case when "t" was a signed short
1867 and the last value on the stack was an unsigned int */
1868 vtop
->type
.t
&= ~VT_UNSIGNED
;
1874 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1875 static void gen_cast(CType
*type
)
1877 int sbt
, dbt
, sf
, df
, c
, p
;
1879 /* special delayed cast for char/short */
1880 /* XXX: in some cases (multiple cascaded casts), it may still
1882 if (vtop
->r
& VT_MUSTCAST
) {
1883 vtop
->r
&= ~VT_MUSTCAST
;
1884 force_charshort_cast(vtop
->type
.t
);
1887 /* bitfields first get cast to ints */
1888 if (vtop
->type
.t
& VT_BITFIELD
) {
1892 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
1893 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
1898 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1899 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
1901 /* constant case: we can do it now */
1902 /* XXX: in ISOC, cannot do it if error in convert */
1903 if (sbt
== VT_FLOAT
)
1904 vtop
->c
.ld
= vtop
->c
.f
;
1905 else if (sbt
== VT_DOUBLE
)
1906 vtop
->c
.ld
= vtop
->c
.d
;
1909 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
1910 if (sbt
& VT_UNSIGNED
)
1911 vtop
->c
.ld
= vtop
->c
.ull
;
1913 vtop
->c
.ld
= vtop
->c
.ll
;
1915 if (sbt
& VT_UNSIGNED
)
1916 vtop
->c
.ld
= vtop
->c
.ui
;
1918 vtop
->c
.ld
= vtop
->c
.i
;
1921 if (dbt
== VT_FLOAT
)
1922 vtop
->c
.f
= (float)vtop
->c
.ld
;
1923 else if (dbt
== VT_DOUBLE
)
1924 vtop
->c
.d
= (double)vtop
->c
.ld
;
1925 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
1926 vtop
->c
.ull
= (unsigned long long)vtop
->c
.ld
;
1927 } else if (sf
&& dbt
== VT_BOOL
) {
1928 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
1931 vtop
->c
.ll
= (long long)vtop
->c
.ld
;
1932 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
1933 vtop
->c
.ll
= vtop
->c
.ull
;
1934 else if (sbt
& VT_UNSIGNED
)
1935 vtop
->c
.ll
= vtop
->c
.ui
;
1936 #ifdef TCC_TARGET_X86_64
1937 else if (sbt
== VT_PTR
)
1940 else if (sbt
!= VT_LLONG
)
1941 vtop
->c
.ll
= vtop
->c
.i
;
1943 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
1944 vtop
->c
.ull
= vtop
->c
.ll
;
1945 else if (dbt
== VT_BOOL
)
1946 vtop
->c
.i
= (vtop
->c
.ll
!= 0);
1947 #ifdef TCC_TARGET_X86_64
1948 else if (dbt
== VT_PTR
)
1951 else if (dbt
!= VT_LLONG
) {
1953 if ((dbt
& VT_BTYPE
) == VT_BYTE
)
1955 else if ((dbt
& VT_BTYPE
) == VT_SHORT
)
1957 if(dbt
& VT_UNSIGNED
)
1958 vtop
->c
.ui
= ((unsigned int)vtop
->c
.ll
<< s
) >> s
;
1960 vtop
->c
.i
= ((int)vtop
->c
.ll
<< s
) >> s
;
1963 } else if (p
&& dbt
== VT_BOOL
) {
1966 } else if (!nocode_wanted
) {
1967 /* non constant case: generate code */
1969 /* convert from fp to fp */
1972 /* convert int to fp */
1975 /* convert fp to int */
1976 if (dbt
== VT_BOOL
) {
1980 /* we handle char/short/etc... with generic code */
1981 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
1982 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
1986 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
1987 /* additional cast for char/short... */
1992 #ifndef TCC_TARGET_X86_64
1993 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
1994 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
1995 /* scalar to long long */
1996 /* machine independent conversion */
1998 /* generate high word */
1999 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
2003 if (sbt
== VT_PTR
) {
2004 /* cast from pointer to int before we apply
2005 shift operation, which pointers don't support*/
2006 gen_cast(&int_type
);
2012 /* patch second register */
2013 vtop
[-1].r2
= vtop
->r
;
2017 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
2018 (dbt
& VT_BTYPE
) == VT_PTR
||
2019 (dbt
& VT_BTYPE
) == VT_FUNC
) {
2020 if ((sbt
& VT_BTYPE
) != VT_LLONG
&&
2021 (sbt
& VT_BTYPE
) != VT_PTR
&&
2022 (sbt
& VT_BTYPE
) != VT_FUNC
) {
2023 /* need to convert from 32bit to 64bit */
2025 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
2026 /* x86_64 specific: movslq */
2028 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
2032 } else if (dbt
== VT_BOOL
) {
2033 /* scalar to bool */
2036 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
2037 (dbt
& VT_BTYPE
) == VT_SHORT
) {
2038 if (sbt
== VT_PTR
) {
2039 vtop
->type
.t
= VT_INT
;
2040 tcc_warning("nonportable conversion from pointer to char/short");
2042 force_charshort_cast(dbt
);
2043 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
2045 if (sbt
== VT_LLONG
) {
2046 /* from long long: just take low order word */
2050 /* if lvalue and single word type, nothing to do because
2051 the lvalue already contains the real type size (see
2052 VT_LVAL_xxx constants) */
2055 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
2056 /* if we are casting between pointer types,
2057 we must update the VT_LVAL_xxx size */
2058 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
2059 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
2064 /* return type size as known at compile time. Put alignment at 'a' */
2065 ST_FUNC
int type_size(CType
*type
, int *a
)
2070 bt
= type
->t
& VT_BTYPE
;
2071 if (bt
== VT_STRUCT
) {
2076 } else if (bt
== VT_PTR
) {
2077 if (type
->t
& VT_ARRAY
) {
2081 ts
= type_size(&s
->type
, a
);
2083 if (ts
< 0 && s
->c
< 0)
2091 } else if (bt
== VT_LDOUBLE
) {
2093 return LDOUBLE_SIZE
;
2094 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
2095 #ifdef TCC_TARGET_I386
2096 #ifdef TCC_TARGET_PE
2101 #elif defined(TCC_TARGET_ARM)
2111 } else if (bt
== VT_INT
|| bt
== VT_ENUM
|| bt
== VT_FLOAT
) {
2114 } else if (bt
== VT_SHORT
) {
2117 } else if (bt
== VT_QLONG
|| bt
== VT_QFLOAT
) {
2121 /* char, void, function, _Bool */
2127 /* push type size as known at runtime time on top of value stack. Put
2129 ST_FUNC
void vla_runtime_type_size(CType
*type
, int *a
)
2131 if (type
->t
& VT_VLA
) {
2132 vset(&int_type
, VT_LOCAL
|VT_LVAL
, type
->ref
->c
);
2134 vpushi(type_size(type
, a
));
2138 static void vla_sp_save(void) {
2139 if (!(vla_flags
& VLA_SP_LOC_SET
)) {
2140 *vla_sp_loc
= (loc
-= PTR_SIZE
);
2141 vla_flags
|= VLA_SP_LOC_SET
;
2143 if (!(vla_flags
& VLA_SP_SAVED
)) {
2144 gen_vla_sp_save(*vla_sp_loc
);
2145 vla_flags
|= VLA_SP_SAVED
;
2149 /* return the pointed type of t */
2150 static inline CType
*pointed_type(CType
*type
)
2152 return &type
->ref
->type
;
2155 /* modify type so that its it is a pointer to type. */
2156 ST_FUNC
void mk_pointer(CType
*type
)
2159 s
= sym_push(SYM_FIELD
, type
, 0, -1);
2160 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
2164 /* compare function types. OLD functions match any new functions */
2165 static int is_compatible_func(CType
*type1
, CType
*type2
)
2171 if (!is_compatible_types(&s1
->type
, &s2
->type
))
2173 /* check func_call */
2174 if (s1
->a
.func_call
!= s2
->a
.func_call
)
2176 /* XXX: not complete */
2177 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
2181 while (s1
!= NULL
) {
2184 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
2194 /* return true if type1 and type2 are the same. If unqualified is
2195 true, qualifiers on the types are ignored.
2197 - enums are not checked as gcc __builtin_types_compatible_p ()
2199 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2203 t1
= type1
->t
& VT_TYPE
;
2204 t2
= type2
->t
& VT_TYPE
;
2206 /* strip qualifiers before comparing */
2207 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2208 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2210 /* Default Vs explicit signedness only matters for char */
2211 if ((t1
& VT_BTYPE
) != VT_BYTE
) {
2215 /* XXX: bitfields ? */
2218 /* test more complicated cases */
2219 bt1
= t1
& VT_BTYPE
;
2220 if (bt1
== VT_PTR
) {
2221 type1
= pointed_type(type1
);
2222 type2
= pointed_type(type2
);
2223 return is_compatible_types(type1
, type2
);
2224 } else if (bt1
== VT_STRUCT
) {
2225 return (type1
->ref
== type2
->ref
);
2226 } else if (bt1
== VT_FUNC
) {
2227 return is_compatible_func(type1
, type2
);
2233 /* return true if type1 and type2 are exactly the same (including
2236 static int is_compatible_types(CType
*type1
, CType
*type2
)
2238 return compare_types(type1
,type2
,0);
2241 /* return true if type1 and type2 are the same (ignoring qualifiers).
2243 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
2245 return compare_types(type1
,type2
,1);
2248 /* print a type. If 'varstr' is not NULL, then the variable is also
2249 printed in the type */
2251 /* XXX: add array and function pointers */
2252 static void type_to_str(char *buf
, int buf_size
,
2253 CType
*type
, const char *varstr
)
2260 t
= type
->t
& VT_TYPE
;
2263 if (t
& VT_CONSTANT
)
2264 pstrcat(buf
, buf_size
, "const ");
2265 if (t
& VT_VOLATILE
)
2266 pstrcat(buf
, buf_size
, "volatile ");
2267 if ((t
& (VT_DEFSIGN
| VT_UNSIGNED
)) == (VT_DEFSIGN
| VT_UNSIGNED
))
2268 pstrcat(buf
, buf_size
, "unsigned ");
2269 else if (t
& VT_DEFSIGN
)
2270 pstrcat(buf
, buf_size
, "signed ");
2300 tstr
= "long double";
2302 pstrcat(buf
, buf_size
, tstr
);
2306 if (bt
== VT_STRUCT
)
2310 pstrcat(buf
, buf_size
, tstr
);
2311 v
= type
->ref
->v
& ~SYM_STRUCT
;
2312 if (v
>= SYM_FIRST_ANOM
)
2313 pstrcat(buf
, buf_size
, "<anonymous>");
2315 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
2319 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
2320 pstrcat(buf
, buf_size
, "(");
2322 while (sa
!= NULL
) {
2323 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
2324 pstrcat(buf
, buf_size
, buf1
);
2327 pstrcat(buf
, buf_size
, ", ");
2329 pstrcat(buf
, buf_size
, ")");
2333 pstrcpy(buf1
, sizeof(buf1
), "*");
2335 pstrcat(buf1
, sizeof(buf1
), varstr
);
2336 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2340 pstrcat(buf
, buf_size
, " ");
2341 pstrcat(buf
, buf_size
, varstr
);
2346 /* verify type compatibility to store vtop in 'dt' type, and generate
2348 static void gen_assign_cast(CType
*dt
)
2350 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
2351 char buf1
[256], buf2
[256];
2354 st
= &vtop
->type
; /* source type */
2355 dbt
= dt
->t
& VT_BTYPE
;
2356 sbt
= st
->t
& VT_BTYPE
;
2357 if (sbt
== VT_VOID
|| dbt
== VT_VOID
)
2358 tcc_error("cannot cast from/to void");
2359 if (dt
->t
& VT_CONSTANT
)
2360 tcc_warning("assignment of read-only location");
2363 /* special cases for pointers */
2364 /* '0' can also be a pointer */
2365 if (is_null_pointer(vtop
))
2367 /* accept implicit pointer to integer cast with warning */
2368 if (is_integer_btype(sbt
)) {
2369 tcc_warning("assignment makes pointer from integer without a cast");
2372 type1
= pointed_type(dt
);
2373 /* a function is implicitely a function pointer */
2374 if (sbt
== VT_FUNC
) {
2375 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
2376 !is_compatible_types(pointed_type(dt
), st
))
2377 tcc_warning("assignment from incompatible pointer type");
2382 type2
= pointed_type(st
);
2383 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2384 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
2385 /* void * can match anything */
2387 /* exact type match, except for unsigned */
2390 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
|
2392 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
|
2394 if (!is_compatible_types(&tmp_type1
, &tmp_type2
))
2395 tcc_warning("assignment from incompatible pointer type");
2397 /* check const and volatile */
2398 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
2399 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
2400 tcc_warning("assignment discards qualifiers from pointer target type");
2406 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
2407 tcc_warning("assignment makes integer from pointer without a cast");
2409 /* XXX: more tests */
2414 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2415 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2416 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2418 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
2419 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
2420 tcc_error("cannot cast '%s' to '%s'", buf1
, buf2
);
2428 /* store vtop in lvalue pushed on stack */
2429 ST_FUNC
void vstore(void)
2431 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
2433 ft
= vtop
[-1].type
.t
;
2434 sbt
= vtop
->type
.t
& VT_BTYPE
;
2435 dbt
= ft
& VT_BTYPE
;
2436 if ((((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2437 (sbt
== VT_INT
&& dbt
== VT_SHORT
))
2438 && !(vtop
->type
.t
& VT_BITFIELD
)) {
2439 /* optimize char/short casts */
2440 delayed_cast
= VT_MUSTCAST
;
2441 vtop
->type
.t
= ft
& (VT_TYPE
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
)));
2442 /* XXX: factorize */
2443 if (ft
& VT_CONSTANT
)
2444 tcc_warning("assignment of read-only location");
2447 if (!(ft
& VT_BITFIELD
))
2448 gen_assign_cast(&vtop
[-1].type
);
2451 if (sbt
== VT_STRUCT
) {
2452 /* if structure, only generate pointer */
2453 /* structure assignment : generate memcpy */
2454 /* XXX: optimize if small size */
2455 if (!nocode_wanted
) {
2456 size
= type_size(&vtop
->type
, &align
);
2460 vtop
->type
.t
= VT_PTR
;
2463 /* address of memcpy() */
2466 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2467 else if(!(align
& 3))
2468 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2471 vpush_global_sym(&func_old_type
, TOK_memcpy
);
2476 vtop
->type
.t
= VT_PTR
;
2485 /* leave source on stack */
2486 } else if (ft
& VT_BITFIELD
) {
2487 /* bitfield store handling */
2488 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2489 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2490 /* remove bit field info to avoid loops */
2491 vtop
[-1].type
.t
= ft
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
2493 /* duplicate source into other register */
2498 if((ft
& VT_BTYPE
) == VT_BOOL
) {
2499 gen_cast(&vtop
[-1].type
);
2500 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2503 /* duplicate destination */
2505 vtop
[-1] = vtop
[-2];
2507 /* mask and shift source */
2508 if((ft
& VT_BTYPE
) != VT_BOOL
) {
2509 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2510 vpushll((1ULL << bit_size
) - 1ULL);
2512 vpushi((1 << bit_size
) - 1);
2518 /* load destination, mask and or with source */
2520 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2521 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2523 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2530 /* pop off shifted source from "duplicate source..." above */
2534 #ifdef CONFIG_TCC_BCHECK
2535 /* bound check case */
2536 if (vtop
[-1].r
& VT_MUSTBOUND
) {
2542 if (!nocode_wanted
) {
2546 #ifdef TCC_TARGET_X86_64
2547 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
2549 } else if ((ft
& VT_BTYPE
) == VT_QFLOAT
) {
2554 r
= gv(rc
); /* generate value */
2555 /* if lvalue was saved on stack, must read it */
2556 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
2558 t
= get_reg(RC_INT
);
2559 #ifdef TCC_TARGET_X86_64
2564 sv
.r
= VT_LOCAL
| VT_LVAL
;
2565 sv
.c
.ul
= vtop
[-1].c
.ul
;
2567 vtop
[-1].r
= t
| VT_LVAL
;
2569 /* two word case handling : store second register at word + 4 (or +8 for x86-64) */
2570 #ifdef TCC_TARGET_X86_64
2571 if (((ft
& VT_BTYPE
) == VT_QLONG
) || ((ft
& VT_BTYPE
) == VT_QFLOAT
)) {
2572 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
2574 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
2575 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
2577 vtop
[-1].type
.t
= load_type
;
2580 /* convert to int to increment easily */
2581 vtop
->type
.t
= addr_type
;
2587 vtop
[-1].type
.t
= load_type
;
2588 /* XXX: it works because r2 is spilled last ! */
2589 store(vtop
->r2
, vtop
- 1);
2595 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
2596 vtop
->r
|= delayed_cast
;
2600 /* post defines POST/PRE add. c is the token ++ or -- */
2601 ST_FUNC
void inc(int post
, int c
)
2604 vdup(); /* save lvalue */
2606 gv_dup(); /* duplicate value */
2611 vpushi(c
- TOK_MID
);
2613 vstore(); /* store value */
2615 vpop(); /* if post op, return saved value */
2618 /* Parse GNUC __attribute__ extension. Currently, the following
2619 extensions are recognized:
2620 - aligned(n) : set data/function alignment.
2621 - packed : force data alignment to 1
2622 - section(x) : generate data/code in this section.
2623 - unused : currently ignored, but may be used someday.
2624 - regparm(n) : pass function parameters in registers (i386 only)
2626 static void parse_attribute(AttributeDef
*ad
)
2630 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
2634 while (tok
!= ')') {
2635 if (tok
< TOK_IDENT
)
2636 expect("attribute name");
2644 expect("section name");
2645 ad
->section
= find_section(tcc_state
, (char *)tokc
.cstr
->data
);
2653 expect("alias(\"target\")");
2654 ad
->alias_target
= /* save string as token, for later */
2655 tok_alloc((char*)tokc
.cstr
->data
, tokc
.cstr
->size
-1)->tok
;
2664 if (n
<= 0 || (n
& (n
- 1)) != 0)
2665 tcc_error("alignment must be a positive power of two");
2682 /* currently, no need to handle it because tcc does not
2683 track unused objects */
2687 /* currently, no need to handle it because tcc does not
2688 track unused objects */
2693 ad
->a
.func_call
= FUNC_CDECL
;
2698 ad
->a
.func_call
= FUNC_STDCALL
;
2700 #ifdef TCC_TARGET_I386
2710 ad
->a
.func_call
= FUNC_FASTCALL1
+ n
- 1;
2716 ad
->a
.func_call
= FUNC_FASTCALLW
;
2723 ad
->a
.mode
= VT_LLONG
+ 1;
2726 ad
->a
.mode
= VT_SHORT
+ 1;
2729 ad
->a
.mode
= VT_INT
+ 1;
2732 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
2739 ad
->a
.func_export
= 1;
2742 ad
->a
.func_import
= 1;
2745 if (tcc_state
->warn_unsupported
)
2746 tcc_warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
2747 /* skip parameters */
2749 int parenthesis
= 0;
2753 else if (tok
== ')')
2756 } while (parenthesis
&& tok
!= -1);
2769 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2770 static void struct_decl(CType
*type
, int u
, int tdef
)
2772 int a
, v
, size
, align
, maxalign
, c
, offset
, flexible
;
2773 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
, prevbt
;
2774 Sym
*s
, *ss
, *ass
, **ps
;
2778 a
= tok
; /* save decl type */
2783 /* struct already defined ? return it */
2785 expect("struct/union/enum name");
2789 tcc_error("invalid type");
2791 } else if (tok
>= TOK_IDENT
&& !tdef
)
2792 tcc_error("unknown struct/union/enum");
2798 /* we put an undefined size for struct/union */
2799 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
2800 s
->r
= 0; /* default alignment is zero as gcc */
2801 /* put struct/union/enum name in type */
2809 tcc_error("struct/union/enum already defined");
2810 /* cannot be empty */
2812 /* non empty enums are not allowed */
2813 if (a
== TOK_ENUM
) {
2817 expect("identifier");
2819 if (ss
&& !local_stack
)
2820 tcc_error("redefinition of enumerator '%s'",
2821 get_tok_str(v
, NULL
));
2827 /* enum symbols have static storage */
2828 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
2829 ss
->type
.t
|= VT_STATIC
;
2834 /* NOTE: we accept a trailing comma */
2838 s
->c
= type_size(&int_type
, &align
);
2847 while (tok
!= '}') {
2848 parse_btype(&btype
, &ad
);
2851 tcc_error("flexible array member '%s' not at the end of struct",
2852 get_tok_str(v
, NULL
));
2857 type_decl(&type1
, &ad
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
2858 if (v
== 0 && (type1
.t
& VT_BTYPE
) != VT_STRUCT
)
2859 expect("identifier");
2860 if (type_size(&type1
, &align
) < 0) {
2861 if ((a
== TOK_STRUCT
) && (type1
.t
& VT_ARRAY
) && c
)
2864 tcc_error("field '%s' has incomplete type",
2865 get_tok_str(v
, NULL
));
2867 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
2868 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
2869 tcc_error("invalid type for '%s'",
2870 get_tok_str(v
, NULL
));
2874 bit_size
= expr_const();
2875 /* XXX: handle v = 0 case for messages */
2877 tcc_error("negative width in bit-field '%s'",
2878 get_tok_str(v
, NULL
));
2879 if (v
&& bit_size
== 0)
2880 tcc_error("zero width for bit-field '%s'",
2881 get_tok_str(v
, NULL
));
2883 size
= type_size(&type1
, &align
);
2885 if (align
< ad
.a
.aligned
)
2886 align
= ad
.a
.aligned
;
2887 } else if (ad
.a
.packed
) {
2889 } else if (*tcc_state
->pack_stack_ptr
) {
2890 if (align
> *tcc_state
->pack_stack_ptr
)
2891 align
= *tcc_state
->pack_stack_ptr
;
2894 if (bit_size
>= 0) {
2895 bt
= type1
.t
& VT_BTYPE
;
2902 tcc_error("bitfields must have scalar type");
2904 if (bit_size
> bsize
) {
2905 tcc_error("width of '%s' exceeds its type",
2906 get_tok_str(v
, NULL
));
2907 } else if (bit_size
== bsize
) {
2908 /* no need for bit fields */
2910 } else if (bit_size
== 0) {
2911 /* XXX: what to do if only padding in a
2913 /* zero size: means to pad */
2916 /* we do not have enough room ?
2917 did the type change?
2919 if ((bit_pos
+ bit_size
) > bsize
||
2920 bt
!= prevbt
|| a
== TOK_UNION
)
2923 /* XXX: handle LSB first */
2924 type1
.t
|= VT_BITFIELD
|
2925 (bit_pos
<< VT_STRUCT_SHIFT
) |
2926 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
2927 bit_pos
+= bit_size
;
2933 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2934 /* add new memory data only if starting
2936 if (lbit_pos
== 0) {
2937 if (a
== TOK_STRUCT
) {
2938 c
= (c
+ align
- 1) & -align
;
2947 if (align
> maxalign
)
2951 printf("add field %s offset=%d",
2952 get_tok_str(v
, NULL
), offset
);
2953 if (type1
.t
& VT_BITFIELD
) {
2954 printf(" pos=%d size=%d",
2955 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
2956 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
2961 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2963 while ((ass
= ass
->next
) != NULL
) {
2964 ss
= sym_push(ass
->v
, &ass
->type
, 0, offset
+ ass
->c
);
2969 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
2973 if (tok
== ';' || tok
== TOK_EOF
)
2980 /* store size and alignment */
2981 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
2987 /* return 1 if basic type is a type size (short, long, long long) */
2988 int is_btype_size (int bt
)
2990 return bt
== VT_SHORT
|| bt
== VT_LONG
|| bt
== VT_LLONG
;
2993 /* return 0 if no type declaration. otherwise, return the basic type
2996 static int parse_btype(CType
*type
, AttributeDef
*ad
)
2998 int t
, u
, bt_size
, complete
, type_found
, typespec_found
;
3002 memset(ad
, 0, sizeof(AttributeDef
));
3010 /* currently, we really ignore extension */
3021 tcc_error("too many basic types");
3023 bt_size
= is_btype_size (u
& VT_BTYPE
);
3024 if (u
== VT_INT
|| (!bt_size
&& !(t
& VT_TYPEDEF
)))
3039 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
3040 #ifndef TCC_TARGET_PE
3041 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3043 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
3044 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3058 if ((t
& VT_BTYPE
) == VT_LONG
) {
3059 #ifdef TCC_TARGET_PE
3060 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
3062 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3070 struct_decl(&type1
, VT_ENUM
, t
& VT_TYPEDEF
);
3073 type
->ref
= type1
.ref
;
3077 struct_decl(&type1
, VT_STRUCT
, t
& VT_TYPEDEF
);
3080 /* type modifiers */
3096 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == (VT_DEFSIGN
|VT_UNSIGNED
))
3097 tcc_error("signed and unsigned modifier");
3110 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == VT_DEFSIGN
)
3111 tcc_error("signed and unsigned modifier");
3112 t
|= VT_DEFSIGN
| VT_UNSIGNED
;
3137 /* GNUC attribute */
3138 case TOK_ATTRIBUTE1
:
3139 case TOK_ATTRIBUTE2
:
3140 parse_attribute(ad
);
3143 t
= (t
& ~VT_BTYPE
) | u
;
3151 parse_expr_type(&type1
);
3152 /* remove all storage modifiers except typedef */
3153 type1
.t
&= ~(VT_STORAGE
&~VT_TYPEDEF
);
3159 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
3161 t
|= (s
->type
.t
& ~VT_TYPEDEF
);
3162 type
->ref
= s
->type
.ref
;
3164 /* get attributes from typedef */
3165 if (0 == ad
->a
.aligned
)
3166 ad
->a
.aligned
= s
->a
.aligned
;
3167 if (0 == ad
->a
.func_call
)
3168 ad
->a
.func_call
= s
->a
.func_call
;
3169 ad
->a
.packed
|= s
->a
.packed
;
3178 if (tcc_state
->char_is_unsigned
) {
3179 if ((t
& (VT_DEFSIGN
|VT_BTYPE
)) == VT_BYTE
)
3183 /* long is never used as type */
3184 if ((t
& VT_BTYPE
) == VT_LONG
)
3185 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
3186 t
= (t
& ~VT_BTYPE
) | VT_INT
;
3188 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3194 /* convert a function parameter type (array to pointer and function to
3195 function pointer) */
3196 static inline void convert_parameter_type(CType
*pt
)
3198 /* remove const and volatile qualifiers (XXX: const could be used
3199 to indicate a const function parameter */
3200 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3201 /* array must be transformed to pointer according to ANSI C */
3203 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
3208 ST_FUNC
void parse_asm_str(CString
*astr
)
3211 /* read the string */
3213 expect("string constant");
3215 while (tok
== TOK_STR
) {
3216 /* XXX: add \0 handling too ? */
3217 cstr_cat(astr
, tokc
.cstr
->data
);
3220 cstr_ccat(astr
, '\0');
3223 /* Parse an asm label and return the label
3224 * Don't forget to free the CString in the caller! */
3225 static void asm_label_instr(CString
*astr
)
3228 parse_asm_str(astr
);
3231 printf("asm_alias: \"%s\"\n", (char *)astr
->data
);
3235 static void post_type(CType
*type
, AttributeDef
*ad
)
3237 int n
, l
, t1
, arg_size
, align
;
3238 Sym
**plast
, *s
, *first
;
3243 /* function declaration */
3251 /* read param name and compute offset */
3252 if (l
!= FUNC_OLD
) {
3253 if (!parse_btype(&pt
, &ad1
)) {
3255 tcc_error("invalid type");
3262 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
3264 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3265 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
3266 tcc_error("parameter declared as void");
3267 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
3272 expect("identifier");
3276 convert_parameter_type(&pt
);
3277 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
3283 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
3290 /* if no parameters, then old type prototype */
3294 /* NOTE: const is ignored in returned type as it has a special
3295 meaning in gcc / C++ */
3296 type
->t
&= ~VT_CONSTANT
;
3297 /* some ancient pre-K&R C allows a function to return an array
3298 and the array brackets to be put after the arguments, such
3299 that "int c()[]" means something like "int[] c()" */
3302 skip(']'); /* only handle simple "[]" */
3305 /* we push a anonymous symbol which will contain the function prototype */
3306 ad
->a
.func_args
= arg_size
;
3307 s
= sym_push(SYM_FIELD
, type
, 0, l
);
3312 } else if (tok
== '[') {
3313 /* array definition */
3315 if (tok
== TOK_RESTRICT1
)
3320 if (!local_stack
|| nocode_wanted
)
3321 vpushi(expr_const());
3323 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3326 tcc_error("invalid array size");
3328 if (!is_integer_btype(vtop
->type
.t
& VT_BTYPE
))
3329 tcc_error("size of variable length array should be an integer");
3334 /* parse next post type */
3335 post_type(type
, ad
);
3336 if (type
->t
== VT_FUNC
)
3337 tcc_error("declaration of an array of functions");
3338 t1
|= type
->t
& VT_VLA
;
3341 loc
-= type_size(&int_type
, &align
);
3345 vla_runtime_type_size(type
, &align
);
3347 vset(&int_type
, VT_LOCAL
|VT_LVAL
, loc
);
3354 /* we push an anonymous symbol which will contain the array
3356 s
= sym_push(SYM_FIELD
, type
, 0, n
);
3357 type
->t
= (t1
? VT_VLA
: VT_ARRAY
) | VT_PTR
;
3362 /* Parse a type declaration (except basic type), and return the type
3363 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3364 expected. 'type' should contain the basic type. 'ad' is the
3365 attribute definition of the basic type. It can be modified by
3368 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
3371 CType type1
, *type2
;
3372 int qualifiers
, storage
;
3374 while (tok
== '*') {
3382 qualifiers
|= VT_CONSTANT
;
3387 qualifiers
|= VT_VOLATILE
;
3395 type
->t
|= qualifiers
;
3398 /* XXX: clarify attribute handling */
3399 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3400 parse_attribute(ad
);
3402 /* recursive type */
3403 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3404 type1
.t
= 0; /* XXX: same as int */
3407 /* XXX: this is not correct to modify 'ad' at this point, but
3408 the syntax is not clear */
3409 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3410 parse_attribute(ad
);
3411 type_decl(&type1
, ad
, v
, td
);
3414 /* type identifier */
3415 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
3419 if (!(td
& TYPE_ABSTRACT
))
3420 expect("identifier");
3424 storage
= type
->t
& VT_STORAGE
;
3425 type
->t
&= ~VT_STORAGE
;
3426 if (storage
& VT_STATIC
) {
3427 int saved_nocode_wanted
= nocode_wanted
;
3429 post_type(type
, ad
);
3430 nocode_wanted
= saved_nocode_wanted
;
3432 post_type(type
, ad
);
3434 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3435 parse_attribute(ad
);
3439 /* append type at the end of type1 */
3452 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3453 ST_FUNC
int lvalue_type(int t
)
3458 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
3460 else if (bt
== VT_SHORT
)
3464 if (t
& VT_UNSIGNED
)
3465 r
|= VT_LVAL_UNSIGNED
;
3469 /* indirection with full error checking and bound check */
3470 ST_FUNC
void indir(void)
3472 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
3473 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
3477 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
3479 vtop
->type
= *pointed_type(&vtop
->type
);
3480 /* Arrays and functions are never lvalues */
3481 if (!(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_VLA
)
3482 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3483 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3484 /* if bound checking, the referenced pointer must be checked */
3485 #ifdef CONFIG_TCC_BCHECK
3486 if (tcc_state
->do_bounds_check
)
3487 vtop
->r
|= VT_MUSTBOUND
;
3492 /* pass a parameter to a function and do type checking and casting */
3493 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
3498 func_type
= func
->c
;
3499 if (func_type
== FUNC_OLD
||
3500 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
3501 /* default casting : only need to convert float to double */
3502 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
3505 } else if (vtop
->type
.t
& VT_BITFIELD
) {
3506 type
.t
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
3509 } else if (arg
== NULL
) {
3510 tcc_error("too many arguments to function");
3513 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
3514 gen_assign_cast(&type
);
3518 /* parse an expression of the form '(type)' or '(expr)' and return its
3520 static void parse_expr_type(CType
*type
)
3526 if (parse_btype(type
, &ad
)) {
3527 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3534 static void parse_type(CType
*type
)
3539 if (!parse_btype(type
, &ad
)) {
3542 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3545 static void vpush_tokc(int t
)
3550 vsetc(&type
, VT_CONST
, &tokc
);
3553 ST_FUNC
void unary(void)
3555 int n
, t
, align
, size
, r
, sizeof_caller
;
3559 static int in_sizeof
= 0;
3561 sizeof_caller
= in_sizeof
;
3563 /* XXX: GCC 2.95.3 does not generate a table although it should be
3577 vpush_tokc(VT_INT
| VT_UNSIGNED
);
3581 vpush_tokc(VT_LLONG
);
3585 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
3589 vpush_tokc(VT_FLOAT
);
3593 vpush_tokc(VT_DOUBLE
);
3597 vpush_tokc(VT_LDOUBLE
);
3600 case TOK___FUNCTION__
:
3602 goto tok_identifier
;
3608 /* special function name identifier */
3609 len
= strlen(funcname
) + 1;
3610 /* generate char[len] type */
3615 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
3616 ptr
= section_ptr_add(data_section
, len
);
3617 memcpy(ptr
, funcname
, len
);
3622 #ifdef TCC_TARGET_PE
3623 t
= VT_SHORT
| VT_UNSIGNED
;
3629 /* string parsing */
3632 if (tcc_state
->warn_write_strings
)
3637 memset(&ad
, 0, sizeof(AttributeDef
));
3638 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, NULL
, 0);
3643 if (parse_btype(&type
, &ad
)) {
3644 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
3646 /* check ISOC99 compound literal */
3648 /* data is allocated locally by default */
3653 /* all except arrays are lvalues */
3654 if (!(type
.t
& VT_ARRAY
))
3655 r
|= lvalue_type(type
.t
);
3656 memset(&ad
, 0, sizeof(AttributeDef
));
3657 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, NULL
, 0);
3659 if (sizeof_caller
) {
3666 } else if (tok
== '{') {
3667 /* save all registers */
3669 /* statement expression : we do not accept break/continue
3670 inside as GCC does */
3671 block(NULL
, NULL
, NULL
, NULL
, 0, 1);
3686 /* functions names must be treated as function pointers,
3687 except for unary '&' and sizeof. Since we consider that
3688 functions are not lvalues, we only have to handle it
3689 there and in function calls. */
3690 /* arrays can also be used although they are not lvalues */
3691 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
3692 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
3694 mk_pointer(&vtop
->type
);
3700 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3702 boolean
.t
= VT_BOOL
;
3704 vtop
->c
.i
= !vtop
->c
.i
;
3705 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
3706 vtop
->c
.i
= vtop
->c
.i
^ 1;
3709 vseti(VT_JMP
, gvtst(1, 0));
3721 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
3722 tcc_error("pointer not accepted for unary plus");
3723 /* In order to force cast, we add zero, except for floating point
3724 where we really need an noop (otherwise -0.0 will be transformed
3726 if (!is_float(vtop
->type
.t
)) {
3737 unary_type(&type
); // Perform a in_sizeof = 0;
3738 size
= type_size(&type
, &align
);
3739 if (t
== TOK_SIZEOF
) {
3740 if (!(type
.t
& VT_VLA
)) {
3742 tcc_error("sizeof applied to an incomplete type");
3745 vla_runtime_type_size(&type
, &align
);
3750 vtop
->type
.t
|= VT_UNSIGNED
;
3753 case TOK_builtin_types_compatible_p
:
3762 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3763 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3764 vpushi(is_compatible_types(&type1
, &type2
));
3767 case TOK_builtin_constant_p
:
3769 int saved_nocode_wanted
, res
;
3772 saved_nocode_wanted
= nocode_wanted
;
3775 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
3777 nocode_wanted
= saved_nocode_wanted
;
3782 case TOK_builtin_frame_address
:
3788 if (tok
!= TOK_CINT
|| tokc
.i
< 0) {
3789 tcc_error("__builtin_frame_address only takes positive integers");
3796 vset(&type
, VT_LOCAL
, 0); /* local frame */
3798 mk_pointer(&vtop
->type
);
3799 indir(); /* -> parent frame */
3803 #ifdef TCC_TARGET_X86_64
3804 #ifdef TCC_TARGET_PE
3805 case TOK_builtin_va_start
:
3813 if ((vtop
->r
& VT_VALMASK
) != VT_LOCAL
)
3814 tcc_error("__builtin_va_start expects a local variable");
3815 vtop
->r
&= ~(VT_LVAL
| VT_REF
);
3816 vtop
->type
= char_pointer_type
;
3821 case TOK_builtin_va_arg_types
:
3828 vpushi(classify_x86_64_va_arg(&type
));
3843 t
= vtop
->type
.t
& VT_BTYPE
;
3845 /* In IEEE negate(x) isn't subtract(0,x), but rather
3850 else if (t
== VT_DOUBLE
)
3861 goto tok_identifier
;
3863 /* allow to take the address of a label */
3864 if (tok
< TOK_UIDENT
)
3865 expect("label identifier");
3866 s
= label_find(tok
);
3868 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
3870 if (s
->r
== LABEL_DECLARED
)
3871 s
->r
= LABEL_FORWARD
;
3874 s
->type
.t
= VT_VOID
;
3875 mk_pointer(&s
->type
);
3876 s
->type
.t
|= VT_STATIC
;
3878 vpushsym(&s
->type
, s
);
3882 // special qnan , snan and infinity values
3884 vpush64(VT_DOUBLE
, 0x7ff8000000000000ULL
);
3888 vpush64(VT_DOUBLE
, 0x7ff0000000000001ULL
);
3892 vpush64(VT_DOUBLE
, 0x7ff0000000000000ULL
);
3901 expect("identifier");
3905 tcc_error("'%s' undeclared", get_tok_str(t
, NULL
));
3906 /* for simple function calls, we tolerate undeclared
3907 external reference to int() function */
3908 if (tcc_state
->warn_implicit_function_declaration
)
3909 tcc_warning("implicit declaration of function '%s'",
3910 get_tok_str(t
, NULL
));
3911 s
= external_global_sym(t
, &func_old_type
, 0);
3913 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
3914 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
3915 /* if referencing an inline function, then we generate a
3916 symbol to it if not already done. It will have the
3917 effect to generate code for it at the end of the
3918 compilation unit. Inline function as always
3919 generated in the text section. */
3921 put_extern_sym(s
, text_section
, 0, 0);
3922 r
= VT_SYM
| VT_CONST
;
3926 vset(&s
->type
, r
, s
->c
);
3927 /* if forward reference, we must point to s */
3928 if (vtop
->r
& VT_SYM
) {
3930 vtop
->c
.ptr_offset
= 0;
3935 /* post operations */
3937 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
3940 } else if (tok
== '.' || tok
== TOK_ARROW
) {
3943 if (tok
== TOK_ARROW
)
3945 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
3949 /* expect pointer on structure */
3950 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
3951 expect("struct or union");
3955 while ((s
= s
->next
) != NULL
) {
3960 tcc_error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, NULL
));
3961 /* add field offset to pointer */
3962 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
3965 /* change type to field type, and set to lvalue */
3966 vtop
->type
= s
->type
;
3967 vtop
->type
.t
|= qualifiers
;
3968 /* an array is never an lvalue */
3969 if (!(vtop
->type
.t
& VT_ARRAY
)) {
3970 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3971 #ifdef CONFIG_TCC_BCHECK
3972 /* if bound checking, the referenced pointer must be checked */
3973 if (tcc_state
->do_bounds_check
)
3974 vtop
->r
|= VT_MUSTBOUND
;
3978 } else if (tok
== '[') {
3984 } else if (tok
== '(') {
3987 int nb_args
, ret_nregs
, ret_align
, variadic
;
3990 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3991 /* pointer test (no array accepted) */
3992 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
3993 vtop
->type
= *pointed_type(&vtop
->type
);
3994 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
3998 expect("function pointer");
4001 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
4003 /* get return type */
4006 sa
= s
->next
; /* first parameter */
4009 /* compute first implicit argument if a structure is returned */
4010 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
4011 variadic
= (s
->c
== FUNC_ELLIPSIS
);
4012 ret_nregs
= gfunc_sret(&s
->type
, variadic
, &ret
.type
,
4015 /* get some space for the returned structure */
4016 size
= type_size(&s
->type
, &align
);
4017 loc
= (loc
- size
) & -align
;
4019 ret
.r
= VT_LOCAL
| VT_LVAL
;
4020 /* pass it as 'int' to avoid structure arg passing
4022 vseti(VT_LOCAL
, loc
);
4032 /* return in register */
4033 if (is_float(ret
.type
.t
)) {
4034 ret
.r
= reg_fret(ret
.type
.t
);
4035 #ifdef TCC_TARGET_X86_64
4036 if ((ret
.type
.t
& VT_BTYPE
) == VT_QFLOAT
)
4040 #ifdef TCC_TARGET_X86_64
4041 if ((ret
.type
.t
& VT_BTYPE
) == VT_QLONG
)
4043 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
4053 gfunc_param_typed(s
, sa
);
4063 tcc_error("too few arguments to function");
4065 if (!nocode_wanted
) {
4066 gfunc_call(nb_args
);
4068 vtop
-= (nb_args
+ 1);
4072 for (r
= ret
.r
+ ret_nregs
+ !ret_nregs
; r
-- > ret
.r
;) {
4073 vsetc(&ret
.type
, r
, &ret
.c
);
4074 vtop
->r2
= ret
.r2
; /* Loop only happens when r2 is VT_CONST */
4077 /* handle packed struct return */
4078 if (((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) && ret_nregs
) {
4081 size
= type_size(&s
->type
, &align
);
4082 loc
= (loc
- size
) & -align
;
4086 vset(&ret
.type
, VT_LOCAL
| VT_LVAL
, addr
+ offset
);
4090 if (--ret_nregs
== 0)
4092 /* XXX: compatible with arm only: ret_align == register_size */
4093 offset
+= ret_align
;
4095 vset(&s
->type
, VT_LOCAL
| VT_LVAL
, addr
);
4103 ST_FUNC
void expr_prod(void)
4108 while (tok
== '*' || tok
== '/' || tok
== '%') {
4116 ST_FUNC
void expr_sum(void)
4121 while (tok
== '+' || tok
== '-') {
4129 static void expr_shift(void)
4134 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
4142 static void expr_cmp(void)
4147 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
4148 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
4156 static void expr_cmpeq(void)
4161 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
4169 static void expr_and(void)
4172 while (tok
== '&') {
4179 static void expr_xor(void)
4182 while (tok
== '^') {
4189 static void expr_or(void)
4192 while (tok
== '|') {
4199 /* XXX: fix this mess */
4200 static void expr_land_const(void)
4203 while (tok
== TOK_LAND
) {
4210 /* XXX: fix this mess */
4211 static void expr_lor_const(void)
4214 while (tok
== TOK_LOR
) {
4221 /* only used if non constant */
4222 static void expr_land(void)
4227 if (tok
== TOK_LAND
) {
4232 if (tok
!= TOK_LAND
) {
4242 static void expr_lor(void)
4247 if (tok
== TOK_LOR
) {
4252 if (tok
!= TOK_LOR
) {
4262 /* XXX: better constant handling */
4263 static void expr_cond(void)
4265 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
;
4267 CType type
, type1
, type2
;
4274 boolean
.t
= VT_BOOL
;
4280 if (tok
!= ':' || !gnu_ext
) {
4295 if (vtop
!= vstack
) {
4296 /* needed to avoid having different registers saved in
4298 if (is_float(vtop
->type
.t
)) {
4300 #ifdef TCC_TARGET_X86_64
4301 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4311 if (tok
== ':' && gnu_ext
) {
4319 sv
= *vtop
; /* save value to handle it later */
4320 vtop
--; /* no vpop so that FP stack is not flushed */
4328 bt1
= t1
& VT_BTYPE
;
4330 bt2
= t2
& VT_BTYPE
;
4331 /* cast operands to correct type according to ISOC rules */
4332 if (is_float(bt1
) || is_float(bt2
)) {
4333 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
4334 type
.t
= VT_LDOUBLE
;
4335 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
4340 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
4341 /* cast to biggest op */
4343 /* convert to unsigned if it does not fit in a long long */
4344 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
4345 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
4346 type
.t
|= VT_UNSIGNED
;
4347 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
4348 /* If one is a null ptr constant the result type
4350 if (is_null_pointer (vtop
))
4352 else if (is_null_pointer (&sv
))
4354 /* XXX: test pointer compatibility, C99 has more elaborate
4358 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
4359 /* XXX: test function pointer compatibility */
4360 type
= bt1
== VT_FUNC
? type1
: type2
;
4361 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
4362 /* XXX: test structure compatibility */
4363 type
= bt1
== VT_STRUCT
? type1
: type2
;
4364 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
4365 /* NOTE: as an extension, we accept void on only one side */
4368 /* integer operations */
4370 /* convert to unsigned if it does not fit in an integer */
4371 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
4372 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
4373 type
.t
|= VT_UNSIGNED
;
4376 /* now we convert second operand */
4378 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4381 if (is_float(type
.t
)) {
4383 #ifdef TCC_TARGET_X86_64
4384 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4388 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
4389 /* for long longs, we use fixed registers to avoid having
4390 to handle a complicated move */
4395 /* this is horrible, but we must also convert first
4399 /* put again first value and cast it */
4402 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4405 move_reg(r2
, r1
, type
.t
);
4412 static void expr_eq(void)
4418 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
4419 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
4420 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
4435 ST_FUNC
void gexpr(void)
4446 /* parse an expression and return its type without any side effect. */
4447 static void expr_type(CType
*type
)
4449 int saved_nocode_wanted
;
4451 saved_nocode_wanted
= nocode_wanted
;
4456 nocode_wanted
= saved_nocode_wanted
;
4459 /* parse a unary expression and return its type without any side
4461 static void unary_type(CType
*type
)
4473 /* parse a constant expression and return value in vtop. */
4474 static void expr_const1(void)
4483 /* parse an integer constant and return its value. */
4484 ST_FUNC
int expr_const(void)
4488 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
4489 expect("constant expression");
4495 /* return the label token if current token is a label, otherwise
4497 static int is_label(void)
4501 /* fast test first */
4502 if (tok
< TOK_UIDENT
)
4504 /* no need to save tokc because tok is an identifier */
4511 unget_tok(last_tok
);
4516 static void label_or_decl(int l
)
4520 /* fast test first */
4521 if (tok
>= TOK_UIDENT
)
4523 /* no need to save tokc because tok is an identifier */
4527 unget_tok(last_tok
);
4530 unget_tok(last_tok
);
4535 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
4536 int case_reg
, int is_expr
)
4539 Sym
*s
, *frame_bottom
;
4541 /* generate line number info */
4542 if (tcc_state
->do_debug
&&
4543 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
4544 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
4546 last_line_num
= file
->line_num
;
4550 /* default return value is (void) */
4552 vtop
->type
.t
= VT_VOID
;
4555 if (tok
== TOK_IF
) {
4562 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4564 if (c
== TOK_ELSE
) {
4568 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4569 gsym(d
); /* patch else jmp */
4572 } else if (tok
== TOK_WHILE
) {
4580 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4584 } else if (tok
== '{') {
4586 int block_vla_sp_loc
, *saved_vla_sp_loc
, saved_vla_flags
;
4589 /* record local declaration stack position */
4591 frame_bottom
= sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4592 frame_bottom
->next
= scope_stack_bottom
;
4593 scope_stack_bottom
= frame_bottom
;
4594 llabel
= local_label_stack
;
4596 /* save VLA state */
4597 block_vla_sp_loc
= *(saved_vla_sp_loc
= vla_sp_loc
);
4598 if (saved_vla_sp_loc
!= &vla_sp_root_loc
)
4599 vla_sp_loc
= &block_vla_sp_loc
;
4601 saved_vla_flags
= vla_flags
;
4602 vla_flags
|= VLA_NEED_NEW_FRAME
;
4604 /* handle local labels declarations */
4605 if (tok
== TOK_LABEL
) {
4608 if (tok
< TOK_UIDENT
)
4609 expect("label identifier");
4610 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
4620 while (tok
!= '}') {
4621 label_or_decl(VT_LOCAL
);
4625 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4628 /* pop locally defined labels */
4629 label_pop(&local_label_stack
, llabel
);
4631 /* XXX: this solution makes only valgrind happy...
4632 triggered by gcc.c-torture/execute/20000917-1.c */
4634 switch(vtop
->type
.t
& VT_BTYPE
) {
4639 for(p
=vtop
->type
.ref
;p
;p
=p
->prev
)
4641 tcc_error("unsupported expression type");
4644 /* pop locally defined symbols */
4645 scope_stack_bottom
= scope_stack_bottom
->next
;
4646 sym_pop(&local_stack
, s
);
4648 /* Pop VLA frames and restore stack pointer if required */
4649 if (saved_vla_sp_loc
!= &vla_sp_root_loc
)
4650 *saved_vla_sp_loc
= block_vla_sp_loc
;
4651 if (vla_sp_loc
!= (saved_vla_sp_loc
== &vla_sp_root_loc
? &vla_sp_root_loc
: &block_vla_sp_loc
)) {
4652 vla_sp_loc
= saved_vla_sp_loc
;
4653 gen_vla_sp_restore(*vla_sp_loc
);
4655 vla_flags
= (vla_flags
& ~VLA_SCOPE_FLAGS
) | (saved_vla_flags
& VLA_SCOPE_FLAGS
);
4658 } else if (tok
== TOK_RETURN
) {
4662 gen_assign_cast(&func_vt
);
4663 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
4664 CType type
, ret_type
;
4665 int ret_align
, ret_nregs
;
4666 ret_nregs
= gfunc_sret(&func_vt
, func_var
, &ret_type
,
4668 if (0 == ret_nregs
) {
4669 /* if returning structure, must copy it to implicit
4670 first pointer arg location */
4673 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
4676 /* copy structure value to pointer */
4679 /* returning structure packed into registers */
4680 int r
, size
, addr
, align
;
4681 size
= type_size(&func_vt
,&align
);
4682 if ((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) || (vtop
->c
.i
& (ret_align
-1)))
4683 && (align
& (ret_align
-1))) {
4684 loc
= (loc
- size
) & -align
;
4687 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
4690 vset(&ret_type
, VT_LOCAL
| VT_LVAL
, addr
);
4692 vtop
->type
= ret_type
;
4693 if (is_float(ret_type
.t
))
4694 r
= rc_fret(ret_type
.t
);
4700 if (--ret_nregs
== 0)
4702 /* We assume that when a structure is returned in multiple
4703 registers, their classes are consecutive values of the
4706 /* XXX: compatible with arm only: ret_align == register_size */
4707 vtop
->c
.i
+= ret_align
;
4708 vtop
->r
= VT_LOCAL
| VT_LVAL
;
4711 } else if (is_float(func_vt
.t
)) {
4712 gv(rc_fret(func_vt
.t
));
4716 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
4719 rsym
= gjmp(rsym
); /* jmp */
4720 } else if (tok
== TOK_BREAK
) {
4723 tcc_error("cannot break");
4724 *bsym
= gjmp(*bsym
);
4727 } else if (tok
== TOK_CONTINUE
) {
4730 tcc_error("cannot continue");
4731 *csym
= gjmp(*csym
);
4734 } else if (tok
== TOK_FOR
) {
4739 frame_bottom
= sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4740 frame_bottom
->next
= scope_stack_bottom
;
4741 scope_stack_bottom
= frame_bottom
;
4743 /* c99 for-loop init decl? */
4744 if (!decl0(VT_LOCAL
, 1)) {
4745 /* no, regular for-loop init expr */
4769 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4773 scope_stack_bottom
= scope_stack_bottom
->next
;
4774 sym_pop(&local_stack
, s
);
4776 if (tok
== TOK_DO
) {
4781 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4792 if (tok
== TOK_SWITCH
) {
4796 /* XXX: other types than integer */
4797 case_reg
= gv(RC_INT
);
4801 b
= gjmp(0); /* jump to first case */
4803 block(&a
, csym
, &b
, &c
, case_reg
, 0);
4804 /* if no default, jmp after switch */
4812 if (tok
== TOK_CASE
) {
4819 if (gnu_ext
&& tok
== TOK_DOTS
) {
4823 tcc_warning("empty case range");
4825 /* since a case is like a label, we must skip it with a jmp */
4832 *case_sym
= gtst(1, 0);
4835 *case_sym
= gtst(1, 0);
4839 *case_sym
= gtst(1, *case_sym
);
4844 goto block_after_label
;
4846 if (tok
== TOK_DEFAULT
) {
4852 tcc_error("too many 'default'");
4855 goto block_after_label
;
4857 if (tok
== TOK_GOTO
) {
4859 if (tok
== '*' && gnu_ext
) {
4863 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
4866 } else if (tok
>= TOK_UIDENT
) {
4867 s
= label_find(tok
);
4868 /* put forward definition if needed */
4870 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4872 if (s
->r
== LABEL_DECLARED
)
4873 s
->r
= LABEL_FORWARD
;
4875 /* label already defined */
4876 if (vla_flags
& VLA_IN_SCOPE
) {
4877 /* If VLAs are in use, save the current stack pointer and
4878 reset the stack pointer to what it was at function entry
4879 (label will restore stack pointer in inner scopes) */
4881 gen_vla_sp_restore(vla_sp_root_loc
);
4883 if (s
->r
& LABEL_FORWARD
)
4884 s
->jnext
= gjmp(s
->jnext
);
4886 gjmp_addr(s
->jnext
);
4889 expect("label identifier");
4892 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
4898 if (vla_flags
& VLA_IN_SCOPE
) {
4899 /* save/restore stack pointer across label
4900 this is a no-op when combined with the load immediately
4901 after the label unless we arrive via goto */
4906 if (s
->r
== LABEL_DEFINED
)
4907 tcc_error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
4909 s
->r
= LABEL_DEFINED
;
4911 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
4914 if (vla_flags
& VLA_IN_SCOPE
) {
4915 gen_vla_sp_restore(*vla_sp_loc
);
4916 vla_flags
|= VLA_NEED_NEW_FRAME
;
4918 /* we accept this, but it is a mistake */
4921 tcc_warning("deprecated use of label at end of compound statement");
4925 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4928 /* expression case */
4943 /* t is the array or struct type. c is the array or struct
4944 address. cur_index/cur_field is the pointer to the current
4945 value. 'size_only' is true if only size info is needed (only used
4947 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
4948 int *cur_index
, Sym
**cur_field
,
4952 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
4958 if (gnu_ext
&& (l
= is_label()) != 0)
4960 while (tok
== '[' || tok
== '.') {
4962 if (!(type
->t
& VT_ARRAY
))
4963 expect("array type");
4966 index
= expr_const();
4967 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
4968 expect("invalid index");
4969 if (tok
== TOK_DOTS
&& gnu_ext
) {
4971 index_last
= expr_const();
4972 if (index_last
< 0 ||
4973 (s
->c
>= 0 && index_last
>= s
->c
) ||
4975 expect("invalid index");
4981 *cur_index
= index_last
;
4982 type
= pointed_type(type
);
4983 elem_size
= type_size(type
, &align
);
4984 c
+= index
* elem_size
;
4985 /* NOTE: we only support ranges for last designator */
4986 nb_elems
= index_last
- index
+ 1;
4987 if (nb_elems
!= 1) {
4996 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
4997 expect("struct/union type");
5010 /* XXX: fix this mess by using explicit storage field */
5012 type1
.t
|= (type
->t
& ~VT_TYPE
);
5026 if (type
->t
& VT_ARRAY
) {
5028 type
= pointed_type(type
);
5029 c
+= index
* type_size(type
, &align
);
5033 tcc_error("too many field init");
5034 /* XXX: fix this mess by using explicit storage field */
5036 type1
.t
|= (type
->t
& ~VT_TYPE
);
5041 decl_initializer(type
, sec
, c
, 0, size_only
);
5043 /* XXX: make it more general */
5044 if (!size_only
&& nb_elems
> 1) {
5045 unsigned long c_end
;
5050 tcc_error("range init not supported yet for dynamic storage");
5051 c_end
= c
+ nb_elems
* elem_size
;
5052 if (c_end
> sec
->data_allocated
)
5053 section_realloc(sec
, c_end
);
5054 src
= sec
->data
+ c
;
5056 for(i
= 1; i
< nb_elems
; i
++) {
5058 memcpy(dst
, src
, elem_size
);
5064 #define EXPR_CONST 1
5067 /* store a value or an expression directly in global data or in local array */
5068 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
5069 int v
, int expr_type
)
5071 int saved_global_expr
, bt
, bit_pos
, bit_size
;
5073 unsigned long long bit_mask
;
5081 /* compound literals must be allocated globally in this case */
5082 saved_global_expr
= global_expr
;
5085 global_expr
= saved_global_expr
;
5086 /* NOTE: symbols are accepted */
5087 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
5088 tcc_error("initializer element is not constant");
5096 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
5099 /* XXX: not portable */
5100 /* XXX: generate error if incorrect relocation */
5101 gen_assign_cast(&dtype
);
5102 bt
= type
->t
& VT_BTYPE
;
5103 /* we'll write at most 12 bytes */
5104 if (c
+ 12 > sec
->data_allocated
) {
5105 section_realloc(sec
, c
+ 12);
5107 ptr
= sec
->data
+ c
;
5108 /* XXX: make code faster ? */
5109 if (!(type
->t
& VT_BITFIELD
)) {
5114 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5115 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
5116 bit_mask
= (1LL << bit_size
) - 1;
5118 if ((vtop
->r
& VT_SYM
) &&
5124 (bt
== VT_INT
&& bit_size
!= 32)))
5125 tcc_error("initializer element is not computable at load time");
5128 vtop
->c
.i
= (vtop
->c
.i
!= 0);
5130 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5133 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5136 *(double *)ptr
= vtop
->c
.d
;
5139 *(long double *)ptr
= vtop
->c
.ld
;
5142 *(long long *)ptr
|= (vtop
->c
.ll
& bit_mask
) << bit_pos
;
5145 if (vtop
->r
& VT_SYM
) {
5146 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
5148 *(addr_t
*)ptr
|= (vtop
->c
.ptr_offset
& bit_mask
) << bit_pos
;
5151 if (vtop
->r
& VT_SYM
) {
5152 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
5154 *(int *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5159 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
5166 /* put zeros for variable based init */
5167 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
5170 /* nothing to do because globals are already set to zero */
5172 vpush_global_sym(&func_old_type
, TOK_memset
);
5174 #ifdef TCC_TARGET_ARM
5185 /* 't' contains the type and storage info. 'c' is the offset of the
5186 object in section 'sec'. If 'sec' is NULL, it means stack based
5187 allocation. 'first' is true if array '{' must be read (multi
5188 dimension implicit array init handling). 'size_only' is true if
5189 size only evaluation is wanted (only for arrays). */
5190 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
5191 int first
, int size_only
)
5193 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, parlevel1
, i
;
5194 int size1
, align1
, expr_type
;
5198 if (type
->t
& VT_VLA
) {
5201 /* save current stack pointer */
5202 if (vla_flags
& VLA_NEED_NEW_FRAME
) {
5204 vla_flags
= VLA_IN_SCOPE
;
5205 vla_sp_loc
= &vla_sp_loc_tmp
;
5208 vla_runtime_type_size(type
, &a
);
5209 gen_vla_alloc(type
, a
);
5210 vset(type
, VT_LOCAL
|VT_LVAL
, c
);
5214 } else if (type
->t
& VT_ARRAY
) {
5218 t1
= pointed_type(type
);
5219 size1
= type_size(t1
, &align1
);
5222 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
5225 tcc_error("character array initializer must be a literal,"
5226 " optionally enclosed in braces");
5231 /* only parse strings here if correct type (otherwise: handle
5232 them as ((w)char *) expressions */
5233 if ((tok
== TOK_LSTR
&&
5234 #ifdef TCC_TARGET_PE
5235 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
5237 (t1
->t
& VT_BTYPE
) == VT_INT
5239 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
5240 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5245 /* compute maximum number of chars wanted */
5247 cstr_len
= cstr
->size
;
5249 cstr_len
= cstr
->size
/ sizeof(nwchar_t
);
5252 if (n
>= 0 && nb
> (n
- array_length
))
5253 nb
= n
- array_length
;
5256 tcc_warning("initializer-string for array is too long");
5257 /* in order to go faster for common case (char
5258 string in global variable, we handle it
5260 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
5261 memcpy(sec
->data
+ c
+ array_length
, cstr
->data
, nb
);
5265 ch
= ((unsigned char *)cstr
->data
)[i
];
5267 ch
= ((nwchar_t
*)cstr
->data
)[i
];
5268 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
5276 /* only add trailing zero if enough storage (no
5277 warning in this case since it is standard) */
5278 if (n
< 0 || array_length
< n
) {
5280 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
5286 while (tok
!= '}') {
5287 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
5288 if (n
>= 0 && index
>= n
)
5289 tcc_error("index too large");
5290 /* must put zero in holes (note that doing it that way
5291 ensures that it even works with designators) */
5292 if (!size_only
&& array_length
< index
) {
5293 init_putz(t1
, sec
, c
+ array_length
* size1
,
5294 (index
- array_length
) * size1
);
5297 if (index
> array_length
)
5298 array_length
= index
;
5299 /* special test for multi dimensional arrays (may not
5300 be strictly correct if designators are used at the
5302 if (index
>= n
&& no_oblock
)
5311 /* put zeros at the end */
5312 if (!size_only
&& n
>= 0 && array_length
< n
) {
5313 init_putz(t1
, sec
, c
+ array_length
* size1
,
5314 (n
- array_length
) * size1
);
5316 /* patch type size if needed */
5318 s
->c
= array_length
;
5319 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
5320 (sec
|| !first
|| tok
== '{')) {
5323 /* NOTE: the previous test is a specific case for automatic
5324 struct/union init */
5325 /* XXX: union needs only one init */
5327 /* XXX: this test is incorrect for local initializers
5328 beginning with ( without {. It would be much more difficult
5329 to do it correctly (ideally, the expression parser should
5330 be used in all cases) */
5336 while (tok
== '(') {
5340 if (!parse_btype(&type1
, &ad1
))
5342 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
5344 if (!is_assignable_types(type
, &type1
))
5345 tcc_error("invalid type for cast");
5350 if (first
|| tok
== '{') {
5359 while (tok
!= '}') {
5360 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
5362 if (!size_only
&& array_length
< index
) {
5363 init_putz(type
, sec
, c
+ array_length
,
5364 index
- array_length
);
5366 index
= index
+ type_size(&f
->type
, &align1
);
5367 if (index
> array_length
)
5368 array_length
= index
;
5370 /* gr: skip fields from same union - ugly. */
5372 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
5373 /* test for same offset */
5374 if (f
->next
->c
!= f
->c
)
5376 /* if yes, test for bitfield shift */
5377 if ((f
->type
.t
& VT_BITFIELD
) && (f
->next
->type
.t
& VT_BITFIELD
)) {
5378 int bit_pos_1
= (f
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5379 int bit_pos_2
= (f
->next
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5380 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
5381 if (bit_pos_1
!= bit_pos_2
)
5388 if (no_oblock
&& f
== NULL
)
5394 /* put zeros at the end */
5395 if (!size_only
&& array_length
< n
) {
5396 init_putz(type
, sec
, c
+ array_length
,
5405 } else if (tok
== '{') {
5407 decl_initializer(type
, sec
, c
, first
, size_only
);
5409 } else if (size_only
) {
5410 /* just skip expression */
5411 parlevel
= parlevel1
= 0;
5412 while ((parlevel
> 0 || parlevel1
> 0 ||
5413 (tok
!= '}' && tok
!= ',')) && tok
!= -1) {
5416 else if (tok
== ')')
5418 else if (tok
== '{')
5420 else if (tok
== '}')
5425 /* currently, we always use constant expression for globals
5426 (may change for scripting case) */
5427 expr_type
= EXPR_CONST
;
5429 expr_type
= EXPR_ANY
;
5430 init_putv(type
, sec
, c
, 0, expr_type
);
5434 /* parse an initializer for type 't' if 'has_init' is non zero, and
5435 allocate space in local or global data space ('r' is either
5436 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5437 variable 'v' with an associated name represented by 'asm_label' of
5438 scope 'scope' is declared before initializers are parsed. If 'v' is
5439 zero, then a reference to the new object is put in the value stack.
5440 If 'has_init' is 2, a special parsing is done to handle string
5442 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
5443 int has_init
, int v
, char *asm_label
,
5446 int size
, align
, addr
, data_offset
;
5448 ParseState saved_parse_state
= {0};
5449 TokenString init_str
;
5451 Sym
*flexible_array
;
5453 flexible_array
= NULL
;
5454 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
5455 Sym
*field
= type
->ref
->next
;
5458 field
= field
->next
;
5459 if (field
->type
.t
& VT_ARRAY
&& field
->type
.ref
->c
< 0)
5460 flexible_array
= field
;
5464 size
= type_size(type
, &align
);
5465 /* If unknown size, we must evaluate it before
5466 evaluating initializers because
5467 initializers can generate global data too
5468 (e.g. string pointers or ISOC99 compound
5469 literals). It also simplifies local
5470 initializers handling */
5471 tok_str_new(&init_str
);
5472 if (size
< 0 || (flexible_array
&& has_init
)) {
5474 tcc_error("unknown type size");
5475 /* get all init string */
5476 if (has_init
== 2) {
5477 /* only get strings */
5478 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5479 tok_str_add_tok(&init_str
);
5484 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
5486 tcc_error("unexpected end of file in initializer");
5487 tok_str_add_tok(&init_str
);
5490 else if (tok
== '}') {
5500 tok_str_add(&init_str
, -1);
5501 tok_str_add(&init_str
, 0);
5504 save_parse_state(&saved_parse_state
);
5506 macro_ptr
= init_str
.str
;
5508 decl_initializer(type
, NULL
, 0, 1, 1);
5509 /* prepare second initializer parsing */
5510 macro_ptr
= init_str
.str
;
5513 /* if still unknown size, error */
5514 size
= type_size(type
, &align
);
5516 tcc_error("unknown type size");
5519 size
+= flexible_array
->type
.ref
->c
* pointed_size(&flexible_array
->type
);
5520 /* take into account specified alignment if bigger */
5521 if (ad
->a
.aligned
) {
5522 if (ad
->a
.aligned
> align
)
5523 align
= ad
->a
.aligned
;
5524 } else if (ad
->a
.packed
) {
5527 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
5529 #ifdef CONFIG_TCC_BCHECK
5530 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5534 loc
= (loc
- size
) & -align
;
5536 #ifdef CONFIG_TCC_BCHECK
5537 /* handles bounds */
5538 /* XXX: currently, since we do only one pass, we cannot track
5539 '&' operators, so we add only arrays */
5540 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5541 unsigned long *bounds_ptr
;
5542 /* add padding between regions */
5544 /* then add local bound info */
5545 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(unsigned long));
5546 bounds_ptr
[0] = addr
;
5547 bounds_ptr
[1] = size
;
5551 /* local variable */
5552 sym_push(v
, type
, r
, addr
);
5554 /* push local reference */
5555 vset(type
, r
, addr
);
5561 if (v
&& scope
== VT_CONST
) {
5562 /* see if the symbol was already defined */
5565 if (!is_compatible_types(&sym
->type
, type
))
5566 tcc_error("incompatible types for redefinition of '%s'",
5567 get_tok_str(v
, NULL
));
5568 if (sym
->type
.t
& VT_EXTERN
) {
5569 /* if the variable is extern, it was not allocated */
5570 sym
->type
.t
&= ~VT_EXTERN
;
5571 /* set array size if it was ommited in extern
5573 if ((sym
->type
.t
& VT_ARRAY
) &&
5574 sym
->type
.ref
->c
< 0 &&
5576 sym
->type
.ref
->c
= type
->ref
->c
;
5578 /* we accept several definitions of the same
5579 global variable. this is tricky, because we
5580 must play with the SHN_COMMON type of the symbol */
5581 /* XXX: should check if the variable was already
5582 initialized. It is incorrect to initialized it
5584 /* no init data, we won't add more to the symbol */
5591 /* allocate symbol in corresponding section */
5596 else if (tcc_state
->nocommon
)
5600 data_offset
= sec
->data_offset
;
5601 data_offset
= (data_offset
+ align
- 1) & -align
;
5603 /* very important to increment global pointer at this time
5604 because initializers themselves can create new initializers */
5605 data_offset
+= size
;
5606 #ifdef CONFIG_TCC_BCHECK
5607 /* add padding if bound check */
5608 if (tcc_state
->do_bounds_check
)
5611 sec
->data_offset
= data_offset
;
5612 /* allocate section space to put the data */
5613 if (sec
->sh_type
!= SHT_NOBITS
&&
5614 data_offset
> sec
->data_allocated
)
5615 section_realloc(sec
, data_offset
);
5616 /* align section if needed */
5617 if (align
> sec
->sh_addralign
)
5618 sec
->sh_addralign
= align
;
5620 addr
= 0; /* avoid warning */
5624 if (scope
!= VT_CONST
|| !sym
) {
5625 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
5626 sym
->asm_label
= asm_label
;
5628 /* update symbol definition */
5630 put_extern_sym(sym
, sec
, addr
, size
);
5633 /* put a common area */
5634 put_extern_sym(sym
, NULL
, align
, size
);
5635 /* XXX: find a nicer way */
5636 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
5637 esym
->st_shndx
= SHN_COMMON
;
5640 /* push global reference */
5641 sym
= get_sym_ref(type
, sec
, addr
, size
);
5642 vpushsym(type
, sym
);
5644 /* patch symbol weakness */
5645 if (type
->t
& VT_WEAK
)
5647 #ifdef CONFIG_TCC_BCHECK
5648 /* handles bounds now because the symbol must be defined
5649 before for the relocation */
5650 if (tcc_state
->do_bounds_check
) {
5651 unsigned long *bounds_ptr
;
5653 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
5654 /* then add global bound info */
5655 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(long));
5656 bounds_ptr
[0] = 0; /* relocated */
5657 bounds_ptr
[1] = size
;
5661 if (has_init
|| (type
->t
& VT_VLA
)) {
5662 decl_initializer(type
, sec
, addr
, 1, 0);
5663 /* restore parse state if needed */
5665 tok_str_free(init_str
.str
);
5666 restore_parse_state(&saved_parse_state
);
5668 /* patch flexible array member size back to -1, */
5669 /* for possible subsequent similar declarations */
5671 flexible_array
->type
.ref
->c
= -1;
5676 static void put_func_debug(Sym
*sym
)
5681 /* XXX: we put here a dummy type */
5682 snprintf(buf
, sizeof(buf
), "%s:%c1",
5683 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
5684 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
5685 cur_text_section
, sym
->c
);
5686 /* //gr gdb wants a line at the function */
5687 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
5692 /* parse an old style function declaration list */
5693 /* XXX: check multiple parameter */
5694 static void func_decl_list(Sym
*func_sym
)
5701 /* parse each declaration */
5702 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
&&
5703 tok
!= TOK_ASM1
&& tok
!= TOK_ASM2
&& tok
!= TOK_ASM3
) {
5704 if (!parse_btype(&btype
, &ad
))
5705 expect("declaration list");
5706 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5707 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5709 /* we accept no variable after */
5713 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5714 /* find parameter in function parameter list */
5717 if ((s
->v
& ~SYM_FIELD
) == v
)
5721 tcc_error("declaration for parameter '%s' but no such parameter",
5722 get_tok_str(v
, NULL
));
5724 /* check that no storage specifier except 'register' was given */
5725 if (type
.t
& VT_STORAGE
)
5726 tcc_error("storage class specified for '%s'", get_tok_str(v
, NULL
));
5727 convert_parameter_type(&type
);
5728 /* we can add the type (NOTE: it could be local to the function) */
5730 /* accept other parameters */
5741 /* parse a function defined by symbol 'sym' and generate its code in
5742 'cur_text_section' */
5743 static void gen_function(Sym
*sym
)
5745 int saved_nocode_wanted
= nocode_wanted
;
5747 ind
= cur_text_section
->data_offset
;
5748 /* NOTE: we patch the symbol size later */
5749 put_extern_sym(sym
, cur_text_section
, ind
, 0);
5750 funcname
= get_tok_str(sym
->v
, NULL
);
5752 /* Initialize VLA state */
5753 vla_sp_loc
= &vla_sp_root_loc
;
5754 vla_flags
= VLA_NEED_NEW_FRAME
;
5755 /* put debug symbol */
5756 if (tcc_state
->do_debug
)
5757 put_func_debug(sym
);
5758 /* push a dummy symbol to enable local sym storage */
5759 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
5760 gfunc_prolog(&sym
->type
);
5761 #ifdef CONFIG_TCC_BCHECK
5762 if (tcc_state
->do_bounds_check
5763 && !strcmp(get_tok_str(sym
->v
, NULL
), "main")) {
5767 for (i
= 0, sym
= local_stack
; i
< 2; i
++, sym
= sym
->prev
) {
5768 if (sym
->v
& SYM_FIELD
|| sym
->prev
->v
& SYM_FIELD
)
5770 vpush_global_sym(&func_old_type
, TOK___bound_main_arg
);
5771 vset(&sym
->type
, sym
->r
, sym
->c
);
5777 block(NULL
, NULL
, NULL
, NULL
, 0, 0);
5780 cur_text_section
->data_offset
= ind
;
5781 label_pop(&global_label_stack
, NULL
);
5782 /* reset local stack */
5783 scope_stack_bottom
= NULL
;
5784 sym_pop(&local_stack
, NULL
);
5785 /* end of function */
5786 /* patch symbol size */
5787 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
5789 /* patch symbol weakness (this definition overrules any prototype) */
5790 if (sym
->type
.t
& VT_WEAK
)
5792 if (tcc_state
->do_debug
) {
5793 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
5795 /* It's better to crash than to generate wrong code */
5796 cur_text_section
= NULL
;
5797 funcname
= ""; /* for safety */
5798 func_vt
.t
= VT_VOID
; /* for safety */
5799 func_var
= 0; /* for safety */
5800 ind
= 0; /* for safety */
5801 nocode_wanted
= saved_nocode_wanted
;
5804 ST_FUNC
void gen_inline_functions(void)
5807 int *str
, inline_generated
, i
;
5808 struct InlineFunc
*fn
;
5810 /* iterate while inline function are referenced */
5812 inline_generated
= 0;
5813 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5814 fn
= tcc_state
->inline_fns
[i
];
5816 if (sym
&& sym
->c
) {
5817 /* the function was used: generate its code and
5818 convert it to a normal function */
5819 str
= fn
->token_str
;
5822 pstrcpy(file
->filename
, sizeof file
->filename
, fn
->filename
);
5823 sym
->r
= VT_SYM
| VT_CONST
;
5824 sym
->type
.t
&= ~VT_INLINE
;
5828 cur_text_section
= text_section
;
5830 macro_ptr
= NULL
; /* fail safe */
5832 inline_generated
= 1;
5835 if (!inline_generated
)
5838 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5839 fn
= tcc_state
->inline_fns
[i
];
5840 str
= fn
->token_str
;
5843 dynarray_reset(&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
);
5846 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
5847 static int decl0(int l
, int is_for_loop_init
)
5855 if (!parse_btype(&btype
, &ad
)) {
5856 if (is_for_loop_init
)
5858 /* skip redundant ';' */
5859 /* XXX: find more elegant solution */
5864 if (l
== VT_CONST
&&
5865 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5866 /* global asm block */
5870 /* special test for old K&R protos without explicit int
5871 type. Only accepted when defining global data */
5872 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
5876 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5877 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5879 /* we accept no variable after */
5883 while (1) { /* iterate thru each declaration */
5884 char *asm_label
; // associated asm label
5886 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5890 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
5891 printf("type = '%s'\n", buf
);
5894 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5895 if ((type
.t
& VT_STATIC
) && (l
== VT_LOCAL
)) {
5896 tcc_error("function without file scope cannot be static");
5898 /* if old style function prototype, we accept a
5901 if (sym
->c
== FUNC_OLD
)
5902 func_decl_list(sym
);
5906 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5909 asm_label_instr(&astr
);
5910 asm_label
= tcc_strdup(astr
.data
);
5913 /* parse one last attribute list, after asm label */
5914 parse_attribute(&ad
);
5919 #ifdef TCC_TARGET_PE
5920 if (ad
.a
.func_import
)
5921 type
.t
|= VT_IMPORT
;
5922 if (ad
.a
.func_export
)
5923 type
.t
|= VT_EXPORT
;
5927 tcc_error("cannot use local functions");
5928 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
5929 expect("function definition");
5931 /* reject abstract declarators in function definition */
5933 while ((sym
= sym
->next
) != NULL
)
5934 if (!(sym
->v
& ~SYM_FIELD
))
5935 expect("identifier");
5937 /* XXX: cannot do better now: convert extern line to static inline */
5938 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
5939 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5944 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
5947 ref
= sym
->type
.ref
;
5948 if (0 == ref
->a
.func_proto
)
5949 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
5951 /* use func_call from prototype if not defined */
5952 if (ref
->a
.func_call
!= FUNC_CDECL
5953 && type
.ref
->a
.func_call
== FUNC_CDECL
)
5954 type
.ref
->a
.func_call
= ref
->a
.func_call
;
5956 /* use export from prototype */
5957 if (ref
->a
.func_export
)
5958 type
.ref
->a
.func_export
= 1;
5960 /* use static from prototype */
5961 if (sym
->type
.t
& VT_STATIC
)
5962 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5964 if (!is_compatible_types(&sym
->type
, &type
)) {
5966 tcc_error("incompatible types for redefinition of '%s'",
5967 get_tok_str(v
, NULL
));
5969 type
.ref
->a
.func_proto
= 0;
5970 /* if symbol is already defined, then put complete type */
5973 /* put function symbol */
5974 sym
= global_identifier_push(v
, type
.t
, 0);
5975 sym
->type
.ref
= type
.ref
;
5978 /* static inline functions are just recorded as a kind
5979 of macro. Their code will be emitted at the end of
5980 the compilation unit only if they are used */
5981 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
5982 (VT_INLINE
| VT_STATIC
)) {
5983 TokenString func_str
;
5985 struct InlineFunc
*fn
;
5986 const char *filename
;
5988 tok_str_new(&func_str
);
5994 tcc_error("unexpected end of file");
5995 tok_str_add_tok(&func_str
);
6000 } else if (t
== '}') {
6002 if (block_level
== 0)
6006 tok_str_add(&func_str
, -1);
6007 tok_str_add(&func_str
, 0);
6008 filename
= file
? file
->filename
: "";
6009 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
6010 strcpy(fn
->filename
, filename
);
6012 fn
->token_str
= func_str
.str
;
6013 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
6016 /* compute text section */
6017 cur_text_section
= ad
.section
;
6018 if (!cur_text_section
)
6019 cur_text_section
= text_section
;
6020 sym
->r
= VT_SYM
| VT_CONST
;
6025 if (btype
.t
& VT_TYPEDEF
) {
6026 /* save typedefed type */
6027 /* XXX: test storage specifiers ? */
6028 sym
= sym_push(v
, &type
, 0, 0);
6030 sym
->type
.t
|= VT_TYPEDEF
;
6033 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
6034 /* external function definition */
6035 /* specific case for func_call attribute */
6036 ad
.a
.func_proto
= 1;
6038 } else if (!(type
.t
& VT_ARRAY
)) {
6039 /* not lvalue if array */
6040 r
|= lvalue_type(type
.t
);
6042 has_init
= (tok
== '=');
6043 if (has_init
&& (type
.t
& VT_VLA
))
6044 tcc_error("Variable length array cannot be initialized");
6045 if ((btype
.t
& VT_EXTERN
) || ((type
.t
& VT_BTYPE
) == VT_FUNC
) ||
6046 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
6047 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
6048 /* external variable or function */
6049 /* NOTE: as GCC, uninitialized global static
6050 arrays of null size are considered as
6052 sym
= external_sym(v
, &type
, r
, asm_label
);
6054 if (type
.t
& VT_WEAK
)
6057 if (ad
.alias_target
) {
6062 alias_target
= sym_find(ad
.alias_target
);
6063 if (!alias_target
|| !alias_target
->c
)
6064 tcc_error("unsupported forward __alias__ attribute");
6065 esym
= &((Elf32_Sym
*)symtab_section
->data
)[alias_target
->c
];
6066 tsec
.sh_num
= esym
->st_shndx
;
6067 put_extern_sym2(sym
, &tsec
, esym
->st_value
, esym
->st_size
, 0);
6070 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
6071 if (type
.t
& VT_STATIC
)
6077 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, asm_label
, l
);
6081 if (is_for_loop_init
)
6094 ST_FUNC
void decl(int l
)