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
22 /********************************************************/
23 /* global variables */
25 /* loc : local variable index
26 ind : output code index
28 anon_sym: anonymous symbol index
30 ST_DATA
int rsym
, anon_sym
, ind
, loc
;
32 ST_DATA Sym
*sym_free_first
;
33 ST_DATA
void **sym_pools
;
34 ST_DATA
int nb_sym_pools
;
36 ST_DATA Sym
*global_stack
;
37 ST_DATA Sym
*local_stack
;
38 ST_DATA Sym
*define_stack
;
39 ST_DATA Sym
*global_label_stack
;
40 ST_DATA Sym
*local_label_stack
;
42 static Sym
*all_cleanups
, *current_cleanups
, *pending_gotos
;
45 static int local_scope
;
47 static int section_sym
;
49 ST_DATA
int vlas_in_scope
; /* number of VLAs that are currently in scope */
50 ST_DATA
int vla_sp_root_loc
; /* vla_sp_loc for SP before any VLAs were pushed */
51 ST_DATA
int vla_sp_loc
; /* Pointer to variable holding location to store stack pointer on the stack when modifying stack pointer */
53 ST_DATA SValue __vstack
[1+VSTACK_SIZE
], *vtop
, *pvtop
;
55 ST_DATA
int const_wanted
; /* true if constant wanted */
56 ST_DATA
int nocode_wanted
; /* no code generation wanted */
57 #define NODATA_WANTED (nocode_wanted > 0) /* no static data output wanted either */
58 #define STATIC_DATA_WANTED (nocode_wanted & 0xC0000000) /* only static data output */
59 ST_DATA
int global_expr
; /* true if compound literals must be allocated globally (used during initializers parsing */
60 ST_DATA CType func_vt
; /* current function return type (used by return instruction) */
61 ST_DATA
int func_var
; /* true if current function is variadic (used by return instruction) */
63 ST_DATA
int last_line_num
, last_ind
, func_ind
; /* debug last line number and pc */
64 ST_DATA
const char *funcname
;
67 ST_DATA CType char_pointer_type
, func_old_type
, int_type
, size_type
, ptrdiff_type
;
69 ST_DATA
struct switch_t
{
73 } **p
; int n
; /* list of case ranges */
74 int def_sym
; /* default symbol */
75 } *cur_switch
; /* current switch */
77 #define MAX_TEMP_LOCAL_VARIABLE_NUMBER 0x4
78 /*list of temporary local variables on the stack in current function. */
79 ST_DATA
struct temp_local_variable
{
80 int location
; //offset on stack. Svalue.c.i
83 } arr_temp_local_vars
[MAX_TEMP_LOCAL_VARIABLE_NUMBER
];
84 short nb_temp_local_vars
;
86 /* ------------------------------------------------------------------------- */
88 static void gen_cast(CType
*type
);
89 static void gen_cast_s(int t
);
90 static inline CType
*pointed_type(CType
*type
);
91 static int is_compatible_types(CType
*type1
, CType
*type2
);
92 static int parse_btype(CType
*type
, AttributeDef
*ad
);
93 static CType
*type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
);
94 static void parse_expr_type(CType
*type
);
95 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
);
96 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
, int flags
);
97 static void block(int *bsym
, Sym
*bcl
, int *csym
, Sym
*ccl
, int is_expr
);
98 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
, int has_init
, int v
, int scope
);
99 static void decl(int l
);
100 static int decl0(int l
, int is_for_loop_init
, Sym
*);
101 static void expr_eq(void);
102 static void vla_runtime_type_size(CType
*type
, int *a
);
103 static void vla_sp_restore(void);
104 static void vla_sp_restore_root(void);
105 static int is_compatible_unqualified_types(CType
*type1
, CType
*type2
);
106 static inline int64_t expr_const64(void);
107 static void vpush64(int ty
, unsigned long long v
);
108 static void vpush(CType
*type
);
109 static int gvtst(int inv
, int t
);
110 static void gen_inline_functions(TCCState
*s
);
111 static void skip_or_save_block(TokenString
**str
);
112 static void gv_dup(void);
113 static int get_temp_local_var(int size
,int align
);
114 static void clear_temp_local_var_list();
117 static void reset_local_scope(void)
119 if (current_cleanups
)
120 tcc_error("ICE current_cleanups");
121 sym_pop(&all_cleanups
, NULL
, 0);
125 ST_INLN
int is_float(int t
)
129 return bt
== VT_LDOUBLE
|| bt
== VT_DOUBLE
|| bt
== VT_FLOAT
|| bt
== VT_QFLOAT
;
132 /* we use our own 'finite' function to avoid potential problems with
133 non standard math libs */
134 /* XXX: endianness dependent */
135 ST_FUNC
int ieee_finite(double d
)
138 memcpy(p
, &d
, sizeof(double));
139 return ((unsigned)((p
[1] | 0x800fffff) + 1)) >> 31;
142 /* compiling intel long double natively */
143 #if (defined __i386__ || defined __x86_64__) \
144 && (defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64)
145 # define TCC_IS_NATIVE_387
148 ST_FUNC
void test_lvalue(void)
150 if (!(vtop
->r
& VT_LVAL
))
154 ST_FUNC
void check_vstack(void)
157 tcc_error("internal compiler error: vstack leak (%d)", vtop
- pvtop
);
160 /* ------------------------------------------------------------------------- */
161 /* vstack debugging aid */
164 void pv (const char *lbl
, int a
, int b
)
167 for (i
= a
; i
< a
+ b
; ++i
) {
168 SValue
*p
= &vtop
[-i
];
169 printf("%s vtop[-%d] : type.t:%04x r:%04x r2:%04x c.i:%d\n",
170 lbl
, i
, p
->type
.t
, p
->r
, p
->r2
, (int)p
->c
.i
);
175 /* ------------------------------------------------------------------------- */
176 /* start of translation unit info */
177 ST_FUNC
void tcc_debug_start(TCCState
*s1
)
182 /* file info: full path + filename */
183 section_sym
= put_elf_sym(symtab_section
, 0, 0,
184 ELFW(ST_INFO
)(STB_LOCAL
, STT_SECTION
), 0,
185 text_section
->sh_num
, NULL
);
186 getcwd(buf
, sizeof(buf
));
188 normalize_slashes(buf
);
190 pstrcat(buf
, sizeof(buf
), "/");
191 put_stabs_r(buf
, N_SO
, 0, 0,
192 text_section
->data_offset
, text_section
, section_sym
);
193 put_stabs_r(file
->filename
, N_SO
, 0, 0,
194 text_section
->data_offset
, text_section
, section_sym
);
199 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
200 symbols can be safely used */
201 put_elf_sym(symtab_section
, 0, 0,
202 ELFW(ST_INFO
)(STB_LOCAL
, STT_FILE
), 0,
203 SHN_ABS
, file
->filename
);
206 /* put end of translation unit info */
207 ST_FUNC
void tcc_debug_end(TCCState
*s1
)
211 put_stabs_r(NULL
, N_SO
, 0, 0,
212 text_section
->data_offset
, text_section
, section_sym
);
216 /* generate line number info */
217 ST_FUNC
void tcc_debug_line(TCCState
*s1
)
221 if ((last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
222 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
224 last_line_num
= file
->line_num
;
228 /* put function symbol */
229 ST_FUNC
void tcc_debug_funcstart(TCCState
*s1
, Sym
*sym
)
237 /* XXX: we put here a dummy type */
238 snprintf(buf
, sizeof(buf
), "%s:%c1",
239 funcname
, sym
->type
.t
& VT_STATIC ?
'f' : 'F');
240 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
241 cur_text_section
, sym
->c
);
242 /* //gr gdb wants a line at the function */
243 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
249 /* put function size */
250 ST_FUNC
void tcc_debug_funcend(TCCState
*s1
, int size
)
254 put_stabn(N_FUN
, 0, 0, size
);
257 /* ------------------------------------------------------------------------- */
258 ST_FUNC
int tccgen_compile(TCCState
*s1
)
260 cur_text_section
= NULL
;
262 anon_sym
= SYM_FIRST_ANOM
;
265 nocode_wanted
= 0x80000000;
267 /* define some often used types */
269 char_pointer_type
.t
= VT_BYTE
;
270 mk_pointer(&char_pointer_type
);
272 size_type
.t
= VT_INT
| VT_UNSIGNED
;
273 ptrdiff_type
.t
= VT_INT
;
275 size_type
.t
= VT_LLONG
| VT_UNSIGNED
;
276 ptrdiff_type
.t
= VT_LLONG
;
278 size_type
.t
= VT_LONG
| VT_LLONG
| VT_UNSIGNED
;
279 ptrdiff_type
.t
= VT_LONG
| VT_LLONG
;
281 func_old_type
.t
= VT_FUNC
;
282 func_old_type
.ref
= sym_push(SYM_FIELD
, &int_type
, 0, 0);
283 func_old_type
.ref
->f
.func_call
= FUNC_CDECL
;
284 func_old_type
.ref
->f
.func_type
= FUNC_OLD
;
288 #ifdef TCC_TARGET_ARM
293 printf("%s: **** new file\n", file
->filename
);
296 parse_flags
= PARSE_FLAG_PREPROCESS
| PARSE_FLAG_TOK_NUM
| PARSE_FLAG_TOK_STR
;
299 gen_inline_functions(s1
);
301 /* end of translation unit info */
306 /* ------------------------------------------------------------------------- */
307 ST_FUNC ElfSym
*elfsym(Sym
*s
)
311 return &((ElfSym
*)symtab_section
->data
)[s
->c
];
314 /* apply storage attributes to Elf symbol */
315 ST_FUNC
void update_storage(Sym
*sym
)
318 int sym_bind
, old_sym_bind
;
324 if (sym
->a
.visibility
)
325 esym
->st_other
= (esym
->st_other
& ~ELFW(ST_VISIBILITY
)(-1))
328 if ((sym
->type
.t
& VT_STATIC
)
329 || (sym
->type
.t
& (VT_EXTERN
| VT_INLINE
)) == VT_INLINE
)
330 sym_bind
= STB_LOCAL
;
331 else if (sym
->a
.weak
)
334 sym_bind
= STB_GLOBAL
;
335 old_sym_bind
= ELFW(ST_BIND
)(esym
->st_info
);
336 if (sym_bind
!= old_sym_bind
) {
337 esym
->st_info
= ELFW(ST_INFO
)(sym_bind
, ELFW(ST_TYPE
)(esym
->st_info
));
341 if (sym
->a
.dllimport
)
342 esym
->st_other
|= ST_PE_IMPORT
;
343 if (sym
->a
.dllexport
)
344 esym
->st_other
|= ST_PE_EXPORT
;
348 printf("storage %s: bind=%c vis=%d exp=%d imp=%d\n",
349 get_tok_str(sym
->v
, NULL
),
350 sym_bind
== STB_WEAK ?
'w' : sym_bind
== STB_LOCAL ?
'l' : 'g',
358 /* ------------------------------------------------------------------------- */
359 /* update sym->c so that it points to an external symbol in section
360 'section' with value 'value' */
362 ST_FUNC
void put_extern_sym2(Sym
*sym
, int sh_num
,
363 addr_t value
, unsigned long size
,
364 int can_add_underscore
)
366 int sym_type
, sym_bind
, info
, other
, t
;
370 #ifdef CONFIG_TCC_BCHECK
375 name
= get_tok_str(sym
->v
, NULL
);
376 #ifdef CONFIG_TCC_BCHECK
377 if (tcc_state
->do_bounds_check
) {
378 /* XXX: avoid doing that for statics ? */
379 /* if bound checking is activated, we change some function
380 names by adding the "__bound" prefix */
383 /* XXX: we rely only on malloc hooks */
396 strcpy(buf
, "__bound_");
404 if ((t
& VT_BTYPE
) == VT_FUNC
) {
406 } else if ((t
& VT_BTYPE
) == VT_VOID
) {
407 sym_type
= STT_NOTYPE
;
409 sym_type
= STT_OBJECT
;
411 if ((t
& VT_STATIC
) || (t
& (VT_EXTERN
| VT_INLINE
)) == VT_INLINE
)
412 sym_bind
= STB_LOCAL
;
414 sym_bind
= STB_GLOBAL
;
417 if (sym_type
== STT_FUNC
&& sym
->type
.ref
) {
418 Sym
*ref
= sym
->type
.ref
;
419 if (ref
->a
.nodecorate
) {
420 can_add_underscore
= 0;
422 if (ref
->f
.func_call
== FUNC_STDCALL
&& can_add_underscore
) {
423 sprintf(buf1
, "_%s@%d", name
, ref
->f
.func_args
* PTR_SIZE
);
425 other
|= ST_PE_STDCALL
;
426 can_add_underscore
= 0;
430 if (tcc_state
->leading_underscore
&& can_add_underscore
) {
432 pstrcpy(buf1
+ 1, sizeof(buf1
) - 1, name
);
436 name
= get_tok_str(sym
->asm_label
, NULL
);
437 info
= ELFW(ST_INFO
)(sym_bind
, sym_type
);
438 sym
->c
= put_elf_sym(symtab_section
, value
, size
, info
, other
, sh_num
, name
);
441 esym
->st_value
= value
;
442 esym
->st_size
= size
;
443 esym
->st_shndx
= sh_num
;
448 ST_FUNC
void put_extern_sym(Sym
*sym
, Section
*section
,
449 addr_t value
, unsigned long size
)
451 int sh_num
= section ? section
->sh_num
: SHN_UNDEF
;
452 put_extern_sym2(sym
, sh_num
, value
, size
, 1);
455 /* add a new relocation entry to symbol 'sym' in section 's' */
456 ST_FUNC
void greloca(Section
*s
, Sym
*sym
, unsigned long offset
, int type
,
461 if (nocode_wanted
&& s
== cur_text_section
)
466 put_extern_sym(sym
, NULL
, 0, 0);
470 /* now we can add ELF relocation info */
471 put_elf_reloca(symtab_section
, s
, offset
, type
, c
, addend
);
475 ST_FUNC
void greloc(Section
*s
, Sym
*sym
, unsigned long offset
, int type
)
477 greloca(s
, sym
, offset
, type
, 0);
481 /* ------------------------------------------------------------------------- */
482 /* symbol allocator */
483 static Sym
*__sym_malloc(void)
485 Sym
*sym_pool
, *sym
, *last_sym
;
488 sym_pool
= tcc_malloc(SYM_POOL_NB
* sizeof(Sym
));
489 dynarray_add(&sym_pools
, &nb_sym_pools
, sym_pool
);
491 last_sym
= sym_free_first
;
493 for(i
= 0; i
< SYM_POOL_NB
; i
++) {
494 sym
->next
= last_sym
;
498 sym_free_first
= last_sym
;
502 static inline Sym
*sym_malloc(void)
506 sym
= sym_free_first
;
508 sym
= __sym_malloc();
509 sym_free_first
= sym
->next
;
512 sym
= tcc_malloc(sizeof(Sym
));
517 ST_INLN
void sym_free(Sym
*sym
)
520 sym
->next
= sym_free_first
;
521 sym_free_first
= sym
;
527 /* push, without hashing */
528 ST_FUNC Sym
*sym_push2(Sym
**ps
, int v
, int t
, int c
)
533 memset(s
, 0, sizeof *s
);
543 /* find a symbol and return its associated structure. 's' is the top
544 of the symbol stack */
545 ST_FUNC Sym
*sym_find2(Sym
*s
, int v
)
557 /* structure lookup */
558 ST_INLN Sym
*struct_find(int v
)
561 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
563 return table_ident
[v
]->sym_struct
;
566 /* find an identifier */
567 ST_INLN Sym
*sym_find(int v
)
570 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
572 return table_ident
[v
]->sym_identifier
;
575 static int sym_scope(Sym
*s
)
577 if (IS_ENUM_VAL (s
->type
.t
))
578 return s
->type
.ref
->sym_scope
;
583 /* push a given symbol on the symbol stack */
584 ST_FUNC Sym
*sym_push(int v
, CType
*type
, int r
, int c
)
593 s
= sym_push2(ps
, v
, type
->t
, c
);
594 s
->type
.ref
= type
->ref
;
596 /* don't record fields or anonymous symbols */
598 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
599 /* record symbol in token array */
600 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
602 ps
= &ts
->sym_struct
;
604 ps
= &ts
->sym_identifier
;
607 s
->sym_scope
= local_scope
;
608 if (s
->prev_tok
&& sym_scope(s
->prev_tok
) == s
->sym_scope
)
609 tcc_error("redeclaration of '%s'",
610 get_tok_str(v
& ~SYM_STRUCT
, NULL
));
615 /* push a global identifier */
616 ST_FUNC Sym
*global_identifier_push(int v
, int t
, int c
)
619 s
= sym_push2(&global_stack
, v
, t
, c
);
620 s
->r
= VT_CONST
| VT_SYM
;
621 /* don't record anonymous symbol */
622 if (v
< SYM_FIRST_ANOM
) {
623 ps
= &table_ident
[v
- TOK_IDENT
]->sym_identifier
;
624 /* modify the top most local identifier, so that sym_identifier will
625 point to 's' when popped; happens when called from inline asm */
626 while (*ps
!= NULL
&& (*ps
)->sym_scope
)
627 ps
= &(*ps
)->prev_tok
;
634 /* pop symbols until top reaches 'b'. If KEEP is non-zero don't really
635 pop them yet from the list, but do remove them from the token array. */
636 ST_FUNC
void sym_pop(Sym
**ptop
, Sym
*b
, int keep
)
646 /* remove symbol in token array */
648 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
649 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
651 ps
= &ts
->sym_struct
;
653 ps
= &ts
->sym_identifier
;
664 /* ------------------------------------------------------------------------- */
666 static void vsetc(CType
*type
, int r
, CValue
*vc
)
670 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
671 tcc_error("memory full (vstack)");
672 /* cannot let cpu flags if other instruction are generated. Also
673 avoid leaving VT_JMP anywhere except on the top of the stack
674 because it would complicate the code generator.
676 Don't do this when nocode_wanted. vtop might come from
677 !nocode_wanted regions (see 88_codeopt.c) and transforming
678 it to a register without actually generating code is wrong
679 as their value might still be used for real. All values
680 we push under nocode_wanted will eventually be popped
681 again, so that the VT_CMP/VT_JMP value will be in vtop
682 when code is unsuppressed again.
684 Same logic below in vswap(); */
685 if (vtop
>= vstack
&& !nocode_wanted
) {
686 v
= vtop
->r
& VT_VALMASK
;
687 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
699 ST_FUNC
void vswap(void)
702 /* cannot vswap cpu flags. See comment at vsetc() above */
703 if (vtop
>= vstack
&& !nocode_wanted
) {
704 int v
= vtop
->r
& VT_VALMASK
;
705 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
713 /* pop stack value */
714 ST_FUNC
void vpop(void)
717 v
= vtop
->r
& VT_VALMASK
;
718 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
719 /* for x86, we need to pop the FP stack */
721 o(0xd8dd); /* fstp %st(0) */
724 if (v
== VT_JMP
|| v
== VT_JMPI
) {
725 /* need to put correct jump if && or || without test */
731 /* push constant of type "type" with useless value */
732 ST_FUNC
void vpush(CType
*type
)
734 vset(type
, VT_CONST
, 0);
737 /* push integer constant */
738 ST_FUNC
void vpushi(int v
)
742 vsetc(&int_type
, VT_CONST
, &cval
);
745 /* push a pointer sized constant */
746 static void vpushs(addr_t v
)
750 vsetc(&size_type
, VT_CONST
, &cval
);
753 /* push arbitrary 64bit constant */
754 ST_FUNC
void vpush64(int ty
, unsigned long long v
)
761 vsetc(&ctype
, VT_CONST
, &cval
);
764 /* push long long constant */
765 static inline void vpushll(long long v
)
767 vpush64(VT_LLONG
, v
);
770 ST_FUNC
void vset(CType
*type
, int r
, int v
)
775 vsetc(type
, r
, &cval
);
778 static void vseti(int r
, int v
)
786 ST_FUNC
void vpushv(SValue
*v
)
788 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
789 tcc_error("memory full (vstack)");
794 static void vdup(void)
799 /* rotate n first stack elements to the bottom
800 I1 ... In -> I2 ... In I1 [top is right]
802 ST_FUNC
void vrotb(int n
)
813 /* rotate the n elements before entry e towards the top
814 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
816 ST_FUNC
void vrote(SValue
*e
, int n
)
822 for(i
= 0;i
< n
- 1; i
++)
827 /* rotate n first stack elements to the top
828 I1 ... In -> In I1 ... I(n-1) [top is right]
830 ST_FUNC
void vrott(int n
)
835 /* push a symbol value of TYPE */
836 static inline void vpushsym(CType
*type
, Sym
*sym
)
840 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
844 /* Return a static symbol pointing to a section */
845 ST_FUNC Sym
*get_sym_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
851 sym
= sym_push(v
, type
, VT_CONST
| VT_SYM
, 0);
852 sym
->type
.t
|= VT_STATIC
;
853 put_extern_sym(sym
, sec
, offset
, size
);
857 /* push a reference to a section offset by adding a dummy symbol */
858 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
860 vpushsym(type
, get_sym_ref(type
, sec
, offset
, size
));
863 /* define a new external reference to a symbol 'v' of type 'u' */
864 ST_FUNC Sym
*external_global_sym(int v
, CType
*type
)
870 /* push forward reference */
871 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
872 s
->type
.ref
= type
->ref
;
873 } else if (IS_ASM_SYM(s
)) {
874 s
->type
.t
= type
->t
| (s
->type
.t
& VT_EXTERN
);
875 s
->type
.ref
= type
->ref
;
881 /* Merge symbol attributes. */
882 static void merge_symattr(struct SymAttr
*sa
, struct SymAttr
*sa1
)
884 if (sa1
->aligned
&& !sa
->aligned
)
885 sa
->aligned
= sa1
->aligned
;
886 sa
->packed
|= sa1
->packed
;
887 sa
->weak
|= sa1
->weak
;
888 if (sa1
->visibility
!= STV_DEFAULT
) {
889 int vis
= sa
->visibility
;
890 if (vis
== STV_DEFAULT
891 || vis
> sa1
->visibility
)
892 vis
= sa1
->visibility
;
893 sa
->visibility
= vis
;
895 sa
->dllexport
|= sa1
->dllexport
;
896 sa
->nodecorate
|= sa1
->nodecorate
;
897 sa
->dllimport
|= sa1
->dllimport
;
900 /* Merge function attributes. */
901 static void merge_funcattr(struct FuncAttr
*fa
, struct FuncAttr
*fa1
)
903 if (fa1
->func_call
&& !fa
->func_call
)
904 fa
->func_call
= fa1
->func_call
;
905 if (fa1
->func_type
&& !fa
->func_type
)
906 fa
->func_type
= fa1
->func_type
;
907 if (fa1
->func_args
&& !fa
->func_args
)
908 fa
->func_args
= fa1
->func_args
;
911 /* Merge attributes. */
912 static void merge_attr(AttributeDef
*ad
, AttributeDef
*ad1
)
914 merge_symattr(&ad
->a
, &ad1
->a
);
915 merge_funcattr(&ad
->f
, &ad1
->f
);
918 ad
->section
= ad1
->section
;
919 if (ad1
->alias_target
)
920 ad
->alias_target
= ad1
->alias_target
;
922 ad
->asm_label
= ad1
->asm_label
;
924 ad
->attr_mode
= ad1
->attr_mode
;
927 /* Merge some type attributes. */
928 static void patch_type(Sym
*sym
, CType
*type
)
930 if ((!(type
->t
& VT_EXTERN
) || IS_ENUM_VAL(sym
->type
.t
))
931 && (type
->t
& VT_BTYPE
) != VT_FUNC
) {
932 if (!(sym
->type
.t
& VT_EXTERN
))
933 tcc_error("redefinition of '%s'", get_tok_str(sym
->v
, NULL
));
934 sym
->type
.t
&= ~VT_EXTERN
;
937 if (IS_ASM_SYM(sym
)) {
938 /* stay static if both are static */
939 sym
->type
.t
= type
->t
& (sym
->type
.t
| ~VT_STATIC
);
940 sym
->type
.ref
= type
->ref
;
943 if (!is_compatible_types(&sym
->type
, type
)) {
944 tcc_error("incompatible types for redefinition of '%s'",
945 get_tok_str(sym
->v
, NULL
));
947 } else if ((sym
->type
.t
& VT_BTYPE
) == VT_FUNC
) {
948 int static_proto
= sym
->type
.t
& VT_STATIC
;
949 /* warn if static follows non-static function declaration */
950 if ((type
->t
& VT_STATIC
) && !static_proto
951 /* XXX this test for inline shouldn't be here. Until we
952 implement gnu-inline mode again it silences a warning for
953 mingw caused by our workarounds. */
954 && !((type
->t
| sym
->type
.t
) & VT_INLINE
))
955 tcc_warning("static storage ignored for redefinition of '%s'",
956 get_tok_str(sym
->v
, NULL
));
958 /* Force external definition if unequal inline specifier
959 or an explicit extern one. */
960 if ((sym
->type
.t
| type
->t
) & VT_STATIC
) {
961 type
->t
|= sym
->type
.t
& VT_INLINE
;
962 sym
->type
.t
|= type
->t
& VT_INLINE
;
963 } else if (((type
->t
& VT_INLINE
) != (sym
->type
.t
& VT_INLINE
)
964 || (type
->t
| sym
->type
.t
) & VT_EXTERN
)
966 type
->t
&= ~VT_INLINE
;
967 sym
->type
.t
&= ~VT_INLINE
;
969 if (0 == (type
->t
& VT_EXTERN
)) {
970 /* put complete type, use static from prototype, but don't
971 overwrite type.ref, it might contain parameter names */
972 sym
->type
.t
= (type
->t
& ~VT_STATIC
) | static_proto
;
975 if ((sym
->type
.t
& VT_ARRAY
) && type
->ref
->c
>= 0) {
976 /* set array size if it was omitted in extern declaration */
977 if (sym
->type
.ref
->c
< 0)
978 sym
->type
.ref
->c
= type
->ref
->c
;
979 else if (sym
->type
.ref
->c
!= type
->ref
->c
)
980 tcc_error("conflicting type for '%s'", get_tok_str(sym
->v
, NULL
));
982 if ((type
->t
^ sym
->type
.t
) & VT_STATIC
)
983 tcc_warning("storage mismatch for redefinition of '%s'",
984 get_tok_str(sym
->v
, NULL
));
989 /* Merge some storage attributes. */
990 static void patch_storage(Sym
*sym
, AttributeDef
*ad
, CType
*type
)
993 patch_type(sym
, type
);
996 if (sym
->a
.dllimport
!= ad
->a
.dllimport
)
997 tcc_error("incompatible dll linkage for redefinition of '%s'",
998 get_tok_str(sym
->v
, NULL
));
1000 merge_symattr(&sym
->a
, &ad
->a
);
1002 sym
->asm_label
= ad
->asm_label
;
1003 update_storage(sym
);
1006 /* define a new external reference to a symbol 'v' */
1007 static Sym
*external_sym(int v
, CType
*type
, int r
, AttributeDef
*ad
)
1011 if (!s
|| (!IS_ASM_SYM(s
) && !(s
->type
.t
& VT_EXTERN
)
1012 && (!(type
->t
& VT_EXTERN
) || s
->sym_scope
)
1013 && (s
->type
.t
& VT_BTYPE
) != VT_FUNC
)) {
1014 if (s
&& !is_compatible_types(&s
->type
, type
))
1015 tcc_error("conflicting types for '%s'", get_tok_str(s
->v
, NULL
));
1016 /* push forward reference */
1017 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
1019 s
->asm_label
= ad
->asm_label
;
1022 if (s
->type
.ref
== func_old_type
.ref
) {
1023 s
->type
.ref
= type
->ref
;
1024 s
->r
= r
| VT_CONST
| VT_SYM
;
1025 s
->type
.t
|= VT_EXTERN
;
1027 patch_storage(s
, ad
, type
);
1032 /* push a reference to global symbol v */
1033 ST_FUNC
void vpush_global_sym(CType
*type
, int v
)
1035 vpushsym(type
, external_global_sym(v
, type
));
1038 /* save registers up to (vtop - n) stack entry */
1039 ST_FUNC
void save_regs(int n
)
1042 for(p
= vstack
, p1
= vtop
- n
; p
<= p1
; p
++)
1046 /* save r to the memory stack, and mark it as being free */
1047 ST_FUNC
void save_reg(int r
)
1049 save_reg_upstack(r
, 0);
1052 /* save r to the memory stack, and mark it as being free,
1053 if seen up to (vtop - n) stack entry */
1054 ST_FUNC
void save_reg_upstack(int r
, int n
)
1056 int l
, saved
, size
, align
;
1060 if ((r
&= VT_VALMASK
) >= VT_CONST
)
1065 /* modify all stack values */
1068 for(p
= vstack
, p1
= vtop
- n
; p
<= p1
; p
++) {
1069 if ((p
->r
& VT_VALMASK
) == r
||
1070 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
1071 /* must save value on stack if not already done */
1073 /* NOTE: must reload 'r' because r might be equal to r2 */
1074 r
= p
->r
& VT_VALMASK
;
1075 /* store register in the stack */
1077 if ((p
->r
& VT_LVAL
) ||
1078 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
1080 type
= &char_pointer_type
;
1084 size
= type_size(type
, &align
);
1085 l
=get_temp_local_var(size
,align
);
1086 sv
.type
.t
= type
->t
;
1087 sv
.r
= VT_LOCAL
| VT_LVAL
;
1090 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1091 /* x86 specific: need to pop fp register ST0 if saved */
1092 if (r
== TREG_ST0
) {
1093 o(0xd8dd); /* fstp %st(0) */
1097 /* special long long case */
1098 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
1105 /* mark that stack entry as being saved on the stack */
1106 if (p
->r
& VT_LVAL
) {
1107 /* also clear the bounded flag because the
1108 relocation address of the function was stored in
1110 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
1112 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
1120 #ifdef TCC_TARGET_ARM
1121 /* find a register of class 'rc2' with at most one reference on stack.
1122 * If none, call get_reg(rc) */
1123 ST_FUNC
int get_reg_ex(int rc
, int rc2
)
1128 for(r
=0;r
<NB_REGS
;r
++) {
1129 if (reg_classes
[r
] & rc2
) {
1132 for(p
= vstack
; p
<= vtop
; p
++) {
1133 if ((p
->r
& VT_VALMASK
) == r
||
1134 (p
->r2
& VT_VALMASK
) == r
)
1145 /* find a free register of class 'rc'. If none, save one register */
1146 ST_FUNC
int get_reg(int rc
)
1151 /* find a free register */
1152 for(r
=0;r
<NB_REGS
;r
++) {
1153 if (reg_classes
[r
] & rc
) {
1156 for(p
=vstack
;p
<=vtop
;p
++) {
1157 if ((p
->r
& VT_VALMASK
) == r
||
1158 (p
->r2
& VT_VALMASK
) == r
)
1166 /* no register left : free the first one on the stack (VERY
1167 IMPORTANT to start from the bottom to ensure that we don't
1168 spill registers used in gen_opi()) */
1169 for(p
=vstack
;p
<=vtop
;p
++) {
1170 /* look at second register (if long long) */
1171 r
= p
->r2
& VT_VALMASK
;
1172 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
1174 r
= p
->r
& VT_VALMASK
;
1175 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
1181 /* Should never comes here */
1185 /* find a free temporary local variable (return the offset on stack) match the size and align. If none, add new temporary stack variable*/
1186 static int get_temp_local_var(int size
,int align
){
1188 struct temp_local_variable
*temp_var
;
1195 for(i
=0;i
<nb_temp_local_vars
;i
++){
1196 temp_var
=&arr_temp_local_vars
[i
];
1197 if(temp_var
->size
<size
||align
!=temp_var
->align
){
1200 /*check if temp_var is free*/
1202 for(p
=vstack
;p
<=vtop
;p
++) {
1204 if(r
==VT_LOCAL
||r
==VT_LLOCAL
){
1205 if(p
->c
.i
==temp_var
->location
){
1212 found_var
=temp_var
->location
;
1218 loc
= (loc
- size
) & -align
;
1219 if(nb_temp_local_vars
<MAX_TEMP_LOCAL_VARIABLE_NUMBER
){
1220 temp_var
=&arr_temp_local_vars
[i
];
1221 temp_var
->location
=loc
;
1222 temp_var
->size
=size
;
1223 temp_var
->align
=align
;
1224 nb_temp_local_vars
++;
1231 static void clear_temp_local_var_list(){
1232 nb_temp_local_vars
=0;
1235 /* move register 's' (of type 't') to 'r', and flush previous value of r to memory
1237 static void move_reg(int r
, int s
, int t
)
1251 /* get address of vtop (vtop MUST BE an lvalue) */
1252 ST_FUNC
void gaddrof(void)
1254 vtop
->r
&= ~VT_LVAL
;
1255 /* tricky: if saved lvalue, then we can go back to lvalue */
1256 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
1257 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
1262 #ifdef CONFIG_TCC_BCHECK
1263 /* generate lvalue bound code */
1264 static void gbound(void)
1269 vtop
->r
&= ~VT_MUSTBOUND
;
1270 /* if lvalue, then use checking code before dereferencing */
1271 if (vtop
->r
& VT_LVAL
) {
1272 /* if not VT_BOUNDED value, then make one */
1273 if (!(vtop
->r
& VT_BOUNDED
)) {
1274 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
1275 /* must save type because we must set it to int to get pointer */
1277 vtop
->type
.t
= VT_PTR
;
1280 gen_bounded_ptr_add();
1281 vtop
->r
|= lval_type
;
1284 /* then check for dereferencing */
1285 gen_bounded_ptr_deref();
1290 static void incr_bf_adr(int o
)
1292 vtop
->type
= char_pointer_type
;
1296 vtop
->type
.t
= (vtop
->type
.t
& ~(VT_BTYPE
|VT_DEFSIGN
))
1297 | (VT_BYTE
|VT_UNSIGNED
);
1298 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
1299 | (VT_LVAL_BYTE
|VT_LVAL_UNSIGNED
|VT_LVAL
);
1302 /* single-byte load mode for packed or otherwise unaligned bitfields */
1303 static void load_packed_bf(CType
*type
, int bit_pos
, int bit_size
)
1306 save_reg_upstack(vtop
->r
, 1);
1307 vpush64(type
->t
& VT_BTYPE
, 0); // B X
1308 bits
= 0, o
= bit_pos
>> 3, bit_pos
&= 7;
1317 vpushi(bit_pos
), gen_op(TOK_SHR
), bit_pos
= 0; // X B Y
1319 vpushi((1 << n
) - 1), gen_op('&');
1322 vpushi(bits
), gen_op(TOK_SHL
);
1325 bits
+= n
, bit_size
-= n
, o
= 1;
1328 if (!(type
->t
& VT_UNSIGNED
)) {
1329 n
= ((type
->t
& VT_BTYPE
) == VT_LLONG ?
64 : 32) - bits
;
1330 vpushi(n
), gen_op(TOK_SHL
);
1331 vpushi(n
), gen_op(TOK_SAR
);
1335 /* single-byte store mode for packed or otherwise unaligned bitfields */
1336 static void store_packed_bf(int bit_pos
, int bit_size
)
1338 int bits
, n
, o
, m
, c
;
1340 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1342 save_reg_upstack(vtop
->r
, 1);
1343 bits
= 0, o
= bit_pos
>> 3, bit_pos
&= 7;
1345 incr_bf_adr(o
); // X B
1347 c ?
vdup() : gv_dup(); // B V X
1350 vpushi(bits
), gen_op(TOK_SHR
);
1352 vpushi(bit_pos
), gen_op(TOK_SHL
);
1357 m
= ((1 << n
) - 1) << bit_pos
;
1358 vpushi(m
), gen_op('&'); // X B V1
1359 vpushv(vtop
-1); // X B V1 B
1360 vpushi(m
& 0x80 ?
~m
& 0x7f : ~m
);
1361 gen_op('&'); // X B V1 B1
1362 gen_op('|'); // X B V2
1364 vdup(), vtop
[-1] = vtop
[-2]; // X B B V2
1365 vstore(), vpop(); // X B
1366 bits
+= n
, bit_size
-= n
, bit_pos
= 0, o
= 1;
1371 static int adjust_bf(SValue
*sv
, int bit_pos
, int bit_size
)
1374 if (0 == sv
->type
.ref
)
1376 t
= sv
->type
.ref
->auxtype
;
1377 if (t
!= -1 && t
!= VT_STRUCT
) {
1378 sv
->type
.t
= (sv
->type
.t
& ~VT_BTYPE
) | t
;
1379 sv
->r
= (sv
->r
& ~VT_LVAL_TYPE
) | lvalue_type(sv
->type
.t
);
1384 /* store vtop a register belonging to class 'rc'. lvalues are
1385 converted to values. Cannot be used if cannot be converted to
1386 register value (such as structures). */
1387 ST_FUNC
int gv(int rc
)
1389 int r
, bit_pos
, bit_size
, size
, align
, rc2
;
1391 /* NOTE: get_reg can modify vstack[] */
1392 if (vtop
->type
.t
& VT_BITFIELD
) {
1395 bit_pos
= BIT_POS(vtop
->type
.t
);
1396 bit_size
= BIT_SIZE(vtop
->type
.t
);
1397 /* remove bit field info to avoid loops */
1398 vtop
->type
.t
&= ~VT_STRUCT_MASK
;
1401 type
.t
= vtop
->type
.t
& VT_UNSIGNED
;
1402 if ((vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
1403 type
.t
|= VT_UNSIGNED
;
1405 r
= adjust_bf(vtop
, bit_pos
, bit_size
);
1407 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
)
1412 if (r
== VT_STRUCT
) {
1413 load_packed_bf(&type
, bit_pos
, bit_size
);
1415 int bits
= (type
.t
& VT_BTYPE
) == VT_LLONG ?
64 : 32;
1416 /* cast to int to propagate signedness in following ops */
1418 /* generate shifts */
1419 vpushi(bits
- (bit_pos
+ bit_size
));
1421 vpushi(bits
- bit_size
);
1422 /* NOTE: transformed to SHR if unsigned */
1427 if (is_float(vtop
->type
.t
) &&
1428 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
1429 unsigned long offset
;
1430 /* CPUs usually cannot use float constants, so we store them
1431 generically in data segment */
1432 size
= type_size(&vtop
->type
, &align
);
1434 size
= 0, align
= 1;
1435 offset
= section_add(data_section
, size
, align
);
1436 vpush_ref(&vtop
->type
, data_section
, offset
, size
);
1438 init_putv(&vtop
->type
, data_section
, offset
);
1441 #ifdef CONFIG_TCC_BCHECK
1442 if (vtop
->r
& VT_MUSTBOUND
)
1446 r
= vtop
->r
& VT_VALMASK
;
1447 rc2
= (rc
& RC_FLOAT
) ? RC_FLOAT
: RC_INT
;
1448 #ifndef TCC_TARGET_ARM64
1451 #ifdef TCC_TARGET_X86_64
1452 else if (rc
== RC_FRET
)
1456 /* need to reload if:
1458 - lvalue (need to dereference pointer)
1459 - already a register, but not in the right class */
1461 || (vtop
->r
& VT_LVAL
)
1462 || !(reg_classes
[r
] & rc
)
1464 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
1465 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
&& !(reg_classes
[vtop
->r2
] & rc2
))
1467 || ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
1473 if (((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
)) {
1474 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
1476 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
1477 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
1478 unsigned long long ll
;
1480 int r2
, original_type
;
1481 original_type
= vtop
->type
.t
;
1482 /* two register type load : expand to two words
1485 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
1488 vtop
->c
.i
= ll
; /* first word */
1490 vtop
->r
= r
; /* save register value */
1491 vpushi(ll
>> 32); /* second word */
1494 if (vtop
->r
& VT_LVAL
) {
1495 /* We do not want to modifier the long long
1496 pointer here, so the safest (and less
1497 efficient) is to save all the other registers
1498 in the stack. XXX: totally inefficient. */
1502 /* lvalue_save: save only if used further down the stack */
1503 save_reg_upstack(vtop
->r
, 1);
1505 /* load from memory */
1506 vtop
->type
.t
= load_type
;
1509 vtop
[-1].r
= r
; /* save register value */
1510 /* increment pointer to get second word */
1511 vtop
->type
.t
= addr_type
;
1516 vtop
->type
.t
= load_type
;
1518 /* move registers */
1521 vtop
[-1].r
= r
; /* save register value */
1522 vtop
->r
= vtop
[-1].r2
;
1524 /* Allocate second register. Here we rely on the fact that
1525 get_reg() tries first to free r2 of an SValue. */
1529 /* write second register */
1531 vtop
->type
.t
= original_type
;
1532 } else if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
1534 /* lvalue of scalar type : need to use lvalue type
1535 because of possible cast */
1538 /* compute memory access type */
1539 if (vtop
->r
& VT_LVAL_BYTE
)
1541 else if (vtop
->r
& VT_LVAL_SHORT
)
1543 if (vtop
->r
& VT_LVAL_UNSIGNED
)
1547 /* restore wanted type */
1550 /* one register type load */
1555 #ifdef TCC_TARGET_C67
1556 /* uses register pairs for doubles */
1557 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
1564 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
1565 ST_FUNC
void gv2(int rc1
, int rc2
)
1569 /* generate more generic register first. But VT_JMP or VT_CMP
1570 values must be generated first in all cases to avoid possible
1572 v
= vtop
[0].r
& VT_VALMASK
;
1573 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
1578 /* test if reload is needed for first register */
1579 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
1589 /* test if reload is needed for first register */
1590 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
1596 #ifndef TCC_TARGET_ARM64
1597 /* wrapper around RC_FRET to return a register by type */
1598 static int rc_fret(int t
)
1600 #ifdef TCC_TARGET_X86_64
1601 if (t
== VT_LDOUBLE
) {
1609 /* wrapper around REG_FRET to return a register by type */
1610 static int reg_fret(int t
)
1612 #ifdef TCC_TARGET_X86_64
1613 if (t
== VT_LDOUBLE
) {
1621 /* expand 64bit on stack in two ints */
1622 ST_FUNC
void lexpand(void)
1625 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
1626 v
= vtop
->r
& (VT_VALMASK
| VT_LVAL
);
1627 if (v
== VT_CONST
) {
1630 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
1636 vtop
[0].r
= vtop
[-1].r2
;
1637 vtop
[0].r2
= vtop
[-1].r2
= VT_CONST
;
1639 vtop
[0].type
.t
= vtop
[-1].type
.t
= VT_INT
| u
;
1644 /* build a long long from two ints */
1645 static void lbuild(int t
)
1647 gv2(RC_INT
, RC_INT
);
1648 vtop
[-1].r2
= vtop
[0].r
;
1649 vtop
[-1].type
.t
= t
;
1654 /* convert stack entry to register and duplicate its value in another
1656 static void gv_dup(void)
1663 if ((t
& VT_BTYPE
) == VT_LLONG
) {
1664 if (t
& VT_BITFIELD
) {
1674 /* stack: H L L1 H1 */
1684 /* duplicate value */
1689 #ifdef TCC_TARGET_X86_64
1690 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1700 load(r1
, &sv
); /* move r to r1 */
1702 /* duplicates value */
1708 /* Generate value test
1710 * Generate a test for any value (jump, comparison and integers) */
1711 ST_FUNC
int gvtst(int inv
, int t
)
1713 int v
= vtop
->r
& VT_VALMASK
;
1714 if (v
!= VT_CMP
&& v
!= VT_JMP
&& v
!= VT_JMPI
) {
1718 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1719 /* constant jmp optimization */
1720 if ((vtop
->c
.i
!= 0) != inv
)
1725 return gtst(inv
, t
);
1729 /* generate CPU independent (unsigned) long long operations */
1730 static void gen_opl(int op
)
1732 int t
, a
, b
, op1
, c
, i
;
1734 unsigned short reg_iret
= REG_IRET
;
1735 unsigned short reg_lret
= REG_LRET
;
1741 func
= TOK___divdi3
;
1744 func
= TOK___udivdi3
;
1747 func
= TOK___moddi3
;
1750 func
= TOK___umoddi3
;
1757 /* call generic long long function */
1758 vpush_global_sym(&func_old_type
, func
);
1763 vtop
->r2
= reg_lret
;
1771 //pv("gen_opl A",0,2);
1777 /* stack: L1 H1 L2 H2 */
1782 vtop
[-2] = vtop
[-3];
1785 /* stack: H1 H2 L1 L2 */
1786 //pv("gen_opl B",0,4);
1792 /* stack: H1 H2 L1 L2 ML MH */
1795 /* stack: ML MH H1 H2 L1 L2 */
1799 /* stack: ML MH H1 L2 H2 L1 */
1804 /* stack: ML MH M1 M2 */
1807 } else if (op
== '+' || op
== '-') {
1808 /* XXX: add non carry method too (for MIPS or alpha) */
1814 /* stack: H1 H2 (L1 op L2) */
1817 gen_op(op1
+ 1); /* TOK_xxxC2 */
1820 /* stack: H1 H2 (L1 op L2) */
1823 /* stack: (L1 op L2) H1 H2 */
1825 /* stack: (L1 op L2) (H1 op H2) */
1833 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1834 t
= vtop
[-1].type
.t
;
1838 /* stack: L H shift */
1840 /* constant: simpler */
1841 /* NOTE: all comments are for SHL. the other cases are
1842 done by swapping words */
1853 if (op
!= TOK_SAR
) {
1886 /* XXX: should provide a faster fallback on x86 ? */
1889 func
= TOK___ashrdi3
;
1892 func
= TOK___lshrdi3
;
1895 func
= TOK___ashldi3
;
1901 /* compare operations */
1907 /* stack: L1 H1 L2 H2 */
1909 vtop
[-1] = vtop
[-2];
1911 /* stack: L1 L2 H1 H2 */
1914 /* when values are equal, we need to compare low words. since
1915 the jump is inverted, we invert the test too. */
1918 else if (op1
== TOK_GT
)
1920 else if (op1
== TOK_ULT
)
1922 else if (op1
== TOK_UGT
)
1932 /* generate non equal test */
1938 /* compare low. Always unsigned */
1942 else if (op1
== TOK_LE
)
1944 else if (op1
== TOK_GT
)
1946 else if (op1
== TOK_GE
)
1957 static uint64_t gen_opic_sdiv(uint64_t a
, uint64_t b
)
1959 uint64_t x
= (a
>> 63 ?
-a
: a
) / (b
>> 63 ?
-b
: b
);
1960 return (a
^ b
) >> 63 ?
-x
: x
;
1963 static int gen_opic_lt(uint64_t a
, uint64_t b
)
1965 return (a
^ (uint64_t)1 << 63) < (b
^ (uint64_t)1 << 63);
1968 /* handle integer constant optimizations and various machine
1970 static void gen_opic(int op
)
1972 SValue
*v1
= vtop
- 1;
1974 int t1
= v1
->type
.t
& VT_BTYPE
;
1975 int t2
= v2
->type
.t
& VT_BTYPE
;
1976 int c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1977 int c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1978 uint64_t l1
= c1 ? v1
->c
.i
: 0;
1979 uint64_t l2
= c2 ? v2
->c
.i
: 0;
1980 int shm
= (t1
== VT_LLONG
) ?
63 : 31;
1982 if (t1
!= VT_LLONG
&& (PTR_SIZE
!= 8 || t1
!= VT_PTR
))
1983 l1
= ((uint32_t)l1
|
1984 (v1
->type
.t
& VT_UNSIGNED ?
0 : -(l1
& 0x80000000)));
1985 if (t2
!= VT_LLONG
&& (PTR_SIZE
!= 8 || t2
!= VT_PTR
))
1986 l2
= ((uint32_t)l2
|
1987 (v2
->type
.t
& VT_UNSIGNED ?
0 : -(l2
& 0x80000000)));
1991 case '+': l1
+= l2
; break;
1992 case '-': l1
-= l2
; break;
1993 case '&': l1
&= l2
; break;
1994 case '^': l1
^= l2
; break;
1995 case '|': l1
|= l2
; break;
1996 case '*': l1
*= l2
; break;
2003 /* if division by zero, generate explicit division */
2006 tcc_error("division by zero in constant");
2010 default: l1
= gen_opic_sdiv(l1
, l2
); break;
2011 case '%': l1
= l1
- l2
* gen_opic_sdiv(l1
, l2
); break;
2012 case TOK_UDIV
: l1
= l1
/ l2
; break;
2013 case TOK_UMOD
: l1
= l1
% l2
; break;
2016 case TOK_SHL
: l1
<<= (l2
& shm
); break;
2017 case TOK_SHR
: l1
>>= (l2
& shm
); break;
2019 l1
= (l1
>> 63) ?
~(~l1
>> (l2
& shm
)) : l1
>> (l2
& shm
);
2022 case TOK_ULT
: l1
= l1
< l2
; break;
2023 case TOK_UGE
: l1
= l1
>= l2
; break;
2024 case TOK_EQ
: l1
= l1
== l2
; break;
2025 case TOK_NE
: l1
= l1
!= l2
; break;
2026 case TOK_ULE
: l1
= l1
<= l2
; break;
2027 case TOK_UGT
: l1
= l1
> l2
; break;
2028 case TOK_LT
: l1
= gen_opic_lt(l1
, l2
); break;
2029 case TOK_GE
: l1
= !gen_opic_lt(l1
, l2
); break;
2030 case TOK_LE
: l1
= !gen_opic_lt(l2
, l1
); break;
2031 case TOK_GT
: l1
= gen_opic_lt(l2
, l1
); break;
2033 case TOK_LAND
: l1
= l1
&& l2
; break;
2034 case TOK_LOR
: l1
= l1
|| l2
; break;
2038 if (t1
!= VT_LLONG
&& (PTR_SIZE
!= 8 || t1
!= VT_PTR
))
2039 l1
= ((uint32_t)l1
|
2040 (v1
->type
.t
& VT_UNSIGNED ?
0 : -(l1
& 0x80000000)));
2044 /* if commutative ops, put c2 as constant */
2045 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
2046 op
== '|' || op
== '*')) {
2048 c2
= c1
; //c = c1, c1 = c2, c2 = c;
2049 l2
= l1
; //l = l1, l1 = l2, l2 = l;
2051 if (!const_wanted
&&
2053 (op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
)) ||
2054 (l1
== -1 && op
== TOK_SAR
))) {
2055 /* treat (0 << x), (0 >> x) and (-1 >> x) as constant */
2057 } else if (!const_wanted
&&
2058 c2
&& ((l2
== 0 && (op
== '&' || op
== '*')) ||
2060 (l2
== -1 || (l2
== 0xFFFFFFFF && t2
!= VT_LLONG
))) ||
2061 (l2
== 1 && (op
== '%' || op
== TOK_UMOD
)))) {
2062 /* treat (x & 0), (x * 0), (x | -1) and (x % 1) as constant */
2067 } else if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
2070 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
2071 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
2074 (l2
== -1 || (l2
== 0xFFFFFFFF && t2
!= VT_LLONG
))))) {
2075 /* filter out NOP operations like x*1, x-0, x&-1... */
2077 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
2078 /* try to use shifts instead of muls or divs */
2079 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
2088 else if (op
== TOK_PDIV
)
2094 } else if (c2
&& (op
== '+' || op
== '-') &&
2095 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
2096 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
2097 /* symbol + constant case */
2101 /* The backends can't always deal with addends to symbols
2102 larger than +-1<<31. Don't construct such. */
2109 /* call low level op generator */
2110 if (t1
== VT_LLONG
|| t2
== VT_LLONG
||
2111 (PTR_SIZE
== 8 && (t1
== VT_PTR
|| t2
== VT_PTR
)))
2119 /* generate a floating point operation with constant propagation */
2120 static void gen_opif(int op
)
2124 #if defined _MSC_VER && defined _AMD64_
2125 /* avoid bad optimization with f1 -= f2 for f1:-0.0, f2:0.0 */
2132 /* currently, we cannot do computations with forward symbols */
2133 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
2134 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
2136 if (v1
->type
.t
== VT_FLOAT
) {
2139 } else if (v1
->type
.t
== VT_DOUBLE
) {
2147 /* NOTE: we only do constant propagation if finite number (not
2148 NaN or infinity) (ANSI spec) */
2149 if (!ieee_finite(f1
) || !ieee_finite(f2
))
2153 case '+': f1
+= f2
; break;
2154 case '-': f1
-= f2
; break;
2155 case '*': f1
*= f2
; break;
2158 /* If not in initializer we need to potentially generate
2159 FP exceptions at runtime, otherwise we want to fold. */
2165 /* XXX: also handles tests ? */
2169 /* XXX: overflow test ? */
2170 if (v1
->type
.t
== VT_FLOAT
) {
2172 } else if (v1
->type
.t
== VT_DOUBLE
) {
2184 static int pointed_size(CType
*type
)
2187 return type_size(pointed_type(type
), &align
);
2190 static void vla_runtime_pointed_size(CType
*type
)
2193 vla_runtime_type_size(pointed_type(type
), &align
);
2196 static inline int is_null_pointer(SValue
*p
)
2198 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
2200 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& (uint32_t)p
->c
.i
== 0) ||
2201 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.i
== 0) ||
2202 ((p
->type
.t
& VT_BTYPE
) == VT_PTR
&&
2203 (PTR_SIZE
== 4 ?
(uint32_t)p
->c
.i
== 0 : p
->c
.i
== 0) &&
2204 ((pointed_type(&p
->type
)->t
& VT_BTYPE
) == VT_VOID
) &&
2205 0 == (pointed_type(&p
->type
)->t
& (VT_CONSTANT
| VT_VOLATILE
))
2208 static inline int is_integer_btype(int bt
)
2210 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
2211 bt
== VT_INT
|| bt
== VT_LLONG
);
2214 /* check types for comparison or subtraction of pointers */
2215 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
2217 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
2220 /* null pointers are accepted for all comparisons as gcc */
2221 if (is_null_pointer(p1
) || is_null_pointer(p2
))
2225 bt1
= type1
->t
& VT_BTYPE
;
2226 bt2
= type2
->t
& VT_BTYPE
;
2227 /* accept comparison between pointer and integer with a warning */
2228 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
2229 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
2230 tcc_warning("comparison between pointer and integer");
2234 /* both must be pointers or implicit function pointers */
2235 if (bt1
== VT_PTR
) {
2236 type1
= pointed_type(type1
);
2237 } else if (bt1
!= VT_FUNC
)
2238 goto invalid_operands
;
2240 if (bt2
== VT_PTR
) {
2241 type2
= pointed_type(type2
);
2242 } else if (bt2
!= VT_FUNC
) {
2244 tcc_error("invalid operands to binary %s", get_tok_str(op
, NULL
));
2246 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2247 (type2
->t
& VT_BTYPE
) == VT_VOID
)
2251 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2252 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2253 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2254 /* gcc-like error if '-' is used */
2256 goto invalid_operands
;
2258 tcc_warning("comparison of distinct pointer types lacks a cast");
2262 /* generic gen_op: handles types problems */
2263 ST_FUNC
void gen_op(int op
)
2265 int u
, t1
, t2
, bt1
, bt2
, t
;
2269 t1
= vtop
[-1].type
.t
;
2270 t2
= vtop
[0].type
.t
;
2271 bt1
= t1
& VT_BTYPE
;
2272 bt2
= t2
& VT_BTYPE
;
2274 if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
2275 tcc_error("operation on a struct");
2276 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
2277 if (bt2
== VT_FUNC
) {
2278 mk_pointer(&vtop
->type
);
2281 if (bt1
== VT_FUNC
) {
2283 mk_pointer(&vtop
->type
);
2288 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
2289 /* at least one operand is a pointer */
2290 /* relational op: must be both pointers */
2291 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
2292 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
2293 /* pointers are handled are unsigned */
2295 t
= VT_LLONG
| VT_UNSIGNED
;
2297 t
= VT_INT
| VT_UNSIGNED
;
2301 /* if both pointers, then it must be the '-' op */
2302 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
2304 tcc_error("cannot use pointers here");
2305 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
2306 /* XXX: check that types are compatible */
2307 if (vtop
[-1].type
.t
& VT_VLA
) {
2308 vla_runtime_pointed_size(&vtop
[-1].type
);
2310 vpushi(pointed_size(&vtop
[-1].type
));
2314 vtop
->type
.t
= ptrdiff_type
.t
;
2318 /* exactly one pointer : must be '+' or '-'. */
2319 if (op
!= '-' && op
!= '+')
2320 tcc_error("cannot use pointers here");
2321 /* Put pointer as first operand */
2322 if (bt2
== VT_PTR
) {
2324 t
= t1
, t1
= t2
, t2
= t
;
2327 if ((vtop
[0].type
.t
& VT_BTYPE
) == VT_LLONG
)
2328 /* XXX: truncate here because gen_opl can't handle ptr + long long */
2331 type1
= vtop
[-1].type
;
2332 type1
.t
&= ~VT_ARRAY
;
2333 if (vtop
[-1].type
.t
& VT_VLA
)
2334 vla_runtime_pointed_size(&vtop
[-1].type
);
2336 u
= pointed_size(&vtop
[-1].type
);
2338 tcc_error("unknown array element size");
2342 /* XXX: cast to int ? (long long case) */
2348 /* #ifdef CONFIG_TCC_BCHECK
2349 The main reason to removing this code:
2356 fprintf(stderr, "v+i-j = %p\n", v+i-j);
2357 fprintf(stderr, "v+(i-j) = %p\n", v+(i-j));
2359 When this code is on. then the output looks like
2361 v+(i-j) = 0xbff84000
2363 /* if evaluating constant expression, no code should be
2364 generated, so no bound check */
2365 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
2366 /* if bounded pointers, we generate a special code to
2373 gen_bounded_ptr_add();
2379 /* put again type if gen_opic() swaped operands */
2382 } else if (is_float(bt1
) || is_float(bt2
)) {
2383 /* compute bigger type and do implicit casts */
2384 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
2386 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
2391 /* floats can only be used for a few operations */
2392 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
2393 (op
< TOK_ULT
|| op
> TOK_GT
))
2394 tcc_error("invalid operands for binary operation");
2396 } else if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
) {
2397 t
= bt1
== VT_LLONG ? VT_LLONG
: VT_INT
;
2398 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (t
| VT_UNSIGNED
))
2400 t
|= (VT_LONG
& t1
);
2402 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
2403 /* cast to biggest op */
2404 t
= VT_LLONG
| VT_LONG
;
2405 if (bt1
== VT_LLONG
)
2407 if (bt2
== VT_LLONG
)
2409 /* convert to unsigned if it does not fit in a long long */
2410 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_LLONG
| VT_UNSIGNED
) ||
2411 (t2
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_LLONG
| VT_UNSIGNED
))
2415 /* integer operations */
2416 t
= VT_INT
| (VT_LONG
& (t1
| t2
));
2417 /* convert to unsigned if it does not fit in an integer */
2418 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_INT
| VT_UNSIGNED
) ||
2419 (t2
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_INT
| VT_UNSIGNED
))
2422 /* XXX: currently, some unsigned operations are explicit, so
2423 we modify them here */
2424 if (t
& VT_UNSIGNED
) {
2431 else if (op
== TOK_LT
)
2433 else if (op
== TOK_GT
)
2435 else if (op
== TOK_LE
)
2437 else if (op
== TOK_GE
)
2445 /* special case for shifts and long long: we keep the shift as
2447 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
2454 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
2455 /* relational op: the result is an int */
2456 vtop
->type
.t
= VT_INT
;
2461 // Make sure that we have converted to an rvalue:
2462 if (vtop
->r
& VT_LVAL
)
2463 gv(is_float(vtop
->type
.t
& VT_BTYPE
) ? RC_FLOAT
: RC_INT
);
2466 #ifndef TCC_TARGET_ARM
2467 /* generic itof for unsigned long long case */
2468 static void gen_cvt_itof1(int t
)
2470 #ifdef TCC_TARGET_ARM64
2473 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
2474 (VT_LLONG
| VT_UNSIGNED
)) {
2477 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
2478 #if LDOUBLE_SIZE != 8
2479 else if (t
== VT_LDOUBLE
)
2480 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
2483 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
2487 vtop
->r
= reg_fret(t
);
2495 /* generic ftoi for unsigned long long case */
2496 static void gen_cvt_ftoi1(int t
)
2498 #ifdef TCC_TARGET_ARM64
2503 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
2504 /* not handled natively */
2505 st
= vtop
->type
.t
& VT_BTYPE
;
2507 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
2508 #if LDOUBLE_SIZE != 8
2509 else if (st
== VT_LDOUBLE
)
2510 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
2513 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
2518 vtop
->r2
= REG_LRET
;
2525 /* force char or short cast */
2526 static void force_charshort_cast(int t
)
2530 /* cannot cast static initializers */
2531 if (STATIC_DATA_WANTED
)
2535 /* XXX: add optimization if lvalue : just change type and offset */
2540 if (t
& VT_UNSIGNED
) {
2541 vpushi((1 << bits
) - 1);
2544 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
)
2550 /* result must be signed or the SAR is converted to an SHL
2551 This was not the case when "t" was a signed short
2552 and the last value on the stack was an unsigned int */
2553 vtop
->type
.t
&= ~VT_UNSIGNED
;
2559 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
2560 static void gen_cast_s(int t
)
2568 static void gen_cast(CType
*type
)
2570 int sbt
, dbt
, sf
, df
, c
, p
;
2572 /* special delayed cast for char/short */
2573 /* XXX: in some cases (multiple cascaded casts), it may still
2575 if (vtop
->r
& VT_MUSTCAST
) {
2576 vtop
->r
&= ~VT_MUSTCAST
;
2577 force_charshort_cast(vtop
->type
.t
);
2580 /* bitfields first get cast to ints */
2581 if (vtop
->type
.t
& VT_BITFIELD
) {
2585 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
2586 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
2591 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
2592 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
2593 #if !defined TCC_IS_NATIVE && !defined TCC_IS_NATIVE_387
2594 c
&= dbt
!= VT_LDOUBLE
;
2597 /* constant case: we can do it now */
2598 /* XXX: in ISOC, cannot do it if error in convert */
2599 if (sbt
== VT_FLOAT
)
2600 vtop
->c
.ld
= vtop
->c
.f
;
2601 else if (sbt
== VT_DOUBLE
)
2602 vtop
->c
.ld
= vtop
->c
.d
;
2605 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
2606 if ((sbt
& VT_UNSIGNED
) || !(vtop
->c
.i
>> 63))
2607 vtop
->c
.ld
= vtop
->c
.i
;
2609 vtop
->c
.ld
= -(long double)-vtop
->c
.i
;
2611 if ((sbt
& VT_UNSIGNED
) || !(vtop
->c
.i
>> 31))
2612 vtop
->c
.ld
= (uint32_t)vtop
->c
.i
;
2614 vtop
->c
.ld
= -(long double)-(uint32_t)vtop
->c
.i
;
2617 if (dbt
== VT_FLOAT
)
2618 vtop
->c
.f
= (float)vtop
->c
.ld
;
2619 else if (dbt
== VT_DOUBLE
)
2620 vtop
->c
.d
= (double)vtop
->c
.ld
;
2621 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
2622 vtop
->c
.i
= vtop
->c
.ld
;
2623 } else if (sf
&& dbt
== VT_BOOL
) {
2624 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
2627 vtop
->c
.i
= vtop
->c
.ld
;
2628 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
2630 else if (sbt
& VT_UNSIGNED
)
2631 vtop
->c
.i
= (uint32_t)vtop
->c
.i
;
2633 else if (sbt
== VT_PTR
)
2636 else if (sbt
!= VT_LLONG
)
2637 vtop
->c
.i
= ((uint32_t)vtop
->c
.i
|
2638 -(vtop
->c
.i
& 0x80000000));
2640 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
2642 else if (dbt
== VT_BOOL
)
2643 vtop
->c
.i
= (vtop
->c
.i
!= 0);
2645 else if (dbt
== VT_PTR
)
2648 else if (dbt
!= VT_LLONG
) {
2649 uint32_t m
= ((dbt
& VT_BTYPE
) == VT_BYTE ?
0xff :
2650 (dbt
& VT_BTYPE
) == VT_SHORT ?
0xffff :
2653 if (!(dbt
& VT_UNSIGNED
))
2654 vtop
->c
.i
|= -(vtop
->c
.i
& ((m
>> 1) + 1));
2657 } else if (p
&& dbt
== VT_BOOL
) {
2661 /* non constant case: generate code */
2663 /* convert from fp to fp */
2666 /* convert int to fp */
2669 /* convert fp to int */
2670 if (dbt
== VT_BOOL
) {
2674 /* we handle char/short/etc... with generic code */
2675 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
2676 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
2680 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
2681 /* additional cast for char/short... */
2687 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
2688 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
2689 /* scalar to long long */
2690 /* machine independent conversion */
2692 /* generate high word */
2693 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
2697 if (sbt
== VT_PTR
) {
2698 /* cast from pointer to int before we apply
2699 shift operation, which pointers don't support*/
2706 /* patch second register */
2707 vtop
[-1].r2
= vtop
->r
;
2711 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
2712 (dbt
& VT_BTYPE
) == VT_PTR
||
2713 (dbt
& VT_BTYPE
) == VT_FUNC
) {
2714 if ((sbt
& VT_BTYPE
) != VT_LLONG
&&
2715 (sbt
& VT_BTYPE
) != VT_PTR
&&
2716 (sbt
& VT_BTYPE
) != VT_FUNC
) {
2717 /* need to convert from 32bit to 64bit */
2719 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
2720 #if defined(TCC_TARGET_ARM64)
2722 #elif defined(TCC_TARGET_X86_64)
2724 /* x86_64 specific: movslq */
2726 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
2733 } else if (dbt
== VT_BOOL
) {
2734 /* scalar to bool */
2737 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
2738 (dbt
& VT_BTYPE
) == VT_SHORT
) {
2739 if (sbt
== VT_PTR
) {
2740 vtop
->type
.t
= VT_INT
;
2741 tcc_warning("nonportable conversion from pointer to char/short");
2743 force_charshort_cast(dbt
);
2744 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
2746 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
2748 /* from long long: just take low order word */
2753 vtop
->type
.t
|= VT_UNSIGNED
;
2757 /* if lvalue and single word type, nothing to do because
2758 the lvalue already contains the real type size (see
2759 VT_LVAL_xxx constants) */
2762 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
2763 /* if we are casting between pointer types,
2764 we must update the VT_LVAL_xxx size */
2765 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
2766 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
2769 vtop
->type
.t
&= ~ ( VT_CONSTANT
| VT_VOLATILE
| VT_ARRAY
);
2772 /* return type size as known at compile time. Put alignment at 'a' */
2773 ST_FUNC
int type_size(CType
*type
, int *a
)
2778 bt
= type
->t
& VT_BTYPE
;
2779 if (bt
== VT_STRUCT
) {
2784 } else if (bt
== VT_PTR
) {
2785 if (type
->t
& VT_ARRAY
) {
2789 ts
= type_size(&s
->type
, a
);
2791 if (ts
< 0 && s
->c
< 0)
2799 } else if (IS_ENUM(type
->t
) && type
->ref
->c
< 0) {
2800 return -1; /* incomplete enum */
2801 } else if (bt
== VT_LDOUBLE
) {
2803 return LDOUBLE_SIZE
;
2804 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
2805 #ifdef TCC_TARGET_I386
2806 #ifdef TCC_TARGET_PE
2811 #elif defined(TCC_TARGET_ARM)
2821 } else if (bt
== VT_INT
|| bt
== VT_FLOAT
) {
2824 } else if (bt
== VT_SHORT
) {
2827 } else if (bt
== VT_QLONG
|| bt
== VT_QFLOAT
) {
2831 /* char, void, function, _Bool */
2837 /* push type size as known at runtime time on top of value stack. Put
2839 ST_FUNC
void vla_runtime_type_size(CType
*type
, int *a
)
2841 if (type
->t
& VT_VLA
) {
2842 type_size(&type
->ref
->type
, a
);
2843 vset(&int_type
, VT_LOCAL
|VT_LVAL
, type
->ref
->c
);
2845 vpushi(type_size(type
, a
));
2849 static void vla_sp_restore(void) {
2850 if (vlas_in_scope
) {
2851 gen_vla_sp_restore(vla_sp_loc
);
2855 static void vla_sp_restore_root(void) {
2856 if (vlas_in_scope
) {
2857 gen_vla_sp_restore(vla_sp_root_loc
);
2861 /* return the pointed type of t */
2862 static inline CType
*pointed_type(CType
*type
)
2864 return &type
->ref
->type
;
2867 /* modify type so that its it is a pointer to type. */
2868 ST_FUNC
void mk_pointer(CType
*type
)
2871 s
= sym_push(SYM_FIELD
, type
, 0, -1);
2872 type
->t
= VT_PTR
| (type
->t
& VT_STORAGE
);
2876 /* compare function types. OLD functions match any new functions */
2877 static int is_compatible_func(CType
*type1
, CType
*type2
)
2883 if (!is_compatible_types(&s1
->type
, &s2
->type
))
2885 /* check func_call */
2886 if (s1
->f
.func_call
!= s2
->f
.func_call
)
2888 /* XXX: not complete */
2889 if (s1
->f
.func_type
== FUNC_OLD
|| s2
->f
.func_type
== FUNC_OLD
)
2891 if (s1
->f
.func_type
!= s2
->f
.func_type
)
2893 while (s1
!= NULL
) {
2896 if (!is_compatible_unqualified_types(&s1
->type
, &s2
->type
))
2906 /* return true if type1 and type2 are the same. If unqualified is
2907 true, qualifiers on the types are ignored.
2909 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2913 t1
= type1
->t
& VT_TYPE
;
2914 t2
= type2
->t
& VT_TYPE
;
2916 /* strip qualifiers before comparing */
2917 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2918 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2921 /* Default Vs explicit signedness only matters for char */
2922 if ((t1
& VT_BTYPE
) != VT_BYTE
) {
2926 /* XXX: bitfields ? */
2929 /* test more complicated cases */
2930 bt1
= t1
& (VT_BTYPE
| VT_ARRAY
);
2931 if (bt1
== VT_PTR
) {
2932 type1
= pointed_type(type1
);
2933 type2
= pointed_type(type2
);
2934 return is_compatible_types(type1
, type2
);
2935 } else if (bt1
& VT_ARRAY
) {
2936 return type1
->ref
->c
< 0 || type2
->ref
->c
< 0
2937 || type1
->ref
->c
== type2
->ref
->c
;
2938 } else if (bt1
== VT_STRUCT
) {
2939 return (type1
->ref
== type2
->ref
);
2940 } else if (bt1
== VT_FUNC
) {
2941 return is_compatible_func(type1
, type2
);
2942 } else if (IS_ENUM(type1
->t
) || IS_ENUM(type2
->t
)) {
2943 return type1
->ref
== type2
->ref
;
2949 /* return true if type1 and type2 are exactly the same (including
2952 static int is_compatible_types(CType
*type1
, CType
*type2
)
2954 return compare_types(type1
,type2
,0);
2957 /* return true if type1 and type2 are the same (ignoring qualifiers).
2959 static int is_compatible_unqualified_types(CType
*type1
, CType
*type2
)
2961 return compare_types(type1
,type2
,1);
2964 /* print a type. If 'varstr' is not NULL, then the variable is also
2965 printed in the type */
2967 /* XXX: add array and function pointers */
2968 static void type_to_str(char *buf
, int buf_size
,
2969 CType
*type
, const char *varstr
)
2981 pstrcat(buf
, buf_size
, "extern ");
2983 pstrcat(buf
, buf_size
, "static ");
2985 pstrcat(buf
, buf_size
, "typedef ");
2987 pstrcat(buf
, buf_size
, "inline ");
2988 if (t
& VT_VOLATILE
)
2989 pstrcat(buf
, buf_size
, "volatile ");
2990 if (t
& VT_CONSTANT
)
2991 pstrcat(buf
, buf_size
, "const ");
2993 if (((t
& VT_DEFSIGN
) && bt
== VT_BYTE
)
2994 || ((t
& VT_UNSIGNED
)
2995 && (bt
== VT_SHORT
|| bt
== VT_INT
|| bt
== VT_LLONG
)
2998 pstrcat(buf
, buf_size
, (t
& VT_UNSIGNED
) ?
"unsigned " : "signed ");
3000 buf_size
-= strlen(buf
);
3035 tstr
= "long double";
3037 pstrcat(buf
, buf_size
, tstr
);
3044 pstrcat(buf
, buf_size
, tstr
);
3045 v
= type
->ref
->v
& ~SYM_STRUCT
;
3046 if (v
>= SYM_FIRST_ANOM
)
3047 pstrcat(buf
, buf_size
, "<anonymous>");
3049 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
3054 if (varstr
&& '*' == *varstr
) {
3055 pstrcat(buf1
, sizeof(buf1
), "(");
3056 pstrcat(buf1
, sizeof(buf1
), varstr
);
3057 pstrcat(buf1
, sizeof(buf1
), ")");
3059 pstrcat(buf1
, buf_size
, "(");
3061 while (sa
!= NULL
) {
3063 type_to_str(buf2
, sizeof(buf2
), &sa
->type
, NULL
);
3064 pstrcat(buf1
, sizeof(buf1
), buf2
);
3067 pstrcat(buf1
, sizeof(buf1
), ", ");
3069 if (s
->f
.func_type
== FUNC_ELLIPSIS
)
3070 pstrcat(buf1
, sizeof(buf1
), ", ...");
3071 pstrcat(buf1
, sizeof(buf1
), ")");
3072 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
3077 if (varstr
&& '*' == *varstr
)
3078 snprintf(buf1
, sizeof(buf1
), "(%s)[%d]", varstr
, s
->c
);
3080 snprintf(buf1
, sizeof(buf1
), "%s[%d]", varstr ? varstr
: "", s
->c
);
3081 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
3084 pstrcpy(buf1
, sizeof(buf1
), "*");
3085 if (t
& VT_CONSTANT
)
3086 pstrcat(buf1
, buf_size
, "const ");
3087 if (t
& VT_VOLATILE
)
3088 pstrcat(buf1
, buf_size
, "volatile ");
3090 pstrcat(buf1
, sizeof(buf1
), varstr
);
3091 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
3095 pstrcat(buf
, buf_size
, " ");
3096 pstrcat(buf
, buf_size
, varstr
);
3101 /* verify type compatibility to store vtop in 'dt' type, and generate
3103 static void gen_assign_cast(CType
*dt
)
3105 CType
*st
, *type1
, *type2
;
3106 char buf1
[256], buf2
[256];
3107 int dbt
, sbt
, qualwarn
, lvl
;
3109 st
= &vtop
->type
; /* source type */
3110 dbt
= dt
->t
& VT_BTYPE
;
3111 sbt
= st
->t
& VT_BTYPE
;
3112 if (sbt
== VT_VOID
|| dbt
== VT_VOID
) {
3113 if (sbt
== VT_VOID
&& dbt
== VT_VOID
)
3114 ; /* It is Ok if both are void */
3116 tcc_error("cannot cast from/to void");
3118 if (dt
->t
& VT_CONSTANT
)
3119 tcc_warning("assignment of read-only location");
3122 /* special cases for pointers */
3123 /* '0' can also be a pointer */
3124 if (is_null_pointer(vtop
))
3126 /* accept implicit pointer to integer cast with warning */
3127 if (is_integer_btype(sbt
)) {
3128 tcc_warning("assignment makes pointer from integer without a cast");
3131 type1
= pointed_type(dt
);
3133 type2
= pointed_type(st
);
3134 else if (sbt
== VT_FUNC
)
3135 type2
= st
; /* a function is implicitly a function pointer */
3138 if (is_compatible_types(type1
, type2
))
3140 for (qualwarn
= lvl
= 0;; ++lvl
) {
3141 if (((type2
->t
& VT_CONSTANT
) && !(type1
->t
& VT_CONSTANT
)) ||
3142 ((type2
->t
& VT_VOLATILE
) && !(type1
->t
& VT_VOLATILE
)))
3144 dbt
= type1
->t
& (VT_BTYPE
|VT_LONG
);
3145 sbt
= type2
->t
& (VT_BTYPE
|VT_LONG
);
3146 if (dbt
!= VT_PTR
|| sbt
!= VT_PTR
)
3148 type1
= pointed_type(type1
);
3149 type2
= pointed_type(type2
);
3151 if (!is_compatible_unqualified_types(type1
, type2
)) {
3152 if ((dbt
== VT_VOID
|| sbt
== VT_VOID
) && lvl
== 0) {
3153 /* void * can match anything */
3154 } else if (dbt
== sbt
3155 && is_integer_btype(sbt
& VT_BTYPE
)
3156 && IS_ENUM(type1
->t
) + IS_ENUM(type2
->t
)
3157 + !!((type1
->t
^ type2
->t
) & VT_UNSIGNED
) < 2) {
3158 /* Like GCC don't warn by default for merely changes
3159 in pointer target signedness. Do warn for different
3160 base types, though, in particular for unsigned enums
3161 and signed int targets. */
3163 tcc_warning("assignment from incompatible pointer type");
3168 tcc_warning("assignment discards qualifiers from pointer target type");
3174 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
3175 tcc_warning("assignment makes integer from pointer without a cast");
3176 } else if (sbt
== VT_STRUCT
) {
3177 goto case_VT_STRUCT
;
3179 /* XXX: more tests */
3183 if (!is_compatible_unqualified_types(dt
, st
)) {
3185 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
3186 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
3187 tcc_error("cannot cast '%s' to '%s'", buf1
, buf2
);
3194 /* store vtop in lvalue pushed on stack */
3195 ST_FUNC
void vstore(void)
3197 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
3199 ft
= vtop
[-1].type
.t
;
3200 sbt
= vtop
->type
.t
& VT_BTYPE
;
3201 dbt
= ft
& VT_BTYPE
;
3202 if ((((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
3203 (sbt
== VT_INT
&& dbt
== VT_SHORT
))
3204 && !(vtop
->type
.t
& VT_BITFIELD
)) {
3205 /* optimize char/short casts */
3206 delayed_cast
= VT_MUSTCAST
;
3207 vtop
->type
.t
= ft
& VT_TYPE
;
3208 /* XXX: factorize */
3209 if (ft
& VT_CONSTANT
)
3210 tcc_warning("assignment of read-only location");
3213 if (!(ft
& VT_BITFIELD
))
3214 gen_assign_cast(&vtop
[-1].type
);
3217 if (sbt
== VT_STRUCT
) {
3218 /* if structure, only generate pointer */
3219 /* structure assignment : generate memcpy */
3220 /* XXX: optimize if small size */
3221 size
= type_size(&vtop
->type
, &align
);
3225 vtop
->type
.t
= VT_PTR
;
3228 /* address of memcpy() */
3231 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
3232 else if(!(align
& 3))
3233 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
3236 /* Use memmove, rather than memcpy, as dest and src may be same: */
3237 vpush_global_sym(&func_old_type
, TOK_memmove
);
3242 vtop
->type
.t
= VT_PTR
;
3248 /* leave source on stack */
3249 } else if (ft
& VT_BITFIELD
) {
3250 /* bitfield store handling */
3252 /* save lvalue as expression result (example: s.b = s.a = n;) */
3253 vdup(), vtop
[-1] = vtop
[-2];
3255 bit_pos
= BIT_POS(ft
);
3256 bit_size
= BIT_SIZE(ft
);
3257 /* remove bit field info to avoid loops */
3258 vtop
[-1].type
.t
= ft
& ~VT_STRUCT_MASK
;
3260 if ((ft
& VT_BTYPE
) == VT_BOOL
) {
3261 gen_cast(&vtop
[-1].type
);
3262 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
3265 r
= adjust_bf(vtop
- 1, bit_pos
, bit_size
);
3266 if (r
== VT_STRUCT
) {
3267 gen_cast_s((ft
& VT_BTYPE
) == VT_LLONG ? VT_LLONG
: VT_INT
);
3268 store_packed_bf(bit_pos
, bit_size
);
3270 unsigned long long mask
= (1ULL << bit_size
) - 1;
3271 if ((ft
& VT_BTYPE
) != VT_BOOL
) {
3273 if ((vtop
[-1].type
.t
& VT_BTYPE
) == VT_LLONG
)
3276 vpushi((unsigned)mask
);
3283 /* duplicate destination */
3286 /* load destination, mask and or with source */
3287 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
)
3288 vpushll(~(mask
<< bit_pos
));
3290 vpushi(~((unsigned)mask
<< bit_pos
));
3295 /* ... and discard */
3298 } else if (dbt
== VT_VOID
) {
3301 #ifdef CONFIG_TCC_BCHECK
3302 /* bound check case */
3303 if (vtop
[-1].r
& VT_MUSTBOUND
) {
3312 #ifdef TCC_TARGET_X86_64
3313 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
3315 } else if ((ft
& VT_BTYPE
) == VT_QFLOAT
) {
3320 r
= gv(rc
); /* generate value */
3321 /* if lvalue was saved on stack, must read it */
3322 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
3324 t
= get_reg(RC_INT
);
3330 sv
.r
= VT_LOCAL
| VT_LVAL
;
3331 sv
.c
.i
= vtop
[-1].c
.i
;
3333 vtop
[-1].r
= t
| VT_LVAL
;
3335 /* two word case handling : store second register at word + 4 (or +8 for x86-64) */
3337 if (((ft
& VT_BTYPE
) == VT_QLONG
) || ((ft
& VT_BTYPE
) == VT_QFLOAT
)) {
3338 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
3340 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
3341 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
3343 vtop
[-1].type
.t
= load_type
;
3346 /* convert to int to increment easily */
3347 vtop
->type
.t
= addr_type
;
3353 vtop
[-1].type
.t
= load_type
;
3354 /* XXX: it works because r2 is spilled last ! */
3355 store(vtop
->r2
, vtop
- 1);
3361 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
3362 vtop
->r
|= delayed_cast
;
3366 /* post defines POST/PRE add. c is the token ++ or -- */
3367 ST_FUNC
void inc(int post
, int c
)
3370 vdup(); /* save lvalue */
3372 gv_dup(); /* duplicate value */
3377 vpushi(c
- TOK_MID
);
3379 vstore(); /* store value */
3381 vpop(); /* if post op, return saved value */
3384 ST_FUNC
void parse_mult_str (CString
*astr
, const char *msg
)
3386 /* read the string */
3390 while (tok
== TOK_STR
) {
3391 /* XXX: add \0 handling too ? */
3392 cstr_cat(astr
, tokc
.str
.data
, -1);
3395 cstr_ccat(astr
, '\0');
3398 /* If I is >= 1 and a power of two, returns log2(i)+1.
3399 If I is 0 returns 0. */
3400 static int exact_log2p1(int i
)
3405 for (ret
= 1; i
>= 1 << 8; ret
+= 8)
3416 /* Parse __attribute__((...)) GNUC extension. */
3417 static void parse_attribute(AttributeDef
*ad
)
3423 if (tok
!= TOK_ATTRIBUTE1
&& tok
!= TOK_ATTRIBUTE2
)
3428 while (tok
!= ')') {
3429 if (tok
< TOK_IDENT
)
3430 expect("attribute name");
3442 tcc_warning("implicit declaration of function '%s'",
3443 get_tok_str(tok
, &tokc
));
3444 s
= external_global_sym(tok
, &func_old_type
);
3446 ad
->cleanup_func
= s
;
3454 parse_mult_str(&astr
, "section name");
3455 ad
->section
= find_section(tcc_state
, (char *)astr
.data
);
3462 parse_mult_str(&astr
, "alias(\"target\")");
3463 ad
->alias_target
= /* save string as token, for later */
3464 tok_alloc((char*)astr
.data
, astr
.size
-1)->tok
;
3468 case TOK_VISIBILITY1
:
3469 case TOK_VISIBILITY2
:
3471 parse_mult_str(&astr
,
3472 "visibility(\"default|hidden|internal|protected\")");
3473 if (!strcmp (astr
.data
, "default"))
3474 ad
->a
.visibility
= STV_DEFAULT
;
3475 else if (!strcmp (astr
.data
, "hidden"))
3476 ad
->a
.visibility
= STV_HIDDEN
;
3477 else if (!strcmp (astr
.data
, "internal"))
3478 ad
->a
.visibility
= STV_INTERNAL
;
3479 else if (!strcmp (astr
.data
, "protected"))
3480 ad
->a
.visibility
= STV_PROTECTED
;
3482 expect("visibility(\"default|hidden|internal|protected\")");
3491 if (n
<= 0 || (n
& (n
- 1)) != 0)
3492 tcc_error("alignment must be a positive power of two");
3497 ad
->a
.aligned
= exact_log2p1(n
);
3498 if (n
!= 1 << (ad
->a
.aligned
- 1))
3499 tcc_error("alignment of %d is larger than implemented", n
);
3511 /* currently, no need to handle it because tcc does not
3512 track unused objects */
3516 /* currently, no need to handle it because tcc does not
3517 track unused objects */
3522 ad
->f
.func_call
= FUNC_CDECL
;
3527 ad
->f
.func_call
= FUNC_STDCALL
;
3529 #ifdef TCC_TARGET_I386
3539 ad
->f
.func_call
= FUNC_FASTCALL1
+ n
- 1;
3545 ad
->f
.func_call
= FUNC_FASTCALLW
;
3552 ad
->attr_mode
= VT_LLONG
+ 1;
3555 ad
->attr_mode
= VT_BYTE
+ 1;
3558 ad
->attr_mode
= VT_SHORT
+ 1;
3562 ad
->attr_mode
= VT_INT
+ 1;
3565 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
3572 ad
->a
.dllexport
= 1;
3574 case TOK_NODECORATE
:
3575 ad
->a
.nodecorate
= 1;
3578 ad
->a
.dllimport
= 1;
3581 if (tcc_state
->warn_unsupported
)
3582 tcc_warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
3583 /* skip parameters */
3585 int parenthesis
= 0;
3589 else if (tok
== ')')
3592 } while (parenthesis
&& tok
!= -1);
3605 static Sym
* find_field (CType
*type
, int v
, int *cumofs
)
3609 while ((s
= s
->next
) != NULL
) {
3610 if ((s
->v
& SYM_FIELD
) &&
3611 (s
->type
.t
& VT_BTYPE
) == VT_STRUCT
&&
3612 (s
->v
& ~SYM_FIELD
) >= SYM_FIRST_ANOM
) {
3613 Sym
*ret
= find_field (&s
->type
, v
, cumofs
);
3625 static void struct_layout(CType
*type
, AttributeDef
*ad
)
3627 int size
, align
, maxalign
, offset
, c
, bit_pos
, bit_size
;
3628 int packed
, a
, bt
, prevbt
, prev_bit_size
;
3629 int pcc
= !tcc_state
->ms_bitfields
;
3630 int pragma_pack
= *tcc_state
->pack_stack_ptr
;
3637 prevbt
= VT_STRUCT
; /* make it never match */
3642 for (f
= type
->ref
->next
; f
; f
= f
->next
) {
3643 if (f
->type
.t
& VT_BITFIELD
)
3644 bit_size
= BIT_SIZE(f
->type
.t
);
3647 size
= type_size(&f
->type
, &align
);
3648 a
= f
->a
.aligned ?
1 << (f
->a
.aligned
- 1) : 0;
3651 if (pcc
&& bit_size
== 0) {
3652 /* in pcc mode, packing does not affect zero-width bitfields */
3655 /* in pcc mode, attribute packed overrides if set. */
3656 if (pcc
&& (f
->a
.packed
|| ad
->a
.packed
))
3659 /* pragma pack overrides align if lesser and packs bitfields always */
3662 if (pragma_pack
< align
)
3663 align
= pragma_pack
;
3664 /* in pcc mode pragma pack also overrides individual align */
3665 if (pcc
&& pragma_pack
< a
)
3669 /* some individual align was specified */
3673 if (type
->ref
->type
.t
== VT_UNION
) {
3674 if (pcc
&& bit_size
>= 0)
3675 size
= (bit_size
+ 7) >> 3;
3680 } else if (bit_size
< 0) {
3682 c
+= (bit_pos
+ 7) >> 3;
3683 c
= (c
+ align
- 1) & -align
;
3692 /* A bit-field. Layout is more complicated. There are two
3693 options: PCC (GCC) compatible and MS compatible */
3695 /* In PCC layout a bit-field is placed adjacent to the
3696 preceding bit-fields, except if:
3698 - an individual alignment was given
3699 - it would overflow its base type container and
3700 there is no packing */
3701 if (bit_size
== 0) {
3703 c
= (c
+ ((bit_pos
+ 7) >> 3) + align
- 1) & -align
;
3705 } else if (f
->a
.aligned
) {
3707 } else if (!packed
) {
3709 int ofs
= ((c
* 8 + bit_pos
) % a8
+ bit_size
+ a8
- 1) / a8
;
3710 if (ofs
> size
/ align
)
3714 /* in pcc mode, long long bitfields have type int if they fit */
3715 if (size
== 8 && bit_size
<= 32)
3716 f
->type
.t
= (f
->type
.t
& ~VT_BTYPE
) | VT_INT
, size
= 4;
3718 while (bit_pos
>= align
* 8)
3719 c
+= align
, bit_pos
-= align
* 8;
3722 /* In PCC layout named bit-fields influence the alignment
3723 of the containing struct using the base types alignment,
3724 except for packed fields (which here have correct align). */
3725 if (f
->v
& SYM_FIRST_ANOM
3726 // && bit_size // ??? gcc on ARM/rpi does that
3731 bt
= f
->type
.t
& VT_BTYPE
;
3732 if ((bit_pos
+ bit_size
> size
* 8)
3733 || (bit_size
> 0) == (bt
!= prevbt
)
3735 c
= (c
+ align
- 1) & -align
;
3738 /* In MS bitfield mode a bit-field run always uses
3739 at least as many bits as the underlying type.
3740 To start a new run it's also required that this
3741 or the last bit-field had non-zero width. */
3742 if (bit_size
|| prev_bit_size
)
3745 /* In MS layout the records alignment is normally
3746 influenced by the field, except for a zero-width
3747 field at the start of a run (but by further zero-width
3748 fields it is again). */
3749 if (bit_size
== 0 && prevbt
!= bt
)
3752 prev_bit_size
= bit_size
;
3755 f
->type
.t
= (f
->type
.t
& ~(0x3f << VT_STRUCT_SHIFT
))
3756 | (bit_pos
<< VT_STRUCT_SHIFT
);
3757 bit_pos
+= bit_size
;
3759 if (align
> maxalign
)
3763 printf("set field %s offset %-2d size %-2d align %-2d",
3764 get_tok_str(f
->v
& ~SYM_FIELD
, NULL
), offset
, size
, align
);
3765 if (f
->type
.t
& VT_BITFIELD
) {
3766 printf(" pos %-2d bits %-2d",
3779 c
+= (bit_pos
+ 7) >> 3;
3781 /* store size and alignment */
3782 a
= bt
= ad
->a
.aligned ?
1 << (ad
->a
.aligned
- 1) : 1;
3786 if (pragma_pack
&& pragma_pack
< maxalign
&& 0 == pcc
) {
3787 /* can happen if individual align for some member was given. In
3788 this case MSVC ignores maxalign when aligning the size */
3793 c
= (c
+ a
- 1) & -a
;
3797 printf("struct size %-2d align %-2d\n\n", c
, a
), fflush(stdout
);
3800 /* check whether we can access bitfields by their type */
3801 for (f
= type
->ref
->next
; f
; f
= f
->next
) {
3805 if (0 == (f
->type
.t
& VT_BITFIELD
))
3809 bit_size
= BIT_SIZE(f
->type
.t
);
3812 bit_pos
= BIT_POS(f
->type
.t
);
3813 size
= type_size(&f
->type
, &align
);
3814 if (bit_pos
+ bit_size
<= size
* 8 && f
->c
+ size
<= c
)
3817 /* try to access the field using a different type */
3818 c0
= -1, s
= align
= 1;
3820 px
= f
->c
* 8 + bit_pos
;
3821 cx
= (px
>> 3) & -align
;
3822 px
= px
- (cx
<< 3);
3825 s
= (px
+ bit_size
+ 7) >> 3;
3835 s
= type_size(&t
, &align
);
3839 if (px
+ bit_size
<= s
* 8 && cx
+ s
<= c
) {
3840 /* update offset and bit position */
3843 f
->type
.t
= (f
->type
.t
& ~(0x3f << VT_STRUCT_SHIFT
))
3844 | (bit_pos
<< VT_STRUCT_SHIFT
);
3848 printf("FIX field %s offset %-2d size %-2d align %-2d "
3849 "pos %-2d bits %-2d\n",
3850 get_tok_str(f
->v
& ~SYM_FIELD
, NULL
),
3851 cx
, s
, align
, px
, bit_size
);
3854 /* fall back to load/store single-byte wise */
3855 f
->auxtype
= VT_STRUCT
;
3857 printf("FIX field %s : load byte-wise\n",
3858 get_tok_str(f
->v
& ~SYM_FIELD
, NULL
));
3864 /* enum/struct/union declaration. u is VT_ENUM/VT_STRUCT/VT_UNION */
3865 static void struct_decl(CType
*type
, int u
)
3867 int v
, c
, size
, align
, flexible
;
3868 int bit_size
, bsize
, bt
;
3870 AttributeDef ad
, ad1
;
3873 memset(&ad
, 0, sizeof ad
);
3875 parse_attribute(&ad
);
3879 /* struct already defined ? return it */
3881 expect("struct/union/enum name");
3883 if (s
&& (s
->sym_scope
== local_scope
|| tok
!= '{')) {
3886 if (u
== VT_ENUM
&& IS_ENUM(s
->type
.t
))
3888 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
3893 /* Record the original enum/struct/union token. */
3894 type1
.t
= u
== VT_ENUM ? u
| VT_INT
| VT_UNSIGNED
: u
;
3896 /* we put an undefined size for struct/union */
3897 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
3898 s
->r
= 0; /* default alignment is zero as gcc */
3900 type
->t
= s
->type
.t
;
3906 tcc_error("struct/union/enum already defined");
3908 /* cannot be empty */
3909 /* non empty enums are not allowed */
3912 long long ll
= 0, pl
= 0, nl
= 0;
3915 /* enum symbols have static storage */
3916 t
.t
= VT_INT
|VT_STATIC
|VT_ENUM_VAL
;
3920 expect("identifier");
3922 if (ss
&& !local_stack
)
3923 tcc_error("redefinition of enumerator '%s'",
3924 get_tok_str(v
, NULL
));
3928 ll
= expr_const64();
3930 ss
= sym_push(v
, &t
, VT_CONST
, 0);
3932 *ps
= ss
, ps
= &ss
->next
;
3941 /* NOTE: we accept a trailing comma */
3946 /* set integral type of the enum */
3949 if (pl
!= (unsigned)pl
)
3950 t
.t
= (LONG_SIZE
==8 ? VT_LLONG
|VT_LONG
: VT_LLONG
);
3952 } else if (pl
!= (int)pl
|| nl
!= (int)nl
)
3953 t
.t
= (LONG_SIZE
==8 ? VT_LLONG
|VT_LONG
: VT_LLONG
);
3954 s
->type
.t
= type
->t
= t
.t
| VT_ENUM
;
3956 /* set type for enum members */
3957 for (ss
= s
->next
; ss
; ss
= ss
->next
) {
3959 if (ll
== (int)ll
) /* default is int if it fits */
3961 if (t
.t
& VT_UNSIGNED
) {
3962 ss
->type
.t
|= VT_UNSIGNED
;
3963 if (ll
== (unsigned)ll
)
3966 ss
->type
.t
= (ss
->type
.t
& ~VT_BTYPE
)
3967 | (LONG_SIZE
==8 ? VT_LLONG
|VT_LONG
: VT_LLONG
);
3972 while (tok
!= '}') {
3973 if (!parse_btype(&btype
, &ad1
)) {
3979 tcc_error("flexible array member '%s' not at the end of struct",
3980 get_tok_str(v
, NULL
));