2 * TCC - Tiny C Compiler
4 * Copyright (c) 2001-2004 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 /********************************************************/
24 /* global variables */
26 /* loc : local variable index
27 ind : output code index
29 anon_sym: anonymous symbol index
31 ST_DATA
int rsym
, anon_sym
, ind
, loc
;
33 ST_DATA Sym
*sym_free_first
;
34 ST_DATA
void **sym_pools
;
35 ST_DATA
int nb_sym_pools
;
37 ST_DATA Sym
*global_stack
;
38 ST_DATA Sym
*local_stack
;
39 ST_DATA Sym
*define_stack
;
40 ST_DATA Sym
*global_label_stack
;
41 ST_DATA Sym
*local_label_stack
;
42 static int local_scope
;
44 static int section_sym
;
46 ST_DATA
int vlas_in_scope
; /* number of VLAs that are currently in scope */
47 ST_DATA
int vla_sp_root_loc
; /* vla_sp_loc for SP before any VLAs were pushed */
48 ST_DATA
int vla_sp_loc
; /* Pointer to variable holding location to store stack pointer on the stack when modifying stack pointer */
50 ST_DATA SValue __vstack
[1+VSTACK_SIZE
], *vtop
, *pvtop
;
52 ST_DATA
int const_wanted
; /* true if constant wanted */
53 ST_DATA
int nocode_wanted
; /* true if no code generation wanted for an expression */
54 ST_DATA
int global_expr
; /* true if compound literals must be allocated globally (used during initializers parsing */
55 ST_DATA CType func_vt
; /* current function return type (used by return instruction) */
56 ST_DATA
int func_var
; /* true if current function is variadic (used by return instruction) */
58 ST_DATA
int last_line_num
, last_ind
, func_ind
; /* debug last line number and pc */
59 ST_DATA
const char *funcname
;
61 ST_DATA CType char_pointer_type
, func_old_type
, int_type
, size_type
;
63 ST_DATA
struct switch_t
{
66 } **p
; int n
; /* list of case ranges */
67 int def_sym
; /* default symbol */
68 } *cur_switch
; /* current switch */
70 /* ------------------------------------------------------------------------- */
71 static void gen_cast(CType
*type
);
72 static inline CType
*pointed_type(CType
*type
);
73 static int is_compatible_types(CType
*type1
, CType
*type2
);
74 static int parse_btype(CType
*type
, AttributeDef
*ad
);
75 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
);
76 static void parse_expr_type(CType
*type
);
77 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
, int first
, int size_only
);
78 static void block(int *bsym
, int *csym
, int is_expr
);
79 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
, int has_init
, int v
, int scope
);
80 static int decl0(int l
, int is_for_loop_init
);
81 static void expr_eq(void);
82 static void expr_lor_const(void);
83 static void unary_type(CType
*type
);
84 static void vla_runtime_type_size(CType
*type
, int *a
);
85 static void vla_sp_restore(void);
86 static void vla_sp_restore_root(void);
87 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
);
88 static void expr_type(CType
*type
);
89 ST_FUNC
void vpush64(int ty
, unsigned long long v
);
90 ST_FUNC
void vpush(CType
*type
);
91 ST_FUNC
int gvtst(int inv
, int t
);
92 ST_FUNC
int is_btype_size(int bt
);
94 ST_INLN
int is_float(int t
)
98 return bt
== VT_LDOUBLE
|| bt
== VT_DOUBLE
|| bt
== VT_FLOAT
|| bt
== VT_QFLOAT
;
101 /* we use our own 'finite' function to avoid potential problems with
102 non standard math libs */
103 /* XXX: endianness dependent */
104 ST_FUNC
int ieee_finite(double d
)
107 memcpy(p
, &d
, sizeof(double));
108 return ((unsigned)((p
[1] | 0x800fffff) + 1)) >> 31;
111 ST_FUNC
void test_lvalue(void)
113 if (!(vtop
->r
& VT_LVAL
))
117 ST_FUNC
void check_vstack(void)
120 tcc_error("internal compiler error: vstack leak (%d)", vtop
- pvtop
);
123 /* ------------------------------------------------------------------------- */
124 /* vstack debugging aid */
127 void pv (const char *lbl
, int a
, int b
)
130 for (i
= a
; i
< a
+ b
; ++i
) {
131 SValue
*p
= &vtop
[-i
];
132 printf("%s vtop[-%d] : type.t:%04x r:%04x r2:%04x c.i:%d\n",
133 lbl
, i
, p
->type
.t
, p
->r
, p
->r2
, (int)p
->c
.i
);
138 /* ------------------------------------------------------------------------- */
139 ST_FUNC
void tccgen_start(TCCState
*s1
)
141 cur_text_section
= NULL
;
143 anon_sym
= SYM_FIRST_ANOM
;
147 /* define some often used types */
149 char_pointer_type
.t
= VT_BYTE
;
150 mk_pointer(&char_pointer_type
);
152 size_type
.t
= VT_INT
;
154 size_type
.t
= VT_LLONG
;
156 func_old_type
.t
= VT_FUNC
;
157 func_old_type
.ref
= sym_push(SYM_FIELD
, &int_type
, FUNC_CDECL
, FUNC_OLD
);
162 /* file info: full path + filename */
163 section_sym
= put_elf_sym(symtab_section
, 0, 0,
164 ELFW(ST_INFO
)(STB_LOCAL
, STT_SECTION
), 0,
165 text_section
->sh_num
, NULL
);
166 getcwd(buf
, sizeof(buf
));
168 normalize_slashes(buf
);
170 pstrcat(buf
, sizeof(buf
), "/");
171 put_stabs_r(buf
, N_SO
, 0, 0,
172 text_section
->data_offset
, text_section
, section_sym
);
173 put_stabs_r(file
->filename
, N_SO
, 0, 0,
174 text_section
->data_offset
, text_section
, section_sym
);
176 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
177 symbols can be safely used */
178 put_elf_sym(symtab_section
, 0, 0,
179 ELFW(ST_INFO
)(STB_LOCAL
, STT_FILE
), 0,
180 SHN_ABS
, file
->filename
);
182 #ifdef TCC_TARGET_ARM
187 /* define 'void *alloca(unsigned int)' builtin function */
192 sym
= sym_push(p
, mk_pointer(VT_VOID
), FUNC_CDECL
, FUNC_NEW
);
193 s1
= sym_push(SYM_FIELD
, VT_UNSIGNED
| VT_INT
, 0, 0);
196 sym_push(TOK_alloca
, VT_FUNC
| (p
<< VT_STRUCT_SHIFT
), VT_CONST
, 0);
201 ST_FUNC
void tccgen_end(TCCState
*s1
)
204 /* end of translation unit info */
206 put_stabs_r(NULL
, N_SO
, 0, 0,
207 text_section
->data_offset
, text_section
, section_sym
);
211 /* ------------------------------------------------------------------------- */
212 /* update sym->c so that it points to an external symbol in section
213 'section' with value 'value' */
215 ST_FUNC
void put_extern_sym2(Sym
*sym
, Section
*section
,
216 addr_t value
, unsigned long size
,
217 int can_add_underscore
)
219 int sym_type
, sym_bind
, sh_num
, info
, other
;
224 #ifdef CONFIG_TCC_BCHECK
230 else if (section
== SECTION_ABS
)
233 sh_num
= section
->sh_num
;
235 if ((sym
->type
.t
& VT_BTYPE
) == VT_FUNC
) {
237 } else if ((sym
->type
.t
& VT_BTYPE
) == VT_VOID
) {
238 sym_type
= STT_NOTYPE
;
240 sym_type
= STT_OBJECT
;
243 if (sym
->type
.t
& VT_STATIC
)
244 sym_bind
= STB_LOCAL
;
246 if (sym
->type
.t
& VT_WEAK
)
249 sym_bind
= STB_GLOBAL
;
253 name
= get_tok_str(sym
->v
, NULL
);
254 #ifdef CONFIG_TCC_BCHECK
255 if (tcc_state
->do_bounds_check
) {
256 /* XXX: avoid doing that for statics ? */
257 /* if bound checking is activated, we change some function
258 names by adding the "__bound" prefix */
261 /* XXX: we rely only on malloc hooks */
274 strcpy(buf
, "__bound_");
284 if (sym
->type
.t
& VT_EXPORT
)
285 other
|= ST_PE_EXPORT
;
286 if (sym_type
== STT_FUNC
&& sym
->type
.ref
) {
287 Sym
*ref
= sym
->type
.ref
;
288 if (ref
->a
.func_export
)
289 other
|= ST_PE_EXPORT
;
290 if (ref
->a
.func_call
== FUNC_STDCALL
&& can_add_underscore
) {
291 sprintf(buf1
, "_%s@%d", name
, ref
->a
.func_args
* PTR_SIZE
);
293 other
|= ST_PE_STDCALL
;
294 can_add_underscore
= 0;
297 if (find_elf_sym(tcc_state
->dynsymtab_section
, name
))
298 other
|= ST_PE_IMPORT
;
299 if (sym
->type
.t
& VT_IMPORT
)
300 other
|= ST_PE_IMPORT
;
303 if (! (sym
->type
.t
& VT_STATIC
))
304 other
= (sym
->type
.t
& VT_VIS_MASK
) >> VT_VIS_SHIFT
;
306 if (tcc_state
->leading_underscore
&& can_add_underscore
) {
308 pstrcpy(buf1
+ 1, sizeof(buf1
) - 1, name
);
311 if (sym
->asm_label
) {
312 name
= get_tok_str(sym
->asm_label
, NULL
);
314 info
= ELFW(ST_INFO
)(sym_bind
, sym_type
);
315 sym
->c
= add_elf_sym(symtab_section
, value
, size
, info
, other
, sh_num
, name
);
317 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
318 esym
->st_value
= value
;
319 esym
->st_size
= size
;
320 esym
->st_shndx
= sh_num
;
324 ST_FUNC
void put_extern_sym(Sym
*sym
, Section
*section
,
325 addr_t value
, unsigned long size
)
327 put_extern_sym2(sym
, section
, value
, size
, 1);
330 /* add a new relocation entry to symbol 'sym' in section 's' */
331 ST_FUNC
void greloca(Section
*s
, Sym
*sym
, unsigned long offset
, int type
,
337 put_extern_sym(sym
, NULL
, 0, 0);
340 /* now we can add ELF relocation info */
341 put_elf_reloca(symtab_section
, s
, offset
, type
, c
, addend
);
344 ST_FUNC
void greloc(Section
*s
, Sym
*sym
, unsigned long offset
, int type
)
346 greloca(s
, sym
, offset
, type
, 0);
349 /* ------------------------------------------------------------------------- */
350 /* symbol allocator */
351 static Sym
*__sym_malloc(void)
353 Sym
*sym_pool
, *sym
, *last_sym
;
356 sym_pool
= tcc_malloc(SYM_POOL_NB
* sizeof(Sym
));
357 dynarray_add(&sym_pools
, &nb_sym_pools
, sym_pool
);
359 last_sym
= sym_free_first
;
361 for(i
= 0; i
< SYM_POOL_NB
; i
++) {
362 sym
->next
= last_sym
;
366 sym_free_first
= last_sym
;
370 static inline Sym
*sym_malloc(void)
373 sym
= sym_free_first
;
375 sym
= __sym_malloc();
376 sym_free_first
= sym
->next
;
380 ST_INLN
void sym_free(Sym
*sym
)
382 sym
->next
= sym_free_first
;
383 sym_free_first
= sym
;
386 /* push, without hashing */
387 ST_FUNC Sym
*sym_push2(Sym
**ps
, int v
, int t
, long c
)
407 /* find a symbol and return its associated structure. 's' is the top
408 of the symbol stack */
409 ST_FUNC Sym
*sym_find2(Sym
*s
, int v
)
421 /* structure lookup */
422 ST_INLN Sym
*struct_find(int v
)
425 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
427 return table_ident
[v
]->sym_struct
;
430 /* find an identifier */
431 ST_INLN Sym
*sym_find(int v
)
434 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
436 return table_ident
[v
]->sym_identifier
;
439 /* push a given symbol on the symbol stack */
440 ST_FUNC Sym
*sym_push(int v
, CType
*type
, int r
, int c
)
449 s
= sym_push2(ps
, v
, type
->t
, c
);
450 s
->type
.ref
= type
->ref
;
452 /* don't record fields or anonymous symbols */
454 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
455 /* record symbol in token array */
456 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
458 ps
= &ts
->sym_struct
;
460 ps
= &ts
->sym_identifier
;
463 s
->scope
= local_scope
;
464 if (s
->prev_tok
&& s
->prev_tok
->scope
== s
->scope
)
465 tcc_error("redeclaration of '%s'",
466 get_tok_str(v
& ~SYM_STRUCT
, NULL
));
471 /* push a global identifier */
472 ST_FUNC Sym
*global_identifier_push(int v
, int t
, int c
)
475 s
= sym_push2(&global_stack
, v
, t
, c
);
476 /* don't record anonymous symbol */
477 if (v
< SYM_FIRST_ANOM
) {
478 ps
= &table_ident
[v
- TOK_IDENT
]->sym_identifier
;
479 /* modify the top most local identifier, so that
480 sym_identifier will point to 's' when popped */
482 ps
= &(*ps
)->prev_tok
;
489 /* pop symbols until top reaches 'b' */
490 ST_FUNC
void sym_pop(Sym
**ptop
, Sym
*b
)
500 /* remove symbol in token array */
502 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
503 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
505 ps
= &ts
->sym_struct
;
507 ps
= &ts
->sym_identifier
;
516 static void weaken_symbol(Sym
*sym
)
518 sym
->type
.t
|= VT_WEAK
;
523 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
524 esym_type
= ELFW(ST_TYPE
)(esym
->st_info
);
525 esym
->st_info
= ELFW(ST_INFO
)(STB_WEAK
, esym_type
);
529 static void apply_visibility(Sym
*sym
, CType
*type
)
531 int vis
= sym
->type
.t
& VT_VIS_MASK
;
532 int vis2
= type
->t
& VT_VIS_MASK
;
533 if (vis
== (STV_DEFAULT
<< VT_VIS_SHIFT
))
535 else if (vis2
== (STV_DEFAULT
<< VT_VIS_SHIFT
))
538 vis
= (vis
< vis2
) ? vis
: vis2
;
539 sym
->type
.t
&= ~VT_VIS_MASK
;
545 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
546 vis
>>= VT_VIS_SHIFT
;
547 esym
->st_other
= (esym
->st_other
& ~ELFW(ST_VISIBILITY
)(-1)) | vis
;
551 /* ------------------------------------------------------------------------- */
553 ST_FUNC
void swap(int *p
, int *q
)
561 static void vsetc(CType
*type
, int r
, CValue
*vc
)
565 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
566 tcc_error("memory full (vstack)");
567 /* cannot let cpu flags if other instruction are generated. Also
568 avoid leaving VT_JMP anywhere except on the top of the stack
569 because it would complicate the code generator. */
570 if (vtop
>= vstack
) {
571 v
= vtop
->r
& VT_VALMASK
;
572 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
582 /* push constant of type "type" with useless value */
583 ST_FUNC
void vpush(CType
*type
)
586 vsetc(type
, VT_CONST
, &cval
);
589 /* push integer constant */
590 ST_FUNC
void vpushi(int v
)
594 vsetc(&int_type
, VT_CONST
, &cval
);
597 /* push a pointer sized constant */
598 static void vpushs(addr_t v
)
602 vsetc(&size_type
, VT_CONST
, &cval
);
605 /* push arbitrary 64bit constant */
606 ST_FUNC
void vpush64(int ty
, unsigned long long v
)
613 vsetc(&ctype
, VT_CONST
, &cval
);
616 /* push long long constant */
617 static inline void vpushll(long long v
)
619 vpush64(VT_LLONG
, v
);
622 /* push a symbol value of TYPE */
623 static inline void vpushsym(CType
*type
, Sym
*sym
)
627 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
631 /* Return a static symbol pointing to a section */
632 ST_FUNC Sym
*get_sym_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
638 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
639 sym
->type
.ref
= type
->ref
;
640 sym
->r
= VT_CONST
| VT_SYM
;
641 put_extern_sym(sym
, sec
, offset
, size
);
645 /* push a reference to a section offset by adding a dummy symbol */
646 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
648 vpushsym(type
, get_sym_ref(type
, sec
, offset
, size
));
651 /* define a new external reference to a symbol 'v' of type 'u' */
652 ST_FUNC Sym
*external_global_sym(int v
, CType
*type
, int r
)
658 /* push forward reference */
659 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
660 s
->type
.ref
= type
->ref
;
661 s
->r
= r
| VT_CONST
| VT_SYM
;
666 /* define a new external reference to a symbol 'v' */
667 static Sym
*external_sym(int v
, CType
*type
, int r
)
673 /* push forward reference */
674 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
675 s
->type
.t
|= VT_EXTERN
;
676 } else if (s
->type
.ref
== func_old_type
.ref
) {
677 s
->type
.ref
= type
->ref
;
678 s
->r
= r
| VT_CONST
| VT_SYM
;
679 s
->type
.t
|= VT_EXTERN
;
680 } else if (!is_compatible_types(&s
->type
, type
)) {
681 tcc_error("incompatible types for redefinition of '%s'",
682 get_tok_str(v
, NULL
));
684 /* Merge some storage attributes. */
685 if (type
->t
& VT_WEAK
)
688 if (type
->t
& VT_VIS_MASK
)
689 apply_visibility(s
, type
);
694 /* push a reference to global symbol v */
695 ST_FUNC
void vpush_global_sym(CType
*type
, int v
)
697 vpushsym(type
, external_global_sym(v
, type
, 0));
700 ST_FUNC
void vset(CType
*type
, int r
, int v
)
705 vsetc(type
, r
, &cval
);
708 static void vseti(int r
, int v
)
716 ST_FUNC
void vswap(void)
719 /* cannot let cpu flags if other instruction are generated. Also
720 avoid leaving VT_JMP anywhere except on the top of the stack
721 because it would complicate the code generator. */
722 if (vtop
>= vstack
) {
723 int v
= vtop
->r
& VT_VALMASK
;
724 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
731 /* XXX: +2% overall speed possible with optimized memswap
733 * memswap(&vtop[0], &vtop[1], sizeof *vtop);
737 ST_FUNC
void vpushv(SValue
*v
)
739 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
740 tcc_error("memory full (vstack)");
745 static void vdup(void)
750 /* save registers up to (vtop - n) stack entry */
751 ST_FUNC
void save_regs(int n
)
754 for(p
= vstack
, p1
= vtop
- n
; p
<= p1
; p
++)
758 /* save r to the memory stack, and mark it as being free */
759 ST_FUNC
void save_reg(int r
)
761 save_reg_upstack(r
, 0);
764 /* save r to the memory stack, and mark it as being free,
765 if seen up to (vtop - n) stack entry */
766 ST_FUNC
void save_reg_upstack(int r
, int n
)
768 int l
, saved
, size
, align
;
772 if ((r
&= VT_VALMASK
) >= VT_CONST
)
775 /* modify all stack values */
778 for(p
= vstack
, p1
= vtop
- n
; p
<= p1
; p
++) {
779 if ((p
->r
& VT_VALMASK
) == r
||
780 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
781 /* must save value on stack if not already done */
783 /* NOTE: must reload 'r' because r might be equal to r2 */
784 r
= p
->r
& VT_VALMASK
;
785 /* store register in the stack */
787 if ((p
->r
& VT_LVAL
) ||
788 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
789 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
790 type
= &char_pointer_type
;
794 size
= type_size(type
, &align
);
795 loc
= (loc
- size
) & -align
;
797 sv
.r
= VT_LOCAL
| VT_LVAL
;
800 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
801 /* x86 specific: need to pop fp register ST0 if saved */
803 o(0xd8dd); /* fstp %st(0) */
806 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
807 /* special long long case */
808 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
816 /* mark that stack entry as being saved on the stack */
817 if (p
->r
& VT_LVAL
) {
818 /* also clear the bounded flag because the
819 relocation address of the function was stored in
821 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
823 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
831 #ifdef TCC_TARGET_ARM
832 /* find a register of class 'rc2' with at most one reference on stack.
833 * If none, call get_reg(rc) */
834 ST_FUNC
int get_reg_ex(int rc
, int rc2
)
839 for(r
=0;r
<NB_REGS
;r
++) {
840 if (reg_classes
[r
] & rc2
) {
843 for(p
= vstack
; p
<= vtop
; p
++) {
844 if ((p
->r
& VT_VALMASK
) == r
||
845 (p
->r2
& VT_VALMASK
) == r
)
856 /* find a free register of class 'rc'. If none, save one register */
857 ST_FUNC
int get_reg(int rc
)
862 /* find a free register */
863 for(r
=0;r
<NB_REGS
;r
++) {
864 if (reg_classes
[r
] & rc
) {
865 for(p
=vstack
;p
<=vtop
;p
++) {
866 if ((p
->r
& VT_VALMASK
) == r
||
867 (p
->r2
& VT_VALMASK
) == r
)
875 /* no register left : free the first one on the stack (VERY
876 IMPORTANT to start from the bottom to ensure that we don't
877 spill registers used in gen_opi()) */
878 for(p
=vstack
;p
<=vtop
;p
++) {
879 /* look at second register (if long long) */
880 r
= p
->r2
& VT_VALMASK
;
881 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
883 r
= p
->r
& VT_VALMASK
;
884 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
890 /* Should never comes here */
894 /* move register 's' (of type 't') to 'r', and flush previous value of r to memory
896 static void move_reg(int r
, int s
, int t
)
910 /* get address of vtop (vtop MUST BE an lvalue) */
911 ST_FUNC
void gaddrof(void)
913 if (vtop
->r
& VT_REF
&& !nocode_wanted
)
916 /* tricky: if saved lvalue, then we can go back to lvalue */
917 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
918 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
923 #ifdef CONFIG_TCC_BCHECK
924 /* generate lvalue bound code */
925 static void gbound(void)
930 vtop
->r
&= ~VT_MUSTBOUND
;
931 /* if lvalue, then use checking code before dereferencing */
932 if (vtop
->r
& VT_LVAL
) {
933 /* if not VT_BOUNDED value, then make one */
934 if (!(vtop
->r
& VT_BOUNDED
)) {
935 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
936 /* must save type because we must set it to int to get pointer */
938 vtop
->type
.t
= VT_PTR
;
941 gen_bounded_ptr_add();
942 vtop
->r
|= lval_type
;
945 /* then check for dereferencing */
946 gen_bounded_ptr_deref();
951 /* store vtop a register belonging to class 'rc'. lvalues are
952 converted to values. Cannot be used if cannot be converted to
953 register value (such as structures). */
954 ST_FUNC
int gv(int rc
)
956 int r
, bit_pos
, bit_size
, size
, align
, i
;
959 /* NOTE: get_reg can modify vstack[] */
960 if (vtop
->type
.t
& VT_BITFIELD
) {
963 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
964 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
965 /* remove bit field info to avoid loops */
966 vtop
->type
.t
&= ~VT_BITFIELD
& ((1 << VT_STRUCT_SHIFT
) - 1);
967 /* cast to int to propagate signedness in following ops */
968 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
973 if((vtop
->type
.t
& VT_UNSIGNED
) ||
974 (vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
975 type
.t
|= VT_UNSIGNED
;
977 /* generate shifts */
978 vpushi(bits
- (bit_pos
+ bit_size
));
980 vpushi(bits
- bit_size
);
981 /* NOTE: transformed to SHR if unsigned */
985 if (is_float(vtop
->type
.t
) &&
986 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
989 unsigned long offset
;
990 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
994 /* XXX: unify with initializers handling ? */
995 /* CPUs usually cannot use float constants, so we store them
996 generically in data segment */
997 size
= type_size(&vtop
->type
, &align
);
998 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
999 data_section
->data_offset
= offset
;
1000 /* XXX: not portable yet */
1001 #if defined(__i386__) || defined(__x86_64__)
1002 /* Zero pad x87 tenbyte long doubles */
1003 if (size
== LDOUBLE_SIZE
) {
1004 vtop
->c
.tab
[2] &= 0xffff;
1005 #if LDOUBLE_SIZE == 16
1010 ptr
= section_ptr_add(data_section
, size
);
1012 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
1016 ptr
[i
] = vtop
->c
.tab
[size
-1-i
];
1020 ptr
[i
] = vtop
->c
.tab
[i
];
1021 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
1022 vtop
->r
|= VT_LVAL
| VT_SYM
;
1026 #ifdef CONFIG_TCC_BCHECK
1027 if (vtop
->r
& VT_MUSTBOUND
)
1031 r
= vtop
->r
& VT_VALMASK
;
1032 rc2
= (rc
& RC_FLOAT
) ? RC_FLOAT
: RC_INT
;
1033 #ifndef TCC_TARGET_ARM64
1036 #ifdef TCC_TARGET_X86_64
1037 else if (rc
== RC_FRET
)
1042 /* need to reload if:
1044 - lvalue (need to dereference pointer)
1045 - already a register, but not in the right class */
1047 || (vtop
->r
& VT_LVAL
)
1048 || !(reg_classes
[r
] & rc
)
1049 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1050 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
1051 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
&& !(reg_classes
[vtop
->r2
] & rc2
))
1053 || ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
1058 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1059 if (((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
)) {
1060 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
1062 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
1063 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
1064 unsigned long long ll
;
1066 int r2
, original_type
;
1067 original_type
= vtop
->type
.t
;
1068 /* two register type load : expand to two words
1070 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
1071 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
1074 vtop
->c
.i
= ll
; /* first word */
1076 vtop
->r
= r
; /* save register value */
1077 vpushi(ll
>> 32); /* second word */
1080 if (vtop
->r
& VT_LVAL
) {
1081 /* We do not want to modifier the long long
1082 pointer here, so the safest (and less
1083 efficient) is to save all the other registers
1084 in the stack. XXX: totally inefficient. */
1088 /* lvalue_save: save only if used further down the stack */
1089 save_reg_upstack(vtop
->r
, 1);
1091 /* load from memory */
1092 vtop
->type
.t
= load_type
;
1095 vtop
[-1].r
= r
; /* save register value */
1096 /* increment pointer to get second word */
1097 vtop
->type
.t
= addr_type
;
1102 vtop
->type
.t
= load_type
;
1104 /* move registers */
1107 vtop
[-1].r
= r
; /* save register value */
1108 vtop
->r
= vtop
[-1].r2
;
1110 /* Allocate second register. Here we rely on the fact that
1111 get_reg() tries first to free r2 of an SValue. */
1115 /* write second register */
1117 vtop
->type
.t
= original_type
;
1118 } else if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
1120 /* lvalue of scalar type : need to use lvalue type
1121 because of possible cast */
1124 /* compute memory access type */
1125 if (vtop
->r
& VT_REF
)
1126 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1131 else if (vtop
->r
& VT_LVAL_BYTE
)
1133 else if (vtop
->r
& VT_LVAL_SHORT
)
1135 if (vtop
->r
& VT_LVAL_UNSIGNED
)
1139 /* restore wanted type */
1142 /* one register type load */
1147 #ifdef TCC_TARGET_C67
1148 /* uses register pairs for doubles */
1149 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
1156 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
1157 ST_FUNC
void gv2(int rc1
, int rc2
)
1161 /* generate more generic register first. But VT_JMP or VT_CMP
1162 values must be generated first in all cases to avoid possible
1164 v
= vtop
[0].r
& VT_VALMASK
;
1165 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
1170 /* test if reload is needed for first register */
1171 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
1181 /* test if reload is needed for first register */
1182 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
1188 #ifndef TCC_TARGET_ARM64
1189 /* wrapper around RC_FRET to return a register by type */
1190 static int rc_fret(int t
)
1192 #ifdef TCC_TARGET_X86_64
1193 if (t
== VT_LDOUBLE
) {
1201 /* wrapper around REG_FRET to return a register by type */
1202 static int reg_fret(int t
)
1204 #ifdef TCC_TARGET_X86_64
1205 if (t
== VT_LDOUBLE
) {
1212 /* expand long long on stack in two int registers */
1213 static void lexpand(void)
1217 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
1220 vtop
[0].r
= vtop
[-1].r2
;
1221 vtop
[0].r2
= VT_CONST
;
1222 vtop
[-1].r2
= VT_CONST
;
1223 vtop
[0].type
.t
= VT_INT
| u
;
1224 vtop
[-1].type
.t
= VT_INT
| u
;
1227 #ifdef TCC_TARGET_ARM
1228 /* expand long long on stack */
1229 ST_FUNC
void lexpand_nr(void)
1233 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
1235 vtop
->r2
= VT_CONST
;
1236 vtop
->type
.t
= VT_INT
| u
;
1237 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
1238 if (v
== VT_CONST
) {
1239 vtop
[-1].c
.i
= vtop
->c
.i
;
1240 vtop
->c
.i
= vtop
->c
.i
>> 32;
1242 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
1244 vtop
->r
= vtop
[-1].r
;
1245 } else if (v
> VT_CONST
) {
1249 vtop
->r
= vtop
[-1].r2
;
1250 vtop
[-1].r2
= VT_CONST
;
1251 vtop
[-1].type
.t
= VT_INT
| u
;
1255 /* build a long long from two ints */
1256 static void lbuild(int t
)
1258 gv2(RC_INT
, RC_INT
);
1259 vtop
[-1].r2
= vtop
[0].r
;
1260 vtop
[-1].type
.t
= t
;
1264 /* rotate n first stack elements to the bottom
1265 I1 ... In -> I2 ... In I1 [top is right]
1267 ST_FUNC
void vrotb(int n
)
1273 for(i
=-n
+1;i
!=0;i
++)
1274 vtop
[i
] = vtop
[i
+1];
1278 /* rotate the n elements before entry e towards the top
1279 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
1281 ST_FUNC
void vrote(SValue
*e
, int n
)
1287 for(i
= 0;i
< n
- 1; i
++)
1292 /* rotate n first stack elements to the top
1293 I1 ... In -> In I1 ... I(n-1) [top is right]
1295 ST_FUNC
void vrott(int n
)
1300 /* pop stack value */
1301 ST_FUNC
void vpop(void)
1304 v
= vtop
->r
& VT_VALMASK
;
1305 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1306 /* for x86, we need to pop the FP stack */
1307 if (v
== TREG_ST0
&& !nocode_wanted
) {
1308 o(0xd8dd); /* fstp %st(0) */
1311 if (v
== VT_JMP
|| v
== VT_JMPI
) {
1312 /* need to put correct jump if && or || without test */
1318 /* convert stack entry to register and duplicate its value in another
1320 static void gv_dup(void)
1326 if ((t
& VT_BTYPE
) == VT_LLONG
) {
1333 /* stack: H L L1 H1 */
1341 /* duplicate value */
1346 #ifdef TCC_TARGET_X86_64
1347 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1357 load(r1
, &sv
); /* move r to r1 */
1359 /* duplicates value */
1365 /* Generate value test
1367 * Generate a test for any value (jump, comparison and integers) */
1368 ST_FUNC
int gvtst(int inv
, int t
)
1370 int v
= vtop
->r
& VT_VALMASK
;
1371 if (v
!= VT_CMP
&& v
!= VT_JMP
&& v
!= VT_JMPI
) {
1375 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1376 /* constant jmp optimization */
1377 if ((vtop
->c
.i
!= 0) != inv
)
1382 return gtst(inv
, t
);
1385 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
1386 /* generate CPU independent (unsigned) long long operations */
1387 static void gen_opl(int op
)
1389 int t
, a
, b
, op1
, c
, i
;
1391 unsigned short reg_iret
= REG_IRET
;
1392 unsigned short reg_lret
= REG_LRET
;
1398 func
= TOK___divdi3
;
1401 func
= TOK___udivdi3
;
1404 func
= TOK___moddi3
;
1407 func
= TOK___umoddi3
;
1414 /* call generic long long function */
1415 vpush_global_sym(&func_old_type
, func
);
1420 vtop
->r2
= reg_lret
;
1428 //pv("gen_opl A",0,2);
1434 /* stack: L1 H1 L2 H2 */
1439 vtop
[-2] = vtop
[-3];
1442 /* stack: H1 H2 L1 L2 */
1443 //pv("gen_opl B",0,4);
1449 /* stack: H1 H2 L1 L2 ML MH */
1452 /* stack: ML MH H1 H2 L1 L2 */
1456 /* stack: ML MH H1 L2 H2 L1 */
1461 /* stack: ML MH M1 M2 */
1464 } else if (op
== '+' || op
== '-') {
1465 /* XXX: add non carry method too (for MIPS or alpha) */
1471 /* stack: H1 H2 (L1 op L2) */
1474 gen_op(op1
+ 1); /* TOK_xxxC2 */
1477 /* stack: H1 H2 (L1 op L2) */
1480 /* stack: (L1 op L2) H1 H2 */
1482 /* stack: (L1 op L2) (H1 op H2) */
1490 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1491 t
= vtop
[-1].type
.t
;
1495 /* stack: L H shift */
1497 /* constant: simpler */
1498 /* NOTE: all comments are for SHL. the other cases are
1499 done by swaping words */
1510 if (op
!= TOK_SAR
) {
1543 /* XXX: should provide a faster fallback on x86 ? */
1546 func
= TOK___ashrdi3
;
1549 func
= TOK___lshrdi3
;
1552 func
= TOK___ashldi3
;
1558 /* compare operations */
1564 /* stack: L1 H1 L2 H2 */
1566 vtop
[-1] = vtop
[-2];
1568 /* stack: L1 L2 H1 H2 */
1571 /* when values are equal, we need to compare low words. since
1572 the jump is inverted, we invert the test too. */
1575 else if (op1
== TOK_GT
)
1577 else if (op1
== TOK_ULT
)
1579 else if (op1
== TOK_UGT
)
1584 if (op1
!= TOK_NE
) {
1588 /* generate non equal test */
1589 /* XXX: NOT PORTABLE yet */
1593 #if defined(TCC_TARGET_I386)
1594 b
= psym(0x850f, 0);
1595 #elif defined(TCC_TARGET_ARM)
1597 o(0x1A000000 | encbranch(ind
, 0, 1));
1598 #elif defined(TCC_TARGET_C67) || defined(TCC_TARGET_ARM64)
1599 tcc_error("not implemented");
1601 #error not supported
1605 /* compare low. Always unsigned */
1609 else if (op1
== TOK_LE
)
1611 else if (op1
== TOK_GT
)
1613 else if (op1
== TOK_GE
)
1624 static uint64_t gen_opic_sdiv(uint64_t a
, uint64_t b
)
1626 uint64_t x
= (a
>> 63 ? -a
: a
) / (b
>> 63 ? -b
: b
);
1627 return (a
^ b
) >> 63 ? -x
: x
;
1630 static int gen_opic_lt(uint64_t a
, uint64_t b
)
1632 return (a
^ (uint64_t)1 << 63) < (b
^ (uint64_t)1 << 63);
1635 /* handle integer constant optimizations and various machine
1637 static void gen_opic(int op
)
1639 SValue
*v1
= vtop
- 1;
1641 int t1
= v1
->type
.t
& VT_BTYPE
;
1642 int t2
= v2
->type
.t
& VT_BTYPE
;
1643 int c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1644 int c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1645 uint64_t l1
= c1
? v1
->c
.i
: 0;
1646 uint64_t l2
= c2
? v2
->c
.i
: 0;
1647 int shm
= (t1
== VT_LLONG
) ? 63 : 31;
1650 l1
= ((uint32_t)l1
|
1651 (v1
->type
.t
& VT_UNSIGNED
? 0 : -(l1
& 0x80000000)));
1653 l2
= ((uint32_t)l2
|
1654 (v2
->type
.t
& VT_UNSIGNED
? 0 : -(l2
& 0x80000000)));
1658 case '+': l1
+= l2
; break;
1659 case '-': l1
-= l2
; break;
1660 case '&': l1
&= l2
; break;
1661 case '^': l1
^= l2
; break;
1662 case '|': l1
|= l2
; break;
1663 case '*': l1
*= l2
; break;
1670 /* if division by zero, generate explicit division */
1673 tcc_error("division by zero in constant");
1677 default: l1
= gen_opic_sdiv(l1
, l2
); break;
1678 case '%': l1
= l1
- l2
* gen_opic_sdiv(l1
, l2
); break;
1679 case TOK_UDIV
: l1
= l1
/ l2
; break;
1680 case TOK_UMOD
: l1
= l1
% l2
; break;
1683 case TOK_SHL
: l1
<<= (l2
& shm
); break;
1684 case TOK_SHR
: l1
>>= (l2
& shm
); break;
1686 l1
= (l1
>> 63) ? ~(~l1
>> (l2
& shm
)) : l1
>> (l2
& shm
);
1689 case TOK_ULT
: l1
= l1
< l2
; break;
1690 case TOK_UGE
: l1
= l1
>= l2
; break;
1691 case TOK_EQ
: l1
= l1
== l2
; break;
1692 case TOK_NE
: l1
= l1
!= l2
; break;
1693 case TOK_ULE
: l1
= l1
<= l2
; break;
1694 case TOK_UGT
: l1
= l1
> l2
; break;
1695 case TOK_LT
: l1
= gen_opic_lt(l1
, l2
); break;
1696 case TOK_GE
: l1
= !gen_opic_lt(l1
, l2
); break;
1697 case TOK_LE
: l1
= !gen_opic_lt(l2
, l1
); break;
1698 case TOK_GT
: l1
= gen_opic_lt(l2
, l1
); break;
1700 case TOK_LAND
: l1
= l1
&& l2
; break;
1701 case TOK_LOR
: l1
= l1
|| l2
; break;
1708 /* if commutative ops, put c2 as constant */
1709 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1710 op
== '|' || op
== '*')) {
1712 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1713 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1715 if (!const_wanted
&&
1717 (op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
)) ||
1718 (l1
== -1 && op
== TOK_SAR
))) {
1719 /* treat (0 << x), (0 >> x) and (-1 >> x) as constant */
1721 } else if (!const_wanted
&&
1722 c2
&& ((l2
== 0 && (op
== '&' || op
== '*')) ||
1723 (l2
== -1 && op
== '|') ||
1724 (l2
== 0xffffffff && t2
!= VT_LLONG
&& op
== '|') ||
1725 (l2
== 1 && (op
== '%' || op
== TOK_UMOD
)))) {
1726 /* treat (x & 0), (x * 0), (x | -1) and (x % 1) as constant */
1731 } else if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1734 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1735 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1739 /* filter out NOP operations like x*1, x-0, x&-1... */
1741 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1742 /* try to use shifts instead of muls or divs */
1743 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1752 else if (op
== TOK_PDIV
)
1758 } else if (c2
&& (op
== '+' || op
== '-') &&
1759 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
1760 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1761 /* symbol + constant case */
1768 if (!nocode_wanted
) {
1769 /* call low level op generator */
1770 if (t1
== VT_LLONG
|| t2
== VT_LLONG
||
1771 (PTR_SIZE
== 8 && (t1
== VT_PTR
|| t2
== VT_PTR
)))
1782 /* generate a floating point operation with constant propagation */
1783 static void gen_opif(int op
)
1791 /* currently, we cannot do computations with forward symbols */
1792 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1793 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1795 if (v1
->type
.t
== VT_FLOAT
) {
1798 } else if (v1
->type
.t
== VT_DOUBLE
) {
1806 /* NOTE: we only do constant propagation if finite number (not
1807 NaN or infinity) (ANSI spec) */
1808 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1812 case '+': f1
+= f2
; break;
1813 case '-': f1
-= f2
; break;
1814 case '*': f1
*= f2
; break;
1818 tcc_error("division by zero in constant");
1823 /* XXX: also handles tests ? */
1827 /* XXX: overflow test ? */
1828 if (v1
->type
.t
== VT_FLOAT
) {
1830 } else if (v1
->type
.t
== VT_DOUBLE
) {
1838 if (!nocode_wanted
) {
1846 static int pointed_size(CType
*type
)
1849 return type_size(pointed_type(type
), &align
);
1852 static void vla_runtime_pointed_size(CType
*type
)
1855 vla_runtime_type_size(pointed_type(type
), &align
);
1858 static inline int is_null_pointer(SValue
*p
)
1860 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1862 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& (uint32_t)p
->c
.i
== 0) ||
1863 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.i
== 0) ||
1864 ((p
->type
.t
& VT_BTYPE
) == VT_PTR
&&
1865 (PTR_SIZE
== 4 ? (uint32_t)p
->c
.i
== 0 : p
->c
.i
== 0));
1868 static inline int is_integer_btype(int bt
)
1870 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
1871 bt
== VT_INT
|| bt
== VT_LLONG
);
1874 /* check types for comparison or subtraction of pointers */
1875 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
1877 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
1880 /* null pointers are accepted for all comparisons as gcc */
1881 if (is_null_pointer(p1
) || is_null_pointer(p2
))
1885 bt1
= type1
->t
& VT_BTYPE
;
1886 bt2
= type2
->t
& VT_BTYPE
;
1887 /* accept comparison between pointer and integer with a warning */
1888 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
1889 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
1890 tcc_warning("comparison between pointer and integer");
1894 /* both must be pointers or implicit function pointers */
1895 if (bt1
== VT_PTR
) {
1896 type1
= pointed_type(type1
);
1897 } else if (bt1
!= VT_FUNC
)
1898 goto invalid_operands
;
1900 if (bt2
== VT_PTR
) {
1901 type2
= pointed_type(type2
);
1902 } else if (bt2
!= VT_FUNC
) {
1904 tcc_error("invalid operands to binary %s", get_tok_str(op
, NULL
));
1906 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
1907 (type2
->t
& VT_BTYPE
) == VT_VOID
)
1911 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1912 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
1913 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
1914 /* gcc-like error if '-' is used */
1916 goto invalid_operands
;
1918 tcc_warning("comparison of distinct pointer types lacks a cast");
1922 /* generic gen_op: handles types problems */
1923 ST_FUNC
void gen_op(int op
)
1925 int u
, t1
, t2
, bt1
, bt2
, t
;
1928 t1
= vtop
[-1].type
.t
;
1929 t2
= vtop
[0].type
.t
;
1930 bt1
= t1
& VT_BTYPE
;
1931 bt2
= t2
& VT_BTYPE
;
1933 if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
1934 tcc_error("operation on a struct");
1935 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
1936 /* at least one operand is a pointer */
1937 /* relationnal op: must be both pointers */
1938 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
1939 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1940 /* pointers are handled are unsigned */
1941 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1942 t
= VT_LLONG
| VT_UNSIGNED
;
1944 t
= VT_INT
| VT_UNSIGNED
;
1948 /* if both pointers, then it must be the '-' op */
1949 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
1951 tcc_error("cannot use pointers here");
1952 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
1953 /* XXX: check that types are compatible */
1954 if (vtop
[-1].type
.t
& VT_VLA
) {
1955 vla_runtime_pointed_size(&vtop
[-1].type
);
1957 vpushi(pointed_size(&vtop
[-1].type
));
1961 /* set to integer type */
1962 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1963 vtop
->type
.t
= VT_LLONG
;
1965 vtop
->type
.t
= VT_INT
;
1970 /* exactly one pointer : must be '+' or '-'. */
1971 if (op
!= '-' && op
!= '+')
1972 tcc_error("cannot use pointers here");
1973 /* Put pointer as first operand */
1974 if (bt2
== VT_PTR
) {
1979 if ((vtop
[0].type
.t
& VT_BTYPE
) == VT_LLONG
)
1980 /* XXX: truncate here because gen_opl can't handle ptr + long long */
1981 gen_cast(&int_type
);
1983 type1
= vtop
[-1].type
;
1984 type1
.t
&= ~VT_ARRAY
;
1985 if (vtop
[-1].type
.t
& VT_VLA
)
1986 vla_runtime_pointed_size(&vtop
[-1].type
);
1988 u
= pointed_size(&vtop
[-1].type
);
1990 tcc_error("unknown array element size");
1991 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1994 /* XXX: cast to int ? (long long case) */
2000 /* #ifdef CONFIG_TCC_BCHECK
2001 The main reason to removing this code:
2008 fprintf(stderr, "v+i-j = %p\n", v+i-j);
2009 fprintf(stderr, "v+(i-j) = %p\n", v+(i-j));
2011 When this code is on. then the output looks like
2013 v+(i-j) = 0xbff84000
2015 /* if evaluating constant expression, no code should be
2016 generated, so no bound check */
2017 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
2018 /* if bounded pointers, we generate a special code to
2025 gen_bounded_ptr_add();
2031 /* put again type if gen_opic() swaped operands */
2034 } else if (is_float(bt1
) || is_float(bt2
)) {
2035 /* compute bigger type and do implicit casts */
2036 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
2038 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
2043 /* floats can only be used for a few operations */
2044 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
2045 (op
< TOK_ULT
|| op
> TOK_GT
))
2046 tcc_error("invalid operands for binary operation");
2048 } else if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
) {
2049 t
= bt1
== VT_LLONG
? VT_LLONG
: VT_INT
;
2050 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (t
| VT_UNSIGNED
))
2053 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
2054 /* cast to biggest op */
2056 /* convert to unsigned if it does not fit in a long long */
2057 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
2058 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
2062 /* integer operations */
2064 /* convert to unsigned if it does not fit in an integer */
2065 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
2066 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
2069 /* XXX: currently, some unsigned operations are explicit, so
2070 we modify them here */
2071 if (t
& VT_UNSIGNED
) {
2078 else if (op
== TOK_LT
)
2080 else if (op
== TOK_GT
)
2082 else if (op
== TOK_LE
)
2084 else if (op
== TOK_GE
)
2091 /* special case for shifts and long long: we keep the shift as
2093 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
2100 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
2101 /* relationnal op: the result is an int */
2102 vtop
->type
.t
= VT_INT
;
2107 // Make sure that we have converted to an rvalue:
2108 if (vtop
->r
& VT_LVAL
&& !nocode_wanted
)
2109 gv(is_float(vtop
->type
.t
& VT_BTYPE
) ? RC_FLOAT
: RC_INT
);
2112 #ifndef TCC_TARGET_ARM
2113 /* generic itof for unsigned long long case */
2114 static void gen_cvt_itof1(int t
)
2116 #ifdef TCC_TARGET_ARM64
2119 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
2120 (VT_LLONG
| VT_UNSIGNED
)) {
2123 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
2124 #if LDOUBLE_SIZE != 8
2125 else if (t
== VT_LDOUBLE
)
2126 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
2129 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
2133 vtop
->r
= reg_fret(t
);
2141 /* generic ftoi for unsigned long long case */
2142 static void gen_cvt_ftoi1(int t
)
2144 #ifdef TCC_TARGET_ARM64
2149 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
2150 /* not handled natively */
2151 st
= vtop
->type
.t
& VT_BTYPE
;
2153 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
2154 #if LDOUBLE_SIZE != 8
2155 else if (st
== VT_LDOUBLE
)
2156 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
2159 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
2164 vtop
->r2
= REG_LRET
;
2171 /* force char or short cast */
2172 static void force_charshort_cast(int t
)
2176 /* XXX: add optimization if lvalue : just change type and offset */
2181 if (t
& VT_UNSIGNED
) {
2182 vpushi((1 << bits
) - 1);
2185 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
)
2191 /* result must be signed or the SAR is converted to an SHL
2192 This was not the case when "t" was a signed short
2193 and the last value on the stack was an unsigned int */
2194 vtop
->type
.t
&= ~VT_UNSIGNED
;
2200 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
2201 static void gen_cast(CType
*type
)
2203 int sbt
, dbt
, sf
, df
, c
, p
;
2205 /* special delayed cast for char/short */
2206 /* XXX: in some cases (multiple cascaded casts), it may still
2208 if (vtop
->r
& VT_MUSTCAST
) {
2209 vtop
->r
&= ~VT_MUSTCAST
;
2210 force_charshort_cast(vtop
->type
.t
);
2213 /* bitfields first get cast to ints */
2214 if (vtop
->type
.t
& VT_BITFIELD
&& !nocode_wanted
) {
2218 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
2219 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
2224 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
2225 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
2227 /* constant case: we can do it now */
2228 /* XXX: in ISOC, cannot do it if error in convert */
2229 if (sbt
== VT_FLOAT
)
2230 vtop
->c
.ld
= vtop
->c
.f
;
2231 else if (sbt
== VT_DOUBLE
)
2232 vtop
->c
.ld
= vtop
->c
.d
;
2235 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
2236 if ((sbt
& VT_UNSIGNED
) || !(vtop
->c
.i
>> 63))
2237 vtop
->c
.ld
= vtop
->c
.i
;
2239 vtop
->c
.ld
= -(long double)-vtop
->c
.i
;
2241 if ((sbt
& VT_UNSIGNED
) || !(vtop
->c
.i
>> 31))
2242 vtop
->c
.ld
= (uint32_t)vtop
->c
.i
;
2244 vtop
->c
.ld
= -(long double)-(uint32_t)vtop
->c
.i
;
2247 if (dbt
== VT_FLOAT
)
2248 vtop
->c
.f
= (float)vtop
->c
.ld
;
2249 else if (dbt
== VT_DOUBLE
)
2250 vtop
->c
.d
= (double)vtop
->c
.ld
;
2251 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
2252 vtop
->c
.i
= vtop
->c
.ld
;
2253 } else if (sf
&& dbt
== VT_BOOL
) {
2254 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
2257 vtop
->c
.i
= vtop
->c
.ld
;
2258 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
2260 else if (sbt
& VT_UNSIGNED
)
2261 vtop
->c
.i
= (uint32_t)vtop
->c
.i
;
2262 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2263 else if (sbt
== VT_PTR
)
2266 else if (sbt
!= VT_LLONG
)
2267 vtop
->c
.i
= ((uint32_t)vtop
->c
.i
|
2268 -(vtop
->c
.i
& 0x80000000));
2270 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
2272 else if (dbt
== VT_BOOL
)
2273 vtop
->c
.i
= (vtop
->c
.i
!= 0);
2274 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2275 else if (dbt
== VT_PTR
)
2278 else if (dbt
!= VT_LLONG
) {
2279 uint32_t m
= ((dbt
& VT_BTYPE
) == VT_BYTE
? 0xff :
2280 (dbt
& VT_BTYPE
) == VT_SHORT
? 0xffff :
2283 if (!(dbt
& VT_UNSIGNED
))
2284 vtop
->c
.i
|= -(vtop
->c
.i
& ((m
>> 1) + 1));
2287 } else if (p
&& dbt
== VT_BOOL
) {
2290 } else if (!nocode_wanted
) {
2291 /* non constant case: generate code */
2293 /* convert from fp to fp */
2296 /* convert int to fp */
2299 /* convert fp to int */
2300 if (dbt
== VT_BOOL
) {
2304 /* we handle char/short/etc... with generic code */
2305 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
2306 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
2310 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
2311 /* additional cast for char/short... */
2316 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
2317 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
2318 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
2319 /* scalar to long long */
2320 /* machine independent conversion */
2322 /* generate high word */
2323 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
2327 if (sbt
== VT_PTR
) {
2328 /* cast from pointer to int before we apply
2329 shift operation, which pointers don't support*/
2330 gen_cast(&int_type
);
2336 /* patch second register */
2337 vtop
[-1].r2
= vtop
->r
;
2341 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
2342 (dbt
& VT_BTYPE
) == VT_PTR
||
2343 (dbt
& VT_BTYPE
) == VT_FUNC
) {
2344 if ((sbt
& VT_BTYPE
) != VT_LLONG
&&
2345 (sbt
& VT_BTYPE
) != VT_PTR
&&
2346 (sbt
& VT_BTYPE
) != VT_FUNC
) {
2347 /* need to convert from 32bit to 64bit */
2349 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
2350 #if defined(TCC_TARGET_ARM64)
2352 #elif defined(TCC_TARGET_X86_64)
2354 /* x86_64 specific: movslq */
2356 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
2363 } else if (dbt
== VT_BOOL
) {
2364 /* scalar to bool */
2367 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
2368 (dbt
& VT_BTYPE
) == VT_SHORT
) {
2369 if (sbt
== VT_PTR
) {
2370 vtop
->type
.t
= VT_INT
;
2371 tcc_warning("nonportable conversion from pointer to char/short");
2373 force_charshort_cast(dbt
);
2374 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
2376 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
2377 /* from long long: just take low order word */
2381 /* if lvalue and single word type, nothing to do because
2382 the lvalue already contains the real type size (see
2383 VT_LVAL_xxx constants) */
2386 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
2387 /* if we are casting between pointer types,
2388 we must update the VT_LVAL_xxx size */
2389 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
2390 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
2395 /* return type size as known at compile time. Put alignment at 'a' */
2396 ST_FUNC
int type_size(CType
*type
, int *a
)
2401 bt
= type
->t
& VT_BTYPE
;
2402 if (bt
== VT_STRUCT
) {
2407 } else if (bt
== VT_PTR
) {
2408 if (type
->t
& VT_ARRAY
) {
2412 ts
= type_size(&s
->type
, a
);
2414 if (ts
< 0 && s
->c
< 0)
2422 } else if (bt
== VT_LDOUBLE
) {
2424 return LDOUBLE_SIZE
;
2425 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
2426 #ifdef TCC_TARGET_I386
2427 #ifdef TCC_TARGET_PE
2432 #elif defined(TCC_TARGET_ARM)
2442 } else if (bt
== VT_INT
|| bt
== VT_FLOAT
) {
2445 } else if (bt
== VT_SHORT
) {
2448 } else if (bt
== VT_QLONG
|| bt
== VT_QFLOAT
) {
2451 } else if (bt
== VT_ENUM
) {
2453 /* Enums might be incomplete, so don't just return '4' here. */
2454 return type
->ref
->c
;
2456 /* char, void, function, _Bool */
2462 /* push type size as known at runtime time on top of value stack. Put
2464 ST_FUNC
void vla_runtime_type_size(CType
*type
, int *a
)
2466 if (type
->t
& VT_VLA
) {
2467 type_size(&type
->ref
->type
, a
);
2468 vset(&int_type
, VT_LOCAL
|VT_LVAL
, type
->ref
->c
);
2470 vpushi(type_size(type
, a
));
2474 static void vla_sp_restore(void) {
2475 if (vlas_in_scope
) {
2476 gen_vla_sp_restore(vla_sp_loc
);
2480 static void vla_sp_restore_root(void) {
2481 if (vlas_in_scope
) {
2482 gen_vla_sp_restore(vla_sp_root_loc
);
2486 /* return the pointed type of t */
2487 static inline CType
*pointed_type(CType
*type
)
2489 return &type
->ref
->type
;
2492 /* modify type so that its it is a pointer to type. */
2493 ST_FUNC
void mk_pointer(CType
*type
)
2496 s
= sym_push(SYM_FIELD
, type
, 0, -1);
2497 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
2501 /* compare function types. OLD functions match any new functions */
2502 static int is_compatible_func(CType
*type1
, CType
*type2
)
2508 if (!is_compatible_types(&s1
->type
, &s2
->type
))
2510 /* check func_call */
2511 if (s1
->a
.func_call
!= s2
->a
.func_call
)
2513 /* XXX: not complete */
2514 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
2518 while (s1
!= NULL
) {
2521 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
2531 /* return true if type1 and type2 are the same. If unqualified is
2532 true, qualifiers on the types are ignored.
2534 - enums are not checked as gcc __builtin_types_compatible_p ()
2536 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2540 t1
= type1
->t
& VT_TYPE
;
2541 t2
= type2
->t
& VT_TYPE
;
2543 /* strip qualifiers before comparing */
2544 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2545 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2547 /* Default Vs explicit signedness only matters for char */
2548 if ((t1
& VT_BTYPE
) != VT_BYTE
) {
2552 /* XXX: bitfields ? */
2555 /* test more complicated cases */
2556 bt1
= t1
& VT_BTYPE
;
2557 if (bt1
== VT_PTR
) {
2558 type1
= pointed_type(type1
);
2559 type2
= pointed_type(type2
);
2560 return is_compatible_types(type1
, type2
);
2561 } else if (bt1
== VT_STRUCT
) {
2562 return (type1
->ref
== type2
->ref
);
2563 } else if (bt1
== VT_FUNC
) {
2564 return is_compatible_func(type1
, type2
);
2570 /* return true if type1 and type2 are exactly the same (including
2573 static int is_compatible_types(CType
*type1
, CType
*type2
)
2575 return compare_types(type1
,type2
,0);
2578 /* return true if type1 and type2 are the same (ignoring qualifiers).
2580 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
2582 return compare_types(type1
,type2
,1);
2585 /* print a type. If 'varstr' is not NULL, then the variable is also
2586 printed in the type */
2588 /* XXX: add array and function pointers */
2589 static void type_to_str(char *buf
, int buf_size
,
2590 CType
*type
, const char *varstr
)
2597 t
= type
->t
& VT_TYPE
;
2600 if (t
& VT_CONSTANT
)
2601 pstrcat(buf
, buf_size
, "const ");
2602 if (t
& VT_VOLATILE
)
2603 pstrcat(buf
, buf_size
, "volatile ");
2604 if ((t
& (VT_DEFSIGN
| VT_UNSIGNED
)) == (VT_DEFSIGN
| VT_UNSIGNED
))
2605 pstrcat(buf
, buf_size
, "unsigned ");
2606 else if (t
& VT_DEFSIGN
)
2607 pstrcat(buf
, buf_size
, "signed ");
2637 tstr
= "long double";
2639 pstrcat(buf
, buf_size
, tstr
);
2643 if (bt
== VT_STRUCT
)
2647 pstrcat(buf
, buf_size
, tstr
);
2648 v
= type
->ref
->v
& ~SYM_STRUCT
;
2649 if (v
>= SYM_FIRST_ANOM
)
2650 pstrcat(buf
, buf_size
, "<anonymous>");
2652 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
2656 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
2657 pstrcat(buf
, buf_size
, "(");
2659 while (sa
!= NULL
) {
2660 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
2661 pstrcat(buf
, buf_size
, buf1
);
2664 pstrcat(buf
, buf_size
, ", ");
2666 pstrcat(buf
, buf_size
, ")");
2671 snprintf(buf1
, sizeof(buf1
), "%s[%ld]", varstr
? varstr
: "", s
->c
);
2672 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2675 pstrcpy(buf1
, sizeof(buf1
), "*");
2676 if (t
& VT_CONSTANT
)
2677 pstrcat(buf1
, buf_size
, "const ");
2678 if (t
& VT_VOLATILE
)
2679 pstrcat(buf1
, buf_size
, "volatile ");
2681 pstrcat(buf1
, sizeof(buf1
), varstr
);
2682 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2686 pstrcat(buf
, buf_size
, " ");
2687 pstrcat(buf
, buf_size
, varstr
);
2692 /* verify type compatibility to store vtop in 'dt' type, and generate
2694 static void gen_assign_cast(CType
*dt
)
2696 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
2697 char buf1
[256], buf2
[256];
2700 st
= &vtop
->type
; /* source type */
2701 dbt
= dt
->t
& VT_BTYPE
;
2702 sbt
= st
->t
& VT_BTYPE
;
2703 if (sbt
== VT_VOID
|| dbt
== VT_VOID
) {
2704 if (sbt
== VT_VOID
&& dbt
== VT_VOID
)
2706 It is Ok if both are void
2712 gcc accepts this program
2715 tcc_error("cannot cast from/to void");
2717 if (dt
->t
& VT_CONSTANT
)
2718 tcc_warning("assignment of read-only location");
2721 /* special cases for pointers */
2722 /* '0' can also be a pointer */
2723 if (is_null_pointer(vtop
))
2725 /* accept implicit pointer to integer cast with warning */
2726 if (is_integer_btype(sbt
)) {
2727 tcc_warning("assignment makes pointer from integer without a cast");
2730 type1
= pointed_type(dt
);
2731 /* a function is implicitely a function pointer */
2732 if (sbt
== VT_FUNC
) {
2733 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
2734 !is_compatible_types(pointed_type(dt
), st
))
2735 tcc_warning("assignment from incompatible pointer type");
2740 type2
= pointed_type(st
);
2741 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2742 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
2743 /* void * can match anything */
2745 /* exact type match, except for unsigned */
2748 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
|
2750 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
|
2752 if (!is_compatible_types(&tmp_type1
, &tmp_type2
))
2753 tcc_warning("assignment from incompatible pointer type");
2755 /* check const and volatile */
2756 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
2757 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
2758 tcc_warning("assignment discards qualifiers from pointer target type");
2764 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
2765 tcc_warning("assignment makes integer from pointer without a cast");
2766 } else if (sbt
== VT_STRUCT
) {
2767 goto case_VT_STRUCT
;
2769 /* XXX: more tests */
2775 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2776 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2777 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2779 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
2780 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
2781 tcc_error("cannot cast '%s' to '%s'", buf1
, buf2
);
2789 /* store vtop in lvalue pushed on stack */
2790 ST_FUNC
void vstore(void)
2792 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
2794 ft
= vtop
[-1].type
.t
;
2795 sbt
= vtop
->type
.t
& VT_BTYPE
;
2796 dbt
= ft
& VT_BTYPE
;
2797 if ((((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2798 (sbt
== VT_INT
&& dbt
== VT_SHORT
))
2799 && !(vtop
->type
.t
& VT_BITFIELD
)) {
2800 /* optimize char/short casts */
2801 delayed_cast
= VT_MUSTCAST
;
2802 vtop
->type
.t
= (ft
& VT_TYPE
& ~VT_BITFIELD
&
2803 ((1 << VT_STRUCT_SHIFT
) - 1));
2804 /* XXX: factorize */
2805 if (ft
& VT_CONSTANT
)
2806 tcc_warning("assignment of read-only location");
2809 if (!(ft
& VT_BITFIELD
))
2810 gen_assign_cast(&vtop
[-1].type
);
2813 if (sbt
== VT_STRUCT
) {
2814 /* if structure, only generate pointer */
2815 /* structure assignment : generate memcpy */
2816 /* XXX: optimize if small size */
2817 if (!nocode_wanted
) {
2818 size
= type_size(&vtop
->type
, &align
);
2822 vtop
->type
.t
= VT_PTR
;
2825 /* address of memcpy() */
2828 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
2829 else if(!(align
& 3))
2830 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
2833 /* Use memmove, rather than memcpy, as dest and src may be same: */
2834 vpush_global_sym(&func_old_type
, TOK_memmove
);
2839 vtop
->type
.t
= VT_PTR
;
2848 /* leave source on stack */
2849 } else if (ft
& VT_BITFIELD
) {
2850 /* bitfield store handling */
2852 /* save lvalue as expression result (example: s.b = s.a = n;) */
2853 vdup(), vtop
[-1] = vtop
[-2];
2855 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
2856 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
2857 /* remove bit field info to avoid loops */
2858 vtop
[-1].type
.t
= ft
& ~VT_BITFIELD
& ((1 << VT_STRUCT_SHIFT
) - 1);
2860 if((ft
& VT_BTYPE
) == VT_BOOL
) {
2861 gen_cast(&vtop
[-1].type
);
2862 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
2865 /* duplicate destination */
2867 vtop
[-1] = vtop
[-2];
2869 /* mask and shift source */
2870 if((ft
& VT_BTYPE
) != VT_BOOL
) {
2871 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2872 vpushll((1ULL << bit_size
) - 1ULL);
2874 vpushi((1 << bit_size
) - 1);
2880 /* load destination, mask and or with source */
2882 if((ft
& VT_BTYPE
) == VT_LLONG
) {
2883 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
2885 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
2891 /* ... and discard */
2895 if (!nocode_wanted
) {
2896 #ifdef CONFIG_TCC_BCHECK
2897 /* bound check case */
2898 if (vtop
[-1].r
& VT_MUSTBOUND
) {
2907 #ifdef TCC_TARGET_X86_64
2908 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
2910 } else if ((ft
& VT_BTYPE
) == VT_QFLOAT
) {
2915 r
= gv(rc
); /* generate value */
2916 /* if lvalue was saved on stack, must read it */
2917 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
2919 t
= get_reg(RC_INT
);
2920 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2925 sv
.r
= VT_LOCAL
| VT_LVAL
;
2926 sv
.c
.i
= vtop
[-1].c
.i
;
2928 vtop
[-1].r
= t
| VT_LVAL
;
2930 /* two word case handling : store second register at word + 4 (or +8 for x86-64) */
2931 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2932 if (((ft
& VT_BTYPE
) == VT_QLONG
) || ((ft
& VT_BTYPE
) == VT_QFLOAT
)) {
2933 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
2935 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
2936 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
2938 vtop
[-1].type
.t
= load_type
;
2941 /* convert to int to increment easily */
2942 vtop
->type
.t
= addr_type
;
2948 vtop
[-1].type
.t
= load_type
;
2949 /* XXX: it works because r2 is spilled last ! */
2950 store(vtop
->r2
, vtop
- 1);
2956 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
2957 vtop
->r
|= delayed_cast
;
2961 /* post defines POST/PRE add. c is the token ++ or -- */
2962 ST_FUNC
void inc(int post
, int c
)
2965 vdup(); /* save lvalue */
2968 gv_dup(); /* duplicate value */
2970 vdup(); /* duplicate value */
2975 vpushi(c
- TOK_MID
);
2977 vstore(); /* store value */
2979 vpop(); /* if post op, return saved value */
2982 /* Parse GNUC __attribute__ extension. Currently, the following
2983 extensions are recognized:
2984 - aligned(n) : set data/function alignment.
2985 - packed : force data alignment to 1
2986 - section(x) : generate data/code in this section.
2987 - unused : currently ignored, but may be used someday.
2988 - regparm(n) : pass function parameters in registers (i386 only)
2990 static void parse_attribute(AttributeDef
*ad
)
2994 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
2998 while (tok
!= ')') {
2999 if (tok
< TOK_IDENT
)
3000 expect("attribute name");
3008 expect("section name");
3009 ad
->section
= find_section(tcc_state
, (char *)tokc
.str
.data
);
3017 expect("alias(\"target\")");
3018 ad
->alias_target
= /* save string as token, for later */
3019 tok_alloc((char*)tokc
.str
.data
, tokc
.str
.size
-1)->tok
;
3023 case TOK_VISIBILITY1
:
3024 case TOK_VISIBILITY2
:
3027 expect("visibility(\"default|hidden|internal|protected\")");
3028 if (!strcmp (tokc
.str
.data
, "default"))
3029 ad
->a
.visibility
= STV_DEFAULT
;
3030 else if (!strcmp (tokc
.str
.data
, "hidden"))
3031 ad
->a
.visibility
= STV_HIDDEN
;
3032 else if (!strcmp (tokc
.str
.data
, "internal"))
3033 ad
->a
.visibility
= STV_INTERNAL
;
3034 else if (!strcmp (tokc
.str
.data
, "protected"))
3035 ad
->a
.visibility
= STV_PROTECTED
;
3037 expect("visibility(\"default|hidden|internal|protected\")");
3046 if (n
<= 0 || (n
& (n
- 1)) != 0)
3047 tcc_error("alignment must be a positive power of two");
3064 /* currently, no need to handle it because tcc does not
3065 track unused objects */
3069 /* currently, no need to handle it because tcc does not
3070 track unused objects */
3075 ad
->a
.func_call
= FUNC_CDECL
;
3080 ad
->a
.func_call
= FUNC_STDCALL
;
3082 #ifdef TCC_TARGET_I386
3092 ad
->a
.func_call
= FUNC_FASTCALL1
+ n
- 1;
3098 ad
->a
.func_call
= FUNC_FASTCALLW
;
3105 ad
->a
.mode
= VT_LLONG
+ 1;
3108 ad
->a
.mode
= VT_BYTE
+ 1;
3111 ad
->a
.mode
= VT_SHORT
+ 1;
3115 ad
->a
.mode
= VT_INT
+ 1;
3118 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
3125 ad
->a
.func_export
= 1;
3128 ad
->a
.func_import
= 1;
3131 if (tcc_state
->warn_unsupported
)
3132 tcc_warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
3133 /* skip parameters */
3135 int parenthesis
= 0;
3139 else if (tok
== ')')
3142 } while (parenthesis
&& tok
!= -1);
3155 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
3156 static void struct_decl(CType
*type
, AttributeDef
*ad
, int u
)
3158 int a
, v
, size
, align
, maxalign
, c
, offset
, flexible
;
3159 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
, prevbt
;
3160 Sym
*s
, *ss
, *ass
, **ps
;
3164 a
= tok
; /* save decl type */
3166 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
3167 parse_attribute(ad
);
3173 /* struct already defined ? return it */
3175 expect("struct/union/enum name");
3177 if (s
&& (s
->scope
== local_scope
|| (tok
!= '{' && tok
!= ';'))) {
3179 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
3187 /* we put an undefined size for struct/union */
3188 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
3189 s
->r
= 0; /* default alignment is zero as gcc */
3190 /* put struct/union/enum name in type */
3198 tcc_error("struct/union/enum already defined");
3199 /* cannot be empty */
3201 /* non empty enums are not allowed */
3202 if (a
== TOK_ENUM
) {
3206 expect("identifier");
3208 if (ss
&& !local_stack
)
3209 tcc_error("redefinition of enumerator '%s'",
3210 get_tok_str(v
, NULL
));
3216 /* enum symbols have static storage */
3217 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
3218 ss
->type
.t
|= VT_STATIC
;
3223 /* NOTE: we accept a trailing comma */
3227 s
->c
= type_size(&int_type
, &align
);
3236 while (tok
!= '}') {
3237 parse_btype(&btype
, &ad1
);
3240 tcc_error("flexible array member '%s' not at the end of struct",
3241 get_tok_str(v
, NULL
));
3246 type_decl(&type1
, &ad1
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3248 if ((type1
.t
& VT_BTYPE
) != VT_STRUCT
)
3249 expect("identifier");
3251 int v
= btype
.ref
->v
;
3252 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
3253 if (tcc_state
->ms_extensions
== 0)
3254 expect("identifier");
3258 if (type_size(&type1
, &align
) < 0) {
3259 if ((a
== TOK_STRUCT
) && (type1
.t
& VT_ARRAY
) && c
)
3262 tcc_error("field '%s' has incomplete type",
3263 get_tok_str(v
, NULL
));
3265 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
3266 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
3267 tcc_error("invalid type for '%s'",
3268 get_tok_str(v
, NULL
));
3272 bit_size
= expr_const();
3273 /* XXX: handle v = 0 case for messages */
3275 tcc_error("negative width in bit-field '%s'",
3276 get_tok_str(v
, NULL
));
3277 if (v
&& bit_size
== 0)
3278 tcc_error("zero width for bit-field '%s'",
3279 get_tok_str(v
, NULL
));
3281 size
= type_size(&type1
, &align
);
3282 if (ad1
.a
.aligned
) {
3283 if (align
< ad1
.a
.aligned
)
3284 align
= ad1
.a
.aligned
;
3285 } else if (ad1
.a
.packed
) {
3287 } else if (*tcc_state
->pack_stack_ptr
) {
3288 if (align
> *tcc_state
->pack_stack_ptr
)
3289 align
= *tcc_state
->pack_stack_ptr
;
3292 if (bit_size
>= 0) {
3293 bt
= type1
.t
& VT_BTYPE
;
3300 tcc_error("bitfields must have scalar type");
3302 if (bit_size
> bsize
) {
3303 tcc_error("width of '%s' exceeds its type",
3304 get_tok_str(v
, NULL
));
3305 } else if (bit_size
== bsize
) {
3306 /* no need for bit fields */
3308 } else if (bit_size
== 0) {
3309 /* XXX: what to do if only padding in a
3311 /* zero size: means to pad */
3314 /* we do not have enough room ?
3315 did the type change?
3317 if ((bit_pos
+ bit_size
) > bsize
||
3318 bt
!= prevbt
|| a
== TOK_UNION
)
3321 /* XXX: handle LSB first */
3322 type1
.t
|= VT_BITFIELD
|
3323 (bit_pos
<< VT_STRUCT_SHIFT
) |
3324 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
3325 bit_pos
+= bit_size
;
3331 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
3332 /* add new memory data only if starting
3334 if (lbit_pos
== 0) {
3335 if (a
== TOK_STRUCT
) {
3336 c
= (c
+ align
- 1) & -align
;
3345 if (align
> maxalign
)
3349 printf("add field %s offset=%d",
3350 get_tok_str(v
, NULL
), offset
);
3351 if (type1
.t
& VT_BITFIELD
) {
3352 printf(" pos=%d size=%d",
3353 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
3354 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
3359 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
3361 while ((ass
= ass
->next
) != NULL
) {
3362 ss
= sym_push(ass
->v
, &ass
->type
, 0, offset
+ ass
->c
);
3367 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
3371 if (tok
== ';' || tok
== TOK_EOF
)
3378 /* store size and alignment */
3379 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
3385 /* return 1 if basic type is a type size (short, long, long long) */
3386 ST_FUNC
int is_btype_size(int bt
)
3388 return bt
== VT_SHORT
|| bt
== VT_LONG
|| bt
== VT_LLONG
;
3391 /* Add type qualifiers to a type. If the type is an array then the qualifiers
3392 are added to the element type, copied because it could be a typedef. */
3393 static void parse_btype_qualify(CType
*type
, int qualifiers
)
3395 while (type
->t
& VT_ARRAY
) {
3396 type
->ref
= sym_push(SYM_FIELD
, &type
->ref
->type
, 0, type
->ref
->c
);
3397 type
= &type
->ref
->type
;
3399 type
->t
|= qualifiers
;
3402 /* return 0 if no type declaration. otherwise, return the basic type
3405 static int parse_btype(CType
*type
, AttributeDef
*ad
)
3407 int t
, u
, bt_size
, complete
, type_found
, typespec_found
;
3411 memset(ad
, 0, sizeof(AttributeDef
));
3419 /* currently, we really ignore extension */
3430 tcc_error("too many basic types");
3432 bt_size
= is_btype_size (u
& VT_BTYPE
);
3433 if (u
== VT_INT
|| (!bt_size
&& !(t
& VT_TYPEDEF
)))
3448 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
3449 #ifndef TCC_TARGET_PE
3450 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3452 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
3453 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3459 #ifdef TCC_TARGET_ARM64
3461 /* GCC's __uint128_t appears in some Linux header files. Make it a
3462 synonym for long double to get the size and alignment right. */
3474 if ((t
& VT_BTYPE
) == VT_LONG
) {
3475 #ifdef TCC_TARGET_PE
3476 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
3478 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3486 struct_decl(&type1
, ad
, VT_ENUM
);
3489 type
->ref
= type1
.ref
;
3493 struct_decl(&type1
, ad
, VT_STRUCT
);
3496 /* type modifiers */
3501 parse_btype_qualify(type
, VT_CONSTANT
);
3509 parse_btype_qualify(type
, VT_VOLATILE
);
3516 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == (VT_DEFSIGN
|VT_UNSIGNED
))
3517 tcc_error("signed and unsigned modifier");
3530 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == VT_DEFSIGN
)
3531 tcc_error("signed and unsigned modifier");
3532 t
|= VT_DEFSIGN
| VT_UNSIGNED
;
3557 /* GNUC attribute */
3558 case TOK_ATTRIBUTE1
:
3559 case TOK_ATTRIBUTE2
:
3560 parse_attribute(ad
);
3563 t
= (t
& ~VT_BTYPE
) | u
;
3571 parse_expr_type(&type1
);
3572 /* remove all storage modifiers except typedef */
3573 type1
.t
&= ~(VT_STORAGE
&~VT_TYPEDEF
);
3579 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
3582 type
->t
= ((s
->type
.t
& ~VT_TYPEDEF
) |
3583 (t
& ~(VT_CONSTANT
| VT_VOLATILE
)));
3584 type
->ref
= s
->type
.ref
;
3585 if (t
& (VT_CONSTANT
| VT_VOLATILE
))
3586 parse_btype_qualify(type
, t
& (VT_CONSTANT
| VT_VOLATILE
));
3590 /* get attributes from typedef */
3591 if (0 == ad
->a
.aligned
)
3592 ad
->a
.aligned
= s
->a
.aligned
;
3593 if (0 == ad
->a
.func_call
)
3594 ad
->a
.func_call
= s
->a
.func_call
;
3595 ad
->a
.packed
|= s
->a
.packed
;
3604 if (tcc_state
->char_is_unsigned
) {
3605 if ((t
& (VT_DEFSIGN
|VT_BTYPE
)) == VT_BYTE
)
3609 /* long is never used as type */
3610 if ((t
& VT_BTYPE
) == VT_LONG
)
3611 #if (!defined TCC_TARGET_X86_64 && !defined TCC_TARGET_ARM64) || \
3612 defined TCC_TARGET_PE
3613 t
= (t
& ~VT_BTYPE
) | VT_INT
;
3615 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3621 /* convert a function parameter type (array to pointer and function to
3622 function pointer) */
3623 static inline void convert_parameter_type(CType
*pt
)
3625 /* remove const and volatile qualifiers (XXX: const could be used
3626 to indicate a const function parameter */
3627 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
3628 /* array must be transformed to pointer according to ANSI C */
3630 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
3635 ST_FUNC
void parse_asm_str(CString
*astr
)
3638 /* read the string */
3640 expect("string constant");
3642 while (tok
== TOK_STR
) {
3643 /* XXX: add \0 handling too ? */
3644 cstr_cat(astr
, tokc
.str
.data
, -1);
3647 cstr_ccat(astr
, '\0');
3650 /* Parse an asm label and return the token */
3651 static int asm_label_instr(void)
3657 parse_asm_str(&astr
);
3660 printf("asm_alias: \"%s\"\n", (char *)astr
.data
);
3662 v
= tok_alloc(astr
.data
, astr
.size
- 1)->tok
;
3667 static void post_type(CType
*type
, AttributeDef
*ad
)
3669 int n
, l
, t1
, arg_size
, align
;
3670 Sym
**plast
, *s
, *first
;
3675 /* function declaration */
3683 /* read param name and compute offset */
3684 if (l
!= FUNC_OLD
) {
3685 if (!parse_btype(&pt
, &ad1
)) {
3687 tcc_error("invalid type");
3694 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
3696 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
3697 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
3698 tcc_error("parameter declared as void");
3699 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
3704 expect("identifier");
3708 convert_parameter_type(&pt
);
3709 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
3715 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
3722 /* if no parameters, then old type prototype */
3726 /* NOTE: const is ignored in returned type as it has a special
3727 meaning in gcc / C++ */
3728 type
->t
&= ~VT_CONSTANT
;
3729 /* some ancient pre-K&R C allows a function to return an array
3730 and the array brackets to be put after the arguments, such
3731 that "int c()[]" means something like "int[] c()" */
3734 skip(']'); /* only handle simple "[]" */
3737 /* we push a anonymous symbol which will contain the function prototype */
3738 ad
->a
.func_args
= arg_size
;
3739 s
= sym_push(SYM_FIELD
, type
, 0, l
);
3744 } else if (tok
== '[') {
3745 /* array definition */
3747 if (tok
== TOK_RESTRICT1
)
3752 if (!local_stack
|| nocode_wanted
)
3753 vpushi(expr_const());
3755 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3758 tcc_error("invalid array size");
3760 if (!is_integer_btype(vtop
->type
.t
& VT_BTYPE
))
3761 tcc_error("size of variable length array should be an integer");
3766 /* parse next post type */
3767 post_type(type
, ad
);
3768 if (type
->t
== VT_FUNC
)
3769 tcc_error("declaration of an array of functions");
3770 t1
|= type
->t
& VT_VLA
;
3773 loc
-= type_size(&int_type
, &align
);
3777 vla_runtime_type_size(type
, &align
);
3779 vset(&int_type
, VT_LOCAL
|VT_LVAL
, n
);
3786 /* we push an anonymous symbol which will contain the array
3788 s
= sym_push(SYM_FIELD
, type
, 0, n
);
3789 type
->t
= (t1
? VT_VLA
: VT_ARRAY
) | VT_PTR
;
3794 /* Parse a type declaration (except basic type), and return the type
3795 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3796 expected. 'type' should contain the basic type. 'ad' is the
3797 attribute definition of the basic type. It can be modified by
3800 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
3803 CType type1
, *type2
;
3804 int qualifiers
, storage
;
3806 while (tok
== '*') {
3814 qualifiers
|= VT_CONSTANT
;
3819 qualifiers
|= VT_VOLATILE
;
3827 type
->t
|= qualifiers
;
3830 /* XXX: clarify attribute handling */
3831 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3832 parse_attribute(ad
);
3834 /* recursive type */
3835 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3836 type1
.t
= 0; /* XXX: same as int */
3839 /* XXX: this is not correct to modify 'ad' at this point, but
3840 the syntax is not clear */
3841 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3842 parse_attribute(ad
);
3843 type_decl(&type1
, ad
, v
, td
);
3846 /* type identifier */
3847 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
3851 if (!(td
& TYPE_ABSTRACT
))
3852 expect("identifier");
3856 storage
= type
->t
& VT_STORAGE
;
3857 type
->t
&= ~VT_STORAGE
;
3858 if (storage
& VT_STATIC
) {
3859 int saved_nocode_wanted
= nocode_wanted
;
3861 post_type(type
, ad
);
3862 nocode_wanted
= saved_nocode_wanted
;
3864 post_type(type
, ad
);
3866 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
3867 parse_attribute(ad
);
3871 /* append type at the end of type1 */
3884 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3885 ST_FUNC
int lvalue_type(int t
)
3890 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
3892 else if (bt
== VT_SHORT
)
3896 if (t
& VT_UNSIGNED
)
3897 r
|= VT_LVAL_UNSIGNED
;
3901 /* indirection with full error checking and bound check */
3902 ST_FUNC
void indir(void)
3904 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
3905 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
3909 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
3911 vtop
->type
= *pointed_type(&vtop
->type
);
3912 /* Arrays and functions are never lvalues */
3913 if (!(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_VLA
)
3914 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
3915 vtop
->r
|= lvalue_type(vtop
->type
.t
);
3916 /* if bound checking, the referenced pointer must be checked */
3917 #ifdef CONFIG_TCC_BCHECK
3918 if (tcc_state
->do_bounds_check
)
3919 vtop
->r
|= VT_MUSTBOUND
;
3924 /* pass a parameter to a function and do type checking and casting */
3925 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
3930 func_type
= func
->c
;
3931 if (func_type
== FUNC_OLD
||
3932 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
3933 /* default casting : only need to convert float to double */
3934 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
3937 } else if (vtop
->type
.t
& VT_BITFIELD
) {
3938 type
.t
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
3941 } else if (arg
== NULL
) {
3942 tcc_error("too many arguments to function");
3945 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
3946 gen_assign_cast(&type
);
3950 /* parse an expression of the form '(type)' or '(expr)' and return its
3952 static void parse_expr_type(CType
*type
)
3958 if (parse_btype(type
, &ad
)) {
3959 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3966 static void parse_type(CType
*type
)
3971 if (!parse_btype(type
, &ad
)) {
3974 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
3977 static void vpush_tokc(int t
)
3982 vsetc(&type
, VT_CONST
, &tokc
);
3985 ST_FUNC
void unary(void)
3987 int n
, t
, align
, size
, r
, sizeof_caller
;
3992 sizeof_caller
= in_sizeof
;
3994 /* XXX: GCC 2.95.3 does not generate a table although it should be
4008 vpush_tokc(VT_INT
| VT_UNSIGNED
);
4012 vpush_tokc(VT_LLONG
);
4016 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
4020 vpush_tokc(VT_FLOAT
);
4024 vpush_tokc(VT_DOUBLE
);
4028 vpush_tokc(VT_LDOUBLE
);
4031 case TOK___FUNCTION__
:
4033 goto tok_identifier
;
4039 /* special function name identifier */
4040 len
= strlen(funcname
) + 1;
4041 /* generate char[len] type */
4046 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
4047 ptr
= section_ptr_add(data_section
, len
);
4048 memcpy(ptr
, funcname
, len
);
4053 #ifdef TCC_TARGET_PE
4054 t
= VT_SHORT
| VT_UNSIGNED
;
4060 /* string parsing */
4063 if (tcc_state
->warn_write_strings
)
4068 memset(&ad
, 0, sizeof(AttributeDef
));
4069 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, 0);
4074 if (parse_btype(&type
, &ad
)) {
4075 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
4077 /* check ISOC99 compound literal */
4079 /* data is allocated locally by default */
4084 /* all except arrays are lvalues */
4085 if (!(type
.t
& VT_ARRAY
))
4086 r
|= lvalue_type(type
.t
);
4087 memset(&ad
, 0, sizeof(AttributeDef
));
4088 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, 0);
4090 if (sizeof_caller
) {
4097 } else if (tok
== '{') {
4099 tcc_error("expected constant");
4100 /* save all registers */
4103 /* statement expression : we do not accept break/continue
4104 inside as GCC does */
4105 block(NULL
, NULL
, 1);
4120 /* functions names must be treated as function pointers,
4121 except for unary '&' and sizeof. Since we consider that
4122 functions are not lvalues, we only have to handle it
4123 there and in function calls. */
4124 /* arrays can also be used although they are not lvalues */
4125 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
4126 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
4128 mk_pointer(&vtop
->type
);
4134 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4136 boolean
.t
= VT_BOOL
;
4138 vtop
->c
.i
= !vtop
->c
.i
;
4139 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
4143 vseti(VT_JMP
, gvtst(1, 0));
4155 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
4156 tcc_error("pointer not accepted for unary plus");
4157 /* In order to force cast, we add zero, except for floating point
4158 where we really need an noop (otherwise -0.0 will be transformed
4160 if (!is_float(vtop
->type
.t
)) {
4171 unary_type(&type
); // Perform a in_sizeof = 0;
4172 size
= type_size(&type
, &align
);
4173 if (t
== TOK_SIZEOF
) {
4174 if (!(type
.t
& VT_VLA
)) {
4176 tcc_error("sizeof applied to an incomplete type");
4179 vla_runtime_type_size(&type
, &align
);
4184 vtop
->type
.t
|= VT_UNSIGNED
;
4187 case TOK_builtin_expect
:
4189 /* __builtin_expect is a no-op for now */
4190 int saved_nocode_wanted
;
4195 saved_nocode_wanted
= nocode_wanted
;
4199 nocode_wanted
= saved_nocode_wanted
;
4203 case TOK_builtin_types_compatible_p
:
4212 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
4213 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
4214 vpushi(is_compatible_types(&type1
, &type2
));
4217 case TOK_builtin_constant_p
:
4219 int saved_nocode_wanted
, res
;
4222 saved_nocode_wanted
= nocode_wanted
;
4225 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
4227 nocode_wanted
= saved_nocode_wanted
;
4232 case TOK_builtin_frame_address
:
4233 case TOK_builtin_return_address
:
4240 if (tok
!= TOK_CINT
) {
4241 tcc_error("%s only takes positive integers",
4242 tok1
== TOK_builtin_return_address
?
4243 "__builtin_return_address" :
4244 "__builtin_frame_address");
4246 level
= (uint32_t)tokc
.i
;
4251 vset(&type
, VT_LOCAL
, 0); /* local frame */
4253 mk_pointer(&vtop
->type
);
4254 indir(); /* -> parent frame */
4256 if (tok1
== TOK_builtin_return_address
) {
4257 // assume return address is just above frame pointer on stack
4260 mk_pointer(&vtop
->type
);
4265 #ifdef TCC_TARGET_X86_64
4266 #ifdef TCC_TARGET_PE
4267 case TOK_builtin_va_start
:
4275 if ((vtop
->r
& VT_VALMASK
) != VT_LOCAL
)
4276 tcc_error("__builtin_va_start expects a local variable");
4277 vtop
->r
&= ~(VT_LVAL
| VT_REF
);
4278 vtop
->type
= char_pointer_type
;
4284 case TOK_builtin_va_arg_types
:
4291 vpushi(classify_x86_64_va_arg(&type
));
4297 #ifdef TCC_TARGET_ARM64
4298 case TOK___va_start
: {
4300 tcc_error("statement in global scope");
4310 vtop
->type
.t
= VT_VOID
;
4313 case TOK___va_arg
: {
4316 tcc_error("statement in global scope");
4328 case TOK___arm64_clear_cache
: {
4337 vtop
->type
.t
= VT_VOID
;
4341 /* pre operations */
4352 t
= vtop
->type
.t
& VT_BTYPE
;
4354 /* In IEEE negate(x) isn't subtract(0,x), but rather
4359 else if (t
== VT_DOUBLE
)
4370 goto tok_identifier
;
4372 /* allow to take the address of a label */
4373 if (tok
< TOK_UIDENT
)
4374 expect("label identifier");
4375 s
= label_find(tok
);
4377 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4379 if (s
->r
== LABEL_DECLARED
)
4380 s
->r
= LABEL_FORWARD
;
4383 s
->type
.t
= VT_VOID
;
4384 mk_pointer(&s
->type
);
4385 s
->type
.t
|= VT_STATIC
;
4387 vpushsym(&s
->type
, s
);
4391 // special qnan , snan and infinity values
4393 vpush64(VT_DOUBLE
, 0x7ff8000000000000ULL
);
4397 vpush64(VT_DOUBLE
, 0x7ff0000000000001ULL
);
4401 vpush64(VT_DOUBLE
, 0x7ff0000000000000ULL
);
4410 expect("identifier");
4413 const char *name
= get_tok_str(t
, NULL
);
4415 tcc_error("'%s' undeclared", name
);
4416 /* for simple function calls, we tolerate undeclared
4417 external reference to int() function */
4418 if (tcc_state
->warn_implicit_function_declaration
4419 #ifdef TCC_TARGET_PE
4420 /* people must be warned about using undeclared WINAPI functions
4421 (which usually start with uppercase letter) */
4422 || (name
[0] >= 'A' && name
[0] <= 'Z')
4425 tcc_warning("implicit declaration of function '%s'", name
);
4426 s
= external_global_sym(t
, &func_old_type
, 0);
4428 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
4429 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
4430 /* if referencing an inline function, then we generate a
4431 symbol to it if not already done. It will have the
4432 effect to generate code for it at the end of the
4433 compilation unit. Inline function as always
4434 generated in the text section. */
4436 put_extern_sym(s
, text_section
, 0, 0);
4437 r
= VT_SYM
| VT_CONST
;
4441 vset(&s
->type
, r
, s
->c
);
4442 /* if forward reference, we must point to s */
4443 if (vtop
->r
& VT_SYM
) {
4450 /* post operations */
4452 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
4455 } else if (tok
== '.' || tok
== TOK_ARROW
|| tok
== TOK_CDOUBLE
) {
4458 if (tok
== TOK_ARROW
)
4460 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
4463 /* expect pointer on structure */
4464 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
4465 expect("struct or union");
4466 if (tok
== TOK_CDOUBLE
)
4467 expect("field name");
4469 if (tok
== TOK_CINT
|| tok
== TOK_CUINT
)
4470 expect("field name");
4474 while ((s
= s
->next
) != NULL
) {
4479 tcc_error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, &tokc
));
4480 /* add field offset to pointer */
4481 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
4484 /* change type to field type, and set to lvalue */
4485 vtop
->type
= s
->type
;
4486 vtop
->type
.t
|= qualifiers
;
4487 /* an array is never an lvalue */
4488 if (!(vtop
->type
.t
& VT_ARRAY
)) {
4489 vtop
->r
|= lvalue_type(vtop
->type
.t
);
4490 #ifdef CONFIG_TCC_BCHECK
4491 /* if bound checking, the referenced pointer must be checked */
4492 if (tcc_state
->do_bounds_check
)
4493 vtop
->r
|= VT_MUSTBOUND
;
4497 } else if (tok
== '[') {
4503 } else if (tok
== '(') {
4506 int nb_args
, ret_nregs
, ret_align
, regsize
, variadic
;
4509 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
4510 /* pointer test (no array accepted) */
4511 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
4512 vtop
->type
= *pointed_type(&vtop
->type
);
4513 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
4517 expect("function pointer");
4520 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
4522 /* get return type */
4525 sa
= s
->next
; /* first parameter */
4528 /* compute first implicit argument if a structure is returned */
4529 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
4530 variadic
= (s
->c
== FUNC_ELLIPSIS
);
4531 ret_nregs
= gfunc_sret(&s
->type
, variadic
, &ret
.type
,
4532 &ret_align
, ®size
);
4534 /* get some space for the returned structure */
4535 size
= type_size(&s
->type
, &align
);
4536 #ifdef TCC_TARGET_ARM64
4537 /* On arm64, a small struct is return in registers.
4538 It is much easier to write it to memory if we know
4539 that we are allowed to write some extra bytes, so
4540 round the allocated space up to a power of 2: */
4542 while (size
& (size
- 1))
4543 size
= (size
| (size
- 1)) + 1;
4545 loc
= (loc
- size
) & -align
;
4547 ret
.r
= VT_LOCAL
| VT_LVAL
;
4548 /* pass it as 'int' to avoid structure arg passing
4550 vseti(VT_LOCAL
, loc
);
4560 /* return in register */
4561 if (is_float(ret
.type
.t
)) {
4562 ret
.r
= reg_fret(ret
.type
.t
);
4563 #ifdef TCC_TARGET_X86_64
4564 if ((ret
.type
.t
& VT_BTYPE
) == VT_QFLOAT
)
4568 #ifndef TCC_TARGET_ARM64
4569 #ifdef TCC_TARGET_X86_64
4570 if ((ret
.type
.t
& VT_BTYPE
) == VT_QLONG
)
4572 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
4583 gfunc_param_typed(s
, sa
);
4593 tcc_error("too few arguments to function");
4595 if (!nocode_wanted
) {
4596 gfunc_call(nb_args
);
4598 vtop
-= (nb_args
+ 1);
4602 for (r
= ret
.r
+ ret_nregs
+ !ret_nregs
; r
-- > ret
.r
;) {
4603 vsetc(&ret
.type
, r
, &ret
.c
);
4604 vtop
->r2
= ret
.r2
; /* Loop only happens when r2 is VT_CONST */
4607 /* handle packed struct return */
4608 if (((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) && ret_nregs
) {
4611 size
= type_size(&s
->type
, &align
);
4612 /* We're writing whole regs often, make sure there's enough
4613 space. Assume register size is power of 2. */
4614 if (regsize
> align
)
4616 loc
= (loc
- size
) & -align
;
4620 vset(&ret
.type
, VT_LOCAL
| VT_LVAL
, addr
+ offset
);
4624 if (--ret_nregs
== 0)
4628 vset(&s
->type
, VT_LOCAL
| VT_LVAL
, addr
);
4636 ST_FUNC
void expr_prod(void)
4641 while (tok
== '*' || tok
== '/' || tok
== '%') {
4649 ST_FUNC
void expr_sum(void)
4654 while (tok
== '+' || tok
== '-') {
4662 static void expr_shift(void)
4667 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
4675 static void expr_cmp(void)
4680 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
4681 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
4689 static void expr_cmpeq(void)
4694 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
4702 static void expr_and(void)
4705 while (tok
== '&') {
4712 static void expr_xor(void)
4715 while (tok
== '^') {
4722 static void expr_or(void)
4725 while (tok
== '|') {
4732 /* XXX: fix this mess */
4733 static void expr_land_const(void)
4736 while (tok
== TOK_LAND
) {
4742 static void expr_lor_const(void)
4745 while (tok
== TOK_LOR
) {
4752 static void expr_land(void)
4755 if (tok
== TOK_LAND
) {
4756 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4767 int saved_nocode_wanted
= nocode_wanted
;
4771 nocode_wanted
= saved_nocode_wanted
;
4779 if (tok
!= TOK_LAND
) {
4790 static void expr_lor(void)
4793 if (tok
== TOK_LOR
) {
4794 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4801 int saved_nocode_wanted
= nocode_wanted
;
4805 nocode_wanted
= saved_nocode_wanted
;
4817 if (tok
!= TOK_LOR
) {
4828 static void expr_cond(void)
4830 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
, islv
;
4832 CType type
, type1
, type2
;
4837 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4838 int saved_nocode_wanted
= nocode_wanted
;
4841 boolean
.t
= VT_BOOL
;
4847 if (tok
!= ':' || !gnu_ext
) {
4855 nocode_wanted
= saved_nocode_wanted
;
4858 if (tok
!= ':' || !gnu_ext
) {
4862 nocode_wanted
= saved_nocode_wanted
;
4869 if (vtop
!= vstack
) {
4870 /* needed to avoid having different registers saved in
4872 if (is_float(vtop
->type
.t
)) {
4874 #ifdef TCC_TARGET_X86_64
4875 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4885 if (tok
== ':' && gnu_ext
) {
4893 sv
= *vtop
; /* save value to handle it later */
4894 vtop
--; /* no vpop so that FP stack is not flushed */
4902 bt1
= t1
& VT_BTYPE
;
4904 bt2
= t2
& VT_BTYPE
;
4905 /* cast operands to correct type according to ISOC rules */
4906 if (is_float(bt1
) || is_float(bt2
)) {
4907 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
4908 type
.t
= VT_LDOUBLE
;
4909 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
4914 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
4915 /* cast to biggest op */
4917 /* convert to unsigned if it does not fit in a long long */
4918 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
4919 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
4920 type
.t
|= VT_UNSIGNED
;
4921 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
4922 /* If one is a null ptr constant the result type
4924 if (is_null_pointer (vtop
))
4926 else if (is_null_pointer (&sv
))
4928 /* XXX: test pointer compatibility, C99 has more elaborate
4932 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
4933 /* XXX: test function pointer compatibility */
4934 type
= bt1
== VT_FUNC
? type1
: type2
;
4935 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
4936 /* XXX: test structure compatibility */
4937 type
= bt1
== VT_STRUCT
? type1
: type2
;
4938 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
4939 /* NOTE: as an extension, we accept void on only one side */
4942 /* integer operations */
4944 /* convert to unsigned if it does not fit in an integer */
4945 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
4946 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
4947 type
.t
|= VT_UNSIGNED
;
4949 /* keep structs lvalue by transforming `(expr ? a : b)` to `*(expr ? &a : &b)` so
4950 that `(expr ? a : b).mem` does not error with "lvalue expected" */
4951 islv
= (vtop
->r
& VT_LVAL
) && (sv
.r
& VT_LVAL
) && VT_STRUCT
== (type
.t
& VT_BTYPE
);
4953 /* now we convert second operand */
4956 mk_pointer(&vtop
->type
);
4959 else if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4962 if (is_float(type
.t
)) {
4964 #ifdef TCC_TARGET_X86_64
4965 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
4969 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
4970 /* for long longs, we use fixed registers to avoid having
4971 to handle a complicated move */
4976 /* this is horrible, but we must also convert first
4980 /* put again first value and cast it */
4984 mk_pointer(&vtop
->type
);
4987 else if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
4990 move_reg(r2
, r1
, type
.t
);
4999 static void expr_eq(void)
5005 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
5006 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
5007 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
5022 ST_FUNC
void gexpr(void)
5033 /* parse an expression and return its type without any side effect. */
5034 static void expr_type(CType
*type
)
5036 int saved_nocode_wanted
;
5038 saved_nocode_wanted
= nocode_wanted
;
5043 nocode_wanted
= saved_nocode_wanted
;
5046 /* parse a unary expression and return its type without any side
5048 static void unary_type(CType
*type
)
5060 /* parse a constant expression and return value in vtop. */
5061 static void expr_const1(void)
5070 /* parse an integer constant and return its value. */
5071 ST_FUNC
int expr_const(void)
5075 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
5076 expect("constant expression");
5082 /* return the label token if current token is a label, otherwise
5084 static int is_label(void)
5088 /* fast test first */
5089 if (tok
< TOK_UIDENT
)
5091 /* no need to save tokc because tok is an identifier */
5098 unget_tok(last_tok
);
5103 static void label_or_decl(int l
)
5107 /* fast test first */
5108 if (tok
>= TOK_UIDENT
)
5110 /* no need to save tokc because tok is an identifier */
5114 unget_tok(last_tok
);
5117 unget_tok(last_tok
);
5122 static int case_cmp(const void *pa
, const void *pb
)
5124 int a
= (*(struct case_t
**) pa
)->v1
;
5125 int b
= (*(struct case_t
**) pb
)->v1
;
5126 return a
< b
? -1 : a
> b
;
5129 static int gcase(struct case_t
**base
, int len
, int case_reg
, int *bsym
)
5141 case_reg
= gv(RC_INT
);
5147 gtst_addr(0, p
->sym
); /* v1 <= x <= v2 */
5148 case_reg
= gv(RC_INT
);
5151 case_reg
= gcase(base
, len
/2, case_reg
, bsym
);
5152 if (cur_switch
->def_sym
)
5153 gjmp_addr(cur_switch
->def_sym
);
5155 *bsym
= gjmp(*bsym
);
5159 base
+= e
; len
-= e
;
5167 if (p
->v1
== p
->v2
) {
5169 gtst_addr(0, p
->sym
);
5173 case_reg
= gv(RC_INT
);
5179 gtst_addr(0, p
->sym
);
5182 case_reg
= gv(RC_INT
);
5188 static void block(int *bsym
, int *csym
, int is_expr
)
5193 /* generate line number info */
5194 if (tcc_state
->do_debug
&&
5195 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
5196 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
5198 last_line_num
= file
->line_num
;
5202 /* default return value is (void) */
5204 vtop
->type
.t
= VT_VOID
;
5207 if (tok
== TOK_IF
) {
5214 block(bsym
, csym
, 0);
5216 if (c
== TOK_ELSE
) {
5220 block(bsym
, csym
, 0);
5221 gsym(d
); /* patch else jmp */
5224 } else if (tok
== TOK_WHILE
) {
5240 } else if (tok
== '{') {
5242 int block_vla_sp_loc
= vla_sp_loc
, saved_vlas_in_scope
= vlas_in_scope
;
5245 /* record local declaration stack position */
5247 llabel
= local_label_stack
;
5250 /* handle local labels declarations */
5251 if (tok
== TOK_LABEL
) {
5254 if (tok
< TOK_UIDENT
)
5255 expect("label identifier");
5256 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
5266 while (tok
!= '}') {
5267 label_or_decl(VT_LOCAL
);
5271 block(bsym
, csym
, is_expr
);
5274 /* pop locally defined labels */
5275 label_pop(&local_label_stack
, llabel
);
5277 /* XXX: this solution makes only valgrind happy...
5278 triggered by gcc.c-torture/execute/20000917-1.c */
5280 switch(vtop
->type
.t
& VT_BTYPE
) {
5282 /* this breaks a compilation of the linux kernel v2.4.26 */
5283 /* pmd_t *new = ({ __asm__ __volatile__("ud2\n") ; ((pmd_t *)1); }); */
5284 /* Look a commit a80acab: Display error on statement expressions with complex return type */
5285 /* A pointer is not a complex return type */
5289 for(p
=vtop
->type
.ref
;p
;p
=p
->prev
)
5291 tcc_error("unsupported expression type");
5294 /* pop locally defined symbols */
5296 sym_pop(&local_stack
, s
);
5298 /* Pop VLA frames and restore stack pointer if required */
5299 if (vlas_in_scope
> saved_vlas_in_scope
) {
5300 vla_sp_loc
= saved_vlas_in_scope
? block_vla_sp_loc
: vla_sp_root_loc
;
5303 vlas_in_scope
= saved_vlas_in_scope
;
5306 } else if (tok
== TOK_RETURN
) {
5310 gen_assign_cast(&func_vt
);
5311 #ifdef TCC_TARGET_ARM64
5312 // Perhaps it would be better to use this for all backends:
5315 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
5316 CType type
, ret_type
;
5317 int ret_align
, ret_nregs
, regsize
;
5318 ret_nregs
= gfunc_sret(&func_vt
, func_var
, &ret_type
,
5319 &ret_align
, ®size
);
5320 if (0 == ret_nregs
) {
5321 /* if returning structure, must copy it to implicit
5322 first pointer arg location */
5325 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
5328 /* copy structure value to pointer */
5331 /* returning structure packed into registers */
5332 int r
, size
, addr
, align
;
5333 size
= type_size(&func_vt
,&align
);
5334 if ((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) ||
5335 (vtop
->c
.i
& (ret_align
-1)))
5336 && (align
& (ret_align
-1))) {
5337 loc
= (loc
- size
) & -ret_align
;
5340 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
5344 vset(&ret_type
, VT_LOCAL
| VT_LVAL
, addr
);
5346 vtop
->type
= ret_type
;
5347 if (is_float(ret_type
.t
))
5348 r
= rc_fret(ret_type
.t
);
5359 if (--ret_nregs
== 0)
5361 /* We assume that when a structure is returned in multiple
5362 registers, their classes are consecutive values of the
5365 vtop
->c
.i
+= regsize
;
5369 } else if (is_float(func_vt
.t
)) {
5370 gv(rc_fret(func_vt
.t
));
5375 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
5378 /* jump unless last stmt in top-level block */
5379 if (tok
!= '}' || local_scope
!= 1)
5381 } else if (tok
== TOK_BREAK
) {
5384 tcc_error("cannot break");
5385 *bsym
= gjmp(*bsym
);
5388 } else if (tok
== TOK_CONTINUE
) {
5391 tcc_error("cannot continue");
5392 vla_sp_restore_root();
5393 *csym
= gjmp(*csym
);
5396 } else if (tok
== TOK_FOR
) {
5403 /* c99 for-loop init decl? */
5404 if (!decl0(VT_LOCAL
, 1)) {
5405 /* no, regular for-loop init expr */
5437 sym_pop(&local_stack
, s
);
5440 if (tok
== TOK_DO
) {
5457 if (tok
== TOK_SWITCH
) {
5458 struct switch_t
*saved
, sw
;
5462 /* XXX: other types than integer */
5467 b
= gjmp(0); /* jump to first case */
5468 sw
.p
= NULL
; sw
.n
= 0; sw
.def_sym
= 0;
5472 a
= gjmp(a
); /* add implicit break */
5475 qsort(sw
.p
, sw
.n
, sizeof(void*), case_cmp
);
5476 for (b
= 1; b
< sw
.n
; b
++)
5477 if (sw
.p
[b
- 1]->v2
>= sw
.p
[b
]->v1
)
5478 tcc_error("duplicate case value");
5479 gcase(sw
.p
, sw
.n
, c
, &a
);
5481 gjmp_addr(sw
.def_sym
);
5482 dynarray_reset(&sw
.p
, &sw
.n
);
5487 if (tok
== TOK_CASE
) {
5488 struct case_t
*cr
= tcc_malloc(sizeof(struct case_t
));
5492 cr
->v1
= cr
->v2
= expr_const();
5493 if (gnu_ext
&& tok
== TOK_DOTS
) {
5495 cr
->v2
= expr_const();
5496 if (cr
->v2
< cr
->v1
)
5497 tcc_warning("empty case range");
5500 dynarray_add((void***) &cur_switch
->p
, &cur_switch
->n
, cr
);
5503 goto block_after_label
;
5505 if (tok
== TOK_DEFAULT
) {
5510 if (cur_switch
->def_sym
)
5511 tcc_error("too many 'default'");
5512 cur_switch
->def_sym
= ind
;
5514 goto block_after_label
;
5516 if (tok
== TOK_GOTO
) {
5518 if (tok
== '*' && gnu_ext
) {
5522 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
5525 } else if (tok
>= TOK_UIDENT
) {
5526 s
= label_find(tok
);
5527 /* put forward definition if needed */
5529 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
5531 if (s
->r
== LABEL_DECLARED
)
5532 s
->r
= LABEL_FORWARD
;
5534 vla_sp_restore_root();
5535 if (s
->r
& LABEL_FORWARD
)
5536 s
->jnext
= gjmp(s
->jnext
);
5538 gjmp_addr(s
->jnext
);
5541 expect("label identifier");
5544 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
5552 if (s
->r
== LABEL_DEFINED
)
5553 tcc_error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
5555 s
->r
= LABEL_DEFINED
;
5557 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
5561 /* we accept this, but it is a mistake */
5564 tcc_warning("deprecated use of label at end of compound statement");
5568 block(bsym
, csym
, is_expr
);
5571 /* expression case */
5586 /* t is the array or struct type. c is the array or struct
5587 address. cur_index/cur_field is the pointer to the current
5588 value. 'size_only' is true if only size info is needed (only used
5590 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
5591 int *cur_index
, Sym
**cur_field
,
5595 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
5601 if (gnu_ext
&& (l
= is_label()) != 0)
5603 while (tok
== '[' || tok
== '.') {
5605 if (!(type
->t
& VT_ARRAY
))
5606 expect("array type");
5609 index
= expr_const();
5610 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
5611 expect("invalid index");
5612 if (tok
== TOK_DOTS
&& gnu_ext
) {
5614 index_last
= expr_const();
5615 if (index_last
< 0 ||
5616 (s
->c
>= 0 && index_last
>= s
->c
) ||
5618 expect("invalid index");
5624 *cur_index
= index_last
;
5625 type
= pointed_type(type
);
5626 elem_size
= type_size(type
, &align
);
5627 c
+= index
* elem_size
;
5628 /* NOTE: we only support ranges for last designator */
5629 nb_elems
= index_last
- index
+ 1;
5630 if (nb_elems
!= 1) {
5639 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
5640 expect("struct/union type");
5653 /* XXX: fix this mess by using explicit storage field */
5655 type1
.t
|= (type
->t
& ~VT_TYPE
);
5669 if (type
->t
& VT_ARRAY
) {
5671 type
= pointed_type(type
);
5672 c
+= index
* type_size(type
, &align
);
5676 tcc_error("too many field init");
5677 /* XXX: fix this mess by using explicit storage field */
5679 type1
.t
|= (type
->t
& ~VT_TYPE
);
5684 decl_initializer(type
, sec
, c
, 0, size_only
);
5686 /* XXX: make it more general */
5687 if (!size_only
&& nb_elems
> 1) {
5688 unsigned long c_end
;
5693 tcc_error("range init not supported yet for dynamic storage");
5694 c_end
= c
+ nb_elems
* elem_size
;
5695 if (c_end
> sec
->data_allocated
)
5696 section_realloc(sec
, c_end
);
5697 src
= sec
->data
+ c
;
5699 for(i
= 1; i
< nb_elems
; i
++) {
5701 memcpy(dst
, src
, elem_size
);
5707 #define EXPR_CONST 1
5710 /* store a value or an expression directly in global data or in local array */
5711 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
5712 int v
, int expr_type
)
5714 int saved_global_expr
, bt
, bit_pos
, bit_size
;
5716 unsigned long long bit_mask
;
5724 /* compound literals must be allocated globally in this case */
5725 saved_global_expr
= global_expr
;
5728 global_expr
= saved_global_expr
;
5729 /* NOTE: symbols are accepted */
5730 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
5731 tcc_error("initializer element is not constant");
5739 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
5742 /* XXX: not portable */
5743 /* XXX: generate error if incorrect relocation */
5744 gen_assign_cast(&dtype
);
5745 bt
= type
->t
& VT_BTYPE
;
5746 /* we'll write at most 16 bytes */
5747 if (c
+ 16 > sec
->data_allocated
) {
5748 section_realloc(sec
, c
+ 16);
5750 ptr
= sec
->data
+ c
;
5751 /* XXX: make code faster ? */
5752 if (!(type
->t
& VT_BITFIELD
)) {
5757 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
5758 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
5759 bit_mask
= (1LL << bit_size
) - 1;
5761 if ((vtop
->r
& VT_SYM
) &&
5767 (bt
== VT_INT
&& bit_size
!= 32)))
5768 tcc_error("initializer element is not computable at load time");
5770 /* XXX: when cross-compiling we assume that each type has the
5771 same representation on host and target, which is likely to
5772 be wrong in the case of long double */
5774 vtop
->c
.i
= (vtop
->c
.i
!= 0);
5776 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5779 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5782 *(double *)ptr
= vtop
->c
.d
;
5785 if (sizeof(long double) == LDOUBLE_SIZE
)
5786 *(long double *)ptr
= vtop
->c
.ld
;
5787 else if (sizeof(double) == LDOUBLE_SIZE
)
5788 *(double *)ptr
= vtop
->c
.ld
;
5790 tcc_error("can't cross compile long double constants");
5793 *(long long *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5796 addr_t val
= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5797 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
5798 if (vtop
->r
& VT_SYM
)
5799 greloca(sec
, vtop
->sym
, c
, R_DATA_PTR
, val
);
5801 *(addr_t
*)ptr
|= val
;
5803 if (vtop
->r
& VT_SYM
)
5804 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
5805 *(addr_t
*)ptr
|= val
;
5810 int val
= (vtop
->c
.i
& bit_mask
) << bit_pos
;
5811 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
5812 if (vtop
->r
& VT_SYM
)
5813 greloca(sec
, vtop
->sym
, c
, R_DATA_PTR
, val
);
5817 if (vtop
->r
& VT_SYM
)
5818 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
5826 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
5833 /* put zeros for variable based init */
5834 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
5837 /* nothing to do because globals are already set to zero */
5839 vpush_global_sym(&func_old_type
, TOK_memset
);
5841 #ifdef TCC_TARGET_ARM
5852 /* 't' contains the type and storage info. 'c' is the offset of the
5853 object in section 'sec'. If 'sec' is NULL, it means stack based
5854 allocation. 'first' is true if array '{' must be read (multi
5855 dimension implicit array init handling). 'size_only' is true if
5856 size only evaluation is wanted (only for arrays). */
5857 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
5858 int first
, int size_only
)
5860 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, parlevel1
, i
;
5861 int size1
, align1
, expr_type
;
5865 if (type
->t
& VT_VLA
) {
5868 /* save current stack pointer */
5869 if (vlas_in_scope
== 0) {
5870 if (vla_sp_root_loc
== -1)
5871 vla_sp_root_loc
= (loc
-= PTR_SIZE
);
5872 gen_vla_sp_save(vla_sp_root_loc
);
5875 vla_runtime_type_size(type
, &a
);
5876 gen_vla_alloc(type
, a
);
5880 } else if (type
->t
& VT_ARRAY
) {
5884 t1
= pointed_type(type
);
5885 size1
= type_size(t1
, &align1
);
5888 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
5891 tcc_error("character array initializer must be a literal,"
5892 " optionally enclosed in braces");
5897 /* only parse strings here if correct type (otherwise: handle
5898 them as ((w)char *) expressions */
5899 if ((tok
== TOK_LSTR
&&
5900 #ifdef TCC_TARGET_PE
5901 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
5903 (t1
->t
& VT_BTYPE
) == VT_INT
5905 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
5906 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
5909 /* compute maximum number of chars wanted */
5911 cstr_len
= tokc
.str
.size
;
5913 cstr_len
= tokc
.str
.size
/ sizeof(nwchar_t
);
5916 if (n
>= 0 && nb
> (n
- array_length
))
5917 nb
= n
- array_length
;
5920 tcc_warning("initializer-string for array is too long");
5921 /* in order to go faster for common case (char
5922 string in global variable, we handle it
5924 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
5925 memcpy(sec
->data
+ c
+ array_length
, tokc
.str
.data
, nb
);
5929 ch
= ((unsigned char *)tokc
.str
.data
)[i
];
5931 ch
= ((nwchar_t
*)tokc
.str
.data
)[i
];
5932 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
5940 /* only add trailing zero if enough storage (no
5941 warning in this case since it is standard) */
5942 if (n
< 0 || array_length
< n
) {
5944 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
5950 while (tok
!= '}') {
5951 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
5952 if (n
>= 0 && index
>= n
)
5953 tcc_error("index too large");
5954 /* must put zero in holes (note that doing it that way
5955 ensures that it even works with designators) */
5956 if (!size_only
&& array_length
< index
) {
5957 init_putz(t1
, sec
, c
+ array_length
* size1
,
5958 (index
- array_length
) * size1
);
5961 if (index
> array_length
)
5962 array_length
= index
;
5963 /* special test for multi dimensional arrays (may not
5964 be strictly correct if designators are used at the
5966 if (index
>= n
&& no_oblock
)
5975 /* put zeros at the end */
5976 if (!size_only
&& n
>= 0 && array_length
< n
) {
5977 init_putz(t1
, sec
, c
+ array_length
* size1
,
5978 (n
- array_length
) * size1
);
5980 /* patch type size if needed */
5982 s
->c
= array_length
;
5983 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
5984 (sec
|| !first
|| tok
== '{')) {
5986 /* NOTE: the previous test is a specific case for automatic
5987 struct/union init */
5988 /* XXX: union needs only one init */
5995 if (tcc_state
->old_struct_init_code
) {
5996 /* an old version of struct initialization.
5997 It have a problems. But with a new version
5998 linux 2.4.26 can't load ramdisk.
6000 while (tok
== '(') {
6004 if (!parse_btype(&type1
, &ad1
))
6006 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
6008 if (!is_assignable_types(type
, &type1
))
6009 tcc_error("invalid type for cast");
6016 if (!parse_btype(&type1
, &ad1
))
6018 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
6020 if (!is_assignable_types(type
, &type1
))
6021 tcc_error("invalid type for cast");
6030 if (first
|| tok
== '{') {
6039 while (tok
!= '}') {
6040 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
6042 if (!size_only
&& array_length
< index
) {
6043 init_putz(type
, sec
, c
+ array_length
,
6044 index
- array_length
);
6046 index
= index
+ type_size(&f
->type
, &align1
);
6047 if (index
> array_length
)
6048 array_length
= index
;
6050 /* gr: skip fields from same union - ugly. */
6053 int f_size
= type_size(&f
->type
, &align
);
6054 int f_type
= (f
->type
.t
& VT_BTYPE
);
6056 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
6057 /* test for same offset */
6058 if (f
->next
->c
!= f
->c
)
6060 if ((f_type
== VT_STRUCT
) && (f_size
== 0)) {
6062 Lets assume a structure of size 0 can't be a member of the union.
6063 This allow to compile the following code from a linux kernel v2.4.26
6064 typedef struct { } rwlock_t;
6070 struct fs_struct init_fs = { { (1) }, (rwlock_t) {}, 0022, };
6071 tcc-0.9.23 can succesfully compile this version of the kernel.
6072 gcc don't have problems with this code too.
6076 /* if yes, test for bitfield shift */
6077 if ((f
->type
.t
& VT_BITFIELD
) && (f
->next
->type
.t
& VT_BITFIELD
)) {
6078 int bit_pos_1
= (f
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
6079 int bit_pos_2
= (f
->next
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
6080 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
6081 if (bit_pos_1
!= bit_pos_2
)
6088 if (no_oblock
&& f
== NULL
)
6094 /* put zeros at the end */
6095 if (!size_only
&& array_length
< n
) {
6096 init_putz(type
, sec
, c
+ array_length
,
6105 } else if (tok
== '{') {
6107 decl_initializer(type
, sec
, c
, first
, size_only
);
6109 } else if (size_only
) {
6110 /* just skip expression */
6111 parlevel
= parlevel1
= 0;
6112 while ((parlevel
> 0 || parlevel1
> 0 ||
6113 (tok
!= '}' && tok
!= ',')) && tok
!= -1) {
6116 else if (tok
== ')') {
6117 if (parlevel
== 0 && parlevel1
== 0)
6121 else if (tok
== '{')
6123 else if (tok
== '}') {
6124 if (parlevel
== 0 && parlevel1
== 0)
6131 /* currently, we always use constant expression for globals
6132 (may change for scripting case) */
6133 expr_type
= EXPR_CONST
;
6135 expr_type
= EXPR_ANY
;
6136 init_putv(type
, sec
, c
, 0, expr_type
);
6140 /* parse an initializer for type 't' if 'has_init' is non zero, and
6141 allocate space in local or global data space ('r' is either
6142 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
6143 variable 'v' of scope 'scope' is declared before initializers
6144 are parsed. If 'v' is zero, then a reference to the new object
6145 is put in the value stack. If 'has_init' is 2, a special parsing
6146 is done to handle string constants. */
6147 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
6148 int has_init
, int v
, int scope
)
6150 int size
, align
, addr
, data_offset
;
6152 ParseState saved_parse_state
= {0};
6153 TokenString
*init_str
= NULL
;
6155 Sym
*flexible_array
;
6157 flexible_array
= NULL
;
6158 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
6159 Sym
*field
= type
->ref
->next
;
6162 field
= field
->next
;
6163 if (field
->type
.t
& VT_ARRAY
&& field
->type
.ref
->c
< 0)
6164 flexible_array
= field
;
6168 size
= type_size(type
, &align
);
6169 /* If unknown size, we must evaluate it before
6170 evaluating initializers because
6171 initializers can generate global data too
6172 (e.g. string pointers or ISOC99 compound
6173 literals). It also simplifies local
6174 initializers handling */
6175 if (size
< 0 || (flexible_array
&& has_init
)) {
6177 tcc_error("unknown type size");
6178 /* get all init string */
6179 init_str
= tok_str_alloc();
6180 if (has_init
== 2) {
6181 /* only get strings */
6182 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
6183 tok_str_add_tok(init_str
);
6188 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
6190 tcc_error("unexpected end of file in initializer");
6191 tok_str_add_tok(init_str
);
6194 else if (tok
== '}') {
6204 tok_str_add(init_str
, -1);
6205 tok_str_add(init_str
, 0);
6208 save_parse_state(&saved_parse_state
);
6210 begin_macro(init_str
, 1);
6212 decl_initializer(type
, NULL
, 0, 1, 1);
6213 /* prepare second initializer parsing */
6214 macro_ptr
= init_str
->str
;
6217 /* if still unknown size, error */
6218 size
= type_size(type
, &align
);
6220 tcc_error("unknown type size");
6222 /* If there's a flex member and it was used in the initializer
6224 if (flexible_array
&&
6225 flexible_array
->type
.ref
->c
> 0)
6226 size
+= flexible_array
->type
.ref
->c
6227 * pointed_size(&flexible_array
->type
);
6228 /* take into account specified alignment if bigger */
6229 if (ad
->a
.aligned
) {
6230 if (ad
->a
.aligned
> align
)
6231 align
= ad
->a
.aligned
;
6232 } else if (ad
->a
.packed
) {
6235 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
6237 #ifdef CONFIG_TCC_BCHECK
6238 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
6242 loc
= (loc
- size
) & -align
;
6244 #ifdef CONFIG_TCC_BCHECK
6245 /* handles bounds */
6246 /* XXX: currently, since we do only one pass, we cannot track
6247 '&' operators, so we add only arrays */
6248 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
6250 /* add padding between regions */
6252 /* then add local bound info */
6253 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(addr_t
));
6254 bounds_ptr
[0] = addr
;
6255 bounds_ptr
[1] = size
;
6259 /* local variable */
6260 sym_push(v
, type
, r
, addr
);
6262 /* push local reference */
6263 vset(type
, r
, addr
);
6269 if (v
&& scope
== VT_CONST
) {
6270 /* see if the symbol was already defined */
6273 if (!is_compatible_types(&sym
->type
, type
))
6274 tcc_error("incompatible types for redefinition of '%s'",
6275 get_tok_str(v
, NULL
));
6276 if (sym
->type
.t
& VT_EXTERN
) {
6277 /* if the variable is extern, it was not allocated */
6278 sym
->type
.t
&= ~VT_EXTERN
;
6279 /* set array size if it was omitted in extern
6281 if ((sym
->type
.t
& VT_ARRAY
) &&
6282 sym
->type
.ref
->c
< 0 &&
6284 sym
->type
.ref
->c
= type
->ref
->c
;
6286 /* we accept several definitions of the same
6287 global variable. this is tricky, because we
6288 must play with the SHN_COMMON type of the symbol */
6289 /* XXX: should check if the variable was already
6290 initialized. It is incorrect to initialized it
6292 /* no init data, we won't add more to the symbol */
6299 /* allocate symbol in corresponding section */
6304 else if (tcc_state
->nocommon
)
6308 data_offset
= sec
->data_offset
;
6309 data_offset
= (data_offset
+ align
- 1) & -align
;
6311 /* very important to increment global pointer at this time
6312 because initializers themselves can create new initializers */
6313 data_offset
+= size
;
6314 #ifdef CONFIG_TCC_BCHECK
6315 /* add padding if bound check */
6316 if (tcc_state
->do_bounds_check
)
6319 sec
->data_offset
= data_offset
;
6320 /* allocate section space to put the data */
6321 if (sec
->sh_type
!= SHT_NOBITS
&&
6322 data_offset
> sec
->data_allocated
)
6323 section_realloc(sec
, data_offset
);
6324 /* align section if needed */
6325 if (align
> sec
->sh_addralign
)
6326 sec
->sh_addralign
= align
;
6328 addr
= 0; /* avoid warning */
6332 if (scope
!= VT_CONST
|| !sym
) {
6333 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
6334 sym
->asm_label
= ad
->asm_label
;
6336 /* update symbol definition */
6338 put_extern_sym(sym
, sec
, addr
, size
);
6341 /* put a common area */
6342 put_extern_sym(sym
, NULL
, align
, size
);
6343 /* XXX: find a nicer way */
6344 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
6345 esym
->st_shndx
= SHN_COMMON
;
6348 /* push global reference */
6349 sym
= get_sym_ref(type
, sec
, addr
, size
);
6350 vpushsym(type
, sym
);
6352 /* patch symbol weakness */
6353 if (type
->t
& VT_WEAK
)
6355 apply_visibility(sym
, type
);
6356 #ifdef CONFIG_TCC_BCHECK
6357 /* handles bounds now because the symbol must be defined
6358 before for the relocation */
6359 if (tcc_state
->do_bounds_check
) {
6362 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
);
6363 /* then add global bound info */
6364 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(addr_t
));
6365 bounds_ptr
[0] = 0; /* relocated */
6366 bounds_ptr
[1] = size
;
6370 if (has_init
|| (type
->t
& VT_VLA
)) {
6371 decl_initializer(type
, sec
, addr
, 1, 0);
6372 /* patch flexible array member size back to -1, */
6373 /* for possible subsequent similar declarations */
6375 flexible_array
->type
.ref
->c
= -1;
6378 /* restore parse state if needed */
6381 restore_parse_state(&saved_parse_state
);
6385 static void put_func_debug(Sym
*sym
)
6390 /* XXX: we put here a dummy type */
6391 snprintf(buf
, sizeof(buf
), "%s:%c1",
6392 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
6393 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
6394 cur_text_section
, sym
->c
);
6395 /* //gr gdb wants a line at the function */
6396 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
6401 /* parse an old style function declaration list */
6402 /* XXX: check multiple parameter */
6403 static void func_decl_list(Sym
*func_sym
)
6410 /* parse each declaration */
6411 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
&&
6412 tok
!= TOK_ASM1
&& tok
!= TOK_ASM2
&& tok
!= TOK_ASM3
) {
6413 if (!parse_btype(&btype
, &ad
))
6414 expect("declaration list");
6415 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
6416 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
6418 /* we accept no variable after */
6422 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
6423 /* find parameter in function parameter list */
6426 if ((s
->v
& ~SYM_FIELD
) == v
)
6430 tcc_error("declaration for parameter '%s' but no such parameter",
6431 get_tok_str(v
, NULL
));
6433 /* check that no storage specifier except 'register' was given */
6434 if (type
.t
& VT_STORAGE
)
6435 tcc_error("storage class specified for '%s'", get_tok_str(v
, NULL
));
6436 convert_parameter_type(&type
);
6437 /* we can add the type (NOTE: it could be local to the function) */
6439 /* accept other parameters */
6450 /* parse a function defined by symbol 'sym' and generate its code in
6451 'cur_text_section' */
6452 static void gen_function(Sym
*sym
)
6454 int saved_nocode_wanted
= nocode_wanted
;
6457 ind
= cur_text_section
->data_offset
;
6458 /* NOTE: we patch the symbol size later */
6459 put_extern_sym(sym
, cur_text_section
, ind
, 0);
6460 funcname
= get_tok_str(sym
->v
, NULL
);
6462 /* Initialize VLA state */
6464 vla_sp_root_loc
= -1;
6465 /* put debug symbol */
6466 if (tcc_state
->do_debug
)
6467 put_func_debug(sym
);
6469 /* push a dummy symbol to enable local sym storage */
6470 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
6471 local_scope
= 1; /* for function parameters */
6472 gfunc_prolog(&sym
->type
);
6476 block(NULL
, NULL
, 0);
6479 cur_text_section
->data_offset
= ind
;
6480 label_pop(&global_label_stack
, NULL
);
6481 /* reset local stack */
6483 sym_pop(&local_stack
, NULL
);
6484 /* end of function */
6485 /* patch symbol size */
6486 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
6488 /* patch symbol weakness (this definition overrules any prototype) */
6489 if (sym
->type
.t
& VT_WEAK
)
6491 apply_visibility(sym
, &sym
->type
);
6492 if (tcc_state
->do_debug
) {
6493 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
6495 /* It's better to crash than to generate wrong code */
6496 cur_text_section
= NULL
;
6497 funcname
= ""; /* for safety */
6498 func_vt
.t
= VT_VOID
; /* for safety */
6499 func_var
= 0; /* for safety */
6500 ind
= 0; /* for safety */
6501 nocode_wanted
= saved_nocode_wanted
;
6505 ST_FUNC
void gen_inline_functions(void)
6508 int inline_generated
, i
, ln
;
6509 struct InlineFunc
*fn
;
6511 ln
= file
->line_num
;
6512 /* iterate while inline function are referenced */
6514 inline_generated
= 0;
6515 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
6516 fn
= tcc_state
->inline_fns
[i
];
6518 if (sym
&& sym
->c
) {
6519 /* the function was used: generate its code and
6520 convert it to a normal function */
6523 pstrcpy(file
->filename
, sizeof file
->filename
, fn
->filename
);
6524 sym
->r
= VT_SYM
| VT_CONST
;
6525 sym
->type
.t
&= ~VT_INLINE
;
6527 begin_macro(&fn
->func_str
, 0);
6529 cur_text_section
= text_section
;
6533 inline_generated
= 1;
6536 if (!inline_generated
)
6539 file
->line_num
= ln
;
6540 /* free tokens of unused inline functions */
6541 for (i
= 0; i
< tcc_state
->nb_inline_fns
; ++i
) {
6542 fn
= tcc_state
->inline_fns
[i
];
6544 tok_str_free(fn
->func_str
.str
);
6546 dynarray_reset(&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
);
6549 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
6550 static int decl0(int l
, int is_for_loop_init
)
6558 if (!parse_btype(&btype
, &ad
)) {
6559 if (is_for_loop_init
)
6561 /* skip redundant ';' */
6562 /* XXX: find more elegant solution */
6567 if (l
== VT_CONST
&&
6568 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
6569 /* global asm block */
6573 /* special test for old K&R protos without explicit int
6574 type. Only accepted when defining global data */
6575 if (l
== VT_LOCAL
|| tok
< TOK_UIDENT
)
6579 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
6580 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
6582 if ((btype
.t
& VT_BTYPE
) == VT_STRUCT
) {
6583 int v
= btype
.ref
->v
;
6584 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) >= SYM_FIRST_ANOM
)
6585 tcc_warning("unnamed struct/union that defines no instances");
6590 while (1) { /* iterate thru each declaration */
6592 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
6596 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
6597 printf("type = '%s'\n", buf
);
6600 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
6601 if ((type
.t
& VT_STATIC
) && (l
== VT_LOCAL
)) {
6602 tcc_error("function without file scope cannot be static");
6604 /* if old style function prototype, we accept a
6607 if (sym
->c
== FUNC_OLD
)
6608 func_decl_list(sym
);
6611 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
6612 ad
.asm_label
= asm_label_instr();
6613 /* parse one last attribute list, after asm label */
6614 parse_attribute(&ad
);
6621 #ifdef TCC_TARGET_PE
6622 if (ad
.a
.func_import
)
6623 type
.t
|= VT_IMPORT
;
6624 if (ad
.a
.func_export
)
6625 type
.t
|= VT_EXPORT
;
6627 type
.t
|= ad
.a
.visibility
<< VT_VIS_SHIFT
;
6631 tcc_error("cannot use local functions");
6632 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
6633 expect("function definition");
6635 /* reject abstract declarators in function definition */
6637 while ((sym
= sym
->next
) != NULL
)
6638 if (!(sym
->v
& ~SYM_FIELD
))
6639 expect("identifier");
6641 /* XXX: cannot do better now: convert extern line to static inline */
6642 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
6643 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
6648 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
6651 ref
= sym
->type
.ref
;
6652 if (0 == ref
->a
.func_proto
)
6653 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
6655 /* use func_call from prototype if not defined */
6656 if (ref
->a
.func_call
!= FUNC_CDECL
6657 && type
.ref
->a
.func_call
== FUNC_CDECL
)
6658 type
.ref
->a
.func_call
= ref
->a
.func_call
;
6660 /* use export from prototype */
6661 if (ref
->a
.func_export
)
6662 type
.ref
->a
.func_export
= 1;
6664 /* use static from prototype */
6665 if (sym
->type
.t
& VT_STATIC
)
6666 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
6668 /* If the definition has no visibility use the
6669 one from prototype. */
6670 if (! (type
.t
& VT_VIS_MASK
))
6671 type
.t
|= sym
->type
.t
& VT_VIS_MASK
;
6673 if (!is_compatible_types(&sym
->type
, &type
)) {
6675 tcc_error("incompatible types for redefinition of '%s'",
6676 get_tok_str(v
, NULL
));
6678 type
.ref
->a
.func_proto
= 0;
6679 /* if symbol is already defined, then put complete type */
6682 /* put function symbol */
6683 sym
= global_identifier_push(v
, type
.t
, 0);
6684 sym
->type
.ref
= type
.ref
;
6687 /* static inline functions are just recorded as a kind
6688 of macro. Their code will be emitted at the end of
6689 the compilation unit only if they are used */
6690 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
6691 (VT_INLINE
| VT_STATIC
)) {
6693 struct InlineFunc
*fn
;
6694 const char *filename
;
6696 filename
= file
? file
->filename
: "";
6697 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
6698 strcpy(fn
->filename
, filename
);
6700 tok_str_new(&fn
->func_str
);
6706 tcc_error("unexpected end of file");
6707 tok_str_add_tok(&fn
->func_str
);
6712 } else if (t
== '}') {
6714 if (block_level
== 0)
6718 tok_str_add(&fn
->func_str
, -1);
6719 tok_str_add(&fn
->func_str
, 0);
6720 dynarray_add((void ***)&tcc_state
->inline_fns
, &tcc_state
->nb_inline_fns
, fn
);
6723 /* compute text section */
6724 cur_text_section
= ad
.section
;
6725 if (!cur_text_section
)
6726 cur_text_section
= text_section
;
6727 sym
->r
= VT_SYM
| VT_CONST
;
6732 if (btype
.t
& VT_TYPEDEF
) {
6733 /* save typedefed type */
6734 /* XXX: test storage specifiers ? */
6736 if (sym
&& sym
->scope
== local_scope
) {
6737 if (!is_compatible_types(&sym
->type
, &type
)
6738 || !(sym
->type
.t
& VT_TYPEDEF
))
6739 tcc_error("incompatible redefinition of '%s'",
6740 get_tok_str(v
, NULL
));
6743 sym
= sym_push(v
, &type
, 0, 0);
6746 sym
->type
.t
|= VT_TYPEDEF
;
6749 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
6750 /* external function definition */
6751 /* specific case for func_call attribute */
6752 ad
.a
.func_proto
= 1;
6754 } else if (!(type
.t
& VT_ARRAY
)) {
6755 /* not lvalue if array */
6756 r
|= lvalue_type(type
.t
);
6758 has_init
= (tok
== '=');
6759 if (has_init
&& (type
.t
& VT_VLA
))
6760 tcc_error("Variable length array cannot be initialized");
6761 if ((btype
.t
& VT_EXTERN
) || ((type
.t
& VT_BTYPE
) == VT_FUNC
) ||
6762 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
6763 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
6764 /* external variable or function */
6765 /* NOTE: as GCC, uninitialized global static
6766 arrays of null size are considered as
6768 sym
= external_sym(v
, &type
, r
);
6769 sym
->asm_label
= ad
.asm_label
;
6771 if (ad
.alias_target
) {
6776 alias_target
= sym_find(ad
.alias_target
);
6777 if (!alias_target
|| !alias_target
->c
)
6778 tcc_error("unsupported forward __alias__ attribute");
6779 esym
= &((Elf32_Sym
*)symtab_section
->data
)[alias_target
->c
];
6780 tsec
.sh_num
= esym
->st_shndx
;
6781 put_extern_sym2(sym
, &tsec
, esym
->st_value
, esym
->st_size
, 0);
6784 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
6785 if (type
.t
& VT_STATIC
)
6791 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, l
);
6795 if (is_for_loop_init
)
6808 ST_FUNC
void decl(int l
)
6813 /* ------------------------------------------------------------------------- */