2 * TCC - Tiny C Compiler
4 * Copyright (c) 2001-2004 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 /********************************************************/
24 /* global variables */
26 /* loc : local variable index
27 ind : output code index
29 anon_sym: anonymous symbol index
31 ST_DATA
int rsym
, anon_sym
, ind
, loc
;
33 ST_DATA Sym
*sym_free_first
;
34 ST_DATA
void **sym_pools
;
35 ST_DATA
int nb_sym_pools
;
37 ST_DATA Sym
*global_stack
;
38 ST_DATA Sym
*local_stack
;
39 ST_DATA Sym
*define_stack
;
40 ST_DATA Sym
*global_label_stack
;
41 ST_DATA Sym
*local_label_stack
;
42 static int local_scope
;
44 static int section_sym
;
46 ST_DATA
int vlas_in_scope
; /* number of VLAs that are currently in scope */
47 ST_DATA
int vla_sp_root_loc
; /* vla_sp_loc for SP before any VLAs were pushed */
48 ST_DATA
int vla_sp_loc
; /* Pointer to variable holding location to store stack pointer on the stack when modifying stack pointer */
50 ST_DATA SValue __vstack
[1+VSTACK_SIZE
], *vtop
, *pvtop
;
52 ST_DATA
int const_wanted
; /* true if constant wanted */
53 ST_DATA
int nocode_wanted
; /* true if no code generation wanted for an expression */
54 ST_DATA
int global_expr
; /* true if compound literals must be allocated globally (used during initializers parsing */
55 ST_DATA CType func_vt
; /* current function return type (used by return instruction) */
56 ST_DATA
int func_var
; /* true if current function is variadic (used by return instruction) */
58 ST_DATA
int last_line_num
, last_ind
, func_ind
; /* debug last line number and pc */
59 ST_DATA
const char *funcname
;
61 ST_DATA CType char_pointer_type
, func_old_type
, int_type
, size_type
;
63 ST_DATA
struct switch_t
{
66 } **p
; int n
; /* list of case ranges */
67 int def_sym
; /* default symbol */
68 } *cur_switch
; /* current switch */
70 /* ------------------------------------------------------------------------- */
71 static void gen_cast(CType
*type
);
72 static inline CType
*pointed_type(CType
*type
);
73 static int is_compatible_types(CType
*type1
, CType
*type2
);
74 static int parse_btype(CType
*type
, AttributeDef
*ad
);
75 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
);
76 static void parse_expr_type(CType
*type
);
77 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
, int first
, int size_only
);
78 static void block(int *bsym
, int *csym
, int is_expr
);
79 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
, int has_init
, int v
, int scope
);
80 static int decl0(int l
, int is_for_loop_init
);
81 static void expr_eq(void);
82 static void expr_lor_const(void);
83 static void unary_type(CType
*type
);
84 static void vla_runtime_type_size(CType
*type
, int *a
);
85 static void vla_sp_restore(void);
86 static void vla_sp_restore_root(void);
87 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
);
88 static void expr_type(CType
*type
);
89 ST_FUNC
void vpush64(int ty
, unsigned long long v
);
90 ST_FUNC
void vpush(CType
*type
);
91 ST_FUNC
int gvtst(int inv
, int t
);
92 ST_FUNC
int is_btype_size(int bt
);
93 static void gen_inline_functions(TCCState
*s
);
95 ST_INLN
int is_float(int t
)
99 return bt
== VT_LDOUBLE
|| bt
== VT_DOUBLE
|| bt
== VT_FLOAT
|| bt
== VT_QFLOAT
;
102 /* we use our own 'finite' function to avoid potential problems with
103 non standard math libs */
104 /* XXX: endianness dependent */
105 ST_FUNC
int ieee_finite(double d
)
108 memcpy(p
, &d
, sizeof(double));
109 return ((unsigned)((p
[1] | 0x800fffff) + 1)) >> 31;
112 ST_FUNC
void test_lvalue(void)
114 if (!(vtop
->r
& VT_LVAL
))
118 ST_FUNC
void check_vstack(void)
121 tcc_error("internal compiler error: vstack leak (%d)", vtop
- pvtop
);
124 /* ------------------------------------------------------------------------- */
125 /* vstack debugging aid */
128 void pv (const char *lbl
, int a
, int b
)
131 for (i
= a
; i
< a
+ b
; ++i
) {
132 SValue
*p
= &vtop
[-i
];
133 printf("%s vtop[-%d] : type.t:%04x r:%04x r2:%04x c.i:%d\n",
134 lbl
, i
, p
->type
.t
, p
->r
, p
->r2
, (int)p
->c
.i
);
139 /* ------------------------------------------------------------------------- */
140 ST_FUNC
void tccgen_start(TCCState
*s1
)
142 cur_text_section
= NULL
;
144 anon_sym
= SYM_FIRST_ANOM
;
148 /* define some often used types */
150 char_pointer_type
.t
= VT_BYTE
;
151 mk_pointer(&char_pointer_type
);
153 size_type
.t
= VT_INT
;
155 size_type
.t
= VT_LLONG
;
157 func_old_type
.t
= VT_FUNC
;
158 func_old_type
.ref
= sym_push(SYM_FIELD
, &int_type
, FUNC_CDECL
, FUNC_OLD
);
163 /* file info: full path + filename */
164 section_sym
= put_elf_sym(symtab_section
, 0, 0,
165 ELFW(ST_INFO
)(STB_LOCAL
, STT_SECTION
), 0,
166 text_section
->sh_num
, NULL
);
167 getcwd(buf
, sizeof(buf
));
169 normalize_slashes(buf
);
171 pstrcat(buf
, sizeof(buf
), "/");
172 put_stabs_r(buf
, N_SO
, 0, 0,
173 text_section
->data_offset
, text_section
, section_sym
);
174 put_stabs_r(file
->filename
, N_SO
, 0, 0,
175 text_section
->data_offset
, text_section
, section_sym
);
177 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
178 symbols can be safely used */
179 put_elf_sym(symtab_section
, 0, 0,
180 ELFW(ST_INFO
)(STB_LOCAL
, STT_FILE
), 0,
181 SHN_ABS
, file
->filename
);
183 #ifdef TCC_TARGET_ARM
188 ST_FUNC
void tccgen_end(TCCState
*s1
)
190 gen_inline_functions(s1
);
192 /* end of translation unit info */
194 put_stabs_r(NULL
, N_SO
, 0, 0,
195 text_section
->data_offset
, text_section
, section_sym
);
199 /* ------------------------------------------------------------------------- */
200 /* update sym->c so that it points to an external symbol in section
201 'section' with value 'value' */
203 ST_FUNC
void put_extern_sym2(Sym
*sym
, Section
*section
,
204 addr_t value
, unsigned long size
,
205 int can_add_underscore
)
207 int sym_type
, sym_bind
, sh_num
, info
, other
;
212 #ifdef CONFIG_TCC_BCHECK
218 else if (section
== SECTION_ABS
)
221 sh_num
= section
->sh_num
;
223 if ((sym
->type
.t
& VT_BTYPE
) == VT_FUNC
) {
225 } else if ((sym
->type
.t
& VT_BTYPE
) == VT_VOID
) {
226 sym_type
= STT_NOTYPE
;
228 sym_type
= STT_OBJECT
;
231 if (sym
->type
.t
& VT_STATIC
)
232 sym_bind
= STB_LOCAL
;
234 if (sym
->type
.t
& VT_WEAK
)
237 sym_bind
= STB_GLOBAL
;
241 name
= get_tok_str(sym
->v
, NULL
);
242 #ifdef CONFIG_TCC_BCHECK
243 if (tcc_state
->do_bounds_check
) {
244 /* XXX: avoid doing that for statics ? */
245 /* if bound checking is activated, we change some function
246 names by adding the "__bound" prefix */
249 /* XXX: we rely only on malloc hooks */
262 strcpy(buf
, "__bound_");
272 if (sym
->type
.t
& VT_EXPORT
)
273 other
|= ST_PE_EXPORT
;
274 if (sym_type
== STT_FUNC
&& sym
->type
.ref
) {
275 Sym
*ref
= sym
->type
.ref
;
276 if (ref
->a
.func_export
)
277 other
|= ST_PE_EXPORT
;
278 if (ref
->a
.func_call
== FUNC_STDCALL
&& can_add_underscore
) {
279 sprintf(buf1
, "_%s@%d", name
, ref
->a
.func_args
* PTR_SIZE
);
281 other
|= ST_PE_STDCALL
;
282 can_add_underscore
= 0;
285 if (find_elf_sym(tcc_state
->dynsymtab_section
, name
))
286 other
|= ST_PE_IMPORT
;
287 if (sym
->type
.t
& VT_IMPORT
)
288 other
|= ST_PE_IMPORT
;
291 if (! (sym
->type
.t
& VT_STATIC
))
292 other
= (sym
->type
.t
& VT_VIS_MASK
) >> VT_VIS_SHIFT
;
294 if (tcc_state
->leading_underscore
&& can_add_underscore
) {
296 pstrcpy(buf1
+ 1, sizeof(buf1
) - 1, name
);
299 if (sym
->asm_label
) {
300 name
= get_tok_str(sym
->asm_label
, NULL
);
302 info
= ELFW(ST_INFO
)(sym_bind
, sym_type
);
303 sym
->c
= set_elf_sym(symtab_section
, value
, size
, info
, other
, sh_num
, name
);
305 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
306 esym
->st_value
= value
;
307 esym
->st_size
= size
;
308 esym
->st_shndx
= sh_num
;
312 ST_FUNC
void put_extern_sym(Sym
*sym
, Section
*section
,
313 addr_t value
, unsigned long size
)
315 put_extern_sym2(sym
, section
, value
, size
, 1);
318 /* add a new relocation entry to symbol 'sym' in section 's' */
319 ST_FUNC
void greloca(Section
*s
, Sym
*sym
, unsigned long offset
, int type
,
325 put_extern_sym(sym
, NULL
, 0, 0);
328 /* now we can add ELF relocation info */
329 put_elf_reloca(symtab_section
, s
, offset
, type
, c
, addend
);
332 ST_FUNC
void greloc(Section
*s
, Sym
*sym
, unsigned long offset
, int type
)
334 greloca(s
, sym
, offset
, type
, 0);
337 /* ------------------------------------------------------------------------- */
338 /* symbol allocator */
339 static Sym
*__sym_malloc(void)
341 Sym
*sym_pool
, *sym
, *last_sym
;
344 sym_pool
= tcc_malloc(SYM_POOL_NB
* sizeof(Sym
));
345 dynarray_add(&sym_pools
, &nb_sym_pools
, sym_pool
);
347 last_sym
= sym_free_first
;
349 for(i
= 0; i
< SYM_POOL_NB
; i
++) {
350 sym
->next
= last_sym
;
354 sym_free_first
= last_sym
;
358 static inline Sym
*sym_malloc(void)
362 sym
= sym_free_first
;
364 sym
= __sym_malloc();
365 sym_free_first
= sym
->next
;
368 sym
= tcc_malloc(sizeof(Sym
));
373 ST_INLN
void sym_free(Sym
*sym
)
376 sym
->next
= sym_free_first
;
377 sym_free_first
= sym
;
383 /* push, without hashing */
384 ST_FUNC Sym
*sym_push2(Sym
**ps
, int v
, int t
, long c
)
404 /* find a symbol and return its associated structure. 's' is the top
405 of the symbol stack */
406 ST_FUNC Sym
*sym_find2(Sym
*s
, int v
)
418 /* structure lookup */
419 ST_INLN Sym
*struct_find(int v
)
422 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
424 return table_ident
[v
]->sym_struct
;
427 /* find an identifier */
428 ST_INLN Sym
*sym_find(int v
)
431 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
433 return table_ident
[v
]->sym_identifier
;
436 /* push a given symbol on the symbol stack */
437 ST_FUNC Sym
*sym_push(int v
, CType
*type
, int r
, int c
)
446 s
= sym_push2(ps
, v
, type
->t
, c
);
447 s
->type
.ref
= type
->ref
;
449 /* don't record fields or anonymous symbols */
451 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
452 /* record symbol in token array */
453 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
455 ps
= &ts
->sym_struct
;
457 ps
= &ts
->sym_identifier
;
460 s
->scope
= local_scope
;
461 if (s
->prev_tok
&& s
->prev_tok
->scope
== s
->scope
)
462 tcc_error("redeclaration of '%s'",
463 get_tok_str(v
& ~SYM_STRUCT
, NULL
));
468 /* push a global identifier */
469 ST_FUNC Sym
*global_identifier_push(int v
, int t
, int c
)
472 s
= sym_push2(&global_stack
, v
, t
, c
);
473 /* don't record anonymous symbol */
474 if (v
< SYM_FIRST_ANOM
) {
475 ps
= &table_ident
[v
- TOK_IDENT
]->sym_identifier
;
476 /* modify the top most local identifier, so that
477 sym_identifier will point to 's' when popped */
479 ps
= &(*ps
)->prev_tok
;
486 /* pop symbols until top reaches 'b'. If KEEP is non-zero don't really
487 pop them yet from the list, but do remove them from the token array. */
488 ST_FUNC
void sym_pop(Sym
**ptop
, Sym
*b
, int keep
)
498 /* remove symbol in token array */
500 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
501 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
503 ps
= &ts
->sym_struct
;
505 ps
= &ts
->sym_identifier
;
516 static void weaken_symbol(Sym
*sym
)
518 sym
->type
.t
|= VT_WEAK
;
523 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
524 esym_type
= ELFW(ST_TYPE
)(esym
->st_info
);
525 esym
->st_info
= ELFW(ST_INFO
)(STB_WEAK
, esym_type
);
529 static void apply_visibility(Sym
*sym
, CType
*type
)
531 int vis
= sym
->type
.t
& VT_VIS_MASK
;
532 int vis2
= type
->t
& VT_VIS_MASK
;
533 if (vis
== (STV_DEFAULT
<< VT_VIS_SHIFT
))
535 else if (vis2
== (STV_DEFAULT
<< VT_VIS_SHIFT
))
538 vis
= (vis
< vis2
) ? vis
: vis2
;
539 sym
->type
.t
&= ~VT_VIS_MASK
;
545 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
546 vis
>>= VT_VIS_SHIFT
;
547 esym
->st_other
= (esym
->st_other
& ~ELFW(ST_VISIBILITY
)(-1)) | vis
;
551 /* ------------------------------------------------------------------------- */
553 ST_FUNC
void swap(int *p
, int *q
)
561 static void vsetc(CType
*type
, int r
, CValue
*vc
)
565 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
566 tcc_error("memory full (vstack)");
567 /* cannot let cpu flags if other instruction are generated. Also
568 avoid leaving VT_JMP anywhere except on the top of the stack
569 because it would complicate the code generator. */
570 if (vtop
>= vstack
) {
571 v
= vtop
->r
& VT_VALMASK
;
572 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
582 /* push constant of type "type" with useless value */
583 ST_FUNC
void vpush(CType
*type
)
586 vsetc(type
, VT_CONST
, &cval
);
589 /* push integer constant */
590 ST_FUNC
void vpushi(int v
)
594 vsetc(&int_type
, VT_CONST
, &cval
);
597 /* push a pointer sized constant */
598 static void vpushs(addr_t v
)
602 vsetc(&size_type
, VT_CONST
, &cval
);
605 /* push arbitrary 64bit constant */
606 ST_FUNC
void vpush64(int ty
, unsigned long long v
)
613 vsetc(&ctype
, VT_CONST
, &cval
);
616 /* push long long constant */
617 static inline void vpushll(long long v
)
619 vpush64(VT_LLONG
, v
);
622 /* push a symbol value of TYPE */
623 static inline void vpushsym(CType
*type
, Sym
*sym
)
627 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
631 /* Return a static symbol pointing to a section */
632 ST_FUNC Sym
*get_sym_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
638 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
639 sym
->type
.ref
= type
->ref
;
640 sym
->r
= VT_CONST
| VT_SYM
;
641 put_extern_sym(sym
, sec
, offset
, size
);
645 /* push a reference to a section offset by adding a dummy symbol */
646 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
648 vpushsym(type
, get_sym_ref(type
, sec
, offset
, size
));
651 /* define a new external reference to a symbol 'v' of type 'u' */
652 ST_FUNC Sym
*external_global_sym(int v
, CType
*type
, int r
)
658 /* push forward reference */
659 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
660 s
->type
.ref
= type
->ref
;
661 s
->r
= r
| VT_CONST
| VT_SYM
;
666 /* define a new external reference to a symbol 'v' */
667 static Sym
*external_sym(int v
, CType
*type
, int r
)
673 /* push forward reference */
674 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
675 s
->type
.t
|= VT_EXTERN
;
676 } else if (s
->type
.ref
== func_old_type
.ref
) {
677 s
->type
.ref
= type
->ref
;
678 s
->r
= r
| VT_CONST
| VT_SYM
;
679 s
->type
.t
|= VT_EXTERN
;
680 } else if (!is_compatible_types(&s
->type
, type
)) {
681 tcc_error("incompatible types for redefinition of '%s'",
682 get_tok_str(v
, NULL
));
684 /* Merge some storage attributes. */
685 if (type
->t
& VT_WEAK
)
688 if (type
->t
& VT_VIS_MASK
)
689 apply_visibility(s
, type
);
694 /* push a reference to global symbol v */
695 ST_FUNC
void vpush_global_sym(CType
*type
, int v
)
697 vpushsym(type
, external_global_sym(v
, type
, 0));
700 ST_FUNC
void vset(CType
*type
, int r
, int v
)
705 vsetc(type
, r
, &cval
);
708 static void vseti(int r
, int v
)
716 ST_FUNC
void vswap(void)
719 /* cannot let cpu flags if other instruction are generated. Also
720 avoid leaving VT_JMP anywhere except on the top of the stack
721 because it would complicate the code generator. */
722 if (vtop
>= vstack
) {
723 int v
= vtop
->r
& VT_VALMASK
;
724 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
731 /* XXX: +2% overall speed possible with optimized memswap
733 * memswap(&vtop[0], &vtop[1], sizeof *vtop);
737 ST_FUNC
void vpushv(SValue
*v
)
739 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
740 tcc_error("memory full (vstack)");
745 static void vdup(void)
750 /* save registers up to (vtop - n) stack entry */
751 ST_FUNC
void save_regs(int n
)
754 for(p
= vstack
, p1
= vtop
- n
; p
<= p1
; p
++)
758 /* save r to the memory stack, and mark it as being free */
759 ST_FUNC
void save_reg(int r
)
761 save_reg_upstack(r
, 0);
764 /* save r to the memory stack, and mark it as being free,
765 if seen up to (vtop - n) stack entry */
766 ST_FUNC
void save_reg_upstack(int r
, int n
)
768 int l
, saved
, size
, align
;
772 if ((r
&= VT_VALMASK
) >= VT_CONST
)
775 /* modify all stack values */
778 for(p
= vstack
, p1
= vtop
- n
; p
<= p1
; p
++) {
779 if ((p
->r
& VT_VALMASK
) == r
||
780 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
781 /* must save value on stack if not already done */
783 /* NOTE: must reload 'r' because r might be equal to r2 */
784 r
= p
->r
& VT_VALMASK
;
785 /* store register in the stack */
787 if ((p
->r
& VT_LVAL
) ||
788 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
789 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
790 type
= &char_pointer_type
;
794 size
= type_size(type
, &align
);
795 loc
= (loc
- size
) & -align
;
797 sv
.r
= VT_LOCAL
| VT_LVAL
;
800 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
801 /* x86 specific: need to pop fp register ST0 if saved */
803 o(0xd8dd); /* fstp %st(0) */
806 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
807 /* special long long case */
808 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
816 /* mark that stack entry as being saved on the stack */
817 if (p
->r
& VT_LVAL
) {
818 /* also clear the bounded flag because the
819 relocation address of the function was stored in
821 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
823 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
831 #ifdef TCC_TARGET_ARM
832 /* find a register of class 'rc2' with at most one reference on stack.
833 * If none, call get_reg(rc) */
834 ST_FUNC
int get_reg_ex(int rc
, int rc2
)
839 for(r
=0;r
<NB_REGS
;r
++) {
840 if (reg_classes
[r
] & rc2
) {
843 for(p
= vstack
; p
<= vtop
; p
++) {
844 if ((p
->r
& VT_VALMASK
) == r
||
845 (p
->r2
& VT_VALMASK
) == r
)
856 /* find a free register of class 'rc'. If none, save one register */
857 ST_FUNC
int get_reg(int rc
)
862 /* find a free register */
863 for(r
=0;r
<NB_REGS
;r
++) {
864 if (reg_classes
[r
] & rc
) {
865 for(p
=vstack
;p
<=vtop
;p
++) {
866 if ((p
->r
& VT_VALMASK
) == r
||
867 (p
->r2
& VT_VALMASK
) == r
)
875 /* no register left : free the first one on the stack (VERY
876 IMPORTANT to start from the bottom to ensure that we don't
877 spill registers used in gen_opi()) */
878 for(p
=vstack
;p
<=vtop
;p
++) {
879 /* look at second register (if long long) */
880 r
= p
->r2
& VT_VALMASK
;
881 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
883 r
= p
->r
& VT_VALMASK
;
884 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
890 /* Should never comes here */
894 /* move register 's' (of type 't') to 'r', and flush previous value of r to memory
896 static void move_reg(int r
, int s
, int t
)
910 /* get address of vtop (vtop MUST BE an lvalue) */
911 ST_FUNC
void gaddrof(void)
913 if (vtop
->r
& VT_REF
&& !nocode_wanted
)
916 /* tricky: if saved lvalue, then we can go back to lvalue */
917 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
918 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
923 #ifdef CONFIG_TCC_BCHECK
924 /* generate lvalue bound code */
925 static void gbound(void)
930 vtop
->r
&= ~VT_MUSTBOUND
;
931 /* if lvalue, then use checking code before dereferencing */
932 if (vtop
->r
& VT_LVAL
) {
933 /* if not VT_BOUNDED value, then make one */
934 if (!(vtop
->r
& VT_BOUNDED
)) {
935 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
936 /* must save type because we must set it to int to get pointer */
938 vtop
->type
.t
= VT_PTR
;
941 gen_bounded_ptr_add();
942 vtop
->r
|= lval_type
;
945 /* then check for dereferencing */
946 gen_bounded_ptr_deref();
951 /* store vtop a register belonging to class 'rc'. lvalues are
952 converted to values. Cannot be used if cannot be converted to
953 register value (such as structures). */
954 ST_FUNC
int gv(int rc
)
956 int r
, bit_pos
, bit_size
, size
, align
, i
;
959 /* NOTE: get_reg can modify vstack[] */
960 if (vtop
->type
.t
& VT_BITFIELD
) {
963 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
964 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
965 /* remove bit field info to avoid loops */
966 vtop
->type
.t
&= ~VT_BITFIELD
& ((1 << VT_STRUCT_SHIFT
) - 1);
967 /* cast to int to propagate signedness in following ops */
968 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
973 if((vtop
->type
.t
& VT_UNSIGNED
) ||
974 (vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
975 type
.t
|= VT_UNSIGNED
;
977 /* generate shifts */
978 vpushi(bits
- (bit_pos
+ bit_size
));
980 vpushi(bits
- bit_size
);
981 /* NOTE: transformed to SHR if unsigned */
985 if (is_float(vtop
->type
.t
) &&
986 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
989 unsigned long offset
;
990 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
994 /* XXX: unify with initializers handling ? */
995 /* CPUs usually cannot use float constants, so we store them
996 generically in data segment */
997 size
= type_size(&vtop
->type
, &align
);
998 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
999 data_section
->data_offset
= offset
;
1000 /* XXX: not portable yet */
1001 #if defined(__i386__) || defined(__x86_64__)
1002 /* Zero pad x87 tenbyte long doubles */
1003 if (size
== LDOUBLE_SIZE
) {
1004 vtop
->c
.tab
[2] &= 0xffff;
1005 #if LDOUBLE_SIZE == 16
1010 ptr
= section_ptr_add(data_section
, size
);
1012 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
1016 ptr
[i
] = vtop
->c
.tab
[size
-1-i
];
1020 ptr
[i
] = vtop
->c
.tab
[i
];
1021 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
1022 vtop
->r
|= VT_LVAL
| VT_SYM
;
1026 #ifdef CONFIG_TCC_BCHECK
1027 if (vtop
->r
& VT_MUSTBOUND
)
1031 r
= vtop
->r
& VT_VALMASK
;
1032 rc2
= (rc
& RC_FLOAT
) ? RC_FLOAT
: RC_INT
;
1033 #ifndef TCC_TARGET_ARM64
1036 #ifdef TCC_TARGET_X86_64
1037 else if (rc
== RC_FRET
)
1042 /* need to reload if:
1044 - lvalue (need to dereference pointer)
1045 - already a register, but not in the right class */
1047 || (vtop
->r
& VT_LVAL
)
1048 || !(reg_classes
[r
] & rc
)
1049 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1050 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
1051 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
&& !(reg_classes
[vtop
->r2
] & rc2
))
1053 || ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
1058 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1059 if (((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
)) {
1060 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
1062 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
1063 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
1064 unsigned long long ll
;
1066 int r2
, original_type
;
1067 original_type
= vtop
->type
.t
;
1068 /* two register type load : expand to two words
1070 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
1071 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
1074 vtop
->c
.i
= ll
; /* first word */
1076 vtop
->r
= r
; /* save register value */
1077 vpushi(ll
>> 32); /* second word */
1080 if (vtop
->r
& VT_LVAL
) {
1081 /* We do not want to modifier the long long
1082 pointer here, so the safest (and less
1083 efficient) is to save all the other registers
1084 in the stack. XXX: totally inefficient. */
1088 /* lvalue_save: save only if used further down the stack */
1089 save_reg_upstack(vtop
->r
, 1);
1091 /* load from memory */
1092 vtop
->type
.t
= load_type
;
1095 vtop
[-1].r
= r
; /* save register value */
1096 /* increment pointer to get second word */
1097 vtop
->type
.t
= addr_type
;
1102 vtop
->type
.t
= load_type
;
1104 /* move registers */
1107 vtop
[-1].r
= r
; /* save register value */
1108 vtop
->r
= vtop
[-1].r2
;
1110 /* Allocate second register. Here we rely on the fact that
1111 get_reg() tries first to free r2 of an SValue. */
1115 /* write second register */
1117 vtop
->type
.t
= original_type
;
1118 } else if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
1120 /* lvalue of scalar type : need to use lvalue type
1121 because of possible cast */
1124 /* compute memory access type */
1125 if (vtop
->r
& VT_REF
)
1126 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1131 else if (vtop
->r
& VT_LVAL_BYTE
)
1133 else if (vtop
->r
& VT_LVAL_SHORT
)
1135 if (vtop
->r
& VT_LVAL_UNSIGNED
)
1139 /* restore wanted type */
1142 /* one register type load */
1147 #ifdef TCC_TARGET_C67
1148 /* uses register pairs for doubles */
1149 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
1156 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
1157 ST_FUNC
void gv2(int rc1
, int rc2
)
1161 /* generate more generic register first. But VT_JMP or VT_CMP
1162 values must be generated first in all cases to avoid possible
1164 v
= vtop
[0].r
& VT_VALMASK
;
1165 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
1170 /* test if reload is needed for first register */
1171 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
1181 /* test if reload is needed for first register */
1182 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
1188 #ifndef TCC_TARGET_ARM64
1189 /* wrapper around RC_FRET to return a register by type */
1190 static int rc_fret(int t
)
1192 #ifdef TCC_TARGET_X86_64
1193 if (t
== VT_LDOUBLE
) {
1201 /* wrapper around REG_FRET to return a register by type */
1202 static int reg_fret(int t
)
1204 #ifdef TCC_TARGET_X86_64
1205 if (t
== VT_LDOUBLE
) {
1212 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
1213 /* expand 64bit on stack in two ints */
1214 static void lexpand(void)
1217 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
1218 v
= vtop
->r
& (VT_VALMASK
| VT_LVAL
);
1219 if (v
== VT_CONST
) {
1222 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
1228 vtop
[0].r
= vtop
[-1].r2
;
1229 vtop
[0].r2
= vtop
[-1].r2
= VT_CONST
;
1231 vtop
[0].type
.t
= vtop
[-1].type
.t
= VT_INT
| u
;
1235 #ifdef TCC_TARGET_ARM
1236 /* expand long long on stack */
1237 ST_FUNC
void lexpand_nr(void)
1241 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
1243 vtop
->r2
= VT_CONST
;
1244 vtop
->type
.t
= VT_INT
| u
;
1245 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
1246 if (v
== VT_CONST
) {
1247 vtop
[-1].c
.i
= vtop
->c
.i
;
1248 vtop
->c
.i
= vtop
->c
.i
>> 32;
1250 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
1252 vtop
->r
= vtop
[-1].r
;
1253 } else if (v
> VT_CONST
) {
1257 vtop
->r
= vtop
[-1].r2
;
1258 vtop
[-1].r2
= VT_CONST
;
1259 vtop
[-1].type
.t
= VT_INT
| u
;
1263 #if !defined(TCC_TARGET_X86_64) && !defined(TCC_TARGET_ARM64)
1264 /* build a long long from two ints */
1265 static void lbuild(int t
)
1267 gv2(RC_INT
, RC_INT
);
1268 vtop
[-1].r2
= vtop
[0].r
;
1269 vtop
[-1].type
.t
= t
;
1274 /* rotate n first stack elements to the bottom
1275 I1 ... In -> I2 ... In I1 [top is right]
1277 ST_FUNC
void vrotb(int n
)
1283 for(i
=-n
+1;i
!=0;i
++)
1284 vtop
[i
] = vtop
[i
+1];
1288 /* rotate the n elements before entry e towards the top
1289 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
1291 ST_FUNC
void vrote(SValue
*e
, int n
)
1297 for(i
= 0;i
< n
- 1; i
++)
1302 /* rotate n first stack elements to the top
1303 I1 ... In -> In I1 ... I(n-1) [top is right]
1305 ST_FUNC
void vrott(int n
)
1310 /* pop stack value */
1311 ST_FUNC
void vpop(void)
1314 v
= vtop
->r
& VT_VALMASK
;
1315 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1316 /* for x86, we need to pop the FP stack */
1317 if (v
== TREG_ST0
&& !nocode_wanted
) {
1318 o(0xd8dd); /* fstp %st(0) */
1321 if (v
== VT_JMP
|| v
== VT_JMPI
) {
1322 /* need to put correct jump if && or || without test */
1328 /* convert stack entry to register and duplicate its value in another
1330 static void gv_dup(void)
1336 #if !defined(TCC_TARGET_X86_64) && !defined(TCC_TARGET_ARM64)
1337 if ((t
& VT_BTYPE
) == VT_LLONG
) {
1344 /* stack: H L L1 H1 */
1354 /* duplicate value */
1359 #ifdef TCC_TARGET_X86_64
1360 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1370 load(r1
, &sv
); /* move r to r1 */
1372 /* duplicates value */
1378 /* Generate value test
1380 * Generate a test for any value (jump, comparison and integers) */
1381 ST_FUNC
int gvtst(int inv
, int t
)
1383 int v
= vtop
->r
& VT_VALMASK
;
1384 if (v
!= VT_CMP
&& v
!= VT_JMP
&& v
!= VT_JMPI
) {
1388 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1389 /* constant jmp optimization */
1390 if ((vtop
->c
.i
!= 0) != inv
)
1395 return gtst(inv
, t
);
1398 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
1399 /* generate CPU independent (unsigned) long long operations */
1400 static void gen_opl(int op
)
1402 int t
, a
, b
, op1
, c
, i
;
1404 unsigned short reg_iret
= REG_IRET
;
1405 unsigned short reg_lret
= REG_LRET
;
1411 func
= TOK___divdi3
;
1414 func
= TOK___udivdi3
;
1417 func
= TOK___moddi3
;
1420 func
= TOK___umoddi3
;
1427 /* call generic long long function */
1428 vpush_global_sym(&func_old_type
, func
);
1433 vtop
->r2
= reg_lret
;
1441 //pv("gen_opl A",0,2);
1447 /* stack: L1 H1 L2 H2 */
1452 vtop
[-2] = vtop
[-3];
1455 /* stack: H1 H2 L1 L2 */
1456 //pv("gen_opl B",0,4);
1462 /* stack: H1 H2 L1 L2 ML MH */
1465 /* stack: ML MH H1 H2 L1 L2 */
1469 /* stack: ML MH H1 L2 H2 L1 */
1474 /* stack: ML MH M1 M2 */
1477 } else if (op
== '+' || op
== '-') {
1478 /* XXX: add non carry method too (for MIPS or alpha) */
1484 /* stack: H1 H2 (L1 op L2) */
1487 gen_op(op1
+ 1); /* TOK_xxxC2 */
1490 /* stack: H1 H2 (L1 op L2) */
1493 /* stack: (L1 op L2) H1 H2 */
1495 /* stack: (L1 op L2) (H1 op H2) */
1503 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1504 t
= vtop
[-1].type
.t
;
1508 /* stack: L H shift */
1510 /* constant: simpler */
1511 /* NOTE: all comments are for SHL. the other cases are
1512 done by swaping words */
1523 if (op
!= TOK_SAR
) {
1556 /* XXX: should provide a faster fallback on x86 ? */
1559 func
= TOK___ashrdi3
;
1562 func
= TOK___lshrdi3
;
1565 func
= TOK___ashldi3
;
1571 /* compare operations */
1577 /* stack: L1 H1 L2 H2 */
1579 vtop
[-1] = vtop
[-2];
1581 /* stack: L1 L2 H1 H2 */
1584 /* when values are equal, we need to compare low words. since
1585 the jump is inverted, we invert the test too. */
1588 else if (op1
== TOK_GT
)
1590 else if (op1
== TOK_ULT
)
1592 else if (op1
== TOK_UGT
)
1597 if (op1
!= TOK_NE
) {
1601 /* generate non equal test */
1602 /* XXX: NOT PORTABLE yet */
1606 #if defined(TCC_TARGET_I386)
1607 b
= psym(0x850f, 0);
1608 #elif defined(TCC_TARGET_ARM)
1610 o(0x1A000000 | encbranch(ind
, 0, 1));
1611 #elif defined(TCC_TARGET_C67) || defined(TCC_TARGET_ARM64)
1612 tcc_error("not implemented");
1614 #error not supported
1618 /* compare low. Always unsigned */
1622 else if (op1
== TOK_LE
)
1624 else if (op1
== TOK_GT
)
1626 else if (op1
== TOK_GE
)
1637 static uint64_t gen_opic_sdiv(uint64_t a
, uint64_t b
)
1639 uint64_t x
= (a
>> 63 ? -a
: a
) / (b
>> 63 ? -b
: b
);
1640 return (a
^ b
) >> 63 ? -x
: x
;
1643 static int gen_opic_lt(uint64_t a
, uint64_t b
)
1645 return (a
^ (uint64_t)1 << 63) < (b
^ (uint64_t)1 << 63);
1648 /* handle integer constant optimizations and various machine
1650 static void gen_opic(int op
)
1652 SValue
*v1
= vtop
- 1;
1654 int t1
= v1
->type
.t
& VT_BTYPE
;
1655 int t2
= v2
->type
.t
& VT_BTYPE
;
1656 int c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1657 int c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1658 uint64_t l1
= c1
? v1
->c
.i
: 0;
1659 uint64_t l2
= c2
? v2
->c
.i
: 0;
1660 int shm
= (t1
== VT_LLONG
) ? 63 : 31;
1663 l1
= ((uint32_t)l1
|
1664 (v1
->type
.t
& VT_UNSIGNED
? 0 : -(l1
& 0x80000000)));
1666 l2
= ((uint32_t)l2
|
1667 (v2
->type
.t
& VT_UNSIGNED
? 0 : -(l2
& 0x80000000)));
1671 case '+': l1
+= l2
; break;
1672 case '-': l1
-= l2
; break;
1673 case '&': l1
&= l2
; break;
1674 case '^': l1
^= l2
; break;
1675 case '|': l1
|= l2
; break;
1676 case '*': l1
*= l2
; break;
1683 /* if division by zero, generate explicit division */
1686 tcc_error("division by zero in constant");
1690 default: l1
= gen_opic_sdiv(l1
, l2
); break;
1691 case '%': l1
= l1
- l2
* gen_opic_sdiv(l1
, l2
); break;
1692 case TOK_UDIV
: l1
= l1
/ l2
; break;
1693 case TOK_UMOD
: l1
= l1
% l2
; break;
1696 case TOK_SHL
: l1
<<= (l2
& shm
); break;
1697 case TOK_SHR
: l1
>>= (l2
& shm
); break;
1699 l1
= (l1
>> 63) ? ~(~l1
>> (l2
& shm
)) : l1
>> (l2
& shm
);
1702 case TOK_ULT
: l1
= l1
< l2
; break;
1703 case TOK_UGE
: l1
= l1
>= l2
; break;
1704 case TOK_EQ
: l1
= l1
== l2
; break;
1705 case TOK_NE
: l1
= l1
!= l2
; break;
1706 case TOK_ULE
: l1
= l1
<= l2
; break;
1707 case TOK_UGT
: l1
= l1
> l2
; break;
1708 case TOK_LT
: l1
= gen_opic_lt(l1
, l2
); break;
1709 case TOK_GE
: l1
= !gen_opic_lt(l1
, l2
); break;
1710 case TOK_LE
: l1
= !gen_opic_lt(l2
, l1
); break;
1711 case TOK_GT
: l1
= gen_opic_lt(l2
, l1
); break;
1713 case TOK_LAND
: l1
= l1
&& l2
; break;
1714 case TOK_LOR
: l1
= l1
|| l2
; break;
1721 /* if commutative ops, put c2 as constant */
1722 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1723 op
== '|' || op
== '*')) {
1725 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1726 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1728 if (!const_wanted
&&
1730 (op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
)) ||
1731 (l1
== -1 && op
== TOK_SAR
))) {
1732 /* treat (0 << x), (0 >> x) and (-1 >> x) as constant */
1734 } else if (!const_wanted
&&
1735 c2
&& ((l2
== 0 && (op
== '&' || op
== '*')) ||
1736 (l2
== -1 && op
== '|') ||
1737 (l2
== 0xffffffff && t2
!= VT_LLONG
&& op
== '|') ||
1738 (l2
== 1 && (op
== '%' || op
== TOK_UMOD
)))) {
1739 /* treat (x & 0), (x * 0), (x | -1) and (x % 1) as constant */
1744 } else if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1747 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1748 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1752 /* filter out NOP operations like x*1, x-0, x&-1... */
1754 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1755 /* try to use shifts instead of muls or divs */
1756 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1765 else if (op
== TOK_PDIV
)
1771 } else if (c2
&& (op
== '+' || op
== '-') &&
1772 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
1773 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1774 /* symbol + constant case */
1781 if (!nocode_wanted
) {
1782 /* call low level op generator */
1783 if (t1
== VT_LLONG
|| t2
== VT_LLONG
||
1784 (PTR_SIZE
== 8 && (t1
== VT_PTR
|| t2
== VT_PTR
)))
1795 /* generate a floating point operation with constant propagation */
1796 static void gen_opif(int op
)
1804 /* currently, we cannot do computations with forward symbols */
1805 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1806 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1808 if (v1
->type
.t
== VT_FLOAT
) {
1811 } else if (v1
->type
.t
== VT_DOUBLE
) {
1819 /* NOTE: we only do constant propagation if finite number (not
1820 NaN or infinity) (ANSI spec) */
1821 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1825 case '+': f1
+= f2
; break;
1826 case '-': f1
-= f2
; break;
1827 case '*': f1
*= f2
; break;
1831 tcc_error("division by zero in constant");
1836 /* XXX: also handles tests ? */
1840 /* XXX: overflow test ? */
1841 if (v1
->type
.t
== VT_FLOAT
) {
1843 } else if (v1
->type
.t
== VT_DOUBLE
) {
1851 if (!nocode_wanted
) {
1859 static int pointed_size(CType
*type
)
1862 return type_size(pointed_type(type
), &align
);
1865 static void vla_runtime_pointed_size(CType
*type
)
1868 vla_runtime_type_size(pointed_type(type
), &align
);
1871 static inline int is_null_pointer(SValue
*p
)
1873 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1875 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& (uint32_t)p
->c
.i
== 0) ||
1876 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.i
== 0) ||
1877 ((p
->type
.t
& VT_BTYPE
) == VT_PTR
&&
1878 (PTR_SIZE
== 4 ? (uint32_t)p
->c
.i
== 0 : p
->c
.i
== 0));
1881 static inline int is_integer_btype(int bt
)
1883 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
1884 bt
== VT_INT
|| bt
== VT_LLONG
);
1887 /* check types for comparison or subtraction of pointers */
1888 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
1890 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
1893 /* null pointers are accepted for all comparisons as gcc */
1894 if (is_null_pointer(p1
) || is_null_pointer(p2
))
1898 bt1
= type1
->t
& VT_BTYPE
;
1899 bt2
= type2
->t
& VT_BTYPE
;
1900 /* accept comparison between pointer and integer with a warning */
1901 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
1902 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
1903 tcc_warning("comparison between pointer and integer");
1907 /* both must be pointers or implicit function pointers */
1908 if (bt1
== VT_PTR
) {
1909 type1
= pointed_type(type1
);
1910 } else if (bt1
!= VT_FUNC
)
1911 goto invalid_operands
;
1913 if (bt2
== VT_PTR
) {
1914 type2
= pointed_type(type2
);
1915 } else if (bt2
!= VT_FUNC
) {
1917 tcc_error("invalid operands to binary %s", get_tok_str(op
, NULL
));
1919 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1920 (type2
->t
& VT_BTYPE
) == VT_VOID
)
1924 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1925 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1926 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1927 /* gcc-like error if '-' is used */
1929 goto invalid_operands
;
1931 tcc_warning("comparison of distinct pointer types lacks a cast");
1935 /* generic gen_op: handles types problems */
1936 ST_FUNC
void gen_op(int op
)
1938 int u
, t1
, t2
, bt1
, bt2
, t
;
1942 t1
= vtop
[-1].type
.t
;
1943 t2
= vtop
[0].type
.t
;
1944 bt1
= t1
& VT_BTYPE
;
1945 bt2
= t2
& VT_BTYPE
;
1947 if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
1948 tcc_error("operation on a struct");
1949 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
1950 if (bt2
== VT_FUNC
) {
1951 mk_pointer(&vtop
->type
);
1954 if (bt1
== VT_FUNC
) {
1956 mk_pointer(&vtop
->type
);
1961 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
1962 /* at least one operand is a pointer */
1963 /* relationnal op: must be both pointers */
1964 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
1965 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1966 /* pointers are handled are unsigned */
1967 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1968 t
= VT_LLONG
| VT_UNSIGNED
;
1970 t
= VT_INT
| VT_UNSIGNED
;
1974 /* if both pointers, then it must be the '-' op */
1975 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
1977 tcc_error("cannot use pointers here");
1978 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1979 /* XXX: check that types are compatible */
1980 if (vtop
[-1].type
.t
& VT_VLA
) {
1981 vla_runtime_pointed_size(&vtop
[-1].type
);
1983 vpushi(pointed_size(&vtop
[-1].type
));
1987 /* set to integer type */
1988 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1989 vtop
->type
.t
= VT_LLONG
;
1991 vtop
->type
.t
= VT_INT
;
1996 /* exactly one pointer : must be '+' or '-'. */
1997 if (op
!= '-' && op
!= '+')
1998 tcc_error("cannot use pointers here");
1999 /* Put pointer as first operand */
2000 if (bt2
== VT_PTR
) {
2005 if ((vtop
[0].type
.t
& VT_BTYPE
) == VT_LLONG
)
2006 /* XXX: truncate here because gen_opl can't handle ptr + long long */
2007 gen_cast(&int_type
);
2009 type1
= vtop
[-1].type
;
2010 type1
.t
&= ~VT_ARRAY
;
2011 if (vtop
[-1].type
.t
& VT_VLA
)
2012 vla_runtime_pointed_size(&vtop
[-1].type
);
2014 u
= pointed_size(&vtop
[-1].type
);
2016 tcc_error("unknown array element size");
2017 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2020 /* XXX: cast to int ? (long long case) */
2026 /* #ifdef CONFIG_TCC_BCHECK
2027 The main reason to removing this code:
2034 fprintf(stderr, "v+i-j = %p\n", v+i-j);
2035 fprintf(stderr, "v+(i-j) = %p\n", v+(i-j));
2037 When this code is on. then the output looks like
2039 v+(i-j) = 0xbff84000
2041 /* if evaluating constant expression, no code should be
2042 generated, so no bound check */
2043 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
2044 /* if bounded pointers, we generate a special code to
2051 gen_bounded_ptr_add();
2057 /* put again type if gen_opic() swaped operands */
2060 } else if (is_float(bt1
) || is_float(bt2
)) {
2061 /* compute bigger type and do implicit casts */
2062 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
2064 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
2069 /* floats can only be used for a few operations */
2070 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
2071 (op
< TOK_ULT
|| op
> TOK_GT
))
2072 tcc_error("invalid operands for binary operation");
2074 } else if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
) {
2075 t
= bt1
== VT_LLONG
? VT_LLONG
: VT_INT
;
2076 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (t
| VT_UNSIGNED
))
2079 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
2080 /* cast to biggest op */
2082 /* convert to unsigned if it does not fit in a long long */
2083 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
2084 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
2088 /* integer operations */
2090 /* convert to unsigned if it does not fit in an integer */
2091 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
2092 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
2095 /* XXX: currently, some unsigned operations are explicit, so
2096 we modify them here */
2097 if (t
& VT_UNSIGNED
) {
2104 else if (op
== TOK_LT
)
2106 else if (op
== TOK_GT
)
2108 else if (op
== TOK_LE
)
2110 else if (op
== TOK_GE
)
2117 /* special case for shifts and long long: we keep the shift as
2119 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
2126 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
2127 /* relationnal op: the result is an int */
2128 vtop
->type
.t
= VT_INT
;
2133 // Make sure that we have converted to an rvalue:
2134 if (vtop
->r
& VT_LVAL
&& !nocode_wanted
)
2135 gv(is_float(vtop
->type
.t
& VT_BTYPE
) ? RC_FLOAT
: RC_INT
);
2138 #ifndef TCC_TARGET_ARM
2139 /* generic itof for unsigned long long case */
2140 static void gen_cvt_itof1(int t
)
2142 #ifdef TCC_TARGET_ARM64
2145 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
2146 (VT_LLONG
| VT_UNSIGNED
)) {
2149 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
2150 #if LDOUBLE_SIZE != 8
2151 else if (t
== VT_LDOUBLE
)
2152 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
2155 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
2159 vtop
->r
= reg_fret(t
);
2167 /* generic ftoi for unsigned long long case */
2168 static void gen_cvt_ftoi1(int t
)
2170 #ifdef TCC_TARGET_ARM64
2175 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
2176 /* not handled natively */
2177 st
= vtop
->type
.t
& VT_BTYPE
;
2179 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
2180 #if LDOUBLE_SIZE != 8
2181 else if (st
== VT_LDOUBLE
)
2182 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
2185 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
2190 vtop
->r2
= REG_LRET
;
2197 /* force char or short cast */
2198 static void force_charshort_cast(int t
)
2202 /* XXX: add optimization if lvalue : just change type and offset */
2207 if (t
& VT_UNSIGNED
) {
2208 vpushi((1 << bits
) - 1);
2211 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
)
2217 /* result must be signed or the SAR is converted to an SHL
2218 This was not the case when "t" was a signed short
2219 and the last value on the stack was an unsigned int */
2220 vtop
->type
.t
&= ~VT_UNSIGNED
;
2226 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
2227 static void gen_cast(CType
*type
)
2229 int sbt
, dbt
, sf
, df
, c
, p
;
2231 /* special delayed cast for char/short */
2232 /* XXX: in some cases (multiple cascaded casts), it may still
2234 if (vtop
->r
& VT_MUSTCAST
) {
2235 vtop
->r
&= ~VT_MUSTCAST
;
2236 force_charshort_cast(vtop
->type
.t
);
2239 /* bitfields first get cast to ints */
2240 if (vtop
->type
.t
& VT_BITFIELD
&& !nocode_wanted
) {
2244 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
2245 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
2250 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
2251 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
2253 /* constant case: we can do it now */
2254 /* XXX: in ISOC, cannot do it if error in convert */
2255 if (sbt
== VT_FLOAT
)
2256 vtop
->c
.ld
= vtop
->c
.f
;
2257 else if (sbt
== VT_DOUBLE
)
2258 vtop
->c
.ld
= vtop
->c
.d
;
2261 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
2262 if ((sbt
& VT_UNSIGNED
) || !(vtop
->c
.i
>> 63))
2263 vtop
->c
.ld
= vtop
->c
.i
;
2265 vtop
->c
.ld
= -(long double)-vtop
->c
.i
;
2267 if ((sbt
& VT_UNSIGNED
) || !(vtop
->c
.i
>> 31))
2268 vtop
->c
.ld
= (uint32_t)vtop
->c
.i
;
2270 vtop
->c
.ld
= -(long double)-(uint32_t)vtop
->c
.i
;
2273 if (dbt
== VT_FLOAT
)
2274 vtop
->c
.f
= (float)vtop
->c
.ld
;
2275 else if (dbt
== VT_DOUBLE
)
2276 vtop
->c
.d
= (double)vtop
->c
.ld
;
2277 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
2278 vtop
->c
.i
= vtop
->c
.ld
;
2279 } else if (sf
&& dbt
== VT_BOOL
) {
2280 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
2283 vtop
->c
.i
= vtop
->c
.ld
;
2284 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
2286 else if (sbt
& VT_UNSIGNED
)
2287 vtop
->c
.i
= (uint32_t)vtop
->c
.i
;
2288 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2289 else if (sbt
== VT_PTR
)
2292 else if (sbt
!= VT_LLONG
)
2293 vtop
->c
.i
= ((uint32_t)vtop
->c
.i
|
2294 -(vtop
->c
.i
& 0x80000000));
2296 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
2298 else if (dbt
== VT_BOOL
)
2299 vtop
->c
.i
= (vtop
->c
.i
!= 0);
2300 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2301 else if (dbt
== VT_PTR
)
2304 else if (dbt
!= VT_LLONG
) {
2305 uint32_t m
= ((dbt
& VT_BTYPE
) == VT_BYTE
? 0xff :
2306 (dbt
& VT_BTYPE
) == VT_SHORT
? 0xffff :
2309 if (!(dbt
& VT_UNSIGNED
))
2310 vtop
->c
.i
|= -(vtop
->c
.i
& ((m
>> 1) + 1));
2313 } else if (p
&& dbt
== VT_BOOL
) {
2316 } else if (!nocode_wanted
) {
2317 /* non constant case: generate code */
2319 /* convert from fp to fp */
2322 /* convert int to fp */
2325 /* convert fp to int */
2326 if (dbt
== VT_BOOL
) {
2330 /* we handle char/short/etc... with generic code */
2331 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
2332 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
2336 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
2337 /* additional cast for char/short... */
2342 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
2343 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
2344 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
2345 /* scalar to long long */
2346 /* machine independent conversion */
2348 /* generate high word */
2349 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
2353 if (sbt
== VT_PTR
) {
2354 /* cast from pointer to int before we apply
2355 shift operation, which pointers don't support*/
2356 gen_cast(&int_type
);
2362 /* patch second register */
2363 vtop
[-1].r2
= vtop
->r
;
2367 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
2368 (dbt
& VT_BTYPE
) == VT_PTR
||
2369 (dbt
& VT_BTYPE
) == VT_FUNC
) {
2370 if ((sbt
& VT_BTYPE
) != VT_LLONG
&&
2371 (sbt
& VT_BTYPE
) != VT_PTR
&&
2372 (sbt
& VT_BTYPE
) != VT_FUNC
) {
2373 /* need to convert from 32bit to 64bit */
2375 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
2376 #if defined(TCC_TARGET_ARM64)
2378 #elif defined(TCC_TARGET_X86_64)
2380 /* x86_64 specific: movslq */
2382 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
2389 } else if (dbt
== VT_BOOL
) {
2390 /* scalar to bool */
2393 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
2394 (dbt
& VT_BTYPE
) == VT_SHORT
) {
2395 if (sbt
== VT_PTR
) {
2396 vtop
->type
.t
= VT_INT
;
2397 tcc_warning("nonportable conversion from pointer to char/short");
2399 force_charshort_cast(dbt
);
2400 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
2401 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
2403 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
2404 /* from long long: just take low order word */
2408 /* if lvalue and single word type, nothing to do because
2409 the lvalue already contains the real type size (see
2410 VT_LVAL_xxx constants) */
2414 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
2415 /* if we are casting between pointer types,
2416 we must update the VT_LVAL_xxx size */
2417 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
2418 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
2423 /* return type size as known at compile time. Put alignment at 'a' */
2424 ST_FUNC
int type_size(CType
*type
, int *a
)
2429 bt
= type
->t
& VT_BTYPE
;
2430 if (bt
== VT_STRUCT
) {
2435 } else if (bt
== VT_PTR
) {
2436 if (type
->t
& VT_ARRAY
) {
2440 ts
= type_size(&s
->type
, a
);
2442 if (ts
< 0 && s
->c
< 0)
2450 } else if (bt
== VT_LDOUBLE
) {
2452 return LDOUBLE_SIZE
;
2453 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
2454 #ifdef TCC_TARGET_I386
2455 #ifdef TCC_TARGET_PE
2460 #elif defined(TCC_TARGET_ARM)
2470 } else if (bt
== VT_INT
|| bt
== VT_FLOAT
) {
2473 } else if (bt
== VT_SHORT
) {
2476 } else if (bt
== VT_QLONG
|| bt
== VT_QFLOAT
) {
2479 } else if (bt
== VT_ENUM
) {
2481 /* Enums might be incomplete, so don't just return '4' here. */
2482 return type
->ref
->c
;
2484 /* char, void, function, _Bool */
2490 /* push type size as known at runtime time on top of value stack. Put
2492 ST_FUNC
void vla_runtime_type_size(CType
*type
, int *a
)
2494 if (type
->t
& VT_VLA
) {
2495 type_size(&type
->ref
->type
, a
);
2496 vset(&int_type
, VT_LOCAL
|VT_LVAL
, type
->ref
->c
);
2498 vpushi(type_size(type
, a
));
2502 static void vla_sp_restore(void) {
2503 if (vlas_in_scope
) {
2504 gen_vla_sp_restore(vla_sp_loc
);
2508 static void vla_sp_restore_root(void) {
2509 if (vlas_in_scope
) {
2510 gen_vla_sp_restore(vla_sp_root_loc
);
2514 /* return the pointed type of t */
2515 static inline CType
*pointed_type(CType
*type
)
2517 return &type
->ref
->type
;
2520 /* modify type so that its it is a pointer to type. */
2521 ST_FUNC
void mk_pointer(CType
*type
)
2524 s
= sym_push(SYM_FIELD
, type
, 0, -1);
2525 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
2529 /* compare function types. OLD functions match any new functions */
2530 static int is_compatible_func(CType
*type1
, CType
*type2
)
2536 if (!is_compatible_types(&s1
->type
, &s2
->type
))
2538 /* check func_call */
2539 if (s1
->a
.func_call
!= s2
->a
.func_call
)
2541 /* XXX: not complete */
2542 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
2546 while (s1
!= NULL
) {
2549 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
2559 /* return true if type1 and type2 are the same. If unqualified is
2560 true, qualifiers on the types are ignored.
2562 - enums are not checked as gcc __builtin_types_compatible_p ()
2564 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2568 t1
= type1
->t
& VT_TYPE
;
2569 t2
= type2
->t
& VT_TYPE
;
2571 /* strip qualifiers before comparing */
2572 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2573 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2575 /* Default Vs explicit signedness only matters for char */
2576 if ((t1
& VT_BTYPE
) != VT_BYTE
) {
2580 /* An enum is compatible with (unsigned) int. Ideally we would
2581 store the enums signedness in type->ref.a.<some_bit> and
2582 only accept unsigned enums with unsigned int and vice versa.
2583 But one of our callers (gen_assign_cast) always strips VT_UNSIGNED
2584 from pointer target types, so we can't add it here either. */
2585 if ((t1
& VT_BTYPE
) == VT_ENUM
) {
2587 if (type1
->ref
->a
.unsigned_enum
)
2590 if ((t2
& VT_BTYPE
) == VT_ENUM
) {
2592 if (type2
->ref
->a
.unsigned_enum
)
2595 /* XXX: bitfields ? */
2598 /* test more complicated cases */
2599 bt1
= t1
& VT_BTYPE
;
2600 if (bt1
== VT_PTR
) {
2601 type1
= pointed_type(type1
);
2602 type2
= pointed_type(type2
);
2603 return is_compatible_types(type1
, type2
);
2604 } else if (bt1
== VT_STRUCT
) {
2605 return (type1
->ref
== type2
->ref
);
2606 } else if (bt1
== VT_FUNC
) {
2607 return is_compatible_func(type1
, type2
);
2613 /* return true if type1 and type2 are exactly the same (including
2616 static int is_compatible_types(CType
*type1
, CType
*type2
)
2618 return compare_types(type1
,type2
,0);
2621 /* return true if type1 and type2 are the same (ignoring qualifiers).
2623 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
2625 return compare_types(type1
,type2
,1);
2628 /* print a type. If 'varstr' is not NULL, then the variable is also
2629 printed in the type */
2631 /* XXX: add array and function pointers */
2632 static void type_to_str(char *buf
, int buf_size
,
2633 CType
*type
, const char *varstr
)
2640 t
= type
->t
& VT_TYPE
;
2643 if (t
& VT_CONSTANT
)
2644 pstrcat(buf
, buf_size
, "const ");
2645 if (t
& VT_VOLATILE
)
2646 pstrcat(buf
, buf_size
, "volatile ");
2647 if ((t
& (VT_DEFSIGN
| VT_UNSIGNED
)) == (VT_DEFSIGN
| VT_UNSIGNED
))
2648 pstrcat(buf
, buf_size
, "unsigned ");
2649 else if (t
& VT_DEFSIGN
)
2650 pstrcat(buf
, buf_size
, "signed ");
2680 tstr
= "long double";
2682 pstrcat(buf
, buf_size
, tstr
);
2686 if (bt
== VT_STRUCT
)
2690 pstrcat(buf
, buf_size
, tstr
);
2691 v
= type
->ref
->v
& ~SYM_STRUCT
;
2692 if (v
>= SYM_FIRST_ANOM
)
2693 pstrcat(buf
, buf_size
, "<anonymous>");
2695 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
2699 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
2700 pstrcat(buf
, buf_size
, "(");
2702 while (sa
!= NULL
) {
2703 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
2704 pstrcat(buf
, buf_size
, buf1
);
2707 pstrcat(buf
, buf_size
, ", ");
2709 pstrcat(buf
, buf_size
, ")");
2714 snprintf(buf1
, sizeof(buf1
), "%s[%ld]", varstr
? varstr
: "", s
->c
);
2715 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2718 pstrcpy(buf1
, sizeof(buf1
), "*");
2719 if (t
& VT_CONSTANT
)
2720 pstrcat(buf1
, buf_size
, "const ");
2721 if (t
& VT_VOLATILE
)
2722 pstrcat(buf1
, buf_size
, "volatile ");
2724 pstrcat(buf1
, sizeof(buf1
), varstr
);
2725 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2729 pstrcat(buf
, buf_size
, " ");
2730 pstrcat(buf
, buf_size
, varstr
);
2735 /* verify type compatibility to store vtop in 'dt' type, and generate
2737 static void gen_assign_cast(CType
*dt
)
2739 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
2740 char buf1
[256], buf2
[256];
2743 st
= &vtop
->type
; /* source type */
2744 dbt
= dt
->t
& VT_BTYPE
;
2745 sbt
= st
->t
& VT_BTYPE
;
2746 if (sbt
== VT_VOID
|| dbt
== VT_VOID
) {
2747 if (sbt
== VT_VOID
&& dbt
== VT_VOID
)
2749 It is Ok if both are void
2755 gcc accepts this program
2758 tcc_error("cannot cast from/to void");
2760 if (dt
->t
& VT_CONSTANT
)
2761 tcc_warning("assignment of read-only location");
2764 /* special cases for pointers */
2765 /* '0' can also be a pointer */
2766 if (is_null_pointer(vtop
))
2768 /* accept implicit pointer to integer cast with warning */
2769 if (is_integer_btype(sbt
)) {
2770 tcc_warning("assignment makes pointer from integer without a cast");
2773 type1
= pointed_type(dt
);
2774 /* a function is implicitely a function pointer */
2775 if (sbt
== VT_FUNC
) {
2776 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
2777 !is_compatible_types(pointed_type(dt
), st
))
2778 tcc_warning("assignment from incompatible pointer type");
2783 type2
= pointed_type(st
);
2784 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2785 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
2786 /* void * can match anything */
2788 /* exact type match, except for qualifiers */
2791 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2792 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2793 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2794 /* Like GCC don't warn by default for merely changes
2795 in pointer target signedness. Do warn for different
2796 base types, though, in particular for unsigned enums
2797 and signed int targets. */
2798 if ((tmp_type1
.t
& (VT_DEFSIGN
| VT_UNSIGNED
)) !=
2799 (tmp_type2
.t
& (VT_DEFSIGN
| VT_UNSIGNED
)) &&
2800 (tmp_type1
.t
& VT_BTYPE
) == (tmp_type2
.t
& VT_BTYPE
))
2803 tcc_warning("assignment from incompatible pointer type");
2806 /* check const and volatile */
2807 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
2808 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
2809 tcc_warning("assignment discards qualifiers from pointer target type");
2815 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
2816 tcc_warning("assignment makes integer from pointer without a cast");
2817 } else if (sbt
== VT_STRUCT
) {
2818 goto case_VT_STRUCT
;
2820 /* XXX: more tests */
2826 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2827 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2828 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2830 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
2831 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
2832 tcc_error("cannot cast '%s' to '%s'", buf1
, buf2
);
2840 /* store vtop in lvalue pushed on stack */
2841 ST_FUNC
void vstore(void)
2843 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
2845 ft
= vtop
[-1].type
.t
;
2846 sbt
= vtop
->type
.t
& VT_BTYPE
;
2847 dbt
= ft
& VT_BTYPE
;
2848 if ((((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2849 (sbt
== VT_INT
&& dbt
== VT_SHORT
))
2850 && !(vtop
->type
.t
& VT_BITFIELD
)) {
2851 /* optimize char/short casts */
2852 delayed_cast
= VT_MUSTCAST
;
2853 vtop
->type
.t
= (ft
& VT_TYPE
& ~VT_BITFIELD
&
2854 ((1 << VT_STRUCT_SHIFT
) - 1));
2855 /* XXX: factorize */
2856 if (ft
& VT_CONSTANT
)
2857 tcc_warning("assignment of read-only location");
2860 if (!(ft
& VT_BITFIELD
))
2861 gen_assign_cast(&vtop
[-1].type
);
2864 if (sbt
== VT_STRUCT
) {
2865 /* if structure, only generate pointer */
2866 /* structure assignment : generate memcpy */
2867 /* XXX: optimize if small size */
2868 if (!nocode_wanted
) {
2869 size
= type_size(&vtop
->type
, &align
);
2873 vtop
->type
.t
= VT_PTR
;
2876 /* address of memcpy() */
2879 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2880 else if(!(align
& 3))
2881 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2884 /* Use memmove, rather than memcpy, as dest and src may be same: */
2885 vpush_global_sym(&func_old_type
, TOK_memmove
);
2890 vtop
->type
.t
= VT_PTR
;
2899 /* leave source on stack */
2900 } else if (ft
& VT_BITFIELD
) {
2901 /* bitfield store handling */
2903 /* save lvalue as expression result (example: s.b = s.a = n;) */
2904 vdup(), vtop
[-1] = vtop
[-2];
2906 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2907 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2908 /* remove bit field info to avoid loops */
2909 vtop
[-1].type
.t
= ft
& ~VT_BITFIELD
& ((1 << VT_STRUCT_SHIFT
) - 1);
2911 if((ft
& VT_BTYPE
) == VT_BOOL
) {
2912 gen_cast(&vtop
[-1].type
);
2913 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2916 /* duplicate destination */
2918 vtop
[-1] = vtop
[-2];
2920 /* mask and shift source */
2921 if((ft
& VT_BTYPE
) != VT_BOOL
) {
2922 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2923 vpushll((1ULL << bit_size
) - 1ULL);
2925 vpushi((1 << bit_size
) - 1);
2931 /* load destination, mask and or with source */
2933 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2934 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2936 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2942 /* ... and discard */
2946 if (!nocode_wanted
) {
2947 #ifdef CONFIG_TCC_BCHECK
2948 /* bound check case */
2949 if (vtop
[-1].r
& VT_MUSTBOUND
) {
2958 #ifdef TCC_TARGET_X86_64
2959 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
2961 } else if ((ft
& VT_BTYPE
) == VT_QFLOAT
) {
2966 r
= gv(rc
); /* generate value */
2967 /* if lvalue was saved on stack, must read it */
2968 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
2970 t
= get_reg(RC_INT
);
2971 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2976 sv
.r
= VT_LOCAL
| VT_LVAL
;
2977 sv
.c
.i
= vtop
[-1].c
.i
;
2979 vtop
[-1].r
= t
| VT_LVAL
;
2981 /* two word case handling : store second register at word + 4 (or +8 for x86-64) */
2982 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2983 if (((ft
& VT_BTYPE
) == VT_QLONG
) || ((ft
& VT_BTYPE
) == VT_QFLOAT
)) {
2984 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
2986 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
2987 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
2989 vtop
[-1].type
.t
= load_type
;
2992 /* convert to int to increment easily */
2993 vtop
->type
.t
= addr_type
;
2999 vtop
[-1].type
.t
= load_type
;
3000 /* XXX: it works because r2 is spilled last ! */
3001 store(vtop
->r2
, vtop
- 1);
3007 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
3008 vtop
->r
|= delayed_cast
;
3012 /* post defines POST/PRE add. c is the token ++ or -- */
3013 ST_FUNC
void inc(int post
, int c
)
3016 vdup(); /* save lvalue */
3019 gv_dup(); /* duplicate value */
3021 vdup(); /* duplicate value */
3026 vpushi(c
- TOK_MID
);
3028 vstore(); /* store value */
3030 vpop(); /* if post op, return saved value */
3033 ST_FUNC
void parse_mult_str (CString
*astr
, const char *msg
)
3035 /* read the string */
3039 while (tok
== TOK_STR
) {
3040 /* XXX: add \0 handling too ? */
3041 cstr_cat(astr
, tokc
.str
.data
, -1);
3044 cstr_ccat(astr
, '\0');
3047 /* Parse GNUC __attribute__ extension. Currently, the following
3048 extensions are recognized:
3049 - aligned(n) : set data/function alignment.
3050 - packed : force data alignment to 1
3051 - section(x) : generate data/code in this section.
3052 - unused : currently ignored, but may be used someday.
3053 - regparm(n) : pass function parameters in registers (i386 only)
3055 static void parse_attribute(AttributeDef
*ad
)
3060 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
3064 while (tok
!= ')') {
3065 if (tok
< TOK_IDENT
)
3066 expect("attribute name");
3073 parse_mult_str(&astr
, "section name");
3074 ad
->section
= find_section(tcc_state
, (char *)astr
.data
);
3081 parse_mult_str(&astr
, "alias(\"target\")");
3082 ad
->alias_target
= /* save string as token, for later */
3083 tok_alloc((char*)astr
.data
, astr
.size
-1)->tok
;
3087 case TOK_VISIBILITY1
:
3088 case TOK_VISIBILITY2
:
3090 parse_mult_str(&astr
,
3091 "visibility(\"default|hidden|internal|protected\")");
3092 if (!strcmp (astr
.data
, "default"))
3093 ad
->a
.visibility
= STV_DEFAULT
;
3094 else if (!strcmp (astr
.data
, "hidden"))
3095 ad
->a
.visibility
= STV_HIDDEN
;
3096 else if (!strcmp (astr
.data
, "internal"))
3097 ad
->a
.visibility
= STV_INTERNAL
;
3098 else if (!strcmp (astr
.data
, "protected"))
3099 ad
->a
.visibility
= STV_PROTECTED
;
3101 expect("visibility(\"default|hidden|internal|protected\")");
3110 if (n
<= 0 || (n
& (n
- 1)) != 0)
3111 tcc_error("alignment must be a positive power of two");
3128 /* currently, no need to handle it because tcc does not
3129 track unused objects */
3133 /* currently, no need to handle it because tcc does not
3134 track unused objects */
3139 ad
->a
.func_call
= FUNC_CDECL
;
3144 ad
->a
.func_call
= FUNC_STDCALL
;
3146 #ifdef TCC_TARGET_I386
3156 ad
->a
.func_call
= FUNC_FASTCALL1
+ n
- 1;
3162 ad
->a
.func_call
= FUNC_FASTCALLW
;
3169 ad
->a
.mode
= VT_LLONG
+ 1;
3172 ad
->a
.mode
= VT_BYTE
+ 1;
3175 ad
->a
.mode
= VT_SHORT
+ 1;
3179 ad
->a
.mode
= VT_INT
+ 1;
3182 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
3189 ad
->a
.func_export
= 1;
3192 ad
->a
.func_import
= 1;
3195 if (tcc_state
->warn_unsupported
)
3196 tcc_warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
3197 /* skip parameters */
3199 int parenthesis
= 0;
3203 else if (tok
== ')')
3206 } while (parenthesis
&& tok
!= -1);
3219 static Sym
* find_field (CType
*type
, int v
)
3223 while ((s
= s
->next
) != NULL
) {
3224 if ((s
->v
& SYM_FIELD
) && (s
->v
& ~SYM_FIELD
) >= SYM_FIRST_ANOM
) {
3225 Sym
*ret
= find_field (&s
->type
, v
);
3235 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
3236 static void struct_decl(CType
*type
, AttributeDef
*ad
, int u
)
3238 int a
, v
, size
, align
, maxalign
, c
, offset
, flexible
, extra_bytes
;
3239 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
, prevbt
;
3240 Sym
*s
, *ss
, *ass
, **ps
;
3244 a
= tok
; /* save decl type */
3246 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3247 parse_attribute(ad
);
3251 /* struct already defined ? return it */
3253 expect("struct/union/enum name");
3255 if (s
&& (s
->scope
== local_scope
|| (tok
!= '{' && tok
!= ';'))) {
3257 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
3263 /* Record the original enum/struct/union token. */
3266 /* we put an undefined size for struct/union */
3267 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
3268 s
->r
= 0; /* default alignment is zero as gcc */
3269 /* put struct/union/enum name in type */
3277 tcc_error("struct/union/enum already defined");
3278 /* cannot be empty */
3280 /* non empty enums are not allowed */
3281 if (a
== TOK_ENUM
) {
3286 expect("identifier");
3288 if (ss
&& !local_stack
)
3289 tcc_error("redefinition of enumerator '%s'",
3290 get_tok_str(v
, NULL
));
3298 /* enum symbols have static storage */
3299 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
3300 ss
->type
.t
|= VT_STATIC
;
3305 /* NOTE: we accept a trailing comma */
3310 s
->a
.unsigned_enum
= 1;
3311 s
->c
= type_size(&int_type
, &align
);
3320 while (tok
!= '}') {
3321 if (!parse_btype(&btype
, &ad1
)) {
3328 tcc_error("flexible array member '%s' not at the end of struct",
3329 get_tok_str(v
, NULL
));
3334 type_decl(&type1
, &ad1
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3336 if ((type1
.t
& VT_BTYPE
) != VT_STRUCT
)
3337 expect("identifier");
3339 int v
= btype
.ref
->v
;
3340 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
3341 if (tcc_state
->ms_extensions
== 0)
3342 expect("identifier");
3346 if (type_size(&type1
, &align
) < 0) {
3347 if ((a
== TOK_STRUCT
) && (type1
.t
& VT_ARRAY
) && c
)
3350 tcc_error("field '%s' has incomplete type",
3351 get_tok_str(v
, NULL
));
3353 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
3354 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
3355 tcc_error("invalid type for '%s'",
3356 get_tok_str(v
, NULL
));
3360 bit_size
= expr_const();
3361 /* XXX: handle v = 0 case for messages */
3363 tcc_error("negative width in bit-field '%s'",
3364 get_tok_str(v
, NULL
));
3365 if (v
&& bit_size
== 0)
3366 tcc_error("zero width for bit-field '%s'",
3367 get_tok_str(v
, NULL
));
3369 size
= type_size(&type1
, &align
);
3370 if (ad1
.a
.aligned
) {
3371 if (align
< ad1
.a
.aligned
)
3372 align
= ad1
.a
.aligned
;
3373 } else if (ad1
.a
.packed
|| ad
->a
.packed
) {
3375 } else if (*tcc_state
->pack_stack_ptr
) {
3376 if (align
> *tcc_state
->pack_stack_ptr
)
3377 align
= *tcc_state
->pack_stack_ptr
;
3380 if (bit_size
>= 0) {
3381 bt
= type1
.t
& VT_BTYPE
;
3388 tcc_error("bitfields must have scalar type");
3390 if (bit_size
> bsize
) {
3391 tcc_error("width of '%s' exceeds its type",
3392 get_tok_str(v
, NULL
));
3393 } else if (bit_size
== bsize
) {
3394 /* no need for bit fields */
3396 } else if (bit_size
== 0) {
3397 /* XXX: what to do if only padding in a
3399 /* zero size: means to pad */
3402 /* if type change, union, or will overrun
3403 * allignment slot, start at a newly
3405 if ((bit_pos
+ bit_size
) > bsize
||
3406 bt
!= prevbt
|| a
== TOK_UNION
)
3409 /* XXX: handle LSB first */
3410 type1
.t
|= VT_BITFIELD
|
3411 (bit_pos
<< VT_STRUCT_SHIFT
) |
3412 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
3413 bit_pos
+= bit_size
;
3414 /* without ms-bitfields, allocate the
3415 * minimum number of bytes necessary,
3416 * adding single bytes as needed */
3417 if (!tcc_state
->ms_bitfields
) {
3419 /* minimum bytes for new bitfield */
3420 size
= (bit_size
+ 7) / 8;
3422 /* enough spare bits already allocated? */
3423 bit_size
= (lbit_pos
- 1) % 8 + 1 + bit_size
;
3424 if (bit_size
> 8) /* doesn't fit */
3425 extra_bytes
= (bit_size
- 1) / 8;
3433 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
3434 /* add new memory data only if starting bit
3435 field or adding bytes to existing bit field */
3436 if (extra_bytes
) c
+= extra_bytes
;
3437 else if (lbit_pos
== 0) {
3438 if (a
== TOK_STRUCT
) {
3439 c
= (c
+ align
- 1) & -align
;
3448 if (align
> maxalign
)
3452 printf("add field %s offset=%d",
3453 get_tok_str(v
, NULL
), offset
);
3454 if (type1
.t
& VT_BITFIELD
) {
3455 printf(" pos=%d size=%d",
3456 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
3457 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
3462 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
3463 /* An anonymous struct/union. Adjust member offsets
3464 to reflect the real offset of our containing struct.
3465 Also set the offset of this anon member inside
3466 the outer struct to be zero. Via this it
3467 works when accessing the field offset directly
3468 (from base object), as well as when recursing
3469 members in initializer handling. */
3470 int v2
= btype
.ref
->v
;
3471 if (!(v2
& SYM_FIELD
) &&
3472 (v2
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
3474 /* This happens only with MS extensions. The
3475 anon member has a named struct type, so it
3476 potentially is shared with other references.
3477 We need to unshare members so we can modify
3480 type1
.ref
= sym_push(anon_sym
++ | SYM_FIELD
,
3481 &type1
.ref
->type
, 0,
3483 pps
= &type1
.ref
->next
;
3484 while ((ass
= ass
->next
) != NULL
) {
3485 *pps
= sym_push(ass
->v
, &ass
->type
, 0, ass
->c
);
3486 pps
= &((*pps
)->next
);
3491 while ((ass
= ass
->next
) != NULL
)
3497 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
3501 if (tok
== ';' || tok
== TOK_EOF
)
3508 /* store size and alignment */
3509 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
3515 /* return 1 if basic type is a type size (short, long, long long) */
3516 ST_FUNC
int is_btype_size(int bt
)
3518 return bt
== VT_SHORT
|| bt
== VT_LONG
|| bt
== VT_LLONG
;
3521 /* Add type qualifiers to a type. If the type is an array then the qualifiers
3522 are added to the element type, copied because it could be a typedef. */
3523 static void parse_btype_qualify(CType
*type
, int qualifiers
)
3525 while (type
->t
& VT_ARRAY
) {
3526 type
->ref
= sym_push(SYM_FIELD
, &type
->ref
->type
, 0, type
->ref
->c
);
3527 type
= &type
->ref
->type
;
3529 type
->t
|= qualifiers
;
3532 /* return 0 if no type declaration. otherwise, return the basic type
3535 static int parse_btype(CType
*type
, AttributeDef
*ad
)
3537 int t
, u
, bt_size
, complete
, type_found
, typespec_found
;
3541 memset(ad
, 0, sizeof(AttributeDef
));
3549 /* currently, we really ignore extension */
3560 tcc_error("too many basic types");
3562 bt_size
= is_btype_size (u
& VT_BTYPE
);
3563 if (u
== VT_INT
|| (!bt_size
&& !(t
& VT_TYPEDEF
)))
3578 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
3579 #ifndef TCC_TARGET_PE
3580 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3582 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
3583 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3589 #ifdef TCC_TARGET_ARM64
3591 /* GCC's __uint128_t appears in some Linux header files. Make it a
3592 synonym for long double to get the size and alignment right. */
3604 if ((t
& VT_BTYPE
) == VT_LONG
) {
3605 #ifdef TCC_TARGET_PE
3606 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
3608 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3616 struct_decl(&type1
, ad
, VT_ENUM
);
3619 type
->ref
= type1
.ref
;
3623 struct_decl(&type1
, ad
, VT_STRUCT
);
3626 /* type modifiers */
3631 parse_btype_qualify(type
, VT_CONSTANT
);
3639 parse_btype_qualify(type
, VT_VOLATILE
);
3646 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == (VT_DEFSIGN
|VT_UNSIGNED
))
3647 tcc_error("signed and unsigned modifier");
3660 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == VT_DEFSIGN
)
3661 tcc_error("signed and unsigned modifier");
3662 t
|= VT_DEFSIGN
| VT_UNSIGNED
;
3687 /* GNUC attribute */
3688 case TOK_ATTRIBUTE1
:
3689 case TOK_ATTRIBUTE2
:
3690 parse_attribute(ad
);
3693 t
= (t
& ~VT_BTYPE
) | u
;
3701 parse_expr_type(&type1
);
3702 /* remove all storage modifiers except typedef */
3703 type1
.t
&= ~(VT_STORAGE
&~VT_TYPEDEF
);
3709 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
3712 type
->t
= ((s
->type
.t
& ~VT_TYPEDEF
) |
3713 (t
& ~(VT_CONSTANT
| VT_VOLATILE
)));
3714 type
->ref
= s
->type
.ref
;
3715 if (t
& (VT_CONSTANT
| VT_VOLATILE
))
3716 parse_btype_qualify(type
, t
& (VT_CONSTANT
| VT_VOLATILE
));
3720 /* get attributes from typedef */
3721 if (0 == ad
->a
.aligned
)
3722 ad
->a
.aligned
= s
->a
.aligned
;
3723 if (0 == ad
->a
.func_call
)
3724 ad
->a
.func_call
= s
->a
.func_call
;
3725 ad
->a
.packed
|= s
->a
.packed
;
3734 if (tcc_state
->char_is_unsigned
) {
3735 if ((t
& (VT_DEFSIGN
|VT_BTYPE
)) == VT_BYTE
)
3739 /* long is never used as type */
3740 if ((t
& VT_BTYPE
) == VT_LONG
)
3741 #if (!defined TCC_TARGET_X86_64 && !defined TCC_TARGET_ARM64) || \
3742 defined TCC_TARGET_PE
3743 t
= (t
& ~VT_BTYPE
) | VT_INT
;
3745 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3751 /* convert a function parameter type (array to pointer and function to
3752 function pointer) */
3753 static inline void convert_parameter_type(CType
*pt
)
3755 /* remove const and volatile qualifiers (XXX: const could be used
3756 to indicate a const function parameter */
3757 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3758 /* array must be transformed to pointer according to ANSI C */
3760 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
3765 ST_FUNC
void parse_asm_str(CString
*astr
)
3768 parse_mult_str(astr
, "string constant");
3771 /* Parse an asm label and return the token */
3772 static int asm_label_instr(void)
3778 parse_asm_str(&astr
);
3781 printf("asm_alias: \"%s\"\n", (char *)astr
.data
);
3783 v
= tok_alloc(astr
.data
, astr
.size
- 1)->tok
;
3788 static void post_type(CType
*type
, AttributeDef
*ad
)
3790 int n
, l
, t1
, arg_size
, align
;
3791 Sym
**plast
, *s
, *first
;
3796 /* function declaration */
3804 /* read param name and compute offset */
3805 if (l
!= FUNC_OLD
) {
3806 if (!parse_btype(&pt
, &ad1
)) {
3808 tcc_error("invalid type");
3815 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
3817 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3818 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
3819 tcc_error("parameter declared as void");
3820 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
3825 expect("identifier");
3829 convert_parameter_type(&pt
);
3830 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
3836 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
3843 /* if no parameters, then old type prototype */
3847 /* NOTE: const is ignored in returned type as it has a special
3848 meaning in gcc / C++ */
3849 type
->t
&= ~VT_CONSTANT
;
3850 /* some ancient pre-K&R C allows a function to return an array
3851 and the array brackets to be put after the arguments, such
3852 that "int c()[]" means something like "int[] c()" */
3855 skip(']'); /* only handle simple "[]" */
3858 /* we push a anonymous symbol which will contain the function prototype */
3859 ad
->a
.func_args
= arg_size
;
3860 s
= sym_push(SYM_FIELD
, type
, 0, l
);
3865 } else if (tok
== '[') {
3866 /* array definition */
3868 if (tok
== TOK_RESTRICT1
)
3873 if (!local_stack
|| nocode_wanted
)
3874 vpushi(expr_const());
3876 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3879 tcc_error("invalid array size");
3881 if (!is_integer_btype(vtop
->type
.t
& VT_BTYPE
))
3882 tcc_error("size of variable length array should be an integer");
3887 /* parse next post type */
3888 post_type(type
, ad
);
3889 if (type
->t
== VT_FUNC
)
3890 tcc_error("declaration of an array of functions");
3891 t1
|= type
->t
& VT_VLA
;
3894 loc
-= type_size(&int_type
, &align
);
3898 vla_runtime_type_size(type
, &align
);
3900 vset(&int_type
, VT_LOCAL
|VT_LVAL
, n
);
3907 /* we push an anonymous symbol which will contain the array
3909 s
= sym_push(SYM_FIELD
, type
, 0, n
);
3910 type
->t
= (t1
? VT_VLA
: VT_ARRAY
) | VT_PTR
;
3915 /* Parse a type declaration (except basic type), and return the type
3916 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3917 expected. 'type' should contain the basic type. 'ad' is the
3918 attribute definition of the basic type. It can be modified by
3921 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
3924 CType type1
, *type2
;
3925 int qualifiers
, storage
;
3927 while (tok
== '*') {
3935 qualifiers
|= VT_CONSTANT
;
3940 qualifiers
|= VT_VOLATILE
;
3946 /* XXX: clarify attribute handling */
3947 case TOK_ATTRIBUTE1
:
3948 case TOK_ATTRIBUTE2
:
3949 parse_attribute(ad
);
3953 type
->t
|= qualifiers
;
3956 /* recursive type */
3957 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3958 type1
.t
= 0; /* XXX: same as int */
3961 /* XXX: this is not correct to modify 'ad' at this point, but
3962 the syntax is not clear */
3963 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3964 parse_attribute(ad
);
3965 type_decl(&type1
, ad
, v
, td
);
3968 /* type identifier */
3969 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
3973 if (!(td
& TYPE_ABSTRACT
))
3974 expect("identifier");
3978 storage
= type
->t
& VT_STORAGE
;
3979 type
->t
&= ~VT_STORAGE
;
3980 if (storage
& VT_STATIC
) {
3981 int saved_nocode_wanted
= nocode_wanted
;
3983 post_type(type
, ad
);
3984 nocode_wanted
= saved_nocode_wanted
;
3986 post_type(type
, ad
);
3988 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3989 parse_attribute(ad
);
3993 /* append type at the end of type1 */
4006 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
4007 ST_FUNC
int lvalue_type(int t
)
4012 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
4014 else if (bt
== VT_SHORT
)
4018 if (t
& VT_UNSIGNED
)
4019 r
|= VT_LVAL_UNSIGNED
;
4023 /* indirection with full error checking and bound check */
4024 ST_FUNC
void indir(void)
4026 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
4027 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
4031 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
4033 vtop
->type
= *pointed_type(&vtop
->type
);
4034 /* Arrays and functions are never lvalues */
4035 if (!(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_VLA
)
4036 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
4037 vtop
->r
|= lvalue_type(vtop
->type
.t
);
4038 /* if bound checking, the referenced pointer must be checked */
4039 #ifdef CONFIG_TCC_BCHECK
4040 if (tcc_state
->do_bounds_check
)
4041 vtop
->r
|= VT_MUSTBOUND
;
4046 /* pass a parameter to a function and do type checking and casting */
4047 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
4052 func_type
= func
->c
;
4053 if (func_type
== FUNC_OLD
||
4054 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
4055 /* default casting : only need to convert float to double */
4056 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
4059 } else if (vtop
->type
.t
& VT_BITFIELD
) {
4060 type
.t
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
4063 } else if (arg
== NULL
) {
4064 tcc_error("too many arguments to function");
4067 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
4068 gen_assign_cast(&type
);
4072 /* parse an expression of the form '(type)' or '(expr)' and return its
4074 static void parse_expr_type(CType
*type
)
4080 if (parse_btype(type
, &ad
)) {
4081 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
4088 static void parse_type(CType
*type
)
4093 if (!parse_btype(type
, &ad
)) {
4096 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
4099 static void vpush_tokc(int t
)
4104 vsetc(&type
, VT_CONST
, &tokc
);
4107 ST_FUNC
void unary(void)
4109 int n
, t
, align
, size
, r
, sizeof_caller
;
4114 sizeof_caller
= in_sizeof
;
4116 /* XXX: GCC 2.95.3 does not generate a table although it should be
4130 vpush_tokc(VT_INT
| VT_UNSIGNED
);
4134 vpush_tokc(VT_LLONG
);
4138 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
4142 vpush_tokc(VT_FLOAT
);
4146 vpush_tokc(VT_DOUBLE
);
4150 vpush_tokc(VT_LDOUBLE
);
4153 case TOK___FUNCTION__
:
4155 goto tok_identifier
;
4161 /* special function name identifier */
4162 len
= strlen(funcname
) + 1;
4163 /* generate char[len] type */
4168 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
4169 ptr
= section_ptr_add(data_section
, len
);
4170 memcpy(ptr
, funcname
, len
);
4175 #ifdef TCC_TARGET_PE
4176 t
= VT_SHORT
| VT_UNSIGNED
;
4182 /* string parsing */
4185 if (tcc_state
->warn_write_strings
)
4190 memset(&ad
, 0, sizeof(AttributeDef
));
4191 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, 0);
4196 if (parse_btype(&type
, &ad
)) {
4197 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
4199 /* check ISOC99 compound literal */
4201 /* data is allocated locally by default */
4206 /* all except arrays are lvalues */
4207 if (!(type
.t
& VT_ARRAY
))
4208 r
|= lvalue_type(type
.t
);
4209 memset(&ad
, 0, sizeof(AttributeDef
));
4210 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, 0);
4212 if (sizeof_caller
) {
4219 } else if (tok
== '{') {
4221 tcc_error("expected constant");
4222 /* save all registers */
4225 /* statement expression : we do not accept break/continue
4226 inside as GCC does */
4227 block(NULL
, NULL
, 1);
4242 /* functions names must be treated as function pointers,
4243 except for unary '&' and sizeof. Since we consider that
4244 functions are not lvalues, we only have to handle it
4245 there and in function calls. */
4246 /* arrays can also be used although they are not lvalues */
4247 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
4248 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
4250 mk_pointer(&vtop
->type
);
4256 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4258 boolean
.t
= VT_BOOL
;
4260 vtop
->c
.i
= !vtop
->c
.i
;
4261 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
4265 vseti(VT_JMP
, gvtst(1, 0));
4277 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
4278 tcc_error("pointer not accepted for unary plus");
4279 /* In order to force cast, we add zero, except for floating point
4280 where we really need an noop (otherwise -0.0 will be transformed
4282 if (!is_float(vtop
->type
.t
)) {
4293 unary_type(&type
); // Perform a in_sizeof = 0;
4294 size
= type_size(&type
, &align
);
4295 if (t
== TOK_SIZEOF
) {
4296 if (!(type
.t
& VT_VLA
)) {
4298 tcc_error("sizeof applied to an incomplete type");
4301 vla_runtime_type_size(&type
, &align
);
4306 vtop
->type
.t
|= VT_UNSIGNED
;
4309 case TOK_builtin_expect
:
4311 /* __builtin_expect is a no-op for now */
4312 int saved_nocode_wanted
;
4317 saved_nocode_wanted
= nocode_wanted
;
4321 nocode_wanted
= saved_nocode_wanted
;
4325 case TOK_builtin_types_compatible_p
:
4334 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
4335 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
4336 vpushi(is_compatible_types(&type1
, &type2
));
4339 case TOK_builtin_choose_expr
:
4341 int saved_nocode_wanted
, c
;
4347 saved_nocode_wanted
= nocode_wanted
;
4353 nocode_wanted
= saved_nocode_wanted
;
4357 saved_nocode_wanted
= nocode_wanted
;
4363 nocode_wanted
= saved_nocode_wanted
;
4368 case TOK_builtin_constant_p
:
4370 int saved_nocode_wanted
, res
;
4373 saved_nocode_wanted
= nocode_wanted
;
4376 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
4378 nocode_wanted
= saved_nocode_wanted
;
4383 case TOK_builtin_frame_address
:
4384 case TOK_builtin_return_address
:
4391 if (tok
!= TOK_CINT
) {
4392 tcc_error("%s only takes positive integers",
4393 tok1
== TOK_builtin_return_address
?
4394 "__builtin_return_address" :
4395 "__builtin_frame_address");
4397 level
= (uint32_t)tokc
.i
;
4402 vset(&type
, VT_LOCAL
, 0); /* local frame */
4404 mk_pointer(&vtop
->type
);
4405 indir(); /* -> parent frame */
4407 if (tok1
== TOK_builtin_return_address
) {
4408 // assume return address is just above frame pointer on stack
4411 mk_pointer(&vtop
->type
);
4416 #ifdef TCC_TARGET_X86_64
4417 #ifdef TCC_TARGET_PE
4418 case TOK_builtin_va_start
:
4426 if ((vtop
->r
& VT_VALMASK
) != VT_LOCAL
)
4427 tcc_error("__builtin_va_start expects a local variable");
4428 vtop
->r
&= ~(VT_LVAL
| VT_REF
);
4429 vtop
->type
= char_pointer_type
;
4435 case TOK_builtin_va_arg_types
:
4442 vpushi(classify_x86_64_va_arg(&type
));
4448 #ifdef TCC_TARGET_ARM64
4449 case TOK___va_start
: {
4451 tcc_error("statement in global scope");
4461 vtop
->type
.t
= VT_VOID
;
4464 case TOK___va_arg
: {
4467 tcc_error("statement in global scope");
4479 case TOK___arm64_clear_cache
: {
4488 vtop
->type
.t
= VT_VOID
;
4492 /* pre operations */
4503 t
= vtop
->type
.t
& VT_BTYPE
;
4505 /* In IEEE negate(x) isn't subtract(0,x), but rather
4510 else if (t
== VT_DOUBLE
)
4521 goto tok_identifier
;
4523 /* allow to take the address of a label */
4524 if (tok
< TOK_UIDENT
)
4525 expect("label identifier");
4526 s
= label_find(tok
);
4528 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4530 if (s
->r
== LABEL_DECLARED
)
4531 s
->r
= LABEL_FORWARD
;
4534 s
->type
.t
= VT_VOID
;
4535 mk_pointer(&s
->type
);
4536 s
->type
.t
|= VT_STATIC
;
4538 vpushsym(&s
->type
, s
);
4542 // special qnan , snan and infinity values
4544 vpush64(VT_DOUBLE
, 0x7ff8000000000000ULL
);
4548 vpush64(VT_DOUBLE
, 0x7ff0000000000001ULL
);
4552 vpush64(VT_DOUBLE
, 0x7ff0000000000000ULL
);
4561 expect("identifier");
4564 const char *name
= get_tok_str(t
, NULL
);
4566 tcc_error("'%s' undeclared", name
);
4567 /* for simple function calls, we tolerate undeclared
4568 external reference to int() function */
4569 if (tcc_state
->warn_implicit_function_declaration
4570 #ifdef TCC_TARGET_PE
4571 /* people must be warned about using undeclared WINAPI functions
4572 (which usually start with uppercase letter) */
4573 || (name
[0] >= 'A' && name
[0] <= 'Z')
4576 tcc_warning("implicit declaration of function '%s'", name
);
4577 s
= external_global_sym(t
, &func_old_type
, 0);
4579 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
4580 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
4581 /* if referencing an inline function, then we generate a
4582 symbol to it if not already done. It will have the
4583 effect to generate code for it at the end of the
4584 compilation unit. Inline function as always
4585 generated in the text section. */
4587 put_extern_sym(s
, text_section
, 0, 0);
4588 r
= VT_SYM
| VT_CONST
;
4592 vset(&s
->type
, r
, s
->c
);
4593 /* if forward reference, we must point to s */
4594 if (vtop
->r
& VT_SYM
) {
4601 /* post operations */
4603 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
4606 } else if (tok
== '.' || tok
== TOK_ARROW
|| tok
== TOK_CDOUBLE
) {
4609 if (tok
== TOK_ARROW
)
4611 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
4614 /* expect pointer on structure */
4615 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
4616 expect("struct or union");
4617 if (tok
== TOK_CDOUBLE
)
4618 expect("field name");
4620 if (tok
== TOK_CINT
|| tok
== TOK_CUINT
)
4621 expect("field name");
4622 s
= find_field(&vtop
->type
, tok
);
4624 tcc_error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, &tokc
));
4625 /* add field offset to pointer */
4626 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
4629 /* change type to field type, and set to lvalue */
4630 vtop
->type
= s
->type
;
4631 vtop
->type
.t
|= qualifiers
;
4632 /* an array is never an lvalue */
4633 if (!(vtop
->type
.t
& VT_ARRAY
)) {
4634 vtop
->r
|= lvalue_type(vtop
->type
.t
);
4635 #ifdef CONFIG_TCC_BCHECK
4636 /* if bound checking, the referenced pointer must be checked */
4637 if (tcc_state
->do_bounds_check
&& (vtop
->r
& VT_VALMASK
) != VT_LOCAL
)
4638 vtop
->r
|= VT_MUSTBOUND
;
4642 } else if (tok
== '[') {
4648 } else if (tok
== '(') {
4651 int nb_args
, ret_nregs
, ret_align
, regsize
, variadic
;
4654 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
4655 /* pointer test (no array accepted) */
4656 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
4657 vtop
->type
= *pointed_type(&vtop
->type
);
4658 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
4662 expect("function pointer");
4665 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
4667 /* get return type */
4670 sa
= s
->next
; /* first parameter */
4673 /* compute first implicit argument if a structure is returned */
4674 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
4675 variadic
= (s
->c
== FUNC_ELLIPSIS
);
4676 ret_nregs
= gfunc_sret(&s
->type
, variadic
, &ret
.type
,
4677 &ret_align
, ®size
);
4679 /* get some space for the returned structure */
4680 size
= type_size(&s
->type
, &align
);
4681 #ifdef TCC_TARGET_ARM64
4682 /* On arm64, a small struct is return in registers.
4683 It is much easier to write it to memory if we know
4684 that we are allowed to write some extra bytes, so
4685 round the allocated space up to a power of 2: */
4687 while (size
& (size
- 1))
4688 size
= (size
| (size
- 1)) + 1;
4690 loc
= (loc
- size
) & -align
;
4692 ret
.r
= VT_LOCAL
| VT_LVAL
;
4693 /* pass it as 'int' to avoid structure arg passing
4695 vseti(VT_LOCAL
, loc
);
4705 /* return in register */
4706 if (is_float(ret
.type
.t
)) {
4707 ret
.r
= reg_fret(ret
.type
.t
);
4708 #ifdef TCC_TARGET_X86_64
4709 if ((ret
.type
.t
& VT_BTYPE
) == VT_QFLOAT
)
4713 #ifndef TCC_TARGET_ARM64
4714 #ifdef TCC_TARGET_X86_64
4715 if ((ret
.type
.t
& VT_BTYPE
) == VT_QLONG
)
4717 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
4728 gfunc_param_typed(s
, sa
);
4738 tcc_error("too few arguments to function");
4740 if (!nocode_wanted
) {
4741 gfunc_call(nb_args
);
4743 vtop
-= (nb_args
+ 1);
4747 for (r
= ret
.r
+ ret_nregs
+ !ret_nregs
; r
-- > ret
.r
;) {
4748 vsetc(&ret
.type
, r
, &ret
.c
);
4749 vtop
->r2
= ret
.r2
; /* Loop only happens when r2 is VT_CONST */
4752 /* handle packed struct return */
4753 if (((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) && ret_nregs
) {
4756 size
= type_size(&s
->type
, &align
);
4757 /* We're writing whole regs often, make sure there's enough
4758 space. Assume register size is power of 2. */
4759 if (regsize
> align
)
4761 loc
= (loc
- size
) & -align
;
4765 vset(&ret
.type
, VT_LOCAL
| VT_LVAL
, addr
+ offset
);
4769 if (--ret_nregs
== 0)
4773 vset(&s
->type
, VT_LOCAL
| VT_LVAL
, addr
);
4781 ST_FUNC
void expr_prod(void)
4786 while (tok
== '*' || tok
== '/' || tok
== '%') {
4794 ST_FUNC
void expr_sum(void)
4799 while (tok
== '+' || tok
== '-') {
4807 static void expr_shift(void)
4812 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
4820 static void expr_cmp(void)
4825 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
4826 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
4834 static void expr_cmpeq(void)
4839 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
4847 static void expr_and(void)
4850 while (tok
== '&') {
4857 static void expr_xor(void)
4860 while (tok
== '^') {
4867 static void expr_or(void)
4870 while (tok
== '|') {
4877 /* XXX: fix this mess */
4878 static void expr_land_const(void)
4881 while (tok
== TOK_LAND
) {
4887 static void expr_lor_const(void)
4890 while (tok
== TOK_LOR
) {
4897 static void expr_land(void)
4900 if (tok
== TOK_LAND
) {
4901 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4912 int saved_nocode_wanted
= nocode_wanted
;
4916 nocode_wanted
= saved_nocode_wanted
;
4924 if (tok
!= TOK_LAND
) {
4935 static void expr_lor(void)
4938 if (tok
== TOK_LOR
) {
4939 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4946 int saved_nocode_wanted
= nocode_wanted
;
4950 nocode_wanted
= saved_nocode_wanted
;
4962 if (tok
!= TOK_LOR
) {
4973 static void expr_cond(void)
4975 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
, islv
;
4977 CType type
, type1
, type2
;
4982 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4983 int saved_nocode_wanted
= nocode_wanted
;
4986 boolean
.t
= VT_BOOL
;
4992 if (tok
!= ':' || !gnu_ext
) {
5000 nocode_wanted
= saved_nocode_wanted
;
5003 if (tok
!= ':' || !gnu_ext
) {
5007 nocode_wanted
= saved_nocode_wanted
;
5014 if (vtop
!= vstack
) {
5015 /* needed to avoid having different registers saved in
5017 if (is_float(vtop
->type
.t
)) {
5019 #ifdef TCC_TARGET_X86_64
5020 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
5030 if (tok
== ':' && gnu_ext
) {
5038 sv
= *vtop
; /* save value to handle it later */
5039 vtop
--; /* no vpop so that FP stack is not flushed */
5047 bt1
= t1
& VT_BTYPE
;
5049 bt2
= t2
& VT_BTYPE
;
5050 /* cast operands to correct type according to ISOC rules */
5051 if (is_float(bt1
) || is_float(bt2
)) {
5052 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
5053 type
.t
= VT_LDOUBLE
;
5054 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
5059 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
5060 /* cast to biggest op */
5062 /* convert to unsigned if it does not fit in a long long */
5063 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
5064 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
5065 type
.t
|= VT_UNSIGNED
;
5066 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
5067 /* If one is a null ptr constant the result type
5069 if (is_null_pointer (vtop
))
5071 else if (is_null_pointer (&sv
))
5073 /* XXX: test pointer compatibility, C99 has more elaborate
5077 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
5078 /* XXX: test function pointer compatibility */
5079 type
= bt1
== VT_FUNC
? type1
: type2
;
5080 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
5081 /* XXX: test structure compatibility */
5082 type
= bt1
== VT_STRUCT
? type1
: type2
;
5083 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
5084 /* NOTE: as an extension, we accept void on only one side */
5087 /* integer operations */
5089 /* convert to unsigned if it does not fit in an integer */
5090 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
5091 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
5092 type
.t
|= VT_UNSIGNED
;
5094 /* keep structs lvalue by transforming `(expr ? a : b)` to `*(expr ? &a : &b)` so
5095 that `(expr ? a : b).mem` does not error with "lvalue expected" */
5096 islv
= (vtop
->r
& VT_LVAL
) && (sv
.r
& VT_LVAL
) && VT_STRUCT
== (type
.t
& VT_BTYPE
);
5098 /* now we convert second operand */
5101 mk_pointer(&vtop
->type
);
5104 else if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
5107 if (is_float(type
.t
)) {
5109 #ifdef TCC_TARGET_X86_64
5110 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
5114 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
5115 /* for long longs, we use fixed registers to avoid having
5116 to handle a complicated move */
5121 /* this is horrible, but we must also convert first
5125 /* put again first value and cast it */
5129 mk_pointer(&vtop
->type
);
5132 else if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
5135 move_reg(r2
, r1
, type
.t
);
5144 static void expr_eq(void)
5150 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
5151 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
5152 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
5167 ST_FUNC
void gexpr(void)
5178 /* parse an expression and return its type without any side effect. */
5179 static void expr_type(CType
*type
)
5181 int saved_nocode_wanted
;
5183 saved_nocode_wanted
= nocode_wanted
;
5188 nocode_wanted
= saved_nocode_wanted
;
5191 /* parse a unary expression and return its type without any side
5193 static void unary_type(CType
*type
)
5205 /* parse a constant expression and return value in vtop. */
5206 static void expr_const1(void)
5215 /* parse an integer constant and return its value. */
5216 ST_FUNC
int expr_const(void)
5220 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
5221 expect("constant expression");
5227 /* return the label token if current token is a label, otherwise
5229 static int is_label(void)
5233 /* fast test first */
5234 if (tok
< TOK_UIDENT
)
5236 /* no need to save tokc because tok is an identifier */
5243 unget_tok(last_tok
);
5248 static void label_or_decl(int l
)
5252 /* fast test first */
5253 if (tok
>= TOK_UIDENT
)
5255 /* no need to save tokc because tok is an identifier */
5259 unget_tok(last_tok
);
5262 unget_tok(last_tok
);
5267 static int case_cmp(const void *pa
, const void *pb
)
5269 int a
= (*(struct case_t
**) pa
)->v1
;
5270 int b
= (*(struct case_t
**) pb
)->v1
;
5271 return a
< b
? -1 : a
> b
;
5274 static int gcase(struct case_t
**base
, int len
, int case_reg
, int *bsym
)
5286 case_reg
= gv(RC_INT
);
5292 gtst_addr(0, p
->sym
); /* v1 <= x <= v2 */
5293 case_reg
= gv(RC_INT
);
5296 case_reg
= gcase(base
, len
/2, case_reg
, bsym
);
5297 if (cur_switch
->def_sym
)
5298 gjmp_addr(cur_switch
->def_sym
);
5300 *bsym
= gjmp(*bsym
);
5304 base
+= e
; len
-= e
;
5312 if (p
->v1
== p
->v2
) {
5314 gtst_addr(0, p
->sym
);
5318 case_reg
= gv(RC_INT
);
5324 gtst_addr(0, p
->sym
);
5327 case_reg
= gv(RC_INT
);
5333 static void block(int *bsym
, int *csym
, int is_expr
)
5338 /* generate line number info */
5339 if (tcc_state
->do_debug
&&
5340 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
5341 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
5343 last_line_num
= file
->line_num
;
5347 /* default return value is (void) */
5349 vtop
->type
.t
= VT_VOID
;
5352 if (tok
== TOK_IF
) {
5359 block(bsym
, csym
, 0);
5361 if (c
== TOK_ELSE
) {
5365 block(bsym
, csym
, 0);
5366 gsym(d
); /* patch else jmp */
5369 } else if (tok
== TOK_WHILE
) {
5385 } else if (tok
== '{') {
5387 int block_vla_sp_loc
= vla_sp_loc
, saved_vlas_in_scope
= vlas_in_scope
;
5390 /* record local declaration stack position */
5392 llabel
= local_label_stack
;
5395 /* handle local labels declarations */
5396 if (tok
== TOK_LABEL
) {
5399 if (tok
< TOK_UIDENT
)
5400 expect("label identifier");
5401 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
5411 while (tok
!= '}') {
5412 label_or_decl(VT_LOCAL
);
5416 block(bsym
, csym
, is_expr
);
5419 /* pop locally defined labels */
5420 label_pop(&local_label_stack
, llabel
);
5421 /* pop locally defined symbols */
5423 /* In the is_expr case (a statement expression is finished here),
5424 vtop might refer to symbols on the local_stack. Either via the
5425 type or via vtop->sym. We can't pop those nor any that in turn
5426 might be referred to. To make it easier we don't roll back
5427 any symbols in that case; some upper level call to block() will
5428 do that. We do have to remove such symbols from the lookup
5429 tables, though. sym_pop will do that. */
5430 sym_pop(&local_stack
, s
, is_expr
);
5432 /* Pop VLA frames and restore stack pointer if required */
5433 if (vlas_in_scope
> saved_vlas_in_scope
) {
5434 vla_sp_loc
= saved_vlas_in_scope
? block_vla_sp_loc
: vla_sp_root_loc
;
5437 vlas_in_scope
= saved_vlas_in_scope
;
5440 } else if (tok
== TOK_RETURN
) {
5444 gen_assign_cast(&func_vt
);
5445 #ifdef TCC_TARGET_ARM64
5446 // Perhaps it would be better to use this for all backends:
5449 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
5450 CType type
, ret_type
;
5451 int ret_align
, ret_nregs
, regsize
;
5452 ret_nregs
= gfunc_sret(&func_vt
, func_var
, &ret_type
,
5453 &ret_align
, ®size
);
5454 if (0 == ret_nregs
) {
5455 /* if returning structure, must copy it to implicit
5456 first pointer arg location */
5459 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
5462 /* copy structure value to pointer */
5465 /* returning structure packed into registers */
5466 int r
, size
, addr
, align
;
5467 size
= type_size(&func_vt
,&align
);
5468 if ((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) ||
5469 (vtop
->c
.i
& (ret_align
-1)))
5470 && (align
& (ret_align
-1))) {
5471 loc
= (loc
- size
) & -ret_align
;
5474 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
5478 vset(&ret_type
, VT_LOCAL
| VT_LVAL
, addr
);
5480 vtop
->type
= ret_type
;
5481 if (is_float(ret_type
.t
))
5482 r
= rc_fret(ret_type
.t
);
5493 if (--ret_nregs
== 0)
5495 /* We assume that when a structure is returned in multiple
5496 registers, their classes are consecutive values of the
5499 vtop
->c
.i
+= regsize
;
5503 } else if (is_float(func_vt
.t
)) {
5504 gv(rc_fret(func_vt
.t
));
5509 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
5512 /* jump unless last stmt in top-level block */
5513 if (tok
!= '}' || local_scope
!= 1)
5515 } else if (tok
== TOK_BREAK
) {
5518 tcc_error("cannot break");
5519 *bsym
= gjmp(*bsym
);
5522 } else if (tok
== TOK_CONTINUE
) {
5525 tcc_error("cannot continue");
5526 vla_sp_restore_root();
5527 *csym
= gjmp(*csym
);
5530 } else if (tok
== TOK_FOR
) {
5537 /* c99 for-loop init decl? */
5538 if (!decl0(VT_LOCAL
, 1)) {
5539 /* no, regular for-loop init expr */
5571 sym_pop(&local_stack
, s
, 0);
5574 if (tok
== TOK_DO
) {
5591 if (tok
== TOK_SWITCH
) {
5592 struct switch_t
*saved
, sw
;
5596 /* XXX: other types than integer */
5601 b
= gjmp(0); /* jump to first case */
5602 sw
.p
= NULL
; sw
.n
= 0; sw
.def_sym
= 0;
5606 a
= gjmp(a
); /* add implicit break */
5609 qsort(sw
.p
, sw
.n
, sizeof(void*), case_cmp
);
5610 for (b
= 1; b
< sw
.n
; b
++)
5611 if (sw
.p
[b
- 1]->v2
>= sw
.p
[b
]->v1
)
5612 tcc_error("duplicate case value");
5613 gcase(sw
.p
, sw
.n
, c
, &a
);
5615 gjmp_addr(sw
.def_sym
);
5616 dynarray_reset(&sw
.p
, &sw
.n
);
5621 if (tok
== TOK_CASE
) {
5622 struct case_t
*cr
= tcc_malloc(sizeof(struct case_t
));
5626 cr
->v1
= cr
->v2
= expr_const();
5627 if (gnu_ext
&& tok
== TOK_DOTS
) {
5629 cr
->v2
= expr_const();
5630 if (cr
->v2
< cr
->v1
)
5631 tcc_warning("empty case range");
5634 dynarray_add((void***) &cur_switch
->p
, &cur_switch
->n
, cr
);
5637 goto block_after_label
;
5639 if (tok
== TOK_DEFAULT
) {
5644 if (cur_switch
->def_sym
)
5645 tcc_error("too many 'default'");
5646 cur_switch
->def_sym
= ind
;
5648 goto block_after_label
;
5650 if (tok
== TOK_GOTO
) {
5652 if (tok
== '*' && gnu_ext
) {
5656 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
5659 } else if (tok
>= TOK_UIDENT
) {
5660 s
= label_find(tok
);
5661 /* put forward definition if needed */
5663 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
5665 if (s
->r
== LABEL_DECLARED
)
5666 s
->r
= LABEL_FORWARD
;
5668 vla_sp_restore_root();
5669 if (s
->r
& LABEL_FORWARD
)
5670 s
->jnext
= gjmp(s
->jnext
);
5672 gjmp_addr(s
->jnext
);
5675 expect("label identifier");
5678 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
5686 if (s
->r
== LABEL_DEFINED
)
5687 tcc_error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
5689 s
->r
= LABEL_DEFINED
;
5691 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
5695 /* we accept this, but it is a mistake */
5698 tcc_warning("deprecated use of label at end of compound statement");
5702 block(bsym
, csym
, is_expr
);
5705 /* expression case */
5720 #define EXPR_CONST 1
5723 static void parse_init_elem(int expr_type
)
5725 int saved_global_expr
;
5728 /* compound literals must be allocated globally in this case */
5729 saved_global_expr
= global_expr
;
5732 global_expr
= saved_global_expr
;
5733 /* NOTE: symbols are accepted */
5734 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
5735 tcc_error("initializer element is not constant");
5743 /* t is the array or struct type. c is the array or struct
5744 address. cur_field is the pointer to the current
5745 value, for arrays the 'c' member contains the current start
5746 index and the 'r' contains the end index (in case of range init).
5747 'size_only' is true if only size info is needed (only used
5749 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
5750 Sym
**cur_field
, int size_only
)
5753 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
5759 if (gnu_ext
&& (l
= is_label()) != 0)
5761 while (tok
== '[' || tok
== '.') {
5763 if (!(type
->t
& VT_ARRAY
))
5764 expect("array type");
5767 index
= expr_const();
5768 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
5769 tcc_error("invalid index");
5770 if (tok
== TOK_DOTS
&& gnu_ext
) {
5772 index_last
= expr_const();
5773 if (index_last
< 0 ||
5774 (s
->c
>= 0 && index_last
>= s
->c
) ||
5776 tcc_error("invalid index");
5782 (*cur_field
)->c
= index
;
5783 (*cur_field
)->r
= index_last
;
5785 type
= pointed_type(type
);
5786 elem_size
= type_size(type
, &align
);
5787 c
+= index
* elem_size
;
5788 /* NOTE: we only support ranges for last designator */
5789 nb_elems
= index_last
- index
+ 1;
5790 if (nb_elems
!= 1) {
5799 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
5800 expect("struct/union type");
5801 f
= find_field(type
, l
);
5806 /* XXX: fix this mess by using explicit storage field */
5808 type1
.t
|= (type
->t
& ~VT_TYPE
);
5822 if (type
->t
& VT_ARRAY
) {
5823 index
= (*cur_field
)->c
;
5824 if (type
->ref
->c
>= 0 && index
>= type
->ref
->c
)
5825 tcc_error("index too large");
5826 type
= pointed_type(type
);
5827 c
+= index
* type_size(type
, &align
);
5831 tcc_error("too many field init");
5832 /* XXX: fix this mess by using explicit storage field */
5834 type1
.t
|= (type
->t
& ~VT_TYPE
);
5839 decl_initializer(type
, sec
, c
, 0, size_only
);
5841 /* XXX: make it more general */
5842 if (!size_only
&& nb_elems
> 1) {
5843 unsigned long c_end
;
5848 vset(type
, VT_LOCAL
|VT_LVAL
, c
);
5849 for (i
= 1; i
< nb_elems
; i
++) {
5850 vset(type
, VT_LOCAL
|VT_LVAL
, c
+ elem_size
* i
);
5856 c_end
= c
+ nb_elems
* elem_size
;
5857 if (c_end
> sec
->data_allocated
)
5858 section_realloc(sec
, c_end
);
5859 src
= sec
->data
+ c
;
5861 for(i
= 1; i
< nb_elems
; i
++) {
5863 memcpy(dst
, src
, elem_size
);
5869 /* store a value or an expression directly in global data or in local array */
5870 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
)
5872 int bt
, bit_pos
, bit_size
;
5874 unsigned long long bit_mask
;
5878 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
5882 /* XXX: not portable */
5883 /* XXX: generate error if incorrect relocation */
5884 gen_assign_cast(&dtype
);
5885 bt
= type
->t
& VT_BTYPE
;
5886 size
= type_size(type
, &align
);
5887 if (c
+ size
> sec
->data_allocated
) {
5888 section_realloc(sec
, c
+ size
);
5890 ptr
= sec
->data
+ c
;
5891 /* XXX: make code faster ? */
5892 if (!(type
->t
& VT_BITFIELD
)) {
5894 bit_size
= PTR_SIZE
* 8;
5897 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5898 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
5899 bit_mask
= (1LL << bit_size
) - 1;
5901 if ((vtop
->r
& (VT_SYM
|VT_CONST
)) == (VT_SYM
|VT_CONST
) &&
5902 vtop
->sym
->v
>= SYM_FIRST_ANOM
&&
5903 /* XXX This rejects compount literals like
5904 '(void *){ptr}'. The problem is that '&sym' is
5905 represented the same way, which would be ruled out
5906 by the SYM_FIRST_ANOM check above, but also '"string"'
5907 in 'char *p = "string"' is represented the same
5908 with the type being VT_PTR and the symbol being an
5909 anonymous one. That is, there's no difference in vtop
5910 between '(void *){x}' and '&(void *){x}'. Ignore
5911 pointer typed entities here. Hopefully no real code
5912 will every use compound literals with scalar type. */
5913 (vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
5914 /* These come from compound literals, memcpy stuff over. */
5917 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[vtop
->sym
->c
];
5918 ssec
= tcc_state
->sections
[esym
->st_shndx
];
5919 memmove (ptr
, ssec
->data
+ esym
->st_value
, size
);
5921 if ((vtop
->r
& VT_SYM
) &&
5927 (bt
== VT_LLONG
&& bit_size
!= 64) ||
5931 (bt
== VT_INT
&& bit_size
!= 32)
5934 tcc_error("initializer element is not computable at load time");
5936 /* XXX: when cross-compiling we assume that each type has the
5937 same representation on host and target, which is likely to
5938 be wrong in the case of long double */
5940 vtop
->c
.i
= (vtop
->c
.i
!= 0);
5942 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5945 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5948 *(double *)ptr
= vtop
->c
.d
;
5951 if (sizeof(long double) == LDOUBLE_SIZE
)
5952 *(long double *)ptr
= vtop
->c
.ld
;
5953 else if (sizeof(double) == LDOUBLE_SIZE
)
5954 *(double *)ptr
= vtop
->c
.ld
;
5956 tcc_error("can't cross compile long double constants");
5960 *(long long *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5967 addr_t val
= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5968 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
5969 if (vtop
->r
& VT_SYM
)
5970 greloca(sec
, vtop
->sym
, c
, R_DATA_PTR
, val
);
5972 *(addr_t
*)ptr
|= val
;
5974 if (vtop
->r
& VT_SYM
)
5975 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
5976 *(addr_t
*)ptr
|= val
;
5982 int val
= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5983 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
5984 if (vtop
->r
& VT_SYM
)
5985 greloca(sec
, vtop
->sym
, c
, R_DATA_PTR
, val
);
5989 if (vtop
->r
& VT_SYM
)
5990 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
5999 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
6006 /* put zeros for variable based init */
6007 static void init_putz(Section
*sec
, unsigned long c
, int size
)
6010 /* nothing to do because globals are already set to zero */
6012 vpush_global_sym(&func_old_type
, TOK_memset
);
6014 #ifdef TCC_TARGET_ARM
6025 /* 't' contains the type and storage info. 'c' is the offset of the
6026 object in section 'sec'. If 'sec' is NULL, it means stack based
6027 allocation. 'first' is true if array '{' must be read (multi
6028 dimension implicit array init handling). 'size_only' is true if
6029 size only evaluation is wanted (only for arrays). */
6030 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
6031 int first
, int size_only
)
6033 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, parlevel1
, i
;
6040 /* If we currently are at an '}' or ',' we have read an initializer
6041 element in one of our callers, and not yet consumed it. */
6042 have_elem
= tok
== '}' || tok
== ',';
6043 if (!have_elem
&& tok
!= '{' &&
6044 /* In case of strings we have special handling for arrays, so
6045 don't consume them as initializer value (which would commit them
6046 to some anonymous symbol). */
6047 tok
!= TOK_LSTR
&& tok
!= TOK_STR
&&
6049 parse_init_elem(!sec
? EXPR_ANY
: EXPR_CONST
);
6054 !(type
->t
& VT_ARRAY
) &&
6055 /* Use i_c_parameter_t, to strip toplevel qualifiers.
6056 The source type might have VT_CONSTANT set, which is
6057 of course assignable to non-const elements. */
6058 is_compatible_parameter_types(type
, &vtop
->type
)) {
6059 init_putv(type
, sec
, c
);
6060 } else if (type
->t
& VT_ARRAY
) {
6064 t1
= pointed_type(type
);
6065 size1
= type_size(t1
, &align1
);
6068 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
6071 tcc_error("character array initializer must be a literal,"
6072 " optionally enclosed in braces");
6077 /* only parse strings here if correct type (otherwise: handle
6078 them as ((w)char *) expressions */
6079 if ((tok
== TOK_LSTR
&&
6080 #ifdef TCC_TARGET_PE
6081 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
6083 (t1
->t
& VT_BTYPE
) == VT_INT
6085 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
6086 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
6089 /* compute maximum number of chars wanted */
6091 cstr_len
= tokc
.str
.size
;
6093 cstr_len
= tokc
.str
.size
/ sizeof(nwchar_t
);
6096 if (n
>= 0 && nb
> (n
- array_length
))
6097 nb
= n
- array_length
;
6100 tcc_warning("initializer-string for array is too long");
6101 /* in order to go faster for common case (char
6102 string in global variable, we handle it
6104 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
6105 memcpy(sec
->data
+ c
+ array_length
, tokc
.str
.data
, nb
);
6109 ch
= ((unsigned char *)tokc
.str
.data
)[i
];
6111 ch
= ((nwchar_t
*)tokc
.str
.data
)[i
];
6113 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
);
6120 /* only add trailing zero if enough storage (no
6121 warning in this case since it is standard) */
6122 if (n
< 0 || array_length
< n
) {
6125 init_putv(t1
, sec
, c
+ (array_length
* size1
));
6135 while (tok
!= '}' || have_elem
) {
6136 decl_designator(type
, sec
, c
, &f
, size_only
);
6139 /* must put zero in holes (note that doing it that way
6140 ensures that it even works with designators) */
6141 if (!size_only
&& array_length
< index
) {
6142 init_putz(sec
, c
+ array_length
* size1
,
6143 (index
- array_length
) * size1
);
6145 if (type
->t
& VT_ARRAY
) {
6146 index
= indexsym
.c
= ++indexsym
.r
;
6148 index
= index
+ type_size(&f
->type
, &align1
);
6149 if (s
->type
.t
== TOK_UNION
)
6154 if (index
> array_length
)
6155 array_length
= index
;
6157 if (type
->t
& VT_ARRAY
) {
6158 /* special test for multi dimensional arrays (may not
6159 be strictly correct if designators are used at the
6161 if (no_oblock
&& index
>= n
)
6164 if (no_oblock
&& f
== NULL
)
6172 /* put zeros at the end */
6173 if (!size_only
&& array_length
< n
) {
6174 init_putz(sec
, c
+ array_length
* size1
,
6175 (n
- array_length
) * size1
);
6179 /* patch type size if needed, which happens only for array types */
6181 s
->c
= array_length
;
6182 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
6185 if (first
|| tok
== '{') {
6194 } else if (tok
== '{') {
6196 decl_initializer(type
, sec
, c
, first
, size_only
);
6198 } else if (size_only
) {
6199 /* If we supported only ISO C we wouldn't have to accept calling
6200 this on anything than an array size_only==1 (and even then
6201 only on the outermost level, so no recursion would be needed),
6202 because initializing a flex array member isn't supported.
6203 But GNU C supports it, so we need to recurse even into
6204 subfields of structs and arrays when size_only is set. */
6205 /* just skip expression */
6206 parlevel
= parlevel1
= 0;
6207 while ((parlevel
> 0 || parlevel1
> 0 ||
6208 (tok
!= '}' && tok
!= ',')) && tok
!= -1) {
6211 else if (tok
== ')') {
6212 if (parlevel
== 0 && parlevel1
== 0)
6216 else if (tok
== '{')
6218 else if (tok
== '}') {
6219 if (parlevel
== 0 && parlevel1
== 0)
6227 /* This should happen only when we haven't parsed
6228 the init element above for fear of committing a
6229 string constant to memory too early. */
6230 if (tok
!= TOK_STR
&& tok
!= TOK_LSTR
)
6231 expect("string constant");
6232 parse_init_elem(!sec
? EXPR_ANY
: EXPR_CONST
);
6234 init_putv(type
, sec
, c
);
6238 /* parse an initializer for type 't' if 'has_init' is non zero, and
6239 allocate space in local or global data space ('r' is either
6240 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
6241 variable 'v' of scope 'scope' is declared before initializers
6242 are parsed. If 'v' is zero, then a reference to the new object
6243 is put in the value stack. If 'has_init' is 2, a special parsing
6244 is done to handle string constants. */
6245 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
6246 int has_init
, int v
, int scope
)
6248 int size
, align
, addr
, data_offset
;
6250 ParseState saved_parse_state
= {0};
6251 TokenString
*init_str
= NULL
;
6253 Sym
*flexible_array
;
6255 flexible_array
= NULL
;
6256 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
6257 Sym
*field
= type
->ref
->next
;
6260 field
= field
->next
;
6261 if (field
->type
.t
& VT_ARRAY
&& field
->type
.ref
->c
< 0)
6262 flexible_array
= field
;
6266 size
= type_size(type
, &align
);
6267 /* If unknown size, we must evaluate it before
6268 evaluating initializers because
6269 initializers can generate global data too
6270 (e.g. string pointers or ISOC99 compound
6271 literals). It also simplifies local
6272 initializers handling */
6273 if (size
< 0 || (flexible_array
&& has_init
)) {
6275 tcc_error("unknown type size");
6276 /* get all init string */
6277 init_str
= tok_str_alloc();
6278 if (has_init
== 2) {
6279 /* only get strings */
6280 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
6281 tok_str_add_tok(init_str
);
6286 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
6288 tcc_error("unexpected end of file in initializer");
6289 tok_str_add_tok(init_str
);
6292 else if (tok
== '}') {
6302 tok_str_add(init_str
, -1);
6303 tok_str_add(init_str
, 0);
6306 save_parse_state(&saved_parse_state
);
6308 begin_macro(init_str
, 1);
6310 decl_initializer(type
, NULL
, 0, 1, 1);
6311 /* prepare second initializer parsing */
6312 macro_ptr
= init_str
->str
;
6315 /* if still unknown size, error */
6316 size
= type_size(type
, &align
);
6318 tcc_error("unknown type size");
6320 /* If there's a flex member and it was used in the initializer
6322 if (flexible_array
&&
6323 flexible_array
->type
.ref
->c
> 0)
6324 size
+= flexible_array
->type
.ref
->c
6325 * pointed_size(&flexible_array
->type
);
6326 /* take into account specified alignment if bigger */
6327 if (ad
->a
.aligned
) {
6328 if (ad
->a
.aligned
> align
)
6329 align
= ad
->a
.aligned
;
6330 } else if (ad
->a
.packed
) {
6333 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
6335 #ifdef CONFIG_TCC_BCHECK
6336 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
6340 loc
= (loc
- size
) & -align
;
6342 #ifdef CONFIG_TCC_BCHECK
6343 /* handles bounds */
6344 /* XXX: currently, since we do only one pass, we cannot track
6345 '&' operators, so we add only arrays */
6346 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
6348 /* add padding between regions */
6350 /* then add local bound info */
6351 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(addr_t
));
6352 bounds_ptr
[0] = addr
;
6353 bounds_ptr
[1] = size
;
6357 /* local variable */
6358 sym_push(v
, type
, r
, addr
);
6360 /* push local reference */
6361 vset(type
, r
, addr
);
6367 if (v
&& scope
== VT_CONST
) {
6368 /* see if the symbol was already defined */
6371 if (!is_compatible_types(&sym
->type
, type
))
6372 tcc_error("incompatible types for redefinition of '%s'",
6373 get_tok_str(v
, NULL
));
6374 if (sym
->type
.t
& VT_EXTERN
) {
6375 /* if the variable is extern, it was not allocated */
6376 sym
->type
.t
&= ~VT_EXTERN
;
6377 /* set array size if it was omitted in extern
6379 if ((sym
->type
.t
& VT_ARRAY
) &&
6380 sym
->type
.ref
->c
< 0 &&
6382 sym
->type
.ref
->c
= type
->ref
->c
;
6384 /* we accept several definitions of the same
6385 global variable. this is tricky, because we
6386 must play with the SHN_COMMON type of the symbol */
6387 /* XXX: should check if the variable was already
6388 initialized. It is incorrect to initialized it
6390 /* no init data, we won't add more to the symbol */
6397 /* allocate symbol in corresponding section */
6402 else if (tcc_state
->nocommon
)
6406 data_offset
= sec
->data_offset
;
6407 data_offset
= (data_offset
+ align
- 1) & -align
;
6409 /* very important to increment global pointer at this time
6410 because initializers themselves can create new initializers */
6411 data_offset
+= size
;
6412 #ifdef CONFIG_TCC_BCHECK
6413 /* add padding if bound check */
6414 if (tcc_state
->do_bounds_check
)
6417 sec
->data_offset
= data_offset
;
6418 /* allocate section space to put the data */
6419 if (sec
->sh_type
!= SHT_NOBITS
&&
6420 data_offset
> sec
->data_allocated
)
6421 section_realloc(sec
, data_offset
);
6422 /* align section if needed */
6423 if (align
> sec
->sh_addralign
)
6424 sec
->sh_addralign
= align
;
6426 addr
= 0; /* avoid warning */
6430 if (scope
!= VT_CONST
|| !sym
) {
6431 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
6432 sym
->asm_label
= ad
->asm_label
;
6434 /* update symbol definition */
6436 put_extern_sym(sym
, sec
, addr
, size
);
6439 /* put a common area */
6440 put_extern_sym(sym
, NULL
, align
, size
);
6441 /* XXX: find a nicer way */
6442 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
6443 esym
->st_shndx
= SHN_COMMON
;
6446 /* push global reference */
6447 sym
= get_sym_ref(type
, sec
, addr
, size
);
6448 vpushsym(type
, sym
);
6450 /* patch symbol weakness */
6451 if (type
->t
& VT_WEAK
)
6453 apply_visibility(sym
, type
);
6454 #ifdef CONFIG_TCC_BCHECK
6455 /* handles bounds now because the symbol must be defined
6456 before for the relocation */
6457 if (tcc_state
->do_bounds_check
) {
6460 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
6461 /* then add global bound info */
6462 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(addr_t
));
6463 bounds_ptr
[0] = 0; /* relocated */
6464 bounds_ptr
[1] = size
;
6468 if (type
->t
& VT_VLA
) {
6471 /* save current stack pointer */
6472 if (vlas_in_scope
== 0) {
6473 if (vla_sp_root_loc
== -1)
6474 vla_sp_root_loc
= (loc
-= PTR_SIZE
);
6475 gen_vla_sp_save(vla_sp_root_loc
);
6478 vla_runtime_type_size(type
, &a
);
6479 gen_vla_alloc(type
, a
);
6480 gen_vla_sp_save(addr
);
6483 } else if (has_init
) {
6484 decl_initializer(type
, sec
, addr
, 1, 0);
6485 /* patch flexible array member size back to -1, */
6486 /* for possible subsequent similar declarations */
6488 flexible_array
->type
.ref
->c
= -1;
6491 /* restore parse state if needed */
6494 restore_parse_state(&saved_parse_state
);
6498 static void put_func_debug(Sym
*sym
)
6503 /* XXX: we put here a dummy type */
6504 snprintf(buf
, sizeof(buf
), "%s:%c1",
6505 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
6506 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
6507 cur_text_section
, sym
->c
);
6508 /* //gr gdb wants a line at the function */
6509 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
6514 /* parse an old style function declaration list */
6515 /* XXX: check multiple parameter */
6516 static void func_decl_list(Sym
*func_sym
)
6523 /* parse each declaration */
6524 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
&&
6525 tok
!= TOK_ASM1
&& tok
!= TOK_ASM2
&& tok
!= TOK_ASM3
) {
6526 if (!parse_btype(&btype
, &ad
))
6527 expect("declaration list");
6528 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
6529 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
6531 /* we accept no variable after */
6535 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
6536 /* find parameter in function parameter list */
6539 if ((s
->v
& ~SYM_FIELD
) == v
)
6543 tcc_error("declaration for parameter '%s' but no such parameter",
6544 get_tok_str(v
, NULL
));
6546 /* check that no storage specifier except 'register' was given */
6547 if (type
.t
& VT_STORAGE
)
6548 tcc_error("storage class specified for '%s'", get_tok_str(v
, NULL
));
6549 convert_parameter_type(&type
);
6550 /* we can add the type (NOTE: it could be local to the function) */
6552 /* accept other parameters */
6563 /* parse a function defined by symbol 'sym' and generate its code in
6564 'cur_text_section' */
6565 static void gen_function(Sym
*sym
)
6567 int saved_nocode_wanted
= nocode_wanted
;
6570 ind
= cur_text_section
->data_offset
;
6571 /* NOTE: we patch the symbol size later */
6572 put_extern_sym(sym
, cur_text_section
, ind
, 0);
6573 funcname
= get_tok_str(sym
->v
, NULL
);
6575 /* Initialize VLA state */
6577 vla_sp_root_loc
= -1;
6578 /* put debug symbol */
6579 if (tcc_state
->do_debug
)
6580 put_func_debug(sym
);
6582 /* push a dummy symbol to enable local sym storage */
6583 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
6584 local_scope
= 1; /* for function parameters */
6585 gfunc_prolog(&sym
->type
);
6589 block(NULL
, NULL
, 0);
6592 cur_text_section
->data_offset
= ind
;
6593 label_pop(&global_label_stack
, NULL
);
6594 /* reset local stack */
6596 sym_pop(&local_stack
, NULL
, 0);
6597 /* end of function */
6598 /* patch symbol size */
6599 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
6601 /* patch symbol weakness (this definition overrules any prototype) */
6602 if (sym
->type
.t
& VT_WEAK
)
6604 apply_visibility(sym
, &sym
->type
);
6605 if (tcc_state
->do_debug
) {
6606 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
6608 /* It's better to crash than to generate wrong code */
6609 cur_text_section
= NULL
;
6610 funcname
= ""; /* for safety */
6611 func_vt
.t
= VT_VOID
; /* for safety */
6612 func_var
= 0; /* for safety */
6613 ind
= 0; /* for safety */
6614 nocode_wanted
= saved_nocode_wanted
;
6618 static void gen_inline_functions(TCCState
*s
)
6621 int inline_generated
, i
, ln
;
6622 struct InlineFunc
*fn
;
6624 ln
= file
->line_num
;
6625 /* iterate while inline function are referenced */
6627 inline_generated
= 0;
6628 for (i
= 0; i
< s
->nb_inline_fns
; ++i
) {
6629 fn
= s
->inline_fns
[i
];
6631 if (sym
&& sym
->c
) {
6632 /* the function was used: generate its code and
6633 convert it to a normal function */
6636 pstrcpy(file
->filename
, sizeof file
->filename
, fn
->filename
);
6637 sym
->r
= VT_SYM
| VT_CONST
;
6638 sym
->type
.t
&= ~VT_INLINE
;
6640 begin_macro(fn
->func_str
, 1);
6642 cur_text_section
= text_section
;
6646 inline_generated
= 1;
6649 if (!inline_generated
)
6652 file
->line_num
= ln
;
6655 ST_FUNC
void free_inline_functions(TCCState
*s
)
6658 /* free tokens of unused inline functions */
6659 for (i
= 0; i
< s
->nb_inline_fns
; ++i
) {
6660 struct InlineFunc
*fn
= s
->inline_fns
[i
];
6662 tok_str_free(fn
->func_str
);
6664 dynarray_reset(&s
->inline_fns
, &s
->nb_inline_fns
);
6667 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
6668 static int decl0(int l
, int is_for_loop_init
)
6676 if (!parse_btype(&btype
, &ad
)) {
6677 if (is_for_loop_init
)
6679 /* skip redundant ';' */
6680 /* XXX: find more elegant solution */
6685 if (l
== VT_CONST
&&
6686 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
6687 /* global asm block */
6691 /* special test for old K&R protos without explicit int
6692 type. Only accepted when defining global data */
6693 if (l
== VT_LOCAL
|| tok
< TOK_UIDENT
)
6697 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
6698 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
6700 if ((btype
.t
& VT_BTYPE
) == VT_STRUCT
) {
6701 int v
= btype
.ref
->v
;
6702 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) >= SYM_FIRST_ANOM
)
6703 tcc_warning("unnamed struct/union that defines no instances");
6708 while (1) { /* iterate thru each declaration */
6710 /* If the base type itself was an array type of unspecified
6711 size (like in 'typedef int arr[]; arr x = {1};') then
6712 we will overwrite the unknown size by the real one for
6713 this decl. We need to unshare the ref symbol holding
6715 if ((type
.t
& VT_ARRAY
) && type
.ref
->c
< 0) {
6716 type
.ref
= sym_push(SYM_FIELD
, &type
.ref
->type
, 0, type
.ref
->c
);
6718 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
6722 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
6723 printf("type = '%s'\n", buf
);
6726 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
6727 if ((type
.t
& VT_STATIC
) && (l
== VT_LOCAL
)) {
6728 tcc_error("function without file scope cannot be static");
6730 /* if old style function prototype, we accept a
6733 if (sym
->c
== FUNC_OLD
)
6734 func_decl_list(sym
);
6737 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
6738 ad
.asm_label
= asm_label_instr();
6739 /* parse one last attribute list, after asm label */
6740 parse_attribute(&ad
);
6747 #ifdef TCC_TARGET_PE
6748 if (ad
.a
.func_import
)
6749 type
.t
|= VT_IMPORT
;
6750 if (ad
.a
.func_export
)
6751 type
.t
|= VT_EXPORT
;
6753 type
.t
|= ad
.a
.visibility
<< VT_VIS_SHIFT
;
6757 tcc_error("cannot use local functions");
6758 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
6759 expect("function definition");
6761 /* reject abstract declarators in function definition */
6763 while ((sym
= sym
->next
) != NULL
)
6764 if (!(sym
->v
& ~SYM_FIELD
))
6765 expect("identifier");
6767 /* XXX: cannot do better now: convert extern line to static inline */
6768 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
6769 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
6774 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
6777 ref
= sym
->type
.ref
;
6778 if (0 == ref
->a
.func_proto
)
6779 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
6781 /* use func_call from prototype if not defined */
6782 if (ref
->a
.func_call
!= FUNC_CDECL
6783 && type
.ref
->a
.func_call
== FUNC_CDECL
)
6784 type
.ref
->a
.func_call
= ref
->a
.func_call
;
6786 /* use export from prototype */
6787 if (ref
->a
.func_export
)
6788 type
.ref
->a
.func_export
= 1;
6790 /* use static from prototype */
6791 if (sym
->type
.t
& VT_STATIC
)
6792 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
6794 /* If the definition has no visibility use the
6795 one from prototype. */
6796 if (! (type
.t
& VT_VIS_MASK
))
6797 type
.t
|= sym
->type
.t
& VT_VIS_MASK
;
6799 if (!is_compatible_types(&sym
->type
, &type
)) {
6801 tcc_error("incompatible types for redefinition of '%s'",
6802 get_tok_str(v
, NULL
));
6804 type
.ref
->a
.func_proto
= 0;
6805 /* if symbol is already defined, then put complete type */
6808 /* put function symbol */
6809 sym
= global_identifier_push(v
, type
.t
, 0);
6810 sym
->type
.ref
= type
.ref
;
6813 /* static inline functions are just recorded as a kind
6814 of macro. Their code will be emitted at the end of
6815 the compilation unit only if they are used */
6816 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
6817 (VT_INLINE
| VT_STATIC
)) {
6819 struct InlineFunc
*fn
;
6820 const char *filename
;
6822 filename
= file
? file
->filename
: "";
6823 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
6824 strcpy(fn
->filename
, filename
);
6826 fn
->func_str
= tok_str_alloc();
6832 tcc_error("unexpected end of file");
6833 tok_str_add_tok(fn
->func_str
);
6838 } else if (t
== '}') {
6840 if (block_level
== 0)
6844 tok_str_add(fn
->func_str
, -1);
6845 tok_str_add(fn
->func_str
, 0);
6846 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
6849 /* compute text section */
6850 cur_text_section
= ad
.section
;
6851 if (!cur_text_section
)
6852 cur_text_section
= text_section
;
6853 sym
->r
= VT_SYM
| VT_CONST
;
6858 if (btype
.t
& VT_TYPEDEF
) {
6859 /* save typedefed type */
6860 /* XXX: test storage specifiers ? */
6862 if (sym
&& sym
->scope
== local_scope
) {
6863 if (!is_compatible_types(&sym
->type
, &type
)
6864 || !(sym
->type
.t
& VT_TYPEDEF
))
6865 tcc_error("incompatible redefinition of '%s'",
6866 get_tok_str(v
, NULL
));
6869 sym
= sym_push(v
, &type
, 0, 0);
6872 sym
->type
.t
|= VT_TYPEDEF
;
6875 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
6876 /* external function definition */
6877 /* specific case for func_call attribute */
6878 ad
.a
.func_proto
= 1;
6880 } else if (!(type
.t
& VT_ARRAY
)) {
6881 /* not lvalue if array */
6882 r
|= lvalue_type(type
.t
);
6884 has_init
= (tok
== '=');
6885 if (has_init
&& (type
.t
& VT_VLA
))
6886 tcc_error("variable length array cannot be initialized");
6887 if ((btype
.t
& VT_EXTERN
) || ((type
.t
& VT_BTYPE
) == VT_FUNC
) ||
6888 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
6889 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
6890 /* external variable or function */
6891 /* NOTE: as GCC, uninitialized global static
6892 arrays of null size are considered as
6894 sym
= external_sym(v
, &type
, r
);
6895 sym
->asm_label
= ad
.asm_label
;
6897 if (ad
.alias_target
) {
6902 alias_target
= sym_find(ad
.alias_target
);
6903 if (!alias_target
|| !alias_target
->c
)
6904 tcc_error("unsupported forward __alias__ attribute");
6905 esym
= &((Elf32_Sym
*)symtab_section
->data
)[alias_target
->c
];
6906 tsec
.sh_num
= esym
->st_shndx
;
6907 put_extern_sym2(sym
, &tsec
, esym
->st_value
, esym
->st_size
, 0);
6910 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
6911 if (type
.t
& VT_STATIC
)
6917 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, l
);
6921 if (is_for_loop_init
)
6934 ST_FUNC
void decl(int l
)
6939 /* ------------------------------------------------------------------------- */