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
));
3986 type_decl(&type1
, &ad1
, &v
, TYPE_DIRECT
);
3988 if ((type1
.t
& VT_BTYPE
) != VT_STRUCT
)
3989 expect("identifier");
3991 int v
= btype
.ref
->v
;
3992 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
3993 if (tcc_state
->ms_extensions
== 0)
3994 expect("identifier");
3998 if (type_size(&type1
, &align
) < 0) {
3999 if ((u
== VT_STRUCT
) && (type1
.t
& VT_ARRAY
) && c
)
4002 tcc_error("field '%s' has incomplete type",
4003 get_tok_str(v
, NULL
));
4005 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
4006 (type1
.t
& VT_BTYPE
) == VT_VOID
||
4007 (type1
.t
& VT_STORAGE
))
4008 tcc_error("invalid type for '%s'",
4009 get_tok_str(v
, NULL
));
4013 bit_size
= expr_const();
4014 /* XXX: handle v = 0 case for messages */
4016 tcc_error("negative width in bit-field '%s'",
4017 get_tok_str(v
, NULL
));
4018 if (v
&& bit_size
== 0)
4019 tcc_error("zero width for bit-field '%s'",
4020 get_tok_str(v
, NULL
));
4021 parse_attribute(&ad1
);
4023 size
= type_size(&type1
, &align
);
4024 if (bit_size
>= 0) {
4025 bt
= type1
.t
& VT_BTYPE
;
4031 tcc_error("bitfields must have scalar type");
4033 if (bit_size
> bsize
) {
4034 tcc_error("width of '%s' exceeds its type",
4035 get_tok_str(v
, NULL
));
4036 } else if (bit_size
== bsize
4037 && !ad
.a
.packed
&& !ad1
.a
.packed
) {
4038 /* no need for bit fields */
4040 } else if (bit_size
== 64) {
4041 tcc_error("field width 64 not implemented");
4043 type1
.t
= (type1
.t
& ~VT_STRUCT_MASK
)
4045 | (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
4048 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
4049 /* Remember we've seen a real field to check
4050 for placement of flexible array member. */
4053 /* If member is a struct or bit-field, enforce
4054 placing into the struct (as anonymous). */
4056 ((type1
.t
& VT_BTYPE
) == VT_STRUCT
||
4061 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, 0);
4066 if (tok
== ';' || tok
== TOK_EOF
)
4073 parse_attribute(&ad
);
4074 struct_layout(type
, &ad
);
4079 static void sym_to_attr(AttributeDef
*ad
, Sym
*s
)
4081 merge_symattr(&ad
->a
, &s
->a
);
4082 merge_funcattr(&ad
->f
, &s
->f
);
4085 /* Add type qualifiers to a type. If the type is an array then the qualifiers
4086 are added to the element type, copied because it could be a typedef. */
4087 static void parse_btype_qualify(CType
*type
, int qualifiers
)
4089 while (type
->t
& VT_ARRAY
) {
4090 type
->ref
= sym_push(SYM_FIELD
, &type
->ref
->type
, 0, type
->ref
->c
);
4091 type
= &type
->ref
->type
;
4093 type
->t
|= qualifiers
;
4096 /* return 0 if no type declaration. otherwise, return the basic type
4099 static int parse_btype(CType
*type
, AttributeDef
*ad
)
4101 int t
, u
, bt
, st
, type_found
, typespec_found
, g
;
4105 memset(ad
, 0, sizeof(AttributeDef
));
4115 /* currently, we really ignore extension */
4125 if (u
== VT_SHORT
|| u
== VT_LONG
) {
4126 if (st
!= -1 || (bt
!= -1 && bt
!= VT_INT
))
4127 tmbt
: tcc_error("too many basic types");
4130 if (bt
!= -1 || (st
!= -1 && u
!= VT_INT
))
4135 t
= (t
& ~(VT_BTYPE
|VT_LONG
)) | u
;
4152 memset(&ad1
, 0, sizeof(AttributeDef
));
4153 if (parse_btype(&type1
, &ad1
)) {
4154 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
4156 n
= 1 << (ad1
.a
.aligned
- 1);
4158 type_size(&type1
, &n
);
4161 if (n
<= 0 || (n
& (n
- 1)) != 0)
4162 tcc_error("alignment must be a positive power of two");
4165 ad
->a
.aligned
= exact_log2p1(n
);
4169 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
4170 t
= (t
& ~(VT_BTYPE
|VT_LONG
)) | VT_LDOUBLE
;
4171 } else if ((t
& (VT_BTYPE
|VT_LONG
)) == VT_LONG
) {
4172 t
= (t
& ~(VT_BTYPE
|VT_LONG
)) | VT_LLONG
;
4179 #ifdef TCC_TARGET_ARM64
4181 /* GCC's __uint128_t appears in some Linux header files. Make it a
4182 synonym for long double to get the size and alignment right. */
4193 if ((t
& (VT_BTYPE
|VT_LONG
)) == VT_LONG
) {
4194 t
= (t
& ~(VT_BTYPE
|VT_LONG
)) | VT_LDOUBLE
;
4202 struct_decl(&type1
, VT_ENUM
);
4205 type
->ref
= type1
.ref
;
4208 struct_decl(&type1
, VT_STRUCT
);
4211 struct_decl(&type1
, VT_UNION
);
4214 /* type modifiers */
4219 parse_btype_qualify(type
, VT_CONSTANT
);
4227 parse_btype_qualify(type
, VT_VOLATILE
);
4234 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == (VT_DEFSIGN
|VT_UNSIGNED
))
4235 tcc_error("signed and unsigned modifier");
4248 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == VT_DEFSIGN
)
4249 tcc_error("signed and unsigned modifier");
4250 t
|= VT_DEFSIGN
| VT_UNSIGNED
;
4266 if (t
& (VT_EXTERN
|VT_STATIC
|VT_TYPEDEF
) & ~g
)
4267 tcc_error("multiple storage classes");
4278 /* currently, no need to handle it because tcc does not
4279 track unused objects */
4282 /* GNUC attribute */
4283 case TOK_ATTRIBUTE1
:
4284 case TOK_ATTRIBUTE2
:
4285 parse_attribute(ad
);
4286 if (ad
->attr_mode
) {
4287 u
= ad
->attr_mode
-1;
4288 t
= (t
& ~(VT_BTYPE
|VT_LONG
)) | u
;
4296 parse_expr_type(&type1
);
4297 /* remove all storage modifiers except typedef */
4298 type1
.t
&= ~(VT_STORAGE
&~VT_TYPEDEF
);
4300 sym_to_attr(ad
, type1
.ref
);
4306 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
4308 t
&= ~(VT_BTYPE
|VT_LONG
);
4309 u
= t
& ~(VT_CONSTANT
| VT_VOLATILE
), t
^= u
;
4310 type
->t
= (s
->type
.t
& ~VT_TYPEDEF
) | u
;
4311 type
->ref
= s
->type
.ref
;
4313 parse_btype_qualify(type
, t
);
4315 /* get attributes from typedef */
4325 if (tcc_state
->char_is_unsigned
) {
4326 if ((t
& (VT_DEFSIGN
|VT_BTYPE
)) == VT_BYTE
)
4329 /* VT_LONG is used just as a modifier for VT_INT / VT_LLONG */
4330 bt
= t
& (VT_BTYPE
|VT_LONG
);
4332 t
|= LONG_SIZE
== 8 ? VT_LLONG
: VT_INT
;
4333 #ifdef TCC_TARGET_PE
4334 if (bt
== VT_LDOUBLE
)
4335 t
= (t
& ~(VT_BTYPE
|VT_LONG
)) | VT_DOUBLE
;
4341 /* convert a function parameter type (array to pointer and function to
4342 function pointer) */
4343 static inline void convert_parameter_type(CType
*pt
)
4345 /* remove const and volatile qualifiers (XXX: const could be used
4346 to indicate a const function parameter */
4347 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
4348 /* array must be transformed to pointer according to ANSI C */
4350 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
4355 ST_FUNC
void parse_asm_str(CString
*astr
)
4358 parse_mult_str(astr
, "string constant");
4361 /* Parse an asm label and return the token */
4362 static int asm_label_instr(void)
4368 parse_asm_str(&astr
);
4371 printf("asm_alias: \"%s\"\n", (char *)astr
.data
);
4373 v
= tok_alloc(astr
.data
, astr
.size
- 1)->tok
;
4378 static int post_type(CType
*type
, AttributeDef
*ad
, int storage
, int td
)
4380 int n
, l
, t1
, arg_size
, align
;
4381 Sym
**plast
, *s
, *first
;
4386 /* function type, or recursive declarator (return if so) */
4388 if (td
&& !(td
& TYPE_ABSTRACT
))
4392 else if (parse_btype(&pt
, &ad1
))
4395 merge_attr (ad
, &ad1
);
4404 /* read param name and compute offset */
4405 if (l
!= FUNC_OLD
) {
4406 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
4408 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
4409 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
4410 tcc_error("parameter declared as void");
4414 expect("identifier");
4415 pt
.t
= VT_VOID
; /* invalid type */
4418 convert_parameter_type(&pt
);
4419 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
4420 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
4426 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
4431 if (l
== FUNC_NEW
&& !parse_btype(&pt
, &ad1
))
4432 tcc_error("invalid type");
4435 /* if no parameters, then old type prototype */
4438 /* NOTE: const is ignored in returned type as it has a special
4439 meaning in gcc / C++ */
4440 type
->t
&= ~VT_CONSTANT
;
4441 /* some ancient pre-K&R C allows a function to return an array
4442 and the array brackets to be put after the arguments, such
4443 that "int c()[]" means something like "int[] c()" */
4446 skip(']'); /* only handle simple "[]" */
4449 /* we push a anonymous symbol which will contain the function prototype */
4450 ad
->f
.func_args
= arg_size
;
4451 ad
->f
.func_type
= l
;
4452 s
= sym_push(SYM_FIELD
, type
, 0, 0);
4458 } else if (tok
== '[') {
4459 int saved_nocode_wanted
= nocode_wanted
;
4460 /* array definition */
4463 /* XXX The optional type-quals and static should only be accepted
4464 in parameter decls. The '*' as well, and then even only
4465 in prototypes (not function defs). */
4467 case TOK_RESTRICT1
: case TOK_RESTRICT2
: case TOK_RESTRICT3
:
4482 if (!local_stack
|| (storage
& VT_STATIC
))
4483 vpushi(expr_const());
4485 /* VLAs (which can only happen with local_stack && !VT_STATIC)
4486 length must always be evaluated, even under nocode_wanted,
4487 so that its size slot is initialized (e.g. under sizeof
4492 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4495 tcc_error("invalid array size");
4497 if (!is_integer_btype(vtop
->type
.t
& VT_BTYPE
))
4498 tcc_error("size of variable length array should be an integer");
4504 /* parse next post type */
4505 post_type(type
, ad
, storage
, 0);
4506 if (type
->t
== VT_FUNC
)
4507 tcc_error("declaration of an array of functions");
4508 t1
|= type
->t
& VT_VLA
;
4512 tcc_error("need explicit inner array size in VLAs");
4513 loc
-= type_size(&int_type
, &align
);
4517 vla_runtime_type_size(type
, &align
);
4519 vset(&int_type
, VT_LOCAL
|VT_LVAL
, n
);
4525 nocode_wanted
= saved_nocode_wanted
;
4527 /* we push an anonymous symbol which will contain the array
4529 s
= sym_push(SYM_FIELD
, type
, 0, n
);
4530 type
->t
= (t1
? VT_VLA
: VT_ARRAY
) | VT_PTR
;
4536 /* Parse a type declarator (except basic type), and return the type
4537 in 'type'. 'td' is a bitmask indicating which kind of type decl is
4538 expected. 'type' should contain the basic type. 'ad' is the
4539 attribute definition of the basic type. It can be modified by
4540 type_decl(). If this (possibly abstract) declarator is a pointer chain
4541 it returns the innermost pointed to type (equals *type, but is a different
4542 pointer), otherwise returns type itself, that's used for recursive calls. */
4543 static CType
*type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
4546 int qualifiers
, storage
;
4548 /* recursive type, remove storage bits first, apply them later again */
4549 storage
= type
->t
& VT_STORAGE
;
4550 type
->t
&= ~VT_STORAGE
;
4553 while (tok
== '*') {
4561 qualifiers
|= VT_CONSTANT
;
4566 qualifiers
|= VT_VOLATILE
;
4572 /* XXX: clarify attribute handling */
4573 case TOK_ATTRIBUTE1
:
4574 case TOK_ATTRIBUTE2
:
4575 parse_attribute(ad
);
4579 type
->t
|= qualifiers
;
4581 /* innermost pointed to type is the one for the first derivation */
4582 ret
= pointed_type(type
);
4586 /* This is possibly a parameter type list for abstract declarators
4587 ('int ()'), use post_type for testing this. */
4588 if (!post_type(type
, ad
, 0, td
)) {
4589 /* It's not, so it's a nested declarator, and the post operations
4590 apply to the innermost pointed to type (if any). */
4591 /* XXX: this is not correct to modify 'ad' at this point, but
4592 the syntax is not clear */
4593 parse_attribute(ad
);
4594 post
= type_decl(type
, ad
, v
, td
);
4598 } else if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
4599 /* type identifier */
4604 if (!(td
& TYPE_ABSTRACT
))
4605 expect("identifier");
4608 post_type(post
, ad
, storage
, 0);
4609 parse_attribute(ad
);
4614 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
4615 ST_FUNC
int lvalue_type(int t
)
4620 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
4622 else if (bt
== VT_SHORT
)
4626 if (t
& VT_UNSIGNED
)
4627 r
|= VT_LVAL_UNSIGNED
;
4631 /* indirection with full error checking and bound check */
4632 ST_FUNC
void indir(void)
4634 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
4635 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
4639 if (vtop
->r
& VT_LVAL
)
4641 vtop
->type
= *pointed_type(&vtop
->type
);
4642 /* Arrays and functions are never lvalues */
4643 if (!(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_VLA
)
4644 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
4645 vtop
->r
|= lvalue_type(vtop
->type
.t
);
4646 /* if bound checking, the referenced pointer must be checked */
4647 #ifdef CONFIG_TCC_BCHECK
4648 if (tcc_state
->do_bounds_check
)
4649 vtop
->r
|= VT_MUSTBOUND
;
4654 /* pass a parameter to a function and do type checking and casting */
4655 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
4660 func_type
= func
->f
.func_type
;
4661 if (func_type
== FUNC_OLD
||
4662 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
4663 /* default casting : only need to convert float to double */
4664 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
4665 gen_cast_s(VT_DOUBLE
);
4666 } else if (vtop
->type
.t
& VT_BITFIELD
) {
4667 type
.t
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
4668 type
.ref
= vtop
->type
.ref
;
4671 } else if (arg
== NULL
) {
4672 tcc_error("too many arguments to function");
4675 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
4676 gen_assign_cast(&type
);
4680 /* parse an expression and return its type without any side effect. */
4681 static void expr_type(CType
*type
, void (*expr_fn
)(void))
4690 /* parse an expression of the form '(type)' or '(expr)' and return its
4692 static void parse_expr_type(CType
*type
)
4698 if (parse_btype(type
, &ad
)) {
4699 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
4701 expr_type(type
, gexpr
);
4706 static void parse_type(CType
*type
)
4711 if (!parse_btype(type
, &ad
)) {
4714 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
4717 static void parse_builtin_params(int nc
, const char *args
)
4724 while ((c
= *args
++)) {
4728 case 'e': expr_eq(); continue;
4729 case 't': parse_type(&t
); vpush(&t
); continue;
4730 default: tcc_error("internal error"); break;
4738 static void try_call_scope_cleanup(Sym
*stop
)
4740 Sym
*cls
= current_cleanups
;
4742 for (; cls
!= stop
; cls
= cls
->ncl
) {
4743 Sym
*fs
= cls
->next
;
4744 Sym
*vs
= cls
->prev_tok
;
4746 vpushsym(&fs
->type
, fs
);
4747 vset(&vs
->type
, vs
->r
, vs
->c
);
4749 mk_pointer(&vtop
->type
);
4755 static void try_call_cleanup_goto(Sym
*cleanupstate
)
4760 if (!current_cleanups
)
4763 /* search NCA of both cleanup chains given parents and initial depth */
4764 ocd
= cleanupstate
? cleanupstate
->v
& ~SYM_FIELD
: 0;
4765 for (ccd
= ncleanups
, oc
= cleanupstate
; ocd
> ccd
; --ocd
, oc
= oc
->ncl
)
4767 for (cc
= current_cleanups
; ccd
> ocd
; --ccd
, cc
= cc
->ncl
)
4769 for (; cc
!= oc
; cc
= cc
->ncl
, oc
= oc
->ncl
, --ccd
)
4772 try_call_scope_cleanup(cc
);
4775 ST_FUNC
void unary(void)
4777 int n
, t
, align
, size
, r
, sizeof_caller
;
4782 sizeof_caller
= in_sizeof
;
4785 /* XXX: GCC 2.95.3 does not generate a table although it should be
4793 #ifdef TCC_TARGET_PE
4794 t
= VT_SHORT
|VT_UNSIGNED
;
4802 vsetc(&type
, VT_CONST
, &tokc
);
4806 t
= VT_INT
| VT_UNSIGNED
;
4812 t
= VT_LLONG
| VT_UNSIGNED
;
4824 t
= (LONG_SIZE
== 8 ? VT_LLONG
: VT_INT
) | VT_LONG
;
4827 t
= (LONG_SIZE
== 8 ? VT_LLONG
: VT_INT
) | VT_LONG
| VT_UNSIGNED
;
4829 case TOK___FUNCTION__
:
4831 goto tok_identifier
;
4837 /* special function name identifier */
4838 len
= strlen(funcname
) + 1;
4839 /* generate char[len] type */
4844 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
4845 if (!NODATA_WANTED
) {
4846 ptr
= section_ptr_add(data_section
, len
);
4847 memcpy(ptr
, funcname
, len
);
4853 #ifdef TCC_TARGET_PE
4854 t
= VT_SHORT
| VT_UNSIGNED
;
4860 /* string parsing */
4862 if (tcc_state
->char_is_unsigned
)
4863 t
= VT_BYTE
| VT_UNSIGNED
;
4865 if (tcc_state
->warn_write_strings
)
4870 memset(&ad
, 0, sizeof(AttributeDef
));
4871 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, 0);
4876 if (parse_btype(&type
, &ad
)) {
4877 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
4879 /* check ISOC99 compound literal */
4881 /* data is allocated locally by default */
4886 /* all except arrays are lvalues */
4887 if (!(type
.t
& VT_ARRAY
))
4888 r
|= lvalue_type(type
.t
);
4889 memset(&ad
, 0, sizeof(AttributeDef
));
4890 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, 0);
4892 if (sizeof_caller
) {
4899 } else if (tok
== '{') {
4900 int saved_nocode_wanted
= nocode_wanted
;
4902 tcc_error("expected constant");
4903 /* save all registers */
4905 /* statement expression : we do not accept break/continue
4906 inside as GCC does. We do retain the nocode_wanted state,
4907 as statement expressions can't ever be entered from the
4908 outside, so any reactivation of code emission (from labels
4909 or loop heads) can be disabled again after the end of it. */
4910 block(NULL
, NULL
, NULL
, NULL
, 1);
4911 nocode_wanted
= saved_nocode_wanted
;
4926 /* functions names must be treated as function pointers,
4927 except for unary '&' and sizeof. Since we consider that
4928 functions are not lvalues, we only have to handle it
4929 there and in function calls. */
4930 /* arrays can also be used although they are not lvalues */
4931 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
4932 !(vtop
->type
.t
& VT_ARRAY
))
4934 mk_pointer(&vtop
->type
);
4940 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4941 gen_cast_s(VT_BOOL
);
4942 vtop
->c
.i
= !vtop
->c
.i
;
4943 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
4947 vseti(VT_JMP
, gvtst(1, 0));
4959 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
4960 tcc_error("pointer not accepted for unary plus");
4961 /* In order to force cast, we add zero, except for floating point
4962 where we really need an noop (otherwise -0.0 will be transformed
4964 if (!is_float(vtop
->type
.t
)) {
4976 expr_type(&type
, unary
); /* Perform a in_sizeof = 0; */
4977 s
= vtop
[1].sym
; /* hack: accessing previous vtop */
4978 size
= type_size(&type
, &align
);
4979 if (s
&& s
->a
.aligned
)
4980 align
= 1 << (s
->a
.aligned
- 1);
4981 if (t
== TOK_SIZEOF
) {
4982 if (!(type
.t
& VT_VLA
)) {
4984 tcc_error("sizeof applied to an incomplete type");
4987 vla_runtime_type_size(&type
, &align
);
4992 vtop
->type
.t
|= VT_UNSIGNED
;
4995 case TOK_builtin_expect
:
4996 /* __builtin_expect is a no-op for now */
4997 parse_builtin_params(0, "ee");
5000 case TOK_builtin_types_compatible_p
:
5001 parse_builtin_params(0, "tt");
5002 vtop
[-1].type
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
5003 vtop
[0].type
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
5004 n
= is_compatible_types(&vtop
[-1].type
, &vtop
[0].type
);
5008 case TOK_builtin_choose_expr
:
5035 case TOK_builtin_constant_p
:
5036 parse_builtin_params(1, "e");
5037 n
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
5041 case TOK_builtin_frame_address
:
5042 case TOK_builtin_return_address
:
5048 if (tok
!= TOK_CINT
) {
5049 tcc_error("%s only takes positive integers",
5050 tok1
== TOK_builtin_return_address
?
5051 "__builtin_return_address" :
5052 "__builtin_frame_address");
5054 level
= (uint32_t)tokc
.i
;
5059 vset(&type
, VT_LOCAL
, 0); /* local frame */
5061 mk_pointer(&vtop
->type
);
5062 indir(); /* -> parent frame */
5064 if (tok1
== TOK_builtin_return_address
) {
5065 // assume return address is just above frame pointer on stack
5068 mk_pointer(&vtop
->type
);
5073 #ifdef TCC_TARGET_X86_64
5074 #ifdef TCC_TARGET_PE
5075 case TOK_builtin_va_start
:
5076 parse_builtin_params(0, "ee");
5077 r
= vtop
->r
& VT_VALMASK
;
5081 tcc_error("__builtin_va_start expects a local variable");
5083 vtop
->type
= char_pointer_type
;
5088 case TOK_builtin_va_arg_types
:
5089 parse_builtin_params(0, "t");
5090 vpushi(classify_x86_64_va_arg(&vtop
->type
));
5097 #ifdef TCC_TARGET_ARM64
5098 case TOK___va_start
: {
5099 parse_builtin_params(0, "ee");
5103 vtop
->type
.t
= VT_VOID
;
5106 case TOK___va_arg
: {
5107 parse_builtin_params(0, "et");
5115 case TOK___arm64_clear_cache
: {
5116 parse_builtin_params(0, "ee");
5119 vtop
->type
.t
= VT_VOID
;
5123 /* pre operations */
5134 t
= vtop
->type
.t
& VT_BTYPE
;
5136 /* In IEEE negate(x) isn't subtract(0,x), but rather
5140 vtop
->c
.f
= -1.0 * 0.0;
5141 else if (t
== VT_DOUBLE
)
5142 vtop
->c
.d
= -1.0 * 0.0;
5144 vtop
->c
.ld
= -1.0 * 0.0;
5152 goto tok_identifier
;
5154 /* allow to take the address of a label */
5155 if (tok
< TOK_UIDENT
)
5156 expect("label identifier");
5157 s
= label_find(tok
);
5159 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
5161 if (s
->r
== LABEL_DECLARED
)
5162 s
->r
= LABEL_FORWARD
;
5165 s
->type
.t
= VT_VOID
;
5166 mk_pointer(&s
->type
);
5167 s
->type
.t
|= VT_STATIC
;
5169 vpushsym(&s
->type
, s
);
5175 CType controlling_type
;
5176 int has_default
= 0;
5179 TokenString
*str
= NULL
;
5180 int saved_const_wanted
= const_wanted
;
5185 expr_type(&controlling_type
, expr_eq
);
5186 controlling_type
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
| VT_ARRAY
);
5187 if ((controlling_type
.t
& VT_BTYPE
) == VT_FUNC
)
5188 mk_pointer(&controlling_type
);
5189 const_wanted
= saved_const_wanted
;
5193 if (tok
== TOK_DEFAULT
) {
5195 tcc_error("too many 'default'");
5201 AttributeDef ad_tmp
;
5204 parse_btype(&cur_type
, &ad_tmp
);
5205 type_decl(&cur_type
, &ad_tmp
, &itmp
, TYPE_ABSTRACT
);
5206 if (compare_types(&controlling_type
, &cur_type
, 0)) {
5208 tcc_error("type match twice");
5218 skip_or_save_block(&str
);
5220 skip_or_save_block(NULL
);
5227 type_to_str(buf
, sizeof buf
, &controlling_type
, NULL
);
5228 tcc_error("type '%s' does not match any association", buf
);
5230 begin_macro(str
, 1);
5239 // special qnan , snan and infinity values
5244 vtop
->type
.t
= VT_FLOAT
;
5249 goto special_math_val
;
5252 goto special_math_val
;
5259 expect("identifier");
5261 if (!s
|| IS_ASM_SYM(s
)) {
5262 const char *name
= get_tok_str(t
, NULL
);
5264 tcc_error("'%s' undeclared", name
);
5265 /* for simple function calls, we tolerate undeclared
5266 external reference to int() function */
5267 if (tcc_state
->warn_implicit_function_declaration
5268 #ifdef TCC_TARGET_PE
5269 /* people must be warned about using undeclared WINAPI functions
5270 (which usually start with uppercase letter) */
5271 || (name
[0] >= 'A' && name
[0] <= 'Z')
5274 tcc_warning("implicit declaration of function '%s'", name
);
5275 s
= external_global_sym(t
, &func_old_type
);
5279 /* A symbol that has a register is a local register variable,
5280 which starts out as VT_LOCAL value. */
5281 if ((r
& VT_VALMASK
) < VT_CONST
)
5282 r
= (r
& ~VT_VALMASK
) | VT_LOCAL
;
5284 vset(&s
->type
, r
, s
->c
);
5285 /* Point to s as backpointer (even without r&VT_SYM).
5286 Will be used by at least the x86 inline asm parser for
5292 } else if (r
== VT_CONST
&& IS_ENUM_VAL(s
->type
.t
)) {
5293 vtop
->c
.i
= s
->enum_val
;
5298 /* post operations */
5300 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
5303 } else if (tok
== '.' || tok
== TOK_ARROW
|| tok
== TOK_CDOUBLE
) {
5304 int qualifiers
, cumofs
= 0;
5306 if (tok
== TOK_ARROW
)
5308 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
5311 /* expect pointer on structure */
5312 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
5313 expect("struct or union");
5314 if (tok
== TOK_CDOUBLE
)
5315 expect("field name");
5317 if (tok
== TOK_CINT
|| tok
== TOK_CUINT
)
5318 expect("field name");
5319 s
= find_field(&vtop
->type
, tok
, &cumofs
);
5321 tcc_error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, &tokc
));
5322 /* add field offset to pointer */
5323 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
5324 vpushi(cumofs
+ s
->c
);
5326 /* change type to field type, and set to lvalue */
5327 vtop
->type
= s
->type
;
5328 vtop
->type
.t
|= qualifiers
;
5329 /* an array is never an lvalue */
5330 if (!(vtop
->type
.t
& VT_ARRAY
)) {
5331 vtop
->r
|= lvalue_type(vtop
->type
.t
);
5332 #ifdef CONFIG_TCC_BCHECK
5333 /* if bound checking, the referenced pointer must be checked */
5334 if (tcc_state
->do_bounds_check
&& (vtop
->r
& VT_VALMASK
) != VT_LOCAL
)
5335 vtop
->r
|= VT_MUSTBOUND
;
5339 } else if (tok
== '[') {
5345 } else if (tok
== '(') {
5348 int nb_args
, ret_nregs
, ret_align
, regsize
, variadic
;
5351 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
5352 /* pointer test (no array accepted) */
5353 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
5354 vtop
->type
= *pointed_type(&vtop
->type
);
5355 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
5359 expect("function pointer");
5362 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
5364 /* get return type */
5367 sa
= s
->next
; /* first parameter */
5368 nb_args
= regsize
= 0;
5370 /* compute first implicit argument if a structure is returned */
5371 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
5372 variadic
= (s
->f
.func_type
== FUNC_ELLIPSIS
);
5373 ret_nregs
= gfunc_sret(&s
->type
, variadic
, &ret
.type
,
5374 &ret_align
, ®size
);
5376 /* get some space for the returned structure */
5377 size
= type_size(&s
->type
, &align
);
5378 #ifdef TCC_TARGET_ARM64
5379 /* On arm64, a small struct is return in registers.
5380 It is much easier to write it to memory if we know
5381 that we are allowed to write some extra bytes, so
5382 round the allocated space up to a power of 2: */
5384 while (size
& (size
- 1))
5385 size
= (size
| (size
- 1)) + 1;
5387 loc
= (loc
- size
) & -align
;
5389 ret
.r
= VT_LOCAL
| VT_LVAL
;
5390 /* pass it as 'int' to avoid structure arg passing
5392 vseti(VT_LOCAL
, loc
);
5402 /* return in register */
5403 if (is_float(ret
.type
.t
)) {
5404 ret
.r
= reg_fret(ret
.type
.t
);
5405 #ifdef TCC_TARGET_X86_64
5406 if ((ret
.type
.t
& VT_BTYPE
) == VT_QFLOAT
)
5410 #ifndef TCC_TARGET_ARM64
5411 #ifdef TCC_TARGET_X86_64
5412 if ((ret
.type
.t
& VT_BTYPE
) == VT_QLONG
)
5414 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
5425 gfunc_param_typed(s
, sa
);
5435 tcc_error("too few arguments to function");
5437 gfunc_call(nb_args
);
5440 for (r
= ret
.r
+ ret_nregs
+ !ret_nregs
; r
-- > ret
.r
;) {
5441 vsetc(&ret
.type
, r
, &ret
.c
);
5442 vtop
->r2
= ret
.r2
; /* Loop only happens when r2 is VT_CONST */
5445 /* handle packed struct return */
5446 if (((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) && ret_nregs
) {
5449 size
= type_size(&s
->type
, &align
);
5450 /* We're writing whole regs often, make sure there's enough
5451 space. Assume register size is power of 2. */
5452 if (regsize
> align
)
5454 loc
= (loc
- size
) & -align
;
5458 vset(&ret
.type
, VT_LOCAL
| VT_LVAL
, addr
+ offset
);
5462 if (--ret_nregs
== 0)
5466 vset(&s
->type
, VT_LOCAL
| VT_LVAL
, addr
);
5474 ST_FUNC
void expr_prod(void)
5479 while (tok
== '*' || tok
== '/' || tok
== '%') {
5487 ST_FUNC
void expr_sum(void)
5492 while (tok
== '+' || tok
== '-') {
5500 static void expr_shift(void)
5505 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
5513 static void expr_cmp(void)
5518 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
5519 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
5527 static void expr_cmpeq(void)
5532 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
5540 static void expr_and(void)
5543 while (tok
== '&') {
5550 static void expr_xor(void)
5553 while (tok
== '^') {
5560 static void expr_or(void)
5563 while (tok
== '|') {
5570 static void expr_land(void)
5573 if (tok
== TOK_LAND
) {
5576 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
5577 gen_cast_s(VT_BOOL
);
5582 while (tok
== TOK_LAND
) {
5598 if (tok
!= TOK_LAND
) {
5611 static void expr_lor(void)
5614 if (tok
== TOK_LOR
) {
5617 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
5618 gen_cast_s(VT_BOOL
);
5623 while (tok
== TOK_LOR
) {
5639 if (tok
!= TOK_LOR
) {
5652 /* Assuming vtop is a value used in a conditional context
5653 (i.e. compared with zero) return 0 if it's false, 1 if
5654 true and -1 if it can't be statically determined. */
5655 static int condition_3way(void)
5658 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
&&
5659 (!(vtop
->r
& VT_SYM
) || !vtop
->sym
->a
.weak
)) {
5661 gen_cast_s(VT_BOOL
);
5668 static void expr_cond(void)
5670 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
, islv
, c
, g
;
5672 CType type
, type1
, type2
;
5677 c
= condition_3way();
5678 g
= (tok
== ':' && gnu_ext
);
5680 /* needed to avoid having different registers saved in
5682 if (is_float(vtop
->type
.t
)) {
5684 #ifdef TCC_TARGET_X86_64
5685 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
5709 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
5710 mk_pointer(&vtop
->type
);
5712 sv
= *vtop
; /* save value to handle it later */
5713 vtop
--; /* no vpop so that FP stack is not flushed */
5729 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
5730 mk_pointer(&vtop
->type
);
5733 bt1
= t1
& VT_BTYPE
;
5735 bt2
= t2
& VT_BTYPE
;
5739 /* cast operands to correct type according to ISOC rules */
5740 if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
5741 type
.t
= VT_VOID
; /* NOTE: as an extension, we accept void on only one side */
5742 } else if (is_float(bt1
) || is_float(bt2
)) {
5743 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
5744 type
.t
= VT_LDOUBLE
;
5746 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
5751 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
5752 /* cast to biggest op */
5753 type
.t
= VT_LLONG
| VT_LONG
;
5754 if (bt1
== VT_LLONG
)
5756 if (bt2
== VT_LLONG
)
5758 /* convert to unsigned if it does not fit in a long long */
5759 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_LLONG
| VT_UNSIGNED
) ||
5760 (t2
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_LLONG
| VT_UNSIGNED
))
5761 type
.t
|= VT_UNSIGNED
;
5762 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
5763 /* http://port70.net/~nsz/c/c99/n1256.html#6.5.15p6 */
5764 /* If one is a null ptr constant the result type
5766 if (is_null_pointer (vtop
)) type
= type1
;
5767 else if (is_null_pointer (&sv
)) type
= type2
;
5768 else if (bt1
!= bt2
)
5769 tcc_error("incompatible types in conditional expressions");
5771 CType
*pt1
= pointed_type(&type1
);
5772 CType
*pt2
= pointed_type(&type2
);
5773 int pbt1
= pt1
->t
& VT_BTYPE
;
5774 int pbt2
= pt2
->t
& VT_BTYPE
;
5775 int newquals
, copied
= 0;
5776 /* pointers to void get preferred, otherwise the
5777 pointed to types minus qualifs should be compatible */
5778 type
= (pbt1
== VT_VOID
) ? type1
: type2
;
5779 if (pbt1
!= VT_VOID
&& pbt2
!= VT_VOID
) {
5780 if(!compare_types(pt1
, pt2
, 1/*unqualif*/))
5781 tcc_warning("pointer type mismatch in conditional expression\n");
5783 /* combine qualifs */
5784 newquals
= ((pt1
->t
| pt2
->t
) & (VT_CONSTANT
| VT_VOLATILE
));
5785 if ((~pointed_type(&type
)->t
& (VT_CONSTANT
| VT_VOLATILE
))
5788 /* copy the pointer target symbol */
5789 type
.ref
= sym_push(SYM_FIELD
, &type
.ref
->type
,
5792 pointed_type(&type
)->t
|= newquals
;
5794 /* pointers to incomplete arrays get converted to
5795 pointers to completed ones if possible */
5796 if (pt1
->t
& VT_ARRAY
5797 && pt2
->t
& VT_ARRAY
5798 && pointed_type(&type
)->ref
->c
< 0
5799 && (pt1
->ref
->c
> 0 || pt2
->ref
->c
> 0))
5802 type
.ref
= sym_push(SYM_FIELD
, &type
.ref
->type
,
5804 pointed_type(&type
)->ref
=
5805 sym_push(SYM_FIELD
, &pointed_type(&type
)->ref
->type
,
5806 0, pointed_type(&type
)->ref
->c
);
5807 pointed_type(&type
)->ref
->c
=
5808 0 < pt1
->ref
->c
? pt1
->ref
->c
: pt2
->ref
->c
;
5811 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
5812 /* XXX: test structure compatibility */
5813 type
= bt1
== VT_STRUCT
? type1
: type2
;
5815 /* integer operations */
5816 type
.t
= VT_INT
| (VT_LONG
& (t1
| t2
));
5817 /* convert to unsigned if it does not fit in an integer */
5818 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_INT
| VT_UNSIGNED
) ||
5819 (t2
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_INT
| VT_UNSIGNED
))
5820 type
.t
|= VT_UNSIGNED
;
5822 /* keep structs lvalue by transforming `(expr ? a : b)` to `*(expr ? &a : &b)` so
5823 that `(expr ? a : b).mem` does not error with "lvalue expected" */
5824 islv
= (vtop
->r
& VT_LVAL
) && (sv
.r
& VT_LVAL
) && VT_STRUCT
== (type
.t
& VT_BTYPE
);
5826 /* now we convert second operand */
5830 mk_pointer(&vtop
->type
);
5832 } else if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
5837 if (is_float(type
.t
)) {
5839 #ifdef TCC_TARGET_X86_64
5840 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
5844 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
5845 /* for long longs, we use fixed registers to avoid having
5846 to handle a complicated move */
5857 /* this is horrible, but we must also convert first
5863 mk_pointer(&vtop
->type
);
5865 } else if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
5869 if (c
< 0 || islv
) {
5871 move_reg(r2
, r1
, type
.t
);
5881 static void expr_eq(void)
5887 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
5888 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
5889 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
5904 ST_FUNC
void gexpr(void)
5915 /* parse a constant expression and return value in vtop. */
5916 static void expr_const1(void)
5925 /* parse an integer constant and return its value. */
5926 static inline int64_t expr_const64(void)
5930 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
5931 expect("constant expression");
5937 /* parse an integer constant and return its value.
5938 Complain if it doesn't fit 32bit (signed or unsigned). */
5939 ST_FUNC
int expr_const(void)
5942 int64_t wc
= expr_const64();
5944 if (c
!= wc
&& (unsigned)c
!= wc
)
5945 tcc_error("constant exceeds 32 bit");
5949 /* return the label token if current token is a label, otherwise
5951 static int is_label(void)
5955 /* fast test first */
5956 if (tok
< TOK_UIDENT
)
5958 /* no need to save tokc because tok is an identifier */
5964 unget_tok(last_tok
);
5969 #ifndef TCC_TARGET_ARM64
5970 static void gfunc_return(CType
*func_type
)
5972 if ((func_type
->t
& VT_BTYPE
) == VT_STRUCT
) {
5973 CType type
, ret_type
;
5974 int ret_align
, ret_nregs
, regsize
;
5975 ret_nregs
= gfunc_sret(func_type
, func_var
, &ret_type
,
5976 &ret_align
, ®size
);
5977 if (0 == ret_nregs
) {
5978 /* if returning structure, must copy it to implicit
5979 first pointer arg location */
5982 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
5985 /* copy structure value to pointer */
5988 /* returning structure packed into registers */
5989 int r
, size
, addr
, align
;
5990 size
= type_size(func_type
,&align
);
5991 if ((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) ||
5992 (vtop
->c
.i
& (ret_align
-1)))
5993 && (align
& (ret_align
-1))) {
5994 loc
= (loc
- size
) & -ret_align
;
5997 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
6001 vset(&ret_type
, VT_LOCAL
| VT_LVAL
, addr
);
6003 vtop
->type
= ret_type
;
6004 if (is_float(ret_type
.t
))
6005 r
= rc_fret(ret_type
.t
);
6016 if (--ret_nregs
== 0)
6018 /* We assume that when a structure is returned in multiple
6019 registers, their classes are consecutive values of the
6022 vtop
->c
.i
+= regsize
;
6026 } else if (is_float(func_type
->t
)) {
6027 gv(rc_fret(func_type
->t
));
6031 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
6035 static int case_cmp(const void *pa
, const void *pb
)
6037 int64_t a
= (*(struct case_t
**) pa
)->v1
;
6038 int64_t b
= (*(struct case_t
**) pb
)->v1
;
6039 return a
< b
? -1 : a
> b
;
6042 static void gcase(struct case_t
**base
, int len
, int *bsym
)
6046 int ll
= (vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
;
6064 gtst_addr(0, p
->sym
); /* v1 <= x <= v2 */
6066 gcase(base
, len
/2, bsym
);
6067 if (cur_switch
->def_sym
)
6068 gjmp_addr(cur_switch
->def_sym
);
6070 *bsym
= gjmp(*bsym
);
6074 base
+= e
; len
-= e
;
6084 if (p
->v1
== p
->v2
) {
6086 gtst_addr(0, p
->sym
);
6096 gtst_addr(0, p
->sym
);
6102 static void block(int *bsym
, Sym
*bcl
, int *csym
, Sym
*ccl
, int is_expr
)
6104 int a
, b
, c
, d
, cond
;
6107 /* generate line number info */
6108 if (tcc_state
->do_debug
)
6109 tcc_debug_line(tcc_state
);
6112 /* default return value is (void) */
6114 vtop
->type
.t
= VT_VOID
;
6117 if (tok
== TOK_IF
) {
6119 int saved_nocode_wanted
= nocode_wanted
;
6124 cond
= condition_3way();
6130 nocode_wanted
|= 0x20000000;
6131 block(bsym
, bcl
, csym
, ccl
, 0);
6133 nocode_wanted
= saved_nocode_wanted
;
6134 if (tok
== TOK_ELSE
) {
6139 nocode_wanted
|= 0x20000000;
6140 block(bsym
, bcl
, csym
, ccl
, 0);
6141 gsym(d
); /* patch else jmp */
6143 nocode_wanted
= saved_nocode_wanted
;
6146 } else if (tok
== TOK_WHILE
) {
6147 int saved_nocode_wanted
;
6148 nocode_wanted
&= ~0x20000000;
6158 saved_nocode_wanted
= nocode_wanted
;
6159 block(&a
, current_cleanups
, &b
, current_cleanups
, 0);
6160 nocode_wanted
= saved_nocode_wanted
;
6165 } else if (tok
== '{') {
6166 Sym
*llabel
, *lcleanup
;
6167 int block_vla_sp_loc
= vla_sp_loc
, saved_vlas_in_scope
= vlas_in_scope
;
6168 int lncleanups
= ncleanups
;
6171 /* record local declaration stack position */
6173 llabel
= local_label_stack
;
6174 lcleanup
= current_cleanups
;
6177 /* handle local labels declarations */
6178 while (tok
== TOK_LABEL
) {
6181 if (tok
< TOK_UIDENT
)
6182 expect("label identifier");
6183 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
6193 while (tok
!= '}') {
6194 if ((a
= is_label()))
6201 block(bsym
, bcl
, csym
, ccl
, is_expr
);
6205 if (current_cleanups
!= lcleanup
) {
6209 for (pg
= &pending_gotos
; (g
= *pg
) && g
->c
> lncleanups
;)
6210 if (g
->prev_tok
->r
& LABEL_FORWARD
) {
6215 try_call_scope_cleanup(lcleanup
);
6216 pcl
->jnext
= gjmp(0);
6218 goto remove_pending
;
6227 if (!nocode_wanted
) {
6228 try_call_scope_cleanup(lcleanup
);
6232 current_cleanups
= lcleanup
;
6233 ncleanups
= lncleanups
;
6234 /* pop locally defined labels */
6235 label_pop(&local_label_stack
, llabel
, is_expr
);
6236 /* pop locally defined symbols */
6238 /* In the is_expr case (a statement expression is finished here),
6239 vtop might refer to symbols on the local_stack. Either via the
6240 type or via vtop->sym. We can't pop those nor any that in turn
6241 might be referred to. To make it easier we don't roll back
6242 any symbols in that case; some upper level call to block() will
6243 do that. We do have to remove such symbols from the lookup
6244 tables, though. sym_pop will do that. */
6245 sym_pop(&local_stack
, s
, is_expr
);
6247 /* Pop VLA frames and restore stack pointer if required */
6248 if (vlas_in_scope
> saved_vlas_in_scope
) {
6249 vla_sp_loc
= saved_vlas_in_scope
? block_vla_sp_loc
: vla_sp_root_loc
;
6252 vlas_in_scope
= saved_vlas_in_scope
;
6255 } else if (tok
== TOK_RETURN
) {
6259 gen_assign_cast(&func_vt
);
6260 try_call_scope_cleanup(NULL
);
6261 if ((func_vt
.t
& VT_BTYPE
) == VT_VOID
)
6264 gfunc_return(&func_vt
);
6266 try_call_scope_cleanup(NULL
);
6269 /* jump unless last stmt in top-level block */
6270 if (tok
!= '}' || local_scope
!= 1)
6272 nocode_wanted
|= 0x20000000;
6273 } else if (tok
== TOK_BREAK
) {
6276 tcc_error("cannot break");
6277 try_call_scope_cleanup(bcl
);
6278 *bsym
= gjmp(*bsym
);
6281 nocode_wanted
|= 0x20000000;
6282 } else if (tok
== TOK_CONTINUE
) {
6285 tcc_error("cannot continue");
6286 try_call_scope_cleanup(ccl
);
6287 vla_sp_restore_root();
6288 *csym
= gjmp(*csym
);
6291 nocode_wanted
|= 0x20000000;
6292 } else if (tok
== TOK_FOR
) {
6294 int saved_nocode_wanted
;
6295 Sym
*lcleanup
= current_cleanups
;
6296 int lncleanups
= ncleanups
;
6298 nocode_wanted
&= ~0x20000000;
6304 /* c99 for-loop init decl? */
6305 if (!decl0(VT_LOCAL
, 1, NULL
)) {
6306 /* no, regular for-loop init expr */
6332 saved_nocode_wanted
= nocode_wanted
;
6333 block(&a
, current_cleanups
, &b
, current_cleanups
, 0);
6334 nocode_wanted
= saved_nocode_wanted
;
6339 try_call_scope_cleanup(lcleanup
);
6340 ncleanups
= lncleanups
;
6341 current_cleanups
= lcleanup
;
6342 sym_pop(&local_stack
, s
, 0);
6345 if (tok
== TOK_DO
) {
6346 int saved_nocode_wanted
;
6347 nocode_wanted
&= ~0x20000000;
6353 saved_nocode_wanted
= nocode_wanted
;
6354 block(&a
, current_cleanups
, &b
, current_cleanups
, 0);
6359 nocode_wanted
= saved_nocode_wanted
;
6363 nocode_wanted
= saved_nocode_wanted
;
6368 if (tok
== TOK_SWITCH
) {
6369 struct switch_t
*saved
, sw
;
6370 int saved_nocode_wanted
= nocode_wanted
;
6376 switchval
= *vtop
--;
6378 b
= gjmp(0); /* jump to first case */
6379 sw
.p
= NULL
; sw
.n
= 0; sw
.def_sym
= 0;
6382 block(&a
, current_cleanups
, csym
, ccl
, 0);
6383 nocode_wanted
= saved_nocode_wanted
;
6384 a
= gjmp(a
); /* add implicit break */
6387 qsort(sw
.p
, sw
.n
, sizeof(void*), case_cmp
);
6388 for (b
= 1; b
< sw
.n
; b
++)
6389 if (sw
.p
[b
- 1]->v2
>= sw
.p
[b
]->v1
)
6390 tcc_error("duplicate case value");
6391 /* Our switch table sorting is signed, so the compared
6392 value needs to be as well when it's 64bit. */
6393 if ((switchval
.type
.t
& VT_BTYPE
) == VT_LLONG
)
6394 switchval
.type
.t
&= ~VT_UNSIGNED
;
6396 gcase(sw
.p
, sw
.n
, &a
);
6399 gjmp_addr(sw
.def_sym
);
6400 dynarray_reset(&sw
.p
, &sw
.n
);
6405 if (tok
== TOK_CASE
) {
6406 struct case_t
*cr
= tcc_malloc(sizeof(struct case_t
));
6409 nocode_wanted
&= ~0x20000000;
6411 cr
->v1
= cr
->v2
= expr_const64();
6412 if (gnu_ext
&& tok
== TOK_DOTS
) {
6414 cr
->v2
= expr_const64();
6415 if (cr
->v2
< cr
->v1
)
6416 tcc_warning("empty case range");
6419 dynarray_add(&cur_switch
->p
, &cur_switch
->n
, cr
);
6422 goto block_after_label
;
6424 if (tok
== TOK_DEFAULT
) {
6429 if (cur_switch
->def_sym
)
6430 tcc_error("too many 'default'");
6431 cur_switch
->def_sym
= ind
;
6433 goto block_after_label
;
6435 if (tok
== TOK_GOTO
) {
6437 if (tok
== '*' && gnu_ext
) {
6441 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
6444 } else if (tok
>= TOK_UIDENT
) {
6445 s
= label_find(tok
);
6446 /* put forward definition if needed */
6448 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
6449 else if (s
->r
== LABEL_DECLARED
)
6450 s
->r
= LABEL_FORWARD
;
6452 vla_sp_restore_root();
6453 if (s
->r
& LABEL_FORWARD
) {
6454 /* start new goto chain for cleanups, linked via label->next */
6455 if (current_cleanups
) {
6456 sym_push2(&pending_gotos
, SYM_FIELD
, 0, ncleanups
);
6457 pending_gotos
->prev_tok
= s
;
6458 s
= sym_push2(&s
->next
, SYM_FIELD
, 0, 0);
6459 pending_gotos
->next
= s
;
6461 s
->jnext
= gjmp(s
->jnext
);
6463 try_call_cleanup_goto(s
->cleanupstate
);
6464 gjmp_addr(s
->jnext
);
6468 expect("label identifier");
6471 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
6480 if (s
->r
== LABEL_DEFINED
)
6481 tcc_error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
6482 s
->r
= LABEL_DEFINED
;
6484 Sym
*pcl
; /* pending cleanup goto */
6485 for (pcl
= s
->next
; pcl
; pcl
= pcl
->prev
)
6487 sym_pop(&s
->next
, NULL
, 0);
6491 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
6494 s
->cleanupstate
= current_cleanups
;
6496 /* we accept this, but it is a mistake */
6498 nocode_wanted
&= ~0x20000000;
6500 tcc_warning("deprecated use of label at end of compound statement");
6504 block(bsym
, bcl
, csym
, ccl
, is_expr
);
6507 /* expression case */
6522 /* This skips over a stream of tokens containing balanced {} and ()
6523 pairs, stopping at outer ',' ';' and '}' (or matching '}' if we started
6524 with a '{'). If STR then allocates and stores the skipped tokens
6525 in *STR. This doesn't check if () and {} are nested correctly,
6526 i.e. "({)}" is accepted. */
6527 static void skip_or_save_block(TokenString
**str
)
6529 int braces
= tok
== '{';
6532 *str
= tok_str_alloc();
6534 while ((level
> 0 || (tok
!= '}' && tok
!= ',' && tok
!= ';' && tok
!= ')'))) {
6536 if (tok
== TOK_EOF
) {
6537 if (str
|| level
> 0)
6538 tcc_error("unexpected end of file");
6543 tok_str_add_tok(*str
);
6546 if (t
== '{' || t
== '(') {
6548 } else if (t
== '}' || t
== ')') {
6550 if (level
== 0 && braces
&& t
== '}')
6555 tok_str_add(*str
, -1);
6556 tok_str_add(*str
, 0);
6560 #define EXPR_CONST 1
6563 static void parse_init_elem(int expr_type
)
6565 int saved_global_expr
;
6568 /* compound literals must be allocated globally in this case */
6569 saved_global_expr
= global_expr
;
6572 global_expr
= saved_global_expr
;
6573 /* NOTE: symbols are accepted, as well as lvalue for anon symbols
6574 (compound literals). */
6575 if (((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
6576 && ((vtop
->r
& (VT_SYM
|VT_LVAL
)) != (VT_SYM
|VT_LVAL
)
6577 || vtop
->sym
->v
< SYM_FIRST_ANOM
))
6578 #ifdef TCC_TARGET_PE
6579 || ((vtop
->r
& VT_SYM
) && vtop
->sym
->a
.dllimport
)
6582 tcc_error("initializer element is not constant");
6590 /* put zeros for variable based init */
6591 static void init_putz(Section
*sec
, unsigned long c
, int size
)
6594 /* nothing to do because globals are already set to zero */
6596 vpush_global_sym(&func_old_type
, TOK_memset
);
6598 #ifdef TCC_TARGET_ARM
6610 #define DIF_SIZE_ONLY 2
6611 #define DIF_HAVE_ELEM 4
6613 /* t is the array or struct type. c is the array or struct
6614 address. cur_field is the pointer to the current
6615 field, for arrays the 'c' member contains the current start
6616 index. 'flags' is as in decl_initializer.
6617 'al' contains the already initialized length of the
6618 current container (starting at c). This returns the new length of that. */
6619 static int decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
6620 Sym
**cur_field
, int flags
, int al
)
6623 int index
, index_last
, align
, l
, nb_elems
, elem_size
;
6624 unsigned long corig
= c
;
6628 if (flags
& DIF_HAVE_ELEM
)
6630 if (gnu_ext
&& (l
= is_label()) != 0)
6632 /* NOTE: we only support ranges for last designator */
6633 while (nb_elems
== 1 && (tok
== '[' || tok
== '.')) {
6635 if (!(type
->t
& VT_ARRAY
))
6636 expect("array type");
6638 index
= index_last
= expr_const();
6639 if (tok
== TOK_DOTS
&& gnu_ext
) {
6641 index_last
= expr_const();
6645 if (index
< 0 || (s
->c
>= 0 && index_last
>= s
->c
) ||
6647 tcc_error("invalid index");
6649 (*cur_field
)->c
= index_last
;
6650 type
= pointed_type(type
);
6651 elem_size
= type_size(type
, &align
);
6652 c
+= index
* elem_size
;
6653 nb_elems
= index_last
- index
+ 1;
6660 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
6661 expect("struct/union type");
6662 f
= find_field(type
, l
, &cumofs
);
6675 } else if (!gnu_ext
) {
6680 if (type
->t
& VT_ARRAY
) {
6681 index
= (*cur_field
)->c
;
6682 if (type
->ref
->c
>= 0 && index
>= type
->ref
->c
)
6683 tcc_error("index too large");
6684 type
= pointed_type(type
);
6685 c
+= index
* type_size(type
, &align
);
6688 while (f
&& (f
->v
& SYM_FIRST_ANOM
) && (f
->type
.t
& VT_BITFIELD
))
6689 *cur_field
= f
= f
->next
;
6691 tcc_error("too many field init");
6696 /* must put zero in holes (note that doing it that way
6697 ensures that it even works with designators) */
6698 if (!(flags
& DIF_SIZE_ONLY
) && c
- corig
> al
)
6699 init_putz(sec
, corig
+ al
, c
- corig
- al
);
6700 decl_initializer(type
, sec
, c
, flags
& ~DIF_FIRST
);
6702 /* XXX: make it more general */
6703 if (!(flags
& DIF_SIZE_ONLY
) && nb_elems
> 1) {
6704 unsigned long c_end
;
6709 vset(type
, VT_LOCAL
|VT_LVAL
, c
);
6710 for (i
= 1; i
< nb_elems
; i
++) {
6711 vset(type
, VT_LOCAL
|VT_LVAL
, c
+ elem_size
* i
);
6716 } else if (!NODATA_WANTED
) {
6717 c_end
= c
+ nb_elems
* elem_size
;
6718 if (c_end
> sec
->data_allocated
)
6719 section_realloc(sec
, c_end
);
6720 src
= sec
->data
+ c
;
6722 for(i
= 1; i
< nb_elems
; i
++) {
6724 memcpy(dst
, src
, elem_size
);
6728 c
+= nb_elems
* type_size(type
, &align
);
6734 /* store a value or an expression directly in global data or in local array */
6735 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
)
6742 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
6746 /* XXX: not portable */
6747 /* XXX: generate error if incorrect relocation */
6748 gen_assign_cast(&dtype
);
6749 bt
= type
->t
& VT_BTYPE
;
6751 if ((vtop
->r
& VT_SYM
)
6754 && (bt
!= (PTR_SIZE
== 8 ? VT_LLONG
: VT_INT
)
6755 || (type
->t
& VT_BITFIELD
))
6756 && !((vtop
->r
& VT_CONST
) && vtop
->sym
->v
>= SYM_FIRST_ANOM
)
6758 tcc_error("initializer element is not computable at load time");
6760 if (NODATA_WANTED
) {
6765 size
= type_size(type
, &align
);
6766 section_reserve(sec
, c
+ size
);
6767 ptr
= sec
->data
+ c
;
6769 /* XXX: make code faster ? */
6770 if ((vtop
->r
& (VT_SYM
|VT_CONST
)) == (VT_SYM
|VT_CONST
) &&
6771 vtop
->sym
->v
>= SYM_FIRST_ANOM
&&
6772 /* XXX This rejects compound literals like
6773 '(void *){ptr}'. The problem is that '&sym' is
6774 represented the same way, which would be ruled out
6775 by the SYM_FIRST_ANOM check above, but also '"string"'
6776 in 'char *p = "string"' is represented the same
6777 with the type being VT_PTR and the symbol being an
6778 anonymous one. That is, there's no difference in vtop
6779 between '(void *){x}' and '&(void *){x}'. Ignore
6780 pointer typed entities here. Hopefully no real code
6781 will every use compound literals with scalar type. */
6782 (vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
6783 /* These come from compound literals, memcpy stuff over. */
6787 esym
= elfsym(vtop
->sym
);
6788 ssec
= tcc_state
->sections
[esym
->st_shndx
];
6789 memmove (ptr
, ssec
->data
+ esym
->st_value
, size
);
6791 /* We need to copy over all memory contents, and that
6792 includes relocations. Use the fact that relocs are
6793 created it order, so look from the end of relocs
6794 until we hit one before the copied region. */
6795 int num_relocs
= ssec
->reloc
->data_offset
/ sizeof(*rel
);
6796 rel
= (ElfW_Rel
*)(ssec
->reloc
->data
+ ssec
->reloc
->data_offset
);
6797 while (num_relocs
--) {
6799 if (rel
->r_offset
>= esym
->st_value
+ size
)
6801 if (rel
->r_offset
< esym
->st_value
)
6803 /* Note: if the same fields are initialized multiple
6804 times (possible with designators) then we possibly
6805 add multiple relocations for the same offset here.
6806 That would lead to wrong code, the last reloc needs
6807 to win. We clean this up later after the whole
6808 initializer is parsed. */
6809 put_elf_reloca(symtab_section
, sec
,
6810 c
+ rel
->r_offset
- esym
->st_value
,
6811 ELFW(R_TYPE
)(rel
->r_info
),
6812 ELFW(R_SYM
)(rel
->r_info
),
6822 if (type
->t
& VT_BITFIELD
) {
6823 int bit_pos
, bit_size
, bits
, n
;
6824 unsigned char *p
, v
, m
;
6825 bit_pos
= BIT_POS(vtop
->type
.t
);
6826 bit_size
= BIT_SIZE(vtop
->type
.t
);
6827 p
= (unsigned char*)ptr
+ (bit_pos
>> 3);
6828 bit_pos
&= 7, bits
= 0;
6833 v
= vtop
->c
.i
>> bits
<< bit_pos
;
6834 m
= ((1 << n
) - 1) << bit_pos
;
6835 *p
= (*p
& ~m
) | (v
& m
);
6836 bits
+= n
, bit_size
-= n
, bit_pos
= 0, ++p
;
6840 /* XXX: when cross-compiling we assume that each type has the
6841 same representation on host and target, which is likely to
6842 be wrong in the case of long double */
6844 vtop
->c
.i
= vtop
->c
.i
!= 0;
6846 *(char *)ptr
|= vtop
->c
.i
;
6849 *(short *)ptr
|= vtop
->c
.i
;
6852 *(float*)ptr
= vtop
->c
.f
;
6855 *(double *)ptr
= vtop
->c
.d
;
6858 #if defined TCC_IS_NATIVE_387
6859 if (sizeof (long double) >= 10) /* zero pad ten-byte LD */
6860 memcpy(ptr
, &vtop
->c
.ld
, 10);
6862 else if (sizeof (long double) == sizeof (double))
6863 __asm__("fldl %1\nfstpt %0\n" : "=m" (*ptr
) : "m" (vtop
->c
.ld
));
6865 else if (vtop
->c
.ld
== 0.0)
6869 if (sizeof(long double) == LDOUBLE_SIZE
)
6870 *(long double*)ptr
= vtop
->c
.ld
;
6871 else if (sizeof(double) == LDOUBLE_SIZE
)
6872 *(double *)ptr
= (double)vtop
->c
.ld
;
6874 tcc_error("can't cross compile long double constants");
6878 *(long long *)ptr
|= vtop
->c
.i
;
6885 addr_t val
= vtop
->c
.i
;
6887 if (vtop
->r
& VT_SYM
)
6888 greloca(sec
, vtop
->sym
, c
, R_DATA_PTR
, val
);
6890 *(addr_t
*)ptr
|= val
;
6892 if (vtop
->r
& VT_SYM
)
6893 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
6894 *(addr_t
*)ptr
|= val
;
6900 int val
= vtop
->c
.i
;
6902 if (vtop
->r
& VT_SYM
)
6903 greloca(sec
, vtop
->sym
, c
, R_DATA_PTR
, val
);
6907 if (vtop
->r
& VT_SYM
)
6908 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
6917 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
6924 /* 't' contains the type and storage info. 'c' is the offset of the
6925 object in section 'sec'. If 'sec' is NULL, it means stack based
6926 allocation. 'flags & DIF_FIRST' is true if array '{' must be read (multi
6927 dimension implicit array init handling). 'flags & DIF_SIZE_ONLY' is true if
6928 size only evaluation is wanted (only for arrays). */
6929 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
6932 int len
, n
, no_oblock
, nb
, i
;
6938 if (!(flags
& DIF_HAVE_ELEM
) && tok
!= '{' &&
6939 /* In case of strings we have special handling for arrays, so
6940 don't consume them as initializer value (which would commit them
6941 to some anonymous symbol). */
6942 tok
!= TOK_LSTR
&& tok
!= TOK_STR
&&
6943 !(flags
& DIF_SIZE_ONLY
)) {
6944 parse_init_elem(!sec
? EXPR_ANY
: EXPR_CONST
);
6945 flags
|= DIF_HAVE_ELEM
;
6948 if ((flags
& DIF_HAVE_ELEM
) &&
6949 !(type
->t
& VT_ARRAY
) &&
6950 /* Use i_c_parameter_t, to strip toplevel qualifiers.
6951 The source type might have VT_CONSTANT set, which is
6952 of course assignable to non-const elements. */
6953 is_compatible_unqualified_types(type
, &vtop
->type
)) {
6954 init_putv(type
, sec
, c
);
6955 } else if (type
->t
& VT_ARRAY
) {
6958 t1
= pointed_type(type
);
6959 size1
= type_size(t1
, &align1
);
6962 if (((flags
& DIF_FIRST
) && tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
6965 tcc_error("character array initializer must be a literal,"
6966 " optionally enclosed in braces");
6971 /* only parse strings here if correct type (otherwise: handle
6972 them as ((w)char *) expressions */
6973 if ((tok
== TOK_LSTR
&&
6974 #ifdef TCC_TARGET_PE
6975 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
6977 (t1
->t
& VT_BTYPE
) == VT_INT
6979 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
6981 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
6984 /* compute maximum number of chars wanted */
6986 cstr_len
= tokc
.str
.size
;
6988 cstr_len
= tokc
.str
.size
/ sizeof(nwchar_t
);
6991 if (n
>= 0 && nb
> (n
- len
))
6993 if (!(flags
& DIF_SIZE_ONLY
)) {
6995 tcc_warning("initializer-string for array is too long");
6996 /* in order to go faster for common case (char
6997 string in global variable, we handle it
6999 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
7001 memcpy(sec
->data
+ c
+ len
, tokc
.str
.data
, nb
);
7005 ch
= ((unsigned char *)tokc
.str
.data
)[i
];
7007 ch
= ((nwchar_t
*)tokc
.str
.data
)[i
];
7009 init_putv(t1
, sec
, c
+ (len
+ i
) * size1
);
7016 /* only add trailing zero if enough storage (no
7017 warning in this case since it is standard) */
7018 if (n
< 0 || len
< n
) {
7019 if (!(flags
& DIF_SIZE_ONLY
)) {
7021 init_putv(t1
, sec
, c
+ (len
* size1
));
7032 while (tok
!= '}' || (flags
& DIF_HAVE_ELEM
)) {
7033 len
= decl_designator(type
, sec
, c
, &f
, flags
, len
);
7034 flags
&= ~DIF_HAVE_ELEM
;
7035 if (type
->t
& VT_ARRAY
) {
7037 /* special test for multi dimensional arrays (may not
7038 be strictly correct if designators are used at the
7040 if (no_oblock
&& len
>= n
*size1
)
7043 if (s
->type
.t
== VT_UNION
)
7047 if (no_oblock
&& f
== NULL
)
7056 /* put zeros at the end */
7057 if (!(flags
& DIF_SIZE_ONLY
) && len
< n
*size1
)
7058 init_putz(sec
, c
+ len
, n
*size1
- len
);
7061 /* patch type size if needed, which happens only for array types */
7063 s
->c
= size1
== 1 ? len
: ((len
+ size1
- 1)/size1
);
7064 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
7067 if ((flags
& DIF_FIRST
) || tok
== '{') {
7075 } else if (tok
== '{') {
7076 if (flags
& DIF_HAVE_ELEM
)
7079 decl_initializer(type
, sec
, c
, flags
& ~DIF_HAVE_ELEM
);
7081 } else if ((flags
& DIF_SIZE_ONLY
)) {
7082 /* If we supported only ISO C we wouldn't have to accept calling
7083 this on anything than an array if DIF_SIZE_ONLY (and even then
7084 only on the outermost level, so no recursion would be needed),
7085 because initializing a flex array member isn't supported.
7086 But GNU C supports it, so we need to recurse even into
7087 subfields of structs and arrays when DIF_SIZE_ONLY is set. */
7088 /* just skip expression */
7089 skip_or_save_block(NULL
);
7091 if (!(flags
& DIF_HAVE_ELEM
)) {
7092 /* This should happen only when we haven't parsed
7093 the init element above for fear of committing a
7094 string constant to memory too early. */
7095 if (tok
!= TOK_STR
&& tok
!= TOK_LSTR
)
7096 expect("string constant");
7097 parse_init_elem(!sec
? EXPR_ANY
: EXPR_CONST
);
7099 init_putv(type
, sec
, c
);
7103 /* parse an initializer for type 't' if 'has_init' is non zero, and
7104 allocate space in local or global data space ('r' is either
7105 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
7106 variable 'v' of scope 'scope' is declared before initializers
7107 are parsed. If 'v' is zero, then a reference to the new object
7108 is put in the value stack. If 'has_init' is 2, a special parsing
7109 is done to handle string constants. */
7110 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
7111 int has_init
, int v
, int scope
)
7113 int size
, align
, addr
;
7114 TokenString
*init_str
= NULL
;
7117 Sym
*flexible_array
;
7119 int saved_nocode_wanted
= nocode_wanted
;
7120 #ifdef CONFIG_TCC_BCHECK
7124 /* Always allocate static or global variables */
7125 if (v
&& (r
& VT_VALMASK
) == VT_CONST
)
7126 nocode_wanted
|= 0x80000000;
7128 #ifdef CONFIG_TCC_BCHECK
7129 bcheck
= tcc_state
->do_bounds_check
&& !NODATA_WANTED
;
7132 flexible_array
= NULL
;
7133 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
7134 Sym
*field
= type
->ref
->next
;
7137 field
= field
->next
;
7138 if (field
->type
.t
& VT_ARRAY
&& field
->type
.ref
->c
< 0)
7139 flexible_array
= field
;
7143 size
= type_size(type
, &align
);
7144 /* If unknown size, we must evaluate it before
7145 evaluating initializers because
7146 initializers can generate global data too
7147 (e.g. string pointers or ISOC99 compound
7148 literals). It also simplifies local
7149 initializers handling */
7150 if (size
< 0 || (flexible_array
&& has_init
)) {
7152 tcc_error("unknown type size");
7153 /* get all init string */
7154 if (has_init
== 2) {
7155 init_str
= tok_str_alloc();
7156 /* only get strings */
7157 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
7158 tok_str_add_tok(init_str
);
7161 tok_str_add(init_str
, -1);
7162 tok_str_add(init_str
, 0);
7164 skip_or_save_block(&init_str
);
7169 begin_macro(init_str
, 1);
7171 decl_initializer(type
, NULL
, 0, DIF_FIRST
| DIF_SIZE_ONLY
);
7172 /* prepare second initializer parsing */
7173 macro_ptr
= init_str
->str
;
7176 /* if still unknown size, error */
7177 size
= type_size(type
, &align
);
7179 tcc_error("unknown type size");
7181 /* If there's a flex member and it was used in the initializer
7183 if (flexible_array
&&
7184 flexible_array
->type
.ref
->c
> 0)
7185 size
+= flexible_array
->type
.ref
->c
7186 * pointed_size(&flexible_array
->type
);
7187 /* take into account specified alignment if bigger */
7188 if (ad
->a
.aligned
) {
7189 int speca
= 1 << (ad
->a
.aligned
- 1);
7192 } else if (ad
->a
.packed
) {
7196 if (!v
&& NODATA_WANTED
)
7197 size
= 0, align
= 1;
7199 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
7201 #ifdef CONFIG_TCC_BCHECK
7202 if (bcheck
&& (type
->t
& VT_ARRAY
)) {
7206 loc
= (loc
- size
) & -align
;
7208 #ifdef CONFIG_TCC_BCHECK
7209 /* handles bounds */
7210 /* XXX: currently, since we do only one pass, we cannot track
7211 '&' operators, so we add only arrays */
7212 if (bcheck
&& (type
->t
& VT_ARRAY
)) {
7214 /* add padding between regions */
7216 /* then add local bound info */
7217 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(addr_t
));
7218 bounds_ptr
[0] = addr
;
7219 bounds_ptr
[1] = size
;
7223 /* local variable */
7224 #ifdef CONFIG_TCC_ASM
7225 if (ad
->asm_label
) {
7226 int reg
= asm_parse_regvar(ad
->asm_label
);
7228 r
= (r
& ~VT_VALMASK
) | reg
;
7231 sym
= sym_push(v
, type
, r
, addr
);
7232 if (ad
->cleanup_func
) {
7233 Sym
*cls
= sym_push2(&all_cleanups
, SYM_FIELD
| ++ncleanups
, 0, 0);
7234 cls
->prev_tok
= sym
;
7235 cls
->next
= ad
->cleanup_func
;
7236 cls
->ncl
= current_cleanups
;
7237 current_cleanups
= cls
;
7242 /* push local reference */
7243 vset(type
, r
, addr
);
7246 if (v
&& scope
== VT_CONST
) {
7247 /* see if the symbol was already defined */
7250 patch_storage(sym
, ad
, type
);
7251 /* we accept several definitions of the same global variable. */
7252 if (!has_init
&& sym
->c
&& elfsym(sym
)->st_shndx
!= SHN_UNDEF
)
7257 /* allocate symbol in corresponding section */
7262 else if (tcc_state
->nocommon
)
7267 addr
= section_add(sec
, size
, align
);
7268 #ifdef CONFIG_TCC_BCHECK
7269 /* add padding if bound check */
7271 section_add(sec
, 1, 1);
7274 addr
= align
; /* SHN_COMMON is special, symbol value is align */
7275 sec
= common_section
;
7280 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
7281 patch_storage(sym
, ad
, NULL
);
7283 /* Local statics have a scope until now (for
7284 warnings), remove it here. */
7286 /* update symbol definition */
7287 put_extern_sym(sym
, sec
, addr
, size
);
7289 /* push global reference */
7290 vpush_ref(type
, sec
, addr
, size
);
7295 #ifdef CONFIG_TCC_BCHECK
7296 /* handles bounds now because the symbol must be defined
7297 before for the relocation */
7301 greloca(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
, 0);
7302 /* then add global bound info */
7303 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(addr_t
));
7304 bounds_ptr
[0] = 0; /* relocated */
7305 bounds_ptr
[1] = size
;
7310 if (type
->t
& VT_VLA
) {
7316 /* save current stack pointer */
7317 if (vlas_in_scope
== 0) {
7318 if (vla_sp_root_loc
== -1)
7319 vla_sp_root_loc
= (loc
-= PTR_SIZE
);
7320 gen_vla_sp_save(vla_sp_root_loc
);
7323 vla_runtime_type_size(type
, &a
);
7324 gen_vla_alloc(type
, a
);
7325 #if defined TCC_TARGET_PE && defined TCC_TARGET_X86_64
7326 /* on _WIN64, because of the function args scratch area, the
7327 result of alloca differs from RSP and is returned in RAX. */
7328 gen_vla_result(addr
), addr
= (loc
-= PTR_SIZE
);
7330 gen_vla_sp_save(addr
);
7334 } else if (has_init
) {
7335 size_t oldreloc_offset
= 0;
7336 if (sec
&& sec
->reloc
)
7337 oldreloc_offset
= sec
->reloc
->data_offset
;
7338 decl_initializer(type
, sec
, addr
, DIF_FIRST
);
7339 if (sec
&& sec
->reloc
)
7340 squeeze_multi_relocs(sec
, oldreloc_offset
);
7341 /* patch flexible array member size back to -1, */
7342 /* for possible subsequent similar declarations */
7344 flexible_array
->type
.ref
->c
= -1;
7348 /* restore parse state if needed */
7354 nocode_wanted
= saved_nocode_wanted
;
7357 /* parse a function defined by symbol 'sym' and generate its code in
7358 'cur_text_section' */
7359 static void gen_function(Sym
*sym
)
7362 ind
= cur_text_section
->data_offset
;
7363 if (sym
->a
.aligned
) {
7364 size_t newoff
= section_add(cur_text_section
, 0,
7365 1 << (sym
->a
.aligned
- 1));
7366 gen_fill_nops(newoff
- ind
);
7368 /* NOTE: we patch the symbol size later */
7369 put_extern_sym(sym
, cur_text_section
, ind
, 0);
7370 funcname
= get_tok_str(sym
->v
, NULL
);
7372 /* Initialize VLA state */
7374 vla_sp_root_loc
= -1;
7375 /* put debug symbol */
7376 tcc_debug_funcstart(tcc_state
, sym
);
7377 /* push a dummy symbol to enable local sym storage */
7378 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
7379 local_scope
= 1; /* for function parameters */
7380 gfunc_prolog(&sym
->type
);
7381 reset_local_scope();
7383 clear_temp_local_var_list();
7384 block(NULL
, NULL
, NULL
, NULL
, 0);
7385 if (!(nocode_wanted
& 0x20000000)
7386 && ((func_vt
.t
& VT_BTYPE
) == VT_INT
)
7387 && !strcmp (funcname
, "main"))
7391 gen_assign_cast(&func_vt
);
7392 gfunc_return(&func_vt
);
7397 cur_text_section
->data_offset
= ind
;
7398 label_pop(&global_label_stack
, NULL
, 0);
7399 /* reset local stack */
7400 reset_local_scope();
7401 sym_pop(&local_stack
, NULL
, 0);
7402 /* end of function */
7403 /* patch symbol size */
7404 elfsym(sym
)->st_size
= ind
- func_ind
;
7405 tcc_debug_funcend(tcc_state
, ind
- func_ind
);
7406 /* It's better to crash than to generate wrong code */
7407 cur_text_section
= NULL
;
7408 funcname
= ""; /* for safety */
7409 func_vt
.t
= VT_VOID
; /* for safety */
7410 func_var
= 0; /* for safety */
7411 ind
= 0; /* for safety */
7412 nocode_wanted
= 0x80000000;
7416 static void gen_inline_functions(TCCState
*s
)
7419 int inline_generated
, i
, ln
;
7420 struct InlineFunc
*fn
;
7422 ln
= file
->line_num
;
7423 /* iterate while inline function are referenced */
7425 inline_generated
= 0;
7426 for (i
= 0; i
< s
->nb_inline_fns
; ++i
) {
7427 fn
= s
->inline_fns
[i
];
7429 if (sym
&& (sym
->c
|| !(sym
->type
.t
& (VT_INLINE
| VT_STATIC
)) )) {
7430 /* the function was used or forced (and then not internal):
7431 generate its code and convert it to a normal function */
7434 pstrcpy(file
->filename
, sizeof file
->filename
, fn
->filename
);
7435 begin_macro(fn
->func_str
, 1);
7437 cur_text_section
= text_section
;
7441 inline_generated
= 1;
7444 } while (inline_generated
);
7445 file
->line_num
= ln
;
7448 ST_FUNC
void free_inline_functions(TCCState
*s
)
7451 /* free tokens of unused inline functions */
7452 for (i
= 0; i
< s
->nb_inline_fns
; ++i
) {
7453 struct InlineFunc
*fn
= s
->inline_fns
[i
];
7455 tok_str_free(fn
->func_str
);
7457 dynarray_reset(&s
->inline_fns
, &s
->nb_inline_fns
);
7460 /* 'l' is VT_LOCAL or VT_CONST to define default storage type, or VT_CMP
7461 if parsing old style parameter decl list (and FUNC_SYM is set then) */
7462 static int decl0(int l
, int is_for_loop_init
, Sym
*func_sym
)
7467 AttributeDef ad
, adbase
;
7470 if (tok
== TOK_STATIC_ASSERT
) {
7478 tcc_error("%s", get_tok_str(tok
, &tokc
));
7484 if (!parse_btype(&btype
, &adbase
)) {
7485 if (is_for_loop_init
)
7487 /* skip redundant ';' if not in old parameter decl scope */
7488 if (tok
== ';' && l
!= VT_CMP
) {
7494 if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
7495 /* global asm block */
7499 if (tok
>= TOK_UIDENT
) {
7500 /* special test for old K&R protos without explicit int
7501 type. Only accepted when defining global data */
7505 expect("declaration");
7510 if ((btype
.t
& VT_BTYPE
) == VT_STRUCT
) {
7511 int v
= btype
.ref
->v
;
7512 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) >= SYM_FIRST_ANOM
)
7513 tcc_warning("unnamed struct/union that defines no instances");
7517 if (IS_ENUM(btype
.t
)) {
7522 while (1) { /* iterate thru each declaration */
7524 /* If the base type itself was an array type of unspecified
7525 size (like in 'typedef int arr[]; arr x = {1};') then
7526 we will overwrite the unknown size by the real one for
7527 this decl. We need to unshare the ref symbol holding
7529 if ((type
.t
& VT_ARRAY
) && type
.ref
->c
< 0) {
7530 type
.ref
= sym_push(SYM_FIELD
, &type
.ref
->type
, 0, type
.ref
->c
);
7533 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
7537 type_to_str(buf
, sizeof(buf
), &type
, get_tok_str(v
, NULL
));
7538 printf("type = '%s'\n", buf
);
7541 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
7542 if ((type
.t
& VT_STATIC
) && (l
== VT_LOCAL
)) {
7543 tcc_error("function without file scope cannot be static");
7545 /* if old style function prototype, we accept a
7548 if (sym
->f
.func_type
== FUNC_OLD
&& l
== VT_CONST
)
7549 decl0(VT_CMP
, 0, sym
);
7552 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
7553 ad
.asm_label
= asm_label_instr();
7554 /* parse one last attribute list, after asm label */
7555 parse_attribute(&ad
);
7560 #ifdef TCC_TARGET_PE
7561 if (ad
.a
.dllimport
|| ad
.a
.dllexport
) {
7562 if (type
.t
& (VT_STATIC
|VT_TYPEDEF
))
7563 tcc_error("cannot have dll linkage with static or typedef");
7564 if (ad
.a
.dllimport
) {
7565 if ((type
.t
& VT_BTYPE
) == VT_FUNC
)
7568 type
.t
|= VT_EXTERN
;
7574 tcc_error("cannot use local functions");
7575 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
7576 expect("function definition");
7578 /* reject abstract declarators in function definition
7579 make old style params without decl have int type */
7581 while ((sym
= sym
->next
) != NULL
) {
7582 if (!(sym
->v
& ~SYM_FIELD
))
7583 expect("identifier");
7584 if (sym
->type
.t
== VT_VOID
)
7585 sym
->type
= int_type
;
7588 /* put function symbol */
7589 sym
= external_sym(v
, &type
, 0, &ad
);
7590 /* This is the def, so overwrite any other parameter names
7591 we got from prototypes. */
7592 sym
->type
.ref
= type
.ref
;
7593 if (sym
->c
&& elfsym(sym
)->st_shndx
!= SHN_UNDEF
7594 && !(elfsym(sym
)->st_other
& ST_ASM_SET
))
7595 tcc_error("redefinition of '%s'", get_tok_str(sym
->v
, NULL
));
7597 /* static inline functions are just recorded as a kind
7598 of macro. Their code will be emitted at the end of
7599 the compilation unit only if they are used */
7600 if ((sym
->type
.t
& (VT_INLINE
| VT_EXTERN
)) == VT_INLINE
) {
7601 struct InlineFunc
*fn
;
7602 const char *filename
;
7604 filename
= file
? file
->filename
: "";
7605 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
7606 strcpy(fn
->filename
, filename
);
7608 skip_or_save_block(&fn
->func_str
);
7609 dynarray_add(&tcc_state
->inline_fns
,
7610 &tcc_state
->nb_inline_fns
, fn
);
7612 /* compute text section */
7613 cur_text_section
= ad
.section
;
7614 if (!cur_text_section
)
7615 cur_text_section
= text_section
;
7621 /* find parameter in function parameter list */
7622 for (sym
= func_sym
->next
; sym
; sym
= sym
->next
)
7623 if ((sym
->v
& ~SYM_FIELD
) == v
)
7625 tcc_error("declaration for parameter '%s' but no such parameter",
7626 get_tok_str(v
, NULL
));
7628 if (type
.t
& VT_STORAGE
) /* 'register' is okay */
7629 tcc_error("storage class specified for '%s'",
7630 get_tok_str(v
, NULL
));
7631 if (sym
->type
.t
!= VT_VOID
)
7632 tcc_error("redefinition of parameter '%s'",
7633 get_tok_str(v
, NULL
));
7634 convert_parameter_type(&type
);
7636 } else if (type
.t
& VT_TYPEDEF
) {
7637 /* save typedefed type */
7638 /* XXX: test storage specifiers ? */
7640 if (sym
&& sym
->sym_scope
== local_scope
) {
7641 if (!is_compatible_types(&sym
->type
, &type
)
7642 || !(sym
->type
.t
& VT_TYPEDEF
))
7643 tcc_error("incompatible redefinition of '%s'",
7644 get_tok_str(v
, NULL
));
7647 sym
= sym_push(v
, &type
, 0, 0);
7651 } else if ((type
.t
& VT_BTYPE
) == VT_VOID
7652 && !(type
.t
& VT_EXTERN
)) {
7653 tcc_error("declaration of void object");
7656 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
7657 /* external function definition */
7658 /* specific case for func_call attribute */
7660 } else if (!(type
.t
& VT_ARRAY
)) {
7661 /* not lvalue if array */
7662 r
|= lvalue_type(type
.t
);
7664 has_init
= (tok
== '=');
7665 if (has_init
&& (type
.t
& VT_VLA
))
7666 tcc_error("variable length array cannot be initialized");
7667 if (((type
.t
& VT_EXTERN
) && (!has_init
|| l
!= VT_CONST
)) ||
7668 ((type
.t
& VT_BTYPE
) == VT_FUNC
) ||
7669 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
7670 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
7671 /* external variable or function */
7672 /* NOTE: as GCC, uninitialized global static
7673 arrays of null size are considered as
7675 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
7676 type
.t
|= VT_EXTERN
;
7677 sym
= external_sym(v
, &type
, r
, &ad
);
7678 if (ad
.alias_target
) {
7681 alias_target
= sym_find(ad
.alias_target
);
7682 esym
= elfsym(alias_target
);
7684 tcc_error("unsupported forward __alias__ attribute");
7685 /* Local statics have a scope until now (for
7686 warnings), remove it here. */
7688 put_extern_sym2(sym
, esym
->st_shndx
, esym
->st_value
, esym
->st_size
, 0);
7691 if (type
.t
& VT_STATIC
)
7697 else if (l
== VT_CONST
7698 && (type
.t
& VT_BTYPE
) != VT_FUNC
)
7699 /* uninitialized global variables may be overridden */
7700 type
.t
|= VT_EXTERN
;
7701 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, l
);
7705 if (is_for_loop_init
)
7717 static void decl(int l
)
7722 /* ------------------------------------------------------------------------- */