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 SValue __vstack
[1+VSTACK_SIZE
], *vtop
;
60 ST_DATA
int const_wanted
; /* true if constant wanted */
61 ST_DATA
int nocode_wanted
; /* true if no code generation wanted for an expression */
62 ST_DATA
int global_expr
; /* true if compound literals must be allocated globally (used during initializers parsing */
63 ST_DATA CType func_vt
; /* current function return type (used by return instruction) */
65 ST_DATA
int last_line_num
, last_ind
, func_ind
; /* debug last line number and pc */
66 ST_DATA
char *funcname
;
68 ST_DATA CType char_pointer_type
, func_old_type
, int_type
, size_type
;
70 /* ------------------------------------------------------------------------- */
71 static void gen_cast(CType
*type
);
72 static inline CType
*pointed_type(CType
*type
);
73 static int is_compatible_types(CType
*type1
, CType
*type2
);
74 static int parse_btype(CType
*type
, AttributeDef
*ad
);
75 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
);
76 static void parse_expr_type(CType
*type
);
77 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
, int first
, int size_only
);
78 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
, int case_reg
, int is_expr
);
79 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
, int has_init
, int v
, char *asm_label
, int scope
);
80 static int decl0(int l
, int is_for_loop_init
);
81 static void expr_eq(void);
82 static void unary_type(CType
*type
);
83 static void vla_runtime_type_size(CType
*type
, int *a
);
84 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
);
85 static void expr_type(CType
*type
);
87 ST_INLN
int is_float(int t
)
91 return bt
== VT_LDOUBLE
|| bt
== VT_DOUBLE
|| bt
== VT_FLOAT
;
94 /* we use our own 'finite' function to avoid potential problems with
95 non standard math libs */
96 /* XXX: endianness dependent */
97 ST_FUNC
int ieee_finite(double d
)
100 return ((unsigned)((p
[1] | 0x800fffff) + 1)) >> 31;
103 ST_FUNC
void test_lvalue(void)
105 if (!(vtop
->r
& VT_LVAL
))
109 /* ------------------------------------------------------------------------- */
110 /* symbol allocator */
111 static Sym
*__sym_malloc(void)
113 Sym
*sym_pool
, *sym
, *last_sym
;
116 sym_pool
= tcc_malloc(SYM_POOL_NB
* sizeof(Sym
));
117 dynarray_add(&sym_pools
, &nb_sym_pools
, sym_pool
);
119 last_sym
= sym_free_first
;
121 for(i
= 0; i
< SYM_POOL_NB
; i
++) {
122 sym
->next
= last_sym
;
126 sym_free_first
= last_sym
;
130 static inline Sym
*sym_malloc(void)
133 sym
= sym_free_first
;
135 sym
= __sym_malloc();
136 sym_free_first
= sym
->next
;
140 ST_INLN
void sym_free(Sym
*sym
)
142 sym
->next
= sym_free_first
;
143 tcc_free(sym
->asm_label
);
144 sym_free_first
= sym
;
147 /* push, without hashing */
148 ST_FUNC Sym
*sym_push2(Sym
**ps
, int v
, int t
, long c
)
151 if (ps
== &local_stack
) {
152 for (s
= *ps
; s
&& s
!= scope_stack_bottom
; s
= s
->prev
)
153 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
&& s
->v
== v
)
154 tcc_error("incompatible types for redefinition of '%s'",
155 get_tok_str(v
, NULL
));
174 /* find a symbol and return its associated structure. 's' is the top
175 of the symbol stack */
176 ST_FUNC Sym
*sym_find2(Sym
*s
, int v
)
186 /* structure lookup */
187 ST_INLN Sym
*struct_find(int v
)
190 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
192 return table_ident
[v
]->sym_struct
;
195 /* find an identifier */
196 ST_INLN Sym
*sym_find(int v
)
199 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
201 return table_ident
[v
]->sym_identifier
;
204 /* push a given symbol on the symbol stack */
205 ST_FUNC Sym
*sym_push(int v
, CType
*type
, int r
, int c
)
214 s
= sym_push2(ps
, v
, type
->t
, c
);
215 s
->type
.ref
= type
->ref
;
217 /* don't record fields or anonymous symbols */
219 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
220 /* record symbol in token array */
221 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
223 ps
= &ts
->sym_struct
;
225 ps
= &ts
->sym_identifier
;
232 /* push a global identifier */
233 ST_FUNC Sym
*global_identifier_push(int v
, int t
, int c
)
236 s
= sym_push2(&global_stack
, v
, t
, c
);
237 /* don't record anonymous symbol */
238 if (v
< SYM_FIRST_ANOM
) {
239 ps
= &table_ident
[v
- TOK_IDENT
]->sym_identifier
;
240 /* modify the top most local identifier, so that
241 sym_identifier will point to 's' when popped */
243 ps
= &(*ps
)->prev_tok
;
250 /* pop symbols until top reaches 'b' */
251 ST_FUNC
void sym_pop(Sym
**ptop
, Sym
*b
)
261 /* remove symbol in token array */
263 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
264 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
266 ps
= &ts
->sym_struct
;
268 ps
= &ts
->sym_identifier
;
277 static void weaken_symbol(Sym
*sym
)
279 sym
->type
.t
|= VT_WEAK
;
284 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
285 esym_type
= ELFW(ST_TYPE
)(esym
->st_info
);
286 esym
->st_info
= ELFW(ST_INFO
)(STB_WEAK
, esym_type
);
290 /* ------------------------------------------------------------------------- */
292 ST_FUNC
void swap(int *p
, int *q
)
300 static void vsetc(CType
*type
, int r
, CValue
*vc
)
304 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
305 tcc_error("memory full");
306 /* cannot let cpu flags if other instruction are generated. Also
307 avoid leaving VT_JMP anywhere except on the top of the stack
308 because it would complicate the code generator. */
309 if (vtop
>= vstack
) {
310 v
= vtop
->r
& VT_VALMASK
;
311 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
321 /* push constant of type "type" with useless value */
322 void vpush(CType
*type
)
325 vsetc(type
, VT_CONST
, &cval
);
328 /* push integer constant */
329 ST_FUNC
void vpushi(int v
)
333 vsetc(&int_type
, VT_CONST
, &cval
);
336 /* push a pointer sized constant */
337 static void vpushs(long long v
)
344 vsetc(&size_type
, VT_CONST
, &cval
);
347 /* push long long constant */
348 static void vpushll(long long v
)
355 vsetc(&ctype
, VT_CONST
, &cval
);
358 /* push arbitrary 64bit constant */
359 void vpush64(int ty
, unsigned long long v
)
365 vsetc(&ctype
, VT_CONST
, &cval
);
368 /* Return a static symbol pointing to a section */
369 ST_FUNC Sym
*get_sym_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
375 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
376 sym
->type
.ref
= type
->ref
;
377 sym
->r
= VT_CONST
| VT_SYM
;
378 put_extern_sym(sym
, sec
, offset
, size
);
382 /* push a reference to a section offset by adding a dummy symbol */
383 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
388 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
389 vtop
->sym
= get_sym_ref(type
, sec
, offset
, size
);
392 /* define a new external reference to a symbol 'v' of type 'u' */
393 ST_FUNC Sym
*external_global_sym(int v
, CType
*type
, int r
)
399 /* push forward reference */
400 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
401 s
->type
.ref
= type
->ref
;
402 s
->r
= r
| VT_CONST
| VT_SYM
;
407 /* define a new external reference to a symbol 'v' with alternate asm
408 name 'asm_label' of type 'u'. 'asm_label' is equal to NULL if there
409 is no alternate name (most cases) */
410 static Sym
*external_sym(int v
, CType
*type
, int r
, char *asm_label
)
416 /* push forward reference */
417 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
418 s
->asm_label
= asm_label
;
419 s
->type
.t
|= VT_EXTERN
;
420 } else if (s
->type
.ref
== func_old_type
.ref
) {
421 s
->type
.ref
= type
->ref
;
422 s
->r
= r
| VT_CONST
| VT_SYM
;
423 s
->type
.t
|= VT_EXTERN
;
424 } else if (!is_compatible_types(&s
->type
, type
)) {
425 tcc_error("incompatible types for redefinition of '%s'",
426 get_tok_str(v
, NULL
));
431 /* push a reference to global symbol v */
432 ST_FUNC
void vpush_global_sym(CType
*type
, int v
)
437 sym
= external_global_sym(v
, type
, 0);
439 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
443 ST_FUNC
void vset(CType
*type
, int r
, int v
)
448 vsetc(type
, r
, &cval
);
451 static void vseti(int r
, int v
)
459 ST_FUNC
void vswap(void)
461 /* cannot let cpu flags if other instruction are generated. Also
462 avoid leaving VT_JMP anywhere except on the top of the stack
463 because it would complicate the code generator. */
464 if (vtop
>= vstack
) {
465 int v
= vtop
->r
& VT_VALMASK
;
466 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
471 * vtop[0], vtop[-1] = vtop[-1], vtop[0]
473 * vswap is called often and exchanging vtop[0] vs vtop[-1] is hot on
474 * profile, so it is hand optimized
476 unsigned long *vtopl
= (unsigned long *)vtop
;
477 # define VSIZEL (sizeof(*vtop) / sizeof(*vtopl))
479 _STATIC_ASSERT( VSIZEL
*sizeof(*vtopl
) == sizeof(*vtop
) );
480 _STATIC_ASSERT( VSIZEL
<= 16 ); /* should be enough */
484 unsigned long tmpl; \
486 vtopl[i] = vtopl[-1*VSIZEL + i]; \
487 vtopl[-1*VSIZEL + i] = tmpl; \
490 VSWAPL(15); VSWAPL(14); VSWAPL(13); VSWAPL(12);
491 VSWAPL(11); VSWAPL(10); VSWAPL( 9); VSWAPL( 8);
492 VSWAPL( 7); VSWAPL( 6); VSWAPL( 5); VSWAPL( 4);
493 VSWAPL( 3); VSWAPL( 2); VSWAPL( 1); VSWAPL( 0);
500 ST_FUNC
void vpushv(SValue
*v
)
502 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
503 tcc_error("memory full");
508 static void vdup(void)
513 /* save r to the memory stack, and mark it as being free */
514 ST_FUNC
void save_reg(int r
)
516 int l
, saved
, size
, align
;
520 /* modify all stack values */
523 for(p
=vstack
;p
<=vtop
;p
++) {
524 if ((p
->r
& VT_VALMASK
) == r
||
525 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
526 /* must save value on stack if not already done */
528 /* NOTE: must reload 'r' because r might be equal to r2 */
529 r
= p
->r
& VT_VALMASK
;
530 /* store register in the stack */
532 if ((p
->r
& VT_LVAL
) ||
533 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
534 #ifdef TCC_TARGET_X86_64
535 type
= &char_pointer_type
;
539 size
= type_size(type
, &align
);
540 loc
= (loc
- size
) & -align
;
542 sv
.r
= VT_LOCAL
| VT_LVAL
;
545 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
546 /* x86 specific: need to pop fp register ST0 if saved */
548 o(0xd8dd); /* fstp %st(0) */
551 #ifndef TCC_TARGET_X86_64
552 /* special long long case */
553 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
561 /* mark that stack entry as being saved on the stack */
562 if (p
->r
& VT_LVAL
) {
563 /* also clear the bounded flag because the
564 relocation address of the function was stored in
566 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
568 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
576 #ifdef TCC_TARGET_ARM
577 /* find a register of class 'rc2' with at most one reference on stack.
578 * If none, call get_reg(rc) */
579 ST_FUNC
int get_reg_ex(int rc
, int rc2
)
584 for(r
=0;r
<NB_REGS
;r
++) {
585 if (reg_classes
[r
] & rc2
) {
588 for(p
= vstack
; p
<= vtop
; p
++) {
589 if ((p
->r
& VT_VALMASK
) == r
||
590 (p
->r2
& VT_VALMASK
) == r
)
601 /* find a free register of class 'rc'. If none, save one register */
602 ST_FUNC
int get_reg(int rc
)
607 /* find a free register */
608 for(r
=0;r
<NB_REGS
;r
++) {
609 if (reg_classes
[r
] & rc
) {
610 for(p
=vstack
;p
<=vtop
;p
++) {
611 if ((p
->r
& VT_VALMASK
) == r
||
612 (p
->r2
& VT_VALMASK
) == r
)
620 /* no register left : free the first one on the stack (VERY
621 IMPORTANT to start from the bottom to ensure that we don't
622 spill registers used in gen_opi()) */
623 for(p
=vstack
;p
<=vtop
;p
++) {
624 /* look at second register (if long long) */
625 r
= p
->r2
& VT_VALMASK
;
626 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
628 r
= p
->r
& VT_VALMASK
;
629 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
635 /* Should never comes here */
639 /* save registers up to (vtop - n) stack entry */
640 ST_FUNC
void save_regs(int n
)
645 for(p
= vstack
;p
<= p1
; p
++) {
646 r
= p
->r
& VT_VALMASK
;
653 /* move register 's' to 'r', and flush previous value of r to memory
655 static void move_reg(int r
, int s
)
668 /* get address of vtop (vtop MUST BE an lvalue) */
669 static void gaddrof(void)
671 if (vtop
->r
& VT_REF
)
674 /* tricky: if saved lvalue, then we can go back to lvalue */
675 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
676 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
681 #ifdef CONFIG_TCC_BCHECK
682 /* generate lvalue bound code */
683 static void gbound(void)
688 vtop
->r
&= ~VT_MUSTBOUND
;
689 /* if lvalue, then use checking code before dereferencing */
690 if (vtop
->r
& VT_LVAL
) {
691 /* if not VT_BOUNDED value, then make one */
692 if (!(vtop
->r
& VT_BOUNDED
)) {
693 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
694 /* must save type because we must set it to int to get pointer */
696 vtop
->type
.t
= VT_INT
;
699 gen_bounded_ptr_add();
700 vtop
->r
|= lval_type
;
703 /* then check for dereferencing */
704 gen_bounded_ptr_deref();
709 /* store vtop a register belonging to class 'rc'. lvalues are
710 converted to values. Cannot be used if cannot be converted to
711 register value (such as structures). */
712 ST_FUNC
int gv(int rc
)
714 int r
, bit_pos
, bit_size
, size
, align
, i
;
715 #ifndef TCC_TARGET_X86_64
719 /* NOTE: get_reg can modify vstack[] */
720 if (vtop
->type
.t
& VT_BITFIELD
) {
723 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
724 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
725 /* remove bit field info to avoid loops */
726 vtop
->type
.t
&= ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
727 /* cast to int to propagate signedness in following ops */
728 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
733 if((vtop
->type
.t
& VT_UNSIGNED
) ||
734 (vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
735 type
.t
|= VT_UNSIGNED
;
737 /* generate shifts */
738 vpushi(bits
- (bit_pos
+ bit_size
));
740 vpushi(bits
- bit_size
);
741 /* NOTE: transformed to SHR if unsigned */
745 if (is_float(vtop
->type
.t
) &&
746 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
749 unsigned long offset
;
750 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
754 /* XXX: unify with initializers handling ? */
755 /* CPUs usually cannot use float constants, so we store them
756 generically in data segment */
757 size
= type_size(&vtop
->type
, &align
);
758 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
759 data_section
->data_offset
= offset
;
760 /* XXX: not portable yet */
761 #if defined(__i386__) || defined(__x86_64__)
762 /* Zero pad x87 tenbyte long doubles */
763 if (size
== LDOUBLE_SIZE
) {
764 vtop
->c
.tab
[2] &= 0xffff;
765 #if LDOUBLE_SIZE == 16
770 ptr
= section_ptr_add(data_section
, size
);
772 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
776 ptr
[i
] = vtop
->c
.tab
[size
-1-i
];
780 ptr
[i
] = vtop
->c
.tab
[i
];
781 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
782 vtop
->r
|= VT_LVAL
| VT_SYM
;
786 #ifdef CONFIG_TCC_BCHECK
787 if (vtop
->r
& VT_MUSTBOUND
)
791 r
= vtop
->r
& VT_VALMASK
;
792 #ifndef TCC_TARGET_X86_64
797 /* need to reload if:
799 - lvalue (need to dereference pointer)
800 - already a register, but not in the right class */
802 || (vtop
->r
& VT_LVAL
)
803 || !(reg_classes
[r
] & rc
)
804 #ifndef TCC_TARGET_X86_64
805 || ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
810 #ifndef TCC_TARGET_X86_64
811 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
813 unsigned long long ll
;
814 /* two register type load : expand to two words
816 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
819 vtop
->c
.ui
= ll
; /* first word */
821 vtop
->r
= r
; /* save register value */
822 vpushi(ll
>> 32); /* second word */
823 } else if (r
>= VT_CONST
|| /* XXX: test to VT_CONST incorrect ? */
824 (vtop
->r
& VT_LVAL
)) {
825 /* We do not want to modifier the long long
826 pointer here, so the safest (and less
827 efficient) is to save all the other registers
828 in the stack. XXX: totally inefficient. */
830 /* load from memory */
833 vtop
[-1].r
= r
; /* save register value */
834 /* increment pointer to get second word */
835 vtop
->type
.t
= VT_INT
;
844 vtop
[-1].r
= r
; /* save register value */
845 vtop
->r
= vtop
[-1].r2
;
847 /* Allocate second register. Here we rely on the fact that
848 get_reg() tries first to free r2 of an SValue. */
852 /* write second register */
856 if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
858 /* lvalue of scalar type : need to use lvalue type
859 because of possible cast */
862 /* compute memory access type */
863 if (vtop
->r
& VT_LVAL_BYTE
)
865 else if (vtop
->r
& VT_LVAL_SHORT
)
867 if (vtop
->r
& VT_LVAL_UNSIGNED
)
871 /* restore wanted type */
874 /* one register type load */
879 #ifdef TCC_TARGET_C67
880 /* uses register pairs for doubles */
881 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
888 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
889 ST_FUNC
void gv2(int rc1
, int rc2
)
893 /* generate more generic register first. But VT_JMP or VT_CMP
894 values must be generated first in all cases to avoid possible
896 v
= vtop
[0].r
& VT_VALMASK
;
897 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
902 /* test if reload is needed for first register */
903 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
913 /* test if reload is needed for first register */
914 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
920 /* wrapper around RC_FRET to return a register by type */
921 static int rc_fret(int t
)
923 #ifdef TCC_TARGET_X86_64
924 if (t
== VT_LDOUBLE
) {
931 /* wrapper around REG_FRET to return a register by type */
932 static int reg_fret(int t
)
934 #ifdef TCC_TARGET_X86_64
935 if (t
== VT_LDOUBLE
) {
942 /* expand long long on stack in two int registers */
943 static void lexpand(void)
947 u
= vtop
->type
.t
& VT_UNSIGNED
;
950 vtop
[0].r
= vtop
[-1].r2
;
951 vtop
[0].r2
= VT_CONST
;
952 vtop
[-1].r2
= VT_CONST
;
953 vtop
[0].type
.t
= VT_INT
| u
;
954 vtop
[-1].type
.t
= VT_INT
| u
;
957 #ifdef TCC_TARGET_ARM
958 /* expand long long on stack */
959 ST_FUNC
void lexpand_nr(void)
963 u
= vtop
->type
.t
& VT_UNSIGNED
;
966 vtop
->type
.t
= VT_INT
| u
;
967 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
969 vtop
[-1].c
.ui
= vtop
->c
.ull
;
970 vtop
->c
.ui
= vtop
->c
.ull
>> 32;
972 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
974 vtop
->r
= vtop
[-1].r
;
975 } else if (v
> VT_CONST
) {
979 vtop
->r
= vtop
[-1].r2
;
980 vtop
[-1].r2
= VT_CONST
;
981 vtop
[-1].type
.t
= VT_INT
| u
;
985 /* build a long long from two ints */
986 static void lbuild(int t
)
989 vtop
[-1].r2
= vtop
[0].r
;
994 /* rotate n first stack elements to the bottom
995 I1 ... In -> I2 ... In I1 [top is right]
997 ST_FUNC
void vrotb(int n
)
1003 for(i
=-n
+1;i
!=0;i
++)
1004 vtop
[i
] = vtop
[i
+1];
1008 /* rotate the n elements before entry e towards the top
1009 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
1011 ST_FUNC
void vrote(SValue
*e
, int n
)
1017 for(i
= 0;i
< n
- 1; i
++)
1022 /* rotate n first stack elements to the top
1023 I1 ... In -> In I1 ... I(n-1) [top is right]
1025 ST_FUNC
void vrott(int n
)
1030 /* pop stack value */
1031 ST_FUNC
void vpop(void)
1034 v
= vtop
->r
& VT_VALMASK
;
1035 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1036 /* for x86, we need to pop the FP stack */
1037 if (v
== TREG_ST0
&& !nocode_wanted
) {
1038 o(0xd8dd); /* fstp %st(0) */
1041 if (v
== VT_JMP
|| v
== VT_JMPI
) {
1042 /* need to put correct jump if && or || without test */
1048 /* convert stack entry to register and duplicate its value in another
1050 static void gv_dup(void)
1056 if ((t
& VT_BTYPE
) == VT_LLONG
) {
1063 /* stack: H L L1 H1 */
1071 /* duplicate value */
1076 #ifdef TCC_TARGET_X86_64
1077 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1087 load(r1
, &sv
); /* move r to r1 */
1089 /* duplicates value */
1095 #ifndef TCC_TARGET_X86_64
1096 /* generate CPU independent (unsigned) long long operations */
1097 static void gen_opl(int op
)
1099 int t
, a
, b
, op1
, c
, i
;
1101 unsigned short reg_iret
= REG_IRET
;
1102 unsigned short reg_lret
= REG_LRET
;
1108 func
= TOK___divdi3
;
1111 func
= TOK___udivdi3
;
1114 func
= TOK___moddi3
;
1117 func
= TOK___umoddi3
;
1124 /* call generic long long function */
1125 vpush_global_sym(&func_old_type
, func
);
1130 vtop
->r2
= reg_lret
;
1143 /* stack: L1 H1 L2 H2 */
1148 vtop
[-2] = vtop
[-3];
1151 /* stack: H1 H2 L1 L2 */
1157 /* stack: H1 H2 L1 L2 ML MH */
1160 /* stack: ML MH H1 H2 L1 L2 */
1164 /* stack: ML MH H1 L2 H2 L1 */
1169 /* stack: ML MH M1 M2 */
1172 } else if (op
== '+' || op
== '-') {
1173 /* XXX: add non carry method too (for MIPS or alpha) */
1179 /* stack: H1 H2 (L1 op L2) */
1182 gen_op(op1
+ 1); /* TOK_xxxC2 */
1185 /* stack: H1 H2 (L1 op L2) */
1188 /* stack: (L1 op L2) H1 H2 */
1190 /* stack: (L1 op L2) (H1 op H2) */
1198 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1199 t
= vtop
[-1].type
.t
;
1203 /* stack: L H shift */
1205 /* constant: simpler */
1206 /* NOTE: all comments are for SHL. the other cases are
1207 done by swaping words */
1218 if (op
!= TOK_SAR
) {
1251 /* XXX: should provide a faster fallback on x86 ? */
1254 func
= TOK___ashrdi3
;
1257 func
= TOK___lshrdi3
;
1260 func
= TOK___ashldi3
;
1266 /* compare operations */
1272 /* stack: L1 H1 L2 H2 */
1274 vtop
[-1] = vtop
[-2];
1276 /* stack: L1 L2 H1 H2 */
1279 /* when values are equal, we need to compare low words. since
1280 the jump is inverted, we invert the test too. */
1283 else if (op1
== TOK_GT
)
1285 else if (op1
== TOK_ULT
)
1287 else if (op1
== TOK_UGT
)
1292 if (op1
!= TOK_NE
) {
1296 /* generate non equal test */
1297 /* XXX: NOT PORTABLE yet */
1301 #if defined(TCC_TARGET_I386)
1302 b
= psym(0x850f, 0);
1303 #elif defined(TCC_TARGET_ARM)
1305 o(0x1A000000 | encbranch(ind
, 0, 1));
1306 #elif defined(TCC_TARGET_C67)
1307 tcc_error("not implemented");
1309 #error not supported
1313 /* compare low. Always unsigned */
1317 else if (op1
== TOK_LE
)
1319 else if (op1
== TOK_GT
)
1321 else if (op1
== TOK_GE
)
1332 /* handle integer constant optimizations and various machine
1334 static void gen_opic(int op
)
1336 int c1
, c2
, t1
, t2
, n
;
1339 typedef unsigned long long U
;
1343 t1
= v1
->type
.t
& VT_BTYPE
;
1344 t2
= v2
->type
.t
& VT_BTYPE
;
1348 else if (v1
->type
.t
& VT_UNSIGNED
)
1355 else if (v2
->type
.t
& VT_UNSIGNED
)
1360 /* currently, we cannot do computations with forward symbols */
1361 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1362 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1365 case '+': l1
+= l2
; break;
1366 case '-': l1
-= l2
; break;
1367 case '&': l1
&= l2
; break;
1368 case '^': l1
^= l2
; break;
1369 case '|': l1
|= l2
; break;
1370 case '*': l1
*= l2
; break;
1377 /* if division by zero, generate explicit division */
1380 tcc_error("division by zero in constant");
1384 default: l1
/= l2
; break;
1385 case '%': l1
%= l2
; break;
1386 case TOK_UDIV
: l1
= (U
)l1
/ l2
; break;
1387 case TOK_UMOD
: l1
= (U
)l1
% l2
; break;
1390 case TOK_SHL
: l1
<<= l2
; break;
1391 case TOK_SHR
: l1
= (U
)l1
>> l2
; break;
1392 case TOK_SAR
: l1
>>= l2
; break;
1394 case TOK_ULT
: l1
= (U
)l1
< (U
)l2
; break;
1395 case TOK_UGE
: l1
= (U
)l1
>= (U
)l2
; break;
1396 case TOK_EQ
: l1
= l1
== l2
; break;
1397 case TOK_NE
: l1
= l1
!= l2
; break;
1398 case TOK_ULE
: l1
= (U
)l1
<= (U
)l2
; break;
1399 case TOK_UGT
: l1
= (U
)l1
> (U
)l2
; break;
1400 case TOK_LT
: l1
= l1
< l2
; break;
1401 case TOK_GE
: l1
= l1
>= l2
; break;
1402 case TOK_LE
: l1
= l1
<= l2
; break;
1403 case TOK_GT
: l1
= l1
> l2
; break;
1405 case TOK_LAND
: l1
= l1
&& l2
; break;
1406 case TOK_LOR
: l1
= l1
|| l2
; break;
1413 /* if commutative ops, put c2 as constant */
1414 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1415 op
== '|' || op
== '*')) {
1417 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1418 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1420 /* Filter out NOP operations like x*1, x-0, x&-1... */
1421 if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1424 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1425 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1431 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1432 /* try to use shifts instead of muls or divs */
1433 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1442 else if (op
== TOK_PDIV
)
1448 } else if (c2
&& (op
== '+' || op
== '-') &&
1449 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
1450 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1451 /* symbol + constant case */
1458 if (!nocode_wanted
) {
1459 /* call low level op generator */
1460 if (t1
== VT_LLONG
|| t2
== VT_LLONG
)
1471 /* generate a floating point operation with constant propagation */
1472 static void gen_opif(int op
)
1480 /* currently, we cannot do computations with forward symbols */
1481 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1482 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1484 if (v1
->type
.t
== VT_FLOAT
) {
1487 } else if (v1
->type
.t
== VT_DOUBLE
) {
1495 /* NOTE: we only do constant propagation if finite number (not
1496 NaN or infinity) (ANSI spec) */
1497 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1501 case '+': f1
+= f2
; break;
1502 case '-': f1
-= f2
; break;
1503 case '*': f1
*= f2
; break;
1507 tcc_error("division by zero in constant");
1512 /* XXX: also handles tests ? */
1516 /* XXX: overflow test ? */
1517 if (v1
->type
.t
== VT_FLOAT
) {
1519 } else if (v1
->type
.t
== VT_DOUBLE
) {
1527 if (!nocode_wanted
) {
1535 static int pointed_size(CType
*type
)
1538 return type_size(pointed_type(type
), &align
);
1541 static void vla_runtime_pointed_size(CType
*type
)
1544 vla_runtime_type_size(pointed_type(type
), &align
);
1547 static inline int is_null_pointer(SValue
*p
)
1549 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1551 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& p
->c
.i
== 0) ||
1552 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.ll
== 0) ||
1553 ((p
->type
.t
& VT_BTYPE
) == VT_PTR
&& p
->c
.ptr
== 0);
1556 static inline int is_integer_btype(int bt
)
1558 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
1559 bt
== VT_INT
|| bt
== VT_LLONG
);
1562 /* check types for comparison or substraction of pointers */
1563 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
1565 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
1568 /* null pointers are accepted for all comparisons as gcc */
1569 if (is_null_pointer(p1
) || is_null_pointer(p2
))
1573 bt1
= type1
->t
& VT_BTYPE
;
1574 bt2
= type2
->t
& VT_BTYPE
;
1575 /* accept comparison between pointer and integer with a warning */
1576 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
1577 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
1578 tcc_warning("comparison between pointer and integer");
1582 /* both must be pointers or implicit function pointers */
1583 if (bt1
== VT_PTR
) {
1584 type1
= pointed_type(type1
);
1585 } else if (bt1
!= VT_FUNC
)
1586 goto invalid_operands
;
1588 if (bt2
== VT_PTR
) {
1589 type2
= pointed_type(type2
);
1590 } else if (bt2
!= VT_FUNC
) {
1592 tcc_error("invalid operands to binary %s", get_tok_str(op
, NULL
));
1594 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1595 (type2
->t
& VT_BTYPE
) == VT_VOID
)
1599 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1600 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1601 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1602 /* gcc-like error if '-' is used */
1604 goto invalid_operands
;
1606 tcc_warning("comparison of distinct pointer types lacks a cast");
1610 /* generic gen_op: handles types problems */
1611 ST_FUNC
void gen_op(int op
)
1613 int u
, t1
, t2
, bt1
, bt2
, t
;
1616 t1
= vtop
[-1].type
.t
;
1617 t2
= vtop
[0].type
.t
;
1618 bt1
= t1
& VT_BTYPE
;
1619 bt2
= t2
& VT_BTYPE
;
1621 if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
1622 /* at least one operand is a pointer */
1623 /* relationnal op: must be both pointers */
1624 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
1625 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1626 /* pointers are handled are unsigned */
1627 #ifdef TCC_TARGET_X86_64
1628 t
= VT_LLONG
| VT_UNSIGNED
;
1630 t
= VT_INT
| VT_UNSIGNED
;
1634 /* if both pointers, then it must be the '-' op */
1635 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
1637 tcc_error("cannot use pointers here");
1638 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1639 /* XXX: check that types are compatible */
1640 if (vtop
[-1].type
.t
& VT_VLA
) {
1641 vla_runtime_pointed_size(&vtop
[-1].type
);
1643 vpushi(pointed_size(&vtop
[-1].type
));
1647 /* set to integer type */
1648 #ifdef TCC_TARGET_X86_64
1649 vtop
->type
.t
= VT_LLONG
;
1651 vtop
->type
.t
= VT_INT
;
1656 /* exactly one pointer : must be '+' or '-'. */
1657 if (op
!= '-' && op
!= '+')
1658 tcc_error("cannot use pointers here");
1659 /* Put pointer as first operand */
1660 if (bt2
== VT_PTR
) {
1664 type1
= vtop
[-1].type
;
1665 type1
.t
&= ~VT_ARRAY
;
1666 if (vtop
[-1].type
.t
& VT_VLA
)
1667 vla_runtime_pointed_size(&vtop
[-1].type
);
1669 u
= pointed_size(&vtop
[-1].type
);
1671 tcc_error("unknown array element size");
1672 #ifdef TCC_TARGET_X86_64
1675 /* XXX: cast to int ? (long long case) */
1680 #ifdef CONFIG_TCC_BCHECK
1681 /* if evaluating constant expression, no code should be
1682 generated, so no bound check */
1683 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
1684 /* if bounded pointers, we generate a special code to
1691 gen_bounded_ptr_add();
1697 /* put again type if gen_opic() swaped operands */
1700 } else if (is_float(bt1
) || is_float(bt2
)) {
1701 /* compute bigger type and do implicit casts */
1702 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
1704 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
1709 /* floats can only be used for a few operations */
1710 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
1711 (op
< TOK_ULT
|| op
> TOK_GT
))
1712 tcc_error("invalid operands for binary operation");
1714 } else if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
) {
1715 t
= bt1
== VT_LLONG
? VT_LLONG
: VT_INT
;
1716 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (t
| VT_UNSIGNED
))
1719 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
1720 /* cast to biggest op */
1722 /* convert to unsigned if it does not fit in a long long */
1723 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
1724 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
1727 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
1728 tcc_error("comparison of struct");
1730 /* integer operations */
1732 /* convert to unsigned if it does not fit in an integer */
1733 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
1734 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
1737 /* XXX: currently, some unsigned operations are explicit, so
1738 we modify them here */
1739 if (t
& VT_UNSIGNED
) {
1746 else if (op
== TOK_LT
)
1748 else if (op
== TOK_GT
)
1750 else if (op
== TOK_LE
)
1752 else if (op
== TOK_GE
)
1759 /* special case for shifts and long long: we keep the shift as
1761 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
1768 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
1769 /* relationnal op: the result is an int */
1770 vtop
->type
.t
= VT_INT
;
1777 #ifndef TCC_TARGET_ARM
1778 /* generic itof for unsigned long long case */
1779 static void gen_cvt_itof1(int t
)
1781 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
1782 (VT_LLONG
| VT_UNSIGNED
)) {
1785 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
1786 #if LDOUBLE_SIZE != 8
1787 else if (t
== VT_LDOUBLE
)
1788 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
1791 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
1795 vtop
->r
= reg_fret(t
);
1802 /* generic ftoi for unsigned long long case */
1803 static void gen_cvt_ftoi1(int t
)
1807 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
1808 /* not handled natively */
1809 st
= vtop
->type
.t
& VT_BTYPE
;
1811 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
1812 #if LDOUBLE_SIZE != 8
1813 else if (st
== VT_LDOUBLE
)
1814 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
1817 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
1822 vtop
->r2
= REG_LRET
;
1828 /* force char or short cast */
1829 static void force_charshort_cast(int t
)
1833 /* XXX: add optimization if lvalue : just change type and offset */
1838 if (t
& VT_UNSIGNED
) {
1839 vpushi((1 << bits
) - 1);
1845 /* result must be signed or the SAR is converted to an SHL
1846 This was not the case when "t" was a signed short
1847 and the last value on the stack was an unsigned int */
1848 vtop
->type
.t
&= ~VT_UNSIGNED
;
1854 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1855 static void gen_cast(CType
*type
)
1857 int sbt
, dbt
, sf
, df
, c
, p
;
1859 /* special delayed cast for char/short */
1860 /* XXX: in some cases (multiple cascaded casts), it may still
1862 if (vtop
->r
& VT_MUSTCAST
) {
1863 vtop
->r
&= ~VT_MUSTCAST
;
1864 force_charshort_cast(vtop
->type
.t
);
1867 /* bitfields first get cast to ints */
1868 if (vtop
->type
.t
& VT_BITFIELD
) {
1872 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
1873 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
1878 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1879 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
1881 /* constant case: we can do it now */
1882 /* XXX: in ISOC, cannot do it if error in convert */
1883 if (sbt
== VT_FLOAT
)
1884 vtop
->c
.ld
= vtop
->c
.f
;
1885 else if (sbt
== VT_DOUBLE
)
1886 vtop
->c
.ld
= vtop
->c
.d
;
1889 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
1890 if (sbt
& VT_UNSIGNED
)
1891 vtop
->c
.ld
= vtop
->c
.ull
;
1893 vtop
->c
.ld
= vtop
->c
.ll
;
1895 if (sbt
& VT_UNSIGNED
)
1896 vtop
->c
.ld
= vtop
->c
.ui
;
1898 vtop
->c
.ld
= vtop
->c
.i
;
1901 if (dbt
== VT_FLOAT
)
1902 vtop
->c
.f
= (float)vtop
->c
.ld
;
1903 else if (dbt
== VT_DOUBLE
)
1904 vtop
->c
.d
= (double)vtop
->c
.ld
;
1905 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
1906 vtop
->c
.ull
= (unsigned long long)vtop
->c
.ld
;
1907 } else if (sf
&& dbt
== VT_BOOL
) {
1908 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
1911 vtop
->c
.ll
= (long long)vtop
->c
.ld
;
1912 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
1913 vtop
->c
.ll
= vtop
->c
.ull
;
1914 else if (sbt
& VT_UNSIGNED
)
1915 vtop
->c
.ll
= vtop
->c
.ui
;
1916 #ifdef TCC_TARGET_X86_64
1917 else if (sbt
== VT_PTR
)
1920 else if (sbt
!= VT_LLONG
)
1921 vtop
->c
.ll
= vtop
->c
.i
;
1923 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
1924 vtop
->c
.ull
= vtop
->c
.ll
;
1925 else if (dbt
== VT_BOOL
)
1926 vtop
->c
.i
= (vtop
->c
.ll
!= 0);
1927 else if (dbt
!= VT_LLONG
) {
1929 if ((dbt
& VT_BTYPE
) == VT_BYTE
)
1931 else if ((dbt
& VT_BTYPE
) == VT_SHORT
)
1934 if(dbt
& VT_UNSIGNED
)
1935 vtop
->c
.ui
= ((unsigned int)vtop
->c
.ll
<< s
) >> s
;
1937 vtop
->c
.i
= ((int)vtop
->c
.ll
<< s
) >> s
;
1940 } else if (p
&& dbt
== VT_BOOL
) {
1943 } else if (!nocode_wanted
) {
1944 /* non constant case: generate code */
1946 /* convert from fp to fp */
1949 /* convert int to fp */
1952 /* convert fp to int */
1953 if (dbt
== VT_BOOL
) {
1957 /* we handle char/short/etc... with generic code */
1958 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
1959 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
1963 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
1964 /* additional cast for char/short... */
1969 #ifndef TCC_TARGET_X86_64
1970 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
1971 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
1972 /* scalar to long long */
1973 /* machine independent conversion */
1975 /* generate high word */
1976 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
1980 if (sbt
== VT_PTR
) {
1981 /* cast from pointer to int before we apply
1982 shift operation, which pointers don't support*/
1983 gen_cast(&int_type
);
1989 /* patch second register */
1990 vtop
[-1].r2
= vtop
->r
;
1994 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
1995 (dbt
& VT_BTYPE
) == VT_PTR
||
1996 (dbt
& VT_BTYPE
) == VT_FUNC
) {
1997 if ((sbt
& VT_BTYPE
) != VT_LLONG
&&
1998 (sbt
& VT_BTYPE
) != VT_PTR
&&
1999 (sbt
& VT_BTYPE
) != VT_FUNC
) {
2000 /* need to convert from 32bit to 64bit */
2002 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
2003 /* x86_64 specific: movslq */
2005 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
2009 } else if (dbt
== VT_BOOL
) {
2010 /* scalar to bool */
2013 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
2014 (dbt
& VT_BTYPE
) == VT_SHORT
) {
2015 if (sbt
== VT_PTR
) {
2016 vtop
->type
.t
= VT_INT
;
2017 tcc_warning("nonportable conversion from pointer to char/short");
2019 force_charshort_cast(dbt
);
2020 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
2022 if (sbt
== VT_LLONG
) {
2023 /* from long long: just take low order word */
2027 /* if lvalue and single word type, nothing to do because
2028 the lvalue already contains the real type size (see
2029 VT_LVAL_xxx constants) */
2032 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
2033 /* if we are casting between pointer types,
2034 we must update the VT_LVAL_xxx size */
2035 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
2036 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
2041 /* return type size as known at compile time. Put alignment at 'a' */
2042 ST_FUNC
int type_size(CType
*type
, int *a
)
2047 bt
= type
->t
& VT_BTYPE
;
2048 if (bt
== VT_STRUCT
) {
2053 } else if (bt
== VT_PTR
) {
2054 if (type
->t
& VT_ARRAY
) {
2058 ts
= type_size(&s
->type
, a
);
2060 if (ts
< 0 && s
->c
< 0)
2068 } else if (bt
== VT_LDOUBLE
) {
2070 return LDOUBLE_SIZE
;
2071 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
2072 #ifdef TCC_TARGET_I386
2073 #ifdef TCC_TARGET_PE
2078 #elif defined(TCC_TARGET_ARM)
2088 } else if (bt
== VT_INT
|| bt
== VT_ENUM
|| bt
== VT_FLOAT
) {
2091 } else if (bt
== VT_SHORT
) {
2095 /* char, void, function, _Bool */
2101 /* push type size as known at runtime time on top of value stack. Put
2103 ST_FUNC
void vla_runtime_type_size(CType
*type
, int *a
)
2105 if (type
->t
& VT_VLA
) {
2106 vset(&int_type
, VT_LOCAL
|VT_LVAL
, type
->ref
->c
);
2108 vpushi(type_size(type
, a
));
2112 /* return the pointed type of t */
2113 static inline CType
*pointed_type(CType
*type
)
2115 return &type
->ref
->type
;
2118 /* modify type so that its it is a pointer to type. */
2119 ST_FUNC
void mk_pointer(CType
*type
)
2122 s
= sym_push(SYM_FIELD
, type
, 0, -1);
2123 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
2127 /* compare function types. OLD functions match any new functions */
2128 static int is_compatible_func(CType
*type1
, CType
*type2
)
2134 if (!is_compatible_types(&s1
->type
, &s2
->type
))
2136 /* check func_call */
2137 if (FUNC_CALL(s1
->r
) != FUNC_CALL(s2
->r
))
2139 /* XXX: not complete */
2140 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
2144 while (s1
!= NULL
) {
2147 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
2157 /* return true if type1 and type2 are the same. If unqualified is
2158 true, qualifiers on the types are ignored.
2160 - enums are not checked as gcc __builtin_types_compatible_p ()
2162 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2166 t1
= type1
->t
& VT_TYPE
;
2167 t2
= type2
->t
& VT_TYPE
;
2169 /* strip qualifiers before comparing */
2170 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2171 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2173 /* XXX: bitfields ? */
2176 /* test more complicated cases */
2177 bt1
= t1
& VT_BTYPE
;
2178 if (bt1
== VT_PTR
) {
2179 type1
= pointed_type(type1
);
2180 type2
= pointed_type(type2
);
2181 return is_compatible_types(type1
, type2
);
2182 } else if (bt1
== VT_STRUCT
) {
2183 return (type1
->ref
== type2
->ref
);
2184 } else if (bt1
== VT_FUNC
) {
2185 return is_compatible_func(type1
, type2
);
2191 /* return true if type1 and type2 are exactly the same (including
2194 static int is_compatible_types(CType
*type1
, CType
*type2
)
2196 return compare_types(type1
,type2
,0);
2199 /* return true if type1 and type2 are the same (ignoring qualifiers).
2201 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
2203 return compare_types(type1
,type2
,1);
2206 /* print a type. If 'varstr' is not NULL, then the variable is also
2207 printed in the type */
2209 /* XXX: add array and function pointers */
2210 static void type_to_str(char *buf
, int buf_size
,
2211 CType
*type
, const char *varstr
)
2218 t
= type
->t
& VT_TYPE
;
2221 if (t
& VT_CONSTANT
)
2222 pstrcat(buf
, buf_size
, "const ");
2223 if (t
& VT_VOLATILE
)
2224 pstrcat(buf
, buf_size
, "volatile ");
2225 if (t
& VT_UNSIGNED
)
2226 pstrcat(buf
, buf_size
, "unsigned ");
2256 tstr
= "long double";
2258 pstrcat(buf
, buf_size
, tstr
);
2262 if (bt
== VT_STRUCT
)
2266 pstrcat(buf
, buf_size
, tstr
);
2267 v
= type
->ref
->v
& ~SYM_STRUCT
;
2268 if (v
>= SYM_FIRST_ANOM
)
2269 pstrcat(buf
, buf_size
, "<anonymous>");
2271 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
2275 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
2276 pstrcat(buf
, buf_size
, "(");
2278 while (sa
!= NULL
) {
2279 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
2280 pstrcat(buf
, buf_size
, buf1
);
2283 pstrcat(buf
, buf_size
, ", ");
2285 pstrcat(buf
, buf_size
, ")");
2289 pstrcpy(buf1
, sizeof(buf1
), "*");
2291 pstrcat(buf1
, sizeof(buf1
), varstr
);
2292 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2296 pstrcat(buf
, buf_size
, " ");
2297 pstrcat(buf
, buf_size
, varstr
);
2302 /* verify type compatibility to store vtop in 'dt' type, and generate
2304 static void gen_assign_cast(CType
*dt
)
2306 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
2307 char buf1
[256], buf2
[256];
2310 st
= &vtop
->type
; /* source type */
2311 dbt
= dt
->t
& VT_BTYPE
;
2312 sbt
= st
->t
& VT_BTYPE
;
2314 tcc_error("Cannot assign void value");
2315 if (dt
->t
& VT_CONSTANT
)
2316 tcc_warning("assignment of read-only location");
2319 /* special cases for pointers */
2320 /* '0' can also be a pointer */
2321 if (is_null_pointer(vtop
))
2323 /* accept implicit pointer to integer cast with warning */
2324 if (is_integer_btype(sbt
)) {
2325 tcc_warning("assignment makes pointer from integer without a cast");
2328 type1
= pointed_type(dt
);
2329 /* a function is implicitely a function pointer */
2330 if (sbt
== VT_FUNC
) {
2331 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
2332 !is_compatible_types(pointed_type(dt
), st
))
2333 tcc_warning("assignment from incompatible pointer type");
2338 type2
= pointed_type(st
);
2339 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2340 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
2341 /* void * can match anything */
2343 /* exact type match, except for unsigned */
2346 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2347 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2348 if (!is_compatible_types(&tmp_type1
, &tmp_type2
))
2349 tcc_warning("assignment from incompatible pointer type");
2351 /* check const and volatile */
2352 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
2353 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
2354 tcc_warning("assignment discards qualifiers from pointer target type");
2360 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
2361 tcc_warning("assignment makes integer from pointer without a cast");
2363 /* XXX: more tests */
2368 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2369 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2370 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2372 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
2373 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
2374 tcc_error("cannot cast '%s' to '%s'", buf1
, buf2
);
2382 /* store vtop in lvalue pushed on stack */
2383 ST_FUNC
void vstore(void)
2385 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
2387 ft
= vtop
[-1].type
.t
;
2388 sbt
= vtop
->type
.t
& VT_BTYPE
;
2389 dbt
= ft
& VT_BTYPE
;
2390 if ((((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2391 (sbt
== VT_INT
&& dbt
== VT_SHORT
))
2392 && !(vtop
->type
.t
& VT_BITFIELD
)) {
2393 /* optimize char/short casts */
2394 delayed_cast
= VT_MUSTCAST
;
2395 vtop
->type
.t
= ft
& (VT_TYPE
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
)));
2396 /* XXX: factorize */
2397 if (ft
& VT_CONSTANT
)
2398 tcc_warning("assignment of read-only location");
2401 if (!(ft
& VT_BITFIELD
))
2402 gen_assign_cast(&vtop
[-1].type
);
2405 if (sbt
== VT_STRUCT
) {
2406 /* if structure, only generate pointer */
2407 /* structure assignment : generate memcpy */
2408 /* XXX: optimize if small size */
2409 if (!nocode_wanted
) {
2410 size
= type_size(&vtop
->type
, &align
);
2414 vtop
->type
.t
= VT_PTR
;
2417 /* address of memcpy() */
2420 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2421 else if(!(align
& 3))
2422 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2425 vpush_global_sym(&func_old_type
, TOK_memcpy
);
2430 vtop
->type
.t
= VT_PTR
;
2439 /* leave source on stack */
2440 } else if (ft
& VT_BITFIELD
) {
2441 /* bitfield store handling */
2442 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2443 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2444 /* remove bit field info to avoid loops */
2445 vtop
[-1].type
.t
= ft
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
2447 /* duplicate source into other register */
2452 if((ft
& VT_BTYPE
) == VT_BOOL
) {
2453 gen_cast(&vtop
[-1].type
);
2454 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2457 /* duplicate destination */
2459 vtop
[-1] = vtop
[-2];
2461 /* mask and shift source */
2462 if((ft
& VT_BTYPE
) != VT_BOOL
) {
2463 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2464 vpushll((1ULL << bit_size
) - 1ULL);
2466 vpushi((1 << bit_size
) - 1);
2472 /* load destination, mask and or with source */
2474 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2475 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2477 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2484 /* pop off shifted source from "duplicate source..." above */
2488 #ifdef CONFIG_TCC_BCHECK
2489 /* bound check case */
2490 if (vtop
[-1].r
& VT_MUSTBOUND
) {
2496 if (!nocode_wanted
) {
2500 #ifdef TCC_TARGET_X86_64
2501 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
2506 r
= gv(rc
); /* generate value */
2507 /* if lvalue was saved on stack, must read it */
2508 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
2510 t
= get_reg(RC_INT
);
2511 #ifdef TCC_TARGET_X86_64
2516 sv
.r
= VT_LOCAL
| VT_LVAL
;
2517 sv
.c
.ul
= vtop
[-1].c
.ul
;
2519 vtop
[-1].r
= t
| VT_LVAL
;
2522 #ifndef TCC_TARGET_X86_64
2523 /* two word case handling : store second register at word + 4 */
2524 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
2526 /* convert to int to increment easily */
2527 vtop
->type
.t
= VT_INT
;
2533 /* XXX: it works because r2 is spilled last ! */
2534 store(vtop
->r2
, vtop
- 1);
2539 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
2540 vtop
->r
|= delayed_cast
;
2544 /* post defines POST/PRE add. c is the token ++ or -- */
2545 ST_FUNC
void inc(int post
, int c
)
2548 vdup(); /* save lvalue */
2550 gv_dup(); /* duplicate value */
2555 vpushi(c
- TOK_MID
);
2557 vstore(); /* store value */
2559 vpop(); /* if post op, return saved value */
2562 /* Parse GNUC __attribute__ extension. Currently, the following
2563 extensions are recognized:
2564 - aligned(n) : set data/function alignment.
2565 - packed : force data alignment to 1
2566 - section(x) : generate data/code in this section.
2567 - unused : currently ignored, but may be used someday.
2568 - regparm(n) : pass function parameters in registers (i386 only)
2570 static void parse_attribute(AttributeDef
*ad
)
2574 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
2578 while (tok
!= ')') {
2579 if (tok
< TOK_IDENT
)
2580 expect("attribute name");
2588 expect("section name");
2589 ad
->section
= find_section(tcc_state
, (char *)tokc
.cstr
->data
);
2597 expect("alias(\"target\")");
2598 ad
->alias_target
= /* save string as token, for later */
2599 tok_alloc((char*)tokc
.cstr
->data
, tokc
.cstr
->size
-1)->tok
;
2608 if (n
<= 0 || (n
& (n
- 1)) != 0)
2609 tcc_error("alignment must be a positive power of two");
2626 /* currently, no need to handle it because tcc does not
2627 track unused objects */
2631 /* currently, no need to handle it because tcc does not
2632 track unused objects */
2637 ad
->func_call
= FUNC_CDECL
;
2642 ad
->func_call
= FUNC_STDCALL
;
2644 #ifdef TCC_TARGET_I386
2654 ad
->func_call
= FUNC_FASTCALL1
+ n
- 1;
2660 ad
->func_call
= FUNC_FASTCALLW
;
2667 ad
->mode
= VT_LLONG
+ 1;
2670 ad
->mode
= VT_SHORT
+ 1;
2673 ad
->mode
= VT_INT
+ 1;
2676 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
2683 ad
->func_export
= 1;
2686 ad
->func_import
= 1;
2689 if (tcc_state
->warn_unsupported
)
2690 tcc_warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
2691 /* skip parameters */
2693 int parenthesis
= 0;
2697 else if (tok
== ')')
2700 } while (parenthesis
&& tok
!= -1);
2713 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2714 static void struct_decl(CType
*type
, int u
)
2716 int a
, v
, size
, align
, maxalign
, c
, offset
;
2717 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
, prevbt
;
2718 Sym
*s
, *ss
, *ass
, **ps
;
2722 a
= tok
; /* save decl type */
2727 /* struct already defined ? return it */
2729 expect("struct/union/enum name");
2733 tcc_error("invalid type");
2740 /* we put an undefined size for struct/union */
2741 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
2742 s
->r
= 0; /* default alignment is zero as gcc */
2743 /* put struct/union/enum name in type */
2751 tcc_error("struct/union/enum already defined");
2752 /* cannot be empty */
2754 /* non empty enums are not allowed */
2755 if (a
== TOK_ENUM
) {
2759 expect("identifier");
2765 /* enum symbols have static storage */
2766 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
2767 ss
->type
.t
|= VT_STATIC
;
2772 /* NOTE: we accept a trailing comma */
2783 while (tok
!= '}') {
2784 parse_btype(&btype
, &ad
);
2790 type_decl(&type1
, &ad
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
2791 if (v
== 0 && (type1
.t
& VT_BTYPE
) != VT_STRUCT
)
2792 expect("identifier");
2793 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
2794 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
2795 tcc_error("invalid type for '%s'",
2796 get_tok_str(v
, NULL
));
2800 bit_size
= expr_const();
2801 /* XXX: handle v = 0 case for messages */
2803 tcc_error("negative width in bit-field '%s'",
2804 get_tok_str(v
, NULL
));
2805 if (v
&& bit_size
== 0)
2806 tcc_error("zero width for bit-field '%s'",
2807 get_tok_str(v
, NULL
));
2809 size
= type_size(&type1
, &align
);
2811 if (align
< ad
.aligned
)
2813 } else if (ad
.packed
) {
2815 } else if (*tcc_state
->pack_stack_ptr
) {
2816 if (align
> *tcc_state
->pack_stack_ptr
)
2817 align
= *tcc_state
->pack_stack_ptr
;
2820 if (bit_size
>= 0) {
2821 bt
= type1
.t
& VT_BTYPE
;
2828 tcc_error("bitfields must have scalar type");
2830 if (bit_size
> bsize
) {
2831 tcc_error("width of '%s' exceeds its type",
2832 get_tok_str(v
, NULL
));
2833 } else if (bit_size
== bsize
) {
2834 /* no need for bit fields */
2836 } else if (bit_size
== 0) {
2837 /* XXX: what to do if only padding in a
2839 /* zero size: means to pad */
2842 /* we do not have enough room ?
2843 did the type change?
2845 if ((bit_pos
+ bit_size
) > bsize
||
2846 bt
!= prevbt
|| a
== TOK_UNION
)
2849 /* XXX: handle LSB first */
2850 type1
.t
|= VT_BITFIELD
|
2851 (bit_pos
<< VT_STRUCT_SHIFT
) |
2852 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
2853 bit_pos
+= bit_size
;
2859 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2860 /* add new memory data only if starting
2862 if (lbit_pos
== 0) {
2863 if (a
== TOK_STRUCT
) {
2864 c
= (c
+ align
- 1) & -align
;
2873 if (align
> maxalign
)
2877 printf("add field %s offset=%d",
2878 get_tok_str(v
, NULL
), offset
);
2879 if (type1
.t
& VT_BITFIELD
) {
2880 printf(" pos=%d size=%d",
2881 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
2882 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
2887 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2889 while ((ass
= ass
->next
) != NULL
) {
2890 ss
= sym_push(ass
->v
, &ass
->type
, 0, offset
+ ass
->c
);
2895 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
2899 if (tok
== ';' || tok
== TOK_EOF
)
2906 /* store size and alignment */
2907 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
2913 /* return 0 if no type declaration. otherwise, return the basic type
2916 static int parse_btype(CType
*type
, AttributeDef
*ad
)
2918 int t
, u
, type_found
, typespec_found
, typedef_found
;
2922 memset(ad
, 0, sizeof(AttributeDef
));
2930 /* currently, we really ignore extension */
2940 if ((t
& VT_BTYPE
) != 0)
2941 tcc_error("too many basic types");
2957 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
2958 #ifndef TCC_TARGET_PE
2959 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
2961 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
2962 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
2976 if ((t
& VT_BTYPE
) == VT_LONG
) {
2977 #ifdef TCC_TARGET_PE
2978 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
2980 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
2988 struct_decl(&type1
, VT_ENUM
);
2991 type
->ref
= type1
.ref
;
2995 struct_decl(&type1
, VT_STRUCT
);
2998 /* type modifiers */
3051 /* GNUC attribute */
3052 case TOK_ATTRIBUTE1
:
3053 case TOK_ATTRIBUTE2
:
3054 parse_attribute(ad
);
3057 t
= (t
& ~VT_BTYPE
) | u
;
3065 parse_expr_type(&type1
);
3066 /* remove all storage modifiers except typedef */
3067 type1
.t
&= ~(VT_STORAGE
&~VT_TYPEDEF
);
3070 if (typespec_found
|| typedef_found
)
3073 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
3076 t
|= (s
->type
.t
& ~VT_TYPEDEF
);
3077 type
->ref
= s
->type
.ref
;
3079 /* get attributes from typedef */
3080 if (0 == ad
->aligned
)
3081 ad
->aligned
= FUNC_ALIGN(s
->r
);
3082 if (0 == ad
->func_call
)
3083 ad
->func_call
= FUNC_CALL(s
->r
);
3084 ad
->packed
|= FUNC_PACKED(s
->r
);
3093 if ((t
& (VT_SIGNED
|VT_UNSIGNED
)) == (VT_SIGNED
|VT_UNSIGNED
))
3094 tcc_error("signed and unsigned modifier");
3095 if (tcc_state
->char_is_unsigned
) {
3096 if ((t
& (VT_SIGNED
|VT_UNSIGNED
|VT_BTYPE
)) == VT_BYTE
)
3101 /* long is never used as type */
3102 if ((t
& VT_BTYPE
) == VT_LONG
)
3103 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
3104 t
= (t
& ~VT_BTYPE
) | VT_INT
;
3106 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3112 /* convert a function parameter type (array to pointer and function to
3113 function pointer) */
3114 static inline void convert_parameter_type(CType
*pt
)
3116 /* remove const and volatile qualifiers (XXX: const could be used
3117 to indicate a const function parameter */
3118 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3119 /* array must be transformed to pointer according to ANSI C */
3121 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
3126 ST_FUNC
void parse_asm_str(CString
*astr
)
3129 /* read the string */
3131 expect("string constant");
3133 while (tok
== TOK_STR
) {
3134 /* XXX: add \0 handling too ? */
3135 cstr_cat(astr
, tokc
.cstr
->data
);
3138 cstr_ccat(astr
, '\0');
3141 /* Parse an asm label and return the label
3142 * Don't forget to free the CString in the caller! */
3143 static void asm_label_instr(CString
*astr
)
3146 parse_asm_str(astr
);
3149 printf("asm_alias: \"%s\"\n", (char *)astr
->data
);
3153 static void post_type(CType
*type
, AttributeDef
*ad
)
3155 int n
, l
, t1
, arg_size
, align
;
3156 Sym
**plast
, *s
, *first
;
3161 /* function declaration */
3169 /* read param name and compute offset */
3170 if (l
!= FUNC_OLD
) {
3171 if (!parse_btype(&pt
, &ad1
)) {
3173 tcc_error("invalid type");
3180 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
3182 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3183 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
3184 tcc_error("parameter declared as void");
3185 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
3190 expect("identifier");
3194 convert_parameter_type(&pt
);
3195 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
3201 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
3208 /* if no parameters, then old type prototype */
3212 /* NOTE: const is ignored in returned type as it has a special
3213 meaning in gcc / C++ */
3214 type
->t
&= ~VT_CONSTANT
;
3215 /* some ancient pre-K&R C allows a function to return an array
3216 and the array brackets to be put after the arguments, such
3217 that "int c()[]" means something like "int[] c()" */
3220 skip(']'); /* only handle simple "[]" */
3223 /* we push a anonymous symbol which will contain the function prototype */
3224 ad
->func_args
= arg_size
;
3225 s
= sym_push(SYM_FIELD
, type
, INT_ATTR(ad
), l
);
3229 } else if (tok
== '[') {
3230 /* array definition */
3232 if (tok
== TOK_RESTRICT1
)
3237 if (!local_stack
|| nocode_wanted
)
3238 vpushi(expr_const());
3240 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3243 tcc_error("invalid array size");
3245 if (!is_integer_btype(vtop
->type
.t
& VT_BTYPE
))
3246 tcc_error("size of variable length array should be an integer");
3251 /* parse next post type */
3252 post_type(type
, ad
);
3253 t1
|= type
->t
& VT_VLA
;
3256 loc
-= type_size(&int_type
, &align
);
3260 vla_runtime_type_size(type
, &align
);
3262 vset(&int_type
, VT_LOCAL
|VT_LVAL
, loc
);
3269 /* we push an anonymous symbol which will contain the array
3271 s
= sym_push(SYM_FIELD
, type
, 0, n
);
3272 type
->t
= (t1
? VT_VLA
: VT_ARRAY
) | VT_PTR
;
3277 /* Parse a type declaration (except basic type), and return the type
3278 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3279 expected. 'type' should contain the basic type. 'ad' is the
3280 attribute definition of the basic type. It can be modified by
3283 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
3286 CType type1
, *type2
;
3287 int qualifiers
, storage
;
3289 while (tok
== '*') {
3297 qualifiers
|= VT_CONSTANT
;
3302 qualifiers
|= VT_VOLATILE
;
3310 type
->t
|= qualifiers
;
3313 /* XXX: clarify attribute handling */
3314 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3315 parse_attribute(ad
);
3317 /* recursive type */
3318 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3319 type1
.t
= 0; /* XXX: same as int */
3322 /* XXX: this is not correct to modify 'ad' at this point, but
3323 the syntax is not clear */
3324 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3325 parse_attribute(ad
);
3326 type_decl(&type1
, ad
, v
, td
);
3329 /* type identifier */
3330 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
3334 if (!(td
& TYPE_ABSTRACT
))
3335 expect("identifier");
3339 storage
= type
->t
& VT_STORAGE
;
3340 type
->t
&= ~VT_STORAGE
;
3341 if (storage
& VT_STATIC
) {
3342 int saved_nocode_wanted
= nocode_wanted
;
3344 post_type(type
, ad
);
3345 nocode_wanted
= saved_nocode_wanted
;
3347 post_type(type
, ad
);
3349 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3350 parse_attribute(ad
);
3354 /* append type at the end of type1 */
3367 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3368 ST_FUNC
int lvalue_type(int t
)
3373 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
3375 else if (bt
== VT_SHORT
)
3379 if (t
& VT_UNSIGNED
)
3380 r
|= VT_LVAL_UNSIGNED
;
3384 /* indirection with full error checking and bound check */
3385 ST_FUNC
void indir(void)
3387 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
3388 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
3392 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
3394 vtop
->type
= *pointed_type(&vtop
->type
);
3395 /* Arrays and functions are never lvalues */
3396 if (!(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_VLA
)
3397 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3398 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3399 /* if bound checking, the referenced pointer must be checked */
3400 #ifdef CONFIG_TCC_BCHECK
3401 if (tcc_state
->do_bounds_check
)
3402 vtop
->r
|= VT_MUSTBOUND
;
3407 /* pass a parameter to a function and do type checking and casting */
3408 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
3413 func_type
= func
->c
;
3414 if (func_type
== FUNC_OLD
||
3415 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
3416 /* default casting : only need to convert float to double */
3417 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
3421 } else if (arg
== NULL
) {
3422 tcc_error("too many arguments to function");
3425 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
3426 gen_assign_cast(&type
);
3430 /* parse an expression of the form '(type)' or '(expr)' and return its
3432 static void parse_expr_type(CType
*type
)
3438 if (parse_btype(type
, &ad
)) {
3439 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3446 static void parse_type(CType
*type
)
3451 if (!parse_btype(type
, &ad
)) {
3454 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3457 static void vpush_tokc(int t
)
3462 vsetc(&type
, VT_CONST
, &tokc
);
3465 ST_FUNC
void unary(void)
3467 int n
, t
, align
, size
, r
, sizeof_caller
;
3471 static int in_sizeof
= 0;
3473 sizeof_caller
= in_sizeof
;
3475 /* XXX: GCC 2.95.3 does not generate a table although it should be
3489 vpush_tokc(VT_INT
| VT_UNSIGNED
);
3493 vpush_tokc(VT_LLONG
);
3497 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
3501 vpush_tokc(VT_FLOAT
);
3505 vpush_tokc(VT_DOUBLE
);
3509 vpush_tokc(VT_LDOUBLE
);
3512 case TOK___FUNCTION__
:
3514 goto tok_identifier
;
3520 /* special function name identifier */
3521 len
= strlen(funcname
) + 1;
3522 /* generate char[len] type */
3527 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
3528 ptr
= section_ptr_add(data_section
, len
);
3529 memcpy(ptr
, funcname
, len
);
3534 #ifdef TCC_TARGET_PE
3535 t
= VT_SHORT
| VT_UNSIGNED
;
3541 /* string parsing */
3544 if (tcc_state
->warn_write_strings
)
3549 memset(&ad
, 0, sizeof(AttributeDef
));
3550 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, NULL
, 0);
3555 if (parse_btype(&type
, &ad
)) {
3556 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
3558 /* check ISOC99 compound literal */
3560 /* data is allocated locally by default */
3565 /* all except arrays are lvalues */
3566 if (!(type
.t
& VT_ARRAY
))
3567 r
|= lvalue_type(type
.t
);
3568 memset(&ad
, 0, sizeof(AttributeDef
));
3569 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, NULL
, 0);
3571 if (sizeof_caller
) {
3578 } else if (tok
== '{') {
3579 /* save all registers */
3581 /* statement expression : we do not accept break/continue
3582 inside as GCC does */
3583 block(NULL
, NULL
, NULL
, NULL
, 0, 1);
3598 /* functions names must be treated as function pointers,
3599 except for unary '&' and sizeof. Since we consider that
3600 functions are not lvalues, we only have to handle it
3601 there and in function calls. */
3602 /* arrays can also be used although they are not lvalues */
3603 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
3604 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
3606 mk_pointer(&vtop
->type
);
3612 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3614 boolean
.t
= VT_BOOL
;
3616 vtop
->c
.i
= !vtop
->c
.i
;
3617 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
3618 vtop
->c
.i
= vtop
->c
.i
^ 1;
3621 vseti(VT_JMP
, gtst(1, 0));
3632 /* in order to force cast, we add zero */
3634 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
3635 tcc_error("pointer not accepted for unary plus");
3645 unary_type(&type
); // Perform a in_sizeof = 0;
3646 size
= type_size(&type
, &align
);
3647 if (t
== TOK_SIZEOF
) {
3648 if (!(type
.t
& VT_VLA
)) {
3650 tcc_error("sizeof applied to an incomplete type");
3653 vla_runtime_type_size(&type
, &align
);
3658 vtop
->type
.t
|= VT_UNSIGNED
;
3661 case TOK_builtin_types_compatible_p
:
3670 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3671 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3672 vpushi(is_compatible_types(&type1
, &type2
));
3675 case TOK_builtin_constant_p
:
3677 int saved_nocode_wanted
, res
;
3680 saved_nocode_wanted
= nocode_wanted
;
3683 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
3685 nocode_wanted
= saved_nocode_wanted
;
3690 case TOK_builtin_frame_address
:
3696 if (tok
!= TOK_CINT
|| tokc
.i
< 0) {
3697 tcc_error("__builtin_frame_address only takes positive integers");
3704 vset(&type
, VT_LOCAL
, 0); /* local frame */
3706 mk_pointer(&vtop
->type
);
3707 indir(); /* -> parent frame */
3711 #ifdef TCC_TARGET_X86_64
3712 case TOK_builtin_va_arg_types
:
3714 /* This definition must be synced with stdarg.h */
3715 enum __va_arg_type
{
3716 __va_gen_reg
, __va_float_reg
, __va_stack
3724 bt
= type
.t
& VT_BTYPE
;
3725 if (bt
== VT_STRUCT
|| bt
== VT_LDOUBLE
) {
3727 } else if (bt
== VT_FLOAT
|| bt
== VT_DOUBLE
) {
3728 vpushi(__va_float_reg
);
3730 vpushi(__va_gen_reg
);
3750 goto tok_identifier
;
3752 /* allow to take the address of a label */
3753 if (tok
< TOK_UIDENT
)
3754 expect("label identifier");
3755 s
= label_find(tok
);
3757 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
3759 if (s
->r
== LABEL_DECLARED
)
3760 s
->r
= LABEL_FORWARD
;
3763 s
->type
.t
= VT_VOID
;
3764 mk_pointer(&s
->type
);
3765 s
->type
.t
|= VT_STATIC
;
3767 vset(&s
->type
, VT_CONST
| VT_SYM
, 0);
3772 // special qnan , snan and infinity values
3774 vpush64(VT_DOUBLE
, 0x7ff8000000000000ULL
);
3778 vpush64(VT_DOUBLE
, 0x7ff0000000000001ULL
);
3782 vpush64(VT_DOUBLE
, 0x7ff0000000000000ULL
);
3791 expect("identifier");
3795 tcc_error("'%s' undeclared", get_tok_str(t
, NULL
));
3796 /* for simple function calls, we tolerate undeclared
3797 external reference to int() function */
3798 if (tcc_state
->warn_implicit_function_declaration
)
3799 tcc_warning("implicit declaration of function '%s'",
3800 get_tok_str(t
, NULL
));
3801 s
= external_global_sym(t
, &func_old_type
, 0);
3803 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
3804 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
3805 /* if referencing an inline function, then we generate a
3806 symbol to it if not already done. It will have the
3807 effect to generate code for it at the end of the
3808 compilation unit. Inline function as always
3809 generated in the text section. */
3811 put_extern_sym(s
, text_section
, 0, 0);
3812 r
= VT_SYM
| VT_CONST
;
3816 vset(&s
->type
, r
, s
->c
);
3817 /* if forward reference, we must point to s */
3818 if (vtop
->r
& VT_SYM
) {
3825 /* post operations */
3827 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
3830 } else if (tok
== '.' || tok
== TOK_ARROW
) {
3833 if (tok
== TOK_ARROW
)
3835 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
3839 /* expect pointer on structure */
3840 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
3841 expect("struct or union");
3845 while ((s
= s
->next
) != NULL
) {
3850 tcc_error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, NULL
));
3851 /* add field offset to pointer */
3852 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
3855 /* change type to field type, and set to lvalue */
3856 vtop
->type
= s
->type
;
3857 vtop
->type
.t
|= qualifiers
;
3858 /* an array is never an lvalue */
3859 if (!(vtop
->type
.t
& VT_ARRAY
)) {
3860 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3861 #ifdef CONFIG_TCC_BCHECK
3862 /* if bound checking, the referenced pointer must be checked */
3863 if (tcc_state
->do_bounds_check
)
3864 vtop
->r
|= VT_MUSTBOUND
;
3868 } else if (tok
== '[') {
3874 } else if (tok
== '(') {
3880 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3881 /* pointer test (no array accepted) */
3882 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
3883 vtop
->type
= *pointed_type(&vtop
->type
);
3884 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
3888 expect("function pointer");
3891 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
3893 /* get return type */
3896 sa
= s
->next
; /* first parameter */
3899 /* compute first implicit argument if a structure is returned */
3900 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
3901 /* get some space for the returned structure */
3902 size
= type_size(&s
->type
, &align
);
3903 loc
= (loc
- size
) & -align
;
3905 ret
.r
= VT_LOCAL
| VT_LVAL
;
3906 /* pass it as 'int' to avoid structure arg passing
3908 vseti(VT_LOCAL
, loc
);
3913 /* return in register */
3914 if (is_float(ret
.type
.t
)) {
3915 ret
.r
= reg_fret(ret
.type
.t
);
3917 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
3926 gfunc_param_typed(s
, sa
);
3936 tcc_error("too few arguments to function");
3938 if (!nocode_wanted
) {
3939 gfunc_call(nb_args
);
3941 vtop
-= (nb_args
+ 1);
3944 vsetc(&ret
.type
, ret
.r
, &ret
.c
);
3952 ST_FUNC
void expr_prod(void)
3957 while (tok
== '*' || tok
== '/' || tok
== '%') {
3965 ST_FUNC
void expr_sum(void)
3970 while (tok
== '+' || tok
== '-') {
3978 static void expr_shift(void)
3983 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
3991 static void expr_cmp(void)
3996 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
3997 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
4005 static void expr_cmpeq(void)
4010 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
4018 static void expr_and(void)
4021 while (tok
== '&') {
4028 static void expr_xor(void)
4031 while (tok
== '^') {
4038 static void expr_or(void)
4041 while (tok
== '|') {
4048 /* XXX: fix this mess */
4049 static void expr_land_const(void)
4052 while (tok
== TOK_LAND
) {
4059 /* XXX: fix this mess */
4060 static void expr_lor_const(void)
4063 while (tok
== TOK_LOR
) {
4070 /* only used if non constant */
4071 static void expr_land(void)
4076 if (tok
== TOK_LAND
) {
4081 if (tok
!= TOK_LAND
) {
4091 static void expr_lor(void)
4096 if (tok
== TOK_LOR
) {
4101 if (tok
!= TOK_LOR
) {
4111 /* XXX: better constant handling */
4112 static void expr_cond(void)
4114 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
;
4116 CType type
, type1
, type2
;
4123 boolean
.t
= VT_BOOL
;
4129 if (tok
!= ':' || !gnu_ext
) {
4144 if (vtop
!= vstack
) {
4145 /* needed to avoid having different registers saved in
4147 if (is_float(vtop
->type
.t
)) {
4149 #ifdef TCC_TARGET_X86_64
4150 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4160 if (tok
== ':' && gnu_ext
) {
4168 sv
= *vtop
; /* save value to handle it later */
4169 vtop
--; /* no vpop so that FP stack is not flushed */
4177 bt1
= t1
& VT_BTYPE
;
4179 bt2
= t2
& VT_BTYPE
;
4180 /* cast operands to correct type according to ISOC rules */
4181 if (is_float(bt1
) || is_float(bt2
)) {
4182 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
4183 type
.t
= VT_LDOUBLE
;
4184 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
4189 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
4190 /* cast to biggest op */
4192 /* convert to unsigned if it does not fit in a long long */
4193 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
4194 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
4195 type
.t
|= VT_UNSIGNED
;
4196 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
4197 /* If one is a null ptr constant the result type
4199 if (is_null_pointer (vtop
))
4201 else if (is_null_pointer (&sv
))
4203 /* XXX: test pointer compatibility, C99 has more elaborate
4207 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
4208 /* XXX: test function pointer compatibility */
4209 type
= bt1
== VT_FUNC
? type1
: type2
;
4210 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
4211 /* XXX: test structure compatibility */
4212 type
= bt1
== VT_STRUCT
? type1
: type2
;
4213 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
4214 /* NOTE: as an extension, we accept void on only one side */
4217 /* integer operations */
4219 /* convert to unsigned if it does not fit in an integer */
4220 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
4221 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
4222 type
.t
|= VT_UNSIGNED
;
4225 /* now we convert second operand */
4227 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4230 if (is_float(type
.t
)) {
4232 #ifdef TCC_TARGET_X86_64
4233 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4237 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
4238 /* for long longs, we use fixed registers to avoid having
4239 to handle a complicated move */
4244 /* this is horrible, but we must also convert first
4248 /* put again first value and cast it */
4251 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4261 static void expr_eq(void)
4267 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
4268 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
4269 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
4284 ST_FUNC
void gexpr(void)
4295 /* parse an expression and return its type without any side effect. */
4296 static void expr_type(CType
*type
)
4298 int saved_nocode_wanted
;
4300 saved_nocode_wanted
= nocode_wanted
;
4305 nocode_wanted
= saved_nocode_wanted
;
4308 /* parse a unary expression and return its type without any side
4310 static void unary_type(CType
*type
)
4322 /* parse a constant expression and return value in vtop. */
4323 static void expr_const1(void)
4332 /* parse an integer constant and return its value. */
4333 ST_FUNC
int expr_const(void)
4337 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
4338 expect("constant expression");
4344 /* return the label token if current token is a label, otherwise
4346 static int is_label(void)
4350 /* fast test first */
4351 if (tok
< TOK_UIDENT
)
4353 /* no need to save tokc because tok is an identifier */
4360 unget_tok(last_tok
);
4365 static void label_or_decl(int l
)
4369 /* fast test first */
4370 if (tok
>= TOK_UIDENT
)
4372 /* no need to save tokc because tok is an identifier */
4376 unget_tok(last_tok
);
4379 unget_tok(last_tok
);
4384 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
4385 int case_reg
, int is_expr
)
4388 Sym
*s
, *frame_bottom
;
4390 /* generate line number info */
4391 if (tcc_state
->do_debug
&&
4392 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
4393 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
4395 last_line_num
= file
->line_num
;
4399 /* default return value is (void) */
4401 vtop
->type
.t
= VT_VOID
;
4404 if (tok
== TOK_IF
) {
4411 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4413 if (c
== TOK_ELSE
) {
4417 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4418 gsym(d
); /* patch else jmp */
4421 } else if (tok
== TOK_WHILE
) {
4429 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4433 } else if (tok
== '{') {
4437 /* record local declaration stack position */
4439 frame_bottom
= sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4440 frame_bottom
->next
= scope_stack_bottom
;
4441 scope_stack_bottom
= frame_bottom
;
4442 llabel
= local_label_stack
;
4443 /* handle local labels declarations */
4444 if (tok
== TOK_LABEL
) {
4447 if (tok
< TOK_UIDENT
)
4448 expect("label identifier");
4449 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
4459 while (tok
!= '}') {
4460 label_or_decl(VT_LOCAL
);
4464 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4467 /* pop locally defined labels */
4468 label_pop(&local_label_stack
, llabel
);
4470 /* XXX: this solution makes only valgrind happy...
4471 triggered by gcc.c-torture/execute/20000917-1.c */
4473 switch(vtop
->type
.t
& VT_BTYPE
) {
4478 for(p
=vtop
->type
.ref
;p
;p
=p
->prev
)
4480 tcc_error("unsupported expression type");
4483 /* pop locally defined symbols */
4484 scope_stack_bottom
= scope_stack_bottom
->next
;
4485 sym_pop(&local_stack
, s
);
4487 } else if (tok
== TOK_RETURN
) {
4491 gen_assign_cast(&func_vt
);
4492 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
4494 /* if returning structure, must copy it to implicit
4495 first pointer arg location */
4498 size
= type_size(&func_vt
,&align
);
4501 if((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) || (vtop
->c
.i
& 3))
4505 loc
= (loc
- size
) & -4;
4508 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
4511 vset(&int_type
, VT_LOCAL
| VT_LVAL
, addr
);
4513 vtop
->type
= int_type
;
4519 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
4522 /* copy structure value to pointer */
4527 } else if (is_float(func_vt
.t
)) {
4528 gv(rc_fret(func_vt
.t
));
4532 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
4535 rsym
= gjmp(rsym
); /* jmp */
4536 } else if (tok
== TOK_BREAK
) {
4539 tcc_error("cannot break");
4540 *bsym
= gjmp(*bsym
);
4543 } else if (tok
== TOK_CONTINUE
) {
4546 tcc_error("cannot continue");
4547 *csym
= gjmp(*csym
);
4550 } else if (tok
== TOK_FOR
) {
4555 frame_bottom
= sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4556 frame_bottom
->next
= scope_stack_bottom
;
4557 scope_stack_bottom
= frame_bottom
;
4559 /* c99 for-loop init decl? */
4560 if (!decl0(VT_LOCAL
, 1)) {
4561 /* no, regular for-loop init expr */
4585 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4589 scope_stack_bottom
= scope_stack_bottom
->next
;
4590 sym_pop(&local_stack
, s
);
4592 if (tok
== TOK_DO
) {
4597 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4608 if (tok
== TOK_SWITCH
) {
4612 /* XXX: other types than integer */
4613 case_reg
= gv(RC_INT
);
4617 b
= gjmp(0); /* jump to first case */
4619 block(&a
, csym
, &b
, &c
, case_reg
, 0);
4620 /* if no default, jmp after switch */
4628 if (tok
== TOK_CASE
) {
4635 if (gnu_ext
&& tok
== TOK_DOTS
) {
4639 tcc_warning("empty case range");
4641 /* since a case is like a label, we must skip it with a jmp */
4648 *case_sym
= gtst(1, 0);
4651 *case_sym
= gtst(1, 0);
4655 *case_sym
= gtst(1, *case_sym
);
4660 goto block_after_label
;
4662 if (tok
== TOK_DEFAULT
) {
4668 tcc_error("too many 'default'");
4671 goto block_after_label
;
4673 if (tok
== TOK_GOTO
) {
4675 if (tok
== '*' && gnu_ext
) {
4679 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
4682 } else if (tok
>= TOK_UIDENT
) {
4683 s
= label_find(tok
);
4684 /* put forward definition if needed */
4686 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4688 if (s
->r
== LABEL_DECLARED
)
4689 s
->r
= LABEL_FORWARD
;
4691 /* label already defined */
4692 if (s
->r
& LABEL_FORWARD
)
4693 s
->jnext
= gjmp(s
->jnext
);
4695 gjmp_addr(s
->jnext
);
4698 expect("label identifier");
4701 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
4709 if (s
->r
== LABEL_DEFINED
)
4710 tcc_error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
4712 s
->r
= LABEL_DEFINED
;
4714 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
4717 /* we accept this, but it is a mistake */
4720 tcc_warning("deprecated use of label at end of compound statement");
4724 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4727 /* expression case */
4742 /* t is the array or struct type. c is the array or struct
4743 address. cur_index/cur_field is the pointer to the current
4744 value. 'size_only' is true if only size info is needed (only used
4746 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
4747 int *cur_index
, Sym
**cur_field
,
4751 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
4757 if (gnu_ext
&& (l
= is_label()) != 0)
4759 while (tok
== '[' || tok
== '.') {
4761 if (!(type
->t
& VT_ARRAY
))
4762 expect("array type");
4765 index
= expr_const();
4766 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
4767 expect("invalid index");
4768 if (tok
== TOK_DOTS
&& gnu_ext
) {
4770 index_last
= expr_const();
4771 if (index_last
< 0 ||
4772 (s
->c
>= 0 && index_last
>= s
->c
) ||
4774 expect("invalid index");
4780 *cur_index
= index_last
;
4781 type
= pointed_type(type
);
4782 elem_size
= type_size(type
, &align
);
4783 c
+= index
* elem_size
;
4784 /* NOTE: we only support ranges for last designator */
4785 nb_elems
= index_last
- index
+ 1;
4786 if (nb_elems
!= 1) {
4795 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
4796 expect("struct/union type");
4809 /* XXX: fix this mess by using explicit storage field */
4811 type1
.t
|= (type
->t
& ~VT_TYPE
);
4825 if (type
->t
& VT_ARRAY
) {
4827 type
= pointed_type(type
);
4828 c
+= index
* type_size(type
, &align
);
4832 tcc_error("too many field init");
4833 /* XXX: fix this mess by using explicit storage field */
4835 type1
.t
|= (type
->t
& ~VT_TYPE
);
4840 decl_initializer(type
, sec
, c
, 0, size_only
);
4842 /* XXX: make it more general */
4843 if (!size_only
&& nb_elems
> 1) {
4844 unsigned long c_end
;
4849 tcc_error("range init not supported yet for dynamic storage");
4850 c_end
= c
+ nb_elems
* elem_size
;
4851 if (c_end
> sec
->data_allocated
)
4852 section_realloc(sec
, c_end
);
4853 src
= sec
->data
+ c
;
4855 for(i
= 1; i
< nb_elems
; i
++) {
4857 memcpy(dst
, src
, elem_size
);
4863 #define EXPR_CONST 1
4866 /* store a value or an expression directly in global data or in local array */
4867 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
4868 int v
, int expr_type
)
4870 int saved_global_expr
, bt
, bit_pos
, bit_size
;
4872 unsigned long long bit_mask
;
4880 /* compound literals must be allocated globally in this case */
4881 saved_global_expr
= global_expr
;
4884 global_expr
= saved_global_expr
;
4885 /* NOTE: symbols are accepted */
4886 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
4887 tcc_error("initializer element is not constant");
4895 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
4898 /* XXX: not portable */
4899 /* XXX: generate error if incorrect relocation */
4900 gen_assign_cast(&dtype
);
4901 bt
= type
->t
& VT_BTYPE
;
4902 /* we'll write at most 12 bytes */
4903 if (c
+ 12 > sec
->data_allocated
) {
4904 section_realloc(sec
, c
+ 12);
4906 ptr
= sec
->data
+ c
;
4907 /* XXX: make code faster ? */
4908 if (!(type
->t
& VT_BITFIELD
)) {
4913 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4914 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
4915 bit_mask
= (1LL << bit_size
) - 1;
4917 if ((vtop
->r
& VT_SYM
) &&
4923 (bt
== VT_INT
&& bit_size
!= 32)))
4924 tcc_error("initializer element is not computable at load time");
4927 vtop
->c
.i
= (vtop
->c
.i
!= 0);
4929 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4932 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4935 *(double *)ptr
= vtop
->c
.d
;
4938 *(long double *)ptr
= vtop
->c
.ld
;
4941 *(long long *)ptr
|= (vtop
->c
.ll
& bit_mask
) << bit_pos
;
4944 if (vtop
->r
& VT_SYM
) {
4945 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
4947 *(int *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4952 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
4959 /* put zeros for variable based init */
4960 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
4963 /* nothing to do because globals are already set to zero */
4965 vpush_global_sym(&func_old_type
, TOK_memset
);
4973 /* 't' contains the type and storage info. 'c' is the offset of the
4974 object in section 'sec'. If 'sec' is NULL, it means stack based
4975 allocation. 'first' is true if array '{' must be read (multi
4976 dimension implicit array init handling). 'size_only' is true if
4977 size only evaluation is wanted (only for arrays). */
4978 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
4979 int first
, int size_only
)
4981 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, parlevel1
, i
;
4982 int size1
, align1
, expr_type
;
4986 if (type
->t
& VT_VLA
) {
4987 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
4991 vpush_global_sym(&func_old_type
, TOK_alloca
);
4992 vla_runtime_type_size(type
, &a
);
4997 vsetc(type
, REG_IRET
, &retcval
);
4998 vset(type
, VT_LOCAL
|VT_LVAL
, c
);
5003 tcc_error("variable length arrays unsupported for this target");
5005 } else if (type
->t
& VT_ARRAY
) {
5009 t1
= pointed_type(type
);
5010 size1
= type_size(t1
, &align1
);
5013 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
5016 tcc_error("character array initializer must be a literal,"
5017 " optionally enclosed in braces");
5022 /* only parse strings here if correct type (otherwise: handle
5023 them as ((w)char *) expressions */
5024 if ((tok
== TOK_LSTR
&&
5025 #ifdef TCC_TARGET_PE
5026 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
5028 (t1
->t
& VT_BTYPE
) == VT_INT
5030 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
5031 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5036 /* compute maximum number of chars wanted */
5038 cstr_len
= cstr
->size
;
5040 cstr_len
= cstr
->size
/ sizeof(nwchar_t
);
5043 if (n
>= 0 && nb
> (n
- array_length
))
5044 nb
= n
- array_length
;
5047 tcc_warning("initializer-string for array is too long");
5048 /* in order to go faster for common case (char
5049 string in global variable, we handle it
5051 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
5052 memcpy(sec
->data
+ c
+ array_length
, cstr
->data
, nb
);
5056 ch
= ((unsigned char *)cstr
->data
)[i
];
5058 ch
= ((nwchar_t
*)cstr
->data
)[i
];
5059 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
5067 /* only add trailing zero if enough storage (no
5068 warning in this case since it is standard) */
5069 if (n
< 0 || array_length
< n
) {
5071 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
5077 while (tok
!= '}') {
5078 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
5079 if (n
>= 0 && index
>= n
)
5080 tcc_error("index too large");
5081 /* must put zero in holes (note that doing it that way
5082 ensures that it even works with designators) */
5083 if (!size_only
&& array_length
< index
) {
5084 init_putz(t1
, sec
, c
+ array_length
* size1
,
5085 (index
- array_length
) * size1
);
5088 if (index
> array_length
)
5089 array_length
= index
;
5090 /* special test for multi dimensional arrays (may not
5091 be strictly correct if designators are used at the
5093 if (index
>= n
&& no_oblock
)
5102 /* put zeros at the end */
5103 if (!size_only
&& n
>= 0 && array_length
< n
) {
5104 init_putz(t1
, sec
, c
+ array_length
* size1
,
5105 (n
- array_length
) * size1
);
5107 /* patch type size if needed */
5109 s
->c
= array_length
;
5110 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
5111 (sec
|| !first
|| tok
== '{')) {
5114 /* NOTE: the previous test is a specific case for automatic
5115 struct/union init */
5116 /* XXX: union needs only one init */
5118 /* XXX: this test is incorrect for local initializers
5119 beginning with ( without {. It would be much more difficult
5120 to do it correctly (ideally, the expression parser should
5121 be used in all cases) */
5127 while (tok
== '(') {
5131 if (!parse_btype(&type1
, &ad1
))
5133 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
5135 if (!is_assignable_types(type
, &type1
))
5136 tcc_error("invalid type for cast");
5141 if (first
|| tok
== '{') {
5150 while (tok
!= '}') {
5151 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
5153 if (!size_only
&& array_length
< index
) {
5154 init_putz(type
, sec
, c
+ array_length
,
5155 index
- array_length
);
5157 index
= index
+ type_size(&f
->type
, &align1
);
5158 if (index
> array_length
)
5159 array_length
= index
;
5161 /* gr: skip fields from same union - ugly. */
5163 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
5164 /* test for same offset */
5165 if (f
->next
->c
!= f
->c
)
5167 /* if yes, test for bitfield shift */
5168 if ((f
->type
.t
& VT_BITFIELD
) && (f
->next
->type
.t
& VT_BITFIELD
)) {
5169 int bit_pos_1
= (f
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5170 int bit_pos_2
= (f
->next
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5171 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
5172 if (bit_pos_1
!= bit_pos_2
)
5179 if (no_oblock
&& f
== NULL
)
5185 /* put zeros at the end */
5186 if (!size_only
&& array_length
< n
) {
5187 init_putz(type
, sec
, c
+ array_length
,
5196 } else if (tok
== '{') {
5198 decl_initializer(type
, sec
, c
, first
, size_only
);
5200 } else if (size_only
) {
5201 /* just skip expression */
5202 parlevel
= parlevel1
= 0;
5203 while ((parlevel
> 0 || parlevel1
> 0 ||
5204 (tok
!= '}' && tok
!= ',')) && tok
!= -1) {
5207 else if (tok
== ')')
5209 else if (tok
== '{')
5211 else if (tok
== '}')
5216 /* currently, we always use constant expression for globals
5217 (may change for scripting case) */
5218 expr_type
= EXPR_CONST
;
5220 expr_type
= EXPR_ANY
;
5221 init_putv(type
, sec
, c
, 0, expr_type
);
5225 /* parse an initializer for type 't' if 'has_init' is non zero, and
5226 allocate space in local or global data space ('r' is either
5227 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5228 variable 'v' with an associated name represented by 'asm_label' of
5229 scope 'scope' is declared before initializers are parsed. If 'v' is
5230 zero, then a reference to the new object is put in the value stack.
5231 If 'has_init' is 2, a special parsing is done to handle string
5233 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
5234 int has_init
, int v
, char *asm_label
,
5237 int size
, align
, addr
, data_offset
;
5239 ParseState saved_parse_state
= {0};
5240 TokenString init_str
;
5242 Sym
*flexible_array
;
5244 flexible_array
= NULL
;
5245 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
5248 while (field
&& field
->next
)
5249 field
= field
->next
;
5250 if (field
->type
.t
& VT_ARRAY
&& field
->type
.ref
->c
< 0)
5251 flexible_array
= field
;
5254 size
= type_size(type
, &align
);
5255 /* If unknown size, we must evaluate it before
5256 evaluating initializers because
5257 initializers can generate global data too
5258 (e.g. string pointers or ISOC99 compound
5259 literals). It also simplifies local
5260 initializers handling */
5261 tok_str_new(&init_str
);
5262 if (size
< 0 || (flexible_array
&& has_init
)) {
5264 tcc_error("unknown type size");
5265 /* get all init string */
5266 if (has_init
== 2) {
5267 /* only get strings */
5268 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5269 tok_str_add_tok(&init_str
);
5274 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
5276 tcc_error("unexpected end of file in initializer");
5277 tok_str_add_tok(&init_str
);
5280 else if (tok
== '}') {
5290 tok_str_add(&init_str
, -1);
5291 tok_str_add(&init_str
, 0);
5294 save_parse_state(&saved_parse_state
);
5296 macro_ptr
= init_str
.str
;
5298 decl_initializer(type
, NULL
, 0, 1, 1);
5299 /* prepare second initializer parsing */
5300 macro_ptr
= init_str
.str
;
5303 /* if still unknown size, error */
5304 size
= type_size(type
, &align
);
5306 tcc_error("unknown type size");
5309 size
+= flexible_array
->type
.ref
->c
* pointed_size(&flexible_array
->type
);
5310 /* take into account specified alignment if bigger */
5312 if (ad
->aligned
> align
)
5313 align
= ad
->aligned
;
5314 } else if (ad
->packed
) {
5317 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
5319 #ifdef CONFIG_TCC_BCHECK
5320 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5324 loc
= (loc
- size
) & -align
;
5326 #ifdef CONFIG_TCC_BCHECK
5327 /* handles bounds */
5328 /* XXX: currently, since we do only one pass, we cannot track
5329 '&' operators, so we add only arrays */
5330 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5331 unsigned long *bounds_ptr
;
5332 /* add padding between regions */
5334 /* then add local bound info */
5335 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(unsigned long));
5336 bounds_ptr
[0] = addr
;
5337 bounds_ptr
[1] = size
;
5341 /* local variable */
5342 sym_push(v
, type
, r
, addr
);
5344 /* push local reference */
5345 vset(type
, r
, addr
);
5351 if (v
&& scope
== VT_CONST
) {
5352 /* see if the symbol was already defined */
5355 if (!is_compatible_types(&sym
->type
, type
))
5356 tcc_error("incompatible types for redefinition of '%s'",
5357 get_tok_str(v
, NULL
));
5358 if (sym
->type
.t
& VT_EXTERN
) {
5359 /* if the variable is extern, it was not allocated */
5360 sym
->type
.t
&= ~VT_EXTERN
;
5361 /* set array size if it was ommited in extern
5363 if ((sym
->type
.t
& VT_ARRAY
) &&
5364 sym
->type
.ref
->c
< 0 &&
5366 sym
->type
.ref
->c
= type
->ref
->c
;
5368 /* we accept several definitions of the same
5369 global variable. this is tricky, because we
5370 must play with the SHN_COMMON type of the symbol */
5371 /* XXX: should check if the variable was already
5372 initialized. It is incorrect to initialized it
5374 /* no init data, we won't add more to the symbol */
5381 /* allocate symbol in corresponding section */
5386 else if (tcc_state
->nocommon
)
5390 data_offset
= sec
->data_offset
;
5391 data_offset
= (data_offset
+ align
- 1) & -align
;
5393 /* very important to increment global pointer at this time
5394 because initializers themselves can create new initializers */
5395 data_offset
+= size
;
5396 #ifdef CONFIG_TCC_BCHECK
5397 /* add padding if bound check */
5398 if (tcc_state
->do_bounds_check
)
5401 sec
->data_offset
= data_offset
;
5402 /* allocate section space to put the data */
5403 if (sec
->sh_type
!= SHT_NOBITS
&&
5404 data_offset
> sec
->data_allocated
)
5405 section_realloc(sec
, data_offset
);
5406 /* align section if needed */
5407 if (align
> sec
->sh_addralign
)
5408 sec
->sh_addralign
= align
;
5410 addr
= 0; /* avoid warning */
5414 if (scope
!= VT_CONST
|| !sym
) {
5415 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
5416 sym
->asm_label
= asm_label
;
5418 /* update symbol definition */
5420 put_extern_sym(sym
, sec
, addr
, size
);
5423 /* put a common area */
5424 put_extern_sym(sym
, NULL
, align
, size
);
5425 /* XXX: find a nicer way */
5426 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
5427 esym
->st_shndx
= SHN_COMMON
;
5432 /* push global reference */
5433 sym
= get_sym_ref(type
, sec
, addr
, size
);
5435 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
5438 /* patch symbol weakness */
5439 if (type
->t
& VT_WEAK
)
5441 #ifdef CONFIG_TCC_BCHECK
5442 /* handles bounds now because the symbol must be defined
5443 before for the relocation */
5444 if (tcc_state
->do_bounds_check
) {
5445 unsigned long *bounds_ptr
;
5447 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
5448 /* then add global bound info */
5449 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(long));
5450 bounds_ptr
[0] = 0; /* relocated */
5451 bounds_ptr
[1] = size
;
5455 if (has_init
|| (type
->t
& VT_VLA
)) {
5456 decl_initializer(type
, sec
, addr
, 1, 0);
5457 /* restore parse state if needed */
5459 tok_str_free(init_str
.str
);
5460 restore_parse_state(&saved_parse_state
);
5462 /* patch flexible array member size back to -1, */
5463 /* for possible subsequent similar declarations */
5465 flexible_array
->type
.ref
->c
= -1;
5470 static void put_func_debug(Sym
*sym
)
5475 /* XXX: we put here a dummy type */
5476 snprintf(buf
, sizeof(buf
), "%s:%c1",
5477 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
5478 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
5479 cur_text_section
, sym
->c
);
5480 /* //gr gdb wants a line at the function */
5481 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
5486 /* parse an old style function declaration list */
5487 /* XXX: check multiple parameter */
5488 static void func_decl_list(Sym
*func_sym
)
5495 /* parse each declaration */
5496 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
&&
5497 tok
!= TOK_ASM1
&& tok
!= TOK_ASM2
&& tok
!= TOK_ASM3
) {
5498 if (!parse_btype(&btype
, &ad
))
5499 expect("declaration list");
5500 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5501 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5503 /* we accept no variable after */
5507 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5508 /* find parameter in function parameter list */
5511 if ((s
->v
& ~SYM_FIELD
) == v
)
5515 tcc_error("declaration for parameter '%s' but no such parameter",
5516 get_tok_str(v
, NULL
));
5518 /* check that no storage specifier except 'register' was given */
5519 if (type
.t
& VT_STORAGE
)
5520 tcc_error("storage class specified for '%s'", get_tok_str(v
, NULL
));
5521 convert_parameter_type(&type
);
5522 /* we can add the type (NOTE: it could be local to the function) */
5524 /* accept other parameters */
5535 /* parse a function defined by symbol 'sym' and generate its code in
5536 'cur_text_section' */
5537 static void gen_function(Sym
*sym
)
5539 int saved_nocode_wanted
= nocode_wanted
;
5541 ind
= cur_text_section
->data_offset
;
5542 /* NOTE: we patch the symbol size later */
5543 put_extern_sym(sym
, cur_text_section
, ind
, 0);
5544 funcname
= get_tok_str(sym
->v
, NULL
);
5546 /* put debug symbol */
5547 if (tcc_state
->do_debug
)
5548 put_func_debug(sym
);
5549 /* push a dummy symbol to enable local sym storage */
5550 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
5551 gfunc_prolog(&sym
->type
);
5553 block(NULL
, NULL
, NULL
, NULL
, 0, 0);
5556 cur_text_section
->data_offset
= ind
;
5557 label_pop(&global_label_stack
, NULL
);
5558 /* reset local stack */
5559 scope_stack_bottom
= NULL
;
5560 sym_pop(&local_stack
, NULL
);
5561 /* end of function */
5562 /* patch symbol size */
5563 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
5565 /* patch symbol weakness (this definition overrules any prototype) */
5566 if (sym
->type
.t
& VT_WEAK
)
5568 if (tcc_state
->do_debug
) {
5569 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
5571 /* It's better to crash than to generate wrong code */
5572 cur_text_section
= NULL
;
5573 funcname
= ""; /* for safety */
5574 func_vt
.t
= VT_VOID
; /* for safety */
5575 ind
= 0; /* for safety */
5576 nocode_wanted
= saved_nocode_wanted
;
5579 ST_FUNC
void gen_inline_functions(void)
5582 int *str
, inline_generated
, i
;
5583 struct InlineFunc
*fn
;
5585 /* iterate while inline function are referenced */
5587 inline_generated
= 0;
5588 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5589 fn
= tcc_state
->inline_fns
[i
];
5591 if (sym
&& sym
->c
) {
5592 /* the function was used: generate its code and
5593 convert it to a normal function */
5594 str
= fn
->token_str
;
5597 strcpy(file
->filename
, fn
->filename
);
5598 sym
->r
= VT_SYM
| VT_CONST
;
5599 sym
->type
.t
&= ~VT_INLINE
;
5603 cur_text_section
= text_section
;
5605 macro_ptr
= NULL
; /* fail safe */
5607 inline_generated
= 1;
5610 if (!inline_generated
)
5613 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5614 fn
= tcc_state
->inline_fns
[i
];
5615 str
= fn
->token_str
;
5618 dynarray_reset(&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
);
5621 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
5622 static int decl0(int l
, int is_for_loop_init
)
5630 if (!parse_btype(&btype
, &ad
)) {
5631 if (is_for_loop_init
)
5633 /* skip redundant ';' */
5634 /* XXX: find more elegant solution */
5639 if (l
== VT_CONST
&&
5640 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5641 /* global asm block */
5645 /* special test for old K&R protos without explicit int
5646 type. Only accepted when defining global data */
5647 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
5651 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5652 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5654 /* we accept no variable after */
5658 while (1) { /* iterate thru each declaration */
5659 char *asm_label
; // associated asm label
5661 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5665 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
5666 printf("type = '%s'\n", buf
);
5669 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5670 if ((type
.t
& VT_STATIC
) && (l
== VT_LOCAL
)) {
5671 tcc_error("function without file scope cannot be static");
5673 /* if old style function prototype, we accept a
5676 if (sym
->c
== FUNC_OLD
)
5677 func_decl_list(sym
);
5681 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5684 asm_label_instr(&astr
);
5685 asm_label
= tcc_strdup(astr
.data
);
5688 /* parse one last attribute list, after asm label */
5689 parse_attribute(&ad
);
5694 #ifdef TCC_TARGET_PE
5696 type
.t
|= VT_IMPORT
;
5698 type
.t
|= VT_EXPORT
;
5702 tcc_error("cannot use local functions");
5703 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
5704 expect("function definition");
5706 /* reject abstract declarators in function definition */
5708 while ((sym
= sym
->next
) != NULL
)
5709 if (!(sym
->v
& ~SYM_FIELD
))
5710 expect("identifier");
5712 /* XXX: cannot do better now: convert extern line to static inline */
5713 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
5714 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5718 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
5721 r
= sym
->type
.ref
->r
;
5722 /* use func_call from prototype if not defined */
5723 if (FUNC_CALL(r
) != FUNC_CDECL
5724 && FUNC_CALL(type
.ref
->r
) == FUNC_CDECL
)
5725 FUNC_CALL(type
.ref
->r
) = FUNC_CALL(r
);
5727 /* use export from prototype */
5729 FUNC_EXPORT(type
.ref
->r
) = 1;
5731 /* use static from prototype */
5732 if (sym
->type
.t
& VT_STATIC
)
5733 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5735 if (!is_compatible_types(&sym
->type
, &type
)) {
5737 tcc_error("incompatible types for redefinition of '%s'",
5738 get_tok_str(v
, NULL
));
5740 /* if symbol is already defined, then put complete type */
5743 /* put function symbol */
5744 sym
= global_identifier_push(v
, type
.t
, 0);
5745 sym
->type
.ref
= type
.ref
;
5748 /* static inline functions are just recorded as a kind
5749 of macro. Their code will be emitted at the end of
5750 the compilation unit only if they are used */
5751 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
5752 (VT_INLINE
| VT_STATIC
)) {
5753 TokenString func_str
;
5755 struct InlineFunc
*fn
;
5756 const char *filename
;
5758 tok_str_new(&func_str
);
5764 tcc_error("unexpected end of file");
5765 tok_str_add_tok(&func_str
);
5770 } else if (t
== '}') {
5772 if (block_level
== 0)
5776 tok_str_add(&func_str
, -1);
5777 tok_str_add(&func_str
, 0);
5778 filename
= file
? file
->filename
: "";
5779 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
5780 strcpy(fn
->filename
, filename
);
5782 fn
->token_str
= func_str
.str
;
5783 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
5786 /* compute text section */
5787 cur_text_section
= ad
.section
;
5788 if (!cur_text_section
)
5789 cur_text_section
= text_section
;
5790 sym
->r
= VT_SYM
| VT_CONST
;
5795 if (btype
.t
& VT_TYPEDEF
) {
5796 /* save typedefed type */
5797 /* XXX: test storage specifiers ? */
5798 sym
= sym_push(v
, &type
, INT_ATTR(&ad
), 0);
5799 sym
->type
.t
|= VT_TYPEDEF
;
5802 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5803 /* external function definition */
5804 /* specific case for func_call attribute */
5805 type
.ref
->r
= INT_ATTR(&ad
);
5806 } else if (!(type
.t
& VT_ARRAY
)) {
5807 /* not lvalue if array */
5808 r
|= lvalue_type(type
.t
);
5810 has_init
= (tok
== '=');
5811 if (has_init
&& (type
.t
& VT_VLA
))
5812 tcc_error("Variable length array cannot be initialized");
5813 if ((btype
.t
& VT_EXTERN
) || ((type
.t
& VT_BTYPE
) == VT_FUNC
) ||
5814 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
5815 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
5816 /* external variable or function */
5817 /* NOTE: as GCC, uninitialized global static
5818 arrays of null size are considered as
5820 sym
= external_sym(v
, &type
, r
, asm_label
);
5822 if (type
.t
& VT_WEAK
)
5825 if (ad
.alias_target
) {
5830 alias_target
= sym_find(ad
.alias_target
);
5831 if (!alias_target
|| !alias_target
->c
)
5832 tcc_error("unsupported forward __alias__ attribute");
5833 esym
= &((Elf32_Sym
*)symtab_section
->data
)[alias_target
->c
];
5834 tsec
.sh_num
= esym
->st_shndx
;
5835 put_extern_sym2(sym
, &tsec
, esym
->st_value
, esym
->st_size
, 0);
5838 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
5839 if (type
.t
& VT_STATIC
)
5845 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, asm_label
, l
);
5849 if (is_for_loop_init
)
5862 ST_FUNC
void decl(int l
)