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_LVAL_BYTE
)
856 else if (vtop
->r
& VT_LVAL_SHORT
)
858 if (vtop
->r
& VT_LVAL_UNSIGNED
)
862 /* restore wanted type */
865 /* one register type load */
870 #ifdef TCC_TARGET_C67
871 /* uses register pairs for doubles */
872 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
879 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
880 ST_FUNC
void gv2(int rc1
, int rc2
)
884 /* generate more generic register first. But VT_JMP or VT_CMP
885 values must be generated first in all cases to avoid possible
887 v
= vtop
[0].r
& VT_VALMASK
;
888 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
893 /* test if reload is needed for first register */
894 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
904 /* test if reload is needed for first register */
905 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
911 /* wrapper around RC_FRET to return a register by type */
912 static int rc_fret(int t
)
914 #ifdef TCC_TARGET_X86_64
915 if (t
== VT_LDOUBLE
) {
922 /* wrapper around REG_FRET to return a register by type */
923 static int reg_fret(int t
)
925 #ifdef TCC_TARGET_X86_64
926 if (t
== VT_LDOUBLE
) {
933 /* expand long long on stack in two int registers */
934 static void lexpand(void)
938 u
= vtop
->type
.t
& VT_UNSIGNED
;
941 vtop
[0].r
= vtop
[-1].r2
;
942 vtop
[0].r2
= VT_CONST
;
943 vtop
[-1].r2
= VT_CONST
;
944 vtop
[0].type
.t
= VT_INT
| u
;
945 vtop
[-1].type
.t
= VT_INT
| u
;
948 #ifdef TCC_TARGET_ARM
949 /* expand long long on stack */
950 ST_FUNC
void lexpand_nr(void)
954 u
= vtop
->type
.t
& VT_UNSIGNED
;
957 vtop
->type
.t
= VT_INT
| u
;
958 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
960 vtop
[-1].c
.ui
= vtop
->c
.ull
;
961 vtop
->c
.ui
= vtop
->c
.ull
>> 32;
963 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
965 vtop
->r
= vtop
[-1].r
;
966 } else if (v
> VT_CONST
) {
970 vtop
->r
= vtop
[-1].r2
;
971 vtop
[-1].r2
= VT_CONST
;
972 vtop
[-1].type
.t
= VT_INT
| u
;
976 /* build a long long from two ints */
977 static void lbuild(int t
)
980 vtop
[-1].r2
= vtop
[0].r
;
985 /* rotate n first stack elements to the bottom
986 I1 ... In -> I2 ... In I1 [top is right]
988 ST_FUNC
void vrotb(int n
)
999 /* rotate the n elements before entry e towards the top
1000 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
1002 ST_FUNC
void vrote(SValue
*e
, int n
)
1008 for(i
= 0;i
< n
- 1; i
++)
1013 /* rotate n first stack elements to the top
1014 I1 ... In -> In I1 ... I(n-1) [top is right]
1016 ST_FUNC
void vrott(int n
)
1021 /* pop stack value */
1022 ST_FUNC
void vpop(void)
1025 v
= vtop
->r
& VT_VALMASK
;
1026 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1027 /* for x86, we need to pop the FP stack */
1028 if (v
== TREG_ST0
&& !nocode_wanted
) {
1029 o(0xd8dd); /* fstp %st(0) */
1032 if (v
== VT_JMP
|| v
== VT_JMPI
) {
1033 /* need to put correct jump if && or || without test */
1039 /* convert stack entry to register and duplicate its value in another
1041 static void gv_dup(void)
1047 if ((t
& VT_BTYPE
) == VT_LLONG
) {
1054 /* stack: H L L1 H1 */
1062 /* duplicate value */
1067 #ifdef TCC_TARGET_X86_64
1068 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1078 load(r1
, &sv
); /* move r to r1 */
1080 /* duplicates value */
1086 #ifndef TCC_TARGET_X86_64
1087 /* generate CPU independent (unsigned) long long operations */
1088 static void gen_opl(int op
)
1090 int t
, a
, b
, op1
, c
, i
;
1092 unsigned short reg_iret
= REG_IRET
;
1093 unsigned short reg_lret
= REG_LRET
;
1099 func
= TOK___divdi3
;
1102 func
= TOK___udivdi3
;
1105 func
= TOK___moddi3
;
1108 func
= TOK___umoddi3
;
1115 /* call generic long long function */
1116 vpush_global_sym(&func_old_type
, func
);
1121 vtop
->r2
= reg_lret
;
1134 /* stack: L1 H1 L2 H2 */
1139 vtop
[-2] = vtop
[-3];
1142 /* stack: H1 H2 L1 L2 */
1148 /* stack: H1 H2 L1 L2 ML MH */
1151 /* stack: ML MH H1 H2 L1 L2 */
1155 /* stack: ML MH H1 L2 H2 L1 */
1160 /* stack: ML MH M1 M2 */
1163 } else if (op
== '+' || op
== '-') {
1164 /* XXX: add non carry method too (for MIPS or alpha) */
1170 /* stack: H1 H2 (L1 op L2) */
1173 gen_op(op1
+ 1); /* TOK_xxxC2 */
1176 /* stack: H1 H2 (L1 op L2) */
1179 /* stack: (L1 op L2) H1 H2 */
1181 /* stack: (L1 op L2) (H1 op H2) */
1189 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1190 t
= vtop
[-1].type
.t
;
1194 /* stack: L H shift */
1196 /* constant: simpler */
1197 /* NOTE: all comments are for SHL. the other cases are
1198 done by swaping words */
1209 if (op
!= TOK_SAR
) {
1242 /* XXX: should provide a faster fallback on x86 ? */
1245 func
= TOK___ashrdi3
;
1248 func
= TOK___lshrdi3
;
1251 func
= TOK___ashldi3
;
1257 /* compare operations */
1263 /* stack: L1 H1 L2 H2 */
1265 vtop
[-1] = vtop
[-2];
1267 /* stack: L1 L2 H1 H2 */
1270 /* when values are equal, we need to compare low words. since
1271 the jump is inverted, we invert the test too. */
1274 else if (op1
== TOK_GT
)
1276 else if (op1
== TOK_ULT
)
1278 else if (op1
== TOK_UGT
)
1283 if (op1
!= TOK_NE
) {
1287 /* generate non equal test */
1288 /* XXX: NOT PORTABLE yet */
1292 #if defined(TCC_TARGET_I386)
1293 b
= psym(0x850f, 0);
1294 #elif defined(TCC_TARGET_ARM)
1296 o(0x1A000000 | encbranch(ind
, 0, 1));
1297 #elif defined(TCC_TARGET_C67)
1298 tcc_error("not implemented");
1300 #error not supported
1304 /* compare low. Always unsigned */
1308 else if (op1
== TOK_LE
)
1310 else if (op1
== TOK_GT
)
1312 else if (op1
== TOK_GE
)
1323 /* handle integer constant optimizations and various machine
1325 static void gen_opic(int op
)
1327 int c1
, c2
, t1
, t2
, n
;
1330 typedef unsigned long long U
;
1334 t1
= v1
->type
.t
& VT_BTYPE
;
1335 t2
= v2
->type
.t
& VT_BTYPE
;
1339 else if (v1
->type
.t
& VT_UNSIGNED
)
1346 else if (v2
->type
.t
& VT_UNSIGNED
)
1351 /* currently, we cannot do computations with forward symbols */
1352 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1353 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1356 case '+': l1
+= l2
; break;
1357 case '-': l1
-= l2
; break;
1358 case '&': l1
&= l2
; break;
1359 case '^': l1
^= l2
; break;
1360 case '|': l1
|= l2
; break;
1361 case '*': l1
*= l2
; break;
1368 /* if division by zero, generate explicit division */
1371 tcc_error("division by zero in constant");
1375 default: l1
/= l2
; break;
1376 case '%': l1
%= l2
; break;
1377 case TOK_UDIV
: l1
= (U
)l1
/ l2
; break;
1378 case TOK_UMOD
: l1
= (U
)l1
% l2
; break;
1381 case TOK_SHL
: l1
<<= l2
; break;
1382 case TOK_SHR
: l1
= (U
)l1
>> l2
; break;
1383 case TOK_SAR
: l1
>>= l2
; break;
1385 case TOK_ULT
: l1
= (U
)l1
< (U
)l2
; break;
1386 case TOK_UGE
: l1
= (U
)l1
>= (U
)l2
; break;
1387 case TOK_EQ
: l1
= l1
== l2
; break;
1388 case TOK_NE
: l1
= l1
!= l2
; break;
1389 case TOK_ULE
: l1
= (U
)l1
<= (U
)l2
; break;
1390 case TOK_UGT
: l1
= (U
)l1
> (U
)l2
; break;
1391 case TOK_LT
: l1
= l1
< l2
; break;
1392 case TOK_GE
: l1
= l1
>= l2
; break;
1393 case TOK_LE
: l1
= l1
<= l2
; break;
1394 case TOK_GT
: l1
= l1
> l2
; break;
1396 case TOK_LAND
: l1
= l1
&& l2
; break;
1397 case TOK_LOR
: l1
= l1
|| l2
; break;
1404 /* if commutative ops, put c2 as constant */
1405 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1406 op
== '|' || op
== '*')) {
1408 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1409 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1411 /* Filter out NOP operations like x*1, x-0, x&-1... */
1412 if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1415 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1416 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1422 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1423 /* try to use shifts instead of muls or divs */
1424 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1433 else if (op
== TOK_PDIV
)
1439 } else if (c2
&& (op
== '+' || op
== '-') &&
1440 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
1441 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1442 /* symbol + constant case */
1449 if (!nocode_wanted
) {
1450 /* call low level op generator */
1451 if (t1
== VT_LLONG
|| t2
== VT_LLONG
)
1462 /* generate a floating point operation with constant propagation */
1463 static void gen_opif(int op
)
1471 /* currently, we cannot do computations with forward symbols */
1472 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1473 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1475 if (v1
->type
.t
== VT_FLOAT
) {
1478 } else if (v1
->type
.t
== VT_DOUBLE
) {
1486 /* NOTE: we only do constant propagation if finite number (not
1487 NaN or infinity) (ANSI spec) */
1488 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1492 case '+': f1
+= f2
; break;
1493 case '-': f1
-= f2
; break;
1494 case '*': f1
*= f2
; break;
1498 tcc_error("division by zero in constant");
1503 /* XXX: also handles tests ? */
1507 /* XXX: overflow test ? */
1508 if (v1
->type
.t
== VT_FLOAT
) {
1510 } else if (v1
->type
.t
== VT_DOUBLE
) {
1518 if (!nocode_wanted
) {
1526 static int pointed_size(CType
*type
)
1529 return type_size(pointed_type(type
), &align
);
1532 static void vla_runtime_pointed_size(CType
*type
)
1535 vla_runtime_type_size(pointed_type(type
), &align
);
1538 static inline int is_null_pointer(SValue
*p
)
1540 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1542 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& p
->c
.i
== 0) ||
1543 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.ll
== 0) ||
1544 ((p
->type
.t
& VT_BTYPE
) == VT_PTR
&& p
->c
.ptr
== 0);
1547 static inline int is_integer_btype(int bt
)
1549 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
1550 bt
== VT_INT
|| bt
== VT_LLONG
);
1553 /* check types for comparison or substraction of pointers */
1554 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
1556 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
1559 /* null pointers are accepted for all comparisons as gcc */
1560 if (is_null_pointer(p1
) || is_null_pointer(p2
))
1564 bt1
= type1
->t
& VT_BTYPE
;
1565 bt2
= type2
->t
& VT_BTYPE
;
1566 /* accept comparison between pointer and integer with a warning */
1567 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
1568 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
1569 tcc_warning("comparison between pointer and integer");
1573 /* both must be pointers or implicit function pointers */
1574 if (bt1
== VT_PTR
) {
1575 type1
= pointed_type(type1
);
1576 } else if (bt1
!= VT_FUNC
)
1577 goto invalid_operands
;
1579 if (bt2
== VT_PTR
) {
1580 type2
= pointed_type(type2
);
1581 } else if (bt2
!= VT_FUNC
) {
1583 tcc_error("invalid operands to binary %s", get_tok_str(op
, NULL
));
1585 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1586 (type2
->t
& VT_BTYPE
) == VT_VOID
)
1590 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1591 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1592 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1593 /* gcc-like error if '-' is used */
1595 goto invalid_operands
;
1597 tcc_warning("comparison of distinct pointer types lacks a cast");
1601 /* generic gen_op: handles types problems */
1602 ST_FUNC
void gen_op(int op
)
1604 int u
, t1
, t2
, bt1
, bt2
, t
;
1607 t1
= vtop
[-1].type
.t
;
1608 t2
= vtop
[0].type
.t
;
1609 bt1
= t1
& VT_BTYPE
;
1610 bt2
= t2
& VT_BTYPE
;
1612 if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
1613 /* at least one operand is a pointer */
1614 /* relationnal op: must be both pointers */
1615 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
1616 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1617 /* pointers are handled are unsigned */
1618 #ifdef TCC_TARGET_X86_64
1619 t
= VT_LLONG
| VT_UNSIGNED
;
1621 t
= VT_INT
| VT_UNSIGNED
;
1625 /* if both pointers, then it must be the '-' op */
1626 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
1628 tcc_error("cannot use pointers here");
1629 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1630 /* XXX: check that types are compatible */
1631 if (vtop
[-1].type
.t
& VT_VLA
) {
1632 vla_runtime_pointed_size(&vtop
[-1].type
);
1634 vpushi(pointed_size(&vtop
[-1].type
));
1638 /* set to integer type */
1639 #ifdef TCC_TARGET_X86_64
1640 vtop
->type
.t
= VT_LLONG
;
1642 vtop
->type
.t
= VT_INT
;
1647 /* exactly one pointer : must be '+' or '-'. */
1648 if (op
!= '-' && op
!= '+')
1649 tcc_error("cannot use pointers here");
1650 /* Put pointer as first operand */
1651 if (bt2
== VT_PTR
) {
1655 type1
= vtop
[-1].type
;
1656 type1
.t
&= ~VT_ARRAY
;
1657 if (vtop
[-1].type
.t
& VT_VLA
)
1658 vla_runtime_pointed_size(&vtop
[-1].type
);
1660 u
= pointed_size(&vtop
[-1].type
);
1662 tcc_error("unknown array element size");
1663 #ifdef TCC_TARGET_X86_64
1666 /* XXX: cast to int ? (long long case) */
1671 #ifdef CONFIG_TCC_BCHECK
1672 /* if evaluating constant expression, no code should be
1673 generated, so no bound check */
1674 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
1675 /* if bounded pointers, we generate a special code to
1682 gen_bounded_ptr_add();
1688 /* put again type if gen_opic() swaped operands */
1691 } else if (is_float(bt1
) || is_float(bt2
)) {
1692 /* compute bigger type and do implicit casts */
1693 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
1695 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
1700 /* floats can only be used for a few operations */
1701 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
1702 (op
< TOK_ULT
|| op
> TOK_GT
))
1703 tcc_error("invalid operands for binary operation");
1705 } else if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
) {
1706 t
= bt1
== VT_LLONG
? VT_LLONG
: VT_INT
;
1707 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (t
| VT_UNSIGNED
))
1710 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
1711 /* cast to biggest op */
1713 /* convert to unsigned if it does not fit in a long long */
1714 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
1715 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
1718 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
1719 tcc_error("comparison of struct");
1721 /* integer operations */
1723 /* convert to unsigned if it does not fit in an integer */
1724 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
1725 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
1728 /* XXX: currently, some unsigned operations are explicit, so
1729 we modify them here */
1730 if (t
& VT_UNSIGNED
) {
1737 else if (op
== TOK_LT
)
1739 else if (op
== TOK_GT
)
1741 else if (op
== TOK_LE
)
1743 else if (op
== TOK_GE
)
1750 /* special case for shifts and long long: we keep the shift as
1752 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
1759 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
1760 /* relationnal op: the result is an int */
1761 vtop
->type
.t
= VT_INT
;
1768 #ifndef TCC_TARGET_ARM
1769 /* generic itof for unsigned long long case */
1770 static void gen_cvt_itof1(int t
)
1772 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
1773 (VT_LLONG
| VT_UNSIGNED
)) {
1776 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
1777 #if LDOUBLE_SIZE != 8
1778 else if (t
== VT_LDOUBLE
)
1779 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
1782 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
1786 vtop
->r
= reg_fret(t
);
1793 /* generic ftoi for unsigned long long case */
1794 static void gen_cvt_ftoi1(int t
)
1798 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
1799 /* not handled natively */
1800 st
= vtop
->type
.t
& VT_BTYPE
;
1802 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
1803 #if LDOUBLE_SIZE != 8
1804 else if (st
== VT_LDOUBLE
)
1805 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
1808 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
1813 vtop
->r2
= REG_LRET
;
1819 /* force char or short cast */
1820 static void force_charshort_cast(int t
)
1824 /* XXX: add optimization if lvalue : just change type and offset */
1829 if (t
& VT_UNSIGNED
) {
1830 vpushi((1 << bits
) - 1);
1836 /* result must be signed or the SAR is converted to an SHL
1837 This was not the case when "t" was a signed short
1838 and the last value on the stack was an unsigned int */
1839 vtop
->type
.t
&= ~VT_UNSIGNED
;
1845 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1846 static void gen_cast(CType
*type
)
1848 int sbt
, dbt
, sf
, df
, c
, p
;
1850 /* special delayed cast for char/short */
1851 /* XXX: in some cases (multiple cascaded casts), it may still
1853 if (vtop
->r
& VT_MUSTCAST
) {
1854 vtop
->r
&= ~VT_MUSTCAST
;
1855 force_charshort_cast(vtop
->type
.t
);
1858 /* bitfields first get cast to ints */
1859 if (vtop
->type
.t
& VT_BITFIELD
) {
1863 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
1864 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
1869 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1870 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
1872 /* constant case: we can do it now */
1873 /* XXX: in ISOC, cannot do it if error in convert */
1874 if (sbt
== VT_FLOAT
)
1875 vtop
->c
.ld
= vtop
->c
.f
;
1876 else if (sbt
== VT_DOUBLE
)
1877 vtop
->c
.ld
= vtop
->c
.d
;
1880 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
1881 if (sbt
& VT_UNSIGNED
)
1882 vtop
->c
.ld
= vtop
->c
.ull
;
1884 vtop
->c
.ld
= vtop
->c
.ll
;
1886 if (sbt
& VT_UNSIGNED
)
1887 vtop
->c
.ld
= vtop
->c
.ui
;
1889 vtop
->c
.ld
= vtop
->c
.i
;
1892 if (dbt
== VT_FLOAT
)
1893 vtop
->c
.f
= (float)vtop
->c
.ld
;
1894 else if (dbt
== VT_DOUBLE
)
1895 vtop
->c
.d
= (double)vtop
->c
.ld
;
1896 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
1897 vtop
->c
.ull
= (unsigned long long)vtop
->c
.ld
;
1898 } else if (sf
&& dbt
== VT_BOOL
) {
1899 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
1902 vtop
->c
.ll
= (long long)vtop
->c
.ld
;
1903 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
1904 vtop
->c
.ll
= vtop
->c
.ull
;
1905 else if (sbt
& VT_UNSIGNED
)
1906 vtop
->c
.ll
= vtop
->c
.ui
;
1907 #ifdef TCC_TARGET_X86_64
1908 else if (sbt
== VT_PTR
)
1911 else if (sbt
!= VT_LLONG
)
1912 vtop
->c
.ll
= vtop
->c
.i
;
1914 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
1915 vtop
->c
.ull
= vtop
->c
.ll
;
1916 else if (dbt
== VT_BOOL
)
1917 vtop
->c
.i
= (vtop
->c
.ll
!= 0);
1918 else if (dbt
!= VT_LLONG
) {
1920 if ((dbt
& VT_BTYPE
) == VT_BYTE
)
1922 else if ((dbt
& VT_BTYPE
) == VT_SHORT
)
1925 if(dbt
& VT_UNSIGNED
)
1926 vtop
->c
.ui
= ((unsigned int)vtop
->c
.ll
<< s
) >> s
;
1928 vtop
->c
.i
= ((int)vtop
->c
.ll
<< s
) >> s
;
1931 } else if (p
&& dbt
== VT_BOOL
) {
1934 } else if (!nocode_wanted
) {
1935 /* non constant case: generate code */
1937 /* convert from fp to fp */
1940 /* convert int to fp */
1943 /* convert fp to int */
1944 if (dbt
== VT_BOOL
) {
1948 /* we handle char/short/etc... with generic code */
1949 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
1950 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
1954 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
1955 /* additional cast for char/short... */
1960 #ifndef TCC_TARGET_X86_64
1961 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
1962 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
1963 /* scalar to long long */
1964 /* machine independent conversion */
1966 /* generate high word */
1967 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
1971 if (sbt
== VT_PTR
) {
1972 /* cast from pointer to int before we apply
1973 shift operation, which pointers don't support*/
1974 gen_cast(&int_type
);
1980 /* patch second register */
1981 vtop
[-1].r2
= vtop
->r
;
1985 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
1986 (dbt
& VT_BTYPE
) == VT_PTR
||
1987 (dbt
& VT_BTYPE
) == VT_FUNC
) {
1988 if ((sbt
& VT_BTYPE
) != VT_LLONG
&&
1989 (sbt
& VT_BTYPE
) != VT_PTR
&&
1990 (sbt
& VT_BTYPE
) != VT_FUNC
) {
1991 /* need to convert from 32bit to 64bit */
1993 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
1994 /* x86_64 specific: movslq */
1996 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
2000 } else if (dbt
== VT_BOOL
) {
2001 /* scalar to bool */
2004 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
2005 (dbt
& VT_BTYPE
) == VT_SHORT
) {
2006 if (sbt
== VT_PTR
) {
2007 vtop
->type
.t
= VT_INT
;
2008 tcc_warning("nonportable conversion from pointer to char/short");
2010 force_charshort_cast(dbt
);
2011 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
2013 if (sbt
== VT_LLONG
) {
2014 /* from long long: just take low order word */
2018 /* if lvalue and single word type, nothing to do because
2019 the lvalue already contains the real type size (see
2020 VT_LVAL_xxx constants) */
2023 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
2024 /* if we are casting between pointer types,
2025 we must update the VT_LVAL_xxx size */
2026 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
2027 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
2032 /* return type size as known at compile time. Put alignment at 'a' */
2033 ST_FUNC
int type_size(CType
*type
, int *a
)
2038 bt
= type
->t
& VT_BTYPE
;
2039 if (bt
== VT_STRUCT
) {
2044 } else if (bt
== VT_PTR
) {
2045 if (type
->t
& VT_ARRAY
) {
2049 ts
= type_size(&s
->type
, a
);
2051 if (ts
< 0 && s
->c
< 0)
2059 } else if (bt
== VT_LDOUBLE
) {
2061 return LDOUBLE_SIZE
;
2062 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
2063 #ifdef TCC_TARGET_I386
2064 #ifdef TCC_TARGET_PE
2069 #elif defined(TCC_TARGET_ARM)
2079 } else if (bt
== VT_INT
|| bt
== VT_ENUM
|| bt
== VT_FLOAT
) {
2082 } else if (bt
== VT_SHORT
) {
2085 } else if (bt
== VT_QLONG
|| bt
== VT_QFLOAT
) {
2089 /* char, void, function, _Bool */
2095 /* push type size as known at runtime time on top of value stack. Put
2097 ST_FUNC
void vla_runtime_type_size(CType
*type
, int *a
)
2099 if (type
->t
& VT_VLA
) {
2100 vset(&int_type
, VT_LOCAL
|VT_LVAL
, type
->ref
->c
);
2102 vpushi(type_size(type
, a
));
2106 /* return the pointed type of t */
2107 static inline CType
*pointed_type(CType
*type
)
2109 return &type
->ref
->type
;
2112 /* modify type so that its it is a pointer to type. */
2113 ST_FUNC
void mk_pointer(CType
*type
)
2116 s
= sym_push(SYM_FIELD
, type
, 0, -1);
2117 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
2121 /* compare function types. OLD functions match any new functions */
2122 static int is_compatible_func(CType
*type1
, CType
*type2
)
2128 if (!is_compatible_types(&s1
->type
, &s2
->type
))
2130 /* check func_call */
2131 if (FUNC_CALL(s1
->r
) != FUNC_CALL(s2
->r
))
2133 /* XXX: not complete */
2134 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
2138 while (s1
!= NULL
) {
2141 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
2151 /* return true if type1 and type2 are the same. If unqualified is
2152 true, qualifiers on the types are ignored.
2154 - enums are not checked as gcc __builtin_types_compatible_p ()
2156 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2160 t1
= type1
->t
& VT_TYPE
;
2161 t2
= type2
->t
& VT_TYPE
;
2163 /* strip qualifiers before comparing */
2164 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2165 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2167 /* XXX: bitfields ? */
2170 /* test more complicated cases */
2171 bt1
= t1
& VT_BTYPE
;
2172 if (bt1
== VT_PTR
) {
2173 type1
= pointed_type(type1
);
2174 type2
= pointed_type(type2
);
2175 return is_compatible_types(type1
, type2
);
2176 } else if (bt1
== VT_STRUCT
) {
2177 return (type1
->ref
== type2
->ref
);
2178 } else if (bt1
== VT_FUNC
) {
2179 return is_compatible_func(type1
, type2
);
2185 /* return true if type1 and type2 are exactly the same (including
2188 static int is_compatible_types(CType
*type1
, CType
*type2
)
2190 return compare_types(type1
,type2
,0);
2193 /* return true if type1 and type2 are the same (ignoring qualifiers).
2195 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
2197 return compare_types(type1
,type2
,1);
2200 /* print a type. If 'varstr' is not NULL, then the variable is also
2201 printed in the type */
2203 /* XXX: add array and function pointers */
2204 static void type_to_str(char *buf
, int buf_size
,
2205 CType
*type
, const char *varstr
)
2212 t
= type
->t
& VT_TYPE
;
2215 if (t
& VT_CONSTANT
)
2216 pstrcat(buf
, buf_size
, "const ");
2217 if (t
& VT_VOLATILE
)
2218 pstrcat(buf
, buf_size
, "volatile ");
2219 if (t
& VT_UNSIGNED
)
2220 pstrcat(buf
, buf_size
, "unsigned ");
2250 tstr
= "long double";
2252 pstrcat(buf
, buf_size
, tstr
);
2256 if (bt
== VT_STRUCT
)
2260 pstrcat(buf
, buf_size
, tstr
);
2261 v
= type
->ref
->v
& ~SYM_STRUCT
;
2262 if (v
>= SYM_FIRST_ANOM
)
2263 pstrcat(buf
, buf_size
, "<anonymous>");
2265 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
2269 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
2270 pstrcat(buf
, buf_size
, "(");
2272 while (sa
!= NULL
) {
2273 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
2274 pstrcat(buf
, buf_size
, buf1
);
2277 pstrcat(buf
, buf_size
, ", ");
2279 pstrcat(buf
, buf_size
, ")");
2283 pstrcpy(buf1
, sizeof(buf1
), "*");
2285 pstrcat(buf1
, sizeof(buf1
), varstr
);
2286 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2290 pstrcat(buf
, buf_size
, " ");
2291 pstrcat(buf
, buf_size
, varstr
);
2296 /* verify type compatibility to store vtop in 'dt' type, and generate
2298 static void gen_assign_cast(CType
*dt
)
2300 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
2301 char buf1
[256], buf2
[256];
2304 st
= &vtop
->type
; /* source type */
2305 dbt
= dt
->t
& VT_BTYPE
;
2306 sbt
= st
->t
& VT_BTYPE
;
2308 tcc_error("Cannot assign void value");
2309 if (dt
->t
& VT_CONSTANT
)
2310 tcc_warning("assignment of read-only location");
2313 /* special cases for pointers */
2314 /* '0' can also be a pointer */
2315 if (is_null_pointer(vtop
))
2317 /* accept implicit pointer to integer cast with warning */
2318 if (is_integer_btype(sbt
)) {
2319 tcc_warning("assignment makes pointer from integer without a cast");
2322 type1
= pointed_type(dt
);
2323 /* a function is implicitely a function pointer */
2324 if (sbt
== VT_FUNC
) {
2325 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
2326 !is_compatible_types(pointed_type(dt
), st
))
2327 tcc_warning("assignment from incompatible pointer type");
2332 type2
= pointed_type(st
);
2333 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2334 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
2335 /* void * can match anything */
2337 /* exact type match, except for unsigned */
2340 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2341 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2342 if (!is_compatible_types(&tmp_type1
, &tmp_type2
))
2343 tcc_warning("assignment from incompatible pointer type");
2345 /* check const and volatile */
2346 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
2347 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
2348 tcc_warning("assignment discards qualifiers from pointer target type");
2354 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
2355 tcc_warning("assignment makes integer from pointer without a cast");
2357 /* XXX: more tests */
2362 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2363 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2364 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2366 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
2367 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
2368 tcc_error("cannot cast '%s' to '%s'", buf1
, buf2
);
2376 /* store vtop in lvalue pushed on stack */
2377 ST_FUNC
void vstore(void)
2379 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
2381 ft
= vtop
[-1].type
.t
;
2382 sbt
= vtop
->type
.t
& VT_BTYPE
;
2383 dbt
= ft
& VT_BTYPE
;
2384 if ((((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2385 (sbt
== VT_INT
&& dbt
== VT_SHORT
))
2386 && !(vtop
->type
.t
& VT_BITFIELD
)) {
2387 /* optimize char/short casts */
2388 delayed_cast
= VT_MUSTCAST
;
2389 vtop
->type
.t
= ft
& (VT_TYPE
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
)));
2390 /* XXX: factorize */
2391 if (ft
& VT_CONSTANT
)
2392 tcc_warning("assignment of read-only location");
2395 if (!(ft
& VT_BITFIELD
))
2396 gen_assign_cast(&vtop
[-1].type
);
2399 if (sbt
== VT_STRUCT
) {
2400 /* if structure, only generate pointer */
2401 /* structure assignment : generate memcpy */
2402 /* XXX: optimize if small size */
2403 if (!nocode_wanted
) {
2404 size
= type_size(&vtop
->type
, &align
);
2408 vtop
->type
.t
= VT_PTR
;
2411 /* address of memcpy() */
2414 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2415 else if(!(align
& 3))
2416 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2419 vpush_global_sym(&func_old_type
, TOK_memcpy
);
2424 vtop
->type
.t
= VT_PTR
;
2433 /* leave source on stack */
2434 } else if (ft
& VT_BITFIELD
) {
2435 /* bitfield store handling */
2436 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2437 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2438 /* remove bit field info to avoid loops */
2439 vtop
[-1].type
.t
= ft
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
2441 /* duplicate source into other register */
2446 if((ft
& VT_BTYPE
) == VT_BOOL
) {
2447 gen_cast(&vtop
[-1].type
);
2448 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2451 /* duplicate destination */
2453 vtop
[-1] = vtop
[-2];
2455 /* mask and shift source */
2456 if((ft
& VT_BTYPE
) != VT_BOOL
) {
2457 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2458 vpushll((1ULL << bit_size
) - 1ULL);
2460 vpushi((1 << bit_size
) - 1);
2466 /* load destination, mask and or with source */
2468 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2469 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2471 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2478 /* pop off shifted source from "duplicate source..." above */
2482 #ifdef CONFIG_TCC_BCHECK
2483 /* bound check case */
2484 if (vtop
[-1].r
& VT_MUSTBOUND
) {
2490 if (!nocode_wanted
) {
2494 #ifdef TCC_TARGET_X86_64
2495 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
2497 } else if ((ft
& VT_BTYPE
) == VT_QFLOAT
) {
2502 r
= gv(rc
); /* generate value */
2503 /* if lvalue was saved on stack, must read it */
2504 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
2506 t
= get_reg(RC_INT
);
2507 #ifdef TCC_TARGET_X86_64
2512 sv
.r
= VT_LOCAL
| VT_LVAL
;
2513 sv
.c
.ul
= vtop
[-1].c
.ul
;
2515 vtop
[-1].r
= t
| VT_LVAL
;
2517 /* two word case handling : store second register at word + 4 (or +8 for x86-64) */
2518 #ifdef TCC_TARGET_X86_64
2519 if (((ft
& VT_BTYPE
) == VT_QLONG
) || ((ft
& VT_BTYPE
) == VT_QFLOAT
)) {
2520 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
2522 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
2523 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
2525 vtop
[-1].type
.t
= load_type
;
2528 /* convert to int to increment easily */
2529 vtop
->type
.t
= addr_type
;
2535 vtop
[-1].type
.t
= load_type
;
2536 /* XXX: it works because r2 is spilled last ! */
2537 store(vtop
->r2
, vtop
- 1);
2543 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
2544 vtop
->r
|= delayed_cast
;
2548 /* post defines POST/PRE add. c is the token ++ or -- */
2549 ST_FUNC
void inc(int post
, int c
)
2552 vdup(); /* save lvalue */
2554 gv_dup(); /* duplicate value */
2559 vpushi(c
- TOK_MID
);
2561 vstore(); /* store value */
2563 vpop(); /* if post op, return saved value */
2566 /* Parse GNUC __attribute__ extension. Currently, the following
2567 extensions are recognized:
2568 - aligned(n) : set data/function alignment.
2569 - packed : force data alignment to 1
2570 - section(x) : generate data/code in this section.
2571 - unused : currently ignored, but may be used someday.
2572 - regparm(n) : pass function parameters in registers (i386 only)
2574 static void parse_attribute(AttributeDef
*ad
)
2578 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
2582 while (tok
!= ')') {
2583 if (tok
< TOK_IDENT
)
2584 expect("attribute name");
2592 expect("section name");
2593 ad
->section
= find_section(tcc_state
, (char *)tokc
.cstr
->data
);
2601 expect("alias(\"target\")");
2602 ad
->alias_target
= /* save string as token, for later */
2603 tok_alloc((char*)tokc
.cstr
->data
, tokc
.cstr
->size
-1)->tok
;
2612 if (n
<= 0 || (n
& (n
- 1)) != 0)
2613 tcc_error("alignment must be a positive power of two");
2630 /* currently, no need to handle it because tcc does not
2631 track unused objects */
2635 /* currently, no need to handle it because tcc does not
2636 track unused objects */
2641 ad
->func_call
= FUNC_CDECL
;
2646 ad
->func_call
= FUNC_STDCALL
;
2648 #ifdef TCC_TARGET_I386
2658 ad
->func_call
= FUNC_FASTCALL1
+ n
- 1;
2664 ad
->func_call
= FUNC_FASTCALLW
;
2671 ad
->mode
= VT_LLONG
+ 1;
2674 ad
->mode
= VT_SHORT
+ 1;
2677 ad
->mode
= VT_INT
+ 1;
2680 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
2687 ad
->func_export
= 1;
2690 ad
->func_import
= 1;
2693 if (tcc_state
->warn_unsupported
)
2694 tcc_warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
2695 /* skip parameters */
2697 int parenthesis
= 0;
2701 else if (tok
== ')')
2704 } while (parenthesis
&& tok
!= -1);
2717 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2718 static void struct_decl(CType
*type
, int u
)
2720 int a
, v
, size
, align
, maxalign
, c
, offset
;
2721 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
, prevbt
;
2722 Sym
*s
, *ss
, *ass
, **ps
;
2726 a
= tok
; /* save decl type */
2731 /* struct already defined ? return it */
2733 expect("struct/union/enum name");
2737 tcc_error("invalid type");
2744 /* we put an undefined size for struct/union */
2745 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
2746 s
->r
= 0; /* default alignment is zero as gcc */
2747 /* put struct/union/enum name in type */
2755 tcc_error("struct/union/enum already defined");
2756 /* cannot be empty */
2758 /* non empty enums are not allowed */
2759 if (a
== TOK_ENUM
) {
2763 expect("identifier");
2769 /* enum symbols have static storage */
2770 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
2771 ss
->type
.t
|= VT_STATIC
;
2776 /* NOTE: we accept a trailing comma */
2787 while (tok
!= '}') {
2788 parse_btype(&btype
, &ad
);
2794 type_decl(&type1
, &ad
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
2795 if (v
== 0 && (type1
.t
& VT_BTYPE
) != VT_STRUCT
)
2796 expect("identifier");
2797 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
2798 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
2799 tcc_error("invalid type for '%s'",
2800 get_tok_str(v
, NULL
));
2804 bit_size
= expr_const();
2805 /* XXX: handle v = 0 case for messages */
2807 tcc_error("negative width in bit-field '%s'",
2808 get_tok_str(v
, NULL
));
2809 if (v
&& bit_size
== 0)
2810 tcc_error("zero width for bit-field '%s'",
2811 get_tok_str(v
, NULL
));
2813 size
= type_size(&type1
, &align
);
2815 if (align
< ad
.aligned
)
2817 } else if (ad
.packed
) {
2819 } else if (*tcc_state
->pack_stack_ptr
) {
2820 if (align
> *tcc_state
->pack_stack_ptr
)
2821 align
= *tcc_state
->pack_stack_ptr
;
2824 if (bit_size
>= 0) {
2825 bt
= type1
.t
& VT_BTYPE
;
2832 tcc_error("bitfields must have scalar type");
2834 if (bit_size
> bsize
) {
2835 tcc_error("width of '%s' exceeds its type",
2836 get_tok_str(v
, NULL
));
2837 } else if (bit_size
== bsize
) {
2838 /* no need for bit fields */
2840 } else if (bit_size
== 0) {
2841 /* XXX: what to do if only padding in a
2843 /* zero size: means to pad */
2846 /* we do not have enough room ?
2847 did the type change?
2849 if ((bit_pos
+ bit_size
) > bsize
||
2850 bt
!= prevbt
|| a
== TOK_UNION
)
2853 /* XXX: handle LSB first */
2854 type1
.t
|= VT_BITFIELD
|
2855 (bit_pos
<< VT_STRUCT_SHIFT
) |
2856 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
2857 bit_pos
+= bit_size
;
2863 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2864 /* add new memory data only if starting
2866 if (lbit_pos
== 0) {
2867 if (a
== TOK_STRUCT
) {
2868 c
= (c
+ align
- 1) & -align
;
2877 if (align
> maxalign
)
2881 printf("add field %s offset=%d",
2882 get_tok_str(v
, NULL
), offset
);
2883 if (type1
.t
& VT_BITFIELD
) {
2884 printf(" pos=%d size=%d",
2885 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
2886 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
2891 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2893 while ((ass
= ass
->next
) != NULL
) {
2894 ss
= sym_push(ass
->v
, &ass
->type
, 0, offset
+ ass
->c
);
2899 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
2903 if (tok
== ';' || tok
== TOK_EOF
)
2910 /* store size and alignment */
2911 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
2917 /* return 0 if no type declaration. otherwise, return the basic type
2920 static int parse_btype(CType
*type
, AttributeDef
*ad
)
2922 int t
, u
, type_found
, typespec_found
, typedef_found
;
2926 memset(ad
, 0, sizeof(AttributeDef
));
2934 /* currently, we really ignore extension */
2944 if ((t
& VT_BTYPE
) != 0)
2945 tcc_error("too many basic types");
2961 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
2962 #ifndef TCC_TARGET_PE
2963 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
2965 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
2966 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
2980 if ((t
& VT_BTYPE
) == VT_LONG
) {
2981 #ifdef TCC_TARGET_PE
2982 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
2984 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
2992 struct_decl(&type1
, VT_ENUM
);
2995 type
->ref
= type1
.ref
;
2999 struct_decl(&type1
, VT_STRUCT
);
3002 /* type modifiers */
3055 /* GNUC attribute */
3056 case TOK_ATTRIBUTE1
:
3057 case TOK_ATTRIBUTE2
:
3058 parse_attribute(ad
);
3061 t
= (t
& ~VT_BTYPE
) | u
;
3069 parse_expr_type(&type1
);
3070 /* remove all storage modifiers except typedef */
3071 type1
.t
&= ~(VT_STORAGE
&~VT_TYPEDEF
);
3074 if (typespec_found
|| typedef_found
)
3077 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
3080 t
|= (s
->type
.t
& ~VT_TYPEDEF
);
3081 type
->ref
= s
->type
.ref
;
3083 /* get attributes from typedef */
3084 if (0 == ad
->aligned
)
3085 ad
->aligned
= FUNC_ALIGN(s
->r
);
3086 if (0 == ad
->func_call
)
3087 ad
->func_call
= FUNC_CALL(s
->r
);
3088 ad
->packed
|= FUNC_PACKED(s
->r
);
3097 if ((t
& (VT_SIGNED
|VT_UNSIGNED
)) == (VT_SIGNED
|VT_UNSIGNED
))
3098 tcc_error("signed and unsigned modifier");
3099 if (tcc_state
->char_is_unsigned
) {
3100 if ((t
& (VT_SIGNED
|VT_UNSIGNED
|VT_BTYPE
)) == VT_BYTE
)
3105 /* long is never used as type */
3106 if ((t
& VT_BTYPE
) == VT_LONG
)
3107 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
3108 t
= (t
& ~VT_BTYPE
) | VT_INT
;
3110 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3116 /* convert a function parameter type (array to pointer and function to
3117 function pointer) */
3118 static inline void convert_parameter_type(CType
*pt
)
3120 /* remove const and volatile qualifiers (XXX: const could be used
3121 to indicate a const function parameter */
3122 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3123 /* array must be transformed to pointer according to ANSI C */
3125 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
3130 ST_FUNC
void parse_asm_str(CString
*astr
)
3133 /* read the string */
3135 expect("string constant");
3137 while (tok
== TOK_STR
) {
3138 /* XXX: add \0 handling too ? */
3139 cstr_cat(astr
, tokc
.cstr
->data
);
3142 cstr_ccat(astr
, '\0');
3145 /* Parse an asm label and return the label
3146 * Don't forget to free the CString in the caller! */
3147 static void asm_label_instr(CString
*astr
)
3150 parse_asm_str(astr
);
3153 printf("asm_alias: \"%s\"\n", (char *)astr
->data
);
3157 static void post_type(CType
*type
, AttributeDef
*ad
)
3159 int n
, l
, t1
, arg_size
, align
;
3160 Sym
**plast
, *s
, *first
;
3165 /* function declaration */
3173 /* read param name and compute offset */
3174 if (l
!= FUNC_OLD
) {
3175 if (!parse_btype(&pt
, &ad1
)) {
3177 tcc_error("invalid type");
3184 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
3186 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3187 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
3188 tcc_error("parameter declared as void");
3189 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
3194 expect("identifier");
3198 convert_parameter_type(&pt
);
3199 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
3205 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
3212 /* if no parameters, then old type prototype */
3216 /* NOTE: const is ignored in returned type as it has a special
3217 meaning in gcc / C++ */
3218 type
->t
&= ~VT_CONSTANT
;
3219 /* some ancient pre-K&R C allows a function to return an array
3220 and the array brackets to be put after the arguments, such
3221 that "int c()[]" means something like "int[] c()" */
3224 skip(']'); /* only handle simple "[]" */
3227 /* we push a anonymous symbol which will contain the function prototype */
3228 ad
->func_args
= arg_size
;
3229 s
= sym_push(SYM_FIELD
, type
, INT_ATTR(ad
), l
);
3233 } else if (tok
== '[') {
3234 /* array definition */
3236 if (tok
== TOK_RESTRICT1
)
3241 if (!local_stack
|| nocode_wanted
)
3242 vpushi(expr_const());
3244 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3247 tcc_error("invalid array size");
3249 if (!is_integer_btype(vtop
->type
.t
& VT_BTYPE
))
3250 tcc_error("size of variable length array should be an integer");
3255 /* parse next post type */
3256 post_type(type
, ad
);
3257 t1
|= type
->t
& VT_VLA
;
3260 loc
-= type_size(&int_type
, &align
);
3264 vla_runtime_type_size(type
, &align
);
3266 vset(&int_type
, VT_LOCAL
|VT_LVAL
, loc
);
3273 /* we push an anonymous symbol which will contain the array
3275 s
= sym_push(SYM_FIELD
, type
, 0, n
);
3276 type
->t
= (t1
? VT_VLA
: VT_ARRAY
) | VT_PTR
;
3281 /* Parse a type declaration (except basic type), and return the type
3282 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3283 expected. 'type' should contain the basic type. 'ad' is the
3284 attribute definition of the basic type. It can be modified by
3287 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
3290 CType type1
, *type2
;
3291 int qualifiers
, storage
;
3293 while (tok
== '*') {
3301 qualifiers
|= VT_CONSTANT
;
3306 qualifiers
|= VT_VOLATILE
;
3314 type
->t
|= qualifiers
;
3317 /* XXX: clarify attribute handling */
3318 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3319 parse_attribute(ad
);
3321 /* recursive type */
3322 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3323 type1
.t
= 0; /* XXX: same as int */
3326 /* XXX: this is not correct to modify 'ad' at this point, but
3327 the syntax is not clear */
3328 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3329 parse_attribute(ad
);
3330 type_decl(&type1
, ad
, v
, td
);
3333 /* type identifier */
3334 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
3338 if (!(td
& TYPE_ABSTRACT
))
3339 expect("identifier");
3343 storage
= type
->t
& VT_STORAGE
;
3344 type
->t
&= ~VT_STORAGE
;
3345 if (storage
& VT_STATIC
) {
3346 int saved_nocode_wanted
= nocode_wanted
;
3348 post_type(type
, ad
);
3349 nocode_wanted
= saved_nocode_wanted
;
3351 post_type(type
, ad
);
3353 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3354 parse_attribute(ad
);
3358 /* append type at the end of type1 */
3371 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3372 ST_FUNC
int lvalue_type(int t
)
3377 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
3379 else if (bt
== VT_SHORT
)
3383 if (t
& VT_UNSIGNED
)
3384 r
|= VT_LVAL_UNSIGNED
;
3388 /* indirection with full error checking and bound check */
3389 ST_FUNC
void indir(void)
3391 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
3392 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
3396 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
3398 vtop
->type
= *pointed_type(&vtop
->type
);
3399 /* Arrays and functions are never lvalues */
3400 if (!(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_VLA
)
3401 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3402 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3403 /* if bound checking, the referenced pointer must be checked */
3404 #ifdef CONFIG_TCC_BCHECK
3405 if (tcc_state
->do_bounds_check
)
3406 vtop
->r
|= VT_MUSTBOUND
;
3411 /* pass a parameter to a function and do type checking and casting */
3412 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
3417 func_type
= func
->c
;
3418 if (func_type
== FUNC_OLD
||
3419 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
3420 /* default casting : only need to convert float to double */
3421 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
3425 } else if (arg
== NULL
) {
3426 tcc_error("too many arguments to function");
3429 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
3430 gen_assign_cast(&type
);
3434 /* parse an expression of the form '(type)' or '(expr)' and return its
3436 static void parse_expr_type(CType
*type
)
3442 if (parse_btype(type
, &ad
)) {
3443 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3450 static void parse_type(CType
*type
)
3455 if (!parse_btype(type
, &ad
)) {
3458 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3461 static void vpush_tokc(int t
)
3466 vsetc(&type
, VT_CONST
, &tokc
);
3469 ST_FUNC
void unary(void)
3471 int n
, t
, align
, size
, r
, sizeof_caller
;
3475 static int in_sizeof
= 0;
3477 sizeof_caller
= in_sizeof
;
3479 /* XXX: GCC 2.95.3 does not generate a table although it should be
3493 vpush_tokc(VT_INT
| VT_UNSIGNED
);
3497 vpush_tokc(VT_LLONG
);
3501 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
3505 vpush_tokc(VT_FLOAT
);
3509 vpush_tokc(VT_DOUBLE
);
3513 vpush_tokc(VT_LDOUBLE
);
3516 case TOK___FUNCTION__
:
3518 goto tok_identifier
;
3524 /* special function name identifier */
3525 len
= strlen(funcname
) + 1;
3526 /* generate char[len] type */
3531 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
3532 ptr
= section_ptr_add(data_section
, len
);
3533 memcpy(ptr
, funcname
, len
);
3538 #ifdef TCC_TARGET_PE
3539 t
= VT_SHORT
| VT_UNSIGNED
;
3545 /* string parsing */
3548 if (tcc_state
->warn_write_strings
)
3553 memset(&ad
, 0, sizeof(AttributeDef
));
3554 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, NULL
, 0);
3559 if (parse_btype(&type
, &ad
)) {
3560 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
3562 /* check ISOC99 compound literal */
3564 /* data is allocated locally by default */
3569 /* all except arrays are lvalues */
3570 if (!(type
.t
& VT_ARRAY
))
3571 r
|= lvalue_type(type
.t
);
3572 memset(&ad
, 0, sizeof(AttributeDef
));
3573 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, NULL
, 0);
3575 if (sizeof_caller
) {
3582 } else if (tok
== '{') {
3583 /* save all registers */
3585 /* statement expression : we do not accept break/continue
3586 inside as GCC does */
3587 block(NULL
, NULL
, NULL
, NULL
, 0, 1);
3602 /* functions names must be treated as function pointers,
3603 except for unary '&' and sizeof. Since we consider that
3604 functions are not lvalues, we only have to handle it
3605 there and in function calls. */
3606 /* arrays can also be used although they are not lvalues */
3607 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
3608 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
3610 mk_pointer(&vtop
->type
);
3616 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3618 boolean
.t
= VT_BOOL
;
3620 vtop
->c
.i
= !vtop
->c
.i
;
3621 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
3622 vtop
->c
.i
= vtop
->c
.i
^ 1;
3625 vseti(VT_JMP
, gtst(1, 0));
3636 /* in order to force cast, we add zero */
3638 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
3639 tcc_error("pointer not accepted for unary plus");
3649 unary_type(&type
); // Perform a in_sizeof = 0;
3650 size
= type_size(&type
, &align
);
3651 if (t
== TOK_SIZEOF
) {
3652 if (!(type
.t
& VT_VLA
)) {
3654 tcc_error("sizeof applied to an incomplete type");
3657 vla_runtime_type_size(&type
, &align
);
3662 vtop
->type
.t
|= VT_UNSIGNED
;
3665 case TOK_builtin_types_compatible_p
:
3674 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3675 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3676 vpushi(is_compatible_types(&type1
, &type2
));
3679 case TOK_builtin_constant_p
:
3681 int saved_nocode_wanted
, res
;
3684 saved_nocode_wanted
= nocode_wanted
;
3687 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
3689 nocode_wanted
= saved_nocode_wanted
;
3694 case TOK_builtin_frame_address
:
3700 if (tok
!= TOK_CINT
|| tokc
.i
< 0) {
3701 tcc_error("__builtin_frame_address only takes positive integers");
3708 vset(&type
, VT_LOCAL
, 0); /* local frame */
3710 mk_pointer(&vtop
->type
);
3711 indir(); /* -> parent frame */
3715 #ifdef TCC_TARGET_X86_64
3716 case TOK_builtin_va_arg_types
:
3724 vpushi(classify_x86_64_va_arg(&type
));
3743 goto tok_identifier
;
3745 /* allow to take the address of a label */
3746 if (tok
< TOK_UIDENT
)
3747 expect("label identifier");
3748 s
= label_find(tok
);
3750 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
3752 if (s
->r
== LABEL_DECLARED
)
3753 s
->r
= LABEL_FORWARD
;
3756 s
->type
.t
= VT_VOID
;
3757 mk_pointer(&s
->type
);
3758 s
->type
.t
|= VT_STATIC
;
3760 vset(&s
->type
, VT_CONST
| VT_SYM
, 0);
3765 // special qnan , snan and infinity values
3767 vpush64(VT_DOUBLE
, 0x7ff8000000000000ULL
);
3771 vpush64(VT_DOUBLE
, 0x7ff0000000000001ULL
);
3775 vpush64(VT_DOUBLE
, 0x7ff0000000000000ULL
);
3784 expect("identifier");
3788 tcc_error("'%s' undeclared", get_tok_str(t
, NULL
));
3789 /* for simple function calls, we tolerate undeclared
3790 external reference to int() function */
3791 if (tcc_state
->warn_implicit_function_declaration
)
3792 tcc_warning("implicit declaration of function '%s'",
3793 get_tok_str(t
, NULL
));
3794 s
= external_global_sym(t
, &func_old_type
, 0);
3796 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
3797 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
3798 /* if referencing an inline function, then we generate a
3799 symbol to it if not already done. It will have the
3800 effect to generate code for it at the end of the
3801 compilation unit. Inline function as always
3802 generated in the text section. */
3804 put_extern_sym(s
, text_section
, 0, 0);
3805 r
= VT_SYM
| VT_CONST
;
3809 vset(&s
->type
, r
, s
->c
);
3810 /* if forward reference, we must point to s */
3811 if (vtop
->r
& VT_SYM
) {
3818 /* post operations */
3820 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
3823 } else if (tok
== '.' || tok
== TOK_ARROW
) {
3826 if (tok
== TOK_ARROW
)
3828 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
3832 /* expect pointer on structure */
3833 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
3834 expect("struct or union");
3838 while ((s
= s
->next
) != NULL
) {
3843 tcc_error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, NULL
));
3844 /* add field offset to pointer */
3845 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
3848 /* change type to field type, and set to lvalue */
3849 vtop
->type
= s
->type
;
3850 vtop
->type
.t
|= qualifiers
;
3851 /* an array is never an lvalue */
3852 if (!(vtop
->type
.t
& VT_ARRAY
)) {
3853 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3854 #ifdef CONFIG_TCC_BCHECK
3855 /* if bound checking, the referenced pointer must be checked */
3856 if (tcc_state
->do_bounds_check
)
3857 vtop
->r
|= VT_MUSTBOUND
;
3861 } else if (tok
== '[') {
3867 } else if (tok
== '(') {
3873 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3874 /* pointer test (no array accepted) */
3875 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
3876 vtop
->type
= *pointed_type(&vtop
->type
);
3877 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
3881 expect("function pointer");
3884 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
3886 /* get return type */
3889 sa
= s
->next
; /* first parameter */
3892 /* compute first implicit argument if a structure is returned */
3893 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
3895 sret
= gfunc_sret(&s
->type
, &ret
.type
, &ret_align
);
3897 /* get some space for the returned structure */
3898 size
= type_size(&s
->type
, &align
);
3899 loc
= (loc
- size
) & -align
;
3901 ret
.r
= VT_LOCAL
| VT_LVAL
;
3902 /* pass it as 'int' to avoid structure arg passing
3904 vseti(VT_LOCAL
, loc
);
3914 /* return in register */
3915 if (is_float(ret
.type
.t
)) {
3916 ret
.r
= reg_fret(ret
.type
.t
);
3917 #ifdef TCC_TARGET_X86_64
3918 if ((ret
.type
.t
& VT_BTYPE
) == VT_QFLOAT
)
3922 #ifdef TCC_TARGET_X86_64
3923 if ((ret
.type
.t
& VT_BTYPE
) == VT_QLONG
)
3925 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
3935 gfunc_param_typed(s
, sa
);
3945 tcc_error("too few arguments to function");
3947 if (!nocode_wanted
) {
3948 gfunc_call(nb_args
);
3950 vtop
-= (nb_args
+ 1);
3953 vsetc(&ret
.type
, ret
.r
, &ret
.c
);
3955 /* handle packed struct return */
3956 if (((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) && !sret
) {
3957 size
= type_size(&s
->type
, &align
);
3958 loc
= (loc
- size
) & -align
;
3960 vset(&ret
.type
, VT_LOCAL
| VT_LVAL
, addr
);
3964 vset(&s
->type
, VT_LOCAL
| VT_LVAL
, addr
);
3972 ST_FUNC
void expr_prod(void)
3977 while (tok
== '*' || tok
== '/' || tok
== '%') {
3985 ST_FUNC
void expr_sum(void)
3990 while (tok
== '+' || tok
== '-') {
3998 static void expr_shift(void)
4003 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
4011 static void expr_cmp(void)
4016 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
4017 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
4025 static void expr_cmpeq(void)
4030 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
4038 static void expr_and(void)
4041 while (tok
== '&') {
4048 static void expr_xor(void)
4051 while (tok
== '^') {
4058 static void expr_or(void)
4061 while (tok
== '|') {
4068 /* XXX: fix this mess */
4069 static void expr_land_const(void)
4072 while (tok
== TOK_LAND
) {
4079 /* XXX: fix this mess */
4080 static void expr_lor_const(void)
4083 while (tok
== TOK_LOR
) {
4090 /* only used if non constant */
4091 static void expr_land(void)
4096 if (tok
== TOK_LAND
) {
4101 if (tok
!= TOK_LAND
) {
4111 static void expr_lor(void)
4116 if (tok
== TOK_LOR
) {
4121 if (tok
!= TOK_LOR
) {
4131 /* XXX: better constant handling */
4132 static void expr_cond(void)
4134 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
;
4136 CType type
, type1
, type2
;
4143 boolean
.t
= VT_BOOL
;
4149 if (tok
!= ':' || !gnu_ext
) {
4164 if (vtop
!= vstack
) {
4165 /* needed to avoid having different registers saved in
4167 if (is_float(vtop
->type
.t
)) {
4169 #ifdef TCC_TARGET_X86_64
4170 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4180 if (tok
== ':' && gnu_ext
) {
4188 sv
= *vtop
; /* save value to handle it later */
4189 vtop
--; /* no vpop so that FP stack is not flushed */
4197 bt1
= t1
& VT_BTYPE
;
4199 bt2
= t2
& VT_BTYPE
;
4200 /* cast operands to correct type according to ISOC rules */
4201 if (is_float(bt1
) || is_float(bt2
)) {
4202 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
4203 type
.t
= VT_LDOUBLE
;
4204 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
4209 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
4210 /* cast to biggest op */
4212 /* convert to unsigned if it does not fit in a long long */
4213 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
4214 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
4215 type
.t
|= VT_UNSIGNED
;
4216 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
4217 /* If one is a null ptr constant the result type
4219 if (is_null_pointer (vtop
))
4221 else if (is_null_pointer (&sv
))
4223 /* XXX: test pointer compatibility, C99 has more elaborate
4227 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
4228 /* XXX: test function pointer compatibility */
4229 type
= bt1
== VT_FUNC
? type1
: type2
;
4230 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
4231 /* XXX: test structure compatibility */
4232 type
= bt1
== VT_STRUCT
? type1
: type2
;
4233 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
4234 /* NOTE: as an extension, we accept void on only one side */
4237 /* integer operations */
4239 /* convert to unsigned if it does not fit in an integer */
4240 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
4241 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
4242 type
.t
|= VT_UNSIGNED
;
4245 /* now we convert second operand */
4247 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4250 if (is_float(type
.t
)) {
4252 #ifdef TCC_TARGET_X86_64
4253 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4257 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
4258 /* for long longs, we use fixed registers to avoid having
4259 to handle a complicated move */
4264 /* this is horrible, but we must also convert first
4268 /* put again first value and cast it */
4271 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4274 move_reg(r2
, r1
, type
.t
);
4281 static void expr_eq(void)
4287 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
4288 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
4289 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
4304 ST_FUNC
void gexpr(void)
4315 /* parse an expression and return its type without any side effect. */
4316 static void expr_type(CType
*type
)
4318 int saved_nocode_wanted
;
4320 saved_nocode_wanted
= nocode_wanted
;
4325 nocode_wanted
= saved_nocode_wanted
;
4328 /* parse a unary expression and return its type without any side
4330 static void unary_type(CType
*type
)
4342 /* parse a constant expression and return value in vtop. */
4343 static void expr_const1(void)
4352 /* parse an integer constant and return its value. */
4353 ST_FUNC
int expr_const(void)
4357 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
4358 expect("constant expression");
4364 /* return the label token if current token is a label, otherwise
4366 static int is_label(void)
4370 /* fast test first */
4371 if (tok
< TOK_UIDENT
)
4373 /* no need to save tokc because tok is an identifier */
4380 unget_tok(last_tok
);
4385 static void label_or_decl(int l
)
4389 /* fast test first */
4390 if (tok
>= TOK_UIDENT
)
4392 /* no need to save tokc because tok is an identifier */
4396 unget_tok(last_tok
);
4399 unget_tok(last_tok
);
4404 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
4405 int case_reg
, int is_expr
)
4408 Sym
*s
, *frame_bottom
;
4410 /* generate line number info */
4411 if (tcc_state
->do_debug
&&
4412 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
4413 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
4415 last_line_num
= file
->line_num
;
4419 /* default return value is (void) */
4421 vtop
->type
.t
= VT_VOID
;
4424 if (tok
== TOK_IF
) {
4431 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4433 if (c
== TOK_ELSE
) {
4437 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4438 gsym(d
); /* patch else jmp */
4441 } else if (tok
== TOK_WHILE
) {
4449 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4453 } else if (tok
== '{') {
4457 /* record local declaration stack position */
4459 frame_bottom
= sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4460 frame_bottom
->next
= scope_stack_bottom
;
4461 scope_stack_bottom
= frame_bottom
;
4462 llabel
= local_label_stack
;
4463 /* handle local labels declarations */
4464 if (tok
== TOK_LABEL
) {
4467 if (tok
< TOK_UIDENT
)
4468 expect("label identifier");
4469 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
4479 while (tok
!= '}') {
4480 label_or_decl(VT_LOCAL
);
4484 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4487 /* pop locally defined labels */
4488 label_pop(&local_label_stack
, llabel
);
4490 /* XXX: this solution makes only valgrind happy...
4491 triggered by gcc.c-torture/execute/20000917-1.c */
4493 switch(vtop
->type
.t
& VT_BTYPE
) {
4498 for(p
=vtop
->type
.ref
;p
;p
=p
->prev
)
4500 tcc_error("unsupported expression type");
4503 /* pop locally defined symbols */
4504 scope_stack_bottom
= scope_stack_bottom
->next
;
4505 sym_pop(&local_stack
, s
);
4507 } else if (tok
== TOK_RETURN
) {
4511 gen_assign_cast(&func_vt
);
4512 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
4513 CType type
, ret_type
;
4515 if (gfunc_sret(&func_vt
, &ret_type
, &ret_align
)) {
4516 /* if returning structure, must copy it to implicit
4517 first pointer arg location */
4520 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
4523 /* copy structure value to pointer */
4526 /* returning structure packed into registers */
4527 int size
, addr
, align
;
4528 size
= type_size(&func_vt
,&align
);
4529 if ((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) || (vtop
->c
.i
& (ret_align
-1)))
4530 && (align
& (ret_align
-1))) {
4531 loc
= (loc
- size
) & -align
;
4534 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
4537 vset(&ret_type
, VT_LOCAL
| VT_LVAL
, addr
);
4539 vtop
->type
= ret_type
;
4540 if (is_float(ret_type
.t
))
4541 gv(rc_fret(ret_type
.t
));
4545 } else if (is_float(func_vt
.t
)) {
4546 gv(rc_fret(func_vt
.t
));
4550 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
4553 rsym
= gjmp(rsym
); /* jmp */
4554 } else if (tok
== TOK_BREAK
) {
4557 tcc_error("cannot break");
4558 *bsym
= gjmp(*bsym
);
4561 } else if (tok
== TOK_CONTINUE
) {
4564 tcc_error("cannot continue");
4565 *csym
= gjmp(*csym
);
4568 } else if (tok
== TOK_FOR
) {
4573 frame_bottom
= sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4574 frame_bottom
->next
= scope_stack_bottom
;
4575 scope_stack_bottom
= frame_bottom
;
4577 /* c99 for-loop init decl? */
4578 if (!decl0(VT_LOCAL
, 1)) {
4579 /* no, regular for-loop init expr */
4603 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4607 scope_stack_bottom
= scope_stack_bottom
->next
;
4608 sym_pop(&local_stack
, s
);
4610 if (tok
== TOK_DO
) {
4615 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4626 if (tok
== TOK_SWITCH
) {
4630 /* XXX: other types than integer */
4631 case_reg
= gv(RC_INT
);
4635 b
= gjmp(0); /* jump to first case */
4637 block(&a
, csym
, &b
, &c
, case_reg
, 0);
4638 /* if no default, jmp after switch */
4646 if (tok
== TOK_CASE
) {
4653 if (gnu_ext
&& tok
== TOK_DOTS
) {
4657 tcc_warning("empty case range");
4659 /* since a case is like a label, we must skip it with a jmp */
4666 *case_sym
= gtst(1, 0);
4669 *case_sym
= gtst(1, 0);
4673 *case_sym
= gtst(1, *case_sym
);
4678 goto block_after_label
;
4680 if (tok
== TOK_DEFAULT
) {
4686 tcc_error("too many 'default'");
4689 goto block_after_label
;
4691 if (tok
== TOK_GOTO
) {
4693 if (tok
== '*' && gnu_ext
) {
4697 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
4700 } else if (tok
>= TOK_UIDENT
) {
4701 s
= label_find(tok
);
4702 /* put forward definition if needed */
4704 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4706 if (s
->r
== LABEL_DECLARED
)
4707 s
->r
= LABEL_FORWARD
;
4709 /* label already defined */
4710 if (s
->r
& LABEL_FORWARD
)
4711 s
->jnext
= gjmp(s
->jnext
);
4713 gjmp_addr(s
->jnext
);
4716 expect("label identifier");
4719 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
4727 if (s
->r
== LABEL_DEFINED
)
4728 tcc_error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
4730 s
->r
= LABEL_DEFINED
;
4732 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
4735 /* we accept this, but it is a mistake */
4738 tcc_warning("deprecated use of label at end of compound statement");
4742 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4745 /* expression case */
4760 /* t is the array or struct type. c is the array or struct
4761 address. cur_index/cur_field is the pointer to the current
4762 value. 'size_only' is true if only size info is needed (only used
4764 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
4765 int *cur_index
, Sym
**cur_field
,
4769 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
4775 if (gnu_ext
&& (l
= is_label()) != 0)
4777 while (tok
== '[' || tok
== '.') {
4779 if (!(type
->t
& VT_ARRAY
))
4780 expect("array type");
4783 index
= expr_const();
4784 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
4785 expect("invalid index");
4786 if (tok
== TOK_DOTS
&& gnu_ext
) {
4788 index_last
= expr_const();
4789 if (index_last
< 0 ||
4790 (s
->c
>= 0 && index_last
>= s
->c
) ||
4792 expect("invalid index");
4798 *cur_index
= index_last
;
4799 type
= pointed_type(type
);
4800 elem_size
= type_size(type
, &align
);
4801 c
+= index
* elem_size
;
4802 /* NOTE: we only support ranges for last designator */
4803 nb_elems
= index_last
- index
+ 1;
4804 if (nb_elems
!= 1) {
4813 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
4814 expect("struct/union type");
4827 /* XXX: fix this mess by using explicit storage field */
4829 type1
.t
|= (type
->t
& ~VT_TYPE
);
4843 if (type
->t
& VT_ARRAY
) {
4845 type
= pointed_type(type
);
4846 c
+= index
* type_size(type
, &align
);
4850 tcc_error("too many field init");
4851 /* XXX: fix this mess by using explicit storage field */
4853 type1
.t
|= (type
->t
& ~VT_TYPE
);
4858 decl_initializer(type
, sec
, c
, 0, size_only
);
4860 /* XXX: make it more general */
4861 if (!size_only
&& nb_elems
> 1) {
4862 unsigned long c_end
;
4867 tcc_error("range init not supported yet for dynamic storage");
4868 c_end
= c
+ nb_elems
* elem_size
;
4869 if (c_end
> sec
->data_allocated
)
4870 section_realloc(sec
, c_end
);
4871 src
= sec
->data
+ c
;
4873 for(i
= 1; i
< nb_elems
; i
++) {
4875 memcpy(dst
, src
, elem_size
);
4881 #define EXPR_CONST 1
4884 /* store a value or an expression directly in global data or in local array */
4885 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
4886 int v
, int expr_type
)
4888 int saved_global_expr
, bt
, bit_pos
, bit_size
;
4890 unsigned long long bit_mask
;
4898 /* compound literals must be allocated globally in this case */
4899 saved_global_expr
= global_expr
;
4902 global_expr
= saved_global_expr
;
4903 /* NOTE: symbols are accepted */
4904 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
4905 tcc_error("initializer element is not constant");
4913 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
4916 /* XXX: not portable */
4917 /* XXX: generate error if incorrect relocation */
4918 gen_assign_cast(&dtype
);
4919 bt
= type
->t
& VT_BTYPE
;
4920 /* we'll write at most 12 bytes */
4921 if (c
+ 12 > sec
->data_allocated
) {
4922 section_realloc(sec
, c
+ 12);
4924 ptr
= sec
->data
+ c
;
4925 /* XXX: make code faster ? */
4926 if (!(type
->t
& VT_BITFIELD
)) {
4931 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4932 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
4933 bit_mask
= (1LL << bit_size
) - 1;
4935 if ((vtop
->r
& VT_SYM
) &&
4941 (bt
== VT_INT
&& bit_size
!= 32)))
4942 tcc_error("initializer element is not computable at load time");
4945 vtop
->c
.i
= (vtop
->c
.i
!= 0);
4947 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4950 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4953 *(double *)ptr
= vtop
->c
.d
;
4956 *(long double *)ptr
= vtop
->c
.ld
;
4959 *(long long *)ptr
|= (vtop
->c
.ll
& bit_mask
) << bit_pos
;
4962 if (vtop
->r
& VT_SYM
) {
4963 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
4965 *(int *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4970 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
4977 /* put zeros for variable based init */
4978 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
4981 /* nothing to do because globals are already set to zero */
4983 vpush_global_sym(&func_old_type
, TOK_memset
);
4991 /* 't' contains the type and storage info. 'c' is the offset of the
4992 object in section 'sec'. If 'sec' is NULL, it means stack based
4993 allocation. 'first' is true if array '{' must be read (multi
4994 dimension implicit array init handling). 'size_only' is true if
4995 size only evaluation is wanted (only for arrays). */
4996 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
4997 int first
, int size_only
)
4999 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, parlevel1
, i
;
5000 int size1
, align1
, expr_type
;
5004 if (type
->t
& VT_VLA
) {
5005 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
5009 vpush_global_sym(&func_old_type
, TOK_alloca
);
5010 vla_runtime_type_size(type
, &a
);
5015 vsetc(type
, REG_IRET
, &retcval
);
5016 vset(type
, VT_LOCAL
|VT_LVAL
, c
);
5021 tcc_error("variable length arrays unsupported for this target");
5023 } else if (type
->t
& VT_ARRAY
) {
5027 t1
= pointed_type(type
);
5028 size1
= type_size(t1
, &align1
);
5031 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
5034 tcc_error("character array initializer must be a literal,"
5035 " optionally enclosed in braces");
5040 /* only parse strings here if correct type (otherwise: handle
5041 them as ((w)char *) expressions */
5042 if ((tok
== TOK_LSTR
&&
5043 #ifdef TCC_TARGET_PE
5044 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
5046 (t1
->t
& VT_BTYPE
) == VT_INT
5048 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
5049 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5054 /* compute maximum number of chars wanted */
5056 cstr_len
= cstr
->size
;
5058 cstr_len
= cstr
->size
/ sizeof(nwchar_t
);
5061 if (n
>= 0 && nb
> (n
- array_length
))
5062 nb
= n
- array_length
;
5065 tcc_warning("initializer-string for array is too long");
5066 /* in order to go faster for common case (char
5067 string in global variable, we handle it
5069 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
5070 memcpy(sec
->data
+ c
+ array_length
, cstr
->data
, nb
);
5074 ch
= ((unsigned char *)cstr
->data
)[i
];
5076 ch
= ((nwchar_t
*)cstr
->data
)[i
];
5077 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
5085 /* only add trailing zero if enough storage (no
5086 warning in this case since it is standard) */
5087 if (n
< 0 || array_length
< n
) {
5089 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
5095 while (tok
!= '}') {
5096 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
5097 if (n
>= 0 && index
>= n
)
5098 tcc_error("index too large");
5099 /* must put zero in holes (note that doing it that way
5100 ensures that it even works with designators) */
5101 if (!size_only
&& array_length
< index
) {
5102 init_putz(t1
, sec
, c
+ array_length
* size1
,
5103 (index
- array_length
) * size1
);
5106 if (index
> array_length
)
5107 array_length
= index
;
5108 /* special test for multi dimensional arrays (may not
5109 be strictly correct if designators are used at the
5111 if (index
>= n
&& no_oblock
)
5120 /* put zeros at the end */
5121 if (!size_only
&& n
>= 0 && array_length
< n
) {
5122 init_putz(t1
, sec
, c
+ array_length
* size1
,
5123 (n
- array_length
) * size1
);
5125 /* patch type size if needed */
5127 s
->c
= array_length
;
5128 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
5129 (sec
|| !first
|| tok
== '{')) {
5132 /* NOTE: the previous test is a specific case for automatic
5133 struct/union init */
5134 /* XXX: union needs only one init */
5136 /* XXX: this test is incorrect for local initializers
5137 beginning with ( without {. It would be much more difficult
5138 to do it correctly (ideally, the expression parser should
5139 be used in all cases) */
5145 while (tok
== '(') {
5149 if (!parse_btype(&type1
, &ad1
))
5151 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
5153 if (!is_assignable_types(type
, &type1
))
5154 tcc_error("invalid type for cast");
5159 if (first
|| tok
== '{') {
5168 while (tok
!= '}') {
5169 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
5171 if (!size_only
&& array_length
< index
) {
5172 init_putz(type
, sec
, c
+ array_length
,
5173 index
- array_length
);
5175 index
= index
+ type_size(&f
->type
, &align1
);
5176 if (index
> array_length
)
5177 array_length
= index
;
5179 /* gr: skip fields from same union - ugly. */
5181 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
5182 /* test for same offset */
5183 if (f
->next
->c
!= f
->c
)
5185 /* if yes, test for bitfield shift */
5186 if ((f
->type
.t
& VT_BITFIELD
) && (f
->next
->type
.t
& VT_BITFIELD
)) {
5187 int bit_pos_1
= (f
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5188 int bit_pos_2
= (f
->next
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5189 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
5190 if (bit_pos_1
!= bit_pos_2
)
5197 if (no_oblock
&& f
== NULL
)
5203 /* put zeros at the end */
5204 if (!size_only
&& array_length
< n
) {
5205 init_putz(type
, sec
, c
+ array_length
,
5214 } else if (tok
== '{') {
5216 decl_initializer(type
, sec
, c
, first
, size_only
);
5218 } else if (size_only
) {
5219 /* just skip expression */
5220 parlevel
= parlevel1
= 0;
5221 while ((parlevel
> 0 || parlevel1
> 0 ||
5222 (tok
!= '}' && tok
!= ',')) && tok
!= -1) {
5225 else if (tok
== ')')
5227 else if (tok
== '{')
5229 else if (tok
== '}')
5234 /* currently, we always use constant expression for globals
5235 (may change for scripting case) */
5236 expr_type
= EXPR_CONST
;
5238 expr_type
= EXPR_ANY
;
5239 init_putv(type
, sec
, c
, 0, expr_type
);
5243 /* parse an initializer for type 't' if 'has_init' is non zero, and
5244 allocate space in local or global data space ('r' is either
5245 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5246 variable 'v' with an associated name represented by 'asm_label' of
5247 scope 'scope' is declared before initializers are parsed. If 'v' is
5248 zero, then a reference to the new object is put in the value stack.
5249 If 'has_init' is 2, a special parsing is done to handle string
5251 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
5252 int has_init
, int v
, char *asm_label
,
5255 int size
, align
, addr
, data_offset
;
5257 ParseState saved_parse_state
= {0};
5258 TokenString init_str
;
5260 Sym
*flexible_array
;
5262 flexible_array
= NULL
;
5263 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
5266 while (field
&& field
->next
)
5267 field
= field
->next
;
5268 if (field
->type
.t
& VT_ARRAY
&& field
->type
.ref
->c
< 0)
5269 flexible_array
= field
;
5272 size
= type_size(type
, &align
);
5273 /* If unknown size, we must evaluate it before
5274 evaluating initializers because
5275 initializers can generate global data too
5276 (e.g. string pointers or ISOC99 compound
5277 literals). It also simplifies local
5278 initializers handling */
5279 tok_str_new(&init_str
);
5280 if (size
< 0 || (flexible_array
&& has_init
)) {
5282 tcc_error("unknown type size");
5283 /* get all init string */
5284 if (has_init
== 2) {
5285 /* only get strings */
5286 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5287 tok_str_add_tok(&init_str
);
5292 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
5294 tcc_error("unexpected end of file in initializer");
5295 tok_str_add_tok(&init_str
);
5298 else if (tok
== '}') {
5308 tok_str_add(&init_str
, -1);
5309 tok_str_add(&init_str
, 0);
5312 save_parse_state(&saved_parse_state
);
5314 macro_ptr
= init_str
.str
;
5316 decl_initializer(type
, NULL
, 0, 1, 1);
5317 /* prepare second initializer parsing */
5318 macro_ptr
= init_str
.str
;
5321 /* if still unknown size, error */
5322 size
= type_size(type
, &align
);
5324 tcc_error("unknown type size");
5327 size
+= flexible_array
->type
.ref
->c
* pointed_size(&flexible_array
->type
);
5328 /* take into account specified alignment if bigger */
5330 if (ad
->aligned
> align
)
5331 align
= ad
->aligned
;
5332 } else if (ad
->packed
) {
5335 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
5337 #ifdef CONFIG_TCC_BCHECK
5338 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5342 loc
= (loc
- size
) & -align
;
5344 #ifdef CONFIG_TCC_BCHECK
5345 /* handles bounds */
5346 /* XXX: currently, since we do only one pass, we cannot track
5347 '&' operators, so we add only arrays */
5348 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5349 unsigned long *bounds_ptr
;
5350 /* add padding between regions */
5352 /* then add local bound info */
5353 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(unsigned long));
5354 bounds_ptr
[0] = addr
;
5355 bounds_ptr
[1] = size
;
5359 /* local variable */
5360 sym_push(v
, type
, r
, addr
);
5362 /* push local reference */
5363 vset(type
, r
, addr
);
5369 if (v
&& scope
== VT_CONST
) {
5370 /* see if the symbol was already defined */
5373 if (!is_compatible_types(&sym
->type
, type
))
5374 tcc_error("incompatible types for redefinition of '%s'",
5375 get_tok_str(v
, NULL
));
5376 if (sym
->type
.t
& VT_EXTERN
) {
5377 /* if the variable is extern, it was not allocated */
5378 sym
->type
.t
&= ~VT_EXTERN
;
5379 /* set array size if it was ommited in extern
5381 if ((sym
->type
.t
& VT_ARRAY
) &&
5382 sym
->type
.ref
->c
< 0 &&
5384 sym
->type
.ref
->c
= type
->ref
->c
;
5386 /* we accept several definitions of the same
5387 global variable. this is tricky, because we
5388 must play with the SHN_COMMON type of the symbol */
5389 /* XXX: should check if the variable was already
5390 initialized. It is incorrect to initialized it
5392 /* no init data, we won't add more to the symbol */
5399 /* allocate symbol in corresponding section */
5404 else if (tcc_state
->nocommon
)
5408 data_offset
= sec
->data_offset
;
5409 data_offset
= (data_offset
+ align
- 1) & -align
;
5411 /* very important to increment global pointer at this time
5412 because initializers themselves can create new initializers */
5413 data_offset
+= size
;
5414 #ifdef CONFIG_TCC_BCHECK
5415 /* add padding if bound check */
5416 if (tcc_state
->do_bounds_check
)
5419 sec
->data_offset
= data_offset
;
5420 /* allocate section space to put the data */
5421 if (sec
->sh_type
!= SHT_NOBITS
&&
5422 data_offset
> sec
->data_allocated
)
5423 section_realloc(sec
, data_offset
);
5424 /* align section if needed */
5425 if (align
> sec
->sh_addralign
)
5426 sec
->sh_addralign
= align
;
5428 addr
= 0; /* avoid warning */
5432 if (scope
!= VT_CONST
|| !sym
) {
5433 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
5434 sym
->asm_label
= asm_label
;
5436 /* update symbol definition */
5438 put_extern_sym(sym
, sec
, addr
, size
);
5441 /* put a common area */
5442 put_extern_sym(sym
, NULL
, align
, size
);
5443 /* XXX: find a nicer way */
5444 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
5445 esym
->st_shndx
= SHN_COMMON
;
5450 /* push global reference */
5451 sym
= get_sym_ref(type
, sec
, addr
, size
);
5453 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
5456 /* patch symbol weakness */
5457 if (type
->t
& VT_WEAK
)
5459 #ifdef CONFIG_TCC_BCHECK
5460 /* handles bounds now because the symbol must be defined
5461 before for the relocation */
5462 if (tcc_state
->do_bounds_check
) {
5463 unsigned long *bounds_ptr
;
5465 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
5466 /* then add global bound info */
5467 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(long));
5468 bounds_ptr
[0] = 0; /* relocated */
5469 bounds_ptr
[1] = size
;
5473 if (has_init
|| (type
->t
& VT_VLA
)) {
5474 decl_initializer(type
, sec
, addr
, 1, 0);
5475 /* restore parse state if needed */
5477 tok_str_free(init_str
.str
);
5478 restore_parse_state(&saved_parse_state
);
5480 /* patch flexible array member size back to -1, */
5481 /* for possible subsequent similar declarations */
5483 flexible_array
->type
.ref
->c
= -1;
5488 static void put_func_debug(Sym
*sym
)
5493 /* XXX: we put here a dummy type */
5494 snprintf(buf
, sizeof(buf
), "%s:%c1",
5495 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
5496 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
5497 cur_text_section
, sym
->c
);
5498 /* //gr gdb wants a line at the function */
5499 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
5504 /* parse an old style function declaration list */
5505 /* XXX: check multiple parameter */
5506 static void func_decl_list(Sym
*func_sym
)
5513 /* parse each declaration */
5514 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
&&
5515 tok
!= TOK_ASM1
&& tok
!= TOK_ASM2
&& tok
!= TOK_ASM3
) {
5516 if (!parse_btype(&btype
, &ad
))
5517 expect("declaration list");
5518 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5519 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5521 /* we accept no variable after */
5525 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5526 /* find parameter in function parameter list */
5529 if ((s
->v
& ~SYM_FIELD
) == v
)
5533 tcc_error("declaration for parameter '%s' but no such parameter",
5534 get_tok_str(v
, NULL
));
5536 /* check that no storage specifier except 'register' was given */
5537 if (type
.t
& VT_STORAGE
)
5538 tcc_error("storage class specified for '%s'", get_tok_str(v
, NULL
));
5539 convert_parameter_type(&type
);
5540 /* we can add the type (NOTE: it could be local to the function) */
5542 /* accept other parameters */
5553 /* parse a function defined by symbol 'sym' and generate its code in
5554 'cur_text_section' */
5555 static void gen_function(Sym
*sym
)
5557 int saved_nocode_wanted
= nocode_wanted
;
5559 ind
= cur_text_section
->data_offset
;
5560 /* NOTE: we patch the symbol size later */
5561 put_extern_sym(sym
, cur_text_section
, ind
, 0);
5562 funcname
= get_tok_str(sym
->v
, NULL
);
5564 /* put debug symbol */
5565 if (tcc_state
->do_debug
)
5566 put_func_debug(sym
);
5567 /* push a dummy symbol to enable local sym storage */
5568 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
5569 gfunc_prolog(&sym
->type
);
5571 block(NULL
, NULL
, NULL
, NULL
, 0, 0);
5574 cur_text_section
->data_offset
= ind
;
5575 label_pop(&global_label_stack
, NULL
);
5576 /* reset local stack */
5577 scope_stack_bottom
= NULL
;
5578 sym_pop(&local_stack
, NULL
);
5579 /* end of function */
5580 /* patch symbol size */
5581 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
5583 /* patch symbol weakness (this definition overrules any prototype) */
5584 if (sym
->type
.t
& VT_WEAK
)
5586 if (tcc_state
->do_debug
) {
5587 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
5589 /* It's better to crash than to generate wrong code */
5590 cur_text_section
= NULL
;
5591 funcname
= ""; /* for safety */
5592 func_vt
.t
= VT_VOID
; /* for safety */
5593 ind
= 0; /* for safety */
5594 nocode_wanted
= saved_nocode_wanted
;
5597 ST_FUNC
void gen_inline_functions(void)
5600 int *str
, inline_generated
, i
;
5601 struct InlineFunc
*fn
;
5603 /* iterate while inline function are referenced */
5605 inline_generated
= 0;
5606 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5607 fn
= tcc_state
->inline_fns
[i
];
5609 if (sym
&& sym
->c
) {
5610 /* the function was used: generate its code and
5611 convert it to a normal function */
5612 str
= fn
->token_str
;
5615 pstrcpy(file
->filename
, sizeof file
->filename
, fn
->filename
);
5616 sym
->r
= VT_SYM
| VT_CONST
;
5617 sym
->type
.t
&= ~VT_INLINE
;
5621 cur_text_section
= text_section
;
5623 macro_ptr
= NULL
; /* fail safe */
5625 inline_generated
= 1;
5628 if (!inline_generated
)
5631 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5632 fn
= tcc_state
->inline_fns
[i
];
5633 str
= fn
->token_str
;
5636 dynarray_reset(&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
);
5639 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
5640 static int decl0(int l
, int is_for_loop_init
)
5648 if (!parse_btype(&btype
, &ad
)) {
5649 if (is_for_loop_init
)
5651 /* skip redundant ';' */
5652 /* XXX: find more elegant solution */
5657 if (l
== VT_CONST
&&
5658 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5659 /* global asm block */
5663 /* special test for old K&R protos without explicit int
5664 type. Only accepted when defining global data */
5665 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
5669 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5670 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5672 /* we accept no variable after */
5676 while (1) { /* iterate thru each declaration */
5677 char *asm_label
; // associated asm label
5679 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5683 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
5684 printf("type = '%s'\n", buf
);
5687 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5688 if ((type
.t
& VT_STATIC
) && (l
== VT_LOCAL
)) {
5689 tcc_error("function without file scope cannot be static");
5691 /* if old style function prototype, we accept a
5694 if (sym
->c
== FUNC_OLD
)
5695 func_decl_list(sym
);
5699 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5702 asm_label_instr(&astr
);
5703 asm_label
= tcc_strdup(astr
.data
);
5706 /* parse one last attribute list, after asm label */
5707 parse_attribute(&ad
);
5712 #ifdef TCC_TARGET_PE
5714 type
.t
|= VT_IMPORT
;
5716 type
.t
|= VT_EXPORT
;
5720 tcc_error("cannot use local functions");
5721 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
5722 expect("function definition");
5724 /* reject abstract declarators in function definition */
5726 while ((sym
= sym
->next
) != NULL
)
5727 if (!(sym
->v
& ~SYM_FIELD
))
5728 expect("identifier");
5730 /* XXX: cannot do better now: convert extern line to static inline */
5731 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
5732 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5736 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
5739 r
= sym
->type
.ref
->r
;
5740 /* use func_call from prototype if not defined */
5741 if (FUNC_CALL(r
) != FUNC_CDECL
5742 && FUNC_CALL(type
.ref
->r
) == FUNC_CDECL
)
5743 FUNC_CALL(type
.ref
->r
) = FUNC_CALL(r
);
5745 /* use export from prototype */
5747 FUNC_EXPORT(type
.ref
->r
) = 1;
5749 /* use static from prototype */
5750 if (sym
->type
.t
& VT_STATIC
)
5751 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5753 if (!is_compatible_types(&sym
->type
, &type
)) {
5755 tcc_error("incompatible types for redefinition of '%s'",
5756 get_tok_str(v
, NULL
));
5758 /* if symbol is already defined, then put complete type */
5761 /* put function symbol */
5762 sym
= global_identifier_push(v
, type
.t
, 0);
5763 sym
->type
.ref
= type
.ref
;
5766 /* static inline functions are just recorded as a kind
5767 of macro. Their code will be emitted at the end of
5768 the compilation unit only if they are used */
5769 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
5770 (VT_INLINE
| VT_STATIC
)) {
5771 TokenString func_str
;
5773 struct InlineFunc
*fn
;
5774 const char *filename
;
5776 tok_str_new(&func_str
);
5782 tcc_error("unexpected end of file");
5783 tok_str_add_tok(&func_str
);
5788 } else if (t
== '}') {
5790 if (block_level
== 0)
5794 tok_str_add(&func_str
, -1);
5795 tok_str_add(&func_str
, 0);
5796 filename
= file
? file
->filename
: "";
5797 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
5798 strcpy(fn
->filename
, filename
);
5800 fn
->token_str
= func_str
.str
;
5801 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
5804 /* compute text section */
5805 cur_text_section
= ad
.section
;
5806 if (!cur_text_section
)
5807 cur_text_section
= text_section
;
5808 sym
->r
= VT_SYM
| VT_CONST
;
5813 if (btype
.t
& VT_TYPEDEF
) {
5814 /* save typedefed type */
5815 /* XXX: test storage specifiers ? */
5816 sym
= sym_push(v
, &type
, INT_ATTR(&ad
), 0);
5817 sym
->type
.t
|= VT_TYPEDEF
;
5820 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5821 /* external function definition */
5822 /* specific case for func_call attribute */
5823 type
.ref
->r
= INT_ATTR(&ad
);
5824 } else if (!(type
.t
& VT_ARRAY
)) {
5825 /* not lvalue if array */
5826 r
|= lvalue_type(type
.t
);
5828 has_init
= (tok
== '=');
5829 if (has_init
&& (type
.t
& VT_VLA
))
5830 tcc_error("Variable length array cannot be initialized");
5831 if ((btype
.t
& VT_EXTERN
) || ((type
.t
& VT_BTYPE
) == VT_FUNC
) ||
5832 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
5833 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
5834 /* external variable or function */
5835 /* NOTE: as GCC, uninitialized global static
5836 arrays of null size are considered as
5838 sym
= external_sym(v
, &type
, r
, asm_label
);
5840 if (type
.t
& VT_WEAK
)
5843 if (ad
.alias_target
) {
5848 alias_target
= sym_find(ad
.alias_target
);
5849 if (!alias_target
|| !alias_target
->c
)
5850 tcc_error("unsupported forward __alias__ attribute");
5851 esym
= &((Elf32_Sym
*)symtab_section
->data
)[alias_target
->c
];
5852 tsec
.sh_num
= esym
->st_shndx
;
5853 put_extern_sym2(sym
, &tsec
, esym
->st_value
, esym
->st_size
, 0);
5856 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
5857 if (type
.t
& VT_STATIC
)
5863 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, asm_label
, l
);
5867 if (is_for_loop_init
)
5880 ST_FUNC
void decl(int l
)