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 static void vsetc(CType
*type
, int r
, CValue
*vc
)
566 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
567 tcc_error("memory full (vstack)");
568 /* cannot let cpu flags if other instruction are generated. Also
569 avoid leaving VT_JMP anywhere except on the top of the stack
570 because it would complicate the code generator.
572 Don't do this when nocode_wanted. vtop might come from
573 !nocode_wanted regions (see 88_codeopt.c) and transforming
574 it to a register without actually generating code is wrong
575 as their value might still be used for real. All values
576 we push under nocode_wanted will eventually be popped
577 again, so that the VT_CMP/VT_JMP value will be in vtop
578 when code is unsuppressed again.
580 Same logic below in vswap(); */
581 if (vtop
>= vstack
&& !nocode_wanted
) {
582 v
= vtop
->r
& VT_VALMASK
;
583 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
595 ST_FUNC
void vswap(void)
598 /* cannot vswap cpu flags. See comment at vsetc() above */
599 if (vtop
>= vstack
&& !nocode_wanted
) {
600 int v
= vtop
->r
& VT_VALMASK
;
601 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
609 /* pop stack value */
610 ST_FUNC
void vpop(void)
613 v
= vtop
->r
& VT_VALMASK
;
614 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
615 /* for x86, we need to pop the FP stack */
617 o(0xd8dd); /* fstp %st(0) */
620 if (v
== VT_JMP
|| v
== VT_JMPI
) {
621 /* need to put correct jump if && or || without test */
627 /* push constant of type "type" with useless value */
628 ST_FUNC
void vpush(CType
*type
)
631 vsetc(type
, VT_CONST
, &cval
);
634 /* push integer constant */
635 ST_FUNC
void vpushi(int v
)
639 vsetc(&int_type
, VT_CONST
, &cval
);
642 /* push a pointer sized constant */
643 static void vpushs(addr_t v
)
647 vsetc(&size_type
, VT_CONST
, &cval
);
650 /* push arbitrary 64bit constant */
651 ST_FUNC
void vpush64(int ty
, unsigned long long v
)
658 vsetc(&ctype
, VT_CONST
, &cval
);
661 /* push long long constant */
662 static inline void vpushll(long long v
)
664 vpush64(VT_LLONG
, v
);
667 ST_FUNC
void vset(CType
*type
, int r
, long v
)
672 vsetc(type
, r
, &cval
);
675 static void vseti(int r
, int v
)
683 ST_FUNC
void vpushv(SValue
*v
)
685 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
686 tcc_error("memory full (vstack)");
691 static void vdup(void)
696 /* rotate n first stack elements to the bottom
697 I1 ... In -> I2 ... In I1 [top is right]
699 ST_FUNC
void vrotb(int n
)
710 /* rotate the n elements before entry e towards the top
711 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
713 ST_FUNC
void vrote(SValue
*e
, int n
)
719 for(i
= 0;i
< n
- 1; i
++)
724 /* rotate n first stack elements to the top
725 I1 ... In -> In I1 ... I(n-1) [top is right]
727 ST_FUNC
void vrott(int n
)
732 /* push a symbol value of TYPE */
733 static inline void vpushsym(CType
*type
, Sym
*sym
)
737 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
741 /* Return a static symbol pointing to a section */
742 ST_FUNC Sym
*get_sym_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
748 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
749 sym
->type
.ref
= type
->ref
;
750 sym
->r
= VT_CONST
| VT_SYM
;
751 put_extern_sym(sym
, sec
, offset
, size
);
755 /* push a reference to a section offset by adding a dummy symbol */
756 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
758 vpushsym(type
, get_sym_ref(type
, sec
, offset
, size
));
761 /* define a new external reference to a symbol 'v' of type 'u' */
762 ST_FUNC Sym
*external_global_sym(int v
, CType
*type
, int r
)
768 /* push forward reference */
769 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
770 s
->type
.ref
= type
->ref
;
771 s
->r
= r
| VT_CONST
| VT_SYM
;
776 /* define a new external reference to a symbol 'v' */
777 static Sym
*external_sym(int v
, CType
*type
, int r
)
783 /* push forward reference */
784 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
785 s
->type
.t
|= VT_EXTERN
;
786 } else if (s
->type
.ref
== func_old_type
.ref
) {
787 s
->type
.ref
= type
->ref
;
788 s
->r
= r
| VT_CONST
| VT_SYM
;
789 s
->type
.t
|= VT_EXTERN
;
790 } else if (!is_compatible_types(&s
->type
, type
)) {
791 tcc_error("incompatible types for redefinition of '%s'",
792 get_tok_str(v
, NULL
));
794 /* Merge some storage attributes. */
795 if (type
->t
& VT_WEAK
)
798 if (type
->t
& VT_VIS_MASK
)
799 apply_visibility(s
, type
);
804 /* push a reference to global symbol v */
805 ST_FUNC
void vpush_global_sym(CType
*type
, int v
)
807 vpushsym(type
, external_global_sym(v
, type
, 0));
810 /* save registers up to (vtop - n) stack entry */
811 ST_FUNC
void save_regs(int n
)
814 for(p
= vstack
, p1
= vtop
- n
; p
<= p1
; p
++)
818 /* save r to the memory stack, and mark it as being free */
819 ST_FUNC
void save_reg(int r
)
821 save_reg_upstack(r
, 0);
824 /* save r to the memory stack, and mark it as being free,
825 if seen up to (vtop - n) stack entry */
826 ST_FUNC
void save_reg_upstack(int r
, int n
)
828 int l
, saved
, size
, align
;
832 if ((r
&= VT_VALMASK
) >= VT_CONST
)
837 /* modify all stack values */
840 for(p
= vstack
, p1
= vtop
- n
; p
<= p1
; p
++) {
841 if ((p
->r
& VT_VALMASK
) == r
||
842 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
843 /* must save value on stack if not already done */
845 /* NOTE: must reload 'r' because r might be equal to r2 */
846 r
= p
->r
& VT_VALMASK
;
847 /* store register in the stack */
849 if ((p
->r
& VT_LVAL
) ||
850 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
851 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
852 type
= &char_pointer_type
;
856 if ((type
->t
& VT_BTYPE
) == VT_FLOAT
) {
857 /* cast to DOUBLE to avoid precision loss */
858 type
->t
= (type
->t
& ~VT_BTYPE
) | VT_DOUBLE
;
860 size
= type_size(type
, &align
);
861 loc
= (loc
- size
) & -align
;
863 sv
.r
= VT_LOCAL
| VT_LVAL
;
866 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
867 /* x86 specific: need to pop fp register ST0 if saved */
869 o(0xd8dd); /* fstp %st(0) */
872 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
873 /* special long long case */
874 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
882 /* mark that stack entry as being saved on the stack */
883 if (p
->r
& VT_LVAL
) {
884 /* also clear the bounded flag because the
885 relocation address of the function was stored in
887 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
889 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
897 #ifdef TCC_TARGET_ARM
898 /* find a register of class 'rc2' with at most one reference on stack.
899 * If none, call get_reg(rc) */
900 ST_FUNC
int get_reg_ex(int rc
, int rc2
)
905 for(r
=0;r
<NB_REGS
;r
++) {
906 if (reg_classes
[r
] & rc2
) {
909 for(p
= vstack
; p
<= vtop
; p
++) {
910 if ((p
->r
& VT_VALMASK
) == r
||
911 (p
->r2
& VT_VALMASK
) == r
)
922 /* find a free register of class 'rc'. If none, save one register */
923 ST_FUNC
int get_reg(int rc
)
928 /* find a free register */
929 for(r
=0;r
<NB_REGS
;r
++) {
930 if (reg_classes
[r
] & rc
) {
933 for(p
=vstack
;p
<=vtop
;p
++) {
934 if ((p
->r
& VT_VALMASK
) == r
||
935 (p
->r2
& VT_VALMASK
) == r
)
943 /* no register left : free the first one on the stack (VERY
944 IMPORTANT to start from the bottom to ensure that we don't
945 spill registers used in gen_opi()) */
946 for(p
=vstack
;p
<=vtop
;p
++) {
947 /* look at second register (if long long) */
948 r
= p
->r2
& VT_VALMASK
;
949 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
951 r
= p
->r
& VT_VALMASK
;
952 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
958 /* Should never comes here */
962 /* move register 's' (of type 't') to 'r', and flush previous value of r to memory
964 static void move_reg(int r
, int s
, int t
)
978 /* get address of vtop (vtop MUST BE an lvalue) */
979 ST_FUNC
void gaddrof(void)
981 if (vtop
->r
& VT_REF
)
984 /* tricky: if saved lvalue, then we can go back to lvalue */
985 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
986 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
991 #ifdef CONFIG_TCC_BCHECK
992 /* generate lvalue bound code */
993 static void gbound(void)
998 vtop
->r
&= ~VT_MUSTBOUND
;
999 /* if lvalue, then use checking code before dereferencing */
1000 if (vtop
->r
& VT_LVAL
) {
1001 /* if not VT_BOUNDED value, then make one */
1002 if (!(vtop
->r
& VT_BOUNDED
)) {
1003 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
1004 /* must save type because we must set it to int to get pointer */
1006 vtop
->type
.t
= VT_PTR
;
1009 gen_bounded_ptr_add();
1010 vtop
->r
|= lval_type
;
1013 /* then check for dereferencing */
1014 gen_bounded_ptr_deref();
1019 /* store vtop a register belonging to class 'rc'. lvalues are
1020 converted to values. Cannot be used if cannot be converted to
1021 register value (such as structures). */
1022 ST_FUNC
int gv(int rc
)
1024 int r
, bit_pos
, bit_size
, size
, align
, i
;
1027 /* NOTE: get_reg can modify vstack[] */
1028 if (vtop
->type
.t
& VT_BITFIELD
) {
1031 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
1032 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
1033 /* remove bit field info to avoid loops */
1034 vtop
->type
.t
&= ~VT_BITFIELD
& ((1 << VT_STRUCT_SHIFT
) - 1);
1035 /* cast to int to propagate signedness in following ops */
1036 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
1041 if((vtop
->type
.t
& VT_UNSIGNED
) ||
1042 (vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
1043 type
.t
|= VT_UNSIGNED
;
1045 /* generate shifts */
1046 vpushi(bits
- (bit_pos
+ bit_size
));
1048 vpushi(bits
- bit_size
);
1049 /* NOTE: transformed to SHR if unsigned */
1053 if (is_float(vtop
->type
.t
) &&
1054 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
1057 unsigned long offset
;
1058 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
1062 /* XXX: unify with initializers handling ? */
1063 /* CPUs usually cannot use float constants, so we store them
1064 generically in data segment */
1065 size
= type_size(&vtop
->type
, &align
);
1066 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
1067 data_section
->data_offset
= offset
;
1068 /* XXX: not portable yet */
1069 #if defined(__i386__) || defined(__x86_64__)
1070 /* Zero pad x87 tenbyte long doubles */
1071 if (size
== LDOUBLE_SIZE
) {
1072 vtop
->c
.tab
[2] &= 0xffff;
1073 #if LDOUBLE_SIZE == 16
1078 ptr
= section_ptr_add(data_section
, size
);
1080 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
1084 ptr
[i
] = vtop
->c
.tab
[size
-1-i
];
1088 ptr
[i
] = vtop
->c
.tab
[i
];
1089 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
1090 vtop
->r
|= VT_LVAL
| VT_SYM
;
1094 #ifdef CONFIG_TCC_BCHECK
1095 if (vtop
->r
& VT_MUSTBOUND
)
1099 r
= vtop
->r
& VT_VALMASK
;
1100 rc2
= (rc
& RC_FLOAT
) ? RC_FLOAT
: RC_INT
;
1101 #ifndef TCC_TARGET_ARM64
1104 #ifdef TCC_TARGET_X86_64
1105 else if (rc
== RC_FRET
)
1110 /* need to reload if:
1112 - lvalue (need to dereference pointer)
1113 - already a register, but not in the right class */
1115 || (vtop
->r
& VT_LVAL
)
1116 || !(reg_classes
[r
] & rc
)
1117 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1118 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
1119 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
&& !(reg_classes
[vtop
->r2
] & rc2
))
1121 || ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
1126 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1127 if (((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
)) {
1128 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
1130 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
1131 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
1132 unsigned long long ll
;
1134 int r2
, original_type
;
1135 original_type
= vtop
->type
.t
;
1136 /* two register type load : expand to two words
1138 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
1139 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
1142 vtop
->c
.i
= ll
; /* first word */
1144 vtop
->r
= r
; /* save register value */
1145 vpushi(ll
>> 32); /* second word */
1148 if (vtop
->r
& VT_LVAL
) {
1149 /* We do not want to modifier the long long
1150 pointer here, so the safest (and less
1151 efficient) is to save all the other registers
1152 in the stack. XXX: totally inefficient. */
1156 /* lvalue_save: save only if used further down the stack */
1157 save_reg_upstack(vtop
->r
, 1);
1159 /* load from memory */
1160 vtop
->type
.t
= load_type
;
1163 vtop
[-1].r
= r
; /* save register value */
1164 /* increment pointer to get second word */
1165 vtop
->type
.t
= addr_type
;
1170 vtop
->type
.t
= load_type
;
1172 /* move registers */
1175 vtop
[-1].r
= r
; /* save register value */
1176 vtop
->r
= vtop
[-1].r2
;
1178 /* Allocate second register. Here we rely on the fact that
1179 get_reg() tries first to free r2 of an SValue. */
1183 /* write second register */
1185 vtop
->type
.t
= original_type
;
1186 } else if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
1188 /* lvalue of scalar type : need to use lvalue type
1189 because of possible cast */
1192 /* compute memory access type */
1193 if (vtop
->r
& VT_REF
)
1194 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1199 else if (vtop
->r
& VT_LVAL_BYTE
)
1201 else if (vtop
->r
& VT_LVAL_SHORT
)
1203 if (vtop
->r
& VT_LVAL_UNSIGNED
)
1207 /* restore wanted type */
1210 /* one register type load */
1215 #ifdef TCC_TARGET_C67
1216 /* uses register pairs for doubles */
1217 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
1224 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
1225 ST_FUNC
void gv2(int rc1
, int rc2
)
1229 /* generate more generic register first. But VT_JMP or VT_CMP
1230 values must be generated first in all cases to avoid possible
1232 v
= vtop
[0].r
& VT_VALMASK
;
1233 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
1238 /* test if reload is needed for first register */
1239 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
1249 /* test if reload is needed for first register */
1250 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
1256 #ifndef TCC_TARGET_ARM64
1257 /* wrapper around RC_FRET to return a register by type */
1258 static int rc_fret(int t
)
1260 #ifdef TCC_TARGET_X86_64
1261 if (t
== VT_LDOUBLE
) {
1269 /* wrapper around REG_FRET to return a register by type */
1270 static int reg_fret(int t
)
1272 #ifdef TCC_TARGET_X86_64
1273 if (t
== VT_LDOUBLE
) {
1280 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
1281 /* expand 64bit on stack in two ints */
1282 static void lexpand(void)
1285 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
1286 v
= vtop
->r
& (VT_VALMASK
| VT_LVAL
);
1287 if (v
== VT_CONST
) {
1290 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
1296 vtop
[0].r
= vtop
[-1].r2
;
1297 vtop
[0].r2
= vtop
[-1].r2
= VT_CONST
;
1299 vtop
[0].type
.t
= vtop
[-1].type
.t
= VT_INT
| u
;
1303 #ifdef TCC_TARGET_ARM
1304 /* expand long long on stack */
1305 ST_FUNC
void lexpand_nr(void)
1309 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
1311 vtop
->r2
= VT_CONST
;
1312 vtop
->type
.t
= VT_INT
| u
;
1313 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
1314 if (v
== VT_CONST
) {
1315 vtop
[-1].c
.i
= vtop
->c
.i
;
1316 vtop
->c
.i
= vtop
->c
.i
>> 32;
1318 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
1320 vtop
->r
= vtop
[-1].r
;
1321 } else if (v
> VT_CONST
) {
1325 vtop
->r
= vtop
[-1].r2
;
1326 vtop
[-1].r2
= VT_CONST
;
1327 vtop
[-1].type
.t
= VT_INT
| u
;
1331 #if !defined(TCC_TARGET_X86_64) && !defined(TCC_TARGET_ARM64)
1332 /* build a long long from two ints */
1333 static void lbuild(int t
)
1335 gv2(RC_INT
, RC_INT
);
1336 vtop
[-1].r2
= vtop
[0].r
;
1337 vtop
[-1].type
.t
= t
;
1342 /* convert stack entry to register and duplicate its value in another
1344 static void gv_dup(void)
1350 #if !defined(TCC_TARGET_X86_64) && !defined(TCC_TARGET_ARM64)
1351 if ((t
& VT_BTYPE
) == VT_LLONG
) {
1358 /* stack: H L L1 H1 */
1368 /* duplicate value */
1373 #ifdef TCC_TARGET_X86_64
1374 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1384 load(r1
, &sv
); /* move r to r1 */
1386 /* duplicates value */
1392 /* Generate value test
1394 * Generate a test for any value (jump, comparison and integers) */
1395 ST_FUNC
int gvtst(int inv
, int t
)
1397 int v
= vtop
->r
& VT_VALMASK
;
1398 if (v
!= VT_CMP
&& v
!= VT_JMP
&& v
!= VT_JMPI
) {
1402 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1403 /* constant jmp optimization */
1404 if ((vtop
->c
.i
!= 0) != inv
)
1409 return gtst(inv
, t
);
1412 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
1413 /* generate CPU independent (unsigned) long long operations */
1414 static void gen_opl(int op
)
1416 int t
, a
, b
, op1
, c
, i
;
1418 unsigned short reg_iret
= REG_IRET
;
1419 unsigned short reg_lret
= REG_LRET
;
1425 func
= TOK___divdi3
;
1428 func
= TOK___udivdi3
;
1431 func
= TOK___moddi3
;
1434 func
= TOK___umoddi3
;
1441 /* call generic long long function */
1442 vpush_global_sym(&func_old_type
, func
);
1447 vtop
->r2
= reg_lret
;
1455 //pv("gen_opl A",0,2);
1461 /* stack: L1 H1 L2 H2 */
1466 vtop
[-2] = vtop
[-3];
1469 /* stack: H1 H2 L1 L2 */
1470 //pv("gen_opl B",0,4);
1476 /* stack: H1 H2 L1 L2 ML MH */
1479 /* stack: ML MH H1 H2 L1 L2 */
1483 /* stack: ML MH H1 L2 H2 L1 */
1488 /* stack: ML MH M1 M2 */
1491 } else if (op
== '+' || op
== '-') {
1492 /* XXX: add non carry method too (for MIPS or alpha) */
1498 /* stack: H1 H2 (L1 op L2) */
1501 gen_op(op1
+ 1); /* TOK_xxxC2 */
1504 /* stack: H1 H2 (L1 op L2) */
1507 /* stack: (L1 op L2) H1 H2 */
1509 /* stack: (L1 op L2) (H1 op H2) */
1517 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1518 t
= vtop
[-1].type
.t
;
1522 /* stack: L H shift */
1524 /* constant: simpler */
1525 /* NOTE: all comments are for SHL. the other cases are
1526 done by swaping words */
1537 if (op
!= TOK_SAR
) {
1570 /* XXX: should provide a faster fallback on x86 ? */
1573 func
= TOK___ashrdi3
;
1576 func
= TOK___lshrdi3
;
1579 func
= TOK___ashldi3
;
1585 /* compare operations */
1591 /* stack: L1 H1 L2 H2 */
1593 vtop
[-1] = vtop
[-2];
1595 /* stack: L1 L2 H1 H2 */
1598 /* when values are equal, we need to compare low words. since
1599 the jump is inverted, we invert the test too. */
1602 else if (op1
== TOK_GT
)
1604 else if (op1
== TOK_ULT
)
1606 else if (op1
== TOK_UGT
)
1611 if (op1
!= TOK_NE
) {
1615 /* generate non equal test */
1616 /* XXX: NOT PORTABLE yet */
1617 if (op1
== TOK_NE
) {
1620 #if defined(TCC_TARGET_I386)
1621 b
= gjmp2(0x850f, 0);
1622 #elif defined(TCC_TARGET_ARM)
1624 o(0x1A000000 | encbranch(ind
, 0, 1));
1626 tcc_error("not implemented");
1630 /* compare low. Always unsigned */
1634 else if (op1
== TOK_LE
)
1636 else if (op1
== TOK_GT
)
1638 else if (op1
== TOK_GE
)
1649 static uint64_t gen_opic_sdiv(uint64_t a
, uint64_t b
)
1651 uint64_t x
= (a
>> 63 ? -a
: a
) / (b
>> 63 ? -b
: b
);
1652 return (a
^ b
) >> 63 ? -x
: x
;
1655 static int gen_opic_lt(uint64_t a
, uint64_t b
)
1657 return (a
^ (uint64_t)1 << 63) < (b
^ (uint64_t)1 << 63);
1660 /* handle integer constant optimizations and various machine
1662 static void gen_opic(int op
)
1664 SValue
*v1
= vtop
- 1;
1666 int t1
= v1
->type
.t
& VT_BTYPE
;
1667 int t2
= v2
->type
.t
& VT_BTYPE
;
1668 int c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1669 int c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1670 uint64_t l1
= c1
? v1
->c
.i
: 0;
1671 uint64_t l2
= c2
? v2
->c
.i
: 0;
1672 int shm
= (t1
== VT_LLONG
) ? 63 : 31;
1674 if (t1
!= VT_LLONG
&& (PTR_SIZE
!= 8 || t1
!= VT_PTR
))
1675 l1
= ((uint32_t)l1
|
1676 (v1
->type
.t
& VT_UNSIGNED
? 0 : -(l1
& 0x80000000)));
1677 if (t2
!= VT_LLONG
&& (PTR_SIZE
!= 8 || t2
!= VT_PTR
))
1678 l2
= ((uint32_t)l2
|
1679 (v2
->type
.t
& VT_UNSIGNED
? 0 : -(l2
& 0x80000000)));
1683 case '+': l1
+= l2
; break;
1684 case '-': l1
-= l2
; break;
1685 case '&': l1
&= l2
; break;
1686 case '^': l1
^= l2
; break;
1687 case '|': l1
|= l2
; break;
1688 case '*': l1
*= l2
; break;
1695 /* if division by zero, generate explicit division */
1698 tcc_error("division by zero in constant");
1702 default: l1
= gen_opic_sdiv(l1
, l2
); break;
1703 case '%': l1
= l1
- l2
* gen_opic_sdiv(l1
, l2
); break;
1704 case TOK_UDIV
: l1
= l1
/ l2
; break;
1705 case TOK_UMOD
: l1
= l1
% l2
; break;
1708 case TOK_SHL
: l1
<<= (l2
& shm
); break;
1709 case TOK_SHR
: l1
>>= (l2
& shm
); break;
1711 l1
= (l1
>> 63) ? ~(~l1
>> (l2
& shm
)) : l1
>> (l2
& shm
);
1714 case TOK_ULT
: l1
= l1
< l2
; break;
1715 case TOK_UGE
: l1
= l1
>= l2
; break;
1716 case TOK_EQ
: l1
= l1
== l2
; break;
1717 case TOK_NE
: l1
= l1
!= l2
; break;
1718 case TOK_ULE
: l1
= l1
<= l2
; break;
1719 case TOK_UGT
: l1
= l1
> l2
; break;
1720 case TOK_LT
: l1
= gen_opic_lt(l1
, l2
); break;
1721 case TOK_GE
: l1
= !gen_opic_lt(l1
, l2
); break;
1722 case TOK_LE
: l1
= !gen_opic_lt(l2
, l1
); break;
1723 case TOK_GT
: l1
= gen_opic_lt(l2
, l1
); break;
1725 case TOK_LAND
: l1
= l1
&& l2
; break;
1726 case TOK_LOR
: l1
= l1
|| l2
; break;
1730 if (t1
!= VT_LLONG
&& (PTR_SIZE
!= 8 || t1
!= VT_PTR
))
1731 l1
= ((uint32_t)l1
|
1732 (v1
->type
.t
& VT_UNSIGNED
? 0 : -(l1
& 0x80000000)));
1736 /* if commutative ops, put c2 as constant */
1737 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1738 op
== '|' || op
== '*')) {
1740 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1741 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1743 if (!const_wanted
&&
1745 (op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
)) ||
1746 (l1
== -1 && op
== TOK_SAR
))) {
1747 /* treat (0 << x), (0 >> x) and (-1 >> x) as constant */
1749 } else if (!const_wanted
&&
1750 c2
&& ((l2
== 0 && (op
== '&' || op
== '*')) ||
1751 (l2
== -1 && op
== '|') ||
1752 (l2
== 0xffffffff && t2
!= VT_LLONG
&& op
== '|') ||
1753 (l2
== 1 && (op
== '%' || op
== TOK_UMOD
)))) {
1754 /* treat (x & 0), (x * 0), (x | -1) and (x % 1) as constant */
1759 } else if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1762 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1763 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1767 /* filter out NOP operations like x*1, x-0, x&-1... */
1769 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1770 /* try to use shifts instead of muls or divs */
1771 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1780 else if (op
== TOK_PDIV
)
1786 } else if (c2
&& (op
== '+' || op
== '-') &&
1787 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
1788 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1789 /* symbol + constant case */
1793 /* The backends can't always deal with addends to symbols
1794 larger than +-1<<31. Don't construct such. */
1801 /* call low level op generator */
1802 if (t1
== VT_LLONG
|| t2
== VT_LLONG
||
1803 (PTR_SIZE
== 8 && (t1
== VT_PTR
|| t2
== VT_PTR
)))
1811 /* generate a floating point operation with constant propagation */
1812 static void gen_opif(int op
)
1820 /* currently, we cannot do computations with forward symbols */
1821 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1822 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1824 if (v1
->type
.t
== VT_FLOAT
) {
1827 } else if (v1
->type
.t
== VT_DOUBLE
) {
1835 /* NOTE: we only do constant propagation if finite number (not
1836 NaN or infinity) (ANSI spec) */
1837 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1841 case '+': f1
+= f2
; break;
1842 case '-': f1
-= f2
; break;
1843 case '*': f1
*= f2
; break;
1847 tcc_error("division by zero in constant");
1852 /* XXX: also handles tests ? */
1856 /* XXX: overflow test ? */
1857 if (v1
->type
.t
== VT_FLOAT
) {
1859 } else if (v1
->type
.t
== VT_DOUBLE
) {
1871 static int pointed_size(CType
*type
)
1874 return type_size(pointed_type(type
), &align
);
1877 static void vla_runtime_pointed_size(CType
*type
)
1880 vla_runtime_type_size(pointed_type(type
), &align
);
1883 static inline int is_null_pointer(SValue
*p
)
1885 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1887 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& (uint32_t)p
->c
.i
== 0) ||
1888 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.i
== 0) ||
1889 ((p
->type
.t
& VT_BTYPE
) == VT_PTR
&&
1890 (PTR_SIZE
== 4 ? (uint32_t)p
->c
.i
== 0 : p
->c
.i
== 0));
1893 static inline int is_integer_btype(int bt
)
1895 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
1896 bt
== VT_INT
|| bt
== VT_LLONG
);
1899 /* check types for comparison or subtraction of pointers */
1900 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
1902 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
1905 /* null pointers are accepted for all comparisons as gcc */
1906 if (is_null_pointer(p1
) || is_null_pointer(p2
))
1910 bt1
= type1
->t
& VT_BTYPE
;
1911 bt2
= type2
->t
& VT_BTYPE
;
1912 /* accept comparison between pointer and integer with a warning */
1913 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
1914 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
1915 tcc_warning("comparison between pointer and integer");
1919 /* both must be pointers or implicit function pointers */
1920 if (bt1
== VT_PTR
) {
1921 type1
= pointed_type(type1
);
1922 } else if (bt1
!= VT_FUNC
)
1923 goto invalid_operands
;
1925 if (bt2
== VT_PTR
) {
1926 type2
= pointed_type(type2
);
1927 } else if (bt2
!= VT_FUNC
) {
1929 tcc_error("invalid operands to binary %s", get_tok_str(op
, NULL
));
1931 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1932 (type2
->t
& VT_BTYPE
) == VT_VOID
)
1936 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1937 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1938 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1939 /* gcc-like error if '-' is used */
1941 goto invalid_operands
;
1943 tcc_warning("comparison of distinct pointer types lacks a cast");
1947 /* generic gen_op: handles types problems */
1948 ST_FUNC
void gen_op(int op
)
1950 int u
, t1
, t2
, bt1
, bt2
, t
;
1954 t1
= vtop
[-1].type
.t
;
1955 t2
= vtop
[0].type
.t
;
1956 bt1
= t1
& VT_BTYPE
;
1957 bt2
= t2
& VT_BTYPE
;
1959 if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
1960 tcc_error("operation on a struct");
1961 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
1962 if (bt2
== VT_FUNC
) {
1963 mk_pointer(&vtop
->type
);
1966 if (bt1
== VT_FUNC
) {
1968 mk_pointer(&vtop
->type
);
1973 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
1974 /* at least one operand is a pointer */
1975 /* relationnal op: must be both pointers */
1976 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
1977 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1978 /* pointers are handled are unsigned */
1979 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1980 t
= VT_LLONG
| VT_UNSIGNED
;
1982 t
= VT_INT
| VT_UNSIGNED
;
1986 /* if both pointers, then it must be the '-' op */
1987 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
1989 tcc_error("cannot use pointers here");
1990 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1991 /* XXX: check that types are compatible */
1992 if (vtop
[-1].type
.t
& VT_VLA
) {
1993 vla_runtime_pointed_size(&vtop
[-1].type
);
1995 vpushi(pointed_size(&vtop
[-1].type
));
1999 /* set to integer type */
2000 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2001 vtop
->type
.t
= VT_LLONG
;
2003 vtop
->type
.t
= VT_INT
;
2008 /* exactly one pointer : must be '+' or '-'. */
2009 if (op
!= '-' && op
!= '+')
2010 tcc_error("cannot use pointers here");
2011 /* Put pointer as first operand */
2012 if (bt2
== VT_PTR
) {
2014 t
= t1
, t1
= t2
, t2
= t
;
2017 if ((vtop
[0].type
.t
& VT_BTYPE
) == VT_LLONG
)
2018 /* XXX: truncate here because gen_opl can't handle ptr + long long */
2019 gen_cast(&int_type
);
2021 type1
= vtop
[-1].type
;
2022 type1
.t
&= ~VT_ARRAY
;
2023 if (vtop
[-1].type
.t
& VT_VLA
)
2024 vla_runtime_pointed_size(&vtop
[-1].type
);
2026 u
= pointed_size(&vtop
[-1].type
);
2028 tcc_error("unknown array element size");
2029 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2032 /* XXX: cast to int ? (long long case) */
2038 /* #ifdef CONFIG_TCC_BCHECK
2039 The main reason to removing this code:
2046 fprintf(stderr, "v+i-j = %p\n", v+i-j);
2047 fprintf(stderr, "v+(i-j) = %p\n", v+(i-j));
2049 When this code is on. then the output looks like
2051 v+(i-j) = 0xbff84000
2053 /* if evaluating constant expression, no code should be
2054 generated, so no bound check */
2055 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
2056 /* if bounded pointers, we generate a special code to
2063 gen_bounded_ptr_add();
2069 /* put again type if gen_opic() swaped operands */
2072 } else if (is_float(bt1
) || is_float(bt2
)) {
2073 /* compute bigger type and do implicit casts */
2074 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
2076 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
2081 /* floats can only be used for a few operations */
2082 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
2083 (op
< TOK_ULT
|| op
> TOK_GT
))
2084 tcc_error("invalid operands for binary operation");
2086 } else if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
) {
2087 t
= bt1
== VT_LLONG
? VT_LLONG
: VT_INT
;
2088 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (t
| VT_UNSIGNED
))
2091 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
2092 /* cast to biggest op */
2094 /* convert to unsigned if it does not fit in a long long */
2095 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
2096 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
2100 /* integer operations */
2102 /* convert to unsigned if it does not fit in an integer */
2103 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
2104 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
2107 /* XXX: currently, some unsigned operations are explicit, so
2108 we modify them here */
2109 if (t
& VT_UNSIGNED
) {
2116 else if (op
== TOK_LT
)
2118 else if (op
== TOK_GT
)
2120 else if (op
== TOK_LE
)
2122 else if (op
== TOK_GE
)
2129 /* special case for shifts and long long: we keep the shift as
2131 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
2138 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
2139 /* relationnal op: the result is an int */
2140 vtop
->type
.t
= VT_INT
;
2145 // Make sure that we have converted to an rvalue:
2146 if (vtop
->r
& VT_LVAL
)
2147 gv(is_float(vtop
->type
.t
& VT_BTYPE
) ? RC_FLOAT
: RC_INT
);
2150 #ifndef TCC_TARGET_ARM
2151 /* generic itof for unsigned long long case */
2152 static void gen_cvt_itof1(int t
)
2154 #ifdef TCC_TARGET_ARM64
2157 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
2158 (VT_LLONG
| VT_UNSIGNED
)) {
2161 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
2162 #if LDOUBLE_SIZE != 8
2163 else if (t
== VT_LDOUBLE
)
2164 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
2167 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
2171 vtop
->r
= reg_fret(t
);
2179 /* generic ftoi for unsigned long long case */
2180 static void gen_cvt_ftoi1(int t
)
2182 #ifdef TCC_TARGET_ARM64
2187 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
2188 /* not handled natively */
2189 st
= vtop
->type
.t
& VT_BTYPE
;
2191 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
2192 #if LDOUBLE_SIZE != 8
2193 else if (st
== VT_LDOUBLE
)
2194 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
2197 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
2202 vtop
->r2
= REG_LRET
;
2209 /* force char or short cast */
2210 static void force_charshort_cast(int t
)
2214 /* XXX: add optimization if lvalue : just change type and offset */
2219 if (t
& VT_UNSIGNED
) {
2220 vpushi((1 << bits
) - 1);
2223 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
)
2229 /* result must be signed or the SAR is converted to an SHL
2230 This was not the case when "t" was a signed short
2231 and the last value on the stack was an unsigned int */
2232 vtop
->type
.t
&= ~VT_UNSIGNED
;
2238 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
2239 static void gen_cast(CType
*type
)
2241 int sbt
, dbt
, sf
, df
, c
, p
;
2243 /* special delayed cast for char/short */
2244 /* XXX: in some cases (multiple cascaded casts), it may still
2246 if (vtop
->r
& VT_MUSTCAST
) {
2247 vtop
->r
&= ~VT_MUSTCAST
;
2248 force_charshort_cast(vtop
->type
.t
);
2251 /* bitfields first get cast to ints */
2252 if (vtop
->type
.t
& VT_BITFIELD
) {
2256 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
2257 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
2262 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
2263 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
2265 /* constant case: we can do it now */
2266 /* XXX: in ISOC, cannot do it if error in convert */
2267 if (sbt
== VT_FLOAT
)
2268 vtop
->c
.ld
= vtop
->c
.f
;
2269 else if (sbt
== VT_DOUBLE
)
2270 vtop
->c
.ld
= vtop
->c
.d
;
2273 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
2274 if ((sbt
& VT_UNSIGNED
) || !(vtop
->c
.i
>> 63))
2275 vtop
->c
.ld
= vtop
->c
.i
;
2277 vtop
->c
.ld
= -(long double)-vtop
->c
.i
;
2279 if ((sbt
& VT_UNSIGNED
) || !(vtop
->c
.i
>> 31))
2280 vtop
->c
.ld
= (uint32_t)vtop
->c
.i
;
2282 vtop
->c
.ld
= -(long double)-(uint32_t)vtop
->c
.i
;
2285 if (dbt
== VT_FLOAT
)
2286 vtop
->c
.f
= (float)vtop
->c
.ld
;
2287 else if (dbt
== VT_DOUBLE
)
2288 vtop
->c
.d
= (double)vtop
->c
.ld
;
2289 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
2290 vtop
->c
.i
= vtop
->c
.ld
;
2291 } else if (sf
&& dbt
== VT_BOOL
) {
2292 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
2295 vtop
->c
.i
= vtop
->c
.ld
;
2296 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
2298 else if (sbt
& VT_UNSIGNED
)
2299 vtop
->c
.i
= (uint32_t)vtop
->c
.i
;
2300 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2301 else if (sbt
== VT_PTR
)
2304 else if (sbt
!= VT_LLONG
)
2305 vtop
->c
.i
= ((uint32_t)vtop
->c
.i
|
2306 -(vtop
->c
.i
& 0x80000000));
2308 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
2310 else if (dbt
== VT_BOOL
)
2311 vtop
->c
.i
= (vtop
->c
.i
!= 0);
2312 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2313 else if (dbt
== VT_PTR
)
2316 else if (dbt
!= VT_LLONG
) {
2317 uint32_t m
= ((dbt
& VT_BTYPE
) == VT_BYTE
? 0xff :
2318 (dbt
& VT_BTYPE
) == VT_SHORT
? 0xffff :
2321 if (!(dbt
& VT_UNSIGNED
))
2322 vtop
->c
.i
|= -(vtop
->c
.i
& ((m
>> 1) + 1));
2325 } else if (p
&& dbt
== VT_BOOL
) {
2329 /* non constant case: generate code */
2331 /* convert from fp to fp */
2334 /* convert int to fp */
2337 /* convert fp to int */
2338 if (dbt
== VT_BOOL
) {
2342 if (sbt
== VT_FLOAT
) {
2343 /* cast to DOUBLE to avoid precision loss */
2344 gen_cvt_ftof(VT_DOUBLE
);
2345 vtop
->type
.t
= (vtop
->type
.t
& ~VT_BTYPE
) | VT_DOUBLE
;
2347 /* we handle char/short/etc... with generic code */
2348 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
2349 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
2353 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
2354 /* additional cast for char/short... */
2359 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
2360 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
2361 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
2362 /* scalar to long long */
2363 /* machine independent conversion */
2365 /* generate high word */
2366 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
2370 if (sbt
== VT_PTR
) {
2371 /* cast from pointer to int before we apply
2372 shift operation, which pointers don't support*/
2373 gen_cast(&int_type
);
2379 /* patch second register */
2380 vtop
[-1].r2
= vtop
->r
;
2384 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
2385 (dbt
& VT_BTYPE
) == VT_PTR
||
2386 (dbt
& VT_BTYPE
) == VT_FUNC
) {
2387 if ((sbt
& VT_BTYPE
) != VT_LLONG
&&
2388 (sbt
& VT_BTYPE
) != VT_PTR
&&
2389 (sbt
& VT_BTYPE
) != VT_FUNC
) {
2390 /* need to convert from 32bit to 64bit */
2392 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
2393 #if defined(TCC_TARGET_ARM64)
2395 #elif defined(TCC_TARGET_X86_64)
2397 /* x86_64 specific: movslq */
2399 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
2406 } else if (dbt
== VT_BOOL
) {
2407 /* scalar to bool */
2410 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
2411 (dbt
& VT_BTYPE
) == VT_SHORT
) {
2412 if (sbt
== VT_PTR
) {
2413 vtop
->type
.t
= VT_INT
;
2414 tcc_warning("nonportable conversion from pointer to char/short");
2416 force_charshort_cast(dbt
);
2417 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
2418 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
2420 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
2421 /* from long long: just take low order word */
2425 /* if lvalue and single word type, nothing to do because
2426 the lvalue already contains the real type size (see
2427 VT_LVAL_xxx constants) */
2431 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
2432 /* if we are casting between pointer types,
2433 we must update the VT_LVAL_xxx size */
2434 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
2435 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
2440 /* return type size as known at compile time. Put alignment at 'a' */
2441 ST_FUNC
int type_size(CType
*type
, int *a
)
2446 bt
= type
->t
& VT_BTYPE
;
2447 if (bt
== VT_STRUCT
) {
2452 } else if (bt
== VT_PTR
) {
2453 if (type
->t
& VT_ARRAY
) {
2457 ts
= type_size(&s
->type
, a
);
2459 if (ts
< 0 && s
->c
< 0)
2467 } else if (bt
== VT_LDOUBLE
) {
2469 return LDOUBLE_SIZE
;
2470 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
2471 #ifdef TCC_TARGET_I386
2472 #ifdef TCC_TARGET_PE
2477 #elif defined(TCC_TARGET_ARM)
2487 } else if (bt
== VT_INT
|| bt
== VT_FLOAT
) {
2490 } else if (bt
== VT_SHORT
) {
2493 } else if (bt
== VT_QLONG
|| bt
== VT_QFLOAT
) {
2496 } else if (bt
== VT_ENUM
) {
2498 /* Enums might be incomplete, so don't just return '4' here. */
2499 return type
->ref
->c
;
2501 /* char, void, function, _Bool */
2507 /* push type size as known at runtime time on top of value stack. Put
2509 ST_FUNC
void vla_runtime_type_size(CType
*type
, int *a
)
2511 if (type
->t
& VT_VLA
) {
2512 type_size(&type
->ref
->type
, a
);
2513 vset(&int_type
, VT_LOCAL
|VT_LVAL
, type
->ref
->c
);
2515 vpushi(type_size(type
, a
));
2519 static void vla_sp_restore(void) {
2520 if (vlas_in_scope
) {
2521 gen_vla_sp_restore(vla_sp_loc
);
2525 static void vla_sp_restore_root(void) {
2526 if (vlas_in_scope
) {
2527 gen_vla_sp_restore(vla_sp_root_loc
);
2531 /* return the pointed type of t */
2532 static inline CType
*pointed_type(CType
*type
)
2534 return &type
->ref
->type
;
2537 /* modify type so that its it is a pointer to type. */
2538 ST_FUNC
void mk_pointer(CType
*type
)
2541 s
= sym_push(SYM_FIELD
, type
, 0, -1);
2542 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
2546 /* compare function types. OLD functions match any new functions */
2547 static int is_compatible_func(CType
*type1
, CType
*type2
)
2553 if (!is_compatible_types(&s1
->type
, &s2
->type
))
2555 /* check func_call */
2556 if (s1
->a
.func_call
!= s2
->a
.func_call
)
2558 /* XXX: not complete */
2559 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
2563 while (s1
!= NULL
) {
2566 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
2576 /* return true if type1 and type2 are the same. If unqualified is
2577 true, qualifiers on the types are ignored.
2579 - enums are not checked as gcc __builtin_types_compatible_p ()
2581 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2585 t1
= type1
->t
& VT_TYPE
;
2586 t2
= type2
->t
& VT_TYPE
;
2588 /* strip qualifiers before comparing */
2589 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2590 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2592 /* Default Vs explicit signedness only matters for char */
2593 if ((t1
& VT_BTYPE
) != VT_BYTE
) {
2597 /* An enum is compatible with (unsigned) int. Ideally we would
2598 store the enums signedness in type->ref.a.<some_bit> and
2599 only accept unsigned enums with unsigned int and vice versa.
2600 But one of our callers (gen_assign_cast) always strips VT_UNSIGNED
2601 from pointer target types, so we can't add it here either. */
2602 if ((t1
& VT_BTYPE
) == VT_ENUM
) {
2604 if (type1
->ref
->a
.unsigned_enum
)
2607 if ((t2
& VT_BTYPE
) == VT_ENUM
) {
2609 if (type2
->ref
->a
.unsigned_enum
)
2612 /* XXX: bitfields ? */
2615 /* test more complicated cases */
2616 bt1
= t1
& VT_BTYPE
;
2617 if (bt1
== VT_PTR
) {
2618 type1
= pointed_type(type1
);
2619 type2
= pointed_type(type2
);
2620 return is_compatible_types(type1
, type2
);
2621 } else if (bt1
== VT_STRUCT
) {
2622 return (type1
->ref
== type2
->ref
);
2623 } else if (bt1
== VT_FUNC
) {
2624 return is_compatible_func(type1
, type2
);
2630 /* return true if type1 and type2 are exactly the same (including
2633 static int is_compatible_types(CType
*type1
, CType
*type2
)
2635 return compare_types(type1
,type2
,0);
2638 /* return true if type1 and type2 are the same (ignoring qualifiers).
2640 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
2642 return compare_types(type1
,type2
,1);
2645 /* print a type. If 'varstr' is not NULL, then the variable is also
2646 printed in the type */
2648 /* XXX: add array and function pointers */
2649 static void type_to_str(char *buf
, int buf_size
,
2650 CType
*type
, const char *varstr
)
2657 t
= type
->t
& VT_TYPE
;
2660 if (t
& VT_CONSTANT
)
2661 pstrcat(buf
, buf_size
, "const ");
2662 if (t
& VT_VOLATILE
)
2663 pstrcat(buf
, buf_size
, "volatile ");
2664 if ((t
& (VT_DEFSIGN
| VT_UNSIGNED
)) == (VT_DEFSIGN
| VT_UNSIGNED
))
2665 pstrcat(buf
, buf_size
, "unsigned ");
2666 else if (t
& VT_DEFSIGN
)
2667 pstrcat(buf
, buf_size
, "signed ");
2697 tstr
= "long double";
2699 pstrcat(buf
, buf_size
, tstr
);
2703 if (bt
== VT_STRUCT
)
2707 pstrcat(buf
, buf_size
, tstr
);
2708 v
= type
->ref
->v
& ~SYM_STRUCT
;
2709 if (v
>= SYM_FIRST_ANOM
)
2710 pstrcat(buf
, buf_size
, "<anonymous>");
2712 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
2716 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
2717 pstrcat(buf
, buf_size
, "(");
2719 while (sa
!= NULL
) {
2720 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
2721 pstrcat(buf
, buf_size
, buf1
);
2724 pstrcat(buf
, buf_size
, ", ");
2726 pstrcat(buf
, buf_size
, ")");
2731 snprintf(buf1
, sizeof(buf1
), "%s[%ld]", varstr
? varstr
: "", s
->c
);
2732 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2735 pstrcpy(buf1
, sizeof(buf1
), "*");
2736 if (t
& VT_CONSTANT
)
2737 pstrcat(buf1
, buf_size
, "const ");
2738 if (t
& VT_VOLATILE
)
2739 pstrcat(buf1
, buf_size
, "volatile ");
2741 pstrcat(buf1
, sizeof(buf1
), varstr
);
2742 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2746 pstrcat(buf
, buf_size
, " ");
2747 pstrcat(buf
, buf_size
, varstr
);
2752 /* verify type compatibility to store vtop in 'dt' type, and generate
2754 static void gen_assign_cast(CType
*dt
)
2756 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
2757 char buf1
[256], buf2
[256];
2760 st
= &vtop
->type
; /* source type */
2761 dbt
= dt
->t
& VT_BTYPE
;
2762 sbt
= st
->t
& VT_BTYPE
;
2763 if (sbt
== VT_VOID
|| dbt
== VT_VOID
) {
2764 if (sbt
== VT_VOID
&& dbt
== VT_VOID
)
2766 It is Ok if both are void
2772 gcc accepts this program
2775 tcc_error("cannot cast from/to void");
2777 if (dt
->t
& VT_CONSTANT
)
2778 tcc_warning("assignment of read-only location");
2781 /* special cases for pointers */
2782 /* '0' can also be a pointer */
2783 if (is_null_pointer(vtop
))
2785 /* accept implicit pointer to integer cast with warning */
2786 if (is_integer_btype(sbt
)) {
2787 tcc_warning("assignment makes pointer from integer without a cast");
2790 type1
= pointed_type(dt
);
2791 /* a function is implicitely a function pointer */
2792 if (sbt
== VT_FUNC
) {
2793 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
2794 !is_compatible_types(pointed_type(dt
), st
))
2795 tcc_warning("assignment from incompatible pointer type");
2800 type2
= pointed_type(st
);
2801 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2802 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
2803 /* void * can match anything */
2805 /* exact type match, except for qualifiers */
2808 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2809 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2810 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2811 /* Like GCC don't warn by default for merely changes
2812 in pointer target signedness. Do warn for different
2813 base types, though, in particular for unsigned enums
2814 and signed int targets. */
2815 if ((tmp_type1
.t
& (VT_DEFSIGN
| VT_UNSIGNED
)) !=
2816 (tmp_type2
.t
& (VT_DEFSIGN
| VT_UNSIGNED
)) &&
2817 (tmp_type1
.t
& VT_BTYPE
) == (tmp_type2
.t
& VT_BTYPE
))
2820 tcc_warning("assignment from incompatible pointer type");
2823 /* check const and volatile */
2824 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
2825 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
2826 tcc_warning("assignment discards qualifiers from pointer target type");
2832 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
2833 tcc_warning("assignment makes integer from pointer without a cast");
2834 } else if (sbt
== VT_STRUCT
) {
2835 goto case_VT_STRUCT
;
2837 /* XXX: more tests */
2843 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2844 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2845 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2847 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
2848 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
2849 tcc_error("cannot cast '%s' to '%s'", buf1
, buf2
);
2857 /* store vtop in lvalue pushed on stack */
2858 ST_FUNC
void vstore(void)
2860 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
2862 ft
= vtop
[-1].type
.t
;
2863 sbt
= vtop
->type
.t
& VT_BTYPE
;
2864 dbt
= ft
& VT_BTYPE
;
2865 if ((((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2866 (sbt
== VT_INT
&& dbt
== VT_SHORT
))
2867 && !(vtop
->type
.t
& VT_BITFIELD
)) {
2868 /* optimize char/short casts */
2869 delayed_cast
= VT_MUSTCAST
;
2870 vtop
->type
.t
= (ft
& VT_TYPE
& ~VT_BITFIELD
&
2871 ((1 << VT_STRUCT_SHIFT
) - 1));
2872 /* XXX: factorize */
2873 if (ft
& VT_CONSTANT
)
2874 tcc_warning("assignment of read-only location");
2877 if (!(ft
& VT_BITFIELD
))
2878 gen_assign_cast(&vtop
[-1].type
);
2881 if (sbt
== VT_STRUCT
) {
2882 /* if structure, only generate pointer */
2883 /* structure assignment : generate memcpy */
2884 /* XXX: optimize if small size */
2885 size
= type_size(&vtop
->type
, &align
);
2889 vtop
->type
.t
= VT_PTR
;
2892 /* address of memcpy() */
2895 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2896 else if(!(align
& 3))
2897 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2900 /* Use memmove, rather than memcpy, as dest and src may be same: */
2901 vpush_global_sym(&func_old_type
, TOK_memmove
);
2906 vtop
->type
.t
= VT_PTR
;
2912 /* leave source on stack */
2913 } else if (ft
& VT_BITFIELD
) {
2914 /* bitfield store handling */
2916 /* save lvalue as expression result (example: s.b = s.a = n;) */
2917 vdup(), vtop
[-1] = vtop
[-2];
2919 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2920 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2921 /* remove bit field info to avoid loops */
2922 vtop
[-1].type
.t
= ft
& ~VT_BITFIELD
& ((1 << VT_STRUCT_SHIFT
) - 1);
2924 if((ft
& VT_BTYPE
) == VT_BOOL
) {
2925 gen_cast(&vtop
[-1].type
);
2926 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2929 /* duplicate destination */
2931 vtop
[-1] = vtop
[-2];
2933 /* mask and shift source */
2934 if((ft
& VT_BTYPE
) != VT_BOOL
) {
2935 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2936 vpushll((1ULL << bit_size
) - 1ULL);
2938 vpushi((1 << bit_size
) - 1);
2944 /* load destination, mask and or with source */
2946 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2947 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2949 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2955 /* ... and discard */
2959 #ifdef CONFIG_TCC_BCHECK
2960 /* bound check case */
2961 if (vtop
[-1].r
& VT_MUSTBOUND
) {
2970 #ifdef TCC_TARGET_X86_64
2971 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
2973 } else if ((ft
& VT_BTYPE
) == VT_QFLOAT
) {
2978 r
= gv(rc
); /* generate value */
2979 /* if lvalue was saved on stack, must read it */
2980 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
2982 t
= get_reg(RC_INT
);
2983 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2988 sv
.r
= VT_LOCAL
| VT_LVAL
;
2989 sv
.c
.i
= vtop
[-1].c
.i
;
2991 vtop
[-1].r
= t
| VT_LVAL
;
2993 /* two word case handling : store second register at word + 4 (or +8 for x86-64) */
2994 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2995 if (((ft
& VT_BTYPE
) == VT_QLONG
) || ((ft
& VT_BTYPE
) == VT_QFLOAT
)) {
2996 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
2998 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
2999 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
3001 vtop
[-1].type
.t
= load_type
;
3004 /* convert to int to increment easily */
3005 vtop
->type
.t
= addr_type
;
3011 vtop
[-1].type
.t
= load_type
;
3012 /* XXX: it works because r2 is spilled last ! */
3013 store(vtop
->r2
, vtop
- 1);
3019 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
3020 vtop
->r
|= delayed_cast
;
3024 /* post defines POST/PRE add. c is the token ++ or -- */
3025 ST_FUNC
void inc(int post
, int c
)
3028 vdup(); /* save lvalue */
3030 gv_dup(); /* duplicate value */
3035 vpushi(c
- TOK_MID
);
3037 vstore(); /* store value */
3039 vpop(); /* if post op, return saved value */
3042 ST_FUNC
void parse_mult_str (CString
*astr
, const char *msg
)
3044 /* read the string */
3048 while (tok
== TOK_STR
) {
3049 /* XXX: add \0 handling too ? */
3050 cstr_cat(astr
, tokc
.str
.data
, -1);
3053 cstr_ccat(astr
, '\0');
3056 /* If I is >= 1 and a power of two, returns log2(i)+1.
3057 If I is 0 returns 0. */
3058 static int exact_log2p1(int i
)
3063 for (ret
= 1; i
>= 1 << 8; ret
+= 8)
3074 /* Parse GNUC __attribute__ extension. Currently, the following
3075 extensions are recognized:
3076 - aligned(n) : set data/function alignment.
3077 - packed : force data alignment to 1
3078 - section(x) : generate data/code in this section.
3079 - unused : currently ignored, but may be used someday.
3080 - regparm(n) : pass function parameters in registers (i386 only)
3082 static void parse_attribute(AttributeDef
*ad
)
3087 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
3091 while (tok
!= ')') {
3092 if (tok
< TOK_IDENT
)
3093 expect("attribute name");
3100 parse_mult_str(&astr
, "section name");
3101 ad
->section
= find_section(tcc_state
, (char *)astr
.data
);
3108 parse_mult_str(&astr
, "alias(\"target\")");
3109 ad
->alias_target
= /* save string as token, for later */
3110 tok_alloc((char*)astr
.data
, astr
.size
-1)->tok
;
3114 case TOK_VISIBILITY1
:
3115 case TOK_VISIBILITY2
:
3117 parse_mult_str(&astr
,
3118 "visibility(\"default|hidden|internal|protected\")");
3119 if (!strcmp (astr
.data
, "default"))
3120 ad
->a
.visibility
= STV_DEFAULT
;
3121 else if (!strcmp (astr
.data
, "hidden"))
3122 ad
->a
.visibility
= STV_HIDDEN
;
3123 else if (!strcmp (astr
.data
, "internal"))
3124 ad
->a
.visibility
= STV_INTERNAL
;
3125 else if (!strcmp (astr
.data
, "protected"))
3126 ad
->a
.visibility
= STV_PROTECTED
;
3128 expect("visibility(\"default|hidden|internal|protected\")");
3137 if (n
<= 0 || (n
& (n
- 1)) != 0)
3138 tcc_error("alignment must be a positive power of two");
3143 ad
->a
.aligned
= exact_log2p1(n
);
3144 if (n
!= 1 << (ad
->a
.aligned
- 1))
3145 tcc_error("alignment of %d is larger than implemented", n
);
3157 /* currently, no need to handle it because tcc does not
3158 track unused objects */
3162 /* currently, no need to handle it because tcc does not
3163 track unused objects */
3168 ad
->a
.func_call
= FUNC_CDECL
;
3173 ad
->a
.func_call
= FUNC_STDCALL
;
3175 #ifdef TCC_TARGET_I386
3185 ad
->a
.func_call
= FUNC_FASTCALL1
+ n
- 1;
3191 ad
->a
.func_call
= FUNC_FASTCALLW
;
3198 ad
->a
.mode
= VT_LLONG
+ 1;
3201 ad
->a
.mode
= VT_BYTE
+ 1;
3204 ad
->a
.mode
= VT_SHORT
+ 1;
3208 ad
->a
.mode
= VT_INT
+ 1;
3211 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
3218 ad
->a
.func_export
= 1;
3221 ad
->a
.func_import
= 1;
3224 if (tcc_state
->warn_unsupported
)
3225 tcc_warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
3226 /* skip parameters */
3228 int parenthesis
= 0;
3232 else if (tok
== ')')
3235 } while (parenthesis
&& tok
!= -1);
3248 static Sym
* find_field (CType
*type
, int v
)
3252 while ((s
= s
->next
) != NULL
) {
3253 if ((s
->v
& SYM_FIELD
) &&
3254 (s
->type
.t
& VT_BTYPE
) == VT_STRUCT
&&
3255 (s
->v
& ~SYM_FIELD
) >= SYM_FIRST_ANOM
) {
3256 Sym
*ret
= find_field (&s
->type
, v
);
3266 static void struct_add_offset (Sym
*s
, int offset
)
3268 while ((s
= s
->next
) != NULL
) {
3269 if ((s
->v
& SYM_FIELD
) &&
3270 (s
->type
.t
& VT_BTYPE
) == VT_STRUCT
&&
3271 (s
->v
& ~SYM_FIELD
) >= SYM_FIRST_ANOM
) {
3272 struct_add_offset(s
->type
.ref
, offset
);
3278 static void struct_layout(CType
*type
, AttributeDef
*ad
)
3280 int align
, maxalign
, offset
, c
, bit_pos
, bt
, prevbt
, prev_bit_size
;
3281 int pcc
= !tcc_state
->ms_bitfields
;
3284 maxalign
= 1 << (ad
->a
.aligned
- 1);
3290 prevbt
= VT_STRUCT
; /* make it never match */
3292 for (f
= type
->ref
->next
; f
; f
= f
->next
) {
3293 int typealign
, bit_size
;
3294 int size
= type_size(&f
->type
, &typealign
);
3295 if (f
->type
.t
& VT_BITFIELD
)
3296 bit_size
= (f
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
3299 if (bit_size
== 0 && pcc
) {
3300 /* Zero-width bit-fields in PCC mode aren't affected
3301 by any packing (attribute or pragma). */
3303 } else if (f
->r
> 1) {
3305 } else if (ad
->a
.packed
|| f
->r
== 1) {
3307 /* Packed fields or packed records don't let the base type
3308 influence the records type alignment. */
3313 if (type
->ref
->type
.t
!= TOK_STRUCT
) {
3314 if (pcc
&& bit_size
>= 0)
3315 size
= (bit_size
+ 7) >> 3;
3316 /* Bit position is already zero from our caller. */
3320 } else if (bit_size
< 0) {
3321 int addbytes
= pcc
? (bit_pos
+ 7) >> 3 : 0;
3324 c
= (c
+ addbytes
+ align
- 1) & -align
;
3330 /* A bit-field. Layout is more complicated. There are two
3331 options TCC implements: PCC compatible and MS compatible
3332 (PCC compatible is what GCC uses for almost all targets).
3333 In PCC layout the overall size of the struct (in c) is
3334 _excluding_ the current run of bit-fields (that is,
3335 there's at least additional bit_pos bits after c). In
3336 MS layout c does include the current run of bit-fields.
3338 This matters for calculating the natural alignment buckets
3341 /* 'align' will be used to influence records alignment,
3342 so it's the max of specified and type alignment, except
3343 in certain cases that depend on the mode. */
3344 if (align
< typealign
)
3347 /* In PCC layout a non-packed bit-field is placed adjacent
3348 to the preceding bit-fields, except if it would overflow
3349 its container (depending on base type) or it's a zero-width
3350 bit-field. Packed non-zero-width bit-fields always are
3352 int ofs
= (c
* 8 + bit_pos
) % (typealign
* 8);
3353 int ofs2
= ofs
+ bit_size
+ (typealign
* 8) - 1;
3354 if (bit_size
== 0 ||
3356 (ofs2
/ (typealign
* 8)) > (size
/typealign
))) {
3357 c
= (c
+ ((bit_pos
+ 7) >> 3) + typealign
- 1) & -typealign
;
3361 /* In PCC layout named bit-fields influence the alignment
3362 of the containing struct using the base types alignment,
3363 except for packed fields (which here have correct
3364 align/typealign). */
3365 if ((f
->v
& SYM_FIRST_ANOM
))
3368 bt
= f
->type
.t
& VT_BTYPE
;
3369 if ((bit_pos
+ bit_size
> size
* 8) ||
3370 (bit_size
> 0) == (bt
!= prevbt
)) {
3371 c
= (c
+ typealign
- 1) & -typealign
;
3374 /* In MS bitfield mode a bit-field run always uses
3375 at least as many bits as the underlying type.
3376 To start a new run it's also required that this
3377 or the last bit-field had non-zero width. */
3378 if (bit_size
|| prev_bit_size
)
3381 /* In MS layout the records alignment is normally
3382 influenced by the field, except for a zero-width
3383 field at the start of a run (but by further zero-width
3384 fields it is again). */
3385 if (bit_size
== 0 && prevbt
!= bt
)
3388 prev_bit_size
= bit_size
;
3390 f
->type
.t
= (f
->type
.t
& ~(0x3f << VT_STRUCT_SHIFT
))
3391 | (bit_pos
<< VT_STRUCT_SHIFT
);
3392 bit_pos
+= bit_size
;
3393 if (pcc
&& bit_pos
>= size
* 8) {
3395 bit_pos
-= size
* 8;
3398 if (align
> maxalign
)
3401 printf("set field %s offset=%d c=%d",
3402 get_tok_str(f
->v
& ~SYM_FIELD
, NULL
), offset
, c
);
3403 if (f
->type
.t
& VT_BITFIELD
) {
3404 printf(" pos=%d size=%d",
3405 (f
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
3406 (f
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
3411 if (f
->v
& SYM_FIRST_ANOM
&& (f
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
3413 /* An anonymous struct/union. Adjust member offsets
3414 to reflect the real offset of our containing struct.
3415 Also set the offset of this anon member inside
3416 the outer struct to be zero. Via this it
3417 works when accessing the field offset directly
3418 (from base object), as well as when recursing
3419 members in initializer handling. */
3420 int v2
= f
->type
.ref
->v
;
3421 if (!(v2
& SYM_FIELD
) &&
3422 (v2
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
3424 /* This happens only with MS extensions. The
3425 anon member has a named struct type, so it
3426 potentially is shared with other references.
3427 We need to unshare members so we can modify
3430 f
->type
.ref
= sym_push(anon_sym
++ | SYM_FIELD
,
3431 &f
->type
.ref
->type
, 0,
3433 pps
= &f
->type
.ref
->next
;
3434 while ((ass
= ass
->next
) != NULL
) {
3435 *pps
= sym_push(ass
->v
, &ass
->type
, 0, ass
->c
);
3436 pps
= &((*pps
)->next
);
3440 struct_add_offset(f
->type
.ref
, offset
);
3448 /* store size and alignment */
3449 type
->ref
->c
= (c
+ (pcc
? (bit_pos
+ 7) >> 3 : 0)
3450 + maxalign
- 1) & -maxalign
;
3451 type
->ref
->r
= maxalign
;
3454 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
3455 static void struct_decl(CType
*type
, AttributeDef
*ad
, int u
)
3457 int a
, v
, size
, align
, flexible
, alignoverride
;
3459 int bit_size
, bsize
, bt
;
3464 a
= tok
; /* save decl type */
3466 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3467 parse_attribute(ad
);
3471 /* struct already defined ? return it */
3473 expect("struct/union/enum name");
3475 if (s
&& (s
->scope
== local_scope
|| (tok
!= '{' && tok
!= ';'))) {
3477 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
3483 /* Record the original enum/struct/union token. */
3486 /* we put an undefined size for struct/union */
3487 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
3488 s
->r
= 0; /* default alignment is zero as gcc */
3489 /* put struct/union/enum name in type */
3497 tcc_error("struct/union/enum already defined");
3498 /* cannot be empty */
3500 /* non empty enums are not allowed */
3501 if (a
== TOK_ENUM
) {
3505 CType
*t
= &int_type
;
3508 expect("identifier");
3510 if (ss
&& !local_stack
)
3511 tcc_error("redefinition of enumerator '%s'",
3512 get_tok_str(v
, NULL
));
3516 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
3519 /* We really want to support long long enums
3520 on i386 as well, but the Sym structure only
3521 holds a 'long' for associated constants,
3522 and enlarging it would bump its size (no
3523 available padding). So punt for now. */
3529 if (c
!= (int)c
&& (unsigned long)c
!= (unsigned int)c
)
3530 seen_wide
= 1, t
= &size_type
;
3531 /* enum symbols have static storage */
3532 ss
= sym_push(v
, t
, VT_CONST
, c
);
3533 ss
->type
.t
|= VT_STATIC
;
3538 /* NOTE: we accept a trailing comma */
3543 s
->a
.unsigned_enum
= 1;
3544 s
->c
= type_size(seen_wide
? &size_type
: &int_type
, &align
);
3549 while (tok
!= '}') {
3550 if (!parse_btype(&btype
, &ad1
)) {
3556 tcc_error("flexible array member '%s' not at the end of struct",
3557 get_tok_str(v
, NULL
));
3562 type_decl(&type1
, &ad1
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3564 if ((type1
.t
& VT_BTYPE
) != VT_STRUCT
)
3565 expect("identifier");
3567 int v
= btype
.ref
->v
;
3568 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
3569 if (tcc_state
->ms_extensions
== 0)
3570 expect("identifier");
3574 if (type_size(&type1
, &align
) < 0) {
3575 if ((a
== TOK_STRUCT
) && (type1
.t
& VT_ARRAY
) && c
)
3578 tcc_error("field '%s' has incomplete type",
3579 get_tok_str(v
, NULL
));
3581 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
3582 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
3583 tcc_error("invalid type for '%s'",
3584 get_tok_str(v
, NULL
));
3588 bit_size
= expr_const();
3589 /* XXX: handle v = 0 case for messages */
3591 tcc_error("negative width in bit-field '%s'",
3592 get_tok_str(v
, NULL
));
3593 if (v
&& bit_size
== 0)
3594 tcc_error("zero width for bit-field '%s'",
3595 get_tok_str(v
, NULL
));
3596 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3597 parse_attribute(&ad1
);
3599 size
= type_size(&type1
, &align
);
3600 /* Only remember non-default alignment. */
3602 if (ad1
.a
.aligned
) {
3603 int speca
= 1 << (ad1
.a
.aligned
- 1);
3604 alignoverride
= speca
;
3605 } else if (ad1
.a
.packed
|| ad
->a
.packed
) {
3607 } else if (*tcc_state
->pack_stack_ptr
) {
3608 if (align
> *tcc_state
->pack_stack_ptr
)
3609 alignoverride
= *tcc_state
->pack_stack_ptr
;
3611 if (bit_size
>= 0) {
3612 bt
= type1
.t
& VT_BTYPE
;
3619 tcc_error("bitfields must have scalar type");
3621 if (bit_size
> bsize
) {
3622 tcc_error("width of '%s' exceeds its type",
3623 get_tok_str(v
, NULL
));
3624 } else if (bit_size
== bsize
) {
3625 /* no need for bit fields */
3628 type1
.t
|= VT_BITFIELD
|
3629 (0 << VT_STRUCT_SHIFT
) |
3630 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
3633 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
3634 /* Remember we've seen a real field to check
3635 for placement of flexible array member. */
3638 /* If member is a struct or bit-field, enforce
3639 placing into the struct (as anonymous). */
3641 ((type1
.t
& VT_BTYPE
) == VT_STRUCT
||
3646 ss
= sym_push(v
| SYM_FIELD
, &type1
, alignoverride
, 0);
3650 if (tok
== ';' || tok
== TOK_EOF
)
3657 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3658 parse_attribute(ad
);
3659 struct_layout(type
, ad
);
3664 /* return 1 if basic type is a type size (short, long, long long) */
3665 ST_FUNC
int is_btype_size(int bt
)
3667 return bt
== VT_SHORT
|| bt
== VT_LONG
|| bt
== VT_LLONG
;
3670 /* Add type qualifiers to a type. If the type is an array then the qualifiers
3671 are added to the element type, copied because it could be a typedef. */
3672 static void parse_btype_qualify(CType
*type
, int qualifiers
)
3674 while (type
->t
& VT_ARRAY
) {
3675 type
->ref
= sym_push(SYM_FIELD
, &type
->ref
->type
, 0, type
->ref
->c
);
3676 type
= &type
->ref
->type
;
3678 type
->t
|= qualifiers
;
3681 /* return 0 if no type declaration. otherwise, return the basic type
3684 static int parse_btype(CType
*type
, AttributeDef
*ad
)
3686 int t
, u
, bt_size
, complete
, type_found
, typespec_found
;
3690 memset(ad
, 0, sizeof(AttributeDef
));
3698 /* currently, we really ignore extension */
3709 tcc_error("too many basic types");
3711 bt_size
= is_btype_size (u
& VT_BTYPE
);
3712 if (u
== VT_INT
|| (!bt_size
&& !(t
& VT_TYPEDEF
)))
3727 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
3728 #ifndef TCC_TARGET_PE
3729 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3731 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
3732 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3738 #ifdef TCC_TARGET_ARM64
3740 /* GCC's __uint128_t appears in some Linux header files. Make it a
3741 synonym for long double to get the size and alignment right. */
3753 if ((t
& VT_BTYPE
) == VT_LONG
) {
3754 #ifdef TCC_TARGET_PE
3755 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
3757 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3765 struct_decl(&type1
, ad
, VT_ENUM
);
3768 type
->ref
= type1
.ref
;
3772 struct_decl(&type1
, ad
, VT_STRUCT
);
3775 /* type modifiers */
3780 parse_btype_qualify(type
, VT_CONSTANT
);
3788 parse_btype_qualify(type
, VT_VOLATILE
);
3795 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == (VT_DEFSIGN
|VT_UNSIGNED
))
3796 tcc_error("signed and unsigned modifier");
3809 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == VT_DEFSIGN
)
3810 tcc_error("signed and unsigned modifier");
3811 t
|= VT_DEFSIGN
| VT_UNSIGNED
;
3836 /* GNUC attribute */
3837 case TOK_ATTRIBUTE1
:
3838 case TOK_ATTRIBUTE2
:
3839 parse_attribute(ad
);
3842 t
= (t
& ~VT_BTYPE
) | u
;
3850 parse_expr_type(&type1
);
3851 /* remove all storage modifiers except typedef */
3852 type1
.t
&= ~(VT_STORAGE
&~VT_TYPEDEF
);
3858 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
3861 type
->t
= ((s
->type
.t
& ~VT_TYPEDEF
) |
3862 (t
& ~(VT_CONSTANT
| VT_VOLATILE
)));
3863 type
->ref
= s
->type
.ref
;
3864 if (t
& (VT_CONSTANT
| VT_VOLATILE
))
3865 parse_btype_qualify(type
, t
& (VT_CONSTANT
| VT_VOLATILE
));
3869 /* get attributes from typedef */
3870 if (0 == ad
->a
.aligned
)
3871 ad
->a
.aligned
= s
->a
.aligned
;
3872 if (0 == ad
->a
.func_call
)
3873 ad
->a
.func_call
= s
->a
.func_call
;
3874 ad
->a
.packed
|= s
->a
.packed
;
3883 if (tcc_state
->char_is_unsigned
) {
3884 if ((t
& (VT_DEFSIGN
|VT_BTYPE
)) == VT_BYTE
)
3888 /* long is never used as type */
3889 if ((t
& VT_BTYPE
) == VT_LONG
)
3890 #if (!defined TCC_TARGET_X86_64 && !defined TCC_TARGET_ARM64) || \
3891 defined TCC_TARGET_PE
3892 t
= (t
& ~VT_BTYPE
) | VT_INT
;
3894 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3900 /* convert a function parameter type (array to pointer and function to
3901 function pointer) */
3902 static inline void convert_parameter_type(CType
*pt
)
3904 /* remove const and volatile qualifiers (XXX: const could be used
3905 to indicate a const function parameter */
3906 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3907 /* array must be transformed to pointer according to ANSI C */
3909 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
3914 ST_FUNC
void parse_asm_str(CString
*astr
)
3917 parse_mult_str(astr
, "string constant");
3920 /* Parse an asm label and return the token */
3921 static int asm_label_instr(void)
3927 parse_asm_str(&astr
);
3930 printf("asm_alias: \"%s\"\n", (char *)astr
.data
);
3932 v
= tok_alloc(astr
.data
, astr
.size
- 1)->tok
;
3937 static void post_type(CType
*type
, AttributeDef
*ad
, int storage
)
3939 int n
, l
, t1
, arg_size
, align
;
3940 Sym
**plast
, *s
, *first
;
3945 /* function declaration */
3953 /* read param name and compute offset */
3954 if (l
!= FUNC_OLD
) {
3955 if (!parse_btype(&pt
, &ad1
)) {
3957 tcc_error("invalid type");
3964 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
3966 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3967 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
3968 tcc_error("parameter declared as void");
3969 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
3974 expect("identifier");
3978 convert_parameter_type(&pt
);
3979 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
3985 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
3992 /* if no parameters, then old type prototype */
3996 /* NOTE: const is ignored in returned type as it has a special
3997 meaning in gcc / C++ */
3998 type
->t
&= ~VT_CONSTANT
;
3999 /* some ancient pre-K&R C allows a function to return an array
4000 and the array brackets to be put after the arguments, such
4001 that "int c()[]" means something like "int[] c()" */
4004 skip(']'); /* only handle simple "[]" */
4007 /* we push a anonymous symbol which will contain the function prototype */
4008 ad
->a
.func_args
= arg_size
;
4009 s
= sym_push(SYM_FIELD
, type
, 0, l
);
4014 } else if (tok
== '[') {
4015 int saved_nocode_wanted
= nocode_wanted
;
4016 /* array definition */
4018 if (tok
== TOK_RESTRICT1
)
4023 if (!local_stack
|| (storage
& VT_STATIC
))
4024 vpushi(expr_const());
4026 /* VLAs (which can only happen with local_stack && !VT_STATIC)
4027 length must always be evaluated, even under nocode_wanted,
4028 so that its size slot is initialized (e.g. under sizeof
4033 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4036 tcc_error("invalid array size");
4038 if (!is_integer_btype(vtop
->type
.t
& VT_BTYPE
))
4039 tcc_error("size of variable length array should be an integer");
4044 /* parse next post type */
4045 post_type(type
, ad
, storage
);
4046 if (type
->t
== VT_FUNC
)
4047 tcc_error("declaration of an array of functions");
4048 t1
|= type
->t
& VT_VLA
;
4051 loc
-= type_size(&int_type
, &align
);
4055 vla_runtime_type_size(type
, &align
);
4057 vset(&int_type
, VT_LOCAL
|VT_LVAL
, n
);
4063 nocode_wanted
= saved_nocode_wanted
;
4065 /* we push an anonymous symbol which will contain the array
4067 s
= sym_push(SYM_FIELD
, type
, 0, n
);
4068 type
->t
= (t1
? VT_VLA
: VT_ARRAY
) | VT_PTR
;
4073 /* Parse a type declaration (except basic type), and return the type
4074 in 'type'. 'td' is a bitmask indicating which kind of type decl is
4075 expected. 'type' should contain the basic type. 'ad' is the
4076 attribute definition of the basic type. It can be modified by
4079 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
4082 CType type1
, *type2
;
4083 int qualifiers
, storage
;
4085 while (tok
== '*') {
4093 qualifiers
|= VT_CONSTANT
;
4098 qualifiers
|= VT_VOLATILE
;
4104 /* XXX: clarify attribute handling */
4105 case TOK_ATTRIBUTE1
:
4106 case TOK_ATTRIBUTE2
:
4107 parse_attribute(ad
);
4111 type
->t
|= qualifiers
;
4114 /* recursive type */
4115 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
4116 type1
.t
= 0; /* XXX: same as int */
4119 /* XXX: this is not correct to modify 'ad' at this point, but
4120 the syntax is not clear */
4121 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
4122 parse_attribute(ad
);
4123 type_decl(&type1
, ad
, v
, td
);
4126 /* type identifier */
4127 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
4131 if (!(td
& TYPE_ABSTRACT
))
4132 expect("identifier");
4136 storage
= type
->t
& VT_STORAGE
;
4137 type
->t
&= ~VT_STORAGE
;
4138 post_type(type
, ad
, storage
);
4140 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
4141 parse_attribute(ad
);
4145 /* append type at the end of type1 */
4158 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
4159 ST_FUNC
int lvalue_type(int t
)
4164 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
4166 else if (bt
== VT_SHORT
)
4170 if (t
& VT_UNSIGNED
)
4171 r
|= VT_LVAL_UNSIGNED
;
4175 /* indirection with full error checking and bound check */
4176 ST_FUNC
void indir(void)
4178 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
4179 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
4183 if (vtop
->r
& VT_LVAL
)
4185 vtop
->type
= *pointed_type(&vtop
->type
);
4186 /* Arrays and functions are never lvalues */
4187 if (!(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_VLA
)
4188 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
4189 vtop
->r
|= lvalue_type(vtop
->type
.t
);
4190 /* if bound checking, the referenced pointer must be checked */
4191 #ifdef CONFIG_TCC_BCHECK
4192 if (tcc_state
->do_bounds_check
)
4193 vtop
->r
|= VT_MUSTBOUND
;
4198 /* pass a parameter to a function and do type checking and casting */
4199 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
4204 func_type
= func
->c
;
4205 if (func_type
== FUNC_OLD
||
4206 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
4207 /* default casting : only need to convert float to double */
4208 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
4211 } else if (vtop
->type
.t
& VT_BITFIELD
) {
4212 type
.t
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
4213 type
.ref
= vtop
->type
.ref
;
4216 } else if (arg
== NULL
) {
4217 tcc_error("too many arguments to function");
4220 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
4221 gen_assign_cast(&type
);
4225 /* parse an expression of the form '(type)' or '(expr)' and return its
4227 static void parse_expr_type(CType
*type
)
4233 if (parse_btype(type
, &ad
)) {
4234 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
4241 static void parse_type(CType
*type
)
4246 if (!parse_btype(type
, &ad
)) {
4249 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
4252 static void vpush_tokc(int t
)
4257 vsetc(&type
, VT_CONST
, &tokc
);
4260 ST_FUNC
void unary(void)
4262 int n
, t
, align
, size
, r
, sizeof_caller
;
4267 sizeof_caller
= in_sizeof
;
4269 /* XXX: GCC 2.95.3 does not generate a table although it should be
4283 vpush_tokc(VT_INT
| VT_UNSIGNED
);
4287 vpush_tokc(VT_LLONG
);
4291 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
4295 vpush_tokc(VT_FLOAT
);
4299 vpush_tokc(VT_DOUBLE
);
4303 vpush_tokc(VT_LDOUBLE
);
4306 case TOK___FUNCTION__
:
4308 goto tok_identifier
;
4314 /* special function name identifier */
4315 len
= strlen(funcname
) + 1;
4316 /* generate char[len] type */
4321 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
4322 ptr
= section_ptr_add(data_section
, len
);
4323 memcpy(ptr
, funcname
, len
);
4328 #ifdef TCC_TARGET_PE
4329 t
= VT_SHORT
| VT_UNSIGNED
;
4335 /* string parsing */
4338 if (tcc_state
->warn_write_strings
)
4343 memset(&ad
, 0, sizeof(AttributeDef
));
4344 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, 0);
4349 if (parse_btype(&type
, &ad
)) {
4350 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
4352 /* check ISOC99 compound literal */
4354 /* data is allocated locally by default */
4359 /* all except arrays are lvalues */
4360 if (!(type
.t
& VT_ARRAY
))
4361 r
|= lvalue_type(type
.t
);
4362 memset(&ad
, 0, sizeof(AttributeDef
));
4363 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, 0);
4365 if (sizeof_caller
) {
4372 } else if (tok
== '{') {
4373 int saved_nocode_wanted
= nocode_wanted
;
4375 tcc_error("expected constant");
4376 /* save all registers */
4378 /* statement expression : we do not accept break/continue
4379 inside as GCC does. We do retain the nocode_wanted state,
4380 as statement expressions can't ever be entered from the
4381 outside, so any reactivation of code emission (from labels
4382 or loop heads) can be disabled again after the end of it. */
4383 block(NULL
, NULL
, 1);
4384 nocode_wanted
= saved_nocode_wanted
;
4399 /* functions names must be treated as function pointers,
4400 except for unary '&' and sizeof. Since we consider that
4401 functions are not lvalues, we only have to handle it
4402 there and in function calls. */
4403 /* arrays can also be used although they are not lvalues */
4404 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
4405 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
4407 mk_pointer(&vtop
->type
);
4413 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4415 boolean
.t
= VT_BOOL
;
4417 vtop
->c
.i
= !vtop
->c
.i
;
4418 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
4422 vseti(VT_JMP
, gvtst(1, 0));
4434 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
4435 tcc_error("pointer not accepted for unary plus");
4436 /* In order to force cast, we add zero, except for floating point
4437 where we really need an noop (otherwise -0.0 will be transformed
4439 if (!is_float(vtop
->type
.t
)) {
4450 unary_type(&type
); // Perform a in_sizeof = 0;
4451 size
= type_size(&type
, &align
);
4452 if (t
== TOK_SIZEOF
) {
4453 if (!(type
.t
& VT_VLA
)) {
4455 tcc_error("sizeof applied to an incomplete type");
4458 vla_runtime_type_size(&type
, &align
);
4463 vtop
->type
.t
|= VT_UNSIGNED
;
4466 case TOK_builtin_expect
:
4468 /* __builtin_expect is a no-op for now */
4480 case TOK_builtin_types_compatible_p
:
4489 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
4490 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
4491 vpushi(is_compatible_types(&type1
, &type2
));
4494 case TOK_builtin_choose_expr
:
4521 case TOK_builtin_constant_p
:
4528 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
4535 case TOK_builtin_frame_address
:
4536 case TOK_builtin_return_address
:
4543 if (tok
!= TOK_CINT
) {
4544 tcc_error("%s only takes positive integers",
4545 tok1
== TOK_builtin_return_address
?
4546 "__builtin_return_address" :
4547 "__builtin_frame_address");
4549 level
= (uint32_t)tokc
.i
;
4554 vset(&type
, VT_LOCAL
, 0); /* local frame */
4556 mk_pointer(&vtop
->type
);
4557 indir(); /* -> parent frame */
4559 if (tok1
== TOK_builtin_return_address
) {
4560 // assume return address is just above frame pointer on stack
4563 mk_pointer(&vtop
->type
);
4568 #ifdef TCC_TARGET_X86_64
4569 #ifdef TCC_TARGET_PE
4570 case TOK_builtin_va_start
:
4578 if ((vtop
->r
& VT_VALMASK
) != VT_LOCAL
)
4579 tcc_error("__builtin_va_start expects a local variable");
4580 vtop
->r
&= ~(VT_LVAL
| VT_REF
);
4581 vtop
->type
= char_pointer_type
;
4587 case TOK_builtin_va_arg_types
:
4594 vpushi(classify_x86_64_va_arg(&type
));
4600 #ifdef TCC_TARGET_ARM64
4601 case TOK___va_start
: {
4611 vtop
->type
.t
= VT_VOID
;
4614 case TOK___va_arg
: {
4627 case TOK___arm64_clear_cache
: {
4636 vtop
->type
.t
= VT_VOID
;
4640 /* pre operations */
4651 t
= vtop
->type
.t
& VT_BTYPE
;
4653 /* In IEEE negate(x) isn't subtract(0,x), but rather
4657 vtop
->c
.f
= -1.0 * 0.0;
4658 else if (t
== VT_DOUBLE
)
4659 vtop
->c
.d
= -1.0 * 0.0;
4661 vtop
->c
.ld
= -1.0 * 0.0;
4669 goto tok_identifier
;
4671 /* allow to take the address of a label */
4672 if (tok
< TOK_UIDENT
)
4673 expect("label identifier");
4674 s
= label_find(tok
);
4676 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4678 if (s
->r
== LABEL_DECLARED
)
4679 s
->r
= LABEL_FORWARD
;
4682 s
->type
.t
= VT_VOID
;
4683 mk_pointer(&s
->type
);
4684 s
->type
.t
|= VT_STATIC
;
4686 vpushsym(&s
->type
, s
);
4690 // special qnan , snan and infinity values
4692 vpush64(VT_DOUBLE
, 0x7ff8000000000000ULL
);
4696 vpush64(VT_DOUBLE
, 0x7ff0000000000001ULL
);
4700 vpush64(VT_DOUBLE
, 0x7ff0000000000000ULL
);
4709 expect("identifier");
4712 const char *name
= get_tok_str(t
, NULL
);
4714 tcc_error("'%s' undeclared", name
);
4715 /* for simple function calls, we tolerate undeclared
4716 external reference to int() function */
4717 if (tcc_state
->warn_implicit_function_declaration
4718 #ifdef TCC_TARGET_PE
4719 /* people must be warned about using undeclared WINAPI functions
4720 (which usually start with uppercase letter) */
4721 || (name
[0] >= 'A' && name
[0] <= 'Z')
4724 tcc_warning("implicit declaration of function '%s'", name
);
4725 s
= external_global_sym(t
, &func_old_type
, 0);
4727 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
4728 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
4729 /* if referencing an inline function, then we generate a
4730 symbol to it if not already done. It will have the
4731 effect to generate code for it at the end of the
4732 compilation unit. Inline function as always
4733 generated in the text section. */
4734 if (!s
->c
&& !nocode_wanted
)
4735 put_extern_sym(s
, text_section
, 0, 0);
4736 r
= VT_SYM
| VT_CONST
;
4739 /* A symbol that has a register is a local register variable,
4740 which starts out as VT_LOCAL value. */
4741 if ((r
& VT_VALMASK
) < VT_CONST
)
4742 r
= (r
& ~VT_VALMASK
) | VT_LOCAL
;
4744 vset(&s
->type
, r
, s
->c
);
4745 /* Point to s as backpointer (even without r&VT_SYM).
4746 Will be used by at least the x86 inline asm parser for
4749 if (vtop
->r
& VT_SYM
) {
4755 /* post operations */
4757 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
4760 } else if (tok
== '.' || tok
== TOK_ARROW
|| tok
== TOK_CDOUBLE
) {
4763 if (tok
== TOK_ARROW
)
4765 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
4768 /* expect pointer on structure */
4769 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
4770 expect("struct or union");
4771 if (tok
== TOK_CDOUBLE
)
4772 expect("field name");
4774 if (tok
== TOK_CINT
|| tok
== TOK_CUINT
)
4775 expect("field name");
4776 s
= find_field(&vtop
->type
, tok
);
4778 tcc_error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, &tokc
));
4779 /* add field offset to pointer */
4780 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
4783 /* change type to field type, and set to lvalue */
4784 vtop
->type
= s
->type
;
4785 vtop
->type
.t
|= qualifiers
;
4786 /* an array is never an lvalue */
4787 if (!(vtop
->type
.t
& VT_ARRAY
)) {
4788 vtop
->r
|= lvalue_type(vtop
->type
.t
);
4789 #ifdef CONFIG_TCC_BCHECK
4790 /* if bound checking, the referenced pointer must be checked */
4791 if (tcc_state
->do_bounds_check
&& (vtop
->r
& VT_VALMASK
) != VT_LOCAL
)
4792 vtop
->r
|= VT_MUSTBOUND
;
4796 } else if (tok
== '[') {
4802 } else if (tok
== '(') {
4805 int nb_args
, ret_nregs
, ret_align
, regsize
, variadic
;
4808 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
4809 /* pointer test (no array accepted) */
4810 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
4811 vtop
->type
= *pointed_type(&vtop
->type
);
4812 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
4816 expect("function pointer");
4819 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
4821 /* get return type */
4824 sa
= s
->next
; /* first parameter */
4827 /* compute first implicit argument if a structure is returned */
4828 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
4829 variadic
= (s
->c
== FUNC_ELLIPSIS
);
4830 ret_nregs
= gfunc_sret(&s
->type
, variadic
, &ret
.type
,
4831 &ret_align
, ®size
);
4833 /* get some space for the returned structure */
4834 size
= type_size(&s
->type
, &align
);
4835 #ifdef TCC_TARGET_ARM64
4836 /* On arm64, a small struct is return in registers.
4837 It is much easier to write it to memory if we know
4838 that we are allowed to write some extra bytes, so
4839 round the allocated space up to a power of 2: */
4841 while (size
& (size
- 1))
4842 size
= (size
| (size
- 1)) + 1;
4844 loc
= (loc
- size
) & -align
;
4846 ret
.r
= VT_LOCAL
| VT_LVAL
;
4847 /* pass it as 'int' to avoid structure arg passing
4849 vseti(VT_LOCAL
, loc
);
4859 /* return in register */
4860 if (is_float(ret
.type
.t
)) {
4861 ret
.r
= reg_fret(ret
.type
.t
);
4862 #ifdef TCC_TARGET_X86_64
4863 if ((ret
.type
.t
& VT_BTYPE
) == VT_QFLOAT
)
4867 #ifndef TCC_TARGET_ARM64
4868 #ifdef TCC_TARGET_X86_64
4869 if ((ret
.type
.t
& VT_BTYPE
) == VT_QLONG
)
4871 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
4882 gfunc_param_typed(s
, sa
);
4892 tcc_error("too few arguments to function");
4894 gfunc_call(nb_args
);
4897 for (r
= ret
.r
+ ret_nregs
+ !ret_nregs
; r
-- > ret
.r
;) {
4898 vsetc(&ret
.type
, r
, &ret
.c
);
4899 vtop
->r2
= ret
.r2
; /* Loop only happens when r2 is VT_CONST */
4902 /* handle packed struct return */
4903 if (((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) && ret_nregs
) {
4906 size
= type_size(&s
->type
, &align
);
4907 /* We're writing whole regs often, make sure there's enough
4908 space. Assume register size is power of 2. */
4909 if (regsize
> align
)
4911 loc
= (loc
- size
) & -align
;
4915 vset(&ret
.type
, VT_LOCAL
| VT_LVAL
, addr
+ offset
);
4919 if (--ret_nregs
== 0)
4923 vset(&s
->type
, VT_LOCAL
| VT_LVAL
, addr
);
4931 ST_FUNC
void expr_prod(void)
4936 while (tok
== '*' || tok
== '/' || tok
== '%') {
4944 ST_FUNC
void expr_sum(void)
4949 while (tok
== '+' || tok
== '-') {
4957 static void expr_shift(void)
4962 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
4970 static void expr_cmp(void)
4975 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
4976 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
4984 static void expr_cmpeq(void)
4989 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
4997 static void expr_and(void)
5000 while (tok
== '&') {
5007 static void expr_xor(void)
5010 while (tok
== '^') {
5017 static void expr_or(void)
5020 while (tok
== '|') {
5027 /* XXX: fix this mess */
5028 static void expr_land_const(void)
5031 while (tok
== TOK_LAND
) {
5037 static void expr_lor_const(void)
5040 while (tok
== TOK_LOR
) {
5047 static void expr_land(void)
5050 if (tok
== TOK_LAND
) {
5053 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
5061 while (tok
== TOK_LAND
) {
5069 gen_cast(&int_type
);
5077 if (tok
!= TOK_LAND
) {
5090 static void expr_lor(void)
5093 if (tok
== TOK_LOR
) {
5096 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
5104 while (tok
== TOK_LOR
) {
5112 gen_cast(&int_type
);
5120 if (tok
!= TOK_LOR
) {
5133 /* Assuming vtop is a value used in a conditional context
5134 (i.e. compared with zero) return 0 if it's false, 1 if
5135 true and -1 if it can't be statically determined. */
5136 static int condition_3way(void)
5139 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
&&
5140 (!(vtop
->r
& VT_SYM
) ||
5141 !(vtop
->sym
->type
.t
& VT_WEAK
))) {
5143 boolean
.t
= VT_BOOL
;
5152 static void expr_cond(void)
5154 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
, islv
, c
, g
;
5156 CType type
, type1
, type2
;
5161 c
= condition_3way();
5162 g
= (tok
== ':' && gnu_ext
);
5164 /* needed to avoid having different registers saved in
5166 if (is_float(vtop
->type
.t
)) {
5168 #ifdef TCC_TARGET_X86_64
5169 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
5194 sv
= *vtop
; /* save value to handle it later */
5195 vtop
--; /* no vpop so that FP stack is not flushed */
5213 bt1
= t1
& VT_BTYPE
;
5215 bt2
= t2
& VT_BTYPE
;
5216 /* cast operands to correct type according to ISOC rules */
5217 if (is_float(bt1
) || is_float(bt2
)) {
5218 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
5219 type
.t
= VT_LDOUBLE
;
5221 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
5226 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
5227 /* cast to biggest op */
5229 /* convert to unsigned if it does not fit in a long long */
5230 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
5231 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
5232 type
.t
|= VT_UNSIGNED
;
5233 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
5234 /* If one is a null ptr constant the result type
5236 if (is_null_pointer (vtop
))
5238 else if (is_null_pointer (&sv
))
5240 /* XXX: test pointer compatibility, C99 has more elaborate
5244 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
5245 /* XXX: test function pointer compatibility */
5246 type
= bt1
== VT_FUNC
? type1
: type2
;
5247 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
5248 /* XXX: test structure compatibility */
5249 type
= bt1
== VT_STRUCT
? type1
: type2
;
5250 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
5251 /* NOTE: as an extension, we accept void on only one side */
5254 /* integer operations */
5256 /* convert to unsigned if it does not fit in an integer */
5257 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
5258 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
5259 type
.t
|= VT_UNSIGNED
;
5261 /* keep structs lvalue by transforming `(expr ? a : b)` to `*(expr ? &a : &b)` so
5262 that `(expr ? a : b).mem` does not error with "lvalue expected" */
5263 islv
= (vtop
->r
& VT_LVAL
) && (sv
.r
& VT_LVAL
) && VT_STRUCT
== (type
.t
& VT_BTYPE
);
5266 /* now we convert second operand */
5270 mk_pointer(&vtop
->type
);
5272 } else if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
5277 if (is_float(type
.t
)) {
5279 #ifdef TCC_TARGET_X86_64
5280 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
5284 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
5285 /* for long longs, we use fixed registers to avoid having
5286 to handle a complicated move */
5297 /* this is horrible, but we must also convert first
5303 mk_pointer(&vtop
->type
);
5305 } else if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
5311 move_reg(r2
, r1
, type
.t
);
5321 static void expr_eq(void)
5327 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
5328 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
5329 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
5344 ST_FUNC
void gexpr(void)
5355 /* parse an expression and return its type without any side effect. */
5356 static void expr_type(CType
*type
)
5366 /* parse a unary expression and return its type without any side
5368 static void unary_type(CType
*type
)
5377 /* parse a constant expression and return value in vtop. */
5378 static void expr_const1(void)
5385 /* parse an integer constant and return its value. */
5386 static inline int64_t expr_const64(void)
5390 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
5391 expect("constant expression");
5397 /* parse an integer constant and return its value.
5398 Complain if it doesn't fit 32bit (signed or unsigned). */
5399 ST_FUNC
int expr_const(void)
5402 int64_t wc
= expr_const64();
5404 if (c
!= wc
&& (unsigned)c
!= wc
)
5405 tcc_error("constant exceeds 32 bit");
5409 /* return the label token if current token is a label, otherwise
5411 static int is_label(void)
5415 /* fast test first */
5416 if (tok
< TOK_UIDENT
)
5418 /* no need to save tokc because tok is an identifier */
5425 unget_tok(last_tok
);
5430 static void label_or_decl(int l
)
5434 /* fast test first */
5435 if (tok
>= TOK_UIDENT
)
5437 /* no need to save tokc because tok is an identifier */
5441 unget_tok(last_tok
);
5444 unget_tok(last_tok
);
5449 #ifndef TCC_TARGET_ARM64
5450 static void gfunc_return(CType
*func_type
)
5452 if ((func_type
->t
& VT_BTYPE
) == VT_STRUCT
) {
5453 CType type
, ret_type
;
5454 int ret_align
, ret_nregs
, regsize
;
5455 ret_nregs
= gfunc_sret(func_type
, func_var
, &ret_type
,
5456 &ret_align
, ®size
);
5457 if (0 == ret_nregs
) {
5458 /* if returning structure, must copy it to implicit
5459 first pointer arg location */
5462 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
5465 /* copy structure value to pointer */
5468 /* returning structure packed into registers */
5469 int r
, size
, addr
, align
;
5470 size
= type_size(func_type
,&align
);
5471 if ((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) ||
5472 (vtop
->c
.i
& (ret_align
-1)))
5473 && (align
& (ret_align
-1))) {
5474 loc
= (loc
- size
) & -ret_align
;
5477 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
5481 vset(&ret_type
, VT_LOCAL
| VT_LVAL
, addr
);
5483 vtop
->type
= ret_type
;
5484 if (is_float(ret_type
.t
))
5485 r
= rc_fret(ret_type
.t
);
5496 if (--ret_nregs
== 0)
5498 /* We assume that when a structure is returned in multiple
5499 registers, their classes are consecutive values of the
5502 vtop
->c
.i
+= regsize
;
5506 } else if (is_float(func_type
->t
)) {
5507 gv(rc_fret(func_type
->t
));
5511 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
5515 static int case_cmp(const void *pa
, const void *pb
)
5517 int64_t a
= (*(struct case_t
**) pa
)->v1
;
5518 int64_t b
= (*(struct case_t
**) pb
)->v1
;
5519 return a
< b
? -1 : a
> b
;
5522 static void gcase(struct case_t
**base
, int len
, int *bsym
)
5526 int ll
= (vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
;
5544 gtst_addr(0, p
->sym
); /* v1 <= x <= v2 */
5546 gcase(base
, len
/2, bsym
);
5547 if (cur_switch
->def_sym
)
5548 gjmp_addr(cur_switch
->def_sym
);
5550 *bsym
= gjmp(*bsym
);
5554 base
+= e
; len
-= e
;
5564 if (p
->v1
== p
->v2
) {
5566 gtst_addr(0, p
->sym
);
5576 gtst_addr(0, p
->sym
);
5582 static void block(int *bsym
, int *csym
, int is_expr
)
5584 int a
, b
, c
, d
, cond
;
5587 /* generate line number info */
5588 if (tcc_state
->do_debug
&&
5589 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
5590 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
5592 last_line_num
= file
->line_num
;
5596 /* default return value is (void) */
5598 vtop
->type
.t
= VT_VOID
;
5601 if (tok
== TOK_IF
) {
5603 int saved_nocode_wanted
= nocode_wanted
;
5608 cond
= condition_3way();
5614 nocode_wanted
|= 0x20000000;
5615 block(bsym
, csym
, 0);
5617 nocode_wanted
= saved_nocode_wanted
;
5619 if (c
== TOK_ELSE
) {
5624 nocode_wanted
|= 0x20000000;
5625 block(bsym
, csym
, 0);
5626 gsym(d
); /* patch else jmp */
5628 nocode_wanted
= saved_nocode_wanted
;
5631 } else if (tok
== TOK_WHILE
) {
5632 int saved_nocode_wanted
;
5633 nocode_wanted
&= ~0x20000000;
5643 saved_nocode_wanted
= nocode_wanted
;
5645 nocode_wanted
= saved_nocode_wanted
;
5650 } else if (tok
== '{') {
5652 int block_vla_sp_loc
= vla_sp_loc
, saved_vlas_in_scope
= vlas_in_scope
;
5655 /* record local declaration stack position */
5657 llabel
= local_label_stack
;
5660 /* handle local labels declarations */
5661 if (tok
== TOK_LABEL
) {
5664 if (tok
< TOK_UIDENT
)
5665 expect("label identifier");
5666 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
5676 while (tok
!= '}') {
5677 label_or_decl(VT_LOCAL
);
5681 block(bsym
, csym
, is_expr
);
5684 /* pop locally defined labels */
5685 label_pop(&local_label_stack
, llabel
);
5686 /* pop locally defined symbols */
5688 /* In the is_expr case (a statement expression is finished here),
5689 vtop might refer to symbols on the local_stack. Either via the
5690 type or via vtop->sym. We can't pop those nor any that in turn
5691 might be referred to. To make it easier we don't roll back
5692 any symbols in that case; some upper level call to block() will
5693 do that. We do have to remove such symbols from the lookup
5694 tables, though. sym_pop will do that. */
5695 sym_pop(&local_stack
, s
, is_expr
);
5697 /* Pop VLA frames and restore stack pointer if required */
5698 if (vlas_in_scope
> saved_vlas_in_scope
) {
5699 vla_sp_loc
= saved_vlas_in_scope
? block_vla_sp_loc
: vla_sp_root_loc
;
5702 vlas_in_scope
= saved_vlas_in_scope
;
5705 } else if (tok
== TOK_RETURN
) {
5709 gen_assign_cast(&func_vt
);
5710 gfunc_return(&func_vt
);
5713 /* jump unless last stmt in top-level block */
5714 if (tok
!= '}' || local_scope
!= 1)
5716 nocode_wanted
|= 0x20000000;
5717 } else if (tok
== TOK_BREAK
) {
5720 tcc_error("cannot break");
5721 *bsym
= gjmp(*bsym
);
5724 nocode_wanted
|= 0x20000000;
5725 } else if (tok
== TOK_CONTINUE
) {
5728 tcc_error("cannot continue");
5729 vla_sp_restore_root();
5730 *csym
= gjmp(*csym
);
5733 } else if (tok
== TOK_FOR
) {
5735 int saved_nocode_wanted
;
5736 nocode_wanted
&= ~0x20000000;
5742 /* c99 for-loop init decl? */
5743 if (!decl0(VT_LOCAL
, 1)) {
5744 /* no, regular for-loop init expr */
5770 saved_nocode_wanted
= nocode_wanted
;
5772 nocode_wanted
= saved_nocode_wanted
;
5777 sym_pop(&local_stack
, s
, 0);
5780 if (tok
== TOK_DO
) {
5781 int saved_nocode_wanted
;
5782 nocode_wanted
&= ~0x20000000;
5788 saved_nocode_wanted
= nocode_wanted
;
5796 nocode_wanted
= saved_nocode_wanted
;
5801 if (tok
== TOK_SWITCH
) {
5802 struct switch_t
*saved
, sw
;
5803 int saved_nocode_wanted
= nocode_wanted
;
5809 switchval
= *vtop
--;
5811 b
= gjmp(0); /* jump to first case */
5812 sw
.p
= NULL
; sw
.n
= 0; sw
.def_sym
= 0;
5816 nocode_wanted
= saved_nocode_wanted
;
5817 a
= gjmp(a
); /* add implicit break */
5820 qsort(sw
.p
, sw
.n
, sizeof(void*), case_cmp
);
5821 for (b
= 1; b
< sw
.n
; b
++)
5822 if (sw
.p
[b
- 1]->v2
>= sw
.p
[b
]->v1
)
5823 tcc_error("duplicate case value");
5824 /* Our switch table sorting is signed, so the compared
5825 value needs to be as well when it's 64bit. */
5826 if ((switchval
.type
.t
& VT_BTYPE
) == VT_LLONG
)
5827 switchval
.type
.t
&= ~VT_UNSIGNED
;
5829 gcase(sw
.p
, sw
.n
, &a
);
5832 gjmp_addr(sw
.def_sym
);
5833 dynarray_reset(&sw
.p
, &sw
.n
);
5838 if (tok
== TOK_CASE
) {
5839 struct case_t
*cr
= tcc_malloc(sizeof(struct case_t
));
5842 nocode_wanted
&= ~0x20000000;
5844 cr
->v1
= cr
->v2
= expr_const64();
5845 if (gnu_ext
&& tok
== TOK_DOTS
) {
5847 cr
->v2
= expr_const64();
5848 if (cr
->v2
< cr
->v1
)
5849 tcc_warning("empty case range");
5852 dynarray_add((void***) &cur_switch
->p
, &cur_switch
->n
, cr
);
5855 goto block_after_label
;
5857 if (tok
== TOK_DEFAULT
) {
5862 if (cur_switch
->def_sym
)
5863 tcc_error("too many 'default'");
5864 cur_switch
->def_sym
= ind
;
5866 goto block_after_label
;
5868 if (tok
== TOK_GOTO
) {
5870 if (tok
== '*' && gnu_ext
) {
5874 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
5877 } else if (tok
>= TOK_UIDENT
) {
5878 s
= label_find(tok
);
5879 /* put forward definition if needed */
5881 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
5883 if (s
->r
== LABEL_DECLARED
)
5884 s
->r
= LABEL_FORWARD
;
5886 vla_sp_restore_root();
5887 if (s
->r
& LABEL_FORWARD
)
5888 s
->jnext
= gjmp(s
->jnext
);
5890 gjmp_addr(s
->jnext
);
5893 expect("label identifier");
5896 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
5904 if (s
->r
== LABEL_DEFINED
)
5905 tcc_error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
5907 s
->r
= LABEL_DEFINED
;
5909 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
5913 /* we accept this, but it is a mistake */
5915 nocode_wanted
&= ~0x20000000;
5917 tcc_warning("deprecated use of label at end of compound statement");
5921 block(bsym
, csym
, is_expr
);
5924 /* expression case */
5939 #define EXPR_CONST 1
5942 static void parse_init_elem(int expr_type
)
5944 int saved_global_expr
;
5947 /* compound literals must be allocated globally in this case */
5948 saved_global_expr
= global_expr
;
5951 global_expr
= saved_global_expr
;
5952 /* NOTE: symbols are accepted */
5953 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
5954 tcc_error("initializer element is not constant");
5962 /* t is the array or struct type. c is the array or struct
5963 address. cur_field is the pointer to the current
5964 value, for arrays the 'c' member contains the current start
5965 index and the 'r' contains the end index (in case of range init).
5966 'size_only' is true if only size info is needed (only used
5968 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
5969 Sym
**cur_field
, int size_only
)
5972 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
5978 if (gnu_ext
&& (l
= is_label()) != 0)
5980 while (tok
== '[' || tok
== '.') {
5982 if (!(type
->t
& VT_ARRAY
))
5983 expect("array type");
5986 index
= expr_const();
5987 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
5988 tcc_error("invalid index");
5989 if (tok
== TOK_DOTS
&& gnu_ext
) {
5991 index_last
= expr_const();
5992 if (index_last
< 0 ||
5993 (s
->c
>= 0 && index_last
>= s
->c
) ||
5995 tcc_error("invalid index");
6001 (*cur_field
)->c
= index
;
6002 (*cur_field
)->r
= index_last
;
6004 type
= pointed_type(type
);
6005 elem_size
= type_size(type
, &align
);
6006 c
+= index
* elem_size
;
6007 /* NOTE: we only support ranges for last designator */
6008 nb_elems
= index_last
- index
+ 1;
6009 if (nb_elems
!= 1) {
6018 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
6019 expect("struct/union type");
6020 f
= find_field(type
, l
);
6025 /* XXX: fix this mess by using explicit storage field */
6027 type1
.t
|= (type
->t
& ~VT_TYPE
);
6041 if (type
->t
& VT_ARRAY
) {
6042 index
= (*cur_field
)->c
;
6043 if (type
->ref
->c
>= 0 && index
>= type
->ref
->c
)
6044 tcc_error("index too large");
6045 type
= pointed_type(type
);
6046 c
+= index
* type_size(type
, &align
);
6050 tcc_error("too many field init");
6051 /* XXX: fix this mess by using explicit storage field */
6053 type1
.t
|= (type
->t
& ~VT_TYPE
);
6058 decl_initializer(type
, sec
, c
, 0, size_only
);
6060 /* XXX: make it more general */
6061 if (!size_only
&& nb_elems
> 1) {
6062 unsigned long c_end
;
6067 vset(type
, VT_LOCAL
|VT_LVAL
, c
);
6068 for (i
= 1; i
< nb_elems
; i
++) {
6069 vset(type
, VT_LOCAL
|VT_LVAL
, c
+ elem_size
* i
);
6075 c_end
= c
+ nb_elems
* elem_size
;
6076 if (c_end
> sec
->data_allocated
)
6077 section_realloc(sec
, c_end
);
6078 src
= sec
->data
+ c
;
6080 for(i
= 1; i
< nb_elems
; i
++) {
6082 memcpy(dst
, src
, elem_size
);
6088 /* store a value or an expression directly in global data or in local array */
6089 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
)
6091 int bt
, bit_pos
, bit_size
;
6093 unsigned long long bit_mask
;
6097 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
6101 /* XXX: not portable */
6102 /* XXX: generate error if incorrect relocation */
6103 gen_assign_cast(&dtype
);
6104 bt
= type
->t
& VT_BTYPE
;
6105 size
= type_size(type
, &align
);
6106 if (c
+ size
> sec
->data_allocated
) {
6107 section_realloc(sec
, c
+ size
);
6109 ptr
= sec
->data
+ c
;
6110 /* XXX: make code faster ? */
6111 if (!(type
->t
& VT_BITFIELD
)) {
6113 bit_size
= PTR_SIZE
* 8;
6116 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
6117 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
6118 bit_mask
= (1LL << bit_size
) - 1;
6120 if ((vtop
->r
& (VT_SYM
|VT_CONST
)) == (VT_SYM
|VT_CONST
) &&
6121 vtop
->sym
->v
>= SYM_FIRST_ANOM
&&
6122 /* XXX This rejects compount literals like
6123 '(void *){ptr}'. The problem is that '&sym' is
6124 represented the same way, which would be ruled out
6125 by the SYM_FIRST_ANOM check above, but also '"string"'
6126 in 'char *p = "string"' is represented the same
6127 with the type being VT_PTR and the symbol being an
6128 anonymous one. That is, there's no difference in vtop
6129 between '(void *){x}' and '&(void *){x}'. Ignore
6130 pointer typed entities here. Hopefully no real code
6131 will every use compound literals with scalar type. */
6132 (vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
6133 /* These come from compound literals, memcpy stuff over. */
6137 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[vtop
->sym
->c
];
6138 ssec
= tcc_state
->sections
[esym
->st_shndx
];
6139 memmove (ptr
, ssec
->data
+ esym
->st_value
, size
);
6141 /* We need to copy over all memory contents, and that
6142 includes relocations. Use the fact that relocs are
6143 created it order, so look from the end of relocs
6144 until we hit one before the copied region. */
6145 int num_relocs
= ssec
->reloc
->data_offset
/ sizeof(*rel
);
6146 rel
= (ElfW_Rel
*)(ssec
->reloc
->data
+ ssec
->reloc
->data_offset
);
6147 while (num_relocs
--) {
6149 if (rel
->r_offset
>= esym
->st_value
+ size
)
6151 if (rel
->r_offset
< esym
->st_value
)
6153 /* Note: if the same fields are initialized multiple
6154 times (possible with designators) then we possibly
6155 add multiple relocations for the same offset here.
6156 That would lead to wrong code, the last reloc needs
6157 to win. We clean this up later after the whole
6158 initializer is parsed. */
6159 put_elf_reloca(symtab_section
, sec
,
6160 c
+ rel
->r_offset
- esym
->st_value
,
6161 ELFW(R_TYPE
)(rel
->r_info
),
6162 ELFW(R_SYM
)(rel
->r_info
),
6163 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
6172 if ((vtop
->r
& VT_SYM
) &&
6178 (bt
== VT_LLONG
&& bit_size
!= 64) ||
6182 (bt
== VT_INT
&& bit_size
!= 32)
6185 tcc_error("initializer element is not computable at load time");
6187 /* XXX: when cross-compiling we assume that each type has the
6188 same representation on host and target, which is likely to
6189 be wrong in the case of long double */
6191 vtop
->c
.i
= (vtop
->c
.i
!= 0);
6193 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
6196 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
6199 *(double *)ptr
= vtop
->c
.d
;
6202 if (sizeof(long double) == LDOUBLE_SIZE
)
6203 *(long double *)ptr
= vtop
->c
.ld
;
6204 else if (sizeof(double) == LDOUBLE_SIZE
)
6205 *(double *)ptr
= vtop
->c
.ld
;
6207 tcc_error("can't cross compile long double constants");
6211 *(long long *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
6218 addr_t val
= (vtop
->c
.i
& bit_mask
) << bit_pos
;
6219 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
6220 if (vtop
->r
& VT_SYM
)
6221 greloca(sec
, vtop
->sym
, c
, R_DATA_PTR
, val
);
6223 *(addr_t
*)ptr
|= val
;
6225 if (vtop
->r
& VT_SYM
)
6226 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
6227 *(addr_t
*)ptr
|= val
;
6233 int val
= (vtop
->c
.i
& bit_mask
) << bit_pos
;
6234 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
6235 if (vtop
->r
& VT_SYM
)
6236 greloca(sec
, vtop
->sym
, c
, R_DATA_PTR
, val
);
6240 if (vtop
->r
& VT_SYM
)
6241 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
6250 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
6257 /* put zeros for variable based init */
6258 static void init_putz(Section
*sec
, unsigned long c
, int size
)
6261 /* nothing to do because globals are already set to zero */
6263 vpush_global_sym(&func_old_type
, TOK_memset
);
6265 #ifdef TCC_TARGET_ARM
6276 /* 't' contains the type and storage info. 'c' is the offset of the
6277 object in section 'sec'. If 'sec' is NULL, it means stack based
6278 allocation. 'first' is true if array '{' must be read (multi
6279 dimension implicit array init handling). 'size_only' is true if
6280 size only evaluation is wanted (only for arrays). */
6281 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
6282 int first
, int size_only
)
6284 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, parlevel1
, i
;
6291 /* If we currently are at an '}' or ',' we have read an initializer
6292 element in one of our callers, and not yet consumed it. */
6293 have_elem
= tok
== '}' || tok
== ',';
6294 if (!have_elem
&& tok
!= '{' &&
6295 /* In case of strings we have special handling for arrays, so
6296 don't consume them as initializer value (which would commit them
6297 to some anonymous symbol). */
6298 tok
!= TOK_LSTR
&& tok
!= TOK_STR
&&
6300 parse_init_elem(!sec
? EXPR_ANY
: EXPR_CONST
);
6305 !(type
->t
& VT_ARRAY
) &&
6306 /* Use i_c_parameter_t, to strip toplevel qualifiers.
6307 The source type might have VT_CONSTANT set, which is
6308 of course assignable to non-const elements. */
6309 is_compatible_parameter_types(type
, &vtop
->type
)) {
6310 init_putv(type
, sec
, c
);
6311 } else if (type
->t
& VT_ARRAY
) {
6315 t1
= pointed_type(type
);
6316 size1
= type_size(t1
, &align1
);
6319 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
6322 tcc_error("character array initializer must be a literal,"
6323 " optionally enclosed in braces");
6328 /* only parse strings here if correct type (otherwise: handle
6329 them as ((w)char *) expressions */
6330 if ((tok
== TOK_LSTR
&&
6331 #ifdef TCC_TARGET_PE
6332 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
6334 (t1
->t
& VT_BTYPE
) == VT_INT
6336 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
6337 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
6340 /* compute maximum number of chars wanted */
6342 cstr_len
= tokc
.str
.size
;
6344 cstr_len
= tokc
.str
.size
/ sizeof(nwchar_t
);
6347 if (n
>= 0 && nb
> (n
- array_length
))
6348 nb
= n
- array_length
;
6351 tcc_warning("initializer-string for array is too long");
6352 /* in order to go faster for common case (char
6353 string in global variable, we handle it
6355 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
6356 memcpy(sec
->data
+ c
+ array_length
, tokc
.str
.data
, nb
);
6360 ch
= ((unsigned char *)tokc
.str
.data
)[i
];
6362 ch
= ((nwchar_t
*)tokc
.str
.data
)[i
];
6364 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
);
6371 /* only add trailing zero if enough storage (no
6372 warning in this case since it is standard) */
6373 if (n
< 0 || array_length
< n
) {
6376 init_putv(t1
, sec
, c
+ (array_length
* size1
));
6386 while (tok
!= '}' || have_elem
) {
6387 decl_designator(type
, sec
, c
, &f
, size_only
);
6390 /* must put zero in holes (note that doing it that way
6391 ensures that it even works with designators) */
6392 if (!size_only
&& array_length
< index
) {
6393 init_putz(sec
, c
+ array_length
* size1
,
6394 (index
- array_length
) * size1
);
6396 if (type
->t
& VT_ARRAY
) {
6397 index
= indexsym
.c
= ++indexsym
.r
;
6399 index
= index
+ type_size(&f
->type
, &align1
);
6400 if (s
->type
.t
== TOK_UNION
)
6405 if (index
> array_length
)
6406 array_length
= index
;
6408 if (type
->t
& VT_ARRAY
) {
6409 /* special test for multi dimensional arrays (may not
6410 be strictly correct if designators are used at the
6412 if (no_oblock
&& index
>= n
)
6415 if (no_oblock
&& f
== NULL
)
6423 /* put zeros at the end */
6424 if (!size_only
&& array_length
< n
) {
6425 init_putz(sec
, c
+ array_length
* size1
,
6426 (n
- array_length
) * size1
);
6430 /* patch type size if needed, which happens only for array types */
6432 s
->c
= array_length
;
6433 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
6436 if (first
|| tok
== '{') {
6445 } else if (tok
== '{') {
6447 decl_initializer(type
, sec
, c
, first
, size_only
);
6449 } else if (size_only
) {
6450 /* If we supported only ISO C we wouldn't have to accept calling
6451 this on anything than an array size_only==1 (and even then
6452 only on the outermost level, so no recursion would be needed),
6453 because initializing a flex array member isn't supported.
6454 But GNU C supports it, so we need to recurse even into
6455 subfields of structs and arrays when size_only is set. */
6456 /* just skip expression */
6457 parlevel
= parlevel1
= 0;
6458 while ((parlevel
> 0 || parlevel1
> 0 ||
6459 (tok
!= '}' && tok
!= ',')) && tok
!= -1) {
6462 else if (tok
== ')') {
6463 if (parlevel
== 0 && parlevel1
== 0)
6467 else if (tok
== '{')
6469 else if (tok
== '}') {
6470 if (parlevel
== 0 && parlevel1
== 0)
6478 /* This should happen only when we haven't parsed
6479 the init element above for fear of committing a
6480 string constant to memory too early. */
6481 if (tok
!= TOK_STR
&& tok
!= TOK_LSTR
)
6482 expect("string constant");
6483 parse_init_elem(!sec
? EXPR_ANY
: EXPR_CONST
);
6485 init_putv(type
, sec
, c
);
6489 /* parse an initializer for type 't' if 'has_init' is non zero, and
6490 allocate space in local or global data space ('r' is either
6491 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
6492 variable 'v' of scope 'scope' is declared before initializers
6493 are parsed. If 'v' is zero, then a reference to the new object
6494 is put in the value stack. If 'has_init' is 2, a special parsing
6495 is done to handle string constants. */
6496 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
6497 int has_init
, int v
, int scope
)
6499 int size
, align
, addr
, data_offset
;
6501 ParseState saved_parse_state
= {0};
6502 TokenString
*init_str
= NULL
;
6504 Sym
*flexible_array
;
6506 flexible_array
= NULL
;
6507 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
6508 Sym
*field
= type
->ref
->next
;
6511 field
= field
->next
;
6512 if (field
->type
.t
& VT_ARRAY
&& field
->type
.ref
->c
< 0)
6513 flexible_array
= field
;
6517 size
= type_size(type
, &align
);
6518 /* If unknown size, we must evaluate it before
6519 evaluating initializers because
6520 initializers can generate global data too
6521 (e.g. string pointers or ISOC99 compound
6522 literals). It also simplifies local
6523 initializers handling */
6524 if (size
< 0 || (flexible_array
&& has_init
)) {
6526 tcc_error("unknown type size");
6527 /* get all init string */
6528 init_str
= tok_str_alloc();
6529 if (has_init
== 2) {
6530 /* only get strings */
6531 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
6532 tok_str_add_tok(init_str
);
6537 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
6539 tcc_error("unexpected end of file in initializer");
6540 tok_str_add_tok(init_str
);
6543 else if (tok
== '}') {
6553 tok_str_add(init_str
, -1);
6554 tok_str_add(init_str
, 0);
6557 save_parse_state(&saved_parse_state
);
6559 begin_macro(init_str
, 1);
6561 decl_initializer(type
, NULL
, 0, 1, 1);
6562 /* prepare second initializer parsing */
6563 macro_ptr
= init_str
->str
;
6566 /* if still unknown size, error */
6567 size
= type_size(type
, &align
);
6569 tcc_error("unknown type size");
6571 /* If there's a flex member and it was used in the initializer
6573 if (flexible_array
&&
6574 flexible_array
->type
.ref
->c
> 0)
6575 size
+= flexible_array
->type
.ref
->c
6576 * pointed_size(&flexible_array
->type
);
6577 /* take into account specified alignment if bigger */
6578 if (ad
->a
.aligned
) {
6579 int speca
= 1 << (ad
->a
.aligned
- 1);
6582 } else if (ad
->a
.packed
) {
6585 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
6587 #ifdef CONFIG_TCC_BCHECK
6588 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
6592 loc
= (loc
- size
) & -align
;
6594 #ifdef CONFIG_TCC_BCHECK
6595 /* handles bounds */
6596 /* XXX: currently, since we do only one pass, we cannot track
6597 '&' operators, so we add only arrays */
6598 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
6600 /* add padding between regions */
6602 /* then add local bound info */
6603 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(addr_t
));
6604 bounds_ptr
[0] = addr
;
6605 bounds_ptr
[1] = size
;
6609 /* local variable */
6610 #ifdef CONFIG_TCC_ASM
6611 if (ad
->asm_label
) {
6612 int reg
= asm_parse_regvar(ad
->asm_label
);
6614 r
= (r
& ~VT_VALMASK
) | reg
;
6617 sym_push(v
, type
, r
, addr
);
6619 /* push local reference */
6620 vset(type
, r
, addr
);
6626 if (v
&& scope
== VT_CONST
) {
6627 /* see if the symbol was already defined */
6630 if (!is_compatible_types(&sym
->type
, type
))
6631 tcc_error("incompatible types for redefinition of '%s'",
6632 get_tok_str(v
, NULL
));
6633 if (sym
->type
.t
& VT_EXTERN
) {
6634 /* if the variable is extern, it was not allocated */
6635 sym
->type
.t
&= ~VT_EXTERN
;
6636 /* set array size if it was omitted in extern
6638 if ((sym
->type
.t
& VT_ARRAY
) &&
6639 sym
->type
.ref
->c
< 0 &&
6641 sym
->type
.ref
->c
= type
->ref
->c
;
6643 /* we accept several definitions of the same
6644 global variable. this is tricky, because we
6645 must play with the SHN_COMMON type of the symbol */
6646 /* XXX: should check if the variable was already
6647 initialized. It is incorrect to initialized it
6649 /* no init data, we won't add more to the symbol */
6656 /* allocate symbol in corresponding section */
6661 else if (tcc_state
->nocommon
)
6665 data_offset
= sec
->data_offset
;
6666 data_offset
= (data_offset
+ align
- 1) & -align
;
6668 /* very important to increment global pointer at this time
6669 because initializers themselves can create new initializers */
6670 data_offset
+= size
;
6671 #ifdef CONFIG_TCC_BCHECK
6672 /* add padding if bound check */
6673 if (tcc_state
->do_bounds_check
)
6676 sec
->data_offset
= data_offset
;
6677 /* allocate section space to put the data */
6678 if (sec
->sh_type
!= SHT_NOBITS
&&
6679 data_offset
> sec
->data_allocated
)
6680 section_realloc(sec
, data_offset
);
6681 /* align section if needed */
6682 if (align
> sec
->sh_addralign
)
6683 sec
->sh_addralign
= align
;
6685 addr
= 0; /* avoid warning */
6689 if (scope
!= VT_CONST
|| !sym
) {
6690 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
6691 sym
->asm_label
= ad
->asm_label
;
6693 /* update symbol definition */
6695 put_extern_sym(sym
, sec
, addr
, size
);
6698 /* put a common area */
6699 put_extern_sym(sym
, NULL
, align
, size
);
6700 /* XXX: find a nicer way */
6701 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
6702 esym
->st_shndx
= SHN_COMMON
;
6705 /* push global reference */
6706 sym
= get_sym_ref(type
, sec
, addr
, size
);
6707 vpushsym(type
, sym
);
6709 /* patch symbol weakness */
6710 if (type
->t
& VT_WEAK
)
6712 apply_visibility(sym
, type
);
6713 #ifdef CONFIG_TCC_BCHECK
6714 /* handles bounds now because the symbol must be defined
6715 before for the relocation */
6716 if (tcc_state
->do_bounds_check
) {
6719 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
6720 /* then add global bound info */
6721 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(addr_t
));
6722 bounds_ptr
[0] = 0; /* relocated */
6723 bounds_ptr
[1] = size
;
6727 if (type
->t
& VT_VLA
) {
6730 /* save current stack pointer */
6731 if (vlas_in_scope
== 0) {
6732 if (vla_sp_root_loc
== -1)
6733 vla_sp_root_loc
= (loc
-= PTR_SIZE
);
6734 gen_vla_sp_save(vla_sp_root_loc
);
6737 vla_runtime_type_size(type
, &a
);
6738 gen_vla_alloc(type
, a
);
6739 gen_vla_sp_save(addr
);
6742 } else if (has_init
) {
6743 size_t oldreloc_offset
= 0;
6744 if (sec
&& sec
->reloc
)
6745 oldreloc_offset
= sec
->reloc
->data_offset
;
6746 decl_initializer(type
, sec
, addr
, 1, 0);
6747 if (sec
&& sec
->reloc
)
6748 squeeze_multi_relocs(sec
, oldreloc_offset
);
6749 /* patch flexible array member size back to -1, */
6750 /* for possible subsequent similar declarations */
6752 flexible_array
->type
.ref
->c
= -1;
6755 /* restore parse state if needed */
6758 restore_parse_state(&saved_parse_state
);
6762 static void put_func_debug(Sym
*sym
)
6767 /* XXX: we put here a dummy type */
6768 snprintf(buf
, sizeof(buf
), "%s:%c1",
6769 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
6770 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
6771 cur_text_section
, sym
->c
);
6772 /* //gr gdb wants a line at the function */
6773 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
6778 /* parse an old style function declaration list */
6779 /* XXX: check multiple parameter */
6780 static void func_decl_list(Sym
*func_sym
)
6787 /* parse each declaration */
6788 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
&&
6789 tok
!= TOK_ASM1
&& tok
!= TOK_ASM2
&& tok
!= TOK_ASM3
) {
6790 if (!parse_btype(&btype
, &ad
))
6791 expect("declaration list");
6792 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
6793 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
6795 /* we accept no variable after */
6799 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
6800 /* find parameter in function parameter list */
6803 if ((s
->v
& ~SYM_FIELD
) == v
)
6807 tcc_error("declaration for parameter '%s' but no such parameter",
6808 get_tok_str(v
, NULL
));
6810 /* check that no storage specifier except 'register' was given */
6811 if (type
.t
& VT_STORAGE
)
6812 tcc_error("storage class specified for '%s'", get_tok_str(v
, NULL
));
6813 convert_parameter_type(&type
);
6814 /* we can add the type (NOTE: it could be local to the function) */
6816 /* accept other parameters */
6827 /* parse a function defined by symbol 'sym' and generate its code in
6828 'cur_text_section' */
6829 static void gen_function(Sym
*sym
)
6832 ind
= cur_text_section
->data_offset
;
6833 /* NOTE: we patch the symbol size later */
6834 put_extern_sym(sym
, cur_text_section
, ind
, 0);
6835 funcname
= get_tok_str(sym
->v
, NULL
);
6837 /* Initialize VLA state */
6839 vla_sp_root_loc
= -1;
6840 /* put debug symbol */
6841 if (tcc_state
->do_debug
)
6842 put_func_debug(sym
);
6844 /* push a dummy symbol to enable local sym storage */
6845 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
6846 local_scope
= 1; /* for function parameters */
6847 gfunc_prolog(&sym
->type
);
6851 block(NULL
, NULL
, 0);
6855 cur_text_section
->data_offset
= ind
;
6856 label_pop(&global_label_stack
, NULL
);
6857 /* reset local stack */
6859 sym_pop(&local_stack
, NULL
, 0);
6860 /* end of function */
6861 /* patch symbol size */
6862 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
6864 /* patch symbol weakness (this definition overrules any prototype) */
6865 if (sym
->type
.t
& VT_WEAK
)
6867 apply_visibility(sym
, &sym
->type
);
6868 if (tcc_state
->do_debug
) {
6869 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
6871 /* It's better to crash than to generate wrong code */
6872 cur_text_section
= NULL
;
6873 funcname
= ""; /* for safety */
6874 func_vt
.t
= VT_VOID
; /* for safety */
6875 func_var
= 0; /* for safety */
6876 ind
= 0; /* for safety */
6881 static void gen_inline_functions(TCCState
*s
)
6884 int inline_generated
, i
, ln
;
6885 struct InlineFunc
*fn
;
6887 ln
= file
->line_num
;
6888 /* iterate while inline function are referenced */
6890 inline_generated
= 0;
6891 for (i
= 0; i
< s
->nb_inline_fns
; ++i
) {
6892 fn
= s
->inline_fns
[i
];
6894 if (sym
&& sym
->c
) {
6895 /* the function was used: generate its code and
6896 convert it to a normal function */
6899 pstrcpy(file
->filename
, sizeof file
->filename
, fn
->filename
);
6900 sym
->r
= VT_SYM
| VT_CONST
;
6901 sym
->type
.t
&= ~VT_INLINE
;
6903 begin_macro(fn
->func_str
, 1);
6905 cur_text_section
= text_section
;
6909 inline_generated
= 1;
6912 if (!inline_generated
)
6915 file
->line_num
= ln
;
6918 ST_FUNC
void free_inline_functions(TCCState
*s
)
6921 /* free tokens of unused inline functions */
6922 for (i
= 0; i
< s
->nb_inline_fns
; ++i
) {
6923 struct InlineFunc
*fn
= s
->inline_fns
[i
];
6925 tok_str_free(fn
->func_str
);
6927 dynarray_reset(&s
->inline_fns
, &s
->nb_inline_fns
);
6930 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
6931 static int decl0(int l
, int is_for_loop_init
)
6939 if (!parse_btype(&btype
, &ad
)) {
6940 if (is_for_loop_init
)
6942 /* skip redundant ';' */
6943 /* XXX: find more elegant solution */
6948 if (l
== VT_CONST
&&
6949 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
6950 /* global asm block */
6954 /* special test for old K&R protos without explicit int
6955 type. Only accepted when defining global data */
6956 if (l
== VT_LOCAL
|| tok
< TOK_UIDENT
)
6960 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
6961 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
6963 if ((btype
.t
& VT_BTYPE
) == VT_STRUCT
) {
6964 int v
= btype
.ref
->v
;
6965 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) >= SYM_FIRST_ANOM
)
6966 tcc_warning("unnamed struct/union that defines no instances");
6971 while (1) { /* iterate thru each declaration */
6973 /* If the base type itself was an array type of unspecified
6974 size (like in 'typedef int arr[]; arr x = {1};') then
6975 we will overwrite the unknown size by the real one for
6976 this decl. We need to unshare the ref symbol holding
6978 if ((type
.t
& VT_ARRAY
) && type
.ref
->c
< 0) {
6979 type
.ref
= sym_push(SYM_FIELD
, &type
.ref
->type
, 0, type
.ref
->c
);
6981 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
6985 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
6986 printf("type = '%s'\n", buf
);
6989 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
6990 if ((type
.t
& VT_STATIC
) && (l
== VT_LOCAL
)) {
6991 tcc_error("function without file scope cannot be static");
6993 /* if old style function prototype, we accept a
6996 if (sym
->c
== FUNC_OLD
)
6997 func_decl_list(sym
);
7000 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
7001 ad
.asm_label
= asm_label_instr();
7002 /* parse one last attribute list, after asm label */
7003 parse_attribute(&ad
);
7010 #ifdef TCC_TARGET_PE
7011 if (ad
.a
.func_import
)
7012 type
.t
|= VT_IMPORT
;
7013 if (ad
.a
.func_export
)
7014 type
.t
|= VT_EXPORT
;
7016 type
.t
|= ad
.a
.visibility
<< VT_VIS_SHIFT
;
7020 tcc_error("cannot use local functions");
7021 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
7022 expect("function definition");
7024 /* reject abstract declarators in function definition */
7026 while ((sym
= sym
->next
) != NULL
)
7027 if (!(sym
->v
& ~SYM_FIELD
))
7028 expect("identifier");
7030 /* XXX: cannot do better now: convert extern line to static inline */
7031 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
7032 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
7037 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
7040 ref
= sym
->type
.ref
;
7041 if (0 == ref
->a
.func_proto
)
7042 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
7044 /* use func_call from prototype if not defined */
7045 if (ref
->a
.func_call
!= FUNC_CDECL
7046 && type
.ref
->a
.func_call
== FUNC_CDECL
)
7047 type
.ref
->a
.func_call
= ref
->a
.func_call
;
7049 /* use export from prototype */
7050 if (ref
->a
.func_export
)
7051 type
.ref
->a
.func_export
= 1;
7053 /* use static from prototype */
7054 if (sym
->type
.t
& VT_STATIC
)
7055 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
7057 /* If the definition has no visibility use the
7058 one from prototype. */
7059 if (! (type
.t
& VT_VIS_MASK
))
7060 type
.t
|= sym
->type
.t
& VT_VIS_MASK
;
7062 if (!is_compatible_types(&sym
->type
, &type
)) {
7064 tcc_error("incompatible types for redefinition of '%s'",
7065 get_tok_str(v
, NULL
));
7067 type
.ref
->a
.func_proto
= 0;
7068 /* if symbol is already defined, then put complete type */
7071 /* put function symbol */
7072 sym
= global_identifier_push(v
, type
.t
, 0);
7073 sym
->type
.ref
= type
.ref
;
7076 /* static inline functions are just recorded as a kind
7077 of macro. Their code will be emitted at the end of
7078 the compilation unit only if they are used */
7079 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
7080 (VT_INLINE
| VT_STATIC
)) {
7082 struct InlineFunc
*fn
;
7083 const char *filename
;
7085 filename
= file
? file
->filename
: "";
7086 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
7087 strcpy(fn
->filename
, filename
);
7089 fn
->func_str
= tok_str_alloc();
7095 tcc_error("unexpected end of file");
7096 tok_str_add_tok(fn
->func_str
);
7101 } else if (t
== '}') {
7103 if (block_level
== 0)
7107 tok_str_add(fn
->func_str
, -1);
7108 tok_str_add(fn
->func_str
, 0);
7109 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
7112 /* compute text section */
7113 cur_text_section
= ad
.section
;
7114 if (!cur_text_section
)
7115 cur_text_section
= text_section
;
7116 sym
->r
= VT_SYM
| VT_CONST
;
7121 if (btype
.t
& VT_TYPEDEF
) {
7122 /* save typedefed type */
7123 /* XXX: test storage specifiers ? */
7125 if (sym
&& sym
->scope
== local_scope
) {
7126 if (!is_compatible_types(&sym
->type
, &type
)
7127 || !(sym
->type
.t
& VT_TYPEDEF
))
7128 tcc_error("incompatible redefinition of '%s'",
7129 get_tok_str(v
, NULL
));
7132 sym
= sym_push(v
, &type
, 0, 0);
7135 sym
->type
.t
|= VT_TYPEDEF
;
7138 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
7139 /* external function definition */
7140 /* specific case for func_call attribute */
7141 ad
.a
.func_proto
= 1;
7143 } else if (!(type
.t
& VT_ARRAY
)) {
7144 /* not lvalue if array */
7145 r
|= lvalue_type(type
.t
);
7147 has_init
= (tok
== '=');
7148 if (has_init
&& (type
.t
& VT_VLA
))
7149 tcc_error("variable length array cannot be initialized");
7150 if ((btype
.t
& VT_EXTERN
) || ((type
.t
& VT_BTYPE
) == VT_FUNC
) ||
7151 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
7152 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
7153 /* external variable or function */
7154 /* NOTE: as GCC, uninitialized global static
7155 arrays of null size are considered as
7157 sym
= external_sym(v
, &type
, r
);
7158 sym
->asm_label
= ad
.asm_label
;
7160 if (ad
.alias_target
) {
7165 alias_target
= sym_find(ad
.alias_target
);
7166 if (!alias_target
|| !alias_target
->c
)
7167 tcc_error("unsupported forward __alias__ attribute");
7168 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[alias_target
->c
];
7169 tsec
.sh_num
= esym
->st_shndx
;
7170 put_extern_sym2(sym
, &tsec
, esym
->st_value
, esym
->st_size
, 0);
7173 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
7174 if (type
.t
& VT_STATIC
)
7180 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, l
);
7184 if (is_for_loop_init
)
7197 ST_FUNC
void decl(int l
)
7202 /* ------------------------------------------------------------------------- */