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) */
70 ST_DATA
int last_line_num
, last_ind
, func_ind
; /* debug last line number and pc */
71 ST_DATA
char *funcname
;
73 ST_DATA CType char_pointer_type
, func_old_type
, int_type
, size_type
;
75 /* ------------------------------------------------------------------------- */
76 static void gen_cast(CType
*type
);
77 static inline CType
*pointed_type(CType
*type
);
78 static int is_compatible_types(CType
*type1
, CType
*type2
);
79 static int parse_btype(CType
*type
, AttributeDef
*ad
);
80 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
);
81 static void parse_expr_type(CType
*type
);
82 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
, int first
, int size_only
);
83 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
, int case_reg
, int is_expr
);
84 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
, int has_init
, int v
, char *asm_label
, int scope
);
85 static int decl0(int l
, int is_for_loop_init
);
86 static void expr_eq(void);
87 static void unary_type(CType
*type
);
88 static void vla_runtime_type_size(CType
*type
, int *a
);
89 static void vla_sp_save(void);
90 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
);
91 static void expr_type(CType
*type
);
93 ST_INLN
int is_float(int t
)
97 return bt
== VT_LDOUBLE
|| bt
== VT_DOUBLE
|| bt
== VT_FLOAT
|| bt
== VT_QFLOAT
;
100 /* we use our own 'finite' function to avoid potential problems with
101 non standard math libs */
102 /* XXX: endianness dependent */
103 ST_FUNC
int ieee_finite(double d
)
106 return ((unsigned)((p
[1] | 0x800fffff) + 1)) >> 31;
109 ST_FUNC
void test_lvalue(void)
111 if (!(vtop
->r
& VT_LVAL
))
115 /* ------------------------------------------------------------------------- */
116 /* symbol allocator */
117 static Sym
*__sym_malloc(void)
119 Sym
*sym_pool
, *sym
, *last_sym
;
122 sym_pool
= tcc_malloc(SYM_POOL_NB
* sizeof(Sym
));
123 dynarray_add(&sym_pools
, &nb_sym_pools
, sym_pool
);
125 last_sym
= sym_free_first
;
127 for(i
= 0; i
< SYM_POOL_NB
; i
++) {
128 sym
->next
= last_sym
;
132 sym_free_first
= last_sym
;
136 static inline Sym
*sym_malloc(void)
139 sym
= sym_free_first
;
141 sym
= __sym_malloc();
142 sym_free_first
= sym
->next
;
146 ST_INLN
void sym_free(Sym
*sym
)
148 sym
->next
= sym_free_first
;
149 tcc_free(sym
->asm_label
);
150 sym_free_first
= sym
;
153 /* push, without hashing */
154 ST_FUNC Sym
*sym_push2(Sym
**ps
, int v
, int t
, long c
)
157 if (ps
== &local_stack
) {
158 for (s
= *ps
; s
&& s
!= scope_stack_bottom
; s
= s
->prev
)
159 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
&& s
->v
== v
)
160 tcc_error("incompatible types for redefinition of '%s'",
161 get_tok_str(v
, NULL
));
180 /* find a symbol and return its associated structure. 's' is the top
181 of the symbol stack */
182 ST_FUNC Sym
*sym_find2(Sym
*s
, int v
)
192 /* structure lookup */
193 ST_INLN Sym
*struct_find(int v
)
196 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
198 return table_ident
[v
]->sym_struct
;
201 /* find an identifier */
202 ST_INLN Sym
*sym_find(int v
)
205 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
207 return table_ident
[v
]->sym_identifier
;
210 /* push a given symbol on the symbol stack */
211 ST_FUNC Sym
*sym_push(int v
, CType
*type
, int r
, int c
)
220 s
= sym_push2(ps
, v
, type
->t
, c
);
221 s
->type
.ref
= type
->ref
;
223 /* don't record fields or anonymous symbols */
225 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
226 /* record symbol in token array */
227 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
229 ps
= &ts
->sym_struct
;
231 ps
= &ts
->sym_identifier
;
238 /* push a global identifier */
239 ST_FUNC Sym
*global_identifier_push(int v
, int t
, int c
)
242 s
= sym_push2(&global_stack
, v
, t
, c
);
243 /* don't record anonymous symbol */
244 if (v
< SYM_FIRST_ANOM
) {
245 ps
= &table_ident
[v
- TOK_IDENT
]->sym_identifier
;
246 /* modify the top most local identifier, so that
247 sym_identifier will point to 's' when popped */
249 ps
= &(*ps
)->prev_tok
;
256 /* pop symbols until top reaches 'b' */
257 ST_FUNC
void sym_pop(Sym
**ptop
, Sym
*b
)
267 /* remove symbol in token array */
269 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
270 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
272 ps
= &ts
->sym_struct
;
274 ps
= &ts
->sym_identifier
;
283 static void weaken_symbol(Sym
*sym
)
285 sym
->type
.t
|= VT_WEAK
;
290 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
291 esym_type
= ELFW(ST_TYPE
)(esym
->st_info
);
292 esym
->st_info
= ELFW(ST_INFO
)(STB_WEAK
, esym_type
);
296 /* ------------------------------------------------------------------------- */
298 ST_FUNC
void swap(int *p
, int *q
)
306 static void vsetc(CType
*type
, int r
, CValue
*vc
)
310 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
311 tcc_error("memory full");
312 /* cannot let cpu flags if other instruction are generated. Also
313 avoid leaving VT_JMP anywhere except on the top of the stack
314 because it would complicate the code generator. */
315 if (vtop
>= vstack
) {
316 v
= vtop
->r
& VT_VALMASK
;
317 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
327 /* push constant of type "type" with useless value */
328 void vpush(CType
*type
)
331 vsetc(type
, VT_CONST
, &cval
);
334 /* push integer constant */
335 ST_FUNC
void vpushi(int v
)
339 vsetc(&int_type
, VT_CONST
, &cval
);
342 /* push a pointer sized constant */
343 static void vpushs(long long v
)
350 vsetc(&size_type
, VT_CONST
, &cval
);
353 /* push arbitrary 64bit constant */
354 void vpush64(int ty
, unsigned long long v
)
361 vsetc(&ctype
, VT_CONST
, &cval
);
364 /* push long long constant */
365 static inline void vpushll(long long v
)
367 vpush64(VT_LLONG
, v
);
370 /* Return a static symbol pointing to a section */
371 ST_FUNC Sym
*get_sym_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
377 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
378 sym
->type
.ref
= type
->ref
;
379 sym
->r
= VT_CONST
| VT_SYM
;
380 put_extern_sym(sym
, sec
, offset
, size
);
384 /* push a reference to a section offset by adding a dummy symbol */
385 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
390 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
391 vtop
->sym
= get_sym_ref(type
, sec
, offset
, size
);
394 /* define a new external reference to a symbol 'v' of type 'u' */
395 ST_FUNC Sym
*external_global_sym(int v
, CType
*type
, int r
)
401 /* push forward reference */
402 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
403 s
->type
.ref
= type
->ref
;
404 s
->r
= r
| VT_CONST
| VT_SYM
;
409 /* define a new external reference to a symbol 'v' with alternate asm
410 name 'asm_label' of type 'u'. 'asm_label' is equal to NULL if there
411 is no alternate name (most cases) */
412 static Sym
*external_sym(int v
, CType
*type
, int r
, char *asm_label
)
418 /* push forward reference */
419 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
420 s
->asm_label
= asm_label
;
421 s
->type
.t
|= VT_EXTERN
;
422 } else if (s
->type
.ref
== func_old_type
.ref
) {
423 s
->type
.ref
= type
->ref
;
424 s
->r
= r
| VT_CONST
| VT_SYM
;
425 s
->type
.t
|= VT_EXTERN
;
426 } else if (!is_compatible_types(&s
->type
, type
)) {
427 tcc_error("incompatible types for redefinition of '%s'",
428 get_tok_str(v
, NULL
));
433 /* push a reference to global symbol v */
434 ST_FUNC
void vpush_global_sym(CType
*type
, int v
)
439 sym
= external_global_sym(v
, type
, 0);
441 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
445 ST_FUNC
void vset(CType
*type
, int r
, int v
)
450 vsetc(type
, r
, &cval
);
453 static void vseti(int r
, int v
)
461 ST_FUNC
void vswap(void)
464 /* cannot let cpu flags if other instruction are generated. Also
465 avoid leaving VT_JMP anywhere except on the top of the stack
466 because it would complicate the code generator. */
467 if (vtop
>= vstack
) {
468 int v
= vtop
->r
& VT_VALMASK
;
469 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
476 /* XXX: +2% overall speed possible with optimized memswap
478 * memswap(&vtop[0], &vtop[1], sizeof *vtop);
482 ST_FUNC
void vpushv(SValue
*v
)
484 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
485 tcc_error("memory full");
490 static void vdup(void)
495 /* save r to the memory stack, and mark it as being free */
496 ST_FUNC
void save_reg(int r
)
498 int l
, saved
, size
, align
;
502 /* modify all stack values */
505 for(p
=vstack
;p
<=vtop
;p
++) {
506 if ((p
->r
& VT_VALMASK
) == r
||
507 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
508 /* must save value on stack if not already done */
510 /* NOTE: must reload 'r' because r might be equal to r2 */
511 r
= p
->r
& VT_VALMASK
;
512 /* store register in the stack */
514 if ((p
->r
& VT_LVAL
) ||
515 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
516 #ifdef TCC_TARGET_X86_64
517 type
= &char_pointer_type
;
521 size
= type_size(type
, &align
);
522 loc
= (loc
- size
) & -align
;
524 sv
.r
= VT_LOCAL
| VT_LVAL
;
527 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
528 /* x86 specific: need to pop fp register ST0 if saved */
530 o(0xd8dd); /* fstp %st(0) */
533 #ifndef TCC_TARGET_X86_64
534 /* special long long case */
535 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
543 /* mark that stack entry as being saved on the stack */
544 if (p
->r
& VT_LVAL
) {
545 /* also clear the bounded flag because the
546 relocation address of the function was stored in
548 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
550 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
558 #ifdef TCC_TARGET_ARM
559 /* find a register of class 'rc2' with at most one reference on stack.
560 * If none, call get_reg(rc) */
561 ST_FUNC
int get_reg_ex(int rc
, int rc2
)
566 for(r
=0;r
<NB_REGS
;r
++) {
567 if (reg_classes
[r
] & rc2
) {
570 for(p
= vstack
; p
<= vtop
; p
++) {
571 if ((p
->r
& VT_VALMASK
) == r
||
572 (p
->r2
& VT_VALMASK
) == r
)
583 /* find a free register of class 'rc'. If none, save one register */
584 ST_FUNC
int get_reg(int rc
)
589 /* find a free register */
590 for(r
=0;r
<NB_REGS
;r
++) {
591 if (reg_classes
[r
] & rc
) {
592 for(p
=vstack
;p
<=vtop
;p
++) {
593 if ((p
->r
& VT_VALMASK
) == r
||
594 (p
->r2
& VT_VALMASK
) == r
)
602 /* no register left : free the first one on the stack (VERY
603 IMPORTANT to start from the bottom to ensure that we don't
604 spill registers used in gen_opi()) */
605 for(p
=vstack
;p
<=vtop
;p
++) {
606 /* look at second register (if long long) */
607 r
= p
->r2
& VT_VALMASK
;
608 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
610 r
= p
->r
& VT_VALMASK
;
611 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
617 /* Should never comes here */
621 /* save registers up to (vtop - n) stack entry */
622 ST_FUNC
void save_regs(int n
)
627 for(p
= vstack
;p
<= p1
; p
++) {
628 r
= p
->r
& VT_VALMASK
;
635 /* move register 's' (of type 't') to 'r', and flush previous value of r to memory
637 static void move_reg(int r
, int s
, int t
)
651 /* get address of vtop (vtop MUST BE an lvalue) */
652 static void gaddrof(void)
654 if (vtop
->r
& VT_REF
)
657 /* tricky: if saved lvalue, then we can go back to lvalue */
658 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
659 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
664 #ifdef CONFIG_TCC_BCHECK
665 /* generate lvalue bound code */
666 static void gbound(void)
671 vtop
->r
&= ~VT_MUSTBOUND
;
672 /* if lvalue, then use checking code before dereferencing */
673 if (vtop
->r
& VT_LVAL
) {
674 /* if not VT_BOUNDED value, then make one */
675 if (!(vtop
->r
& VT_BOUNDED
)) {
676 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
677 /* must save type because we must set it to int to get pointer */
679 vtop
->type
.t
= VT_INT
;
682 gen_bounded_ptr_add();
683 vtop
->r
|= lval_type
;
686 /* then check for dereferencing */
687 gen_bounded_ptr_deref();
692 /* store vtop a register belonging to class 'rc'. lvalues are
693 converted to values. Cannot be used if cannot be converted to
694 register value (such as structures). */
695 ST_FUNC
int gv(int rc
)
697 int r
, bit_pos
, bit_size
, size
, align
, i
;
700 /* NOTE: get_reg can modify vstack[] */
701 if (vtop
->type
.t
& VT_BITFIELD
) {
704 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
705 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
706 /* remove bit field info to avoid loops */
707 vtop
->type
.t
&= ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
708 /* cast to int to propagate signedness in following ops */
709 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
714 if((vtop
->type
.t
& VT_UNSIGNED
) ||
715 (vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
716 type
.t
|= VT_UNSIGNED
;
718 /* generate shifts */
719 vpushi(bits
- (bit_pos
+ bit_size
));
721 vpushi(bits
- bit_size
);
722 /* NOTE: transformed to SHR if unsigned */
726 if (is_float(vtop
->type
.t
) &&
727 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
730 unsigned long offset
;
731 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
735 /* XXX: unify with initializers handling ? */
736 /* CPUs usually cannot use float constants, so we store them
737 generically in data segment */
738 size
= type_size(&vtop
->type
, &align
);
739 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
740 data_section
->data_offset
= offset
;
741 /* XXX: not portable yet */
742 #if defined(__i386__) || defined(__x86_64__)
743 /* Zero pad x87 tenbyte long doubles */
744 if (size
== LDOUBLE_SIZE
) {
745 vtop
->c
.tab
[2] &= 0xffff;
746 #if LDOUBLE_SIZE == 16
751 ptr
= section_ptr_add(data_section
, size
);
753 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
757 ptr
[i
] = vtop
->c
.tab
[size
-1-i
];
761 ptr
[i
] = vtop
->c
.tab
[i
];
762 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
763 vtop
->r
|= VT_LVAL
| VT_SYM
;
767 #ifdef CONFIG_TCC_BCHECK
768 if (vtop
->r
& VT_MUSTBOUND
)
772 r
= vtop
->r
& VT_VALMASK
;
773 rc2
= (rc
& RC_FLOAT
) ? RC_FLOAT
: RC_INT
;
776 #ifdef TCC_TARGET_X86_64
777 else if (rc
== RC_FRET
)
781 /* need to reload if:
783 - lvalue (need to dereference pointer)
784 - already a register, but not in the right class */
786 || (vtop
->r
& VT_LVAL
)
787 || !(reg_classes
[r
] & rc
)
788 #ifdef TCC_TARGET_X86_64
789 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
790 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
&& !(reg_classes
[vtop
->r2
] & rc2
))
792 || ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
797 #ifdef TCC_TARGET_X86_64
798 if (((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
)) {
799 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
801 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
802 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
803 unsigned long long ll
;
805 int r2
, original_type
;
806 original_type
= vtop
->type
.t
;
807 /* two register type load : expand to two words
809 #ifndef TCC_TARGET_X86_64
810 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
813 vtop
->c
.ui
= ll
; /* first word */
815 vtop
->r
= r
; /* save register value */
816 vpushi(ll
>> 32); /* second word */
819 if (r
>= VT_CONST
|| /* XXX: test to VT_CONST incorrect ? */
820 (vtop
->r
& VT_LVAL
)) {
821 /* We do not want to modifier the long long
822 pointer here, so the safest (and less
823 efficient) is to save all the other registers
824 in the stack. XXX: totally inefficient. */
826 /* load from memory */
827 vtop
->type
.t
= load_type
;
830 vtop
[-1].r
= r
; /* save register value */
831 /* increment pointer to get second word */
832 vtop
->type
.t
= addr_type
;
837 vtop
->type
.t
= load_type
;
842 vtop
[-1].r
= r
; /* save register value */
843 vtop
->r
= vtop
[-1].r2
;
845 /* Allocate second register. Here we rely on the fact that
846 get_reg() tries first to free r2 of an SValue. */
850 /* write second register */
852 vtop
->type
.t
= original_type
;
853 } else if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
855 /* lvalue of scalar type : need to use lvalue type
856 because of possible cast */
859 /* compute memory access type */
860 if (vtop
->r
& VT_REF
)
861 #ifdef TCC_TARGET_X86_64
866 else if (vtop
->r
& VT_LVAL_BYTE
)
868 else if (vtop
->r
& VT_LVAL_SHORT
)
870 if (vtop
->r
& VT_LVAL_UNSIGNED
)
874 /* restore wanted type */
877 /* one register type load */
882 #ifdef TCC_TARGET_C67
883 /* uses register pairs for doubles */
884 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
891 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
892 ST_FUNC
void gv2(int rc1
, int rc2
)
896 /* generate more generic register first. But VT_JMP or VT_CMP
897 values must be generated first in all cases to avoid possible
899 v
= vtop
[0].r
& VT_VALMASK
;
900 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
905 /* test if reload is needed for first register */
906 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
916 /* test if reload is needed for first register */
917 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
923 /* wrapper around RC_FRET to return a register by type */
924 static int rc_fret(int t
)
926 #ifdef TCC_TARGET_X86_64
927 if (t
== VT_LDOUBLE
) {
934 /* wrapper around REG_FRET to return a register by type */
935 static int reg_fret(int t
)
937 #ifdef TCC_TARGET_X86_64
938 if (t
== VT_LDOUBLE
) {
945 /* expand long long on stack in two int registers */
946 static void lexpand(void)
950 u
= vtop
->type
.t
& VT_UNSIGNED
;
953 vtop
[0].r
= vtop
[-1].r2
;
954 vtop
[0].r2
= VT_CONST
;
955 vtop
[-1].r2
= VT_CONST
;
956 vtop
[0].type
.t
= VT_INT
| u
;
957 vtop
[-1].type
.t
= VT_INT
| u
;
960 #ifdef TCC_TARGET_ARM
961 /* expand long long on stack */
962 ST_FUNC
void lexpand_nr(void)
966 u
= vtop
->type
.t
& VT_UNSIGNED
;
969 vtop
->type
.t
= VT_INT
| u
;
970 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
972 vtop
[-1].c
.ui
= vtop
->c
.ull
;
973 vtop
->c
.ui
= vtop
->c
.ull
>> 32;
975 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
977 vtop
->r
= vtop
[-1].r
;
978 } else if (v
> VT_CONST
) {
982 vtop
->r
= vtop
[-1].r2
;
983 vtop
[-1].r2
= VT_CONST
;
984 vtop
[-1].type
.t
= VT_INT
| u
;
988 /* build a long long from two ints */
989 static void lbuild(int t
)
992 vtop
[-1].r2
= vtop
[0].r
;
997 /* rotate n first stack elements to the bottom
998 I1 ... In -> I2 ... In I1 [top is right]
1000 ST_FUNC
void vrotb(int n
)
1006 for(i
=-n
+1;i
!=0;i
++)
1007 vtop
[i
] = vtop
[i
+1];
1011 /* rotate the n elements before entry e towards the top
1012 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
1014 ST_FUNC
void vrote(SValue
*e
, int n
)
1020 for(i
= 0;i
< n
- 1; i
++)
1025 /* rotate n first stack elements to the top
1026 I1 ... In -> In I1 ... I(n-1) [top is right]
1028 ST_FUNC
void vrott(int n
)
1033 /* pop stack value */
1034 ST_FUNC
void vpop(void)
1037 v
= vtop
->r
& VT_VALMASK
;
1038 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1039 /* for x86, we need to pop the FP stack */
1040 if (v
== TREG_ST0
&& !nocode_wanted
) {
1041 o(0xd8dd); /* fstp %st(0) */
1044 if (v
== VT_JMP
|| v
== VT_JMPI
) {
1045 /* need to put correct jump if && or || without test */
1051 /* convert stack entry to register and duplicate its value in another
1053 static void gv_dup(void)
1059 if ((t
& VT_BTYPE
) == VT_LLONG
) {
1066 /* stack: H L L1 H1 */
1074 /* duplicate value */
1079 #ifdef TCC_TARGET_X86_64
1080 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1090 load(r1
, &sv
); /* move r to r1 */
1092 /* duplicates value */
1098 #ifndef TCC_TARGET_X86_64
1099 /* generate CPU independent (unsigned) long long operations */
1100 static void gen_opl(int op
)
1102 int t
, a
, b
, op1
, c
, i
;
1104 unsigned short reg_iret
= REG_IRET
;
1105 unsigned short reg_lret
= REG_LRET
;
1111 func
= TOK___divdi3
;
1114 func
= TOK___udivdi3
;
1117 func
= TOK___moddi3
;
1120 func
= TOK___umoddi3
;
1127 /* call generic long long function */
1128 vpush_global_sym(&func_old_type
, func
);
1133 vtop
->r2
= reg_lret
;
1146 /* stack: L1 H1 L2 H2 */
1151 vtop
[-2] = vtop
[-3];
1154 /* stack: H1 H2 L1 L2 */
1160 /* stack: H1 H2 L1 L2 ML MH */
1163 /* stack: ML MH H1 H2 L1 L2 */
1167 /* stack: ML MH H1 L2 H2 L1 */
1172 /* stack: ML MH M1 M2 */
1175 } else if (op
== '+' || op
== '-') {
1176 /* XXX: add non carry method too (for MIPS or alpha) */
1182 /* stack: H1 H2 (L1 op L2) */
1185 gen_op(op1
+ 1); /* TOK_xxxC2 */
1188 /* stack: H1 H2 (L1 op L2) */
1191 /* stack: (L1 op L2) H1 H2 */
1193 /* stack: (L1 op L2) (H1 op H2) */
1201 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1202 t
= vtop
[-1].type
.t
;
1206 /* stack: L H shift */
1208 /* constant: simpler */
1209 /* NOTE: all comments are for SHL. the other cases are
1210 done by swaping words */
1221 if (op
!= TOK_SAR
) {
1254 /* XXX: should provide a faster fallback on x86 ? */
1257 func
= TOK___ashrdi3
;
1260 func
= TOK___lshrdi3
;
1263 func
= TOK___ashldi3
;
1269 /* compare operations */
1275 /* stack: L1 H1 L2 H2 */
1277 vtop
[-1] = vtop
[-2];
1279 /* stack: L1 L2 H1 H2 */
1282 /* when values are equal, we need to compare low words. since
1283 the jump is inverted, we invert the test too. */
1286 else if (op1
== TOK_GT
)
1288 else if (op1
== TOK_ULT
)
1290 else if (op1
== TOK_UGT
)
1295 if (op1
!= TOK_NE
) {
1299 /* generate non equal test */
1300 /* XXX: NOT PORTABLE yet */
1304 #if defined(TCC_TARGET_I386)
1305 b
= psym(0x850f, 0);
1306 #elif defined(TCC_TARGET_ARM)
1308 o(0x1A000000 | encbranch(ind
, 0, 1));
1309 #elif defined(TCC_TARGET_C67)
1310 tcc_error("not implemented");
1312 #error not supported
1316 /* compare low. Always unsigned */
1320 else if (op1
== TOK_LE
)
1322 else if (op1
== TOK_GT
)
1324 else if (op1
== TOK_GE
)
1335 /* handle integer constant optimizations and various machine
1337 static void gen_opic(int op
)
1339 int c1
, c2
, t1
, t2
, n
;
1342 typedef unsigned long long U
;
1346 t1
= v1
->type
.t
& VT_BTYPE
;
1347 t2
= v2
->type
.t
& VT_BTYPE
;
1351 else if (v1
->type
.t
& VT_UNSIGNED
)
1358 else if (v2
->type
.t
& VT_UNSIGNED
)
1363 /* currently, we cannot do computations with forward symbols */
1364 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1365 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1368 case '+': l1
+= l2
; break;
1369 case '-': l1
-= l2
; break;
1370 case '&': l1
&= l2
; break;
1371 case '^': l1
^= l2
; break;
1372 case '|': l1
|= l2
; break;
1373 case '*': l1
*= l2
; break;
1380 /* if division by zero, generate explicit division */
1383 tcc_error("division by zero in constant");
1387 default: l1
/= l2
; break;
1388 case '%': l1
%= l2
; break;
1389 case TOK_UDIV
: l1
= (U
)l1
/ l2
; break;
1390 case TOK_UMOD
: l1
= (U
)l1
% l2
; break;
1393 case TOK_SHL
: l1
<<= l2
; break;
1394 case TOK_SHR
: l1
= (U
)l1
>> l2
; break;
1395 case TOK_SAR
: l1
>>= l2
; break;
1397 case TOK_ULT
: l1
= (U
)l1
< (U
)l2
; break;
1398 case TOK_UGE
: l1
= (U
)l1
>= (U
)l2
; break;
1399 case TOK_EQ
: l1
= l1
== l2
; break;
1400 case TOK_NE
: l1
= l1
!= l2
; break;
1401 case TOK_ULE
: l1
= (U
)l1
<= (U
)l2
; break;
1402 case TOK_UGT
: l1
= (U
)l1
> (U
)l2
; break;
1403 case TOK_LT
: l1
= l1
< l2
; break;
1404 case TOK_GE
: l1
= l1
>= l2
; break;
1405 case TOK_LE
: l1
= l1
<= l2
; break;
1406 case TOK_GT
: l1
= l1
> l2
; break;
1408 case TOK_LAND
: l1
= l1
&& l2
; break;
1409 case TOK_LOR
: l1
= l1
|| l2
; break;
1416 /* if commutative ops, put c2 as constant */
1417 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1418 op
== '|' || op
== '*')) {
1420 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1421 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1423 /* Filter out NOP operations like x*1, x-0, x&-1... */
1424 if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1427 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1428 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1434 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1435 /* try to use shifts instead of muls or divs */
1436 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1445 else if (op
== TOK_PDIV
)
1451 } else if (c2
&& (op
== '+' || op
== '-') &&
1452 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
1453 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1454 /* symbol + constant case */
1461 if (!nocode_wanted
) {
1462 /* call low level op generator */
1463 if (t1
== VT_LLONG
|| t2
== VT_LLONG
)
1474 /* generate a floating point operation with constant propagation */
1475 static void gen_opif(int op
)
1483 /* currently, we cannot do computations with forward symbols */
1484 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1485 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1487 if (v1
->type
.t
== VT_FLOAT
) {
1490 } else if (v1
->type
.t
== VT_DOUBLE
) {
1498 /* NOTE: we only do constant propagation if finite number (not
1499 NaN or infinity) (ANSI spec) */
1500 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1504 case '+': f1
+= f2
; break;
1505 case '-': f1
-= f2
; break;
1506 case '*': f1
*= f2
; break;
1510 tcc_error("division by zero in constant");
1515 /* XXX: also handles tests ? */
1519 /* XXX: overflow test ? */
1520 if (v1
->type
.t
== VT_FLOAT
) {
1522 } else if (v1
->type
.t
== VT_DOUBLE
) {
1530 if (!nocode_wanted
) {
1538 static int pointed_size(CType
*type
)
1541 return type_size(pointed_type(type
), &align
);
1544 static void vla_runtime_pointed_size(CType
*type
)
1547 vla_runtime_type_size(pointed_type(type
), &align
);
1550 static inline int is_null_pointer(SValue
*p
)
1552 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1554 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& p
->c
.i
== 0) ||
1555 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.ll
== 0) ||
1556 ((p
->type
.t
& VT_BTYPE
) == VT_PTR
&& p
->c
.ptr
== 0);
1559 static inline int is_integer_btype(int bt
)
1561 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
1562 bt
== VT_INT
|| bt
== VT_LLONG
);
1565 /* check types for comparison or substraction of pointers */
1566 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
1568 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
1571 /* null pointers are accepted for all comparisons as gcc */
1572 if (is_null_pointer(p1
) || is_null_pointer(p2
))
1576 bt1
= type1
->t
& VT_BTYPE
;
1577 bt2
= type2
->t
& VT_BTYPE
;
1578 /* accept comparison between pointer and integer with a warning */
1579 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
1580 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
1581 tcc_warning("comparison between pointer and integer");
1585 /* both must be pointers or implicit function pointers */
1586 if (bt1
== VT_PTR
) {
1587 type1
= pointed_type(type1
);
1588 } else if (bt1
!= VT_FUNC
)
1589 goto invalid_operands
;
1591 if (bt2
== VT_PTR
) {
1592 type2
= pointed_type(type2
);
1593 } else if (bt2
!= VT_FUNC
) {
1595 tcc_error("invalid operands to binary %s", get_tok_str(op
, NULL
));
1597 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1598 (type2
->t
& VT_BTYPE
) == VT_VOID
)
1602 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1603 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1604 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1605 /* gcc-like error if '-' is used */
1607 goto invalid_operands
;
1609 tcc_warning("comparison of distinct pointer types lacks a cast");
1613 /* generic gen_op: handles types problems */
1614 ST_FUNC
void gen_op(int op
)
1616 int u
, t1
, t2
, bt1
, bt2
, t
;
1619 t1
= vtop
[-1].type
.t
;
1620 t2
= vtop
[0].type
.t
;
1621 bt1
= t1
& VT_BTYPE
;
1622 bt2
= t2
& VT_BTYPE
;
1624 if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
1625 /* at least one operand is a pointer */
1626 /* relationnal op: must be both pointers */
1627 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
1628 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1629 /* pointers are handled are unsigned */
1630 #ifdef TCC_TARGET_X86_64
1631 t
= VT_LLONG
| VT_UNSIGNED
;
1633 t
= VT_INT
| VT_UNSIGNED
;
1637 /* if both pointers, then it must be the '-' op */
1638 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
1640 tcc_error("cannot use pointers here");
1641 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1642 /* XXX: check that types are compatible */
1643 if (vtop
[-1].type
.t
& VT_VLA
) {
1644 vla_runtime_pointed_size(&vtop
[-1].type
);
1646 vpushi(pointed_size(&vtop
[-1].type
));
1650 /* set to integer type */
1651 #ifdef TCC_TARGET_X86_64
1652 vtop
->type
.t
= VT_LLONG
;
1654 vtop
->type
.t
= VT_INT
;
1659 /* exactly one pointer : must be '+' or '-'. */
1660 if (op
!= '-' && op
!= '+')
1661 tcc_error("cannot use pointers here");
1662 /* Put pointer as first operand */
1663 if (bt2
== VT_PTR
) {
1667 type1
= vtop
[-1].type
;
1668 type1
.t
&= ~VT_ARRAY
;
1669 if (vtop
[-1].type
.t
& VT_VLA
)
1670 vla_runtime_pointed_size(&vtop
[-1].type
);
1672 u
= pointed_size(&vtop
[-1].type
);
1674 tcc_error("unknown array element size");
1675 #ifdef TCC_TARGET_X86_64
1678 /* XXX: cast to int ? (long long case) */
1683 #ifdef CONFIG_TCC_BCHECK
1684 /* if evaluating constant expression, no code should be
1685 generated, so no bound check */
1686 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
1687 /* if bounded pointers, we generate a special code to
1694 gen_bounded_ptr_add();
1700 /* put again type if gen_opic() swaped operands */
1703 } else if (is_float(bt1
) || is_float(bt2
)) {
1704 /* compute bigger type and do implicit casts */
1705 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
1707 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
1712 /* floats can only be used for a few operations */
1713 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
1714 (op
< TOK_ULT
|| op
> TOK_GT
))
1715 tcc_error("invalid operands for binary operation");
1717 } else if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
) {
1718 t
= bt1
== VT_LLONG
? VT_LLONG
: VT_INT
;
1719 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (t
| VT_UNSIGNED
))
1722 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
1723 /* cast to biggest op */
1725 /* convert to unsigned if it does not fit in a long long */
1726 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
1727 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
1730 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
1731 tcc_error("comparison of struct");
1733 /* integer operations */
1735 /* convert to unsigned if it does not fit in an integer */
1736 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
1737 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
1740 /* XXX: currently, some unsigned operations are explicit, so
1741 we modify them here */
1742 if (t
& VT_UNSIGNED
) {
1749 else if (op
== TOK_LT
)
1751 else if (op
== TOK_GT
)
1753 else if (op
== TOK_LE
)
1755 else if (op
== TOK_GE
)
1762 /* special case for shifts and long long: we keep the shift as
1764 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
1771 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
1772 /* relationnal op: the result is an int */
1773 vtop
->type
.t
= VT_INT
;
1780 #ifndef TCC_TARGET_ARM
1781 /* generic itof for unsigned long long case */
1782 static void gen_cvt_itof1(int t
)
1784 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
1785 (VT_LLONG
| VT_UNSIGNED
)) {
1788 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
1789 #if LDOUBLE_SIZE != 8
1790 else if (t
== VT_LDOUBLE
)
1791 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
1794 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
1798 vtop
->r
= reg_fret(t
);
1805 /* generic ftoi for unsigned long long case */
1806 static void gen_cvt_ftoi1(int t
)
1810 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
1811 /* not handled natively */
1812 st
= vtop
->type
.t
& VT_BTYPE
;
1814 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
1815 #if LDOUBLE_SIZE != 8
1816 else if (st
== VT_LDOUBLE
)
1817 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
1820 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
1825 vtop
->r2
= REG_LRET
;
1831 /* force char or short cast */
1832 static void force_charshort_cast(int t
)
1836 /* XXX: add optimization if lvalue : just change type and offset */
1841 if (t
& VT_UNSIGNED
) {
1842 vpushi((1 << bits
) - 1);
1848 /* result must be signed or the SAR is converted to an SHL
1849 This was not the case when "t" was a signed short
1850 and the last value on the stack was an unsigned int */
1851 vtop
->type
.t
&= ~VT_UNSIGNED
;
1857 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1858 static void gen_cast(CType
*type
)
1860 int sbt
, dbt
, sf
, df
, c
, p
;
1862 /* special delayed cast for char/short */
1863 /* XXX: in some cases (multiple cascaded casts), it may still
1865 if (vtop
->r
& VT_MUSTCAST
) {
1866 vtop
->r
&= ~VT_MUSTCAST
;
1867 force_charshort_cast(vtop
->type
.t
);
1870 /* bitfields first get cast to ints */
1871 if (vtop
->type
.t
& VT_BITFIELD
) {
1875 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
1876 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
1881 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1882 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
1884 /* constant case: we can do it now */
1885 /* XXX: in ISOC, cannot do it if error in convert */
1886 if (sbt
== VT_FLOAT
)
1887 vtop
->c
.ld
= vtop
->c
.f
;
1888 else if (sbt
== VT_DOUBLE
)
1889 vtop
->c
.ld
= vtop
->c
.d
;
1892 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
1893 if (sbt
& VT_UNSIGNED
)
1894 vtop
->c
.ld
= vtop
->c
.ull
;
1896 vtop
->c
.ld
= vtop
->c
.ll
;
1898 if (sbt
& VT_UNSIGNED
)
1899 vtop
->c
.ld
= vtop
->c
.ui
;
1901 vtop
->c
.ld
= vtop
->c
.i
;
1904 if (dbt
== VT_FLOAT
)
1905 vtop
->c
.f
= (float)vtop
->c
.ld
;
1906 else if (dbt
== VT_DOUBLE
)
1907 vtop
->c
.d
= (double)vtop
->c
.ld
;
1908 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
1909 vtop
->c
.ull
= (unsigned long long)vtop
->c
.ld
;
1910 } else if (sf
&& dbt
== VT_BOOL
) {
1911 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
1914 vtop
->c
.ll
= (long long)vtop
->c
.ld
;
1915 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
1916 vtop
->c
.ll
= vtop
->c
.ull
;
1917 else if (sbt
& VT_UNSIGNED
)
1918 vtop
->c
.ll
= vtop
->c
.ui
;
1919 #ifdef TCC_TARGET_X86_64
1920 else if (sbt
== VT_PTR
)
1923 else if (sbt
!= VT_LLONG
)
1924 vtop
->c
.ll
= vtop
->c
.i
;
1926 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
1927 vtop
->c
.ull
= vtop
->c
.ll
;
1928 else if (dbt
== VT_BOOL
)
1929 vtop
->c
.i
= (vtop
->c
.ll
!= 0);
1930 else if (dbt
!= VT_LLONG
) {
1932 if ((dbt
& VT_BTYPE
) == VT_BYTE
)
1934 else if ((dbt
& VT_BTYPE
) == VT_SHORT
)
1937 if(dbt
& VT_UNSIGNED
)
1938 vtop
->c
.ui
= ((unsigned int)vtop
->c
.ll
<< s
) >> s
;
1940 vtop
->c
.i
= ((int)vtop
->c
.ll
<< s
) >> s
;
1943 } else if (p
&& dbt
== VT_BOOL
) {
1946 } else if (!nocode_wanted
) {
1947 /* non constant case: generate code */
1949 /* convert from fp to fp */
1952 /* convert int to fp */
1955 /* convert fp to int */
1956 if (dbt
== VT_BOOL
) {
1960 /* we handle char/short/etc... with generic code */
1961 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
1962 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
1966 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
1967 /* additional cast for char/short... */
1972 #ifndef TCC_TARGET_X86_64
1973 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
1974 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
1975 /* scalar to long long */
1976 /* machine independent conversion */
1978 /* generate high word */
1979 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
1983 if (sbt
== VT_PTR
) {
1984 /* cast from pointer to int before we apply
1985 shift operation, which pointers don't support*/
1986 gen_cast(&int_type
);
1992 /* patch second register */
1993 vtop
[-1].r2
= vtop
->r
;
1997 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
1998 (dbt
& VT_BTYPE
) == VT_PTR
||
1999 (dbt
& VT_BTYPE
) == VT_FUNC
) {
2000 if ((sbt
& VT_BTYPE
) != VT_LLONG
&&
2001 (sbt
& VT_BTYPE
) != VT_PTR
&&
2002 (sbt
& VT_BTYPE
) != VT_FUNC
) {
2003 /* need to convert from 32bit to 64bit */
2005 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
2006 /* x86_64 specific: movslq */
2008 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
2012 } else if (dbt
== VT_BOOL
) {
2013 /* scalar to bool */
2016 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
2017 (dbt
& VT_BTYPE
) == VT_SHORT
) {
2018 if (sbt
== VT_PTR
) {
2019 vtop
->type
.t
= VT_INT
;
2020 tcc_warning("nonportable conversion from pointer to char/short");
2022 force_charshort_cast(dbt
);
2023 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
2025 if (sbt
== VT_LLONG
) {
2026 /* from long long: just take low order word */
2030 /* if lvalue and single word type, nothing to do because
2031 the lvalue already contains the real type size (see
2032 VT_LVAL_xxx constants) */
2035 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
2036 /* if we are casting between pointer types,
2037 we must update the VT_LVAL_xxx size */
2038 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
2039 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
2044 /* return type size as known at compile time. Put alignment at 'a' */
2045 ST_FUNC
int type_size(CType
*type
, int *a
)
2050 bt
= type
->t
& VT_BTYPE
;
2051 if (bt
== VT_STRUCT
) {
2056 } else if (bt
== VT_PTR
) {
2057 if (type
->t
& VT_ARRAY
) {
2061 ts
= type_size(&s
->type
, a
);
2063 if (ts
< 0 && s
->c
< 0)
2071 } else if (bt
== VT_LDOUBLE
) {
2073 return LDOUBLE_SIZE
;
2074 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
2075 #ifdef TCC_TARGET_I386
2076 #ifdef TCC_TARGET_PE
2081 #elif defined(TCC_TARGET_ARM)
2091 } else if (bt
== VT_INT
|| bt
== VT_ENUM
|| bt
== VT_FLOAT
) {
2094 } else if (bt
== VT_SHORT
) {
2097 } else if (bt
== VT_QLONG
|| bt
== VT_QFLOAT
) {
2101 /* char, void, function, _Bool */
2107 /* push type size as known at runtime time on top of value stack. Put
2109 ST_FUNC
void vla_runtime_type_size(CType
*type
, int *a
)
2111 if (type
->t
& VT_VLA
) {
2112 vset(&int_type
, VT_LOCAL
|VT_LVAL
, type
->ref
->c
);
2114 vpushi(type_size(type
, a
));
2118 static void vla_sp_save(void) {
2119 if (!(vla_flags
& VLA_SP_LOC_SET
)) {
2120 *vla_sp_loc
= (loc
-= PTR_SIZE
);
2121 vla_flags
|= VLA_SP_LOC_SET
;
2123 if (!(vla_flags
& VLA_SP_SAVED
)) {
2124 gen_vla_sp_save(*vla_sp_loc
);
2125 vla_flags
|= VLA_SP_SAVED
;
2129 /* return the pointed type of t */
2130 static inline CType
*pointed_type(CType
*type
)
2132 return &type
->ref
->type
;
2135 /* modify type so that its it is a pointer to type. */
2136 ST_FUNC
void mk_pointer(CType
*type
)
2139 s
= sym_push(SYM_FIELD
, type
, 0, -1);
2140 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
2144 /* compare function types. OLD functions match any new functions */
2145 static int is_compatible_func(CType
*type1
, CType
*type2
)
2151 if (!is_compatible_types(&s1
->type
, &s2
->type
))
2153 /* check func_call */
2154 if (FUNC_CALL(s1
->r
) != FUNC_CALL(s2
->r
))
2156 /* XXX: not complete */
2157 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
2161 while (s1
!= NULL
) {
2164 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
2174 /* return true if type1 and type2 are the same. If unqualified is
2175 true, qualifiers on the types are ignored.
2177 - enums are not checked as gcc __builtin_types_compatible_p ()
2179 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2183 t1
= type1
->t
& VT_TYPE
;
2184 t2
= type2
->t
& VT_TYPE
;
2186 /* strip qualifiers before comparing */
2187 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2188 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2190 /* XXX: bitfields ? */
2193 /* test more complicated cases */
2194 bt1
= t1
& VT_BTYPE
;
2195 if (bt1
== VT_PTR
) {
2196 type1
= pointed_type(type1
);
2197 type2
= pointed_type(type2
);
2198 return is_compatible_types(type1
, type2
);
2199 } else if (bt1
== VT_STRUCT
) {
2200 return (type1
->ref
== type2
->ref
);
2201 } else if (bt1
== VT_FUNC
) {
2202 return is_compatible_func(type1
, type2
);
2208 /* return true if type1 and type2 are exactly the same (including
2211 static int is_compatible_types(CType
*type1
, CType
*type2
)
2213 return compare_types(type1
,type2
,0);
2216 /* return true if type1 and type2 are the same (ignoring qualifiers).
2218 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
2220 return compare_types(type1
,type2
,1);
2223 /* print a type. If 'varstr' is not NULL, then the variable is also
2224 printed in the type */
2226 /* XXX: add array and function pointers */
2227 static void type_to_str(char *buf
, int buf_size
,
2228 CType
*type
, const char *varstr
)
2235 t
= type
->t
& VT_TYPE
;
2238 if (t
& VT_CONSTANT
)
2239 pstrcat(buf
, buf_size
, "const ");
2240 if (t
& VT_VOLATILE
)
2241 pstrcat(buf
, buf_size
, "volatile ");
2242 if (t
& VT_UNSIGNED
)
2243 pstrcat(buf
, buf_size
, "unsigned ");
2273 tstr
= "long double";
2275 pstrcat(buf
, buf_size
, tstr
);
2279 if (bt
== VT_STRUCT
)
2283 pstrcat(buf
, buf_size
, tstr
);
2284 v
= type
->ref
->v
& ~SYM_STRUCT
;
2285 if (v
>= SYM_FIRST_ANOM
)
2286 pstrcat(buf
, buf_size
, "<anonymous>");
2288 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
2292 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
2293 pstrcat(buf
, buf_size
, "(");
2295 while (sa
!= NULL
) {
2296 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
2297 pstrcat(buf
, buf_size
, buf1
);
2300 pstrcat(buf
, buf_size
, ", ");
2302 pstrcat(buf
, buf_size
, ")");
2306 pstrcpy(buf1
, sizeof(buf1
), "*");
2308 pstrcat(buf1
, sizeof(buf1
), varstr
);
2309 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2313 pstrcat(buf
, buf_size
, " ");
2314 pstrcat(buf
, buf_size
, varstr
);
2319 /* verify type compatibility to store vtop in 'dt' type, and generate
2321 static void gen_assign_cast(CType
*dt
)
2323 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
2324 char buf1
[256], buf2
[256];
2327 st
= &vtop
->type
; /* source type */
2328 dbt
= dt
->t
& VT_BTYPE
;
2329 sbt
= st
->t
& VT_BTYPE
;
2331 tcc_error("Cannot assign void value");
2332 if (dt
->t
& VT_CONSTANT
)
2333 tcc_warning("assignment of read-only location");
2336 /* special cases for pointers */
2337 /* '0' can also be a pointer */
2338 if (is_null_pointer(vtop
))
2340 /* accept implicit pointer to integer cast with warning */
2341 if (is_integer_btype(sbt
)) {
2342 tcc_warning("assignment makes pointer from integer without a cast");
2345 type1
= pointed_type(dt
);
2346 /* a function is implicitely a function pointer */
2347 if (sbt
== VT_FUNC
) {
2348 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
2349 !is_compatible_types(pointed_type(dt
), st
))
2350 tcc_warning("assignment from incompatible pointer type");
2355 type2
= pointed_type(st
);
2356 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2357 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
2358 /* void * can match anything */
2360 /* exact type match, except for unsigned */
2363 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2364 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2365 if (!is_compatible_types(&tmp_type1
, &tmp_type2
))
2366 tcc_warning("assignment from incompatible pointer type");
2368 /* check const and volatile */
2369 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
2370 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
2371 tcc_warning("assignment discards qualifiers from pointer target type");
2377 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
2378 tcc_warning("assignment makes integer from pointer without a cast");
2380 /* XXX: more tests */
2385 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2386 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2387 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2389 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
2390 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
2391 tcc_error("cannot cast '%s' to '%s'", buf1
, buf2
);
2399 /* store vtop in lvalue pushed on stack */
2400 ST_FUNC
void vstore(void)
2402 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
2404 ft
= vtop
[-1].type
.t
;
2405 sbt
= vtop
->type
.t
& VT_BTYPE
;
2406 dbt
= ft
& VT_BTYPE
;
2407 if ((((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2408 (sbt
== VT_INT
&& dbt
== VT_SHORT
))
2409 && !(vtop
->type
.t
& VT_BITFIELD
)) {
2410 /* optimize char/short casts */
2411 delayed_cast
= VT_MUSTCAST
;
2412 vtop
->type
.t
= ft
& (VT_TYPE
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
)));
2413 /* XXX: factorize */
2414 if (ft
& VT_CONSTANT
)
2415 tcc_warning("assignment of read-only location");
2418 if (!(ft
& VT_BITFIELD
))
2419 gen_assign_cast(&vtop
[-1].type
);
2422 if (sbt
== VT_STRUCT
) {
2423 /* if structure, only generate pointer */
2424 /* structure assignment : generate memcpy */
2425 /* XXX: optimize if small size */
2426 if (!nocode_wanted
) {
2427 size
= type_size(&vtop
->type
, &align
);
2431 vtop
->type
.t
= VT_PTR
;
2434 /* address of memcpy() */
2437 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2438 else if(!(align
& 3))
2439 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2442 vpush_global_sym(&func_old_type
, TOK_memcpy
);
2447 vtop
->type
.t
= VT_PTR
;
2456 /* leave source on stack */
2457 } else if (ft
& VT_BITFIELD
) {
2458 /* bitfield store handling */
2459 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2460 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2461 /* remove bit field info to avoid loops */
2462 vtop
[-1].type
.t
= ft
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
2464 /* duplicate source into other register */
2469 if((ft
& VT_BTYPE
) == VT_BOOL
) {
2470 gen_cast(&vtop
[-1].type
);
2471 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2474 /* duplicate destination */
2476 vtop
[-1] = vtop
[-2];
2478 /* mask and shift source */
2479 if((ft
& VT_BTYPE
) != VT_BOOL
) {
2480 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2481 vpushll((1ULL << bit_size
) - 1ULL);
2483 vpushi((1 << bit_size
) - 1);
2489 /* load destination, mask and or with source */
2491 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2492 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2494 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2501 /* pop off shifted source from "duplicate source..." above */
2505 #ifdef CONFIG_TCC_BCHECK
2506 /* bound check case */
2507 if (vtop
[-1].r
& VT_MUSTBOUND
) {
2513 if (!nocode_wanted
) {
2517 #ifdef TCC_TARGET_X86_64
2518 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
2520 } else if ((ft
& VT_BTYPE
) == VT_QFLOAT
) {
2525 r
= gv(rc
); /* generate value */
2526 /* if lvalue was saved on stack, must read it */
2527 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
2529 t
= get_reg(RC_INT
);
2530 #ifdef TCC_TARGET_X86_64
2535 sv
.r
= VT_LOCAL
| VT_LVAL
;
2536 sv
.c
.ul
= vtop
[-1].c
.ul
;
2538 vtop
[-1].r
= t
| VT_LVAL
;
2540 /* two word case handling : store second register at word + 4 (or +8 for x86-64) */
2541 #ifdef TCC_TARGET_X86_64
2542 if (((ft
& VT_BTYPE
) == VT_QLONG
) || ((ft
& VT_BTYPE
) == VT_QFLOAT
)) {
2543 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
2545 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
2546 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
2548 vtop
[-1].type
.t
= load_type
;
2551 /* convert to int to increment easily */
2552 vtop
->type
.t
= addr_type
;
2558 vtop
[-1].type
.t
= load_type
;
2559 /* XXX: it works because r2 is spilled last ! */
2560 store(vtop
->r2
, vtop
- 1);
2566 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
2567 vtop
->r
|= delayed_cast
;
2571 /* post defines POST/PRE add. c is the token ++ or -- */
2572 ST_FUNC
void inc(int post
, int c
)
2575 vdup(); /* save lvalue */
2577 gv_dup(); /* duplicate value */
2582 vpushi(c
- TOK_MID
);
2584 vstore(); /* store value */
2586 vpop(); /* if post op, return saved value */
2589 /* Parse GNUC __attribute__ extension. Currently, the following
2590 extensions are recognized:
2591 - aligned(n) : set data/function alignment.
2592 - packed : force data alignment to 1
2593 - section(x) : generate data/code in this section.
2594 - unused : currently ignored, but may be used someday.
2595 - regparm(n) : pass function parameters in registers (i386 only)
2597 static void parse_attribute(AttributeDef
*ad
)
2601 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
2605 while (tok
!= ')') {
2606 if (tok
< TOK_IDENT
)
2607 expect("attribute name");
2615 expect("section name");
2616 ad
->section
= find_section(tcc_state
, (char *)tokc
.cstr
->data
);
2624 expect("alias(\"target\")");
2625 ad
->alias_target
= /* save string as token, for later */
2626 tok_alloc((char*)tokc
.cstr
->data
, tokc
.cstr
->size
-1)->tok
;
2635 if (n
<= 0 || (n
& (n
- 1)) != 0)
2636 tcc_error("alignment must be a positive power of two");
2653 /* currently, no need to handle it because tcc does not
2654 track unused objects */
2658 /* currently, no need to handle it because tcc does not
2659 track unused objects */
2664 ad
->func_call
= FUNC_CDECL
;
2669 ad
->func_call
= FUNC_STDCALL
;
2671 #ifdef TCC_TARGET_I386
2681 ad
->func_call
= FUNC_FASTCALL1
+ n
- 1;
2687 ad
->func_call
= FUNC_FASTCALLW
;
2694 ad
->mode
= VT_LLONG
+ 1;
2697 ad
->mode
= VT_SHORT
+ 1;
2700 ad
->mode
= VT_INT
+ 1;
2703 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
2710 ad
->func_export
= 1;
2713 ad
->func_import
= 1;
2716 if (tcc_state
->warn_unsupported
)
2717 tcc_warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
2718 /* skip parameters */
2720 int parenthesis
= 0;
2724 else if (tok
== ')')
2727 } while (parenthesis
&& tok
!= -1);
2740 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2741 static void struct_decl(CType
*type
, int u
, int tdef
)
2743 int a
, v
, size
, align
, maxalign
, c
, offset
, flexible
;
2744 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
, prevbt
;
2745 Sym
*s
, *ss
, *ass
, **ps
;
2749 a
= tok
; /* save decl type */
2754 /* struct already defined ? return it */
2756 expect("struct/union/enum name");
2760 tcc_error("invalid type");
2762 } else if (tok
>= TOK_IDENT
&& !tdef
)
2763 tcc_error("unknown struct/union/enum");
2769 /* we put an undefined size for struct/union */
2770 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
2771 s
->r
= 0; /* default alignment is zero as gcc */
2772 /* put struct/union/enum name in type */
2780 tcc_error("struct/union/enum already defined");
2781 /* cannot be empty */
2783 /* non empty enums are not allowed */
2784 if (a
== TOK_ENUM
) {
2788 expect("identifier");
2791 tcc_error("redefinition of enumerator '%s'",
2792 get_tok_str(v
, NULL
));
2798 /* enum symbols have static storage */
2799 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
2800 ss
->type
.t
|= VT_STATIC
;
2805 /* NOTE: we accept a trailing comma */
2809 s
->c
= type_size(&int_type
, &align
);
2818 while (tok
!= '}') {
2819 parse_btype(&btype
, &ad
);
2822 tcc_error("flexible array member '%s' not at the end of struct",
2823 get_tok_str(v
, NULL
));
2828 type_decl(&type1
, &ad
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
2829 if (v
== 0 && (type1
.t
& VT_BTYPE
) != VT_STRUCT
)
2830 expect("identifier");
2831 if (type_size(&type1
, &align
) < 0) {
2832 if ((a
== TOK_STRUCT
) && (type1
.t
& VT_ARRAY
) && c
)
2835 tcc_error("field '%s' has incomplete type",
2836 get_tok_str(v
, NULL
));
2838 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
2839 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
2840 tcc_error("invalid type for '%s'",
2841 get_tok_str(v
, NULL
));
2845 bit_size
= expr_const();
2846 /* XXX: handle v = 0 case for messages */
2848 tcc_error("negative width in bit-field '%s'",
2849 get_tok_str(v
, NULL
));
2850 if (v
&& bit_size
== 0)
2851 tcc_error("zero width for bit-field '%s'",
2852 get_tok_str(v
, NULL
));
2854 size
= type_size(&type1
, &align
);
2856 if (align
< ad
.aligned
)
2858 } else if (ad
.packed
) {
2860 } else if (*tcc_state
->pack_stack_ptr
) {
2861 if (align
> *tcc_state
->pack_stack_ptr
)
2862 align
= *tcc_state
->pack_stack_ptr
;
2865 if (bit_size
>= 0) {
2866 bt
= type1
.t
& VT_BTYPE
;
2873 tcc_error("bitfields must have scalar type");
2875 if (bit_size
> bsize
) {
2876 tcc_error("width of '%s' exceeds its type",
2877 get_tok_str(v
, NULL
));
2878 } else if (bit_size
== bsize
) {
2879 /* no need for bit fields */
2881 } else if (bit_size
== 0) {
2882 /* XXX: what to do if only padding in a
2884 /* zero size: means to pad */
2887 /* we do not have enough room ?
2888 did the type change?
2890 if ((bit_pos
+ bit_size
) > bsize
||
2891 bt
!= prevbt
|| a
== TOK_UNION
)
2894 /* XXX: handle LSB first */
2895 type1
.t
|= VT_BITFIELD
|
2896 (bit_pos
<< VT_STRUCT_SHIFT
) |
2897 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
2898 bit_pos
+= bit_size
;
2904 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2905 /* add new memory data only if starting
2907 if (lbit_pos
== 0) {
2908 if (a
== TOK_STRUCT
) {
2909 c
= (c
+ align
- 1) & -align
;
2918 if (align
> maxalign
)
2922 printf("add field %s offset=%d",
2923 get_tok_str(v
, NULL
), offset
);
2924 if (type1
.t
& VT_BITFIELD
) {
2925 printf(" pos=%d size=%d",
2926 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
2927 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
2932 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2934 while ((ass
= ass
->next
) != NULL
) {
2935 ss
= sym_push(ass
->v
, &ass
->type
, 0, offset
+ ass
->c
);
2940 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
2944 if (tok
== ';' || tok
== TOK_EOF
)
2951 /* store size and alignment */
2952 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
2958 /* return 0 if no type declaration. otherwise, return the basic type
2961 static int parse_btype(CType
*type
, AttributeDef
*ad
)
2963 int t
, u
, type_found
, typespec_found
, typedef_found
;
2967 memset(ad
, 0, sizeof(AttributeDef
));
2975 /* currently, we really ignore extension */
2985 if ((t
& VT_BTYPE
) != 0)
2986 tcc_error("too many basic types");
3002 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
3003 #ifndef TCC_TARGET_PE
3004 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3006 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
3007 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3021 if ((t
& VT_BTYPE
) == VT_LONG
) {
3022 #ifdef TCC_TARGET_PE
3023 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
3025 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3033 struct_decl(&type1
, VT_ENUM
, t
& VT_TYPEDEF
);
3036 type
->ref
= type1
.ref
;
3040 struct_decl(&type1
, VT_STRUCT
, t
& VT_TYPEDEF
);
3043 /* type modifiers */
3096 /* GNUC attribute */
3097 case TOK_ATTRIBUTE1
:
3098 case TOK_ATTRIBUTE2
:
3099 parse_attribute(ad
);
3102 t
= (t
& ~VT_BTYPE
) | u
;
3110 parse_expr_type(&type1
);
3111 /* remove all storage modifiers except typedef */
3112 type1
.t
&= ~(VT_STORAGE
&~VT_TYPEDEF
);
3115 if (typespec_found
|| typedef_found
)
3118 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
3121 t
|= (s
->type
.t
& ~VT_TYPEDEF
);
3122 type
->ref
= s
->type
.ref
;
3124 /* get attributes from typedef */
3125 if (0 == ad
->aligned
)
3126 ad
->aligned
= FUNC_ALIGN(s
->r
);
3127 if (0 == ad
->func_call
)
3128 ad
->func_call
= FUNC_CALL(s
->r
);
3129 ad
->packed
|= FUNC_PACKED(s
->r
);
3138 if ((t
& (VT_SIGNED
|VT_UNSIGNED
)) == (VT_SIGNED
|VT_UNSIGNED
))
3139 tcc_error("signed and unsigned modifier");
3140 if (tcc_state
->char_is_unsigned
) {
3141 if ((t
& (VT_SIGNED
|VT_UNSIGNED
|VT_BTYPE
)) == VT_BYTE
)
3146 /* long is never used as type */
3147 if ((t
& VT_BTYPE
) == VT_LONG
)
3148 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
3149 t
= (t
& ~VT_BTYPE
) | VT_INT
;
3151 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3157 /* convert a function parameter type (array to pointer and function to
3158 function pointer) */
3159 static inline void convert_parameter_type(CType
*pt
)
3161 /* remove const and volatile qualifiers (XXX: const could be used
3162 to indicate a const function parameter */
3163 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3164 /* array must be transformed to pointer according to ANSI C */
3166 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
3171 ST_FUNC
void parse_asm_str(CString
*astr
)
3174 /* read the string */
3176 expect("string constant");
3178 while (tok
== TOK_STR
) {
3179 /* XXX: add \0 handling too ? */
3180 cstr_cat(astr
, tokc
.cstr
->data
);
3183 cstr_ccat(astr
, '\0');
3186 /* Parse an asm label and return the label
3187 * Don't forget to free the CString in the caller! */
3188 static void asm_label_instr(CString
*astr
)
3191 parse_asm_str(astr
);
3194 printf("asm_alias: \"%s\"\n", (char *)astr
->data
);
3198 static void post_type(CType
*type
, AttributeDef
*ad
)
3200 int n
, l
, t1
, arg_size
, align
;
3201 Sym
**plast
, *s
, *first
;
3206 /* function declaration */
3214 /* read param name and compute offset */
3215 if (l
!= FUNC_OLD
) {
3216 if (!parse_btype(&pt
, &ad1
)) {
3218 tcc_error("invalid type");
3225 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
3227 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3228 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
3229 tcc_error("parameter declared as void");
3230 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
3235 expect("identifier");
3239 convert_parameter_type(&pt
);
3240 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
3246 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
3253 /* if no parameters, then old type prototype */
3257 /* NOTE: const is ignored in returned type as it has a special
3258 meaning in gcc / C++ */
3259 type
->t
&= ~VT_CONSTANT
;
3260 /* some ancient pre-K&R C allows a function to return an array
3261 and the array brackets to be put after the arguments, such
3262 that "int c()[]" means something like "int[] c()" */
3265 skip(']'); /* only handle simple "[]" */
3268 /* we push a anonymous symbol which will contain the function prototype */
3269 ad
->func_args
= arg_size
;
3270 s
= sym_push(SYM_FIELD
, type
, INT_ATTR(ad
), l
);
3274 } else if (tok
== '[') {
3275 /* array definition */
3277 if (tok
== TOK_RESTRICT1
)
3282 if (!local_stack
|| nocode_wanted
)
3283 vpushi(expr_const());
3285 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3288 tcc_error("invalid array size");
3290 if (!is_integer_btype(vtop
->type
.t
& VT_BTYPE
))
3291 tcc_error("size of variable length array should be an integer");
3296 /* parse next post type */
3297 post_type(type
, ad
);
3298 if (type
->t
== VT_FUNC
)
3299 tcc_error("declaration of an array of functions");
3300 t1
|= type
->t
& VT_VLA
;
3303 loc
-= type_size(&int_type
, &align
);
3307 vla_runtime_type_size(type
, &align
);
3309 vset(&int_type
, VT_LOCAL
|VT_LVAL
, loc
);
3316 /* we push an anonymous symbol which will contain the array
3318 s
= sym_push(SYM_FIELD
, type
, 0, n
);
3319 type
->t
= (t1
? VT_VLA
: VT_ARRAY
) | VT_PTR
;
3324 /* Parse a type declaration (except basic type), and return the type
3325 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3326 expected. 'type' should contain the basic type. 'ad' is the
3327 attribute definition of the basic type. It can be modified by
3330 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
3333 CType type1
, *type2
;
3334 int qualifiers
, storage
;
3336 while (tok
== '*') {
3344 qualifiers
|= VT_CONSTANT
;
3349 qualifiers
|= VT_VOLATILE
;
3357 type
->t
|= qualifiers
;
3360 /* XXX: clarify attribute handling */
3361 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3362 parse_attribute(ad
);
3364 /* recursive type */
3365 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3366 type1
.t
= 0; /* XXX: same as int */
3369 /* XXX: this is not correct to modify 'ad' at this point, but
3370 the syntax is not clear */
3371 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3372 parse_attribute(ad
);
3373 type_decl(&type1
, ad
, v
, td
);
3376 /* type identifier */
3377 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
3381 if (!(td
& TYPE_ABSTRACT
))
3382 expect("identifier");
3386 storage
= type
->t
& VT_STORAGE
;
3387 type
->t
&= ~VT_STORAGE
;
3388 if (storage
& VT_STATIC
) {
3389 int saved_nocode_wanted
= nocode_wanted
;
3391 post_type(type
, ad
);
3392 nocode_wanted
= saved_nocode_wanted
;
3394 post_type(type
, ad
);
3396 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3397 parse_attribute(ad
);
3401 /* append type at the end of type1 */
3414 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3415 ST_FUNC
int lvalue_type(int t
)
3420 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
3422 else if (bt
== VT_SHORT
)
3426 if (t
& VT_UNSIGNED
)
3427 r
|= VT_LVAL_UNSIGNED
;
3431 /* indirection with full error checking and bound check */
3432 ST_FUNC
void indir(void)
3434 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
3435 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
3439 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
3441 vtop
->type
= *pointed_type(&vtop
->type
);
3442 /* Arrays and functions are never lvalues */
3443 if (!(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_VLA
)
3444 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3445 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3446 /* if bound checking, the referenced pointer must be checked */
3447 #ifdef CONFIG_TCC_BCHECK
3448 if (tcc_state
->do_bounds_check
)
3449 vtop
->r
|= VT_MUSTBOUND
;
3454 /* pass a parameter to a function and do type checking and casting */
3455 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
3460 func_type
= func
->c
;
3461 if (func_type
== FUNC_OLD
||
3462 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
3463 /* default casting : only need to convert float to double */
3464 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
3468 } else if (arg
== NULL
) {
3469 tcc_error("too many arguments to function");
3472 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
3473 gen_assign_cast(&type
);
3477 /* parse an expression of the form '(type)' or '(expr)' and return its
3479 static void parse_expr_type(CType
*type
)
3485 if (parse_btype(type
, &ad
)) {
3486 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3493 static void parse_type(CType
*type
)
3498 if (!parse_btype(type
, &ad
)) {
3501 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3504 static void vpush_tokc(int t
)
3509 vsetc(&type
, VT_CONST
, &tokc
);
3512 ST_FUNC
void unary(void)
3514 int n
, t
, align
, size
, r
, sizeof_caller
;
3518 static int in_sizeof
= 0;
3520 sizeof_caller
= in_sizeof
;
3522 /* XXX: GCC 2.95.3 does not generate a table although it should be
3536 vpush_tokc(VT_INT
| VT_UNSIGNED
);
3540 vpush_tokc(VT_LLONG
);
3544 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
3548 vpush_tokc(VT_FLOAT
);
3552 vpush_tokc(VT_DOUBLE
);
3556 vpush_tokc(VT_LDOUBLE
);
3559 case TOK___FUNCTION__
:
3561 goto tok_identifier
;
3567 /* special function name identifier */
3568 len
= strlen(funcname
) + 1;
3569 /* generate char[len] type */
3574 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
3575 ptr
= section_ptr_add(data_section
, len
);
3576 memcpy(ptr
, funcname
, len
);
3581 #ifdef TCC_TARGET_PE
3582 t
= VT_SHORT
| VT_UNSIGNED
;
3588 /* string parsing */
3591 if (tcc_state
->warn_write_strings
)
3596 memset(&ad
, 0, sizeof(AttributeDef
));
3597 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, NULL
, 0);
3602 if (parse_btype(&type
, &ad
)) {
3603 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
3605 /* check ISOC99 compound literal */
3607 /* data is allocated locally by default */
3612 /* all except arrays are lvalues */
3613 if (!(type
.t
& VT_ARRAY
))
3614 r
|= lvalue_type(type
.t
);
3615 memset(&ad
, 0, sizeof(AttributeDef
));
3616 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, NULL
, 0);
3618 if (sizeof_caller
) {
3625 } else if (tok
== '{') {
3626 /* save all registers */
3628 /* statement expression : we do not accept break/continue
3629 inside as GCC does */
3630 block(NULL
, NULL
, NULL
, NULL
, 0, 1);
3645 /* functions names must be treated as function pointers,
3646 except for unary '&' and sizeof. Since we consider that
3647 functions are not lvalues, we only have to handle it
3648 there and in function calls. */
3649 /* arrays can also be used although they are not lvalues */
3650 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
3651 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
3653 mk_pointer(&vtop
->type
);
3659 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3661 boolean
.t
= VT_BOOL
;
3663 vtop
->c
.i
= !vtop
->c
.i
;
3664 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
3665 vtop
->c
.i
= vtop
->c
.i
^ 1;
3668 vseti(VT_JMP
, gtst(1, 0));
3679 /* in order to force cast, we add zero */
3681 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
3682 tcc_error("pointer not accepted for unary plus");
3692 unary_type(&type
); // Perform a in_sizeof = 0;
3693 size
= type_size(&type
, &align
);
3694 if (t
== TOK_SIZEOF
) {
3695 if (!(type
.t
& VT_VLA
)) {
3697 tcc_error("sizeof applied to an incomplete type");
3700 vla_runtime_type_size(&type
, &align
);
3705 vtop
->type
.t
|= VT_UNSIGNED
;
3708 case TOK_builtin_types_compatible_p
:
3717 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3718 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3719 vpushi(is_compatible_types(&type1
, &type2
));
3722 case TOK_builtin_constant_p
:
3724 int saved_nocode_wanted
, res
;
3727 saved_nocode_wanted
= nocode_wanted
;
3730 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
3732 nocode_wanted
= saved_nocode_wanted
;
3737 case TOK_builtin_frame_address
:
3743 if (tok
!= TOK_CINT
|| tokc
.i
< 0) {
3744 tcc_error("__builtin_frame_address only takes positive integers");
3751 vset(&type
, VT_LOCAL
, 0); /* local frame */
3753 mk_pointer(&vtop
->type
);
3754 indir(); /* -> parent frame */
3758 #ifdef TCC_TARGET_X86_64
3759 #ifdef TCC_TARGET_PE
3760 case TOK_builtin_va_start
:
3768 if ((vtop
->r
& VT_VALMASK
) != VT_LOCAL
)
3769 tcc_error("__builtin_va_start expects a local variable");
3770 vtop
->r
&= ~(VT_LVAL
| VT_REF
);
3771 vtop
->type
= char_pointer_type
;
3776 case TOK_builtin_va_arg_types
:
3783 vpushi(classify_x86_64_va_arg(&type
));
3803 goto tok_identifier
;
3805 /* allow to take the address of a label */
3806 if (tok
< TOK_UIDENT
)
3807 expect("label identifier");
3808 s
= label_find(tok
);
3810 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
3812 if (s
->r
== LABEL_DECLARED
)
3813 s
->r
= LABEL_FORWARD
;
3816 s
->type
.t
= VT_VOID
;
3817 mk_pointer(&s
->type
);
3818 s
->type
.t
|= VT_STATIC
;
3820 vset(&s
->type
, VT_CONST
| VT_SYM
, 0);
3825 // special qnan , snan and infinity values
3827 vpush64(VT_DOUBLE
, 0x7ff8000000000000ULL
);
3831 vpush64(VT_DOUBLE
, 0x7ff0000000000001ULL
);
3835 vpush64(VT_DOUBLE
, 0x7ff0000000000000ULL
);
3844 expect("identifier");
3848 tcc_error("'%s' undeclared", get_tok_str(t
, NULL
));
3849 /* for simple function calls, we tolerate undeclared
3850 external reference to int() function */
3851 if (tcc_state
->warn_implicit_function_declaration
)
3852 tcc_warning("implicit declaration of function '%s'",
3853 get_tok_str(t
, NULL
));
3854 s
= external_global_sym(t
, &func_old_type
, 0);
3856 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
3857 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
3858 /* if referencing an inline function, then we generate a
3859 symbol to it if not already done. It will have the
3860 effect to generate code for it at the end of the
3861 compilation unit. Inline function as always
3862 generated in the text section. */
3864 put_extern_sym(s
, text_section
, 0, 0);
3865 r
= VT_SYM
| VT_CONST
;
3869 vset(&s
->type
, r
, s
->c
);
3870 /* if forward reference, we must point to s */
3871 if (vtop
->r
& VT_SYM
) {
3878 /* post operations */
3880 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
3883 } else if (tok
== '.' || tok
== TOK_ARROW
) {
3886 if (tok
== TOK_ARROW
)
3888 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
3892 /* expect pointer on structure */
3893 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
3894 expect("struct or union");
3898 while ((s
= s
->next
) != NULL
) {
3903 tcc_error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, NULL
));
3904 /* add field offset to pointer */
3905 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
3908 /* change type to field type, and set to lvalue */
3909 vtop
->type
= s
->type
;
3910 vtop
->type
.t
|= qualifiers
;
3911 /* an array is never an lvalue */
3912 if (!(vtop
->type
.t
& VT_ARRAY
)) {
3913 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3914 #ifdef CONFIG_TCC_BCHECK
3915 /* if bound checking, the referenced pointer must be checked */
3916 if (tcc_state
->do_bounds_check
)
3917 vtop
->r
|= VT_MUSTBOUND
;
3921 } else if (tok
== '[') {
3927 } else if (tok
== '(') {
3930 int nb_args
, sret
, ret_align
;
3933 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3934 /* pointer test (no array accepted) */
3935 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
3936 vtop
->type
= *pointed_type(&vtop
->type
);
3937 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
3941 expect("function pointer");
3944 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
3946 /* get return type */
3949 sa
= s
->next
; /* first parameter */
3952 /* compute first implicit argument if a structure is returned */
3953 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
3954 sret
= gfunc_sret(&s
->type
, &ret
.type
, &ret_align
);
3956 /* get some space for the returned structure */
3957 size
= type_size(&s
->type
, &align
);
3958 loc
= (loc
- size
) & -align
;
3960 ret
.r
= VT_LOCAL
| VT_LVAL
;
3961 /* pass it as 'int' to avoid structure arg passing
3963 vseti(VT_LOCAL
, loc
);
3973 /* return in register */
3974 if (is_float(ret
.type
.t
)) {
3975 ret
.r
= reg_fret(ret
.type
.t
);
3976 #ifdef TCC_TARGET_X86_64
3977 if ((ret
.type
.t
& VT_BTYPE
) == VT_QFLOAT
)
3981 #ifdef TCC_TARGET_X86_64
3982 if ((ret
.type
.t
& VT_BTYPE
) == VT_QLONG
)
3984 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
3994 gfunc_param_typed(s
, sa
);
4004 tcc_error("too few arguments to function");
4006 if (!nocode_wanted
) {
4007 gfunc_call(nb_args
);
4009 vtop
-= (nb_args
+ 1);
4012 for (r
= ret
.r
+ sret
+ !sret
; r
-- > ret
.r
;) {
4013 vsetc(&ret
.type
, r
, &ret
.c
);
4014 vtop
->r2
= ret
.r2
; /* Loop only happens when r2 is VT_CONST */
4016 /* handle packed struct return */
4017 if (((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) && sret
) {
4020 size
= type_size(&s
->type
, &align
);
4021 loc
= (loc
- size
) & -align
;
4023 for(offset
= 0; offset
< size
; offset
+= ret_align
) {
4024 vset(&ret
.type
, VT_LOCAL
| VT_LVAL
, addr
+ offset
);
4029 vset(&s
->type
, VT_LOCAL
| VT_LVAL
, addr
);
4037 ST_FUNC
void expr_prod(void)
4042 while (tok
== '*' || tok
== '/' || tok
== '%') {
4050 ST_FUNC
void expr_sum(void)
4055 while (tok
== '+' || tok
== '-') {
4063 static void expr_shift(void)
4068 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
4076 static void expr_cmp(void)
4081 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
4082 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
4090 static void expr_cmpeq(void)
4095 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
4103 static void expr_and(void)
4106 while (tok
== '&') {
4113 static void expr_xor(void)
4116 while (tok
== '^') {
4123 static void expr_or(void)
4126 while (tok
== '|') {
4133 /* XXX: fix this mess */
4134 static void expr_land_const(void)
4137 while (tok
== TOK_LAND
) {
4144 /* XXX: fix this mess */
4145 static void expr_lor_const(void)
4148 while (tok
== TOK_LOR
) {
4155 /* only used if non constant */
4156 static void expr_land(void)
4161 if (tok
== TOK_LAND
) {
4166 if (tok
!= TOK_LAND
) {
4176 static void expr_lor(void)
4181 if (tok
== TOK_LOR
) {
4186 if (tok
!= TOK_LOR
) {
4196 /* XXX: better constant handling */
4197 static void expr_cond(void)
4199 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
;
4201 CType type
, type1
, type2
;
4208 boolean
.t
= VT_BOOL
;
4214 if (tok
!= ':' || !gnu_ext
) {
4229 if (vtop
!= vstack
) {
4230 /* needed to avoid having different registers saved in
4232 if (is_float(vtop
->type
.t
)) {
4234 #ifdef TCC_TARGET_X86_64
4235 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4245 if (tok
== ':' && gnu_ext
) {
4253 sv
= *vtop
; /* save value to handle it later */
4254 vtop
--; /* no vpop so that FP stack is not flushed */
4262 bt1
= t1
& VT_BTYPE
;
4264 bt2
= t2
& VT_BTYPE
;
4265 /* cast operands to correct type according to ISOC rules */
4266 if (is_float(bt1
) || is_float(bt2
)) {
4267 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
4268 type
.t
= VT_LDOUBLE
;
4269 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
4274 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
4275 /* cast to biggest op */
4277 /* convert to unsigned if it does not fit in a long long */
4278 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
4279 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
4280 type
.t
|= VT_UNSIGNED
;
4281 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
4282 /* If one is a null ptr constant the result type
4284 if (is_null_pointer (vtop
))
4286 else if (is_null_pointer (&sv
))
4288 /* XXX: test pointer compatibility, C99 has more elaborate
4292 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
4293 /* XXX: test function pointer compatibility */
4294 type
= bt1
== VT_FUNC
? type1
: type2
;
4295 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
4296 /* XXX: test structure compatibility */
4297 type
= bt1
== VT_STRUCT
? type1
: type2
;
4298 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
4299 /* NOTE: as an extension, we accept void on only one side */
4302 /* integer operations */
4304 /* convert to unsigned if it does not fit in an integer */
4305 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
4306 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
4307 type
.t
|= VT_UNSIGNED
;
4310 /* now we convert second operand */
4312 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4315 if (is_float(type
.t
)) {
4317 #ifdef TCC_TARGET_X86_64
4318 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4322 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
4323 /* for long longs, we use fixed registers to avoid having
4324 to handle a complicated move */
4329 /* this is horrible, but we must also convert first
4333 /* put again first value and cast it */
4336 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4339 move_reg(r2
, r1
, type
.t
);
4346 static void expr_eq(void)
4352 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
4353 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
4354 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
4369 ST_FUNC
void gexpr(void)
4380 /* parse an expression and return its type without any side effect. */
4381 static void expr_type(CType
*type
)
4383 int saved_nocode_wanted
;
4385 saved_nocode_wanted
= nocode_wanted
;
4390 nocode_wanted
= saved_nocode_wanted
;
4393 /* parse a unary expression and return its type without any side
4395 static void unary_type(CType
*type
)
4407 /* parse a constant expression and return value in vtop. */
4408 static void expr_const1(void)
4417 /* parse an integer constant and return its value. */
4418 ST_FUNC
int expr_const(void)
4422 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
4423 expect("constant expression");
4429 /* return the label token if current token is a label, otherwise
4431 static int is_label(void)
4435 /* fast test first */
4436 if (tok
< TOK_UIDENT
)
4438 /* no need to save tokc because tok is an identifier */
4445 unget_tok(last_tok
);
4450 static void label_or_decl(int l
)
4454 /* fast test first */
4455 if (tok
>= TOK_UIDENT
)
4457 /* no need to save tokc because tok is an identifier */
4461 unget_tok(last_tok
);
4464 unget_tok(last_tok
);
4469 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
4470 int case_reg
, int is_expr
)
4473 Sym
*s
, *frame_bottom
;
4475 /* generate line number info */
4476 if (tcc_state
->do_debug
&&
4477 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
4478 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
4480 last_line_num
= file
->line_num
;
4484 /* default return value is (void) */
4486 vtop
->type
.t
= VT_VOID
;
4489 if (tok
== TOK_IF
) {
4496 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4498 if (c
== TOK_ELSE
) {
4502 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4503 gsym(d
); /* patch else jmp */
4506 } else if (tok
== TOK_WHILE
) {
4514 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4518 } else if (tok
== '{') {
4520 int block_vla_sp_loc
, *saved_vla_sp_loc
, saved_vla_flags
;
4523 /* record local declaration stack position */
4525 frame_bottom
= sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4526 frame_bottom
->next
= scope_stack_bottom
;
4527 scope_stack_bottom
= frame_bottom
;
4528 llabel
= local_label_stack
;
4530 /* save VLA state */
4531 block_vla_sp_loc
= *(saved_vla_sp_loc
= vla_sp_loc
);
4532 if (saved_vla_sp_loc
!= &vla_sp_root_loc
)
4533 vla_sp_loc
= &block_vla_sp_loc
;
4535 saved_vla_flags
= vla_flags
;
4536 vla_flags
|= VLA_NEED_NEW_FRAME
;
4538 /* handle local labels declarations */
4539 if (tok
== TOK_LABEL
) {
4542 if (tok
< TOK_UIDENT
)
4543 expect("label identifier");
4544 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
4554 while (tok
!= '}') {
4555 label_or_decl(VT_LOCAL
);
4559 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4562 /* pop locally defined labels */
4563 label_pop(&local_label_stack
, llabel
);
4565 /* XXX: this solution makes only valgrind happy...
4566 triggered by gcc.c-torture/execute/20000917-1.c */
4568 switch(vtop
->type
.t
& VT_BTYPE
) {
4573 for(p
=vtop
->type
.ref
;p
;p
=p
->prev
)
4575 tcc_error("unsupported expression type");
4578 /* pop locally defined symbols */
4579 scope_stack_bottom
= scope_stack_bottom
->next
;
4580 sym_pop(&local_stack
, s
);
4582 /* Pop VLA frames and restore stack pointer if required */
4583 if (saved_vla_sp_loc
!= &vla_sp_root_loc
)
4584 *saved_vla_sp_loc
= block_vla_sp_loc
;
4585 if (vla_sp_loc
!= (saved_vla_sp_loc
== &vla_sp_root_loc
? &vla_sp_root_loc
: &block_vla_sp_loc
)) {
4586 vla_sp_loc
= saved_vla_sp_loc
;
4587 gen_vla_sp_restore(*vla_sp_loc
);
4589 vla_flags
= (vla_flags
& ~VLA_SCOPE_FLAGS
) | (saved_vla_flags
& VLA_SCOPE_FLAGS
);
4592 } else if (tok
== TOK_RETURN
) {
4596 gen_assign_cast(&func_vt
);
4597 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
4598 CType type
, ret_type
;
4600 if (!gfunc_sret(&func_vt
, &ret_type
, &ret_align
)) {
4601 /* if returning structure, must copy it to implicit
4602 first pointer arg location */
4605 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
4608 /* copy structure value to pointer */
4611 /* returning structure packed into registers */
4612 int r
, size
, addr
, offset
, align
;
4613 size
= type_size(&func_vt
,&align
);
4614 if ((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) || (vtop
->c
.i
& (ret_align
-1)))
4615 && (align
& (ret_align
-1))) {
4616 loc
= (loc
- size
) & -align
;
4619 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
4622 vset(&ret_type
, VT_LOCAL
| VT_LVAL
, addr
);
4624 vtop
->type
= ret_type
;
4625 if (is_float(ret_type
.t
))
4626 r
= rc_fret(ret_type
.t
);
4629 /* We assume that when a structure is returned in multiple
4630 registers, their classes are consecutive values of the
4632 for (offset
= 0; offset
< size
; offset
+= ret_align
, r
<<=1) {
4634 vtop
->c
.i
+= ret_align
;
4635 vtop
->r
= VT_LOCAL
| VT_LVAL
;
4638 } else if (is_float(func_vt
.t
)) {
4639 gv(rc_fret(func_vt
.t
));
4643 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
4646 rsym
= gjmp(rsym
); /* jmp */
4647 } else if (tok
== TOK_BREAK
) {
4650 tcc_error("cannot break");
4651 *bsym
= gjmp(*bsym
);
4654 } else if (tok
== TOK_CONTINUE
) {
4657 tcc_error("cannot continue");
4658 *csym
= gjmp(*csym
);
4661 } else if (tok
== TOK_FOR
) {
4666 frame_bottom
= sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4667 frame_bottom
->next
= scope_stack_bottom
;
4668 scope_stack_bottom
= frame_bottom
;
4670 /* c99 for-loop init decl? */
4671 if (!decl0(VT_LOCAL
, 1)) {
4672 /* no, regular for-loop init expr */
4696 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4700 scope_stack_bottom
= scope_stack_bottom
->next
;
4701 sym_pop(&local_stack
, s
);
4703 if (tok
== TOK_DO
) {
4708 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4719 if (tok
== TOK_SWITCH
) {
4723 /* XXX: other types than integer */
4724 case_reg
= gv(RC_INT
);
4728 b
= gjmp(0); /* jump to first case */
4730 block(&a
, csym
, &b
, &c
, case_reg
, 0);
4731 /* if no default, jmp after switch */
4739 if (tok
== TOK_CASE
) {
4746 if (gnu_ext
&& tok
== TOK_DOTS
) {
4750 tcc_warning("empty case range");
4752 /* since a case is like a label, we must skip it with a jmp */
4759 *case_sym
= gtst(1, 0);
4762 *case_sym
= gtst(1, 0);
4766 *case_sym
= gtst(1, *case_sym
);
4771 goto block_after_label
;
4773 if (tok
== TOK_DEFAULT
) {
4779 tcc_error("too many 'default'");
4782 goto block_after_label
;
4784 if (tok
== TOK_GOTO
) {
4786 if (tok
== '*' && gnu_ext
) {
4790 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
4793 } else if (tok
>= TOK_UIDENT
) {
4794 s
= label_find(tok
);
4795 /* put forward definition if needed */
4797 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4799 if (s
->r
== LABEL_DECLARED
)
4800 s
->r
= LABEL_FORWARD
;
4802 /* label already defined */
4803 if (vla_flags
& VLA_IN_SCOPE
) {
4804 /* If VLAs are in use, save the current stack pointer and
4805 reset the stack pointer to what it was at function entry
4806 (label will restore stack pointer in inner scopes) */
4808 gen_vla_sp_restore(vla_sp_root_loc
);
4810 if (s
->r
& LABEL_FORWARD
)
4811 s
->jnext
= gjmp(s
->jnext
);
4813 gjmp_addr(s
->jnext
);
4816 expect("label identifier");
4819 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
4825 if (vla_flags
& VLA_IN_SCOPE
) {
4826 /* save/restore stack pointer across label
4827 this is a no-op when combined with the load immediately
4828 after the label unless we arrive via goto */
4833 if (s
->r
== LABEL_DEFINED
)
4834 tcc_error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
4836 s
->r
= LABEL_DEFINED
;
4838 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
4841 if (vla_flags
& VLA_IN_SCOPE
) {
4842 gen_vla_sp_restore(*vla_sp_loc
);
4843 vla_flags
|= VLA_NEED_NEW_FRAME
;
4845 /* we accept this, but it is a mistake */
4848 tcc_warning("deprecated use of label at end of compound statement");
4852 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4855 /* expression case */
4870 /* t is the array or struct type. c is the array or struct
4871 address. cur_index/cur_field is the pointer to the current
4872 value. 'size_only' is true if only size info is needed (only used
4874 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
4875 int *cur_index
, Sym
**cur_field
,
4879 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
4885 if (gnu_ext
&& (l
= is_label()) != 0)
4887 while (tok
== '[' || tok
== '.') {
4889 if (!(type
->t
& VT_ARRAY
))
4890 expect("array type");
4893 index
= expr_const();
4894 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
4895 expect("invalid index");
4896 if (tok
== TOK_DOTS
&& gnu_ext
) {
4898 index_last
= expr_const();
4899 if (index_last
< 0 ||
4900 (s
->c
>= 0 && index_last
>= s
->c
) ||
4902 expect("invalid index");
4908 *cur_index
= index_last
;
4909 type
= pointed_type(type
);
4910 elem_size
= type_size(type
, &align
);
4911 c
+= index
* elem_size
;
4912 /* NOTE: we only support ranges for last designator */
4913 nb_elems
= index_last
- index
+ 1;
4914 if (nb_elems
!= 1) {
4923 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
4924 expect("struct/union type");
4937 /* XXX: fix this mess by using explicit storage field */
4939 type1
.t
|= (type
->t
& ~VT_TYPE
);
4953 if (type
->t
& VT_ARRAY
) {
4955 type
= pointed_type(type
);
4956 c
+= index
* type_size(type
, &align
);
4960 tcc_error("too many field init");
4961 /* XXX: fix this mess by using explicit storage field */
4963 type1
.t
|= (type
->t
& ~VT_TYPE
);
4968 decl_initializer(type
, sec
, c
, 0, size_only
);
4970 /* XXX: make it more general */
4971 if (!size_only
&& nb_elems
> 1) {
4972 unsigned long c_end
;
4977 tcc_error("range init not supported yet for dynamic storage");
4978 c_end
= c
+ nb_elems
* elem_size
;
4979 if (c_end
> sec
->data_allocated
)
4980 section_realloc(sec
, c_end
);
4981 src
= sec
->data
+ c
;
4983 for(i
= 1; i
< nb_elems
; i
++) {
4985 memcpy(dst
, src
, elem_size
);
4991 #define EXPR_CONST 1
4994 /* store a value or an expression directly in global data or in local array */
4995 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
4996 int v
, int expr_type
)
4998 int saved_global_expr
, bt
, bit_pos
, bit_size
;
5000 unsigned long long bit_mask
;
5008 /* compound literals must be allocated globally in this case */
5009 saved_global_expr
= global_expr
;
5012 global_expr
= saved_global_expr
;
5013 /* NOTE: symbols are accepted */
5014 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
5015 tcc_error("initializer element is not constant");
5023 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
5026 /* XXX: not portable */
5027 /* XXX: generate error if incorrect relocation */
5028 gen_assign_cast(&dtype
);
5029 bt
= type
->t
& VT_BTYPE
;
5030 /* we'll write at most 12 bytes */
5031 if (c
+ 12 > sec
->data_allocated
) {
5032 section_realloc(sec
, c
+ 12);
5034 ptr
= sec
->data
+ c
;
5035 /* XXX: make code faster ? */
5036 if (!(type
->t
& VT_BITFIELD
)) {
5041 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5042 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
5043 bit_mask
= (1LL << bit_size
) - 1;
5045 if ((vtop
->r
& VT_SYM
) &&
5051 (bt
== VT_INT
&& bit_size
!= 32)))
5052 tcc_error("initializer element is not computable at load time");
5055 vtop
->c
.i
= (vtop
->c
.i
!= 0);
5057 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5060 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5063 *(double *)ptr
= vtop
->c
.d
;
5066 *(long double *)ptr
= vtop
->c
.ld
;
5069 *(long long *)ptr
|= (vtop
->c
.ll
& bit_mask
) << bit_pos
;
5072 if (vtop
->r
& VT_SYM
) {
5073 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
5075 *(int *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5080 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
5087 /* put zeros for variable based init */
5088 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
5091 /* nothing to do because globals are already set to zero */
5093 vpush_global_sym(&func_old_type
, TOK_memset
);
5101 /* 't' contains the type and storage info. 'c' is the offset of the
5102 object in section 'sec'. If 'sec' is NULL, it means stack based
5103 allocation. 'first' is true if array '{' must be read (multi
5104 dimension implicit array init handling). 'size_only' is true if
5105 size only evaluation is wanted (only for arrays). */
5106 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
5107 int first
, int size_only
)
5109 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, parlevel1
, i
;
5110 int size1
, align1
, expr_type
;
5114 if (type
->t
& VT_VLA
) {
5117 /* save current stack pointer */
5118 if (vla_flags
& VLA_NEED_NEW_FRAME
) {
5120 vla_flags
= VLA_IN_SCOPE
;
5121 vla_sp_loc
= &vla_sp_loc_tmp
;
5124 vla_runtime_type_size(type
, &a
);
5125 gen_vla_alloc(type
, a
);
5126 vset(type
, VT_LOCAL
|VT_LVAL
, c
);
5130 } else if (type
->t
& VT_ARRAY
) {
5134 t1
= pointed_type(type
);
5135 size1
= type_size(t1
, &align1
);
5138 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
5141 tcc_error("character array initializer must be a literal,"
5142 " optionally enclosed in braces");
5147 /* only parse strings here if correct type (otherwise: handle
5148 them as ((w)char *) expressions */
5149 if ((tok
== TOK_LSTR
&&
5150 #ifdef TCC_TARGET_PE
5151 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
5153 (t1
->t
& VT_BTYPE
) == VT_INT
5155 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
5156 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5161 /* compute maximum number of chars wanted */
5163 cstr_len
= cstr
->size
;
5165 cstr_len
= cstr
->size
/ sizeof(nwchar_t
);
5168 if (n
>= 0 && nb
> (n
- array_length
))
5169 nb
= n
- array_length
;
5172 tcc_warning("initializer-string for array is too long");
5173 /* in order to go faster for common case (char
5174 string in global variable, we handle it
5176 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
5177 memcpy(sec
->data
+ c
+ array_length
, cstr
->data
, nb
);
5181 ch
= ((unsigned char *)cstr
->data
)[i
];
5183 ch
= ((nwchar_t
*)cstr
->data
)[i
];
5184 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
5192 /* only add trailing zero if enough storage (no
5193 warning in this case since it is standard) */
5194 if (n
< 0 || array_length
< n
) {
5196 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
5202 while (tok
!= '}') {
5203 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
5204 if (n
>= 0 && index
>= n
)
5205 tcc_error("index too large");
5206 /* must put zero in holes (note that doing it that way
5207 ensures that it even works with designators) */
5208 if (!size_only
&& array_length
< index
) {
5209 init_putz(t1
, sec
, c
+ array_length
* size1
,
5210 (index
- array_length
) * size1
);
5213 if (index
> array_length
)
5214 array_length
= index
;
5215 /* special test for multi dimensional arrays (may not
5216 be strictly correct if designators are used at the
5218 if (index
>= n
&& no_oblock
)
5227 /* put zeros at the end */
5228 if (!size_only
&& n
>= 0 && array_length
< n
) {
5229 init_putz(t1
, sec
, c
+ array_length
* size1
,
5230 (n
- array_length
) * size1
);
5232 /* patch type size if needed */
5234 s
->c
= array_length
;
5235 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
5236 (sec
|| !first
|| tok
== '{')) {
5239 /* NOTE: the previous test is a specific case for automatic
5240 struct/union init */
5241 /* XXX: union needs only one init */
5243 /* XXX: this test is incorrect for local initializers
5244 beginning with ( without {. It would be much more difficult
5245 to do it correctly (ideally, the expression parser should
5246 be used in all cases) */
5252 while (tok
== '(') {
5256 if (!parse_btype(&type1
, &ad1
))
5258 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
5260 if (!is_assignable_types(type
, &type1
))
5261 tcc_error("invalid type for cast");
5266 if (first
|| tok
== '{') {
5275 while (tok
!= '}') {
5276 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
5278 if (!size_only
&& array_length
< index
) {
5279 init_putz(type
, sec
, c
+ array_length
,
5280 index
- array_length
);
5282 index
= index
+ type_size(&f
->type
, &align1
);
5283 if (index
> array_length
)
5284 array_length
= index
;
5286 /* gr: skip fields from same union - ugly. */
5288 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
5289 /* test for same offset */
5290 if (f
->next
->c
!= f
->c
)
5292 /* if yes, test for bitfield shift */
5293 if ((f
->type
.t
& VT_BITFIELD
) && (f
->next
->type
.t
& VT_BITFIELD
)) {
5294 int bit_pos_1
= (f
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5295 int bit_pos_2
= (f
->next
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5296 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
5297 if (bit_pos_1
!= bit_pos_2
)
5304 if (no_oblock
&& f
== NULL
)
5310 /* put zeros at the end */
5311 if (!size_only
&& array_length
< n
) {
5312 init_putz(type
, sec
, c
+ array_length
,
5321 } else if (tok
== '{') {
5323 decl_initializer(type
, sec
, c
, first
, size_only
);
5325 } else if (size_only
) {
5326 /* just skip expression */
5327 parlevel
= parlevel1
= 0;
5328 while ((parlevel
> 0 || parlevel1
> 0 ||
5329 (tok
!= '}' && tok
!= ',')) && tok
!= -1) {
5332 else if (tok
== ')')
5334 else if (tok
== '{')
5336 else if (tok
== '}')
5341 /* currently, we always use constant expression for globals
5342 (may change for scripting case) */
5343 expr_type
= EXPR_CONST
;
5345 expr_type
= EXPR_ANY
;
5346 init_putv(type
, sec
, c
, 0, expr_type
);
5350 /* parse an initializer for type 't' if 'has_init' is non zero, and
5351 allocate space in local or global data space ('r' is either
5352 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5353 variable 'v' with an associated name represented by 'asm_label' of
5354 scope 'scope' is declared before initializers are parsed. If 'v' is
5355 zero, then a reference to the new object is put in the value stack.
5356 If 'has_init' is 2, a special parsing is done to handle string
5358 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
5359 int has_init
, int v
, char *asm_label
,
5362 int size
, align
, addr
, data_offset
;
5364 ParseState saved_parse_state
= {0};
5365 TokenString init_str
;
5367 Sym
*flexible_array
;
5369 flexible_array
= NULL
;
5370 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
5371 Sym
*field
= type
->ref
->next
;
5374 field
= field
->next
;
5375 if (field
->type
.t
& VT_ARRAY
&& field
->type
.ref
->c
< 0)
5376 flexible_array
= field
;
5380 size
= type_size(type
, &align
);
5381 /* If unknown size, we must evaluate it before
5382 evaluating initializers because
5383 initializers can generate global data too
5384 (e.g. string pointers or ISOC99 compound
5385 literals). It also simplifies local
5386 initializers handling */
5387 tok_str_new(&init_str
);
5388 if (size
< 0 || (flexible_array
&& has_init
)) {
5390 tcc_error("unknown type size");
5391 /* get all init string */
5392 if (has_init
== 2) {
5393 /* only get strings */
5394 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5395 tok_str_add_tok(&init_str
);
5400 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
5402 tcc_error("unexpected end of file in initializer");
5403 tok_str_add_tok(&init_str
);
5406 else if (tok
== '}') {
5416 tok_str_add(&init_str
, -1);
5417 tok_str_add(&init_str
, 0);
5420 save_parse_state(&saved_parse_state
);
5422 macro_ptr
= init_str
.str
;
5424 decl_initializer(type
, NULL
, 0, 1, 1);
5425 /* prepare second initializer parsing */
5426 macro_ptr
= init_str
.str
;
5429 /* if still unknown size, error */
5430 size
= type_size(type
, &align
);
5432 tcc_error("unknown type size");
5435 size
+= flexible_array
->type
.ref
->c
* pointed_size(&flexible_array
->type
);
5436 /* take into account specified alignment if bigger */
5438 if (ad
->aligned
> align
)
5439 align
= ad
->aligned
;
5440 } else if (ad
->packed
) {
5443 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
5445 #ifdef CONFIG_TCC_BCHECK
5446 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5450 loc
= (loc
- size
) & -align
;
5452 #ifdef CONFIG_TCC_BCHECK
5453 /* handles bounds */
5454 /* XXX: currently, since we do only one pass, we cannot track
5455 '&' operators, so we add only arrays */
5456 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5457 unsigned long *bounds_ptr
;
5458 /* add padding between regions */
5460 /* then add local bound info */
5461 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(unsigned long));
5462 bounds_ptr
[0] = addr
;
5463 bounds_ptr
[1] = size
;
5467 /* local variable */
5468 sym_push(v
, type
, r
, addr
);
5470 /* push local reference */
5471 vset(type
, r
, addr
);
5477 if (v
&& scope
== VT_CONST
) {
5478 /* see if the symbol was already defined */
5481 if (!is_compatible_types(&sym
->type
, type
))
5482 tcc_error("incompatible types for redefinition of '%s'",
5483 get_tok_str(v
, NULL
));
5484 if (sym
->type
.t
& VT_EXTERN
) {
5485 /* if the variable is extern, it was not allocated */
5486 sym
->type
.t
&= ~VT_EXTERN
;
5487 /* set array size if it was ommited in extern
5489 if ((sym
->type
.t
& VT_ARRAY
) &&
5490 sym
->type
.ref
->c
< 0 &&
5492 sym
->type
.ref
->c
= type
->ref
->c
;
5494 /* we accept several definitions of the same
5495 global variable. this is tricky, because we
5496 must play with the SHN_COMMON type of the symbol */
5497 /* XXX: should check if the variable was already
5498 initialized. It is incorrect to initialized it
5500 /* no init data, we won't add more to the symbol */
5507 /* allocate symbol in corresponding section */
5512 else if (tcc_state
->nocommon
)
5516 data_offset
= sec
->data_offset
;
5517 data_offset
= (data_offset
+ align
- 1) & -align
;
5519 /* very important to increment global pointer at this time
5520 because initializers themselves can create new initializers */
5521 data_offset
+= size
;
5522 #ifdef CONFIG_TCC_BCHECK
5523 /* add padding if bound check */
5524 if (tcc_state
->do_bounds_check
)
5527 sec
->data_offset
= data_offset
;
5528 /* allocate section space to put the data */
5529 if (sec
->sh_type
!= SHT_NOBITS
&&
5530 data_offset
> sec
->data_allocated
)
5531 section_realloc(sec
, data_offset
);
5532 /* align section if needed */
5533 if (align
> sec
->sh_addralign
)
5534 sec
->sh_addralign
= align
;
5536 addr
= 0; /* avoid warning */
5540 if (scope
!= VT_CONST
|| !sym
) {
5541 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
5542 sym
->asm_label
= asm_label
;
5544 /* update symbol definition */
5546 put_extern_sym(sym
, sec
, addr
, size
);
5549 /* put a common area */
5550 put_extern_sym(sym
, NULL
, align
, size
);
5551 /* XXX: find a nicer way */
5552 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
5553 esym
->st_shndx
= SHN_COMMON
;
5558 /* push global reference */
5559 sym
= get_sym_ref(type
, sec
, addr
, size
);
5561 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
5564 /* patch symbol weakness */
5565 if (type
->t
& VT_WEAK
)
5567 #ifdef CONFIG_TCC_BCHECK
5568 /* handles bounds now because the symbol must be defined
5569 before for the relocation */
5570 if (tcc_state
->do_bounds_check
) {
5571 unsigned long *bounds_ptr
;
5573 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
5574 /* then add global bound info */
5575 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(long));
5576 bounds_ptr
[0] = 0; /* relocated */
5577 bounds_ptr
[1] = size
;
5581 if (has_init
|| (type
->t
& VT_VLA
)) {
5582 decl_initializer(type
, sec
, addr
, 1, 0);
5583 /* restore parse state if needed */
5585 tok_str_free(init_str
.str
);
5586 restore_parse_state(&saved_parse_state
);
5588 /* patch flexible array member size back to -1, */
5589 /* for possible subsequent similar declarations */
5591 flexible_array
->type
.ref
->c
= -1;
5596 static void put_func_debug(Sym
*sym
)
5601 /* XXX: we put here a dummy type */
5602 snprintf(buf
, sizeof(buf
), "%s:%c1",
5603 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
5604 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
5605 cur_text_section
, sym
->c
);
5606 /* //gr gdb wants a line at the function */
5607 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
5612 /* parse an old style function declaration list */
5613 /* XXX: check multiple parameter */
5614 static void func_decl_list(Sym
*func_sym
)
5621 /* parse each declaration */
5622 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
&&
5623 tok
!= TOK_ASM1
&& tok
!= TOK_ASM2
&& tok
!= TOK_ASM3
) {
5624 if (!parse_btype(&btype
, &ad
))
5625 expect("declaration list");
5626 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5627 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5629 /* we accept no variable after */
5633 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5634 /* find parameter in function parameter list */
5637 if ((s
->v
& ~SYM_FIELD
) == v
)
5641 tcc_error("declaration for parameter '%s' but no such parameter",
5642 get_tok_str(v
, NULL
));
5644 /* check that no storage specifier except 'register' was given */
5645 if (type
.t
& VT_STORAGE
)
5646 tcc_error("storage class specified for '%s'", get_tok_str(v
, NULL
));
5647 convert_parameter_type(&type
);
5648 /* we can add the type (NOTE: it could be local to the function) */
5650 /* accept other parameters */
5661 /* parse a function defined by symbol 'sym' and generate its code in
5662 'cur_text_section' */
5663 static void gen_function(Sym
*sym
)
5665 int saved_nocode_wanted
= nocode_wanted
;
5667 ind
= cur_text_section
->data_offset
;
5668 /* NOTE: we patch the symbol size later */
5669 put_extern_sym(sym
, cur_text_section
, ind
, 0);
5670 funcname
= get_tok_str(sym
->v
, NULL
);
5672 /* Initialize VLA state */
5673 vla_sp_loc
= &vla_sp_root_loc
;
5674 vla_flags
= VLA_NEED_NEW_FRAME
;
5675 /* put debug symbol */
5676 if (tcc_state
->do_debug
)
5677 put_func_debug(sym
);
5678 /* push a dummy symbol to enable local sym storage */
5679 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
5680 gfunc_prolog(&sym
->type
);
5682 block(NULL
, NULL
, NULL
, NULL
, 0, 0);
5685 cur_text_section
->data_offset
= ind
;
5686 label_pop(&global_label_stack
, NULL
);
5687 /* reset local stack */
5688 scope_stack_bottom
= NULL
;
5689 sym_pop(&local_stack
, NULL
);
5690 /* end of function */
5691 /* patch symbol size */
5692 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
5694 /* patch symbol weakness (this definition overrules any prototype) */
5695 if (sym
->type
.t
& VT_WEAK
)
5697 if (tcc_state
->do_debug
) {
5698 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
5700 /* It's better to crash than to generate wrong code */
5701 cur_text_section
= NULL
;
5702 funcname
= ""; /* for safety */
5703 func_vt
.t
= VT_VOID
; /* for safety */
5704 ind
= 0; /* for safety */
5705 nocode_wanted
= saved_nocode_wanted
;
5708 ST_FUNC
void gen_inline_functions(void)
5711 int *str
, inline_generated
, i
;
5712 struct InlineFunc
*fn
;
5714 /* iterate while inline function are referenced */
5716 inline_generated
= 0;
5717 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5718 fn
= tcc_state
->inline_fns
[i
];
5720 if (sym
&& sym
->c
) {
5721 /* the function was used: generate its code and
5722 convert it to a normal function */
5723 str
= fn
->token_str
;
5726 pstrcpy(file
->filename
, sizeof file
->filename
, fn
->filename
);
5727 sym
->r
= VT_SYM
| VT_CONST
;
5728 sym
->type
.t
&= ~VT_INLINE
;
5732 cur_text_section
= text_section
;
5734 macro_ptr
= NULL
; /* fail safe */
5736 inline_generated
= 1;
5739 if (!inline_generated
)
5742 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5743 fn
= tcc_state
->inline_fns
[i
];
5744 str
= fn
->token_str
;
5747 dynarray_reset(&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
);
5750 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
5751 static int decl0(int l
, int is_for_loop_init
)
5759 if (!parse_btype(&btype
, &ad
)) {
5760 if (is_for_loop_init
)
5762 /* skip redundant ';' */
5763 /* XXX: find more elegant solution */
5768 if (l
== VT_CONST
&&
5769 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5770 /* global asm block */
5774 /* special test for old K&R protos without explicit int
5775 type. Only accepted when defining global data */
5776 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
5780 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5781 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5783 /* we accept no variable after */
5787 while (1) { /* iterate thru each declaration */
5788 char *asm_label
; // associated asm label
5790 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5794 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
5795 printf("type = '%s'\n", buf
);
5798 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5799 if ((type
.t
& VT_STATIC
) && (l
== VT_LOCAL
)) {
5800 tcc_error("function without file scope cannot be static");
5802 /* if old style function prototype, we accept a
5805 if (sym
->c
== FUNC_OLD
)
5806 func_decl_list(sym
);
5810 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5813 asm_label_instr(&astr
);
5814 asm_label
= tcc_strdup(astr
.data
);
5817 /* parse one last attribute list, after asm label */
5818 parse_attribute(&ad
);
5823 #ifdef TCC_TARGET_PE
5825 type
.t
|= VT_IMPORT
;
5827 type
.t
|= VT_EXPORT
;
5831 tcc_error("cannot use local functions");
5832 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
5833 expect("function definition");
5835 /* reject abstract declarators in function definition */
5837 while ((sym
= sym
->next
) != NULL
)
5838 if (!(sym
->v
& ~SYM_FIELD
))
5839 expect("identifier");
5841 /* XXX: cannot do better now: convert extern line to static inline */
5842 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
5843 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5847 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
5850 r
= sym
->type
.ref
->r
;
5853 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
5855 /* use func_call from prototype if not defined */
5856 if (FUNC_CALL(r
) != FUNC_CDECL
5857 && FUNC_CALL(type
.ref
->r
) == FUNC_CDECL
)
5858 FUNC_CALL(type
.ref
->r
) = FUNC_CALL(r
);
5860 /* use export from prototype */
5862 FUNC_EXPORT(type
.ref
->r
) = 1;
5864 /* use static from prototype */
5865 if (sym
->type
.t
& VT_STATIC
)
5866 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5868 if (!is_compatible_types(&sym
->type
, &type
)) {
5870 tcc_error("incompatible types for redefinition of '%s'",
5871 get_tok_str(v
, NULL
));
5873 FUNC_PROTO(type
.ref
->r
) = 0;
5874 /* if symbol is already defined, then put complete type */
5877 /* put function symbol */
5878 sym
= global_identifier_push(v
, type
.t
, 0);
5879 sym
->type
.ref
= type
.ref
;
5882 /* static inline functions are just recorded as a kind
5883 of macro. Their code will be emitted at the end of
5884 the compilation unit only if they are used */
5885 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
5886 (VT_INLINE
| VT_STATIC
)) {
5887 TokenString func_str
;
5889 struct InlineFunc
*fn
;
5890 const char *filename
;
5892 tok_str_new(&func_str
);
5898 tcc_error("unexpected end of file");
5899 tok_str_add_tok(&func_str
);
5904 } else if (t
== '}') {
5906 if (block_level
== 0)
5910 tok_str_add(&func_str
, -1);
5911 tok_str_add(&func_str
, 0);
5912 filename
= file
? file
->filename
: "";
5913 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
5914 strcpy(fn
->filename
, filename
);
5916 fn
->token_str
= func_str
.str
;
5917 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
5920 /* compute text section */
5921 cur_text_section
= ad
.section
;
5922 if (!cur_text_section
)
5923 cur_text_section
= text_section
;
5924 sym
->r
= VT_SYM
| VT_CONST
;
5929 if (btype
.t
& VT_TYPEDEF
) {
5930 /* save typedefed type */
5931 /* XXX: test storage specifiers ? */
5932 sym
= sym_push(v
, &type
, INT_ATTR(&ad
), 0);
5933 sym
->type
.t
|= VT_TYPEDEF
;
5936 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5937 /* external function definition */
5938 /* specific case for func_call attribute */
5940 type
.ref
->r
= INT_ATTR(&ad
);
5941 } else if (!(type
.t
& VT_ARRAY
)) {
5942 /* not lvalue if array */
5943 r
|= lvalue_type(type
.t
);
5945 has_init
= (tok
== '=');
5946 if (has_init
&& (type
.t
& VT_VLA
))
5947 tcc_error("Variable length array cannot be initialized");
5948 if ((btype
.t
& VT_EXTERN
) || ((type
.t
& VT_BTYPE
) == VT_FUNC
) ||
5949 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
5950 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
5951 /* external variable or function */
5952 /* NOTE: as GCC, uninitialized global static
5953 arrays of null size are considered as
5955 sym
= external_sym(v
, &type
, r
, asm_label
);
5957 if (type
.t
& VT_WEAK
)
5960 if (ad
.alias_target
) {
5965 alias_target
= sym_find(ad
.alias_target
);
5966 if (!alias_target
|| !alias_target
->c
)
5967 tcc_error("unsupported forward __alias__ attribute");
5968 esym
= &((Elf32_Sym
*)symtab_section
->data
)[alias_target
->c
];
5969 tsec
.sh_num
= esym
->st_shndx
;
5970 put_extern_sym2(sym
, &tsec
, esym
->st_value
, esym
->st_size
, 0);
5973 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
5974 if (type
.t
& VT_STATIC
)
5980 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, asm_label
, l
);
5984 if (is_for_loop_init
)
5997 ST_FUNC
void decl(int l
)