2 * TCC - Tiny C Compiler
4 * Copyright (c) 2001-2004 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 /********************************************************/
24 /* global variables */
26 /* loc : local variable index
27 ind : output code index
29 anon_sym: anonymous symbol index
31 ST_DATA
int rsym
, anon_sym
, ind
, loc
;
33 ST_DATA Section
*text_section
, *data_section
, *bss_section
; /* predefined sections */
34 ST_DATA Section
*cur_text_section
; /* current section where function code is generated */
36 ST_DATA Section
*last_text_section
; /* to handle .previous asm directive */
38 #ifdef CONFIG_TCC_BCHECK
39 /* bound check related sections */
40 ST_DATA Section
*bounds_section
; /* contains global data bound description */
41 ST_DATA Section
*lbounds_section
; /* contains local data bound description */
44 ST_DATA Section
*symtab_section
, *strtab_section
;
46 ST_DATA Section
*stab_section
, *stabstr_section
;
47 ST_DATA Sym
*sym_free_first
;
48 ST_DATA
void **sym_pools
;
49 ST_DATA
int nb_sym_pools
;
51 ST_DATA Sym
*global_stack
;
52 ST_DATA Sym
*local_stack
;
53 ST_DATA Sym
*scope_stack_bottom
;
54 ST_DATA Sym
*define_stack
;
55 ST_DATA Sym
*global_label_stack
;
56 ST_DATA Sym
*local_label_stack
;
58 ST_DATA
int vla_sp_loc_tmp
; /* vla_sp_loc is set to this when the value won't be needed later */
59 ST_DATA
int vla_sp_root_loc
; /* vla_sp_loc for SP before any VLAs were pushed */
60 ST_DATA
int *vla_sp_loc
; /* Pointer to variable holding location to store stack pointer on the stack when modifying stack pointer */
61 ST_DATA
int vla_flags
; /* VLA_* flags */
63 ST_DATA SValue __vstack
[1+VSTACK_SIZE
], *vtop
;
65 ST_DATA
int const_wanted
; /* true if constant wanted */
66 ST_DATA
int nocode_wanted
; /* true if no code generation wanted for an expression */
67 ST_DATA
int global_expr
; /* true if compound literals must be allocated globally (used during initializers parsing */
68 ST_DATA CType func_vt
; /* current function return type (used by return instruction) */
69 ST_DATA
int func_var
; /* true if current function is variadic (used by return instruction) */
71 ST_DATA
int last_line_num
, last_ind
, func_ind
; /* debug last line number and pc */
72 ST_DATA
char *funcname
;
74 ST_DATA CType char_pointer_type
, func_old_type
, int_type
, size_type
;
76 /* ------------------------------------------------------------------------- */
77 static void gen_cast(CType
*type
);
78 static inline CType
*pointed_type(CType
*type
);
79 static int is_compatible_types(CType
*type1
, CType
*type2
);
80 static int parse_btype(CType
*type
, AttributeDef
*ad
);
81 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
);
82 static void parse_expr_type(CType
*type
);
83 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
, int first
, int size_only
);
84 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
, int case_reg
, int is_expr
);
85 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
, int has_init
, int v
, char *asm_label
, int scope
);
86 static int decl0(int l
, int is_for_loop_init
);
87 static void expr_eq(void);
88 static void unary_type(CType
*type
);
89 static void vla_runtime_type_size(CType
*type
, int *a
);
90 static void vla_sp_save(void);
91 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
);
92 static void expr_type(CType
*type
);
93 ST_FUNC
void vpush64(int ty
, unsigned long long v
);
94 ST_FUNC
void vpush(CType
*type
);
95 ST_FUNC
int gvtst(int inv
, int t
);
96 ST_FUNC
int is_btype_size(int bt
);
98 ST_INLN
int is_float(int t
)
102 return bt
== VT_LDOUBLE
|| bt
== VT_DOUBLE
|| bt
== VT_FLOAT
|| bt
== VT_QFLOAT
;
105 /* we use our own 'finite' function to avoid potential problems with
106 non standard math libs */
107 /* XXX: endianness dependent */
108 ST_FUNC
int ieee_finite(double d
)
111 memcpy(p
, &d
, sizeof(double));
112 return ((unsigned)((p
[1] | 0x800fffff) + 1)) >> 31;
115 ST_FUNC
void test_lvalue(void)
117 if (!(vtop
->r
& VT_LVAL
))
121 /* ------------------------------------------------------------------------- */
122 /* symbol allocator */
123 static Sym
*__sym_malloc(void)
125 Sym
*sym_pool
, *sym
, *last_sym
;
128 sym_pool
= tcc_malloc(SYM_POOL_NB
* sizeof(Sym
));
129 dynarray_add(&sym_pools
, &nb_sym_pools
, sym_pool
);
131 last_sym
= sym_free_first
;
133 for(i
= 0; i
< SYM_POOL_NB
; i
++) {
134 sym
->next
= last_sym
;
138 sym_free_first
= last_sym
;
142 static inline Sym
*sym_malloc(void)
145 sym
= sym_free_first
;
147 sym
= __sym_malloc();
148 sym_free_first
= sym
->next
;
152 ST_INLN
void sym_free(Sym
*sym
)
154 sym
->next
= sym_free_first
;
155 tcc_free(sym
->asm_label
);
156 sym_free_first
= sym
;
159 /* push, without hashing */
160 ST_FUNC Sym
*sym_push2(Sym
**ps
, int v
, int t
, long c
)
163 if (ps
== &local_stack
) {
164 for (s
= *ps
; s
&& s
!= scope_stack_bottom
; s
= s
->prev
)
165 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
&& s
->v
== v
)
166 tcc_error("incompatible types for redefinition of '%s'",
167 get_tok_str(v
, NULL
));
185 /* find a symbol and return its associated structure. 's' is the top
186 of the symbol stack */
187 ST_FUNC Sym
*sym_find2(Sym
*s
, int v
)
199 /* structure lookup */
200 ST_INLN Sym
*struct_find(int v
)
203 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
205 return table_ident
[v
]->sym_struct
;
208 /* find an identifier */
209 ST_INLN Sym
*sym_find(int v
)
212 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
214 return table_ident
[v
]->sym_identifier
;
217 /* push a given symbol on the symbol stack */
218 ST_FUNC Sym
*sym_push(int v
, CType
*type
, int r
, int c
)
227 s
= sym_push2(ps
, v
, type
->t
, c
);
228 s
->type
.ref
= type
->ref
;
230 /* don't record fields or anonymous symbols */
232 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
233 /* record symbol in token array */
234 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
236 ps
= &ts
->sym_struct
;
238 ps
= &ts
->sym_identifier
;
245 /* push a global identifier */
246 ST_FUNC Sym
*global_identifier_push(int v
, int t
, int c
)
249 s
= sym_push2(&global_stack
, v
, t
, c
);
250 /* don't record anonymous symbol */
251 if (v
< SYM_FIRST_ANOM
) {
252 ps
= &table_ident
[v
- TOK_IDENT
]->sym_identifier
;
253 /* modify the top most local identifier, so that
254 sym_identifier will point to 's' when popped */
256 ps
= &(*ps
)->prev_tok
;
263 /* pop symbols until top reaches 'b' */
264 ST_FUNC
void sym_pop(Sym
**ptop
, Sym
*b
)
274 /* remove symbol in token array */
276 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
277 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
279 ps
= &ts
->sym_struct
;
281 ps
= &ts
->sym_identifier
;
290 static void weaken_symbol(Sym
*sym
)
292 sym
->type
.t
|= VT_WEAK
;
297 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
298 esym_type
= ELFW(ST_TYPE
)(esym
->st_info
);
299 esym
->st_info
= ELFW(ST_INFO
)(STB_WEAK
, esym_type
);
303 /* ------------------------------------------------------------------------- */
305 ST_FUNC
void swap(int *p
, int *q
)
313 static void vsetc(CType
*type
, int r
, CValue
*vc
)
317 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
318 tcc_error("memory full (vstack)");
319 /* cannot let cpu flags if other instruction are generated. Also
320 avoid leaving VT_JMP anywhere except on the top of the stack
321 because it would complicate the code generator. */
322 if (vtop
>= vstack
) {
323 v
= vtop
->r
& VT_VALMASK
;
324 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
334 /* push constant of type "type" with useless value */
335 ST_FUNC
void vpush(CType
*type
)
338 vsetc(type
, VT_CONST
, &cval
);
341 /* push integer constant */
342 ST_FUNC
void vpushi(int v
)
346 vsetc(&int_type
, VT_CONST
, &cval
);
349 /* push a pointer sized constant */
350 static void vpushs(addr_t v
)
354 vsetc(&size_type
, VT_CONST
, &cval
);
357 /* push arbitrary 64bit constant */
358 ST_FUNC
void vpush64(int ty
, unsigned long long v
)
365 vsetc(&ctype
, VT_CONST
, &cval
);
368 /* push long long constant */
369 static inline void vpushll(long long v
)
371 vpush64(VT_LLONG
, v
);
374 /* push a symbol value of TYPE */
375 static inline void vpushsym(CType
*type
, Sym
*sym
)
379 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
383 /* Return a static symbol pointing to a section */
384 ST_FUNC Sym
*get_sym_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
390 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
391 sym
->type
.ref
= type
->ref
;
392 sym
->r
= VT_CONST
| VT_SYM
;
393 put_extern_sym(sym
, sec
, offset
, size
);
397 /* push a reference to a section offset by adding a dummy symbol */
398 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
400 vpushsym(type
, get_sym_ref(type
, sec
, offset
, size
));
403 /* define a new external reference to a symbol 'v' of type 'u' */
404 ST_FUNC Sym
*external_global_sym(int v
, CType
*type
, int r
)
410 /* push forward reference */
411 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
412 s
->type
.ref
= type
->ref
;
413 s
->r
= r
| VT_CONST
| VT_SYM
;
418 /* define a new external reference to a symbol 'v' with alternate asm
419 name 'asm_label' of type 'u'. 'asm_label' is equal to NULL if there
420 is no alternate name (most cases) */
421 static Sym
*external_sym(int v
, CType
*type
, int r
, char *asm_label
)
427 /* push forward reference */
428 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
429 s
->asm_label
= asm_label
;
430 s
->type
.t
|= VT_EXTERN
;
431 } else if (s
->type
.ref
== func_old_type
.ref
) {
432 s
->type
.ref
= type
->ref
;
433 s
->r
= r
| VT_CONST
| VT_SYM
;
434 s
->type
.t
|= VT_EXTERN
;
435 } else if (!is_compatible_types(&s
->type
, type
)) {
436 tcc_error("incompatible types for redefinition of '%s'",
437 get_tok_str(v
, NULL
));
442 /* push a reference to global symbol v */
443 ST_FUNC
void vpush_global_sym(CType
*type
, int v
)
445 vpushsym(type
, external_global_sym(v
, type
, 0));
448 ST_FUNC
void vset(CType
*type
, int r
, int v
)
453 vsetc(type
, r
, &cval
);
456 static void vseti(int r
, int v
)
464 ST_FUNC
void vswap(void)
467 /* cannot let cpu flags if other instruction are generated. Also
468 avoid leaving VT_JMP anywhere except on the top of the stack
469 because it would complicate the code generator. */
470 if (vtop
>= vstack
) {
471 int v
= vtop
->r
& VT_VALMASK
;
472 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
479 /* XXX: +2% overall speed possible with optimized memswap
481 * memswap(&vtop[0], &vtop[1], sizeof *vtop);
485 ST_FUNC
void vpushv(SValue
*v
)
487 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
488 tcc_error("memory full (vstack)");
493 static void vdup(void)
498 /* save r to the memory stack, and mark it as being free */
499 ST_FUNC
void save_reg(int r
)
501 int l
, saved
, size
, align
;
505 /* modify all stack values */
508 for(p
=vstack
;p
<=vtop
;p
++) {
509 if ((p
->r
& VT_VALMASK
) == r
||
510 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
511 /* must save value on stack if not already done */
513 /* NOTE: must reload 'r' because r might be equal to r2 */
514 r
= p
->r
& VT_VALMASK
;
515 /* store register in the stack */
517 if ((p
->r
& VT_LVAL
) ||
518 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
519 #ifdef TCC_TARGET_X86_64
520 type
= &char_pointer_type
;
524 size
= type_size(type
, &align
);
525 loc
= (loc
- size
) & -align
;
527 sv
.r
= VT_LOCAL
| VT_LVAL
;
530 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
531 /* x86 specific: need to pop fp register ST0 if saved */
533 o(0xd8dd); /* fstp %st(0) */
536 #ifndef TCC_TARGET_X86_64
537 /* special long long case */
538 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
546 /* mark that stack entry as being saved on the stack */
547 if (p
->r
& VT_LVAL
) {
548 /* also clear the bounded flag because the
549 relocation address of the function was stored in
551 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
553 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
561 #ifdef TCC_TARGET_ARM
562 /* find a register of class 'rc2' with at most one reference on stack.
563 * If none, call get_reg(rc) */
564 ST_FUNC
int get_reg_ex(int rc
, int rc2
)
569 for(r
=0;r
<NB_REGS
;r
++) {
570 if (reg_classes
[r
] & rc2
) {
573 for(p
= vstack
; p
<= vtop
; p
++) {
574 if ((p
->r
& VT_VALMASK
) == r
||
575 (p
->r2
& VT_VALMASK
) == r
)
586 /* find a free register of class 'rc'. If none, save one register */
587 ST_FUNC
int get_reg(int rc
)
592 /* find a free register */
593 for(r
=0;r
<NB_REGS
;r
++) {
594 if (reg_classes
[r
] & rc
) {
595 for(p
=vstack
;p
<=vtop
;p
++) {
596 if ((p
->r
& VT_VALMASK
) == r
||
597 (p
->r2
& VT_VALMASK
) == r
)
605 /* no register left : free the first one on the stack (VERY
606 IMPORTANT to start from the bottom to ensure that we don't
607 spill registers used in gen_opi()) */
608 for(p
=vstack
;p
<=vtop
;p
++) {
609 /* look at second register (if long long) */
610 r
= p
->r2
& VT_VALMASK
;
611 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
613 r
= p
->r
& VT_VALMASK
;
614 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
620 /* Should never comes here */
624 /* save registers up to (vtop - n) stack entry */
625 ST_FUNC
void save_regs(int n
)
630 for(p
= vstack
;p
<= p1
; p
++) {
631 r
= p
->r
& VT_VALMASK
;
638 /* move register 's' (of type 't') to 'r', and flush previous value of r to memory
640 static void move_reg(int r
, int s
, int t
)
654 /* get address of vtop (vtop MUST BE an lvalue) */
655 static void gaddrof(void)
657 if (vtop
->r
& VT_REF
)
660 /* tricky: if saved lvalue, then we can go back to lvalue */
661 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
662 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
667 #ifdef CONFIG_TCC_BCHECK
668 /* generate lvalue bound code */
669 static void gbound(void)
674 vtop
->r
&= ~VT_MUSTBOUND
;
675 /* if lvalue, then use checking code before dereferencing */
676 if (vtop
->r
& VT_LVAL
) {
677 /* if not VT_BOUNDED value, then make one */
678 if (!(vtop
->r
& VT_BOUNDED
)) {
679 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
680 /* must save type because we must set it to int to get pointer */
682 vtop
->type
.t
= VT_INT
;
685 gen_bounded_ptr_add();
686 vtop
->r
|= lval_type
;
689 /* then check for dereferencing */
690 gen_bounded_ptr_deref();
695 /* store vtop a register belonging to class 'rc'. lvalues are
696 converted to values. Cannot be used if cannot be converted to
697 register value (such as structures). */
698 ST_FUNC
int gv(int rc
)
700 int r
, bit_pos
, bit_size
, size
, align
, i
;
703 /* NOTE: get_reg can modify vstack[] */
704 if (vtop
->type
.t
& VT_BITFIELD
) {
707 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
708 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
709 /* remove bit field info to avoid loops */
710 vtop
->type
.t
&= ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
711 /* cast to int to propagate signedness in following ops */
712 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
717 if((vtop
->type
.t
& VT_UNSIGNED
) ||
718 (vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
719 type
.t
|= VT_UNSIGNED
;
721 /* generate shifts */
722 vpushi(bits
- (bit_pos
+ bit_size
));
724 vpushi(bits
- bit_size
);
725 /* NOTE: transformed to SHR if unsigned */
729 if (is_float(vtop
->type
.t
) &&
730 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
733 unsigned long offset
;
734 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
738 /* XXX: unify with initializers handling ? */
739 /* CPUs usually cannot use float constants, so we store them
740 generically in data segment */
741 size
= type_size(&vtop
->type
, &align
);
742 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
743 data_section
->data_offset
= offset
;
744 /* XXX: not portable yet */
745 #if defined(__i386__) || defined(__x86_64__)
746 /* Zero pad x87 tenbyte long doubles */
747 if (size
== LDOUBLE_SIZE
) {
748 vtop
->c
.tab
[2] &= 0xffff;
749 #if LDOUBLE_SIZE == 16
754 ptr
= section_ptr_add(data_section
, size
);
756 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
760 ptr
[i
] = vtop
->c
.tab
[size
-1-i
];
764 ptr
[i
] = vtop
->c
.tab
[i
];
765 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
766 vtop
->r
|= VT_LVAL
| VT_SYM
;
768 vtop
->c
.ptr_offset
= 0;
770 #ifdef CONFIG_TCC_BCHECK
771 if (vtop
->r
& VT_MUSTBOUND
)
775 r
= vtop
->r
& VT_VALMASK
;
776 rc2
= (rc
& RC_FLOAT
) ? RC_FLOAT
: RC_INT
;
779 #ifdef TCC_TARGET_X86_64
780 else if (rc
== RC_FRET
)
784 /* need to reload if:
786 - lvalue (need to dereference pointer)
787 - already a register, but not in the right class */
789 || (vtop
->r
& VT_LVAL
)
790 || !(reg_classes
[r
] & rc
)
791 #ifdef TCC_TARGET_X86_64
792 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
793 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
&& !(reg_classes
[vtop
->r2
] & rc2
))
795 || ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
800 #ifdef TCC_TARGET_X86_64
801 if (((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
)) {
802 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
804 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
805 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
806 unsigned long long ll
;
808 int r2
, original_type
;
809 original_type
= vtop
->type
.t
;
810 /* two register type load : expand to two words
812 #ifndef TCC_TARGET_X86_64
813 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
816 vtop
->c
.ui
= ll
; /* first word */
818 vtop
->r
= r
; /* save register value */
819 vpushi(ll
>> 32); /* second word */
822 if (r
>= VT_CONST
|| /* XXX: test to VT_CONST incorrect ? */
823 (vtop
->r
& VT_LVAL
)) {
824 /* We do not want to modifier the long long
825 pointer here, so the safest (and less
826 efficient) is to save all the other registers
827 in the stack. XXX: totally inefficient. */
829 /* load from memory */
830 vtop
->type
.t
= load_type
;
833 vtop
[-1].r
= r
; /* save register value */
834 /* increment pointer to get second word */
835 vtop
->type
.t
= addr_type
;
840 vtop
->type
.t
= load_type
;
845 vtop
[-1].r
= r
; /* save register value */
846 vtop
->r
= vtop
[-1].r2
;
848 /* Allocate second register. Here we rely on the fact that
849 get_reg() tries first to free r2 of an SValue. */
853 /* write second register */
855 vtop
->type
.t
= original_type
;
856 } else if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
858 /* lvalue of scalar type : need to use lvalue type
859 because of possible cast */
862 /* compute memory access type */
863 if (vtop
->r
& VT_REF
)
864 #ifdef TCC_TARGET_X86_64
869 else if (vtop
->r
& VT_LVAL_BYTE
)
871 else if (vtop
->r
& VT_LVAL_SHORT
)
873 if (vtop
->r
& VT_LVAL_UNSIGNED
)
877 /* restore wanted type */
880 /* one register type load */
885 #ifdef TCC_TARGET_C67
886 /* uses register pairs for doubles */
887 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
894 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
895 ST_FUNC
void gv2(int rc1
, int rc2
)
899 /* generate more generic register first. But VT_JMP or VT_CMP
900 values must be generated first in all cases to avoid possible
902 v
= vtop
[0].r
& VT_VALMASK
;
903 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
908 /* test if reload is needed for first register */
909 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
919 /* test if reload is needed for first register */
920 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
926 /* wrapper around RC_FRET to return a register by type */
927 static int rc_fret(int t
)
929 #ifdef TCC_TARGET_X86_64
930 if (t
== VT_LDOUBLE
) {
937 /* wrapper around REG_FRET to return a register by type */
938 static int reg_fret(int t
)
940 #ifdef TCC_TARGET_X86_64
941 if (t
== VT_LDOUBLE
) {
948 /* expand long long on stack in two int registers */
949 static void lexpand(void)
953 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
956 vtop
[0].r
= vtop
[-1].r2
;
957 vtop
[0].r2
= VT_CONST
;
958 vtop
[-1].r2
= VT_CONST
;
959 vtop
[0].type
.t
= VT_INT
| u
;
960 vtop
[-1].type
.t
= VT_INT
| u
;
963 #ifdef TCC_TARGET_ARM
964 /* expand long long on stack */
965 ST_FUNC
void lexpand_nr(void)
969 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
972 vtop
->type
.t
= VT_INT
| u
;
973 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
975 vtop
[-1].c
.ui
= vtop
->c
.ull
;
976 vtop
->c
.ui
= vtop
->c
.ull
>> 32;
978 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
980 vtop
->r
= vtop
[-1].r
;
981 } else if (v
> VT_CONST
) {
985 vtop
->r
= vtop
[-1].r2
;
986 vtop
[-1].r2
= VT_CONST
;
987 vtop
[-1].type
.t
= VT_INT
| u
;
991 /* build a long long from two ints */
992 static void lbuild(int t
)
995 vtop
[-1].r2
= vtop
[0].r
;
1000 /* rotate n first stack elements to the bottom
1001 I1 ... In -> I2 ... In I1 [top is right]
1003 ST_FUNC
void vrotb(int n
)
1009 for(i
=-n
+1;i
!=0;i
++)
1010 vtop
[i
] = vtop
[i
+1];
1014 /* rotate the n elements before entry e towards the top
1015 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
1017 ST_FUNC
void vrote(SValue
*e
, int n
)
1023 for(i
= 0;i
< n
- 1; i
++)
1028 /* rotate n first stack elements to the top
1029 I1 ... In -> In I1 ... I(n-1) [top is right]
1031 ST_FUNC
void vrott(int n
)
1036 /* pop stack value */
1037 ST_FUNC
void vpop(void)
1040 v
= vtop
->r
& VT_VALMASK
;
1041 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1042 /* for x86, we need to pop the FP stack */
1043 if (v
== TREG_ST0
&& !nocode_wanted
) {
1044 o(0xd8dd); /* fstp %st(0) */
1047 if (v
== VT_JMP
|| v
== VT_JMPI
) {
1048 /* need to put correct jump if && or || without test */
1054 /* convert stack entry to register and duplicate its value in another
1056 static void gv_dup(void)
1062 if ((t
& VT_BTYPE
) == VT_LLONG
) {
1069 /* stack: H L L1 H1 */
1077 /* duplicate value */
1082 #ifdef TCC_TARGET_X86_64
1083 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1093 load(r1
, &sv
); /* move r to r1 */
1095 /* duplicates value */
1101 /* Generate value test
1103 * Generate a test for any value (jump, comparison and integers) */
1104 ST_FUNC
int gvtst(int inv
, int t
)
1106 int v
= vtop
->r
& VT_VALMASK
;
1107 if (v
!= VT_CMP
&& v
!= VT_JMP
&& v
!= VT_JMPI
) {
1111 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1112 /* constant jmp optimization */
1113 if ((vtop
->c
.i
!= 0) != inv
)
1118 return gtst(inv
, t
);
1121 #ifndef TCC_TARGET_X86_64
1122 /* generate CPU independent (unsigned) long long operations */
1123 static void gen_opl(int op
)
1125 int t
, a
, b
, op1
, c
, i
;
1127 unsigned short reg_iret
= REG_IRET
;
1128 unsigned short reg_lret
= REG_LRET
;
1134 func
= TOK___divdi3
;
1137 func
= TOK___udivdi3
;
1140 func
= TOK___moddi3
;
1143 func
= TOK___umoddi3
;
1150 /* call generic long long function */
1151 vpush_global_sym(&func_old_type
, func
);
1156 vtop
->r2
= reg_lret
;
1169 /* stack: L1 H1 L2 H2 */
1174 vtop
[-2] = vtop
[-3];
1177 /* stack: H1 H2 L1 L2 */
1183 /* stack: H1 H2 L1 L2 ML MH */
1186 /* stack: ML MH H1 H2 L1 L2 */
1190 /* stack: ML MH H1 L2 H2 L1 */
1195 /* stack: ML MH M1 M2 */
1198 } else if (op
== '+' || op
== '-') {
1199 /* XXX: add non carry method too (for MIPS or alpha) */
1205 /* stack: H1 H2 (L1 op L2) */
1208 gen_op(op1
+ 1); /* TOK_xxxC2 */
1211 /* stack: H1 H2 (L1 op L2) */
1214 /* stack: (L1 op L2) H1 H2 */
1216 /* stack: (L1 op L2) (H1 op H2) */
1224 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1225 t
= vtop
[-1].type
.t
;
1229 /* stack: L H shift */
1231 /* constant: simpler */
1232 /* NOTE: all comments are for SHL. the other cases are
1233 done by swaping words */
1244 if (op
!= TOK_SAR
) {
1277 /* XXX: should provide a faster fallback on x86 ? */
1280 func
= TOK___ashrdi3
;
1283 func
= TOK___lshrdi3
;
1286 func
= TOK___ashldi3
;
1292 /* compare operations */
1298 /* stack: L1 H1 L2 H2 */
1300 vtop
[-1] = vtop
[-2];
1302 /* stack: L1 L2 H1 H2 */
1305 /* when values are equal, we need to compare low words. since
1306 the jump is inverted, we invert the test too. */
1309 else if (op1
== TOK_GT
)
1311 else if (op1
== TOK_ULT
)
1313 else if (op1
== TOK_UGT
)
1318 if (op1
!= TOK_NE
) {
1322 /* generate non equal test */
1323 /* XXX: NOT PORTABLE yet */
1327 #if defined(TCC_TARGET_I386)
1328 b
= psym(0x850f, 0);
1329 #elif defined(TCC_TARGET_ARM)
1331 o(0x1A000000 | encbranch(ind
, 0, 1));
1332 #elif defined(TCC_TARGET_C67)
1333 tcc_error("not implemented");
1335 #error not supported
1339 /* compare low. Always unsigned */
1343 else if (op1
== TOK_LE
)
1345 else if (op1
== TOK_GT
)
1347 else if (op1
== TOK_GE
)
1358 /* handle integer constant optimizations and various machine
1360 static void gen_opic(int op
)
1362 int c1
, c2
, t1
, t2
, n
;
1365 typedef unsigned long long U
;
1369 t1
= v1
->type
.t
& VT_BTYPE
;
1370 t2
= v2
->type
.t
& VT_BTYPE
;
1374 else if (v1
->type
.t
& VT_UNSIGNED
)
1381 else if (v2
->type
.t
& VT_UNSIGNED
)
1386 /* currently, we cannot do computations with forward symbols */
1387 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1388 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1391 case '+': l1
+= l2
; break;
1392 case '-': l1
-= l2
; break;
1393 case '&': l1
&= l2
; break;
1394 case '^': l1
^= l2
; break;
1395 case '|': l1
|= l2
; break;
1396 case '*': l1
*= l2
; break;
1403 /* if division by zero, generate explicit division */
1406 tcc_error("division by zero in constant");
1410 default: l1
/= l2
; break;
1411 case '%': l1
%= l2
; break;
1412 case TOK_UDIV
: l1
= (U
)l1
/ l2
; break;
1413 case TOK_UMOD
: l1
= (U
)l1
% l2
; break;
1416 case TOK_SHL
: l1
<<= l2
; break;
1417 case TOK_SHR
: l1
= (U
)l1
>> l2
; break;
1418 case TOK_SAR
: l1
>>= l2
; break;
1420 case TOK_ULT
: l1
= (U
)l1
< (U
)l2
; break;
1421 case TOK_UGE
: l1
= (U
)l1
>= (U
)l2
; break;
1422 case TOK_EQ
: l1
= l1
== l2
; break;
1423 case TOK_NE
: l1
= l1
!= l2
; break;
1424 case TOK_ULE
: l1
= (U
)l1
<= (U
)l2
; break;
1425 case TOK_UGT
: l1
= (U
)l1
> (U
)l2
; break;
1426 case TOK_LT
: l1
= l1
< l2
; break;
1427 case TOK_GE
: l1
= l1
>= l2
; break;
1428 case TOK_LE
: l1
= l1
<= l2
; break;
1429 case TOK_GT
: l1
= l1
> l2
; break;
1431 case TOK_LAND
: l1
= l1
&& l2
; break;
1432 case TOK_LOR
: l1
= l1
|| l2
; break;
1439 /* if commutative ops, put c2 as constant */
1440 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1441 op
== '|' || op
== '*')) {
1443 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1444 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1446 /* Filter out NOP operations like x*1, x-0, x&-1... */
1447 if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1450 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1451 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1457 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1458 /* try to use shifts instead of muls or divs */
1459 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1468 else if (op
== TOK_PDIV
)
1474 } else if (c2
&& (op
== '+' || op
== '-') &&
1475 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
1476 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1477 /* symbol + constant case */
1484 if (!nocode_wanted
) {
1485 /* call low level op generator */
1486 if (t1
== VT_LLONG
|| t2
== VT_LLONG
)
1497 /* generate a floating point operation with constant propagation */
1498 static void gen_opif(int op
)
1506 /* currently, we cannot do computations with forward symbols */
1507 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1508 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1510 if (v1
->type
.t
== VT_FLOAT
) {
1513 } else if (v1
->type
.t
== VT_DOUBLE
) {
1521 /* NOTE: we only do constant propagation if finite number (not
1522 NaN or infinity) (ANSI spec) */
1523 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1527 case '+': f1
+= f2
; break;
1528 case '-': f1
-= f2
; break;
1529 case '*': f1
*= f2
; break;
1533 tcc_error("division by zero in constant");
1538 /* XXX: also handles tests ? */
1542 /* XXX: overflow test ? */
1543 if (v1
->type
.t
== VT_FLOAT
) {
1545 } else if (v1
->type
.t
== VT_DOUBLE
) {
1553 if (!nocode_wanted
) {
1561 static int pointed_size(CType
*type
)
1564 return type_size(pointed_type(type
), &align
);
1567 static void vla_runtime_pointed_size(CType
*type
)
1570 vla_runtime_type_size(pointed_type(type
), &align
);
1573 static inline int is_null_pointer(SValue
*p
)
1575 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1577 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& p
->c
.i
== 0) ||
1578 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.ll
== 0) ||
1579 ((p
->type
.t
& VT_BTYPE
) == VT_PTR
&& p
->c
.ptr_offset
== 0);
1582 static inline int is_integer_btype(int bt
)
1584 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
1585 bt
== VT_INT
|| bt
== VT_LLONG
);
1588 /* check types for comparison or subtraction of pointers */
1589 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
1591 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
1594 /* null pointers are accepted for all comparisons as gcc */
1595 if (is_null_pointer(p1
) || is_null_pointer(p2
))
1599 bt1
= type1
->t
& VT_BTYPE
;
1600 bt2
= type2
->t
& VT_BTYPE
;
1601 /* accept comparison between pointer and integer with a warning */
1602 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
1603 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
1604 tcc_warning("comparison between pointer and integer");
1608 /* both must be pointers or implicit function pointers */
1609 if (bt1
== VT_PTR
) {
1610 type1
= pointed_type(type1
);
1611 } else if (bt1
!= VT_FUNC
)
1612 goto invalid_operands
;
1614 if (bt2
== VT_PTR
) {
1615 type2
= pointed_type(type2
);
1616 } else if (bt2
!= VT_FUNC
) {
1618 tcc_error("invalid operands to binary %s", get_tok_str(op
, NULL
));
1620 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1621 (type2
->t
& VT_BTYPE
) == VT_VOID
)
1625 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1626 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1627 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1628 /* gcc-like error if '-' is used */
1630 goto invalid_operands
;
1632 tcc_warning("comparison of distinct pointer types lacks a cast");
1636 /* generic gen_op: handles types problems */
1637 ST_FUNC
void gen_op(int op
)
1639 int u
, t1
, t2
, bt1
, bt2
, t
;
1642 t1
= vtop
[-1].type
.t
;
1643 t2
= vtop
[0].type
.t
;
1644 bt1
= t1
& VT_BTYPE
;
1645 bt2
= t2
& VT_BTYPE
;
1647 if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
1648 /* at least one operand is a pointer */
1649 /* relationnal op: must be both pointers */
1650 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
1651 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1652 /* pointers are handled are unsigned */
1653 #ifdef TCC_TARGET_X86_64
1654 t
= VT_LLONG
| VT_UNSIGNED
;
1656 t
= VT_INT
| VT_UNSIGNED
;
1660 /* if both pointers, then it must be the '-' op */
1661 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
1663 tcc_error("cannot use pointers here");
1664 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1665 /* XXX: check that types are compatible */
1666 if (vtop
[-1].type
.t
& VT_VLA
) {
1667 vla_runtime_pointed_size(&vtop
[-1].type
);
1669 vpushi(pointed_size(&vtop
[-1].type
));
1673 /* set to integer type */
1674 #ifdef TCC_TARGET_X86_64
1675 vtop
->type
.t
= VT_LLONG
;
1677 vtop
->type
.t
= VT_INT
;
1682 /* exactly one pointer : must be '+' or '-'. */
1683 if (op
!= '-' && op
!= '+')
1684 tcc_error("cannot use pointers here");
1685 /* Put pointer as first operand */
1686 if (bt2
== VT_PTR
) {
1690 type1
= vtop
[-1].type
;
1691 type1
.t
&= ~VT_ARRAY
;
1692 if (vtop
[-1].type
.t
& VT_VLA
)
1693 vla_runtime_pointed_size(&vtop
[-1].type
);
1695 u
= pointed_size(&vtop
[-1].type
);
1697 tcc_error("unknown array element size");
1698 #ifdef TCC_TARGET_X86_64
1701 /* XXX: cast to int ? (long long case) */
1706 #ifdef CONFIG_TCC_BCHECK
1707 /* if evaluating constant expression, no code should be
1708 generated, so no bound check */
1709 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
1710 /* if bounded pointers, we generate a special code to
1717 gen_bounded_ptr_add();
1723 /* put again type if gen_opic() swaped operands */
1726 } else if (is_float(bt1
) || is_float(bt2
)) {
1727 /* compute bigger type and do implicit casts */
1728 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
1730 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
1735 /* floats can only be used for a few operations */
1736 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
1737 (op
< TOK_ULT
|| op
> TOK_GT
))
1738 tcc_error("invalid operands for binary operation");
1740 } else if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
) {
1741 t
= bt1
== VT_LLONG
? VT_LLONG
: VT_INT
;
1742 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (t
| VT_UNSIGNED
))
1745 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
1746 /* cast to biggest op */
1748 /* convert to unsigned if it does not fit in a long long */
1749 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
1750 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
1753 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
1754 tcc_error("comparison of struct");
1756 /* integer operations */
1758 /* convert to unsigned if it does not fit in an integer */
1759 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
1760 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
1763 /* XXX: currently, some unsigned operations are explicit, so
1764 we modify them here */
1765 if (t
& VT_UNSIGNED
) {
1772 else if (op
== TOK_LT
)
1774 else if (op
== TOK_GT
)
1776 else if (op
== TOK_LE
)
1778 else if (op
== TOK_GE
)
1785 /* special case for shifts and long long: we keep the shift as
1787 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
1794 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
1795 /* relationnal op: the result is an int */
1796 vtop
->type
.t
= VT_INT
;
1803 #ifndef TCC_TARGET_ARM
1804 /* generic itof for unsigned long long case */
1805 static void gen_cvt_itof1(int t
)
1807 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
1808 (VT_LLONG
| VT_UNSIGNED
)) {
1811 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
1812 #if LDOUBLE_SIZE != 8
1813 else if (t
== VT_LDOUBLE
)
1814 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
1817 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
1821 vtop
->r
= reg_fret(t
);
1828 /* generic ftoi for unsigned long long case */
1829 static void gen_cvt_ftoi1(int t
)
1833 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
1834 /* not handled natively */
1835 st
= vtop
->type
.t
& VT_BTYPE
;
1837 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
1838 #if LDOUBLE_SIZE != 8
1839 else if (st
== VT_LDOUBLE
)
1840 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
1843 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
1848 vtop
->r2
= REG_LRET
;
1854 /* force char or short cast */
1855 static void force_charshort_cast(int t
)
1859 /* XXX: add optimization if lvalue : just change type and offset */
1864 if (t
& VT_UNSIGNED
) {
1865 vpushi((1 << bits
) - 1);
1871 /* result must be signed or the SAR is converted to an SHL
1872 This was not the case when "t" was a signed short
1873 and the last value on the stack was an unsigned int */
1874 vtop
->type
.t
&= ~VT_UNSIGNED
;
1880 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1881 static void gen_cast(CType
*type
)
1883 int sbt
, dbt
, sf
, df
, c
, p
;
1885 /* special delayed cast for char/short */
1886 /* XXX: in some cases (multiple cascaded casts), it may still
1888 if (vtop
->r
& VT_MUSTCAST
) {
1889 vtop
->r
&= ~VT_MUSTCAST
;
1890 force_charshort_cast(vtop
->type
.t
);
1893 /* bitfields first get cast to ints */
1894 if (vtop
->type
.t
& VT_BITFIELD
) {
1898 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
1899 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
1904 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1905 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
1907 /* constant case: we can do it now */
1908 /* XXX: in ISOC, cannot do it if error in convert */
1909 if (sbt
== VT_FLOAT
)
1910 vtop
->c
.ld
= vtop
->c
.f
;
1911 else if (sbt
== VT_DOUBLE
)
1912 vtop
->c
.ld
= vtop
->c
.d
;
1915 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
1916 if (sbt
& VT_UNSIGNED
)
1917 vtop
->c
.ld
= vtop
->c
.ull
;
1919 vtop
->c
.ld
= vtop
->c
.ll
;
1921 if (sbt
& VT_UNSIGNED
)
1922 vtop
->c
.ld
= vtop
->c
.ui
;
1924 vtop
->c
.ld
= vtop
->c
.i
;
1927 if (dbt
== VT_FLOAT
)
1928 vtop
->c
.f
= (float)vtop
->c
.ld
;
1929 else if (dbt
== VT_DOUBLE
)
1930 vtop
->c
.d
= (double)vtop
->c
.ld
;
1931 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
1932 vtop
->c
.ull
= (unsigned long long)vtop
->c
.ld
;
1933 } else if (sf
&& dbt
== VT_BOOL
) {
1934 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
1937 vtop
->c
.ll
= (long long)vtop
->c
.ld
;
1938 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
1939 vtop
->c
.ll
= vtop
->c
.ull
;
1940 else if (sbt
& VT_UNSIGNED
)
1941 vtop
->c
.ll
= vtop
->c
.ui
;
1942 #ifdef TCC_TARGET_X86_64
1943 else if (sbt
== VT_PTR
)
1946 else if (sbt
!= VT_LLONG
)
1947 vtop
->c
.ll
= vtop
->c
.i
;
1949 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
1950 vtop
->c
.ull
= vtop
->c
.ll
;
1951 else if (dbt
== VT_BOOL
)
1952 vtop
->c
.i
= (vtop
->c
.ll
!= 0);
1953 #ifdef TCC_TARGET_X86_64
1954 else if (dbt
== VT_PTR
)
1957 else if (dbt
!= VT_LLONG
) {
1959 if ((dbt
& VT_BTYPE
) == VT_BYTE
)
1961 else if ((dbt
& VT_BTYPE
) == VT_SHORT
)
1963 if(dbt
& VT_UNSIGNED
)
1964 vtop
->c
.ui
= ((unsigned int)vtop
->c
.ll
<< s
) >> s
;
1966 vtop
->c
.i
= ((int)vtop
->c
.ll
<< s
) >> s
;
1969 } else if (p
&& dbt
== VT_BOOL
) {
1972 } else if (!nocode_wanted
) {
1973 /* non constant case: generate code */
1975 /* convert from fp to fp */
1978 /* convert int to fp */
1981 /* convert fp to int */
1982 if (dbt
== VT_BOOL
) {
1986 /* we handle char/short/etc... with generic code */
1987 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
1988 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
1992 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
1993 /* additional cast for char/short... */
1998 #ifndef TCC_TARGET_X86_64
1999 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
2000 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
2001 /* scalar to long long */
2002 /* machine independent conversion */
2004 /* generate high word */
2005 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
2009 if (sbt
== VT_PTR
) {
2010 /* cast from pointer to int before we apply
2011 shift operation, which pointers don't support*/
2012 gen_cast(&int_type
);
2018 /* patch second register */
2019 vtop
[-1].r2
= vtop
->r
;
2023 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
2024 (dbt
& VT_BTYPE
) == VT_PTR
||
2025 (dbt
& VT_BTYPE
) == VT_FUNC
) {
2026 if ((sbt
& VT_BTYPE
) != VT_LLONG
&&
2027 (sbt
& VT_BTYPE
) != VT_PTR
&&
2028 (sbt
& VT_BTYPE
) != VT_FUNC
) {
2029 /* need to convert from 32bit to 64bit */
2031 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
2032 /* x86_64 specific: movslq */
2034 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
2038 } else if (dbt
== VT_BOOL
) {
2039 /* scalar to bool */
2042 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
2043 (dbt
& VT_BTYPE
) == VT_SHORT
) {
2044 if (sbt
== VT_PTR
) {
2045 vtop
->type
.t
= VT_INT
;
2046 tcc_warning("nonportable conversion from pointer to char/short");
2048 force_charshort_cast(dbt
);
2049 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
2051 if (sbt
== VT_LLONG
) {
2052 /* from long long: just take low order word */
2056 /* if lvalue and single word type, nothing to do because
2057 the lvalue already contains the real type size (see
2058 VT_LVAL_xxx constants) */
2061 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
2062 /* if we are casting between pointer types,
2063 we must update the VT_LVAL_xxx size */
2064 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
2065 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
2070 /* return type size as known at compile time. Put alignment at 'a' */
2071 ST_FUNC
int type_size(CType
*type
, int *a
)
2076 bt
= type
->t
& VT_BTYPE
;
2077 if (bt
== VT_STRUCT
) {
2082 } else if (bt
== VT_PTR
) {
2083 if (type
->t
& VT_ARRAY
) {
2087 ts
= type_size(&s
->type
, a
);
2089 if (ts
< 0 && s
->c
< 0)
2097 } else if (bt
== VT_LDOUBLE
) {
2099 return LDOUBLE_SIZE
;
2100 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
2101 #ifdef TCC_TARGET_I386
2102 #ifdef TCC_TARGET_PE
2107 #elif defined(TCC_TARGET_ARM)
2117 } else if (bt
== VT_INT
|| bt
== VT_ENUM
|| bt
== VT_FLOAT
) {
2120 } else if (bt
== VT_SHORT
) {
2123 } else if (bt
== VT_QLONG
|| bt
== VT_QFLOAT
) {
2127 /* char, void, function, _Bool */
2133 /* push type size as known at runtime time on top of value stack. Put
2135 ST_FUNC
void vla_runtime_type_size(CType
*type
, int *a
)
2137 if (type
->t
& VT_VLA
) {
2138 vset(&int_type
, VT_LOCAL
|VT_LVAL
, type
->ref
->c
);
2140 vpushi(type_size(type
, a
));
2144 static void vla_sp_save(void) {
2145 if (!(vla_flags
& VLA_SP_LOC_SET
)) {
2146 *vla_sp_loc
= (loc
-= PTR_SIZE
);
2147 vla_flags
|= VLA_SP_LOC_SET
;
2149 if (!(vla_flags
& VLA_SP_SAVED
)) {
2150 gen_vla_sp_save(*vla_sp_loc
);
2151 vla_flags
|= VLA_SP_SAVED
;
2155 /* return the pointed type of t */
2156 static inline CType
*pointed_type(CType
*type
)
2158 return &type
->ref
->type
;
2161 /* modify type so that its it is a pointer to type. */
2162 ST_FUNC
void mk_pointer(CType
*type
)
2165 s
= sym_push(SYM_FIELD
, type
, 0, -1);
2166 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
2170 /* compare function types. OLD functions match any new functions */
2171 static int is_compatible_func(CType
*type1
, CType
*type2
)
2177 if (!is_compatible_types(&s1
->type
, &s2
->type
))
2179 /* check func_call */
2180 if (s1
->a
.func_call
!= s2
->a
.func_call
)
2182 /* XXX: not complete */
2183 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
2187 while (s1
!= NULL
) {
2190 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
2200 /* return true if type1 and type2 are the same. If unqualified is
2201 true, qualifiers on the types are ignored.
2203 - enums are not checked as gcc __builtin_types_compatible_p ()
2205 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2209 t1
= type1
->t
& VT_TYPE
;
2210 t2
= type2
->t
& VT_TYPE
;
2212 /* strip qualifiers before comparing */
2213 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2214 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2216 /* Default Vs explicit signedness only matters for char */
2217 if ((t1
& VT_BTYPE
) != VT_BYTE
) {
2221 /* XXX: bitfields ? */
2224 /* test more complicated cases */
2225 bt1
= t1
& VT_BTYPE
;
2226 if (bt1
== VT_PTR
) {
2227 type1
= pointed_type(type1
);
2228 type2
= pointed_type(type2
);
2229 return is_compatible_types(type1
, type2
);
2230 } else if (bt1
== VT_STRUCT
) {
2231 return (type1
->ref
== type2
->ref
);
2232 } else if (bt1
== VT_FUNC
) {
2233 return is_compatible_func(type1
, type2
);
2239 /* return true if type1 and type2 are exactly the same (including
2242 static int is_compatible_types(CType
*type1
, CType
*type2
)
2244 return compare_types(type1
,type2
,0);
2247 /* return true if type1 and type2 are the same (ignoring qualifiers).
2249 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
2251 return compare_types(type1
,type2
,1);
2254 /* print a type. If 'varstr' is not NULL, then the variable is also
2255 printed in the type */
2257 /* XXX: add array and function pointers */
2258 static void type_to_str(char *buf
, int buf_size
,
2259 CType
*type
, const char *varstr
)
2266 t
= type
->t
& VT_TYPE
;
2269 if (t
& VT_CONSTANT
)
2270 pstrcat(buf
, buf_size
, "const ");
2271 if (t
& VT_VOLATILE
)
2272 pstrcat(buf
, buf_size
, "volatile ");
2273 if ((t
& (VT_DEFSIGN
| VT_UNSIGNED
)) == (VT_DEFSIGN
| VT_UNSIGNED
))
2274 pstrcat(buf
, buf_size
, "unsigned ");
2275 else if (t
& VT_DEFSIGN
)
2276 pstrcat(buf
, buf_size
, "signed ");
2306 tstr
= "long double";
2308 pstrcat(buf
, buf_size
, tstr
);
2312 if (bt
== VT_STRUCT
)
2316 pstrcat(buf
, buf_size
, tstr
);
2317 v
= type
->ref
->v
& ~SYM_STRUCT
;
2318 if (v
>= SYM_FIRST_ANOM
)
2319 pstrcat(buf
, buf_size
, "<anonymous>");
2321 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
2325 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
2326 pstrcat(buf
, buf_size
, "(");
2328 while (sa
!= NULL
) {
2329 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
2330 pstrcat(buf
, buf_size
, buf1
);
2333 pstrcat(buf
, buf_size
, ", ");
2335 pstrcat(buf
, buf_size
, ")");
2339 pstrcpy(buf1
, sizeof(buf1
), "*");
2341 pstrcat(buf1
, sizeof(buf1
), varstr
);
2342 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2346 pstrcat(buf
, buf_size
, " ");
2347 pstrcat(buf
, buf_size
, varstr
);
2352 /* verify type compatibility to store vtop in 'dt' type, and generate
2354 static void gen_assign_cast(CType
*dt
)
2356 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
2357 char buf1
[256], buf2
[256];
2360 st
= &vtop
->type
; /* source type */
2361 dbt
= dt
->t
& VT_BTYPE
;
2362 sbt
= st
->t
& VT_BTYPE
;
2363 if (sbt
== VT_VOID
|| dbt
== VT_VOID
)
2364 tcc_error("cannot cast from/to void");
2365 if (dt
->t
& VT_CONSTANT
)
2366 tcc_warning("assignment of read-only location");
2369 /* special cases for pointers */
2370 /* '0' can also be a pointer */
2371 if (is_null_pointer(vtop
))
2373 /* accept implicit pointer to integer cast with warning */
2374 if (is_integer_btype(sbt
)) {
2375 tcc_warning("assignment makes pointer from integer without a cast");
2378 type1
= pointed_type(dt
);
2379 /* a function is implicitely a function pointer */
2380 if (sbt
== VT_FUNC
) {
2381 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
2382 !is_compatible_types(pointed_type(dt
), st
))
2383 tcc_warning("assignment from incompatible pointer type");
2388 type2
= pointed_type(st
);
2389 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2390 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
2391 /* void * can match anything */
2393 /* exact type match, except for unsigned */
2396 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
|
2398 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
|
2400 if (!is_compatible_types(&tmp_type1
, &tmp_type2
))
2401 tcc_warning("assignment from incompatible pointer type");
2403 /* check const and volatile */
2404 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
2405 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
2406 tcc_warning("assignment discards qualifiers from pointer target type");
2412 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
2413 tcc_warning("assignment makes integer from pointer without a cast");
2415 /* XXX: more tests */
2420 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2421 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2422 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2424 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
2425 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
2426 tcc_error("cannot cast '%s' to '%s'", buf1
, buf2
);
2434 /* store vtop in lvalue pushed on stack */
2435 ST_FUNC
void vstore(void)
2437 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
2439 ft
= vtop
[-1].type
.t
;
2440 sbt
= vtop
->type
.t
& VT_BTYPE
;
2441 dbt
= ft
& VT_BTYPE
;
2442 if ((((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2443 (sbt
== VT_INT
&& dbt
== VT_SHORT
))
2444 && !(vtop
->type
.t
& VT_BITFIELD
)) {
2445 /* optimize char/short casts */
2446 delayed_cast
= VT_MUSTCAST
;
2447 vtop
->type
.t
= ft
& (VT_TYPE
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
)));
2448 /* XXX: factorize */
2449 if (ft
& VT_CONSTANT
)
2450 tcc_warning("assignment of read-only location");
2453 if (!(ft
& VT_BITFIELD
))
2454 gen_assign_cast(&vtop
[-1].type
);
2457 if (sbt
== VT_STRUCT
) {
2458 /* if structure, only generate pointer */
2459 /* structure assignment : generate memcpy */
2460 /* XXX: optimize if small size */
2461 if (!nocode_wanted
) {
2462 size
= type_size(&vtop
->type
, &align
);
2466 vtop
->type
.t
= VT_PTR
;
2469 /* address of memcpy() */
2472 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2473 else if(!(align
& 3))
2474 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2477 vpush_global_sym(&func_old_type
, TOK_memcpy
);
2482 vtop
->type
.t
= VT_PTR
;
2491 /* leave source on stack */
2492 } else if (ft
& VT_BITFIELD
) {
2493 /* bitfield store handling */
2494 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2495 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2496 /* remove bit field info to avoid loops */
2497 vtop
[-1].type
.t
= ft
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
2499 /* duplicate source into other register */
2504 if((ft
& VT_BTYPE
) == VT_BOOL
) {
2505 gen_cast(&vtop
[-1].type
);
2506 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2509 /* duplicate destination */
2511 vtop
[-1] = vtop
[-2];
2513 /* mask and shift source */
2514 if((ft
& VT_BTYPE
) != VT_BOOL
) {
2515 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2516 vpushll((1ULL << bit_size
) - 1ULL);
2518 vpushi((1 << bit_size
) - 1);
2524 /* load destination, mask and or with source */
2526 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2527 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2529 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2536 /* pop off shifted source from "duplicate source..." above */
2540 #ifdef CONFIG_TCC_BCHECK
2541 /* bound check case */
2542 if (vtop
[-1].r
& VT_MUSTBOUND
) {
2548 if (!nocode_wanted
) {
2552 #ifdef TCC_TARGET_X86_64
2553 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
2555 } else if ((ft
& VT_BTYPE
) == VT_QFLOAT
) {
2560 r
= gv(rc
); /* generate value */
2561 /* if lvalue was saved on stack, must read it */
2562 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
2564 t
= get_reg(RC_INT
);
2565 #ifdef TCC_TARGET_X86_64
2570 sv
.r
= VT_LOCAL
| VT_LVAL
;
2571 sv
.c
.ul
= vtop
[-1].c
.ul
;
2573 vtop
[-1].r
= t
| VT_LVAL
;
2575 /* two word case handling : store second register at word + 4 (or +8 for x86-64) */
2576 #ifdef TCC_TARGET_X86_64
2577 if (((ft
& VT_BTYPE
) == VT_QLONG
) || ((ft
& VT_BTYPE
) == VT_QFLOAT
)) {
2578 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
2580 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
2581 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
2583 vtop
[-1].type
.t
= load_type
;
2586 /* convert to int to increment easily */
2587 vtop
->type
.t
= addr_type
;
2593 vtop
[-1].type
.t
= load_type
;
2594 /* XXX: it works because r2 is spilled last ! */
2595 store(vtop
->r2
, vtop
- 1);
2601 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
2602 vtop
->r
|= delayed_cast
;
2606 /* post defines POST/PRE add. c is the token ++ or -- */
2607 ST_FUNC
void inc(int post
, int c
)
2610 vdup(); /* save lvalue */
2612 gv_dup(); /* duplicate value */
2617 vpushi(c
- TOK_MID
);
2619 vstore(); /* store value */
2621 vpop(); /* if post op, return saved value */
2624 /* Parse GNUC __attribute__ extension. Currently, the following
2625 extensions are recognized:
2626 - aligned(n) : set data/function alignment.
2627 - packed : force data alignment to 1
2628 - section(x) : generate data/code in this section.
2629 - unused : currently ignored, but may be used someday.
2630 - regparm(n) : pass function parameters in registers (i386 only)
2632 static void parse_attribute(AttributeDef
*ad
)
2636 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
2640 while (tok
!= ')') {
2641 if (tok
< TOK_IDENT
)
2642 expect("attribute name");
2650 expect("section name");
2651 ad
->section
= find_section(tcc_state
, (char *)tokc
.cstr
->data
);
2659 expect("alias(\"target\")");
2660 ad
->alias_target
= /* save string as token, for later */
2661 tok_alloc((char*)tokc
.cstr
->data
, tokc
.cstr
->size
-1)->tok
;
2670 if (n
<= 0 || (n
& (n
- 1)) != 0)
2671 tcc_error("alignment must be a positive power of two");
2688 /* currently, no need to handle it because tcc does not
2689 track unused objects */
2693 /* currently, no need to handle it because tcc does not
2694 track unused objects */
2699 ad
->a
.func_call
= FUNC_CDECL
;
2704 ad
->a
.func_call
= FUNC_STDCALL
;
2706 #ifdef TCC_TARGET_I386
2716 ad
->a
.func_call
= FUNC_FASTCALL1
+ n
- 1;
2722 ad
->a
.func_call
= FUNC_FASTCALLW
;
2729 ad
->a
.mode
= VT_LLONG
+ 1;
2732 ad
->a
.mode
= VT_SHORT
+ 1;
2735 ad
->a
.mode
= VT_INT
+ 1;
2738 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
2745 ad
->a
.func_export
= 1;
2748 ad
->a
.func_import
= 1;
2751 if (tcc_state
->warn_unsupported
)
2752 tcc_warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
2753 /* skip parameters */
2755 int parenthesis
= 0;
2759 else if (tok
== ')')
2762 } while (parenthesis
&& tok
!= -1);
2775 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2776 static void struct_decl(CType
*type
, int u
, int tdef
)
2778 int a
, v
, size
, align
, maxalign
, c
, offset
, flexible
;
2779 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
, prevbt
;
2780 Sym
*s
, *ss
, *ass
, **ps
;
2784 a
= tok
; /* save decl type */
2789 /* struct already defined ? return it */
2791 expect("struct/union/enum name");
2795 tcc_error("invalid type");
2797 } else if (tok
>= TOK_IDENT
&& !tdef
)
2798 tcc_error("unknown struct/union/enum");
2804 /* we put an undefined size for struct/union */
2805 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
2806 s
->r
= 0; /* default alignment is zero as gcc */
2807 /* put struct/union/enum name in type */
2815 tcc_error("struct/union/enum already defined");
2816 /* cannot be empty */
2818 /* non empty enums are not allowed */
2819 if (a
== TOK_ENUM
) {
2823 expect("identifier");
2825 if (ss
&& !local_stack
)
2826 tcc_error("redefinition of enumerator '%s'",
2827 get_tok_str(v
, NULL
));
2833 /* enum symbols have static storage */
2834 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
2835 ss
->type
.t
|= VT_STATIC
;
2840 /* NOTE: we accept a trailing comma */
2844 s
->c
= type_size(&int_type
, &align
);
2853 while (tok
!= '}') {
2854 parse_btype(&btype
, &ad
);
2857 tcc_error("flexible array member '%s' not at the end of struct",
2858 get_tok_str(v
, NULL
));
2863 type_decl(&type1
, &ad
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
2864 if (v
== 0 && (type1
.t
& VT_BTYPE
) != VT_STRUCT
)
2865 expect("identifier");
2866 if (type_size(&type1
, &align
) < 0) {
2867 if ((a
== TOK_STRUCT
) && (type1
.t
& VT_ARRAY
) && c
)
2870 tcc_error("field '%s' has incomplete type",
2871 get_tok_str(v
, NULL
));
2873 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
2874 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
2875 tcc_error("invalid type for '%s'",
2876 get_tok_str(v
, NULL
));
2880 bit_size
= expr_const();
2881 /* XXX: handle v = 0 case for messages */
2883 tcc_error("negative width in bit-field '%s'",
2884 get_tok_str(v
, NULL
));
2885 if (v
&& bit_size
== 0)
2886 tcc_error("zero width for bit-field '%s'",
2887 get_tok_str(v
, NULL
));
2889 size
= type_size(&type1
, &align
);
2891 if (align
< ad
.a
.aligned
)
2892 align
= ad
.a
.aligned
;
2893 } else if (ad
.a
.packed
) {
2895 } else if (*tcc_state
->pack_stack_ptr
) {
2896 if (align
> *tcc_state
->pack_stack_ptr
)
2897 align
= *tcc_state
->pack_stack_ptr
;
2900 if (bit_size
>= 0) {
2901 bt
= type1
.t
& VT_BTYPE
;
2908 tcc_error("bitfields must have scalar type");
2910 if (bit_size
> bsize
) {
2911 tcc_error("width of '%s' exceeds its type",
2912 get_tok_str(v
, NULL
));
2913 } else if (bit_size
== bsize
) {
2914 /* no need for bit fields */
2916 } else if (bit_size
== 0) {
2917 /* XXX: what to do if only padding in a
2919 /* zero size: means to pad */
2922 /* we do not have enough room ?
2923 did the type change?
2925 if ((bit_pos
+ bit_size
) > bsize
||
2926 bt
!= prevbt
|| a
== TOK_UNION
)
2929 /* XXX: handle LSB first */
2930 type1
.t
|= VT_BITFIELD
|
2931 (bit_pos
<< VT_STRUCT_SHIFT
) |
2932 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
2933 bit_pos
+= bit_size
;
2939 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2940 /* add new memory data only if starting
2942 if (lbit_pos
== 0) {
2943 if (a
== TOK_STRUCT
) {
2944 c
= (c
+ align
- 1) & -align
;
2953 if (align
> maxalign
)
2957 printf("add field %s offset=%d",
2958 get_tok_str(v
, NULL
), offset
);
2959 if (type1
.t
& VT_BITFIELD
) {
2960 printf(" pos=%d size=%d",
2961 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
2962 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
2967 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
2969 while ((ass
= ass
->next
) != NULL
) {
2970 ss
= sym_push(ass
->v
, &ass
->type
, 0, offset
+ ass
->c
);
2975 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
2979 if (tok
== ';' || tok
== TOK_EOF
)
2986 /* store size and alignment */
2987 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
2993 /* return 1 if basic type is a type size (short, long, long long) */
2994 ST_FUNC
int is_btype_size(int bt
)
2996 return bt
== VT_SHORT
|| bt
== VT_LONG
|| bt
== VT_LLONG
;
2999 /* return 0 if no type declaration. otherwise, return the basic type
3002 static int parse_btype(CType
*type
, AttributeDef
*ad
)
3004 int t
, u
, bt_size
, complete
, type_found
, typespec_found
;
3008 memset(ad
, 0, sizeof(AttributeDef
));
3016 /* currently, we really ignore extension */
3027 tcc_error("too many basic types");
3029 bt_size
= is_btype_size (u
& VT_BTYPE
);
3030 if (u
== VT_INT
|| (!bt_size
&& !(t
& VT_TYPEDEF
)))
3045 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
3046 #ifndef TCC_TARGET_PE
3047 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3049 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
3050 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3064 if ((t
& VT_BTYPE
) == VT_LONG
) {
3065 #ifdef TCC_TARGET_PE
3066 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
3068 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3076 struct_decl(&type1
, VT_ENUM
, t
& VT_TYPEDEF
);
3079 type
->ref
= type1
.ref
;
3083 struct_decl(&type1
, VT_STRUCT
, t
& VT_TYPEDEF
);
3086 /* type modifiers */
3102 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == (VT_DEFSIGN
|VT_UNSIGNED
))
3103 tcc_error("signed and unsigned modifier");
3116 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == VT_DEFSIGN
)
3117 tcc_error("signed and unsigned modifier");
3118 t
|= VT_DEFSIGN
| VT_UNSIGNED
;
3143 /* GNUC attribute */
3144 case TOK_ATTRIBUTE1
:
3145 case TOK_ATTRIBUTE2
:
3146 parse_attribute(ad
);
3149 t
= (t
& ~VT_BTYPE
) | u
;
3157 parse_expr_type(&type1
);
3158 /* remove all storage modifiers except typedef */
3159 type1
.t
&= ~(VT_STORAGE
&~VT_TYPEDEF
);
3165 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
3167 t
|= (s
->type
.t
& ~VT_TYPEDEF
);
3168 type
->ref
= s
->type
.ref
;
3170 /* get attributes from typedef */
3171 if (0 == ad
->a
.aligned
)
3172 ad
->a
.aligned
= s
->a
.aligned
;
3173 if (0 == ad
->a
.func_call
)
3174 ad
->a
.func_call
= s
->a
.func_call
;
3175 ad
->a
.packed
|= s
->a
.packed
;
3184 if (tcc_state
->char_is_unsigned
) {
3185 if ((t
& (VT_DEFSIGN
|VT_BTYPE
)) == VT_BYTE
)
3189 /* long is never used as type */
3190 if ((t
& VT_BTYPE
) == VT_LONG
)
3191 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
3192 t
= (t
& ~VT_BTYPE
) | VT_INT
;
3194 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3200 /* convert a function parameter type (array to pointer and function to
3201 function pointer) */
3202 static inline void convert_parameter_type(CType
*pt
)
3204 /* remove const and volatile qualifiers (XXX: const could be used
3205 to indicate a const function parameter */
3206 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3207 /* array must be transformed to pointer according to ANSI C */
3209 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
3214 ST_FUNC
void parse_asm_str(CString
*astr
)
3217 /* read the string */
3219 expect("string constant");
3221 while (tok
== TOK_STR
) {
3222 /* XXX: add \0 handling too ? */
3223 cstr_cat(astr
, tokc
.cstr
->data
);
3226 cstr_ccat(astr
, '\0');
3229 /* Parse an asm label and return the label
3230 * Don't forget to free the CString in the caller! */
3231 static void asm_label_instr(CString
*astr
)
3234 parse_asm_str(astr
);
3237 printf("asm_alias: \"%s\"\n", (char *)astr
->data
);
3241 static void post_type(CType
*type
, AttributeDef
*ad
)
3243 int n
, l
, t1
, arg_size
, align
;
3244 Sym
**plast
, *s
, *first
;
3249 /* function declaration */
3257 /* read param name and compute offset */
3258 if (l
!= FUNC_OLD
) {
3259 if (!parse_btype(&pt
, &ad1
)) {
3261 tcc_error("invalid type");
3268 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
3270 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3271 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
3272 tcc_error("parameter declared as void");
3273 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
3278 expect("identifier");
3282 convert_parameter_type(&pt
);
3283 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
3289 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
3296 /* if no parameters, then old type prototype */
3300 /* NOTE: const is ignored in returned type as it has a special
3301 meaning in gcc / C++ */
3302 type
->t
&= ~VT_CONSTANT
;
3303 /* some ancient pre-K&R C allows a function to return an array
3304 and the array brackets to be put after the arguments, such
3305 that "int c()[]" means something like "int[] c()" */
3308 skip(']'); /* only handle simple "[]" */
3311 /* we push a anonymous symbol which will contain the function prototype */
3312 ad
->a
.func_args
= arg_size
;
3313 s
= sym_push(SYM_FIELD
, type
, 0, l
);
3318 } else if (tok
== '[') {
3319 /* array definition */
3321 if (tok
== TOK_RESTRICT1
)
3326 if (!local_stack
|| nocode_wanted
)
3327 vpushi(expr_const());
3329 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3332 tcc_error("invalid array size");
3334 if (!is_integer_btype(vtop
->type
.t
& VT_BTYPE
))
3335 tcc_error("size of variable length array should be an integer");
3340 /* parse next post type */
3341 post_type(type
, ad
);
3342 if (type
->t
== VT_FUNC
)
3343 tcc_error("declaration of an array of functions");
3344 t1
|= type
->t
& VT_VLA
;
3347 loc
-= type_size(&int_type
, &align
);
3351 vla_runtime_type_size(type
, &align
);
3353 vset(&int_type
, VT_LOCAL
|VT_LVAL
, loc
);
3360 /* we push an anonymous symbol which will contain the array
3362 s
= sym_push(SYM_FIELD
, type
, 0, n
);
3363 type
->t
= (t1
? VT_VLA
: VT_ARRAY
) | VT_PTR
;
3368 /* Parse a type declaration (except basic type), and return the type
3369 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3370 expected. 'type' should contain the basic type. 'ad' is the
3371 attribute definition of the basic type. It can be modified by
3374 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
3377 CType type1
, *type2
;
3378 int qualifiers
, storage
;
3380 while (tok
== '*') {
3388 qualifiers
|= VT_CONSTANT
;
3393 qualifiers
|= VT_VOLATILE
;
3401 type
->t
|= qualifiers
;
3404 /* XXX: clarify attribute handling */
3405 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3406 parse_attribute(ad
);
3408 /* recursive type */
3409 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3410 type1
.t
= 0; /* XXX: same as int */
3413 /* XXX: this is not correct to modify 'ad' at this point, but
3414 the syntax is not clear */
3415 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3416 parse_attribute(ad
);
3417 type_decl(&type1
, ad
, v
, td
);
3420 /* type identifier */
3421 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
3425 if (!(td
& TYPE_ABSTRACT
))
3426 expect("identifier");
3430 storage
= type
->t
& VT_STORAGE
;
3431 type
->t
&= ~VT_STORAGE
;
3432 if (storage
& VT_STATIC
) {
3433 int saved_nocode_wanted
= nocode_wanted
;
3435 post_type(type
, ad
);
3436 nocode_wanted
= saved_nocode_wanted
;
3438 post_type(type
, ad
);
3440 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3441 parse_attribute(ad
);
3445 /* append type at the end of type1 */
3458 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3459 ST_FUNC
int lvalue_type(int t
)
3464 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
3466 else if (bt
== VT_SHORT
)
3470 if (t
& VT_UNSIGNED
)
3471 r
|= VT_LVAL_UNSIGNED
;
3475 /* indirection with full error checking and bound check */
3476 ST_FUNC
void indir(void)
3478 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
3479 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
3483 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
3485 vtop
->type
= *pointed_type(&vtop
->type
);
3486 /* Arrays and functions are never lvalues */
3487 if (!(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_VLA
)
3488 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3489 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3490 /* if bound checking, the referenced pointer must be checked */
3491 #ifdef CONFIG_TCC_BCHECK
3492 if (tcc_state
->do_bounds_check
)
3493 vtop
->r
|= VT_MUSTBOUND
;
3498 /* pass a parameter to a function and do type checking and casting */
3499 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
3504 func_type
= func
->c
;
3505 if (func_type
== FUNC_OLD
||
3506 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
3507 /* default casting : only need to convert float to double */
3508 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
3511 } else if (vtop
->type
.t
& VT_BITFIELD
) {
3512 type
.t
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
3515 } else if (arg
== NULL
) {
3516 tcc_error("too many arguments to function");
3519 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
3520 gen_assign_cast(&type
);
3524 /* parse an expression of the form '(type)' or '(expr)' and return its
3526 static void parse_expr_type(CType
*type
)
3532 if (parse_btype(type
, &ad
)) {
3533 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3540 static void parse_type(CType
*type
)
3545 if (!parse_btype(type
, &ad
)) {
3548 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3551 static void vpush_tokc(int t
)
3556 vsetc(&type
, VT_CONST
, &tokc
);
3559 ST_FUNC
void unary(void)
3561 int n
, t
, align
, size
, r
, sizeof_caller
;
3565 static int in_sizeof
= 0;
3567 sizeof_caller
= in_sizeof
;
3569 /* XXX: GCC 2.95.3 does not generate a table although it should be
3583 vpush_tokc(VT_INT
| VT_UNSIGNED
);
3587 vpush_tokc(VT_LLONG
);
3591 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
3595 vpush_tokc(VT_FLOAT
);
3599 vpush_tokc(VT_DOUBLE
);
3603 vpush_tokc(VT_LDOUBLE
);
3606 case TOK___FUNCTION__
:
3608 goto tok_identifier
;
3614 /* special function name identifier */
3615 len
= strlen(funcname
) + 1;
3616 /* generate char[len] type */
3621 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
3622 ptr
= section_ptr_add(data_section
, len
);
3623 memcpy(ptr
, funcname
, len
);
3628 #ifdef TCC_TARGET_PE
3629 t
= VT_SHORT
| VT_UNSIGNED
;
3635 /* string parsing */
3638 if (tcc_state
->warn_write_strings
)
3643 memset(&ad
, 0, sizeof(AttributeDef
));
3644 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, NULL
, 0);
3649 if (parse_btype(&type
, &ad
)) {
3650 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
3652 /* check ISOC99 compound literal */
3654 /* data is allocated locally by default */
3659 /* all except arrays are lvalues */
3660 if (!(type
.t
& VT_ARRAY
))
3661 r
|= lvalue_type(type
.t
);
3662 memset(&ad
, 0, sizeof(AttributeDef
));
3663 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, NULL
, 0);
3665 if (sizeof_caller
) {
3672 } else if (tok
== '{') {
3673 /* save all registers */
3675 /* statement expression : we do not accept break/continue
3676 inside as GCC does */
3677 block(NULL
, NULL
, NULL
, NULL
, 0, 1);
3692 /* functions names must be treated as function pointers,
3693 except for unary '&' and sizeof. Since we consider that
3694 functions are not lvalues, we only have to handle it
3695 there and in function calls. */
3696 /* arrays can also be used although they are not lvalues */
3697 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
3698 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
3700 mk_pointer(&vtop
->type
);
3706 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3708 boolean
.t
= VT_BOOL
;
3710 vtop
->c
.i
= !vtop
->c
.i
;
3711 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
3712 vtop
->c
.i
= vtop
->c
.i
^ 1;
3715 vseti(VT_JMP
, gvtst(1, 0));
3727 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
3728 tcc_error("pointer not accepted for unary plus");
3729 /* In order to force cast, we add zero, except for floating point
3730 where we really need an noop (otherwise -0.0 will be transformed
3732 if (!is_float(vtop
->type
.t
)) {
3743 unary_type(&type
); // Perform a in_sizeof = 0;
3744 size
= type_size(&type
, &align
);
3745 if (t
== TOK_SIZEOF
) {
3746 if (!(type
.t
& VT_VLA
)) {
3748 tcc_error("sizeof applied to an incomplete type");
3751 vla_runtime_type_size(&type
, &align
);
3756 vtop
->type
.t
|= VT_UNSIGNED
;
3759 case TOK_builtin_types_compatible_p
:
3768 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3769 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3770 vpushi(is_compatible_types(&type1
, &type2
));
3773 case TOK_builtin_constant_p
:
3775 int saved_nocode_wanted
, res
;
3778 saved_nocode_wanted
= nocode_wanted
;
3781 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
3783 nocode_wanted
= saved_nocode_wanted
;
3788 case TOK_builtin_frame_address
:
3794 if (tok
!= TOK_CINT
|| tokc
.i
< 0) {
3795 tcc_error("__builtin_frame_address only takes positive integers");
3802 vset(&type
, VT_LOCAL
, 0); /* local frame */
3804 mk_pointer(&vtop
->type
);
3805 indir(); /* -> parent frame */
3809 #ifdef TCC_TARGET_X86_64
3810 #ifdef TCC_TARGET_PE
3811 case TOK_builtin_va_start
:
3819 if ((vtop
->r
& VT_VALMASK
) != VT_LOCAL
)
3820 tcc_error("__builtin_va_start expects a local variable");
3821 vtop
->r
&= ~(VT_LVAL
| VT_REF
);
3822 vtop
->type
= char_pointer_type
;
3827 case TOK_builtin_va_arg_types
:
3834 vpushi(classify_x86_64_va_arg(&type
));
3849 t
= vtop
->type
.t
& VT_BTYPE
;
3851 /* In IEEE negate(x) isn't subtract(0,x), but rather
3856 else if (t
== VT_DOUBLE
)
3867 goto tok_identifier
;
3869 /* allow to take the address of a label */
3870 if (tok
< TOK_UIDENT
)
3871 expect("label identifier");
3872 s
= label_find(tok
);
3874 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
3876 if (s
->r
== LABEL_DECLARED
)
3877 s
->r
= LABEL_FORWARD
;
3880 s
->type
.t
= VT_VOID
;
3881 mk_pointer(&s
->type
);
3882 s
->type
.t
|= VT_STATIC
;
3884 vpushsym(&s
->type
, s
);
3888 // special qnan , snan and infinity values
3890 vpush64(VT_DOUBLE
, 0x7ff8000000000000ULL
);
3894 vpush64(VT_DOUBLE
, 0x7ff0000000000001ULL
);
3898 vpush64(VT_DOUBLE
, 0x7ff0000000000000ULL
);
3907 expect("identifier");
3910 const char *name
= get_tok_str(t
, NULL
);
3912 tcc_error("'%s' undeclared", name
);
3913 /* for simple function calls, we tolerate undeclared
3914 external reference to int() function */
3915 if (tcc_state
->warn_implicit_function_declaration
3916 #ifdef TCC_TARGET_PE
3917 /* people must be warned about using undeclared WINAPI functions
3918 (which usually start with uppercase letter) */
3919 || (name
[0] >= 'A' && name
[0] <= 'Z')
3922 tcc_warning("implicit declaration of function '%s'", name
);
3923 s
= external_global_sym(t
, &func_old_type
, 0);
3925 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
3926 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
3927 /* if referencing an inline function, then we generate a
3928 symbol to it if not already done. It will have the
3929 effect to generate code for it at the end of the
3930 compilation unit. Inline function as always
3931 generated in the text section. */
3933 put_extern_sym(s
, text_section
, 0, 0);
3934 r
= VT_SYM
| VT_CONST
;
3938 vset(&s
->type
, r
, s
->c
);
3939 /* if forward reference, we must point to s */
3940 if (vtop
->r
& VT_SYM
) {
3942 vtop
->c
.ptr_offset
= 0;
3947 /* post operations */
3949 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
3952 } else if (tok
== '.' || tok
== TOK_ARROW
) {
3955 if (tok
== TOK_ARROW
)
3957 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
3961 /* expect pointer on structure */
3962 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
3963 expect("struct or union");
3967 while ((s
= s
->next
) != NULL
) {
3972 tcc_error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, NULL
));
3973 /* add field offset to pointer */
3974 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
3977 /* change type to field type, and set to lvalue */
3978 vtop
->type
= s
->type
;
3979 vtop
->type
.t
|= qualifiers
;
3980 /* an array is never an lvalue */
3981 if (!(vtop
->type
.t
& VT_ARRAY
)) {
3982 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3983 #ifdef CONFIG_TCC_BCHECK
3984 /* if bound checking, the referenced pointer must be checked */
3985 if (tcc_state
->do_bounds_check
)
3986 vtop
->r
|= VT_MUSTBOUND
;
3990 } else if (tok
== '[') {
3996 } else if (tok
== '(') {
3999 int nb_args
, ret_nregs
, ret_align
, variadic
;
4002 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
4003 /* pointer test (no array accepted) */
4004 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
4005 vtop
->type
= *pointed_type(&vtop
->type
);
4006 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
4010 expect("function pointer");
4013 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
4015 /* get return type */
4018 sa
= s
->next
; /* first parameter */
4021 /* compute first implicit argument if a structure is returned */
4022 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
4023 variadic
= (s
->c
== FUNC_ELLIPSIS
);
4024 ret_nregs
= gfunc_sret(&s
->type
, variadic
, &ret
.type
,
4027 /* get some space for the returned structure */
4028 size
= type_size(&s
->type
, &align
);
4029 loc
= (loc
- size
) & -align
;
4031 ret
.r
= VT_LOCAL
| VT_LVAL
;
4032 /* pass it as 'int' to avoid structure arg passing
4034 vseti(VT_LOCAL
, loc
);
4044 /* return in register */
4045 if (is_float(ret
.type
.t
)) {
4046 ret
.r
= reg_fret(ret
.type
.t
);
4047 #ifdef TCC_TARGET_X86_64
4048 if ((ret
.type
.t
& VT_BTYPE
) == VT_QFLOAT
)
4052 #ifdef TCC_TARGET_X86_64
4053 if ((ret
.type
.t
& VT_BTYPE
) == VT_QLONG
)
4055 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
4065 gfunc_param_typed(s
, sa
);
4075 tcc_error("too few arguments to function");
4077 if (!nocode_wanted
) {
4078 gfunc_call(nb_args
);
4080 vtop
-= (nb_args
+ 1);
4084 for (r
= ret
.r
+ ret_nregs
+ !ret_nregs
; r
-- > ret
.r
;) {
4085 vsetc(&ret
.type
, r
, &ret
.c
);
4086 vtop
->r2
= ret
.r2
; /* Loop only happens when r2 is VT_CONST */
4089 /* handle packed struct return */
4090 if (((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) && ret_nregs
) {
4093 size
= type_size(&s
->type
, &align
);
4094 loc
= (loc
- size
) & -align
;
4098 vset(&ret
.type
, VT_LOCAL
| VT_LVAL
, addr
+ offset
);
4102 if (--ret_nregs
== 0)
4104 /* XXX: compatible with arm only: ret_align == register_size */
4105 offset
+= ret_align
;
4107 vset(&s
->type
, VT_LOCAL
| VT_LVAL
, addr
);
4115 ST_FUNC
void expr_prod(void)
4120 while (tok
== '*' || tok
== '/' || tok
== '%') {
4128 ST_FUNC
void expr_sum(void)
4133 while (tok
== '+' || tok
== '-') {
4141 static void expr_shift(void)
4146 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
4154 static void expr_cmp(void)
4159 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
4160 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
4168 static void expr_cmpeq(void)
4173 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
4181 static void expr_and(void)
4184 while (tok
== '&') {
4191 static void expr_xor(void)
4194 while (tok
== '^') {
4201 static void expr_or(void)
4204 while (tok
== '|') {
4211 /* XXX: fix this mess */
4212 static void expr_land_const(void)
4215 while (tok
== TOK_LAND
) {
4222 /* XXX: fix this mess */
4223 static void expr_lor_const(void)
4226 while (tok
== TOK_LOR
) {
4233 /* only used if non constant */
4234 static void expr_land(void)
4239 if (tok
== TOK_LAND
) {
4244 if (tok
!= TOK_LAND
) {
4254 static void expr_lor(void)
4259 if (tok
== TOK_LOR
) {
4264 if (tok
!= TOK_LOR
) {
4274 /* XXX: better constant handling */
4275 static void expr_cond(void)
4277 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
;
4279 CType type
, type1
, type2
;
4286 boolean
.t
= VT_BOOL
;
4292 if (tok
!= ':' || !gnu_ext
) {
4307 if (vtop
!= vstack
) {
4308 /* needed to avoid having different registers saved in
4310 if (is_float(vtop
->type
.t
)) {
4312 #ifdef TCC_TARGET_X86_64
4313 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4323 if (tok
== ':' && gnu_ext
) {
4331 sv
= *vtop
; /* save value to handle it later */
4332 vtop
--; /* no vpop so that FP stack is not flushed */
4340 bt1
= t1
& VT_BTYPE
;
4342 bt2
= t2
& VT_BTYPE
;
4343 /* cast operands to correct type according to ISOC rules */
4344 if (is_float(bt1
) || is_float(bt2
)) {
4345 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
4346 type
.t
= VT_LDOUBLE
;
4347 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
4352 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
4353 /* cast to biggest op */
4355 /* convert to unsigned if it does not fit in a long long */
4356 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
4357 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
4358 type
.t
|= VT_UNSIGNED
;
4359 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
4360 /* If one is a null ptr constant the result type
4362 if (is_null_pointer (vtop
))
4364 else if (is_null_pointer (&sv
))
4366 /* XXX: test pointer compatibility, C99 has more elaborate
4370 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
4371 /* XXX: test function pointer compatibility */
4372 type
= bt1
== VT_FUNC
? type1
: type2
;
4373 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
4374 /* XXX: test structure compatibility */
4375 type
= bt1
== VT_STRUCT
? type1
: type2
;
4376 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
4377 /* NOTE: as an extension, we accept void on only one side */
4380 /* integer operations */
4382 /* convert to unsigned if it does not fit in an integer */
4383 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
4384 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
4385 type
.t
|= VT_UNSIGNED
;
4388 /* now we convert second operand */
4390 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4393 if (is_float(type
.t
)) {
4395 #ifdef TCC_TARGET_X86_64
4396 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4400 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
4401 /* for long longs, we use fixed registers to avoid having
4402 to handle a complicated move */
4407 /* this is horrible, but we must also convert first
4411 /* put again first value and cast it */
4414 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4417 move_reg(r2
, r1
, type
.t
);
4424 static void expr_eq(void)
4430 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
4431 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
4432 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
4447 ST_FUNC
void gexpr(void)
4458 /* parse an expression and return its type without any side effect. */
4459 static void expr_type(CType
*type
)
4461 int saved_nocode_wanted
;
4463 saved_nocode_wanted
= nocode_wanted
;
4468 nocode_wanted
= saved_nocode_wanted
;
4471 /* parse a unary expression and return its type without any side
4473 static void unary_type(CType
*type
)
4485 /* parse a constant expression and return value in vtop. */
4486 static void expr_const1(void)
4495 /* parse an integer constant and return its value. */
4496 ST_FUNC
int expr_const(void)
4500 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
4501 expect("constant expression");
4507 /* return the label token if current token is a label, otherwise
4509 static int is_label(void)
4513 /* fast test first */
4514 if (tok
< TOK_UIDENT
)
4516 /* no need to save tokc because tok is an identifier */
4523 unget_tok(last_tok
);
4528 static void label_or_decl(int l
)
4532 /* fast test first */
4533 if (tok
>= TOK_UIDENT
)
4535 /* no need to save tokc because tok is an identifier */
4539 unget_tok(last_tok
);
4542 unget_tok(last_tok
);
4547 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
4548 int case_reg
, int is_expr
)
4551 Sym
*s
, *frame_bottom
;
4553 /* generate line number info */
4554 if (tcc_state
->do_debug
&&
4555 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
4556 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
4558 last_line_num
= file
->line_num
;
4562 /* default return value is (void) */
4564 vtop
->type
.t
= VT_VOID
;
4567 if (tok
== TOK_IF
) {
4574 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4576 if (c
== TOK_ELSE
) {
4580 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
4581 gsym(d
); /* patch else jmp */
4584 } else if (tok
== TOK_WHILE
) {
4592 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4596 } else if (tok
== '{') {
4598 int block_vla_sp_loc
, *saved_vla_sp_loc
, saved_vla_flags
;
4601 /* record local declaration stack position */
4603 frame_bottom
= sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4604 frame_bottom
->next
= scope_stack_bottom
;
4605 scope_stack_bottom
= frame_bottom
;
4606 llabel
= local_label_stack
;
4608 /* save VLA state */
4609 block_vla_sp_loc
= *(saved_vla_sp_loc
= vla_sp_loc
);
4610 if (saved_vla_sp_loc
!= &vla_sp_root_loc
)
4611 vla_sp_loc
= &block_vla_sp_loc
;
4613 saved_vla_flags
= vla_flags
;
4614 vla_flags
|= VLA_NEED_NEW_FRAME
;
4616 /* handle local labels declarations */
4617 if (tok
== TOK_LABEL
) {
4620 if (tok
< TOK_UIDENT
)
4621 expect("label identifier");
4622 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
4632 while (tok
!= '}') {
4633 label_or_decl(VT_LOCAL
);
4637 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4640 /* pop locally defined labels */
4641 label_pop(&local_label_stack
, llabel
);
4643 /* XXX: this solution makes only valgrind happy...
4644 triggered by gcc.c-torture/execute/20000917-1.c */
4646 switch(vtop
->type
.t
& VT_BTYPE
) {
4651 for(p
=vtop
->type
.ref
;p
;p
=p
->prev
)
4653 tcc_error("unsupported expression type");
4656 /* pop locally defined symbols */
4657 scope_stack_bottom
= scope_stack_bottom
->next
;
4658 sym_pop(&local_stack
, s
);
4660 /* Pop VLA frames and restore stack pointer if required */
4661 if (saved_vla_sp_loc
!= &vla_sp_root_loc
)
4662 *saved_vla_sp_loc
= block_vla_sp_loc
;
4663 if (vla_sp_loc
!= (saved_vla_sp_loc
== &vla_sp_root_loc
? &vla_sp_root_loc
: &block_vla_sp_loc
)) {
4664 vla_sp_loc
= saved_vla_sp_loc
;
4665 gen_vla_sp_restore(*vla_sp_loc
);
4667 vla_flags
= (vla_flags
& ~VLA_SCOPE_FLAGS
) | (saved_vla_flags
& VLA_SCOPE_FLAGS
);
4670 } else if (tok
== TOK_RETURN
) {
4674 gen_assign_cast(&func_vt
);
4675 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
4676 CType type
, ret_type
;
4677 int ret_align
, ret_nregs
;
4678 ret_nregs
= gfunc_sret(&func_vt
, func_var
, &ret_type
,
4680 if (0 == ret_nregs
) {
4681 /* if returning structure, must copy it to implicit
4682 first pointer arg location */
4685 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
4688 /* copy structure value to pointer */
4691 /* returning structure packed into registers */
4692 int r
, size
, addr
, align
;
4693 size
= type_size(&func_vt
,&align
);
4694 if ((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) || (vtop
->c
.i
& (ret_align
-1)))
4695 && (align
& (ret_align
-1))) {
4696 loc
= (loc
- size
) & -align
;
4699 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
4702 vset(&ret_type
, VT_LOCAL
| VT_LVAL
, addr
);
4704 vtop
->type
= ret_type
;
4705 if (is_float(ret_type
.t
))
4706 r
= rc_fret(ret_type
.t
);
4712 if (--ret_nregs
== 0)
4714 /* We assume that when a structure is returned in multiple
4715 registers, their classes are consecutive values of the
4718 /* XXX: compatible with arm only: ret_align == register_size */
4719 vtop
->c
.i
+= ret_align
;
4720 vtop
->r
= VT_LOCAL
| VT_LVAL
;
4723 } else if (is_float(func_vt
.t
)) {
4724 gv(rc_fret(func_vt
.t
));
4728 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
4731 rsym
= gjmp(rsym
); /* jmp */
4732 } else if (tok
== TOK_BREAK
) {
4735 tcc_error("cannot break");
4736 *bsym
= gjmp(*bsym
);
4739 } else if (tok
== TOK_CONTINUE
) {
4742 tcc_error("cannot continue");
4743 *csym
= gjmp(*csym
);
4746 } else if (tok
== TOK_FOR
) {
4751 frame_bottom
= sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
4752 frame_bottom
->next
= scope_stack_bottom
;
4753 scope_stack_bottom
= frame_bottom
;
4755 /* c99 for-loop init decl? */
4756 if (!decl0(VT_LOCAL
, 1)) {
4757 /* no, regular for-loop init expr */
4781 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4785 scope_stack_bottom
= scope_stack_bottom
->next
;
4786 sym_pop(&local_stack
, s
);
4788 if (tok
== TOK_DO
) {
4793 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
4804 if (tok
== TOK_SWITCH
) {
4808 /* XXX: other types than integer */
4809 case_reg
= gv(RC_INT
);
4813 b
= gjmp(0); /* jump to first case */
4815 block(&a
, csym
, &b
, &c
, case_reg
, 0);
4816 /* if no default, jmp after switch */
4824 if (tok
== TOK_CASE
) {
4831 if (gnu_ext
&& tok
== TOK_DOTS
) {
4835 tcc_warning("empty case range");
4837 /* since a case is like a label, we must skip it with a jmp */
4844 *case_sym
= gtst(1, 0);
4847 *case_sym
= gtst(1, 0);
4851 *case_sym
= gtst(1, *case_sym
);
4856 goto block_after_label
;
4858 if (tok
== TOK_DEFAULT
) {
4864 tcc_error("too many 'default'");
4867 goto block_after_label
;
4869 if (tok
== TOK_GOTO
) {
4871 if (tok
== '*' && gnu_ext
) {
4875 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
4878 } else if (tok
>= TOK_UIDENT
) {
4879 s
= label_find(tok
);
4880 /* put forward definition if needed */
4882 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4884 if (s
->r
== LABEL_DECLARED
)
4885 s
->r
= LABEL_FORWARD
;
4887 /* label already defined */
4888 if (vla_flags
& VLA_IN_SCOPE
) {
4889 /* If VLAs are in use, save the current stack pointer and
4890 reset the stack pointer to what it was at function entry
4891 (label will restore stack pointer in inner scopes) */
4893 gen_vla_sp_restore(vla_sp_root_loc
);
4895 if (s
->r
& LABEL_FORWARD
)
4896 s
->jnext
= gjmp(s
->jnext
);
4898 gjmp_addr(s
->jnext
);
4901 expect("label identifier");
4904 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
4910 if (vla_flags
& VLA_IN_SCOPE
) {
4911 /* save/restore stack pointer across label
4912 this is a no-op when combined with the load immediately
4913 after the label unless we arrive via goto */
4918 if (s
->r
== LABEL_DEFINED
)
4919 tcc_error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
4921 s
->r
= LABEL_DEFINED
;
4923 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
4926 if (vla_flags
& VLA_IN_SCOPE
) {
4927 gen_vla_sp_restore(*vla_sp_loc
);
4928 vla_flags
|= VLA_NEED_NEW_FRAME
;
4930 /* we accept this, but it is a mistake */
4933 tcc_warning("deprecated use of label at end of compound statement");
4937 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
4940 /* expression case */
4955 /* t is the array or struct type. c is the array or struct
4956 address. cur_index/cur_field is the pointer to the current
4957 value. 'size_only' is true if only size info is needed (only used
4959 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
4960 int *cur_index
, Sym
**cur_field
,
4964 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
4970 if (gnu_ext
&& (l
= is_label()) != 0)
4972 while (tok
== '[' || tok
== '.') {
4974 if (!(type
->t
& VT_ARRAY
))
4975 expect("array type");
4978 index
= expr_const();
4979 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
4980 expect("invalid index");
4981 if (tok
== TOK_DOTS
&& gnu_ext
) {
4983 index_last
= expr_const();
4984 if (index_last
< 0 ||
4985 (s
->c
>= 0 && index_last
>= s
->c
) ||
4987 expect("invalid index");
4993 *cur_index
= index_last
;
4994 type
= pointed_type(type
);
4995 elem_size
= type_size(type
, &align
);
4996 c
+= index
* elem_size
;
4997 /* NOTE: we only support ranges for last designator */
4998 nb_elems
= index_last
- index
+ 1;
4999 if (nb_elems
!= 1) {
5008 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
5009 expect("struct/union type");
5022 /* XXX: fix this mess by using explicit storage field */
5024 type1
.t
|= (type
->t
& ~VT_TYPE
);
5038 if (type
->t
& VT_ARRAY
) {
5040 type
= pointed_type(type
);
5041 c
+= index
* type_size(type
, &align
);
5045 tcc_error("too many field init");
5046 /* XXX: fix this mess by using explicit storage field */
5048 type1
.t
|= (type
->t
& ~VT_TYPE
);
5053 decl_initializer(type
, sec
, c
, 0, size_only
);
5055 /* XXX: make it more general */
5056 if (!size_only
&& nb_elems
> 1) {
5057 unsigned long c_end
;
5062 tcc_error("range init not supported yet for dynamic storage");
5063 c_end
= c
+ nb_elems
* elem_size
;
5064 if (c_end
> sec
->data_allocated
)
5065 section_realloc(sec
, c_end
);
5066 src
= sec
->data
+ c
;
5068 for(i
= 1; i
< nb_elems
; i
++) {
5070 memcpy(dst
, src
, elem_size
);
5076 #define EXPR_CONST 1
5079 /* store a value or an expression directly in global data or in local array */
5080 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
5081 int v
, int expr_type
)
5083 int saved_global_expr
, bt
, bit_pos
, bit_size
;
5085 unsigned long long bit_mask
;
5093 /* compound literals must be allocated globally in this case */
5094 saved_global_expr
= global_expr
;
5097 global_expr
= saved_global_expr
;
5098 /* NOTE: symbols are accepted */
5099 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
5100 tcc_error("initializer element is not constant");
5108 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
5111 /* XXX: not portable */
5112 /* XXX: generate error if incorrect relocation */
5113 gen_assign_cast(&dtype
);
5114 bt
= type
->t
& VT_BTYPE
;
5115 /* we'll write at most 12 bytes */
5116 if (c
+ 12 > sec
->data_allocated
) {
5117 section_realloc(sec
, c
+ 12);
5119 ptr
= sec
->data
+ c
;
5120 /* XXX: make code faster ? */
5121 if (!(type
->t
& VT_BITFIELD
)) {
5126 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5127 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
5128 bit_mask
= (1LL << bit_size
) - 1;
5130 if ((vtop
->r
& VT_SYM
) &&
5136 (bt
== VT_INT
&& bit_size
!= 32)))
5137 tcc_error("initializer element is not computable at load time");
5140 vtop
->c
.i
= (vtop
->c
.i
!= 0);
5142 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5145 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5148 *(double *)ptr
= vtop
->c
.d
;
5151 *(long double *)ptr
= vtop
->c
.ld
;
5154 *(long long *)ptr
|= (vtop
->c
.ll
& bit_mask
) << bit_pos
;
5157 if (vtop
->r
& VT_SYM
) {
5158 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
5160 *(addr_t
*)ptr
|= (vtop
->c
.ptr_offset
& bit_mask
) << bit_pos
;
5163 if (vtop
->r
& VT_SYM
) {
5164 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
5166 *(int *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5171 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
5178 /* put zeros for variable based init */
5179 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
5182 /* nothing to do because globals are already set to zero */
5184 vpush_global_sym(&func_old_type
, TOK_memset
);
5186 #ifdef TCC_TARGET_ARM
5197 /* 't' contains the type and storage info. 'c' is the offset of the
5198 object in section 'sec'. If 'sec' is NULL, it means stack based
5199 allocation. 'first' is true if array '{' must be read (multi
5200 dimension implicit array init handling). 'size_only' is true if
5201 size only evaluation is wanted (only for arrays). */
5202 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
5203 int first
, int size_only
)
5205 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, parlevel1
, i
;
5206 int size1
, align1
, expr_type
;
5210 if (type
->t
& VT_VLA
) {
5213 /* save current stack pointer */
5214 if (vla_flags
& VLA_NEED_NEW_FRAME
) {
5216 vla_flags
= VLA_IN_SCOPE
;
5217 vla_sp_loc
= &vla_sp_loc_tmp
;
5220 vla_runtime_type_size(type
, &a
);
5221 gen_vla_alloc(type
, a
);
5222 vset(type
, VT_LOCAL
|VT_LVAL
, c
);
5226 } else if (type
->t
& VT_ARRAY
) {
5230 t1
= pointed_type(type
);
5231 size1
= type_size(t1
, &align1
);
5234 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
5237 tcc_error("character array initializer must be a literal,"
5238 " optionally enclosed in braces");
5243 /* only parse strings here if correct type (otherwise: handle
5244 them as ((w)char *) expressions */
5245 if ((tok
== TOK_LSTR
&&
5246 #ifdef TCC_TARGET_PE
5247 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
5249 (t1
->t
& VT_BTYPE
) == VT_INT
5251 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
5252 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5257 /* compute maximum number of chars wanted */
5259 cstr_len
= cstr
->size
;
5261 cstr_len
= cstr
->size
/ sizeof(nwchar_t
);
5264 if (n
>= 0 && nb
> (n
- array_length
))
5265 nb
= n
- array_length
;
5268 tcc_warning("initializer-string for array is too long");
5269 /* in order to go faster for common case (char
5270 string in global variable, we handle it
5272 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
5273 memcpy(sec
->data
+ c
+ array_length
, cstr
->data
, nb
);
5277 ch
= ((unsigned char *)cstr
->data
)[i
];
5279 ch
= ((nwchar_t
*)cstr
->data
)[i
];
5280 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
5288 /* only add trailing zero if enough storage (no
5289 warning in this case since it is standard) */
5290 if (n
< 0 || array_length
< n
) {
5292 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
5298 while (tok
!= '}') {
5299 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
5300 if (n
>= 0 && index
>= n
)
5301 tcc_error("index too large");
5302 /* must put zero in holes (note that doing it that way
5303 ensures that it even works with designators) */
5304 if (!size_only
&& array_length
< index
) {
5305 init_putz(t1
, sec
, c
+ array_length
* size1
,
5306 (index
- array_length
) * size1
);
5309 if (index
> array_length
)
5310 array_length
= index
;
5311 /* special test for multi dimensional arrays (may not
5312 be strictly correct if designators are used at the
5314 if (index
>= n
&& no_oblock
)
5323 /* put zeros at the end */
5324 if (!size_only
&& n
>= 0 && array_length
< n
) {
5325 init_putz(t1
, sec
, c
+ array_length
* size1
,
5326 (n
- array_length
) * size1
);
5328 /* patch type size if needed */
5330 s
->c
= array_length
;
5331 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
5332 (sec
|| !first
|| tok
== '{')) {
5335 /* NOTE: the previous test is a specific case for automatic
5336 struct/union init */
5337 /* XXX: union needs only one init */
5339 /* XXX: this test is incorrect for local initializers
5340 beginning with ( without {. It would be much more difficult
5341 to do it correctly (ideally, the expression parser should
5342 be used in all cases) */
5348 while (tok
== '(') {
5352 if (!parse_btype(&type1
, &ad1
))
5354 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
5356 if (!is_assignable_types(type
, &type1
))
5357 tcc_error("invalid type for cast");
5362 if (first
|| tok
== '{') {
5371 while (tok
!= '}') {
5372 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
5374 if (!size_only
&& array_length
< index
) {
5375 init_putz(type
, sec
, c
+ array_length
,
5376 index
- array_length
);
5378 index
= index
+ type_size(&f
->type
, &align1
);
5379 if (index
> array_length
)
5380 array_length
= index
;
5382 /* gr: skip fields from same union - ugly. */
5384 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
5385 /* test for same offset */
5386 if (f
->next
->c
!= f
->c
)
5388 /* if yes, test for bitfield shift */
5389 if ((f
->type
.t
& VT_BITFIELD
) && (f
->next
->type
.t
& VT_BITFIELD
)) {
5390 int bit_pos_1
= (f
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5391 int bit_pos_2
= (f
->next
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5392 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
5393 if (bit_pos_1
!= bit_pos_2
)
5400 if (no_oblock
&& f
== NULL
)
5406 /* put zeros at the end */
5407 if (!size_only
&& array_length
< n
) {
5408 init_putz(type
, sec
, c
+ array_length
,
5417 } else if (tok
== '{') {
5419 decl_initializer(type
, sec
, c
, first
, size_only
);
5421 } else if (size_only
) {
5422 /* just skip expression */
5423 parlevel
= parlevel1
= 0;
5424 while ((parlevel
> 0 || parlevel1
> 0 ||
5425 (tok
!= '}' && tok
!= ',')) && tok
!= -1) {
5428 else if (tok
== ')')
5430 else if (tok
== '{')
5432 else if (tok
== '}')
5437 /* currently, we always use constant expression for globals
5438 (may change for scripting case) */
5439 expr_type
= EXPR_CONST
;
5441 expr_type
= EXPR_ANY
;
5442 init_putv(type
, sec
, c
, 0, expr_type
);
5446 /* parse an initializer for type 't' if 'has_init' is non zero, and
5447 allocate space in local or global data space ('r' is either
5448 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5449 variable 'v' with an associated name represented by 'asm_label' of
5450 scope 'scope' is declared before initializers are parsed. If 'v' is
5451 zero, then a reference to the new object is put in the value stack.
5452 If 'has_init' is 2, a special parsing is done to handle string
5454 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
5455 int has_init
, int v
, char *asm_label
,
5458 int size
, align
, addr
, data_offset
;
5460 ParseState saved_parse_state
= {0};
5461 TokenString init_str
;
5463 Sym
*flexible_array
;
5465 flexible_array
= NULL
;
5466 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
5467 Sym
*field
= type
->ref
->next
;
5470 field
= field
->next
;
5471 if (field
->type
.t
& VT_ARRAY
&& field
->type
.ref
->c
< 0)
5472 flexible_array
= field
;
5476 size
= type_size(type
, &align
);
5477 /* If unknown size, we must evaluate it before
5478 evaluating initializers because
5479 initializers can generate global data too
5480 (e.g. string pointers or ISOC99 compound
5481 literals). It also simplifies local
5482 initializers handling */
5483 tok_str_new(&init_str
);
5484 if (size
< 0 || (flexible_array
&& has_init
)) {
5486 tcc_error("unknown type size");
5487 /* get all init string */
5488 if (has_init
== 2) {
5489 /* only get strings */
5490 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5491 tok_str_add_tok(&init_str
);
5496 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
5498 tcc_error("unexpected end of file in initializer");
5499 tok_str_add_tok(&init_str
);
5502 else if (tok
== '}') {
5512 tok_str_add(&init_str
, -1);
5513 tok_str_add(&init_str
, 0);
5516 save_parse_state(&saved_parse_state
);
5518 macro_ptr
= init_str
.str
;
5520 decl_initializer(type
, NULL
, 0, 1, 1);
5521 /* prepare second initializer parsing */
5522 macro_ptr
= init_str
.str
;
5525 /* if still unknown size, error */
5526 size
= type_size(type
, &align
);
5528 tcc_error("unknown type size");
5531 size
+= flexible_array
->type
.ref
->c
* pointed_size(&flexible_array
->type
);
5532 /* take into account specified alignment if bigger */
5533 if (ad
->a
.aligned
) {
5534 if (ad
->a
.aligned
> align
)
5535 align
= ad
->a
.aligned
;
5536 } else if (ad
->a
.packed
) {
5539 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
5541 #ifdef CONFIG_TCC_BCHECK
5542 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5546 loc
= (loc
- size
) & -align
;
5548 #ifdef CONFIG_TCC_BCHECK
5549 /* handles bounds */
5550 /* XXX: currently, since we do only one pass, we cannot track
5551 '&' operators, so we add only arrays */
5552 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
5553 unsigned long *bounds_ptr
;
5554 /* add padding between regions */
5556 /* then add local bound info */
5557 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(unsigned long));
5558 bounds_ptr
[0] = addr
;
5559 bounds_ptr
[1] = size
;
5563 /* local variable */
5564 sym_push(v
, type
, r
, addr
);
5566 /* push local reference */
5567 vset(type
, r
, addr
);
5573 if (v
&& scope
== VT_CONST
) {
5574 /* see if the symbol was already defined */
5577 if (!is_compatible_types(&sym
->type
, type
))
5578 tcc_error("incompatible types for redefinition of '%s'",
5579 get_tok_str(v
, NULL
));
5580 if (sym
->type
.t
& VT_EXTERN
) {
5581 /* if the variable is extern, it was not allocated */
5582 sym
->type
.t
&= ~VT_EXTERN
;
5583 /* set array size if it was omitted in extern
5585 if ((sym
->type
.t
& VT_ARRAY
) &&
5586 sym
->type
.ref
->c
< 0 &&
5588 sym
->type
.ref
->c
= type
->ref
->c
;
5590 /* we accept several definitions of the same
5591 global variable. this is tricky, because we
5592 must play with the SHN_COMMON type of the symbol */
5593 /* XXX: should check if the variable was already
5594 initialized. It is incorrect to initialized it
5596 /* no init data, we won't add more to the symbol */
5603 /* allocate symbol in corresponding section */
5608 else if (tcc_state
->nocommon
)
5612 data_offset
= sec
->data_offset
;
5613 data_offset
= (data_offset
+ align
- 1) & -align
;
5615 /* very important to increment global pointer at this time
5616 because initializers themselves can create new initializers */
5617 data_offset
+= size
;
5618 #ifdef CONFIG_TCC_BCHECK
5619 /* add padding if bound check */
5620 if (tcc_state
->do_bounds_check
)
5623 sec
->data_offset
= data_offset
;
5624 /* allocate section space to put the data */
5625 if (sec
->sh_type
!= SHT_NOBITS
&&
5626 data_offset
> sec
->data_allocated
)
5627 section_realloc(sec
, data_offset
);
5628 /* align section if needed */
5629 if (align
> sec
->sh_addralign
)
5630 sec
->sh_addralign
= align
;
5632 addr
= 0; /* avoid warning */
5636 if (scope
!= VT_CONST
|| !sym
) {
5637 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
5638 sym
->asm_label
= asm_label
;
5640 /* update symbol definition */
5642 put_extern_sym(sym
, sec
, addr
, size
);
5645 /* put a common area */
5646 put_extern_sym(sym
, NULL
, align
, size
);
5647 /* XXX: find a nicer way */
5648 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
5649 esym
->st_shndx
= SHN_COMMON
;
5652 /* push global reference */
5653 sym
= get_sym_ref(type
, sec
, addr
, size
);
5654 vpushsym(type
, sym
);
5656 /* patch symbol weakness */
5657 if (type
->t
& VT_WEAK
)
5659 #ifdef CONFIG_TCC_BCHECK
5660 /* handles bounds now because the symbol must be defined
5661 before for the relocation */
5662 if (tcc_state
->do_bounds_check
) {
5663 unsigned long *bounds_ptr
;
5665 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
5666 /* then add global bound info */
5667 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(long));
5668 bounds_ptr
[0] = 0; /* relocated */
5669 bounds_ptr
[1] = size
;
5673 if (has_init
|| (type
->t
& VT_VLA
)) {
5674 decl_initializer(type
, sec
, addr
, 1, 0);
5675 /* restore parse state if needed */
5677 tok_str_free(init_str
.str
);
5678 restore_parse_state(&saved_parse_state
);
5680 /* patch flexible array member size back to -1, */
5681 /* for possible subsequent similar declarations */
5683 flexible_array
->type
.ref
->c
= -1;
5688 static void put_func_debug(Sym
*sym
)
5693 /* XXX: we put here a dummy type */
5694 snprintf(buf
, sizeof(buf
), "%s:%c1",
5695 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
5696 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
5697 cur_text_section
, sym
->c
);
5698 /* //gr gdb wants a line at the function */
5699 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
5704 /* parse an old style function declaration list */
5705 /* XXX: check multiple parameter */
5706 static void func_decl_list(Sym
*func_sym
)
5713 /* parse each declaration */
5714 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
&&
5715 tok
!= TOK_ASM1
&& tok
!= TOK_ASM2
&& tok
!= TOK_ASM3
) {
5716 if (!parse_btype(&btype
, &ad
))
5717 expect("declaration list");
5718 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5719 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5721 /* we accept no variable after */
5725 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5726 /* find parameter in function parameter list */
5729 if ((s
->v
& ~SYM_FIELD
) == v
)
5733 tcc_error("declaration for parameter '%s' but no such parameter",
5734 get_tok_str(v
, NULL
));
5736 /* check that no storage specifier except 'register' was given */
5737 if (type
.t
& VT_STORAGE
)
5738 tcc_error("storage class specified for '%s'", get_tok_str(v
, NULL
));
5739 convert_parameter_type(&type
);
5740 /* we can add the type (NOTE: it could be local to the function) */
5742 /* accept other parameters */
5753 /* parse a function defined by symbol 'sym' and generate its code in
5754 'cur_text_section' */
5755 static void gen_function(Sym
*sym
)
5757 int saved_nocode_wanted
= nocode_wanted
;
5759 ind
= cur_text_section
->data_offset
;
5760 /* NOTE: we patch the symbol size later */
5761 put_extern_sym(sym
, cur_text_section
, ind
, 0);
5762 funcname
= get_tok_str(sym
->v
, NULL
);
5764 /* Initialize VLA state */
5765 vla_sp_loc
= &vla_sp_root_loc
;
5766 vla_flags
= VLA_NEED_NEW_FRAME
;
5767 /* put debug symbol */
5768 if (tcc_state
->do_debug
)
5769 put_func_debug(sym
);
5770 /* push a dummy symbol to enable local sym storage */
5771 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
5772 gfunc_prolog(&sym
->type
);
5773 #ifdef CONFIG_TCC_BCHECK
5774 if (tcc_state
->do_bounds_check
5775 && !strcmp(get_tok_str(sym
->v
, NULL
), "main")) {
5779 for (i
= 0, sym
= local_stack
; i
< 2; i
++, sym
= sym
->prev
) {
5780 if (sym
->v
& SYM_FIELD
|| sym
->prev
->v
& SYM_FIELD
)
5782 vpush_global_sym(&func_old_type
, TOK___bound_main_arg
);
5783 vset(&sym
->type
, sym
->r
, sym
->c
);
5789 block(NULL
, NULL
, NULL
, NULL
, 0, 0);
5792 cur_text_section
->data_offset
= ind
;
5793 label_pop(&global_label_stack
, NULL
);
5794 /* reset local stack */
5795 scope_stack_bottom
= NULL
;
5796 sym_pop(&local_stack
, NULL
);
5797 /* end of function */
5798 /* patch symbol size */
5799 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
5801 /* patch symbol weakness (this definition overrules any prototype) */
5802 if (sym
->type
.t
& VT_WEAK
)
5804 if (tcc_state
->do_debug
) {
5805 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
5807 /* It's better to crash than to generate wrong code */
5808 cur_text_section
= NULL
;
5809 funcname
= ""; /* for safety */
5810 func_vt
.t
= VT_VOID
; /* for safety */
5811 func_var
= 0; /* for safety */
5812 ind
= 0; /* for safety */
5813 nocode_wanted
= saved_nocode_wanted
;
5816 ST_FUNC
void gen_inline_functions(void)
5819 int *str
, inline_generated
, i
;
5820 struct InlineFunc
*fn
;
5822 /* iterate while inline function are referenced */
5824 inline_generated
= 0;
5825 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5826 fn
= tcc_state
->inline_fns
[i
];
5828 if (sym
&& sym
->c
) {
5829 /* the function was used: generate its code and
5830 convert it to a normal function */
5831 str
= fn
->token_str
;
5834 pstrcpy(file
->filename
, sizeof file
->filename
, fn
->filename
);
5835 sym
->r
= VT_SYM
| VT_CONST
;
5836 sym
->type
.t
&= ~VT_INLINE
;
5840 cur_text_section
= text_section
;
5842 macro_ptr
= NULL
; /* fail safe */
5844 inline_generated
= 1;
5847 if (!inline_generated
)
5850 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
5851 fn
= tcc_state
->inline_fns
[i
];
5852 str
= fn
->token_str
;
5855 dynarray_reset(&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
);
5858 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
5859 static int decl0(int l
, int is_for_loop_init
)
5867 if (!parse_btype(&btype
, &ad
)) {
5868 if (is_for_loop_init
)
5870 /* skip redundant ';' */
5871 /* XXX: find more elegant solution */
5876 if (l
== VT_CONST
&&
5877 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5878 /* global asm block */
5882 /* special test for old K&R protos without explicit int
5883 type. Only accepted when defining global data */
5884 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
5888 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
5889 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
5891 /* we accept no variable after */
5895 while (1) { /* iterate thru each declaration */
5896 char *asm_label
; // associated asm label
5898 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
5902 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
5903 printf("type = '%s'\n", buf
);
5906 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
5907 if ((type
.t
& VT_STATIC
) && (l
== VT_LOCAL
)) {
5908 tcc_error("function without file scope cannot be static");
5910 /* if old style function prototype, we accept a
5913 if (sym
->c
== FUNC_OLD
)
5914 func_decl_list(sym
);
5918 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
5921 asm_label_instr(&astr
);
5922 asm_label
= tcc_strdup(astr
.data
);
5925 /* parse one last attribute list, after asm label */
5926 parse_attribute(&ad
);
5931 #ifdef TCC_TARGET_PE
5932 if (ad
.a
.func_import
)
5933 type
.t
|= VT_IMPORT
;
5934 if (ad
.a
.func_export
)
5935 type
.t
|= VT_EXPORT
;
5939 tcc_error("cannot use local functions");
5940 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
5941 expect("function definition");
5943 /* reject abstract declarators in function definition */
5945 while ((sym
= sym
->next
) != NULL
)
5946 if (!(sym
->v
& ~SYM_FIELD
))
5947 expect("identifier");
5949 /* XXX: cannot do better now: convert extern line to static inline */
5950 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
5951 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5956 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
5959 ref
= sym
->type
.ref
;
5960 if (0 == ref
->a
.func_proto
)
5961 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
5963 /* use func_call from prototype if not defined */
5964 if (ref
->a
.func_call
!= FUNC_CDECL
5965 && type
.ref
->a
.func_call
== FUNC_CDECL
)
5966 type
.ref
->a
.func_call
= ref
->a
.func_call
;
5968 /* use export from prototype */
5969 if (ref
->a
.func_export
)
5970 type
.ref
->a
.func_export
= 1;
5972 /* use static from prototype */
5973 if (sym
->type
.t
& VT_STATIC
)
5974 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
5976 if (!is_compatible_types(&sym
->type
, &type
)) {
5978 tcc_error("incompatible types for redefinition of '%s'",
5979 get_tok_str(v
, NULL
));
5981 type
.ref
->a
.func_proto
= 0;
5982 /* if symbol is already defined, then put complete type */
5985 /* put function symbol */
5986 sym
= global_identifier_push(v
, type
.t
, 0);
5987 sym
->type
.ref
= type
.ref
;
5990 /* static inline functions are just recorded as a kind
5991 of macro. Their code will be emitted at the end of
5992 the compilation unit only if they are used */
5993 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
5994 (VT_INLINE
| VT_STATIC
)) {
5995 TokenString func_str
;
5997 struct InlineFunc
*fn
;
5998 const char *filename
;
6000 tok_str_new(&func_str
);
6006 tcc_error("unexpected end of file");
6007 tok_str_add_tok(&func_str
);
6012 } else if (t
== '}') {
6014 if (block_level
== 0)
6018 tok_str_add(&func_str
, -1);
6019 tok_str_add(&func_str
, 0);
6020 filename
= file
? file
->filename
: "";
6021 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
6022 strcpy(fn
->filename
, filename
);
6024 fn
->token_str
= func_str
.str
;
6025 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
6028 /* compute text section */
6029 cur_text_section
= ad
.section
;
6030 if (!cur_text_section
)
6031 cur_text_section
= text_section
;
6032 sym
->r
= VT_SYM
| VT_CONST
;
6037 if (btype
.t
& VT_TYPEDEF
) {
6038 /* save typedefed type */
6039 /* XXX: test storage specifiers ? */
6040 sym
= sym_push(v
, &type
, 0, 0);
6042 sym
->type
.t
|= VT_TYPEDEF
;
6045 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
6046 /* external function definition */
6047 /* specific case for func_call attribute */
6048 ad
.a
.func_proto
= 1;
6050 } else if (!(type
.t
& VT_ARRAY
)) {
6051 /* not lvalue if array */
6052 r
|= lvalue_type(type
.t
);
6054 has_init
= (tok
== '=');
6055 if (has_init
&& (type
.t
& VT_VLA
))
6056 tcc_error("Variable length array cannot be initialized");
6057 if ((btype
.t
& VT_EXTERN
) || ((type
.t
& VT_BTYPE
) == VT_FUNC
) ||
6058 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
6059 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
6060 /* external variable or function */
6061 /* NOTE: as GCC, uninitialized global static
6062 arrays of null size are considered as
6064 sym
= external_sym(v
, &type
, r
, asm_label
);
6066 if (type
.t
& VT_WEAK
)
6069 if (ad
.alias_target
) {
6074 alias_target
= sym_find(ad
.alias_target
);
6075 if (!alias_target
|| !alias_target
->c
)
6076 tcc_error("unsupported forward __alias__ attribute");
6077 esym
= &((Elf32_Sym
*)symtab_section
->data
)[alias_target
->c
];
6078 tsec
.sh_num
= esym
->st_shndx
;
6079 put_extern_sym2(sym
, &tsec
, esym
->st_value
, esym
->st_size
, 0);
6082 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
6083 if (type
.t
& VT_STATIC
)
6089 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, asm_label
, l
);
6093 if (is_for_loop_init
)
6106 ST_FUNC
void decl(int l
)