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)
361 sym
= sym_free_first
;
363 sym
= __sym_malloc();
364 sym_free_first
= sym
->next
;
368 ST_INLN
void sym_free(Sym
*sym
)
370 sym
->next
= sym_free_first
;
371 sym_free_first
= sym
;
374 /* push, without hashing */
375 ST_FUNC Sym
*sym_push2(Sym
**ps
, int v
, int t
, long c
)
395 /* find a symbol and return its associated structure. 's' is the top
396 of the symbol stack */
397 ST_FUNC Sym
*sym_find2(Sym
*s
, int v
)
409 /* structure lookup */
410 ST_INLN Sym
*struct_find(int v
)
413 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
415 return table_ident
[v
]->sym_struct
;
418 /* find an identifier */
419 ST_INLN Sym
*sym_find(int v
)
422 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
424 return table_ident
[v
]->sym_identifier
;
427 /* push a given symbol on the symbol stack */
428 ST_FUNC Sym
*sym_push(int v
, CType
*type
, int r
, int c
)
437 s
= sym_push2(ps
, v
, type
->t
, c
);
438 s
->type
.ref
= type
->ref
;
440 /* don't record fields or anonymous symbols */
442 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
443 /* record symbol in token array */
444 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
446 ps
= &ts
->sym_struct
;
448 ps
= &ts
->sym_identifier
;
451 s
->scope
= local_scope
;
452 if (s
->prev_tok
&& s
->prev_tok
->scope
== s
->scope
)
453 tcc_error("redeclaration of '%s'",
454 get_tok_str(v
& ~SYM_STRUCT
, NULL
));
459 /* push a global identifier */
460 ST_FUNC Sym
*global_identifier_push(int v
, int t
, int c
)
463 s
= sym_push2(&global_stack
, v
, t
, c
);
464 /* don't record anonymous symbol */
465 if (v
< SYM_FIRST_ANOM
) {
466 ps
= &table_ident
[v
- TOK_IDENT
]->sym_identifier
;
467 /* modify the top most local identifier, so that
468 sym_identifier will point to 's' when popped */
470 ps
= &(*ps
)->prev_tok
;
477 /* pop symbols until top reaches 'b' */
478 ST_FUNC
void sym_pop(Sym
**ptop
, Sym
*b
)
488 /* remove symbol in token array */
490 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
491 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
493 ps
= &ts
->sym_struct
;
495 ps
= &ts
->sym_identifier
;
504 static void weaken_symbol(Sym
*sym
)
506 sym
->type
.t
|= VT_WEAK
;
511 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
512 esym_type
= ELFW(ST_TYPE
)(esym
->st_info
);
513 esym
->st_info
= ELFW(ST_INFO
)(STB_WEAK
, esym_type
);
517 static void apply_visibility(Sym
*sym
, CType
*type
)
519 int vis
= sym
->type
.t
& VT_VIS_MASK
;
520 int vis2
= type
->t
& VT_VIS_MASK
;
521 if (vis
== (STV_DEFAULT
<< VT_VIS_SHIFT
))
523 else if (vis2
== (STV_DEFAULT
<< VT_VIS_SHIFT
))
526 vis
= (vis
< vis2
) ? vis
: vis2
;
527 sym
->type
.t
&= ~VT_VIS_MASK
;
533 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
534 vis
>>= VT_VIS_SHIFT
;
535 esym
->st_other
= (esym
->st_other
& ~ELFW(ST_VISIBILITY
)(-1)) | vis
;
539 /* ------------------------------------------------------------------------- */
541 ST_FUNC
void swap(int *p
, int *q
)
549 static void vsetc(CType
*type
, int r
, CValue
*vc
)
553 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
554 tcc_error("memory full (vstack)");
555 /* cannot let cpu flags if other instruction are generated. Also
556 avoid leaving VT_JMP anywhere except on the top of the stack
557 because it would complicate the code generator. */
558 if (vtop
>= vstack
) {
559 v
= vtop
->r
& VT_VALMASK
;
560 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
570 /* push constant of type "type" with useless value */
571 ST_FUNC
void vpush(CType
*type
)
574 vsetc(type
, VT_CONST
, &cval
);
577 /* push integer constant */
578 ST_FUNC
void vpushi(int v
)
582 vsetc(&int_type
, VT_CONST
, &cval
);
585 /* push a pointer sized constant */
586 static void vpushs(addr_t v
)
590 vsetc(&size_type
, VT_CONST
, &cval
);
593 /* push arbitrary 64bit constant */
594 ST_FUNC
void vpush64(int ty
, unsigned long long v
)
601 vsetc(&ctype
, VT_CONST
, &cval
);
604 /* push long long constant */
605 static inline void vpushll(long long v
)
607 vpush64(VT_LLONG
, v
);
610 /* push a symbol value of TYPE */
611 static inline void vpushsym(CType
*type
, Sym
*sym
)
615 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
619 /* Return a static symbol pointing to a section */
620 ST_FUNC Sym
*get_sym_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
626 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
627 sym
->type
.ref
= type
->ref
;
628 sym
->r
= VT_CONST
| VT_SYM
;
629 put_extern_sym(sym
, sec
, offset
, size
);
633 /* push a reference to a section offset by adding a dummy symbol */
634 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
636 vpushsym(type
, get_sym_ref(type
, sec
, offset
, size
));
639 /* define a new external reference to a symbol 'v' of type 'u' */
640 ST_FUNC Sym
*external_global_sym(int v
, CType
*type
, int r
)
646 /* push forward reference */
647 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
648 s
->type
.ref
= type
->ref
;
649 s
->r
= r
| VT_CONST
| VT_SYM
;
654 /* define a new external reference to a symbol 'v' */
655 static Sym
*external_sym(int v
, CType
*type
, int r
)
661 /* push forward reference */
662 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
663 s
->type
.t
|= VT_EXTERN
;
664 } else if (s
->type
.ref
== func_old_type
.ref
) {
665 s
->type
.ref
= type
->ref
;
666 s
->r
= r
| VT_CONST
| VT_SYM
;
667 s
->type
.t
|= VT_EXTERN
;
668 } else if (!is_compatible_types(&s
->type
, type
)) {
669 tcc_error("incompatible types for redefinition of '%s'",
670 get_tok_str(v
, NULL
));
672 /* Merge some storage attributes. */
673 if (type
->t
& VT_WEAK
)
676 if (type
->t
& VT_VIS_MASK
)
677 apply_visibility(s
, type
);
682 /* push a reference to global symbol v */
683 ST_FUNC
void vpush_global_sym(CType
*type
, int v
)
685 vpushsym(type
, external_global_sym(v
, type
, 0));
688 ST_FUNC
void vset(CType
*type
, int r
, int v
)
693 vsetc(type
, r
, &cval
);
696 static void vseti(int r
, int v
)
704 ST_FUNC
void vswap(void)
707 /* cannot let cpu flags if other instruction are generated. Also
708 avoid leaving VT_JMP anywhere except on the top of the stack
709 because it would complicate the code generator. */
710 if (vtop
>= vstack
) {
711 int v
= vtop
->r
& VT_VALMASK
;
712 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
719 /* XXX: +2% overall speed possible with optimized memswap
721 * memswap(&vtop[0], &vtop[1], sizeof *vtop);
725 ST_FUNC
void vpushv(SValue
*v
)
727 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
728 tcc_error("memory full (vstack)");
733 static void vdup(void)
738 /* save registers up to (vtop - n) stack entry */
739 ST_FUNC
void save_regs(int n
)
742 for(p
= vstack
, p1
= vtop
- n
; p
<= p1
; p
++)
746 /* save r to the memory stack, and mark it as being free */
747 ST_FUNC
void save_reg(int r
)
749 save_reg_upstack(r
, 0);
752 /* save r to the memory stack, and mark it as being free,
753 if seen up to (vtop - n) stack entry */
754 ST_FUNC
void save_reg_upstack(int r
, int n
)
756 int l
, saved
, size
, align
;
760 if ((r
&= VT_VALMASK
) >= VT_CONST
)
763 /* modify all stack values */
766 for(p
= vstack
, p1
= vtop
- n
; p
<= p1
; p
++) {
767 if ((p
->r
& VT_VALMASK
) == r
||
768 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
769 /* must save value on stack if not already done */
771 /* NOTE: must reload 'r' because r might be equal to r2 */
772 r
= p
->r
& VT_VALMASK
;
773 /* store register in the stack */
775 if ((p
->r
& VT_LVAL
) ||
776 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
777 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
778 type
= &char_pointer_type
;
782 size
= type_size(type
, &align
);
783 loc
= (loc
- size
) & -align
;
785 sv
.r
= VT_LOCAL
| VT_LVAL
;
788 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
789 /* x86 specific: need to pop fp register ST0 if saved */
791 o(0xd8dd); /* fstp %st(0) */
794 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
795 /* special long long case */
796 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
804 /* mark that stack entry as being saved on the stack */
805 if (p
->r
& VT_LVAL
) {
806 /* also clear the bounded flag because the
807 relocation address of the function was stored in
809 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
811 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
819 #ifdef TCC_TARGET_ARM
820 /* find a register of class 'rc2' with at most one reference on stack.
821 * If none, call get_reg(rc) */
822 ST_FUNC
int get_reg_ex(int rc
, int rc2
)
827 for(r
=0;r
<NB_REGS
;r
++) {
828 if (reg_classes
[r
] & rc2
) {
831 for(p
= vstack
; p
<= vtop
; p
++) {
832 if ((p
->r
& VT_VALMASK
) == r
||
833 (p
->r2
& VT_VALMASK
) == r
)
844 /* find a free register of class 'rc'. If none, save one register */
845 ST_FUNC
int get_reg(int rc
)
850 /* find a free register */
851 for(r
=0;r
<NB_REGS
;r
++) {
852 if (reg_classes
[r
] & rc
) {
853 for(p
=vstack
;p
<=vtop
;p
++) {
854 if ((p
->r
& VT_VALMASK
) == r
||
855 (p
->r2
& VT_VALMASK
) == r
)
863 /* no register left : free the first one on the stack (VERY
864 IMPORTANT to start from the bottom to ensure that we don't
865 spill registers used in gen_opi()) */
866 for(p
=vstack
;p
<=vtop
;p
++) {
867 /* look at second register (if long long) */
868 r
= p
->r2
& VT_VALMASK
;
869 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
871 r
= p
->r
& VT_VALMASK
;
872 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
878 /* Should never comes here */
882 /* move register 's' (of type 't') to 'r', and flush previous value of r to memory
884 static void move_reg(int r
, int s
, int t
)
898 /* get address of vtop (vtop MUST BE an lvalue) */
899 ST_FUNC
void gaddrof(void)
901 if (vtop
->r
& VT_REF
&& !nocode_wanted
)
904 /* tricky: if saved lvalue, then we can go back to lvalue */
905 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
906 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
911 #ifdef CONFIG_TCC_BCHECK
912 /* generate lvalue bound code */
913 static void gbound(void)
918 vtop
->r
&= ~VT_MUSTBOUND
;
919 /* if lvalue, then use checking code before dereferencing */
920 if (vtop
->r
& VT_LVAL
) {
921 /* if not VT_BOUNDED value, then make one */
922 if (!(vtop
->r
& VT_BOUNDED
)) {
923 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
924 /* must save type because we must set it to int to get pointer */
926 vtop
->type
.t
= VT_PTR
;
929 gen_bounded_ptr_add();
930 vtop
->r
|= lval_type
;
933 /* then check for dereferencing */
934 gen_bounded_ptr_deref();
939 /* store vtop a register belonging to class 'rc'. lvalues are
940 converted to values. Cannot be used if cannot be converted to
941 register value (such as structures). */
942 ST_FUNC
int gv(int rc
)
944 int r
, bit_pos
, bit_size
, size
, align
, i
;
947 /* NOTE: get_reg can modify vstack[] */
948 if (vtop
->type
.t
& VT_BITFIELD
) {
951 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
952 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
953 /* remove bit field info to avoid loops */
954 vtop
->type
.t
&= ~VT_BITFIELD
& ((1 << VT_STRUCT_SHIFT
) - 1);
955 /* cast to int to propagate signedness in following ops */
956 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
961 if((vtop
->type
.t
& VT_UNSIGNED
) ||
962 (vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
963 type
.t
|= VT_UNSIGNED
;
965 /* generate shifts */
966 vpushi(bits
- (bit_pos
+ bit_size
));
968 vpushi(bits
- bit_size
);
969 /* NOTE: transformed to SHR if unsigned */
973 if (is_float(vtop
->type
.t
) &&
974 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
977 unsigned long offset
;
978 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
982 /* XXX: unify with initializers handling ? */
983 /* CPUs usually cannot use float constants, so we store them
984 generically in data segment */
985 size
= type_size(&vtop
->type
, &align
);
986 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
987 data_section
->data_offset
= offset
;
988 /* XXX: not portable yet */
989 #if defined(__i386__) || defined(__x86_64__)
990 /* Zero pad x87 tenbyte long doubles */
991 if (size
== LDOUBLE_SIZE
) {
992 vtop
->c
.tab
[2] &= 0xffff;
993 #if LDOUBLE_SIZE == 16
998 ptr
= section_ptr_add(data_section
, size
);
1000 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
1004 ptr
[i
] = vtop
->c
.tab
[size
-1-i
];
1008 ptr
[i
] = vtop
->c
.tab
[i
];
1009 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
1010 vtop
->r
|= VT_LVAL
| VT_SYM
;
1014 #ifdef CONFIG_TCC_BCHECK
1015 if (vtop
->r
& VT_MUSTBOUND
)
1019 r
= vtop
->r
& VT_VALMASK
;
1020 rc2
= (rc
& RC_FLOAT
) ? RC_FLOAT
: RC_INT
;
1021 #ifndef TCC_TARGET_ARM64
1024 #ifdef TCC_TARGET_X86_64
1025 else if (rc
== RC_FRET
)
1030 /* need to reload if:
1032 - lvalue (need to dereference pointer)
1033 - already a register, but not in the right class */
1035 || (vtop
->r
& VT_LVAL
)
1036 || !(reg_classes
[r
] & rc
)
1037 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1038 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
1039 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
&& !(reg_classes
[vtop
->r2
] & rc2
))
1041 || ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
1046 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1047 if (((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
)) {
1048 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
1050 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
1051 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
1052 unsigned long long ll
;
1054 int r2
, original_type
;
1055 original_type
= vtop
->type
.t
;
1056 /* two register type load : expand to two words
1058 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
1059 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
1062 vtop
->c
.i
= ll
; /* first word */
1064 vtop
->r
= r
; /* save register value */
1065 vpushi(ll
>> 32); /* second word */
1068 if (vtop
->r
& VT_LVAL
) {
1069 /* We do not want to modifier the long long
1070 pointer here, so the safest (and less
1071 efficient) is to save all the other registers
1072 in the stack. XXX: totally inefficient. */
1076 /* lvalue_save: save only if used further down the stack */
1077 save_reg_upstack(vtop
->r
, 1);
1079 /* load from memory */
1080 vtop
->type
.t
= load_type
;
1083 vtop
[-1].r
= r
; /* save register value */
1084 /* increment pointer to get second word */
1085 vtop
->type
.t
= addr_type
;
1090 vtop
->type
.t
= load_type
;
1092 /* move registers */
1095 vtop
[-1].r
= r
; /* save register value */
1096 vtop
->r
= vtop
[-1].r2
;
1098 /* Allocate second register. Here we rely on the fact that
1099 get_reg() tries first to free r2 of an SValue. */
1103 /* write second register */
1105 vtop
->type
.t
= original_type
;
1106 } else if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
1108 /* lvalue of scalar type : need to use lvalue type
1109 because of possible cast */
1112 /* compute memory access type */
1113 if (vtop
->r
& VT_REF
)
1114 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1119 else if (vtop
->r
& VT_LVAL_BYTE
)
1121 else if (vtop
->r
& VT_LVAL_SHORT
)
1123 if (vtop
->r
& VT_LVAL_UNSIGNED
)
1127 /* restore wanted type */
1130 /* one register type load */
1135 #ifdef TCC_TARGET_C67
1136 /* uses register pairs for doubles */
1137 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
1144 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
1145 ST_FUNC
void gv2(int rc1
, int rc2
)
1149 /* generate more generic register first. But VT_JMP or VT_CMP
1150 values must be generated first in all cases to avoid possible
1152 v
= vtop
[0].r
& VT_VALMASK
;
1153 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
1158 /* test if reload is needed for first register */
1159 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
1169 /* test if reload is needed for first register */
1170 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
1176 #ifndef TCC_TARGET_ARM64
1177 /* wrapper around RC_FRET to return a register by type */
1178 static int rc_fret(int t
)
1180 #ifdef TCC_TARGET_X86_64
1181 if (t
== VT_LDOUBLE
) {
1189 /* wrapper around REG_FRET to return a register by type */
1190 static int reg_fret(int t
)
1192 #ifdef TCC_TARGET_X86_64
1193 if (t
== VT_LDOUBLE
) {
1200 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
1201 /* expand 64bit on stack in two ints */
1202 static void lexpand(void)
1205 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
1206 v
= vtop
->r
& (VT_VALMASK
| VT_LVAL
);
1207 if (v
== VT_CONST
) {
1210 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
1216 vtop
[0].r
= vtop
[-1].r2
;
1217 vtop
[0].r2
= vtop
[-1].r2
= VT_CONST
;
1219 vtop
[0].type
.t
= vtop
[-1].type
.t
= VT_INT
| u
;
1223 #ifdef TCC_TARGET_ARM
1224 /* expand long long on stack */
1225 ST_FUNC
void lexpand_nr(void)
1229 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
1231 vtop
->r2
= VT_CONST
;
1232 vtop
->type
.t
= VT_INT
| u
;
1233 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
1234 if (v
== VT_CONST
) {
1235 vtop
[-1].c
.i
= vtop
->c
.i
;
1236 vtop
->c
.i
= vtop
->c
.i
>> 32;
1238 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
1240 vtop
->r
= vtop
[-1].r
;
1241 } else if (v
> VT_CONST
) {
1245 vtop
->r
= vtop
[-1].r2
;
1246 vtop
[-1].r2
= VT_CONST
;
1247 vtop
[-1].type
.t
= VT_INT
| u
;
1251 #if !defined(TCC_TARGET_X86_64) && !defined(TCC_TARGET_ARM64)
1252 /* build a long long from two ints */
1253 static void lbuild(int t
)
1255 gv2(RC_INT
, RC_INT
);
1256 vtop
[-1].r2
= vtop
[0].r
;
1257 vtop
[-1].type
.t
= t
;
1262 /* rotate n first stack elements to the bottom
1263 I1 ... In -> I2 ... In I1 [top is right]
1265 ST_FUNC
void vrotb(int n
)
1271 for(i
=-n
+1;i
!=0;i
++)
1272 vtop
[i
] = vtop
[i
+1];
1276 /* rotate the n elements before entry e towards the top
1277 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
1279 ST_FUNC
void vrote(SValue
*e
, int n
)
1285 for(i
= 0;i
< n
- 1; i
++)
1290 /* rotate n first stack elements to the top
1291 I1 ... In -> In I1 ... I(n-1) [top is right]
1293 ST_FUNC
void vrott(int n
)
1298 /* pop stack value */
1299 ST_FUNC
void vpop(void)
1302 v
= vtop
->r
& VT_VALMASK
;
1303 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1304 /* for x86, we need to pop the FP stack */
1305 if (v
== TREG_ST0
&& !nocode_wanted
) {
1306 o(0xd8dd); /* fstp %st(0) */
1309 if (v
== VT_JMP
|| v
== VT_JMPI
) {
1310 /* need to put correct jump if && or || without test */
1316 /* convert stack entry to register and duplicate its value in another
1318 static void gv_dup(void)
1324 #if !defined(TCC_TARGET_X86_64) && !defined(TCC_TARGET_ARM64)
1325 if ((t
& VT_BTYPE
) == VT_LLONG
) {
1332 /* stack: H L L1 H1 */
1342 /* duplicate value */
1347 #ifdef TCC_TARGET_X86_64
1348 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1358 load(r1
, &sv
); /* move r to r1 */
1360 /* duplicates value */
1366 /* Generate value test
1368 * Generate a test for any value (jump, comparison and integers) */
1369 ST_FUNC
int gvtst(int inv
, int t
)
1371 int v
= vtop
->r
& VT_VALMASK
;
1372 if (v
!= VT_CMP
&& v
!= VT_JMP
&& v
!= VT_JMPI
) {
1376 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1377 /* constant jmp optimization */
1378 if ((vtop
->c
.i
!= 0) != inv
)
1383 return gtst(inv
, t
);
1386 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
1387 /* generate CPU independent (unsigned) long long operations */
1388 static void gen_opl(int op
)
1390 int t
, a
, b
, op1
, c
, i
;
1392 unsigned short reg_iret
= REG_IRET
;
1393 unsigned short reg_lret
= REG_LRET
;
1399 func
= TOK___divdi3
;
1402 func
= TOK___udivdi3
;
1405 func
= TOK___moddi3
;
1408 func
= TOK___umoddi3
;
1415 /* call generic long long function */
1416 vpush_global_sym(&func_old_type
, func
);
1421 vtop
->r2
= reg_lret
;
1429 //pv("gen_opl A",0,2);
1435 /* stack: L1 H1 L2 H2 */
1440 vtop
[-2] = vtop
[-3];
1443 /* stack: H1 H2 L1 L2 */
1444 //pv("gen_opl B",0,4);
1450 /* stack: H1 H2 L1 L2 ML MH */
1453 /* stack: ML MH H1 H2 L1 L2 */
1457 /* stack: ML MH H1 L2 H2 L1 */
1462 /* stack: ML MH M1 M2 */
1465 } else if (op
== '+' || op
== '-') {
1466 /* XXX: add non carry method too (for MIPS or alpha) */
1472 /* stack: H1 H2 (L1 op L2) */
1475 gen_op(op1
+ 1); /* TOK_xxxC2 */
1478 /* stack: H1 H2 (L1 op L2) */
1481 /* stack: (L1 op L2) H1 H2 */
1483 /* stack: (L1 op L2) (H1 op H2) */
1491 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1492 t
= vtop
[-1].type
.t
;
1496 /* stack: L H shift */
1498 /* constant: simpler */
1499 /* NOTE: all comments are for SHL. the other cases are
1500 done by swaping words */
1511 if (op
!= TOK_SAR
) {
1544 /* XXX: should provide a faster fallback on x86 ? */
1547 func
= TOK___ashrdi3
;
1550 func
= TOK___lshrdi3
;
1553 func
= TOK___ashldi3
;
1559 /* compare operations */
1565 /* stack: L1 H1 L2 H2 */
1567 vtop
[-1] = vtop
[-2];
1569 /* stack: L1 L2 H1 H2 */
1572 /* when values are equal, we need to compare low words. since
1573 the jump is inverted, we invert the test too. */
1576 else if (op1
== TOK_GT
)
1578 else if (op1
== TOK_ULT
)
1580 else if (op1
== TOK_UGT
)
1585 if (op1
!= TOK_NE
) {
1589 /* generate non equal test */
1590 /* XXX: NOT PORTABLE yet */
1594 #if defined(TCC_TARGET_I386)
1595 b
= psym(0x850f, 0);
1596 #elif defined(TCC_TARGET_ARM)
1598 o(0x1A000000 | encbranch(ind
, 0, 1));
1599 #elif defined(TCC_TARGET_C67) || defined(TCC_TARGET_ARM64)
1600 tcc_error("not implemented");
1602 #error not supported
1606 /* compare low. Always unsigned */
1610 else if (op1
== TOK_LE
)
1612 else if (op1
== TOK_GT
)
1614 else if (op1
== TOK_GE
)
1625 static uint64_t gen_opic_sdiv(uint64_t a
, uint64_t b
)
1627 uint64_t x
= (a
>> 63 ? -a
: a
) / (b
>> 63 ? -b
: b
);
1628 return (a
^ b
) >> 63 ? -x
: x
;
1631 static int gen_opic_lt(uint64_t a
, uint64_t b
)
1633 return (a
^ (uint64_t)1 << 63) < (b
^ (uint64_t)1 << 63);
1636 /* handle integer constant optimizations and various machine
1638 static void gen_opic(int op
)
1640 SValue
*v1
= vtop
- 1;
1642 int t1
= v1
->type
.t
& VT_BTYPE
;
1643 int t2
= v2
->type
.t
& VT_BTYPE
;
1644 int c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1645 int c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1646 uint64_t l1
= c1
? v1
->c
.i
: 0;
1647 uint64_t l2
= c2
? v2
->c
.i
: 0;
1648 int shm
= (t1
== VT_LLONG
) ? 63 : 31;
1651 l1
= ((uint32_t)l1
|
1652 (v1
->type
.t
& VT_UNSIGNED
? 0 : -(l1
& 0x80000000)));
1654 l2
= ((uint32_t)l2
|
1655 (v2
->type
.t
& VT_UNSIGNED
? 0 : -(l2
& 0x80000000)));
1659 case '+': l1
+= l2
; break;
1660 case '-': l1
-= l2
; break;
1661 case '&': l1
&= l2
; break;
1662 case '^': l1
^= l2
; break;
1663 case '|': l1
|= l2
; break;
1664 case '*': l1
*= l2
; break;
1671 /* if division by zero, generate explicit division */
1674 tcc_error("division by zero in constant");
1678 default: l1
= gen_opic_sdiv(l1
, l2
); break;
1679 case '%': l1
= l1
- l2
* gen_opic_sdiv(l1
, l2
); break;
1680 case TOK_UDIV
: l1
= l1
/ l2
; break;
1681 case TOK_UMOD
: l1
= l1
% l2
; break;
1684 case TOK_SHL
: l1
<<= (l2
& shm
); break;
1685 case TOK_SHR
: l1
>>= (l2
& shm
); break;
1687 l1
= (l1
>> 63) ? ~(~l1
>> (l2
& shm
)) : l1
>> (l2
& shm
);
1690 case TOK_ULT
: l1
= l1
< l2
; break;
1691 case TOK_UGE
: l1
= l1
>= l2
; break;
1692 case TOK_EQ
: l1
= l1
== l2
; break;
1693 case TOK_NE
: l1
= l1
!= l2
; break;
1694 case TOK_ULE
: l1
= l1
<= l2
; break;
1695 case TOK_UGT
: l1
= l1
> l2
; break;
1696 case TOK_LT
: l1
= gen_opic_lt(l1
, l2
); break;
1697 case TOK_GE
: l1
= !gen_opic_lt(l1
, l2
); break;
1698 case TOK_LE
: l1
= !gen_opic_lt(l2
, l1
); break;
1699 case TOK_GT
: l1
= gen_opic_lt(l2
, l1
); break;
1701 case TOK_LAND
: l1
= l1
&& l2
; break;
1702 case TOK_LOR
: l1
= l1
|| l2
; break;
1709 /* if commutative ops, put c2 as constant */
1710 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1711 op
== '|' || op
== '*')) {
1713 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1714 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1716 if (!const_wanted
&&
1718 (op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
)) ||
1719 (l1
== -1 && op
== TOK_SAR
))) {
1720 /* treat (0 << x), (0 >> x) and (-1 >> x) as constant */
1722 } else if (!const_wanted
&&
1723 c2
&& ((l2
== 0 && (op
== '&' || op
== '*')) ||
1724 (l2
== -1 && op
== '|') ||
1725 (l2
== 0xffffffff && t2
!= VT_LLONG
&& op
== '|') ||
1726 (l2
== 1 && (op
== '%' || op
== TOK_UMOD
)))) {
1727 /* treat (x & 0), (x * 0), (x | -1) and (x % 1) as constant */
1732 } else if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1735 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1736 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1740 /* filter out NOP operations like x*1, x-0, x&-1... */
1742 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1743 /* try to use shifts instead of muls or divs */
1744 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1753 else if (op
== TOK_PDIV
)
1759 } else if (c2
&& (op
== '+' || op
== '-') &&
1760 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
1761 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1762 /* symbol + constant case */
1769 if (!nocode_wanted
) {
1770 /* call low level op generator */
1771 if (t1
== VT_LLONG
|| t2
== VT_LLONG
||
1772 (PTR_SIZE
== 8 && (t1
== VT_PTR
|| t2
== VT_PTR
)))
1783 /* generate a floating point operation with constant propagation */
1784 static void gen_opif(int op
)
1792 /* currently, we cannot do computations with forward symbols */
1793 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1794 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1796 if (v1
->type
.t
== VT_FLOAT
) {
1799 } else if (v1
->type
.t
== VT_DOUBLE
) {
1807 /* NOTE: we only do constant propagation if finite number (not
1808 NaN or infinity) (ANSI spec) */
1809 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1813 case '+': f1
+= f2
; break;
1814 case '-': f1
-= f2
; break;
1815 case '*': f1
*= f2
; break;
1819 tcc_error("division by zero in constant");
1824 /* XXX: also handles tests ? */
1828 /* XXX: overflow test ? */
1829 if (v1
->type
.t
== VT_FLOAT
) {
1831 } else if (v1
->type
.t
== VT_DOUBLE
) {
1839 if (!nocode_wanted
) {
1847 static int pointed_size(CType
*type
)
1850 return type_size(pointed_type(type
), &align
);
1853 static void vla_runtime_pointed_size(CType
*type
)
1856 vla_runtime_type_size(pointed_type(type
), &align
);
1859 static inline int is_null_pointer(SValue
*p
)
1861 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1863 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& (uint32_t)p
->c
.i
== 0) ||
1864 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.i
== 0) ||
1865 ((p
->type
.t
& VT_BTYPE
) == VT_PTR
&&
1866 (PTR_SIZE
== 4 ? (uint32_t)p
->c
.i
== 0 : p
->c
.i
== 0));
1869 static inline int is_integer_btype(int bt
)
1871 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
1872 bt
== VT_INT
|| bt
== VT_LLONG
);
1875 /* check types for comparison or subtraction of pointers */
1876 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
1878 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
1881 /* null pointers are accepted for all comparisons as gcc */
1882 if (is_null_pointer(p1
) || is_null_pointer(p2
))
1886 bt1
= type1
->t
& VT_BTYPE
;
1887 bt2
= type2
->t
& VT_BTYPE
;
1888 /* accept comparison between pointer and integer with a warning */
1889 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
1890 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
1891 tcc_warning("comparison between pointer and integer");
1895 /* both must be pointers or implicit function pointers */
1896 if (bt1
== VT_PTR
) {
1897 type1
= pointed_type(type1
);
1898 } else if (bt1
!= VT_FUNC
)
1899 goto invalid_operands
;
1901 if (bt2
== VT_PTR
) {
1902 type2
= pointed_type(type2
);
1903 } else if (bt2
!= VT_FUNC
) {
1905 tcc_error("invalid operands to binary %s", get_tok_str(op
, NULL
));
1907 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1908 (type2
->t
& VT_BTYPE
) == VT_VOID
)
1912 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1913 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1914 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1915 /* gcc-like error if '-' is used */
1917 goto invalid_operands
;
1919 tcc_warning("comparison of distinct pointer types lacks a cast");
1923 /* generic gen_op: handles types problems */
1924 ST_FUNC
void gen_op(int op
)
1926 int u
, t1
, t2
, bt1
, bt2
, t
;
1930 t1
= vtop
[-1].type
.t
;
1931 t2
= vtop
[0].type
.t
;
1932 bt1
= t1
& VT_BTYPE
;
1933 bt2
= t2
& VT_BTYPE
;
1935 if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
1936 tcc_error("operation on a struct");
1937 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
1938 if (bt2
== VT_FUNC
) {
1939 mk_pointer(&vtop
->type
);
1942 if (bt1
== VT_FUNC
) {
1944 mk_pointer(&vtop
->type
);
1949 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
1950 /* at least one operand is a pointer */
1951 /* relationnal op: must be both pointers */
1952 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
1953 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1954 /* pointers are handled are unsigned */
1955 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1956 t
= VT_LLONG
| VT_UNSIGNED
;
1958 t
= VT_INT
| VT_UNSIGNED
;
1962 /* if both pointers, then it must be the '-' op */
1963 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
1965 tcc_error("cannot use pointers here");
1966 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1967 /* XXX: check that types are compatible */
1968 if (vtop
[-1].type
.t
& VT_VLA
) {
1969 vla_runtime_pointed_size(&vtop
[-1].type
);
1971 vpushi(pointed_size(&vtop
[-1].type
));
1975 /* set to integer type */
1976 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1977 vtop
->type
.t
= VT_LLONG
;
1979 vtop
->type
.t
= VT_INT
;
1984 /* exactly one pointer : must be '+' or '-'. */
1985 if (op
!= '-' && op
!= '+')
1986 tcc_error("cannot use pointers here");
1987 /* Put pointer as first operand */
1988 if (bt2
== VT_PTR
) {
1993 if ((vtop
[0].type
.t
& VT_BTYPE
) == VT_LLONG
)
1994 /* XXX: truncate here because gen_opl can't handle ptr + long long */
1995 gen_cast(&int_type
);
1997 type1
= vtop
[-1].type
;
1998 type1
.t
&= ~VT_ARRAY
;
1999 if (vtop
[-1].type
.t
& VT_VLA
)
2000 vla_runtime_pointed_size(&vtop
[-1].type
);
2002 u
= pointed_size(&vtop
[-1].type
);
2004 tcc_error("unknown array element size");
2005 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2008 /* XXX: cast to int ? (long long case) */
2014 /* #ifdef CONFIG_TCC_BCHECK
2015 The main reason to removing this code:
2022 fprintf(stderr, "v+i-j = %p\n", v+i-j);
2023 fprintf(stderr, "v+(i-j) = %p\n", v+(i-j));
2025 When this code is on. then the output looks like
2027 v+(i-j) = 0xbff84000
2029 /* if evaluating constant expression, no code should be
2030 generated, so no bound check */
2031 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
2032 /* if bounded pointers, we generate a special code to
2039 gen_bounded_ptr_add();
2045 /* put again type if gen_opic() swaped operands */
2048 } else if (is_float(bt1
) || is_float(bt2
)) {
2049 /* compute bigger type and do implicit casts */
2050 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
2052 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
2057 /* floats can only be used for a few operations */
2058 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
2059 (op
< TOK_ULT
|| op
> TOK_GT
))
2060 tcc_error("invalid operands for binary operation");
2062 } else if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
) {
2063 t
= bt1
== VT_LLONG
? VT_LLONG
: VT_INT
;
2064 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (t
| VT_UNSIGNED
))
2067 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
2068 /* cast to biggest op */
2070 /* convert to unsigned if it does not fit in a long long */
2071 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
2072 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
2076 /* integer operations */
2078 /* convert to unsigned if it does not fit in an integer */
2079 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
2080 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
2083 /* XXX: currently, some unsigned operations are explicit, so
2084 we modify them here */
2085 if (t
& VT_UNSIGNED
) {
2092 else if (op
== TOK_LT
)
2094 else if (op
== TOK_GT
)
2096 else if (op
== TOK_LE
)
2098 else if (op
== TOK_GE
)
2105 /* special case for shifts and long long: we keep the shift as
2107 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
2114 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
2115 /* relationnal op: the result is an int */
2116 vtop
->type
.t
= VT_INT
;
2121 // Make sure that we have converted to an rvalue:
2122 if (vtop
->r
& VT_LVAL
&& !nocode_wanted
)
2123 gv(is_float(vtop
->type
.t
& VT_BTYPE
) ? RC_FLOAT
: RC_INT
);
2126 #ifndef TCC_TARGET_ARM
2127 /* generic itof for unsigned long long case */
2128 static void gen_cvt_itof1(int t
)
2130 #ifdef TCC_TARGET_ARM64
2133 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
2134 (VT_LLONG
| VT_UNSIGNED
)) {
2137 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
2138 #if LDOUBLE_SIZE != 8
2139 else if (t
== VT_LDOUBLE
)
2140 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
2143 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
2147 vtop
->r
= reg_fret(t
);
2155 /* generic ftoi for unsigned long long case */
2156 static void gen_cvt_ftoi1(int t
)
2158 #ifdef TCC_TARGET_ARM64
2163 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
2164 /* not handled natively */
2165 st
= vtop
->type
.t
& VT_BTYPE
;
2167 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
2168 #if LDOUBLE_SIZE != 8
2169 else if (st
== VT_LDOUBLE
)
2170 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
2173 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
2178 vtop
->r2
= REG_LRET
;
2185 /* force char or short cast */
2186 static void force_charshort_cast(int t
)
2190 /* XXX: add optimization if lvalue : just change type and offset */
2195 if (t
& VT_UNSIGNED
) {
2196 vpushi((1 << bits
) - 1);
2199 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
)
2205 /* result must be signed or the SAR is converted to an SHL
2206 This was not the case when "t" was a signed short
2207 and the last value on the stack was an unsigned int */
2208 vtop
->type
.t
&= ~VT_UNSIGNED
;
2214 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
2215 static void gen_cast(CType
*type
)
2217 int sbt
, dbt
, sf
, df
, c
, p
;
2219 /* special delayed cast for char/short */
2220 /* XXX: in some cases (multiple cascaded casts), it may still
2222 if (vtop
->r
& VT_MUSTCAST
) {
2223 vtop
->r
&= ~VT_MUSTCAST
;
2224 force_charshort_cast(vtop
->type
.t
);
2227 /* bitfields first get cast to ints */
2228 if (vtop
->type
.t
& VT_BITFIELD
&& !nocode_wanted
) {
2232 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
2233 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
2238 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
2239 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
2241 /* constant case: we can do it now */
2242 /* XXX: in ISOC, cannot do it if error in convert */
2243 if (sbt
== VT_FLOAT
)
2244 vtop
->c
.ld
= vtop
->c
.f
;
2245 else if (sbt
== VT_DOUBLE
)
2246 vtop
->c
.ld
= vtop
->c
.d
;
2249 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
2250 if ((sbt
& VT_UNSIGNED
) || !(vtop
->c
.i
>> 63))
2251 vtop
->c
.ld
= vtop
->c
.i
;
2253 vtop
->c
.ld
= -(long double)-vtop
->c
.i
;
2255 if ((sbt
& VT_UNSIGNED
) || !(vtop
->c
.i
>> 31))
2256 vtop
->c
.ld
= (uint32_t)vtop
->c
.i
;
2258 vtop
->c
.ld
= -(long double)-(uint32_t)vtop
->c
.i
;
2261 if (dbt
== VT_FLOAT
)
2262 vtop
->c
.f
= (float)vtop
->c
.ld
;
2263 else if (dbt
== VT_DOUBLE
)
2264 vtop
->c
.d
= (double)vtop
->c
.ld
;
2265 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
2266 vtop
->c
.i
= vtop
->c
.ld
;
2267 } else if (sf
&& dbt
== VT_BOOL
) {
2268 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
2271 vtop
->c
.i
= vtop
->c
.ld
;
2272 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
2274 else if (sbt
& VT_UNSIGNED
)
2275 vtop
->c
.i
= (uint32_t)vtop
->c
.i
;
2276 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2277 else if (sbt
== VT_PTR
)
2280 else if (sbt
!= VT_LLONG
)
2281 vtop
->c
.i
= ((uint32_t)vtop
->c
.i
|
2282 -(vtop
->c
.i
& 0x80000000));
2284 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
2286 else if (dbt
== VT_BOOL
)
2287 vtop
->c
.i
= (vtop
->c
.i
!= 0);
2288 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2289 else if (dbt
== VT_PTR
)
2292 else if (dbt
!= VT_LLONG
) {
2293 uint32_t m
= ((dbt
& VT_BTYPE
) == VT_BYTE
? 0xff :
2294 (dbt
& VT_BTYPE
) == VT_SHORT
? 0xffff :
2297 if (!(dbt
& VT_UNSIGNED
))
2298 vtop
->c
.i
|= -(vtop
->c
.i
& ((m
>> 1) + 1));
2301 } else if (p
&& dbt
== VT_BOOL
) {
2304 } else if (!nocode_wanted
) {
2305 /* non constant case: generate code */
2307 /* convert from fp to fp */
2310 /* convert int to fp */
2313 /* convert fp to int */
2314 if (dbt
== VT_BOOL
) {
2318 /* we handle char/short/etc... with generic code */
2319 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
2320 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
2324 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
2325 /* additional cast for char/short... */
2330 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
2331 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
2332 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
2333 /* scalar to long long */
2334 /* machine independent conversion */
2336 /* generate high word */
2337 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
2341 if (sbt
== VT_PTR
) {
2342 /* cast from pointer to int before we apply
2343 shift operation, which pointers don't support*/
2344 gen_cast(&int_type
);
2350 /* patch second register */
2351 vtop
[-1].r2
= vtop
->r
;
2355 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
2356 (dbt
& VT_BTYPE
) == VT_PTR
||
2357 (dbt
& VT_BTYPE
) == VT_FUNC
) {
2358 if ((sbt
& VT_BTYPE
) != VT_LLONG
&&
2359 (sbt
& VT_BTYPE
) != VT_PTR
&&
2360 (sbt
& VT_BTYPE
) != VT_FUNC
) {
2361 /* need to convert from 32bit to 64bit */
2363 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
2364 #if defined(TCC_TARGET_ARM64)
2366 #elif defined(TCC_TARGET_X86_64)
2368 /* x86_64 specific: movslq */
2370 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
2377 } else if (dbt
== VT_BOOL
) {
2378 /* scalar to bool */
2381 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
2382 (dbt
& VT_BTYPE
) == VT_SHORT
) {
2383 if (sbt
== VT_PTR
) {
2384 vtop
->type
.t
= VT_INT
;
2385 tcc_warning("nonportable conversion from pointer to char/short");
2387 force_charshort_cast(dbt
);
2388 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
2389 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
2391 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
2392 /* from long long: just take low order word */
2396 /* if lvalue and single word type, nothing to do because
2397 the lvalue already contains the real type size (see
2398 VT_LVAL_xxx constants) */
2402 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
2403 /* if we are casting between pointer types,
2404 we must update the VT_LVAL_xxx size */
2405 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
2406 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
2411 /* return type size as known at compile time. Put alignment at 'a' */
2412 ST_FUNC
int type_size(CType
*type
, int *a
)
2417 bt
= type
->t
& VT_BTYPE
;
2418 if (bt
== VT_STRUCT
) {
2423 } else if (bt
== VT_PTR
) {
2424 if (type
->t
& VT_ARRAY
) {
2428 ts
= type_size(&s
->type
, a
);
2430 if (ts
< 0 && s
->c
< 0)
2438 } else if (bt
== VT_LDOUBLE
) {
2440 return LDOUBLE_SIZE
;
2441 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
2442 #ifdef TCC_TARGET_I386
2443 #ifdef TCC_TARGET_PE
2448 #elif defined(TCC_TARGET_ARM)
2458 } else if (bt
== VT_INT
|| bt
== VT_FLOAT
) {
2461 } else if (bt
== VT_SHORT
) {
2464 } else if (bt
== VT_QLONG
|| bt
== VT_QFLOAT
) {
2467 } else if (bt
== VT_ENUM
) {
2469 /* Enums might be incomplete, so don't just return '4' here. */
2470 return type
->ref
->c
;
2472 /* char, void, function, _Bool */
2478 /* push type size as known at runtime time on top of value stack. Put
2480 ST_FUNC
void vla_runtime_type_size(CType
*type
, int *a
)
2482 if (type
->t
& VT_VLA
) {
2483 type_size(&type
->ref
->type
, a
);
2484 vset(&int_type
, VT_LOCAL
|VT_LVAL
, type
->ref
->c
);
2486 vpushi(type_size(type
, a
));
2490 static void vla_sp_restore(void) {
2491 if (vlas_in_scope
) {
2492 gen_vla_sp_restore(vla_sp_loc
);
2496 static void vla_sp_restore_root(void) {
2497 if (vlas_in_scope
) {
2498 gen_vla_sp_restore(vla_sp_root_loc
);
2502 /* return the pointed type of t */
2503 static inline CType
*pointed_type(CType
*type
)
2505 return &type
->ref
->type
;
2508 /* modify type so that its it is a pointer to type. */
2509 ST_FUNC
void mk_pointer(CType
*type
)
2512 s
= sym_push(SYM_FIELD
, type
, 0, -1);
2513 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
2517 /* compare function types. OLD functions match any new functions */
2518 static int is_compatible_func(CType
*type1
, CType
*type2
)
2524 if (!is_compatible_types(&s1
->type
, &s2
->type
))
2526 /* check func_call */
2527 if (s1
->a
.func_call
!= s2
->a
.func_call
)
2529 /* XXX: not complete */
2530 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
2534 while (s1
!= NULL
) {
2537 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
2547 /* return true if type1 and type2 are the same. If unqualified is
2548 true, qualifiers on the types are ignored.
2550 - enums are not checked as gcc __builtin_types_compatible_p ()
2552 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2556 t1
= type1
->t
& VT_TYPE
;
2557 t2
= type2
->t
& VT_TYPE
;
2559 /* strip qualifiers before comparing */
2560 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2561 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2563 /* Default Vs explicit signedness only matters for char */
2564 if ((t1
& VT_BTYPE
) != VT_BYTE
) {
2568 /* An enum is compatible with (unsigned) int. Ideally we would
2569 store the enums signedness in type->ref.a.<some_bit> and
2570 only accept unsigned enums with unsigned int and vice versa.
2571 But one of our callers (gen_assign_cast) always strips VT_UNSIGNED
2572 from pointer target types, so we can't add it here either. */
2573 if ((t1
& VT_BTYPE
) == VT_ENUM
) {
2575 if (type1
->ref
->a
.unsigned_enum
)
2578 if ((t2
& VT_BTYPE
) == VT_ENUM
) {
2580 if (type2
->ref
->a
.unsigned_enum
)
2583 /* XXX: bitfields ? */
2586 /* test more complicated cases */
2587 bt1
= t1
& VT_BTYPE
;
2588 if (bt1
== VT_PTR
) {
2589 type1
= pointed_type(type1
);
2590 type2
= pointed_type(type2
);
2591 return is_compatible_types(type1
, type2
);
2592 } else if (bt1
== VT_STRUCT
) {
2593 return (type1
->ref
== type2
->ref
);
2594 } else if (bt1
== VT_FUNC
) {
2595 return is_compatible_func(type1
, type2
);
2601 /* return true if type1 and type2 are exactly the same (including
2604 static int is_compatible_types(CType
*type1
, CType
*type2
)
2606 return compare_types(type1
,type2
,0);
2609 /* return true if type1 and type2 are the same (ignoring qualifiers).
2611 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
2613 return compare_types(type1
,type2
,1);
2616 /* print a type. If 'varstr' is not NULL, then the variable is also
2617 printed in the type */
2619 /* XXX: add array and function pointers */
2620 static void type_to_str(char *buf
, int buf_size
,
2621 CType
*type
, const char *varstr
)
2628 t
= type
->t
& VT_TYPE
;
2631 if (t
& VT_CONSTANT
)
2632 pstrcat(buf
, buf_size
, "const ");
2633 if (t
& VT_VOLATILE
)
2634 pstrcat(buf
, buf_size
, "volatile ");
2635 if ((t
& (VT_DEFSIGN
| VT_UNSIGNED
)) == (VT_DEFSIGN
| VT_UNSIGNED
))
2636 pstrcat(buf
, buf_size
, "unsigned ");
2637 else if (t
& VT_DEFSIGN
)
2638 pstrcat(buf
, buf_size
, "signed ");
2668 tstr
= "long double";
2670 pstrcat(buf
, buf_size
, tstr
);
2674 if (bt
== VT_STRUCT
)
2678 pstrcat(buf
, buf_size
, tstr
);
2679 v
= type
->ref
->v
& ~SYM_STRUCT
;
2680 if (v
>= SYM_FIRST_ANOM
)
2681 pstrcat(buf
, buf_size
, "<anonymous>");
2683 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
2687 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
2688 pstrcat(buf
, buf_size
, "(");
2690 while (sa
!= NULL
) {
2691 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
2692 pstrcat(buf
, buf_size
, buf1
);
2695 pstrcat(buf
, buf_size
, ", ");
2697 pstrcat(buf
, buf_size
, ")");
2702 snprintf(buf1
, sizeof(buf1
), "%s[%ld]", varstr
? varstr
: "", s
->c
);
2703 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2706 pstrcpy(buf1
, sizeof(buf1
), "*");
2707 if (t
& VT_CONSTANT
)
2708 pstrcat(buf1
, buf_size
, "const ");
2709 if (t
& VT_VOLATILE
)
2710 pstrcat(buf1
, buf_size
, "volatile ");
2712 pstrcat(buf1
, sizeof(buf1
), varstr
);
2713 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2717 pstrcat(buf
, buf_size
, " ");
2718 pstrcat(buf
, buf_size
, varstr
);
2723 /* verify type compatibility to store vtop in 'dt' type, and generate
2725 static void gen_assign_cast(CType
*dt
)
2727 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
2728 char buf1
[256], buf2
[256];
2731 st
= &vtop
->type
; /* source type */
2732 dbt
= dt
->t
& VT_BTYPE
;
2733 sbt
= st
->t
& VT_BTYPE
;
2734 if (sbt
== VT_VOID
|| dbt
== VT_VOID
) {
2735 if (sbt
== VT_VOID
&& dbt
== VT_VOID
)
2737 It is Ok if both are void
2743 gcc accepts this program
2746 tcc_error("cannot cast from/to void");
2748 if (dt
->t
& VT_CONSTANT
)
2749 tcc_warning("assignment of read-only location");
2752 /* special cases for pointers */
2753 /* '0' can also be a pointer */
2754 if (is_null_pointer(vtop
))
2756 /* accept implicit pointer to integer cast with warning */
2757 if (is_integer_btype(sbt
)) {
2758 tcc_warning("assignment makes pointer from integer without a cast");
2761 type1
= pointed_type(dt
);
2762 /* a function is implicitely a function pointer */
2763 if (sbt
== VT_FUNC
) {
2764 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
2765 !is_compatible_types(pointed_type(dt
), st
))
2766 tcc_warning("assignment from incompatible pointer type");
2771 type2
= pointed_type(st
);
2772 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2773 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
2774 /* void * can match anything */
2776 /* exact type match, except for qualifiers */
2779 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2780 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2781 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2782 /* Like GCC don't warn by default for merely changes
2783 in pointer target signedness. Do warn for different
2784 base types, though, in particular for unsigned enums
2785 and signed int targets. */
2786 if ((tmp_type1
.t
& (VT_DEFSIGN
| VT_UNSIGNED
)) !=
2787 (tmp_type2
.t
& (VT_DEFSIGN
| VT_UNSIGNED
)) &&
2788 (tmp_type1
.t
& VT_BTYPE
) == (tmp_type2
.t
& VT_BTYPE
))
2791 tcc_warning("assignment from incompatible pointer type");
2794 /* check const and volatile */
2795 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
2796 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
2797 tcc_warning("assignment discards qualifiers from pointer target type");
2803 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
2804 tcc_warning("assignment makes integer from pointer without a cast");
2805 } else if (sbt
== VT_STRUCT
) {
2806 goto case_VT_STRUCT
;
2808 /* XXX: more tests */
2814 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2815 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2816 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2818 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
2819 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
2820 tcc_error("cannot cast '%s' to '%s'", buf1
, buf2
);
2828 /* store vtop in lvalue pushed on stack */
2829 ST_FUNC
void vstore(void)
2831 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
2833 ft
= vtop
[-1].type
.t
;
2834 sbt
= vtop
->type
.t
& VT_BTYPE
;
2835 dbt
= ft
& VT_BTYPE
;
2836 if ((((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2837 (sbt
== VT_INT
&& dbt
== VT_SHORT
))
2838 && !(vtop
->type
.t
& VT_BITFIELD
)) {
2839 /* optimize char/short casts */
2840 delayed_cast
= VT_MUSTCAST
;
2841 vtop
->type
.t
= (ft
& VT_TYPE
& ~VT_BITFIELD
&
2842 ((1 << VT_STRUCT_SHIFT
) - 1));
2843 /* XXX: factorize */
2844 if (ft
& VT_CONSTANT
)
2845 tcc_warning("assignment of read-only location");
2848 if (!(ft
& VT_BITFIELD
))
2849 gen_assign_cast(&vtop
[-1].type
);
2852 if (sbt
== VT_STRUCT
) {
2853 /* if structure, only generate pointer */
2854 /* structure assignment : generate memcpy */
2855 /* XXX: optimize if small size */
2856 if (!nocode_wanted
) {
2857 size
= type_size(&vtop
->type
, &align
);
2861 vtop
->type
.t
= VT_PTR
;
2864 /* address of memcpy() */
2867 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2868 else if(!(align
& 3))
2869 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2872 /* Use memmove, rather than memcpy, as dest and src may be same: */
2873 vpush_global_sym(&func_old_type
, TOK_memmove
);
2878 vtop
->type
.t
= VT_PTR
;
2887 /* leave source on stack */
2888 } else if (ft
& VT_BITFIELD
) {
2889 /* bitfield store handling */
2891 /* save lvalue as expression result (example: s.b = s.a = n;) */
2892 vdup(), vtop
[-1] = vtop
[-2];
2894 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2895 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2896 /* remove bit field info to avoid loops */
2897 vtop
[-1].type
.t
= ft
& ~VT_BITFIELD
& ((1 << VT_STRUCT_SHIFT
) - 1);
2899 if((ft
& VT_BTYPE
) == VT_BOOL
) {
2900 gen_cast(&vtop
[-1].type
);
2901 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2904 /* duplicate destination */
2906 vtop
[-1] = vtop
[-2];
2908 /* mask and shift source */
2909 if((ft
& VT_BTYPE
) != VT_BOOL
) {
2910 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2911 vpushll((1ULL << bit_size
) - 1ULL);
2913 vpushi((1 << bit_size
) - 1);
2919 /* load destination, mask and or with source */
2921 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2922 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2924 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2930 /* ... and discard */
2934 if (!nocode_wanted
) {
2935 #ifdef CONFIG_TCC_BCHECK
2936 /* bound check case */
2937 if (vtop
[-1].r
& VT_MUSTBOUND
) {
2946 #ifdef TCC_TARGET_X86_64
2947 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
2949 } else if ((ft
& VT_BTYPE
) == VT_QFLOAT
) {
2954 r
= gv(rc
); /* generate value */
2955 /* if lvalue was saved on stack, must read it */
2956 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
2958 t
= get_reg(RC_INT
);
2959 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2964 sv
.r
= VT_LOCAL
| VT_LVAL
;
2965 sv
.c
.i
= vtop
[-1].c
.i
;
2967 vtop
[-1].r
= t
| VT_LVAL
;
2969 /* two word case handling : store second register at word + 4 (or +8 for x86-64) */
2970 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2971 if (((ft
& VT_BTYPE
) == VT_QLONG
) || ((ft
& VT_BTYPE
) == VT_QFLOAT
)) {
2972 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
2974 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
2975 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
2977 vtop
[-1].type
.t
= load_type
;
2980 /* convert to int to increment easily */
2981 vtop
->type
.t
= addr_type
;
2987 vtop
[-1].type
.t
= load_type
;
2988 /* XXX: it works because r2 is spilled last ! */
2989 store(vtop
->r2
, vtop
- 1);
2995 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
2996 vtop
->r
|= delayed_cast
;
3000 /* post defines POST/PRE add. c is the token ++ or -- */
3001 ST_FUNC
void inc(int post
, int c
)
3004 vdup(); /* save lvalue */
3007 gv_dup(); /* duplicate value */
3009 vdup(); /* duplicate value */
3014 vpushi(c
- TOK_MID
);
3016 vstore(); /* store value */
3018 vpop(); /* if post op, return saved value */
3021 ST_FUNC
void parse_mult_str (CString
*astr
, const char *msg
)
3023 /* read the string */
3027 while (tok
== TOK_STR
) {
3028 /* XXX: add \0 handling too ? */
3029 cstr_cat(astr
, tokc
.str
.data
, -1);
3032 cstr_ccat(astr
, '\0');
3035 /* Parse GNUC __attribute__ extension. Currently, the following
3036 extensions are recognized:
3037 - aligned(n) : set data/function alignment.
3038 - packed : force data alignment to 1
3039 - section(x) : generate data/code in this section.
3040 - unused : currently ignored, but may be used someday.
3041 - regparm(n) : pass function parameters in registers (i386 only)
3043 static void parse_attribute(AttributeDef
*ad
)
3048 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
3052 while (tok
!= ')') {
3053 if (tok
< TOK_IDENT
)
3054 expect("attribute name");
3061 parse_mult_str(&astr
, "section name");
3062 ad
->section
= find_section(tcc_state
, (char *)astr
.data
);
3069 parse_mult_str(&astr
, "alias(\"target\")");
3070 ad
->alias_target
= /* save string as token, for later */
3071 tok_alloc((char*)astr
.data
, astr
.size
-1)->tok
;
3075 case TOK_VISIBILITY1
:
3076 case TOK_VISIBILITY2
:
3078 parse_mult_str(&astr
,
3079 "visibility(\"default|hidden|internal|protected\")");
3080 if (!strcmp (astr
.data
, "default"))
3081 ad
->a
.visibility
= STV_DEFAULT
;
3082 else if (!strcmp (astr
.data
, "hidden"))
3083 ad
->a
.visibility
= STV_HIDDEN
;
3084 else if (!strcmp (astr
.data
, "internal"))
3085 ad
->a
.visibility
= STV_INTERNAL
;
3086 else if (!strcmp (astr
.data
, "protected"))
3087 ad
->a
.visibility
= STV_PROTECTED
;
3089 expect("visibility(\"default|hidden|internal|protected\")");
3098 if (n
<= 0 || (n
& (n
- 1)) != 0)
3099 tcc_error("alignment must be a positive power of two");
3116 /* currently, no need to handle it because tcc does not
3117 track unused objects */
3121 /* currently, no need to handle it because tcc does not
3122 track unused objects */
3127 ad
->a
.func_call
= FUNC_CDECL
;
3132 ad
->a
.func_call
= FUNC_STDCALL
;
3134 #ifdef TCC_TARGET_I386
3144 ad
->a
.func_call
= FUNC_FASTCALL1
+ n
- 1;
3150 ad
->a
.func_call
= FUNC_FASTCALLW
;
3157 ad
->a
.mode
= VT_LLONG
+ 1;
3160 ad
->a
.mode
= VT_BYTE
+ 1;
3163 ad
->a
.mode
= VT_SHORT
+ 1;
3167 ad
->a
.mode
= VT_INT
+ 1;
3170 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
3177 ad
->a
.func_export
= 1;
3180 ad
->a
.func_import
= 1;
3183 if (tcc_state
->warn_unsupported
)
3184 tcc_warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
3185 /* skip parameters */
3187 int parenthesis
= 0;
3191 else if (tok
== ')')
3194 } while (parenthesis
&& tok
!= -1);
3207 static Sym
* find_field (CType
*type
, int v
)
3211 while ((s
= s
->next
) != NULL
) {
3212 if ((s
->v
& SYM_FIELD
) && (s
->v
& ~SYM_FIELD
) >= SYM_FIRST_ANOM
) {
3213 Sym
*ret
= find_field (&s
->type
, v
);
3223 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
3224 static void struct_decl(CType
*type
, AttributeDef
*ad
, int u
)
3226 int a
, v
, size
, align
, maxalign
, c
, offset
, flexible
, extra_bytes
;
3227 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
, prevbt
;
3228 Sym
*s
, *ss
, *ass
, **ps
;
3232 a
= tok
; /* save decl type */
3234 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3235 parse_attribute(ad
);
3239 /* struct already defined ? return it */
3241 expect("struct/union/enum name");
3243 if (s
&& (s
->scope
== local_scope
|| (tok
!= '{' && tok
!= ';'))) {
3245 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
3251 /* Record the original enum/struct/union token. */
3254 /* we put an undefined size for struct/union */
3255 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
3256 s
->r
= 0; /* default alignment is zero as gcc */
3257 /* put struct/union/enum name in type */
3265 tcc_error("struct/union/enum already defined");
3266 /* cannot be empty */
3268 /* non empty enums are not allowed */
3269 if (a
== TOK_ENUM
) {
3274 expect("identifier");
3276 if (ss
&& !local_stack
)
3277 tcc_error("redefinition of enumerator '%s'",
3278 get_tok_str(v
, NULL
));
3286 /* enum symbols have static storage */
3287 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
3288 ss
->type
.t
|= VT_STATIC
;
3293 /* NOTE: we accept a trailing comma */
3298 s
->a
.unsigned_enum
= 1;
3299 s
->c
= type_size(&int_type
, &align
);
3308 while (tok
!= '}') {
3309 if (!parse_btype(&btype
, &ad1
)) {
3316 tcc_error("flexible array member '%s' not at the end of struct",
3317 get_tok_str(v
, NULL
));
3322 type_decl(&type1
, &ad1
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3324 if ((type1
.t
& VT_BTYPE
) != VT_STRUCT
)
3325 expect("identifier");
3327 int v
= btype
.ref
->v
;
3328 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
3329 if (tcc_state
->ms_extensions
== 0)
3330 expect("identifier");
3334 if (type_size(&type1
, &align
) < 0) {
3335 if ((a
== TOK_STRUCT
) && (type1
.t
& VT_ARRAY
) && c
)
3338 tcc_error("field '%s' has incomplete type",
3339 get_tok_str(v
, NULL
));
3341 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
3342 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
3343 tcc_error("invalid type for '%s'",
3344 get_tok_str(v
, NULL
));
3348 bit_size
= expr_const();
3349 /* XXX: handle v = 0 case for messages */
3351 tcc_error("negative width in bit-field '%s'",
3352 get_tok_str(v
, NULL
));
3353 if (v
&& bit_size
== 0)
3354 tcc_error("zero width for bit-field '%s'",
3355 get_tok_str(v
, NULL
));
3357 size
= type_size(&type1
, &align
);
3358 if (ad1
.a
.aligned
) {
3359 if (align
< ad1
.a
.aligned
)
3360 align
= ad1
.a
.aligned
;
3361 } else if (ad1
.a
.packed
|| ad
->a
.packed
) {
3363 } else if (*tcc_state
->pack_stack_ptr
) {
3364 if (align
> *tcc_state
->pack_stack_ptr
)
3365 align
= *tcc_state
->pack_stack_ptr
;
3368 if (bit_size
>= 0) {
3369 bt
= type1
.t
& VT_BTYPE
;
3376 tcc_error("bitfields must have scalar type");
3378 if (bit_size
> bsize
) {
3379 tcc_error("width of '%s' exceeds its type",
3380 get_tok_str(v
, NULL
));
3381 } else if (bit_size
== bsize
) {
3382 /* no need for bit fields */
3384 } else if (bit_size
== 0) {
3385 /* XXX: what to do if only padding in a
3387 /* zero size: means to pad */
3390 /* if type change, union, or will overrun
3391 * allignment slot, start at a newly
3393 if ((bit_pos
+ bit_size
) > bsize
||
3394 bt
!= prevbt
|| a
== TOK_UNION
)
3397 /* XXX: handle LSB first */
3398 type1
.t
|= VT_BITFIELD
|
3399 (bit_pos
<< VT_STRUCT_SHIFT
) |
3400 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
3401 bit_pos
+= bit_size
;
3402 /* without ms-bitfields, allocate the
3403 * minimum number of bytes necessary,
3404 * adding single bytes as needed */
3405 if (!tcc_state
->ms_bitfields
) {
3407 /* minimum bytes for new bitfield */
3408 size
= (bit_size
+ 7) / 8;
3410 /* enough spare bits already allocated? */
3411 bit_size
= (lbit_pos
- 1) % 8 + 1 + bit_size
;
3412 if (bit_size
> 8) /* doesn't fit */
3413 extra_bytes
= (bit_size
- 1) / 8;
3421 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
3422 /* add new memory data only if starting bit
3423 field or adding bytes to existing bit field */
3424 if (extra_bytes
) c
+= extra_bytes
;
3425 else if (lbit_pos
== 0) {
3426 if (a
== TOK_STRUCT
) {
3427 c
= (c
+ align
- 1) & -align
;
3436 if (align
> maxalign
)
3440 printf("add field %s offset=%d",
3441 get_tok_str(v
, NULL
), offset
);
3442 if (type1
.t
& VT_BITFIELD
) {
3443 printf(" pos=%d size=%d",
3444 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
3445 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
3450 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
3451 /* An anonymous struct/union. Adjust member offsets
3452 to reflect the real offset of our containing struct.
3453 Also set the offset of this anon member inside
3454 the outer struct to be zero. Via this it
3455 works when accessing the field offset directly
3456 (from base object), as well as when recursing
3457 members in initializer handling. */
3458 int v2
= btype
.ref
->v
;
3459 if (!(v2
& SYM_FIELD
) &&
3460 (v2
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
3462 /* This happens only with MS extensions. The
3463 anon member has a named struct type, so it
3464 potentially is shared with other references.
3465 We need to unshare members so we can modify
3468 type1
.ref
= sym_push(anon_sym
++ | SYM_FIELD
,
3469 &type1
.ref
->type
, 0,
3471 pps
= &type1
.ref
->next
;
3472 while ((ass
= ass
->next
) != NULL
) {
3473 *pps
= sym_push(ass
->v
, &ass
->type
, 0, ass
->c
);
3474 pps
= &((*pps
)->next
);
3479 while ((ass
= ass
->next
) != NULL
)
3485 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
3489 if (tok
== ';' || tok
== TOK_EOF
)
3496 /* store size and alignment */
3497 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
3503 /* return 1 if basic type is a type size (short, long, long long) */
3504 ST_FUNC
int is_btype_size(int bt
)
3506 return bt
== VT_SHORT
|| bt
== VT_LONG
|| bt
== VT_LLONG
;
3509 /* Add type qualifiers to a type. If the type is an array then the qualifiers
3510 are added to the element type, copied because it could be a typedef. */
3511 static void parse_btype_qualify(CType
*type
, int qualifiers
)
3513 while (type
->t
& VT_ARRAY
) {
3514 type
->ref
= sym_push(SYM_FIELD
, &type
->ref
->type
, 0, type
->ref
->c
);
3515 type
= &type
->ref
->type
;
3517 type
->t
|= qualifiers
;
3520 /* return 0 if no type declaration. otherwise, return the basic type
3523 static int parse_btype(CType
*type
, AttributeDef
*ad
)
3525 int t
, u
, bt_size
, complete
, type_found
, typespec_found
;
3529 memset(ad
, 0, sizeof(AttributeDef
));
3537 /* currently, we really ignore extension */
3548 tcc_error("too many basic types");
3550 bt_size
= is_btype_size (u
& VT_BTYPE
);
3551 if (u
== VT_INT
|| (!bt_size
&& !(t
& VT_TYPEDEF
)))
3566 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
3567 #ifndef TCC_TARGET_PE
3568 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3570 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
3571 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3577 #ifdef TCC_TARGET_ARM64
3579 /* GCC's __uint128_t appears in some Linux header files. Make it a
3580 synonym for long double to get the size and alignment right. */
3592 if ((t
& VT_BTYPE
) == VT_LONG
) {
3593 #ifdef TCC_TARGET_PE
3594 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
3596 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3604 struct_decl(&type1
, ad
, VT_ENUM
);
3607 type
->ref
= type1
.ref
;
3611 struct_decl(&type1
, ad
, VT_STRUCT
);
3614 /* type modifiers */
3619 parse_btype_qualify(type
, VT_CONSTANT
);
3627 parse_btype_qualify(type
, VT_VOLATILE
);
3634 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == (VT_DEFSIGN
|VT_UNSIGNED
))
3635 tcc_error("signed and unsigned modifier");
3648 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == VT_DEFSIGN
)
3649 tcc_error("signed and unsigned modifier");
3650 t
|= VT_DEFSIGN
| VT_UNSIGNED
;
3675 /* GNUC attribute */
3676 case TOK_ATTRIBUTE1
:
3677 case TOK_ATTRIBUTE2
:
3678 parse_attribute(ad
);
3681 t
= (t
& ~VT_BTYPE
) | u
;
3689 parse_expr_type(&type1
);
3690 /* remove all storage modifiers except typedef */
3691 type1
.t
&= ~(VT_STORAGE
&~VT_TYPEDEF
);
3697 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
3700 type
->t
= ((s
->type
.t
& ~VT_TYPEDEF
) |
3701 (t
& ~(VT_CONSTANT
| VT_VOLATILE
)));
3702 type
->ref
= s
->type
.ref
;
3703 if (t
& (VT_CONSTANT
| VT_VOLATILE
))
3704 parse_btype_qualify(type
, t
& (VT_CONSTANT
| VT_VOLATILE
));
3708 /* get attributes from typedef */
3709 if (0 == ad
->a
.aligned
)
3710 ad
->a
.aligned
= s
->a
.aligned
;
3711 if (0 == ad
->a
.func_call
)
3712 ad
->a
.func_call
= s
->a
.func_call
;
3713 ad
->a
.packed
|= s
->a
.packed
;
3722 if (tcc_state
->char_is_unsigned
) {
3723 if ((t
& (VT_DEFSIGN
|VT_BTYPE
)) == VT_BYTE
)
3727 /* long is never used as type */
3728 if ((t
& VT_BTYPE
) == VT_LONG
)
3729 #if (!defined TCC_TARGET_X86_64 && !defined TCC_TARGET_ARM64) || \
3730 defined TCC_TARGET_PE
3731 t
= (t
& ~VT_BTYPE
) | VT_INT
;
3733 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3739 /* convert a function parameter type (array to pointer and function to
3740 function pointer) */
3741 static inline void convert_parameter_type(CType
*pt
)
3743 /* remove const and volatile qualifiers (XXX: const could be used
3744 to indicate a const function parameter */
3745 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3746 /* array must be transformed to pointer according to ANSI C */
3748 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
3753 ST_FUNC
void parse_asm_str(CString
*astr
)
3756 parse_mult_str(astr
, "string constant");
3759 /* Parse an asm label and return the token */
3760 static int asm_label_instr(void)
3766 parse_asm_str(&astr
);
3769 printf("asm_alias: \"%s\"\n", (char *)astr
.data
);
3771 v
= tok_alloc(astr
.data
, astr
.size
- 1)->tok
;
3776 static void post_type(CType
*type
, AttributeDef
*ad
)
3778 int n
, l
, t1
, arg_size
, align
;
3779 Sym
**plast
, *s
, *first
;
3784 /* function declaration */
3792 /* read param name and compute offset */
3793 if (l
!= FUNC_OLD
) {
3794 if (!parse_btype(&pt
, &ad1
)) {
3796 tcc_error("invalid type");
3803 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
3805 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3806 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
3807 tcc_error("parameter declared as void");
3808 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
3813 expect("identifier");
3817 convert_parameter_type(&pt
);
3818 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
3824 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
3831 /* if no parameters, then old type prototype */
3835 /* NOTE: const is ignored in returned type as it has a special
3836 meaning in gcc / C++ */
3837 type
->t
&= ~VT_CONSTANT
;
3838 /* some ancient pre-K&R C allows a function to return an array
3839 and the array brackets to be put after the arguments, such
3840 that "int c()[]" means something like "int[] c()" */
3843 skip(']'); /* only handle simple "[]" */
3846 /* we push a anonymous symbol which will contain the function prototype */
3847 ad
->a
.func_args
= arg_size
;
3848 s
= sym_push(SYM_FIELD
, type
, 0, l
);
3853 } else if (tok
== '[') {
3854 /* array definition */
3856 if (tok
== TOK_RESTRICT1
)
3861 if (!local_stack
|| nocode_wanted
)
3862 vpushi(expr_const());
3864 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3867 tcc_error("invalid array size");
3869 if (!is_integer_btype(vtop
->type
.t
& VT_BTYPE
))
3870 tcc_error("size of variable length array should be an integer");
3875 /* parse next post type */
3876 post_type(type
, ad
);
3877 if (type
->t
== VT_FUNC
)
3878 tcc_error("declaration of an array of functions");
3879 t1
|= type
->t
& VT_VLA
;
3882 loc
-= type_size(&int_type
, &align
);
3886 vla_runtime_type_size(type
, &align
);
3888 vset(&int_type
, VT_LOCAL
|VT_LVAL
, n
);
3895 /* we push an anonymous symbol which will contain the array
3897 s
= sym_push(SYM_FIELD
, type
, 0, n
);
3898 type
->t
= (t1
? VT_VLA
: VT_ARRAY
) | VT_PTR
;
3903 /* Parse a type declaration (except basic type), and return the type
3904 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3905 expected. 'type' should contain the basic type. 'ad' is the
3906 attribute definition of the basic type. It can be modified by
3909 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
3912 CType type1
, *type2
;
3913 int qualifiers
, storage
;
3915 while (tok
== '*') {
3923 qualifiers
|= VT_CONSTANT
;
3928 qualifiers
|= VT_VOLATILE
;
3934 /* XXX: clarify attribute handling */
3935 case TOK_ATTRIBUTE1
:
3936 case TOK_ATTRIBUTE2
:
3937 parse_attribute(ad
);
3941 type
->t
|= qualifiers
;
3944 /* recursive type */
3945 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3946 type1
.t
= 0; /* XXX: same as int */
3949 /* XXX: this is not correct to modify 'ad' at this point, but
3950 the syntax is not clear */
3951 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3952 parse_attribute(ad
);
3953 type_decl(&type1
, ad
, v
, td
);
3956 /* type identifier */
3957 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
3961 if (!(td
& TYPE_ABSTRACT
))
3962 expect("identifier");
3966 storage
= type
->t
& VT_STORAGE
;
3967 type
->t
&= ~VT_STORAGE
;
3968 if (storage
& VT_STATIC
) {
3969 int saved_nocode_wanted
= nocode_wanted
;
3971 post_type(type
, ad
);
3972 nocode_wanted
= saved_nocode_wanted
;
3974 post_type(type
, ad
);
3976 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3977 parse_attribute(ad
);
3981 /* append type at the end of type1 */
3994 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3995 ST_FUNC
int lvalue_type(int t
)
4000 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
4002 else if (bt
== VT_SHORT
)
4006 if (t
& VT_UNSIGNED
)
4007 r
|= VT_LVAL_UNSIGNED
;
4011 /* indirection with full error checking and bound check */
4012 ST_FUNC
void indir(void)
4014 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
4015 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
4019 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
4021 vtop
->type
= *pointed_type(&vtop
->type
);
4022 /* Arrays and functions are never lvalues */
4023 if (!(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_VLA
)
4024 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
4025 vtop
->r
|= lvalue_type(vtop
->type
.t
);
4026 /* if bound checking, the referenced pointer must be checked */
4027 #ifdef CONFIG_TCC_BCHECK
4028 if (tcc_state
->do_bounds_check
)
4029 vtop
->r
|= VT_MUSTBOUND
;
4034 /* pass a parameter to a function and do type checking and casting */
4035 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
4040 func_type
= func
->c
;
4041 if (func_type
== FUNC_OLD
||
4042 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
4043 /* default casting : only need to convert float to double */
4044 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
4047 } else if (vtop
->type
.t
& VT_BITFIELD
) {
4048 type
.t
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
4051 } else if (arg
== NULL
) {
4052 tcc_error("too many arguments to function");
4055 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
4056 gen_assign_cast(&type
);
4060 /* parse an expression of the form '(type)' or '(expr)' and return its
4062 static void parse_expr_type(CType
*type
)
4068 if (parse_btype(type
, &ad
)) {
4069 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
4076 static void parse_type(CType
*type
)
4081 if (!parse_btype(type
, &ad
)) {
4084 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
4087 static void vpush_tokc(int t
)
4092 vsetc(&type
, VT_CONST
, &tokc
);
4095 ST_FUNC
void unary(void)
4097 int n
, t
, align
, size
, r
, sizeof_caller
;
4102 sizeof_caller
= in_sizeof
;
4104 /* XXX: GCC 2.95.3 does not generate a table although it should be
4118 vpush_tokc(VT_INT
| VT_UNSIGNED
);
4122 vpush_tokc(VT_LLONG
);
4126 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
4130 vpush_tokc(VT_FLOAT
);
4134 vpush_tokc(VT_DOUBLE
);
4138 vpush_tokc(VT_LDOUBLE
);
4141 case TOK___FUNCTION__
:
4143 goto tok_identifier
;
4149 /* special function name identifier */
4150 len
= strlen(funcname
) + 1;
4151 /* generate char[len] type */
4156 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
4157 ptr
= section_ptr_add(data_section
, len
);
4158 memcpy(ptr
, funcname
, len
);
4163 #ifdef TCC_TARGET_PE
4164 t
= VT_SHORT
| VT_UNSIGNED
;
4170 /* string parsing */
4173 if (tcc_state
->warn_write_strings
)
4178 memset(&ad
, 0, sizeof(AttributeDef
));
4179 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, 0);
4184 if (parse_btype(&type
, &ad
)) {
4185 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
4187 /* check ISOC99 compound literal */
4189 /* data is allocated locally by default */
4194 /* all except arrays are lvalues */
4195 if (!(type
.t
& VT_ARRAY
))
4196 r
|= lvalue_type(type
.t
);
4197 memset(&ad
, 0, sizeof(AttributeDef
));
4198 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, 0);
4200 if (sizeof_caller
) {
4207 } else if (tok
== '{') {
4209 tcc_error("expected constant");
4210 /* save all registers */
4213 /* statement expression : we do not accept break/continue
4214 inside as GCC does */
4215 block(NULL
, NULL
, 1);
4230 /* functions names must be treated as function pointers,
4231 except for unary '&' and sizeof. Since we consider that
4232 functions are not lvalues, we only have to handle it
4233 there and in function calls. */
4234 /* arrays can also be used although they are not lvalues */
4235 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
4236 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
4238 mk_pointer(&vtop
->type
);
4244 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4246 boolean
.t
= VT_BOOL
;
4248 vtop
->c
.i
= !vtop
->c
.i
;
4249 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
4253 vseti(VT_JMP
, gvtst(1, 0));
4265 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
4266 tcc_error("pointer not accepted for unary plus");
4267 /* In order to force cast, we add zero, except for floating point
4268 where we really need an noop (otherwise -0.0 will be transformed
4270 if (!is_float(vtop
->type
.t
)) {
4281 unary_type(&type
); // Perform a in_sizeof = 0;
4282 size
= type_size(&type
, &align
);
4283 if (t
== TOK_SIZEOF
) {
4284 if (!(type
.t
& VT_VLA
)) {
4286 tcc_error("sizeof applied to an incomplete type");
4289 vla_runtime_type_size(&type
, &align
);
4294 vtop
->type
.t
|= VT_UNSIGNED
;
4297 case TOK_builtin_expect
:
4299 /* __builtin_expect is a no-op for now */
4300 int saved_nocode_wanted
;
4305 saved_nocode_wanted
= nocode_wanted
;
4309 nocode_wanted
= saved_nocode_wanted
;
4313 case TOK_builtin_types_compatible_p
:
4322 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
4323 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
4324 vpushi(is_compatible_types(&type1
, &type2
));
4327 case TOK_builtin_choose_expr
:
4329 int saved_nocode_wanted
, c
;
4335 saved_nocode_wanted
= nocode_wanted
;
4341 nocode_wanted
= saved_nocode_wanted
;
4345 saved_nocode_wanted
= nocode_wanted
;
4351 nocode_wanted
= saved_nocode_wanted
;
4356 case TOK_builtin_constant_p
:
4358 int saved_nocode_wanted
, res
;
4361 saved_nocode_wanted
= nocode_wanted
;
4364 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
4366 nocode_wanted
= saved_nocode_wanted
;
4371 case TOK_builtin_frame_address
:
4372 case TOK_builtin_return_address
:
4379 if (tok
!= TOK_CINT
) {
4380 tcc_error("%s only takes positive integers",
4381 tok1
== TOK_builtin_return_address
?
4382 "__builtin_return_address" :
4383 "__builtin_frame_address");
4385 level
= (uint32_t)tokc
.i
;
4390 vset(&type
, VT_LOCAL
, 0); /* local frame */
4392 mk_pointer(&vtop
->type
);
4393 indir(); /* -> parent frame */
4395 if (tok1
== TOK_builtin_return_address
) {
4396 // assume return address is just above frame pointer on stack
4399 mk_pointer(&vtop
->type
);
4404 #ifdef TCC_TARGET_X86_64
4405 #ifdef TCC_TARGET_PE
4406 case TOK_builtin_va_start
:
4414 if ((vtop
->r
& VT_VALMASK
) != VT_LOCAL
)
4415 tcc_error("__builtin_va_start expects a local variable");
4416 vtop
->r
&= ~(VT_LVAL
| VT_REF
);
4417 vtop
->type
= char_pointer_type
;
4423 case TOK_builtin_va_arg_types
:
4430 vpushi(classify_x86_64_va_arg(&type
));
4436 #ifdef TCC_TARGET_ARM64
4437 case TOK___va_start
: {
4439 tcc_error("statement in global scope");
4449 vtop
->type
.t
= VT_VOID
;
4452 case TOK___va_arg
: {
4455 tcc_error("statement in global scope");
4467 case TOK___arm64_clear_cache
: {
4476 vtop
->type
.t
= VT_VOID
;
4480 /* pre operations */
4491 t
= vtop
->type
.t
& VT_BTYPE
;
4493 /* In IEEE negate(x) isn't subtract(0,x), but rather
4498 else if (t
== VT_DOUBLE
)
4509 goto tok_identifier
;
4511 /* allow to take the address of a label */
4512 if (tok
< TOK_UIDENT
)
4513 expect("label identifier");
4514 s
= label_find(tok
);
4516 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4518 if (s
->r
== LABEL_DECLARED
)
4519 s
->r
= LABEL_FORWARD
;
4522 s
->type
.t
= VT_VOID
;
4523 mk_pointer(&s
->type
);
4524 s
->type
.t
|= VT_STATIC
;
4526 vpushsym(&s
->type
, s
);
4530 // special qnan , snan and infinity values
4532 vpush64(VT_DOUBLE
, 0x7ff8000000000000ULL
);
4536 vpush64(VT_DOUBLE
, 0x7ff0000000000001ULL
);
4540 vpush64(VT_DOUBLE
, 0x7ff0000000000000ULL
);
4549 expect("identifier");
4552 const char *name
= get_tok_str(t
, NULL
);
4554 tcc_error("'%s' undeclared", name
);
4555 /* for simple function calls, we tolerate undeclared
4556 external reference to int() function */
4557 if (tcc_state
->warn_implicit_function_declaration
4558 #ifdef TCC_TARGET_PE
4559 /* people must be warned about using undeclared WINAPI functions
4560 (which usually start with uppercase letter) */
4561 || (name
[0] >= 'A' && name
[0] <= 'Z')
4564 tcc_warning("implicit declaration of function '%s'", name
);
4565 s
= external_global_sym(t
, &func_old_type
, 0);
4567 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
4568 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
4569 /* if referencing an inline function, then we generate a
4570 symbol to it if not already done. It will have the
4571 effect to generate code for it at the end of the
4572 compilation unit. Inline function as always
4573 generated in the text section. */
4575 put_extern_sym(s
, text_section
, 0, 0);
4576 r
= VT_SYM
| VT_CONST
;
4580 vset(&s
->type
, r
, s
->c
);
4581 /* if forward reference, we must point to s */
4582 if (vtop
->r
& VT_SYM
) {
4589 /* post operations */
4591 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
4594 } else if (tok
== '.' || tok
== TOK_ARROW
|| tok
== TOK_CDOUBLE
) {
4597 if (tok
== TOK_ARROW
)
4599 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
4602 /* expect pointer on structure */
4603 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
4604 expect("struct or union");
4605 if (tok
== TOK_CDOUBLE
)
4606 expect("field name");
4608 if (tok
== TOK_CINT
|| tok
== TOK_CUINT
)
4609 expect("field name");
4610 s
= find_field(&vtop
->type
, tok
);
4612 tcc_error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, &tokc
));
4613 /* add field offset to pointer */
4614 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
4617 /* change type to field type, and set to lvalue */
4618 vtop
->type
= s
->type
;
4619 vtop
->type
.t
|= qualifiers
;
4620 /* an array is never an lvalue */
4621 if (!(vtop
->type
.t
& VT_ARRAY
)) {
4622 vtop
->r
|= lvalue_type(vtop
->type
.t
);
4623 #ifdef CONFIG_TCC_BCHECK
4624 /* if bound checking, the referenced pointer must be checked */
4625 if (tcc_state
->do_bounds_check
&& (vtop
->r
& VT_VALMASK
) != VT_LOCAL
)
4626 vtop
->r
|= VT_MUSTBOUND
;
4630 } else if (tok
== '[') {
4636 } else if (tok
== '(') {
4639 int nb_args
, ret_nregs
, ret_align
, regsize
, variadic
;
4642 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
4643 /* pointer test (no array accepted) */
4644 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
4645 vtop
->type
= *pointed_type(&vtop
->type
);
4646 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
4650 expect("function pointer");
4653 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
4655 /* get return type */
4658 sa
= s
->next
; /* first parameter */
4661 /* compute first implicit argument if a structure is returned */
4662 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
4663 variadic
= (s
->c
== FUNC_ELLIPSIS
);
4664 ret_nregs
= gfunc_sret(&s
->type
, variadic
, &ret
.type
,
4665 &ret_align
, ®size
);
4667 /* get some space for the returned structure */
4668 size
= type_size(&s
->type
, &align
);
4669 #ifdef TCC_TARGET_ARM64
4670 /* On arm64, a small struct is return in registers.
4671 It is much easier to write it to memory if we know
4672 that we are allowed to write some extra bytes, so
4673 round the allocated space up to a power of 2: */
4675 while (size
& (size
- 1))
4676 size
= (size
| (size
- 1)) + 1;
4678 loc
= (loc
- size
) & -align
;
4680 ret
.r
= VT_LOCAL
| VT_LVAL
;
4681 /* pass it as 'int' to avoid structure arg passing
4683 vseti(VT_LOCAL
, loc
);
4693 /* return in register */
4694 if (is_float(ret
.type
.t
)) {
4695 ret
.r
= reg_fret(ret
.type
.t
);
4696 #ifdef TCC_TARGET_X86_64
4697 if ((ret
.type
.t
& VT_BTYPE
) == VT_QFLOAT
)
4701 #ifndef TCC_TARGET_ARM64
4702 #ifdef TCC_TARGET_X86_64
4703 if ((ret
.type
.t
& VT_BTYPE
) == VT_QLONG
)
4705 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
4716 gfunc_param_typed(s
, sa
);
4726 tcc_error("too few arguments to function");
4728 if (!nocode_wanted
) {
4729 gfunc_call(nb_args
);
4731 vtop
-= (nb_args
+ 1);
4735 for (r
= ret
.r
+ ret_nregs
+ !ret_nregs
; r
-- > ret
.r
;) {
4736 vsetc(&ret
.type
, r
, &ret
.c
);
4737 vtop
->r2
= ret
.r2
; /* Loop only happens when r2 is VT_CONST */
4740 /* handle packed struct return */
4741 if (((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) && ret_nregs
) {
4744 size
= type_size(&s
->type
, &align
);
4745 /* We're writing whole regs often, make sure there's enough
4746 space. Assume register size is power of 2. */
4747 if (regsize
> align
)
4749 loc
= (loc
- size
) & -align
;
4753 vset(&ret
.type
, VT_LOCAL
| VT_LVAL
, addr
+ offset
);
4757 if (--ret_nregs
== 0)
4761 vset(&s
->type
, VT_LOCAL
| VT_LVAL
, addr
);
4769 ST_FUNC
void expr_prod(void)
4774 while (tok
== '*' || tok
== '/' || tok
== '%') {
4782 ST_FUNC
void expr_sum(void)
4787 while (tok
== '+' || tok
== '-') {
4795 static void expr_shift(void)
4800 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
4808 static void expr_cmp(void)
4813 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
4814 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
4822 static void expr_cmpeq(void)
4827 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
4835 static void expr_and(void)
4838 while (tok
== '&') {
4845 static void expr_xor(void)
4848 while (tok
== '^') {
4855 static void expr_or(void)
4858 while (tok
== '|') {
4865 /* XXX: fix this mess */
4866 static void expr_land_const(void)
4869 while (tok
== TOK_LAND
) {
4875 static void expr_lor_const(void)
4878 while (tok
== TOK_LOR
) {
4885 static void expr_land(void)
4888 if (tok
== TOK_LAND
) {
4889 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4900 int saved_nocode_wanted
= nocode_wanted
;
4904 nocode_wanted
= saved_nocode_wanted
;
4912 if (tok
!= TOK_LAND
) {
4923 static void expr_lor(void)
4926 if (tok
== TOK_LOR
) {
4927 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4934 int saved_nocode_wanted
= nocode_wanted
;
4938 nocode_wanted
= saved_nocode_wanted
;
4950 if (tok
!= TOK_LOR
) {
4961 static void expr_cond(void)
4963 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
, islv
;
4965 CType type
, type1
, type2
;
4970 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4971 int saved_nocode_wanted
= nocode_wanted
;
4974 boolean
.t
= VT_BOOL
;
4980 if (tok
!= ':' || !gnu_ext
) {
4988 nocode_wanted
= saved_nocode_wanted
;
4991 if (tok
!= ':' || !gnu_ext
) {
4995 nocode_wanted
= saved_nocode_wanted
;
5002 if (vtop
!= vstack
) {
5003 /* needed to avoid having different registers saved in
5005 if (is_float(vtop
->type
.t
)) {
5007 #ifdef TCC_TARGET_X86_64
5008 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
5018 if (tok
== ':' && gnu_ext
) {
5026 sv
= *vtop
; /* save value to handle it later */
5027 vtop
--; /* no vpop so that FP stack is not flushed */
5035 bt1
= t1
& VT_BTYPE
;
5037 bt2
= t2
& VT_BTYPE
;
5038 /* cast operands to correct type according to ISOC rules */
5039 if (is_float(bt1
) || is_float(bt2
)) {
5040 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
5041 type
.t
= VT_LDOUBLE
;
5042 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
5047 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
5048 /* cast to biggest op */
5050 /* convert to unsigned if it does not fit in a long long */
5051 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
5052 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
5053 type
.t
|= VT_UNSIGNED
;
5054 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
5055 /* If one is a null ptr constant the result type
5057 if (is_null_pointer (vtop
))
5059 else if (is_null_pointer (&sv
))
5061 /* XXX: test pointer compatibility, C99 has more elaborate
5065 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
5066 /* XXX: test function pointer compatibility */
5067 type
= bt1
== VT_FUNC
? type1
: type2
;
5068 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
5069 /* XXX: test structure compatibility */
5070 type
= bt1
== VT_STRUCT
? type1
: type2
;
5071 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
5072 /* NOTE: as an extension, we accept void on only one side */
5075 /* integer operations */
5077 /* convert to unsigned if it does not fit in an integer */
5078 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
5079 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
5080 type
.t
|= VT_UNSIGNED
;
5082 /* keep structs lvalue by transforming `(expr ? a : b)` to `*(expr ? &a : &b)` so
5083 that `(expr ? a : b).mem` does not error with "lvalue expected" */
5084 islv
= (vtop
->r
& VT_LVAL
) && (sv
.r
& VT_LVAL
) && VT_STRUCT
== (type
.t
& VT_BTYPE
);
5086 /* now we convert second operand */
5089 mk_pointer(&vtop
->type
);
5092 else if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
5095 if (is_float(type
.t
)) {
5097 #ifdef TCC_TARGET_X86_64
5098 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
5102 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
5103 /* for long longs, we use fixed registers to avoid having
5104 to handle a complicated move */
5109 /* this is horrible, but we must also convert first
5113 /* put again first value and cast it */
5117 mk_pointer(&vtop
->type
);
5120 else if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
5123 move_reg(r2
, r1
, type
.t
);
5132 static void expr_eq(void)
5138 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
5139 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
5140 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
5155 ST_FUNC
void gexpr(void)
5166 /* parse an expression and return its type without any side effect. */
5167 static void expr_type(CType
*type
)
5169 int saved_nocode_wanted
;
5171 saved_nocode_wanted
= nocode_wanted
;
5176 nocode_wanted
= saved_nocode_wanted
;
5179 /* parse a unary expression and return its type without any side
5181 static void unary_type(CType
*type
)
5193 /* parse a constant expression and return value in vtop. */
5194 static void expr_const1(void)
5203 /* parse an integer constant and return its value. */
5204 ST_FUNC
int expr_const(void)
5208 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
5209 expect("constant expression");
5215 /* return the label token if current token is a label, otherwise
5217 static int is_label(void)
5221 /* fast test first */
5222 if (tok
< TOK_UIDENT
)
5224 /* no need to save tokc because tok is an identifier */
5231 unget_tok(last_tok
);
5236 static void label_or_decl(int l
)
5240 /* fast test first */
5241 if (tok
>= TOK_UIDENT
)
5243 /* no need to save tokc because tok is an identifier */
5247 unget_tok(last_tok
);
5250 unget_tok(last_tok
);
5255 static int case_cmp(const void *pa
, const void *pb
)
5257 int a
= (*(struct case_t
**) pa
)->v1
;
5258 int b
= (*(struct case_t
**) pb
)->v1
;
5259 return a
< b
? -1 : a
> b
;
5262 static int gcase(struct case_t
**base
, int len
, int case_reg
, int *bsym
)
5274 case_reg
= gv(RC_INT
);
5280 gtst_addr(0, p
->sym
); /* v1 <= x <= v2 */
5281 case_reg
= gv(RC_INT
);
5284 case_reg
= gcase(base
, len
/2, case_reg
, bsym
);
5285 if (cur_switch
->def_sym
)
5286 gjmp_addr(cur_switch
->def_sym
);
5288 *bsym
= gjmp(*bsym
);
5292 base
+= e
; len
-= e
;
5300 if (p
->v1
== p
->v2
) {
5302 gtst_addr(0, p
->sym
);
5306 case_reg
= gv(RC_INT
);
5312 gtst_addr(0, p
->sym
);
5315 case_reg
= gv(RC_INT
);
5321 static void block(int *bsym
, int *csym
, int is_expr
)
5326 /* generate line number info */
5327 if (tcc_state
->do_debug
&&
5328 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
5329 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
5331 last_line_num
= file
->line_num
;
5335 /* default return value is (void) */
5337 vtop
->type
.t
= VT_VOID
;
5340 if (tok
== TOK_IF
) {
5347 block(bsym
, csym
, 0);
5349 if (c
== TOK_ELSE
) {
5353 block(bsym
, csym
, 0);
5354 gsym(d
); /* patch else jmp */
5357 } else if (tok
== TOK_WHILE
) {
5373 } else if (tok
== '{') {
5375 int block_vla_sp_loc
= vla_sp_loc
, saved_vlas_in_scope
= vlas_in_scope
;
5378 /* record local declaration stack position */
5380 llabel
= local_label_stack
;
5383 /* handle local labels declarations */
5384 if (tok
== TOK_LABEL
) {
5387 if (tok
< TOK_UIDENT
)
5388 expect("label identifier");
5389 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
5399 while (tok
!= '}') {
5400 label_or_decl(VT_LOCAL
);
5404 block(bsym
, csym
, is_expr
);
5407 /* pop locally defined labels */
5408 label_pop(&local_label_stack
, llabel
);
5410 /* XXX: this solution makes only valgrind happy...
5411 triggered by gcc.c-torture/execute/20000917-1.c */
5413 switch(vtop
->type
.t
& VT_BTYPE
) {
5415 /* this breaks a compilation of the linux kernel v2.4.26 */
5416 /* pmd_t *new = ({ __asm__ __volatile__("ud2\n") ; ((pmd_t *)1); }); */
5417 /* Look a commit a80acab: Display error on statement expressions with complex return type */
5418 /* A pointer is not a complex return type */
5422 for(p
=vtop
->type
.ref
;p
;p
=p
->prev
)
5424 tcc_error("unsupported expression type");
5427 /* pop locally defined symbols */
5429 sym_pop(&local_stack
, s
);
5431 /* Pop VLA frames and restore stack pointer if required */
5432 if (vlas_in_scope
> saved_vlas_in_scope
) {
5433 vla_sp_loc
= saved_vlas_in_scope
? block_vla_sp_loc
: vla_sp_root_loc
;
5436 vlas_in_scope
= saved_vlas_in_scope
;
5439 } else if (tok
== TOK_RETURN
) {
5443 gen_assign_cast(&func_vt
);
5444 #ifdef TCC_TARGET_ARM64
5445 // Perhaps it would be better to use this for all backends:
5448 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
5449 CType type
, ret_type
;
5450 int ret_align
, ret_nregs
, regsize
;
5451 ret_nregs
= gfunc_sret(&func_vt
, func_var
, &ret_type
,
5452 &ret_align
, ®size
);
5453 if (0 == ret_nregs
) {
5454 /* if returning structure, must copy it to implicit
5455 first pointer arg location */
5458 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
5461 /* copy structure value to pointer */
5464 /* returning structure packed into registers */
5465 int r
, size
, addr
, align
;
5466 size
= type_size(&func_vt
,&align
);
5467 if ((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) ||
5468 (vtop
->c
.i
& (ret_align
-1)))
5469 && (align
& (ret_align
-1))) {
5470 loc
= (loc
- size
) & -ret_align
;
5473 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
5477 vset(&ret_type
, VT_LOCAL
| VT_LVAL
, addr
);
5479 vtop
->type
= ret_type
;
5480 if (is_float(ret_type
.t
))
5481 r
= rc_fret(ret_type
.t
);
5492 if (--ret_nregs
== 0)
5494 /* We assume that when a structure is returned in multiple
5495 registers, their classes are consecutive values of the
5498 vtop
->c
.i
+= regsize
;
5502 } else if (is_float(func_vt
.t
)) {
5503 gv(rc_fret(func_vt
.t
));
5508 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
5511 /* jump unless last stmt in top-level block */
5512 if (tok
!= '}' || local_scope
!= 1)
5514 } else if (tok
== TOK_BREAK
) {
5517 tcc_error("cannot break");
5518 *bsym
= gjmp(*bsym
);
5521 } else if (tok
== TOK_CONTINUE
) {
5524 tcc_error("cannot continue");
5525 vla_sp_restore_root();
5526 *csym
= gjmp(*csym
);
5529 } else if (tok
== TOK_FOR
) {
5536 /* c99 for-loop init decl? */
5537 if (!decl0(VT_LOCAL
, 1)) {
5538 /* no, regular for-loop init expr */
5570 sym_pop(&local_stack
, s
);
5573 if (tok
== TOK_DO
) {
5590 if (tok
== TOK_SWITCH
) {
5591 struct switch_t
*saved
, sw
;
5595 /* XXX: other types than integer */
5600 b
= gjmp(0); /* jump to first case */
5601 sw
.p
= NULL
; sw
.n
= 0; sw
.def_sym
= 0;
5605 a
= gjmp(a
); /* add implicit break */
5608 qsort(sw
.p
, sw
.n
, sizeof(void*), case_cmp
);
5609 for (b
= 1; b
< sw
.n
; b
++)
5610 if (sw
.p
[b
- 1]->v2
>= sw
.p
[b
]->v1
)
5611 tcc_error("duplicate case value");
5612 gcase(sw
.p
, sw
.n
, c
, &a
);
5614 gjmp_addr(sw
.def_sym
);
5615 dynarray_reset(&sw
.p
, &sw
.n
);
5620 if (tok
== TOK_CASE
) {
5621 struct case_t
*cr
= tcc_malloc(sizeof(struct case_t
));
5625 cr
->v1
= cr
->v2
= expr_const();
5626 if (gnu_ext
&& tok
== TOK_DOTS
) {
5628 cr
->v2
= expr_const();
5629 if (cr
->v2
< cr
->v1
)
5630 tcc_warning("empty case range");
5633 dynarray_add((void***) &cur_switch
->p
, &cur_switch
->n
, cr
);
5636 goto block_after_label
;
5638 if (tok
== TOK_DEFAULT
) {
5643 if (cur_switch
->def_sym
)
5644 tcc_error("too many 'default'");
5645 cur_switch
->def_sym
= ind
;
5647 goto block_after_label
;
5649 if (tok
== TOK_GOTO
) {
5651 if (tok
== '*' && gnu_ext
) {
5655 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
5658 } else if (tok
>= TOK_UIDENT
) {
5659 s
= label_find(tok
);
5660 /* put forward definition if needed */
5662 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
5664 if (s
->r
== LABEL_DECLARED
)
5665 s
->r
= LABEL_FORWARD
;
5667 vla_sp_restore_root();
5668 if (s
->r
& LABEL_FORWARD
)
5669 s
->jnext
= gjmp(s
->jnext
);
5671 gjmp_addr(s
->jnext
);
5674 expect("label identifier");
5677 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
5685 if (s
->r
== LABEL_DEFINED
)
5686 tcc_error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
5688 s
->r
= LABEL_DEFINED
;
5690 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
5694 /* we accept this, but it is a mistake */
5697 tcc_warning("deprecated use of label at end of compound statement");
5701 block(bsym
, csym
, is_expr
);
5704 /* expression case */
5719 #define EXPR_CONST 1
5722 static void parse_init_elem(int expr_type
)
5724 int saved_global_expr
;
5727 /* compound literals must be allocated globally in this case */
5728 saved_global_expr
= global_expr
;
5731 global_expr
= saved_global_expr
;
5732 /* NOTE: symbols are accepted */
5733 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
5734 tcc_error("initializer element is not constant");
5742 /* t is the array or struct type. c is the array or struct
5743 address. cur_field is the pointer to the current
5744 value, for arrays the 'c' member contains the current start
5745 index and the 'r' contains the end index (in case of range init).
5746 'size_only' is true if only size info is needed (only used
5748 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
5749 Sym
**cur_field
, int size_only
)
5752 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
5758 if (gnu_ext
&& (l
= is_label()) != 0)
5760 while (tok
== '[' || tok
== '.') {
5762 if (!(type
->t
& VT_ARRAY
))
5763 expect("array type");
5766 index
= expr_const();
5767 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
5768 tcc_error("invalid index");
5769 if (tok
== TOK_DOTS
&& gnu_ext
) {
5771 index_last
= expr_const();
5772 if (index_last
< 0 ||
5773 (s
->c
>= 0 && index_last
>= s
->c
) ||
5775 tcc_error("invalid index");
5781 (*cur_field
)->c
= index
;
5782 (*cur_field
)->r
= index_last
;
5784 type
= pointed_type(type
);
5785 elem_size
= type_size(type
, &align
);
5786 c
+= index
* elem_size
;
5787 /* NOTE: we only support ranges for last designator */
5788 nb_elems
= index_last
- index
+ 1;
5789 if (nb_elems
!= 1) {
5798 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
5799 expect("struct/union type");
5800 f
= find_field(type
, l
);
5805 /* XXX: fix this mess by using explicit storage field */
5807 type1
.t
|= (type
->t
& ~VT_TYPE
);
5821 if (type
->t
& VT_ARRAY
) {
5822 index
= (*cur_field
)->c
;
5823 if (type
->ref
->c
>= 0 && index
>= type
->ref
->c
)
5824 tcc_error("index too large");
5825 type
= pointed_type(type
);
5826 c
+= index
* type_size(type
, &align
);
5830 tcc_error("too many field init");
5831 /* XXX: fix this mess by using explicit storage field */
5833 type1
.t
|= (type
->t
& ~VT_TYPE
);
5838 decl_initializer(type
, sec
, c
, 0, size_only
);
5840 /* XXX: make it more general */
5841 if (!size_only
&& nb_elems
> 1) {
5842 unsigned long c_end
;
5847 vset(type
, VT_LOCAL
|VT_LVAL
, c
);
5848 for (i
= 1; i
< nb_elems
; i
++) {
5849 vset(type
, VT_LOCAL
|VT_LVAL
, c
+ elem_size
* i
);
5855 c_end
= c
+ nb_elems
* elem_size
;
5856 if (c_end
> sec
->data_allocated
)
5857 section_realloc(sec
, c_end
);
5858 src
= sec
->data
+ c
;
5860 for(i
= 1; i
< nb_elems
; i
++) {
5862 memcpy(dst
, src
, elem_size
);
5868 /* store a value or an expression directly in global data or in local array */
5869 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
)
5871 int bt
, bit_pos
, bit_size
;
5873 unsigned long long bit_mask
;
5877 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
5881 /* XXX: not portable */
5882 /* XXX: generate error if incorrect relocation */
5883 gen_assign_cast(&dtype
);
5884 bt
= type
->t
& VT_BTYPE
;
5885 size
= type_size(type
, &align
);
5886 if (c
+ size
> sec
->data_allocated
) {
5887 section_realloc(sec
, c
+ size
);
5889 ptr
= sec
->data
+ c
;
5890 /* XXX: make code faster ? */
5891 if (!(type
->t
& VT_BITFIELD
)) {
5893 bit_size
= PTR_SIZE
* 8;
5896 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5897 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
5898 bit_mask
= (1LL << bit_size
) - 1;
5900 if ((vtop
->r
& (VT_SYM
|VT_CONST
)) == (VT_SYM
|VT_CONST
) &&
5901 vtop
->sym
->v
>= SYM_FIRST_ANOM
&&
5902 /* XXX This rejects compount literals like
5903 '(void *){ptr}'. The problem is that '&sym' is
5904 represented the same way, which would be ruled out
5905 by the SYM_FIRST_ANOM check above, but also '"string"'
5906 in 'char *p = "string"' is represented the same
5907 with the type being VT_PTR and the symbol being an
5908 anonymous one. That is, there's no difference in vtop
5909 between '(void *){x}' and '&(void *){x}'. Ignore
5910 pointer typed entities here. Hopefully no real code
5911 will every use compound literals with scalar type. */
5912 (vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
5913 /* These come from compound literals, memcpy stuff over. */
5916 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[vtop
->sym
->c
];
5917 ssec
= tcc_state
->sections
[esym
->st_shndx
];
5918 memmove (ptr
, ssec
->data
+ esym
->st_value
, size
);
5920 if ((vtop
->r
& VT_SYM
) &&
5926 (bt
== VT_LLONG
&& bit_size
!= 64) ||
5930 (bt
== VT_INT
&& bit_size
!= 32)
5933 tcc_error("initializer element is not computable at load time");
5935 /* XXX: when cross-compiling we assume that each type has the
5936 same representation on host and target, which is likely to
5937 be wrong in the case of long double */
5939 vtop
->c
.i
= (vtop
->c
.i
!= 0);
5941 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5944 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5947 *(double *)ptr
= vtop
->c
.d
;
5950 if (sizeof(long double) == LDOUBLE_SIZE
)
5951 *(long double *)ptr
= vtop
->c
.ld
;
5952 else if (sizeof(double) == LDOUBLE_SIZE
)
5953 *(double *)ptr
= vtop
->c
.ld
;
5955 tcc_error("can't cross compile long double constants");
5959 *(long long *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5966 addr_t val
= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5967 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
5968 if (vtop
->r
& VT_SYM
)
5969 greloca(sec
, vtop
->sym
, c
, R_DATA_PTR
, val
);
5971 *(addr_t
*)ptr
|= val
;
5973 if (vtop
->r
& VT_SYM
)
5974 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
5975 *(addr_t
*)ptr
|= val
;
5981 int val
= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5982 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
5983 if (vtop
->r
& VT_SYM
)
5984 greloca(sec
, vtop
->sym
, c
, R_DATA_PTR
, val
);
5988 if (vtop
->r
& VT_SYM
)
5989 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
5998 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
6005 /* put zeros for variable based init */
6006 static void init_putz(Section
*sec
, unsigned long c
, int size
)
6009 /* nothing to do because globals are already set to zero */
6011 vpush_global_sym(&func_old_type
, TOK_memset
);
6013 #ifdef TCC_TARGET_ARM
6024 /* 't' contains the type and storage info. 'c' is the offset of the
6025 object in section 'sec'. If 'sec' is NULL, it means stack based
6026 allocation. 'first' is true if array '{' must be read (multi
6027 dimension implicit array init handling). 'size_only' is true if
6028 size only evaluation is wanted (only for arrays). */
6029 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
6030 int first
, int size_only
)
6032 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, parlevel1
, i
;
6039 /* If we currently are at an '}' or ',' we have read an initializer
6040 element in one of our callers, and not yet consumed it. */
6041 have_elem
= tok
== '}' || tok
== ',';
6042 if (!have_elem
&& tok
!= '{' &&
6043 /* In case of strings we have special handling for arrays, so
6044 don't consume them as initializer value (which would commit them
6045 to some anonymous symbol). */
6046 tok
!= TOK_LSTR
&& tok
!= TOK_STR
&&
6048 parse_init_elem(!sec
? EXPR_ANY
: EXPR_CONST
);
6053 !(type
->t
& VT_ARRAY
) &&
6054 /* Use i_c_parameter_t, to strip toplevel qualifiers.
6055 The source type might have VT_CONSTANT set, which is
6056 of course assignable to non-const elements. */
6057 is_compatible_parameter_types(type
, &vtop
->type
)) {
6058 init_putv(type
, sec
, c
);
6059 } else if (type
->t
& VT_ARRAY
) {
6063 t1
= pointed_type(type
);
6064 size1
= type_size(t1
, &align1
);
6067 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
6070 tcc_error("character array initializer must be a literal,"
6071 " optionally enclosed in braces");
6076 /* only parse strings here if correct type (otherwise: handle
6077 them as ((w)char *) expressions */
6078 if ((tok
== TOK_LSTR
&&
6079 #ifdef TCC_TARGET_PE
6080 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
6082 (t1
->t
& VT_BTYPE
) == VT_INT
6084 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
6085 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
6088 /* compute maximum number of chars wanted */
6090 cstr_len
= tokc
.str
.size
;
6092 cstr_len
= tokc
.str
.size
/ sizeof(nwchar_t
);
6095 if (n
>= 0 && nb
> (n
- array_length
))
6096 nb
= n
- array_length
;
6099 tcc_warning("initializer-string for array is too long");
6100 /* in order to go faster for common case (char
6101 string in global variable, we handle it
6103 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
6104 memcpy(sec
->data
+ c
+ array_length
, tokc
.str
.data
, nb
);
6108 ch
= ((unsigned char *)tokc
.str
.data
)[i
];
6110 ch
= ((nwchar_t
*)tokc
.str
.data
)[i
];
6112 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
);
6119 /* only add trailing zero if enough storage (no
6120 warning in this case since it is standard) */
6121 if (n
< 0 || array_length
< n
) {
6124 init_putv(t1
, sec
, c
+ (array_length
* size1
));
6134 while (tok
!= '}' || have_elem
) {
6135 decl_designator(type
, sec
, c
, &f
, size_only
);
6138 /* must put zero in holes (note that doing it that way
6139 ensures that it even works with designators) */
6140 if (!size_only
&& array_length
< index
) {
6141 init_putz(sec
, c
+ array_length
* size1
,
6142 (index
- array_length
) * size1
);
6144 if (type
->t
& VT_ARRAY
) {
6145 index
= indexsym
.c
= ++indexsym
.r
;
6147 index
= index
+ type_size(&f
->type
, &align1
);
6148 if (s
->type
.t
== TOK_UNION
)
6153 if (index
> array_length
)
6154 array_length
= index
;
6156 if (type
->t
& VT_ARRAY
) {
6157 /* special test for multi dimensional arrays (may not
6158 be strictly correct if designators are used at the
6160 if (no_oblock
&& index
>= n
)
6163 if (no_oblock
&& f
== NULL
)
6171 /* put zeros at the end */
6172 if (!size_only
&& array_length
< n
) {
6173 init_putz(sec
, c
+ array_length
* size1
,
6174 (n
- array_length
) * size1
);
6178 /* patch type size if needed, which happens only for array types */
6180 s
->c
= array_length
;
6181 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
6184 if (first
|| tok
== '{') {
6193 } else if (tok
== '{') {
6195 decl_initializer(type
, sec
, c
, first
, size_only
);
6197 } else if (size_only
) {
6198 /* If we supported only ISO C we wouldn't have to accept calling
6199 this on anything than an array size_only==1 (and even then
6200 only on the outermost level, so no recursion would be needed),
6201 because initializing a flex array member isn't supported.
6202 But GNU C supports it, so we need to recurse even into
6203 subfields of structs and arrays when size_only is set. */
6204 /* just skip expression */
6205 parlevel
= parlevel1
= 0;
6206 while ((parlevel
> 0 || parlevel1
> 0 ||
6207 (tok
!= '}' && tok
!= ',')) && tok
!= -1) {
6210 else if (tok
== ')') {
6211 if (parlevel
== 0 && parlevel1
== 0)
6215 else if (tok
== '{')
6217 else if (tok
== '}') {
6218 if (parlevel
== 0 && parlevel1
== 0)
6226 /* This should happen only when we haven't parsed
6227 the init element above for fear of committing a
6228 string constant to memory too early. */
6229 if (tok
!= TOK_STR
&& tok
!= TOK_LSTR
)
6230 expect("string constant");
6231 parse_init_elem(!sec
? EXPR_ANY
: EXPR_CONST
);
6233 init_putv(type
, sec
, c
);
6237 /* parse an initializer for type 't' if 'has_init' is non zero, and
6238 allocate space in local or global data space ('r' is either
6239 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
6240 variable 'v' of scope 'scope' is declared before initializers
6241 are parsed. If 'v' is zero, then a reference to the new object
6242 is put in the value stack. If 'has_init' is 2, a special parsing
6243 is done to handle string constants. */
6244 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
6245 int has_init
, int v
, int scope
)
6247 int size
, align
, addr
, data_offset
;
6249 ParseState saved_parse_state
= {0};
6250 TokenString
*init_str
= NULL
;
6252 Sym
*flexible_array
;
6254 flexible_array
= NULL
;
6255 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
6256 Sym
*field
= type
->ref
->next
;
6259 field
= field
->next
;
6260 if (field
->type
.t
& VT_ARRAY
&& field
->type
.ref
->c
< 0)
6261 flexible_array
= field
;
6265 size
= type_size(type
, &align
);
6266 /* If unknown size, we must evaluate it before
6267 evaluating initializers because
6268 initializers can generate global data too
6269 (e.g. string pointers or ISOC99 compound
6270 literals). It also simplifies local
6271 initializers handling */
6272 if (size
< 0 || (flexible_array
&& has_init
)) {
6274 tcc_error("unknown type size");
6275 /* get all init string */
6276 init_str
= tok_str_alloc();
6277 if (has_init
== 2) {
6278 /* only get strings */
6279 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
6280 tok_str_add_tok(init_str
);
6285 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
6287 tcc_error("unexpected end of file in initializer");
6288 tok_str_add_tok(init_str
);
6291 else if (tok
== '}') {
6301 tok_str_add(init_str
, -1);
6302 tok_str_add(init_str
, 0);
6305 save_parse_state(&saved_parse_state
);
6307 begin_macro(init_str
, 1);
6309 decl_initializer(type
, NULL
, 0, 1, 1);
6310 /* prepare second initializer parsing */
6311 macro_ptr
= init_str
->str
;
6314 /* if still unknown size, error */
6315 size
= type_size(type
, &align
);
6317 tcc_error("unknown type size");
6319 /* If there's a flex member and it was used in the initializer
6321 if (flexible_array
&&
6322 flexible_array
->type
.ref
->c
> 0)
6323 size
+= flexible_array
->type
.ref
->c
6324 * pointed_size(&flexible_array
->type
);
6325 /* take into account specified alignment if bigger */
6326 if (ad
->a
.aligned
) {
6327 if (ad
->a
.aligned
> align
)
6328 align
= ad
->a
.aligned
;
6329 } else if (ad
->a
.packed
) {
6332 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
6334 #ifdef CONFIG_TCC_BCHECK
6335 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
6339 loc
= (loc
- size
) & -align
;
6341 #ifdef CONFIG_TCC_BCHECK
6342 /* handles bounds */
6343 /* XXX: currently, since we do only one pass, we cannot track
6344 '&' operators, so we add only arrays */
6345 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
6347 /* add padding between regions */
6349 /* then add local bound info */
6350 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(addr_t
));
6351 bounds_ptr
[0] = addr
;
6352 bounds_ptr
[1] = size
;
6356 /* local variable */
6357 sym_push(v
, type
, r
, addr
);
6359 /* push local reference */
6360 vset(type
, r
, addr
);
6366 if (v
&& scope
== VT_CONST
) {
6367 /* see if the symbol was already defined */
6370 if (!is_compatible_types(&sym
->type
, type
))
6371 tcc_error("incompatible types for redefinition of '%s'",
6372 get_tok_str(v
, NULL
));
6373 if (sym
->type
.t
& VT_EXTERN
) {
6374 /* if the variable is extern, it was not allocated */
6375 sym
->type
.t
&= ~VT_EXTERN
;
6376 /* set array size if it was omitted in extern
6378 if ((sym
->type
.t
& VT_ARRAY
) &&
6379 sym
->type
.ref
->c
< 0 &&
6381 sym
->type
.ref
->c
= type
->ref
->c
;
6383 /* we accept several definitions of the same
6384 global variable. this is tricky, because we
6385 must play with the SHN_COMMON type of the symbol */
6386 /* XXX: should check if the variable was already
6387 initialized. It is incorrect to initialized it
6389 /* no init data, we won't add more to the symbol */
6396 /* allocate symbol in corresponding section */
6401 else if (tcc_state
->nocommon
)
6405 data_offset
= sec
->data_offset
;
6406 data_offset
= (data_offset
+ align
- 1) & -align
;
6408 /* very important to increment global pointer at this time
6409 because initializers themselves can create new initializers */
6410 data_offset
+= size
;
6411 #ifdef CONFIG_TCC_BCHECK
6412 /* add padding if bound check */
6413 if (tcc_state
->do_bounds_check
)
6416 sec
->data_offset
= data_offset
;
6417 /* allocate section space to put the data */
6418 if (sec
->sh_type
!= SHT_NOBITS
&&
6419 data_offset
> sec
->data_allocated
)
6420 section_realloc(sec
, data_offset
);
6421 /* align section if needed */
6422 if (align
> sec
->sh_addralign
)
6423 sec
->sh_addralign
= align
;
6425 addr
= 0; /* avoid warning */
6429 if (scope
!= VT_CONST
|| !sym
) {
6430 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
6431 sym
->asm_label
= ad
->asm_label
;
6433 /* update symbol definition */
6435 put_extern_sym(sym
, sec
, addr
, size
);
6438 /* put a common area */
6439 put_extern_sym(sym
, NULL
, align
, size
);
6440 /* XXX: find a nicer way */
6441 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
6442 esym
->st_shndx
= SHN_COMMON
;
6445 /* push global reference */
6446 sym
= get_sym_ref(type
, sec
, addr
, size
);
6447 vpushsym(type
, sym
);
6449 /* patch symbol weakness */
6450 if (type
->t
& VT_WEAK
)
6452 apply_visibility(sym
, type
);
6453 #ifdef CONFIG_TCC_BCHECK
6454 /* handles bounds now because the symbol must be defined
6455 before for the relocation */
6456 if (tcc_state
->do_bounds_check
) {
6459 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
6460 /* then add global bound info */
6461 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(addr_t
));
6462 bounds_ptr
[0] = 0; /* relocated */
6463 bounds_ptr
[1] = size
;
6467 if (type
->t
& VT_VLA
) {
6470 /* save current stack pointer */
6471 if (vlas_in_scope
== 0) {
6472 if (vla_sp_root_loc
== -1)
6473 vla_sp_root_loc
= (loc
-= PTR_SIZE
);
6474 gen_vla_sp_save(vla_sp_root_loc
);
6477 vla_runtime_type_size(type
, &a
);
6478 gen_vla_alloc(type
, a
);
6479 gen_vla_sp_save(addr
);
6482 } else if (has_init
) {
6483 decl_initializer(type
, sec
, addr
, 1, 0);
6484 /* patch flexible array member size back to -1, */
6485 /* for possible subsequent similar declarations */
6487 flexible_array
->type
.ref
->c
= -1;
6490 /* restore parse state if needed */
6493 restore_parse_state(&saved_parse_state
);
6497 static void put_func_debug(Sym
*sym
)
6502 /* XXX: we put here a dummy type */
6503 snprintf(buf
, sizeof(buf
), "%s:%c1",
6504 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
6505 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
6506 cur_text_section
, sym
->c
);
6507 /* //gr gdb wants a line at the function */
6508 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
6513 /* parse an old style function declaration list */
6514 /* XXX: check multiple parameter */
6515 static void func_decl_list(Sym
*func_sym
)
6522 /* parse each declaration */
6523 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
&&
6524 tok
!= TOK_ASM1
&& tok
!= TOK_ASM2
&& tok
!= TOK_ASM3
) {
6525 if (!parse_btype(&btype
, &ad
))
6526 expect("declaration list");
6527 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
6528 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
6530 /* we accept no variable after */
6534 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
6535 /* find parameter in function parameter list */
6538 if ((s
->v
& ~SYM_FIELD
) == v
)
6542 tcc_error("declaration for parameter '%s' but no such parameter",
6543 get_tok_str(v
, NULL
));
6545 /* check that no storage specifier except 'register' was given */
6546 if (type
.t
& VT_STORAGE
)
6547 tcc_error("storage class specified for '%s'", get_tok_str(v
, NULL
));
6548 convert_parameter_type(&type
);
6549 /* we can add the type (NOTE: it could be local to the function) */
6551 /* accept other parameters */
6562 /* parse a function defined by symbol 'sym' and generate its code in
6563 'cur_text_section' */
6564 static void gen_function(Sym
*sym
)
6566 int saved_nocode_wanted
= nocode_wanted
;
6569 ind
= cur_text_section
->data_offset
;
6570 /* NOTE: we patch the symbol size later */
6571 put_extern_sym(sym
, cur_text_section
, ind
, 0);
6572 funcname
= get_tok_str(sym
->v
, NULL
);
6574 /* Initialize VLA state */
6576 vla_sp_root_loc
= -1;
6577 /* put debug symbol */
6578 if (tcc_state
->do_debug
)
6579 put_func_debug(sym
);
6581 /* push a dummy symbol to enable local sym storage */
6582 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
6583 local_scope
= 1; /* for function parameters */
6584 gfunc_prolog(&sym
->type
);
6588 block(NULL
, NULL
, 0);
6591 cur_text_section
->data_offset
= ind
;
6592 label_pop(&global_label_stack
, NULL
);
6593 /* reset local stack */
6595 sym_pop(&local_stack
, NULL
);
6596 /* end of function */
6597 /* patch symbol size */
6598 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
6600 /* patch symbol weakness (this definition overrules any prototype) */
6601 if (sym
->type
.t
& VT_WEAK
)
6603 apply_visibility(sym
, &sym
->type
);
6604 if (tcc_state
->do_debug
) {
6605 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
6607 /* It's better to crash than to generate wrong code */
6608 cur_text_section
= NULL
;
6609 funcname
= ""; /* for safety */
6610 func_vt
.t
= VT_VOID
; /* for safety */
6611 func_var
= 0; /* for safety */
6612 ind
= 0; /* for safety */
6613 nocode_wanted
= saved_nocode_wanted
;
6617 static void gen_inline_functions(TCCState
*s
)
6620 int inline_generated
, i
, ln
;
6621 struct InlineFunc
*fn
;
6623 ln
= file
->line_num
;
6624 /* iterate while inline function are referenced */
6626 inline_generated
= 0;
6627 for (i
= 0; i
< s
->nb_inline_fns
; ++i
) {
6628 fn
= s
->inline_fns
[i
];
6630 if (sym
&& sym
->c
) {
6631 /* the function was used: generate its code and
6632 convert it to a normal function */
6635 pstrcpy(file
->filename
, sizeof file
->filename
, fn
->filename
);
6636 sym
->r
= VT_SYM
| VT_CONST
;
6637 sym
->type
.t
&= ~VT_INLINE
;
6639 begin_macro(fn
->func_str
, 1);
6641 cur_text_section
= text_section
;
6645 inline_generated
= 1;
6648 if (!inline_generated
)
6651 file
->line_num
= ln
;
6654 ST_FUNC
void free_inline_functions(TCCState
*s
)
6657 /* free tokens of unused inline functions */
6658 for (i
= 0; i
< s
->nb_inline_fns
; ++i
) {
6659 struct InlineFunc
*fn
= s
->inline_fns
[i
];
6661 tok_str_free(fn
->func_str
);
6663 dynarray_reset(&s
->inline_fns
, &s
->nb_inline_fns
);
6666 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
6667 static int decl0(int l
, int is_for_loop_init
)
6675 if (!parse_btype(&btype
, &ad
)) {
6676 if (is_for_loop_init
)
6678 /* skip redundant ';' */
6679 /* XXX: find more elegant solution */
6684 if (l
== VT_CONST
&&
6685 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
6686 /* global asm block */
6690 /* special test for old K&R protos without explicit int
6691 type. Only accepted when defining global data */
6692 if (l
== VT_LOCAL
|| tok
< TOK_UIDENT
)
6696 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
6697 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
6699 if ((btype
.t
& VT_BTYPE
) == VT_STRUCT
) {
6700 int v
= btype
.ref
->v
;
6701 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) >= SYM_FIRST_ANOM
)
6702 tcc_warning("unnamed struct/union that defines no instances");
6707 while (1) { /* iterate thru each declaration */
6709 /* If the base type itself was an array type of unspecified
6710 size (like in 'typedef int arr[]; arr x = {1};') then
6711 we will overwrite the unknown size by the real one for
6712 this decl. We need to unshare the ref symbol holding
6714 if ((type
.t
& VT_ARRAY
) && type
.ref
->c
< 0) {
6715 type
.ref
= sym_push(SYM_FIELD
, &type
.ref
->type
, 0, type
.ref
->c
);
6717 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
6721 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
6722 printf("type = '%s'\n", buf
);
6725 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
6726 if ((type
.t
& VT_STATIC
) && (l
== VT_LOCAL
)) {
6727 tcc_error("function without file scope cannot be static");
6729 /* if old style function prototype, we accept a
6732 if (sym
->c
== FUNC_OLD
)
6733 func_decl_list(sym
);
6736 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
6737 ad
.asm_label
= asm_label_instr();
6738 /* parse one last attribute list, after asm label */
6739 parse_attribute(&ad
);
6746 #ifdef TCC_TARGET_PE
6747 if (ad
.a
.func_import
)
6748 type
.t
|= VT_IMPORT
;
6749 if (ad
.a
.func_export
)
6750 type
.t
|= VT_EXPORT
;
6752 type
.t
|= ad
.a
.visibility
<< VT_VIS_SHIFT
;
6756 tcc_error("cannot use local functions");
6757 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
6758 expect("function definition");
6760 /* reject abstract declarators in function definition */
6762 while ((sym
= sym
->next
) != NULL
)
6763 if (!(sym
->v
& ~SYM_FIELD
))
6764 expect("identifier");
6766 /* XXX: cannot do better now: convert extern line to static inline */
6767 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
6768 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
6773 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
6776 ref
= sym
->type
.ref
;
6777 if (0 == ref
->a
.func_proto
)
6778 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
6780 /* use func_call from prototype if not defined */
6781 if (ref
->a
.func_call
!= FUNC_CDECL
6782 && type
.ref
->a
.func_call
== FUNC_CDECL
)
6783 type
.ref
->a
.func_call
= ref
->a
.func_call
;
6785 /* use export from prototype */
6786 if (ref
->a
.func_export
)
6787 type
.ref
->a
.func_export
= 1;
6789 /* use static from prototype */
6790 if (sym
->type
.t
& VT_STATIC
)
6791 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
6793 /* If the definition has no visibility use the
6794 one from prototype. */
6795 if (! (type
.t
& VT_VIS_MASK
))
6796 type
.t
|= sym
->type
.t
& VT_VIS_MASK
;
6798 if (!is_compatible_types(&sym
->type
, &type
)) {
6800 tcc_error("incompatible types for redefinition of '%s'",
6801 get_tok_str(v
, NULL
));
6803 type
.ref
->a
.func_proto
= 0;
6804 /* if symbol is already defined, then put complete type */
6807 /* put function symbol */
6808 sym
= global_identifier_push(v
, type
.t
, 0);
6809 sym
->type
.ref
= type
.ref
;
6812 /* static inline functions are just recorded as a kind
6813 of macro. Their code will be emitted at the end of
6814 the compilation unit only if they are used */
6815 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
6816 (VT_INLINE
| VT_STATIC
)) {
6818 struct InlineFunc
*fn
;
6819 const char *filename
;
6821 filename
= file
? file
->filename
: "";
6822 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
6823 strcpy(fn
->filename
, filename
);
6825 fn
->func_str
= tok_str_alloc();
6831 tcc_error("unexpected end of file");
6832 tok_str_add_tok(fn
->func_str
);
6837 } else if (t
== '}') {
6839 if (block_level
== 0)
6843 tok_str_add(fn
->func_str
, -1);
6844 tok_str_add(fn
->func_str
, 0);
6845 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
6848 /* compute text section */
6849 cur_text_section
= ad
.section
;
6850 if (!cur_text_section
)
6851 cur_text_section
= text_section
;
6852 sym
->r
= VT_SYM
| VT_CONST
;
6857 if (btype
.t
& VT_TYPEDEF
) {
6858 /* save typedefed type */
6859 /* XXX: test storage specifiers ? */
6861 if (sym
&& sym
->scope
== local_scope
) {
6862 if (!is_compatible_types(&sym
->type
, &type
)
6863 || !(sym
->type
.t
& VT_TYPEDEF
))
6864 tcc_error("incompatible redefinition of '%s'",
6865 get_tok_str(v
, NULL
));
6868 sym
= sym_push(v
, &type
, 0, 0);
6871 sym
->type
.t
|= VT_TYPEDEF
;
6874 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
6875 /* external function definition */
6876 /* specific case for func_call attribute */
6877 ad
.a
.func_proto
= 1;
6879 } else if (!(type
.t
& VT_ARRAY
)) {
6880 /* not lvalue if array */
6881 r
|= lvalue_type(type
.t
);
6883 has_init
= (tok
== '=');
6884 if (has_init
&& (type
.t
& VT_VLA
))
6885 tcc_error("variable length array cannot be initialized");
6886 if ((btype
.t
& VT_EXTERN
) || ((type
.t
& VT_BTYPE
) == VT_FUNC
) ||
6887 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
6888 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
6889 /* external variable or function */
6890 /* NOTE: as GCC, uninitialized global static
6891 arrays of null size are considered as
6893 sym
= external_sym(v
, &type
, r
);
6894 sym
->asm_label
= ad
.asm_label
;
6896 if (ad
.alias_target
) {
6901 alias_target
= sym_find(ad
.alias_target
);
6902 if (!alias_target
|| !alias_target
->c
)
6903 tcc_error("unsupported forward __alias__ attribute");
6904 esym
= &((Elf32_Sym
*)symtab_section
->data
)[alias_target
->c
];
6905 tsec
.sh_num
= esym
->st_shndx
;
6906 put_extern_sym2(sym
, &tsec
, esym
->st_value
, esym
->st_size
, 0);
6909 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
6910 if (type
.t
& VT_STATIC
)
6916 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, l
);
6920 if (is_for_loop_init
)
6933 ST_FUNC
void decl(int l
)
6938 /* ------------------------------------------------------------------------- */