2 * TCC - Tiny C Compiler
4 * Copyright (c) 2001-2004 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 /********************************************************/
24 /* global variables */
26 /* loc : local variable index
27 ind : output code index
29 anon_sym: anonymous symbol index
31 ST_DATA
int rsym
, anon_sym
, ind
, loc
;
33 ST_DATA Sym
*sym_free_first
;
34 ST_DATA
void **sym_pools
;
35 ST_DATA
int nb_sym_pools
;
37 ST_DATA Sym
*global_stack
;
38 ST_DATA Sym
*local_stack
;
39 ST_DATA Sym
*define_stack
;
40 ST_DATA Sym
*global_label_stack
;
41 ST_DATA Sym
*local_label_stack
;
42 static int local_scope
;
44 static int section_sym
;
46 ST_DATA
int vlas_in_scope
; /* number of VLAs that are currently in scope */
47 ST_DATA
int vla_sp_root_loc
; /* vla_sp_loc for SP before any VLAs were pushed */
48 ST_DATA
int vla_sp_loc
; /* Pointer to variable holding location to store stack pointer on the stack when modifying stack pointer */
50 ST_DATA SValue __vstack
[1+VSTACK_SIZE
], *vtop
, *pvtop
;
52 ST_DATA
int const_wanted
; /* true if constant wanted */
53 ST_DATA
int nocode_wanted
; /* true if no code generation wanted for an expression */
54 ST_DATA
int global_expr
; /* true if compound literals must be allocated globally (used during initializers parsing */
55 ST_DATA CType func_vt
; /* current function return type (used by return instruction) */
56 ST_DATA
int func_var
; /* true if current function is variadic (used by return instruction) */
58 ST_DATA
int last_line_num
, last_ind
, func_ind
; /* debug last line number and pc */
59 ST_DATA
const char *funcname
;
61 ST_DATA CType char_pointer_type
, func_old_type
, int_type
, size_type
;
63 ST_DATA
struct switch_t
{
67 } **p
; int n
; /* list of case ranges */
68 int def_sym
; /* default symbol */
69 } *cur_switch
; /* current switch */
71 /* ------------------------------------------------------------------------- */
73 static void gen_cast(CType
*type
);
74 static inline CType
*pointed_type(CType
*type
);
75 static int is_compatible_types(CType
*type1
, CType
*type2
);
76 static int parse_btype(CType
*type
, AttributeDef
*ad
);
77 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
);
78 static void parse_expr_type(CType
*type
);
79 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
, int first
, int size_only
);
80 static void block(int *bsym
, int *csym
, int is_expr
);
81 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
, int has_init
, int v
, int scope
);
82 static int decl0(int l
, int is_for_loop_init
);
83 static void expr_eq(void);
84 static void expr_lor_const(void);
85 static void unary_type(CType
*type
);
86 static void vla_runtime_type_size(CType
*type
, int *a
);
87 static void vla_sp_restore(void);
88 static void vla_sp_restore_root(void);
89 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
);
90 static void expr_type(CType
*type
);
91 static inline int64_t expr_const64(void);
92 ST_FUNC
void vpush64(int ty
, unsigned long long v
);
93 ST_FUNC
void vpush(CType
*type
);
94 ST_FUNC
int gvtst(int inv
, int t
);
95 ST_FUNC
int is_btype_size(int bt
);
96 static void gen_inline_functions(TCCState
*s
);
98 ST_INLN
int is_float(int t
)
102 return bt
== VT_LDOUBLE
|| bt
== VT_DOUBLE
|| bt
== VT_FLOAT
|| bt
== VT_QFLOAT
;
105 /* we use our own 'finite' function to avoid potential problems with
106 non standard math libs */
107 /* XXX: endianness dependent */
108 ST_FUNC
int ieee_finite(double d
)
111 memcpy(p
, &d
, sizeof(double));
112 return ((unsigned)((p
[1] | 0x800fffff) + 1)) >> 31;
115 ST_FUNC
void test_lvalue(void)
117 if (!(vtop
->r
& VT_LVAL
))
121 ST_FUNC
void check_vstack(void)
124 tcc_error("internal compiler error: vstack leak (%d)", vtop
- pvtop
);
127 /* ------------------------------------------------------------------------- */
128 /* vstack debugging aid */
131 void pv (const char *lbl
, int a
, int b
)
134 for (i
= a
; i
< a
+ b
; ++i
) {
135 SValue
*p
= &vtop
[-i
];
136 printf("%s vtop[-%d] : type.t:%04x r:%04x r2:%04x c.i:%d\n",
137 lbl
, i
, p
->type
.t
, p
->r
, p
->r2
, (int)p
->c
.i
);
142 /* ------------------------------------------------------------------------- */
143 ST_FUNC
void tccgen_start(TCCState
*s1
)
145 cur_text_section
= NULL
;
147 anon_sym
= SYM_FIRST_ANOM
;
152 /* define some often used types */
154 char_pointer_type
.t
= VT_BYTE
;
155 mk_pointer(&char_pointer_type
);
157 size_type
.t
= VT_INT
;
159 size_type
.t
= VT_LLONG
;
161 func_old_type
.t
= VT_FUNC
;
162 func_old_type
.ref
= sym_push(SYM_FIELD
, &int_type
, FUNC_CDECL
, FUNC_OLD
);
167 /* file info: full path + filename */
168 section_sym
= put_elf_sym(symtab_section
, 0, 0,
169 ELFW(ST_INFO
)(STB_LOCAL
, STT_SECTION
), 0,
170 text_section
->sh_num
, NULL
);
171 getcwd(buf
, sizeof(buf
));
173 normalize_slashes(buf
);
175 pstrcat(buf
, sizeof(buf
), "/");
176 put_stabs_r(buf
, N_SO
, 0, 0,
177 text_section
->data_offset
, text_section
, section_sym
);
178 put_stabs_r(file
->filename
, N_SO
, 0, 0,
179 text_section
->data_offset
, text_section
, section_sym
);
181 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
182 symbols can be safely used */
183 put_elf_sym(symtab_section
, 0, 0,
184 ELFW(ST_INFO
)(STB_LOCAL
, STT_FILE
), 0,
185 SHN_ABS
, file
->filename
);
187 #ifdef TCC_TARGET_ARM
192 ST_FUNC
void tccgen_end(TCCState
*s1
)
194 gen_inline_functions(s1
);
196 /* end of translation unit info */
198 put_stabs_r(NULL
, N_SO
, 0, 0,
199 text_section
->data_offset
, text_section
, section_sym
);
203 /* ------------------------------------------------------------------------- */
204 /* update sym->c so that it points to an external symbol in section
205 'section' with value 'value' */
207 ST_FUNC
void put_extern_sym2(Sym
*sym
, Section
*section
,
208 addr_t value
, unsigned long size
,
209 int can_add_underscore
)
211 int sym_type
, sym_bind
, sh_num
, info
, other
;
216 #ifdef CONFIG_TCC_BCHECK
222 else if (section
== SECTION_ABS
)
225 sh_num
= section
->sh_num
;
227 if ((sym
->type
.t
& VT_BTYPE
) == VT_FUNC
) {
229 } else if ((sym
->type
.t
& VT_BTYPE
) == VT_VOID
) {
230 sym_type
= STT_NOTYPE
;
232 sym_type
= STT_OBJECT
;
235 if (sym
->type
.t
& VT_STATIC
)
236 sym_bind
= STB_LOCAL
;
238 if (sym
->type
.t
& VT_WEAK
)
241 sym_bind
= STB_GLOBAL
;
245 name
= get_tok_str(sym
->v
, NULL
);
246 #ifdef CONFIG_TCC_BCHECK
247 if (tcc_state
->do_bounds_check
) {
248 /* XXX: avoid doing that for statics ? */
249 /* if bound checking is activated, we change some function
250 names by adding the "__bound" prefix */
253 /* XXX: we rely only on malloc hooks */
266 strcpy(buf
, "__bound_");
276 if (sym
->type
.t
& VT_EXPORT
)
277 other
|= ST_PE_EXPORT
;
278 if (sym_type
== STT_FUNC
&& sym
->type
.ref
) {
279 Sym
*ref
= sym
->type
.ref
;
280 if (ref
->a
.func_export
)
281 other
|= ST_PE_EXPORT
;
282 if (ref
->a
.func_call
== FUNC_STDCALL
&& can_add_underscore
) {
283 sprintf(buf1
, "_%s@%d", name
, ref
->a
.func_args
* PTR_SIZE
);
285 other
|= ST_PE_STDCALL
;
286 can_add_underscore
= 0;
289 if (find_elf_sym(tcc_state
->dynsymtab_section
, name
))
290 other
|= ST_PE_IMPORT
;
291 if (sym
->type
.t
& VT_IMPORT
)
292 other
|= ST_PE_IMPORT
;
295 if (! (sym
->type
.t
& VT_STATIC
))
296 other
= (sym
->type
.t
& VT_VIS_MASK
) >> VT_VIS_SHIFT
;
298 if (tcc_state
->leading_underscore
&& can_add_underscore
) {
300 pstrcpy(buf1
+ 1, sizeof(buf1
) - 1, name
);
303 if (sym
->asm_label
) {
304 name
= get_tok_str(sym
->asm_label
, NULL
);
306 info
= ELFW(ST_INFO
)(sym_bind
, sym_type
);
307 sym
->c
= set_elf_sym(symtab_section
, value
, size
, info
, other
, sh_num
, name
);
309 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
310 esym
->st_value
= value
;
311 esym
->st_size
= size
;
312 esym
->st_shndx
= sh_num
;
316 ST_FUNC
void put_extern_sym(Sym
*sym
, Section
*section
,
317 addr_t value
, unsigned long size
)
319 put_extern_sym2(sym
, section
, value
, size
, 1);
322 /* add a new relocation entry to symbol 'sym' in section 's' */
323 ST_FUNC
void greloca(Section
*s
, Sym
*sym
, unsigned long offset
, int type
,
328 if (nocode_wanted
&& s
== cur_text_section
)
333 put_extern_sym(sym
, NULL
, 0, 0);
337 /* now we can add ELF relocation info */
338 put_elf_reloca(symtab_section
, s
, offset
, type
, c
, addend
);
341 ST_FUNC
void greloc(Section
*s
, Sym
*sym
, unsigned long offset
, int type
)
343 greloca(s
, sym
, offset
, type
, 0);
346 /* ------------------------------------------------------------------------- */
347 /* symbol allocator */
348 static Sym
*__sym_malloc(void)
350 Sym
*sym_pool
, *sym
, *last_sym
;
353 sym_pool
= tcc_malloc(SYM_POOL_NB
* sizeof(Sym
));
354 dynarray_add(&sym_pools
, &nb_sym_pools
, sym_pool
);
356 last_sym
= sym_free_first
;
358 for(i
= 0; i
< SYM_POOL_NB
; i
++) {
359 sym
->next
= last_sym
;
363 sym_free_first
= last_sym
;
367 static inline Sym
*sym_malloc(void)
371 sym
= sym_free_first
;
373 sym
= __sym_malloc();
374 sym_free_first
= sym
->next
;
377 sym
= tcc_malloc(sizeof(Sym
));
382 ST_INLN
void sym_free(Sym
*sym
)
385 sym
->next
= sym_free_first
;
386 sym_free_first
= sym
;
392 /* push, without hashing */
393 ST_FUNC Sym
*sym_push2(Sym
**ps
, int v
, int t
, long c
)
413 /* find a symbol and return its associated structure. 's' is the top
414 of the symbol stack */
415 ST_FUNC Sym
*sym_find2(Sym
*s
, int v
)
427 /* structure lookup */
428 ST_INLN Sym
*struct_find(int v
)
431 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
433 return table_ident
[v
]->sym_struct
;
436 /* find an identifier */
437 ST_INLN Sym
*sym_find(int v
)
440 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
442 return table_ident
[v
]->sym_identifier
;
445 /* push a given symbol on the symbol stack */
446 ST_FUNC Sym
*sym_push(int v
, CType
*type
, int r
, long c
)
455 s
= sym_push2(ps
, v
, type
->t
, c
);
456 s
->type
.ref
= type
->ref
;
458 /* don't record fields or anonymous symbols */
460 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
461 /* record symbol in token array */
462 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
464 ps
= &ts
->sym_struct
;
466 ps
= &ts
->sym_identifier
;
469 s
->scope
= local_scope
;
470 if (s
->prev_tok
&& s
->prev_tok
->scope
== s
->scope
)
471 tcc_error("redeclaration of '%s'",
472 get_tok_str(v
& ~SYM_STRUCT
, NULL
));
477 /* push a global identifier */
478 ST_FUNC Sym
*global_identifier_push(int v
, int t
, long c
)
481 s
= sym_push2(&global_stack
, v
, t
, c
);
482 /* don't record anonymous symbol */
483 if (v
< SYM_FIRST_ANOM
) {
484 ps
= &table_ident
[v
- TOK_IDENT
]->sym_identifier
;
485 /* modify the top most local identifier, so that
486 sym_identifier will point to 's' when popped */
488 ps
= &(*ps
)->prev_tok
;
495 /* pop symbols until top reaches 'b'. If KEEP is non-zero don't really
496 pop them yet from the list, but do remove them from the token array. */
497 ST_FUNC
void sym_pop(Sym
**ptop
, Sym
*b
, int keep
)
507 /* remove symbol in token array */
509 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
510 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
512 ps
= &ts
->sym_struct
;
514 ps
= &ts
->sym_identifier
;
525 static void weaken_symbol(Sym
*sym
)
527 sym
->type
.t
|= VT_WEAK
;
532 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
533 esym_type
= ELFW(ST_TYPE
)(esym
->st_info
);
534 esym
->st_info
= ELFW(ST_INFO
)(STB_WEAK
, esym_type
);
538 static void apply_visibility(Sym
*sym
, CType
*type
)
540 int vis
= sym
->type
.t
& VT_VIS_MASK
;
541 int vis2
= type
->t
& VT_VIS_MASK
;
542 if (vis
== (STV_DEFAULT
<< VT_VIS_SHIFT
))
544 else if (vis2
== (STV_DEFAULT
<< VT_VIS_SHIFT
))
547 vis
= (vis
< vis2
) ? vis
: vis2
;
548 sym
->type
.t
&= ~VT_VIS_MASK
;
554 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
555 vis
>>= VT_VIS_SHIFT
;
556 esym
->st_other
= (esym
->st_other
& ~ELFW(ST_VISIBILITY
)(-1)) | vis
;
560 /* ------------------------------------------------------------------------- */
562 ST_FUNC
void swap(int *p
, int *q
)
570 static void vsetc(CType
*type
, int r
, CValue
*vc
)
574 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
575 tcc_error("memory full (vstack)");
576 /* cannot let cpu flags if other instruction are generated. Also
577 avoid leaving VT_JMP anywhere except on the top of the stack
578 because it would complicate the code generator.
580 Don't do this when nocode_wanted. vtop might come from
581 !nocode_wanted regions (see 88_codeopt.c) and transforming
582 it to a register without actually generating code is wrong
583 as their value might still be used for real. All values
584 we push under nocode_wanted will eventually be popped
585 again, so that the VT_CMP/VT_JMP value will be in vtop
586 when code is unsuppressed again. */
587 if (vtop
>= vstack
&& !nocode_wanted
) {
588 v
= vtop
->r
& VT_VALMASK
;
589 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
600 /* push constant of type "type" with useless value */
601 ST_FUNC
void vpush(CType
*type
)
604 vsetc(type
, VT_CONST
, &cval
);
607 /* push integer constant */
608 ST_FUNC
void vpushi(int v
)
612 vsetc(&int_type
, VT_CONST
, &cval
);
615 /* push a pointer sized constant */
616 static void vpushs(addr_t v
)
620 vsetc(&size_type
, VT_CONST
, &cval
);
623 /* push arbitrary 64bit constant */
624 ST_FUNC
void vpush64(int ty
, unsigned long long v
)
631 vsetc(&ctype
, VT_CONST
, &cval
);
634 /* push long long constant */
635 static inline void vpushll(long long v
)
637 vpush64(VT_LLONG
, v
);
640 /* push a symbol value of TYPE */
641 static inline void vpushsym(CType
*type
, Sym
*sym
)
645 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
649 /* Return a static symbol pointing to a section */
650 ST_FUNC Sym
*get_sym_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
656 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
657 sym
->type
.ref
= type
->ref
;
658 sym
->r
= VT_CONST
| VT_SYM
;
659 put_extern_sym(sym
, sec
, offset
, size
);
663 /* push a reference to a section offset by adding a dummy symbol */
664 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
666 vpushsym(type
, get_sym_ref(type
, sec
, offset
, size
));
669 /* define a new external reference to a symbol 'v' of type 'u' */
670 ST_FUNC Sym
*external_global_sym(int v
, CType
*type
, int r
)
676 /* push forward reference */
677 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
678 s
->type
.ref
= type
->ref
;
679 s
->r
= r
| VT_CONST
| VT_SYM
;
684 /* define a new external reference to a symbol 'v' */
685 static Sym
*external_sym(int v
, CType
*type
, int r
)
691 /* push forward reference */
692 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
693 s
->type
.t
|= VT_EXTERN
;
694 } else if (s
->type
.ref
== func_old_type
.ref
) {
695 s
->type
.ref
= type
->ref
;
696 s
->r
= r
| VT_CONST
| VT_SYM
;
697 s
->type
.t
|= VT_EXTERN
;
698 } else if (!is_compatible_types(&s
->type
, type
)) {
699 tcc_error("incompatible types for redefinition of '%s'",
700 get_tok_str(v
, NULL
));
702 /* Merge some storage attributes. */
703 if (type
->t
& VT_WEAK
)
706 if (type
->t
& VT_VIS_MASK
)
707 apply_visibility(s
, type
);
712 /* push a reference to global symbol v */
713 ST_FUNC
void vpush_global_sym(CType
*type
, int v
)
715 vpushsym(type
, external_global_sym(v
, type
, 0));
718 ST_FUNC
void vset(CType
*type
, int r
, long v
)
723 vsetc(type
, r
, &cval
);
726 static void vseti(int r
, int v
)
734 ST_FUNC
void vswap(void)
737 /* cannot let cpu flags if other instruction are generated. Also
738 avoid leaving VT_JMP anywhere except on the top of the stack
739 because it would complicate the code generator. */
740 if (vtop
>= vstack
) {
741 int v
= vtop
->r
& VT_VALMASK
;
742 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
749 /* XXX: +2% overall speed possible with optimized memswap
751 * memswap(&vtop[0], &vtop[1], sizeof *vtop);
755 ST_FUNC
void vpushv(SValue
*v
)
757 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
758 tcc_error("memory full (vstack)");
763 static void vdup(void)
768 /* save registers up to (vtop - n) stack entry */
769 ST_FUNC
void save_regs(int n
)
772 for(p
= vstack
, p1
= vtop
- n
; p
<= p1
; p
++)
776 /* save r to the memory stack, and mark it as being free */
777 ST_FUNC
void save_reg(int r
)
779 save_reg_upstack(r
, 0);
782 /* save r to the memory stack, and mark it as being free,
783 if seen up to (vtop - n) stack entry */
784 ST_FUNC
void save_reg_upstack(int r
, int n
)
786 int l
, saved
, size
, align
;
790 if ((r
&= VT_VALMASK
) >= VT_CONST
)
795 /* modify all stack values */
798 for(p
= vstack
, p1
= vtop
- n
; p
<= p1
; p
++) {
799 if ((p
->r
& VT_VALMASK
) == r
||
800 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
801 /* must save value on stack if not already done */
803 /* NOTE: must reload 'r' because r might be equal to r2 */
804 r
= p
->r
& VT_VALMASK
;
805 /* store register in the stack */
807 if ((p
->r
& VT_LVAL
) ||
808 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
809 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
810 type
= &char_pointer_type
;
814 size
= type_size(type
, &align
);
815 loc
= (loc
- size
) & -align
;
817 sv
.r
= VT_LOCAL
| VT_LVAL
;
820 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
821 /* x86 specific: need to pop fp register ST0 if saved */
823 o(0xd8dd); /* fstp %st(0) */
826 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
827 /* special long long case */
828 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
836 /* mark that stack entry as being saved on the stack */
837 if (p
->r
& VT_LVAL
) {
838 /* also clear the bounded flag because the
839 relocation address of the function was stored in
841 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
843 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
851 #ifdef TCC_TARGET_ARM
852 /* find a register of class 'rc2' with at most one reference on stack.
853 * If none, call get_reg(rc) */
854 ST_FUNC
int get_reg_ex(int rc
, int rc2
)
859 for(r
=0;r
<NB_REGS
;r
++) {
860 if (reg_classes
[r
] & rc2
) {
863 for(p
= vstack
; p
<= vtop
; p
++) {
864 if ((p
->r
& VT_VALMASK
) == r
||
865 (p
->r2
& VT_VALMASK
) == r
)
876 /* find a free register of class 'rc'. If none, save one register */
877 ST_FUNC
int get_reg(int rc
)
882 /* find a free register */
883 for(r
=0;r
<NB_REGS
;r
++) {
884 if (reg_classes
[r
] & rc
) {
887 for(p
=vstack
;p
<=vtop
;p
++) {
888 if ((p
->r
& VT_VALMASK
) == r
||
889 (p
->r2
& VT_VALMASK
) == r
)
897 /* no register left : free the first one on the stack (VERY
898 IMPORTANT to start from the bottom to ensure that we don't
899 spill registers used in gen_opi()) */
900 for(p
=vstack
;p
<=vtop
;p
++) {
901 /* look at second register (if long long) */
902 r
= p
->r2
& VT_VALMASK
;
903 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
905 r
= p
->r
& VT_VALMASK
;
906 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
912 /* Should never comes here */
916 /* move register 's' (of type 't') to 'r', and flush previous value of r to memory
918 static void move_reg(int r
, int s
, int t
)
932 /* get address of vtop (vtop MUST BE an lvalue) */
933 ST_FUNC
void gaddrof(void)
935 if (vtop
->r
& VT_REF
)
938 /* tricky: if saved lvalue, then we can go back to lvalue */
939 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
940 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
945 #ifdef CONFIG_TCC_BCHECK
946 /* generate lvalue bound code */
947 static void gbound(void)
952 vtop
->r
&= ~VT_MUSTBOUND
;
953 /* if lvalue, then use checking code before dereferencing */
954 if (vtop
->r
& VT_LVAL
) {
955 /* if not VT_BOUNDED value, then make one */
956 if (!(vtop
->r
& VT_BOUNDED
)) {
957 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
958 /* must save type because we must set it to int to get pointer */
960 vtop
->type
.t
= VT_PTR
;
963 gen_bounded_ptr_add();
964 vtop
->r
|= lval_type
;
967 /* then check for dereferencing */
968 gen_bounded_ptr_deref();
973 /* store vtop a register belonging to class 'rc'. lvalues are
974 converted to values. Cannot be used if cannot be converted to
975 register value (such as structures). */
976 ST_FUNC
int gv(int rc
)
978 int r
, bit_pos
, bit_size
, size
, align
, i
;
981 /* NOTE: get_reg can modify vstack[] */
982 if (vtop
->type
.t
& VT_BITFIELD
) {
985 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
986 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
987 /* remove bit field info to avoid loops */
988 vtop
->type
.t
&= ~VT_BITFIELD
& ((1 << VT_STRUCT_SHIFT
) - 1);
989 /* cast to int to propagate signedness in following ops */
990 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
995 if((vtop
->type
.t
& VT_UNSIGNED
) ||
996 (vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
997 type
.t
|= VT_UNSIGNED
;
999 /* generate shifts */
1000 vpushi(bits
- (bit_pos
+ bit_size
));
1002 vpushi(bits
- bit_size
);
1003 /* NOTE: transformed to SHR if unsigned */
1007 if (is_float(vtop
->type
.t
) &&
1008 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
1011 unsigned long offset
;
1012 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
1016 /* XXX: unify with initializers handling ? */
1017 /* CPUs usually cannot use float constants, so we store them
1018 generically in data segment */
1019 size
= type_size(&vtop
->type
, &align
);
1020 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
1021 data_section
->data_offset
= offset
;
1022 /* XXX: not portable yet */
1023 #if defined(__i386__) || defined(__x86_64__)
1024 /* Zero pad x87 tenbyte long doubles */
1025 if (size
== LDOUBLE_SIZE
) {
1026 vtop
->c
.tab
[2] &= 0xffff;
1027 #if LDOUBLE_SIZE == 16
1032 ptr
= section_ptr_add(data_section
, size
);
1034 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
1038 ptr
[i
] = vtop
->c
.tab
[size
-1-i
];
1042 ptr
[i
] = vtop
->c
.tab
[i
];
1043 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
1044 vtop
->r
|= VT_LVAL
| VT_SYM
;
1048 #ifdef CONFIG_TCC_BCHECK
1049 if (vtop
->r
& VT_MUSTBOUND
)
1053 r
= vtop
->r
& VT_VALMASK
;
1054 rc2
= (rc
& RC_FLOAT
) ? RC_FLOAT
: RC_INT
;
1055 #ifndef TCC_TARGET_ARM64
1058 #ifdef TCC_TARGET_X86_64
1059 else if (rc
== RC_FRET
)
1064 /* need to reload if:
1066 - lvalue (need to dereference pointer)
1067 - already a register, but not in the right class */
1069 || (vtop
->r
& VT_LVAL
)
1070 || !(reg_classes
[r
] & rc
)
1071 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1072 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
1073 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
&& !(reg_classes
[vtop
->r2
] & rc2
))
1075 || ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
1080 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1081 if (((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
)) {
1082 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
1084 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
1085 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
1086 unsigned long long ll
;
1088 int r2
, original_type
;
1089 original_type
= vtop
->type
.t
;
1090 /* two register type load : expand to two words
1092 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
1093 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
1096 vtop
->c
.i
= ll
; /* first word */
1098 vtop
->r
= r
; /* save register value */
1099 vpushi(ll
>> 32); /* second word */
1102 if (vtop
->r
& VT_LVAL
) {
1103 /* We do not want to modifier the long long
1104 pointer here, so the safest (and less
1105 efficient) is to save all the other registers
1106 in the stack. XXX: totally inefficient. */
1110 /* lvalue_save: save only if used further down the stack */
1111 save_reg_upstack(vtop
->r
, 1);
1113 /* load from memory */
1114 vtop
->type
.t
= load_type
;
1117 vtop
[-1].r
= r
; /* save register value */
1118 /* increment pointer to get second word */
1119 vtop
->type
.t
= addr_type
;
1124 vtop
->type
.t
= load_type
;
1126 /* move registers */
1129 vtop
[-1].r
= r
; /* save register value */
1130 vtop
->r
= vtop
[-1].r2
;
1132 /* Allocate second register. Here we rely on the fact that
1133 get_reg() tries first to free r2 of an SValue. */
1137 /* write second register */
1139 vtop
->type
.t
= original_type
;
1140 } else if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
1142 /* lvalue of scalar type : need to use lvalue type
1143 because of possible cast */
1146 /* compute memory access type */
1147 if (vtop
->r
& VT_REF
)
1148 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1153 else if (vtop
->r
& VT_LVAL_BYTE
)
1155 else if (vtop
->r
& VT_LVAL_SHORT
)
1157 if (vtop
->r
& VT_LVAL_UNSIGNED
)
1161 /* restore wanted type */
1164 /* one register type load */
1169 #ifdef TCC_TARGET_C67
1170 /* uses register pairs for doubles */
1171 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
1178 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
1179 ST_FUNC
void gv2(int rc1
, int rc2
)
1183 /* generate more generic register first. But VT_JMP or VT_CMP
1184 values must be generated first in all cases to avoid possible
1186 v
= vtop
[0].r
& VT_VALMASK
;
1187 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
1192 /* test if reload is needed for first register */
1193 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
1203 /* test if reload is needed for first register */
1204 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
1210 #ifndef TCC_TARGET_ARM64
1211 /* wrapper around RC_FRET to return a register by type */
1212 static int rc_fret(int t
)
1214 #ifdef TCC_TARGET_X86_64
1215 if (t
== VT_LDOUBLE
) {
1223 /* wrapper around REG_FRET to return a register by type */
1224 static int reg_fret(int t
)
1226 #ifdef TCC_TARGET_X86_64
1227 if (t
== VT_LDOUBLE
) {
1234 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
1235 /* expand 64bit on stack in two ints */
1236 static void lexpand(void)
1239 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
1240 v
= vtop
->r
& (VT_VALMASK
| VT_LVAL
);
1241 if (v
== VT_CONST
) {
1244 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
1250 vtop
[0].r
= vtop
[-1].r2
;
1251 vtop
[0].r2
= vtop
[-1].r2
= VT_CONST
;
1253 vtop
[0].type
.t
= vtop
[-1].type
.t
= VT_INT
| u
;
1257 #ifdef TCC_TARGET_ARM
1258 /* expand long long on stack */
1259 ST_FUNC
void lexpand_nr(void)
1263 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
1265 vtop
->r2
= VT_CONST
;
1266 vtop
->type
.t
= VT_INT
| u
;
1267 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
1268 if (v
== VT_CONST
) {
1269 vtop
[-1].c
.i
= vtop
->c
.i
;
1270 vtop
->c
.i
= vtop
->c
.i
>> 32;
1272 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
1274 vtop
->r
= vtop
[-1].r
;
1275 } else if (v
> VT_CONST
) {
1279 vtop
->r
= vtop
[-1].r2
;
1280 vtop
[-1].r2
= VT_CONST
;
1281 vtop
[-1].type
.t
= VT_INT
| u
;
1285 #if !defined(TCC_TARGET_X86_64) && !defined(TCC_TARGET_ARM64)
1286 /* build a long long from two ints */
1287 static void lbuild(int t
)
1289 gv2(RC_INT
, RC_INT
);
1290 vtop
[-1].r2
= vtop
[0].r
;
1291 vtop
[-1].type
.t
= t
;
1296 /* rotate n first stack elements to the bottom
1297 I1 ... In -> I2 ... In I1 [top is right]
1299 ST_FUNC
void vrotb(int n
)
1305 for(i
=-n
+1;i
!=0;i
++)
1306 vtop
[i
] = vtop
[i
+1];
1310 /* rotate the n elements before entry e towards the top
1311 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
1313 ST_FUNC
void vrote(SValue
*e
, int n
)
1319 for(i
= 0;i
< n
- 1; i
++)
1324 /* rotate n first stack elements to the top
1325 I1 ... In -> In I1 ... I(n-1) [top is right]
1327 ST_FUNC
void vrott(int n
)
1332 /* pop stack value */
1333 ST_FUNC
void vpop(void)
1336 v
= vtop
->r
& VT_VALMASK
;
1337 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1338 /* for x86, we need to pop the FP stack */
1339 if (v
== TREG_ST0
) {
1340 o(0xd8dd); /* fstp %st(0) */
1343 if (v
== VT_JMP
|| v
== VT_JMPI
) {
1344 /* need to put correct jump if && or || without test */
1350 /* convert stack entry to register and duplicate its value in another
1352 static void gv_dup(void)
1358 #if !defined(TCC_TARGET_X86_64) && !defined(TCC_TARGET_ARM64)
1359 if ((t
& VT_BTYPE
) == VT_LLONG
) {
1366 /* stack: H L L1 H1 */
1376 /* duplicate value */
1381 #ifdef TCC_TARGET_X86_64
1382 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1392 load(r1
, &sv
); /* move r to r1 */
1394 /* duplicates value */
1400 /* Generate value test
1402 * Generate a test for any value (jump, comparison and integers) */
1403 ST_FUNC
int gvtst(int inv
, int t
)
1405 int v
= vtop
->r
& VT_VALMASK
;
1406 if (v
!= VT_CMP
&& v
!= VT_JMP
&& v
!= VT_JMPI
) {
1410 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1411 /* constant jmp optimization */
1412 if ((vtop
->c
.i
!= 0) != inv
)
1417 return gtst(inv
, t
);
1420 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
1421 /* generate CPU independent (unsigned) long long operations */
1422 static void gen_opl(int op
)
1424 int t
, a
, b
, op1
, c
, i
;
1426 unsigned short reg_iret
= REG_IRET
;
1427 unsigned short reg_lret
= REG_LRET
;
1433 func
= TOK___divdi3
;
1436 func
= TOK___udivdi3
;
1439 func
= TOK___moddi3
;
1442 func
= TOK___umoddi3
;
1449 /* call generic long long function */
1450 vpush_global_sym(&func_old_type
, func
);
1455 vtop
->r2
= reg_lret
;
1463 //pv("gen_opl A",0,2);
1469 /* stack: L1 H1 L2 H2 */
1474 vtop
[-2] = vtop
[-3];
1477 /* stack: H1 H2 L1 L2 */
1478 //pv("gen_opl B",0,4);
1484 /* stack: H1 H2 L1 L2 ML MH */
1487 /* stack: ML MH H1 H2 L1 L2 */
1491 /* stack: ML MH H1 L2 H2 L1 */
1496 /* stack: ML MH M1 M2 */
1499 } else if (op
== '+' || op
== '-') {
1500 /* XXX: add non carry method too (for MIPS or alpha) */
1506 /* stack: H1 H2 (L1 op L2) */
1509 gen_op(op1
+ 1); /* TOK_xxxC2 */
1512 /* stack: H1 H2 (L1 op L2) */
1515 /* stack: (L1 op L2) H1 H2 */
1517 /* stack: (L1 op L2) (H1 op H2) */
1525 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1526 t
= vtop
[-1].type
.t
;
1530 /* stack: L H shift */
1532 /* constant: simpler */
1533 /* NOTE: all comments are for SHL. the other cases are
1534 done by swaping words */
1545 if (op
!= TOK_SAR
) {
1578 /* XXX: should provide a faster fallback on x86 ? */
1581 func
= TOK___ashrdi3
;
1584 func
= TOK___lshrdi3
;
1587 func
= TOK___ashldi3
;
1593 /* compare operations */
1599 /* stack: L1 H1 L2 H2 */
1601 vtop
[-1] = vtop
[-2];
1603 /* stack: L1 L2 H1 H2 */
1606 /* when values are equal, we need to compare low words. since
1607 the jump is inverted, we invert the test too. */
1610 else if (op1
== TOK_GT
)
1612 else if (op1
== TOK_ULT
)
1614 else if (op1
== TOK_UGT
)
1619 if (op1
!= TOK_NE
) {
1623 /* generate non equal test */
1624 /* XXX: NOT PORTABLE yet */
1628 #if defined(TCC_TARGET_I386)
1629 b
= gjmp2(0x850f, 0);
1630 #elif defined(TCC_TARGET_ARM)
1632 o(0x1A000000 | encbranch(ind
, 0, 1));
1633 #elif defined(TCC_TARGET_C67) || defined(TCC_TARGET_ARM64)
1634 tcc_error("not implemented");
1636 #error not supported
1640 /* compare low. Always unsigned */
1644 else if (op1
== TOK_LE
)
1646 else if (op1
== TOK_GT
)
1648 else if (op1
== TOK_GE
)
1659 static uint64_t gen_opic_sdiv(uint64_t a
, uint64_t b
)
1661 uint64_t x
= (a
>> 63 ? -a
: a
) / (b
>> 63 ? -b
: b
);
1662 return (a
^ b
) >> 63 ? -x
: x
;
1665 static int gen_opic_lt(uint64_t a
, uint64_t b
)
1667 return (a
^ (uint64_t)1 << 63) < (b
^ (uint64_t)1 << 63);
1670 /* handle integer constant optimizations and various machine
1672 static void gen_opic(int op
)
1674 SValue
*v1
= vtop
- 1;
1676 int t1
= v1
->type
.t
& VT_BTYPE
;
1677 int t2
= v2
->type
.t
& VT_BTYPE
;
1678 int c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1679 int c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1680 uint64_t l1
= c1
? v1
->c
.i
: 0;
1681 uint64_t l2
= c2
? v2
->c
.i
: 0;
1682 int shm
= (t1
== VT_LLONG
) ? 63 : 31;
1684 if (t1
!= VT_LLONG
&& (PTR_SIZE
!= 8 || t1
!= VT_PTR
))
1685 l1
= ((uint32_t)l1
|
1686 (v1
->type
.t
& VT_UNSIGNED
? 0 : -(l1
& 0x80000000)));
1687 if (t2
!= VT_LLONG
&& (PTR_SIZE
!= 8 || t2
!= VT_PTR
))
1688 l2
= ((uint32_t)l2
|
1689 (v2
->type
.t
& VT_UNSIGNED
? 0 : -(l2
& 0x80000000)));
1693 case '+': l1
+= l2
; break;
1694 case '-': l1
-= l2
; break;
1695 case '&': l1
&= l2
; break;
1696 case '^': l1
^= l2
; break;
1697 case '|': l1
|= l2
; break;
1698 case '*': l1
*= l2
; break;
1705 /* if division by zero, generate explicit division */
1708 tcc_error("division by zero in constant");
1712 default: l1
= gen_opic_sdiv(l1
, l2
); break;
1713 case '%': l1
= l1
- l2
* gen_opic_sdiv(l1
, l2
); break;
1714 case TOK_UDIV
: l1
= l1
/ l2
; break;
1715 case TOK_UMOD
: l1
= l1
% l2
; break;
1718 case TOK_SHL
: l1
<<= (l2
& shm
); break;
1719 case TOK_SHR
: l1
>>= (l2
& shm
); break;
1721 l1
= (l1
>> 63) ? ~(~l1
>> (l2
& shm
)) : l1
>> (l2
& shm
);
1724 case TOK_ULT
: l1
= l1
< l2
; break;
1725 case TOK_UGE
: l1
= l1
>= l2
; break;
1726 case TOK_EQ
: l1
= l1
== l2
; break;
1727 case TOK_NE
: l1
= l1
!= l2
; break;
1728 case TOK_ULE
: l1
= l1
<= l2
; break;
1729 case TOK_UGT
: l1
= l1
> l2
; break;
1730 case TOK_LT
: l1
= gen_opic_lt(l1
, l2
); break;
1731 case TOK_GE
: l1
= !gen_opic_lt(l1
, l2
); break;
1732 case TOK_LE
: l1
= !gen_opic_lt(l2
, l1
); break;
1733 case TOK_GT
: l1
= gen_opic_lt(l2
, l1
); break;
1735 case TOK_LAND
: l1
= l1
&& l2
; break;
1736 case TOK_LOR
: l1
= l1
|| l2
; break;
1740 if (t1
!= VT_LLONG
&& (PTR_SIZE
!= 8 || t1
!= VT_PTR
))
1741 l1
= ((uint32_t)l1
|
1742 (v1
->type
.t
& VT_UNSIGNED
? 0 : -(l1
& 0x80000000)));
1746 /* if commutative ops, put c2 as constant */
1747 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1748 op
== '|' || op
== '*')) {
1750 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1751 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1753 if (!const_wanted
&&
1755 (op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
)) ||
1756 (l1
== -1 && op
== TOK_SAR
))) {
1757 /* treat (0 << x), (0 >> x) and (-1 >> x) as constant */
1759 } else if (!const_wanted
&&
1760 c2
&& ((l2
== 0 && (op
== '&' || op
== '*')) ||
1761 (l2
== -1 && op
== '|') ||
1762 (l2
== 0xffffffff && t2
!= VT_LLONG
&& op
== '|') ||
1763 (l2
== 1 && (op
== '%' || op
== TOK_UMOD
)))) {
1764 /* treat (x & 0), (x * 0), (x | -1) and (x % 1) as constant */
1769 } else if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1772 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1773 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1777 /* filter out NOP operations like x*1, x-0, x&-1... */
1779 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1780 /* try to use shifts instead of muls or divs */
1781 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1790 else if (op
== TOK_PDIV
)
1796 } else if (c2
&& (op
== '+' || op
== '-') &&
1797 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
1798 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1799 /* symbol + constant case */
1803 /* The backends can't always deal with addends to symbols
1804 larger than +-1<<31. Don't construct such. */
1811 /* call low level op generator */
1812 if (t1
== VT_LLONG
|| t2
== VT_LLONG
||
1813 (PTR_SIZE
== 8 && (t1
== VT_PTR
|| t2
== VT_PTR
)))
1821 /* generate a floating point operation with constant propagation */
1822 static void gen_opif(int op
)
1830 /* currently, we cannot do computations with forward symbols */
1831 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1832 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1834 if (v1
->type
.t
== VT_FLOAT
) {
1837 } else if (v1
->type
.t
== VT_DOUBLE
) {
1845 /* NOTE: we only do constant propagation if finite number (not
1846 NaN or infinity) (ANSI spec) */
1847 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1851 case '+': f1
+= f2
; break;
1852 case '-': f1
-= f2
; break;
1853 case '*': f1
*= f2
; break;
1857 tcc_error("division by zero in constant");
1862 /* XXX: also handles tests ? */
1866 /* XXX: overflow test ? */
1867 if (v1
->type
.t
== VT_FLOAT
) {
1869 } else if (v1
->type
.t
== VT_DOUBLE
) {
1881 static int pointed_size(CType
*type
)
1884 return type_size(pointed_type(type
), &align
);
1887 static void vla_runtime_pointed_size(CType
*type
)
1890 vla_runtime_type_size(pointed_type(type
), &align
);
1893 static inline int is_null_pointer(SValue
*p
)
1895 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1897 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& (uint32_t)p
->c
.i
== 0) ||
1898 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.i
== 0) ||
1899 ((p
->type
.t
& VT_BTYPE
) == VT_PTR
&&
1900 (PTR_SIZE
== 4 ? (uint32_t)p
->c
.i
== 0 : p
->c
.i
== 0));
1903 static inline int is_integer_btype(int bt
)
1905 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
1906 bt
== VT_INT
|| bt
== VT_LLONG
);
1909 /* check types for comparison or subtraction of pointers */
1910 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
1912 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
1915 /* null pointers are accepted for all comparisons as gcc */
1916 if (is_null_pointer(p1
) || is_null_pointer(p2
))
1920 bt1
= type1
->t
& VT_BTYPE
;
1921 bt2
= type2
->t
& VT_BTYPE
;
1922 /* accept comparison between pointer and integer with a warning */
1923 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
1924 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
1925 tcc_warning("comparison between pointer and integer");
1929 /* both must be pointers or implicit function pointers */
1930 if (bt1
== VT_PTR
) {
1931 type1
= pointed_type(type1
);
1932 } else if (bt1
!= VT_FUNC
)
1933 goto invalid_operands
;
1935 if (bt2
== VT_PTR
) {
1936 type2
= pointed_type(type2
);
1937 } else if (bt2
!= VT_FUNC
) {
1939 tcc_error("invalid operands to binary %s", get_tok_str(op
, NULL
));
1941 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1942 (type2
->t
& VT_BTYPE
) == VT_VOID
)
1946 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1947 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1948 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1949 /* gcc-like error if '-' is used */
1951 goto invalid_operands
;
1953 tcc_warning("comparison of distinct pointer types lacks a cast");
1957 /* generic gen_op: handles types problems */
1958 ST_FUNC
void gen_op(int op
)
1960 int u
, t1
, t2
, bt1
, bt2
, t
;
1964 t1
= vtop
[-1].type
.t
;
1965 t2
= vtop
[0].type
.t
;
1966 bt1
= t1
& VT_BTYPE
;
1967 bt2
= t2
& VT_BTYPE
;
1969 if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
1970 tcc_error("operation on a struct");
1971 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
1972 if (bt2
== VT_FUNC
) {
1973 mk_pointer(&vtop
->type
);
1976 if (bt1
== VT_FUNC
) {
1978 mk_pointer(&vtop
->type
);
1983 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
1984 /* at least one operand is a pointer */
1985 /* relationnal op: must be both pointers */
1986 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
1987 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1988 /* pointers are handled are unsigned */
1989 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1990 t
= VT_LLONG
| VT_UNSIGNED
;
1992 t
= VT_INT
| VT_UNSIGNED
;
1996 /* if both pointers, then it must be the '-' op */
1997 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
1999 tcc_error("cannot use pointers here");
2000 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
2001 /* XXX: check that types are compatible */
2002 if (vtop
[-1].type
.t
& VT_VLA
) {
2003 vla_runtime_pointed_size(&vtop
[-1].type
);
2005 vpushi(pointed_size(&vtop
[-1].type
));
2009 /* set to integer type */
2010 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2011 vtop
->type
.t
= VT_LLONG
;
2013 vtop
->type
.t
= VT_INT
;
2018 /* exactly one pointer : must be '+' or '-'. */
2019 if (op
!= '-' && op
!= '+')
2020 tcc_error("cannot use pointers here");
2021 /* Put pointer as first operand */
2022 if (bt2
== VT_PTR
) {
2027 if ((vtop
[0].type
.t
& VT_BTYPE
) == VT_LLONG
)
2028 /* XXX: truncate here because gen_opl can't handle ptr + long long */
2029 gen_cast(&int_type
);
2031 type1
= vtop
[-1].type
;
2032 type1
.t
&= ~VT_ARRAY
;
2033 if (vtop
[-1].type
.t
& VT_VLA
)
2034 vla_runtime_pointed_size(&vtop
[-1].type
);
2036 u
= pointed_size(&vtop
[-1].type
);
2038 tcc_error("unknown array element size");
2039 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2042 /* XXX: cast to int ? (long long case) */
2048 /* #ifdef CONFIG_TCC_BCHECK
2049 The main reason to removing this code:
2056 fprintf(stderr, "v+i-j = %p\n", v+i-j);
2057 fprintf(stderr, "v+(i-j) = %p\n", v+(i-j));
2059 When this code is on. then the output looks like
2061 v+(i-j) = 0xbff84000
2063 /* if evaluating constant expression, no code should be
2064 generated, so no bound check */
2065 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
2066 /* if bounded pointers, we generate a special code to
2073 gen_bounded_ptr_add();
2079 /* put again type if gen_opic() swaped operands */
2082 } else if (is_float(bt1
) || is_float(bt2
)) {
2083 /* compute bigger type and do implicit casts */
2084 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
2086 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
2091 /* floats can only be used for a few operations */
2092 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
2093 (op
< TOK_ULT
|| op
> TOK_GT
))
2094 tcc_error("invalid operands for binary operation");
2096 } else if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
) {
2097 t
= bt1
== VT_LLONG
? VT_LLONG
: VT_INT
;
2098 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (t
| VT_UNSIGNED
))
2101 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
2102 /* cast to biggest op */
2104 /* convert to unsigned if it does not fit in a long long */
2105 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
2106 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
2110 /* integer operations */
2112 /* convert to unsigned if it does not fit in an integer */
2113 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
2114 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
2117 /* XXX: currently, some unsigned operations are explicit, so
2118 we modify them here */
2119 if (t
& VT_UNSIGNED
) {
2126 else if (op
== TOK_LT
)
2128 else if (op
== TOK_GT
)
2130 else if (op
== TOK_LE
)
2132 else if (op
== TOK_GE
)
2139 /* special case for shifts and long long: we keep the shift as
2141 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
2148 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
2149 /* relationnal op: the result is an int */
2150 vtop
->type
.t
= VT_INT
;
2155 // Make sure that we have converted to an rvalue:
2156 if (vtop
->r
& VT_LVAL
)
2157 gv(is_float(vtop
->type
.t
& VT_BTYPE
) ? RC_FLOAT
: RC_INT
);
2160 #ifndef TCC_TARGET_ARM
2161 /* generic itof for unsigned long long case */
2162 static void gen_cvt_itof1(int t
)
2164 #ifdef TCC_TARGET_ARM64
2167 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
2168 (VT_LLONG
| VT_UNSIGNED
)) {
2171 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
2172 #if LDOUBLE_SIZE != 8
2173 else if (t
== VT_LDOUBLE
)
2174 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
2177 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
2181 vtop
->r
= reg_fret(t
);
2189 /* generic ftoi for unsigned long long case */
2190 static void gen_cvt_ftoi1(int t
)
2192 #ifdef TCC_TARGET_ARM64
2197 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
2198 /* not handled natively */
2199 st
= vtop
->type
.t
& VT_BTYPE
;
2201 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
2202 #if LDOUBLE_SIZE != 8
2203 else if (st
== VT_LDOUBLE
)
2204 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
2207 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
2212 vtop
->r2
= REG_LRET
;
2219 /* force char or short cast */
2220 static void force_charshort_cast(int t
)
2224 /* XXX: add optimization if lvalue : just change type and offset */
2229 if (t
& VT_UNSIGNED
) {
2230 vpushi((1 << bits
) - 1);
2233 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
)
2239 /* result must be signed or the SAR is converted to an SHL
2240 This was not the case when "t" was a signed short
2241 and the last value on the stack was an unsigned int */
2242 vtop
->type
.t
&= ~VT_UNSIGNED
;
2248 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
2249 static void gen_cast(CType
*type
)
2251 int sbt
, dbt
, sf
, df
, c
, p
;
2253 /* special delayed cast for char/short */
2254 /* XXX: in some cases (multiple cascaded casts), it may still
2256 if (vtop
->r
& VT_MUSTCAST
) {
2257 vtop
->r
&= ~VT_MUSTCAST
;
2258 force_charshort_cast(vtop
->type
.t
);
2261 /* bitfields first get cast to ints */
2262 if (vtop
->type
.t
& VT_BITFIELD
) {
2266 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
2267 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
2272 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
2273 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
2275 /* constant case: we can do it now */
2276 /* XXX: in ISOC, cannot do it if error in convert */
2277 if (sbt
== VT_FLOAT
)
2278 vtop
->c
.ld
= vtop
->c
.f
;
2279 else if (sbt
== VT_DOUBLE
)
2280 vtop
->c
.ld
= vtop
->c
.d
;
2283 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
2284 if ((sbt
& VT_UNSIGNED
) || !(vtop
->c
.i
>> 63))
2285 vtop
->c
.ld
= vtop
->c
.i
;
2287 vtop
->c
.ld
= -(long double)-vtop
->c
.i
;
2289 if ((sbt
& VT_UNSIGNED
) || !(vtop
->c
.i
>> 31))
2290 vtop
->c
.ld
= (uint32_t)vtop
->c
.i
;
2292 vtop
->c
.ld
= -(long double)-(uint32_t)vtop
->c
.i
;
2295 if (dbt
== VT_FLOAT
)
2296 vtop
->c
.f
= (float)vtop
->c
.ld
;
2297 else if (dbt
== VT_DOUBLE
)
2298 vtop
->c
.d
= (double)vtop
->c
.ld
;
2299 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
2300 vtop
->c
.i
= vtop
->c
.ld
;
2301 } else if (sf
&& dbt
== VT_BOOL
) {
2302 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
2305 vtop
->c
.i
= vtop
->c
.ld
;
2306 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
2308 else if (sbt
& VT_UNSIGNED
)
2309 vtop
->c
.i
= (uint32_t)vtop
->c
.i
;
2310 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2311 else if (sbt
== VT_PTR
)
2314 else if (sbt
!= VT_LLONG
)
2315 vtop
->c
.i
= ((uint32_t)vtop
->c
.i
|
2316 -(vtop
->c
.i
& 0x80000000));
2318 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
2320 else if (dbt
== VT_BOOL
)
2321 vtop
->c
.i
= (vtop
->c
.i
!= 0);
2322 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2323 else if (dbt
== VT_PTR
)
2326 else if (dbt
!= VT_LLONG
) {
2327 uint32_t m
= ((dbt
& VT_BTYPE
) == VT_BYTE
? 0xff :
2328 (dbt
& VT_BTYPE
) == VT_SHORT
? 0xffff :
2331 if (!(dbt
& VT_UNSIGNED
))
2332 vtop
->c
.i
|= -(vtop
->c
.i
& ((m
>> 1) + 1));
2335 } else if (p
&& dbt
== VT_BOOL
) {
2339 /* non constant case: generate code */
2341 /* convert from fp to fp */
2344 /* convert int to fp */
2347 /* convert fp to int */
2348 if (dbt
== VT_BOOL
) {
2352 /* we handle char/short/etc... with generic code */
2353 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
2354 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
2358 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
2359 /* additional cast for char/short... */
2364 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
2365 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
2366 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
2367 /* scalar to long long */
2368 /* machine independent conversion */
2370 /* generate high word */
2371 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
2375 if (sbt
== VT_PTR
) {
2376 /* cast from pointer to int before we apply
2377 shift operation, which pointers don't support*/
2378 gen_cast(&int_type
);
2384 /* patch second register */
2385 vtop
[-1].r2
= vtop
->r
;
2389 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
2390 (dbt
& VT_BTYPE
) == VT_PTR
||
2391 (dbt
& VT_BTYPE
) == VT_FUNC
) {
2392 if ((sbt
& VT_BTYPE
) != VT_LLONG
&&
2393 (sbt
& VT_BTYPE
) != VT_PTR
&&
2394 (sbt
& VT_BTYPE
) != VT_FUNC
) {
2395 /* need to convert from 32bit to 64bit */
2397 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
2398 #if defined(TCC_TARGET_ARM64)
2400 #elif defined(TCC_TARGET_X86_64)
2402 /* x86_64 specific: movslq */
2404 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
2411 } else if (dbt
== VT_BOOL
) {
2412 /* scalar to bool */
2415 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
2416 (dbt
& VT_BTYPE
) == VT_SHORT
) {
2417 if (sbt
== VT_PTR
) {
2418 vtop
->type
.t
= VT_INT
;
2419 tcc_warning("nonportable conversion from pointer to char/short");
2421 force_charshort_cast(dbt
);
2422 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
2423 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
2425 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
2426 /* from long long: just take low order word */
2430 /* if lvalue and single word type, nothing to do because
2431 the lvalue already contains the real type size (see
2432 VT_LVAL_xxx constants) */
2436 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
2437 /* if we are casting between pointer types,
2438 we must update the VT_LVAL_xxx size */
2439 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
2440 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
2445 /* return type size as known at compile time. Put alignment at 'a' */
2446 ST_FUNC
int type_size(CType
*type
, int *a
)
2451 bt
= type
->t
& VT_BTYPE
;
2452 if (bt
== VT_STRUCT
) {
2457 } else if (bt
== VT_PTR
) {
2458 if (type
->t
& VT_ARRAY
) {
2462 ts
= type_size(&s
->type
, a
);
2464 if (ts
< 0 && s
->c
< 0)
2472 } else if (bt
== VT_LDOUBLE
) {
2474 return LDOUBLE_SIZE
;
2475 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
2476 #ifdef TCC_TARGET_I386
2477 #ifdef TCC_TARGET_PE
2482 #elif defined(TCC_TARGET_ARM)
2492 } else if (bt
== VT_INT
|| bt
== VT_FLOAT
) {
2495 } else if (bt
== VT_SHORT
) {
2498 } else if (bt
== VT_QLONG
|| bt
== VT_QFLOAT
) {
2501 } else if (bt
== VT_ENUM
) {
2503 /* Enums might be incomplete, so don't just return '4' here. */
2504 return type
->ref
->c
;
2506 /* char, void, function, _Bool */
2512 /* push type size as known at runtime time on top of value stack. Put
2514 ST_FUNC
void vla_runtime_type_size(CType
*type
, int *a
)
2516 if (type
->t
& VT_VLA
) {
2517 type_size(&type
->ref
->type
, a
);
2518 vset(&int_type
, VT_LOCAL
|VT_LVAL
, type
->ref
->c
);
2520 vpushi(type_size(type
, a
));
2524 static void vla_sp_restore(void) {
2525 if (vlas_in_scope
) {
2526 gen_vla_sp_restore(vla_sp_loc
);
2530 static void vla_sp_restore_root(void) {
2531 if (vlas_in_scope
) {
2532 gen_vla_sp_restore(vla_sp_root_loc
);
2536 /* return the pointed type of t */
2537 static inline CType
*pointed_type(CType
*type
)
2539 return &type
->ref
->type
;
2542 /* modify type so that its it is a pointer to type. */
2543 ST_FUNC
void mk_pointer(CType
*type
)
2546 s
= sym_push(SYM_FIELD
, type
, 0, -1);
2547 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
2551 /* compare function types. OLD functions match any new functions */
2552 static int is_compatible_func(CType
*type1
, CType
*type2
)
2558 if (!is_compatible_types(&s1
->type
, &s2
->type
))
2560 /* check func_call */
2561 if (s1
->a
.func_call
!= s2
->a
.func_call
)
2563 /* XXX: not complete */
2564 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
2568 while (s1
!= NULL
) {
2571 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
2581 /* return true if type1 and type2 are the same. If unqualified is
2582 true, qualifiers on the types are ignored.
2584 - enums are not checked as gcc __builtin_types_compatible_p ()
2586 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2590 t1
= type1
->t
& VT_TYPE
;
2591 t2
= type2
->t
& VT_TYPE
;
2593 /* strip qualifiers before comparing */
2594 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2595 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2597 /* Default Vs explicit signedness only matters for char */
2598 if ((t1
& VT_BTYPE
) != VT_BYTE
) {
2602 /* An enum is compatible with (unsigned) int. Ideally we would
2603 store the enums signedness in type->ref.a.<some_bit> and
2604 only accept unsigned enums with unsigned int and vice versa.
2605 But one of our callers (gen_assign_cast) always strips VT_UNSIGNED
2606 from pointer target types, so we can't add it here either. */
2607 if ((t1
& VT_BTYPE
) == VT_ENUM
) {
2609 if (type1
->ref
->a
.unsigned_enum
)
2612 if ((t2
& VT_BTYPE
) == VT_ENUM
) {
2614 if (type2
->ref
->a
.unsigned_enum
)
2617 /* XXX: bitfields ? */
2620 /* test more complicated cases */
2621 bt1
= t1
& VT_BTYPE
;
2622 if (bt1
== VT_PTR
) {
2623 type1
= pointed_type(type1
);
2624 type2
= pointed_type(type2
);
2625 return is_compatible_types(type1
, type2
);
2626 } else if (bt1
== VT_STRUCT
) {
2627 return (type1
->ref
== type2
->ref
);
2628 } else if (bt1
== VT_FUNC
) {
2629 return is_compatible_func(type1
, type2
);
2635 /* return true if type1 and type2 are exactly the same (including
2638 static int is_compatible_types(CType
*type1
, CType
*type2
)
2640 return compare_types(type1
,type2
,0);
2643 /* return true if type1 and type2 are the same (ignoring qualifiers).
2645 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
2647 return compare_types(type1
,type2
,1);
2650 /* print a type. If 'varstr' is not NULL, then the variable is also
2651 printed in the type */
2653 /* XXX: add array and function pointers */
2654 static void type_to_str(char *buf
, int buf_size
,
2655 CType
*type
, const char *varstr
)
2662 t
= type
->t
& VT_TYPE
;
2665 if (t
& VT_CONSTANT
)
2666 pstrcat(buf
, buf_size
, "const ");
2667 if (t
& VT_VOLATILE
)
2668 pstrcat(buf
, buf_size
, "volatile ");
2669 if ((t
& (VT_DEFSIGN
| VT_UNSIGNED
)) == (VT_DEFSIGN
| VT_UNSIGNED
))
2670 pstrcat(buf
, buf_size
, "unsigned ");
2671 else if (t
& VT_DEFSIGN
)
2672 pstrcat(buf
, buf_size
, "signed ");
2702 tstr
= "long double";
2704 pstrcat(buf
, buf_size
, tstr
);
2708 if (bt
== VT_STRUCT
)
2712 pstrcat(buf
, buf_size
, tstr
);
2713 v
= type
->ref
->v
& ~SYM_STRUCT
;
2714 if (v
>= SYM_FIRST_ANOM
)
2715 pstrcat(buf
, buf_size
, "<anonymous>");
2717 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
2721 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
2722 pstrcat(buf
, buf_size
, "(");
2724 while (sa
!= NULL
) {
2725 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
2726 pstrcat(buf
, buf_size
, buf1
);
2729 pstrcat(buf
, buf_size
, ", ");
2731 pstrcat(buf
, buf_size
, ")");
2736 snprintf(buf1
, sizeof(buf1
), "%s[%ld]", varstr
? varstr
: "", s
->c
);
2737 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2740 pstrcpy(buf1
, sizeof(buf1
), "*");
2741 if (t
& VT_CONSTANT
)
2742 pstrcat(buf1
, buf_size
, "const ");
2743 if (t
& VT_VOLATILE
)
2744 pstrcat(buf1
, buf_size
, "volatile ");
2746 pstrcat(buf1
, sizeof(buf1
), varstr
);
2747 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2751 pstrcat(buf
, buf_size
, " ");
2752 pstrcat(buf
, buf_size
, varstr
);
2757 /* verify type compatibility to store vtop in 'dt' type, and generate
2759 static void gen_assign_cast(CType
*dt
)
2761 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
2762 char buf1
[256], buf2
[256];
2765 st
= &vtop
->type
; /* source type */
2766 dbt
= dt
->t
& VT_BTYPE
;
2767 sbt
= st
->t
& VT_BTYPE
;
2768 if (sbt
== VT_VOID
|| dbt
== VT_VOID
) {
2769 if (sbt
== VT_VOID
&& dbt
== VT_VOID
)
2771 It is Ok if both are void
2777 gcc accepts this program
2780 tcc_error("cannot cast from/to void");
2782 if (dt
->t
& VT_CONSTANT
)
2783 tcc_warning("assignment of read-only location");
2786 /* special cases for pointers */
2787 /* '0' can also be a pointer */
2788 if (is_null_pointer(vtop
))
2790 /* accept implicit pointer to integer cast with warning */
2791 if (is_integer_btype(sbt
)) {
2792 tcc_warning("assignment makes pointer from integer without a cast");
2795 type1
= pointed_type(dt
);
2796 /* a function is implicitely a function pointer */
2797 if (sbt
== VT_FUNC
) {
2798 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
2799 !is_compatible_types(pointed_type(dt
), st
))
2800 tcc_warning("assignment from incompatible pointer type");
2805 type2
= pointed_type(st
);
2806 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2807 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
2808 /* void * can match anything */
2810 /* exact type match, except for qualifiers */
2813 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2814 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2815 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2816 /* Like GCC don't warn by default for merely changes
2817 in pointer target signedness. Do warn for different
2818 base types, though, in particular for unsigned enums
2819 and signed int targets. */
2820 if ((tmp_type1
.t
& (VT_DEFSIGN
| VT_UNSIGNED
)) !=
2821 (tmp_type2
.t
& (VT_DEFSIGN
| VT_UNSIGNED
)) &&
2822 (tmp_type1
.t
& VT_BTYPE
) == (tmp_type2
.t
& VT_BTYPE
))
2825 tcc_warning("assignment from incompatible pointer type");
2828 /* check const and volatile */
2829 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
2830 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
2831 tcc_warning("assignment discards qualifiers from pointer target type");
2837 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
2838 tcc_warning("assignment makes integer from pointer without a cast");
2839 } else if (sbt
== VT_STRUCT
) {
2840 goto case_VT_STRUCT
;
2842 /* XXX: more tests */
2848 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2849 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2850 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2852 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
2853 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
2854 tcc_error("cannot cast '%s' to '%s'", buf1
, buf2
);
2862 /* store vtop in lvalue pushed on stack */
2863 ST_FUNC
void vstore(void)
2865 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
2867 ft
= vtop
[-1].type
.t
;
2868 sbt
= vtop
->type
.t
& VT_BTYPE
;
2869 dbt
= ft
& VT_BTYPE
;
2870 if ((((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2871 (sbt
== VT_INT
&& dbt
== VT_SHORT
))
2872 && !(vtop
->type
.t
& VT_BITFIELD
)) {
2873 /* optimize char/short casts */
2874 delayed_cast
= VT_MUSTCAST
;
2875 vtop
->type
.t
= (ft
& VT_TYPE
& ~VT_BITFIELD
&
2876 ((1 << VT_STRUCT_SHIFT
) - 1));
2877 /* XXX: factorize */
2878 if (ft
& VT_CONSTANT
)
2879 tcc_warning("assignment of read-only location");
2882 if (!(ft
& VT_BITFIELD
))
2883 gen_assign_cast(&vtop
[-1].type
);
2886 if (sbt
== VT_STRUCT
) {
2887 /* if structure, only generate pointer */
2888 /* structure assignment : generate memcpy */
2889 /* XXX: optimize if small size */
2890 size
= type_size(&vtop
->type
, &align
);
2894 vtop
->type
.t
= VT_PTR
;
2897 /* address of memcpy() */
2900 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2901 else if(!(align
& 3))
2902 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2905 /* Use memmove, rather than memcpy, as dest and src may be same: */
2906 vpush_global_sym(&func_old_type
, TOK_memmove
);
2911 vtop
->type
.t
= VT_PTR
;
2917 /* leave source on stack */
2918 } else if (ft
& VT_BITFIELD
) {
2919 /* bitfield store handling */
2921 /* save lvalue as expression result (example: s.b = s.a = n;) */
2922 vdup(), vtop
[-1] = vtop
[-2];
2924 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2925 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2926 /* remove bit field info to avoid loops */
2927 vtop
[-1].type
.t
= ft
& ~VT_BITFIELD
& ((1 << VT_STRUCT_SHIFT
) - 1);
2929 if((ft
& VT_BTYPE
) == VT_BOOL
) {
2930 gen_cast(&vtop
[-1].type
);
2931 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2934 /* duplicate destination */
2936 vtop
[-1] = vtop
[-2];
2938 /* mask and shift source */
2939 if((ft
& VT_BTYPE
) != VT_BOOL
) {
2940 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2941 vpushll((1ULL << bit_size
) - 1ULL);
2943 vpushi((1 << bit_size
) - 1);
2949 /* load destination, mask and or with source */
2951 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2952 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2954 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2960 /* ... and discard */
2964 #ifdef CONFIG_TCC_BCHECK
2965 /* bound check case */
2966 if (vtop
[-1].r
& VT_MUSTBOUND
) {
2975 #ifdef TCC_TARGET_X86_64
2976 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
2978 } else if ((ft
& VT_BTYPE
) == VT_QFLOAT
) {
2983 r
= gv(rc
); /* generate value */
2984 /* if lvalue was saved on stack, must read it */
2985 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
2987 t
= get_reg(RC_INT
);
2988 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2993 sv
.r
= VT_LOCAL
| VT_LVAL
;
2994 sv
.c
.i
= vtop
[-1].c
.i
;
2996 vtop
[-1].r
= t
| VT_LVAL
;
2998 /* two word case handling : store second register at word + 4 (or +8 for x86-64) */
2999 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
3000 if (((ft
& VT_BTYPE
) == VT_QLONG
) || ((ft
& VT_BTYPE
) == VT_QFLOAT
)) {
3001 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
3003 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
3004 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
3006 vtop
[-1].type
.t
= load_type
;
3009 /* convert to int to increment easily */
3010 vtop
->type
.t
= addr_type
;
3016 vtop
[-1].type
.t
= load_type
;
3017 /* XXX: it works because r2 is spilled last ! */
3018 store(vtop
->r2
, vtop
- 1);
3024 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
3025 vtop
->r
|= delayed_cast
;
3029 /* post defines POST/PRE add. c is the token ++ or -- */
3030 ST_FUNC
void inc(int post
, int c
)
3033 vdup(); /* save lvalue */
3035 gv_dup(); /* duplicate value */
3040 vpushi(c
- TOK_MID
);
3042 vstore(); /* store value */
3044 vpop(); /* if post op, return saved value */
3047 ST_FUNC
void parse_mult_str (CString
*astr
, const char *msg
)
3049 /* read the string */
3053 while (tok
== TOK_STR
) {
3054 /* XXX: add \0 handling too ? */
3055 cstr_cat(astr
, tokc
.str
.data
, -1);
3058 cstr_ccat(astr
, '\0');
3061 /* If I is >= 1 and a power of two, returns log2(i)+1.
3062 If I is 0 returns 0. */
3063 static int exact_log2p1(int i
)
3068 for (ret
= 1; i
>= 1 << 8; ret
+= 8)
3079 /* Parse GNUC __attribute__ extension. Currently, the following
3080 extensions are recognized:
3081 - aligned(n) : set data/function alignment.
3082 - packed : force data alignment to 1
3083 - section(x) : generate data/code in this section.
3084 - unused : currently ignored, but may be used someday.
3085 - regparm(n) : pass function parameters in registers (i386 only)
3087 static void parse_attribute(AttributeDef
*ad
)
3092 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
3096 while (tok
!= ')') {
3097 if (tok
< TOK_IDENT
)
3098 expect("attribute name");
3105 parse_mult_str(&astr
, "section name");
3106 ad
->section
= find_section(tcc_state
, (char *)astr
.data
);
3113 parse_mult_str(&astr
, "alias(\"target\")");
3114 ad
->alias_target
= /* save string as token, for later */
3115 tok_alloc((char*)astr
.data
, astr
.size
-1)->tok
;
3119 case TOK_VISIBILITY1
:
3120 case TOK_VISIBILITY2
:
3122 parse_mult_str(&astr
,
3123 "visibility(\"default|hidden|internal|protected\")");
3124 if (!strcmp (astr
.data
, "default"))
3125 ad
->a
.visibility
= STV_DEFAULT
;
3126 else if (!strcmp (astr
.data
, "hidden"))
3127 ad
->a
.visibility
= STV_HIDDEN
;
3128 else if (!strcmp (astr
.data
, "internal"))
3129 ad
->a
.visibility
= STV_INTERNAL
;
3130 else if (!strcmp (astr
.data
, "protected"))
3131 ad
->a
.visibility
= STV_PROTECTED
;
3133 expect("visibility(\"default|hidden|internal|protected\")");
3142 if (n
<= 0 || (n
& (n
- 1)) != 0)
3143 tcc_error("alignment must be a positive power of two");
3148 ad
->a
.aligned
= exact_log2p1(n
);
3149 if (n
!= 1 << (ad
->a
.aligned
- 1))
3150 tcc_error("alignment of %d is larger than implemented", n
);
3162 /* currently, no need to handle it because tcc does not
3163 track unused objects */
3167 /* currently, no need to handle it because tcc does not
3168 track unused objects */
3173 ad
->a
.func_call
= FUNC_CDECL
;
3178 ad
->a
.func_call
= FUNC_STDCALL
;
3180 #ifdef TCC_TARGET_I386
3190 ad
->a
.func_call
= FUNC_FASTCALL1
+ n
- 1;
3196 ad
->a
.func_call
= FUNC_FASTCALLW
;
3203 ad
->a
.mode
= VT_LLONG
+ 1;
3206 ad
->a
.mode
= VT_BYTE
+ 1;
3209 ad
->a
.mode
= VT_SHORT
+ 1;
3213 ad
->a
.mode
= VT_INT
+ 1;
3216 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
3223 ad
->a
.func_export
= 1;
3226 ad
->a
.func_import
= 1;
3229 if (tcc_state
->warn_unsupported
)
3230 tcc_warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
3231 /* skip parameters */
3233 int parenthesis
= 0;
3237 else if (tok
== ')')
3240 } while (parenthesis
&& tok
!= -1);
3253 static Sym
* find_field (CType
*type
, int v
)
3257 while ((s
= s
->next
) != NULL
) {
3258 if ((s
->v
& SYM_FIELD
) &&
3259 (s
->type
.t
& VT_BTYPE
) == VT_STRUCT
&&
3260 (s
->v
& ~SYM_FIELD
) >= SYM_FIRST_ANOM
) {
3261 Sym
*ret
= find_field (&s
->type
, v
);
3271 static void struct_add_offset (Sym
*s
, int offset
)
3273 while ((s
= s
->next
) != NULL
) {
3274 if ((s
->v
& SYM_FIELD
) &&
3275 (s
->type
.t
& VT_BTYPE
) == VT_STRUCT
&&
3276 (s
->v
& ~SYM_FIELD
) >= SYM_FIRST_ANOM
) {
3277 struct_add_offset(s
->type
.ref
, offset
);
3283 static void struct_layout(CType
*type
, AttributeDef
*ad
)
3285 int align
, maxalign
, offset
, c
, bit_pos
, bt
, prevbt
, prev_bit_size
;
3286 int pcc
= !tcc_state
->ms_bitfields
;
3289 maxalign
= 1 << (ad
->a
.aligned
- 1);
3295 prevbt
= VT_STRUCT
; /* make it never match */
3297 for (f
= type
->ref
->next
; f
; f
= f
->next
) {
3298 int typealign
, bit_size
;
3299 int size
= type_size(&f
->type
, &typealign
);
3300 if (f
->type
.t
& VT_BITFIELD
)
3301 bit_size
= (f
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
3304 if (bit_size
== 0 && pcc
) {
3305 /* Zero-width bit-fields in PCC mode aren't affected
3306 by any packing (attribute or pragma). */
3308 } else if (f
->r
> 1) {
3310 } else if (ad
->a
.packed
|| f
->r
== 1) {
3312 /* Packed fields or packed records don't let the base type
3313 influence the records type alignment. */
3318 if (type
->ref
->type
.t
!= TOK_STRUCT
) {
3319 if (pcc
&& bit_size
>= 0)
3320 size
= (bit_size
+ 7) >> 3;
3321 /* Bit position is already zero from our caller. */
3325 } else if (bit_size
< 0) {
3326 int addbytes
= pcc
? (bit_pos
+ 7) >> 3 : 0;
3329 c
= (c
+ addbytes
+ align
- 1) & -align
;
3335 /* A bit-field. Layout is more complicated. There are two
3336 options TCC implements: PCC compatible and MS compatible
3337 (PCC compatible is what GCC uses for almost all targets).
3338 In PCC layout the overall size of the struct (in c) is
3339 _excluding_ the current run of bit-fields (that is,
3340 there's at least additional bit_pos bits after c). In
3341 MS layout c does include the current run of bit-fields.
3343 This matters for calculating the natural alignment buckets
3346 /* 'align' will be used to influence records alignment,
3347 so it's the max of specified and type alignment, except
3348 in certain cases that depend on the mode. */
3349 if (align
< typealign
)
3352 /* In PCC layout a non-packed bit-field is placed adjacent
3353 to the preceding bit-fields, except if it would overflow
3354 its container (depending on base type) or it's a zero-width
3355 bit-field. Packed non-zero-width bit-fields always are
3357 int ofs
= (c
* 8 + bit_pos
) % (typealign
* 8);
3358 int ofs2
= ofs
+ bit_size
+ (typealign
* 8) - 1;
3359 if (bit_size
== 0 ||
3361 (ofs2
/ (typealign
* 8)) > (size
/typealign
))) {
3362 c
= (c
+ ((bit_pos
+ 7) >> 3) + typealign
- 1) & -typealign
;
3366 /* In PCC layout named bit-fields influence the alignment
3367 of the containing struct using the base types alignment,
3368 except for packed fields (which here have correct
3369 align/typealign). */
3370 if ((f
->v
& SYM_FIRST_ANOM
))
3373 bt
= f
->type
.t
& VT_BTYPE
;
3374 if ((bit_pos
+ bit_size
> size
* 8) ||
3375 (bit_size
> 0) == (bt
!= prevbt
)) {
3376 c
= (c
+ typealign
- 1) & -typealign
;
3379 /* In MS bitfield mode a bit-field run always uses
3380 at least as many bits as the underlying type.
3381 To start a new run it's also required that this
3382 or the last bit-field had non-zero width. */
3383 if (bit_size
|| prev_bit_size
)
3386 /* In MS layout the records alignment is normally
3387 influenced by the field, except for a zero-width
3388 field at the start of a run (but by further zero-width
3389 fields it is again). */
3390 if (bit_size
== 0 && prevbt
!= bt
)
3393 prev_bit_size
= bit_size
;
3395 f
->type
.t
= (f
->type
.t
& ~(0x3f << VT_STRUCT_SHIFT
))
3396 | (bit_pos
<< VT_STRUCT_SHIFT
);
3397 bit_pos
+= bit_size
;
3398 if (pcc
&& bit_pos
>= size
* 8) {
3400 bit_pos
-= size
* 8;
3403 if (align
> maxalign
)
3406 printf("set field %s offset=%d c=%d",
3407 get_tok_str(f
->v
& ~SYM_FIELD
, NULL
), offset
, c
);
3408 if (f
->type
.t
& VT_BITFIELD
) {
3409 printf(" pos=%d size=%d",
3410 (f
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
3411 (f
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
3416 if (f
->v
& SYM_FIRST_ANOM
&& (f
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
3418 /* An anonymous struct/union. Adjust member offsets
3419 to reflect the real offset of our containing struct.
3420 Also set the offset of this anon member inside
3421 the outer struct to be zero. Via this it
3422 works when accessing the field offset directly
3423 (from base object), as well as when recursing
3424 members in initializer handling. */
3425 int v2
= f
->type
.ref
->v
;
3426 if (!(v2
& SYM_FIELD
) &&
3427 (v2
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
3429 /* This happens only with MS extensions. The
3430 anon member has a named struct type, so it
3431 potentially is shared with other references.
3432 We need to unshare members so we can modify
3435 f
->type
.ref
= sym_push(anon_sym
++ | SYM_FIELD
,
3436 &f
->type
.ref
->type
, 0,
3438 pps
= &f
->type
.ref
->next
;
3439 while ((ass
= ass
->next
) != NULL
) {
3440 *pps
= sym_push(ass
->v
, &ass
->type
, 0, ass
->c
);
3441 pps
= &((*pps
)->next
);
3445 struct_add_offset(f
->type
.ref
, offset
);
3453 /* store size and alignment */
3454 type
->ref
->c
= (c
+ (pcc
? (bit_pos
+ 7) >> 3 : 0)
3455 + maxalign
- 1) & -maxalign
;
3456 type
->ref
->r
= maxalign
;
3459 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
3460 static void struct_decl(CType
*type
, AttributeDef
*ad
, int u
)
3462 int a
, v
, size
, align
, flexible
, alignoverride
;
3464 int bit_size
, bsize
, bt
;
3469 a
= tok
; /* save decl type */
3471 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3472 parse_attribute(ad
);
3476 /* struct already defined ? return it */
3478 expect("struct/union/enum name");
3480 if (s
&& (s
->scope
== local_scope
|| (tok
!= '{' && tok
!= ';'))) {
3482 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
3488 /* Record the original enum/struct/union token. */
3491 /* we put an undefined size for struct/union */
3492 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
3493 s
->r
= 0; /* default alignment is zero as gcc */
3494 /* put struct/union/enum name in type */
3502 tcc_error("struct/union/enum already defined");
3503 /* cannot be empty */
3505 /* non empty enums are not allowed */
3506 if (a
== TOK_ENUM
) {
3510 CType
*t
= &int_type
;
3513 expect("identifier");
3515 if (ss
&& !local_stack
)
3516 tcc_error("redefinition of enumerator '%s'",
3517 get_tok_str(v
, NULL
));
3521 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
3524 /* We really want to support long long enums
3525 on i386 as well, but the Sym structure only
3526 holds a 'long' for associated constants,
3527 and enlarging it would bump its size (no
3528 available padding). So punt for now. */
3534 if (c
!= (int)c
&& (unsigned long)c
!= (unsigned int)c
)
3535 seen_wide
= 1, t
= &size_type
;
3536 /* enum symbols have static storage */
3537 ss
= sym_push(v
, t
, VT_CONST
, c
);
3538 ss
->type
.t
|= VT_STATIC
;
3543 /* NOTE: we accept a trailing comma */
3548 s
->a
.unsigned_enum
= 1;
3549 s
->c
= type_size(seen_wide
? &size_type
: &int_type
, &align
);
3554 while (tok
!= '}') {
3555 if (!parse_btype(&btype
, &ad1
)) {
3561 tcc_error("flexible array member '%s' not at the end of struct",
3562 get_tok_str(v
, NULL
));
3567 type_decl(&type1
, &ad1
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3569 if ((type1
.t
& VT_BTYPE
) != VT_STRUCT
)
3570 expect("identifier");
3572 int v
= btype
.ref
->v
;
3573 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
3574 if (tcc_state
->ms_extensions
== 0)
3575 expect("identifier");
3579 if (type_size(&type1
, &align
) < 0) {
3580 if ((a
== TOK_STRUCT
) && (type1
.t
& VT_ARRAY
) && c
)
3583 tcc_error("field '%s' has incomplete type",
3584 get_tok_str(v
, NULL
));
3586 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
3587 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
3588 tcc_error("invalid type for '%s'",
3589 get_tok_str(v
, NULL
));
3593 bit_size
= expr_const();
3594 /* XXX: handle v = 0 case for messages */
3596 tcc_error("negative width in bit-field '%s'",
3597 get_tok_str(v
, NULL
));
3598 if (v
&& bit_size
== 0)
3599 tcc_error("zero width for bit-field '%s'",
3600 get_tok_str(v
, NULL
));
3601 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3602 parse_attribute(&ad1
);
3604 size
= type_size(&type1
, &align
);
3605 /* Only remember non-default alignment. */
3607 if (ad1
.a
.aligned
) {
3608 int speca
= 1 << (ad1
.a
.aligned
- 1);
3609 alignoverride
= speca
;
3610 } else if (ad1
.a
.packed
|| ad
->a
.packed
) {
3612 } else if (*tcc_state
->pack_stack_ptr
) {
3613 if (align
> *tcc_state
->pack_stack_ptr
)
3614 alignoverride
= *tcc_state
->pack_stack_ptr
;
3616 if (bit_size
>= 0) {
3617 bt
= type1
.t
& VT_BTYPE
;
3624 tcc_error("bitfields must have scalar type");
3626 if (bit_size
> bsize
) {
3627 tcc_error("width of '%s' exceeds its type",
3628 get_tok_str(v
, NULL
));
3629 } else if (bit_size
== bsize
) {
3630 /* no need for bit fields */
3633 type1
.t
|= VT_BITFIELD
|
3634 (0 << VT_STRUCT_SHIFT
) |
3635 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
3638 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
3639 /* Remember we've seen a real field to check
3640 for placement of flexible array member. */
3643 /* If member is a struct or bit-field, enforce
3644 placing into the struct (as anonymous). */
3646 ((type1
.t
& VT_BTYPE
) == VT_STRUCT
||
3651 ss
= sym_push(v
| SYM_FIELD
, &type1
, alignoverride
, 0);
3655 if (tok
== ';' || tok
== TOK_EOF
)
3662 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3663 parse_attribute(ad
);
3664 struct_layout(type
, ad
);
3669 /* return 1 if basic type is a type size (short, long, long long) */
3670 ST_FUNC
int is_btype_size(int bt
)
3672 return bt
== VT_SHORT
|| bt
== VT_LONG
|| bt
== VT_LLONG
;
3675 /* Add type qualifiers to a type. If the type is an array then the qualifiers
3676 are added to the element type, copied because it could be a typedef. */
3677 static void parse_btype_qualify(CType
*type
, int qualifiers
)
3679 while (type
->t
& VT_ARRAY
) {
3680 type
->ref
= sym_push(SYM_FIELD
, &type
->ref
->type
, 0, type
->ref
->c
);
3681 type
= &type
->ref
->type
;
3683 type
->t
|= qualifiers
;
3686 /* return 0 if no type declaration. otherwise, return the basic type
3689 static int parse_btype(CType
*type
, AttributeDef
*ad
)
3691 int t
, u
, bt_size
, complete
, type_found
, typespec_found
;
3695 memset(ad
, 0, sizeof(AttributeDef
));
3703 /* currently, we really ignore extension */
3714 tcc_error("too many basic types");
3716 bt_size
= is_btype_size (u
& VT_BTYPE
);
3717 if (u
== VT_INT
|| (!bt_size
&& !(t
& VT_TYPEDEF
)))
3732 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
3733 #ifndef TCC_TARGET_PE
3734 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3736 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
3737 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3743 #ifdef TCC_TARGET_ARM64
3745 /* GCC's __uint128_t appears in some Linux header files. Make it a
3746 synonym for long double to get the size and alignment right. */
3758 if ((t
& VT_BTYPE
) == VT_LONG
) {
3759 #ifdef TCC_TARGET_PE
3760 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
3762 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3770 struct_decl(&type1
, ad
, VT_ENUM
);
3773 type
->ref
= type1
.ref
;
3777 struct_decl(&type1
, ad
, VT_STRUCT
);
3780 /* type modifiers */
3785 parse_btype_qualify(type
, VT_CONSTANT
);
3793 parse_btype_qualify(type
, VT_VOLATILE
);
3800 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == (VT_DEFSIGN
|VT_UNSIGNED
))
3801 tcc_error("signed and unsigned modifier");
3814 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == VT_DEFSIGN
)
3815 tcc_error("signed and unsigned modifier");
3816 t
|= VT_DEFSIGN
| VT_UNSIGNED
;
3841 /* GNUC attribute */
3842 case TOK_ATTRIBUTE1
:
3843 case TOK_ATTRIBUTE2
:
3844 parse_attribute(ad
);
3847 t
= (t
& ~VT_BTYPE
) | u
;
3855 parse_expr_type(&type1
);
3856 /* remove all storage modifiers except typedef */
3857 type1
.t
&= ~(VT_STORAGE
&~VT_TYPEDEF
);
3863 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
3866 type
->t
= ((s
->type
.t
& ~VT_TYPEDEF
) |
3867 (t
& ~(VT_CONSTANT
| VT_VOLATILE
)));
3868 type
->ref
= s
->type
.ref
;
3869 if (t
& (VT_CONSTANT
| VT_VOLATILE
))
3870 parse_btype_qualify(type
, t
& (VT_CONSTANT
| VT_VOLATILE
));
3874 /* get attributes from typedef */
3875 if (0 == ad
->a
.aligned
)
3876 ad
->a
.aligned
= s
->a
.aligned
;
3877 if (0 == ad
->a
.func_call
)
3878 ad
->a
.func_call
= s
->a
.func_call
;
3879 ad
->a
.packed
|= s
->a
.packed
;
3888 if (tcc_state
->char_is_unsigned
) {
3889 if ((t
& (VT_DEFSIGN
|VT_BTYPE
)) == VT_BYTE
)
3893 /* long is never used as type */
3894 if ((t
& VT_BTYPE
) == VT_LONG
)
3895 #if (!defined TCC_TARGET_X86_64 && !defined TCC_TARGET_ARM64) || \
3896 defined TCC_TARGET_PE
3897 t
= (t
& ~VT_BTYPE
) | VT_INT
;
3899 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3905 /* convert a function parameter type (array to pointer and function to
3906 function pointer) */
3907 static inline void convert_parameter_type(CType
*pt
)
3909 /* remove const and volatile qualifiers (XXX: const could be used
3910 to indicate a const function parameter */
3911 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3912 /* array must be transformed to pointer according to ANSI C */
3914 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
3919 ST_FUNC
void parse_asm_str(CString
*astr
)
3922 parse_mult_str(astr
, "string constant");
3925 /* Parse an asm label and return the token */
3926 static int asm_label_instr(void)
3932 parse_asm_str(&astr
);
3935 printf("asm_alias: \"%s\"\n", (char *)astr
.data
);
3937 v
= tok_alloc(astr
.data
, astr
.size
- 1)->tok
;
3942 static void post_type(CType
*type
, AttributeDef
*ad
, int storage
)
3944 int n
, l
, t1
, arg_size
, align
;
3945 Sym
**plast
, *s
, *first
;
3950 /* function declaration */
3958 /* read param name and compute offset */
3959 if (l
!= FUNC_OLD
) {
3960 if (!parse_btype(&pt
, &ad1
)) {
3962 tcc_error("invalid type");
3969 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
3971 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3972 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
3973 tcc_error("parameter declared as void");
3974 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
3979 expect("identifier");
3983 convert_parameter_type(&pt
);
3984 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
3990 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
3997 /* if no parameters, then old type prototype */
4001 /* NOTE: const is ignored in returned type as it has a special
4002 meaning in gcc / C++ */
4003 type
->t
&= ~VT_CONSTANT
;
4004 /* some ancient pre-K&R C allows a function to return an array
4005 and the array brackets to be put after the arguments, such
4006 that "int c()[]" means something like "int[] c()" */
4009 skip(']'); /* only handle simple "[]" */
4012 /* we push a anonymous symbol which will contain the function prototype */
4013 ad
->a
.func_args
= arg_size
;
4014 s
= sym_push(SYM_FIELD
, type
, 0, l
);
4019 } else if (tok
== '[') {
4020 int saved_nocode_wanted
= nocode_wanted
;
4021 /* array definition */
4023 if (tok
== TOK_RESTRICT1
)
4028 if (!local_stack
|| (storage
& VT_STATIC
))
4029 vpushi(expr_const());
4031 /* VLAs (which can only happen with local_stack && !VT_STATIC)
4032 length must always be evaluated, even under nocode_wanted,
4033 so that its size slot is initialized (e.g. under sizeof
4038 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4041 tcc_error("invalid array size");
4043 if (!is_integer_btype(vtop
->type
.t
& VT_BTYPE
))
4044 tcc_error("size of variable length array should be an integer");
4049 /* parse next post type */
4050 post_type(type
, ad
, storage
);
4051 if (type
->t
== VT_FUNC
)
4052 tcc_error("declaration of an array of functions");
4053 t1
|= type
->t
& VT_VLA
;
4056 loc
-= type_size(&int_type
, &align
);
4060 vla_runtime_type_size(type
, &align
);
4062 vset(&int_type
, VT_LOCAL
|VT_LVAL
, n
);
4068 nocode_wanted
= saved_nocode_wanted
;
4070 /* we push an anonymous symbol which will contain the array
4072 s
= sym_push(SYM_FIELD
, type
, 0, n
);
4073 type
->t
= (t1
? VT_VLA
: VT_ARRAY
) | VT_PTR
;
4078 /* Parse a type declaration (except basic type), and return the type
4079 in 'type'. 'td' is a bitmask indicating which kind of type decl is
4080 expected. 'type' should contain the basic type. 'ad' is the
4081 attribute definition of the basic type. It can be modified by
4084 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
4087 CType type1
, *type2
;
4088 int qualifiers
, storage
;
4090 while (tok
== '*') {
4098 qualifiers
|= VT_CONSTANT
;
4103 qualifiers
|= VT_VOLATILE
;
4109 /* XXX: clarify attribute handling */
4110 case TOK_ATTRIBUTE1
:
4111 case TOK_ATTRIBUTE2
:
4112 parse_attribute(ad
);
4116 type
->t
|= qualifiers
;
4119 /* recursive type */
4120 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
4121 type1
.t
= 0; /* XXX: same as int */
4124 /* XXX: this is not correct to modify 'ad' at this point, but
4125 the syntax is not clear */
4126 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
4127 parse_attribute(ad
);
4128 type_decl(&type1
, ad
, v
, td
);
4131 /* type identifier */
4132 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
4136 if (!(td
& TYPE_ABSTRACT
))
4137 expect("identifier");
4141 storage
= type
->t
& VT_STORAGE
;
4142 type
->t
&= ~VT_STORAGE
;
4143 post_type(type
, ad
, storage
);
4145 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
4146 parse_attribute(ad
);
4150 /* append type at the end of type1 */
4163 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
4164 ST_FUNC
int lvalue_type(int t
)
4169 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
4171 else if (bt
== VT_SHORT
)
4175 if (t
& VT_UNSIGNED
)
4176 r
|= VT_LVAL_UNSIGNED
;
4180 /* indirection with full error checking and bound check */
4181 ST_FUNC
void indir(void)
4183 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
4184 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
4188 if (vtop
->r
& VT_LVAL
)
4190 vtop
->type
= *pointed_type(&vtop
->type
);
4191 /* Arrays and functions are never lvalues */
4192 if (!(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_VLA
)
4193 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
4194 vtop
->r
|= lvalue_type(vtop
->type
.t
);
4195 /* if bound checking, the referenced pointer must be checked */
4196 #ifdef CONFIG_TCC_BCHECK
4197 if (tcc_state
->do_bounds_check
)
4198 vtop
->r
|= VT_MUSTBOUND
;
4203 /* pass a parameter to a function and do type checking and casting */
4204 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
4209 func_type
= func
->c
;
4210 if (func_type
== FUNC_OLD
||
4211 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
4212 /* default casting : only need to convert float to double */
4213 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
4216 } else if (vtop
->type
.t
& VT_BITFIELD
) {
4217 type
.t
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
4218 type
.ref
= vtop
->type
.ref
;
4221 } else if (arg
== NULL
) {
4222 tcc_error("too many arguments to function");
4225 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
4226 gen_assign_cast(&type
);
4230 /* parse an expression of the form '(type)' or '(expr)' and return its
4232 static void parse_expr_type(CType
*type
)
4238 if (parse_btype(type
, &ad
)) {
4239 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
4246 static void parse_type(CType
*type
)
4251 if (!parse_btype(type
, &ad
)) {
4254 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
4257 static void vpush_tokc(int t
)
4262 vsetc(&type
, VT_CONST
, &tokc
);
4265 ST_FUNC
void unary(void)
4267 int n
, t
, align
, size
, r
, sizeof_caller
;
4272 sizeof_caller
= in_sizeof
;
4274 /* XXX: GCC 2.95.3 does not generate a table although it should be
4288 vpush_tokc(VT_INT
| VT_UNSIGNED
);
4292 vpush_tokc(VT_LLONG
);
4296 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
4300 vpush_tokc(VT_FLOAT
);
4304 vpush_tokc(VT_DOUBLE
);
4308 vpush_tokc(VT_LDOUBLE
);
4311 case TOK___FUNCTION__
:
4313 goto tok_identifier
;
4319 /* special function name identifier */
4320 len
= strlen(funcname
) + 1;
4321 /* generate char[len] type */
4326 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
4327 ptr
= section_ptr_add(data_section
, len
);
4328 memcpy(ptr
, funcname
, len
);
4333 #ifdef TCC_TARGET_PE
4334 t
= VT_SHORT
| VT_UNSIGNED
;
4340 /* string parsing */
4343 if (tcc_state
->warn_write_strings
)
4348 memset(&ad
, 0, sizeof(AttributeDef
));
4349 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, 0);
4354 if (parse_btype(&type
, &ad
)) {
4355 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
4357 /* check ISOC99 compound literal */
4359 /* data is allocated locally by default */
4364 /* all except arrays are lvalues */
4365 if (!(type
.t
& VT_ARRAY
))
4366 r
|= lvalue_type(type
.t
);
4367 memset(&ad
, 0, sizeof(AttributeDef
));
4368 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, 0);
4370 if (sizeof_caller
) {
4377 } else if (tok
== '{') {
4378 int saved_nocode_wanted
= nocode_wanted
;
4380 tcc_error("expected constant");
4381 /* save all registers */
4383 /* statement expression : we do not accept break/continue
4384 inside as GCC does. We do retain the nocode_wanted state,
4385 as statement expressions can't ever be entered from the
4386 outside, so any reactivation of code emission (from labels
4387 or loop heads) can be disabled again after the end of it. */
4388 block(NULL
, NULL
, 1);
4389 nocode_wanted
= saved_nocode_wanted
;
4404 /* functions names must be treated as function pointers,
4405 except for unary '&' and sizeof. Since we consider that
4406 functions are not lvalues, we only have to handle it
4407 there and in function calls. */
4408 /* arrays can also be used although they are not lvalues */
4409 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
4410 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
4412 mk_pointer(&vtop
->type
);
4418 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4420 boolean
.t
= VT_BOOL
;
4422 vtop
->c
.i
= !vtop
->c
.i
;
4423 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
4427 vseti(VT_JMP
, gvtst(1, 0));
4439 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
4440 tcc_error("pointer not accepted for unary plus");
4441 /* In order to force cast, we add zero, except for floating point
4442 where we really need an noop (otherwise -0.0 will be transformed
4444 if (!is_float(vtop
->type
.t
)) {
4455 unary_type(&type
); // Perform a in_sizeof = 0;
4456 size
= type_size(&type
, &align
);
4457 if (t
== TOK_SIZEOF
) {
4458 if (!(type
.t
& VT_VLA
)) {
4460 tcc_error("sizeof applied to an incomplete type");
4463 vla_runtime_type_size(&type
, &align
);
4468 vtop
->type
.t
|= VT_UNSIGNED
;
4471 case TOK_builtin_expect
:
4473 /* __builtin_expect is a no-op for now */
4485 case TOK_builtin_types_compatible_p
:
4494 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
4495 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
4496 vpushi(is_compatible_types(&type1
, &type2
));
4499 case TOK_builtin_choose_expr
:
4526 case TOK_builtin_constant_p
:
4533 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
4540 case TOK_builtin_frame_address
:
4541 case TOK_builtin_return_address
:
4548 if (tok
!= TOK_CINT
) {
4549 tcc_error("%s only takes positive integers",
4550 tok1
== TOK_builtin_return_address
?
4551 "__builtin_return_address" :
4552 "__builtin_frame_address");
4554 level
= (uint32_t)tokc
.i
;
4559 vset(&type
, VT_LOCAL
, 0); /* local frame */
4561 mk_pointer(&vtop
->type
);
4562 indir(); /* -> parent frame */
4564 if (tok1
== TOK_builtin_return_address
) {
4565 // assume return address is just above frame pointer on stack
4568 mk_pointer(&vtop
->type
);
4573 #ifdef TCC_TARGET_X86_64
4574 #ifdef TCC_TARGET_PE
4575 case TOK_builtin_va_start
:
4583 if ((vtop
->r
& VT_VALMASK
) != VT_LOCAL
)
4584 tcc_error("__builtin_va_start expects a local variable");
4585 vtop
->r
&= ~(VT_LVAL
| VT_REF
);
4586 vtop
->type
= char_pointer_type
;
4592 case TOK_builtin_va_arg_types
:
4599 vpushi(classify_x86_64_va_arg(&type
));
4605 #ifdef TCC_TARGET_ARM64
4606 case TOK___va_start
: {
4616 vtop
->type
.t
= VT_VOID
;
4619 case TOK___va_arg
: {
4632 case TOK___arm64_clear_cache
: {
4641 vtop
->type
.t
= VT_VOID
;
4645 /* pre operations */
4656 t
= vtop
->type
.t
& VT_BTYPE
;
4658 /* In IEEE negate(x) isn't subtract(0,x), but rather
4663 else if (t
== VT_DOUBLE
)
4674 goto tok_identifier
;
4676 /* allow to take the address of a label */
4677 if (tok
< TOK_UIDENT
)
4678 expect("label identifier");
4679 s
= label_find(tok
);
4681 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4683 if (s
->r
== LABEL_DECLARED
)
4684 s
->r
= LABEL_FORWARD
;
4687 s
->type
.t
= VT_VOID
;
4688 mk_pointer(&s
->type
);
4689 s
->type
.t
|= VT_STATIC
;
4691 vpushsym(&s
->type
, s
);
4695 // special qnan , snan and infinity values
4697 vpush64(VT_DOUBLE
, 0x7ff8000000000000ULL
);
4701 vpush64(VT_DOUBLE
, 0x7ff0000000000001ULL
);
4705 vpush64(VT_DOUBLE
, 0x7ff0000000000000ULL
);
4714 expect("identifier");
4717 const char *name
= get_tok_str(t
, NULL
);
4719 tcc_error("'%s' undeclared", name
);
4720 /* for simple function calls, we tolerate undeclared
4721 external reference to int() function */
4722 if (tcc_state
->warn_implicit_function_declaration
4723 #ifdef TCC_TARGET_PE
4724 /* people must be warned about using undeclared WINAPI functions
4725 (which usually start with uppercase letter) */
4726 || (name
[0] >= 'A' && name
[0] <= 'Z')
4729 tcc_warning("implicit declaration of function '%s'", name
);
4730 s
= external_global_sym(t
, &func_old_type
, 0);
4732 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
4733 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
4734 /* if referencing an inline function, then we generate a
4735 symbol to it if not already done. It will have the
4736 effect to generate code for it at the end of the
4737 compilation unit. Inline function as always
4738 generated in the text section. */
4739 if (!s
->c
&& !nocode_wanted
)
4740 put_extern_sym(s
, text_section
, 0, 0);
4741 r
= VT_SYM
| VT_CONST
;
4744 /* A symbol that has a register is a local register variable,
4745 which starts out as VT_LOCAL value. */
4746 if ((r
& VT_VALMASK
) < VT_CONST
)
4747 r
= (r
& ~VT_VALMASK
) | VT_LOCAL
;
4749 vset(&s
->type
, r
, s
->c
);
4750 /* Point to s as backpointer (even without r&VT_SYM).
4751 Will be used by at least the x86 inline asm parser for
4754 if (vtop
->r
& VT_SYM
) {
4760 /* post operations */
4762 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
4765 } else if (tok
== '.' || tok
== TOK_ARROW
|| tok
== TOK_CDOUBLE
) {
4768 if (tok
== TOK_ARROW
)
4770 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
4773 /* expect pointer on structure */
4774 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
4775 expect("struct or union");
4776 if (tok
== TOK_CDOUBLE
)
4777 expect("field name");
4779 if (tok
== TOK_CINT
|| tok
== TOK_CUINT
)
4780 expect("field name");
4781 s
= find_field(&vtop
->type
, tok
);
4783 tcc_error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, &tokc
));
4784 /* add field offset to pointer */
4785 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
4788 /* change type to field type, and set to lvalue */
4789 vtop
->type
= s
->type
;
4790 vtop
->type
.t
|= qualifiers
;
4791 /* an array is never an lvalue */
4792 if (!(vtop
->type
.t
& VT_ARRAY
)) {
4793 vtop
->r
|= lvalue_type(vtop
->type
.t
);
4794 #ifdef CONFIG_TCC_BCHECK
4795 /* if bound checking, the referenced pointer must be checked */
4796 if (tcc_state
->do_bounds_check
&& (vtop
->r
& VT_VALMASK
) != VT_LOCAL
)
4797 vtop
->r
|= VT_MUSTBOUND
;
4801 } else if (tok
== '[') {
4807 } else if (tok
== '(') {
4810 int nb_args
, ret_nregs
, ret_align
, regsize
, variadic
;
4813 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
4814 /* pointer test (no array accepted) */
4815 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
4816 vtop
->type
= *pointed_type(&vtop
->type
);
4817 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
4821 expect("function pointer");
4824 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
4826 /* get return type */
4829 sa
= s
->next
; /* first parameter */
4832 /* compute first implicit argument if a structure is returned */
4833 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
4834 variadic
= (s
->c
== FUNC_ELLIPSIS
);
4835 ret_nregs
= gfunc_sret(&s
->type
, variadic
, &ret
.type
,
4836 &ret_align
, ®size
);
4838 /* get some space for the returned structure */
4839 size
= type_size(&s
->type
, &align
);
4840 #ifdef TCC_TARGET_ARM64
4841 /* On arm64, a small struct is return in registers.
4842 It is much easier to write it to memory if we know
4843 that we are allowed to write some extra bytes, so
4844 round the allocated space up to a power of 2: */
4846 while (size
& (size
- 1))
4847 size
= (size
| (size
- 1)) + 1;
4849 loc
= (loc
- size
) & -align
;
4851 ret
.r
= VT_LOCAL
| VT_LVAL
;
4852 /* pass it as 'int' to avoid structure arg passing
4854 vseti(VT_LOCAL
, loc
);
4864 /* return in register */
4865 if (is_float(ret
.type
.t
)) {
4866 ret
.r
= reg_fret(ret
.type
.t
);
4867 #ifdef TCC_TARGET_X86_64
4868 if ((ret
.type
.t
& VT_BTYPE
) == VT_QFLOAT
)
4872 #ifndef TCC_TARGET_ARM64
4873 #ifdef TCC_TARGET_X86_64
4874 if ((ret
.type
.t
& VT_BTYPE
) == VT_QLONG
)
4876 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
4887 gfunc_param_typed(s
, sa
);
4897 tcc_error("too few arguments to function");
4899 gfunc_call(nb_args
);
4902 for (r
= ret
.r
+ ret_nregs
+ !ret_nregs
; r
-- > ret
.r
;) {
4903 vsetc(&ret
.type
, r
, &ret
.c
);
4904 vtop
->r2
= ret
.r2
; /* Loop only happens when r2 is VT_CONST */
4907 /* handle packed struct return */
4908 if (((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) && ret_nregs
) {
4911 size
= type_size(&s
->type
, &align
);
4912 /* We're writing whole regs often, make sure there's enough
4913 space. Assume register size is power of 2. */
4914 if (regsize
> align
)
4916 loc
= (loc
- size
) & -align
;
4920 vset(&ret
.type
, VT_LOCAL
| VT_LVAL
, addr
+ offset
);
4924 if (--ret_nregs
== 0)
4928 vset(&s
->type
, VT_LOCAL
| VT_LVAL
, addr
);
4936 ST_FUNC
void expr_prod(void)
4941 while (tok
== '*' || tok
== '/' || tok
== '%') {
4949 ST_FUNC
void expr_sum(void)
4954 while (tok
== '+' || tok
== '-') {
4962 static void expr_shift(void)
4967 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
4975 static void expr_cmp(void)
4980 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
4981 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
4989 static void expr_cmpeq(void)
4994 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
5002 static void expr_and(void)
5005 while (tok
== '&') {
5012 static void expr_xor(void)
5015 while (tok
== '^') {
5022 static void expr_or(void)
5025 while (tok
== '|') {
5032 /* XXX: fix this mess */
5033 static void expr_land_const(void)
5036 while (tok
== TOK_LAND
) {
5042 static void expr_lor_const(void)
5045 while (tok
== TOK_LOR
) {
5052 static void expr_land(void)
5055 if (tok
== TOK_LAND
) {
5058 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
5066 while (tok
== TOK_LAND
) {
5074 gen_cast(&int_type
);
5082 if (tok
!= TOK_LAND
) {
5095 static void expr_lor(void)
5098 if (tok
== TOK_LOR
) {
5101 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
5109 while (tok
== TOK_LOR
) {
5117 gen_cast(&int_type
);
5125 if (tok
!= TOK_LOR
) {
5138 /* Assuming vtop is a value used in a conditional context
5139 (i.e. compared with zero) return 0 if it's false, 1 if
5140 true and -1 if it can't be statically determined. */
5141 static int condition_3way(void)
5144 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
&&
5145 (!(vtop
->r
& VT_SYM
) ||
5146 !(vtop
->sym
->type
.t
& VT_WEAK
))) {
5148 boolean
.t
= VT_BOOL
;
5157 static void expr_cond(void)
5159 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
, islv
, c
, g
;
5161 CType type
, type1
, type2
;
5166 c
= condition_3way();
5167 g
= (tok
== ':' && gnu_ext
);
5169 /* needed to avoid having different registers saved in
5171 if (is_float(vtop
->type
.t
)) {
5173 #ifdef TCC_TARGET_X86_64
5174 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
5199 sv
= *vtop
; /* save value to handle it later */
5200 vtop
--; /* no vpop so that FP stack is not flushed */
5218 bt1
= t1
& VT_BTYPE
;
5220 bt2
= t2
& VT_BTYPE
;
5221 /* cast operands to correct type according to ISOC rules */
5222 if (is_float(bt1
) || is_float(bt2
)) {
5223 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
5224 type
.t
= VT_LDOUBLE
;
5226 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
5231 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
5232 /* cast to biggest op */
5234 /* convert to unsigned if it does not fit in a long long */
5235 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
5236 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
5237 type
.t
|= VT_UNSIGNED
;
5238 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
5239 /* If one is a null ptr constant the result type
5241 if (is_null_pointer (vtop
))
5243 else if (is_null_pointer (&sv
))
5245 /* XXX: test pointer compatibility, C99 has more elaborate
5249 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
5250 /* XXX: test function pointer compatibility */
5251 type
= bt1
== VT_FUNC
? type1
: type2
;
5252 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
5253 /* XXX: test structure compatibility */
5254 type
= bt1
== VT_STRUCT
? type1
: type2
;
5255 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
5256 /* NOTE: as an extension, we accept void on only one side */
5259 /* integer operations */
5261 /* convert to unsigned if it does not fit in an integer */
5262 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
5263 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
5264 type
.t
|= VT_UNSIGNED
;
5266 /* keep structs lvalue by transforming `(expr ? a : b)` to `*(expr ? &a : &b)` so
5267 that `(expr ? a : b).mem` does not error with "lvalue expected" */
5268 islv
= (vtop
->r
& VT_LVAL
) && (sv
.r
& VT_LVAL
) && VT_STRUCT
== (type
.t
& VT_BTYPE
);
5271 /* now we convert second operand */
5275 mk_pointer(&vtop
->type
);
5277 } else if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
5282 if (is_float(type
.t
)) {
5284 #ifdef TCC_TARGET_X86_64
5285 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
5289 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
5290 /* for long longs, we use fixed registers to avoid having
5291 to handle a complicated move */
5302 /* this is horrible, but we must also convert first
5308 mk_pointer(&vtop
->type
);
5310 } else if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
5316 move_reg(r2
, r1
, type
.t
);
5326 static void expr_eq(void)
5332 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
5333 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
5334 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
5349 ST_FUNC
void gexpr(void)
5360 /* parse an expression and return its type without any side effect. */
5361 static void expr_type(CType
*type
)
5371 /* parse a unary expression and return its type without any side
5373 static void unary_type(CType
*type
)
5382 /* parse a constant expression and return value in vtop. */
5383 static void expr_const1(void)
5390 /* parse an integer constant and return its value. */
5391 static inline int64_t expr_const64(void)
5395 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
5396 expect("constant expression");
5402 /* parse an integer constant and return its value.
5403 Complain if it doesn't fit 32bit (signed or unsigned). */
5404 ST_FUNC
int expr_const(void)
5407 int64_t wc
= expr_const64();
5409 if (c
!= wc
&& (unsigned)c
!= wc
)
5410 tcc_error("constant exceeds 32 bit");
5414 /* return the label token if current token is a label, otherwise
5416 static int is_label(void)
5420 /* fast test first */
5421 if (tok
< TOK_UIDENT
)
5423 /* no need to save tokc because tok is an identifier */
5430 unget_tok(last_tok
);
5435 static void label_or_decl(int l
)
5439 /* fast test first */
5440 if (tok
>= TOK_UIDENT
)
5442 /* no need to save tokc because tok is an identifier */
5446 unget_tok(last_tok
);
5449 unget_tok(last_tok
);
5454 static int case_cmp(const void *pa
, const void *pb
)
5456 int64_t a
= (*(struct case_t
**) pa
)->v1
;
5457 int64_t b
= (*(struct case_t
**) pb
)->v1
;
5458 return a
< b
? -1 : a
> b
;
5461 static void gcase(struct case_t
**base
, int len
, int *bsym
)
5465 int ll
= (vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
;
5483 gtst_addr(0, p
->sym
); /* v1 <= x <= v2 */
5485 gcase(base
, len
/2, bsym
);
5486 if (cur_switch
->def_sym
)
5487 gjmp_addr(cur_switch
->def_sym
);
5489 *bsym
= gjmp(*bsym
);
5493 base
+= e
; len
-= e
;
5503 if (p
->v1
== p
->v2
) {
5505 gtst_addr(0, p
->sym
);
5515 gtst_addr(0, p
->sym
);
5521 static void block(int *bsym
, int *csym
, int is_expr
)
5523 int a
, b
, c
, d
, cond
;
5526 /* generate line number info */
5527 if (tcc_state
->do_debug
&&
5528 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
5529 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
5531 last_line_num
= file
->line_num
;
5535 /* default return value is (void) */
5537 vtop
->type
.t
= VT_VOID
;
5540 if (tok
== TOK_IF
) {
5542 int saved_nocode_wanted
= nocode_wanted
;
5547 cond
= condition_3way();
5553 nocode_wanted
|= 0x20000000;
5554 block(bsym
, csym
, 0);
5556 nocode_wanted
= saved_nocode_wanted
;
5558 if (c
== TOK_ELSE
) {
5563 nocode_wanted
|= 0x20000000;
5564 block(bsym
, csym
, 0);
5565 gsym(d
); /* patch else jmp */
5567 nocode_wanted
= saved_nocode_wanted
;
5570 } else if (tok
== TOK_WHILE
) {
5571 int saved_nocode_wanted
;
5572 nocode_wanted
&= ~0x20000000;
5582 saved_nocode_wanted
= nocode_wanted
;
5584 nocode_wanted
= saved_nocode_wanted
;
5589 } else if (tok
== '{') {
5591 int block_vla_sp_loc
= vla_sp_loc
, saved_vlas_in_scope
= vlas_in_scope
;
5594 /* record local declaration stack position */
5596 llabel
= local_label_stack
;
5599 /* handle local labels declarations */
5600 if (tok
== TOK_LABEL
) {
5603 if (tok
< TOK_UIDENT
)
5604 expect("label identifier");
5605 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
5615 while (tok
!= '}') {
5616 label_or_decl(VT_LOCAL
);
5620 block(bsym
, csym
, is_expr
);
5623 /* pop locally defined labels */
5624 label_pop(&local_label_stack
, llabel
);
5625 /* pop locally defined symbols */
5627 /* In the is_expr case (a statement expression is finished here),
5628 vtop might refer to symbols on the local_stack. Either via the
5629 type or via vtop->sym. We can't pop those nor any that in turn
5630 might be referred to. To make it easier we don't roll back
5631 any symbols in that case; some upper level call to block() will
5632 do that. We do have to remove such symbols from the lookup
5633 tables, though. sym_pop will do that. */
5634 sym_pop(&local_stack
, s
, is_expr
);
5636 /* Pop VLA frames and restore stack pointer if required */
5637 if (vlas_in_scope
> saved_vlas_in_scope
) {
5638 vla_sp_loc
= saved_vlas_in_scope
? block_vla_sp_loc
: vla_sp_root_loc
;
5641 vlas_in_scope
= saved_vlas_in_scope
;
5644 } else if (tok
== TOK_RETURN
) {
5648 gen_assign_cast(&func_vt
);
5649 #ifdef TCC_TARGET_ARM64
5650 // Perhaps it would be better to use this for all backends:
5653 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
5654 CType type
, ret_type
;
5655 int ret_align
, ret_nregs
, regsize
;
5656 ret_nregs
= gfunc_sret(&func_vt
, func_var
, &ret_type
,
5657 &ret_align
, ®size
);
5658 if (0 == ret_nregs
) {
5659 /* if returning structure, must copy it to implicit
5660 first pointer arg location */
5663 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
5666 /* copy structure value to pointer */
5669 /* returning structure packed into registers */
5670 int r
, size
, addr
, align
;
5671 size
= type_size(&func_vt
,&align
);
5672 if ((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) ||
5673 (vtop
->c
.i
& (ret_align
-1)))
5674 && (align
& (ret_align
-1))) {
5675 loc
= (loc
- size
) & -ret_align
;
5678 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
5682 vset(&ret_type
, VT_LOCAL
| VT_LVAL
, addr
);
5684 vtop
->type
= ret_type
;
5685 if (is_float(ret_type
.t
))
5686 r
= rc_fret(ret_type
.t
);
5697 if (--ret_nregs
== 0)
5699 /* We assume that when a structure is returned in multiple
5700 registers, their classes are consecutive values of the
5703 vtop
->c
.i
+= regsize
;
5707 } else if (is_float(func_vt
.t
)) {
5708 gv(rc_fret(func_vt
.t
));
5713 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
5716 /* jump unless last stmt in top-level block */
5717 if (tok
!= '}' || local_scope
!= 1)
5719 nocode_wanted
|= 0x20000000;
5720 } else if (tok
== TOK_BREAK
) {
5723 tcc_error("cannot break");
5724 *bsym
= gjmp(*bsym
);
5727 nocode_wanted
|= 0x20000000;
5728 } else if (tok
== TOK_CONTINUE
) {
5731 tcc_error("cannot continue");
5732 vla_sp_restore_root();
5733 *csym
= gjmp(*csym
);
5736 } else if (tok
== TOK_FOR
) {
5738 int saved_nocode_wanted
;
5739 nocode_wanted
&= ~0x20000000;
5745 /* c99 for-loop init decl? */
5746 if (!decl0(VT_LOCAL
, 1)) {
5747 /* no, regular for-loop init expr */
5773 saved_nocode_wanted
= nocode_wanted
;
5775 nocode_wanted
= saved_nocode_wanted
;
5780 sym_pop(&local_stack
, s
, 0);
5783 if (tok
== TOK_DO
) {
5784 int saved_nocode_wanted
;
5785 nocode_wanted
&= ~0x20000000;
5791 saved_nocode_wanted
= nocode_wanted
;
5799 nocode_wanted
= saved_nocode_wanted
;
5804 if (tok
== TOK_SWITCH
) {
5805 struct switch_t
*saved
, sw
;
5806 int saved_nocode_wanted
= nocode_wanted
;
5812 switchval
= *vtop
--;
5814 b
= gjmp(0); /* jump to first case */
5815 sw
.p
= NULL
; sw
.n
= 0; sw
.def_sym
= 0;
5819 nocode_wanted
= saved_nocode_wanted
;
5820 a
= gjmp(a
); /* add implicit break */
5823 qsort(sw
.p
, sw
.n
, sizeof(void*), case_cmp
);
5824 for (b
= 1; b
< sw
.n
; b
++)
5825 if (sw
.p
[b
- 1]->v2
>= sw
.p
[b
]->v1
)
5826 tcc_error("duplicate case value");
5827 /* Our switch table sorting is signed, so the compared
5828 value needs to be as well when it's 64bit. */
5829 if ((switchval
.type
.t
& VT_BTYPE
) == VT_LLONG
)
5830 switchval
.type
.t
&= ~VT_UNSIGNED
;
5832 gcase(sw
.p
, sw
.n
, &a
);
5835 gjmp_addr(sw
.def_sym
);
5836 dynarray_reset(&sw
.p
, &sw
.n
);
5841 if (tok
== TOK_CASE
) {
5842 struct case_t
*cr
= tcc_malloc(sizeof(struct case_t
));
5845 nocode_wanted
&= ~0x20000000;
5847 cr
->v1
= cr
->v2
= expr_const64();
5848 if (gnu_ext
&& tok
== TOK_DOTS
) {
5850 cr
->v2
= expr_const64();
5851 if (cr
->v2
< cr
->v1
)
5852 tcc_warning("empty case range");
5855 dynarray_add((void***) &cur_switch
->p
, &cur_switch
->n
, cr
);
5858 goto block_after_label
;
5860 if (tok
== TOK_DEFAULT
) {
5865 if (cur_switch
->def_sym
)
5866 tcc_error("too many 'default'");
5867 cur_switch
->def_sym
= ind
;
5869 goto block_after_label
;
5871 if (tok
== TOK_GOTO
) {
5873 if (tok
== '*' && gnu_ext
) {
5877 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
5880 } else if (tok
>= TOK_UIDENT
) {
5881 s
= label_find(tok
);
5882 /* put forward definition if needed */
5884 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
5886 if (s
->r
== LABEL_DECLARED
)
5887 s
->r
= LABEL_FORWARD
;
5889 vla_sp_restore_root();
5890 if (s
->r
& LABEL_FORWARD
)
5891 s
->jnext
= gjmp(s
->jnext
);
5893 gjmp_addr(s
->jnext
);
5896 expect("label identifier");
5899 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
5907 if (s
->r
== LABEL_DEFINED
)
5908 tcc_error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
5910 s
->r
= LABEL_DEFINED
;
5912 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
5916 /* we accept this, but it is a mistake */
5918 nocode_wanted
&= ~0x20000000;
5920 tcc_warning("deprecated use of label at end of compound statement");
5924 block(bsym
, csym
, is_expr
);
5927 /* expression case */
5942 #define EXPR_CONST 1
5945 static void parse_init_elem(int expr_type
)
5947 int saved_global_expr
;
5950 /* compound literals must be allocated globally in this case */
5951 saved_global_expr
= global_expr
;
5954 global_expr
= saved_global_expr
;
5955 /* NOTE: symbols are accepted */
5956 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
5957 tcc_error("initializer element is not constant");
5965 /* t is the array or struct type. c is the array or struct
5966 address. cur_field is the pointer to the current
5967 value, for arrays the 'c' member contains the current start
5968 index and the 'r' contains the end index (in case of range init).
5969 'size_only' is true if only size info is needed (only used
5971 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
5972 Sym
**cur_field
, int size_only
)
5975 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
5981 if (gnu_ext
&& (l
= is_label()) != 0)
5983 while (tok
== '[' || tok
== '.') {
5985 if (!(type
->t
& VT_ARRAY
))
5986 expect("array type");
5989 index
= expr_const();
5990 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
5991 tcc_error("invalid index");
5992 if (tok
== TOK_DOTS
&& gnu_ext
) {
5994 index_last
= expr_const();
5995 if (index_last
< 0 ||
5996 (s
->c
>= 0 && index_last
>= s
->c
) ||
5998 tcc_error("invalid index");
6004 (*cur_field
)->c
= index
;
6005 (*cur_field
)->r
= index_last
;
6007 type
= pointed_type(type
);
6008 elem_size
= type_size(type
, &align
);
6009 c
+= index
* elem_size
;
6010 /* NOTE: we only support ranges for last designator */
6011 nb_elems
= index_last
- index
+ 1;
6012 if (nb_elems
!= 1) {
6021 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
6022 expect("struct/union type");
6023 f
= find_field(type
, l
);
6028 /* XXX: fix this mess by using explicit storage field */
6030 type1
.t
|= (type
->t
& ~VT_TYPE
);
6044 if (type
->t
& VT_ARRAY
) {
6045 index
= (*cur_field
)->c
;
6046 if (type
->ref
->c
>= 0 && index
>= type
->ref
->c
)
6047 tcc_error("index too large");
6048 type
= pointed_type(type
);
6049 c
+= index
* type_size(type
, &align
);
6053 tcc_error("too many field init");
6054 /* XXX: fix this mess by using explicit storage field */
6056 type1
.t
|= (type
->t
& ~VT_TYPE
);
6061 decl_initializer(type
, sec
, c
, 0, size_only
);
6063 /* XXX: make it more general */
6064 if (!size_only
&& nb_elems
> 1) {
6065 unsigned long c_end
;
6070 vset(type
, VT_LOCAL
|VT_LVAL
, c
);
6071 for (i
= 1; i
< nb_elems
; i
++) {
6072 vset(type
, VT_LOCAL
|VT_LVAL
, c
+ elem_size
* i
);
6078 c_end
= c
+ nb_elems
* elem_size
;
6079 if (c_end
> sec
->data_allocated
)
6080 section_realloc(sec
, c_end
);
6081 src
= sec
->data
+ c
;
6083 for(i
= 1; i
< nb_elems
; i
++) {
6085 memcpy(dst
, src
, elem_size
);
6091 /* store a value or an expression directly in global data or in local array */
6092 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
)
6094 int bt
, bit_pos
, bit_size
;
6096 unsigned long long bit_mask
;
6100 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
6104 /* XXX: not portable */
6105 /* XXX: generate error if incorrect relocation */
6106 gen_assign_cast(&dtype
);
6107 bt
= type
->t
& VT_BTYPE
;
6108 size
= type_size(type
, &align
);
6109 if (c
+ size
> sec
->data_allocated
) {
6110 section_realloc(sec
, c
+ size
);
6112 ptr
= sec
->data
+ c
;
6113 /* XXX: make code faster ? */
6114 if (!(type
->t
& VT_BITFIELD
)) {
6116 bit_size
= PTR_SIZE
* 8;
6119 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
6120 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
6121 bit_mask
= (1LL << bit_size
) - 1;
6123 if ((vtop
->r
& (VT_SYM
|VT_CONST
)) == (VT_SYM
|VT_CONST
) &&
6124 vtop
->sym
->v
>= SYM_FIRST_ANOM
&&
6125 /* XXX This rejects compount literals like
6126 '(void *){ptr}'. The problem is that '&sym' is
6127 represented the same way, which would be ruled out
6128 by the SYM_FIRST_ANOM check above, but also '"string"'
6129 in 'char *p = "string"' is represented the same
6130 with the type being VT_PTR and the symbol being an
6131 anonymous one. That is, there's no difference in vtop
6132 between '(void *){x}' and '&(void *){x}'. Ignore
6133 pointer typed entities here. Hopefully no real code
6134 will every use compound literals with scalar type. */
6135 (vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
6136 /* These come from compound literals, memcpy stuff over. */
6140 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[vtop
->sym
->c
];
6141 ssec
= tcc_state
->sections
[esym
->st_shndx
];
6142 memmove (ptr
, ssec
->data
+ esym
->st_value
, size
);
6144 /* We need to copy over all memory contents, and that
6145 includes relocations. Use the fact that relocs are
6146 created it order, so look from the end of relocs
6147 until we hit one before the copied region. */
6148 int num_relocs
= ssec
->reloc
->data_offset
/ sizeof(*rel
);
6149 rel
= (ElfW_Rel
*)(ssec
->reloc
->data
+ ssec
->reloc
->data_offset
);
6150 while (num_relocs
--) {
6152 if (rel
->r_offset
>= esym
->st_value
+ size
)
6154 if (rel
->r_offset
< esym
->st_value
)
6156 /* Note: if the same fields are initialized multiple
6157 times (possible with designators) then we possibly
6158 add multiple relocations for the same offset here.
6159 That would lead to wrong code, the last reloc needs
6160 to win. We clean this up later after the whole
6161 initializer is parsed. */
6162 put_elf_reloca(symtab_section
, sec
,
6163 c
+ rel
->r_offset
- esym
->st_value
,
6164 ELFW(R_TYPE
)(rel
->r_info
),
6165 ELFW(R_SYM
)(rel
->r_info
),
6166 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
6175 if ((vtop
->r
& VT_SYM
) &&
6181 (bt
== VT_LLONG
&& bit_size
!= 64) ||
6185 (bt
== VT_INT
&& bit_size
!= 32)
6188 tcc_error("initializer element is not computable at load time");
6190 /* XXX: when cross-compiling we assume that each type has the
6191 same representation on host and target, which is likely to
6192 be wrong in the case of long double */
6194 vtop
->c
.i
= (vtop
->c
.i
!= 0);
6196 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
6199 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
6202 *(double *)ptr
= vtop
->c
.d
;
6205 if (sizeof(long double) == LDOUBLE_SIZE
)
6206 *(long double *)ptr
= vtop
->c
.ld
;
6207 else if (sizeof(double) == LDOUBLE_SIZE
)
6208 *(double *)ptr
= vtop
->c
.ld
;
6210 tcc_error("can't cross compile long double constants");
6214 *(long long *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
6221 addr_t val
= (vtop
->c
.i
& bit_mask
) << bit_pos
;
6222 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
6223 if (vtop
->r
& VT_SYM
)
6224 greloca(sec
, vtop
->sym
, c
, R_DATA_PTR
, val
);
6226 *(addr_t
*)ptr
|= val
;
6228 if (vtop
->r
& VT_SYM
)
6229 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
6230 *(addr_t
*)ptr
|= val
;
6236 int val
= (vtop
->c
.i
& bit_mask
) << bit_pos
;
6237 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
6238 if (vtop
->r
& VT_SYM
)
6239 greloca(sec
, vtop
->sym
, c
, R_DATA_PTR
, val
);
6243 if (vtop
->r
& VT_SYM
)
6244 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
6253 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
6260 /* put zeros for variable based init */
6261 static void init_putz(Section
*sec
, unsigned long c
, int size
)
6264 /* nothing to do because globals are already set to zero */
6266 vpush_global_sym(&func_old_type
, TOK_memset
);
6268 #ifdef TCC_TARGET_ARM
6279 /* 't' contains the type and storage info. 'c' is the offset of the
6280 object in section 'sec'. If 'sec' is NULL, it means stack based
6281 allocation. 'first' is true if array '{' must be read (multi
6282 dimension implicit array init handling). 'size_only' is true if
6283 size only evaluation is wanted (only for arrays). */
6284 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
6285 int first
, int size_only
)
6287 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, parlevel1
, i
;
6294 /* If we currently are at an '}' or ',' we have read an initializer
6295 element in one of our callers, and not yet consumed it. */
6296 have_elem
= tok
== '}' || tok
== ',';
6297 if (!have_elem
&& tok
!= '{' &&
6298 /* In case of strings we have special handling for arrays, so
6299 don't consume them as initializer value (which would commit them
6300 to some anonymous symbol). */
6301 tok
!= TOK_LSTR
&& tok
!= TOK_STR
&&
6303 parse_init_elem(!sec
? EXPR_ANY
: EXPR_CONST
);
6308 !(type
->t
& VT_ARRAY
) &&
6309 /* Use i_c_parameter_t, to strip toplevel qualifiers.
6310 The source type might have VT_CONSTANT set, which is
6311 of course assignable to non-const elements. */
6312 is_compatible_parameter_types(type
, &vtop
->type
)) {
6313 init_putv(type
, sec
, c
);
6314 } else if (type
->t
& VT_ARRAY
) {
6318 t1
= pointed_type(type
);
6319 size1
= type_size(t1
, &align1
);
6322 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
6325 tcc_error("character array initializer must be a literal,"
6326 " optionally enclosed in braces");
6331 /* only parse strings here if correct type (otherwise: handle
6332 them as ((w)char *) expressions */
6333 if ((tok
== TOK_LSTR
&&
6334 #ifdef TCC_TARGET_PE
6335 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
6337 (t1
->t
& VT_BTYPE
) == VT_INT
6339 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
6340 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
6343 /* compute maximum number of chars wanted */
6345 cstr_len
= tokc
.str
.size
;
6347 cstr_len
= tokc
.str
.size
/ sizeof(nwchar_t
);
6350 if (n
>= 0 && nb
> (n
- array_length
))
6351 nb
= n
- array_length
;
6354 tcc_warning("initializer-string for array is too long");
6355 /* in order to go faster for common case (char
6356 string in global variable, we handle it
6358 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
6359 memcpy(sec
->data
+ c
+ array_length
, tokc
.str
.data
, nb
);
6363 ch
= ((unsigned char *)tokc
.str
.data
)[i
];
6365 ch
= ((nwchar_t
*)tokc
.str
.data
)[i
];
6367 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
);
6374 /* only add trailing zero if enough storage (no
6375 warning in this case since it is standard) */
6376 if (n
< 0 || array_length
< n
) {
6379 init_putv(t1
, sec
, c
+ (array_length
* size1
));
6389 while (tok
!= '}' || have_elem
) {
6390 decl_designator(type
, sec
, c
, &f
, size_only
);
6393 /* must put zero in holes (note that doing it that way
6394 ensures that it even works with designators) */
6395 if (!size_only
&& array_length
< index
) {
6396 init_putz(sec
, c
+ array_length
* size1
,
6397 (index
- array_length
) * size1
);
6399 if (type
->t
& VT_ARRAY
) {
6400 index
= indexsym
.c
= ++indexsym
.r
;
6402 index
= index
+ type_size(&f
->type
, &align1
);
6403 if (s
->type
.t
== TOK_UNION
)
6408 if (index
> array_length
)
6409 array_length
= index
;
6411 if (type
->t
& VT_ARRAY
) {
6412 /* special test for multi dimensional arrays (may not
6413 be strictly correct if designators are used at the
6415 if (no_oblock
&& index
>= n
)
6418 if (no_oblock
&& f
== NULL
)
6426 /* put zeros at the end */
6427 if (!size_only
&& array_length
< n
) {
6428 init_putz(sec
, c
+ array_length
* size1
,
6429 (n
- array_length
) * size1
);
6433 /* patch type size if needed, which happens only for array types */
6435 s
->c
= array_length
;
6436 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
6439 if (first
|| tok
== '{') {
6448 } else if (tok
== '{') {
6450 decl_initializer(type
, sec
, c
, first
, size_only
);
6452 } else if (size_only
) {
6453 /* If we supported only ISO C we wouldn't have to accept calling
6454 this on anything than an array size_only==1 (and even then
6455 only on the outermost level, so no recursion would be needed),
6456 because initializing a flex array member isn't supported.
6457 But GNU C supports it, so we need to recurse even into
6458 subfields of structs and arrays when size_only is set. */
6459 /* just skip expression */
6460 parlevel
= parlevel1
= 0;
6461 while ((parlevel
> 0 || parlevel1
> 0 ||
6462 (tok
!= '}' && tok
!= ',')) && tok
!= -1) {
6465 else if (tok
== ')') {
6466 if (parlevel
== 0 && parlevel1
== 0)
6470 else if (tok
== '{')
6472 else if (tok
== '}') {
6473 if (parlevel
== 0 && parlevel1
== 0)
6481 /* This should happen only when we haven't parsed
6482 the init element above for fear of committing a
6483 string constant to memory too early. */
6484 if (tok
!= TOK_STR
&& tok
!= TOK_LSTR
)
6485 expect("string constant");
6486 parse_init_elem(!sec
? EXPR_ANY
: EXPR_CONST
);
6488 init_putv(type
, sec
, c
);
6492 /* parse an initializer for type 't' if 'has_init' is non zero, and
6493 allocate space in local or global data space ('r' is either
6494 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
6495 variable 'v' of scope 'scope' is declared before initializers
6496 are parsed. If 'v' is zero, then a reference to the new object
6497 is put in the value stack. If 'has_init' is 2, a special parsing
6498 is done to handle string constants. */
6499 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
6500 int has_init
, int v
, int scope
)
6502 int size
, align
, addr
, data_offset
;
6504 ParseState saved_parse_state
= {0};
6505 TokenString
*init_str
= NULL
;
6507 Sym
*flexible_array
;
6509 flexible_array
= NULL
;
6510 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
6511 Sym
*field
= type
->ref
->next
;
6514 field
= field
->next
;
6515 if (field
->type
.t
& VT_ARRAY
&& field
->type
.ref
->c
< 0)
6516 flexible_array
= field
;
6520 size
= type_size(type
, &align
);
6521 /* If unknown size, we must evaluate it before
6522 evaluating initializers because
6523 initializers can generate global data too
6524 (e.g. string pointers or ISOC99 compound
6525 literals). It also simplifies local
6526 initializers handling */
6527 if (size
< 0 || (flexible_array
&& has_init
)) {
6529 tcc_error("unknown type size");
6530 /* get all init string */
6531 init_str
= tok_str_alloc();
6532 if (has_init
== 2) {
6533 /* only get strings */
6534 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
6535 tok_str_add_tok(init_str
);
6540 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
6542 tcc_error("unexpected end of file in initializer");
6543 tok_str_add_tok(init_str
);
6546 else if (tok
== '}') {
6556 tok_str_add(init_str
, -1);
6557 tok_str_add(init_str
, 0);
6560 save_parse_state(&saved_parse_state
);
6562 begin_macro(init_str
, 1);
6564 decl_initializer(type
, NULL
, 0, 1, 1);
6565 /* prepare second initializer parsing */
6566 macro_ptr
= init_str
->str
;
6569 /* if still unknown size, error */
6570 size
= type_size(type
, &align
);
6572 tcc_error("unknown type size");
6574 /* If there's a flex member and it was used in the initializer
6576 if (flexible_array
&&
6577 flexible_array
->type
.ref
->c
> 0)
6578 size
+= flexible_array
->type
.ref
->c
6579 * pointed_size(&flexible_array
->type
);
6580 /* take into account specified alignment if bigger */
6581 if (ad
->a
.aligned
) {
6582 int speca
= 1 << (ad
->a
.aligned
- 1);
6585 } else if (ad
->a
.packed
) {
6588 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
6590 #ifdef CONFIG_TCC_BCHECK
6591 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
6595 loc
= (loc
- size
) & -align
;
6597 #ifdef CONFIG_TCC_BCHECK
6598 /* handles bounds */
6599 /* XXX: currently, since we do only one pass, we cannot track
6600 '&' operators, so we add only arrays */
6601 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
6603 /* add padding between regions */
6605 /* then add local bound info */
6606 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(addr_t
));
6607 bounds_ptr
[0] = addr
;
6608 bounds_ptr
[1] = size
;
6612 /* local variable */
6613 #ifdef CONFIG_TCC_ASM
6614 if (ad
->asm_label
) {
6615 int reg
= asm_parse_regvar(ad
->asm_label
);
6617 r
= (r
& ~VT_VALMASK
) | reg
;
6620 sym_push(v
, type
, r
, addr
);
6622 /* push local reference */
6623 vset(type
, r
, addr
);
6629 if (v
&& scope
== VT_CONST
) {
6630 /* see if the symbol was already defined */
6633 if (!is_compatible_types(&sym
->type
, type
))
6634 tcc_error("incompatible types for redefinition of '%s'",
6635 get_tok_str(v
, NULL
));
6636 if (sym
->type
.t
& VT_EXTERN
) {
6637 /* if the variable is extern, it was not allocated */
6638 sym
->type
.t
&= ~VT_EXTERN
;
6639 /* set array size if it was omitted in extern
6641 if ((sym
->type
.t
& VT_ARRAY
) &&
6642 sym
->type
.ref
->c
< 0 &&
6644 sym
->type
.ref
->c
= type
->ref
->c
;
6646 /* we accept several definitions of the same
6647 global variable. this is tricky, because we
6648 must play with the SHN_COMMON type of the symbol */
6649 /* XXX: should check if the variable was already
6650 initialized. It is incorrect to initialized it
6652 /* no init data, we won't add more to the symbol */
6659 /* allocate symbol in corresponding section */
6664 else if (tcc_state
->nocommon
)
6668 data_offset
= sec
->data_offset
;
6669 data_offset
= (data_offset
+ align
- 1) & -align
;
6671 /* very important to increment global pointer at this time
6672 because initializers themselves can create new initializers */
6673 data_offset
+= size
;
6674 #ifdef CONFIG_TCC_BCHECK
6675 /* add padding if bound check */
6676 if (tcc_state
->do_bounds_check
)
6679 sec
->data_offset
= data_offset
;
6680 /* allocate section space to put the data */
6681 if (sec
->sh_type
!= SHT_NOBITS
&&
6682 data_offset
> sec
->data_allocated
)
6683 section_realloc(sec
, data_offset
);
6684 /* align section if needed */
6685 if (align
> sec
->sh_addralign
)
6686 sec
->sh_addralign
= align
;
6688 addr
= 0; /* avoid warning */
6692 if (scope
!= VT_CONST
|| !sym
) {
6693 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
6694 sym
->asm_label
= ad
->asm_label
;
6696 /* update symbol definition */
6698 put_extern_sym(sym
, sec
, addr
, size
);
6701 /* put a common area */
6702 put_extern_sym(sym
, NULL
, align
, size
);
6703 /* XXX: find a nicer way */
6704 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
6705 esym
->st_shndx
= SHN_COMMON
;
6708 /* push global reference */
6709 sym
= get_sym_ref(type
, sec
, addr
, size
);
6710 vpushsym(type
, sym
);
6712 /* patch symbol weakness */
6713 if (type
->t
& VT_WEAK
)
6715 apply_visibility(sym
, type
);
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
;
6730 if (type
->t
& VT_VLA
) {
6733 /* save current stack pointer */
6734 if (vlas_in_scope
== 0) {
6735 if (vla_sp_root_loc
== -1)
6736 vla_sp_root_loc
= (loc
-= PTR_SIZE
);
6737 gen_vla_sp_save(vla_sp_root_loc
);
6740 vla_runtime_type_size(type
, &a
);
6741 gen_vla_alloc(type
, a
);
6742 gen_vla_sp_save(addr
);
6745 } else if (has_init
) {
6746 size_t oldreloc_offset
= 0;
6747 if (sec
&& sec
->reloc
)
6748 oldreloc_offset
= sec
->reloc
->data_offset
;
6749 decl_initializer(type
, sec
, addr
, 1, 0);
6750 if (sec
&& sec
->reloc
)
6751 squeeze_multi_relocs(sec
, oldreloc_offset
);
6752 /* patch flexible array member size back to -1, */
6753 /* for possible subsequent similar declarations */
6755 flexible_array
->type
.ref
->c
= -1;
6758 /* restore parse state if needed */
6761 restore_parse_state(&saved_parse_state
);
6765 static void put_func_debug(Sym
*sym
)
6770 /* XXX: we put here a dummy type */
6771 snprintf(buf
, sizeof(buf
), "%s:%c1",
6772 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
6773 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
6774 cur_text_section
, sym
->c
);
6775 /* //gr gdb wants a line at the function */
6776 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
6781 /* parse an old style function declaration list */
6782 /* XXX: check multiple parameter */
6783 static void func_decl_list(Sym
*func_sym
)
6790 /* parse each declaration */
6791 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
&&
6792 tok
!= TOK_ASM1
&& tok
!= TOK_ASM2
&& tok
!= TOK_ASM3
) {
6793 if (!parse_btype(&btype
, &ad
))
6794 expect("declaration list");
6795 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
6796 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
6798 /* we accept no variable after */
6802 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
6803 /* find parameter in function parameter list */
6806 if ((s
->v
& ~SYM_FIELD
) == v
)
6810 tcc_error("declaration for parameter '%s' but no such parameter",
6811 get_tok_str(v
, NULL
));
6813 /* check that no storage specifier except 'register' was given */
6814 if (type
.t
& VT_STORAGE
)
6815 tcc_error("storage class specified for '%s'", get_tok_str(v
, NULL
));
6816 convert_parameter_type(&type
);
6817 /* we can add the type (NOTE: it could be local to the function) */
6819 /* accept other parameters */
6830 /* parse a function defined by symbol 'sym' and generate its code in
6831 'cur_text_section' */
6832 static void gen_function(Sym
*sym
)
6835 ind
= cur_text_section
->data_offset
;
6836 /* NOTE: we patch the symbol size later */
6837 put_extern_sym(sym
, cur_text_section
, ind
, 0);
6838 funcname
= get_tok_str(sym
->v
, NULL
);
6840 /* Initialize VLA state */
6842 vla_sp_root_loc
= -1;
6843 /* put debug symbol */
6844 if (tcc_state
->do_debug
)
6845 put_func_debug(sym
);
6847 /* push a dummy symbol to enable local sym storage */
6848 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
6849 local_scope
= 1; /* for function parameters */
6850 gfunc_prolog(&sym
->type
);
6854 block(NULL
, NULL
, 0);
6858 cur_text_section
->data_offset
= ind
;
6859 label_pop(&global_label_stack
, NULL
);
6860 /* reset local stack */
6862 sym_pop(&local_stack
, NULL
, 0);
6863 /* end of function */
6864 /* patch symbol size */
6865 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
6867 /* patch symbol weakness (this definition overrules any prototype) */
6868 if (sym
->type
.t
& VT_WEAK
)
6870 apply_visibility(sym
, &sym
->type
);
6871 if (tcc_state
->do_debug
) {
6872 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
6874 /* It's better to crash than to generate wrong code */
6875 cur_text_section
= NULL
;
6876 funcname
= ""; /* for safety */
6877 func_vt
.t
= VT_VOID
; /* for safety */
6878 func_var
= 0; /* for safety */
6879 ind
= 0; /* for safety */
6884 static void gen_inline_functions(TCCState
*s
)
6887 int inline_generated
, i
, ln
;
6888 struct InlineFunc
*fn
;
6890 ln
= file
->line_num
;
6891 /* iterate while inline function are referenced */
6893 inline_generated
= 0;
6894 for (i
= 0; i
< s
->nb_inline_fns
; ++i
) {
6895 fn
= s
->inline_fns
[i
];
6897 if (sym
&& sym
->c
) {
6898 /* the function was used: generate its code and
6899 convert it to a normal function */
6902 pstrcpy(file
->filename
, sizeof file
->filename
, fn
->filename
);
6903 sym
->r
= VT_SYM
| VT_CONST
;
6904 sym
->type
.t
&= ~VT_INLINE
;
6906 begin_macro(fn
->func_str
, 1);
6908 cur_text_section
= text_section
;
6912 inline_generated
= 1;
6915 if (!inline_generated
)
6918 file
->line_num
= ln
;
6921 ST_FUNC
void free_inline_functions(TCCState
*s
)
6924 /* free tokens of unused inline functions */
6925 for (i
= 0; i
< s
->nb_inline_fns
; ++i
) {
6926 struct InlineFunc
*fn
= s
->inline_fns
[i
];
6928 tok_str_free(fn
->func_str
);
6930 dynarray_reset(&s
->inline_fns
, &s
->nb_inline_fns
);
6933 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
6934 static int decl0(int l
, int is_for_loop_init
)
6942 if (!parse_btype(&btype
, &ad
)) {
6943 if (is_for_loop_init
)
6945 /* skip redundant ';' */
6946 /* XXX: find more elegant solution */
6951 if (l
== VT_CONST
&&
6952 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
6953 /* global asm block */
6957 /* special test for old K&R protos without explicit int
6958 type. Only accepted when defining global data */
6959 if (l
== VT_LOCAL
|| tok
< TOK_UIDENT
)
6963 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
6964 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
6966 if ((btype
.t
& VT_BTYPE
) == VT_STRUCT
) {
6967 int v
= btype
.ref
->v
;
6968 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) >= SYM_FIRST_ANOM
)
6969 tcc_warning("unnamed struct/union that defines no instances");
6974 while (1) { /* iterate thru each declaration */
6976 /* If the base type itself was an array type of unspecified
6977 size (like in 'typedef int arr[]; arr x = {1};') then
6978 we will overwrite the unknown size by the real one for
6979 this decl. We need to unshare the ref symbol holding
6981 if ((type
.t
& VT_ARRAY
) && type
.ref
->c
< 0) {
6982 type
.ref
= sym_push(SYM_FIELD
, &type
.ref
->type
, 0, type
.ref
->c
);
6984 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
6988 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
6989 printf("type = '%s'\n", buf
);
6992 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
6993 if ((type
.t
& VT_STATIC
) && (l
== VT_LOCAL
)) {
6994 tcc_error("function without file scope cannot be static");
6996 /* if old style function prototype, we accept a
6999 if (sym
->c
== FUNC_OLD
)
7000 func_decl_list(sym
);
7003 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
7004 ad
.asm_label
= asm_label_instr();
7005 /* parse one last attribute list, after asm label */
7006 parse_attribute(&ad
);
7013 #ifdef TCC_TARGET_PE
7014 if (ad
.a
.func_import
)
7015 type
.t
|= VT_IMPORT
;
7016 if (ad
.a
.func_export
)
7017 type
.t
|= VT_EXPORT
;
7019 type
.t
|= ad
.a
.visibility
<< VT_VIS_SHIFT
;
7023 tcc_error("cannot use local functions");
7024 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
7025 expect("function definition");
7027 /* reject abstract declarators in function definition */
7029 while ((sym
= sym
->next
) != NULL
)
7030 if (!(sym
->v
& ~SYM_FIELD
))
7031 expect("identifier");
7033 /* XXX: cannot do better now: convert extern line to static inline */
7034 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
7035 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
7040 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
7043 ref
= sym
->type
.ref
;
7044 if (0 == ref
->a
.func_proto
)
7045 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
7047 /* use func_call from prototype if not defined */
7048 if (ref
->a
.func_call
!= FUNC_CDECL
7049 && type
.ref
->a
.func_call
== FUNC_CDECL
)
7050 type
.ref
->a
.func_call
= ref
->a
.func_call
;
7052 /* use export from prototype */
7053 if (ref
->a
.func_export
)
7054 type
.ref
->a
.func_export
= 1;
7056 /* use static from prototype */
7057 if (sym
->type
.t
& VT_STATIC
)
7058 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
7060 /* If the definition has no visibility use the
7061 one from prototype. */
7062 if (! (type
.t
& VT_VIS_MASK
))
7063 type
.t
|= sym
->type
.t
& VT_VIS_MASK
;
7065 if (!is_compatible_types(&sym
->type
, &type
)) {
7067 tcc_error("incompatible types for redefinition of '%s'",
7068 get_tok_str(v
, NULL
));
7070 type
.ref
->a
.func_proto
= 0;
7071 /* if symbol is already defined, then put complete type */
7074 /* put function symbol */
7075 sym
= global_identifier_push(v
, type
.t
, 0);
7076 sym
->type
.ref
= type
.ref
;
7079 /* static inline functions are just recorded as a kind
7080 of macro. Their code will be emitted at the end of
7081 the compilation unit only if they are used */
7082 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
7083 (VT_INLINE
| VT_STATIC
)) {
7085 struct InlineFunc
*fn
;
7086 const char *filename
;
7088 filename
= file
? file
->filename
: "";
7089 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
7090 strcpy(fn
->filename
, filename
);
7092 fn
->func_str
= tok_str_alloc();
7098 tcc_error("unexpected end of file");
7099 tok_str_add_tok(fn
->func_str
);
7104 } else if (t
== '}') {
7106 if (block_level
== 0)
7110 tok_str_add(fn
->func_str
, -1);
7111 tok_str_add(fn
->func_str
, 0);
7112 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
7115 /* compute text section */
7116 cur_text_section
= ad
.section
;
7117 if (!cur_text_section
)
7118 cur_text_section
= text_section
;
7119 sym
->r
= VT_SYM
| VT_CONST
;
7124 if (btype
.t
& VT_TYPEDEF
) {
7125 /* save typedefed type */
7126 /* XXX: test storage specifiers ? */
7128 if (sym
&& sym
->scope
== local_scope
) {
7129 if (!is_compatible_types(&sym
->type
, &type
)
7130 || !(sym
->type
.t
& VT_TYPEDEF
))
7131 tcc_error("incompatible redefinition of '%s'",
7132 get_tok_str(v
, NULL
));
7135 sym
= sym_push(v
, &type
, 0, 0);
7138 sym
->type
.t
|= VT_TYPEDEF
;
7141 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
7142 /* external function definition */
7143 /* specific case for func_call attribute */
7144 ad
.a
.func_proto
= 1;
7146 } else if (!(type
.t
& VT_ARRAY
)) {
7147 /* not lvalue if array */
7148 r
|= lvalue_type(type
.t
);
7150 has_init
= (tok
== '=');
7151 if (has_init
&& (type
.t
& VT_VLA
))
7152 tcc_error("variable length array cannot be initialized");
7153 if ((btype
.t
& VT_EXTERN
) || ((type
.t
& VT_BTYPE
) == VT_FUNC
) ||
7154 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
7155 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
7156 /* external variable or function */
7157 /* NOTE: as GCC, uninitialized global static
7158 arrays of null size are considered as
7160 sym
= external_sym(v
, &type
, r
);
7161 sym
->asm_label
= ad
.asm_label
;
7163 if (ad
.alias_target
) {
7168 alias_target
= sym_find(ad
.alias_target
);
7169 if (!alias_target
|| !alias_target
->c
)
7170 tcc_error("unsupported forward __alias__ attribute");
7171 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[alias_target
->c
];
7172 tsec
.sh_num
= esym
->st_shndx
;
7173 put_extern_sym2(sym
, &tsec
, esym
->st_value
, esym
->st_size
, 0);
7176 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
7177 if (type
.t
& VT_STATIC
)
7183 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, l
);
7187 if (is_for_loop_init
)
7200 ST_FUNC
void decl(int l
)
7205 /* ------------------------------------------------------------------------- */