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
{
66 } **p
; int n
; /* list of case ranges */
67 int def_sym
; /* default symbol */
68 } *cur_switch
; /* current switch */
70 /* ------------------------------------------------------------------------- */
71 static void gen_cast(CType
*type
);
72 static inline CType
*pointed_type(CType
*type
);
73 static int is_compatible_types(CType
*type1
, CType
*type2
);
74 static int parse_btype(CType
*type
, AttributeDef
*ad
);
75 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
);
76 static void parse_expr_type(CType
*type
);
77 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
, int first
, int size_only
);
78 static void block(int *bsym
, int *csym
, int is_expr
);
79 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
, int has_init
, int v
, int scope
);
80 static int decl0(int l
, int is_for_loop_init
);
81 static void expr_eq(void);
82 static void expr_lor_const(void);
83 static void unary_type(CType
*type
);
84 static void vla_runtime_type_size(CType
*type
, int *a
);
85 static void vla_sp_restore(void);
86 static void vla_sp_restore_root(void);
87 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
);
88 static void expr_type(CType
*type
);
89 ST_FUNC
void vpush64(int ty
, unsigned long long v
);
90 ST_FUNC
void vpush(CType
*type
);
91 ST_FUNC
int gvtst(int inv
, int t
);
92 ST_FUNC
int is_btype_size(int bt
);
93 static void gen_inline_functions(TCCState
*s
);
95 ST_INLN
int is_float(int t
)
99 return bt
== VT_LDOUBLE
|| bt
== VT_DOUBLE
|| bt
== VT_FLOAT
|| bt
== VT_QFLOAT
;
102 /* we use our own 'finite' function to avoid potential problems with
103 non standard math libs */
104 /* XXX: endianness dependent */
105 ST_FUNC
int ieee_finite(double d
)
108 memcpy(p
, &d
, sizeof(double));
109 return ((unsigned)((p
[1] | 0x800fffff) + 1)) >> 31;
112 ST_FUNC
void test_lvalue(void)
114 if (!(vtop
->r
& VT_LVAL
))
118 ST_FUNC
void check_vstack(void)
121 tcc_error("internal compiler error: vstack leak (%d)", vtop
- pvtop
);
124 /* ------------------------------------------------------------------------- */
125 /* vstack debugging aid */
128 void pv (const char *lbl
, int a
, int b
)
131 for (i
= a
; i
< a
+ b
; ++i
) {
132 SValue
*p
= &vtop
[-i
];
133 printf("%s vtop[-%d] : type.t:%04x r:%04x r2:%04x c.i:%d\n",
134 lbl
, i
, p
->type
.t
, p
->r
, p
->r2
, (int)p
->c
.i
);
139 /* ------------------------------------------------------------------------- */
140 ST_FUNC
void tccgen_start(TCCState
*s1
)
142 cur_text_section
= NULL
;
144 anon_sym
= SYM_FIRST_ANOM
;
148 /* define some often used types */
150 char_pointer_type
.t
= VT_BYTE
;
151 mk_pointer(&char_pointer_type
);
153 size_type
.t
= VT_INT
;
155 size_type
.t
= VT_LLONG
;
157 func_old_type
.t
= VT_FUNC
;
158 func_old_type
.ref
= sym_push(SYM_FIELD
, &int_type
, FUNC_CDECL
, FUNC_OLD
);
163 /* file info: full path + filename */
164 section_sym
= put_elf_sym(symtab_section
, 0, 0,
165 ELFW(ST_INFO
)(STB_LOCAL
, STT_SECTION
), 0,
166 text_section
->sh_num
, NULL
);
167 getcwd(buf
, sizeof(buf
));
169 normalize_slashes(buf
);
171 pstrcat(buf
, sizeof(buf
), "/");
172 put_stabs_r(buf
, N_SO
, 0, 0,
173 text_section
->data_offset
, text_section
, section_sym
);
174 put_stabs_r(file
->filename
, N_SO
, 0, 0,
175 text_section
->data_offset
, text_section
, section_sym
);
177 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
178 symbols can be safely used */
179 put_elf_sym(symtab_section
, 0, 0,
180 ELFW(ST_INFO
)(STB_LOCAL
, STT_FILE
), 0,
181 SHN_ABS
, file
->filename
);
183 #ifdef TCC_TARGET_ARM
188 ST_FUNC
void tccgen_end(TCCState
*s1
)
190 gen_inline_functions(s1
);
192 /* end of translation unit info */
194 put_stabs_r(NULL
, N_SO
, 0, 0,
195 text_section
->data_offset
, text_section
, section_sym
);
199 /* ------------------------------------------------------------------------- */
200 /* update sym->c so that it points to an external symbol in section
201 'section' with value 'value' */
203 ST_FUNC
void put_extern_sym2(Sym
*sym
, Section
*section
,
204 addr_t value
, unsigned long size
,
205 int can_add_underscore
)
207 int sym_type
, sym_bind
, sh_num
, info
, other
;
212 #ifdef CONFIG_TCC_BCHECK
218 else if (section
== SECTION_ABS
)
221 sh_num
= section
->sh_num
;
223 if ((sym
->type
.t
& VT_BTYPE
) == VT_FUNC
) {
225 } else if ((sym
->type
.t
& VT_BTYPE
) == VT_VOID
) {
226 sym_type
= STT_NOTYPE
;
228 sym_type
= STT_OBJECT
;
231 if (sym
->type
.t
& VT_STATIC
)
232 sym_bind
= STB_LOCAL
;
234 if (sym
->type
.t
& VT_WEAK
)
237 sym_bind
= STB_GLOBAL
;
241 name
= get_tok_str(sym
->v
, NULL
);
242 #ifdef CONFIG_TCC_BCHECK
243 if (tcc_state
->do_bounds_check
) {
244 /* XXX: avoid doing that for statics ? */
245 /* if bound checking is activated, we change some function
246 names by adding the "__bound" prefix */
249 /* XXX: we rely only on malloc hooks */
262 strcpy(buf
, "__bound_");
272 if (sym
->type
.t
& VT_EXPORT
)
273 other
|= ST_PE_EXPORT
;
274 if (sym_type
== STT_FUNC
&& sym
->type
.ref
) {
275 Sym
*ref
= sym
->type
.ref
;
276 if (ref
->a
.func_export
)
277 other
|= ST_PE_EXPORT
;
278 if (ref
->a
.func_call
== FUNC_STDCALL
&& can_add_underscore
) {
279 sprintf(buf1
, "_%s@%d", name
, ref
->a
.func_args
* PTR_SIZE
);
281 other
|= ST_PE_STDCALL
;
282 can_add_underscore
= 0;
285 if (find_elf_sym(tcc_state
->dynsymtab_section
, name
))
286 other
|= ST_PE_IMPORT
;
287 if (sym
->type
.t
& VT_IMPORT
)
288 other
|= ST_PE_IMPORT
;
291 if (! (sym
->type
.t
& VT_STATIC
))
292 other
= (sym
->type
.t
& VT_VIS_MASK
) >> VT_VIS_SHIFT
;
294 if (tcc_state
->leading_underscore
&& can_add_underscore
) {
296 pstrcpy(buf1
+ 1, sizeof(buf1
) - 1, name
);
299 if (sym
->asm_label
) {
300 name
= get_tok_str(sym
->asm_label
, NULL
);
302 info
= ELFW(ST_INFO
)(sym_bind
, sym_type
);
303 sym
->c
= set_elf_sym(symtab_section
, value
, size
, info
, other
, sh_num
, name
);
305 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
306 esym
->st_value
= value
;
307 esym
->st_size
= size
;
308 esym
->st_shndx
= sh_num
;
312 ST_FUNC
void put_extern_sym(Sym
*sym
, Section
*section
,
313 addr_t value
, unsigned long size
)
315 put_extern_sym2(sym
, section
, value
, size
, 1);
318 /* add a new relocation entry to symbol 'sym' in section 's' */
319 ST_FUNC
void greloca(Section
*s
, Sym
*sym
, unsigned long offset
, int type
,
325 put_extern_sym(sym
, NULL
, 0, 0);
328 /* now we can add ELF relocation info */
329 put_elf_reloca(symtab_section
, s
, offset
, type
, c
, addend
);
332 ST_FUNC
void greloc(Section
*s
, Sym
*sym
, unsigned long offset
, int type
)
334 greloca(s
, sym
, offset
, type
, 0);
337 /* ------------------------------------------------------------------------- */
338 /* symbol allocator */
339 static Sym
*__sym_malloc(void)
341 Sym
*sym_pool
, *sym
, *last_sym
;
344 sym_pool
= tcc_malloc(SYM_POOL_NB
* sizeof(Sym
));
345 dynarray_add(&sym_pools
, &nb_sym_pools
, sym_pool
);
347 last_sym
= sym_free_first
;
349 for(i
= 0; i
< SYM_POOL_NB
; i
++) {
350 sym
->next
= last_sym
;
354 sym_free_first
= last_sym
;
358 static inline Sym
*sym_malloc(void)
362 sym
= sym_free_first
;
364 sym
= __sym_malloc();
365 sym_free_first
= sym
->next
;
368 sym
= tcc_malloc(sizeof(Sym
));
373 ST_INLN
void sym_free(Sym
*sym
)
376 sym
->next
= sym_free_first
;
377 sym_free_first
= sym
;
383 /* push, without hashing */
384 ST_FUNC Sym
*sym_push2(Sym
**ps
, int v
, int t
, long c
)
404 /* find a symbol and return its associated structure. 's' is the top
405 of the symbol stack */
406 ST_FUNC Sym
*sym_find2(Sym
*s
, int v
)
418 /* structure lookup */
419 ST_INLN Sym
*struct_find(int v
)
422 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
424 return table_ident
[v
]->sym_struct
;
427 /* find an identifier */
428 ST_INLN Sym
*sym_find(int v
)
431 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
433 return table_ident
[v
]->sym_identifier
;
436 /* push a given symbol on the symbol stack */
437 ST_FUNC Sym
*sym_push(int v
, CType
*type
, int r
, int c
)
446 s
= sym_push2(ps
, v
, type
->t
, c
);
447 s
->type
.ref
= type
->ref
;
449 /* don't record fields or anonymous symbols */
451 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
452 /* record symbol in token array */
453 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
455 ps
= &ts
->sym_struct
;
457 ps
= &ts
->sym_identifier
;
460 s
->scope
= local_scope
;
461 if (s
->prev_tok
&& s
->prev_tok
->scope
== s
->scope
)
462 tcc_error("redeclaration of '%s'",
463 get_tok_str(v
& ~SYM_STRUCT
, NULL
));
468 /* push a global identifier */
469 ST_FUNC Sym
*global_identifier_push(int v
, int t
, int c
)
472 s
= sym_push2(&global_stack
, v
, t
, c
);
473 /* don't record anonymous symbol */
474 if (v
< SYM_FIRST_ANOM
) {
475 ps
= &table_ident
[v
- TOK_IDENT
]->sym_identifier
;
476 /* modify the top most local identifier, so that
477 sym_identifier will point to 's' when popped */
479 ps
= &(*ps
)->prev_tok
;
486 /* pop symbols until top reaches 'b'. If KEEP is non-zero don't really
487 pop them yet from the list, but do remove them from the token array. */
488 ST_FUNC
void sym_pop(Sym
**ptop
, Sym
*b
, int keep
)
498 /* remove symbol in token array */
500 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
501 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
503 ps
= &ts
->sym_struct
;
505 ps
= &ts
->sym_identifier
;
516 static void weaken_symbol(Sym
*sym
)
518 sym
->type
.t
|= VT_WEAK
;
523 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
524 esym_type
= ELFW(ST_TYPE
)(esym
->st_info
);
525 esym
->st_info
= ELFW(ST_INFO
)(STB_WEAK
, esym_type
);
529 static void apply_visibility(Sym
*sym
, CType
*type
)
531 int vis
= sym
->type
.t
& VT_VIS_MASK
;
532 int vis2
= type
->t
& VT_VIS_MASK
;
533 if (vis
== (STV_DEFAULT
<< VT_VIS_SHIFT
))
535 else if (vis2
== (STV_DEFAULT
<< VT_VIS_SHIFT
))
538 vis
= (vis
< vis2
) ? vis
: vis2
;
539 sym
->type
.t
&= ~VT_VIS_MASK
;
545 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
546 vis
>>= VT_VIS_SHIFT
;
547 esym
->st_other
= (esym
->st_other
& ~ELFW(ST_VISIBILITY
)(-1)) | vis
;
551 /* ------------------------------------------------------------------------- */
553 ST_FUNC
void swap(int *p
, int *q
)
561 static void vsetc(CType
*type
, int r
, CValue
*vc
)
565 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
566 tcc_error("memory full (vstack)");
567 /* cannot let cpu flags if other instruction are generated. Also
568 avoid leaving VT_JMP anywhere except on the top of the stack
569 because it would complicate the code generator. */
570 if (vtop
>= vstack
) {
571 v
= vtop
->r
& VT_VALMASK
;
572 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
582 /* push constant of type "type" with useless value */
583 ST_FUNC
void vpush(CType
*type
)
586 vsetc(type
, VT_CONST
, &cval
);
589 /* push integer constant */
590 ST_FUNC
void vpushi(int v
)
594 vsetc(&int_type
, VT_CONST
, &cval
);
597 /* push a pointer sized constant */
598 static void vpushs(addr_t v
)
602 vsetc(&size_type
, VT_CONST
, &cval
);
605 /* push arbitrary 64bit constant */
606 ST_FUNC
void vpush64(int ty
, unsigned long long v
)
613 vsetc(&ctype
, VT_CONST
, &cval
);
616 /* push long long constant */
617 static inline void vpushll(long long v
)
619 vpush64(VT_LLONG
, v
);
622 /* push a symbol value of TYPE */
623 static inline void vpushsym(CType
*type
, Sym
*sym
)
627 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
631 /* Return a static symbol pointing to a section */
632 ST_FUNC Sym
*get_sym_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
638 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
639 sym
->type
.ref
= type
->ref
;
640 sym
->r
= VT_CONST
| VT_SYM
;
641 put_extern_sym(sym
, sec
, offset
, size
);
645 /* push a reference to a section offset by adding a dummy symbol */
646 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
648 vpushsym(type
, get_sym_ref(type
, sec
, offset
, size
));
651 /* define a new external reference to a symbol 'v' of type 'u' */
652 ST_FUNC Sym
*external_global_sym(int v
, CType
*type
, int r
)
658 /* push forward reference */
659 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
660 s
->type
.ref
= type
->ref
;
661 s
->r
= r
| VT_CONST
| VT_SYM
;
666 /* define a new external reference to a symbol 'v' */
667 static Sym
*external_sym(int v
, CType
*type
, int r
)
673 /* push forward reference */
674 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
675 s
->type
.t
|= VT_EXTERN
;
676 } else if (s
->type
.ref
== func_old_type
.ref
) {
677 s
->type
.ref
= type
->ref
;
678 s
->r
= r
| VT_CONST
| VT_SYM
;
679 s
->type
.t
|= VT_EXTERN
;
680 } else if (!is_compatible_types(&s
->type
, type
)) {
681 tcc_error("incompatible types for redefinition of '%s'",
682 get_tok_str(v
, NULL
));
684 /* Merge some storage attributes. */
685 if (type
->t
& VT_WEAK
)
688 if (type
->t
& VT_VIS_MASK
)
689 apply_visibility(s
, type
);
694 /* push a reference to global symbol v */
695 ST_FUNC
void vpush_global_sym(CType
*type
, int v
)
697 vpushsym(type
, external_global_sym(v
, type
, 0));
700 ST_FUNC
void vset(CType
*type
, int r
, int v
)
705 vsetc(type
, r
, &cval
);
708 static void vseti(int r
, int v
)
716 ST_FUNC
void vswap(void)
719 /* cannot let cpu flags if other instruction are generated. Also
720 avoid leaving VT_JMP anywhere except on the top of the stack
721 because it would complicate the code generator. */
722 if (vtop
>= vstack
) {
723 int v
= vtop
->r
& VT_VALMASK
;
724 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
731 /* XXX: +2% overall speed possible with optimized memswap
733 * memswap(&vtop[0], &vtop[1], sizeof *vtop);
737 ST_FUNC
void vpushv(SValue
*v
)
739 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
740 tcc_error("memory full (vstack)");
745 static void vdup(void)
750 /* save registers up to (vtop - n) stack entry */
751 ST_FUNC
void save_regs(int n
)
754 for(p
= vstack
, p1
= vtop
- n
; p
<= p1
; p
++)
758 /* save r to the memory stack, and mark it as being free */
759 ST_FUNC
void save_reg(int r
)
761 save_reg_upstack(r
, 0);
764 /* save r to the memory stack, and mark it as being free,
765 if seen up to (vtop - n) stack entry */
766 ST_FUNC
void save_reg_upstack(int r
, int n
)
768 int l
, saved
, size
, align
;
772 if ((r
&= VT_VALMASK
) >= VT_CONST
)
775 /* modify all stack values */
778 for(p
= vstack
, p1
= vtop
- n
; p
<= p1
; p
++) {
779 if ((p
->r
& VT_VALMASK
) == r
||
780 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
781 /* must save value on stack if not already done */
783 /* NOTE: must reload 'r' because r might be equal to r2 */
784 r
= p
->r
& VT_VALMASK
;
785 /* store register in the stack */
787 if ((p
->r
& VT_LVAL
) ||
788 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
789 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
790 type
= &char_pointer_type
;
794 size
= type_size(type
, &align
);
795 loc
= (loc
- size
) & -align
;
797 sv
.r
= VT_LOCAL
| VT_LVAL
;
800 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
801 /* x86 specific: need to pop fp register ST0 if saved */
803 o(0xd8dd); /* fstp %st(0) */
806 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
807 /* special long long case */
808 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
816 /* mark that stack entry as being saved on the stack */
817 if (p
->r
& VT_LVAL
) {
818 /* also clear the bounded flag because the
819 relocation address of the function was stored in
821 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
823 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
831 #ifdef TCC_TARGET_ARM
832 /* find a register of class 'rc2' with at most one reference on stack.
833 * If none, call get_reg(rc) */
834 ST_FUNC
int get_reg_ex(int rc
, int rc2
)
839 for(r
=0;r
<NB_REGS
;r
++) {
840 if (reg_classes
[r
] & rc2
) {
843 for(p
= vstack
; p
<= vtop
; p
++) {
844 if ((p
->r
& VT_VALMASK
) == r
||
845 (p
->r2
& VT_VALMASK
) == r
)
856 /* find a free register of class 'rc'. If none, save one register */
857 ST_FUNC
int get_reg(int rc
)
862 /* find a free register */
863 for(r
=0;r
<NB_REGS
;r
++) {
864 if (reg_classes
[r
] & rc
) {
865 for(p
=vstack
;p
<=vtop
;p
++) {
866 if ((p
->r
& VT_VALMASK
) == r
||
867 (p
->r2
& VT_VALMASK
) == r
)
875 /* no register left : free the first one on the stack (VERY
876 IMPORTANT to start from the bottom to ensure that we don't
877 spill registers used in gen_opi()) */
878 for(p
=vstack
;p
<=vtop
;p
++) {
879 /* look at second register (if long long) */
880 r
= p
->r2
& VT_VALMASK
;
881 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
883 r
= p
->r
& VT_VALMASK
;
884 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
890 /* Should never comes here */
894 /* move register 's' (of type 't') to 'r', and flush previous value of r to memory
896 static void move_reg(int r
, int s
, int t
)
910 /* get address of vtop (vtop MUST BE an lvalue) */
911 ST_FUNC
void gaddrof(void)
913 if (vtop
->r
& VT_REF
&& !nocode_wanted
)
916 /* tricky: if saved lvalue, then we can go back to lvalue */
917 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
918 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
923 #ifdef CONFIG_TCC_BCHECK
924 /* generate lvalue bound code */
925 static void gbound(void)
930 vtop
->r
&= ~VT_MUSTBOUND
;
931 /* if lvalue, then use checking code before dereferencing */
932 if (vtop
->r
& VT_LVAL
) {
933 /* if not VT_BOUNDED value, then make one */
934 if (!(vtop
->r
& VT_BOUNDED
)) {
935 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
936 /* must save type because we must set it to int to get pointer */
938 vtop
->type
.t
= VT_PTR
;
941 gen_bounded_ptr_add();
942 vtop
->r
|= lval_type
;
945 /* then check for dereferencing */
946 gen_bounded_ptr_deref();
951 /* store vtop a register belonging to class 'rc'. lvalues are
952 converted to values. Cannot be used if cannot be converted to
953 register value (such as structures). */
954 ST_FUNC
int gv(int rc
)
956 int r
, bit_pos
, bit_size
, size
, align
, i
;
959 /* NOTE: get_reg can modify vstack[] */
960 if (vtop
->type
.t
& VT_BITFIELD
) {
963 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
964 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
965 /* remove bit field info to avoid loops */
966 vtop
->type
.t
&= ~VT_BITFIELD
& ((1 << VT_STRUCT_SHIFT
) - 1);
967 /* cast to int to propagate signedness in following ops */
968 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
973 if((vtop
->type
.t
& VT_UNSIGNED
) ||
974 (vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
975 type
.t
|= VT_UNSIGNED
;
977 /* generate shifts */
978 vpushi(bits
- (bit_pos
+ bit_size
));
980 vpushi(bits
- bit_size
);
981 /* NOTE: transformed to SHR if unsigned */
985 if (is_float(vtop
->type
.t
) &&
986 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
989 unsigned long offset
;
990 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
994 /* XXX: unify with initializers handling ? */
995 /* CPUs usually cannot use float constants, so we store them
996 generically in data segment */
997 size
= type_size(&vtop
->type
, &align
);
998 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
999 data_section
->data_offset
= offset
;
1000 /* XXX: not portable yet */
1001 #if defined(__i386__) || defined(__x86_64__)
1002 /* Zero pad x87 tenbyte long doubles */
1003 if (size
== LDOUBLE_SIZE
) {
1004 vtop
->c
.tab
[2] &= 0xffff;
1005 #if LDOUBLE_SIZE == 16
1010 ptr
= section_ptr_add(data_section
, size
);
1012 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
1016 ptr
[i
] = vtop
->c
.tab
[size
-1-i
];
1020 ptr
[i
] = vtop
->c
.tab
[i
];
1021 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
1022 vtop
->r
|= VT_LVAL
| VT_SYM
;
1026 #ifdef CONFIG_TCC_BCHECK
1027 if (vtop
->r
& VT_MUSTBOUND
)
1031 r
= vtop
->r
& VT_VALMASK
;
1032 rc2
= (rc
& RC_FLOAT
) ? RC_FLOAT
: RC_INT
;
1033 #ifndef TCC_TARGET_ARM64
1036 #ifdef TCC_TARGET_X86_64
1037 else if (rc
== RC_FRET
)
1042 /* need to reload if:
1044 - lvalue (need to dereference pointer)
1045 - already a register, but not in the right class */
1047 || (vtop
->r
& VT_LVAL
)
1048 || !(reg_classes
[r
] & rc
)
1049 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1050 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
1051 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
&& !(reg_classes
[vtop
->r2
] & rc2
))
1053 || ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
1058 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1059 if (((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
)) {
1060 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
1062 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
1063 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
1064 unsigned long long ll
;
1066 int r2
, original_type
;
1067 original_type
= vtop
->type
.t
;
1068 /* two register type load : expand to two words
1070 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
1071 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
1074 vtop
->c
.i
= ll
; /* first word */
1076 vtop
->r
= r
; /* save register value */
1077 vpushi(ll
>> 32); /* second word */
1080 if (vtop
->r
& VT_LVAL
) {
1081 /* We do not want to modifier the long long
1082 pointer here, so the safest (and less
1083 efficient) is to save all the other registers
1084 in the stack. XXX: totally inefficient. */
1088 /* lvalue_save: save only if used further down the stack */
1089 save_reg_upstack(vtop
->r
, 1);
1091 /* load from memory */
1092 vtop
->type
.t
= load_type
;
1095 vtop
[-1].r
= r
; /* save register value */
1096 /* increment pointer to get second word */
1097 vtop
->type
.t
= addr_type
;
1102 vtop
->type
.t
= load_type
;
1104 /* move registers */
1107 vtop
[-1].r
= r
; /* save register value */
1108 vtop
->r
= vtop
[-1].r2
;
1110 /* Allocate second register. Here we rely on the fact that
1111 get_reg() tries first to free r2 of an SValue. */
1115 /* write second register */
1117 vtop
->type
.t
= original_type
;
1118 } else if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
1120 /* lvalue of scalar type : need to use lvalue type
1121 because of possible cast */
1124 /* compute memory access type */
1125 if (vtop
->r
& VT_REF
)
1126 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1131 else if (vtop
->r
& VT_LVAL_BYTE
)
1133 else if (vtop
->r
& VT_LVAL_SHORT
)
1135 if (vtop
->r
& VT_LVAL_UNSIGNED
)
1139 /* restore wanted type */
1142 /* one register type load */
1147 #ifdef TCC_TARGET_C67
1148 /* uses register pairs for doubles */
1149 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
1156 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
1157 ST_FUNC
void gv2(int rc1
, int rc2
)
1161 /* generate more generic register first. But VT_JMP or VT_CMP
1162 values must be generated first in all cases to avoid possible
1164 v
= vtop
[0].r
& VT_VALMASK
;
1165 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
1170 /* test if reload is needed for first register */
1171 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
1181 /* test if reload is needed for first register */
1182 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
1188 #ifndef TCC_TARGET_ARM64
1189 /* wrapper around RC_FRET to return a register by type */
1190 static int rc_fret(int t
)
1192 #ifdef TCC_TARGET_X86_64
1193 if (t
== VT_LDOUBLE
) {
1201 /* wrapper around REG_FRET to return a register by type */
1202 static int reg_fret(int t
)
1204 #ifdef TCC_TARGET_X86_64
1205 if (t
== VT_LDOUBLE
) {
1212 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
1213 /* expand 64bit on stack in two ints */
1214 static void lexpand(void)
1217 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
1218 v
= vtop
->r
& (VT_VALMASK
| VT_LVAL
);
1219 if (v
== VT_CONST
) {
1222 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
1228 vtop
[0].r
= vtop
[-1].r2
;
1229 vtop
[0].r2
= vtop
[-1].r2
= VT_CONST
;
1231 vtop
[0].type
.t
= vtop
[-1].type
.t
= VT_INT
| u
;
1235 #ifdef TCC_TARGET_ARM
1236 /* expand long long on stack */
1237 ST_FUNC
void lexpand_nr(void)
1241 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
1243 vtop
->r2
= VT_CONST
;
1244 vtop
->type
.t
= VT_INT
| u
;
1245 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
1246 if (v
== VT_CONST
) {
1247 vtop
[-1].c
.i
= vtop
->c
.i
;
1248 vtop
->c
.i
= vtop
->c
.i
>> 32;
1250 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
1252 vtop
->r
= vtop
[-1].r
;
1253 } else if (v
> VT_CONST
) {
1257 vtop
->r
= vtop
[-1].r2
;
1258 vtop
[-1].r2
= VT_CONST
;
1259 vtop
[-1].type
.t
= VT_INT
| u
;
1263 #if !defined(TCC_TARGET_X86_64) && !defined(TCC_TARGET_ARM64)
1264 /* build a long long from two ints */
1265 static void lbuild(int t
)
1267 gv2(RC_INT
, RC_INT
);
1268 vtop
[-1].r2
= vtop
[0].r
;
1269 vtop
[-1].type
.t
= t
;
1274 /* rotate n first stack elements to the bottom
1275 I1 ... In -> I2 ... In I1 [top is right]
1277 ST_FUNC
void vrotb(int n
)
1283 for(i
=-n
+1;i
!=0;i
++)
1284 vtop
[i
] = vtop
[i
+1];
1288 /* rotate the n elements before entry e towards the top
1289 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
1291 ST_FUNC
void vrote(SValue
*e
, int n
)
1297 for(i
= 0;i
< n
- 1; i
++)
1302 /* rotate n first stack elements to the top
1303 I1 ... In -> In I1 ... I(n-1) [top is right]
1305 ST_FUNC
void vrott(int n
)
1310 /* pop stack value */
1311 ST_FUNC
void vpop(void)
1314 v
= vtop
->r
& VT_VALMASK
;
1315 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1316 /* for x86, we need to pop the FP stack */
1317 if (v
== TREG_ST0
&& !nocode_wanted
) {
1318 o(0xd8dd); /* fstp %st(0) */
1321 if (v
== VT_JMP
|| v
== VT_JMPI
) {
1322 /* need to put correct jump if && or || without test */
1328 /* convert stack entry to register and duplicate its value in another
1330 static void gv_dup(void)
1336 #if !defined(TCC_TARGET_X86_64) && !defined(TCC_TARGET_ARM64)
1337 if ((t
& VT_BTYPE
) == VT_LLONG
) {
1344 /* stack: H L L1 H1 */
1354 /* duplicate value */
1359 #ifdef TCC_TARGET_X86_64
1360 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1370 load(r1
, &sv
); /* move r to r1 */
1372 /* duplicates value */
1378 /* Generate value test
1380 * Generate a test for any value (jump, comparison and integers) */
1381 ST_FUNC
int gvtst(int inv
, int t
)
1383 int v
= vtop
->r
& VT_VALMASK
;
1384 if (v
!= VT_CMP
&& v
!= VT_JMP
&& v
!= VT_JMPI
) {
1388 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1389 /* constant jmp optimization */
1390 if ((vtop
->c
.i
!= 0) != inv
)
1395 return gtst(inv
, t
);
1398 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
1399 /* generate CPU independent (unsigned) long long operations */
1400 static void gen_opl(int op
)
1402 int t
, a
, b
, op1
, c
, i
;
1404 unsigned short reg_iret
= REG_IRET
;
1405 unsigned short reg_lret
= REG_LRET
;
1411 func
= TOK___divdi3
;
1414 func
= TOK___udivdi3
;
1417 func
= TOK___moddi3
;
1420 func
= TOK___umoddi3
;
1427 /* call generic long long function */
1428 vpush_global_sym(&func_old_type
, func
);
1433 vtop
->r2
= reg_lret
;
1441 //pv("gen_opl A",0,2);
1447 /* stack: L1 H1 L2 H2 */
1452 vtop
[-2] = vtop
[-3];
1455 /* stack: H1 H2 L1 L2 */
1456 //pv("gen_opl B",0,4);
1462 /* stack: H1 H2 L1 L2 ML MH */
1465 /* stack: ML MH H1 H2 L1 L2 */
1469 /* stack: ML MH H1 L2 H2 L1 */
1474 /* stack: ML MH M1 M2 */
1477 } else if (op
== '+' || op
== '-') {
1478 /* XXX: add non carry method too (for MIPS or alpha) */
1484 /* stack: H1 H2 (L1 op L2) */
1487 gen_op(op1
+ 1); /* TOK_xxxC2 */
1490 /* stack: H1 H2 (L1 op L2) */
1493 /* stack: (L1 op L2) H1 H2 */
1495 /* stack: (L1 op L2) (H1 op H2) */
1503 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1504 t
= vtop
[-1].type
.t
;
1508 /* stack: L H shift */
1510 /* constant: simpler */
1511 /* NOTE: all comments are for SHL. the other cases are
1512 done by swaping words */
1523 if (op
!= TOK_SAR
) {
1556 /* XXX: should provide a faster fallback on x86 ? */
1559 func
= TOK___ashrdi3
;
1562 func
= TOK___lshrdi3
;
1565 func
= TOK___ashldi3
;
1571 /* compare operations */
1577 /* stack: L1 H1 L2 H2 */
1579 vtop
[-1] = vtop
[-2];
1581 /* stack: L1 L2 H1 H2 */
1584 /* when values are equal, we need to compare low words. since
1585 the jump is inverted, we invert the test too. */
1588 else if (op1
== TOK_GT
)
1590 else if (op1
== TOK_ULT
)
1592 else if (op1
== TOK_UGT
)
1597 if (op1
!= TOK_NE
) {
1601 /* generate non equal test */
1602 /* XXX: NOT PORTABLE yet */
1606 #if defined(TCC_TARGET_I386)
1607 b
= psym(0x850f, 0);
1608 #elif defined(TCC_TARGET_ARM)
1610 o(0x1A000000 | encbranch(ind
, 0, 1));
1611 #elif defined(TCC_TARGET_C67) || defined(TCC_TARGET_ARM64)
1612 tcc_error("not implemented");
1614 #error not supported
1618 /* compare low. Always unsigned */
1622 else if (op1
== TOK_LE
)
1624 else if (op1
== TOK_GT
)
1626 else if (op1
== TOK_GE
)
1637 static uint64_t gen_opic_sdiv(uint64_t a
, uint64_t b
)
1639 uint64_t x
= (a
>> 63 ? -a
: a
) / (b
>> 63 ? -b
: b
);
1640 return (a
^ b
) >> 63 ? -x
: x
;
1643 static int gen_opic_lt(uint64_t a
, uint64_t b
)
1645 return (a
^ (uint64_t)1 << 63) < (b
^ (uint64_t)1 << 63);
1648 /* handle integer constant optimizations and various machine
1650 static void gen_opic(int op
)
1652 SValue
*v1
= vtop
- 1;
1654 int t1
= v1
->type
.t
& VT_BTYPE
;
1655 int t2
= v2
->type
.t
& VT_BTYPE
;
1656 int c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1657 int c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1658 uint64_t l1
= c1
? v1
->c
.i
: 0;
1659 uint64_t l2
= c2
? v2
->c
.i
: 0;
1660 int shm
= (t1
== VT_LLONG
) ? 63 : 31;
1663 l1
= ((uint32_t)l1
|
1664 (v1
->type
.t
& VT_UNSIGNED
? 0 : -(l1
& 0x80000000)));
1666 l2
= ((uint32_t)l2
|
1667 (v2
->type
.t
& VT_UNSIGNED
? 0 : -(l2
& 0x80000000)));
1671 case '+': l1
+= l2
; break;
1672 case '-': l1
-= l2
; break;
1673 case '&': l1
&= l2
; break;
1674 case '^': l1
^= l2
; break;
1675 case '|': l1
|= l2
; break;
1676 case '*': l1
*= l2
; break;
1683 /* if division by zero, generate explicit division */
1686 tcc_error("division by zero in constant");
1690 default: l1
= gen_opic_sdiv(l1
, l2
); break;
1691 case '%': l1
= l1
- l2
* gen_opic_sdiv(l1
, l2
); break;
1692 case TOK_UDIV
: l1
= l1
/ l2
; break;
1693 case TOK_UMOD
: l1
= l1
% l2
; break;
1696 case TOK_SHL
: l1
<<= (l2
& shm
); break;
1697 case TOK_SHR
: l1
>>= (l2
& shm
); break;
1699 l1
= (l1
>> 63) ? ~(~l1
>> (l2
& shm
)) : l1
>> (l2
& shm
);
1702 case TOK_ULT
: l1
= l1
< l2
; break;
1703 case TOK_UGE
: l1
= l1
>= l2
; break;
1704 case TOK_EQ
: l1
= l1
== l2
; break;
1705 case TOK_NE
: l1
= l1
!= l2
; break;
1706 case TOK_ULE
: l1
= l1
<= l2
; break;
1707 case TOK_UGT
: l1
= l1
> l2
; break;
1708 case TOK_LT
: l1
= gen_opic_lt(l1
, l2
); break;
1709 case TOK_GE
: l1
= !gen_opic_lt(l1
, l2
); break;
1710 case TOK_LE
: l1
= !gen_opic_lt(l2
, l1
); break;
1711 case TOK_GT
: l1
= gen_opic_lt(l2
, l1
); break;
1713 case TOK_LAND
: l1
= l1
&& l2
; break;
1714 case TOK_LOR
: l1
= l1
|| l2
; break;
1721 /* if commutative ops, put c2 as constant */
1722 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1723 op
== '|' || op
== '*')) {
1725 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1726 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1728 if (!const_wanted
&&
1730 (op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
)) ||
1731 (l1
== -1 && op
== TOK_SAR
))) {
1732 /* treat (0 << x), (0 >> x) and (-1 >> x) as constant */
1734 } else if (!const_wanted
&&
1735 c2
&& ((l2
== 0 && (op
== '&' || op
== '*')) ||
1736 (l2
== -1 && op
== '|') ||
1737 (l2
== 0xffffffff && t2
!= VT_LLONG
&& op
== '|') ||
1738 (l2
== 1 && (op
== '%' || op
== TOK_UMOD
)))) {
1739 /* treat (x & 0), (x * 0), (x | -1) and (x % 1) as constant */
1744 } else if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1747 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1748 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1752 /* filter out NOP operations like x*1, x-0, x&-1... */
1754 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1755 /* try to use shifts instead of muls or divs */
1756 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1765 else if (op
== TOK_PDIV
)
1771 } else if (c2
&& (op
== '+' || op
== '-') &&
1772 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
1773 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1774 /* symbol + constant case */
1781 if (!nocode_wanted
) {
1782 /* call low level op generator */
1783 if (t1
== VT_LLONG
|| t2
== VT_LLONG
||
1784 (PTR_SIZE
== 8 && (t1
== VT_PTR
|| t2
== VT_PTR
)))
1790 /* Ensure vtop isn't marked VT_CONST in case something
1791 up our callchain is interested in const-ness of the
1792 expression. Also make it a non-LVAL if it was,
1793 so that further code can't accidentally generate
1794 a deref (happen only for buggy uses of e.g.
1795 gv() under nocode_wanted). */
1796 vtop
->r
&= ~(VT_VALMASK
| VT_LVAL
);
1802 /* generate a floating point operation with constant propagation */
1803 static void gen_opif(int op
)
1811 /* currently, we cannot do computations with forward symbols */
1812 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1813 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1815 if (v1
->type
.t
== VT_FLOAT
) {
1818 } else if (v1
->type
.t
== VT_DOUBLE
) {
1826 /* NOTE: we only do constant propagation if finite number (not
1827 NaN or infinity) (ANSI spec) */
1828 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1832 case '+': f1
+= f2
; break;
1833 case '-': f1
-= f2
; break;
1834 case '*': f1
*= f2
; break;
1838 tcc_error("division by zero in constant");
1843 /* XXX: also handles tests ? */
1847 /* XXX: overflow test ? */
1848 if (v1
->type
.t
== VT_FLOAT
) {
1850 } else if (v1
->type
.t
== VT_DOUBLE
) {
1858 if (!nocode_wanted
) {
1866 static int pointed_size(CType
*type
)
1869 return type_size(pointed_type(type
), &align
);
1872 static void vla_runtime_pointed_size(CType
*type
)
1875 vla_runtime_type_size(pointed_type(type
), &align
);
1878 static inline int is_null_pointer(SValue
*p
)
1880 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1882 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& (uint32_t)p
->c
.i
== 0) ||
1883 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.i
== 0) ||
1884 ((p
->type
.t
& VT_BTYPE
) == VT_PTR
&&
1885 (PTR_SIZE
== 4 ? (uint32_t)p
->c
.i
== 0 : p
->c
.i
== 0));
1888 static inline int is_integer_btype(int bt
)
1890 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
1891 bt
== VT_INT
|| bt
== VT_LLONG
);
1894 /* check types for comparison or subtraction of pointers */
1895 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
1897 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
1900 /* null pointers are accepted for all comparisons as gcc */
1901 if (is_null_pointer(p1
) || is_null_pointer(p2
))
1905 bt1
= type1
->t
& VT_BTYPE
;
1906 bt2
= type2
->t
& VT_BTYPE
;
1907 /* accept comparison between pointer and integer with a warning */
1908 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
1909 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
1910 tcc_warning("comparison between pointer and integer");
1914 /* both must be pointers or implicit function pointers */
1915 if (bt1
== VT_PTR
) {
1916 type1
= pointed_type(type1
);
1917 } else if (bt1
!= VT_FUNC
)
1918 goto invalid_operands
;
1920 if (bt2
== VT_PTR
) {
1921 type2
= pointed_type(type2
);
1922 } else if (bt2
!= VT_FUNC
) {
1924 tcc_error("invalid operands to binary %s", get_tok_str(op
, NULL
));
1926 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1927 (type2
->t
& VT_BTYPE
) == VT_VOID
)
1931 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1932 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1933 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1934 /* gcc-like error if '-' is used */
1936 goto invalid_operands
;
1938 tcc_warning("comparison of distinct pointer types lacks a cast");
1942 /* generic gen_op: handles types problems */
1943 ST_FUNC
void gen_op(int op
)
1945 int u
, t1
, t2
, bt1
, bt2
, t
;
1949 t1
= vtop
[-1].type
.t
;
1950 t2
= vtop
[0].type
.t
;
1951 bt1
= t1
& VT_BTYPE
;
1952 bt2
= t2
& VT_BTYPE
;
1954 if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
1955 tcc_error("operation on a struct");
1956 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
1957 if (bt2
== VT_FUNC
) {
1958 mk_pointer(&vtop
->type
);
1961 if (bt1
== VT_FUNC
) {
1963 mk_pointer(&vtop
->type
);
1968 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
1969 /* at least one operand is a pointer */
1970 /* relationnal op: must be both pointers */
1971 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
1972 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1973 /* pointers are handled are unsigned */
1974 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1975 t
= VT_LLONG
| VT_UNSIGNED
;
1977 t
= VT_INT
| VT_UNSIGNED
;
1981 /* if both pointers, then it must be the '-' op */
1982 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
1984 tcc_error("cannot use pointers here");
1985 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1986 /* XXX: check that types are compatible */
1987 if (vtop
[-1].type
.t
& VT_VLA
) {
1988 vla_runtime_pointed_size(&vtop
[-1].type
);
1990 vpushi(pointed_size(&vtop
[-1].type
));
1994 /* set to integer type */
1995 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1996 vtop
->type
.t
= VT_LLONG
;
1998 vtop
->type
.t
= VT_INT
;
2003 /* exactly one pointer : must be '+' or '-'. */
2004 if (op
!= '-' && op
!= '+')
2005 tcc_error("cannot use pointers here");
2006 /* Put pointer as first operand */
2007 if (bt2
== VT_PTR
) {
2012 if ((vtop
[0].type
.t
& VT_BTYPE
) == VT_LLONG
)
2013 /* XXX: truncate here because gen_opl can't handle ptr + long long */
2014 gen_cast(&int_type
);
2016 type1
= vtop
[-1].type
;
2017 type1
.t
&= ~VT_ARRAY
;
2018 if (vtop
[-1].type
.t
& VT_VLA
)
2019 vla_runtime_pointed_size(&vtop
[-1].type
);
2021 u
= pointed_size(&vtop
[-1].type
);
2023 tcc_error("unknown array element size");
2024 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2027 /* XXX: cast to int ? (long long case) */
2033 /* #ifdef CONFIG_TCC_BCHECK
2034 The main reason to removing this code:
2041 fprintf(stderr, "v+i-j = %p\n", v+i-j);
2042 fprintf(stderr, "v+(i-j) = %p\n", v+(i-j));
2044 When this code is on. then the output looks like
2046 v+(i-j) = 0xbff84000
2048 /* if evaluating constant expression, no code should be
2049 generated, so no bound check */
2050 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
2051 /* if bounded pointers, we generate a special code to
2058 gen_bounded_ptr_add();
2064 /* put again type if gen_opic() swaped operands */
2067 } else if (is_float(bt1
) || is_float(bt2
)) {
2068 /* compute bigger type and do implicit casts */
2069 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
2071 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
2076 /* floats can only be used for a few operations */
2077 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
2078 (op
< TOK_ULT
|| op
> TOK_GT
))
2079 tcc_error("invalid operands for binary operation");
2081 } else if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
) {
2082 t
= bt1
== VT_LLONG
? VT_LLONG
: VT_INT
;
2083 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (t
| VT_UNSIGNED
))
2086 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
2087 /* cast to biggest op */
2089 /* convert to unsigned if it does not fit in a long long */
2090 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
2091 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
2095 /* integer operations */
2097 /* convert to unsigned if it does not fit in an integer */
2098 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
2099 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
2102 /* XXX: currently, some unsigned operations are explicit, so
2103 we modify them here */
2104 if (t
& VT_UNSIGNED
) {
2111 else if (op
== TOK_LT
)
2113 else if (op
== TOK_GT
)
2115 else if (op
== TOK_LE
)
2117 else if (op
== TOK_GE
)
2124 /* special case for shifts and long long: we keep the shift as
2126 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
2133 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
2134 /* relationnal op: the result is an int */
2135 vtop
->type
.t
= VT_INT
;
2140 // Make sure that we have converted to an rvalue:
2141 if (vtop
->r
& VT_LVAL
&& !nocode_wanted
)
2142 gv(is_float(vtop
->type
.t
& VT_BTYPE
) ? RC_FLOAT
: RC_INT
);
2145 #ifndef TCC_TARGET_ARM
2146 /* generic itof for unsigned long long case */
2147 static void gen_cvt_itof1(int t
)
2149 #ifdef TCC_TARGET_ARM64
2152 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
2153 (VT_LLONG
| VT_UNSIGNED
)) {
2156 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
2157 #if LDOUBLE_SIZE != 8
2158 else if (t
== VT_LDOUBLE
)
2159 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
2162 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
2166 vtop
->r
= reg_fret(t
);
2174 /* generic ftoi for unsigned long long case */
2175 static void gen_cvt_ftoi1(int t
)
2177 #ifdef TCC_TARGET_ARM64
2182 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
2183 /* not handled natively */
2184 st
= vtop
->type
.t
& VT_BTYPE
;
2186 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
2187 #if LDOUBLE_SIZE != 8
2188 else if (st
== VT_LDOUBLE
)
2189 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
2192 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
2197 vtop
->r2
= REG_LRET
;
2204 /* force char or short cast */
2205 static void force_charshort_cast(int t
)
2209 /* XXX: add optimization if lvalue : just change type and offset */
2214 if (t
& VT_UNSIGNED
) {
2215 vpushi((1 << bits
) - 1);
2218 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
)
2224 /* result must be signed or the SAR is converted to an SHL
2225 This was not the case when "t" was a signed short
2226 and the last value on the stack was an unsigned int */
2227 vtop
->type
.t
&= ~VT_UNSIGNED
;
2233 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
2234 static void gen_cast(CType
*type
)
2236 int sbt
, dbt
, sf
, df
, c
, p
;
2238 /* special delayed cast for char/short */
2239 /* XXX: in some cases (multiple cascaded casts), it may still
2241 if (vtop
->r
& VT_MUSTCAST
) {
2242 vtop
->r
&= ~VT_MUSTCAST
;
2243 force_charshort_cast(vtop
->type
.t
);
2246 /* bitfields first get cast to ints */
2247 if (vtop
->type
.t
& VT_BITFIELD
&& !nocode_wanted
) {
2251 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
2252 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
2257 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
2258 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
2260 /* constant case: we can do it now */
2261 /* XXX: in ISOC, cannot do it if error in convert */
2262 if (sbt
== VT_FLOAT
)
2263 vtop
->c
.ld
= vtop
->c
.f
;
2264 else if (sbt
== VT_DOUBLE
)
2265 vtop
->c
.ld
= vtop
->c
.d
;
2268 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
2269 if ((sbt
& VT_UNSIGNED
) || !(vtop
->c
.i
>> 63))
2270 vtop
->c
.ld
= vtop
->c
.i
;
2272 vtop
->c
.ld
= -(long double)-vtop
->c
.i
;
2274 if ((sbt
& VT_UNSIGNED
) || !(vtop
->c
.i
>> 31))
2275 vtop
->c
.ld
= (uint32_t)vtop
->c
.i
;
2277 vtop
->c
.ld
= -(long double)-(uint32_t)vtop
->c
.i
;
2280 if (dbt
== VT_FLOAT
)
2281 vtop
->c
.f
= (float)vtop
->c
.ld
;
2282 else if (dbt
== VT_DOUBLE
)
2283 vtop
->c
.d
= (double)vtop
->c
.ld
;
2284 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
2285 vtop
->c
.i
= vtop
->c
.ld
;
2286 } else if (sf
&& dbt
== VT_BOOL
) {
2287 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
2290 vtop
->c
.i
= vtop
->c
.ld
;
2291 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
2293 else if (sbt
& VT_UNSIGNED
)
2294 vtop
->c
.i
= (uint32_t)vtop
->c
.i
;
2295 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2296 else if (sbt
== VT_PTR
)
2299 else if (sbt
!= VT_LLONG
)
2300 vtop
->c
.i
= ((uint32_t)vtop
->c
.i
|
2301 -(vtop
->c
.i
& 0x80000000));
2303 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
2305 else if (dbt
== VT_BOOL
)
2306 vtop
->c
.i
= (vtop
->c
.i
!= 0);
2307 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2308 else if (dbt
== VT_PTR
)
2311 else if (dbt
!= VT_LLONG
) {
2312 uint32_t m
= ((dbt
& VT_BTYPE
) == VT_BYTE
? 0xff :
2313 (dbt
& VT_BTYPE
) == VT_SHORT
? 0xffff :
2316 if (!(dbt
& VT_UNSIGNED
))
2317 vtop
->c
.i
|= -(vtop
->c
.i
& ((m
>> 1) + 1));
2320 } else if (p
&& dbt
== VT_BOOL
) {
2323 } else if (!nocode_wanted
) {
2324 /* non constant case: generate code */
2326 /* convert from fp to fp */
2329 /* convert int to fp */
2332 /* convert fp to int */
2333 if (dbt
== VT_BOOL
) {
2337 /* we handle char/short/etc... with generic code */
2338 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
2339 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
2343 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
2344 /* additional cast for char/short... */
2349 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
2350 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
2351 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
2352 /* scalar to long long */
2353 /* machine independent conversion */
2355 /* generate high word */
2356 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
2360 if (sbt
== VT_PTR
) {
2361 /* cast from pointer to int before we apply
2362 shift operation, which pointers don't support*/
2363 gen_cast(&int_type
);
2369 /* patch second register */
2370 vtop
[-1].r2
= vtop
->r
;
2374 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
2375 (dbt
& VT_BTYPE
) == VT_PTR
||
2376 (dbt
& VT_BTYPE
) == VT_FUNC
) {
2377 if ((sbt
& VT_BTYPE
) != VT_LLONG
&&
2378 (sbt
& VT_BTYPE
) != VT_PTR
&&
2379 (sbt
& VT_BTYPE
) != VT_FUNC
) {
2380 /* need to convert from 32bit to 64bit */
2382 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
2383 #if defined(TCC_TARGET_ARM64)
2385 #elif defined(TCC_TARGET_X86_64)
2387 /* x86_64 specific: movslq */
2389 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
2396 } else if (dbt
== VT_BOOL
) {
2397 /* scalar to bool */
2400 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
2401 (dbt
& VT_BTYPE
) == VT_SHORT
) {
2402 if (sbt
== VT_PTR
) {
2403 vtop
->type
.t
= VT_INT
;
2404 tcc_warning("nonportable conversion from pointer to char/short");
2406 force_charshort_cast(dbt
);
2407 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
2408 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
2410 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
2411 /* from long long: just take low order word */
2415 /* if lvalue and single word type, nothing to do because
2416 the lvalue already contains the real type size (see
2417 VT_LVAL_xxx constants) */
2421 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
2422 /* if we are casting between pointer types,
2423 we must update the VT_LVAL_xxx size */
2424 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
2425 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
2430 /* return type size as known at compile time. Put alignment at 'a' */
2431 ST_FUNC
int type_size(CType
*type
, int *a
)
2436 bt
= type
->t
& VT_BTYPE
;
2437 if (bt
== VT_STRUCT
) {
2442 } else if (bt
== VT_PTR
) {
2443 if (type
->t
& VT_ARRAY
) {
2447 ts
= type_size(&s
->type
, a
);
2449 if (ts
< 0 && s
->c
< 0)
2457 } else if (bt
== VT_LDOUBLE
) {
2459 return LDOUBLE_SIZE
;
2460 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
2461 #ifdef TCC_TARGET_I386
2462 #ifdef TCC_TARGET_PE
2467 #elif defined(TCC_TARGET_ARM)
2477 } else if (bt
== VT_INT
|| bt
== VT_FLOAT
) {
2480 } else if (bt
== VT_SHORT
) {
2483 } else if (bt
== VT_QLONG
|| bt
== VT_QFLOAT
) {
2486 } else if (bt
== VT_ENUM
) {
2488 /* Enums might be incomplete, so don't just return '4' here. */
2489 return type
->ref
->c
;
2491 /* char, void, function, _Bool */
2497 /* push type size as known at runtime time on top of value stack. Put
2499 ST_FUNC
void vla_runtime_type_size(CType
*type
, int *a
)
2501 if (type
->t
& VT_VLA
) {
2502 type_size(&type
->ref
->type
, a
);
2503 vset(&int_type
, VT_LOCAL
|VT_LVAL
, type
->ref
->c
);
2505 vpushi(type_size(type
, a
));
2509 static void vla_sp_restore(void) {
2510 if (vlas_in_scope
) {
2511 gen_vla_sp_restore(vla_sp_loc
);
2515 static void vla_sp_restore_root(void) {
2516 if (vlas_in_scope
) {
2517 gen_vla_sp_restore(vla_sp_root_loc
);
2521 /* return the pointed type of t */
2522 static inline CType
*pointed_type(CType
*type
)
2524 return &type
->ref
->type
;
2527 /* modify type so that its it is a pointer to type. */
2528 ST_FUNC
void mk_pointer(CType
*type
)
2531 s
= sym_push(SYM_FIELD
, type
, 0, -1);
2532 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
2536 /* compare function types. OLD functions match any new functions */
2537 static int is_compatible_func(CType
*type1
, CType
*type2
)
2543 if (!is_compatible_types(&s1
->type
, &s2
->type
))
2545 /* check func_call */
2546 if (s1
->a
.func_call
!= s2
->a
.func_call
)
2548 /* XXX: not complete */
2549 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
2553 while (s1
!= NULL
) {
2556 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
2566 /* return true if type1 and type2 are the same. If unqualified is
2567 true, qualifiers on the types are ignored.
2569 - enums are not checked as gcc __builtin_types_compatible_p ()
2571 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2575 t1
= type1
->t
& VT_TYPE
;
2576 t2
= type2
->t
& VT_TYPE
;
2578 /* strip qualifiers before comparing */
2579 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2580 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2582 /* Default Vs explicit signedness only matters for char */
2583 if ((t1
& VT_BTYPE
) != VT_BYTE
) {
2587 /* An enum is compatible with (unsigned) int. Ideally we would
2588 store the enums signedness in type->ref.a.<some_bit> and
2589 only accept unsigned enums with unsigned int and vice versa.
2590 But one of our callers (gen_assign_cast) always strips VT_UNSIGNED
2591 from pointer target types, so we can't add it here either. */
2592 if ((t1
& VT_BTYPE
) == VT_ENUM
) {
2594 if (type1
->ref
->a
.unsigned_enum
)
2597 if ((t2
& VT_BTYPE
) == VT_ENUM
) {
2599 if (type2
->ref
->a
.unsigned_enum
)
2602 /* XXX: bitfields ? */
2605 /* test more complicated cases */
2606 bt1
= t1
& VT_BTYPE
;
2607 if (bt1
== VT_PTR
) {
2608 type1
= pointed_type(type1
);
2609 type2
= pointed_type(type2
);
2610 return is_compatible_types(type1
, type2
);
2611 } else if (bt1
== VT_STRUCT
) {
2612 return (type1
->ref
== type2
->ref
);
2613 } else if (bt1
== VT_FUNC
) {
2614 return is_compatible_func(type1
, type2
);
2620 /* return true if type1 and type2 are exactly the same (including
2623 static int is_compatible_types(CType
*type1
, CType
*type2
)
2625 return compare_types(type1
,type2
,0);
2628 /* return true if type1 and type2 are the same (ignoring qualifiers).
2630 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
2632 return compare_types(type1
,type2
,1);
2635 /* print a type. If 'varstr' is not NULL, then the variable is also
2636 printed in the type */
2638 /* XXX: add array and function pointers */
2639 static void type_to_str(char *buf
, int buf_size
,
2640 CType
*type
, const char *varstr
)
2647 t
= type
->t
& VT_TYPE
;
2650 if (t
& VT_CONSTANT
)
2651 pstrcat(buf
, buf_size
, "const ");
2652 if (t
& VT_VOLATILE
)
2653 pstrcat(buf
, buf_size
, "volatile ");
2654 if ((t
& (VT_DEFSIGN
| VT_UNSIGNED
)) == (VT_DEFSIGN
| VT_UNSIGNED
))
2655 pstrcat(buf
, buf_size
, "unsigned ");
2656 else if (t
& VT_DEFSIGN
)
2657 pstrcat(buf
, buf_size
, "signed ");
2687 tstr
= "long double";
2689 pstrcat(buf
, buf_size
, tstr
);
2693 if (bt
== VT_STRUCT
)
2697 pstrcat(buf
, buf_size
, tstr
);
2698 v
= type
->ref
->v
& ~SYM_STRUCT
;
2699 if (v
>= SYM_FIRST_ANOM
)
2700 pstrcat(buf
, buf_size
, "<anonymous>");
2702 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
2706 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
2707 pstrcat(buf
, buf_size
, "(");
2709 while (sa
!= NULL
) {
2710 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
2711 pstrcat(buf
, buf_size
, buf1
);
2714 pstrcat(buf
, buf_size
, ", ");
2716 pstrcat(buf
, buf_size
, ")");
2721 snprintf(buf1
, sizeof(buf1
), "%s[%ld]", varstr
? varstr
: "", s
->c
);
2722 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2725 pstrcpy(buf1
, sizeof(buf1
), "*");
2726 if (t
& VT_CONSTANT
)
2727 pstrcat(buf1
, buf_size
, "const ");
2728 if (t
& VT_VOLATILE
)
2729 pstrcat(buf1
, buf_size
, "volatile ");
2731 pstrcat(buf1
, sizeof(buf1
), varstr
);
2732 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2736 pstrcat(buf
, buf_size
, " ");
2737 pstrcat(buf
, buf_size
, varstr
);
2742 /* verify type compatibility to store vtop in 'dt' type, and generate
2744 static void gen_assign_cast(CType
*dt
)
2746 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
2747 char buf1
[256], buf2
[256];
2750 st
= &vtop
->type
; /* source type */
2751 dbt
= dt
->t
& VT_BTYPE
;
2752 sbt
= st
->t
& VT_BTYPE
;
2753 if (sbt
== VT_VOID
|| dbt
== VT_VOID
) {
2754 if (sbt
== VT_VOID
&& dbt
== VT_VOID
)
2756 It is Ok if both are void
2762 gcc accepts this program
2765 tcc_error("cannot cast from/to void");
2767 if (dt
->t
& VT_CONSTANT
)
2768 tcc_warning("assignment of read-only location");
2771 /* special cases for pointers */
2772 /* '0' can also be a pointer */
2773 if (is_null_pointer(vtop
))
2775 /* accept implicit pointer to integer cast with warning */
2776 if (is_integer_btype(sbt
)) {
2777 tcc_warning("assignment makes pointer from integer without a cast");
2780 type1
= pointed_type(dt
);
2781 /* a function is implicitely a function pointer */
2782 if (sbt
== VT_FUNC
) {
2783 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
2784 !is_compatible_types(pointed_type(dt
), st
))
2785 tcc_warning("assignment from incompatible pointer type");
2790 type2
= pointed_type(st
);
2791 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2792 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
2793 /* void * can match anything */
2795 /* exact type match, except for qualifiers */
2798 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2799 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2800 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2801 /* Like GCC don't warn by default for merely changes
2802 in pointer target signedness. Do warn for different
2803 base types, though, in particular for unsigned enums
2804 and signed int targets. */
2805 if ((tmp_type1
.t
& (VT_DEFSIGN
| VT_UNSIGNED
)) !=
2806 (tmp_type2
.t
& (VT_DEFSIGN
| VT_UNSIGNED
)) &&
2807 (tmp_type1
.t
& VT_BTYPE
) == (tmp_type2
.t
& VT_BTYPE
))
2810 tcc_warning("assignment from incompatible pointer type");
2813 /* check const and volatile */
2814 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
2815 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
2816 tcc_warning("assignment discards qualifiers from pointer target type");
2822 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
2823 tcc_warning("assignment makes integer from pointer without a cast");
2824 } else if (sbt
== VT_STRUCT
) {
2825 goto case_VT_STRUCT
;
2827 /* XXX: more tests */
2833 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2834 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2835 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2837 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
2838 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
2839 tcc_error("cannot cast '%s' to '%s'", buf1
, buf2
);
2847 /* store vtop in lvalue pushed on stack */
2848 ST_FUNC
void vstore(void)
2850 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
2852 ft
= vtop
[-1].type
.t
;
2853 sbt
= vtop
->type
.t
& VT_BTYPE
;
2854 dbt
= ft
& VT_BTYPE
;
2855 if ((((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2856 (sbt
== VT_INT
&& dbt
== VT_SHORT
))
2857 && !(vtop
->type
.t
& VT_BITFIELD
)) {
2858 /* optimize char/short casts */
2859 delayed_cast
= VT_MUSTCAST
;
2860 vtop
->type
.t
= (ft
& VT_TYPE
& ~VT_BITFIELD
&
2861 ((1 << VT_STRUCT_SHIFT
) - 1));
2862 /* XXX: factorize */
2863 if (ft
& VT_CONSTANT
)
2864 tcc_warning("assignment of read-only location");
2867 if (!(ft
& VT_BITFIELD
))
2868 gen_assign_cast(&vtop
[-1].type
);
2871 if (sbt
== VT_STRUCT
) {
2872 /* if structure, only generate pointer */
2873 /* structure assignment : generate memcpy */
2874 /* XXX: optimize if small size */
2875 if (!nocode_wanted
) {
2876 size
= type_size(&vtop
->type
, &align
);
2880 vtop
->type
.t
= VT_PTR
;
2883 /* address of memcpy() */
2886 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2887 else if(!(align
& 3))
2888 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2891 /* Use memmove, rather than memcpy, as dest and src may be same: */
2892 vpush_global_sym(&func_old_type
, TOK_memmove
);
2897 vtop
->type
.t
= VT_PTR
;
2906 /* leave source on stack */
2907 } else if (ft
& VT_BITFIELD
) {
2908 /* bitfield store handling */
2910 /* save lvalue as expression result (example: s.b = s.a = n;) */
2911 vdup(), vtop
[-1] = vtop
[-2];
2913 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2914 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2915 /* remove bit field info to avoid loops */
2916 vtop
[-1].type
.t
= ft
& ~VT_BITFIELD
& ((1 << VT_STRUCT_SHIFT
) - 1);
2918 if((ft
& VT_BTYPE
) == VT_BOOL
) {
2919 gen_cast(&vtop
[-1].type
);
2920 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2923 /* duplicate destination */
2925 vtop
[-1] = vtop
[-2];
2927 /* mask and shift source */
2928 if((ft
& VT_BTYPE
) != VT_BOOL
) {
2929 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2930 vpushll((1ULL << bit_size
) - 1ULL);
2932 vpushi((1 << bit_size
) - 1);
2938 /* load destination, mask and or with source */
2940 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2941 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2943 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2949 /* ... and discard */
2953 if (!nocode_wanted
) {
2954 #ifdef CONFIG_TCC_BCHECK
2955 /* bound check case */
2956 if (vtop
[-1].r
& VT_MUSTBOUND
) {
2965 #ifdef TCC_TARGET_X86_64
2966 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
2968 } else if ((ft
& VT_BTYPE
) == VT_QFLOAT
) {
2973 r
= gv(rc
); /* generate value */
2974 /* if lvalue was saved on stack, must read it */
2975 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
2977 t
= get_reg(RC_INT
);
2978 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2983 sv
.r
= VT_LOCAL
| VT_LVAL
;
2984 sv
.c
.i
= vtop
[-1].c
.i
;
2986 vtop
[-1].r
= t
| VT_LVAL
;
2988 /* two word case handling : store second register at word + 4 (or +8 for x86-64) */
2989 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2990 if (((ft
& VT_BTYPE
) == VT_QLONG
) || ((ft
& VT_BTYPE
) == VT_QFLOAT
)) {
2991 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
2993 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
2994 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
2996 vtop
[-1].type
.t
= load_type
;
2999 /* convert to int to increment easily */
3000 vtop
->type
.t
= addr_type
;
3006 vtop
[-1].type
.t
= load_type
;
3007 /* XXX: it works because r2 is spilled last ! */
3008 store(vtop
->r2
, vtop
- 1);
3014 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
3015 vtop
->r
|= delayed_cast
;
3019 /* post defines POST/PRE add. c is the token ++ or -- */
3020 ST_FUNC
void inc(int post
, int c
)
3023 vdup(); /* save lvalue */
3026 gv_dup(); /* duplicate value */
3028 vdup(); /* duplicate value */
3033 vpushi(c
- TOK_MID
);
3035 vstore(); /* store value */
3037 vpop(); /* if post op, return saved value */
3040 ST_FUNC
void parse_mult_str (CString
*astr
, const char *msg
)
3042 /* read the string */
3046 while (tok
== TOK_STR
) {
3047 /* XXX: add \0 handling too ? */
3048 cstr_cat(astr
, tokc
.str
.data
, -1);
3051 cstr_ccat(astr
, '\0');
3054 /* Parse GNUC __attribute__ extension. Currently, the following
3055 extensions are recognized:
3056 - aligned(n) : set data/function alignment.
3057 - packed : force data alignment to 1
3058 - section(x) : generate data/code in this section.
3059 - unused : currently ignored, but may be used someday.
3060 - regparm(n) : pass function parameters in registers (i386 only)
3062 static void parse_attribute(AttributeDef
*ad
)
3067 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
3071 while (tok
!= ')') {
3072 if (tok
< TOK_IDENT
)
3073 expect("attribute name");
3080 parse_mult_str(&astr
, "section name");
3081 ad
->section
= find_section(tcc_state
, (char *)astr
.data
);
3088 parse_mult_str(&astr
, "alias(\"target\")");
3089 ad
->alias_target
= /* save string as token, for later */
3090 tok_alloc((char*)astr
.data
, astr
.size
-1)->tok
;
3094 case TOK_VISIBILITY1
:
3095 case TOK_VISIBILITY2
:
3097 parse_mult_str(&astr
,
3098 "visibility(\"default|hidden|internal|protected\")");
3099 if (!strcmp (astr
.data
, "default"))
3100 ad
->a
.visibility
= STV_DEFAULT
;
3101 else if (!strcmp (astr
.data
, "hidden"))
3102 ad
->a
.visibility
= STV_HIDDEN
;
3103 else if (!strcmp (astr
.data
, "internal"))
3104 ad
->a
.visibility
= STV_INTERNAL
;
3105 else if (!strcmp (astr
.data
, "protected"))
3106 ad
->a
.visibility
= STV_PROTECTED
;
3108 expect("visibility(\"default|hidden|internal|protected\")");
3117 if (n
<= 0 || (n
& (n
- 1)) != 0)
3118 tcc_error("alignment must be a positive power of two");
3135 /* currently, no need to handle it because tcc does not
3136 track unused objects */
3140 /* currently, no need to handle it because tcc does not
3141 track unused objects */
3146 ad
->a
.func_call
= FUNC_CDECL
;
3151 ad
->a
.func_call
= FUNC_STDCALL
;
3153 #ifdef TCC_TARGET_I386
3163 ad
->a
.func_call
= FUNC_FASTCALL1
+ n
- 1;
3169 ad
->a
.func_call
= FUNC_FASTCALLW
;
3176 ad
->a
.mode
= VT_LLONG
+ 1;
3179 ad
->a
.mode
= VT_BYTE
+ 1;
3182 ad
->a
.mode
= VT_SHORT
+ 1;
3186 ad
->a
.mode
= VT_INT
+ 1;
3189 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
3196 ad
->a
.func_export
= 1;
3199 ad
->a
.func_import
= 1;
3202 if (tcc_state
->warn_unsupported
)
3203 tcc_warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
3204 /* skip parameters */
3206 int parenthesis
= 0;
3210 else if (tok
== ')')
3213 } while (parenthesis
&& tok
!= -1);
3226 static Sym
* find_field (CType
*type
, int v
)
3230 while ((s
= s
->next
) != NULL
) {
3231 if ((s
->v
& SYM_FIELD
) && (s
->v
& ~SYM_FIELD
) >= SYM_FIRST_ANOM
) {
3232 Sym
*ret
= find_field (&s
->type
, v
);
3242 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
3243 static void struct_decl(CType
*type
, AttributeDef
*ad
, int u
)
3245 int a
, v
, size
, align
, maxalign
, c
, offset
, flexible
, extra_bytes
;
3246 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
, prevbt
;
3247 Sym
*s
, *ss
, *ass
, **ps
;
3251 a
= tok
; /* save decl type */
3253 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3254 parse_attribute(ad
);
3258 /* struct already defined ? return it */
3260 expect("struct/union/enum name");
3262 if (s
&& (s
->scope
== local_scope
|| (tok
!= '{' && tok
!= ';'))) {
3264 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
3270 /* Record the original enum/struct/union token. */
3273 /* we put an undefined size for struct/union */
3274 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
3275 s
->r
= 0; /* default alignment is zero as gcc */
3276 /* put struct/union/enum name in type */
3284 tcc_error("struct/union/enum already defined");
3285 /* cannot be empty */
3287 /* non empty enums are not allowed */
3288 if (a
== TOK_ENUM
) {
3293 expect("identifier");
3295 if (ss
&& !local_stack
)
3296 tcc_error("redefinition of enumerator '%s'",
3297 get_tok_str(v
, NULL
));
3305 /* enum symbols have static storage */
3306 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
3307 ss
->type
.t
|= VT_STATIC
;
3312 /* NOTE: we accept a trailing comma */
3317 s
->a
.unsigned_enum
= 1;
3318 s
->c
= type_size(&int_type
, &align
);
3327 while (tok
!= '}') {
3328 if (!parse_btype(&btype
, &ad1
)) {
3335 tcc_error("flexible array member '%s' not at the end of struct",
3336 get_tok_str(v
, NULL
));
3341 type_decl(&type1
, &ad1
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3343 if ((type1
.t
& VT_BTYPE
) != VT_STRUCT
)
3344 expect("identifier");
3346 int v
= btype
.ref
->v
;
3347 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
3348 if (tcc_state
->ms_extensions
== 0)
3349 expect("identifier");
3353 if (type_size(&type1
, &align
) < 0) {
3354 if ((a
== TOK_STRUCT
) && (type1
.t
& VT_ARRAY
) && c
)
3357 tcc_error("field '%s' has incomplete type",
3358 get_tok_str(v
, NULL
));
3360 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
3361 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
3362 tcc_error("invalid type for '%s'",
3363 get_tok_str(v
, NULL
));
3367 bit_size
= expr_const();
3368 /* XXX: handle v = 0 case for messages */
3370 tcc_error("negative width in bit-field '%s'",
3371 get_tok_str(v
, NULL
));
3372 if (v
&& bit_size
== 0)
3373 tcc_error("zero width for bit-field '%s'",
3374 get_tok_str(v
, NULL
));
3376 size
= type_size(&type1
, &align
);
3377 if (ad1
.a
.aligned
) {
3378 if (align
< ad1
.a
.aligned
)
3379 align
= ad1
.a
.aligned
;
3380 } else if (ad1
.a
.packed
|| ad
->a
.packed
) {
3382 } else if (*tcc_state
->pack_stack_ptr
) {
3383 if (align
> *tcc_state
->pack_stack_ptr
)
3384 align
= *tcc_state
->pack_stack_ptr
;
3387 if (bit_size
>= 0) {
3388 bt
= type1
.t
& VT_BTYPE
;
3395 tcc_error("bitfields must have scalar type");
3397 if (bit_size
> bsize
) {
3398 tcc_error("width of '%s' exceeds its type",
3399 get_tok_str(v
, NULL
));
3400 } else if (bit_size
== bsize
) {
3401 /* no need for bit fields */
3403 } else if (bit_size
== 0) {
3404 /* XXX: what to do if only padding in a
3406 /* zero size: means to pad */
3409 /* if type change, union, or will overrun
3410 * allignment slot, start at a newly
3412 if ((bit_pos
+ bit_size
) > bsize
||
3413 bt
!= prevbt
|| a
== TOK_UNION
)
3416 /* XXX: handle LSB first */
3417 type1
.t
|= VT_BITFIELD
|
3418 (bit_pos
<< VT_STRUCT_SHIFT
) |
3419 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
3420 bit_pos
+= bit_size
;
3421 /* without ms-bitfields, allocate the
3422 * minimum number of bytes necessary,
3423 * adding single bytes as needed */
3424 if (!tcc_state
->ms_bitfields
) {
3426 /* minimum bytes for new bitfield */
3427 size
= (bit_size
+ 7) / 8;
3429 /* enough spare bits already allocated? */
3430 bit_size
= (lbit_pos
- 1) % 8 + 1 + bit_size
;
3431 if (bit_size
> 8) /* doesn't fit */
3432 extra_bytes
= (bit_size
- 1) / 8;
3440 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
3441 /* add new memory data only if starting bit
3442 field or adding bytes to existing bit field */
3443 if (extra_bytes
) c
+= extra_bytes
;
3444 else if (lbit_pos
== 0) {
3445 if (a
== TOK_STRUCT
) {
3446 c
= (c
+ align
- 1) & -align
;
3455 if (align
> maxalign
)
3459 printf("add field %s offset=%d",
3460 get_tok_str(v
, NULL
), offset
);
3461 if (type1
.t
& VT_BITFIELD
) {
3462 printf(" pos=%d size=%d",
3463 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
3464 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
3469 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
3470 /* An anonymous struct/union. Adjust member offsets
3471 to reflect the real offset of our containing struct.
3472 Also set the offset of this anon member inside
3473 the outer struct to be zero. Via this it
3474 works when accessing the field offset directly
3475 (from base object), as well as when recursing
3476 members in initializer handling. */
3477 int v2
= btype
.ref
->v
;
3478 if (!(v2
& SYM_FIELD
) &&
3479 (v2
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
3481 /* This happens only with MS extensions. The
3482 anon member has a named struct type, so it
3483 potentially is shared with other references.
3484 We need to unshare members so we can modify
3487 type1
.ref
= sym_push(anon_sym
++ | SYM_FIELD
,
3488 &type1
.ref
->type
, 0,
3490 pps
= &type1
.ref
->next
;
3491 while ((ass
= ass
->next
) != NULL
) {
3492 *pps
= sym_push(ass
->v
, &ass
->type
, 0, ass
->c
);
3493 pps
= &((*pps
)->next
);
3498 while ((ass
= ass
->next
) != NULL
)
3504 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
3508 if (tok
== ';' || tok
== TOK_EOF
)
3515 /* store size and alignment */
3516 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
3522 /* return 1 if basic type is a type size (short, long, long long) */
3523 ST_FUNC
int is_btype_size(int bt
)
3525 return bt
== VT_SHORT
|| bt
== VT_LONG
|| bt
== VT_LLONG
;
3528 /* Add type qualifiers to a type. If the type is an array then the qualifiers
3529 are added to the element type, copied because it could be a typedef. */
3530 static void parse_btype_qualify(CType
*type
, int qualifiers
)
3532 while (type
->t
& VT_ARRAY
) {
3533 type
->ref
= sym_push(SYM_FIELD
, &type
->ref
->type
, 0, type
->ref
->c
);
3534 type
= &type
->ref
->type
;
3536 type
->t
|= qualifiers
;
3539 /* return 0 if no type declaration. otherwise, return the basic type
3542 static int parse_btype(CType
*type
, AttributeDef
*ad
)
3544 int t
, u
, bt_size
, complete
, type_found
, typespec_found
;
3548 memset(ad
, 0, sizeof(AttributeDef
));
3556 /* currently, we really ignore extension */
3567 tcc_error("too many basic types");
3569 bt_size
= is_btype_size (u
& VT_BTYPE
);
3570 if (u
== VT_INT
|| (!bt_size
&& !(t
& VT_TYPEDEF
)))
3585 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
3586 #ifndef TCC_TARGET_PE
3587 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3589 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
3590 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3596 #ifdef TCC_TARGET_ARM64
3598 /* GCC's __uint128_t appears in some Linux header files. Make it a
3599 synonym for long double to get the size and alignment right. */
3611 if ((t
& VT_BTYPE
) == VT_LONG
) {
3612 #ifdef TCC_TARGET_PE
3613 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
3615 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3623 struct_decl(&type1
, ad
, VT_ENUM
);
3626 type
->ref
= type1
.ref
;
3630 struct_decl(&type1
, ad
, VT_STRUCT
);
3633 /* type modifiers */
3638 parse_btype_qualify(type
, VT_CONSTANT
);
3646 parse_btype_qualify(type
, VT_VOLATILE
);
3653 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == (VT_DEFSIGN
|VT_UNSIGNED
))
3654 tcc_error("signed and unsigned modifier");
3667 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == VT_DEFSIGN
)
3668 tcc_error("signed and unsigned modifier");
3669 t
|= VT_DEFSIGN
| VT_UNSIGNED
;
3694 /* GNUC attribute */
3695 case TOK_ATTRIBUTE1
:
3696 case TOK_ATTRIBUTE2
:
3697 parse_attribute(ad
);
3700 t
= (t
& ~VT_BTYPE
) | u
;
3708 parse_expr_type(&type1
);
3709 /* remove all storage modifiers except typedef */
3710 type1
.t
&= ~(VT_STORAGE
&~VT_TYPEDEF
);
3716 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
3719 type
->t
= ((s
->type
.t
& ~VT_TYPEDEF
) |
3720 (t
& ~(VT_CONSTANT
| VT_VOLATILE
)));
3721 type
->ref
= s
->type
.ref
;
3722 if (t
& (VT_CONSTANT
| VT_VOLATILE
))
3723 parse_btype_qualify(type
, t
& (VT_CONSTANT
| VT_VOLATILE
));
3727 /* get attributes from typedef */
3728 if (0 == ad
->a
.aligned
)
3729 ad
->a
.aligned
= s
->a
.aligned
;
3730 if (0 == ad
->a
.func_call
)
3731 ad
->a
.func_call
= s
->a
.func_call
;
3732 ad
->a
.packed
|= s
->a
.packed
;
3741 if (tcc_state
->char_is_unsigned
) {
3742 if ((t
& (VT_DEFSIGN
|VT_BTYPE
)) == VT_BYTE
)
3746 /* long is never used as type */
3747 if ((t
& VT_BTYPE
) == VT_LONG
)
3748 #if (!defined TCC_TARGET_X86_64 && !defined TCC_TARGET_ARM64) || \
3749 defined TCC_TARGET_PE
3750 t
= (t
& ~VT_BTYPE
) | VT_INT
;
3752 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3758 /* convert a function parameter type (array to pointer and function to
3759 function pointer) */
3760 static inline void convert_parameter_type(CType
*pt
)
3762 /* remove const and volatile qualifiers (XXX: const could be used
3763 to indicate a const function parameter */
3764 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3765 /* array must be transformed to pointer according to ANSI C */
3767 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
3772 ST_FUNC
void parse_asm_str(CString
*astr
)
3775 parse_mult_str(astr
, "string constant");
3778 /* Parse an asm label and return the token */
3779 static int asm_label_instr(void)
3785 parse_asm_str(&astr
);
3788 printf("asm_alias: \"%s\"\n", (char *)astr
.data
);
3790 v
= tok_alloc(astr
.data
, astr
.size
- 1)->tok
;
3795 static void post_type(CType
*type
, AttributeDef
*ad
, int storage
)
3797 int n
, l
, t1
, arg_size
, align
;
3798 Sym
**plast
, *s
, *first
;
3803 /* function declaration */
3811 /* read param name and compute offset */
3812 if (l
!= FUNC_OLD
) {
3813 if (!parse_btype(&pt
, &ad1
)) {
3815 tcc_error("invalid type");
3822 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
3824 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3825 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
3826 tcc_error("parameter declared as void");
3827 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
3832 expect("identifier");
3836 convert_parameter_type(&pt
);
3837 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
3843 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
3850 /* if no parameters, then old type prototype */
3854 /* NOTE: const is ignored in returned type as it has a special
3855 meaning in gcc / C++ */
3856 type
->t
&= ~VT_CONSTANT
;
3857 /* some ancient pre-K&R C allows a function to return an array
3858 and the array brackets to be put after the arguments, such
3859 that "int c()[]" means something like "int[] c()" */
3862 skip(']'); /* only handle simple "[]" */
3865 /* we push a anonymous symbol which will contain the function prototype */
3866 ad
->a
.func_args
= arg_size
;
3867 s
= sym_push(SYM_FIELD
, type
, 0, l
);
3872 } else if (tok
== '[') {
3873 int saved_nocode_wanted
= nocode_wanted
;
3874 /* array definition */
3876 if (tok
== TOK_RESTRICT1
)
3881 if (!local_stack
|| (storage
& VT_STATIC
))
3882 vpushi(expr_const());
3884 /* VLAs (which can only happen with local_stack && !VT_STATIC)
3885 length must always be evaluated, even under nocode_wanted,
3886 so that its size slot is initialized (e.g. under sizeof
3891 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3894 tcc_error("invalid array size");
3896 if (!is_integer_btype(vtop
->type
.t
& VT_BTYPE
))
3897 tcc_error("size of variable length array should be an integer");
3902 /* parse next post type */
3903 post_type(type
, ad
, storage
);
3904 if (type
->t
== VT_FUNC
)
3905 tcc_error("declaration of an array of functions");
3906 t1
|= type
->t
& VT_VLA
;
3909 loc
-= type_size(&int_type
, &align
);
3913 vla_runtime_type_size(type
, &align
);
3915 vset(&int_type
, VT_LOCAL
|VT_LVAL
, n
);
3921 nocode_wanted
= saved_nocode_wanted
;
3923 /* we push an anonymous symbol which will contain the array
3925 s
= sym_push(SYM_FIELD
, type
, 0, n
);
3926 type
->t
= (t1
? VT_VLA
: VT_ARRAY
) | VT_PTR
;
3931 /* Parse a type declaration (except basic type), and return the type
3932 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3933 expected. 'type' should contain the basic type. 'ad' is the
3934 attribute definition of the basic type. It can be modified by
3937 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
3940 CType type1
, *type2
;
3941 int qualifiers
, storage
;
3943 while (tok
== '*') {
3951 qualifiers
|= VT_CONSTANT
;
3956 qualifiers
|= VT_VOLATILE
;
3962 /* XXX: clarify attribute handling */
3963 case TOK_ATTRIBUTE1
:
3964 case TOK_ATTRIBUTE2
:
3965 parse_attribute(ad
);
3969 type
->t
|= qualifiers
;
3972 /* recursive type */
3973 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3974 type1
.t
= 0; /* XXX: same as int */
3977 /* XXX: this is not correct to modify 'ad' at this point, but
3978 the syntax is not clear */
3979 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3980 parse_attribute(ad
);
3981 type_decl(&type1
, ad
, v
, td
);
3984 /* type identifier */
3985 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
3989 if (!(td
& TYPE_ABSTRACT
))
3990 expect("identifier");
3994 storage
= type
->t
& VT_STORAGE
;
3995 type
->t
&= ~VT_STORAGE
;
3996 post_type(type
, ad
, storage
);
3998 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3999 parse_attribute(ad
);
4003 /* append type at the end of type1 */
4016 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
4017 ST_FUNC
int lvalue_type(int t
)
4022 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
4024 else if (bt
== VT_SHORT
)
4028 if (t
& VT_UNSIGNED
)
4029 r
|= VT_LVAL_UNSIGNED
;
4033 /* indirection with full error checking and bound check */
4034 ST_FUNC
void indir(void)
4036 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
4037 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
4041 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
4043 vtop
->type
= *pointed_type(&vtop
->type
);
4044 /* Arrays and functions are never lvalues */
4045 if (!(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_VLA
)
4046 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
4047 vtop
->r
|= lvalue_type(vtop
->type
.t
);
4048 /* if bound checking, the referenced pointer must be checked */
4049 #ifdef CONFIG_TCC_BCHECK
4050 if (tcc_state
->do_bounds_check
)
4051 vtop
->r
|= VT_MUSTBOUND
;
4056 /* pass a parameter to a function and do type checking and casting */
4057 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
4062 func_type
= func
->c
;
4063 if (func_type
== FUNC_OLD
||
4064 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
4065 /* default casting : only need to convert float to double */
4066 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
4069 } else if (vtop
->type
.t
& VT_BITFIELD
) {
4070 type
.t
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
4071 type
.ref
= vtop
->type
.ref
;
4074 } else if (arg
== NULL
) {
4075 tcc_error("too many arguments to function");
4078 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
4079 gen_assign_cast(&type
);
4083 /* parse an expression of the form '(type)' or '(expr)' and return its
4085 static void parse_expr_type(CType
*type
)
4091 if (parse_btype(type
, &ad
)) {
4092 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
4099 static void parse_type(CType
*type
)
4104 if (!parse_btype(type
, &ad
)) {
4107 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
4110 static void vpush_tokc(int t
)
4115 vsetc(&type
, VT_CONST
, &tokc
);
4118 ST_FUNC
void unary(void)
4120 int n
, t
, align
, size
, r
, sizeof_caller
;
4125 sizeof_caller
= in_sizeof
;
4127 /* XXX: GCC 2.95.3 does not generate a table although it should be
4141 vpush_tokc(VT_INT
| VT_UNSIGNED
);
4145 vpush_tokc(VT_LLONG
);
4149 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
4153 vpush_tokc(VT_FLOAT
);
4157 vpush_tokc(VT_DOUBLE
);
4161 vpush_tokc(VT_LDOUBLE
);
4164 case TOK___FUNCTION__
:
4166 goto tok_identifier
;
4172 /* special function name identifier */
4173 len
= strlen(funcname
) + 1;
4174 /* generate char[len] type */
4179 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
4180 ptr
= section_ptr_add(data_section
, len
);
4181 memcpy(ptr
, funcname
, len
);
4186 #ifdef TCC_TARGET_PE
4187 t
= VT_SHORT
| VT_UNSIGNED
;
4193 /* string parsing */
4196 if (tcc_state
->warn_write_strings
)
4201 memset(&ad
, 0, sizeof(AttributeDef
));
4202 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, 0);
4207 if (parse_btype(&type
, &ad
)) {
4208 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
4210 /* check ISOC99 compound literal */
4212 /* data is allocated locally by default */
4217 /* all except arrays are lvalues */
4218 if (!(type
.t
& VT_ARRAY
))
4219 r
|= lvalue_type(type
.t
);
4220 memset(&ad
, 0, sizeof(AttributeDef
));
4221 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, 0);
4223 if (sizeof_caller
) {
4230 } else if (tok
== '{') {
4232 tcc_error("expected constant");
4233 /* save all registers */
4236 /* statement expression : we do not accept break/continue
4237 inside as GCC does */
4238 block(NULL
, NULL
, 1);
4253 /* functions names must be treated as function pointers,
4254 except for unary '&' and sizeof. Since we consider that
4255 functions are not lvalues, we only have to handle it
4256 there and in function calls. */
4257 /* arrays can also be used although they are not lvalues */
4258 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
4259 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
4261 mk_pointer(&vtop
->type
);
4267 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4269 boolean
.t
= VT_BOOL
;
4271 vtop
->c
.i
= !vtop
->c
.i
;
4272 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
4276 vseti(VT_JMP
, gvtst(1, 0));
4288 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
4289 tcc_error("pointer not accepted for unary plus");
4290 /* In order to force cast, we add zero, except for floating point
4291 where we really need an noop (otherwise -0.0 will be transformed
4293 if (!is_float(vtop
->type
.t
)) {
4304 unary_type(&type
); // Perform a in_sizeof = 0;
4305 size
= type_size(&type
, &align
);
4306 if (t
== TOK_SIZEOF
) {
4307 if (!(type
.t
& VT_VLA
)) {
4309 tcc_error("sizeof applied to an incomplete type");
4312 vla_runtime_type_size(&type
, &align
);
4317 vtop
->type
.t
|= VT_UNSIGNED
;
4320 case TOK_builtin_expect
:
4322 /* __builtin_expect is a no-op for now */
4323 int saved_nocode_wanted
;
4328 saved_nocode_wanted
= nocode_wanted
;
4332 nocode_wanted
= saved_nocode_wanted
;
4336 case TOK_builtin_types_compatible_p
:
4345 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
4346 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
4347 vpushi(is_compatible_types(&type1
, &type2
));
4350 case TOK_builtin_choose_expr
:
4352 int saved_nocode_wanted
, c
;
4358 saved_nocode_wanted
= nocode_wanted
;
4364 nocode_wanted
= saved_nocode_wanted
;
4368 saved_nocode_wanted
= nocode_wanted
;
4374 nocode_wanted
= saved_nocode_wanted
;
4379 case TOK_builtin_constant_p
:
4381 int saved_nocode_wanted
, res
;
4384 saved_nocode_wanted
= nocode_wanted
;
4387 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
4389 nocode_wanted
= saved_nocode_wanted
;
4394 case TOK_builtin_frame_address
:
4395 case TOK_builtin_return_address
:
4402 if (tok
!= TOK_CINT
) {
4403 tcc_error("%s only takes positive integers",
4404 tok1
== TOK_builtin_return_address
?
4405 "__builtin_return_address" :
4406 "__builtin_frame_address");
4408 level
= (uint32_t)tokc
.i
;
4413 vset(&type
, VT_LOCAL
, 0); /* local frame */
4415 mk_pointer(&vtop
->type
);
4416 indir(); /* -> parent frame */
4418 if (tok1
== TOK_builtin_return_address
) {
4419 // assume return address is just above frame pointer on stack
4422 mk_pointer(&vtop
->type
);
4427 #ifdef TCC_TARGET_X86_64
4428 #ifdef TCC_TARGET_PE
4429 case TOK_builtin_va_start
:
4437 if ((vtop
->r
& VT_VALMASK
) != VT_LOCAL
)
4438 tcc_error("__builtin_va_start expects a local variable");
4439 vtop
->r
&= ~(VT_LVAL
| VT_REF
);
4440 vtop
->type
= char_pointer_type
;
4446 case TOK_builtin_va_arg_types
:
4453 vpushi(classify_x86_64_va_arg(&type
));
4459 #ifdef TCC_TARGET_ARM64
4460 case TOK___va_start
: {
4462 tcc_error("statement in global scope");
4472 vtop
->type
.t
= VT_VOID
;
4475 case TOK___va_arg
: {
4478 tcc_error("statement in global scope");
4490 case TOK___arm64_clear_cache
: {
4499 vtop
->type
.t
= VT_VOID
;
4503 /* pre operations */
4514 t
= vtop
->type
.t
& VT_BTYPE
;
4516 /* In IEEE negate(x) isn't subtract(0,x), but rather
4521 else if (t
== VT_DOUBLE
)
4532 goto tok_identifier
;
4534 /* allow to take the address of a label */
4535 if (tok
< TOK_UIDENT
)
4536 expect("label identifier");
4537 s
= label_find(tok
);
4539 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4541 if (s
->r
== LABEL_DECLARED
)
4542 s
->r
= LABEL_FORWARD
;
4545 s
->type
.t
= VT_VOID
;
4546 mk_pointer(&s
->type
);
4547 s
->type
.t
|= VT_STATIC
;
4549 vpushsym(&s
->type
, s
);
4553 // special qnan , snan and infinity values
4555 vpush64(VT_DOUBLE
, 0x7ff8000000000000ULL
);
4559 vpush64(VT_DOUBLE
, 0x7ff0000000000001ULL
);
4563 vpush64(VT_DOUBLE
, 0x7ff0000000000000ULL
);
4572 expect("identifier");
4575 const char *name
= get_tok_str(t
, NULL
);
4577 tcc_error("'%s' undeclared", name
);
4578 /* for simple function calls, we tolerate undeclared
4579 external reference to int() function */
4580 if (tcc_state
->warn_implicit_function_declaration
4581 #ifdef TCC_TARGET_PE
4582 /* people must be warned about using undeclared WINAPI functions
4583 (which usually start with uppercase letter) */
4584 || (name
[0] >= 'A' && name
[0] <= 'Z')
4587 tcc_warning("implicit declaration of function '%s'", name
);
4588 s
= external_global_sym(t
, &func_old_type
, 0);
4590 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
4591 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
4592 /* if referencing an inline function, then we generate a
4593 symbol to it if not already done. It will have the
4594 effect to generate code for it at the end of the
4595 compilation unit. Inline function as always
4596 generated in the text section. */
4597 if (!s
->c
&& !nocode_wanted
)
4598 put_extern_sym(s
, text_section
, 0, 0);
4599 r
= VT_SYM
| VT_CONST
;
4603 vset(&s
->type
, r
, s
->c
);
4604 /* if forward reference, we must point to s */
4605 if (vtop
->r
& VT_SYM
) {
4612 /* post operations */
4614 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
4617 } else if (tok
== '.' || tok
== TOK_ARROW
|| tok
== TOK_CDOUBLE
) {
4620 if (tok
== TOK_ARROW
)
4622 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
4625 /* expect pointer on structure */
4626 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
4627 expect("struct or union");
4628 if (tok
== TOK_CDOUBLE
)
4629 expect("field name");
4631 if (tok
== TOK_CINT
|| tok
== TOK_CUINT
)
4632 expect("field name");
4633 s
= find_field(&vtop
->type
, tok
);
4635 tcc_error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, &tokc
));
4636 /* add field offset to pointer */
4637 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
4640 /* change type to field type, and set to lvalue */
4641 vtop
->type
= s
->type
;
4642 vtop
->type
.t
|= qualifiers
;
4643 /* an array is never an lvalue */
4644 if (!(vtop
->type
.t
& VT_ARRAY
)) {
4645 vtop
->r
|= lvalue_type(vtop
->type
.t
);
4646 #ifdef CONFIG_TCC_BCHECK
4647 /* if bound checking, the referenced pointer must be checked */
4648 if (tcc_state
->do_bounds_check
&& (vtop
->r
& VT_VALMASK
) != VT_LOCAL
)
4649 vtop
->r
|= VT_MUSTBOUND
;
4653 } else if (tok
== '[') {
4659 } else if (tok
== '(') {
4662 int nb_args
, ret_nregs
, ret_align
, regsize
, variadic
;
4665 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
4666 /* pointer test (no array accepted) */
4667 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
4668 vtop
->type
= *pointed_type(&vtop
->type
);
4669 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
4673 expect("function pointer");
4676 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
4678 /* get return type */
4681 sa
= s
->next
; /* first parameter */
4684 /* compute first implicit argument if a structure is returned */
4685 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
4686 variadic
= (s
->c
== FUNC_ELLIPSIS
);
4687 ret_nregs
= gfunc_sret(&s
->type
, variadic
, &ret
.type
,
4688 &ret_align
, ®size
);
4690 /* get some space for the returned structure */
4691 size
= type_size(&s
->type
, &align
);
4692 #ifdef TCC_TARGET_ARM64
4693 /* On arm64, a small struct is return in registers.
4694 It is much easier to write it to memory if we know
4695 that we are allowed to write some extra bytes, so
4696 round the allocated space up to a power of 2: */
4698 while (size
& (size
- 1))
4699 size
= (size
| (size
- 1)) + 1;
4701 loc
= (loc
- size
) & -align
;
4703 ret
.r
= VT_LOCAL
| VT_LVAL
;
4704 /* pass it as 'int' to avoid structure arg passing
4706 vseti(VT_LOCAL
, loc
);
4716 /* return in register */
4717 if (is_float(ret
.type
.t
)) {
4718 ret
.r
= reg_fret(ret
.type
.t
);
4719 #ifdef TCC_TARGET_X86_64
4720 if ((ret
.type
.t
& VT_BTYPE
) == VT_QFLOAT
)
4724 #ifndef TCC_TARGET_ARM64
4725 #ifdef TCC_TARGET_X86_64
4726 if ((ret
.type
.t
& VT_BTYPE
) == VT_QLONG
)
4728 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
4739 gfunc_param_typed(s
, sa
);
4749 tcc_error("too few arguments to function");
4751 if (!nocode_wanted
) {
4752 gfunc_call(nb_args
);
4754 vtop
-= (nb_args
+ 1);
4758 for (r
= ret
.r
+ ret_nregs
+ !ret_nregs
; r
-- > ret
.r
;) {
4759 vsetc(&ret
.type
, r
, &ret
.c
);
4760 vtop
->r2
= ret
.r2
; /* Loop only happens when r2 is VT_CONST */
4763 /* handle packed struct return */
4764 if (((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) && ret_nregs
) {
4767 size
= type_size(&s
->type
, &align
);
4768 /* We're writing whole regs often, make sure there's enough
4769 space. Assume register size is power of 2. */
4770 if (regsize
> align
)
4772 loc
= (loc
- size
) & -align
;
4776 vset(&ret
.type
, VT_LOCAL
| VT_LVAL
, addr
+ offset
);
4780 if (--ret_nregs
== 0)
4784 vset(&s
->type
, VT_LOCAL
| VT_LVAL
, addr
);
4792 ST_FUNC
void expr_prod(void)
4797 while (tok
== '*' || tok
== '/' || tok
== '%') {
4805 ST_FUNC
void expr_sum(void)
4810 while (tok
== '+' || tok
== '-') {
4818 static void expr_shift(void)
4823 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
4831 static void expr_cmp(void)
4836 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
4837 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
4845 static void expr_cmpeq(void)
4850 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
4858 static void expr_and(void)
4861 while (tok
== '&') {
4868 static void expr_xor(void)
4871 while (tok
== '^') {
4878 static void expr_or(void)
4881 while (tok
== '|') {
4888 /* XXX: fix this mess */
4889 static void expr_land_const(void)
4892 while (tok
== TOK_LAND
) {
4898 static void expr_lor_const(void)
4901 while (tok
== TOK_LOR
) {
4908 static void expr_land(void)
4911 if (tok
== TOK_LAND
) {
4914 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4921 int saved_nocode_wanted
= nocode_wanted
;
4923 while (tok
== TOK_LAND
) {
4930 nocode_wanted
= saved_nocode_wanted
;
4931 gen_cast(&int_type
);
4939 if (tok
!= TOK_LAND
) {
4952 static void expr_lor(void)
4955 if (tok
== TOK_LOR
) {
4958 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4965 int saved_nocode_wanted
= nocode_wanted
;
4967 while (tok
== TOK_LOR
) {
4974 nocode_wanted
= saved_nocode_wanted
;
4975 gen_cast(&int_type
);
4983 if (tok
!= TOK_LOR
) {
4996 /* Assuming vtop is a value used in a conditional context
4997 (i.e. compared with zero) return 0 if it's false, 1 if
4998 true and -1 if it can't be statically determined. */
4999 static int condition_3way(void)
5002 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
&&
5003 (!(vtop
->r
& VT_SYM
) ||
5004 !(vtop
->sym
->type
.t
& VT_WEAK
))) {
5006 boolean
.t
= VT_BOOL
;
5015 static void expr_cond(void)
5017 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
, islv
;
5020 CType type
, type1
, type2
;
5025 c
= condition_3way();
5027 int saved_nocode_wanted
= nocode_wanted
;
5029 if (tok
!= ':' || !gnu_ext
) {
5037 nocode_wanted
= saved_nocode_wanted
;
5040 if (tok
!= ':' || !gnu_ext
) {
5044 nocode_wanted
= saved_nocode_wanted
;
5051 /* XXX This doesn't handle nocode_wanted correctly at all.
5052 It unconditionally calls gv/gvtst and friends. That's
5053 the case for many of the expr_ routines. Currently
5054 that should generate only useless code, but depending
5055 on other operand handling this might also generate
5056 pointer derefs for lvalue conversions whose result
5057 is useless, but nevertheless can lead to segfault.
5059 Somewhen we need to overhaul the whole nocode_wanted
5061 if (vtop
!= vstack
) {
5062 /* needed to avoid having different registers saved in
5064 if (is_float(vtop
->type
.t
)) {
5066 #ifdef TCC_TARGET_X86_64
5067 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
5077 if (tok
== ':' && gnu_ext
) {
5085 sv
= *vtop
; /* save value to handle it later */
5086 vtop
--; /* no vpop so that FP stack is not flushed */
5094 bt1
= t1
& VT_BTYPE
;
5096 bt2
= t2
& VT_BTYPE
;
5097 /* cast operands to correct type according to ISOC rules */
5098 if (is_float(bt1
) || is_float(bt2
)) {
5099 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
5100 type
.t
= VT_LDOUBLE
;
5101 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
5106 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
5107 /* cast to biggest op */
5109 /* convert to unsigned if it does not fit in a long long */
5110 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
5111 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
5112 type
.t
|= VT_UNSIGNED
;
5113 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
5114 /* If one is a null ptr constant the result type
5116 if (is_null_pointer (vtop
))
5118 else if (is_null_pointer (&sv
))
5120 /* XXX: test pointer compatibility, C99 has more elaborate
5124 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
5125 /* XXX: test function pointer compatibility */
5126 type
= bt1
== VT_FUNC
? type1
: type2
;
5127 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
5128 /* XXX: test structure compatibility */
5129 type
= bt1
== VT_STRUCT
? type1
: type2
;
5130 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
5131 /* NOTE: as an extension, we accept void on only one side */
5134 /* integer operations */
5136 /* convert to unsigned if it does not fit in an integer */
5137 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
5138 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
5139 type
.t
|= VT_UNSIGNED
;
5141 /* keep structs lvalue by transforming `(expr ? a : b)` to `*(expr ? &a : &b)` so
5142 that `(expr ? a : b).mem` does not error with "lvalue expected" */
5143 islv
= (vtop
->r
& VT_LVAL
) && (sv
.r
& VT_LVAL
) && VT_STRUCT
== (type
.t
& VT_BTYPE
);
5145 /* now we convert second operand */
5148 mk_pointer(&vtop
->type
);
5151 else if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
5154 if (is_float(type
.t
)) {
5156 #ifdef TCC_TARGET_X86_64
5157 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
5161 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
5162 /* for long longs, we use fixed registers to avoid having
5163 to handle a complicated move */
5168 /* this is horrible, but we must also convert first
5172 /* put again first value and cast it */
5176 mk_pointer(&vtop
->type
);
5179 else if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
5182 move_reg(r2
, r1
, type
.t
);
5191 static void expr_eq(void)
5197 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
5198 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
5199 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
5214 ST_FUNC
void gexpr(void)
5225 /* parse an expression and return its type without any side effect. */
5226 static void expr_type(CType
*type
)
5228 int saved_nocode_wanted
;
5230 saved_nocode_wanted
= nocode_wanted
;
5235 nocode_wanted
= saved_nocode_wanted
;
5238 /* parse a unary expression and return its type without any side
5240 static void unary_type(CType
*type
)
5252 /* parse a constant expression and return value in vtop. */
5253 static void expr_const1(void)
5262 /* parse an integer constant and return its value. */
5263 ST_FUNC
int expr_const(void)
5267 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
5268 expect("constant expression");
5274 /* return the label token if current token is a label, otherwise
5276 static int is_label(void)
5280 /* fast test first */
5281 if (tok
< TOK_UIDENT
)
5283 /* no need to save tokc because tok is an identifier */
5290 unget_tok(last_tok
);
5295 static void label_or_decl(int l
)
5299 /* fast test first */
5300 if (tok
>= TOK_UIDENT
)
5302 /* no need to save tokc because tok is an identifier */
5306 unget_tok(last_tok
);
5309 unget_tok(last_tok
);
5314 static int case_cmp(const void *pa
, const void *pb
)
5316 int a
= (*(struct case_t
**) pa
)->v1
;
5317 int b
= (*(struct case_t
**) pb
)->v1
;
5318 return a
< b
? -1 : a
> b
;
5321 static int gcase(struct case_t
**base
, int len
, int case_reg
, int *bsym
)
5333 case_reg
= gv(RC_INT
);
5339 gtst_addr(0, p
->sym
); /* v1 <= x <= v2 */
5340 case_reg
= gv(RC_INT
);
5343 case_reg
= gcase(base
, len
/2, case_reg
, bsym
);
5344 if (cur_switch
->def_sym
)
5345 gjmp_addr(cur_switch
->def_sym
);
5347 *bsym
= gjmp(*bsym
);
5351 base
+= e
; len
-= e
;
5359 if (p
->v1
== p
->v2
) {
5361 gtst_addr(0, p
->sym
);
5365 case_reg
= gv(RC_INT
);
5371 gtst_addr(0, p
->sym
);
5374 case_reg
= gv(RC_INT
);
5380 static void block(int *bsym
, int *csym
, int is_expr
)
5382 int a
, b
, c
, d
, cond
;
5385 /* generate line number info */
5386 if (tcc_state
->do_debug
&&
5387 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
5388 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
5390 last_line_num
= file
->line_num
;
5394 /* default return value is (void) */
5396 vtop
->type
.t
= VT_VOID
;
5399 if (tok
== TOK_IF
) {
5401 int saved_nocode_wanted
= nocode_wanted
;
5406 cond
= condition_3way();
5410 block(bsym
, csym
, 0);
5412 nocode_wanted
= saved_nocode_wanted
;
5414 if (c
== TOK_ELSE
) {
5420 block(bsym
, csym
, 0);
5421 gsym(d
); /* patch else jmp */
5423 nocode_wanted
= saved_nocode_wanted
;
5426 } else if (tok
== TOK_WHILE
) {
5427 int saved_nocode_wanted
;
5428 nocode_wanted
&= ~2;
5438 saved_nocode_wanted
= nocode_wanted
;
5440 nocode_wanted
= saved_nocode_wanted
;
5446 } else if (tok
== '{') {
5448 int block_vla_sp_loc
= vla_sp_loc
, saved_vlas_in_scope
= vlas_in_scope
;
5451 /* record local declaration stack position */
5453 llabel
= local_label_stack
;
5456 /* handle local labels declarations */
5457 if (tok
== TOK_LABEL
) {
5460 if (tok
< TOK_UIDENT
)
5461 expect("label identifier");
5462 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
5472 while (tok
!= '}') {
5473 label_or_decl(VT_LOCAL
);
5477 block(bsym
, csym
, is_expr
);
5480 /* pop locally defined labels */
5481 label_pop(&local_label_stack
, llabel
);
5482 /* pop locally defined symbols */
5484 /* In the is_expr case (a statement expression is finished here),
5485 vtop might refer to symbols on the local_stack. Either via the
5486 type or via vtop->sym. We can't pop those nor any that in turn
5487 might be referred to. To make it easier we don't roll back
5488 any symbols in that case; some upper level call to block() will
5489 do that. We do have to remove such symbols from the lookup
5490 tables, though. sym_pop will do that. */
5491 sym_pop(&local_stack
, s
, is_expr
);
5493 /* Pop VLA frames and restore stack pointer if required */
5494 if (vlas_in_scope
> saved_vlas_in_scope
) {
5495 vla_sp_loc
= saved_vlas_in_scope
? block_vla_sp_loc
: vla_sp_root_loc
;
5498 vlas_in_scope
= saved_vlas_in_scope
;
5501 } else if (tok
== TOK_RETURN
) {
5505 gen_assign_cast(&func_vt
);
5506 #ifdef TCC_TARGET_ARM64
5507 // Perhaps it would be better to use this for all backends:
5510 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
5511 CType type
, ret_type
;
5512 int ret_align
, ret_nregs
, regsize
;
5513 ret_nregs
= gfunc_sret(&func_vt
, func_var
, &ret_type
,
5514 &ret_align
, ®size
);
5515 if (0 == ret_nregs
) {
5516 /* if returning structure, must copy it to implicit
5517 first pointer arg location */
5520 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
5523 /* copy structure value to pointer */
5526 /* returning structure packed into registers */
5527 int r
, size
, addr
, align
;
5528 size
= type_size(&func_vt
,&align
);
5529 if ((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) ||
5530 (vtop
->c
.i
& (ret_align
-1)))
5531 && (align
& (ret_align
-1))) {
5532 loc
= (loc
- size
) & -ret_align
;
5535 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
5539 vset(&ret_type
, VT_LOCAL
| VT_LVAL
, addr
);
5541 vtop
->type
= ret_type
;
5542 if (is_float(ret_type
.t
))
5543 r
= rc_fret(ret_type
.t
);
5554 if (--ret_nregs
== 0)
5556 /* We assume that when a structure is returned in multiple
5557 registers, their classes are consecutive values of the
5560 vtop
->c
.i
+= regsize
;
5564 } else if (is_float(func_vt
.t
)) {
5565 gv(rc_fret(func_vt
.t
));
5570 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
5573 /* jump unless last stmt in top-level block */
5574 if (tok
!= '}' || local_scope
!= 1)
5577 } else if (tok
== TOK_BREAK
) {
5580 tcc_error("cannot break");
5581 *bsym
= gjmp(*bsym
);
5585 } else if (tok
== TOK_CONTINUE
) {
5588 tcc_error("cannot continue");
5589 vla_sp_restore_root();
5590 *csym
= gjmp(*csym
);
5593 } else if (tok
== TOK_FOR
) {
5595 int saved_nocode_wanted
;
5596 nocode_wanted
&= ~2;
5602 /* c99 for-loop init decl? */
5603 if (!decl0(VT_LOCAL
, 1)) {
5604 /* no, regular for-loop init expr */
5630 saved_nocode_wanted
= nocode_wanted
;
5632 nocode_wanted
= saved_nocode_wanted
;
5638 sym_pop(&local_stack
, s
, 0);
5641 if (tok
== TOK_DO
) {
5642 int saved_nocode_wanted
;
5643 nocode_wanted
&= ~2;
5649 saved_nocode_wanted
= nocode_wanted
;
5651 nocode_wanted
= saved_nocode_wanted
;
5663 if (tok
== TOK_SWITCH
) {
5664 struct switch_t
*saved
, sw
;
5665 int saved_nocode_wanted
= nocode_wanted
;
5669 /* XXX: other types than integer */
5674 b
= gjmp(0); /* jump to first case */
5675 sw
.p
= NULL
; sw
.n
= 0; sw
.def_sym
= 0;
5679 nocode_wanted
= saved_nocode_wanted
;
5680 a
= gjmp(a
); /* add implicit break */
5683 qsort(sw
.p
, sw
.n
, sizeof(void*), case_cmp
);
5684 for (b
= 1; b
< sw
.n
; b
++)
5685 if (sw
.p
[b
- 1]->v2
>= sw
.p
[b
]->v1
)
5686 tcc_error("duplicate case value");
5687 gcase(sw
.p
, sw
.n
, c
, &a
);
5689 gjmp_addr(sw
.def_sym
);
5690 dynarray_reset(&sw
.p
, &sw
.n
);
5695 if (tok
== TOK_CASE
) {
5696 struct case_t
*cr
= tcc_malloc(sizeof(struct case_t
));
5699 nocode_wanted
&= ~2;
5701 cr
->v1
= cr
->v2
= expr_const();
5702 if (gnu_ext
&& tok
== TOK_DOTS
) {
5704 cr
->v2
= expr_const();
5705 if (cr
->v2
< cr
->v1
)
5706 tcc_warning("empty case range");
5709 dynarray_add((void***) &cur_switch
->p
, &cur_switch
->n
, cr
);
5712 goto block_after_label
;
5714 if (tok
== TOK_DEFAULT
) {
5719 if (cur_switch
->def_sym
)
5720 tcc_error("too many 'default'");
5721 cur_switch
->def_sym
= ind
;
5723 goto block_after_label
;
5725 if (tok
== TOK_GOTO
) {
5727 if (tok
== '*' && gnu_ext
) {
5731 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
5737 } else if (tok
>= TOK_UIDENT
) {
5738 s
= label_find(tok
);
5739 /* put forward definition if needed */
5741 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
5743 if (s
->r
== LABEL_DECLARED
)
5744 s
->r
= LABEL_FORWARD
;
5746 vla_sp_restore_root();
5749 else if (s
->r
& LABEL_FORWARD
)
5750 s
->jnext
= gjmp(s
->jnext
);
5752 gjmp_addr(s
->jnext
);
5755 expect("label identifier");
5758 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
5766 if (s
->r
== LABEL_DEFINED
)
5767 tcc_error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
5769 s
->r
= LABEL_DEFINED
;
5771 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
5775 /* we accept this, but it is a mistake */
5777 nocode_wanted
&= ~2;
5779 tcc_warning("deprecated use of label at end of compound statement");
5783 block(bsym
, csym
, is_expr
);
5786 /* expression case */
5801 #define EXPR_CONST 1
5804 static void parse_init_elem(int expr_type
)
5806 int saved_global_expr
;
5809 /* compound literals must be allocated globally in this case */
5810 saved_global_expr
= global_expr
;
5813 global_expr
= saved_global_expr
;
5814 /* NOTE: symbols are accepted */
5815 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
5816 tcc_error("initializer element is not constant");
5824 /* t is the array or struct type. c is the array or struct
5825 address. cur_field is the pointer to the current
5826 value, for arrays the 'c' member contains the current start
5827 index and the 'r' contains the end index (in case of range init).
5828 'size_only' is true if only size info is needed (only used
5830 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
5831 Sym
**cur_field
, int size_only
)
5834 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
5840 if (gnu_ext
&& (l
= is_label()) != 0)
5842 while (tok
== '[' || tok
== '.') {
5844 if (!(type
->t
& VT_ARRAY
))
5845 expect("array type");
5848 index
= expr_const();
5849 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
5850 tcc_error("invalid index");
5851 if (tok
== TOK_DOTS
&& gnu_ext
) {
5853 index_last
= expr_const();
5854 if (index_last
< 0 ||
5855 (s
->c
>= 0 && index_last
>= s
->c
) ||
5857 tcc_error("invalid index");
5863 (*cur_field
)->c
= index
;
5864 (*cur_field
)->r
= index_last
;
5866 type
= pointed_type(type
);
5867 elem_size
= type_size(type
, &align
);
5868 c
+= index
* elem_size
;
5869 /* NOTE: we only support ranges for last designator */
5870 nb_elems
= index_last
- index
+ 1;
5871 if (nb_elems
!= 1) {
5880 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
5881 expect("struct/union type");
5882 f
= find_field(type
, l
);
5887 /* XXX: fix this mess by using explicit storage field */
5889 type1
.t
|= (type
->t
& ~VT_TYPE
);
5903 if (type
->t
& VT_ARRAY
) {
5904 index
= (*cur_field
)->c
;
5905 if (type
->ref
->c
>= 0 && index
>= type
->ref
->c
)
5906 tcc_error("index too large");
5907 type
= pointed_type(type
);
5908 c
+= index
* type_size(type
, &align
);
5912 tcc_error("too many field init");
5913 /* XXX: fix this mess by using explicit storage field */
5915 type1
.t
|= (type
->t
& ~VT_TYPE
);
5920 decl_initializer(type
, sec
, c
, 0, size_only
);
5922 /* XXX: make it more general */
5923 if (!size_only
&& nb_elems
> 1) {
5924 unsigned long c_end
;
5929 vset(type
, VT_LOCAL
|VT_LVAL
, c
);
5930 for (i
= 1; i
< nb_elems
; i
++) {
5931 vset(type
, VT_LOCAL
|VT_LVAL
, c
+ elem_size
* i
);
5937 c_end
= c
+ nb_elems
* elem_size
;
5938 if (c_end
> sec
->data_allocated
)
5939 section_realloc(sec
, c_end
);
5940 src
= sec
->data
+ c
;
5942 for(i
= 1; i
< nb_elems
; i
++) {
5944 memcpy(dst
, src
, elem_size
);
5950 /* store a value or an expression directly in global data or in local array */
5951 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
)
5953 int bt
, bit_pos
, bit_size
;
5955 unsigned long long bit_mask
;
5959 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
5963 /* XXX: not portable */
5964 /* XXX: generate error if incorrect relocation */
5965 gen_assign_cast(&dtype
);
5966 bt
= type
->t
& VT_BTYPE
;
5967 size
= type_size(type
, &align
);
5968 if (c
+ size
> sec
->data_allocated
) {
5969 section_realloc(sec
, c
+ size
);
5971 ptr
= sec
->data
+ c
;
5972 /* XXX: make code faster ? */
5973 if (!(type
->t
& VT_BITFIELD
)) {
5975 bit_size
= PTR_SIZE
* 8;
5978 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5979 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
5980 bit_mask
= (1LL << bit_size
) - 1;
5982 if ((vtop
->r
& (VT_SYM
|VT_CONST
)) == (VT_SYM
|VT_CONST
) &&
5983 vtop
->sym
->v
>= SYM_FIRST_ANOM
&&
5984 /* XXX This rejects compount literals like
5985 '(void *){ptr}'. The problem is that '&sym' is
5986 represented the same way, which would be ruled out
5987 by the SYM_FIRST_ANOM check above, but also '"string"'
5988 in 'char *p = "string"' is represented the same
5989 with the type being VT_PTR and the symbol being an
5990 anonymous one. That is, there's no difference in vtop
5991 between '(void *){x}' and '&(void *){x}'. Ignore
5992 pointer typed entities here. Hopefully no real code
5993 will every use compound literals with scalar type. */
5994 (vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
5995 /* These come from compound literals, memcpy stuff over. */
5998 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[vtop
->sym
->c
];
5999 ssec
= tcc_state
->sections
[esym
->st_shndx
];
6000 memmove (ptr
, ssec
->data
+ esym
->st_value
, size
);
6002 if ((vtop
->r
& VT_SYM
) &&
6008 (bt
== VT_LLONG
&& bit_size
!= 64) ||
6012 (bt
== VT_INT
&& bit_size
!= 32)
6015 tcc_error("initializer element is not computable at load time");
6017 /* XXX: when cross-compiling we assume that each type has the
6018 same representation on host and target, which is likely to
6019 be wrong in the case of long double */
6021 vtop
->c
.i
= (vtop
->c
.i
!= 0);
6023 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
6026 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
6029 *(double *)ptr
= vtop
->c
.d
;
6032 if (sizeof(long double) == LDOUBLE_SIZE
)
6033 *(long double *)ptr
= vtop
->c
.ld
;
6034 else if (sizeof(double) == LDOUBLE_SIZE
)
6035 *(double *)ptr
= vtop
->c
.ld
;
6037 tcc_error("can't cross compile long double constants");
6041 *(long long *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
6048 addr_t val
= (vtop
->c
.i
& bit_mask
) << bit_pos
;
6049 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
6050 if (vtop
->r
& VT_SYM
)
6051 greloca(sec
, vtop
->sym
, c
, R_DATA_PTR
, val
);
6053 *(addr_t
*)ptr
|= val
;
6055 if (vtop
->r
& VT_SYM
)
6056 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
6057 *(addr_t
*)ptr
|= val
;
6063 int val
= (vtop
->c
.i
& bit_mask
) << bit_pos
;
6064 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
6065 if (vtop
->r
& VT_SYM
)
6066 greloca(sec
, vtop
->sym
, c
, R_DATA_PTR
, val
);
6070 if (vtop
->r
& VT_SYM
)
6071 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
6080 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
6087 /* put zeros for variable based init */
6088 static void init_putz(Section
*sec
, unsigned long c
, int size
)
6091 /* nothing to do because globals are already set to zero */
6093 vpush_global_sym(&func_old_type
, TOK_memset
);
6095 #ifdef TCC_TARGET_ARM
6106 /* 't' contains the type and storage info. 'c' is the offset of the
6107 object in section 'sec'. If 'sec' is NULL, it means stack based
6108 allocation. 'first' is true if array '{' must be read (multi
6109 dimension implicit array init handling). 'size_only' is true if
6110 size only evaluation is wanted (only for arrays). */
6111 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
6112 int first
, int size_only
)
6114 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, parlevel1
, i
;
6121 /* If we currently are at an '}' or ',' we have read an initializer
6122 element in one of our callers, and not yet consumed it. */
6123 have_elem
= tok
== '}' || tok
== ',';
6124 if (!have_elem
&& tok
!= '{' &&
6125 /* In case of strings we have special handling for arrays, so
6126 don't consume them as initializer value (which would commit them
6127 to some anonymous symbol). */
6128 tok
!= TOK_LSTR
&& tok
!= TOK_STR
&&
6130 parse_init_elem(!sec
? EXPR_ANY
: EXPR_CONST
);
6135 !(type
->t
& VT_ARRAY
) &&
6136 /* Use i_c_parameter_t, to strip toplevel qualifiers.
6137 The source type might have VT_CONSTANT set, which is
6138 of course assignable to non-const elements. */
6139 is_compatible_parameter_types(type
, &vtop
->type
)) {
6140 init_putv(type
, sec
, c
);
6141 } else if (type
->t
& VT_ARRAY
) {
6145 t1
= pointed_type(type
);
6146 size1
= type_size(t1
, &align1
);
6149 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
6152 tcc_error("character array initializer must be a literal,"
6153 " optionally enclosed in braces");
6158 /* only parse strings here if correct type (otherwise: handle
6159 them as ((w)char *) expressions */
6160 if ((tok
== TOK_LSTR
&&
6161 #ifdef TCC_TARGET_PE
6162 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
6164 (t1
->t
& VT_BTYPE
) == VT_INT
6166 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
6167 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
6170 /* compute maximum number of chars wanted */
6172 cstr_len
= tokc
.str
.size
;
6174 cstr_len
= tokc
.str
.size
/ sizeof(nwchar_t
);
6177 if (n
>= 0 && nb
> (n
- array_length
))
6178 nb
= n
- array_length
;
6181 tcc_warning("initializer-string for array is too long");
6182 /* in order to go faster for common case (char
6183 string in global variable, we handle it
6185 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
6186 memcpy(sec
->data
+ c
+ array_length
, tokc
.str
.data
, nb
);
6190 ch
= ((unsigned char *)tokc
.str
.data
)[i
];
6192 ch
= ((nwchar_t
*)tokc
.str
.data
)[i
];
6194 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
);
6201 /* only add trailing zero if enough storage (no
6202 warning in this case since it is standard) */
6203 if (n
< 0 || array_length
< n
) {
6206 init_putv(t1
, sec
, c
+ (array_length
* size1
));
6216 while (tok
!= '}' || have_elem
) {
6217 decl_designator(type
, sec
, c
, &f
, size_only
);
6220 /* must put zero in holes (note that doing it that way
6221 ensures that it even works with designators) */
6222 if (!size_only
&& array_length
< index
) {
6223 init_putz(sec
, c
+ array_length
* size1
,
6224 (index
- array_length
) * size1
);
6226 if (type
->t
& VT_ARRAY
) {
6227 index
= indexsym
.c
= ++indexsym
.r
;
6229 index
= index
+ type_size(&f
->type
, &align1
);
6230 if (s
->type
.t
== TOK_UNION
)
6235 if (index
> array_length
)
6236 array_length
= index
;
6238 if (type
->t
& VT_ARRAY
) {
6239 /* special test for multi dimensional arrays (may not
6240 be strictly correct if designators are used at the
6242 if (no_oblock
&& index
>= n
)
6245 if (no_oblock
&& f
== NULL
)
6253 /* put zeros at the end */
6254 if (!size_only
&& array_length
< n
) {
6255 init_putz(sec
, c
+ array_length
* size1
,
6256 (n
- array_length
) * size1
);
6260 /* patch type size if needed, which happens only for array types */
6262 s
->c
= array_length
;
6263 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
6266 if (first
|| tok
== '{') {
6275 } else if (tok
== '{') {
6277 decl_initializer(type
, sec
, c
, first
, size_only
);
6279 } else if (size_only
) {
6280 /* If we supported only ISO C we wouldn't have to accept calling
6281 this on anything than an array size_only==1 (and even then
6282 only on the outermost level, so no recursion would be needed),
6283 because initializing a flex array member isn't supported.
6284 But GNU C supports it, so we need to recurse even into
6285 subfields of structs and arrays when size_only is set. */
6286 /* just skip expression */
6287 parlevel
= parlevel1
= 0;
6288 while ((parlevel
> 0 || parlevel1
> 0 ||
6289 (tok
!= '}' && tok
!= ',')) && tok
!= -1) {
6292 else if (tok
== ')') {
6293 if (parlevel
== 0 && parlevel1
== 0)
6297 else if (tok
== '{')
6299 else if (tok
== '}') {
6300 if (parlevel
== 0 && parlevel1
== 0)
6308 /* This should happen only when we haven't parsed
6309 the init element above for fear of committing a
6310 string constant to memory too early. */
6311 if (tok
!= TOK_STR
&& tok
!= TOK_LSTR
)
6312 expect("string constant");
6313 parse_init_elem(!sec
? EXPR_ANY
: EXPR_CONST
);
6315 init_putv(type
, sec
, c
);
6319 /* parse an initializer for type 't' if 'has_init' is non zero, and
6320 allocate space in local or global data space ('r' is either
6321 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
6322 variable 'v' of scope 'scope' is declared before initializers
6323 are parsed. If 'v' is zero, then a reference to the new object
6324 is put in the value stack. If 'has_init' is 2, a special parsing
6325 is done to handle string constants. */
6326 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
6327 int has_init
, int v
, int scope
)
6329 int size
, align
, addr
, data_offset
;
6331 ParseState saved_parse_state
= {0};
6332 TokenString
*init_str
= NULL
;
6334 Sym
*flexible_array
;
6336 flexible_array
= NULL
;
6337 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
6338 Sym
*field
= type
->ref
->next
;
6341 field
= field
->next
;
6342 if (field
->type
.t
& VT_ARRAY
&& field
->type
.ref
->c
< 0)
6343 flexible_array
= field
;
6347 size
= type_size(type
, &align
);
6348 /* If unknown size, we must evaluate it before
6349 evaluating initializers because
6350 initializers can generate global data too
6351 (e.g. string pointers or ISOC99 compound
6352 literals). It also simplifies local
6353 initializers handling */
6354 if (size
< 0 || (flexible_array
&& has_init
)) {
6356 tcc_error("unknown type size");
6357 /* get all init string */
6358 init_str
= tok_str_alloc();
6359 if (has_init
== 2) {
6360 /* only get strings */
6361 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
6362 tok_str_add_tok(init_str
);
6367 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
6369 tcc_error("unexpected end of file in initializer");
6370 tok_str_add_tok(init_str
);
6373 else if (tok
== '}') {
6383 tok_str_add(init_str
, -1);
6384 tok_str_add(init_str
, 0);
6387 save_parse_state(&saved_parse_state
);
6389 begin_macro(init_str
, 1);
6391 decl_initializer(type
, NULL
, 0, 1, 1);
6392 /* prepare second initializer parsing */
6393 macro_ptr
= init_str
->str
;
6396 /* if still unknown size, error */
6397 size
= type_size(type
, &align
);
6399 tcc_error("unknown type size");
6401 /* If there's a flex member and it was used in the initializer
6403 if (flexible_array
&&
6404 flexible_array
->type
.ref
->c
> 0)
6405 size
+= flexible_array
->type
.ref
->c
6406 * pointed_size(&flexible_array
->type
);
6407 /* take into account specified alignment if bigger */
6408 if (ad
->a
.aligned
) {
6409 if (ad
->a
.aligned
> align
)
6410 align
= ad
->a
.aligned
;
6411 } else if (ad
->a
.packed
) {
6414 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
6416 #ifdef CONFIG_TCC_BCHECK
6417 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
6421 loc
= (loc
- size
) & -align
;
6423 #ifdef CONFIG_TCC_BCHECK
6424 /* handles bounds */
6425 /* XXX: currently, since we do only one pass, we cannot track
6426 '&' operators, so we add only arrays */
6427 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
6429 /* add padding between regions */
6431 /* then add local bound info */
6432 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(addr_t
));
6433 bounds_ptr
[0] = addr
;
6434 bounds_ptr
[1] = size
;
6438 /* local variable */
6439 sym_push(v
, type
, r
, addr
);
6441 /* push local reference */
6442 vset(type
, r
, addr
);
6448 if (v
&& scope
== VT_CONST
) {
6449 /* see if the symbol was already defined */
6452 if (!is_compatible_types(&sym
->type
, type
))
6453 tcc_error("incompatible types for redefinition of '%s'",
6454 get_tok_str(v
, NULL
));
6455 if (sym
->type
.t
& VT_EXTERN
) {
6456 /* if the variable is extern, it was not allocated */
6457 sym
->type
.t
&= ~VT_EXTERN
;
6458 /* set array size if it was omitted in extern
6460 if ((sym
->type
.t
& VT_ARRAY
) &&
6461 sym
->type
.ref
->c
< 0 &&
6463 sym
->type
.ref
->c
= type
->ref
->c
;
6465 /* we accept several definitions of the same
6466 global variable. this is tricky, because we
6467 must play with the SHN_COMMON type of the symbol */
6468 /* XXX: should check if the variable was already
6469 initialized. It is incorrect to initialized it
6471 /* no init data, we won't add more to the symbol */
6478 /* allocate symbol in corresponding section */
6483 else if (tcc_state
->nocommon
)
6487 data_offset
= sec
->data_offset
;
6488 data_offset
= (data_offset
+ align
- 1) & -align
;
6490 /* very important to increment global pointer at this time
6491 because initializers themselves can create new initializers */
6492 data_offset
+= size
;
6493 #ifdef CONFIG_TCC_BCHECK
6494 /* add padding if bound check */
6495 if (tcc_state
->do_bounds_check
)
6498 sec
->data_offset
= data_offset
;
6499 /* allocate section space to put the data */
6500 if (sec
->sh_type
!= SHT_NOBITS
&&
6501 data_offset
> sec
->data_allocated
)
6502 section_realloc(sec
, data_offset
);
6503 /* align section if needed */
6504 if (align
> sec
->sh_addralign
)
6505 sec
->sh_addralign
= align
;
6507 addr
= 0; /* avoid warning */
6511 if (scope
!= VT_CONST
|| !sym
) {
6512 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
6513 sym
->asm_label
= ad
->asm_label
;
6515 /* update symbol definition */
6517 put_extern_sym(sym
, sec
, addr
, size
);
6520 /* put a common area */
6521 put_extern_sym(sym
, NULL
, align
, size
);
6522 /* XXX: find a nicer way */
6523 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
6524 esym
->st_shndx
= SHN_COMMON
;
6527 /* push global reference */
6528 sym
= get_sym_ref(type
, sec
, addr
, size
);
6529 vpushsym(type
, sym
);
6531 /* patch symbol weakness */
6532 if (type
->t
& VT_WEAK
)
6534 apply_visibility(sym
, type
);
6535 #ifdef CONFIG_TCC_BCHECK
6536 /* handles bounds now because the symbol must be defined
6537 before for the relocation */
6538 if (tcc_state
->do_bounds_check
) {
6541 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
6542 /* then add global bound info */
6543 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(addr_t
));
6544 bounds_ptr
[0] = 0; /* relocated */
6545 bounds_ptr
[1] = size
;
6549 if (type
->t
& VT_VLA
) {
6552 /* save current stack pointer */
6553 if (vlas_in_scope
== 0) {
6554 if (vla_sp_root_loc
== -1)
6555 vla_sp_root_loc
= (loc
-= PTR_SIZE
);
6556 gen_vla_sp_save(vla_sp_root_loc
);
6559 vla_runtime_type_size(type
, &a
);
6560 gen_vla_alloc(type
, a
);
6561 gen_vla_sp_save(addr
);
6564 } else if (has_init
) {
6565 decl_initializer(type
, sec
, addr
, 1, 0);
6566 /* patch flexible array member size back to -1, */
6567 /* for possible subsequent similar declarations */
6569 flexible_array
->type
.ref
->c
= -1;
6572 /* restore parse state if needed */
6575 restore_parse_state(&saved_parse_state
);
6579 static void put_func_debug(Sym
*sym
)
6584 /* XXX: we put here a dummy type */
6585 snprintf(buf
, sizeof(buf
), "%s:%c1",
6586 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
6587 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
6588 cur_text_section
, sym
->c
);
6589 /* //gr gdb wants a line at the function */
6590 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
6595 /* parse an old style function declaration list */
6596 /* XXX: check multiple parameter */
6597 static void func_decl_list(Sym
*func_sym
)
6604 /* parse each declaration */
6605 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
&&
6606 tok
!= TOK_ASM1
&& tok
!= TOK_ASM2
&& tok
!= TOK_ASM3
) {
6607 if (!parse_btype(&btype
, &ad
))
6608 expect("declaration list");
6609 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
6610 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
6612 /* we accept no variable after */
6616 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
6617 /* find parameter in function parameter list */
6620 if ((s
->v
& ~SYM_FIELD
) == v
)
6624 tcc_error("declaration for parameter '%s' but no such parameter",
6625 get_tok_str(v
, NULL
));
6627 /* check that no storage specifier except 'register' was given */
6628 if (type
.t
& VT_STORAGE
)
6629 tcc_error("storage class specified for '%s'", get_tok_str(v
, NULL
));
6630 convert_parameter_type(&type
);
6631 /* we can add the type (NOTE: it could be local to the function) */
6633 /* accept other parameters */
6644 /* parse a function defined by symbol 'sym' and generate its code in
6645 'cur_text_section' */
6646 static void gen_function(Sym
*sym
)
6648 int saved_nocode_wanted
= nocode_wanted
;
6651 ind
= cur_text_section
->data_offset
;
6652 /* NOTE: we patch the symbol size later */
6653 put_extern_sym(sym
, cur_text_section
, ind
, 0);
6654 funcname
= get_tok_str(sym
->v
, NULL
);
6656 /* Initialize VLA state */
6658 vla_sp_root_loc
= -1;
6659 /* put debug symbol */
6660 if (tcc_state
->do_debug
)
6661 put_func_debug(sym
);
6663 /* push a dummy symbol to enable local sym storage */
6664 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
6665 local_scope
= 1; /* for function parameters */
6666 gfunc_prolog(&sym
->type
);
6670 block(NULL
, NULL
, 0);
6673 cur_text_section
->data_offset
= ind
;
6674 label_pop(&global_label_stack
, NULL
);
6675 /* reset local stack */
6677 sym_pop(&local_stack
, NULL
, 0);
6678 /* end of function */
6679 /* patch symbol size */
6680 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
6682 /* patch symbol weakness (this definition overrules any prototype) */
6683 if (sym
->type
.t
& VT_WEAK
)
6685 apply_visibility(sym
, &sym
->type
);
6686 if (tcc_state
->do_debug
) {
6687 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
6689 /* It's better to crash than to generate wrong code */
6690 cur_text_section
= NULL
;
6691 funcname
= ""; /* for safety */
6692 func_vt
.t
= VT_VOID
; /* for safety */
6693 func_var
= 0; /* for safety */
6694 ind
= 0; /* for safety */
6695 nocode_wanted
= saved_nocode_wanted
;
6699 static void gen_inline_functions(TCCState
*s
)
6702 int inline_generated
, i
, ln
;
6703 struct InlineFunc
*fn
;
6705 ln
= file
->line_num
;
6706 /* iterate while inline function are referenced */
6708 inline_generated
= 0;
6709 for (i
= 0; i
< s
->nb_inline_fns
; ++i
) {
6710 fn
= s
->inline_fns
[i
];
6712 if (sym
&& sym
->c
) {
6713 /* the function was used: generate its code and
6714 convert it to a normal function */
6717 pstrcpy(file
->filename
, sizeof file
->filename
, fn
->filename
);
6718 sym
->r
= VT_SYM
| VT_CONST
;
6719 sym
->type
.t
&= ~VT_INLINE
;
6721 begin_macro(fn
->func_str
, 1);
6723 cur_text_section
= text_section
;
6727 inline_generated
= 1;
6730 if (!inline_generated
)
6733 file
->line_num
= ln
;
6736 ST_FUNC
void free_inline_functions(TCCState
*s
)
6739 /* free tokens of unused inline functions */
6740 for (i
= 0; i
< s
->nb_inline_fns
; ++i
) {
6741 struct InlineFunc
*fn
= s
->inline_fns
[i
];
6743 tok_str_free(fn
->func_str
);
6745 dynarray_reset(&s
->inline_fns
, &s
->nb_inline_fns
);
6748 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
6749 static int decl0(int l
, int is_for_loop_init
)
6757 if (!parse_btype(&btype
, &ad
)) {
6758 if (is_for_loop_init
)
6760 /* skip redundant ';' */
6761 /* XXX: find more elegant solution */
6766 if (l
== VT_CONST
&&
6767 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
6768 /* global asm block */
6772 /* special test for old K&R protos without explicit int
6773 type. Only accepted when defining global data */
6774 if (l
== VT_LOCAL
|| tok
< TOK_UIDENT
)
6778 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
6779 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
6781 if ((btype
.t
& VT_BTYPE
) == VT_STRUCT
) {
6782 int v
= btype
.ref
->v
;
6783 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) >= SYM_FIRST_ANOM
)
6784 tcc_warning("unnamed struct/union that defines no instances");
6789 while (1) { /* iterate thru each declaration */
6791 /* If the base type itself was an array type of unspecified
6792 size (like in 'typedef int arr[]; arr x = {1};') then
6793 we will overwrite the unknown size by the real one for
6794 this decl. We need to unshare the ref symbol holding
6796 if ((type
.t
& VT_ARRAY
) && type
.ref
->c
< 0) {
6797 type
.ref
= sym_push(SYM_FIELD
, &type
.ref
->type
, 0, type
.ref
->c
);
6799 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
6803 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
6804 printf("type = '%s'\n", buf
);
6807 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
6808 if ((type
.t
& VT_STATIC
) && (l
== VT_LOCAL
)) {
6809 tcc_error("function without file scope cannot be static");
6811 /* if old style function prototype, we accept a
6814 if (sym
->c
== FUNC_OLD
)
6815 func_decl_list(sym
);
6818 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
6819 ad
.asm_label
= asm_label_instr();
6820 /* parse one last attribute list, after asm label */
6821 parse_attribute(&ad
);
6828 #ifdef TCC_TARGET_PE
6829 if (ad
.a
.func_import
)
6830 type
.t
|= VT_IMPORT
;
6831 if (ad
.a
.func_export
)
6832 type
.t
|= VT_EXPORT
;
6834 type
.t
|= ad
.a
.visibility
<< VT_VIS_SHIFT
;
6838 tcc_error("cannot use local functions");
6839 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
6840 expect("function definition");
6842 /* reject abstract declarators in function definition */
6844 while ((sym
= sym
->next
) != NULL
)
6845 if (!(sym
->v
& ~SYM_FIELD
))
6846 expect("identifier");
6848 /* XXX: cannot do better now: convert extern line to static inline */
6849 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
6850 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
6855 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
6858 ref
= sym
->type
.ref
;
6859 if (0 == ref
->a
.func_proto
)
6860 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
6862 /* use func_call from prototype if not defined */
6863 if (ref
->a
.func_call
!= FUNC_CDECL
6864 && type
.ref
->a
.func_call
== FUNC_CDECL
)
6865 type
.ref
->a
.func_call
= ref
->a
.func_call
;
6867 /* use export from prototype */
6868 if (ref
->a
.func_export
)
6869 type
.ref
->a
.func_export
= 1;
6871 /* use static from prototype */
6872 if (sym
->type
.t
& VT_STATIC
)
6873 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
6875 /* If the definition has no visibility use the
6876 one from prototype. */
6877 if (! (type
.t
& VT_VIS_MASK
))
6878 type
.t
|= sym
->type
.t
& VT_VIS_MASK
;
6880 if (!is_compatible_types(&sym
->type
, &type
)) {
6882 tcc_error("incompatible types for redefinition of '%s'",
6883 get_tok_str(v
, NULL
));
6885 type
.ref
->a
.func_proto
= 0;
6886 /* if symbol is already defined, then put complete type */
6889 /* put function symbol */
6890 sym
= global_identifier_push(v
, type
.t
, 0);
6891 sym
->type
.ref
= type
.ref
;
6894 /* static inline functions are just recorded as a kind
6895 of macro. Their code will be emitted at the end of
6896 the compilation unit only if they are used */
6897 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
6898 (VT_INLINE
| VT_STATIC
)) {
6900 struct InlineFunc
*fn
;
6901 const char *filename
;
6903 filename
= file
? file
->filename
: "";
6904 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
6905 strcpy(fn
->filename
, filename
);
6907 fn
->func_str
= tok_str_alloc();
6913 tcc_error("unexpected end of file");
6914 tok_str_add_tok(fn
->func_str
);
6919 } else if (t
== '}') {
6921 if (block_level
== 0)
6925 tok_str_add(fn
->func_str
, -1);
6926 tok_str_add(fn
->func_str
, 0);
6927 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
6930 /* compute text section */
6931 cur_text_section
= ad
.section
;
6932 if (!cur_text_section
)
6933 cur_text_section
= text_section
;
6934 sym
->r
= VT_SYM
| VT_CONST
;
6939 if (btype
.t
& VT_TYPEDEF
) {
6940 /* save typedefed type */
6941 /* XXX: test storage specifiers ? */
6943 if (sym
&& sym
->scope
== local_scope
) {
6944 if (!is_compatible_types(&sym
->type
, &type
)
6945 || !(sym
->type
.t
& VT_TYPEDEF
))
6946 tcc_error("incompatible redefinition of '%s'",
6947 get_tok_str(v
, NULL
));
6950 sym
= sym_push(v
, &type
, 0, 0);
6953 sym
->type
.t
|= VT_TYPEDEF
;
6956 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
6957 /* external function definition */
6958 /* specific case for func_call attribute */
6959 ad
.a
.func_proto
= 1;
6961 } else if (!(type
.t
& VT_ARRAY
)) {
6962 /* not lvalue if array */
6963 r
|= lvalue_type(type
.t
);
6965 has_init
= (tok
== '=');
6966 if (has_init
&& (type
.t
& VT_VLA
))
6967 tcc_error("variable length array cannot be initialized");
6968 if ((btype
.t
& VT_EXTERN
) || ((type
.t
& VT_BTYPE
) == VT_FUNC
) ||
6969 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
6970 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
6971 /* external variable or function */
6972 /* NOTE: as GCC, uninitialized global static
6973 arrays of null size are considered as
6975 sym
= external_sym(v
, &type
, r
);
6976 sym
->asm_label
= ad
.asm_label
;
6978 if (ad
.alias_target
) {
6983 alias_target
= sym_find(ad
.alias_target
);
6984 if (!alias_target
|| !alias_target
->c
)
6985 tcc_error("unsupported forward __alias__ attribute");
6986 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[alias_target
->c
];
6987 tsec
.sh_num
= esym
->st_shndx
;
6988 put_extern_sym2(sym
, &tsec
, esym
->st_value
, esym
->st_size
, 0);
6991 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
6992 if (type
.t
& VT_STATIC
)
6998 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, l
);
7002 if (is_for_loop_init
)
7015 ST_FUNC
void decl(int l
)
7020 /* ------------------------------------------------------------------------- */