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
)
195 /* structure lookup */
196 ST_INLN Sym
*struct_find(int v
)
199 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
201 return table_ident
[v
]->sym_struct
;
204 /* find an identifier */
205 ST_INLN Sym
*sym_find(int v
)
208 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
210 return table_ident
[v
]->sym_identifier
;
213 /* push a given symbol on the symbol stack */
214 ST_FUNC Sym
*sym_push(int v
, CType
*type
, int r
, int c
)
223 s
= sym_push2(ps
, v
, type
->t
, c
);
224 s
->type
.ref
= type
->ref
;
226 /* don't record fields or anonymous symbols */
228 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
229 /* record symbol in token array */
230 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
232 ps
= &ts
->sym_struct
;
234 ps
= &ts
->sym_identifier
;
241 /* push a global identifier */
242 ST_FUNC Sym
*global_identifier_push(int v
, int t
, int c
)
245 s
= sym_push2(&global_stack
, v
, t
, c
);
246 /* don't record anonymous symbol */
247 if (v
< SYM_FIRST_ANOM
) {
248 ps
= &table_ident
[v
- TOK_IDENT
]->sym_identifier
;
249 /* modify the top most local identifier, so that
250 sym_identifier will point to 's' when popped */
252 ps
= &(*ps
)->prev_tok
;
259 /* pop symbols until top reaches 'b' */
260 ST_FUNC
void sym_pop(Sym
**ptop
, Sym
*b
)
270 /* remove symbol in token array */
272 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
273 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
275 ps
= &ts
->sym_struct
;
277 ps
= &ts
->sym_identifier
;
286 static void weaken_symbol(Sym
*sym
)
288 sym
->type
.t
|= VT_WEAK
;
293 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
294 esym_type
= ELFW(ST_TYPE
)(esym
->st_info
);
295 esym
->st_info
= ELFW(ST_INFO
)(STB_WEAK
, esym_type
);
299 /* ------------------------------------------------------------------------- */
301 ST_FUNC
void swap(int *p
, int *q
)
309 static void vsetc(CType
*type
, int r
, CValue
*vc
)
313 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
314 tcc_error("memory full (vstack)");
315 /* cannot let cpu flags if other instruction are generated. Also
316 avoid leaving VT_JMP anywhere except on the top of the stack
317 because it would complicate the code generator. */
318 if (vtop
>= vstack
) {
319 v
= vtop
->r
& VT_VALMASK
;
320 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
330 /* push constant of type "type" with useless value */
331 void vpush(CType
*type
)
334 vsetc(type
, VT_CONST
, &cval
);
337 /* push integer constant */
338 ST_FUNC
void vpushi(int v
)
342 vsetc(&int_type
, VT_CONST
, &cval
);
345 /* push a pointer sized constant */
346 static void vpushs(addr_t v
)
350 vsetc(&size_type
, VT_CONST
, &cval
);
353 /* push arbitrary 64bit constant */
354 void vpush64(int ty
, unsigned long long v
)
361 vsetc(&ctype
, VT_CONST
, &cval
);
364 /* push long long constant */
365 static inline void vpushll(long long v
)
367 vpush64(VT_LLONG
, v
);
370 /* push a symbol value of TYPE */
371 static inline void vpushsym(CType
*type
, Sym
*sym
)
375 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
379 /* Return a static symbol pointing to a section */
380 ST_FUNC Sym
*get_sym_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
386 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
387 sym
->type
.ref
= type
->ref
;
388 sym
->r
= VT_CONST
| VT_SYM
;
389 put_extern_sym(sym
, sec
, offset
, size
);
393 /* push a reference to a section offset by adding a dummy symbol */
394 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
396 vpushsym(type
, get_sym_ref(type
, sec
, offset
, size
));
399 /* define a new external reference to a symbol 'v' of type 'u' */
400 ST_FUNC Sym
*external_global_sym(int v
, CType
*type
, int r
)
406 /* push forward reference */
407 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
408 s
->type
.ref
= type
->ref
;
409 s
->r
= r
| VT_CONST
| VT_SYM
;
414 /* define a new external reference to a symbol 'v' with alternate asm
415 name 'asm_label' of type 'u'. 'asm_label' is equal to NULL if there
416 is no alternate name (most cases) */
417 static Sym
*external_sym(int v
, CType
*type
, int r
, char *asm_label
)
423 /* push forward reference */
424 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
425 s
->asm_label
= asm_label
;
426 s
->type
.t
|= VT_EXTERN
;
427 } else if (s
->type
.ref
== func_old_type
.ref
) {
428 s
->type
.ref
= type
->ref
;
429 s
->r
= r
| VT_CONST
| VT_SYM
;
430 s
->type
.t
|= VT_EXTERN
;
431 } else if (!is_compatible_types(&s
->type
, type
)) {
432 tcc_error("incompatible types for redefinition of '%s'",
433 get_tok_str(v
, NULL
));
438 /* push a reference to global symbol v */
439 ST_FUNC
void vpush_global_sym(CType
*type
, int v
)
441 vpushsym(type
, external_global_sym(v
, type
, 0));
444 ST_FUNC
void vset(CType
*type
, int r
, int v
)
449 vsetc(type
, r
, &cval
);
452 static void vseti(int r
, int v
)
460 ST_FUNC
void vswap(void)
463 /* cannot let cpu flags if other instruction are generated. Also
464 avoid leaving VT_JMP anywhere except on the top of the stack
465 because it would complicate the code generator. */
466 if (vtop
>= vstack
) {
467 int v
= vtop
->r
& VT_VALMASK
;
468 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
475 /* XXX: +2% overall speed possible with optimized memswap
477 * memswap(&vtop[0], &vtop[1], sizeof *vtop);
481 ST_FUNC
void vpushv(SValue
*v
)
483 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
484 tcc_error("memory full (vstack)");
489 static void vdup(void)
494 /* save r to the memory stack, and mark it as being free */
495 ST_FUNC
void save_reg(int r
)
497 int l
, saved
, size
, align
;
501 /* modify all stack values */
504 for(p
=vstack
;p
<=vtop
;p
++) {
505 if ((p
->r
& VT_VALMASK
) == r
||
506 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
507 /* must save value on stack if not already done */
509 /* NOTE: must reload 'r' because r might be equal to r2 */
510 r
= p
->r
& VT_VALMASK
;
511 /* store register in the stack */
513 if ((p
->r
& VT_LVAL
) ||
514 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
515 #ifdef TCC_TARGET_X86_64
516 type
= &char_pointer_type
;
520 size
= type_size(type
, &align
);
521 loc
= (loc
- size
) & -align
;
523 sv
.r
= VT_LOCAL
| VT_LVAL
;
526 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
527 /* x86 specific: need to pop fp register ST0 if saved */
529 o(0xd8dd); /* fstp %st(0) */
532 #ifndef TCC_TARGET_X86_64
533 /* special long long case */
534 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
542 /* mark that stack entry as being saved on the stack */
543 if (p
->r
& VT_LVAL
) {
544 /* also clear the bounded flag because the
545 relocation address of the function was stored in
547 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
549 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
557 #ifdef TCC_TARGET_ARM
558 /* find a register of class 'rc2' with at most one reference on stack.
559 * If none, call get_reg(rc) */
560 ST_FUNC
int get_reg_ex(int rc
, int rc2
)
565 for(r
=0;r
<NB_REGS
;r
++) {
566 if (reg_classes
[r
] & rc2
) {
569 for(p
= vstack
; p
<= vtop
; p
++) {
570 if ((p
->r
& VT_VALMASK
) == r
||
571 (p
->r2
& VT_VALMASK
) == r
)
582 /* find a free register of class 'rc'. If none, save one register */
583 ST_FUNC
int get_reg(int rc
)
588 /* find a free register */
589 for(r
=0;r
<NB_REGS
;r
++) {
590 if (reg_classes
[r
] & rc
) {
591 for(p
=vstack
;p
<=vtop
;p
++) {
592 if ((p
->r
& VT_VALMASK
) == r
||
593 (p
->r2
& VT_VALMASK
) == r
)
601 /* no register left : free the first one on the stack (VERY
602 IMPORTANT to start from the bottom to ensure that we don't
603 spill registers used in gen_opi()) */
604 for(p
=vstack
;p
<=vtop
;p
++) {
605 /* look at second register (if long long) */
606 r
= p
->r2
& VT_VALMASK
;
607 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
609 r
= p
->r
& VT_VALMASK
;
610 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
616 /* Should never comes here */
620 /* save registers up to (vtop - n) stack entry */
621 ST_FUNC
void save_regs(int n
)
626 for(p
= vstack
;p
<= p1
; p
++) {
627 r
= p
->r
& VT_VALMASK
;
634 /* move register 's' (of type 't') to 'r', and flush previous value of r to memory
636 static void move_reg(int r
, int s
, int t
)
650 /* get address of vtop (vtop MUST BE an lvalue) */
651 static void gaddrof(void)
653 if (vtop
->r
& VT_REF
)
656 /* tricky: if saved lvalue, then we can go back to lvalue */
657 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
658 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
663 #ifdef CONFIG_TCC_BCHECK
664 /* generate lvalue bound code */
665 static void gbound(void)
670 vtop
->r
&= ~VT_MUSTBOUND
;
671 /* if lvalue, then use checking code before dereferencing */
672 if (vtop
->r
& VT_LVAL
) {
673 /* if not VT_BOUNDED value, then make one */
674 if (!(vtop
->r
& VT_BOUNDED
)) {
675 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
676 /* must save type because we must set it to int to get pointer */
678 vtop
->type
.t
= VT_INT
;
681 gen_bounded_ptr_add();
682 vtop
->r
|= lval_type
;
685 /* then check for dereferencing */
686 gen_bounded_ptr_deref();
691 /* store vtop a register belonging to class 'rc'. lvalues are
692 converted to values. Cannot be used if cannot be converted to
693 register value (such as structures). */
694 ST_FUNC
int gv(int rc
)
696 int r
, bit_pos
, bit_size
, size
, align
, i
;
699 /* NOTE: get_reg can modify vstack[] */
700 if (vtop
->type
.t
& VT_BITFIELD
) {
703 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
704 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
705 /* remove bit field info to avoid loops */
706 vtop
->type
.t
&= ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
707 /* cast to int to propagate signedness in following ops */
708 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
713 if((vtop
->type
.t
& VT_UNSIGNED
) ||
714 (vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
715 type
.t
|= VT_UNSIGNED
;
717 /* generate shifts */
718 vpushi(bits
- (bit_pos
+ bit_size
));
720 vpushi(bits
- bit_size
);
721 /* NOTE: transformed to SHR if unsigned */
725 if (is_float(vtop
->type
.t
) &&
726 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
729 unsigned long offset
;
730 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
734 /* XXX: unify with initializers handling ? */
735 /* CPUs usually cannot use float constants, so we store them
736 generically in data segment */
737 size
= type_size(&vtop
->type
, &align
);
738 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
739 data_section
->data_offset
= offset
;
740 /* XXX: not portable yet */
741 #if defined(__i386__) || defined(__x86_64__)
742 /* Zero pad x87 tenbyte long doubles */
743 if (size
== LDOUBLE_SIZE
) {
744 vtop
->c
.tab
[2] &= 0xffff;
745 #if LDOUBLE_SIZE == 16
750 ptr
= section_ptr_add(data_section
, size
);
752 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
756 ptr
[i
] = vtop
->c
.tab
[size
-1-i
];
760 ptr
[i
] = vtop
->c
.tab
[i
];
761 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
762 vtop
->r
|= VT_LVAL
| VT_SYM
;
764 vtop
->c
.ptr_offset
= 0;
766 #ifdef CONFIG_TCC_BCHECK
767 if (vtop
->r
& VT_MUSTBOUND
)
771 r
= vtop
->r
& VT_VALMASK
;
772 rc2
= (rc
& RC_FLOAT
) ? RC_FLOAT
: RC_INT
;
775 #ifdef TCC_TARGET_X86_64
776 else if (rc
== RC_FRET
)
780 /* need to reload if:
782 - lvalue (need to dereference pointer)
783 - already a register, but not in the right class */
785 || (vtop
->r
& VT_LVAL
)
786 || !(reg_classes
[r
] & rc
)
787 #ifdef TCC_TARGET_X86_64
788 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
789 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
&& !(reg_classes
[vtop
->r2
] & rc2
))
791 || ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
796 #ifdef TCC_TARGET_X86_64
797 if (((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
)) {
798 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
800 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
801 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
802 unsigned long long ll
;
804 int r2
, original_type
;
805 original_type
= vtop
->type
.t
;
806 /* two register type load : expand to two words
808 #ifndef TCC_TARGET_X86_64
809 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
812 vtop
->c
.ui
= ll
; /* first word */
814 vtop
->r
= r
; /* save register value */
815 vpushi(ll
>> 32); /* second word */
818 if (r
>= VT_CONST
|| /* XXX: test to VT_CONST incorrect ? */
819 (vtop
->r
& VT_LVAL
)) {
820 /* We do not want to modifier the long long
821 pointer here, so the safest (and less
822 efficient) is to save all the other registers
823 in the stack. XXX: totally inefficient. */
825 /* load from memory */
826 vtop
->type
.t
= load_type
;
829 vtop
[-1].r
= r
; /* save register value */
830 /* increment pointer to get second word */
831 vtop
->type
.t
= addr_type
;
836 vtop
->type
.t
= load_type
;
841 vtop
[-1].r
= r
; /* save register value */
842 vtop
->r
= vtop
[-1].r2
;
844 /* Allocate second register. Here we rely on the fact that
845 get_reg() tries first to free r2 of an SValue. */
849 /* write second register */
851 vtop
->type
.t
= original_type
;
852 } else if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
854 /* lvalue of scalar type : need to use lvalue type
855 because of possible cast */
858 /* compute memory access type */
859 if (vtop
->r
& VT_REF
)
860 #ifdef TCC_TARGET_X86_64
865 else if (vtop
->r
& VT_LVAL_BYTE
)
867 else if (vtop
->r
& VT_LVAL_SHORT
)
869 if (vtop
->r
& VT_LVAL_UNSIGNED
)
873 /* restore wanted type */
876 /* one register type load */
881 #ifdef TCC_TARGET_C67
882 /* uses register pairs for doubles */
883 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
890 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
891 ST_FUNC
void gv2(int rc1
, int rc2
)
895 /* generate more generic register first. But VT_JMP or VT_CMP
896 values must be generated first in all cases to avoid possible
898 v
= vtop
[0].r
& VT_VALMASK
;
899 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
904 /* test if reload is needed for first register */
905 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
915 /* test if reload is needed for first register */
916 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
922 /* wrapper around RC_FRET to return a register by type */
923 static int rc_fret(int t
)
925 #ifdef TCC_TARGET_X86_64
926 if (t
== VT_LDOUBLE
) {
933 /* wrapper around REG_FRET to return a register by type */
934 static int reg_fret(int t
)
936 #ifdef TCC_TARGET_X86_64
937 if (t
== VT_LDOUBLE
) {
944 /* expand long long on stack in two int registers */
945 static void lexpand(void)
949 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
952 vtop
[0].r
= vtop
[-1].r2
;
953 vtop
[0].r2
= VT_CONST
;
954 vtop
[-1].r2
= VT_CONST
;
955 vtop
[0].type
.t
= VT_INT
| u
;
956 vtop
[-1].type
.t
= VT_INT
| u
;
959 #ifdef TCC_TARGET_ARM
960 /* expand long long on stack */
961 ST_FUNC
void lexpand_nr(void)
965 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
968 vtop
->type
.t
= VT_INT
| u
;
969 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
971 vtop
[-1].c
.ui
= vtop
->c
.ull
;
972 vtop
->c
.ui
= vtop
->c
.ull
>> 32;
974 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
976 vtop
->r
= vtop
[-1].r
;
977 } else if (v
> VT_CONST
) {
981 vtop
->r
= vtop
[-1].r2
;
982 vtop
[-1].r2
= VT_CONST
;
983 vtop
[-1].type
.t
= VT_INT
| u
;
987 /* build a long long from two ints */
988 static void lbuild(int t
)
991 vtop
[-1].r2
= vtop
[0].r
;
996 /* rotate n first stack elements to the bottom
997 I1 ... In -> I2 ... In I1 [top is right]
999 ST_FUNC
void vrotb(int n
)
1005 for(i
=-n
+1;i
!=0;i
++)
1006 vtop
[i
] = vtop
[i
+1];
1010 /* rotate the n elements before entry e towards the top
1011 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
1013 ST_FUNC
void vrote(SValue
*e
, int n
)
1019 for(i
= 0;i
< n
- 1; i
++)
1024 /* rotate n first stack elements to the top
1025 I1 ... In -> In I1 ... I(n-1) [top is right]
1027 ST_FUNC
void vrott(int n
)
1032 /* pop stack value */
1033 ST_FUNC
void vpop(void)
1036 v
= vtop
->r
& VT_VALMASK
;
1037 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1038 /* for x86, we need to pop the FP stack */
1039 if (v
== TREG_ST0
&& !nocode_wanted
) {
1040 o(0xd8dd); /* fstp %st(0) */
1043 if (v
== VT_JMP
|| v
== VT_JMPI
) {
1044 /* need to put correct jump if && or || without test */
1050 /* convert stack entry to register and duplicate its value in another
1052 static void gv_dup(void)
1058 if ((t
& VT_BTYPE
) == VT_LLONG
) {
1065 /* stack: H L L1 H1 */
1073 /* duplicate value */
1078 #ifdef TCC_TARGET_X86_64
1079 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1089 load(r1
, &sv
); /* move r to r1 */
1091 /* duplicates value */
1097 /* Generate value test
1099 * Generate a test for any value (jump, comparison and integers) */
1100 int gvtst(int inv
, int t
)
1102 int v
= vtop
->r
& VT_VALMASK
;
1103 if (v
!= VT_CMP
&& v
!= VT_JMP
&& v
!= VT_JMPI
) {
1107 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1108 /* constant jmp optimization */
1109 if ((vtop
->c
.i
!= 0) != inv
)
1114 return gtst(inv
, t
);
1117 #ifndef TCC_TARGET_X86_64
1118 /* generate CPU independent (unsigned) long long operations */
1119 static void gen_opl(int op
)
1121 int t
, a
, b
, op1
, c
, i
;
1123 unsigned short reg_iret
= REG_IRET
;
1124 unsigned short reg_lret
= REG_LRET
;
1130 func
= TOK___divdi3
;
1133 func
= TOK___udivdi3
;
1136 func
= TOK___moddi3
;
1139 func
= TOK___umoddi3
;
1146 /* call generic long long function */
1147 vpush_global_sym(&func_old_type
, func
);
1152 vtop
->r2
= reg_lret
;
1165 /* stack: L1 H1 L2 H2 */
1170 vtop
[-2] = vtop
[-3];
1173 /* stack: H1 H2 L1 L2 */
1179 /* stack: H1 H2 L1 L2 ML MH */
1182 /* stack: ML MH H1 H2 L1 L2 */
1186 /* stack: ML MH H1 L2 H2 L1 */
1191 /* stack: ML MH M1 M2 */
1194 } else if (op
== '+' || op
== '-') {
1195 /* XXX: add non carry method too (for MIPS or alpha) */
1201 /* stack: H1 H2 (L1 op L2) */
1204 gen_op(op1
+ 1); /* TOK_xxxC2 */
1207 /* stack: H1 H2 (L1 op L2) */
1210 /* stack: (L1 op L2) H1 H2 */
1212 /* stack: (L1 op L2) (H1 op H2) */
1220 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1221 t
= vtop
[-1].type
.t
;
1225 /* stack: L H shift */
1227 /* constant: simpler */
1228 /* NOTE: all comments are for SHL. the other cases are
1229 done by swaping words */
1240 if (op
!= TOK_SAR
) {
1273 /* XXX: should provide a faster fallback on x86 ? */
1276 func
= TOK___ashrdi3
;
1279 func
= TOK___lshrdi3
;
1282 func
= TOK___ashldi3
;
1288 /* compare operations */
1294 /* stack: L1 H1 L2 H2 */
1296 vtop
[-1] = vtop
[-2];
1298 /* stack: L1 L2 H1 H2 */
1301 /* when values are equal, we need to compare low words. since
1302 the jump is inverted, we invert the test too. */
1305 else if (op1
== TOK_GT
)
1307 else if (op1
== TOK_ULT
)
1309 else if (op1
== TOK_UGT
)
1314 if (op1
!= TOK_NE
) {
1318 /* generate non equal test */
1319 /* XXX: NOT PORTABLE yet */
1323 #if defined(TCC_TARGET_I386)
1324 b
= psym(0x850f, 0);
1325 #elif defined(TCC_TARGET_ARM)
1327 o(0x1A000000 | encbranch(ind
, 0, 1));
1328 #elif defined(TCC_TARGET_C67)
1329 tcc_error("not implemented");
1331 #error not supported
1335 /* compare low. Always unsigned */
1339 else if (op1
== TOK_LE
)
1341 else if (op1
== TOK_GT
)
1343 else if (op1
== TOK_GE
)
1354 /* handle integer constant optimizations and various machine
1356 static void gen_opic(int op
)
1358 int c1
, c2
, t1
, t2
, n
;
1361 typedef unsigned long long U
;
1365 t1
= v1
->type
.t
& VT_BTYPE
;
1366 t2
= v2
->type
.t
& VT_BTYPE
;
1370 else if (v1
->type
.t
& VT_UNSIGNED
)
1377 else if (v2
->type
.t
& VT_UNSIGNED
)
1382 /* currently, we cannot do computations with forward symbols */
1383 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1384 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1387 case '+': l1
+= l2
; break;
1388 case '-': l1
-= l2
; break;
1389 case '&': l1
&= l2
; break;
1390 case '^': l1
^= l2
; break;
1391 case '|': l1
|= l2
; break;
1392 case '*': l1
*= l2
; break;
1399 /* if division by zero, generate explicit division */
1402 tcc_error("division by zero in constant");
1406 default: l1
/= l2
; break;
1407 case '%': l1
%= l2
; break;
1408 case TOK_UDIV
: l1
= (U
)l1
/ l2
; break;
1409 case TOK_UMOD
: l1
= (U
)l1
% l2
; break;
1412 case TOK_SHL
: l1
<<= l2
; break;
1413 case TOK_SHR
: l1
= (U
)l1
>> l2
; break;
1414 case TOK_SAR
: l1
>>= l2
; break;
1416 case TOK_ULT
: l1
= (U
)l1
< (U
)l2
; break;
1417 case TOK_UGE
: l1
= (U
)l1
>= (U
)l2
; break;
1418 case TOK_EQ
: l1
= l1
== l2
; break;
1419 case TOK_NE
: l1
= l1
!= l2
; break;
1420 case TOK_ULE
: l1
= (U
)l1
<= (U
)l2
; break;
1421 case TOK_UGT
: l1
= (U
)l1
> (U
)l2
; break;
1422 case TOK_LT
: l1
= l1
< l2
; break;
1423 case TOK_GE
: l1
= l1
>= l2
; break;
1424 case TOK_LE
: l1
= l1
<= l2
; break;
1425 case TOK_GT
: l1
= l1
> l2
; break;
1427 case TOK_LAND
: l1
= l1
&& l2
; break;
1428 case TOK_LOR
: l1
= l1
|| l2
; break;
1435 /* if commutative ops, put c2 as constant */
1436 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1437 op
== '|' || op
== '*')) {
1439 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1440 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1442 /* Filter out NOP operations like x*1, x-0, x&-1... */
1443 if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1446 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1447 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1453 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1454 /* try to use shifts instead of muls or divs */
1455 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1464 else if (op
== TOK_PDIV
)
1470 } else if (c2
&& (op
== '+' || op
== '-') &&
1471 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
1472 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1473 /* symbol + constant case */
1480 if (!nocode_wanted
) {
1481 /* call low level op generator */
1482 if (t1
== VT_LLONG
|| t2
== VT_LLONG
)
1493 /* generate a floating point operation with constant propagation */
1494 static void gen_opif(int op
)
1502 /* currently, we cannot do computations with forward symbols */
1503 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1504 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1506 if (v1
->type
.t
== VT_FLOAT
) {
1509 } else if (v1
->type
.t
== VT_DOUBLE
) {
1517 /* NOTE: we only do constant propagation if finite number (not
1518 NaN or infinity) (ANSI spec) */
1519 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1523 case '+': f1
+= f2
; break;
1524 case '-': f1
-= f2
; break;
1525 case '*': f1
*= f2
; break;
1529 tcc_error("division by zero in constant");
1534 /* XXX: also handles tests ? */
1538 /* XXX: overflow test ? */
1539 if (v1
->type
.t
== VT_FLOAT
) {
1541 } else if (v1
->type
.t
== VT_DOUBLE
) {
1549 if (!nocode_wanted
) {
1557 static int pointed_size(CType
*type
)
1560 return type_size(pointed_type(type
), &align
);
1563 static void vla_runtime_pointed_size(CType
*type
)
1566 vla_runtime_type_size(pointed_type(type
), &align
);
1569 static inline int is_null_pointer(SValue
*p
)
1571 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1573 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& p
->c
.i
== 0) ||
1574 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.ll
== 0) ||
1575 ((p
->type
.t
& VT_BTYPE
) == VT_PTR
&& p
->c
.ptr_offset
== 0);
1578 static inline int is_integer_btype(int bt
)
1580 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
1581 bt
== VT_INT
|| bt
== VT_LLONG
);
1584 /* check types for comparison or subtraction of pointers */
1585 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
1587 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
1590 /* null pointers are accepted for all comparisons as gcc */
1591 if (is_null_pointer(p1
) || is_null_pointer(p2
))
1595 bt1
= type1
->t
& VT_BTYPE
;
1596 bt2
= type2
->t
& VT_BTYPE
;
1597 /* accept comparison between pointer and integer with a warning */
1598 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
1599 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
1600 tcc_warning("comparison between pointer and integer");
1604 /* both must be pointers or implicit function pointers */
1605 if (bt1
== VT_PTR
) {
1606 type1
= pointed_type(type1
);
1607 } else if (bt1
!= VT_FUNC
)
1608 goto invalid_operands
;
1610 if (bt2
== VT_PTR
) {
1611 type2
= pointed_type(type2
);
1612 } else if (bt2
!= VT_FUNC
) {
1614 tcc_error("invalid operands to binary %s", get_tok_str(op
, NULL
));
1616 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1617 (type2
->t
& VT_BTYPE
) == VT_VOID
)
1621 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1622 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1623 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1624 /* gcc-like error if '-' is used */
1626 goto invalid_operands
;
1628 tcc_warning("comparison of distinct pointer types lacks a cast");
1632 /* generic gen_op: handles types problems */
1633 ST_FUNC
void gen_op(int op
)
1635 int u
, t1
, t2
, bt1
, bt2
, t
;
1638 t1
= vtop
[-1].type
.t
;
1639 t2
= vtop
[0].type
.t
;
1640 bt1
= t1
& VT_BTYPE
;
1641 bt2
= t2
& VT_BTYPE
;
1643 if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
1644 /* at least one operand is a pointer */
1645 /* relationnal op: must be both pointers */
1646 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
1647 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1648 /* pointers are handled are unsigned */
1649 #ifdef TCC_TARGET_X86_64
1650 t
= VT_LLONG
| VT_UNSIGNED
;
1652 t
= VT_INT
| VT_UNSIGNED
;
1656 /* if both pointers, then it must be the '-' op */
1657 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
1659 tcc_error("cannot use pointers here");
1660 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1661 /* XXX: check that types are compatible */
1662 if (vtop
[-1].type
.t
& VT_VLA
) {
1663 vla_runtime_pointed_size(&vtop
[-1].type
);
1665 vpushi(pointed_size(&vtop
[-1].type
));
1669 /* set to integer type */
1670 #ifdef TCC_TARGET_X86_64
1671 vtop
->type
.t
= VT_LLONG
;
1673 vtop
->type
.t
= VT_INT
;
1678 /* exactly one pointer : must be '+' or '-'. */
1679 if (op
!= '-' && op
!= '+')
1680 tcc_error("cannot use pointers here");
1681 /* Put pointer as first operand */
1682 if (bt2
== VT_PTR
) {
1686 type1
= vtop
[-1].type
;
1687 type1
.t
&= ~VT_ARRAY
;
1688 if (vtop
[-1].type
.t
& VT_VLA
)
1689 vla_runtime_pointed_size(&vtop
[-1].type
);
1691 u
= pointed_size(&vtop
[-1].type
);
1693 tcc_error("unknown array element size");
1694 #ifdef TCC_TARGET_X86_64
1697 /* XXX: cast to int ? (long long case) */
1702 #ifdef CONFIG_TCC_BCHECK
1703 /* if evaluating constant expression, no code should be
1704 generated, so no bound check */
1705 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
1706 /* if bounded pointers, we generate a special code to
1713 gen_bounded_ptr_add();
1719 /* put again type if gen_opic() swaped operands */
1722 } else if (is_float(bt1
) || is_float(bt2
)) {
1723 /* compute bigger type and do implicit casts */
1724 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
1726 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
1731 /* floats can only be used for a few operations */
1732 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
1733 (op
< TOK_ULT
|| op
> TOK_GT
))
1734 tcc_error("invalid operands for binary operation");
1736 } else if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
) {
1737 t
= bt1
== VT_LLONG
? VT_LLONG
: VT_INT
;
1738 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (t
| VT_UNSIGNED
))
1741 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
1742 /* cast to biggest op */
1744 /* convert to unsigned if it does not fit in a long long */
1745 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
1746 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
1749 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
1750 tcc_error("comparison of struct");
1752 /* integer operations */
1754 /* convert to unsigned if it does not fit in an integer */
1755 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
1756 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
1759 /* XXX: currently, some unsigned operations are explicit, so
1760 we modify them here */
1761 if (t
& VT_UNSIGNED
) {
1768 else if (op
== TOK_LT
)
1770 else if (op
== TOK_GT
)
1772 else if (op
== TOK_LE
)
1774 else if (op
== TOK_GE
)
1781 /* special case for shifts and long long: we keep the shift as
1783 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
1790 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
1791 /* relationnal op: the result is an int */
1792 vtop
->type
.t
= VT_INT
;
1799 #ifndef TCC_TARGET_ARM
1800 /* generic itof for unsigned long long case */
1801 static void gen_cvt_itof1(int t
)
1803 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
1804 (VT_LLONG
| VT_UNSIGNED
)) {
1807 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
1808 #if LDOUBLE_SIZE != 8
1809 else if (t
== VT_LDOUBLE
)
1810 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
1813 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
1817 vtop
->r
= reg_fret(t
);
1824 /* generic ftoi for unsigned long long case */
1825 static void gen_cvt_ftoi1(int t
)
1829 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
1830 /* not handled natively */
1831 st
= vtop
->type
.t
& VT_BTYPE
;
1833 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
1834 #if LDOUBLE_SIZE != 8
1835 else if (st
== VT_LDOUBLE
)
1836 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
1839 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
1844 vtop
->r2
= REG_LRET
;
1850 /* force char or short cast */
1851 static void force_charshort_cast(int t
)
1855 /* XXX: add optimization if lvalue : just change type and offset */
1860 if (t
& VT_UNSIGNED
) {
1861 vpushi((1 << bits
) - 1);
1867 /* result must be signed or the SAR is converted to an SHL
1868 This was not the case when "t" was a signed short
1869 and the last value on the stack was an unsigned int */
1870 vtop
->type
.t
&= ~VT_UNSIGNED
;
1876 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1877 static void gen_cast(CType
*type
)
1879 int sbt
, dbt
, sf
, df
, c
, p
;
1881 /* special delayed cast for char/short */
1882 /* XXX: in some cases (multiple cascaded casts), it may still
1884 if (vtop
->r
& VT_MUSTCAST
) {
1885 vtop
->r
&= ~VT_MUSTCAST
;
1886 force_charshort_cast(vtop
->type
.t
);
1889 /* bitfields first get cast to ints */
1890 if (vtop
->type
.t
& VT_BITFIELD
) {
1894 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
1895 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
1900 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1901 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
1903 /* constant case: we can do it now */
1904 /* XXX: in ISOC, cannot do it if error in convert */
1905 if (sbt
== VT_FLOAT
)
1906 vtop
->c
.ld
= vtop
->c
.f
;
1907 else if (sbt
== VT_DOUBLE
)
1908 vtop
->c
.ld
= vtop
->c
.d
;
1911 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
1912 if (sbt
& VT_UNSIGNED
)
1913 vtop
->c
.ld
= vtop
->c
.ull
;
1915 vtop
->c
.ld
= vtop
->c
.ll
;
1917 if (sbt
& VT_UNSIGNED
)
1918 vtop
->c
.ld
= vtop
->c
.ui
;
1920 vtop
->c
.ld
= vtop
->c
.i
;
1923 if (dbt
== VT_FLOAT
)
1924 vtop
->c
.f
= (float)vtop
->c
.ld
;
1925 else if (dbt
== VT_DOUBLE
)
1926 vtop
->c
.d
= (double)vtop
->c
.ld
;
1927 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
1928 vtop
->c
.ull
= (unsigned long long)vtop
->c
.ld
;
1929 } else if (sf
&& dbt
== VT_BOOL
) {
1930 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
1933 vtop
->c
.ll
= (long long)vtop
->c
.ld
;
1934 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
1935 vtop
->c
.ll
= vtop
->c
.ull
;
1936 else if (sbt
& VT_UNSIGNED
)
1937 vtop
->c
.ll
= vtop
->c
.ui
;
1938 #ifdef TCC_TARGET_X86_64
1939 else if (sbt
== VT_PTR
)
1942 else if (sbt
!= VT_LLONG
)
1943 vtop
->c
.ll
= vtop
->c
.i
;
1945 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
1946 vtop
->c
.ull
= vtop
->c
.ll
;
1947 else if (dbt
== VT_BOOL
)
1948 vtop
->c
.i
= (vtop
->c
.ll
!= 0);
1949 #ifdef TCC_TARGET_X86_64
1950 else if (dbt
== VT_PTR
)
1953 else if (dbt
!= VT_LLONG
) {
1955 if ((dbt
& VT_BTYPE
) == VT_BYTE
)
1957 else if ((dbt
& VT_BTYPE
) == VT_SHORT
)
1959 if(dbt
& VT_UNSIGNED
)
1960 vtop
->c
.ui
= ((unsigned int)vtop
->c
.ll
<< s
) >> s
;
1962 vtop
->c
.i
= ((int)vtop
->c
.ll
<< s
) >> s
;
1965 } else if (p
&& dbt
== VT_BOOL
) {
1968 } else if (!nocode_wanted
) {
1969 /* non constant case: generate code */
1971 /* convert from fp to fp */
1974 /* convert int to fp */
1977 /* convert fp to int */
1978 if (dbt
== VT_BOOL
) {
1982 /* we handle char/short/etc... with generic code */
1983 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
1984 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
1988 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
1989 /* additional cast for char/short... */
1994 #ifndef TCC_TARGET_X86_64
1995 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
1996 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
1997 /* scalar to long long */
1998 /* machine independent conversion */
2000 /* generate high word */
2001 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
2005 if (sbt
== VT_PTR
) {
2006 /* cast from pointer to int before we apply
2007 shift operation, which pointers don't support*/
2008 gen_cast(&int_type
);
2014 /* patch second register */
2015 vtop
[-1].r2
= vtop
->r
;
2019 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
2020 (dbt
& VT_BTYPE
) == VT_PTR
||
2021 (dbt
& VT_BTYPE
) == VT_FUNC
) {
2022 if ((sbt
& VT_BTYPE
) != VT_LLONG
&&
2023 (sbt
& VT_BTYPE
) != VT_PTR
&&
2024 (sbt
& VT_BTYPE
) != VT_FUNC
) {
2025 /* need to convert from 32bit to 64bit */
2027 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
2028 /* x86_64 specific: movslq */
2030 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
2034 } else if (dbt
== VT_BOOL
) {
2035 /* scalar to bool */
2038 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
2039 (dbt
& VT_BTYPE
) == VT_SHORT
) {
2040 if (sbt
== VT_PTR
) {
2041 vtop
->type
.t
= VT_INT
;
2042 tcc_warning("nonportable conversion from pointer to char/short");
2044 force_charshort_cast(dbt
);
2045 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
2047 if (sbt
== VT_LLONG
) {
2048 /* from long long: just take low order word */
2052 /* if lvalue and single word type, nothing to do because
2053 the lvalue already contains the real type size (see
2054 VT_LVAL_xxx constants) */
2057 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
2058 /* if we are casting between pointer types,
2059 we must update the VT_LVAL_xxx size */
2060 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
2061 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
2066 /* return type size as known at compile time. Put alignment at 'a' */
2067 ST_FUNC
int type_size(CType
*type
, int *a
)
2072 bt
= type
->t
& VT_BTYPE
;
2073 if (bt
== VT_STRUCT
) {
2078 } else if (bt
== VT_PTR
) {
2079 if (type
->t
& VT_ARRAY
) {
2083 ts
= type_size(&s
->type
, a
);
2085 if (ts
< 0 && s
->c
< 0)
2093 } else if (bt
== VT_LDOUBLE
) {
2095 return LDOUBLE_SIZE
;
2096 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
2097 #ifdef TCC_TARGET_I386
2098 #ifdef TCC_TARGET_PE
2103 #elif defined(TCC_TARGET_ARM)
2113 } else if (bt
== VT_INT
|| bt
== VT_ENUM
|| bt
== VT_FLOAT
) {
2116 } else if (bt
== VT_SHORT
) {
2119 } else if (bt
== VT_QLONG
|| bt
== VT_QFLOAT
) {
2123 /* char, void, function, _Bool */
2129 /* push type size as known at runtime time on top of value stack. Put
2131 ST_FUNC
void vla_runtime_type_size(CType
*type
, int *a
)
2133 if (type
->t
& VT_VLA
) {
2134 vset(&int_type
, VT_LOCAL
|VT_LVAL
, type
->ref
->c
);
2136 vpushi(type_size(type
, a
));
2140 static void vla_sp_save(void) {
2141 if (!(vla_flags
& VLA_SP_LOC_SET
)) {
2142 *vla_sp_loc
= (loc
-= PTR_SIZE
);
2143 vla_flags
|= VLA_SP_LOC_SET
;
2145 if (!(vla_flags
& VLA_SP_SAVED
)) {
2146 gen_vla_sp_save(*vla_sp_loc
);
2147 vla_flags
|= VLA_SP_SAVED
;
2151 /* return the pointed type of t */
2152 static inline CType
*pointed_type(CType
*type
)
2154 return &type
->ref
->type
;
2157 /* modify type so that its it is a pointer to type. */
2158 ST_FUNC
void mk_pointer(CType
*type
)
2161 s
= sym_push(SYM_FIELD
, type
, 0, -1);
2162 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
2166 /* compare function types. OLD functions match any new functions */
2167 static int is_compatible_func(CType
*type1
, CType
*type2
)
2173 if (!is_compatible_types(&s1
->type
, &s2
->type
))
2175 /* check func_call */
2176 if (s1
->a
.func_call
!= s2
->a
.func_call
)
2178 /* XXX: not complete */
2179 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
2183 while (s1
!= NULL
) {
2186 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
2196 /* return true if type1 and type2 are the same. If unqualified is
2197 true, qualifiers on the types are ignored.
2199 - enums are not checked as gcc __builtin_types_compatible_p ()
2201 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2205 t1
= type1
->t
& VT_TYPE
;
2206 t2
= type2
->t
& VT_TYPE
;
2208 /* strip qualifiers before comparing */
2209 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2210 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2212 /* Default Vs explicit signedness only matters for char */
2213 if ((t1
& VT_BTYPE
) != VT_BYTE
) {
2217 /* XXX: bitfields ? */
2220 /* test more complicated cases */
2221 bt1
= t1
& VT_BTYPE
;
2222 if (bt1
== VT_PTR
) {
2223 type1
= pointed_type(type1
);
2224 type2
= pointed_type(type2
);
2225 return is_compatible_types(type1
, type2
);
2226 } else if (bt1
== VT_STRUCT
) {
2227 return (type1
->ref
== type2
->ref
);
2228 } else if (bt1
== VT_FUNC
) {
2229 return is_compatible_func(type1
, type2
);
2235 /* return true if type1 and type2 are exactly the same (including
2238 static int is_compatible_types(CType
*type1
, CType
*type2
)
2240 return compare_types(type1
,type2
,0);
2243 /* return true if type1 and type2 are the same (ignoring qualifiers).
2245 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
2247 return compare_types(type1
,type2
,1);
2250 /* print a type. If 'varstr' is not NULL, then the variable is also
2251 printed in the type */
2253 /* XXX: add array and function pointers */
2254 static void type_to_str(char *buf
, int buf_size
,
2255 CType
*type
, const char *varstr
)
2262 t
= type
->t
& VT_TYPE
;
2265 if (t
& VT_CONSTANT
)
2266 pstrcat(buf
, buf_size
, "const ");
2267 if (t
& VT_VOLATILE
)
2268 pstrcat(buf
, buf_size
, "volatile ");
2269 if ((t
& (VT_DEFSIGN
| VT_UNSIGNED
)) == (VT_DEFSIGN
| VT_UNSIGNED
))
2270 pstrcat(buf
, buf_size
, "unsigned ");
2271 else if (t
& VT_DEFSIGN
)
2272 pstrcat(buf
, buf_size
, "signed ");
2302 tstr
= "long double";
2304 pstrcat(buf
, buf_size
, tstr
);
2308 if (bt
== VT_STRUCT
)
2312 pstrcat(buf
, buf_size
, tstr
);
2313 v
= type
->ref
->v
& ~SYM_STRUCT
;
2314 if (v
>= SYM_FIRST_ANOM
)
2315 pstrcat(buf
, buf_size
, "<anonymous>");
2317 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
2321 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
2322 pstrcat(buf
, buf_size
, "(");
2324 while (sa
!= NULL
) {
2325 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
2326 pstrcat(buf
, buf_size
, buf1
);
2329 pstrcat(buf
, buf_size
, ", ");
2331 pstrcat(buf
, buf_size
, ")");
2335 pstrcpy(buf1
, sizeof(buf1
), "*");
2337 pstrcat(buf1
, sizeof(buf1
), varstr
);
2338 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2342 pstrcat(buf
, buf_size
, " ");
2343 pstrcat(buf
, buf_size
, varstr
);
2348 /* verify type compatibility to store vtop in 'dt' type, and generate
2350 static void gen_assign_cast(CType
*dt
)
2352 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
2353 char buf1
[256], buf2
[256];
2356 st
= &vtop
->type
; /* source type */
2357 dbt
= dt
->t
& VT_BTYPE
;
2358 sbt
= st
->t
& VT_BTYPE
;
2359 if (sbt
== VT_VOID
|| dbt
== VT_VOID
)
2360 tcc_error("cannot cast from/to void");
2361 if (dt
->t
& VT_CONSTANT
)
2362 tcc_warning("assignment of read-only location");
2365 /* special cases for pointers */
2366 /* '0' can also be a pointer */
2367 if (is_null_pointer(vtop
))
2369 /* accept implicit pointer to integer cast with warning */
2370 if (is_integer_btype(sbt
)) {
2371 tcc_warning("assignment makes pointer from integer without a cast");
2374 type1
= pointed_type(dt
);
2375 /* a function is implicitely a function pointer */
2376 if (sbt
== VT_FUNC
) {
2377 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
2378 !is_compatible_types(pointed_type(dt
), st
))
2379 tcc_warning("assignment from incompatible pointer type");
2384 type2
= pointed_type(st
);
2385 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2386 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
2387 /* void * can match anything */
2389 /* exact type match, except for unsigned */
2392 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
|
2394 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
|
2396 if (!is_compatible_types(&tmp_type1
, &tmp_type2
))
2397 tcc_warning("assignment from incompatible pointer type");
2399 /* check const and volatile */
2400 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
2401 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
2402 tcc_warning("assignment discards qualifiers from pointer target type");
2408 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
2409 tcc_warning("assignment makes integer from pointer without a cast");
2411 /* XXX: more tests */
2416 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2417 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2418 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2420 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
2421 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
2422 tcc_error("cannot cast '%s' to '%s'", buf1
, buf2
);
2430 /* store vtop in lvalue pushed on stack */
2431 ST_FUNC
void vstore(void)
2433 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
2435 ft
= vtop
[-1].type
.t
;
2436 sbt
= vtop
->type
.t
& VT_BTYPE
;
2437 dbt
= ft
& VT_BTYPE
;
2438 if ((((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2439 (sbt
== VT_INT
&& dbt
== VT_SHORT
))
2440 && !(vtop
->type
.t
& VT_BITFIELD
)) {
2441 /* optimize char/short casts */
2442 delayed_cast
= VT_MUSTCAST
;
2443 vtop
->type
.t
= ft
& (VT_TYPE
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
)));
2444 /* XXX: factorize */
2445 if (ft
& VT_CONSTANT
)
2446 tcc_warning("assignment of read-only location");
2449 if (!(ft
& VT_BITFIELD
))
2450 gen_assign_cast(&vtop
[-1].type
);
2453 if (sbt
== VT_STRUCT
) {
2454 /* if structure, only generate pointer */
2455 /* structure assignment : generate memcpy */
2456 /* XXX: optimize if small size */
2457 if (!nocode_wanted
) {
2458 size
= type_size(&vtop
->type
, &align
);
2462 vtop
->type
.t
= VT_PTR
;
2465 /* address of memcpy() */
2468 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2469 else if(!(align
& 3))
2470 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2473 vpush_global_sym(&func_old_type
, TOK_memcpy
);
2478 vtop
->type
.t
= VT_PTR
;
2487 /* leave source on stack */
2488 } else if (ft
& VT_BITFIELD
) {
2489 /* bitfield store handling */
2490 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2491 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2492 /* remove bit field info to avoid loops */
2493 vtop
[-1].type
.t
= ft
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
2495 /* duplicate source into other register */
2500 if((ft
& VT_BTYPE
) == VT_BOOL
) {
2501 gen_cast(&vtop
[-1].type
);
2502 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2505 /* duplicate destination */
2507 vtop
[-1] = vtop
[-2];
2509 /* mask and shift source */
2510 if((ft
& VT_BTYPE
) != VT_BOOL
) {
2511 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2512 vpushll((1ULL << bit_size
) - 1ULL);
2514 vpushi((1 << bit_size
) - 1);
2520 /* load destination, mask and or with source */
2522 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2523 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2525 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2532 /* pop off shifted source from "duplicate source..." above */
2536 #ifdef CONFIG_TCC_BCHECK
2537 /* bound check case */
2538 if (vtop
[-1].r
& VT_MUSTBOUND
) {
2544 if (!nocode_wanted
) {
2548 #ifdef TCC_TARGET_X86_64
2549 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
2551 } else if ((ft
& VT_BTYPE
) == VT_QFLOAT
) {
2556 r
= gv(rc
); /* generate value */
2557 /* if lvalue was saved on stack, must read it */
2558 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
2560 t
= get_reg(RC_INT
);
2561 #ifdef TCC_TARGET_X86_64
2566 sv
.r
= VT_LOCAL
| VT_LVAL
;
2567 sv
.c
.ul
= vtop
[-1].c
.ul
;
2569 vtop
[-1].r
= t
| VT_LVAL
;
2571 /* two word case handling : store second register at word + 4 (or +8 for x86-64) */
2572 #ifdef TCC_TARGET_X86_64
2573 if (((ft
& VT_BTYPE
) == VT_QLONG
) || ((ft
& VT_BTYPE
) == VT_QFLOAT
)) {
2574 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
2576 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
2577 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
2579 vtop
[-1].type
.t
= load_type
;
2582 /* convert to int to increment easily */
2583 vtop
->type
.t
= addr_type
;
2589 vtop
[-1].type
.t
= load_type
;
2590 /* XXX: it works because r2 is spilled last ! */
2591 store(vtop
->r2
, vtop
- 1);
2597 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
2598 vtop
->r
|= delayed_cast
;
2602 /* post defines POST/PRE add. c is the token ++ or -- */
2603 ST_FUNC
void inc(int post
, int c
)
2606 vdup(); /* save lvalue */
2608 gv_dup(); /* duplicate value */
2613 vpushi(c
- TOK_MID
);
2615 vstore(); /* store value */
2617 vpop(); /* if post op, return saved value */
2620 /* Parse GNUC __attribute__ extension. Currently, the following
2621 extensions are recognized:
2622 - aligned(n) : set data/function alignment.
2623 - packed : force data alignment to 1
2624 - section(x) : generate data/code in this section.
2625 - unused : currently ignored, but may be used someday.
2626 - regparm(n) : pass function parameters in registers (i386 only)
2628 static void parse_attribute(AttributeDef
*ad
)
2632 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
2636 while (tok
!= ')') {
2637 if (tok
< TOK_IDENT
)
2638 expect("attribute name");
2646 expect("section name");
2647 ad
->section
= find_section(tcc_state
, (char *)tokc
.cstr
->data
);
2655 expect("alias(\"target\")");
2656 ad
->alias_target
= /* save string as token, for later */
2657 tok_alloc((char*)tokc
.cstr
->data
, tokc
.cstr
->size
-1)->tok
;
2666 if (n
<= 0 || (n
& (n
- 1)) != 0)
2667 tcc_error("alignment must be a positive power of two");
2684 /* currently, no need to handle it because tcc does not
2685 track unused objects */
2689 /* currently, no need to handle it because tcc does not
2690 track unused objects */
2695 ad
->a
.func_call
= FUNC_CDECL
;
2700 ad
->a
.func_call
= FUNC_STDCALL
;
2702 #ifdef TCC_TARGET_I386
2712 ad
->a
.func_call
= FUNC_FASTCALL1
+ n
- 1;
2718 ad
->a
.func_call
= FUNC_FASTCALLW
;
2725 ad
->a
.mode
= VT_LLONG
+ 1;
2728 ad
->a
.mode
= VT_SHORT
+ 1;
2731 ad
->a
.mode
= VT_INT
+ 1;
2734 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
2741 ad
->a
.func_export
= 1;
2744 ad
->a
.func_import
= 1;
2747 if (tcc_state
->warn_unsupported
)
2748 tcc_warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
2749 /* skip parameters */
2751 int parenthesis
= 0;
2755 else if (tok
== ')')
2758 } while (parenthesis
&& tok
!= -1);
2771 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2772 static void struct_decl(CType
*type
, int u
, int tdef
)
2774 int a
, v
, size
, align
, maxalign
, c
, offset
, flexible
;
2775 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
, prevbt
;
2776 Sym
*s
, *ss
, *ass
, **ps
;
2780 a
= tok
; /* save decl type */
2785 /* struct already defined ? return it */
2787 expect("struct/union/enum name");
2791 tcc_error("invalid type");
2793 } else if (tok
>= TOK_IDENT
&& !tdef
)
2794 tcc_error("unknown struct/union/enum");
2800 /* we put an undefined size for struct/union */
2801 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
2802 s
->r
= 0; /* default alignment is zero as gcc */
2803 /* put struct/union/enum name in type */
2811 tcc_error("struct/union/enum already defined");
2812 /* cannot be empty */
2814 /* non empty enums are not allowed */
2815 if (a
== TOK_ENUM
) {
2819 expect("identifier");
2821 if (ss
&& !local_stack
)
2822 tcc_error("redefinition of enumerator '%s'",
2823 get_tok_str(v
, NULL
));
2829 /* enum symbols have static storage */
2830 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
2831 ss
->type
.t
|= VT_STATIC
;
2836 /* NOTE: we accept a trailing comma */
2840 s
->c
= type_size(&int_type
, &align
);
2849 while (tok
!= '}') {
2850 parse_btype(&btype
, &ad
);
2853 tcc_error("flexible array member '%s' not at the end of struct",
2854 get_tok_str(v
, NULL
));
2859 type_decl(&type1
, &ad
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
2860 if (v
== 0 && (type1
.t
& VT_BTYPE
) != VT_STRUCT
)
2861 expect("identifier");
2862 if (type_size(&type1
, &align
) < 0) {
2863 if ((a
== TOK_STRUCT
) && (type1
.t
& VT_ARRAY
) && c
)
2866 tcc_error("field '%s' has incomplete type",
2867 get_tok_str(v
, NULL
));
2869 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
2870 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
2871 tcc_error("invalid type for '%s'",
2872 get_tok_str(v
, NULL
));
2876 bit_size
= expr_const();
2877 /* XXX: handle v = 0 case for messages */
2879 tcc_error("negative width in bit-field '%s'",
2880 get_tok_str(v
, NULL
));
2881 if (v
&& bit_size
== 0)
2882 tcc_error("zero width for bit-field '%s'",
2883 get_tok_str(v
, NULL
));
2885 size
= type_size(&type1
, &align
);
2887 if (align
< ad
.a
.aligned
)
2888 align
= ad
.a
.aligned
;
2889 } else if (ad
.a
.packed
) {
2891 } else if (*tcc_state
->pack_stack_ptr
) {
2892 if (align
> *tcc_state
->pack_stack_ptr
)
2893 align
= *tcc_state
->pack_stack_ptr
;
2896 if (bit_size
>= 0) {
2897 bt
= type1
.t
& VT_BTYPE
;
2904 tcc_error("bitfields must have scalar type");
2906 if (bit_size
> bsize
) {
2907 tcc_error("width of '%s' exceeds its type",
2908 get_tok_str(v
, NULL
));
2909 } else if (bit_size
== bsize
) {
2910 /* no need for bit fields */
2912 } else if (bit_size
== 0) {
2913 /* XXX: what to do if only padding in a
2915 /* zero size: means to pad */
2918 /* we do not have enough room ?
2919 did the type change?
2921 if ((bit_pos
+ bit_size
) > bsize
||
2922 bt
!= prevbt
|| a
== TOK_UNION
)
2925 /* XXX: handle LSB first */
2926 type1
.t
|= VT_BITFIELD
|
2927 (bit_pos
<< VT_STRUCT_SHIFT
) |
2928 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
2929 bit_pos
+= bit_size
;
2935 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2936 /* add new memory data only if starting
2938 if (lbit_pos
== 0) {
2939 if (a
== TOK_STRUCT
) {
2940 c
= (c
+ align
- 1) & -align
;
2949 if (align
> maxalign
)
2953 printf("add field %s offset=%d",
2954 get_tok_str(v
, NULL
), offset
);
2955 if (type1
.t
& VT_BITFIELD
) {
2956 printf(" pos=%d size=%d",
2957 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
2958 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
2963 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2965 while ((ass
= ass
->next
) != NULL
) {
2966 ss
= sym_push(ass
->v
, &ass
->type
, 0, offset
+ ass
->c
);
2971 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
2975 if (tok
== ';' || tok
== TOK_EOF
)
2982 /* store size and alignment */
2983 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
2989 /* return 1 if basic type is a type size (short, long, long long) */
2990 int is_btype_size (int bt
)
2992 return bt
== VT_SHORT
|| bt
== VT_LONG
|| bt
== VT_LLONG
;
2995 /* return 0 if no type declaration. otherwise, return the basic type
2998 static int parse_btype(CType
*type
, AttributeDef
*ad
)
3000 int t
, u
, bt_size
, complete
, type_found
, typespec_found
;
3004 memset(ad
, 0, sizeof(AttributeDef
));
3012 /* currently, we really ignore extension */
3023 tcc_error("too many basic types");
3025 bt_size
= is_btype_size (u
& VT_BTYPE
);
3026 if (u
== VT_INT
|| (!bt_size
&& !(t
& VT_TYPEDEF
)))
3041 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
3042 #ifndef TCC_TARGET_PE
3043 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3045 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
3046 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3060 if ((t
& VT_BTYPE
) == VT_LONG
) {
3061 #ifdef TCC_TARGET_PE
3062 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
3064 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3072 struct_decl(&type1
, VT_ENUM
, t
& VT_TYPEDEF
);
3075 type
->ref
= type1
.ref
;
3079 struct_decl(&type1
, VT_STRUCT
, t
& VT_TYPEDEF
);
3082 /* type modifiers */
3098 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == (VT_DEFSIGN
|VT_UNSIGNED
))
3099 tcc_error("signed and unsigned modifier");
3112 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == VT_DEFSIGN
)
3113 tcc_error("signed and unsigned modifier");
3114 t
|= VT_DEFSIGN
| VT_UNSIGNED
;
3139 /* GNUC attribute */
3140 case TOK_ATTRIBUTE1
:
3141 case TOK_ATTRIBUTE2
:
3142 parse_attribute(ad
);
3145 t
= (t
& ~VT_BTYPE
) | u
;
3153 parse_expr_type(&type1
);
3154 /* remove all storage modifiers except typedef */
3155 type1
.t
&= ~(VT_STORAGE
&~VT_TYPEDEF
);
3161 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
3163 t
|= (s
->type
.t
& ~VT_TYPEDEF
);
3164 type
->ref
= s
->type
.ref
;
3166 /* get attributes from typedef */
3167 if (0 == ad
->a
.aligned
)
3168 ad
->a
.aligned
= s
->a
.aligned
;
3169 if (0 == ad
->a
.func_call
)
3170 ad
->a
.func_call
= s
->a
.func_call
;
3171 ad
->a
.packed
|= s
->a
.packed
;
3180 if (tcc_state
->char_is_unsigned
) {
3181 if ((t
& (VT_DEFSIGN
|VT_BTYPE
)) == VT_BYTE
)
3185 /* long is never used as type */
3186 if ((t
& VT_BTYPE
) == VT_LONG
)
3187 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
3188 t
= (t
& ~VT_BTYPE
) | VT_INT
;
3190 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3196 /* convert a function parameter type (array to pointer and function to
3197 function pointer) */
3198 static inline void convert_parameter_type(CType
*pt
)
3200 /* remove const and volatile qualifiers (XXX: const could be used
3201 to indicate a const function parameter */
3202 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3203 /* array must be transformed to pointer according to ANSI C */
3205 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
3210 ST_FUNC
void parse_asm_str(CString
*astr
)
3213 /* read the string */
3215 expect("string constant");
3217 while (tok
== TOK_STR
) {
3218 /* XXX: add \0 handling too ? */
3219 cstr_cat(astr
, tokc
.cstr
->data
);
3222 cstr_ccat(astr
, '\0');
3225 /* Parse an asm label and return the label
3226 * Don't forget to free the CString in the caller! */
3227 static void asm_label_instr(CString
*astr
)
3230 parse_asm_str(astr
);
3233 printf("asm_alias: \"%s\"\n", (char *)astr
->data
);
3237 static void post_type(CType
*type
, AttributeDef
*ad
)
3239 int n
, l
, t1
, arg_size
, align
;
3240 Sym
**plast
, *s
, *first
;
3245 /* function declaration */
3253 /* read param name and compute offset */
3254 if (l
!= FUNC_OLD
) {
3255 if (!parse_btype(&pt
, &ad1
)) {
3257 tcc_error("invalid type");
3264 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
3266 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3267 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
3268 tcc_error("parameter declared as void");
3269 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
3274 expect("identifier");
3278 convert_parameter_type(&pt
);
3279 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
3285 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
3292 /* if no parameters, then old type prototype */
3296 /* NOTE: const is ignored in returned type as it has a special
3297 meaning in gcc / C++ */
3298 type
->t
&= ~VT_CONSTANT
;
3299 /* some ancient pre-K&R C allows a function to return an array
3300 and the array brackets to be put after the arguments, such
3301 that "int c()[]" means something like "int[] c()" */
3304 skip(']'); /* only handle simple "[]" */
3307 /* we push a anonymous symbol which will contain the function prototype */
3308 ad
->a
.func_args
= arg_size
;
3309 s
= sym_push(SYM_FIELD
, type
, 0, l
);
3314 } else if (tok
== '[') {
3315 /* array definition */
3317 if (tok
== TOK_RESTRICT1
)
3322 if (!local_stack
|| nocode_wanted
)
3323 vpushi(expr_const());
3325 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3328 tcc_error("invalid array size");
3330 if (!is_integer_btype(vtop
->type
.t
& VT_BTYPE
))
3331 tcc_error("size of variable length array should be an integer");
3336 /* parse next post type */
3337 post_type(type
, ad
);
3338 if (type
->t
== VT_FUNC
)
3339 tcc_error("declaration of an array of functions");
3340 t1
|= type
->t
& VT_VLA
;
3343 loc
-= type_size(&int_type
, &align
);
3347 vla_runtime_type_size(type
, &align
);
3349 vset(&int_type
, VT_LOCAL
|VT_LVAL
, loc
);
3356 /* we push an anonymous symbol which will contain the array
3358 s
= sym_push(SYM_FIELD
, type
, 0, n
);
3359 type
->t
= (t1
? VT_VLA
: VT_ARRAY
) | VT_PTR
;
3364 /* Parse a type declaration (except basic type), and return the type
3365 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3366 expected. 'type' should contain the basic type. 'ad' is the
3367 attribute definition of the basic type. It can be modified by
3370 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
3373 CType type1
, *type2
;
3374 int qualifiers
, storage
;
3376 while (tok
== '*') {
3384 qualifiers
|= VT_CONSTANT
;
3389 qualifiers
|= VT_VOLATILE
;
3397 type
->t
|= qualifiers
;
3400 /* XXX: clarify attribute handling */
3401 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3402 parse_attribute(ad
);
3404 /* recursive type */
3405 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3406 type1
.t
= 0; /* XXX: same as int */
3409 /* XXX: this is not correct to modify 'ad' at this point, but
3410 the syntax is not clear */
3411 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3412 parse_attribute(ad
);
3413 type_decl(&type1
, ad
, v
, td
);
3416 /* type identifier */
3417 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
3421 if (!(td
& TYPE_ABSTRACT
))
3422 expect("identifier");
3426 storage
= type
->t
& VT_STORAGE
;
3427 type
->t
&= ~VT_STORAGE
;
3428 if (storage
& VT_STATIC
) {
3429 int saved_nocode_wanted
= nocode_wanted
;
3431 post_type(type
, ad
);
3432 nocode_wanted
= saved_nocode_wanted
;
3434 post_type(type
, ad
);
3436 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3437 parse_attribute(ad
);
3441 /* append type at the end of type1 */
3454 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3455 ST_FUNC
int lvalue_type(int t
)
3460 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
3462 else if (bt
== VT_SHORT
)
3466 if (t
& VT_UNSIGNED
)
3467 r
|= VT_LVAL_UNSIGNED
;
3471 /* indirection with full error checking and bound check */
3472 ST_FUNC
void indir(void)
3474 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
3475 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
3479 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
3481 vtop
->type
= *pointed_type(&vtop
->type
);
3482 /* Arrays and functions are never lvalues */
3483 if (!(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_VLA
)
3484 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3485 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3486 /* if bound checking, the referenced pointer must be checked */
3487 #ifdef CONFIG_TCC_BCHECK
3488 if (tcc_state
->do_bounds_check
)
3489 vtop
->r
|= VT_MUSTBOUND
;
3494 /* pass a parameter to a function and do type checking and casting */
3495 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
3500 func_type
= func
->c
;
3501 if (func_type
== FUNC_OLD
||
3502 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
3503 /* default casting : only need to convert float to double */
3504 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
3507 } else if (vtop
->type
.t
& VT_BITFIELD
) {
3508 type
.t
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
3511 } else if (arg
== NULL
) {
3512 tcc_error("too many arguments to function");
3515 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
3516 gen_assign_cast(&type
);
3520 /* parse an expression of the form '(type)' or '(expr)' and return its
3522 static void parse_expr_type(CType
*type
)
3528 if (parse_btype(type
, &ad
)) {
3529 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3536 static void parse_type(CType
*type
)
3541 if (!parse_btype(type
, &ad
)) {
3544 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3547 static void vpush_tokc(int t
)
3552 vsetc(&type
, VT_CONST
, &tokc
);
3555 ST_FUNC
void unary(void)
3557 int n
, t
, align
, size
, r
, sizeof_caller
;
3561 static int in_sizeof
= 0;
3563 sizeof_caller
= in_sizeof
;
3565 /* XXX: GCC 2.95.3 does not generate a table although it should be
3579 vpush_tokc(VT_INT
| VT_UNSIGNED
);
3583 vpush_tokc(VT_LLONG
);
3587 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
3591 vpush_tokc(VT_FLOAT
);
3595 vpush_tokc(VT_DOUBLE
);
3599 vpush_tokc(VT_LDOUBLE
);
3602 case TOK___FUNCTION__
:
3604 goto tok_identifier
;
3610 /* special function name identifier */
3611 len
= strlen(funcname
) + 1;
3612 /* generate char[len] type */
3617 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
3618 ptr
= section_ptr_add(data_section
, len
);
3619 memcpy(ptr
, funcname
, len
);
3624 #ifdef TCC_TARGET_PE
3625 t
= VT_SHORT
| VT_UNSIGNED
;
3631 /* string parsing */
3634 if (tcc_state
->warn_write_strings
)
3639 memset(&ad
, 0, sizeof(AttributeDef
));
3640 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, NULL
, 0);
3645 if (parse_btype(&type
, &ad
)) {
3646 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
3648 /* check ISOC99 compound literal */
3650 /* data is allocated locally by default */
3655 /* all except arrays are lvalues */
3656 if (!(type
.t
& VT_ARRAY
))
3657 r
|= lvalue_type(type
.t
);
3658 memset(&ad
, 0, sizeof(AttributeDef
));
3659 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, NULL
, 0);
3661 if (sizeof_caller
) {
3668 } else if (tok
== '{') {
3669 /* save all registers */
3671 /* statement expression : we do not accept break/continue
3672 inside as GCC does */
3673 block(NULL
, NULL
, NULL
, NULL
, 0, 1);
3688 /* functions names must be treated as function pointers,
3689 except for unary '&' and sizeof. Since we consider that
3690 functions are not lvalues, we only have to handle it
3691 there and in function calls. */
3692 /* arrays can also be used although they are not lvalues */
3693 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
3694 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
3696 mk_pointer(&vtop
->type
);
3702 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3704 boolean
.t
= VT_BOOL
;
3706 vtop
->c
.i
= !vtop
->c
.i
;
3707 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
3708 vtop
->c
.i
= vtop
->c
.i
^ 1;
3711 vseti(VT_JMP
, gvtst(1, 0));
3723 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
3724 tcc_error("pointer not accepted for unary plus");
3725 /* In order to force cast, we add zero, except for floating point
3726 where we really need an noop (otherwise -0.0 will be transformed
3728 if (!is_float(vtop
->type
.t
)) {
3739 unary_type(&type
); // Perform a in_sizeof = 0;
3740 size
= type_size(&type
, &align
);
3741 if (t
== TOK_SIZEOF
) {
3742 if (!(type
.t
& VT_VLA
)) {
3744 tcc_error("sizeof applied to an incomplete type");
3747 vla_runtime_type_size(&type
, &align
);
3752 vtop
->type
.t
|= VT_UNSIGNED
;
3755 case TOK_builtin_types_compatible_p
:
3764 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3765 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3766 vpushi(is_compatible_types(&type1
, &type2
));
3769 case TOK_builtin_constant_p
:
3771 int saved_nocode_wanted
, res
;
3774 saved_nocode_wanted
= nocode_wanted
;
3777 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
3779 nocode_wanted
= saved_nocode_wanted
;
3784 case TOK_builtin_frame_address
:
3790 if (tok
!= TOK_CINT
|| tokc
.i
< 0) {
3791 tcc_error("__builtin_frame_address only takes positive integers");
3798 vset(&type
, VT_LOCAL
, 0); /* local frame */
3800 mk_pointer(&vtop
->type
);
3801 indir(); /* -> parent frame */
3805 #ifdef TCC_TARGET_X86_64
3806 #ifdef TCC_TARGET_PE
3807 case TOK_builtin_va_start
:
3815 if ((vtop
->r
& VT_VALMASK
) != VT_LOCAL
)
3816 tcc_error("__builtin_va_start expects a local variable");
3817 vtop
->r
&= ~(VT_LVAL
| VT_REF
);
3818 vtop
->type
= char_pointer_type
;
3823 case TOK_builtin_va_arg_types
:
3830 vpushi(classify_x86_64_va_arg(&type
));
3845 t
= vtop
->type
.t
& VT_BTYPE
;
3847 /* In IEEE negate(x) isn't subtract(0,x), but rather
3852 else if (t
== VT_DOUBLE
)
3863 goto tok_identifier
;
3865 /* allow to take the address of a label */
3866 if (tok
< TOK_UIDENT
)
3867 expect("label identifier");
3868 s
= label_find(tok
);
3870 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
3872 if (s
->r
== LABEL_DECLARED
)
3873 s
->r
= LABEL_FORWARD
;
3876 s
->type
.t
= VT_VOID
;
3877 mk_pointer(&s
->type
);
3878 s
->type
.t
|= VT_STATIC
;
3880 vpushsym(&s
->type
, s
);
3884 // special qnan , snan and infinity values
3886 vpush64(VT_DOUBLE
, 0x7ff8000000000000ULL
);
3890 vpush64(VT_DOUBLE
, 0x7ff0000000000001ULL
);
3894 vpush64(VT_DOUBLE
, 0x7ff0000000000000ULL
);
3903 expect("identifier");
3906 const char *name
= get_tok_str(t
, NULL
);
3908 tcc_error("'%s' undeclared", name
);
3909 /* for simple function calls, we tolerate undeclared
3910 external reference to int() function */
3911 if (tcc_state
->warn_implicit_function_declaration
3912 #ifdef TCC_TARGET_PE
3913 /* people must be warned about using undeclared WINAPI functions
3914 (which usually start with uppercase letter) */
3915 || (name
[0] >= 'A' && name
[0] <= 'Z')
3918 tcc_warning("implicit declaration of function '%s'", name
);
3919 s
= external_global_sym(t
, &func_old_type
, 0);
3921 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
3922 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
3923 /* if referencing an inline function, then we generate a
3924 symbol to it if not already done. It will have the
3925 effect to generate code for it at the end of the
3926 compilation unit. Inline function as always
3927 generated in the text section. */
3929 put_extern_sym(s
, text_section
, 0, 0);
3930 r
= VT_SYM
| VT_CONST
;
3934 vset(&s
->type
, r
, s
->c
);
3935 /* if forward reference, we must point to s */
3936 if (vtop
->r
& VT_SYM
) {
3938 vtop
->c
.ptr_offset
= 0;
3943 /* post operations */
3945 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
3948 } else if (tok
== '.' || tok
== TOK_ARROW
) {
3951 if (tok
== TOK_ARROW
)
3953 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
3957 /* expect pointer on structure */
3958 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
3959 expect("struct or union");
3963 while ((s
= s
->next
) != NULL
) {
3968 tcc_error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, NULL
));
3969 /* add field offset to pointer */
3970 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
3973 /* change type to field type, and set to lvalue */
3974 vtop
->type
= s
->type
;
3975 vtop
->type
.t
|= qualifiers
;
3976 /* an array is never an lvalue */
3977 if (!(vtop
->type
.t
& VT_ARRAY
)) {
3978 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3979 #ifdef CONFIG_TCC_BCHECK
3980 /* if bound checking, the referenced pointer must be checked */
3981 if (tcc_state
->do_bounds_check
)
3982 vtop
->r
|= VT_MUSTBOUND
;
3986 } else if (tok
== '[') {
3992 } else if (tok
== '(') {
3995 int nb_args
, ret_nregs
, ret_align
, variadic
;
3998 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3999 /* pointer test (no array accepted) */
4000 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
4001 vtop
->type
= *pointed_type(&vtop
->type
);
4002 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
4006 expect("function pointer");
4009 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
4011 /* get return type */
4014 sa
= s
->next
; /* first parameter */
4017 /* compute first implicit argument if a structure is returned */
4018 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
4019 variadic
= (s
->c
== FUNC_ELLIPSIS
);
4020 ret_nregs
= gfunc_sret(&s
->type
, variadic
, &ret
.type
,
4023 /* get some space for the returned structure */
4024 size
= type_size(&s
->type
, &align
);
4025 loc
= (loc
- size
) & -align
;
4027 ret
.r
= VT_LOCAL
| VT_LVAL
;
4028 /* pass it as 'int' to avoid structure arg passing
4030 vseti(VT_LOCAL
, loc
);
4040 /* return in register */
4041 if (is_float(ret
.type
.t
)) {
4042 ret
.r
= reg_fret(ret
.type
.t
);
4043 #ifdef TCC_TARGET_X86_64
4044 if ((ret
.type
.t
& VT_BTYPE
) == VT_QFLOAT
)
4048 #ifdef TCC_TARGET_X86_64
4049 if ((ret
.type
.t
& VT_BTYPE
) == VT_QLONG
)
4051 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
4061 gfunc_param_typed(s
, sa
);
4071 tcc_error("too few arguments to function");
4073 if (!nocode_wanted
) {
4074 gfunc_call(nb_args
);
4076 vtop
-= (nb_args
+ 1);
4080 for (r
= ret
.r
+ ret_nregs
+ !ret_nregs
; r
-- > ret
.r
;) {
4081 vsetc(&ret
.type
, r
, &ret
.c
);
4082 vtop
->r2
= ret
.r2
; /* Loop only happens when r2 is VT_CONST */
4085 /* handle packed struct return */
4086 if (((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) && ret_nregs
) {
4089 size
= type_size(&s
->type
, &align
);
4090 loc
= (loc
- size
) & -align
;
4094 vset(&ret
.type
, VT_LOCAL
| VT_LVAL
, addr
+ offset
);
4098 if (--ret_nregs
== 0)
4100 /* XXX: compatible with arm only: ret_align == register_size */
4101 offset
+= ret_align
;
4103 vset(&s
->type
, VT_LOCAL
| VT_LVAL
, addr
);
4111 ST_FUNC
void expr_prod(void)
4116 while (tok
== '*' || tok
== '/' || tok
== '%') {
4124 ST_FUNC
void expr_sum(void)
4129 while (tok
== '+' || tok
== '-') {
4137 static void expr_shift(void)
4142 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
4150 static void expr_cmp(void)
4155 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
4156 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
4164 static void expr_cmpeq(void)
4169 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
4177 static void expr_and(void)
4180 while (tok
== '&') {
4187 static void expr_xor(void)
4190 while (tok
== '^') {
4197 static void expr_or(void)
4200 while (tok
== '|') {
4207 /* XXX: fix this mess */
4208 static void expr_land_const(void)
4211 while (tok
== TOK_LAND
) {
4218 /* XXX: fix this mess */
4219 static void expr_lor_const(void)
4222 while (tok
== TOK_LOR
) {
4229 /* only used if non constant */
4230 static void expr_land(void)
4235 if (tok
== TOK_LAND
) {
4240 if (tok
!= TOK_LAND
) {
4250 static void expr_lor(void)
4255 if (tok
== TOK_LOR
) {
4260 if (tok
!= TOK_LOR
) {
4270 /* XXX: better constant handling */
4271 static void expr_cond(void)
4273 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
;
4275 CType type
, type1
, type2
;
4282 boolean
.t
= VT_BOOL
;
4288 if (tok
!= ':' || !gnu_ext
) {
4303 if (vtop
!= vstack
) {
4304 /* needed to avoid having different registers saved in
4306 if (is_float(vtop
->type
.t
)) {
4308 #ifdef TCC_TARGET_X86_64
4309 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4319 if (tok
== ':' && gnu_ext
) {
4327 sv
= *vtop
; /* save value to handle it later */
4328 vtop
--; /* no vpop so that FP stack is not flushed */
4336 bt1
= t1
& VT_BTYPE
;
4338 bt2
= t2
& VT_BTYPE
;
4339 /* cast operands to correct type according to ISOC rules */
4340 if (is_float(bt1
) || is_float(bt2
)) {
4341 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
4342 type
.t
= VT_LDOUBLE
;
4343 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
4348 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
4349 /* cast to biggest op */
4351 /* convert to unsigned if it does not fit in a long long */
4352 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
4353 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
4354 type
.t
|= VT_UNSIGNED
;
4355 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
4356 /* If one is a null ptr constant the result type
4358 if (is_null_pointer (vtop
))
4360 else if (is_null_pointer (&sv
))
4362 /* XXX: test pointer compatibility, C99 has more elaborate
4366 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
4367 /* XXX: test function pointer compatibility */
4368 type
= bt1
== VT_FUNC
? type1
: type2
;
4369 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
4370 /* XXX: test structure compatibility */
4371 type
= bt1
== VT_STRUCT
? type1
: type2
;
4372 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
4373 /* NOTE: as an extension, we accept void on only one side */
4376 /* integer operations */
4378 /* convert to unsigned if it does not fit in an integer */
4379 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
4380 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
4381 type
.t
|= VT_UNSIGNED
;
4384 /* now we convert second operand */
4386 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4389 if (is_float(type
.t
)) {
4391 #ifdef TCC_TARGET_X86_64
4392 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4396 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
4397 /* for long longs, we use fixed registers to avoid having
4398 to handle a complicated move */
4403 /* this is horrible, but we must also convert first
4407 /* put again first value and cast it */
4410 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4413 move_reg(r2
, r1
, type
.t
);
4420 static void expr_eq(void)
4426 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
4427 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
4428 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
4443 ST_FUNC
void gexpr(void)
4454 /* parse an expression and return its type without any side effect. */
4455 static void expr_type(CType
*type
)
4457 int saved_nocode_wanted
;
4459 saved_nocode_wanted
= nocode_wanted
;
4464 nocode_wanted
= saved_nocode_wanted
;
4467 /* parse a unary expression and return its type without any side
4469 static void unary_type(CType
*type
)
4481 /* parse a constant expression and return value in vtop. */
4482 static void expr_const1(void)
4491 /* parse an integer constant and return its value. */
4492 ST_FUNC
int expr_const(void)
4496 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
4497 expect("constant expression");
4503 /* return the label token if current token is a label, otherwise
4505 static int is_label(void)
4509 /* fast test first */
4510 if (tok
< TOK_UIDENT
)
4512 /* no need to save tokc because tok is an identifier */
4519 unget_tok(last_tok
);
4524 static void label_or_decl(int l
)
4528 /* fast test first */
4529 if (tok
>= TOK_UIDENT
)
4531 /* no need to save tokc because tok is an identifier */
4535 unget_tok(last_tok
);
4538 unget_tok(last_tok
);
4543 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
4544 int case_reg
, int is_expr
)
4547 Sym
*s
, *frame_bottom
;
4549 /* generate line number info */
4550 if (tcc_state
->do_debug
&&
4551 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
4552 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
4554 last_line_num
= file
->line_num
;
4558 /* default return value is (void) */
4560 vtop
->type
.t
= VT_VOID
;
4563 if (tok
== TOK_IF
) {
4570 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4572 if (c
== TOK_ELSE
) {
4576 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4577 gsym(d
); /* patch else jmp */
4580 } else if (tok
== TOK_WHILE
) {
4588 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4592 } else if (tok
== '{') {
4594 int block_vla_sp_loc
, *saved_vla_sp_loc
, saved_vla_flags
;
4597 /* record local declaration stack position */
4599 frame_bottom
= sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4600 frame_bottom
->next
= scope_stack_bottom
;
4601 scope_stack_bottom
= frame_bottom
;
4602 llabel
= local_label_stack
;
4604 /* save VLA state */
4605 block_vla_sp_loc
= *(saved_vla_sp_loc
= vla_sp_loc
);
4606 if (saved_vla_sp_loc
!= &vla_sp_root_loc
)
4607 vla_sp_loc
= &block_vla_sp_loc
;
4609 saved_vla_flags
= vla_flags
;
4610 vla_flags
|= VLA_NEED_NEW_FRAME
;
4612 /* handle local labels declarations */
4613 if (tok
== TOK_LABEL
) {
4616 if (tok
< TOK_UIDENT
)
4617 expect("label identifier");
4618 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
4628 while (tok
!= '}') {
4629 label_or_decl(VT_LOCAL
);
4633 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4636 /* pop locally defined labels */
4637 label_pop(&local_label_stack
, llabel
);
4639 /* XXX: this solution makes only valgrind happy...
4640 triggered by gcc.c-torture/execute/20000917-1.c */
4642 switch(vtop
->type
.t
& VT_BTYPE
) {
4647 for(p
=vtop
->type
.ref
;p
;p
=p
->prev
)
4649 tcc_error("unsupported expression type");
4652 /* pop locally defined symbols */
4653 scope_stack_bottom
= scope_stack_bottom
->next
;
4654 sym_pop(&local_stack
, s
);
4656 /* Pop VLA frames and restore stack pointer if required */
4657 if (saved_vla_sp_loc
!= &vla_sp_root_loc
)
4658 *saved_vla_sp_loc
= block_vla_sp_loc
;
4659 if (vla_sp_loc
!= (saved_vla_sp_loc
== &vla_sp_root_loc
? &vla_sp_root_loc
: &block_vla_sp_loc
)) {
4660 vla_sp_loc
= saved_vla_sp_loc
;
4661 gen_vla_sp_restore(*vla_sp_loc
);
4663 vla_flags
= (vla_flags
& ~VLA_SCOPE_FLAGS
) | (saved_vla_flags
& VLA_SCOPE_FLAGS
);
4666 } else if (tok
== TOK_RETURN
) {
4670 gen_assign_cast(&func_vt
);
4671 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
4672 CType type
, ret_type
;
4673 int ret_align
, ret_nregs
;
4674 ret_nregs
= gfunc_sret(&func_vt
, func_var
, &ret_type
,
4676 if (0 == ret_nregs
) {
4677 /* if returning structure, must copy it to implicit
4678 first pointer arg location */
4681 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
4684 /* copy structure value to pointer */
4687 /* returning structure packed into registers */
4688 int r
, size
, addr
, align
;
4689 size
= type_size(&func_vt
,&align
);
4690 if ((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) || (vtop
->c
.i
& (ret_align
-1)))
4691 && (align
& (ret_align
-1))) {
4692 loc
= (loc
- size
) & -align
;
4695 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
4698 vset(&ret_type
, VT_LOCAL
| VT_LVAL
, addr
);
4700 vtop
->type
= ret_type
;
4701 if (is_float(ret_type
.t
))
4702 r
= rc_fret(ret_type
.t
);
4708 if (--ret_nregs
== 0)
4710 /* We assume that when a structure is returned in multiple
4711 registers, their classes are consecutive values of the
4714 /* XXX: compatible with arm only: ret_align == register_size */
4715 vtop
->c
.i
+= ret_align
;
4716 vtop
->r
= VT_LOCAL
| VT_LVAL
;
4719 } else if (is_float(func_vt
.t
)) {
4720 gv(rc_fret(func_vt
.t
));
4724 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
4727 rsym
= gjmp(rsym
); /* jmp */
4728 } else if (tok
== TOK_BREAK
) {
4731 tcc_error("cannot break");
4732 *bsym
= gjmp(*bsym
);
4735 } else if (tok
== TOK_CONTINUE
) {
4738 tcc_error("cannot continue");
4739 *csym
= gjmp(*csym
);
4742 } else if (tok
== TOK_FOR
) {
4747 frame_bottom
= sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4748 frame_bottom
->next
= scope_stack_bottom
;
4749 scope_stack_bottom
= frame_bottom
;
4751 /* c99 for-loop init decl? */
4752 if (!decl0(VT_LOCAL
, 1)) {
4753 /* no, regular for-loop init expr */
4777 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4781 scope_stack_bottom
= scope_stack_bottom
->next
;
4782 sym_pop(&local_stack
, s
);
4784 if (tok
== TOK_DO
) {
4789 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4800 if (tok
== TOK_SWITCH
) {
4804 /* XXX: other types than integer */
4805 case_reg
= gv(RC_INT
);
4809 b
= gjmp(0); /* jump to first case */
4811 block(&a
, csym
, &b
, &c
, case_reg
, 0);
4812 /* if no default, jmp after switch */
4820 if (tok
== TOK_CASE
) {
4827 if (gnu_ext
&& tok
== TOK_DOTS
) {
4831 tcc_warning("empty case range");
4833 /* since a case is like a label, we must skip it with a jmp */
4840 *case_sym
= gtst(1, 0);
4843 *case_sym
= gtst(1, 0);
4847 *case_sym
= gtst(1, *case_sym
);
4852 goto block_after_label
;
4854 if (tok
== TOK_DEFAULT
) {
4860 tcc_error("too many 'default'");
4863 goto block_after_label
;
4865 if (tok
== TOK_GOTO
) {
4867 if (tok
== '*' && gnu_ext
) {
4871 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
4874 } else if (tok
>= TOK_UIDENT
) {
4875 s
= label_find(tok
);
4876 /* put forward definition if needed */
4878 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4880 if (s
->r
== LABEL_DECLARED
)
4881 s
->r
= LABEL_FORWARD
;
4883 /* label already defined */
4884 if (vla_flags
& VLA_IN_SCOPE
) {
4885 /* If VLAs are in use, save the current stack pointer and
4886 reset the stack pointer to what it was at function entry
4887 (label will restore stack pointer in inner scopes) */
4889 gen_vla_sp_restore(vla_sp_root_loc
);
4891 if (s
->r
& LABEL_FORWARD
)
4892 s
->jnext
= gjmp(s
->jnext
);
4894 gjmp_addr(s
->jnext
);
4897 expect("label identifier");
4900 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
4906 if (vla_flags
& VLA_IN_SCOPE
) {
4907 /* save/restore stack pointer across label
4908 this is a no-op when combined with the load immediately
4909 after the label unless we arrive via goto */
4914 if (s
->r
== LABEL_DEFINED
)
4915 tcc_error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
4917 s
->r
= LABEL_DEFINED
;
4919 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
4922 if (vla_flags
& VLA_IN_SCOPE
) {
4923 gen_vla_sp_restore(*vla_sp_loc
);
4924 vla_flags
|= VLA_NEED_NEW_FRAME
;
4926 /* we accept this, but it is a mistake */
4929 tcc_warning("deprecated use of label at end of compound statement");
4933 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4936 /* expression case */
4951 /* t is the array or struct type. c is the array or struct
4952 address. cur_index/cur_field is the pointer to the current
4953 value. 'size_only' is true if only size info is needed (only used
4955 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
4956 int *cur_index
, Sym
**cur_field
,
4960 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
4966 if (gnu_ext
&& (l
= is_label()) != 0)
4968 while (tok
== '[' || tok
== '.') {
4970 if (!(type
->t
& VT_ARRAY
))
4971 expect("array type");
4974 index
= expr_const();
4975 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
4976 expect("invalid index");
4977 if (tok
== TOK_DOTS
&& gnu_ext
) {
4979 index_last
= expr_const();
4980 if (index_last
< 0 ||
4981 (s
->c
>= 0 && index_last
>= s
->c
) ||
4983 expect("invalid index");
4989 *cur_index
= index_last
;
4990 type
= pointed_type(type
);
4991 elem_size
= type_size(type
, &align
);
4992 c
+= index
* elem_size
;
4993 /* NOTE: we only support ranges for last designator */
4994 nb_elems
= index_last
- index
+ 1;
4995 if (nb_elems
!= 1) {
5004 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
5005 expect("struct/union type");
5018 /* XXX: fix this mess by using explicit storage field */
5020 type1
.t
|= (type
->t
& ~VT_TYPE
);
5034 if (type
->t
& VT_ARRAY
) {
5036 type
= pointed_type(type
);
5037 c
+= index
* type_size(type
, &align
);
5041 tcc_error("too many field init");
5042 /* XXX: fix this mess by using explicit storage field */
5044 type1
.t
|= (type
->t
& ~VT_TYPE
);
5049 decl_initializer(type
, sec
, c
, 0, size_only
);
5051 /* XXX: make it more general */
5052 if (!size_only
&& nb_elems
> 1) {
5053 unsigned long c_end
;
5058 tcc_error("range init not supported yet for dynamic storage");
5059 c_end
= c
+ nb_elems
* elem_size
;
5060 if (c_end
> sec
->data_allocated
)
5061 section_realloc(sec
, c_end
);
5062 src
= sec
->data
+ c
;
5064 for(i
= 1; i
< nb_elems
; i
++) {
5066 memcpy(dst
, src
, elem_size
);
5072 #define EXPR_CONST 1
5075 /* store a value or an expression directly in global data or in local array */
5076 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
5077 int v
, int expr_type
)
5079 int saved_global_expr
, bt
, bit_pos
, bit_size
;
5081 unsigned long long bit_mask
;
5089 /* compound literals must be allocated globally in this case */
5090 saved_global_expr
= global_expr
;
5093 global_expr
= saved_global_expr
;
5094 /* NOTE: symbols are accepted */
5095 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
5096 tcc_error("initializer element is not constant");
5104 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
5107 /* XXX: not portable */
5108 /* XXX: generate error if incorrect relocation */
5109 gen_assign_cast(&dtype
);
5110 bt
= type
->t
& VT_BTYPE
;
5111 /* we'll write at most 12 bytes */
5112 if (c
+ 12 > sec
->data_allocated
) {
5113 section_realloc(sec
, c
+ 12);
5115 ptr
= sec
->data
+ c
;
5116 /* XXX: make code faster ? */
5117 if (!(type
->t
& VT_BITFIELD
)) {
5122 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5123 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
5124 bit_mask
= (1LL << bit_size
) - 1;
5126 if ((vtop
->r
& VT_SYM
) &&
5132 (bt
== VT_INT
&& bit_size
!= 32)))
5133 tcc_error("initializer element is not computable at load time");
5136 vtop
->c
.i
= (vtop
->c
.i
!= 0);
5138 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5141 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5144 *(double *)ptr
= vtop
->c
.d
;
5147 *(long double *)ptr
= vtop
->c
.ld
;
5150 *(long long *)ptr
|= (vtop
->c
.ll
& bit_mask
) << bit_pos
;
5153 if (vtop
->r
& VT_SYM
) {
5154 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
5156 *(addr_t
*)ptr
|= (vtop
->c
.ptr_offset
& bit_mask
) << bit_pos
;
5159 if (vtop
->r
& VT_SYM
) {
5160 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
5162 *(int *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5167 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
5174 /* put zeros for variable based init */
5175 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
5178 /* nothing to do because globals are already set to zero */
5180 vpush_global_sym(&func_old_type
, TOK_memset
);
5182 #ifdef TCC_TARGET_ARM
5193 /* 't' contains the type and storage info. 'c' is the offset of the
5194 object in section 'sec'. If 'sec' is NULL, it means stack based
5195 allocation. 'first' is true if array '{' must be read (multi
5196 dimension implicit array init handling). 'size_only' is true if
5197 size only evaluation is wanted (only for arrays). */
5198 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
5199 int first
, int size_only
)
5201 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, parlevel1
, i
;
5202 int size1
, align1
, expr_type
;
5206 if (type
->t
& VT_VLA
) {
5209 /* save current stack pointer */
5210 if (vla_flags
& VLA_NEED_NEW_FRAME
) {
5212 vla_flags
= VLA_IN_SCOPE
;
5213 vla_sp_loc
= &vla_sp_loc_tmp
;
5216 vla_runtime_type_size(type
, &a
);
5217 gen_vla_alloc(type
, a
);
5218 vset(type
, VT_LOCAL
|VT_LVAL
, c
);
5222 } else if (type
->t
& VT_ARRAY
) {
5226 t1
= pointed_type(type
);
5227 size1
= type_size(t1
, &align1
);
5230 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
5233 tcc_error("character array initializer must be a literal,"
5234 " optionally enclosed in braces");
5239 /* only parse strings here if correct type (otherwise: handle
5240 them as ((w)char *) expressions */
5241 if ((tok
== TOK_LSTR
&&
5242 #ifdef TCC_TARGET_PE
5243 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
5245 (t1
->t
& VT_BTYPE
) == VT_INT
5247 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
5248 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5253 /* compute maximum number of chars wanted */
5255 cstr_len
= cstr
->size
;
5257 cstr_len
= cstr
->size
/ sizeof(nwchar_t
);
5260 if (n
>= 0 && nb
> (n
- array_length
))
5261 nb
= n
- array_length
;
5264 tcc_warning("initializer-string for array is too long");
5265 /* in order to go faster for common case (char
5266 string in global variable, we handle it
5268 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
5269 memcpy(sec
->data
+ c
+ array_length
, cstr
->data
, nb
);
5273 ch
= ((unsigned char *)cstr
->data
)[i
];
5275 ch
= ((nwchar_t
*)cstr
->data
)[i
];
5276 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
5284 /* only add trailing zero if enough storage (no
5285 warning in this case since it is standard) */
5286 if (n
< 0 || array_length
< n
) {
5288 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
5294 while (tok
!= '}') {
5295 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
5296 if (n
>= 0 && index
>= n
)
5297 tcc_error("index too large");
5298 /* must put zero in holes (note that doing it that way
5299 ensures that it even works with designators) */
5300 if (!size_only
&& array_length
< index
) {
5301 init_putz(t1
, sec
, c
+ array_length
* size1
,
5302 (index
- array_length
) * size1
);
5305 if (index
> array_length
)
5306 array_length
= index
;
5307 /* special test for multi dimensional arrays (may not
5308 be strictly correct if designators are used at the
5310 if (index
>= n
&& no_oblock
)
5319 /* put zeros at the end */
5320 if (!size_only
&& n
>= 0 && array_length
< n
) {
5321 init_putz(t1
, sec
, c
+ array_length
* size1
,
5322 (n
- array_length
) * size1
);
5324 /* patch type size if needed */
5326 s
->c
= array_length
;
5327 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
5328 (sec
|| !first
|| tok
== '{')) {
5331 /* NOTE: the previous test is a specific case for automatic
5332 struct/union init */
5333 /* XXX: union needs only one init */
5335 /* XXX: this test is incorrect for local initializers
5336 beginning with ( without {. It would be much more difficult
5337 to do it correctly (ideally, the expression parser should
5338 be used in all cases) */
5344 while (tok
== '(') {
5348 if (!parse_btype(&type1
, &ad1
))
5350 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
5352 if (!is_assignable_types(type
, &type1
))
5353 tcc_error("invalid type for cast");
5358 if (first
|| tok
== '{') {
5367 while (tok
!= '}') {
5368 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
5370 if (!size_only
&& array_length
< index
) {
5371 init_putz(type
, sec
, c
+ array_length
,
5372 index
- array_length
);
5374 index
= index
+ type_size(&f
->type
, &align1
);
5375 if (index
> array_length
)
5376 array_length
= index
;
5378 /* gr: skip fields from same union - ugly. */
5380 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
5381 /* test for same offset */
5382 if (f
->next
->c
!= f
->c
)
5384 /* if yes, test for bitfield shift */
5385 if ((f
->type
.t
& VT_BITFIELD
) && (f
->next
->type
.t
& VT_BITFIELD
)) {
5386 int bit_pos_1
= (f
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5387 int bit_pos_2
= (f
->next
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5388 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
5389 if (bit_pos_1
!= bit_pos_2
)
5396 if (no_oblock
&& f
== NULL
)
5402 /* put zeros at the end */
5403 if (!size_only
&& array_length
< n
) {
5404 init_putz(type
, sec
, c
+ array_length
,
5413 } else if (tok
== '{') {
5415 decl_initializer(type
, sec
, c
, first
, size_only
);
5417 } else if (size_only
) {
5418 /* just skip expression */
5419 parlevel
= parlevel1
= 0;
5420 while ((parlevel
> 0 || parlevel1
> 0 ||
5421 (tok
!= '}' && tok
!= ',')) && tok
!= -1) {
5424 else if (tok
== ')')
5426 else if (tok
== '{')
5428 else if (tok
== '}')
5433 /* currently, we always use constant expression for globals
5434 (may change for scripting case) */
5435 expr_type
= EXPR_CONST
;
5437 expr_type
= EXPR_ANY
;
5438 init_putv(type
, sec
, c
, 0, expr_type
);
5442 /* parse an initializer for type 't' if 'has_init' is non zero, and
5443 allocate space in local or global data space ('r' is either
5444 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5445 variable 'v' with an associated name represented by 'asm_label' of
5446 scope 'scope' is declared before initializers are parsed. If 'v' is
5447 zero, then a reference to the new object is put in the value stack.
5448 If 'has_init' is 2, a special parsing is done to handle string
5450 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
5451 int has_init
, int v
, char *asm_label
,
5454 int size
, align
, addr
, data_offset
;
5456 ParseState saved_parse_state
= {0};
5457 TokenString init_str
;
5459 Sym
*flexible_array
;
5461 flexible_array
= NULL
;
5462 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
5463 Sym
*field
= type
->ref
->next
;
5466 field
= field
->next
;
5467 if (field
->type
.t
& VT_ARRAY
&& field
->type
.ref
->c
< 0)
5468 flexible_array
= field
;
5472 size
= type_size(type
, &align
);
5473 /* If unknown size, we must evaluate it before
5474 evaluating initializers because
5475 initializers can generate global data too
5476 (e.g. string pointers or ISOC99 compound
5477 literals). It also simplifies local
5478 initializers handling */
5479 tok_str_new(&init_str
);
5480 if (size
< 0 || (flexible_array
&& has_init
)) {
5482 tcc_error("unknown type size");
5483 /* get all init string */
5484 if (has_init
== 2) {
5485 /* only get strings */
5486 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5487 tok_str_add_tok(&init_str
);
5492 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
5494 tcc_error("unexpected end of file in initializer");
5495 tok_str_add_tok(&init_str
);
5498 else if (tok
== '}') {
5508 tok_str_add(&init_str
, -1);
5509 tok_str_add(&init_str
, 0);
5512 save_parse_state(&saved_parse_state
);
5514 macro_ptr
= init_str
.str
;
5516 decl_initializer(type
, NULL
, 0, 1, 1);
5517 /* prepare second initializer parsing */
5518 macro_ptr
= init_str
.str
;
5521 /* if still unknown size, error */
5522 size
= type_size(type
, &align
);
5524 tcc_error("unknown type size");
5527 size
+= flexible_array
->type
.ref
->c
* pointed_size(&flexible_array
->type
);
5528 /* take into account specified alignment if bigger */
5529 if (ad
->a
.aligned
) {
5530 if (ad
->a
.aligned
> align
)
5531 align
= ad
->a
.aligned
;
5532 } else if (ad
->a
.packed
) {
5535 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
5537 #ifdef CONFIG_TCC_BCHECK
5538 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5542 loc
= (loc
- size
) & -align
;
5544 #ifdef CONFIG_TCC_BCHECK
5545 /* handles bounds */
5546 /* XXX: currently, since we do only one pass, we cannot track
5547 '&' operators, so we add only arrays */
5548 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5549 unsigned long *bounds_ptr
;
5550 /* add padding between regions */
5552 /* then add local bound info */
5553 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(unsigned long));
5554 bounds_ptr
[0] = addr
;
5555 bounds_ptr
[1] = size
;
5559 /* local variable */
5560 sym_push(v
, type
, r
, addr
);
5562 /* push local reference */
5563 vset(type
, r
, addr
);
5569 if (v
&& scope
== VT_CONST
) {
5570 /* see if the symbol was already defined */
5573 if (!is_compatible_types(&sym
->type
, type
))
5574 tcc_error("incompatible types for redefinition of '%s'",
5575 get_tok_str(v
, NULL
));
5576 if (sym
->type
.t
& VT_EXTERN
) {
5577 /* if the variable is extern, it was not allocated */
5578 sym
->type
.t
&= ~VT_EXTERN
;
5579 /* set array size if it was omitted in extern
5581 if ((sym
->type
.t
& VT_ARRAY
) &&
5582 sym
->type
.ref
->c
< 0 &&
5584 sym
->type
.ref
->c
= type
->ref
->c
;
5586 /* we accept several definitions of the same
5587 global variable. this is tricky, because we
5588 must play with the SHN_COMMON type of the symbol */
5589 /* XXX: should check if the variable was already
5590 initialized. It is incorrect to initialized it
5592 /* no init data, we won't add more to the symbol */
5599 /* allocate symbol in corresponding section */
5604 else if (tcc_state
->nocommon
)
5608 data_offset
= sec
->data_offset
;
5609 data_offset
= (data_offset
+ align
- 1) & -align
;
5611 /* very important to increment global pointer at this time
5612 because initializers themselves can create new initializers */
5613 data_offset
+= size
;
5614 #ifdef CONFIG_TCC_BCHECK
5615 /* add padding if bound check */
5616 if (tcc_state
->do_bounds_check
)
5619 sec
->data_offset
= data_offset
;
5620 /* allocate section space to put the data */
5621 if (sec
->sh_type
!= SHT_NOBITS
&&
5622 data_offset
> sec
->data_allocated
)
5623 section_realloc(sec
, data_offset
);
5624 /* align section if needed */
5625 if (align
> sec
->sh_addralign
)
5626 sec
->sh_addralign
= align
;
5628 addr
= 0; /* avoid warning */
5632 if (scope
!= VT_CONST
|| !sym
) {
5633 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
5634 sym
->asm_label
= asm_label
;
5636 /* update symbol definition */
5638 put_extern_sym(sym
, sec
, addr
, size
);
5641 /* put a common area */
5642 put_extern_sym(sym
, NULL
, align
, size
);
5643 /* XXX: find a nicer way */
5644 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
5645 esym
->st_shndx
= SHN_COMMON
;
5648 /* push global reference */
5649 sym
= get_sym_ref(type
, sec
, addr
, size
);
5650 vpushsym(type
, sym
);
5652 /* patch symbol weakness */
5653 if (type
->t
& VT_WEAK
)
5655 #ifdef CONFIG_TCC_BCHECK
5656 /* handles bounds now because the symbol must be defined
5657 before for the relocation */
5658 if (tcc_state
->do_bounds_check
) {
5659 unsigned long *bounds_ptr
;
5661 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
5662 /* then add global bound info */
5663 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(long));
5664 bounds_ptr
[0] = 0; /* relocated */
5665 bounds_ptr
[1] = size
;
5669 if (has_init
|| (type
->t
& VT_VLA
)) {
5670 decl_initializer(type
, sec
, addr
, 1, 0);
5671 /* restore parse state if needed */
5673 tok_str_free(init_str
.str
);
5674 restore_parse_state(&saved_parse_state
);
5676 /* patch flexible array member size back to -1, */
5677 /* for possible subsequent similar declarations */
5679 flexible_array
->type
.ref
->c
= -1;
5684 static void put_func_debug(Sym
*sym
)
5689 /* XXX: we put here a dummy type */
5690 snprintf(buf
, sizeof(buf
), "%s:%c1",
5691 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
5692 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
5693 cur_text_section
, sym
->c
);
5694 /* //gr gdb wants a line at the function */
5695 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
5700 /* parse an old style function declaration list */
5701 /* XXX: check multiple parameter */
5702 static void func_decl_list(Sym
*func_sym
)
5709 /* parse each declaration */
5710 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
&&
5711 tok
!= TOK_ASM1
&& tok
!= TOK_ASM2
&& tok
!= TOK_ASM3
) {
5712 if (!parse_btype(&btype
, &ad
))
5713 expect("declaration list");
5714 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5715 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5717 /* we accept no variable after */
5721 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5722 /* find parameter in function parameter list */
5725 if ((s
->v
& ~SYM_FIELD
) == v
)
5729 tcc_error("declaration for parameter '%s' but no such parameter",
5730 get_tok_str(v
, NULL
));
5732 /* check that no storage specifier except 'register' was given */
5733 if (type
.t
& VT_STORAGE
)
5734 tcc_error("storage class specified for '%s'", get_tok_str(v
, NULL
));
5735 convert_parameter_type(&type
);
5736 /* we can add the type (NOTE: it could be local to the function) */
5738 /* accept other parameters */
5749 /* parse a function defined by symbol 'sym' and generate its code in
5750 'cur_text_section' */
5751 static void gen_function(Sym
*sym
)
5753 int saved_nocode_wanted
= nocode_wanted
;
5755 ind
= cur_text_section
->data_offset
;
5756 /* NOTE: we patch the symbol size later */
5757 put_extern_sym(sym
, cur_text_section
, ind
, 0);
5758 funcname
= get_tok_str(sym
->v
, NULL
);
5760 /* Initialize VLA state */
5761 vla_sp_loc
= &vla_sp_root_loc
;
5762 vla_flags
= VLA_NEED_NEW_FRAME
;
5763 /* put debug symbol */
5764 if (tcc_state
->do_debug
)
5765 put_func_debug(sym
);
5766 /* push a dummy symbol to enable local sym storage */
5767 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
5768 gfunc_prolog(&sym
->type
);
5769 #ifdef CONFIG_TCC_BCHECK
5770 if (tcc_state
->do_bounds_check
5771 && !strcmp(get_tok_str(sym
->v
, NULL
), "main")) {
5775 for (i
= 0, sym
= local_stack
; i
< 2; i
++, sym
= sym
->prev
) {
5776 if (sym
->v
& SYM_FIELD
|| sym
->prev
->v
& SYM_FIELD
)
5778 vpush_global_sym(&func_old_type
, TOK___bound_main_arg
);
5779 vset(&sym
->type
, sym
->r
, sym
->c
);
5785 block(NULL
, NULL
, NULL
, NULL
, 0, 0);
5788 cur_text_section
->data_offset
= ind
;
5789 label_pop(&global_label_stack
, NULL
);
5790 /* reset local stack */
5791 scope_stack_bottom
= NULL
;
5792 sym_pop(&local_stack
, NULL
);
5793 /* end of function */
5794 /* patch symbol size */
5795 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
5797 /* patch symbol weakness (this definition overrules any prototype) */
5798 if (sym
->type
.t
& VT_WEAK
)
5800 if (tcc_state
->do_debug
) {
5801 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
5803 /* It's better to crash than to generate wrong code */
5804 cur_text_section
= NULL
;
5805 funcname
= ""; /* for safety */
5806 func_vt
.t
= VT_VOID
; /* for safety */
5807 func_var
= 0; /* for safety */
5808 ind
= 0; /* for safety */
5809 nocode_wanted
= saved_nocode_wanted
;
5812 ST_FUNC
void gen_inline_functions(void)
5815 int *str
, inline_generated
, i
;
5816 struct InlineFunc
*fn
;
5818 /* iterate while inline function are referenced */
5820 inline_generated
= 0;
5821 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5822 fn
= tcc_state
->inline_fns
[i
];
5824 if (sym
&& sym
->c
) {
5825 /* the function was used: generate its code and
5826 convert it to a normal function */
5827 str
= fn
->token_str
;
5830 pstrcpy(file
->filename
, sizeof file
->filename
, fn
->filename
);
5831 sym
->r
= VT_SYM
| VT_CONST
;
5832 sym
->type
.t
&= ~VT_INLINE
;
5836 cur_text_section
= text_section
;
5838 macro_ptr
= NULL
; /* fail safe */
5840 inline_generated
= 1;
5843 if (!inline_generated
)
5846 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5847 fn
= tcc_state
->inline_fns
[i
];
5848 str
= fn
->token_str
;
5851 dynarray_reset(&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
);
5854 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
5855 static int decl0(int l
, int is_for_loop_init
)
5863 if (!parse_btype(&btype
, &ad
)) {
5864 if (is_for_loop_init
)
5866 /* skip redundant ';' */
5867 /* XXX: find more elegant solution */
5872 if (l
== VT_CONST
&&
5873 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5874 /* global asm block */
5878 /* special test for old K&R protos without explicit int
5879 type. Only accepted when defining global data */
5880 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
5884 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5885 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5887 /* we accept no variable after */
5891 while (1) { /* iterate thru each declaration */
5892 char *asm_label
; // associated asm label
5894 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5898 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
5899 printf("type = '%s'\n", buf
);
5902 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5903 if ((type
.t
& VT_STATIC
) && (l
== VT_LOCAL
)) {
5904 tcc_error("function without file scope cannot be static");
5906 /* if old style function prototype, we accept a
5909 if (sym
->c
== FUNC_OLD
)
5910 func_decl_list(sym
);
5914 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5917 asm_label_instr(&astr
);
5918 asm_label
= tcc_strdup(astr
.data
);
5921 /* parse one last attribute list, after asm label */
5922 parse_attribute(&ad
);
5927 #ifdef TCC_TARGET_PE
5928 if (ad
.a
.func_import
)
5929 type
.t
|= VT_IMPORT
;
5930 if (ad
.a
.func_export
)
5931 type
.t
|= VT_EXPORT
;
5935 tcc_error("cannot use local functions");
5936 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
5937 expect("function definition");
5939 /* reject abstract declarators in function definition */
5941 while ((sym
= sym
->next
) != NULL
)
5942 if (!(sym
->v
& ~SYM_FIELD
))
5943 expect("identifier");
5945 /* XXX: cannot do better now: convert extern line to static inline */
5946 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
5947 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5952 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
5955 ref
= sym
->type
.ref
;
5956 if (0 == ref
->a
.func_proto
)
5957 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
5959 /* use func_call from prototype if not defined */
5960 if (ref
->a
.func_call
!= FUNC_CDECL
5961 && type
.ref
->a
.func_call
== FUNC_CDECL
)
5962 type
.ref
->a
.func_call
= ref
->a
.func_call
;
5964 /* use export from prototype */
5965 if (ref
->a
.func_export
)
5966 type
.ref
->a
.func_export
= 1;
5968 /* use static from prototype */
5969 if (sym
->type
.t
& VT_STATIC
)
5970 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5972 if (!is_compatible_types(&sym
->type
, &type
)) {
5974 tcc_error("incompatible types for redefinition of '%s'",
5975 get_tok_str(v
, NULL
));
5977 type
.ref
->a
.func_proto
= 0;
5978 /* if symbol is already defined, then put complete type */
5981 /* put function symbol */
5982 sym
= global_identifier_push(v
, type
.t
, 0);
5983 sym
->type
.ref
= type
.ref
;
5986 /* static inline functions are just recorded as a kind
5987 of macro. Their code will be emitted at the end of
5988 the compilation unit only if they are used */
5989 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
5990 (VT_INLINE
| VT_STATIC
)) {
5991 TokenString func_str
;
5993 struct InlineFunc
*fn
;
5994 const char *filename
;
5996 tok_str_new(&func_str
);
6002 tcc_error("unexpected end of file");
6003 tok_str_add_tok(&func_str
);
6008 } else if (t
== '}') {
6010 if (block_level
== 0)
6014 tok_str_add(&func_str
, -1);
6015 tok_str_add(&func_str
, 0);
6016 filename
= file
? file
->filename
: "";
6017 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
6018 strcpy(fn
->filename
, filename
);
6020 fn
->token_str
= func_str
.str
;
6021 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
6024 /* compute text section */
6025 cur_text_section
= ad
.section
;
6026 if (!cur_text_section
)
6027 cur_text_section
= text_section
;
6028 sym
->r
= VT_SYM
| VT_CONST
;
6033 if (btype
.t
& VT_TYPEDEF
) {
6034 /* save typedefed type */
6035 /* XXX: test storage specifiers ? */
6036 sym
= sym_push(v
, &type
, 0, 0);
6038 sym
->type
.t
|= VT_TYPEDEF
;
6041 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
6042 /* external function definition */
6043 /* specific case for func_call attribute */
6044 ad
.a
.func_proto
= 1;
6046 } else if (!(type
.t
& VT_ARRAY
)) {
6047 /* not lvalue if array */
6048 r
|= lvalue_type(type
.t
);
6050 has_init
= (tok
== '=');
6051 if (has_init
&& (type
.t
& VT_VLA
))
6052 tcc_error("Variable length array cannot be initialized");
6053 if ((btype
.t
& VT_EXTERN
) || ((type
.t
& VT_BTYPE
) == VT_FUNC
) ||
6054 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
6055 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
6056 /* external variable or function */
6057 /* NOTE: as GCC, uninitialized global static
6058 arrays of null size are considered as
6060 sym
= external_sym(v
, &type
, r
, asm_label
);
6062 if (type
.t
& VT_WEAK
)
6065 if (ad
.alias_target
) {
6070 alias_target
= sym_find(ad
.alias_target
);
6071 if (!alias_target
|| !alias_target
->c
)
6072 tcc_error("unsupported forward __alias__ attribute");
6073 esym
= &((Elf32_Sym
*)symtab_section
->data
)[alias_target
->c
];
6074 tsec
.sh_num
= esym
->st_shndx
;
6075 put_extern_sym2(sym
, &tsec
, esym
->st_value
, esym
->st_size
, 0);
6078 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
6079 if (type
.t
& VT_STATIC
)
6085 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, asm_label
, l
);
6089 if (is_for_loop_init
)
6102 ST_FUNC
void decl(int l
)