2 * TCC - Tiny C Compiler
4 * Copyright (c) 2001-2004 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 /********************************************************/
24 /* global variables */
26 /* loc : local variable index
27 ind : output code index
29 anon_sym: anonymous symbol index
31 ST_DATA
int rsym
, anon_sym
, ind
, loc
;
33 ST_DATA Section
*text_section
, *data_section
, *bss_section
; /* predefined sections */
34 ST_DATA Section
*cur_text_section
; /* current section where function code is generated */
36 ST_DATA Section
*last_text_section
; /* to handle .previous asm directive */
38 #ifdef CONFIG_TCC_BCHECK
39 /* bound check related sections */
40 ST_DATA Section
*bounds_section
; /* contains global data bound description */
41 ST_DATA Section
*lbounds_section
; /* contains local data bound description */
44 ST_DATA Section
*symtab_section
, *strtab_section
;
46 ST_DATA Section
*stab_section
, *stabstr_section
;
47 ST_DATA Sym
*sym_free_first
;
48 ST_DATA
void **sym_pools
;
49 ST_DATA
int nb_sym_pools
;
51 ST_DATA Sym
*global_stack
;
52 ST_DATA Sym
*local_stack
;
53 ST_DATA Sym
*scope_stack_bottom
;
54 ST_DATA Sym
*define_stack
;
55 ST_DATA Sym
*global_label_stack
;
56 ST_DATA Sym
*local_label_stack
;
58 ST_DATA
int vla_sp_loc_tmp
; /* vla_sp_loc is set to this when the value won't be needed later */
59 ST_DATA
int vla_sp_root_loc
; /* vla_sp_loc for SP before any VLAs were pushed */
60 ST_DATA
int *vla_sp_loc
; /* Pointer to variable holding location to store stack pointer on the stack when modifying stack pointer */
61 ST_DATA
int vla_flags
; /* VLA_* flags */
63 ST_DATA SValue __vstack
[1+VSTACK_SIZE
], *vtop
;
65 ST_DATA
int const_wanted
; /* true if constant wanted */
66 ST_DATA
int nocode_wanted
; /* true if no code generation wanted for an expression */
67 ST_DATA
int global_expr
; /* true if compound literals must be allocated globally (used during initializers parsing */
68 ST_DATA CType func_vt
; /* current function return type (used by return instruction) */
69 ST_DATA
int func_var
; /* true if current function is variadic (used by return instruction) */
71 ST_DATA
int last_line_num
, last_ind
, func_ind
; /* debug last line number and pc */
72 ST_DATA
char *funcname
;
74 ST_DATA CType char_pointer_type
, func_old_type
, int_type
, size_type
;
76 /* ------------------------------------------------------------------------- */
77 static void gen_cast(CType
*type
);
78 static inline CType
*pointed_type(CType
*type
);
79 static int is_compatible_types(CType
*type1
, CType
*type2
);
80 static int parse_btype(CType
*type
, AttributeDef
*ad
);
81 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
);
82 static void parse_expr_type(CType
*type
);
83 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
, int first
, int size_only
);
84 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
, int case_reg
, int is_expr
);
85 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
, int has_init
, int v
, char *asm_label
, int scope
);
86 static int decl0(int l
, int is_for_loop_init
);
87 static void expr_eq(void);
88 static void unary_type(CType
*type
);
89 static void vla_runtime_type_size(CType
*type
, int *a
);
90 static void vla_sp_save(void);
91 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
);
92 static void expr_type(CType
*type
);
94 ST_INLN
int is_float(int t
)
98 return bt
== VT_LDOUBLE
|| bt
== VT_DOUBLE
|| bt
== VT_FLOAT
|| bt
== VT_QFLOAT
;
101 /* we use our own 'finite' function to avoid potential problems with
102 non standard math libs */
103 /* XXX: endianness dependent */
104 ST_FUNC
int ieee_finite(double d
)
107 memcpy(p
, &d
, sizeof(double));
108 return ((unsigned)((p
[1] | 0x800fffff) + 1)) >> 31;
111 ST_FUNC
void test_lvalue(void)
113 if (!(vtop
->r
& VT_LVAL
))
117 /* ------------------------------------------------------------------------- */
118 /* symbol allocator */
119 static Sym
*__sym_malloc(void)
121 Sym
*sym_pool
, *sym
, *last_sym
;
124 sym_pool
= tcc_malloc(SYM_POOL_NB
* sizeof(Sym
));
125 dynarray_add(&sym_pools
, &nb_sym_pools
, sym_pool
);
127 last_sym
= sym_free_first
;
129 for(i
= 0; i
< SYM_POOL_NB
; i
++) {
130 sym
->next
= last_sym
;
134 sym_free_first
= last_sym
;
138 static inline Sym
*sym_malloc(void)
141 sym
= sym_free_first
;
143 sym
= __sym_malloc();
144 sym_free_first
= sym
->next
;
148 ST_INLN
void sym_free(Sym
*sym
)
150 sym
->next
= sym_free_first
;
151 tcc_free(sym
->asm_label
);
152 sym_free_first
= sym
;
155 /* push, without hashing */
156 ST_FUNC Sym
*sym_push2(Sym
**ps
, int v
, int t
, long c
)
159 if (ps
== &local_stack
) {
160 for (s
= *ps
; s
&& s
!= scope_stack_bottom
; s
= s
->prev
)
161 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
&& s
->v
== v
)
162 tcc_error("incompatible types for redefinition of '%s'",
163 get_tok_str(v
, NULL
));
182 /* find a symbol and return its associated structure. 's' is the top
183 of the symbol stack */
184 ST_FUNC Sym
*sym_find2(Sym
*s
, int v
)
194 /* structure lookup */
195 ST_INLN Sym
*struct_find(int v
)
198 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
200 return table_ident
[v
]->sym_struct
;
203 /* find an identifier */
204 ST_INLN Sym
*sym_find(int v
)
207 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
209 return table_ident
[v
]->sym_identifier
;
212 /* push a given symbol on the symbol stack */
213 ST_FUNC Sym
*sym_push(int v
, CType
*type
, int r
, int c
)
222 s
= sym_push2(ps
, v
, type
->t
, c
);
223 s
->type
.ref
= type
->ref
;
225 /* don't record fields or anonymous symbols */
227 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
228 /* record symbol in token array */
229 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
231 ps
= &ts
->sym_struct
;
233 ps
= &ts
->sym_identifier
;
240 /* push a global identifier */
241 ST_FUNC Sym
*global_identifier_push(int v
, int t
, int c
)
244 s
= sym_push2(&global_stack
, v
, t
, c
);
245 /* don't record anonymous symbol */
246 if (v
< SYM_FIRST_ANOM
) {
247 ps
= &table_ident
[v
- TOK_IDENT
]->sym_identifier
;
248 /* modify the top most local identifier, so that
249 sym_identifier will point to 's' when popped */
251 ps
= &(*ps
)->prev_tok
;
258 /* pop symbols until top reaches 'b' */
259 ST_FUNC
void sym_pop(Sym
**ptop
, Sym
*b
)
269 /* remove symbol in token array */
271 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
272 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
274 ps
= &ts
->sym_struct
;
276 ps
= &ts
->sym_identifier
;
285 static void weaken_symbol(Sym
*sym
)
287 sym
->type
.t
|= VT_WEAK
;
292 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
293 esym_type
= ELFW(ST_TYPE
)(esym
->st_info
);
294 esym
->st_info
= ELFW(ST_INFO
)(STB_WEAK
, esym_type
);
298 /* ------------------------------------------------------------------------- */
300 ST_FUNC
void swap(int *p
, int *q
)
308 static void vsetc(CType
*type
, int r
, CValue
*vc
)
312 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
313 tcc_error("memory full (vstack)");
314 /* cannot let cpu flags if other instruction are generated. Also
315 avoid leaving VT_JMP anywhere except on the top of the stack
316 because it would complicate the code generator. */
317 if (vtop
>= vstack
) {
318 v
= vtop
->r
& VT_VALMASK
;
319 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
329 /* push constant of type "type" with useless value */
330 void vpush(CType
*type
)
333 vsetc(type
, VT_CONST
, &cval
);
336 /* push integer constant */
337 ST_FUNC
void vpushi(int v
)
341 vsetc(&int_type
, VT_CONST
, &cval
);
344 /* push a pointer sized constant */
345 static void vpushs(long long v
)
352 vsetc(&size_type
, VT_CONST
, &cval
);
355 /* push arbitrary 64bit constant */
356 void vpush64(int ty
, unsigned long long v
)
363 vsetc(&ctype
, VT_CONST
, &cval
);
366 /* push long long constant */
367 static inline void vpushll(long long v
)
369 vpush64(VT_LLONG
, v
);
372 /* push a symbol value of TYPE */
373 static inline void vpushsym(CType
*type
, Sym
*sym
)
378 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
382 /* Return a static symbol pointing to a section */
383 ST_FUNC Sym
*get_sym_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
389 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
390 sym
->type
.ref
= type
->ref
;
391 sym
->r
= VT_CONST
| VT_SYM
;
392 put_extern_sym(sym
, sec
, offset
, size
);
396 /* push a reference to a section offset by adding a dummy symbol */
397 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
399 vpushsym(type
, get_sym_ref(type
, sec
, offset
, size
));
402 /* define a new external reference to a symbol 'v' of type 'u' */
403 ST_FUNC Sym
*external_global_sym(int v
, CType
*type
, int r
)
409 /* push forward reference */
410 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
411 s
->type
.ref
= type
->ref
;
412 s
->r
= r
| VT_CONST
| VT_SYM
;
417 /* define a new external reference to a symbol 'v' with alternate asm
418 name 'asm_label' of type 'u'. 'asm_label' is equal to NULL if there
419 is no alternate name (most cases) */
420 static Sym
*external_sym(int v
, CType
*type
, int r
, char *asm_label
)
426 /* push forward reference */
427 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
428 s
->asm_label
= asm_label
;
429 s
->type
.t
|= VT_EXTERN
;
430 } else if (s
->type
.ref
== func_old_type
.ref
) {
431 s
->type
.ref
= type
->ref
;
432 s
->r
= r
| VT_CONST
| VT_SYM
;
433 s
->type
.t
|= VT_EXTERN
;
434 } else if (!is_compatible_types(&s
->type
, type
)) {
435 tcc_error("incompatible types for redefinition of '%s'",
436 get_tok_str(v
, NULL
));
441 /* push a reference to global symbol v */
442 ST_FUNC
void vpush_global_sym(CType
*type
, int v
)
444 vpushsym(type
, external_global_sym(v
, type
, 0));
447 ST_FUNC
void vset(CType
*type
, int r
, int v
)
452 vsetc(type
, r
, &cval
);
455 static void vseti(int r
, int v
)
463 ST_FUNC
void vswap(void)
466 /* cannot let cpu flags if other instruction are generated. Also
467 avoid leaving VT_JMP anywhere except on the top of the stack
468 because it would complicate the code generator. */
469 if (vtop
>= vstack
) {
470 int v
= vtop
->r
& VT_VALMASK
;
471 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
478 /* XXX: +2% overall speed possible with optimized memswap
480 * memswap(&vtop[0], &vtop[1], sizeof *vtop);
484 ST_FUNC
void vpushv(SValue
*v
)
486 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
487 tcc_error("memory full (vstack)");
492 static void vdup(void)
497 /* save r to the memory stack, and mark it as being free */
498 ST_FUNC
void save_reg(int r
)
500 int l
, saved
, size
, align
;
504 /* modify all stack values */
507 for(p
=vstack
;p
<=vtop
;p
++) {
508 if ((p
->r
& VT_VALMASK
) == r
||
509 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
510 /* must save value on stack if not already done */
512 /* NOTE: must reload 'r' because r might be equal to r2 */
513 r
= p
->r
& VT_VALMASK
;
514 /* store register in the stack */
516 if ((p
->r
& VT_LVAL
) ||
517 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
518 #ifdef TCC_TARGET_X86_64
519 type
= &char_pointer_type
;
523 size
= type_size(type
, &align
);
524 loc
= (loc
- size
) & -align
;
526 sv
.r
= VT_LOCAL
| VT_LVAL
;
529 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
530 /* x86 specific: need to pop fp register ST0 if saved */
532 o(0xd8dd); /* fstp %st(0) */
535 #ifndef TCC_TARGET_X86_64
536 /* special long long case */
537 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
545 /* mark that stack entry as being saved on the stack */
546 if (p
->r
& VT_LVAL
) {
547 /* also clear the bounded flag because the
548 relocation address of the function was stored in
550 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
552 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
560 #ifdef TCC_TARGET_ARM
561 /* find a register of class 'rc2' with at most one reference on stack.
562 * If none, call get_reg(rc) */
563 ST_FUNC
int get_reg_ex(int rc
, int rc2
)
568 for(r
=0;r
<NB_REGS
;r
++) {
569 if (reg_classes
[r
] & rc2
) {
572 for(p
= vstack
; p
<= vtop
; p
++) {
573 if ((p
->r
& VT_VALMASK
) == r
||
574 (p
->r2
& VT_VALMASK
) == r
)
585 /* find a free register of class 'rc'. If none, save one register */
586 ST_FUNC
int get_reg(int rc
)
591 /* find a free register */
592 for(r
=0;r
<NB_REGS
;r
++) {
593 if (reg_classes
[r
] & rc
) {
594 for(p
=vstack
;p
<=vtop
;p
++) {
595 if ((p
->r
& VT_VALMASK
) == r
||
596 (p
->r2
& VT_VALMASK
) == r
)
604 /* no register left : free the first one on the stack (VERY
605 IMPORTANT to start from the bottom to ensure that we don't
606 spill registers used in gen_opi()) */
607 for(p
=vstack
;p
<=vtop
;p
++) {
608 /* look at second register (if long long) */
609 r
= p
->r2
& VT_VALMASK
;
610 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
612 r
= p
->r
& VT_VALMASK
;
613 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
619 /* Should never comes here */
623 /* save registers up to (vtop - n) stack entry */
624 ST_FUNC
void save_regs(int n
)
629 for(p
= vstack
;p
<= p1
; p
++) {
630 r
= p
->r
& VT_VALMASK
;
637 /* move register 's' (of type 't') to 'r', and flush previous value of r to memory
639 static void move_reg(int r
, int s
, int t
)
653 /* get address of vtop (vtop MUST BE an lvalue) */
654 static void gaddrof(void)
656 if (vtop
->r
& VT_REF
)
659 /* tricky: if saved lvalue, then we can go back to lvalue */
660 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
661 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
666 #ifdef CONFIG_TCC_BCHECK
667 /* generate lvalue bound code */
668 static void gbound(void)
673 vtop
->r
&= ~VT_MUSTBOUND
;
674 /* if lvalue, then use checking code before dereferencing */
675 if (vtop
->r
& VT_LVAL
) {
676 /* if not VT_BOUNDED value, then make one */
677 if (!(vtop
->r
& VT_BOUNDED
)) {
678 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
679 /* must save type because we must set it to int to get pointer */
681 vtop
->type
.t
= VT_INT
;
684 gen_bounded_ptr_add();
685 vtop
->r
|= lval_type
;
688 /* then check for dereferencing */
689 gen_bounded_ptr_deref();
694 /* store vtop a register belonging to class 'rc'. lvalues are
695 converted to values. Cannot be used if cannot be converted to
696 register value (such as structures). */
697 ST_FUNC
int gv(int rc
)
699 int r
, bit_pos
, bit_size
, size
, align
, i
;
702 /* NOTE: get_reg can modify vstack[] */
703 if (vtop
->type
.t
& VT_BITFIELD
) {
706 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
707 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
708 /* remove bit field info to avoid loops */
709 vtop
->type
.t
&= ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
710 /* cast to int to propagate signedness in following ops */
711 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
716 if((vtop
->type
.t
& VT_UNSIGNED
) ||
717 (vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
718 type
.t
|= VT_UNSIGNED
;
720 /* generate shifts */
721 vpushi(bits
- (bit_pos
+ bit_size
));
723 vpushi(bits
- bit_size
);
724 /* NOTE: transformed to SHR if unsigned */
728 if (is_float(vtop
->type
.t
) &&
729 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
732 unsigned long offset
;
733 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
737 /* XXX: unify with initializers handling ? */
738 /* CPUs usually cannot use float constants, so we store them
739 generically in data segment */
740 size
= type_size(&vtop
->type
, &align
);
741 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
742 data_section
->data_offset
= offset
;
743 /* XXX: not portable yet */
744 #if defined(__i386__) || defined(__x86_64__)
745 /* Zero pad x87 tenbyte long doubles */
746 if (size
== LDOUBLE_SIZE
) {
747 vtop
->c
.tab
[2] &= 0xffff;
748 #if LDOUBLE_SIZE == 16
753 ptr
= section_ptr_add(data_section
, size
);
755 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
759 ptr
[i
] = vtop
->c
.tab
[size
-1-i
];
763 ptr
[i
] = vtop
->c
.tab
[i
];
764 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
765 vtop
->r
|= VT_LVAL
| VT_SYM
;
769 #ifdef CONFIG_TCC_BCHECK
770 if (vtop
->r
& VT_MUSTBOUND
)
774 r
= vtop
->r
& VT_VALMASK
;
775 rc2
= (rc
& RC_FLOAT
) ? RC_FLOAT
: RC_INT
;
778 #ifdef TCC_TARGET_X86_64
779 else if (rc
== RC_FRET
)
783 /* need to reload if:
785 - lvalue (need to dereference pointer)
786 - already a register, but not in the right class */
788 || (vtop
->r
& VT_LVAL
)
789 || !(reg_classes
[r
] & rc
)
790 #ifdef TCC_TARGET_X86_64
791 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
792 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
&& !(reg_classes
[vtop
->r2
] & rc2
))
794 || ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
799 #ifdef TCC_TARGET_X86_64
800 if (((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
)) {
801 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
803 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
804 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
805 unsigned long long ll
;
807 int r2
, original_type
;
808 original_type
= vtop
->type
.t
;
809 /* two register type load : expand to two words
811 #ifndef TCC_TARGET_X86_64
812 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
815 vtop
->c
.ui
= ll
; /* first word */
817 vtop
->r
= r
; /* save register value */
818 vpushi(ll
>> 32); /* second word */
821 if (r
>= VT_CONST
|| /* XXX: test to VT_CONST incorrect ? */
822 (vtop
->r
& VT_LVAL
)) {
823 /* We do not want to modifier the long long
824 pointer here, so the safest (and less
825 efficient) is to save all the other registers
826 in the stack. XXX: totally inefficient. */
828 /* load from memory */
829 vtop
->type
.t
= load_type
;
832 vtop
[-1].r
= r
; /* save register value */
833 /* increment pointer to get second word */
834 vtop
->type
.t
= addr_type
;
839 vtop
->type
.t
= load_type
;
844 vtop
[-1].r
= r
; /* save register value */
845 vtop
->r
= vtop
[-1].r2
;
847 /* Allocate second register. Here we rely on the fact that
848 get_reg() tries first to free r2 of an SValue. */
852 /* write second register */
854 vtop
->type
.t
= original_type
;
855 } else if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
857 /* lvalue of scalar type : need to use lvalue type
858 because of possible cast */
861 /* compute memory access type */
862 if (vtop
->r
& VT_REF
)
863 #ifdef TCC_TARGET_X86_64
868 else if (vtop
->r
& VT_LVAL_BYTE
)
870 else if (vtop
->r
& VT_LVAL_SHORT
)
872 if (vtop
->r
& VT_LVAL_UNSIGNED
)
876 /* restore wanted type */
879 /* one register type load */
884 #ifdef TCC_TARGET_C67
885 /* uses register pairs for doubles */
886 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
893 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
894 ST_FUNC
void gv2(int rc1
, int rc2
)
898 /* generate more generic register first. But VT_JMP or VT_CMP
899 values must be generated first in all cases to avoid possible
901 v
= vtop
[0].r
& VT_VALMASK
;
902 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
907 /* test if reload is needed for first register */
908 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
918 /* test if reload is needed for first register */
919 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
925 /* wrapper around RC_FRET to return a register by type */
926 static int rc_fret(int t
)
928 #ifdef TCC_TARGET_X86_64
929 if (t
== VT_LDOUBLE
) {
936 /* wrapper around REG_FRET to return a register by type */
937 static int reg_fret(int t
)
939 #ifdef TCC_TARGET_X86_64
940 if (t
== VT_LDOUBLE
) {
947 /* expand long long on stack in two int registers */
948 static void lexpand(void)
952 u
= vtop
->type
.t
& VT_UNSIGNED
;
955 vtop
[0].r
= vtop
[-1].r2
;
956 vtop
[0].r2
= VT_CONST
;
957 vtop
[-1].r2
= VT_CONST
;
958 vtop
[0].type
.t
= VT_INT
| u
;
959 vtop
[-1].type
.t
= VT_INT
| u
;
962 #ifdef TCC_TARGET_ARM
963 /* expand long long on stack */
964 ST_FUNC
void lexpand_nr(void)
968 u
= vtop
->type
.t
& VT_UNSIGNED
;
971 vtop
->type
.t
= VT_INT
| u
;
972 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
974 vtop
[-1].c
.ui
= vtop
->c
.ull
;
975 vtop
->c
.ui
= vtop
->c
.ull
>> 32;
977 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
979 vtop
->r
= vtop
[-1].r
;
980 } else if (v
> VT_CONST
) {
984 vtop
->r
= vtop
[-1].r2
;
985 vtop
[-1].r2
= VT_CONST
;
986 vtop
[-1].type
.t
= VT_INT
| u
;
990 /* build a long long from two ints */
991 static void lbuild(int t
)
994 vtop
[-1].r2
= vtop
[0].r
;
999 /* rotate n first stack elements to the bottom
1000 I1 ... In -> I2 ... In I1 [top is right]
1002 ST_FUNC
void vrotb(int n
)
1008 for(i
=-n
+1;i
!=0;i
++)
1009 vtop
[i
] = vtop
[i
+1];
1013 /* rotate the n elements before entry e towards the top
1014 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
1016 ST_FUNC
void vrote(SValue
*e
, int n
)
1022 for(i
= 0;i
< n
- 1; i
++)
1027 /* rotate n first stack elements to the top
1028 I1 ... In -> In I1 ... I(n-1) [top is right]
1030 ST_FUNC
void vrott(int n
)
1035 /* pop stack value */
1036 ST_FUNC
void vpop(void)
1039 v
= vtop
->r
& VT_VALMASK
;
1040 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1041 /* for x86, we need to pop the FP stack */
1042 if (v
== TREG_ST0
&& !nocode_wanted
) {
1043 o(0xd8dd); /* fstp %st(0) */
1046 if (v
== VT_JMP
|| v
== VT_JMPI
) {
1047 /* need to put correct jump if && or || without test */
1053 /* convert stack entry to register and duplicate its value in another
1055 static void gv_dup(void)
1061 if ((t
& VT_BTYPE
) == VT_LLONG
) {
1068 /* stack: H L L1 H1 */
1076 /* duplicate value */
1081 #ifdef TCC_TARGET_X86_64
1082 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1092 load(r1
, &sv
); /* move r to r1 */
1094 /* duplicates value */
1100 /* Generate value test
1102 * Generate a test for any value (jump, comparison and integers) */
1103 int gvtst(int inv
, int t
)
1105 int v
= vtop
->r
& VT_VALMASK
;
1106 if (v
!= VT_CMP
&& v
!= VT_JMP
&& v
!= VT_JMPI
) {
1110 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1111 /* constant jmp optimization */
1112 if ((vtop
->c
.i
!= 0) != inv
)
1117 return gtst(inv
, t
);
1120 #ifndef TCC_TARGET_X86_64
1121 /* generate CPU independent (unsigned) long long operations */
1122 static void gen_opl(int op
)
1124 int t
, a
, b
, op1
, c
, i
;
1126 unsigned short reg_iret
= REG_IRET
;
1127 unsigned short reg_lret
= REG_LRET
;
1133 func
= TOK___divdi3
;
1136 func
= TOK___udivdi3
;
1139 func
= TOK___moddi3
;
1142 func
= TOK___umoddi3
;
1149 /* call generic long long function */
1150 vpush_global_sym(&func_old_type
, func
);
1155 vtop
->r2
= reg_lret
;
1168 /* stack: L1 H1 L2 H2 */
1173 vtop
[-2] = vtop
[-3];
1176 /* stack: H1 H2 L1 L2 */
1182 /* stack: H1 H2 L1 L2 ML MH */
1185 /* stack: ML MH H1 H2 L1 L2 */
1189 /* stack: ML MH H1 L2 H2 L1 */
1194 /* stack: ML MH M1 M2 */
1197 } else if (op
== '+' || op
== '-') {
1198 /* XXX: add non carry method too (for MIPS or alpha) */
1204 /* stack: H1 H2 (L1 op L2) */
1207 gen_op(op1
+ 1); /* TOK_xxxC2 */
1210 /* stack: H1 H2 (L1 op L2) */
1213 /* stack: (L1 op L2) H1 H2 */
1215 /* stack: (L1 op L2) (H1 op H2) */
1223 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1224 t
= vtop
[-1].type
.t
;
1228 /* stack: L H shift */
1230 /* constant: simpler */
1231 /* NOTE: all comments are for SHL. the other cases are
1232 done by swaping words */
1243 if (op
!= TOK_SAR
) {
1276 /* XXX: should provide a faster fallback on x86 ? */
1279 func
= TOK___ashrdi3
;
1282 func
= TOK___lshrdi3
;
1285 func
= TOK___ashldi3
;
1291 /* compare operations */
1297 /* stack: L1 H1 L2 H2 */
1299 vtop
[-1] = vtop
[-2];
1301 /* stack: L1 L2 H1 H2 */
1304 /* when values are equal, we need to compare low words. since
1305 the jump is inverted, we invert the test too. */
1308 else if (op1
== TOK_GT
)
1310 else if (op1
== TOK_ULT
)
1312 else if (op1
== TOK_UGT
)
1317 if (op1
!= TOK_NE
) {
1321 /* generate non equal test */
1322 /* XXX: NOT PORTABLE yet */
1326 #if defined(TCC_TARGET_I386)
1327 b
= psym(0x850f, 0);
1328 #elif defined(TCC_TARGET_ARM)
1330 o(0x1A000000 | encbranch(ind
, 0, 1));
1331 #elif defined(TCC_TARGET_C67)
1332 tcc_error("not implemented");
1334 #error not supported
1338 /* compare low. Always unsigned */
1342 else if (op1
== TOK_LE
)
1344 else if (op1
== TOK_GT
)
1346 else if (op1
== TOK_GE
)
1357 /* handle integer constant optimizations and various machine
1359 static void gen_opic(int op
)
1361 int c1
, c2
, t1
, t2
, n
;
1364 typedef unsigned long long U
;
1368 t1
= v1
->type
.t
& VT_BTYPE
;
1369 t2
= v2
->type
.t
& VT_BTYPE
;
1373 else if (v1
->type
.t
& VT_UNSIGNED
)
1380 else if (v2
->type
.t
& VT_UNSIGNED
)
1385 /* currently, we cannot do computations with forward symbols */
1386 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1387 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1390 case '+': l1
+= l2
; break;
1391 case '-': l1
-= l2
; break;
1392 case '&': l1
&= l2
; break;
1393 case '^': l1
^= l2
; break;
1394 case '|': l1
|= l2
; break;
1395 case '*': l1
*= l2
; break;
1402 /* if division by zero, generate explicit division */
1405 tcc_error("division by zero in constant");
1409 default: l1
/= l2
; break;
1410 case '%': l1
%= l2
; break;
1411 case TOK_UDIV
: l1
= (U
)l1
/ l2
; break;
1412 case TOK_UMOD
: l1
= (U
)l1
% l2
; break;
1415 case TOK_SHL
: l1
<<= l2
; break;
1416 case TOK_SHR
: l1
= (U
)l1
>> l2
; break;
1417 case TOK_SAR
: l1
>>= l2
; break;
1419 case TOK_ULT
: l1
= (U
)l1
< (U
)l2
; break;
1420 case TOK_UGE
: l1
= (U
)l1
>= (U
)l2
; break;
1421 case TOK_EQ
: l1
= l1
== l2
; break;
1422 case TOK_NE
: l1
= l1
!= l2
; break;
1423 case TOK_ULE
: l1
= (U
)l1
<= (U
)l2
; break;
1424 case TOK_UGT
: l1
= (U
)l1
> (U
)l2
; break;
1425 case TOK_LT
: l1
= l1
< l2
; break;
1426 case TOK_GE
: l1
= l1
>= l2
; break;
1427 case TOK_LE
: l1
= l1
<= l2
; break;
1428 case TOK_GT
: l1
= l1
> l2
; break;
1430 case TOK_LAND
: l1
= l1
&& l2
; break;
1431 case TOK_LOR
: l1
= l1
|| l2
; break;
1438 /* if commutative ops, put c2 as constant */
1439 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1440 op
== '|' || op
== '*')) {
1442 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1443 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1445 /* Filter out NOP operations like x*1, x-0, x&-1... */
1446 if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1449 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1450 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1456 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1457 /* try to use shifts instead of muls or divs */
1458 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1467 else if (op
== TOK_PDIV
)
1473 } else if (c2
&& (op
== '+' || op
== '-') &&
1474 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
1475 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1476 /* symbol + constant case */
1483 if (!nocode_wanted
) {
1484 /* call low level op generator */
1485 if (t1
== VT_LLONG
|| t2
== VT_LLONG
)
1496 /* generate a floating point operation with constant propagation */
1497 static void gen_opif(int op
)
1505 /* currently, we cannot do computations with forward symbols */
1506 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1507 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1509 if (v1
->type
.t
== VT_FLOAT
) {
1512 } else if (v1
->type
.t
== VT_DOUBLE
) {
1520 /* NOTE: we only do constant propagation if finite number (not
1521 NaN or infinity) (ANSI spec) */
1522 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1526 case '+': f1
+= f2
; break;
1527 case '-': f1
-= f2
; break;
1528 case '*': f1
*= f2
; break;
1532 tcc_error("division by zero in constant");
1537 /* XXX: also handles tests ? */
1541 /* XXX: overflow test ? */
1542 if (v1
->type
.t
== VT_FLOAT
) {
1544 } else if (v1
->type
.t
== VT_DOUBLE
) {
1552 if (!nocode_wanted
) {
1560 static int pointed_size(CType
*type
)
1563 return type_size(pointed_type(type
), &align
);
1566 static void vla_runtime_pointed_size(CType
*type
)
1569 vla_runtime_type_size(pointed_type(type
), &align
);
1572 static inline int is_null_pointer(SValue
*p
)
1574 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1576 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& p
->c
.i
== 0) ||
1577 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.ll
== 0) ||
1578 ((p
->type
.t
& VT_BTYPE
) == VT_PTR
&& p
->c
.ptr
== 0);
1581 static inline int is_integer_btype(int bt
)
1583 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
1584 bt
== VT_INT
|| bt
== VT_LLONG
);
1587 /* check types for comparison or substraction of pointers */
1588 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
1590 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
1593 /* null pointers are accepted for all comparisons as gcc */
1594 if (is_null_pointer(p1
) || is_null_pointer(p2
))
1598 bt1
= type1
->t
& VT_BTYPE
;
1599 bt2
= type2
->t
& VT_BTYPE
;
1600 /* accept comparison between pointer and integer with a warning */
1601 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
1602 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
1603 tcc_warning("comparison between pointer and integer");
1607 /* both must be pointers or implicit function pointers */
1608 if (bt1
== VT_PTR
) {
1609 type1
= pointed_type(type1
);
1610 } else if (bt1
!= VT_FUNC
)
1611 goto invalid_operands
;
1613 if (bt2
== VT_PTR
) {
1614 type2
= pointed_type(type2
);
1615 } else if (bt2
!= VT_FUNC
) {
1617 tcc_error("invalid operands to binary %s", get_tok_str(op
, NULL
));
1619 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1620 (type2
->t
& VT_BTYPE
) == VT_VOID
)
1624 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1625 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1626 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1627 /* gcc-like error if '-' is used */
1629 goto invalid_operands
;
1631 tcc_warning("comparison of distinct pointer types lacks a cast");
1635 /* generic gen_op: handles types problems */
1636 ST_FUNC
void gen_op(int op
)
1638 int u
, t1
, t2
, bt1
, bt2
, t
;
1641 t1
= vtop
[-1].type
.t
;
1642 t2
= vtop
[0].type
.t
;
1643 bt1
= t1
& VT_BTYPE
;
1644 bt2
= t2
& VT_BTYPE
;
1646 if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
1647 /* at least one operand is a pointer */
1648 /* relationnal op: must be both pointers */
1649 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
1650 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1651 /* pointers are handled are unsigned */
1652 #ifdef TCC_TARGET_X86_64
1653 t
= VT_LLONG
| VT_UNSIGNED
;
1655 t
= VT_INT
| VT_UNSIGNED
;
1659 /* if both pointers, then it must be the '-' op */
1660 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
1662 tcc_error("cannot use pointers here");
1663 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1664 /* XXX: check that types are compatible */
1665 if (vtop
[-1].type
.t
& VT_VLA
) {
1666 vla_runtime_pointed_size(&vtop
[-1].type
);
1668 vpushi(pointed_size(&vtop
[-1].type
));
1672 /* set to integer type */
1673 #ifdef TCC_TARGET_X86_64
1674 vtop
->type
.t
= VT_LLONG
;
1676 vtop
->type
.t
= VT_INT
;
1681 /* exactly one pointer : must be '+' or '-'. */
1682 if (op
!= '-' && op
!= '+')
1683 tcc_error("cannot use pointers here");
1684 /* Put pointer as first operand */
1685 if (bt2
== VT_PTR
) {
1689 type1
= vtop
[-1].type
;
1690 type1
.t
&= ~VT_ARRAY
;
1691 if (vtop
[-1].type
.t
& VT_VLA
)
1692 vla_runtime_pointed_size(&vtop
[-1].type
);
1694 u
= pointed_size(&vtop
[-1].type
);
1696 tcc_error("unknown array element size");
1697 #ifdef TCC_TARGET_X86_64
1700 /* XXX: cast to int ? (long long case) */
1705 #ifdef CONFIG_TCC_BCHECK
1706 /* if evaluating constant expression, no code should be
1707 generated, so no bound check */
1708 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
1709 /* if bounded pointers, we generate a special code to
1716 gen_bounded_ptr_add();
1722 /* put again type if gen_opic() swaped operands */
1725 } else if (is_float(bt1
) || is_float(bt2
)) {
1726 /* compute bigger type and do implicit casts */
1727 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
1729 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
1734 /* floats can only be used for a few operations */
1735 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
1736 (op
< TOK_ULT
|| op
> TOK_GT
))
1737 tcc_error("invalid operands for binary operation");
1739 } else if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
) {
1740 t
= bt1
== VT_LLONG
? VT_LLONG
: VT_INT
;
1741 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (t
| VT_UNSIGNED
))
1744 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
1745 /* cast to biggest op */
1747 /* convert to unsigned if it does not fit in a long long */
1748 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
1749 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
1752 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
1753 tcc_error("comparison of struct");
1755 /* integer operations */
1757 /* convert to unsigned if it does not fit in an integer */
1758 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
1759 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
1762 /* XXX: currently, some unsigned operations are explicit, so
1763 we modify them here */
1764 if (t
& VT_UNSIGNED
) {
1771 else if (op
== TOK_LT
)
1773 else if (op
== TOK_GT
)
1775 else if (op
== TOK_LE
)
1777 else if (op
== TOK_GE
)
1784 /* special case for shifts and long long: we keep the shift as
1786 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
1793 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
1794 /* relationnal op: the result is an int */
1795 vtop
->type
.t
= VT_INT
;
1802 #ifndef TCC_TARGET_ARM
1803 /* generic itof for unsigned long long case */
1804 static void gen_cvt_itof1(int t
)
1806 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
1807 (VT_LLONG
| VT_UNSIGNED
)) {
1810 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
1811 #if LDOUBLE_SIZE != 8
1812 else if (t
== VT_LDOUBLE
)
1813 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
1816 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
1820 vtop
->r
= reg_fret(t
);
1827 /* generic ftoi for unsigned long long case */
1828 static void gen_cvt_ftoi1(int t
)
1832 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
1833 /* not handled natively */
1834 st
= vtop
->type
.t
& VT_BTYPE
;
1836 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
1837 #if LDOUBLE_SIZE != 8
1838 else if (st
== VT_LDOUBLE
)
1839 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
1842 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
1847 vtop
->r2
= REG_LRET
;
1853 /* force char or short cast */
1854 static void force_charshort_cast(int t
)
1858 /* XXX: add optimization if lvalue : just change type and offset */
1863 if (t
& VT_UNSIGNED
) {
1864 vpushi((1 << bits
) - 1);
1870 /* result must be signed or the SAR is converted to an SHL
1871 This was not the case when "t" was a signed short
1872 and the last value on the stack was an unsigned int */
1873 vtop
->type
.t
&= ~VT_UNSIGNED
;
1879 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1880 static void gen_cast(CType
*type
)
1882 int sbt
, dbt
, sf
, df
, c
, p
;
1884 /* special delayed cast for char/short */
1885 /* XXX: in some cases (multiple cascaded casts), it may still
1887 if (vtop
->r
& VT_MUSTCAST
) {
1888 vtop
->r
&= ~VT_MUSTCAST
;
1889 force_charshort_cast(vtop
->type
.t
);
1892 /* bitfields first get cast to ints */
1893 if (vtop
->type
.t
& VT_BITFIELD
) {
1897 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
1898 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
1903 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1904 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
1906 /* constant case: we can do it now */
1907 /* XXX: in ISOC, cannot do it if error in convert */
1908 if (sbt
== VT_FLOAT
)
1909 vtop
->c
.ld
= vtop
->c
.f
;
1910 else if (sbt
== VT_DOUBLE
)
1911 vtop
->c
.ld
= vtop
->c
.d
;
1914 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
1915 if (sbt
& VT_UNSIGNED
)
1916 vtop
->c
.ld
= vtop
->c
.ull
;
1918 vtop
->c
.ld
= vtop
->c
.ll
;
1920 if (sbt
& VT_UNSIGNED
)
1921 vtop
->c
.ld
= vtop
->c
.ui
;
1923 vtop
->c
.ld
= vtop
->c
.i
;
1926 if (dbt
== VT_FLOAT
)
1927 vtop
->c
.f
= (float)vtop
->c
.ld
;
1928 else if (dbt
== VT_DOUBLE
)
1929 vtop
->c
.d
= (double)vtop
->c
.ld
;
1930 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
1931 vtop
->c
.ull
= (unsigned long long)vtop
->c
.ld
;
1932 } else if (sf
&& dbt
== VT_BOOL
) {
1933 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
1936 vtop
->c
.ll
= (long long)vtop
->c
.ld
;
1937 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
1938 vtop
->c
.ll
= vtop
->c
.ull
;
1939 else if (sbt
& VT_UNSIGNED
)
1940 vtop
->c
.ll
= vtop
->c
.ui
;
1941 #ifdef TCC_TARGET_X86_64
1942 else if (sbt
== VT_PTR
)
1945 else if (sbt
!= VT_LLONG
)
1946 vtop
->c
.ll
= vtop
->c
.i
;
1948 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
1949 vtop
->c
.ull
= vtop
->c
.ll
;
1950 else if (dbt
== VT_BOOL
)
1951 vtop
->c
.i
= (vtop
->c
.ll
!= 0);
1952 #ifdef TCC_TARGET_X86_64
1953 else if (dbt
== VT_PTR
)
1956 else if (dbt
!= VT_LLONG
) {
1958 if ((dbt
& VT_BTYPE
) == VT_BYTE
)
1960 else if ((dbt
& VT_BTYPE
) == VT_SHORT
)
1962 if(dbt
& VT_UNSIGNED
)
1963 vtop
->c
.ui
= ((unsigned int)vtop
->c
.ll
<< s
) >> s
;
1965 vtop
->c
.i
= ((int)vtop
->c
.ll
<< s
) >> s
;
1968 } else if (p
&& dbt
== VT_BOOL
) {
1971 } else if (!nocode_wanted
) {
1972 /* non constant case: generate code */
1974 /* convert from fp to fp */
1977 /* convert int to fp */
1980 /* convert fp to int */
1981 if (dbt
== VT_BOOL
) {
1985 /* we handle char/short/etc... with generic code */
1986 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
1987 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
1991 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
1992 /* additional cast for char/short... */
1997 #ifndef TCC_TARGET_X86_64
1998 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
1999 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
2000 /* scalar to long long */
2001 /* machine independent conversion */
2003 /* generate high word */
2004 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
2008 if (sbt
== VT_PTR
) {
2009 /* cast from pointer to int before we apply
2010 shift operation, which pointers don't support*/
2011 gen_cast(&int_type
);
2017 /* patch second register */
2018 vtop
[-1].r2
= vtop
->r
;
2022 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
2023 (dbt
& VT_BTYPE
) == VT_PTR
||
2024 (dbt
& VT_BTYPE
) == VT_FUNC
) {
2025 if ((sbt
& VT_BTYPE
) != VT_LLONG
&&
2026 (sbt
& VT_BTYPE
) != VT_PTR
&&
2027 (sbt
& VT_BTYPE
) != VT_FUNC
) {
2028 /* need to convert from 32bit to 64bit */
2030 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
2031 /* x86_64 specific: movslq */
2033 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
2037 } else if (dbt
== VT_BOOL
) {
2038 /* scalar to bool */
2041 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
2042 (dbt
& VT_BTYPE
) == VT_SHORT
) {
2043 if (sbt
== VT_PTR
) {
2044 vtop
->type
.t
= VT_INT
;
2045 tcc_warning("nonportable conversion from pointer to char/short");
2047 force_charshort_cast(dbt
);
2048 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
2050 if (sbt
== VT_LLONG
) {
2051 /* from long long: just take low order word */
2055 /* if lvalue and single word type, nothing to do because
2056 the lvalue already contains the real type size (see
2057 VT_LVAL_xxx constants) */
2060 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
2061 /* if we are casting between pointer types,
2062 we must update the VT_LVAL_xxx size */
2063 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
2064 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
2069 /* return type size as known at compile time. Put alignment at 'a' */
2070 ST_FUNC
int type_size(CType
*type
, int *a
)
2075 bt
= type
->t
& VT_BTYPE
;
2076 if (bt
== VT_STRUCT
) {
2081 } else if (bt
== VT_PTR
) {
2082 if (type
->t
& VT_ARRAY
) {
2086 ts
= type_size(&s
->type
, a
);
2088 if (ts
< 0 && s
->c
< 0)
2096 } else if (bt
== VT_LDOUBLE
) {
2098 return LDOUBLE_SIZE
;
2099 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
2100 #ifdef TCC_TARGET_I386
2101 #ifdef TCC_TARGET_PE
2106 #elif defined(TCC_TARGET_ARM)
2116 } else if (bt
== VT_INT
|| bt
== VT_ENUM
|| bt
== VT_FLOAT
) {
2119 } else if (bt
== VT_SHORT
) {
2122 } else if (bt
== VT_QLONG
|| bt
== VT_QFLOAT
) {
2126 /* char, void, function, _Bool */
2132 /* push type size as known at runtime time on top of value stack. Put
2134 ST_FUNC
void vla_runtime_type_size(CType
*type
, int *a
)
2136 if (type
->t
& VT_VLA
) {
2137 vset(&int_type
, VT_LOCAL
|VT_LVAL
, type
->ref
->c
);
2139 vpushi(type_size(type
, a
));
2143 static void vla_sp_save(void) {
2144 if (!(vla_flags
& VLA_SP_LOC_SET
)) {
2145 *vla_sp_loc
= (loc
-= PTR_SIZE
);
2146 vla_flags
|= VLA_SP_LOC_SET
;
2148 if (!(vla_flags
& VLA_SP_SAVED
)) {
2149 gen_vla_sp_save(*vla_sp_loc
);
2150 vla_flags
|= VLA_SP_SAVED
;
2154 /* return the pointed type of t */
2155 static inline CType
*pointed_type(CType
*type
)
2157 return &type
->ref
->type
;
2160 /* modify type so that its it is a pointer to type. */
2161 ST_FUNC
void mk_pointer(CType
*type
)
2164 s
= sym_push(SYM_FIELD
, type
, 0, -1);
2165 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
2169 /* compare function types. OLD functions match any new functions */
2170 static int is_compatible_func(CType
*type1
, CType
*type2
)
2176 if (!is_compatible_types(&s1
->type
, &s2
->type
))
2178 /* check func_call */
2179 if (s1
->a
.func_call
!= s2
->a
.func_call
)
2181 /* XXX: not complete */
2182 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
2186 while (s1
!= NULL
) {
2189 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
2199 /* return true if type1 and type2 are the same. If unqualified is
2200 true, qualifiers on the types are ignored.
2202 - enums are not checked as gcc __builtin_types_compatible_p ()
2204 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2208 t1
= type1
->t
& VT_TYPE
;
2209 t2
= type2
->t
& VT_TYPE
;
2211 /* strip qualifiers before comparing */
2212 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2213 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2215 /* XXX: bitfields ? */
2218 /* test more complicated cases */
2219 bt1
= t1
& VT_BTYPE
;
2220 if (bt1
== VT_PTR
) {
2221 type1
= pointed_type(type1
);
2222 type2
= pointed_type(type2
);
2223 return is_compatible_types(type1
, type2
);
2224 } else if (bt1
== VT_STRUCT
) {
2225 return (type1
->ref
== type2
->ref
);
2226 } else if (bt1
== VT_FUNC
) {
2227 return is_compatible_func(type1
, type2
);
2233 /* return true if type1 and type2 are exactly the same (including
2236 static int is_compatible_types(CType
*type1
, CType
*type2
)
2238 return compare_types(type1
,type2
,0);
2241 /* return true if type1 and type2 are the same (ignoring qualifiers).
2243 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
2245 return compare_types(type1
,type2
,1);
2248 /* print a type. If 'varstr' is not NULL, then the variable is also
2249 printed in the type */
2251 /* XXX: add array and function pointers */
2252 static void type_to_str(char *buf
, int buf_size
,
2253 CType
*type
, const char *varstr
)
2260 t
= type
->t
& VT_TYPE
;
2263 if (t
& VT_CONSTANT
)
2264 pstrcat(buf
, buf_size
, "const ");
2265 if (t
& VT_VOLATILE
)
2266 pstrcat(buf
, buf_size
, "volatile ");
2267 if (t
& VT_UNSIGNED
)
2268 pstrcat(buf
, buf_size
, "unsigned ");
2298 tstr
= "long double";
2300 pstrcat(buf
, buf_size
, tstr
);
2304 if (bt
== VT_STRUCT
)
2308 pstrcat(buf
, buf_size
, tstr
);
2309 v
= type
->ref
->v
& ~SYM_STRUCT
;
2310 if (v
>= SYM_FIRST_ANOM
)
2311 pstrcat(buf
, buf_size
, "<anonymous>");
2313 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
2317 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
2318 pstrcat(buf
, buf_size
, "(");
2320 while (sa
!= NULL
) {
2321 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
2322 pstrcat(buf
, buf_size
, buf1
);
2325 pstrcat(buf
, buf_size
, ", ");
2327 pstrcat(buf
, buf_size
, ")");
2331 pstrcpy(buf1
, sizeof(buf1
), "*");
2333 pstrcat(buf1
, sizeof(buf1
), varstr
);
2334 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2338 pstrcat(buf
, buf_size
, " ");
2339 pstrcat(buf
, buf_size
, varstr
);
2344 /* verify type compatibility to store vtop in 'dt' type, and generate
2346 static void gen_assign_cast(CType
*dt
)
2348 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
2349 char buf1
[256], buf2
[256];
2352 st
= &vtop
->type
; /* source type */
2353 dbt
= dt
->t
& VT_BTYPE
;
2354 sbt
= st
->t
& VT_BTYPE
;
2355 if (sbt
== VT_VOID
|| dbt
== VT_VOID
)
2356 tcc_error("cannot cast from/to void");
2357 if (dt
->t
& VT_CONSTANT
)
2358 tcc_warning("assignment of read-only location");
2361 /* special cases for pointers */
2362 /* '0' can also be a pointer */
2363 if (is_null_pointer(vtop
))
2365 /* accept implicit pointer to integer cast with warning */
2366 if (is_integer_btype(sbt
)) {
2367 tcc_warning("assignment makes pointer from integer without a cast");
2370 type1
= pointed_type(dt
);
2371 /* a function is implicitely a function pointer */
2372 if (sbt
== VT_FUNC
) {
2373 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
2374 !is_compatible_types(pointed_type(dt
), st
))
2375 tcc_warning("assignment from incompatible pointer type");
2380 type2
= pointed_type(st
);
2381 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2382 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
2383 /* void * can match anything */
2385 /* exact type match, except for unsigned */
2388 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2389 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2390 if (!is_compatible_types(&tmp_type1
, &tmp_type2
))
2391 tcc_warning("assignment from incompatible pointer type");
2393 /* check const and volatile */
2394 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
2395 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
2396 tcc_warning("assignment discards qualifiers from pointer target type");
2402 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
2403 tcc_warning("assignment makes integer from pointer without a cast");
2405 /* XXX: more tests */
2410 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2411 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2412 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2414 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
2415 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
2416 tcc_error("cannot cast '%s' to '%s'", buf1
, buf2
);
2424 /* store vtop in lvalue pushed on stack */
2425 ST_FUNC
void vstore(void)
2427 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
2429 ft
= vtop
[-1].type
.t
;
2430 sbt
= vtop
->type
.t
& VT_BTYPE
;
2431 dbt
= ft
& VT_BTYPE
;
2432 if ((((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2433 (sbt
== VT_INT
&& dbt
== VT_SHORT
))
2434 && !(vtop
->type
.t
& VT_BITFIELD
)) {
2435 /* optimize char/short casts */
2436 delayed_cast
= VT_MUSTCAST
;
2437 vtop
->type
.t
= ft
& (VT_TYPE
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
)));
2438 /* XXX: factorize */
2439 if (ft
& VT_CONSTANT
)
2440 tcc_warning("assignment of read-only location");
2443 if (!(ft
& VT_BITFIELD
))
2444 gen_assign_cast(&vtop
[-1].type
);
2447 if (sbt
== VT_STRUCT
) {
2448 /* if structure, only generate pointer */
2449 /* structure assignment : generate memcpy */
2450 /* XXX: optimize if small size */
2451 if (!nocode_wanted
) {
2452 size
= type_size(&vtop
->type
, &align
);
2456 vtop
->type
.t
= VT_PTR
;
2459 /* address of memcpy() */
2462 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2463 else if(!(align
& 3))
2464 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2467 vpush_global_sym(&func_old_type
, TOK_memcpy
);
2472 vtop
->type
.t
= VT_PTR
;
2481 /* leave source on stack */
2482 } else if (ft
& VT_BITFIELD
) {
2483 /* bitfield store handling */
2484 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2485 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2486 /* remove bit field info to avoid loops */
2487 vtop
[-1].type
.t
= ft
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
2489 /* duplicate source into other register */
2494 if((ft
& VT_BTYPE
) == VT_BOOL
) {
2495 gen_cast(&vtop
[-1].type
);
2496 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2499 /* duplicate destination */
2501 vtop
[-1] = vtop
[-2];
2503 /* mask and shift source */
2504 if((ft
& VT_BTYPE
) != VT_BOOL
) {
2505 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2506 vpushll((1ULL << bit_size
) - 1ULL);
2508 vpushi((1 << bit_size
) - 1);
2514 /* load destination, mask and or with source */
2516 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2517 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2519 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2526 /* pop off shifted source from "duplicate source..." above */
2530 #ifdef CONFIG_TCC_BCHECK
2531 /* bound check case */
2532 if (vtop
[-1].r
& VT_MUSTBOUND
) {
2538 if (!nocode_wanted
) {
2542 #ifdef TCC_TARGET_X86_64
2543 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
2545 } else if ((ft
& VT_BTYPE
) == VT_QFLOAT
) {
2550 r
= gv(rc
); /* generate value */
2551 /* if lvalue was saved on stack, must read it */
2552 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
2554 t
= get_reg(RC_INT
);
2555 #ifdef TCC_TARGET_X86_64
2560 sv
.r
= VT_LOCAL
| VT_LVAL
;
2561 sv
.c
.ul
= vtop
[-1].c
.ul
;
2563 vtop
[-1].r
= t
| VT_LVAL
;
2565 /* two word case handling : store second register at word + 4 (or +8 for x86-64) */
2566 #ifdef TCC_TARGET_X86_64
2567 if (((ft
& VT_BTYPE
) == VT_QLONG
) || ((ft
& VT_BTYPE
) == VT_QFLOAT
)) {
2568 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
2570 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
2571 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
2573 vtop
[-1].type
.t
= load_type
;
2576 /* convert to int to increment easily */
2577 vtop
->type
.t
= addr_type
;
2583 vtop
[-1].type
.t
= load_type
;
2584 /* XXX: it works because r2 is spilled last ! */
2585 store(vtop
->r2
, vtop
- 1);
2591 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
2592 vtop
->r
|= delayed_cast
;
2596 /* post defines POST/PRE add. c is the token ++ or -- */
2597 ST_FUNC
void inc(int post
, int c
)
2600 vdup(); /* save lvalue */
2602 gv_dup(); /* duplicate value */
2607 vpushi(c
- TOK_MID
);
2609 vstore(); /* store value */
2611 vpop(); /* if post op, return saved value */
2614 /* Parse GNUC __attribute__ extension. Currently, the following
2615 extensions are recognized:
2616 - aligned(n) : set data/function alignment.
2617 - packed : force data alignment to 1
2618 - section(x) : generate data/code in this section.
2619 - unused : currently ignored, but may be used someday.
2620 - regparm(n) : pass function parameters in registers (i386 only)
2622 static void parse_attribute(AttributeDef
*ad
)
2626 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
2630 while (tok
!= ')') {
2631 if (tok
< TOK_IDENT
)
2632 expect("attribute name");
2640 expect("section name");
2641 ad
->section
= find_section(tcc_state
, (char *)tokc
.cstr
->data
);
2649 expect("alias(\"target\")");
2650 ad
->alias_target
= /* save string as token, for later */
2651 tok_alloc((char*)tokc
.cstr
->data
, tokc
.cstr
->size
-1)->tok
;
2660 if (n
<= 0 || (n
& (n
- 1)) != 0)
2661 tcc_error("alignment must be a positive power of two");
2678 /* currently, no need to handle it because tcc does not
2679 track unused objects */
2683 /* currently, no need to handle it because tcc does not
2684 track unused objects */
2689 ad
->a
.func_call
= FUNC_CDECL
;
2694 ad
->a
.func_call
= FUNC_STDCALL
;
2696 #ifdef TCC_TARGET_I386
2706 ad
->a
.func_call
= FUNC_FASTCALL1
+ n
- 1;
2712 ad
->a
.func_call
= FUNC_FASTCALLW
;
2719 ad
->a
.mode
= VT_LLONG
+ 1;
2722 ad
->a
.mode
= VT_SHORT
+ 1;
2725 ad
->a
.mode
= VT_INT
+ 1;
2728 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
2735 ad
->a
.func_export
= 1;
2738 ad
->a
.func_import
= 1;
2741 if (tcc_state
->warn_unsupported
)
2742 tcc_warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
2743 /* skip parameters */
2745 int parenthesis
= 0;
2749 else if (tok
== ')')
2752 } while (parenthesis
&& tok
!= -1);
2765 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2766 static void struct_decl(CType
*type
, int u
, int tdef
)
2768 int a
, v
, size
, align
, maxalign
, c
, offset
, flexible
;
2769 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
, prevbt
;
2770 Sym
*s
, *ss
, *ass
, **ps
;
2774 a
= tok
; /* save decl type */
2779 /* struct already defined ? return it */
2781 expect("struct/union/enum name");
2785 tcc_error("invalid type");
2787 } else if (tok
>= TOK_IDENT
&& !tdef
)
2788 tcc_error("unknown struct/union/enum");
2794 /* we put an undefined size for struct/union */
2795 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
2796 s
->r
= 0; /* default alignment is zero as gcc */
2797 /* put struct/union/enum name in type */
2805 tcc_error("struct/union/enum already defined");
2806 /* cannot be empty */
2808 /* non empty enums are not allowed */
2809 if (a
== TOK_ENUM
) {
2813 expect("identifier");
2816 tcc_error("redefinition of enumerator '%s'",
2817 get_tok_str(v
, NULL
));
2823 /* enum symbols have static storage */
2824 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
2825 ss
->type
.t
|= VT_STATIC
;
2830 /* NOTE: we accept a trailing comma */
2834 s
->c
= type_size(&int_type
, &align
);
2843 while (tok
!= '}') {
2844 parse_btype(&btype
, &ad
);
2847 tcc_error("flexible array member '%s' not at the end of struct",
2848 get_tok_str(v
, NULL
));
2853 type_decl(&type1
, &ad
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
2854 if (v
== 0 && (type1
.t
& VT_BTYPE
) != VT_STRUCT
)
2855 expect("identifier");
2856 if (type_size(&type1
, &align
) < 0) {
2857 if ((a
== TOK_STRUCT
) && (type1
.t
& VT_ARRAY
) && c
)
2860 tcc_error("field '%s' has incomplete type",
2861 get_tok_str(v
, NULL
));
2863 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
2864 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
2865 tcc_error("invalid type for '%s'",
2866 get_tok_str(v
, NULL
));
2870 bit_size
= expr_const();
2871 /* XXX: handle v = 0 case for messages */
2873 tcc_error("negative width in bit-field '%s'",
2874 get_tok_str(v
, NULL
));
2875 if (v
&& bit_size
== 0)
2876 tcc_error("zero width for bit-field '%s'",
2877 get_tok_str(v
, NULL
));
2879 size
= type_size(&type1
, &align
);
2881 if (align
< ad
.a
.aligned
)
2882 align
= ad
.a
.aligned
;
2883 } else if (ad
.a
.packed
) {
2885 } else if (*tcc_state
->pack_stack_ptr
) {
2886 if (align
> *tcc_state
->pack_stack_ptr
)
2887 align
= *tcc_state
->pack_stack_ptr
;
2890 if (bit_size
>= 0) {
2891 bt
= type1
.t
& VT_BTYPE
;
2898 tcc_error("bitfields must have scalar type");
2900 if (bit_size
> bsize
) {
2901 tcc_error("width of '%s' exceeds its type",
2902 get_tok_str(v
, NULL
));
2903 } else if (bit_size
== bsize
) {
2904 /* no need for bit fields */
2906 } else if (bit_size
== 0) {
2907 /* XXX: what to do if only padding in a
2909 /* zero size: means to pad */
2912 /* we do not have enough room ?
2913 did the type change?
2915 if ((bit_pos
+ bit_size
) > bsize
||
2916 bt
!= prevbt
|| a
== TOK_UNION
)
2919 /* XXX: handle LSB first */
2920 type1
.t
|= VT_BITFIELD
|
2921 (bit_pos
<< VT_STRUCT_SHIFT
) |
2922 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
2923 bit_pos
+= bit_size
;
2929 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2930 /* add new memory data only if starting
2932 if (lbit_pos
== 0) {
2933 if (a
== TOK_STRUCT
) {
2934 c
= (c
+ align
- 1) & -align
;
2943 if (align
> maxalign
)
2947 printf("add field %s offset=%d",
2948 get_tok_str(v
, NULL
), offset
);
2949 if (type1
.t
& VT_BITFIELD
) {
2950 printf(" pos=%d size=%d",
2951 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
2952 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
2957 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2959 while ((ass
= ass
->next
) != NULL
) {
2960 ss
= sym_push(ass
->v
, &ass
->type
, 0, offset
+ ass
->c
);
2965 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
2969 if (tok
== ';' || tok
== TOK_EOF
)
2976 /* store size and alignment */
2977 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
2983 /* return 0 if no type declaration. otherwise, return the basic type
2986 static int parse_btype(CType
*type
, AttributeDef
*ad
)
2988 int t
, u
, type_found
, typespec_found
, typedef_found
;
2992 memset(ad
, 0, sizeof(AttributeDef
));
3000 /* currently, we really ignore extension */
3010 if ((t
& VT_BTYPE
) != 0)
3011 tcc_error("too many basic types");
3027 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
3028 #ifndef TCC_TARGET_PE
3029 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3031 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
3032 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3046 if ((t
& VT_BTYPE
) == VT_LONG
) {
3047 #ifdef TCC_TARGET_PE
3048 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
3050 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3058 struct_decl(&type1
, VT_ENUM
, t
& VT_TYPEDEF
);
3061 type
->ref
= type1
.ref
;
3065 struct_decl(&type1
, VT_STRUCT
, t
& VT_TYPEDEF
);
3068 /* type modifiers */
3121 /* GNUC attribute */
3122 case TOK_ATTRIBUTE1
:
3123 case TOK_ATTRIBUTE2
:
3124 parse_attribute(ad
);
3127 t
= (t
& ~VT_BTYPE
) | u
;
3135 parse_expr_type(&type1
);
3136 /* remove all storage modifiers except typedef */
3137 type1
.t
&= ~(VT_STORAGE
&~VT_TYPEDEF
);
3140 if (typespec_found
|| typedef_found
)
3143 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
3146 t
|= (s
->type
.t
& ~VT_TYPEDEF
);
3147 type
->ref
= s
->type
.ref
;
3149 /* get attributes from typedef */
3150 if (0 == ad
->a
.aligned
)
3151 ad
->a
.aligned
= s
->a
.aligned
;
3152 if (0 == ad
->a
.func_call
)
3153 ad
->a
.func_call
= s
->a
.func_call
;
3154 ad
->a
.packed
|= s
->a
.packed
;
3163 if ((t
& (VT_SIGNED
|VT_UNSIGNED
)) == (VT_SIGNED
|VT_UNSIGNED
))
3164 tcc_error("signed and unsigned modifier");
3165 if (tcc_state
->char_is_unsigned
) {
3166 if ((t
& (VT_SIGNED
|VT_UNSIGNED
|VT_BTYPE
)) == VT_BYTE
)
3171 /* long is never used as type */
3172 if ((t
& VT_BTYPE
) == VT_LONG
)
3173 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
3174 t
= (t
& ~VT_BTYPE
) | VT_INT
;
3176 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3182 /* convert a function parameter type (array to pointer and function to
3183 function pointer) */
3184 static inline void convert_parameter_type(CType
*pt
)
3186 /* remove const and volatile qualifiers (XXX: const could be used
3187 to indicate a const function parameter */
3188 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3189 /* array must be transformed to pointer according to ANSI C */
3191 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
3196 ST_FUNC
void parse_asm_str(CString
*astr
)
3199 /* read the string */
3201 expect("string constant");
3203 while (tok
== TOK_STR
) {
3204 /* XXX: add \0 handling too ? */
3205 cstr_cat(astr
, tokc
.cstr
->data
);
3208 cstr_ccat(astr
, '\0');
3211 /* Parse an asm label and return the label
3212 * Don't forget to free the CString in the caller! */
3213 static void asm_label_instr(CString
*astr
)
3216 parse_asm_str(astr
);
3219 printf("asm_alias: \"%s\"\n", (char *)astr
->data
);
3223 static void post_type(CType
*type
, AttributeDef
*ad
)
3225 int n
, l
, t1
, arg_size
, align
;
3226 Sym
**plast
, *s
, *first
;
3231 /* function declaration */
3239 /* read param name and compute offset */
3240 if (l
!= FUNC_OLD
) {
3241 if (!parse_btype(&pt
, &ad1
)) {
3243 tcc_error("invalid type");
3250 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
3252 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3253 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
3254 tcc_error("parameter declared as void");
3255 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
3260 expect("identifier");
3264 convert_parameter_type(&pt
);
3265 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
3271 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
3278 /* if no parameters, then old type prototype */
3282 /* NOTE: const is ignored in returned type as it has a special
3283 meaning in gcc / C++ */
3284 type
->t
&= ~VT_CONSTANT
;
3285 /* some ancient pre-K&R C allows a function to return an array
3286 and the array brackets to be put after the arguments, such
3287 that "int c()[]" means something like "int[] c()" */
3290 skip(']'); /* only handle simple "[]" */
3293 /* we push a anonymous symbol which will contain the function prototype */
3294 ad
->a
.func_args
= arg_size
;
3295 s
= sym_push(SYM_FIELD
, type
, 0, l
);
3300 } else if (tok
== '[') {
3301 /* array definition */
3303 if (tok
== TOK_RESTRICT1
)
3308 if (!local_stack
|| nocode_wanted
)
3309 vpushi(expr_const());
3311 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3314 tcc_error("invalid array size");
3316 if (!is_integer_btype(vtop
->type
.t
& VT_BTYPE
))
3317 tcc_error("size of variable length array should be an integer");
3322 /* parse next post type */
3323 post_type(type
, ad
);
3324 if (type
->t
== VT_FUNC
)
3325 tcc_error("declaration of an array of functions");
3326 t1
|= type
->t
& VT_VLA
;
3329 loc
-= type_size(&int_type
, &align
);
3333 vla_runtime_type_size(type
, &align
);
3335 vset(&int_type
, VT_LOCAL
|VT_LVAL
, loc
);
3342 /* we push an anonymous symbol which will contain the array
3344 s
= sym_push(SYM_FIELD
, type
, 0, n
);
3345 type
->t
= (t1
? VT_VLA
: VT_ARRAY
) | VT_PTR
;
3350 /* Parse a type declaration (except basic type), and return the type
3351 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3352 expected. 'type' should contain the basic type. 'ad' is the
3353 attribute definition of the basic type. It can be modified by
3356 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
3359 CType type1
, *type2
;
3360 int qualifiers
, storage
;
3362 while (tok
== '*') {
3370 qualifiers
|= VT_CONSTANT
;
3375 qualifiers
|= VT_VOLATILE
;
3383 type
->t
|= qualifiers
;
3386 /* XXX: clarify attribute handling */
3387 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3388 parse_attribute(ad
);
3390 /* recursive type */
3391 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3392 type1
.t
= 0; /* XXX: same as int */
3395 /* XXX: this is not correct to modify 'ad' at this point, but
3396 the syntax is not clear */
3397 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3398 parse_attribute(ad
);
3399 type_decl(&type1
, ad
, v
, td
);
3402 /* type identifier */
3403 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
3407 if (!(td
& TYPE_ABSTRACT
))
3408 expect("identifier");
3412 storage
= type
->t
& VT_STORAGE
;
3413 type
->t
&= ~VT_STORAGE
;
3414 if (storage
& VT_STATIC
) {
3415 int saved_nocode_wanted
= nocode_wanted
;
3417 post_type(type
, ad
);
3418 nocode_wanted
= saved_nocode_wanted
;
3420 post_type(type
, ad
);
3422 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3423 parse_attribute(ad
);
3427 /* append type at the end of type1 */
3440 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3441 ST_FUNC
int lvalue_type(int t
)
3446 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
3448 else if (bt
== VT_SHORT
)
3452 if (t
& VT_UNSIGNED
)
3453 r
|= VT_LVAL_UNSIGNED
;
3457 /* indirection with full error checking and bound check */
3458 ST_FUNC
void indir(void)
3460 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
3461 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
3465 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
3467 vtop
->type
= *pointed_type(&vtop
->type
);
3468 /* Arrays and functions are never lvalues */
3469 if (!(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_VLA
)
3470 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3471 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3472 /* if bound checking, the referenced pointer must be checked */
3473 #ifdef CONFIG_TCC_BCHECK
3474 if (tcc_state
->do_bounds_check
)
3475 vtop
->r
|= VT_MUSTBOUND
;
3480 /* pass a parameter to a function and do type checking and casting */
3481 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
3486 func_type
= func
->c
;
3487 if (func_type
== FUNC_OLD
||
3488 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
3489 /* default casting : only need to convert float to double */
3490 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
3494 } else if (arg
== NULL
) {
3495 tcc_error("too many arguments to function");
3498 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
3499 gen_assign_cast(&type
);
3503 /* parse an expression of the form '(type)' or '(expr)' and return its
3505 static void parse_expr_type(CType
*type
)
3511 if (parse_btype(type
, &ad
)) {
3512 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3519 static void parse_type(CType
*type
)
3524 if (!parse_btype(type
, &ad
)) {
3527 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3530 static void vpush_tokc(int t
)
3535 vsetc(&type
, VT_CONST
, &tokc
);
3538 ST_FUNC
void unary(void)
3540 int n
, t
, align
, size
, r
, sizeof_caller
;
3544 static int in_sizeof
= 0;
3546 sizeof_caller
= in_sizeof
;
3548 /* XXX: GCC 2.95.3 does not generate a table although it should be
3562 vpush_tokc(VT_INT
| VT_UNSIGNED
);
3566 vpush_tokc(VT_LLONG
);
3570 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
3574 vpush_tokc(VT_FLOAT
);
3578 vpush_tokc(VT_DOUBLE
);
3582 vpush_tokc(VT_LDOUBLE
);
3585 case TOK___FUNCTION__
:
3587 goto tok_identifier
;
3593 /* special function name identifier */
3594 len
= strlen(funcname
) + 1;
3595 /* generate char[len] type */
3600 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
3601 ptr
= section_ptr_add(data_section
, len
);
3602 memcpy(ptr
, funcname
, len
);
3607 #ifdef TCC_TARGET_PE
3608 t
= VT_SHORT
| VT_UNSIGNED
;
3614 /* string parsing */
3617 if (tcc_state
->warn_write_strings
)
3622 memset(&ad
, 0, sizeof(AttributeDef
));
3623 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, NULL
, 0);
3628 if (parse_btype(&type
, &ad
)) {
3629 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
3631 /* check ISOC99 compound literal */
3633 /* data is allocated locally by default */
3638 /* all except arrays are lvalues */
3639 if (!(type
.t
& VT_ARRAY
))
3640 r
|= lvalue_type(type
.t
);
3641 memset(&ad
, 0, sizeof(AttributeDef
));
3642 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, NULL
, 0);
3644 if (sizeof_caller
) {
3651 } else if (tok
== '{') {
3652 /* save all registers */
3654 /* statement expression : we do not accept break/continue
3655 inside as GCC does */
3656 block(NULL
, NULL
, NULL
, NULL
, 0, 1);
3671 /* functions names must be treated as function pointers,
3672 except for unary '&' and sizeof. Since we consider that
3673 functions are not lvalues, we only have to handle it
3674 there and in function calls. */
3675 /* arrays can also be used although they are not lvalues */
3676 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
3677 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
3679 mk_pointer(&vtop
->type
);
3685 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3687 boolean
.t
= VT_BOOL
;
3689 vtop
->c
.i
= !vtop
->c
.i
;
3690 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
3691 vtop
->c
.i
= vtop
->c
.i
^ 1;
3694 vseti(VT_JMP
, gvtst(1, 0));
3706 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
3707 tcc_error("pointer not accepted for unary plus");
3708 /* In order to force cast, we add zero, except for floating point
3709 where we really need an noop (otherwise -0.0 will be transformed
3711 if (!is_float(vtop
->type
.t
)) {
3722 unary_type(&type
); // Perform a in_sizeof = 0;
3723 size
= type_size(&type
, &align
);
3724 if (t
== TOK_SIZEOF
) {
3725 if (!(type
.t
& VT_VLA
)) {
3727 tcc_error("sizeof applied to an incomplete type");
3730 vla_runtime_type_size(&type
, &align
);
3735 vtop
->type
.t
|= VT_UNSIGNED
;
3738 case TOK_builtin_types_compatible_p
:
3747 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3748 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3749 vpushi(is_compatible_types(&type1
, &type2
));
3752 case TOK_builtin_constant_p
:
3754 int saved_nocode_wanted
, res
;
3757 saved_nocode_wanted
= nocode_wanted
;
3760 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
3762 nocode_wanted
= saved_nocode_wanted
;
3767 case TOK_builtin_frame_address
:
3773 if (tok
!= TOK_CINT
|| tokc
.i
< 0) {
3774 tcc_error("__builtin_frame_address only takes positive integers");
3781 vset(&type
, VT_LOCAL
, 0); /* local frame */
3783 mk_pointer(&vtop
->type
);
3784 indir(); /* -> parent frame */
3788 #ifdef TCC_TARGET_X86_64
3789 #ifdef TCC_TARGET_PE
3790 case TOK_builtin_va_start
:
3798 if ((vtop
->r
& VT_VALMASK
) != VT_LOCAL
)
3799 tcc_error("__builtin_va_start expects a local variable");
3800 vtop
->r
&= ~(VT_LVAL
| VT_REF
);
3801 vtop
->type
= char_pointer_type
;
3806 case TOK_builtin_va_arg_types
:
3813 vpushi(classify_x86_64_va_arg(&type
));
3828 t
= vtop
->type
.t
& VT_BTYPE
;
3830 /* In IEEE negate(x) isn't subtract(0,x), but rather
3835 else if (t
== VT_DOUBLE
)
3846 goto tok_identifier
;
3848 /* allow to take the address of a label */
3849 if (tok
< TOK_UIDENT
)
3850 expect("label identifier");
3851 s
= label_find(tok
);
3853 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
3855 if (s
->r
== LABEL_DECLARED
)
3856 s
->r
= LABEL_FORWARD
;
3859 s
->type
.t
= VT_VOID
;
3860 mk_pointer(&s
->type
);
3861 s
->type
.t
|= VT_STATIC
;
3863 vset(&s
->type
, VT_CONST
| VT_SYM
, 0);
3868 // special qnan , snan and infinity values
3870 vpush64(VT_DOUBLE
, 0x7ff8000000000000ULL
);
3874 vpush64(VT_DOUBLE
, 0x7ff0000000000001ULL
);
3878 vpush64(VT_DOUBLE
, 0x7ff0000000000000ULL
);
3887 expect("identifier");
3891 tcc_error("'%s' undeclared", get_tok_str(t
, NULL
));
3892 /* for simple function calls, we tolerate undeclared
3893 external reference to int() function */
3894 if (tcc_state
->warn_implicit_function_declaration
)
3895 tcc_warning("implicit declaration of function '%s'",
3896 get_tok_str(t
, NULL
));
3897 s
= external_global_sym(t
, &func_old_type
, 0);
3899 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
3900 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
3901 /* if referencing an inline function, then we generate a
3902 symbol to it if not already done. It will have the
3903 effect to generate code for it at the end of the
3904 compilation unit. Inline function as always
3905 generated in the text section. */
3907 put_extern_sym(s
, text_section
, 0, 0);
3908 r
= VT_SYM
| VT_CONST
;
3912 vset(&s
->type
, r
, s
->c
);
3913 /* if forward reference, we must point to s */
3914 if (vtop
->r
& VT_SYM
) {
3921 /* post operations */
3923 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
3926 } else if (tok
== '.' || tok
== TOK_ARROW
) {
3929 if (tok
== TOK_ARROW
)
3931 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
3935 /* expect pointer on structure */
3936 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
3937 expect("struct or union");
3941 while ((s
= s
->next
) != NULL
) {
3946 tcc_error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, NULL
));
3947 /* add field offset to pointer */
3948 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
3951 /* change type to field type, and set to lvalue */
3952 vtop
->type
= s
->type
;
3953 vtop
->type
.t
|= qualifiers
;
3954 /* an array is never an lvalue */
3955 if (!(vtop
->type
.t
& VT_ARRAY
)) {
3956 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3957 #ifdef CONFIG_TCC_BCHECK
3958 /* if bound checking, the referenced pointer must be checked */
3959 if (tcc_state
->do_bounds_check
)
3960 vtop
->r
|= VT_MUSTBOUND
;
3964 } else if (tok
== '[') {
3970 } else if (tok
== '(') {
3973 int nb_args
, ret_nregs
, ret_align
, variadic
;
3976 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3977 /* pointer test (no array accepted) */
3978 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
3979 vtop
->type
= *pointed_type(&vtop
->type
);
3980 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
3984 expect("function pointer");
3987 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
3989 /* get return type */
3992 sa
= s
->next
; /* first parameter */
3995 /* compute first implicit argument if a structure is returned */
3996 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
3997 variadic
= (s
->c
== FUNC_ELLIPSIS
);
3998 ret_nregs
= gfunc_sret(&s
->type
, variadic
, &ret
.type
,
4001 /* get some space for the returned structure */
4002 size
= type_size(&s
->type
, &align
);
4003 loc
= (loc
- size
) & -align
;
4005 ret
.r
= VT_LOCAL
| VT_LVAL
;
4006 /* pass it as 'int' to avoid structure arg passing
4008 vseti(VT_LOCAL
, loc
);
4018 /* return in register */
4019 if (is_float(ret
.type
.t
)) {
4020 ret
.r
= reg_fret(ret
.type
.t
);
4021 #ifdef TCC_TARGET_X86_64
4022 if ((ret
.type
.t
& VT_BTYPE
) == VT_QFLOAT
)
4026 #ifdef TCC_TARGET_X86_64
4027 if ((ret
.type
.t
& VT_BTYPE
) == VT_QLONG
)
4029 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
4039 gfunc_param_typed(s
, sa
);
4049 tcc_error("too few arguments to function");
4051 if (!nocode_wanted
) {
4052 gfunc_call(nb_args
);
4054 vtop
-= (nb_args
+ 1);
4058 for (r
= ret
.r
+ ret_nregs
+ !ret_nregs
; r
-- > ret
.r
;) {
4059 vsetc(&ret
.type
, r
, &ret
.c
);
4060 vtop
->r2
= ret
.r2
; /* Loop only happens when r2 is VT_CONST */
4063 /* handle packed struct return */
4064 if (((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) && ret_nregs
) {
4067 size
= type_size(&s
->type
, &align
);
4068 loc
= (loc
- size
) & -align
;
4072 vset(&ret
.type
, VT_LOCAL
| VT_LVAL
, addr
+ offset
);
4076 if (--ret_nregs
== 0)
4078 /* XXX: compatible with arm only: ret_align == register_size */
4079 offset
+= ret_align
;
4081 vset(&s
->type
, VT_LOCAL
| VT_LVAL
, addr
);
4089 ST_FUNC
void expr_prod(void)
4094 while (tok
== '*' || tok
== '/' || tok
== '%') {
4102 ST_FUNC
void expr_sum(void)
4107 while (tok
== '+' || tok
== '-') {
4115 static void expr_shift(void)
4120 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
4128 static void expr_cmp(void)
4133 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
4134 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
4142 static void expr_cmpeq(void)
4147 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
4155 static void expr_and(void)
4158 while (tok
== '&') {
4165 static void expr_xor(void)
4168 while (tok
== '^') {
4175 static void expr_or(void)
4178 while (tok
== '|') {
4185 /* XXX: fix this mess */
4186 static void expr_land_const(void)
4189 while (tok
== TOK_LAND
) {
4196 /* XXX: fix this mess */
4197 static void expr_lor_const(void)
4200 while (tok
== TOK_LOR
) {
4207 /* only used if non constant */
4208 static void expr_land(void)
4213 if (tok
== TOK_LAND
) {
4218 if (tok
!= TOK_LAND
) {
4228 static void expr_lor(void)
4233 if (tok
== TOK_LOR
) {
4238 if (tok
!= TOK_LOR
) {
4248 /* XXX: better constant handling */
4249 static void expr_cond(void)
4251 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
;
4253 CType type
, type1
, type2
;
4260 boolean
.t
= VT_BOOL
;
4266 if (tok
!= ':' || !gnu_ext
) {
4281 if (vtop
!= vstack
) {
4282 /* needed to avoid having different registers saved in
4284 if (is_float(vtop
->type
.t
)) {
4286 #ifdef TCC_TARGET_X86_64
4287 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4297 if (tok
== ':' && gnu_ext
) {
4305 sv
= *vtop
; /* save value to handle it later */
4306 vtop
--; /* no vpop so that FP stack is not flushed */
4314 bt1
= t1
& VT_BTYPE
;
4316 bt2
= t2
& VT_BTYPE
;
4317 /* cast operands to correct type according to ISOC rules */
4318 if (is_float(bt1
) || is_float(bt2
)) {
4319 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
4320 type
.t
= VT_LDOUBLE
;
4321 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
4326 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
4327 /* cast to biggest op */
4329 /* convert to unsigned if it does not fit in a long long */
4330 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
4331 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
4332 type
.t
|= VT_UNSIGNED
;
4333 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
4334 /* If one is a null ptr constant the result type
4336 if (is_null_pointer (vtop
))
4338 else if (is_null_pointer (&sv
))
4340 /* XXX: test pointer compatibility, C99 has more elaborate
4344 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
4345 /* XXX: test function pointer compatibility */
4346 type
= bt1
== VT_FUNC
? type1
: type2
;
4347 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
4348 /* XXX: test structure compatibility */
4349 type
= bt1
== VT_STRUCT
? type1
: type2
;
4350 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
4351 /* NOTE: as an extension, we accept void on only one side */
4354 /* integer operations */
4356 /* convert to unsigned if it does not fit in an integer */
4357 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
4358 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
4359 type
.t
|= VT_UNSIGNED
;
4362 /* now we convert second operand */
4364 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4367 if (is_float(type
.t
)) {
4369 #ifdef TCC_TARGET_X86_64
4370 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4374 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
4375 /* for long longs, we use fixed registers to avoid having
4376 to handle a complicated move */
4381 /* this is horrible, but we must also convert first
4385 /* put again first value and cast it */
4388 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4391 move_reg(r2
, r1
, type
.t
);
4398 static void expr_eq(void)
4404 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
4405 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
4406 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
4421 ST_FUNC
void gexpr(void)
4432 /* parse an expression and return its type without any side effect. */
4433 static void expr_type(CType
*type
)
4435 int saved_nocode_wanted
;
4437 saved_nocode_wanted
= nocode_wanted
;
4442 nocode_wanted
= saved_nocode_wanted
;
4445 /* parse a unary expression and return its type without any side
4447 static void unary_type(CType
*type
)
4459 /* parse a constant expression and return value in vtop. */
4460 static void expr_const1(void)
4469 /* parse an integer constant and return its value. */
4470 ST_FUNC
int expr_const(void)
4474 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
4475 expect("constant expression");
4481 /* return the label token if current token is a label, otherwise
4483 static int is_label(void)
4487 /* fast test first */
4488 if (tok
< TOK_UIDENT
)
4490 /* no need to save tokc because tok is an identifier */
4497 unget_tok(last_tok
);
4502 static void label_or_decl(int l
)
4506 /* fast test first */
4507 if (tok
>= TOK_UIDENT
)
4509 /* no need to save tokc because tok is an identifier */
4513 unget_tok(last_tok
);
4516 unget_tok(last_tok
);
4521 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
4522 int case_reg
, int is_expr
)
4525 Sym
*s
, *frame_bottom
;
4527 /* generate line number info */
4528 if (tcc_state
->do_debug
&&
4529 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
4530 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
4532 last_line_num
= file
->line_num
;
4536 /* default return value is (void) */
4538 vtop
->type
.t
= VT_VOID
;
4541 if (tok
== TOK_IF
) {
4548 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4550 if (c
== TOK_ELSE
) {
4554 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4555 gsym(d
); /* patch else jmp */
4558 } else if (tok
== TOK_WHILE
) {
4566 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4570 } else if (tok
== '{') {
4572 int block_vla_sp_loc
, *saved_vla_sp_loc
, saved_vla_flags
;
4575 /* record local declaration stack position */
4577 frame_bottom
= sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4578 frame_bottom
->next
= scope_stack_bottom
;
4579 scope_stack_bottom
= frame_bottom
;
4580 llabel
= local_label_stack
;
4582 /* save VLA state */
4583 block_vla_sp_loc
= *(saved_vla_sp_loc
= vla_sp_loc
);
4584 if (saved_vla_sp_loc
!= &vla_sp_root_loc
)
4585 vla_sp_loc
= &block_vla_sp_loc
;
4587 saved_vla_flags
= vla_flags
;
4588 vla_flags
|= VLA_NEED_NEW_FRAME
;
4590 /* handle local labels declarations */
4591 if (tok
== TOK_LABEL
) {
4594 if (tok
< TOK_UIDENT
)
4595 expect("label identifier");
4596 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
4606 while (tok
!= '}') {
4607 label_or_decl(VT_LOCAL
);
4611 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4614 /* pop locally defined labels */
4615 label_pop(&local_label_stack
, llabel
);
4617 /* XXX: this solution makes only valgrind happy...
4618 triggered by gcc.c-torture/execute/20000917-1.c */
4620 switch(vtop
->type
.t
& VT_BTYPE
) {
4625 for(p
=vtop
->type
.ref
;p
;p
=p
->prev
)
4627 tcc_error("unsupported expression type");
4630 /* pop locally defined symbols */
4631 scope_stack_bottom
= scope_stack_bottom
->next
;
4632 sym_pop(&local_stack
, s
);
4634 /* Pop VLA frames and restore stack pointer if required */
4635 if (saved_vla_sp_loc
!= &vla_sp_root_loc
)
4636 *saved_vla_sp_loc
= block_vla_sp_loc
;
4637 if (vla_sp_loc
!= (saved_vla_sp_loc
== &vla_sp_root_loc
? &vla_sp_root_loc
: &block_vla_sp_loc
)) {
4638 vla_sp_loc
= saved_vla_sp_loc
;
4639 gen_vla_sp_restore(*vla_sp_loc
);
4641 vla_flags
= (vla_flags
& ~VLA_SCOPE_FLAGS
) | (saved_vla_flags
& VLA_SCOPE_FLAGS
);
4644 } else if (tok
== TOK_RETURN
) {
4648 gen_assign_cast(&func_vt
);
4649 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
4650 CType type
, ret_type
;
4651 int ret_align
, ret_nregs
;
4652 ret_nregs
= gfunc_sret(&func_vt
, func_var
, &ret_type
,
4654 if (0 == ret_nregs
) {
4655 /* if returning structure, must copy it to implicit
4656 first pointer arg location */
4659 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
4662 /* copy structure value to pointer */
4665 /* returning structure packed into registers */
4666 int r
, size
, addr
, align
;
4667 size
= type_size(&func_vt
,&align
);
4668 if ((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) || (vtop
->c
.i
& (ret_align
-1)))
4669 && (align
& (ret_align
-1))) {
4670 loc
= (loc
- size
) & -align
;
4673 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
4676 vset(&ret_type
, VT_LOCAL
| VT_LVAL
, addr
);
4678 vtop
->type
= ret_type
;
4679 if (is_float(ret_type
.t
))
4680 r
= rc_fret(ret_type
.t
);
4686 if (--ret_nregs
== 0)
4688 /* We assume that when a structure is returned in multiple
4689 registers, their classes are consecutive values of the
4692 /* XXX: compatible with arm only: ret_align == register_size */
4693 vtop
->c
.i
+= ret_align
;
4694 vtop
->r
= VT_LOCAL
| VT_LVAL
;
4697 } else if (is_float(func_vt
.t
)) {
4698 gv(rc_fret(func_vt
.t
));
4702 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
4705 rsym
= gjmp(rsym
); /* jmp */
4706 } else if (tok
== TOK_BREAK
) {
4709 tcc_error("cannot break");
4710 *bsym
= gjmp(*bsym
);
4713 } else if (tok
== TOK_CONTINUE
) {
4716 tcc_error("cannot continue");
4717 *csym
= gjmp(*csym
);
4720 } else if (tok
== TOK_FOR
) {
4725 frame_bottom
= sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4726 frame_bottom
->next
= scope_stack_bottom
;
4727 scope_stack_bottom
= frame_bottom
;
4729 /* c99 for-loop init decl? */
4730 if (!decl0(VT_LOCAL
, 1)) {
4731 /* no, regular for-loop init expr */
4755 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4759 scope_stack_bottom
= scope_stack_bottom
->next
;
4760 sym_pop(&local_stack
, s
);
4762 if (tok
== TOK_DO
) {
4767 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4778 if (tok
== TOK_SWITCH
) {
4782 /* XXX: other types than integer */
4783 case_reg
= gv(RC_INT
);
4787 b
= gjmp(0); /* jump to first case */
4789 block(&a
, csym
, &b
, &c
, case_reg
, 0);
4790 /* if no default, jmp after switch */
4798 if (tok
== TOK_CASE
) {
4805 if (gnu_ext
&& tok
== TOK_DOTS
) {
4809 tcc_warning("empty case range");
4811 /* since a case is like a label, we must skip it with a jmp */
4818 *case_sym
= gtst(1, 0);
4821 *case_sym
= gtst(1, 0);
4825 *case_sym
= gtst(1, *case_sym
);
4830 goto block_after_label
;
4832 if (tok
== TOK_DEFAULT
) {
4838 tcc_error("too many 'default'");
4841 goto block_after_label
;
4843 if (tok
== TOK_GOTO
) {
4845 if (tok
== '*' && gnu_ext
) {
4849 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
4852 } else if (tok
>= TOK_UIDENT
) {
4853 s
= label_find(tok
);
4854 /* put forward definition if needed */
4856 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4858 if (s
->r
== LABEL_DECLARED
)
4859 s
->r
= LABEL_FORWARD
;
4861 /* label already defined */
4862 if (vla_flags
& VLA_IN_SCOPE
) {
4863 /* If VLAs are in use, save the current stack pointer and
4864 reset the stack pointer to what it was at function entry
4865 (label will restore stack pointer in inner scopes) */
4867 gen_vla_sp_restore(vla_sp_root_loc
);
4869 if (s
->r
& LABEL_FORWARD
)
4870 s
->jnext
= gjmp(s
->jnext
);
4872 gjmp_addr(s
->jnext
);
4875 expect("label identifier");
4878 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
4884 if (vla_flags
& VLA_IN_SCOPE
) {
4885 /* save/restore stack pointer across label
4886 this is a no-op when combined with the load immediately
4887 after the label unless we arrive via goto */
4892 if (s
->r
== LABEL_DEFINED
)
4893 tcc_error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
4895 s
->r
= LABEL_DEFINED
;
4897 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
4900 if (vla_flags
& VLA_IN_SCOPE
) {
4901 gen_vla_sp_restore(*vla_sp_loc
);
4902 vla_flags
|= VLA_NEED_NEW_FRAME
;
4904 /* we accept this, but it is a mistake */
4907 tcc_warning("deprecated use of label at end of compound statement");
4911 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4914 /* expression case */
4929 /* t is the array or struct type. c is the array or struct
4930 address. cur_index/cur_field is the pointer to the current
4931 value. 'size_only' is true if only size info is needed (only used
4933 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
4934 int *cur_index
, Sym
**cur_field
,
4938 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
4944 if (gnu_ext
&& (l
= is_label()) != 0)
4946 while (tok
== '[' || tok
== '.') {
4948 if (!(type
->t
& VT_ARRAY
))
4949 expect("array type");
4952 index
= expr_const();
4953 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
4954 expect("invalid index");
4955 if (tok
== TOK_DOTS
&& gnu_ext
) {
4957 index_last
= expr_const();
4958 if (index_last
< 0 ||
4959 (s
->c
>= 0 && index_last
>= s
->c
) ||
4961 expect("invalid index");
4967 *cur_index
= index_last
;
4968 type
= pointed_type(type
);
4969 elem_size
= type_size(type
, &align
);
4970 c
+= index
* elem_size
;
4971 /* NOTE: we only support ranges for last designator */
4972 nb_elems
= index_last
- index
+ 1;
4973 if (nb_elems
!= 1) {
4982 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
4983 expect("struct/union type");
4996 /* XXX: fix this mess by using explicit storage field */
4998 type1
.t
|= (type
->t
& ~VT_TYPE
);
5012 if (type
->t
& VT_ARRAY
) {
5014 type
= pointed_type(type
);
5015 c
+= index
* type_size(type
, &align
);
5019 tcc_error("too many field init");
5020 /* XXX: fix this mess by using explicit storage field */
5022 type1
.t
|= (type
->t
& ~VT_TYPE
);
5027 decl_initializer(type
, sec
, c
, 0, size_only
);
5029 /* XXX: make it more general */
5030 if (!size_only
&& nb_elems
> 1) {
5031 unsigned long c_end
;
5036 tcc_error("range init not supported yet for dynamic storage");
5037 c_end
= c
+ nb_elems
* elem_size
;
5038 if (c_end
> sec
->data_allocated
)
5039 section_realloc(sec
, c_end
);
5040 src
= sec
->data
+ c
;
5042 for(i
= 1; i
< nb_elems
; i
++) {
5044 memcpy(dst
, src
, elem_size
);
5050 #define EXPR_CONST 1
5053 /* store a value or an expression directly in global data or in local array */
5054 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
5055 int v
, int expr_type
)
5057 int saved_global_expr
, bt
, bit_pos
, bit_size
;
5059 unsigned long long bit_mask
;
5067 /* compound literals must be allocated globally in this case */
5068 saved_global_expr
= global_expr
;
5071 global_expr
= saved_global_expr
;
5072 /* NOTE: symbols are accepted */
5073 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
5074 tcc_error("initializer element is not constant");
5082 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
5085 /* XXX: not portable */
5086 /* XXX: generate error if incorrect relocation */
5087 gen_assign_cast(&dtype
);
5088 bt
= type
->t
& VT_BTYPE
;
5089 /* we'll write at most 12 bytes */
5090 if (c
+ 12 > sec
->data_allocated
) {
5091 section_realloc(sec
, c
+ 12);
5093 ptr
= sec
->data
+ c
;
5094 /* XXX: make code faster ? */
5095 if (!(type
->t
& VT_BITFIELD
)) {
5100 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5101 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
5102 bit_mask
= (1LL << bit_size
) - 1;
5104 if ((vtop
->r
& VT_SYM
) &&
5110 (bt
== VT_INT
&& bit_size
!= 32)))
5111 tcc_error("initializer element is not computable at load time");
5114 vtop
->c
.i
= (vtop
->c
.i
!= 0);
5116 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5119 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5122 *(double *)ptr
= vtop
->c
.d
;
5125 *(long double *)ptr
= vtop
->c
.ld
;
5128 *(long long *)ptr
|= (vtop
->c
.ll
& bit_mask
) << bit_pos
;
5131 if (vtop
->r
& VT_SYM
) {
5132 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
5134 *(addr_t
*)ptr
|= (vtop
->c
.ull
& bit_mask
) << bit_pos
;
5137 if (vtop
->r
& VT_SYM
) {
5138 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
5140 *(int *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5145 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
5152 /* put zeros for variable based init */
5153 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
5156 /* nothing to do because globals are already set to zero */
5158 vpush_global_sym(&func_old_type
, TOK_memset
);
5160 #ifdef TCC_TARGET_ARM
5171 /* 't' contains the type and storage info. 'c' is the offset of the
5172 object in section 'sec'. If 'sec' is NULL, it means stack based
5173 allocation. 'first' is true if array '{' must be read (multi
5174 dimension implicit array init handling). 'size_only' is true if
5175 size only evaluation is wanted (only for arrays). */
5176 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
5177 int first
, int size_only
)
5179 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, parlevel1
, i
;
5180 int size1
, align1
, expr_type
;
5184 if (type
->t
& VT_VLA
) {
5187 /* save current stack pointer */
5188 if (vla_flags
& VLA_NEED_NEW_FRAME
) {
5190 vla_flags
= VLA_IN_SCOPE
;
5191 vla_sp_loc
= &vla_sp_loc_tmp
;
5194 vla_runtime_type_size(type
, &a
);
5195 gen_vla_alloc(type
, a
);
5196 vset(type
, VT_LOCAL
|VT_LVAL
, c
);
5200 } else if (type
->t
& VT_ARRAY
) {
5204 t1
= pointed_type(type
);
5205 size1
= type_size(t1
, &align1
);
5208 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
5211 tcc_error("character array initializer must be a literal,"
5212 " optionally enclosed in braces");
5217 /* only parse strings here if correct type (otherwise: handle
5218 them as ((w)char *) expressions */
5219 if ((tok
== TOK_LSTR
&&
5220 #ifdef TCC_TARGET_PE
5221 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
5223 (t1
->t
& VT_BTYPE
) == VT_INT
5225 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
5226 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5231 /* compute maximum number of chars wanted */
5233 cstr_len
= cstr
->size
;
5235 cstr_len
= cstr
->size
/ sizeof(nwchar_t
);
5238 if (n
>= 0 && nb
> (n
- array_length
))
5239 nb
= n
- array_length
;
5242 tcc_warning("initializer-string for array is too long");
5243 /* in order to go faster for common case (char
5244 string in global variable, we handle it
5246 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
5247 memcpy(sec
->data
+ c
+ array_length
, cstr
->data
, nb
);
5251 ch
= ((unsigned char *)cstr
->data
)[i
];
5253 ch
= ((nwchar_t
*)cstr
->data
)[i
];
5254 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
5262 /* only add trailing zero if enough storage (no
5263 warning in this case since it is standard) */
5264 if (n
< 0 || array_length
< n
) {
5266 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
5272 while (tok
!= '}') {
5273 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
5274 if (n
>= 0 && index
>= n
)
5275 tcc_error("index too large");
5276 /* must put zero in holes (note that doing it that way
5277 ensures that it even works with designators) */
5278 if (!size_only
&& array_length
< index
) {
5279 init_putz(t1
, sec
, c
+ array_length
* size1
,
5280 (index
- array_length
) * size1
);
5283 if (index
> array_length
)
5284 array_length
= index
;
5285 /* special test for multi dimensional arrays (may not
5286 be strictly correct if designators are used at the
5288 if (index
>= n
&& no_oblock
)
5297 /* put zeros at the end */
5298 if (!size_only
&& n
>= 0 && array_length
< n
) {
5299 init_putz(t1
, sec
, c
+ array_length
* size1
,
5300 (n
- array_length
) * size1
);
5302 /* patch type size if needed */
5304 s
->c
= array_length
;
5305 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
5306 (sec
|| !first
|| tok
== '{')) {
5309 /* NOTE: the previous test is a specific case for automatic
5310 struct/union init */
5311 /* XXX: union needs only one init */
5313 /* XXX: this test is incorrect for local initializers
5314 beginning with ( without {. It would be much more difficult
5315 to do it correctly (ideally, the expression parser should
5316 be used in all cases) */
5322 while (tok
== '(') {
5326 if (!parse_btype(&type1
, &ad1
))
5328 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
5330 if (!is_assignable_types(type
, &type1
))
5331 tcc_error("invalid type for cast");
5336 if (first
|| tok
== '{') {
5345 while (tok
!= '}') {
5346 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
5348 if (!size_only
&& array_length
< index
) {
5349 init_putz(type
, sec
, c
+ array_length
,
5350 index
- array_length
);
5352 index
= index
+ type_size(&f
->type
, &align1
);
5353 if (index
> array_length
)
5354 array_length
= index
;
5356 /* gr: skip fields from same union - ugly. */
5358 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
5359 /* test for same offset */
5360 if (f
->next
->c
!= f
->c
)
5362 /* if yes, test for bitfield shift */
5363 if ((f
->type
.t
& VT_BITFIELD
) && (f
->next
->type
.t
& VT_BITFIELD
)) {
5364 int bit_pos_1
= (f
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5365 int bit_pos_2
= (f
->next
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5366 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
5367 if (bit_pos_1
!= bit_pos_2
)
5374 if (no_oblock
&& f
== NULL
)
5380 /* put zeros at the end */
5381 if (!size_only
&& array_length
< n
) {
5382 init_putz(type
, sec
, c
+ array_length
,
5391 } else if (tok
== '{') {
5393 decl_initializer(type
, sec
, c
, first
, size_only
);
5395 } else if (size_only
) {
5396 /* just skip expression */
5397 parlevel
= parlevel1
= 0;
5398 while ((parlevel
> 0 || parlevel1
> 0 ||
5399 (tok
!= '}' && tok
!= ',')) && tok
!= -1) {
5402 else if (tok
== ')')
5404 else if (tok
== '{')
5406 else if (tok
== '}')
5411 /* currently, we always use constant expression for globals
5412 (may change for scripting case) */
5413 expr_type
= EXPR_CONST
;
5415 expr_type
= EXPR_ANY
;
5416 init_putv(type
, sec
, c
, 0, expr_type
);
5420 /* parse an initializer for type 't' if 'has_init' is non zero, and
5421 allocate space in local or global data space ('r' is either
5422 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5423 variable 'v' with an associated name represented by 'asm_label' of
5424 scope 'scope' is declared before initializers are parsed. If 'v' is
5425 zero, then a reference to the new object is put in the value stack.
5426 If 'has_init' is 2, a special parsing is done to handle string
5428 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
5429 int has_init
, int v
, char *asm_label
,
5432 int size
, align
, addr
, data_offset
;
5434 ParseState saved_parse_state
= {0};
5435 TokenString init_str
;
5437 Sym
*flexible_array
;
5439 flexible_array
= NULL
;
5440 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
5441 Sym
*field
= type
->ref
->next
;
5444 field
= field
->next
;
5445 if (field
->type
.t
& VT_ARRAY
&& field
->type
.ref
->c
< 0)
5446 flexible_array
= field
;
5450 size
= type_size(type
, &align
);
5451 /* If unknown size, we must evaluate it before
5452 evaluating initializers because
5453 initializers can generate global data too
5454 (e.g. string pointers or ISOC99 compound
5455 literals). It also simplifies local
5456 initializers handling */
5457 tok_str_new(&init_str
);
5458 if (size
< 0 || (flexible_array
&& has_init
)) {
5460 tcc_error("unknown type size");
5461 /* get all init string */
5462 if (has_init
== 2) {
5463 /* only get strings */
5464 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5465 tok_str_add_tok(&init_str
);
5470 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
5472 tcc_error("unexpected end of file in initializer");
5473 tok_str_add_tok(&init_str
);
5476 else if (tok
== '}') {
5486 tok_str_add(&init_str
, -1);
5487 tok_str_add(&init_str
, 0);
5490 save_parse_state(&saved_parse_state
);
5492 macro_ptr
= init_str
.str
;
5494 decl_initializer(type
, NULL
, 0, 1, 1);
5495 /* prepare second initializer parsing */
5496 macro_ptr
= init_str
.str
;
5499 /* if still unknown size, error */
5500 size
= type_size(type
, &align
);
5502 tcc_error("unknown type size");
5505 size
+= flexible_array
->type
.ref
->c
* pointed_size(&flexible_array
->type
);
5506 /* take into account specified alignment if bigger */
5507 if (ad
->a
.aligned
) {
5508 if (ad
->a
.aligned
> align
)
5509 align
= ad
->a
.aligned
;
5510 } else if (ad
->a
.packed
) {
5513 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
5515 #ifdef CONFIG_TCC_BCHECK
5516 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5520 loc
= (loc
- size
) & -align
;
5522 #ifdef CONFIG_TCC_BCHECK
5523 /* handles bounds */
5524 /* XXX: currently, since we do only one pass, we cannot track
5525 '&' operators, so we add only arrays */
5526 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5527 unsigned long *bounds_ptr
;
5528 /* add padding between regions */
5530 /* then add local bound info */
5531 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(unsigned long));
5532 bounds_ptr
[0] = addr
;
5533 bounds_ptr
[1] = size
;
5537 /* local variable */
5538 sym_push(v
, type
, r
, addr
);
5540 /* push local reference */
5541 vset(type
, r
, addr
);
5547 if (v
&& scope
== VT_CONST
) {
5548 /* see if the symbol was already defined */
5551 if (!is_compatible_types(&sym
->type
, type
))
5552 tcc_error("incompatible types for redefinition of '%s'",
5553 get_tok_str(v
, NULL
));
5554 if (sym
->type
.t
& VT_EXTERN
) {
5555 /* if the variable is extern, it was not allocated */
5556 sym
->type
.t
&= ~VT_EXTERN
;
5557 /* set array size if it was ommited in extern
5559 if ((sym
->type
.t
& VT_ARRAY
) &&
5560 sym
->type
.ref
->c
< 0 &&
5562 sym
->type
.ref
->c
= type
->ref
->c
;
5564 /* we accept several definitions of the same
5565 global variable. this is tricky, because we
5566 must play with the SHN_COMMON type of the symbol */
5567 /* XXX: should check if the variable was already
5568 initialized. It is incorrect to initialized it
5570 /* no init data, we won't add more to the symbol */
5577 /* allocate symbol in corresponding section */
5582 else if (tcc_state
->nocommon
)
5586 data_offset
= sec
->data_offset
;
5587 data_offset
= (data_offset
+ align
- 1) & -align
;
5589 /* very important to increment global pointer at this time
5590 because initializers themselves can create new initializers */
5591 data_offset
+= size
;
5592 #ifdef CONFIG_TCC_BCHECK
5593 /* add padding if bound check */
5594 if (tcc_state
->do_bounds_check
)
5597 sec
->data_offset
= data_offset
;
5598 /* allocate section space to put the data */
5599 if (sec
->sh_type
!= SHT_NOBITS
&&
5600 data_offset
> sec
->data_allocated
)
5601 section_realloc(sec
, data_offset
);
5602 /* align section if needed */
5603 if (align
> sec
->sh_addralign
)
5604 sec
->sh_addralign
= align
;
5606 addr
= 0; /* avoid warning */
5610 if (scope
!= VT_CONST
|| !sym
) {
5611 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
5612 sym
->asm_label
= asm_label
;
5614 /* update symbol definition */
5616 put_extern_sym(sym
, sec
, addr
, size
);
5619 /* put a common area */
5620 put_extern_sym(sym
, NULL
, align
, size
);
5621 /* XXX: find a nicer way */
5622 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
5623 esym
->st_shndx
= SHN_COMMON
;
5626 /* push global reference */
5627 sym
= get_sym_ref(type
, sec
, addr
, size
);
5628 vpushsym(type
, sym
);
5630 /* patch symbol weakness */
5631 if (type
->t
& VT_WEAK
)
5633 #ifdef CONFIG_TCC_BCHECK
5634 /* handles bounds now because the symbol must be defined
5635 before for the relocation */
5636 if (tcc_state
->do_bounds_check
) {
5637 unsigned long *bounds_ptr
;
5639 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
5640 /* then add global bound info */
5641 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(long));
5642 bounds_ptr
[0] = 0; /* relocated */
5643 bounds_ptr
[1] = size
;
5647 if (has_init
|| (type
->t
& VT_VLA
)) {
5648 decl_initializer(type
, sec
, addr
, 1, 0);
5649 /* restore parse state if needed */
5651 tok_str_free(init_str
.str
);
5652 restore_parse_state(&saved_parse_state
);
5654 /* patch flexible array member size back to -1, */
5655 /* for possible subsequent similar declarations */
5657 flexible_array
->type
.ref
->c
= -1;
5662 static void put_func_debug(Sym
*sym
)
5667 /* XXX: we put here a dummy type */
5668 snprintf(buf
, sizeof(buf
), "%s:%c1",
5669 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
5670 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
5671 cur_text_section
, sym
->c
);
5672 /* //gr gdb wants a line at the function */
5673 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
5678 /* parse an old style function declaration list */
5679 /* XXX: check multiple parameter */
5680 static void func_decl_list(Sym
*func_sym
)
5687 /* parse each declaration */
5688 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
&&
5689 tok
!= TOK_ASM1
&& tok
!= TOK_ASM2
&& tok
!= TOK_ASM3
) {
5690 if (!parse_btype(&btype
, &ad
))
5691 expect("declaration list");
5692 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5693 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5695 /* we accept no variable after */
5699 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5700 /* find parameter in function parameter list */
5703 if ((s
->v
& ~SYM_FIELD
) == v
)
5707 tcc_error("declaration for parameter '%s' but no such parameter",
5708 get_tok_str(v
, NULL
));
5710 /* check that no storage specifier except 'register' was given */
5711 if (type
.t
& VT_STORAGE
)
5712 tcc_error("storage class specified for '%s'", get_tok_str(v
, NULL
));
5713 convert_parameter_type(&type
);
5714 /* we can add the type (NOTE: it could be local to the function) */
5716 /* accept other parameters */
5727 /* parse a function defined by symbol 'sym' and generate its code in
5728 'cur_text_section' */
5729 static void gen_function(Sym
*sym
)
5731 int saved_nocode_wanted
= nocode_wanted
;
5733 ind
= cur_text_section
->data_offset
;
5734 /* NOTE: we patch the symbol size later */
5735 put_extern_sym(sym
, cur_text_section
, ind
, 0);
5736 funcname
= get_tok_str(sym
->v
, NULL
);
5738 /* Initialize VLA state */
5739 vla_sp_loc
= &vla_sp_root_loc
;
5740 vla_flags
= VLA_NEED_NEW_FRAME
;
5741 /* put debug symbol */
5742 if (tcc_state
->do_debug
)
5743 put_func_debug(sym
);
5744 /* push a dummy symbol to enable local sym storage */
5745 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
5746 gfunc_prolog(&sym
->type
);
5748 block(NULL
, NULL
, NULL
, NULL
, 0, 0);
5751 cur_text_section
->data_offset
= ind
;
5752 label_pop(&global_label_stack
, NULL
);
5753 /* reset local stack */
5754 scope_stack_bottom
= NULL
;
5755 sym_pop(&local_stack
, NULL
);
5756 /* end of function */
5757 /* patch symbol size */
5758 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
5760 /* patch symbol weakness (this definition overrules any prototype) */
5761 if (sym
->type
.t
& VT_WEAK
)
5763 if (tcc_state
->do_debug
) {
5764 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
5766 /* It's better to crash than to generate wrong code */
5767 cur_text_section
= NULL
;
5768 funcname
= ""; /* for safety */
5769 func_vt
.t
= VT_VOID
; /* for safety */
5770 func_var
= 0; /* for safety */
5771 ind
= 0; /* for safety */
5772 nocode_wanted
= saved_nocode_wanted
;
5775 ST_FUNC
void gen_inline_functions(void)
5778 int *str
, inline_generated
, i
;
5779 struct InlineFunc
*fn
;
5781 /* iterate while inline function are referenced */
5783 inline_generated
= 0;
5784 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5785 fn
= tcc_state
->inline_fns
[i
];
5787 if (sym
&& sym
->c
) {
5788 /* the function was used: generate its code and
5789 convert it to a normal function */
5790 str
= fn
->token_str
;
5793 pstrcpy(file
->filename
, sizeof file
->filename
, fn
->filename
);
5794 sym
->r
= VT_SYM
| VT_CONST
;
5795 sym
->type
.t
&= ~VT_INLINE
;
5799 cur_text_section
= text_section
;
5801 macro_ptr
= NULL
; /* fail safe */
5803 inline_generated
= 1;
5806 if (!inline_generated
)
5809 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5810 fn
= tcc_state
->inline_fns
[i
];
5811 str
= fn
->token_str
;
5814 dynarray_reset(&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
);
5817 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
5818 static int decl0(int l
, int is_for_loop_init
)
5826 if (!parse_btype(&btype
, &ad
)) {
5827 if (is_for_loop_init
)
5829 /* skip redundant ';' */
5830 /* XXX: find more elegant solution */
5835 if (l
== VT_CONST
&&
5836 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5837 /* global asm block */
5841 /* special test for old K&R protos without explicit int
5842 type. Only accepted when defining global data */
5843 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
5847 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5848 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5850 /* we accept no variable after */
5854 while (1) { /* iterate thru each declaration */
5855 char *asm_label
; // associated asm label
5857 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5861 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
5862 printf("type = '%s'\n", buf
);
5865 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5866 if ((type
.t
& VT_STATIC
) && (l
== VT_LOCAL
)) {
5867 tcc_error("function without file scope cannot be static");
5869 /* if old style function prototype, we accept a
5872 if (sym
->c
== FUNC_OLD
)
5873 func_decl_list(sym
);
5877 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5880 asm_label_instr(&astr
);
5881 asm_label
= tcc_strdup(astr
.data
);
5884 /* parse one last attribute list, after asm label */
5885 parse_attribute(&ad
);
5890 #ifdef TCC_TARGET_PE
5891 if (ad
.a
.func_import
)
5892 type
.t
|= VT_IMPORT
;
5893 if (ad
.a
.func_export
)
5894 type
.t
|= VT_EXPORT
;
5898 tcc_error("cannot use local functions");
5899 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
5900 expect("function definition");
5902 /* reject abstract declarators in function definition */
5904 while ((sym
= sym
->next
) != NULL
)
5905 if (!(sym
->v
& ~SYM_FIELD
))
5906 expect("identifier");
5908 /* XXX: cannot do better now: convert extern line to static inline */
5909 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
5910 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5915 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
5918 ref
= sym
->type
.ref
;
5919 if (0 == ref
->a
.func_proto
)
5920 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
5922 /* use func_call from prototype if not defined */
5923 if (ref
->a
.func_call
!= FUNC_CDECL
5924 && type
.ref
->a
.func_call
== FUNC_CDECL
)
5925 type
.ref
->a
.func_call
= ref
->a
.func_call
;
5927 /* use export from prototype */
5928 if (ref
->a
.func_export
)
5929 type
.ref
->a
.func_export
= 1;
5931 /* use static from prototype */
5932 if (sym
->type
.t
& VT_STATIC
)
5933 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5935 if (!is_compatible_types(&sym
->type
, &type
)) {
5937 tcc_error("incompatible types for redefinition of '%s'",
5938 get_tok_str(v
, NULL
));
5940 type
.ref
->a
.func_proto
= 0;
5941 /* if symbol is already defined, then put complete type */
5944 /* put function symbol */
5945 sym
= global_identifier_push(v
, type
.t
, 0);
5946 sym
->type
.ref
= type
.ref
;
5949 /* static inline functions are just recorded as a kind
5950 of macro. Their code will be emitted at the end of
5951 the compilation unit only if they are used */
5952 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
5953 (VT_INLINE
| VT_STATIC
)) {
5954 TokenString func_str
;
5956 struct InlineFunc
*fn
;
5957 const char *filename
;
5959 tok_str_new(&func_str
);
5965 tcc_error("unexpected end of file");
5966 tok_str_add_tok(&func_str
);
5971 } else if (t
== '}') {
5973 if (block_level
== 0)
5977 tok_str_add(&func_str
, -1);
5978 tok_str_add(&func_str
, 0);
5979 filename
= file
? file
->filename
: "";
5980 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
5981 strcpy(fn
->filename
, filename
);
5983 fn
->token_str
= func_str
.str
;
5984 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
5987 /* compute text section */
5988 cur_text_section
= ad
.section
;
5989 if (!cur_text_section
)
5990 cur_text_section
= text_section
;
5991 sym
->r
= VT_SYM
| VT_CONST
;
5996 if (btype
.t
& VT_TYPEDEF
) {
5997 /* save typedefed type */
5998 /* XXX: test storage specifiers ? */
5999 sym
= sym_push(v
, &type
, 0, 0);
6001 sym
->type
.t
|= VT_TYPEDEF
;
6004 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
6005 /* external function definition */
6006 /* specific case for func_call attribute */
6007 ad
.a
.func_proto
= 1;
6009 } else if (!(type
.t
& VT_ARRAY
)) {
6010 /* not lvalue if array */
6011 r
|= lvalue_type(type
.t
);
6013 has_init
= (tok
== '=');
6014 if (has_init
&& (type
.t
& VT_VLA
))
6015 tcc_error("Variable length array cannot be initialized");
6016 if ((btype
.t
& VT_EXTERN
) || ((type
.t
& VT_BTYPE
) == VT_FUNC
) ||
6017 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
6018 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
6019 /* external variable or function */
6020 /* NOTE: as GCC, uninitialized global static
6021 arrays of null size are considered as
6023 sym
= external_sym(v
, &type
, r
, asm_label
);
6025 if (type
.t
& VT_WEAK
)
6028 if (ad
.alias_target
) {
6033 alias_target
= sym_find(ad
.alias_target
);
6034 if (!alias_target
|| !alias_target
->c
)
6035 tcc_error("unsupported forward __alias__ attribute");
6036 esym
= &((Elf32_Sym
*)symtab_section
->data
)[alias_target
->c
];
6037 tsec
.sh_num
= esym
->st_shndx
;
6038 put_extern_sym2(sym
, &tsec
, esym
->st_value
, esym
->st_size
, 0);
6041 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
6042 if (type
.t
& VT_STATIC
)
6048 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, asm_label
, l
);
6052 if (is_for_loop_init
)
6065 ST_FUNC
void decl(int l
)