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
, ex_rc
;
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
;
73 ST_DATA
int pop_stack
;
75 ST_DATA CType char_pointer_type
, func_old_type
, int_type
, size_type
;
77 /* ------------------------------------------------------------------------- */
78 static void gen_cast(CType
*type
);
79 static inline CType
*pointed_type(CType
*type
);
80 static int is_compatible_types(CType
*type1
, CType
*type2
);
81 static int parse_btype(CType
*type
, AttributeDef
*ad
);
82 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
);
83 static void parse_expr_type(CType
*type
);
84 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
, int first
, int size_only
);
85 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
, int case_reg
, int is_expr
);
86 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
, int has_init
, int v
, char *asm_label
, int scope
);
87 static int decl0(int l
, int is_for_loop_init
);
88 static void expr_eq(void);
89 static void unary_type(CType
*type
);
90 static void vla_runtime_type_size(CType
*type
, int *a
);
91 static void vla_sp_save(void);
92 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
);
93 static void expr_type(CType
*type
);
94 ST_FUNC
void vpush64(int ty
, unsigned long long v
);
95 ST_FUNC
void vpush(CType
*type
);
96 ST_FUNC
int gtst(int inv
, int t
);
97 ST_FUNC
int is_btype_size(int bt
);
99 ST_INLN
int is_float(int t
)
103 return bt
== VT_LDOUBLE
|| bt
== VT_DOUBLE
|| bt
== VT_FLOAT
|| bt
== VT_QFLOAT
;
106 /* we use our own 'finite' function to avoid potential problems with
107 non standard math libs */
108 /* XXX: endianness dependent */
109 ST_FUNC
int ieee_finite(double d
)
112 memcpy(p
, &d
, sizeof(double));
113 return ((unsigned)((p
[1] | 0x800fffff) + 1)) >> 31;
116 ST_FUNC
void test_lvalue(void)
118 if (!(vtop
->r
& VT_LVAL
))
122 /* ------------------------------------------------------------------------- */
123 /* symbol allocator */
124 static Sym
*__sym_malloc(void)
126 Sym
*sym_pool
, *sym
, *last_sym
;
129 sym_pool
= tcc_malloc(SYM_POOL_NB
* sizeof(Sym
));
130 dynarray_add(&sym_pools
, &nb_sym_pools
, sym_pool
);
132 last_sym
= sym_free_first
;
134 for(i
= 0; i
< SYM_POOL_NB
; i
++) {
135 sym
->next
= last_sym
;
139 sym_free_first
= last_sym
;
143 static inline Sym
*sym_malloc(void)
146 sym
= sym_free_first
;
148 sym
= __sym_malloc();
149 sym_free_first
= sym
->next
;
153 ST_INLN
void sym_free(Sym
*sym
)
155 sym
->next
= sym_free_first
;
156 tcc_free(sym
->asm_label
);
157 sym_free_first
= sym
;
160 /* push, without hashing */
161 ST_FUNC Sym
*sym_push2(Sym
**ps
, int v
, int t
, long c
)
164 if (ps
== &local_stack
) {
165 for (s
= *ps
; s
&& s
!= scope_stack_bottom
; s
= s
->prev
)
166 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
&& s
->v
== v
)
167 tcc_error("incompatible types for redefinition of '%s'",
168 get_tok_str(v
, NULL
));
186 /* find a symbol and return its associated structure. 's' is the top
187 of the symbol stack */
188 ST_FUNC Sym
*sym_find2(Sym
*s
, int v
)
200 /* structure lookup */
201 ST_INLN Sym
*struct_find(int v
)
204 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
206 return table_ident
[v
]->sym_struct
;
209 /* find an identifier */
210 ST_INLN Sym
*sym_find(int v
)
213 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
215 return table_ident
[v
]->sym_identifier
;
218 /* push a given symbol on the symbol stack */
219 ST_FUNC Sym
*sym_push(int v
, CType
*type
, int r
, int c
)
228 s
= sym_push2(ps
, v
, type
->t
, c
);
229 s
->type
.ref
= type
->ref
;
231 /* don't record fields or anonymous symbols */
233 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
234 /* record symbol in token array */
235 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
237 ps
= &ts
->sym_struct
;
239 ps
= &ts
->sym_identifier
;
246 /* push a global identifier */
247 ST_FUNC Sym
*global_identifier_push(int v
, int t
, int c
)
250 s
= sym_push2(&global_stack
, v
, t
, c
);
251 /* don't record anonymous symbol */
252 if (v
< SYM_FIRST_ANOM
) {
253 ps
= &table_ident
[v
- TOK_IDENT
]->sym_identifier
;
254 /* modify the top most local identifier, so that
255 sym_identifier will point to 's' when popped */
257 ps
= &(*ps
)->prev_tok
;
264 /* pop symbols until top reaches 'b' */
265 ST_FUNC
void sym_pop(Sym
**ptop
, Sym
*b
)
275 /* remove symbol in token array */
277 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
278 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
280 ps
= &ts
->sym_struct
;
282 ps
= &ts
->sym_identifier
;
291 static void weaken_symbol(Sym
*sym
)
293 sym
->type
.t
|= VT_WEAK
;
298 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
299 esym_type
= ELFW(ST_TYPE
)(esym
->st_info
);
300 esym
->st_info
= ELFW(ST_INFO
)(STB_WEAK
, esym_type
);
304 static void apply_visibility(Sym
*sym
, CType
*type
)
306 int vis
= sym
->type
.t
& VT_VIS_MASK
;
307 int vis2
= type
->t
& VT_VIS_MASK
;
308 if (vis
== (STV_DEFAULT
<< VT_VIS_SHIFT
))
310 else if (vis2
== (STV_DEFAULT
<< VT_VIS_SHIFT
))
313 vis
= (vis
< vis2
) ? vis
: vis2
;
314 sym
->type
.t
&= ~VT_VIS_MASK
;
320 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
321 vis
>>= VT_VIS_SHIFT
;
322 esym
->st_other
= (esym
->st_other
& ~ELFW(ST_VISIBILITY
)(-1)) | vis
;
326 /* ------------------------------------------------------------------------- */
328 ST_FUNC
void swap(int *p
, int *q
)
336 static void vsetc(CType
*type
, int r
, CValue
*vc
)
340 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
341 tcc_error("memory full (vstack)");
342 /* cannot let cpu flags if other instruction are generated. Also
343 avoid leaving VT_JMP anywhere except on the top of the stack
344 because it would complicate the code generator. */
345 if (vtop
>= vstack
) {
346 v
= vtop
->r
& VT_VALMASK
;
347 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
357 /* push constant of type "type" with useless value */
358 ST_FUNC
void vpush(CType
*type
)
361 vsetc(type
, VT_CONST
, &cval
);
364 /* push integer constant */
365 ST_FUNC
void vpushi(int v
)
369 vsetc(&int_type
, VT_CONST
, &cval
);
372 /* push a pointer sized constant */
373 static void vpushs(addr_t v
)
377 vsetc(&size_type
, VT_CONST
, &cval
);
380 /* push arbitrary 64bit constant */
381 ST_FUNC
void vpush64(int ty
, unsigned long long v
)
388 vsetc(&ctype
, VT_CONST
, &cval
);
391 /* push long long constant */
392 static inline void vpushll(long long v
)
394 vpush64(VT_LLONG
, v
);
397 /* push a symbol value of TYPE */
398 static inline void vpushsym(CType
*type
, Sym
*sym
)
402 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
406 /* Return a static symbol pointing to a section */
407 ST_FUNC Sym
*get_sym_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
413 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
414 sym
->type
.ref
= type
->ref
;
415 sym
->r
= VT_CONST
| VT_SYM
;
416 put_extern_sym(sym
, sec
, offset
, size
);
420 /* push a reference to a section offset by adding a dummy symbol */
421 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
423 vpushsym(type
, get_sym_ref(type
, sec
, offset
, size
));
426 /* define a new external reference to a symbol 'v' of type 'u' */
427 ST_FUNC Sym
*external_global_sym(int v
, CType
*type
, int r
)
433 /* push forward reference */
434 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
435 s
->type
.ref
= type
->ref
;
436 s
->r
= r
| VT_CONST
| VT_SYM
;
441 /* define a new external reference to a symbol 'v' with alternate asm
442 name 'asm_label' of type 'u'. 'asm_label' is equal to NULL if there
443 is no alternate name (most cases) */
444 static Sym
*external_sym(int v
, CType
*type
, int r
, char *asm_label
)
450 /* push forward reference */
451 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
452 s
->asm_label
= asm_label
;
453 s
->type
.t
|= VT_EXTERN
;
454 } else if (s
->type
.ref
== func_old_type
.ref
) {
455 s
->type
.ref
= type
->ref
;
456 s
->r
= r
| VT_CONST
| VT_SYM
;
457 s
->type
.t
|= VT_EXTERN
;
458 } else if (!is_compatible_types(&s
->type
, type
)) {
459 tcc_error("incompatible types for redefinition of '%s'",
460 get_tok_str(v
, NULL
));
462 /* Merge some storage attributes. */
463 if (type
->t
& VT_WEAK
)
466 if (type
->t
& VT_VIS_MASK
)
467 apply_visibility(s
, type
);
472 /* push a reference to global symbol v */
473 ST_FUNC
void vpush_global_sym(CType
*type
, int v
)
475 vpushsym(type
, external_global_sym(v
, type
, 0));
478 ST_FUNC
void vset(CType
*type
, int r
, int v
)
483 vsetc(type
, r
, &cval
);
486 static void vseti(int r
, int v
)
494 ST_FUNC
void vswap(void)
497 /* cannot let cpu flags if other instruction are generated. Also
498 avoid leaving VT_JMP anywhere except on the top of the stack
499 because it would complicate the code generator. */
500 if (vtop
>= vstack
) {
501 int v
= vtop
->r
& VT_VALMASK
;
502 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
509 /* XXX: +2% overall speed possible with optimized memswap
511 * memswap(&vtop[0], &vtop[1], sizeof *vtop);
515 ST_FUNC
void vpushv(SValue
*v
)
517 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
518 tcc_error("memory full (vstack)");
523 ST_FUNC
void vdup(void)
528 /* save r to the memory stack, and mark it as being free */
529 ST_FUNC
void save_reg(int r
)
531 int l
, saved
, size
, align
;
535 /* modify all stack values */
538 for(p
=vstack
;p
<=vtop
;p
++) {
539 if ((p
->r
& VT_VALMASK
) == r
||
540 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
541 /* must save value on stack if not already done */
543 /* NOTE: must reload 'r' because r might be equal to r2 */
544 r
= p
->r
& VT_VALMASK
;
545 /* store register in the stack */
547 if ((p
->r
& VT_LVAL
) ||
548 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
549 #ifdef TCC_TARGET_X86_64
550 type
= &char_pointer_type
;
554 size
= type_size(type
, &align
);
555 loc
= (loc
- size
) & -align
;
557 sv
.r
= VT_LOCAL
| VT_LVAL
;
560 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
561 /* x86 specific: need to pop fp register ST0 if saved */
563 o(0xd8dd); /* fstp %st(0) */
566 #ifndef TCC_TARGET_X86_64
567 /* special long long case */
568 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
576 /* mark that stack entry as being saved on the stack */
577 if (p
->r
& VT_LVAL
) {
578 /* also clear the bounded flag because the
579 relocation address of the function was stored in
581 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
583 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
591 #ifdef TCC_TARGET_ARM
592 /* find a register of class 'rc2' with at most one reference on stack.
593 * If none, call get_reg(rc) */
594 ST_FUNC
int get_reg_ex(int rc
, int rc2
)
599 for(r
=0;r
<NB_REGS
;r
++) {
600 if (reg_classes
[r
] & rc2
) {
603 for(p
= vstack
; p
<= vtop
; p
++) {
604 if ((p
->r
& VT_VALMASK
) == r
||
605 (p
->r2
& VT_VALMASK
) == r
)
616 /* find a free register of class 'rc'. If none, save one register */
617 ST_FUNC
int get_reg(int rc
)
622 /* find a free register */
623 for(r
=0;r
<NB_REGS
;r
++) {
624 if (reg_classes
[r
] & rc
) {
625 for(p
=vstack
;p
<=vtop
;p
++) {
626 if ((p
->r
& VT_VALMASK
) == r
||
627 (p
->r2
& VT_VALMASK
) == r
)
635 /* no register left : free the first one on the stack (VERY
636 IMPORTANT to start from the bottom to ensure that we don't
637 spill registers used in gen_opi()) */
638 for(p
=vstack
;p
<=vtop
;p
++) {
639 /* look at second register (if long long) */
640 r
= p
->r2
& VT_VALMASK
;
641 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
643 r
= p
->r
& VT_VALMASK
;
644 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
650 /* Should never comes here */
654 /* save registers up to (vtop - n) stack entry */
655 ST_FUNC
void save_regs(int n
)
660 for(p
= vstack
;p
<= p1
; p
++) {
661 r
= p
->r
& VT_VALMASK
;
668 /* move register 's' (of type 't') to 'r', and flush previous value of r to memory
670 static void move_reg(int r
, int s
, int t
)
684 /* get address of vtop (vtop MUST BE an lvalue) */
685 ST_FUNC
void gaddrof(void)
687 if (vtop
->r
& VT_REF
)
690 /* tricky: if saved lvalue, then we can go back to lvalue */
691 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
692 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
697 #ifdef CONFIG_TCC_BCHECK
698 /* generate lvalue bound code */
699 static void gbound(void)
704 vtop
->r
&= ~VT_MUSTBOUND
;
705 /* if lvalue, then use checking code before dereferencing */
706 if (vtop
->r
& VT_LVAL
) {
707 /* if not VT_BOUNDED value, then make one */
708 if (!(vtop
->r
& VT_BOUNDED
)) {
709 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
710 /* must save type because we must set it to int to get pointer */
712 vtop
->type
.t
= VT_INT
;
715 gen_bounded_ptr_add();
716 vtop
->r
|= lval_type
;
719 /* then check for dereferencing */
720 gen_bounded_ptr_deref();
725 /* store vtop a register belonging to class 'rc'. lvalues are
726 converted to values. Cannot be used if cannot be converted to
727 register value (such as structures). */
728 ST_FUNC
int gv(int rc
)
730 int r
, bit_pos
, bit_size
, size
, align
, i
, ft
, sbt
;
735 /* NOTE: get_reg can modify vstack[] */
736 if (ft
& VT_BITFIELD
) {
739 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
740 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
741 /* remove bit field info to avoid loops */
742 ft
= vtop
->type
.t
&= ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
743 /* cast to int to propagate signedness in following ops */
744 if (sbt
== VT_LLONG
) {
751 if((ft
& VT_UNSIGNED
) || sbt
== VT_BOOL
)
752 type
.t
|= VT_UNSIGNED
;
754 /* generate shifts */
755 vpushi(bits
- (bit_pos
+ bit_size
));
757 vpushi(bits
- bit_size
);
758 /* NOTE: transformed to SHR if unsigned */
762 if (is_float(vtop
->type
.t
) &&
763 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
766 unsigned long offset
;
767 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
771 /* XXX: unify with initializers handling ? */
772 /* CPUs usually cannot use float constants, so we store them
773 generically in data segment */
774 size
= type_size(&vtop
->type
, &align
);
775 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
776 data_section
->data_offset
= offset
;
777 /* XXX: not portable yet */
778 #if defined(__i386__) || defined(__x86_64__)
779 /* Zero pad x87 tenbyte long doubles */
780 if (size
== LDOUBLE_SIZE
) {
781 vtop
->c
.tab
[2] &= 0xffff;
782 #if LDOUBLE_SIZE == 16
787 ptr
= section_ptr_add(data_section
, size
);
789 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
793 ptr
[i
] = vtop
->c
.tab
[size
-1-i
];
797 ptr
[i
] = vtop
->c
.tab
[i
];
798 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
799 vtop
->r
|= VT_LVAL
| VT_SYM
;
801 vtop
->c
.ptr_offset
= 0;
803 #ifdef CONFIG_TCC_BCHECK
804 if (vtop
->r
& VT_MUSTBOUND
)
808 r
= vtop
->r
& VT_VALMASK
;
809 if((rc
& ~RC_MASK
) && (rc
!= RC_ST0
))
812 rc2
= (rc
& RC_FLOAT
) ? RC_FLOAT
: RC_INT
;
814 /* need to reload if:
816 - lvalue (need to dereference pointer)
817 - already a register, but not in the right class */
818 if (r
>= VT_CONST
|| (vtop
->r
& VT_LVAL
) || !(reg_classes
[r
] & rc
)
819 #ifdef TCC_TARGET_X86_64
820 || (sbt
== VT_QLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
821 || (sbt
== VT_QFLOAT
&& !(reg_classes
[vtop
->r2
] & rc2
))
823 || (sbt
== VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
828 #ifdef TCC_TARGET_X86_64
829 if ((sbt
== VT_QLONG
) || (sbt
== VT_QFLOAT
))
834 #ifdef TCC_TARGET_X86_64
835 int load_size
= 8, load_type
= (sbt
== VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
837 int load_size
= 4, load_type
= VT_INT
;
838 unsigned long long ll
;
841 /* two register type load : expand to two words
843 #ifndef TCC_TARGET_X86_64
844 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
847 vtop
->c
.ui
= ll
; /* first word */
849 vtop
->r
= r
; /* save register value */
850 vpushi(ll
>> 32); /* second word */
853 /* XXX: test to VT_CONST incorrect ? */
854 if (r
>= VT_CONST
|| (vtop
->r
& VT_LVAL
)) {
855 /* We do not want to modifier the long long
856 pointer here, so the safest (and less
857 efficient) is to save all the other registers
858 in the regs. use VT_TMP XXX: totally inefficient. */
859 /* load from memory */
860 vtop
->type
.t
= load_type
;
863 vtop
[-1].r
= r
| VT_TMP
; /* lock register value */
864 /* increment pointer to get second word */
865 vtop
->type
= char_pointer_type
;
870 vtop
->type
.t
= load_type
;
875 vtop
[-1].r
= r
| VT_TMP
; /* lock register value */
876 vtop
->r
= vtop
[-1].r2
;
878 /* Allocate second register. Here we rely on the fact that
879 get_reg() tries first to free r2 of an SValue. */
883 /* write second register */
887 } else if ((vtop
->r
& VT_LVAL
) && !is_float(ft
)) {
889 /* lvalue of scalar type : need to use lvalue type
890 because of possible cast */
892 /* compute memory access type */
893 if (vtop
->r
& VT_REF
)
894 #ifdef TCC_TARGET_X86_64
899 else if (vtop
->r
& VT_LVAL_BYTE
)
901 else if (vtop
->r
& VT_LVAL_SHORT
)
903 if (vtop
->r
& VT_LVAL_UNSIGNED
)
907 /* restore wanted type */
910 /* one register type load */
914 vtop
->c
.ptr_offset
= 0;
916 #ifdef TCC_TARGET_C67
917 /* uses register pairs for doubles */
918 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
925 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
926 ST_FUNC
void gv2(int rc1
, int rc2
)
928 /* generate more generic register first. But VT_JMP or VT_CMP
929 values must be generated first in all cases to avoid possible
936 /* test if reload is needed for first register */
937 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
947 /* test if reload is needed for first register */
948 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
954 /* wrapper around RC_FRET to return a register by type */
955 static int rc_fret(int t
)
957 #ifdef TCC_TARGET_X86_64
958 if (t
== VT_LDOUBLE
) {
966 /* wrapper around REG_FRET to return a register by type */
967 static int reg_fret(int t
)
969 #ifdef TCC_TARGET_X86_64
970 if (t
== VT_LDOUBLE
) {
977 /* expand long long on stack in two int registers */
978 static void lexpand(void)
982 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
985 vtop
[0].r
= vtop
[-1].r2
;
986 vtop
[0].r2
= VT_CONST
;
987 vtop
[-1].r2
= VT_CONST
;
988 vtop
[0].type
.t
= VT_INT
| u
;
989 vtop
[-1].type
.t
= VT_INT
| u
;
992 #ifdef TCC_TARGET_ARM
993 /* expand long long on stack */
994 ST_FUNC
void lexpand_nr(void)
998 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
1000 vtop
->r2
= VT_CONST
;
1001 vtop
->type
.t
= VT_INT
| u
;
1002 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
1003 if (v
== VT_CONST
) {
1004 vtop
[-1].c
.ui
= vtop
->c
.ull
;
1005 vtop
->c
.ui
= vtop
->c
.ull
>> 32;
1007 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
1009 vtop
->r
= vtop
[-1].r
;
1010 } else if (v
> VT_CONST
) {
1014 vtop
->r
= vtop
[-1].r2
;
1015 vtop
[-1].r2
= VT_CONST
;
1016 vtop
[-1].type
.t
= VT_INT
| u
;
1020 #ifndef TCC_TARGET_X86_64
1021 /* build a long long from two ints */
1022 static void lbuild(int t
)
1024 gv2(RC_INT
, RC_INT
);
1025 vtop
[-1].r2
= vtop
[0].r
;
1026 vtop
[-1].type
.t
= t
;
1031 /* rotate n first stack elements to the bottom
1032 I1 ... In -> I2 ... In I1 [top is right]
1034 ST_FUNC
void vrotb(int n
)
1040 for(i
=-n
+1;i
!=0;i
++)
1041 vtop
[i
] = vtop
[i
+1];
1045 /* rotate the n elements before entry e towards the top
1046 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
1048 ST_FUNC
void vrote(SValue
*e
, int n
)
1054 for(i
= 0;i
< n
- 1; i
++)
1059 /* rotate n first stack elements to the top
1060 I1 ... In -> In I1 ... I(n-1) [top is right]
1062 ST_FUNC
void vrott(int n
)
1067 /* pop stack value */
1068 ST_FUNC
void vpop(void)
1071 v
= vtop
->r
& VT_VALMASK
;
1072 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1073 /* for x86, we need to pop the FP stack */
1074 if (v
== TREG_ST0
&& !nocode_wanted
) {
1075 o(0xd8dd); /* fstp %st(0) */
1078 if (v
== VT_JMP
|| v
== VT_JMPI
) {
1079 /* need to put correct jump if && or || without test */
1085 /* convert stack entry to register and duplicate its value in another
1087 static void gv_dup(void)
1092 #ifndef TCC_TARGET_X86_64
1093 if ((t
& VT_BTYPE
) == VT_LLONG
) {
1100 /* stack: H L L1 H1 */
1109 /* duplicate value */
1112 #ifdef TCC_TARGET_X86_64
1113 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1124 load(r1
, &sv
); /* move r to r1 */
1126 /* duplicates value */
1131 #ifndef TCC_TARGET_X86_64
1132 /* generate CPU independent (unsigned) long long operations */
1133 static void gen_opl(int op
)
1135 int t
, a
, b
, op1
, c
, i
;
1137 unsigned short reg_iret
= REG_IRET
;
1138 unsigned short reg_lret
= REG_LRET
;
1144 func
= TOK___divdi3
;
1147 func
= TOK___udivdi3
;
1150 func
= TOK___moddi3
;
1153 func
= TOK___umoddi3
;
1160 /* call generic long long function */
1161 vpush_global_sym(&func_old_type
, func
);
1166 vtop
->r2
= reg_lret
;
1179 /* stack: L1 H1 L2 H2 */
1184 vtop
[-2] = vtop
[-3];
1187 /* stack: H1 H2 L1 L2 */
1193 /* stack: H1 H2 L1 L2 ML MH */
1196 /* stack: ML MH H1 H2 L1 L2 */
1200 /* stack: ML MH H1 L2 H2 L1 */
1205 /* stack: ML MH M1 M2 */
1208 } else if (op
== '+' || op
== '-') {
1209 /* XXX: add non carry method too (for MIPS or alpha) */
1215 /* stack: H1 H2 (L1 op L2) */
1218 gen_op(op1
+ 1); /* TOK_xxxC2 */
1221 /* stack: H1 H2 (L1 op L2) */
1224 /* stack: (L1 op L2) H1 H2 */
1226 /* stack: (L1 op L2) (H1 op H2) */
1234 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1235 t
= vtop
[-1].type
.t
;
1239 /* stack: L H shift */
1241 /* constant: simpler */
1242 /* NOTE: all comments are for SHL. the other cases are
1243 done by swaping words */
1254 if (op
!= TOK_SAR
) {
1287 /* XXX: should provide a faster fallback on x86 ? */
1290 func
= TOK___ashrdi3
;
1293 func
= TOK___lshrdi3
;
1296 func
= TOK___ashldi3
;
1302 /* compare operations */
1308 /* stack: L1 H1 L2 H2 */
1310 vtop
[-1] = vtop
[-2];
1312 /* stack: L1 L2 H1 H2 */
1315 /* when values are equal, we need to compare low words. since
1316 the jump is inverted, we invert the test too. */
1319 else if (op1
== TOK_GT
)
1321 else if (op1
== TOK_ULT
)
1323 else if (op1
== TOK_UGT
)
1328 if (op1
!= TOK_NE
) {
1332 /* generate non equal test */
1333 /* XXX: NOT PORTABLE yet */
1337 #if defined(TCC_TARGET_I386)
1338 b
= psym(0x850f, 0);
1339 #elif defined(TCC_TARGET_ARM)
1341 o(0x1A000000 | encbranch(ind
, 0, 1));
1342 #elif defined(TCC_TARGET_C67)
1343 tcc_error("not implemented");
1345 #error not supported
1349 /* compare low. Always unsigned */
1353 else if (op1
== TOK_LE
)
1355 else if (op1
== TOK_GT
)
1357 else if (op1
== TOK_GE
)
1368 /* handle integer constant optimizations and various machine
1370 static void gen_opic(int op
)
1372 int c1
, c2
, t1
, t2
, n
;
1375 typedef unsigned long long U
;
1379 t1
= v1
->type
.t
& VT_BTYPE
;
1380 t2
= v2
->type
.t
& VT_BTYPE
;
1384 else if (v1
->type
.t
& VT_UNSIGNED
)
1391 else if (v2
->type
.t
& VT_UNSIGNED
)
1396 /* currently, we cannot do computations with forward symbols */
1397 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1398 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1401 case '+': l1
+= l2
; break;
1402 case '-': l1
-= l2
; break;
1403 case '&': l1
&= l2
; break;
1404 case '^': l1
^= l2
; break;
1405 case '|': l1
|= l2
; break;
1406 case '*': l1
*= l2
; break;
1413 /* if division by zero, generate explicit division */
1416 tcc_error("division by zero in constant");
1420 default: l1
/= l2
; break;
1421 case '%': l1
%= l2
; break;
1422 case TOK_UDIV
: l1
= (U
)l1
/ l2
; break;
1423 case TOK_UMOD
: l1
= (U
)l1
% l2
; break;
1426 case TOK_SHL
: l1
<<= l2
; break;
1427 case TOK_SHR
: l1
= (U
)l1
>> l2
; break;
1428 case TOK_SAR
: l1
>>= l2
; break;
1430 case TOK_ULT
: l1
= (U
)l1
< (U
)l2
; break;
1431 case TOK_UGE
: l1
= (U
)l1
>= (U
)l2
; break;
1432 case TOK_EQ
: l1
= l1
== l2
; break;
1433 case TOK_NE
: l1
= l1
!= l2
; break;
1434 case TOK_ULE
: l1
= (U
)l1
<= (U
)l2
; break;
1435 case TOK_UGT
: l1
= (U
)l1
> (U
)l2
; break;
1436 case TOK_LT
: l1
= l1
< l2
; break;
1437 case TOK_GE
: l1
= l1
>= l2
; break;
1438 case TOK_LE
: l1
= l1
<= l2
; break;
1439 case TOK_GT
: l1
= l1
> l2
; break;
1441 case TOK_LAND
: l1
= l1
&& l2
; break;
1442 case TOK_LOR
: l1
= l1
|| l2
; break;
1449 /* if commutative ops, put c2 as constant */
1450 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1451 op
== '|' || op
== '*')) {
1453 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1454 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1456 /* Filter out NOP operations like x*1, x-0, x&-1... */
1457 if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1460 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1461 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1467 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1468 /* try to use shifts instead of muls or divs */
1469 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1478 else if (op
== TOK_PDIV
)
1484 } else if (c2
&& (op
== '+' || op
== '-') &&
1485 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
1486 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1487 /* symbol + constant case */
1494 if (!nocode_wanted
) {
1495 /* call low level op generator */
1496 if (t1
== VT_LLONG
|| t2
== VT_LLONG
)
1507 /* generate a floating point operation with constant propagation */
1508 static void gen_opif(int op
)
1516 /* currently, we cannot do computations with forward symbols */
1517 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1518 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1520 if (v1
->type
.t
== VT_FLOAT
) {
1523 } else if (v1
->type
.t
== VT_DOUBLE
) {
1531 /* NOTE: we only do constant propagation if finite number (not
1532 NaN or infinity) (ANSI spec) */
1533 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1537 case '+': f1
+= f2
; break;
1538 case '-': f1
-= f2
; break;
1539 case '*': f1
*= f2
; break;
1543 tcc_error("division by zero in constant");
1548 /* XXX: also handles tests ? */
1552 /* XXX: overflow test ? */
1553 if (v1
->type
.t
== VT_FLOAT
) {
1555 } else if (v1
->type
.t
== VT_DOUBLE
) {
1563 if (!nocode_wanted
) {
1571 static int pointed_size(CType
*type
)
1574 return type_size(pointed_type(type
), &align
);
1577 static void vla_runtime_pointed_size(CType
*type
)
1580 vla_runtime_type_size(pointed_type(type
), &align
);
1583 static inline int is_null_pointer(SValue
*p
)
1585 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1587 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& p
->c
.i
== 0) ||
1588 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.ll
== 0) ||
1589 ((p
->type
.t
& VT_BTYPE
) == VT_PTR
&& p
->c
.ptr_offset
== 0);
1592 static inline int is_integer_btype(int bt
)
1594 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
1595 bt
== VT_INT
|| bt
== VT_LLONG
);
1598 /* check types for comparison or subtraction of pointers */
1599 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
1601 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
1604 /* null pointers are accepted for all comparisons as gcc */
1605 if (is_null_pointer(p1
) || is_null_pointer(p2
))
1609 bt1
= type1
->t
& VT_BTYPE
;
1610 bt2
= type2
->t
& VT_BTYPE
;
1611 /* accept comparison between pointer and integer with a warning */
1612 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
1613 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
1614 tcc_warning("comparison between pointer and integer");
1618 /* both must be pointers or implicit function pointers */
1619 if (bt1
== VT_PTR
) {
1620 type1
= pointed_type(type1
);
1621 } else if (bt1
!= VT_FUNC
)
1622 goto invalid_operands
;
1624 if (bt2
== VT_PTR
) {
1625 type2
= pointed_type(type2
);
1626 } else if (bt2
!= VT_FUNC
) {
1628 tcc_error("invalid operands to binary %s", get_tok_str(op
, NULL
));
1630 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1631 (type2
->t
& VT_BTYPE
) == VT_VOID
)
1635 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1636 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1637 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1638 /* gcc-like error if '-' is used */
1640 goto invalid_operands
;
1642 tcc_warning("comparison of distinct pointer types lacks a cast");
1646 /* generic gen_op: handles types problems */
1647 ST_FUNC
void gen_op(int op
)
1649 int u
, t1
, t2
, bt1
, bt2
, t
;
1652 t1
= vtop
[-1].type
.t
;
1653 t2
= vtop
[0].type
.t
;
1654 bt1
= t1
& VT_BTYPE
;
1655 bt2
= t2
& VT_BTYPE
;
1657 if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
1658 /* at least one operand is a pointer */
1659 /* relationnal op: must be both pointers */
1660 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
1661 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1662 /* pointers are handled are unsigned */
1663 #ifdef TCC_TARGET_X86_64
1664 t
= VT_LLONG
| VT_UNSIGNED
;
1666 t
= VT_INT
| VT_UNSIGNED
;
1670 /* if both pointers, then it must be the '-' op */
1671 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
1673 tcc_error("cannot use pointers here");
1674 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1675 /* XXX: check that types are compatible */
1676 if (vtop
[-1].type
.t
& VT_VLA
) {
1677 vla_runtime_pointed_size(&vtop
[-1].type
);
1679 vpushi(pointed_size(&vtop
[-1].type
));
1683 /* set to integer type */
1684 #ifdef TCC_TARGET_X86_64
1685 vtop
->type
.t
= VT_LLONG
;
1687 vtop
->type
.t
= VT_INT
;
1692 /* exactly one pointer : must be '+' or '-'. */
1693 if (op
!= '-' && op
!= '+')
1694 tcc_error("cannot use pointers here");
1695 /* Put pointer as first operand */
1696 if (bt2
== VT_PTR
) {
1700 type1
= vtop
[-1].type
;
1701 type1
.t
&= ~VT_ARRAY
;
1702 if (vtop
[-1].type
.t
& VT_VLA
)
1703 vla_runtime_pointed_size(&vtop
[-1].type
);
1705 u
= pointed_size(&vtop
[-1].type
);
1707 tcc_error("unknown array element size");
1708 #ifdef TCC_TARGET_X86_64
1711 /* XXX: cast to int ? (long long case) */
1716 #ifdef CONFIG_TCC_BCHECK
1717 /* if evaluating constant expression, no code should be
1718 generated, so no bound check */
1719 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
1720 /* if bounded pointers, we generate a special code to
1727 gen_bounded_ptr_add();
1733 /* put again type if gen_opic() swaped operands */
1736 } else if (is_float(bt1
) || is_float(bt2
)) {
1737 /* compute bigger type and do implicit casts */
1738 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
1740 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
1745 /* floats can only be used for a few operations */
1746 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
1747 (op
< TOK_ULT
|| op
> TOK_GT
))
1748 tcc_error("invalid operands for binary operation");
1750 } else if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
) {
1751 t
= bt1
== VT_LLONG
? VT_LLONG
: VT_INT
;
1752 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (t
| VT_UNSIGNED
))
1755 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
1756 /* cast to biggest op */
1758 /* convert to unsigned if it does not fit in a long long */
1759 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
1760 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
1763 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
1764 tcc_error("comparison of struct");
1766 /* integer operations */
1768 /* convert to unsigned if it does not fit in an integer */
1769 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
1770 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
1773 /* XXX: currently, some unsigned operations are explicit, so
1774 we modify them here */
1775 if (t
& VT_UNSIGNED
) {
1782 else if (op
== TOK_LT
)
1784 else if (op
== TOK_GT
)
1786 else if (op
== TOK_LE
)
1788 else if (op
== TOK_GE
)
1795 /* special case for shifts and long long: we keep the shift as
1797 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
1804 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
1805 /* relationnal op: the result is an int */
1806 vtop
->type
.t
= VT_INT
;
1813 #ifndef TCC_TARGET_ARM
1814 /* generic itof for unsigned long long case */
1815 static void gen_cvt_itof1(int t
)
1817 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
1818 (VT_LLONG
| VT_UNSIGNED
)) {
1821 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
1822 #if LDOUBLE_SIZE != 8
1823 else if (t
== VT_LDOUBLE
)
1824 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
1827 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
1831 vtop
->r
= reg_fret(t
);
1838 /* generic ftoi for unsigned long long case */
1839 static void gen_cvt_ftoi1(int t
)
1843 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
1844 /* not handled natively */
1845 st
= vtop
->type
.t
& VT_BTYPE
;
1847 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
1848 #if LDOUBLE_SIZE != 8
1849 else if (st
== VT_LDOUBLE
)
1850 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
1853 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
1858 vtop
->r2
= REG_LRET
;
1864 /* force char or short cast */
1865 static void force_charshort_cast(int t
)
1869 /* XXX: add optimization if lvalue : just change type and offset */
1874 if (t
& VT_UNSIGNED
) {
1875 vpushi((1 << bits
) - 1);
1881 /* result must be signed or the SAR is converted to an SHL
1882 This was not the case when "t" was a signed short
1883 and the last value on the stack was an unsigned int */
1884 vtop
->type
.t
&= ~VT_UNSIGNED
;
1890 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1891 static void gen_cast(CType
*type
)
1893 int sbt
, dbt
, sf
, df
, c
, p
;
1895 /* special delayed cast for char/short */
1896 /* XXX: in some cases (multiple cascaded casts), it may still
1898 if (vtop
->r
& VT_MUSTCAST
) {
1899 vtop
->r
&= ~VT_MUSTCAST
;
1900 force_charshort_cast(vtop
->type
.t
);
1903 /* bitfields first get cast to ints */
1904 if (vtop
->type
.t
& VT_BITFIELD
) {
1908 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
1909 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
1914 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1915 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
1917 /* constant case: we can do it now */
1918 /* XXX: in ISOC, cannot do it if error in convert */
1919 if (sbt
== VT_FLOAT
)
1920 vtop
->c
.ld
= vtop
->c
.f
;
1921 else if (sbt
== VT_DOUBLE
)
1922 vtop
->c
.ld
= vtop
->c
.d
;
1925 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
1926 if (sbt
& VT_UNSIGNED
)
1927 vtop
->c
.ld
= vtop
->c
.ull
;
1929 vtop
->c
.ld
= vtop
->c
.ll
;
1931 if (sbt
& VT_UNSIGNED
)
1932 vtop
->c
.ld
= vtop
->c
.ui
;
1934 vtop
->c
.ld
= vtop
->c
.i
;
1937 if (dbt
== VT_FLOAT
)
1938 vtop
->c
.f
= (float)vtop
->c
.ld
;
1939 else if (dbt
== VT_DOUBLE
)
1940 vtop
->c
.d
= (double)vtop
->c
.ld
;
1941 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
1942 vtop
->c
.ull
= (unsigned long long)vtop
->c
.ld
;
1943 } else if (sf
&& dbt
== VT_BOOL
) {
1944 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
1947 vtop
->c
.ll
= (long long)vtop
->c
.ld
;
1948 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
1949 vtop
->c
.ll
= vtop
->c
.ull
;
1950 else if (sbt
& VT_UNSIGNED
)
1951 vtop
->c
.ll
= vtop
->c
.ui
;
1952 #ifdef TCC_TARGET_X86_64
1953 else if (sbt
== VT_PTR
)
1956 else if (sbt
!= VT_LLONG
)
1957 vtop
->c
.ll
= vtop
->c
.i
;
1959 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
1960 vtop
->c
.ull
= vtop
->c
.ll
;
1961 else if (dbt
== VT_BOOL
)
1962 vtop
->c
.i
= (vtop
->c
.ll
!= 0);
1963 #ifdef TCC_TARGET_X86_64
1964 else if (dbt
== VT_PTR
)
1967 else if (dbt
!= VT_LLONG
) {
1969 if ((dbt
& VT_BTYPE
) == VT_BYTE
)
1971 else if ((dbt
& VT_BTYPE
) == VT_SHORT
)
1973 if(dbt
& VT_UNSIGNED
)
1974 vtop
->c
.ui
= ((unsigned int)vtop
->c
.ll
<< s
) >> s
;
1976 vtop
->c
.i
= ((int)vtop
->c
.ll
<< s
) >> s
;
1979 } else if (p
&& dbt
== VT_BOOL
) {
1982 } else if (!nocode_wanted
) {
1983 /* non constant case: generate code */
1985 /* convert from fp to fp */
1988 /* convert int to fp */
1991 /* convert fp to int */
1992 if (dbt
== VT_BOOL
) {
1996 /* we handle char/short/etc... with generic code */
1997 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
1998 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
2002 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
2003 /* additional cast for char/short... */
2008 #ifndef TCC_TARGET_X86_64
2009 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
2010 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
2011 /* scalar to long long */
2012 /* machine independent conversion */
2014 /* generate high word */
2015 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
2019 if (sbt
== VT_PTR
) {
2020 /* cast from pointer to int before we apply
2021 shift operation, which pointers don't support*/
2022 gen_cast(&int_type
);
2028 /* patch second register */
2029 vtop
[-1].r2
= vtop
->r
;
2033 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
2034 (dbt
& VT_BTYPE
) == VT_PTR
||
2035 (dbt
& VT_BTYPE
) == VT_FUNC
) {
2036 if ((sbt
& VT_BTYPE
) != VT_LLONG
&&
2037 (sbt
& VT_BTYPE
) != VT_PTR
&&
2038 (sbt
& VT_BTYPE
) != VT_FUNC
) {
2039 /* need to convert from 32bit to 64bit */
2041 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
2042 /* x86_64 specific: movslq */
2044 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
2048 } else if (dbt
== VT_BOOL
) {
2049 /* scalar to bool */
2052 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
2053 (dbt
& VT_BTYPE
) == VT_SHORT
) {
2054 if (sbt
== VT_PTR
) {
2055 vtop
->type
.t
= VT_INT
;
2056 tcc_warning("nonportable conversion from pointer to char/short");
2058 force_charshort_cast(dbt
);
2059 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
2061 if (sbt
== VT_LLONG
) {
2062 /* from long long: just take low order word */
2066 /* if lvalue and single word type, nothing to do because
2067 the lvalue already contains the real type size (see
2068 VT_LVAL_xxx constants) */
2071 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
2072 /* if we are casting between pointer types,
2073 we must update the VT_LVAL_xxx size */
2074 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
2075 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
2080 /* return type size as known at compile time. Put alignment at 'a' */
2081 ST_FUNC
int type_size(CType
*type
, int *a
)
2087 bt
= type
->t
& VT_BTYPE
;
2088 if (bt
== VT_STRUCT
) {
2089 assert(!(type
->t
& VT_VLS
));
2094 } else if (bt
== VT_PTR
) {
2095 if (type
->t
& VT_ARRAY
) {
2098 ts
= type_size(&s
->type
, a
);
2099 if (ts
< 0 && s
->c
< 0)
2101 size
= (size_t)ts
* s
->c
;
2106 } else if (bt
== VT_LDOUBLE
) {
2108 size
= LDOUBLE_SIZE
;
2109 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
2110 #ifdef TCC_TARGET_I386
2111 #ifdef TCC_TARGET_PE
2116 #elif defined(TCC_TARGET_ARM)
2126 } else if (bt
== VT_INT
|| bt
== VT_ENUM
|| bt
== VT_FLOAT
) {
2129 } else if (bt
== VT_SHORT
) {
2132 } else if (bt
== VT_QLONG
|| bt
== VT_QFLOAT
) {
2136 /* char, void, function, _Bool */
2140 assert(size
== (int)size
);
2144 /* push type size as known at runtime time on top of value stack. Put
2146 ST_FUNC
void vla_runtime_type_size(CType
*type
, int *a
)
2148 if (type
->t
& VT_VLA
) {
2149 vset(&int_type
, VT_LOCAL
|VT_LVAL
, type
->ref
->c
);
2151 vpushi(type_size(type
, a
));
2155 static void vla_sp_save(void) {
2156 if (!(vla_flags
& VLA_SP_LOC_SET
)) {
2157 *vla_sp_loc
= (loc
-= PTR_SIZE
);
2158 vla_flags
|= VLA_SP_LOC_SET
;
2160 if (!(vla_flags
& VLA_SP_SAVED
)) {
2161 gen_vla_sp_save(*vla_sp_loc
);
2162 vla_flags
|= VLA_SP_SAVED
;
2166 /* return the pointed type of t */
2167 static inline CType
*pointed_type(CType
*type
)
2169 return &type
->ref
->type
;
2172 /* modify type so that its it is a pointer to type. */
2173 ST_FUNC
void mk_pointer(CType
*type
)
2176 s
= sym_push(SYM_FIELD
, type
, 0, -1);
2177 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
2181 /* compare function types. OLD functions match any new functions */
2182 static int is_compatible_func(CType
*type1
, CType
*type2
)
2188 if (!is_compatible_types(&s1
->type
, &s2
->type
))
2190 /* check func_call */
2191 if (s1
->a
.func_call
!= s2
->a
.func_call
)
2193 /* XXX: not complete */
2194 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
2198 while (s1
!= NULL
) {
2201 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
2211 /* return true if type1 and type2 are the same. If unqualified is
2212 true, qualifiers on the types are ignored.
2214 - enums are not checked as gcc __builtin_types_compatible_p ()
2216 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2220 t1
= type1
->t
& VT_TYPE
;
2221 t2
= type2
->t
& VT_TYPE
;
2223 /* strip qualifiers before comparing */
2224 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2225 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2227 /* Default Vs explicit signedness only matters for char */
2228 if ((t1
& VT_BTYPE
) != VT_BYTE
) {
2232 /* XXX: bitfields ? */
2235 /* test more complicated cases */
2236 bt1
= t1
& VT_BTYPE
;
2237 if (bt1
== VT_PTR
) {
2238 type1
= pointed_type(type1
);
2239 type2
= pointed_type(type2
);
2240 return is_compatible_types(type1
, type2
);
2241 } else if (bt1
== VT_STRUCT
) {
2242 return (type1
->ref
== type2
->ref
);
2243 } else if (bt1
== VT_FUNC
) {
2244 return is_compatible_func(type1
, type2
);
2250 /* return true if type1 and type2 are exactly the same (including
2253 static int is_compatible_types(CType
*type1
, CType
*type2
)
2255 return compare_types(type1
,type2
,0);
2258 /* return true if type1 and type2 are the same (ignoring qualifiers).
2260 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
2262 return compare_types(type1
,type2
,1);
2265 /* print a type. If 'varstr' is not NULL, then the variable is also
2266 printed in the type */
2268 /* XXX: add array and function pointers */
2269 static void type_to_str(char *buf
, int buf_size
,
2270 CType
*type
, const char *varstr
)
2277 t
= type
->t
& VT_TYPE
;
2280 if (t
& VT_CONSTANT
)
2281 pstrcat(buf
, buf_size
, "const ");
2282 if (t
& VT_VOLATILE
)
2283 pstrcat(buf
, buf_size
, "volatile ");
2284 if ((t
& (VT_DEFSIGN
| VT_UNSIGNED
)) == (VT_DEFSIGN
| VT_UNSIGNED
))
2285 pstrcat(buf
, buf_size
, "unsigned ");
2286 else if (t
& VT_DEFSIGN
)
2287 pstrcat(buf
, buf_size
, "signed ");
2317 tstr
= "long double";
2319 pstrcat(buf
, buf_size
, tstr
);
2323 if (bt
== VT_STRUCT
)
2327 pstrcat(buf
, buf_size
, tstr
);
2328 v
= type
->ref
->v
& ~SYM_STRUCT
;
2329 if (v
>= SYM_FIRST_ANOM
)
2330 pstrcat(buf
, buf_size
, "<anonymous>");
2332 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
2336 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
2337 pstrcat(buf
, buf_size
, "(");
2339 while (sa
!= NULL
) {
2340 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
2341 pstrcat(buf
, buf_size
, buf1
);
2344 pstrcat(buf
, buf_size
, ", ");
2346 pstrcat(buf
, buf_size
, ")");
2350 pstrcpy(buf1
, sizeof(buf1
), "*");
2352 pstrcat(buf1
, sizeof(buf1
), varstr
);
2353 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2357 pstrcat(buf
, buf_size
, " ");
2358 pstrcat(buf
, buf_size
, varstr
);
2363 /* verify type compatibility to store vtop in 'dt' type, and generate
2365 static void gen_assign_cast(CType
*dt
)
2367 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
2368 char buf1
[256], buf2
[256];
2371 st
= &vtop
->type
; /* source type */
2372 dbt
= dt
->t
& VT_BTYPE
;
2373 sbt
= st
->t
& VT_BTYPE
;
2374 if (sbt
== VT_VOID
|| dbt
== VT_VOID
)
2375 tcc_error("cannot cast from/to void");
2376 if (dt
->t
& VT_CONSTANT
)
2377 tcc_warning("assignment of read-only location");
2380 /* special cases for pointers */
2381 /* '0' can also be a pointer */
2382 if (is_null_pointer(vtop
))
2384 /* accept implicit pointer to integer cast with warning */
2385 if (is_integer_btype(sbt
)) {
2386 tcc_warning("assignment makes pointer from integer without a cast");
2389 type1
= pointed_type(dt
);
2390 /* a function is implicitely a function pointer */
2391 if (sbt
== VT_FUNC
) {
2392 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
2393 !is_compatible_types(pointed_type(dt
), st
))
2394 tcc_warning("assignment from incompatible pointer type");
2399 type2
= pointed_type(st
);
2400 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2401 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
2402 /* void * can match anything */
2404 /* exact type match, except for unsigned */
2407 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
|
2409 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
|
2411 if (!is_compatible_types(&tmp_type1
, &tmp_type2
))
2412 tcc_warning("assignment from incompatible pointer type");
2414 /* check const and volatile */
2415 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
2416 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
2417 tcc_warning("assignment discards qualifiers from pointer target type");
2423 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
2424 tcc_warning("assignment makes integer from pointer without a cast");
2426 /* XXX: more tests */
2431 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2432 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2433 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2435 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
2436 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
2437 tcc_error("cannot cast '%s' to '%s'", buf1
, buf2
);
2445 static void vstore_im(){
2446 int rc
, ft
, sbt
, dbt
, t
, r
;
2447 ft
= vtop
[-1].type
.t
;
2448 sbt
= vtop
->type
.t
& VT_BTYPE
;
2449 dbt
= ft
& VT_BTYPE
;
2452 #ifdef TCC_TARGET_X86_64
2453 if (dbt
== VT_LDOUBLE
) {
2459 r
= gv(rc
); /* generate value */
2460 /* if lvalue was saved on stack, must read it */
2461 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
2463 t
= get_reg(RC_INT
);
2464 #ifdef TCC_TARGET_X86_64
2469 sv
.r
= VT_LOCAL
| VT_LVAL
;
2470 sv
.c
.ul
= vtop
[-1].c
.ul
;
2472 vtop
[-1].r
= t
| VT_LVAL
;
2475 /* two word case handling : store second register at word + 4 */
2476 #ifdef TCC_TARGET_X86_64
2477 if ((dbt
== VT_QLONG
) || (dbt
== VT_QFLOAT
))
2479 if (dbt
== VT_LLONG
)
2482 #ifdef TCC_TARGET_X86_64
2483 int load_size
= 8, load_type
= (sbt
== VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
2485 int load_size
= 4, load_type
= VT_INT
;
2487 vtop
[-1].type
.t
= load_type
;
2490 /* convert to int to increment easily */
2491 vtop
->type
= char_pointer_type
;
2497 vtop
[-1].type
.t
= load_type
;
2498 /* XXX: it works because r2 is spilled last ! */
2499 store(vtop
->r2
, vtop
- 1);
2501 vtop
[-1].type
.t
= ft
;
2507 /* store vtop in lvalue pushed on stack */
2508 ST_FUNC
void vstore(void)
2510 int sbt
, dbt
, ft
, size
, align
, bit_size
, bit_pos
, delayed_cast
;
2512 ft
= vtop
[-1].type
.t
;
2513 sbt
= vtop
->type
.t
& VT_BTYPE
;
2514 dbt
= ft
& VT_BTYPE
;
2515 if ((((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2516 (sbt
== VT_INT
&& dbt
== VT_SHORT
)) && !(vtop
->type
.t
& VT_BITFIELD
)) {
2517 /* optimize char/short casts */
2518 delayed_cast
= VT_MUSTCAST
;
2519 vtop
->type
.t
= ft
& (VT_TYPE
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
)));
2520 /* XXX: factorize */
2521 if (ft
& VT_CONSTANT
)
2522 tcc_warning("assignment of read-only location");
2525 if (!(ft
& VT_BITFIELD
))
2526 gen_assign_cast(&vtop
[-1].type
);
2529 if (sbt
== VT_STRUCT
) {
2530 /* if structure, only generate pointer */
2531 /* structure assignment : generate memcpy */
2532 /* XXX: optimize if small size */
2533 if (!nocode_wanted
) {
2535 int ret_nregs
, ret_align
;
2536 ret_nregs
= gfunc_sret(&vtop
->type
, func_var
, &ret
.type
, &ret_align
);
2540 vtop
[0].type
= ret
.type
;
2541 vtop
[-1].type
= ret
.type
;
2545 size
= type_size(&vtop
->type
, &align
);
2549 vtop
->type
.t
= VT_PTR
;
2552 /* address of memcpy() */
2554 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2555 else if(!(align
& 3))
2556 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2558 vpush_global_sym(&func_old_type
, TOK_memcpy
);
2563 vtop
->type
.t
= VT_PTR
;
2571 vtop
->type
.t
= VT_PTR
;
2575 vtop
->type
.t
= VT_PTR
;
2579 struct_copy(&vtop
[-2], &vtop
[-1], &vtop
[0]);
2587 /* leave source on stack */
2588 } else if (ft
& VT_BITFIELD
) {
2589 /* bitfield store handling */
2590 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2591 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2592 /* remove bit field info to avoid loops */
2593 vtop
[-1].type
.t
= ft
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
2594 /* duplicate source into other register */
2595 if(dbt
== VT_BOOL
) {
2596 gen_cast(&vtop
[-1].type
);
2597 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2600 /* duplicate destination */
2602 vtop
[-1] = vtop
[-2];
2604 /* mask and shift source */
2605 if(dbt
!= VT_BOOL
) {
2606 if(dbt
== VT_LLONG
) {
2607 vpushll((1ULL << bit_size
) - 1ULL);
2609 vpushi((1 << bit_size
) - 1);
2615 /* load destination, mask and or with source */
2617 if(dbt
== VT_LLONG
) {
2618 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2620 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2627 #ifdef CONFIG_TCC_BCHECK
2628 /* bound check case */
2629 if (vtop
[-1].r
& VT_MUSTBOUND
) {
2635 if (!nocode_wanted
) {
2639 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
2640 vtop
->r
|= delayed_cast
;
2644 /* post defines POST/PRE add. c is the token ++ or -- */
2645 ST_FUNC
void inc(int post
, int c
)
2648 vdup(); /* save lvalue */
2650 gv_dup(); /* duplicate value */
2655 vpushi(c
- TOK_MID
);
2657 vstore(); /* store value */
2659 vpop(); /* if post op, return saved value */
2662 /* Parse GNUC __attribute__ extension. Currently, the following
2663 extensions are recognized:
2664 - aligned(n) : set data/function alignment.
2665 - packed : force data alignment to 1
2666 - section(x) : generate data/code in this section.
2667 - unused : currently ignored, but may be used someday.
2668 - regparm(n) : pass function parameters in registers (i386 only)
2670 static void parse_attribute(AttributeDef
*ad
)
2674 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
2678 while (tok
!= ')') {
2679 if (tok
< TOK_IDENT
)
2680 expect("attribute name");
2688 expect("section name");
2689 ad
->section
= find_section(tcc_state
, (char *)tokc
.cstr
->data
);
2697 expect("alias(\"target\")");
2698 ad
->alias_target
= /* save string as token, for later */
2699 tok_alloc((char*)tokc
.cstr
->data
, tokc
.cstr
->size
-1)->tok
;
2703 case TOK_VISIBILITY1
:
2704 case TOK_VISIBILITY2
:
2707 expect("visibility(\"default|hidden|internal|protected\")");
2708 if (!strcmp (tokc
.cstr
->data
, "default"))
2709 ad
->a
.visibility
= STV_DEFAULT
;
2710 else if (!strcmp (tokc
.cstr
->data
, "hidden"))
2711 ad
->a
.visibility
= STV_HIDDEN
;
2712 else if (!strcmp (tokc
.cstr
->data
, "internal"))
2713 ad
->a
.visibility
= STV_INTERNAL
;
2714 else if (!strcmp (tokc
.cstr
->data
, "protected"))
2715 ad
->a
.visibility
= STV_PROTECTED
;
2717 expect("visibility(\"default|hidden|internal|protected\")");
2726 if (n
<= 0 || (n
& (n
- 1)) != 0)
2727 tcc_error("alignment must be a positive power of two");
2744 /* currently, no need to handle it because tcc does not
2745 track unused objects */
2749 /* currently, no need to handle it because tcc does not
2750 track unused objects */
2755 ad
->a
.func_call
= FUNC_CDECL
;
2760 ad
->a
.func_call
= FUNC_STDCALL
;
2762 #ifdef TCC_TARGET_I386
2772 ad
->a
.func_call
= FUNC_FASTCALL1
+ n
- 1;
2778 ad
->a
.func_call
= FUNC_FASTCALLW
;
2785 ad
->a
.mode
= VT_LLONG
+ 1;
2788 ad
->a
.mode
= VT_SHORT
+ 1;
2791 ad
->a
.mode
= VT_INT
+ 1;
2794 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
2801 ad
->a
.func_export
= 1;
2804 ad
->a
.func_import
= 1;
2807 if (tcc_state
->warn_unsupported
)
2808 tcc_warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
2809 /* skip parameters */
2811 int parenthesis
= 0;
2815 else if (tok
== ')')
2818 } while (parenthesis
&& tok
!= -1);
2831 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2832 static void struct_decl(CType
*type
, int u
, int tdef
)
2834 int a
, v
, size
, align
, maxalign
, c
, offset
, flexible
;
2835 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
, prevbt
;
2836 Sym
*s
, *ss
, *ass
, **ps
;
2840 a
= tok
; /* save decl type */
2845 /* struct already defined ? return it */
2847 expect("struct/union/enum name");
2851 tcc_error("invalid type");
2853 } else if (tok
>= TOK_IDENT
&& !tdef
)
2854 tcc_error("unknown struct/union/enum");
2860 /* we put an undefined size for struct/union */
2861 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
2862 s
->r
= 0; /* default alignment is zero as gcc */
2863 /* put struct/union/enum name in type */
2871 tcc_error("struct/union/enum already defined");
2872 /* cannot be empty */
2874 /* non empty enums are not allowed */
2875 if (a
== TOK_ENUM
) {
2879 expect("identifier");
2881 if (ss
&& !local_stack
)
2882 tcc_error("redefinition of enumerator '%s'",
2883 get_tok_str(v
, NULL
));
2889 /* enum symbols have static storage */
2890 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
2891 ss
->type
.t
|= VT_STATIC
;
2896 /* NOTE: we accept a trailing comma */
2900 s
->c
= type_size(&int_type
, &align
);
2909 while (tok
!= '}') {
2910 parse_btype(&btype
, &ad
);
2913 tcc_error("flexible array member '%s' not at the end of struct",
2914 get_tok_str(v
, NULL
));
2919 type_decl(&type1
, &ad
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
2920 if (v
== 0 && (type1
.t
& VT_BTYPE
) != VT_STRUCT
)
2921 expect("identifier");
2922 if (type_size(&type1
, &align
) < 0) {
2923 if ((a
== TOK_STRUCT
) && (type1
.t
& VT_ARRAY
) && c
)
2926 tcc_error("field '%s' has incomplete type",
2927 get_tok_str(v
, NULL
));
2929 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
2930 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
2931 tcc_error("invalid type for '%s'",
2932 get_tok_str(v
, NULL
));
2936 bit_size
= expr_const();
2937 /* XXX: handle v = 0 case for messages */
2939 tcc_error("negative width in bit-field '%s'",
2940 get_tok_str(v
, NULL
));
2941 if (v
&& bit_size
== 0)
2942 tcc_error("zero width for bit-field '%s'",
2943 get_tok_str(v
, NULL
));
2945 size
= type_size(&type1
, &align
);
2947 if (align
< ad
.a
.aligned
)
2948 align
= ad
.a
.aligned
;
2949 } else if (ad
.a
.packed
) {
2951 } else if (*tcc_state
->pack_stack_ptr
) {
2952 if (align
> *tcc_state
->pack_stack_ptr
)
2953 align
= *tcc_state
->pack_stack_ptr
;
2956 if (bit_size
>= 0) {
2957 bt
= type1
.t
& VT_BTYPE
;
2964 tcc_error("bitfields must have scalar type");
2966 if (bit_size
> bsize
) {
2967 tcc_error("width of '%s' exceeds its type",
2968 get_tok_str(v
, NULL
));
2969 } else if (bit_size
== bsize
) {
2970 /* no need for bit fields */
2972 } else if (bit_size
== 0) {
2973 /* XXX: what to do if only padding in a
2975 /* zero size: means to pad */
2978 /* we do not have enough room ?
2979 did the type change?
2981 if ((bit_pos
+ bit_size
) > bsize
||
2982 bt
!= prevbt
|| a
== TOK_UNION
)
2985 /* XXX: handle LSB first */
2986 type1
.t
|= VT_BITFIELD
|
2987 (bit_pos
<< VT_STRUCT_SHIFT
) |
2988 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
2989 bit_pos
+= bit_size
;
2995 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2996 /* add new memory data only if starting
2998 if (lbit_pos
== 0) {
2999 if (a
== TOK_STRUCT
) {
3000 c
= (c
+ align
- 1) & -align
;
3009 if (align
> maxalign
)
3013 printf("add field %s offset=%d",
3014 get_tok_str(v
, NULL
), offset
);
3015 if (type1
.t
& VT_BITFIELD
) {
3016 printf(" pos=%d size=%d",
3017 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
3018 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
3023 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
3025 while ((ass
= ass
->next
) != NULL
) {
3026 ss
= sym_push(ass
->v
, &ass
->type
, 0, offset
+ ass
->c
);
3031 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
3035 if (tok
== ';' || tok
== TOK_EOF
)
3042 /* store size and alignment */
3043 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
3049 /* return 1 if basic type is a type size (short, long, long long) */
3050 ST_FUNC
int is_btype_size(int bt
)
3052 return bt
== VT_SHORT
|| bt
== VT_LONG
|| bt
== VT_LLONG
;
3055 /* return 0 if no type declaration. otherwise, return the basic type
3058 static int parse_btype(CType
*type
, AttributeDef
*ad
)
3060 int t
, u
, bt_size
, complete
, type_found
, typespec_found
;
3064 memset(ad
, 0, sizeof(AttributeDef
));
3072 /* currently, we really ignore extension */
3083 tcc_error("too many basic types");
3085 bt_size
= is_btype_size (u
& VT_BTYPE
);
3086 if (u
== VT_INT
|| (!bt_size
&& !(t
& VT_TYPEDEF
)))
3101 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
3102 #ifndef TCC_TARGET_PE
3103 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3105 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
3106 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3120 if ((t
& VT_BTYPE
) == VT_LONG
) {
3121 #ifdef TCC_TARGET_PE
3122 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
3124 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3132 struct_decl(&type1
, VT_ENUM
, t
& VT_TYPEDEF
);
3135 type
->ref
= type1
.ref
;
3139 struct_decl(&type1
, VT_STRUCT
, t
& VT_TYPEDEF
);
3142 /* type modifiers */
3158 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == (VT_DEFSIGN
|VT_UNSIGNED
))
3159 tcc_error("signed and unsigned modifier");
3172 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == VT_DEFSIGN
)
3173 tcc_error("signed and unsigned modifier");
3174 t
|= VT_DEFSIGN
| VT_UNSIGNED
;
3199 /* GNUC attribute */
3200 case TOK_ATTRIBUTE1
:
3201 case TOK_ATTRIBUTE2
:
3202 parse_attribute(ad
);
3205 t
= (t
& ~VT_BTYPE
) | u
;
3213 parse_expr_type(&type1
);
3214 /* remove all storage modifiers except typedef */
3215 type1
.t
&= ~(VT_STORAGE
&~VT_TYPEDEF
);
3221 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
3223 t
|= (s
->type
.t
& ~VT_TYPEDEF
);
3224 type
->ref
= s
->type
.ref
;
3226 /* get attributes from typedef */
3227 if (0 == ad
->a
.aligned
)
3228 ad
->a
.aligned
= s
->a
.aligned
;
3229 if (0 == ad
->a
.func_call
)
3230 ad
->a
.func_call
= s
->a
.func_call
;
3231 ad
->a
.packed
|= s
->a
.packed
;
3240 if (tcc_state
->char_is_unsigned
) {
3241 if ((t
& (VT_DEFSIGN
|VT_BTYPE
)) == VT_BYTE
)
3245 /* long is never used as type */
3246 if ((t
& VT_BTYPE
) == VT_LONG
)
3247 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
3248 t
= (t
& ~VT_BTYPE
) | VT_INT
;
3250 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3256 /* convert a function parameter type (array to pointer and function to
3257 function pointer) */
3258 static inline void convert_parameter_type(CType
*pt
)
3260 /* remove const and volatile qualifiers (XXX: const could be used
3261 to indicate a const function parameter */
3262 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3263 /* array must be transformed to pointer according to ANSI C */
3265 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
3270 ST_FUNC
void parse_asm_str(CString
*astr
)
3273 /* read the string */
3275 expect("string constant");
3277 while (tok
== TOK_STR
) {
3278 /* XXX: add \0 handling too ? */
3279 cstr_cat(astr
, tokc
.cstr
->data
);
3282 cstr_ccat(astr
, '\0');
3285 /* Parse an asm label and return the label
3286 * Don't forget to free the CString in the caller! */
3287 static void asm_label_instr(CString
*astr
)
3290 parse_asm_str(astr
);
3293 printf("asm_alias: \"%s\"\n", (char *)astr
->data
);
3297 static void post_type(CType
*type
, AttributeDef
*ad
)
3299 int n
, l
, t1
, arg_size
, align
;
3300 Sym
**plast
, *s
, *first
;
3305 /* function declaration */
3313 /* read param name and compute offset */
3314 if (l
!= FUNC_OLD
) {
3315 if (!parse_btype(&pt
, &ad1
)) {
3317 tcc_error("invalid type");
3324 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
3326 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3327 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
3328 tcc_error("parameter declared as void");
3329 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
3334 expect("identifier");
3338 convert_parameter_type(&pt
);
3339 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
3345 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
3352 /* if no parameters, then old type prototype */
3356 /* NOTE: const is ignored in returned type as it has a special
3357 meaning in gcc / C++ */
3358 type
->t
&= ~VT_CONSTANT
;
3359 /* some ancient pre-K&R C allows a function to return an array
3360 and the array brackets to be put after the arguments, such
3361 that "int c()[]" means something like "int[] c()" */
3364 skip(']'); /* only handle simple "[]" */
3367 /* we push a anonymous symbol which will contain the function prototype */
3368 ad
->a
.func_args
= arg_size
;
3369 s
= sym_push(SYM_FIELD
, type
, 0, l
);
3374 } else if (tok
== '[') {
3375 /* array definition */
3377 if (tok
== TOK_RESTRICT1
)
3382 if (!local_stack
|| nocode_wanted
)
3383 vpushi(expr_const());
3385 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3388 tcc_error("invalid array size");
3390 if (!is_integer_btype(vtop
->type
.t
& VT_BTYPE
))
3391 tcc_error("size of variable length array should be an integer");
3396 /* parse next post type */
3397 post_type(type
, ad
);
3398 if (type
->t
== VT_FUNC
)
3399 tcc_error("declaration of an array of functions");
3400 t1
|= type
->t
& VT_VLA
;
3403 loc
-= type_size(&int_type
, &align
);
3407 vla_runtime_type_size(type
, &align
);
3409 vset(&int_type
, VT_LOCAL
|VT_LVAL
, loc
);
3416 /* we push an anonymous symbol which will contain the array
3418 s
= sym_push(SYM_FIELD
, type
, 0, n
);
3419 type
->t
= (t1
? VT_VLA
: VT_ARRAY
) | VT_PTR
;
3424 /* Parse a type declaration (except basic type), and return the type
3425 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3426 expected. 'type' should contain the basic type. 'ad' is the
3427 attribute definition of the basic type. It can be modified by
3430 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
3433 CType type1
, *type2
;
3434 int qualifiers
, storage
;
3436 while (tok
== '*') {
3444 qualifiers
|= VT_CONSTANT
;
3449 qualifiers
|= VT_VOLATILE
;
3457 type
->t
|= qualifiers
;
3460 /* XXX: clarify attribute handling */
3461 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3462 parse_attribute(ad
);
3464 /* recursive type */
3465 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3466 type1
.t
= 0; /* XXX: same as int */
3469 /* XXX: this is not correct to modify 'ad' at this point, but
3470 the syntax is not clear */
3471 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3472 parse_attribute(ad
);
3473 type_decl(&type1
, ad
, v
, td
);
3476 /* type identifier */
3477 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
3481 if (!(td
& TYPE_ABSTRACT
))
3482 expect("identifier");
3486 storage
= type
->t
& VT_STORAGE
;
3487 type
->t
&= ~VT_STORAGE
;
3488 if (storage
& VT_STATIC
) {
3489 int saved_nocode_wanted
= nocode_wanted
;
3491 post_type(type
, ad
);
3492 nocode_wanted
= saved_nocode_wanted
;
3494 post_type(type
, ad
);
3496 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3497 parse_attribute(ad
);
3501 /* append type at the end of type1 */
3514 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3515 ST_FUNC
int lvalue_type(int t
)
3520 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
3522 else if (bt
== VT_SHORT
)
3526 if (t
& VT_UNSIGNED
)
3527 r
|= VT_LVAL_UNSIGNED
;
3531 /* indirection with full error checking and bound check */
3532 ST_FUNC
void indir(void)
3534 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
3535 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
3539 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
3541 vtop
->type
= *pointed_type(&vtop
->type
);
3542 /* Arrays and functions are never lvalues */
3543 if (!(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_VLA
)
3544 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3545 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3546 /* if bound checking, the referenced pointer must be checked */
3547 #ifdef CONFIG_TCC_BCHECK
3548 if (tcc_state
->do_bounds_check
)
3549 vtop
->r
|= VT_MUSTBOUND
;
3554 /* pass a parameter to a function and do type checking and casting */
3555 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
3560 func_type
= func
->c
;
3561 if (func_type
== FUNC_OLD
||
3562 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
3563 /* default casting : only need to convert float to double */
3564 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
3567 } else if (vtop
->type
.t
& VT_BITFIELD
) {
3568 type
.t
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
3571 } else if (arg
== NULL
) {
3572 tcc_error("too many arguments to function");
3575 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
3576 gen_assign_cast(&type
);
3580 /* parse an expression of the form '(type)' or '(expr)' and return its
3582 static void parse_expr_type(CType
*type
)
3588 if (parse_btype(type
, &ad
)) {
3589 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3596 static void parse_type(CType
*type
)
3601 if (!parse_btype(type
, &ad
)) {
3604 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3607 static void vpush_tokc(int t
)
3612 vsetc(&type
, VT_CONST
, &tokc
);
3615 ST_FUNC
void unary(void)
3617 int n
, t
, align
, size
, r
, sizeof_caller
;
3621 static int in_sizeof
= 0;
3623 sizeof_caller
= in_sizeof
;
3625 /* XXX: GCC 2.95.3 does not generate a table although it should be
3639 vpush_tokc(VT_INT
| VT_UNSIGNED
);
3643 vpush_tokc(VT_LLONG
);
3647 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
3651 vpush_tokc(VT_FLOAT
);
3655 vpush_tokc(VT_DOUBLE
);
3659 vpush_tokc(VT_LDOUBLE
);
3662 case TOK___FUNCTION__
:
3664 goto tok_identifier
;
3670 /* special function name identifier */
3671 len
= strlen(funcname
) + 1;
3672 /* generate char[len] type */
3677 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
3678 ptr
= section_ptr_add(data_section
, len
);
3679 memcpy(ptr
, funcname
, len
);
3684 #ifdef TCC_TARGET_PE
3685 t
= VT_SHORT
| VT_UNSIGNED
;
3691 /* string parsing */
3694 if (tcc_state
->warn_write_strings
)
3699 memset(&ad
, 0, sizeof(AttributeDef
));
3700 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, NULL
, 0);
3705 if (parse_btype(&type
, &ad
)) {
3706 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
3708 /* check ISOC99 compound literal */
3710 /* data is allocated locally by default */
3715 /* all except arrays are lvalues */
3716 if (!(type
.t
& VT_ARRAY
))
3717 r
|= lvalue_type(type
.t
);
3718 memset(&ad
, 0, sizeof(AttributeDef
));
3719 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, NULL
, 0);
3721 if (sizeof_caller
) {
3728 } else if (tok
== '{') {
3729 /* save all registers */
3731 /* statement expression : we do not accept break/continue
3732 inside as GCC does */
3733 block(NULL
, NULL
, NULL
, NULL
, 0, 1);
3748 /* functions names must be treated as function pointers,
3749 except for unary '&' and sizeof. Since we consider that
3750 functions are not lvalues, we only have to handle it
3751 there and in function calls. */
3752 /* arrays can also be used although they are not lvalues */
3753 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
3754 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
3756 mk_pointer(&vtop
->type
);
3762 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3764 boolean
.t
= VT_BOOL
;
3766 vtop
->c
.i
= !vtop
->c
.i
;
3767 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
3768 vtop
->c
.i
= vtop
->c
.i
^ 1;
3771 vseti(VT_JMP
, gtst(1, 0));
3783 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
3784 tcc_error("pointer not accepted for unary plus");
3785 /* In order to force cast, we add zero, except for floating point
3786 where we really need an noop (otherwise -0.0 will be transformed
3788 if (!is_float(vtop
->type
.t
)) {
3799 unary_type(&type
); // Perform a in_sizeof = 0;
3800 size
= type_size(&type
, &align
);
3801 if (t
== TOK_SIZEOF
) {
3802 if (!(type
.t
& VT_VLA
)) {
3804 tcc_error("sizeof applied to an incomplete type");
3807 vla_runtime_type_size(&type
, &align
);
3812 vtop
->type
.t
|= VT_UNSIGNED
;
3815 case TOK_builtin_types_compatible_p
:
3824 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3825 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3826 vpushi(is_compatible_types(&type1
, &type2
));
3829 case TOK_builtin_constant_p
:
3831 int saved_nocode_wanted
, res
;
3834 saved_nocode_wanted
= nocode_wanted
;
3837 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
3839 nocode_wanted
= saved_nocode_wanted
;
3844 case TOK_builtin_frame_address
:
3850 if (tok
!= TOK_CINT
|| tokc
.i
< 0) {
3851 tcc_error("__builtin_frame_address only takes positive integers");
3858 vset(&type
, VT_LOCAL
, 0); /* local frame */
3860 mk_pointer(&vtop
->type
);
3861 indir(); /* -> parent frame */
3865 #ifdef TCC_TARGET_X86_64
3866 #ifdef TCC_TARGET_PE
3867 case TOK_builtin_va_start
:
3875 if ((vtop
->r
& VT_VALMASK
) != VT_LOCAL
)
3876 tcc_error("__builtin_va_start expects a local variable");
3877 vtop
->r
&= ~(VT_LVAL
| VT_REF
);
3878 vtop
->type
= char_pointer_type
;
3883 case TOK_builtin_va_arg_types
:
3890 vpushi(classify_x86_64_va_arg(&type
));
3905 t
= vtop
->type
.t
& VT_BTYPE
;
3907 /* In IEEE negate(x) isn't subtract(0,x), but rather
3912 else if (t
== VT_DOUBLE
)
3923 goto tok_identifier
;
3925 /* allow to take the address of a label */
3926 if (tok
< TOK_UIDENT
)
3927 expect("label identifier");
3928 s
= label_find(tok
);
3930 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
3932 if (s
->r
== LABEL_DECLARED
)
3933 s
->r
= LABEL_FORWARD
;
3936 s
->type
.t
= VT_VOID
;
3937 mk_pointer(&s
->type
);
3938 s
->type
.t
|= VT_STATIC
;
3940 vpushsym(&s
->type
, s
);
3944 // special qnan , snan and infinity values
3946 vpush64(VT_DOUBLE
, 0x7ff8000000000000ULL
);
3950 vpush64(VT_DOUBLE
, 0x7ff0000000000001ULL
);
3954 vpush64(VT_DOUBLE
, 0x7ff0000000000000ULL
);
3963 expect("identifier");
3966 const char *name
= get_tok_str(t
, NULL
);
3968 tcc_error("'%s' undeclared", name
);
3969 /* for simple function calls, we tolerate undeclared
3970 external reference to int() function */
3971 if (tcc_state
->warn_implicit_function_declaration
3972 #ifdef TCC_TARGET_PE
3973 /* people must be warned about using undeclared WINAPI functions
3974 (which usually start with uppercase letter) */
3975 || (name
[0] >= 'A' && name
[0] <= 'Z')
3978 tcc_warning("implicit declaration of function '%s'", name
);
3979 s
= external_global_sym(t
, &func_old_type
, 0);
3981 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
3982 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
3983 /* if referencing an inline function, then we generate a
3984 symbol to it if not already done. It will have the
3985 effect to generate code for it at the end of the
3986 compilation unit. Inline function as always
3987 generated in the text section. */
3989 put_extern_sym(s
, text_section
, 0, 0);
3990 r
= VT_SYM
| VT_CONST
;
3994 vset(&s
->type
, r
, s
->c
);
3995 /* if forward reference, we must point to s */
3996 if (vtop
->r
& VT_SYM
) {
3998 vtop
->c
.ptr_offset
= 0;
4003 /* post operations */
4005 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
4008 } else if (tok
== '.' || tok
== TOK_ARROW
) {
4011 if (tok
== TOK_ARROW
)
4013 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
4017 /* expect pointer on structure */
4018 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
4019 expect("struct or union");
4023 while ((s
= s
->next
) != NULL
) {
4028 tcc_error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, NULL
));
4029 /* add field offset to pointer */
4030 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
4033 /* change type to field type, and set to lvalue */
4034 vtop
->type
= s
->type
;
4035 vtop
->type
.t
|= qualifiers
;
4036 /* an array is never an lvalue */
4037 if (!(vtop
->type
.t
& VT_ARRAY
)) {
4038 vtop
->r
|= lvalue_type(vtop
->type
.t
);
4039 #ifdef CONFIG_TCC_BCHECK
4040 /* if bound checking, the referenced pointer must be checked */
4041 if (tcc_state
->do_bounds_check
)
4042 vtop
->r
|= VT_MUSTBOUND
;
4046 } else if (tok
== '[') {
4052 } else if (tok
== '(') {
4055 int nb_args
, ret_nregs
, ret_align
, variadic
;
4058 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
4059 /* pointer test (no array accepted) */
4060 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
4061 vtop
->type
= *pointed_type(&vtop
->type
);
4062 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
4066 expect("function pointer");
4069 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
4071 /* get return type */
4074 sa
= s
->next
; /* first parameter */
4077 /* compute first implicit argument if a structure is returned */
4078 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
4079 variadic
= (s
->c
== FUNC_ELLIPSIS
);
4080 ret_nregs
= gfunc_sret(&s
->type
, variadic
, &ret
.type
,
4083 /* get some space for the returned structure */
4084 size
= type_size(&s
->type
, &align
);
4085 loc
= (loc
- size
) & -align
;
4087 ret
.r
= VT_LOCAL
| VT_LVAL
;
4088 /* pass it as 'int' to avoid structure arg passing
4090 vseti(VT_LOCAL
, loc
);
4100 /* return in register */
4101 if (is_float(ret
.type
.t
)) {
4102 ret
.r
= reg_fret(ret
.type
.t
);
4103 #ifdef TCC_TARGET_X86_64
4104 if ((ret
.type
.t
& VT_BTYPE
) == VT_QFLOAT
)
4108 #ifdef TCC_TARGET_X86_64
4109 if ((ret
.type
.t
& VT_BTYPE
) == VT_QLONG
)
4111 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
4121 gfunc_param_typed(s
, sa
);
4131 tcc_error("too few arguments to function");
4133 if (!nocode_wanted
) {
4134 gfunc_call(nb_args
);
4136 vtop
-= (nb_args
+ 1);
4140 for (r
= ret
.r
+ ret_nregs
+ !ret_nregs
; r
-- > ret
.r
;) {
4141 vsetc(&ret
.type
, r
, &ret
.c
);
4142 vtop
->r2
= ret
.r2
; /* Loop only happens when r2 is VT_CONST */
4145 /* handle packed struct return */
4146 if (((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) && ret_nregs
) {
4149 size
= type_size(&s
->type
, &align
);
4150 loc
= (loc
- size
) & -align
;
4154 vset(&ret
.type
, VT_LOCAL
| VT_LVAL
, addr
+ offset
);
4158 if (--ret_nregs
== 0)
4160 /* XXX: compatible with arm only: ret_align == register_size */
4161 offset
+= ret_align
;
4163 vset(&s
->type
, VT_LOCAL
| VT_LVAL
, addr
);
4171 ST_FUNC
void expr_prod(void)
4176 while (tok
== '*' || tok
== '/' || tok
== '%') {
4184 ST_FUNC
void expr_sum(void)
4189 while (tok
== '+' || tok
== '-') {
4197 static void expr_shift(void)
4202 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
4210 static void expr_cmp(void)
4215 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
4216 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
4224 static void expr_cmpeq(void)
4229 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
4237 static void expr_and(void)
4240 while (tok
== '&') {
4247 static void expr_xor(void)
4250 while (tok
== '^') {
4257 static void expr_or(void)
4260 while (tok
== '|') {
4267 /* XXX: fix this mess */
4268 static void expr_land_const(void)
4271 while (tok
== TOK_LAND
) {
4278 /* XXX: fix this mess */
4279 static void expr_lor_const(void)
4282 while (tok
== TOK_LOR
) {
4289 /* only used if non constant */
4290 static void expr_land(void)
4295 if (tok
== TOK_LAND
) {
4300 if (tok
!= TOK_LAND
) {
4310 static void expr_lor(void)
4315 if (tok
== TOK_LOR
) {
4320 if (tok
!= TOK_LOR
) {
4330 /* XXX: better constant handling */
4331 static void expr_cond(void)
4333 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
;
4335 CType type
, type1
, type2
;
4342 boolean
.t
= VT_BOOL
;
4348 if (tok
!= ':' || !gnu_ext
) {
4363 if (vtop
!= vstack
) {
4364 /* needed to avoid having different registers saved in
4366 if (is_float(vtop
->type
.t
)) {
4368 #ifdef TCC_TARGET_X86_64
4369 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4379 if (tok
== ':' && gnu_ext
) {
4387 sv
= *vtop
; /* save value to handle it later */
4388 vtop
--; /* no vpop so that FP stack is not flushed */
4396 bt1
= t1
& VT_BTYPE
;
4398 bt2
= t2
& VT_BTYPE
;
4399 /* cast operands to correct type according to ISOC rules */
4400 if (is_float(bt1
) || is_float(bt2
)) {
4401 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
4402 type
.t
= VT_LDOUBLE
;
4403 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
4408 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
4409 /* cast to biggest op */
4411 /* convert to unsigned if it does not fit in a long long */
4412 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
4413 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
4414 type
.t
|= VT_UNSIGNED
;
4415 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
4416 /* If one is a null ptr constant the result type
4418 if (is_null_pointer (vtop
))
4420 else if (is_null_pointer (&sv
))
4422 /* XXX: test pointer compatibility, C99 has more elaborate
4426 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
4427 /* XXX: test function pointer compatibility */
4428 type
= bt1
== VT_FUNC
? type1
: type2
;
4429 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
4430 /* XXX: test structure compatibility */
4431 type
= bt1
== VT_STRUCT
? type1
: type2
;
4432 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
4433 /* NOTE: as an extension, we accept void on only one side */
4436 /* integer operations */
4438 /* convert to unsigned if it does not fit in an integer */
4439 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
4440 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
4441 type
.t
|= VT_UNSIGNED
;
4444 /* now we convert second operand */
4446 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4449 if (is_float(type
.t
)) {
4451 #ifdef TCC_TARGET_X86_64
4452 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4456 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
4457 /* for long longs, we use fixed registers to avoid having
4458 to handle a complicated move */
4463 /* this is horrible, but we must also convert first
4467 /* put again first value and cast it */
4470 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4473 move_reg(r2
, r1
, type
.t
);
4480 static void expr_eq(void)
4486 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
4487 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
4488 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
4503 ST_FUNC
void gexpr(void)
4514 /* parse an expression and return its type without any side effect. */
4515 static void expr_type(CType
*type
)
4517 int saved_nocode_wanted
;
4519 saved_nocode_wanted
= nocode_wanted
;
4524 nocode_wanted
= saved_nocode_wanted
;
4527 /* parse a unary expression and return its type without any side
4529 static void unary_type(CType
*type
)
4541 /* parse a constant expression and return value in vtop. */
4542 static void expr_const1(void)
4551 /* parse an integer constant and return its value. */
4552 ST_FUNC
int expr_const(void)
4556 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
4557 expect("constant expression");
4563 /* return the label token if current token is a label, otherwise
4565 static int is_label(void)
4569 /* fast test first */
4570 if (tok
< TOK_UIDENT
)
4572 /* no need to save tokc because tok is an identifier */
4579 unget_tok(last_tok
);
4584 static void label_or_decl(int l
)
4588 /* fast test first */
4589 if (tok
>= TOK_UIDENT
)
4591 /* no need to save tokc because tok is an identifier */
4595 unget_tok(last_tok
);
4598 unget_tok(last_tok
);
4603 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
4604 int case_reg
, int is_expr
)
4607 Sym
*s
, *frame_bottom
;
4609 /* generate line number info */
4610 if (tcc_state
->do_debug
&&
4611 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
4612 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
4614 last_line_num
= file
->line_num
;
4618 /* default return value is (void) */
4620 vtop
->type
.t
= VT_VOID
;
4623 if (tok
== TOK_IF
) {
4630 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4632 if (c
== TOK_ELSE
) {
4636 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4637 gsym(d
); /* patch else jmp */
4640 } else if (tok
== TOK_WHILE
) {
4648 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4652 } else if (tok
== '{') {
4654 int block_vla_sp_loc
, *saved_vla_sp_loc
, saved_vla_flags
;
4657 /* record local declaration stack position */
4659 frame_bottom
= sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4660 frame_bottom
->next
= scope_stack_bottom
;
4661 scope_stack_bottom
= frame_bottom
;
4662 llabel
= local_label_stack
;
4664 /* save VLA state */
4665 block_vla_sp_loc
= *(saved_vla_sp_loc
= vla_sp_loc
);
4666 if (saved_vla_sp_loc
!= &vla_sp_root_loc
)
4667 vla_sp_loc
= &block_vla_sp_loc
;
4669 saved_vla_flags
= vla_flags
;
4670 vla_flags
|= VLA_NEED_NEW_FRAME
;
4672 /* handle local labels declarations */
4673 if (tok
== TOK_LABEL
) {
4676 if (tok
< TOK_UIDENT
)
4677 expect("label identifier");
4678 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
4688 while (tok
!= '}') {
4689 label_or_decl(VT_LOCAL
);
4693 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4696 /* pop locally defined labels */
4697 label_pop(&local_label_stack
, llabel
);
4699 /* XXX: this solution makes only valgrind happy...
4700 triggered by gcc.c-torture/execute/20000917-1.c */
4702 switch(vtop
->type
.t
& VT_BTYPE
) {
4707 for(p
=vtop
->type
.ref
;p
;p
=p
->prev
)
4709 tcc_error("unsupported expression type");
4712 /* pop locally defined symbols */
4713 scope_stack_bottom
= scope_stack_bottom
->next
;
4714 sym_pop(&local_stack
, s
);
4716 /* Pop VLA frames and restore stack pointer if required */
4717 if (saved_vla_sp_loc
!= &vla_sp_root_loc
)
4718 *saved_vla_sp_loc
= block_vla_sp_loc
;
4719 if (vla_sp_loc
!= (saved_vla_sp_loc
== &vla_sp_root_loc
? &vla_sp_root_loc
: &block_vla_sp_loc
)) {
4720 vla_sp_loc
= saved_vla_sp_loc
;
4721 gen_vla_sp_restore(*vla_sp_loc
);
4723 vla_flags
= (vla_flags
& ~VLA_SCOPE_FLAGS
) | (saved_vla_flags
& VLA_SCOPE_FLAGS
);
4726 } else if (tok
== TOK_RETURN
) {
4730 gen_assign_cast(&func_vt
);
4731 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
4732 CType type
, ret_type
;
4733 int ret_align
, ret_nregs
;
4734 ret_nregs
= gfunc_sret(&func_vt
, func_var
, &ret_type
,
4736 if (0 == ret_nregs
) {
4737 /* if returning structure, must copy it to implicit
4738 first pointer arg location */
4741 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
4744 /* copy structure value to pointer */
4747 /* returning structure packed into registers */
4748 int rc
, size
, addr
, align
;
4749 size
= type_size(&func_vt
,&align
);
4750 if ((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) || (vtop
->c
.i
& (ret_align
-1)))
4751 && (align
& (ret_align
-1))) {
4752 loc
= (loc
- size
) & -align
;
4755 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
4758 vset(&ret_type
, VT_LOCAL
| VT_LVAL
, addr
);
4760 vtop
->type
= ret_type
;
4761 if (is_float(ret_type
.t
))
4762 rc
= rc_fret(ret_type
.t
);
4770 if (--ret_nregs
== 0)
4772 /* We assume that when a structure is returned in multiple
4773 registers, their classes are consecutive values of the
4776 /* XXX: compatible with arm only: ret_align == register_size */
4777 vtop
->c
.i
+= ret_align
;
4778 vtop
->r
= VT_LOCAL
| VT_LVAL
;
4781 } else if (is_float(func_vt
.t
)) {
4782 gv(rc_fret(func_vt
.t
));
4786 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
4789 rsym
= gjmp(rsym
); /* jmp */
4790 } else if (tok
== TOK_BREAK
) {
4793 tcc_error("cannot break");
4794 *bsym
= gjmp(*bsym
);
4797 } else if (tok
== TOK_CONTINUE
) {
4800 tcc_error("cannot continue");
4801 *csym
= gjmp(*csym
);
4804 } else if (tok
== TOK_FOR
) {
4809 frame_bottom
= sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4810 frame_bottom
->next
= scope_stack_bottom
;
4811 scope_stack_bottom
= frame_bottom
;
4813 /* c99 for-loop init decl? */
4814 if (!decl0(VT_LOCAL
, 1)) {
4815 /* no, regular for-loop init expr */
4839 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4843 scope_stack_bottom
= scope_stack_bottom
->next
;
4844 sym_pop(&local_stack
, s
);
4846 if (tok
== TOK_DO
) {
4851 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4862 if (tok
== TOK_SWITCH
) {
4866 /* XXX: other types than integer */
4867 case_reg
= gv(RC_INT
);
4871 b
= gjmp(0); /* jump to first case */
4873 block(&a
, csym
, &b
, &c
, case_reg
, 0);
4874 /* if no default, jmp after switch */
4882 if (tok
== TOK_CASE
) {
4889 if (gnu_ext
&& tok
== TOK_DOTS
) {
4893 tcc_warning("empty case range");
4895 /* since a case is like a label, we must skip it with a jmp */
4902 *case_sym
= gtst(1, 0);
4905 *case_sym
= gtst(1, 0);
4909 *case_sym
= gtst(1, *case_sym
);
4914 goto block_after_label
;
4916 if (tok
== TOK_DEFAULT
) {
4922 tcc_error("too many 'default'");
4925 goto block_after_label
;
4927 if (tok
== TOK_GOTO
) {
4929 if (tok
== '*' && gnu_ext
) {
4933 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
4936 } else if (tok
>= TOK_UIDENT
) {
4937 s
= label_find(tok
);
4938 /* put forward definition if needed */
4940 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4942 if (s
->r
== LABEL_DECLARED
)
4943 s
->r
= LABEL_FORWARD
;
4945 /* label already defined */
4946 if (vla_flags
& VLA_IN_SCOPE
) {
4947 /* If VLAs are in use, save the current stack pointer and
4948 reset the stack pointer to what it was at function entry
4949 (label will restore stack pointer in inner scopes) */
4951 gen_vla_sp_restore(vla_sp_root_loc
);
4953 if (s
->r
& LABEL_FORWARD
)
4954 s
->jnext
= gjmp(s
->jnext
);
4956 gjmp_addr(s
->jnext
);
4959 expect("label identifier");
4962 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
4968 if (vla_flags
& VLA_IN_SCOPE
) {
4969 /* save/restore stack pointer across label
4970 this is a no-op when combined with the load immediately
4971 after the label unless we arrive via goto */
4976 if (s
->r
== LABEL_DEFINED
)
4977 tcc_error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
4979 s
->r
= LABEL_DEFINED
;
4981 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
4984 if (vla_flags
& VLA_IN_SCOPE
) {
4985 gen_vla_sp_restore(*vla_sp_loc
);
4986 vla_flags
|= VLA_NEED_NEW_FRAME
;
4988 /* we accept this, but it is a mistake */
4991 tcc_warning("deprecated use of label at end of compound statement");
4995 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4998 /* expression case */
5013 /* t is the array or struct type. c is the array or struct
5014 address. cur_index/cur_field is the pointer to the current
5015 value. 'size_only' is true if only size info is needed (only used
5017 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
5018 int *cur_index
, Sym
**cur_field
,
5022 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
5028 if (gnu_ext
&& (l
= is_label()) != 0)
5030 while (tok
== '[' || tok
== '.') {
5032 if (!(type
->t
& VT_ARRAY
))
5033 expect("array type");
5036 index
= expr_const();
5037 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
5038 expect("invalid index");
5039 if (tok
== TOK_DOTS
&& gnu_ext
) {
5041 index_last
= expr_const();
5042 if (index_last
< 0 ||
5043 (s
->c
>= 0 && index_last
>= s
->c
) ||
5045 expect("invalid index");
5051 *cur_index
= index_last
;
5052 type
= pointed_type(type
);
5053 elem_size
= type_size(type
, &align
);
5054 c
+= index
* elem_size
;
5055 /* NOTE: we only support ranges for last designator */
5056 nb_elems
= index_last
- index
+ 1;
5057 if (nb_elems
!= 1) {
5066 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
5067 expect("struct/union type");
5080 /* XXX: fix this mess by using explicit storage field */
5082 type1
.t
|= (type
->t
& ~VT_TYPE
);
5096 if (type
->t
& VT_ARRAY
) {
5098 type
= pointed_type(type
);
5099 c
+= index
* type_size(type
, &align
);
5103 tcc_error("too many field init");
5104 /* XXX: fix this mess by using explicit storage field */
5106 type1
.t
|= (type
->t
& ~VT_TYPE
);
5111 decl_initializer(type
, sec
, c
, 0, size_only
);
5113 /* XXX: make it more general */
5114 if (!size_only
&& nb_elems
> 1) {
5115 unsigned long c_end
;
5120 tcc_error("range init not supported yet for dynamic storage");
5121 c_end
= c
+ nb_elems
* elem_size
;
5122 if (c_end
> sec
->data_allocated
)
5123 section_realloc(sec
, c_end
);
5124 src
= sec
->data
+ c
;
5126 for(i
= 1; i
< nb_elems
; i
++) {
5128 memcpy(dst
, src
, elem_size
);
5134 #define EXPR_CONST 1
5137 /* store a value or an expression directly in global data or in local array */
5138 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
5139 int v
, int expr_type
)
5141 int saved_global_expr
, bt
, bit_pos
, bit_size
;
5143 unsigned long long bit_mask
;
5151 /* compound literals must be allocated globally in this case */
5152 saved_global_expr
= global_expr
;
5155 global_expr
= saved_global_expr
;
5156 /* NOTE: symbols are accepted */
5157 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
5158 tcc_error("initializer element is not constant");
5166 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
5169 /* XXX: not portable */
5170 /* XXX: generate error if incorrect relocation */
5171 gen_assign_cast(&dtype
);
5172 bt
= type
->t
& VT_BTYPE
;
5173 /* we'll write at most 12 bytes */
5174 if (c
+ 12 > sec
->data_allocated
) {
5175 section_realloc(sec
, c
+ 12);
5177 ptr
= sec
->data
+ c
;
5178 /* XXX: make code faster ? */
5179 if (!(type
->t
& VT_BITFIELD
)) {
5184 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5185 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
5186 bit_mask
= (1LL << bit_size
) - 1;
5188 if ((vtop
->r
& VT_SYM
) &&
5194 (bt
== VT_INT
&& bit_size
!= 32)))
5195 tcc_error("initializer element is not computable at load time");
5198 vtop
->c
.i
= (vtop
->c
.i
!= 0);
5200 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5203 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5206 *(double *)ptr
= vtop
->c
.d
;
5209 *(long double *)ptr
= vtop
->c
.ld
;
5212 *(long long *)ptr
|= (vtop
->c
.ll
& bit_mask
) << bit_pos
;
5215 if (vtop
->r
& VT_SYM
) {
5216 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
5218 *(addr_t
*)ptr
|= (vtop
->c
.ptr_offset
& bit_mask
) << bit_pos
;
5221 if (vtop
->r
& VT_SYM
) {
5222 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
5224 *(int *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5229 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
5236 /* put zeros for variable based init */
5237 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
5240 /* nothing to do because globals are already set to zero */
5242 #ifdef TCC_TARGET_ARM
5243 vpush_global_sym(&func_old_type
, TOK_memset
);
5250 gen_putz(vtop
, size
);
5256 /* 't' contains the type and storage info. 'c' is the offset of the
5257 object in section 'sec'. If 'sec' is NULL, it means stack based
5258 allocation. 'first' is true if array '{' must be read (multi
5259 dimension implicit array init handling). 'size_only' is true if
5260 size only evaluation is wanted (only for arrays). */
5261 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
5262 int first
, int size_only
)
5264 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, parlevel1
, i
;
5265 int size1
, align1
, expr_type
;
5269 if (type
->t
& VT_VLA
) {
5272 /* save current stack pointer */
5273 if (vla_flags
& VLA_NEED_NEW_FRAME
) {
5275 vla_flags
= VLA_IN_SCOPE
;
5276 vla_sp_loc
= &vla_sp_loc_tmp
;
5279 vla_runtime_type_size(type
, &a
);
5280 gen_vla_alloc(type
, a
);
5281 vset(type
, VT_LOCAL
|VT_LVAL
, c
);
5285 } else if (type
->t
& VT_ARRAY
) {
5289 t1
= pointed_type(type
);
5290 size1
= type_size(t1
, &align1
);
5293 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
5296 tcc_error("character array initializer must be a literal,"
5297 " optionally enclosed in braces");
5302 /* only parse strings here if correct type (otherwise: handle
5303 them as ((w)char *) expressions */
5304 if ((tok
== TOK_LSTR
&&
5305 #ifdef TCC_TARGET_PE
5306 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
5308 (t1
->t
& VT_BTYPE
) == VT_INT
5310 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
5311 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5316 /* compute maximum number of chars wanted */
5318 cstr_len
= cstr
->size
;
5320 cstr_len
= cstr
->size
/ sizeof(nwchar_t
);
5323 if (n
>= 0 && nb
> (n
- array_length
))
5324 nb
= n
- array_length
;
5327 tcc_warning("initializer-string for array is too long");
5328 /* in order to go faster for common case (char
5329 string in global variable, we handle it
5331 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
5332 memcpy(sec
->data
+ c
+ array_length
, cstr
->data
, nb
);
5336 ch
= ((unsigned char *)cstr
->data
)[i
];
5338 ch
= ((nwchar_t
*)cstr
->data
)[i
];
5339 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
5347 /* only add trailing zero if enough storage (no
5348 warning in this case since it is standard) */
5349 if (n
< 0 || array_length
< n
) {
5351 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
5357 while (tok
!= '}') {
5358 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
5359 if (n
>= 0 && index
>= n
)
5360 tcc_error("index too large");
5361 /* must put zero in holes (note that doing it that way
5362 ensures that it even works with designators) */
5363 if (!size_only
&& array_length
< index
) {
5364 init_putz(t1
, sec
, c
+ array_length
* size1
,
5365 (index
- array_length
) * size1
);
5368 if (index
> array_length
)
5369 array_length
= index
;
5370 /* special test for multi dimensional arrays (may not
5371 be strictly correct if designators are used at the
5373 if (index
>= n
&& no_oblock
)
5382 /* put zeros at the end */
5383 if (!size_only
&& n
>= 0 && array_length
< n
) {
5384 init_putz(t1
, sec
, c
+ array_length
* size1
,
5385 (n
- array_length
) * size1
);
5387 /* patch type size if needed */
5389 s
->c
= array_length
;
5390 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
5391 (sec
|| !first
|| tok
== '{')) {
5394 /* NOTE: the previous test is a specific case for automatic
5395 struct/union init */
5396 /* XXX: union needs only one init */
5398 /* XXX: this test is incorrect for local initializers
5399 beginning with ( without {. It would be much more difficult
5400 to do it correctly (ideally, the expression parser should
5401 be used in all cases) */
5407 while (tok
== '(') {
5411 if (!parse_btype(&type1
, &ad1
))
5413 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
5415 if (!is_assignable_types(type
, &type1
))
5416 tcc_error("invalid type for cast");
5421 if (first
|| tok
== '{') {
5430 while (tok
!= '}') {
5431 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
5433 if (!size_only
&& array_length
< index
) {
5434 init_putz(type
, sec
, c
+ array_length
,
5435 index
- array_length
);
5437 index
= index
+ type_size(&f
->type
, &align1
);
5438 if (index
> array_length
)
5439 array_length
= index
;
5441 /* gr: skip fields from same union - ugly. */
5443 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
5444 /* test for same offset */
5445 if (f
->next
->c
!= f
->c
)
5447 /* if yes, test for bitfield shift */
5448 if ((f
->type
.t
& VT_BITFIELD
) && (f
->next
->type
.t
& VT_BITFIELD
)) {
5449 int bit_pos_1
= (f
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5450 int bit_pos_2
= (f
->next
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5451 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
5452 if (bit_pos_1
!= bit_pos_2
)
5459 if (no_oblock
&& f
== NULL
)
5465 /* put zeros at the end */
5466 if (!size_only
&& array_length
< n
) {
5467 init_putz(type
, sec
, c
+ array_length
,
5476 } else if (tok
== '{') {
5478 decl_initializer(type
, sec
, c
, first
, size_only
);
5480 } else if (size_only
) {
5481 /* just skip expression */
5482 parlevel
= parlevel1
= 0;
5483 while ((parlevel
> 0 || parlevel1
> 0 ||
5484 (tok
!= '}' && tok
!= ',')) && tok
!= -1) {
5487 else if (tok
== ')')
5489 else if (tok
== '{')
5491 else if (tok
== '}')
5496 /* currently, we always use constant expression for globals
5497 (may change for scripting case) */
5498 expr_type
= EXPR_CONST
;
5500 expr_type
= EXPR_ANY
;
5501 init_putv(type
, sec
, c
, 0, expr_type
);
5505 /* parse an initializer for type 't' if 'has_init' is non zero, and
5506 allocate space in local or global data space ('r' is either
5507 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5508 variable 'v' with an associated name represented by 'asm_label' of
5509 scope 'scope' is declared before initializers are parsed. If 'v' is
5510 zero, then a reference to the new object is put in the value stack.
5511 If 'has_init' is 2, a special parsing is done to handle string
5513 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
5514 int has_init
, int v
, char *asm_label
,
5517 int size
, align
, addr
, data_offset
;
5519 ParseState saved_parse_state
= {0};
5520 TokenString init_str
;
5522 Sym
*flexible_array
;
5524 flexible_array
= NULL
;
5525 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
5526 Sym
*field
= type
->ref
->next
;
5529 field
= field
->next
;
5530 if (field
->type
.t
& VT_ARRAY
&& field
->type
.ref
->c
< 0)
5531 flexible_array
= field
;
5535 size
= type_size(type
, &align
);
5536 /* If unknown size, we must evaluate it before
5537 evaluating initializers because
5538 initializers can generate global data too
5539 (e.g. string pointers or ISOC99 compound
5540 literals). It also simplifies local
5541 initializers handling */
5542 tok_str_new(&init_str
);
5543 if (size
< 0 || (flexible_array
&& has_init
)) {
5545 tcc_error("unknown type size");
5546 /* get all init string */
5547 if (has_init
== 2) {
5548 /* only get strings */
5549 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5550 tok_str_add_tok(&init_str
);
5555 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
5557 tcc_error("unexpected end of file in initializer");
5558 tok_str_add_tok(&init_str
);
5561 else if (tok
== '}') {
5571 tok_str_add(&init_str
, -1);
5572 tok_str_add(&init_str
, 0);
5575 save_parse_state(&saved_parse_state
);
5577 macro_ptr
= init_str
.str
;
5579 decl_initializer(type
, NULL
, 0, 1, 1);
5580 /* prepare second initializer parsing */
5581 macro_ptr
= init_str
.str
;
5584 /* if still unknown size, error */
5585 size
= type_size(type
, &align
);
5587 tcc_error("unknown type size");
5590 size
+= flexible_array
->type
.ref
->c
* pointed_size(&flexible_array
->type
);
5591 /* take into account specified alignment if bigger */
5592 if (ad
->a
.aligned
) {
5593 if (ad
->a
.aligned
> align
)
5594 align
= ad
->a
.aligned
;
5595 } else if (ad
->a
.packed
) {
5598 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
5600 #ifdef CONFIG_TCC_BCHECK
5601 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5605 loc
= (loc
- size
) & -align
;
5607 #ifdef CONFIG_TCC_BCHECK
5608 /* handles bounds */
5609 /* XXX: currently, since we do only one pass, we cannot track
5610 '&' operators, so we add only arrays */
5611 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5612 unsigned long *bounds_ptr
;
5613 /* add padding between regions */
5615 /* then add local bound info */
5616 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(unsigned long));
5617 bounds_ptr
[0] = addr
;
5618 bounds_ptr
[1] = size
;
5622 /* local variable */
5623 sym_push(v
, type
, r
, addr
);
5625 /* push local reference */
5626 vset(type
, r
, addr
);
5632 if (v
&& scope
== VT_CONST
) {
5633 /* see if the symbol was already defined */
5636 if (!is_compatible_types(&sym
->type
, type
))
5637 tcc_error("incompatible types for redefinition of '%s'",
5638 get_tok_str(v
, NULL
));
5639 if (sym
->type
.t
& VT_EXTERN
) {
5640 /* if the variable is extern, it was not allocated */
5641 sym
->type
.t
&= ~VT_EXTERN
;
5642 /* set array size if it was omitted in extern
5644 if ((sym
->type
.t
& VT_ARRAY
) &&
5645 sym
->type
.ref
->c
< 0 &&
5647 sym
->type
.ref
->c
= type
->ref
->c
;
5649 /* we accept several definitions of the same
5650 global variable. this is tricky, because we
5651 must play with the SHN_COMMON type of the symbol */
5652 /* XXX: should check if the variable was already
5653 initialized. It is incorrect to initialized it
5655 /* no init data, we won't add more to the symbol */
5662 /* allocate symbol in corresponding section */
5667 else if (tcc_state
->nocommon
)
5671 data_offset
= sec
->data_offset
;
5672 data_offset
= (data_offset
+ align
- 1) & -align
;
5674 /* very important to increment global pointer at this time
5675 because initializers themselves can create new initializers */
5676 data_offset
+= size
;
5677 #ifdef CONFIG_TCC_BCHECK
5678 /* add padding if bound check */
5679 if (tcc_state
->do_bounds_check
)
5682 sec
->data_offset
= data_offset
;
5683 /* allocate section space to put the data */
5684 if (sec
->sh_type
!= SHT_NOBITS
&&
5685 data_offset
> sec
->data_allocated
)
5686 section_realloc(sec
, data_offset
);
5687 /* align section if needed */
5688 if (align
> sec
->sh_addralign
)
5689 sec
->sh_addralign
= align
;
5691 addr
= 0; /* avoid warning */
5695 if (scope
!= VT_CONST
|| !sym
) {
5696 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
5697 sym
->asm_label
= asm_label
;
5699 /* update symbol definition */
5701 put_extern_sym(sym
, sec
, addr
, size
);
5704 /* put a common area */
5705 put_extern_sym(sym
, NULL
, align
, size
);
5706 /* XXX: find a nicer way */
5707 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
5708 esym
->st_shndx
= SHN_COMMON
;
5711 /* push global reference */
5712 sym
= get_sym_ref(type
, sec
, addr
, size
);
5713 vpushsym(type
, sym
);
5715 /* patch symbol weakness */
5716 if (type
->t
& VT_WEAK
)
5718 apply_visibility(sym
, type
);
5719 #ifdef CONFIG_TCC_BCHECK
5720 /* handles bounds now because the symbol must be defined
5721 before for the relocation */
5722 if (tcc_state
->do_bounds_check
) {
5723 unsigned long *bounds_ptr
;
5725 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
5726 /* then add global bound info */
5727 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(long));
5728 bounds_ptr
[0] = 0; /* relocated */
5729 bounds_ptr
[1] = size
;
5733 if (has_init
|| (type
->t
& VT_VLA
)) {
5734 decl_initializer(type
, sec
, addr
, 1, 0);
5735 /* restore parse state if needed */
5737 tok_str_free(init_str
.str
);
5738 restore_parse_state(&saved_parse_state
);
5740 /* patch flexible array member size back to -1, */
5741 /* for possible subsequent similar declarations */
5743 flexible_array
->type
.ref
->c
= -1;
5748 static void put_func_debug(Sym
*sym
)
5753 /* XXX: we put here a dummy type */
5754 snprintf(buf
, sizeof(buf
), "%s:%c1",
5755 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
5756 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
5757 cur_text_section
, sym
->c
);
5758 /* //gr gdb wants a line at the function */
5759 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
5764 /* parse an old style function declaration list */
5765 /* XXX: check multiple parameter */
5766 static void func_decl_list(Sym
*func_sym
)
5773 /* parse each declaration */
5774 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
&&
5775 tok
!= TOK_ASM1
&& tok
!= TOK_ASM2
&& tok
!= TOK_ASM3
) {
5776 if (!parse_btype(&btype
, &ad
))
5777 expect("declaration list");
5778 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5779 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5781 /* we accept no variable after */
5785 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5786 /* find parameter in function parameter list */
5789 if ((s
->v
& ~SYM_FIELD
) == v
)
5793 tcc_error("declaration for parameter '%s' but no such parameter",
5794 get_tok_str(v
, NULL
));
5796 /* check that no storage specifier except 'register' was given */
5797 if (type
.t
& VT_STORAGE
)
5798 tcc_error("storage class specified for '%s'", get_tok_str(v
, NULL
));
5799 convert_parameter_type(&type
);
5800 /* we can add the type (NOTE: it could be local to the function) */
5802 /* accept other parameters */
5813 /* parse a function defined by symbol 'sym' and generate its code in
5814 'cur_text_section' */
5815 static void gen_function(Sym
*sym
)
5817 int saved_nocode_wanted
= nocode_wanted
;
5819 ind
= cur_text_section
->data_offset
;
5820 /* NOTE: we patch the symbol size later */
5821 put_extern_sym(sym
, cur_text_section
, ind
, 0);
5822 funcname
= get_tok_str(sym
->v
, NULL
);
5824 /* Initialize VLA state */
5825 vla_sp_loc
= &vla_sp_root_loc
;
5826 vla_flags
= VLA_NEED_NEW_FRAME
;
5827 /* put debug symbol */
5828 if (tcc_state
->do_debug
)
5829 put_func_debug(sym
);
5830 /* push a dummy symbol to enable local sym storage */
5831 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
5832 gfunc_prolog(&sym
->type
);
5833 #ifdef CONFIG_TCC_BCHECK
5834 if (tcc_state
->do_bounds_check
5835 && !strcmp(get_tok_str(sym
->v
, NULL
), "main")) {
5839 for (i
= 0, sym
= local_stack
; i
< 2; i
++, sym
= sym
->prev
) {
5840 if (sym
->v
& SYM_FIELD
|| sym
->prev
->v
& SYM_FIELD
)
5842 vpush_global_sym(&func_old_type
, TOK___bound_main_arg
);
5843 vset(&sym
->type
, sym
->r
, sym
->c
);
5849 block(NULL
, NULL
, NULL
, NULL
, 0, 0);
5852 cur_text_section
->data_offset
= ind
;
5853 label_pop(&global_label_stack
, NULL
);
5854 /* reset local stack */
5855 scope_stack_bottom
= NULL
;
5856 sym_pop(&local_stack
, NULL
);
5857 /* end of function */
5858 /* patch symbol size */
5859 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
5861 /* patch symbol weakness (this definition overrules any prototype) */
5862 if (sym
->type
.t
& VT_WEAK
)
5864 apply_visibility(sym
, &sym
->type
);
5865 if (tcc_state
->do_debug
) {
5866 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
5868 /* It's better to crash than to generate wrong code */
5869 cur_text_section
= NULL
;
5870 funcname
= ""; /* for safety */
5871 func_vt
.t
= VT_VOID
; /* for safety */
5872 func_var
= 0; /* for safety */
5873 ind
= 0; /* for safety */
5874 nocode_wanted
= saved_nocode_wanted
;
5877 ST_FUNC
void gen_inline_functions(void)
5880 int *str
, inline_generated
, i
;
5881 struct InlineFunc
*fn
;
5883 /* iterate while inline function are referenced */
5885 inline_generated
= 0;
5886 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5887 fn
= tcc_state
->inline_fns
[i
];
5889 if (sym
&& sym
->c
) {
5890 /* the function was used: generate its code and
5891 convert it to a normal function */
5892 str
= fn
->token_str
;
5895 pstrcpy(file
->filename
, sizeof file
->filename
, fn
->filename
);
5896 sym
->r
= VT_SYM
| VT_CONST
;
5897 sym
->type
.t
&= ~VT_INLINE
;
5901 cur_text_section
= text_section
;
5903 macro_ptr
= NULL
; /* fail safe */
5905 inline_generated
= 1;
5908 if (!inline_generated
)
5911 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5912 fn
= tcc_state
->inline_fns
[i
];
5913 str
= fn
->token_str
;
5916 dynarray_reset(&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
);
5919 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
5920 static int decl0(int l
, int is_for_loop_init
)
5928 if (!parse_btype(&btype
, &ad
)) {
5929 if (is_for_loop_init
)
5931 /* skip redundant ';' */
5932 /* XXX: find more elegant solution */
5937 if (l
== VT_CONST
&&
5938 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5939 /* global asm block */
5943 /* special test for old K&R protos without explicit int
5944 type. Only accepted when defining global data */
5945 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
5949 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5950 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5952 /* we accept no variable after */
5956 while (1) { /* iterate thru each declaration */
5957 char *asm_label
; // associated asm label
5959 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5963 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
5964 printf("type = '%s'\n", buf
);
5967 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5968 if ((type
.t
& VT_STATIC
) && (l
== VT_LOCAL
)) {
5969 tcc_error("function without file scope cannot be static");
5971 /* if old style function prototype, we accept a
5974 if (sym
->c
== FUNC_OLD
)
5975 func_decl_list(sym
);
5979 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5982 asm_label_instr(&astr
);
5983 asm_label
= tcc_strdup(astr
.data
);
5986 /* parse one last attribute list, after asm label */
5987 parse_attribute(&ad
);
5992 #ifdef TCC_TARGET_PE
5993 if (ad
.a
.func_import
)
5994 type
.t
|= VT_IMPORT
;
5995 if (ad
.a
.func_export
)
5996 type
.t
|= VT_EXPORT
;
5998 type
.t
|= ad
.a
.visibility
<< VT_VIS_SHIFT
;
6002 tcc_error("cannot use local functions");
6003 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
6004 expect("function definition");
6006 /* reject abstract declarators in function definition */
6008 while ((sym
= sym
->next
) != NULL
)
6009 if (!(sym
->v
& ~SYM_FIELD
))
6010 expect("identifier");
6012 /* XXX: cannot do better now: convert extern line to static inline */
6013 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
6014 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
6019 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
6022 ref
= sym
->type
.ref
;
6023 if (0 == ref
->a
.func_proto
)
6024 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
6026 /* use func_call from prototype if not defined */
6027 if (ref
->a
.func_call
!= FUNC_CDECL
6028 && type
.ref
->a
.func_call
== FUNC_CDECL
)
6029 type
.ref
->a
.func_call
= ref
->a
.func_call
;
6031 /* use export from prototype */
6032 if (ref
->a
.func_export
)
6033 type
.ref
->a
.func_export
= 1;
6035 /* use static from prototype */
6036 if (sym
->type
.t
& VT_STATIC
)
6037 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
6039 /* If the definition has no visibility use the
6040 one from prototype. */
6041 if (! (type
.t
& VT_VIS_MASK
))
6042 type
.t
|= sym
->type
.t
& VT_VIS_MASK
;
6044 if (!is_compatible_types(&sym
->type
, &type
)) {
6046 tcc_error("incompatible types for redefinition of '%s'",
6047 get_tok_str(v
, NULL
));
6049 type
.ref
->a
.func_proto
= 0;
6050 /* if symbol is already defined, then put complete type */
6053 /* put function symbol */
6054 sym
= global_identifier_push(v
, type
.t
, 0);
6055 sym
->type
.ref
= type
.ref
;
6058 /* static inline functions are just recorded as a kind
6059 of macro. Their code will be emitted at the end of
6060 the compilation unit only if they are used */
6061 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
6062 (VT_INLINE
| VT_STATIC
)) {
6063 TokenString func_str
;
6065 struct InlineFunc
*fn
;
6066 const char *filename
;
6068 tok_str_new(&func_str
);
6074 tcc_error("unexpected end of file");
6075 tok_str_add_tok(&func_str
);
6080 } else if (t
== '}') {
6082 if (block_level
== 0)
6086 tok_str_add(&func_str
, -1);
6087 tok_str_add(&func_str
, 0);
6088 filename
= file
? file
->filename
: "";
6089 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
6090 strcpy(fn
->filename
, filename
);
6092 fn
->token_str
= func_str
.str
;
6093 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
6096 /* compute text section */
6097 cur_text_section
= ad
.section
;
6098 if (!cur_text_section
)
6099 cur_text_section
= text_section
;
6100 sym
->r
= VT_SYM
| VT_CONST
;
6105 if (btype
.t
& VT_TYPEDEF
) {
6106 /* save typedefed type */
6107 /* XXX: test storage specifiers ? */
6108 sym
= sym_push(v
, &type
, 0, 0);
6110 sym
->type
.t
|= VT_TYPEDEF
;
6113 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
6114 /* external function definition */
6115 /* specific case for func_call attribute */
6116 ad
.a
.func_proto
= 1;
6118 } else if (!(type
.t
& VT_ARRAY
)) {
6119 /* not lvalue if array */
6120 r
|= lvalue_type(type
.t
);
6122 has_init
= (tok
== '=');
6123 if (has_init
&& (type
.t
& VT_VLA
))
6124 tcc_error("Variable length array cannot be initialized");
6125 if ((btype
.t
& VT_EXTERN
) || ((type
.t
& VT_BTYPE
) == VT_FUNC
) ||
6126 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
6127 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
6128 /* external variable or function */
6129 /* NOTE: as GCC, uninitialized global static
6130 arrays of null size are considered as
6132 sym
= external_sym(v
, &type
, r
, asm_label
);
6134 if (ad
.alias_target
) {
6139 alias_target
= sym_find(ad
.alias_target
);
6140 if (!alias_target
|| !alias_target
->c
)
6141 tcc_error("unsupported forward __alias__ attribute");
6142 esym
= &((Elf32_Sym
*)symtab_section
->data
)[alias_target
->c
];
6143 tsec
.sh_num
= esym
->st_shndx
;
6144 put_extern_sym2(sym
, &tsec
, esym
->st_value
, esym
->st_size
, 0);
6147 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
6148 if (type
.t
& VT_STATIC
)
6154 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, asm_label
, l
);
6158 if (is_for_loop_init
)
6171 ST_FUNC
void decl(int l
)