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
*define_stack
;
54 ST_DATA Sym
*global_label_stack
;
55 ST_DATA Sym
*local_label_stack
;
57 ST_DATA SValue vstack
[VSTACK_SIZE
], *vtop
;
59 ST_DATA
int const_wanted
; /* true if constant wanted */
60 ST_DATA
int nocode_wanted
; /* true if no code generation wanted for an expression */
61 ST_DATA
int global_expr
; /* true if compound literals must be allocated globally (used during initializers parsing */
62 ST_DATA CType func_vt
; /* current function return type (used by return instruction) */
64 ST_DATA
int last_line_num
, last_ind
, func_ind
; /* debug last line number and pc */
65 ST_DATA
char *funcname
;
67 ST_DATA CType char_pointer_type
, func_old_type
, int_type
;
69 /* ------------------------------------------------------------------------- */
70 static void gen_cast(CType
*type
);
71 static inline CType
*pointed_type(CType
*type
);
72 static int is_compatible_types(CType
*type1
, CType
*type2
);
73 static int parse_btype(CType
*type
, AttributeDef
*ad
);
74 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
);
75 static void parse_expr_type(CType
*type
);
76 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
, int first
, int size_only
);
77 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
, int case_reg
, int is_expr
);
78 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
, int has_init
, int v
, char *asm_label
, int scope
);
79 static int decl0(int l
, int is_for_loop_init
);
80 static void expr_eq(void);
81 static void unary_type(CType
*type
);
82 static void vla_runtime_type_size(CType
*type
, int *a
);
83 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
);
84 static void expr_type(CType
*type
);
86 ST_INLN
int is_float(int t
)
90 return bt
== VT_LDOUBLE
|| bt
== VT_DOUBLE
|| bt
== VT_FLOAT
;
93 /* we use our own 'finite' function to avoid potential problems with
94 non standard math libs */
95 /* XXX: endianness dependent */
96 ST_FUNC
int ieee_finite(double d
)
99 return ((unsigned)((p
[1] | 0x800fffff) + 1)) >> 31;
102 ST_FUNC
void test_lvalue(void)
104 if (!(vtop
->r
& VT_LVAL
))
108 /* ------------------------------------------------------------------------- */
109 /* symbol allocator */
110 static Sym
*__sym_malloc(void)
112 Sym
*sym_pool
, *sym
, *last_sym
;
115 sym_pool
= tcc_malloc(SYM_POOL_NB
* sizeof(Sym
));
116 dynarray_add(&sym_pools
, &nb_sym_pools
, sym_pool
);
118 last_sym
= sym_free_first
;
120 for(i
= 0; i
< SYM_POOL_NB
; i
++) {
121 sym
->next
= last_sym
;
125 sym_free_first
= last_sym
;
129 static inline Sym
*sym_malloc(void)
132 sym
= sym_free_first
;
134 sym
= __sym_malloc();
135 sym_free_first
= sym
->next
;
139 ST_INLN
void sym_free(Sym
*sym
)
141 sym
->next
= sym_free_first
;
142 tcc_free(sym
->asm_label
);
143 sym_free_first
= sym
;
146 /* push, without hashing */
147 ST_FUNC Sym
*sym_push2(Sym
**ps
, int v
, int t
, long c
)
166 /* find a symbol and return its associated structure. 's' is the top
167 of the symbol stack */
168 ST_FUNC Sym
*sym_find2(Sym
*s
, int v
)
178 /* structure lookup */
179 ST_INLN Sym
*struct_find(int v
)
182 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
184 return table_ident
[v
]->sym_struct
;
187 /* find an identifier */
188 ST_INLN Sym
*sym_find(int v
)
191 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
193 return table_ident
[v
]->sym_identifier
;
196 /* push a given symbol on the symbol stack */
197 ST_FUNC Sym
*sym_push(int v
, CType
*type
, int r
, int c
)
206 s
= sym_push2(ps
, v
, type
->t
, c
);
207 s
->type
.ref
= type
->ref
;
209 /* don't record fields or anonymous symbols */
211 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
212 /* record symbol in token array */
213 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
215 ps
= &ts
->sym_struct
;
217 ps
= &ts
->sym_identifier
;
224 /* push a global identifier */
225 ST_FUNC Sym
*global_identifier_push(int v
, int t
, int c
)
228 s
= sym_push2(&global_stack
, v
, t
, c
);
229 /* don't record anonymous symbol */
230 if (v
< SYM_FIRST_ANOM
) {
231 ps
= &table_ident
[v
- TOK_IDENT
]->sym_identifier
;
232 /* modify the top most local identifier, so that
233 sym_identifier will point to 's' when popped */
235 ps
= &(*ps
)->prev_tok
;
242 /* pop symbols until top reaches 'b' */
243 ST_FUNC
void sym_pop(Sym
**ptop
, Sym
*b
)
253 /* remove symbol in token array */
255 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
256 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
258 ps
= &ts
->sym_struct
;
260 ps
= &ts
->sym_identifier
;
269 static void weaken_symbol(Sym
*sym
)
271 sym
->type
.t
|= VT_WEAK
;
276 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
277 esym_type
= ELFW(ST_TYPE
)(esym
->st_info
);
278 esym
->st_info
= ELFW(ST_INFO
)(STB_WEAK
, esym_type
);
282 /* ------------------------------------------------------------------------- */
284 ST_FUNC
void swap(int *p
, int *q
)
292 static void vsetc(CType
*type
, int r
, CValue
*vc
)
296 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
297 tcc_error("memory full");
298 /* cannot let cpu flags if other instruction are generated. Also
299 avoid leaving VT_JMP anywhere except on the top of the stack
300 because it would complicate the code generator. */
301 if (vtop
>= vstack
) {
302 v
= vtop
->r
& VT_VALMASK
;
303 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
313 /* push constant of type "type" with useless value */
314 void vpush(CType
*type
)
317 vsetc(type
, VT_CONST
, &cval
);
320 /* push integer constant */
321 ST_FUNC
void vpushi(int v
)
325 vsetc(&int_type
, VT_CONST
, &cval
);
328 /* push long long constant */
329 static void vpushll(long long v
)
336 vsetc(&ctype
, VT_CONST
, &cval
);
339 /* push arbitrary 64bit constant */
340 void vpush64(int ty
, unsigned long long v
)
346 vsetc(&ctype
, VT_CONST
, &cval
);
349 /* Return a static symbol pointing to a section */
350 ST_FUNC Sym
*get_sym_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
356 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
357 sym
->type
.ref
= type
->ref
;
358 sym
->r
= VT_CONST
| VT_SYM
;
359 put_extern_sym(sym
, sec
, offset
, size
);
363 /* push a reference to a section offset by adding a dummy symbol */
364 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
369 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
370 vtop
->sym
= get_sym_ref(type
, sec
, offset
, size
);
373 /* define a new external reference to a symbol 'v' of type 'u' */
374 ST_FUNC Sym
*external_global_sym(int v
, CType
*type
, int r
)
380 /* push forward reference */
381 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
382 s
->type
.ref
= type
->ref
;
383 s
->r
= r
| VT_CONST
| VT_SYM
;
388 /* define a new external reference to a symbol 'v' with alternate asm
389 name 'asm_label' of type 'u'. 'asm_label' is equal to NULL if there
390 is no alternate name (most cases) */
391 static Sym
*external_sym(int v
, CType
*type
, int r
, char *asm_label
)
397 /* push forward reference */
398 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
399 s
->asm_label
= asm_label
;
400 s
->type
.t
|= VT_EXTERN
;
401 } else if (s
->type
.ref
== func_old_type
.ref
) {
402 s
->type
.ref
= type
->ref
;
403 s
->r
= r
| VT_CONST
| VT_SYM
;
404 s
->type
.t
|= VT_EXTERN
;
405 } else if (!is_compatible_types(&s
->type
, type
)) {
406 tcc_error("incompatible types for redefinition of '%s'",
407 get_tok_str(v
, NULL
));
412 /* push a reference to global symbol v */
413 ST_FUNC
void vpush_global_sym(CType
*type
, int v
)
418 sym
= external_global_sym(v
, type
, 0);
420 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
424 ST_FUNC
void vset(CType
*type
, int r
, int v
)
429 vsetc(type
, r
, &cval
);
432 static void vseti(int r
, int v
)
440 ST_FUNC
void vswap(void)
449 ST_FUNC
void vpushv(SValue
*v
)
451 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
452 tcc_error("memory full");
457 static void vdup(void)
462 /* save r to the memory stack, and mark it as being free */
463 ST_FUNC
void save_reg(int r
)
465 int l
, saved
, size
, align
;
469 /* modify all stack values */
472 for(p
=vstack
;p
<=vtop
;p
++) {
473 if ((p
->r
& VT_VALMASK
) == r
||
474 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
475 /* must save value on stack if not already done */
477 /* NOTE: must reload 'r' because r might be equal to r2 */
478 r
= p
->r
& VT_VALMASK
;
479 /* store register in the stack */
481 if ((p
->r
& VT_LVAL
) ||
482 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
483 #ifdef TCC_TARGET_X86_64
484 type
= &char_pointer_type
;
488 size
= type_size(type
, &align
);
489 loc
= (loc
- size
) & -align
;
491 sv
.r
= VT_LOCAL
| VT_LVAL
;
494 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
495 /* x86 specific: need to pop fp register ST0 if saved */
497 o(0xd8dd); /* fstp %st(0) */
500 #ifndef TCC_TARGET_X86_64
501 /* special long long case */
502 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
510 /* mark that stack entry as being saved on the stack */
511 if (p
->r
& VT_LVAL
) {
512 /* also clear the bounded flag because the
513 relocation address of the function was stored in
515 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
517 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
525 #ifdef TCC_TARGET_ARM
526 /* find a register of class 'rc2' with at most one reference on stack.
527 * If none, call get_reg(rc) */
528 ST_FUNC
int get_reg_ex(int rc
, int rc2
)
533 for(r
=0;r
<NB_REGS
;r
++) {
534 if (reg_classes
[r
] & rc2
) {
537 for(p
= vstack
; p
<= vtop
; p
++) {
538 if ((p
->r
& VT_VALMASK
) == r
||
539 (p
->r2
& VT_VALMASK
) == r
)
550 /* find a free register of class 'rc'. If none, save one register */
551 ST_FUNC
int get_reg(int rc
)
556 /* find a free register */
557 for(r
=0;r
<NB_REGS
;r
++) {
558 if (reg_classes
[r
] & rc
) {
559 for(p
=vstack
;p
<=vtop
;p
++) {
560 if ((p
->r
& VT_VALMASK
) == r
||
561 (p
->r2
& VT_VALMASK
) == r
)
569 /* no register left : free the first one on the stack (VERY
570 IMPORTANT to start from the bottom to ensure that we don't
571 spill registers used in gen_opi()) */
572 for(p
=vstack
;p
<=vtop
;p
++) {
573 r
= p
->r
& VT_VALMASK
;
574 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
576 /* also look at second register (if long long) */
577 r
= p
->r2
& VT_VALMASK
;
578 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
584 /* Should never comes here */
588 /* save registers up to (vtop - n) stack entry */
589 ST_FUNC
void save_regs(int n
)
594 for(p
= vstack
;p
<= p1
; p
++) {
595 r
= p
->r
& VT_VALMASK
;
602 /* move register 's' to 'r', and flush previous value of r to memory
604 static void move_reg(int r
, int s
)
617 /* get address of vtop (vtop MUST BE an lvalue) */
618 static void gaddrof(void)
620 if (vtop
->r
& VT_REF
)
623 /* tricky: if saved lvalue, then we can go back to lvalue */
624 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
625 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
630 #ifdef CONFIG_TCC_BCHECK
631 /* generate lvalue bound code */
632 static void gbound(void)
637 vtop
->r
&= ~VT_MUSTBOUND
;
638 /* if lvalue, then use checking code before dereferencing */
639 if (vtop
->r
& VT_LVAL
) {
640 /* if not VT_BOUNDED value, then make one */
641 if (!(vtop
->r
& VT_BOUNDED
)) {
642 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
643 /* must save type because we must set it to int to get pointer */
645 vtop
->type
.t
= VT_INT
;
648 gen_bounded_ptr_add();
649 vtop
->r
|= lval_type
;
652 /* then check for dereferencing */
653 gen_bounded_ptr_deref();
658 /* store vtop a register belonging to class 'rc'. lvalues are
659 converted to values. Cannot be used if cannot be converted to
660 register value (such as structures). */
661 ST_FUNC
int gv(int rc
)
663 int r
, bit_pos
, bit_size
, size
, align
, i
;
664 #ifndef TCC_TARGET_X86_64
668 /* NOTE: get_reg can modify vstack[] */
669 if (vtop
->type
.t
& VT_BITFIELD
) {
672 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
673 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
674 /* remove bit field info to avoid loops */
675 vtop
->type
.t
&= ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
676 /* cast to int to propagate signedness in following ops */
677 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
682 if((vtop
->type
.t
& VT_UNSIGNED
) ||
683 (vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
684 type
.t
|= VT_UNSIGNED
;
686 /* generate shifts */
687 vpushi(bits
- (bit_pos
+ bit_size
));
689 vpushi(bits
- bit_size
);
690 /* NOTE: transformed to SHR if unsigned */
694 if (is_float(vtop
->type
.t
) &&
695 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
698 unsigned long offset
;
699 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
703 /* XXX: unify with initializers handling ? */
704 /* CPUs usually cannot use float constants, so we store them
705 generically in data segment */
706 size
= type_size(&vtop
->type
, &align
);
707 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
708 data_section
->data_offset
= offset
;
709 /* XXX: not portable yet */
710 #if defined(__i386__) || defined(__x86_64__)
711 /* Zero pad x87 tenbyte long doubles */
712 if (size
== LDOUBLE_SIZE
) {
713 vtop
->c
.tab
[2] &= 0xffff;
714 #if LDOUBLE_SIZE == 16
719 ptr
= section_ptr_add(data_section
, size
);
721 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
725 ptr
[i
] = vtop
->c
.tab
[size
-1-i
];
729 ptr
[i
] = vtop
->c
.tab
[i
];
730 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
731 vtop
->r
|= VT_LVAL
| VT_SYM
;
735 #ifdef CONFIG_TCC_BCHECK
736 if (vtop
->r
& VT_MUSTBOUND
)
740 r
= vtop
->r
& VT_VALMASK
;
741 #ifndef TCC_TARGET_X86_64
746 /* need to reload if:
748 - lvalue (need to dereference pointer)
749 - already a register, but not in the right class */
751 || (vtop
->r
& VT_LVAL
)
752 || !(reg_classes
[r
] & rc
)
753 #ifndef TCC_TARGET_X86_64
754 || ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
759 #ifndef TCC_TARGET_X86_64
760 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
762 unsigned long long ll
;
763 /* two register type load : expand to two words
765 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
768 vtop
->c
.ui
= ll
; /* first word */
770 vtop
->r
= r
; /* save register value */
771 vpushi(ll
>> 32); /* second word */
772 } else if (r
>= VT_CONST
|| /* XXX: test to VT_CONST incorrect ? */
773 (vtop
->r
& VT_LVAL
)) {
774 /* We do not want to modifier the long long
775 pointer here, so the safest (and less
776 efficient) is to save all the other registers
777 in the stack. XXX: totally inefficient. */
779 /* load from memory */
782 vtop
[-1].r
= r
; /* save register value */
783 /* increment pointer to get second word */
784 vtop
->type
.t
= VT_INT
;
793 vtop
[-1].r
= r
; /* save register value */
794 vtop
->r
= vtop
[-1].r2
;
796 /* allocate second register */
800 /* write second register */
804 if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
806 /* lvalue of scalar type : need to use lvalue type
807 because of possible cast */
810 /* compute memory access type */
811 if (vtop
->r
& VT_LVAL_BYTE
)
813 else if (vtop
->r
& VT_LVAL_SHORT
)
815 if (vtop
->r
& VT_LVAL_UNSIGNED
)
819 /* restore wanted type */
822 /* one register type load */
827 #ifdef TCC_TARGET_C67
828 /* uses register pairs for doubles */
829 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
836 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
837 ST_FUNC
void gv2(int rc1
, int rc2
)
841 /* generate more generic register first. But VT_JMP or VT_CMP
842 values must be generated first in all cases to avoid possible
844 v
= vtop
[0].r
& VT_VALMASK
;
845 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
850 /* test if reload is needed for first register */
851 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
861 /* test if reload is needed for first register */
862 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
868 /* wrapper around RC_FRET to return a register by type */
869 static int rc_fret(int t
)
871 #ifdef TCC_TARGET_X86_64
872 if (t
== VT_LDOUBLE
) {
879 /* wrapper around REG_FRET to return a register by type */
880 static int reg_fret(int t
)
882 #ifdef TCC_TARGET_X86_64
883 if (t
== VT_LDOUBLE
) {
890 /* expand long long on stack in two int registers */
891 static void lexpand(void)
895 u
= vtop
->type
.t
& VT_UNSIGNED
;
898 vtop
[0].r
= vtop
[-1].r2
;
899 vtop
[0].r2
= VT_CONST
;
900 vtop
[-1].r2
= VT_CONST
;
901 vtop
[0].type
.t
= VT_INT
| u
;
902 vtop
[-1].type
.t
= VT_INT
| u
;
905 #ifdef TCC_TARGET_ARM
906 /* expand long long on stack */
907 ST_FUNC
void lexpand_nr(void)
911 u
= vtop
->type
.t
& VT_UNSIGNED
;
914 vtop
->type
.t
= VT_INT
| u
;
915 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
917 vtop
[-1].c
.ui
= vtop
->c
.ull
;
918 vtop
->c
.ui
= vtop
->c
.ull
>> 32;
920 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
922 vtop
->r
= vtop
[-1].r
;
923 } else if (v
> VT_CONST
) {
927 vtop
->r
= vtop
[-1].r2
;
928 vtop
[-1].r2
= VT_CONST
;
929 vtop
[-1].type
.t
= VT_INT
| u
;
933 /* build a long long from two ints */
934 static void lbuild(int t
)
937 vtop
[-1].r2
= vtop
[0].r
;
942 /* rotate n first stack elements to the bottom
943 I1 ... In -> I2 ... In I1 [top is right]
945 static void vrotb(int n
)
956 /* rotate n first stack elements to the top
957 I1 ... In -> In I1 ... I(n-1) [top is right]
959 ST_FUNC
void vrott(int n
)
965 for(i
= 0;i
< n
- 1; i
++)
966 vtop
[-i
] = vtop
[-i
- 1];
970 #ifdef TCC_TARGET_ARM
971 /* like vrott but in other direction
972 In ... I1 -> I(n-1) ... I1 In [top is right]
974 ST_FUNC
void vnrott(int n
)
980 for(i
= n
- 1; i
> 0; i
--)
981 vtop
[-i
] = vtop
[-i
+ 1];
986 /* pop stack value */
987 ST_FUNC
void vpop(void)
990 v
= vtop
->r
& VT_VALMASK
;
991 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
992 /* for x86, we need to pop the FP stack */
993 if (v
== TREG_ST0
&& !nocode_wanted
) {
994 o(0xd8dd); /* fstp %st(0) */
997 if (v
== VT_JMP
|| v
== VT_JMPI
) {
998 /* need to put correct jump if && or || without test */
1004 /* convert stack entry to register and duplicate its value in another
1006 static void gv_dup(void)
1012 if ((t
& VT_BTYPE
) == VT_LLONG
) {
1019 /* stack: H L L1 H1 */
1027 /* duplicate value */
1032 #ifdef TCC_TARGET_X86_64
1033 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1043 load(r1
, &sv
); /* move r to r1 */
1045 /* duplicates value */
1051 #ifndef TCC_TARGET_X86_64
1052 /* generate CPU independent (unsigned) long long operations */
1053 static void gen_opl(int op
)
1055 int t
, a
, b
, op1
, c
, i
;
1057 unsigned short reg_iret
= REG_IRET
;
1058 unsigned short reg_lret
= REG_LRET
;
1064 func
= TOK___divdi3
;
1067 func
= TOK___udivdi3
;
1070 func
= TOK___moddi3
;
1073 func
= TOK___umoddi3
;
1080 /* call generic long long function */
1081 vpush_global_sym(&func_old_type
, func
);
1086 vtop
->r2
= reg_lret
;
1099 /* stack: L1 H1 L2 H2 */
1104 vtop
[-2] = vtop
[-3];
1107 /* stack: H1 H2 L1 L2 */
1113 /* stack: H1 H2 L1 L2 ML MH */
1116 /* stack: ML MH H1 H2 L1 L2 */
1120 /* stack: ML MH H1 L2 H2 L1 */
1125 /* stack: ML MH M1 M2 */
1128 } else if (op
== '+' || op
== '-') {
1129 /* XXX: add non carry method too (for MIPS or alpha) */
1135 /* stack: H1 H2 (L1 op L2) */
1138 gen_op(op1
+ 1); /* TOK_xxxC2 */
1141 /* stack: H1 H2 (L1 op L2) */
1144 /* stack: (L1 op L2) H1 H2 */
1146 /* stack: (L1 op L2) (H1 op H2) */
1154 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1155 t
= vtop
[-1].type
.t
;
1159 /* stack: L H shift */
1161 /* constant: simpler */
1162 /* NOTE: all comments are for SHL. the other cases are
1163 done by swaping words */
1174 if (op
!= TOK_SAR
) {
1207 /* XXX: should provide a faster fallback on x86 ? */
1210 func
= TOK___ashrdi3
;
1213 func
= TOK___lshrdi3
;
1216 func
= TOK___ashldi3
;
1222 /* compare operations */
1228 /* stack: L1 H1 L2 H2 */
1230 vtop
[-1] = vtop
[-2];
1232 /* stack: L1 L2 H1 H2 */
1235 /* when values are equal, we need to compare low words. since
1236 the jump is inverted, we invert the test too. */
1239 else if (op1
== TOK_GT
)
1241 else if (op1
== TOK_ULT
)
1243 else if (op1
== TOK_UGT
)
1248 if (op1
!= TOK_NE
) {
1252 /* generate non equal test */
1253 /* XXX: NOT PORTABLE yet */
1257 #if defined(TCC_TARGET_I386)
1258 b
= psym(0x850f, 0);
1259 #elif defined(TCC_TARGET_ARM)
1261 o(0x1A000000 | encbranch(ind
, 0, 1));
1262 #elif defined(TCC_TARGET_C67)
1263 tcc_error("not implemented");
1265 #error not supported
1269 /* compare low. Always unsigned */
1273 else if (op1
== TOK_LE
)
1275 else if (op1
== TOK_GT
)
1277 else if (op1
== TOK_GE
)
1288 /* handle integer constant optimizations and various machine
1290 static void gen_opic(int op
)
1292 int c1
, c2
, t1
, t2
, n
;
1295 typedef unsigned long long U
;
1299 t1
= v1
->type
.t
& VT_BTYPE
;
1300 t2
= v2
->type
.t
& VT_BTYPE
;
1304 else if (v1
->type
.t
& VT_UNSIGNED
)
1311 else if (v2
->type
.t
& VT_UNSIGNED
)
1316 /* currently, we cannot do computations with forward symbols */
1317 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1318 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1321 case '+': l1
+= l2
; break;
1322 case '-': l1
-= l2
; break;
1323 case '&': l1
&= l2
; break;
1324 case '^': l1
^= l2
; break;
1325 case '|': l1
|= l2
; break;
1326 case '*': l1
*= l2
; break;
1333 /* if division by zero, generate explicit division */
1336 tcc_error("division by zero in constant");
1340 default: l1
/= l2
; break;
1341 case '%': l1
%= l2
; break;
1342 case TOK_UDIV
: l1
= (U
)l1
/ l2
; break;
1343 case TOK_UMOD
: l1
= (U
)l1
% l2
; break;
1346 case TOK_SHL
: l1
<<= l2
; break;
1347 case TOK_SHR
: l1
= (U
)l1
>> l2
; break;
1348 case TOK_SAR
: l1
>>= l2
; break;
1350 case TOK_ULT
: l1
= (U
)l1
< (U
)l2
; break;
1351 case TOK_UGE
: l1
= (U
)l1
>= (U
)l2
; break;
1352 case TOK_EQ
: l1
= l1
== l2
; break;
1353 case TOK_NE
: l1
= l1
!= l2
; break;
1354 case TOK_ULE
: l1
= (U
)l1
<= (U
)l2
; break;
1355 case TOK_UGT
: l1
= (U
)l1
> (U
)l2
; break;
1356 case TOK_LT
: l1
= l1
< l2
; break;
1357 case TOK_GE
: l1
= l1
>= l2
; break;
1358 case TOK_LE
: l1
= l1
<= l2
; break;
1359 case TOK_GT
: l1
= l1
> l2
; break;
1361 case TOK_LAND
: l1
= l1
&& l2
; break;
1362 case TOK_LOR
: l1
= l1
|| l2
; break;
1369 /* if commutative ops, put c2 as constant */
1370 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1371 op
== '|' || op
== '*')) {
1373 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1374 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1376 /* Filter out NOP operations like x*1, x-0, x&-1... */
1377 if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1380 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1381 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1387 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1388 /* try to use shifts instead of muls or divs */
1389 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1398 else if (op
== TOK_PDIV
)
1404 } else if (c2
&& (op
== '+' || op
== '-') &&
1405 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
1406 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1407 /* symbol + constant case */
1414 if (!nocode_wanted
) {
1415 /* call low level op generator */
1416 if (t1
== VT_LLONG
|| t2
== VT_LLONG
)
1427 /* generate a floating point operation with constant propagation */
1428 static void gen_opif(int op
)
1436 /* currently, we cannot do computations with forward symbols */
1437 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1438 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1440 if (v1
->type
.t
== VT_FLOAT
) {
1443 } else if (v1
->type
.t
== VT_DOUBLE
) {
1451 /* NOTE: we only do constant propagation if finite number (not
1452 NaN or infinity) (ANSI spec) */
1453 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1457 case '+': f1
+= f2
; break;
1458 case '-': f1
-= f2
; break;
1459 case '*': f1
*= f2
; break;
1463 tcc_error("division by zero in constant");
1468 /* XXX: also handles tests ? */
1472 /* XXX: overflow test ? */
1473 if (v1
->type
.t
== VT_FLOAT
) {
1475 } else if (v1
->type
.t
== VT_DOUBLE
) {
1483 if (!nocode_wanted
) {
1491 static int pointed_size(CType
*type
)
1494 return type_size(pointed_type(type
), &align
);
1497 static void vla_runtime_pointed_size(CType
*type
)
1500 vla_runtime_type_size(pointed_type(type
), &align
);
1503 static inline int is_null_pointer(SValue
*p
)
1505 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1507 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& p
->c
.i
== 0) ||
1508 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.ll
== 0);
1511 static inline int is_integer_btype(int bt
)
1513 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
1514 bt
== VT_INT
|| bt
== VT_LLONG
);
1517 /* check types for comparison or substraction of pointers */
1518 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
1520 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
1523 /* null pointers are accepted for all comparisons as gcc */
1524 if (is_null_pointer(p1
) || is_null_pointer(p2
))
1528 bt1
= type1
->t
& VT_BTYPE
;
1529 bt2
= type2
->t
& VT_BTYPE
;
1530 /* accept comparison between pointer and integer with a warning */
1531 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
1532 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
1533 tcc_warning("comparison between pointer and integer");
1537 /* both must be pointers or implicit function pointers */
1538 if (bt1
== VT_PTR
) {
1539 type1
= pointed_type(type1
);
1540 } else if (bt1
!= VT_FUNC
)
1541 goto invalid_operands
;
1543 if (bt2
== VT_PTR
) {
1544 type2
= pointed_type(type2
);
1545 } else if (bt2
!= VT_FUNC
) {
1547 tcc_error("invalid operands to binary %s", get_tok_str(op
, NULL
));
1549 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1550 (type2
->t
& VT_BTYPE
) == VT_VOID
)
1554 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1555 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1556 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1557 /* gcc-like error if '-' is used */
1559 goto invalid_operands
;
1561 tcc_warning("comparison of distinct pointer types lacks a cast");
1565 /* generic gen_op: handles types problems */
1566 ST_FUNC
void gen_op(int op
)
1568 int u
, t1
, t2
, bt1
, bt2
, t
;
1571 t1
= vtop
[-1].type
.t
;
1572 t2
= vtop
[0].type
.t
;
1573 bt1
= t1
& VT_BTYPE
;
1574 bt2
= t2
& VT_BTYPE
;
1576 if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
1577 /* at least one operand is a pointer */
1578 /* relationnal op: must be both pointers */
1579 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
1580 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1581 /* pointers are handled are unsigned */
1582 #ifdef TCC_TARGET_X86_64
1583 t
= VT_LLONG
| VT_UNSIGNED
;
1585 t
= VT_INT
| VT_UNSIGNED
;
1589 /* if both pointers, then it must be the '-' op */
1590 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
1592 tcc_error("cannot use pointers here");
1593 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1594 /* XXX: check that types are compatible */
1595 if (vtop
[-1].type
.t
& VT_VLA
) {
1596 vla_runtime_pointed_size(&vtop
[-1].type
);
1598 vpushi(pointed_size(&vtop
[-1].type
));
1602 /* set to integer type */
1603 #ifdef TCC_TARGET_X86_64
1604 vtop
->type
.t
= VT_LLONG
;
1606 vtop
->type
.t
= VT_INT
;
1611 /* exactly one pointer : must be '+' or '-'. */
1612 if (op
!= '-' && op
!= '+')
1613 tcc_error("cannot use pointers here");
1614 /* Put pointer as first operand */
1615 if (bt2
== VT_PTR
) {
1619 type1
= vtop
[-1].type
;
1620 type1
.t
&= ~VT_ARRAY
;
1621 if (vtop
[-1].type
.t
& VT_VLA
)
1622 vla_runtime_pointed_size(&vtop
[-1].type
);
1624 u
= pointed_size(&vtop
[-1].type
);
1626 tcc_error("unknown array element size");
1627 #ifdef TCC_TARGET_X86_64
1630 /* XXX: cast to int ? (long long case) */
1635 #ifdef CONFIG_TCC_BCHECK
1636 /* if evaluating constant expression, no code should be
1637 generated, so no bound check */
1638 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
1639 /* if bounded pointers, we generate a special code to
1646 gen_bounded_ptr_add();
1652 /* put again type if gen_opic() swaped operands */
1655 } else if (is_float(bt1
) || is_float(bt2
)) {
1656 /* compute bigger type and do implicit casts */
1657 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
1659 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
1664 /* floats can only be used for a few operations */
1665 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
1666 (op
< TOK_ULT
|| op
> TOK_GT
))
1667 tcc_error("invalid operands for binary operation");
1669 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
1670 /* cast to biggest op */
1672 /* convert to unsigned if it does not fit in a long long */
1673 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
1674 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
1678 /* integer operations */
1680 /* convert to unsigned if it does not fit in an integer */
1681 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
1682 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
1685 /* XXX: currently, some unsigned operations are explicit, so
1686 we modify them here */
1687 if (t
& VT_UNSIGNED
) {
1694 else if (op
== TOK_LT
)
1696 else if (op
== TOK_GT
)
1698 else if (op
== TOK_LE
)
1700 else if (op
== TOK_GE
)
1707 /* special case for shifts and long long: we keep the shift as
1709 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
1716 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
1717 /* relationnal op: the result is an int */
1718 vtop
->type
.t
= VT_INT
;
1725 #ifndef TCC_TARGET_ARM
1726 /* generic itof for unsigned long long case */
1727 static void gen_cvt_itof1(int t
)
1729 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
1730 (VT_LLONG
| VT_UNSIGNED
)) {
1733 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
1734 #if LDOUBLE_SIZE != 8
1735 else if (t
== VT_LDOUBLE
)
1736 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
1739 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
1743 vtop
->r
= reg_fret(t
);
1750 /* generic ftoi for unsigned long long case */
1751 static void gen_cvt_ftoi1(int t
)
1755 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
1756 /* not handled natively */
1757 st
= vtop
->type
.t
& VT_BTYPE
;
1759 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
1760 #if LDOUBLE_SIZE != 8
1761 else if (st
== VT_LDOUBLE
)
1762 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
1765 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
1770 vtop
->r2
= REG_LRET
;
1776 /* force char or short cast */
1777 static void force_charshort_cast(int t
)
1781 /* XXX: add optimization if lvalue : just change type and offset */
1786 if (t
& VT_UNSIGNED
) {
1787 vpushi((1 << bits
) - 1);
1793 /* result must be signed or the SAR is converted to an SHL
1794 This was not the case when "t" was a signed short
1795 and the last value on the stack was an unsigned int */
1796 vtop
->type
.t
&= ~VT_UNSIGNED
;
1802 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1803 static void gen_cast(CType
*type
)
1805 int sbt
, dbt
, sf
, df
, c
, p
;
1807 /* special delayed cast for char/short */
1808 /* XXX: in some cases (multiple cascaded casts), it may still
1810 if (vtop
->r
& VT_MUSTCAST
) {
1811 vtop
->r
&= ~VT_MUSTCAST
;
1812 force_charshort_cast(vtop
->type
.t
);
1815 /* bitfields first get cast to ints */
1816 if (vtop
->type
.t
& VT_BITFIELD
) {
1820 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
1821 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
1826 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1827 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
1829 /* constant case: we can do it now */
1830 /* XXX: in ISOC, cannot do it if error in convert */
1831 if (sbt
== VT_FLOAT
)
1832 vtop
->c
.ld
= vtop
->c
.f
;
1833 else if (sbt
== VT_DOUBLE
)
1834 vtop
->c
.ld
= vtop
->c
.d
;
1837 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
1838 if (sbt
& VT_UNSIGNED
)
1839 vtop
->c
.ld
= vtop
->c
.ull
;
1841 vtop
->c
.ld
= vtop
->c
.ll
;
1843 if (sbt
& VT_UNSIGNED
)
1844 vtop
->c
.ld
= vtop
->c
.ui
;
1846 vtop
->c
.ld
= vtop
->c
.i
;
1849 if (dbt
== VT_FLOAT
)
1850 vtop
->c
.f
= (float)vtop
->c
.ld
;
1851 else if (dbt
== VT_DOUBLE
)
1852 vtop
->c
.d
= (double)vtop
->c
.ld
;
1853 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
1854 vtop
->c
.ull
= (unsigned long long)vtop
->c
.ld
;
1855 } else if (sf
&& dbt
== VT_BOOL
) {
1856 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
1859 vtop
->c
.ll
= (long long)vtop
->c
.ld
;
1860 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
1861 vtop
->c
.ll
= vtop
->c
.ull
;
1862 else if (sbt
& VT_UNSIGNED
)
1863 vtop
->c
.ll
= vtop
->c
.ui
;
1864 #ifdef TCC_TARGET_X86_64
1865 else if (sbt
== VT_PTR
)
1868 else if (sbt
!= VT_LLONG
)
1869 vtop
->c
.ll
= vtop
->c
.i
;
1871 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
1872 vtop
->c
.ull
= vtop
->c
.ll
;
1873 else if (dbt
== VT_BOOL
)
1874 vtop
->c
.i
= (vtop
->c
.ll
!= 0);
1875 else if (dbt
!= VT_LLONG
) {
1877 if ((dbt
& VT_BTYPE
) == VT_BYTE
)
1879 else if ((dbt
& VT_BTYPE
) == VT_SHORT
)
1882 if(dbt
& VT_UNSIGNED
)
1883 vtop
->c
.ui
= ((unsigned int)vtop
->c
.ll
<< s
) >> s
;
1885 vtop
->c
.i
= ((int)vtop
->c
.ll
<< s
) >> s
;
1888 } else if (p
&& dbt
== VT_BOOL
) {
1891 } else if (!nocode_wanted
) {
1892 /* non constant case: generate code */
1894 /* convert from fp to fp */
1897 /* convert int to fp */
1900 /* convert fp to int */
1901 if (dbt
== VT_BOOL
) {
1905 /* we handle char/short/etc... with generic code */
1906 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
1907 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
1911 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
1912 /* additional cast for char/short... */
1917 #ifndef TCC_TARGET_X86_64
1918 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
1919 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
1920 /* scalar to long long */
1921 /* machine independent conversion */
1923 /* generate high word */
1924 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
1928 if (sbt
== VT_PTR
) {
1929 /* cast from pointer to int before we apply
1930 shift operation, which pointers don't support*/
1931 gen_cast(&int_type
);
1937 /* patch second register */
1938 vtop
[-1].r2
= vtop
->r
;
1942 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
1943 (dbt
& VT_BTYPE
) == VT_PTR
||
1944 (dbt
& VT_BTYPE
) == VT_FUNC
) {
1945 if ((sbt
& VT_BTYPE
) != VT_LLONG
&&
1946 (sbt
& VT_BTYPE
) != VT_PTR
&&
1947 (sbt
& VT_BTYPE
) != VT_FUNC
) {
1948 /* need to convert from 32bit to 64bit */
1950 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
1951 /* x86_64 specific: movslq */
1953 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
1957 } else if (dbt
== VT_BOOL
) {
1958 /* scalar to bool */
1961 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
1962 (dbt
& VT_BTYPE
) == VT_SHORT
) {
1963 if (sbt
== VT_PTR
) {
1964 vtop
->type
.t
= VT_INT
;
1965 tcc_warning("nonportable conversion from pointer to char/short");
1967 force_charshort_cast(dbt
);
1968 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
1970 if (sbt
== VT_LLONG
) {
1971 /* from long long: just take low order word */
1975 /* if lvalue and single word type, nothing to do because
1976 the lvalue already contains the real type size (see
1977 VT_LVAL_xxx constants) */
1980 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
1981 /* if we are casting between pointer types,
1982 we must update the VT_LVAL_xxx size */
1983 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
1984 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
1989 /* return type size as known at compile time. Put alignment at 'a' */
1990 ST_FUNC
int type_size(CType
*type
, int *a
)
1995 bt
= type
->t
& VT_BTYPE
;
1996 if (bt
== VT_STRUCT
) {
2001 } else if (bt
== VT_PTR
) {
2002 if (type
->t
& VT_ARRAY
) {
2006 ts
= type_size(&s
->type
, a
);
2008 if (ts
< 0 && s
->c
< 0)
2016 } else if (bt
== VT_LDOUBLE
) {
2018 return LDOUBLE_SIZE
;
2019 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
2020 #ifdef TCC_TARGET_I386
2021 #ifdef TCC_TARGET_PE
2026 #elif defined(TCC_TARGET_ARM)
2036 } else if (bt
== VT_INT
|| bt
== VT_ENUM
|| bt
== VT_FLOAT
) {
2039 } else if (bt
== VT_SHORT
) {
2043 /* char, void, function, _Bool */
2049 /* push type size as known at runtime time on top of value stack. Put
2051 ST_FUNC
void vla_runtime_type_size(CType
*type
, int *a
)
2053 if (type
->t
& VT_VLA
) {
2054 vset(&int_type
, VT_LOCAL
|VT_LVAL
, type
->ref
->c
);
2056 vpushi(type_size(type
, a
));
2060 /* return the pointed type of t */
2061 static inline CType
*pointed_type(CType
*type
)
2063 return &type
->ref
->type
;
2066 /* modify type so that its it is a pointer to type. */
2067 ST_FUNC
void mk_pointer(CType
*type
)
2070 s
= sym_push(SYM_FIELD
, type
, 0, -1);
2071 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
2075 /* compare function types. OLD functions match any new functions */
2076 static int is_compatible_func(CType
*type1
, CType
*type2
)
2082 if (!is_compatible_types(&s1
->type
, &s2
->type
))
2084 /* check func_call */
2085 if (FUNC_CALL(s1
->r
) != FUNC_CALL(s2
->r
))
2087 /* XXX: not complete */
2088 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
2092 while (s1
!= NULL
) {
2095 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
2105 /* return true if type1 and type2 are the same. If unqualified is
2106 true, qualifiers on the types are ignored.
2108 - enums are not checked as gcc __builtin_types_compatible_p ()
2110 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2114 t1
= type1
->t
& VT_TYPE
;
2115 t2
= type2
->t
& VT_TYPE
;
2117 /* strip qualifiers before comparing */
2118 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2119 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2121 /* XXX: bitfields ? */
2124 /* test more complicated cases */
2125 bt1
= t1
& VT_BTYPE
;
2126 if (bt1
== VT_PTR
) {
2127 type1
= pointed_type(type1
);
2128 type2
= pointed_type(type2
);
2129 return is_compatible_types(type1
, type2
);
2130 } else if (bt1
== VT_STRUCT
) {
2131 return (type1
->ref
== type2
->ref
);
2132 } else if (bt1
== VT_FUNC
) {
2133 return is_compatible_func(type1
, type2
);
2139 /* return true if type1 and type2 are exactly the same (including
2142 static int is_compatible_types(CType
*type1
, CType
*type2
)
2144 return compare_types(type1
,type2
,0);
2147 /* return true if type1 and type2 are the same (ignoring qualifiers).
2149 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
2151 return compare_types(type1
,type2
,1);
2154 /* print a type. If 'varstr' is not NULL, then the variable is also
2155 printed in the type */
2157 /* XXX: add array and function pointers */
2158 static void type_to_str(char *buf
, int buf_size
,
2159 CType
*type
, const char *varstr
)
2166 t
= type
->t
& VT_TYPE
;
2169 if (t
& VT_CONSTANT
)
2170 pstrcat(buf
, buf_size
, "const ");
2171 if (t
& VT_VOLATILE
)
2172 pstrcat(buf
, buf_size
, "volatile ");
2173 if (t
& VT_UNSIGNED
)
2174 pstrcat(buf
, buf_size
, "unsigned ");
2204 tstr
= "long double";
2206 pstrcat(buf
, buf_size
, tstr
);
2210 if (bt
== VT_STRUCT
)
2214 pstrcat(buf
, buf_size
, tstr
);
2215 v
= type
->ref
->v
& ~SYM_STRUCT
;
2216 if (v
>= SYM_FIRST_ANOM
)
2217 pstrcat(buf
, buf_size
, "<anonymous>");
2219 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
2223 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
2224 pstrcat(buf
, buf_size
, "(");
2226 while (sa
!= NULL
) {
2227 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
2228 pstrcat(buf
, buf_size
, buf1
);
2231 pstrcat(buf
, buf_size
, ", ");
2233 pstrcat(buf
, buf_size
, ")");
2237 pstrcpy(buf1
, sizeof(buf1
), "*");
2239 pstrcat(buf1
, sizeof(buf1
), varstr
);
2240 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2244 pstrcat(buf
, buf_size
, " ");
2245 pstrcat(buf
, buf_size
, varstr
);
2250 /* verify type compatibility to store vtop in 'dt' type, and generate
2252 static void gen_assign_cast(CType
*dt
)
2254 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
2255 char buf1
[256], buf2
[256];
2258 st
= &vtop
->type
; /* source type */
2259 dbt
= dt
->t
& VT_BTYPE
;
2260 sbt
= st
->t
& VT_BTYPE
;
2261 if (dt
->t
& VT_CONSTANT
)
2262 tcc_warning("assignment of read-only location");
2265 /* special cases for pointers */
2266 /* '0' can also be a pointer */
2267 if (is_null_pointer(vtop
))
2269 /* accept implicit pointer to integer cast with warning */
2270 if (is_integer_btype(sbt
)) {
2271 tcc_warning("assignment makes pointer from integer without a cast");
2274 type1
= pointed_type(dt
);
2275 /* a function is implicitely a function pointer */
2276 if (sbt
== VT_FUNC
) {
2277 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
2278 !is_compatible_types(pointed_type(dt
), st
))
2279 tcc_warning("assignment from incompatible pointer type");
2284 type2
= pointed_type(st
);
2285 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2286 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
2287 /* void * can match anything */
2289 /* exact type match, except for unsigned */
2292 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2293 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2294 if (!is_compatible_types(&tmp_type1
, &tmp_type2
))
2295 tcc_warning("assignment from incompatible pointer type");
2297 /* check const and volatile */
2298 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
2299 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
2300 tcc_warning("assignment discards qualifiers from pointer target type");
2306 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
2307 tcc_warning("assignment makes integer from pointer without a cast");
2309 /* XXX: more tests */
2314 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2315 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2316 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2318 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
2319 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
2320 tcc_error("cannot cast '%s' to '%s'", buf1
, buf2
);
2328 /* store vtop in lvalue pushed on stack */
2329 ST_FUNC
void vstore(void)
2331 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
2333 ft
= vtop
[-1].type
.t
;
2334 sbt
= vtop
->type
.t
& VT_BTYPE
;
2335 dbt
= ft
& VT_BTYPE
;
2336 if (((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2337 (sbt
== VT_INT
&& dbt
== VT_SHORT
)) {
2338 /* optimize char/short casts */
2339 delayed_cast
= VT_MUSTCAST
;
2340 vtop
->type
.t
= ft
& (VT_TYPE
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
)));
2341 /* XXX: factorize */
2342 if (ft
& VT_CONSTANT
)
2343 tcc_warning("assignment of read-only location");
2346 if (!(ft
& VT_BITFIELD
))
2347 gen_assign_cast(&vtop
[-1].type
);
2350 if (sbt
== VT_STRUCT
) {
2351 /* if structure, only generate pointer */
2352 /* structure assignment : generate memcpy */
2353 /* XXX: optimize if small size */
2354 if (!nocode_wanted
) {
2355 size
= type_size(&vtop
->type
, &align
);
2359 vtop
->type
.t
= VT_PTR
;
2362 /* address of memcpy() */
2365 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2366 else if(!(align
& 3))
2367 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2370 vpush_global_sym(&func_old_type
, TOK_memcpy
);
2375 vtop
->type
.t
= VT_PTR
;
2384 /* leave source on stack */
2385 } else if (ft
& VT_BITFIELD
) {
2386 /* bitfield store handling */
2387 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2388 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2389 /* remove bit field info to avoid loops */
2390 vtop
[-1].type
.t
= ft
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
2392 /* duplicate source into other register */
2397 if((ft
& VT_BTYPE
) == VT_BOOL
) {
2398 gen_cast(&vtop
[-1].type
);
2399 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2402 /* duplicate destination */
2404 vtop
[-1] = vtop
[-2];
2406 /* mask and shift source */
2407 if((ft
& VT_BTYPE
) != VT_BOOL
) {
2408 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2409 vpushll((1ULL << bit_size
) - 1ULL);
2411 vpushi((1 << bit_size
) - 1);
2417 /* load destination, mask and or with source */
2419 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2420 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2422 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2429 /* pop off shifted source from "duplicate source..." above */
2433 #ifdef CONFIG_TCC_BCHECK
2434 /* bound check case */
2435 if (vtop
[-1].r
& VT_MUSTBOUND
) {
2441 if (!nocode_wanted
) {
2445 #ifdef TCC_TARGET_X86_64
2446 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
2451 r
= gv(rc
); /* generate value */
2452 /* if lvalue was saved on stack, must read it */
2453 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
2455 t
= get_reg(RC_INT
);
2456 #ifdef TCC_TARGET_X86_64
2461 sv
.r
= VT_LOCAL
| VT_LVAL
;
2462 sv
.c
.ul
= vtop
[-1].c
.ul
;
2464 vtop
[-1].r
= t
| VT_LVAL
;
2467 #ifndef TCC_TARGET_X86_64
2468 /* two word case handling : store second register at word + 4 */
2469 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
2471 /* convert to int to increment easily */
2472 vtop
->type
.t
= VT_INT
;
2478 /* XXX: it works because r2 is spilled last ! */
2479 store(vtop
->r2
, vtop
- 1);
2484 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
2485 vtop
->r
|= delayed_cast
;
2489 /* post defines POST/PRE add. c is the token ++ or -- */
2490 ST_FUNC
void inc(int post
, int c
)
2493 vdup(); /* save lvalue */
2495 gv_dup(); /* duplicate value */
2500 vpushi(c
- TOK_MID
);
2502 vstore(); /* store value */
2504 vpop(); /* if post op, return saved value */
2507 /* Parse GNUC __attribute__ extension. Currently, the following
2508 extensions are recognized:
2509 - aligned(n) : set data/function alignment.
2510 - packed : force data alignment to 1
2511 - section(x) : generate data/code in this section.
2512 - unused : currently ignored, but may be used someday.
2513 - regparm(n) : pass function parameters in registers (i386 only)
2515 static void parse_attribute(AttributeDef
*ad
)
2519 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
2523 while (tok
!= ')') {
2524 if (tok
< TOK_IDENT
)
2525 expect("attribute name");
2533 expect("section name");
2534 ad
->section
= find_section(tcc_state
, (char *)tokc
.cstr
->data
);
2542 expect("alias(\"target\")");
2543 ad
->alias_target
= /* save string as token, for later */
2544 tok_alloc((char*)tokc
.cstr
->data
, tokc
.cstr
->size
-1)->tok
;
2553 if (n
<= 0 || (n
& (n
- 1)) != 0)
2554 tcc_error("alignment must be a positive power of two");
2571 /* currently, no need to handle it because tcc does not
2572 track unused objects */
2576 /* currently, no need to handle it because tcc does not
2577 track unused objects */
2582 ad
->func_call
= FUNC_CDECL
;
2587 ad
->func_call
= FUNC_STDCALL
;
2589 #ifdef TCC_TARGET_I386
2599 ad
->func_call
= FUNC_FASTCALL1
+ n
- 1;
2605 ad
->func_call
= FUNC_FASTCALLW
;
2612 ad
->mode
= VT_LLONG
+ 1;
2615 ad
->mode
= VT_SHORT
+ 1;
2618 ad
->mode
= VT_INT
+ 1;
2621 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
2628 ad
->func_export
= 1;
2631 ad
->func_import
= 1;
2634 if (tcc_state
->warn_unsupported
)
2635 tcc_warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
2636 /* skip parameters */
2638 int parenthesis
= 0;
2642 else if (tok
== ')')
2645 } while (parenthesis
&& tok
!= -1);
2658 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2659 static void struct_decl(CType
*type
, int u
)
2661 int a
, v
, size
, align
, maxalign
, c
, offset
;
2662 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
, prevbt
;
2663 Sym
*s
, *ss
, *ass
, **ps
;
2667 a
= tok
; /* save decl type */
2672 /* struct already defined ? return it */
2674 expect("struct/union/enum name");
2678 tcc_error("invalid type");
2685 /* we put an undefined size for struct/union */
2686 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
2687 s
->r
= 0; /* default alignment is zero as gcc */
2688 /* put struct/union/enum name in type */
2696 tcc_error("struct/union/enum already defined");
2697 /* cannot be empty */
2699 /* non empty enums are not allowed */
2700 if (a
== TOK_ENUM
) {
2704 expect("identifier");
2710 /* enum symbols have static storage */
2711 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
2712 ss
->type
.t
|= VT_STATIC
;
2717 /* NOTE: we accept a trailing comma */
2728 while (tok
!= '}') {
2729 parse_btype(&btype
, &ad
);
2735 type_decl(&type1
, &ad
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
2736 if (v
== 0 && (type1
.t
& VT_BTYPE
) != VT_STRUCT
)
2737 expect("identifier");
2738 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
2739 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
2740 tcc_error("invalid type for '%s'",
2741 get_tok_str(v
, NULL
));
2745 bit_size
= expr_const();
2746 /* XXX: handle v = 0 case for messages */
2748 tcc_error("negative width in bit-field '%s'",
2749 get_tok_str(v
, NULL
));
2750 if (v
&& bit_size
== 0)
2751 tcc_error("zero width for bit-field '%s'",
2752 get_tok_str(v
, NULL
));
2754 size
= type_size(&type1
, &align
);
2756 if (align
< ad
.aligned
)
2758 } else if (ad
.packed
) {
2760 } else if (*tcc_state
->pack_stack_ptr
) {
2761 if (align
> *tcc_state
->pack_stack_ptr
)
2762 align
= *tcc_state
->pack_stack_ptr
;
2765 if (bit_size
>= 0) {
2766 bt
= type1
.t
& VT_BTYPE
;
2773 tcc_error("bitfields must have scalar type");
2775 if (bit_size
> bsize
) {
2776 tcc_error("width of '%s' exceeds its type",
2777 get_tok_str(v
, NULL
));
2778 } else if (bit_size
== bsize
) {
2779 /* no need for bit fields */
2781 } else if (bit_size
== 0) {
2782 /* XXX: what to do if only padding in a
2784 /* zero size: means to pad */
2787 /* we do not have enough room ?
2788 did the type change?
2790 if ((bit_pos
+ bit_size
) > bsize
||
2791 bt
!= prevbt
|| a
== TOK_UNION
)
2794 /* XXX: handle LSB first */
2795 type1
.t
|= VT_BITFIELD
|
2796 (bit_pos
<< VT_STRUCT_SHIFT
) |
2797 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
2798 bit_pos
+= bit_size
;
2804 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2805 /* add new memory data only if starting
2807 if (lbit_pos
== 0) {
2808 if (a
== TOK_STRUCT
) {
2809 c
= (c
+ align
- 1) & -align
;
2818 if (align
> maxalign
)
2822 printf("add field %s offset=%d",
2823 get_tok_str(v
, NULL
), offset
);
2824 if (type1
.t
& VT_BITFIELD
) {
2825 printf(" pos=%d size=%d",
2826 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
2827 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
2832 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2834 while ((ass
= ass
->next
) != NULL
) {
2835 ss
= sym_push(ass
->v
, &ass
->type
, 0, offset
+ ass
->c
);
2840 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
2844 if (tok
== ';' || tok
== TOK_EOF
)
2851 /* store size and alignment */
2852 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
2858 /* return 0 if no type declaration. otherwise, return the basic type
2861 static int parse_btype(CType
*type
, AttributeDef
*ad
)
2863 int t
, u
, type_found
, typespec_found
, typedef_found
;
2867 memset(ad
, 0, sizeof(AttributeDef
));
2875 /* currently, we really ignore extension */
2885 if ((t
& VT_BTYPE
) != 0)
2886 tcc_error("too many basic types");
2902 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
2903 #ifndef TCC_TARGET_PE
2904 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
2906 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
2907 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
2921 if ((t
& VT_BTYPE
) == VT_LONG
) {
2922 #ifdef TCC_TARGET_PE
2923 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
2925 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
2933 struct_decl(&type1
, VT_ENUM
);
2936 type
->ref
= type1
.ref
;
2940 struct_decl(&type1
, VT_STRUCT
);
2943 /* type modifiers */
2996 /* GNUC attribute */
2997 case TOK_ATTRIBUTE1
:
2998 case TOK_ATTRIBUTE2
:
2999 parse_attribute(ad
);
3002 t
= (t
& ~VT_BTYPE
) | u
;
3010 parse_expr_type(&type1
);
3011 /* remove all storage modifiers except typedef */
3012 type1
.t
&= ~(VT_STORAGE
&~VT_TYPEDEF
);
3015 if (typespec_found
|| typedef_found
)
3018 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
3021 t
|= (s
->type
.t
& ~VT_TYPEDEF
);
3022 type
->ref
= s
->type
.ref
;
3024 /* get attributes from typedef */
3025 if (0 == ad
->aligned
)
3026 ad
->aligned
= FUNC_ALIGN(s
->r
);
3027 if (0 == ad
->func_call
)
3028 ad
->func_call
= FUNC_CALL(s
->r
);
3029 ad
->packed
|= FUNC_PACKED(s
->r
);
3038 if ((t
& (VT_SIGNED
|VT_UNSIGNED
)) == (VT_SIGNED
|VT_UNSIGNED
))
3039 tcc_error("signed and unsigned modifier");
3040 if (tcc_state
->char_is_unsigned
) {
3041 if ((t
& (VT_SIGNED
|VT_UNSIGNED
|VT_BTYPE
)) == VT_BYTE
)
3046 /* long is never used as type */
3047 if ((t
& VT_BTYPE
) == VT_LONG
)
3048 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
3049 t
= (t
& ~VT_BTYPE
) | VT_INT
;
3051 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3057 /* convert a function parameter type (array to pointer and function to
3058 function pointer) */
3059 static inline void convert_parameter_type(CType
*pt
)
3061 /* remove const and volatile qualifiers (XXX: const could be used
3062 to indicate a const function parameter */
3063 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3064 /* array must be transformed to pointer according to ANSI C */
3066 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
3071 ST_FUNC
void parse_asm_str(CString
*astr
)
3074 /* read the string */
3076 expect("string constant");
3078 while (tok
== TOK_STR
) {
3079 /* XXX: add \0 handling too ? */
3080 cstr_cat(astr
, tokc
.cstr
->data
);
3083 cstr_ccat(astr
, '\0');
3086 /* Parse an asm label and return the label
3087 * Don't forget to free the CString in the caller! */
3088 static void asm_label_instr(CString
*astr
)
3091 parse_asm_str(astr
);
3094 printf("asm_alias: \"%s\"\n", (char *)astr
->data
);
3098 static void post_type(CType
*type
, AttributeDef
*ad
)
3100 int n
, l
, t1
, arg_size
, align
;
3101 Sym
**plast
, *s
, *first
;
3106 /* function declaration */
3114 /* read param name and compute offset */
3115 if (l
!= FUNC_OLD
) {
3116 if (!parse_btype(&pt
, &ad1
)) {
3118 tcc_error("invalid type");
3125 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
3127 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3128 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
3129 tcc_error("parameter declared as void");
3130 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
3135 expect("identifier");
3139 convert_parameter_type(&pt
);
3140 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
3146 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
3153 /* if no parameters, then old type prototype */
3157 /* NOTE: const is ignored in returned type as it has a special
3158 meaning in gcc / C++ */
3159 type
->t
&= ~VT_CONSTANT
;
3160 /* some ancient pre-K&R C allows a function to return an array
3161 and the array brackets to be put after the arguments, such
3162 that "int c()[]" means something like "int[] c()" */
3165 skip(']'); /* only handle simple "[]" */
3168 /* we push a anonymous symbol which will contain the function prototype */
3169 ad
->func_args
= arg_size
;
3170 s
= sym_push(SYM_FIELD
, type
, INT_ATTR(ad
), l
);
3174 } else if (tok
== '[') {
3175 /* array definition */
3177 if (tok
== TOK_RESTRICT1
)
3182 if (!local_stack
|| nocode_wanted
)
3183 vpushi(expr_const());
3185 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3188 tcc_error("invalid array size");
3190 if (!is_integer_btype(vtop
->type
.t
& VT_BTYPE
))
3191 tcc_error("size of variable length array should be an integer");
3196 /* parse next post type */
3197 post_type(type
, ad
);
3198 t1
|= type
->t
& VT_VLA
;
3201 loc
-= type_size(&int_type
, &align
);
3205 vla_runtime_type_size(type
, &align
);
3207 vset(&int_type
, VT_LOCAL
|VT_LVAL
, loc
);
3214 /* we push an anonymous symbol which will contain the array
3216 s
= sym_push(SYM_FIELD
, type
, 0, n
);
3217 type
->t
= (t1
? VT_VLA
: VT_ARRAY
) | VT_PTR
;
3222 /* Parse a type declaration (except basic type), and return the type
3223 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3224 expected. 'type' should contain the basic type. 'ad' is the
3225 attribute definition of the basic type. It can be modified by
3228 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
3231 CType type1
, *type2
;
3232 int qualifiers
, storage
;
3234 while (tok
== '*') {
3242 qualifiers
|= VT_CONSTANT
;
3247 qualifiers
|= VT_VOLATILE
;
3255 type
->t
|= qualifiers
;
3258 /* XXX: clarify attribute handling */
3259 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3260 parse_attribute(ad
);
3262 /* recursive type */
3263 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3264 type1
.t
= 0; /* XXX: same as int */
3267 /* XXX: this is not correct to modify 'ad' at this point, but
3268 the syntax is not clear */
3269 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3270 parse_attribute(ad
);
3271 type_decl(&type1
, ad
, v
, td
);
3274 /* type identifier */
3275 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
3279 if (!(td
& TYPE_ABSTRACT
))
3280 expect("identifier");
3284 storage
= type
->t
& VT_STORAGE
;
3285 type
->t
&= ~VT_STORAGE
;
3286 post_type(type
, ad
);
3288 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3289 parse_attribute(ad
);
3293 /* append type at the end of type1 */
3306 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3307 ST_FUNC
int lvalue_type(int t
)
3312 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
3314 else if (bt
== VT_SHORT
)
3318 if (t
& VT_UNSIGNED
)
3319 r
|= VT_LVAL_UNSIGNED
;
3323 /* indirection with full error checking and bound check */
3324 ST_FUNC
void indir(void)
3326 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
3327 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
3331 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
3333 vtop
->type
= *pointed_type(&vtop
->type
);
3334 /* Arrays and functions are never lvalues */
3335 if (!(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_VLA
)
3336 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3337 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3338 /* if bound checking, the referenced pointer must be checked */
3339 #ifdef CONFIG_TCC_BCHECK
3340 if (tcc_state
->do_bounds_check
)
3341 vtop
->r
|= VT_MUSTBOUND
;
3346 /* pass a parameter to a function and do type checking and casting */
3347 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
3352 func_type
= func
->c
;
3353 if (func_type
== FUNC_OLD
||
3354 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
3355 /* default casting : only need to convert float to double */
3356 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
3360 } else if (arg
== NULL
) {
3361 tcc_error("too many arguments to function");
3364 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
3365 gen_assign_cast(&type
);
3369 /* parse an expression of the form '(type)' or '(expr)' and return its
3371 static void parse_expr_type(CType
*type
)
3377 if (parse_btype(type
, &ad
)) {
3378 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3385 static void parse_type(CType
*type
)
3390 if (!parse_btype(type
, &ad
)) {
3393 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3396 static void vpush_tokc(int t
)
3401 vsetc(&type
, VT_CONST
, &tokc
);
3404 ST_FUNC
void unary(void)
3406 int n
, t
, align
, size
, r
, sizeof_caller
;
3410 static int in_sizeof
= 0;
3412 sizeof_caller
= in_sizeof
;
3414 /* XXX: GCC 2.95.3 does not generate a table although it should be
3428 vpush_tokc(VT_INT
| VT_UNSIGNED
);
3432 vpush_tokc(VT_LLONG
);
3436 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
3440 vpush_tokc(VT_FLOAT
);
3444 vpush_tokc(VT_DOUBLE
);
3448 vpush_tokc(VT_LDOUBLE
);
3451 case TOK___FUNCTION__
:
3453 goto tok_identifier
;
3459 /* special function name identifier */
3460 len
= strlen(funcname
) + 1;
3461 /* generate char[len] type */
3466 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
3467 ptr
= section_ptr_add(data_section
, len
);
3468 memcpy(ptr
, funcname
, len
);
3473 #ifdef TCC_TARGET_PE
3474 t
= VT_SHORT
| VT_UNSIGNED
;
3480 /* string parsing */
3483 if (tcc_state
->warn_write_strings
)
3488 memset(&ad
, 0, sizeof(AttributeDef
));
3489 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, NULL
, 0);
3494 if (parse_btype(&type
, &ad
)) {
3495 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
3497 /* check ISOC99 compound literal */
3499 /* data is allocated locally by default */
3504 /* all except arrays are lvalues */
3505 if (!(type
.t
& VT_ARRAY
))
3506 r
|= lvalue_type(type
.t
);
3507 memset(&ad
, 0, sizeof(AttributeDef
));
3508 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, NULL
, 0);
3510 if (sizeof_caller
) {
3517 } else if (tok
== '{') {
3518 /* save all registers */
3520 /* statement expression : we do not accept break/continue
3521 inside as GCC does */
3522 block(NULL
, NULL
, NULL
, NULL
, 0, 1);
3537 /* functions names must be treated as function pointers,
3538 except for unary '&' and sizeof. Since we consider that
3539 functions are not lvalues, we only have to handle it
3540 there and in function calls. */
3541 /* arrays can also be used although they are not lvalues */
3542 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
3543 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
3545 mk_pointer(&vtop
->type
);
3551 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3553 boolean
.t
= VT_BOOL
;
3555 vtop
->c
.i
= !vtop
->c
.i
;
3556 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
3557 vtop
->c
.i
= vtop
->c
.i
^ 1;
3560 vseti(VT_JMP
, gtst(1, 0));
3571 /* in order to force cast, we add zero */
3573 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
3574 tcc_error("pointer not accepted for unary plus");
3584 unary_type(&type
); // Perform a in_sizeof = 0;
3585 size
= type_size(&type
, &align
);
3586 if (t
== TOK_SIZEOF
) {
3587 if (!(type
.t
& VT_VLA
)) {
3589 tcc_error("sizeof applied to an incomplete type");
3592 vla_runtime_type_size(&type
, &align
);
3597 vtop
->type
.t
|= VT_UNSIGNED
;
3600 case TOK_builtin_types_compatible_p
:
3609 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3610 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3611 vpushi(is_compatible_types(&type1
, &type2
));
3614 case TOK_builtin_constant_p
:
3616 int saved_nocode_wanted
, res
;
3619 saved_nocode_wanted
= nocode_wanted
;
3622 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
3624 nocode_wanted
= saved_nocode_wanted
;
3629 case TOK_builtin_frame_address
:
3634 if (tok
!= TOK_CINT
) {
3635 tcc_error("__builtin_frame_address only takes integers");
3638 tcc_error("TCC only supports __builtin_frame_address(0)");
3644 vset(&type
, VT_LOCAL
, 0);
3647 #ifdef TCC_TARGET_X86_64
3648 case TOK_builtin_va_arg_types
:
3650 /* This definition must be synced with stdarg.h */
3651 enum __va_arg_type
{
3652 __va_gen_reg
, __va_float_reg
, __va_stack
3660 bt
= type
.t
& VT_BTYPE
;
3661 if (bt
== VT_STRUCT
|| bt
== VT_LDOUBLE
) {
3663 } else if (bt
== VT_FLOAT
|| bt
== VT_DOUBLE
) {
3664 vpushi(__va_float_reg
);
3666 vpushi(__va_gen_reg
);
3686 goto tok_identifier
;
3688 /* allow to take the address of a label */
3689 if (tok
< TOK_UIDENT
)
3690 expect("label identifier");
3691 s
= label_find(tok
);
3693 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
3695 if (s
->r
== LABEL_DECLARED
)
3696 s
->r
= LABEL_FORWARD
;
3699 s
->type
.t
= VT_VOID
;
3700 mk_pointer(&s
->type
);
3701 s
->type
.t
|= VT_STATIC
;
3703 vset(&s
->type
, VT_CONST
| VT_SYM
, 0);
3708 // special qnan , snan and infinity values
3710 vpush64(VT_DOUBLE
, 0x7ff8000000000000ULL
);
3714 vpush64(VT_DOUBLE
, 0x7ff0000000000001ULL
);
3718 vpush64(VT_DOUBLE
, 0x7ff0000000000000ULL
);
3727 expect("identifier");
3731 tcc_error("'%s' undeclared", get_tok_str(t
, NULL
));
3732 /* for simple function calls, we tolerate undeclared
3733 external reference to int() function */
3734 if (tcc_state
->warn_implicit_function_declaration
)
3735 tcc_warning("implicit declaration of function '%s'",
3736 get_tok_str(t
, NULL
));
3737 s
= external_global_sym(t
, &func_old_type
, 0);
3739 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
3740 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
3741 /* if referencing an inline function, then we generate a
3742 symbol to it if not already done. It will have the
3743 effect to generate code for it at the end of the
3744 compilation unit. Inline function as always
3745 generated in the text section. */
3747 put_extern_sym(s
, text_section
, 0, 0);
3748 r
= VT_SYM
| VT_CONST
;
3752 vset(&s
->type
, r
, s
->c
);
3753 /* if forward reference, we must point to s */
3754 if (vtop
->r
& VT_SYM
) {
3761 /* post operations */
3763 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
3766 } else if (tok
== '.' || tok
== TOK_ARROW
) {
3769 if (tok
== TOK_ARROW
)
3771 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
3775 /* expect pointer on structure */
3776 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
3777 expect("struct or union");
3781 while ((s
= s
->next
) != NULL
) {
3786 tcc_error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, NULL
));
3787 /* add field offset to pointer */
3788 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
3791 /* change type to field type, and set to lvalue */
3792 vtop
->type
= s
->type
;
3793 vtop
->type
.t
|= qualifiers
;
3794 /* an array is never an lvalue */
3795 if (!(vtop
->type
.t
& VT_ARRAY
)) {
3796 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3797 #ifdef CONFIG_TCC_BCHECK
3798 /* if bound checking, the referenced pointer must be checked */
3799 if (tcc_state
->do_bounds_check
)
3800 vtop
->r
|= VT_MUSTBOUND
;
3804 } else if (tok
== '[') {
3810 } else if (tok
== '(') {
3816 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3817 /* pointer test (no array accepted) */
3818 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
3819 vtop
->type
= *pointed_type(&vtop
->type
);
3820 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
3824 expect("function pointer");
3827 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
3829 /* get return type */
3832 sa
= s
->next
; /* first parameter */
3835 /* compute first implicit argument if a structure is returned */
3836 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
3837 /* get some space for the returned structure */
3838 size
= type_size(&s
->type
, &align
);
3839 loc
= (loc
- size
) & -align
;
3841 ret
.r
= VT_LOCAL
| VT_LVAL
;
3842 /* pass it as 'int' to avoid structure arg passing
3844 vseti(VT_LOCAL
, loc
);
3849 /* return in register */
3850 if (is_float(ret
.type
.t
)) {
3851 ret
.r
= reg_fret(ret
.type
.t
);
3853 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
3862 gfunc_param_typed(s
, sa
);
3872 tcc_error("too few arguments to function");
3874 if (!nocode_wanted
) {
3875 gfunc_call(nb_args
);
3877 vtop
-= (nb_args
+ 1);
3880 vsetc(&ret
.type
, ret
.r
, &ret
.c
);
3888 ST_FUNC
void expr_prod(void)
3893 while (tok
== '*' || tok
== '/' || tok
== '%') {
3901 ST_FUNC
void expr_sum(void)
3906 while (tok
== '+' || tok
== '-') {
3914 static void expr_shift(void)
3919 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
3927 static void expr_cmp(void)
3932 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
3933 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
3941 static void expr_cmpeq(void)
3946 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
3954 static void expr_and(void)
3957 while (tok
== '&') {
3964 static void expr_xor(void)
3967 while (tok
== '^') {
3974 static void expr_or(void)
3977 while (tok
== '|') {
3984 /* XXX: fix this mess */
3985 static void expr_land_const(void)
3988 while (tok
== TOK_LAND
) {
3995 /* XXX: fix this mess */
3996 static void expr_lor_const(void)
3999 while (tok
== TOK_LOR
) {
4006 /* only used if non constant */
4007 static void expr_land(void)
4012 if (tok
== TOK_LAND
) {
4017 if (tok
!= TOK_LAND
) {
4027 static void expr_lor(void)
4032 if (tok
== TOK_LOR
) {
4037 if (tok
!= TOK_LOR
) {
4047 /* XXX: better constant handling */
4048 static void expr_cond(void)
4050 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
;
4052 CType type
, type1
, type2
;
4059 boolean
.t
= VT_BOOL
;
4065 if (tok
!= ':' || !gnu_ext
) {
4080 if (vtop
!= vstack
) {
4081 /* needed to avoid having different registers saved in
4083 if (is_float(vtop
->type
.t
)) {
4085 #ifdef TCC_TARGET_X86_64
4086 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4096 if (tok
== ':' && gnu_ext
) {
4104 sv
= *vtop
; /* save value to handle it later */
4105 vtop
--; /* no vpop so that FP stack is not flushed */
4113 bt1
= t1
& VT_BTYPE
;
4115 bt2
= t2
& VT_BTYPE
;
4116 /* cast operands to correct type according to ISOC rules */
4117 if (is_float(bt1
) || is_float(bt2
)) {
4118 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
4119 type
.t
= VT_LDOUBLE
;
4120 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
4125 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
4126 /* cast to biggest op */
4128 /* convert to unsigned if it does not fit in a long long */
4129 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
4130 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
4131 type
.t
|= VT_UNSIGNED
;
4132 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
4133 /* XXX: test pointer compatibility */
4135 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
4136 /* XXX: test function pointer compatibility */
4138 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
4139 /* XXX: test structure compatibility */
4141 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
4142 /* NOTE: as an extension, we accept void on only one side */
4145 /* integer operations */
4147 /* convert to unsigned if it does not fit in an integer */
4148 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
4149 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
4150 type
.t
|= VT_UNSIGNED
;
4153 /* now we convert second operand */
4155 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4158 if (is_float(type
.t
)) {
4160 #ifdef TCC_TARGET_X86_64
4161 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4165 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
4166 /* for long longs, we use fixed registers to avoid having
4167 to handle a complicated move */
4172 /* this is horrible, but we must also convert first
4176 /* put again first value and cast it */
4179 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4189 static void expr_eq(void)
4195 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
4196 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
4197 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
4212 ST_FUNC
void gexpr(void)
4223 /* parse an expression and return its type without any side effect. */
4224 static void expr_type(CType
*type
)
4226 int saved_nocode_wanted
;
4228 saved_nocode_wanted
= nocode_wanted
;
4233 nocode_wanted
= saved_nocode_wanted
;
4236 /* parse a unary expression and return its type without any side
4238 static void unary_type(CType
*type
)
4250 /* parse a constant expression and return value in vtop. */
4251 static void expr_const1(void)
4260 /* parse an integer constant and return its value. */
4261 ST_FUNC
int expr_const(void)
4265 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
4266 expect("constant expression");
4272 /* return the label token if current token is a label, otherwise
4274 static int is_label(void)
4278 /* fast test first */
4279 if (tok
< TOK_UIDENT
)
4281 /* no need to save tokc because tok is an identifier */
4288 unget_tok(last_tok
);
4293 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
4294 int case_reg
, int is_expr
)
4299 /* generate line number info */
4300 if (tcc_state
->do_debug
&&
4301 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
4302 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
4304 last_line_num
= file
->line_num
;
4308 /* default return value is (void) */
4310 vtop
->type
.t
= VT_VOID
;
4313 if (tok
== TOK_IF
) {
4320 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4322 if (c
== TOK_ELSE
) {
4326 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4327 gsym(d
); /* patch else jmp */
4330 } else if (tok
== TOK_WHILE
) {
4338 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4342 } else if (tok
== '{') {
4346 /* record local declaration stack position */
4348 llabel
= local_label_stack
;
4349 /* handle local labels declarations */
4350 if (tok
== TOK_LABEL
) {
4353 if (tok
< TOK_UIDENT
)
4354 expect("label identifier");
4355 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
4365 while (tok
!= '}') {
4370 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4373 /* pop locally defined labels */
4374 label_pop(&local_label_stack
, llabel
);
4376 /* XXX: this solution makes only valgrind happy...
4377 triggered by gcc.c-torture/execute/20000917-1.c */
4379 switch(vtop
->type
.t
& VT_BTYPE
) {
4384 for(p
=vtop
->type
.ref
;p
;p
=p
->prev
)
4386 tcc_error("unsupported expression type");
4389 /* pop locally defined symbols */
4390 sym_pop(&local_stack
, s
);
4392 } else if (tok
== TOK_RETURN
) {
4396 gen_assign_cast(&func_vt
);
4397 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
4399 /* if returning structure, must copy it to implicit
4400 first pointer arg location */
4403 size
= type_size(&func_vt
,&align
);
4406 if((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) || (vtop
->c
.i
& 3))
4410 loc
= (loc
- size
) & -4;
4413 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
4416 vset(&int_type
, VT_LOCAL
| VT_LVAL
, addr
);
4418 vtop
->type
= int_type
;
4424 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
4427 /* copy structure value to pointer */
4432 } else if (is_float(func_vt
.t
)) {
4433 gv(rc_fret(func_vt
.t
));
4437 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
4440 rsym
= gjmp(rsym
); /* jmp */
4441 } else if (tok
== TOK_BREAK
) {
4444 tcc_error("cannot break");
4445 *bsym
= gjmp(*bsym
);
4448 } else if (tok
== TOK_CONTINUE
) {
4451 tcc_error("cannot continue");
4452 *csym
= gjmp(*csym
);
4455 } else if (tok
== TOK_FOR
) {
4461 /* c99 for-loop init decl? */
4462 if (!decl0(VT_LOCAL
, 1)) {
4463 /* no, regular for-loop init expr */
4487 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4491 sym_pop(&local_stack
, s
);
4493 if (tok
== TOK_DO
) {
4498 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4509 if (tok
== TOK_SWITCH
) {
4513 /* XXX: other types than integer */
4514 case_reg
= gv(RC_INT
);
4518 b
= gjmp(0); /* jump to first case */
4520 block(&a
, csym
, &b
, &c
, case_reg
, 0);
4521 /* if no default, jmp after switch */
4529 if (tok
== TOK_CASE
) {
4536 if (gnu_ext
&& tok
== TOK_DOTS
) {
4540 tcc_warning("empty case range");
4542 /* since a case is like a label, we must skip it with a jmp */
4549 *case_sym
= gtst(1, 0);
4552 *case_sym
= gtst(1, 0);
4556 *case_sym
= gtst(1, *case_sym
);
4561 goto block_after_label
;
4563 if (tok
== TOK_DEFAULT
) {
4569 tcc_error("too many 'default'");
4572 goto block_after_label
;
4574 if (tok
== TOK_GOTO
) {
4576 if (tok
== '*' && gnu_ext
) {
4580 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
4583 } else if (tok
>= TOK_UIDENT
) {
4584 s
= label_find(tok
);
4585 /* put forward definition if needed */
4587 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4589 if (s
->r
== LABEL_DECLARED
)
4590 s
->r
= LABEL_FORWARD
;
4592 /* label already defined */
4593 if (s
->r
& LABEL_FORWARD
)
4594 s
->jnext
= gjmp(s
->jnext
);
4596 gjmp_addr(s
->jnext
);
4599 expect("label identifier");
4602 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
4610 if (s
->r
== LABEL_DEFINED
)
4611 tcc_error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
4613 s
->r
= LABEL_DEFINED
;
4615 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
4618 /* we accept this, but it is a mistake */
4621 tcc_warning("deprecated use of label at end of compound statement");
4625 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4628 /* expression case */
4643 /* t is the array or struct type. c is the array or struct
4644 address. cur_index/cur_field is the pointer to the current
4645 value. 'size_only' is true if only size info is needed (only used
4647 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
4648 int *cur_index
, Sym
**cur_field
,
4652 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
4658 if (gnu_ext
&& (l
= is_label()) != 0)
4660 while (tok
== '[' || tok
== '.') {
4662 if (!(type
->t
& VT_ARRAY
))
4663 expect("array type");
4666 index
= expr_const();
4667 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
4668 expect("invalid index");
4669 if (tok
== TOK_DOTS
&& gnu_ext
) {
4671 index_last
= expr_const();
4672 if (index_last
< 0 ||
4673 (s
->c
>= 0 && index_last
>= s
->c
) ||
4675 expect("invalid index");
4681 *cur_index
= index_last
;
4682 type
= pointed_type(type
);
4683 elem_size
= type_size(type
, &align
);
4684 c
+= index
* elem_size
;
4685 /* NOTE: we only support ranges for last designator */
4686 nb_elems
= index_last
- index
+ 1;
4687 if (nb_elems
!= 1) {
4696 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
4697 expect("struct/union type");
4710 /* XXX: fix this mess by using explicit storage field */
4712 type1
.t
|= (type
->t
& ~VT_TYPE
);
4726 if (type
->t
& VT_ARRAY
) {
4728 type
= pointed_type(type
);
4729 c
+= index
* type_size(type
, &align
);
4733 tcc_error("too many field init");
4734 /* XXX: fix this mess by using explicit storage field */
4736 type1
.t
|= (type
->t
& ~VT_TYPE
);
4741 decl_initializer(type
, sec
, c
, 0, size_only
);
4743 /* XXX: make it more general */
4744 if (!size_only
&& nb_elems
> 1) {
4745 unsigned long c_end
;
4750 tcc_error("range init not supported yet for dynamic storage");
4751 c_end
= c
+ nb_elems
* elem_size
;
4752 if (c_end
> sec
->data_allocated
)
4753 section_realloc(sec
, c_end
);
4754 src
= sec
->data
+ c
;
4756 for(i
= 1; i
< nb_elems
; i
++) {
4758 memcpy(dst
, src
, elem_size
);
4764 #define EXPR_CONST 1
4767 /* store a value or an expression directly in global data or in local array */
4768 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
4769 int v
, int expr_type
)
4771 int saved_global_expr
, bt
, bit_pos
, bit_size
;
4773 unsigned long long bit_mask
;
4781 /* compound literals must be allocated globally in this case */
4782 saved_global_expr
= global_expr
;
4785 global_expr
= saved_global_expr
;
4786 /* NOTE: symbols are accepted */
4787 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
4788 tcc_error("initializer element is not constant");
4796 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
4799 /* XXX: not portable */
4800 /* XXX: generate error if incorrect relocation */
4801 gen_assign_cast(&dtype
);
4802 bt
= type
->t
& VT_BTYPE
;
4803 /* we'll write at most 12 bytes */
4804 if (c
+ 12 > sec
->data_allocated
) {
4805 section_realloc(sec
, c
+ 12);
4807 ptr
= sec
->data
+ c
;
4808 /* XXX: make code faster ? */
4809 if (!(type
->t
& VT_BITFIELD
)) {
4814 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4815 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
4816 bit_mask
= (1LL << bit_size
) - 1;
4818 if ((vtop
->r
& VT_SYM
) &&
4824 (bt
== VT_INT
&& bit_size
!= 32)))
4825 tcc_error("initializer element is not computable at load time");
4828 vtop
->c
.i
= (vtop
->c
.i
!= 0);
4830 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4833 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4836 *(double *)ptr
= vtop
->c
.d
;
4839 *(long double *)ptr
= vtop
->c
.ld
;
4842 *(long long *)ptr
|= (vtop
->c
.ll
& bit_mask
) << bit_pos
;
4845 if (vtop
->r
& VT_SYM
) {
4846 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
4848 *(int *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
4853 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
4860 /* put zeros for variable based init */
4861 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
4864 /* nothing to do because globals are already set to zero */
4866 vpush_global_sym(&func_old_type
, TOK_memset
);
4874 /* 't' contains the type and storage info. 'c' is the offset of the
4875 object in section 'sec'. If 'sec' is NULL, it means stack based
4876 allocation. 'first' is true if array '{' must be read (multi
4877 dimension implicit array init handling). 'size_only' is true if
4878 size only evaluation is wanted (only for arrays). */
4879 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
4880 int first
, int size_only
)
4882 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, parlevel1
, i
;
4883 int size1
, align1
, expr_type
;
4887 if (type
->t
& VT_VLA
) {
4888 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
4892 vpush_global_sym(&func_old_type
, TOK_alloca
);
4893 vla_runtime_type_size(type
, &a
);
4898 vsetc(type
, REG_IRET
, &retcval
);
4899 vset(type
, VT_LOCAL
|VT_LVAL
, c
);
4904 tcc_error("variable length arrays unsupported for this target");
4906 } else if (type
->t
& VT_ARRAY
) {
4910 t1
= pointed_type(type
);
4911 size1
= type_size(t1
, &align1
);
4914 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
4917 tcc_error("character array initializer must be a literal,"
4918 " optionally enclosed in braces");
4923 /* only parse strings here if correct type (otherwise: handle
4924 them as ((w)char *) expressions */
4925 if ((tok
== TOK_LSTR
&&
4926 #ifdef TCC_TARGET_PE
4927 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
4929 (t1
->t
& VT_BTYPE
) == VT_INT
4931 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
4932 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
4937 /* compute maximum number of chars wanted */
4939 cstr_len
= cstr
->size
;
4941 cstr_len
= cstr
->size
/ sizeof(nwchar_t
);
4944 if (n
>= 0 && nb
> (n
- array_length
))
4945 nb
= n
- array_length
;
4948 tcc_warning("initializer-string for array is too long");
4949 /* in order to go faster for common case (char
4950 string in global variable, we handle it
4952 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
4953 memcpy(sec
->data
+ c
+ array_length
, cstr
->data
, nb
);
4957 ch
= ((unsigned char *)cstr
->data
)[i
];
4959 ch
= ((nwchar_t
*)cstr
->data
)[i
];
4960 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
4968 /* only add trailing zero if enough storage (no
4969 warning in this case since it is standard) */
4970 if (n
< 0 || array_length
< n
) {
4972 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
4978 while (tok
!= '}') {
4979 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
4980 if (n
>= 0 && index
>= n
)
4981 tcc_error("index too large");
4982 /* must put zero in holes (note that doing it that way
4983 ensures that it even works with designators) */
4984 if (!size_only
&& array_length
< index
) {
4985 init_putz(t1
, sec
, c
+ array_length
* size1
,
4986 (index
- array_length
) * size1
);
4989 if (index
> array_length
)
4990 array_length
= index
;
4991 /* special test for multi dimensional arrays (may not
4992 be strictly correct if designators are used at the
4994 if (index
>= n
&& no_oblock
)
5003 /* put zeros at the end */
5004 if (!size_only
&& n
>= 0 && array_length
< n
) {
5005 init_putz(t1
, sec
, c
+ array_length
* size1
,
5006 (n
- array_length
) * size1
);
5008 /* patch type size if needed */
5010 s
->c
= array_length
;
5011 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
5012 (sec
|| !first
|| tok
== '{')) {
5015 /* NOTE: the previous test is a specific case for automatic
5016 struct/union init */
5017 /* XXX: union needs only one init */
5019 /* XXX: this test is incorrect for local initializers
5020 beginning with ( without {. It would be much more difficult
5021 to do it correctly (ideally, the expression parser should
5022 be used in all cases) */
5028 while (tok
== '(') {
5032 if (!parse_btype(&type1
, &ad1
))
5034 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
5036 if (!is_assignable_types(type
, &type1
))
5037 tcc_error("invalid type for cast");
5042 if (first
|| tok
== '{') {
5051 while (tok
!= '}') {
5052 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
5054 if (!size_only
&& array_length
< index
) {
5055 init_putz(type
, sec
, c
+ array_length
,
5056 index
- array_length
);
5058 index
= index
+ type_size(&f
->type
, &align1
);
5059 if (index
> array_length
)
5060 array_length
= index
;
5062 /* gr: skip fields from same union - ugly. */
5064 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
5065 /* test for same offset */
5066 if (f
->next
->c
!= f
->c
)
5068 /* if yes, test for bitfield shift */
5069 if ((f
->type
.t
& VT_BITFIELD
) && (f
->next
->type
.t
& VT_BITFIELD
)) {
5070 int bit_pos_1
= (f
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5071 int bit_pos_2
= (f
->next
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5072 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
5073 if (bit_pos_1
!= bit_pos_2
)
5080 if (no_oblock
&& f
== NULL
)
5086 /* put zeros at the end */
5087 if (!size_only
&& array_length
< n
) {
5088 init_putz(type
, sec
, c
+ array_length
,
5097 } else if (tok
== '{') {
5099 decl_initializer(type
, sec
, c
, first
, size_only
);
5101 } else if (size_only
) {
5102 /* just skip expression */
5103 parlevel
= parlevel1
= 0;
5104 while ((parlevel
> 0 || parlevel1
> 0 ||
5105 (tok
!= '}' && tok
!= ',')) && tok
!= -1) {
5108 else if (tok
== ')')
5110 else if (tok
== '{')
5112 else if (tok
== '}')
5117 /* currently, we always use constant expression for globals
5118 (may change for scripting case) */
5119 expr_type
= EXPR_CONST
;
5121 expr_type
= EXPR_ANY
;
5122 init_putv(type
, sec
, c
, 0, expr_type
);
5126 /* parse an initializer for type 't' if 'has_init' is non zero, and
5127 allocate space in local or global data space ('r' is either
5128 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5129 variable 'v' with an associated name represented by 'asm_label' of
5130 scope 'scope' is declared before initializers are parsed. If 'v' is
5131 zero, then a reference to the new object is put in the value stack.
5132 If 'has_init' is 2, a special parsing is done to handle string
5134 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
5135 int has_init
, int v
, char *asm_label
,
5138 int size
, align
, addr
, data_offset
;
5140 ParseState saved_parse_state
= {0};
5141 TokenString init_str
;
5143 Sym
*flexible_array
;
5145 flexible_array
= NULL
;
5146 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
5149 while (field
&& field
->next
)
5150 field
= field
->next
;
5151 if (field
->type
.t
& VT_ARRAY
&& field
->type
.ref
->c
< 0)
5152 flexible_array
= field
;
5155 size
= type_size(type
, &align
);
5156 /* If unknown size, we must evaluate it before
5157 evaluating initializers because
5158 initializers can generate global data too
5159 (e.g. string pointers or ISOC99 compound
5160 literals). It also simplifies local
5161 initializers handling */
5162 tok_str_new(&init_str
);
5163 if (size
< 0 || (flexible_array
&& has_init
)) {
5165 tcc_error("unknown type size");
5166 /* get all init string */
5167 if (has_init
== 2) {
5168 /* only get strings */
5169 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5170 tok_str_add_tok(&init_str
);
5175 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
5177 tcc_error("unexpected end of file in initializer");
5178 tok_str_add_tok(&init_str
);
5181 else if (tok
== '}') {
5191 tok_str_add(&init_str
, -1);
5192 tok_str_add(&init_str
, 0);
5195 save_parse_state(&saved_parse_state
);
5197 macro_ptr
= init_str
.str
;
5199 decl_initializer(type
, NULL
, 0, 1, 1);
5200 /* prepare second initializer parsing */
5201 macro_ptr
= init_str
.str
;
5204 /* if still unknown size, error */
5205 size
= type_size(type
, &align
);
5207 tcc_error("unknown type size");
5210 size
+= flexible_array
->type
.ref
->c
* pointed_size(&flexible_array
->type
);
5211 /* take into account specified alignment if bigger */
5213 if (ad
->aligned
> align
)
5214 align
= ad
->aligned
;
5215 } else if (ad
->packed
) {
5218 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
5220 #ifdef CONFIG_TCC_BCHECK
5221 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5225 loc
= (loc
- size
) & -align
;
5227 #ifdef CONFIG_TCC_BCHECK
5228 /* handles bounds */
5229 /* XXX: currently, since we do only one pass, we cannot track
5230 '&' operators, so we add only arrays */
5231 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5232 unsigned long *bounds_ptr
;
5233 /* add padding between regions */
5235 /* then add local bound info */
5236 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(unsigned long));
5237 bounds_ptr
[0] = addr
;
5238 bounds_ptr
[1] = size
;
5242 /* local variable */
5243 sym_push(v
, type
, r
, addr
);
5245 /* push local reference */
5246 vset(type
, r
, addr
);
5252 if (v
&& scope
== VT_CONST
) {
5253 /* see if the symbol was already defined */
5256 if (!is_compatible_types(&sym
->type
, type
))
5257 tcc_error("incompatible types for redefinition of '%s'",
5258 get_tok_str(v
, NULL
));
5259 if (sym
->type
.t
& VT_EXTERN
) {
5260 /* if the variable is extern, it was not allocated */
5261 sym
->type
.t
&= ~VT_EXTERN
;
5262 /* set array size if it was ommited in extern
5264 if ((sym
->type
.t
& VT_ARRAY
) &&
5265 sym
->type
.ref
->c
< 0 &&
5267 sym
->type
.ref
->c
= type
->ref
->c
;
5269 /* we accept several definitions of the same
5270 global variable. this is tricky, because we
5271 must play with the SHN_COMMON type of the symbol */
5272 /* XXX: should check if the variable was already
5273 initialized. It is incorrect to initialized it
5275 /* no init data, we won't add more to the symbol */
5282 /* allocate symbol in corresponding section */
5287 else if (tcc_state
->nocommon
)
5291 data_offset
= sec
->data_offset
;
5292 data_offset
= (data_offset
+ align
- 1) & -align
;
5294 /* very important to increment global pointer at this time
5295 because initializers themselves can create new initializers */
5296 data_offset
+= size
;
5297 #ifdef CONFIG_TCC_BCHECK
5298 /* add padding if bound check */
5299 if (tcc_state
->do_bounds_check
)
5302 sec
->data_offset
= data_offset
;
5303 /* allocate section space to put the data */
5304 if (sec
->sh_type
!= SHT_NOBITS
&&
5305 data_offset
> sec
->data_allocated
)
5306 section_realloc(sec
, data_offset
);
5307 /* align section if needed */
5308 if (align
> sec
->sh_addralign
)
5309 sec
->sh_addralign
= align
;
5311 addr
= 0; /* avoid warning */
5315 if (scope
!= VT_CONST
|| !sym
) {
5316 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
5317 sym
->asm_label
= asm_label
;
5319 /* update symbol definition */
5321 put_extern_sym(sym
, sec
, addr
, size
);
5324 /* put a common area */
5325 put_extern_sym(sym
, NULL
, align
, size
);
5326 /* XXX: find a nicer way */
5327 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
5328 esym
->st_shndx
= SHN_COMMON
;
5333 /* push global reference */
5334 sym
= get_sym_ref(type
, sec
, addr
, size
);
5336 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
5339 /* patch symbol weakness */
5340 if (type
->t
& VT_WEAK
)
5342 #ifdef CONFIG_TCC_BCHECK
5343 /* handles bounds now because the symbol must be defined
5344 before for the relocation */
5345 if (tcc_state
->do_bounds_check
) {
5346 unsigned long *bounds_ptr
;
5348 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
5349 /* then add global bound info */
5350 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(long));
5351 bounds_ptr
[0] = 0; /* relocated */
5352 bounds_ptr
[1] = size
;
5356 if (has_init
|| (type
->t
& VT_VLA
)) {
5357 decl_initializer(type
, sec
, addr
, 1, 0);
5358 /* restore parse state if needed */
5360 tok_str_free(init_str
.str
);
5361 restore_parse_state(&saved_parse_state
);
5363 /* patch flexible array member size back to -1, */
5364 /* for possible subsequent similar declarations */
5366 flexible_array
->type
.ref
->c
= -1;
5371 static void put_func_debug(Sym
*sym
)
5376 /* XXX: we put here a dummy type */
5377 snprintf(buf
, sizeof(buf
), "%s:%c1",
5378 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
5379 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
5380 cur_text_section
, sym
->c
);
5381 /* //gr gdb wants a line at the function */
5382 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
5387 /* parse an old style function declaration list */
5388 /* XXX: check multiple parameter */
5389 static void func_decl_list(Sym
*func_sym
)
5396 /* parse each declaration */
5397 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
&&
5398 tok
!= TOK_ASM1
&& tok
!= TOK_ASM2
&& tok
!= TOK_ASM3
) {
5399 if (!parse_btype(&btype
, &ad
))
5400 expect("declaration list");
5401 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5402 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5404 /* we accept no variable after */
5408 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5409 /* find parameter in function parameter list */
5412 if ((s
->v
& ~SYM_FIELD
) == v
)
5416 tcc_error("declaration for parameter '%s' but no such parameter",
5417 get_tok_str(v
, NULL
));
5419 /* check that no storage specifier except 'register' was given */
5420 if (type
.t
& VT_STORAGE
)
5421 tcc_error("storage class specified for '%s'", get_tok_str(v
, NULL
));
5422 convert_parameter_type(&type
);
5423 /* we can add the type (NOTE: it could be local to the function) */
5425 /* accept other parameters */
5436 /* parse a function defined by symbol 'sym' and generate its code in
5437 'cur_text_section' */
5438 static void gen_function(Sym
*sym
)
5440 int saved_nocode_wanted
= nocode_wanted
;
5442 ind
= cur_text_section
->data_offset
;
5443 /* NOTE: we patch the symbol size later */
5444 put_extern_sym(sym
, cur_text_section
, ind
, 0);
5445 funcname
= get_tok_str(sym
->v
, NULL
);
5447 /* put debug symbol */
5448 if (tcc_state
->do_debug
)
5449 put_func_debug(sym
);
5450 /* push a dummy symbol to enable local sym storage */
5451 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
5452 gfunc_prolog(&sym
->type
);
5454 block(NULL
, NULL
, NULL
, NULL
, 0, 0);
5457 cur_text_section
->data_offset
= ind
;
5458 label_pop(&global_label_stack
, NULL
);
5459 sym_pop(&local_stack
, NULL
); /* reset local stack */
5460 /* end of function */
5461 /* patch symbol size */
5462 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
5464 /* patch symbol weakness (this definition overrules any prototype) */
5465 if (sym
->type
.t
& VT_WEAK
)
5467 if (tcc_state
->do_debug
) {
5468 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
5470 /* It's better to crash than to generate wrong code */
5471 cur_text_section
= NULL
;
5472 funcname
= ""; /* for safety */
5473 func_vt
.t
= VT_VOID
; /* for safety */
5474 ind
= 0; /* for safety */
5475 nocode_wanted
= saved_nocode_wanted
;
5478 ST_FUNC
void gen_inline_functions(void)
5481 int *str
, inline_generated
, i
;
5482 struct InlineFunc
*fn
;
5484 /* iterate while inline function are referenced */
5486 inline_generated
= 0;
5487 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5488 fn
= tcc_state
->inline_fns
[i
];
5490 if (sym
&& sym
->c
) {
5491 /* the function was used: generate its code and
5492 convert it to a normal function */
5493 str
= fn
->token_str
;
5496 strcpy(file
->filename
, fn
->filename
);
5497 sym
->r
= VT_SYM
| VT_CONST
;
5498 sym
->type
.t
&= ~VT_INLINE
;
5502 cur_text_section
= text_section
;
5504 macro_ptr
= NULL
; /* fail safe */
5506 inline_generated
= 1;
5509 if (!inline_generated
)
5512 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5513 fn
= tcc_state
->inline_fns
[i
];
5514 str
= fn
->token_str
;
5517 dynarray_reset(&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
);
5520 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
5521 static int decl0(int l
, int is_for_loop_init
)
5529 if (!parse_btype(&btype
, &ad
)) {
5530 if (is_for_loop_init
)
5532 /* skip redundant ';' */
5533 /* XXX: find more elegant solution */
5538 if (l
== VT_CONST
&&
5539 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5540 /* global asm block */
5544 /* special test for old K&R protos without explicit int
5545 type. Only accepted when defining global data */
5546 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
5550 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5551 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5553 /* we accept no variable after */
5557 while (1) { /* iterate thru each declaration */
5558 char *asm_label
; // associated asm label
5560 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5564 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
5565 printf("type = '%s'\n", buf
);
5568 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5569 if ((type
.t
& VT_STATIC
) && (l
== VT_LOCAL
)) {
5570 tcc_error("function without file scope cannot be static");
5572 /* if old style function prototype, we accept a
5575 if (sym
->c
== FUNC_OLD
)
5576 func_decl_list(sym
);
5580 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5583 asm_label_instr(&astr
);
5584 asm_label
= tcc_strdup(astr
.data
);
5587 /* parse one last attribute list, after asm label */
5588 parse_attribute(&ad
);
5593 #ifdef TCC_TARGET_PE
5595 type
.t
|= VT_IMPORT
;
5597 type
.t
|= VT_EXPORT
;
5601 tcc_error("cannot use local functions");
5602 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
5603 expect("function definition");
5605 /* reject abstract declarators in function definition */
5607 while ((sym
= sym
->next
) != NULL
)
5608 if (!(sym
->v
& ~SYM_FIELD
))
5609 expect("identifier");
5611 /* XXX: cannot do better now: convert extern line to static inline */
5612 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
5613 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5617 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
5620 r
= sym
->type
.ref
->r
;
5621 /* use func_call from prototype if not defined */
5622 if (FUNC_CALL(r
) != FUNC_CDECL
5623 && FUNC_CALL(type
.ref
->r
) == FUNC_CDECL
)
5624 FUNC_CALL(type
.ref
->r
) = FUNC_CALL(r
);
5626 /* use export from prototype */
5628 FUNC_EXPORT(type
.ref
->r
) = 1;
5630 /* use static from prototype */
5631 if (sym
->type
.t
& VT_STATIC
)
5632 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5634 if (!is_compatible_types(&sym
->type
, &type
)) {
5636 tcc_error("incompatible types for redefinition of '%s'",
5637 get_tok_str(v
, NULL
));
5639 /* if symbol is already defined, then put complete type */
5642 /* put function symbol */
5643 sym
= global_identifier_push(v
, type
.t
, 0);
5644 sym
->type
.ref
= type
.ref
;
5647 /* static inline functions are just recorded as a kind
5648 of macro. Their code will be emitted at the end of
5649 the compilation unit only if they are used */
5650 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
5651 (VT_INLINE
| VT_STATIC
)) {
5652 TokenString func_str
;
5654 struct InlineFunc
*fn
;
5655 const char *filename
;
5657 tok_str_new(&func_str
);
5663 tcc_error("unexpected end of file");
5664 tok_str_add_tok(&func_str
);
5669 } else if (t
== '}') {
5671 if (block_level
== 0)
5675 tok_str_add(&func_str
, -1);
5676 tok_str_add(&func_str
, 0);
5677 filename
= file
? file
->filename
: "";
5678 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
5679 strcpy(fn
->filename
, filename
);
5681 fn
->token_str
= func_str
.str
;
5682 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
5685 /* compute text section */
5686 cur_text_section
= ad
.section
;
5687 if (!cur_text_section
)
5688 cur_text_section
= text_section
;
5689 sym
->r
= VT_SYM
| VT_CONST
;
5694 if (btype
.t
& VT_TYPEDEF
) {
5695 /* save typedefed type */
5696 /* XXX: test storage specifiers ? */
5697 sym
= sym_push(v
, &type
, INT_ATTR(&ad
), 0);
5698 sym
->type
.t
|= VT_TYPEDEF
;
5701 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5702 /* external function definition */
5703 /* specific case for func_call attribute */
5704 type
.ref
->r
= INT_ATTR(&ad
);
5705 } else if (!(type
.t
& VT_ARRAY
)) {
5706 /* not lvalue if array */
5707 r
|= lvalue_type(type
.t
);
5709 has_init
= (tok
== '=');
5710 if (has_init
&& (type
.t
& VT_VLA
))
5711 tcc_error("Variable length array cannot be initialized");
5712 if ((btype
.t
& VT_EXTERN
) || ((type
.t
& VT_BTYPE
) == VT_FUNC
) ||
5713 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
5714 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
5715 /* external variable or function */
5716 /* NOTE: as GCC, uninitialized global static
5717 arrays of null size are considered as
5719 sym
= external_sym(v
, &type
, r
, asm_label
);
5721 if (type
.t
& VT_WEAK
)
5724 if (ad
.alias_target
) {
5729 alias_target
= sym_find(ad
.alias_target
);
5730 if (!alias_target
|| !alias_target
->c
)
5731 tcc_error("unsupported forward __alias__ attribute");
5732 esym
= &((Elf32_Sym
*)symtab_section
->data
)[alias_target
->c
];
5733 tsec
.sh_num
= esym
->st_shndx
;
5734 put_extern_sym2(sym
, &tsec
, esym
->st_value
, esym
->st_size
, 0);
5737 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
5738 if (type
.t
& VT_STATIC
)
5744 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, asm_label
, l
);
5748 if (is_for_loop_init
)
5761 ST_FUNC
void decl(int l
)