2 * TCC - Tiny C Compiler
4 * Copyright (c) 2001-2004 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 /********************************************************/
24 /* global variables */
26 /* loc : local variable index
27 ind : output code index
29 anon_sym: anonymous symbol index
31 ST_DATA
int rsym
, anon_sym
, ind
, loc
;
33 ST_DATA Section
*text_section
, *data_section
, *bss_section
; /* predefined sections */
34 ST_DATA Section
*cur_text_section
; /* current section where function code is generated */
36 ST_DATA Section
*last_text_section
; /* to handle .previous asm directive */
38 #ifdef CONFIG_TCC_BCHECK
39 /* bound check related sections */
40 ST_DATA Section
*bounds_section
; /* contains global data bound description */
41 ST_DATA Section
*lbounds_section
; /* contains local data bound description */
44 ST_DATA Section
*symtab_section
, *strtab_section
;
46 ST_DATA Section
*stab_section
, *stabstr_section
;
47 ST_DATA Sym
*sym_free_first
;
48 ST_DATA
void **sym_pools
;
49 ST_DATA
int nb_sym_pools
;
51 ST_DATA Sym
*global_stack
;
52 ST_DATA Sym
*local_stack
;
53 ST_DATA Sym
*scope_stack_bottom
;
54 ST_DATA Sym
*define_stack
;
55 ST_DATA Sym
*global_label_stack
;
56 ST_DATA Sym
*local_label_stack
;
58 ST_DATA SValue __vstack
[1+VSTACK_SIZE
], *vtop
;
60 ST_DATA
int const_wanted
; /* true if constant wanted */
61 ST_DATA
int nocode_wanted
; /* true if no code generation wanted for an expression */
62 ST_DATA
int global_expr
; /* true if compound literals must be allocated globally (used during initializers parsing */
63 ST_DATA CType func_vt
; /* current function return type (used by return instruction) */
65 ST_DATA
int last_line_num
, last_ind
, func_ind
; /* debug last line number and pc */
66 ST_DATA
char *funcname
;
68 ST_DATA CType char_pointer_type
, func_old_type
, int_type
, size_type
;
70 /* ------------------------------------------------------------------------- */
71 static void gen_cast(CType
*type
);
72 static inline CType
*pointed_type(CType
*type
);
73 static int is_compatible_types(CType
*type1
, CType
*type2
);
74 static int parse_btype(CType
*type
, AttributeDef
*ad
);
75 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
);
76 static void parse_expr_type(CType
*type
);
77 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
, int first
, int size_only
);
78 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
, int case_reg
, int is_expr
);
79 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
, int has_init
, int v
, char *asm_label
, int scope
);
80 static int decl0(int l
, int is_for_loop_init
);
81 static void expr_eq(void);
82 static void unary_type(CType
*type
);
83 static void vla_runtime_type_size(CType
*type
, int *a
);
84 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
);
85 static void expr_type(CType
*type
);
87 ST_INLN
int is_float(int t
)
91 return bt
== VT_LDOUBLE
|| bt
== VT_DOUBLE
|| bt
== VT_FLOAT
|| bt
== VT_QFLOAT
;
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 arbitrary 64bit constant */
348 void vpush64(int ty
, unsigned long long v
)
355 vsetc(&ctype
, VT_CONST
, &cval
);
358 /* push long long constant */
359 static inline void vpushll(long long v
)
361 vpush64(VT_LLONG
, v
);
364 /* Return a static symbol pointing to a section */
365 ST_FUNC Sym
*get_sym_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
371 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
372 sym
->type
.ref
= type
->ref
;
373 sym
->r
= VT_CONST
| VT_SYM
;
374 put_extern_sym(sym
, sec
, offset
, size
);
378 /* push a reference to a section offset by adding a dummy symbol */
379 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
384 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
385 vtop
->sym
= get_sym_ref(type
, sec
, offset
, size
);
388 /* define a new external reference to a symbol 'v' of type 'u' */
389 ST_FUNC Sym
*external_global_sym(int v
, CType
*type
, int r
)
395 /* push forward reference */
396 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
397 s
->type
.ref
= type
->ref
;
398 s
->r
= r
| VT_CONST
| VT_SYM
;
403 /* define a new external reference to a symbol 'v' with alternate asm
404 name 'asm_label' of type 'u'. 'asm_label' is equal to NULL if there
405 is no alternate name (most cases) */
406 static Sym
*external_sym(int v
, CType
*type
, int r
, char *asm_label
)
412 /* push forward reference */
413 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
414 s
->asm_label
= asm_label
;
415 s
->type
.t
|= VT_EXTERN
;
416 } else if (s
->type
.ref
== func_old_type
.ref
) {
417 s
->type
.ref
= type
->ref
;
418 s
->r
= r
| VT_CONST
| VT_SYM
;
419 s
->type
.t
|= VT_EXTERN
;
420 } else if (!is_compatible_types(&s
->type
, type
)) {
421 tcc_error("incompatible types for redefinition of '%s'",
422 get_tok_str(v
, NULL
));
427 /* push a reference to global symbol v */
428 ST_FUNC
void vpush_global_sym(CType
*type
, int v
)
433 sym
= external_global_sym(v
, type
, 0);
435 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
439 ST_FUNC
void vset(CType
*type
, int r
, int v
)
444 vsetc(type
, r
, &cval
);
447 static void vseti(int r
, int v
)
455 ST_FUNC
void vswap(void)
458 /* cannot let cpu flags if other instruction are generated. Also
459 avoid leaving VT_JMP anywhere except on the top of the stack
460 because it would complicate the code generator. */
461 if (vtop
>= vstack
) {
462 int v
= vtop
->r
& VT_VALMASK
;
463 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
470 /* XXX: +2% overall speed possible with optimized memswap
472 * memswap(&vtop[0], &vtop[1], sizeof *vtop);
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' (of type 't') to 'r', and flush previous value of r to memory
631 static void move_reg(int r
, int s
, int t
)
645 /* get address of vtop (vtop MUST BE an lvalue) */
646 static void gaddrof(void)
648 if (vtop
->r
& VT_REF
)
651 /* tricky: if saved lvalue, then we can go back to lvalue */
652 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
653 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
658 #ifdef CONFIG_TCC_BCHECK
659 /* generate lvalue bound code */
660 static void gbound(void)
665 vtop
->r
&= ~VT_MUSTBOUND
;
666 /* if lvalue, then use checking code before dereferencing */
667 if (vtop
->r
& VT_LVAL
) {
668 /* if not VT_BOUNDED value, then make one */
669 if (!(vtop
->r
& VT_BOUNDED
)) {
670 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
671 /* must save type because we must set it to int to get pointer */
673 vtop
->type
.t
= VT_INT
;
676 gen_bounded_ptr_add();
677 vtop
->r
|= lval_type
;
680 /* then check for dereferencing */
681 gen_bounded_ptr_deref();
686 /* store vtop a register belonging to class 'rc'. lvalues are
687 converted to values. Cannot be used if cannot be converted to
688 register value (such as structures). */
689 ST_FUNC
int gv(int rc
)
691 int r
, bit_pos
, bit_size
, size
, align
, i
;
694 /* NOTE: get_reg can modify vstack[] */
695 if (vtop
->type
.t
& VT_BITFIELD
) {
698 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
699 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
700 /* remove bit field info to avoid loops */
701 vtop
->type
.t
&= ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
702 /* cast to int to propagate signedness in following ops */
703 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
708 if((vtop
->type
.t
& VT_UNSIGNED
) ||
709 (vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
710 type
.t
|= VT_UNSIGNED
;
712 /* generate shifts */
713 vpushi(bits
- (bit_pos
+ bit_size
));
715 vpushi(bits
- bit_size
);
716 /* NOTE: transformed to SHR if unsigned */
720 if (is_float(vtop
->type
.t
) &&
721 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
724 unsigned long offset
;
725 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
729 /* XXX: unify with initializers handling ? */
730 /* CPUs usually cannot use float constants, so we store them
731 generically in data segment */
732 size
= type_size(&vtop
->type
, &align
);
733 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
734 data_section
->data_offset
= offset
;
735 /* XXX: not portable yet */
736 #if defined(__i386__) || defined(__x86_64__)
737 /* Zero pad x87 tenbyte long doubles */
738 if (size
== LDOUBLE_SIZE
) {
739 vtop
->c
.tab
[2] &= 0xffff;
740 #if LDOUBLE_SIZE == 16
745 ptr
= section_ptr_add(data_section
, size
);
747 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
751 ptr
[i
] = vtop
->c
.tab
[size
-1-i
];
755 ptr
[i
] = vtop
->c
.tab
[i
];
756 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
757 vtop
->r
|= VT_LVAL
| VT_SYM
;
761 #ifdef CONFIG_TCC_BCHECK
762 if (vtop
->r
& VT_MUSTBOUND
)
766 r
= vtop
->r
& VT_VALMASK
;
767 rc2
= (rc
& RC_FLOAT
) ? RC_FLOAT
: RC_INT
;
770 #ifdef TCC_TARGET_X86_64
771 else if (rc
== RC_FRET
)
775 /* need to reload if:
777 - lvalue (need to dereference pointer)
778 - already a register, but not in the right class */
780 || (vtop
->r
& VT_LVAL
)
781 || !(reg_classes
[r
] & rc
)
782 #ifdef TCC_TARGET_X86_64
783 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
784 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
&& !(reg_classes
[vtop
->r2
] & rc2
))
786 || ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
791 #ifdef TCC_TARGET_X86_64
792 if (((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
)) {
793 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
795 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
796 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
798 int r2
, original_type
;
799 unsigned long long ll
;
800 original_type
= vtop
->type
.t
;
801 /* two register type load : expand to two words
803 #ifndef TCC_TARGET_X86_64
804 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
807 vtop
->c
.ui
= ll
; /* first word */
809 vtop
->r
= r
; /* save register value */
810 vpushi(ll
>> 32); /* second word */
813 if (r
>= VT_CONST
|| /* XXX: test to VT_CONST incorrect ? */
814 (vtop
->r
& VT_LVAL
)) {
815 /* We do not want to modifier the long long
816 pointer here, so the safest (and less
817 efficient) is to save all the other registers
818 in the stack. XXX: totally inefficient. */
820 /* load from memory */
821 vtop
->type
.t
= load_type
;
824 vtop
[-1].r
= r
; /* save register value */
825 /* increment pointer to get second word */
826 vtop
->type
.t
= addr_type
;
831 vtop
->type
.t
= load_type
;
836 vtop
[-1].r
= r
; /* save register value */
837 vtop
->r
= vtop
[-1].r2
;
839 /* Allocate second register. Here we rely on the fact that
840 get_reg() tries first to free r2 of an SValue. */
844 /* write second register */
846 vtop
->type
.t
= original_type
;
847 } else if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
849 /* lvalue of scalar type : need to use lvalue type
850 because of possible cast */
853 /* compute memory access type */
854 if (vtop
->r
& VT_REF
)
855 #ifdef TCC_TARGET_X86_64
860 else if (vtop
->r
& VT_LVAL_BYTE
)
862 else if (vtop
->r
& VT_LVAL_SHORT
)
864 if (vtop
->r
& VT_LVAL_UNSIGNED
)
868 /* restore wanted type */
871 /* one register type load */
876 #ifdef TCC_TARGET_C67
877 /* uses register pairs for doubles */
878 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
885 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
886 ST_FUNC
void gv2(int rc1
, int rc2
)
890 /* generate more generic register first. But VT_JMP or VT_CMP
891 values must be generated first in all cases to avoid possible
893 v
= vtop
[0].r
& VT_VALMASK
;
894 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
899 /* test if reload is needed for first register */
900 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
910 /* test if reload is needed for first register */
911 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
917 /* wrapper around RC_FRET to return a register by type */
918 static int rc_fret(int t
)
920 #ifdef TCC_TARGET_X86_64
921 if (t
== VT_LDOUBLE
) {
928 /* wrapper around REG_FRET to return a register by type */
929 static int reg_fret(int t
)
931 #ifdef TCC_TARGET_X86_64
932 if (t
== VT_LDOUBLE
) {
939 /* expand long long on stack in two int registers */
940 static void lexpand(void)
944 u
= vtop
->type
.t
& VT_UNSIGNED
;
947 vtop
[0].r
= vtop
[-1].r2
;
948 vtop
[0].r2
= VT_CONST
;
949 vtop
[-1].r2
= VT_CONST
;
950 vtop
[0].type
.t
= VT_INT
| u
;
951 vtop
[-1].type
.t
= VT_INT
| u
;
954 #ifdef TCC_TARGET_ARM
955 /* expand long long on stack */
956 ST_FUNC
void lexpand_nr(void)
960 u
= vtop
->type
.t
& VT_UNSIGNED
;
963 vtop
->type
.t
= VT_INT
| u
;
964 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
966 vtop
[-1].c
.ui
= vtop
->c
.ull
;
967 vtop
->c
.ui
= vtop
->c
.ull
>> 32;
969 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
971 vtop
->r
= vtop
[-1].r
;
972 } else if (v
> VT_CONST
) {
976 vtop
->r
= vtop
[-1].r2
;
977 vtop
[-1].r2
= VT_CONST
;
978 vtop
[-1].type
.t
= VT_INT
| u
;
982 /* build a long long from two ints */
983 static void lbuild(int t
)
986 vtop
[-1].r2
= vtop
[0].r
;
991 /* rotate n first stack elements to the bottom
992 I1 ... In -> I2 ... In I1 [top is right]
994 ST_FUNC
void vrotb(int n
)
1000 for(i
=-n
+1;i
!=0;i
++)
1001 vtop
[i
] = vtop
[i
+1];
1005 /* rotate the n elements before entry e towards the top
1006 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
1008 ST_FUNC
void vrote(SValue
*e
, int n
)
1014 for(i
= 0;i
< n
- 1; i
++)
1019 /* rotate n first stack elements to the top
1020 I1 ... In -> In I1 ... I(n-1) [top is right]
1022 ST_FUNC
void vrott(int n
)
1027 /* pop stack value */
1028 ST_FUNC
void vpop(void)
1031 v
= vtop
->r
& VT_VALMASK
;
1032 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1033 /* for x86, we need to pop the FP stack */
1034 if (v
== TREG_ST0
&& !nocode_wanted
) {
1035 o(0xd8dd); /* fstp %st(0) */
1038 if (v
== VT_JMP
|| v
== VT_JMPI
) {
1039 /* need to put correct jump if && or || without test */
1045 /* convert stack entry to register and duplicate its value in another
1047 static void gv_dup(void)
1053 if ((t
& VT_BTYPE
) == VT_LLONG
) {
1060 /* stack: H L L1 H1 */
1068 /* duplicate value */
1073 #ifdef TCC_TARGET_X86_64
1074 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1084 load(r1
, &sv
); /* move r to r1 */
1086 /* duplicates value */
1092 #ifndef TCC_TARGET_X86_64
1093 /* generate CPU independent (unsigned) long long operations */
1094 static void gen_opl(int op
)
1096 int t
, a
, b
, op1
, c
, i
;
1098 unsigned short reg_iret
= REG_IRET
;
1099 unsigned short reg_lret
= REG_LRET
;
1105 func
= TOK___divdi3
;
1108 func
= TOK___udivdi3
;
1111 func
= TOK___moddi3
;
1114 func
= TOK___umoddi3
;
1121 /* call generic long long function */
1122 vpush_global_sym(&func_old_type
, func
);
1127 vtop
->r2
= reg_lret
;
1140 /* stack: L1 H1 L2 H2 */
1145 vtop
[-2] = vtop
[-3];
1148 /* stack: H1 H2 L1 L2 */
1154 /* stack: H1 H2 L1 L2 ML MH */
1157 /* stack: ML MH H1 H2 L1 L2 */
1161 /* stack: ML MH H1 L2 H2 L1 */
1166 /* stack: ML MH M1 M2 */
1169 } else if (op
== '+' || op
== '-') {
1170 /* XXX: add non carry method too (for MIPS or alpha) */
1176 /* stack: H1 H2 (L1 op L2) */
1179 gen_op(op1
+ 1); /* TOK_xxxC2 */
1182 /* stack: H1 H2 (L1 op L2) */
1185 /* stack: (L1 op L2) H1 H2 */
1187 /* stack: (L1 op L2) (H1 op H2) */
1195 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1196 t
= vtop
[-1].type
.t
;
1200 /* stack: L H shift */
1202 /* constant: simpler */
1203 /* NOTE: all comments are for SHL. the other cases are
1204 done by swaping words */
1215 if (op
!= TOK_SAR
) {
1248 /* XXX: should provide a faster fallback on x86 ? */
1251 func
= TOK___ashrdi3
;
1254 func
= TOK___lshrdi3
;
1257 func
= TOK___ashldi3
;
1263 /* compare operations */
1269 /* stack: L1 H1 L2 H2 */
1271 vtop
[-1] = vtop
[-2];
1273 /* stack: L1 L2 H1 H2 */
1276 /* when values are equal, we need to compare low words. since
1277 the jump is inverted, we invert the test too. */
1280 else if (op1
== TOK_GT
)
1282 else if (op1
== TOK_ULT
)
1284 else if (op1
== TOK_UGT
)
1289 if (op1
!= TOK_NE
) {
1293 /* generate non equal test */
1294 /* XXX: NOT PORTABLE yet */
1298 #if defined(TCC_TARGET_I386)
1299 b
= psym(0x850f, 0);
1300 #elif defined(TCC_TARGET_ARM)
1302 o(0x1A000000 | encbranch(ind
, 0, 1));
1303 #elif defined(TCC_TARGET_C67)
1304 tcc_error("not implemented");
1306 #error not supported
1310 /* compare low. Always unsigned */
1314 else if (op1
== TOK_LE
)
1316 else if (op1
== TOK_GT
)
1318 else if (op1
== TOK_GE
)
1329 /* handle integer constant optimizations and various machine
1331 static void gen_opic(int op
)
1333 int c1
, c2
, t1
, t2
, n
;
1336 typedef unsigned long long U
;
1340 t1
= v1
->type
.t
& VT_BTYPE
;
1341 t2
= v2
->type
.t
& VT_BTYPE
;
1345 else if (v1
->type
.t
& VT_UNSIGNED
)
1352 else if (v2
->type
.t
& VT_UNSIGNED
)
1357 /* currently, we cannot do computations with forward symbols */
1358 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1359 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1362 case '+': l1
+= l2
; break;
1363 case '-': l1
-= l2
; break;
1364 case '&': l1
&= l2
; break;
1365 case '^': l1
^= l2
; break;
1366 case '|': l1
|= l2
; break;
1367 case '*': l1
*= l2
; break;
1374 /* if division by zero, generate explicit division */
1377 tcc_error("division by zero in constant");
1381 default: l1
/= l2
; break;
1382 case '%': l1
%= l2
; break;
1383 case TOK_UDIV
: l1
= (U
)l1
/ l2
; break;
1384 case TOK_UMOD
: l1
= (U
)l1
% l2
; break;
1387 case TOK_SHL
: l1
<<= l2
; break;
1388 case TOK_SHR
: l1
= (U
)l1
>> l2
; break;
1389 case TOK_SAR
: l1
>>= l2
; break;
1391 case TOK_ULT
: l1
= (U
)l1
< (U
)l2
; break;
1392 case TOK_UGE
: l1
= (U
)l1
>= (U
)l2
; break;
1393 case TOK_EQ
: l1
= l1
== l2
; break;
1394 case TOK_NE
: l1
= l1
!= l2
; break;
1395 case TOK_ULE
: l1
= (U
)l1
<= (U
)l2
; break;
1396 case TOK_UGT
: l1
= (U
)l1
> (U
)l2
; break;
1397 case TOK_LT
: l1
= l1
< l2
; break;
1398 case TOK_GE
: l1
= l1
>= l2
; break;
1399 case TOK_LE
: l1
= l1
<= l2
; break;
1400 case TOK_GT
: l1
= l1
> l2
; break;
1402 case TOK_LAND
: l1
= l1
&& l2
; break;
1403 case TOK_LOR
: l1
= l1
|| l2
; break;
1410 /* if commutative ops, put c2 as constant */
1411 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1412 op
== '|' || op
== '*')) {
1414 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1415 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1417 /* Filter out NOP operations like x*1, x-0, x&-1... */
1418 if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1421 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1422 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1428 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1429 /* try to use shifts instead of muls or divs */
1430 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1439 else if (op
== TOK_PDIV
)
1445 } else if (c2
&& (op
== '+' || op
== '-') &&
1446 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
1447 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1448 /* symbol + constant case */
1455 if (!nocode_wanted
) {
1456 /* call low level op generator */
1457 if (t1
== VT_LLONG
|| t2
== VT_LLONG
)
1468 /* generate a floating point operation with constant propagation */
1469 static void gen_opif(int op
)
1477 /* currently, we cannot do computations with forward symbols */
1478 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1479 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1481 if (v1
->type
.t
== VT_FLOAT
) {
1484 } else if (v1
->type
.t
== VT_DOUBLE
) {
1492 /* NOTE: we only do constant propagation if finite number (not
1493 NaN or infinity) (ANSI spec) */
1494 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1498 case '+': f1
+= f2
; break;
1499 case '-': f1
-= f2
; break;
1500 case '*': f1
*= f2
; break;
1504 tcc_error("division by zero in constant");
1509 /* XXX: also handles tests ? */
1513 /* XXX: overflow test ? */
1514 if (v1
->type
.t
== VT_FLOAT
) {
1516 } else if (v1
->type
.t
== VT_DOUBLE
) {
1524 if (!nocode_wanted
) {
1532 static int pointed_size(CType
*type
)
1535 return type_size(pointed_type(type
), &align
);
1538 static void vla_runtime_pointed_size(CType
*type
)
1541 vla_runtime_type_size(pointed_type(type
), &align
);
1544 static inline int is_null_pointer(SValue
*p
)
1546 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1548 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& p
->c
.i
== 0) ||
1549 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.ll
== 0) ||
1550 ((p
->type
.t
& VT_BTYPE
) == VT_PTR
&& p
->c
.ptr
== 0);
1553 static inline int is_integer_btype(int bt
)
1555 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
1556 bt
== VT_INT
|| bt
== VT_LLONG
);
1559 /* check types for comparison or substraction of pointers */
1560 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
1562 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
1565 /* null pointers are accepted for all comparisons as gcc */
1566 if (is_null_pointer(p1
) || is_null_pointer(p2
))
1570 bt1
= type1
->t
& VT_BTYPE
;
1571 bt2
= type2
->t
& VT_BTYPE
;
1572 /* accept comparison between pointer and integer with a warning */
1573 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
1574 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
1575 tcc_warning("comparison between pointer and integer");
1579 /* both must be pointers or implicit function pointers */
1580 if (bt1
== VT_PTR
) {
1581 type1
= pointed_type(type1
);
1582 } else if (bt1
!= VT_FUNC
)
1583 goto invalid_operands
;
1585 if (bt2
== VT_PTR
) {
1586 type2
= pointed_type(type2
);
1587 } else if (bt2
!= VT_FUNC
) {
1589 tcc_error("invalid operands to binary %s", get_tok_str(op
, NULL
));
1591 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1592 (type2
->t
& VT_BTYPE
) == VT_VOID
)
1596 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1597 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1598 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1599 /* gcc-like error if '-' is used */
1601 goto invalid_operands
;
1603 tcc_warning("comparison of distinct pointer types lacks a cast");
1607 /* generic gen_op: handles types problems */
1608 ST_FUNC
void gen_op(int op
)
1610 int u
, t1
, t2
, bt1
, bt2
, t
;
1613 t1
= vtop
[-1].type
.t
;
1614 t2
= vtop
[0].type
.t
;
1615 bt1
= t1
& VT_BTYPE
;
1616 bt2
= t2
& VT_BTYPE
;
1618 if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
1619 /* at least one operand is a pointer */
1620 /* relationnal op: must be both pointers */
1621 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
1622 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1623 /* pointers are handled are unsigned */
1624 #ifdef TCC_TARGET_X86_64
1625 t
= VT_LLONG
| VT_UNSIGNED
;
1627 t
= VT_INT
| VT_UNSIGNED
;
1631 /* if both pointers, then it must be the '-' op */
1632 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
1634 tcc_error("cannot use pointers here");
1635 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1636 /* XXX: check that types are compatible */
1637 if (vtop
[-1].type
.t
& VT_VLA
) {
1638 vla_runtime_pointed_size(&vtop
[-1].type
);
1640 vpushi(pointed_size(&vtop
[-1].type
));
1644 /* set to integer type */
1645 #ifdef TCC_TARGET_X86_64
1646 vtop
->type
.t
= VT_LLONG
;
1648 vtop
->type
.t
= VT_INT
;
1653 /* exactly one pointer : must be '+' or '-'. */
1654 if (op
!= '-' && op
!= '+')
1655 tcc_error("cannot use pointers here");
1656 /* Put pointer as first operand */
1657 if (bt2
== VT_PTR
) {
1661 type1
= vtop
[-1].type
;
1662 type1
.t
&= ~VT_ARRAY
;
1663 if (vtop
[-1].type
.t
& VT_VLA
)
1664 vla_runtime_pointed_size(&vtop
[-1].type
);
1666 u
= pointed_size(&vtop
[-1].type
);
1668 tcc_error("unknown array element size");
1669 #ifdef TCC_TARGET_X86_64
1672 /* XXX: cast to int ? (long long case) */
1677 #ifdef CONFIG_TCC_BCHECK
1678 /* if evaluating constant expression, no code should be
1679 generated, so no bound check */
1680 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
1681 /* if bounded pointers, we generate a special code to
1688 gen_bounded_ptr_add();
1694 /* put again type if gen_opic() swaped operands */
1697 } else if (is_float(bt1
) || is_float(bt2
)) {
1698 /* compute bigger type and do implicit casts */
1699 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
1701 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
1706 /* floats can only be used for a few operations */
1707 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
1708 (op
< TOK_ULT
|| op
> TOK_GT
))
1709 tcc_error("invalid operands for binary operation");
1711 } else if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
) {
1712 t
= bt1
== VT_LLONG
? VT_LLONG
: VT_INT
;
1713 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (t
| VT_UNSIGNED
))
1716 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
1717 /* cast to biggest op */
1719 /* convert to unsigned if it does not fit in a long long */
1720 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
1721 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
1724 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
1725 tcc_error("comparison of struct");
1727 /* integer operations */
1729 /* convert to unsigned if it does not fit in an integer */
1730 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
1731 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
1734 /* XXX: currently, some unsigned operations are explicit, so
1735 we modify them here */
1736 if (t
& VT_UNSIGNED
) {
1743 else if (op
== TOK_LT
)
1745 else if (op
== TOK_GT
)
1747 else if (op
== TOK_LE
)
1749 else if (op
== TOK_GE
)
1756 /* special case for shifts and long long: we keep the shift as
1758 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
1765 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
1766 /* relationnal op: the result is an int */
1767 vtop
->type
.t
= VT_INT
;
1774 #ifndef TCC_TARGET_ARM
1775 /* generic itof for unsigned long long case */
1776 static void gen_cvt_itof1(int t
)
1778 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
1779 (VT_LLONG
| VT_UNSIGNED
)) {
1782 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
1783 #if LDOUBLE_SIZE != 8
1784 else if (t
== VT_LDOUBLE
)
1785 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
1788 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
1792 vtop
->r
= reg_fret(t
);
1799 /* generic ftoi for unsigned long long case */
1800 static void gen_cvt_ftoi1(int t
)
1804 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
1805 /* not handled natively */
1806 st
= vtop
->type
.t
& VT_BTYPE
;
1808 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
1809 #if LDOUBLE_SIZE != 8
1810 else if (st
== VT_LDOUBLE
)
1811 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
1814 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
1819 vtop
->r2
= REG_LRET
;
1825 /* force char or short cast */
1826 static void force_charshort_cast(int t
)
1830 /* XXX: add optimization if lvalue : just change type and offset */
1835 if (t
& VT_UNSIGNED
) {
1836 vpushi((1 << bits
) - 1);
1842 /* result must be signed or the SAR is converted to an SHL
1843 This was not the case when "t" was a signed short
1844 and the last value on the stack was an unsigned int */
1845 vtop
->type
.t
&= ~VT_UNSIGNED
;
1851 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1852 static void gen_cast(CType
*type
)
1854 int sbt
, dbt
, sf
, df
, c
, p
;
1856 /* special delayed cast for char/short */
1857 /* XXX: in some cases (multiple cascaded casts), it may still
1859 if (vtop
->r
& VT_MUSTCAST
) {
1860 vtop
->r
&= ~VT_MUSTCAST
;
1861 force_charshort_cast(vtop
->type
.t
);
1864 /* bitfields first get cast to ints */
1865 if (vtop
->type
.t
& VT_BITFIELD
) {
1869 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
1870 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
1875 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1876 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
1878 /* constant case: we can do it now */
1879 /* XXX: in ISOC, cannot do it if error in convert */
1880 if (sbt
== VT_FLOAT
)
1881 vtop
->c
.ld
= vtop
->c
.f
;
1882 else if (sbt
== VT_DOUBLE
)
1883 vtop
->c
.ld
= vtop
->c
.d
;
1886 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
1887 if (sbt
& VT_UNSIGNED
)
1888 vtop
->c
.ld
= vtop
->c
.ull
;
1890 vtop
->c
.ld
= vtop
->c
.ll
;
1892 if (sbt
& VT_UNSIGNED
)
1893 vtop
->c
.ld
= vtop
->c
.ui
;
1895 vtop
->c
.ld
= vtop
->c
.i
;
1898 if (dbt
== VT_FLOAT
)
1899 vtop
->c
.f
= (float)vtop
->c
.ld
;
1900 else if (dbt
== VT_DOUBLE
)
1901 vtop
->c
.d
= (double)vtop
->c
.ld
;
1902 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
1903 vtop
->c
.ull
= (unsigned long long)vtop
->c
.ld
;
1904 } else if (sf
&& dbt
== VT_BOOL
) {
1905 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
1908 vtop
->c
.ll
= (long long)vtop
->c
.ld
;
1909 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
1910 vtop
->c
.ll
= vtop
->c
.ull
;
1911 else if (sbt
& VT_UNSIGNED
)
1912 vtop
->c
.ll
= vtop
->c
.ui
;
1913 #ifdef TCC_TARGET_X86_64
1914 else if (sbt
== VT_PTR
)
1917 else if (sbt
!= VT_LLONG
)
1918 vtop
->c
.ll
= vtop
->c
.i
;
1920 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
1921 vtop
->c
.ull
= vtop
->c
.ll
;
1922 else if (dbt
== VT_BOOL
)
1923 vtop
->c
.i
= (vtop
->c
.ll
!= 0);
1924 else if (dbt
!= VT_LLONG
) {
1926 if ((dbt
& VT_BTYPE
) == VT_BYTE
)
1928 else if ((dbt
& VT_BTYPE
) == VT_SHORT
)
1931 if(dbt
& VT_UNSIGNED
)
1932 vtop
->c
.ui
= ((unsigned int)vtop
->c
.ll
<< s
) >> s
;
1934 vtop
->c
.i
= ((int)vtop
->c
.ll
<< s
) >> s
;
1937 } else if (p
&& dbt
== VT_BOOL
) {
1940 } else if (!nocode_wanted
) {
1941 /* non constant case: generate code */
1943 /* convert from fp to fp */
1946 /* convert int to fp */
1949 /* convert fp to int */
1950 if (dbt
== VT_BOOL
) {
1954 /* we handle char/short/etc... with generic code */
1955 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
1956 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
1960 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
1961 /* additional cast for char/short... */
1966 #ifndef TCC_TARGET_X86_64
1967 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
1968 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
1969 /* scalar to long long */
1970 /* machine independent conversion */
1972 /* generate high word */
1973 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
1977 if (sbt
== VT_PTR
) {
1978 /* cast from pointer to int before we apply
1979 shift operation, which pointers don't support*/
1980 gen_cast(&int_type
);
1986 /* patch second register */
1987 vtop
[-1].r2
= vtop
->r
;
1991 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
1992 (dbt
& VT_BTYPE
) == VT_PTR
||
1993 (dbt
& VT_BTYPE
) == VT_FUNC
) {
1994 if ((sbt
& VT_BTYPE
) != VT_LLONG
&&
1995 (sbt
& VT_BTYPE
) != VT_PTR
&&
1996 (sbt
& VT_BTYPE
) != VT_FUNC
) {
1997 /* need to convert from 32bit to 64bit */
1999 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
2000 /* x86_64 specific: movslq */
2002 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
2006 } else if (dbt
== VT_BOOL
) {
2007 /* scalar to bool */
2010 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
2011 (dbt
& VT_BTYPE
) == VT_SHORT
) {
2012 if (sbt
== VT_PTR
) {
2013 vtop
->type
.t
= VT_INT
;
2014 tcc_warning("nonportable conversion from pointer to char/short");
2016 force_charshort_cast(dbt
);
2017 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
2019 if (sbt
== VT_LLONG
) {
2020 /* from long long: just take low order word */
2024 /* if lvalue and single word type, nothing to do because
2025 the lvalue already contains the real type size (see
2026 VT_LVAL_xxx constants) */
2029 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
2030 /* if we are casting between pointer types,
2031 we must update the VT_LVAL_xxx size */
2032 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
2033 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
2038 /* return type size as known at compile time. Put alignment at 'a' */
2039 ST_FUNC
int type_size(CType
*type
, int *a
)
2044 bt
= type
->t
& VT_BTYPE
;
2045 if (bt
== VT_STRUCT
) {
2050 } else if (bt
== VT_PTR
) {
2051 if (type
->t
& VT_ARRAY
) {
2055 ts
= type_size(&s
->type
, a
);
2057 if (ts
< 0 && s
->c
< 0)
2065 } else if (bt
== VT_LDOUBLE
) {
2067 return LDOUBLE_SIZE
;
2068 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
2069 #ifdef TCC_TARGET_I386
2070 #ifdef TCC_TARGET_PE
2075 #elif defined(TCC_TARGET_ARM)
2085 } else if (bt
== VT_INT
|| bt
== VT_ENUM
|| bt
== VT_FLOAT
) {
2088 } else if (bt
== VT_SHORT
) {
2091 } else if (bt
== VT_QLONG
|| bt
== VT_QFLOAT
) {
2095 /* char, void, function, _Bool */
2101 /* push type size as known at runtime time on top of value stack. Put
2103 ST_FUNC
void vla_runtime_type_size(CType
*type
, int *a
)
2105 if (type
->t
& VT_VLA
) {
2106 vset(&int_type
, VT_LOCAL
|VT_LVAL
, type
->ref
->c
);
2108 vpushi(type_size(type
, a
));
2112 /* return the pointed type of t */
2113 static inline CType
*pointed_type(CType
*type
)
2115 return &type
->ref
->type
;
2118 /* modify type so that its it is a pointer to type. */
2119 ST_FUNC
void mk_pointer(CType
*type
)
2122 s
= sym_push(SYM_FIELD
, type
, 0, -1);
2123 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
2127 /* compare function types. OLD functions match any new functions */
2128 static int is_compatible_func(CType
*type1
, CType
*type2
)
2134 if (!is_compatible_types(&s1
->type
, &s2
->type
))
2136 /* check func_call */
2137 if (FUNC_CALL(s1
->r
) != FUNC_CALL(s2
->r
))
2139 /* XXX: not complete */
2140 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
2144 while (s1
!= NULL
) {
2147 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
2157 /* return true if type1 and type2 are the same. If unqualified is
2158 true, qualifiers on the types are ignored.
2160 - enums are not checked as gcc __builtin_types_compatible_p ()
2162 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2166 t1
= type1
->t
& VT_TYPE
;
2167 t2
= type2
->t
& VT_TYPE
;
2169 /* strip qualifiers before comparing */
2170 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2171 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2173 /* XXX: bitfields ? */
2176 /* test more complicated cases */
2177 bt1
= t1
& VT_BTYPE
;
2178 if (bt1
== VT_PTR
) {
2179 type1
= pointed_type(type1
);
2180 type2
= pointed_type(type2
);
2181 return is_compatible_types(type1
, type2
);
2182 } else if (bt1
== VT_STRUCT
) {
2183 return (type1
->ref
== type2
->ref
);
2184 } else if (bt1
== VT_FUNC
) {
2185 return is_compatible_func(type1
, type2
);
2191 /* return true if type1 and type2 are exactly the same (including
2194 static int is_compatible_types(CType
*type1
, CType
*type2
)
2196 return compare_types(type1
,type2
,0);
2199 /* return true if type1 and type2 are the same (ignoring qualifiers).
2201 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
2203 return compare_types(type1
,type2
,1);
2206 /* print a type. If 'varstr' is not NULL, then the variable is also
2207 printed in the type */
2209 /* XXX: add array and function pointers */
2210 static void type_to_str(char *buf
, int buf_size
,
2211 CType
*type
, const char *varstr
)
2218 t
= type
->t
& VT_TYPE
;
2221 if (t
& VT_CONSTANT
)
2222 pstrcat(buf
, buf_size
, "const ");
2223 if (t
& VT_VOLATILE
)
2224 pstrcat(buf
, buf_size
, "volatile ");
2225 if (t
& VT_UNSIGNED
)
2226 pstrcat(buf
, buf_size
, "unsigned ");
2256 tstr
= "long double";
2258 pstrcat(buf
, buf_size
, tstr
);
2262 if (bt
== VT_STRUCT
)
2266 pstrcat(buf
, buf_size
, tstr
);
2267 v
= type
->ref
->v
& ~SYM_STRUCT
;
2268 if (v
>= SYM_FIRST_ANOM
)
2269 pstrcat(buf
, buf_size
, "<anonymous>");
2271 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
2275 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
2276 pstrcat(buf
, buf_size
, "(");
2278 while (sa
!= NULL
) {
2279 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
2280 pstrcat(buf
, buf_size
, buf1
);
2283 pstrcat(buf
, buf_size
, ", ");
2285 pstrcat(buf
, buf_size
, ")");
2289 pstrcpy(buf1
, sizeof(buf1
), "*");
2291 pstrcat(buf1
, sizeof(buf1
), varstr
);
2292 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2296 pstrcat(buf
, buf_size
, " ");
2297 pstrcat(buf
, buf_size
, varstr
);
2302 /* verify type compatibility to store vtop in 'dt' type, and generate
2304 static void gen_assign_cast(CType
*dt
)
2306 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
2307 char buf1
[256], buf2
[256];
2310 st
= &vtop
->type
; /* source type */
2311 dbt
= dt
->t
& VT_BTYPE
;
2312 sbt
= st
->t
& VT_BTYPE
;
2314 tcc_error("Cannot assign void value");
2315 if (dt
->t
& VT_CONSTANT
)
2316 tcc_warning("assignment of read-only location");
2319 /* special cases for pointers */
2320 /* '0' can also be a pointer */
2321 if (is_null_pointer(vtop
))
2323 /* accept implicit pointer to integer cast with warning */
2324 if (is_integer_btype(sbt
)) {
2325 tcc_warning("assignment makes pointer from integer without a cast");
2328 type1
= pointed_type(dt
);
2329 /* a function is implicitely a function pointer */
2330 if (sbt
== VT_FUNC
) {
2331 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
2332 !is_compatible_types(pointed_type(dt
), st
))
2333 tcc_warning("assignment from incompatible pointer type");
2338 type2
= pointed_type(st
);
2339 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2340 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
2341 /* void * can match anything */
2343 /* exact type match, except for unsigned */
2346 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2347 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2348 if (!is_compatible_types(&tmp_type1
, &tmp_type2
))
2349 tcc_warning("assignment from incompatible pointer type");
2351 /* check const and volatile */
2352 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
2353 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
2354 tcc_warning("assignment discards qualifiers from pointer target type");
2360 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
2361 tcc_warning("assignment makes integer from pointer without a cast");
2363 /* XXX: more tests */
2368 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2369 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2370 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2372 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
2373 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
2374 tcc_error("cannot cast '%s' to '%s'", buf1
, buf2
);
2382 /* store vtop in lvalue pushed on stack */
2383 ST_FUNC
void vstore(void)
2385 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
2387 ft
= vtop
[-1].type
.t
;
2388 sbt
= vtop
->type
.t
& VT_BTYPE
;
2389 dbt
= ft
& VT_BTYPE
;
2390 if ((((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2391 (sbt
== VT_INT
&& dbt
== VT_SHORT
))
2392 && !(vtop
->type
.t
& VT_BITFIELD
)) {
2393 /* optimize char/short casts */
2394 delayed_cast
= VT_MUSTCAST
;
2395 vtop
->type
.t
= ft
& (VT_TYPE
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
)));
2396 /* XXX: factorize */
2397 if (ft
& VT_CONSTANT
)
2398 tcc_warning("assignment of read-only location");
2401 if (!(ft
& VT_BITFIELD
))
2402 gen_assign_cast(&vtop
[-1].type
);
2405 if (sbt
== VT_STRUCT
) {
2406 /* if structure, only generate pointer */
2407 /* structure assignment : generate memcpy */
2408 /* XXX: optimize if small size */
2409 if (!nocode_wanted
) {
2410 size
= type_size(&vtop
->type
, &align
);
2414 vtop
->type
.t
= VT_PTR
;
2417 /* address of memcpy() */
2420 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2421 else if(!(align
& 3))
2422 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2425 vpush_global_sym(&func_old_type
, TOK_memcpy
);
2430 vtop
->type
.t
= VT_PTR
;
2439 /* leave source on stack */
2440 } else if (ft
& VT_BITFIELD
) {
2441 /* bitfield store handling */
2442 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2443 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2444 /* remove bit field info to avoid loops */
2445 vtop
[-1].type
.t
= ft
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
2447 /* duplicate source into other register */
2452 if((ft
& VT_BTYPE
) == VT_BOOL
) {
2453 gen_cast(&vtop
[-1].type
);
2454 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2457 /* duplicate destination */
2459 vtop
[-1] = vtop
[-2];
2461 /* mask and shift source */
2462 if((ft
& VT_BTYPE
) != VT_BOOL
) {
2463 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2464 vpushll((1ULL << bit_size
) - 1ULL);
2466 vpushi((1 << bit_size
) - 1);
2472 /* load destination, mask and or with source */
2474 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2475 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2477 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2484 /* pop off shifted source from "duplicate source..." above */
2488 #ifdef CONFIG_TCC_BCHECK
2489 /* bound check case */
2490 if (vtop
[-1].r
& VT_MUSTBOUND
) {
2496 if (!nocode_wanted
) {
2500 #ifdef TCC_TARGET_X86_64
2501 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
2503 } else if ((ft
& VT_BTYPE
) == VT_QFLOAT
) {
2508 r
= gv(rc
); /* generate value */
2509 /* if lvalue was saved on stack, must read it */
2510 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
2512 t
= get_reg(RC_INT
);
2513 #ifdef TCC_TARGET_X86_64
2518 sv
.r
= VT_LOCAL
| VT_LVAL
;
2519 sv
.c
.ul
= vtop
[-1].c
.ul
;
2521 vtop
[-1].r
= t
| VT_LVAL
;
2523 /* two word case handling : store second register at word + 4 (or +8 for x86-64) */
2524 #ifdef TCC_TARGET_X86_64
2525 if (((ft
& VT_BTYPE
) == VT_QLONG
) || ((ft
& VT_BTYPE
) == VT_QFLOAT
)) {
2526 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
2528 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
2529 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
2531 vtop
[-1].type
.t
= load_type
;
2534 /* convert to int to increment easily */
2535 vtop
->type
.t
= addr_type
;
2541 vtop
[-1].type
.t
= load_type
;
2542 /* XXX: it works because r2 is spilled last ! */
2543 store(vtop
->r2
, vtop
- 1);
2549 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
2550 vtop
->r
|= delayed_cast
;
2554 /* post defines POST/PRE add. c is the token ++ or -- */
2555 ST_FUNC
void inc(int post
, int c
)
2558 vdup(); /* save lvalue */
2560 gv_dup(); /* duplicate value */
2565 vpushi(c
- TOK_MID
);
2567 vstore(); /* store value */
2569 vpop(); /* if post op, return saved value */
2572 /* Parse GNUC __attribute__ extension. Currently, the following
2573 extensions are recognized:
2574 - aligned(n) : set data/function alignment.
2575 - packed : force data alignment to 1
2576 - section(x) : generate data/code in this section.
2577 - unused : currently ignored, but may be used someday.
2578 - regparm(n) : pass function parameters in registers (i386 only)
2580 static void parse_attribute(AttributeDef
*ad
)
2584 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
2588 while (tok
!= ')') {
2589 if (tok
< TOK_IDENT
)
2590 expect("attribute name");
2598 expect("section name");
2599 ad
->section
= find_section(tcc_state
, (char *)tokc
.cstr
->data
);
2607 expect("alias(\"target\")");
2608 ad
->alias_target
= /* save string as token, for later */
2609 tok_alloc((char*)tokc
.cstr
->data
, tokc
.cstr
->size
-1)->tok
;
2618 if (n
<= 0 || (n
& (n
- 1)) != 0)
2619 tcc_error("alignment must be a positive power of two");
2636 /* currently, no need to handle it because tcc does not
2637 track unused objects */
2641 /* currently, no need to handle it because tcc does not
2642 track unused objects */
2647 ad
->func_call
= FUNC_CDECL
;
2652 ad
->func_call
= FUNC_STDCALL
;
2654 #ifdef TCC_TARGET_I386
2664 ad
->func_call
= FUNC_FASTCALL1
+ n
- 1;
2670 ad
->func_call
= FUNC_FASTCALLW
;
2677 ad
->mode
= VT_LLONG
+ 1;
2680 ad
->mode
= VT_SHORT
+ 1;
2683 ad
->mode
= VT_INT
+ 1;
2686 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
2693 ad
->func_export
= 1;
2696 ad
->func_import
= 1;
2699 if (tcc_state
->warn_unsupported
)
2700 tcc_warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
2701 /* skip parameters */
2703 int parenthesis
= 0;
2707 else if (tok
== ')')
2710 } while (parenthesis
&& tok
!= -1);
2723 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2724 static void struct_decl(CType
*type
, int u
)
2726 int a
, v
, size
, align
, maxalign
, c
, offset
;
2727 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
, prevbt
;
2728 Sym
*s
, *ss
, *ass
, **ps
;
2732 a
= tok
; /* save decl type */
2737 /* struct already defined ? return it */
2739 expect("struct/union/enum name");
2743 tcc_error("invalid type");
2750 /* we put an undefined size for struct/union */
2751 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
2752 s
->r
= 0; /* default alignment is zero as gcc */
2753 /* put struct/union/enum name in type */
2761 tcc_error("struct/union/enum already defined");
2762 /* cannot be empty */
2764 /* non empty enums are not allowed */
2765 if (a
== TOK_ENUM
) {
2769 expect("identifier");
2775 /* enum symbols have static storage */
2776 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
2777 ss
->type
.t
|= VT_STATIC
;
2782 /* NOTE: we accept a trailing comma */
2793 while (tok
!= '}') {
2794 parse_btype(&btype
, &ad
);
2800 type_decl(&type1
, &ad
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
2801 if (v
== 0 && (type1
.t
& VT_BTYPE
) != VT_STRUCT
)
2802 expect("identifier");
2803 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
2804 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
2805 tcc_error("invalid type for '%s'",
2806 get_tok_str(v
, NULL
));
2810 bit_size
= expr_const();
2811 /* XXX: handle v = 0 case for messages */
2813 tcc_error("negative width in bit-field '%s'",
2814 get_tok_str(v
, NULL
));
2815 if (v
&& bit_size
== 0)
2816 tcc_error("zero width for bit-field '%s'",
2817 get_tok_str(v
, NULL
));
2819 size
= type_size(&type1
, &align
);
2821 if (align
< ad
.aligned
)
2823 } else if (ad
.packed
) {
2825 } else if (*tcc_state
->pack_stack_ptr
) {
2826 if (align
> *tcc_state
->pack_stack_ptr
)
2827 align
= *tcc_state
->pack_stack_ptr
;
2830 if (bit_size
>= 0) {
2831 bt
= type1
.t
& VT_BTYPE
;
2838 tcc_error("bitfields must have scalar type");
2840 if (bit_size
> bsize
) {
2841 tcc_error("width of '%s' exceeds its type",
2842 get_tok_str(v
, NULL
));
2843 } else if (bit_size
== bsize
) {
2844 /* no need for bit fields */
2846 } else if (bit_size
== 0) {
2847 /* XXX: what to do if only padding in a
2849 /* zero size: means to pad */
2852 /* we do not have enough room ?
2853 did the type change?
2855 if ((bit_pos
+ bit_size
) > bsize
||
2856 bt
!= prevbt
|| a
== TOK_UNION
)
2859 /* XXX: handle LSB first */
2860 type1
.t
|= VT_BITFIELD
|
2861 (bit_pos
<< VT_STRUCT_SHIFT
) |
2862 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
2863 bit_pos
+= bit_size
;
2869 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2870 /* add new memory data only if starting
2872 if (lbit_pos
== 0) {
2873 if (a
== TOK_STRUCT
) {
2874 c
= (c
+ align
- 1) & -align
;
2883 if (align
> maxalign
)
2887 printf("add field %s offset=%d",
2888 get_tok_str(v
, NULL
), offset
);
2889 if (type1
.t
& VT_BITFIELD
) {
2890 printf(" pos=%d size=%d",
2891 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
2892 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
2897 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2899 while ((ass
= ass
->next
) != NULL
) {
2900 ss
= sym_push(ass
->v
, &ass
->type
, 0, offset
+ ass
->c
);
2905 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
2909 if (tok
== ';' || tok
== TOK_EOF
)
2916 /* store size and alignment */
2917 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
2923 /* return 0 if no type declaration. otherwise, return the basic type
2926 static int parse_btype(CType
*type
, AttributeDef
*ad
)
2928 int t
, u
, type_found
, typespec_found
, typedef_found
;
2932 memset(ad
, 0, sizeof(AttributeDef
));
2940 /* currently, we really ignore extension */
2950 if ((t
& VT_BTYPE
) != 0)
2951 tcc_error("too many basic types");
2967 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
2968 #ifndef TCC_TARGET_PE
2969 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
2971 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
2972 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
2986 if ((t
& VT_BTYPE
) == VT_LONG
) {
2987 #ifdef TCC_TARGET_PE
2988 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
2990 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
2998 struct_decl(&type1
, VT_ENUM
);
3001 type
->ref
= type1
.ref
;
3005 struct_decl(&type1
, VT_STRUCT
);
3008 /* type modifiers */
3061 /* GNUC attribute */
3062 case TOK_ATTRIBUTE1
:
3063 case TOK_ATTRIBUTE2
:
3064 parse_attribute(ad
);
3067 t
= (t
& ~VT_BTYPE
) | u
;
3075 parse_expr_type(&type1
);
3076 /* remove all storage modifiers except typedef */
3077 type1
.t
&= ~(VT_STORAGE
&~VT_TYPEDEF
);
3080 if (typespec_found
|| typedef_found
)
3083 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
3086 t
|= (s
->type
.t
& ~VT_TYPEDEF
);
3087 type
->ref
= s
->type
.ref
;
3089 /* get attributes from typedef */
3090 if (0 == ad
->aligned
)
3091 ad
->aligned
= FUNC_ALIGN(s
->r
);
3092 if (0 == ad
->func_call
)
3093 ad
->func_call
= FUNC_CALL(s
->r
);
3094 ad
->packed
|= FUNC_PACKED(s
->r
);
3103 if ((t
& (VT_SIGNED
|VT_UNSIGNED
)) == (VT_SIGNED
|VT_UNSIGNED
))
3104 tcc_error("signed and unsigned modifier");
3105 if (tcc_state
->char_is_unsigned
) {
3106 if ((t
& (VT_SIGNED
|VT_UNSIGNED
|VT_BTYPE
)) == VT_BYTE
)
3111 /* long is never used as type */
3112 if ((t
& VT_BTYPE
) == VT_LONG
)
3113 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
3114 t
= (t
& ~VT_BTYPE
) | VT_INT
;
3116 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3122 /* convert a function parameter type (array to pointer and function to
3123 function pointer) */
3124 static inline void convert_parameter_type(CType
*pt
)
3126 /* remove const and volatile qualifiers (XXX: const could be used
3127 to indicate a const function parameter */
3128 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3129 /* array must be transformed to pointer according to ANSI C */
3131 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
3136 ST_FUNC
void parse_asm_str(CString
*astr
)
3139 /* read the string */
3141 expect("string constant");
3143 while (tok
== TOK_STR
) {
3144 /* XXX: add \0 handling too ? */
3145 cstr_cat(astr
, tokc
.cstr
->data
);
3148 cstr_ccat(astr
, '\0');
3151 /* Parse an asm label and return the label
3152 * Don't forget to free the CString in the caller! */
3153 static void asm_label_instr(CString
*astr
)
3156 parse_asm_str(astr
);
3159 printf("asm_alias: \"%s\"\n", (char *)astr
->data
);
3163 static void post_type(CType
*type
, AttributeDef
*ad
)
3165 int n
, l
, t1
, arg_size
, align
;
3166 Sym
**plast
, *s
, *first
;
3171 /* function declaration */
3179 /* read param name and compute offset */
3180 if (l
!= FUNC_OLD
) {
3181 if (!parse_btype(&pt
, &ad1
)) {
3183 tcc_error("invalid type");
3190 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
3192 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3193 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
3194 tcc_error("parameter declared as void");
3195 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
3200 expect("identifier");
3204 convert_parameter_type(&pt
);
3205 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
3211 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
3218 /* if no parameters, then old type prototype */
3222 /* NOTE: const is ignored in returned type as it has a special
3223 meaning in gcc / C++ */
3224 type
->t
&= ~VT_CONSTANT
;
3225 /* some ancient pre-K&R C allows a function to return an array
3226 and the array brackets to be put after the arguments, such
3227 that "int c()[]" means something like "int[] c()" */
3230 skip(']'); /* only handle simple "[]" */
3233 /* we push a anonymous symbol which will contain the function prototype */
3234 ad
->func_args
= arg_size
;
3235 s
= sym_push(SYM_FIELD
, type
, INT_ATTR(ad
), l
);
3239 } else if (tok
== '[') {
3240 /* array definition */
3242 if (tok
== TOK_RESTRICT1
)
3247 if (!local_stack
|| nocode_wanted
)
3248 vpushi(expr_const());
3250 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3253 tcc_error("invalid array size");
3255 if (!is_integer_btype(vtop
->type
.t
& VT_BTYPE
))
3256 tcc_error("size of variable length array should be an integer");
3261 /* parse next post type */
3262 post_type(type
, ad
);
3263 t1
|= type
->t
& VT_VLA
;
3266 loc
-= type_size(&int_type
, &align
);
3270 vla_runtime_type_size(type
, &align
);
3272 vset(&int_type
, VT_LOCAL
|VT_LVAL
, loc
);
3279 /* we push an anonymous symbol which will contain the array
3281 s
= sym_push(SYM_FIELD
, type
, 0, n
);
3282 type
->t
= (t1
? VT_VLA
: VT_ARRAY
) | VT_PTR
;
3287 /* Parse a type declaration (except basic type), and return the type
3288 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3289 expected. 'type' should contain the basic type. 'ad' is the
3290 attribute definition of the basic type. It can be modified by
3293 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
3296 CType type1
, *type2
;
3297 int qualifiers
, storage
;
3299 while (tok
== '*') {
3307 qualifiers
|= VT_CONSTANT
;
3312 qualifiers
|= VT_VOLATILE
;
3320 type
->t
|= qualifiers
;
3323 /* XXX: clarify attribute handling */
3324 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3325 parse_attribute(ad
);
3327 /* recursive type */
3328 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3329 type1
.t
= 0; /* XXX: same as int */
3332 /* XXX: this is not correct to modify 'ad' at this point, but
3333 the syntax is not clear */
3334 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3335 parse_attribute(ad
);
3336 type_decl(&type1
, ad
, v
, td
);
3339 /* type identifier */
3340 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
3344 if (!(td
& TYPE_ABSTRACT
))
3345 expect("identifier");
3349 storage
= type
->t
& VT_STORAGE
;
3350 type
->t
&= ~VT_STORAGE
;
3351 if (storage
& VT_STATIC
) {
3352 int saved_nocode_wanted
= nocode_wanted
;
3354 post_type(type
, ad
);
3355 nocode_wanted
= saved_nocode_wanted
;
3357 post_type(type
, ad
);
3359 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3360 parse_attribute(ad
);
3364 /* append type at the end of type1 */
3377 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3378 ST_FUNC
int lvalue_type(int t
)
3383 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
3385 else if (bt
== VT_SHORT
)
3389 if (t
& VT_UNSIGNED
)
3390 r
|= VT_LVAL_UNSIGNED
;
3394 /* indirection with full error checking and bound check */
3395 ST_FUNC
void indir(void)
3397 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
3398 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
3402 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
3404 vtop
->type
= *pointed_type(&vtop
->type
);
3405 /* Arrays and functions are never lvalues */
3406 if (!(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_VLA
)
3407 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3408 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3409 /* if bound checking, the referenced pointer must be checked */
3410 #ifdef CONFIG_TCC_BCHECK
3411 if (tcc_state
->do_bounds_check
)
3412 vtop
->r
|= VT_MUSTBOUND
;
3417 /* pass a parameter to a function and do type checking and casting */
3418 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
3423 func_type
= func
->c
;
3424 if (func_type
== FUNC_OLD
||
3425 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
3426 /* default casting : only need to convert float to double */
3427 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
3431 } else if (arg
== NULL
) {
3432 tcc_error("too many arguments to function");
3435 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
3436 gen_assign_cast(&type
);
3440 /* parse an expression of the form '(type)' or '(expr)' and return its
3442 static void parse_expr_type(CType
*type
)
3448 if (parse_btype(type
, &ad
)) {
3449 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3456 static void parse_type(CType
*type
)
3461 if (!parse_btype(type
, &ad
)) {
3464 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3467 static void vpush_tokc(int t
)
3472 vsetc(&type
, VT_CONST
, &tokc
);
3475 ST_FUNC
void unary(void)
3477 int n
, t
, align
, size
, r
, sizeof_caller
;
3481 static int in_sizeof
= 0;
3483 sizeof_caller
= in_sizeof
;
3485 /* XXX: GCC 2.95.3 does not generate a table although it should be
3499 vpush_tokc(VT_INT
| VT_UNSIGNED
);
3503 vpush_tokc(VT_LLONG
);
3507 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
3511 vpush_tokc(VT_FLOAT
);
3515 vpush_tokc(VT_DOUBLE
);
3519 vpush_tokc(VT_LDOUBLE
);
3522 case TOK___FUNCTION__
:
3524 goto tok_identifier
;
3530 /* special function name identifier */
3531 len
= strlen(funcname
) + 1;
3532 /* generate char[len] type */
3537 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
3538 ptr
= section_ptr_add(data_section
, len
);
3539 memcpy(ptr
, funcname
, len
);
3544 #ifdef TCC_TARGET_PE
3545 t
= VT_SHORT
| VT_UNSIGNED
;
3551 /* string parsing */
3554 if (tcc_state
->warn_write_strings
)
3559 memset(&ad
, 0, sizeof(AttributeDef
));
3560 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, NULL
, 0);
3565 if (parse_btype(&type
, &ad
)) {
3566 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
3568 /* check ISOC99 compound literal */
3570 /* data is allocated locally by default */
3575 /* all except arrays are lvalues */
3576 if (!(type
.t
& VT_ARRAY
))
3577 r
|= lvalue_type(type
.t
);
3578 memset(&ad
, 0, sizeof(AttributeDef
));
3579 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, NULL
, 0);
3581 if (sizeof_caller
) {
3588 } else if (tok
== '{') {
3589 /* save all registers */
3591 /* statement expression : we do not accept break/continue
3592 inside as GCC does */
3593 block(NULL
, NULL
, NULL
, NULL
, 0, 1);
3608 /* functions names must be treated as function pointers,
3609 except for unary '&' and sizeof. Since we consider that
3610 functions are not lvalues, we only have to handle it
3611 there and in function calls. */
3612 /* arrays can also be used although they are not lvalues */
3613 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
3614 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
3616 mk_pointer(&vtop
->type
);
3622 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3624 boolean
.t
= VT_BOOL
;
3626 vtop
->c
.i
= !vtop
->c
.i
;
3627 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
3628 vtop
->c
.i
= vtop
->c
.i
^ 1;
3631 vseti(VT_JMP
, gtst(1, 0));
3642 /* in order to force cast, we add zero */
3644 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
3645 tcc_error("pointer not accepted for unary plus");
3655 unary_type(&type
); // Perform a in_sizeof = 0;
3656 size
= type_size(&type
, &align
);
3657 if (t
== TOK_SIZEOF
) {
3658 if (!(type
.t
& VT_VLA
)) {
3660 tcc_error("sizeof applied to an incomplete type");
3663 vla_runtime_type_size(&type
, &align
);
3668 vtop
->type
.t
|= VT_UNSIGNED
;
3671 case TOK_builtin_types_compatible_p
:
3680 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3681 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3682 vpushi(is_compatible_types(&type1
, &type2
));
3685 case TOK_builtin_constant_p
:
3687 int saved_nocode_wanted
, res
;
3690 saved_nocode_wanted
= nocode_wanted
;
3693 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
3695 nocode_wanted
= saved_nocode_wanted
;
3700 case TOK_builtin_frame_address
:
3706 if (tok
!= TOK_CINT
|| tokc
.i
< 0) {
3707 tcc_error("__builtin_frame_address only takes positive integers");
3714 vset(&type
, VT_LOCAL
, 0); /* local frame */
3716 mk_pointer(&vtop
->type
);
3717 indir(); /* -> parent frame */
3721 #ifdef TCC_TARGET_X86_64
3722 #ifdef TCC_TARGET_PE
3723 case TOK_builtin_va_start
:
3731 if ((vtop
->r
& VT_VALMASK
) != VT_LOCAL
)
3732 tcc_error("__builtin_va_start expects a local variable");
3733 vtop
->r
&= ~(VT_LVAL
| VT_REF
);
3734 vtop
->type
= char_pointer_type
;
3739 case TOK_builtin_va_arg_types
:
3747 vpushi(classify_x86_64_va_arg(&type
));
3767 goto tok_identifier
;
3769 /* allow to take the address of a label */
3770 if (tok
< TOK_UIDENT
)
3771 expect("label identifier");
3772 s
= label_find(tok
);
3774 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
3776 if (s
->r
== LABEL_DECLARED
)
3777 s
->r
= LABEL_FORWARD
;
3780 s
->type
.t
= VT_VOID
;
3781 mk_pointer(&s
->type
);
3782 s
->type
.t
|= VT_STATIC
;
3784 vset(&s
->type
, VT_CONST
| VT_SYM
, 0);
3789 // special qnan , snan and infinity values
3791 vpush64(VT_DOUBLE
, 0x7ff8000000000000ULL
);
3795 vpush64(VT_DOUBLE
, 0x7ff0000000000001ULL
);
3799 vpush64(VT_DOUBLE
, 0x7ff0000000000000ULL
);
3808 expect("identifier");
3812 tcc_error("'%s' undeclared", get_tok_str(t
, NULL
));
3813 /* for simple function calls, we tolerate undeclared
3814 external reference to int() function */
3815 if (tcc_state
->warn_implicit_function_declaration
)
3816 tcc_warning("implicit declaration of function '%s'",
3817 get_tok_str(t
, NULL
));
3818 s
= external_global_sym(t
, &func_old_type
, 0);
3820 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
3821 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
3822 /* if referencing an inline function, then we generate a
3823 symbol to it if not already done. It will have the
3824 effect to generate code for it at the end of the
3825 compilation unit. Inline function as always
3826 generated in the text section. */
3828 put_extern_sym(s
, text_section
, 0, 0);
3829 r
= VT_SYM
| VT_CONST
;
3833 vset(&s
->type
, r
, s
->c
);
3834 /* if forward reference, we must point to s */
3835 if (vtop
->r
& VT_SYM
) {
3842 /* post operations */
3844 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
3847 } else if (tok
== '.' || tok
== TOK_ARROW
) {
3850 if (tok
== TOK_ARROW
)
3852 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
3856 /* expect pointer on structure */
3857 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
3858 expect("struct or union");
3862 while ((s
= s
->next
) != NULL
) {
3867 tcc_error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, NULL
));
3868 /* add field offset to pointer */
3869 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
3872 /* change type to field type, and set to lvalue */
3873 vtop
->type
= s
->type
;
3874 vtop
->type
.t
|= qualifiers
;
3875 /* an array is never an lvalue */
3876 if (!(vtop
->type
.t
& VT_ARRAY
)) {
3877 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3878 #ifdef CONFIG_TCC_BCHECK
3879 /* if bound checking, the referenced pointer must be checked */
3880 if (tcc_state
->do_bounds_check
)
3881 vtop
->r
|= VT_MUSTBOUND
;
3885 } else if (tok
== '[') {
3891 } else if (tok
== '(') {
3897 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3898 /* pointer test (no array accepted) */
3899 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
3900 vtop
->type
= *pointed_type(&vtop
->type
);
3901 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
3905 expect("function pointer");
3908 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
3910 /* get return type */
3913 sa
= s
->next
; /* first parameter */
3916 /* compute first implicit argument if a structure is returned */
3917 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
3919 sret
= gfunc_sret(&s
->type
, &ret
.type
, &ret_align
);
3921 /* get some space for the returned structure */
3922 size
= type_size(&s
->type
, &align
);
3923 loc
= (loc
- size
) & -align
;
3925 ret
.r
= VT_LOCAL
| VT_LVAL
;
3926 /* pass it as 'int' to avoid structure arg passing
3928 vseti(VT_LOCAL
, loc
);
3938 /* return in register */
3939 if (is_float(ret
.type
.t
)) {
3940 ret
.r
= reg_fret(ret
.type
.t
);
3941 #ifdef TCC_TARGET_X86_64
3942 if ((ret
.type
.t
& VT_BTYPE
) == VT_QFLOAT
)
3946 #ifdef TCC_TARGET_X86_64
3947 if ((ret
.type
.t
& VT_BTYPE
) == VT_QLONG
)
3949 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
3959 gfunc_param_typed(s
, sa
);
3969 tcc_error("too few arguments to function");
3971 if (!nocode_wanted
) {
3972 gfunc_call(nb_args
);
3974 vtop
-= (nb_args
+ 1);
3977 vsetc(&ret
.type
, ret
.r
, &ret
.c
);
3979 /* handle packed struct return */
3980 if (((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) && !sret
) {
3981 size
= type_size(&s
->type
, &align
);
3982 loc
= (loc
- size
) & -align
;
3984 vset(&ret
.type
, VT_LOCAL
| VT_LVAL
, addr
);
3988 vset(&s
->type
, VT_LOCAL
| VT_LVAL
, addr
);
3996 ST_FUNC
void expr_prod(void)
4001 while (tok
== '*' || tok
== '/' || tok
== '%') {
4009 ST_FUNC
void expr_sum(void)
4014 while (tok
== '+' || tok
== '-') {
4022 static void expr_shift(void)
4027 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
4035 static void expr_cmp(void)
4040 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
4041 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
4049 static void expr_cmpeq(void)
4054 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
4062 static void expr_and(void)
4065 while (tok
== '&') {
4072 static void expr_xor(void)
4075 while (tok
== '^') {
4082 static void expr_or(void)
4085 while (tok
== '|') {
4092 /* XXX: fix this mess */
4093 static void expr_land_const(void)
4096 while (tok
== TOK_LAND
) {
4103 /* XXX: fix this mess */
4104 static void expr_lor_const(void)
4107 while (tok
== TOK_LOR
) {
4114 /* only used if non constant */
4115 static void expr_land(void)
4120 if (tok
== TOK_LAND
) {
4125 if (tok
!= TOK_LAND
) {
4135 static void expr_lor(void)
4140 if (tok
== TOK_LOR
) {
4145 if (tok
!= TOK_LOR
) {
4155 /* XXX: better constant handling */
4156 static void expr_cond(void)
4158 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
;
4160 CType type
, type1
, type2
;
4167 boolean
.t
= VT_BOOL
;
4173 if (tok
!= ':' || !gnu_ext
) {
4188 if (vtop
!= vstack
) {
4189 /* needed to avoid having different registers saved in
4191 if (is_float(vtop
->type
.t
)) {
4193 #ifdef TCC_TARGET_X86_64
4194 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4204 if (tok
== ':' && gnu_ext
) {
4212 sv
= *vtop
; /* save value to handle it later */
4213 vtop
--; /* no vpop so that FP stack is not flushed */
4221 bt1
= t1
& VT_BTYPE
;
4223 bt2
= t2
& VT_BTYPE
;
4224 /* cast operands to correct type according to ISOC rules */
4225 if (is_float(bt1
) || is_float(bt2
)) {
4226 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
4227 type
.t
= VT_LDOUBLE
;
4228 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
4233 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
4234 /* cast to biggest op */
4236 /* convert to unsigned if it does not fit in a long long */
4237 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
4238 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
4239 type
.t
|= VT_UNSIGNED
;
4240 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
4241 /* If one is a null ptr constant the result type
4243 if (is_null_pointer (vtop
))
4245 else if (is_null_pointer (&sv
))
4247 /* XXX: test pointer compatibility, C99 has more elaborate
4251 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
4252 /* XXX: test function pointer compatibility */
4253 type
= bt1
== VT_FUNC
? type1
: type2
;
4254 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
4255 /* XXX: test structure compatibility */
4256 type
= bt1
== VT_STRUCT
? type1
: type2
;
4257 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
4258 /* NOTE: as an extension, we accept void on only one side */
4261 /* integer operations */
4263 /* convert to unsigned if it does not fit in an integer */
4264 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
4265 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
4266 type
.t
|= VT_UNSIGNED
;
4269 /* now we convert second operand */
4271 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4274 if (is_float(type
.t
)) {
4276 #ifdef TCC_TARGET_X86_64
4277 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4281 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
4282 /* for long longs, we use fixed registers to avoid having
4283 to handle a complicated move */
4288 /* this is horrible, but we must also convert first
4292 /* put again first value and cast it */
4295 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4298 move_reg(r2
, r1
, type
.t
);
4305 static void expr_eq(void)
4311 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
4312 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
4313 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
4328 ST_FUNC
void gexpr(void)
4339 /* parse an expression and return its type without any side effect. */
4340 static void expr_type(CType
*type
)
4342 int saved_nocode_wanted
;
4344 saved_nocode_wanted
= nocode_wanted
;
4349 nocode_wanted
= saved_nocode_wanted
;
4352 /* parse a unary expression and return its type without any side
4354 static void unary_type(CType
*type
)
4366 /* parse a constant expression and return value in vtop. */
4367 static void expr_const1(void)
4376 /* parse an integer constant and return its value. */
4377 ST_FUNC
int expr_const(void)
4381 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
4382 expect("constant expression");
4388 /* return the label token if current token is a label, otherwise
4390 static int is_label(void)
4394 /* fast test first */
4395 if (tok
< TOK_UIDENT
)
4397 /* no need to save tokc because tok is an identifier */
4404 unget_tok(last_tok
);
4409 static void label_or_decl(int l
)
4413 /* fast test first */
4414 if (tok
>= TOK_UIDENT
)
4416 /* no need to save tokc because tok is an identifier */
4420 unget_tok(last_tok
);
4423 unget_tok(last_tok
);
4428 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
4429 int case_reg
, int is_expr
)
4432 Sym
*s
, *frame_bottom
;
4434 /* generate line number info */
4435 if (tcc_state
->do_debug
&&
4436 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
4437 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
4439 last_line_num
= file
->line_num
;
4443 /* default return value is (void) */
4445 vtop
->type
.t
= VT_VOID
;
4448 if (tok
== TOK_IF
) {
4455 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4457 if (c
== TOK_ELSE
) {
4461 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4462 gsym(d
); /* patch else jmp */
4465 } else if (tok
== TOK_WHILE
) {
4473 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4477 } else if (tok
== '{') {
4481 /* record local declaration stack position */
4483 frame_bottom
= sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4484 frame_bottom
->next
= scope_stack_bottom
;
4485 scope_stack_bottom
= frame_bottom
;
4486 llabel
= local_label_stack
;
4487 /* handle local labels declarations */
4488 if (tok
== TOK_LABEL
) {
4491 if (tok
< TOK_UIDENT
)
4492 expect("label identifier");
4493 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
4503 while (tok
!= '}') {
4504 label_or_decl(VT_LOCAL
);
4508 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4511 /* pop locally defined labels */
4512 label_pop(&local_label_stack
, llabel
);
4514 /* XXX: this solution makes only valgrind happy...
4515 triggered by gcc.c-torture/execute/20000917-1.c */
4517 switch(vtop
->type
.t
& VT_BTYPE
) {
4522 for(p
=vtop
->type
.ref
;p
;p
=p
->prev
)
4524 tcc_error("unsupported expression type");
4527 /* pop locally defined symbols */
4528 scope_stack_bottom
= scope_stack_bottom
->next
;
4529 sym_pop(&local_stack
, s
);
4531 } else if (tok
== TOK_RETURN
) {
4535 gen_assign_cast(&func_vt
);
4536 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
4537 CType type
, ret_type
;
4539 if (gfunc_sret(&func_vt
, &ret_type
, &ret_align
)) {
4540 /* if returning structure, must copy it to implicit
4541 first pointer arg location */
4544 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
4547 /* copy structure value to pointer */
4550 /* returning structure packed into registers */
4551 int size
, addr
, align
;
4552 size
= type_size(&func_vt
,&align
);
4553 if ((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) || (vtop
->c
.i
& (ret_align
-1)))
4554 && (align
& (ret_align
-1))) {
4555 loc
= (loc
- size
) & -align
;
4558 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
4561 vset(&ret_type
, VT_LOCAL
| VT_LVAL
, addr
);
4563 vtop
->type
= ret_type
;
4564 if (is_float(ret_type
.t
))
4565 gv(rc_fret(ret_type
.t
));
4569 } else if (is_float(func_vt
.t
)) {
4570 gv(rc_fret(func_vt
.t
));
4574 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
4577 rsym
= gjmp(rsym
); /* jmp */
4578 } else if (tok
== TOK_BREAK
) {
4581 tcc_error("cannot break");
4582 *bsym
= gjmp(*bsym
);
4585 } else if (tok
== TOK_CONTINUE
) {
4588 tcc_error("cannot continue");
4589 *csym
= gjmp(*csym
);
4592 } else if (tok
== TOK_FOR
) {
4597 frame_bottom
= sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4598 frame_bottom
->next
= scope_stack_bottom
;
4599 scope_stack_bottom
= frame_bottom
;
4601 /* c99 for-loop init decl? */
4602 if (!decl0(VT_LOCAL
, 1)) {
4603 /* no, regular for-loop init expr */
4627 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4631 scope_stack_bottom
= scope_stack_bottom
->next
;
4632 sym_pop(&local_stack
, s
);
4634 if (tok
== TOK_DO
) {
4639 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4650 if (tok
== TOK_SWITCH
) {
4654 /* XXX: other types than integer */
4655 case_reg
= gv(RC_INT
);
4659 b
= gjmp(0); /* jump to first case */
4661 block(&a
, csym
, &b
, &c
, case_reg
, 0);
4662 /* if no default, jmp after switch */
4670 if (tok
== TOK_CASE
) {
4677 if (gnu_ext
&& tok
== TOK_DOTS
) {
4681 tcc_warning("empty case range");
4683 /* since a case is like a label, we must skip it with a jmp */
4690 *case_sym
= gtst(1, 0);
4693 *case_sym
= gtst(1, 0);
4697 *case_sym
= gtst(1, *case_sym
);
4702 goto block_after_label
;
4704 if (tok
== TOK_DEFAULT
) {
4710 tcc_error("too many 'default'");
4713 goto block_after_label
;
4715 if (tok
== TOK_GOTO
) {
4717 if (tok
== '*' && gnu_ext
) {
4721 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
4724 } else if (tok
>= TOK_UIDENT
) {
4725 s
= label_find(tok
);
4726 /* put forward definition if needed */
4728 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4730 if (s
->r
== LABEL_DECLARED
)
4731 s
->r
= LABEL_FORWARD
;
4733 /* label already defined */
4734 if (s
->r
& LABEL_FORWARD
)
4735 s
->jnext
= gjmp(s
->jnext
);
4737 gjmp_addr(s
->jnext
);
4740 expect("label identifier");
4743 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
4751 if (s
->r
== LABEL_DEFINED
)
4752 tcc_error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
4754 s
->r
= LABEL_DEFINED
;
4756 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
4759 /* we accept this, but it is a mistake */
4762 tcc_warning("deprecated use of label at end of compound statement");
4766 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4769 /* expression case */
4784 /* t is the array or struct type. c is the array or struct
4785 address. cur_index/cur_field is the pointer to the current
4786 value. 'size_only' is true if only size info is needed (only used
4788 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
4789 int *cur_index
, Sym
**cur_field
,
4793 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
4799 if (gnu_ext
&& (l
= is_label()) != 0)
4801 while (tok
== '[' || tok
== '.') {
4803 if (!(type
->t
& VT_ARRAY
))
4804 expect("array type");
4807 index
= expr_const();
4808 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
4809 expect("invalid index");
4810 if (tok
== TOK_DOTS
&& gnu_ext
) {
4812 index_last
= expr_const();
4813 if (index_last
< 0 ||
4814 (s
->c
>= 0 && index_last
>= s
->c
) ||
4816 expect("invalid index");
4822 *cur_index
= index_last
;
4823 type
= pointed_type(type
);
4824 elem_size
= type_size(type
, &align
);
4825 c
+= index
* elem_size
;
4826 /* NOTE: we only support ranges for last designator */
4827 nb_elems
= index_last
- index
+ 1;
4828 if (nb_elems
!= 1) {
4837 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
4838 expect("struct/union type");
4851 /* XXX: fix this mess by using explicit storage field */
4853 type1
.t
|= (type
->t
& ~VT_TYPE
);
4867 if (type
->t
& VT_ARRAY
) {
4869 type
= pointed_type(type
);
4870 c
+= index
* type_size(type
, &align
);
4874 tcc_error("too many field init");
4875 /* XXX: fix this mess by using explicit storage field */
4877 type1
.t
|= (type
->t
& ~VT_TYPE
);
4882 decl_initializer(type
, sec
, c
, 0, size_only
);
4884 /* XXX: make it more general */
4885 if (!size_only
&& nb_elems
> 1) {
4886 unsigned long c_end
;
4891 tcc_error("range init not supported yet for dynamic storage");
4892 c_end
= c
+ nb_elems
* elem_size
;
4893 if (c_end
> sec
->data_allocated
)
4894 section_realloc(sec
, c_end
);
4895 src
= sec
->data
+ c
;
4897 for(i
= 1; i
< nb_elems
; i
++) {
4899 memcpy(dst
, src
, elem_size
);
4905 #define EXPR_CONST 1
4908 /* store a value or an expression directly in global data or in local array */
4909 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
4910 int v
, int expr_type
)
4912 int saved_global_expr
, bt
, bit_pos
, bit_size
;
4914 unsigned long long bit_mask
;
4922 /* compound literals must be allocated globally in this case */
4923 saved_global_expr
= global_expr
;
4926 global_expr
= saved_global_expr
;
4927 /* NOTE: symbols are accepted */
4928 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
4929 tcc_error("initializer element is not constant");
4937 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
4940 /* XXX: not portable */
4941 /* XXX: generate error if incorrect relocation */
4942 gen_assign_cast(&dtype
);
4943 bt
= type
->t
& VT_BTYPE
;
4944 /* we'll write at most 12 bytes */
4945 if (c
+ 12 > sec
->data_allocated
) {
4946 section_realloc(sec
, c
+ 12);
4948 ptr
= sec
->data
+ c
;
4949 /* XXX: make code faster ? */
4950 if (!(type
->t
& VT_BITFIELD
)) {
4955 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4956 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
4957 bit_mask
= (1LL << bit_size
) - 1;
4959 if ((vtop
->r
& VT_SYM
) &&
4965 (bt
== VT_INT
&& bit_size
!= 32)))
4966 tcc_error("initializer element is not computable at load time");
4969 vtop
->c
.i
= (vtop
->c
.i
!= 0);
4971 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4974 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4977 *(double *)ptr
= vtop
->c
.d
;
4980 *(long double *)ptr
= vtop
->c
.ld
;
4983 *(long long *)ptr
|= (vtop
->c
.ll
& bit_mask
) << bit_pos
;
4986 if (vtop
->r
& VT_SYM
) {
4987 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
4989 *(int *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4994 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
5001 /* put zeros for variable based init */
5002 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
5005 /* nothing to do because globals are already set to zero */
5007 vpush_global_sym(&func_old_type
, TOK_memset
);
5015 /* 't' contains the type and storage info. 'c' is the offset of the
5016 object in section 'sec'. If 'sec' is NULL, it means stack based
5017 allocation. 'first' is true if array '{' must be read (multi
5018 dimension implicit array init handling). 'size_only' is true if
5019 size only evaluation is wanted (only for arrays). */
5020 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
5021 int first
, int size_only
)
5023 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, parlevel1
, i
;
5024 int size1
, align1
, expr_type
;
5028 if (type
->t
& VT_VLA
) {
5029 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
5033 vpush_global_sym(&func_old_type
, TOK_alloca
);
5034 vla_runtime_type_size(type
, &a
);
5039 vsetc(type
, REG_IRET
, &retcval
);
5040 vset(type
, VT_LOCAL
|VT_LVAL
, c
);
5045 tcc_error("variable length arrays unsupported for this target");
5047 } else if (type
->t
& VT_ARRAY
) {
5051 t1
= pointed_type(type
);
5052 size1
= type_size(t1
, &align1
);
5055 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
5058 tcc_error("character array initializer must be a literal,"
5059 " optionally enclosed in braces");
5064 /* only parse strings here if correct type (otherwise: handle
5065 them as ((w)char *) expressions */
5066 if ((tok
== TOK_LSTR
&&
5067 #ifdef TCC_TARGET_PE
5068 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
5070 (t1
->t
& VT_BTYPE
) == VT_INT
5072 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
5073 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5078 /* compute maximum number of chars wanted */
5080 cstr_len
= cstr
->size
;
5082 cstr_len
= cstr
->size
/ sizeof(nwchar_t
);
5085 if (n
>= 0 && nb
> (n
- array_length
))
5086 nb
= n
- array_length
;
5089 tcc_warning("initializer-string for array is too long");
5090 /* in order to go faster for common case (char
5091 string in global variable, we handle it
5093 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
5094 memcpy(sec
->data
+ c
+ array_length
, cstr
->data
, nb
);
5098 ch
= ((unsigned char *)cstr
->data
)[i
];
5100 ch
= ((nwchar_t
*)cstr
->data
)[i
];
5101 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
5109 /* only add trailing zero if enough storage (no
5110 warning in this case since it is standard) */
5111 if (n
< 0 || array_length
< n
) {
5113 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
5119 while (tok
!= '}') {
5120 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
5121 if (n
>= 0 && index
>= n
)
5122 tcc_error("index too large");
5123 /* must put zero in holes (note that doing it that way
5124 ensures that it even works with designators) */
5125 if (!size_only
&& array_length
< index
) {
5126 init_putz(t1
, sec
, c
+ array_length
* size1
,
5127 (index
- array_length
) * size1
);
5130 if (index
> array_length
)
5131 array_length
= index
;
5132 /* special test for multi dimensional arrays (may not
5133 be strictly correct if designators are used at the
5135 if (index
>= n
&& no_oblock
)
5144 /* put zeros at the end */
5145 if (!size_only
&& n
>= 0 && array_length
< n
) {
5146 init_putz(t1
, sec
, c
+ array_length
* size1
,
5147 (n
- array_length
) * size1
);
5149 /* patch type size if needed */
5151 s
->c
= array_length
;
5152 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
5153 (sec
|| !first
|| tok
== '{')) {
5156 /* NOTE: the previous test is a specific case for automatic
5157 struct/union init */
5158 /* XXX: union needs only one init */
5160 /* XXX: this test is incorrect for local initializers
5161 beginning with ( without {. It would be much more difficult
5162 to do it correctly (ideally, the expression parser should
5163 be used in all cases) */
5169 while (tok
== '(') {
5173 if (!parse_btype(&type1
, &ad1
))
5175 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
5177 if (!is_assignable_types(type
, &type1
))
5178 tcc_error("invalid type for cast");
5183 if (first
|| tok
== '{') {
5192 while (tok
!= '}') {
5193 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
5195 if (!size_only
&& array_length
< index
) {
5196 init_putz(type
, sec
, c
+ array_length
,
5197 index
- array_length
);
5199 index
= index
+ type_size(&f
->type
, &align1
);
5200 if (index
> array_length
)
5201 array_length
= index
;
5203 /* gr: skip fields from same union - ugly. */
5205 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
5206 /* test for same offset */
5207 if (f
->next
->c
!= f
->c
)
5209 /* if yes, test for bitfield shift */
5210 if ((f
->type
.t
& VT_BITFIELD
) && (f
->next
->type
.t
& VT_BITFIELD
)) {
5211 int bit_pos_1
= (f
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5212 int bit_pos_2
= (f
->next
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5213 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
5214 if (bit_pos_1
!= bit_pos_2
)
5221 if (no_oblock
&& f
== NULL
)
5227 /* put zeros at the end */
5228 if (!size_only
&& array_length
< n
) {
5229 init_putz(type
, sec
, c
+ array_length
,
5238 } else if (tok
== '{') {
5240 decl_initializer(type
, sec
, c
, first
, size_only
);
5242 } else if (size_only
) {
5243 /* just skip expression */
5244 parlevel
= parlevel1
= 0;
5245 while ((parlevel
> 0 || parlevel1
> 0 ||
5246 (tok
!= '}' && tok
!= ',')) && tok
!= -1) {
5249 else if (tok
== ')')
5251 else if (tok
== '{')
5253 else if (tok
== '}')
5258 /* currently, we always use constant expression for globals
5259 (may change for scripting case) */
5260 expr_type
= EXPR_CONST
;
5262 expr_type
= EXPR_ANY
;
5263 init_putv(type
, sec
, c
, 0, expr_type
);
5267 /* parse an initializer for type 't' if 'has_init' is non zero, and
5268 allocate space in local or global data space ('r' is either
5269 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5270 variable 'v' with an associated name represented by 'asm_label' of
5271 scope 'scope' is declared before initializers are parsed. If 'v' is
5272 zero, then a reference to the new object is put in the value stack.
5273 If 'has_init' is 2, a special parsing is done to handle string
5275 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
5276 int has_init
, int v
, char *asm_label
,
5279 int size
, align
, addr
, data_offset
;
5281 ParseState saved_parse_state
= {0};
5282 TokenString init_str
;
5284 Sym
*flexible_array
;
5286 flexible_array
= NULL
;
5287 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
5290 while (field
&& field
->next
)
5291 field
= field
->next
;
5292 if (field
->type
.t
& VT_ARRAY
&& field
->type
.ref
->c
< 0)
5293 flexible_array
= field
;
5296 size
= type_size(type
, &align
);
5297 /* If unknown size, we must evaluate it before
5298 evaluating initializers because
5299 initializers can generate global data too
5300 (e.g. string pointers or ISOC99 compound
5301 literals). It also simplifies local
5302 initializers handling */
5303 tok_str_new(&init_str
);
5304 if (size
< 0 || (flexible_array
&& has_init
)) {
5306 tcc_error("unknown type size");
5307 /* get all init string */
5308 if (has_init
== 2) {
5309 /* only get strings */
5310 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5311 tok_str_add_tok(&init_str
);
5316 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
5318 tcc_error("unexpected end of file in initializer");
5319 tok_str_add_tok(&init_str
);
5322 else if (tok
== '}') {
5332 tok_str_add(&init_str
, -1);
5333 tok_str_add(&init_str
, 0);
5336 save_parse_state(&saved_parse_state
);
5338 macro_ptr
= init_str
.str
;
5340 decl_initializer(type
, NULL
, 0, 1, 1);
5341 /* prepare second initializer parsing */
5342 macro_ptr
= init_str
.str
;
5345 /* if still unknown size, error */
5346 size
= type_size(type
, &align
);
5348 tcc_error("unknown type size");
5351 size
+= flexible_array
->type
.ref
->c
* pointed_size(&flexible_array
->type
);
5352 /* take into account specified alignment if bigger */
5354 if (ad
->aligned
> align
)
5355 align
= ad
->aligned
;
5356 } else if (ad
->packed
) {
5359 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
5361 #ifdef CONFIG_TCC_BCHECK
5362 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5366 loc
= (loc
- size
) & -align
;
5368 #ifdef CONFIG_TCC_BCHECK
5369 /* handles bounds */
5370 /* XXX: currently, since we do only one pass, we cannot track
5371 '&' operators, so we add only arrays */
5372 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5373 unsigned long *bounds_ptr
;
5374 /* add padding between regions */
5376 /* then add local bound info */
5377 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(unsigned long));
5378 bounds_ptr
[0] = addr
;
5379 bounds_ptr
[1] = size
;
5383 /* local variable */
5384 sym_push(v
, type
, r
, addr
);
5386 /* push local reference */
5387 vset(type
, r
, addr
);
5393 if (v
&& scope
== VT_CONST
) {
5394 /* see if the symbol was already defined */
5397 if (!is_compatible_types(&sym
->type
, type
))
5398 tcc_error("incompatible types for redefinition of '%s'",
5399 get_tok_str(v
, NULL
));
5400 if (sym
->type
.t
& VT_EXTERN
) {
5401 /* if the variable is extern, it was not allocated */
5402 sym
->type
.t
&= ~VT_EXTERN
;
5403 /* set array size if it was ommited in extern
5405 if ((sym
->type
.t
& VT_ARRAY
) &&
5406 sym
->type
.ref
->c
< 0 &&
5408 sym
->type
.ref
->c
= type
->ref
->c
;
5410 /* we accept several definitions of the same
5411 global variable. this is tricky, because we
5412 must play with the SHN_COMMON type of the symbol */
5413 /* XXX: should check if the variable was already
5414 initialized. It is incorrect to initialized it
5416 /* no init data, we won't add more to the symbol */
5423 /* allocate symbol in corresponding section */
5428 else if (tcc_state
->nocommon
)
5432 data_offset
= sec
->data_offset
;
5433 data_offset
= (data_offset
+ align
- 1) & -align
;
5435 /* very important to increment global pointer at this time
5436 because initializers themselves can create new initializers */
5437 data_offset
+= size
;
5438 #ifdef CONFIG_TCC_BCHECK
5439 /* add padding if bound check */
5440 if (tcc_state
->do_bounds_check
)
5443 sec
->data_offset
= data_offset
;
5444 /* allocate section space to put the data */
5445 if (sec
->sh_type
!= SHT_NOBITS
&&
5446 data_offset
> sec
->data_allocated
)
5447 section_realloc(sec
, data_offset
);
5448 /* align section if needed */
5449 if (align
> sec
->sh_addralign
)
5450 sec
->sh_addralign
= align
;
5452 addr
= 0; /* avoid warning */
5456 if (scope
!= VT_CONST
|| !sym
) {
5457 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
5458 sym
->asm_label
= asm_label
;
5460 /* update symbol definition */
5462 put_extern_sym(sym
, sec
, addr
, size
);
5465 /* put a common area */
5466 put_extern_sym(sym
, NULL
, align
, size
);
5467 /* XXX: find a nicer way */
5468 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
5469 esym
->st_shndx
= SHN_COMMON
;
5474 /* push global reference */
5475 sym
= get_sym_ref(type
, sec
, addr
, size
);
5477 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
5480 /* patch symbol weakness */
5481 if (type
->t
& VT_WEAK
)
5483 #ifdef CONFIG_TCC_BCHECK
5484 /* handles bounds now because the symbol must be defined
5485 before for the relocation */
5486 if (tcc_state
->do_bounds_check
) {
5487 unsigned long *bounds_ptr
;
5489 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
5490 /* then add global bound info */
5491 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(long));
5492 bounds_ptr
[0] = 0; /* relocated */
5493 bounds_ptr
[1] = size
;
5497 if (has_init
|| (type
->t
& VT_VLA
)) {
5498 decl_initializer(type
, sec
, addr
, 1, 0);
5499 /* restore parse state if needed */
5501 tok_str_free(init_str
.str
);
5502 restore_parse_state(&saved_parse_state
);
5504 /* patch flexible array member size back to -1, */
5505 /* for possible subsequent similar declarations */
5507 flexible_array
->type
.ref
->c
= -1;
5512 static void put_func_debug(Sym
*sym
)
5517 /* XXX: we put here a dummy type */
5518 snprintf(buf
, sizeof(buf
), "%s:%c1",
5519 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
5520 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
5521 cur_text_section
, sym
->c
);
5522 /* //gr gdb wants a line at the function */
5523 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
5528 /* parse an old style function declaration list */
5529 /* XXX: check multiple parameter */
5530 static void func_decl_list(Sym
*func_sym
)
5537 /* parse each declaration */
5538 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
&&
5539 tok
!= TOK_ASM1
&& tok
!= TOK_ASM2
&& tok
!= TOK_ASM3
) {
5540 if (!parse_btype(&btype
, &ad
))
5541 expect("declaration list");
5542 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5543 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5545 /* we accept no variable after */
5549 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5550 /* find parameter in function parameter list */
5553 if ((s
->v
& ~SYM_FIELD
) == v
)
5557 tcc_error("declaration for parameter '%s' but no such parameter",
5558 get_tok_str(v
, NULL
));
5560 /* check that no storage specifier except 'register' was given */
5561 if (type
.t
& VT_STORAGE
)
5562 tcc_error("storage class specified for '%s'", get_tok_str(v
, NULL
));
5563 convert_parameter_type(&type
);
5564 /* we can add the type (NOTE: it could be local to the function) */
5566 /* accept other parameters */
5577 /* parse a function defined by symbol 'sym' and generate its code in
5578 'cur_text_section' */
5579 static void gen_function(Sym
*sym
)
5581 int saved_nocode_wanted
= nocode_wanted
;
5583 ind
= cur_text_section
->data_offset
;
5584 /* NOTE: we patch the symbol size later */
5585 put_extern_sym(sym
, cur_text_section
, ind
, 0);
5586 funcname
= get_tok_str(sym
->v
, NULL
);
5588 /* put debug symbol */
5589 if (tcc_state
->do_debug
)
5590 put_func_debug(sym
);
5591 /* push a dummy symbol to enable local sym storage */
5592 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
5593 gfunc_prolog(&sym
->type
);
5595 block(NULL
, NULL
, NULL
, NULL
, 0, 0);
5598 cur_text_section
->data_offset
= ind
;
5599 label_pop(&global_label_stack
, NULL
);
5600 /* reset local stack */
5601 scope_stack_bottom
= NULL
;
5602 sym_pop(&local_stack
, NULL
);
5603 /* end of function */
5604 /* patch symbol size */
5605 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
5607 /* patch symbol weakness (this definition overrules any prototype) */
5608 if (sym
->type
.t
& VT_WEAK
)
5610 if (tcc_state
->do_debug
) {
5611 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
5613 /* It's better to crash than to generate wrong code */
5614 cur_text_section
= NULL
;
5615 funcname
= ""; /* for safety */
5616 func_vt
.t
= VT_VOID
; /* for safety */
5617 ind
= 0; /* for safety */
5618 nocode_wanted
= saved_nocode_wanted
;
5621 ST_FUNC
void gen_inline_functions(void)
5624 int *str
, inline_generated
, i
;
5625 struct InlineFunc
*fn
;
5627 /* iterate while inline function are referenced */
5629 inline_generated
= 0;
5630 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5631 fn
= tcc_state
->inline_fns
[i
];
5633 if (sym
&& sym
->c
) {
5634 /* the function was used: generate its code and
5635 convert it to a normal function */
5636 str
= fn
->token_str
;
5639 pstrcpy(file
->filename
, sizeof file
->filename
, fn
->filename
);
5640 sym
->r
= VT_SYM
| VT_CONST
;
5641 sym
->type
.t
&= ~VT_INLINE
;
5645 cur_text_section
= text_section
;
5647 macro_ptr
= NULL
; /* fail safe */
5649 inline_generated
= 1;
5652 if (!inline_generated
)
5655 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5656 fn
= tcc_state
->inline_fns
[i
];
5657 str
= fn
->token_str
;
5660 dynarray_reset(&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
);
5663 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
5664 static int decl0(int l
, int is_for_loop_init
)
5672 if (!parse_btype(&btype
, &ad
)) {
5673 if (is_for_loop_init
)
5675 /* skip redundant ';' */
5676 /* XXX: find more elegant solution */
5681 if (l
== VT_CONST
&&
5682 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5683 /* global asm block */
5687 /* special test for old K&R protos without explicit int
5688 type. Only accepted when defining global data */
5689 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
5693 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5694 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5696 /* we accept no variable after */
5700 while (1) { /* iterate thru each declaration */
5701 char *asm_label
; // associated asm label
5703 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5707 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
5708 printf("type = '%s'\n", buf
);
5711 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5712 if ((type
.t
& VT_STATIC
) && (l
== VT_LOCAL
)) {
5713 tcc_error("function without file scope cannot be static");
5715 /* if old style function prototype, we accept a
5718 if (sym
->c
== FUNC_OLD
)
5719 func_decl_list(sym
);
5723 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5726 asm_label_instr(&astr
);
5727 asm_label
= tcc_strdup(astr
.data
);
5730 /* parse one last attribute list, after asm label */
5731 parse_attribute(&ad
);
5736 #ifdef TCC_TARGET_PE
5738 type
.t
|= VT_IMPORT
;
5740 type
.t
|= VT_EXPORT
;
5744 tcc_error("cannot use local functions");
5745 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
5746 expect("function definition");
5748 /* reject abstract declarators in function definition */
5750 while ((sym
= sym
->next
) != NULL
)
5751 if (!(sym
->v
& ~SYM_FIELD
))
5752 expect("identifier");
5754 /* XXX: cannot do better now: convert extern line to static inline */
5755 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
5756 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5760 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
5763 r
= sym
->type
.ref
->r
;
5764 /* use func_call from prototype if not defined */
5765 if (FUNC_CALL(r
) != FUNC_CDECL
5766 && FUNC_CALL(type
.ref
->r
) == FUNC_CDECL
)
5767 FUNC_CALL(type
.ref
->r
) = FUNC_CALL(r
);
5769 /* use export from prototype */
5771 FUNC_EXPORT(type
.ref
->r
) = 1;
5773 /* use static from prototype */
5774 if (sym
->type
.t
& VT_STATIC
)
5775 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5777 if (!is_compatible_types(&sym
->type
, &type
)) {
5779 tcc_error("incompatible types for redefinition of '%s'",
5780 get_tok_str(v
, NULL
));
5782 /* if symbol is already defined, then put complete type */
5785 /* put function symbol */
5786 sym
= global_identifier_push(v
, type
.t
, 0);
5787 sym
->type
.ref
= type
.ref
;
5790 /* static inline functions are just recorded as a kind
5791 of macro. Their code will be emitted at the end of
5792 the compilation unit only if they are used */
5793 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
5794 (VT_INLINE
| VT_STATIC
)) {
5795 TokenString func_str
;
5797 struct InlineFunc
*fn
;
5798 const char *filename
;
5800 tok_str_new(&func_str
);
5806 tcc_error("unexpected end of file");
5807 tok_str_add_tok(&func_str
);
5812 } else if (t
== '}') {
5814 if (block_level
== 0)
5818 tok_str_add(&func_str
, -1);
5819 tok_str_add(&func_str
, 0);
5820 filename
= file
? file
->filename
: "";
5821 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
5822 strcpy(fn
->filename
, filename
);
5824 fn
->token_str
= func_str
.str
;
5825 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
5828 /* compute text section */
5829 cur_text_section
= ad
.section
;
5830 if (!cur_text_section
)
5831 cur_text_section
= text_section
;
5832 sym
->r
= VT_SYM
| VT_CONST
;
5837 if (btype
.t
& VT_TYPEDEF
) {
5838 /* save typedefed type */
5839 /* XXX: test storage specifiers ? */
5840 sym
= sym_push(v
, &type
, INT_ATTR(&ad
), 0);
5841 sym
->type
.t
|= VT_TYPEDEF
;
5844 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5845 /* external function definition */
5846 /* specific case for func_call attribute */
5847 type
.ref
->r
= INT_ATTR(&ad
);
5848 } else if (!(type
.t
& VT_ARRAY
)) {
5849 /* not lvalue if array */
5850 r
|= lvalue_type(type
.t
);
5852 has_init
= (tok
== '=');
5853 if (has_init
&& (type
.t
& VT_VLA
))
5854 tcc_error("Variable length array cannot be initialized");
5855 if ((btype
.t
& VT_EXTERN
) || ((type
.t
& VT_BTYPE
) == VT_FUNC
) ||
5856 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
5857 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
5858 /* external variable or function */
5859 /* NOTE: as GCC, uninitialized global static
5860 arrays of null size are considered as
5862 sym
= external_sym(v
, &type
, r
, asm_label
);
5864 if (type
.t
& VT_WEAK
)
5867 if (ad
.alias_target
) {
5872 alias_target
= sym_find(ad
.alias_target
);
5873 if (!alias_target
|| !alias_target
->c
)
5874 tcc_error("unsupported forward __alias__ attribute");
5875 esym
= &((Elf32_Sym
*)symtab_section
->data
)[alias_target
->c
];
5876 tsec
.sh_num
= esym
->st_shndx
;
5877 put_extern_sym2(sym
, &tsec
, esym
->st_value
, esym
->st_size
, 0);
5880 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
5881 if (type
.t
& VT_STATIC
)
5887 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, asm_label
, l
);
5891 if (is_for_loop_init
)
5904 ST_FUNC
void decl(int l
)