2 * TCC - Tiny C Compiler
4 * Copyright (c) 2001-2004 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 /********************************************************/
24 /* global variables */
26 /* loc : local variable index
27 ind : output code index
29 anon_sym: anonymous symbol index
31 ST_DATA
int rsym
, anon_sym
, ind
, loc
;
33 ST_DATA Sym
*sym_free_first
;
34 ST_DATA
void **sym_pools
;
35 ST_DATA
int nb_sym_pools
;
37 ST_DATA Sym
*global_stack
;
38 ST_DATA Sym
*local_stack
;
39 ST_DATA Sym
*define_stack
;
40 ST_DATA Sym
*global_label_stack
;
41 ST_DATA Sym
*local_label_stack
;
42 static int local_scope
;
44 static int section_sym
;
46 ST_DATA
int vlas_in_scope
; /* number of VLAs that are currently in scope */
47 ST_DATA
int vla_sp_root_loc
; /* vla_sp_loc for SP before any VLAs were pushed */
48 ST_DATA
int vla_sp_loc
; /* Pointer to variable holding location to store stack pointer on the stack when modifying stack pointer */
50 ST_DATA SValue __vstack
[1+VSTACK_SIZE
], *vtop
, *pvtop
;
52 ST_DATA
int const_wanted
; /* true if constant wanted */
53 ST_DATA
int nocode_wanted
; /* true if no code generation wanted for an expression */
54 ST_DATA
int global_expr
; /* true if compound literals must be allocated globally (used during initializers parsing */
55 ST_DATA CType func_vt
; /* current function return type (used by return instruction) */
56 ST_DATA
int func_var
; /* true if current function is variadic (used by return instruction) */
58 ST_DATA
int last_line_num
, last_ind
, func_ind
; /* debug last line number and pc */
59 ST_DATA
const char *funcname
;
61 ST_DATA CType char_pointer_type
, func_old_type
, int_type
, size_type
;
63 ST_DATA
struct switch_t
{
67 } **p
; int n
; /* list of case ranges */
68 int def_sym
; /* default symbol */
69 } *cur_switch
; /* current switch */
71 /* ------------------------------------------------------------------------- */
73 static void gen_cast(CType
*type
);
74 static inline CType
*pointed_type(CType
*type
);
75 static int is_compatible_types(CType
*type1
, CType
*type2
);
76 static int parse_btype(CType
*type
, AttributeDef
*ad
);
77 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
);
78 static void parse_expr_type(CType
*type
);
79 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
, int first
, int size_only
);
80 static void block(int *bsym
, int *csym
, int is_expr
);
81 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
, int has_init
, int v
, int scope
);
82 static int decl0(int l
, int is_for_loop_init
);
83 static void expr_eq(void);
84 static void expr_lor_const(void);
85 static void unary_type(CType
*type
);
86 static void vla_runtime_type_size(CType
*type
, int *a
);
87 static void vla_sp_restore(void);
88 static void vla_sp_restore_root(void);
89 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
);
90 static void expr_type(CType
*type
);
91 static inline int64_t expr_const64(void);
92 ST_FUNC
void vpush64(int ty
, unsigned long long v
);
93 ST_FUNC
void vpush(CType
*type
);
94 ST_FUNC
int gvtst(int inv
, int t
);
95 ST_FUNC
int is_btype_size(int bt
);
96 static void gen_inline_functions(TCCState
*s
);
98 ST_INLN
int is_float(int t
)
102 return bt
== VT_LDOUBLE
|| bt
== VT_DOUBLE
|| bt
== VT_FLOAT
|| bt
== VT_QFLOAT
;
105 /* we use our own 'finite' function to avoid potential problems with
106 non standard math libs */
107 /* XXX: endianness dependent */
108 ST_FUNC
int ieee_finite(double d
)
111 memcpy(p
, &d
, sizeof(double));
112 return ((unsigned)((p
[1] | 0x800fffff) + 1)) >> 31;
115 ST_FUNC
void test_lvalue(void)
117 if (!(vtop
->r
& VT_LVAL
))
121 ST_FUNC
void check_vstack(void)
124 tcc_error("internal compiler error: vstack leak (%d)", vtop
- pvtop
);
127 /* ------------------------------------------------------------------------- */
128 /* vstack debugging aid */
131 void pv (const char *lbl
, int a
, int b
)
134 for (i
= a
; i
< a
+ b
; ++i
) {
135 SValue
*p
= &vtop
[-i
];
136 printf("%s vtop[-%d] : type.t:%04x r:%04x r2:%04x c.i:%d\n",
137 lbl
, i
, p
->type
.t
, p
->r
, p
->r2
, (int)p
->c
.i
);
142 /* ------------------------------------------------------------------------- */
143 /* start of translation unit info */
144 ST_FUNC
void tcc_debug_start(TCCState
*s1
)
149 /* file info: full path + filename */
150 section_sym
= put_elf_sym(symtab_section
, 0, 0,
151 ELFW(ST_INFO
)(STB_LOCAL
, STT_SECTION
), 0,
152 text_section
->sh_num
, NULL
);
153 getcwd(buf
, sizeof(buf
));
155 normalize_slashes(buf
);
157 pstrcat(buf
, sizeof(buf
), "/");
158 put_stabs_r(buf
, N_SO
, 0, 0,
159 text_section
->data_offset
, text_section
, section_sym
);
160 put_stabs_r(file
->filename
, N_SO
, 0, 0,
161 text_section
->data_offset
, text_section
, section_sym
);
166 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
167 symbols can be safely used */
168 put_elf_sym(symtab_section
, 0, 0,
169 ELFW(ST_INFO
)(STB_LOCAL
, STT_FILE
), 0,
170 SHN_ABS
, file
->filename
);
173 /* put end of translation unit info */
174 ST_FUNC
void tcc_debug_end(TCCState
*s1
)
178 put_stabs_r(NULL
, N_SO
, 0, 0,
179 text_section
->data_offset
, text_section
, section_sym
);
183 /* generate line number info */
184 ST_FUNC
void tcc_debug_line(TCCState
*s1
)
188 if ((last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
189 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
191 last_line_num
= file
->line_num
;
195 /* put function symbol */
196 ST_FUNC
void tcc_debug_funcstart(TCCState
*s1
, Sym
*sym
)
204 /* XXX: we put here a dummy type */
205 snprintf(buf
, sizeof(buf
), "%s:%c1",
206 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
207 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
208 cur_text_section
, sym
->c
);
209 /* //gr gdb wants a line at the function */
210 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
216 /* put function size */
217 ST_FUNC
void tcc_debug_funcend(TCCState
*s1
, int size
)
221 put_stabn(N_FUN
, 0, 0, size
);
224 /* ------------------------------------------------------------------------- */
225 ST_FUNC
void tccgen_start(TCCState
*s1
)
227 cur_text_section
= NULL
;
229 anon_sym
= SYM_FIRST_ANOM
;
234 /* define some often used types */
236 char_pointer_type
.t
= VT_BYTE
;
237 mk_pointer(&char_pointer_type
);
239 size_type
.t
= VT_INT
;
241 size_type
.t
= VT_LLONG
;
243 func_old_type
.t
= VT_FUNC
;
244 func_old_type
.ref
= sym_push(SYM_FIELD
, &int_type
, FUNC_CDECL
, FUNC_OLD
);
248 #ifdef TCC_TARGET_ARM
253 ST_FUNC
void tccgen_end(TCCState
*s1
)
255 gen_inline_functions(s1
);
257 /* end of translation unit info */
261 /* ------------------------------------------------------------------------- */
262 /* update sym->c so that it points to an external symbol in section
263 'section' with value 'value' */
265 ST_FUNC
void put_extern_sym2(Sym
*sym
, Section
*section
,
266 addr_t value
, unsigned long size
,
267 int can_add_underscore
)
269 int sym_type
, sym_bind
, sh_num
, info
, other
;
274 #ifdef CONFIG_TCC_BCHECK
280 else if (section
== SECTION_ABS
)
283 sh_num
= section
->sh_num
;
285 if ((sym
->type
.t
& VT_BTYPE
) == VT_FUNC
) {
287 } else if ((sym
->type
.t
& VT_BTYPE
) == VT_VOID
) {
288 sym_type
= STT_NOTYPE
;
290 sym_type
= STT_OBJECT
;
293 if (sym
->type
.t
& VT_STATIC
)
294 sym_bind
= STB_LOCAL
;
296 if (sym
->type
.t
& VT_WEAK
)
299 sym_bind
= STB_GLOBAL
;
303 name
= get_tok_str(sym
->v
, NULL
);
304 #ifdef CONFIG_TCC_BCHECK
305 if (tcc_state
->do_bounds_check
) {
306 /* XXX: avoid doing that for statics ? */
307 /* if bound checking is activated, we change some function
308 names by adding the "__bound" prefix */
311 /* XXX: we rely only on malloc hooks */
324 strcpy(buf
, "__bound_");
334 if (sym
->type
.t
& VT_EXPORT
)
335 other
|= ST_PE_EXPORT
;
336 if (sym_type
== STT_FUNC
&& sym
->type
.ref
) {
337 Sym
*ref
= sym
->type
.ref
;
338 if (ref
->a
.func_export
)
339 other
|= ST_PE_EXPORT
;
340 if (ref
->a
.func_call
== FUNC_STDCALL
&& can_add_underscore
) {
341 sprintf(buf1
, "_%s@%d", name
, ref
->a
.func_args
* PTR_SIZE
);
343 other
|= ST_PE_STDCALL
;
344 can_add_underscore
= 0;
347 if (find_elf_sym(tcc_state
->dynsymtab_section
, name
))
348 other
|= ST_PE_IMPORT
;
349 if (sym
->type
.t
& VT_IMPORT
)
350 other
|= ST_PE_IMPORT
;
353 if (! (sym
->type
.t
& VT_STATIC
))
354 other
= (sym
->type
.t
& VT_VIS_MASK
) >> VT_VIS_SHIFT
;
356 if (tcc_state
->leading_underscore
&& can_add_underscore
) {
358 pstrcpy(buf1
+ 1, sizeof(buf1
) - 1, name
);
361 if (sym
->asm_label
) {
362 name
= get_tok_str(sym
->asm_label
, NULL
);
364 info
= ELFW(ST_INFO
)(sym_bind
, sym_type
);
365 sym
->c
= set_elf_sym(symtab_section
, value
, size
, info
, other
, sh_num
, name
);
367 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
368 esym
->st_value
= value
;
369 esym
->st_size
= size
;
370 esym
->st_shndx
= sh_num
;
374 ST_FUNC
void put_extern_sym(Sym
*sym
, Section
*section
,
375 addr_t value
, unsigned long size
)
377 put_extern_sym2(sym
, section
, value
, size
, 1);
380 /* add a new relocation entry to symbol 'sym' in section 's' */
381 ST_FUNC
void greloca(Section
*s
, Sym
*sym
, unsigned long offset
, int type
,
386 if (nocode_wanted
&& s
== cur_text_section
)
391 put_extern_sym(sym
, NULL
, 0, 0);
395 /* now we can add ELF relocation info */
396 put_elf_reloca(symtab_section
, s
, offset
, type
, c
, addend
);
399 ST_FUNC
void greloc(Section
*s
, Sym
*sym
, unsigned long offset
, int type
)
401 greloca(s
, sym
, offset
, type
, 0);
404 /* ------------------------------------------------------------------------- */
405 /* symbol allocator */
406 static Sym
*__sym_malloc(void)
408 Sym
*sym_pool
, *sym
, *last_sym
;
411 sym_pool
= tcc_malloc(SYM_POOL_NB
* sizeof(Sym
));
412 dynarray_add(&sym_pools
, &nb_sym_pools
, sym_pool
);
414 last_sym
= sym_free_first
;
416 for(i
= 0; i
< SYM_POOL_NB
; i
++) {
417 sym
->next
= last_sym
;
421 sym_free_first
= last_sym
;
425 static inline Sym
*sym_malloc(void)
429 sym
= sym_free_first
;
431 sym
= __sym_malloc();
432 sym_free_first
= sym
->next
;
435 sym
= tcc_malloc(sizeof(Sym
));
440 ST_INLN
void sym_free(Sym
*sym
)
443 sym
->next
= sym_free_first
;
444 sym_free_first
= sym
;
450 /* push, without hashing */
451 ST_FUNC Sym
*sym_push2(Sym
**ps
, int v
, int t
, long c
)
471 /* find a symbol and return its associated structure. 's' is the top
472 of the symbol stack */
473 ST_FUNC Sym
*sym_find2(Sym
*s
, int v
)
485 /* structure lookup */
486 ST_INLN Sym
*struct_find(int v
)
489 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
491 return table_ident
[v
]->sym_struct
;
494 /* find an identifier */
495 ST_INLN Sym
*sym_find(int v
)
498 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
500 return table_ident
[v
]->sym_identifier
;
503 /* push a given symbol on the symbol stack */
504 ST_FUNC Sym
*sym_push(int v
, CType
*type
, int r
, long c
)
513 s
= sym_push2(ps
, v
, type
->t
, c
);
514 s
->type
.ref
= type
->ref
;
516 /* don't record fields or anonymous symbols */
518 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
519 /* record symbol in token array */
520 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
522 ps
= &ts
->sym_struct
;
524 ps
= &ts
->sym_identifier
;
527 s
->scope
= local_scope
;
528 if (s
->prev_tok
&& s
->prev_tok
->scope
== s
->scope
)
529 tcc_error("redeclaration of '%s'",
530 get_tok_str(v
& ~SYM_STRUCT
, NULL
));
535 /* push a global identifier */
536 ST_FUNC Sym
*global_identifier_push(int v
, int t
, long c
)
539 s
= sym_push2(&global_stack
, v
, t
, c
);
540 /* don't record anonymous symbol */
541 if (v
< SYM_FIRST_ANOM
) {
542 ps
= &table_ident
[v
- TOK_IDENT
]->sym_identifier
;
543 /* modify the top most local identifier, so that
544 sym_identifier will point to 's' when popped */
546 ps
= &(*ps
)->prev_tok
;
553 /* pop symbols until top reaches 'b'. If KEEP is non-zero don't really
554 pop them yet from the list, but do remove them from the token array. */
555 ST_FUNC
void sym_pop(Sym
**ptop
, Sym
*b
, int keep
)
565 /* remove symbol in token array */
567 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
568 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
570 ps
= &ts
->sym_struct
;
572 ps
= &ts
->sym_identifier
;
583 static void weaken_symbol(Sym
*sym
)
585 sym
->type
.t
|= VT_WEAK
;
590 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
591 esym_type
= ELFW(ST_TYPE
)(esym
->st_info
);
592 esym
->st_info
= ELFW(ST_INFO
)(STB_WEAK
, esym_type
);
596 static void apply_visibility(Sym
*sym
, CType
*type
)
598 int vis
= sym
->type
.t
& VT_VIS_MASK
;
599 int vis2
= type
->t
& VT_VIS_MASK
;
600 if (vis
== (STV_DEFAULT
<< VT_VIS_SHIFT
))
602 else if (vis2
== (STV_DEFAULT
<< VT_VIS_SHIFT
))
605 vis
= (vis
< vis2
) ? vis
: vis2
;
606 sym
->type
.t
&= ~VT_VIS_MASK
;
612 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
613 vis
>>= VT_VIS_SHIFT
;
614 esym
->st_other
= (esym
->st_other
& ~ELFW(ST_VISIBILITY
)(-1)) | vis
;
618 /* ------------------------------------------------------------------------- */
620 static void vsetc(CType
*type
, int r
, CValue
*vc
)
624 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
625 tcc_error("memory full (vstack)");
626 /* cannot let cpu flags if other instruction are generated. Also
627 avoid leaving VT_JMP anywhere except on the top of the stack
628 because it would complicate the code generator.
630 Don't do this when nocode_wanted. vtop might come from
631 !nocode_wanted regions (see 88_codeopt.c) and transforming
632 it to a register without actually generating code is wrong
633 as their value might still be used for real. All values
634 we push under nocode_wanted will eventually be popped
635 again, so that the VT_CMP/VT_JMP value will be in vtop
636 when code is unsuppressed again.
638 Same logic below in vswap(); */
639 if (vtop
>= vstack
&& !nocode_wanted
) {
640 v
= vtop
->r
& VT_VALMASK
;
641 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
653 ST_FUNC
void vswap(void)
656 /* cannot vswap cpu flags. See comment at vsetc() above */
657 if (vtop
>= vstack
&& !nocode_wanted
) {
658 int v
= vtop
->r
& VT_VALMASK
;
659 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
667 /* pop stack value */
668 ST_FUNC
void vpop(void)
671 v
= vtop
->r
& VT_VALMASK
;
672 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
673 /* for x86, we need to pop the FP stack */
675 o(0xd8dd); /* fstp %st(0) */
678 if (v
== VT_JMP
|| v
== VT_JMPI
) {
679 /* need to put correct jump if && or || without test */
685 /* push constant of type "type" with useless value */
686 ST_FUNC
void vpush(CType
*type
)
689 vsetc(type
, VT_CONST
, &cval
);
692 /* push integer constant */
693 ST_FUNC
void vpushi(int v
)
697 vsetc(&int_type
, VT_CONST
, &cval
);
700 /* push a pointer sized constant */
701 static void vpushs(addr_t v
)
705 vsetc(&size_type
, VT_CONST
, &cval
);
708 /* push arbitrary 64bit constant */
709 ST_FUNC
void vpush64(int ty
, unsigned long long v
)
716 vsetc(&ctype
, VT_CONST
, &cval
);
719 /* push long long constant */
720 static inline void vpushll(long long v
)
722 vpush64(VT_LLONG
, v
);
725 ST_FUNC
void vset(CType
*type
, int r
, long v
)
730 vsetc(type
, r
, &cval
);
733 static void vseti(int r
, int v
)
741 ST_FUNC
void vpushv(SValue
*v
)
743 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
744 tcc_error("memory full (vstack)");
749 static void vdup(void)
754 /* rotate n first stack elements to the bottom
755 I1 ... In -> I2 ... In I1 [top is right]
757 ST_FUNC
void vrotb(int n
)
768 /* rotate the n elements before entry e towards the top
769 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
771 ST_FUNC
void vrote(SValue
*e
, int n
)
777 for(i
= 0;i
< n
- 1; i
++)
782 /* rotate n first stack elements to the top
783 I1 ... In -> In I1 ... I(n-1) [top is right]
785 ST_FUNC
void vrott(int n
)
790 /* push a symbol value of TYPE */
791 static inline void vpushsym(CType
*type
, Sym
*sym
)
795 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
799 /* Return a static symbol pointing to a section */
800 ST_FUNC Sym
*get_sym_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
806 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
807 sym
->type
.ref
= type
->ref
;
808 sym
->r
= VT_CONST
| VT_SYM
;
809 put_extern_sym(sym
, sec
, offset
, size
);
813 /* push a reference to a section offset by adding a dummy symbol */
814 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
816 vpushsym(type
, get_sym_ref(type
, sec
, offset
, size
));
819 /* define a new external reference to a symbol 'v' of type 'u' */
820 ST_FUNC Sym
*external_global_sym(int v
, CType
*type
, int r
)
826 /* push forward reference */
827 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
828 s
->type
.ref
= type
->ref
;
829 s
->r
= r
| VT_CONST
| VT_SYM
;
834 /* define a new external reference to a symbol 'v' */
835 static Sym
*external_sym(int v
, CType
*type
, int r
)
841 /* push forward reference */
842 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
843 s
->type
.t
|= VT_EXTERN
;
844 } else if (s
->type
.ref
== func_old_type
.ref
) {
845 s
->type
.ref
= type
->ref
;
846 s
->r
= r
| VT_CONST
| VT_SYM
;
847 s
->type
.t
|= VT_EXTERN
;
848 } else if (!is_compatible_types(&s
->type
, type
)) {
849 tcc_error("incompatible types for redefinition of '%s'",
850 get_tok_str(v
, NULL
));
852 /* Merge some storage attributes. */
853 if (type
->t
& VT_WEAK
)
856 if (type
->t
& VT_VIS_MASK
)
857 apply_visibility(s
, type
);
862 /* push a reference to global symbol v */
863 ST_FUNC
void vpush_global_sym(CType
*type
, int v
)
865 vpushsym(type
, external_global_sym(v
, type
, 0));
868 /* save registers up to (vtop - n) stack entry */
869 ST_FUNC
void save_regs(int n
)
872 for(p
= vstack
, p1
= vtop
- n
; p
<= p1
; p
++)
876 /* save r to the memory stack, and mark it as being free */
877 ST_FUNC
void save_reg(int r
)
879 save_reg_upstack(r
, 0);
882 /* save r to the memory stack, and mark it as being free,
883 if seen up to (vtop - n) stack entry */
884 ST_FUNC
void save_reg_upstack(int r
, int n
)
886 int l
, saved
, size
, align
;
890 if ((r
&= VT_VALMASK
) >= VT_CONST
)
895 /* modify all stack values */
898 for(p
= vstack
, p1
= vtop
- n
; p
<= p1
; p
++) {
899 if ((p
->r
& VT_VALMASK
) == r
||
900 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
901 /* must save value on stack if not already done */
903 /* NOTE: must reload 'r' because r might be equal to r2 */
904 r
= p
->r
& VT_VALMASK
;
905 /* store register in the stack */
907 if ((p
->r
& VT_LVAL
) ||
908 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
909 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
910 type
= &char_pointer_type
;
914 if ((type
->t
& VT_BTYPE
) == VT_FLOAT
) {
915 /* cast to DOUBLE to avoid precision loss */
916 type
->t
= (type
->t
& ~VT_BTYPE
) | VT_DOUBLE
;
918 size
= type_size(type
, &align
);
919 loc
= (loc
- size
) & -align
;
921 sv
.r
= VT_LOCAL
| VT_LVAL
;
924 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
925 /* x86 specific: need to pop fp register ST0 if saved */
927 o(0xd8dd); /* fstp %st(0) */
930 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
931 /* special long long case */
932 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
940 /* mark that stack entry as being saved on the stack */
941 if (p
->r
& VT_LVAL
) {
942 /* also clear the bounded flag because the
943 relocation address of the function was stored in
945 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
947 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
955 #ifdef TCC_TARGET_ARM
956 /* find a register of class 'rc2' with at most one reference on stack.
957 * If none, call get_reg(rc) */
958 ST_FUNC
int get_reg_ex(int rc
, int rc2
)
963 for(r
=0;r
<NB_REGS
;r
++) {
964 if (reg_classes
[r
] & rc2
) {
967 for(p
= vstack
; p
<= vtop
; p
++) {
968 if ((p
->r
& VT_VALMASK
) == r
||
969 (p
->r2
& VT_VALMASK
) == r
)
980 /* find a free register of class 'rc'. If none, save one register */
981 ST_FUNC
int get_reg(int rc
)
986 /* find a free register */
987 for(r
=0;r
<NB_REGS
;r
++) {
988 if (reg_classes
[r
] & rc
) {
991 for(p
=vstack
;p
<=vtop
;p
++) {
992 if ((p
->r
& VT_VALMASK
) == r
||
993 (p
->r2
& VT_VALMASK
) == r
)
1001 /* no register left : free the first one on the stack (VERY
1002 IMPORTANT to start from the bottom to ensure that we don't
1003 spill registers used in gen_opi()) */
1004 for(p
=vstack
;p
<=vtop
;p
++) {
1005 /* look at second register (if long long) */
1006 r
= p
->r2
& VT_VALMASK
;
1007 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
1009 r
= p
->r
& VT_VALMASK
;
1010 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
1016 /* Should never comes here */
1020 /* move register 's' (of type 't') to 'r', and flush previous value of r to memory
1022 static void move_reg(int r
, int s
, int t
)
1036 /* get address of vtop (vtop MUST BE an lvalue) */
1037 ST_FUNC
void gaddrof(void)
1039 if (vtop
->r
& VT_REF
)
1041 vtop
->r
&= ~VT_LVAL
;
1042 /* tricky: if saved lvalue, then we can go back to lvalue */
1043 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
1044 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
1049 #ifdef CONFIG_TCC_BCHECK
1050 /* generate lvalue bound code */
1051 static void gbound(void)
1056 vtop
->r
&= ~VT_MUSTBOUND
;
1057 /* if lvalue, then use checking code before dereferencing */
1058 if (vtop
->r
& VT_LVAL
) {
1059 /* if not VT_BOUNDED value, then make one */
1060 if (!(vtop
->r
& VT_BOUNDED
)) {
1061 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
1062 /* must save type because we must set it to int to get pointer */
1064 vtop
->type
.t
= VT_PTR
;
1067 gen_bounded_ptr_add();
1068 vtop
->r
|= lval_type
;
1071 /* then check for dereferencing */
1072 gen_bounded_ptr_deref();
1077 /* store vtop a register belonging to class 'rc'. lvalues are
1078 converted to values. Cannot be used if cannot be converted to
1079 register value (such as structures). */
1080 ST_FUNC
int gv(int rc
)
1082 int r
, bit_pos
, bit_size
, size
, align
, i
;
1085 /* NOTE: get_reg can modify vstack[] */
1086 if (vtop
->type
.t
& VT_BITFIELD
) {
1089 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
1090 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
1091 /* remove bit field info to avoid loops */
1092 vtop
->type
.t
&= ~VT_BITFIELD
& ((1 << VT_STRUCT_SHIFT
) - 1);
1093 /* cast to int to propagate signedness in following ops */
1094 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
1099 if((vtop
->type
.t
& VT_UNSIGNED
) ||
1100 (vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
1101 type
.t
|= VT_UNSIGNED
;
1103 /* generate shifts */
1104 vpushi(bits
- (bit_pos
+ bit_size
));
1106 vpushi(bits
- bit_size
);
1107 /* NOTE: transformed to SHR if unsigned */
1111 if (is_float(vtop
->type
.t
) &&
1112 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
1115 unsigned long offset
;
1116 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
1120 /* XXX: unify with initializers handling ? */
1121 /* CPUs usually cannot use float constants, so we store them
1122 generically in data segment */
1123 size
= type_size(&vtop
->type
, &align
);
1124 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
1125 data_section
->data_offset
= offset
;
1126 /* XXX: not portable yet */
1127 #if defined(__i386__) || defined(__x86_64__)
1128 /* Zero pad x87 tenbyte long doubles */
1129 if (size
== LDOUBLE_SIZE
) {
1130 vtop
->c
.tab
[2] &= 0xffff;
1131 #if LDOUBLE_SIZE == 16
1136 ptr
= section_ptr_add(data_section
, size
);
1138 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
1142 ptr
[i
] = vtop
->c
.tab
[size
-1-i
];
1146 ptr
[i
] = vtop
->c
.tab
[i
];
1147 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
1148 vtop
->r
|= VT_LVAL
| VT_SYM
;
1152 #ifdef CONFIG_TCC_BCHECK
1153 if (vtop
->r
& VT_MUSTBOUND
)
1157 r
= vtop
->r
& VT_VALMASK
;
1158 rc2
= (rc
& RC_FLOAT
) ? RC_FLOAT
: RC_INT
;
1159 #ifndef TCC_TARGET_ARM64
1162 #ifdef TCC_TARGET_X86_64
1163 else if (rc
== RC_FRET
)
1168 /* need to reload if:
1170 - lvalue (need to dereference pointer)
1171 - already a register, but not in the right class */
1173 || (vtop
->r
& VT_LVAL
)
1174 || !(reg_classes
[r
] & rc
)
1175 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1176 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
1177 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
&& !(reg_classes
[vtop
->r2
] & rc2
))
1179 || ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
1184 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1185 if (((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
)) {
1186 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
1188 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
1189 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
1190 unsigned long long ll
;
1192 int r2
, original_type
;
1193 original_type
= vtop
->type
.t
;
1194 /* two register type load : expand to two words
1196 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
1197 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
1200 vtop
->c
.i
= ll
; /* first word */
1202 vtop
->r
= r
; /* save register value */
1203 vpushi(ll
>> 32); /* second word */
1206 if (vtop
->r
& VT_LVAL
) {
1207 /* We do not want to modifier the long long
1208 pointer here, so the safest (and less
1209 efficient) is to save all the other registers
1210 in the stack. XXX: totally inefficient. */
1214 /* lvalue_save: save only if used further down the stack */
1215 save_reg_upstack(vtop
->r
, 1);
1217 /* load from memory */
1218 vtop
->type
.t
= load_type
;
1221 vtop
[-1].r
= r
; /* save register value */
1222 /* increment pointer to get second word */
1223 vtop
->type
.t
= addr_type
;
1228 vtop
->type
.t
= load_type
;
1230 /* move registers */
1233 vtop
[-1].r
= r
; /* save register value */
1234 vtop
->r
= vtop
[-1].r2
;
1236 /* Allocate second register. Here we rely on the fact that
1237 get_reg() tries first to free r2 of an SValue. */
1241 /* write second register */
1243 vtop
->type
.t
= original_type
;
1244 } else if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
1246 /* lvalue of scalar type : need to use lvalue type
1247 because of possible cast */
1250 /* compute memory access type */
1251 if (vtop
->r
& VT_REF
)
1252 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1257 else if (vtop
->r
& VT_LVAL_BYTE
)
1259 else if (vtop
->r
& VT_LVAL_SHORT
)
1261 if (vtop
->r
& VT_LVAL_UNSIGNED
)
1265 /* restore wanted type */
1268 /* one register type load */
1273 #ifdef TCC_TARGET_C67
1274 /* uses register pairs for doubles */
1275 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
1282 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
1283 ST_FUNC
void gv2(int rc1
, int rc2
)
1287 /* generate more generic register first. But VT_JMP or VT_CMP
1288 values must be generated first in all cases to avoid possible
1290 v
= vtop
[0].r
& VT_VALMASK
;
1291 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
1296 /* test if reload is needed for first register */
1297 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
1307 /* test if reload is needed for first register */
1308 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
1314 #ifndef TCC_TARGET_ARM64
1315 /* wrapper around RC_FRET to return a register by type */
1316 static int rc_fret(int t
)
1318 #ifdef TCC_TARGET_X86_64
1319 if (t
== VT_LDOUBLE
) {
1327 /* wrapper around REG_FRET to return a register by type */
1328 static int reg_fret(int t
)
1330 #ifdef TCC_TARGET_X86_64
1331 if (t
== VT_LDOUBLE
) {
1338 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
1339 /* expand 64bit on stack in two ints */
1340 static void lexpand(void)
1343 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
1344 v
= vtop
->r
& (VT_VALMASK
| VT_LVAL
);
1345 if (v
== VT_CONST
) {
1348 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
1354 vtop
[0].r
= vtop
[-1].r2
;
1355 vtop
[0].r2
= vtop
[-1].r2
= VT_CONST
;
1357 vtop
[0].type
.t
= vtop
[-1].type
.t
= VT_INT
| u
;
1361 #ifdef TCC_TARGET_ARM
1362 /* expand long long on stack */
1363 ST_FUNC
void lexpand_nr(void)
1367 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
1369 vtop
->r2
= VT_CONST
;
1370 vtop
->type
.t
= VT_INT
| u
;
1371 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
1372 if (v
== VT_CONST
) {
1373 vtop
[-1].c
.i
= vtop
->c
.i
;
1374 vtop
->c
.i
= vtop
->c
.i
>> 32;
1376 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
1378 vtop
->r
= vtop
[-1].r
;
1379 } else if (v
> VT_CONST
) {
1383 vtop
->r
= vtop
[-1].r2
;
1384 vtop
[-1].r2
= VT_CONST
;
1385 vtop
[-1].type
.t
= VT_INT
| u
;
1389 #if !defined(TCC_TARGET_X86_64) && !defined(TCC_TARGET_ARM64)
1390 /* build a long long from two ints */
1391 static void lbuild(int t
)
1393 gv2(RC_INT
, RC_INT
);
1394 vtop
[-1].r2
= vtop
[0].r
;
1395 vtop
[-1].type
.t
= t
;
1400 /* convert stack entry to register and duplicate its value in another
1402 static void gv_dup(void)
1408 #if !defined(TCC_TARGET_X86_64) && !defined(TCC_TARGET_ARM64)
1409 if ((t
& VT_BTYPE
) == VT_LLONG
) {
1416 /* stack: H L L1 H1 */
1426 /* duplicate value */
1431 #ifdef TCC_TARGET_X86_64
1432 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1442 load(r1
, &sv
); /* move r to r1 */
1444 /* duplicates value */
1450 /* Generate value test
1452 * Generate a test for any value (jump, comparison and integers) */
1453 ST_FUNC
int gvtst(int inv
, int t
)
1455 int v
= vtop
->r
& VT_VALMASK
;
1456 if (v
!= VT_CMP
&& v
!= VT_JMP
&& v
!= VT_JMPI
) {
1460 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1461 /* constant jmp optimization */
1462 if ((vtop
->c
.i
!= 0) != inv
)
1467 return gtst(inv
, t
);
1470 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
1471 /* generate CPU independent (unsigned) long long operations */
1472 static void gen_opl(int op
)
1474 int t
, a
, b
, op1
, c
, i
;
1476 unsigned short reg_iret
= REG_IRET
;
1477 unsigned short reg_lret
= REG_LRET
;
1483 func
= TOK___divdi3
;
1486 func
= TOK___udivdi3
;
1489 func
= TOK___moddi3
;
1492 func
= TOK___umoddi3
;
1499 /* call generic long long function */
1500 vpush_global_sym(&func_old_type
, func
);
1505 vtop
->r2
= reg_lret
;
1513 //pv("gen_opl A",0,2);
1519 /* stack: L1 H1 L2 H2 */
1524 vtop
[-2] = vtop
[-3];
1527 /* stack: H1 H2 L1 L2 */
1528 //pv("gen_opl B",0,4);
1534 /* stack: H1 H2 L1 L2 ML MH */
1537 /* stack: ML MH H1 H2 L1 L2 */
1541 /* stack: ML MH H1 L2 H2 L1 */
1546 /* stack: ML MH M1 M2 */
1549 } else if (op
== '+' || op
== '-') {
1550 /* XXX: add non carry method too (for MIPS or alpha) */
1556 /* stack: H1 H2 (L1 op L2) */
1559 gen_op(op1
+ 1); /* TOK_xxxC2 */
1562 /* stack: H1 H2 (L1 op L2) */
1565 /* stack: (L1 op L2) H1 H2 */
1567 /* stack: (L1 op L2) (H1 op H2) */
1575 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1576 t
= vtop
[-1].type
.t
;
1580 /* stack: L H shift */
1582 /* constant: simpler */
1583 /* NOTE: all comments are for SHL. the other cases are
1584 done by swaping words */
1595 if (op
!= TOK_SAR
) {
1628 /* XXX: should provide a faster fallback on x86 ? */
1631 func
= TOK___ashrdi3
;
1634 func
= TOK___lshrdi3
;
1637 func
= TOK___ashldi3
;
1643 /* compare operations */
1649 /* stack: L1 H1 L2 H2 */
1651 vtop
[-1] = vtop
[-2];
1653 /* stack: L1 L2 H1 H2 */
1656 /* when values are equal, we need to compare low words. since
1657 the jump is inverted, we invert the test too. */
1660 else if (op1
== TOK_GT
)
1662 else if (op1
== TOK_ULT
)
1664 else if (op1
== TOK_UGT
)
1674 /* generate non equal test */
1680 /* compare low. Always unsigned */
1684 else if (op1
== TOK_LE
)
1686 else if (op1
== TOK_GT
)
1688 else if (op1
== TOK_GE
)
1699 static uint64_t gen_opic_sdiv(uint64_t a
, uint64_t b
)
1701 uint64_t x
= (a
>> 63 ? -a
: a
) / (b
>> 63 ? -b
: b
);
1702 return (a
^ b
) >> 63 ? -x
: x
;
1705 static int gen_opic_lt(uint64_t a
, uint64_t b
)
1707 return (a
^ (uint64_t)1 << 63) < (b
^ (uint64_t)1 << 63);
1710 /* handle integer constant optimizations and various machine
1712 static void gen_opic(int op
)
1714 SValue
*v1
= vtop
- 1;
1716 int t1
= v1
->type
.t
& VT_BTYPE
;
1717 int t2
= v2
->type
.t
& VT_BTYPE
;
1718 int c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1719 int c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1720 uint64_t l1
= c1
? v1
->c
.i
: 0;
1721 uint64_t l2
= c2
? v2
->c
.i
: 0;
1722 int shm
= (t1
== VT_LLONG
) ? 63 : 31;
1724 if (t1
!= VT_LLONG
&& (PTR_SIZE
!= 8 || t1
!= VT_PTR
))
1725 l1
= ((uint32_t)l1
|
1726 (v1
->type
.t
& VT_UNSIGNED
? 0 : -(l1
& 0x80000000)));
1727 if (t2
!= VT_LLONG
&& (PTR_SIZE
!= 8 || t2
!= VT_PTR
))
1728 l2
= ((uint32_t)l2
|
1729 (v2
->type
.t
& VT_UNSIGNED
? 0 : -(l2
& 0x80000000)));
1733 case '+': l1
+= l2
; break;
1734 case '-': l1
-= l2
; break;
1735 case '&': l1
&= l2
; break;
1736 case '^': l1
^= l2
; break;
1737 case '|': l1
|= l2
; break;
1738 case '*': l1
*= l2
; break;
1745 /* if division by zero, generate explicit division */
1748 tcc_error("division by zero in constant");
1752 default: l1
= gen_opic_sdiv(l1
, l2
); break;
1753 case '%': l1
= l1
- l2
* gen_opic_sdiv(l1
, l2
); break;
1754 case TOK_UDIV
: l1
= l1
/ l2
; break;
1755 case TOK_UMOD
: l1
= l1
% l2
; break;
1758 case TOK_SHL
: l1
<<= (l2
& shm
); break;
1759 case TOK_SHR
: l1
>>= (l2
& shm
); break;
1761 l1
= (l1
>> 63) ? ~(~l1
>> (l2
& shm
)) : l1
>> (l2
& shm
);
1764 case TOK_ULT
: l1
= l1
< l2
; break;
1765 case TOK_UGE
: l1
= l1
>= l2
; break;
1766 case TOK_EQ
: l1
= l1
== l2
; break;
1767 case TOK_NE
: l1
= l1
!= l2
; break;
1768 case TOK_ULE
: l1
= l1
<= l2
; break;
1769 case TOK_UGT
: l1
= l1
> l2
; break;
1770 case TOK_LT
: l1
= gen_opic_lt(l1
, l2
); break;
1771 case TOK_GE
: l1
= !gen_opic_lt(l1
, l2
); break;
1772 case TOK_LE
: l1
= !gen_opic_lt(l2
, l1
); break;
1773 case TOK_GT
: l1
= gen_opic_lt(l2
, l1
); break;
1775 case TOK_LAND
: l1
= l1
&& l2
; break;
1776 case TOK_LOR
: l1
= l1
|| l2
; break;
1780 if (t1
!= VT_LLONG
&& (PTR_SIZE
!= 8 || t1
!= VT_PTR
))
1781 l1
= ((uint32_t)l1
|
1782 (v1
->type
.t
& VT_UNSIGNED
? 0 : -(l1
& 0x80000000)));
1786 /* if commutative ops, put c2 as constant */
1787 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1788 op
== '|' || op
== '*')) {
1790 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1791 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1793 if (!const_wanted
&&
1795 (op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
)) ||
1796 (l1
== -1 && op
== TOK_SAR
))) {
1797 /* treat (0 << x), (0 >> x) and (-1 >> x) as constant */
1799 } else if (!const_wanted
&&
1800 c2
&& ((l2
== 0 && (op
== '&' || op
== '*')) ||
1801 (l2
== -1 && op
== '|') ||
1802 (l2
== 0xffffffff && t2
!= VT_LLONG
&& op
== '|') ||
1803 (l2
== 1 && (op
== '%' || op
== TOK_UMOD
)))) {
1804 /* treat (x & 0), (x * 0), (x | -1) and (x % 1) as constant */
1809 } else if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1812 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1813 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1817 /* filter out NOP operations like x*1, x-0, x&-1... */
1819 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1820 /* try to use shifts instead of muls or divs */
1821 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1830 else if (op
== TOK_PDIV
)
1836 } else if (c2
&& (op
== '+' || op
== '-') &&
1837 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
1838 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1839 /* symbol + constant case */
1843 /* The backends can't always deal with addends to symbols
1844 larger than +-1<<31. Don't construct such. */
1851 /* call low level op generator */
1852 if (t1
== VT_LLONG
|| t2
== VT_LLONG
||
1853 (PTR_SIZE
== 8 && (t1
== VT_PTR
|| t2
== VT_PTR
)))
1861 /* generate a floating point operation with constant propagation */
1862 static void gen_opif(int op
)
1870 /* currently, we cannot do computations with forward symbols */
1871 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1872 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1874 if (v1
->type
.t
== VT_FLOAT
) {
1877 } else if (v1
->type
.t
== VT_DOUBLE
) {
1885 /* NOTE: we only do constant propagation if finite number (not
1886 NaN or infinity) (ANSI spec) */
1887 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1891 case '+': f1
+= f2
; break;
1892 case '-': f1
-= f2
; break;
1893 case '*': f1
*= f2
; break;
1897 tcc_error("division by zero in constant");
1902 /* XXX: also handles tests ? */
1906 /* XXX: overflow test ? */
1907 if (v1
->type
.t
== VT_FLOAT
) {
1909 } else if (v1
->type
.t
== VT_DOUBLE
) {
1921 static int pointed_size(CType
*type
)
1924 return type_size(pointed_type(type
), &align
);
1927 static void vla_runtime_pointed_size(CType
*type
)
1930 vla_runtime_type_size(pointed_type(type
), &align
);
1933 static inline int is_null_pointer(SValue
*p
)
1935 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1937 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& (uint32_t)p
->c
.i
== 0) ||
1938 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.i
== 0) ||
1939 ((p
->type
.t
& VT_BTYPE
) == VT_PTR
&&
1940 (PTR_SIZE
== 4 ? (uint32_t)p
->c
.i
== 0 : p
->c
.i
== 0));
1943 static inline int is_integer_btype(int bt
)
1945 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
1946 bt
== VT_INT
|| bt
== VT_LLONG
);
1949 /* check types for comparison or subtraction of pointers */
1950 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
1952 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
1955 /* null pointers are accepted for all comparisons as gcc */
1956 if (is_null_pointer(p1
) || is_null_pointer(p2
))
1960 bt1
= type1
->t
& VT_BTYPE
;
1961 bt2
= type2
->t
& VT_BTYPE
;
1962 /* accept comparison between pointer and integer with a warning */
1963 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
1964 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
1965 tcc_warning("comparison between pointer and integer");
1969 /* both must be pointers or implicit function pointers */
1970 if (bt1
== VT_PTR
) {
1971 type1
= pointed_type(type1
);
1972 } else if (bt1
!= VT_FUNC
)
1973 goto invalid_operands
;
1975 if (bt2
== VT_PTR
) {
1976 type2
= pointed_type(type2
);
1977 } else if (bt2
!= VT_FUNC
) {
1979 tcc_error("invalid operands to binary %s", get_tok_str(op
, NULL
));
1981 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1982 (type2
->t
& VT_BTYPE
) == VT_VOID
)
1986 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1987 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1988 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1989 /* gcc-like error if '-' is used */
1991 goto invalid_operands
;
1993 tcc_warning("comparison of distinct pointer types lacks a cast");
1997 /* generic gen_op: handles types problems */
1998 ST_FUNC
void gen_op(int op
)
2000 int u
, t1
, t2
, bt1
, bt2
, t
;
2004 t1
= vtop
[-1].type
.t
;
2005 t2
= vtop
[0].type
.t
;
2006 bt1
= t1
& VT_BTYPE
;
2007 bt2
= t2
& VT_BTYPE
;
2009 if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
2010 tcc_error("operation on a struct");
2011 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
2012 if (bt2
== VT_FUNC
) {
2013 mk_pointer(&vtop
->type
);
2016 if (bt1
== VT_FUNC
) {
2018 mk_pointer(&vtop
->type
);
2023 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
2024 /* at least one operand is a pointer */
2025 /* relationnal op: must be both pointers */
2026 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
2027 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
2028 /* pointers are handled are unsigned */
2029 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2030 t
= VT_LLONG
| VT_UNSIGNED
;
2032 t
= VT_INT
| VT_UNSIGNED
;
2036 /* if both pointers, then it must be the '-' op */
2037 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
2039 tcc_error("cannot use pointers here");
2040 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
2041 /* XXX: check that types are compatible */
2042 if (vtop
[-1].type
.t
& VT_VLA
) {
2043 vla_runtime_pointed_size(&vtop
[-1].type
);
2045 vpushi(pointed_size(&vtop
[-1].type
));
2049 /* set to integer type */
2050 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2051 vtop
->type
.t
= VT_LLONG
;
2053 vtop
->type
.t
= VT_INT
;
2058 /* exactly one pointer : must be '+' or '-'. */
2059 if (op
!= '-' && op
!= '+')
2060 tcc_error("cannot use pointers here");
2061 /* Put pointer as first operand */
2062 if (bt2
== VT_PTR
) {
2064 t
= t1
, t1
= t2
, t2
= t
;
2067 if ((vtop
[0].type
.t
& VT_BTYPE
) == VT_LLONG
)
2068 /* XXX: truncate here because gen_opl can't handle ptr + long long */
2069 gen_cast(&int_type
);
2071 type1
= vtop
[-1].type
;
2072 type1
.t
&= ~VT_ARRAY
;
2073 if (vtop
[-1].type
.t
& VT_VLA
)
2074 vla_runtime_pointed_size(&vtop
[-1].type
);
2076 u
= pointed_size(&vtop
[-1].type
);
2078 tcc_error("unknown array element size");
2079 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2082 /* XXX: cast to int ? (long long case) */
2088 /* #ifdef CONFIG_TCC_BCHECK
2089 The main reason to removing this code:
2096 fprintf(stderr, "v+i-j = %p\n", v+i-j);
2097 fprintf(stderr, "v+(i-j) = %p\n", v+(i-j));
2099 When this code is on. then the output looks like
2101 v+(i-j) = 0xbff84000
2103 /* if evaluating constant expression, no code should be
2104 generated, so no bound check */
2105 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
2106 /* if bounded pointers, we generate a special code to
2113 gen_bounded_ptr_add();
2119 /* put again type if gen_opic() swaped operands */
2122 } else if (is_float(bt1
) || is_float(bt2
)) {
2123 /* compute bigger type and do implicit casts */
2124 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
2126 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
2131 /* floats can only be used for a few operations */
2132 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
2133 (op
< TOK_ULT
|| op
> TOK_GT
))
2134 tcc_error("invalid operands for binary operation");
2136 } else if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
) {
2137 t
= bt1
== VT_LLONG
? VT_LLONG
: VT_INT
;
2138 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (t
| VT_UNSIGNED
))
2141 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
2142 /* cast to biggest op */
2144 /* convert to unsigned if it does not fit in a long long */
2145 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
2146 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
2150 /* integer operations */
2152 /* convert to unsigned if it does not fit in an integer */
2153 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
2154 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
2157 /* XXX: currently, some unsigned operations are explicit, so
2158 we modify them here */
2159 if (t
& VT_UNSIGNED
) {
2166 else if (op
== TOK_LT
)
2168 else if (op
== TOK_GT
)
2170 else if (op
== TOK_LE
)
2172 else if (op
== TOK_GE
)
2179 /* special case for shifts and long long: we keep the shift as
2181 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
2188 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
2189 /* relationnal op: the result is an int */
2190 vtop
->type
.t
= VT_INT
;
2195 // Make sure that we have converted to an rvalue:
2196 if (vtop
->r
& VT_LVAL
)
2197 gv(is_float(vtop
->type
.t
& VT_BTYPE
) ? RC_FLOAT
: RC_INT
);
2200 #ifndef TCC_TARGET_ARM
2201 /* generic itof for unsigned long long case */
2202 static void gen_cvt_itof1(int t
)
2204 #ifdef TCC_TARGET_ARM64
2207 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
2208 (VT_LLONG
| VT_UNSIGNED
)) {
2211 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
2212 #if LDOUBLE_SIZE != 8
2213 else if (t
== VT_LDOUBLE
)
2214 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
2217 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
2221 vtop
->r
= reg_fret(t
);
2229 /* generic ftoi for unsigned long long case */
2230 static void gen_cvt_ftoi1(int t
)
2232 #ifdef TCC_TARGET_ARM64
2237 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
2238 /* not handled natively */
2239 st
= vtop
->type
.t
& VT_BTYPE
;
2241 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
2242 #if LDOUBLE_SIZE != 8
2243 else if (st
== VT_LDOUBLE
)
2244 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
2247 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
2252 vtop
->r2
= REG_LRET
;
2259 /* force char or short cast */
2260 static void force_charshort_cast(int t
)
2264 /* XXX: add optimization if lvalue : just change type and offset */
2269 if (t
& VT_UNSIGNED
) {
2270 vpushi((1 << bits
) - 1);
2273 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
)
2279 /* result must be signed or the SAR is converted to an SHL
2280 This was not the case when "t" was a signed short
2281 and the last value on the stack was an unsigned int */
2282 vtop
->type
.t
&= ~VT_UNSIGNED
;
2288 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
2289 static void gen_cast(CType
*type
)
2291 int sbt
, dbt
, sf
, df
, c
, p
;
2293 /* special delayed cast for char/short */
2294 /* XXX: in some cases (multiple cascaded casts), it may still
2296 if (vtop
->r
& VT_MUSTCAST
) {
2297 vtop
->r
&= ~VT_MUSTCAST
;
2298 force_charshort_cast(vtop
->type
.t
);
2301 /* bitfields first get cast to ints */
2302 if (vtop
->type
.t
& VT_BITFIELD
) {
2306 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
2307 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
2312 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
2313 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
2315 /* constant case: we can do it now */
2316 /* XXX: in ISOC, cannot do it if error in convert */
2317 if (sbt
== VT_FLOAT
)
2318 vtop
->c
.ld
= vtop
->c
.f
;
2319 else if (sbt
== VT_DOUBLE
)
2320 vtop
->c
.ld
= vtop
->c
.d
;
2323 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
2324 if ((sbt
& VT_UNSIGNED
) || !(vtop
->c
.i
>> 63))
2325 vtop
->c
.ld
= vtop
->c
.i
;
2327 vtop
->c
.ld
= -(long double)-vtop
->c
.i
;
2329 if ((sbt
& VT_UNSIGNED
) || !(vtop
->c
.i
>> 31))
2330 vtop
->c
.ld
= (uint32_t)vtop
->c
.i
;
2332 vtop
->c
.ld
= -(long double)-(uint32_t)vtop
->c
.i
;
2335 if (dbt
== VT_FLOAT
)
2336 vtop
->c
.f
= (float)vtop
->c
.ld
;
2337 else if (dbt
== VT_DOUBLE
)
2338 vtop
->c
.d
= (double)vtop
->c
.ld
;
2339 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
2340 vtop
->c
.i
= vtop
->c
.ld
;
2341 } else if (sf
&& dbt
== VT_BOOL
) {
2342 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
2345 vtop
->c
.i
= vtop
->c
.ld
;
2346 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
2348 else if (sbt
& VT_UNSIGNED
)
2349 vtop
->c
.i
= (uint32_t)vtop
->c
.i
;
2350 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2351 else if (sbt
== VT_PTR
)
2354 else if (sbt
!= VT_LLONG
)
2355 vtop
->c
.i
= ((uint32_t)vtop
->c
.i
|
2356 -(vtop
->c
.i
& 0x80000000));
2358 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
2360 else if (dbt
== VT_BOOL
)
2361 vtop
->c
.i
= (vtop
->c
.i
!= 0);
2362 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2363 else if (dbt
== VT_PTR
)
2366 else if (dbt
!= VT_LLONG
) {
2367 uint32_t m
= ((dbt
& VT_BTYPE
) == VT_BYTE
? 0xff :
2368 (dbt
& VT_BTYPE
) == VT_SHORT
? 0xffff :
2371 if (!(dbt
& VT_UNSIGNED
))
2372 vtop
->c
.i
|= -(vtop
->c
.i
& ((m
>> 1) + 1));
2375 } else if (p
&& dbt
== VT_BOOL
) {
2379 /* non constant case: generate code */
2381 /* convert from fp to fp */
2384 /* convert int to fp */
2387 /* convert fp to int */
2388 if (dbt
== VT_BOOL
) {
2392 if (sbt
== VT_FLOAT
) {
2393 /* cast to DOUBLE to avoid precision loss */
2394 gen_cvt_ftof(VT_DOUBLE
);
2395 vtop
->type
.t
= (vtop
->type
.t
& ~VT_BTYPE
) | VT_DOUBLE
;
2397 /* we handle char/short/etc... with generic code */
2398 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
2399 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
2403 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
2404 /* additional cast for char/short... */
2409 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
2410 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
2411 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
2412 /* scalar to long long */
2413 /* machine independent conversion */
2415 /* generate high word */
2416 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
2420 if (sbt
== VT_PTR
) {
2421 /* cast from pointer to int before we apply
2422 shift operation, which pointers don't support*/
2423 gen_cast(&int_type
);
2429 /* patch second register */
2430 vtop
[-1].r2
= vtop
->r
;
2434 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
2435 (dbt
& VT_BTYPE
) == VT_PTR
||
2436 (dbt
& VT_BTYPE
) == VT_FUNC
) {
2437 if ((sbt
& VT_BTYPE
) != VT_LLONG
&&
2438 (sbt
& VT_BTYPE
) != VT_PTR
&&
2439 (sbt
& VT_BTYPE
) != VT_FUNC
) {
2440 /* need to convert from 32bit to 64bit */
2442 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
2443 #if defined(TCC_TARGET_ARM64)
2445 #elif defined(TCC_TARGET_X86_64)
2447 /* x86_64 specific: movslq */
2449 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
2456 } else if (dbt
== VT_BOOL
) {
2457 /* scalar to bool */
2460 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
2461 (dbt
& VT_BTYPE
) == VT_SHORT
) {
2462 if (sbt
== VT_PTR
) {
2463 vtop
->type
.t
= VT_INT
;
2464 tcc_warning("nonportable conversion from pointer to char/short");
2466 force_charshort_cast(dbt
);
2467 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
2468 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
2470 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
2471 /* from long long: just take low order word */
2475 /* if lvalue and single word type, nothing to do because
2476 the lvalue already contains the real type size (see
2477 VT_LVAL_xxx constants) */
2481 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
2482 /* if we are casting between pointer types,
2483 we must update the VT_LVAL_xxx size */
2484 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
2485 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
2490 /* return type size as known at compile time. Put alignment at 'a' */
2491 ST_FUNC
int type_size(CType
*type
, int *a
)
2496 bt
= type
->t
& VT_BTYPE
;
2497 if (bt
== VT_STRUCT
) {
2502 } else if (bt
== VT_PTR
) {
2503 if (type
->t
& VT_ARRAY
) {
2507 ts
= type_size(&s
->type
, a
);
2509 if (ts
< 0 && s
->c
< 0)
2517 } else if (bt
== VT_LDOUBLE
) {
2519 return LDOUBLE_SIZE
;
2520 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
2521 #ifdef TCC_TARGET_I386
2522 #ifdef TCC_TARGET_PE
2527 #elif defined(TCC_TARGET_ARM)
2537 } else if (bt
== VT_INT
|| bt
== VT_FLOAT
) {
2540 } else if (bt
== VT_SHORT
) {
2543 } else if (bt
== VT_QLONG
|| bt
== VT_QFLOAT
) {
2546 } else if (bt
== VT_ENUM
) {
2548 /* Enums might be incomplete, so don't just return '4' here. */
2549 return type
->ref
->c
;
2551 /* char, void, function, _Bool */
2557 /* push type size as known at runtime time on top of value stack. Put
2559 ST_FUNC
void vla_runtime_type_size(CType
*type
, int *a
)
2561 if (type
->t
& VT_VLA
) {
2562 type_size(&type
->ref
->type
, a
);
2563 vset(&int_type
, VT_LOCAL
|VT_LVAL
, type
->ref
->c
);
2565 vpushi(type_size(type
, a
));
2569 static void vla_sp_restore(void) {
2570 if (vlas_in_scope
) {
2571 gen_vla_sp_restore(vla_sp_loc
);
2575 static void vla_sp_restore_root(void) {
2576 if (vlas_in_scope
) {
2577 gen_vla_sp_restore(vla_sp_root_loc
);
2581 /* return the pointed type of t */
2582 static inline CType
*pointed_type(CType
*type
)
2584 return &type
->ref
->type
;
2587 /* modify type so that its it is a pointer to type. */
2588 ST_FUNC
void mk_pointer(CType
*type
)
2591 s
= sym_push(SYM_FIELD
, type
, 0, -1);
2592 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
2596 /* compare function types. OLD functions match any new functions */
2597 static int is_compatible_func(CType
*type1
, CType
*type2
)
2603 if (!is_compatible_types(&s1
->type
, &s2
->type
))
2605 /* check func_call */
2606 if (s1
->a
.func_call
!= s2
->a
.func_call
)
2608 /* XXX: not complete */
2609 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
2613 while (s1
!= NULL
) {
2616 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
2626 /* return true if type1 and type2 are the same. If unqualified is
2627 true, qualifiers on the types are ignored.
2629 - enums are not checked as gcc __builtin_types_compatible_p ()
2631 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2635 t1
= type1
->t
& VT_TYPE
;
2636 t2
= type2
->t
& VT_TYPE
;
2638 /* strip qualifiers before comparing */
2639 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2640 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2642 /* Default Vs explicit signedness only matters for char */
2643 if ((t1
& VT_BTYPE
) != VT_BYTE
) {
2647 /* An enum is compatible with (unsigned) int. Ideally we would
2648 store the enums signedness in type->ref.a.<some_bit> and
2649 only accept unsigned enums with unsigned int and vice versa.
2650 But one of our callers (gen_assign_cast) always strips VT_UNSIGNED
2651 from pointer target types, so we can't add it here either. */
2652 if ((t1
& VT_BTYPE
) == VT_ENUM
) {
2654 if (type1
->ref
->a
.unsigned_enum
)
2657 if ((t2
& VT_BTYPE
) == VT_ENUM
) {
2659 if (type2
->ref
->a
.unsigned_enum
)
2662 /* XXX: bitfields ? */
2665 /* test more complicated cases */
2666 bt1
= t1
& VT_BTYPE
;
2667 if (bt1
== VT_PTR
) {
2668 type1
= pointed_type(type1
);
2669 type2
= pointed_type(type2
);
2670 return is_compatible_types(type1
, type2
);
2671 } else if (bt1
== VT_STRUCT
) {
2672 return (type1
->ref
== type2
->ref
);
2673 } else if (bt1
== VT_FUNC
) {
2674 return is_compatible_func(type1
, type2
);
2680 /* return true if type1 and type2 are exactly the same (including
2683 static int is_compatible_types(CType
*type1
, CType
*type2
)
2685 return compare_types(type1
,type2
,0);
2688 /* return true if type1 and type2 are the same (ignoring qualifiers).
2690 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
2692 return compare_types(type1
,type2
,1);
2695 /* print a type. If 'varstr' is not NULL, then the variable is also
2696 printed in the type */
2698 /* XXX: add array and function pointers */
2699 static void type_to_str(char *buf
, int buf_size
,
2700 CType
*type
, const char *varstr
)
2707 t
= type
->t
& VT_TYPE
;
2710 if (t
& VT_CONSTANT
)
2711 pstrcat(buf
, buf_size
, "const ");
2712 if (t
& VT_VOLATILE
)
2713 pstrcat(buf
, buf_size
, "volatile ");
2714 if ((t
& (VT_DEFSIGN
| VT_UNSIGNED
)) == (VT_DEFSIGN
| VT_UNSIGNED
))
2715 pstrcat(buf
, buf_size
, "unsigned ");
2716 else if (t
& VT_DEFSIGN
)
2717 pstrcat(buf
, buf_size
, "signed ");
2747 tstr
= "long double";
2749 pstrcat(buf
, buf_size
, tstr
);
2753 if (bt
== VT_STRUCT
)
2757 pstrcat(buf
, buf_size
, tstr
);
2758 v
= type
->ref
->v
& ~SYM_STRUCT
;
2759 if (v
>= SYM_FIRST_ANOM
)
2760 pstrcat(buf
, buf_size
, "<anonymous>");
2762 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
2766 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
2767 pstrcat(buf
, buf_size
, "(");
2769 while (sa
!= NULL
) {
2770 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
2771 pstrcat(buf
, buf_size
, buf1
);
2774 pstrcat(buf
, buf_size
, ", ");
2776 pstrcat(buf
, buf_size
, ")");
2781 snprintf(buf1
, sizeof(buf1
), "%s[%ld]", varstr
? varstr
: "", s
->c
);
2782 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2785 pstrcpy(buf1
, sizeof(buf1
), "*");
2786 if (t
& VT_CONSTANT
)
2787 pstrcat(buf1
, buf_size
, "const ");
2788 if (t
& VT_VOLATILE
)
2789 pstrcat(buf1
, buf_size
, "volatile ");
2791 pstrcat(buf1
, sizeof(buf1
), varstr
);
2792 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2796 pstrcat(buf
, buf_size
, " ");
2797 pstrcat(buf
, buf_size
, varstr
);
2802 /* verify type compatibility to store vtop in 'dt' type, and generate
2804 static void gen_assign_cast(CType
*dt
)
2806 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
2807 char buf1
[256], buf2
[256];
2810 st
= &vtop
->type
; /* source type */
2811 dbt
= dt
->t
& VT_BTYPE
;
2812 sbt
= st
->t
& VT_BTYPE
;
2813 if (sbt
== VT_VOID
|| dbt
== VT_VOID
) {
2814 if (sbt
== VT_VOID
&& dbt
== VT_VOID
)
2816 It is Ok if both are void
2822 gcc accepts this program
2825 tcc_error("cannot cast from/to void");
2827 if (dt
->t
& VT_CONSTANT
)
2828 tcc_warning("assignment of read-only location");
2831 /* special cases for pointers */
2832 /* '0' can also be a pointer */
2833 if (is_null_pointer(vtop
))
2835 /* accept implicit pointer to integer cast with warning */
2836 if (is_integer_btype(sbt
)) {
2837 tcc_warning("assignment makes pointer from integer without a cast");
2840 type1
= pointed_type(dt
);
2841 /* a function is implicitely a function pointer */
2842 if (sbt
== VT_FUNC
) {
2843 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
2844 !is_compatible_types(pointed_type(dt
), st
))
2845 tcc_warning("assignment from incompatible pointer type");
2850 type2
= pointed_type(st
);
2851 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2852 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
2853 /* void * can match anything */
2855 /* exact type match, except for qualifiers */
2858 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2859 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2860 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2861 /* Like GCC don't warn by default for merely changes
2862 in pointer target signedness. Do warn for different
2863 base types, though, in particular for unsigned enums
2864 and signed int targets. */
2865 if ((tmp_type1
.t
& (VT_DEFSIGN
| VT_UNSIGNED
)) !=
2866 (tmp_type2
.t
& (VT_DEFSIGN
| VT_UNSIGNED
)) &&
2867 (tmp_type1
.t
& VT_BTYPE
) == (tmp_type2
.t
& VT_BTYPE
))
2870 tcc_warning("assignment from incompatible pointer type");
2873 /* check const and volatile */
2874 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
2875 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
2876 tcc_warning("assignment discards qualifiers from pointer target type");
2882 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
2883 tcc_warning("assignment makes integer from pointer without a cast");
2884 } else if (sbt
== VT_STRUCT
) {
2885 goto case_VT_STRUCT
;
2887 /* XXX: more tests */
2893 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2894 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2895 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2897 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
2898 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
2899 tcc_error("cannot cast '%s' to '%s'", buf1
, buf2
);
2907 /* store vtop in lvalue pushed on stack */
2908 ST_FUNC
void vstore(void)
2910 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
2912 ft
= vtop
[-1].type
.t
;
2913 sbt
= vtop
->type
.t
& VT_BTYPE
;
2914 dbt
= ft
& VT_BTYPE
;
2915 if ((((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2916 (sbt
== VT_INT
&& dbt
== VT_SHORT
))
2917 && !(vtop
->type
.t
& VT_BITFIELD
)) {
2918 /* optimize char/short casts */
2919 delayed_cast
= VT_MUSTCAST
;
2920 vtop
->type
.t
= (ft
& VT_TYPE
& ~VT_BITFIELD
&
2921 ((1 << VT_STRUCT_SHIFT
) - 1));
2922 /* XXX: factorize */
2923 if (ft
& VT_CONSTANT
)
2924 tcc_warning("assignment of read-only location");
2927 if (!(ft
& VT_BITFIELD
))
2928 gen_assign_cast(&vtop
[-1].type
);
2931 if (sbt
== VT_STRUCT
) {
2932 /* if structure, only generate pointer */
2933 /* structure assignment : generate memcpy */
2934 /* XXX: optimize if small size */
2935 size
= type_size(&vtop
->type
, &align
);
2939 vtop
->type
.t
= VT_PTR
;
2942 /* address of memcpy() */
2945 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2946 else if(!(align
& 3))
2947 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2950 /* Use memmove, rather than memcpy, as dest and src may be same: */
2951 vpush_global_sym(&func_old_type
, TOK_memmove
);
2956 vtop
->type
.t
= VT_PTR
;
2962 /* leave source on stack */
2963 } else if (ft
& VT_BITFIELD
) {
2964 /* bitfield store handling */
2966 /* save lvalue as expression result (example: s.b = s.a = n;) */
2967 vdup(), vtop
[-1] = vtop
[-2];
2969 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2970 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2971 /* remove bit field info to avoid loops */
2972 vtop
[-1].type
.t
= ft
& ~VT_BITFIELD
& ((1 << VT_STRUCT_SHIFT
) - 1);
2974 if((ft
& VT_BTYPE
) == VT_BOOL
) {
2975 gen_cast(&vtop
[-1].type
);
2976 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2979 /* duplicate destination */
2981 vtop
[-1] = vtop
[-2];
2983 /* mask and shift source */
2984 if((ft
& VT_BTYPE
) != VT_BOOL
) {
2985 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2986 vpushll((1ULL << bit_size
) - 1ULL);
2988 vpushi((1 << bit_size
) - 1);
2994 /* load destination, mask and or with source */
2996 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2997 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2999 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
3005 /* ... and discard */
3009 #ifdef CONFIG_TCC_BCHECK
3010 /* bound check case */
3011 if (vtop
[-1].r
& VT_MUSTBOUND
) {
3020 #ifdef TCC_TARGET_X86_64
3021 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
3023 } else if ((ft
& VT_BTYPE
) == VT_QFLOAT
) {
3028 r
= gv(rc
); /* generate value */
3029 /* if lvalue was saved on stack, must read it */
3030 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
3032 t
= get_reg(RC_INT
);
3033 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
3038 sv
.r
= VT_LOCAL
| VT_LVAL
;
3039 sv
.c
.i
= vtop
[-1].c
.i
;
3041 vtop
[-1].r
= t
| VT_LVAL
;
3043 /* two word case handling : store second register at word + 4 (or +8 for x86-64) */
3044 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
3045 if (((ft
& VT_BTYPE
) == VT_QLONG
) || ((ft
& VT_BTYPE
) == VT_QFLOAT
)) {
3046 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
3048 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
3049 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
3051 vtop
[-1].type
.t
= load_type
;
3054 /* convert to int to increment easily */
3055 vtop
->type
.t
= addr_type
;
3061 vtop
[-1].type
.t
= load_type
;
3062 /* XXX: it works because r2 is spilled last ! */
3063 store(vtop
->r2
, vtop
- 1);
3069 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
3070 vtop
->r
|= delayed_cast
;
3074 /* post defines POST/PRE add. c is the token ++ or -- */
3075 ST_FUNC
void inc(int post
, int c
)
3078 vdup(); /* save lvalue */
3080 gv_dup(); /* duplicate value */
3085 vpushi(c
- TOK_MID
);
3087 vstore(); /* store value */
3089 vpop(); /* if post op, return saved value */
3092 ST_FUNC
void parse_mult_str (CString
*astr
, const char *msg
)
3094 /* read the string */
3098 while (tok
== TOK_STR
) {
3099 /* XXX: add \0 handling too ? */
3100 cstr_cat(astr
, tokc
.str
.data
, -1);
3103 cstr_ccat(astr
, '\0');
3106 /* If I is >= 1 and a power of two, returns log2(i)+1.
3107 If I is 0 returns 0. */
3108 static int exact_log2p1(int i
)
3113 for (ret
= 1; i
>= 1 << 8; ret
+= 8)
3124 /* Parse GNUC __attribute__ extension. Currently, the following
3125 extensions are recognized:
3126 - aligned(n) : set data/function alignment.
3127 - packed : force data alignment to 1
3128 - section(x) : generate data/code in this section.
3129 - unused : currently ignored, but may be used someday.
3130 - regparm(n) : pass function parameters in registers (i386 only)
3132 static void parse_attribute(AttributeDef
*ad
)
3137 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
3141 while (tok
!= ')') {
3142 if (tok
< TOK_IDENT
)
3143 expect("attribute name");
3150 parse_mult_str(&astr
, "section name");
3151 ad
->section
= find_section(tcc_state
, (char *)astr
.data
);
3158 parse_mult_str(&astr
, "alias(\"target\")");
3159 ad
->alias_target
= /* save string as token, for later */
3160 tok_alloc((char*)astr
.data
, astr
.size
-1)->tok
;
3164 case TOK_VISIBILITY1
:
3165 case TOK_VISIBILITY2
:
3167 parse_mult_str(&astr
,
3168 "visibility(\"default|hidden|internal|protected\")");
3169 if (!strcmp (astr
.data
, "default"))
3170 ad
->a
.visibility
= STV_DEFAULT
;
3171 else if (!strcmp (astr
.data
, "hidden"))
3172 ad
->a
.visibility
= STV_HIDDEN
;
3173 else if (!strcmp (astr
.data
, "internal"))
3174 ad
->a
.visibility
= STV_INTERNAL
;
3175 else if (!strcmp (astr
.data
, "protected"))
3176 ad
->a
.visibility
= STV_PROTECTED
;
3178 expect("visibility(\"default|hidden|internal|protected\")");
3187 if (n
<= 0 || (n
& (n
- 1)) != 0)
3188 tcc_error("alignment must be a positive power of two");
3193 ad
->a
.aligned
= exact_log2p1(n
);
3194 if (n
!= 1 << (ad
->a
.aligned
- 1))
3195 tcc_error("alignment of %d is larger than implemented", n
);
3207 /* currently, no need to handle it because tcc does not
3208 track unused objects */
3212 /* currently, no need to handle it because tcc does not
3213 track unused objects */
3218 ad
->a
.func_call
= FUNC_CDECL
;
3223 ad
->a
.func_call
= FUNC_STDCALL
;
3225 #ifdef TCC_TARGET_I386
3235 ad
->a
.func_call
= FUNC_FASTCALL1
+ n
- 1;
3241 ad
->a
.func_call
= FUNC_FASTCALLW
;
3248 ad
->a
.mode
= VT_LLONG
+ 1;
3251 ad
->a
.mode
= VT_BYTE
+ 1;
3254 ad
->a
.mode
= VT_SHORT
+ 1;
3258 ad
->a
.mode
= VT_INT
+ 1;
3261 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
3268 ad
->a
.func_export
= 1;
3271 ad
->a
.func_import
= 1;
3274 if (tcc_state
->warn_unsupported
)
3275 tcc_warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
3276 /* skip parameters */
3278 int parenthesis
= 0;
3282 else if (tok
== ')')
3285 } while (parenthesis
&& tok
!= -1);
3298 static Sym
* find_field (CType
*type
, int v
)
3302 while ((s
= s
->next
) != NULL
) {
3303 if ((s
->v
& SYM_FIELD
) &&
3304 (s
->type
.t
& VT_BTYPE
) == VT_STRUCT
&&
3305 (s
->v
& ~SYM_FIELD
) >= SYM_FIRST_ANOM
) {
3306 Sym
*ret
= find_field (&s
->type
, v
);
3316 static void struct_add_offset (Sym
*s
, int offset
)
3318 while ((s
= s
->next
) != NULL
) {
3319 if ((s
->v
& SYM_FIELD
) &&
3320 (s
->type
.t
& VT_BTYPE
) == VT_STRUCT
&&
3321 (s
->v
& ~SYM_FIELD
) >= SYM_FIRST_ANOM
) {
3322 struct_add_offset(s
->type
.ref
, offset
);
3328 static void struct_layout(CType
*type
, AttributeDef
*ad
)
3330 int align
, maxalign
, offset
, c
, bit_pos
, bt
, prevbt
, prev_bit_size
;
3331 int pcc
= !tcc_state
->ms_bitfields
;
3334 maxalign
= 1 << (ad
->a
.aligned
- 1);
3340 prevbt
= VT_STRUCT
; /* make it never match */
3342 for (f
= type
->ref
->next
; f
; f
= f
->next
) {
3343 int typealign
, bit_size
;
3344 int size
= type_size(&f
->type
, &typealign
);
3345 if (f
->type
.t
& VT_BITFIELD
)
3346 bit_size
= (f
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
3349 if (bit_size
== 0 && pcc
) {
3350 /* Zero-width bit-fields in PCC mode aren't affected
3351 by any packing (attribute or pragma). */
3353 } else if (f
->r
> 1) {
3355 } else if (ad
->a
.packed
|| f
->r
== 1) {
3357 /* Packed fields or packed records don't let the base type
3358 influence the records type alignment. */
3363 if (type
->ref
->type
.t
!= TOK_STRUCT
) {
3364 if (pcc
&& bit_size
>= 0)
3365 size
= (bit_size
+ 7) >> 3;
3366 /* Bit position is already zero from our caller. */
3370 } else if (bit_size
< 0) {
3371 int addbytes
= pcc
? (bit_pos
+ 7) >> 3 : 0;
3374 c
= (c
+ addbytes
+ align
- 1) & -align
;
3380 /* A bit-field. Layout is more complicated. There are two
3381 options TCC implements: PCC compatible and MS compatible
3382 (PCC compatible is what GCC uses for almost all targets).
3383 In PCC layout the overall size of the struct (in c) is
3384 _excluding_ the current run of bit-fields (that is,
3385 there's at least additional bit_pos bits after c). In
3386 MS layout c does include the current run of bit-fields.
3388 This matters for calculating the natural alignment buckets
3391 /* 'align' will be used to influence records alignment,
3392 so it's the max of specified and type alignment, except
3393 in certain cases that depend on the mode. */
3394 if (align
< typealign
)
3397 /* In PCC layout a non-packed bit-field is placed adjacent
3398 to the preceding bit-fields, except if it would overflow
3399 its container (depending on base type) or it's a zero-width
3400 bit-field. Packed non-zero-width bit-fields always are
3402 int ofs
= (c
* 8 + bit_pos
) % (typealign
* 8);
3403 int ofs2
= ofs
+ bit_size
+ (typealign
* 8) - 1;
3404 if (bit_size
== 0 ||
3406 (ofs2
/ (typealign
* 8)) > (size
/typealign
))) {
3407 c
= (c
+ ((bit_pos
+ 7) >> 3) + typealign
- 1) & -typealign
;
3411 /* In PCC layout named bit-fields influence the alignment
3412 of the containing struct using the base types alignment,
3413 except for packed fields (which here have correct
3414 align/typealign). */
3415 if ((f
->v
& SYM_FIRST_ANOM
))
3418 bt
= f
->type
.t
& VT_BTYPE
;
3419 if ((bit_pos
+ bit_size
> size
* 8) ||
3420 (bit_size
> 0) == (bt
!= prevbt
)) {
3421 c
= (c
+ typealign
- 1) & -typealign
;
3424 /* In MS bitfield mode a bit-field run always uses
3425 at least as many bits as the underlying type.
3426 To start a new run it's also required that this
3427 or the last bit-field had non-zero width. */
3428 if (bit_size
|| prev_bit_size
)
3431 /* In MS layout the records alignment is normally
3432 influenced by the field, except for a zero-width
3433 field at the start of a run (but by further zero-width
3434 fields it is again). */
3435 if (bit_size
== 0 && prevbt
!= bt
)
3438 prev_bit_size
= bit_size
;
3440 f
->type
.t
= (f
->type
.t
& ~(0x3f << VT_STRUCT_SHIFT
))
3441 | (bit_pos
<< VT_STRUCT_SHIFT
);
3442 bit_pos
+= bit_size
;
3443 if (pcc
&& bit_pos
>= size
* 8) {
3445 bit_pos
-= size
* 8;
3448 if (align
> maxalign
)
3451 printf("set field %s offset=%d c=%d",
3452 get_tok_str(f
->v
& ~SYM_FIELD
, NULL
), offset
, c
);
3453 if (f
->type
.t
& VT_BITFIELD
) {
3454 printf(" pos=%d size=%d",
3455 (f
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
3456 (f
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
3461 if (f
->v
& SYM_FIRST_ANOM
&& (f
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
3463 /* An anonymous struct/union. Adjust member offsets
3464 to reflect the real offset of our containing struct.
3465 Also set the offset of this anon member inside
3466 the outer struct to be zero. Via this it
3467 works when accessing the field offset directly
3468 (from base object), as well as when recursing
3469 members in initializer handling. */
3470 int v2
= f
->type
.ref
->v
;
3471 if (!(v2
& SYM_FIELD
) &&
3472 (v2
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
3474 /* This happens only with MS extensions. The
3475 anon member has a named struct type, so it
3476 potentially is shared with other references.
3477 We need to unshare members so we can modify
3480 f
->type
.ref
= sym_push(anon_sym
++ | SYM_FIELD
,
3481 &f
->type
.ref
->type
, 0,
3483 pps
= &f
->type
.ref
->next
;
3484 while ((ass
= ass
->next
) != NULL
) {
3485 *pps
= sym_push(ass
->v
, &ass
->type
, 0, ass
->c
);
3486 pps
= &((*pps
)->next
);
3490 struct_add_offset(f
->type
.ref
, offset
);
3498 /* store size and alignment */
3499 type
->ref
->c
= (c
+ (pcc
? (bit_pos
+ 7) >> 3 : 0)
3500 + maxalign
- 1) & -maxalign
;
3501 type
->ref
->r
= maxalign
;
3504 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
3505 static void struct_decl(CType
*type
, AttributeDef
*ad
, int u
)
3507 int a
, v
, size
, align
, flexible
, alignoverride
;
3509 int bit_size
, bsize
, bt
;
3514 a
= tok
; /* save decl type */
3516 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3517 parse_attribute(ad
);
3521 /* struct already defined ? return it */
3523 expect("struct/union/enum name");
3525 if (s
&& (s
->scope
== local_scope
|| (tok
!= '{' && tok
!= ';'))) {
3527 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
3533 /* Record the original enum/struct/union token. */
3536 /* we put an undefined size for struct/union */
3537 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
3538 s
->r
= 0; /* default alignment is zero as gcc */
3539 /* put struct/union/enum name in type */
3547 tcc_error("struct/union/enum already defined");
3548 /* cannot be empty */
3550 /* non empty enums are not allowed */
3551 if (a
== TOK_ENUM
) {
3555 CType
*t
= &int_type
;
3558 expect("identifier");
3560 if (ss
&& !local_stack
)
3561 tcc_error("redefinition of enumerator '%s'",
3562 get_tok_str(v
, NULL
));
3566 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
3569 /* We really want to support long long enums
3570 on i386 as well, but the Sym structure only
3571 holds a 'long' for associated constants,
3572 and enlarging it would bump its size (no
3573 available padding). So punt for now. */
3579 if (c
!= (int)c
&& (unsigned long)c
!= (unsigned int)c
)
3580 seen_wide
= 1, t
= &size_type
;
3581 /* enum symbols have static storage */
3582 ss
= sym_push(v
, t
, VT_CONST
, c
);
3583 ss
->type
.t
|= VT_STATIC
;
3588 /* NOTE: we accept a trailing comma */
3593 s
->a
.unsigned_enum
= 1;
3594 s
->c
= type_size(seen_wide
? &size_type
: &int_type
, &align
);
3599 while (tok
!= '}') {
3600 if (!parse_btype(&btype
, &ad1
)) {
3606 tcc_error("flexible array member '%s' not at the end of struct",
3607 get_tok_str(v
, NULL
));
3612 type_decl(&type1
, &ad1
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3614 if ((type1
.t
& VT_BTYPE
) != VT_STRUCT
)
3615 expect("identifier");
3617 int v
= btype
.ref
->v
;
3618 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
3619 if (tcc_state
->ms_extensions
== 0)
3620 expect("identifier");
3624 if (type_size(&type1
, &align
) < 0) {
3625 if ((a
== TOK_STRUCT
) && (type1
.t
& VT_ARRAY
) && c
)
3628 tcc_error("field '%s' has incomplete type",
3629 get_tok_str(v
, NULL
));
3631 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
3632 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
3633 tcc_error("invalid type for '%s'",
3634 get_tok_str(v
, NULL
));
3638 bit_size
= expr_const();
3639 /* XXX: handle v = 0 case for messages */
3641 tcc_error("negative width in bit-field '%s'",
3642 get_tok_str(v
, NULL
));
3643 if (v
&& bit_size
== 0)
3644 tcc_error("zero width for bit-field '%s'",
3645 get_tok_str(v
, NULL
));
3646 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3647 parse_attribute(&ad1
);
3649 size
= type_size(&type1
, &align
);
3650 /* Only remember non-default alignment. */
3652 if (ad1
.a
.aligned
) {
3653 int speca
= 1 << (ad1
.a
.aligned
- 1);
3654 alignoverride
= speca
;
3655 } else if (ad1
.a
.packed
|| ad
->a
.packed
) {
3657 } else if (*tcc_state
->pack_stack_ptr
) {
3658 if (align
> *tcc_state
->pack_stack_ptr
)
3659 alignoverride
= *tcc_state
->pack_stack_ptr
;
3661 if (bit_size
>= 0) {
3662 bt
= type1
.t
& VT_BTYPE
;
3669 tcc_error("bitfields must have scalar type");
3671 if (bit_size
> bsize
) {
3672 tcc_error("width of '%s' exceeds its type",
3673 get_tok_str(v
, NULL
));
3674 } else if (bit_size
== bsize
) {
3675 /* no need for bit fields */
3678 type1
.t
|= VT_BITFIELD
|
3679 (0 << VT_STRUCT_SHIFT
) |
3680 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
3683 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
3684 /* Remember we've seen a real field to check
3685 for placement of flexible array member. */
3688 /* If member is a struct or bit-field, enforce
3689 placing into the struct (as anonymous). */
3691 ((type1
.t
& VT_BTYPE
) == VT_STRUCT
||
3696 ss
= sym_push(v
| SYM_FIELD
, &type1
, alignoverride
, 0);
3700 if (tok
== ';' || tok
== TOK_EOF
)
3707 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3708 parse_attribute(ad
);
3709 struct_layout(type
, ad
);
3714 /* return 1 if basic type is a type size (short, long, long long) */
3715 ST_FUNC
int is_btype_size(int bt
)
3717 return bt
== VT_SHORT
|| bt
== VT_LONG
|| bt
== VT_LLONG
;
3720 /* Add type qualifiers to a type. If the type is an array then the qualifiers
3721 are added to the element type, copied because it could be a typedef. */
3722 static void parse_btype_qualify(CType
*type
, int qualifiers
)
3724 while (type
->t
& VT_ARRAY
) {
3725 type
->ref
= sym_push(SYM_FIELD
, &type
->ref
->type
, 0, type
->ref
->c
);
3726 type
= &type
->ref
->type
;
3728 type
->t
|= qualifiers
;
3731 /* return 0 if no type declaration. otherwise, return the basic type
3734 static int parse_btype(CType
*type
, AttributeDef
*ad
)
3736 int t
, u
, bt_size
, complete
, type_found
, typespec_found
;
3740 memset(ad
, 0, sizeof(AttributeDef
));
3748 /* currently, we really ignore extension */
3759 tcc_error("too many basic types");
3761 bt_size
= is_btype_size (u
& VT_BTYPE
);
3762 if (u
== VT_INT
|| (!bt_size
&& !(t
& VT_TYPEDEF
)))
3777 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
3778 #ifndef TCC_TARGET_PE
3779 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3781 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
3782 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3788 #ifdef TCC_TARGET_ARM64
3790 /* GCC's __uint128_t appears in some Linux header files. Make it a
3791 synonym for long double to get the size and alignment right. */
3803 if ((t
& VT_BTYPE
) == VT_LONG
) {
3804 #ifdef TCC_TARGET_PE
3805 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
3807 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3815 struct_decl(&type1
, ad
, VT_ENUM
);
3818 type
->ref
= type1
.ref
;
3822 struct_decl(&type1
, ad
, VT_STRUCT
);
3825 /* type modifiers */
3830 parse_btype_qualify(type
, VT_CONSTANT
);
3838 parse_btype_qualify(type
, VT_VOLATILE
);
3845 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == (VT_DEFSIGN
|VT_UNSIGNED
))
3846 tcc_error("signed and unsigned modifier");
3859 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == VT_DEFSIGN
)
3860 tcc_error("signed and unsigned modifier");
3861 t
|= VT_DEFSIGN
| VT_UNSIGNED
;
3886 /* GNUC attribute */
3887 case TOK_ATTRIBUTE1
:
3888 case TOK_ATTRIBUTE2
:
3889 parse_attribute(ad
);
3892 t
= (t
& ~VT_BTYPE
) | u
;
3900 parse_expr_type(&type1
);
3901 /* remove all storage modifiers except typedef */
3902 type1
.t
&= ~(VT_STORAGE
&~VT_TYPEDEF
);
3908 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
3911 type
->t
= ((s
->type
.t
& ~VT_TYPEDEF
) |
3912 (t
& ~(VT_CONSTANT
| VT_VOLATILE
)));
3913 type
->ref
= s
->type
.ref
;
3914 if (t
& (VT_CONSTANT
| VT_VOLATILE
))
3915 parse_btype_qualify(type
, t
& (VT_CONSTANT
| VT_VOLATILE
));
3919 /* get attributes from typedef */
3920 if (0 == ad
->a
.aligned
)
3921 ad
->a
.aligned
= s
->a
.aligned
;
3922 if (0 == ad
->a
.func_call
)
3923 ad
->a
.func_call
= s
->a
.func_call
;
3924 ad
->a
.packed
|= s
->a
.packed
;
3933 if (tcc_state
->char_is_unsigned
) {
3934 if ((t
& (VT_DEFSIGN
|VT_BTYPE
)) == VT_BYTE
)
3938 /* long is never used as type */
3939 if ((t
& VT_BTYPE
) == VT_LONG
)
3940 #if (!defined TCC_TARGET_X86_64 && !defined TCC_TARGET_ARM64) || \
3941 defined TCC_TARGET_PE
3942 t
= (t
& ~VT_BTYPE
) | VT_INT
;
3944 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3950 /* convert a function parameter type (array to pointer and function to
3951 function pointer) */
3952 static inline void convert_parameter_type(CType
*pt
)
3954 /* remove const and volatile qualifiers (XXX: const could be used
3955 to indicate a const function parameter */
3956 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3957 /* array must be transformed to pointer according to ANSI C */
3959 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
3964 ST_FUNC
void parse_asm_str(CString
*astr
)
3967 parse_mult_str(astr
, "string constant");
3970 /* Parse an asm label and return the token */
3971 static int asm_label_instr(void)
3977 parse_asm_str(&astr
);
3980 printf("asm_alias: \"%s\"\n", (char *)astr
.data
);
3982 v
= tok_alloc(astr
.data
, astr
.size
- 1)->tok
;
3987 static void post_type(CType
*type
, AttributeDef
*ad
, int storage
)
3989 int n
, l
, t1
, arg_size
, align
;
3990 Sym
**plast
, *s
, *first
;
3995 /* function declaration */
4003 /* read param name and compute offset */
4004 if (l
!= FUNC_OLD
) {
4005 if (!parse_btype(&pt
, &ad1
)) {
4007 tcc_error("invalid type");
4014 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
4016 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
4017 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
4018 tcc_error("parameter declared as void");
4019 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
4024 expect("identifier");
4028 convert_parameter_type(&pt
);
4029 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
4035 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
4042 /* if no parameters, then old type prototype */
4046 /* NOTE: const is ignored in returned type as it has a special
4047 meaning in gcc / C++ */
4048 type
->t
&= ~VT_CONSTANT
;
4049 /* some ancient pre-K&R C allows a function to return an array
4050 and the array brackets to be put after the arguments, such
4051 that "int c()[]" means something like "int[] c()" */
4054 skip(']'); /* only handle simple "[]" */
4057 /* we push a anonymous symbol which will contain the function prototype */
4058 ad
->a
.func_args
= arg_size
;
4059 s
= sym_push(SYM_FIELD
, type
, 0, l
);
4064 } else if (tok
== '[') {
4065 int saved_nocode_wanted
= nocode_wanted
;
4066 /* array definition */
4068 if (tok
== TOK_RESTRICT1
)
4073 if (!local_stack
|| (storage
& VT_STATIC
))
4074 vpushi(expr_const());
4076 /* VLAs (which can only happen with local_stack && !VT_STATIC)
4077 length must always be evaluated, even under nocode_wanted,
4078 so that its size slot is initialized (e.g. under sizeof
4083 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4086 tcc_error("invalid array size");
4088 if (!is_integer_btype(vtop
->type
.t
& VT_BTYPE
))
4089 tcc_error("size of variable length array should be an integer");
4094 /* parse next post type */
4095 post_type(type
, ad
, storage
);
4096 if (type
->t
== VT_FUNC
)
4097 tcc_error("declaration of an array of functions");
4098 t1
|= type
->t
& VT_VLA
;
4101 loc
-= type_size(&int_type
, &align
);
4105 vla_runtime_type_size(type
, &align
);
4107 vset(&int_type
, VT_LOCAL
|VT_LVAL
, n
);
4113 nocode_wanted
= saved_nocode_wanted
;
4115 /* we push an anonymous symbol which will contain the array
4117 s
= sym_push(SYM_FIELD
, type
, 0, n
);
4118 type
->t
= (t1
? VT_VLA
: VT_ARRAY
) | VT_PTR
;
4123 /* Parse a type declaration (except basic type), and return the type
4124 in 'type'. 'td' is a bitmask indicating which kind of type decl is
4125 expected. 'type' should contain the basic type. 'ad' is the
4126 attribute definition of the basic type. It can be modified by
4129 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
4132 CType type1
, *type2
;
4133 int qualifiers
, storage
;
4135 while (tok
== '*') {
4143 qualifiers
|= VT_CONSTANT
;
4148 qualifiers
|= VT_VOLATILE
;
4154 /* XXX: clarify attribute handling */
4155 case TOK_ATTRIBUTE1
:
4156 case TOK_ATTRIBUTE2
:
4157 parse_attribute(ad
);
4161 type
->t
|= qualifiers
;
4164 /* recursive type */
4165 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
4166 type1
.t
= 0; /* XXX: same as int */
4169 /* XXX: this is not correct to modify 'ad' at this point, but
4170 the syntax is not clear */
4171 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
4172 parse_attribute(ad
);
4173 type_decl(&type1
, ad
, v
, td
);
4176 /* type identifier */
4177 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
4181 if (!(td
& TYPE_ABSTRACT
))
4182 expect("identifier");
4186 storage
= type
->t
& VT_STORAGE
;
4187 type
->t
&= ~VT_STORAGE
;
4188 post_type(type
, ad
, storage
);
4190 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
4191 parse_attribute(ad
);
4195 /* append type at the end of type1 */
4208 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
4209 ST_FUNC
int lvalue_type(int t
)
4214 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
4216 else if (bt
== VT_SHORT
)
4220 if (t
& VT_UNSIGNED
)
4221 r
|= VT_LVAL_UNSIGNED
;
4225 /* indirection with full error checking and bound check */
4226 ST_FUNC
void indir(void)
4228 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
4229 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
4233 if (vtop
->r
& VT_LVAL
)
4235 vtop
->type
= *pointed_type(&vtop
->type
);
4236 /* Arrays and functions are never lvalues */
4237 if (!(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_VLA
)
4238 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
4239 vtop
->r
|= lvalue_type(vtop
->type
.t
);
4240 /* if bound checking, the referenced pointer must be checked */
4241 #ifdef CONFIG_TCC_BCHECK
4242 if (tcc_state
->do_bounds_check
)
4243 vtop
->r
|= VT_MUSTBOUND
;
4248 /* pass a parameter to a function and do type checking and casting */
4249 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
4254 func_type
= func
->c
;
4255 if (func_type
== FUNC_OLD
||
4256 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
4257 /* default casting : only need to convert float to double */
4258 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
4261 } else if (vtop
->type
.t
& VT_BITFIELD
) {
4262 type
.t
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
4263 type
.ref
= vtop
->type
.ref
;
4266 } else if (arg
== NULL
) {
4267 tcc_error("too many arguments to function");
4270 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
4271 gen_assign_cast(&type
);
4275 /* parse an expression of the form '(type)' or '(expr)' and return its
4277 static void parse_expr_type(CType
*type
)
4283 if (parse_btype(type
, &ad
)) {
4284 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
4291 static void parse_type(CType
*type
)
4296 if (!parse_btype(type
, &ad
)) {
4299 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
4302 static void vpush_tokc(int t
)
4307 vsetc(&type
, VT_CONST
, &tokc
);
4310 ST_FUNC
void unary(void)
4312 int n
, t
, align
, size
, r
, sizeof_caller
;
4317 sizeof_caller
= in_sizeof
;
4319 /* XXX: GCC 2.95.3 does not generate a table although it should be
4333 vpush_tokc(VT_INT
| VT_UNSIGNED
);
4337 vpush_tokc(VT_LLONG
);
4341 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
4345 vpush_tokc(VT_FLOAT
);
4349 vpush_tokc(VT_DOUBLE
);
4353 vpush_tokc(VT_LDOUBLE
);
4356 case TOK___FUNCTION__
:
4358 goto tok_identifier
;
4364 /* special function name identifier */
4365 len
= strlen(funcname
) + 1;
4366 /* generate char[len] type */
4371 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
4372 ptr
= section_ptr_add(data_section
, len
);
4373 memcpy(ptr
, funcname
, len
);
4378 #ifdef TCC_TARGET_PE
4379 t
= VT_SHORT
| VT_UNSIGNED
;
4385 /* string parsing */
4388 if (tcc_state
->warn_write_strings
)
4393 memset(&ad
, 0, sizeof(AttributeDef
));
4394 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, 0);
4399 if (parse_btype(&type
, &ad
)) {
4400 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
4402 /* check ISOC99 compound literal */
4404 /* data is allocated locally by default */
4409 /* all except arrays are lvalues */
4410 if (!(type
.t
& VT_ARRAY
))
4411 r
|= lvalue_type(type
.t
);
4412 memset(&ad
, 0, sizeof(AttributeDef
));
4413 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, 0);
4415 if (sizeof_caller
) {
4422 } else if (tok
== '{') {
4423 int saved_nocode_wanted
= nocode_wanted
;
4425 tcc_error("expected constant");
4426 /* save all registers */
4428 /* statement expression : we do not accept break/continue
4429 inside as GCC does. We do retain the nocode_wanted state,
4430 as statement expressions can't ever be entered from the
4431 outside, so any reactivation of code emission (from labels
4432 or loop heads) can be disabled again after the end of it. */
4433 block(NULL
, NULL
, 1);
4434 nocode_wanted
= saved_nocode_wanted
;
4449 /* functions names must be treated as function pointers,
4450 except for unary '&' and sizeof. Since we consider that
4451 functions are not lvalues, we only have to handle it
4452 there and in function calls. */
4453 /* arrays can also be used although they are not lvalues */
4454 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
4455 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
4457 mk_pointer(&vtop
->type
);
4463 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4465 boolean
.t
= VT_BOOL
;
4467 vtop
->c
.i
= !vtop
->c
.i
;
4468 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
4472 vseti(VT_JMP
, gvtst(1, 0));
4484 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
4485 tcc_error("pointer not accepted for unary plus");
4486 /* In order to force cast, we add zero, except for floating point
4487 where we really need an noop (otherwise -0.0 will be transformed
4489 if (!is_float(vtop
->type
.t
)) {
4500 unary_type(&type
); // Perform a in_sizeof = 0;
4501 size
= type_size(&type
, &align
);
4502 if (t
== TOK_SIZEOF
) {
4503 if (!(type
.t
& VT_VLA
)) {
4505 tcc_error("sizeof applied to an incomplete type");
4508 vla_runtime_type_size(&type
, &align
);
4513 vtop
->type
.t
|= VT_UNSIGNED
;
4516 case TOK_builtin_expect
:
4518 /* __builtin_expect is a no-op for now */
4530 case TOK_builtin_types_compatible_p
:
4539 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
4540 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
4541 vpushi(is_compatible_types(&type1
, &type2
));
4544 case TOK_builtin_choose_expr
:
4571 case TOK_builtin_constant_p
:
4578 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
4585 case TOK_builtin_frame_address
:
4586 case TOK_builtin_return_address
:
4593 if (tok
!= TOK_CINT
) {
4594 tcc_error("%s only takes positive integers",
4595 tok1
== TOK_builtin_return_address
?
4596 "__builtin_return_address" :
4597 "__builtin_frame_address");
4599 level
= (uint32_t)tokc
.i
;
4604 vset(&type
, VT_LOCAL
, 0); /* local frame */
4606 mk_pointer(&vtop
->type
);
4607 indir(); /* -> parent frame */
4609 if (tok1
== TOK_builtin_return_address
) {
4610 // assume return address is just above frame pointer on stack
4613 mk_pointer(&vtop
->type
);
4618 #ifdef TCC_TARGET_X86_64
4619 #ifdef TCC_TARGET_PE
4620 case TOK_builtin_va_start
:
4628 if ((vtop
->r
& VT_VALMASK
) != VT_LOCAL
)
4629 tcc_error("__builtin_va_start expects a local variable");
4630 vtop
->r
&= ~(VT_LVAL
| VT_REF
);
4631 vtop
->type
= char_pointer_type
;
4637 case TOK_builtin_va_arg_types
:
4644 vpushi(classify_x86_64_va_arg(&type
));
4650 #ifdef TCC_TARGET_ARM64
4651 case TOK___va_start
: {
4661 vtop
->type
.t
= VT_VOID
;
4664 case TOK___va_arg
: {
4677 case TOK___arm64_clear_cache
: {
4686 vtop
->type
.t
= VT_VOID
;
4690 /* pre operations */
4701 t
= vtop
->type
.t
& VT_BTYPE
;
4703 /* In IEEE negate(x) isn't subtract(0,x), but rather
4707 vtop
->c
.f
= -1.0 * 0.0;
4708 else if (t
== VT_DOUBLE
)
4709 vtop
->c
.d
= -1.0 * 0.0;
4711 vtop
->c
.ld
= -1.0 * 0.0;
4719 goto tok_identifier
;
4721 /* allow to take the address of a label */
4722 if (tok
< TOK_UIDENT
)
4723 expect("label identifier");
4724 s
= label_find(tok
);
4726 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4728 if (s
->r
== LABEL_DECLARED
)
4729 s
->r
= LABEL_FORWARD
;
4732 s
->type
.t
= VT_VOID
;
4733 mk_pointer(&s
->type
);
4734 s
->type
.t
|= VT_STATIC
;
4736 vpushsym(&s
->type
, s
);
4740 // special qnan , snan and infinity values
4742 vpush64(VT_DOUBLE
, 0x7ff8000000000000ULL
);
4746 vpush64(VT_DOUBLE
, 0x7ff0000000000001ULL
);
4750 vpush64(VT_DOUBLE
, 0x7ff0000000000000ULL
);
4759 expect("identifier");
4762 const char *name
= get_tok_str(t
, NULL
);
4764 tcc_error("'%s' undeclared", name
);
4765 /* for simple function calls, we tolerate undeclared
4766 external reference to int() function */
4767 if (tcc_state
->warn_implicit_function_declaration
4768 #ifdef TCC_TARGET_PE
4769 /* people must be warned about using undeclared WINAPI functions
4770 (which usually start with uppercase letter) */
4771 || (name
[0] >= 'A' && name
[0] <= 'Z')
4774 tcc_warning("implicit declaration of function '%s'", name
);
4775 s
= external_global_sym(t
, &func_old_type
, 0);
4779 /* A symbol that has a register is a local register variable,
4780 which starts out as VT_LOCAL value. */
4781 if ((r
& VT_VALMASK
) < VT_CONST
)
4782 r
= (r
& ~VT_VALMASK
) | VT_LOCAL
;
4784 vset(&s
->type
, r
, s
->c
);
4785 /* Point to s as backpointer (even without r&VT_SYM).
4786 Will be used by at least the x86 inline asm parser for
4789 if (vtop
->r
& VT_SYM
) {
4795 /* post operations */
4797 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
4800 } else if (tok
== '.' || tok
== TOK_ARROW
|| tok
== TOK_CDOUBLE
) {
4803 if (tok
== TOK_ARROW
)
4805 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
4808 /* expect pointer on structure */
4809 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
4810 expect("struct or union");
4811 if (tok
== TOK_CDOUBLE
)
4812 expect("field name");
4814 if (tok
== TOK_CINT
|| tok
== TOK_CUINT
)
4815 expect("field name");
4816 s
= find_field(&vtop
->type
, tok
);
4818 tcc_error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, &tokc
));
4819 /* add field offset to pointer */
4820 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
4823 /* change type to field type, and set to lvalue */
4824 vtop
->type
= s
->type
;
4825 vtop
->type
.t
|= qualifiers
;
4826 /* an array is never an lvalue */
4827 if (!(vtop
->type
.t
& VT_ARRAY
)) {
4828 vtop
->r
|= lvalue_type(vtop
->type
.t
);
4829 #ifdef CONFIG_TCC_BCHECK
4830 /* if bound checking, the referenced pointer must be checked */
4831 if (tcc_state
->do_bounds_check
&& (vtop
->r
& VT_VALMASK
) != VT_LOCAL
)
4832 vtop
->r
|= VT_MUSTBOUND
;
4836 } else if (tok
== '[') {
4842 } else if (tok
== '(') {
4845 int nb_args
, ret_nregs
, ret_align
, regsize
, variadic
;
4848 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
4849 /* pointer test (no array accepted) */
4850 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
4851 vtop
->type
= *pointed_type(&vtop
->type
);
4852 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
4856 expect("function pointer");
4859 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
4861 /* get return type */
4864 sa
= s
->next
; /* first parameter */
4865 nb_args
= regsize
= 0;
4867 /* compute first implicit argument if a structure is returned */
4868 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
4869 variadic
= (s
->c
== FUNC_ELLIPSIS
);
4870 ret_nregs
= gfunc_sret(&s
->type
, variadic
, &ret
.type
,
4871 &ret_align
, ®size
);
4873 /* get some space for the returned structure */
4874 size
= type_size(&s
->type
, &align
);
4875 #ifdef TCC_TARGET_ARM64
4876 /* On arm64, a small struct is return in registers.
4877 It is much easier to write it to memory if we know
4878 that we are allowed to write some extra bytes, so
4879 round the allocated space up to a power of 2: */
4881 while (size
& (size
- 1))
4882 size
= (size
| (size
- 1)) + 1;
4884 loc
= (loc
- size
) & -align
;
4886 ret
.r
= VT_LOCAL
| VT_LVAL
;
4887 /* pass it as 'int' to avoid structure arg passing
4889 vseti(VT_LOCAL
, loc
);
4899 /* return in register */
4900 if (is_float(ret
.type
.t
)) {
4901 ret
.r
= reg_fret(ret
.type
.t
);
4902 #ifdef TCC_TARGET_X86_64
4903 if ((ret
.type
.t
& VT_BTYPE
) == VT_QFLOAT
)
4907 #ifndef TCC_TARGET_ARM64
4908 #ifdef TCC_TARGET_X86_64
4909 if ((ret
.type
.t
& VT_BTYPE
) == VT_QLONG
)
4911 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
4922 gfunc_param_typed(s
, sa
);
4932 tcc_error("too few arguments to function");
4934 gfunc_call(nb_args
);
4937 for (r
= ret
.r
+ ret_nregs
+ !ret_nregs
; r
-- > ret
.r
;) {
4938 vsetc(&ret
.type
, r
, &ret
.c
);
4939 vtop
->r2
= ret
.r2
; /* Loop only happens when r2 is VT_CONST */
4942 /* handle packed struct return */
4943 if (((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) && ret_nregs
) {
4946 size
= type_size(&s
->type
, &align
);
4947 /* We're writing whole regs often, make sure there's enough
4948 space. Assume register size is power of 2. */
4949 if (regsize
> align
)
4951 loc
= (loc
- size
) & -align
;
4955 vset(&ret
.type
, VT_LOCAL
| VT_LVAL
, addr
+ offset
);
4959 if (--ret_nregs
== 0)
4963 vset(&s
->type
, VT_LOCAL
| VT_LVAL
, addr
);
4971 ST_FUNC
void expr_prod(void)
4976 while (tok
== '*' || tok
== '/' || tok
== '%') {
4984 ST_FUNC
void expr_sum(void)
4989 while (tok
== '+' || tok
== '-') {
4997 static void expr_shift(void)
5002 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
5010 static void expr_cmp(void)
5015 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
5016 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
5024 static void expr_cmpeq(void)
5029 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
5037 static void expr_and(void)
5040 while (tok
== '&') {
5047 static void expr_xor(void)
5050 while (tok
== '^') {
5057 static void expr_or(void)
5060 while (tok
== '|') {
5067 /* XXX: fix this mess */
5068 static void expr_land_const(void)
5071 while (tok
== TOK_LAND
) {
5077 static void expr_lor_const(void)
5080 while (tok
== TOK_LOR
) {
5087 static void expr_land(void)
5090 if (tok
== TOK_LAND
) {
5093 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
5101 while (tok
== TOK_LAND
) {
5109 gen_cast(&int_type
);
5117 if (tok
!= TOK_LAND
) {
5130 static void expr_lor(void)
5133 if (tok
== TOK_LOR
) {
5136 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
5144 while (tok
== TOK_LOR
) {
5152 gen_cast(&int_type
);
5160 if (tok
!= TOK_LOR
) {
5173 /* Assuming vtop is a value used in a conditional context
5174 (i.e. compared with zero) return 0 if it's false, 1 if
5175 true and -1 if it can't be statically determined. */
5176 static int condition_3way(void)
5179 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
&&
5180 (!(vtop
->r
& VT_SYM
) ||
5181 !(vtop
->sym
->type
.t
& VT_WEAK
))) {
5183 boolean
.t
= VT_BOOL
;
5192 static void expr_cond(void)
5194 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
, islv
, c
, g
;
5196 CType type
, type1
, type2
;
5201 c
= condition_3way();
5202 g
= (tok
== ':' && gnu_ext
);
5204 /* needed to avoid having different registers saved in
5206 if (is_float(vtop
->type
.t
)) {
5208 #ifdef TCC_TARGET_X86_64
5209 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
5234 sv
= *vtop
; /* save value to handle it later */
5235 vtop
--; /* no vpop so that FP stack is not flushed */
5253 bt1
= t1
& VT_BTYPE
;
5255 bt2
= t2
& VT_BTYPE
;
5256 /* cast operands to correct type according to ISOC rules */
5257 if (is_float(bt1
) || is_float(bt2
)) {
5258 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
5259 type
.t
= VT_LDOUBLE
;
5261 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
5266 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
5267 /* cast to biggest op */
5269 /* convert to unsigned if it does not fit in a long long */
5270 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
5271 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
5272 type
.t
|= VT_UNSIGNED
;
5273 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
5274 /* If one is a null ptr constant the result type
5276 if (is_null_pointer (vtop
))
5278 else if (is_null_pointer (&sv
))
5280 /* XXX: test pointer compatibility, C99 has more elaborate
5284 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
5285 /* XXX: test function pointer compatibility */
5286 type
= bt1
== VT_FUNC
? type1
: type2
;
5287 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
5288 /* XXX: test structure compatibility */
5289 type
= bt1
== VT_STRUCT
? type1
: type2
;
5290 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
5291 /* NOTE: as an extension, we accept void on only one side */
5294 /* integer operations */
5296 /* convert to unsigned if it does not fit in an integer */
5297 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
5298 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
5299 type
.t
|= VT_UNSIGNED
;
5301 /* keep structs lvalue by transforming `(expr ? a : b)` to `*(expr ? &a : &b)` so
5302 that `(expr ? a : b).mem` does not error with "lvalue expected" */
5303 islv
= (vtop
->r
& VT_LVAL
) && (sv
.r
& VT_LVAL
) && VT_STRUCT
== (type
.t
& VT_BTYPE
);
5306 /* now we convert second operand */
5310 mk_pointer(&vtop
->type
);
5312 } else if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
5317 if (is_float(type
.t
)) {
5319 #ifdef TCC_TARGET_X86_64
5320 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
5324 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
5325 /* for long longs, we use fixed registers to avoid having
5326 to handle a complicated move */
5337 /* this is horrible, but we must also convert first
5343 mk_pointer(&vtop
->type
);
5345 } else if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
5351 move_reg(r2
, r1
, type
.t
);
5361 static void expr_eq(void)
5367 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
5368 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
5369 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
5384 ST_FUNC
void gexpr(void)
5395 /* parse an expression and return its type without any side effect. */
5396 static void expr_type(CType
*type
)
5406 /* parse a unary expression and return its type without any side
5408 static void unary_type(CType
*type
)
5417 /* parse a constant expression and return value in vtop. */
5418 static void expr_const1(void)
5425 /* parse an integer constant and return its value. */
5426 static inline int64_t expr_const64(void)
5430 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
5431 expect("constant expression");
5437 /* parse an integer constant and return its value.
5438 Complain if it doesn't fit 32bit (signed or unsigned). */
5439 ST_FUNC
int expr_const(void)
5442 int64_t wc
= expr_const64();
5444 if (c
!= wc
&& (unsigned)c
!= wc
)
5445 tcc_error("constant exceeds 32 bit");
5449 /* return the label token if current token is a label, otherwise
5451 static int is_label(void)
5455 /* fast test first */
5456 if (tok
< TOK_UIDENT
)
5458 /* no need to save tokc because tok is an identifier */
5465 unget_tok(last_tok
);
5470 static void label_or_decl(int l
)
5474 /* fast test first */
5475 if (tok
>= TOK_UIDENT
)
5477 /* no need to save tokc because tok is an identifier */
5481 unget_tok(last_tok
);
5484 unget_tok(last_tok
);
5489 #ifndef TCC_TARGET_ARM64
5490 static void gfunc_return(CType
*func_type
)
5492 if ((func_type
->t
& VT_BTYPE
) == VT_STRUCT
) {
5493 CType type
, ret_type
;
5494 int ret_align
, ret_nregs
, regsize
;
5495 ret_nregs
= gfunc_sret(func_type
, func_var
, &ret_type
,
5496 &ret_align
, ®size
);
5497 if (0 == ret_nregs
) {
5498 /* if returning structure, must copy it to implicit
5499 first pointer arg location */
5502 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
5505 /* copy structure value to pointer */
5508 /* returning structure packed into registers */
5509 int r
, size
, addr
, align
;
5510 size
= type_size(func_type
,&align
);
5511 if ((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) ||
5512 (vtop
->c
.i
& (ret_align
-1)))
5513 && (align
& (ret_align
-1))) {
5514 loc
= (loc
- size
) & -ret_align
;
5517 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
5521 vset(&ret_type
, VT_LOCAL
| VT_LVAL
, addr
);
5523 vtop
->type
= ret_type
;
5524 if (is_float(ret_type
.t
))
5525 r
= rc_fret(ret_type
.t
);
5536 if (--ret_nregs
== 0)
5538 /* We assume that when a structure is returned in multiple
5539 registers, their classes are consecutive values of the
5542 vtop
->c
.i
+= regsize
;
5546 } else if (is_float(func_type
->t
)) {
5547 gv(rc_fret(func_type
->t
));
5551 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
5555 static int case_cmp(const void *pa
, const void *pb
)
5557 int64_t a
= (*(struct case_t
**) pa
)->v1
;
5558 int64_t b
= (*(struct case_t
**) pb
)->v1
;
5559 return a
< b
? -1 : a
> b
;
5562 static void gcase(struct case_t
**base
, int len
, int *bsym
)
5566 int ll
= (vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
;
5584 gtst_addr(0, p
->sym
); /* v1 <= x <= v2 */
5586 gcase(base
, len
/2, bsym
);
5587 if (cur_switch
->def_sym
)
5588 gjmp_addr(cur_switch
->def_sym
);
5590 *bsym
= gjmp(*bsym
);
5594 base
+= e
; len
-= e
;
5604 if (p
->v1
== p
->v2
) {
5606 gtst_addr(0, p
->sym
);
5616 gtst_addr(0, p
->sym
);
5622 static void block(int *bsym
, int *csym
, int is_expr
)
5624 int a
, b
, c
, d
, cond
;
5627 /* generate line number info */
5628 if (tcc_state
->do_debug
)
5629 tcc_debug_line(tcc_state
);
5632 /* default return value is (void) */
5634 vtop
->type
.t
= VT_VOID
;
5637 if (tok
== TOK_IF
) {
5639 int saved_nocode_wanted
= nocode_wanted
;
5644 cond
= condition_3way();
5650 nocode_wanted
|= 0x20000000;
5651 block(bsym
, csym
, 0);
5653 nocode_wanted
= saved_nocode_wanted
;
5655 if (c
== TOK_ELSE
) {
5660 nocode_wanted
|= 0x20000000;
5661 block(bsym
, csym
, 0);
5662 gsym(d
); /* patch else jmp */
5664 nocode_wanted
= saved_nocode_wanted
;
5667 } else if (tok
== TOK_WHILE
) {
5668 int saved_nocode_wanted
;
5669 nocode_wanted
&= ~0x20000000;
5679 saved_nocode_wanted
= nocode_wanted
;
5681 nocode_wanted
= saved_nocode_wanted
;
5686 } else if (tok
== '{') {
5688 int block_vla_sp_loc
= vla_sp_loc
, saved_vlas_in_scope
= vlas_in_scope
;
5691 /* record local declaration stack position */
5693 llabel
= local_label_stack
;
5696 /* handle local labels declarations */
5697 if (tok
== TOK_LABEL
) {
5700 if (tok
< TOK_UIDENT
)
5701 expect("label identifier");
5702 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
5712 while (tok
!= '}') {
5713 label_or_decl(VT_LOCAL
);
5717 block(bsym
, csym
, is_expr
);
5720 /* pop locally defined labels */
5721 label_pop(&local_label_stack
, llabel
);
5722 /* pop locally defined symbols */
5724 /* In the is_expr case (a statement expression is finished here),
5725 vtop might refer to symbols on the local_stack. Either via the
5726 type or via vtop->sym. We can't pop those nor any that in turn
5727 might be referred to. To make it easier we don't roll back
5728 any symbols in that case; some upper level call to block() will
5729 do that. We do have to remove such symbols from the lookup
5730 tables, though. sym_pop will do that. */
5731 sym_pop(&local_stack
, s
, is_expr
);
5733 /* Pop VLA frames and restore stack pointer if required */
5734 if (vlas_in_scope
> saved_vlas_in_scope
) {
5735 vla_sp_loc
= saved_vlas_in_scope
? block_vla_sp_loc
: vla_sp_root_loc
;
5738 vlas_in_scope
= saved_vlas_in_scope
;
5741 } else if (tok
== TOK_RETURN
) {
5745 gen_assign_cast(&func_vt
);
5746 gfunc_return(&func_vt
);
5749 /* jump unless last stmt in top-level block */
5750 if (tok
!= '}' || local_scope
!= 1)
5752 nocode_wanted
|= 0x20000000;
5753 } else if (tok
== TOK_BREAK
) {
5756 tcc_error("cannot break");
5757 *bsym
= gjmp(*bsym
);
5760 nocode_wanted
|= 0x20000000;
5761 } else if (tok
== TOK_CONTINUE
) {
5764 tcc_error("cannot continue");
5765 vla_sp_restore_root();
5766 *csym
= gjmp(*csym
);
5769 } else if (tok
== TOK_FOR
) {
5771 int saved_nocode_wanted
;
5772 nocode_wanted
&= ~0x20000000;
5778 /* c99 for-loop init decl? */
5779 if (!decl0(VT_LOCAL
, 1)) {
5780 /* no, regular for-loop init expr */
5806 saved_nocode_wanted
= nocode_wanted
;
5808 nocode_wanted
= saved_nocode_wanted
;
5813 sym_pop(&local_stack
, s
, 0);
5816 if (tok
== TOK_DO
) {
5817 int saved_nocode_wanted
;
5818 nocode_wanted
&= ~0x20000000;
5824 saved_nocode_wanted
= nocode_wanted
;
5832 nocode_wanted
= saved_nocode_wanted
;
5837 if (tok
== TOK_SWITCH
) {
5838 struct switch_t
*saved
, sw
;
5839 int saved_nocode_wanted
= nocode_wanted
;
5845 switchval
= *vtop
--;
5847 b
= gjmp(0); /* jump to first case */
5848 sw
.p
= NULL
; sw
.n
= 0; sw
.def_sym
= 0;
5852 nocode_wanted
= saved_nocode_wanted
;
5853 a
= gjmp(a
); /* add implicit break */
5856 qsort(sw
.p
, sw
.n
, sizeof(void*), case_cmp
);
5857 for (b
= 1; b
< sw
.n
; b
++)
5858 if (sw
.p
[b
- 1]->v2
>= sw
.p
[b
]->v1
)
5859 tcc_error("duplicate case value");
5860 /* Our switch table sorting is signed, so the compared
5861 value needs to be as well when it's 64bit. */
5862 if ((switchval
.type
.t
& VT_BTYPE
) == VT_LLONG
)
5863 switchval
.type
.t
&= ~VT_UNSIGNED
;
5865 gcase(sw
.p
, sw
.n
, &a
);
5868 gjmp_addr(sw
.def_sym
);
5869 dynarray_reset(&sw
.p
, &sw
.n
);
5874 if (tok
== TOK_CASE
) {
5875 struct case_t
*cr
= tcc_malloc(sizeof(struct case_t
));
5878 nocode_wanted
&= ~0x20000000;
5880 cr
->v1
= cr
->v2
= expr_const64();
5881 if (gnu_ext
&& tok
== TOK_DOTS
) {
5883 cr
->v2
= expr_const64();
5884 if (cr
->v2
< cr
->v1
)
5885 tcc_warning("empty case range");
5888 dynarray_add(&cur_switch
->p
, &cur_switch
->n
, cr
);
5891 goto block_after_label
;
5893 if (tok
== TOK_DEFAULT
) {
5898 if (cur_switch
->def_sym
)
5899 tcc_error("too many 'default'");
5900 cur_switch
->def_sym
= ind
;
5902 goto block_after_label
;
5904 if (tok
== TOK_GOTO
) {
5906 if (tok
== '*' && gnu_ext
) {
5910 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
5913 } else if (tok
>= TOK_UIDENT
) {
5914 s
= label_find(tok
);
5915 /* put forward definition if needed */
5917 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
5919 if (s
->r
== LABEL_DECLARED
)
5920 s
->r
= LABEL_FORWARD
;
5922 vla_sp_restore_root();
5923 if (s
->r
& LABEL_FORWARD
)
5924 s
->jnext
= gjmp(s
->jnext
);
5926 gjmp_addr(s
->jnext
);
5929 expect("label identifier");
5932 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
5940 if (s
->r
== LABEL_DEFINED
)
5941 tcc_error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
5943 s
->r
= LABEL_DEFINED
;
5945 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
5949 /* we accept this, but it is a mistake */
5951 nocode_wanted
&= ~0x20000000;
5953 tcc_warning("deprecated use of label at end of compound statement");
5957 block(bsym
, csym
, is_expr
);
5960 /* expression case */
5975 #define EXPR_CONST 1
5978 static void parse_init_elem(int expr_type
)
5980 int saved_global_expr
;
5983 /* compound literals must be allocated globally in this case */
5984 saved_global_expr
= global_expr
;
5987 global_expr
= saved_global_expr
;
5988 /* NOTE: symbols are accepted */
5989 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
5990 tcc_error("initializer element is not constant");
5998 /* t is the array or struct type. c is the array or struct
5999 address. cur_field is the pointer to the current
6000 value, for arrays the 'c' member contains the current start
6001 index and the 'r' contains the end index (in case of range init).
6002 'size_only' is true if only size info is needed (only used
6004 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
6005 Sym
**cur_field
, int size_only
)
6008 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
6014 if (gnu_ext
&& (l
= is_label()) != 0)
6016 while (tok
== '[' || tok
== '.') {
6018 if (!(type
->t
& VT_ARRAY
))
6019 expect("array type");
6022 index
= expr_const();
6023 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
6024 tcc_error("invalid index");
6025 if (tok
== TOK_DOTS
&& gnu_ext
) {
6027 index_last
= expr_const();
6028 if (index_last
< 0 ||
6029 (s
->c
>= 0 && index_last
>= s
->c
) ||
6031 tcc_error("invalid index");
6037 (*cur_field
)->c
= index
;
6038 (*cur_field
)->r
= index_last
;
6040 type
= pointed_type(type
);
6041 elem_size
= type_size(type
, &align
);
6042 c
+= index
* elem_size
;
6043 /* NOTE: we only support ranges for last designator */
6044 nb_elems
= index_last
- index
+ 1;
6045 if (nb_elems
!= 1) {
6054 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
6055 expect("struct/union type");
6056 f
= find_field(type
, l
);
6061 /* XXX: fix this mess by using explicit storage field */
6063 type1
.t
|= (type
->t
& ~VT_TYPE
);
6077 if (type
->t
& VT_ARRAY
) {
6078 index
= (*cur_field
)->c
;
6079 if (type
->ref
->c
>= 0 && index
>= type
->ref
->c
)
6080 tcc_error("index too large");
6081 type
= pointed_type(type
);
6082 c
+= index
* type_size(type
, &align
);
6086 tcc_error("too many field init");
6087 /* XXX: fix this mess by using explicit storage field */
6089 type1
.t
|= (type
->t
& ~VT_TYPE
);
6094 decl_initializer(type
, sec
, c
, 0, size_only
);
6096 /* XXX: make it more general */
6097 if (!size_only
&& nb_elems
> 1) {
6098 unsigned long c_end
;
6103 vset(type
, VT_LOCAL
|VT_LVAL
, c
);
6104 for (i
= 1; i
< nb_elems
; i
++) {
6105 vset(type
, VT_LOCAL
|VT_LVAL
, c
+ elem_size
* i
);
6111 c_end
= c
+ nb_elems
* elem_size
;
6112 if (c_end
> sec
->data_allocated
)
6113 section_realloc(sec
, c_end
);
6114 src
= sec
->data
+ c
;
6116 for(i
= 1; i
< nb_elems
; i
++) {
6118 memcpy(dst
, src
, elem_size
);
6124 /* store a value or an expression directly in global data or in local array */
6125 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
)
6127 int bt
, bit_pos
, bit_size
;
6129 unsigned long long bit_mask
;
6133 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
6137 /* XXX: not portable */
6138 /* XXX: generate error if incorrect relocation */
6139 gen_assign_cast(&dtype
);
6140 bt
= type
->t
& VT_BTYPE
;
6141 size
= type_size(type
, &align
);
6142 if (c
+ size
> sec
->data_allocated
) {
6143 section_realloc(sec
, c
+ size
);
6145 ptr
= sec
->data
+ c
;
6146 /* XXX: make code faster ? */
6147 if (!(type
->t
& VT_BITFIELD
)) {
6149 bit_size
= PTR_SIZE
* 8;
6152 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
6153 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
6154 bit_mask
= (1LL << bit_size
) - 1;
6156 if ((vtop
->r
& (VT_SYM
|VT_CONST
)) == (VT_SYM
|VT_CONST
) &&
6157 vtop
->sym
->v
>= SYM_FIRST_ANOM
&&
6158 /* XXX This rejects compount literals like
6159 '(void *){ptr}'. The problem is that '&sym' is
6160 represented the same way, which would be ruled out
6161 by the SYM_FIRST_ANOM check above, but also '"string"'
6162 in 'char *p = "string"' is represented the same
6163 with the type being VT_PTR and the symbol being an
6164 anonymous one. That is, there's no difference in vtop
6165 between '(void *){x}' and '&(void *){x}'. Ignore
6166 pointer typed entities here. Hopefully no real code
6167 will every use compound literals with scalar type. */
6168 (vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
6169 /* These come from compound literals, memcpy stuff over. */
6173 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[vtop
->sym
->c
];
6174 ssec
= tcc_state
->sections
[esym
->st_shndx
];
6175 memmove (ptr
, ssec
->data
+ esym
->st_value
, size
);
6177 /* We need to copy over all memory contents, and that
6178 includes relocations. Use the fact that relocs are
6179 created it order, so look from the end of relocs
6180 until we hit one before the copied region. */
6181 int num_relocs
= ssec
->reloc
->data_offset
/ sizeof(*rel
);
6182 rel
= (ElfW_Rel
*)(ssec
->reloc
->data
+ ssec
->reloc
->data_offset
);
6183 while (num_relocs
--) {
6185 if (rel
->r_offset
>= esym
->st_value
+ size
)
6187 if (rel
->r_offset
< esym
->st_value
)
6189 /* Note: if the same fields are initialized multiple
6190 times (possible with designators) then we possibly
6191 add multiple relocations for the same offset here.
6192 That would lead to wrong code, the last reloc needs
6193 to win. We clean this up later after the whole
6194 initializer is parsed. */
6195 put_elf_reloca(symtab_section
, sec
,
6196 c
+ rel
->r_offset
- esym
->st_value
,
6197 ELFW(R_TYPE
)(rel
->r_info
),
6198 ELFW(R_SYM
)(rel
->r_info
),
6199 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
6208 if ((vtop
->r
& VT_SYM
) &&
6214 (bt
== VT_LLONG
&& bit_size
!= 64) ||
6218 (bt
== VT_INT
&& bit_size
!= 32)
6221 tcc_error("initializer element is not computable at load time");
6223 /* XXX: when cross-compiling we assume that each type has the
6224 same representation on host and target, which is likely to
6225 be wrong in the case of long double */
6227 vtop
->c
.i
= (vtop
->c
.i
!= 0);
6229 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
6232 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
6235 *(double *)ptr
= vtop
->c
.d
;
6238 if (sizeof(long double) == LDOUBLE_SIZE
)
6239 *(long double *)ptr
= vtop
->c
.ld
;
6240 else if (sizeof(double) == LDOUBLE_SIZE
)
6241 *(double *)ptr
= vtop
->c
.ld
;
6243 tcc_error("can't cross compile long double constants");
6247 *(long long *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
6254 addr_t val
= (vtop
->c
.i
& bit_mask
) << bit_pos
;
6255 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
6256 if (vtop
->r
& VT_SYM
)
6257 greloca(sec
, vtop
->sym
, c
, R_DATA_PTR
, val
);
6259 *(addr_t
*)ptr
|= val
;
6261 if (vtop
->r
& VT_SYM
)
6262 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
6263 *(addr_t
*)ptr
|= val
;
6269 int val
= (vtop
->c
.i
& bit_mask
) << bit_pos
;
6270 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
6271 if (vtop
->r
& VT_SYM
)
6272 greloca(sec
, vtop
->sym
, c
, R_DATA_PTR
, val
);
6276 if (vtop
->r
& VT_SYM
)
6277 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
6286 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
6293 /* put zeros for variable based init */
6294 static void init_putz(Section
*sec
, unsigned long c
, int size
)
6297 /* nothing to do because globals are already set to zero */
6299 vpush_global_sym(&func_old_type
, TOK_memset
);
6301 #ifdef TCC_TARGET_ARM
6312 /* 't' contains the type and storage info. 'c' is the offset of the
6313 object in section 'sec'. If 'sec' is NULL, it means stack based
6314 allocation. 'first' is true if array '{' must be read (multi
6315 dimension implicit array init handling). 'size_only' is true if
6316 size only evaluation is wanted (only for arrays). */
6317 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
6318 int first
, int size_only
)
6320 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, parlevel1
, i
;
6327 /* If we currently are at an '}' or ',' we have read an initializer
6328 element in one of our callers, and not yet consumed it. */
6329 have_elem
= tok
== '}' || tok
== ',';
6330 if (!have_elem
&& tok
!= '{' &&
6331 /* In case of strings we have special handling for arrays, so
6332 don't consume them as initializer value (which would commit them
6333 to some anonymous symbol). */
6334 tok
!= TOK_LSTR
&& tok
!= TOK_STR
&&
6336 parse_init_elem(!sec
? EXPR_ANY
: EXPR_CONST
);
6341 !(type
->t
& VT_ARRAY
) &&
6342 /* Use i_c_parameter_t, to strip toplevel qualifiers.
6343 The source type might have VT_CONSTANT set, which is
6344 of course assignable to non-const elements. */
6345 is_compatible_parameter_types(type
, &vtop
->type
)) {
6346 init_putv(type
, sec
, c
);
6347 } else if (type
->t
& VT_ARRAY
) {
6351 t1
= pointed_type(type
);
6352 size1
= type_size(t1
, &align1
);
6355 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
6358 tcc_error("character array initializer must be a literal,"
6359 " optionally enclosed in braces");
6364 /* only parse strings here if correct type (otherwise: handle
6365 them as ((w)char *) expressions */
6366 if ((tok
== TOK_LSTR
&&
6367 #ifdef TCC_TARGET_PE
6368 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
6370 (t1
->t
& VT_BTYPE
) == VT_INT
6372 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
6373 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
6376 /* compute maximum number of chars wanted */
6378 cstr_len
= tokc
.str
.size
;
6380 cstr_len
= tokc
.str
.size
/ sizeof(nwchar_t
);
6383 if (n
>= 0 && nb
> (n
- array_length
))
6384 nb
= n
- array_length
;
6387 tcc_warning("initializer-string for array is too long");
6388 /* in order to go faster for common case (char
6389 string in global variable, we handle it
6391 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
6392 memcpy(sec
->data
+ c
+ array_length
, tokc
.str
.data
, nb
);
6396 ch
= ((unsigned char *)tokc
.str
.data
)[i
];
6398 ch
= ((nwchar_t
*)tokc
.str
.data
)[i
];
6400 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
);
6407 /* only add trailing zero if enough storage (no
6408 warning in this case since it is standard) */
6409 if (n
< 0 || array_length
< n
) {
6412 init_putv(t1
, sec
, c
+ (array_length
* size1
));
6422 while (tok
!= '}' || have_elem
) {
6423 decl_designator(type
, sec
, c
, &f
, size_only
);
6426 /* must put zero in holes (note that doing it that way
6427 ensures that it even works with designators) */
6428 if (!size_only
&& array_length
< index
) {
6429 init_putz(sec
, c
+ array_length
* size1
,
6430 (index
- array_length
) * size1
);
6432 if (type
->t
& VT_ARRAY
) {
6433 index
= indexsym
.c
= ++indexsym
.r
;
6435 index
= index
+ type_size(&f
->type
, &align1
);
6436 if (s
->type
.t
== TOK_UNION
)
6441 if (index
> array_length
)
6442 array_length
= index
;
6444 if (type
->t
& VT_ARRAY
) {
6445 /* special test for multi dimensional arrays (may not
6446 be strictly correct if designators are used at the
6448 if (no_oblock
&& index
>= n
)
6451 if (no_oblock
&& f
== NULL
)
6459 /* put zeros at the end */
6460 if (!size_only
&& array_length
< n
) {
6461 init_putz(sec
, c
+ array_length
* size1
,
6462 (n
- array_length
) * size1
);
6466 /* patch type size if needed, which happens only for array types */
6468 s
->c
= array_length
;
6469 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
6472 if (first
|| tok
== '{') {
6481 } else if (tok
== '{') {
6483 decl_initializer(type
, sec
, c
, first
, size_only
);
6485 } else if (size_only
) {
6486 /* If we supported only ISO C we wouldn't have to accept calling
6487 this on anything than an array size_only==1 (and even then
6488 only on the outermost level, so no recursion would be needed),
6489 because initializing a flex array member isn't supported.
6490 But GNU C supports it, so we need to recurse even into
6491 subfields of structs and arrays when size_only is set. */
6492 /* just skip expression */
6493 parlevel
= parlevel1
= 0;
6494 while ((parlevel
> 0 || parlevel1
> 0 ||
6495 (tok
!= '}' && tok
!= ',')) && tok
!= -1) {
6498 else if (tok
== ')') {
6499 if (parlevel
== 0 && parlevel1
== 0)
6503 else if (tok
== '{')
6505 else if (tok
== '}') {
6506 if (parlevel
== 0 && parlevel1
== 0)
6514 /* This should happen only when we haven't parsed
6515 the init element above for fear of committing a
6516 string constant to memory too early. */
6517 if (tok
!= TOK_STR
&& tok
!= TOK_LSTR
)
6518 expect("string constant");
6519 parse_init_elem(!sec
? EXPR_ANY
: EXPR_CONST
);
6521 init_putv(type
, sec
, c
);
6525 /* parse an initializer for type 't' if 'has_init' is non zero, and
6526 allocate space in local or global data space ('r' is either
6527 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
6528 variable 'v' of scope 'scope' is declared before initializers
6529 are parsed. If 'v' is zero, then a reference to the new object
6530 is put in the value stack. If 'has_init' is 2, a special parsing
6531 is done to handle string constants. */
6532 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
6533 int has_init
, int v
, int scope
)
6535 int size
, align
, addr
, data_offset
;
6537 ParseState saved_parse_state
= {0};
6538 TokenString
*init_str
= NULL
;
6540 Sym
*flexible_array
;
6542 flexible_array
= NULL
;
6543 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
6544 Sym
*field
= type
->ref
->next
;
6547 field
= field
->next
;
6548 if (field
->type
.t
& VT_ARRAY
&& field
->type
.ref
->c
< 0)
6549 flexible_array
= field
;
6553 size
= type_size(type
, &align
);
6554 /* If unknown size, we must evaluate it before
6555 evaluating initializers because
6556 initializers can generate global data too
6557 (e.g. string pointers or ISOC99 compound
6558 literals). It also simplifies local
6559 initializers handling */
6560 if (size
< 0 || (flexible_array
&& has_init
)) {
6562 tcc_error("unknown type size");
6563 /* get all init string */
6564 init_str
= tok_str_alloc();
6565 if (has_init
== 2) {
6566 /* only get strings */
6567 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
6568 tok_str_add_tok(init_str
);
6573 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
6575 tcc_error("unexpected end of file in initializer");
6576 tok_str_add_tok(init_str
);
6579 else if (tok
== '}') {
6589 tok_str_add(init_str
, -1);
6590 tok_str_add(init_str
, 0);
6593 save_parse_state(&saved_parse_state
);
6595 begin_macro(init_str
, 1);
6597 decl_initializer(type
, NULL
, 0, 1, 1);
6598 /* prepare second initializer parsing */
6599 macro_ptr
= init_str
->str
;
6602 /* if still unknown size, error */
6603 size
= type_size(type
, &align
);
6605 tcc_error("unknown type size");
6607 /* If there's a flex member and it was used in the initializer
6609 if (flexible_array
&&
6610 flexible_array
->type
.ref
->c
> 0)
6611 size
+= flexible_array
->type
.ref
->c
6612 * pointed_size(&flexible_array
->type
);
6613 /* take into account specified alignment if bigger */
6614 if (ad
->a
.aligned
) {
6615 int speca
= 1 << (ad
->a
.aligned
- 1);
6618 } else if (ad
->a
.packed
) {
6621 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
6623 #ifdef CONFIG_TCC_BCHECK
6624 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
6628 loc
= (loc
- size
) & -align
;
6630 #ifdef CONFIG_TCC_BCHECK
6631 /* handles bounds */
6632 /* XXX: currently, since we do only one pass, we cannot track
6633 '&' operators, so we add only arrays */
6634 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
6636 /* add padding between regions */
6638 /* then add local bound info */
6639 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(addr_t
));
6640 bounds_ptr
[0] = addr
;
6641 bounds_ptr
[1] = size
;
6645 /* local variable */
6646 #ifdef CONFIG_TCC_ASM
6647 if (ad
->asm_label
) {
6648 int reg
= asm_parse_regvar(ad
->asm_label
);
6650 r
= (r
& ~VT_VALMASK
) | reg
;
6653 sym_push(v
, type
, r
, addr
);
6655 /* push local reference */
6656 vset(type
, r
, addr
);
6662 if (v
&& scope
== VT_CONST
) {
6663 /* see if the symbol was already defined */
6666 if (!is_compatible_types(&sym
->type
, type
))
6667 tcc_error("incompatible types for redefinition of '%s'",
6668 get_tok_str(v
, NULL
));
6669 if (sym
->type
.t
& VT_EXTERN
) {
6670 /* if the variable is extern, it was not allocated */
6671 sym
->type
.t
&= ~VT_EXTERN
;
6672 /* set array size if it was omitted in extern
6674 if ((sym
->type
.t
& VT_ARRAY
) &&
6675 sym
->type
.ref
->c
< 0 &&
6677 sym
->type
.ref
->c
= type
->ref
->c
;
6679 /* we accept several definitions of the same
6680 global variable. this is tricky, because we
6681 must play with the SHN_COMMON type of the symbol */
6682 /* XXX: should check if the variable was already
6683 initialized. It is incorrect to initialized it
6685 /* no init data, we won't add more to the symbol */
6692 /* allocate symbol in corresponding section */
6697 else if (tcc_state
->nocommon
)
6701 data_offset
= sec
->data_offset
;
6702 data_offset
= (data_offset
+ align
- 1) & -align
;
6704 /* very important to increment global pointer at this time
6705 because initializers themselves can create new initializers */
6706 data_offset
+= size
;
6707 #ifdef CONFIG_TCC_BCHECK
6708 /* add padding if bound check */
6709 if (tcc_state
->do_bounds_check
)
6712 sec
->data_offset
= data_offset
;
6713 /* allocate section space to put the data */
6714 if (sec
->sh_type
!= SHT_NOBITS
&&
6715 data_offset
> sec
->data_allocated
)
6716 section_realloc(sec
, data_offset
);
6717 /* align section if needed */
6718 if (align
> sec
->sh_addralign
)
6719 sec
->sh_addralign
= align
;
6721 addr
= 0; /* avoid warning */
6725 if (scope
!= VT_CONST
|| !sym
) {
6726 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
6727 sym
->asm_label
= ad
->asm_label
;
6729 /* update symbol definition */
6731 put_extern_sym(sym
, sec
, addr
, size
);
6734 /* put a common area */
6735 put_extern_sym(sym
, NULL
, align
, size
);
6736 /* XXX: find a nicer way */
6737 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
6738 esym
->st_shndx
= SHN_COMMON
;
6741 /* push global reference */
6742 sym
= get_sym_ref(type
, sec
, addr
, size
);
6743 vpushsym(type
, sym
);
6745 /* patch symbol weakness */
6746 if (type
->t
& VT_WEAK
)
6748 apply_visibility(sym
, type
);
6749 #ifdef CONFIG_TCC_BCHECK
6750 /* handles bounds now because the symbol must be defined
6751 before for the relocation */
6752 if (tcc_state
->do_bounds_check
) {
6755 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
6756 /* then add global bound info */
6757 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(addr_t
));
6758 bounds_ptr
[0] = 0; /* relocated */
6759 bounds_ptr
[1] = size
;
6763 if (type
->t
& VT_VLA
) {
6766 /* save current stack pointer */
6767 if (vlas_in_scope
== 0) {
6768 if (vla_sp_root_loc
== -1)
6769 vla_sp_root_loc
= (loc
-= PTR_SIZE
);
6770 gen_vla_sp_save(vla_sp_root_loc
);
6773 vla_runtime_type_size(type
, &a
);
6774 gen_vla_alloc(type
, a
);
6775 gen_vla_sp_save(addr
);
6778 } else if (has_init
) {
6779 size_t oldreloc_offset
= 0;
6780 if (sec
&& sec
->reloc
)
6781 oldreloc_offset
= sec
->reloc
->data_offset
;
6782 decl_initializer(type
, sec
, addr
, 1, 0);
6783 if (sec
&& sec
->reloc
)
6784 squeeze_multi_relocs(sec
, oldreloc_offset
);
6785 /* patch flexible array member size back to -1, */
6786 /* for possible subsequent similar declarations */
6788 flexible_array
->type
.ref
->c
= -1;
6791 /* restore parse state if needed */
6794 restore_parse_state(&saved_parse_state
);
6798 /* parse an old style function declaration list */
6799 /* XXX: check multiple parameter */
6800 static void func_decl_list(Sym
*func_sym
)
6807 /* parse each declaration */
6808 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
&&
6809 tok
!= TOK_ASM1
&& tok
!= TOK_ASM2
&& tok
!= TOK_ASM3
) {
6810 if (!parse_btype(&btype
, &ad
))
6811 expect("declaration list");
6812 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
6813 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
6815 /* we accept no variable after */
6819 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
6820 /* find parameter in function parameter list */
6823 if ((s
->v
& ~SYM_FIELD
) == v
)
6827 tcc_error("declaration for parameter '%s' but no such parameter",
6828 get_tok_str(v
, NULL
));
6830 /* check that no storage specifier except 'register' was given */
6831 if (type
.t
& VT_STORAGE
)
6832 tcc_error("storage class specified for '%s'", get_tok_str(v
, NULL
));
6833 convert_parameter_type(&type
);
6834 /* we can add the type (NOTE: it could be local to the function) */
6836 /* accept other parameters */
6847 /* parse a function defined by symbol 'sym' and generate its code in
6848 'cur_text_section' */
6849 static void gen_function(Sym
*sym
)
6852 ind
= cur_text_section
->data_offset
;
6853 /* NOTE: we patch the symbol size later */
6854 put_extern_sym(sym
, cur_text_section
, ind
, 0);
6855 funcname
= get_tok_str(sym
->v
, NULL
);
6857 /* Initialize VLA state */
6859 vla_sp_root_loc
= -1;
6860 /* put debug symbol */
6861 tcc_debug_funcstart(tcc_state
, sym
);
6862 /* push a dummy symbol to enable local sym storage */
6863 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
6864 local_scope
= 1; /* for function parameters */
6865 gfunc_prolog(&sym
->type
);
6868 block(NULL
, NULL
, 0);
6872 cur_text_section
->data_offset
= ind
;
6873 label_pop(&global_label_stack
, NULL
);
6874 /* reset local stack */
6876 sym_pop(&local_stack
, NULL
, 0);
6877 /* end of function */
6878 /* patch symbol size */
6879 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
6881 /* patch symbol weakness (this definition overrules any prototype) */
6882 if (sym
->type
.t
& VT_WEAK
)
6884 apply_visibility(sym
, &sym
->type
);
6885 tcc_debug_funcend(tcc_state
, ind
- func_ind
);
6886 /* It's better to crash than to generate wrong code */
6887 cur_text_section
= NULL
;
6888 funcname
= ""; /* for safety */
6889 func_vt
.t
= VT_VOID
; /* for safety */
6890 func_var
= 0; /* for safety */
6891 ind
= 0; /* for safety */
6896 static void gen_inline_functions(TCCState
*s
)
6899 int inline_generated
, i
, ln
;
6900 struct InlineFunc
*fn
;
6902 ln
= file
->line_num
;
6903 /* iterate while inline function are referenced */
6905 inline_generated
= 0;
6906 for (i
= 0; i
< s
->nb_inline_fns
; ++i
) {
6907 fn
= s
->inline_fns
[i
];
6909 if (sym
&& sym
->c
) {
6910 /* the function was used: generate its code and
6911 convert it to a normal function */
6914 pstrcpy(file
->filename
, sizeof file
->filename
, fn
->filename
);
6915 sym
->type
.t
&= ~VT_INLINE
;
6917 begin_macro(fn
->func_str
, 1);
6919 cur_text_section
= text_section
;
6923 inline_generated
= 1;
6926 if (!inline_generated
)
6929 file
->line_num
= ln
;
6932 ST_FUNC
void free_inline_functions(TCCState
*s
)
6935 /* free tokens of unused inline functions */
6936 for (i
= 0; i
< s
->nb_inline_fns
; ++i
) {
6937 struct InlineFunc
*fn
= s
->inline_fns
[i
];
6939 tok_str_free(fn
->func_str
);
6941 dynarray_reset(&s
->inline_fns
, &s
->nb_inline_fns
);
6944 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
6945 static int decl0(int l
, int is_for_loop_init
)
6953 if (!parse_btype(&btype
, &ad
)) {
6954 if (is_for_loop_init
)
6956 /* skip redundant ';' */
6957 /* XXX: find more elegant solution */
6962 if (l
== VT_CONST
&&
6963 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
6964 /* global asm block */
6968 /* special test for old K&R protos without explicit int
6969 type. Only accepted when defining global data */
6970 if (l
== VT_LOCAL
|| tok
< TOK_UIDENT
)
6974 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
6975 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
6977 if ((btype
.t
& VT_BTYPE
) == VT_STRUCT
) {
6978 int v
= btype
.ref
->v
;
6979 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) >= SYM_FIRST_ANOM
)
6980 tcc_warning("unnamed struct/union that defines no instances");
6985 while (1) { /* iterate thru each declaration */
6987 /* If the base type itself was an array type of unspecified
6988 size (like in 'typedef int arr[]; arr x = {1};') then
6989 we will overwrite the unknown size by the real one for
6990 this decl. We need to unshare the ref symbol holding
6992 if ((type
.t
& VT_ARRAY
) && type
.ref
->c
< 0) {
6993 type
.ref
= sym_push(SYM_FIELD
, &type
.ref
->type
, 0, type
.ref
->c
);
6995 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
6999 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
7000 printf("type = '%s'\n", buf
);
7003 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
7004 if ((type
.t
& VT_STATIC
) && (l
== VT_LOCAL
)) {
7005 tcc_error("function without file scope cannot be static");
7007 /* if old style function prototype, we accept a
7010 if (sym
->c
== FUNC_OLD
)
7011 func_decl_list(sym
);
7014 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
7015 ad
.asm_label
= asm_label_instr();
7016 /* parse one last attribute list, after asm label */
7017 parse_attribute(&ad
);
7024 #ifdef TCC_TARGET_PE
7025 if (ad
.a
.func_import
)
7026 type
.t
|= VT_IMPORT
;
7027 if (ad
.a
.func_export
)
7028 type
.t
|= VT_EXPORT
;
7030 type
.t
|= ad
.a
.visibility
<< VT_VIS_SHIFT
;
7034 tcc_error("cannot use local functions");
7035 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
7036 expect("function definition");
7038 /* reject abstract declarators in function definition */
7040 while ((sym
= sym
->next
) != NULL
)
7041 if (!(sym
->v
& ~SYM_FIELD
))
7042 expect("identifier");
7044 /* XXX: cannot do better now: convert extern line to static inline */
7045 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
7046 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
7051 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
7054 ref
= sym
->type
.ref
;
7056 /* use func_call from prototype if not defined */
7057 if (ref
->a
.func_call
!= FUNC_CDECL
7058 && type
.ref
->a
.func_call
== FUNC_CDECL
)
7059 type
.ref
->a
.func_call
= ref
->a
.func_call
;
7061 /* use export from prototype */
7062 if (ref
->a
.func_export
)
7063 type
.ref
->a
.func_export
= 1;
7065 /* use static from prototype */
7066 if (sym
->type
.t
& VT_STATIC
)
7067 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
7069 /* If the definition has no visibility use the
7070 one from prototype. */
7071 if (! (type
.t
& VT_VIS_MASK
))
7072 type
.t
|= sym
->type
.t
& VT_VIS_MASK
;
7074 if (!is_compatible_types(&sym
->type
, &type
)) {
7076 tcc_error("incompatible types for redefinition of '%s'",
7077 get_tok_str(v
, NULL
));
7079 if (ref
->a
.func_body
)
7080 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
7081 /* if symbol is already defined, then put complete type */
7085 /* put function symbol */
7086 sym
= global_identifier_push(v
, type
.t
, 0);
7087 sym
->type
.ref
= type
.ref
;
7090 sym
->type
.ref
->a
.func_body
= 1;
7091 sym
->r
= VT_SYM
| VT_CONST
;
7093 /* static inline functions are just recorded as a kind
7094 of macro. Their code will be emitted at the end of
7095 the compilation unit only if they are used */
7096 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
7097 (VT_INLINE
| VT_STATIC
)) {
7099 struct InlineFunc
*fn
;
7100 const char *filename
;
7102 filename
= file
? file
->filename
: "";
7103 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
7104 strcpy(fn
->filename
, filename
);
7106 fn
->func_str
= tok_str_alloc();
7112 tcc_error("unexpected end of file");
7113 tok_str_add_tok(fn
->func_str
);
7118 } else if (t
== '}') {
7120 if (block_level
== 0)
7124 tok_str_add(fn
->func_str
, -1);
7125 tok_str_add(fn
->func_str
, 0);
7126 dynarray_add(&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
7129 /* compute text section */
7130 cur_text_section
= ad
.section
;
7131 if (!cur_text_section
)
7132 cur_text_section
= text_section
;
7137 if (btype
.t
& VT_TYPEDEF
) {
7138 /* save typedefed type */
7139 /* XXX: test storage specifiers ? */
7141 if (sym
&& sym
->scope
== local_scope
) {
7142 if (!is_compatible_types(&sym
->type
, &type
)
7143 || !(sym
->type
.t
& VT_TYPEDEF
))
7144 tcc_error("incompatible redefinition of '%s'",
7145 get_tok_str(v
, NULL
));
7148 sym
= sym_push(v
, &type
, 0, 0);
7151 sym
->type
.t
|= VT_TYPEDEF
;
7154 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
7155 /* external function definition */
7156 /* specific case for func_call attribute */
7158 } else if (!(type
.t
& VT_ARRAY
)) {
7159 /* not lvalue if array */
7160 r
|= lvalue_type(type
.t
);
7162 has_init
= (tok
== '=');
7163 if (has_init
&& (type
.t
& VT_VLA
))
7164 tcc_error("variable length array cannot be initialized");
7165 if ((btype
.t
& VT_EXTERN
) || ((type
.t
& VT_BTYPE
) == VT_FUNC
) ||
7166 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
7167 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
7168 /* external variable or function */
7169 /* NOTE: as GCC, uninitialized global static
7170 arrays of null size are considered as
7172 sym
= external_sym(v
, &type
, r
);
7173 sym
->asm_label
= ad
.asm_label
;
7175 if (ad
.alias_target
) {
7180 alias_target
= sym_find(ad
.alias_target
);
7181 if (!alias_target
|| !alias_target
->c
)
7182 tcc_error("unsupported forward __alias__ attribute");
7183 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[alias_target
->c
];
7184 tsec
.sh_num
= esym
->st_shndx
;
7185 put_extern_sym2(sym
, &tsec
, esym
->st_value
, esym
->st_size
, 0);
7188 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
7189 if (type
.t
& VT_STATIC
)
7195 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, l
);
7199 if (is_for_loop_init
)
7212 ST_FUNC
void decl(int l
)
7217 /* ------------------------------------------------------------------------- */