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
[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)
463 /* cannot let cpu flags if other instruction are generated. Also
464 avoid leaving VT_JMP anywhere except on the top of the stack
465 because it would complicate the code generator. */
466 if (vtop
>= vstack
) {
467 int v
= vtop
->r
& VT_VALMASK
;
468 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
476 ST_FUNC
void vpushv(SValue
*v
)
478 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
479 tcc_error("memory full");
484 static void vdup(void)
489 /* save r to the memory stack, and mark it as being free */
490 ST_FUNC
void save_reg(int r
)
492 int l
, saved
, size
, align
;
496 /* modify all stack values */
499 for(p
=vstack
;p
<=vtop
;p
++) {
500 if ((p
->r
& VT_VALMASK
) == r
||
501 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
502 /* must save value on stack if not already done */
504 /* NOTE: must reload 'r' because r might be equal to r2 */
505 r
= p
->r
& VT_VALMASK
;
506 /* store register in the stack */
508 if ((p
->r
& VT_LVAL
) ||
509 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
510 #ifdef TCC_TARGET_X86_64
511 type
= &char_pointer_type
;
515 size
= type_size(type
, &align
);
516 loc
= (loc
- size
) & -align
;
518 sv
.r
= VT_LOCAL
| VT_LVAL
;
521 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
522 /* x86 specific: need to pop fp register ST0 if saved */
524 o(0xd8dd); /* fstp %st(0) */
527 #ifndef TCC_TARGET_X86_64
528 /* special long long case */
529 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
537 /* mark that stack entry as being saved on the stack */
538 if (p
->r
& VT_LVAL
) {
539 /* also clear the bounded flag because the
540 relocation address of the function was stored in
542 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
544 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
552 #ifdef TCC_TARGET_ARM
553 /* find a register of class 'rc2' with at most one reference on stack.
554 * If none, call get_reg(rc) */
555 ST_FUNC
int get_reg_ex(int rc
, int rc2
)
560 for(r
=0;r
<NB_REGS
;r
++) {
561 if (reg_classes
[r
] & rc2
) {
564 for(p
= vstack
; p
<= vtop
; p
++) {
565 if ((p
->r
& VT_VALMASK
) == r
||
566 (p
->r2
& VT_VALMASK
) == r
)
577 /* find a free register of class 'rc'. If none, save one register */
578 ST_FUNC
int get_reg(int rc
)
583 /* find a free register */
584 for(r
=0;r
<NB_REGS
;r
++) {
585 if (reg_classes
[r
] & rc
) {
586 for(p
=vstack
;p
<=vtop
;p
++) {
587 if ((p
->r
& VT_VALMASK
) == r
||
588 (p
->r2
& VT_VALMASK
) == r
)
596 /* no register left : free the first one on the stack (VERY
597 IMPORTANT to start from the bottom to ensure that we don't
598 spill registers used in gen_opi()) */
599 for(p
=vstack
;p
<=vtop
;p
++) {
600 /* look at second register (if long long) */
601 r
= p
->r2
& VT_VALMASK
;
602 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
604 r
= p
->r
& VT_VALMASK
;
605 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
611 /* Should never comes here */
615 /* save registers up to (vtop - n) stack entry */
616 ST_FUNC
void save_regs(int n
)
621 for(p
= vstack
;p
<= p1
; p
++) {
622 r
= p
->r
& VT_VALMASK
;
629 /* move register 's' to 'r', and flush previous value of r to memory
631 static void move_reg(int r
, int s
)
644 /* get address of vtop (vtop MUST BE an lvalue) */
645 static void gaddrof(void)
647 if (vtop
->r
& VT_REF
)
650 /* tricky: if saved lvalue, then we can go back to lvalue */
651 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
652 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
657 #ifdef CONFIG_TCC_BCHECK
658 /* generate lvalue bound code */
659 static void gbound(void)
664 vtop
->r
&= ~VT_MUSTBOUND
;
665 /* if lvalue, then use checking code before dereferencing */
666 if (vtop
->r
& VT_LVAL
) {
667 /* if not VT_BOUNDED value, then make one */
668 if (!(vtop
->r
& VT_BOUNDED
)) {
669 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
670 /* must save type because we must set it to int to get pointer */
672 vtop
->type
.t
= VT_INT
;
675 gen_bounded_ptr_add();
676 vtop
->r
|= lval_type
;
679 /* then check for dereferencing */
680 gen_bounded_ptr_deref();
685 /* store vtop a register belonging to class 'rc'. lvalues are
686 converted to values. Cannot be used if cannot be converted to
687 register value (such as structures). */
688 ST_FUNC
int gv(int rc
)
690 int r
, bit_pos
, bit_size
, size
, align
, i
;
691 #ifndef TCC_TARGET_X86_64
695 /* NOTE: get_reg can modify vstack[] */
696 if (vtop
->type
.t
& VT_BITFIELD
) {
699 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
700 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
701 /* remove bit field info to avoid loops */
702 vtop
->type
.t
&= ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
703 /* cast to int to propagate signedness in following ops */
704 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
709 if((vtop
->type
.t
& VT_UNSIGNED
) ||
710 (vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
711 type
.t
|= VT_UNSIGNED
;
713 /* generate shifts */
714 vpushi(bits
- (bit_pos
+ bit_size
));
716 vpushi(bits
- bit_size
);
717 /* NOTE: transformed to SHR if unsigned */
721 if (is_float(vtop
->type
.t
) &&
722 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
725 unsigned long offset
;
726 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
730 /* XXX: unify with initializers handling ? */
731 /* CPUs usually cannot use float constants, so we store them
732 generically in data segment */
733 size
= type_size(&vtop
->type
, &align
);
734 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
735 data_section
->data_offset
= offset
;
736 /* XXX: not portable yet */
737 #if defined(__i386__) || defined(__x86_64__)
738 /* Zero pad x87 tenbyte long doubles */
739 if (size
== LDOUBLE_SIZE
) {
740 vtop
->c
.tab
[2] &= 0xffff;
741 #if LDOUBLE_SIZE == 16
746 ptr
= section_ptr_add(data_section
, size
);
748 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
752 ptr
[i
] = vtop
->c
.tab
[size
-1-i
];
756 ptr
[i
] = vtop
->c
.tab
[i
];
757 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
758 vtop
->r
|= VT_LVAL
| VT_SYM
;
762 #ifdef CONFIG_TCC_BCHECK
763 if (vtop
->r
& VT_MUSTBOUND
)
767 r
= vtop
->r
& VT_VALMASK
;
768 #ifndef TCC_TARGET_X86_64
773 /* need to reload if:
775 - lvalue (need to dereference pointer)
776 - already a register, but not in the right class */
778 || (vtop
->r
& VT_LVAL
)
779 || !(reg_classes
[r
] & rc
)
780 #ifndef TCC_TARGET_X86_64
781 || ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
786 #ifndef TCC_TARGET_X86_64
787 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
789 unsigned long long ll
;
790 /* two register type load : expand to two words
792 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
795 vtop
->c
.ui
= ll
; /* first word */
797 vtop
->r
= r
; /* save register value */
798 vpushi(ll
>> 32); /* second word */
799 } else if (r
>= VT_CONST
|| /* XXX: test to VT_CONST incorrect ? */
800 (vtop
->r
& VT_LVAL
)) {
801 /* We do not want to modifier the long long
802 pointer here, so the safest (and less
803 efficient) is to save all the other registers
804 in the stack. XXX: totally inefficient. */
806 /* load from memory */
809 vtop
[-1].r
= r
; /* save register value */
810 /* increment pointer to get second word */
811 vtop
->type
.t
= VT_INT
;
820 vtop
[-1].r
= r
; /* save register value */
821 vtop
->r
= vtop
[-1].r2
;
823 /* Allocate second register. Here we rely on the fact that
824 get_reg() tries first to free r2 of an SValue. */
828 /* write second register */
832 if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
834 /* lvalue of scalar type : need to use lvalue type
835 because of possible cast */
838 /* compute memory access type */
839 if (vtop
->r
& VT_LVAL_BYTE
)
841 else if (vtop
->r
& VT_LVAL_SHORT
)
843 if (vtop
->r
& VT_LVAL_UNSIGNED
)
847 /* restore wanted type */
850 /* one register type load */
855 #ifdef TCC_TARGET_C67
856 /* uses register pairs for doubles */
857 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
864 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
865 ST_FUNC
void gv2(int rc1
, int rc2
)
869 /* generate more generic register first. But VT_JMP or VT_CMP
870 values must be generated first in all cases to avoid possible
872 v
= vtop
[0].r
& VT_VALMASK
;
873 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
878 /* test if reload is needed for first register */
879 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
889 /* test if reload is needed for first register */
890 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
896 /* wrapper around RC_FRET to return a register by type */
897 static int rc_fret(int t
)
899 #ifdef TCC_TARGET_X86_64
900 if (t
== VT_LDOUBLE
) {
907 /* wrapper around REG_FRET to return a register by type */
908 static int reg_fret(int t
)
910 #ifdef TCC_TARGET_X86_64
911 if (t
== VT_LDOUBLE
) {
918 /* expand long long on stack in two int registers */
919 static void lexpand(void)
923 u
= vtop
->type
.t
& VT_UNSIGNED
;
926 vtop
[0].r
= vtop
[-1].r2
;
927 vtop
[0].r2
= VT_CONST
;
928 vtop
[-1].r2
= VT_CONST
;
929 vtop
[0].type
.t
= VT_INT
| u
;
930 vtop
[-1].type
.t
= VT_INT
| u
;
933 #ifdef TCC_TARGET_ARM
934 /* expand long long on stack */
935 ST_FUNC
void lexpand_nr(void)
939 u
= vtop
->type
.t
& VT_UNSIGNED
;
942 vtop
->type
.t
= VT_INT
| u
;
943 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
945 vtop
[-1].c
.ui
= vtop
->c
.ull
;
946 vtop
->c
.ui
= vtop
->c
.ull
>> 32;
948 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
950 vtop
->r
= vtop
[-1].r
;
951 } else if (v
> VT_CONST
) {
955 vtop
->r
= vtop
[-1].r2
;
956 vtop
[-1].r2
= VT_CONST
;
957 vtop
[-1].type
.t
= VT_INT
| u
;
961 /* build a long long from two ints */
962 static void lbuild(int t
)
965 vtop
[-1].r2
= vtop
[0].r
;
970 /* rotate n first stack elements to the bottom
971 I1 ... In -> I2 ... In I1 [top is right]
973 ST_FUNC
void vrotb(int n
)
984 /* rotate the n elements before entry e towards the top
985 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
987 ST_FUNC
void vrote(SValue
*e
, int n
)
993 for(i
= 0;i
< n
- 1; i
++)
998 /* rotate n first stack elements to the top
999 I1 ... In -> In I1 ... I(n-1) [top is right]
1001 ST_FUNC
void vrott(int n
)
1006 /* pop stack value */
1007 ST_FUNC
void vpop(void)
1010 v
= vtop
->r
& VT_VALMASK
;
1011 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1012 /* for x86, we need to pop the FP stack */
1013 if (v
== TREG_ST0
&& !nocode_wanted
) {
1014 o(0xd8dd); /* fstp %st(0) */
1017 if (v
== VT_JMP
|| v
== VT_JMPI
) {
1018 /* need to put correct jump if && or || without test */
1024 /* convert stack entry to register and duplicate its value in another
1026 static void gv_dup(void)
1032 if ((t
& VT_BTYPE
) == VT_LLONG
) {
1039 /* stack: H L L1 H1 */
1047 /* duplicate value */
1052 #ifdef TCC_TARGET_X86_64
1053 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1063 load(r1
, &sv
); /* move r to r1 */
1065 /* duplicates value */
1071 #ifndef TCC_TARGET_X86_64
1072 /* generate CPU independent (unsigned) long long operations */
1073 static void gen_opl(int op
)
1075 int t
, a
, b
, op1
, c
, i
;
1077 unsigned short reg_iret
= REG_IRET
;
1078 unsigned short reg_lret
= REG_LRET
;
1084 func
= TOK___divdi3
;
1087 func
= TOK___udivdi3
;
1090 func
= TOK___moddi3
;
1093 func
= TOK___umoddi3
;
1100 /* call generic long long function */
1101 vpush_global_sym(&func_old_type
, func
);
1106 vtop
->r2
= reg_lret
;
1119 /* stack: L1 H1 L2 H2 */
1124 vtop
[-2] = vtop
[-3];
1127 /* stack: H1 H2 L1 L2 */
1133 /* stack: H1 H2 L1 L2 ML MH */
1136 /* stack: ML MH H1 H2 L1 L2 */
1140 /* stack: ML MH H1 L2 H2 L1 */
1145 /* stack: ML MH M1 M2 */
1148 } else if (op
== '+' || op
== '-') {
1149 /* XXX: add non carry method too (for MIPS or alpha) */
1155 /* stack: H1 H2 (L1 op L2) */
1158 gen_op(op1
+ 1); /* TOK_xxxC2 */
1161 /* stack: H1 H2 (L1 op L2) */
1164 /* stack: (L1 op L2) H1 H2 */
1166 /* stack: (L1 op L2) (H1 op H2) */
1174 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1175 t
= vtop
[-1].type
.t
;
1179 /* stack: L H shift */
1181 /* constant: simpler */
1182 /* NOTE: all comments are for SHL. the other cases are
1183 done by swaping words */
1194 if (op
!= TOK_SAR
) {
1227 /* XXX: should provide a faster fallback on x86 ? */
1230 func
= TOK___ashrdi3
;
1233 func
= TOK___lshrdi3
;
1236 func
= TOK___ashldi3
;
1242 /* compare operations */
1248 /* stack: L1 H1 L2 H2 */
1250 vtop
[-1] = vtop
[-2];
1252 /* stack: L1 L2 H1 H2 */
1255 /* when values are equal, we need to compare low words. since
1256 the jump is inverted, we invert the test too. */
1259 else if (op1
== TOK_GT
)
1261 else if (op1
== TOK_ULT
)
1263 else if (op1
== TOK_UGT
)
1268 if (op1
!= TOK_NE
) {
1272 /* generate non equal test */
1273 /* XXX: NOT PORTABLE yet */
1277 #if defined(TCC_TARGET_I386)
1278 b
= psym(0x850f, 0);
1279 #elif defined(TCC_TARGET_ARM)
1281 o(0x1A000000 | encbranch(ind
, 0, 1));
1282 #elif defined(TCC_TARGET_C67)
1283 tcc_error("not implemented");
1285 #error not supported
1289 /* compare low. Always unsigned */
1293 else if (op1
== TOK_LE
)
1295 else if (op1
== TOK_GT
)
1297 else if (op1
== TOK_GE
)
1308 /* handle integer constant optimizations and various machine
1310 static void gen_opic(int op
)
1312 int c1
, c2
, t1
, t2
, n
;
1315 typedef unsigned long long U
;
1319 t1
= v1
->type
.t
& VT_BTYPE
;
1320 t2
= v2
->type
.t
& VT_BTYPE
;
1324 else if (v1
->type
.t
& VT_UNSIGNED
)
1331 else if (v2
->type
.t
& VT_UNSIGNED
)
1336 /* currently, we cannot do computations with forward symbols */
1337 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1338 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1341 case '+': l1
+= l2
; break;
1342 case '-': l1
-= l2
; break;
1343 case '&': l1
&= l2
; break;
1344 case '^': l1
^= l2
; break;
1345 case '|': l1
|= l2
; break;
1346 case '*': l1
*= l2
; break;
1353 /* if division by zero, generate explicit division */
1356 tcc_error("division by zero in constant");
1360 default: l1
/= l2
; break;
1361 case '%': l1
%= l2
; break;
1362 case TOK_UDIV
: l1
= (U
)l1
/ l2
; break;
1363 case TOK_UMOD
: l1
= (U
)l1
% l2
; break;
1366 case TOK_SHL
: l1
<<= l2
; break;
1367 case TOK_SHR
: l1
= (U
)l1
>> l2
; break;
1368 case TOK_SAR
: l1
>>= l2
; break;
1370 case TOK_ULT
: l1
= (U
)l1
< (U
)l2
; break;
1371 case TOK_UGE
: l1
= (U
)l1
>= (U
)l2
; break;
1372 case TOK_EQ
: l1
= l1
== l2
; break;
1373 case TOK_NE
: l1
= l1
!= l2
; break;
1374 case TOK_ULE
: l1
= (U
)l1
<= (U
)l2
; break;
1375 case TOK_UGT
: l1
= (U
)l1
> (U
)l2
; break;
1376 case TOK_LT
: l1
= l1
< l2
; break;
1377 case TOK_GE
: l1
= l1
>= l2
; break;
1378 case TOK_LE
: l1
= l1
<= l2
; break;
1379 case TOK_GT
: l1
= l1
> l2
; break;
1381 case TOK_LAND
: l1
= l1
&& l2
; break;
1382 case TOK_LOR
: l1
= l1
|| l2
; break;
1389 /* if commutative ops, put c2 as constant */
1390 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1391 op
== '|' || op
== '*')) {
1393 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1394 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1396 /* Filter out NOP operations like x*1, x-0, x&-1... */
1397 if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1400 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1401 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1407 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1408 /* try to use shifts instead of muls or divs */
1409 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1418 else if (op
== TOK_PDIV
)
1424 } else if (c2
&& (op
== '+' || op
== '-') &&
1425 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
1426 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1427 /* symbol + constant case */
1434 if (!nocode_wanted
) {
1435 /* call low level op generator */
1436 if (t1
== VT_LLONG
|| t2
== VT_LLONG
)
1447 /* generate a floating point operation with constant propagation */
1448 static void gen_opif(int op
)
1456 /* currently, we cannot do computations with forward symbols */
1457 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1458 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1460 if (v1
->type
.t
== VT_FLOAT
) {
1463 } else if (v1
->type
.t
== VT_DOUBLE
) {
1471 /* NOTE: we only do constant propagation if finite number (not
1472 NaN or infinity) (ANSI spec) */
1473 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1477 case '+': f1
+= f2
; break;
1478 case '-': f1
-= f2
; break;
1479 case '*': f1
*= f2
; break;
1483 tcc_error("division by zero in constant");
1488 /* XXX: also handles tests ? */
1492 /* XXX: overflow test ? */
1493 if (v1
->type
.t
== VT_FLOAT
) {
1495 } else if (v1
->type
.t
== VT_DOUBLE
) {
1503 if (!nocode_wanted
) {
1511 static int pointed_size(CType
*type
)
1514 return type_size(pointed_type(type
), &align
);
1517 static void vla_runtime_pointed_size(CType
*type
)
1520 vla_runtime_type_size(pointed_type(type
), &align
);
1523 static inline int is_null_pointer(SValue
*p
)
1525 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1527 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& p
->c
.i
== 0) ||
1528 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.ll
== 0) ||
1529 ((p
->type
.t
& VT_BTYPE
) == VT_PTR
&& p
->c
.ptr
== 0);
1532 static inline int is_integer_btype(int bt
)
1534 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
1535 bt
== VT_INT
|| bt
== VT_LLONG
);
1538 /* check types for comparison or substraction of pointers */
1539 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
1541 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
1544 /* null pointers are accepted for all comparisons as gcc */
1545 if (is_null_pointer(p1
) || is_null_pointer(p2
))
1549 bt1
= type1
->t
& VT_BTYPE
;
1550 bt2
= type2
->t
& VT_BTYPE
;
1551 /* accept comparison between pointer and integer with a warning */
1552 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
1553 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
1554 tcc_warning("comparison between pointer and integer");
1558 /* both must be pointers or implicit function pointers */
1559 if (bt1
== VT_PTR
) {
1560 type1
= pointed_type(type1
);
1561 } else if (bt1
!= VT_FUNC
)
1562 goto invalid_operands
;
1564 if (bt2
== VT_PTR
) {
1565 type2
= pointed_type(type2
);
1566 } else if (bt2
!= VT_FUNC
) {
1568 tcc_error("invalid operands to binary %s", get_tok_str(op
, NULL
));
1570 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1571 (type2
->t
& VT_BTYPE
) == VT_VOID
)
1575 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1576 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1577 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1578 /* gcc-like error if '-' is used */
1580 goto invalid_operands
;
1582 tcc_warning("comparison of distinct pointer types lacks a cast");
1586 /* generic gen_op: handles types problems */
1587 ST_FUNC
void gen_op(int op
)
1589 int u
, t1
, t2
, bt1
, bt2
, t
;
1592 t1
= vtop
[-1].type
.t
;
1593 t2
= vtop
[0].type
.t
;
1594 bt1
= t1
& VT_BTYPE
;
1595 bt2
= t2
& VT_BTYPE
;
1597 if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
1598 /* at least one operand is a pointer */
1599 /* relationnal op: must be both pointers */
1600 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
1601 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1602 /* pointers are handled are unsigned */
1603 #ifdef TCC_TARGET_X86_64
1604 t
= VT_LLONG
| VT_UNSIGNED
;
1606 t
= VT_INT
| VT_UNSIGNED
;
1610 /* if both pointers, then it must be the '-' op */
1611 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
1613 tcc_error("cannot use pointers here");
1614 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1615 /* XXX: check that types are compatible */
1616 if (vtop
[-1].type
.t
& VT_VLA
) {
1617 vla_runtime_pointed_size(&vtop
[-1].type
);
1619 vpushi(pointed_size(&vtop
[-1].type
));
1623 /* set to integer type */
1624 #ifdef TCC_TARGET_X86_64
1625 vtop
->type
.t
= VT_LLONG
;
1627 vtop
->type
.t
= VT_INT
;
1632 /* exactly one pointer : must be '+' or '-'. */
1633 if (op
!= '-' && op
!= '+')
1634 tcc_error("cannot use pointers here");
1635 /* Put pointer as first operand */
1636 if (bt2
== VT_PTR
) {
1640 type1
= vtop
[-1].type
;
1641 type1
.t
&= ~VT_ARRAY
;
1642 if (vtop
[-1].type
.t
& VT_VLA
)
1643 vla_runtime_pointed_size(&vtop
[-1].type
);
1645 u
= pointed_size(&vtop
[-1].type
);
1647 tcc_error("unknown array element size");
1648 #ifdef TCC_TARGET_X86_64
1651 /* XXX: cast to int ? (long long case) */
1656 #ifdef CONFIG_TCC_BCHECK
1657 /* if evaluating constant expression, no code should be
1658 generated, so no bound check */
1659 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
1660 /* if bounded pointers, we generate a special code to
1667 gen_bounded_ptr_add();
1673 /* put again type if gen_opic() swaped operands */
1676 } else if (is_float(bt1
) || is_float(bt2
)) {
1677 /* compute bigger type and do implicit casts */
1678 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
1680 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
1685 /* floats can only be used for a few operations */
1686 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
1687 (op
< TOK_ULT
|| op
> TOK_GT
))
1688 tcc_error("invalid operands for binary operation");
1690 } else if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
) {
1691 t
= bt1
== VT_LLONG
? VT_LLONG
: VT_INT
;
1692 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (t
| VT_UNSIGNED
))
1695 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
1696 /* cast to biggest op */
1698 /* convert to unsigned if it does not fit in a long long */
1699 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
1700 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
1703 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
1704 tcc_error("comparison of struct");
1706 /* integer operations */
1708 /* convert to unsigned if it does not fit in an integer */
1709 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
1710 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
1713 /* XXX: currently, some unsigned operations are explicit, so
1714 we modify them here */
1715 if (t
& VT_UNSIGNED
) {
1722 else if (op
== TOK_LT
)
1724 else if (op
== TOK_GT
)
1726 else if (op
== TOK_LE
)
1728 else if (op
== TOK_GE
)
1735 /* special case for shifts and long long: we keep the shift as
1737 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
1744 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
1745 /* relationnal op: the result is an int */
1746 vtop
->type
.t
= VT_INT
;
1753 #ifndef TCC_TARGET_ARM
1754 /* generic itof for unsigned long long case */
1755 static void gen_cvt_itof1(int t
)
1757 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
1758 (VT_LLONG
| VT_UNSIGNED
)) {
1761 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
1762 #if LDOUBLE_SIZE != 8
1763 else if (t
== VT_LDOUBLE
)
1764 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
1767 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
1771 vtop
->r
= reg_fret(t
);
1778 /* generic ftoi for unsigned long long case */
1779 static void gen_cvt_ftoi1(int t
)
1783 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
1784 /* not handled natively */
1785 st
= vtop
->type
.t
& VT_BTYPE
;
1787 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
1788 #if LDOUBLE_SIZE != 8
1789 else if (st
== VT_LDOUBLE
)
1790 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
1793 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
1798 vtop
->r2
= REG_LRET
;
1804 /* force char or short cast */
1805 static void force_charshort_cast(int t
)
1809 /* XXX: add optimization if lvalue : just change type and offset */
1814 if (t
& VT_UNSIGNED
) {
1815 vpushi((1 << bits
) - 1);
1821 /* result must be signed or the SAR is converted to an SHL
1822 This was not the case when "t" was a signed short
1823 and the last value on the stack was an unsigned int */
1824 vtop
->type
.t
&= ~VT_UNSIGNED
;
1830 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1831 static void gen_cast(CType
*type
)
1833 int sbt
, dbt
, sf
, df
, c
, p
;
1835 /* special delayed cast for char/short */
1836 /* XXX: in some cases (multiple cascaded casts), it may still
1838 if (vtop
->r
& VT_MUSTCAST
) {
1839 vtop
->r
&= ~VT_MUSTCAST
;
1840 force_charshort_cast(vtop
->type
.t
);
1843 /* bitfields first get cast to ints */
1844 if (vtop
->type
.t
& VT_BITFIELD
) {
1848 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
1849 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
1854 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1855 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
1857 /* constant case: we can do it now */
1858 /* XXX: in ISOC, cannot do it if error in convert */
1859 if (sbt
== VT_FLOAT
)
1860 vtop
->c
.ld
= vtop
->c
.f
;
1861 else if (sbt
== VT_DOUBLE
)
1862 vtop
->c
.ld
= vtop
->c
.d
;
1865 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
1866 if (sbt
& VT_UNSIGNED
)
1867 vtop
->c
.ld
= vtop
->c
.ull
;
1869 vtop
->c
.ld
= vtop
->c
.ll
;
1871 if (sbt
& VT_UNSIGNED
)
1872 vtop
->c
.ld
= vtop
->c
.ui
;
1874 vtop
->c
.ld
= vtop
->c
.i
;
1877 if (dbt
== VT_FLOAT
)
1878 vtop
->c
.f
= (float)vtop
->c
.ld
;
1879 else if (dbt
== VT_DOUBLE
)
1880 vtop
->c
.d
= (double)vtop
->c
.ld
;
1881 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
1882 vtop
->c
.ull
= (unsigned long long)vtop
->c
.ld
;
1883 } else if (sf
&& dbt
== VT_BOOL
) {
1884 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
1887 vtop
->c
.ll
= (long long)vtop
->c
.ld
;
1888 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
1889 vtop
->c
.ll
= vtop
->c
.ull
;
1890 else if (sbt
& VT_UNSIGNED
)
1891 vtop
->c
.ll
= vtop
->c
.ui
;
1892 #ifdef TCC_TARGET_X86_64
1893 else if (sbt
== VT_PTR
)
1896 else if (sbt
!= VT_LLONG
)
1897 vtop
->c
.ll
= vtop
->c
.i
;
1899 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
1900 vtop
->c
.ull
= vtop
->c
.ll
;
1901 else if (dbt
== VT_BOOL
)
1902 vtop
->c
.i
= (vtop
->c
.ll
!= 0);
1903 else if (dbt
!= VT_LLONG
) {
1905 if ((dbt
& VT_BTYPE
) == VT_BYTE
)
1907 else if ((dbt
& VT_BTYPE
) == VT_SHORT
)
1910 if(dbt
& VT_UNSIGNED
)
1911 vtop
->c
.ui
= ((unsigned int)vtop
->c
.ll
<< s
) >> s
;
1913 vtop
->c
.i
= ((int)vtop
->c
.ll
<< s
) >> s
;
1916 } else if (p
&& dbt
== VT_BOOL
) {
1919 } else if (!nocode_wanted
) {
1920 /* non constant case: generate code */
1922 /* convert from fp to fp */
1925 /* convert int to fp */
1928 /* convert fp to int */
1929 if (dbt
== VT_BOOL
) {
1933 /* we handle char/short/etc... with generic code */
1934 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
1935 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
1939 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
1940 /* additional cast for char/short... */
1945 #ifndef TCC_TARGET_X86_64
1946 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
1947 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
1948 /* scalar to long long */
1949 /* machine independent conversion */
1951 /* generate high word */
1952 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
1956 if (sbt
== VT_PTR
) {
1957 /* cast from pointer to int before we apply
1958 shift operation, which pointers don't support*/
1959 gen_cast(&int_type
);
1965 /* patch second register */
1966 vtop
[-1].r2
= vtop
->r
;
1970 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
1971 (dbt
& VT_BTYPE
) == VT_PTR
||
1972 (dbt
& VT_BTYPE
) == VT_FUNC
) {
1973 if ((sbt
& VT_BTYPE
) != VT_LLONG
&&
1974 (sbt
& VT_BTYPE
) != VT_PTR
&&
1975 (sbt
& VT_BTYPE
) != VT_FUNC
) {
1976 /* need to convert from 32bit to 64bit */
1978 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
1979 /* x86_64 specific: movslq */
1981 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
1985 } else if (dbt
== VT_BOOL
) {
1986 /* scalar to bool */
1989 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
1990 (dbt
& VT_BTYPE
) == VT_SHORT
) {
1991 if (sbt
== VT_PTR
) {
1992 vtop
->type
.t
= VT_INT
;
1993 tcc_warning("nonportable conversion from pointer to char/short");
1995 force_charshort_cast(dbt
);
1996 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
1998 if (sbt
== VT_LLONG
) {
1999 /* from long long: just take low order word */
2003 /* if lvalue and single word type, nothing to do because
2004 the lvalue already contains the real type size (see
2005 VT_LVAL_xxx constants) */
2008 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
2009 /* if we are casting between pointer types,
2010 we must update the VT_LVAL_xxx size */
2011 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
2012 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
2017 /* return type size as known at compile time. Put alignment at 'a' */
2018 ST_FUNC
int type_size(CType
*type
, int *a
)
2023 bt
= type
->t
& VT_BTYPE
;
2024 if (bt
== VT_STRUCT
) {
2029 } else if (bt
== VT_PTR
) {
2030 if (type
->t
& VT_ARRAY
) {
2034 ts
= type_size(&s
->type
, a
);
2036 if (ts
< 0 && s
->c
< 0)
2044 } else if (bt
== VT_LDOUBLE
) {
2046 return LDOUBLE_SIZE
;
2047 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
2048 #ifdef TCC_TARGET_I386
2049 #ifdef TCC_TARGET_PE
2054 #elif defined(TCC_TARGET_ARM)
2064 } else if (bt
== VT_INT
|| bt
== VT_ENUM
|| bt
== VT_FLOAT
) {
2067 } else if (bt
== VT_SHORT
) {
2071 /* char, void, function, _Bool */
2077 /* push type size as known at runtime time on top of value stack. Put
2079 ST_FUNC
void vla_runtime_type_size(CType
*type
, int *a
)
2081 if (type
->t
& VT_VLA
) {
2082 vset(&int_type
, VT_LOCAL
|VT_LVAL
, type
->ref
->c
);
2084 vpushi(type_size(type
, a
));
2088 /* return the pointed type of t */
2089 static inline CType
*pointed_type(CType
*type
)
2091 return &type
->ref
->type
;
2094 /* modify type so that its it is a pointer to type. */
2095 ST_FUNC
void mk_pointer(CType
*type
)
2098 s
= sym_push(SYM_FIELD
, type
, 0, -1);
2099 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
2103 /* compare function types. OLD functions match any new functions */
2104 static int is_compatible_func(CType
*type1
, CType
*type2
)
2110 if (!is_compatible_types(&s1
->type
, &s2
->type
))
2112 /* check func_call */
2113 if (FUNC_CALL(s1
->r
) != FUNC_CALL(s2
->r
))
2115 /* XXX: not complete */
2116 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
2120 while (s1
!= NULL
) {
2123 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
2133 /* return true if type1 and type2 are the same. If unqualified is
2134 true, qualifiers on the types are ignored.
2136 - enums are not checked as gcc __builtin_types_compatible_p ()
2138 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2142 t1
= type1
->t
& VT_TYPE
;
2143 t2
= type2
->t
& VT_TYPE
;
2145 /* strip qualifiers before comparing */
2146 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2147 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2149 /* XXX: bitfields ? */
2152 /* test more complicated cases */
2153 bt1
= t1
& VT_BTYPE
;
2154 if (bt1
== VT_PTR
) {
2155 type1
= pointed_type(type1
);
2156 type2
= pointed_type(type2
);
2157 return is_compatible_types(type1
, type2
);
2158 } else if (bt1
== VT_STRUCT
) {
2159 return (type1
->ref
== type2
->ref
);
2160 } else if (bt1
== VT_FUNC
) {
2161 return is_compatible_func(type1
, type2
);
2167 /* return true if type1 and type2 are exactly the same (including
2170 static int is_compatible_types(CType
*type1
, CType
*type2
)
2172 return compare_types(type1
,type2
,0);
2175 /* return true if type1 and type2 are the same (ignoring qualifiers).
2177 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
2179 return compare_types(type1
,type2
,1);
2182 /* print a type. If 'varstr' is not NULL, then the variable is also
2183 printed in the type */
2185 /* XXX: add array and function pointers */
2186 static void type_to_str(char *buf
, int buf_size
,
2187 CType
*type
, const char *varstr
)
2194 t
= type
->t
& VT_TYPE
;
2197 if (t
& VT_CONSTANT
)
2198 pstrcat(buf
, buf_size
, "const ");
2199 if (t
& VT_VOLATILE
)
2200 pstrcat(buf
, buf_size
, "volatile ");
2201 if (t
& VT_UNSIGNED
)
2202 pstrcat(buf
, buf_size
, "unsigned ");
2232 tstr
= "long double";
2234 pstrcat(buf
, buf_size
, tstr
);
2238 if (bt
== VT_STRUCT
)
2242 pstrcat(buf
, buf_size
, tstr
);
2243 v
= type
->ref
->v
& ~SYM_STRUCT
;
2244 if (v
>= SYM_FIRST_ANOM
)
2245 pstrcat(buf
, buf_size
, "<anonymous>");
2247 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
2251 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
2252 pstrcat(buf
, buf_size
, "(");
2254 while (sa
!= NULL
) {
2255 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
2256 pstrcat(buf
, buf_size
, buf1
);
2259 pstrcat(buf
, buf_size
, ", ");
2261 pstrcat(buf
, buf_size
, ")");
2265 pstrcpy(buf1
, sizeof(buf1
), "*");
2267 pstrcat(buf1
, sizeof(buf1
), varstr
);
2268 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2272 pstrcat(buf
, buf_size
, " ");
2273 pstrcat(buf
, buf_size
, varstr
);
2278 /* verify type compatibility to store vtop in 'dt' type, and generate
2280 static void gen_assign_cast(CType
*dt
)
2282 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
2283 char buf1
[256], buf2
[256];
2286 st
= &vtop
->type
; /* source type */
2287 dbt
= dt
->t
& VT_BTYPE
;
2288 sbt
= st
->t
& VT_BTYPE
;
2290 tcc_error("Cannot assign void value");
2291 if (dt
->t
& VT_CONSTANT
)
2292 tcc_warning("assignment of read-only location");
2295 /* special cases for pointers */
2296 /* '0' can also be a pointer */
2297 if (is_null_pointer(vtop
))
2299 /* accept implicit pointer to integer cast with warning */
2300 if (is_integer_btype(sbt
)) {
2301 tcc_warning("assignment makes pointer from integer without a cast");
2304 type1
= pointed_type(dt
);
2305 /* a function is implicitely a function pointer */
2306 if (sbt
== VT_FUNC
) {
2307 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
2308 !is_compatible_types(pointed_type(dt
), st
))
2309 tcc_warning("assignment from incompatible pointer type");
2314 type2
= pointed_type(st
);
2315 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2316 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
2317 /* void * can match anything */
2319 /* exact type match, except for unsigned */
2322 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2323 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2324 if (!is_compatible_types(&tmp_type1
, &tmp_type2
))
2325 tcc_warning("assignment from incompatible pointer type");
2327 /* check const and volatile */
2328 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
2329 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
2330 tcc_warning("assignment discards qualifiers from pointer target type");
2336 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
2337 tcc_warning("assignment makes integer from pointer without a cast");
2339 /* XXX: more tests */
2344 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2345 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2346 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2348 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
2349 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
2350 tcc_error("cannot cast '%s' to '%s'", buf1
, buf2
);
2358 /* store vtop in lvalue pushed on stack */
2359 ST_FUNC
void vstore(void)
2361 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
2363 ft
= vtop
[-1].type
.t
;
2364 sbt
= vtop
->type
.t
& VT_BTYPE
;
2365 dbt
= ft
& VT_BTYPE
;
2366 if ((((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2367 (sbt
== VT_INT
&& dbt
== VT_SHORT
))
2368 && !(vtop
->type
.t
& VT_BITFIELD
)) {
2369 /* optimize char/short casts */
2370 delayed_cast
= VT_MUSTCAST
;
2371 vtop
->type
.t
= ft
& (VT_TYPE
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
)));
2372 /* XXX: factorize */
2373 if (ft
& VT_CONSTANT
)
2374 tcc_warning("assignment of read-only location");
2377 if (!(ft
& VT_BITFIELD
))
2378 gen_assign_cast(&vtop
[-1].type
);
2381 if (sbt
== VT_STRUCT
) {
2382 /* if structure, only generate pointer */
2383 /* structure assignment : generate memcpy */
2384 /* XXX: optimize if small size */
2385 if (!nocode_wanted
) {
2386 size
= type_size(&vtop
->type
, &align
);
2390 vtop
->type
.t
= VT_PTR
;
2393 /* address of memcpy() */
2396 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2397 else if(!(align
& 3))
2398 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2401 vpush_global_sym(&func_old_type
, TOK_memcpy
);
2406 vtop
->type
.t
= VT_PTR
;
2415 /* leave source on stack */
2416 } else if (ft
& VT_BITFIELD
) {
2417 /* bitfield store handling */
2418 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2419 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2420 /* remove bit field info to avoid loops */
2421 vtop
[-1].type
.t
= ft
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
2423 /* duplicate source into other register */
2428 if((ft
& VT_BTYPE
) == VT_BOOL
) {
2429 gen_cast(&vtop
[-1].type
);
2430 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2433 /* duplicate destination */
2435 vtop
[-1] = vtop
[-2];
2437 /* mask and shift source */
2438 if((ft
& VT_BTYPE
) != VT_BOOL
) {
2439 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2440 vpushll((1ULL << bit_size
) - 1ULL);
2442 vpushi((1 << bit_size
) - 1);
2448 /* load destination, mask and or with source */
2450 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2451 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2453 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2460 /* pop off shifted source from "duplicate source..." above */
2464 #ifdef CONFIG_TCC_BCHECK
2465 /* bound check case */
2466 if (vtop
[-1].r
& VT_MUSTBOUND
) {
2472 if (!nocode_wanted
) {
2476 #ifdef TCC_TARGET_X86_64
2477 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
2482 r
= gv(rc
); /* generate value */
2483 /* if lvalue was saved on stack, must read it */
2484 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
2486 t
= get_reg(RC_INT
);
2487 #ifdef TCC_TARGET_X86_64
2492 sv
.r
= VT_LOCAL
| VT_LVAL
;
2493 sv
.c
.ul
= vtop
[-1].c
.ul
;
2495 vtop
[-1].r
= t
| VT_LVAL
;
2498 #ifndef TCC_TARGET_X86_64
2499 /* two word case handling : store second register at word + 4 */
2500 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
2502 /* convert to int to increment easily */
2503 vtop
->type
.t
= VT_INT
;
2509 /* XXX: it works because r2 is spilled last ! */
2510 store(vtop
->r2
, vtop
- 1);
2515 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
2516 vtop
->r
|= delayed_cast
;
2520 /* post defines POST/PRE add. c is the token ++ or -- */
2521 ST_FUNC
void inc(int post
, int c
)
2524 vdup(); /* save lvalue */
2526 gv_dup(); /* duplicate value */
2531 vpushi(c
- TOK_MID
);
2533 vstore(); /* store value */
2535 vpop(); /* if post op, return saved value */
2538 /* Parse GNUC __attribute__ extension. Currently, the following
2539 extensions are recognized:
2540 - aligned(n) : set data/function alignment.
2541 - packed : force data alignment to 1
2542 - section(x) : generate data/code in this section.
2543 - unused : currently ignored, but may be used someday.
2544 - regparm(n) : pass function parameters in registers (i386 only)
2546 static void parse_attribute(AttributeDef
*ad
)
2550 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
2554 while (tok
!= ')') {
2555 if (tok
< TOK_IDENT
)
2556 expect("attribute name");
2564 expect("section name");
2565 ad
->section
= find_section(tcc_state
, (char *)tokc
.cstr
->data
);
2573 expect("alias(\"target\")");
2574 ad
->alias_target
= /* save string as token, for later */
2575 tok_alloc((char*)tokc
.cstr
->data
, tokc
.cstr
->size
-1)->tok
;
2584 if (n
<= 0 || (n
& (n
- 1)) != 0)
2585 tcc_error("alignment must be a positive power of two");
2602 /* currently, no need to handle it because tcc does not
2603 track unused objects */
2607 /* currently, no need to handle it because tcc does not
2608 track unused objects */
2613 ad
->func_call
= FUNC_CDECL
;
2618 ad
->func_call
= FUNC_STDCALL
;
2620 #ifdef TCC_TARGET_I386
2630 ad
->func_call
= FUNC_FASTCALL1
+ n
- 1;
2636 ad
->func_call
= FUNC_FASTCALLW
;
2643 ad
->mode
= VT_LLONG
+ 1;
2646 ad
->mode
= VT_SHORT
+ 1;
2649 ad
->mode
= VT_INT
+ 1;
2652 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
2659 ad
->func_export
= 1;
2662 ad
->func_import
= 1;
2665 if (tcc_state
->warn_unsupported
)
2666 tcc_warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
2667 /* skip parameters */
2669 int parenthesis
= 0;
2673 else if (tok
== ')')
2676 } while (parenthesis
&& tok
!= -1);
2689 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2690 static void struct_decl(CType
*type
, int u
)
2692 int a
, v
, size
, align
, maxalign
, c
, offset
;
2693 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
, prevbt
;
2694 Sym
*s
, *ss
, *ass
, **ps
;
2698 a
= tok
; /* save decl type */
2703 /* struct already defined ? return it */
2705 expect("struct/union/enum name");
2709 tcc_error("invalid type");
2716 /* we put an undefined size for struct/union */
2717 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
2718 s
->r
= 0; /* default alignment is zero as gcc */
2719 /* put struct/union/enum name in type */
2727 tcc_error("struct/union/enum already defined");
2728 /* cannot be empty */
2730 /* non empty enums are not allowed */
2731 if (a
== TOK_ENUM
) {
2735 expect("identifier");
2741 /* enum symbols have static storage */
2742 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
2743 ss
->type
.t
|= VT_STATIC
;
2748 /* NOTE: we accept a trailing comma */
2759 while (tok
!= '}') {
2760 parse_btype(&btype
, &ad
);
2766 type_decl(&type1
, &ad
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
2767 if (v
== 0 && (type1
.t
& VT_BTYPE
) != VT_STRUCT
)
2768 expect("identifier");
2769 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
2770 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
2771 tcc_error("invalid type for '%s'",
2772 get_tok_str(v
, NULL
));
2776 bit_size
= expr_const();
2777 /* XXX: handle v = 0 case for messages */
2779 tcc_error("negative width in bit-field '%s'",
2780 get_tok_str(v
, NULL
));
2781 if (v
&& bit_size
== 0)
2782 tcc_error("zero width for bit-field '%s'",
2783 get_tok_str(v
, NULL
));
2785 size
= type_size(&type1
, &align
);
2787 if (align
< ad
.aligned
)
2789 } else if (ad
.packed
) {
2791 } else if (*tcc_state
->pack_stack_ptr
) {
2792 if (align
> *tcc_state
->pack_stack_ptr
)
2793 align
= *tcc_state
->pack_stack_ptr
;
2796 if (bit_size
>= 0) {
2797 bt
= type1
.t
& VT_BTYPE
;
2804 tcc_error("bitfields must have scalar type");
2806 if (bit_size
> bsize
) {
2807 tcc_error("width of '%s' exceeds its type",
2808 get_tok_str(v
, NULL
));
2809 } else if (bit_size
== bsize
) {
2810 /* no need for bit fields */
2812 } else if (bit_size
== 0) {
2813 /* XXX: what to do if only padding in a
2815 /* zero size: means to pad */
2818 /* we do not have enough room ?
2819 did the type change?
2821 if ((bit_pos
+ bit_size
) > bsize
||
2822 bt
!= prevbt
|| a
== TOK_UNION
)
2825 /* XXX: handle LSB first */
2826 type1
.t
|= VT_BITFIELD
|
2827 (bit_pos
<< VT_STRUCT_SHIFT
) |
2828 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
2829 bit_pos
+= bit_size
;
2835 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2836 /* add new memory data only if starting
2838 if (lbit_pos
== 0) {
2839 if (a
== TOK_STRUCT
) {
2840 c
= (c
+ align
- 1) & -align
;
2849 if (align
> maxalign
)
2853 printf("add field %s offset=%d",
2854 get_tok_str(v
, NULL
), offset
);
2855 if (type1
.t
& VT_BITFIELD
) {
2856 printf(" pos=%d size=%d",
2857 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
2858 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
2863 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2865 while ((ass
= ass
->next
) != NULL
) {
2866 ss
= sym_push(ass
->v
, &ass
->type
, 0, offset
+ ass
->c
);
2871 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
2875 if (tok
== ';' || tok
== TOK_EOF
)
2882 /* store size and alignment */
2883 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
2889 /* return 0 if no type declaration. otherwise, return the basic type
2892 static int parse_btype(CType
*type
, AttributeDef
*ad
)
2894 int t
, u
, type_found
, typespec_found
, typedef_found
;
2898 memset(ad
, 0, sizeof(AttributeDef
));
2906 /* currently, we really ignore extension */
2916 if ((t
& VT_BTYPE
) != 0)
2917 tcc_error("too many basic types");
2933 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
2934 #ifndef TCC_TARGET_PE
2935 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
2937 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
2938 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
2952 if ((t
& VT_BTYPE
) == VT_LONG
) {
2953 #ifdef TCC_TARGET_PE
2954 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
2956 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
2964 struct_decl(&type1
, VT_ENUM
);
2967 type
->ref
= type1
.ref
;
2971 struct_decl(&type1
, VT_STRUCT
);
2974 /* type modifiers */
3027 /* GNUC attribute */
3028 case TOK_ATTRIBUTE1
:
3029 case TOK_ATTRIBUTE2
:
3030 parse_attribute(ad
);
3033 t
= (t
& ~VT_BTYPE
) | u
;
3041 parse_expr_type(&type1
);
3042 /* remove all storage modifiers except typedef */
3043 type1
.t
&= ~(VT_STORAGE
&~VT_TYPEDEF
);
3046 if (typespec_found
|| typedef_found
)
3049 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
3052 t
|= (s
->type
.t
& ~VT_TYPEDEF
);
3053 type
->ref
= s
->type
.ref
;
3055 /* get attributes from typedef */
3056 if (0 == ad
->aligned
)
3057 ad
->aligned
= FUNC_ALIGN(s
->r
);
3058 if (0 == ad
->func_call
)
3059 ad
->func_call
= FUNC_CALL(s
->r
);
3060 ad
->packed
|= FUNC_PACKED(s
->r
);
3069 if ((t
& (VT_SIGNED
|VT_UNSIGNED
)) == (VT_SIGNED
|VT_UNSIGNED
))
3070 tcc_error("signed and unsigned modifier");
3071 if (tcc_state
->char_is_unsigned
) {
3072 if ((t
& (VT_SIGNED
|VT_UNSIGNED
|VT_BTYPE
)) == VT_BYTE
)
3077 /* long is never used as type */
3078 if ((t
& VT_BTYPE
) == VT_LONG
)
3079 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
3080 t
= (t
& ~VT_BTYPE
) | VT_INT
;
3082 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3088 /* convert a function parameter type (array to pointer and function to
3089 function pointer) */
3090 static inline void convert_parameter_type(CType
*pt
)
3092 /* remove const and volatile qualifiers (XXX: const could be used
3093 to indicate a const function parameter */
3094 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3095 /* array must be transformed to pointer according to ANSI C */
3097 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
3102 ST_FUNC
void parse_asm_str(CString
*astr
)
3105 /* read the string */
3107 expect("string constant");
3109 while (tok
== TOK_STR
) {
3110 /* XXX: add \0 handling too ? */
3111 cstr_cat(astr
, tokc
.cstr
->data
);
3114 cstr_ccat(astr
, '\0');
3117 /* Parse an asm label and return the label
3118 * Don't forget to free the CString in the caller! */
3119 static void asm_label_instr(CString
*astr
)
3122 parse_asm_str(astr
);
3125 printf("asm_alias: \"%s\"\n", (char *)astr
->data
);
3129 static void post_type(CType
*type
, AttributeDef
*ad
)
3131 int n
, l
, t1
, arg_size
, align
;
3132 Sym
**plast
, *s
, *first
;
3137 /* function declaration */
3145 /* read param name and compute offset */
3146 if (l
!= FUNC_OLD
) {
3147 if (!parse_btype(&pt
, &ad1
)) {
3149 tcc_error("invalid type");
3156 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
3158 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3159 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
3160 tcc_error("parameter declared as void");
3161 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
3166 expect("identifier");
3170 convert_parameter_type(&pt
);
3171 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
3177 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
3184 /* if no parameters, then old type prototype */
3188 /* NOTE: const is ignored in returned type as it has a special
3189 meaning in gcc / C++ */
3190 type
->t
&= ~VT_CONSTANT
;
3191 /* some ancient pre-K&R C allows a function to return an array
3192 and the array brackets to be put after the arguments, such
3193 that "int c()[]" means something like "int[] c()" */
3196 skip(']'); /* only handle simple "[]" */
3199 /* we push a anonymous symbol which will contain the function prototype */
3200 ad
->func_args
= arg_size
;
3201 s
= sym_push(SYM_FIELD
, type
, INT_ATTR(ad
), l
);
3205 } else if (tok
== '[') {
3206 /* array definition */
3208 if (tok
== TOK_RESTRICT1
)
3213 if (!local_stack
|| nocode_wanted
)
3214 vpushi(expr_const());
3216 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3219 tcc_error("invalid array size");
3221 if (!is_integer_btype(vtop
->type
.t
& VT_BTYPE
))
3222 tcc_error("size of variable length array should be an integer");
3227 /* parse next post type */
3228 post_type(type
, ad
);
3229 t1
|= type
->t
& VT_VLA
;
3232 loc
-= type_size(&int_type
, &align
);
3236 vla_runtime_type_size(type
, &align
);
3238 vset(&int_type
, VT_LOCAL
|VT_LVAL
, loc
);
3245 /* we push an anonymous symbol which will contain the array
3247 s
= sym_push(SYM_FIELD
, type
, 0, n
);
3248 type
->t
= (t1
? VT_VLA
: VT_ARRAY
) | VT_PTR
;
3253 /* Parse a type declaration (except basic type), and return the type
3254 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3255 expected. 'type' should contain the basic type. 'ad' is the
3256 attribute definition of the basic type. It can be modified by
3259 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
3262 CType type1
, *type2
;
3263 int qualifiers
, storage
, saved_nocode_wanted
;
3265 while (tok
== '*') {
3273 qualifiers
|= VT_CONSTANT
;
3278 qualifiers
|= VT_VOLATILE
;
3286 type
->t
|= qualifiers
;
3289 /* XXX: clarify attribute handling */
3290 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3291 parse_attribute(ad
);
3293 /* recursive type */
3294 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3295 type1
.t
= 0; /* XXX: same as int */
3298 /* XXX: this is not correct to modify 'ad' at this point, but
3299 the syntax is not clear */
3300 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3301 parse_attribute(ad
);
3302 type_decl(&type1
, ad
, v
, td
);
3305 /* type identifier */
3306 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
3310 if (!(td
& TYPE_ABSTRACT
))
3311 expect("identifier");
3315 storage
= type
->t
& VT_STORAGE
;
3316 type
->t
&= ~VT_STORAGE
;
3317 if (storage
& VT_STATIC
) {
3318 saved_nocode_wanted
= nocode_wanted
;
3321 post_type(type
, ad
);
3322 if (storage
& VT_STATIC
)
3323 nocode_wanted
= saved_nocode_wanted
;
3325 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3326 parse_attribute(ad
);
3330 /* append type at the end of type1 */
3343 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3344 ST_FUNC
int lvalue_type(int t
)
3349 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
3351 else if (bt
== VT_SHORT
)
3355 if (t
& VT_UNSIGNED
)
3356 r
|= VT_LVAL_UNSIGNED
;
3360 /* indirection with full error checking and bound check */
3361 ST_FUNC
void indir(void)
3363 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
3364 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
3368 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
3370 vtop
->type
= *pointed_type(&vtop
->type
);
3371 /* Arrays and functions are never lvalues */
3372 if (!(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_VLA
)
3373 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3374 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3375 /* if bound checking, the referenced pointer must be checked */
3376 #ifdef CONFIG_TCC_BCHECK
3377 if (tcc_state
->do_bounds_check
)
3378 vtop
->r
|= VT_MUSTBOUND
;
3383 /* pass a parameter to a function and do type checking and casting */
3384 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
3389 func_type
= func
->c
;
3390 if (func_type
== FUNC_OLD
||
3391 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
3392 /* default casting : only need to convert float to double */
3393 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
3397 } else if (arg
== NULL
) {
3398 tcc_error("too many arguments to function");
3401 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
3402 gen_assign_cast(&type
);
3406 /* parse an expression of the form '(type)' or '(expr)' and return its
3408 static void parse_expr_type(CType
*type
)
3414 if (parse_btype(type
, &ad
)) {
3415 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3422 static void parse_type(CType
*type
)
3427 if (!parse_btype(type
, &ad
)) {
3430 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3433 static void vpush_tokc(int t
)
3438 vsetc(&type
, VT_CONST
, &tokc
);
3441 ST_FUNC
void unary(void)
3443 int n
, t
, align
, size
, r
, sizeof_caller
;
3447 static int in_sizeof
= 0;
3449 sizeof_caller
= in_sizeof
;
3451 /* XXX: GCC 2.95.3 does not generate a table although it should be
3465 vpush_tokc(VT_INT
| VT_UNSIGNED
);
3469 vpush_tokc(VT_LLONG
);
3473 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
3477 vpush_tokc(VT_FLOAT
);
3481 vpush_tokc(VT_DOUBLE
);
3485 vpush_tokc(VT_LDOUBLE
);
3488 case TOK___FUNCTION__
:
3490 goto tok_identifier
;
3496 /* special function name identifier */
3497 len
= strlen(funcname
) + 1;
3498 /* generate char[len] type */
3503 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
3504 ptr
= section_ptr_add(data_section
, len
);
3505 memcpy(ptr
, funcname
, len
);
3510 #ifdef TCC_TARGET_PE
3511 t
= VT_SHORT
| VT_UNSIGNED
;
3517 /* string parsing */
3520 if (tcc_state
->warn_write_strings
)
3525 memset(&ad
, 0, sizeof(AttributeDef
));
3526 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, NULL
, 0);
3531 if (parse_btype(&type
, &ad
)) {
3532 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
3534 /* check ISOC99 compound literal */
3536 /* data is allocated locally by default */
3541 /* all except arrays are lvalues */
3542 if (!(type
.t
& VT_ARRAY
))
3543 r
|= lvalue_type(type
.t
);
3544 memset(&ad
, 0, sizeof(AttributeDef
));
3545 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, NULL
, 0);
3547 if (sizeof_caller
) {
3554 } else if (tok
== '{') {
3555 /* save all registers */
3557 /* statement expression : we do not accept break/continue
3558 inside as GCC does */
3559 block(NULL
, NULL
, NULL
, NULL
, 0, 1);
3574 /* functions names must be treated as function pointers,
3575 except for unary '&' and sizeof. Since we consider that
3576 functions are not lvalues, we only have to handle it
3577 there and in function calls. */
3578 /* arrays can also be used although they are not lvalues */
3579 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
3580 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
3582 mk_pointer(&vtop
->type
);
3588 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3590 boolean
.t
= VT_BOOL
;
3592 vtop
->c
.i
= !vtop
->c
.i
;
3593 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
3594 vtop
->c
.i
= vtop
->c
.i
^ 1;
3597 vseti(VT_JMP
, gtst(1, 0));
3608 /* in order to force cast, we add zero */
3610 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
3611 tcc_error("pointer not accepted for unary plus");
3621 unary_type(&type
); // Perform a in_sizeof = 0;
3622 size
= type_size(&type
, &align
);
3623 if (t
== TOK_SIZEOF
) {
3624 if (!(type
.t
& VT_VLA
)) {
3626 tcc_error("sizeof applied to an incomplete type");
3629 vla_runtime_type_size(&type
, &align
);
3634 vtop
->type
.t
|= VT_UNSIGNED
;
3637 case TOK_builtin_types_compatible_p
:
3646 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3647 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3648 vpushi(is_compatible_types(&type1
, &type2
));
3651 case TOK_builtin_constant_p
:
3653 int saved_nocode_wanted
, res
;
3656 saved_nocode_wanted
= nocode_wanted
;
3659 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
3661 nocode_wanted
= saved_nocode_wanted
;
3666 case TOK_builtin_frame_address
:
3671 if (tok
!= TOK_CINT
) {
3672 tcc_error("__builtin_frame_address only takes integers");
3675 tcc_error("TCC only supports __builtin_frame_address(0)");
3681 vset(&type
, VT_LOCAL
, 0);
3684 #ifdef TCC_TARGET_X86_64
3685 case TOK_builtin_va_arg_types
:
3687 /* This definition must be synced with stdarg.h */
3688 enum __va_arg_type
{
3689 __va_gen_reg
, __va_float_reg
, __va_stack
3697 bt
= type
.t
& VT_BTYPE
;
3698 if (bt
== VT_STRUCT
|| bt
== VT_LDOUBLE
) {
3700 } else if (bt
== VT_FLOAT
|| bt
== VT_DOUBLE
) {
3701 vpushi(__va_float_reg
);
3703 vpushi(__va_gen_reg
);
3723 goto tok_identifier
;
3725 /* allow to take the address of a label */
3726 if (tok
< TOK_UIDENT
)
3727 expect("label identifier");
3728 s
= label_find(tok
);
3730 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
3732 if (s
->r
== LABEL_DECLARED
)
3733 s
->r
= LABEL_FORWARD
;
3736 s
->type
.t
= VT_VOID
;
3737 mk_pointer(&s
->type
);
3738 s
->type
.t
|= VT_STATIC
;
3740 vset(&s
->type
, VT_CONST
| VT_SYM
, 0);
3745 // special qnan , snan and infinity values
3747 vpush64(VT_DOUBLE
, 0x7ff8000000000000ULL
);
3751 vpush64(VT_DOUBLE
, 0x7ff0000000000001ULL
);
3755 vpush64(VT_DOUBLE
, 0x7ff0000000000000ULL
);
3764 expect("identifier");
3768 tcc_error("'%s' undeclared", get_tok_str(t
, NULL
));
3769 /* for simple function calls, we tolerate undeclared
3770 external reference to int() function */
3771 if (tcc_state
->warn_implicit_function_declaration
)
3772 tcc_warning("implicit declaration of function '%s'",
3773 get_tok_str(t
, NULL
));
3774 s
= external_global_sym(t
, &func_old_type
, 0);
3776 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
3777 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
3778 /* if referencing an inline function, then we generate a
3779 symbol to it if not already done. It will have the
3780 effect to generate code for it at the end of the
3781 compilation unit. Inline function as always
3782 generated in the text section. */
3784 put_extern_sym(s
, text_section
, 0, 0);
3785 r
= VT_SYM
| VT_CONST
;
3789 vset(&s
->type
, r
, s
->c
);
3790 /* if forward reference, we must point to s */
3791 if (vtop
->r
& VT_SYM
) {
3798 /* post operations */
3800 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
3803 } else if (tok
== '.' || tok
== TOK_ARROW
) {
3806 if (tok
== TOK_ARROW
)
3808 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
3812 /* expect pointer on structure */
3813 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
3814 expect("struct or union");
3818 while ((s
= s
->next
) != NULL
) {
3823 tcc_error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, NULL
));
3824 /* add field offset to pointer */
3825 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
3828 /* change type to field type, and set to lvalue */
3829 vtop
->type
= s
->type
;
3830 vtop
->type
.t
|= qualifiers
;
3831 /* an array is never an lvalue */
3832 if (!(vtop
->type
.t
& VT_ARRAY
)) {
3833 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3834 #ifdef CONFIG_TCC_BCHECK
3835 /* if bound checking, the referenced pointer must be checked */
3836 if (tcc_state
->do_bounds_check
)
3837 vtop
->r
|= VT_MUSTBOUND
;
3841 } else if (tok
== '[') {
3847 } else if (tok
== '(') {
3853 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3854 /* pointer test (no array accepted) */
3855 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
3856 vtop
->type
= *pointed_type(&vtop
->type
);
3857 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
3861 expect("function pointer");
3864 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
3866 /* get return type */
3869 sa
= s
->next
; /* first parameter */
3872 /* compute first implicit argument if a structure is returned */
3873 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
3874 /* get some space for the returned structure */
3875 size
= type_size(&s
->type
, &align
);
3876 loc
= (loc
- size
) & -align
;
3878 ret
.r
= VT_LOCAL
| VT_LVAL
;
3879 /* pass it as 'int' to avoid structure arg passing
3881 vseti(VT_LOCAL
, loc
);
3886 /* return in register */
3887 if (is_float(ret
.type
.t
)) {
3888 ret
.r
= reg_fret(ret
.type
.t
);
3890 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
3899 gfunc_param_typed(s
, sa
);
3909 tcc_error("too few arguments to function");
3911 if (!nocode_wanted
) {
3912 gfunc_call(nb_args
);
3914 vtop
-= (nb_args
+ 1);
3917 vsetc(&ret
.type
, ret
.r
, &ret
.c
);
3925 ST_FUNC
void expr_prod(void)
3930 while (tok
== '*' || tok
== '/' || tok
== '%') {
3938 ST_FUNC
void expr_sum(void)
3943 while (tok
== '+' || tok
== '-') {
3951 static void expr_shift(void)
3956 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
3964 static void expr_cmp(void)
3969 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
3970 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
3978 static void expr_cmpeq(void)
3983 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
3991 static void expr_and(void)
3994 while (tok
== '&') {
4001 static void expr_xor(void)
4004 while (tok
== '^') {
4011 static void expr_or(void)
4014 while (tok
== '|') {
4021 /* XXX: fix this mess */
4022 static void expr_land_const(void)
4025 while (tok
== TOK_LAND
) {
4032 /* XXX: fix this mess */
4033 static void expr_lor_const(void)
4036 while (tok
== TOK_LOR
) {
4043 /* only used if non constant */
4044 static void expr_land(void)
4049 if (tok
== TOK_LAND
) {
4054 if (tok
!= TOK_LAND
) {
4064 static void expr_lor(void)
4069 if (tok
== TOK_LOR
) {
4074 if (tok
!= TOK_LOR
) {
4084 /* XXX: better constant handling */
4085 static void expr_cond(void)
4087 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
;
4089 CType type
, type1
, type2
;
4096 boolean
.t
= VT_BOOL
;
4102 if (tok
!= ':' || !gnu_ext
) {
4117 if (vtop
!= vstack
) {
4118 /* needed to avoid having different registers saved in
4120 if (is_float(vtop
->type
.t
)) {
4122 #ifdef TCC_TARGET_X86_64
4123 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4133 if (tok
== ':' && gnu_ext
) {
4141 sv
= *vtop
; /* save value to handle it later */
4142 vtop
--; /* no vpop so that FP stack is not flushed */
4150 bt1
= t1
& VT_BTYPE
;
4152 bt2
= t2
& VT_BTYPE
;
4153 /* cast operands to correct type according to ISOC rules */
4154 if (is_float(bt1
) || is_float(bt2
)) {
4155 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
4156 type
.t
= VT_LDOUBLE
;
4157 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
4162 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
4163 /* cast to biggest op */
4165 /* convert to unsigned if it does not fit in a long long */
4166 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
4167 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
4168 type
.t
|= VT_UNSIGNED
;
4169 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
4170 /* If one is a null ptr constant the result type
4172 if (is_null_pointer (vtop
))
4174 else if (is_null_pointer (&sv
))
4176 /* XXX: test pointer compatibility, C99 has more elaborate
4180 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
4181 /* XXX: test function pointer compatibility */
4182 type
= bt1
== VT_FUNC
? type1
: type2
;
4183 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
4184 /* XXX: test structure compatibility */
4185 type
= bt1
== VT_STRUCT
? type1
: type2
;
4186 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
4187 /* NOTE: as an extension, we accept void on only one side */
4190 /* integer operations */
4192 /* convert to unsigned if it does not fit in an integer */
4193 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
4194 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
4195 type
.t
|= VT_UNSIGNED
;
4198 /* now we convert second operand */
4200 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4203 if (is_float(type
.t
)) {
4205 #ifdef TCC_TARGET_X86_64
4206 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4210 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
4211 /* for long longs, we use fixed registers to avoid having
4212 to handle a complicated move */
4217 /* this is horrible, but we must also convert first
4221 /* put again first value and cast it */
4224 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4234 static void expr_eq(void)
4240 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
4241 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
4242 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
4257 ST_FUNC
void gexpr(void)
4268 /* parse an expression and return its type without any side effect. */
4269 static void expr_type(CType
*type
)
4271 int saved_nocode_wanted
;
4273 saved_nocode_wanted
= nocode_wanted
;
4278 nocode_wanted
= saved_nocode_wanted
;
4281 /* parse a unary expression and return its type without any side
4283 static void unary_type(CType
*type
)
4295 /* parse a constant expression and return value in vtop. */
4296 static void expr_const1(void)
4305 /* parse an integer constant and return its value. */
4306 ST_FUNC
int expr_const(void)
4310 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
4311 expect("constant expression");
4317 /* return the label token if current token is a label, otherwise
4319 static int is_label(void)
4323 /* fast test first */
4324 if (tok
< TOK_UIDENT
)
4326 /* no need to save tokc because tok is an identifier */
4333 unget_tok(last_tok
);
4338 static void label_or_decl(int l
)
4342 /* fast test first */
4343 if (tok
>= TOK_UIDENT
)
4345 /* no need to save tokc because tok is an identifier */
4349 unget_tok(last_tok
);
4352 unget_tok(last_tok
);
4357 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
4358 int case_reg
, int is_expr
)
4361 Sym
*s
, *frame_bottom
;
4363 /* generate line number info */
4364 if (tcc_state
->do_debug
&&
4365 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
4366 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
4368 last_line_num
= file
->line_num
;
4372 /* default return value is (void) */
4374 vtop
->type
.t
= VT_VOID
;
4377 if (tok
== TOK_IF
) {
4384 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4386 if (c
== TOK_ELSE
) {
4390 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4391 gsym(d
); /* patch else jmp */
4394 } else if (tok
== TOK_WHILE
) {
4402 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4406 } else if (tok
== '{') {
4410 /* record local declaration stack position */
4412 frame_bottom
= sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4413 frame_bottom
->next
= scope_stack_bottom
;
4414 scope_stack_bottom
= frame_bottom
;
4415 llabel
= local_label_stack
;
4416 /* handle local labels declarations */
4417 if (tok
== TOK_LABEL
) {
4420 if (tok
< TOK_UIDENT
)
4421 expect("label identifier");
4422 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
4432 while (tok
!= '}') {
4433 label_or_decl(VT_LOCAL
);
4437 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4440 /* pop locally defined labels */
4441 label_pop(&local_label_stack
, llabel
);
4443 /* XXX: this solution makes only valgrind happy...
4444 triggered by gcc.c-torture/execute/20000917-1.c */
4446 switch(vtop
->type
.t
& VT_BTYPE
) {
4451 for(p
=vtop
->type
.ref
;p
;p
=p
->prev
)
4453 tcc_error("unsupported expression type");
4456 /* pop locally defined symbols */
4457 scope_stack_bottom
= scope_stack_bottom
->next
;
4458 sym_pop(&local_stack
, s
);
4460 } else if (tok
== TOK_RETURN
) {
4464 gen_assign_cast(&func_vt
);
4465 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
4467 /* if returning structure, must copy it to implicit
4468 first pointer arg location */
4471 size
= type_size(&func_vt
,&align
);
4474 if((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) || (vtop
->c
.i
& 3))
4478 loc
= (loc
- size
) & -4;
4481 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
4484 vset(&int_type
, VT_LOCAL
| VT_LVAL
, addr
);
4486 vtop
->type
= int_type
;
4492 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
4495 /* copy structure value to pointer */
4500 } else if (is_float(func_vt
.t
)) {
4501 gv(rc_fret(func_vt
.t
));
4505 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
4508 rsym
= gjmp(rsym
); /* jmp */
4509 } else if (tok
== TOK_BREAK
) {
4512 tcc_error("cannot break");
4513 *bsym
= gjmp(*bsym
);
4516 } else if (tok
== TOK_CONTINUE
) {
4519 tcc_error("cannot continue");
4520 *csym
= gjmp(*csym
);
4523 } else if (tok
== TOK_FOR
) {
4528 frame_bottom
= sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4529 frame_bottom
->next
= scope_stack_bottom
;
4530 scope_stack_bottom
= frame_bottom
;
4532 /* c99 for-loop init decl? */
4533 if (!decl0(VT_LOCAL
, 1)) {
4534 /* no, regular for-loop init expr */
4558 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4562 scope_stack_bottom
= scope_stack_bottom
->next
;
4563 sym_pop(&local_stack
, s
);
4565 if (tok
== TOK_DO
) {
4570 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4581 if (tok
== TOK_SWITCH
) {
4585 /* XXX: other types than integer */
4586 case_reg
= gv(RC_INT
);
4590 b
= gjmp(0); /* jump to first case */
4592 block(&a
, csym
, &b
, &c
, case_reg
, 0);
4593 /* if no default, jmp after switch */
4601 if (tok
== TOK_CASE
) {
4608 if (gnu_ext
&& tok
== TOK_DOTS
) {
4612 tcc_warning("empty case range");
4614 /* since a case is like a label, we must skip it with a jmp */
4621 *case_sym
= gtst(1, 0);
4624 *case_sym
= gtst(1, 0);
4628 *case_sym
= gtst(1, *case_sym
);
4633 goto block_after_label
;
4635 if (tok
== TOK_DEFAULT
) {
4641 tcc_error("too many 'default'");
4644 goto block_after_label
;
4646 if (tok
== TOK_GOTO
) {
4648 if (tok
== '*' && gnu_ext
) {
4652 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
4655 } else if (tok
>= TOK_UIDENT
) {
4656 s
= label_find(tok
);
4657 /* put forward definition if needed */
4659 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4661 if (s
->r
== LABEL_DECLARED
)
4662 s
->r
= LABEL_FORWARD
;
4664 /* label already defined */
4665 if (s
->r
& LABEL_FORWARD
)
4666 s
->jnext
= gjmp(s
->jnext
);
4668 gjmp_addr(s
->jnext
);
4671 expect("label identifier");
4674 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
4682 if (s
->r
== LABEL_DEFINED
)
4683 tcc_error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
4685 s
->r
= LABEL_DEFINED
;
4687 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
4690 /* we accept this, but it is a mistake */
4693 tcc_warning("deprecated use of label at end of compound statement");
4697 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4700 /* expression case */
4715 /* t is the array or struct type. c is the array or struct
4716 address. cur_index/cur_field is the pointer to the current
4717 value. 'size_only' is true if only size info is needed (only used
4719 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
4720 int *cur_index
, Sym
**cur_field
,
4724 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
4730 if (gnu_ext
&& (l
= is_label()) != 0)
4732 while (tok
== '[' || tok
== '.') {
4734 if (!(type
->t
& VT_ARRAY
))
4735 expect("array type");
4738 index
= expr_const();
4739 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
4740 expect("invalid index");
4741 if (tok
== TOK_DOTS
&& gnu_ext
) {
4743 index_last
= expr_const();
4744 if (index_last
< 0 ||
4745 (s
->c
>= 0 && index_last
>= s
->c
) ||
4747 expect("invalid index");
4753 *cur_index
= index_last
;
4754 type
= pointed_type(type
);
4755 elem_size
= type_size(type
, &align
);
4756 c
+= index
* elem_size
;
4757 /* NOTE: we only support ranges for last designator */
4758 nb_elems
= index_last
- index
+ 1;
4759 if (nb_elems
!= 1) {
4768 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
4769 expect("struct/union type");
4782 /* XXX: fix this mess by using explicit storage field */
4784 type1
.t
|= (type
->t
& ~VT_TYPE
);
4798 if (type
->t
& VT_ARRAY
) {
4800 type
= pointed_type(type
);
4801 c
+= index
* type_size(type
, &align
);
4805 tcc_error("too many field init");
4806 /* XXX: fix this mess by using explicit storage field */
4808 type1
.t
|= (type
->t
& ~VT_TYPE
);
4813 decl_initializer(type
, sec
, c
, 0, size_only
);
4815 /* XXX: make it more general */
4816 if (!size_only
&& nb_elems
> 1) {
4817 unsigned long c_end
;
4822 tcc_error("range init not supported yet for dynamic storage");
4823 c_end
= c
+ nb_elems
* elem_size
;
4824 if (c_end
> sec
->data_allocated
)
4825 section_realloc(sec
, c_end
);
4826 src
= sec
->data
+ c
;
4828 for(i
= 1; i
< nb_elems
; i
++) {
4830 memcpy(dst
, src
, elem_size
);
4836 #define EXPR_CONST 1
4839 /* store a value or an expression directly in global data or in local array */
4840 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
4841 int v
, int expr_type
)
4843 int saved_global_expr
, bt
, bit_pos
, bit_size
;
4845 unsigned long long bit_mask
;
4853 /* compound literals must be allocated globally in this case */
4854 saved_global_expr
= global_expr
;
4857 global_expr
= saved_global_expr
;
4858 /* NOTE: symbols are accepted */
4859 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
4860 tcc_error("initializer element is not constant");
4868 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
4871 /* XXX: not portable */
4872 /* XXX: generate error if incorrect relocation */
4873 gen_assign_cast(&dtype
);
4874 bt
= type
->t
& VT_BTYPE
;
4875 /* we'll write at most 12 bytes */
4876 if (c
+ 12 > sec
->data_allocated
) {
4877 section_realloc(sec
, c
+ 12);
4879 ptr
= sec
->data
+ c
;
4880 /* XXX: make code faster ? */
4881 if (!(type
->t
& VT_BITFIELD
)) {
4886 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4887 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
4888 bit_mask
= (1LL << bit_size
) - 1;
4890 if ((vtop
->r
& VT_SYM
) &&
4896 (bt
== VT_INT
&& bit_size
!= 32)))
4897 tcc_error("initializer element is not computable at load time");
4900 vtop
->c
.i
= (vtop
->c
.i
!= 0);
4902 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4905 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4908 *(double *)ptr
= vtop
->c
.d
;
4911 *(long double *)ptr
= vtop
->c
.ld
;
4914 *(long long *)ptr
|= (vtop
->c
.ll
& bit_mask
) << bit_pos
;
4917 if (vtop
->r
& VT_SYM
) {
4918 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
4920 *(int *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4925 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
4932 /* put zeros for variable based init */
4933 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
4936 /* nothing to do because globals are already set to zero */
4938 vpush_global_sym(&func_old_type
, TOK_memset
);
4946 /* 't' contains the type and storage info. 'c' is the offset of the
4947 object in section 'sec'. If 'sec' is NULL, it means stack based
4948 allocation. 'first' is true if array '{' must be read (multi
4949 dimension implicit array init handling). 'size_only' is true if
4950 size only evaluation is wanted (only for arrays). */
4951 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
4952 int first
, int size_only
)
4954 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, parlevel1
, i
;
4955 int size1
, align1
, expr_type
;
4959 if (type
->t
& VT_VLA
) {
4960 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
4964 vpush_global_sym(&func_old_type
, TOK_alloca
);
4965 vla_runtime_type_size(type
, &a
);
4970 vsetc(type
, REG_IRET
, &retcval
);
4971 vset(type
, VT_LOCAL
|VT_LVAL
, c
);
4976 tcc_error("variable length arrays unsupported for this target");
4978 } else if (type
->t
& VT_ARRAY
) {
4982 t1
= pointed_type(type
);
4983 size1
= type_size(t1
, &align1
);
4986 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
4989 tcc_error("character array initializer must be a literal,"
4990 " optionally enclosed in braces");
4995 /* only parse strings here if correct type (otherwise: handle
4996 them as ((w)char *) expressions */
4997 if ((tok
== TOK_LSTR
&&
4998 #ifdef TCC_TARGET_PE
4999 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
5001 (t1
->t
& VT_BTYPE
) == VT_INT
5003 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
5004 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5009 /* compute maximum number of chars wanted */
5011 cstr_len
= cstr
->size
;
5013 cstr_len
= cstr
->size
/ sizeof(nwchar_t
);
5016 if (n
>= 0 && nb
> (n
- array_length
))
5017 nb
= n
- array_length
;
5020 tcc_warning("initializer-string for array is too long");
5021 /* in order to go faster for common case (char
5022 string in global variable, we handle it
5024 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
5025 memcpy(sec
->data
+ c
+ array_length
, cstr
->data
, nb
);
5029 ch
= ((unsigned char *)cstr
->data
)[i
];
5031 ch
= ((nwchar_t
*)cstr
->data
)[i
];
5032 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
5040 /* only add trailing zero if enough storage (no
5041 warning in this case since it is standard) */
5042 if (n
< 0 || array_length
< n
) {
5044 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
5050 while (tok
!= '}') {
5051 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
5052 if (n
>= 0 && index
>= n
)
5053 tcc_error("index too large");
5054 /* must put zero in holes (note that doing it that way
5055 ensures that it even works with designators) */
5056 if (!size_only
&& array_length
< index
) {
5057 init_putz(t1
, sec
, c
+ array_length
* size1
,
5058 (index
- array_length
) * size1
);
5061 if (index
> array_length
)
5062 array_length
= index
;
5063 /* special test for multi dimensional arrays (may not
5064 be strictly correct if designators are used at the
5066 if (index
>= n
&& no_oblock
)
5075 /* put zeros at the end */
5076 if (!size_only
&& n
>= 0 && array_length
< n
) {
5077 init_putz(t1
, sec
, c
+ array_length
* size1
,
5078 (n
- array_length
) * size1
);
5080 /* patch type size if needed */
5082 s
->c
= array_length
;
5083 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
5084 (sec
|| !first
|| tok
== '{')) {
5087 /* NOTE: the previous test is a specific case for automatic
5088 struct/union init */
5089 /* XXX: union needs only one init */
5091 /* XXX: this test is incorrect for local initializers
5092 beginning with ( without {. It would be much more difficult
5093 to do it correctly (ideally, the expression parser should
5094 be used in all cases) */
5100 while (tok
== '(') {
5104 if (!parse_btype(&type1
, &ad1
))
5106 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
5108 if (!is_assignable_types(type
, &type1
))
5109 tcc_error("invalid type for cast");
5114 if (first
|| tok
== '{') {
5123 while (tok
!= '}') {
5124 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
5126 if (!size_only
&& array_length
< index
) {
5127 init_putz(type
, sec
, c
+ array_length
,
5128 index
- array_length
);
5130 index
= index
+ type_size(&f
->type
, &align1
);
5131 if (index
> array_length
)
5132 array_length
= index
;
5134 /* gr: skip fields from same union - ugly. */
5136 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
5137 /* test for same offset */
5138 if (f
->next
->c
!= f
->c
)
5140 /* if yes, test for bitfield shift */
5141 if ((f
->type
.t
& VT_BITFIELD
) && (f
->next
->type
.t
& VT_BITFIELD
)) {
5142 int bit_pos_1
= (f
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5143 int bit_pos_2
= (f
->next
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5144 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
5145 if (bit_pos_1
!= bit_pos_2
)
5152 if (no_oblock
&& f
== NULL
)
5158 /* put zeros at the end */
5159 if (!size_only
&& array_length
< n
) {
5160 init_putz(type
, sec
, c
+ array_length
,
5169 } else if (tok
== '{') {
5171 decl_initializer(type
, sec
, c
, first
, size_only
);
5173 } else if (size_only
) {
5174 /* just skip expression */
5175 parlevel
= parlevel1
= 0;
5176 while ((parlevel
> 0 || parlevel1
> 0 ||
5177 (tok
!= '}' && tok
!= ',')) && tok
!= -1) {
5180 else if (tok
== ')')
5182 else if (tok
== '{')
5184 else if (tok
== '}')
5189 /* currently, we always use constant expression for globals
5190 (may change for scripting case) */
5191 expr_type
= EXPR_CONST
;
5193 expr_type
= EXPR_ANY
;
5194 init_putv(type
, sec
, c
, 0, expr_type
);
5198 /* parse an initializer for type 't' if 'has_init' is non zero, and
5199 allocate space in local or global data space ('r' is either
5200 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5201 variable 'v' with an associated name represented by 'asm_label' of
5202 scope 'scope' is declared before initializers are parsed. If 'v' is
5203 zero, then a reference to the new object is put in the value stack.
5204 If 'has_init' is 2, a special parsing is done to handle string
5206 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
5207 int has_init
, int v
, char *asm_label
,
5210 int size
, align
, addr
, data_offset
;
5212 ParseState saved_parse_state
= {0};
5213 TokenString init_str
;
5215 Sym
*flexible_array
;
5217 flexible_array
= NULL
;
5218 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
5221 while (field
&& field
->next
)
5222 field
= field
->next
;
5223 if (field
->type
.t
& VT_ARRAY
&& field
->type
.ref
->c
< 0)
5224 flexible_array
= field
;
5227 size
= type_size(type
, &align
);
5228 /* If unknown size, we must evaluate it before
5229 evaluating initializers because
5230 initializers can generate global data too
5231 (e.g. string pointers or ISOC99 compound
5232 literals). It also simplifies local
5233 initializers handling */
5234 tok_str_new(&init_str
);
5235 if (size
< 0 || (flexible_array
&& has_init
)) {
5237 tcc_error("unknown type size");
5238 /* get all init string */
5239 if (has_init
== 2) {
5240 /* only get strings */
5241 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5242 tok_str_add_tok(&init_str
);
5247 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
5249 tcc_error("unexpected end of file in initializer");
5250 tok_str_add_tok(&init_str
);
5253 else if (tok
== '}') {
5263 tok_str_add(&init_str
, -1);
5264 tok_str_add(&init_str
, 0);
5267 save_parse_state(&saved_parse_state
);
5269 macro_ptr
= init_str
.str
;
5271 decl_initializer(type
, NULL
, 0, 1, 1);
5272 /* prepare second initializer parsing */
5273 macro_ptr
= init_str
.str
;
5276 /* if still unknown size, error */
5277 size
= type_size(type
, &align
);
5279 tcc_error("unknown type size");
5282 size
+= flexible_array
->type
.ref
->c
* pointed_size(&flexible_array
->type
);
5283 /* take into account specified alignment if bigger */
5285 if (ad
->aligned
> align
)
5286 align
= ad
->aligned
;
5287 } else if (ad
->packed
) {
5290 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
5292 #ifdef CONFIG_TCC_BCHECK
5293 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5297 loc
= (loc
- size
) & -align
;
5299 #ifdef CONFIG_TCC_BCHECK
5300 /* handles bounds */
5301 /* XXX: currently, since we do only one pass, we cannot track
5302 '&' operators, so we add only arrays */
5303 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5304 unsigned long *bounds_ptr
;
5305 /* add padding between regions */
5307 /* then add local bound info */
5308 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(unsigned long));
5309 bounds_ptr
[0] = addr
;
5310 bounds_ptr
[1] = size
;
5314 /* local variable */
5315 sym_push(v
, type
, r
, addr
);
5317 /* push local reference */
5318 vset(type
, r
, addr
);
5324 if (v
&& scope
== VT_CONST
) {
5325 /* see if the symbol was already defined */
5328 if (!is_compatible_types(&sym
->type
, type
))
5329 tcc_error("incompatible types for redefinition of '%s'",
5330 get_tok_str(v
, NULL
));
5331 if (sym
->type
.t
& VT_EXTERN
) {
5332 /* if the variable is extern, it was not allocated */
5333 sym
->type
.t
&= ~VT_EXTERN
;
5334 /* set array size if it was ommited in extern
5336 if ((sym
->type
.t
& VT_ARRAY
) &&
5337 sym
->type
.ref
->c
< 0 &&
5339 sym
->type
.ref
->c
= type
->ref
->c
;
5341 /* we accept several definitions of the same
5342 global variable. this is tricky, because we
5343 must play with the SHN_COMMON type of the symbol */
5344 /* XXX: should check if the variable was already
5345 initialized. It is incorrect to initialized it
5347 /* no init data, we won't add more to the symbol */
5354 /* allocate symbol in corresponding section */
5359 else if (tcc_state
->nocommon
)
5363 data_offset
= sec
->data_offset
;
5364 data_offset
= (data_offset
+ align
- 1) & -align
;
5366 /* very important to increment global pointer at this time
5367 because initializers themselves can create new initializers */
5368 data_offset
+= size
;
5369 #ifdef CONFIG_TCC_BCHECK
5370 /* add padding if bound check */
5371 if (tcc_state
->do_bounds_check
)
5374 sec
->data_offset
= data_offset
;
5375 /* allocate section space to put the data */
5376 if (sec
->sh_type
!= SHT_NOBITS
&&
5377 data_offset
> sec
->data_allocated
)
5378 section_realloc(sec
, data_offset
);
5379 /* align section if needed */
5380 if (align
> sec
->sh_addralign
)
5381 sec
->sh_addralign
= align
;
5383 addr
= 0; /* avoid warning */
5387 if (scope
!= VT_CONST
|| !sym
) {
5388 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
5389 sym
->asm_label
= asm_label
;
5391 /* update symbol definition */
5393 put_extern_sym(sym
, sec
, addr
, size
);
5396 /* put a common area */
5397 put_extern_sym(sym
, NULL
, align
, size
);
5398 /* XXX: find a nicer way */
5399 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
5400 esym
->st_shndx
= SHN_COMMON
;
5405 /* push global reference */
5406 sym
= get_sym_ref(type
, sec
, addr
, size
);
5408 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
5411 /* patch symbol weakness */
5412 if (type
->t
& VT_WEAK
)
5414 #ifdef CONFIG_TCC_BCHECK
5415 /* handles bounds now because the symbol must be defined
5416 before for the relocation */
5417 if (tcc_state
->do_bounds_check
) {
5418 unsigned long *bounds_ptr
;
5420 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
5421 /* then add global bound info */
5422 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(long));
5423 bounds_ptr
[0] = 0; /* relocated */
5424 bounds_ptr
[1] = size
;
5428 if (has_init
|| (type
->t
& VT_VLA
)) {
5429 decl_initializer(type
, sec
, addr
, 1, 0);
5430 /* restore parse state if needed */
5432 tok_str_free(init_str
.str
);
5433 restore_parse_state(&saved_parse_state
);
5435 /* patch flexible array member size back to -1, */
5436 /* for possible subsequent similar declarations */
5438 flexible_array
->type
.ref
->c
= -1;
5443 static void put_func_debug(Sym
*sym
)
5448 /* XXX: we put here a dummy type */
5449 snprintf(buf
, sizeof(buf
), "%s:%c1",
5450 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
5451 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
5452 cur_text_section
, sym
->c
);
5453 /* //gr gdb wants a line at the function */
5454 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
5459 /* parse an old style function declaration list */
5460 /* XXX: check multiple parameter */
5461 static void func_decl_list(Sym
*func_sym
)
5468 /* parse each declaration */
5469 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
&&
5470 tok
!= TOK_ASM1
&& tok
!= TOK_ASM2
&& tok
!= TOK_ASM3
) {
5471 if (!parse_btype(&btype
, &ad
))
5472 expect("declaration list");
5473 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5474 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5476 /* we accept no variable after */
5480 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5481 /* find parameter in function parameter list */
5484 if ((s
->v
& ~SYM_FIELD
) == v
)
5488 tcc_error("declaration for parameter '%s' but no such parameter",
5489 get_tok_str(v
, NULL
));
5491 /* check that no storage specifier except 'register' was given */
5492 if (type
.t
& VT_STORAGE
)
5493 tcc_error("storage class specified for '%s'", get_tok_str(v
, NULL
));
5494 convert_parameter_type(&type
);
5495 /* we can add the type (NOTE: it could be local to the function) */
5497 /* accept other parameters */
5508 /* parse a function defined by symbol 'sym' and generate its code in
5509 'cur_text_section' */
5510 static void gen_function(Sym
*sym
)
5512 int saved_nocode_wanted
= nocode_wanted
;
5514 ind
= cur_text_section
->data_offset
;
5515 /* NOTE: we patch the symbol size later */
5516 put_extern_sym(sym
, cur_text_section
, ind
, 0);
5517 funcname
= get_tok_str(sym
->v
, NULL
);
5519 /* put debug symbol */
5520 if (tcc_state
->do_debug
)
5521 put_func_debug(sym
);
5522 /* push a dummy symbol to enable local sym storage */
5523 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
5524 gfunc_prolog(&sym
->type
);
5526 block(NULL
, NULL
, NULL
, NULL
, 0, 0);
5529 cur_text_section
->data_offset
= ind
;
5530 label_pop(&global_label_stack
, NULL
);
5531 /* reset local stack */
5532 scope_stack_bottom
= NULL
;
5533 sym_pop(&local_stack
, NULL
);
5534 /* end of function */
5535 /* patch symbol size */
5536 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
5538 /* patch symbol weakness (this definition overrules any prototype) */
5539 if (sym
->type
.t
& VT_WEAK
)
5541 if (tcc_state
->do_debug
) {
5542 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
5544 /* It's better to crash than to generate wrong code */
5545 cur_text_section
= NULL
;
5546 funcname
= ""; /* for safety */
5547 func_vt
.t
= VT_VOID
; /* for safety */
5548 ind
= 0; /* for safety */
5549 nocode_wanted
= saved_nocode_wanted
;
5552 ST_FUNC
void gen_inline_functions(void)
5555 int *str
, inline_generated
, i
;
5556 struct InlineFunc
*fn
;
5558 /* iterate while inline function are referenced */
5560 inline_generated
= 0;
5561 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5562 fn
= tcc_state
->inline_fns
[i
];
5564 if (sym
&& sym
->c
) {
5565 /* the function was used: generate its code and
5566 convert it to a normal function */
5567 str
= fn
->token_str
;
5570 strcpy(file
->filename
, fn
->filename
);
5571 sym
->r
= VT_SYM
| VT_CONST
;
5572 sym
->type
.t
&= ~VT_INLINE
;
5576 cur_text_section
= text_section
;
5578 macro_ptr
= NULL
; /* fail safe */
5580 inline_generated
= 1;
5583 if (!inline_generated
)
5586 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5587 fn
= tcc_state
->inline_fns
[i
];
5588 str
= fn
->token_str
;
5591 dynarray_reset(&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
);
5594 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
5595 static int decl0(int l
, int is_for_loop_init
)
5603 if (!parse_btype(&btype
, &ad
)) {
5604 if (is_for_loop_init
)
5606 /* skip redundant ';' */
5607 /* XXX: find more elegant solution */
5612 if (l
== VT_CONST
&&
5613 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5614 /* global asm block */
5618 /* special test for old K&R protos without explicit int
5619 type. Only accepted when defining global data */
5620 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
5624 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5625 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5627 /* we accept no variable after */
5631 while (1) { /* iterate thru each declaration */
5632 char *asm_label
; // associated asm label
5634 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5638 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
5639 printf("type = '%s'\n", buf
);
5642 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5643 if ((type
.t
& VT_STATIC
) && (l
== VT_LOCAL
)) {
5644 tcc_error("function without file scope cannot be static");
5646 /* if old style function prototype, we accept a
5649 if (sym
->c
== FUNC_OLD
)
5650 func_decl_list(sym
);
5654 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5657 asm_label_instr(&astr
);
5658 asm_label
= tcc_strdup(astr
.data
);
5661 /* parse one last attribute list, after asm label */
5662 parse_attribute(&ad
);
5667 #ifdef TCC_TARGET_PE
5669 type
.t
|= VT_IMPORT
;
5671 type
.t
|= VT_EXPORT
;
5675 tcc_error("cannot use local functions");
5676 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
5677 expect("function definition");
5679 /* reject abstract declarators in function definition */
5681 while ((sym
= sym
->next
) != NULL
)
5682 if (!(sym
->v
& ~SYM_FIELD
))
5683 expect("identifier");
5685 /* XXX: cannot do better now: convert extern line to static inline */
5686 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
5687 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5691 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
5694 r
= sym
->type
.ref
->r
;
5695 /* use func_call from prototype if not defined */
5696 if (FUNC_CALL(r
) != FUNC_CDECL
5697 && FUNC_CALL(type
.ref
->r
) == FUNC_CDECL
)
5698 FUNC_CALL(type
.ref
->r
) = FUNC_CALL(r
);
5700 /* use export from prototype */
5702 FUNC_EXPORT(type
.ref
->r
) = 1;
5704 /* use static from prototype */
5705 if (sym
->type
.t
& VT_STATIC
)
5706 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5708 if (!is_compatible_types(&sym
->type
, &type
)) {
5710 tcc_error("incompatible types for redefinition of '%s'",
5711 get_tok_str(v
, NULL
));
5713 /* if symbol is already defined, then put complete type */
5716 /* put function symbol */
5717 sym
= global_identifier_push(v
, type
.t
, 0);
5718 sym
->type
.ref
= type
.ref
;
5721 /* static inline functions are just recorded as a kind
5722 of macro. Their code will be emitted at the end of
5723 the compilation unit only if they are used */
5724 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
5725 (VT_INLINE
| VT_STATIC
)) {
5726 TokenString func_str
;
5728 struct InlineFunc
*fn
;
5729 const char *filename
;
5731 tok_str_new(&func_str
);
5737 tcc_error("unexpected end of file");
5738 tok_str_add_tok(&func_str
);
5743 } else if (t
== '}') {
5745 if (block_level
== 0)
5749 tok_str_add(&func_str
, -1);
5750 tok_str_add(&func_str
, 0);
5751 filename
= file
? file
->filename
: "";
5752 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
5753 strcpy(fn
->filename
, filename
);
5755 fn
->token_str
= func_str
.str
;
5756 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
5759 /* compute text section */
5760 cur_text_section
= ad
.section
;
5761 if (!cur_text_section
)
5762 cur_text_section
= text_section
;
5763 sym
->r
= VT_SYM
| VT_CONST
;
5768 if (btype
.t
& VT_TYPEDEF
) {
5769 /* save typedefed type */
5770 /* XXX: test storage specifiers ? */
5771 sym
= sym_push(v
, &type
, INT_ATTR(&ad
), 0);
5772 sym
->type
.t
|= VT_TYPEDEF
;
5775 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5776 /* external function definition */
5777 /* specific case for func_call attribute */
5778 type
.ref
->r
= INT_ATTR(&ad
);
5779 } else if (!(type
.t
& VT_ARRAY
)) {
5780 /* not lvalue if array */
5781 r
|= lvalue_type(type
.t
);
5783 has_init
= (tok
== '=');
5784 if (has_init
&& (type
.t
& VT_VLA
))
5785 tcc_error("Variable length array cannot be initialized");
5786 if ((btype
.t
& VT_EXTERN
) || ((type
.t
& VT_BTYPE
) == VT_FUNC
) ||
5787 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
5788 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
5789 /* external variable or function */
5790 /* NOTE: as GCC, uninitialized global static
5791 arrays of null size are considered as
5793 sym
= external_sym(v
, &type
, r
, asm_label
);
5795 if (type
.t
& VT_WEAK
)
5798 if (ad
.alias_target
) {
5803 alias_target
= sym_find(ad
.alias_target
);
5804 if (!alias_target
|| !alias_target
->c
)
5805 tcc_error("unsupported forward __alias__ attribute");
5806 esym
= &((Elf32_Sym
*)symtab_section
->data
)[alias_target
->c
];
5807 tsec
.sh_num
= esym
->st_shndx
;
5808 put_extern_sym2(sym
, &tsec
, esym
->st_value
, esym
->st_size
, 0);
5811 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
5812 if (type
.t
& VT_STATIC
)
5818 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, asm_label
, l
);
5822 if (is_for_loop_init
)
5835 ST_FUNC
void decl(int l
)