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 unary_type(CType
*type
);
85 static void vla_runtime_type_size(CType
*type
, int *a
);
86 static void vla_sp_restore(void);
87 static void vla_sp_restore_root(void);
88 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
);
89 static void expr_type(CType
*type
);
90 static inline int64_t expr_const64(void);
91 ST_FUNC
void vpush64(int ty
, unsigned long long v
);
92 ST_FUNC
void vpush(CType
*type
);
93 ST_FUNC
int gvtst(int inv
, int t
);
94 ST_FUNC
int is_btype_size(int bt
);
95 static void gen_inline_functions(TCCState
*s
);
97 ST_INLN
int is_float(int t
)
101 return bt
== VT_LDOUBLE
|| bt
== VT_DOUBLE
|| bt
== VT_FLOAT
|| bt
== VT_QFLOAT
;
104 /* we use our own 'finite' function to avoid potential problems with
105 non standard math libs */
106 /* XXX: endianness dependent */
107 ST_FUNC
int ieee_finite(double d
)
110 memcpy(p
, &d
, sizeof(double));
111 return ((unsigned)((p
[1] | 0x800fffff) + 1)) >> 31;
114 ST_FUNC
void test_lvalue(void)
116 if (!(vtop
->r
& VT_LVAL
))
120 ST_FUNC
void check_vstack(void)
123 tcc_error("internal compiler error: vstack leak (%d)", vtop
- pvtop
);
126 /* ------------------------------------------------------------------------- */
127 /* vstack debugging aid */
130 void pv (const char *lbl
, int a
, int b
)
133 for (i
= a
; i
< a
+ b
; ++i
) {
134 SValue
*p
= &vtop
[-i
];
135 printf("%s vtop[-%d] : type.t:%04x r:%04x r2:%04x c.i:%d\n",
136 lbl
, i
, p
->type
.t
, p
->r
, p
->r2
, (int)p
->c
.i
);
141 /* ------------------------------------------------------------------------- */
142 /* start of translation unit info */
143 ST_FUNC
void tcc_debug_start(TCCState
*s1
)
148 /* file info: full path + filename */
149 section_sym
= put_elf_sym(symtab_section
, 0, 0,
150 ELFW(ST_INFO
)(STB_LOCAL
, STT_SECTION
), 0,
151 text_section
->sh_num
, NULL
);
152 getcwd(buf
, sizeof(buf
));
154 normalize_slashes(buf
);
156 pstrcat(buf
, sizeof(buf
), "/");
157 put_stabs_r(buf
, N_SO
, 0, 0,
158 text_section
->data_offset
, text_section
, section_sym
);
159 put_stabs_r(file
->filename
, N_SO
, 0, 0,
160 text_section
->data_offset
, text_section
, section_sym
);
165 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
166 symbols can be safely used */
167 put_elf_sym(symtab_section
, 0, 0,
168 ELFW(ST_INFO
)(STB_LOCAL
, STT_FILE
), 0,
169 SHN_ABS
, file
->filename
);
172 /* put end of translation unit info */
173 ST_FUNC
void tcc_debug_end(TCCState
*s1
)
177 put_stabs_r(NULL
, N_SO
, 0, 0,
178 text_section
->data_offset
, text_section
, section_sym
);
182 /* generate line number info */
183 ST_FUNC
void tcc_debug_line(TCCState
*s1
)
187 if ((last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
188 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
190 last_line_num
= file
->line_num
;
194 /* put function symbol */
195 ST_FUNC
void tcc_debug_funcstart(TCCState
*s1
, Sym
*sym
)
203 /* XXX: we put here a dummy type */
204 snprintf(buf
, sizeof(buf
), "%s:%c1",
205 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
206 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
207 cur_text_section
, sym
->c
);
208 /* //gr gdb wants a line at the function */
209 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
215 /* put function size */
216 ST_FUNC
void tcc_debug_funcend(TCCState
*s1
, int size
)
220 put_stabn(N_FUN
, 0, 0, size
);
223 /* ------------------------------------------------------------------------- */
224 ST_FUNC
void tccgen_start(TCCState
*s1
)
226 cur_text_section
= NULL
;
228 anon_sym
= SYM_FIRST_ANOM
;
233 /* define some often used types */
235 char_pointer_type
.t
= VT_BYTE
;
236 mk_pointer(&char_pointer_type
);
238 size_type
.t
= VT_INT
;
240 size_type
.t
= VT_LLONG
;
242 func_old_type
.t
= VT_FUNC
;
243 func_old_type
.ref
= sym_push(SYM_FIELD
, &int_type
, FUNC_CDECL
, FUNC_OLD
);
247 #ifdef TCC_TARGET_ARM
252 ST_FUNC
void tccgen_end(TCCState
*s1
)
254 gen_inline_functions(s1
);
256 /* end of translation unit info */
260 /* ------------------------------------------------------------------------- */
261 /* apply storage attibutes to Elf symbol */
263 static void update_storage(Sym
*sym
)
272 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
275 esym
->st_other
= (esym
->st_other
& ~ELFW(ST_VISIBILITY
)(-1))
276 | ((t
& VT_VIS_MASK
) >> VT_VIS_SHIFT
);
279 esym
->st_info
= ELFW(ST_INFO
)(STB_WEAK
, ELFW(ST_TYPE
)(esym
->st_info
));
283 esym
->st_other
|= ST_PE_EXPORT
;
287 /* ------------------------------------------------------------------------- */
288 /* update sym->c so that it points to an external symbol in section
289 'section' with value 'value' */
291 ST_FUNC
void put_extern_sym2(Sym
*sym
, Section
*section
,
292 addr_t value
, unsigned long size
,
293 int can_add_underscore
)
295 int sym_type
, sym_bind
, sh_num
, info
, other
, t
;
299 #ifdef CONFIG_TCC_BCHECK
305 else if (section
== SECTION_ABS
)
307 else if (section
== SECTION_COMMON
)
310 sh_num
= section
->sh_num
;
313 name
= get_tok_str(sym
->v
, NULL
);
314 #ifdef CONFIG_TCC_BCHECK
315 if (tcc_state
->do_bounds_check
) {
316 /* XXX: avoid doing that for statics ? */
317 /* if bound checking is activated, we change some function
318 names by adding the "__bound" prefix */
321 /* XXX: we rely only on malloc hooks */
334 strcpy(buf
, "__bound_");
342 if ((t
& VT_BTYPE
) == VT_FUNC
) {
344 } else if ((t
& VT_BTYPE
) == VT_VOID
) {
345 sym_type
= STT_NOTYPE
;
347 sym_type
= STT_OBJECT
;
350 sym_bind
= STB_LOCAL
;
352 sym_bind
= STB_GLOBAL
;
355 if (sym_type
== STT_FUNC
&& sym
->type
.ref
) {
356 Sym
*ref
= sym
->type
.ref
;
357 if (ref
->a
.func_call
== FUNC_STDCALL
&& can_add_underscore
) {
358 sprintf(buf1
, "_%s@%d", name
, ref
->a
.func_args
* PTR_SIZE
);
360 other
|= ST_PE_STDCALL
;
361 can_add_underscore
= 0;
365 other
|= ST_PE_IMPORT
;
367 if (tcc_state
->leading_underscore
&& can_add_underscore
) {
369 pstrcpy(buf1
+ 1, sizeof(buf1
) - 1, name
);
373 name
= get_tok_str(sym
->asm_label
, NULL
);
374 info
= ELFW(ST_INFO
)(sym_bind
, sym_type
);
375 sym
->c
= set_elf_sym(symtab_section
, value
, size
, info
, other
, sh_num
, name
);
377 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
378 esym
->st_value
= value
;
379 esym
->st_size
= size
;
380 esym
->st_shndx
= sh_num
;
385 ST_FUNC
void put_extern_sym(Sym
*sym
, Section
*section
,
386 addr_t value
, unsigned long size
)
388 put_extern_sym2(sym
, section
, value
, size
, 1);
391 /* add a new relocation entry to symbol 'sym' in section 's' */
392 ST_FUNC
void greloca(Section
*s
, Sym
*sym
, unsigned long offset
, int type
,
397 if (nocode_wanted
&& s
== cur_text_section
)
402 put_extern_sym(sym
, NULL
, 0, 0);
406 /* now we can add ELF relocation info */
407 put_elf_reloca(symtab_section
, s
, offset
, type
, c
, addend
);
410 ST_FUNC
void greloc(Section
*s
, Sym
*sym
, unsigned long offset
, int type
)
412 greloca(s
, sym
, offset
, type
, 0);
415 /* ------------------------------------------------------------------------- */
416 /* symbol allocator */
417 static Sym
*__sym_malloc(void)
419 Sym
*sym_pool
, *sym
, *last_sym
;
422 sym_pool
= tcc_malloc(SYM_POOL_NB
* sizeof(Sym
));
423 dynarray_add(&sym_pools
, &nb_sym_pools
, sym_pool
);
425 last_sym
= sym_free_first
;
427 for(i
= 0; i
< SYM_POOL_NB
; i
++) {
428 sym
->next
= last_sym
;
432 sym_free_first
= last_sym
;
436 static inline Sym
*sym_malloc(void)
440 sym
= sym_free_first
;
442 sym
= __sym_malloc();
443 sym_free_first
= sym
->next
;
446 sym
= tcc_malloc(sizeof(Sym
));
451 ST_INLN
void sym_free(Sym
*sym
)
454 sym
->next
= sym_free_first
;
455 sym_free_first
= sym
;
461 /* push, without hashing */
462 ST_FUNC Sym
*sym_push2(Sym
**ps
, int v
, int t
, long c
)
482 /* find a symbol and return its associated structure. 's' is the top
483 of the symbol stack */
484 ST_FUNC Sym
*sym_find2(Sym
*s
, int v
)
496 /* structure lookup */
497 ST_INLN Sym
*struct_find(int v
)
500 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
502 return table_ident
[v
]->sym_struct
;
505 /* find an identifier */
506 ST_INLN Sym
*sym_find(int v
)
509 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
511 return table_ident
[v
]->sym_identifier
;
514 /* push a given symbol on the symbol stack */
515 ST_FUNC Sym
*sym_push(int v
, CType
*type
, int r
, long c
)
524 s
= sym_push2(ps
, v
, type
->t
, c
);
525 s
->type
.ref
= type
->ref
;
527 /* don't record fields or anonymous symbols */
529 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
530 /* record symbol in token array */
531 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
533 ps
= &ts
->sym_struct
;
535 ps
= &ts
->sym_identifier
;
538 s
->scope
= local_scope
;
539 if (s
->prev_tok
&& s
->prev_tok
->scope
== s
->scope
)
540 tcc_error("redeclaration of '%s'",
541 get_tok_str(v
& ~SYM_STRUCT
, NULL
));
546 /* push a global identifier */
547 ST_FUNC Sym
*global_identifier_push(int v
, int t
, long c
)
550 s
= sym_push2(&global_stack
, v
, t
, c
);
551 /* don't record anonymous symbol */
552 if (v
< SYM_FIRST_ANOM
) {
553 ps
= &table_ident
[v
- TOK_IDENT
]->sym_identifier
;
554 /* modify the top most local identifier, so that
555 sym_identifier will point to 's' when popped */
557 ps
= &(*ps
)->prev_tok
;
564 /* pop symbols until top reaches 'b'. If KEEP is non-zero don't really
565 pop them yet from the list, but do remove them from the token array. */
566 ST_FUNC
void sym_pop(Sym
**ptop
, Sym
*b
, int keep
)
576 /* remove symbol in token array */
578 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
579 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
581 ps
= &ts
->sym_struct
;
583 ps
= &ts
->sym_identifier
;
594 /* ------------------------------------------------------------------------- */
596 static void vsetc(CType
*type
, int r
, CValue
*vc
)
600 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
601 tcc_error("memory full (vstack)");
602 /* cannot let cpu flags if other instruction are generated. Also
603 avoid leaving VT_JMP anywhere except on the top of the stack
604 because it would complicate the code generator.
606 Don't do this when nocode_wanted. vtop might come from
607 !nocode_wanted regions (see 88_codeopt.c) and transforming
608 it to a register without actually generating code is wrong
609 as their value might still be used for real. All values
610 we push under nocode_wanted will eventually be popped
611 again, so that the VT_CMP/VT_JMP value will be in vtop
612 when code is unsuppressed again.
614 Same logic below in vswap(); */
615 if (vtop
>= vstack
&& !nocode_wanted
) {
616 v
= vtop
->r
& VT_VALMASK
;
617 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
629 ST_FUNC
void vswap(void)
632 /* cannot vswap cpu flags. See comment at vsetc() above */
633 if (vtop
>= vstack
&& !nocode_wanted
) {
634 int v
= vtop
->r
& VT_VALMASK
;
635 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
643 /* pop stack value */
644 ST_FUNC
void vpop(void)
647 v
= vtop
->r
& VT_VALMASK
;
648 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
649 /* for x86, we need to pop the FP stack */
651 o(0xd8dd); /* fstp %st(0) */
654 if (v
== VT_JMP
|| v
== VT_JMPI
) {
655 /* need to put correct jump if && or || without test */
661 /* push constant of type "type" with useless value */
662 ST_FUNC
void vpush(CType
*type
)
665 vsetc(type
, VT_CONST
, &cval
);
668 /* push integer constant */
669 ST_FUNC
void vpushi(int v
)
673 vsetc(&int_type
, VT_CONST
, &cval
);
676 /* push a pointer sized constant */
677 static void vpushs(addr_t v
)
681 vsetc(&size_type
, VT_CONST
, &cval
);
684 /* push arbitrary 64bit constant */
685 ST_FUNC
void vpush64(int ty
, unsigned long long v
)
692 vsetc(&ctype
, VT_CONST
, &cval
);
695 /* push long long constant */
696 static inline void vpushll(long long v
)
698 vpush64(VT_LLONG
, v
);
701 ST_FUNC
void vset(CType
*type
, int r
, long v
)
706 vsetc(type
, r
, &cval
);
709 static void vseti(int r
, int v
)
717 ST_FUNC
void vpushv(SValue
*v
)
719 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
720 tcc_error("memory full (vstack)");
725 static void vdup(void)
730 /* rotate n first stack elements to the bottom
731 I1 ... In -> I2 ... In I1 [top is right]
733 ST_FUNC
void vrotb(int n
)
744 /* rotate the n elements before entry e towards the top
745 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
747 ST_FUNC
void vrote(SValue
*e
, int n
)
753 for(i
= 0;i
< n
- 1; i
++)
758 /* rotate n first stack elements to the top
759 I1 ... In -> In I1 ... I(n-1) [top is right]
761 ST_FUNC
void vrott(int n
)
766 /* push a symbol value of TYPE */
767 static inline void vpushsym(CType
*type
, Sym
*sym
)
771 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
775 /* Return a static symbol pointing to a section */
776 ST_FUNC Sym
*get_sym_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
782 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
783 sym
->type
.ref
= type
->ref
;
784 sym
->r
= VT_CONST
| VT_SYM
;
785 put_extern_sym(sym
, sec
, offset
, size
);
789 /* push a reference to a section offset by adding a dummy symbol */
790 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
792 vpushsym(type
, get_sym_ref(type
, sec
, offset
, size
));
795 /* define a new external reference to a symbol 'v' of type 'u' */
796 ST_FUNC Sym
*external_global_sym(int v
, CType
*type
, int r
)
802 /* push forward reference */
803 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
804 s
->type
.ref
= type
->ref
;
805 s
->r
= r
| VT_CONST
| VT_SYM
;
810 /* Merge some storage attributes. */
811 static void patch_storage(Sym
*sym
, CType
*type
)
814 if (!is_compatible_types(&sym
->type
, type
))
815 tcc_error("incompatible types for redefinition of '%s'",
816 get_tok_str(sym
->v
, NULL
));
819 if ((sym
->type
.t
^ t
) & VT_IMPORT
)
820 tcc_error("incompatible dll linkage for redefinition of '%s'",
821 get_tok_str(sym
->v
, NULL
));
823 sym
->type
.t
|= t
& (VT_EXPORT
|VT_WEAK
);
824 if (t
& VT_VIS_MASK
) {
825 int vis
= sym
->type
.t
& VT_VIS_MASK
;
826 int vis2
= t
& VT_VIS_MASK
;
827 if (vis
== (STV_DEFAULT
<< VT_VIS_SHIFT
))
829 else if (vis2
!= (STV_DEFAULT
<< VT_VIS_SHIFT
))
830 vis
= (vis
< vis2
) ? vis
: vis2
;
831 sym
->type
.t
= (sym
->type
.t
& ~VT_VIS_MASK
) | vis
;
835 /* define a new external reference to a symbol 'v' */
836 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
;
845 if (s
->type
.ref
== func_old_type
.ref
) {
846 s
->type
.ref
= type
->ref
;
847 s
->r
= r
| VT_CONST
| VT_SYM
;
848 s
->type
.t
|= VT_EXTERN
;
850 patch_storage(s
, type
);
856 /* push a reference to global symbol v */
857 ST_FUNC
void vpush_global_sym(CType
*type
, int v
)
859 vpushsym(type
, external_global_sym(v
, type
, 0));
862 /* save registers up to (vtop - n) stack entry */
863 ST_FUNC
void save_regs(int n
)
866 for(p
= vstack
, p1
= vtop
- n
; p
<= p1
; p
++)
870 /* save r to the memory stack, and mark it as being free */
871 ST_FUNC
void save_reg(int r
)
873 save_reg_upstack(r
, 0);
876 /* save r to the memory stack, and mark it as being free,
877 if seen up to (vtop - n) stack entry */
878 ST_FUNC
void save_reg_upstack(int r
, int n
)
880 int l
, saved
, size
, align
;
884 if ((r
&= VT_VALMASK
) >= VT_CONST
)
889 /* modify all stack values */
892 for(p
= vstack
, p1
= vtop
- n
; p
<= p1
; p
++) {
893 if ((p
->r
& VT_VALMASK
) == r
||
894 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
895 /* must save value on stack if not already done */
897 /* NOTE: must reload 'r' because r might be equal to r2 */
898 r
= p
->r
& VT_VALMASK
;
899 /* store register in the stack */
901 if ((p
->r
& VT_LVAL
) ||
902 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
903 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
904 type
= &char_pointer_type
;
908 size
= type_size(type
, &align
);
909 loc
= (loc
- size
) & -align
;
911 sv
.r
= VT_LOCAL
| VT_LVAL
;
914 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
915 /* x86 specific: need to pop fp register ST0 if saved */
917 o(0xd8dd); /* fstp %st(0) */
920 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
921 /* special long long case */
922 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
930 /* mark that stack entry as being saved on the stack */
931 if (p
->r
& VT_LVAL
) {
932 /* also clear the bounded flag because the
933 relocation address of the function was stored in
935 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
937 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
945 #ifdef TCC_TARGET_ARM
946 /* find a register of class 'rc2' with at most one reference on stack.
947 * If none, call get_reg(rc) */
948 ST_FUNC
int get_reg_ex(int rc
, int rc2
)
953 for(r
=0;r
<NB_REGS
;r
++) {
954 if (reg_classes
[r
] & rc2
) {
957 for(p
= vstack
; p
<= vtop
; p
++) {
958 if ((p
->r
& VT_VALMASK
) == r
||
959 (p
->r2
& VT_VALMASK
) == r
)
970 /* find a free register of class 'rc'. If none, save one register */
971 ST_FUNC
int get_reg(int rc
)
976 /* find a free register */
977 for(r
=0;r
<NB_REGS
;r
++) {
978 if (reg_classes
[r
] & rc
) {
981 for(p
=vstack
;p
<=vtop
;p
++) {
982 if ((p
->r
& VT_VALMASK
) == r
||
983 (p
->r2
& VT_VALMASK
) == r
)
991 /* no register left : free the first one on the stack (VERY
992 IMPORTANT to start from the bottom to ensure that we don't
993 spill registers used in gen_opi()) */
994 for(p
=vstack
;p
<=vtop
;p
++) {
995 /* look at second register (if long long) */
996 r
= p
->r2
& VT_VALMASK
;
997 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
999 r
= p
->r
& VT_VALMASK
;
1000 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
1006 /* Should never comes here */
1010 /* move register 's' (of type 't') to 'r', and flush previous value of r to memory
1012 static void move_reg(int r
, int s
, int t
)
1026 /* get address of vtop (vtop MUST BE an lvalue) */
1027 ST_FUNC
void gaddrof(void)
1029 if (vtop
->r
& VT_REF
)
1031 vtop
->r
&= ~VT_LVAL
;
1032 /* tricky: if saved lvalue, then we can go back to lvalue */
1033 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
1034 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
1039 #ifdef CONFIG_TCC_BCHECK
1040 /* generate lvalue bound code */
1041 static void gbound(void)
1046 vtop
->r
&= ~VT_MUSTBOUND
;
1047 /* if lvalue, then use checking code before dereferencing */
1048 if (vtop
->r
& VT_LVAL
) {
1049 /* if not VT_BOUNDED value, then make one */
1050 if (!(vtop
->r
& VT_BOUNDED
)) {
1051 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
1052 /* must save type because we must set it to int to get pointer */
1054 vtop
->type
.t
= VT_PTR
;
1057 gen_bounded_ptr_add();
1058 vtop
->r
|= lval_type
;
1061 /* then check for dereferencing */
1062 gen_bounded_ptr_deref();
1067 /* store vtop a register belonging to class 'rc'. lvalues are
1068 converted to values. Cannot be used if cannot be converted to
1069 register value (such as structures). */
1070 ST_FUNC
int gv(int rc
)
1072 int r
, bit_pos
, bit_size
, size
, align
, i
;
1075 /* NOTE: get_reg can modify vstack[] */
1076 if (vtop
->type
.t
& VT_BITFIELD
) {
1079 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
1080 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
1081 /* remove bit field info to avoid loops */
1082 vtop
->type
.t
&= ~VT_BITFIELD
& ((1 << VT_STRUCT_SHIFT
) - 1);
1083 /* cast to int to propagate signedness in following ops */
1084 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
1089 if((vtop
->type
.t
& VT_UNSIGNED
) ||
1090 (vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
1091 type
.t
|= VT_UNSIGNED
;
1093 /* generate shifts */
1094 vpushi(bits
- (bit_pos
+ bit_size
));
1096 vpushi(bits
- bit_size
);
1097 /* NOTE: transformed to SHR if unsigned */
1101 if (is_float(vtop
->type
.t
) &&
1102 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
1105 unsigned long offset
;
1106 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
1110 /* XXX: unify with initializers handling ? */
1111 /* CPUs usually cannot use float constants, so we store them
1112 generically in data segment */
1113 size
= type_size(&vtop
->type
, &align
);
1114 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
1115 data_section
->data_offset
= offset
;
1116 /* XXX: not portable yet */
1117 #if defined(__i386__) || defined(__x86_64__)
1118 /* Zero pad x87 tenbyte long doubles */
1119 if (size
== LDOUBLE_SIZE
) {
1120 vtop
->c
.tab
[2] &= 0xffff;
1121 #if LDOUBLE_SIZE == 16
1126 ptr
= section_ptr_add(data_section
, size
);
1128 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
1132 ptr
[i
] = vtop
->c
.tab
[size
-1-i
];
1136 ptr
[i
] = vtop
->c
.tab
[i
];
1137 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
1138 vtop
->r
|= VT_LVAL
| VT_SYM
;
1142 #ifdef CONFIG_TCC_BCHECK
1143 if (vtop
->r
& VT_MUSTBOUND
)
1147 r
= vtop
->r
& VT_VALMASK
;
1148 rc2
= (rc
& RC_FLOAT
) ? RC_FLOAT
: RC_INT
;
1149 #ifndef TCC_TARGET_ARM64
1152 #ifdef TCC_TARGET_X86_64
1153 else if (rc
== RC_FRET
)
1157 /* need to reload if:
1159 - lvalue (need to dereference pointer)
1160 - already a register, but not in the right class */
1162 || (vtop
->r
& VT_LVAL
)
1163 || !(reg_classes
[r
] & rc
)
1164 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1165 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
1166 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
&& !(reg_classes
[vtop
->r2
] & rc2
))
1168 || ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
1173 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1174 if (((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
)) {
1175 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
1177 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
1178 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
1179 unsigned long long ll
;
1181 int r2
, original_type
;
1182 original_type
= vtop
->type
.t
;
1183 /* two register type load : expand to two words
1185 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
1186 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
1189 vtop
->c
.i
= ll
; /* first word */
1191 vtop
->r
= r
; /* save register value */
1192 vpushi(ll
>> 32); /* second word */
1195 if (vtop
->r
& VT_LVAL
) {
1196 /* We do not want to modifier the long long
1197 pointer here, so the safest (and less
1198 efficient) is to save all the other registers
1199 in the stack. XXX: totally inefficient. */
1203 /* lvalue_save: save only if used further down the stack */
1204 save_reg_upstack(vtop
->r
, 1);
1206 /* load from memory */
1207 vtop
->type
.t
= load_type
;
1210 vtop
[-1].r
= r
; /* save register value */
1211 /* increment pointer to get second word */
1212 vtop
->type
.t
= addr_type
;
1217 vtop
->type
.t
= load_type
;
1219 /* move registers */
1222 vtop
[-1].r
= r
; /* save register value */
1223 vtop
->r
= vtop
[-1].r2
;
1225 /* Allocate second register. Here we rely on the fact that
1226 get_reg() tries first to free r2 of an SValue. */
1230 /* write second register */
1232 vtop
->type
.t
= original_type
;
1233 } else if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
1235 /* lvalue of scalar type : need to use lvalue type
1236 because of possible cast */
1239 /* compute memory access type */
1240 if (vtop
->r
& VT_REF
)
1241 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1246 else if (vtop
->r
& VT_LVAL_BYTE
)
1248 else if (vtop
->r
& VT_LVAL_SHORT
)
1250 if (vtop
->r
& VT_LVAL_UNSIGNED
)
1254 /* restore wanted type */
1257 /* one register type load */
1262 #ifdef TCC_TARGET_C67
1263 /* uses register pairs for doubles */
1264 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
1271 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
1272 ST_FUNC
void gv2(int rc1
, int rc2
)
1276 /* generate more generic register first. But VT_JMP or VT_CMP
1277 values must be generated first in all cases to avoid possible
1279 v
= vtop
[0].r
& VT_VALMASK
;
1280 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
1285 /* test if reload is needed for first register */
1286 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
1296 /* test if reload is needed for first register */
1297 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
1303 #ifndef TCC_TARGET_ARM64
1304 /* wrapper around RC_FRET to return a register by type */
1305 static int rc_fret(int t
)
1307 #ifdef TCC_TARGET_X86_64
1308 if (t
== VT_LDOUBLE
) {
1316 /* wrapper around REG_FRET to return a register by type */
1317 static int reg_fret(int t
)
1319 #ifdef TCC_TARGET_X86_64
1320 if (t
== VT_LDOUBLE
) {
1327 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
1328 /* expand 64bit on stack in two ints */
1329 static void lexpand(void)
1332 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
1333 v
= vtop
->r
& (VT_VALMASK
| VT_LVAL
);
1334 if (v
== VT_CONST
) {
1337 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
1343 vtop
[0].r
= vtop
[-1].r2
;
1344 vtop
[0].r2
= vtop
[-1].r2
= VT_CONST
;
1346 vtop
[0].type
.t
= vtop
[-1].type
.t
= VT_INT
| u
;
1350 #ifdef TCC_TARGET_ARM
1351 /* expand long long on stack */
1352 ST_FUNC
void lexpand_nr(void)
1356 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
1358 vtop
->r2
= VT_CONST
;
1359 vtop
->type
.t
= VT_INT
| u
;
1360 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
1361 if (v
== VT_CONST
) {
1362 vtop
[-1].c
.i
= vtop
->c
.i
;
1363 vtop
->c
.i
= vtop
->c
.i
>> 32;
1365 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
1367 vtop
->r
= vtop
[-1].r
;
1368 } else if (v
> VT_CONST
) {
1372 vtop
->r
= vtop
[-1].r2
;
1373 vtop
[-1].r2
= VT_CONST
;
1374 vtop
[-1].type
.t
= VT_INT
| u
;
1378 #if !defined(TCC_TARGET_X86_64) && !defined(TCC_TARGET_ARM64)
1379 /* build a long long from two ints */
1380 static void lbuild(int t
)
1382 gv2(RC_INT
, RC_INT
);
1383 vtop
[-1].r2
= vtop
[0].r
;
1384 vtop
[-1].type
.t
= t
;
1389 /* convert stack entry to register and duplicate its value in another
1391 static void gv_dup(void)
1397 #if !defined(TCC_TARGET_X86_64) && !defined(TCC_TARGET_ARM64)
1398 if ((t
& VT_BTYPE
) == VT_LLONG
) {
1405 /* stack: H L L1 H1 */
1415 /* duplicate value */
1420 #ifdef TCC_TARGET_X86_64
1421 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1431 load(r1
, &sv
); /* move r to r1 */
1433 /* duplicates value */
1439 /* Generate value test
1441 * Generate a test for any value (jump, comparison and integers) */
1442 ST_FUNC
int gvtst(int inv
, int t
)
1444 int v
= vtop
->r
& VT_VALMASK
;
1445 if (v
!= VT_CMP
&& v
!= VT_JMP
&& v
!= VT_JMPI
) {
1449 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1450 /* constant jmp optimization */
1451 if ((vtop
->c
.i
!= 0) != inv
)
1456 return gtst(inv
, t
);
1459 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
1460 /* generate CPU independent (unsigned) long long operations */
1461 static void gen_opl(int op
)
1463 int t
, a
, b
, op1
, c
, i
;
1465 unsigned short reg_iret
= REG_IRET
;
1466 unsigned short reg_lret
= REG_LRET
;
1472 func
= TOK___divdi3
;
1475 func
= TOK___udivdi3
;
1478 func
= TOK___moddi3
;
1481 func
= TOK___umoddi3
;
1488 /* call generic long long function */
1489 vpush_global_sym(&func_old_type
, func
);
1494 vtop
->r2
= reg_lret
;
1502 //pv("gen_opl A",0,2);
1508 /* stack: L1 H1 L2 H2 */
1513 vtop
[-2] = vtop
[-3];
1516 /* stack: H1 H2 L1 L2 */
1517 //pv("gen_opl B",0,4);
1523 /* stack: H1 H2 L1 L2 ML MH */
1526 /* stack: ML MH H1 H2 L1 L2 */
1530 /* stack: ML MH H1 L2 H2 L1 */
1535 /* stack: ML MH M1 M2 */
1538 } else if (op
== '+' || op
== '-') {
1539 /* XXX: add non carry method too (for MIPS or alpha) */
1545 /* stack: H1 H2 (L1 op L2) */
1548 gen_op(op1
+ 1); /* TOK_xxxC2 */
1551 /* stack: H1 H2 (L1 op L2) */
1554 /* stack: (L1 op L2) H1 H2 */
1556 /* stack: (L1 op L2) (H1 op H2) */
1564 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1565 t
= vtop
[-1].type
.t
;
1569 /* stack: L H shift */
1571 /* constant: simpler */
1572 /* NOTE: all comments are for SHL. the other cases are
1573 done by swaping words */
1584 if (op
!= TOK_SAR
) {
1617 /* XXX: should provide a faster fallback on x86 ? */
1620 func
= TOK___ashrdi3
;
1623 func
= TOK___lshrdi3
;
1626 func
= TOK___ashldi3
;
1632 /* compare operations */
1638 /* stack: L1 H1 L2 H2 */
1640 vtop
[-1] = vtop
[-2];
1642 /* stack: L1 L2 H1 H2 */
1645 /* when values are equal, we need to compare low words. since
1646 the jump is inverted, we invert the test too. */
1649 else if (op1
== TOK_GT
)
1651 else if (op1
== TOK_ULT
)
1653 else if (op1
== TOK_UGT
)
1663 /* generate non equal test */
1669 /* compare low. Always unsigned */
1673 else if (op1
== TOK_LE
)
1675 else if (op1
== TOK_GT
)
1677 else if (op1
== TOK_GE
)
1688 static uint64_t gen_opic_sdiv(uint64_t a
, uint64_t b
)
1690 uint64_t x
= (a
>> 63 ? -a
: a
) / (b
>> 63 ? -b
: b
);
1691 return (a
^ b
) >> 63 ? -x
: x
;
1694 static int gen_opic_lt(uint64_t a
, uint64_t b
)
1696 return (a
^ (uint64_t)1 << 63) < (b
^ (uint64_t)1 << 63);
1699 /* handle integer constant optimizations and various machine
1701 static void gen_opic(int op
)
1703 SValue
*v1
= vtop
- 1;
1705 int t1
= v1
->type
.t
& VT_BTYPE
;
1706 int t2
= v2
->type
.t
& VT_BTYPE
;
1707 int c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1708 int c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1709 uint64_t l1
= c1
? v1
->c
.i
: 0;
1710 uint64_t l2
= c2
? v2
->c
.i
: 0;
1711 int shm
= (t1
== VT_LLONG
) ? 63 : 31;
1713 if (t1
!= VT_LLONG
&& (PTR_SIZE
!= 8 || t1
!= VT_PTR
))
1714 l1
= ((uint32_t)l1
|
1715 (v1
->type
.t
& VT_UNSIGNED
? 0 : -(l1
& 0x80000000)));
1716 if (t2
!= VT_LLONG
&& (PTR_SIZE
!= 8 || t2
!= VT_PTR
))
1717 l2
= ((uint32_t)l2
|
1718 (v2
->type
.t
& VT_UNSIGNED
? 0 : -(l2
& 0x80000000)));
1722 case '+': l1
+= l2
; break;
1723 case '-': l1
-= l2
; break;
1724 case '&': l1
&= l2
; break;
1725 case '^': l1
^= l2
; break;
1726 case '|': l1
|= l2
; break;
1727 case '*': l1
*= l2
; break;
1734 /* if division by zero, generate explicit division */
1737 tcc_error("division by zero in constant");
1741 default: l1
= gen_opic_sdiv(l1
, l2
); break;
1742 case '%': l1
= l1
- l2
* gen_opic_sdiv(l1
, l2
); break;
1743 case TOK_UDIV
: l1
= l1
/ l2
; break;
1744 case TOK_UMOD
: l1
= l1
% l2
; break;
1747 case TOK_SHL
: l1
<<= (l2
& shm
); break;
1748 case TOK_SHR
: l1
>>= (l2
& shm
); break;
1750 l1
= (l1
>> 63) ? ~(~l1
>> (l2
& shm
)) : l1
>> (l2
& shm
);
1753 case TOK_ULT
: l1
= l1
< l2
; break;
1754 case TOK_UGE
: l1
= l1
>= l2
; break;
1755 case TOK_EQ
: l1
= l1
== l2
; break;
1756 case TOK_NE
: l1
= l1
!= l2
; break;
1757 case TOK_ULE
: l1
= l1
<= l2
; break;
1758 case TOK_UGT
: l1
= l1
> l2
; break;
1759 case TOK_LT
: l1
= gen_opic_lt(l1
, l2
); break;
1760 case TOK_GE
: l1
= !gen_opic_lt(l1
, l2
); break;
1761 case TOK_LE
: l1
= !gen_opic_lt(l2
, l1
); break;
1762 case TOK_GT
: l1
= gen_opic_lt(l2
, l1
); break;
1764 case TOK_LAND
: l1
= l1
&& l2
; break;
1765 case TOK_LOR
: l1
= l1
|| l2
; break;
1769 if (t1
!= VT_LLONG
&& (PTR_SIZE
!= 8 || t1
!= VT_PTR
))
1770 l1
= ((uint32_t)l1
|
1771 (v1
->type
.t
& VT_UNSIGNED
? 0 : -(l1
& 0x80000000)));
1775 /* if commutative ops, put c2 as constant */
1776 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1777 op
== '|' || op
== '*')) {
1779 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1780 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1782 if (!const_wanted
&&
1784 (op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
)) ||
1785 (l1
== -1 && op
== TOK_SAR
))) {
1786 /* treat (0 << x), (0 >> x) and (-1 >> x) as constant */
1788 } else if (!const_wanted
&&
1789 c2
&& ((l2
== 0 && (op
== '&' || op
== '*')) ||
1790 (l2
== -1 && op
== '|') ||
1791 (l2
== 0xffffffff && t2
!= VT_LLONG
&& op
== '|') ||
1792 (l2
== 1 && (op
== '%' || op
== TOK_UMOD
)))) {
1793 /* treat (x & 0), (x * 0), (x | -1) and (x % 1) as constant */
1798 } else if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1801 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1802 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1806 /* filter out NOP operations like x*1, x-0, x&-1... */
1808 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1809 /* try to use shifts instead of muls or divs */
1810 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1819 else if (op
== TOK_PDIV
)
1825 } else if (c2
&& (op
== '+' || op
== '-') &&
1826 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
1827 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1828 /* symbol + constant case */
1832 /* The backends can't always deal with addends to symbols
1833 larger than +-1<<31. Don't construct such. */
1840 /* call low level op generator */
1841 if (t1
== VT_LLONG
|| t2
== VT_LLONG
||
1842 (PTR_SIZE
== 8 && (t1
== VT_PTR
|| t2
== VT_PTR
)))
1850 /* generate a floating point operation with constant propagation */
1851 static void gen_opif(int op
)
1859 /* currently, we cannot do computations with forward symbols */
1860 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1861 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1863 if (v1
->type
.t
== VT_FLOAT
) {
1866 } else if (v1
->type
.t
== VT_DOUBLE
) {
1874 /* NOTE: we only do constant propagation if finite number (not
1875 NaN or infinity) (ANSI spec) */
1876 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1880 case '+': f1
+= f2
; break;
1881 case '-': f1
-= f2
; break;
1882 case '*': f1
*= f2
; break;
1886 tcc_error("division by zero in constant");
1891 /* XXX: also handles tests ? */
1895 /* XXX: overflow test ? */
1896 if (v1
->type
.t
== VT_FLOAT
) {
1898 } else if (v1
->type
.t
== VT_DOUBLE
) {
1910 static int pointed_size(CType
*type
)
1913 return type_size(pointed_type(type
), &align
);
1916 static void vla_runtime_pointed_size(CType
*type
)
1919 vla_runtime_type_size(pointed_type(type
), &align
);
1922 static inline int is_null_pointer(SValue
*p
)
1924 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1926 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& (uint32_t)p
->c
.i
== 0) ||
1927 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.i
== 0) ||
1928 ((p
->type
.t
& VT_BTYPE
) == VT_PTR
&&
1929 (PTR_SIZE
== 4 ? (uint32_t)p
->c
.i
== 0 : p
->c
.i
== 0));
1932 static inline int is_integer_btype(int bt
)
1934 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
1935 bt
== VT_INT
|| bt
== VT_LLONG
);
1938 /* check types for comparison or subtraction of pointers */
1939 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
1941 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
1944 /* null pointers are accepted for all comparisons as gcc */
1945 if (is_null_pointer(p1
) || is_null_pointer(p2
))
1949 bt1
= type1
->t
& VT_BTYPE
;
1950 bt2
= type2
->t
& VT_BTYPE
;
1951 /* accept comparison between pointer and integer with a warning */
1952 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
1953 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
1954 tcc_warning("comparison between pointer and integer");
1958 /* both must be pointers or implicit function pointers */
1959 if (bt1
== VT_PTR
) {
1960 type1
= pointed_type(type1
);
1961 } else if (bt1
!= VT_FUNC
)
1962 goto invalid_operands
;
1964 if (bt2
== VT_PTR
) {
1965 type2
= pointed_type(type2
);
1966 } else if (bt2
!= VT_FUNC
) {
1968 tcc_error("invalid operands to binary %s", get_tok_str(op
, NULL
));
1970 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1971 (type2
->t
& VT_BTYPE
) == VT_VOID
)
1975 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1976 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1977 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1978 /* gcc-like error if '-' is used */
1980 goto invalid_operands
;
1982 tcc_warning("comparison of distinct pointer types lacks a cast");
1986 /* generic gen_op: handles types problems */
1987 ST_FUNC
void gen_op(int op
)
1989 int u
, t1
, t2
, bt1
, bt2
, t
;
1993 t1
= vtop
[-1].type
.t
;
1994 t2
= vtop
[0].type
.t
;
1995 bt1
= t1
& VT_BTYPE
;
1996 bt2
= t2
& VT_BTYPE
;
1998 if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
1999 tcc_error("operation on a struct");
2000 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
2001 if (bt2
== VT_FUNC
) {
2002 mk_pointer(&vtop
->type
);
2005 if (bt1
== VT_FUNC
) {
2007 mk_pointer(&vtop
->type
);
2012 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
2013 /* at least one operand is a pointer */
2014 /* relationnal op: must be both pointers */
2015 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
2016 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
2017 /* pointers are handled are unsigned */
2018 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2019 t
= VT_LLONG
| VT_UNSIGNED
;
2021 t
= VT_INT
| VT_UNSIGNED
;
2025 /* if both pointers, then it must be the '-' op */
2026 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
2028 tcc_error("cannot use pointers here");
2029 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
2030 /* XXX: check that types are compatible */
2031 if (vtop
[-1].type
.t
& VT_VLA
) {
2032 vla_runtime_pointed_size(&vtop
[-1].type
);
2034 vpushi(pointed_size(&vtop
[-1].type
));
2038 /* set to integer type */
2039 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2040 vtop
->type
.t
= VT_LLONG
;
2042 vtop
->type
.t
= VT_INT
;
2047 /* exactly one pointer : must be '+' or '-'. */
2048 if (op
!= '-' && op
!= '+')
2049 tcc_error("cannot use pointers here");
2050 /* Put pointer as first operand */
2051 if (bt2
== VT_PTR
) {
2053 t
= t1
, t1
= t2
, t2
= t
;
2056 if ((vtop
[0].type
.t
& VT_BTYPE
) == VT_LLONG
)
2057 /* XXX: truncate here because gen_opl can't handle ptr + long long */
2058 gen_cast(&int_type
);
2060 type1
= vtop
[-1].type
;
2061 type1
.t
&= ~VT_ARRAY
;
2062 if (vtop
[-1].type
.t
& VT_VLA
)
2063 vla_runtime_pointed_size(&vtop
[-1].type
);
2065 u
= pointed_size(&vtop
[-1].type
);
2067 tcc_error("unknown array element size");
2068 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2071 /* XXX: cast to int ? (long long case) */
2077 /* #ifdef CONFIG_TCC_BCHECK
2078 The main reason to removing this code:
2085 fprintf(stderr, "v+i-j = %p\n", v+i-j);
2086 fprintf(stderr, "v+(i-j) = %p\n", v+(i-j));
2088 When this code is on. then the output looks like
2090 v+(i-j) = 0xbff84000
2092 /* if evaluating constant expression, no code should be
2093 generated, so no bound check */
2094 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
2095 /* if bounded pointers, we generate a special code to
2102 gen_bounded_ptr_add();
2108 /* put again type if gen_opic() swaped operands */
2111 } else if (is_float(bt1
) || is_float(bt2
)) {
2112 /* compute bigger type and do implicit casts */
2113 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
2115 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
2120 /* floats can only be used for a few operations */
2121 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
2122 (op
< TOK_ULT
|| op
> TOK_GT
))
2123 tcc_error("invalid operands for binary operation");
2125 } else if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
) {
2126 t
= bt1
== VT_LLONG
? VT_LLONG
: VT_INT
;
2127 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (t
| VT_UNSIGNED
))
2130 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
2131 /* cast to biggest op */
2133 /* convert to unsigned if it does not fit in a long long */
2134 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
2135 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
2139 /* integer operations */
2141 /* convert to unsigned if it does not fit in an integer */
2142 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
2143 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
2146 /* XXX: currently, some unsigned operations are explicit, so
2147 we modify them here */
2148 if (t
& VT_UNSIGNED
) {
2155 else if (op
== TOK_LT
)
2157 else if (op
== TOK_GT
)
2159 else if (op
== TOK_LE
)
2161 else if (op
== TOK_GE
)
2168 /* special case for shifts and long long: we keep the shift as
2170 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
2177 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
2178 /* relationnal op: the result is an int */
2179 vtop
->type
.t
= VT_INT
;
2184 // Make sure that we have converted to an rvalue:
2185 if (vtop
->r
& VT_LVAL
)
2186 gv(is_float(vtop
->type
.t
& VT_BTYPE
) ? RC_FLOAT
: RC_INT
);
2189 #ifndef TCC_TARGET_ARM
2190 /* generic itof for unsigned long long case */
2191 static void gen_cvt_itof1(int t
)
2193 #ifdef TCC_TARGET_ARM64
2196 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
2197 (VT_LLONG
| VT_UNSIGNED
)) {
2200 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
2201 #if LDOUBLE_SIZE != 8
2202 else if (t
== VT_LDOUBLE
)
2203 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
2206 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
2210 vtop
->r
= reg_fret(t
);
2218 /* generic ftoi for unsigned long long case */
2219 static void gen_cvt_ftoi1(int t
)
2221 #ifdef TCC_TARGET_ARM64
2226 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
2227 /* not handled natively */
2228 st
= vtop
->type
.t
& VT_BTYPE
;
2230 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
2231 #if LDOUBLE_SIZE != 8
2232 else if (st
== VT_LDOUBLE
)
2233 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
2236 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
2241 vtop
->r2
= REG_LRET
;
2248 /* force char or short cast */
2249 static void force_charshort_cast(int t
)
2253 /* XXX: add optimization if lvalue : just change type and offset */
2258 if (t
& VT_UNSIGNED
) {
2259 vpushi((1 << bits
) - 1);
2262 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
)
2268 /* result must be signed or the SAR is converted to an SHL
2269 This was not the case when "t" was a signed short
2270 and the last value on the stack was an unsigned int */
2271 vtop
->type
.t
&= ~VT_UNSIGNED
;
2277 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
2278 static void gen_cast(CType
*type
)
2280 int sbt
, dbt
, sf
, df
, c
, p
;
2282 /* special delayed cast for char/short */
2283 /* XXX: in some cases (multiple cascaded casts), it may still
2285 if (vtop
->r
& VT_MUSTCAST
) {
2286 vtop
->r
&= ~VT_MUSTCAST
;
2287 force_charshort_cast(vtop
->type
.t
);
2290 /* bitfields first get cast to ints */
2291 if (vtop
->type
.t
& VT_BITFIELD
) {
2295 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
2296 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
2301 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
2302 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
2304 /* constant case: we can do it now */
2305 /* XXX: in ISOC, cannot do it if error in convert */
2306 if (sbt
== VT_FLOAT
)
2307 vtop
->c
.ld
= vtop
->c
.f
;
2308 else if (sbt
== VT_DOUBLE
)
2309 vtop
->c
.ld
= vtop
->c
.d
;
2312 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
2313 if ((sbt
& VT_UNSIGNED
) || !(vtop
->c
.i
>> 63))
2314 vtop
->c
.ld
= vtop
->c
.i
;
2316 vtop
->c
.ld
= -(long double)-vtop
->c
.i
;
2318 if ((sbt
& VT_UNSIGNED
) || !(vtop
->c
.i
>> 31))
2319 vtop
->c
.ld
= (uint32_t)vtop
->c
.i
;
2321 vtop
->c
.ld
= -(long double)-(uint32_t)vtop
->c
.i
;
2324 if (dbt
== VT_FLOAT
)
2325 vtop
->c
.f
= (float)vtop
->c
.ld
;
2326 else if (dbt
== VT_DOUBLE
)
2327 vtop
->c
.d
= (double)vtop
->c
.ld
;
2328 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
2329 vtop
->c
.i
= vtop
->c
.ld
;
2330 } else if (sf
&& dbt
== VT_BOOL
) {
2331 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
2334 vtop
->c
.i
= vtop
->c
.ld
;
2335 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
2337 else if (sbt
& VT_UNSIGNED
)
2338 vtop
->c
.i
= (uint32_t)vtop
->c
.i
;
2339 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2340 else if (sbt
== VT_PTR
)
2343 else if (sbt
!= VT_LLONG
)
2344 vtop
->c
.i
= ((uint32_t)vtop
->c
.i
|
2345 -(vtop
->c
.i
& 0x80000000));
2347 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
2349 else if (dbt
== VT_BOOL
)
2350 vtop
->c
.i
= (vtop
->c
.i
!= 0);
2351 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2352 else if (dbt
== VT_PTR
)
2355 else if (dbt
!= VT_LLONG
) {
2356 uint32_t m
= ((dbt
& VT_BTYPE
) == VT_BYTE
? 0xff :
2357 (dbt
& VT_BTYPE
) == VT_SHORT
? 0xffff :
2360 if (!(dbt
& VT_UNSIGNED
))
2361 vtop
->c
.i
|= -(vtop
->c
.i
& ((m
>> 1) + 1));
2364 } else if (p
&& dbt
== VT_BOOL
) {
2368 /* non constant case: generate code */
2370 /* convert from fp to fp */
2373 /* convert int to fp */
2376 /* convert fp to int */
2377 if (dbt
== VT_BOOL
) {
2381 /* we handle char/short/etc... with generic code */
2382 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
2383 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
2387 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
2388 /* additional cast for char/short... */
2393 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
2394 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
2395 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
2396 /* scalar to long long */
2397 /* machine independent conversion */
2399 /* generate high word */
2400 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
2404 if (sbt
== VT_PTR
) {
2405 /* cast from pointer to int before we apply
2406 shift operation, which pointers don't support*/
2407 gen_cast(&int_type
);
2413 /* patch second register */
2414 vtop
[-1].r2
= vtop
->r
;
2418 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
2419 (dbt
& VT_BTYPE
) == VT_PTR
||
2420 (dbt
& VT_BTYPE
) == VT_FUNC
) {
2421 if ((sbt
& VT_BTYPE
) != VT_LLONG
&&
2422 (sbt
& VT_BTYPE
) != VT_PTR
&&
2423 (sbt
& VT_BTYPE
) != VT_FUNC
) {
2424 /* need to convert from 32bit to 64bit */
2426 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
2427 #if defined(TCC_TARGET_ARM64)
2429 #elif defined(TCC_TARGET_X86_64)
2431 /* x86_64 specific: movslq */
2433 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
2440 } else if (dbt
== VT_BOOL
) {
2441 /* scalar to bool */
2444 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
2445 (dbt
& VT_BTYPE
) == VT_SHORT
) {
2446 if (sbt
== VT_PTR
) {
2447 vtop
->type
.t
= VT_INT
;
2448 tcc_warning("nonportable conversion from pointer to char/short");
2450 force_charshort_cast(dbt
);
2451 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
2452 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
2454 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
2455 /* from long long: just take low order word */
2459 /* if lvalue and single word type, nothing to do because
2460 the lvalue already contains the real type size (see
2461 VT_LVAL_xxx constants) */
2465 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
2466 /* if we are casting between pointer types,
2467 we must update the VT_LVAL_xxx size */
2468 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
2469 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
2474 /* return type size as known at compile time. Put alignment at 'a' */
2475 ST_FUNC
int type_size(CType
*type
, int *a
)
2480 bt
= type
->t
& VT_BTYPE
;
2481 if (bt
== VT_STRUCT
) {
2486 } else if (bt
== VT_PTR
) {
2487 if (type
->t
& VT_ARRAY
) {
2491 ts
= type_size(&s
->type
, a
);
2493 if (ts
< 0 && s
->c
< 0)
2501 } else if (bt
== VT_LDOUBLE
) {
2503 return LDOUBLE_SIZE
;
2504 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
2505 #ifdef TCC_TARGET_I386
2506 #ifdef TCC_TARGET_PE
2511 #elif defined(TCC_TARGET_ARM)
2521 } else if (bt
== VT_INT
|| bt
== VT_FLOAT
) {
2524 } else if (bt
== VT_SHORT
) {
2527 } else if (bt
== VT_QLONG
|| bt
== VT_QFLOAT
) {
2530 } else if (bt
== VT_ENUM
) {
2532 /* Enums might be incomplete, so don't just return '4' here. */
2533 return type
->ref
->c
;
2535 /* char, void, function, _Bool */
2541 /* push type size as known at runtime time on top of value stack. Put
2543 ST_FUNC
void vla_runtime_type_size(CType
*type
, int *a
)
2545 if (type
->t
& VT_VLA
) {
2546 type_size(&type
->ref
->type
, a
);
2547 vset(&int_type
, VT_LOCAL
|VT_LVAL
, type
->ref
->c
);
2549 vpushi(type_size(type
, a
));
2553 static void vla_sp_restore(void) {
2554 if (vlas_in_scope
) {
2555 gen_vla_sp_restore(vla_sp_loc
);
2559 static void vla_sp_restore_root(void) {
2560 if (vlas_in_scope
) {
2561 gen_vla_sp_restore(vla_sp_root_loc
);
2565 /* return the pointed type of t */
2566 static inline CType
*pointed_type(CType
*type
)
2568 return &type
->ref
->type
;
2571 /* modify type so that its it is a pointer to type. */
2572 ST_FUNC
void mk_pointer(CType
*type
)
2575 s
= sym_push(SYM_FIELD
, type
, 0, -1);
2576 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
2580 /* compare function types. OLD functions match any new functions */
2581 static int is_compatible_func(CType
*type1
, CType
*type2
)
2587 if (!is_compatible_types(&s1
->type
, &s2
->type
))
2589 /* check func_call */
2590 if (s1
->a
.func_call
!= s2
->a
.func_call
)
2592 /* XXX: not complete */
2593 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
2597 while (s1
!= NULL
) {
2600 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
2610 /* return true if type1 and type2 are the same. If unqualified is
2611 true, qualifiers on the types are ignored.
2613 - enums are not checked as gcc __builtin_types_compatible_p ()
2615 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2619 t1
= type1
->t
& VT_TYPE
;
2620 t2
= type2
->t
& VT_TYPE
;
2622 /* strip qualifiers before comparing */
2623 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2624 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2626 /* Default Vs explicit signedness only matters for char */
2627 if ((t1
& VT_BTYPE
) != VT_BYTE
) {
2631 /* An enum is compatible with (unsigned) int. Ideally we would
2632 store the enums signedness in type->ref.a.<some_bit> and
2633 only accept unsigned enums with unsigned int and vice versa.
2634 But one of our callers (gen_assign_cast) always strips VT_UNSIGNED
2635 from pointer target types, so we can't add it here either. */
2636 if ((t1
& VT_BTYPE
) == VT_ENUM
) {
2638 if (type1
->ref
->a
.unsigned_enum
)
2641 if ((t2
& VT_BTYPE
) == VT_ENUM
) {
2643 if (type2
->ref
->a
.unsigned_enum
)
2646 /* XXX: bitfields ? */
2649 /* test more complicated cases */
2650 bt1
= t1
& VT_BTYPE
;
2651 if (bt1
== VT_PTR
) {
2652 type1
= pointed_type(type1
);
2653 type2
= pointed_type(type2
);
2654 return is_compatible_types(type1
, type2
);
2655 } else if (bt1
== VT_STRUCT
) {
2656 return (type1
->ref
== type2
->ref
);
2657 } else if (bt1
== VT_FUNC
) {
2658 return is_compatible_func(type1
, type2
);
2664 /* return true if type1 and type2 are exactly the same (including
2667 static int is_compatible_types(CType
*type1
, CType
*type2
)
2669 return compare_types(type1
,type2
,0);
2672 /* return true if type1 and type2 are the same (ignoring qualifiers).
2674 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
2676 return compare_types(type1
,type2
,1);
2679 /* print a type. If 'varstr' is not NULL, then the variable is also
2680 printed in the type */
2682 /* XXX: add array and function pointers */
2683 static void type_to_str(char *buf
, int buf_size
,
2684 CType
*type
, const char *varstr
)
2691 t
= type
->t
& VT_TYPE
;
2694 if (t
& VT_CONSTANT
)
2695 pstrcat(buf
, buf_size
, "const ");
2696 if (t
& VT_VOLATILE
)
2697 pstrcat(buf
, buf_size
, "volatile ");
2698 if ((t
& (VT_DEFSIGN
| VT_UNSIGNED
)) == (VT_DEFSIGN
| VT_UNSIGNED
))
2699 pstrcat(buf
, buf_size
, "unsigned ");
2700 else if (t
& VT_DEFSIGN
)
2701 pstrcat(buf
, buf_size
, "signed ");
2731 tstr
= "long double";
2733 pstrcat(buf
, buf_size
, tstr
);
2737 if (bt
== VT_STRUCT
)
2741 pstrcat(buf
, buf_size
, tstr
);
2742 v
= type
->ref
->v
& ~SYM_STRUCT
;
2743 if (v
>= SYM_FIRST_ANOM
)
2744 pstrcat(buf
, buf_size
, "<anonymous>");
2746 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
2750 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
2751 pstrcat(buf
, buf_size
, "(");
2753 while (sa
!= NULL
) {
2754 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
2755 pstrcat(buf
, buf_size
, buf1
);
2758 pstrcat(buf
, buf_size
, ", ");
2760 pstrcat(buf
, buf_size
, ")");
2765 snprintf(buf1
, sizeof(buf1
), "%s[%ld]", varstr
? varstr
: "", s
->c
);
2766 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2769 pstrcpy(buf1
, sizeof(buf1
), "*");
2770 if (t
& VT_CONSTANT
)
2771 pstrcat(buf1
, buf_size
, "const ");
2772 if (t
& VT_VOLATILE
)
2773 pstrcat(buf1
, buf_size
, "volatile ");
2775 pstrcat(buf1
, sizeof(buf1
), varstr
);
2776 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2780 pstrcat(buf
, buf_size
, " ");
2781 pstrcat(buf
, buf_size
, varstr
);
2786 /* verify type compatibility to store vtop in 'dt' type, and generate
2788 static void gen_assign_cast(CType
*dt
)
2790 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
2791 char buf1
[256], buf2
[256];
2794 st
= &vtop
->type
; /* source type */
2795 dbt
= dt
->t
& VT_BTYPE
;
2796 sbt
= st
->t
& VT_BTYPE
;
2797 if (sbt
== VT_VOID
|| dbt
== VT_VOID
) {
2798 if (sbt
== VT_VOID
&& dbt
== VT_VOID
)
2800 It is Ok if both are void
2806 gcc accepts this program
2809 tcc_error("cannot cast from/to void");
2811 if (dt
->t
& VT_CONSTANT
)
2812 tcc_warning("assignment of read-only location");
2815 /* special cases for pointers */
2816 /* '0' can also be a pointer */
2817 if (is_null_pointer(vtop
))
2819 /* accept implicit pointer to integer cast with warning */
2820 if (is_integer_btype(sbt
)) {
2821 tcc_warning("assignment makes pointer from integer without a cast");
2824 type1
= pointed_type(dt
);
2825 /* a function is implicitely a function pointer */
2826 if (sbt
== VT_FUNC
) {
2827 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
2828 !is_compatible_types(pointed_type(dt
), st
))
2829 tcc_warning("assignment from incompatible pointer type");
2834 type2
= pointed_type(st
);
2835 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2836 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
2837 /* void * can match anything */
2839 /* exact type match, except for qualifiers */
2842 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2843 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2844 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2845 /* Like GCC don't warn by default for merely changes
2846 in pointer target signedness. Do warn for different
2847 base types, though, in particular for unsigned enums
2848 and signed int targets. */
2849 if ((tmp_type1
.t
& (VT_DEFSIGN
| VT_UNSIGNED
)) !=
2850 (tmp_type2
.t
& (VT_DEFSIGN
| VT_UNSIGNED
)) &&
2851 (tmp_type1
.t
& VT_BTYPE
) == (tmp_type2
.t
& VT_BTYPE
))
2854 tcc_warning("assignment from incompatible pointer type");
2857 /* check const and volatile */
2858 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
2859 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
2860 tcc_warning("assignment discards qualifiers from pointer target type");
2866 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
2867 tcc_warning("assignment makes integer from pointer without a cast");
2868 } else if (sbt
== VT_STRUCT
) {
2869 goto case_VT_STRUCT
;
2871 /* XXX: more tests */
2877 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2878 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2879 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2881 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
2882 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
2883 tcc_error("cannot cast '%s' to '%s'", buf1
, buf2
);
2891 /* store vtop in lvalue pushed on stack */
2892 ST_FUNC
void vstore(void)
2894 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
2896 ft
= vtop
[-1].type
.t
;
2897 sbt
= vtop
->type
.t
& VT_BTYPE
;
2898 dbt
= ft
& VT_BTYPE
;
2899 if ((((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2900 (sbt
== VT_INT
&& dbt
== VT_SHORT
))
2901 && !(vtop
->type
.t
& VT_BITFIELD
)) {
2902 /* optimize char/short casts */
2903 delayed_cast
= VT_MUSTCAST
;
2904 vtop
->type
.t
= (ft
& VT_TYPE
& ~VT_BITFIELD
&
2905 ((1 << VT_STRUCT_SHIFT
) - 1));
2906 /* XXX: factorize */
2907 if (ft
& VT_CONSTANT
)
2908 tcc_warning("assignment of read-only location");
2911 if (!(ft
& VT_BITFIELD
))
2912 gen_assign_cast(&vtop
[-1].type
);
2915 if (sbt
== VT_STRUCT
) {
2916 /* if structure, only generate pointer */
2917 /* structure assignment : generate memcpy */
2918 /* XXX: optimize if small size */
2919 size
= type_size(&vtop
->type
, &align
);
2923 vtop
->type
.t
= VT_PTR
;
2926 /* address of memcpy() */
2929 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2930 else if(!(align
& 3))
2931 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2934 /* Use memmove, rather than memcpy, as dest and src may be same: */
2935 vpush_global_sym(&func_old_type
, TOK_memmove
);
2940 vtop
->type
.t
= VT_PTR
;
2946 /* leave source on stack */
2947 } else if (ft
& VT_BITFIELD
) {
2948 /* bitfield store handling */
2950 /* save lvalue as expression result (example: s.b = s.a = n;) */
2951 vdup(), vtop
[-1] = vtop
[-2];
2953 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2954 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2955 /* remove bit field info to avoid loops */
2956 vtop
[-1].type
.t
= ft
& ~VT_BITFIELD
& ((1 << VT_STRUCT_SHIFT
) - 1);
2958 if((ft
& VT_BTYPE
) == VT_BOOL
) {
2959 gen_cast(&vtop
[-1].type
);
2960 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2963 /* duplicate destination */
2965 vtop
[-1] = vtop
[-2];
2967 /* mask and shift source */
2968 if((ft
& VT_BTYPE
) != VT_BOOL
) {
2969 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2970 vpushll((1ULL << bit_size
) - 1ULL);
2972 vpushi((1 << bit_size
) - 1);
2978 /* load destination, mask and or with source */
2980 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2981 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2983 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2989 /* ... and discard */
2993 #ifdef CONFIG_TCC_BCHECK
2994 /* bound check case */
2995 if (vtop
[-1].r
& VT_MUSTBOUND
) {
3004 #ifdef TCC_TARGET_X86_64
3005 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
3007 } else if ((ft
& VT_BTYPE
) == VT_QFLOAT
) {
3012 r
= gv(rc
); /* generate value */
3013 /* if lvalue was saved on stack, must read it */
3014 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
3016 t
= get_reg(RC_INT
);
3017 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
3022 sv
.r
= VT_LOCAL
| VT_LVAL
;
3023 sv
.c
.i
= vtop
[-1].c
.i
;
3025 vtop
[-1].r
= t
| VT_LVAL
;
3027 /* two word case handling : store second register at word + 4 (or +8 for x86-64) */
3028 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
3029 if (((ft
& VT_BTYPE
) == VT_QLONG
) || ((ft
& VT_BTYPE
) == VT_QFLOAT
)) {
3030 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
3032 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
3033 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
3035 vtop
[-1].type
.t
= load_type
;
3038 /* convert to int to increment easily */
3039 vtop
->type
.t
= addr_type
;
3045 vtop
[-1].type
.t
= load_type
;
3046 /* XXX: it works because r2 is spilled last ! */
3047 store(vtop
->r2
, vtop
- 1);
3053 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
3054 vtop
->r
|= delayed_cast
;
3058 /* post defines POST/PRE add. c is the token ++ or -- */
3059 ST_FUNC
void inc(int post
, int c
)
3062 vdup(); /* save lvalue */
3064 gv_dup(); /* duplicate value */
3069 vpushi(c
- TOK_MID
);
3071 vstore(); /* store value */
3073 vpop(); /* if post op, return saved value */
3076 ST_FUNC
void parse_mult_str (CString
*astr
, const char *msg
)
3078 /* read the string */
3082 while (tok
== TOK_STR
) {
3083 /* XXX: add \0 handling too ? */
3084 cstr_cat(astr
, tokc
.str
.data
, -1);
3087 cstr_ccat(astr
, '\0');
3090 /* If I is >= 1 and a power of two, returns log2(i)+1.
3091 If I is 0 returns 0. */
3092 static int exact_log2p1(int i
)
3097 for (ret
= 1; i
>= 1 << 8; ret
+= 8)
3108 /* Parse GNUC __attribute__ extension. Currently, the following
3109 extensions are recognized:
3110 - aligned(n) : set data/function alignment.
3111 - packed : force data alignment to 1
3112 - section(x) : generate data/code in this section.
3113 - unused : currently ignored, but may be used someday.
3114 - regparm(n) : pass function parameters in registers (i386 only)
3116 static void parse_attribute(AttributeDef
*ad
)
3121 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
3125 while (tok
!= ')') {
3126 if (tok
< TOK_IDENT
)
3127 expect("attribute name");
3134 parse_mult_str(&astr
, "section name");
3135 ad
->section
= find_section(tcc_state
, (char *)astr
.data
);
3142 parse_mult_str(&astr
, "alias(\"target\")");
3143 ad
->alias_target
= /* save string as token, for later */
3144 tok_alloc((char*)astr
.data
, astr
.size
-1)->tok
;
3148 case TOK_VISIBILITY1
:
3149 case TOK_VISIBILITY2
:
3151 parse_mult_str(&astr
,
3152 "visibility(\"default|hidden|internal|protected\")");
3153 if (!strcmp (astr
.data
, "default"))
3154 ad
->a
.visibility
= STV_DEFAULT
;
3155 else if (!strcmp (astr
.data
, "hidden"))
3156 ad
->a
.visibility
= STV_HIDDEN
;
3157 else if (!strcmp (astr
.data
, "internal"))
3158 ad
->a
.visibility
= STV_INTERNAL
;
3159 else if (!strcmp (astr
.data
, "protected"))
3160 ad
->a
.visibility
= STV_PROTECTED
;
3162 expect("visibility(\"default|hidden|internal|protected\")");
3171 if (n
<= 0 || (n
& (n
- 1)) != 0)
3172 tcc_error("alignment must be a positive power of two");
3177 ad
->a
.aligned
= exact_log2p1(n
);
3178 if (n
!= 1 << (ad
->a
.aligned
- 1))
3179 tcc_error("alignment of %d is larger than implemented", n
);
3191 /* currently, no need to handle it because tcc does not
3192 track unused objects */
3196 /* currently, no need to handle it because tcc does not
3197 track unused objects */
3202 ad
->a
.func_call
= FUNC_CDECL
;
3207 ad
->a
.func_call
= FUNC_STDCALL
;
3209 #ifdef TCC_TARGET_I386
3219 ad
->a
.func_call
= FUNC_FASTCALL1
+ n
- 1;
3225 ad
->a
.func_call
= FUNC_FASTCALLW
;
3232 ad
->a
.mode
= VT_LLONG
+ 1;
3235 ad
->a
.mode
= VT_BYTE
+ 1;
3238 ad
->a
.mode
= VT_SHORT
+ 1;
3242 ad
->a
.mode
= VT_INT
+ 1;
3245 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
3252 ad
->a
.func_export
= 1;
3255 ad
->a
.func_import
= 1;
3258 if (tcc_state
->warn_unsupported
)
3259 tcc_warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
3260 /* skip parameters */
3262 int parenthesis
= 0;
3266 else if (tok
== ')')
3269 } while (parenthesis
&& tok
!= -1);
3282 static Sym
* find_field (CType
*type
, int v
)
3286 while ((s
= s
->next
) != NULL
) {
3287 if ((s
->v
& SYM_FIELD
) &&
3288 (s
->type
.t
& VT_BTYPE
) == VT_STRUCT
&&
3289 (s
->v
& ~SYM_FIELD
) >= SYM_FIRST_ANOM
) {
3290 Sym
*ret
= find_field (&s
->type
, v
);
3300 static void struct_add_offset (Sym
*s
, int offset
)
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 struct_add_offset(s
->type
.ref
, offset
);
3312 static void struct_layout(CType
*type
, AttributeDef
*ad
)
3314 int align
, maxalign
, offset
, c
, bit_pos
, bt
, prevbt
, prev_bit_size
;
3315 int pcc
= !tcc_state
->ms_bitfields
;
3318 maxalign
= 1 << (ad
->a
.aligned
- 1);
3324 prevbt
= VT_STRUCT
; /* make it never match */
3326 for (f
= type
->ref
->next
; f
; f
= f
->next
) {
3327 int typealign
, bit_size
;
3328 int size
= type_size(&f
->type
, &typealign
);
3329 if (f
->type
.t
& VT_BITFIELD
)
3330 bit_size
= (f
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
3333 if (bit_size
== 0 && pcc
) {
3334 /* Zero-width bit-fields in PCC mode aren't affected
3335 by any packing (attribute or pragma). */
3337 } else if (f
->r
> 1) {
3339 } else if (ad
->a
.packed
|| f
->r
== 1) {
3341 /* Packed fields or packed records don't let the base type
3342 influence the records type alignment. */
3347 if (type
->ref
->type
.t
!= TOK_STRUCT
) {
3348 if (pcc
&& bit_size
>= 0)
3349 size
= (bit_size
+ 7) >> 3;
3350 /* Bit position is already zero from our caller. */
3354 } else if (bit_size
< 0) {
3355 int addbytes
= pcc
? (bit_pos
+ 7) >> 3 : 0;
3358 c
= (c
+ addbytes
+ align
- 1) & -align
;
3364 /* A bit-field. Layout is more complicated. There are two
3365 options TCC implements: PCC compatible and MS compatible
3366 (PCC compatible is what GCC uses for almost all targets).
3367 In PCC layout the overall size of the struct (in c) is
3368 _excluding_ the current run of bit-fields (that is,
3369 there's at least additional bit_pos bits after c). In
3370 MS layout c does include the current run of bit-fields.
3372 This matters for calculating the natural alignment buckets
3375 /* 'align' will be used to influence records alignment,
3376 so it's the max of specified and type alignment, except
3377 in certain cases that depend on the mode. */
3378 if (align
< typealign
)
3381 /* In PCC layout a non-packed bit-field is placed adjacent
3382 to the preceding bit-fields, except if it would overflow
3383 its container (depending on base type) or it's a zero-width
3384 bit-field. Packed non-zero-width bit-fields always are
3386 int ofs
= (c
* 8 + bit_pos
) % (typealign
* 8);
3387 int ofs2
= ofs
+ bit_size
+ (typealign
* 8) - 1;
3388 if (bit_size
== 0 ||
3389 ((typealign
!= 1 || size
== 1) &&
3390 (ofs2
/ (typealign
* 8)) > (size
/typealign
))) {
3391 c
= (c
+ ((bit_pos
+ 7) >> 3) + typealign
- 1) & -typealign
;
3393 } else while (bit_pos
+ bit_size
> size
* 8) {
3395 bit_pos
-= size
* 8;
3398 /* In PCC layout named bit-fields influence the alignment
3399 of the containing struct using the base types alignment,
3400 except for packed fields (which here have correct
3401 align/typealign). */
3402 if ((f
->v
& SYM_FIRST_ANOM
))
3405 bt
= f
->type
.t
& VT_BTYPE
;
3406 if ((bit_pos
+ bit_size
> size
* 8) ||
3407 (bit_size
> 0) == (bt
!= prevbt
)) {
3408 c
= (c
+ typealign
- 1) & -typealign
;
3411 /* In MS bitfield mode a bit-field run always uses
3412 at least as many bits as the underlying type.
3413 To start a new run it's also required that this
3414 or the last bit-field had non-zero width. */
3415 if (bit_size
|| prev_bit_size
)
3418 /* In MS layout the records alignment is normally
3419 influenced by the field, except for a zero-width
3420 field at the start of a run (but by further zero-width
3421 fields it is again). */
3422 if (bit_size
== 0 && prevbt
!= bt
)
3425 prev_bit_size
= bit_size
;
3427 f
->type
.t
= (f
->type
.t
& ~(0x3f << VT_STRUCT_SHIFT
))
3428 | (bit_pos
<< VT_STRUCT_SHIFT
);
3429 bit_pos
+= bit_size
;
3430 if (pcc
&& bit_pos
>= size
* 8) {
3432 bit_pos
-= size
* 8;
3435 if (align
> maxalign
)
3438 printf("set field %s offset=%d c=%d",
3439 get_tok_str(f
->v
& ~SYM_FIELD
, NULL
), offset
, c
);
3440 if (f
->type
.t
& VT_BITFIELD
) {
3441 printf(" pos=%d size=%d",
3442 (f
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
3443 (f
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
3448 if (f
->v
& SYM_FIRST_ANOM
&& (f
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
3450 /* An anonymous struct/union. Adjust member offsets
3451 to reflect the real offset of our containing struct.
3452 Also set the offset of this anon member inside
3453 the outer struct to be zero. Via this it
3454 works when accessing the field offset directly
3455 (from base object), as well as when recursing
3456 members in initializer handling. */
3457 int v2
= f
->type
.ref
->v
;
3458 if (!(v2
& SYM_FIELD
) &&
3459 (v2
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
3461 /* This happens only with MS extensions. The
3462 anon member has a named struct type, so it
3463 potentially is shared with other references.
3464 We need to unshare members so we can modify
3467 f
->type
.ref
= sym_push(anon_sym
++ | SYM_FIELD
,
3468 &f
->type
.ref
->type
, 0,
3470 pps
= &f
->type
.ref
->next
;
3471 while ((ass
= ass
->next
) != NULL
) {
3472 *pps
= sym_push(ass
->v
, &ass
->type
, 0, ass
->c
);
3473 pps
= &((*pps
)->next
);
3477 struct_add_offset(f
->type
.ref
, offset
);
3485 /* store size and alignment */
3486 type
->ref
->c
= (c
+ (pcc
? (bit_pos
+ 7) >> 3 : 0)
3487 + maxalign
- 1) & -maxalign
;
3488 type
->ref
->r
= maxalign
;
3491 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
3492 static void struct_decl(CType
*type
, AttributeDef
*ad
, int u
)
3494 int a
, v
, size
, align
, flexible
, alignoverride
;
3496 int bit_size
, bsize
, bt
;
3501 a
= tok
; /* save decl type */
3503 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3504 parse_attribute(ad
);
3508 /* struct already defined ? return it */
3510 expect("struct/union/enum name");
3512 if (s
&& (s
->scope
== local_scope
|| (tok
!= '{' && tok
!= ';'))) {
3514 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
3520 /* Record the original enum/struct/union token. */
3523 /* we put an undefined size for struct/union */
3524 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
3525 s
->r
= 0; /* default alignment is zero as gcc */
3526 /* put struct/union/enum name in type */
3534 tcc_error("struct/union/enum already defined");
3535 /* cannot be empty */
3537 /* non empty enums are not allowed */
3538 if (a
== TOK_ENUM
) {
3542 CType
*t
= &int_type
;
3545 expect("identifier");
3547 if (ss
&& !local_stack
)
3548 tcc_error("redefinition of enumerator '%s'",
3549 get_tok_str(v
, NULL
));
3553 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
3556 /* We really want to support long long enums
3557 on i386 as well, but the Sym structure only
3558 holds a 'long' for associated constants,
3559 and enlarging it would bump its size (no
3560 available padding). So punt for now. */
3566 if (c
!= (int)c
&& (unsigned long)c
!= (unsigned int)c
)
3567 seen_wide
= 1, t
= &size_type
;
3568 /* enum symbols have static storage */
3569 ss
= sym_push(v
, t
, VT_CONST
, c
);
3570 ss
->type
.t
|= VT_STATIC
;
3575 /* NOTE: we accept a trailing comma */
3580 s
->a
.unsigned_enum
= 1;
3581 s
->c
= type_size(seen_wide
? &size_type
: &int_type
, &align
);
3586 while (tok
!= '}') {
3587 if (!parse_btype(&btype
, &ad1
)) {
3593 tcc_error("flexible array member '%s' not at the end of struct",
3594 get_tok_str(v
, NULL
));
3599 type_decl(&type1
, &ad1
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3601 if ((type1
.t
& VT_BTYPE
) != VT_STRUCT
)
3602 expect("identifier");
3604 int v
= btype
.ref
->v
;
3605 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
3606 if (tcc_state
->ms_extensions
== 0)
3607 expect("identifier");
3611 if (type_size(&type1
, &align
) < 0) {
3612 if ((a
== TOK_STRUCT
) && (type1
.t
& VT_ARRAY
) && c
)
3615 tcc_error("field '%s' has incomplete type",
3616 get_tok_str(v
, NULL
));
3618 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
3619 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
3620 tcc_error("invalid type for '%s'",
3621 get_tok_str(v
, NULL
));
3625 bit_size
= expr_const();
3626 /* XXX: handle v = 0 case for messages */
3628 tcc_error("negative width in bit-field '%s'",
3629 get_tok_str(v
, NULL
));
3630 if (v
&& bit_size
== 0)
3631 tcc_error("zero width for bit-field '%s'",
3632 get_tok_str(v
, NULL
));
3633 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3634 parse_attribute(&ad1
);
3636 size
= type_size(&type1
, &align
);
3637 /* Only remember non-default alignment. */
3639 if (ad1
.a
.aligned
) {
3640 int speca
= 1 << (ad1
.a
.aligned
- 1);
3641 alignoverride
= speca
;
3642 } else if (ad1
.a
.packed
|| ad
->a
.packed
) {
3644 } else if (*tcc_state
->pack_stack_ptr
) {
3645 if (align
> *tcc_state
->pack_stack_ptr
)
3646 alignoverride
= *tcc_state
->pack_stack_ptr
;
3648 if (bit_size
>= 0) {
3649 bt
= type1
.t
& VT_BTYPE
;
3656 tcc_error("bitfields must have scalar type");
3658 if (bit_size
> bsize
) {
3659 tcc_error("width of '%s' exceeds its type",
3660 get_tok_str(v
, NULL
));
3661 } else if (bit_size
== bsize
) {
3662 /* no need for bit fields */
3665 type1
.t
|= VT_BITFIELD
|
3666 (0 << VT_STRUCT_SHIFT
) |
3667 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
3670 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
3671 /* Remember we've seen a real field to check
3672 for placement of flexible array member. */
3675 /* If member is a struct or bit-field, enforce
3676 placing into the struct (as anonymous). */
3678 ((type1
.t
& VT_BTYPE
) == VT_STRUCT
||
3683 ss
= sym_push(v
| SYM_FIELD
, &type1
, alignoverride
, 0);
3687 if (tok
== ';' || tok
== TOK_EOF
)
3694 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3695 parse_attribute(ad
);
3696 struct_layout(type
, ad
);
3701 /* return 1 if basic type is a type size (short, long, long long) */
3702 ST_FUNC
int is_btype_size(int bt
)
3704 return bt
== VT_SHORT
|| bt
== VT_LONG
|| bt
== VT_LLONG
;
3707 /* Add type qualifiers to a type. If the type is an array then the qualifiers
3708 are added to the element type, copied because it could be a typedef. */
3709 static void parse_btype_qualify(CType
*type
, int qualifiers
)
3711 while (type
->t
& VT_ARRAY
) {
3712 type
->ref
= sym_push(SYM_FIELD
, &type
->ref
->type
, 0, type
->ref
->c
);
3713 type
= &type
->ref
->type
;
3715 type
->t
|= qualifiers
;
3718 /* return 0 if no type declaration. otherwise, return the basic type
3721 static int parse_btype(CType
*type
, AttributeDef
*ad
)
3723 int t
, u
, bt_size
, complete
, type_found
, typespec_found
, g
;
3727 memset(ad
, 0, sizeof(AttributeDef
));
3735 /* currently, we really ignore extension */
3746 tcc_error("too many basic types");
3748 bt_size
= is_btype_size (u
& VT_BTYPE
);
3749 if (u
== VT_INT
|| (!bt_size
&& !(t
& VT_TYPEDEF
)))
3764 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
3765 #ifndef TCC_TARGET_PE
3766 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3768 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
3769 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3775 #ifdef TCC_TARGET_ARM64
3777 /* GCC's __uint128_t appears in some Linux header files. Make it a
3778 synonym for long double to get the size and alignment right. */
3790 if ((t
& VT_BTYPE
) == VT_LONG
) {
3791 #ifdef TCC_TARGET_PE
3792 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
3794 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3802 struct_decl(&type1
, ad
, VT_ENUM
);
3805 type
->ref
= type1
.ref
;
3809 struct_decl(&type1
, ad
, VT_STRUCT
);
3812 /* type modifiers */
3817 parse_btype_qualify(type
, VT_CONSTANT
);
3825 parse_btype_qualify(type
, VT_VOLATILE
);
3832 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == (VT_DEFSIGN
|VT_UNSIGNED
))
3833 tcc_error("signed and unsigned modifier");
3846 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == VT_DEFSIGN
)
3847 tcc_error("signed and unsigned modifier");
3848 t
|= VT_DEFSIGN
| VT_UNSIGNED
;
3864 if (t
& (VT_EXTERN
|VT_STATIC
|VT_TYPEDEF
) & ~g
)
3865 tcc_error("multiple storage classes");
3876 /* GNUC attribute */
3877 case TOK_ATTRIBUTE1
:
3878 case TOK_ATTRIBUTE2
:
3879 parse_attribute(ad
);
3882 t
= (t
& ~VT_BTYPE
) | u
;
3890 parse_expr_type(&type1
);
3891 /* remove all storage modifiers except typedef */
3892 type1
.t
&= ~(VT_STORAGE
&~VT_TYPEDEF
);
3898 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
3901 type
->t
= ((s
->type
.t
& ~VT_TYPEDEF
) |
3902 (t
& ~(VT_CONSTANT
| VT_VOLATILE
)));
3903 type
->ref
= s
->type
.ref
;
3904 if (t
& (VT_CONSTANT
| VT_VOLATILE
))
3905 parse_btype_qualify(type
, t
& (VT_CONSTANT
| VT_VOLATILE
));
3909 /* get attributes from typedef */
3910 if (0 == ad
->a
.aligned
)
3911 ad
->a
.aligned
= s
->a
.aligned
;
3912 if (0 == ad
->a
.func_call
)
3913 ad
->a
.func_call
= s
->a
.func_call
;
3914 ad
->a
.packed
|= s
->a
.packed
;
3923 if (tcc_state
->char_is_unsigned
) {
3924 if ((t
& (VT_DEFSIGN
|VT_BTYPE
)) == VT_BYTE
)
3928 /* long is never used as type */
3929 if ((t
& VT_BTYPE
) == VT_LONG
)
3930 #if (!defined TCC_TARGET_X86_64 && !defined TCC_TARGET_ARM64) || \
3931 defined TCC_TARGET_PE
3932 t
= (t
& ~VT_BTYPE
) | VT_INT
;
3934 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3940 /* convert a function parameter type (array to pointer and function to
3941 function pointer) */
3942 static inline void convert_parameter_type(CType
*pt
)
3944 /* remove const and volatile qualifiers (XXX: const could be used
3945 to indicate a const function parameter */
3946 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3947 /* array must be transformed to pointer according to ANSI C */
3949 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
3954 ST_FUNC
void parse_asm_str(CString
*astr
)
3957 parse_mult_str(astr
, "string constant");
3960 /* Parse an asm label and return the token */
3961 static int asm_label_instr(void)
3967 parse_asm_str(&astr
);
3970 printf("asm_alias: \"%s\"\n", (char *)astr
.data
);
3972 v
= tok_alloc(astr
.data
, astr
.size
- 1)->tok
;
3977 static void post_type(CType
*type
, AttributeDef
*ad
, int storage
)
3979 int n
, l
, t1
, arg_size
, align
;
3980 Sym
**plast
, *s
, *first
;
3985 /* function declaration */
3993 /* read param name and compute offset */
3994 if (l
!= FUNC_OLD
) {
3995 if (!parse_btype(&pt
, &ad1
)) {
3997 tcc_error("invalid type");
4004 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
4006 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
4007 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
4008 tcc_error("parameter declared as void");
4009 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
4014 expect("identifier");
4018 convert_parameter_type(&pt
);
4019 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
4025 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
4032 /* if no parameters, then old type prototype */
4036 /* NOTE: const is ignored in returned type as it has a special
4037 meaning in gcc / C++ */
4038 type
->t
&= ~VT_CONSTANT
;
4039 /* some ancient pre-K&R C allows a function to return an array
4040 and the array brackets to be put after the arguments, such
4041 that "int c()[]" means something like "int[] c()" */
4044 skip(']'); /* only handle simple "[]" */
4047 /* we push a anonymous symbol which will contain the function prototype */
4048 ad
->a
.func_args
= arg_size
;
4049 s
= sym_push(SYM_FIELD
, type
, 0, l
);
4054 } else if (tok
== '[') {
4055 int saved_nocode_wanted
= nocode_wanted
;
4056 /* array definition */
4058 if (tok
== TOK_RESTRICT1
)
4063 if (!local_stack
|| (storage
& VT_STATIC
))
4064 vpushi(expr_const());
4066 /* VLAs (which can only happen with local_stack && !VT_STATIC)
4067 length must always be evaluated, even under nocode_wanted,
4068 so that its size slot is initialized (e.g. under sizeof
4073 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4076 tcc_error("invalid array size");
4078 if (!is_integer_btype(vtop
->type
.t
& VT_BTYPE
))
4079 tcc_error("size of variable length array should be an integer");
4084 /* parse next post type */
4085 post_type(type
, ad
, storage
);
4086 if (type
->t
== VT_FUNC
)
4087 tcc_error("declaration of an array of functions");
4088 t1
|= type
->t
& VT_VLA
;
4091 loc
-= type_size(&int_type
, &align
);
4095 vla_runtime_type_size(type
, &align
);
4097 vset(&int_type
, VT_LOCAL
|VT_LVAL
, n
);
4103 nocode_wanted
= saved_nocode_wanted
;
4105 /* we push an anonymous symbol which will contain the array
4107 s
= sym_push(SYM_FIELD
, type
, 0, n
);
4108 type
->t
= (t1
? VT_VLA
: VT_ARRAY
) | VT_PTR
;
4113 /* Parse a type declaration (except basic type), and return the type
4114 in 'type'. 'td' is a bitmask indicating which kind of type decl is
4115 expected. 'type' should contain the basic type. 'ad' is the
4116 attribute definition of the basic type. It can be modified by
4119 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
4122 CType type1
, *type2
;
4123 int qualifiers
, storage
;
4125 while (tok
== '*') {
4133 qualifiers
|= VT_CONSTANT
;
4138 qualifiers
|= VT_VOLATILE
;
4144 /* XXX: clarify attribute handling */
4145 case TOK_ATTRIBUTE1
:
4146 case TOK_ATTRIBUTE2
:
4147 parse_attribute(ad
);
4151 type
->t
|= qualifiers
;
4154 /* recursive type */
4155 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
4156 type1
.t
= 0; /* XXX: same as int */
4159 /* XXX: this is not correct to modify 'ad' at this point, but
4160 the syntax is not clear */
4161 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
4162 parse_attribute(ad
);
4163 type_decl(&type1
, ad
, v
, td
);
4166 /* type identifier */
4167 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
4171 if (!(td
& TYPE_ABSTRACT
))
4172 expect("identifier");
4176 storage
= type
->t
& VT_STORAGE
;
4177 type
->t
&= ~VT_STORAGE
;
4178 post_type(type
, ad
, storage
);
4180 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
4181 parse_attribute(ad
);
4185 /* append type at the end of type1 */
4199 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
4200 ST_FUNC
int lvalue_type(int t
)
4205 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
4207 else if (bt
== VT_SHORT
)
4211 if (t
& VT_UNSIGNED
)
4212 r
|= VT_LVAL_UNSIGNED
;
4216 /* indirection with full error checking and bound check */
4217 ST_FUNC
void indir(void)
4219 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
4220 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
4224 if (vtop
->r
& VT_LVAL
)
4226 vtop
->type
= *pointed_type(&vtop
->type
);
4227 /* Arrays and functions are never lvalues */
4228 if (!(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_VLA
)
4229 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
4230 vtop
->r
|= lvalue_type(vtop
->type
.t
);
4231 /* if bound checking, the referenced pointer must be checked */
4232 #ifdef CONFIG_TCC_BCHECK
4233 if (tcc_state
->do_bounds_check
)
4234 vtop
->r
|= VT_MUSTBOUND
;
4239 /* pass a parameter to a function and do type checking and casting */
4240 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
4245 func_type
= func
->c
;
4246 if (func_type
== FUNC_OLD
||
4247 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
4248 /* default casting : only need to convert float to double */
4249 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
4252 } else if (vtop
->type
.t
& VT_BITFIELD
) {
4253 type
.t
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
4254 type
.ref
= vtop
->type
.ref
;
4257 } else if (arg
== NULL
) {
4258 tcc_error("too many arguments to function");
4261 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
4262 gen_assign_cast(&type
);
4266 /* parse an expression of the form '(type)' or '(expr)' and return its
4268 static void parse_expr_type(CType
*type
)
4274 if (parse_btype(type
, &ad
)) {
4275 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
4282 static void parse_type(CType
*type
)
4287 if (!parse_btype(type
, &ad
)) {
4290 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
4293 static void vpush_tokc(int t
)
4298 vsetc(&type
, VT_CONST
, &tokc
);
4301 ST_FUNC
void unary(void)
4303 int n
, t
, align
, size
, r
, sizeof_caller
;
4308 sizeof_caller
= in_sizeof
;
4310 /* XXX: GCC 2.95.3 does not generate a table although it should be
4324 vpush_tokc(VT_INT
| VT_UNSIGNED
);
4328 vpush_tokc(VT_LLONG
);
4332 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
4336 vpush_tokc(VT_FLOAT
);
4340 vpush_tokc(VT_DOUBLE
);
4344 vpush_tokc(VT_LDOUBLE
);
4347 case TOK___FUNCTION__
:
4349 goto tok_identifier
;
4355 /* special function name identifier */
4356 len
= strlen(funcname
) + 1;
4357 /* generate char[len] type */
4362 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
4363 ptr
= section_ptr_add(data_section
, len
);
4364 memcpy(ptr
, funcname
, len
);
4369 #ifdef TCC_TARGET_PE
4370 t
= VT_SHORT
| VT_UNSIGNED
;
4376 /* string parsing */
4379 if (tcc_state
->warn_write_strings
)
4384 memset(&ad
, 0, sizeof(AttributeDef
));
4385 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, 0);
4390 if (parse_btype(&type
, &ad
)) {
4391 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
4393 /* check ISOC99 compound literal */
4395 /* data is allocated locally by default */
4400 /* all except arrays are lvalues */
4401 if (!(type
.t
& VT_ARRAY
))
4402 r
|= lvalue_type(type
.t
);
4403 memset(&ad
, 0, sizeof(AttributeDef
));
4404 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, 0);
4406 if (sizeof_caller
) {
4413 } else if (tok
== '{') {
4414 int saved_nocode_wanted
= nocode_wanted
;
4416 tcc_error("expected constant");
4417 /* save all registers */
4419 /* statement expression : we do not accept break/continue
4420 inside as GCC does. We do retain the nocode_wanted state,
4421 as statement expressions can't ever be entered from the
4422 outside, so any reactivation of code emission (from labels
4423 or loop heads) can be disabled again after the end of it. */
4424 block(NULL
, NULL
, 1);
4425 nocode_wanted
= saved_nocode_wanted
;
4440 /* functions names must be treated as function pointers,
4441 except for unary '&' and sizeof. Since we consider that
4442 functions are not lvalues, we only have to handle it
4443 there and in function calls. */
4444 /* arrays can also be used although they are not lvalues */
4445 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
4446 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
4448 mk_pointer(&vtop
->type
);
4454 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4456 boolean
.t
= VT_BOOL
;
4458 vtop
->c
.i
= !vtop
->c
.i
;
4459 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
4463 vseti(VT_JMP
, gvtst(1, 0));
4475 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
4476 tcc_error("pointer not accepted for unary plus");
4477 /* In order to force cast, we add zero, except for floating point
4478 where we really need an noop (otherwise -0.0 will be transformed
4480 if (!is_float(vtop
->type
.t
)) {
4491 unary_type(&type
); // Perform a in_sizeof = 0;
4492 size
= type_size(&type
, &align
);
4493 if (t
== TOK_SIZEOF
) {
4494 if (!(type
.t
& VT_VLA
)) {
4496 tcc_error("sizeof applied to an incomplete type");
4499 vla_runtime_type_size(&type
, &align
);
4504 vtop
->type
.t
|= VT_UNSIGNED
;
4507 case TOK_builtin_expect
:
4509 /* __builtin_expect is a no-op for now */
4519 case TOK_builtin_types_compatible_p
:
4528 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
4529 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
4530 vpushi(is_compatible_types(&type1
, &type2
));
4533 case TOK_builtin_choose_expr
:
4560 case TOK_builtin_constant_p
:
4567 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
4574 case TOK_builtin_frame_address
:
4575 case TOK_builtin_return_address
:
4582 if (tok
!= TOK_CINT
) {
4583 tcc_error("%s only takes positive integers",
4584 tok1
== TOK_builtin_return_address
?
4585 "__builtin_return_address" :
4586 "__builtin_frame_address");
4588 level
= (uint32_t)tokc
.i
;
4593 vset(&type
, VT_LOCAL
, 0); /* local frame */
4595 mk_pointer(&vtop
->type
);
4596 indir(); /* -> parent frame */
4598 if (tok1
== TOK_builtin_return_address
) {
4599 // assume return address is just above frame pointer on stack
4602 mk_pointer(&vtop
->type
);
4607 #ifdef TCC_TARGET_X86_64
4608 #ifdef TCC_TARGET_PE
4609 case TOK_builtin_va_start
:
4617 if ((vtop
->r
& VT_VALMASK
) != VT_LOCAL
)
4618 tcc_error("__builtin_va_start expects a local variable");
4619 vtop
->r
&= ~(VT_LVAL
| VT_REF
);
4620 vtop
->type
= char_pointer_type
;
4626 case TOK_builtin_va_arg_types
:
4633 vpushi(classify_x86_64_va_arg(&type
));
4639 #ifdef TCC_TARGET_ARM64
4640 case TOK___va_start
: {
4650 vtop
->type
.t
= VT_VOID
;
4653 case TOK___va_arg
: {
4666 case TOK___arm64_clear_cache
: {
4675 vtop
->type
.t
= VT_VOID
;
4679 /* pre operations */
4690 t
= vtop
->type
.t
& VT_BTYPE
;
4692 /* In IEEE negate(x) isn't subtract(0,x), but rather
4696 vtop
->c
.f
= -1.0 * 0.0;
4697 else if (t
== VT_DOUBLE
)
4698 vtop
->c
.d
= -1.0 * 0.0;
4700 vtop
->c
.ld
= -1.0 * 0.0;
4708 goto tok_identifier
;
4710 /* allow to take the address of a label */
4711 if (tok
< TOK_UIDENT
)
4712 expect("label identifier");
4713 s
= label_find(tok
);
4715 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4717 if (s
->r
== LABEL_DECLARED
)
4718 s
->r
= LABEL_FORWARD
;
4721 s
->type
.t
= VT_VOID
;
4722 mk_pointer(&s
->type
);
4723 s
->type
.t
|= VT_STATIC
;
4725 vpushsym(&s
->type
, s
);
4729 // special qnan , snan and infinity values
4731 vpush64(VT_DOUBLE
, 0x7ff8000000000000ULL
);
4735 vpush64(VT_DOUBLE
, 0x7ff0000000000001ULL
);
4739 vpush64(VT_DOUBLE
, 0x7ff0000000000000ULL
);
4748 expect("identifier");
4751 const char *name
= get_tok_str(t
, NULL
);
4753 tcc_error("'%s' undeclared", name
);
4754 /* for simple function calls, we tolerate undeclared
4755 external reference to int() function */
4756 if (tcc_state
->warn_implicit_function_declaration
4757 #ifdef TCC_TARGET_PE
4758 /* people must be warned about using undeclared WINAPI functions
4759 (which usually start with uppercase letter) */
4760 || (name
[0] >= 'A' && name
[0] <= 'Z')
4763 tcc_warning("implicit declaration of function '%s'", name
);
4764 s
= external_global_sym(t
, &func_old_type
, 0);
4768 /* A symbol that has a register is a local register variable,
4769 which starts out as VT_LOCAL value. */
4770 if ((r
& VT_VALMASK
) < VT_CONST
)
4771 r
= (r
& ~VT_VALMASK
) | VT_LOCAL
;
4773 vset(&s
->type
, r
, s
->c
);
4774 /* Point to s as backpointer (even without r&VT_SYM).
4775 Will be used by at least the x86 inline asm parser for
4778 if (vtop
->r
& VT_SYM
) {
4784 /* post operations */
4786 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
4789 } else if (tok
== '.' || tok
== TOK_ARROW
|| tok
== TOK_CDOUBLE
) {
4792 if (tok
== TOK_ARROW
)
4794 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
4797 /* expect pointer on structure */
4798 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
4799 expect("struct or union");
4800 if (tok
== TOK_CDOUBLE
)
4801 expect("field name");
4803 if (tok
== TOK_CINT
|| tok
== TOK_CUINT
)
4804 expect("field name");
4805 s
= find_field(&vtop
->type
, tok
);
4807 tcc_error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, &tokc
));
4808 /* add field offset to pointer */
4809 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
4812 /* change type to field type, and set to lvalue */
4813 vtop
->type
= s
->type
;
4814 vtop
->type
.t
|= qualifiers
;
4815 /* an array is never an lvalue */
4816 if (!(vtop
->type
.t
& VT_ARRAY
)) {
4817 vtop
->r
|= lvalue_type(vtop
->type
.t
);
4818 #ifdef CONFIG_TCC_BCHECK
4819 /* if bound checking, the referenced pointer must be checked */
4820 if (tcc_state
->do_bounds_check
&& (vtop
->r
& VT_VALMASK
) != VT_LOCAL
)
4821 vtop
->r
|= VT_MUSTBOUND
;
4825 } else if (tok
== '[') {
4831 } else if (tok
== '(') {
4834 int nb_args
, ret_nregs
, ret_align
, regsize
, variadic
;
4837 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
4838 /* pointer test (no array accepted) */
4839 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
4840 vtop
->type
= *pointed_type(&vtop
->type
);
4841 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
4845 expect("function pointer");
4848 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
4850 /* get return type */
4853 sa
= s
->next
; /* first parameter */
4854 nb_args
= regsize
= 0;
4856 /* compute first implicit argument if a structure is returned */
4857 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
4858 variadic
= (s
->c
== FUNC_ELLIPSIS
);
4859 ret_nregs
= gfunc_sret(&s
->type
, variadic
, &ret
.type
,
4860 &ret_align
, ®size
);
4862 /* get some space for the returned structure */
4863 size
= type_size(&s
->type
, &align
);
4864 #ifdef TCC_TARGET_ARM64
4865 /* On arm64, a small struct is return in registers.
4866 It is much easier to write it to memory if we know
4867 that we are allowed to write some extra bytes, so
4868 round the allocated space up to a power of 2: */
4870 while (size
& (size
- 1))
4871 size
= (size
| (size
- 1)) + 1;
4873 loc
= (loc
- size
) & -align
;
4875 ret
.r
= VT_LOCAL
| VT_LVAL
;
4876 /* pass it as 'int' to avoid structure arg passing
4878 vseti(VT_LOCAL
, loc
);
4888 /* return in register */
4889 if (is_float(ret
.type
.t
)) {
4890 ret
.r
= reg_fret(ret
.type
.t
);
4891 #ifdef TCC_TARGET_X86_64
4892 if ((ret
.type
.t
& VT_BTYPE
) == VT_QFLOAT
)
4896 #ifndef TCC_TARGET_ARM64
4897 #ifdef TCC_TARGET_X86_64
4898 if ((ret
.type
.t
& VT_BTYPE
) == VT_QLONG
)
4900 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
4911 gfunc_param_typed(s
, sa
);
4921 tcc_error("too few arguments to function");
4923 gfunc_call(nb_args
);
4926 for (r
= ret
.r
+ ret_nregs
+ !ret_nregs
; r
-- > ret
.r
;) {
4927 vsetc(&ret
.type
, r
, &ret
.c
);
4928 vtop
->r2
= ret
.r2
; /* Loop only happens when r2 is VT_CONST */
4931 /* handle packed struct return */
4932 if (((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) && ret_nregs
) {
4935 size
= type_size(&s
->type
, &align
);
4936 /* We're writing whole regs often, make sure there's enough
4937 space. Assume register size is power of 2. */
4938 if (regsize
> align
)
4940 loc
= (loc
- size
) & -align
;
4944 vset(&ret
.type
, VT_LOCAL
| VT_LVAL
, addr
+ offset
);
4948 if (--ret_nregs
== 0)
4952 vset(&s
->type
, VT_LOCAL
| VT_LVAL
, addr
);
4960 ST_FUNC
void expr_prod(void)
4965 while (tok
== '*' || tok
== '/' || tok
== '%') {
4973 ST_FUNC
void expr_sum(void)
4978 while (tok
== '+' || tok
== '-') {
4986 static void expr_shift(void)
4991 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
4999 static void expr_cmp(void)
5004 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
5005 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
5013 static void expr_cmpeq(void)
5018 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
5026 static void expr_and(void)
5029 while (tok
== '&') {
5036 static void expr_xor(void)
5039 while (tok
== '^') {
5046 static void expr_or(void)
5049 while (tok
== '|') {
5056 static void expr_land(void)
5059 if (tok
== TOK_LAND
) {
5062 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
5070 while (tok
== TOK_LAND
) {
5078 gen_cast(&int_type
);
5086 if (tok
!= TOK_LAND
) {
5099 static void expr_lor(void)
5102 if (tok
== TOK_LOR
) {
5105 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
5113 while (tok
== TOK_LOR
) {
5121 gen_cast(&int_type
);
5129 if (tok
!= TOK_LOR
) {
5142 /* Assuming vtop is a value used in a conditional context
5143 (i.e. compared with zero) return 0 if it's false, 1 if
5144 true and -1 if it can't be statically determined. */
5145 static int condition_3way(void)
5148 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
&&
5149 (!(vtop
->r
& VT_SYM
) ||
5150 !(vtop
->sym
->type
.t
& VT_WEAK
))) {
5152 boolean
.t
= VT_BOOL
;
5161 static void expr_cond(void)
5163 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
, islv
, c
, g
;
5165 CType type
, type1
, type2
;
5170 c
= condition_3way();
5171 g
= (tok
== ':' && gnu_ext
);
5173 /* needed to avoid having different registers saved in
5175 if (is_float(vtop
->type
.t
)) {
5177 #ifdef TCC_TARGET_X86_64
5178 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
5203 sv
= *vtop
; /* save value to handle it later */
5204 vtop
--; /* no vpop so that FP stack is not flushed */
5222 bt1
= t1
& VT_BTYPE
;
5224 bt2
= t2
& VT_BTYPE
;
5225 /* cast operands to correct type according to ISOC rules */
5226 if (is_float(bt1
) || is_float(bt2
)) {
5227 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
5228 type
.t
= VT_LDOUBLE
;
5230 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
5235 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
5236 /* cast to biggest op */
5238 /* convert to unsigned if it does not fit in a long long */
5239 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
5240 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
5241 type
.t
|= VT_UNSIGNED
;
5242 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
5243 /* If one is a null ptr constant the result type
5245 if (is_null_pointer (vtop
))
5247 else if (is_null_pointer (&sv
))
5249 /* XXX: test pointer compatibility, C99 has more elaborate
5253 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
5254 /* XXX: test function pointer compatibility */
5255 type
= bt1
== VT_FUNC
? type1
: type2
;
5256 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
5257 /* XXX: test structure compatibility */
5258 type
= bt1
== VT_STRUCT
? type1
: type2
;
5259 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
5260 /* NOTE: as an extension, we accept void on only one side */
5263 /* integer operations */
5265 /* convert to unsigned if it does not fit in an integer */
5266 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
5267 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
5268 type
.t
|= VT_UNSIGNED
;
5270 /* keep structs lvalue by transforming `(expr ? a : b)` to `*(expr ? &a : &b)` so
5271 that `(expr ? a : b).mem` does not error with "lvalue expected" */
5272 islv
= (vtop
->r
& VT_LVAL
) && (sv
.r
& VT_LVAL
) && VT_STRUCT
== (type
.t
& VT_BTYPE
);
5275 /* now we convert second operand */
5279 mk_pointer(&vtop
->type
);
5281 } else if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
5286 if (is_float(type
.t
)) {
5288 #ifdef TCC_TARGET_X86_64
5289 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
5293 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
5294 /* for long longs, we use fixed registers to avoid having
5295 to handle a complicated move */
5306 /* this is horrible, but we must also convert first
5312 mk_pointer(&vtop
->type
);
5314 } else if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
5320 move_reg(r2
, r1
, type
.t
);
5330 static void expr_eq(void)
5336 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
5337 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
5338 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
5353 ST_FUNC
void gexpr(void)
5364 /* parse an expression and return its type without any side effect. */
5365 static void expr_type(CType
*type
)
5375 /* parse a unary expression and return its type without any side
5377 static void unary_type(CType
*type
)
5386 /* parse a constant expression and return value in vtop. */
5387 static void expr_const1(void)
5394 /* parse an integer constant and return its value. */
5395 static inline int64_t expr_const64(void)
5399 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
5400 expect("constant expression");
5406 /* parse an integer constant and return its value.
5407 Complain if it doesn't fit 32bit (signed or unsigned). */
5408 ST_FUNC
int expr_const(void)
5411 int64_t wc
= expr_const64();
5413 if (c
!= wc
&& (unsigned)c
!= wc
)
5414 tcc_error("constant exceeds 32 bit");
5418 /* return the label token if current token is a label, otherwise
5420 static int is_label(void)
5424 /* fast test first */
5425 if (tok
< TOK_UIDENT
)
5427 /* no need to save tokc because tok is an identifier */
5434 unget_tok(last_tok
);
5439 static void label_or_decl(int l
)
5443 /* fast test first */
5444 if (tok
>= TOK_UIDENT
)
5446 /* no need to save tokc because tok is an identifier */
5450 unget_tok(last_tok
);
5453 unget_tok(last_tok
);
5458 #ifndef TCC_TARGET_ARM64
5459 static void gfunc_return(CType
*func_type
)
5461 if ((func_type
->t
& VT_BTYPE
) == VT_STRUCT
) {
5462 CType type
, ret_type
;
5463 int ret_align
, ret_nregs
, regsize
;
5464 ret_nregs
= gfunc_sret(func_type
, func_var
, &ret_type
,
5465 &ret_align
, ®size
);
5466 if (0 == ret_nregs
) {
5467 /* if returning structure, must copy it to implicit
5468 first pointer arg location */
5471 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
5474 /* copy structure value to pointer */
5477 /* returning structure packed into registers */
5478 int r
, size
, addr
, align
;
5479 size
= type_size(func_type
,&align
);
5480 if ((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) ||
5481 (vtop
->c
.i
& (ret_align
-1)))
5482 && (align
& (ret_align
-1))) {
5483 loc
= (loc
- size
) & -ret_align
;
5486 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
5490 vset(&ret_type
, VT_LOCAL
| VT_LVAL
, addr
);
5492 vtop
->type
= ret_type
;
5493 if (is_float(ret_type
.t
))
5494 r
= rc_fret(ret_type
.t
);
5505 if (--ret_nregs
== 0)
5507 /* We assume that when a structure is returned in multiple
5508 registers, their classes are consecutive values of the
5511 vtop
->c
.i
+= regsize
;
5515 } else if (is_float(func_type
->t
)) {
5516 gv(rc_fret(func_type
->t
));
5520 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
5524 static int case_cmp(const void *pa
, const void *pb
)
5526 int64_t a
= (*(struct case_t
**) pa
)->v1
;
5527 int64_t b
= (*(struct case_t
**) pb
)->v1
;
5528 return a
< b
? -1 : a
> b
;
5531 static void gcase(struct case_t
**base
, int len
, int *bsym
)
5535 int ll
= (vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
;
5553 gtst_addr(0, p
->sym
); /* v1 <= x <= v2 */
5555 gcase(base
, len
/2, bsym
);
5556 if (cur_switch
->def_sym
)
5557 gjmp_addr(cur_switch
->def_sym
);
5559 *bsym
= gjmp(*bsym
);
5563 base
+= e
; len
-= e
;
5573 if (p
->v1
== p
->v2
) {
5575 gtst_addr(0, p
->sym
);
5585 gtst_addr(0, p
->sym
);
5591 static void block(int *bsym
, int *csym
, int is_expr
)
5593 int a
, b
, c
, d
, cond
;
5596 /* generate line number info */
5597 if (tcc_state
->do_debug
)
5598 tcc_debug_line(tcc_state
);
5601 /* default return value is (void) */
5603 vtop
->type
.t
= VT_VOID
;
5606 if (tok
== TOK_IF
) {
5608 int saved_nocode_wanted
= nocode_wanted
;
5613 cond
= condition_3way();
5619 nocode_wanted
|= 0x20000000;
5620 block(bsym
, csym
, 0);
5622 nocode_wanted
= saved_nocode_wanted
;
5624 if (c
== TOK_ELSE
) {
5629 nocode_wanted
|= 0x20000000;
5630 block(bsym
, csym
, 0);
5631 gsym(d
); /* patch else jmp */
5633 nocode_wanted
= saved_nocode_wanted
;
5636 } else if (tok
== TOK_WHILE
) {
5637 int saved_nocode_wanted
;
5638 nocode_wanted
&= ~0x20000000;
5648 saved_nocode_wanted
= nocode_wanted
;
5650 nocode_wanted
= saved_nocode_wanted
;
5655 } else if (tok
== '{') {
5657 int block_vla_sp_loc
= vla_sp_loc
, saved_vlas_in_scope
= vlas_in_scope
;
5660 /* record local declaration stack position */
5662 llabel
= local_label_stack
;
5665 /* handle local labels declarations */
5666 if (tok
== TOK_LABEL
) {
5669 if (tok
< TOK_UIDENT
)
5670 expect("label identifier");
5671 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
5681 while (tok
!= '}') {
5682 label_or_decl(VT_LOCAL
);
5686 block(bsym
, csym
, is_expr
);
5689 /* pop locally defined labels */
5690 label_pop(&local_label_stack
, llabel
);
5691 /* pop locally defined symbols */
5693 /* In the is_expr case (a statement expression is finished here),
5694 vtop might refer to symbols on the local_stack. Either via the
5695 type or via vtop->sym. We can't pop those nor any that in turn
5696 might be referred to. To make it easier we don't roll back
5697 any symbols in that case; some upper level call to block() will
5698 do that. We do have to remove such symbols from the lookup
5699 tables, though. sym_pop will do that. */
5700 sym_pop(&local_stack
, s
, is_expr
);
5702 /* Pop VLA frames and restore stack pointer if required */
5703 if (vlas_in_scope
> saved_vlas_in_scope
) {
5704 vla_sp_loc
= saved_vlas_in_scope
? block_vla_sp_loc
: vla_sp_root_loc
;
5707 vlas_in_scope
= saved_vlas_in_scope
;
5710 } else if (tok
== TOK_RETURN
) {
5714 gen_assign_cast(&func_vt
);
5715 gfunc_return(&func_vt
);
5718 /* jump unless last stmt in top-level block */
5719 if (tok
!= '}' || local_scope
!= 1)
5721 nocode_wanted
|= 0x20000000;
5722 } else if (tok
== TOK_BREAK
) {
5725 tcc_error("cannot break");
5726 *bsym
= gjmp(*bsym
);
5729 nocode_wanted
|= 0x20000000;
5730 } else if (tok
== TOK_CONTINUE
) {
5733 tcc_error("cannot continue");
5734 vla_sp_restore_root();
5735 *csym
= gjmp(*csym
);
5738 } else if (tok
== TOK_FOR
) {
5740 int saved_nocode_wanted
;
5741 nocode_wanted
&= ~0x20000000;
5747 /* c99 for-loop init decl? */
5748 if (!decl0(VT_LOCAL
, 1)) {
5749 /* no, regular for-loop init expr */
5775 saved_nocode_wanted
= nocode_wanted
;
5777 nocode_wanted
= saved_nocode_wanted
;
5782 sym_pop(&local_stack
, s
, 0);
5785 if (tok
== TOK_DO
) {
5786 int saved_nocode_wanted
;
5787 nocode_wanted
&= ~0x20000000;
5793 saved_nocode_wanted
= nocode_wanted
;
5801 nocode_wanted
= saved_nocode_wanted
;
5806 if (tok
== TOK_SWITCH
) {
5807 struct switch_t
*saved
, sw
;
5808 int saved_nocode_wanted
= nocode_wanted
;
5814 switchval
= *vtop
--;
5816 b
= gjmp(0); /* jump to first case */
5817 sw
.p
= NULL
; sw
.n
= 0; sw
.def_sym
= 0;
5821 nocode_wanted
= saved_nocode_wanted
;
5822 a
= gjmp(a
); /* add implicit break */
5825 qsort(sw
.p
, sw
.n
, sizeof(void*), case_cmp
);
5826 for (b
= 1; b
< sw
.n
; b
++)
5827 if (sw
.p
[b
- 1]->v2
>= sw
.p
[b
]->v1
)
5828 tcc_error("duplicate case value");
5829 /* Our switch table sorting is signed, so the compared
5830 value needs to be as well when it's 64bit. */
5831 if ((switchval
.type
.t
& VT_BTYPE
) == VT_LLONG
)
5832 switchval
.type
.t
&= ~VT_UNSIGNED
;
5834 gcase(sw
.p
, sw
.n
, &a
);
5837 gjmp_addr(sw
.def_sym
);
5838 dynarray_reset(&sw
.p
, &sw
.n
);
5843 if (tok
== TOK_CASE
) {
5844 struct case_t
*cr
= tcc_malloc(sizeof(struct case_t
));
5847 nocode_wanted
&= ~0x20000000;
5849 cr
->v1
= cr
->v2
= expr_const64();
5850 if (gnu_ext
&& tok
== TOK_DOTS
) {
5852 cr
->v2
= expr_const64();
5853 if (cr
->v2
< cr
->v1
)
5854 tcc_warning("empty case range");
5857 dynarray_add(&cur_switch
->p
, &cur_switch
->n
, cr
);
5860 goto block_after_label
;
5862 if (tok
== TOK_DEFAULT
) {
5867 if (cur_switch
->def_sym
)
5868 tcc_error("too many 'default'");
5869 cur_switch
->def_sym
= ind
;
5871 goto block_after_label
;
5873 if (tok
== TOK_GOTO
) {
5875 if (tok
== '*' && gnu_ext
) {
5879 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
5882 } else if (tok
>= TOK_UIDENT
) {
5883 s
= label_find(tok
);
5884 /* put forward definition if needed */
5886 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
5888 if (s
->r
== LABEL_DECLARED
)
5889 s
->r
= LABEL_FORWARD
;
5891 vla_sp_restore_root();
5892 if (s
->r
& LABEL_FORWARD
)
5893 s
->jnext
= gjmp(s
->jnext
);
5895 gjmp_addr(s
->jnext
);
5898 expect("label identifier");
5901 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
5909 if (s
->r
== LABEL_DEFINED
)
5910 tcc_error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
5912 s
->r
= LABEL_DEFINED
;
5914 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
5918 /* we accept this, but it is a mistake */
5920 nocode_wanted
&= ~0x20000000;
5922 tcc_warning("deprecated use of label at end of compound statement");
5926 block(bsym
, csym
, is_expr
);
5929 /* expression case */
5944 #define EXPR_CONST 1
5947 static void parse_init_elem(int expr_type
)
5949 int saved_global_expr
;
5952 /* compound literals must be allocated globally in this case */
5953 saved_global_expr
= global_expr
;
5956 global_expr
= saved_global_expr
;
5957 /* NOTE: symbols are accepted */
5958 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
5959 #ifdef TCC_TARGET_PE
5960 || (vtop
->type
.t
& VT_IMPORT
)
5963 tcc_error("initializer element is not constant");
5971 /* t is the array or struct type. c is the array or struct
5972 address. cur_field is the pointer to the current
5973 value, for arrays the 'c' member contains the current start
5974 index and the 'r' contains the end index (in case of range init).
5975 'size_only' is true if only size info is needed (only used
5977 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
5978 Sym
**cur_field
, int size_only
)
5981 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
5987 if (gnu_ext
&& (l
= is_label()) != 0)
5989 while (tok
== '[' || tok
== '.') {
5991 if (!(type
->t
& VT_ARRAY
))
5992 expect("array type");
5995 index
= expr_const();
5996 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
5997 tcc_error("invalid index");
5998 if (tok
== TOK_DOTS
&& gnu_ext
) {
6000 index_last
= expr_const();
6001 if (index_last
< 0 ||
6002 (s
->c
>= 0 && index_last
>= s
->c
) ||
6004 tcc_error("invalid index");
6010 (*cur_field
)->c
= index
;
6011 (*cur_field
)->r
= index_last
;
6013 type
= pointed_type(type
);
6014 elem_size
= type_size(type
, &align
);
6015 c
+= index
* elem_size
;
6016 /* NOTE: we only support ranges for last designator */
6017 nb_elems
= index_last
- index
+ 1;
6018 if (nb_elems
!= 1) {
6027 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
6028 expect("struct/union type");
6029 f
= find_field(type
, l
);
6034 /* XXX: fix this mess by using explicit storage field */
6036 type1
.t
|= (type
->t
& ~VT_TYPE
);
6050 if (type
->t
& VT_ARRAY
) {
6051 index
= (*cur_field
)->c
;
6052 if (type
->ref
->c
>= 0 && index
>= type
->ref
->c
)
6053 tcc_error("index too large");
6054 type
= pointed_type(type
);
6055 c
+= index
* type_size(type
, &align
);
6058 while (f
&& (f
->v
& SYM_FIRST_ANOM
) && (f
->type
.t
& VT_BITFIELD
))
6059 *cur_field
= f
= f
->next
;
6061 tcc_error("too many field init");
6062 /* XXX: fix this mess by using explicit storage field */
6064 type1
.t
|= (type
->t
& ~VT_TYPE
);
6069 decl_initializer(type
, sec
, c
, 0, size_only
);
6071 /* XXX: make it more general */
6072 if (!size_only
&& nb_elems
> 1) {
6073 unsigned long c_end
;
6078 vset(type
, VT_LOCAL
|VT_LVAL
, c
);
6079 for (i
= 1; i
< nb_elems
; i
++) {
6080 vset(type
, VT_LOCAL
|VT_LVAL
, c
+ elem_size
* i
);
6086 c_end
= c
+ nb_elems
* elem_size
;
6087 if (c_end
> sec
->data_allocated
)
6088 section_realloc(sec
, c_end
);
6089 src
= sec
->data
+ c
;
6091 for(i
= 1; i
< nb_elems
; i
++) {
6093 memcpy(dst
, src
, elem_size
);
6099 /* store a value or an expression directly in global data or in local array */
6100 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
)
6102 int bt
, bit_pos
, bit_size
;
6104 unsigned long long bit_mask
;
6108 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
6112 /* XXX: not portable */
6113 /* XXX: generate error if incorrect relocation */
6114 gen_assign_cast(&dtype
);
6115 bt
= type
->t
& VT_BTYPE
;
6116 size
= type_size(type
, &align
);
6117 if (c
+ size
> sec
->data_allocated
) {
6118 section_realloc(sec
, c
+ size
);
6120 ptr
= sec
->data
+ c
;
6121 /* XXX: make code faster ? */
6122 if (!(type
->t
& VT_BITFIELD
)) {
6124 bit_size
= PTR_SIZE
* 8;
6127 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
6128 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
6129 bit_mask
= (1LL << bit_size
) - 1;
6131 if ((vtop
->r
& (VT_SYM
|VT_CONST
)) == (VT_SYM
|VT_CONST
) &&
6132 vtop
->sym
->v
>= SYM_FIRST_ANOM
&&
6133 /* XXX This rejects compount literals like
6134 '(void *){ptr}'. The problem is that '&sym' is
6135 represented the same way, which would be ruled out
6136 by the SYM_FIRST_ANOM check above, but also '"string"'
6137 in 'char *p = "string"' is represented the same
6138 with the type being VT_PTR and the symbol being an
6139 anonymous one. That is, there's no difference in vtop
6140 between '(void *){x}' and '&(void *){x}'. Ignore
6141 pointer typed entities here. Hopefully no real code
6142 will every use compound literals with scalar type. */
6143 (vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
6144 /* These come from compound literals, memcpy stuff over. */
6148 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[vtop
->sym
->c
];
6149 ssec
= tcc_state
->sections
[esym
->st_shndx
];
6150 memmove (ptr
, ssec
->data
+ esym
->st_value
, size
);
6152 /* We need to copy over all memory contents, and that
6153 includes relocations. Use the fact that relocs are
6154 created it order, so look from the end of relocs
6155 until we hit one before the copied region. */
6156 int num_relocs
= ssec
->reloc
->data_offset
/ sizeof(*rel
);
6157 rel
= (ElfW_Rel
*)(ssec
->reloc
->data
+ ssec
->reloc
->data_offset
);
6158 while (num_relocs
--) {
6160 if (rel
->r_offset
>= esym
->st_value
+ size
)
6162 if (rel
->r_offset
< esym
->st_value
)
6164 /* Note: if the same fields are initialized multiple
6165 times (possible with designators) then we possibly
6166 add multiple relocations for the same offset here.
6167 That would lead to wrong code, the last reloc needs
6168 to win. We clean this up later after the whole
6169 initializer is parsed. */
6170 put_elf_reloca(symtab_section
, sec
,
6171 c
+ rel
->r_offset
- esym
->st_value
,
6172 ELFW(R_TYPE
)(rel
->r_info
),
6173 ELFW(R_SYM
)(rel
->r_info
),
6174 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
6183 if ((vtop
->r
& VT_SYM
) &&
6189 (bt
== VT_LLONG
&& bit_size
!= 64) ||
6193 (bt
== VT_INT
&& bit_size
!= 32)
6196 tcc_error("initializer element is not computable at load time");
6198 /* XXX: when cross-compiling we assume that each type has the
6199 same representation on host and target, which is likely to
6200 be wrong in the case of long double */
6202 vtop
->c
.i
= (vtop
->c
.i
!= 0);
6204 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
6207 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
6210 *(double *)ptr
= vtop
->c
.d
;
6213 if (sizeof(long double) == LDOUBLE_SIZE
)
6214 *(long double *)ptr
= vtop
->c
.ld
;
6215 else if (sizeof(double) == LDOUBLE_SIZE
)
6216 *(double *)ptr
= vtop
->c
.ld
;
6218 tcc_error("can't cross compile long double constants");
6222 *(long long *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
6229 addr_t val
= (vtop
->c
.i
& bit_mask
) << bit_pos
;
6230 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
6231 if (vtop
->r
& VT_SYM
)
6232 greloca(sec
, vtop
->sym
, c
, R_DATA_PTR
, val
);
6234 *(addr_t
*)ptr
|= val
;
6236 if (vtop
->r
& VT_SYM
)
6237 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
6238 *(addr_t
*)ptr
|= val
;
6244 int val
= (vtop
->c
.i
& bit_mask
) << bit_pos
;
6245 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
6246 if (vtop
->r
& VT_SYM
)
6247 greloca(sec
, vtop
->sym
, c
, R_DATA_PTR
, val
);
6251 if (vtop
->r
& VT_SYM
)
6252 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
6261 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
6268 /* put zeros for variable based init */
6269 static void init_putz(Section
*sec
, unsigned long c
, int size
)
6272 /* nothing to do because globals are already set to zero */
6274 vpush_global_sym(&func_old_type
, TOK_memset
);
6276 #ifdef TCC_TARGET_ARM
6287 /* 't' contains the type and storage info. 'c' is the offset of the
6288 object in section 'sec'. If 'sec' is NULL, it means stack based
6289 allocation. 'first' is true if array '{' must be read (multi
6290 dimension implicit array init handling). 'size_only' is true if
6291 size only evaluation is wanted (only for arrays). */
6292 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
6293 int first
, int size_only
)
6295 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, parlevel1
, i
;
6302 /* If we currently are at an '}' or ',' we have read an initializer
6303 element in one of our callers, and not yet consumed it. */
6304 have_elem
= tok
== '}' || tok
== ',';
6305 if (!have_elem
&& tok
!= '{' &&
6306 /* In case of strings we have special handling for arrays, so
6307 don't consume them as initializer value (which would commit them
6308 to some anonymous symbol). */
6309 tok
!= TOK_LSTR
&& tok
!= TOK_STR
&&
6311 parse_init_elem(!sec
? EXPR_ANY
: EXPR_CONST
);
6316 !(type
->t
& VT_ARRAY
) &&
6317 /* Use i_c_parameter_t, to strip toplevel qualifiers.
6318 The source type might have VT_CONSTANT set, which is
6319 of course assignable to non-const elements. */
6320 is_compatible_parameter_types(type
, &vtop
->type
)) {
6321 init_putv(type
, sec
, c
);
6322 } else if (type
->t
& VT_ARRAY
) {
6326 t1
= pointed_type(type
);
6327 size1
= type_size(t1
, &align1
);
6330 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
6333 tcc_error("character array initializer must be a literal,"
6334 " optionally enclosed in braces");
6339 /* only parse strings here if correct type (otherwise: handle
6340 them as ((w)char *) expressions */
6341 if ((tok
== TOK_LSTR
&&
6342 #ifdef TCC_TARGET_PE
6343 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
6345 (t1
->t
& VT_BTYPE
) == VT_INT
6347 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
6348 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
6351 /* compute maximum number of chars wanted */
6353 cstr_len
= tokc
.str
.size
;
6355 cstr_len
= tokc
.str
.size
/ sizeof(nwchar_t
);
6358 if (n
>= 0 && nb
> (n
- array_length
))
6359 nb
= n
- array_length
;
6362 tcc_warning("initializer-string for array is too long");
6363 /* in order to go faster for common case (char
6364 string in global variable, we handle it
6366 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
6367 memcpy(sec
->data
+ c
+ array_length
, tokc
.str
.data
, nb
);
6371 ch
= ((unsigned char *)tokc
.str
.data
)[i
];
6373 ch
= ((nwchar_t
*)tokc
.str
.data
)[i
];
6375 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
);
6382 /* only add trailing zero if enough storage (no
6383 warning in this case since it is standard) */
6384 if (n
< 0 || array_length
< n
) {
6387 init_putv(t1
, sec
, c
+ (array_length
* size1
));
6397 while (tok
!= '}' || have_elem
) {
6398 decl_designator(type
, sec
, c
, &f
, size_only
);
6401 /* must put zero in holes (note that doing it that way
6402 ensures that it even works with designators) */
6403 if (!size_only
&& array_length
< index
) {
6404 init_putz(sec
, c
+ array_length
* size1
,
6405 (index
- array_length
) * size1
);
6407 if (type
->t
& VT_ARRAY
) {
6408 index
= indexsym
.c
= ++indexsym
.r
;
6410 index
= index
+ type_size(&f
->type
, &align1
);
6411 if (s
->type
.t
== TOK_UNION
)
6416 if (index
> array_length
)
6417 array_length
= index
;
6419 if (type
->t
& VT_ARRAY
) {
6420 /* special test for multi dimensional arrays (may not
6421 be strictly correct if designators are used at the
6423 if (no_oblock
&& index
>= n
)
6426 if (no_oblock
&& f
== NULL
)
6434 /* put zeros at the end */
6435 if (!size_only
&& array_length
< n
) {
6436 init_putz(sec
, c
+ array_length
* size1
,
6437 (n
- array_length
) * size1
);
6441 /* patch type size if needed, which happens only for array types */
6443 s
->c
= array_length
;
6444 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
6447 if (first
|| tok
== '{') {
6456 } else if (tok
== '{') {
6458 decl_initializer(type
, sec
, c
, first
, size_only
);
6460 } else if (size_only
) {
6461 /* If we supported only ISO C we wouldn't have to accept calling
6462 this on anything than an array size_only==1 (and even then
6463 only on the outermost level, so no recursion would be needed),
6464 because initializing a flex array member isn't supported.
6465 But GNU C supports it, so we need to recurse even into
6466 subfields of structs and arrays when size_only is set. */
6467 /* just skip expression */
6468 parlevel
= parlevel1
= 0;
6469 while ((parlevel
> 0 || parlevel1
> 0 ||
6470 (tok
!= '}' && tok
!= ',')) && tok
!= -1) {
6473 else if (tok
== ')') {
6474 if (parlevel
== 0 && parlevel1
== 0)
6478 else if (tok
== '{')
6480 else if (tok
== '}') {
6481 if (parlevel
== 0 && parlevel1
== 0)
6489 /* This should happen only when we haven't parsed
6490 the init element above for fear of committing a
6491 string constant to memory too early. */
6492 if (tok
!= TOK_STR
&& tok
!= TOK_LSTR
)
6493 expect("string constant");
6494 parse_init_elem(!sec
? EXPR_ANY
: EXPR_CONST
);
6496 init_putv(type
, sec
, c
);
6500 /* parse an initializer for type 't' if 'has_init' is non zero, and
6501 allocate space in local or global data space ('r' is either
6502 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
6503 variable 'v' of scope 'scope' is declared before initializers
6504 are parsed. If 'v' is zero, then a reference to the new object
6505 is put in the value stack. If 'has_init' is 2, a special parsing
6506 is done to handle string constants. */
6507 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
6508 int has_init
, int v
, int scope
)
6510 int size
, align
, addr
, data_offset
;
6512 ParseState saved_parse_state
= {0};
6513 TokenString
*init_str
= NULL
;
6515 Sym
*flexible_array
;
6517 flexible_array
= NULL
;
6518 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
6519 Sym
*field
= type
->ref
->next
;
6522 field
= field
->next
;
6523 if (field
->type
.t
& VT_ARRAY
&& field
->type
.ref
->c
< 0)
6524 flexible_array
= field
;
6528 size
= type_size(type
, &align
);
6529 /* If unknown size, we must evaluate it before
6530 evaluating initializers because
6531 initializers can generate global data too
6532 (e.g. string pointers or ISOC99 compound
6533 literals). It also simplifies local
6534 initializers handling */
6535 if (size
< 0 || (flexible_array
&& has_init
)) {
6537 tcc_error("unknown type size");
6538 /* get all init string */
6539 init_str
= tok_str_alloc();
6540 if (has_init
== 2) {
6541 /* only get strings */
6542 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
6543 tok_str_add_tok(init_str
);
6548 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
6550 tcc_error("unexpected end of file in initializer");
6551 tok_str_add_tok(init_str
);
6554 else if (tok
== '}') {
6564 tok_str_add(init_str
, -1);
6565 tok_str_add(init_str
, 0);
6568 save_parse_state(&saved_parse_state
);
6570 begin_macro(init_str
, 1);
6572 decl_initializer(type
, NULL
, 0, 1, 1);
6573 /* prepare second initializer parsing */
6574 macro_ptr
= init_str
->str
;
6577 /* if still unknown size, error */
6578 size
= type_size(type
, &align
);
6580 tcc_error("unknown type size");
6582 /* If there's a flex member and it was used in the initializer
6584 if (flexible_array
&&
6585 flexible_array
->type
.ref
->c
> 0)
6586 size
+= flexible_array
->type
.ref
->c
6587 * pointed_size(&flexible_array
->type
);
6588 /* take into account specified alignment if bigger */
6589 if (ad
->a
.aligned
) {
6590 int speca
= 1 << (ad
->a
.aligned
- 1);
6593 } else if (ad
->a
.packed
) {
6596 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
6598 #ifdef CONFIG_TCC_BCHECK
6599 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
6603 loc
= (loc
- size
) & -align
;
6605 #ifdef CONFIG_TCC_BCHECK
6606 /* handles bounds */
6607 /* XXX: currently, since we do only one pass, we cannot track
6608 '&' operators, so we add only arrays */
6609 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
6611 /* add padding between regions */
6613 /* then add local bound info */
6614 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(addr_t
));
6615 bounds_ptr
[0] = addr
;
6616 bounds_ptr
[1] = size
;
6620 /* local variable */
6621 #ifdef CONFIG_TCC_ASM
6622 if (ad
->asm_label
) {
6623 int reg
= asm_parse_regvar(ad
->asm_label
);
6625 r
= (r
& ~VT_VALMASK
) | reg
;
6628 sym_push(v
, type
, r
, addr
);
6630 /* push local reference */
6631 vset(type
, r
, addr
);
6635 if (v
&& scope
== VT_CONST
) {
6636 /* see if the symbol was already defined */
6639 patch_storage(sym
, type
);
6640 if (sym
->type
.t
& VT_EXTERN
) {
6641 /* if the variable is extern, it was not allocated */
6642 sym
->type
.t
&= ~VT_EXTERN
;
6643 /* set array size if it was omitted in extern
6645 if ((sym
->type
.t
& VT_ARRAY
) &&
6646 sym
->type
.ref
->c
< 0 &&
6648 sym
->type
.ref
->c
= type
->ref
->c
;
6649 } else if (!has_init
) {
6650 /* we accept several definitions of the same
6651 global variable. this is tricky, because we
6652 must play with the SHN_COMMON type of the symbol */
6653 /* no init data, we won't add more to the symbol */
6654 update_storage(sym
);
6656 } else if (sym
->c
) {
6658 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
6659 if (esym
->st_shndx
== data_section
->sh_num
)
6660 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
6665 /* allocate symbol in corresponding section */
6670 else if (tcc_state
->nocommon
)
6675 data_offset
= sec
->data_offset
;
6676 data_offset
= (data_offset
+ align
- 1) & -align
;
6678 /* very important to increment global pointer at this time
6679 because initializers themselves can create new initializers */
6680 data_offset
+= size
;
6681 #ifdef CONFIG_TCC_BCHECK
6682 /* add padding if bound check */
6683 if (tcc_state
->do_bounds_check
)
6686 sec
->data_offset
= data_offset
;
6687 /* allocate section space to put the data */
6688 if (sec
->sh_type
!= SHT_NOBITS
&&
6689 data_offset
> sec
->data_allocated
)
6690 section_realloc(sec
, data_offset
);
6691 /* align section if needed */
6692 if (align
> sec
->sh_addralign
)
6693 sec
->sh_addralign
= align
;
6695 addr
= 0; /* avoid warning */
6699 if (scope
!= VT_CONST
|| !sym
) {
6700 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
6701 sym
->asm_label
= ad
->asm_label
;
6703 /* update symbol definition */
6705 put_extern_sym(sym
, sec
, addr
, size
);
6707 put_extern_sym(sym
, SECTION_COMMON
, align
, size
);
6711 /* push global reference */
6712 sym
= get_sym_ref(type
, sec
, addr
, size
);
6713 vpushsym(type
, sym
);
6716 #ifdef CONFIG_TCC_BCHECK
6717 /* handles bounds now because the symbol must be defined
6718 before for the relocation */
6719 if (tcc_state
->do_bounds_check
) {
6722 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
6723 /* then add global bound info */
6724 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(addr_t
));
6725 bounds_ptr
[0] = 0; /* relocated */
6726 bounds_ptr
[1] = size
;
6731 if (type
->t
& VT_VLA
) {
6734 /* save current stack pointer */
6735 if (vlas_in_scope
== 0) {
6736 if (vla_sp_root_loc
== -1)
6737 vla_sp_root_loc
= (loc
-= PTR_SIZE
);
6738 gen_vla_sp_save(vla_sp_root_loc
);
6741 vla_runtime_type_size(type
, &a
);
6742 gen_vla_alloc(type
, a
);
6743 gen_vla_sp_save(addr
);
6747 } else if (has_init
) {
6748 size_t oldreloc_offset
= 0;
6749 if (sec
&& sec
->reloc
)
6750 oldreloc_offset
= sec
->reloc
->data_offset
;
6751 decl_initializer(type
, sec
, addr
, 1, 0);
6752 if (sec
&& sec
->reloc
)
6753 squeeze_multi_relocs(sec
, oldreloc_offset
);
6754 /* patch flexible array member size back to -1, */
6755 /* for possible subsequent similar declarations */
6757 flexible_array
->type
.ref
->c
= -1;
6761 /* restore parse state if needed */
6764 restore_parse_state(&saved_parse_state
);
6768 /* parse an old style function declaration list */
6769 /* XXX: check multiple parameter */
6770 static void func_decl_list(Sym
*func_sym
)
6777 /* parse each declaration */
6778 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
&&
6779 tok
!= TOK_ASM1
&& tok
!= TOK_ASM2
&& tok
!= TOK_ASM3
) {
6780 if (!parse_btype(&btype
, &ad
))
6781 expect("declaration list");
6782 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
6783 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
6785 /* we accept no variable after */
6789 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
6790 /* find parameter in function parameter list */
6793 if ((s
->v
& ~SYM_FIELD
) == v
)
6797 tcc_error("declaration for parameter '%s' but no such parameter",
6798 get_tok_str(v
, NULL
));
6800 /* check that no storage specifier except 'register' was given */
6801 if (type
.t
& VT_STORAGE
)
6802 tcc_error("storage class specified for '%s'", get_tok_str(v
, NULL
));
6803 convert_parameter_type(&type
);
6804 /* we can add the type (NOTE: it could be local to the function) */
6806 /* accept other parameters */
6817 /* parse a function defined by symbol 'sym' and generate its code in
6818 'cur_text_section' */
6819 static void gen_function(Sym
*sym
)
6822 ind
= cur_text_section
->data_offset
;
6823 /* NOTE: we patch the symbol size later */
6824 put_extern_sym(sym
, cur_text_section
, ind
, 0);
6825 funcname
= get_tok_str(sym
->v
, NULL
);
6827 /* Initialize VLA state */
6829 vla_sp_root_loc
= -1;
6830 /* put debug symbol */
6831 tcc_debug_funcstart(tcc_state
, sym
);
6832 /* push a dummy symbol to enable local sym storage */
6833 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
6834 local_scope
= 1; /* for function parameters */
6835 gfunc_prolog(&sym
->type
);
6838 block(NULL
, NULL
, 0);
6842 cur_text_section
->data_offset
= ind
;
6843 label_pop(&global_label_stack
, NULL
);
6844 /* reset local stack */
6846 sym_pop(&local_stack
, NULL
, 0);
6847 /* end of function */
6848 /* patch symbol size */
6849 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
6851 tcc_debug_funcend(tcc_state
, ind
- func_ind
);
6852 /* It's better to crash than to generate wrong code */
6853 cur_text_section
= NULL
;
6854 funcname
= ""; /* for safety */
6855 func_vt
.t
= VT_VOID
; /* for safety */
6856 func_var
= 0; /* for safety */
6857 ind
= 0; /* for safety */
6862 static void gen_inline_functions(TCCState
*s
)
6865 int inline_generated
, i
, ln
;
6866 struct InlineFunc
*fn
;
6868 ln
= file
->line_num
;
6869 /* iterate while inline function are referenced */
6871 inline_generated
= 0;
6872 for (i
= 0; i
< s
->nb_inline_fns
; ++i
) {
6873 fn
= s
->inline_fns
[i
];
6875 if (sym
&& sym
->c
) {
6876 /* the function was used: generate its code and
6877 convert it to a normal function */
6880 pstrcpy(file
->filename
, sizeof file
->filename
, fn
->filename
);
6881 sym
->type
.t
&= ~VT_INLINE
;
6883 begin_macro(fn
->func_str
, 1);
6885 cur_text_section
= text_section
;
6889 inline_generated
= 1;
6892 if (!inline_generated
)
6895 file
->line_num
= ln
;
6898 ST_FUNC
void free_inline_functions(TCCState
*s
)
6901 /* free tokens of unused inline functions */
6902 for (i
= 0; i
< s
->nb_inline_fns
; ++i
) {
6903 struct InlineFunc
*fn
= s
->inline_fns
[i
];
6905 tok_str_free(fn
->func_str
);
6907 dynarray_reset(&s
->inline_fns
, &s
->nb_inline_fns
);
6910 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
6911 static int decl0(int l
, int is_for_loop_init
)
6919 if (!parse_btype(&btype
, &ad
)) {
6920 if (is_for_loop_init
)
6922 /* skip redundant ';' */
6923 /* XXX: find more elegant solution */
6928 if (l
== VT_CONST
&&
6929 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
6930 /* global asm block */
6934 /* special test for old K&R protos without explicit int
6935 type. Only accepted when defining global data */
6936 if (l
== VT_LOCAL
|| tok
< TOK_UIDENT
)
6940 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
6941 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
6943 if ((btype
.t
& VT_BTYPE
) == VT_STRUCT
) {
6944 int v
= btype
.ref
->v
;
6945 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) >= SYM_FIRST_ANOM
)
6946 tcc_warning("unnamed struct/union that defines no instances");
6951 while (1) { /* iterate thru each declaration */
6953 /* If the base type itself was an array type of unspecified
6954 size (like in 'typedef int arr[]; arr x = {1};') then
6955 we will overwrite the unknown size by the real one for
6956 this decl. We need to unshare the ref symbol holding
6958 if ((type
.t
& VT_ARRAY
) && type
.ref
->c
< 0) {
6959 type
.ref
= sym_push(SYM_FIELD
, &type
.ref
->type
, 0, type
.ref
->c
);
6961 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
6965 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
6966 printf("type = '%s'\n", buf
);
6969 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
6970 if ((type
.t
& VT_STATIC
) && (l
== VT_LOCAL
)) {
6971 tcc_error("function without file scope cannot be static");
6973 /* if old style function prototype, we accept a
6976 if (sym
->c
== FUNC_OLD
)
6977 func_decl_list(sym
);
6980 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
6981 ad
.asm_label
= asm_label_instr();
6982 /* parse one last attribute list, after asm label */
6983 parse_attribute(&ad
);
6990 #ifdef TCC_TARGET_PE
6991 if (ad
.a
.func_import
|| ad
.a
.func_export
) {
6992 if (type
.t
& (VT_STATIC
|VT_TYPEDEF
))
6993 tcc_error("cannot have dll linkage with static or typedef");
6994 if (ad
.a
.func_export
)
6995 type
.t
|= VT_EXPORT
;
6996 else if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
6997 type
.t
|= VT_IMPORT
|VT_EXTERN
;
7000 type
.t
|= ad
.a
.visibility
<< VT_VIS_SHIFT
;
7004 tcc_error("cannot use local functions");
7005 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
7006 expect("function definition");
7008 /* reject abstract declarators in function definition */
7010 while ((sym
= sym
->next
) != NULL
)
7011 if (!(sym
->v
& ~SYM_FIELD
))
7012 expect("identifier");
7014 /* XXX: cannot do better now: convert extern line to static inline */
7015 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
7016 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
7021 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
7024 ref
= sym
->type
.ref
;
7026 /* use func_call from prototype if not defined */
7027 if (ref
->a
.func_call
!= FUNC_CDECL
7028 && type
.ref
->a
.func_call
== FUNC_CDECL
)
7029 type
.ref
->a
.func_call
= ref
->a
.func_call
;
7031 /* use static from prototype */
7032 if (sym
->type
.t
& VT_STATIC
)
7033 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
7035 /* If the definition has no visibility use the
7036 one from prototype. */
7037 if (! (type
.t
& VT_VIS_MASK
))
7038 type
.t
|= sym
->type
.t
& VT_VIS_MASK
;
7040 /* apply other storage attributes from prototype */
7041 type
.t
|= sym
->type
.t
& (VT_EXPORT
|VT_WEAK
);
7043 if (!is_compatible_types(&sym
->type
, &type
)) {
7045 tcc_error("incompatible types for redefinition of '%s'",
7046 get_tok_str(v
, NULL
));
7048 if (ref
->a
.func_body
)
7049 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
7050 /* if symbol is already defined, then put complete type */
7054 /* put function symbol */
7055 sym
= global_identifier_push(v
, type
.t
, 0);
7056 sym
->type
.ref
= type
.ref
;
7059 sym
->type
.ref
->a
.func_body
= 1;
7060 sym
->r
= VT_SYM
| VT_CONST
;
7062 /* static inline functions are just recorded as a kind
7063 of macro. Their code will be emitted at the end of
7064 the compilation unit only if they are used */
7065 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
7066 (VT_INLINE
| VT_STATIC
)) {
7068 struct InlineFunc
*fn
;
7069 const char *filename
;
7071 filename
= file
? file
->filename
: "";
7072 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
7073 strcpy(fn
->filename
, filename
);
7075 fn
->func_str
= tok_str_alloc();
7081 tcc_error("unexpected end of file");
7082 tok_str_add_tok(fn
->func_str
);
7087 } else if (t
== '}') {
7089 if (block_level
== 0)
7093 tok_str_add(fn
->func_str
, -1);
7094 tok_str_add(fn
->func_str
, 0);
7095 dynarray_add(&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
7098 /* compute text section */
7099 cur_text_section
= ad
.section
;
7100 if (!cur_text_section
)
7101 cur_text_section
= text_section
;
7106 if (type
.t
& VT_TYPEDEF
) {
7107 /* save typedefed type */
7108 /* XXX: test storage specifiers ? */
7110 if (sym
&& sym
->scope
== local_scope
) {
7111 if (!is_compatible_types(&sym
->type
, &type
)
7112 || !(sym
->type
.t
& VT_TYPEDEF
))
7113 tcc_error("incompatible redefinition of '%s'",
7114 get_tok_str(v
, NULL
));
7117 sym
= sym_push(v
, &type
, 0, 0);
7122 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
7123 /* external function definition */
7124 /* specific case for func_call attribute */
7126 } else if (!(type
.t
& VT_ARRAY
)) {
7127 /* not lvalue if array */
7128 r
|= lvalue_type(type
.t
);
7130 has_init
= (tok
== '=');
7131 if (has_init
&& (type
.t
& VT_VLA
))
7132 tcc_error("variable length array cannot be initialized");
7133 if ((type
.t
& VT_EXTERN
) || ((type
.t
& VT_BTYPE
) == VT_FUNC
) ||
7134 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
7135 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
7136 /* external variable or function */
7137 /* NOTE: as GCC, uninitialized global static
7138 arrays of null size are considered as
7140 sym
= external_sym(v
, &type
, r
);
7141 sym
->asm_label
= ad
.asm_label
;
7142 if (ad
.alias_target
) {
7147 alias_target
= sym_find(ad
.alias_target
);
7148 if (!alias_target
|| !alias_target
->c
)
7149 tcc_error("unsupported forward __alias__ attribute");
7150 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[alias_target
->c
];
7151 tsec
.sh_num
= esym
->st_shndx
;
7152 put_extern_sym2(sym
, &tsec
, esym
->st_value
, esym
->st_size
, 0);
7155 if (type
.t
& VT_STATIC
)
7161 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, l
);
7165 if (is_for_loop_init
)
7178 ST_FUNC
void decl(int l
)
7183 /* ------------------------------------------------------------------------- */