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
;
62 ST_DATA CType char_pointer_type
, func_old_type
, int_type
, size_type
;
64 ST_DATA
struct switch_t
{
68 } **p
; int n
; /* list of case ranges */
69 int def_sym
; /* default symbol */
70 } *cur_switch
; /* current switch */
72 /* ------------------------------------------------------------------------- */
74 static void gen_cast(CType
*type
);
75 static void gen_cast_s(int t
);
76 static inline CType
*pointed_type(CType
*type
);
77 static int is_compatible_types(CType
*type1
, CType
*type2
);
78 static int parse_btype(CType
*type
, AttributeDef
*ad
);
79 static CType
*type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
);
80 static void parse_expr_type(CType
*type
);
81 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
);
82 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
, int first
, int size_only
);
83 static void block(int *bsym
, int *csym
, int is_expr
);
84 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
, int has_init
, int v
, int scope
);
85 static int decl0(int l
, int is_for_loop_init
, Sym
*);
86 static void expr_eq(void);
87 static void vla_runtime_type_size(CType
*type
, int *a
);
88 static void vla_sp_restore(void);
89 static void vla_sp_restore_root(void);
90 static int is_compatible_unqualified_types(CType
*type1
, CType
*type2
);
91 static inline int64_t expr_const64(void);
92 ST_FUNC
void vpush64(int ty
, unsigned long long v
);
93 ST_FUNC
void vpush(CType
*type
);
94 ST_FUNC
int gvtst(int inv
, int t
);
95 static void gen_inline_functions(TCCState
*s
);
96 static void skip_or_save_block(TokenString
**str
);
97 static void gv_dup(void);
99 ST_INLN
int is_float(int t
)
103 return bt
== VT_LDOUBLE
|| bt
== VT_DOUBLE
|| bt
== VT_FLOAT
|| bt
== VT_QFLOAT
;
106 /* we use our own 'finite' function to avoid potential problems with
107 non standard math libs */
108 /* XXX: endianness dependent */
109 ST_FUNC
int ieee_finite(double d
)
112 memcpy(p
, &d
, sizeof(double));
113 return ((unsigned)((p
[1] | 0x800fffff) + 1)) >> 31;
116 ST_FUNC
void test_lvalue(void)
118 if (!(vtop
->r
& VT_LVAL
))
122 ST_FUNC
void check_vstack(void)
125 tcc_error("internal compiler error: vstack leak (%d)", vtop
- pvtop
);
128 /* ------------------------------------------------------------------------- */
129 /* vstack debugging aid */
132 void pv (const char *lbl
, int a
, int b
)
135 for (i
= a
; i
< a
+ b
; ++i
) {
136 SValue
*p
= &vtop
[-i
];
137 printf("%s vtop[-%d] : type.t:%04x r:%04x r2:%04x c.i:%d\n",
138 lbl
, i
, p
->type
.t
, p
->r
, p
->r2
, (int)p
->c
.i
);
143 /* ------------------------------------------------------------------------- */
144 /* start of translation unit info */
145 ST_FUNC
void tcc_debug_start(TCCState
*s1
)
150 /* file info: full path + filename */
151 section_sym
= put_elf_sym(symtab_section
, 0, 0,
152 ELFW(ST_INFO
)(STB_LOCAL
, STT_SECTION
), 0,
153 text_section
->sh_num
, NULL
);
154 getcwd(buf
, sizeof(buf
));
156 normalize_slashes(buf
);
158 pstrcat(buf
, sizeof(buf
), "/");
159 put_stabs_r(buf
, N_SO
, 0, 0,
160 text_section
->data_offset
, text_section
, section_sym
);
161 put_stabs_r(file
->filename
, N_SO
, 0, 0,
162 text_section
->data_offset
, text_section
, section_sym
);
167 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
168 symbols can be safely used */
169 put_elf_sym(symtab_section
, 0, 0,
170 ELFW(ST_INFO
)(STB_LOCAL
, STT_FILE
), 0,
171 SHN_ABS
, file
->filename
);
174 /* put end of translation unit info */
175 ST_FUNC
void tcc_debug_end(TCCState
*s1
)
179 put_stabs_r(NULL
, N_SO
, 0, 0,
180 text_section
->data_offset
, text_section
, section_sym
);
184 /* generate line number info */
185 ST_FUNC
void tcc_debug_line(TCCState
*s1
)
189 if ((last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
190 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
192 last_line_num
= file
->line_num
;
196 /* put function symbol */
197 ST_FUNC
void tcc_debug_funcstart(TCCState
*s1
, Sym
*sym
)
205 /* XXX: we put here a dummy type */
206 snprintf(buf
, sizeof(buf
), "%s:%c1",
207 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
208 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
209 cur_text_section
, sym
->c
);
210 /* //gr gdb wants a line at the function */
211 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
217 /* put function size */
218 ST_FUNC
void tcc_debug_funcend(TCCState
*s1
, int size
)
222 put_stabn(N_FUN
, 0, 0, size
);
225 /* ------------------------------------------------------------------------- */
226 ST_FUNC
void tccgen_start(TCCState
*s1
)
228 cur_text_section
= NULL
;
230 anon_sym
= SYM_FIRST_ANOM
;
235 /* define some often used types */
237 char_pointer_type
.t
= VT_BYTE
;
238 mk_pointer(&char_pointer_type
);
240 size_type
.t
= VT_INT
;
242 size_type
.t
= VT_LLONG
;
244 func_old_type
.t
= VT_FUNC
;
245 func_old_type
.ref
= sym_push(SYM_FIELD
, &int_type
, 0, 0);
246 func_old_type
.ref
->f
.func_call
= FUNC_CDECL
;
247 func_old_type
.ref
->f
.func_type
= FUNC_OLD
;
251 #ifdef TCC_TARGET_ARM
256 ST_FUNC
void tccgen_end(TCCState
*s1
)
258 gen_inline_functions(s1
);
260 /* end of translation unit info */
264 /* ------------------------------------------------------------------------- */
265 /* apply storage attributes to Elf symbol */
267 static void update_storage(Sym
*sym
)
272 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
273 if (sym
->a
.visibility
)
274 esym
->st_other
= (esym
->st_other
& ~ELFW(ST_VISIBILITY
)(-1))
277 esym
->st_info
= ELFW(ST_INFO
)(STB_WEAK
, ELFW(ST_TYPE
)(esym
->st_info
));
279 if (sym
->a
.dllimport
)
280 esym
->st_other
|= ST_PE_IMPORT
;
281 if (sym
->a
.dllexport
)
282 esym
->st_other
|= ST_PE_EXPORT
;
285 printf("storage %s: vis=%d weak=%d exp=%d imp=%d\n",
286 get_tok_str(sym
->v
, NULL
),
295 /* ------------------------------------------------------------------------- */
296 /* update sym->c so that it points to an external symbol in section
297 'section' with value 'value' */
299 ST_FUNC
void put_extern_sym2(Sym
*sym
, Section
*section
,
300 addr_t value
, unsigned long size
,
301 int can_add_underscore
)
303 int sym_type
, sym_bind
, sh_num
, info
, other
, t
;
307 #ifdef CONFIG_TCC_BCHECK
313 else if (section
== SECTION_ABS
)
316 sh_num
= section
->sh_num
;
319 name
= get_tok_str(sym
->v
, NULL
);
320 #ifdef CONFIG_TCC_BCHECK
321 if (tcc_state
->do_bounds_check
) {
322 /* XXX: avoid doing that for statics ? */
323 /* if bound checking is activated, we change some function
324 names by adding the "__bound" prefix */
327 /* XXX: we rely only on malloc hooks */
340 strcpy(buf
, "__bound_");
348 if ((t
& VT_BTYPE
) == VT_FUNC
) {
350 } else if ((t
& VT_BTYPE
) == VT_VOID
) {
351 sym_type
= STT_NOTYPE
;
353 sym_type
= STT_OBJECT
;
356 sym_bind
= STB_LOCAL
;
358 sym_bind
= STB_GLOBAL
;
361 if (sym_type
== STT_FUNC
&& sym
->type
.ref
) {
362 Sym
*ref
= sym
->type
.ref
;
363 if (ref
->f
.func_call
== FUNC_STDCALL
&& can_add_underscore
) {
364 sprintf(buf1
, "_%s@%d", name
, ref
->f
.func_args
* PTR_SIZE
);
366 other
|= ST_PE_STDCALL
;
367 can_add_underscore
= 0;
371 if (tcc_state
->leading_underscore
&& can_add_underscore
) {
373 pstrcpy(buf1
+ 1, sizeof(buf1
) - 1, name
);
377 name
= get_tok_str(sym
->asm_label
, NULL
);
378 info
= ELFW(ST_INFO
)(sym_bind
, sym_type
);
379 sym
->c
= set_elf_sym(symtab_section
, value
, size
, info
, other
, sh_num
, name
);
381 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
382 esym
->st_value
= value
;
383 esym
->st_size
= size
;
384 esym
->st_shndx
= sh_num
;
389 ST_FUNC
void put_extern_sym(Sym
*sym
, Section
*section
,
390 addr_t value
, unsigned long size
)
392 put_extern_sym2(sym
, section
, value
, size
, 1);
395 /* add a new relocation entry to symbol 'sym' in section 's' */
396 ST_FUNC
void greloca(Section
*s
, Sym
*sym
, unsigned long offset
, int type
,
401 if (nocode_wanted
&& s
== cur_text_section
)
406 put_extern_sym(sym
, NULL
, 0, 0);
410 /* now we can add ELF relocation info */
411 put_elf_reloca(symtab_section
, s
, offset
, type
, c
, addend
);
415 ST_FUNC
void greloc(Section
*s
, Sym
*sym
, unsigned long offset
, int type
)
417 greloca(s
, sym
, offset
, type
, 0);
421 /* ------------------------------------------------------------------------- */
422 /* symbol allocator */
423 static Sym
*__sym_malloc(void)
425 Sym
*sym_pool
, *sym
, *last_sym
;
428 sym_pool
= tcc_malloc(SYM_POOL_NB
* sizeof(Sym
));
429 dynarray_add(&sym_pools
, &nb_sym_pools
, sym_pool
);
431 last_sym
= sym_free_first
;
433 for(i
= 0; i
< SYM_POOL_NB
; i
++) {
434 sym
->next
= last_sym
;
438 sym_free_first
= last_sym
;
442 static inline Sym
*sym_malloc(void)
446 sym
= sym_free_first
;
448 sym
= __sym_malloc();
449 sym_free_first
= sym
->next
;
452 sym
= tcc_malloc(sizeof(Sym
));
457 ST_INLN
void sym_free(Sym
*sym
)
460 sym
->next
= sym_free_first
;
461 sym_free_first
= sym
;
467 /* push, without hashing */
468 ST_FUNC Sym
*sym_push2(Sym
**ps
, int v
, int t
, int c
)
473 memset(s
, 0, sizeof *s
);
483 /* find a symbol and return its associated structure. 's' is the top
484 of the symbol stack */
485 ST_FUNC Sym
*sym_find2(Sym
*s
, int v
)
497 /* structure lookup */
498 ST_INLN Sym
*struct_find(int v
)
501 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
503 return table_ident
[v
]->sym_struct
;
506 /* find an identifier */
507 ST_INLN Sym
*sym_find(int v
)
510 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
512 return table_ident
[v
]->sym_identifier
;
515 /* push a given symbol on the symbol stack */
516 ST_FUNC Sym
*sym_push(int v
, CType
*type
, int r
, int c
)
525 s
= sym_push2(ps
, v
, type
->t
, c
);
526 s
->type
.ref
= type
->ref
;
528 /* don't record fields or anonymous symbols */
530 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
531 /* record symbol in token array */
532 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
534 ps
= &ts
->sym_struct
;
536 ps
= &ts
->sym_identifier
;
539 s
->sym_scope
= local_scope
;
540 if (s
->prev_tok
&& s
->prev_tok
->sym_scope
== s
->sym_scope
)
541 tcc_error("redeclaration of '%s'",
542 get_tok_str(v
& ~SYM_STRUCT
, NULL
));
547 /* push a global identifier */
548 ST_FUNC Sym
*global_identifier_push(int v
, int t
, int c
)
551 s
= sym_push2(&global_stack
, v
, t
, c
);
552 /* don't record anonymous symbol */
553 if (v
< SYM_FIRST_ANOM
) {
554 ps
= &table_ident
[v
- TOK_IDENT
]->sym_identifier
;
555 /* modify the top most local identifier, so that
556 sym_identifier will point to 's' when popped */
558 ps
= &(*ps
)->prev_tok
;
565 /* pop symbols until top reaches 'b'. If KEEP is non-zero don't really
566 pop them yet from the list, but do remove them from the token array. */
567 ST_FUNC
void sym_pop(Sym
**ptop
, Sym
*b
, int keep
)
577 /* remove symbol in token array */
579 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
580 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
582 ps
= &ts
->sym_struct
;
584 ps
= &ts
->sym_identifier
;
595 /* ------------------------------------------------------------------------- */
597 static void vsetc(CType
*type
, int r
, CValue
*vc
)
601 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
602 tcc_error("memory full (vstack)");
603 /* cannot let cpu flags if other instruction are generated. Also
604 avoid leaving VT_JMP anywhere except on the top of the stack
605 because it would complicate the code generator.
607 Don't do this when nocode_wanted. vtop might come from
608 !nocode_wanted regions (see 88_codeopt.c) and transforming
609 it to a register without actually generating code is wrong
610 as their value might still be used for real. All values
611 we push under nocode_wanted will eventually be popped
612 again, so that the VT_CMP/VT_JMP value will be in vtop
613 when code is unsuppressed again.
615 Same logic below in vswap(); */
616 if (vtop
>= vstack
&& !nocode_wanted
) {
617 v
= vtop
->r
& VT_VALMASK
;
618 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
630 ST_FUNC
void vswap(void)
633 /* cannot vswap cpu flags. See comment at vsetc() above */
634 if (vtop
>= vstack
&& !nocode_wanted
) {
635 int v
= vtop
->r
& VT_VALMASK
;
636 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
644 /* pop stack value */
645 ST_FUNC
void vpop(void)
648 v
= vtop
->r
& VT_VALMASK
;
649 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
650 /* for x86, we need to pop the FP stack */
652 o(0xd8dd); /* fstp %st(0) */
655 if (v
== VT_JMP
|| v
== VT_JMPI
) {
656 /* need to put correct jump if && or || without test */
662 /* push constant of type "type" with useless value */
663 ST_FUNC
void vpush(CType
*type
)
665 vset(type
, VT_CONST
, 0);
668 /* push integer constant */
669 ST_FUNC
void vpushi(int v
)
673 vsetc(&int_type
, VT_CONST
, &cval
);
676 /* push a pointer sized constant */
677 static void vpushs(addr_t v
)
681 vsetc(&size_type
, VT_CONST
, &cval
);
684 /* push arbitrary 64bit constant */
685 ST_FUNC
void vpush64(int ty
, unsigned long long v
)
692 vsetc(&ctype
, VT_CONST
, &cval
);
695 /* push long long constant */
696 static inline void vpushll(long long v
)
698 vpush64(VT_LLONG
, v
);
701 ST_FUNC
void vset(CType
*type
, int r
, int v
)
706 vsetc(type
, r
, &cval
);
709 static void vseti(int r
, int v
)
717 ST_FUNC
void vpushv(SValue
*v
)
719 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
720 tcc_error("memory full (vstack)");
725 static void vdup(void)
730 /* rotate n first stack elements to the bottom
731 I1 ... In -> I2 ... In I1 [top is right]
733 ST_FUNC
void vrotb(int n
)
744 /* rotate the n elements before entry e towards the top
745 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
747 ST_FUNC
void vrote(SValue
*e
, int n
)
753 for(i
= 0;i
< n
- 1; i
++)
758 /* rotate n first stack elements to the top
759 I1 ... In -> In I1 ... I(n-1) [top is right]
761 ST_FUNC
void vrott(int n
)
766 /* push a symbol value of TYPE */
767 static inline void vpushsym(CType
*type
, Sym
*sym
)
771 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
775 /* Return a static symbol pointing to a section */
776 ST_FUNC Sym
*get_sym_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
782 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
783 sym
->type
.ref
= type
->ref
;
784 sym
->r
= VT_CONST
| VT_SYM
;
785 put_extern_sym(sym
, sec
, offset
, size
);
789 /* push a reference to a section offset by adding a dummy symbol */
790 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
792 vpushsym(type
, get_sym_ref(type
, sec
, offset
, size
));
795 /* define a new external reference to a symbol 'v' of type 'u' */
796 ST_FUNC Sym
*external_global_sym(int v
, CType
*type
, int r
)
802 /* push forward reference */
803 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
804 s
->type
.ref
= type
->ref
;
805 s
->r
= r
| VT_CONST
| VT_SYM
;
810 /* Merge some storage attributes. */
811 static void patch_storage(Sym
*sym
, AttributeDef
*ad
, CType
*type
)
813 if (type
&& !is_compatible_types(&sym
->type
, type
))
814 tcc_error("incompatible types for redefinition of '%s'",
815 get_tok_str(sym
->v
, NULL
));
817 if (sym
->a
.dllimport
!= ad
->a
.dllimport
)
818 tcc_error("incompatible dll linkage for redefinition of '%s'",
819 get_tok_str(sym
->v
, NULL
));
821 sym
->a
.dllexport
|= ad
->a
.dllexport
;
822 sym
->a
.weak
|= ad
->a
.weak
;
823 if (ad
->a
.visibility
) {
824 int vis
= sym
->a
.visibility
;
825 int vis2
= ad
->a
.visibility
;
826 if (vis
== STV_DEFAULT
)
828 else if (vis2
!= STV_DEFAULT
)
829 vis
= (vis
< vis2
) ? vis
: vis2
;
830 sym
->a
.visibility
= vis
;
833 sym
->a
.aligned
= ad
->a
.aligned
;
835 sym
->asm_label
= ad
->asm_label
;
839 /* define a new external reference to a symbol 'v' */
840 static Sym
*external_sym(int v
, CType
*type
, int r
, AttributeDef
*ad
)
845 /* push forward reference */
846 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
847 s
->type
.t
|= VT_EXTERN
;
851 if (s
->type
.ref
== func_old_type
.ref
) {
852 s
->type
.ref
= type
->ref
;
853 s
->r
= r
| VT_CONST
| VT_SYM
;
854 s
->type
.t
|= VT_EXTERN
;
856 patch_storage(s
, ad
, type
);
861 /* push a reference to global symbol v */
862 ST_FUNC
void vpush_global_sym(CType
*type
, int v
)
864 vpushsym(type
, external_global_sym(v
, type
, 0));
867 /* save registers up to (vtop - n) stack entry */
868 ST_FUNC
void save_regs(int n
)
871 for(p
= vstack
, p1
= vtop
- n
; p
<= p1
; p
++)
875 /* save r to the memory stack, and mark it as being free */
876 ST_FUNC
void save_reg(int r
)
878 save_reg_upstack(r
, 0);
881 /* save r to the memory stack, and mark it as being free,
882 if seen up to (vtop - n) stack entry */
883 ST_FUNC
void save_reg_upstack(int r
, int n
)
885 int l
, saved
, size
, align
;
889 if ((r
&= VT_VALMASK
) >= VT_CONST
)
894 /* modify all stack values */
897 for(p
= vstack
, p1
= vtop
- n
; p
<= p1
; p
++) {
898 if ((p
->r
& VT_VALMASK
) == r
||
899 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
900 /* must save value on stack if not already done */
902 /* NOTE: must reload 'r' because r might be equal to r2 */
903 r
= p
->r
& VT_VALMASK
;
904 /* store register in the stack */
906 if ((p
->r
& VT_LVAL
) ||
907 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
909 type
= &char_pointer_type
;
913 size
= type_size(type
, &align
);
914 loc
= (loc
- size
) & -align
;
916 sv
.r
= VT_LOCAL
| VT_LVAL
;
919 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
920 /* x86 specific: need to pop fp register ST0 if saved */
922 o(0xd8dd); /* fstp %st(0) */
926 /* special long long case */
927 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
935 /* mark that stack entry as being saved on the stack */
936 if (p
->r
& VT_LVAL
) {
937 /* also clear the bounded flag because the
938 relocation address of the function was stored in
940 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
942 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
950 #ifdef TCC_TARGET_ARM
951 /* find a register of class 'rc2' with at most one reference on stack.
952 * If none, call get_reg(rc) */
953 ST_FUNC
int get_reg_ex(int rc
, int rc2
)
958 for(r
=0;r
<NB_REGS
;r
++) {
959 if (reg_classes
[r
] & rc2
) {
962 for(p
= vstack
; p
<= vtop
; p
++) {
963 if ((p
->r
& VT_VALMASK
) == r
||
964 (p
->r2
& VT_VALMASK
) == r
)
975 /* find a free register of class 'rc'. If none, save one register */
976 ST_FUNC
int get_reg(int rc
)
981 /* find a free register */
982 for(r
=0;r
<NB_REGS
;r
++) {
983 if (reg_classes
[r
] & rc
) {
986 for(p
=vstack
;p
<=vtop
;p
++) {
987 if ((p
->r
& VT_VALMASK
) == r
||
988 (p
->r2
& VT_VALMASK
) == r
)
996 /* no register left : free the first one on the stack (VERY
997 IMPORTANT to start from the bottom to ensure that we don't
998 spill registers used in gen_opi()) */
999 for(p
=vstack
;p
<=vtop
;p
++) {
1000 /* look at second register (if long long) */
1001 r
= p
->r2
& VT_VALMASK
;
1002 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
1004 r
= p
->r
& VT_VALMASK
;
1005 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
1011 /* Should never comes here */
1015 /* move register 's' (of type 't') to 'r', and flush previous value of r to memory
1017 static void move_reg(int r
, int s
, int t
)
1031 /* get address of vtop (vtop MUST BE an lvalue) */
1032 ST_FUNC
void gaddrof(void)
1034 vtop
->r
&= ~VT_LVAL
;
1035 /* tricky: if saved lvalue, then we can go back to lvalue */
1036 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
1037 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
1042 #ifdef CONFIG_TCC_BCHECK
1043 /* generate lvalue bound code */
1044 static void gbound(void)
1049 vtop
->r
&= ~VT_MUSTBOUND
;
1050 /* if lvalue, then use checking code before dereferencing */
1051 if (vtop
->r
& VT_LVAL
) {
1052 /* if not VT_BOUNDED value, then make one */
1053 if (!(vtop
->r
& VT_BOUNDED
)) {
1054 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
1055 /* must save type because we must set it to int to get pointer */
1057 vtop
->type
.t
= VT_PTR
;
1060 gen_bounded_ptr_add();
1061 vtop
->r
|= lval_type
;
1064 /* then check for dereferencing */
1065 gen_bounded_ptr_deref();
1070 static void incr_bf_adr(int o
)
1072 vtop
->type
= char_pointer_type
;
1076 vtop
->type
.t
= (vtop
->type
.t
& ~(VT_BTYPE
|VT_DEFSIGN
))
1077 | (VT_BYTE
|VT_UNSIGNED
);
1078 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
1079 | (VT_LVAL_BYTE
|VT_LVAL_UNSIGNED
|VT_LVAL
);
1082 /* single-byte load mode for packed or otherwise unaligned bitfields */
1083 static void load_packed_bf(CType
*type
, int bit_pos
, int bit_size
)
1086 save_reg_upstack(vtop
->r
, 1);
1087 vpush64(type
->t
& VT_BTYPE
, 0); // B X
1088 bits
= 0, o
= bit_pos
>> 3, bit_pos
&= 7;
1097 vpushi(bit_pos
), gen_op(TOK_SHR
), bit_pos
= 0; // X B Y
1099 vpushi((1 << n
) - 1), gen_op('&');
1102 vpushi(bits
), gen_op(TOK_SHL
);
1105 bits
+= n
, bit_size
-= n
, o
= 1;
1108 if (!(type
->t
& VT_UNSIGNED
)) {
1109 n
= ((type
->t
& VT_BTYPE
) == VT_LLONG
? 64 : 32) - bits
;
1110 vpushi(n
), gen_op(TOK_SHL
);
1111 vpushi(n
), gen_op(TOK_SAR
);
1115 /* single-byte store mode for packed or otherwise unaligned bitfields */
1116 static void store_packed_bf(int bit_pos
, int bit_size
)
1118 int bits
, n
, o
, m
, c
;
1120 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1122 save_reg_upstack(vtop
->r
, 1);
1123 bits
= 0, o
= bit_pos
>> 3, bit_pos
&= 7;
1125 incr_bf_adr(o
); // X B
1127 c
? vdup() : gv_dup(); // B V X
1130 vpushi(bits
), gen_op(TOK_SHR
);
1132 vpushi(bit_pos
), gen_op(TOK_SHL
);
1137 m
= ((1 << n
) - 1) << bit_pos
;
1138 vpushi(m
), gen_op('&'); // X B V1
1139 vpushv(vtop
-1); // X B V1 B
1140 vpushi(m
& 0x80 ? ~m
& 0x7f : ~m
);
1141 gen_op('&'); // X B V1 B1
1142 gen_op('|'); // X B V2
1144 vdup(), vtop
[-1] = vtop
[-2]; // X B B V2
1145 vstore(), vpop(); // X B
1146 bits
+= n
, bit_size
-= n
, bit_pos
= 0, o
= 1;
1151 static int adjust_bf(SValue
*sv
, int bit_pos
, int bit_size
)
1154 if (0 == sv
->type
.ref
)
1156 t
= sv
->type
.ref
->auxtype
;
1157 if (t
!= -1 && t
!= VT_STRUCT
) {
1158 sv
->type
.t
= (sv
->type
.t
& ~VT_BTYPE
) | t
;
1159 sv
->r
= (sv
->r
& ~VT_LVAL_TYPE
) | lvalue_type(sv
->type
.t
);
1164 /* store vtop a register belonging to class 'rc'. lvalues are
1165 converted to values. Cannot be used if cannot be converted to
1166 register value (such as structures). */
1167 ST_FUNC
int gv(int rc
)
1169 int r
, bit_pos
, bit_size
, size
, align
, rc2
;
1171 /* NOTE: get_reg can modify vstack[] */
1172 if (vtop
->type
.t
& VT_BITFIELD
) {
1175 bit_pos
= BIT_POS(vtop
->type
.t
);
1176 bit_size
= BIT_SIZE(vtop
->type
.t
);
1177 /* remove bit field info to avoid loops */
1178 vtop
->type
.t
&= ~VT_STRUCT_MASK
;
1181 type
.t
= vtop
->type
.t
& VT_UNSIGNED
;
1182 if ((vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
1183 type
.t
|= VT_UNSIGNED
;
1185 r
= adjust_bf(vtop
, bit_pos
, bit_size
);
1187 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
)
1192 if (r
== VT_STRUCT
) {
1193 load_packed_bf(&type
, bit_pos
, bit_size
);
1195 int bits
= (type
.t
& VT_BTYPE
) == VT_LLONG
? 64 : 32;
1196 /* cast to int to propagate signedness in following ops */
1198 /* generate shifts */
1199 vpushi(bits
- (bit_pos
+ bit_size
));
1201 vpushi(bits
- bit_size
);
1202 /* NOTE: transformed to SHR if unsigned */
1207 if (is_float(vtop
->type
.t
) &&
1208 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
1209 unsigned long offset
;
1210 /* CPUs usually cannot use float constants, so we store them
1211 generically in data segment */
1212 size
= type_size(&vtop
->type
, &align
);
1213 offset
= section_add(data_section
, size
, align
);
1214 vpush_ref(&vtop
->type
, data_section
, offset
, size
);
1216 init_putv(&vtop
->type
, data_section
, offset
);
1219 #ifdef CONFIG_TCC_BCHECK
1220 if (vtop
->r
& VT_MUSTBOUND
)
1224 r
= vtop
->r
& VT_VALMASK
;
1225 rc2
= (rc
& RC_FLOAT
) ? RC_FLOAT
: RC_INT
;
1226 #ifndef TCC_TARGET_ARM64
1229 #ifdef TCC_TARGET_X86_64
1230 else if (rc
== RC_FRET
)
1234 /* need to reload if:
1236 - lvalue (need to dereference pointer)
1237 - already a register, but not in the right class */
1239 || (vtop
->r
& VT_LVAL
)
1240 || !(reg_classes
[r
] & rc
)
1242 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
1243 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
&& !(reg_classes
[vtop
->r2
] & rc2
))
1245 || ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
1251 if (((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
)) {
1252 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
1254 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
1255 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
1256 unsigned long long ll
;
1258 int r2
, original_type
;
1259 original_type
= vtop
->type
.t
;
1260 /* two register type load : expand to two words
1263 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
1266 vtop
->c
.i
= ll
; /* first word */
1268 vtop
->r
= r
; /* save register value */
1269 vpushi(ll
>> 32); /* second word */
1272 if (vtop
->r
& VT_LVAL
) {
1273 /* We do not want to modifier the long long
1274 pointer here, so the safest (and less
1275 efficient) is to save all the other registers
1276 in the stack. XXX: totally inefficient. */
1280 /* lvalue_save: save only if used further down the stack */
1281 save_reg_upstack(vtop
->r
, 1);
1283 /* load from memory */
1284 vtop
->type
.t
= load_type
;
1287 vtop
[-1].r
= r
; /* save register value */
1288 /* increment pointer to get second word */
1289 vtop
->type
.t
= addr_type
;
1294 vtop
->type
.t
= load_type
;
1296 /* move registers */
1299 vtop
[-1].r
= r
; /* save register value */
1300 vtop
->r
= vtop
[-1].r2
;
1302 /* Allocate second register. Here we rely on the fact that
1303 get_reg() tries first to free r2 of an SValue. */
1307 /* write second register */
1309 vtop
->type
.t
= original_type
;
1310 } else if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
1312 /* lvalue of scalar type : need to use lvalue type
1313 because of possible cast */
1316 /* compute memory access type */
1317 if (vtop
->r
& VT_LVAL_BYTE
)
1319 else if (vtop
->r
& VT_LVAL_SHORT
)
1321 if (vtop
->r
& VT_LVAL_UNSIGNED
)
1325 /* restore wanted type */
1328 /* one register type load */
1333 #ifdef TCC_TARGET_C67
1334 /* uses register pairs for doubles */
1335 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
1342 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
1343 ST_FUNC
void gv2(int rc1
, int rc2
)
1347 /* generate more generic register first. But VT_JMP or VT_CMP
1348 values must be generated first in all cases to avoid possible
1350 v
= vtop
[0].r
& VT_VALMASK
;
1351 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
1356 /* test if reload is needed for first register */
1357 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
1367 /* test if reload is needed for first register */
1368 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
1374 #ifndef TCC_TARGET_ARM64
1375 /* wrapper around RC_FRET to return a register by type */
1376 static int rc_fret(int t
)
1378 #ifdef TCC_TARGET_X86_64
1379 if (t
== VT_LDOUBLE
) {
1387 /* wrapper around REG_FRET to return a register by type */
1388 static int reg_fret(int t
)
1390 #ifdef TCC_TARGET_X86_64
1391 if (t
== VT_LDOUBLE
) {
1399 /* expand 64bit on stack in two ints */
1400 static void lexpand(void)
1403 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
1404 v
= vtop
->r
& (VT_VALMASK
| VT_LVAL
);
1405 if (v
== VT_CONST
) {
1408 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
1414 vtop
[0].r
= vtop
[-1].r2
;
1415 vtop
[0].r2
= vtop
[-1].r2
= VT_CONST
;
1417 vtop
[0].type
.t
= vtop
[-1].type
.t
= VT_INT
| u
;
1421 #ifdef TCC_TARGET_ARM
1422 /* expand long long on stack */
1423 ST_FUNC
void lexpand_nr(void)
1427 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
1429 vtop
->r2
= VT_CONST
;
1430 vtop
->type
.t
= VT_INT
| u
;
1431 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
1432 if (v
== VT_CONST
) {
1433 vtop
[-1].c
.i
= vtop
->c
.i
;
1434 vtop
->c
.i
= vtop
->c
.i
>> 32;
1436 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
1438 vtop
->r
= vtop
[-1].r
;
1439 } else if (v
> VT_CONST
) {
1443 vtop
->r
= vtop
[-1].r2
;
1444 vtop
[-1].r2
= VT_CONST
;
1445 vtop
[-1].type
.t
= VT_INT
| u
;
1450 /* build a long long from two ints */
1451 static void lbuild(int t
)
1453 gv2(RC_INT
, RC_INT
);
1454 vtop
[-1].r2
= vtop
[0].r
;
1455 vtop
[-1].type
.t
= t
;
1460 /* convert stack entry to register and duplicate its value in another
1462 static void gv_dup(void)
1469 if ((t
& VT_BTYPE
) == VT_LLONG
) {
1470 if (t
& VT_BITFIELD
) {
1480 /* stack: H L L1 H1 */
1490 /* duplicate value */
1495 #ifdef TCC_TARGET_X86_64
1496 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1506 load(r1
, &sv
); /* move r to r1 */
1508 /* duplicates value */
1514 /* Generate value test
1516 * Generate a test for any value (jump, comparison and integers) */
1517 ST_FUNC
int gvtst(int inv
, int t
)
1519 int v
= vtop
->r
& VT_VALMASK
;
1520 if (v
!= VT_CMP
&& v
!= VT_JMP
&& v
!= VT_JMPI
) {
1524 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1525 /* constant jmp optimization */
1526 if ((vtop
->c
.i
!= 0) != inv
)
1531 return gtst(inv
, t
);
1535 /* generate CPU independent (unsigned) long long operations */
1536 static void gen_opl(int op
)
1538 int t
, a
, b
, op1
, c
, i
;
1540 unsigned short reg_iret
= REG_IRET
;
1541 unsigned short reg_lret
= REG_LRET
;
1547 func
= TOK___divdi3
;
1550 func
= TOK___udivdi3
;
1553 func
= TOK___moddi3
;
1556 func
= TOK___umoddi3
;
1563 /* call generic long long function */
1564 vpush_global_sym(&func_old_type
, func
);
1569 vtop
->r2
= reg_lret
;
1577 //pv("gen_opl A",0,2);
1583 /* stack: L1 H1 L2 H2 */
1588 vtop
[-2] = vtop
[-3];
1591 /* stack: H1 H2 L1 L2 */
1592 //pv("gen_opl B",0,4);
1598 /* stack: H1 H2 L1 L2 ML MH */
1601 /* stack: ML MH H1 H2 L1 L2 */
1605 /* stack: ML MH H1 L2 H2 L1 */
1610 /* stack: ML MH M1 M2 */
1613 } else if (op
== '+' || op
== '-') {
1614 /* XXX: add non carry method too (for MIPS or alpha) */
1620 /* stack: H1 H2 (L1 op L2) */
1623 gen_op(op1
+ 1); /* TOK_xxxC2 */
1626 /* stack: H1 H2 (L1 op L2) */
1629 /* stack: (L1 op L2) H1 H2 */
1631 /* stack: (L1 op L2) (H1 op H2) */
1639 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1640 t
= vtop
[-1].type
.t
;
1644 /* stack: L H shift */
1646 /* constant: simpler */
1647 /* NOTE: all comments are for SHL. the other cases are
1648 done by swapping words */
1659 if (op
!= TOK_SAR
) {
1692 /* XXX: should provide a faster fallback on x86 ? */
1695 func
= TOK___ashrdi3
;
1698 func
= TOK___lshrdi3
;
1701 func
= TOK___ashldi3
;
1707 /* compare operations */
1713 /* stack: L1 H1 L2 H2 */
1715 vtop
[-1] = vtop
[-2];
1717 /* stack: L1 L2 H1 H2 */
1720 /* when values are equal, we need to compare low words. since
1721 the jump is inverted, we invert the test too. */
1724 else if (op1
== TOK_GT
)
1726 else if (op1
== TOK_ULT
)
1728 else if (op1
== TOK_UGT
)
1738 /* generate non equal test */
1744 /* compare low. Always unsigned */
1748 else if (op1
== TOK_LE
)
1750 else if (op1
== TOK_GT
)
1752 else if (op1
== TOK_GE
)
1763 static uint64_t gen_opic_sdiv(uint64_t a
, uint64_t b
)
1765 uint64_t x
= (a
>> 63 ? -a
: a
) / (b
>> 63 ? -b
: b
);
1766 return (a
^ b
) >> 63 ? -x
: x
;
1769 static int gen_opic_lt(uint64_t a
, uint64_t b
)
1771 return (a
^ (uint64_t)1 << 63) < (b
^ (uint64_t)1 << 63);
1774 /* handle integer constant optimizations and various machine
1776 static void gen_opic(int op
)
1778 SValue
*v1
= vtop
- 1;
1780 int t1
= v1
->type
.t
& VT_BTYPE
;
1781 int t2
= v2
->type
.t
& VT_BTYPE
;
1782 int c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1783 int c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1784 uint64_t l1
= c1
? v1
->c
.i
: 0;
1785 uint64_t l2
= c2
? v2
->c
.i
: 0;
1786 int shm
= (t1
== VT_LLONG
) ? 63 : 31;
1788 if (t1
!= VT_LLONG
&& (PTR_SIZE
!= 8 || t1
!= VT_PTR
))
1789 l1
= ((uint32_t)l1
|
1790 (v1
->type
.t
& VT_UNSIGNED
? 0 : -(l1
& 0x80000000)));
1791 if (t2
!= VT_LLONG
&& (PTR_SIZE
!= 8 || t2
!= VT_PTR
))
1792 l2
= ((uint32_t)l2
|
1793 (v2
->type
.t
& VT_UNSIGNED
? 0 : -(l2
& 0x80000000)));
1797 case '+': l1
+= l2
; break;
1798 case '-': l1
-= l2
; break;
1799 case '&': l1
&= l2
; break;
1800 case '^': l1
^= l2
; break;
1801 case '|': l1
|= l2
; break;
1802 case '*': l1
*= l2
; break;
1809 /* if division by zero, generate explicit division */
1812 tcc_error("division by zero in constant");
1816 default: l1
= gen_opic_sdiv(l1
, l2
); break;
1817 case '%': l1
= l1
- l2
* gen_opic_sdiv(l1
, l2
); break;
1818 case TOK_UDIV
: l1
= l1
/ l2
; break;
1819 case TOK_UMOD
: l1
= l1
% l2
; break;
1822 case TOK_SHL
: l1
<<= (l2
& shm
); break;
1823 case TOK_SHR
: l1
>>= (l2
& shm
); break;
1825 l1
= (l1
>> 63) ? ~(~l1
>> (l2
& shm
)) : l1
>> (l2
& shm
);
1828 case TOK_ULT
: l1
= l1
< l2
; break;
1829 case TOK_UGE
: l1
= l1
>= l2
; break;
1830 case TOK_EQ
: l1
= l1
== l2
; break;
1831 case TOK_NE
: l1
= l1
!= l2
; break;
1832 case TOK_ULE
: l1
= l1
<= l2
; break;
1833 case TOK_UGT
: l1
= l1
> l2
; break;
1834 case TOK_LT
: l1
= gen_opic_lt(l1
, l2
); break;
1835 case TOK_GE
: l1
= !gen_opic_lt(l1
, l2
); break;
1836 case TOK_LE
: l1
= !gen_opic_lt(l2
, l1
); break;
1837 case TOK_GT
: l1
= gen_opic_lt(l2
, l1
); break;
1839 case TOK_LAND
: l1
= l1
&& l2
; break;
1840 case TOK_LOR
: l1
= l1
|| l2
; break;
1844 if (t1
!= VT_LLONG
&& (PTR_SIZE
!= 8 || t1
!= VT_PTR
))
1845 l1
= ((uint32_t)l1
|
1846 (v1
->type
.t
& VT_UNSIGNED
? 0 : -(l1
& 0x80000000)));
1850 /* if commutative ops, put c2 as constant */
1851 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
1852 op
== '|' || op
== '*')) {
1854 c2
= c1
; //c = c1, c1 = c2, c2 = c;
1855 l2
= l1
; //l = l1, l1 = l2, l2 = l;
1857 if (!const_wanted
&&
1859 (op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
)) ||
1860 (l1
== -1 && op
== TOK_SAR
))) {
1861 /* treat (0 << x), (0 >> x) and (-1 >> x) as constant */
1863 } else if (!const_wanted
&&
1864 c2
&& ((l2
== 0 && (op
== '&' || op
== '*')) ||
1866 (l2
== -1 || (l2
== 0xFFFFFFFF && t2
!= VT_LLONG
))) ||
1867 (l2
== 1 && (op
== '%' || op
== TOK_UMOD
)))) {
1868 /* treat (x & 0), (x * 0), (x | -1) and (x % 1) as constant */
1873 } else if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
1876 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
1877 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
1880 (l2
== -1 || (l2
== 0xFFFFFFFF && t2
!= VT_LLONG
))))) {
1881 /* filter out NOP operations like x*1, x-0, x&-1... */
1883 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
1884 /* try to use shifts instead of muls or divs */
1885 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
1894 else if (op
== TOK_PDIV
)
1900 } else if (c2
&& (op
== '+' || op
== '-') &&
1901 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
1902 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
1903 /* symbol + constant case */
1907 /* The backends can't always deal with addends to symbols
1908 larger than +-1<<31. Don't construct such. */
1915 /* call low level op generator */
1916 if (t1
== VT_LLONG
|| t2
== VT_LLONG
||
1917 (PTR_SIZE
== 8 && (t1
== VT_PTR
|| t2
== VT_PTR
)))
1925 /* generate a floating point operation with constant propagation */
1926 static void gen_opif(int op
)
1930 #if defined _MSC_VER && defined _AMD64_
1931 /* avoid bad optimization with f1 -= f2 for f1:-0.0, f2:0.0 */
1938 /* currently, we cannot do computations with forward symbols */
1939 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1940 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1942 if (v1
->type
.t
== VT_FLOAT
) {
1945 } else if (v1
->type
.t
== VT_DOUBLE
) {
1953 /* NOTE: we only do constant propagation if finite number (not
1954 NaN or infinity) (ANSI spec) */
1955 if (!ieee_finite(f1
) || !ieee_finite(f2
))
1959 case '+': f1
+= f2
; break;
1960 case '-': f1
-= f2
; break;
1961 case '*': f1
*= f2
; break;
1965 tcc_error("division by zero in constant");
1970 /* XXX: also handles tests ? */
1974 /* XXX: overflow test ? */
1975 if (v1
->type
.t
== VT_FLOAT
) {
1977 } else if (v1
->type
.t
== VT_DOUBLE
) {
1989 static int pointed_size(CType
*type
)
1992 return type_size(pointed_type(type
), &align
);
1995 static void vla_runtime_pointed_size(CType
*type
)
1998 vla_runtime_type_size(pointed_type(type
), &align
);
2001 static inline int is_null_pointer(SValue
*p
)
2003 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
2005 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& (uint32_t)p
->c
.i
== 0) ||
2006 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.i
== 0) ||
2007 ((p
->type
.t
& VT_BTYPE
) == VT_PTR
&&
2008 (PTR_SIZE
== 4 ? (uint32_t)p
->c
.i
== 0 : p
->c
.i
== 0));
2011 static inline int is_integer_btype(int bt
)
2013 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
2014 bt
== VT_INT
|| bt
== VT_LLONG
);
2017 /* check types for comparison or subtraction of pointers */
2018 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
2020 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
2023 /* null pointers are accepted for all comparisons as gcc */
2024 if (is_null_pointer(p1
) || is_null_pointer(p2
))
2028 bt1
= type1
->t
& VT_BTYPE
;
2029 bt2
= type2
->t
& VT_BTYPE
;
2030 /* accept comparison between pointer and integer with a warning */
2031 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
2032 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
2033 tcc_warning("comparison between pointer and integer");
2037 /* both must be pointers or implicit function pointers */
2038 if (bt1
== VT_PTR
) {
2039 type1
= pointed_type(type1
);
2040 } else if (bt1
!= VT_FUNC
)
2041 goto invalid_operands
;
2043 if (bt2
== VT_PTR
) {
2044 type2
= pointed_type(type2
);
2045 } else if (bt2
!= VT_FUNC
) {
2047 tcc_error("invalid operands to binary %s", get_tok_str(op
, NULL
));
2049 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2050 (type2
->t
& VT_BTYPE
) == VT_VOID
)
2054 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2055 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2056 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2057 /* gcc-like error if '-' is used */
2059 goto invalid_operands
;
2061 tcc_warning("comparison of distinct pointer types lacks a cast");
2065 /* generic gen_op: handles types problems */
2066 ST_FUNC
void gen_op(int op
)
2068 int u
, t1
, t2
, bt1
, bt2
, t
;
2072 t1
= vtop
[-1].type
.t
;
2073 t2
= vtop
[0].type
.t
;
2074 bt1
= t1
& VT_BTYPE
;
2075 bt2
= t2
& VT_BTYPE
;
2077 if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
2078 tcc_error("operation on a struct");
2079 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
2080 if (bt2
== VT_FUNC
) {
2081 mk_pointer(&vtop
->type
);
2084 if (bt1
== VT_FUNC
) {
2086 mk_pointer(&vtop
->type
);
2091 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
2092 /* at least one operand is a pointer */
2093 /* relational op: must be both pointers */
2094 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
2095 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
2096 /* pointers are handled are unsigned */
2098 t
= VT_LLONG
| VT_UNSIGNED
;
2100 t
= VT_INT
| VT_UNSIGNED
;
2104 /* if both pointers, then it must be the '-' op */
2105 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
2107 tcc_error("cannot use pointers here");
2108 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
2109 /* XXX: check that types are compatible */
2110 if (vtop
[-1].type
.t
& VT_VLA
) {
2111 vla_runtime_pointed_size(&vtop
[-1].type
);
2113 vpushi(pointed_size(&vtop
[-1].type
));
2117 /* set to integer type */
2119 vtop
->type
.t
= VT_LLONG
;
2121 vtop
->type
.t
= VT_INT
;
2126 /* exactly one pointer : must be '+' or '-'. */
2127 if (op
!= '-' && op
!= '+')
2128 tcc_error("cannot use pointers here");
2129 /* Put pointer as first operand */
2130 if (bt2
== VT_PTR
) {
2132 t
= t1
, t1
= t2
, t2
= t
;
2135 if ((vtop
[0].type
.t
& VT_BTYPE
) == VT_LLONG
)
2136 /* XXX: truncate here because gen_opl can't handle ptr + long long */
2139 type1
= vtop
[-1].type
;
2140 type1
.t
&= ~VT_ARRAY
;
2141 if (vtop
[-1].type
.t
& VT_VLA
)
2142 vla_runtime_pointed_size(&vtop
[-1].type
);
2144 u
= pointed_size(&vtop
[-1].type
);
2146 tcc_error("unknown array element size");
2150 /* XXX: cast to int ? (long long case) */
2156 /* #ifdef CONFIG_TCC_BCHECK
2157 The main reason to removing this code:
2164 fprintf(stderr, "v+i-j = %p\n", v+i-j);
2165 fprintf(stderr, "v+(i-j) = %p\n", v+(i-j));
2167 When this code is on. then the output looks like
2169 v+(i-j) = 0xbff84000
2171 /* if evaluating constant expression, no code should be
2172 generated, so no bound check */
2173 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
2174 /* if bounded pointers, we generate a special code to
2181 gen_bounded_ptr_add();
2187 /* put again type if gen_opic() swaped operands */
2190 } else if (is_float(bt1
) || is_float(bt2
)) {
2191 /* compute bigger type and do implicit casts */
2192 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
2194 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
2199 /* floats can only be used for a few operations */
2200 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
2201 (op
< TOK_ULT
|| op
> TOK_GT
))
2202 tcc_error("invalid operands for binary operation");
2204 } else if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
) {
2205 t
= bt1
== VT_LLONG
? VT_LLONG
: VT_INT
;
2206 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (t
| VT_UNSIGNED
))
2209 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
2210 /* cast to biggest op */
2212 /* convert to unsigned if it does not fit in a long long */
2213 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_LLONG
| VT_UNSIGNED
) ||
2214 (t2
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_LLONG
| VT_UNSIGNED
))
2218 /* integer operations */
2220 /* convert to unsigned if it does not fit in an integer */
2221 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_INT
| VT_UNSIGNED
) ||
2222 (t2
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_INT
| VT_UNSIGNED
))
2225 /* XXX: currently, some unsigned operations are explicit, so
2226 we modify them here */
2227 if (t
& VT_UNSIGNED
) {
2234 else if (op
== TOK_LT
)
2236 else if (op
== TOK_GT
)
2238 else if (op
== TOK_LE
)
2240 else if (op
== TOK_GE
)
2248 /* special case for shifts and long long: we keep the shift as
2250 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
2257 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
2258 /* relational op: the result is an int */
2259 vtop
->type
.t
= VT_INT
;
2264 // Make sure that we have converted to an rvalue:
2265 if (vtop
->r
& VT_LVAL
)
2266 gv(is_float(vtop
->type
.t
& VT_BTYPE
) ? RC_FLOAT
: RC_INT
);
2269 #ifndef TCC_TARGET_ARM
2270 /* generic itof for unsigned long long case */
2271 static void gen_cvt_itof1(int t
)
2273 #ifdef TCC_TARGET_ARM64
2276 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
2277 (VT_LLONG
| VT_UNSIGNED
)) {
2280 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
2281 #if LDOUBLE_SIZE != 8
2282 else if (t
== VT_LDOUBLE
)
2283 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
2286 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
2290 vtop
->r
= reg_fret(t
);
2298 /* generic ftoi for unsigned long long case */
2299 static void gen_cvt_ftoi1(int t
)
2301 #ifdef TCC_TARGET_ARM64
2306 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
2307 /* not handled natively */
2308 st
= vtop
->type
.t
& VT_BTYPE
;
2310 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
2311 #if LDOUBLE_SIZE != 8
2312 else if (st
== VT_LDOUBLE
)
2313 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
2316 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
2321 vtop
->r2
= REG_LRET
;
2328 /* force char or short cast */
2329 static void force_charshort_cast(int t
)
2333 /* XXX: add optimization if lvalue : just change type and offset */
2338 if (t
& VT_UNSIGNED
) {
2339 vpushi((1 << bits
) - 1);
2342 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
)
2348 /* result must be signed or the SAR is converted to an SHL
2349 This was not the case when "t" was a signed short
2350 and the last value on the stack was an unsigned int */
2351 vtop
->type
.t
&= ~VT_UNSIGNED
;
2357 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
2358 static void gen_cast_s(int t
)
2366 static void gen_cast(CType
*type
)
2368 int sbt
, dbt
, sf
, df
, c
, p
;
2370 /* special delayed cast for char/short */
2371 /* XXX: in some cases (multiple cascaded casts), it may still
2373 if (vtop
->r
& VT_MUSTCAST
) {
2374 vtop
->r
&= ~VT_MUSTCAST
;
2375 force_charshort_cast(vtop
->type
.t
);
2378 /* bitfields first get cast to ints */
2379 if (vtop
->type
.t
& VT_BITFIELD
) {
2383 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
2384 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
2389 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
2390 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
2392 /* constant case: we can do it now */
2393 /* XXX: in ISOC, cannot do it if error in convert */
2394 if (sbt
== VT_FLOAT
)
2395 vtop
->c
.ld
= vtop
->c
.f
;
2396 else if (sbt
== VT_DOUBLE
)
2397 vtop
->c
.ld
= vtop
->c
.d
;
2400 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
2401 if ((sbt
& VT_UNSIGNED
) || !(vtop
->c
.i
>> 63))
2402 vtop
->c
.ld
= vtop
->c
.i
;
2404 vtop
->c
.ld
= -(long double)-vtop
->c
.i
;
2406 if ((sbt
& VT_UNSIGNED
) || !(vtop
->c
.i
>> 31))
2407 vtop
->c
.ld
= (uint32_t)vtop
->c
.i
;
2409 vtop
->c
.ld
= -(long double)-(uint32_t)vtop
->c
.i
;
2412 if (dbt
== VT_FLOAT
)
2413 vtop
->c
.f
= (float)vtop
->c
.ld
;
2414 else if (dbt
== VT_DOUBLE
)
2415 vtop
->c
.d
= (double)vtop
->c
.ld
;
2416 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
2417 vtop
->c
.i
= vtop
->c
.ld
;
2418 } else if (sf
&& dbt
== VT_BOOL
) {
2419 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
2422 vtop
->c
.i
= vtop
->c
.ld
;
2423 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
2425 else if (sbt
& VT_UNSIGNED
)
2426 vtop
->c
.i
= (uint32_t)vtop
->c
.i
;
2428 else if (sbt
== VT_PTR
)
2431 else if (sbt
!= VT_LLONG
)
2432 vtop
->c
.i
= ((uint32_t)vtop
->c
.i
|
2433 -(vtop
->c
.i
& 0x80000000));
2435 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
2437 else if (dbt
== VT_BOOL
)
2438 vtop
->c
.i
= (vtop
->c
.i
!= 0);
2440 else if (dbt
== VT_PTR
)
2443 else if (dbt
!= VT_LLONG
) {
2444 uint32_t m
= ((dbt
& VT_BTYPE
) == VT_BYTE
? 0xff :
2445 (dbt
& VT_BTYPE
) == VT_SHORT
? 0xffff :
2448 if (!(dbt
& VT_UNSIGNED
))
2449 vtop
->c
.i
|= -(vtop
->c
.i
& ((m
>> 1) + 1));
2452 } else if (p
&& dbt
== VT_BOOL
) {
2456 /* non constant case: generate code */
2458 /* convert from fp to fp */
2461 /* convert int to fp */
2464 /* convert fp to int */
2465 if (dbt
== VT_BOOL
) {
2469 /* we handle char/short/etc... with generic code */
2470 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
2471 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
2475 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
2476 /* additional cast for char/short... */
2482 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
2483 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
2484 /* scalar to long long */
2485 /* machine independent conversion */
2487 /* generate high word */
2488 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
2492 if (sbt
== VT_PTR
) {
2493 /* cast from pointer to int before we apply
2494 shift operation, which pointers don't support*/
2501 /* patch second register */
2502 vtop
[-1].r2
= vtop
->r
;
2506 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
2507 (dbt
& VT_BTYPE
) == VT_PTR
||
2508 (dbt
& VT_BTYPE
) == VT_FUNC
) {
2509 if ((sbt
& VT_BTYPE
) != VT_LLONG
&&
2510 (sbt
& VT_BTYPE
) != VT_PTR
&&
2511 (sbt
& VT_BTYPE
) != VT_FUNC
) {
2512 /* need to convert from 32bit to 64bit */
2514 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
2515 #if defined(TCC_TARGET_ARM64)
2517 #elif defined(TCC_TARGET_X86_64)
2519 /* x86_64 specific: movslq */
2521 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
2528 } else if (dbt
== VT_BOOL
) {
2529 /* scalar to bool */
2532 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
2533 (dbt
& VT_BTYPE
) == VT_SHORT
) {
2534 if (sbt
== VT_PTR
) {
2535 vtop
->type
.t
= VT_INT
;
2536 tcc_warning("nonportable conversion from pointer to char/short");
2538 force_charshort_cast(dbt
);
2540 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
2542 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
2543 /* from long long: just take low order word */
2547 /* if lvalue and single word type, nothing to do because
2548 the lvalue already contains the real type size (see
2549 VT_LVAL_xxx constants) */
2553 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
2554 /* if we are casting between pointer types,
2555 we must update the VT_LVAL_xxx size */
2556 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
2557 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
2562 /* return type size as known at compile time. Put alignment at 'a' */
2563 ST_FUNC
int type_size(CType
*type
, int *a
)
2568 bt
= type
->t
& VT_BTYPE
;
2569 if (bt
== VT_STRUCT
) {
2574 } else if (bt
== VT_PTR
) {
2575 if (type
->t
& VT_ARRAY
) {
2579 ts
= type_size(&s
->type
, a
);
2581 if (ts
< 0 && s
->c
< 0)
2589 } else if (IS_ENUM(type
->t
) && type
->ref
->c
== -1) {
2590 return -1; /* incomplete enum */
2591 } else if (bt
== VT_LDOUBLE
) {
2593 return LDOUBLE_SIZE
;
2594 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
2595 #ifdef TCC_TARGET_I386
2596 #ifdef TCC_TARGET_PE
2601 #elif defined(TCC_TARGET_ARM)
2611 } else if (bt
== VT_INT
|| bt
== VT_FLOAT
) {
2614 } else if (bt
== VT_SHORT
) {
2617 } else if (bt
== VT_QLONG
|| bt
== VT_QFLOAT
) {
2621 /* char, void, function, _Bool */
2627 /* push type size as known at runtime time on top of value stack. Put
2629 ST_FUNC
void vla_runtime_type_size(CType
*type
, int *a
)
2631 if (type
->t
& VT_VLA
) {
2632 type_size(&type
->ref
->type
, a
);
2633 vset(&int_type
, VT_LOCAL
|VT_LVAL
, type
->ref
->c
);
2635 vpushi(type_size(type
, a
));
2639 static void vla_sp_restore(void) {
2640 if (vlas_in_scope
) {
2641 gen_vla_sp_restore(vla_sp_loc
);
2645 static void vla_sp_restore_root(void) {
2646 if (vlas_in_scope
) {
2647 gen_vla_sp_restore(vla_sp_root_loc
);
2651 /* return the pointed type of t */
2652 static inline CType
*pointed_type(CType
*type
)
2654 return &type
->ref
->type
;
2657 /* modify type so that its it is a pointer to type. */
2658 ST_FUNC
void mk_pointer(CType
*type
)
2661 s
= sym_push(SYM_FIELD
, type
, 0, -1);
2662 type
->t
= VT_PTR
| (type
->t
& VT_STORAGE
);
2666 /* compare function types. OLD functions match any new functions */
2667 static int is_compatible_func(CType
*type1
, CType
*type2
)
2673 if (!is_compatible_types(&s1
->type
, &s2
->type
))
2675 /* check func_call */
2676 if (s1
->f
.func_call
!= s2
->f
.func_call
)
2678 /* XXX: not complete */
2679 if (s1
->f
.func_type
== FUNC_OLD
|| s2
->f
.func_type
== FUNC_OLD
)
2681 if (s1
->f
.func_type
!= s2
->f
.func_type
)
2683 while (s1
!= NULL
) {
2686 if (!is_compatible_unqualified_types(&s1
->type
, &s2
->type
))
2696 /* return true if type1 and type2 are the same. If unqualified is
2697 true, qualifiers on the types are ignored.
2699 - enums are not checked as gcc __builtin_types_compatible_p ()
2701 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2705 t1
= type1
->t
& VT_TYPE
;
2706 t2
= type2
->t
& VT_TYPE
;
2708 /* strip qualifiers before comparing */
2709 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2710 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2712 /* Default Vs explicit signedness only matters for char */
2713 if ((t1
& VT_BTYPE
) != VT_BYTE
) {
2718 /* XXX: bitfields ? */
2721 /* test more complicated cases */
2722 bt1
= t1
& VT_BTYPE
;
2723 if (bt1
== VT_PTR
) {
2724 type1
= pointed_type(type1
);
2725 type2
= pointed_type(type2
);
2726 return is_compatible_types(type1
, type2
);
2727 } else if (bt1
== VT_STRUCT
) {
2728 return (type1
->ref
== type2
->ref
);
2729 } else if (bt1
== VT_FUNC
) {
2730 return is_compatible_func(type1
, type2
);
2736 /* return true if type1 and type2 are exactly the same (including
2739 static int is_compatible_types(CType
*type1
, CType
*type2
)
2741 return compare_types(type1
,type2
,0);
2744 /* return true if type1 and type2 are the same (ignoring qualifiers).
2746 static int is_compatible_unqualified_types(CType
*type1
, CType
*type2
)
2748 return compare_types(type1
,type2
,1);
2751 /* print a type. If 'varstr' is not NULL, then the variable is also
2752 printed in the type */
2754 /* XXX: add array and function pointers */
2755 static void type_to_str(char *buf
, int buf_size
,
2756 CType
*type
, const char *varstr
)
2766 if (t
& VT_CONSTANT
)
2767 pstrcat(buf
, buf_size
, "const ");
2768 if (t
& VT_VOLATILE
)
2769 pstrcat(buf
, buf_size
, "volatile ");
2770 if ((t
& (VT_DEFSIGN
| VT_UNSIGNED
)) == (VT_DEFSIGN
| VT_UNSIGNED
))
2771 pstrcat(buf
, buf_size
, "unsigned ");
2772 else if (t
& VT_DEFSIGN
)
2773 pstrcat(buf
, buf_size
, "signed ");
2775 pstrcat(buf
, buf_size
, "extern ");
2777 pstrcat(buf
, buf_size
, "static ");
2779 pstrcat(buf
, buf_size
, "typedef ");
2781 pstrcat(buf
, buf_size
, "inline ");
2782 buf_size
-= strlen(buf
);
2817 tstr
= "long double";
2819 pstrcat(buf
, buf_size
, tstr
);
2826 pstrcat(buf
, buf_size
, tstr
);
2827 v
= type
->ref
->v
& ~SYM_STRUCT
;
2828 if (v
>= SYM_FIRST_ANOM
)
2829 pstrcat(buf
, buf_size
, "<anonymous>");
2831 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
2835 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
2836 pstrcat(buf
, buf_size
, "(");
2838 while (sa
!= NULL
) {
2839 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
2840 pstrcat(buf
, buf_size
, buf1
);
2843 pstrcat(buf
, buf_size
, ", ");
2845 pstrcat(buf
, buf_size
, ")");
2850 snprintf(buf1
, sizeof(buf1
), "%s[%d]", varstr
? varstr
: "", s
->c
);
2851 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2854 pstrcpy(buf1
, sizeof(buf1
), "*");
2855 if (t
& VT_CONSTANT
)
2856 pstrcat(buf1
, buf_size
, "const ");
2857 if (t
& VT_VOLATILE
)
2858 pstrcat(buf1
, buf_size
, "volatile ");
2860 pstrcat(buf1
, sizeof(buf1
), varstr
);
2861 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
2865 pstrcat(buf
, buf_size
, " ");
2866 pstrcat(buf
, buf_size
, varstr
);
2871 /* verify type compatibility to store vtop in 'dt' type, and generate
2873 static void gen_assign_cast(CType
*dt
)
2875 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
2876 char buf1
[256], buf2
[256];
2879 st
= &vtop
->type
; /* source type */
2880 dbt
= dt
->t
& VT_BTYPE
;
2881 sbt
= st
->t
& VT_BTYPE
;
2882 if (sbt
== VT_VOID
|| dbt
== VT_VOID
) {
2883 if (sbt
== VT_VOID
&& dbt
== VT_VOID
)
2885 It is Ok if both are void
2891 gcc accepts this program
2894 tcc_error("cannot cast from/to void");
2896 if (dt
->t
& VT_CONSTANT
)
2897 tcc_warning("assignment of read-only location");
2900 /* special cases for pointers */
2901 /* '0' can also be a pointer */
2902 if (is_null_pointer(vtop
))
2904 /* accept implicit pointer to integer cast with warning */
2905 if (is_integer_btype(sbt
)) {
2906 tcc_warning("assignment makes pointer from integer without a cast");
2909 type1
= pointed_type(dt
);
2910 /* a function is implicitly a function pointer */
2911 if (sbt
== VT_FUNC
) {
2912 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
2913 !is_compatible_types(pointed_type(dt
), st
))
2914 tcc_warning("assignment from incompatible pointer type");
2919 type2
= pointed_type(st
);
2920 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2921 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
2922 /* void * can match anything */
2924 //printf("types %08x %08x\n", type1->t, type2->t);
2925 /* exact type match, except for qualifiers */
2926 if (!is_compatible_unqualified_types(type1
, type2
)) {
2927 /* Like GCC don't warn by default for merely changes
2928 in pointer target signedness. Do warn for different
2929 base types, though, in particular for unsigned enums
2930 and signed int targets. */
2931 if ((type1
->t
& VT_BTYPE
) != (type2
->t
& VT_BTYPE
)
2932 || IS_ENUM(type1
->t
) || IS_ENUM(type2
->t
)
2934 tcc_warning("assignment from incompatible pointer type");
2937 /* check const and volatile */
2938 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
2939 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
2940 tcc_warning("assignment discards qualifiers from pointer target type");
2946 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
2947 tcc_warning("assignment makes integer from pointer without a cast");
2948 } else if (sbt
== VT_STRUCT
) {
2949 goto case_VT_STRUCT
;
2951 /* XXX: more tests */
2957 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2958 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2959 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2961 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
2962 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
2963 tcc_error("cannot cast '%s' to '%s'", buf1
, buf2
);
2971 /* store vtop in lvalue pushed on stack */
2972 ST_FUNC
void vstore(void)
2974 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
2976 ft
= vtop
[-1].type
.t
;
2977 sbt
= vtop
->type
.t
& VT_BTYPE
;
2978 dbt
= ft
& VT_BTYPE
;
2979 if ((((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
2980 (sbt
== VT_INT
&& dbt
== VT_SHORT
))
2981 && !(vtop
->type
.t
& VT_BITFIELD
)) {
2982 /* optimize char/short casts */
2983 delayed_cast
= VT_MUSTCAST
;
2984 vtop
->type
.t
= ft
& VT_TYPE
;
2985 /* XXX: factorize */
2986 if (ft
& VT_CONSTANT
)
2987 tcc_warning("assignment of read-only location");
2990 if (!(ft
& VT_BITFIELD
))
2991 gen_assign_cast(&vtop
[-1].type
);
2994 if (sbt
== VT_STRUCT
) {
2995 /* if structure, only generate pointer */
2996 /* structure assignment : generate memcpy */
2997 /* XXX: optimize if small size */
2998 size
= type_size(&vtop
->type
, &align
);
3002 vtop
->type
.t
= VT_PTR
;
3005 /* address of memcpy() */
3008 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
3009 else if(!(align
& 3))
3010 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
3013 /* Use memmove, rather than memcpy, as dest and src may be same: */
3014 vpush_global_sym(&func_old_type
, TOK_memmove
);
3019 vtop
->type
.t
= VT_PTR
;
3025 /* leave source on stack */
3026 } else if (ft
& VT_BITFIELD
) {
3027 /* bitfield store handling */
3029 /* save lvalue as expression result (example: s.b = s.a = n;) */
3030 vdup(), vtop
[-1] = vtop
[-2];
3032 bit_pos
= BIT_POS(ft
);
3033 bit_size
= BIT_SIZE(ft
);
3034 /* remove bit field info to avoid loops */
3035 vtop
[-1].type
.t
= ft
& ~VT_STRUCT_MASK
;
3037 if ((ft
& VT_BTYPE
) == VT_BOOL
) {
3038 gen_cast(&vtop
[-1].type
);
3039 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
3042 r
= adjust_bf(vtop
- 1, bit_pos
, bit_size
);
3043 if (r
== VT_STRUCT
) {
3044 gen_cast_s((ft
& VT_BTYPE
) == VT_LLONG
? VT_LLONG
: VT_INT
);
3045 store_packed_bf(bit_pos
, bit_size
);
3047 unsigned long long mask
= (1ULL << bit_size
) - 1;
3048 if ((ft
& VT_BTYPE
) != VT_BOOL
) {
3050 if ((vtop
[-1].type
.t
& VT_BTYPE
) == VT_LLONG
)
3053 vpushi((unsigned)mask
);
3060 /* duplicate destination */
3063 /* load destination, mask and or with source */
3064 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
)
3065 vpushll(~(mask
<< bit_pos
));
3067 vpushi(~((unsigned)mask
<< bit_pos
));
3072 /* ... and discard */
3076 #ifdef CONFIG_TCC_BCHECK
3077 /* bound check case */
3078 if (vtop
[-1].r
& VT_MUSTBOUND
) {
3087 #ifdef TCC_TARGET_X86_64
3088 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
3090 } else if ((ft
& VT_BTYPE
) == VT_QFLOAT
) {
3095 r
= gv(rc
); /* generate value */
3096 /* if lvalue was saved on stack, must read it */
3097 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
3099 t
= get_reg(RC_INT
);
3105 sv
.r
= VT_LOCAL
| VT_LVAL
;
3106 sv
.c
.i
= vtop
[-1].c
.i
;
3108 vtop
[-1].r
= t
| VT_LVAL
;
3110 /* two word case handling : store second register at word + 4 (or +8 for x86-64) */
3112 if (((ft
& VT_BTYPE
) == VT_QLONG
) || ((ft
& VT_BTYPE
) == VT_QFLOAT
)) {
3113 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
3115 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
3116 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
3118 vtop
[-1].type
.t
= load_type
;
3121 /* convert to int to increment easily */
3122 vtop
->type
.t
= addr_type
;
3128 vtop
[-1].type
.t
= load_type
;
3129 /* XXX: it works because r2 is spilled last ! */
3130 store(vtop
->r2
, vtop
- 1);
3136 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
3137 vtop
->r
|= delayed_cast
;
3141 /* post defines POST/PRE add. c is the token ++ or -- */
3142 ST_FUNC
void inc(int post
, int c
)
3145 vdup(); /* save lvalue */
3147 gv_dup(); /* duplicate value */
3152 vpushi(c
- TOK_MID
);
3154 vstore(); /* store value */
3156 vpop(); /* if post op, return saved value */
3159 ST_FUNC
void parse_mult_str (CString
*astr
, const char *msg
)
3161 /* read the string */
3165 while (tok
== TOK_STR
) {
3166 /* XXX: add \0 handling too ? */
3167 cstr_cat(astr
, tokc
.str
.data
, -1);
3170 cstr_ccat(astr
, '\0');
3173 /* If I is >= 1 and a power of two, returns log2(i)+1.
3174 If I is 0 returns 0. */
3175 static int exact_log2p1(int i
)
3180 for (ret
= 1; i
>= 1 << 8; ret
+= 8)
3191 /* Parse __attribute__((...)) GNUC extension. */
3192 static void parse_attribute(AttributeDef
*ad
)
3198 if (tok
!= TOK_ATTRIBUTE1
&& tok
!= TOK_ATTRIBUTE2
)
3203 while (tok
!= ')') {
3204 if (tok
< TOK_IDENT
)
3205 expect("attribute name");
3212 parse_mult_str(&astr
, "section name");
3213 ad
->section
= find_section(tcc_state
, (char *)astr
.data
);
3220 parse_mult_str(&astr
, "alias(\"target\")");
3221 ad
->alias_target
= /* save string as token, for later */
3222 tok_alloc((char*)astr
.data
, astr
.size
-1)->tok
;
3226 case TOK_VISIBILITY1
:
3227 case TOK_VISIBILITY2
:
3229 parse_mult_str(&astr
,
3230 "visibility(\"default|hidden|internal|protected\")");
3231 if (!strcmp (astr
.data
, "default"))
3232 ad
->a
.visibility
= STV_DEFAULT
;
3233 else if (!strcmp (astr
.data
, "hidden"))
3234 ad
->a
.visibility
= STV_HIDDEN
;
3235 else if (!strcmp (astr
.data
, "internal"))
3236 ad
->a
.visibility
= STV_INTERNAL
;
3237 else if (!strcmp (astr
.data
, "protected"))
3238 ad
->a
.visibility
= STV_PROTECTED
;
3240 expect("visibility(\"default|hidden|internal|protected\")");
3249 if (n
<= 0 || (n
& (n
- 1)) != 0)
3250 tcc_error("alignment must be a positive power of two");
3255 ad
->a
.aligned
= exact_log2p1(n
);
3256 if (n
!= 1 << (ad
->a
.aligned
- 1))
3257 tcc_error("alignment of %d is larger than implemented", n
);
3269 /* currently, no need to handle it because tcc does not
3270 track unused objects */
3274 /* currently, no need to handle it because tcc does not
3275 track unused objects */
3280 ad
->f
.func_call
= FUNC_CDECL
;
3285 ad
->f
.func_call
= FUNC_STDCALL
;
3287 #ifdef TCC_TARGET_I386
3297 ad
->f
.func_call
= FUNC_FASTCALL1
+ n
- 1;
3303 ad
->f
.func_call
= FUNC_FASTCALLW
;
3310 ad
->attr_mode
= VT_LLONG
+ 1;
3313 ad
->attr_mode
= VT_BYTE
+ 1;
3316 ad
->attr_mode
= VT_SHORT
+ 1;
3320 ad
->attr_mode
= VT_INT
+ 1;
3323 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
3330 ad
->a
.dllexport
= 1;
3333 ad
->a
.dllimport
= 1;
3336 if (tcc_state
->warn_unsupported
)
3337 tcc_warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
3338 /* skip parameters */
3340 int parenthesis
= 0;
3344 else if (tok
== ')')
3347 } while (parenthesis
&& tok
!= -1);
3360 static Sym
* find_field (CType
*type
, int v
)
3364 while ((s
= s
->next
) != NULL
) {
3365 if ((s
->v
& SYM_FIELD
) &&
3366 (s
->type
.t
& VT_BTYPE
) == VT_STRUCT
&&
3367 (s
->v
& ~SYM_FIELD
) >= SYM_FIRST_ANOM
) {
3368 Sym
*ret
= find_field (&s
->type
, v
);
3378 static void struct_add_offset (Sym
*s
, int offset
)
3380 while ((s
= s
->next
) != NULL
) {
3381 if ((s
->v
& SYM_FIELD
) &&
3382 (s
->type
.t
& VT_BTYPE
) == VT_STRUCT
&&
3383 (s
->v
& ~SYM_FIELD
) >= SYM_FIRST_ANOM
) {
3384 struct_add_offset(s
->type
.ref
, offset
);
3390 static void struct_layout(CType
*type
, AttributeDef
*ad
)
3392 int size
, align
, maxalign
, offset
, c
, bit_pos
, bit_size
;
3393 int packed
, a
, bt
, prevbt
, prev_bit_size
;
3394 int pcc
= !tcc_state
->ms_bitfields
;
3395 int pragma_pack
= *tcc_state
->pack_stack_ptr
;
3402 prevbt
= VT_STRUCT
; /* make it never match */
3407 for (f
= type
->ref
->next
; f
; f
= f
->next
) {
3408 if (f
->type
.t
& VT_BITFIELD
) {
3409 bit_size
= BIT_SIZE(f
->type
.t
);
3410 /* in pcc mode, long long bitfields have type int if they fit */
3411 if (pcc
&& (f
->type
.t
& VT_BTYPE
) == VT_LLONG
&& bit_size
<= 32)
3412 f
->type
.t
= (f
->type
.t
& ~VT_BTYPE
) | VT_INT
;
3415 size
= type_size(&f
->type
, &align
);
3416 a
= f
->a
.aligned
? 1 << (f
->a
.aligned
- 1) : 0;
3419 if (pcc
&& bit_size
== 0) {
3420 /* in pcc mode, packing does not affect zero-width bitfields */
3423 /* in pcc mode, attribute packed overrides if set. */
3424 if (pcc
&& (f
->a
.packed
|| ad
->a
.packed
))
3427 /* pragma pack overrides align if lesser and packs bitfields always */
3430 if (pragma_pack
< align
)
3431 align
= pragma_pack
;
3432 /* in pcc mode pragma pack also overrides individual align */
3433 if (pcc
&& pragma_pack
< a
)
3437 /* some individual align was specified */
3441 if (type
->ref
->type
.t
== VT_UNION
) {
3442 if (pcc
&& bit_size
>= 0)
3443 size
= (bit_size
+ 7) >> 3;
3448 } else if (bit_size
< 0) {
3450 c
+= (bit_pos
+ 7) >> 3;
3451 c
= (c
+ align
- 1) & -align
;
3460 /* A bit-field. Layout is more complicated. There are two
3461 options: PCC (GCC) compatible and MS compatible */
3463 /* In PCC layout a bit-field is placed adjacent to the
3464 preceding bit-fields, except if:
3466 - an individual alignment was given
3467 - it would overflow its base type container and
3468 there is no packing */
3469 if (bit_size
== 0) {
3471 c
= (c
+ ((bit_pos
+ 7) >> 3) + align
- 1) & -align
;
3473 } else if (f
->a
.aligned
) {
3475 } else if (!packed
) {
3477 int ofs
= ((c
* 8 + bit_pos
) % a8
+ bit_size
+ a8
- 1) / a8
;
3478 if (ofs
> size
/ align
)
3482 while (bit_pos
>= align
* 8)
3483 c
+= align
, bit_pos
-= align
* 8;
3486 /* In PCC layout named bit-fields influence the alignment
3487 of the containing struct using the base types alignment,
3488 except for packed fields (which here have correct align). */
3489 if (f
->v
& SYM_FIRST_ANOM
)
3492 bt
= f
->type
.t
& VT_BTYPE
;
3493 if ((bit_pos
+ bit_size
> size
* 8)
3494 || (bit_size
> 0) == (bt
!= prevbt
)
3496 c
= (c
+ align
- 1) & -align
;
3499 /* In MS bitfield mode a bit-field run always uses
3500 at least as many bits as the underlying type.
3501 To start a new run it's also required that this
3502 or the last bit-field had non-zero width. */
3503 if (bit_size
|| prev_bit_size
)
3506 /* In MS layout the records alignment is normally
3507 influenced by the field, except for a zero-width
3508 field at the start of a run (but by further zero-width
3509 fields it is again). */
3510 if (bit_size
== 0 && prevbt
!= bt
)
3513 prev_bit_size
= bit_size
;
3516 f
->type
.t
= (f
->type
.t
& ~(0x3f << VT_STRUCT_SHIFT
))
3517 | (bit_pos
<< VT_STRUCT_SHIFT
);
3518 bit_pos
+= bit_size
;
3520 if (align
> maxalign
)
3524 printf("set field %s offset %-2d size %-2d align %-2d",
3525 get_tok_str(f
->v
& ~SYM_FIELD
, NULL
), offset
, size
, align
);
3526 if (f
->type
.t
& VT_BITFIELD
) {
3527 printf(" pos %-2d bits %-2d",
3535 if (f
->v
& SYM_FIRST_ANOM
&& (f
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
3537 /* An anonymous struct/union. Adjust member offsets
3538 to reflect the real offset of our containing struct.
3539 Also set the offset of this anon member inside
3540 the outer struct to be zero. Via this it
3541 works when accessing the field offset directly
3542 (from base object), as well as when recursing
3543 members in initializer handling. */
3544 int v2
= f
->type
.ref
->v
;
3545 if (!(v2
& SYM_FIELD
) &&
3546 (v2
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
3548 /* This happens only with MS extensions. The
3549 anon member has a named struct type, so it
3550 potentially is shared with other references.
3551 We need to unshare members so we can modify
3554 f
->type
.ref
= sym_push(anon_sym
++ | SYM_FIELD
,
3555 &f
->type
.ref
->type
, 0,
3557 pps
= &f
->type
.ref
->next
;
3558 while ((ass
= ass
->next
) != NULL
) {
3559 *pps
= sym_push(ass
->v
, &ass
->type
, 0, ass
->c
);
3560 pps
= &((*pps
)->next
);
3564 struct_add_offset(f
->type
.ref
, offset
);
3574 c
+= (bit_pos
+ 7) >> 3;
3576 /* store size and alignment */
3577 a
= bt
= ad
->a
.aligned
? 1 << (ad
->a
.aligned
- 1) : 1;
3581 if (pragma_pack
&& pragma_pack
< maxalign
) {
3582 /* can happen if individual align for some member was given. In
3583 this case MSVC ignores maxalign when aligning the size */
3588 c
= (c
+ a
- 1) & -a
;
3592 printf("struct size %-2d align %-2d\n\n", c
, a
), fflush(stdout
);
3595 /* check whether we can access bitfields by their type */
3596 for (f
= type
->ref
->next
; f
; f
= f
->next
) {
3600 if (0 == (f
->type
.t
& VT_BITFIELD
))
3604 bit_size
= BIT_SIZE(f
->type
.t
);
3607 bit_pos
= BIT_POS(f
->type
.t
);
3608 size
= type_size(&f
->type
, &align
);
3609 if (bit_pos
+ bit_size
<= size
* 8 && f
->c
+ size
<= c
)
3612 /* try to access the field using a differnt type */
3613 c0
= -1, s
= align
= 1;
3615 px
= f
->c
* 8 + bit_pos
;
3616 cx
= (px
>> 3) & -align
;
3617 px
= px
- (cx
<< 3);
3620 s
= (px
+ bit_size
+ 7) >> 3;
3630 s
= type_size(&t
, &align
);
3634 if (px
+ bit_size
<= s
* 8 && cx
+ s
<= c
) {
3635 /* update offset and bit position */
3638 f
->type
.t
= (f
->type
.t
& ~(0x3f << VT_STRUCT_SHIFT
))
3639 | (bit_pos
<< VT_STRUCT_SHIFT
);
3643 printf("FIX field %s offset %-2d size %-2d align %-2d "
3644 "pos %-2d bits %-2d\n",
3645 get_tok_str(f
->v
& ~SYM_FIELD
, NULL
),
3646 cx
, s
, align
, px
, bit_size
);
3649 /* fall back to load/store single-byte wise */
3650 f
->auxtype
= VT_STRUCT
;
3652 printf("FIX field %s : load byte-wise\n",
3653 get_tok_str(f
->v
& ~SYM_FIELD
, NULL
));
3659 /* enum/struct/union declaration. u is VT_ENUM/VT_STRUCT/VT_UNION */
3660 static void struct_decl(CType
*type
, int u
)
3662 int v
, c
, size
, align
, flexible
;
3663 int bit_size
, bsize
, bt
;
3665 AttributeDef ad
, ad1
;
3668 memset(&ad
, 0, sizeof ad
);
3670 parse_attribute(&ad
);
3674 /* struct already defined ? return it */
3676 expect("struct/union/enum name");
3678 if (s
&& (s
->sym_scope
== local_scope
|| tok
!= '{')) {
3681 if (u
== VT_ENUM
&& IS_ENUM(s
->type
.t
))
3683 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
3688 /* Record the original enum/struct/union token. */
3689 type1
.t
= u
== VT_ENUM
? u
| VT_INT
| VT_UNSIGNED
: u
;
3691 /* we put an undefined size for struct/union */
3692 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
3693 s
->r
= 0; /* default alignment is zero as gcc */
3695 type
->t
= s
->type
.t
;
3701 tcc_error("struct/union/enum already defined");
3702 /* cannot be empty */
3703 /* non empty enums are not allowed */
3706 long long ll
= 0, pl
= 0, nl
= 0;
3709 /* enum symbols have static storage */
3710 t
.t
= VT_INT
|VT_STATIC
|VT_ENUM_VAL
;
3714 expect("identifier");
3716 if (ss
&& !local_stack
)
3717 tcc_error("redefinition of enumerator '%s'",
3718 get_tok_str(v
, NULL
));
3722 ll
= expr_const64();
3724 ss
= sym_push(v
, &t
, VT_CONST
, 0);
3726 *ps
= ss
, ps
= &ss
->next
;
3735 /* NOTE: we accept a trailing comma */
3740 /* set integral type of the enum */
3743 if (pl
!= (unsigned)pl
)
3746 } else if (pl
!= (int)pl
|| nl
!= (int)nl
)
3748 s
->type
.t
= type
->t
= t
.t
| VT_ENUM
;
3750 /* set type for enum members */
3751 for (ss
= s
->next
; ss
; ss
= ss
->next
) {
3753 if (ll
== (int)ll
) /* default is int if it fits */
3755 if (t
.t
& VT_UNSIGNED
) {
3756 ss
->type
.t
|= VT_UNSIGNED
;
3757 if (ll
== (unsigned)ll
)
3760 ss
->type
.t
= (ss
->type
.t
& ~VT_BTYPE
) | VT_LLONG
;
3765 while (tok
!= '}') {
3766 if (!parse_btype(&btype
, &ad1
)) {
3772 tcc_error("flexible array member '%s' not at the end of struct",
3773 get_tok_str(v
, NULL
));
3779 type_decl(&type1
, &ad1
, &v
, TYPE_DIRECT
);
3781 if ((type1
.t
& VT_BTYPE
) != VT_STRUCT
)
3782 expect("identifier");
3784 int v
= btype
.ref
->v
;
3785 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
3786 if (tcc_state
->ms_extensions
== 0)
3787 expect("identifier");
3791 if (type_size(&type1
, &align
) < 0) {
3792 if ((u
== VT_STRUCT
) && (type1
.t
& VT_ARRAY
) && c
)
3795 tcc_error("field '%s' has incomplete type",
3796 get_tok_str(v
, NULL
));
3798 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
3799 (type1
.t
& VT_STORAGE
))
3800 tcc_error("invalid type for '%s'",
3801 get_tok_str(v
, NULL
));
3805 bit_size
= expr_const();
3806 /* XXX: handle v = 0 case for messages */
3808 tcc_error("negative width in bit-field '%s'",
3809 get_tok_str(v
, NULL
));
3810 if (v
&& bit_size
== 0)
3811 tcc_error("zero width for bit-field '%s'",
3812 get_tok_str(v
, NULL
));
3813 parse_attribute(&ad1
);
3815 size
= type_size(&type1
, &align
);
3816 if (bit_size
>= 0) {
3817 bt
= type1
.t
& VT_BTYPE
;
3823 tcc_error("bitfields must have scalar type");
3825 if (bit_size
> bsize
) {
3826 tcc_error("width of '%s' exceeds its type",
3827 get_tok_str(v
, NULL
));
3828 } else if (bit_size
== bsize
3829 && !ad
.a
.packed
&& !ad1
.a
.packed
) {
3830 /* no need for bit fields */
3832 } else if (bit_size
== 64) {
3833 tcc_error("field width 64 not implemented");
3835 type1
.t
= (type1
.t
& ~VT_STRUCT_MASK
)
3837 | (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
3840 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
3841 /* Remember we've seen a real field to check
3842 for placement of flexible array member. */
3845 /* If member is a struct or bit-field, enforce
3846 placing into the struct (as anonymous). */
3848 ((type1
.t
& VT_BTYPE
) == VT_STRUCT
||
3853 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, 0);
3858 if (tok
== ';' || tok
== TOK_EOF
)
3865 parse_attribute(&ad
);
3866 struct_layout(type
, &ad
);
3871 static void sym_to_attr(AttributeDef
*ad
, Sym
*s
)
3873 if (s
->a
.aligned
&& 0 == ad
->a
.aligned
)
3874 ad
->a
.aligned
= s
->a
.aligned
;
3875 if (s
->f
.func_call
&& 0 == ad
->f
.func_call
)
3876 ad
->f
.func_call
= s
->f
.func_call
;
3877 if (s
->f
.func_type
&& 0 == ad
->f
.func_type
)
3878 ad
->f
.func_type
= s
->f
.func_type
;
3883 /* Add type qualifiers to a type. If the type is an array then the qualifiers
3884 are added to the element type, copied because it could be a typedef. */
3885 static void parse_btype_qualify(CType
*type
, int qualifiers
)
3887 while (type
->t
& VT_ARRAY
) {
3888 type
->ref
= sym_push(SYM_FIELD
, &type
->ref
->type
, 0, type
->ref
->c
);
3889 type
= &type
->ref
->type
;
3891 type
->t
|= qualifiers
;
3894 /* return 0 if no type declaration. otherwise, return the basic type
3897 static int parse_btype(CType
*type
, AttributeDef
*ad
)
3899 int t
, u
, bt
, st
, type_found
, typespec_found
, g
;
3903 memset(ad
, 0, sizeof(AttributeDef
));
3913 /* currently, we really ignore extension */
3923 if (u
== VT_SHORT
|| u
== VT_LONG
) {
3924 if (st
!= -1 || (bt
!= -1 && bt
!= VT_INT
))
3925 tmbt
: tcc_error("too many basic types");
3928 if (bt
!= -1 || (st
!= -1 && u
!= VT_INT
))
3933 t
= (t
& ~VT_BTYPE
) | u
;
3946 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
3947 #ifndef TCC_TARGET_PE
3948 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3950 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
3951 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
3958 #ifdef TCC_TARGET_ARM64
3960 /* GCC's __uint128_t appears in some Linux header files. Make it a
3961 synonym for long double to get the size and alignment right. */
3972 if ((t
& VT_BTYPE
) == VT_LONG
) {
3973 #ifdef TCC_TARGET_PE
3974 t
= (t
& ~VT_BTYPE
) | VT_DOUBLE
;
3976 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
3985 struct_decl(&type1
, VT_ENUM
);
3988 type
->ref
= type1
.ref
;
3991 struct_decl(&type1
, VT_STRUCT
);
3994 struct_decl(&type1
, VT_UNION
);
3997 /* type modifiers */
4002 parse_btype_qualify(type
, VT_CONSTANT
);
4010 parse_btype_qualify(type
, VT_VOLATILE
);
4017 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == (VT_DEFSIGN
|VT_UNSIGNED
))
4018 tcc_error("signed and unsigned modifier");
4031 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == VT_DEFSIGN
)
4032 tcc_error("signed and unsigned modifier");
4033 t
|= VT_DEFSIGN
| VT_UNSIGNED
;
4049 if (t
& (VT_EXTERN
|VT_STATIC
|VT_TYPEDEF
) & ~g
)
4050 tcc_error("multiple storage classes");
4061 /* GNUC attribute */
4062 case TOK_ATTRIBUTE1
:
4063 case TOK_ATTRIBUTE2
:
4064 parse_attribute(ad
);
4065 if (ad
->attr_mode
) {
4066 u
= ad
->attr_mode
-1;
4067 t
= (t
& ~VT_BTYPE
) | u
;
4075 parse_expr_type(&type1
);
4076 /* remove all storage modifiers except typedef */
4077 type1
.t
&= ~(VT_STORAGE
&~VT_TYPEDEF
);
4079 sym_to_attr(ad
, type1
.ref
);
4085 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
4088 u
= t
& ~(VT_CONSTANT
| VT_VOLATILE
), t
^= u
;
4089 type
->t
= (s
->type
.t
& ~VT_TYPEDEF
) | u
;
4090 type
->ref
= s
->type
.ref
;
4092 parse_btype_qualify(type
, t
);
4094 /* get attributes from typedef */
4104 if (tcc_state
->char_is_unsigned
) {
4105 if ((t
& (VT_DEFSIGN
|VT_BTYPE
)) == VT_BYTE
)
4109 /* long is never used as type */
4110 if ((t
& VT_BTYPE
) == VT_LONG
)
4111 #if PTR_SIZE == 8 && !defined TCC_TARGET_PE
4112 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
4114 t
= (t
& ~VT_BTYPE
) | VT_INT
;
4120 /* convert a function parameter type (array to pointer and function to
4121 function pointer) */
4122 static inline void convert_parameter_type(CType
*pt
)
4124 /* remove const and volatile qualifiers (XXX: const could be used
4125 to indicate a const function parameter */
4126 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
4127 /* array must be transformed to pointer according to ANSI C */
4129 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
4134 ST_FUNC
void parse_asm_str(CString
*astr
)
4137 parse_mult_str(astr
, "string constant");
4140 /* Parse an asm label and return the token */
4141 static int asm_label_instr(void)
4147 parse_asm_str(&astr
);
4150 printf("asm_alias: \"%s\"\n", (char *)astr
.data
);
4152 v
= tok_alloc(astr
.data
, astr
.size
- 1)->tok
;
4157 static int post_type(CType
*type
, AttributeDef
*ad
, int storage
, int td
)
4159 int n
, l
, t1
, arg_size
, align
;
4160 Sym
**plast
, *s
, *first
;
4165 /* function type, or recursive declarator (return if so) */
4167 if (td
&& !(td
& TYPE_ABSTRACT
))
4171 else if (parse_btype(&pt
, &ad1
))
4182 /* read param name and compute offset */
4183 if (l
!= FUNC_OLD
) {
4184 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
4186 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
4187 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
4188 tcc_error("parameter declared as void");
4189 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
4193 expect("identifier");
4194 pt
.t
= VT_VOID
; /* invalid type */
4197 convert_parameter_type(&pt
);
4198 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
4204 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
4209 if (l
== FUNC_NEW
&& !parse_btype(&pt
, &ad1
))
4210 tcc_error("invalid type");
4213 /* if no parameters, then old type prototype */
4216 /* NOTE: const is ignored in returned type as it has a special
4217 meaning in gcc / C++ */
4218 type
->t
&= ~VT_CONSTANT
;
4219 /* some ancient pre-K&R C allows a function to return an array
4220 and the array brackets to be put after the arguments, such
4221 that "int c()[]" means something like "int[] c()" */
4224 skip(']'); /* only handle simple "[]" */
4227 /* we push a anonymous symbol which will contain the function prototype */
4228 ad
->f
.func_args
= arg_size
;
4229 ad
->f
.func_type
= l
;
4230 s
= sym_push(SYM_FIELD
, type
, 0, 0);
4236 } else if (tok
== '[') {
4237 int saved_nocode_wanted
= nocode_wanted
;
4238 /* array definition */
4240 if (tok
== TOK_RESTRICT1
)
4245 if (!local_stack
|| (storage
& VT_STATIC
))
4246 vpushi(expr_const());
4248 /* VLAs (which can only happen with local_stack && !VT_STATIC)
4249 length must always be evaluated, even under nocode_wanted,
4250 so that its size slot is initialized (e.g. under sizeof
4255 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4258 tcc_error("invalid array size");
4260 if (!is_integer_btype(vtop
->type
.t
& VT_BTYPE
))
4261 tcc_error("size of variable length array should be an integer");
4266 /* parse next post type */
4267 post_type(type
, ad
, storage
, 0);
4268 if (type
->t
== VT_FUNC
)
4269 tcc_error("declaration of an array of functions");
4270 t1
|= type
->t
& VT_VLA
;
4273 loc
-= type_size(&int_type
, &align
);
4277 vla_runtime_type_size(type
, &align
);
4279 vset(&int_type
, VT_LOCAL
|VT_LVAL
, n
);
4285 nocode_wanted
= saved_nocode_wanted
;
4287 /* we push an anonymous symbol which will contain the array
4289 s
= sym_push(SYM_FIELD
, type
, 0, n
);
4290 type
->t
= (t1
? VT_VLA
: VT_ARRAY
) | VT_PTR
;
4296 /* Parse a type declarator (except basic type), and return the type
4297 in 'type'. 'td' is a bitmask indicating which kind of type decl is
4298 expected. 'type' should contain the basic type. 'ad' is the
4299 attribute definition of the basic type. It can be modified by
4300 type_decl(). If this (possibly abstract) declarator is a pointer chain
4301 it returns the innermost pointed to type (equals *type, but is a different
4302 pointer), otherwise returns type itself, that's used for recursive calls. */
4303 static CType
*type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
4306 int qualifiers
, storage
;
4308 /* recursive type, remove storage bits first, apply them later again */
4309 storage
= type
->t
& VT_STORAGE
;
4310 type
->t
&= ~VT_STORAGE
;
4313 while (tok
== '*') {
4321 qualifiers
|= VT_CONSTANT
;
4326 qualifiers
|= VT_VOLATILE
;
4332 /* XXX: clarify attribute handling */
4333 case TOK_ATTRIBUTE1
:
4334 case TOK_ATTRIBUTE2
:
4335 parse_attribute(ad
);
4339 type
->t
|= qualifiers
;
4341 /* innermost pointed to type is the one for the first derivation */
4342 ret
= pointed_type(type
);
4346 /* This is possibly a parameter type list for abstract declarators
4347 ('int ()'), use post_type for testing this. */
4348 if (!post_type(type
, ad
, 0, td
)) {
4349 /* It's not, so it's a nested declarator, and the post operations
4350 apply to the innermost pointed to type (if any). */
4351 /* XXX: this is not correct to modify 'ad' at this point, but
4352 the syntax is not clear */
4353 parse_attribute(ad
);
4354 post
= type_decl(type
, ad
, v
, td
);
4357 } else if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
4358 /* type identifier */
4362 if (!(td
& TYPE_ABSTRACT
))
4363 expect("identifier");
4366 post_type(post
, ad
, storage
, 0);
4367 parse_attribute(ad
);
4372 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
4373 ST_FUNC
int lvalue_type(int t
)
4378 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
4380 else if (bt
== VT_SHORT
)
4384 if (t
& VT_UNSIGNED
)
4385 r
|= VT_LVAL_UNSIGNED
;
4389 /* indirection with full error checking and bound check */
4390 ST_FUNC
void indir(void)
4392 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
4393 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
4397 if (vtop
->r
& VT_LVAL
)
4399 vtop
->type
= *pointed_type(&vtop
->type
);
4400 /* Arrays and functions are never lvalues */
4401 if (!(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_VLA
)
4402 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
4403 vtop
->r
|= lvalue_type(vtop
->type
.t
);
4404 /* if bound checking, the referenced pointer must be checked */
4405 #ifdef CONFIG_TCC_BCHECK
4406 if (tcc_state
->do_bounds_check
)
4407 vtop
->r
|= VT_MUSTBOUND
;
4412 /* pass a parameter to a function and do type checking and casting */
4413 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
4418 func_type
= func
->f
.func_type
;
4419 if (func_type
== FUNC_OLD
||
4420 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
4421 /* default casting : only need to convert float to double */
4422 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
4423 gen_cast_s(VT_DOUBLE
);
4424 } else if (vtop
->type
.t
& VT_BITFIELD
) {
4425 type
.t
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
4426 type
.ref
= vtop
->type
.ref
;
4429 } else if (arg
== NULL
) {
4430 tcc_error("too many arguments to function");
4433 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
4434 gen_assign_cast(&type
);
4438 /* parse an expression and return its type without any side effect.
4439 If UNRY we parse an unary expression, otherwise a full one. */
4440 static void expr_type(CType
*type
, int unry
)
4452 /* parse an expression of the form '(type)' or '(expr)' and return its
4454 static void parse_expr_type(CType
*type
)
4460 if (parse_btype(type
, &ad
)) {
4461 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
4468 static void parse_type(CType
*type
)
4473 if (!parse_btype(type
, &ad
)) {
4476 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
4479 static void parse_builtin_params(int nc
, const char *args
)
4486 while ((c
= *args
++)) {
4490 case 'e': expr_eq(); continue;
4491 case 't': parse_type(&t
); vpush(&t
); continue;
4492 default: tcc_error("internal error"); break;
4500 ST_FUNC
void unary(void)
4502 int n
, t
, align
, size
, r
, sizeof_caller
;
4507 sizeof_caller
= in_sizeof
;
4510 /* XXX: GCC 2.95.3 does not generate a table although it should be
4523 vsetc(&type
, VT_CONST
, &tokc
);
4527 t
= VT_INT
| VT_UNSIGNED
;
4533 t
= VT_LLONG
| VT_UNSIGNED
;
4545 case TOK___FUNCTION__
:
4547 goto tok_identifier
;
4553 /* special function name identifier */
4554 len
= strlen(funcname
) + 1;
4555 /* generate char[len] type */
4560 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
4561 ptr
= section_ptr_add(data_section
, len
);
4562 memcpy(ptr
, funcname
, len
);
4567 #ifdef TCC_TARGET_PE
4568 t
= VT_SHORT
| VT_UNSIGNED
;
4574 /* string parsing */
4576 if (tcc_state
->char_is_unsigned
)
4577 t
= VT_BYTE
| VT_UNSIGNED
;
4579 if (tcc_state
->warn_write_strings
)
4584 memset(&ad
, 0, sizeof(AttributeDef
));
4585 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, 0);
4590 if (parse_btype(&type
, &ad
)) {
4591 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
4593 /* check ISOC99 compound literal */
4595 /* data is allocated locally by default */
4600 /* all except arrays are lvalues */
4601 if (!(type
.t
& VT_ARRAY
))
4602 r
|= lvalue_type(type
.t
);
4603 memset(&ad
, 0, sizeof(AttributeDef
));
4604 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, 0);
4606 if (sizeof_caller
) {
4613 } else if (tok
== '{') {
4614 int saved_nocode_wanted
= nocode_wanted
;
4616 tcc_error("expected constant");
4617 /* save all registers */
4619 /* statement expression : we do not accept break/continue
4620 inside as GCC does. We do retain the nocode_wanted state,
4621 as statement expressions can't ever be entered from the
4622 outside, so any reactivation of code emission (from labels
4623 or loop heads) can be disabled again after the end of it. */
4624 block(NULL
, NULL
, 1);
4625 nocode_wanted
= saved_nocode_wanted
;
4640 /* functions names must be treated as function pointers,
4641 except for unary '&' and sizeof. Since we consider that
4642 functions are not lvalues, we only have to handle it
4643 there and in function calls. */
4644 /* arrays can also be used although they are not lvalues */
4645 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
4646 !(vtop
->type
.t
& VT_ARRAY
))
4648 mk_pointer(&vtop
->type
);
4654 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4655 gen_cast_s(VT_BOOL
);
4656 vtop
->c
.i
= !vtop
->c
.i
;
4657 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
4661 vseti(VT_JMP
, gvtst(1, 0));
4673 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
4674 tcc_error("pointer not accepted for unary plus");
4675 /* In order to force cast, we add zero, except for floating point
4676 where we really need an noop (otherwise -0.0 will be transformed
4678 if (!is_float(vtop
->type
.t
)) {
4689 expr_type(&type
, 1); // Perform a in_sizeof = 0;
4690 s
= vtop
[1].sym
; /* hack: accessing previous vtop */
4691 size
= type_size(&type
, &align
);
4692 if (s
&& s
->a
.aligned
)
4693 align
= 1 << (s
->a
.aligned
- 1);
4694 if (t
== TOK_SIZEOF
) {
4695 if (!(type
.t
& VT_VLA
)) {
4697 tcc_error("sizeof applied to an incomplete type");
4700 vla_runtime_type_size(&type
, &align
);
4705 vtop
->type
.t
|= VT_UNSIGNED
;
4708 case TOK_builtin_expect
:
4709 /* __builtin_expect is a no-op for now */
4710 parse_builtin_params(0, "ee");
4713 case TOK_builtin_types_compatible_p
:
4714 parse_builtin_params(0, "tt");
4715 vtop
[-1].type
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
4716 vtop
[0].type
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
4717 n
= is_compatible_types(&vtop
[-1].type
, &vtop
[0].type
);
4721 case TOK_builtin_choose_expr
:
4748 case TOK_builtin_constant_p
:
4749 parse_builtin_params(1, "e");
4750 n
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
4754 case TOK_builtin_frame_address
:
4755 case TOK_builtin_return_address
:
4761 if (tok
!= TOK_CINT
) {
4762 tcc_error("%s only takes positive integers",
4763 tok1
== TOK_builtin_return_address
?
4764 "__builtin_return_address" :
4765 "__builtin_frame_address");
4767 level
= (uint32_t)tokc
.i
;
4772 vset(&type
, VT_LOCAL
, 0); /* local frame */
4774 mk_pointer(&vtop
->type
);
4775 indir(); /* -> parent frame */
4777 if (tok1
== TOK_builtin_return_address
) {
4778 // assume return address is just above frame pointer on stack
4781 mk_pointer(&vtop
->type
);
4786 #ifdef TCC_TARGET_X86_64
4787 #ifdef TCC_TARGET_PE
4788 case TOK_builtin_va_start
:
4789 parse_builtin_params(0, "ee");
4790 r
= vtop
->r
& VT_VALMASK
;
4794 tcc_error("__builtin_va_start expects a local variable");
4796 vtop
->type
= char_pointer_type
;
4801 case TOK_builtin_va_arg_types
:
4802 parse_builtin_params(0, "t");
4803 vpushi(classify_x86_64_va_arg(&vtop
->type
));
4810 #ifdef TCC_TARGET_ARM64
4811 case TOK___va_start
: {
4812 parse_builtin_params(0, "ee");
4816 vtop
->type
.t
= VT_VOID
;
4819 case TOK___va_arg
: {
4820 parse_builtin_params(0, "et");
4828 case TOK___arm64_clear_cache
: {
4829 parse_builtin_params(0, "ee");
4832 vtop
->type
.t
= VT_VOID
;
4836 /* pre operations */
4847 t
= vtop
->type
.t
& VT_BTYPE
;
4849 /* In IEEE negate(x) isn't subtract(0,x), but rather
4853 vtop
->c
.f
= -1.0 * 0.0;
4854 else if (t
== VT_DOUBLE
)
4855 vtop
->c
.d
= -1.0 * 0.0;
4857 vtop
->c
.ld
= -1.0 * 0.0;
4865 goto tok_identifier
;
4867 /* allow to take the address of a label */
4868 if (tok
< TOK_UIDENT
)
4869 expect("label identifier");
4870 s
= label_find(tok
);
4872 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
4874 if (s
->r
== LABEL_DECLARED
)
4875 s
->r
= LABEL_FORWARD
;
4878 s
->type
.t
= VT_VOID
;
4879 mk_pointer(&s
->type
);
4880 s
->type
.t
|= VT_STATIC
;
4882 vpushsym(&s
->type
, s
);
4888 CType controlling_type
;
4889 int has_default
= 0;
4892 AttributeDef ad_tmp
;
4894 TokenString
*str
= NULL
;
4895 ParseState saved_parse_state
;
4899 expr_type(&controlling_type
, 1);
4900 if (controlling_type
.t
& VT_ARRAY
)
4901 controlling_type
.t
= VT_PTR
;
4902 controlling_type
.t
&= ~VT_CONSTANT
;
4906 if (tok
== TOK_DEFAULT
) {
4908 tcc_error("too many 'default'");
4917 parse_btype(&cur_type
, &ad_tmp
);
4918 type_decl(&cur_type
, &ad_tmp
, &itmp
, TYPE_ABSTRACT
);
4919 if (compare_types(&controlling_type
, &cur_type
, 0)) {
4921 // tcc_error("type march twice");
4931 skip_or_save_block(&str
);
4933 skip_or_save_block(NULL
);
4937 else if (tok
== ')')
4940 if (!has_match
&& !has_default
) {
4943 type_to_str(buf
, 256, &controlling_type
, NULL
);
4944 tcc_error("_Generic sellector of type '%s' is not compatible with any assosiation",
4948 save_parse_state(&saved_parse_state
);
4949 begin_macro(str
, 1);
4953 restore_parse_state(&saved_parse_state
);
4956 // special qnan , snan and infinity values
4958 vpush64(VT_DOUBLE
, 0x7ff8000000000000ULL
);
4962 vpush64(VT_DOUBLE
, 0x7ff0000000000001ULL
);
4966 vpush64(VT_DOUBLE
, 0x7ff0000000000000ULL
);
4975 expect("identifier");
4978 const char *name
= get_tok_str(t
, NULL
);
4980 tcc_error("'%s' undeclared", name
);
4981 /* for simple function calls, we tolerate undeclared
4982 external reference to int() function */
4983 if (tcc_state
->warn_implicit_function_declaration
4984 #ifdef TCC_TARGET_PE
4985 /* people must be warned about using undeclared WINAPI functions
4986 (which usually start with uppercase letter) */
4987 || (name
[0] >= 'A' && name
[0] <= 'Z')
4990 tcc_warning("implicit declaration of function '%s'", name
);
4991 s
= external_global_sym(t
, &func_old_type
, 0);
4995 /* A symbol that has a register is a local register variable,
4996 which starts out as VT_LOCAL value. */
4997 if ((r
& VT_VALMASK
) < VT_CONST
)
4998 r
= (r
& ~VT_VALMASK
) | VT_LOCAL
;
5000 vset(&s
->type
, r
, s
->c
);
5001 /* Point to s as backpointer (even without r&VT_SYM).
5002 Will be used by at least the x86 inline asm parser for
5008 } else if (r
== VT_CONST
&& IS_ENUM_VAL(s
->type
.t
)) {
5009 vtop
->c
.i
= s
->enum_val
;
5014 /* post operations */
5016 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
5019 } else if (tok
== '.' || tok
== TOK_ARROW
|| tok
== TOK_CDOUBLE
) {
5022 if (tok
== TOK_ARROW
)
5024 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
5027 /* expect pointer on structure */
5028 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
5029 expect("struct or union");
5030 if (tok
== TOK_CDOUBLE
)
5031 expect("field name");
5033 if (tok
== TOK_CINT
|| tok
== TOK_CUINT
)
5034 expect("field name");
5035 s
= find_field(&vtop
->type
, tok
);
5037 tcc_error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, &tokc
));
5038 /* add field offset to pointer */
5039 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
5042 /* change type to field type, and set to lvalue */
5043 vtop
->type
= s
->type
;
5044 vtop
->type
.t
|= qualifiers
;
5045 /* an array is never an lvalue */
5046 if (!(vtop
->type
.t
& VT_ARRAY
)) {
5047 vtop
->r
|= lvalue_type(vtop
->type
.t
);
5048 #ifdef CONFIG_TCC_BCHECK
5049 /* if bound checking, the referenced pointer must be checked */
5050 if (tcc_state
->do_bounds_check
&& (vtop
->r
& VT_VALMASK
) != VT_LOCAL
)
5051 vtop
->r
|= VT_MUSTBOUND
;
5055 } else if (tok
== '[') {
5061 } else if (tok
== '(') {
5064 int nb_args
, ret_nregs
, ret_align
, regsize
, variadic
;
5067 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
5068 /* pointer test (no array accepted) */
5069 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
5070 vtop
->type
= *pointed_type(&vtop
->type
);
5071 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
5075 expect("function pointer");
5078 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
5080 /* get return type */
5083 sa
= s
->next
; /* first parameter */
5084 nb_args
= regsize
= 0;
5086 /* compute first implicit argument if a structure is returned */
5087 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
5088 variadic
= (s
->f
.func_type
== FUNC_ELLIPSIS
);
5089 ret_nregs
= gfunc_sret(&s
->type
, variadic
, &ret
.type
,
5090 &ret_align
, ®size
);
5092 /* get some space for the returned structure */
5093 size
= type_size(&s
->type
, &align
);
5094 #ifdef TCC_TARGET_ARM64
5095 /* On arm64, a small struct is return in registers.
5096 It is much easier to write it to memory if we know
5097 that we are allowed to write some extra bytes, so
5098 round the allocated space up to a power of 2: */
5100 while (size
& (size
- 1))
5101 size
= (size
| (size
- 1)) + 1;
5103 loc
= (loc
- size
) & -align
;
5105 ret
.r
= VT_LOCAL
| VT_LVAL
;
5106 /* pass it as 'int' to avoid structure arg passing
5108 vseti(VT_LOCAL
, loc
);
5118 /* return in register */
5119 if (is_float(ret
.type
.t
)) {
5120 ret
.r
= reg_fret(ret
.type
.t
);
5121 #ifdef TCC_TARGET_X86_64
5122 if ((ret
.type
.t
& VT_BTYPE
) == VT_QFLOAT
)
5126 #ifndef TCC_TARGET_ARM64
5127 #ifdef TCC_TARGET_X86_64
5128 if ((ret
.type
.t
& VT_BTYPE
) == VT_QLONG
)
5130 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
5141 gfunc_param_typed(s
, sa
);
5151 tcc_error("too few arguments to function");
5153 gfunc_call(nb_args
);
5156 for (r
= ret
.r
+ ret_nregs
+ !ret_nregs
; r
-- > ret
.r
;) {
5157 vsetc(&ret
.type
, r
, &ret
.c
);
5158 vtop
->r2
= ret
.r2
; /* Loop only happens when r2 is VT_CONST */
5161 /* handle packed struct return */
5162 if (((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) && ret_nregs
) {
5165 size
= type_size(&s
->type
, &align
);
5166 /* We're writing whole regs often, make sure there's enough
5167 space. Assume register size is power of 2. */
5168 if (regsize
> align
)
5170 loc
= (loc
- size
) & -align
;
5174 vset(&ret
.type
, VT_LOCAL
| VT_LVAL
, addr
+ offset
);
5178 if (--ret_nregs
== 0)
5182 vset(&s
->type
, VT_LOCAL
| VT_LVAL
, addr
);
5190 ST_FUNC
void expr_prod(void)
5195 while (tok
== '*' || tok
== '/' || tok
== '%') {
5203 ST_FUNC
void expr_sum(void)
5208 while (tok
== '+' || tok
== '-') {
5216 static void expr_shift(void)
5221 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
5229 static void expr_cmp(void)
5234 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
5235 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
5243 static void expr_cmpeq(void)
5248 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
5256 static void expr_and(void)
5259 while (tok
== '&') {
5266 static void expr_xor(void)
5269 while (tok
== '^') {
5276 static void expr_or(void)
5279 while (tok
== '|') {
5286 static void expr_land(void)
5289 if (tok
== TOK_LAND
) {
5292 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
5293 gen_cast_s(VT_BOOL
);
5298 while (tok
== TOK_LAND
) {
5314 if (tok
!= TOK_LAND
) {
5327 static void expr_lor(void)
5330 if (tok
== TOK_LOR
) {
5333 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
5334 gen_cast_s(VT_BOOL
);
5339 while (tok
== TOK_LOR
) {
5355 if (tok
!= TOK_LOR
) {
5368 /* Assuming vtop is a value used in a conditional context
5369 (i.e. compared with zero) return 0 if it's false, 1 if
5370 true and -1 if it can't be statically determined. */
5371 static int condition_3way(void)
5374 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
&&
5375 (!(vtop
->r
& VT_SYM
) || !vtop
->sym
->a
.weak
)) {
5377 gen_cast_s(VT_BOOL
);
5384 static void expr_cond(void)
5386 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
, islv
, c
, g
;
5388 CType type
, type1
, type2
;
5393 c
= condition_3way();
5394 g
= (tok
== ':' && gnu_ext
);
5396 /* needed to avoid having different registers saved in
5398 if (is_float(vtop
->type
.t
)) {
5400 #ifdef TCC_TARGET_X86_64
5401 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
5426 sv
= *vtop
; /* save value to handle it later */
5427 vtop
--; /* no vpop so that FP stack is not flushed */
5445 bt1
= t1
& VT_BTYPE
;
5447 bt2
= t2
& VT_BTYPE
;
5450 /* cast operands to correct type according to ISOC rules */
5451 if (is_float(bt1
) || is_float(bt2
)) {
5452 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
5453 type
.t
= VT_LDOUBLE
;
5455 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
5460 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
5461 /* cast to biggest op */
5463 /* convert to unsigned if it does not fit in a long long */
5464 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_LLONG
| VT_UNSIGNED
) ||
5465 (t2
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_LLONG
| VT_UNSIGNED
))
5466 type
.t
|= VT_UNSIGNED
;
5467 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
5468 /* If one is a null ptr constant the result type
5470 if (is_null_pointer (vtop
))
5472 else if (is_null_pointer (&sv
))
5474 /* XXX: test pointer compatibility, C99 has more elaborate
5478 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
5479 /* XXX: test function pointer compatibility */
5480 type
= bt1
== VT_FUNC
? type1
: type2
;
5481 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
5482 /* XXX: test structure compatibility */
5483 type
= bt1
== VT_STRUCT
? type1
: type2
;
5484 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
5485 /* NOTE: as an extension, we accept void on only one side */
5488 /* integer operations */
5490 /* convert to unsigned if it does not fit in an integer */
5491 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_INT
| VT_UNSIGNED
) ||
5492 (t2
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_INT
| VT_UNSIGNED
))
5493 type
.t
|= VT_UNSIGNED
;
5495 /* keep structs lvalue by transforming `(expr ? a : b)` to `*(expr ? &a : &b)` so
5496 that `(expr ? a : b).mem` does not error with "lvalue expected" */
5497 islv
= (vtop
->r
& VT_LVAL
) && (sv
.r
& VT_LVAL
) && VT_STRUCT
== (type
.t
& VT_BTYPE
);
5500 /* now we convert second operand */
5504 mk_pointer(&vtop
->type
);
5506 } else if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
5511 if (is_float(type
.t
)) {
5513 #ifdef TCC_TARGET_X86_64
5514 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
5518 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
5519 /* for long longs, we use fixed registers to avoid having
5520 to handle a complicated move */
5531 /* this is horrible, but we must also convert first
5537 mk_pointer(&vtop
->type
);
5539 } else if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
5545 move_reg(r2
, r1
, type
.t
);
5555 static void expr_eq(void)
5561 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
5562 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
5563 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
5578 ST_FUNC
void gexpr(void)
5589 /* parse a constant expression and return value in vtop. */
5590 static void expr_const1(void)
5597 /* parse an integer constant and return its value. */
5598 static inline int64_t expr_const64(void)
5602 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
5603 expect("constant expression");
5609 /* parse an integer constant and return its value.
5610 Complain if it doesn't fit 32bit (signed or unsigned). */
5611 ST_FUNC
int expr_const(void)
5614 int64_t wc
= expr_const64();
5616 if (c
!= wc
&& (unsigned)c
!= wc
)
5617 tcc_error("constant exceeds 32 bit");
5621 /* return the label token if current token is a label, otherwise
5623 static int is_label(void)
5627 /* fast test first */
5628 if (tok
< TOK_UIDENT
)
5630 /* no need to save tokc because tok is an identifier */
5636 unget_tok(last_tok
);
5641 #ifndef TCC_TARGET_ARM64
5642 static void gfunc_return(CType
*func_type
)
5644 if ((func_type
->t
& VT_BTYPE
) == VT_STRUCT
) {
5645 CType type
, ret_type
;
5646 int ret_align
, ret_nregs
, regsize
;
5647 ret_nregs
= gfunc_sret(func_type
, func_var
, &ret_type
,
5648 &ret_align
, ®size
);
5649 if (0 == ret_nregs
) {
5650 /* if returning structure, must copy it to implicit
5651 first pointer arg location */
5654 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
5657 /* copy structure value to pointer */
5660 /* returning structure packed into registers */
5661 int r
, size
, addr
, align
;
5662 size
= type_size(func_type
,&align
);
5663 if ((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) ||
5664 (vtop
->c
.i
& (ret_align
-1)))
5665 && (align
& (ret_align
-1))) {
5666 loc
= (loc
- size
) & -ret_align
;
5669 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
5673 vset(&ret_type
, VT_LOCAL
| VT_LVAL
, addr
);
5675 vtop
->type
= ret_type
;
5676 if (is_float(ret_type
.t
))
5677 r
= rc_fret(ret_type
.t
);
5688 if (--ret_nregs
== 0)
5690 /* We assume that when a structure is returned in multiple
5691 registers, their classes are consecutive values of the
5694 vtop
->c
.i
+= regsize
;
5698 } else if (is_float(func_type
->t
)) {
5699 gv(rc_fret(func_type
->t
));
5703 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
5707 static int case_cmp(const void *pa
, const void *pb
)
5709 int64_t a
= (*(struct case_t
**) pa
)->v1
;
5710 int64_t b
= (*(struct case_t
**) pb
)->v1
;
5711 return a
< b
? -1 : a
> b
;
5714 static void gcase(struct case_t
**base
, int len
, int *bsym
)
5718 int ll
= (vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
;
5736 gtst_addr(0, p
->sym
); /* v1 <= x <= v2 */
5738 gcase(base
, len
/2, bsym
);
5739 if (cur_switch
->def_sym
)
5740 gjmp_addr(cur_switch
->def_sym
);
5742 *bsym
= gjmp(*bsym
);
5746 base
+= e
; len
-= e
;
5756 if (p
->v1
== p
->v2
) {
5758 gtst_addr(0, p
->sym
);
5768 gtst_addr(0, p
->sym
);
5774 static void block(int *bsym
, int *csym
, int is_expr
)
5776 int a
, b
, c
, d
, cond
;
5779 /* generate line number info */
5780 if (tcc_state
->do_debug
)
5781 tcc_debug_line(tcc_state
);
5784 /* default return value is (void) */
5786 vtop
->type
.t
= VT_VOID
;
5789 if (tok
== TOK_IF
) {
5791 int saved_nocode_wanted
= nocode_wanted
;
5796 cond
= condition_3way();
5802 nocode_wanted
|= 0x20000000;
5803 block(bsym
, csym
, 0);
5805 nocode_wanted
= saved_nocode_wanted
;
5807 if (c
== TOK_ELSE
) {
5812 nocode_wanted
|= 0x20000000;
5813 block(bsym
, csym
, 0);
5814 gsym(d
); /* patch else jmp */
5816 nocode_wanted
= saved_nocode_wanted
;
5819 } else if (tok
== TOK_WHILE
) {
5820 int saved_nocode_wanted
;
5821 nocode_wanted
&= ~0x20000000;
5831 saved_nocode_wanted
= nocode_wanted
;
5833 nocode_wanted
= saved_nocode_wanted
;
5838 } else if (tok
== '{') {
5840 int block_vla_sp_loc
= vla_sp_loc
, saved_vlas_in_scope
= vlas_in_scope
;
5843 /* record local declaration stack position */
5845 llabel
= local_label_stack
;
5848 /* handle local labels declarations */
5849 if (tok
== TOK_LABEL
) {
5852 if (tok
< TOK_UIDENT
)
5853 expect("label identifier");
5854 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
5864 while (tok
!= '}') {
5865 if ((a
= is_label()))
5872 block(bsym
, csym
, is_expr
);
5875 /* pop locally defined labels */
5876 label_pop(&local_label_stack
, llabel
, is_expr
);
5877 /* pop locally defined symbols */
5879 /* In the is_expr case (a statement expression is finished here),
5880 vtop might refer to symbols on the local_stack. Either via the
5881 type or via vtop->sym. We can't pop those nor any that in turn
5882 might be referred to. To make it easier we don't roll back
5883 any symbols in that case; some upper level call to block() will
5884 do that. We do have to remove such symbols from the lookup
5885 tables, though. sym_pop will do that. */
5886 sym_pop(&local_stack
, s
, is_expr
);
5888 /* Pop VLA frames and restore stack pointer if required */
5889 if (vlas_in_scope
> saved_vlas_in_scope
) {
5890 vla_sp_loc
= saved_vlas_in_scope
? block_vla_sp_loc
: vla_sp_root_loc
;
5893 vlas_in_scope
= saved_vlas_in_scope
;
5896 } else if (tok
== TOK_RETURN
) {
5900 gen_assign_cast(&func_vt
);
5901 gfunc_return(&func_vt
);
5904 /* jump unless last stmt in top-level block */
5905 if (tok
!= '}' || local_scope
!= 1)
5907 nocode_wanted
|= 0x20000000;
5908 } else if (tok
== TOK_BREAK
) {
5911 tcc_error("cannot break");
5912 *bsym
= gjmp(*bsym
);
5915 nocode_wanted
|= 0x20000000;
5916 } else if (tok
== TOK_CONTINUE
) {
5919 tcc_error("cannot continue");
5920 vla_sp_restore_root();
5921 *csym
= gjmp(*csym
);
5924 } else if (tok
== TOK_FOR
) {
5926 int saved_nocode_wanted
;
5927 nocode_wanted
&= ~0x20000000;
5933 /* c99 for-loop init decl? */
5934 if (!decl0(VT_LOCAL
, 1, NULL
)) {
5935 /* no, regular for-loop init expr */
5961 saved_nocode_wanted
= nocode_wanted
;
5963 nocode_wanted
= saved_nocode_wanted
;
5968 sym_pop(&local_stack
, s
, 0);
5971 if (tok
== TOK_DO
) {
5972 int saved_nocode_wanted
;
5973 nocode_wanted
&= ~0x20000000;
5979 saved_nocode_wanted
= nocode_wanted
;
5987 nocode_wanted
= saved_nocode_wanted
;
5992 if (tok
== TOK_SWITCH
) {
5993 struct switch_t
*saved
, sw
;
5994 int saved_nocode_wanted
= nocode_wanted
;
6000 switchval
= *vtop
--;
6002 b
= gjmp(0); /* jump to first case */
6003 sw
.p
= NULL
; sw
.n
= 0; sw
.def_sym
= 0;
6007 nocode_wanted
= saved_nocode_wanted
;
6008 a
= gjmp(a
); /* add implicit break */
6011 qsort(sw
.p
, sw
.n
, sizeof(void*), case_cmp
);
6012 for (b
= 1; b
< sw
.n
; b
++)
6013 if (sw
.p
[b
- 1]->v2
>= sw
.p
[b
]->v1
)
6014 tcc_error("duplicate case value");
6015 /* Our switch table sorting is signed, so the compared
6016 value needs to be as well when it's 64bit. */
6017 if ((switchval
.type
.t
& VT_BTYPE
) == VT_LLONG
)
6018 switchval
.type
.t
&= ~VT_UNSIGNED
;
6020 gcase(sw
.p
, sw
.n
, &a
);
6023 gjmp_addr(sw
.def_sym
);
6024 dynarray_reset(&sw
.p
, &sw
.n
);
6029 if (tok
== TOK_CASE
) {
6030 struct case_t
*cr
= tcc_malloc(sizeof(struct case_t
));
6033 nocode_wanted
&= ~0x20000000;
6035 cr
->v1
= cr
->v2
= expr_const64();
6036 if (gnu_ext
&& tok
== TOK_DOTS
) {
6038 cr
->v2
= expr_const64();
6039 if (cr
->v2
< cr
->v1
)
6040 tcc_warning("empty case range");
6043 dynarray_add(&cur_switch
->p
, &cur_switch
->n
, cr
);
6046 goto block_after_label
;
6048 if (tok
== TOK_DEFAULT
) {
6053 if (cur_switch
->def_sym
)
6054 tcc_error("too many 'default'");
6055 cur_switch
->def_sym
= ind
;
6057 goto block_after_label
;
6059 if (tok
== TOK_GOTO
) {
6061 if (tok
== '*' && gnu_ext
) {
6065 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
6068 } else if (tok
>= TOK_UIDENT
) {
6069 s
= label_find(tok
);
6070 /* put forward definition if needed */
6072 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
6074 if (s
->r
== LABEL_DECLARED
)
6075 s
->r
= LABEL_FORWARD
;
6077 vla_sp_restore_root();
6078 if (s
->r
& LABEL_FORWARD
)
6079 s
->jnext
= gjmp(s
->jnext
);
6081 gjmp_addr(s
->jnext
);
6084 expect("label identifier");
6087 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
6096 if (s
->r
== LABEL_DEFINED
)
6097 tcc_error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
6099 s
->r
= LABEL_DEFINED
;
6101 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
6105 /* we accept this, but it is a mistake */
6107 nocode_wanted
&= ~0x20000000;
6109 tcc_warning("deprecated use of label at end of compound statement");
6113 block(bsym
, csym
, is_expr
);
6116 /* expression case */
6131 /* This skips over a stream of tokens containing balanced {} and ()
6132 pairs, stopping at outer ',' ';' and '}' (or matching '}' if we started
6133 with a '{'). If STR then allocates and stores the skipped tokens
6134 in *STR. This doesn't check if () and {} are nested correctly,
6135 i.e. "({)}" is accepted. */
6136 static void skip_or_save_block(TokenString
**str
)
6138 int braces
= tok
== '{';
6141 *str
= tok_str_alloc();
6143 while ((level
> 0 || (tok
!= '}' && tok
!= ',' && tok
!= ';' && tok
!= ')'))) {
6145 if (tok
== TOK_EOF
) {
6146 if (str
|| level
> 0)
6147 tcc_error("unexpected end of file");
6152 tok_str_add_tok(*str
);
6155 if (t
== '{' || t
== '(') {
6157 } else if (t
== '}' || t
== ')') {
6159 if (level
== 0 && braces
&& t
== '}')
6164 tok_str_add(*str
, -1);
6165 tok_str_add(*str
, 0);
6169 #define EXPR_CONST 1
6172 static void parse_init_elem(int expr_type
)
6174 int saved_global_expr
;
6177 /* compound literals must be allocated globally in this case */
6178 saved_global_expr
= global_expr
;
6181 global_expr
= saved_global_expr
;
6182 /* NOTE: symbols are accepted, as well as lvalue for anon symbols
6183 (compound literals). */
6184 if (((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
6185 && ((vtop
->r
& (VT_SYM
|VT_LVAL
)) != (VT_SYM
|VT_LVAL
)
6186 || vtop
->sym
->v
< SYM_FIRST_ANOM
))
6187 #ifdef TCC_TARGET_PE
6188 || ((vtop
->r
& VT_SYM
) && vtop
->sym
->a
.dllimport
)
6191 tcc_error("initializer element is not constant");
6199 /* put zeros for variable based init */
6200 static void init_putz(Section
*sec
, unsigned long c
, int size
)
6203 /* nothing to do because globals are already set to zero */
6205 vpush_global_sym(&func_old_type
, TOK_memset
);
6207 #ifdef TCC_TARGET_ARM
6218 /* t is the array or struct type. c is the array or struct
6219 address. cur_field is the pointer to the current
6220 field, for arrays the 'c' member contains the current start
6221 index. 'size_only' is true if only size info is needed (only used
6222 in arrays). al contains the already initialized length of the
6223 current container (starting at c). This returns the new length of that. */
6224 static int decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
6225 Sym
**cur_field
, int size_only
, int al
)
6228 int index
, index_last
, align
, l
, nb_elems
, elem_size
;
6229 unsigned long corig
= c
;
6233 if (gnu_ext
&& (l
= is_label()) != 0)
6235 /* NOTE: we only support ranges for last designator */
6236 while (nb_elems
== 1 && (tok
== '[' || tok
== '.')) {
6238 if (!(type
->t
& VT_ARRAY
))
6239 expect("array type");
6241 index
= index_last
= expr_const();
6242 if (tok
== TOK_DOTS
&& gnu_ext
) {
6244 index_last
= expr_const();
6248 if (index
< 0 || (s
->c
>= 0 && index_last
>= s
->c
) ||
6250 tcc_error("invalid index");
6252 (*cur_field
)->c
= index_last
;
6253 type
= pointed_type(type
);
6254 elem_size
= type_size(type
, &align
);
6255 c
+= index
* elem_size
;
6256 nb_elems
= index_last
- index
+ 1;
6262 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
6263 expect("struct/union type");
6264 f
= find_field(type
, l
);
6277 } else if (!gnu_ext
) {
6281 if (type
->t
& VT_ARRAY
) {
6282 index
= (*cur_field
)->c
;
6283 if (type
->ref
->c
>= 0 && index
>= type
->ref
->c
)
6284 tcc_error("index too large");
6285 type
= pointed_type(type
);
6286 c
+= index
* type_size(type
, &align
);
6289 while (f
&& (f
->v
& SYM_FIRST_ANOM
) && (f
->type
.t
& VT_BITFIELD
))
6290 *cur_field
= f
= f
->next
;
6292 tcc_error("too many field init");
6297 /* must put zero in holes (note that doing it that way
6298 ensures that it even works with designators) */
6299 if (!size_only
&& c
- corig
> al
)
6300 init_putz(sec
, corig
+ al
, c
- corig
- al
);
6301 decl_initializer(type
, sec
, c
, 0, size_only
);
6303 /* XXX: make it more general */
6304 if (!size_only
&& nb_elems
> 1) {
6305 unsigned long c_end
;
6310 vset(type
, VT_LOCAL
|VT_LVAL
, c
);
6311 for (i
= 1; i
< nb_elems
; i
++) {
6312 vset(type
, VT_LOCAL
|VT_LVAL
, c
+ elem_size
* i
);
6318 c_end
= c
+ nb_elems
* elem_size
;
6319 if (c_end
> sec
->data_allocated
)
6320 section_realloc(sec
, c_end
);
6321 src
= sec
->data
+ c
;
6323 for(i
= 1; i
< nb_elems
; i
++) {
6325 memcpy(dst
, src
, elem_size
);
6329 c
+= nb_elems
* type_size(type
, &align
);
6335 /* store a value or an expression directly in global data or in local array */
6336 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
)
6343 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
6347 /* XXX: not portable */
6348 /* XXX: generate error if incorrect relocation */
6349 gen_assign_cast(&dtype
);
6350 bt
= type
->t
& VT_BTYPE
;
6351 size
= type_size(type
, &align
);
6352 section_reserve(sec
, c
+ size
);
6353 ptr
= sec
->data
+ c
;
6354 /* XXX: make code faster ? */
6355 if ((vtop
->r
& (VT_SYM
|VT_CONST
)) == (VT_SYM
|VT_CONST
) &&
6356 vtop
->sym
->v
>= SYM_FIRST_ANOM
&&
6357 /* XXX This rejects compound literals like
6358 '(void *){ptr}'. The problem is that '&sym' is
6359 represented the same way, which would be ruled out
6360 by the SYM_FIRST_ANOM check above, but also '"string"'
6361 in 'char *p = "string"' is represented the same
6362 with the type being VT_PTR and the symbol being an
6363 anonymous one. That is, there's no difference in vtop
6364 between '(void *){x}' and '&(void *){x}'. Ignore
6365 pointer typed entities here. Hopefully no real code
6366 will every use compound literals with scalar type. */
6367 (vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
6368 /* These come from compound literals, memcpy stuff over. */
6372 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[vtop
->sym
->c
];
6373 ssec
= tcc_state
->sections
[esym
->st_shndx
];
6374 memmove (ptr
, ssec
->data
+ esym
->st_value
, size
);
6376 /* We need to copy over all memory contents, and that
6377 includes relocations. Use the fact that relocs are
6378 created it order, so look from the end of relocs
6379 until we hit one before the copied region. */
6380 int num_relocs
= ssec
->reloc
->data_offset
/ sizeof(*rel
);
6381 rel
= (ElfW_Rel
*)(ssec
->reloc
->data
+ ssec
->reloc
->data_offset
);
6382 while (num_relocs
--) {
6384 if (rel
->r_offset
>= esym
->st_value
+ size
)
6386 if (rel
->r_offset
< esym
->st_value
)
6388 /* Note: if the same fields are initialized multiple
6389 times (possible with designators) then we possibly
6390 add multiple relocations for the same offset here.
6391 That would lead to wrong code, the last reloc needs
6392 to win. We clean this up later after the whole
6393 initializer is parsed. */
6394 put_elf_reloca(symtab_section
, sec
,
6395 c
+ rel
->r_offset
- esym
->st_value
,
6396 ELFW(R_TYPE
)(rel
->r_info
),
6397 ELFW(R_SYM
)(rel
->r_info
),
6407 if ((vtop
->r
& VT_SYM
) &&
6417 (type
->t
& VT_BITFIELD
)
6419 tcc_error("initializer element is not computable at load time");
6421 if (type
->t
& VT_BITFIELD
) {
6422 int bit_pos
, bit_size
, bits
, n
;
6423 unsigned char *p
, v
, m
;
6424 bit_pos
= BIT_POS(vtop
->type
.t
);
6425 bit_size
= BIT_SIZE(vtop
->type
.t
);
6426 p
= (unsigned char*)ptr
+ (bit_pos
>> 3);
6427 bit_pos
&= 7, bits
= 0;
6432 v
= vtop
->c
.i
>> bits
<< bit_pos
;
6433 m
= ((1 << n
) - 1) << bit_pos
;
6434 *p
= (*p
& ~m
) | (v
& m
);
6435 bits
+= n
, bit_size
-= n
, bit_pos
= 0, ++p
;
6439 /* XXX: when cross-compiling we assume that each type has the
6440 same representation on host and target, which is likely to
6441 be wrong in the case of long double */
6443 vtop
->c
.i
= vtop
->c
.i
!= 0;
6445 *(char *)ptr
|= vtop
->c
.i
;
6448 *(short *)ptr
|= vtop
->c
.i
;
6451 *(float*)ptr
= vtop
->c
.f
;
6454 *(double *)ptr
= vtop
->c
.d
;
6457 if (sizeof(long double) == LDOUBLE_SIZE
)
6458 *(long double *)ptr
= vtop
->c
.ld
;
6459 else if (sizeof(double) == LDOUBLE_SIZE
)
6460 *(double *)ptr
= (double)vtop
->c
.ld
;
6461 #if (defined __i386__ || defined __x86_64__) && (defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64)
6462 else if (sizeof (long double) >= 10)
6463 memcpy(memset(ptr
, 0, LDOUBLE_SIZE
), &vtop
->c
.ld
, 10);
6465 else if (sizeof (long double) == sizeof (double))
6466 __asm__("fldl %1\nfstpt %0\n" : "=m"
6467 (memset(ptr
, 0, LDOUBLE_SIZE
), ptr
) : "m" (vtop
->c
.ld
));
6471 tcc_error("can't cross compile long double constants");
6475 *(long long *)ptr
|= vtop
->c
.i
;
6482 addr_t val
= vtop
->c
.i
;
6484 if (vtop
->r
& VT_SYM
)
6485 greloca(sec
, vtop
->sym
, c
, R_DATA_PTR
, val
);
6487 *(addr_t
*)ptr
|= val
;
6489 if (vtop
->r
& VT_SYM
)
6490 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
6491 *(addr_t
*)ptr
|= val
;
6497 int val
= vtop
->c
.i
;
6499 if (vtop
->r
& VT_SYM
)
6500 greloca(sec
, vtop
->sym
, c
, R_DATA_PTR
, val
);
6504 if (vtop
->r
& VT_SYM
)
6505 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
6514 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
6521 /* 't' contains the type and storage info. 'c' is the offset of the
6522 object in section 'sec'. If 'sec' is NULL, it means stack based
6523 allocation. 'first' is true if array '{' must be read (multi
6524 dimension implicit array init handling). 'size_only' is true if
6525 size only evaluation is wanted (only for arrays). */
6526 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
6527 int first
, int size_only
)
6529 int len
, n
, no_oblock
, nb
, i
;
6536 /* If we currently are at an '}' or ',' we have read an initializer
6537 element in one of our callers, and not yet consumed it. */
6538 have_elem
= tok
== '}' || tok
== ',';
6539 if (!have_elem
&& tok
!= '{' &&
6540 /* In case of strings we have special handling for arrays, so
6541 don't consume them as initializer value (which would commit them
6542 to some anonymous symbol). */
6543 tok
!= TOK_LSTR
&& tok
!= TOK_STR
&&
6545 parse_init_elem(!sec
? EXPR_ANY
: EXPR_CONST
);
6550 !(type
->t
& VT_ARRAY
) &&
6551 /* Use i_c_parameter_t, to strip toplevel qualifiers.
6552 The source type might have VT_CONSTANT set, which is
6553 of course assignable to non-const elements. */
6554 is_compatible_unqualified_types(type
, &vtop
->type
)) {
6555 init_putv(type
, sec
, c
);
6556 } else if (type
->t
& VT_ARRAY
) {
6559 t1
= pointed_type(type
);
6560 size1
= type_size(t1
, &align1
);
6563 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
6566 tcc_error("character array initializer must be a literal,"
6567 " optionally enclosed in braces");
6572 /* only parse strings here if correct type (otherwise: handle
6573 them as ((w)char *) expressions */
6574 if ((tok
== TOK_LSTR
&&
6575 #ifdef TCC_TARGET_PE
6576 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
6578 (t1
->t
& VT_BTYPE
) == VT_INT
6580 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
6582 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
6585 /* compute maximum number of chars wanted */
6587 cstr_len
= tokc
.str
.size
;
6589 cstr_len
= tokc
.str
.size
/ sizeof(nwchar_t
);
6592 if (n
>= 0 && nb
> (n
- len
))
6596 tcc_warning("initializer-string for array is too long");
6597 /* in order to go faster for common case (char
6598 string in global variable, we handle it
6600 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
6601 memcpy(sec
->data
+ c
+ len
, tokc
.str
.data
, nb
);
6605 ch
= ((unsigned char *)tokc
.str
.data
)[i
];
6607 ch
= ((nwchar_t
*)tokc
.str
.data
)[i
];
6609 init_putv(t1
, sec
, c
+ (len
+ i
) * size1
);
6616 /* only add trailing zero if enough storage (no
6617 warning in this case since it is standard) */
6618 if (n
< 0 || len
< n
) {
6621 init_putv(t1
, sec
, c
+ (len
* size1
));
6632 while (tok
!= '}' || have_elem
) {
6633 len
= decl_designator(type
, sec
, c
, &f
, size_only
, len
);
6635 if (type
->t
& VT_ARRAY
) {
6637 /* special test for multi dimensional arrays (may not
6638 be strictly correct if designators are used at the
6640 if (no_oblock
&& len
>= n
*size1
)
6643 if (s
->type
.t
== VT_UNION
)
6647 if (no_oblock
&& f
== NULL
)
6656 /* put zeros at the end */
6657 if (!size_only
&& len
< n
*size1
)
6658 init_putz(sec
, c
+ len
, n
*size1
- len
);
6661 /* patch type size if needed, which happens only for array types */
6663 s
->c
= size1
== 1 ? len
: ((len
+ size1
- 1)/size1
);
6664 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
6667 if (first
|| tok
== '{') {
6675 } else if (tok
== '{') {
6677 decl_initializer(type
, sec
, c
, first
, size_only
);
6679 } else if (size_only
) {
6680 /* If we supported only ISO C we wouldn't have to accept calling
6681 this on anything than an array size_only==1 (and even then
6682 only on the outermost level, so no recursion would be needed),
6683 because initializing a flex array member isn't supported.
6684 But GNU C supports it, so we need to recurse even into
6685 subfields of structs and arrays when size_only is set. */
6686 /* just skip expression */
6687 skip_or_save_block(NULL
);
6690 /* This should happen only when we haven't parsed
6691 the init element above for fear of committing a
6692 string constant to memory too early. */
6693 if (tok
!= TOK_STR
&& tok
!= TOK_LSTR
)
6694 expect("string constant");
6695 parse_init_elem(!sec
? EXPR_ANY
: EXPR_CONST
);
6697 init_putv(type
, sec
, c
);
6701 /* parse an initializer for type 't' if 'has_init' is non zero, and
6702 allocate space in local or global data space ('r' is either
6703 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
6704 variable 'v' of scope 'scope' is declared before initializers
6705 are parsed. If 'v' is zero, then a reference to the new object
6706 is put in the value stack. If 'has_init' is 2, a special parsing
6707 is done to handle string constants. */
6708 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
6709 int has_init
, int v
, int scope
)
6711 int size
, align
, addr
;
6712 ParseState saved_parse_state
= {0};
6713 TokenString
*init_str
= NULL
;
6715 Sym
*flexible_array
;
6718 flexible_array
= NULL
;
6719 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
6720 Sym
*field
= type
->ref
->next
;
6723 field
= field
->next
;
6724 if (field
->type
.t
& VT_ARRAY
&& field
->type
.ref
->c
< 0)
6725 flexible_array
= field
;
6729 size
= type_size(type
, &align
);
6730 /* If unknown size, we must evaluate it before
6731 evaluating initializers because
6732 initializers can generate global data too
6733 (e.g. string pointers or ISOC99 compound
6734 literals). It also simplifies local
6735 initializers handling */
6736 if (size
< 0 || (flexible_array
&& has_init
)) {
6738 tcc_error("unknown type size");
6739 /* get all init string */
6740 if (has_init
== 2) {
6741 init_str
= tok_str_alloc();
6742 /* only get strings */
6743 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
6744 tok_str_add_tok(init_str
);
6747 tok_str_add(init_str
, -1);
6748 tok_str_add(init_str
, 0);
6750 skip_or_save_block(&init_str
);
6754 save_parse_state(&saved_parse_state
);
6756 begin_macro(init_str
, 1);
6758 decl_initializer(type
, NULL
, 0, 1, 1);
6759 /* prepare second initializer parsing */
6760 macro_ptr
= init_str
->str
;
6763 /* if still unknown size, error */
6764 size
= type_size(type
, &align
);
6766 tcc_error("unknown type size");
6768 /* If there's a flex member and it was used in the initializer
6770 if (flexible_array
&&
6771 flexible_array
->type
.ref
->c
> 0)
6772 size
+= flexible_array
->type
.ref
->c
6773 * pointed_size(&flexible_array
->type
);
6774 /* take into account specified alignment if bigger */
6775 if (ad
->a
.aligned
) {
6776 int speca
= 1 << (ad
->a
.aligned
- 1);
6779 } else if (ad
->a
.packed
) {
6782 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
6784 #ifdef CONFIG_TCC_BCHECK
6785 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
6789 loc
= (loc
- size
) & -align
;
6791 #ifdef CONFIG_TCC_BCHECK
6792 /* handles bounds */
6793 /* XXX: currently, since we do only one pass, we cannot track
6794 '&' operators, so we add only arrays */
6795 if (tcc_state
->do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
6797 /* add padding between regions */
6799 /* then add local bound info */
6800 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(addr_t
));
6801 bounds_ptr
[0] = addr
;
6802 bounds_ptr
[1] = size
;
6806 /* local variable */
6807 #ifdef CONFIG_TCC_ASM
6808 if (ad
->asm_label
) {
6809 int reg
= asm_parse_regvar(ad
->asm_label
);
6811 r
= (r
& ~VT_VALMASK
) | reg
;
6814 sym
= sym_push(v
, type
, r
, addr
);
6817 /* push local reference */
6818 vset(type
, r
, addr
);
6821 if (v
&& scope
== VT_CONST
) {
6822 /* see if the symbol was already defined */
6825 patch_storage(sym
, ad
, type
);
6826 if (sym
->type
.t
& VT_EXTERN
) {
6827 /* if the variable is extern, it was not allocated */
6828 sym
->type
.t
&= ~VT_EXTERN
;
6829 /* set array size if it was omitted in extern
6831 if ((sym
->type
.t
& VT_ARRAY
) &&
6832 sym
->type
.ref
->c
< 0 &&
6834 sym
->type
.ref
->c
= type
->ref
->c
;
6835 } else if (!has_init
) {
6836 /* we accept several definitions of the same
6837 global variable. this is tricky, because we
6838 must play with the SHN_COMMON type of the symbol */
6839 /* no init data, we won't add more to the symbol */
6841 } else if (sym
->c
) {
6843 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
6844 if (esym
->st_shndx
== data_section
->sh_num
)
6845 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
6850 /* allocate symbol in corresponding section */
6855 else if (tcc_state
->nocommon
)
6860 addr
= section_add(sec
, size
, align
);
6861 #ifdef CONFIG_TCC_BCHECK
6862 /* add padding if bound check */
6863 if (tcc_state
->do_bounds_check
)
6864 section_add(sec
, 1, 1);
6867 addr
= align
; /* SHN_COMMON is special, symbol value is align */
6868 sec
= common_section
;
6873 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
6874 patch_storage(sym
, ad
, NULL
);
6876 /* Local statics have a scope until now (for
6877 warnings), remove it here. */
6879 /* update symbol definition */
6880 put_extern_sym(sym
, sec
, addr
, size
);
6882 /* push global reference */
6883 sym
= get_sym_ref(type
, sec
, addr
, size
);
6884 vpushsym(type
, sym
);
6888 #ifdef CONFIG_TCC_BCHECK
6889 /* handles bounds now because the symbol must be defined
6890 before for the relocation */
6891 if (tcc_state
->do_bounds_check
) {
6894 greloca(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
, 0);
6895 /* then add global bound info */
6896 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(addr_t
));
6897 bounds_ptr
[0] = 0; /* relocated */
6898 bounds_ptr
[1] = size
;
6903 if (type
->t
& VT_VLA
) {
6906 /* save current stack pointer */
6907 if (vlas_in_scope
== 0) {
6908 if (vla_sp_root_loc
== -1)
6909 vla_sp_root_loc
= (loc
-= PTR_SIZE
);
6910 gen_vla_sp_save(vla_sp_root_loc
);
6913 vla_runtime_type_size(type
, &a
);
6914 gen_vla_alloc(type
, a
);
6915 gen_vla_sp_save(addr
);
6919 } else if (has_init
) {
6920 size_t oldreloc_offset
= 0;
6921 if (sec
&& sec
->reloc
)
6922 oldreloc_offset
= sec
->reloc
->data_offset
;
6923 decl_initializer(type
, sec
, addr
, 1, 0);
6924 if (sec
&& sec
->reloc
)
6925 squeeze_multi_relocs(sec
, oldreloc_offset
);
6926 /* patch flexible array member size back to -1, */
6927 /* for possible subsequent similar declarations */
6929 flexible_array
->type
.ref
->c
= -1;
6933 /* restore parse state if needed */
6936 restore_parse_state(&saved_parse_state
);
6940 /* parse a function defined by symbol 'sym' and generate its code in
6941 'cur_text_section' */
6942 static void gen_function(Sym
*sym
)
6945 ind
= cur_text_section
->data_offset
;
6946 /* NOTE: we patch the symbol size later */
6947 put_extern_sym(sym
, cur_text_section
, ind
, 0);
6948 funcname
= get_tok_str(sym
->v
, NULL
);
6950 /* Initialize VLA state */
6952 vla_sp_root_loc
= -1;
6953 /* put debug symbol */
6954 tcc_debug_funcstart(tcc_state
, sym
);
6955 /* push a dummy symbol to enable local sym storage */
6956 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
6957 local_scope
= 1; /* for function parameters */
6958 gfunc_prolog(&sym
->type
);
6961 block(NULL
, NULL
, 0);
6965 cur_text_section
->data_offset
= ind
;
6966 label_pop(&global_label_stack
, NULL
, 0);
6967 /* reset local stack */
6969 sym_pop(&local_stack
, NULL
, 0);
6970 /* end of function */
6971 /* patch symbol size */
6972 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
6974 tcc_debug_funcend(tcc_state
, ind
- func_ind
);
6975 /* It's better to crash than to generate wrong code */
6976 cur_text_section
= NULL
;
6977 funcname
= ""; /* for safety */
6978 func_vt
.t
= VT_VOID
; /* for safety */
6979 func_var
= 0; /* for safety */
6980 ind
= 0; /* for safety */
6985 static void gen_inline_functions(TCCState
*s
)
6988 int inline_generated
, i
, ln
;
6989 struct InlineFunc
*fn
;
6991 ln
= file
->line_num
;
6992 /* iterate while inline function are referenced */
6994 inline_generated
= 0;
6995 for (i
= 0; i
< s
->nb_inline_fns
; ++i
) {
6996 fn
= s
->inline_fns
[i
];
6998 if (sym
&& sym
->c
) {
6999 /* the function was used: generate its code and
7000 convert it to a normal function */
7003 pstrcpy(file
->filename
, sizeof file
->filename
, fn
->filename
);
7004 sym
->type
.t
&= ~VT_INLINE
;
7006 begin_macro(fn
->func_str
, 1);
7008 cur_text_section
= text_section
;
7012 inline_generated
= 1;
7015 } while (inline_generated
);
7016 file
->line_num
= ln
;
7019 ST_FUNC
void free_inline_functions(TCCState
*s
)
7022 /* free tokens of unused inline functions */
7023 for (i
= 0; i
< s
->nb_inline_fns
; ++i
) {
7024 struct InlineFunc
*fn
= s
->inline_fns
[i
];
7026 tok_str_free(fn
->func_str
);
7028 dynarray_reset(&s
->inline_fns
, &s
->nb_inline_fns
);
7031 /* 'l' is VT_LOCAL or VT_CONST to define default storage type, or VT_CMP
7032 if parsing old style parameter decl list (and FUNC_SYM is set then) */
7033 static int decl0(int l
, int is_for_loop_init
, Sym
*func_sym
)
7041 if (!parse_btype(&btype
, &ad
)) {
7042 if (is_for_loop_init
)
7044 /* skip redundant ';' if not in old parameter decl scope */
7045 if (tok
== ';' && l
!= VT_CMP
) {
7049 if (l
== VT_CONST
&&
7050 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
7051 /* global asm block */
7055 /* special test for old K&R protos without explicit int
7056 type. Only accepted when defining global data */
7057 if (l
!= VT_CONST
|| tok
< TOK_UIDENT
)
7062 if ((btype
.t
& VT_BTYPE
) == VT_STRUCT
) {
7063 int v
= btype
.ref
->v
;
7064 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) >= SYM_FIRST_ANOM
)
7065 tcc_warning("unnamed struct/union that defines no instances");
7069 if (IS_ENUM(btype
.t
)) {
7074 while (1) { /* iterate thru each declaration */
7076 /* If the base type itself was an array type of unspecified
7077 size (like in 'typedef int arr[]; arr x = {1};') then
7078 we will overwrite the unknown size by the real one for
7079 this decl. We need to unshare the ref symbol holding
7081 if ((type
.t
& VT_ARRAY
) && type
.ref
->c
< 0) {
7082 type
.ref
= sym_push(SYM_FIELD
, &type
.ref
->type
, 0, type
.ref
->c
);
7084 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
7088 type_to_str(buf
, sizeof(buf
), &type
, get_tok_str(v
, NULL
));
7089 printf("type = '%s'\n", buf
);
7092 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
7093 if ((type
.t
& VT_STATIC
) && (l
== VT_LOCAL
)) {
7094 tcc_error("function without file scope cannot be static");
7096 /* if old style function prototype, we accept a
7099 if (sym
->f
.func_type
== FUNC_OLD
&& l
== VT_CONST
)
7100 decl0(VT_CMP
, 0, sym
);
7103 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
7104 ad
.asm_label
= asm_label_instr();
7105 /* parse one last attribute list, after asm label */
7106 parse_attribute(&ad
);
7111 #ifdef TCC_TARGET_PE
7112 if (ad
.a
.dllimport
|| ad
.a
.dllexport
) {
7113 if (type
.t
& (VT_STATIC
|VT_TYPEDEF
))
7114 tcc_error("cannot have dll linkage with static or typedef");
7115 if (ad
.a
.dllimport
) {
7116 if ((type
.t
& VT_BTYPE
) == VT_FUNC
)
7119 type
.t
|= VT_EXTERN
;
7125 tcc_error("cannot use local functions");
7126 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
7127 expect("function definition");
7129 /* reject abstract declarators in function definition
7130 make old style params without decl have int type */
7132 while ((sym
= sym
->next
) != NULL
) {
7133 if (!(sym
->v
& ~SYM_FIELD
))
7134 expect("identifier");
7135 if (sym
->type
.t
== VT_VOID
)
7136 sym
->type
= int_type
;
7139 /* XXX: cannot do better now: convert extern line to static inline */
7140 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
7141 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
7146 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
7149 ref
= sym
->type
.ref
;
7151 /* use func_call from prototype if not defined */
7152 if (ref
->f
.func_call
!= FUNC_CDECL
7153 && type
.ref
->f
.func_call
== FUNC_CDECL
)
7154 type
.ref
->f
.func_call
= ref
->f
.func_call
;
7156 /* use static from prototype */
7157 if (sym
->type
.t
& VT_STATIC
)
7158 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
7160 /* If the definition has no visibility use the
7161 one from prototype. */
7162 if (!type
.ref
->a
.visibility
)
7163 type
.ref
->a
.visibility
= ref
->a
.visibility
;
7164 /* apply other storage attributes from prototype */
7165 type
.ref
->a
.dllexport
|= ref
->a
.dllexport
;
7166 type
.ref
->a
.weak
|= ref
->a
.weak
;
7168 if (!is_compatible_types(&sym
->type
, &type
)) {
7170 tcc_error("incompatible types for redefinition of '%s'",
7171 get_tok_str(v
, NULL
));
7173 if (ref
->f
.func_body
)
7174 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
7175 /* if symbol is already defined, then put complete type */
7179 /* put function symbol */
7180 sym
= global_identifier_push(v
, type
.t
, 0);
7181 sym
->type
.ref
= type
.ref
;
7184 sym
->type
.ref
->f
.func_body
= 1;
7185 sym
->r
= VT_SYM
| VT_CONST
;
7186 patch_storage(sym
, &ad
, NULL
);
7188 /* static inline functions are just recorded as a kind
7189 of macro. Their code will be emitted at the end of
7190 the compilation unit only if they are used */
7191 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
7192 (VT_INLINE
| VT_STATIC
)) {
7193 struct InlineFunc
*fn
;
7194 const char *filename
;
7196 filename
= file
? file
->filename
: "";
7197 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
7198 strcpy(fn
->filename
, filename
);
7200 skip_or_save_block(&fn
->func_str
);
7201 dynarray_add(&tcc_state
->inline_fns
,
7202 &tcc_state
->nb_inline_fns
, fn
);
7204 /* compute text section */
7205 cur_text_section
= ad
.section
;
7206 if (!cur_text_section
)
7207 cur_text_section
= text_section
;
7213 /* find parameter in function parameter list */
7214 for (sym
= func_sym
->next
; sym
; sym
= sym
->next
)
7215 if ((sym
->v
& ~SYM_FIELD
) == v
)
7217 tcc_error("declaration for parameter '%s' but no such parameter",
7218 get_tok_str(v
, NULL
));
7220 if (type
.t
& VT_STORAGE
) /* 'register' is okay */
7221 tcc_error("storage class specified for '%s'",
7222 get_tok_str(v
, NULL
));
7223 if (sym
->type
.t
!= VT_VOID
)
7224 tcc_error("redefinition of parameter '%s'",
7225 get_tok_str(v
, NULL
));
7226 convert_parameter_type(&type
);
7228 } else if (type
.t
& VT_TYPEDEF
) {
7229 /* save typedefed type */
7230 /* XXX: test storage specifiers ? */
7232 if (sym
&& sym
->sym_scope
== local_scope
) {
7233 if (!is_compatible_types(&sym
->type
, &type
)
7234 || !(sym
->type
.t
& VT_TYPEDEF
))
7235 tcc_error("incompatible redefinition of '%s'",
7236 get_tok_str(v
, NULL
));
7239 sym
= sym_push(v
, &type
, 0, 0);
7245 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
7246 /* external function definition */
7247 /* specific case for func_call attribute */
7249 } else if (!(type
.t
& VT_ARRAY
)) {
7250 /* not lvalue if array */
7251 r
|= lvalue_type(type
.t
);
7253 has_init
= (tok
== '=');
7254 if (has_init
&& (type
.t
& VT_VLA
))
7255 tcc_error("variable length array cannot be initialized");
7256 if (((type
.t
& VT_EXTERN
) && (!has_init
|| l
!= VT_CONST
)) ||
7257 ((type
.t
& VT_BTYPE
) == VT_FUNC
) ||
7258 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
7259 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
7260 /* external variable or function */
7261 /* NOTE: as GCC, uninitialized global static
7262 arrays of null size are considered as
7264 sym
= external_sym(v
, &type
, r
, &ad
);
7265 if (ad
.alias_target
) {
7269 alias_target
= sym_find(ad
.alias_target
);
7270 if (!alias_target
|| !alias_target
->c
)
7271 tcc_error("unsupported forward __alias__ attribute");
7272 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[alias_target
->c
];
7273 tsec
.sh_num
= esym
->st_shndx
;
7274 /* Local statics have a scope until now (for
7275 warnings), remove it here. */
7277 put_extern_sym2(sym
, &tsec
, esym
->st_value
, esym
->st_size
, 0);
7280 if (type
.t
& VT_STATIC
)
7286 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, l
);
7290 if (is_for_loop_init
)
7303 ST_FUNC
void decl(int l
)
7308 /* ------------------------------------------------------------------------- */