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
)) {
794 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
797 unsigned long long ll
;
798 /* two register type load : expand to two words
800 #ifndef TCC_TARGET_X86_64
801 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
804 vtop
->c
.ui
= ll
; /* first word */
806 vtop
->r
= r
; /* save register value */
807 vpushi(ll
>> 32); /* second word */
810 if (r
>= VT_CONST
|| /* XXX: test to VT_CONST incorrect ? */
811 (vtop
->r
& VT_LVAL
)) {
812 #ifdef TCC_TARGET_X86_64
813 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
815 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
817 /* We do not want to modifier the long long
818 pointer here, so the safest (and less
819 efficient) is to save all the other registers
820 in the stack. XXX: totally inefficient. */
822 /* load from memory */
823 vtop
->type
.t
= load_type
;
826 vtop
[-1].r
= r
; /* save register value */
827 /* increment pointer to get second word */
828 vtop
->type
.t
= addr_type
;
833 vtop
->type
.t
= load_type
;
838 vtop
[-1].r
= r
; /* save register value */
839 vtop
->r
= vtop
[-1].r2
;
841 /* Allocate second register. Here we rely on the fact that
842 get_reg() tries first to free r2 of an SValue. */
846 /* write second register */
848 } else if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
850 /* lvalue of scalar type : need to use lvalue type
851 because of possible cast */
854 /* compute memory access type */
855 if (vtop
->r
& VT_LVAL_BYTE
)
857 else if (vtop
->r
& VT_LVAL_SHORT
)
859 if (vtop
->r
& VT_LVAL_UNSIGNED
)
863 /* restore wanted type */
866 /* one register type load */
871 #ifdef TCC_TARGET_C67
872 /* uses register pairs for doubles */
873 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
880 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
881 ST_FUNC
void gv2(int rc1
, int rc2
)
885 /* generate more generic register first. But VT_JMP or VT_CMP
886 values must be generated first in all cases to avoid possible
888 v
= vtop
[0].r
& VT_VALMASK
;
889 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
894 /* test if reload is needed for first register */
895 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
905 /* test if reload is needed for first register */
906 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
912 /* wrapper around RC_FRET to return a register by type */
913 static int rc_fret(int t
)
915 #ifdef TCC_TARGET_X86_64
916 if (t
== VT_LDOUBLE
) {
923 /* wrapper around REG_FRET to return a register by type */
924 static int reg_fret(int t
)
926 #ifdef TCC_TARGET_X86_64
927 if (t
== VT_LDOUBLE
) {
934 /* expand long long on stack in two int registers */
935 static void lexpand(void)
939 u
= vtop
->type
.t
& VT_UNSIGNED
;
942 vtop
[0].r
= vtop
[-1].r2
;
943 vtop
[0].r2
= VT_CONST
;
944 vtop
[-1].r2
= VT_CONST
;
945 vtop
[0].type
.t
= VT_INT
| u
;
946 vtop
[-1].type
.t
= VT_INT
| u
;
949 #ifdef TCC_TARGET_ARM
950 /* expand long long on stack */
951 ST_FUNC
void lexpand_nr(void)
955 u
= vtop
->type
.t
& VT_UNSIGNED
;
958 vtop
->type
.t
= VT_INT
| u
;
959 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
961 vtop
[-1].c
.ui
= vtop
->c
.ull
;
962 vtop
->c
.ui
= vtop
->c
.ull
>> 32;
964 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
966 vtop
->r
= vtop
[-1].r
;
967 } else if (v
> VT_CONST
) {
971 vtop
->r
= vtop
[-1].r2
;
972 vtop
[-1].r2
= VT_CONST
;
973 vtop
[-1].type
.t
= VT_INT
| u
;
977 /* build a long long from two ints */
978 static void lbuild(int t
)
981 vtop
[-1].r2
= vtop
[0].r
;
986 /* rotate n first stack elements to the bottom
987 I1 ... In -> I2 ... In I1 [top is right]
989 ST_FUNC
void vrotb(int n
)
1000 /* rotate the n elements before entry e towards the top
1001 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
1003 ST_FUNC
void vrote(SValue
*e
, int n
)
1009 for(i
= 0;i
< n
- 1; i
++)
1014 /* rotate n first stack elements to the top
1015 I1 ... In -> In I1 ... I(n-1) [top is right]
1017 ST_FUNC
void vrott(int n
)
1022 /* pop stack value */
1023 ST_FUNC
void vpop(void)
1026 v
= vtop
->r
& VT_VALMASK
;
1027 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1028 /* for x86, we need to pop the FP stack */
1029 if (v
== TREG_ST0
&& !nocode_wanted
) {
1030 o(0xd8dd); /* fstp %st(0) */
1033 if (v
== VT_JMP
|| v
== VT_JMPI
) {
1034 /* need to put correct jump if && or || without test */
1040 /* convert stack entry to register and duplicate its value in another
1042 static void gv_dup(void)
1048 if ((t
& VT_BTYPE
) == VT_LLONG
) {
1055 /* stack: H L L1 H1 */
1063 /* duplicate value */
1068 #ifdef TCC_TARGET_X86_64
1069 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1079 load(r1
, &sv
); /* move r to r1 */
1081 /* duplicates value */
1087 #ifndef TCC_TARGET_X86_64
1088 /* generate CPU independent (unsigned) long long operations */
1089 static void gen_opl(int op
)
1091 int t
, a
, b
, op1
, c
, i
;
1093 unsigned short reg_iret
= REG_IRET
;
1094 unsigned short reg_lret
= REG_LRET
;
1100 func
= TOK___divdi3
;
1103 func
= TOK___udivdi3
;
1106 func
= TOK___moddi3
;
1109 func
= TOK___umoddi3
;
1116 /* call generic long long function */
1117 vpush_global_sym(&func_old_type
, func
);
1122 vtop
->r2
= reg_lret
;
1135 /* stack: L1 H1 L2 H2 */
1140 vtop
[-2] = vtop
[-3];
1143 /* stack: H1 H2 L1 L2 */
1149 /* stack: H1 H2 L1 L2 ML MH */
1152 /* stack: ML MH H1 H2 L1 L2 */
1156 /* stack: ML MH H1 L2 H2 L1 */
1161 /* stack: ML MH M1 M2 */
1164 } else if (op
== '+' || op
== '-') {
1165 /* XXX: add non carry method too (for MIPS or alpha) */
1171 /* stack: H1 H2 (L1 op L2) */
1174 gen_op(op1
+ 1); /* TOK_xxxC2 */
1177 /* stack: H1 H2 (L1 op L2) */
1180 /* stack: (L1 op L2) H1 H2 */
1182 /* stack: (L1 op L2) (H1 op H2) */
1190 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1191 t
= vtop
[-1].type
.t
;
1195 /* stack: L H shift */
1197 /* constant: simpler */
1198 /* NOTE: all comments are for SHL. the other cases are
1199 done by swaping words */
1210 if (op
!= TOK_SAR
) {
1243 /* XXX: should provide a faster fallback on x86 ? */
1246 func
= TOK___ashrdi3
;
1249 func
= TOK___lshrdi3
;
1252 func
= TOK___ashldi3
;
1258 /* compare operations */
1264 /* stack: L1 H1 L2 H2 */
1266 vtop
[-1] = vtop
[-2];
1268 /* stack: L1 L2 H1 H2 */
1271 /* when values are equal, we need to compare low words. since
1272 the jump is inverted, we invert the test too. */
1275 else if (op1
== TOK_GT
)
1277 else if (op1
== TOK_ULT
)
1279 else if (op1
== TOK_UGT
)
1284 if (op1
!= TOK_NE
) {
1288 /* generate non equal test */
1289 /* XXX: NOT PORTABLE yet */
1293 #if defined(TCC_TARGET_I386)
1294 b
= psym(0x850f, 0);
1295 #elif defined(TCC_TARGET_ARM)
1297 o(0x1A000000 | encbranch(ind
, 0, 1));
1298 #elif defined(TCC_TARGET_C67)
1299 tcc_error("not implemented");
1301 #error not supported
1305 /* compare low. Always unsigned */
1309 else if (op1
== TOK_LE
)
1311 else if (op1
== TOK_GT
)
1313 else if (op1
== TOK_GE
)
1324 /* handle integer constant optimizations and various machine
1326 static void gen_opic(int op
)
1328 int c1
, c2
, t1
, t2
, n
;
1331 typedef unsigned long long U
;
1335 t1
= v1
->type
.t
& VT_BTYPE
;
1336 t2
= v2
->type
.t
& VT_BTYPE
;
1340 else if (v1
->type
.t
& VT_UNSIGNED
)
1347 else if (v2
->type
.t
& VT_UNSIGNED
)
1352 /* currently, we cannot do computations with forward symbols */
1353 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1354 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
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;
1362 case '*': l1
*= l2
; break;
1369 /* if division by zero, generate explicit division */
1372 tcc_error("division by zero in constant");
1376 default: l1
/= l2
; break;
1377 case '%': l1
%= l2
; break;
1378 case TOK_UDIV
: l1
= (U
)l1
/ l2
; break;
1379 case TOK_UMOD
: l1
= (U
)l1
% l2
; break;
1382 case TOK_SHL
: l1
<<= l2
; break;
1383 case TOK_SHR
: l1
= (U
)l1
>> l2
; break;
1384 case TOK_SAR
: l1
>>= l2
; break;
1386 case TOK_ULT
: l1
= (U
)l1
< (U
)l2
; break;
1387 case TOK_UGE
: l1
= (U
)l1
>= (U
)l2
; break;
1388 case TOK_EQ
: l1
= l1
== l2
; break;
1389 case TOK_NE
: l1
= l1
!= l2
; break;
1390 case TOK_ULE
: l1
= (U
)l1
<= (U
)l2
; break;
1391 case TOK_UGT
: l1
= (U
)l1
> (U
)l2
; break;
1392 case TOK_LT
: l1
= l1
< l2
; break;
1393 case TOK_GE
: l1
= l1
>= l2
; break;
1394 case TOK_LE
: l1
= l1
<= l2
; break;
1395 case TOK_GT
: l1
= l1
> l2
; break;
1397 case TOK_LAND
: l1
= l1
&& l2
; break;
1398 case TOK_LOR
: l1
= l1
|| l2
; break;
1405 /* if commutative ops, put c2 as constant */
1406 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1407 op
== '|' || op
== '*')) {
1409 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1410 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1412 /* Filter out NOP operations like x*1, x-0, x&-1... */
1413 if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1416 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1417 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1423 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1424 /* try to use shifts instead of muls or divs */
1425 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1434 else if (op
== TOK_PDIV
)
1440 } else if (c2
&& (op
== '+' || op
== '-') &&
1441 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
1442 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1443 /* symbol + constant case */
1450 if (!nocode_wanted
) {
1451 /* call low level op generator */
1452 if (t1
== VT_LLONG
|| t2
== VT_LLONG
)
1463 /* generate a floating point operation with constant propagation */
1464 static void gen_opif(int op
)
1472 /* currently, we cannot do computations with forward symbols */
1473 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1474 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1476 if (v1
->type
.t
== VT_FLOAT
) {
1479 } else if (v1
->type
.t
== VT_DOUBLE
) {
1487 /* NOTE: we only do constant propagation if finite number (not
1488 NaN or infinity) (ANSI spec) */
1489 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1493 case '+': f1
+= f2
; break;
1494 case '-': f1
-= f2
; break;
1495 case '*': f1
*= f2
; break;
1499 tcc_error("division by zero in constant");
1504 /* XXX: also handles tests ? */
1508 /* XXX: overflow test ? */
1509 if (v1
->type
.t
== VT_FLOAT
) {
1511 } else if (v1
->type
.t
== VT_DOUBLE
) {
1519 if (!nocode_wanted
) {
1527 static int pointed_size(CType
*type
)
1530 return type_size(pointed_type(type
), &align
);
1533 static void vla_runtime_pointed_size(CType
*type
)
1536 vla_runtime_type_size(pointed_type(type
), &align
);
1539 static inline int is_null_pointer(SValue
*p
)
1541 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1543 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& p
->c
.i
== 0) ||
1544 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.ll
== 0) ||
1545 ((p
->type
.t
& VT_BTYPE
) == VT_PTR
&& p
->c
.ptr
== 0);
1548 static inline int is_integer_btype(int bt
)
1550 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
1551 bt
== VT_INT
|| bt
== VT_LLONG
);
1554 /* check types for comparison or substraction of pointers */
1555 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
1557 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
1560 /* null pointers are accepted for all comparisons as gcc */
1561 if (is_null_pointer(p1
) || is_null_pointer(p2
))
1565 bt1
= type1
->t
& VT_BTYPE
;
1566 bt2
= type2
->t
& VT_BTYPE
;
1567 /* accept comparison between pointer and integer with a warning */
1568 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
1569 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
1570 tcc_warning("comparison between pointer and integer");
1574 /* both must be pointers or implicit function pointers */
1575 if (bt1
== VT_PTR
) {
1576 type1
= pointed_type(type1
);
1577 } else if (bt1
!= VT_FUNC
)
1578 goto invalid_operands
;
1580 if (bt2
== VT_PTR
) {
1581 type2
= pointed_type(type2
);
1582 } else if (bt2
!= VT_FUNC
) {
1584 tcc_error("invalid operands to binary %s", get_tok_str(op
, NULL
));
1586 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1587 (type2
->t
& VT_BTYPE
) == VT_VOID
)
1591 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1592 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1593 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1594 /* gcc-like error if '-' is used */
1596 goto invalid_operands
;
1598 tcc_warning("comparison of distinct pointer types lacks a cast");
1602 /* generic gen_op: handles types problems */
1603 ST_FUNC
void gen_op(int op
)
1605 int u
, t1
, t2
, bt1
, bt2
, t
;
1608 t1
= vtop
[-1].type
.t
;
1609 t2
= vtop
[0].type
.t
;
1610 bt1
= t1
& VT_BTYPE
;
1611 bt2
= t2
& VT_BTYPE
;
1613 if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
1614 /* at least one operand is a pointer */
1615 /* relationnal op: must be both pointers */
1616 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
1617 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1618 /* pointers are handled are unsigned */
1619 #ifdef TCC_TARGET_X86_64
1620 t
= VT_LLONG
| VT_UNSIGNED
;
1622 t
= VT_INT
| VT_UNSIGNED
;
1626 /* if both pointers, then it must be the '-' op */
1627 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
1629 tcc_error("cannot use pointers here");
1630 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1631 /* XXX: check that types are compatible */
1632 if (vtop
[-1].type
.t
& VT_VLA
) {
1633 vla_runtime_pointed_size(&vtop
[-1].type
);
1635 vpushi(pointed_size(&vtop
[-1].type
));
1639 /* set to integer type */
1640 #ifdef TCC_TARGET_X86_64
1641 vtop
->type
.t
= VT_LLONG
;
1643 vtop
->type
.t
= VT_INT
;
1648 /* exactly one pointer : must be '+' or '-'. */
1649 if (op
!= '-' && op
!= '+')
1650 tcc_error("cannot use pointers here");
1651 /* Put pointer as first operand */
1652 if (bt2
== VT_PTR
) {
1656 type1
= vtop
[-1].type
;
1657 type1
.t
&= ~VT_ARRAY
;
1658 if (vtop
[-1].type
.t
& VT_VLA
)
1659 vla_runtime_pointed_size(&vtop
[-1].type
);
1661 u
= pointed_size(&vtop
[-1].type
);
1663 tcc_error("unknown array element size");
1664 #ifdef TCC_TARGET_X86_64
1667 /* XXX: cast to int ? (long long case) */
1672 #ifdef CONFIG_TCC_BCHECK
1673 /* if evaluating constant expression, no code should be
1674 generated, so no bound check */
1675 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
1676 /* if bounded pointers, we generate a special code to
1683 gen_bounded_ptr_add();
1689 /* put again type if gen_opic() swaped operands */
1692 } else if (is_float(bt1
) || is_float(bt2
)) {
1693 /* compute bigger type and do implicit casts */
1694 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
1696 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
1701 /* floats can only be used for a few operations */
1702 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
1703 (op
< TOK_ULT
|| op
> TOK_GT
))
1704 tcc_error("invalid operands for binary operation");
1706 } else if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
) {
1707 t
= bt1
== VT_LLONG
? VT_LLONG
: VT_INT
;
1708 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (t
| VT_UNSIGNED
))
1711 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
1712 /* cast to biggest op */
1714 /* convert to unsigned if it does not fit in a long long */
1715 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
1716 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
1719 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
1720 tcc_error("comparison of struct");
1722 /* integer operations */
1724 /* convert to unsigned if it does not fit in an integer */
1725 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
1726 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
1729 /* XXX: currently, some unsigned operations are explicit, so
1730 we modify them here */
1731 if (t
& VT_UNSIGNED
) {
1738 else if (op
== TOK_LT
)
1740 else if (op
== TOK_GT
)
1742 else if (op
== TOK_LE
)
1744 else if (op
== TOK_GE
)
1751 /* special case for shifts and long long: we keep the shift as
1753 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
1760 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
1761 /* relationnal op: the result is an int */
1762 vtop
->type
.t
= VT_INT
;
1769 #ifndef TCC_TARGET_ARM
1770 /* generic itof for unsigned long long case */
1771 static void gen_cvt_itof1(int t
)
1773 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
1774 (VT_LLONG
| VT_UNSIGNED
)) {
1777 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
1778 #if LDOUBLE_SIZE != 8
1779 else if (t
== VT_LDOUBLE
)
1780 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
1783 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
1787 vtop
->r
= reg_fret(t
);
1794 /* generic ftoi for unsigned long long case */
1795 static void gen_cvt_ftoi1(int t
)
1799 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
1800 /* not handled natively */
1801 st
= vtop
->type
.t
& VT_BTYPE
;
1803 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
1804 #if LDOUBLE_SIZE != 8
1805 else if (st
== VT_LDOUBLE
)
1806 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
1809 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
1814 vtop
->r2
= REG_LRET
;
1820 /* force char or short cast */
1821 static void force_charshort_cast(int t
)
1825 /* XXX: add optimization if lvalue : just change type and offset */
1830 if (t
& VT_UNSIGNED
) {
1831 vpushi((1 << bits
) - 1);
1837 /* result must be signed or the SAR is converted to an SHL
1838 This was not the case when "t" was a signed short
1839 and the last value on the stack was an unsigned int */
1840 vtop
->type
.t
&= ~VT_UNSIGNED
;
1846 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1847 static void gen_cast(CType
*type
)
1849 int sbt
, dbt
, sf
, df
, c
, p
;
1851 /* special delayed cast for char/short */
1852 /* XXX: in some cases (multiple cascaded casts), it may still
1854 if (vtop
->r
& VT_MUSTCAST
) {
1855 vtop
->r
&= ~VT_MUSTCAST
;
1856 force_charshort_cast(vtop
->type
.t
);
1859 /* bitfields first get cast to ints */
1860 if (vtop
->type
.t
& VT_BITFIELD
) {
1864 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
1865 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
1870 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1871 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
1873 /* constant case: we can do it now */
1874 /* XXX: in ISOC, cannot do it if error in convert */
1875 if (sbt
== VT_FLOAT
)
1876 vtop
->c
.ld
= vtop
->c
.f
;
1877 else if (sbt
== VT_DOUBLE
)
1878 vtop
->c
.ld
= vtop
->c
.d
;
1881 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
1882 if (sbt
& VT_UNSIGNED
)
1883 vtop
->c
.ld
= vtop
->c
.ull
;
1885 vtop
->c
.ld
= vtop
->c
.ll
;
1887 if (sbt
& VT_UNSIGNED
)
1888 vtop
->c
.ld
= vtop
->c
.ui
;
1890 vtop
->c
.ld
= vtop
->c
.i
;
1893 if (dbt
== VT_FLOAT
)
1894 vtop
->c
.f
= (float)vtop
->c
.ld
;
1895 else if (dbt
== VT_DOUBLE
)
1896 vtop
->c
.d
= (double)vtop
->c
.ld
;
1897 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
1898 vtop
->c
.ull
= (unsigned long long)vtop
->c
.ld
;
1899 } else if (sf
&& dbt
== VT_BOOL
) {
1900 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
1903 vtop
->c
.ll
= (long long)vtop
->c
.ld
;
1904 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
1905 vtop
->c
.ll
= vtop
->c
.ull
;
1906 else if (sbt
& VT_UNSIGNED
)
1907 vtop
->c
.ll
= vtop
->c
.ui
;
1908 #ifdef TCC_TARGET_X86_64
1909 else if (sbt
== VT_PTR
)
1912 else if (sbt
!= VT_LLONG
)
1913 vtop
->c
.ll
= vtop
->c
.i
;
1915 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
1916 vtop
->c
.ull
= vtop
->c
.ll
;
1917 else if (dbt
== VT_BOOL
)
1918 vtop
->c
.i
= (vtop
->c
.ll
!= 0);
1919 else if (dbt
!= VT_LLONG
) {
1921 if ((dbt
& VT_BTYPE
) == VT_BYTE
)
1923 else if ((dbt
& VT_BTYPE
) == VT_SHORT
)
1926 if(dbt
& VT_UNSIGNED
)
1927 vtop
->c
.ui
= ((unsigned int)vtop
->c
.ll
<< s
) >> s
;
1929 vtop
->c
.i
= ((int)vtop
->c
.ll
<< s
) >> s
;
1932 } else if (p
&& dbt
== VT_BOOL
) {
1935 } else if (!nocode_wanted
) {
1936 /* non constant case: generate code */
1938 /* convert from fp to fp */
1941 /* convert int to fp */
1944 /* convert fp to int */
1945 if (dbt
== VT_BOOL
) {
1949 /* we handle char/short/etc... with generic code */
1950 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
1951 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
1955 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
1956 /* additional cast for char/short... */
1961 #ifndef TCC_TARGET_X86_64
1962 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
1963 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
1964 /* scalar to long long */
1965 /* machine independent conversion */
1967 /* generate high word */
1968 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
1972 if (sbt
== VT_PTR
) {
1973 /* cast from pointer to int before we apply
1974 shift operation, which pointers don't support*/
1975 gen_cast(&int_type
);
1981 /* patch second register */
1982 vtop
[-1].r2
= vtop
->r
;
1986 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
1987 (dbt
& VT_BTYPE
) == VT_PTR
||
1988 (dbt
& VT_BTYPE
) == VT_FUNC
) {
1989 if ((sbt
& VT_BTYPE
) != VT_LLONG
&&
1990 (sbt
& VT_BTYPE
) != VT_PTR
&&
1991 (sbt
& VT_BTYPE
) != VT_FUNC
) {
1992 /* need to convert from 32bit to 64bit */
1994 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
1995 /* x86_64 specific: movslq */
1997 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
2001 } else if (dbt
== VT_BOOL
) {
2002 /* scalar to bool */
2005 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
2006 (dbt
& VT_BTYPE
) == VT_SHORT
) {
2007 if (sbt
== VT_PTR
) {
2008 vtop
->type
.t
= VT_INT
;
2009 tcc_warning("nonportable conversion from pointer to char/short");
2011 force_charshort_cast(dbt
);
2012 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
2014 if (sbt
== VT_LLONG
) {
2015 /* from long long: just take low order word */
2019 /* if lvalue and single word type, nothing to do because
2020 the lvalue already contains the real type size (see
2021 VT_LVAL_xxx constants) */
2024 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
2025 /* if we are casting between pointer types,
2026 we must update the VT_LVAL_xxx size */
2027 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
2028 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
2033 /* return type size as known at compile time. Put alignment at 'a' */
2034 ST_FUNC
int type_size(CType
*type
, int *a
)
2039 bt
= type
->t
& VT_BTYPE
;
2040 if (bt
== VT_STRUCT
) {
2045 } else if (bt
== VT_PTR
) {
2046 if (type
->t
& VT_ARRAY
) {
2050 ts
= type_size(&s
->type
, a
);
2052 if (ts
< 0 && s
->c
< 0)
2060 } else if (bt
== VT_LDOUBLE
) {
2062 return LDOUBLE_SIZE
;
2063 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
2064 #ifdef TCC_TARGET_I386
2065 #ifdef TCC_TARGET_PE
2070 #elif defined(TCC_TARGET_ARM)
2080 } else if (bt
== VT_INT
|| bt
== VT_ENUM
|| bt
== VT_FLOAT
) {
2083 } else if (bt
== VT_SHORT
) {
2086 } else if (bt
== VT_QLONG
|| bt
== VT_QFLOAT
) {
2090 /* char, void, function, _Bool */
2096 /* push type size as known at runtime time on top of value stack. Put
2098 ST_FUNC
void vla_runtime_type_size(CType
*type
, int *a
)
2100 if (type
->t
& VT_VLA
) {
2101 vset(&int_type
, VT_LOCAL
|VT_LVAL
, type
->ref
->c
);
2103 vpushi(type_size(type
, a
));
2107 /* return the pointed type of t */
2108 static inline CType
*pointed_type(CType
*type
)
2110 return &type
->ref
->type
;
2113 /* modify type so that its it is a pointer to type. */
2114 ST_FUNC
void mk_pointer(CType
*type
)
2117 s
= sym_push(SYM_FIELD
, type
, 0, -1);
2118 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
2122 /* compare function types. OLD functions match any new functions */
2123 static int is_compatible_func(CType
*type1
, CType
*type2
)
2129 if (!is_compatible_types(&s1
->type
, &s2
->type
))
2131 /* check func_call */
2132 if (FUNC_CALL(s1
->r
) != FUNC_CALL(s2
->r
))
2134 /* XXX: not complete */
2135 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
2139 while (s1
!= NULL
) {
2142 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
2152 /* return true if type1 and type2 are the same. If unqualified is
2153 true, qualifiers on the types are ignored.
2155 - enums are not checked as gcc __builtin_types_compatible_p ()
2157 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2161 t1
= type1
->t
& VT_TYPE
;
2162 t2
= type2
->t
& VT_TYPE
;
2164 /* strip qualifiers before comparing */
2165 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2166 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2168 /* XXX: bitfields ? */
2171 /* test more complicated cases */
2172 bt1
= t1
& VT_BTYPE
;
2173 if (bt1
== VT_PTR
) {
2174 type1
= pointed_type(type1
);
2175 type2
= pointed_type(type2
);
2176 return is_compatible_types(type1
, type2
);
2177 } else if (bt1
== VT_STRUCT
) {
2178 return (type1
->ref
== type2
->ref
);
2179 } else if (bt1
== VT_FUNC
) {
2180 return is_compatible_func(type1
, type2
);
2186 /* return true if type1 and type2 are exactly the same (including
2189 static int is_compatible_types(CType
*type1
, CType
*type2
)
2191 return compare_types(type1
,type2
,0);
2194 /* return true if type1 and type2 are the same (ignoring qualifiers).
2196 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
2198 return compare_types(type1
,type2
,1);
2201 /* print a type. If 'varstr' is not NULL, then the variable is also
2202 printed in the type */
2204 /* XXX: add array and function pointers */
2205 static void type_to_str(char *buf
, int buf_size
,
2206 CType
*type
, const char *varstr
)
2213 t
= type
->t
& VT_TYPE
;
2216 if (t
& VT_CONSTANT
)
2217 pstrcat(buf
, buf_size
, "const ");
2218 if (t
& VT_VOLATILE
)
2219 pstrcat(buf
, buf_size
, "volatile ");
2220 if (t
& VT_UNSIGNED
)
2221 pstrcat(buf
, buf_size
, "unsigned ");
2251 tstr
= "long double";
2253 pstrcat(buf
, buf_size
, tstr
);
2257 if (bt
== VT_STRUCT
)
2261 pstrcat(buf
, buf_size
, tstr
);
2262 v
= type
->ref
->v
& ~SYM_STRUCT
;
2263 if (v
>= SYM_FIRST_ANOM
)
2264 pstrcat(buf
, buf_size
, "<anonymous>");
2266 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
2270 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
2271 pstrcat(buf
, buf_size
, "(");
2273 while (sa
!= NULL
) {
2274 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
2275 pstrcat(buf
, buf_size
, buf1
);
2278 pstrcat(buf
, buf_size
, ", ");
2280 pstrcat(buf
, buf_size
, ")");
2284 pstrcpy(buf1
, sizeof(buf1
), "*");
2286 pstrcat(buf1
, sizeof(buf1
), varstr
);
2287 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2291 pstrcat(buf
, buf_size
, " ");
2292 pstrcat(buf
, buf_size
, varstr
);
2297 /* verify type compatibility to store vtop in 'dt' type, and generate
2299 static void gen_assign_cast(CType
*dt
)
2301 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
2302 char buf1
[256], buf2
[256];
2305 st
= &vtop
->type
; /* source type */
2306 dbt
= dt
->t
& VT_BTYPE
;
2307 sbt
= st
->t
& VT_BTYPE
;
2309 tcc_error("Cannot assign void value");
2310 if (dt
->t
& VT_CONSTANT
)
2311 tcc_warning("assignment of read-only location");
2314 /* special cases for pointers */
2315 /* '0' can also be a pointer */
2316 if (is_null_pointer(vtop
))
2318 /* accept implicit pointer to integer cast with warning */
2319 if (is_integer_btype(sbt
)) {
2320 tcc_warning("assignment makes pointer from integer without a cast");
2323 type1
= pointed_type(dt
);
2324 /* a function is implicitely a function pointer */
2325 if (sbt
== VT_FUNC
) {
2326 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
2327 !is_compatible_types(pointed_type(dt
), st
))
2328 tcc_warning("assignment from incompatible pointer type");
2333 type2
= pointed_type(st
);
2334 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2335 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
2336 /* void * can match anything */
2338 /* exact type match, except for unsigned */
2341 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2342 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2343 if (!is_compatible_types(&tmp_type1
, &tmp_type2
))
2344 tcc_warning("assignment from incompatible pointer type");
2346 /* check const and volatile */
2347 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
2348 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
2349 tcc_warning("assignment discards qualifiers from pointer target type");
2355 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
2356 tcc_warning("assignment makes integer from pointer without a cast");
2358 /* XXX: more tests */
2363 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2364 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2365 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2367 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
2368 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
2369 tcc_error("cannot cast '%s' to '%s'", buf1
, buf2
);
2377 /* store vtop in lvalue pushed on stack */
2378 ST_FUNC
void vstore(void)
2380 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
2382 ft
= vtop
[-1].type
.t
;
2383 sbt
= vtop
->type
.t
& VT_BTYPE
;
2384 dbt
= ft
& VT_BTYPE
;
2385 if ((((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2386 (sbt
== VT_INT
&& dbt
== VT_SHORT
))
2387 && !(vtop
->type
.t
& VT_BITFIELD
)) {
2388 /* optimize char/short casts */
2389 delayed_cast
= VT_MUSTCAST
;
2390 vtop
->type
.t
= ft
& (VT_TYPE
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
)));
2391 /* XXX: factorize */
2392 if (ft
& VT_CONSTANT
)
2393 tcc_warning("assignment of read-only location");
2396 if (!(ft
& VT_BITFIELD
))
2397 gen_assign_cast(&vtop
[-1].type
);
2400 if (sbt
== VT_STRUCT
) {
2401 /* if structure, only generate pointer */
2402 /* structure assignment : generate memcpy */
2403 /* XXX: optimize if small size */
2404 if (!nocode_wanted
) {
2405 size
= type_size(&vtop
->type
, &align
);
2409 vtop
->type
.t
= VT_PTR
;
2412 /* address of memcpy() */
2415 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2416 else if(!(align
& 3))
2417 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2420 vpush_global_sym(&func_old_type
, TOK_memcpy
);
2425 vtop
->type
.t
= VT_PTR
;
2434 /* leave source on stack */
2435 } else if (ft
& VT_BITFIELD
) {
2436 /* bitfield store handling */
2437 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2438 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2439 /* remove bit field info to avoid loops */
2440 vtop
[-1].type
.t
= ft
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
2442 /* duplicate source into other register */
2447 if((ft
& VT_BTYPE
) == VT_BOOL
) {
2448 gen_cast(&vtop
[-1].type
);
2449 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2452 /* duplicate destination */
2454 vtop
[-1] = vtop
[-2];
2456 /* mask and shift source */
2457 if((ft
& VT_BTYPE
) != VT_BOOL
) {
2458 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2459 vpushll((1ULL << bit_size
) - 1ULL);
2461 vpushi((1 << bit_size
) - 1);
2467 /* load destination, mask and or with source */
2469 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2470 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2472 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2479 /* pop off shifted source from "duplicate source..." above */
2483 #ifdef CONFIG_TCC_BCHECK
2484 /* bound check case */
2485 if (vtop
[-1].r
& VT_MUSTBOUND
) {
2491 if (!nocode_wanted
) {
2495 #ifdef TCC_TARGET_X86_64
2496 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
2498 } else if ((ft
& VT_BTYPE
) == VT_QFLOAT
) {
2503 r
= gv(rc
); /* generate value */
2504 /* if lvalue was saved on stack, must read it */
2505 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
2507 t
= get_reg(RC_INT
);
2508 #ifdef TCC_TARGET_X86_64
2513 sv
.r
= VT_LOCAL
| VT_LVAL
;
2514 sv
.c
.ul
= vtop
[-1].c
.ul
;
2516 vtop
[-1].r
= t
| VT_LVAL
;
2518 /* two word case handling : store second register at word + 4 (or +8 for x86-64) */
2519 #ifdef TCC_TARGET_X86_64
2520 if (((ft
& VT_BTYPE
) == VT_QLONG
) || ((ft
& VT_BTYPE
) == VT_QFLOAT
)) {
2521 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
2523 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
2524 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
2526 vtop
[-1].type
.t
= load_type
;
2529 /* convert to int to increment easily */
2530 vtop
->type
.t
= addr_type
;
2536 vtop
[-1].type
.t
= load_type
;
2537 /* XXX: it works because r2 is spilled last ! */
2538 store(vtop
->r2
, vtop
- 1);
2544 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
2545 vtop
->r
|= delayed_cast
;
2549 /* post defines POST/PRE add. c is the token ++ or -- */
2550 ST_FUNC
void inc(int post
, int c
)
2553 vdup(); /* save lvalue */
2555 gv_dup(); /* duplicate value */
2560 vpushi(c
- TOK_MID
);
2562 vstore(); /* store value */
2564 vpop(); /* if post op, return saved value */
2567 /* Parse GNUC __attribute__ extension. Currently, the following
2568 extensions are recognized:
2569 - aligned(n) : set data/function alignment.
2570 - packed : force data alignment to 1
2571 - section(x) : generate data/code in this section.
2572 - unused : currently ignored, but may be used someday.
2573 - regparm(n) : pass function parameters in registers (i386 only)
2575 static void parse_attribute(AttributeDef
*ad
)
2579 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
2583 while (tok
!= ')') {
2584 if (tok
< TOK_IDENT
)
2585 expect("attribute name");
2593 expect("section name");
2594 ad
->section
= find_section(tcc_state
, (char *)tokc
.cstr
->data
);
2602 expect("alias(\"target\")");
2603 ad
->alias_target
= /* save string as token, for later */
2604 tok_alloc((char*)tokc
.cstr
->data
, tokc
.cstr
->size
-1)->tok
;
2613 if (n
<= 0 || (n
& (n
- 1)) != 0)
2614 tcc_error("alignment must be a positive power of two");
2631 /* currently, no need to handle it because tcc does not
2632 track unused objects */
2636 /* currently, no need to handle it because tcc does not
2637 track unused objects */
2642 ad
->func_call
= FUNC_CDECL
;
2647 ad
->func_call
= FUNC_STDCALL
;
2649 #ifdef TCC_TARGET_I386
2659 ad
->func_call
= FUNC_FASTCALL1
+ n
- 1;
2665 ad
->func_call
= FUNC_FASTCALLW
;
2672 ad
->mode
= VT_LLONG
+ 1;
2675 ad
->mode
= VT_SHORT
+ 1;
2678 ad
->mode
= VT_INT
+ 1;
2681 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
2688 ad
->func_export
= 1;
2691 ad
->func_import
= 1;
2694 if (tcc_state
->warn_unsupported
)
2695 tcc_warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
2696 /* skip parameters */
2698 int parenthesis
= 0;
2702 else if (tok
== ')')
2705 } while (parenthesis
&& tok
!= -1);
2718 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2719 static void struct_decl(CType
*type
, int u
)
2721 int a
, v
, size
, align
, maxalign
, c
, offset
;
2722 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
, prevbt
;
2723 Sym
*s
, *ss
, *ass
, **ps
;
2727 a
= tok
; /* save decl type */
2732 /* struct already defined ? return it */
2734 expect("struct/union/enum name");
2738 tcc_error("invalid type");
2745 /* we put an undefined size for struct/union */
2746 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
2747 s
->r
= 0; /* default alignment is zero as gcc */
2748 /* put struct/union/enum name in type */
2756 tcc_error("struct/union/enum already defined");
2757 /* cannot be empty */
2759 /* non empty enums are not allowed */
2760 if (a
== TOK_ENUM
) {
2764 expect("identifier");
2770 /* enum symbols have static storage */
2771 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
2772 ss
->type
.t
|= VT_STATIC
;
2777 /* NOTE: we accept a trailing comma */
2788 while (tok
!= '}') {
2789 parse_btype(&btype
, &ad
);
2795 type_decl(&type1
, &ad
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
2796 if (v
== 0 && (type1
.t
& VT_BTYPE
) != VT_STRUCT
)
2797 expect("identifier");
2798 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
2799 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
2800 tcc_error("invalid type for '%s'",
2801 get_tok_str(v
, NULL
));
2805 bit_size
= expr_const();
2806 /* XXX: handle v = 0 case for messages */
2808 tcc_error("negative width in bit-field '%s'",
2809 get_tok_str(v
, NULL
));
2810 if (v
&& bit_size
== 0)
2811 tcc_error("zero width for bit-field '%s'",
2812 get_tok_str(v
, NULL
));
2814 size
= type_size(&type1
, &align
);
2816 if (align
< ad
.aligned
)
2818 } else if (ad
.packed
) {
2820 } else if (*tcc_state
->pack_stack_ptr
) {
2821 if (align
> *tcc_state
->pack_stack_ptr
)
2822 align
= *tcc_state
->pack_stack_ptr
;
2825 if (bit_size
>= 0) {
2826 bt
= type1
.t
& VT_BTYPE
;
2833 tcc_error("bitfields must have scalar type");
2835 if (bit_size
> bsize
) {
2836 tcc_error("width of '%s' exceeds its type",
2837 get_tok_str(v
, NULL
));
2838 } else if (bit_size
== bsize
) {
2839 /* no need for bit fields */
2841 } else if (bit_size
== 0) {
2842 /* XXX: what to do if only padding in a
2844 /* zero size: means to pad */
2847 /* we do not have enough room ?
2848 did the type change?
2850 if ((bit_pos
+ bit_size
) > bsize
||
2851 bt
!= prevbt
|| a
== TOK_UNION
)
2854 /* XXX: handle LSB first */
2855 type1
.t
|= VT_BITFIELD
|
2856 (bit_pos
<< VT_STRUCT_SHIFT
) |
2857 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
2858 bit_pos
+= bit_size
;
2864 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2865 /* add new memory data only if starting
2867 if (lbit_pos
== 0) {
2868 if (a
== TOK_STRUCT
) {
2869 c
= (c
+ align
- 1) & -align
;
2878 if (align
> maxalign
)
2882 printf("add field %s offset=%d",
2883 get_tok_str(v
, NULL
), offset
);
2884 if (type1
.t
& VT_BITFIELD
) {
2885 printf(" pos=%d size=%d",
2886 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
2887 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
2892 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2894 while ((ass
= ass
->next
) != NULL
) {
2895 ss
= sym_push(ass
->v
, &ass
->type
, 0, offset
+ ass
->c
);
2900 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
2904 if (tok
== ';' || tok
== TOK_EOF
)
2911 /* store size and alignment */
2912 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
2918 /* return 0 if no type declaration. otherwise, return the basic type
2921 static int parse_btype(CType
*type
, AttributeDef
*ad
)
2923 int t
, u
, type_found
, typespec_found
, typedef_found
;
2927 memset(ad
, 0, sizeof(AttributeDef
));
2935 /* currently, we really ignore extension */
2945 if ((t
& VT_BTYPE
) != 0)
2946 tcc_error("too many basic types");
2962 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
2963 #ifndef TCC_TARGET_PE
2964 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
2966 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
2967 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
2981 if ((t
& VT_BTYPE
) == VT_LONG
) {
2982 #ifdef TCC_TARGET_PE
2983 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
2985 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
2993 struct_decl(&type1
, VT_ENUM
);
2996 type
->ref
= type1
.ref
;
3000 struct_decl(&type1
, VT_STRUCT
);
3003 /* type modifiers */
3056 /* GNUC attribute */
3057 case TOK_ATTRIBUTE1
:
3058 case TOK_ATTRIBUTE2
:
3059 parse_attribute(ad
);
3062 t
= (t
& ~VT_BTYPE
) | u
;
3070 parse_expr_type(&type1
);
3071 /* remove all storage modifiers except typedef */
3072 type1
.t
&= ~(VT_STORAGE
&~VT_TYPEDEF
);
3075 if (typespec_found
|| typedef_found
)
3078 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
3081 t
|= (s
->type
.t
& ~VT_TYPEDEF
);
3082 type
->ref
= s
->type
.ref
;
3084 /* get attributes from typedef */
3085 if (0 == ad
->aligned
)
3086 ad
->aligned
= FUNC_ALIGN(s
->r
);
3087 if (0 == ad
->func_call
)
3088 ad
->func_call
= FUNC_CALL(s
->r
);
3089 ad
->packed
|= FUNC_PACKED(s
->r
);
3098 if ((t
& (VT_SIGNED
|VT_UNSIGNED
)) == (VT_SIGNED
|VT_UNSIGNED
))
3099 tcc_error("signed and unsigned modifier");
3100 if (tcc_state
->char_is_unsigned
) {
3101 if ((t
& (VT_SIGNED
|VT_UNSIGNED
|VT_BTYPE
)) == VT_BYTE
)
3106 /* long is never used as type */
3107 if ((t
& VT_BTYPE
) == VT_LONG
)
3108 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
3109 t
= (t
& ~VT_BTYPE
) | VT_INT
;
3111 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3117 /* convert a function parameter type (array to pointer and function to
3118 function pointer) */
3119 static inline void convert_parameter_type(CType
*pt
)
3121 /* remove const and volatile qualifiers (XXX: const could be used
3122 to indicate a const function parameter */
3123 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3124 /* array must be transformed to pointer according to ANSI C */
3126 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
3131 ST_FUNC
void parse_asm_str(CString
*astr
)
3134 /* read the string */
3136 expect("string constant");
3138 while (tok
== TOK_STR
) {
3139 /* XXX: add \0 handling too ? */
3140 cstr_cat(astr
, tokc
.cstr
->data
);
3143 cstr_ccat(astr
, '\0');
3146 /* Parse an asm label and return the label
3147 * Don't forget to free the CString in the caller! */
3148 static void asm_label_instr(CString
*astr
)
3151 parse_asm_str(astr
);
3154 printf("asm_alias: \"%s\"\n", (char *)astr
->data
);
3158 static void post_type(CType
*type
, AttributeDef
*ad
)
3160 int n
, l
, t1
, arg_size
, align
;
3161 Sym
**plast
, *s
, *first
;
3166 /* function declaration */
3174 /* read param name and compute offset */
3175 if (l
!= FUNC_OLD
) {
3176 if (!parse_btype(&pt
, &ad1
)) {
3178 tcc_error("invalid type");
3185 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
3187 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3188 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
3189 tcc_error("parameter declared as void");
3190 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
3195 expect("identifier");
3199 convert_parameter_type(&pt
);
3200 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
3206 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
3213 /* if no parameters, then old type prototype */
3217 /* NOTE: const is ignored in returned type as it has a special
3218 meaning in gcc / C++ */
3219 type
->t
&= ~VT_CONSTANT
;
3220 /* some ancient pre-K&R C allows a function to return an array
3221 and the array brackets to be put after the arguments, such
3222 that "int c()[]" means something like "int[] c()" */
3225 skip(']'); /* only handle simple "[]" */
3228 /* we push a anonymous symbol which will contain the function prototype */
3229 ad
->func_args
= arg_size
;
3230 s
= sym_push(SYM_FIELD
, type
, INT_ATTR(ad
), l
);
3234 } else if (tok
== '[') {
3235 /* array definition */
3237 if (tok
== TOK_RESTRICT1
)
3242 if (!local_stack
|| nocode_wanted
)
3243 vpushi(expr_const());
3245 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3248 tcc_error("invalid array size");
3250 if (!is_integer_btype(vtop
->type
.t
& VT_BTYPE
))
3251 tcc_error("size of variable length array should be an integer");
3256 /* parse next post type */
3257 post_type(type
, ad
);
3258 t1
|= type
->t
& VT_VLA
;
3261 loc
-= type_size(&int_type
, &align
);
3265 vla_runtime_type_size(type
, &align
);
3267 vset(&int_type
, VT_LOCAL
|VT_LVAL
, loc
);
3274 /* we push an anonymous symbol which will contain the array
3276 s
= sym_push(SYM_FIELD
, type
, 0, n
);
3277 type
->t
= (t1
? VT_VLA
: VT_ARRAY
) | VT_PTR
;
3282 /* Parse a type declaration (except basic type), and return the type
3283 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3284 expected. 'type' should contain the basic type. 'ad' is the
3285 attribute definition of the basic type. It can be modified by
3288 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
3291 CType type1
, *type2
;
3292 int qualifiers
, storage
;
3294 while (tok
== '*') {
3302 qualifiers
|= VT_CONSTANT
;
3307 qualifiers
|= VT_VOLATILE
;
3315 type
->t
|= qualifiers
;
3318 /* XXX: clarify attribute handling */
3319 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3320 parse_attribute(ad
);
3322 /* recursive type */
3323 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3324 type1
.t
= 0; /* XXX: same as int */
3327 /* XXX: this is not correct to modify 'ad' at this point, but
3328 the syntax is not clear */
3329 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3330 parse_attribute(ad
);
3331 type_decl(&type1
, ad
, v
, td
);
3334 /* type identifier */
3335 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
3339 if (!(td
& TYPE_ABSTRACT
))
3340 expect("identifier");
3344 storage
= type
->t
& VT_STORAGE
;
3345 type
->t
&= ~VT_STORAGE
;
3346 if (storage
& VT_STATIC
) {
3347 int saved_nocode_wanted
= nocode_wanted
;
3349 post_type(type
, ad
);
3350 nocode_wanted
= saved_nocode_wanted
;
3352 post_type(type
, ad
);
3354 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3355 parse_attribute(ad
);
3359 /* append type at the end of type1 */
3372 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3373 ST_FUNC
int lvalue_type(int t
)
3378 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
3380 else if (bt
== VT_SHORT
)
3384 if (t
& VT_UNSIGNED
)
3385 r
|= VT_LVAL_UNSIGNED
;
3389 /* indirection with full error checking and bound check */
3390 ST_FUNC
void indir(void)
3392 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
3393 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
3397 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
3399 vtop
->type
= *pointed_type(&vtop
->type
);
3400 /* Arrays and functions are never lvalues */
3401 if (!(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_VLA
)
3402 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3403 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3404 /* if bound checking, the referenced pointer must be checked */
3405 #ifdef CONFIG_TCC_BCHECK
3406 if (tcc_state
->do_bounds_check
)
3407 vtop
->r
|= VT_MUSTBOUND
;
3412 /* pass a parameter to a function and do type checking and casting */
3413 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
3418 func_type
= func
->c
;
3419 if (func_type
== FUNC_OLD
||
3420 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
3421 /* default casting : only need to convert float to double */
3422 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
3426 } else if (arg
== NULL
) {
3427 tcc_error("too many arguments to function");
3430 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
3431 gen_assign_cast(&type
);
3435 /* parse an expression of the form '(type)' or '(expr)' and return its
3437 static void parse_expr_type(CType
*type
)
3443 if (parse_btype(type
, &ad
)) {
3444 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3451 static void parse_type(CType
*type
)
3456 if (!parse_btype(type
, &ad
)) {
3459 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3462 static void vpush_tokc(int t
)
3467 vsetc(&type
, VT_CONST
, &tokc
);
3470 ST_FUNC
void unary(void)
3472 int n
, t
, align
, size
, r
, sizeof_caller
;
3476 static int in_sizeof
= 0;
3478 sizeof_caller
= in_sizeof
;
3480 /* XXX: GCC 2.95.3 does not generate a table although it should be
3494 vpush_tokc(VT_INT
| VT_UNSIGNED
);
3498 vpush_tokc(VT_LLONG
);
3502 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
3506 vpush_tokc(VT_FLOAT
);
3510 vpush_tokc(VT_DOUBLE
);
3514 vpush_tokc(VT_LDOUBLE
);
3517 case TOK___FUNCTION__
:
3519 goto tok_identifier
;
3525 /* special function name identifier */
3526 len
= strlen(funcname
) + 1;
3527 /* generate char[len] type */
3532 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
3533 ptr
= section_ptr_add(data_section
, len
);
3534 memcpy(ptr
, funcname
, len
);
3539 #ifdef TCC_TARGET_PE
3540 t
= VT_SHORT
| VT_UNSIGNED
;
3546 /* string parsing */
3549 if (tcc_state
->warn_write_strings
)
3554 memset(&ad
, 0, sizeof(AttributeDef
));
3555 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, NULL
, 0);
3560 if (parse_btype(&type
, &ad
)) {
3561 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
3563 /* check ISOC99 compound literal */
3565 /* data is allocated locally by default */
3570 /* all except arrays are lvalues */
3571 if (!(type
.t
& VT_ARRAY
))
3572 r
|= lvalue_type(type
.t
);
3573 memset(&ad
, 0, sizeof(AttributeDef
));
3574 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, NULL
, 0);
3576 if (sizeof_caller
) {
3583 } else if (tok
== '{') {
3584 /* save all registers */
3586 /* statement expression : we do not accept break/continue
3587 inside as GCC does */
3588 block(NULL
, NULL
, NULL
, NULL
, 0, 1);
3603 /* functions names must be treated as function pointers,
3604 except for unary '&' and sizeof. Since we consider that
3605 functions are not lvalues, we only have to handle it
3606 there and in function calls. */
3607 /* arrays can also be used although they are not lvalues */
3608 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
3609 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
3611 mk_pointer(&vtop
->type
);
3617 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3619 boolean
.t
= VT_BOOL
;
3621 vtop
->c
.i
= !vtop
->c
.i
;
3622 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
3623 vtop
->c
.i
= vtop
->c
.i
^ 1;
3626 vseti(VT_JMP
, gtst(1, 0));
3637 /* in order to force cast, we add zero */
3639 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
3640 tcc_error("pointer not accepted for unary plus");
3650 unary_type(&type
); // Perform a in_sizeof = 0;
3651 size
= type_size(&type
, &align
);
3652 if (t
== TOK_SIZEOF
) {
3653 if (!(type
.t
& VT_VLA
)) {
3655 tcc_error("sizeof applied to an incomplete type");
3658 vla_runtime_type_size(&type
, &align
);
3663 vtop
->type
.t
|= VT_UNSIGNED
;
3666 case TOK_builtin_types_compatible_p
:
3675 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3676 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3677 vpushi(is_compatible_types(&type1
, &type2
));
3680 case TOK_builtin_constant_p
:
3682 int saved_nocode_wanted
, res
;
3685 saved_nocode_wanted
= nocode_wanted
;
3688 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
3690 nocode_wanted
= saved_nocode_wanted
;
3695 case TOK_builtin_frame_address
:
3701 if (tok
!= TOK_CINT
|| tokc
.i
< 0) {
3702 tcc_error("__builtin_frame_address only takes positive integers");
3709 vset(&type
, VT_LOCAL
, 0); /* local frame */
3711 mk_pointer(&vtop
->type
);
3712 indir(); /* -> parent frame */
3716 #ifdef TCC_TARGET_X86_64
3717 case TOK_builtin_va_arg_types
:
3725 vpushi(classify_x86_64_va_arg(&type
));
3744 goto tok_identifier
;
3746 /* allow to take the address of a label */
3747 if (tok
< TOK_UIDENT
)
3748 expect("label identifier");
3749 s
= label_find(tok
);
3751 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
3753 if (s
->r
== LABEL_DECLARED
)
3754 s
->r
= LABEL_FORWARD
;
3757 s
->type
.t
= VT_VOID
;
3758 mk_pointer(&s
->type
);
3759 s
->type
.t
|= VT_STATIC
;
3761 vset(&s
->type
, VT_CONST
| VT_SYM
, 0);
3766 // special qnan , snan and infinity values
3768 vpush64(VT_DOUBLE
, 0x7ff8000000000000ULL
);
3772 vpush64(VT_DOUBLE
, 0x7ff0000000000001ULL
);
3776 vpush64(VT_DOUBLE
, 0x7ff0000000000000ULL
);
3785 expect("identifier");
3789 tcc_error("'%s' undeclared", get_tok_str(t
, NULL
));
3790 /* for simple function calls, we tolerate undeclared
3791 external reference to int() function */
3792 if (tcc_state
->warn_implicit_function_declaration
)
3793 tcc_warning("implicit declaration of function '%s'",
3794 get_tok_str(t
, NULL
));
3795 s
= external_global_sym(t
, &func_old_type
, 0);
3797 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
3798 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
3799 /* if referencing an inline function, then we generate a
3800 symbol to it if not already done. It will have the
3801 effect to generate code for it at the end of the
3802 compilation unit. Inline function as always
3803 generated in the text section. */
3805 put_extern_sym(s
, text_section
, 0, 0);
3806 r
= VT_SYM
| VT_CONST
;
3810 vset(&s
->type
, r
, s
->c
);
3811 /* if forward reference, we must point to s */
3812 if (vtop
->r
& VT_SYM
) {
3819 /* post operations */
3821 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
3824 } else if (tok
== '.' || tok
== TOK_ARROW
) {
3827 if (tok
== TOK_ARROW
)
3829 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
3833 /* expect pointer on structure */
3834 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
3835 expect("struct or union");
3839 while ((s
= s
->next
) != NULL
) {
3844 tcc_error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, NULL
));
3845 /* add field offset to pointer */
3846 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
3849 /* change type to field type, and set to lvalue */
3850 vtop
->type
= s
->type
;
3851 vtop
->type
.t
|= qualifiers
;
3852 /* an array is never an lvalue */
3853 if (!(vtop
->type
.t
& VT_ARRAY
)) {
3854 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3855 #ifdef CONFIG_TCC_BCHECK
3856 /* if bound checking, the referenced pointer must be checked */
3857 if (tcc_state
->do_bounds_check
)
3858 vtop
->r
|= VT_MUSTBOUND
;
3862 } else if (tok
== '[') {
3868 } else if (tok
== '(') {
3874 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3875 /* pointer test (no array accepted) */
3876 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
3877 vtop
->type
= *pointed_type(&vtop
->type
);
3878 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
3882 expect("function pointer");
3885 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
3887 /* get return type */
3890 sa
= s
->next
; /* first parameter */
3893 /* compute first implicit argument if a structure is returned */
3894 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
3896 sret
= gfunc_sret(&s
->type
, &ret
.type
, &ret_align
);
3898 /* get some space for the returned structure */
3899 size
= type_size(&s
->type
, &align
);
3900 loc
= (loc
- size
) & -align
;
3902 ret
.r
= VT_LOCAL
| VT_LVAL
;
3903 /* pass it as 'int' to avoid structure arg passing
3905 vseti(VT_LOCAL
, loc
);
3915 /* return in register */
3916 if (is_float(ret
.type
.t
)) {
3917 ret
.r
= reg_fret(ret
.type
.t
);
3918 #ifdef TCC_TARGET_X86_64
3919 if ((ret
.type
.t
& VT_BTYPE
) == VT_QFLOAT
)
3923 #ifdef TCC_TARGET_X86_64
3924 if ((ret
.type
.t
& VT_BTYPE
) == VT_QLONG
)
3926 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
3936 gfunc_param_typed(s
, sa
);
3946 tcc_error("too few arguments to function");
3948 if (!nocode_wanted
) {
3949 gfunc_call(nb_args
);
3951 vtop
-= (nb_args
+ 1);
3954 vsetc(&ret
.type
, ret
.r
, &ret
.c
);
3956 /* handle packed struct return */
3957 if (((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) && !sret
) {
3958 size
= type_size(&s
->type
, &align
);
3959 loc
= (loc
- size
) & -align
;
3961 vset(&ret
.type
, VT_LOCAL
| VT_LVAL
, addr
);
3965 vset(&s
->type
, VT_LOCAL
| VT_LVAL
, addr
);
3973 ST_FUNC
void expr_prod(void)
3978 while (tok
== '*' || tok
== '/' || tok
== '%') {
3986 ST_FUNC
void expr_sum(void)
3991 while (tok
== '+' || tok
== '-') {
3999 static void expr_shift(void)
4004 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
4012 static void expr_cmp(void)
4017 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
4018 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
4026 static void expr_cmpeq(void)
4031 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
4039 static void expr_and(void)
4042 while (tok
== '&') {
4049 static void expr_xor(void)
4052 while (tok
== '^') {
4059 static void expr_or(void)
4062 while (tok
== '|') {
4069 /* XXX: fix this mess */
4070 static void expr_land_const(void)
4073 while (tok
== TOK_LAND
) {
4080 /* XXX: fix this mess */
4081 static void expr_lor_const(void)
4084 while (tok
== TOK_LOR
) {
4091 /* only used if non constant */
4092 static void expr_land(void)
4097 if (tok
== TOK_LAND
) {
4102 if (tok
!= TOK_LAND
) {
4112 static void expr_lor(void)
4117 if (tok
== TOK_LOR
) {
4122 if (tok
!= TOK_LOR
) {
4132 /* XXX: better constant handling */
4133 static void expr_cond(void)
4135 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
;
4137 CType type
, type1
, type2
;
4144 boolean
.t
= VT_BOOL
;
4150 if (tok
!= ':' || !gnu_ext
) {
4165 if (vtop
!= vstack
) {
4166 /* needed to avoid having different registers saved in
4168 if (is_float(vtop
->type
.t
)) {
4170 #ifdef TCC_TARGET_X86_64
4171 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4181 if (tok
== ':' && gnu_ext
) {
4189 sv
= *vtop
; /* save value to handle it later */
4190 vtop
--; /* no vpop so that FP stack is not flushed */
4198 bt1
= t1
& VT_BTYPE
;
4200 bt2
= t2
& VT_BTYPE
;
4201 /* cast operands to correct type according to ISOC rules */
4202 if (is_float(bt1
) || is_float(bt2
)) {
4203 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
4204 type
.t
= VT_LDOUBLE
;
4205 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
4210 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
4211 /* cast to biggest op */
4213 /* convert to unsigned if it does not fit in a long long */
4214 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
4215 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
4216 type
.t
|= VT_UNSIGNED
;
4217 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
4218 /* If one is a null ptr constant the result type
4220 if (is_null_pointer (vtop
))
4222 else if (is_null_pointer (&sv
))
4224 /* XXX: test pointer compatibility, C99 has more elaborate
4228 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
4229 /* XXX: test function pointer compatibility */
4230 type
= bt1
== VT_FUNC
? type1
: type2
;
4231 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
4232 /* XXX: test structure compatibility */
4233 type
= bt1
== VT_STRUCT
? type1
: type2
;
4234 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
4235 /* NOTE: as an extension, we accept void on only one side */
4238 /* integer operations */
4240 /* convert to unsigned if it does not fit in an integer */
4241 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
4242 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
4243 type
.t
|= VT_UNSIGNED
;
4246 /* now we convert second operand */
4248 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4251 if (is_float(type
.t
)) {
4253 #ifdef TCC_TARGET_X86_64
4254 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4258 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
4259 /* for long longs, we use fixed registers to avoid having
4260 to handle a complicated move */
4265 /* this is horrible, but we must also convert first
4269 /* put again first value and cast it */
4272 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4275 move_reg(r2
, r1
, type
.t
);
4282 static void expr_eq(void)
4288 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
4289 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
4290 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
4305 ST_FUNC
void gexpr(void)
4316 /* parse an expression and return its type without any side effect. */
4317 static void expr_type(CType
*type
)
4319 int saved_nocode_wanted
;
4321 saved_nocode_wanted
= nocode_wanted
;
4326 nocode_wanted
= saved_nocode_wanted
;
4329 /* parse a unary expression and return its type without any side
4331 static void unary_type(CType
*type
)
4343 /* parse a constant expression and return value in vtop. */
4344 static void expr_const1(void)
4353 /* parse an integer constant and return its value. */
4354 ST_FUNC
int expr_const(void)
4358 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
4359 expect("constant expression");
4365 /* return the label token if current token is a label, otherwise
4367 static int is_label(void)
4371 /* fast test first */
4372 if (tok
< TOK_UIDENT
)
4374 /* no need to save tokc because tok is an identifier */
4381 unget_tok(last_tok
);
4386 static void label_or_decl(int l
)
4390 /* fast test first */
4391 if (tok
>= TOK_UIDENT
)
4393 /* no need to save tokc because tok is an identifier */
4397 unget_tok(last_tok
);
4400 unget_tok(last_tok
);
4405 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
4406 int case_reg
, int is_expr
)
4409 Sym
*s
, *frame_bottom
;
4411 /* generate line number info */
4412 if (tcc_state
->do_debug
&&
4413 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
4414 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
4416 last_line_num
= file
->line_num
;
4420 /* default return value is (void) */
4422 vtop
->type
.t
= VT_VOID
;
4425 if (tok
== TOK_IF
) {
4432 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4434 if (c
== TOK_ELSE
) {
4438 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4439 gsym(d
); /* patch else jmp */
4442 } else if (tok
== TOK_WHILE
) {
4450 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4454 } else if (tok
== '{') {
4458 /* record local declaration stack position */
4460 frame_bottom
= sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4461 frame_bottom
->next
= scope_stack_bottom
;
4462 scope_stack_bottom
= frame_bottom
;
4463 llabel
= local_label_stack
;
4464 /* handle local labels declarations */
4465 if (tok
== TOK_LABEL
) {
4468 if (tok
< TOK_UIDENT
)
4469 expect("label identifier");
4470 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
4480 while (tok
!= '}') {
4481 label_or_decl(VT_LOCAL
);
4485 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4488 /* pop locally defined labels */
4489 label_pop(&local_label_stack
, llabel
);
4491 /* XXX: this solution makes only valgrind happy...
4492 triggered by gcc.c-torture/execute/20000917-1.c */
4494 switch(vtop
->type
.t
& VT_BTYPE
) {
4499 for(p
=vtop
->type
.ref
;p
;p
=p
->prev
)
4501 tcc_error("unsupported expression type");
4504 /* pop locally defined symbols */
4505 scope_stack_bottom
= scope_stack_bottom
->next
;
4506 sym_pop(&local_stack
, s
);
4508 } else if (tok
== TOK_RETURN
) {
4512 gen_assign_cast(&func_vt
);
4513 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
4514 CType type
, ret_type
;
4516 if (gfunc_sret(&func_vt
, &ret_type
, &ret_align
)) {
4517 /* if returning structure, must copy it to implicit
4518 first pointer arg location */
4521 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
4524 /* copy structure value to pointer */
4527 /* returning structure packed into registers */
4528 int size
, addr
, align
;
4529 size
= type_size(&func_vt
,&align
);
4530 if ((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) || (vtop
->c
.i
& (ret_align
-1)))
4531 && (align
& (ret_align
-1))) {
4532 loc
= (loc
- size
) & -align
;
4535 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
4538 vset(&ret_type
, VT_LOCAL
| VT_LVAL
, addr
);
4540 vtop
->type
= ret_type
;
4541 if (is_float(ret_type
.t
))
4542 gv(rc_fret(ret_type
.t
));
4546 } else if (is_float(func_vt
.t
)) {
4547 gv(rc_fret(func_vt
.t
));
4551 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
4554 rsym
= gjmp(rsym
); /* jmp */
4555 } else if (tok
== TOK_BREAK
) {
4558 tcc_error("cannot break");
4559 *bsym
= gjmp(*bsym
);
4562 } else if (tok
== TOK_CONTINUE
) {
4565 tcc_error("cannot continue");
4566 *csym
= gjmp(*csym
);
4569 } else if (tok
== TOK_FOR
) {
4574 frame_bottom
= sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4575 frame_bottom
->next
= scope_stack_bottom
;
4576 scope_stack_bottom
= frame_bottom
;
4578 /* c99 for-loop init decl? */
4579 if (!decl0(VT_LOCAL
, 1)) {
4580 /* no, regular for-loop init expr */
4604 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4608 scope_stack_bottom
= scope_stack_bottom
->next
;
4609 sym_pop(&local_stack
, s
);
4611 if (tok
== TOK_DO
) {
4616 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4627 if (tok
== TOK_SWITCH
) {
4631 /* XXX: other types than integer */
4632 case_reg
= gv(RC_INT
);
4636 b
= gjmp(0); /* jump to first case */
4638 block(&a
, csym
, &b
, &c
, case_reg
, 0);
4639 /* if no default, jmp after switch */
4647 if (tok
== TOK_CASE
) {
4654 if (gnu_ext
&& tok
== TOK_DOTS
) {
4658 tcc_warning("empty case range");
4660 /* since a case is like a label, we must skip it with a jmp */
4667 *case_sym
= gtst(1, 0);
4670 *case_sym
= gtst(1, 0);
4674 *case_sym
= gtst(1, *case_sym
);
4679 goto block_after_label
;
4681 if (tok
== TOK_DEFAULT
) {
4687 tcc_error("too many 'default'");
4690 goto block_after_label
;
4692 if (tok
== TOK_GOTO
) {
4694 if (tok
== '*' && gnu_ext
) {
4698 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
4701 } else if (tok
>= TOK_UIDENT
) {
4702 s
= label_find(tok
);
4703 /* put forward definition if needed */
4705 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4707 if (s
->r
== LABEL_DECLARED
)
4708 s
->r
= LABEL_FORWARD
;
4710 /* label already defined */
4711 if (s
->r
& LABEL_FORWARD
)
4712 s
->jnext
= gjmp(s
->jnext
);
4714 gjmp_addr(s
->jnext
);
4717 expect("label identifier");
4720 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
4728 if (s
->r
== LABEL_DEFINED
)
4729 tcc_error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
4731 s
->r
= LABEL_DEFINED
;
4733 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
4736 /* we accept this, but it is a mistake */
4739 tcc_warning("deprecated use of label at end of compound statement");
4743 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4746 /* expression case */
4761 /* t is the array or struct type. c is the array or struct
4762 address. cur_index/cur_field is the pointer to the current
4763 value. 'size_only' is true if only size info is needed (only used
4765 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
4766 int *cur_index
, Sym
**cur_field
,
4770 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
4776 if (gnu_ext
&& (l
= is_label()) != 0)
4778 while (tok
== '[' || tok
== '.') {
4780 if (!(type
->t
& VT_ARRAY
))
4781 expect("array type");
4784 index
= expr_const();
4785 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
4786 expect("invalid index");
4787 if (tok
== TOK_DOTS
&& gnu_ext
) {
4789 index_last
= expr_const();
4790 if (index_last
< 0 ||
4791 (s
->c
>= 0 && index_last
>= s
->c
) ||
4793 expect("invalid index");
4799 *cur_index
= index_last
;
4800 type
= pointed_type(type
);
4801 elem_size
= type_size(type
, &align
);
4802 c
+= index
* elem_size
;
4803 /* NOTE: we only support ranges for last designator */
4804 nb_elems
= index_last
- index
+ 1;
4805 if (nb_elems
!= 1) {
4814 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
4815 expect("struct/union type");
4828 /* XXX: fix this mess by using explicit storage field */
4830 type1
.t
|= (type
->t
& ~VT_TYPE
);
4844 if (type
->t
& VT_ARRAY
) {
4846 type
= pointed_type(type
);
4847 c
+= index
* type_size(type
, &align
);
4851 tcc_error("too many field init");
4852 /* XXX: fix this mess by using explicit storage field */
4854 type1
.t
|= (type
->t
& ~VT_TYPE
);
4859 decl_initializer(type
, sec
, c
, 0, size_only
);
4861 /* XXX: make it more general */
4862 if (!size_only
&& nb_elems
> 1) {
4863 unsigned long c_end
;
4868 tcc_error("range init not supported yet for dynamic storage");
4869 c_end
= c
+ nb_elems
* elem_size
;
4870 if (c_end
> sec
->data_allocated
)
4871 section_realloc(sec
, c_end
);
4872 src
= sec
->data
+ c
;
4874 for(i
= 1; i
< nb_elems
; i
++) {
4876 memcpy(dst
, src
, elem_size
);
4882 #define EXPR_CONST 1
4885 /* store a value or an expression directly in global data or in local array */
4886 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
4887 int v
, int expr_type
)
4889 int saved_global_expr
, bt
, bit_pos
, bit_size
;
4891 unsigned long long bit_mask
;
4899 /* compound literals must be allocated globally in this case */
4900 saved_global_expr
= global_expr
;
4903 global_expr
= saved_global_expr
;
4904 /* NOTE: symbols are accepted */
4905 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
4906 tcc_error("initializer element is not constant");
4914 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
4917 /* XXX: not portable */
4918 /* XXX: generate error if incorrect relocation */
4919 gen_assign_cast(&dtype
);
4920 bt
= type
->t
& VT_BTYPE
;
4921 /* we'll write at most 12 bytes */
4922 if (c
+ 12 > sec
->data_allocated
) {
4923 section_realloc(sec
, c
+ 12);
4925 ptr
= sec
->data
+ c
;
4926 /* XXX: make code faster ? */
4927 if (!(type
->t
& VT_BITFIELD
)) {
4932 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4933 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
4934 bit_mask
= (1LL << bit_size
) - 1;
4936 if ((vtop
->r
& VT_SYM
) &&
4942 (bt
== VT_INT
&& bit_size
!= 32)))
4943 tcc_error("initializer element is not computable at load time");
4946 vtop
->c
.i
= (vtop
->c
.i
!= 0);
4948 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4951 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4954 *(double *)ptr
= vtop
->c
.d
;
4957 *(long double *)ptr
= vtop
->c
.ld
;
4960 *(long long *)ptr
|= (vtop
->c
.ll
& bit_mask
) << bit_pos
;
4963 if (vtop
->r
& VT_SYM
) {
4964 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
4966 *(int *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4971 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
4978 /* put zeros for variable based init */
4979 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
4982 /* nothing to do because globals are already set to zero */
4984 vpush_global_sym(&func_old_type
, TOK_memset
);
4992 /* 't' contains the type and storage info. 'c' is the offset of the
4993 object in section 'sec'. If 'sec' is NULL, it means stack based
4994 allocation. 'first' is true if array '{' must be read (multi
4995 dimension implicit array init handling). 'size_only' is true if
4996 size only evaluation is wanted (only for arrays). */
4997 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
4998 int first
, int size_only
)
5000 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, parlevel1
, i
;
5001 int size1
, align1
, expr_type
;
5005 if (type
->t
& VT_VLA
) {
5006 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
5010 vpush_global_sym(&func_old_type
, TOK_alloca
);
5011 vla_runtime_type_size(type
, &a
);
5016 vsetc(type
, REG_IRET
, &retcval
);
5017 vset(type
, VT_LOCAL
|VT_LVAL
, c
);
5022 tcc_error("variable length arrays unsupported for this target");
5024 } else if (type
->t
& VT_ARRAY
) {
5028 t1
= pointed_type(type
);
5029 size1
= type_size(t1
, &align1
);
5032 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
5035 tcc_error("character array initializer must be a literal,"
5036 " optionally enclosed in braces");
5041 /* only parse strings here if correct type (otherwise: handle
5042 them as ((w)char *) expressions */
5043 if ((tok
== TOK_LSTR
&&
5044 #ifdef TCC_TARGET_PE
5045 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
5047 (t1
->t
& VT_BTYPE
) == VT_INT
5049 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
5050 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5055 /* compute maximum number of chars wanted */
5057 cstr_len
= cstr
->size
;
5059 cstr_len
= cstr
->size
/ sizeof(nwchar_t
);
5062 if (n
>= 0 && nb
> (n
- array_length
))
5063 nb
= n
- array_length
;
5066 tcc_warning("initializer-string for array is too long");
5067 /* in order to go faster for common case (char
5068 string in global variable, we handle it
5070 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
5071 memcpy(sec
->data
+ c
+ array_length
, cstr
->data
, nb
);
5075 ch
= ((unsigned char *)cstr
->data
)[i
];
5077 ch
= ((nwchar_t
*)cstr
->data
)[i
];
5078 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
5086 /* only add trailing zero if enough storage (no
5087 warning in this case since it is standard) */
5088 if (n
< 0 || array_length
< n
) {
5090 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
5096 while (tok
!= '}') {
5097 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
5098 if (n
>= 0 && index
>= n
)
5099 tcc_error("index too large");
5100 /* must put zero in holes (note that doing it that way
5101 ensures that it even works with designators) */
5102 if (!size_only
&& array_length
< index
) {
5103 init_putz(t1
, sec
, c
+ array_length
* size1
,
5104 (index
- array_length
) * size1
);
5107 if (index
> array_length
)
5108 array_length
= index
;
5109 /* special test for multi dimensional arrays (may not
5110 be strictly correct if designators are used at the
5112 if (index
>= n
&& no_oblock
)
5121 /* put zeros at the end */
5122 if (!size_only
&& n
>= 0 && array_length
< n
) {
5123 init_putz(t1
, sec
, c
+ array_length
* size1
,
5124 (n
- array_length
) * size1
);
5126 /* patch type size if needed */
5128 s
->c
= array_length
;
5129 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
5130 (sec
|| !first
|| tok
== '{')) {
5133 /* NOTE: the previous test is a specific case for automatic
5134 struct/union init */
5135 /* XXX: union needs only one init */
5137 /* XXX: this test is incorrect for local initializers
5138 beginning with ( without {. It would be much more difficult
5139 to do it correctly (ideally, the expression parser should
5140 be used in all cases) */
5146 while (tok
== '(') {
5150 if (!parse_btype(&type1
, &ad1
))
5152 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
5154 if (!is_assignable_types(type
, &type1
))
5155 tcc_error("invalid type for cast");
5160 if (first
|| tok
== '{') {
5169 while (tok
!= '}') {
5170 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
5172 if (!size_only
&& array_length
< index
) {
5173 init_putz(type
, sec
, c
+ array_length
,
5174 index
- array_length
);
5176 index
= index
+ type_size(&f
->type
, &align1
);
5177 if (index
> array_length
)
5178 array_length
= index
;
5180 /* gr: skip fields from same union - ugly. */
5182 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
5183 /* test for same offset */
5184 if (f
->next
->c
!= f
->c
)
5186 /* if yes, test for bitfield shift */
5187 if ((f
->type
.t
& VT_BITFIELD
) && (f
->next
->type
.t
& VT_BITFIELD
)) {
5188 int bit_pos_1
= (f
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5189 int bit_pos_2
= (f
->next
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5190 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
5191 if (bit_pos_1
!= bit_pos_2
)
5198 if (no_oblock
&& f
== NULL
)
5204 /* put zeros at the end */
5205 if (!size_only
&& array_length
< n
) {
5206 init_putz(type
, sec
, c
+ array_length
,
5215 } else if (tok
== '{') {
5217 decl_initializer(type
, sec
, c
, first
, size_only
);
5219 } else if (size_only
) {
5220 /* just skip expression */
5221 parlevel
= parlevel1
= 0;
5222 while ((parlevel
> 0 || parlevel1
> 0 ||
5223 (tok
!= '}' && tok
!= ',')) && tok
!= -1) {
5226 else if (tok
== ')')
5228 else if (tok
== '{')
5230 else if (tok
== '}')
5235 /* currently, we always use constant expression for globals
5236 (may change for scripting case) */
5237 expr_type
= EXPR_CONST
;
5239 expr_type
= EXPR_ANY
;
5240 init_putv(type
, sec
, c
, 0, expr_type
);
5244 /* parse an initializer for type 't' if 'has_init' is non zero, and
5245 allocate space in local or global data space ('r' is either
5246 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5247 variable 'v' with an associated name represented by 'asm_label' of
5248 scope 'scope' is declared before initializers are parsed. If 'v' is
5249 zero, then a reference to the new object is put in the value stack.
5250 If 'has_init' is 2, a special parsing is done to handle string
5252 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
5253 int has_init
, int v
, char *asm_label
,
5256 int size
, align
, addr
, data_offset
;
5258 ParseState saved_parse_state
= {0};
5259 TokenString init_str
;
5261 Sym
*flexible_array
;
5263 flexible_array
= NULL
;
5264 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
5267 while (field
&& field
->next
)
5268 field
= field
->next
;
5269 if (field
->type
.t
& VT_ARRAY
&& field
->type
.ref
->c
< 0)
5270 flexible_array
= field
;
5273 size
= type_size(type
, &align
);
5274 /* If unknown size, we must evaluate it before
5275 evaluating initializers because
5276 initializers can generate global data too
5277 (e.g. string pointers or ISOC99 compound
5278 literals). It also simplifies local
5279 initializers handling */
5280 tok_str_new(&init_str
);
5281 if (size
< 0 || (flexible_array
&& has_init
)) {
5283 tcc_error("unknown type size");
5284 /* get all init string */
5285 if (has_init
== 2) {
5286 /* only get strings */
5287 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5288 tok_str_add_tok(&init_str
);
5293 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
5295 tcc_error("unexpected end of file in initializer");
5296 tok_str_add_tok(&init_str
);
5299 else if (tok
== '}') {
5309 tok_str_add(&init_str
, -1);
5310 tok_str_add(&init_str
, 0);
5313 save_parse_state(&saved_parse_state
);
5315 macro_ptr
= init_str
.str
;
5317 decl_initializer(type
, NULL
, 0, 1, 1);
5318 /* prepare second initializer parsing */
5319 macro_ptr
= init_str
.str
;
5322 /* if still unknown size, error */
5323 size
= type_size(type
, &align
);
5325 tcc_error("unknown type size");
5328 size
+= flexible_array
->type
.ref
->c
* pointed_size(&flexible_array
->type
);
5329 /* take into account specified alignment if bigger */
5331 if (ad
->aligned
> align
)
5332 align
= ad
->aligned
;
5333 } else if (ad
->packed
) {
5336 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
5338 #ifdef CONFIG_TCC_BCHECK
5339 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5343 loc
= (loc
- size
) & -align
;
5345 #ifdef CONFIG_TCC_BCHECK
5346 /* handles bounds */
5347 /* XXX: currently, since we do only one pass, we cannot track
5348 '&' operators, so we add only arrays */
5349 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5350 unsigned long *bounds_ptr
;
5351 /* add padding between regions */
5353 /* then add local bound info */
5354 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(unsigned long));
5355 bounds_ptr
[0] = addr
;
5356 bounds_ptr
[1] = size
;
5360 /* local variable */
5361 sym_push(v
, type
, r
, addr
);
5363 /* push local reference */
5364 vset(type
, r
, addr
);
5370 if (v
&& scope
== VT_CONST
) {
5371 /* see if the symbol was already defined */
5374 if (!is_compatible_types(&sym
->type
, type
))
5375 tcc_error("incompatible types for redefinition of '%s'",
5376 get_tok_str(v
, NULL
));
5377 if (sym
->type
.t
& VT_EXTERN
) {
5378 /* if the variable is extern, it was not allocated */
5379 sym
->type
.t
&= ~VT_EXTERN
;
5380 /* set array size if it was ommited in extern
5382 if ((sym
->type
.t
& VT_ARRAY
) &&
5383 sym
->type
.ref
->c
< 0 &&
5385 sym
->type
.ref
->c
= type
->ref
->c
;
5387 /* we accept several definitions of the same
5388 global variable. this is tricky, because we
5389 must play with the SHN_COMMON type of the symbol */
5390 /* XXX: should check if the variable was already
5391 initialized. It is incorrect to initialized it
5393 /* no init data, we won't add more to the symbol */
5400 /* allocate symbol in corresponding section */
5405 else if (tcc_state
->nocommon
)
5409 data_offset
= sec
->data_offset
;
5410 data_offset
= (data_offset
+ align
- 1) & -align
;
5412 /* very important to increment global pointer at this time
5413 because initializers themselves can create new initializers */
5414 data_offset
+= size
;
5415 #ifdef CONFIG_TCC_BCHECK
5416 /* add padding if bound check */
5417 if (tcc_state
->do_bounds_check
)
5420 sec
->data_offset
= data_offset
;
5421 /* allocate section space to put the data */
5422 if (sec
->sh_type
!= SHT_NOBITS
&&
5423 data_offset
> sec
->data_allocated
)
5424 section_realloc(sec
, data_offset
);
5425 /* align section if needed */
5426 if (align
> sec
->sh_addralign
)
5427 sec
->sh_addralign
= align
;
5429 addr
= 0; /* avoid warning */
5433 if (scope
!= VT_CONST
|| !sym
) {
5434 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
5435 sym
->asm_label
= asm_label
;
5437 /* update symbol definition */
5439 put_extern_sym(sym
, sec
, addr
, size
);
5442 /* put a common area */
5443 put_extern_sym(sym
, NULL
, align
, size
);
5444 /* XXX: find a nicer way */
5445 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
5446 esym
->st_shndx
= SHN_COMMON
;
5451 /* push global reference */
5452 sym
= get_sym_ref(type
, sec
, addr
, size
);
5454 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
5457 /* patch symbol weakness */
5458 if (type
->t
& VT_WEAK
)
5460 #ifdef CONFIG_TCC_BCHECK
5461 /* handles bounds now because the symbol must be defined
5462 before for the relocation */
5463 if (tcc_state
->do_bounds_check
) {
5464 unsigned long *bounds_ptr
;
5466 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
5467 /* then add global bound info */
5468 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(long));
5469 bounds_ptr
[0] = 0; /* relocated */
5470 bounds_ptr
[1] = size
;
5474 if (has_init
|| (type
->t
& VT_VLA
)) {
5475 decl_initializer(type
, sec
, addr
, 1, 0);
5476 /* restore parse state if needed */
5478 tok_str_free(init_str
.str
);
5479 restore_parse_state(&saved_parse_state
);
5481 /* patch flexible array member size back to -1, */
5482 /* for possible subsequent similar declarations */
5484 flexible_array
->type
.ref
->c
= -1;
5489 static void put_func_debug(Sym
*sym
)
5494 /* XXX: we put here a dummy type */
5495 snprintf(buf
, sizeof(buf
), "%s:%c1",
5496 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
5497 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
5498 cur_text_section
, sym
->c
);
5499 /* //gr gdb wants a line at the function */
5500 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
5505 /* parse an old style function declaration list */
5506 /* XXX: check multiple parameter */
5507 static void func_decl_list(Sym
*func_sym
)
5514 /* parse each declaration */
5515 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
&&
5516 tok
!= TOK_ASM1
&& tok
!= TOK_ASM2
&& tok
!= TOK_ASM3
) {
5517 if (!parse_btype(&btype
, &ad
))
5518 expect("declaration list");
5519 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5520 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5522 /* we accept no variable after */
5526 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5527 /* find parameter in function parameter list */
5530 if ((s
->v
& ~SYM_FIELD
) == v
)
5534 tcc_error("declaration for parameter '%s' but no such parameter",
5535 get_tok_str(v
, NULL
));
5537 /* check that no storage specifier except 'register' was given */
5538 if (type
.t
& VT_STORAGE
)
5539 tcc_error("storage class specified for '%s'", get_tok_str(v
, NULL
));
5540 convert_parameter_type(&type
);
5541 /* we can add the type (NOTE: it could be local to the function) */
5543 /* accept other parameters */
5554 /* parse a function defined by symbol 'sym' and generate its code in
5555 'cur_text_section' */
5556 static void gen_function(Sym
*sym
)
5558 int saved_nocode_wanted
= nocode_wanted
;
5560 ind
= cur_text_section
->data_offset
;
5561 /* NOTE: we patch the symbol size later */
5562 put_extern_sym(sym
, cur_text_section
, ind
, 0);
5563 funcname
= get_tok_str(sym
->v
, NULL
);
5565 /* put debug symbol */
5566 if (tcc_state
->do_debug
)
5567 put_func_debug(sym
);
5568 /* push a dummy symbol to enable local sym storage */
5569 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
5570 gfunc_prolog(&sym
->type
);
5572 block(NULL
, NULL
, NULL
, NULL
, 0, 0);
5575 cur_text_section
->data_offset
= ind
;
5576 label_pop(&global_label_stack
, NULL
);
5577 /* reset local stack */
5578 scope_stack_bottom
= NULL
;
5579 sym_pop(&local_stack
, NULL
);
5580 /* end of function */
5581 /* patch symbol size */
5582 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
5584 /* patch symbol weakness (this definition overrules any prototype) */
5585 if (sym
->type
.t
& VT_WEAK
)
5587 if (tcc_state
->do_debug
) {
5588 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
5590 /* It's better to crash than to generate wrong code */
5591 cur_text_section
= NULL
;
5592 funcname
= ""; /* for safety */
5593 func_vt
.t
= VT_VOID
; /* for safety */
5594 ind
= 0; /* for safety */
5595 nocode_wanted
= saved_nocode_wanted
;
5598 ST_FUNC
void gen_inline_functions(void)
5601 int *str
, inline_generated
, i
;
5602 struct InlineFunc
*fn
;
5604 /* iterate while inline function are referenced */
5606 inline_generated
= 0;
5607 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5608 fn
= tcc_state
->inline_fns
[i
];
5610 if (sym
&& sym
->c
) {
5611 /* the function was used: generate its code and
5612 convert it to a normal function */
5613 str
= fn
->token_str
;
5616 pstrcpy(file
->filename
, sizeof file
->filename
, fn
->filename
);
5617 sym
->r
= VT_SYM
| VT_CONST
;
5618 sym
->type
.t
&= ~VT_INLINE
;
5622 cur_text_section
= text_section
;
5624 macro_ptr
= NULL
; /* fail safe */
5626 inline_generated
= 1;
5629 if (!inline_generated
)
5632 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5633 fn
= tcc_state
->inline_fns
[i
];
5634 str
= fn
->token_str
;
5637 dynarray_reset(&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
);
5640 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
5641 static int decl0(int l
, int is_for_loop_init
)
5649 if (!parse_btype(&btype
, &ad
)) {
5650 if (is_for_loop_init
)
5652 /* skip redundant ';' */
5653 /* XXX: find more elegant solution */
5658 if (l
== VT_CONST
&&
5659 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5660 /* global asm block */
5664 /* special test for old K&R protos without explicit int
5665 type. Only accepted when defining global data */
5666 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
5670 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5671 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5673 /* we accept no variable after */
5677 while (1) { /* iterate thru each declaration */
5678 char *asm_label
; // associated asm label
5680 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5684 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
5685 printf("type = '%s'\n", buf
);
5688 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5689 if ((type
.t
& VT_STATIC
) && (l
== VT_LOCAL
)) {
5690 tcc_error("function without file scope cannot be static");
5692 /* if old style function prototype, we accept a
5695 if (sym
->c
== FUNC_OLD
)
5696 func_decl_list(sym
);
5700 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5703 asm_label_instr(&astr
);
5704 asm_label
= tcc_strdup(astr
.data
);
5707 /* parse one last attribute list, after asm label */
5708 parse_attribute(&ad
);
5713 #ifdef TCC_TARGET_PE
5715 type
.t
|= VT_IMPORT
;
5717 type
.t
|= VT_EXPORT
;
5721 tcc_error("cannot use local functions");
5722 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
5723 expect("function definition");
5725 /* reject abstract declarators in function definition */
5727 while ((sym
= sym
->next
) != NULL
)
5728 if (!(sym
->v
& ~SYM_FIELD
))
5729 expect("identifier");
5731 /* XXX: cannot do better now: convert extern line to static inline */
5732 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
5733 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5737 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
5740 r
= sym
->type
.ref
->r
;
5741 /* use func_call from prototype if not defined */
5742 if (FUNC_CALL(r
) != FUNC_CDECL
5743 && FUNC_CALL(type
.ref
->r
) == FUNC_CDECL
)
5744 FUNC_CALL(type
.ref
->r
) = FUNC_CALL(r
);
5746 /* use export from prototype */
5748 FUNC_EXPORT(type
.ref
->r
) = 1;
5750 /* use static from prototype */
5751 if (sym
->type
.t
& VT_STATIC
)
5752 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5754 if (!is_compatible_types(&sym
->type
, &type
)) {
5756 tcc_error("incompatible types for redefinition of '%s'",
5757 get_tok_str(v
, NULL
));
5759 /* if symbol is already defined, then put complete type */
5762 /* put function symbol */
5763 sym
= global_identifier_push(v
, type
.t
, 0);
5764 sym
->type
.ref
= type
.ref
;
5767 /* static inline functions are just recorded as a kind
5768 of macro. Their code will be emitted at the end of
5769 the compilation unit only if they are used */
5770 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
5771 (VT_INLINE
| VT_STATIC
)) {
5772 TokenString func_str
;
5774 struct InlineFunc
*fn
;
5775 const char *filename
;
5777 tok_str_new(&func_str
);
5783 tcc_error("unexpected end of file");
5784 tok_str_add_tok(&func_str
);
5789 } else if (t
== '}') {
5791 if (block_level
== 0)
5795 tok_str_add(&func_str
, -1);
5796 tok_str_add(&func_str
, 0);
5797 filename
= file
? file
->filename
: "";
5798 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
5799 strcpy(fn
->filename
, filename
);
5801 fn
->token_str
= func_str
.str
;
5802 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
5805 /* compute text section */
5806 cur_text_section
= ad
.section
;
5807 if (!cur_text_section
)
5808 cur_text_section
= text_section
;
5809 sym
->r
= VT_SYM
| VT_CONST
;
5814 if (btype
.t
& VT_TYPEDEF
) {
5815 /* save typedefed type */
5816 /* XXX: test storage specifiers ? */
5817 sym
= sym_push(v
, &type
, INT_ATTR(&ad
), 0);
5818 sym
->type
.t
|= VT_TYPEDEF
;
5821 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5822 /* external function definition */
5823 /* specific case for func_call attribute */
5824 type
.ref
->r
= INT_ATTR(&ad
);
5825 } else if (!(type
.t
& VT_ARRAY
)) {
5826 /* not lvalue if array */
5827 r
|= lvalue_type(type
.t
);
5829 has_init
= (tok
== '=');
5830 if (has_init
&& (type
.t
& VT_VLA
))
5831 tcc_error("Variable length array cannot be initialized");
5832 if ((btype
.t
& VT_EXTERN
) || ((type
.t
& VT_BTYPE
) == VT_FUNC
) ||
5833 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
5834 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
5835 /* external variable or function */
5836 /* NOTE: as GCC, uninitialized global static
5837 arrays of null size are considered as
5839 sym
= external_sym(v
, &type
, r
, asm_label
);
5841 if (type
.t
& VT_WEAK
)
5844 if (ad
.alias_target
) {
5849 alias_target
= sym_find(ad
.alias_target
);
5850 if (!alias_target
|| !alias_target
->c
)
5851 tcc_error("unsupported forward __alias__ attribute");
5852 esym
= &((Elf32_Sym
*)symtab_section
->data
)[alias_target
->c
];
5853 tsec
.sh_num
= esym
->st_shndx
;
5854 put_extern_sym2(sym
, &tsec
, esym
->st_value
, esym
->st_size
, 0);
5857 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
5858 if (type
.t
& VT_STATIC
)
5864 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, asm_label
, l
);
5868 if (is_for_loop_init
)
5881 ST_FUNC
void decl(int l
)