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
)
583 /* push constant of type "type" with useless value */
584 ST_FUNC
void vpush(CType
*type
)
587 vsetc(type
, VT_CONST
, &cval
);
590 /* push integer constant */
591 ST_FUNC
void vpushi(int v
)
595 vsetc(&int_type
, VT_CONST
, &cval
);
598 /* push a pointer sized constant */
599 static void vpushs(addr_t v
)
603 vsetc(&size_type
, VT_CONST
, &cval
);
606 /* push arbitrary 64bit constant */
607 ST_FUNC
void vpush64(int ty
, unsigned long long v
)
614 vsetc(&ctype
, VT_CONST
, &cval
);
617 /* push long long constant */
618 static inline void vpushll(long long v
)
620 vpush64(VT_LLONG
, v
);
623 /* push a symbol value of TYPE */
624 static inline void vpushsym(CType
*type
, Sym
*sym
)
628 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
632 /* Return a static symbol pointing to a section */
633 ST_FUNC Sym
*get_sym_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
639 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
640 sym
->type
.ref
= type
->ref
;
641 sym
->r
= VT_CONST
| VT_SYM
;
642 put_extern_sym(sym
, sec
, offset
, size
);
646 /* push a reference to a section offset by adding a dummy symbol */
647 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
649 vpushsym(type
, get_sym_ref(type
, sec
, offset
, size
));
652 /* define a new external reference to a symbol 'v' of type 'u' */
653 ST_FUNC Sym
*external_global_sym(int v
, CType
*type
, int r
)
659 /* push forward reference */
660 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
661 s
->type
.ref
= type
->ref
;
662 s
->r
= r
| VT_CONST
| VT_SYM
;
667 /* define a new external reference to a symbol 'v' */
668 static Sym
*external_sym(int v
, CType
*type
, int r
)
674 /* push forward reference */
675 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
676 s
->type
.t
|= VT_EXTERN
;
677 } else if (s
->type
.ref
== func_old_type
.ref
) {
678 s
->type
.ref
= type
->ref
;
679 s
->r
= r
| VT_CONST
| VT_SYM
;
680 s
->type
.t
|= VT_EXTERN
;
681 } else if (!is_compatible_types(&s
->type
, type
)) {
682 tcc_error("incompatible types for redefinition of '%s'",
683 get_tok_str(v
, NULL
));
685 /* Merge some storage attributes. */
686 if (type
->t
& VT_WEAK
)
689 if (type
->t
& VT_VIS_MASK
)
690 apply_visibility(s
, type
);
695 /* push a reference to global symbol v */
696 ST_FUNC
void vpush_global_sym(CType
*type
, int v
)
698 vpushsym(type
, external_global_sym(v
, type
, 0));
701 ST_FUNC
void vset(CType
*type
, int r
, int v
)
706 vsetc(type
, r
, &cval
);
709 static void vseti(int r
, int v
)
717 ST_FUNC
void vswap(void)
720 /* cannot let cpu flags if other instruction are generated. Also
721 avoid leaving VT_JMP anywhere except on the top of the stack
722 because it would complicate the code generator. */
723 if (vtop
>= vstack
) {
724 int v
= vtop
->r
& VT_VALMASK
;
725 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
732 /* XXX: +2% overall speed possible with optimized memswap
734 * memswap(&vtop[0], &vtop[1], sizeof *vtop);
738 ST_FUNC
void vpushv(SValue
*v
)
740 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
741 tcc_error("memory full (vstack)");
746 static void vdup(void)
751 /* save registers up to (vtop - n) stack entry */
752 ST_FUNC
void save_regs(int n
)
755 for(p
= vstack
, p1
= vtop
- n
; p
<= p1
; p
++)
759 /* save r to the memory stack, and mark it as being free */
760 ST_FUNC
void save_reg(int r
)
762 save_reg_upstack(r
, 0);
765 /* save r to the memory stack, and mark it as being free,
766 if seen up to (vtop - n) stack entry */
767 ST_FUNC
void save_reg_upstack(int r
, int n
)
769 int l
, saved
, size
, align
;
773 if ((r
&= VT_VALMASK
) >= VT_CONST
)
776 /* modify all stack values */
779 for(p
= vstack
, p1
= vtop
- n
; p
<= p1
; p
++) {
780 if ((p
->r
& VT_VALMASK
) == r
||
781 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
782 /* must save value on stack if not already done */
784 /* NOTE: must reload 'r' because r might be equal to r2 */
785 r
= p
->r
& VT_VALMASK
;
786 /* store register in the stack */
788 if ((p
->r
& VT_LVAL
) ||
789 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
790 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
791 type
= &char_pointer_type
;
795 size
= type_size(type
, &align
);
796 loc
= (loc
- size
) & -align
;
798 sv
.r
= VT_LOCAL
| VT_LVAL
;
801 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
802 /* x86 specific: need to pop fp register ST0 if saved */
804 o(0xd8dd); /* fstp %st(0) */
807 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
808 /* special long long case */
809 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
817 /* mark that stack entry as being saved on the stack */
818 if (p
->r
& VT_LVAL
) {
819 /* also clear the bounded flag because the
820 relocation address of the function was stored in
822 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
824 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
832 #ifdef TCC_TARGET_ARM
833 /* find a register of class 'rc2' with at most one reference on stack.
834 * If none, call get_reg(rc) */
835 ST_FUNC
int get_reg_ex(int rc
, int rc2
)
840 for(r
=0;r
<NB_REGS
;r
++) {
841 if (reg_classes
[r
] & rc2
) {
844 for(p
= vstack
; p
<= vtop
; p
++) {
845 if ((p
->r
& VT_VALMASK
) == r
||
846 (p
->r2
& VT_VALMASK
) == r
)
857 /* find a free register of class 'rc'. If none, save one register */
858 ST_FUNC
int get_reg(int rc
)
863 /* find a free register */
864 for(r
=0;r
<NB_REGS
;r
++) {
865 if (reg_classes
[r
] & rc
) {
866 for(p
=vstack
;p
<=vtop
;p
++) {
867 if ((p
->r
& VT_VALMASK
) == r
||
868 (p
->r2
& VT_VALMASK
) == r
)
876 /* no register left : free the first one on the stack (VERY
877 IMPORTANT to start from the bottom to ensure that we don't
878 spill registers used in gen_opi()) */
879 for(p
=vstack
;p
<=vtop
;p
++) {
880 /* look at second register (if long long) */
881 r
= p
->r2
& VT_VALMASK
;
882 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
884 r
= p
->r
& VT_VALMASK
;
885 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
891 /* Should never comes here */
895 /* move register 's' (of type 't') to 'r', and flush previous value of r to memory
897 static void move_reg(int r
, int s
, int t
)
911 /* get address of vtop (vtop MUST BE an lvalue) */
912 ST_FUNC
void gaddrof(void)
914 if (vtop
->r
& VT_REF
&& !nocode_wanted
)
917 /* tricky: if saved lvalue, then we can go back to lvalue */
918 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
919 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
924 #ifdef CONFIG_TCC_BCHECK
925 /* generate lvalue bound code */
926 static void gbound(void)
931 vtop
->r
&= ~VT_MUSTBOUND
;
932 /* if lvalue, then use checking code before dereferencing */
933 if (vtop
->r
& VT_LVAL
) {
934 /* if not VT_BOUNDED value, then make one */
935 if (!(vtop
->r
& VT_BOUNDED
)) {
936 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
937 /* must save type because we must set it to int to get pointer */
939 vtop
->type
.t
= VT_PTR
;
942 gen_bounded_ptr_add();
943 vtop
->r
|= lval_type
;
946 /* then check for dereferencing */
947 gen_bounded_ptr_deref();
952 /* store vtop a register belonging to class 'rc'. lvalues are
953 converted to values. Cannot be used if cannot be converted to
954 register value (such as structures). */
955 ST_FUNC
int gv(int rc
)
957 int r
, bit_pos
, bit_size
, size
, align
, i
;
960 /* NOTE: get_reg can modify vstack[] */
961 if (vtop
->type
.t
& VT_BITFIELD
) {
964 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
965 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
966 /* remove bit field info to avoid loops */
967 vtop
->type
.t
&= ~VT_BITFIELD
& ((1 << VT_STRUCT_SHIFT
) - 1);
968 /* cast to int to propagate signedness in following ops */
969 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
974 if((vtop
->type
.t
& VT_UNSIGNED
) ||
975 (vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
976 type
.t
|= VT_UNSIGNED
;
978 /* generate shifts */
979 vpushi(bits
- (bit_pos
+ bit_size
));
981 vpushi(bits
- bit_size
);
982 /* NOTE: transformed to SHR if unsigned */
986 if (is_float(vtop
->type
.t
) &&
987 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
990 unsigned long offset
;
991 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
995 /* XXX: unify with initializers handling ? */
996 /* CPUs usually cannot use float constants, so we store them
997 generically in data segment */
998 size
= type_size(&vtop
->type
, &align
);
999 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
1000 data_section
->data_offset
= offset
;
1001 /* XXX: not portable yet */
1002 #if defined(__i386__) || defined(__x86_64__)
1003 /* Zero pad x87 tenbyte long doubles */
1004 if (size
== LDOUBLE_SIZE
) {
1005 vtop
->c
.tab
[2] &= 0xffff;
1006 #if LDOUBLE_SIZE == 16
1011 ptr
= section_ptr_add(data_section
, size
);
1013 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
1017 ptr
[i
] = vtop
->c
.tab
[size
-1-i
];
1021 ptr
[i
] = vtop
->c
.tab
[i
];
1022 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
1023 vtop
->r
|= VT_LVAL
| VT_SYM
;
1027 #ifdef CONFIG_TCC_BCHECK
1028 if (vtop
->r
& VT_MUSTBOUND
)
1032 r
= vtop
->r
& VT_VALMASK
;
1033 rc2
= (rc
& RC_FLOAT
) ? RC_FLOAT
: RC_INT
;
1034 #ifndef TCC_TARGET_ARM64
1037 #ifdef TCC_TARGET_X86_64
1038 else if (rc
== RC_FRET
)
1043 /* need to reload if:
1045 - lvalue (need to dereference pointer)
1046 - already a register, but not in the right class */
1048 || (vtop
->r
& VT_LVAL
)
1049 || !(reg_classes
[r
] & rc
)
1050 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1051 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
1052 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
&& !(reg_classes
[vtop
->r2
] & rc2
))
1054 || ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
1059 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1060 if (((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
)) {
1061 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
1063 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
1064 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
1065 unsigned long long ll
;
1067 int r2
, original_type
;
1068 original_type
= vtop
->type
.t
;
1069 /* two register type load : expand to two words
1071 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
1072 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
1075 vtop
->c
.i
= ll
; /* first word */
1077 vtop
->r
= r
; /* save register value */
1078 vpushi(ll
>> 32); /* second word */
1081 if (vtop
->r
& VT_LVAL
) {
1082 /* We do not want to modifier the long long
1083 pointer here, so the safest (and less
1084 efficient) is to save all the other registers
1085 in the stack. XXX: totally inefficient. */
1089 /* lvalue_save: save only if used further down the stack */
1090 save_reg_upstack(vtop
->r
, 1);
1092 /* load from memory */
1093 vtop
->type
.t
= load_type
;
1096 vtop
[-1].r
= r
; /* save register value */
1097 /* increment pointer to get second word */
1098 vtop
->type
.t
= addr_type
;
1103 vtop
->type
.t
= load_type
;
1105 /* move registers */
1108 vtop
[-1].r
= r
; /* save register value */
1109 vtop
->r
= vtop
[-1].r2
;
1111 /* Allocate second register. Here we rely on the fact that
1112 get_reg() tries first to free r2 of an SValue. */
1116 /* write second register */
1118 vtop
->type
.t
= original_type
;
1119 } else if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
1121 /* lvalue of scalar type : need to use lvalue type
1122 because of possible cast */
1125 /* compute memory access type */
1126 if (vtop
->r
& VT_REF
)
1127 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1132 else if (vtop
->r
& VT_LVAL_BYTE
)
1134 else if (vtop
->r
& VT_LVAL_SHORT
)
1136 if (vtop
->r
& VT_LVAL_UNSIGNED
)
1140 /* restore wanted type */
1143 /* one register type load */
1148 #ifdef TCC_TARGET_C67
1149 /* uses register pairs for doubles */
1150 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
1157 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
1158 ST_FUNC
void gv2(int rc1
, int rc2
)
1162 /* generate more generic register first. But VT_JMP or VT_CMP
1163 values must be generated first in all cases to avoid possible
1165 v
= vtop
[0].r
& VT_VALMASK
;
1166 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
1171 /* test if reload is needed for first register */
1172 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
1182 /* test if reload is needed for first register */
1183 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
1189 #ifndef TCC_TARGET_ARM64
1190 /* wrapper around RC_FRET to return a register by type */
1191 static int rc_fret(int t
)
1193 #ifdef TCC_TARGET_X86_64
1194 if (t
== VT_LDOUBLE
) {
1202 /* wrapper around REG_FRET to return a register by type */
1203 static int reg_fret(int t
)
1205 #ifdef TCC_TARGET_X86_64
1206 if (t
== VT_LDOUBLE
) {
1213 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
1214 /* expand 64bit on stack in two ints */
1215 static void lexpand(void)
1218 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
1219 v
= vtop
->r
& (VT_VALMASK
| VT_LVAL
);
1220 if (v
== VT_CONST
) {
1223 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
1229 vtop
[0].r
= vtop
[-1].r2
;
1230 vtop
[0].r2
= vtop
[-1].r2
= VT_CONST
;
1232 vtop
[0].type
.t
= vtop
[-1].type
.t
= VT_INT
| u
;
1236 #ifdef TCC_TARGET_ARM
1237 /* expand long long on stack */
1238 ST_FUNC
void lexpand_nr(void)
1242 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
1244 vtop
->r2
= VT_CONST
;
1245 vtop
->type
.t
= VT_INT
| u
;
1246 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
1247 if (v
== VT_CONST
) {
1248 vtop
[-1].c
.i
= vtop
->c
.i
;
1249 vtop
->c
.i
= vtop
->c
.i
>> 32;
1251 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
1253 vtop
->r
= vtop
[-1].r
;
1254 } else if (v
> VT_CONST
) {
1258 vtop
->r
= vtop
[-1].r2
;
1259 vtop
[-1].r2
= VT_CONST
;
1260 vtop
[-1].type
.t
= VT_INT
| u
;
1264 #if !defined(TCC_TARGET_X86_64) && !defined(TCC_TARGET_ARM64)
1265 /* build a long long from two ints */
1266 static void lbuild(int t
)
1268 gv2(RC_INT
, RC_INT
);
1269 vtop
[-1].r2
= vtop
[0].r
;
1270 vtop
[-1].type
.t
= t
;
1275 /* rotate n first stack elements to the bottom
1276 I1 ... In -> I2 ... In I1 [top is right]
1278 ST_FUNC
void vrotb(int n
)
1284 for(i
=-n
+1;i
!=0;i
++)
1285 vtop
[i
] = vtop
[i
+1];
1289 /* rotate the n elements before entry e towards the top
1290 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
1292 ST_FUNC
void vrote(SValue
*e
, int n
)
1298 for(i
= 0;i
< n
- 1; i
++)
1303 /* rotate n first stack elements to the top
1304 I1 ... In -> In I1 ... I(n-1) [top is right]
1306 ST_FUNC
void vrott(int n
)
1311 /* pop stack value */
1312 ST_FUNC
void vpop(void)
1315 v
= vtop
->r
& VT_VALMASK
;
1316 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1317 /* for x86, we need to pop the FP stack */
1318 if (v
== TREG_ST0
&& !nocode_wanted
) {
1319 o(0xd8dd); /* fstp %st(0) */
1322 if (v
== VT_JMP
|| v
== VT_JMPI
) {
1323 /* need to put correct jump if && or || without test */
1329 /* convert stack entry to register and duplicate its value in another
1331 static void gv_dup(void)
1337 #if !defined(TCC_TARGET_X86_64) && !defined(TCC_TARGET_ARM64)
1338 if ((t
& VT_BTYPE
) == VT_LLONG
) {
1345 /* stack: H L L1 H1 */
1355 /* duplicate value */
1360 #ifdef TCC_TARGET_X86_64
1361 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1371 load(r1
, &sv
); /* move r to r1 */
1373 /* duplicates value */
1379 /* Generate value test
1381 * Generate a test for any value (jump, comparison and integers) */
1382 ST_FUNC
int gvtst(int inv
, int t
)
1384 int v
= vtop
->r
& VT_VALMASK
;
1385 if (v
!= VT_CMP
&& v
!= VT_JMP
&& v
!= VT_JMPI
) {
1389 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1390 /* constant jmp optimization */
1391 if ((vtop
->c
.i
!= 0) != inv
)
1396 return gtst(inv
, t
);
1399 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
1400 /* generate CPU independent (unsigned) long long operations */
1401 static void gen_opl(int op
)
1403 int t
, a
, b
, op1
, c
, i
;
1405 unsigned short reg_iret
= REG_IRET
;
1406 unsigned short reg_lret
= REG_LRET
;
1412 func
= TOK___divdi3
;
1415 func
= TOK___udivdi3
;
1418 func
= TOK___moddi3
;
1421 func
= TOK___umoddi3
;
1428 /* call generic long long function */
1429 vpush_global_sym(&func_old_type
, func
);
1434 vtop
->r2
= reg_lret
;
1442 //pv("gen_opl A",0,2);
1448 /* stack: L1 H1 L2 H2 */
1453 vtop
[-2] = vtop
[-3];
1456 /* stack: H1 H2 L1 L2 */
1457 //pv("gen_opl B",0,4);
1463 /* stack: H1 H2 L1 L2 ML MH */
1466 /* stack: ML MH H1 H2 L1 L2 */
1470 /* stack: ML MH H1 L2 H2 L1 */
1475 /* stack: ML MH M1 M2 */
1478 } else if (op
== '+' || op
== '-') {
1479 /* XXX: add non carry method too (for MIPS or alpha) */
1485 /* stack: H1 H2 (L1 op L2) */
1488 gen_op(op1
+ 1); /* TOK_xxxC2 */
1491 /* stack: H1 H2 (L1 op L2) */
1494 /* stack: (L1 op L2) H1 H2 */
1496 /* stack: (L1 op L2) (H1 op H2) */
1504 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1505 t
= vtop
[-1].type
.t
;
1509 /* stack: L H shift */
1511 /* constant: simpler */
1512 /* NOTE: all comments are for SHL. the other cases are
1513 done by swaping words */
1524 if (op
!= TOK_SAR
) {
1557 /* XXX: should provide a faster fallback on x86 ? */
1560 func
= TOK___ashrdi3
;
1563 func
= TOK___lshrdi3
;
1566 func
= TOK___ashldi3
;
1572 /* compare operations */
1578 /* stack: L1 H1 L2 H2 */
1580 vtop
[-1] = vtop
[-2];
1582 /* stack: L1 L2 H1 H2 */
1585 /* when values are equal, we need to compare low words. since
1586 the jump is inverted, we invert the test too. */
1589 else if (op1
== TOK_GT
)
1591 else if (op1
== TOK_ULT
)
1593 else if (op1
== TOK_UGT
)
1598 if (op1
!= TOK_NE
) {
1602 /* generate non equal test */
1603 /* XXX: NOT PORTABLE yet */
1607 #if defined(TCC_TARGET_I386)
1608 b
= psym(0x850f, 0);
1609 #elif defined(TCC_TARGET_ARM)
1611 o(0x1A000000 | encbranch(ind
, 0, 1));
1612 #elif defined(TCC_TARGET_C67) || defined(TCC_TARGET_ARM64)
1613 tcc_error("not implemented");
1615 #error not supported
1619 /* compare low. Always unsigned */
1623 else if (op1
== TOK_LE
)
1625 else if (op1
== TOK_GT
)
1627 else if (op1
== TOK_GE
)
1638 static uint64_t gen_opic_sdiv(uint64_t a
, uint64_t b
)
1640 uint64_t x
= (a
>> 63 ? -a
: a
) / (b
>> 63 ? -b
: b
);
1641 return (a
^ b
) >> 63 ? -x
: x
;
1644 static int gen_opic_lt(uint64_t a
, uint64_t b
)
1646 return (a
^ (uint64_t)1 << 63) < (b
^ (uint64_t)1 << 63);
1649 /* handle integer constant optimizations and various machine
1651 static void gen_opic(int op
)
1653 SValue
*v1
= vtop
- 1;
1655 int t1
= v1
->type
.t
& VT_BTYPE
;
1656 int t2
= v2
->type
.t
& VT_BTYPE
;
1657 int c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1658 int c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1659 uint64_t l1
= c1
? v1
->c
.i
: 0;
1660 uint64_t l2
= c2
? v2
->c
.i
: 0;
1661 int shm
= (t1
== VT_LLONG
) ? 63 : 31;
1663 if (t1
!= VT_LLONG
&& (PTR_SIZE
!= 8 || t1
!= VT_PTR
))
1664 l1
= ((uint32_t)l1
|
1665 (v1
->type
.t
& VT_UNSIGNED
? 0 : -(l1
& 0x80000000)));
1666 if (t2
!= VT_LLONG
&& (PTR_SIZE
!= 8 || t2
!= VT_PTR
))
1667 l2
= ((uint32_t)l2
|
1668 (v2
->type
.t
& VT_UNSIGNED
? 0 : -(l2
& 0x80000000)));
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;
1677 case '*': l1
*= l2
; break;
1684 /* if division by zero, generate explicit division */
1687 tcc_error("division by zero in constant");
1691 default: l1
= gen_opic_sdiv(l1
, l2
); break;
1692 case '%': l1
= l1
- l2
* gen_opic_sdiv(l1
, l2
); break;
1693 case TOK_UDIV
: l1
= l1
/ l2
; break;
1694 case TOK_UMOD
: l1
= l1
% l2
; break;
1697 case TOK_SHL
: l1
<<= (l2
& shm
); break;
1698 case TOK_SHR
: l1
>>= (l2
& shm
); break;
1700 l1
= (l1
>> 63) ? ~(~l1
>> (l2
& shm
)) : l1
>> (l2
& shm
);
1703 case TOK_ULT
: l1
= l1
< l2
; break;
1704 case TOK_UGE
: l1
= l1
>= l2
; break;
1705 case TOK_EQ
: l1
= l1
== l2
; break;
1706 case TOK_NE
: l1
= l1
!= l2
; break;
1707 case TOK_ULE
: l1
= l1
<= l2
; break;
1708 case TOK_UGT
: l1
= l1
> l2
; break;
1709 case TOK_LT
: l1
= gen_opic_lt(l1
, l2
); break;
1710 case TOK_GE
: l1
= !gen_opic_lt(l1
, l2
); break;
1711 case TOK_LE
: l1
= !gen_opic_lt(l2
, l1
); break;
1712 case TOK_GT
: l1
= gen_opic_lt(l2
, l1
); break;
1714 case TOK_LAND
: l1
= l1
&& l2
; break;
1715 case TOK_LOR
: l1
= l1
|| l2
; break;
1722 /* if commutative ops, put c2 as constant */
1723 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1724 op
== '|' || op
== '*')) {
1726 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1727 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1729 if (!const_wanted
&&
1731 (op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
)) ||
1732 (l1
== -1 && op
== TOK_SAR
))) {
1733 /* treat (0 << x), (0 >> x) and (-1 >> x) as constant */
1735 } else if (!const_wanted
&&
1736 c2
&& ((l2
== 0 && (op
== '&' || op
== '*')) ||
1737 (l2
== -1 && op
== '|') ||
1738 (l2
== 0xffffffff && t2
!= VT_LLONG
&& op
== '|') ||
1739 (l2
== 1 && (op
== '%' || op
== TOK_UMOD
)))) {
1740 /* treat (x & 0), (x * 0), (x | -1) and (x % 1) as constant */
1745 } else if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1748 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1749 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1753 /* filter out NOP operations like x*1, x-0, x&-1... */
1755 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1756 /* try to use shifts instead of muls or divs */
1757 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1766 else if (op
== TOK_PDIV
)
1772 } else if (c2
&& (op
== '+' || op
== '-') &&
1773 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
1774 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1775 /* symbol + constant case */
1782 if (!nocode_wanted
) {
1783 /* call low level op generator */
1784 if (t1
== VT_LLONG
|| t2
== VT_LLONG
||
1785 (PTR_SIZE
== 8 && (t1
== VT_PTR
|| t2
== VT_PTR
)))
1791 /* Ensure vtop isn't marked VT_CONST in case something
1792 up our callchain is interested in const-ness of the
1793 expression. Also make it a non-LVAL if it was,
1794 so that further code can't accidentally generate
1795 a deref (happen only for buggy uses of e.g.
1796 gv() under nocode_wanted). */
1797 vtop
->r
&= ~(VT_VALMASK
| VT_LVAL
);
1803 /* generate a floating point operation with constant propagation */
1804 static void gen_opif(int op
)
1812 /* currently, we cannot do computations with forward symbols */
1813 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1814 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1816 if (v1
->type
.t
== VT_FLOAT
) {
1819 } else if (v1
->type
.t
== VT_DOUBLE
) {
1827 /* NOTE: we only do constant propagation if finite number (not
1828 NaN or infinity) (ANSI spec) */
1829 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1833 case '+': f1
+= f2
; break;
1834 case '-': f1
-= f2
; break;
1835 case '*': f1
*= f2
; break;
1839 tcc_error("division by zero in constant");
1844 /* XXX: also handles tests ? */
1848 /* XXX: overflow test ? */
1849 if (v1
->type
.t
== VT_FLOAT
) {
1851 } else if (v1
->type
.t
== VT_DOUBLE
) {
1859 if (!nocode_wanted
) {
1867 static int pointed_size(CType
*type
)
1870 return type_size(pointed_type(type
), &align
);
1873 static void vla_runtime_pointed_size(CType
*type
)
1876 vla_runtime_type_size(pointed_type(type
), &align
);
1879 static inline int is_null_pointer(SValue
*p
)
1881 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1883 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& (uint32_t)p
->c
.i
== 0) ||
1884 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.i
== 0) ||
1885 ((p
->type
.t
& VT_BTYPE
) == VT_PTR
&&
1886 (PTR_SIZE
== 4 ? (uint32_t)p
->c
.i
== 0 : p
->c
.i
== 0));
1889 static inline int is_integer_btype(int bt
)
1891 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
1892 bt
== VT_INT
|| bt
== VT_LLONG
);
1895 /* check types for comparison or subtraction of pointers */
1896 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
1898 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
1901 /* null pointers are accepted for all comparisons as gcc */
1902 if (is_null_pointer(p1
) || is_null_pointer(p2
))
1906 bt1
= type1
->t
& VT_BTYPE
;
1907 bt2
= type2
->t
& VT_BTYPE
;
1908 /* accept comparison between pointer and integer with a warning */
1909 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
1910 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
1911 tcc_warning("comparison between pointer and integer");
1915 /* both must be pointers or implicit function pointers */
1916 if (bt1
== VT_PTR
) {
1917 type1
= pointed_type(type1
);
1918 } else if (bt1
!= VT_FUNC
)
1919 goto invalid_operands
;
1921 if (bt2
== VT_PTR
) {
1922 type2
= pointed_type(type2
);
1923 } else if (bt2
!= VT_FUNC
) {
1925 tcc_error("invalid operands to binary %s", get_tok_str(op
, NULL
));
1927 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1928 (type2
->t
& VT_BTYPE
) == VT_VOID
)
1932 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1933 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1934 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1935 /* gcc-like error if '-' is used */
1937 goto invalid_operands
;
1939 tcc_warning("comparison of distinct pointer types lacks a cast");
1943 /* generic gen_op: handles types problems */
1944 ST_FUNC
void gen_op(int op
)
1946 int u
, t1
, t2
, bt1
, bt2
, t
;
1950 t1
= vtop
[-1].type
.t
;
1951 t2
= vtop
[0].type
.t
;
1952 bt1
= t1
& VT_BTYPE
;
1953 bt2
= t2
& VT_BTYPE
;
1955 if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
1956 tcc_error("operation on a struct");
1957 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
1958 if (bt2
== VT_FUNC
) {
1959 mk_pointer(&vtop
->type
);
1962 if (bt1
== VT_FUNC
) {
1964 mk_pointer(&vtop
->type
);
1969 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
1970 /* at least one operand is a pointer */
1971 /* relationnal op: must be both pointers */
1972 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
1973 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1974 /* pointers are handled are unsigned */
1975 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1976 t
= VT_LLONG
| VT_UNSIGNED
;
1978 t
= VT_INT
| VT_UNSIGNED
;
1982 /* if both pointers, then it must be the '-' op */
1983 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
1985 tcc_error("cannot use pointers here");
1986 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1987 /* XXX: check that types are compatible */
1988 if (vtop
[-1].type
.t
& VT_VLA
) {
1989 vla_runtime_pointed_size(&vtop
[-1].type
);
1991 vpushi(pointed_size(&vtop
[-1].type
));
1995 /* set to integer type */
1996 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1997 vtop
->type
.t
= VT_LLONG
;
1999 vtop
->type
.t
= VT_INT
;
2004 /* exactly one pointer : must be '+' or '-'. */
2005 if (op
!= '-' && op
!= '+')
2006 tcc_error("cannot use pointers here");
2007 /* Put pointer as first operand */
2008 if (bt2
== VT_PTR
) {
2013 if ((vtop
[0].type
.t
& VT_BTYPE
) == VT_LLONG
)
2014 /* XXX: truncate here because gen_opl can't handle ptr + long long */
2015 gen_cast(&int_type
);
2017 type1
= vtop
[-1].type
;
2018 type1
.t
&= ~VT_ARRAY
;
2019 if (vtop
[-1].type
.t
& VT_VLA
)
2020 vla_runtime_pointed_size(&vtop
[-1].type
);
2022 u
= pointed_size(&vtop
[-1].type
);
2024 tcc_error("unknown array element size");
2025 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2028 /* XXX: cast to int ? (long long case) */
2034 /* #ifdef CONFIG_TCC_BCHECK
2035 The main reason to removing this code:
2042 fprintf(stderr, "v+i-j = %p\n", v+i-j);
2043 fprintf(stderr, "v+(i-j) = %p\n", v+(i-j));
2045 When this code is on. then the output looks like
2047 v+(i-j) = 0xbff84000
2049 /* if evaluating constant expression, no code should be
2050 generated, so no bound check */
2051 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
2052 /* if bounded pointers, we generate a special code to
2059 gen_bounded_ptr_add();
2065 /* put again type if gen_opic() swaped operands */
2068 } else if (is_float(bt1
) || is_float(bt2
)) {
2069 /* compute bigger type and do implicit casts */
2070 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
2072 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
2077 /* floats can only be used for a few operations */
2078 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
2079 (op
< TOK_ULT
|| op
> TOK_GT
))
2080 tcc_error("invalid operands for binary operation");
2082 } else if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
) {
2083 t
= bt1
== VT_LLONG
? VT_LLONG
: VT_INT
;
2084 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (t
| VT_UNSIGNED
))
2087 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
2088 /* cast to biggest op */
2090 /* convert to unsigned if it does not fit in a long long */
2091 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
2092 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
2096 /* integer operations */
2098 /* convert to unsigned if it does not fit in an integer */
2099 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
2100 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
2103 /* XXX: currently, some unsigned operations are explicit, so
2104 we modify them here */
2105 if (t
& VT_UNSIGNED
) {
2112 else if (op
== TOK_LT
)
2114 else if (op
== TOK_GT
)
2116 else if (op
== TOK_LE
)
2118 else if (op
== TOK_GE
)
2125 /* special case for shifts and long long: we keep the shift as
2127 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
2134 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
2135 /* relationnal op: the result is an int */
2136 vtop
->type
.t
= VT_INT
;
2141 // Make sure that we have converted to an rvalue:
2142 if (vtop
->r
& VT_LVAL
&& !nocode_wanted
)
2143 gv(is_float(vtop
->type
.t
& VT_BTYPE
) ? RC_FLOAT
: RC_INT
);
2146 #ifndef TCC_TARGET_ARM
2147 /* generic itof for unsigned long long case */
2148 static void gen_cvt_itof1(int t
)
2150 #ifdef TCC_TARGET_ARM64
2153 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
2154 (VT_LLONG
| VT_UNSIGNED
)) {
2157 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
2158 #if LDOUBLE_SIZE != 8
2159 else if (t
== VT_LDOUBLE
)
2160 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
2163 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
2167 vtop
->r
= reg_fret(t
);
2175 /* generic ftoi for unsigned long long case */
2176 static void gen_cvt_ftoi1(int t
)
2178 #ifdef TCC_TARGET_ARM64
2183 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
2184 /* not handled natively */
2185 st
= vtop
->type
.t
& VT_BTYPE
;
2187 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
2188 #if LDOUBLE_SIZE != 8
2189 else if (st
== VT_LDOUBLE
)
2190 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
2193 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
2198 vtop
->r2
= REG_LRET
;
2205 /* force char or short cast */
2206 static void force_charshort_cast(int t
)
2210 /* XXX: add optimization if lvalue : just change type and offset */
2215 if (t
& VT_UNSIGNED
) {
2216 vpushi((1 << bits
) - 1);
2219 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
)
2225 /* result must be signed or the SAR is converted to an SHL
2226 This was not the case when "t" was a signed short
2227 and the last value on the stack was an unsigned int */
2228 vtop
->type
.t
&= ~VT_UNSIGNED
;
2234 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
2235 static void gen_cast(CType
*type
)
2237 int sbt
, dbt
, sf
, df
, c
, p
;
2239 /* special delayed cast for char/short */
2240 /* XXX: in some cases (multiple cascaded casts), it may still
2242 if (vtop
->r
& VT_MUSTCAST
) {
2243 vtop
->r
&= ~VT_MUSTCAST
;
2244 force_charshort_cast(vtop
->type
.t
);
2247 /* bitfields first get cast to ints */
2248 if (vtop
->type
.t
& VT_BITFIELD
&& !nocode_wanted
) {
2252 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
2253 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
2258 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
2259 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
2261 /* constant case: we can do it now */
2262 /* XXX: in ISOC, cannot do it if error in convert */
2263 if (sbt
== VT_FLOAT
)
2264 vtop
->c
.ld
= vtop
->c
.f
;
2265 else if (sbt
== VT_DOUBLE
)
2266 vtop
->c
.ld
= vtop
->c
.d
;
2269 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
2270 if ((sbt
& VT_UNSIGNED
) || !(vtop
->c
.i
>> 63))
2271 vtop
->c
.ld
= vtop
->c
.i
;
2273 vtop
->c
.ld
= -(long double)-vtop
->c
.i
;
2275 if ((sbt
& VT_UNSIGNED
) || !(vtop
->c
.i
>> 31))
2276 vtop
->c
.ld
= (uint32_t)vtop
->c
.i
;
2278 vtop
->c
.ld
= -(long double)-(uint32_t)vtop
->c
.i
;
2281 if (dbt
== VT_FLOAT
)
2282 vtop
->c
.f
= (float)vtop
->c
.ld
;
2283 else if (dbt
== VT_DOUBLE
)
2284 vtop
->c
.d
= (double)vtop
->c
.ld
;
2285 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
2286 vtop
->c
.i
= vtop
->c
.ld
;
2287 } else if (sf
&& dbt
== VT_BOOL
) {
2288 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
2291 vtop
->c
.i
= vtop
->c
.ld
;
2292 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
2294 else if (sbt
& VT_UNSIGNED
)
2295 vtop
->c
.i
= (uint32_t)vtop
->c
.i
;
2296 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2297 else if (sbt
== VT_PTR
)
2300 else if (sbt
!= VT_LLONG
)
2301 vtop
->c
.i
= ((uint32_t)vtop
->c
.i
|
2302 -(vtop
->c
.i
& 0x80000000));
2304 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
2306 else if (dbt
== VT_BOOL
)
2307 vtop
->c
.i
= (vtop
->c
.i
!= 0);
2308 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2309 else if (dbt
== VT_PTR
)
2312 else if (dbt
!= VT_LLONG
) {
2313 uint32_t m
= ((dbt
& VT_BTYPE
) == VT_BYTE
? 0xff :
2314 (dbt
& VT_BTYPE
) == VT_SHORT
? 0xffff :
2317 if (!(dbt
& VT_UNSIGNED
))
2318 vtop
->c
.i
|= -(vtop
->c
.i
& ((m
>> 1) + 1));
2321 } else if (p
&& dbt
== VT_BOOL
) {
2324 } else if (!nocode_wanted
) {
2325 /* non constant case: generate code */
2327 /* convert from fp to fp */
2330 /* convert int to fp */
2333 /* convert fp to int */
2334 if (dbt
== VT_BOOL
) {
2338 /* we handle char/short/etc... with generic code */
2339 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
2340 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
2344 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
2345 /* additional cast for char/short... */
2350 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
2351 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
2352 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
2353 /* scalar to long long */
2354 /* machine independent conversion */
2356 /* generate high word */
2357 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
2361 if (sbt
== VT_PTR
) {
2362 /* cast from pointer to int before we apply
2363 shift operation, which pointers don't support*/
2364 gen_cast(&int_type
);
2370 /* patch second register */
2371 vtop
[-1].r2
= vtop
->r
;
2375 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
2376 (dbt
& VT_BTYPE
) == VT_PTR
||
2377 (dbt
& VT_BTYPE
) == VT_FUNC
) {
2378 if ((sbt
& VT_BTYPE
) != VT_LLONG
&&
2379 (sbt
& VT_BTYPE
) != VT_PTR
&&
2380 (sbt
& VT_BTYPE
) != VT_FUNC
) {
2381 /* need to convert from 32bit to 64bit */
2383 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
2384 #if defined(TCC_TARGET_ARM64)
2386 #elif defined(TCC_TARGET_X86_64)
2388 /* x86_64 specific: movslq */
2390 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
2397 } else if (dbt
== VT_BOOL
) {
2398 /* scalar to bool */
2401 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
2402 (dbt
& VT_BTYPE
) == VT_SHORT
) {
2403 if (sbt
== VT_PTR
) {
2404 vtop
->type
.t
= VT_INT
;
2405 tcc_warning("nonportable conversion from pointer to char/short");
2407 force_charshort_cast(dbt
);
2408 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
2409 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
2411 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
2412 /* from long long: just take low order word */
2416 /* if lvalue and single word type, nothing to do because
2417 the lvalue already contains the real type size (see
2418 VT_LVAL_xxx constants) */
2422 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
2423 /* if we are casting between pointer types,
2424 we must update the VT_LVAL_xxx size */
2425 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
2426 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
2431 /* return type size as known at compile time. Put alignment at 'a' */
2432 ST_FUNC
int type_size(CType
*type
, int *a
)
2437 bt
= type
->t
& VT_BTYPE
;
2438 if (bt
== VT_STRUCT
) {
2443 } else if (bt
== VT_PTR
) {
2444 if (type
->t
& VT_ARRAY
) {
2448 ts
= type_size(&s
->type
, a
);
2450 if (ts
< 0 && s
->c
< 0)
2458 } else if (bt
== VT_LDOUBLE
) {
2460 return LDOUBLE_SIZE
;
2461 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
2462 #ifdef TCC_TARGET_I386
2463 #ifdef TCC_TARGET_PE
2468 #elif defined(TCC_TARGET_ARM)
2478 } else if (bt
== VT_INT
|| bt
== VT_FLOAT
) {
2481 } else if (bt
== VT_SHORT
) {
2484 } else if (bt
== VT_QLONG
|| bt
== VT_QFLOAT
) {
2487 } else if (bt
== VT_ENUM
) {
2489 /* Enums might be incomplete, so don't just return '4' here. */
2490 return type
->ref
->c
;
2492 /* char, void, function, _Bool */
2498 /* push type size as known at runtime time on top of value stack. Put
2500 ST_FUNC
void vla_runtime_type_size(CType
*type
, int *a
)
2502 if (type
->t
& VT_VLA
) {
2503 type_size(&type
->ref
->type
, a
);
2504 vset(&int_type
, VT_LOCAL
|VT_LVAL
, type
->ref
->c
);
2506 vpushi(type_size(type
, a
));
2510 static void vla_sp_restore(void) {
2511 if (vlas_in_scope
) {
2512 gen_vla_sp_restore(vla_sp_loc
);
2516 static void vla_sp_restore_root(void) {
2517 if (vlas_in_scope
) {
2518 gen_vla_sp_restore(vla_sp_root_loc
);
2522 /* return the pointed type of t */
2523 static inline CType
*pointed_type(CType
*type
)
2525 return &type
->ref
->type
;
2528 /* modify type so that its it is a pointer to type. */
2529 ST_FUNC
void mk_pointer(CType
*type
)
2532 s
= sym_push(SYM_FIELD
, type
, 0, -1);
2533 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
2537 /* compare function types. OLD functions match any new functions */
2538 static int is_compatible_func(CType
*type1
, CType
*type2
)
2544 if (!is_compatible_types(&s1
->type
, &s2
->type
))
2546 /* check func_call */
2547 if (s1
->a
.func_call
!= s2
->a
.func_call
)
2549 /* XXX: not complete */
2550 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
2554 while (s1
!= NULL
) {
2557 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
2567 /* return true if type1 and type2 are the same. If unqualified is
2568 true, qualifiers on the types are ignored.
2570 - enums are not checked as gcc __builtin_types_compatible_p ()
2572 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2576 t1
= type1
->t
& VT_TYPE
;
2577 t2
= type2
->t
& VT_TYPE
;
2579 /* strip qualifiers before comparing */
2580 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2581 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2583 /* Default Vs explicit signedness only matters for char */
2584 if ((t1
& VT_BTYPE
) != VT_BYTE
) {
2588 /* An enum is compatible with (unsigned) int. Ideally we would
2589 store the enums signedness in type->ref.a.<some_bit> and
2590 only accept unsigned enums with unsigned int and vice versa.
2591 But one of our callers (gen_assign_cast) always strips VT_UNSIGNED
2592 from pointer target types, so we can't add it here either. */
2593 if ((t1
& VT_BTYPE
) == VT_ENUM
) {
2595 if (type1
->ref
->a
.unsigned_enum
)
2598 if ((t2
& VT_BTYPE
) == VT_ENUM
) {
2600 if (type2
->ref
->a
.unsigned_enum
)
2603 /* XXX: bitfields ? */
2606 /* test more complicated cases */
2607 bt1
= t1
& VT_BTYPE
;
2608 if (bt1
== VT_PTR
) {
2609 type1
= pointed_type(type1
);
2610 type2
= pointed_type(type2
);
2611 return is_compatible_types(type1
, type2
);
2612 } else if (bt1
== VT_STRUCT
) {
2613 return (type1
->ref
== type2
->ref
);
2614 } else if (bt1
== VT_FUNC
) {
2615 return is_compatible_func(type1
, type2
);
2621 /* return true if type1 and type2 are exactly the same (including
2624 static int is_compatible_types(CType
*type1
, CType
*type2
)
2626 return compare_types(type1
,type2
,0);
2629 /* return true if type1 and type2 are the same (ignoring qualifiers).
2631 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
2633 return compare_types(type1
,type2
,1);
2636 /* print a type. If 'varstr' is not NULL, then the variable is also
2637 printed in the type */
2639 /* XXX: add array and function pointers */
2640 static void type_to_str(char *buf
, int buf_size
,
2641 CType
*type
, const char *varstr
)
2648 t
= type
->t
& VT_TYPE
;
2651 if (t
& VT_CONSTANT
)
2652 pstrcat(buf
, buf_size
, "const ");
2653 if (t
& VT_VOLATILE
)
2654 pstrcat(buf
, buf_size
, "volatile ");
2655 if ((t
& (VT_DEFSIGN
| VT_UNSIGNED
)) == (VT_DEFSIGN
| VT_UNSIGNED
))
2656 pstrcat(buf
, buf_size
, "unsigned ");
2657 else if (t
& VT_DEFSIGN
)
2658 pstrcat(buf
, buf_size
, "signed ");
2688 tstr
= "long double";
2690 pstrcat(buf
, buf_size
, tstr
);
2694 if (bt
== VT_STRUCT
)
2698 pstrcat(buf
, buf_size
, tstr
);
2699 v
= type
->ref
->v
& ~SYM_STRUCT
;
2700 if (v
>= SYM_FIRST_ANOM
)
2701 pstrcat(buf
, buf_size
, "<anonymous>");
2703 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
2707 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
2708 pstrcat(buf
, buf_size
, "(");
2710 while (sa
!= NULL
) {
2711 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
2712 pstrcat(buf
, buf_size
, buf1
);
2715 pstrcat(buf
, buf_size
, ", ");
2717 pstrcat(buf
, buf_size
, ")");
2722 snprintf(buf1
, sizeof(buf1
), "%s[%ld]", varstr
? varstr
: "", s
->c
);
2723 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2726 pstrcpy(buf1
, sizeof(buf1
), "*");
2727 if (t
& VT_CONSTANT
)
2728 pstrcat(buf1
, buf_size
, "const ");
2729 if (t
& VT_VOLATILE
)
2730 pstrcat(buf1
, buf_size
, "volatile ");
2732 pstrcat(buf1
, sizeof(buf1
), varstr
);
2733 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2737 pstrcat(buf
, buf_size
, " ");
2738 pstrcat(buf
, buf_size
, varstr
);
2743 /* verify type compatibility to store vtop in 'dt' type, and generate
2745 static void gen_assign_cast(CType
*dt
)
2747 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
2748 char buf1
[256], buf2
[256];
2751 st
= &vtop
->type
; /* source type */
2752 dbt
= dt
->t
& VT_BTYPE
;
2753 sbt
= st
->t
& VT_BTYPE
;
2754 if (sbt
== VT_VOID
|| dbt
== VT_VOID
) {
2755 if (sbt
== VT_VOID
&& dbt
== VT_VOID
)
2757 It is Ok if both are void
2763 gcc accepts this program
2766 tcc_error("cannot cast from/to void");
2768 if (dt
->t
& VT_CONSTANT
)
2769 tcc_warning("assignment of read-only location");
2772 /* special cases for pointers */
2773 /* '0' can also be a pointer */
2774 if (is_null_pointer(vtop
))
2776 /* accept implicit pointer to integer cast with warning */
2777 if (is_integer_btype(sbt
)) {
2778 tcc_warning("assignment makes pointer from integer without a cast");
2781 type1
= pointed_type(dt
);
2782 /* a function is implicitely a function pointer */
2783 if (sbt
== VT_FUNC
) {
2784 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
2785 !is_compatible_types(pointed_type(dt
), st
))
2786 tcc_warning("assignment from incompatible pointer type");
2791 type2
= pointed_type(st
);
2792 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2793 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
2794 /* void * can match anything */
2796 /* exact type match, except for qualifiers */
2799 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2800 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2801 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2802 /* Like GCC don't warn by default for merely changes
2803 in pointer target signedness. Do warn for different
2804 base types, though, in particular for unsigned enums
2805 and signed int targets. */
2806 if ((tmp_type1
.t
& (VT_DEFSIGN
| VT_UNSIGNED
)) !=
2807 (tmp_type2
.t
& (VT_DEFSIGN
| VT_UNSIGNED
)) &&
2808 (tmp_type1
.t
& VT_BTYPE
) == (tmp_type2
.t
& VT_BTYPE
))
2811 tcc_warning("assignment from incompatible pointer type");
2814 /* check const and volatile */
2815 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
2816 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
2817 tcc_warning("assignment discards qualifiers from pointer target type");
2823 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
2824 tcc_warning("assignment makes integer from pointer without a cast");
2825 } else if (sbt
== VT_STRUCT
) {
2826 goto case_VT_STRUCT
;
2828 /* XXX: more tests */
2834 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2835 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2836 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2838 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
2839 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
2840 tcc_error("cannot cast '%s' to '%s'", buf1
, buf2
);
2848 /* store vtop in lvalue pushed on stack */
2849 ST_FUNC
void vstore(void)
2851 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
2853 ft
= vtop
[-1].type
.t
;
2854 sbt
= vtop
->type
.t
& VT_BTYPE
;
2855 dbt
= ft
& VT_BTYPE
;
2856 if ((((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2857 (sbt
== VT_INT
&& dbt
== VT_SHORT
))
2858 && !(vtop
->type
.t
& VT_BITFIELD
)) {
2859 /* optimize char/short casts */
2860 delayed_cast
= VT_MUSTCAST
;
2861 vtop
->type
.t
= (ft
& VT_TYPE
& ~VT_BITFIELD
&
2862 ((1 << VT_STRUCT_SHIFT
) - 1));
2863 /* XXX: factorize */
2864 if (ft
& VT_CONSTANT
)
2865 tcc_warning("assignment of read-only location");
2868 if (!(ft
& VT_BITFIELD
))
2869 gen_assign_cast(&vtop
[-1].type
);
2872 if (sbt
== VT_STRUCT
) {
2873 /* if structure, only generate pointer */
2874 /* structure assignment : generate memcpy */
2875 /* XXX: optimize if small size */
2876 if (!nocode_wanted
) {
2877 size
= type_size(&vtop
->type
, &align
);
2881 vtop
->type
.t
= VT_PTR
;
2884 /* address of memcpy() */
2887 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2888 else if(!(align
& 3))
2889 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2892 /* Use memmove, rather than memcpy, as dest and src may be same: */
2893 vpush_global_sym(&func_old_type
, TOK_memmove
);
2898 vtop
->type
.t
= VT_PTR
;
2907 /* leave source on stack */
2908 } else if (ft
& VT_BITFIELD
) {
2909 /* bitfield store handling */
2911 /* save lvalue as expression result (example: s.b = s.a = n;) */
2912 vdup(), vtop
[-1] = vtop
[-2];
2914 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2915 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2916 /* remove bit field info to avoid loops */
2917 vtop
[-1].type
.t
= ft
& ~VT_BITFIELD
& ((1 << VT_STRUCT_SHIFT
) - 1);
2919 if((ft
& VT_BTYPE
) == VT_BOOL
) {
2920 gen_cast(&vtop
[-1].type
);
2921 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2924 /* duplicate destination */
2926 vtop
[-1] = vtop
[-2];
2928 /* mask and shift source */
2929 if((ft
& VT_BTYPE
) != VT_BOOL
) {
2930 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2931 vpushll((1ULL << bit_size
) - 1ULL);
2933 vpushi((1 << bit_size
) - 1);
2939 /* load destination, mask and or with source */
2941 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2942 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2944 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2950 /* ... and discard */
2954 if (!nocode_wanted
) {
2955 #ifdef CONFIG_TCC_BCHECK
2956 /* bound check case */
2957 if (vtop
[-1].r
& VT_MUSTBOUND
) {
2966 #ifdef TCC_TARGET_X86_64
2967 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
2969 } else if ((ft
& VT_BTYPE
) == VT_QFLOAT
) {
2974 r
= gv(rc
); /* generate value */
2975 /* if lvalue was saved on stack, must read it */
2976 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
2978 t
= get_reg(RC_INT
);
2979 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2984 sv
.r
= VT_LOCAL
| VT_LVAL
;
2985 sv
.c
.i
= vtop
[-1].c
.i
;
2987 vtop
[-1].r
= t
| VT_LVAL
;
2989 /* two word case handling : store second register at word + 4 (or +8 for x86-64) */
2990 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2991 if (((ft
& VT_BTYPE
) == VT_QLONG
) || ((ft
& VT_BTYPE
) == VT_QFLOAT
)) {
2992 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
2994 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
2995 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
2997 vtop
[-1].type
.t
= load_type
;
3000 /* convert to int to increment easily */
3001 vtop
->type
.t
= addr_type
;
3007 vtop
[-1].type
.t
= load_type
;
3008 /* XXX: it works because r2 is spilled last ! */
3009 store(vtop
->r2
, vtop
- 1);
3015 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
3016 vtop
->r
|= delayed_cast
;
3020 /* post defines POST/PRE add. c is the token ++ or -- */
3021 ST_FUNC
void inc(int post
, int c
)
3024 vdup(); /* save lvalue */
3027 gv_dup(); /* duplicate value */
3029 vdup(); /* duplicate value */
3034 vpushi(c
- TOK_MID
);
3036 vstore(); /* store value */
3038 vpop(); /* if post op, return saved value */
3041 ST_FUNC
void parse_mult_str (CString
*astr
, const char *msg
)
3043 /* read the string */
3047 while (tok
== TOK_STR
) {
3048 /* XXX: add \0 handling too ? */
3049 cstr_cat(astr
, tokc
.str
.data
, -1);
3052 cstr_ccat(astr
, '\0');
3055 /* Parse GNUC __attribute__ extension. Currently, the following
3056 extensions are recognized:
3057 - aligned(n) : set data/function alignment.
3058 - packed : force data alignment to 1
3059 - section(x) : generate data/code in this section.
3060 - unused : currently ignored, but may be used someday.
3061 - regparm(n) : pass function parameters in registers (i386 only)
3063 static void parse_attribute(AttributeDef
*ad
)
3068 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
3072 while (tok
!= ')') {
3073 if (tok
< TOK_IDENT
)
3074 expect("attribute name");
3081 parse_mult_str(&astr
, "section name");
3082 ad
->section
= find_section(tcc_state
, (char *)astr
.data
);
3089 parse_mult_str(&astr
, "alias(\"target\")");
3090 ad
->alias_target
= /* save string as token, for later */
3091 tok_alloc((char*)astr
.data
, astr
.size
-1)->tok
;
3095 case TOK_VISIBILITY1
:
3096 case TOK_VISIBILITY2
:
3098 parse_mult_str(&astr
,
3099 "visibility(\"default|hidden|internal|protected\")");
3100 if (!strcmp (astr
.data
, "default"))
3101 ad
->a
.visibility
= STV_DEFAULT
;
3102 else if (!strcmp (astr
.data
, "hidden"))
3103 ad
->a
.visibility
= STV_HIDDEN
;
3104 else if (!strcmp (astr
.data
, "internal"))
3105 ad
->a
.visibility
= STV_INTERNAL
;
3106 else if (!strcmp (astr
.data
, "protected"))
3107 ad
->a
.visibility
= STV_PROTECTED
;
3109 expect("visibility(\"default|hidden|internal|protected\")");
3118 if (n
<= 0 || (n
& (n
- 1)) != 0)
3119 tcc_error("alignment must be a positive power of two");
3136 /* currently, no need to handle it because tcc does not
3137 track unused objects */
3141 /* currently, no need to handle it because tcc does not
3142 track unused objects */
3147 ad
->a
.func_call
= FUNC_CDECL
;
3152 ad
->a
.func_call
= FUNC_STDCALL
;
3154 #ifdef TCC_TARGET_I386
3164 ad
->a
.func_call
= FUNC_FASTCALL1
+ n
- 1;
3170 ad
->a
.func_call
= FUNC_FASTCALLW
;
3177 ad
->a
.mode
= VT_LLONG
+ 1;
3180 ad
->a
.mode
= VT_BYTE
+ 1;
3183 ad
->a
.mode
= VT_SHORT
+ 1;
3187 ad
->a
.mode
= VT_INT
+ 1;
3190 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
3197 ad
->a
.func_export
= 1;
3200 ad
->a
.func_import
= 1;
3203 if (tcc_state
->warn_unsupported
)
3204 tcc_warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
3205 /* skip parameters */
3207 int parenthesis
= 0;
3211 else if (tok
== ')')
3214 } while (parenthesis
&& tok
!= -1);
3227 static Sym
* find_field (CType
*type
, int v
)
3231 while ((s
= s
->next
) != NULL
) {
3232 if ((s
->v
& SYM_FIELD
) && (s
->v
& ~SYM_FIELD
) >= SYM_FIRST_ANOM
) {
3233 Sym
*ret
= find_field (&s
->type
, v
);
3243 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
3244 static void struct_decl(CType
*type
, AttributeDef
*ad
, int u
)
3246 int a
, v
, size
, align
, maxalign
, c
, offset
, flexible
, extra_bytes
;
3247 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
, prevbt
;
3248 Sym
*s
, *ss
, *ass
, **ps
;
3252 a
= tok
; /* save decl type */
3254 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3255 parse_attribute(ad
);
3259 /* struct already defined ? return it */
3261 expect("struct/union/enum name");
3263 if (s
&& (s
->scope
== local_scope
|| (tok
!= '{' && tok
!= ';'))) {
3265 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
3271 /* Record the original enum/struct/union token. */
3274 /* we put an undefined size for struct/union */
3275 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
3276 s
->r
= 0; /* default alignment is zero as gcc */
3277 /* put struct/union/enum name in type */
3285 tcc_error("struct/union/enum already defined");
3286 /* cannot be empty */
3288 /* non empty enums are not allowed */
3289 if (a
== TOK_ENUM
) {
3294 expect("identifier");
3296 if (ss
&& !local_stack
)
3297 tcc_error("redefinition of enumerator '%s'",
3298 get_tok_str(v
, NULL
));
3306 /* enum symbols have static storage */
3307 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
3308 ss
->type
.t
|= VT_STATIC
;
3313 /* NOTE: we accept a trailing comma */
3318 s
->a
.unsigned_enum
= 1;
3319 s
->c
= type_size(&int_type
, &align
);
3328 while (tok
!= '}') {
3329 if (!parse_btype(&btype
, &ad1
)) {
3336 tcc_error("flexible array member '%s' not at the end of struct",
3337 get_tok_str(v
, NULL
));
3342 type_decl(&type1
, &ad1
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3344 if ((type1
.t
& VT_BTYPE
) != VT_STRUCT
)
3345 expect("identifier");
3347 int v
= btype
.ref
->v
;
3348 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
3349 if (tcc_state
->ms_extensions
== 0)
3350 expect("identifier");
3354 if (type_size(&type1
, &align
) < 0) {
3355 if ((a
== TOK_STRUCT
) && (type1
.t
& VT_ARRAY
) && c
)
3358 tcc_error("field '%s' has incomplete type",
3359 get_tok_str(v
, NULL
));
3361 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
3362 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
3363 tcc_error("invalid type for '%s'",
3364 get_tok_str(v
, NULL
));
3368 bit_size
= expr_const();
3369 /* XXX: handle v = 0 case for messages */
3371 tcc_error("negative width in bit-field '%s'",
3372 get_tok_str(v
, NULL
));
3373 if (v
&& bit_size
== 0)
3374 tcc_error("zero width for bit-field '%s'",
3375 get_tok_str(v
, NULL
));
3377 size
= type_size(&type1
, &align
);
3378 if (ad1
.a
.aligned
) {
3379 if (align
< ad1
.a
.aligned
)
3380 align
= ad1
.a
.aligned
;
3381 } else if (ad1
.a
.packed
|| ad
->a
.packed
) {
3383 } else if (*tcc_state
->pack_stack_ptr
) {
3384 if (align
> *tcc_state
->pack_stack_ptr
)
3385 align
= *tcc_state
->pack_stack_ptr
;
3388 if (bit_size
>= 0) {
3389 bt
= type1
.t
& VT_BTYPE
;
3396 tcc_error("bitfields must have scalar type");
3398 if (bit_size
> bsize
) {
3399 tcc_error("width of '%s' exceeds its type",
3400 get_tok_str(v
, NULL
));
3401 } else if (bit_size
== bsize
) {
3402 /* no need for bit fields */
3404 } else if (bit_size
== 0) {
3405 /* XXX: what to do if only padding in a
3407 /* zero size: means to pad */
3410 /* if type change, union, or will overrun
3411 * allignment slot, start at a newly
3413 if ((bit_pos
+ bit_size
) > bsize
||
3414 bt
!= prevbt
|| a
== TOK_UNION
)
3417 /* XXX: handle LSB first */
3418 type1
.t
|= VT_BITFIELD
|
3419 (bit_pos
<< VT_STRUCT_SHIFT
) |
3420 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
3421 bit_pos
+= bit_size
;
3422 /* without ms-bitfields, allocate the
3423 * minimum number of bytes necessary,
3424 * adding single bytes as needed */
3425 if (!tcc_state
->ms_bitfields
) {
3427 /* minimum bytes for new bitfield */
3428 size
= (bit_size
+ 7) / 8;
3430 /* enough spare bits already allocated? */
3431 bit_size
= (lbit_pos
- 1) % 8 + 1 + bit_size
;
3432 if (bit_size
> 8) /* doesn't fit */
3433 extra_bytes
= (bit_size
- 1) / 8;
3441 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
3442 /* add new memory data only if starting bit
3443 field or adding bytes to existing bit field */
3444 if (extra_bytes
) c
+= extra_bytes
;
3445 else if (lbit_pos
== 0) {
3446 if (a
== TOK_STRUCT
) {
3447 c
= (c
+ align
- 1) & -align
;
3456 if (align
> maxalign
)
3460 printf("add field %s offset=%d",
3461 get_tok_str(v
, NULL
), offset
);
3462 if (type1
.t
& VT_BITFIELD
) {
3463 printf(" pos=%d size=%d",
3464 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
3465 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
3470 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
3471 /* An anonymous struct/union. Adjust member offsets
3472 to reflect the real offset of our containing struct.
3473 Also set the offset of this anon member inside
3474 the outer struct to be zero. Via this it
3475 works when accessing the field offset directly
3476 (from base object), as well as when recursing
3477 members in initializer handling. */
3478 int v2
= btype
.ref
->v
;
3479 if (!(v2
& SYM_FIELD
) &&
3480 (v2
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
3482 /* This happens only with MS extensions. The
3483 anon member has a named struct type, so it
3484 potentially is shared with other references.
3485 We need to unshare members so we can modify
3488 type1
.ref
= sym_push(anon_sym
++ | SYM_FIELD
,
3489 &type1
.ref
->type
, 0,
3491 pps
= &type1
.ref
->next
;
3492 while ((ass
= ass
->next
) != NULL
) {
3493 *pps
= sym_push(ass
->v
, &ass
->type
, 0, ass
->c
);
3494 pps
= &((*pps
)->next
);
3499 while ((ass
= ass
->next
) != NULL
)
3505 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
3509 if (tok
== ';' || tok
== TOK_EOF
)
3516 /* store size and alignment */
3517 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
3523 /* return 1 if basic type is a type size (short, long, long long) */
3524 ST_FUNC
int is_btype_size(int bt
)
3526 return bt
== VT_SHORT
|| bt
== VT_LONG
|| bt
== VT_LLONG
;
3529 /* Add type qualifiers to a type. If the type is an array then the qualifiers
3530 are added to the element type, copied because it could be a typedef. */
3531 static void parse_btype_qualify(CType
*type
, int qualifiers
)
3533 while (type
->t
& VT_ARRAY
) {
3534 type
->ref
= sym_push(SYM_FIELD
, &type
->ref
->type
, 0, type
->ref
->c
);
3535 type
= &type
->ref
->type
;
3537 type
->t
|= qualifiers
;
3540 /* return 0 if no type declaration. otherwise, return the basic type
3543 static int parse_btype(CType
*type
, AttributeDef
*ad
)
3545 int t
, u
, bt_size
, complete
, type_found
, typespec_found
;
3549 memset(ad
, 0, sizeof(AttributeDef
));
3557 /* currently, we really ignore extension */
3568 tcc_error("too many basic types");
3570 bt_size
= is_btype_size (u
& VT_BTYPE
);
3571 if (u
== VT_INT
|| (!bt_size
&& !(t
& VT_TYPEDEF
)))
3586 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
3587 #ifndef TCC_TARGET_PE
3588 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3590 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
3591 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3597 #ifdef TCC_TARGET_ARM64
3599 /* GCC's __uint128_t appears in some Linux header files. Make it a
3600 synonym for long double to get the size and alignment right. */
3612 if ((t
& VT_BTYPE
) == VT_LONG
) {
3613 #ifdef TCC_TARGET_PE
3614 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
3616 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3624 struct_decl(&type1
, ad
, VT_ENUM
);
3627 type
->ref
= type1
.ref
;
3631 struct_decl(&type1
, ad
, VT_STRUCT
);
3634 /* type modifiers */
3639 parse_btype_qualify(type
, VT_CONSTANT
);
3647 parse_btype_qualify(type
, VT_VOLATILE
);
3654 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == (VT_DEFSIGN
|VT_UNSIGNED
))
3655 tcc_error("signed and unsigned modifier");
3668 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == VT_DEFSIGN
)
3669 tcc_error("signed and unsigned modifier");
3670 t
|= VT_DEFSIGN
| VT_UNSIGNED
;
3695 /* GNUC attribute */
3696 case TOK_ATTRIBUTE1
:
3697 case TOK_ATTRIBUTE2
:
3698 parse_attribute(ad
);
3701 t
= (t
& ~VT_BTYPE
) | u
;
3709 parse_expr_type(&type1
);
3710 /* remove all storage modifiers except typedef */
3711 type1
.t
&= ~(VT_STORAGE
&~VT_TYPEDEF
);
3717 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
3720 type
->t
= ((s
->type
.t
& ~VT_TYPEDEF
) |
3721 (t
& ~(VT_CONSTANT
| VT_VOLATILE
)));
3722 type
->ref
= s
->type
.ref
;
3723 if (t
& (VT_CONSTANT
| VT_VOLATILE
))
3724 parse_btype_qualify(type
, t
& (VT_CONSTANT
| VT_VOLATILE
));
3728 /* get attributes from typedef */
3729 if (0 == ad
->a
.aligned
)
3730 ad
->a
.aligned
= s
->a
.aligned
;
3731 if (0 == ad
->a
.func_call
)
3732 ad
->a
.func_call
= s
->a
.func_call
;
3733 ad
->a
.packed
|= s
->a
.packed
;
3742 if (tcc_state
->char_is_unsigned
) {
3743 if ((t
& (VT_DEFSIGN
|VT_BTYPE
)) == VT_BYTE
)
3747 /* long is never used as type */
3748 if ((t
& VT_BTYPE
) == VT_LONG
)
3749 #if (!defined TCC_TARGET_X86_64 && !defined TCC_TARGET_ARM64) || \
3750 defined TCC_TARGET_PE
3751 t
= (t
& ~VT_BTYPE
) | VT_INT
;
3753 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3759 /* convert a function parameter type (array to pointer and function to
3760 function pointer) */
3761 static inline void convert_parameter_type(CType
*pt
)
3763 /* remove const and volatile qualifiers (XXX: const could be used
3764 to indicate a const function parameter */
3765 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3766 /* array must be transformed to pointer according to ANSI C */
3768 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
3773 ST_FUNC
void parse_asm_str(CString
*astr
)
3776 parse_mult_str(astr
, "string constant");
3779 /* Parse an asm label and return the token */
3780 static int asm_label_instr(void)
3786 parse_asm_str(&astr
);
3789 printf("asm_alias: \"%s\"\n", (char *)astr
.data
);
3791 v
= tok_alloc(astr
.data
, astr
.size
- 1)->tok
;
3796 static void post_type(CType
*type
, AttributeDef
*ad
, int storage
)
3798 int n
, l
, t1
, arg_size
, align
;
3799 Sym
**plast
, *s
, *first
;
3804 /* function declaration */
3812 /* read param name and compute offset */
3813 if (l
!= FUNC_OLD
) {
3814 if (!parse_btype(&pt
, &ad1
)) {
3816 tcc_error("invalid type");
3823 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
3825 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3826 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
3827 tcc_error("parameter declared as void");
3828 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
3833 expect("identifier");
3837 convert_parameter_type(&pt
);
3838 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
3844 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
3851 /* if no parameters, then old type prototype */
3855 /* NOTE: const is ignored in returned type as it has a special
3856 meaning in gcc / C++ */
3857 type
->t
&= ~VT_CONSTANT
;
3858 /* some ancient pre-K&R C allows a function to return an array
3859 and the array brackets to be put after the arguments, such
3860 that "int c()[]" means something like "int[] c()" */
3863 skip(']'); /* only handle simple "[]" */
3866 /* we push a anonymous symbol which will contain the function prototype */
3867 ad
->a
.func_args
= arg_size
;
3868 s
= sym_push(SYM_FIELD
, type
, 0, l
);
3873 } else if (tok
== '[') {
3874 int saved_nocode_wanted
= nocode_wanted
;
3875 /* array definition */
3877 if (tok
== TOK_RESTRICT1
)
3882 if (!local_stack
|| (storage
& VT_STATIC
))
3883 vpushi(expr_const());
3885 /* VLAs (which can only happen with local_stack && !VT_STATIC)
3886 length must always be evaluated, even under nocode_wanted,
3887 so that its size slot is initialized (e.g. under sizeof
3892 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3895 tcc_error("invalid array size");
3897 if (!is_integer_btype(vtop
->type
.t
& VT_BTYPE
))
3898 tcc_error("size of variable length array should be an integer");
3903 /* parse next post type */
3904 post_type(type
, ad
, storage
);
3905 if (type
->t
== VT_FUNC
)
3906 tcc_error("declaration of an array of functions");
3907 t1
|= type
->t
& VT_VLA
;
3910 loc
-= type_size(&int_type
, &align
);
3914 vla_runtime_type_size(type
, &align
);
3916 vset(&int_type
, VT_LOCAL
|VT_LVAL
, n
);
3922 nocode_wanted
= saved_nocode_wanted
;
3924 /* we push an anonymous symbol which will contain the array
3926 s
= sym_push(SYM_FIELD
, type
, 0, n
);
3927 type
->t
= (t1
? VT_VLA
: VT_ARRAY
) | VT_PTR
;
3932 /* Parse a type declaration (except basic type), and return the type
3933 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3934 expected. 'type' should contain the basic type. 'ad' is the
3935 attribute definition of the basic type. It can be modified by
3938 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
3941 CType type1
, *type2
;
3942 int qualifiers
, storage
;
3944 while (tok
== '*') {
3952 qualifiers
|= VT_CONSTANT
;
3957 qualifiers
|= VT_VOLATILE
;
3963 /* XXX: clarify attribute handling */
3964 case TOK_ATTRIBUTE1
:
3965 case TOK_ATTRIBUTE2
:
3966 parse_attribute(ad
);
3970 type
->t
|= qualifiers
;
3973 /* recursive type */
3974 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3975 type1
.t
= 0; /* XXX: same as int */
3978 /* XXX: this is not correct to modify 'ad' at this point, but
3979 the syntax is not clear */
3980 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3981 parse_attribute(ad
);
3982 type_decl(&type1
, ad
, v
, td
);
3985 /* type identifier */
3986 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
3990 if (!(td
& TYPE_ABSTRACT
))
3991 expect("identifier");
3995 storage
= type
->t
& VT_STORAGE
;
3996 type
->t
&= ~VT_STORAGE
;
3997 post_type(type
, ad
, storage
);
3999 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
4000 parse_attribute(ad
);
4004 /* append type at the end of type1 */
4017 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
4018 ST_FUNC
int lvalue_type(int t
)
4023 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
4025 else if (bt
== VT_SHORT
)
4029 if (t
& VT_UNSIGNED
)
4030 r
|= VT_LVAL_UNSIGNED
;
4034 /* indirection with full error checking and bound check */
4035 ST_FUNC
void indir(void)
4037 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
4038 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
4042 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
4044 vtop
->type
= *pointed_type(&vtop
->type
);
4045 /* Arrays and functions are never lvalues */
4046 if (!(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_VLA
)
4047 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
4048 vtop
->r
|= lvalue_type(vtop
->type
.t
);
4049 /* if bound checking, the referenced pointer must be checked */
4050 #ifdef CONFIG_TCC_BCHECK
4051 if (tcc_state
->do_bounds_check
)
4052 vtop
->r
|= VT_MUSTBOUND
;
4057 /* pass a parameter to a function and do type checking and casting */
4058 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
4063 func_type
= func
->c
;
4064 if (func_type
== FUNC_OLD
||
4065 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
4066 /* default casting : only need to convert float to double */
4067 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
4070 } else if (vtop
->type
.t
& VT_BITFIELD
) {
4071 type
.t
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
4072 type
.ref
= vtop
->type
.ref
;
4075 } else if (arg
== NULL
) {
4076 tcc_error("too many arguments to function");
4079 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
4080 gen_assign_cast(&type
);
4084 /* parse an expression of the form '(type)' or '(expr)' and return its
4086 static void parse_expr_type(CType
*type
)
4092 if (parse_btype(type
, &ad
)) {
4093 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
4100 static void parse_type(CType
*type
)
4105 if (!parse_btype(type
, &ad
)) {
4108 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
4111 static void vpush_tokc(int t
)
4116 vsetc(&type
, VT_CONST
, &tokc
);
4119 ST_FUNC
void unary(void)
4121 int n
, t
, align
, size
, r
, sizeof_caller
;
4126 sizeof_caller
= in_sizeof
;
4128 /* XXX: GCC 2.95.3 does not generate a table although it should be
4142 vpush_tokc(VT_INT
| VT_UNSIGNED
);
4146 vpush_tokc(VT_LLONG
);
4150 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
4154 vpush_tokc(VT_FLOAT
);
4158 vpush_tokc(VT_DOUBLE
);
4162 vpush_tokc(VT_LDOUBLE
);
4165 case TOK___FUNCTION__
:
4167 goto tok_identifier
;
4173 /* special function name identifier */
4174 len
= strlen(funcname
) + 1;
4175 /* generate char[len] type */
4180 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
4181 ptr
= section_ptr_add(data_section
, len
);
4182 memcpy(ptr
, funcname
, len
);
4187 #ifdef TCC_TARGET_PE
4188 t
= VT_SHORT
| VT_UNSIGNED
;
4194 /* string parsing */
4197 if (tcc_state
->warn_write_strings
)
4202 memset(&ad
, 0, sizeof(AttributeDef
));
4203 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, 0);
4208 if (parse_btype(&type
, &ad
)) {
4209 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
4211 /* check ISOC99 compound literal */
4213 /* data is allocated locally by default */
4218 /* all except arrays are lvalues */
4219 if (!(type
.t
& VT_ARRAY
))
4220 r
|= lvalue_type(type
.t
);
4221 memset(&ad
, 0, sizeof(AttributeDef
));
4222 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, 0);
4224 if (sizeof_caller
) {
4231 } else if (tok
== '{') {
4233 tcc_error("expected constant");
4234 /* save all registers */
4237 /* statement expression : we do not accept break/continue
4238 inside as GCC does */
4239 block(NULL
, NULL
, 1);
4254 /* functions names must be treated as function pointers,
4255 except for unary '&' and sizeof. Since we consider that
4256 functions are not lvalues, we only have to handle it
4257 there and in function calls. */
4258 /* arrays can also be used although they are not lvalues */
4259 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
4260 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
4262 mk_pointer(&vtop
->type
);
4268 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4270 boolean
.t
= VT_BOOL
;
4272 vtop
->c
.i
= !vtop
->c
.i
;
4273 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
4277 vseti(VT_JMP
, gvtst(1, 0));
4289 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
4290 tcc_error("pointer not accepted for unary plus");
4291 /* In order to force cast, we add zero, except for floating point
4292 where we really need an noop (otherwise -0.0 will be transformed
4294 if (!is_float(vtop
->type
.t
)) {
4305 unary_type(&type
); // Perform a in_sizeof = 0;
4306 size
= type_size(&type
, &align
);
4307 if (t
== TOK_SIZEOF
) {
4308 if (!(type
.t
& VT_VLA
)) {
4310 tcc_error("sizeof applied to an incomplete type");
4313 vla_runtime_type_size(&type
, &align
);
4318 vtop
->type
.t
|= VT_UNSIGNED
;
4321 case TOK_builtin_expect
:
4323 /* __builtin_expect is a no-op for now */
4324 int saved_nocode_wanted
;
4329 saved_nocode_wanted
= nocode_wanted
;
4333 nocode_wanted
= saved_nocode_wanted
;
4337 case TOK_builtin_types_compatible_p
:
4346 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
4347 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
4348 vpushi(is_compatible_types(&type1
, &type2
));
4351 case TOK_builtin_choose_expr
:
4353 int saved_nocode_wanted
, c
;
4359 saved_nocode_wanted
= nocode_wanted
;
4365 nocode_wanted
= saved_nocode_wanted
;
4369 saved_nocode_wanted
= nocode_wanted
;
4375 nocode_wanted
= saved_nocode_wanted
;
4380 case TOK_builtin_constant_p
:
4382 int saved_nocode_wanted
, res
;
4385 saved_nocode_wanted
= nocode_wanted
;
4388 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
4390 nocode_wanted
= saved_nocode_wanted
;
4395 case TOK_builtin_frame_address
:
4396 case TOK_builtin_return_address
:
4403 if (tok
!= TOK_CINT
) {
4404 tcc_error("%s only takes positive integers",
4405 tok1
== TOK_builtin_return_address
?
4406 "__builtin_return_address" :
4407 "__builtin_frame_address");
4409 level
= (uint32_t)tokc
.i
;
4414 vset(&type
, VT_LOCAL
, 0); /* local frame */
4416 mk_pointer(&vtop
->type
);
4417 indir(); /* -> parent frame */
4419 if (tok1
== TOK_builtin_return_address
) {
4420 // assume return address is just above frame pointer on stack
4423 mk_pointer(&vtop
->type
);
4428 #ifdef TCC_TARGET_X86_64
4429 #ifdef TCC_TARGET_PE
4430 case TOK_builtin_va_start
:
4438 if ((vtop
->r
& VT_VALMASK
) != VT_LOCAL
)
4439 tcc_error("__builtin_va_start expects a local variable");
4440 vtop
->r
&= ~(VT_LVAL
| VT_REF
);
4441 vtop
->type
= char_pointer_type
;
4447 case TOK_builtin_va_arg_types
:
4454 vpushi(classify_x86_64_va_arg(&type
));
4460 #ifdef TCC_TARGET_ARM64
4461 case TOK___va_start
: {
4463 tcc_error("statement in global scope");
4473 vtop
->type
.t
= VT_VOID
;
4476 case TOK___va_arg
: {
4479 tcc_error("statement in global scope");
4491 case TOK___arm64_clear_cache
: {
4500 vtop
->type
.t
= VT_VOID
;
4504 /* pre operations */
4515 t
= vtop
->type
.t
& VT_BTYPE
;
4517 /* In IEEE negate(x) isn't subtract(0,x), but rather
4522 else if (t
== VT_DOUBLE
)
4533 goto tok_identifier
;
4535 /* allow to take the address of a label */
4536 if (tok
< TOK_UIDENT
)
4537 expect("label identifier");
4538 s
= label_find(tok
);
4540 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4542 if (s
->r
== LABEL_DECLARED
)
4543 s
->r
= LABEL_FORWARD
;
4546 s
->type
.t
= VT_VOID
;
4547 mk_pointer(&s
->type
);
4548 s
->type
.t
|= VT_STATIC
;
4550 vpushsym(&s
->type
, s
);
4554 // special qnan , snan and infinity values
4556 vpush64(VT_DOUBLE
, 0x7ff8000000000000ULL
);
4560 vpush64(VT_DOUBLE
, 0x7ff0000000000001ULL
);
4564 vpush64(VT_DOUBLE
, 0x7ff0000000000000ULL
);
4573 expect("identifier");
4576 const char *name
= get_tok_str(t
, NULL
);
4578 tcc_error("'%s' undeclared", name
);
4579 /* for simple function calls, we tolerate undeclared
4580 external reference to int() function */
4581 if (tcc_state
->warn_implicit_function_declaration
4582 #ifdef TCC_TARGET_PE
4583 /* people must be warned about using undeclared WINAPI functions
4584 (which usually start with uppercase letter) */
4585 || (name
[0] >= 'A' && name
[0] <= 'Z')
4588 tcc_warning("implicit declaration of function '%s'", name
);
4589 s
= external_global_sym(t
, &func_old_type
, 0);
4591 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
4592 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
4593 /* if referencing an inline function, then we generate a
4594 symbol to it if not already done. It will have the
4595 effect to generate code for it at the end of the
4596 compilation unit. Inline function as always
4597 generated in the text section. */
4598 if (!s
->c
&& !nocode_wanted
)
4599 put_extern_sym(s
, text_section
, 0, 0);
4600 r
= VT_SYM
| VT_CONST
;
4603 /* A symbol that has a register is a local register variable,
4604 which starts out as VT_LOCAL value. */
4605 if ((r
& VT_VALMASK
) < VT_CONST
)
4606 r
= (r
& ~VT_VALMASK
) | VT_LOCAL
;
4608 vset(&s
->type
, r
, s
->c
);
4609 /* Point to s as backpointer (even without r&VT_SYM).
4610 Will be used by at least the x86 inline asm parser for
4613 if (vtop
->r
& VT_SYM
) {
4619 /* post operations */
4621 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
4624 } else if (tok
== '.' || tok
== TOK_ARROW
|| tok
== TOK_CDOUBLE
) {
4627 if (tok
== TOK_ARROW
)
4629 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
4632 /* expect pointer on structure */
4633 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
4634 expect("struct or union");
4635 if (tok
== TOK_CDOUBLE
)
4636 expect("field name");
4638 if (tok
== TOK_CINT
|| tok
== TOK_CUINT
)
4639 expect("field name");
4640 s
= find_field(&vtop
->type
, tok
);
4642 tcc_error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, &tokc
));
4643 /* add field offset to pointer */
4644 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
4647 /* change type to field type, and set to lvalue */
4648 vtop
->type
= s
->type
;
4649 vtop
->type
.t
|= qualifiers
;
4650 /* an array is never an lvalue */
4651 if (!(vtop
->type
.t
& VT_ARRAY
)) {
4652 vtop
->r
|= lvalue_type(vtop
->type
.t
);
4653 #ifdef CONFIG_TCC_BCHECK
4654 /* if bound checking, the referenced pointer must be checked */
4655 if (tcc_state
->do_bounds_check
&& (vtop
->r
& VT_VALMASK
) != VT_LOCAL
)
4656 vtop
->r
|= VT_MUSTBOUND
;
4660 } else if (tok
== '[') {
4666 } else if (tok
== '(') {
4669 int nb_args
, ret_nregs
, ret_align
, regsize
, variadic
;
4672 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
4673 /* pointer test (no array accepted) */
4674 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
4675 vtop
->type
= *pointed_type(&vtop
->type
);
4676 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
4680 expect("function pointer");
4683 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
4685 /* get return type */
4688 sa
= s
->next
; /* first parameter */
4691 /* compute first implicit argument if a structure is returned */
4692 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
4693 variadic
= (s
->c
== FUNC_ELLIPSIS
);
4694 ret_nregs
= gfunc_sret(&s
->type
, variadic
, &ret
.type
,
4695 &ret_align
, ®size
);
4697 /* get some space for the returned structure */
4698 size
= type_size(&s
->type
, &align
);
4699 #ifdef TCC_TARGET_ARM64
4700 /* On arm64, a small struct is return in registers.
4701 It is much easier to write it to memory if we know
4702 that we are allowed to write some extra bytes, so
4703 round the allocated space up to a power of 2: */
4705 while (size
& (size
- 1))
4706 size
= (size
| (size
- 1)) + 1;
4708 loc
= (loc
- size
) & -align
;
4710 ret
.r
= VT_LOCAL
| VT_LVAL
;
4711 /* pass it as 'int' to avoid structure arg passing
4713 vseti(VT_LOCAL
, loc
);
4723 /* return in register */
4724 if (is_float(ret
.type
.t
)) {
4725 ret
.r
= reg_fret(ret
.type
.t
);
4726 #ifdef TCC_TARGET_X86_64
4727 if ((ret
.type
.t
& VT_BTYPE
) == VT_QFLOAT
)
4731 #ifndef TCC_TARGET_ARM64
4732 #ifdef TCC_TARGET_X86_64
4733 if ((ret
.type
.t
& VT_BTYPE
) == VT_QLONG
)
4735 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
4746 gfunc_param_typed(s
, sa
);
4756 tcc_error("too few arguments to function");
4758 if (!nocode_wanted
) {
4759 gfunc_call(nb_args
);
4761 vtop
-= (nb_args
+ 1);
4765 for (r
= ret
.r
+ ret_nregs
+ !ret_nregs
; r
-- > ret
.r
;) {
4766 vsetc(&ret
.type
, r
, &ret
.c
);
4767 vtop
->r2
= ret
.r2
; /* Loop only happens when r2 is VT_CONST */
4770 /* handle packed struct return */
4771 if (((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) && ret_nregs
) {
4774 size
= type_size(&s
->type
, &align
);
4775 /* We're writing whole regs often, make sure there's enough
4776 space. Assume register size is power of 2. */
4777 if (regsize
> align
)
4779 loc
= (loc
- size
) & -align
;
4783 vset(&ret
.type
, VT_LOCAL
| VT_LVAL
, addr
+ offset
);
4787 if (--ret_nregs
== 0)
4791 vset(&s
->type
, VT_LOCAL
| VT_LVAL
, addr
);
4799 ST_FUNC
void expr_prod(void)
4804 while (tok
== '*' || tok
== '/' || tok
== '%') {
4812 ST_FUNC
void expr_sum(void)
4817 while (tok
== '+' || tok
== '-') {
4825 static void expr_shift(void)
4830 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
4838 static void expr_cmp(void)
4843 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
4844 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
4852 static void expr_cmpeq(void)
4857 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
4865 static void expr_and(void)
4868 while (tok
== '&') {
4875 static void expr_xor(void)
4878 while (tok
== '^') {
4885 static void expr_or(void)
4888 while (tok
== '|') {
4895 /* XXX: fix this mess */
4896 static void expr_land_const(void)
4899 while (tok
== TOK_LAND
) {
4905 static void expr_lor_const(void)
4908 while (tok
== TOK_LOR
) {
4915 static void expr_land(void)
4918 if (tok
== TOK_LAND
) {
4921 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4928 int saved_nocode_wanted
= nocode_wanted
;
4930 while (tok
== TOK_LAND
) {
4937 nocode_wanted
= saved_nocode_wanted
;
4938 gen_cast(&int_type
);
4946 if (tok
!= TOK_LAND
) {
4959 static void expr_lor(void)
4962 if (tok
== TOK_LOR
) {
4965 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4972 int saved_nocode_wanted
= nocode_wanted
;
4974 while (tok
== TOK_LOR
) {
4981 nocode_wanted
= saved_nocode_wanted
;
4982 gen_cast(&int_type
);
4990 if (tok
!= TOK_LOR
) {
5003 /* Assuming vtop is a value used in a conditional context
5004 (i.e. compared with zero) return 0 if it's false, 1 if
5005 true and -1 if it can't be statically determined. */
5006 static int condition_3way(void)
5009 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
&&
5010 (!(vtop
->r
& VT_SYM
) ||
5011 !(vtop
->sym
->type
.t
& VT_WEAK
))) {
5013 boolean
.t
= VT_BOOL
;
5022 static void expr_cond(void)
5024 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
, islv
;
5027 CType type
, type1
, type2
;
5032 c
= condition_3way();
5034 int saved_nocode_wanted
= nocode_wanted
;
5036 if (tok
!= ':' || !gnu_ext
) {
5044 nocode_wanted
= saved_nocode_wanted
;
5047 if (tok
!= ':' || !gnu_ext
) {
5051 nocode_wanted
= saved_nocode_wanted
;
5058 /* XXX This doesn't handle nocode_wanted correctly at all.
5059 It unconditionally calls gv/gvtst and friends. That's
5060 the case for many of the expr_ routines. Currently
5061 that should generate only useless code, but depending
5062 on other operand handling this might also generate
5063 pointer derefs for lvalue conversions whose result
5064 is useless, but nevertheless can lead to segfault.
5066 Somewhen we need to overhaul the whole nocode_wanted
5068 if (vtop
!= vstack
) {
5069 /* needed to avoid having different registers saved in
5071 if (is_float(vtop
->type
.t
)) {
5073 #ifdef TCC_TARGET_X86_64
5074 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
5084 if (tok
== ':' && gnu_ext
) {
5092 sv
= *vtop
; /* save value to handle it later */
5093 vtop
--; /* no vpop so that FP stack is not flushed */
5101 bt1
= t1
& VT_BTYPE
;
5103 bt2
= t2
& VT_BTYPE
;
5104 /* cast operands to correct type according to ISOC rules */
5105 if (is_float(bt1
) || is_float(bt2
)) {
5106 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
5107 type
.t
= VT_LDOUBLE
;
5108 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
5113 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
5114 /* cast to biggest op */
5116 /* convert to unsigned if it does not fit in a long long */
5117 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
5118 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
5119 type
.t
|= VT_UNSIGNED
;
5120 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
5121 /* If one is a null ptr constant the result type
5123 if (is_null_pointer (vtop
))
5125 else if (is_null_pointer (&sv
))
5127 /* XXX: test pointer compatibility, C99 has more elaborate
5131 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
5132 /* XXX: test function pointer compatibility */
5133 type
= bt1
== VT_FUNC
? type1
: type2
;
5134 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
5135 /* XXX: test structure compatibility */
5136 type
= bt1
== VT_STRUCT
? type1
: type2
;
5137 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
5138 /* NOTE: as an extension, we accept void on only one side */
5141 /* integer operations */
5143 /* convert to unsigned if it does not fit in an integer */
5144 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
5145 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
5146 type
.t
|= VT_UNSIGNED
;
5148 /* keep structs lvalue by transforming `(expr ? a : b)` to `*(expr ? &a : &b)` so
5149 that `(expr ? a : b).mem` does not error with "lvalue expected" */
5150 islv
= (vtop
->r
& VT_LVAL
) && (sv
.r
& VT_LVAL
) && VT_STRUCT
== (type
.t
& VT_BTYPE
);
5152 /* now we convert second operand */
5155 mk_pointer(&vtop
->type
);
5158 else if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
5161 if (is_float(type
.t
)) {
5163 #ifdef TCC_TARGET_X86_64
5164 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
5168 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
5169 /* for long longs, we use fixed registers to avoid having
5170 to handle a complicated move */
5175 /* this is horrible, but we must also convert first
5179 /* put again first value and cast it */
5183 mk_pointer(&vtop
->type
);
5186 else if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
5189 move_reg(r2
, r1
, type
.t
);
5198 static void expr_eq(void)
5204 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
5205 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
5206 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
5221 ST_FUNC
void gexpr(void)
5232 /* parse an expression and return its type without any side effect. */
5233 static void expr_type(CType
*type
)
5235 int saved_nocode_wanted
;
5237 saved_nocode_wanted
= nocode_wanted
;
5242 nocode_wanted
= saved_nocode_wanted
;
5245 /* parse a unary expression and return its type without any side
5247 static void unary_type(CType
*type
)
5259 /* parse a constant expression and return value in vtop. */
5260 static void expr_const1(void)
5269 /* parse an integer constant and return its value. */
5270 ST_FUNC
int expr_const(void)
5274 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
5275 expect("constant expression");
5281 /* return the label token if current token is a label, otherwise
5283 static int is_label(void)
5287 /* fast test first */
5288 if (tok
< TOK_UIDENT
)
5290 /* no need to save tokc because tok is an identifier */
5297 unget_tok(last_tok
);
5302 static void label_or_decl(int l
)
5306 /* fast test first */
5307 if (tok
>= TOK_UIDENT
)
5309 /* no need to save tokc because tok is an identifier */
5313 unget_tok(last_tok
);
5316 unget_tok(last_tok
);
5321 static int case_cmp(const void *pa
, const void *pb
)
5323 int a
= (*(struct case_t
**) pa
)->v1
;
5324 int b
= (*(struct case_t
**) pb
)->v1
;
5325 return a
< b
? -1 : a
> b
;
5328 static int gcase(struct case_t
**base
, int len
, int case_reg
, int *bsym
)
5340 case_reg
= gv(RC_INT
);
5346 gtst_addr(0, p
->sym
); /* v1 <= x <= v2 */
5347 case_reg
= gv(RC_INT
);
5350 case_reg
= gcase(base
, len
/2, case_reg
, bsym
);
5351 if (cur_switch
->def_sym
)
5352 gjmp_addr(cur_switch
->def_sym
);
5354 *bsym
= gjmp(*bsym
);
5358 base
+= e
; len
-= e
;
5366 if (p
->v1
== p
->v2
) {
5368 gtst_addr(0, p
->sym
);
5372 case_reg
= gv(RC_INT
);
5378 gtst_addr(0, p
->sym
);
5381 case_reg
= gv(RC_INT
);
5387 static void block(int *bsym
, int *csym
, int is_expr
)
5389 int a
, b
, c
, d
, cond
;
5392 /* generate line number info */
5393 if (tcc_state
->do_debug
&&
5394 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
5395 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
5397 last_line_num
= file
->line_num
;
5401 /* default return value is (void) */
5403 vtop
->type
.t
= VT_VOID
;
5406 if (tok
== TOK_IF
) {
5408 int saved_nocode_wanted
= nocode_wanted
;
5413 cond
= condition_3way();
5417 block(bsym
, csym
, 0);
5419 nocode_wanted
= saved_nocode_wanted
;
5421 if (c
== TOK_ELSE
) {
5427 block(bsym
, csym
, 0);
5428 gsym(d
); /* patch else jmp */
5430 nocode_wanted
= saved_nocode_wanted
;
5433 } else if (tok
== TOK_WHILE
) {
5434 int saved_nocode_wanted
;
5435 nocode_wanted
&= ~2;
5445 saved_nocode_wanted
= nocode_wanted
;
5447 nocode_wanted
= saved_nocode_wanted
;
5453 } else if (tok
== '{') {
5455 int block_vla_sp_loc
= vla_sp_loc
, saved_vlas_in_scope
= vlas_in_scope
;
5458 /* record local declaration stack position */
5460 llabel
= local_label_stack
;
5463 /* handle local labels declarations */
5464 if (tok
== TOK_LABEL
) {
5467 if (tok
< TOK_UIDENT
)
5468 expect("label identifier");
5469 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
5479 while (tok
!= '}') {
5480 label_or_decl(VT_LOCAL
);
5484 block(bsym
, csym
, is_expr
);
5487 /* pop locally defined labels */
5488 label_pop(&local_label_stack
, llabel
);
5489 /* pop locally defined symbols */
5491 /* In the is_expr case (a statement expression is finished here),
5492 vtop might refer to symbols on the local_stack. Either via the
5493 type or via vtop->sym. We can't pop those nor any that in turn
5494 might be referred to. To make it easier we don't roll back
5495 any symbols in that case; some upper level call to block() will
5496 do that. We do have to remove such symbols from the lookup
5497 tables, though. sym_pop will do that. */
5498 sym_pop(&local_stack
, s
, is_expr
);
5500 /* Pop VLA frames and restore stack pointer if required */
5501 if (vlas_in_scope
> saved_vlas_in_scope
) {
5502 vla_sp_loc
= saved_vlas_in_scope
? block_vla_sp_loc
: vla_sp_root_loc
;
5505 vlas_in_scope
= saved_vlas_in_scope
;
5508 } else if (tok
== TOK_RETURN
) {
5512 gen_assign_cast(&func_vt
);
5513 #ifdef TCC_TARGET_ARM64
5514 // Perhaps it would be better to use this for all backends:
5517 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
5518 CType type
, ret_type
;
5519 int ret_align
, ret_nregs
, regsize
;
5520 ret_nregs
= gfunc_sret(&func_vt
, func_var
, &ret_type
,
5521 &ret_align
, ®size
);
5522 if (0 == ret_nregs
) {
5523 /* if returning structure, must copy it to implicit
5524 first pointer arg location */
5527 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
5530 /* copy structure value to pointer */
5533 /* returning structure packed into registers */
5534 int r
, size
, addr
, align
;
5535 size
= type_size(&func_vt
,&align
);
5536 if ((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) ||
5537 (vtop
->c
.i
& (ret_align
-1)))
5538 && (align
& (ret_align
-1))) {
5539 loc
= (loc
- size
) & -ret_align
;
5542 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
5546 vset(&ret_type
, VT_LOCAL
| VT_LVAL
, addr
);
5548 vtop
->type
= ret_type
;
5549 if (is_float(ret_type
.t
))
5550 r
= rc_fret(ret_type
.t
);
5561 if (--ret_nregs
== 0)
5563 /* We assume that when a structure is returned in multiple
5564 registers, their classes are consecutive values of the
5567 vtop
->c
.i
+= regsize
;
5571 } else if (is_float(func_vt
.t
)) {
5572 gv(rc_fret(func_vt
.t
));
5577 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
5580 /* jump unless last stmt in top-level block */
5581 if (tok
!= '}' || local_scope
!= 1)
5584 } else if (tok
== TOK_BREAK
) {
5587 tcc_error("cannot break");
5588 *bsym
= gjmp(*bsym
);
5592 } else if (tok
== TOK_CONTINUE
) {
5595 tcc_error("cannot continue");
5596 vla_sp_restore_root();
5597 *csym
= gjmp(*csym
);
5600 } else if (tok
== TOK_FOR
) {
5602 int saved_nocode_wanted
;
5603 nocode_wanted
&= ~2;
5609 /* c99 for-loop init decl? */
5610 if (!decl0(VT_LOCAL
, 1)) {
5611 /* no, regular for-loop init expr */
5637 saved_nocode_wanted
= nocode_wanted
;
5639 nocode_wanted
= saved_nocode_wanted
;
5645 sym_pop(&local_stack
, s
, 0);
5648 if (tok
== TOK_DO
) {
5649 int saved_nocode_wanted
;
5650 nocode_wanted
&= ~2;
5656 saved_nocode_wanted
= nocode_wanted
;
5658 nocode_wanted
= saved_nocode_wanted
;
5670 if (tok
== TOK_SWITCH
) {
5671 struct switch_t
*saved
, sw
;
5672 int saved_nocode_wanted
= nocode_wanted
;
5676 /* XXX: other types than integer */
5681 b
= gjmp(0); /* jump to first case */
5682 sw
.p
= NULL
; sw
.n
= 0; sw
.def_sym
= 0;
5686 nocode_wanted
= saved_nocode_wanted
;
5687 a
= gjmp(a
); /* add implicit break */
5690 qsort(sw
.p
, sw
.n
, sizeof(void*), case_cmp
);
5691 for (b
= 1; b
< sw
.n
; b
++)
5692 if (sw
.p
[b
- 1]->v2
>= sw
.p
[b
]->v1
)
5693 tcc_error("duplicate case value");
5694 gcase(sw
.p
, sw
.n
, c
, &a
);
5696 gjmp_addr(sw
.def_sym
);
5697 dynarray_reset(&sw
.p
, &sw
.n
);
5702 if (tok
== TOK_CASE
) {
5703 struct case_t
*cr
= tcc_malloc(sizeof(struct case_t
));
5706 nocode_wanted
&= ~2;
5708 cr
->v1
= cr
->v2
= expr_const();
5709 if (gnu_ext
&& tok
== TOK_DOTS
) {
5711 cr
->v2
= expr_const();
5712 if (cr
->v2
< cr
->v1
)
5713 tcc_warning("empty case range");
5716 dynarray_add((void***) &cur_switch
->p
, &cur_switch
->n
, cr
);
5719 goto block_after_label
;
5721 if (tok
== TOK_DEFAULT
) {
5726 if (cur_switch
->def_sym
)
5727 tcc_error("too many 'default'");
5728 cur_switch
->def_sym
= ind
;
5730 goto block_after_label
;
5732 if (tok
== TOK_GOTO
) {
5734 if (tok
== '*' && gnu_ext
) {
5738 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
5744 } else if (tok
>= TOK_UIDENT
) {
5745 s
= label_find(tok
);
5746 /* put forward definition if needed */
5748 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
5750 if (s
->r
== LABEL_DECLARED
)
5751 s
->r
= LABEL_FORWARD
;
5753 vla_sp_restore_root();
5756 else if (s
->r
& LABEL_FORWARD
)
5757 s
->jnext
= gjmp(s
->jnext
);
5759 gjmp_addr(s
->jnext
);
5762 expect("label identifier");
5765 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
5773 if (s
->r
== LABEL_DEFINED
)
5774 tcc_error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
5776 s
->r
= LABEL_DEFINED
;
5778 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
5782 /* we accept this, but it is a mistake */
5784 nocode_wanted
&= ~2;
5786 tcc_warning("deprecated use of label at end of compound statement");
5790 block(bsym
, csym
, is_expr
);
5793 /* expression case */
5808 #define EXPR_CONST 1
5811 static void parse_init_elem(int expr_type
)
5813 int saved_global_expr
;
5816 /* compound literals must be allocated globally in this case */
5817 saved_global_expr
= global_expr
;
5820 global_expr
= saved_global_expr
;
5821 /* NOTE: symbols are accepted */
5822 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
5823 tcc_error("initializer element is not constant");
5831 /* t is the array or struct type. c is the array or struct
5832 address. cur_field is the pointer to the current
5833 value, for arrays the 'c' member contains the current start
5834 index and the 'r' contains the end index (in case of range init).
5835 'size_only' is true if only size info is needed (only used
5837 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
5838 Sym
**cur_field
, int size_only
)
5841 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
5847 if (gnu_ext
&& (l
= is_label()) != 0)
5849 while (tok
== '[' || tok
== '.') {
5851 if (!(type
->t
& VT_ARRAY
))
5852 expect("array type");
5855 index
= expr_const();
5856 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
5857 tcc_error("invalid index");
5858 if (tok
== TOK_DOTS
&& gnu_ext
) {
5860 index_last
= expr_const();
5861 if (index_last
< 0 ||
5862 (s
->c
>= 0 && index_last
>= s
->c
) ||
5864 tcc_error("invalid index");
5870 (*cur_field
)->c
= index
;
5871 (*cur_field
)->r
= index_last
;
5873 type
= pointed_type(type
);
5874 elem_size
= type_size(type
, &align
);
5875 c
+= index
* elem_size
;
5876 /* NOTE: we only support ranges for last designator */
5877 nb_elems
= index_last
- index
+ 1;
5878 if (nb_elems
!= 1) {
5887 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
5888 expect("struct/union type");
5889 f
= find_field(type
, l
);
5894 /* XXX: fix this mess by using explicit storage field */
5896 type1
.t
|= (type
->t
& ~VT_TYPE
);
5910 if (type
->t
& VT_ARRAY
) {
5911 index
= (*cur_field
)->c
;
5912 if (type
->ref
->c
>= 0 && index
>= type
->ref
->c
)
5913 tcc_error("index too large");
5914 type
= pointed_type(type
);
5915 c
+= index
* type_size(type
, &align
);
5919 tcc_error("too many field init");
5920 /* XXX: fix this mess by using explicit storage field */
5922 type1
.t
|= (type
->t
& ~VT_TYPE
);
5927 decl_initializer(type
, sec
, c
, 0, size_only
);
5929 /* XXX: make it more general */
5930 if (!size_only
&& nb_elems
> 1) {
5931 unsigned long c_end
;
5936 vset(type
, VT_LOCAL
|VT_LVAL
, c
);
5937 for (i
= 1; i
< nb_elems
; i
++) {
5938 vset(type
, VT_LOCAL
|VT_LVAL
, c
+ elem_size
* i
);
5944 c_end
= c
+ nb_elems
* elem_size
;
5945 if (c_end
> sec
->data_allocated
)
5946 section_realloc(sec
, c_end
);
5947 src
= sec
->data
+ c
;
5949 for(i
= 1; i
< nb_elems
; i
++) {
5951 memcpy(dst
, src
, elem_size
);
5957 /* store a value or an expression directly in global data or in local array */
5958 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
)
5960 int bt
, bit_pos
, bit_size
;
5962 unsigned long long bit_mask
;
5966 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
5970 /* XXX: not portable */
5971 /* XXX: generate error if incorrect relocation */
5972 gen_assign_cast(&dtype
);
5973 bt
= type
->t
& VT_BTYPE
;
5974 size
= type_size(type
, &align
);
5975 if (c
+ size
> sec
->data_allocated
) {
5976 section_realloc(sec
, c
+ size
);
5978 ptr
= sec
->data
+ c
;
5979 /* XXX: make code faster ? */
5980 if (!(type
->t
& VT_BITFIELD
)) {
5982 bit_size
= PTR_SIZE
* 8;
5985 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5986 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
5987 bit_mask
= (1LL << bit_size
) - 1;
5989 if ((vtop
->r
& (VT_SYM
|VT_CONST
)) == (VT_SYM
|VT_CONST
) &&
5990 vtop
->sym
->v
>= SYM_FIRST_ANOM
&&
5991 /* XXX This rejects compount literals like
5992 '(void *){ptr}'. The problem is that '&sym' is
5993 represented the same way, which would be ruled out
5994 by the SYM_FIRST_ANOM check above, but also '"string"'
5995 in 'char *p = "string"' is represented the same
5996 with the type being VT_PTR and the symbol being an
5997 anonymous one. That is, there's no difference in vtop
5998 between '(void *){x}' and '&(void *){x}'. Ignore
5999 pointer typed entities here. Hopefully no real code
6000 will every use compound literals with scalar type. */
6001 (vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
6002 /* These come from compound literals, memcpy stuff over. */
6006 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[vtop
->sym
->c
];
6007 ssec
= tcc_state
->sections
[esym
->st_shndx
];
6008 memmove (ptr
, ssec
->data
+ esym
->st_value
, size
);
6010 /* We need to copy over all memory contents, and that
6011 includes relocations. Use the fact that relocs are
6012 created it order, so look from the end of relocs
6013 until we hit one before the copied region. */
6014 int num_relocs
= ssec
->reloc
->data_offset
/ sizeof(*rel
);
6015 rel
= (ElfW_Rel
*)(ssec
->reloc
->data
+ ssec
->reloc
->data_offset
);
6016 while (num_relocs
--) {
6018 if (rel
->r_offset
>= esym
->st_value
+ size
)
6020 if (rel
->r_offset
< esym
->st_value
)
6022 /* Note: if the same fields are initialized multiple
6023 times (possible with designators) then we possibly
6024 add multiple relocations for the same offset here.
6025 That would lead to wrong code, the last reloc needs
6026 to win. We clean this up later after the whole
6027 initializer is parsed. */
6028 put_elf_reloca(symtab_section
, sec
,
6029 c
+ rel
->r_offset
- esym
->st_value
,
6030 ELFW(R_TYPE
)(rel
->r_info
),
6031 ELFW(R_SYM
)(rel
->r_info
),
6032 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
6041 if ((vtop
->r
& VT_SYM
) &&
6047 (bt
== VT_LLONG
&& bit_size
!= 64) ||
6051 (bt
== VT_INT
&& bit_size
!= 32)
6054 tcc_error("initializer element is not computable at load time");
6056 /* XXX: when cross-compiling we assume that each type has the
6057 same representation on host and target, which is likely to
6058 be wrong in the case of long double */
6060 vtop
->c
.i
= (vtop
->c
.i
!= 0);
6062 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
6065 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
6068 *(double *)ptr
= vtop
->c
.d
;
6071 if (sizeof(long double) == LDOUBLE_SIZE
)
6072 *(long double *)ptr
= vtop
->c
.ld
;
6073 else if (sizeof(double) == LDOUBLE_SIZE
)
6074 *(double *)ptr
= vtop
->c
.ld
;
6076 tcc_error("can't cross compile long double constants");
6080 *(long long *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
6087 addr_t val
= (vtop
->c
.i
& bit_mask
) << bit_pos
;
6088 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
6089 if (vtop
->r
& VT_SYM
)
6090 greloca(sec
, vtop
->sym
, c
, R_DATA_PTR
, val
);
6092 *(addr_t
*)ptr
|= val
;
6094 if (vtop
->r
& VT_SYM
)
6095 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
6096 *(addr_t
*)ptr
|= val
;
6102 int val
= (vtop
->c
.i
& bit_mask
) << bit_pos
;
6103 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
6104 if (vtop
->r
& VT_SYM
)
6105 greloca(sec
, vtop
->sym
, c
, R_DATA_PTR
, val
);
6109 if (vtop
->r
& VT_SYM
)
6110 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
6119 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
6126 /* put zeros for variable based init */
6127 static void init_putz(Section
*sec
, unsigned long c
, int size
)
6130 /* nothing to do because globals are already set to zero */
6132 vpush_global_sym(&func_old_type
, TOK_memset
);
6134 #ifdef TCC_TARGET_ARM
6145 /* 't' contains the type and storage info. 'c' is the offset of the
6146 object in section 'sec'. If 'sec' is NULL, it means stack based
6147 allocation. 'first' is true if array '{' must be read (multi
6148 dimension implicit array init handling). 'size_only' is true if
6149 size only evaluation is wanted (only for arrays). */
6150 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
6151 int first
, int size_only
)
6153 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, parlevel1
, i
;
6160 /* If we currently are at an '}' or ',' we have read an initializer
6161 element in one of our callers, and not yet consumed it. */
6162 have_elem
= tok
== '}' || tok
== ',';
6163 if (!have_elem
&& tok
!= '{' &&
6164 /* In case of strings we have special handling for arrays, so
6165 don't consume them as initializer value (which would commit them
6166 to some anonymous symbol). */
6167 tok
!= TOK_LSTR
&& tok
!= TOK_STR
&&
6169 parse_init_elem(!sec
? EXPR_ANY
: EXPR_CONST
);
6174 !(type
->t
& VT_ARRAY
) &&
6175 /* Use i_c_parameter_t, to strip toplevel qualifiers.
6176 The source type might have VT_CONSTANT set, which is
6177 of course assignable to non-const elements. */
6178 is_compatible_parameter_types(type
, &vtop
->type
)) {
6179 init_putv(type
, sec
, c
);
6180 } else if (type
->t
& VT_ARRAY
) {
6184 t1
= pointed_type(type
);
6185 size1
= type_size(t1
, &align1
);
6188 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
6191 tcc_error("character array initializer must be a literal,"
6192 " optionally enclosed in braces");
6197 /* only parse strings here if correct type (otherwise: handle
6198 them as ((w)char *) expressions */
6199 if ((tok
== TOK_LSTR
&&
6200 #ifdef TCC_TARGET_PE
6201 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
6203 (t1
->t
& VT_BTYPE
) == VT_INT
6205 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
6206 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
6209 /* compute maximum number of chars wanted */
6211 cstr_len
= tokc
.str
.size
;
6213 cstr_len
= tokc
.str
.size
/ sizeof(nwchar_t
);
6216 if (n
>= 0 && nb
> (n
- array_length
))
6217 nb
= n
- array_length
;
6220 tcc_warning("initializer-string for array is too long");
6221 /* in order to go faster for common case (char
6222 string in global variable, we handle it
6224 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
6225 memcpy(sec
->data
+ c
+ array_length
, tokc
.str
.data
, nb
);
6229 ch
= ((unsigned char *)tokc
.str
.data
)[i
];
6231 ch
= ((nwchar_t
*)tokc
.str
.data
)[i
];
6233 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
);
6240 /* only add trailing zero if enough storage (no
6241 warning in this case since it is standard) */
6242 if (n
< 0 || array_length
< n
) {
6245 init_putv(t1
, sec
, c
+ (array_length
* size1
));
6255 while (tok
!= '}' || have_elem
) {
6256 decl_designator(type
, sec
, c
, &f
, size_only
);
6259 /* must put zero in holes (note that doing it that way
6260 ensures that it even works with designators) */
6261 if (!size_only
&& array_length
< index
) {
6262 init_putz(sec
, c
+ array_length
* size1
,
6263 (index
- array_length
) * size1
);
6265 if (type
->t
& VT_ARRAY
) {
6266 index
= indexsym
.c
= ++indexsym
.r
;
6268 index
= index
+ type_size(&f
->type
, &align1
);
6269 if (s
->type
.t
== TOK_UNION
)
6274 if (index
> array_length
)
6275 array_length
= index
;
6277 if (type
->t
& VT_ARRAY
) {
6278 /* special test for multi dimensional arrays (may not
6279 be strictly correct if designators are used at the
6281 if (no_oblock
&& index
>= n
)
6284 if (no_oblock
&& f
== NULL
)
6292 /* put zeros at the end */
6293 if (!size_only
&& array_length
< n
) {
6294 init_putz(sec
, c
+ array_length
* size1
,
6295 (n
- array_length
) * size1
);
6299 /* patch type size if needed, which happens only for array types */
6301 s
->c
= array_length
;
6302 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
6305 if (first
|| tok
== '{') {
6314 } else if (tok
== '{') {
6316 decl_initializer(type
, sec
, c
, first
, size_only
);
6318 } else if (size_only
) {
6319 /* If we supported only ISO C we wouldn't have to accept calling
6320 this on anything than an array size_only==1 (and even then
6321 only on the outermost level, so no recursion would be needed),
6322 because initializing a flex array member isn't supported.
6323 But GNU C supports it, so we need to recurse even into
6324 subfields of structs and arrays when size_only is set. */
6325 /* just skip expression */
6326 parlevel
= parlevel1
= 0;
6327 while ((parlevel
> 0 || parlevel1
> 0 ||
6328 (tok
!= '}' && tok
!= ',')) && tok
!= -1) {
6331 else if (tok
== ')') {
6332 if (parlevel
== 0 && parlevel1
== 0)
6336 else if (tok
== '{')
6338 else if (tok
== '}') {
6339 if (parlevel
== 0 && parlevel1
== 0)
6347 /* This should happen only when we haven't parsed
6348 the init element above for fear of committing a
6349 string constant to memory too early. */
6350 if (tok
!= TOK_STR
&& tok
!= TOK_LSTR
)
6351 expect("string constant");
6352 parse_init_elem(!sec
? EXPR_ANY
: EXPR_CONST
);
6354 init_putv(type
, sec
, c
);
6358 /* parse an initializer for type 't' if 'has_init' is non zero, and
6359 allocate space in local or global data space ('r' is either
6360 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
6361 variable 'v' of scope 'scope' is declared before initializers
6362 are parsed. If 'v' is zero, then a reference to the new object
6363 is put in the value stack. If 'has_init' is 2, a special parsing
6364 is done to handle string constants. */
6365 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
6366 int has_init
, int v
, int scope
)
6368 int size
, align
, addr
, data_offset
;
6370 ParseState saved_parse_state
= {0};
6371 TokenString
*init_str
= NULL
;
6373 Sym
*flexible_array
;
6375 flexible_array
= NULL
;
6376 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
6377 Sym
*field
= type
->ref
->next
;
6380 field
= field
->next
;
6381 if (field
->type
.t
& VT_ARRAY
&& field
->type
.ref
->c
< 0)
6382 flexible_array
= field
;
6386 size
= type_size(type
, &align
);
6387 /* If unknown size, we must evaluate it before
6388 evaluating initializers because
6389 initializers can generate global data too
6390 (e.g. string pointers or ISOC99 compound
6391 literals). It also simplifies local
6392 initializers handling */
6393 if (size
< 0 || (flexible_array
&& has_init
)) {
6395 tcc_error("unknown type size");
6396 /* get all init string */
6397 init_str
= tok_str_alloc();
6398 if (has_init
== 2) {
6399 /* only get strings */
6400 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
6401 tok_str_add_tok(init_str
);
6406 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
6408 tcc_error("unexpected end of file in initializer");
6409 tok_str_add_tok(init_str
);
6412 else if (tok
== '}') {
6422 tok_str_add(init_str
, -1);
6423 tok_str_add(init_str
, 0);
6426 save_parse_state(&saved_parse_state
);
6428 begin_macro(init_str
, 1);
6430 decl_initializer(type
, NULL
, 0, 1, 1);
6431 /* prepare second initializer parsing */
6432 macro_ptr
= init_str
->str
;
6435 /* if still unknown size, error */
6436 size
= type_size(type
, &align
);
6438 tcc_error("unknown type size");
6440 /* If there's a flex member and it was used in the initializer
6442 if (flexible_array
&&
6443 flexible_array
->type
.ref
->c
> 0)
6444 size
+= flexible_array
->type
.ref
->c
6445 * pointed_size(&flexible_array
->type
);
6446 /* take into account specified alignment if bigger */
6447 if (ad
->a
.aligned
) {
6448 if (ad
->a
.aligned
> align
)
6449 align
= ad
->a
.aligned
;
6450 } else if (ad
->a
.packed
) {
6453 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
6455 #ifdef CONFIG_TCC_BCHECK
6456 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
6460 loc
= (loc
- size
) & -align
;
6462 #ifdef CONFIG_TCC_BCHECK
6463 /* handles bounds */
6464 /* XXX: currently, since we do only one pass, we cannot track
6465 '&' operators, so we add only arrays */
6466 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
6468 /* add padding between regions */
6470 /* then add local bound info */
6471 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(addr_t
));
6472 bounds_ptr
[0] = addr
;
6473 bounds_ptr
[1] = size
;
6477 /* local variable */
6478 #ifdef CONFIG_TCC_ASM
6479 if (ad
->asm_label
) {
6480 int reg
= asm_parse_regvar(ad
->asm_label
);
6482 r
= (r
& ~VT_VALMASK
) | reg
;
6485 sym_push(v
, type
, r
, addr
);
6487 /* push local reference */
6488 vset(type
, r
, addr
);
6494 if (v
&& scope
== VT_CONST
) {
6495 /* see if the symbol was already defined */
6498 if (!is_compatible_types(&sym
->type
, type
))
6499 tcc_error("incompatible types for redefinition of '%s'",
6500 get_tok_str(v
, NULL
));
6501 if (sym
->type
.t
& VT_EXTERN
) {
6502 /* if the variable is extern, it was not allocated */
6503 sym
->type
.t
&= ~VT_EXTERN
;
6504 /* set array size if it was omitted in extern
6506 if ((sym
->type
.t
& VT_ARRAY
) &&
6507 sym
->type
.ref
->c
< 0 &&
6509 sym
->type
.ref
->c
= type
->ref
->c
;
6511 /* we accept several definitions of the same
6512 global variable. this is tricky, because we
6513 must play with the SHN_COMMON type of the symbol */
6514 /* XXX: should check if the variable was already
6515 initialized. It is incorrect to initialized it
6517 /* no init data, we won't add more to the symbol */
6524 /* allocate symbol in corresponding section */
6529 else if (tcc_state
->nocommon
)
6533 data_offset
= sec
->data_offset
;
6534 data_offset
= (data_offset
+ align
- 1) & -align
;
6536 /* very important to increment global pointer at this time
6537 because initializers themselves can create new initializers */
6538 data_offset
+= size
;
6539 #ifdef CONFIG_TCC_BCHECK
6540 /* add padding if bound check */
6541 if (tcc_state
->do_bounds_check
)
6544 sec
->data_offset
= data_offset
;
6545 /* allocate section space to put the data */
6546 if (sec
->sh_type
!= SHT_NOBITS
&&
6547 data_offset
> sec
->data_allocated
)
6548 section_realloc(sec
, data_offset
);
6549 /* align section if needed */
6550 if (align
> sec
->sh_addralign
)
6551 sec
->sh_addralign
= align
;
6553 addr
= 0; /* avoid warning */
6557 if (scope
!= VT_CONST
|| !sym
) {
6558 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
6559 sym
->asm_label
= ad
->asm_label
;
6561 /* update symbol definition */
6563 put_extern_sym(sym
, sec
, addr
, size
);
6566 /* put a common area */
6567 put_extern_sym(sym
, NULL
, align
, size
);
6568 /* XXX: find a nicer way */
6569 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
6570 esym
->st_shndx
= SHN_COMMON
;
6573 /* push global reference */
6574 sym
= get_sym_ref(type
, sec
, addr
, size
);
6575 vpushsym(type
, sym
);
6577 /* patch symbol weakness */
6578 if (type
->t
& VT_WEAK
)
6580 apply_visibility(sym
, type
);
6581 #ifdef CONFIG_TCC_BCHECK
6582 /* handles bounds now because the symbol must be defined
6583 before for the relocation */
6584 if (tcc_state
->do_bounds_check
) {
6587 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
6588 /* then add global bound info */
6589 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(addr_t
));
6590 bounds_ptr
[0] = 0; /* relocated */
6591 bounds_ptr
[1] = size
;
6595 if (type
->t
& VT_VLA
) {
6598 /* save current stack pointer */
6599 if (vlas_in_scope
== 0) {
6600 if (vla_sp_root_loc
== -1)
6601 vla_sp_root_loc
= (loc
-= PTR_SIZE
);
6602 gen_vla_sp_save(vla_sp_root_loc
);
6605 vla_runtime_type_size(type
, &a
);
6606 gen_vla_alloc(type
, a
);
6607 gen_vla_sp_save(addr
);
6610 } else if (has_init
) {
6611 size_t oldreloc_offset
= 0;
6612 if (sec
&& sec
->reloc
)
6613 oldreloc_offset
= sec
->reloc
->data_offset
;
6614 decl_initializer(type
, sec
, addr
, 1, 0);
6615 if (sec
&& sec
->reloc
)
6616 squeeze_multi_relocs(sec
, oldreloc_offset
);
6617 /* patch flexible array member size back to -1, */
6618 /* for possible subsequent similar declarations */
6620 flexible_array
->type
.ref
->c
= -1;
6623 /* restore parse state if needed */
6626 restore_parse_state(&saved_parse_state
);
6630 static void put_func_debug(Sym
*sym
)
6635 /* XXX: we put here a dummy type */
6636 snprintf(buf
, sizeof(buf
), "%s:%c1",
6637 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
6638 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
6639 cur_text_section
, sym
->c
);
6640 /* //gr gdb wants a line at the function */
6641 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
6646 /* parse an old style function declaration list */
6647 /* XXX: check multiple parameter */
6648 static void func_decl_list(Sym
*func_sym
)
6655 /* parse each declaration */
6656 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
&&
6657 tok
!= TOK_ASM1
&& tok
!= TOK_ASM2
&& tok
!= TOK_ASM3
) {
6658 if (!parse_btype(&btype
, &ad
))
6659 expect("declaration list");
6660 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
6661 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
6663 /* we accept no variable after */
6667 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
6668 /* find parameter in function parameter list */
6671 if ((s
->v
& ~SYM_FIELD
) == v
)
6675 tcc_error("declaration for parameter '%s' but no such parameter",
6676 get_tok_str(v
, NULL
));
6678 /* check that no storage specifier except 'register' was given */
6679 if (type
.t
& VT_STORAGE
)
6680 tcc_error("storage class specified for '%s'", get_tok_str(v
, NULL
));
6681 convert_parameter_type(&type
);
6682 /* we can add the type (NOTE: it could be local to the function) */
6684 /* accept other parameters */
6695 /* parse a function defined by symbol 'sym' and generate its code in
6696 'cur_text_section' */
6697 static void gen_function(Sym
*sym
)
6699 int saved_nocode_wanted
= nocode_wanted
;
6702 ind
= cur_text_section
->data_offset
;
6703 /* NOTE: we patch the symbol size later */
6704 put_extern_sym(sym
, cur_text_section
, ind
, 0);
6705 funcname
= get_tok_str(sym
->v
, NULL
);
6707 /* Initialize VLA state */
6709 vla_sp_root_loc
= -1;
6710 /* put debug symbol */
6711 if (tcc_state
->do_debug
)
6712 put_func_debug(sym
);
6714 /* push a dummy symbol to enable local sym storage */
6715 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
6716 local_scope
= 1; /* for function parameters */
6717 gfunc_prolog(&sym
->type
);
6721 block(NULL
, NULL
, 0);
6724 cur_text_section
->data_offset
= ind
;
6725 label_pop(&global_label_stack
, NULL
);
6726 /* reset local stack */
6728 sym_pop(&local_stack
, NULL
, 0);
6729 /* end of function */
6730 /* patch symbol size */
6731 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
6733 /* patch symbol weakness (this definition overrules any prototype) */
6734 if (sym
->type
.t
& VT_WEAK
)
6736 apply_visibility(sym
, &sym
->type
);
6737 if (tcc_state
->do_debug
) {
6738 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
6740 /* It's better to crash than to generate wrong code */
6741 cur_text_section
= NULL
;
6742 funcname
= ""; /* for safety */
6743 func_vt
.t
= VT_VOID
; /* for safety */
6744 func_var
= 0; /* for safety */
6745 ind
= 0; /* for safety */
6746 nocode_wanted
= saved_nocode_wanted
;
6750 static void gen_inline_functions(TCCState
*s
)
6753 int inline_generated
, i
, ln
;
6754 struct InlineFunc
*fn
;
6756 ln
= file
->line_num
;
6757 /* iterate while inline function are referenced */
6759 inline_generated
= 0;
6760 for (i
= 0; i
< s
->nb_inline_fns
; ++i
) {
6761 fn
= s
->inline_fns
[i
];
6763 if (sym
&& sym
->c
) {
6764 /* the function was used: generate its code and
6765 convert it to a normal function */
6768 pstrcpy(file
->filename
, sizeof file
->filename
, fn
->filename
);
6769 sym
->r
= VT_SYM
| VT_CONST
;
6770 sym
->type
.t
&= ~VT_INLINE
;
6772 begin_macro(fn
->func_str
, 1);
6774 cur_text_section
= text_section
;
6778 inline_generated
= 1;
6781 if (!inline_generated
)
6784 file
->line_num
= ln
;
6787 ST_FUNC
void free_inline_functions(TCCState
*s
)
6790 /* free tokens of unused inline functions */
6791 for (i
= 0; i
< s
->nb_inline_fns
; ++i
) {
6792 struct InlineFunc
*fn
= s
->inline_fns
[i
];
6794 tok_str_free(fn
->func_str
);
6796 dynarray_reset(&s
->inline_fns
, &s
->nb_inline_fns
);
6799 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
6800 static int decl0(int l
, int is_for_loop_init
)
6808 if (!parse_btype(&btype
, &ad
)) {
6809 if (is_for_loop_init
)
6811 /* skip redundant ';' */
6812 /* XXX: find more elegant solution */
6817 if (l
== VT_CONST
&&
6818 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
6819 /* global asm block */
6823 /* special test for old K&R protos without explicit int
6824 type. Only accepted when defining global data */
6825 if (l
== VT_LOCAL
|| tok
< TOK_UIDENT
)
6829 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
6830 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
6832 if ((btype
.t
& VT_BTYPE
) == VT_STRUCT
) {
6833 int v
= btype
.ref
->v
;
6834 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) >= SYM_FIRST_ANOM
)
6835 tcc_warning("unnamed struct/union that defines no instances");
6840 while (1) { /* iterate thru each declaration */
6842 /* If the base type itself was an array type of unspecified
6843 size (like in 'typedef int arr[]; arr x = {1};') then
6844 we will overwrite the unknown size by the real one for
6845 this decl. We need to unshare the ref symbol holding
6847 if ((type
.t
& VT_ARRAY
) && type
.ref
->c
< 0) {
6848 type
.ref
= sym_push(SYM_FIELD
, &type
.ref
->type
, 0, type
.ref
->c
);
6850 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
6854 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
6855 printf("type = '%s'\n", buf
);
6858 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
6859 if ((type
.t
& VT_STATIC
) && (l
== VT_LOCAL
)) {
6860 tcc_error("function without file scope cannot be static");
6862 /* if old style function prototype, we accept a
6865 if (sym
->c
== FUNC_OLD
)
6866 func_decl_list(sym
);
6869 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
6870 ad
.asm_label
= asm_label_instr();
6871 /* parse one last attribute list, after asm label */
6872 parse_attribute(&ad
);
6879 #ifdef TCC_TARGET_PE
6880 if (ad
.a
.func_import
)
6881 type
.t
|= VT_IMPORT
;
6882 if (ad
.a
.func_export
)
6883 type
.t
|= VT_EXPORT
;
6885 type
.t
|= ad
.a
.visibility
<< VT_VIS_SHIFT
;
6889 tcc_error("cannot use local functions");
6890 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
6891 expect("function definition");
6893 /* reject abstract declarators in function definition */
6895 while ((sym
= sym
->next
) != NULL
)
6896 if (!(sym
->v
& ~SYM_FIELD
))
6897 expect("identifier");
6899 /* XXX: cannot do better now: convert extern line to static inline */
6900 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
6901 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
6906 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
6909 ref
= sym
->type
.ref
;
6910 if (0 == ref
->a
.func_proto
)
6911 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
6913 /* use func_call from prototype if not defined */
6914 if (ref
->a
.func_call
!= FUNC_CDECL
6915 && type
.ref
->a
.func_call
== FUNC_CDECL
)
6916 type
.ref
->a
.func_call
= ref
->a
.func_call
;
6918 /* use export from prototype */
6919 if (ref
->a
.func_export
)
6920 type
.ref
->a
.func_export
= 1;
6922 /* use static from prototype */
6923 if (sym
->type
.t
& VT_STATIC
)
6924 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
6926 /* If the definition has no visibility use the
6927 one from prototype. */
6928 if (! (type
.t
& VT_VIS_MASK
))
6929 type
.t
|= sym
->type
.t
& VT_VIS_MASK
;
6931 if (!is_compatible_types(&sym
->type
, &type
)) {
6933 tcc_error("incompatible types for redefinition of '%s'",
6934 get_tok_str(v
, NULL
));
6936 type
.ref
->a
.func_proto
= 0;
6937 /* if symbol is already defined, then put complete type */
6940 /* put function symbol */
6941 sym
= global_identifier_push(v
, type
.t
, 0);
6942 sym
->type
.ref
= type
.ref
;
6945 /* static inline functions are just recorded as a kind
6946 of macro. Their code will be emitted at the end of
6947 the compilation unit only if they are used */
6948 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
6949 (VT_INLINE
| VT_STATIC
)) {
6951 struct InlineFunc
*fn
;
6952 const char *filename
;
6954 filename
= file
? file
->filename
: "";
6955 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
6956 strcpy(fn
->filename
, filename
);
6958 fn
->func_str
= tok_str_alloc();
6964 tcc_error("unexpected end of file");
6965 tok_str_add_tok(fn
->func_str
);
6970 } else if (t
== '}') {
6972 if (block_level
== 0)
6976 tok_str_add(fn
->func_str
, -1);
6977 tok_str_add(fn
->func_str
, 0);
6978 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
6981 /* compute text section */
6982 cur_text_section
= ad
.section
;
6983 if (!cur_text_section
)
6984 cur_text_section
= text_section
;
6985 sym
->r
= VT_SYM
| VT_CONST
;
6990 if (btype
.t
& VT_TYPEDEF
) {
6991 /* save typedefed type */
6992 /* XXX: test storage specifiers ? */
6994 if (sym
&& sym
->scope
== local_scope
) {
6995 if (!is_compatible_types(&sym
->type
, &type
)
6996 || !(sym
->type
.t
& VT_TYPEDEF
))
6997 tcc_error("incompatible redefinition of '%s'",
6998 get_tok_str(v
, NULL
));
7001 sym
= sym_push(v
, &type
, 0, 0);
7004 sym
->type
.t
|= VT_TYPEDEF
;
7007 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
7008 /* external function definition */
7009 /* specific case for func_call attribute */
7010 ad
.a
.func_proto
= 1;
7012 } else if (!(type
.t
& VT_ARRAY
)) {
7013 /* not lvalue if array */
7014 r
|= lvalue_type(type
.t
);
7016 has_init
= (tok
== '=');
7017 if (has_init
&& (type
.t
& VT_VLA
))
7018 tcc_error("variable length array cannot be initialized");
7019 if ((btype
.t
& VT_EXTERN
) || ((type
.t
& VT_BTYPE
) == VT_FUNC
) ||
7020 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
7021 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
7022 /* external variable or function */
7023 /* NOTE: as GCC, uninitialized global static
7024 arrays of null size are considered as
7026 sym
= external_sym(v
, &type
, r
);
7027 sym
->asm_label
= ad
.asm_label
;
7029 if (ad
.alias_target
) {
7034 alias_target
= sym_find(ad
.alias_target
);
7035 if (!alias_target
|| !alias_target
->c
)
7036 tcc_error("unsupported forward __alias__ attribute");
7037 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[alias_target
->c
];
7038 tsec
.sh_num
= esym
->st_shndx
;
7039 put_extern_sym2(sym
, &tsec
, esym
->st_value
, esym
->st_size
, 0);
7042 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
7043 if (type
.t
& VT_STATIC
)
7049 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, l
);
7053 if (is_for_loop_init
)
7066 ST_FUNC
void decl(int l
)
7071 /* ------------------------------------------------------------------------- */