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 Sym
*sym_free_first
;
34 ST_DATA
void **sym_pools
;
35 ST_DATA
int nb_sym_pools
;
37 ST_DATA Sym
*global_stack
;
38 ST_DATA Sym
*local_stack
;
39 ST_DATA Sym
*define_stack
;
40 ST_DATA Sym
*global_label_stack
;
41 ST_DATA Sym
*local_label_stack
;
42 static int local_scope
;
44 static int section_sym
;
46 ST_DATA
int vlas_in_scope
; /* number of VLAs that are currently in scope */
47 ST_DATA
int vla_sp_root_loc
; /* vla_sp_loc for SP before any VLAs were pushed */
48 ST_DATA
int vla_sp_loc
; /* Pointer to variable holding location to store stack pointer on the stack when modifying stack pointer */
50 ST_DATA SValue __vstack
[1+VSTACK_SIZE
], *vtop
, *pvtop
;
52 ST_DATA
int const_wanted
; /* true if constant wanted */
53 ST_DATA
int nocode_wanted
; /* true if no code generation wanted for an expression */
54 ST_DATA
int global_expr
; /* true if compound literals must be allocated globally (used during initializers parsing */
55 ST_DATA CType func_vt
; /* current function return type (used by return instruction) */
56 ST_DATA
int func_var
; /* true if current function is variadic (used by return instruction) */
58 ST_DATA
int last_line_num
, last_ind
, func_ind
; /* debug last line number and pc */
59 ST_DATA
const char *funcname
;
61 ST_DATA CType char_pointer_type
, func_old_type
, int_type
, size_type
;
63 ST_DATA
struct switch_t
{
67 } **p
; int n
; /* list of case ranges */
68 int def_sym
; /* default symbol */
69 } *cur_switch
; /* current switch */
71 /* ------------------------------------------------------------------------- */
73 static void gen_cast(CType
*type
);
74 static inline CType
*pointed_type(CType
*type
);
75 static int is_compatible_types(CType
*type1
, CType
*type2
);
76 static int parse_btype(CType
*type
, AttributeDef
*ad
);
77 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
);
78 static void parse_expr_type(CType
*type
);
79 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
, int first
, int size_only
);
80 static void block(int *bsym
, int *csym
, int is_expr
);
81 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
, int has_init
, int v
, int scope
);
82 static int decl0(int l
, int is_for_loop_init
);
83 static void expr_eq(void);
84 static void expr_lor_const(void);
85 static void unary_type(CType
*type
);
86 static void vla_runtime_type_size(CType
*type
, int *a
);
87 static void vla_sp_restore(void);
88 static void vla_sp_restore_root(void);
89 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
);
90 static void expr_type(CType
*type
);
91 static inline int64_t expr_const64(void);
92 ST_FUNC
void vpush64(int ty
, unsigned long long v
);
93 ST_FUNC
void vpush(CType
*type
);
94 ST_FUNC
int gvtst(int inv
, int t
);
95 ST_FUNC
int is_btype_size(int bt
);
96 static void gen_inline_functions(TCCState
*s
);
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 ST_FUNC
void check_vstack(void)
124 tcc_error("internal compiler error: vstack leak (%d)", vtop
- pvtop
);
127 /* ------------------------------------------------------------------------- */
128 /* vstack debugging aid */
131 void pv (const char *lbl
, int a
, int b
)
134 for (i
= a
; i
< a
+ b
; ++i
) {
135 SValue
*p
= &vtop
[-i
];
136 printf("%s vtop[-%d] : type.t:%04x r:%04x r2:%04x c.i:%d\n",
137 lbl
, i
, p
->type
.t
, p
->r
, p
->r2
, (int)p
->c
.i
);
142 /* ------------------------------------------------------------------------- */
143 ST_FUNC
void tccgen_start(TCCState
*s1
)
145 cur_text_section
= NULL
;
147 anon_sym
= SYM_FIRST_ANOM
;
152 /* define some often used types */
154 char_pointer_type
.t
= VT_BYTE
;
155 mk_pointer(&char_pointer_type
);
157 size_type
.t
= VT_INT
;
159 size_type
.t
= VT_LLONG
;
161 func_old_type
.t
= VT_FUNC
;
162 func_old_type
.ref
= sym_push(SYM_FIELD
, &int_type
, FUNC_CDECL
, FUNC_OLD
);
167 /* file info: full path + filename */
168 section_sym
= put_elf_sym(symtab_section
, 0, 0,
169 ELFW(ST_INFO
)(STB_LOCAL
, STT_SECTION
), 0,
170 text_section
->sh_num
, NULL
);
171 getcwd(buf
, sizeof(buf
));
173 normalize_slashes(buf
);
175 pstrcat(buf
, sizeof(buf
), "/");
176 put_stabs_r(buf
, N_SO
, 0, 0,
177 text_section
->data_offset
, text_section
, section_sym
);
178 put_stabs_r(file
->filename
, N_SO
, 0, 0,
179 text_section
->data_offset
, text_section
, section_sym
);
181 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
182 symbols can be safely used */
183 put_elf_sym(symtab_section
, 0, 0,
184 ELFW(ST_INFO
)(STB_LOCAL
, STT_FILE
), 0,
185 SHN_ABS
, file
->filename
);
187 #ifdef TCC_TARGET_ARM
192 ST_FUNC
void tccgen_end(TCCState
*s1
)
194 gen_inline_functions(s1
);
196 /* end of translation unit info */
198 put_stabs_r(NULL
, N_SO
, 0, 0,
199 text_section
->data_offset
, text_section
, section_sym
);
203 /* ------------------------------------------------------------------------- */
204 /* update sym->c so that it points to an external symbol in section
205 'section' with value 'value' */
207 ST_FUNC
void put_extern_sym2(Sym
*sym
, Section
*section
,
208 addr_t value
, unsigned long size
,
209 int can_add_underscore
)
211 int sym_type
, sym_bind
, sh_num
, info
, other
;
216 #ifdef CONFIG_TCC_BCHECK
222 else if (section
== SECTION_ABS
)
225 sh_num
= section
->sh_num
;
227 if ((sym
->type
.t
& VT_BTYPE
) == VT_FUNC
) {
229 } else if ((sym
->type
.t
& VT_BTYPE
) == VT_VOID
) {
230 sym_type
= STT_NOTYPE
;
232 sym_type
= STT_OBJECT
;
235 if (sym
->type
.t
& VT_STATIC
)
236 sym_bind
= STB_LOCAL
;
238 if (sym
->type
.t
& VT_WEAK
)
241 sym_bind
= STB_GLOBAL
;
245 name
= get_tok_str(sym
->v
, NULL
);
246 #ifdef CONFIG_TCC_BCHECK
247 if (tcc_state
->do_bounds_check
) {
248 /* XXX: avoid doing that for statics ? */
249 /* if bound checking is activated, we change some function
250 names by adding the "__bound" prefix */
253 /* XXX: we rely only on malloc hooks */
266 strcpy(buf
, "__bound_");
276 if (sym
->type
.t
& VT_EXPORT
)
277 other
|= ST_PE_EXPORT
;
278 if (sym_type
== STT_FUNC
&& sym
->type
.ref
) {
279 Sym
*ref
= sym
->type
.ref
;
280 if (ref
->a
.func_export
)
281 other
|= ST_PE_EXPORT
;
282 if (ref
->a
.func_call
== FUNC_STDCALL
&& can_add_underscore
) {
283 sprintf(buf1
, "_%s@%d", name
, ref
->a
.func_args
* PTR_SIZE
);
285 other
|= ST_PE_STDCALL
;
286 can_add_underscore
= 0;
289 if (find_elf_sym(tcc_state
->dynsymtab_section
, name
))
290 other
|= ST_PE_IMPORT
;
291 if (sym
->type
.t
& VT_IMPORT
)
292 other
|= ST_PE_IMPORT
;
295 if (! (sym
->type
.t
& VT_STATIC
))
296 other
= (sym
->type
.t
& VT_VIS_MASK
) >> VT_VIS_SHIFT
;
298 if (tcc_state
->leading_underscore
&& can_add_underscore
) {
300 pstrcpy(buf1
+ 1, sizeof(buf1
) - 1, name
);
303 if (sym
->asm_label
) {
304 name
= get_tok_str(sym
->asm_label
, NULL
);
306 info
= ELFW(ST_INFO
)(sym_bind
, sym_type
);
307 sym
->c
= set_elf_sym(symtab_section
, value
, size
, info
, other
, sh_num
, name
);
309 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
310 esym
->st_value
= value
;
311 esym
->st_size
= size
;
312 esym
->st_shndx
= sh_num
;
316 ST_FUNC
void put_extern_sym(Sym
*sym
, Section
*section
,
317 addr_t value
, unsigned long size
)
319 put_extern_sym2(sym
, section
, value
, size
, 1);
322 /* add a new relocation entry to symbol 'sym' in section 's' */
323 ST_FUNC
void greloca(Section
*s
, Sym
*sym
, unsigned long offset
, int type
,
328 if (nocode_wanted
&& s
== cur_text_section
)
333 put_extern_sym(sym
, NULL
, 0, 0);
337 /* now we can add ELF relocation info */
338 put_elf_reloca(symtab_section
, s
, offset
, type
, c
, addend
);
341 ST_FUNC
void greloc(Section
*s
, Sym
*sym
, unsigned long offset
, int type
)
343 greloca(s
, sym
, offset
, type
, 0);
346 /* ------------------------------------------------------------------------- */
347 /* symbol allocator */
348 static Sym
*__sym_malloc(void)
350 Sym
*sym_pool
, *sym
, *last_sym
;
353 sym_pool
= tcc_malloc(SYM_POOL_NB
* sizeof(Sym
));
354 dynarray_add(&sym_pools
, &nb_sym_pools
, sym_pool
);
356 last_sym
= sym_free_first
;
358 for(i
= 0; i
< SYM_POOL_NB
; i
++) {
359 sym
->next
= last_sym
;
363 sym_free_first
= last_sym
;
367 static inline Sym
*sym_malloc(void)
371 sym
= sym_free_first
;
373 sym
= __sym_malloc();
374 sym_free_first
= sym
->next
;
377 sym
= tcc_malloc(sizeof(Sym
));
382 ST_INLN
void sym_free(Sym
*sym
)
385 sym
->next
= sym_free_first
;
386 sym_free_first
= sym
;
392 /* push, without hashing */
393 ST_FUNC Sym
*sym_push2(Sym
**ps
, int v
, int t
, long c
)
413 /* find a symbol and return its associated structure. 's' is the top
414 of the symbol stack */
415 ST_FUNC Sym
*sym_find2(Sym
*s
, int v
)
427 /* structure lookup */
428 ST_INLN Sym
*struct_find(int v
)
431 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
433 return table_ident
[v
]->sym_struct
;
436 /* find an identifier */
437 ST_INLN Sym
*sym_find(int v
)
440 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
442 return table_ident
[v
]->sym_identifier
;
445 /* push a given symbol on the symbol stack */
446 ST_FUNC Sym
*sym_push(int v
, CType
*type
, int r
, long c
)
455 s
= sym_push2(ps
, v
, type
->t
, c
);
456 s
->type
.ref
= type
->ref
;
458 /* don't record fields or anonymous symbols */
460 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
461 /* record symbol in token array */
462 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
464 ps
= &ts
->sym_struct
;
466 ps
= &ts
->sym_identifier
;
469 s
->scope
= local_scope
;
470 if (s
->prev_tok
&& s
->prev_tok
->scope
== s
->scope
)
471 tcc_error("redeclaration of '%s'",
472 get_tok_str(v
& ~SYM_STRUCT
, NULL
));
477 /* push a global identifier */
478 ST_FUNC Sym
*global_identifier_push(int v
, int t
, long c
)
481 s
= sym_push2(&global_stack
, v
, t
, c
);
482 /* don't record anonymous symbol */
483 if (v
< SYM_FIRST_ANOM
) {
484 ps
= &table_ident
[v
- TOK_IDENT
]->sym_identifier
;
485 /* modify the top most local identifier, so that
486 sym_identifier will point to 's' when popped */
488 ps
= &(*ps
)->prev_tok
;
495 /* pop symbols until top reaches 'b'. If KEEP is non-zero don't really
496 pop them yet from the list, but do remove them from the token array. */
497 ST_FUNC
void sym_pop(Sym
**ptop
, Sym
*b
, int keep
)
507 /* remove symbol in token array */
509 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
510 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
512 ps
= &ts
->sym_struct
;
514 ps
= &ts
->sym_identifier
;
525 static void weaken_symbol(Sym
*sym
)
527 sym
->type
.t
|= VT_WEAK
;
532 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
533 esym_type
= ELFW(ST_TYPE
)(esym
->st_info
);
534 esym
->st_info
= ELFW(ST_INFO
)(STB_WEAK
, esym_type
);
538 static void apply_visibility(Sym
*sym
, CType
*type
)
540 int vis
= sym
->type
.t
& VT_VIS_MASK
;
541 int vis2
= type
->t
& VT_VIS_MASK
;
542 if (vis
== (STV_DEFAULT
<< VT_VIS_SHIFT
))
544 else if (vis2
== (STV_DEFAULT
<< VT_VIS_SHIFT
))
547 vis
= (vis
< vis2
) ? vis
: vis2
;
548 sym
->type
.t
&= ~VT_VIS_MASK
;
554 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
555 vis
>>= VT_VIS_SHIFT
;
556 esym
->st_other
= (esym
->st_other
& ~ELFW(ST_VISIBILITY
)(-1)) | vis
;
560 /* ------------------------------------------------------------------------- */
562 ST_FUNC
void swap(int *p
, int *q
)
570 static void vsetc(CType
*type
, int r
, CValue
*vc
)
574 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
575 tcc_error("memory full (vstack)");
576 /* cannot let cpu flags if other instruction are generated. Also
577 avoid leaving VT_JMP anywhere except on the top of the stack
578 because it would complicate the code generator. */
579 if (vtop
>= vstack
) {
580 v
= vtop
->r
& VT_VALMASK
;
581 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
592 /* push constant of type "type" with useless value */
593 ST_FUNC
void vpush(CType
*type
)
596 vsetc(type
, VT_CONST
, &cval
);
599 /* push integer constant */
600 ST_FUNC
void vpushi(int v
)
604 vsetc(&int_type
, VT_CONST
, &cval
);
607 /* push a pointer sized constant */
608 static void vpushs(addr_t v
)
612 vsetc(&size_type
, VT_CONST
, &cval
);
615 /* push arbitrary 64bit constant */
616 ST_FUNC
void vpush64(int ty
, unsigned long long v
)
623 vsetc(&ctype
, VT_CONST
, &cval
);
626 /* push long long constant */
627 static inline void vpushll(long long v
)
629 vpush64(VT_LLONG
, v
);
632 /* push a symbol value of TYPE */
633 static inline void vpushsym(CType
*type
, Sym
*sym
)
637 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
641 /* Return a static symbol pointing to a section */
642 ST_FUNC Sym
*get_sym_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
648 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
649 sym
->type
.ref
= type
->ref
;
650 sym
->r
= VT_CONST
| VT_SYM
;
651 put_extern_sym(sym
, sec
, offset
, size
);
655 /* push a reference to a section offset by adding a dummy symbol */
656 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
658 vpushsym(type
, get_sym_ref(type
, sec
, offset
, size
));
661 /* define a new external reference to a symbol 'v' of type 'u' */
662 ST_FUNC Sym
*external_global_sym(int v
, CType
*type
, int r
)
668 /* push forward reference */
669 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
670 s
->type
.ref
= type
->ref
;
671 s
->r
= r
| VT_CONST
| VT_SYM
;
676 /* define a new external reference to a symbol 'v' */
677 static Sym
*external_sym(int v
, CType
*type
, int r
)
683 /* push forward reference */
684 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
685 s
->type
.t
|= VT_EXTERN
;
686 } else if (s
->type
.ref
== func_old_type
.ref
) {
687 s
->type
.ref
= type
->ref
;
688 s
->r
= r
| VT_CONST
| VT_SYM
;
689 s
->type
.t
|= VT_EXTERN
;
690 } else if (!is_compatible_types(&s
->type
, type
)) {
691 tcc_error("incompatible types for redefinition of '%s'",
692 get_tok_str(v
, NULL
));
694 /* Merge some storage attributes. */
695 if (type
->t
& VT_WEAK
)
698 if (type
->t
& VT_VIS_MASK
)
699 apply_visibility(s
, type
);
704 /* push a reference to global symbol v */
705 ST_FUNC
void vpush_global_sym(CType
*type
, int v
)
707 vpushsym(type
, external_global_sym(v
, type
, 0));
710 ST_FUNC
void vset(CType
*type
, int r
, long v
)
715 vsetc(type
, r
, &cval
);
718 static void vseti(int r
, int v
)
726 ST_FUNC
void vswap(void)
729 /* cannot let cpu flags if other instruction are generated. Also
730 avoid leaving VT_JMP anywhere except on the top of the stack
731 because it would complicate the code generator. */
732 if (vtop
>= vstack
) {
733 int v
= vtop
->r
& VT_VALMASK
;
734 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
741 /* XXX: +2% overall speed possible with optimized memswap
743 * memswap(&vtop[0], &vtop[1], sizeof *vtop);
747 ST_FUNC
void vpushv(SValue
*v
)
749 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
750 tcc_error("memory full (vstack)");
755 static void vdup(void)
760 /* save registers up to (vtop - n) stack entry */
761 ST_FUNC
void save_regs(int n
)
764 for(p
= vstack
, p1
= vtop
- n
; p
<= p1
; p
++)
768 /* save r to the memory stack, and mark it as being free */
769 ST_FUNC
void save_reg(int r
)
771 save_reg_upstack(r
, 0);
774 /* save r to the memory stack, and mark it as being free,
775 if seen up to (vtop - n) stack entry */
776 ST_FUNC
void save_reg_upstack(int r
, int n
)
778 int l
, saved
, size
, align
;
782 if ((r
&= VT_VALMASK
) >= VT_CONST
)
787 /* modify all stack values */
790 for(p
= vstack
, p1
= vtop
- n
; p
<= p1
; p
++) {
791 if ((p
->r
& VT_VALMASK
) == r
||
792 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
793 /* must save value on stack if not already done */
795 /* NOTE: must reload 'r' because r might be equal to r2 */
796 r
= p
->r
& VT_VALMASK
;
797 /* store register in the stack */
799 if ((p
->r
& VT_LVAL
) ||
800 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
801 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
802 type
= &char_pointer_type
;
806 size
= type_size(type
, &align
);
807 loc
= (loc
- size
) & -align
;
809 sv
.r
= VT_LOCAL
| VT_LVAL
;
812 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
813 /* x86 specific: need to pop fp register ST0 if saved */
815 o(0xd8dd); /* fstp %st(0) */
818 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
819 /* special long long case */
820 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
828 /* mark that stack entry as being saved on the stack */
829 if (p
->r
& VT_LVAL
) {
830 /* also clear the bounded flag because the
831 relocation address of the function was stored in
833 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
835 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
843 #ifdef TCC_TARGET_ARM
844 /* find a register of class 'rc2' with at most one reference on stack.
845 * If none, call get_reg(rc) */
846 ST_FUNC
int get_reg_ex(int rc
, int rc2
)
851 for(r
=0;r
<NB_REGS
;r
++) {
852 if (reg_classes
[r
] & rc2
) {
855 for(p
= vstack
; p
<= vtop
; p
++) {
856 if ((p
->r
& VT_VALMASK
) == r
||
857 (p
->r2
& VT_VALMASK
) == r
)
868 /* find a free register of class 'rc'. If none, save one register */
869 ST_FUNC
int get_reg(int rc
)
874 /* find a free register */
875 for(r
=0;r
<NB_REGS
;r
++) {
876 if (reg_classes
[r
] & rc
) {
879 for(p
=vstack
;p
<=vtop
;p
++) {
880 if ((p
->r
& VT_VALMASK
) == r
||
881 (p
->r2
& VT_VALMASK
) == r
)
889 /* no register left : free the first one on the stack (VERY
890 IMPORTANT to start from the bottom to ensure that we don't
891 spill registers used in gen_opi()) */
892 for(p
=vstack
;p
<=vtop
;p
++) {
893 /* look at second register (if long long) */
894 r
= p
->r2
& VT_VALMASK
;
895 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
897 r
= p
->r
& VT_VALMASK
;
898 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
904 /* Should never comes here */
908 /* move register 's' (of type 't') to 'r', and flush previous value of r to memory
910 static void move_reg(int r
, int s
, int t
)
924 /* get address of vtop (vtop MUST BE an lvalue) */
925 ST_FUNC
void gaddrof(void)
927 if (vtop
->r
& VT_REF
)
930 /* tricky: if saved lvalue, then we can go back to lvalue */
931 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
932 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
937 #ifdef CONFIG_TCC_BCHECK
938 /* generate lvalue bound code */
939 static void gbound(void)
944 vtop
->r
&= ~VT_MUSTBOUND
;
945 /* if lvalue, then use checking code before dereferencing */
946 if (vtop
->r
& VT_LVAL
) {
947 /* if not VT_BOUNDED value, then make one */
948 if (!(vtop
->r
& VT_BOUNDED
)) {
949 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
950 /* must save type because we must set it to int to get pointer */
952 vtop
->type
.t
= VT_PTR
;
955 gen_bounded_ptr_add();
956 vtop
->r
|= lval_type
;
959 /* then check for dereferencing */
960 gen_bounded_ptr_deref();
965 /* store vtop a register belonging to class 'rc'. lvalues are
966 converted to values. Cannot be used if cannot be converted to
967 register value (such as structures). */
968 ST_FUNC
int gv(int rc
)
970 int r
, bit_pos
, bit_size
, size
, align
, i
;
973 /* NOTE: get_reg can modify vstack[] */
974 if (vtop
->type
.t
& VT_BITFIELD
) {
977 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
978 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
979 /* remove bit field info to avoid loops */
980 vtop
->type
.t
&= ~VT_BITFIELD
& ((1 << VT_STRUCT_SHIFT
) - 1);
981 /* cast to int to propagate signedness in following ops */
982 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
987 if((vtop
->type
.t
& VT_UNSIGNED
) ||
988 (vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
989 type
.t
|= VT_UNSIGNED
;
991 /* generate shifts */
992 vpushi(bits
- (bit_pos
+ bit_size
));
994 vpushi(bits
- bit_size
);
995 /* NOTE: transformed to SHR if unsigned */
999 if (is_float(vtop
->type
.t
) &&
1000 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
1003 unsigned long offset
;
1004 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
1008 /* XXX: unify with initializers handling ? */
1009 /* CPUs usually cannot use float constants, so we store them
1010 generically in data segment */
1011 size
= type_size(&vtop
->type
, &align
);
1012 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
1013 data_section
->data_offset
= offset
;
1014 /* XXX: not portable yet */
1015 #if defined(__i386__) || defined(__x86_64__)
1016 /* Zero pad x87 tenbyte long doubles */
1017 if (size
== LDOUBLE_SIZE
) {
1018 vtop
->c
.tab
[2] &= 0xffff;
1019 #if LDOUBLE_SIZE == 16
1024 ptr
= section_ptr_add(data_section
, size
);
1026 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
1030 ptr
[i
] = vtop
->c
.tab
[size
-1-i
];
1034 ptr
[i
] = vtop
->c
.tab
[i
];
1035 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
1036 vtop
->r
|= VT_LVAL
| VT_SYM
;
1040 #ifdef CONFIG_TCC_BCHECK
1041 if (vtop
->r
& VT_MUSTBOUND
)
1045 r
= vtop
->r
& VT_VALMASK
;
1046 rc2
= (rc
& RC_FLOAT
) ? RC_FLOAT
: RC_INT
;
1047 #ifndef TCC_TARGET_ARM64
1050 #ifdef TCC_TARGET_X86_64
1051 else if (rc
== RC_FRET
)
1056 /* need to reload if:
1058 - lvalue (need to dereference pointer)
1059 - already a register, but not in the right class */
1061 || (vtop
->r
& VT_LVAL
)
1062 || !(reg_classes
[r
] & rc
)
1063 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1064 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
1065 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
&& !(reg_classes
[vtop
->r2
] & rc2
))
1067 || ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
1072 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1073 if (((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
)) {
1074 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
1076 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
1077 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
1078 unsigned long long ll
;
1080 int r2
, original_type
;
1081 original_type
= vtop
->type
.t
;
1082 /* two register type load : expand to two words
1084 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
1085 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
1088 vtop
->c
.i
= ll
; /* first word */
1090 vtop
->r
= r
; /* save register value */
1091 vpushi(ll
>> 32); /* second word */
1094 if (vtop
->r
& VT_LVAL
) {
1095 /* We do not want to modifier the long long
1096 pointer here, so the safest (and less
1097 efficient) is to save all the other registers
1098 in the stack. XXX: totally inefficient. */
1102 /* lvalue_save: save only if used further down the stack */
1103 save_reg_upstack(vtop
->r
, 1);
1105 /* load from memory */
1106 vtop
->type
.t
= load_type
;
1109 vtop
[-1].r
= r
; /* save register value */
1110 /* increment pointer to get second word */
1111 vtop
->type
.t
= addr_type
;
1116 vtop
->type
.t
= load_type
;
1118 /* move registers */
1121 vtop
[-1].r
= r
; /* save register value */
1122 vtop
->r
= vtop
[-1].r2
;
1124 /* Allocate second register. Here we rely on the fact that
1125 get_reg() tries first to free r2 of an SValue. */
1129 /* write second register */
1131 vtop
->type
.t
= original_type
;
1132 } else if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
1134 /* lvalue of scalar type : need to use lvalue type
1135 because of possible cast */
1138 /* compute memory access type */
1139 if (vtop
->r
& VT_REF
)
1140 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1145 else if (vtop
->r
& VT_LVAL_BYTE
)
1147 else if (vtop
->r
& VT_LVAL_SHORT
)
1149 if (vtop
->r
& VT_LVAL_UNSIGNED
)
1153 /* restore wanted type */
1156 /* one register type load */
1161 #ifdef TCC_TARGET_C67
1162 /* uses register pairs for doubles */
1163 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
1170 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
1171 ST_FUNC
void gv2(int rc1
, int rc2
)
1175 /* generate more generic register first. But VT_JMP or VT_CMP
1176 values must be generated first in all cases to avoid possible
1178 v
= vtop
[0].r
& VT_VALMASK
;
1179 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
1184 /* test if reload is needed for first register */
1185 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
1195 /* test if reload is needed for first register */
1196 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
1202 #ifndef TCC_TARGET_ARM64
1203 /* wrapper around RC_FRET to return a register by type */
1204 static int rc_fret(int t
)
1206 #ifdef TCC_TARGET_X86_64
1207 if (t
== VT_LDOUBLE
) {
1215 /* wrapper around REG_FRET to return a register by type */
1216 static int reg_fret(int t
)
1218 #ifdef TCC_TARGET_X86_64
1219 if (t
== VT_LDOUBLE
) {
1226 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
1227 /* expand 64bit on stack in two ints */
1228 static void lexpand(void)
1231 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
1232 v
= vtop
->r
& (VT_VALMASK
| VT_LVAL
);
1233 if (v
== VT_CONST
) {
1236 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
1242 vtop
[0].r
= vtop
[-1].r2
;
1243 vtop
[0].r2
= vtop
[-1].r2
= VT_CONST
;
1245 vtop
[0].type
.t
= vtop
[-1].type
.t
= VT_INT
| u
;
1249 #ifdef TCC_TARGET_ARM
1250 /* expand long long on stack */
1251 ST_FUNC
void lexpand_nr(void)
1255 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
1257 vtop
->r2
= VT_CONST
;
1258 vtop
->type
.t
= VT_INT
| u
;
1259 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
1260 if (v
== VT_CONST
) {
1261 vtop
[-1].c
.i
= vtop
->c
.i
;
1262 vtop
->c
.i
= vtop
->c
.i
>> 32;
1264 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
1266 vtop
->r
= vtop
[-1].r
;
1267 } else if (v
> VT_CONST
) {
1271 vtop
->r
= vtop
[-1].r2
;
1272 vtop
[-1].r2
= VT_CONST
;
1273 vtop
[-1].type
.t
= VT_INT
| u
;
1277 #if !defined(TCC_TARGET_X86_64) && !defined(TCC_TARGET_ARM64)
1278 /* build a long long from two ints */
1279 static void lbuild(int t
)
1281 gv2(RC_INT
, RC_INT
);
1282 vtop
[-1].r2
= vtop
[0].r
;
1283 vtop
[-1].type
.t
= t
;
1288 /* rotate n first stack elements to the bottom
1289 I1 ... In -> I2 ... In I1 [top is right]
1291 ST_FUNC
void vrotb(int n
)
1297 for(i
=-n
+1;i
!=0;i
++)
1298 vtop
[i
] = vtop
[i
+1];
1302 /* rotate the n elements before entry e towards the top
1303 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
1305 ST_FUNC
void vrote(SValue
*e
, int n
)
1311 for(i
= 0;i
< n
- 1; i
++)
1316 /* rotate n first stack elements to the top
1317 I1 ... In -> In I1 ... I(n-1) [top is right]
1319 ST_FUNC
void vrott(int n
)
1324 /* pop stack value */
1325 ST_FUNC
void vpop(void)
1328 v
= vtop
->r
& VT_VALMASK
;
1329 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1330 /* for x86, we need to pop the FP stack */
1331 if (v
== TREG_ST0
) {
1332 o(0xd8dd); /* fstp %st(0) */
1335 if (v
== VT_JMP
|| v
== VT_JMPI
) {
1336 /* need to put correct jump if && or || without test */
1342 /* convert stack entry to register and duplicate its value in another
1344 static void gv_dup(void)
1350 #if !defined(TCC_TARGET_X86_64) && !defined(TCC_TARGET_ARM64)
1351 if ((t
& VT_BTYPE
) == VT_LLONG
) {
1358 /* stack: H L L1 H1 */
1368 /* duplicate value */
1373 #ifdef TCC_TARGET_X86_64
1374 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1384 load(r1
, &sv
); /* move r to r1 */
1386 /* duplicates value */
1392 /* Generate value test
1394 * Generate a test for any value (jump, comparison and integers) */
1395 ST_FUNC
int gvtst(int inv
, int t
)
1397 int v
= vtop
->r
& VT_VALMASK
;
1398 if (v
!= VT_CMP
&& v
!= VT_JMP
&& v
!= VT_JMPI
) {
1402 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1403 /* constant jmp optimization */
1404 if ((vtop
->c
.i
!= 0) != inv
)
1409 return gtst(inv
, t
);
1412 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
1413 /* generate CPU independent (unsigned) long long operations */
1414 static void gen_opl(int op
)
1416 int t
, a
, b
, op1
, c
, i
;
1418 unsigned short reg_iret
= REG_IRET
;
1419 unsigned short reg_lret
= REG_LRET
;
1425 func
= TOK___divdi3
;
1428 func
= TOK___udivdi3
;
1431 func
= TOK___moddi3
;
1434 func
= TOK___umoddi3
;
1441 /* call generic long long function */
1442 vpush_global_sym(&func_old_type
, func
);
1447 vtop
->r2
= reg_lret
;
1455 //pv("gen_opl A",0,2);
1461 /* stack: L1 H1 L2 H2 */
1466 vtop
[-2] = vtop
[-3];
1469 /* stack: H1 H2 L1 L2 */
1470 //pv("gen_opl B",0,4);
1476 /* stack: H1 H2 L1 L2 ML MH */
1479 /* stack: ML MH H1 H2 L1 L2 */
1483 /* stack: ML MH H1 L2 H2 L1 */
1488 /* stack: ML MH M1 M2 */
1491 } else if (op
== '+' || op
== '-') {
1492 /* XXX: add non carry method too (for MIPS or alpha) */
1498 /* stack: H1 H2 (L1 op L2) */
1501 gen_op(op1
+ 1); /* TOK_xxxC2 */
1504 /* stack: H1 H2 (L1 op L2) */
1507 /* stack: (L1 op L2) H1 H2 */
1509 /* stack: (L1 op L2) (H1 op H2) */
1517 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1518 t
= vtop
[-1].type
.t
;
1522 /* stack: L H shift */
1524 /* constant: simpler */
1525 /* NOTE: all comments are for SHL. the other cases are
1526 done by swaping words */
1537 if (op
!= TOK_SAR
) {
1570 /* XXX: should provide a faster fallback on x86 ? */
1573 func
= TOK___ashrdi3
;
1576 func
= TOK___lshrdi3
;
1579 func
= TOK___ashldi3
;
1585 /* compare operations */
1591 /* stack: L1 H1 L2 H2 */
1593 vtop
[-1] = vtop
[-2];
1595 /* stack: L1 L2 H1 H2 */
1598 /* when values are equal, we need to compare low words. since
1599 the jump is inverted, we invert the test too. */
1602 else if (op1
== TOK_GT
)
1604 else if (op1
== TOK_ULT
)
1606 else if (op1
== TOK_UGT
)
1611 if (op1
!= TOK_NE
) {
1615 /* generate non equal test */
1616 /* XXX: NOT PORTABLE yet */
1620 #if defined(TCC_TARGET_I386)
1621 b
= gjmp2(0x850f, 0);
1622 #elif defined(TCC_TARGET_ARM)
1624 o(0x1A000000 | encbranch(ind
, 0, 1));
1625 #elif defined(TCC_TARGET_C67) || defined(TCC_TARGET_ARM64)
1626 tcc_error("not implemented");
1628 #error not supported
1632 /* compare low. Always unsigned */
1636 else if (op1
== TOK_LE
)
1638 else if (op1
== TOK_GT
)
1640 else if (op1
== TOK_GE
)
1651 static uint64_t gen_opic_sdiv(uint64_t a
, uint64_t b
)
1653 uint64_t x
= (a
>> 63 ? -a
: a
) / (b
>> 63 ? -b
: b
);
1654 return (a
^ b
) >> 63 ? -x
: x
;
1657 static int gen_opic_lt(uint64_t a
, uint64_t b
)
1659 return (a
^ (uint64_t)1 << 63) < (b
^ (uint64_t)1 << 63);
1662 /* handle integer constant optimizations and various machine
1664 static void gen_opic(int op
)
1666 SValue
*v1
= vtop
- 1;
1668 int t1
= v1
->type
.t
& VT_BTYPE
;
1669 int t2
= v2
->type
.t
& VT_BTYPE
;
1670 int c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1671 int c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1672 uint64_t l1
= c1
? v1
->c
.i
: 0;
1673 uint64_t l2
= c2
? v2
->c
.i
: 0;
1674 int shm
= (t1
== VT_LLONG
) ? 63 : 31;
1676 if (t1
!= VT_LLONG
&& (PTR_SIZE
!= 8 || t1
!= VT_PTR
))
1677 l1
= ((uint32_t)l1
|
1678 (v1
->type
.t
& VT_UNSIGNED
? 0 : -(l1
& 0x80000000)));
1679 if (t2
!= VT_LLONG
&& (PTR_SIZE
!= 8 || t2
!= VT_PTR
))
1680 l2
= ((uint32_t)l2
|
1681 (v2
->type
.t
& VT_UNSIGNED
? 0 : -(l2
& 0x80000000)));
1685 case '+': l1
+= l2
; break;
1686 case '-': l1
-= l2
; break;
1687 case '&': l1
&= l2
; break;
1688 case '^': l1
^= l2
; break;
1689 case '|': l1
|= l2
; break;
1690 case '*': l1
*= l2
; break;
1697 /* if division by zero, generate explicit division */
1700 tcc_error("division by zero in constant");
1704 default: l1
= gen_opic_sdiv(l1
, l2
); break;
1705 case '%': l1
= l1
- l2
* gen_opic_sdiv(l1
, l2
); break;
1706 case TOK_UDIV
: l1
= l1
/ l2
; break;
1707 case TOK_UMOD
: l1
= l1
% l2
; break;
1710 case TOK_SHL
: l1
<<= (l2
& shm
); break;
1711 case TOK_SHR
: l1
>>= (l2
& shm
); break;
1713 l1
= (l1
>> 63) ? ~(~l1
>> (l2
& shm
)) : l1
>> (l2
& shm
);
1716 case TOK_ULT
: l1
= l1
< l2
; break;
1717 case TOK_UGE
: l1
= l1
>= l2
; break;
1718 case TOK_EQ
: l1
= l1
== l2
; break;
1719 case TOK_NE
: l1
= l1
!= l2
; break;
1720 case TOK_ULE
: l1
= l1
<= l2
; break;
1721 case TOK_UGT
: l1
= l1
> l2
; break;
1722 case TOK_LT
: l1
= gen_opic_lt(l1
, l2
); break;
1723 case TOK_GE
: l1
= !gen_opic_lt(l1
, l2
); break;
1724 case TOK_LE
: l1
= !gen_opic_lt(l2
, l1
); break;
1725 case TOK_GT
: l1
= gen_opic_lt(l2
, l1
); break;
1727 case TOK_LAND
: l1
= l1
&& l2
; break;
1728 case TOK_LOR
: l1
= l1
|| l2
; break;
1732 if (t1
!= VT_LLONG
&& (PTR_SIZE
!= 8 || t1
!= VT_PTR
))
1733 l1
= ((uint32_t)l1
|
1734 (v1
->type
.t
& VT_UNSIGNED
? 0 : -(l1
& 0x80000000)));
1738 /* if commutative ops, put c2 as constant */
1739 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1740 op
== '|' || op
== '*')) {
1742 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1743 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1745 if (!const_wanted
&&
1747 (op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
)) ||
1748 (l1
== -1 && op
== TOK_SAR
))) {
1749 /* treat (0 << x), (0 >> x) and (-1 >> x) as constant */
1751 } else if (!const_wanted
&&
1752 c2
&& ((l2
== 0 && (op
== '&' || op
== '*')) ||
1753 (l2
== -1 && op
== '|') ||
1754 (l2
== 0xffffffff && t2
!= VT_LLONG
&& op
== '|') ||
1755 (l2
== 1 && (op
== '%' || op
== TOK_UMOD
)))) {
1756 /* treat (x & 0), (x * 0), (x | -1) and (x % 1) as constant */
1761 } else if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1764 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1765 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1769 /* filter out NOP operations like x*1, x-0, x&-1... */
1771 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1772 /* try to use shifts instead of muls or divs */
1773 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1782 else if (op
== TOK_PDIV
)
1788 } else if (c2
&& (op
== '+' || op
== '-') &&
1789 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
1790 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1791 /* symbol + constant case */
1795 /* The backends can't always deal with addends to symbols
1796 larger than +-1<<31. Don't construct such. */
1803 /* call low level op generator */
1804 if (t1
== VT_LLONG
|| t2
== VT_LLONG
||
1805 (PTR_SIZE
== 8 && (t1
== VT_PTR
|| t2
== VT_PTR
)))
1813 /* generate a floating point operation with constant propagation */
1814 static void gen_opif(int op
)
1822 /* currently, we cannot do computations with forward symbols */
1823 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1824 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1826 if (v1
->type
.t
== VT_FLOAT
) {
1829 } else if (v1
->type
.t
== VT_DOUBLE
) {
1837 /* NOTE: we only do constant propagation if finite number (not
1838 NaN or infinity) (ANSI spec) */
1839 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1843 case '+': f1
+= f2
; break;
1844 case '-': f1
-= f2
; break;
1845 case '*': f1
*= f2
; break;
1849 tcc_error("division by zero in constant");
1854 /* XXX: also handles tests ? */
1858 /* XXX: overflow test ? */
1859 if (v1
->type
.t
== VT_FLOAT
) {
1861 } else if (v1
->type
.t
== VT_DOUBLE
) {
1873 static int pointed_size(CType
*type
)
1876 return type_size(pointed_type(type
), &align
);
1879 static void vla_runtime_pointed_size(CType
*type
)
1882 vla_runtime_type_size(pointed_type(type
), &align
);
1885 static inline int is_null_pointer(SValue
*p
)
1887 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1889 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& (uint32_t)p
->c
.i
== 0) ||
1890 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.i
== 0) ||
1891 ((p
->type
.t
& VT_BTYPE
) == VT_PTR
&&
1892 (PTR_SIZE
== 4 ? (uint32_t)p
->c
.i
== 0 : p
->c
.i
== 0));
1895 static inline int is_integer_btype(int bt
)
1897 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
1898 bt
== VT_INT
|| bt
== VT_LLONG
);
1901 /* check types for comparison or subtraction of pointers */
1902 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
1904 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
1907 /* null pointers are accepted for all comparisons as gcc */
1908 if (is_null_pointer(p1
) || is_null_pointer(p2
))
1912 bt1
= type1
->t
& VT_BTYPE
;
1913 bt2
= type2
->t
& VT_BTYPE
;
1914 /* accept comparison between pointer and integer with a warning */
1915 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
1916 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
1917 tcc_warning("comparison between pointer and integer");
1921 /* both must be pointers or implicit function pointers */
1922 if (bt1
== VT_PTR
) {
1923 type1
= pointed_type(type1
);
1924 } else if (bt1
!= VT_FUNC
)
1925 goto invalid_operands
;
1927 if (bt2
== VT_PTR
) {
1928 type2
= pointed_type(type2
);
1929 } else if (bt2
!= VT_FUNC
) {
1931 tcc_error("invalid operands to binary %s", get_tok_str(op
, NULL
));
1933 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1934 (type2
->t
& VT_BTYPE
) == VT_VOID
)
1938 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1939 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1940 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1941 /* gcc-like error if '-' is used */
1943 goto invalid_operands
;
1945 tcc_warning("comparison of distinct pointer types lacks a cast");
1949 /* generic gen_op: handles types problems */
1950 ST_FUNC
void gen_op(int op
)
1952 int u
, t1
, t2
, bt1
, bt2
, t
;
1956 t1
= vtop
[-1].type
.t
;
1957 t2
= vtop
[0].type
.t
;
1958 bt1
= t1
& VT_BTYPE
;
1959 bt2
= t2
& VT_BTYPE
;
1961 if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
1962 tcc_error("operation on a struct");
1963 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
1964 if (bt2
== VT_FUNC
) {
1965 mk_pointer(&vtop
->type
);
1968 if (bt1
== VT_FUNC
) {
1970 mk_pointer(&vtop
->type
);
1975 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
1976 /* at least one operand is a pointer */
1977 /* relationnal op: must be both pointers */
1978 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
1979 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1980 /* pointers are handled are unsigned */
1981 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1982 t
= VT_LLONG
| VT_UNSIGNED
;
1984 t
= VT_INT
| VT_UNSIGNED
;
1988 /* if both pointers, then it must be the '-' op */
1989 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
1991 tcc_error("cannot use pointers here");
1992 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1993 /* XXX: check that types are compatible */
1994 if (vtop
[-1].type
.t
& VT_VLA
) {
1995 vla_runtime_pointed_size(&vtop
[-1].type
);
1997 vpushi(pointed_size(&vtop
[-1].type
));
2001 /* set to integer type */
2002 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2003 vtop
->type
.t
= VT_LLONG
;
2005 vtop
->type
.t
= VT_INT
;
2010 /* exactly one pointer : must be '+' or '-'. */
2011 if (op
!= '-' && op
!= '+')
2012 tcc_error("cannot use pointers here");
2013 /* Put pointer as first operand */
2014 if (bt2
== VT_PTR
) {
2019 if ((vtop
[0].type
.t
& VT_BTYPE
) == VT_LLONG
)
2020 /* XXX: truncate here because gen_opl can't handle ptr + long long */
2021 gen_cast(&int_type
);
2023 type1
= vtop
[-1].type
;
2024 type1
.t
&= ~VT_ARRAY
;
2025 if (vtop
[-1].type
.t
& VT_VLA
)
2026 vla_runtime_pointed_size(&vtop
[-1].type
);
2028 u
= pointed_size(&vtop
[-1].type
);
2030 tcc_error("unknown array element size");
2031 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2034 /* XXX: cast to int ? (long long case) */
2040 /* #ifdef CONFIG_TCC_BCHECK
2041 The main reason to removing this code:
2048 fprintf(stderr, "v+i-j = %p\n", v+i-j);
2049 fprintf(stderr, "v+(i-j) = %p\n", v+(i-j));
2051 When this code is on. then the output looks like
2053 v+(i-j) = 0xbff84000
2055 /* if evaluating constant expression, no code should be
2056 generated, so no bound check */
2057 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
2058 /* if bounded pointers, we generate a special code to
2065 gen_bounded_ptr_add();
2071 /* put again type if gen_opic() swaped operands */
2074 } else if (is_float(bt1
) || is_float(bt2
)) {
2075 /* compute bigger type and do implicit casts */
2076 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
2078 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
2083 /* floats can only be used for a few operations */
2084 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
2085 (op
< TOK_ULT
|| op
> TOK_GT
))
2086 tcc_error("invalid operands for binary operation");
2088 } else if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
) {
2089 t
= bt1
== VT_LLONG
? VT_LLONG
: VT_INT
;
2090 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (t
| VT_UNSIGNED
))
2093 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
2094 /* cast to biggest op */
2096 /* convert to unsigned if it does not fit in a long long */
2097 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
2098 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
2102 /* integer operations */
2104 /* convert to unsigned if it does not fit in an integer */
2105 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
2106 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
2109 /* XXX: currently, some unsigned operations are explicit, so
2110 we modify them here */
2111 if (t
& VT_UNSIGNED
) {
2118 else if (op
== TOK_LT
)
2120 else if (op
== TOK_GT
)
2122 else if (op
== TOK_LE
)
2124 else if (op
== TOK_GE
)
2131 /* special case for shifts and long long: we keep the shift as
2133 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
2140 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
2141 /* relationnal op: the result is an int */
2142 vtop
->type
.t
= VT_INT
;
2147 // Make sure that we have converted to an rvalue:
2148 if (vtop
->r
& VT_LVAL
)
2149 gv(is_float(vtop
->type
.t
& VT_BTYPE
) ? RC_FLOAT
: RC_INT
);
2152 #ifndef TCC_TARGET_ARM
2153 /* generic itof for unsigned long long case */
2154 static void gen_cvt_itof1(int t
)
2156 #ifdef TCC_TARGET_ARM64
2159 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
2160 (VT_LLONG
| VT_UNSIGNED
)) {
2163 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
2164 #if LDOUBLE_SIZE != 8
2165 else if (t
== VT_LDOUBLE
)
2166 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
2169 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
2173 vtop
->r
= reg_fret(t
);
2181 /* generic ftoi for unsigned long long case */
2182 static void gen_cvt_ftoi1(int t
)
2184 #ifdef TCC_TARGET_ARM64
2189 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
2190 /* not handled natively */
2191 st
= vtop
->type
.t
& VT_BTYPE
;
2193 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
2194 #if LDOUBLE_SIZE != 8
2195 else if (st
== VT_LDOUBLE
)
2196 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
2199 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
2204 vtop
->r2
= REG_LRET
;
2211 /* force char or short cast */
2212 static void force_charshort_cast(int t
)
2216 /* XXX: add optimization if lvalue : just change type and offset */
2221 if (t
& VT_UNSIGNED
) {
2222 vpushi((1 << bits
) - 1);
2225 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
)
2231 /* result must be signed or the SAR is converted to an SHL
2232 This was not the case when "t" was a signed short
2233 and the last value on the stack was an unsigned int */
2234 vtop
->type
.t
&= ~VT_UNSIGNED
;
2240 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
2241 static void gen_cast(CType
*type
)
2243 int sbt
, dbt
, sf
, df
, c
, p
;
2245 /* special delayed cast for char/short */
2246 /* XXX: in some cases (multiple cascaded casts), it may still
2248 if (vtop
->r
& VT_MUSTCAST
) {
2249 vtop
->r
&= ~VT_MUSTCAST
;
2250 force_charshort_cast(vtop
->type
.t
);
2253 /* bitfields first get cast to ints */
2254 if (vtop
->type
.t
& VT_BITFIELD
) {
2258 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
2259 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
2264 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
2265 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
2267 /* constant case: we can do it now */
2268 /* XXX: in ISOC, cannot do it if error in convert */
2269 if (sbt
== VT_FLOAT
)
2270 vtop
->c
.ld
= vtop
->c
.f
;
2271 else if (sbt
== VT_DOUBLE
)
2272 vtop
->c
.ld
= vtop
->c
.d
;
2275 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
2276 if ((sbt
& VT_UNSIGNED
) || !(vtop
->c
.i
>> 63))
2277 vtop
->c
.ld
= vtop
->c
.i
;
2279 vtop
->c
.ld
= -(long double)-vtop
->c
.i
;
2281 if ((sbt
& VT_UNSIGNED
) || !(vtop
->c
.i
>> 31))
2282 vtop
->c
.ld
= (uint32_t)vtop
->c
.i
;
2284 vtop
->c
.ld
= -(long double)-(uint32_t)vtop
->c
.i
;
2287 if (dbt
== VT_FLOAT
)
2288 vtop
->c
.f
= (float)vtop
->c
.ld
;
2289 else if (dbt
== VT_DOUBLE
)
2290 vtop
->c
.d
= (double)vtop
->c
.ld
;
2291 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
2292 vtop
->c
.i
= vtop
->c
.ld
;
2293 } else if (sf
&& dbt
== VT_BOOL
) {
2294 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
2297 vtop
->c
.i
= vtop
->c
.ld
;
2298 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
2300 else if (sbt
& VT_UNSIGNED
)
2301 vtop
->c
.i
= (uint32_t)vtop
->c
.i
;
2302 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2303 else if (sbt
== VT_PTR
)
2306 else if (sbt
!= VT_LLONG
)
2307 vtop
->c
.i
= ((uint32_t)vtop
->c
.i
|
2308 -(vtop
->c
.i
& 0x80000000));
2310 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
2312 else if (dbt
== VT_BOOL
)
2313 vtop
->c
.i
= (vtop
->c
.i
!= 0);
2314 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2315 else if (dbt
== VT_PTR
)
2318 else if (dbt
!= VT_LLONG
) {
2319 uint32_t m
= ((dbt
& VT_BTYPE
) == VT_BYTE
? 0xff :
2320 (dbt
& VT_BTYPE
) == VT_SHORT
? 0xffff :
2323 if (!(dbt
& VT_UNSIGNED
))
2324 vtop
->c
.i
|= -(vtop
->c
.i
& ((m
>> 1) + 1));
2327 } else if (p
&& dbt
== VT_BOOL
) {
2331 /* non constant case: generate code */
2333 /* convert from fp to fp */
2336 /* convert int to fp */
2339 /* convert fp to int */
2340 if (dbt
== VT_BOOL
) {
2344 /* we handle char/short/etc... with generic code */
2345 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
2346 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
2350 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
2351 /* additional cast for char/short... */
2356 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
2357 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
2358 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
2359 /* scalar to long long */
2360 /* machine independent conversion */
2362 /* generate high word */
2363 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
2367 if (sbt
== VT_PTR
) {
2368 /* cast from pointer to int before we apply
2369 shift operation, which pointers don't support*/
2370 gen_cast(&int_type
);
2376 /* patch second register */
2377 vtop
[-1].r2
= vtop
->r
;
2381 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
2382 (dbt
& VT_BTYPE
) == VT_PTR
||
2383 (dbt
& VT_BTYPE
) == VT_FUNC
) {
2384 if ((sbt
& VT_BTYPE
) != VT_LLONG
&&
2385 (sbt
& VT_BTYPE
) != VT_PTR
&&
2386 (sbt
& VT_BTYPE
) != VT_FUNC
) {
2387 /* need to convert from 32bit to 64bit */
2389 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
2390 #if defined(TCC_TARGET_ARM64)
2392 #elif defined(TCC_TARGET_X86_64)
2394 /* x86_64 specific: movslq */
2396 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
2403 } else if (dbt
== VT_BOOL
) {
2404 /* scalar to bool */
2407 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
2408 (dbt
& VT_BTYPE
) == VT_SHORT
) {
2409 if (sbt
== VT_PTR
) {
2410 vtop
->type
.t
= VT_INT
;
2411 tcc_warning("nonportable conversion from pointer to char/short");
2413 force_charshort_cast(dbt
);
2414 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
2415 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
2417 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
2418 /* from long long: just take low order word */
2422 /* if lvalue and single word type, nothing to do because
2423 the lvalue already contains the real type size (see
2424 VT_LVAL_xxx constants) */
2428 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
2429 /* if we are casting between pointer types,
2430 we must update the VT_LVAL_xxx size */
2431 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
2432 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
2437 /* return type size as known at compile time. Put alignment at 'a' */
2438 ST_FUNC
int type_size(CType
*type
, int *a
)
2443 bt
= type
->t
& VT_BTYPE
;
2444 if (bt
== VT_STRUCT
) {
2449 } else if (bt
== VT_PTR
) {
2450 if (type
->t
& VT_ARRAY
) {
2454 ts
= type_size(&s
->type
, a
);
2456 if (ts
< 0 && s
->c
< 0)
2464 } else if (bt
== VT_LDOUBLE
) {
2466 return LDOUBLE_SIZE
;
2467 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
2468 #ifdef TCC_TARGET_I386
2469 #ifdef TCC_TARGET_PE
2474 #elif defined(TCC_TARGET_ARM)
2484 } else if (bt
== VT_INT
|| bt
== VT_FLOAT
) {
2487 } else if (bt
== VT_SHORT
) {
2490 } else if (bt
== VT_QLONG
|| bt
== VT_QFLOAT
) {
2493 } else if (bt
== VT_ENUM
) {
2495 /* Enums might be incomplete, so don't just return '4' here. */
2496 return type
->ref
->c
;
2498 /* char, void, function, _Bool */
2504 /* push type size as known at runtime time on top of value stack. Put
2506 ST_FUNC
void vla_runtime_type_size(CType
*type
, int *a
)
2508 if (type
->t
& VT_VLA
) {
2509 type_size(&type
->ref
->type
, a
);
2510 vset(&int_type
, VT_LOCAL
|VT_LVAL
, type
->ref
->c
);
2512 vpushi(type_size(type
, a
));
2516 static void vla_sp_restore(void) {
2517 if (vlas_in_scope
) {
2518 gen_vla_sp_restore(vla_sp_loc
);
2522 static void vla_sp_restore_root(void) {
2523 if (vlas_in_scope
) {
2524 gen_vla_sp_restore(vla_sp_root_loc
);
2528 /* return the pointed type of t */
2529 static inline CType
*pointed_type(CType
*type
)
2531 return &type
->ref
->type
;
2534 /* modify type so that its it is a pointer to type. */
2535 ST_FUNC
void mk_pointer(CType
*type
)
2538 s
= sym_push(SYM_FIELD
, type
, 0, -1);
2539 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
2543 /* compare function types. OLD functions match any new functions */
2544 static int is_compatible_func(CType
*type1
, CType
*type2
)
2550 if (!is_compatible_types(&s1
->type
, &s2
->type
))
2552 /* check func_call */
2553 if (s1
->a
.func_call
!= s2
->a
.func_call
)
2555 /* XXX: not complete */
2556 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
2560 while (s1
!= NULL
) {
2563 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
2573 /* return true if type1 and type2 are the same. If unqualified is
2574 true, qualifiers on the types are ignored.
2576 - enums are not checked as gcc __builtin_types_compatible_p ()
2578 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2582 t1
= type1
->t
& VT_TYPE
;
2583 t2
= type2
->t
& VT_TYPE
;
2585 /* strip qualifiers before comparing */
2586 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2587 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2589 /* Default Vs explicit signedness only matters for char */
2590 if ((t1
& VT_BTYPE
) != VT_BYTE
) {
2594 /* An enum is compatible with (unsigned) int. Ideally we would
2595 store the enums signedness in type->ref.a.<some_bit> and
2596 only accept unsigned enums with unsigned int and vice versa.
2597 But one of our callers (gen_assign_cast) always strips VT_UNSIGNED
2598 from pointer target types, so we can't add it here either. */
2599 if ((t1
& VT_BTYPE
) == VT_ENUM
) {
2601 if (type1
->ref
->a
.unsigned_enum
)
2604 if ((t2
& VT_BTYPE
) == VT_ENUM
) {
2606 if (type2
->ref
->a
.unsigned_enum
)
2609 /* XXX: bitfields ? */
2612 /* test more complicated cases */
2613 bt1
= t1
& VT_BTYPE
;
2614 if (bt1
== VT_PTR
) {
2615 type1
= pointed_type(type1
);
2616 type2
= pointed_type(type2
);
2617 return is_compatible_types(type1
, type2
);
2618 } else if (bt1
== VT_STRUCT
) {
2619 return (type1
->ref
== type2
->ref
);
2620 } else if (bt1
== VT_FUNC
) {
2621 return is_compatible_func(type1
, type2
);
2627 /* return true if type1 and type2 are exactly the same (including
2630 static int is_compatible_types(CType
*type1
, CType
*type2
)
2632 return compare_types(type1
,type2
,0);
2635 /* return true if type1 and type2 are the same (ignoring qualifiers).
2637 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
2639 return compare_types(type1
,type2
,1);
2642 /* print a type. If 'varstr' is not NULL, then the variable is also
2643 printed in the type */
2645 /* XXX: add array and function pointers */
2646 static void type_to_str(char *buf
, int buf_size
,
2647 CType
*type
, const char *varstr
)
2654 t
= type
->t
& VT_TYPE
;
2657 if (t
& VT_CONSTANT
)
2658 pstrcat(buf
, buf_size
, "const ");
2659 if (t
& VT_VOLATILE
)
2660 pstrcat(buf
, buf_size
, "volatile ");
2661 if ((t
& (VT_DEFSIGN
| VT_UNSIGNED
)) == (VT_DEFSIGN
| VT_UNSIGNED
))
2662 pstrcat(buf
, buf_size
, "unsigned ");
2663 else if (t
& VT_DEFSIGN
)
2664 pstrcat(buf
, buf_size
, "signed ");
2694 tstr
= "long double";
2696 pstrcat(buf
, buf_size
, tstr
);
2700 if (bt
== VT_STRUCT
)
2704 pstrcat(buf
, buf_size
, tstr
);
2705 v
= type
->ref
->v
& ~SYM_STRUCT
;
2706 if (v
>= SYM_FIRST_ANOM
)
2707 pstrcat(buf
, buf_size
, "<anonymous>");
2709 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
2713 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
2714 pstrcat(buf
, buf_size
, "(");
2716 while (sa
!= NULL
) {
2717 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
2718 pstrcat(buf
, buf_size
, buf1
);
2721 pstrcat(buf
, buf_size
, ", ");
2723 pstrcat(buf
, buf_size
, ")");
2728 snprintf(buf1
, sizeof(buf1
), "%s[%ld]", varstr
? varstr
: "", s
->c
);
2729 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2732 pstrcpy(buf1
, sizeof(buf1
), "*");
2733 if (t
& VT_CONSTANT
)
2734 pstrcat(buf1
, buf_size
, "const ");
2735 if (t
& VT_VOLATILE
)
2736 pstrcat(buf1
, buf_size
, "volatile ");
2738 pstrcat(buf1
, sizeof(buf1
), varstr
);
2739 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2743 pstrcat(buf
, buf_size
, " ");
2744 pstrcat(buf
, buf_size
, varstr
);
2749 /* verify type compatibility to store vtop in 'dt' type, and generate
2751 static void gen_assign_cast(CType
*dt
)
2753 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
2754 char buf1
[256], buf2
[256];
2757 st
= &vtop
->type
; /* source type */
2758 dbt
= dt
->t
& VT_BTYPE
;
2759 sbt
= st
->t
& VT_BTYPE
;
2760 if (sbt
== VT_VOID
|| dbt
== VT_VOID
) {
2761 if (sbt
== VT_VOID
&& dbt
== VT_VOID
)
2763 It is Ok if both are void
2769 gcc accepts this program
2772 tcc_error("cannot cast from/to void");
2774 if (dt
->t
& VT_CONSTANT
)
2775 tcc_warning("assignment of read-only location");
2778 /* special cases for pointers */
2779 /* '0' can also be a pointer */
2780 if (is_null_pointer(vtop
))
2782 /* accept implicit pointer to integer cast with warning */
2783 if (is_integer_btype(sbt
)) {
2784 tcc_warning("assignment makes pointer from integer without a cast");
2787 type1
= pointed_type(dt
);
2788 /* a function is implicitely a function pointer */
2789 if (sbt
== VT_FUNC
) {
2790 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
2791 !is_compatible_types(pointed_type(dt
), st
))
2792 tcc_warning("assignment from incompatible pointer type");
2797 type2
= pointed_type(st
);
2798 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2799 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
2800 /* void * can match anything */
2802 /* exact type match, except for qualifiers */
2805 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2806 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2807 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2808 /* Like GCC don't warn by default for merely changes
2809 in pointer target signedness. Do warn for different
2810 base types, though, in particular for unsigned enums
2811 and signed int targets. */
2812 if ((tmp_type1
.t
& (VT_DEFSIGN
| VT_UNSIGNED
)) !=
2813 (tmp_type2
.t
& (VT_DEFSIGN
| VT_UNSIGNED
)) &&
2814 (tmp_type1
.t
& VT_BTYPE
) == (tmp_type2
.t
& VT_BTYPE
))
2817 tcc_warning("assignment from incompatible pointer type");
2820 /* check const and volatile */
2821 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
2822 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
2823 tcc_warning("assignment discards qualifiers from pointer target type");
2829 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
2830 tcc_warning("assignment makes integer from pointer without a cast");
2831 } else if (sbt
== VT_STRUCT
) {
2832 goto case_VT_STRUCT
;
2834 /* XXX: more tests */
2840 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2841 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2842 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2844 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
2845 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
2846 tcc_error("cannot cast '%s' to '%s'", buf1
, buf2
);
2854 /* store vtop in lvalue pushed on stack */
2855 ST_FUNC
void vstore(void)
2857 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
2859 ft
= vtop
[-1].type
.t
;
2860 sbt
= vtop
->type
.t
& VT_BTYPE
;
2861 dbt
= ft
& VT_BTYPE
;
2862 if ((((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2863 (sbt
== VT_INT
&& dbt
== VT_SHORT
))
2864 && !(vtop
->type
.t
& VT_BITFIELD
)) {
2865 /* optimize char/short casts */
2866 delayed_cast
= VT_MUSTCAST
;
2867 vtop
->type
.t
= (ft
& VT_TYPE
& ~VT_BITFIELD
&
2868 ((1 << VT_STRUCT_SHIFT
) - 1));
2869 /* XXX: factorize */
2870 if (ft
& VT_CONSTANT
)
2871 tcc_warning("assignment of read-only location");
2874 if (!(ft
& VT_BITFIELD
))
2875 gen_assign_cast(&vtop
[-1].type
);
2878 if (sbt
== VT_STRUCT
) {
2879 /* if structure, only generate pointer */
2880 /* structure assignment : generate memcpy */
2881 /* XXX: optimize if small size */
2882 size
= type_size(&vtop
->type
, &align
);
2886 vtop
->type
.t
= VT_PTR
;
2889 /* address of memcpy() */
2892 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2893 else if(!(align
& 3))
2894 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2897 /* Use memmove, rather than memcpy, as dest and src may be same: */
2898 vpush_global_sym(&func_old_type
, TOK_memmove
);
2903 vtop
->type
.t
= VT_PTR
;
2909 /* leave source on stack */
2910 } else if (ft
& VT_BITFIELD
) {
2911 /* bitfield store handling */
2913 /* save lvalue as expression result (example: s.b = s.a = n;) */
2914 vdup(), vtop
[-1] = vtop
[-2];
2916 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2917 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2918 /* remove bit field info to avoid loops */
2919 vtop
[-1].type
.t
= ft
& ~VT_BITFIELD
& ((1 << VT_STRUCT_SHIFT
) - 1);
2921 if((ft
& VT_BTYPE
) == VT_BOOL
) {
2922 gen_cast(&vtop
[-1].type
);
2923 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2926 /* duplicate destination */
2928 vtop
[-1] = vtop
[-2];
2930 /* mask and shift source */
2931 if((ft
& VT_BTYPE
) != VT_BOOL
) {
2932 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2933 vpushll((1ULL << bit_size
) - 1ULL);
2935 vpushi((1 << bit_size
) - 1);
2941 /* load destination, mask and or with source */
2943 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2944 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2946 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2952 /* ... and discard */
2956 #ifdef CONFIG_TCC_BCHECK
2957 /* bound check case */
2958 if (vtop
[-1].r
& VT_MUSTBOUND
) {
2967 #ifdef TCC_TARGET_X86_64
2968 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
2970 } else if ((ft
& VT_BTYPE
) == VT_QFLOAT
) {
2975 r
= gv(rc
); /* generate value */
2976 /* if lvalue was saved on stack, must read it */
2977 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
2979 t
= get_reg(RC_INT
);
2980 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2985 sv
.r
= VT_LOCAL
| VT_LVAL
;
2986 sv
.c
.i
= vtop
[-1].c
.i
;
2988 vtop
[-1].r
= t
| VT_LVAL
;
2990 /* two word case handling : store second register at word + 4 (or +8 for x86-64) */
2991 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2992 if (((ft
& VT_BTYPE
) == VT_QLONG
) || ((ft
& VT_BTYPE
) == VT_QFLOAT
)) {
2993 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
2995 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
2996 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
2998 vtop
[-1].type
.t
= load_type
;
3001 /* convert to int to increment easily */
3002 vtop
->type
.t
= addr_type
;
3008 vtop
[-1].type
.t
= load_type
;
3009 /* XXX: it works because r2 is spilled last ! */
3010 store(vtop
->r2
, vtop
- 1);
3016 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
3017 vtop
->r
|= delayed_cast
;
3021 /* post defines POST/PRE add. c is the token ++ or -- */
3022 ST_FUNC
void inc(int post
, int c
)
3025 vdup(); /* save lvalue */
3027 gv_dup(); /* duplicate value */
3032 vpushi(c
- TOK_MID
);
3034 vstore(); /* store value */
3036 vpop(); /* if post op, return saved value */
3039 ST_FUNC
void parse_mult_str (CString
*astr
, const char *msg
)
3041 /* read the string */
3045 while (tok
== TOK_STR
) {
3046 /* XXX: add \0 handling too ? */
3047 cstr_cat(astr
, tokc
.str
.data
, -1);
3050 cstr_ccat(astr
, '\0');
3053 /* If I is >= 1 and a power of two, returns log2(i)+1.
3054 If I is 0 returns 0. */
3055 static int exact_log2p1(int i
)
3060 for (ret
= 1; i
>= 1 << 8; ret
+= 8)
3071 /* Parse GNUC __attribute__ extension. Currently, the following
3072 extensions are recognized:
3073 - aligned(n) : set data/function alignment.
3074 - packed : force data alignment to 1
3075 - section(x) : generate data/code in this section.
3076 - unused : currently ignored, but may be used someday.
3077 - regparm(n) : pass function parameters in registers (i386 only)
3079 static void parse_attribute(AttributeDef
*ad
)
3084 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
3088 while (tok
!= ')') {
3089 if (tok
< TOK_IDENT
)
3090 expect("attribute name");
3097 parse_mult_str(&astr
, "section name");
3098 ad
->section
= find_section(tcc_state
, (char *)astr
.data
);
3105 parse_mult_str(&astr
, "alias(\"target\")");
3106 ad
->alias_target
= /* save string as token, for later */
3107 tok_alloc((char*)astr
.data
, astr
.size
-1)->tok
;
3111 case TOK_VISIBILITY1
:
3112 case TOK_VISIBILITY2
:
3114 parse_mult_str(&astr
,
3115 "visibility(\"default|hidden|internal|protected\")");
3116 if (!strcmp (astr
.data
, "default"))
3117 ad
->a
.visibility
= STV_DEFAULT
;
3118 else if (!strcmp (astr
.data
, "hidden"))
3119 ad
->a
.visibility
= STV_HIDDEN
;
3120 else if (!strcmp (astr
.data
, "internal"))
3121 ad
->a
.visibility
= STV_INTERNAL
;
3122 else if (!strcmp (astr
.data
, "protected"))
3123 ad
->a
.visibility
= STV_PROTECTED
;
3125 expect("visibility(\"default|hidden|internal|protected\")");
3134 if (n
<= 0 || (n
& (n
- 1)) != 0)
3135 tcc_error("alignment must be a positive power of two");
3140 ad
->a
.aligned
= exact_log2p1(n
);
3141 if (n
!= 1 << (ad
->a
.aligned
- 1))
3142 tcc_error("alignment of %d is larger than implemented", n
);
3154 /* currently, no need to handle it because tcc does not
3155 track unused objects */
3159 /* currently, no need to handle it because tcc does not
3160 track unused objects */
3165 ad
->a
.func_call
= FUNC_CDECL
;
3170 ad
->a
.func_call
= FUNC_STDCALL
;
3172 #ifdef TCC_TARGET_I386
3182 ad
->a
.func_call
= FUNC_FASTCALL1
+ n
- 1;
3188 ad
->a
.func_call
= FUNC_FASTCALLW
;
3195 ad
->a
.mode
= VT_LLONG
+ 1;
3198 ad
->a
.mode
= VT_BYTE
+ 1;
3201 ad
->a
.mode
= VT_SHORT
+ 1;
3205 ad
->a
.mode
= VT_INT
+ 1;
3208 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
3215 ad
->a
.func_export
= 1;
3218 ad
->a
.func_import
= 1;
3221 if (tcc_state
->warn_unsupported
)
3222 tcc_warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
3223 /* skip parameters */
3225 int parenthesis
= 0;
3229 else if (tok
== ')')
3232 } while (parenthesis
&& tok
!= -1);
3245 static Sym
* find_field (CType
*type
, int v
)
3249 while ((s
= s
->next
) != NULL
) {
3250 if ((s
->v
& SYM_FIELD
) &&
3251 (s
->type
.t
& VT_BTYPE
) == VT_STRUCT
&&
3252 (s
->v
& ~SYM_FIELD
) >= SYM_FIRST_ANOM
) {
3253 Sym
*ret
= find_field (&s
->type
, v
);
3263 static void struct_add_offset (Sym
*s
, int offset
)
3265 while ((s
= s
->next
) != NULL
) {
3266 if ((s
->v
& SYM_FIELD
) &&
3267 (s
->type
.t
& VT_BTYPE
) == VT_STRUCT
&&
3268 (s
->v
& ~SYM_FIELD
) >= SYM_FIRST_ANOM
) {
3269 struct_add_offset(s
->type
.ref
, offset
);
3275 static void struct_layout(CType
*type
, AttributeDef
*ad
)
3277 int align
, maxalign
, offset
, c
, bit_pos
, bt
, prevbt
, prev_bit_size
;
3278 int pcc
= !tcc_state
->ms_bitfields
;
3281 maxalign
= 1 << (ad
->a
.aligned
- 1);
3287 prevbt
= VT_STRUCT
; /* make it never match */
3289 for (f
= type
->ref
->next
; f
; f
= f
->next
) {
3290 int typealign
, bit_size
;
3291 int size
= type_size(&f
->type
, &typealign
);
3292 if (f
->type
.t
& VT_BITFIELD
)
3293 bit_size
= (f
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
3296 if (bit_size
== 0 && pcc
) {
3297 /* Zero-width bit-fields in PCC mode aren't affected
3298 by any packing (attribute or pragma). */
3300 } else if (f
->r
> 1) {
3302 } else if (ad
->a
.packed
|| f
->r
== 1) {
3304 /* Packed fields or packed records don't let the base type
3305 influence the records type alignment. */
3310 if (type
->ref
->type
.t
!= TOK_STRUCT
) {
3311 if (pcc
&& bit_size
>= 0)
3312 size
= (bit_size
+ 7) >> 3;
3313 /* Bit position is already zero from our caller. */
3317 } else if (bit_size
< 0) {
3318 int addbytes
= pcc
? (bit_pos
+ 7) >> 3 : 0;
3321 c
= (c
+ addbytes
+ align
- 1) & -align
;
3327 /* A bit-field. Layout is more complicated. There are two
3328 options TCC implements: PCC compatible and MS compatible
3329 (PCC compatible is what GCC uses for almost all targets).
3330 In PCC layout the overall size of the struct (in c) is
3331 _excluding_ the current run of bit-fields (that is,
3332 there's at least additional bit_pos bits after c). In
3333 MS layout c does include the current run of bit-fields.
3335 This matters for calculating the natural alignment buckets
3338 /* 'align' will be used to influence records alignment,
3339 so it's the max of specified and type alignment, except
3340 in certain cases that depend on the mode. */
3341 if (align
< typealign
)
3344 /* In PCC layout a non-packed bit-field is placed adjacent
3345 to the preceding bit-fields, except if it would overflow
3346 its container (depending on base type) or it's a zero-width
3347 bit-field. Packed non-zero-width bit-fields always are
3349 int ofs
= (c
* 8 + bit_pos
) % (typealign
* 8);
3350 int ofs2
= ofs
+ bit_size
+ (typealign
* 8) - 1;
3351 if (bit_size
== 0 ||
3353 (ofs2
/ (typealign
* 8)) > (size
/typealign
))) {
3354 c
= (c
+ ((bit_pos
+ 7) >> 3) + typealign
- 1) & -typealign
;
3358 /* In PCC layout named bit-fields influence the alignment
3359 of the containing struct using the base types alignment,
3360 except for packed fields (which here have correct
3361 align/typealign). */
3362 if ((f
->v
& SYM_FIRST_ANOM
))
3365 bt
= f
->type
.t
& VT_BTYPE
;
3366 if ((bit_pos
+ bit_size
> size
* 8) ||
3367 (bit_size
> 0) == (bt
!= prevbt
)) {
3368 c
= (c
+ typealign
- 1) & -typealign
;
3371 /* In MS bitfield mode a bit-field run always uses
3372 at least as many bits as the underlying type.
3373 To start a new run it's also required that this
3374 or the last bit-field had non-zero width. */
3375 if (bit_size
|| prev_bit_size
)
3378 /* In MS layout the records alignment is normally
3379 influenced by the field, except for a zero-width
3380 field at the start of a run (but by further zero-width
3381 fields it is again). */
3382 if (bit_size
== 0 && prevbt
!= bt
)
3385 prev_bit_size
= bit_size
;
3387 f
->type
.t
= (f
->type
.t
& ~(0x3f << VT_STRUCT_SHIFT
))
3388 | (bit_pos
<< VT_STRUCT_SHIFT
);
3389 bit_pos
+= bit_size
;
3390 if (pcc
&& bit_pos
>= size
* 8) {
3392 bit_pos
-= size
* 8;
3395 if (align
> maxalign
)
3398 printf("set field %s offset=%d c=%d",
3399 get_tok_str(f
->v
& ~SYM_FIELD
, NULL
), offset
, c
);
3400 if (f
->type
.t
& VT_BITFIELD
) {
3401 printf(" pos=%d size=%d",
3402 (f
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
3403 (f
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
3408 if (f
->v
& SYM_FIRST_ANOM
&& (f
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
3410 /* An anonymous struct/union. Adjust member offsets
3411 to reflect the real offset of our containing struct.
3412 Also set the offset of this anon member inside
3413 the outer struct to be zero. Via this it
3414 works when accessing the field offset directly
3415 (from base object), as well as when recursing
3416 members in initializer handling. */
3417 int v2
= f
->type
.ref
->v
;
3418 if (!(v2
& SYM_FIELD
) &&
3419 (v2
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
3421 /* This happens only with MS extensions. The
3422 anon member has a named struct type, so it
3423 potentially is shared with other references.
3424 We need to unshare members so we can modify
3427 f
->type
.ref
= sym_push(anon_sym
++ | SYM_FIELD
,
3428 &f
->type
.ref
->type
, 0,
3430 pps
= &f
->type
.ref
->next
;
3431 while ((ass
= ass
->next
) != NULL
) {
3432 *pps
= sym_push(ass
->v
, &ass
->type
, 0, ass
->c
);
3433 pps
= &((*pps
)->next
);
3437 struct_add_offset(f
->type
.ref
, offset
);
3445 /* store size and alignment */
3446 type
->ref
->c
= (c
+ (pcc
? (bit_pos
+ 7) >> 3 : 0)
3447 + maxalign
- 1) & -maxalign
;
3448 type
->ref
->r
= maxalign
;
3451 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
3452 static void struct_decl(CType
*type
, AttributeDef
*ad
, int u
)
3454 int a
, v
, size
, align
, flexible
, alignoverride
;
3456 int bit_size
, bsize
, bt
;
3461 a
= tok
; /* save decl type */
3463 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3464 parse_attribute(ad
);
3468 /* struct already defined ? return it */
3470 expect("struct/union/enum name");
3472 if (s
&& (s
->scope
== local_scope
|| (tok
!= '{' && tok
!= ';'))) {
3474 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
3480 /* Record the original enum/struct/union token. */
3483 /* we put an undefined size for struct/union */
3484 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
3485 s
->r
= 0; /* default alignment is zero as gcc */
3486 /* put struct/union/enum name in type */
3494 tcc_error("struct/union/enum already defined");
3495 /* cannot be empty */
3497 /* non empty enums are not allowed */
3498 if (a
== TOK_ENUM
) {
3502 CType
*t
= &int_type
;
3505 expect("identifier");
3507 if (ss
&& !local_stack
)
3508 tcc_error("redefinition of enumerator '%s'",
3509 get_tok_str(v
, NULL
));
3513 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
3516 /* We really want to support long long enums
3517 on i386 as well, but the Sym structure only
3518 holds a 'long' for associated constants,
3519 and enlarging it would bump its size (no
3520 available padding). So punt for now. */
3526 if (c
!= (int)c
&& (unsigned long)c
!= (unsigned int)c
)
3527 seen_wide
= 1, t
= &size_type
;
3528 /* enum symbols have static storage */
3529 ss
= sym_push(v
, t
, VT_CONST
, c
);
3530 ss
->type
.t
|= VT_STATIC
;
3535 /* NOTE: we accept a trailing comma */
3540 s
->a
.unsigned_enum
= 1;
3541 s
->c
= type_size(seen_wide
? &size_type
: &int_type
, &align
);
3546 while (tok
!= '}') {
3547 if (!parse_btype(&btype
, &ad1
)) {
3553 tcc_error("flexible array member '%s' not at the end of struct",
3554 get_tok_str(v
, NULL
));
3559 type_decl(&type1
, &ad1
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3561 if ((type1
.t
& VT_BTYPE
) != VT_STRUCT
)
3562 expect("identifier");
3564 int v
= btype
.ref
->v
;
3565 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
3566 if (tcc_state
->ms_extensions
== 0)
3567 expect("identifier");
3571 if (type_size(&type1
, &align
) < 0) {
3572 if ((a
== TOK_STRUCT
) && (type1
.t
& VT_ARRAY
) && c
)
3575 tcc_error("field '%s' has incomplete type",
3576 get_tok_str(v
, NULL
));
3578 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
3579 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
3580 tcc_error("invalid type for '%s'",
3581 get_tok_str(v
, NULL
));
3585 bit_size
= expr_const();
3586 /* XXX: handle v = 0 case for messages */
3588 tcc_error("negative width in bit-field '%s'",
3589 get_tok_str(v
, NULL
));
3590 if (v
&& bit_size
== 0)
3591 tcc_error("zero width for bit-field '%s'",
3592 get_tok_str(v
, NULL
));
3593 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3594 parse_attribute(&ad1
);
3596 size
= type_size(&type1
, &align
);
3597 /* Only remember non-default alignment. */
3599 if (ad1
.a
.aligned
) {
3600 int speca
= 1 << (ad1
.a
.aligned
- 1);
3601 alignoverride
= speca
;
3602 } else if (ad1
.a
.packed
|| ad
->a
.packed
) {
3604 } else if (*tcc_state
->pack_stack_ptr
) {
3605 if (align
> *tcc_state
->pack_stack_ptr
)
3606 alignoverride
= *tcc_state
->pack_stack_ptr
;
3608 if (bit_size
>= 0) {
3609 bt
= type1
.t
& VT_BTYPE
;
3616 tcc_error("bitfields must have scalar type");
3618 if (bit_size
> bsize
) {
3619 tcc_error("width of '%s' exceeds its type",
3620 get_tok_str(v
, NULL
));
3621 } else if (bit_size
== bsize
) {
3622 /* no need for bit fields */
3625 type1
.t
|= VT_BITFIELD
|
3626 (0 << VT_STRUCT_SHIFT
) |
3627 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
3630 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
3631 /* Remember we've seen a real field to check
3632 for placement of flexible array member. */
3635 /* If member is a struct or bit-field, enforce
3636 placing into the struct (as anonymous). */
3638 ((type1
.t
& VT_BTYPE
) == VT_STRUCT
||
3643 ss
= sym_push(v
| SYM_FIELD
, &type1
, alignoverride
, 0);
3647 if (tok
== ';' || tok
== TOK_EOF
)
3654 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3655 parse_attribute(ad
);
3656 struct_layout(type
, ad
);
3661 /* return 1 if basic type is a type size (short, long, long long) */
3662 ST_FUNC
int is_btype_size(int bt
)
3664 return bt
== VT_SHORT
|| bt
== VT_LONG
|| bt
== VT_LLONG
;
3667 /* Add type qualifiers to a type. If the type is an array then the qualifiers
3668 are added to the element type, copied because it could be a typedef. */
3669 static void parse_btype_qualify(CType
*type
, int qualifiers
)
3671 while (type
->t
& VT_ARRAY
) {
3672 type
->ref
= sym_push(SYM_FIELD
, &type
->ref
->type
, 0, type
->ref
->c
);
3673 type
= &type
->ref
->type
;
3675 type
->t
|= qualifiers
;
3678 /* return 0 if no type declaration. otherwise, return the basic type
3681 static int parse_btype(CType
*type
, AttributeDef
*ad
)
3683 int t
, u
, bt_size
, complete
, type_found
, typespec_found
;
3687 memset(ad
, 0, sizeof(AttributeDef
));
3695 /* currently, we really ignore extension */
3706 tcc_error("too many basic types");
3708 bt_size
= is_btype_size (u
& VT_BTYPE
);
3709 if (u
== VT_INT
|| (!bt_size
&& !(t
& VT_TYPEDEF
)))
3724 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
3725 #ifndef TCC_TARGET_PE
3726 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3728 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
3729 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3735 #ifdef TCC_TARGET_ARM64
3737 /* GCC's __uint128_t appears in some Linux header files. Make it a
3738 synonym for long double to get the size and alignment right. */
3750 if ((t
& VT_BTYPE
) == VT_LONG
) {
3751 #ifdef TCC_TARGET_PE
3752 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
3754 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3762 struct_decl(&type1
, ad
, VT_ENUM
);
3765 type
->ref
= type1
.ref
;
3769 struct_decl(&type1
, ad
, VT_STRUCT
);
3772 /* type modifiers */
3777 parse_btype_qualify(type
, VT_CONSTANT
);
3785 parse_btype_qualify(type
, VT_VOLATILE
);
3792 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == (VT_DEFSIGN
|VT_UNSIGNED
))
3793 tcc_error("signed and unsigned modifier");
3806 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == VT_DEFSIGN
)
3807 tcc_error("signed and unsigned modifier");
3808 t
|= VT_DEFSIGN
| VT_UNSIGNED
;
3833 /* GNUC attribute */
3834 case TOK_ATTRIBUTE1
:
3835 case TOK_ATTRIBUTE2
:
3836 parse_attribute(ad
);
3839 t
= (t
& ~VT_BTYPE
) | u
;
3847 parse_expr_type(&type1
);
3848 /* remove all storage modifiers except typedef */
3849 type1
.t
&= ~(VT_STORAGE
&~VT_TYPEDEF
);
3855 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
3858 type
->t
= ((s
->type
.t
& ~VT_TYPEDEF
) |
3859 (t
& ~(VT_CONSTANT
| VT_VOLATILE
)));
3860 type
->ref
= s
->type
.ref
;
3861 if (t
& (VT_CONSTANT
| VT_VOLATILE
))
3862 parse_btype_qualify(type
, t
& (VT_CONSTANT
| VT_VOLATILE
));
3866 /* get attributes from typedef */
3867 if (0 == ad
->a
.aligned
)
3868 ad
->a
.aligned
= s
->a
.aligned
;
3869 if (0 == ad
->a
.func_call
)
3870 ad
->a
.func_call
= s
->a
.func_call
;
3871 ad
->a
.packed
|= s
->a
.packed
;
3880 if (tcc_state
->char_is_unsigned
) {
3881 if ((t
& (VT_DEFSIGN
|VT_BTYPE
)) == VT_BYTE
)
3885 /* long is never used as type */
3886 if ((t
& VT_BTYPE
) == VT_LONG
)
3887 #if (!defined TCC_TARGET_X86_64 && !defined TCC_TARGET_ARM64) || \
3888 defined TCC_TARGET_PE
3889 t
= (t
& ~VT_BTYPE
) | VT_INT
;
3891 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3897 /* convert a function parameter type (array to pointer and function to
3898 function pointer) */
3899 static inline void convert_parameter_type(CType
*pt
)
3901 /* remove const and volatile qualifiers (XXX: const could be used
3902 to indicate a const function parameter */
3903 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3904 /* array must be transformed to pointer according to ANSI C */
3906 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
3911 ST_FUNC
void parse_asm_str(CString
*astr
)
3914 parse_mult_str(astr
, "string constant");
3917 /* Parse an asm label and return the token */
3918 static int asm_label_instr(void)
3924 parse_asm_str(&astr
);
3927 printf("asm_alias: \"%s\"\n", (char *)astr
.data
);
3929 v
= tok_alloc(astr
.data
, astr
.size
- 1)->tok
;
3934 static void post_type(CType
*type
, AttributeDef
*ad
, int storage
)
3936 int n
, l
, t1
, arg_size
, align
;
3937 Sym
**plast
, *s
, *first
;
3942 /* function declaration */
3950 /* read param name and compute offset */
3951 if (l
!= FUNC_OLD
) {
3952 if (!parse_btype(&pt
, &ad1
)) {
3954 tcc_error("invalid type");
3961 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
3963 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3964 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
3965 tcc_error("parameter declared as void");
3966 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
3971 expect("identifier");
3975 convert_parameter_type(&pt
);
3976 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
3982 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
3989 /* if no parameters, then old type prototype */
3993 /* NOTE: const is ignored in returned type as it has a special
3994 meaning in gcc / C++ */
3995 type
->t
&= ~VT_CONSTANT
;
3996 /* some ancient pre-K&R C allows a function to return an array
3997 and the array brackets to be put after the arguments, such
3998 that "int c()[]" means something like "int[] c()" */
4001 skip(']'); /* only handle simple "[]" */
4004 /* we push a anonymous symbol which will contain the function prototype */
4005 ad
->a
.func_args
= arg_size
;
4006 s
= sym_push(SYM_FIELD
, type
, 0, l
);
4011 } else if (tok
== '[') {
4012 int saved_nocode_wanted
= nocode_wanted
;
4013 /* array definition */
4015 if (tok
== TOK_RESTRICT1
)
4020 if (!local_stack
|| (storage
& VT_STATIC
))
4021 vpushi(expr_const());
4023 /* VLAs (which can only happen with local_stack && !VT_STATIC)
4024 length must always be evaluated, even under nocode_wanted,
4025 so that its size slot is initialized (e.g. under sizeof
4030 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4033 tcc_error("invalid array size");
4035 if (!is_integer_btype(vtop
->type
.t
& VT_BTYPE
))
4036 tcc_error("size of variable length array should be an integer");
4041 /* parse next post type */
4042 post_type(type
, ad
, storage
);
4043 if (type
->t
== VT_FUNC
)
4044 tcc_error("declaration of an array of functions");
4045 t1
|= type
->t
& VT_VLA
;
4048 loc
-= type_size(&int_type
, &align
);
4052 vla_runtime_type_size(type
, &align
);
4054 vset(&int_type
, VT_LOCAL
|VT_LVAL
, n
);
4060 nocode_wanted
= saved_nocode_wanted
;
4062 /* we push an anonymous symbol which will contain the array
4064 s
= sym_push(SYM_FIELD
, type
, 0, n
);
4065 type
->t
= (t1
? VT_VLA
: VT_ARRAY
) | VT_PTR
;
4070 /* Parse a type declaration (except basic type), and return the type
4071 in 'type'. 'td' is a bitmask indicating which kind of type decl is
4072 expected. 'type' should contain the basic type. 'ad' is the
4073 attribute definition of the basic type. It can be modified by
4076 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
4079 CType type1
, *type2
;
4080 int qualifiers
, storage
;
4082 while (tok
== '*') {
4090 qualifiers
|= VT_CONSTANT
;
4095 qualifiers
|= VT_VOLATILE
;
4101 /* XXX: clarify attribute handling */
4102 case TOK_ATTRIBUTE1
:
4103 case TOK_ATTRIBUTE2
:
4104 parse_attribute(ad
);
4108 type
->t
|= qualifiers
;
4111 /* recursive type */
4112 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
4113 type1
.t
= 0; /* XXX: same as int */
4116 /* XXX: this is not correct to modify 'ad' at this point, but
4117 the syntax is not clear */
4118 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
4119 parse_attribute(ad
);
4120 type_decl(&type1
, ad
, v
, td
);
4123 /* type identifier */
4124 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
4128 if (!(td
& TYPE_ABSTRACT
))
4129 expect("identifier");
4133 storage
= type
->t
& VT_STORAGE
;
4134 type
->t
&= ~VT_STORAGE
;
4135 post_type(type
, ad
, storage
);
4137 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
4138 parse_attribute(ad
);
4142 /* append type at the end of type1 */
4155 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
4156 ST_FUNC
int lvalue_type(int t
)
4161 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
4163 else if (bt
== VT_SHORT
)
4167 if (t
& VT_UNSIGNED
)
4168 r
|= VT_LVAL_UNSIGNED
;
4172 /* indirection with full error checking and bound check */
4173 ST_FUNC
void indir(void)
4175 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
4176 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
4180 if (vtop
->r
& VT_LVAL
)
4182 vtop
->type
= *pointed_type(&vtop
->type
);
4183 /* Arrays and functions are never lvalues */
4184 if (!(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_VLA
)
4185 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
4186 vtop
->r
|= lvalue_type(vtop
->type
.t
);
4187 /* if bound checking, the referenced pointer must be checked */
4188 #ifdef CONFIG_TCC_BCHECK
4189 if (tcc_state
->do_bounds_check
)
4190 vtop
->r
|= VT_MUSTBOUND
;
4195 /* pass a parameter to a function and do type checking and casting */
4196 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
4201 func_type
= func
->c
;
4202 if (func_type
== FUNC_OLD
||
4203 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
4204 /* default casting : only need to convert float to double */
4205 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
4208 } else if (vtop
->type
.t
& VT_BITFIELD
) {
4209 type
.t
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
4210 type
.ref
= vtop
->type
.ref
;
4213 } else if (arg
== NULL
) {
4214 tcc_error("too many arguments to function");
4217 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
4218 gen_assign_cast(&type
);
4222 /* parse an expression of the form '(type)' or '(expr)' and return its
4224 static void parse_expr_type(CType
*type
)
4230 if (parse_btype(type
, &ad
)) {
4231 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
4238 static void parse_type(CType
*type
)
4243 if (!parse_btype(type
, &ad
)) {
4246 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
4249 static void vpush_tokc(int t
)
4254 vsetc(&type
, VT_CONST
, &tokc
);
4257 ST_FUNC
void unary(void)
4259 int n
, t
, align
, size
, r
, sizeof_caller
;
4264 sizeof_caller
= in_sizeof
;
4266 /* XXX: GCC 2.95.3 does not generate a table although it should be
4280 vpush_tokc(VT_INT
| VT_UNSIGNED
);
4284 vpush_tokc(VT_LLONG
);
4288 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
4292 vpush_tokc(VT_FLOAT
);
4296 vpush_tokc(VT_DOUBLE
);
4300 vpush_tokc(VT_LDOUBLE
);
4303 case TOK___FUNCTION__
:
4305 goto tok_identifier
;
4311 /* special function name identifier */
4312 len
= strlen(funcname
) + 1;
4313 /* generate char[len] type */
4318 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
4319 ptr
= section_ptr_add(data_section
, len
);
4320 memcpy(ptr
, funcname
, len
);
4325 #ifdef TCC_TARGET_PE
4326 t
= VT_SHORT
| VT_UNSIGNED
;
4332 /* string parsing */
4335 if (tcc_state
->warn_write_strings
)
4340 memset(&ad
, 0, sizeof(AttributeDef
));
4341 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, 0);
4346 if (parse_btype(&type
, &ad
)) {
4347 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
4349 /* check ISOC99 compound literal */
4351 /* data is allocated locally by default */
4356 /* all except arrays are lvalues */
4357 if (!(type
.t
& VT_ARRAY
))
4358 r
|= lvalue_type(type
.t
);
4359 memset(&ad
, 0, sizeof(AttributeDef
));
4360 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, 0);
4362 if (sizeof_caller
) {
4369 } else if (tok
== '{') {
4371 tcc_error("expected constant");
4372 /* save all registers */
4374 /* statement expression : we do not accept break/continue
4375 inside as GCC does */
4376 block(NULL
, NULL
, 1);
4391 /* functions names must be treated as function pointers,
4392 except for unary '&' and sizeof. Since we consider that
4393 functions are not lvalues, we only have to handle it
4394 there and in function calls. */
4395 /* arrays can also be used although they are not lvalues */
4396 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
4397 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
4399 mk_pointer(&vtop
->type
);
4405 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4407 boolean
.t
= VT_BOOL
;
4409 vtop
->c
.i
= !vtop
->c
.i
;
4410 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
4414 vseti(VT_JMP
, gvtst(1, 0));
4426 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
4427 tcc_error("pointer not accepted for unary plus");
4428 /* In order to force cast, we add zero, except for floating point
4429 where we really need an noop (otherwise -0.0 will be transformed
4431 if (!is_float(vtop
->type
.t
)) {
4442 unary_type(&type
); // Perform a in_sizeof = 0;
4443 size
= type_size(&type
, &align
);
4444 if (t
== TOK_SIZEOF
) {
4445 if (!(type
.t
& VT_VLA
)) {
4447 tcc_error("sizeof applied to an incomplete type");
4450 vla_runtime_type_size(&type
, &align
);
4455 vtop
->type
.t
|= VT_UNSIGNED
;
4458 case TOK_builtin_expect
:
4460 /* __builtin_expect is a no-op for now */
4472 case TOK_builtin_types_compatible_p
:
4481 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
4482 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
4483 vpushi(is_compatible_types(&type1
, &type2
));
4486 case TOK_builtin_choose_expr
:
4513 case TOK_builtin_constant_p
:
4520 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
4527 case TOK_builtin_frame_address
:
4528 case TOK_builtin_return_address
:
4535 if (tok
!= TOK_CINT
) {
4536 tcc_error("%s only takes positive integers",
4537 tok1
== TOK_builtin_return_address
?
4538 "__builtin_return_address" :
4539 "__builtin_frame_address");
4541 level
= (uint32_t)tokc
.i
;
4546 vset(&type
, VT_LOCAL
, 0); /* local frame */
4548 mk_pointer(&vtop
->type
);
4549 indir(); /* -> parent frame */
4551 if (tok1
== TOK_builtin_return_address
) {
4552 // assume return address is just above frame pointer on stack
4555 mk_pointer(&vtop
->type
);
4560 #ifdef TCC_TARGET_X86_64
4561 #ifdef TCC_TARGET_PE
4562 case TOK_builtin_va_start
:
4570 if ((vtop
->r
& VT_VALMASK
) != VT_LOCAL
)
4571 tcc_error("__builtin_va_start expects a local variable");
4572 vtop
->r
&= ~(VT_LVAL
| VT_REF
);
4573 vtop
->type
= char_pointer_type
;
4579 case TOK_builtin_va_arg_types
:
4586 vpushi(classify_x86_64_va_arg(&type
));
4592 #ifdef TCC_TARGET_ARM64
4593 case TOK___va_start
: {
4603 vtop
->type
.t
= VT_VOID
;
4606 case TOK___va_arg
: {
4619 case TOK___arm64_clear_cache
: {
4628 vtop
->type
.t
= VT_VOID
;
4632 /* pre operations */
4643 t
= vtop
->type
.t
& VT_BTYPE
;
4645 /* In IEEE negate(x) isn't subtract(0,x), but rather
4650 else if (t
== VT_DOUBLE
)
4661 goto tok_identifier
;
4663 /* allow to take the address of a label */
4664 if (tok
< TOK_UIDENT
)
4665 expect("label identifier");
4666 s
= label_find(tok
);
4668 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4670 if (s
->r
== LABEL_DECLARED
)
4671 s
->r
= LABEL_FORWARD
;
4674 s
->type
.t
= VT_VOID
;
4675 mk_pointer(&s
->type
);
4676 s
->type
.t
|= VT_STATIC
;
4678 vpushsym(&s
->type
, s
);
4682 // special qnan , snan and infinity values
4684 vpush64(VT_DOUBLE
, 0x7ff8000000000000ULL
);
4688 vpush64(VT_DOUBLE
, 0x7ff0000000000001ULL
);
4692 vpush64(VT_DOUBLE
, 0x7ff0000000000000ULL
);
4701 expect("identifier");
4704 const char *name
= get_tok_str(t
, NULL
);
4706 tcc_error("'%s' undeclared", name
);
4707 /* for simple function calls, we tolerate undeclared
4708 external reference to int() function */
4709 if (tcc_state
->warn_implicit_function_declaration
4710 #ifdef TCC_TARGET_PE
4711 /* people must be warned about using undeclared WINAPI functions
4712 (which usually start with uppercase letter) */
4713 || (name
[0] >= 'A' && name
[0] <= 'Z')
4716 tcc_warning("implicit declaration of function '%s'", name
);
4717 s
= external_global_sym(t
, &func_old_type
, 0);
4719 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
4720 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
4721 /* if referencing an inline function, then we generate a
4722 symbol to it if not already done. It will have the
4723 effect to generate code for it at the end of the
4724 compilation unit. Inline function as always
4725 generated in the text section. */
4726 if (!s
->c
&& !nocode_wanted
)
4727 put_extern_sym(s
, text_section
, 0, 0);
4728 r
= VT_SYM
| VT_CONST
;
4731 /* A symbol that has a register is a local register variable,
4732 which starts out as VT_LOCAL value. */
4733 if ((r
& VT_VALMASK
) < VT_CONST
)
4734 r
= (r
& ~VT_VALMASK
) | VT_LOCAL
;
4736 vset(&s
->type
, r
, s
->c
);
4737 /* Point to s as backpointer (even without r&VT_SYM).
4738 Will be used by at least the x86 inline asm parser for
4741 if (vtop
->r
& VT_SYM
) {
4747 /* post operations */
4749 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
4752 } else if (tok
== '.' || tok
== TOK_ARROW
|| tok
== TOK_CDOUBLE
) {
4755 if (tok
== TOK_ARROW
)
4757 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
4760 /* expect pointer on structure */
4761 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
4762 expect("struct or union");
4763 if (tok
== TOK_CDOUBLE
)
4764 expect("field name");
4766 if (tok
== TOK_CINT
|| tok
== TOK_CUINT
)
4767 expect("field name");
4768 s
= find_field(&vtop
->type
, tok
);
4770 tcc_error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, &tokc
));
4771 /* add field offset to pointer */
4772 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
4775 /* change type to field type, and set to lvalue */
4776 vtop
->type
= s
->type
;
4777 vtop
->type
.t
|= qualifiers
;
4778 /* an array is never an lvalue */
4779 if (!(vtop
->type
.t
& VT_ARRAY
)) {
4780 vtop
->r
|= lvalue_type(vtop
->type
.t
);
4781 #ifdef CONFIG_TCC_BCHECK
4782 /* if bound checking, the referenced pointer must be checked */
4783 if (tcc_state
->do_bounds_check
&& (vtop
->r
& VT_VALMASK
) != VT_LOCAL
)
4784 vtop
->r
|= VT_MUSTBOUND
;
4788 } else if (tok
== '[') {
4794 } else if (tok
== '(') {
4797 int nb_args
, ret_nregs
, ret_align
, regsize
, variadic
;
4800 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
4801 /* pointer test (no array accepted) */
4802 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
4803 vtop
->type
= *pointed_type(&vtop
->type
);
4804 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
4808 expect("function pointer");
4811 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
4813 /* get return type */
4816 sa
= s
->next
; /* first parameter */
4819 /* compute first implicit argument if a structure is returned */
4820 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
4821 variadic
= (s
->c
== FUNC_ELLIPSIS
);
4822 ret_nregs
= gfunc_sret(&s
->type
, variadic
, &ret
.type
,
4823 &ret_align
, ®size
);
4825 /* get some space for the returned structure */
4826 size
= type_size(&s
->type
, &align
);
4827 #ifdef TCC_TARGET_ARM64
4828 /* On arm64, a small struct is return in registers.
4829 It is much easier to write it to memory if we know
4830 that we are allowed to write some extra bytes, so
4831 round the allocated space up to a power of 2: */
4833 while (size
& (size
- 1))
4834 size
= (size
| (size
- 1)) + 1;
4836 loc
= (loc
- size
) & -align
;
4838 ret
.r
= VT_LOCAL
| VT_LVAL
;
4839 /* pass it as 'int' to avoid structure arg passing
4841 vseti(VT_LOCAL
, loc
);
4851 /* return in register */
4852 if (is_float(ret
.type
.t
)) {
4853 ret
.r
= reg_fret(ret
.type
.t
);
4854 #ifdef TCC_TARGET_X86_64
4855 if ((ret
.type
.t
& VT_BTYPE
) == VT_QFLOAT
)
4859 #ifndef TCC_TARGET_ARM64
4860 #ifdef TCC_TARGET_X86_64
4861 if ((ret
.type
.t
& VT_BTYPE
) == VT_QLONG
)
4863 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
4874 gfunc_param_typed(s
, sa
);
4884 tcc_error("too few arguments to function");
4886 gfunc_call(nb_args
);
4889 for (r
= ret
.r
+ ret_nregs
+ !ret_nregs
; r
-- > ret
.r
;) {
4890 vsetc(&ret
.type
, r
, &ret
.c
);
4891 vtop
->r2
= ret
.r2
; /* Loop only happens when r2 is VT_CONST */
4894 /* handle packed struct return */
4895 if (((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) && ret_nregs
) {
4898 size
= type_size(&s
->type
, &align
);
4899 /* We're writing whole regs often, make sure there's enough
4900 space. Assume register size is power of 2. */
4901 if (regsize
> align
)
4903 loc
= (loc
- size
) & -align
;
4907 vset(&ret
.type
, VT_LOCAL
| VT_LVAL
, addr
+ offset
);
4911 if (--ret_nregs
== 0)
4915 vset(&s
->type
, VT_LOCAL
| VT_LVAL
, addr
);
4923 ST_FUNC
void expr_prod(void)
4928 while (tok
== '*' || tok
== '/' || tok
== '%') {
4936 ST_FUNC
void expr_sum(void)
4941 while (tok
== '+' || tok
== '-') {
4949 static void expr_shift(void)
4954 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
4962 static void expr_cmp(void)
4967 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
4968 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
4976 static void expr_cmpeq(void)
4981 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
4989 static void expr_and(void)
4992 while (tok
== '&') {
4999 static void expr_xor(void)
5002 while (tok
== '^') {
5009 static void expr_or(void)
5012 while (tok
== '|') {
5019 /* XXX: fix this mess */
5020 static void expr_land_const(void)
5023 while (tok
== TOK_LAND
) {
5029 static void expr_lor_const(void)
5032 while (tok
== TOK_LOR
) {
5039 static void expr_land(void)
5042 if (tok
== TOK_LAND
) {
5045 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
5053 while (tok
== TOK_LAND
) {
5061 gen_cast(&int_type
);
5069 if (tok
!= TOK_LAND
) {
5082 static void expr_lor(void)
5085 if (tok
== TOK_LOR
) {
5088 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
5096 while (tok
== TOK_LOR
) {
5104 gen_cast(&int_type
);
5112 if (tok
!= TOK_LOR
) {
5125 /* Assuming vtop is a value used in a conditional context
5126 (i.e. compared with zero) return 0 if it's false, 1 if
5127 true and -1 if it can't be statically determined. */
5128 static int condition_3way(void)
5131 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
&&
5132 (!(vtop
->r
& VT_SYM
) ||
5133 !(vtop
->sym
->type
.t
& VT_WEAK
))) {
5135 boolean
.t
= VT_BOOL
;
5144 static void expr_cond(void)
5146 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
, islv
, c
, g
;
5148 CType type
, type1
, type2
;
5153 c
= condition_3way();
5154 g
= (tok
== ':' && gnu_ext
);
5156 /* needed to avoid having different registers saved in
5158 if (is_float(vtop
->type
.t
)) {
5160 #ifdef TCC_TARGET_X86_64
5161 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
5186 sv
= *vtop
; /* save value to handle it later */
5187 vtop
--; /* no vpop so that FP stack is not flushed */
5205 bt1
= t1
& VT_BTYPE
;
5207 bt2
= t2
& VT_BTYPE
;
5208 /* cast operands to correct type according to ISOC rules */
5209 if (is_float(bt1
) || is_float(bt2
)) {
5210 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
5211 type
.t
= VT_LDOUBLE
;
5213 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
5218 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
5219 /* cast to biggest op */
5221 /* convert to unsigned if it does not fit in a long long */
5222 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
5223 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
5224 type
.t
|= VT_UNSIGNED
;
5225 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
5226 /* If one is a null ptr constant the result type
5228 if (is_null_pointer (vtop
))
5230 else if (is_null_pointer (&sv
))
5232 /* XXX: test pointer compatibility, C99 has more elaborate
5236 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
5237 /* XXX: test function pointer compatibility */
5238 type
= bt1
== VT_FUNC
? type1
: type2
;
5239 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
5240 /* XXX: test structure compatibility */
5241 type
= bt1
== VT_STRUCT
? type1
: type2
;
5242 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
5243 /* NOTE: as an extension, we accept void on only one side */
5246 /* integer operations */
5248 /* convert to unsigned if it does not fit in an integer */
5249 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
5250 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
5251 type
.t
|= VT_UNSIGNED
;
5253 /* keep structs lvalue by transforming `(expr ? a : b)` to `*(expr ? &a : &b)` so
5254 that `(expr ? a : b).mem` does not error with "lvalue expected" */
5255 islv
= (vtop
->r
& VT_LVAL
) && (sv
.r
& VT_LVAL
) && VT_STRUCT
== (type
.t
& VT_BTYPE
);
5258 /* now we convert second operand */
5262 mk_pointer(&vtop
->type
);
5264 } else if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
5269 if (is_float(type
.t
)) {
5271 #ifdef TCC_TARGET_X86_64
5272 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
5276 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
5277 /* for long longs, we use fixed registers to avoid having
5278 to handle a complicated move */
5289 /* this is horrible, but we must also convert first
5295 mk_pointer(&vtop
->type
);
5297 } else if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
5303 move_reg(r2
, r1
, type
.t
);
5313 static void expr_eq(void)
5319 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
5320 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
5321 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
5336 ST_FUNC
void gexpr(void)
5347 /* parse an expression and return its type without any side effect. */
5348 static void expr_type(CType
*type
)
5358 /* parse a unary expression and return its type without any side
5360 static void unary_type(CType
*type
)
5369 /* parse a constant expression and return value in vtop. */
5370 static void expr_const1(void)
5377 /* parse an integer constant and return its value. */
5378 static inline int64_t expr_const64(void)
5382 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
5383 expect("constant expression");
5389 /* parse an integer constant and return its value.
5390 Complain if it doesn't fit 32bit (signed or unsigned). */
5391 ST_FUNC
int expr_const(void)
5394 int64_t wc
= expr_const64();
5396 if (c
!= wc
&& (unsigned)c
!= wc
)
5397 tcc_error("constant exceeds 32 bit");
5401 /* return the label token if current token is a label, otherwise
5403 static int is_label(void)
5407 /* fast test first */
5408 if (tok
< TOK_UIDENT
)
5410 /* no need to save tokc because tok is an identifier */
5417 unget_tok(last_tok
);
5422 static void label_or_decl(int l
)
5426 /* fast test first */
5427 if (tok
>= TOK_UIDENT
)
5429 /* no need to save tokc because tok is an identifier */
5433 unget_tok(last_tok
);
5436 unget_tok(last_tok
);
5441 static int case_cmp(const void *pa
, const void *pb
)
5443 int64_t a
= (*(struct case_t
**) pa
)->v1
;
5444 int64_t b
= (*(struct case_t
**) pb
)->v1
;
5445 return a
< b
? -1 : a
> b
;
5448 static void gcase(struct case_t
**base
, int len
, int *bsym
)
5452 int ll
= (vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
;
5470 gtst_addr(0, p
->sym
); /* v1 <= x <= v2 */
5472 gcase(base
, len
/2, bsym
);
5473 if (cur_switch
->def_sym
)
5474 gjmp_addr(cur_switch
->def_sym
);
5476 *bsym
= gjmp(*bsym
);
5480 base
+= e
; len
-= e
;
5490 if (p
->v1
== p
->v2
) {
5492 gtst_addr(0, p
->sym
);
5502 gtst_addr(0, p
->sym
);
5508 static void block(int *bsym
, int *csym
, int is_expr
)
5510 int a
, b
, c
, d
, cond
;
5513 /* generate line number info */
5514 if (tcc_state
->do_debug
&&
5515 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
5516 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
5518 last_line_num
= file
->line_num
;
5522 /* default return value is (void) */
5524 vtop
->type
.t
= VT_VOID
;
5527 if (tok
== TOK_IF
) {
5529 int saved_nocode_wanted
= nocode_wanted
;
5534 cond
= condition_3way();
5540 nocode_wanted
|= 0x20000000;
5541 block(bsym
, csym
, 0);
5543 nocode_wanted
= saved_nocode_wanted
;
5545 if (c
== TOK_ELSE
) {
5550 nocode_wanted
|= 0x20000000;
5551 block(bsym
, csym
, 0);
5552 gsym(d
); /* patch else jmp */
5554 nocode_wanted
= saved_nocode_wanted
;
5557 } else if (tok
== TOK_WHILE
) {
5558 int saved_nocode_wanted
;
5559 nocode_wanted
&= ~0x20000000;
5569 saved_nocode_wanted
= nocode_wanted
;
5571 nocode_wanted
= saved_nocode_wanted
;
5576 } else if (tok
== '{') {
5578 int block_vla_sp_loc
= vla_sp_loc
, saved_vlas_in_scope
= vlas_in_scope
;
5581 /* record local declaration stack position */
5583 llabel
= local_label_stack
;
5586 /* handle local labels declarations */
5587 if (tok
== TOK_LABEL
) {
5590 if (tok
< TOK_UIDENT
)
5591 expect("label identifier");
5592 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
5602 while (tok
!= '}') {
5603 label_or_decl(VT_LOCAL
);
5607 block(bsym
, csym
, is_expr
);
5610 /* pop locally defined labels */
5611 label_pop(&local_label_stack
, llabel
);
5612 /* pop locally defined symbols */
5614 /* In the is_expr case (a statement expression is finished here),
5615 vtop might refer to symbols on the local_stack. Either via the
5616 type or via vtop->sym. We can't pop those nor any that in turn
5617 might be referred to. To make it easier we don't roll back
5618 any symbols in that case; some upper level call to block() will
5619 do that. We do have to remove such symbols from the lookup
5620 tables, though. sym_pop will do that. */
5621 sym_pop(&local_stack
, s
, is_expr
);
5623 /* Pop VLA frames and restore stack pointer if required */
5624 if (vlas_in_scope
> saved_vlas_in_scope
) {
5625 vla_sp_loc
= saved_vlas_in_scope
? block_vla_sp_loc
: vla_sp_root_loc
;
5628 vlas_in_scope
= saved_vlas_in_scope
;
5631 } else if (tok
== TOK_RETURN
) {
5635 gen_assign_cast(&func_vt
);
5636 #ifdef TCC_TARGET_ARM64
5637 // Perhaps it would be better to use this for all backends:
5640 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
5641 CType type
, ret_type
;
5642 int ret_align
, ret_nregs
, regsize
;
5643 ret_nregs
= gfunc_sret(&func_vt
, func_var
, &ret_type
,
5644 &ret_align
, ®size
);
5645 if (0 == ret_nregs
) {
5646 /* if returning structure, must copy it to implicit
5647 first pointer arg location */
5650 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
5653 /* copy structure value to pointer */
5656 /* returning structure packed into registers */
5657 int r
, size
, addr
, align
;
5658 size
= type_size(&func_vt
,&align
);
5659 if ((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) ||
5660 (vtop
->c
.i
& (ret_align
-1)))
5661 && (align
& (ret_align
-1))) {
5662 loc
= (loc
- size
) & -ret_align
;
5665 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
5669 vset(&ret_type
, VT_LOCAL
| VT_LVAL
, addr
);
5671 vtop
->type
= ret_type
;
5672 if (is_float(ret_type
.t
))
5673 r
= rc_fret(ret_type
.t
);
5684 if (--ret_nregs
== 0)
5686 /* We assume that when a structure is returned in multiple
5687 registers, their classes are consecutive values of the
5690 vtop
->c
.i
+= regsize
;
5694 } else if (is_float(func_vt
.t
)) {
5695 gv(rc_fret(func_vt
.t
));
5700 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
5703 /* jump unless last stmt in top-level block */
5704 if (tok
!= '}' || local_scope
!= 1)
5706 nocode_wanted
|= 0x20000000;
5707 } else if (tok
== TOK_BREAK
) {
5710 tcc_error("cannot break");
5711 *bsym
= gjmp(*bsym
);
5714 nocode_wanted
|= 0x20000000;
5715 } else if (tok
== TOK_CONTINUE
) {
5718 tcc_error("cannot continue");
5719 vla_sp_restore_root();
5720 *csym
= gjmp(*csym
);
5723 } else if (tok
== TOK_FOR
) {
5725 int saved_nocode_wanted
;
5726 nocode_wanted
&= ~0x20000000;
5732 /* c99 for-loop init decl? */
5733 if (!decl0(VT_LOCAL
, 1)) {
5734 /* no, regular for-loop init expr */
5760 saved_nocode_wanted
= nocode_wanted
;
5762 nocode_wanted
= saved_nocode_wanted
;
5767 sym_pop(&local_stack
, s
, 0);
5770 if (tok
== TOK_DO
) {
5771 int saved_nocode_wanted
;
5772 nocode_wanted
&= ~0x20000000;
5778 saved_nocode_wanted
= nocode_wanted
;
5786 nocode_wanted
= saved_nocode_wanted
;
5791 if (tok
== TOK_SWITCH
) {
5792 struct switch_t
*saved
, sw
;
5793 int saved_nocode_wanted
= nocode_wanted
;
5799 switchval
= *vtop
--;
5801 b
= gjmp(0); /* jump to first case */
5802 sw
.p
= NULL
; sw
.n
= 0; sw
.def_sym
= 0;
5806 nocode_wanted
= saved_nocode_wanted
;
5807 a
= gjmp(a
); /* add implicit break */
5810 qsort(sw
.p
, sw
.n
, sizeof(void*), case_cmp
);
5811 for (b
= 1; b
< sw
.n
; b
++)
5812 if (sw
.p
[b
- 1]->v2
>= sw
.p
[b
]->v1
)
5813 tcc_error("duplicate case value");
5814 /* Our switch table sorting is signed, so the compared
5815 value needs to be as well when it's 64bit. */
5816 if ((switchval
.type
.t
& VT_BTYPE
) == VT_LLONG
)
5817 switchval
.type
.t
&= ~VT_UNSIGNED
;
5819 gcase(sw
.p
, sw
.n
, &a
);
5822 gjmp_addr(sw
.def_sym
);
5823 dynarray_reset(&sw
.p
, &sw
.n
);
5828 if (tok
== TOK_CASE
) {
5829 struct case_t
*cr
= tcc_malloc(sizeof(struct case_t
));
5832 nocode_wanted
&= ~0x20000000;
5834 cr
->v1
= cr
->v2
= expr_const64();
5835 if (gnu_ext
&& tok
== TOK_DOTS
) {
5837 cr
->v2
= expr_const64();
5838 if (cr
->v2
< cr
->v1
)
5839 tcc_warning("empty case range");
5842 dynarray_add((void***) &cur_switch
->p
, &cur_switch
->n
, cr
);
5845 goto block_after_label
;
5847 if (tok
== TOK_DEFAULT
) {
5852 if (cur_switch
->def_sym
)
5853 tcc_error("too many 'default'");
5854 cur_switch
->def_sym
= ind
;
5856 goto block_after_label
;
5858 if (tok
== TOK_GOTO
) {
5860 if (tok
== '*' && gnu_ext
) {
5864 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
5867 } else if (tok
>= TOK_UIDENT
) {
5868 s
= label_find(tok
);
5869 /* put forward definition if needed */
5871 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
5873 if (s
->r
== LABEL_DECLARED
)
5874 s
->r
= LABEL_FORWARD
;
5876 vla_sp_restore_root();
5877 if (s
->r
& LABEL_FORWARD
)
5878 s
->jnext
= gjmp(s
->jnext
);
5880 gjmp_addr(s
->jnext
);
5883 expect("label identifier");
5886 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
5894 if (s
->r
== LABEL_DEFINED
)
5895 tcc_error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
5897 s
->r
= LABEL_DEFINED
;
5899 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
5903 /* we accept this, but it is a mistake */
5905 nocode_wanted
&= ~0x20000000;
5907 tcc_warning("deprecated use of label at end of compound statement");
5911 block(bsym
, csym
, is_expr
);
5914 /* expression case */
5929 #define EXPR_CONST 1
5932 static void parse_init_elem(int expr_type
)
5934 int saved_global_expr
;
5937 /* compound literals must be allocated globally in this case */
5938 saved_global_expr
= global_expr
;
5941 global_expr
= saved_global_expr
;
5942 /* NOTE: symbols are accepted */
5943 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
5944 tcc_error("initializer element is not constant");
5952 /* t is the array or struct type. c is the array or struct
5953 address. cur_field is the pointer to the current
5954 value, for arrays the 'c' member contains the current start
5955 index and the 'r' contains the end index (in case of range init).
5956 'size_only' is true if only size info is needed (only used
5958 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
5959 Sym
**cur_field
, int size_only
)
5962 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
5968 if (gnu_ext
&& (l
= is_label()) != 0)
5970 while (tok
== '[' || tok
== '.') {
5972 if (!(type
->t
& VT_ARRAY
))
5973 expect("array type");
5976 index
= expr_const();
5977 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
5978 tcc_error("invalid index");
5979 if (tok
== TOK_DOTS
&& gnu_ext
) {
5981 index_last
= expr_const();
5982 if (index_last
< 0 ||
5983 (s
->c
>= 0 && index_last
>= s
->c
) ||
5985 tcc_error("invalid index");
5991 (*cur_field
)->c
= index
;
5992 (*cur_field
)->r
= index_last
;
5994 type
= pointed_type(type
);
5995 elem_size
= type_size(type
, &align
);
5996 c
+= index
* elem_size
;
5997 /* NOTE: we only support ranges for last designator */
5998 nb_elems
= index_last
- index
+ 1;
5999 if (nb_elems
!= 1) {
6008 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
6009 expect("struct/union type");
6010 f
= find_field(type
, l
);
6015 /* XXX: fix this mess by using explicit storage field */
6017 type1
.t
|= (type
->t
& ~VT_TYPE
);
6031 if (type
->t
& VT_ARRAY
) {
6032 index
= (*cur_field
)->c
;
6033 if (type
->ref
->c
>= 0 && index
>= type
->ref
->c
)
6034 tcc_error("index too large");
6035 type
= pointed_type(type
);
6036 c
+= index
* type_size(type
, &align
);
6040 tcc_error("too many field init");
6041 /* XXX: fix this mess by using explicit storage field */
6043 type1
.t
|= (type
->t
& ~VT_TYPE
);
6048 decl_initializer(type
, sec
, c
, 0, size_only
);
6050 /* XXX: make it more general */
6051 if (!size_only
&& nb_elems
> 1) {
6052 unsigned long c_end
;
6057 vset(type
, VT_LOCAL
|VT_LVAL
, c
);
6058 for (i
= 1; i
< nb_elems
; i
++) {
6059 vset(type
, VT_LOCAL
|VT_LVAL
, c
+ elem_size
* i
);
6065 c_end
= c
+ nb_elems
* elem_size
;
6066 if (c_end
> sec
->data_allocated
)
6067 section_realloc(sec
, c_end
);
6068 src
= sec
->data
+ c
;
6070 for(i
= 1; i
< nb_elems
; i
++) {
6072 memcpy(dst
, src
, elem_size
);
6078 /* store a value or an expression directly in global data or in local array */
6079 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
)
6081 int bt
, bit_pos
, bit_size
;
6083 unsigned long long bit_mask
;
6087 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
6091 /* XXX: not portable */
6092 /* XXX: generate error if incorrect relocation */
6093 gen_assign_cast(&dtype
);
6094 bt
= type
->t
& VT_BTYPE
;
6095 size
= type_size(type
, &align
);
6096 if (c
+ size
> sec
->data_allocated
) {
6097 section_realloc(sec
, c
+ size
);
6099 ptr
= sec
->data
+ c
;
6100 /* XXX: make code faster ? */
6101 if (!(type
->t
& VT_BITFIELD
)) {
6103 bit_size
= PTR_SIZE
* 8;
6106 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
6107 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
6108 bit_mask
= (1LL << bit_size
) - 1;
6110 if ((vtop
->r
& (VT_SYM
|VT_CONST
)) == (VT_SYM
|VT_CONST
) &&
6111 vtop
->sym
->v
>= SYM_FIRST_ANOM
&&
6112 /* XXX This rejects compount literals like
6113 '(void *){ptr}'. The problem is that '&sym' is
6114 represented the same way, which would be ruled out
6115 by the SYM_FIRST_ANOM check above, but also '"string"'
6116 in 'char *p = "string"' is represented the same
6117 with the type being VT_PTR and the symbol being an
6118 anonymous one. That is, there's no difference in vtop
6119 between '(void *){x}' and '&(void *){x}'. Ignore
6120 pointer typed entities here. Hopefully no real code
6121 will every use compound literals with scalar type. */
6122 (vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
6123 /* These come from compound literals, memcpy stuff over. */
6127 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[vtop
->sym
->c
];
6128 ssec
= tcc_state
->sections
[esym
->st_shndx
];
6129 memmove (ptr
, ssec
->data
+ esym
->st_value
, size
);
6131 /* We need to copy over all memory contents, and that
6132 includes relocations. Use the fact that relocs are
6133 created it order, so look from the end of relocs
6134 until we hit one before the copied region. */
6135 int num_relocs
= ssec
->reloc
->data_offset
/ sizeof(*rel
);
6136 rel
= (ElfW_Rel
*)(ssec
->reloc
->data
+ ssec
->reloc
->data_offset
);
6137 while (num_relocs
--) {
6139 if (rel
->r_offset
>= esym
->st_value
+ size
)
6141 if (rel
->r_offset
< esym
->st_value
)
6143 /* Note: if the same fields are initialized multiple
6144 times (possible with designators) then we possibly
6145 add multiple relocations for the same offset here.
6146 That would lead to wrong code, the last reloc needs
6147 to win. We clean this up later after the whole
6148 initializer is parsed. */
6149 put_elf_reloca(symtab_section
, sec
,
6150 c
+ rel
->r_offset
- esym
->st_value
,
6151 ELFW(R_TYPE
)(rel
->r_info
),
6152 ELFW(R_SYM
)(rel
->r_info
),
6153 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
6162 if ((vtop
->r
& VT_SYM
) &&
6168 (bt
== VT_LLONG
&& bit_size
!= 64) ||
6172 (bt
== VT_INT
&& bit_size
!= 32)
6175 tcc_error("initializer element is not computable at load time");
6177 /* XXX: when cross-compiling we assume that each type has the
6178 same representation on host and target, which is likely to
6179 be wrong in the case of long double */
6181 vtop
->c
.i
= (vtop
->c
.i
!= 0);
6183 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
6186 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
6189 *(double *)ptr
= vtop
->c
.d
;
6192 if (sizeof(long double) == LDOUBLE_SIZE
)
6193 *(long double *)ptr
= vtop
->c
.ld
;
6194 else if (sizeof(double) == LDOUBLE_SIZE
)
6195 *(double *)ptr
= vtop
->c
.ld
;
6197 tcc_error("can't cross compile long double constants");
6201 *(long long *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
6208 addr_t val
= (vtop
->c
.i
& bit_mask
) << bit_pos
;
6209 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
6210 if (vtop
->r
& VT_SYM
)
6211 greloca(sec
, vtop
->sym
, c
, R_DATA_PTR
, val
);
6213 *(addr_t
*)ptr
|= val
;
6215 if (vtop
->r
& VT_SYM
)
6216 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
6217 *(addr_t
*)ptr
|= val
;
6223 int val
= (vtop
->c
.i
& bit_mask
) << bit_pos
;
6224 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
6225 if (vtop
->r
& VT_SYM
)
6226 greloca(sec
, vtop
->sym
, c
, R_DATA_PTR
, val
);
6230 if (vtop
->r
& VT_SYM
)
6231 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
6240 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
6247 /* put zeros for variable based init */
6248 static void init_putz(Section
*sec
, unsigned long c
, int size
)
6251 /* nothing to do because globals are already set to zero */
6253 vpush_global_sym(&func_old_type
, TOK_memset
);
6255 #ifdef TCC_TARGET_ARM
6266 /* 't' contains the type and storage info. 'c' is the offset of the
6267 object in section 'sec'. If 'sec' is NULL, it means stack based
6268 allocation. 'first' is true if array '{' must be read (multi
6269 dimension implicit array init handling). 'size_only' is true if
6270 size only evaluation is wanted (only for arrays). */
6271 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
6272 int first
, int size_only
)
6274 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, parlevel1
, i
;
6281 /* If we currently are at an '}' or ',' we have read an initializer
6282 element in one of our callers, and not yet consumed it. */
6283 have_elem
= tok
== '}' || tok
== ',';
6284 if (!have_elem
&& tok
!= '{' &&
6285 /* In case of strings we have special handling for arrays, so
6286 don't consume them as initializer value (which would commit them
6287 to some anonymous symbol). */
6288 tok
!= TOK_LSTR
&& tok
!= TOK_STR
&&
6290 parse_init_elem(!sec
? EXPR_ANY
: EXPR_CONST
);
6295 !(type
->t
& VT_ARRAY
) &&
6296 /* Use i_c_parameter_t, to strip toplevel qualifiers.
6297 The source type might have VT_CONSTANT set, which is
6298 of course assignable to non-const elements. */
6299 is_compatible_parameter_types(type
, &vtop
->type
)) {
6300 init_putv(type
, sec
, c
);
6301 } else if (type
->t
& VT_ARRAY
) {
6305 t1
= pointed_type(type
);
6306 size1
= type_size(t1
, &align1
);
6309 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
6312 tcc_error("character array initializer must be a literal,"
6313 " optionally enclosed in braces");
6318 /* only parse strings here if correct type (otherwise: handle
6319 them as ((w)char *) expressions */
6320 if ((tok
== TOK_LSTR
&&
6321 #ifdef TCC_TARGET_PE
6322 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
6324 (t1
->t
& VT_BTYPE
) == VT_INT
6326 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
6327 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
6330 /* compute maximum number of chars wanted */
6332 cstr_len
= tokc
.str
.size
;
6334 cstr_len
= tokc
.str
.size
/ sizeof(nwchar_t
);
6337 if (n
>= 0 && nb
> (n
- array_length
))
6338 nb
= n
- array_length
;
6341 tcc_warning("initializer-string for array is too long");
6342 /* in order to go faster for common case (char
6343 string in global variable, we handle it
6345 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
6346 memcpy(sec
->data
+ c
+ array_length
, tokc
.str
.data
, nb
);
6350 ch
= ((unsigned char *)tokc
.str
.data
)[i
];
6352 ch
= ((nwchar_t
*)tokc
.str
.data
)[i
];
6354 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
);
6361 /* only add trailing zero if enough storage (no
6362 warning in this case since it is standard) */
6363 if (n
< 0 || array_length
< n
) {
6366 init_putv(t1
, sec
, c
+ (array_length
* size1
));
6376 while (tok
!= '}' || have_elem
) {
6377 decl_designator(type
, sec
, c
, &f
, size_only
);
6380 /* must put zero in holes (note that doing it that way
6381 ensures that it even works with designators) */
6382 if (!size_only
&& array_length
< index
) {
6383 init_putz(sec
, c
+ array_length
* size1
,
6384 (index
- array_length
) * size1
);
6386 if (type
->t
& VT_ARRAY
) {
6387 index
= indexsym
.c
= ++indexsym
.r
;
6389 index
= index
+ type_size(&f
->type
, &align1
);
6390 if (s
->type
.t
== TOK_UNION
)
6395 if (index
> array_length
)
6396 array_length
= index
;
6398 if (type
->t
& VT_ARRAY
) {
6399 /* special test for multi dimensional arrays (may not
6400 be strictly correct if designators are used at the
6402 if (no_oblock
&& index
>= n
)
6405 if (no_oblock
&& f
== NULL
)
6413 /* put zeros at the end */
6414 if (!size_only
&& array_length
< n
) {
6415 init_putz(sec
, c
+ array_length
* size1
,
6416 (n
- array_length
) * size1
);
6420 /* patch type size if needed, which happens only for array types */
6422 s
->c
= array_length
;
6423 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
6426 if (first
|| tok
== '{') {
6435 } else if (tok
== '{') {
6437 decl_initializer(type
, sec
, c
, first
, size_only
);
6439 } else if (size_only
) {
6440 /* If we supported only ISO C we wouldn't have to accept calling
6441 this on anything than an array size_only==1 (and even then
6442 only on the outermost level, so no recursion would be needed),
6443 because initializing a flex array member isn't supported.
6444 But GNU C supports it, so we need to recurse even into
6445 subfields of structs and arrays when size_only is set. */
6446 /* just skip expression */
6447 parlevel
= parlevel1
= 0;
6448 while ((parlevel
> 0 || parlevel1
> 0 ||
6449 (tok
!= '}' && tok
!= ',')) && tok
!= -1) {
6452 else if (tok
== ')') {
6453 if (parlevel
== 0 && parlevel1
== 0)
6457 else if (tok
== '{')
6459 else if (tok
== '}') {
6460 if (parlevel
== 0 && parlevel1
== 0)
6468 /* This should happen only when we haven't parsed
6469 the init element above for fear of committing a
6470 string constant to memory too early. */
6471 if (tok
!= TOK_STR
&& tok
!= TOK_LSTR
)
6472 expect("string constant");
6473 parse_init_elem(!sec
? EXPR_ANY
: EXPR_CONST
);
6475 init_putv(type
, sec
, c
);
6479 /* parse an initializer for type 't' if 'has_init' is non zero, and
6480 allocate space in local or global data space ('r' is either
6481 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
6482 variable 'v' of scope 'scope' is declared before initializers
6483 are parsed. If 'v' is zero, then a reference to the new object
6484 is put in the value stack. If 'has_init' is 2, a special parsing
6485 is done to handle string constants. */
6486 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
6487 int has_init
, int v
, int scope
)
6489 int size
, align
, addr
, data_offset
;
6491 ParseState saved_parse_state
= {0};
6492 TokenString
*init_str
= NULL
;
6494 Sym
*flexible_array
;
6496 flexible_array
= NULL
;
6497 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
6498 Sym
*field
= type
->ref
->next
;
6501 field
= field
->next
;
6502 if (field
->type
.t
& VT_ARRAY
&& field
->type
.ref
->c
< 0)
6503 flexible_array
= field
;
6507 size
= type_size(type
, &align
);
6508 /* If unknown size, we must evaluate it before
6509 evaluating initializers because
6510 initializers can generate global data too
6511 (e.g. string pointers or ISOC99 compound
6512 literals). It also simplifies local
6513 initializers handling */
6514 if (size
< 0 || (flexible_array
&& has_init
)) {
6516 tcc_error("unknown type size");
6517 /* get all init string */
6518 init_str
= tok_str_alloc();
6519 if (has_init
== 2) {
6520 /* only get strings */
6521 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
6522 tok_str_add_tok(init_str
);
6527 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
6529 tcc_error("unexpected end of file in initializer");
6530 tok_str_add_tok(init_str
);
6533 else if (tok
== '}') {
6543 tok_str_add(init_str
, -1);
6544 tok_str_add(init_str
, 0);
6547 save_parse_state(&saved_parse_state
);
6549 begin_macro(init_str
, 1);
6551 decl_initializer(type
, NULL
, 0, 1, 1);
6552 /* prepare second initializer parsing */
6553 macro_ptr
= init_str
->str
;
6556 /* if still unknown size, error */
6557 size
= type_size(type
, &align
);
6559 tcc_error("unknown type size");
6561 /* If there's a flex member and it was used in the initializer
6563 if (flexible_array
&&
6564 flexible_array
->type
.ref
->c
> 0)
6565 size
+= flexible_array
->type
.ref
->c
6566 * pointed_size(&flexible_array
->type
);
6567 /* take into account specified alignment if bigger */
6568 if (ad
->a
.aligned
) {
6569 int speca
= 1 << (ad
->a
.aligned
- 1);
6572 } else if (ad
->a
.packed
) {
6575 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
6577 #ifdef CONFIG_TCC_BCHECK
6578 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
6582 loc
= (loc
- size
) & -align
;
6584 #ifdef CONFIG_TCC_BCHECK
6585 /* handles bounds */
6586 /* XXX: currently, since we do only one pass, we cannot track
6587 '&' operators, so we add only arrays */
6588 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
6590 /* add padding between regions */
6592 /* then add local bound info */
6593 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(addr_t
));
6594 bounds_ptr
[0] = addr
;
6595 bounds_ptr
[1] = size
;
6599 /* local variable */
6600 #ifdef CONFIG_TCC_ASM
6601 if (ad
->asm_label
) {
6602 int reg
= asm_parse_regvar(ad
->asm_label
);
6604 r
= (r
& ~VT_VALMASK
) | reg
;
6607 sym_push(v
, type
, r
, addr
);
6609 /* push local reference */
6610 vset(type
, r
, addr
);
6616 if (v
&& scope
== VT_CONST
) {
6617 /* see if the symbol was already defined */
6620 if (!is_compatible_types(&sym
->type
, type
))
6621 tcc_error("incompatible types for redefinition of '%s'",
6622 get_tok_str(v
, NULL
));
6623 if (sym
->type
.t
& VT_EXTERN
) {
6624 /* if the variable is extern, it was not allocated */
6625 sym
->type
.t
&= ~VT_EXTERN
;
6626 /* set array size if it was omitted in extern
6628 if ((sym
->type
.t
& VT_ARRAY
) &&
6629 sym
->type
.ref
->c
< 0 &&
6631 sym
->type
.ref
->c
= type
->ref
->c
;
6633 /* we accept several definitions of the same
6634 global variable. this is tricky, because we
6635 must play with the SHN_COMMON type of the symbol */
6636 /* XXX: should check if the variable was already
6637 initialized. It is incorrect to initialized it
6639 /* no init data, we won't add more to the symbol */
6646 /* allocate symbol in corresponding section */
6651 else if (tcc_state
->nocommon
)
6655 data_offset
= sec
->data_offset
;
6656 data_offset
= (data_offset
+ align
- 1) & -align
;
6658 /* very important to increment global pointer at this time
6659 because initializers themselves can create new initializers */
6660 data_offset
+= size
;
6661 #ifdef CONFIG_TCC_BCHECK
6662 /* add padding if bound check */
6663 if (tcc_state
->do_bounds_check
)
6666 sec
->data_offset
= data_offset
;
6667 /* allocate section space to put the data */
6668 if (sec
->sh_type
!= SHT_NOBITS
&&
6669 data_offset
> sec
->data_allocated
)
6670 section_realloc(sec
, data_offset
);
6671 /* align section if needed */
6672 if (align
> sec
->sh_addralign
)
6673 sec
->sh_addralign
= align
;
6675 addr
= 0; /* avoid warning */
6679 if (scope
!= VT_CONST
|| !sym
) {
6680 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
6681 sym
->asm_label
= ad
->asm_label
;
6683 /* update symbol definition */
6685 put_extern_sym(sym
, sec
, addr
, size
);
6688 /* put a common area */
6689 put_extern_sym(sym
, NULL
, align
, size
);
6690 /* XXX: find a nicer way */
6691 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
6692 esym
->st_shndx
= SHN_COMMON
;
6695 /* push global reference */
6696 sym
= get_sym_ref(type
, sec
, addr
, size
);
6697 vpushsym(type
, sym
);
6699 /* patch symbol weakness */
6700 if (type
->t
& VT_WEAK
)
6702 apply_visibility(sym
, type
);
6703 #ifdef CONFIG_TCC_BCHECK
6704 /* handles bounds now because the symbol must be defined
6705 before for the relocation */
6706 if (tcc_state
->do_bounds_check
) {
6709 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
6710 /* then add global bound info */
6711 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(addr_t
));
6712 bounds_ptr
[0] = 0; /* relocated */
6713 bounds_ptr
[1] = size
;
6717 if (type
->t
& VT_VLA
) {
6720 /* save current stack pointer */
6721 if (vlas_in_scope
== 0) {
6722 if (vla_sp_root_loc
== -1)
6723 vla_sp_root_loc
= (loc
-= PTR_SIZE
);
6724 gen_vla_sp_save(vla_sp_root_loc
);
6727 vla_runtime_type_size(type
, &a
);
6728 gen_vla_alloc(type
, a
);
6729 gen_vla_sp_save(addr
);
6732 } else if (has_init
) {
6733 size_t oldreloc_offset
= 0;
6734 if (sec
&& sec
->reloc
)
6735 oldreloc_offset
= sec
->reloc
->data_offset
;
6736 decl_initializer(type
, sec
, addr
, 1, 0);
6737 if (sec
&& sec
->reloc
)
6738 squeeze_multi_relocs(sec
, oldreloc_offset
);
6739 /* patch flexible array member size back to -1, */
6740 /* for possible subsequent similar declarations */
6742 flexible_array
->type
.ref
->c
= -1;
6745 /* restore parse state if needed */
6748 restore_parse_state(&saved_parse_state
);
6752 static void put_func_debug(Sym
*sym
)
6757 /* XXX: we put here a dummy type */
6758 snprintf(buf
, sizeof(buf
), "%s:%c1",
6759 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
6760 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
6761 cur_text_section
, sym
->c
);
6762 /* //gr gdb wants a line at the function */
6763 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
6768 /* parse an old style function declaration list */
6769 /* XXX: check multiple parameter */
6770 static void func_decl_list(Sym
*func_sym
)
6777 /* parse each declaration */
6778 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
&&
6779 tok
!= TOK_ASM1
&& tok
!= TOK_ASM2
&& tok
!= TOK_ASM3
) {
6780 if (!parse_btype(&btype
, &ad
))
6781 expect("declaration list");
6782 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
6783 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
6785 /* we accept no variable after */
6789 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
6790 /* find parameter in function parameter list */
6793 if ((s
->v
& ~SYM_FIELD
) == v
)
6797 tcc_error("declaration for parameter '%s' but no such parameter",
6798 get_tok_str(v
, NULL
));
6800 /* check that no storage specifier except 'register' was given */
6801 if (type
.t
& VT_STORAGE
)
6802 tcc_error("storage class specified for '%s'", get_tok_str(v
, NULL
));
6803 convert_parameter_type(&type
);
6804 /* we can add the type (NOTE: it could be local to the function) */
6806 /* accept other parameters */
6817 /* parse a function defined by symbol 'sym' and generate its code in
6818 'cur_text_section' */
6819 static void gen_function(Sym
*sym
)
6822 ind
= cur_text_section
->data_offset
;
6823 /* NOTE: we patch the symbol size later */
6824 put_extern_sym(sym
, cur_text_section
, ind
, 0);
6825 funcname
= get_tok_str(sym
->v
, NULL
);
6827 /* Initialize VLA state */
6829 vla_sp_root_loc
= -1;
6830 /* put debug symbol */
6831 if (tcc_state
->do_debug
)
6832 put_func_debug(sym
);
6834 /* push a dummy symbol to enable local sym storage */
6835 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
6836 local_scope
= 1; /* for function parameters */
6837 gfunc_prolog(&sym
->type
);
6841 block(NULL
, NULL
, 0);
6845 cur_text_section
->data_offset
= ind
;
6846 label_pop(&global_label_stack
, NULL
);
6847 /* reset local stack */
6849 sym_pop(&local_stack
, NULL
, 0);
6850 /* end of function */
6851 /* patch symbol size */
6852 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
6854 /* patch symbol weakness (this definition overrules any prototype) */
6855 if (sym
->type
.t
& VT_WEAK
)
6857 apply_visibility(sym
, &sym
->type
);
6858 if (tcc_state
->do_debug
) {
6859 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
6861 /* It's better to crash than to generate wrong code */
6862 cur_text_section
= NULL
;
6863 funcname
= ""; /* for safety */
6864 func_vt
.t
= VT_VOID
; /* for safety */
6865 func_var
= 0; /* for safety */
6866 ind
= 0; /* for safety */
6871 static void gen_inline_functions(TCCState
*s
)
6874 int inline_generated
, i
, ln
;
6875 struct InlineFunc
*fn
;
6877 ln
= file
->line_num
;
6878 /* iterate while inline function are referenced */
6880 inline_generated
= 0;
6881 for (i
= 0; i
< s
->nb_inline_fns
; ++i
) {
6882 fn
= s
->inline_fns
[i
];
6884 if (sym
&& sym
->c
) {
6885 /* the function was used: generate its code and
6886 convert it to a normal function */
6889 pstrcpy(file
->filename
, sizeof file
->filename
, fn
->filename
);
6890 sym
->r
= VT_SYM
| VT_CONST
;
6891 sym
->type
.t
&= ~VT_INLINE
;
6893 begin_macro(fn
->func_str
, 1);
6895 cur_text_section
= text_section
;
6899 inline_generated
= 1;
6902 if (!inline_generated
)
6905 file
->line_num
= ln
;
6908 ST_FUNC
void free_inline_functions(TCCState
*s
)
6911 /* free tokens of unused inline functions */
6912 for (i
= 0; i
< s
->nb_inline_fns
; ++i
) {
6913 struct InlineFunc
*fn
= s
->inline_fns
[i
];
6915 tok_str_free(fn
->func_str
);
6917 dynarray_reset(&s
->inline_fns
, &s
->nb_inline_fns
);
6920 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
6921 static int decl0(int l
, int is_for_loop_init
)
6929 if (!parse_btype(&btype
, &ad
)) {
6930 if (is_for_loop_init
)
6932 /* skip redundant ';' */
6933 /* XXX: find more elegant solution */
6938 if (l
== VT_CONST
&&
6939 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
6940 /* global asm block */
6944 /* special test for old K&R protos without explicit int
6945 type. Only accepted when defining global data */
6946 if (l
== VT_LOCAL
|| tok
< TOK_UIDENT
)
6950 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
6951 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
6953 if ((btype
.t
& VT_BTYPE
) == VT_STRUCT
) {
6954 int v
= btype
.ref
->v
;
6955 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) >= SYM_FIRST_ANOM
)
6956 tcc_warning("unnamed struct/union that defines no instances");
6961 while (1) { /* iterate thru each declaration */
6963 /* If the base type itself was an array type of unspecified
6964 size (like in 'typedef int arr[]; arr x = {1};') then
6965 we will overwrite the unknown size by the real one for
6966 this decl. We need to unshare the ref symbol holding
6968 if ((type
.t
& VT_ARRAY
) && type
.ref
->c
< 0) {
6969 type
.ref
= sym_push(SYM_FIELD
, &type
.ref
->type
, 0, type
.ref
->c
);
6971 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
6975 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
6976 printf("type = '%s'\n", buf
);
6979 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
6980 if ((type
.t
& VT_STATIC
) && (l
== VT_LOCAL
)) {
6981 tcc_error("function without file scope cannot be static");
6983 /* if old style function prototype, we accept a
6986 if (sym
->c
== FUNC_OLD
)
6987 func_decl_list(sym
);
6990 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
6991 ad
.asm_label
= asm_label_instr();
6992 /* parse one last attribute list, after asm label */
6993 parse_attribute(&ad
);
7000 #ifdef TCC_TARGET_PE
7001 if (ad
.a
.func_import
)
7002 type
.t
|= VT_IMPORT
;
7003 if (ad
.a
.func_export
)
7004 type
.t
|= VT_EXPORT
;
7006 type
.t
|= ad
.a
.visibility
<< VT_VIS_SHIFT
;
7010 tcc_error("cannot use local functions");
7011 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
7012 expect("function definition");
7014 /* reject abstract declarators in function definition */
7016 while ((sym
= sym
->next
) != NULL
)
7017 if (!(sym
->v
& ~SYM_FIELD
))
7018 expect("identifier");
7020 /* XXX: cannot do better now: convert extern line to static inline */
7021 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
7022 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
7027 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
7030 ref
= sym
->type
.ref
;
7031 if (0 == ref
->a
.func_proto
)
7032 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
7034 /* use func_call from prototype if not defined */
7035 if (ref
->a
.func_call
!= FUNC_CDECL
7036 && type
.ref
->a
.func_call
== FUNC_CDECL
)
7037 type
.ref
->a
.func_call
= ref
->a
.func_call
;
7039 /* use export from prototype */
7040 if (ref
->a
.func_export
)
7041 type
.ref
->a
.func_export
= 1;
7043 /* use static from prototype */
7044 if (sym
->type
.t
& VT_STATIC
)
7045 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
7047 /* If the definition has no visibility use the
7048 one from prototype. */
7049 if (! (type
.t
& VT_VIS_MASK
))
7050 type
.t
|= sym
->type
.t
& VT_VIS_MASK
;
7052 if (!is_compatible_types(&sym
->type
, &type
)) {
7054 tcc_error("incompatible types for redefinition of '%s'",
7055 get_tok_str(v
, NULL
));
7057 type
.ref
->a
.func_proto
= 0;
7058 /* if symbol is already defined, then put complete type */
7061 /* put function symbol */
7062 sym
= global_identifier_push(v
, type
.t
, 0);
7063 sym
->type
.ref
= type
.ref
;
7066 /* static inline functions are just recorded as a kind
7067 of macro. Their code will be emitted at the end of
7068 the compilation unit only if they are used */
7069 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
7070 (VT_INLINE
| VT_STATIC
)) {
7072 struct InlineFunc
*fn
;
7073 const char *filename
;
7075 filename
= file
? file
->filename
: "";
7076 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
7077 strcpy(fn
->filename
, filename
);
7079 fn
->func_str
= tok_str_alloc();
7085 tcc_error("unexpected end of file");
7086 tok_str_add_tok(fn
->func_str
);
7091 } else if (t
== '}') {
7093 if (block_level
== 0)
7097 tok_str_add(fn
->func_str
, -1);
7098 tok_str_add(fn
->func_str
, 0);
7099 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
7102 /* compute text section */
7103 cur_text_section
= ad
.section
;
7104 if (!cur_text_section
)
7105 cur_text_section
= text_section
;
7106 sym
->r
= VT_SYM
| VT_CONST
;
7111 if (btype
.t
& VT_TYPEDEF
) {
7112 /* save typedefed type */
7113 /* XXX: test storage specifiers ? */
7115 if (sym
&& sym
->scope
== local_scope
) {
7116 if (!is_compatible_types(&sym
->type
, &type
)
7117 || !(sym
->type
.t
& VT_TYPEDEF
))
7118 tcc_error("incompatible redefinition of '%s'",
7119 get_tok_str(v
, NULL
));
7122 sym
= sym_push(v
, &type
, 0, 0);
7125 sym
->type
.t
|= VT_TYPEDEF
;
7128 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
7129 /* external function definition */
7130 /* specific case for func_call attribute */
7131 ad
.a
.func_proto
= 1;
7133 } else if (!(type
.t
& VT_ARRAY
)) {
7134 /* not lvalue if array */
7135 r
|= lvalue_type(type
.t
);
7137 has_init
= (tok
== '=');
7138 if (has_init
&& (type
.t
& VT_VLA
))
7139 tcc_error("variable length array cannot be initialized");
7140 if ((btype
.t
& VT_EXTERN
) || ((type
.t
& VT_BTYPE
) == VT_FUNC
) ||
7141 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
7142 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
7143 /* external variable or function */
7144 /* NOTE: as GCC, uninitialized global static
7145 arrays of null size are considered as
7147 sym
= external_sym(v
, &type
, r
);
7148 sym
->asm_label
= ad
.asm_label
;
7150 if (ad
.alias_target
) {
7155 alias_target
= sym_find(ad
.alias_target
);
7156 if (!alias_target
|| !alias_target
->c
)
7157 tcc_error("unsupported forward __alias__ attribute");
7158 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[alias_target
->c
];
7159 tsec
.sh_num
= esym
->st_shndx
;
7160 put_extern_sym2(sym
, &tsec
, esym
->st_value
, esym
->st_size
, 0);
7163 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
7164 if (type
.t
& VT_STATIC
)
7170 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, l
);
7174 if (is_for_loop_init
)
7187 ST_FUNC
void decl(int l
)
7192 /* ------------------------------------------------------------------------- */