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
, int *csym
, 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_bind
= STB_LOCAL
;
330 else if (sym
->a
.weak
)
333 sym_bind
= STB_GLOBAL
;
334 old_sym_bind
= ELFW(ST_BIND
)(esym
->st_info
);
335 if (sym_bind
!= old_sym_bind
) {
336 esym
->st_info
= ELFW(ST_INFO
)(sym_bind
, ELFW(ST_TYPE
)(esym
->st_info
));
340 if (sym
->a
.dllimport
)
341 esym
->st_other
|= ST_PE_IMPORT
;
342 if (sym
->a
.dllexport
)
343 esym
->st_other
|= ST_PE_EXPORT
;
347 printf("storage %s: bind=%c vis=%d exp=%d imp=%d\n",
348 get_tok_str(sym
->v
, NULL
),
349 sym_bind
== STB_WEAK
? 'w' : sym_bind
== STB_LOCAL
? 'l' : 'g',
357 /* ------------------------------------------------------------------------- */
358 /* update sym->c so that it points to an external symbol in section
359 'section' with value 'value' */
361 ST_FUNC
void put_extern_sym2(Sym
*sym
, int sh_num
,
362 addr_t value
, unsigned long size
,
363 int can_add_underscore
)
365 int sym_type
, sym_bind
, info
, other
, t
;
369 #ifdef CONFIG_TCC_BCHECK
374 name
= get_tok_str(sym
->v
, NULL
);
375 #ifdef CONFIG_TCC_BCHECK
376 if (tcc_state
->do_bounds_check
) {
377 /* XXX: avoid doing that for statics ? */
378 /* if bound checking is activated, we change some function
379 names by adding the "__bound" prefix */
382 /* XXX: we rely only on malloc hooks */
395 strcpy(buf
, "__bound_");
403 if ((t
& VT_BTYPE
) == VT_FUNC
) {
405 } else if ((t
& VT_BTYPE
) == VT_VOID
) {
406 sym_type
= STT_NOTYPE
;
408 sym_type
= STT_OBJECT
;
411 sym_bind
= STB_LOCAL
;
413 sym_bind
= STB_GLOBAL
;
416 if (sym_type
== STT_FUNC
&& sym
->type
.ref
) {
417 Sym
*ref
= sym
->type
.ref
;
418 if (ref
->a
.nodecorate
) {
419 can_add_underscore
= 0;
421 if (ref
->f
.func_call
== FUNC_STDCALL
&& can_add_underscore
) {
422 sprintf(buf1
, "_%s@%d", name
, ref
->f
.func_args
* PTR_SIZE
);
424 other
|= ST_PE_STDCALL
;
425 can_add_underscore
= 0;
429 if (tcc_state
->leading_underscore
&& can_add_underscore
) {
431 pstrcpy(buf1
+ 1, sizeof(buf1
) - 1, name
);
435 name
= get_tok_str(sym
->asm_label
, NULL
);
436 info
= ELFW(ST_INFO
)(sym_bind
, sym_type
);
437 sym
->c
= put_elf_sym(symtab_section
, value
, size
, info
, other
, sh_num
, name
);
440 esym
->st_value
= value
;
441 esym
->st_size
= size
;
442 esym
->st_shndx
= sh_num
;
447 ST_FUNC
void put_extern_sym(Sym
*sym
, Section
*section
,
448 addr_t value
, unsigned long size
)
450 int sh_num
= section
? section
->sh_num
: SHN_UNDEF
;
451 put_extern_sym2(sym
, sh_num
, value
, size
, 1);
454 /* add a new relocation entry to symbol 'sym' in section 's' */
455 ST_FUNC
void greloca(Section
*s
, Sym
*sym
, unsigned long offset
, int type
,
460 if (nocode_wanted
&& s
== cur_text_section
)
465 put_extern_sym(sym
, NULL
, 0, 0);
469 /* now we can add ELF relocation info */
470 put_elf_reloca(symtab_section
, s
, offset
, type
, c
, addend
);
474 ST_FUNC
void greloc(Section
*s
, Sym
*sym
, unsigned long offset
, int type
)
476 greloca(s
, sym
, offset
, type
, 0);
480 /* ------------------------------------------------------------------------- */
481 /* symbol allocator */
482 static Sym
*__sym_malloc(void)
484 Sym
*sym_pool
, *sym
, *last_sym
;
487 sym_pool
= tcc_malloc(SYM_POOL_NB
* sizeof(Sym
));
488 dynarray_add(&sym_pools
, &nb_sym_pools
, sym_pool
);
490 last_sym
= sym_free_first
;
492 for(i
= 0; i
< SYM_POOL_NB
; i
++) {
493 sym
->next
= last_sym
;
497 sym_free_first
= last_sym
;
501 static inline Sym
*sym_malloc(void)
505 sym
= sym_free_first
;
507 sym
= __sym_malloc();
508 sym_free_first
= sym
->next
;
511 sym
= tcc_malloc(sizeof(Sym
));
516 ST_INLN
void sym_free(Sym
*sym
)
519 sym
->next
= sym_free_first
;
520 sym_free_first
= sym
;
526 /* push, without hashing */
527 ST_FUNC Sym
*sym_push2(Sym
**ps
, int v
, int t
, int c
)
532 memset(s
, 0, sizeof *s
);
542 /* find a symbol and return its associated structure. 's' is the top
543 of the symbol stack */
544 ST_FUNC Sym
*sym_find2(Sym
*s
, int v
)
556 /* structure lookup */
557 ST_INLN Sym
*struct_find(int v
)
560 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
562 return table_ident
[v
]->sym_struct
;
565 /* find an identifier */
566 ST_INLN Sym
*sym_find(int v
)
569 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
571 return table_ident
[v
]->sym_identifier
;
574 static int sym_scope(Sym
*s
)
576 if (IS_ENUM_VAL (s
->type
.t
))
577 return s
->type
.ref
->sym_scope
;
582 /* push a given symbol on the symbol stack */
583 ST_FUNC Sym
*sym_push(int v
, CType
*type
, int r
, int c
)
592 s
= sym_push2(ps
, v
, type
->t
, c
);
593 s
->type
.ref
= type
->ref
;
595 /* don't record fields or anonymous symbols */
597 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
598 /* record symbol in token array */
599 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
601 ps
= &ts
->sym_struct
;
603 ps
= &ts
->sym_identifier
;
606 s
->sym_scope
= local_scope
;
607 if (s
->prev_tok
&& sym_scope(s
->prev_tok
) == s
->sym_scope
)
608 tcc_error("redeclaration of '%s'",
609 get_tok_str(v
& ~SYM_STRUCT
, NULL
));
614 /* push a global identifier */
615 ST_FUNC Sym
*global_identifier_push(int v
, int t
, int c
)
618 s
= sym_push2(&global_stack
, v
, t
, c
);
619 s
->r
= VT_CONST
| VT_SYM
;
620 /* don't record anonymous symbol */
621 if (v
< SYM_FIRST_ANOM
) {
622 ps
= &table_ident
[v
- TOK_IDENT
]->sym_identifier
;
623 /* modify the top most local identifier, so that sym_identifier will
624 point to 's' when popped; happens when called from inline asm */
625 while (*ps
!= NULL
&& (*ps
)->sym_scope
)
626 ps
= &(*ps
)->prev_tok
;
633 /* pop symbols until top reaches 'b'. If KEEP is non-zero don't really
634 pop them yet from the list, but do remove them from the token array. */
635 ST_FUNC
void sym_pop(Sym
**ptop
, Sym
*b
, int keep
)
645 /* remove symbol in token array */
647 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
648 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
650 ps
= &ts
->sym_struct
;
652 ps
= &ts
->sym_identifier
;
663 /* ------------------------------------------------------------------------- */
665 static void vsetc(CType
*type
, int r
, CValue
*vc
)
669 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
670 tcc_error("memory full (vstack)");
671 /* cannot let cpu flags if other instruction are generated. Also
672 avoid leaving VT_JMP anywhere except on the top of the stack
673 because it would complicate the code generator.
675 Don't do this when nocode_wanted. vtop might come from
676 !nocode_wanted regions (see 88_codeopt.c) and transforming
677 it to a register without actually generating code is wrong
678 as their value might still be used for real. All values
679 we push under nocode_wanted will eventually be popped
680 again, so that the VT_CMP/VT_JMP value will be in vtop
681 when code is unsuppressed again.
683 Same logic below in vswap(); */
684 if (vtop
>= vstack
&& !nocode_wanted
) {
685 v
= vtop
->r
& VT_VALMASK
;
686 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
698 ST_FUNC
void vswap(void)
701 /* cannot vswap cpu flags. See comment at vsetc() above */
702 if (vtop
>= vstack
&& !nocode_wanted
) {
703 int v
= vtop
->r
& VT_VALMASK
;
704 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
712 /* pop stack value */
713 ST_FUNC
void vpop(void)
716 v
= vtop
->r
& VT_VALMASK
;
717 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
718 /* for x86, we need to pop the FP stack */
720 o(0xd8dd); /* fstp %st(0) */
723 if (v
== VT_JMP
|| v
== VT_JMPI
) {
724 /* need to put correct jump if && or || without test */
730 /* push constant of type "type" with useless value */
731 ST_FUNC
void vpush(CType
*type
)
733 vset(type
, VT_CONST
, 0);
736 /* push integer constant */
737 ST_FUNC
void vpushi(int v
)
741 vsetc(&int_type
, VT_CONST
, &cval
);
744 /* push a pointer sized constant */
745 static void vpushs(addr_t v
)
749 vsetc(&size_type
, VT_CONST
, &cval
);
752 /* push arbitrary 64bit constant */
753 ST_FUNC
void vpush64(int ty
, unsigned long long v
)
760 vsetc(&ctype
, VT_CONST
, &cval
);
763 /* push long long constant */
764 static inline void vpushll(long long v
)
766 vpush64(VT_LLONG
, v
);
769 ST_FUNC
void vset(CType
*type
, int r
, int v
)
774 vsetc(type
, r
, &cval
);
777 static void vseti(int r
, int v
)
785 ST_FUNC
void vpushv(SValue
*v
)
787 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
788 tcc_error("memory full (vstack)");
793 static void vdup(void)
798 /* rotate n first stack elements to the bottom
799 I1 ... In -> I2 ... In I1 [top is right]
801 ST_FUNC
void vrotb(int n
)
812 /* rotate the n elements before entry e towards the top
813 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
815 ST_FUNC
void vrote(SValue
*e
, int n
)
821 for(i
= 0;i
< n
- 1; i
++)
826 /* rotate n first stack elements to the top
827 I1 ... In -> In I1 ... I(n-1) [top is right]
829 ST_FUNC
void vrott(int n
)
834 /* push a symbol value of TYPE */
835 static inline void vpushsym(CType
*type
, Sym
*sym
)
839 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
843 /* Return a static symbol pointing to a section */
844 ST_FUNC Sym
*get_sym_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
850 sym
= sym_push(v
, type
, VT_CONST
| VT_SYM
, 0);
851 sym
->type
.t
|= VT_STATIC
;
852 put_extern_sym(sym
, sec
, offset
, size
);
856 /* push a reference to a section offset by adding a dummy symbol */
857 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
859 vpushsym(type
, get_sym_ref(type
, sec
, offset
, size
));
862 /* define a new external reference to a symbol 'v' of type 'u' */
863 ST_FUNC Sym
*external_global_sym(int v
, CType
*type
)
869 /* push forward reference */
870 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
871 s
->type
.ref
= type
->ref
;
872 } else if (IS_ASM_SYM(s
)) {
873 s
->type
.t
= type
->t
| (s
->type
.t
& VT_EXTERN
);
874 s
->type
.ref
= type
->ref
;
880 /* Merge symbol attributes. */
881 static void merge_symattr(struct SymAttr
*sa
, struct SymAttr
*sa1
)
883 if (sa1
->aligned
&& !sa
->aligned
)
884 sa
->aligned
= sa1
->aligned
;
885 sa
->packed
|= sa1
->packed
;
886 sa
->weak
|= sa1
->weak
;
887 if (sa1
->visibility
!= STV_DEFAULT
) {
888 int vis
= sa
->visibility
;
889 if (vis
== STV_DEFAULT
890 || vis
> sa1
->visibility
)
891 vis
= sa1
->visibility
;
892 sa
->visibility
= vis
;
894 sa
->dllexport
|= sa1
->dllexport
;
895 sa
->nodecorate
|= sa1
->nodecorate
;
896 sa
->dllimport
|= sa1
->dllimport
;
899 /* Merge function attributes. */
900 static void merge_funcattr(struct FuncAttr
*fa
, struct FuncAttr
*fa1
)
902 if (fa1
->func_call
&& !fa
->func_call
)
903 fa
->func_call
= fa1
->func_call
;
904 if (fa1
->func_type
&& !fa
->func_type
)
905 fa
->func_type
= fa1
->func_type
;
906 if (fa1
->func_args
&& !fa
->func_args
)
907 fa
->func_args
= fa1
->func_args
;
910 /* Merge attributes. */
911 static void merge_attr(AttributeDef
*ad
, AttributeDef
*ad1
)
913 merge_symattr(&ad
->a
, &ad1
->a
);
914 merge_funcattr(&ad
->f
, &ad1
->f
);
917 ad
->section
= ad1
->section
;
918 if (ad1
->alias_target
)
919 ad
->alias_target
= ad1
->alias_target
;
921 ad
->asm_label
= ad1
->asm_label
;
923 ad
->attr_mode
= ad1
->attr_mode
;
926 /* Merge some type attributes. */
927 static void patch_type(Sym
*sym
, CType
*type
)
929 if (!(type
->t
& VT_EXTERN
) || IS_ENUM_VAL(sym
->type
.t
)) {
930 if (!(sym
->type
.t
& VT_EXTERN
))
931 tcc_error("redefinition of '%s'", get_tok_str(sym
->v
, NULL
));
932 sym
->type
.t
&= ~VT_EXTERN
;
935 if (IS_ASM_SYM(sym
)) {
936 /* stay static if both are static */
937 sym
->type
.t
= type
->t
& (sym
->type
.t
| ~VT_STATIC
);
938 sym
->type
.ref
= type
->ref
;
941 if (!is_compatible_types(&sym
->type
, type
)) {
942 tcc_error("incompatible types for redefinition of '%s'",
943 get_tok_str(sym
->v
, NULL
));
945 } else if ((sym
->type
.t
& VT_BTYPE
) == VT_FUNC
) {
946 int static_proto
= sym
->type
.t
& VT_STATIC
;
947 /* warn if static follows non-static function declaration */
948 if ((type
->t
& VT_STATIC
) && !static_proto
&& !(type
->t
& VT_INLINE
))
949 tcc_warning("static storage ignored for redefinition of '%s'",
950 get_tok_str(sym
->v
, NULL
));
952 if (0 == (type
->t
& VT_EXTERN
)) {
953 /* put complete type, use static from prototype */
954 sym
->type
.t
= (type
->t
& ~VT_STATIC
) | static_proto
;
955 if (type
->t
& VT_INLINE
)
956 sym
->type
.t
= type
->t
;
957 sym
->type
.ref
= type
->ref
;
961 if ((sym
->type
.t
& VT_ARRAY
) && type
->ref
->c
>= 0) {
962 /* set array size if it was omitted in extern declaration */
963 if (sym
->type
.ref
->c
< 0)
964 sym
->type
.ref
->c
= type
->ref
->c
;
965 else if (sym
->type
.ref
->c
!= type
->ref
->c
)
966 tcc_error("conflicting type for '%s'", get_tok_str(sym
->v
, NULL
));
968 if ((type
->t
^ sym
->type
.t
) & VT_STATIC
)
969 tcc_warning("storage mismatch for redefinition of '%s'",
970 get_tok_str(sym
->v
, NULL
));
975 /* Merge some storage attributes. */
976 static void patch_storage(Sym
*sym
, AttributeDef
*ad
, CType
*type
)
979 patch_type(sym
, type
);
982 if (sym
->a
.dllimport
!= ad
->a
.dllimport
)
983 tcc_error("incompatible dll linkage for redefinition of '%s'",
984 get_tok_str(sym
->v
, NULL
));
986 merge_symattr(&sym
->a
, &ad
->a
);
988 sym
->asm_label
= ad
->asm_label
;
992 /* define a new external reference to a symbol 'v' */
993 static Sym
*external_sym(int v
, CType
*type
, int r
, AttributeDef
*ad
)
997 if (!s
|| (!IS_ASM_SYM(s
) && !(s
->type
.t
& VT_EXTERN
)
998 && (s
->type
.t
& VT_BTYPE
) != VT_FUNC
)) {
999 if (s
&& !is_compatible_types(&s
->type
, type
))
1000 tcc_error("conflicting types for '%s'", get_tok_str(s
->v
, NULL
));
1001 /* push forward reference */
1002 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
1006 if (s
->type
.ref
== func_old_type
.ref
) {
1007 s
->type
.ref
= type
->ref
;
1008 s
->r
= r
| VT_CONST
| VT_SYM
;
1009 s
->type
.t
|= VT_EXTERN
;
1011 patch_storage(s
, ad
, type
);
1016 /* push a reference to global symbol v */
1017 ST_FUNC
void vpush_global_sym(CType
*type
, int v
)
1019 vpushsym(type
, external_global_sym(v
, type
));
1022 /* save registers up to (vtop - n) stack entry */
1023 ST_FUNC
void save_regs(int n
)
1026 for(p
= vstack
, p1
= vtop
- n
; p
<= p1
; p
++)
1030 /* save r to the memory stack, and mark it as being free */
1031 ST_FUNC
void save_reg(int r
)
1033 save_reg_upstack(r
, 0);
1036 /* save r to the memory stack, and mark it as being free,
1037 if seen up to (vtop - n) stack entry */
1038 ST_FUNC
void save_reg_upstack(int r
, int n
)
1040 int l
, saved
, size
, align
;
1044 if ((r
&= VT_VALMASK
) >= VT_CONST
)
1049 /* modify all stack values */
1052 for(p
= vstack
, p1
= vtop
- n
; p
<= p1
; p
++) {
1053 if ((p
->r
& VT_VALMASK
) == r
||
1054 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
1055 /* must save value on stack if not already done */
1057 /* NOTE: must reload 'r' because r might be equal to r2 */
1058 r
= p
->r
& VT_VALMASK
;
1059 /* store register in the stack */
1061 if ((p
->r
& VT_LVAL
) ||
1062 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
1064 type
= &char_pointer_type
;
1068 size
= type_size(type
, &align
);
1069 l
=get_temp_local_var(size
,align
);
1070 sv
.type
.t
= type
->t
;
1071 sv
.r
= VT_LOCAL
| VT_LVAL
;
1074 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1075 /* x86 specific: need to pop fp register ST0 if saved */
1076 if (r
== TREG_ST0
) {
1077 o(0xd8dd); /* fstp %st(0) */
1081 /* special long long case */
1082 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
1089 /* mark that stack entry as being saved on the stack */
1090 if (p
->r
& VT_LVAL
) {
1091 /* also clear the bounded flag because the
1092 relocation address of the function was stored in
1094 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
1096 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
1104 #ifdef TCC_TARGET_ARM
1105 /* find a register of class 'rc2' with at most one reference on stack.
1106 * If none, call get_reg(rc) */
1107 ST_FUNC
int get_reg_ex(int rc
, int rc2
)
1112 for(r
=0;r
<NB_REGS
;r
++) {
1113 if (reg_classes
[r
] & rc2
) {
1116 for(p
= vstack
; p
<= vtop
; p
++) {
1117 if ((p
->r
& VT_VALMASK
) == r
||
1118 (p
->r2
& VT_VALMASK
) == r
)
1129 /* find a free register of class 'rc'. If none, save one register */
1130 ST_FUNC
int get_reg(int rc
)
1135 /* find a free register */
1136 for(r
=0;r
<NB_REGS
;r
++) {
1137 if (reg_classes
[r
] & rc
) {
1140 for(p
=vstack
;p
<=vtop
;p
++) {
1141 if ((p
->r
& VT_VALMASK
) == r
||
1142 (p
->r2
& VT_VALMASK
) == r
)
1150 /* no register left : free the first one on the stack (VERY
1151 IMPORTANT to start from the bottom to ensure that we don't
1152 spill registers used in gen_opi()) */
1153 for(p
=vstack
;p
<=vtop
;p
++) {
1154 /* look at second register (if long long) */
1155 r
= p
->r2
& VT_VALMASK
;
1156 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
1158 r
= p
->r
& VT_VALMASK
;
1159 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
1165 /* Should never comes here */
1169 /* find a free temporary local variable (return the offset on stack) match the size and align. If none, add new temporary stack variable*/
1170 static int get_temp_local_var(int size
,int align
){
1172 struct temp_local_variable
*temp_var
;
1179 for(i
=0;i
<nb_temp_local_vars
;i
++){
1180 temp_var
=&arr_temp_local_vars
[i
];
1181 if(temp_var
->size
<size
||align
!=temp_var
->align
){
1184 /*check if temp_var is free*/
1186 for(p
=vstack
;p
<=vtop
;p
++) {
1188 if(r
==VT_LOCAL
||r
==VT_LLOCAL
){
1189 if(p
->c
.i
==temp_var
->location
){
1196 found_var
=temp_var
->location
;
1202 loc
= (loc
- size
) & -align
;
1203 if(nb_temp_local_vars
<MAX_TEMP_LOCAL_VARIABLE_NUMBER
){
1204 temp_var
=&arr_temp_local_vars
[i
];
1205 temp_var
->location
=loc
;
1206 temp_var
->size
=size
;
1207 temp_var
->align
=align
;
1208 nb_temp_local_vars
++;
1215 static void clear_temp_local_var_list(){
1216 nb_temp_local_vars
=0;
1219 /* move register 's' (of type 't') to 'r', and flush previous value of r to memory
1221 static void move_reg(int r
, int s
, int t
)
1235 /* get address of vtop (vtop MUST BE an lvalue) */
1236 ST_FUNC
void gaddrof(void)
1238 vtop
->r
&= ~VT_LVAL
;
1239 /* tricky: if saved lvalue, then we can go back to lvalue */
1240 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
1241 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
1246 #ifdef CONFIG_TCC_BCHECK
1247 /* generate lvalue bound code */
1248 static void gbound(void)
1253 vtop
->r
&= ~VT_MUSTBOUND
;
1254 /* if lvalue, then use checking code before dereferencing */
1255 if (vtop
->r
& VT_LVAL
) {
1256 /* if not VT_BOUNDED value, then make one */
1257 if (!(vtop
->r
& VT_BOUNDED
)) {
1258 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
1259 /* must save type because we must set it to int to get pointer */
1261 vtop
->type
.t
= VT_PTR
;
1264 gen_bounded_ptr_add();
1265 vtop
->r
|= lval_type
;
1268 /* then check for dereferencing */
1269 gen_bounded_ptr_deref();
1274 static void incr_bf_adr(int o
)
1276 vtop
->type
= char_pointer_type
;
1280 vtop
->type
.t
= (vtop
->type
.t
& ~(VT_BTYPE
|VT_DEFSIGN
))
1281 | (VT_BYTE
|VT_UNSIGNED
);
1282 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
1283 | (VT_LVAL_BYTE
|VT_LVAL_UNSIGNED
|VT_LVAL
);
1286 /* single-byte load mode for packed or otherwise unaligned bitfields */
1287 static void load_packed_bf(CType
*type
, int bit_pos
, int bit_size
)
1290 save_reg_upstack(vtop
->r
, 1);
1291 vpush64(type
->t
& VT_BTYPE
, 0); // B X
1292 bits
= 0, o
= bit_pos
>> 3, bit_pos
&= 7;
1301 vpushi(bit_pos
), gen_op(TOK_SHR
), bit_pos
= 0; // X B Y
1303 vpushi((1 << n
) - 1), gen_op('&');
1306 vpushi(bits
), gen_op(TOK_SHL
);
1309 bits
+= n
, bit_size
-= n
, o
= 1;
1312 if (!(type
->t
& VT_UNSIGNED
)) {
1313 n
= ((type
->t
& VT_BTYPE
) == VT_LLONG
? 64 : 32) - bits
;
1314 vpushi(n
), gen_op(TOK_SHL
);
1315 vpushi(n
), gen_op(TOK_SAR
);
1319 /* single-byte store mode for packed or otherwise unaligned bitfields */
1320 static void store_packed_bf(int bit_pos
, int bit_size
)
1322 int bits
, n
, o
, m
, c
;
1324 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1326 save_reg_upstack(vtop
->r
, 1);
1327 bits
= 0, o
= bit_pos
>> 3, bit_pos
&= 7;
1329 incr_bf_adr(o
); // X B
1331 c
? vdup() : gv_dup(); // B V X
1334 vpushi(bits
), gen_op(TOK_SHR
);
1336 vpushi(bit_pos
), gen_op(TOK_SHL
);
1341 m
= ((1 << n
) - 1) << bit_pos
;
1342 vpushi(m
), gen_op('&'); // X B V1
1343 vpushv(vtop
-1); // X B V1 B
1344 vpushi(m
& 0x80 ? ~m
& 0x7f : ~m
);
1345 gen_op('&'); // X B V1 B1
1346 gen_op('|'); // X B V2
1348 vdup(), vtop
[-1] = vtop
[-2]; // X B B V2
1349 vstore(), vpop(); // X B
1350 bits
+= n
, bit_size
-= n
, bit_pos
= 0, o
= 1;
1355 static int adjust_bf(SValue
*sv
, int bit_pos
, int bit_size
)
1358 if (0 == sv
->type
.ref
)
1360 t
= sv
->type
.ref
->auxtype
;
1361 if (t
!= -1 && t
!= VT_STRUCT
) {
1362 sv
->type
.t
= (sv
->type
.t
& ~VT_BTYPE
) | t
;
1363 sv
->r
= (sv
->r
& ~VT_LVAL_TYPE
) | lvalue_type(sv
->type
.t
);
1368 /* store vtop a register belonging to class 'rc'. lvalues are
1369 converted to values. Cannot be used if cannot be converted to
1370 register value (such as structures). */
1371 ST_FUNC
int gv(int rc
)
1373 int r
, bit_pos
, bit_size
, size
, align
, rc2
;
1375 /* NOTE: get_reg can modify vstack[] */
1376 if (vtop
->type
.t
& VT_BITFIELD
) {
1379 bit_pos
= BIT_POS(vtop
->type
.t
);
1380 bit_size
= BIT_SIZE(vtop
->type
.t
);
1381 /* remove bit field info to avoid loops */
1382 vtop
->type
.t
&= ~VT_STRUCT_MASK
;
1385 type
.t
= vtop
->type
.t
& VT_UNSIGNED
;
1386 if ((vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
1387 type
.t
|= VT_UNSIGNED
;
1389 r
= adjust_bf(vtop
, bit_pos
, bit_size
);
1391 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
)
1396 if (r
== VT_STRUCT
) {
1397 load_packed_bf(&type
, bit_pos
, bit_size
);
1399 int bits
= (type
.t
& VT_BTYPE
) == VT_LLONG
? 64 : 32;
1400 /* cast to int to propagate signedness in following ops */
1402 /* generate shifts */
1403 vpushi(bits
- (bit_pos
+ bit_size
));
1405 vpushi(bits
- bit_size
);
1406 /* NOTE: transformed to SHR if unsigned */
1411 if (is_float(vtop
->type
.t
) &&
1412 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
1413 unsigned long offset
;
1414 /* CPUs usually cannot use float constants, so we store them
1415 generically in data segment */
1416 size
= type_size(&vtop
->type
, &align
);
1418 size
= 0, align
= 1;
1419 offset
= section_add(data_section
, size
, align
);
1420 vpush_ref(&vtop
->type
, data_section
, offset
, size
);
1422 init_putv(&vtop
->type
, data_section
, offset
);
1425 #ifdef CONFIG_TCC_BCHECK
1426 if (vtop
->r
& VT_MUSTBOUND
)
1430 r
= vtop
->r
& VT_VALMASK
;
1431 rc2
= (rc
& RC_FLOAT
) ? RC_FLOAT
: RC_INT
;
1432 #ifndef TCC_TARGET_ARM64
1435 #ifdef TCC_TARGET_X86_64
1436 else if (rc
== RC_FRET
)
1440 /* need to reload if:
1442 - lvalue (need to dereference pointer)
1443 - already a register, but not in the right class */
1445 || (vtop
->r
& VT_LVAL
)
1446 || !(reg_classes
[r
] & rc
)
1448 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
1449 || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
&& !(reg_classes
[vtop
->r2
] & rc2
))
1451 || ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&& !(reg_classes
[vtop
->r2
] & rc2
))
1457 if (((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) || ((vtop
->type
.t
& VT_BTYPE
) == VT_QFLOAT
)) {
1458 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
1460 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
1461 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
1462 unsigned long long ll
;
1464 int r2
, original_type
;
1465 original_type
= vtop
->type
.t
;
1466 /* two register type load : expand to two words
1469 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
1472 vtop
->c
.i
= ll
; /* first word */
1474 vtop
->r
= r
; /* save register value */
1475 vpushi(ll
>> 32); /* second word */
1478 if (vtop
->r
& VT_LVAL
) {
1479 /* We do not want to modifier the long long
1480 pointer here, so the safest (and less
1481 efficient) is to save all the other registers
1482 in the stack. XXX: totally inefficient. */
1486 /* lvalue_save: save only if used further down the stack */
1487 save_reg_upstack(vtop
->r
, 1);
1489 /* load from memory */
1490 vtop
->type
.t
= load_type
;
1493 vtop
[-1].r
= r
; /* save register value */
1494 /* increment pointer to get second word */
1495 vtop
->type
.t
= addr_type
;
1500 vtop
->type
.t
= load_type
;
1502 /* move registers */
1505 vtop
[-1].r
= r
; /* save register value */
1506 vtop
->r
= vtop
[-1].r2
;
1508 /* Allocate second register. Here we rely on the fact that
1509 get_reg() tries first to free r2 of an SValue. */
1513 /* write second register */
1515 vtop
->type
.t
= original_type
;
1516 } else if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
1518 /* lvalue of scalar type : need to use lvalue type
1519 because of possible cast */
1522 /* compute memory access type */
1523 if (vtop
->r
& VT_LVAL_BYTE
)
1525 else if (vtop
->r
& VT_LVAL_SHORT
)
1527 if (vtop
->r
& VT_LVAL_UNSIGNED
)
1531 /* restore wanted type */
1534 /* one register type load */
1539 #ifdef TCC_TARGET_C67
1540 /* uses register pairs for doubles */
1541 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
1548 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
1549 ST_FUNC
void gv2(int rc1
, int rc2
)
1553 /* generate more generic register first. But VT_JMP or VT_CMP
1554 values must be generated first in all cases to avoid possible
1556 v
= vtop
[0].r
& VT_VALMASK
;
1557 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
1562 /* test if reload is needed for first register */
1563 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
1573 /* test if reload is needed for first register */
1574 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
1580 #ifndef TCC_TARGET_ARM64
1581 /* wrapper around RC_FRET to return a register by type */
1582 static int rc_fret(int t
)
1584 #ifdef TCC_TARGET_X86_64
1585 if (t
== VT_LDOUBLE
) {
1593 /* wrapper around REG_FRET to return a register by type */
1594 static int reg_fret(int t
)
1596 #ifdef TCC_TARGET_X86_64
1597 if (t
== VT_LDOUBLE
) {
1605 /* expand 64bit on stack in two ints */
1606 ST_FUNC
void lexpand(void)
1609 u
= vtop
->type
.t
& (VT_DEFSIGN
| VT_UNSIGNED
);
1610 v
= vtop
->r
& (VT_VALMASK
| VT_LVAL
);
1611 if (v
== VT_CONST
) {
1614 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
1620 vtop
[0].r
= vtop
[-1].r2
;
1621 vtop
[0].r2
= vtop
[-1].r2
= VT_CONST
;
1623 vtop
[0].type
.t
= vtop
[-1].type
.t
= VT_INT
| u
;
1628 /* build a long long from two ints */
1629 static void lbuild(int t
)
1631 gv2(RC_INT
, RC_INT
);
1632 vtop
[-1].r2
= vtop
[0].r
;
1633 vtop
[-1].type
.t
= t
;
1638 /* convert stack entry to register and duplicate its value in another
1640 static void gv_dup(void)
1647 if ((t
& VT_BTYPE
) == VT_LLONG
) {
1648 if (t
& VT_BITFIELD
) {
1658 /* stack: H L L1 H1 */
1668 /* duplicate value */
1673 #ifdef TCC_TARGET_X86_64
1674 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1684 load(r1
, &sv
); /* move r to r1 */
1686 /* duplicates value */
1692 /* Generate value test
1694 * Generate a test for any value (jump, comparison and integers) */
1695 ST_FUNC
int gvtst(int inv
, int t
)
1697 int v
= vtop
->r
& VT_VALMASK
;
1698 if (v
!= VT_CMP
&& v
!= VT_JMP
&& v
!= VT_JMPI
) {
1702 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1703 /* constant jmp optimization */
1704 if ((vtop
->c
.i
!= 0) != inv
)
1709 return gtst(inv
, t
);
1713 /* generate CPU independent (unsigned) long long operations */
1714 static void gen_opl(int op
)
1716 int t
, a
, b
, op1
, c
, i
;
1718 unsigned short reg_iret
= REG_IRET
;
1719 unsigned short reg_lret
= REG_LRET
;
1725 func
= TOK___divdi3
;
1728 func
= TOK___udivdi3
;
1731 func
= TOK___moddi3
;
1734 func
= TOK___umoddi3
;
1741 /* call generic long long function */
1742 vpush_global_sym(&func_old_type
, func
);
1747 vtop
->r2
= reg_lret
;
1755 //pv("gen_opl A",0,2);
1761 /* stack: L1 H1 L2 H2 */
1766 vtop
[-2] = vtop
[-3];
1769 /* stack: H1 H2 L1 L2 */
1770 //pv("gen_opl B",0,4);
1776 /* stack: H1 H2 L1 L2 ML MH */
1779 /* stack: ML MH H1 H2 L1 L2 */
1783 /* stack: ML MH H1 L2 H2 L1 */
1788 /* stack: ML MH M1 M2 */
1791 } else if (op
== '+' || op
== '-') {
1792 /* XXX: add non carry method too (for MIPS or alpha) */
1798 /* stack: H1 H2 (L1 op L2) */
1801 gen_op(op1
+ 1); /* TOK_xxxC2 */
1804 /* stack: H1 H2 (L1 op L2) */
1807 /* stack: (L1 op L2) H1 H2 */
1809 /* stack: (L1 op L2) (H1 op H2) */
1817 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1818 t
= vtop
[-1].type
.t
;
1822 /* stack: L H shift */
1824 /* constant: simpler */
1825 /* NOTE: all comments are for SHL. the other cases are
1826 done by swapping words */
1837 if (op
!= TOK_SAR
) {
1870 /* XXX: should provide a faster fallback on x86 ? */
1873 func
= TOK___ashrdi3
;
1876 func
= TOK___lshrdi3
;
1879 func
= TOK___ashldi3
;
1885 /* compare operations */
1891 /* stack: L1 H1 L2 H2 */
1893 vtop
[-1] = vtop
[-2];
1895 /* stack: L1 L2 H1 H2 */
1898 /* when values are equal, we need to compare low words. since
1899 the jump is inverted, we invert the test too. */
1902 else if (op1
== TOK_GT
)
1904 else if (op1
== TOK_ULT
)
1906 else if (op1
== TOK_UGT
)
1916 /* generate non equal test */
1922 /* compare low. Always unsigned */
1926 else if (op1
== TOK_LE
)
1928 else if (op1
== TOK_GT
)
1930 else if (op1
== TOK_GE
)
1941 static uint64_t gen_opic_sdiv(uint64_t a
, uint64_t b
)
1943 uint64_t x
= (a
>> 63 ? -a
: a
) / (b
>> 63 ? -b
: b
);
1944 return (a
^ b
) >> 63 ? -x
: x
;
1947 static int gen_opic_lt(uint64_t a
, uint64_t b
)
1949 return (a
^ (uint64_t)1 << 63) < (b
^ (uint64_t)1 << 63);
1952 /* handle integer constant optimizations and various machine
1954 static void gen_opic(int op
)
1956 SValue
*v1
= vtop
- 1;
1958 int t1
= v1
->type
.t
& VT_BTYPE
;
1959 int t2
= v2
->type
.t
& VT_BTYPE
;
1960 int c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1961 int c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1962 uint64_t l1
= c1
? v1
->c
.i
: 0;
1963 uint64_t l2
= c2
? v2
->c
.i
: 0;
1964 int shm
= (t1
== VT_LLONG
) ? 63 : 31;
1966 if (t1
!= VT_LLONG
&& (PTR_SIZE
!= 8 || t1
!= VT_PTR
))
1967 l1
= ((uint32_t)l1
|
1968 (v1
->type
.t
& VT_UNSIGNED
? 0 : -(l1
& 0x80000000)));
1969 if (t2
!= VT_LLONG
&& (PTR_SIZE
!= 8 || t2
!= VT_PTR
))
1970 l2
= ((uint32_t)l2
|
1971 (v2
->type
.t
& VT_UNSIGNED
? 0 : -(l2
& 0x80000000)));
1975 case '+': l1
+= l2
; break;
1976 case '-': l1
-= l2
; break;
1977 case '&': l1
&= l2
; break;
1978 case '^': l1
^= l2
; break;
1979 case '|': l1
|= l2
; break;
1980 case '*': l1
*= l2
; break;
1987 /* if division by zero, generate explicit division */
1990 tcc_error("division by zero in constant");
1994 default: l1
= gen_opic_sdiv(l1
, l2
); break;
1995 case '%': l1
= l1
- l2
* gen_opic_sdiv(l1
, l2
); break;
1996 case TOK_UDIV
: l1
= l1
/ l2
; break;
1997 case TOK_UMOD
: l1
= l1
% l2
; break;
2000 case TOK_SHL
: l1
<<= (l2
& shm
); break;
2001 case TOK_SHR
: l1
>>= (l2
& shm
); break;
2003 l1
= (l1
>> 63) ? ~(~l1
>> (l2
& shm
)) : l1
>> (l2
& shm
);
2006 case TOK_ULT
: l1
= l1
< l2
; break;
2007 case TOK_UGE
: l1
= l1
>= l2
; break;
2008 case TOK_EQ
: l1
= l1
== l2
; break;
2009 case TOK_NE
: l1
= l1
!= l2
; break;
2010 case TOK_ULE
: l1
= l1
<= l2
; break;
2011 case TOK_UGT
: l1
= l1
> l2
; break;
2012 case TOK_LT
: l1
= gen_opic_lt(l1
, l2
); break;
2013 case TOK_GE
: l1
= !gen_opic_lt(l1
, l2
); break;
2014 case TOK_LE
: l1
= !gen_opic_lt(l2
, l1
); break;
2015 case TOK_GT
: l1
= gen_opic_lt(l2
, l1
); break;
2017 case TOK_LAND
: l1
= l1
&& l2
; break;
2018 case TOK_LOR
: l1
= l1
|| l2
; break;
2022 if (t1
!= VT_LLONG
&& (PTR_SIZE
!= 8 || t1
!= VT_PTR
))
2023 l1
= ((uint32_t)l1
|
2024 (v1
->type
.t
& VT_UNSIGNED
? 0 : -(l1
& 0x80000000)));
2028 /* if commutative ops, put c2 as constant */
2029 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
2030 op
== '|' || op
== '*')) {
2032 c2
= c1
; //c = c1, c1 = c2, c2 = c;
2033 l2
= l1
; //l = l1, l1 = l2, l2 = l;
2035 if (!const_wanted
&&
2037 (op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
)) ||
2038 (l1
== -1 && op
== TOK_SAR
))) {
2039 /* treat (0 << x), (0 >> x) and (-1 >> x) as constant */
2041 } else if (!const_wanted
&&
2042 c2
&& ((l2
== 0 && (op
== '&' || op
== '*')) ||
2044 (l2
== -1 || (l2
== 0xFFFFFFFF && t2
!= VT_LLONG
))) ||
2045 (l2
== 1 && (op
== '%' || op
== TOK_UMOD
)))) {
2046 /* treat (x & 0), (x * 0), (x | -1) and (x % 1) as constant */
2051 } else if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
2054 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
2055 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
2058 (l2
== -1 || (l2
== 0xFFFFFFFF && t2
!= VT_LLONG
))))) {
2059 /* filter out NOP operations like x*1, x-0, x&-1... */
2061 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
2062 /* try to use shifts instead of muls or divs */
2063 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
2072 else if (op
== TOK_PDIV
)
2078 } else if (c2
&& (op
== '+' || op
== '-') &&
2079 (((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
))
2080 || (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
2081 /* symbol + constant case */
2085 /* The backends can't always deal with addends to symbols
2086 larger than +-1<<31. Don't construct such. */
2093 /* call low level op generator */
2094 if (t1
== VT_LLONG
|| t2
== VT_LLONG
||
2095 (PTR_SIZE
== 8 && (t1
== VT_PTR
|| t2
== VT_PTR
)))
2103 /* generate a floating point operation with constant propagation */
2104 static void gen_opif(int op
)
2108 #if defined _MSC_VER && defined _AMD64_
2109 /* avoid bad optimization with f1 -= f2 for f1:-0.0, f2:0.0 */
2116 /* currently, we cannot do computations with forward symbols */
2117 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
2118 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
2120 if (v1
->type
.t
== VT_FLOAT
) {
2123 } else if (v1
->type
.t
== VT_DOUBLE
) {
2131 /* NOTE: we only do constant propagation if finite number (not
2132 NaN or infinity) (ANSI spec) */
2133 if (!ieee_finite(f1
) || !ieee_finite(f2
))
2137 case '+': f1
+= f2
; break;
2138 case '-': f1
-= f2
; break;
2139 case '*': f1
*= f2
; break;
2142 /* If not in initializer we need to potentially generate
2143 FP exceptions at runtime, otherwise we want to fold. */
2149 /* XXX: also handles tests ? */
2153 /* XXX: overflow test ? */
2154 if (v1
->type
.t
== VT_FLOAT
) {
2156 } else if (v1
->type
.t
== VT_DOUBLE
) {
2168 static int pointed_size(CType
*type
)
2171 return type_size(pointed_type(type
), &align
);
2174 static void vla_runtime_pointed_size(CType
*type
)
2177 vla_runtime_type_size(pointed_type(type
), &align
);
2180 static inline int is_null_pointer(SValue
*p
)
2182 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
2184 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& (uint32_t)p
->c
.i
== 0) ||
2185 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.i
== 0) ||
2186 ((p
->type
.t
& VT_BTYPE
) == VT_PTR
&&
2187 (PTR_SIZE
== 4 ? (uint32_t)p
->c
.i
== 0 : p
->c
.i
== 0) &&
2188 ((pointed_type(&p
->type
)->t
& VT_BTYPE
) == VT_VOID
) &&
2189 0 == (pointed_type(&p
->type
)->t
& (VT_CONSTANT
| VT_VOLATILE
))
2192 static inline int is_integer_btype(int bt
)
2194 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
2195 bt
== VT_INT
|| bt
== VT_LLONG
);
2198 /* check types for comparison or subtraction of pointers */
2199 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
2201 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
2204 /* null pointers are accepted for all comparisons as gcc */
2205 if (is_null_pointer(p1
) || is_null_pointer(p2
))
2209 bt1
= type1
->t
& VT_BTYPE
;
2210 bt2
= type2
->t
& VT_BTYPE
;
2211 /* accept comparison between pointer and integer with a warning */
2212 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
2213 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
2214 tcc_warning("comparison between pointer and integer");
2218 /* both must be pointers or implicit function pointers */
2219 if (bt1
== VT_PTR
) {
2220 type1
= pointed_type(type1
);
2221 } else if (bt1
!= VT_FUNC
)
2222 goto invalid_operands
;
2224 if (bt2
== VT_PTR
) {
2225 type2
= pointed_type(type2
);
2226 } else if (bt2
!= VT_FUNC
) {
2228 tcc_error("invalid operands to binary %s", get_tok_str(op
, NULL
));
2230 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
2231 (type2
->t
& VT_BTYPE
) == VT_VOID
)
2235 tmp_type1
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2236 tmp_type2
.t
&= ~(VT_DEFSIGN
| VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
2237 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
2238 /* gcc-like error if '-' is used */
2240 goto invalid_operands
;
2242 tcc_warning("comparison of distinct pointer types lacks a cast");
2246 /* generic gen_op: handles types problems */
2247 ST_FUNC
void gen_op(int op
)
2249 int u
, t1
, t2
, bt1
, bt2
, t
;
2253 t1
= vtop
[-1].type
.t
;
2254 t2
= vtop
[0].type
.t
;
2255 bt1
= t1
& VT_BTYPE
;
2256 bt2
= t2
& VT_BTYPE
;
2258 if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
2259 tcc_error("operation on a struct");
2260 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
2261 if (bt2
== VT_FUNC
) {
2262 mk_pointer(&vtop
->type
);
2265 if (bt1
== VT_FUNC
) {
2267 mk_pointer(&vtop
->type
);
2272 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
2273 /* at least one operand is a pointer */
2274 /* relational op: must be both pointers */
2275 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
2276 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
2277 /* pointers are handled are unsigned */
2279 t
= VT_LLONG
| VT_UNSIGNED
;
2281 t
= VT_INT
| VT_UNSIGNED
;
2285 /* if both pointers, then it must be the '-' op */
2286 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
2288 tcc_error("cannot use pointers here");
2289 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
2290 /* XXX: check that types are compatible */
2291 if (vtop
[-1].type
.t
& VT_VLA
) {
2292 vla_runtime_pointed_size(&vtop
[-1].type
);
2294 vpushi(pointed_size(&vtop
[-1].type
));
2298 vtop
->type
.t
= ptrdiff_type
.t
;
2302 /* exactly one pointer : must be '+' or '-'. */
2303 if (op
!= '-' && op
!= '+')
2304 tcc_error("cannot use pointers here");
2305 /* Put pointer as first operand */
2306 if (bt2
== VT_PTR
) {
2308 t
= t1
, t1
= t2
, t2
= t
;
2311 if ((vtop
[0].type
.t
& VT_BTYPE
) == VT_LLONG
)
2312 /* XXX: truncate here because gen_opl can't handle ptr + long long */
2315 type1
= vtop
[-1].type
;
2316 type1
.t
&= ~VT_ARRAY
;
2317 if (vtop
[-1].type
.t
& VT_VLA
)
2318 vla_runtime_pointed_size(&vtop
[-1].type
);
2320 u
= pointed_size(&vtop
[-1].type
);
2322 tcc_error("unknown array element size");
2326 /* XXX: cast to int ? (long long case) */
2332 /* #ifdef CONFIG_TCC_BCHECK
2333 The main reason to removing this code:
2340 fprintf(stderr, "v+i-j = %p\n", v+i-j);
2341 fprintf(stderr, "v+(i-j) = %p\n", v+(i-j));
2343 When this code is on. then the output looks like
2345 v+(i-j) = 0xbff84000
2347 /* if evaluating constant expression, no code should be
2348 generated, so no bound check */
2349 if (tcc_state
->do_bounds_check
&& !const_wanted
) {
2350 /* if bounded pointers, we generate a special code to
2357 gen_bounded_ptr_add();
2363 /* put again type if gen_opic() swaped operands */
2366 } else if (is_float(bt1
) || is_float(bt2
)) {
2367 /* compute bigger type and do implicit casts */
2368 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
2370 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
2375 /* floats can only be used for a few operations */
2376 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
2377 (op
< TOK_ULT
|| op
> TOK_GT
))
2378 tcc_error("invalid operands for binary operation");
2380 } else if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
) {
2381 t
= bt1
== VT_LLONG
? VT_LLONG
: VT_INT
;
2382 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (t
| VT_UNSIGNED
))
2384 t
|= (VT_LONG
& t1
);
2386 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
2387 /* cast to biggest op */
2388 t
= VT_LLONG
| VT_LONG
;
2389 if (bt1
== VT_LLONG
)
2391 if (bt2
== VT_LLONG
)
2393 /* convert to unsigned if it does not fit in a long long */
2394 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_LLONG
| VT_UNSIGNED
) ||
2395 (t2
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_LLONG
| VT_UNSIGNED
))
2399 /* integer operations */
2400 t
= VT_INT
| (VT_LONG
& (t1
| t2
));
2401 /* convert to unsigned if it does not fit in an integer */
2402 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_INT
| VT_UNSIGNED
) ||
2403 (t2
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_INT
| VT_UNSIGNED
))
2406 /* XXX: currently, some unsigned operations are explicit, so
2407 we modify them here */
2408 if (t
& VT_UNSIGNED
) {
2415 else if (op
== TOK_LT
)
2417 else if (op
== TOK_GT
)
2419 else if (op
== TOK_LE
)
2421 else if (op
== TOK_GE
)
2429 /* special case for shifts and long long: we keep the shift as
2431 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
2438 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
2439 /* relational op: the result is an int */
2440 vtop
->type
.t
= VT_INT
;
2445 // Make sure that we have converted to an rvalue:
2446 if (vtop
->r
& VT_LVAL
)
2447 gv(is_float(vtop
->type
.t
& VT_BTYPE
) ? RC_FLOAT
: RC_INT
);
2450 #ifndef TCC_TARGET_ARM
2451 /* generic itof for unsigned long long case */
2452 static void gen_cvt_itof1(int t
)
2454 #ifdef TCC_TARGET_ARM64
2457 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
2458 (VT_LLONG
| VT_UNSIGNED
)) {
2461 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
2462 #if LDOUBLE_SIZE != 8
2463 else if (t
== VT_LDOUBLE
)
2464 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
2467 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
2471 vtop
->r
= reg_fret(t
);
2479 /* generic ftoi for unsigned long long case */
2480 static void gen_cvt_ftoi1(int t
)
2482 #ifdef TCC_TARGET_ARM64
2487 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
2488 /* not handled natively */
2489 st
= vtop
->type
.t
& VT_BTYPE
;
2491 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
2492 #if LDOUBLE_SIZE != 8
2493 else if (st
== VT_LDOUBLE
)
2494 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
2497 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
2502 vtop
->r2
= REG_LRET
;
2509 /* force char or short cast */
2510 static void force_charshort_cast(int t
)
2514 /* cannot cast static initializers */
2515 if (STATIC_DATA_WANTED
)
2519 /* XXX: add optimization if lvalue : just change type and offset */
2524 if (t
& VT_UNSIGNED
) {
2525 vpushi((1 << bits
) - 1);
2528 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
)
2534 /* result must be signed or the SAR is converted to an SHL
2535 This was not the case when "t" was a signed short
2536 and the last value on the stack was an unsigned int */
2537 vtop
->type
.t
&= ~VT_UNSIGNED
;
2543 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
2544 static void gen_cast_s(int t
)
2552 static void gen_cast(CType
*type
)
2554 int sbt
, dbt
, sf
, df
, c
, p
;
2556 /* special delayed cast for char/short */
2557 /* XXX: in some cases (multiple cascaded casts), it may still
2559 if (vtop
->r
& VT_MUSTCAST
) {
2560 vtop
->r
&= ~VT_MUSTCAST
;
2561 force_charshort_cast(vtop
->type
.t
);
2564 /* bitfields first get cast to ints */
2565 if (vtop
->type
.t
& VT_BITFIELD
) {
2569 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
2570 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
2575 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
2576 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
2577 #if !defined TCC_IS_NATIVE && !defined TCC_IS_NATIVE_387
2578 c
&= dbt
!= VT_LDOUBLE
;
2581 /* constant case: we can do it now */
2582 /* XXX: in ISOC, cannot do it if error in convert */
2583 if (sbt
== VT_FLOAT
)
2584 vtop
->c
.ld
= vtop
->c
.f
;
2585 else if (sbt
== VT_DOUBLE
)
2586 vtop
->c
.ld
= vtop
->c
.d
;
2589 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
2590 if ((sbt
& VT_UNSIGNED
) || !(vtop
->c
.i
>> 63))
2591 vtop
->c
.ld
= vtop
->c
.i
;
2593 vtop
->c
.ld
= -(long double)-vtop
->c
.i
;
2595 if ((sbt
& VT_UNSIGNED
) || !(vtop
->c
.i
>> 31))
2596 vtop
->c
.ld
= (uint32_t)vtop
->c
.i
;
2598 vtop
->c
.ld
= -(long double)-(uint32_t)vtop
->c
.i
;
2601 if (dbt
== VT_FLOAT
)
2602 vtop
->c
.f
= (float)vtop
->c
.ld
;
2603 else if (dbt
== VT_DOUBLE
)
2604 vtop
->c
.d
= (double)vtop
->c
.ld
;
2605 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
2606 vtop
->c
.i
= vtop
->c
.ld
;
2607 } else if (sf
&& dbt
== VT_BOOL
) {
2608 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
2611 vtop
->c
.i
= vtop
->c
.ld
;
2612 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
2614 else if (sbt
& VT_UNSIGNED
)
2615 vtop
->c
.i
= (uint32_t)vtop
->c
.i
;
2617 else if (sbt
== VT_PTR
)
2620 else if (sbt
!= VT_LLONG
)
2621 vtop
->c
.i
= ((uint32_t)vtop
->c
.i
|
2622 -(vtop
->c
.i
& 0x80000000));
2624 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
2626 else if (dbt
== VT_BOOL
)
2627 vtop
->c
.i
= (vtop
->c
.i
!= 0);
2629 else if (dbt
== VT_PTR
)
2632 else if (dbt
!= VT_LLONG
) {
2633 uint32_t m
= ((dbt
& VT_BTYPE
) == VT_BYTE
? 0xff :
2634 (dbt
& VT_BTYPE
) == VT_SHORT
? 0xffff :
2637 if (!(dbt
& VT_UNSIGNED
))
2638 vtop
->c
.i
|= -(vtop
->c
.i
& ((m
>> 1) + 1));
2641 } else if (p
&& dbt
== VT_BOOL
) {
2645 /* non constant case: generate code */
2647 /* convert from fp to fp */
2650 /* convert int to fp */
2653 /* convert fp to int */
2654 if (dbt
== VT_BOOL
) {
2658 /* we handle char/short/etc... with generic code */
2659 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
2660 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
2664 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
2665 /* additional cast for char/short... */
2671 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
2672 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
2673 /* scalar to long long */
2674 /* machine independent conversion */
2676 /* generate high word */
2677 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
2681 if (sbt
== VT_PTR
) {
2682 /* cast from pointer to int before we apply
2683 shift operation, which pointers don't support*/
2690 /* patch second register */
2691 vtop
[-1].r2
= vtop
->r
;
2695 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
||
2696 (dbt
& VT_BTYPE
) == VT_PTR
||
2697 (dbt
& VT_BTYPE
) == VT_FUNC
) {
2698 if ((sbt
& VT_BTYPE
) != VT_LLONG
&&
2699 (sbt
& VT_BTYPE
) != VT_PTR
&&
2700 (sbt
& VT_BTYPE
) != VT_FUNC
) {
2701 /* need to convert from 32bit to 64bit */
2703 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
2704 #if defined(TCC_TARGET_ARM64)
2706 #elif defined(TCC_TARGET_X86_64)
2708 /* x86_64 specific: movslq */
2710 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
2717 } else if (dbt
== VT_BOOL
) {
2718 /* scalar to bool */
2721 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
2722 (dbt
& VT_BTYPE
) == VT_SHORT
) {
2723 if (sbt
== VT_PTR
) {
2724 vtop
->type
.t
= VT_INT
;
2725 tcc_warning("nonportable conversion from pointer to char/short");
2727 force_charshort_cast(dbt
);
2728 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
2730 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
2732 /* from long long: just take low order word */
2737 vtop
->type
.t
|= VT_UNSIGNED
;
2741 /* if lvalue and single word type, nothing to do because
2742 the lvalue already contains the real type size (see
2743 VT_LVAL_xxx constants) */
2746 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
2747 /* if we are casting between pointer types,
2748 we must update the VT_LVAL_xxx size */
2749 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
2750 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
2753 vtop
->type
.t
&= ~ ( VT_CONSTANT
| VT_VOLATILE
| VT_ARRAY
);
2756 /* return type size as known at compile time. Put alignment at 'a' */
2757 ST_FUNC
int type_size(CType
*type
, int *a
)
2762 bt
= type
->t
& VT_BTYPE
;
2763 if (bt
== VT_STRUCT
) {
2768 } else if (bt
== VT_PTR
) {
2769 if (type
->t
& VT_ARRAY
) {
2773 ts
= type_size(&s
->type
, a
);
2775 if (ts
< 0 && s
->c
< 0)
2783 } else if (IS_ENUM(type
->t
) && type
->ref
->c
< 0) {
2784 return -1; /* incomplete enum */
2785 } else if (bt
== VT_LDOUBLE
) {
2787 return LDOUBLE_SIZE
;
2788 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
2789 #ifdef TCC_TARGET_I386
2790 #ifdef TCC_TARGET_PE
2795 #elif defined(TCC_TARGET_ARM)
2805 } else if (bt
== VT_INT
|| bt
== VT_FLOAT
) {
2808 } else if (bt
== VT_SHORT
) {
2811 } else if (bt
== VT_QLONG
|| bt
== VT_QFLOAT
) {
2815 /* char, void, function, _Bool */
2821 /* push type size as known at runtime time on top of value stack. Put
2823 ST_FUNC
void vla_runtime_type_size(CType
*type
, int *a
)
2825 if (type
->t
& VT_VLA
) {
2826 type_size(&type
->ref
->type
, a
);
2827 vset(&int_type
, VT_LOCAL
|VT_LVAL
, type
->ref
->c
);
2829 vpushi(type_size(type
, a
));
2833 static void vla_sp_restore(void) {
2834 if (vlas_in_scope
) {
2835 gen_vla_sp_restore(vla_sp_loc
);
2839 static void vla_sp_restore_root(void) {
2840 if (vlas_in_scope
) {
2841 gen_vla_sp_restore(vla_sp_root_loc
);
2845 /* return the pointed type of t */
2846 static inline CType
*pointed_type(CType
*type
)
2848 return &type
->ref
->type
;
2851 /* modify type so that its it is a pointer to type. */
2852 ST_FUNC
void mk_pointer(CType
*type
)
2855 s
= sym_push(SYM_FIELD
, type
, 0, -1);
2856 type
->t
= VT_PTR
| (type
->t
& VT_STORAGE
);
2860 /* compare function types. OLD functions match any new functions */
2861 static int is_compatible_func(CType
*type1
, CType
*type2
)
2867 if (!is_compatible_types(&s1
->type
, &s2
->type
))
2869 /* check func_call */
2870 if (s1
->f
.func_call
!= s2
->f
.func_call
)
2872 /* XXX: not complete */
2873 if (s1
->f
.func_type
== FUNC_OLD
|| s2
->f
.func_type
== FUNC_OLD
)
2875 if (s1
->f
.func_type
!= s2
->f
.func_type
)
2877 while (s1
!= NULL
) {
2880 if (!is_compatible_unqualified_types(&s1
->type
, &s2
->type
))
2890 /* return true if type1 and type2 are the same. If unqualified is
2891 true, qualifiers on the types are ignored.
2893 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
2897 t1
= type1
->t
& VT_TYPE
;
2898 t2
= type2
->t
& VT_TYPE
;
2900 /* strip qualifiers before comparing */
2901 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2902 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
2905 /* Default Vs explicit signedness only matters for char */
2906 if ((t1
& VT_BTYPE
) != VT_BYTE
) {
2910 /* XXX: bitfields ? */
2913 /* test more complicated cases */
2914 bt1
= t1
& (VT_BTYPE
| VT_ARRAY
);
2915 if (bt1
== VT_PTR
) {
2916 type1
= pointed_type(type1
);
2917 type2
= pointed_type(type2
);
2918 return is_compatible_types(type1
, type2
);
2919 } else if (bt1
& VT_ARRAY
) {
2920 return type1
->ref
->c
< 0 || type2
->ref
->c
< 0
2921 || type1
->ref
->c
== type2
->ref
->c
;
2922 } else if (bt1
== VT_STRUCT
) {
2923 return (type1
->ref
== type2
->ref
);
2924 } else if (bt1
== VT_FUNC
) {
2925 return is_compatible_func(type1
, type2
);
2926 } else if (IS_ENUM(type1
->t
) || IS_ENUM(type2
->t
)) {
2927 return type1
->ref
== type2
->ref
;
2933 /* return true if type1 and type2 are exactly the same (including
2936 static int is_compatible_types(CType
*type1
, CType
*type2
)
2938 return compare_types(type1
,type2
,0);
2941 /* return true if type1 and type2 are the same (ignoring qualifiers).
2943 static int is_compatible_unqualified_types(CType
*type1
, CType
*type2
)
2945 return compare_types(type1
,type2
,1);
2948 /* print a type. If 'varstr' is not NULL, then the variable is also
2949 printed in the type */
2951 /* XXX: add array and function pointers */
2952 static void type_to_str(char *buf
, int buf_size
,
2953 CType
*type
, const char *varstr
)
2965 pstrcat(buf
, buf_size
, "extern ");
2967 pstrcat(buf
, buf_size
, "static ");
2969 pstrcat(buf
, buf_size
, "typedef ");
2971 pstrcat(buf
, buf_size
, "inline ");
2972 if (t
& VT_VOLATILE
)
2973 pstrcat(buf
, buf_size
, "volatile ");
2974 if (t
& VT_CONSTANT
)
2975 pstrcat(buf
, buf_size
, "const ");
2977 if (((t
& VT_DEFSIGN
) && bt
== VT_BYTE
)
2978 || ((t
& VT_UNSIGNED
)
2979 && (bt
== VT_SHORT
|| bt
== VT_INT
|| bt
== VT_LLONG
)
2982 pstrcat(buf
, buf_size
, (t
& VT_UNSIGNED
) ? "unsigned " : "signed ");
2984 buf_size
-= strlen(buf
);
3019 tstr
= "long double";
3021 pstrcat(buf
, buf_size
, tstr
);
3028 pstrcat(buf
, buf_size
, tstr
);
3029 v
= type
->ref
->v
& ~SYM_STRUCT
;
3030 if (v
>= SYM_FIRST_ANOM
)
3031 pstrcat(buf
, buf_size
, "<anonymous>");
3033 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
3038 if (varstr
&& '*' == *varstr
) {
3039 pstrcat(buf1
, sizeof(buf1
), "(");
3040 pstrcat(buf1
, sizeof(buf1
), varstr
);
3041 pstrcat(buf1
, sizeof(buf1
), ")");
3043 pstrcat(buf1
, buf_size
, "(");
3045 while (sa
!= NULL
) {
3047 type_to_str(buf2
, sizeof(buf2
), &sa
->type
, NULL
);
3048 pstrcat(buf1
, sizeof(buf1
), buf2
);
3051 pstrcat(buf1
, sizeof(buf1
), ", ");
3053 if (s
->f
.func_type
== FUNC_ELLIPSIS
)
3054 pstrcat(buf1
, sizeof(buf1
), ", ...");
3055 pstrcat(buf1
, sizeof(buf1
), ")");
3056 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
3061 if (varstr
&& '*' == *varstr
)
3062 snprintf(buf1
, sizeof(buf1
), "(%s)[%d]", varstr
, s
->c
);
3064 snprintf(buf1
, sizeof(buf1
), "%s[%d]", varstr
? varstr
: "", s
->c
);
3065 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
3068 pstrcpy(buf1
, sizeof(buf1
), "*");
3069 if (t
& VT_CONSTANT
)
3070 pstrcat(buf1
, buf_size
, "const ");
3071 if (t
& VT_VOLATILE
)
3072 pstrcat(buf1
, buf_size
, "volatile ");
3074 pstrcat(buf1
, sizeof(buf1
), varstr
);
3075 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
3079 pstrcat(buf
, buf_size
, " ");
3080 pstrcat(buf
, buf_size
, varstr
);
3085 /* verify type compatibility to store vtop in 'dt' type, and generate
3087 static void gen_assign_cast(CType
*dt
)
3089 CType
*st
, *type1
, *type2
;
3090 char buf1
[256], buf2
[256];
3091 int dbt
, sbt
, qualwarn
, lvl
;
3093 st
= &vtop
->type
; /* source type */
3094 dbt
= dt
->t
& VT_BTYPE
;
3095 sbt
= st
->t
& VT_BTYPE
;
3096 if (sbt
== VT_VOID
|| dbt
== VT_VOID
) {
3097 if (sbt
== VT_VOID
&& dbt
== VT_VOID
)
3098 ; /* It is Ok if both are void */
3100 tcc_error("cannot cast from/to void");
3102 if (dt
->t
& VT_CONSTANT
)
3103 tcc_warning("assignment of read-only location");
3106 /* special cases for pointers */
3107 /* '0' can also be a pointer */
3108 if (is_null_pointer(vtop
))
3110 /* accept implicit pointer to integer cast with warning */
3111 if (is_integer_btype(sbt
)) {
3112 tcc_warning("assignment makes pointer from integer without a cast");
3115 type1
= pointed_type(dt
);
3117 type2
= pointed_type(st
);
3118 else if (sbt
== VT_FUNC
)
3119 type2
= st
; /* a function is implicitly a function pointer */
3122 if (is_compatible_types(type1
, type2
))
3124 for (qualwarn
= lvl
= 0;; ++lvl
) {
3125 if (((type2
->t
& VT_CONSTANT
) && !(type1
->t
& VT_CONSTANT
)) ||
3126 ((type2
->t
& VT_VOLATILE
) && !(type1
->t
& VT_VOLATILE
)))
3128 dbt
= type1
->t
& (VT_BTYPE
|VT_LONG
);
3129 sbt
= type2
->t
& (VT_BTYPE
|VT_LONG
);
3130 if (dbt
!= VT_PTR
|| sbt
!= VT_PTR
)
3132 type1
= pointed_type(type1
);
3133 type2
= pointed_type(type2
);
3135 if (!is_compatible_unqualified_types(type1
, type2
)) {
3136 if ((dbt
== VT_VOID
|| sbt
== VT_VOID
) && lvl
== 0) {
3137 /* void * can match anything */
3138 } else if (dbt
== sbt
3139 && is_integer_btype(sbt
& VT_BTYPE
)
3140 && IS_ENUM(type1
->t
) + IS_ENUM(type2
->t
)
3141 + !!((type1
->t
^ type2
->t
) & VT_UNSIGNED
) < 2) {
3142 /* Like GCC don't warn by default for merely changes
3143 in pointer target signedness. Do warn for different
3144 base types, though, in particular for unsigned enums
3145 and signed int targets. */
3147 tcc_warning("assignment from incompatible pointer type");
3152 tcc_warning("assignment discards qualifiers from pointer target type");
3158 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
3159 tcc_warning("assignment makes integer from pointer without a cast");
3160 } else if (sbt
== VT_STRUCT
) {
3161 goto case_VT_STRUCT
;
3163 /* XXX: more tests */
3167 if (!is_compatible_unqualified_types(dt
, st
)) {
3169 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
3170 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
3171 tcc_error("cannot cast '%s' to '%s'", buf1
, buf2
);
3178 /* store vtop in lvalue pushed on stack */
3179 ST_FUNC
void vstore(void)
3181 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
3183 ft
= vtop
[-1].type
.t
;
3184 sbt
= vtop
->type
.t
& VT_BTYPE
;
3185 dbt
= ft
& VT_BTYPE
;
3186 if ((((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
3187 (sbt
== VT_INT
&& dbt
== VT_SHORT
))
3188 && !(vtop
->type
.t
& VT_BITFIELD
)) {
3189 /* optimize char/short casts */
3190 delayed_cast
= VT_MUSTCAST
;
3191 vtop
->type
.t
= ft
& VT_TYPE
;
3192 /* XXX: factorize */
3193 if (ft
& VT_CONSTANT
)
3194 tcc_warning("assignment of read-only location");
3197 if (!(ft
& VT_BITFIELD
))
3198 gen_assign_cast(&vtop
[-1].type
);
3201 if (sbt
== VT_STRUCT
) {
3202 /* if structure, only generate pointer */
3203 /* structure assignment : generate memcpy */
3204 /* XXX: optimize if small size */
3205 size
= type_size(&vtop
->type
, &align
);
3209 vtop
->type
.t
= VT_PTR
;
3212 /* address of memcpy() */
3215 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
3216 else if(!(align
& 3))
3217 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
3220 /* Use memmove, rather than memcpy, as dest and src may be same: */
3221 vpush_global_sym(&func_old_type
, TOK_memmove
);
3226 vtop
->type
.t
= VT_PTR
;
3232 /* leave source on stack */
3233 } else if (ft
& VT_BITFIELD
) {
3234 /* bitfield store handling */
3236 /* save lvalue as expression result (example: s.b = s.a = n;) */
3237 vdup(), vtop
[-1] = vtop
[-2];
3239 bit_pos
= BIT_POS(ft
);
3240 bit_size
= BIT_SIZE(ft
);
3241 /* remove bit field info to avoid loops */
3242 vtop
[-1].type
.t
= ft
& ~VT_STRUCT_MASK
;
3244 if ((ft
& VT_BTYPE
) == VT_BOOL
) {
3245 gen_cast(&vtop
[-1].type
);
3246 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
3249 r
= adjust_bf(vtop
- 1, bit_pos
, bit_size
);
3250 if (r
== VT_STRUCT
) {
3251 gen_cast_s((ft
& VT_BTYPE
) == VT_LLONG
? VT_LLONG
: VT_INT
);
3252 store_packed_bf(bit_pos
, bit_size
);
3254 unsigned long long mask
= (1ULL << bit_size
) - 1;
3255 if ((ft
& VT_BTYPE
) != VT_BOOL
) {
3257 if ((vtop
[-1].type
.t
& VT_BTYPE
) == VT_LLONG
)
3260 vpushi((unsigned)mask
);
3267 /* duplicate destination */
3270 /* load destination, mask and or with source */
3271 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
)
3272 vpushll(~(mask
<< bit_pos
));
3274 vpushi(~((unsigned)mask
<< bit_pos
));
3279 /* ... and discard */
3282 } else if (dbt
== VT_VOID
) {
3285 #ifdef CONFIG_TCC_BCHECK
3286 /* bound check case */
3287 if (vtop
[-1].r
& VT_MUSTBOUND
) {
3296 #ifdef TCC_TARGET_X86_64
3297 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
3299 } else if ((ft
& VT_BTYPE
) == VT_QFLOAT
) {
3304 r
= gv(rc
); /* generate value */
3305 /* if lvalue was saved on stack, must read it */
3306 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
3308 t
= get_reg(RC_INT
);
3314 sv
.r
= VT_LOCAL
| VT_LVAL
;
3315 sv
.c
.i
= vtop
[-1].c
.i
;
3317 vtop
[-1].r
= t
| VT_LVAL
;
3319 /* two word case handling : store second register at word + 4 (or +8 for x86-64) */
3321 if (((ft
& VT_BTYPE
) == VT_QLONG
) || ((ft
& VT_BTYPE
) == VT_QFLOAT
)) {
3322 int addr_type
= VT_LLONG
, load_size
= 8, load_type
= ((vtop
->type
.t
& VT_BTYPE
) == VT_QLONG
) ? VT_LLONG
: VT_DOUBLE
;
3324 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
3325 int addr_type
= VT_INT
, load_size
= 4, load_type
= VT_INT
;
3327 vtop
[-1].type
.t
= load_type
;
3330 /* convert to int to increment easily */
3331 vtop
->type
.t
= addr_type
;
3337 vtop
[-1].type
.t
= load_type
;
3338 /* XXX: it works because r2 is spilled last ! */
3339 store(vtop
->r2
, vtop
- 1);
3345 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
3346 vtop
->r
|= delayed_cast
;
3350 /* post defines POST/PRE add. c is the token ++ or -- */
3351 ST_FUNC
void inc(int post
, int c
)
3354 vdup(); /* save lvalue */
3356 gv_dup(); /* duplicate value */
3361 vpushi(c
- TOK_MID
);
3363 vstore(); /* store value */
3365 vpop(); /* if post op, return saved value */
3368 ST_FUNC
void parse_mult_str (CString
*astr
, const char *msg
)
3370 /* read the string */
3374 while (tok
== TOK_STR
) {
3375 /* XXX: add \0 handling too ? */
3376 cstr_cat(astr
, tokc
.str
.data
, -1);
3379 cstr_ccat(astr
, '\0');
3382 /* If I is >= 1 and a power of two, returns log2(i)+1.
3383 If I is 0 returns 0. */
3384 static int exact_log2p1(int i
)
3389 for (ret
= 1; i
>= 1 << 8; ret
+= 8)
3400 /* Parse __attribute__((...)) GNUC extension. */
3401 static void parse_attribute(AttributeDef
*ad
)
3407 if (tok
!= TOK_ATTRIBUTE1
&& tok
!= TOK_ATTRIBUTE2
)
3412 while (tok
!= ')') {
3413 if (tok
< TOK_IDENT
)
3414 expect("attribute name");
3426 tcc_warning("implicit declaration of function '%s'",
3427 get_tok_str(tok
, &tokc
));
3428 s
= external_global_sym(tok
, &func_old_type
);
3430 ad
->cleanup_func
= s
;
3438 parse_mult_str(&astr
, "section name");
3439 ad
->section
= find_section(tcc_state
, (char *)astr
.data
);
3446 parse_mult_str(&astr
, "alias(\"target\")");
3447 ad
->alias_target
= /* save string as token, for later */
3448 tok_alloc((char*)astr
.data
, astr
.size
-1)->tok
;
3452 case TOK_VISIBILITY1
:
3453 case TOK_VISIBILITY2
:
3455 parse_mult_str(&astr
,
3456 "visibility(\"default|hidden|internal|protected\")");
3457 if (!strcmp (astr
.data
, "default"))
3458 ad
->a
.visibility
= STV_DEFAULT
;
3459 else if (!strcmp (astr
.data
, "hidden"))
3460 ad
->a
.visibility
= STV_HIDDEN
;
3461 else if (!strcmp (astr
.data
, "internal"))
3462 ad
->a
.visibility
= STV_INTERNAL
;
3463 else if (!strcmp (astr
.data
, "protected"))
3464 ad
->a
.visibility
= STV_PROTECTED
;
3466 expect("visibility(\"default|hidden|internal|protected\")");
3475 if (n
<= 0 || (n
& (n
- 1)) != 0)
3476 tcc_error("alignment must be a positive power of two");
3481 ad
->a
.aligned
= exact_log2p1(n
);
3482 if (n
!= 1 << (ad
->a
.aligned
- 1))
3483 tcc_error("alignment of %d is larger than implemented", n
);
3495 /* currently, no need to handle it because tcc does not
3496 track unused objects */
3500 /* currently, no need to handle it because tcc does not
3501 track unused objects */
3506 ad
->f
.func_call
= FUNC_CDECL
;
3511 ad
->f
.func_call
= FUNC_STDCALL
;
3513 #ifdef TCC_TARGET_I386
3523 ad
->f
.func_call
= FUNC_FASTCALL1
+ n
- 1;
3529 ad
->f
.func_call
= FUNC_FASTCALLW
;
3536 ad
->attr_mode
= VT_LLONG
+ 1;
3539 ad
->attr_mode
= VT_BYTE
+ 1;
3542 ad
->attr_mode
= VT_SHORT
+ 1;
3546 ad
->attr_mode
= VT_INT
+ 1;
3549 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok
, NULL
));
3556 ad
->a
.dllexport
= 1;
3558 case TOK_NODECORATE
:
3559 ad
->a
.nodecorate
= 1;
3562 ad
->a
.dllimport
= 1;
3565 if (tcc_state
->warn_unsupported
)
3566 tcc_warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
3567 /* skip parameters */
3569 int parenthesis
= 0;
3573 else if (tok
== ')')
3576 } while (parenthesis
&& tok
!= -1);
3589 static Sym
* find_field (CType
*type
, int v
, int *cumofs
)
3593 while ((s
= s
->next
) != NULL
) {
3594 if ((s
->v
& SYM_FIELD
) &&
3595 (s
->type
.t
& VT_BTYPE
) == VT_STRUCT
&&
3596 (s
->v
& ~SYM_FIELD
) >= SYM_FIRST_ANOM
) {
3597 Sym
*ret
= find_field (&s
->type
, v
, cumofs
);
3609 static void struct_layout(CType
*type
, AttributeDef
*ad
)
3611 int size
, align
, maxalign
, offset
, c
, bit_pos
, bit_size
;
3612 int packed
, a
, bt
, prevbt
, prev_bit_size
;
3613 int pcc
= !tcc_state
->ms_bitfields
;
3614 int pragma_pack
= *tcc_state
->pack_stack_ptr
;
3621 prevbt
= VT_STRUCT
; /* make it never match */
3626 for (f
= type
->ref
->next
; f
; f
= f
->next
) {
3627 if (f
->type
.t
& VT_BITFIELD
)
3628 bit_size
= BIT_SIZE(f
->type
.t
);
3631 size
= type_size(&f
->type
, &align
);
3632 a
= f
->a
.aligned
? 1 << (f
->a
.aligned
- 1) : 0;
3635 if (pcc
&& bit_size
== 0) {
3636 /* in pcc mode, packing does not affect zero-width bitfields */
3639 /* in pcc mode, attribute packed overrides if set. */
3640 if (pcc
&& (f
->a
.packed
|| ad
->a
.packed
))
3643 /* pragma pack overrides align if lesser and packs bitfields always */
3646 if (pragma_pack
< align
)
3647 align
= pragma_pack
;
3648 /* in pcc mode pragma pack also overrides individual align */
3649 if (pcc
&& pragma_pack
< a
)
3653 /* some individual align was specified */
3657 if (type
->ref
->type
.t
== VT_UNION
) {
3658 if (pcc
&& bit_size
>= 0)
3659 size
= (bit_size
+ 7) >> 3;
3664 } else if (bit_size
< 0) {
3666 c
+= (bit_pos
+ 7) >> 3;
3667 c
= (c
+ align
- 1) & -align
;
3676 /* A bit-field. Layout is more complicated. There are two
3677 options: PCC (GCC) compatible and MS compatible */
3679 /* In PCC layout a bit-field is placed adjacent to the
3680 preceding bit-fields, except if:
3682 - an individual alignment was given
3683 - it would overflow its base type container and
3684 there is no packing */
3685 if (bit_size
== 0) {
3687 c
= (c
+ ((bit_pos
+ 7) >> 3) + align
- 1) & -align
;
3689 } else if (f
->a
.aligned
) {
3691 } else if (!packed
) {
3693 int ofs
= ((c
* 8 + bit_pos
) % a8
+ bit_size
+ a8
- 1) / a8
;
3694 if (ofs
> size
/ align
)
3698 /* in pcc mode, long long bitfields have type int if they fit */
3699 if (size
== 8 && bit_size
<= 32)
3700 f
->type
.t
= (f
->type
.t
& ~VT_BTYPE
) | VT_INT
, size
= 4;
3702 while (bit_pos
>= align
* 8)
3703 c
+= align
, bit_pos
-= align
* 8;
3706 /* In PCC layout named bit-fields influence the alignment
3707 of the containing struct using the base types alignment,
3708 except for packed fields (which here have correct align). */
3709 if (f
->v
& SYM_FIRST_ANOM
3710 // && bit_size // ??? gcc on ARM/rpi does that
3715 bt
= f
->type
.t
& VT_BTYPE
;
3716 if ((bit_pos
+ bit_size
> size
* 8)
3717 || (bit_size
> 0) == (bt
!= prevbt
)
3719 c
= (c
+ align
- 1) & -align
;
3722 /* In MS bitfield mode a bit-field run always uses
3723 at least as many bits as the underlying type.
3724 To start a new run it's also required that this
3725 or the last bit-field had non-zero width. */
3726 if (bit_size
|| prev_bit_size
)
3729 /* In MS layout the records alignment is normally
3730 influenced by the field, except for a zero-width
3731 field at the start of a run (but by further zero-width
3732 fields it is again). */
3733 if (bit_size
== 0 && prevbt
!= bt
)
3736 prev_bit_size
= bit_size
;
3739 f
->type
.t
= (f
->type
.t
& ~(0x3f << VT_STRUCT_SHIFT
))
3740 | (bit_pos
<< VT_STRUCT_SHIFT
);
3741 bit_pos
+= bit_size
;
3743 if (align
> maxalign
)
3747 printf("set field %s offset %-2d size %-2d align %-2d",
3748 get_tok_str(f
->v
& ~SYM_FIELD
, NULL
), offset
, size
, align
);
3749 if (f
->type
.t
& VT_BITFIELD
) {
3750 printf(" pos %-2d bits %-2d",
3763 c
+= (bit_pos
+ 7) >> 3;
3765 /* store size and alignment */
3766 a
= bt
= ad
->a
.aligned
? 1 << (ad
->a
.aligned
- 1) : 1;
3770 if (pragma_pack
&& pragma_pack
< maxalign
&& 0 == pcc
) {
3771 /* can happen if individual align for some member was given. In
3772 this case MSVC ignores maxalign when aligning the size */
3777 c
= (c
+ a
- 1) & -a
;
3781 printf("struct size %-2d align %-2d\n\n", c
, a
), fflush(stdout
);
3784 /* check whether we can access bitfields by their type */
3785 for (f
= type
->ref
->next
; f
; f
= f
->next
) {
3789 if (0 == (f
->type
.t
& VT_BITFIELD
))
3793 bit_size
= BIT_SIZE(f
->type
.t
);
3796 bit_pos
= BIT_POS(f
->type
.t
);
3797 size
= type_size(&f
->type
, &align
);
3798 if (bit_pos
+ bit_size
<= size
* 8 && f
->c
+ size
<= c
)
3801 /* try to access the field using a different type */
3802 c0
= -1, s
= align
= 1;
3804 px
= f
->c
* 8 + bit_pos
;
3805 cx
= (px
>> 3) & -align
;
3806 px
= px
- (cx
<< 3);
3809 s
= (px
+ bit_size
+ 7) >> 3;
3819 s
= type_size(&t
, &align
);
3823 if (px
+ bit_size
<= s
* 8 && cx
+ s
<= c
) {
3824 /* update offset and bit position */
3827 f
->type
.t
= (f
->type
.t
& ~(0x3f << VT_STRUCT_SHIFT
))
3828 | (bit_pos
<< VT_STRUCT_SHIFT
);
3832 printf("FIX field %s offset %-2d size %-2d align %-2d "
3833 "pos %-2d bits %-2d\n",
3834 get_tok_str(f
->v
& ~SYM_FIELD
, NULL
),
3835 cx
, s
, align
, px
, bit_size
);
3838 /* fall back to load/store single-byte wise */
3839 f
->auxtype
= VT_STRUCT
;
3841 printf("FIX field %s : load byte-wise\n",
3842 get_tok_str(f
->v
& ~SYM_FIELD
, NULL
));
3848 /* enum/struct/union declaration. u is VT_ENUM/VT_STRUCT/VT_UNION */
3849 static void struct_decl(CType
*type
, int u
)
3851 int v
, c
, size
, align
, flexible
;
3852 int bit_size
, bsize
, bt
;
3854 AttributeDef ad
, ad1
;
3857 memset(&ad
, 0, sizeof ad
);
3859 parse_attribute(&ad
);
3863 /* struct already defined ? return it */
3865 expect("struct/union/enum name");
3867 if (s
&& (s
->sym_scope
== local_scope
|| tok
!= '{')) {
3870 if (u
== VT_ENUM
&& IS_ENUM(s
->type
.t
))
3872 tcc_error("redefinition of '%s'", get_tok_str(v
, NULL
));
3877 /* Record the original enum/struct/union token. */
3878 type1
.t
= u
== VT_ENUM
? u
| VT_INT
| VT_UNSIGNED
: u
;
3880 /* we put an undefined size for struct/union */
3881 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
3882 s
->r
= 0; /* default alignment is zero as gcc */
3884 type
->t
= s
->type
.t
;
3890 tcc_error("struct/union/enum already defined");
3892 /* cannot be empty */
3893 /* non empty enums are not allowed */
3896 long long ll
= 0, pl
= 0, nl
= 0;
3899 /* enum symbols have static storage */
3900 t
.t
= VT_INT
|VT_STATIC
|VT_ENUM_VAL
;
3904 expect("identifier");
3906 if (ss
&& !local_stack
)
3907 tcc_error("redefinition of enumerator '%s'",
3908 get_tok_str(v
, NULL
));
3912 ll
= expr_const64();
3914 ss
= sym_push(v
, &t
, VT_CONST
, 0);
3916 *ps
= ss
, ps
= &ss
->next
;
3925 /* NOTE: we accept a trailing comma */
3930 /* set integral type of the enum */
3933 if (pl
!= (unsigned)pl
)
3934 t
.t
= (LONG_SIZE
==8 ? VT_LLONG
|VT_LONG
: VT_LLONG
);
3936 } else if (pl
!= (int)pl
|| nl
!= (int)nl
)
3937 t
.t
= (LONG_SIZE
==8 ? VT_LLONG
|VT_LONG
: VT_LLONG
);
3938 s
->type
.t
= type
->t
= t
.t
| VT_ENUM
;
3940 /* set type for enum members */
3941 for (ss
= s
->next
; ss
; ss
= ss
->next
) {
3943 if (ll
== (int)ll
) /* default is int if it fits */
3945 if (t
.t
& VT_UNSIGNED
) {
3946 ss
->type
.t
|= VT_UNSIGNED
;
3947 if (ll
== (unsigned)ll
)
3950 ss
->type
.t
= (ss
->type
.t
& ~VT_BTYPE
)
3951 | (LONG_SIZE
==8 ? VT_LLONG
|VT_LONG
: VT_LLONG
);
3956 while (tok
!= '}') {
3957 if (!parse_btype(&btype
, &ad1
)) {
3963 tcc_error("flexible array member '%s' not at the end of struct",
3964 get_tok_str(v
, NULL
));
3970 type_decl(&type1
, &ad1
, &v
, TYPE_DIRECT
);
3972 if ((type1
.t
& VT_BTYPE
) != VT_STRUCT
)
3973 expect("identifier");
3975 int v
= btype
.ref
->v
;
3976 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
3977 if (tcc_state
->ms_extensions
== 0)
3978 expect("identifier");
3982 if (type_size(&type1
, &align
) < 0) {
3983 if ((u
== VT_STRUCT
) && (type1
.t
& VT_ARRAY
) && c
)
3986 tcc_error("field '%s' has incomplete type",
3987 get_tok_str(v
, NULL
));
3989 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
3990 (type1
.t
& VT_BTYPE
) == VT_VOID
||
3991 (type1
.t
& VT_STORAGE
))
3992 tcc_error("invalid type for '%s'",
3993 get_tok_str(v
, NULL
));
3997 bit_size
= expr_const();
3998 /* XXX: handle v = 0 case for messages */
4000 tcc_error("negative width in bit-field '%s'",
4001 get_tok_str(v
, NULL
));
4002 if (v
&& bit_size
== 0)
4003 tcc_error("zero width for bit-field '%s'",
4004 get_tok_str(v
, NULL
));
4005 parse_attribute(&ad1
);
4007 size
= type_size(&type1
, &align
);
4008 if (bit_size
>= 0) {
4009 bt
= type1
.t
& VT_BTYPE
;
4015 tcc_error("bitfields must have scalar type");
4017 if (bit_size
> bsize
) {
4018 tcc_error("width of '%s' exceeds its type",
4019 get_tok_str(v
, NULL
));
4020 } else if (bit_size
== bsize
4021 && !ad
.a
.packed
&& !ad1
.a
.packed
) {
4022 /* no need for bit fields */
4024 } else if (bit_size
== 64) {
4025 tcc_error("field width 64 not implemented");
4027 type1
.t
= (type1
.t
& ~VT_STRUCT_MASK
)
4029 | (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
4032 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
4033 /* Remember we've seen a real field to check
4034 for placement of flexible array member. */
4037 /* If member is a struct or bit-field, enforce
4038 placing into the struct (as anonymous). */
4040 ((type1
.t
& VT_BTYPE
) == VT_STRUCT
||
4045 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, 0);
4050 if (tok
== ';' || tok
== TOK_EOF
)
4057 parse_attribute(&ad
);
4058 struct_layout(type
, &ad
);
4063 static void sym_to_attr(AttributeDef
*ad
, Sym
*s
)
4065 merge_symattr(&ad
->a
, &s
->a
);
4066 merge_funcattr(&ad
->f
, &s
->f
);
4069 /* Add type qualifiers to a type. If the type is an array then the qualifiers
4070 are added to the element type, copied because it could be a typedef. */
4071 static void parse_btype_qualify(CType
*type
, int qualifiers
)
4073 while (type
->t
& VT_ARRAY
) {
4074 type
->ref
= sym_push(SYM_FIELD
, &type
->ref
->type
, 0, type
->ref
->c
);
4075 type
= &type
->ref
->type
;
4077 type
->t
|= qualifiers
;
4080 /* return 0 if no type declaration. otherwise, return the basic type
4083 static int parse_btype(CType
*type
, AttributeDef
*ad
)
4085 int t
, u
, bt
, st
, type_found
, typespec_found
, g
;
4089 memset(ad
, 0, sizeof(AttributeDef
));
4099 /* currently, we really ignore extension */
4109 if (u
== VT_SHORT
|| u
== VT_LONG
) {
4110 if (st
!= -1 || (bt
!= -1 && bt
!= VT_INT
))
4111 tmbt
: tcc_error("too many basic types");
4114 if (bt
!= -1 || (st
!= -1 && u
!= VT_INT
))
4119 t
= (t
& ~(VT_BTYPE
|VT_LONG
)) | u
;
4136 memset(&ad1
, 0, sizeof(AttributeDef
));
4137 if (parse_btype(&type1
, &ad1
)) {
4138 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
4140 n
= 1 << (ad1
.a
.aligned
- 1);
4142 type_size(&type1
, &n
);
4145 if (n
<= 0 || (n
& (n
- 1)) != 0)
4146 tcc_error("alignment must be a positive power of two");
4149 ad
->a
.aligned
= exact_log2p1(n
);
4153 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
4154 t
= (t
& ~(VT_BTYPE
|VT_LONG
)) | VT_LDOUBLE
;
4155 } else if ((t
& (VT_BTYPE
|VT_LONG
)) == VT_LONG
) {
4156 t
= (t
& ~(VT_BTYPE
|VT_LONG
)) | VT_LLONG
;
4163 #ifdef TCC_TARGET_ARM64
4165 /* GCC's __uint128_t appears in some Linux header files. Make it a
4166 synonym for long double to get the size and alignment right. */
4177 if ((t
& (VT_BTYPE
|VT_LONG
)) == VT_LONG
) {
4178 t
= (t
& ~(VT_BTYPE
|VT_LONG
)) | VT_LDOUBLE
;
4186 struct_decl(&type1
, VT_ENUM
);
4189 type
->ref
= type1
.ref
;
4192 struct_decl(&type1
, VT_STRUCT
);
4195 struct_decl(&type1
, VT_UNION
);
4198 /* type modifiers */
4203 parse_btype_qualify(type
, VT_CONSTANT
);
4211 parse_btype_qualify(type
, VT_VOLATILE
);
4218 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == (VT_DEFSIGN
|VT_UNSIGNED
))
4219 tcc_error("signed and unsigned modifier");
4232 if ((t
& (VT_DEFSIGN
|VT_UNSIGNED
)) == VT_DEFSIGN
)
4233 tcc_error("signed and unsigned modifier");
4234 t
|= VT_DEFSIGN
| VT_UNSIGNED
;
4250 if (t
& (VT_EXTERN
|VT_STATIC
|VT_TYPEDEF
) & ~g
)
4251 tcc_error("multiple storage classes");
4262 /* currently, no need to handle it because tcc does not
4263 track unused objects */
4266 /* GNUC attribute */
4267 case TOK_ATTRIBUTE1
:
4268 case TOK_ATTRIBUTE2
:
4269 parse_attribute(ad
);
4270 if (ad
->attr_mode
) {
4271 u
= ad
->attr_mode
-1;
4272 t
= (t
& ~(VT_BTYPE
|VT_LONG
)) | u
;
4280 parse_expr_type(&type1
);
4281 /* remove all storage modifiers except typedef */
4282 type1
.t
&= ~(VT_STORAGE
&~VT_TYPEDEF
);
4284 sym_to_attr(ad
, type1
.ref
);
4290 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
4292 t
&= ~(VT_BTYPE
|VT_LONG
);
4293 u
= t
& ~(VT_CONSTANT
| VT_VOLATILE
), t
^= u
;
4294 type
->t
= (s
->type
.t
& ~VT_TYPEDEF
) | u
;
4295 type
->ref
= s
->type
.ref
;
4297 parse_btype_qualify(type
, t
);
4299 /* get attributes from typedef */
4309 if (tcc_state
->char_is_unsigned
) {
4310 if ((t
& (VT_DEFSIGN
|VT_BTYPE
)) == VT_BYTE
)
4313 /* VT_LONG is used just as a modifier for VT_INT / VT_LLONG */
4314 bt
= t
& (VT_BTYPE
|VT_LONG
);
4316 t
|= LONG_SIZE
== 8 ? VT_LLONG
: VT_INT
;
4317 #ifdef TCC_TARGET_PE
4318 if (bt
== VT_LDOUBLE
)
4319 t
= (t
& ~(VT_BTYPE
|VT_LONG
)) | VT_DOUBLE
;
4325 /* convert a function parameter type (array to pointer and function to
4326 function pointer) */
4327 static inline void convert_parameter_type(CType
*pt
)
4329 /* remove const and volatile qualifiers (XXX: const could be used
4330 to indicate a const function parameter */
4331 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
4332 /* array must be transformed to pointer according to ANSI C */
4334 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
4339 ST_FUNC
void parse_asm_str(CString
*astr
)
4342 parse_mult_str(astr
, "string constant");
4345 /* Parse an asm label and return the token */
4346 static int asm_label_instr(void)
4352 parse_asm_str(&astr
);
4355 printf("asm_alias: \"%s\"\n", (char *)astr
.data
);
4357 v
= tok_alloc(astr
.data
, astr
.size
- 1)->tok
;
4362 static int post_type(CType
*type
, AttributeDef
*ad
, int storage
, int td
)
4364 int n
, l
, t1
, arg_size
, align
;
4365 Sym
**plast
, *s
, *first
;
4370 /* function type, or recursive declarator (return if so) */
4372 if (td
&& !(td
& TYPE_ABSTRACT
))
4376 else if (parse_btype(&pt
, &ad1
))
4379 merge_attr (ad
, &ad1
);
4388 /* read param name and compute offset */
4389 if (l
!= FUNC_OLD
) {
4390 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
4392 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
4393 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
4394 tcc_error("parameter declared as void");
4398 expect("identifier");
4399 pt
.t
= VT_VOID
; /* invalid type */
4402 convert_parameter_type(&pt
);
4403 arg_size
+= (type_size(&pt
, &align
) + PTR_SIZE
- 1) / PTR_SIZE
;
4404 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
4410 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
4415 if (l
== FUNC_NEW
&& !parse_btype(&pt
, &ad1
))
4416 tcc_error("invalid type");
4419 /* if no parameters, then old type prototype */
4422 /* NOTE: const is ignored in returned type as it has a special
4423 meaning in gcc / C++ */
4424 type
->t
&= ~VT_CONSTANT
;
4425 /* some ancient pre-K&R C allows a function to return an array
4426 and the array brackets to be put after the arguments, such
4427 that "int c()[]" means something like "int[] c()" */
4430 skip(']'); /* only handle simple "[]" */
4433 /* we push a anonymous symbol which will contain the function prototype */
4434 ad
->f
.func_args
= arg_size
;
4435 ad
->f
.func_type
= l
;
4436 s
= sym_push(SYM_FIELD
, type
, 0, 0);
4442 } else if (tok
== '[') {
4443 int saved_nocode_wanted
= nocode_wanted
;
4444 /* array definition */
4447 /* XXX The optional type-quals and static should only be accepted
4448 in parameter decls. The '*' as well, and then even only
4449 in prototypes (not function defs). */
4451 case TOK_RESTRICT1
: case TOK_RESTRICT2
: case TOK_RESTRICT3
:
4466 if (!local_stack
|| (storage
& VT_STATIC
))
4467 vpushi(expr_const());
4469 /* VLAs (which can only happen with local_stack && !VT_STATIC)
4470 length must always be evaluated, even under nocode_wanted,
4471 so that its size slot is initialized (e.g. under sizeof
4476 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4479 tcc_error("invalid array size");
4481 if (!is_integer_btype(vtop
->type
.t
& VT_BTYPE
))
4482 tcc_error("size of variable length array should be an integer");
4488 /* parse next post type */
4489 post_type(type
, ad
, storage
, 0);
4490 if (type
->t
== VT_FUNC
)
4491 tcc_error("declaration of an array of functions");
4492 t1
|= type
->t
& VT_VLA
;
4496 tcc_error("need explicit inner array size in VLAs");
4497 loc
-= type_size(&int_type
, &align
);
4501 vla_runtime_type_size(type
, &align
);
4503 vset(&int_type
, VT_LOCAL
|VT_LVAL
, n
);
4509 nocode_wanted
= saved_nocode_wanted
;
4511 /* we push an anonymous symbol which will contain the array
4513 s
= sym_push(SYM_FIELD
, type
, 0, n
);
4514 type
->t
= (t1
? VT_VLA
: VT_ARRAY
) | VT_PTR
;
4520 /* Parse a type declarator (except basic type), and return the type
4521 in 'type'. 'td' is a bitmask indicating which kind of type decl is
4522 expected. 'type' should contain the basic type. 'ad' is the
4523 attribute definition of the basic type. It can be modified by
4524 type_decl(). If this (possibly abstract) declarator is a pointer chain
4525 it returns the innermost pointed to type (equals *type, but is a different
4526 pointer), otherwise returns type itself, that's used for recursive calls. */
4527 static CType
*type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
4530 int qualifiers
, storage
;
4532 /* recursive type, remove storage bits first, apply them later again */
4533 storage
= type
->t
& VT_STORAGE
;
4534 type
->t
&= ~VT_STORAGE
;
4537 while (tok
== '*') {
4545 qualifiers
|= VT_CONSTANT
;
4550 qualifiers
|= VT_VOLATILE
;
4556 /* XXX: clarify attribute handling */
4557 case TOK_ATTRIBUTE1
:
4558 case TOK_ATTRIBUTE2
:
4559 parse_attribute(ad
);
4563 type
->t
|= qualifiers
;
4565 /* innermost pointed to type is the one for the first derivation */
4566 ret
= pointed_type(type
);
4570 /* This is possibly a parameter type list for abstract declarators
4571 ('int ()'), use post_type for testing this. */
4572 if (!post_type(type
, ad
, 0, td
)) {
4573 /* It's not, so it's a nested declarator, and the post operations
4574 apply to the innermost pointed to type (if any). */
4575 /* XXX: this is not correct to modify 'ad' at this point, but
4576 the syntax is not clear */
4577 parse_attribute(ad
);
4578 post
= type_decl(type
, ad
, v
, td
);
4582 } else if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
4583 /* type identifier */
4588 if (!(td
& TYPE_ABSTRACT
))
4589 expect("identifier");
4592 post_type(post
, ad
, storage
, 0);
4593 parse_attribute(ad
);
4598 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
4599 ST_FUNC
int lvalue_type(int t
)
4604 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
4606 else if (bt
== VT_SHORT
)
4610 if (t
& VT_UNSIGNED
)
4611 r
|= VT_LVAL_UNSIGNED
;
4615 /* indirection with full error checking and bound check */
4616 ST_FUNC
void indir(void)
4618 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
4619 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
4623 if (vtop
->r
& VT_LVAL
)
4625 vtop
->type
= *pointed_type(&vtop
->type
);
4626 /* Arrays and functions are never lvalues */
4627 if (!(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_VLA
)
4628 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
4629 vtop
->r
|= lvalue_type(vtop
->type
.t
);
4630 /* if bound checking, the referenced pointer must be checked */
4631 #ifdef CONFIG_TCC_BCHECK
4632 if (tcc_state
->do_bounds_check
)
4633 vtop
->r
|= VT_MUSTBOUND
;
4638 /* pass a parameter to a function and do type checking and casting */
4639 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
4644 func_type
= func
->f
.func_type
;
4645 if (func_type
== FUNC_OLD
||
4646 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
4647 /* default casting : only need to convert float to double */
4648 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
4649 gen_cast_s(VT_DOUBLE
);
4650 } else if (vtop
->type
.t
& VT_BITFIELD
) {
4651 type
.t
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
4652 type
.ref
= vtop
->type
.ref
;
4655 } else if (arg
== NULL
) {
4656 tcc_error("too many arguments to function");
4659 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
4660 gen_assign_cast(&type
);
4664 /* parse an expression and return its type without any side effect. */
4665 static void expr_type(CType
*type
, void (*expr_fn
)(void))
4674 /* parse an expression of the form '(type)' or '(expr)' and return its
4676 static void parse_expr_type(CType
*type
)
4682 if (parse_btype(type
, &ad
)) {
4683 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
4685 expr_type(type
, gexpr
);
4690 static void parse_type(CType
*type
)
4695 if (!parse_btype(type
, &ad
)) {
4698 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
4701 static void parse_builtin_params(int nc
, const char *args
)
4708 while ((c
= *args
++)) {
4712 case 'e': expr_eq(); continue;
4713 case 't': parse_type(&t
); vpush(&t
); continue;
4714 default: tcc_error("internal error"); break;
4722 static void try_call_scope_cleanup(Sym
*stop
)
4724 Sym
*cls
= current_cleanups
;
4726 for (; cls
!= stop
; cls
= cls
->ncl
) {
4727 Sym
*fs
= cls
->next
;
4728 Sym
*vs
= cls
->prev_tok
;
4730 vpushsym(&fs
->type
, fs
);
4731 vset(&vs
->type
, vs
->r
, vs
->c
);
4733 mk_pointer(&vtop
->type
);
4739 static void try_call_cleanup_goto(Sym
*cleanupstate
)
4744 if (!current_cleanups
)
4747 /* search NCA of both cleanup chains given parents and initial depth */
4748 ocd
= cleanupstate
? cleanupstate
->v
& ~SYM_FIELD
: 0;
4749 for (ccd
= ncleanups
, oc
= cleanupstate
; ocd
> ccd
; --ocd
, oc
= oc
->ncl
)
4751 for (cc
= current_cleanups
; ccd
> ocd
; --ccd
, cc
= cc
->ncl
)
4753 for (; cc
!= oc
; cc
= cc
->ncl
, oc
= oc
->ncl
, --ccd
)
4756 try_call_scope_cleanup(cc
);
4759 ST_FUNC
void unary(void)
4761 int n
, t
, align
, size
, r
, sizeof_caller
;
4766 sizeof_caller
= in_sizeof
;
4769 /* XXX: GCC 2.95.3 does not generate a table although it should be
4777 #ifdef TCC_TARGET_PE
4778 t
= VT_SHORT
|VT_UNSIGNED
;
4786 vsetc(&type
, VT_CONST
, &tokc
);
4790 t
= VT_INT
| VT_UNSIGNED
;
4796 t
= VT_LLONG
| VT_UNSIGNED
;
4808 t
= (LONG_SIZE
== 8 ? VT_LLONG
: VT_INT
) | VT_LONG
;
4811 t
= (LONG_SIZE
== 8 ? VT_LLONG
: VT_INT
) | VT_LONG
| VT_UNSIGNED
;
4813 case TOK___FUNCTION__
:
4815 goto tok_identifier
;
4821 /* special function name identifier */
4822 len
= strlen(funcname
) + 1;
4823 /* generate char[len] type */
4828 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
4829 if (!NODATA_WANTED
) {
4830 ptr
= section_ptr_add(data_section
, len
);
4831 memcpy(ptr
, funcname
, len
);
4837 #ifdef TCC_TARGET_PE
4838 t
= VT_SHORT
| VT_UNSIGNED
;
4844 /* string parsing */
4846 if (tcc_state
->char_is_unsigned
)
4847 t
= VT_BYTE
| VT_UNSIGNED
;
4849 if (tcc_state
->warn_write_strings
)
4854 memset(&ad
, 0, sizeof(AttributeDef
));
4855 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, 0);
4860 if (parse_btype(&type
, &ad
)) {
4861 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
4863 /* check ISOC99 compound literal */
4865 /* data is allocated locally by default */
4870 /* all except arrays are lvalues */
4871 if (!(type
.t
& VT_ARRAY
))
4872 r
|= lvalue_type(type
.t
);
4873 memset(&ad
, 0, sizeof(AttributeDef
));
4874 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, 0);
4876 if (sizeof_caller
) {
4883 } else if (tok
== '{') {
4884 int saved_nocode_wanted
= nocode_wanted
;
4886 tcc_error("expected constant");
4887 /* save all registers */
4889 /* statement expression : we do not accept break/continue
4890 inside as GCC does. We do retain the nocode_wanted state,
4891 as statement expressions can't ever be entered from the
4892 outside, so any reactivation of code emission (from labels
4893 or loop heads) can be disabled again after the end of it. */
4894 block(NULL
, NULL
, 1);
4895 nocode_wanted
= saved_nocode_wanted
;
4910 /* functions names must be treated as function pointers,
4911 except for unary '&' and sizeof. Since we consider that
4912 functions are not lvalues, we only have to handle it
4913 there and in function calls. */
4914 /* arrays can also be used although they are not lvalues */
4915 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
4916 !(vtop
->type
.t
& VT_ARRAY
))
4918 mk_pointer(&vtop
->type
);
4924 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4925 gen_cast_s(VT_BOOL
);
4926 vtop
->c
.i
= !vtop
->c
.i
;
4927 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
4931 vseti(VT_JMP
, gvtst(1, 0));
4943 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
4944 tcc_error("pointer not accepted for unary plus");
4945 /* In order to force cast, we add zero, except for floating point
4946 where we really need an noop (otherwise -0.0 will be transformed
4948 if (!is_float(vtop
->type
.t
)) {
4960 expr_type(&type
, unary
); /* Perform a in_sizeof = 0; */
4961 s
= vtop
[1].sym
; /* hack: accessing previous vtop */
4962 size
= type_size(&type
, &align
);
4963 if (s
&& s
->a
.aligned
)
4964 align
= 1 << (s
->a
.aligned
- 1);
4965 if (t
== TOK_SIZEOF
) {
4966 if (!(type
.t
& VT_VLA
)) {
4968 tcc_error("sizeof applied to an incomplete type");
4971 vla_runtime_type_size(&type
, &align
);
4976 vtop
->type
.t
|= VT_UNSIGNED
;
4979 case TOK_builtin_expect
:
4980 /* __builtin_expect is a no-op for now */
4981 parse_builtin_params(0, "ee");
4984 case TOK_builtin_types_compatible_p
:
4985 parse_builtin_params(0, "tt");
4986 vtop
[-1].type
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
4987 vtop
[0].type
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
4988 n
= is_compatible_types(&vtop
[-1].type
, &vtop
[0].type
);
4992 case TOK_builtin_choose_expr
:
5019 case TOK_builtin_constant_p
:
5020 parse_builtin_params(1, "e");
5021 n
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
5025 case TOK_builtin_frame_address
:
5026 case TOK_builtin_return_address
:
5032 if (tok
!= TOK_CINT
) {
5033 tcc_error("%s only takes positive integers",
5034 tok1
== TOK_builtin_return_address
?
5035 "__builtin_return_address" :
5036 "__builtin_frame_address");
5038 level
= (uint32_t)tokc
.i
;
5043 vset(&type
, VT_LOCAL
, 0); /* local frame */
5045 mk_pointer(&vtop
->type
);
5046 indir(); /* -> parent frame */
5048 if (tok1
== TOK_builtin_return_address
) {
5049 // assume return address is just above frame pointer on stack
5052 mk_pointer(&vtop
->type
);
5057 #ifdef TCC_TARGET_X86_64
5058 #ifdef TCC_TARGET_PE
5059 case TOK_builtin_va_start
:
5060 parse_builtin_params(0, "ee");
5061 r
= vtop
->r
& VT_VALMASK
;
5065 tcc_error("__builtin_va_start expects a local variable");
5067 vtop
->type
= char_pointer_type
;
5072 case TOK_builtin_va_arg_types
:
5073 parse_builtin_params(0, "t");
5074 vpushi(classify_x86_64_va_arg(&vtop
->type
));
5081 #ifdef TCC_TARGET_ARM64
5082 case TOK___va_start
: {
5083 parse_builtin_params(0, "ee");
5087 vtop
->type
.t
= VT_VOID
;
5090 case TOK___va_arg
: {
5091 parse_builtin_params(0, "et");
5099 case TOK___arm64_clear_cache
: {
5100 parse_builtin_params(0, "ee");
5103 vtop
->type
.t
= VT_VOID
;
5107 /* pre operations */
5118 t
= vtop
->type
.t
& VT_BTYPE
;
5120 /* In IEEE negate(x) isn't subtract(0,x), but rather
5124 vtop
->c
.f
= -1.0 * 0.0;
5125 else if (t
== VT_DOUBLE
)
5126 vtop
->c
.d
= -1.0 * 0.0;
5128 vtop
->c
.ld
= -1.0 * 0.0;
5136 goto tok_identifier
;
5138 /* allow to take the address of a label */
5139 if (tok
< TOK_UIDENT
)
5140 expect("label identifier");
5141 s
= label_find(tok
);
5143 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
5145 if (s
->r
== LABEL_DECLARED
)
5146 s
->r
= LABEL_FORWARD
;
5149 s
->type
.t
= VT_VOID
;
5150 mk_pointer(&s
->type
);
5151 s
->type
.t
|= VT_STATIC
;
5153 vpushsym(&s
->type
, s
);
5159 CType controlling_type
;
5160 int has_default
= 0;
5163 TokenString
*str
= NULL
;
5164 int saved_const_wanted
= const_wanted
;
5169 expr_type(&controlling_type
, expr_eq
);
5170 controlling_type
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
| VT_ARRAY
);
5171 if ((controlling_type
.t
& VT_BTYPE
) == VT_FUNC
)
5172 mk_pointer(&controlling_type
);
5173 const_wanted
= saved_const_wanted
;
5177 if (tok
== TOK_DEFAULT
) {
5179 tcc_error("too many 'default'");
5185 AttributeDef ad_tmp
;
5188 parse_btype(&cur_type
, &ad_tmp
);
5189 type_decl(&cur_type
, &ad_tmp
, &itmp
, TYPE_ABSTRACT
);
5190 if (compare_types(&controlling_type
, &cur_type
, 0)) {
5192 tcc_error("type match twice");
5202 skip_or_save_block(&str
);
5204 skip_or_save_block(NULL
);
5211 type_to_str(buf
, sizeof buf
, &controlling_type
, NULL
);
5212 tcc_error("type '%s' does not match any association", buf
);
5214 begin_macro(str
, 1);
5223 // special qnan , snan and infinity values
5228 vtop
->type
.t
= VT_FLOAT
;
5233 goto special_math_val
;
5236 goto special_math_val
;
5243 expect("identifier");
5245 if (!s
|| IS_ASM_SYM(s
)) {
5246 const char *name
= get_tok_str(t
, NULL
);
5248 tcc_error("'%s' undeclared", name
);
5249 /* for simple function calls, we tolerate undeclared
5250 external reference to int() function */
5251 if (tcc_state
->warn_implicit_function_declaration
5252 #ifdef TCC_TARGET_PE
5253 /* people must be warned about using undeclared WINAPI functions
5254 (which usually start with uppercase letter) */
5255 || (name
[0] >= 'A' && name
[0] <= 'Z')
5258 tcc_warning("implicit declaration of function '%s'", name
);
5259 s
= external_global_sym(t
, &func_old_type
);
5263 /* A symbol that has a register is a local register variable,
5264 which starts out as VT_LOCAL value. */
5265 if ((r
& VT_VALMASK
) < VT_CONST
)
5266 r
= (r
& ~VT_VALMASK
) | VT_LOCAL
;
5268 vset(&s
->type
, r
, s
->c
);
5269 /* Point to s as backpointer (even without r&VT_SYM).
5270 Will be used by at least the x86 inline asm parser for
5276 } else if (r
== VT_CONST
&& IS_ENUM_VAL(s
->type
.t
)) {
5277 vtop
->c
.i
= s
->enum_val
;
5282 /* post operations */
5284 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
5287 } else if (tok
== '.' || tok
== TOK_ARROW
|| tok
== TOK_CDOUBLE
) {
5288 int qualifiers
, cumofs
= 0;
5290 if (tok
== TOK_ARROW
)
5292 qualifiers
= vtop
->type
.t
& (VT_CONSTANT
| VT_VOLATILE
);
5295 /* expect pointer on structure */
5296 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
5297 expect("struct or union");
5298 if (tok
== TOK_CDOUBLE
)
5299 expect("field name");
5301 if (tok
== TOK_CINT
|| tok
== TOK_CUINT
)
5302 expect("field name");
5303 s
= find_field(&vtop
->type
, tok
, &cumofs
);
5305 tcc_error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, &tokc
));
5306 /* add field offset to pointer */
5307 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
5308 vpushi(cumofs
+ s
->c
);
5310 /* change type to field type, and set to lvalue */
5311 vtop
->type
= s
->type
;
5312 vtop
->type
.t
|= qualifiers
;
5313 /* an array is never an lvalue */
5314 if (!(vtop
->type
.t
& VT_ARRAY
)) {
5315 vtop
->r
|= lvalue_type(vtop
->type
.t
);
5316 #ifdef CONFIG_TCC_BCHECK
5317 /* if bound checking, the referenced pointer must be checked */
5318 if (tcc_state
->do_bounds_check
&& (vtop
->r
& VT_VALMASK
) != VT_LOCAL
)
5319 vtop
->r
|= VT_MUSTBOUND
;
5323 } else if (tok
== '[') {
5329 } else if (tok
== '(') {
5332 int nb_args
, ret_nregs
, ret_align
, regsize
, variadic
;
5335 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
5336 /* pointer test (no array accepted) */
5337 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
5338 vtop
->type
= *pointed_type(&vtop
->type
);
5339 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
5343 expect("function pointer");
5346 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
5348 /* get return type */
5351 sa
= s
->next
; /* first parameter */
5352 nb_args
= regsize
= 0;
5354 /* compute first implicit argument if a structure is returned */
5355 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
5356 variadic
= (s
->f
.func_type
== FUNC_ELLIPSIS
);
5357 ret_nregs
= gfunc_sret(&s
->type
, variadic
, &ret
.type
,
5358 &ret_align
, ®size
);
5360 /* get some space for the returned structure */
5361 size
= type_size(&s
->type
, &align
);
5362 #ifdef TCC_TARGET_ARM64
5363 /* On arm64, a small struct is return in registers.
5364 It is much easier to write it to memory if we know
5365 that we are allowed to write some extra bytes, so
5366 round the allocated space up to a power of 2: */
5368 while (size
& (size
- 1))
5369 size
= (size
| (size
- 1)) + 1;
5371 loc
= (loc
- size
) & -align
;
5373 ret
.r
= VT_LOCAL
| VT_LVAL
;
5374 /* pass it as 'int' to avoid structure arg passing
5376 vseti(VT_LOCAL
, loc
);
5386 /* return in register */
5387 if (is_float(ret
.type
.t
)) {
5388 ret
.r
= reg_fret(ret
.type
.t
);
5389 #ifdef TCC_TARGET_X86_64
5390 if ((ret
.type
.t
& VT_BTYPE
) == VT_QFLOAT
)
5394 #ifndef TCC_TARGET_ARM64
5395 #ifdef TCC_TARGET_X86_64
5396 if ((ret
.type
.t
& VT_BTYPE
) == VT_QLONG
)
5398 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
5409 gfunc_param_typed(s
, sa
);
5419 tcc_error("too few arguments to function");
5421 gfunc_call(nb_args
);
5424 for (r
= ret
.r
+ ret_nregs
+ !ret_nregs
; r
-- > ret
.r
;) {
5425 vsetc(&ret
.type
, r
, &ret
.c
);
5426 vtop
->r2
= ret
.r2
; /* Loop only happens when r2 is VT_CONST */
5429 /* handle packed struct return */
5430 if (((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) && ret_nregs
) {
5433 size
= type_size(&s
->type
, &align
);
5434 /* We're writing whole regs often, make sure there's enough
5435 space. Assume register size is power of 2. */
5436 if (regsize
> align
)
5438 loc
= (loc
- size
) & -align
;
5442 vset(&ret
.type
, VT_LOCAL
| VT_LVAL
, addr
+ offset
);
5446 if (--ret_nregs
== 0)
5450 vset(&s
->type
, VT_LOCAL
| VT_LVAL
, addr
);
5458 ST_FUNC
void expr_prod(void)
5463 while (tok
== '*' || tok
== '/' || tok
== '%') {
5471 ST_FUNC
void expr_sum(void)
5476 while (tok
== '+' || tok
== '-') {
5484 static void expr_shift(void)
5489 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
5497 static void expr_cmp(void)
5502 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
5503 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
5511 static void expr_cmpeq(void)
5516 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
5524 static void expr_and(void)
5527 while (tok
== '&') {
5534 static void expr_xor(void)
5537 while (tok
== '^') {
5544 static void expr_or(void)
5547 while (tok
== '|') {
5554 static void expr_land(void)
5557 if (tok
== TOK_LAND
) {
5560 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
5561 gen_cast_s(VT_BOOL
);
5566 while (tok
== TOK_LAND
) {
5582 if (tok
!= TOK_LAND
) {
5595 static void expr_lor(void)
5598 if (tok
== TOK_LOR
) {
5601 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
5602 gen_cast_s(VT_BOOL
);
5607 while (tok
== TOK_LOR
) {
5623 if (tok
!= TOK_LOR
) {
5636 /* Assuming vtop is a value used in a conditional context
5637 (i.e. compared with zero) return 0 if it's false, 1 if
5638 true and -1 if it can't be statically determined. */
5639 static int condition_3way(void)
5642 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
&&
5643 (!(vtop
->r
& VT_SYM
) || !vtop
->sym
->a
.weak
)) {
5645 gen_cast_s(VT_BOOL
);
5652 static void expr_cond(void)
5654 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
, islv
, c
, g
;
5656 CType type
, type1
, type2
;
5661 c
= condition_3way();
5662 g
= (tok
== ':' && gnu_ext
);
5664 /* needed to avoid having different registers saved in
5666 if (is_float(vtop
->type
.t
)) {
5668 #ifdef TCC_TARGET_X86_64
5669 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
5693 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
5694 mk_pointer(&vtop
->type
);
5696 sv
= *vtop
; /* save value to handle it later */
5697 vtop
--; /* no vpop so that FP stack is not flushed */
5713 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
5714 mk_pointer(&vtop
->type
);
5717 bt1
= t1
& VT_BTYPE
;
5719 bt2
= t2
& VT_BTYPE
;
5723 /* cast operands to correct type according to ISOC rules */
5724 if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
5725 type
.t
= VT_VOID
; /* NOTE: as an extension, we accept void on only one side */
5726 } else if (is_float(bt1
) || is_float(bt2
)) {
5727 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
5728 type
.t
= VT_LDOUBLE
;
5730 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
5735 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
5736 /* cast to biggest op */
5737 type
.t
= VT_LLONG
| VT_LONG
;
5738 if (bt1
== VT_LLONG
)
5740 if (bt2
== VT_LLONG
)
5742 /* convert to unsigned if it does not fit in a long long */
5743 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_LLONG
| VT_UNSIGNED
) ||
5744 (t2
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_LLONG
| VT_UNSIGNED
))
5745 type
.t
|= VT_UNSIGNED
;
5746 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
5747 /* http://port70.net/~nsz/c/c99/n1256.html#6.5.15p6 */
5748 /* If one is a null ptr constant the result type
5750 if (is_null_pointer (vtop
)) type
= type1
;
5751 else if (is_null_pointer (&sv
)) type
= type2
;
5752 else if (bt1
!= bt2
)
5753 tcc_error("incompatible types in conditional expressions");
5755 CType
*pt1
= pointed_type(&type1
);
5756 CType
*pt2
= pointed_type(&type2
);
5757 int pbt1
= pt1
->t
& VT_BTYPE
;
5758 int pbt2
= pt2
->t
& VT_BTYPE
;
5759 int newquals
, copied
= 0;
5760 /* pointers to void get preferred, otherwise the
5761 pointed to types minus qualifs should be compatible */
5762 type
= (pbt1
== VT_VOID
) ? type1
: type2
;
5763 if (pbt1
!= VT_VOID
&& pbt2
!= VT_VOID
) {
5764 if(!compare_types(pt1
, pt2
, 1/*unqualif*/))
5765 tcc_warning("pointer type mismatch in conditional expression\n");
5767 /* combine qualifs */
5768 newquals
= ((pt1
->t
| pt2
->t
) & (VT_CONSTANT
| VT_VOLATILE
));
5769 if ((~pointed_type(&type
)->t
& (VT_CONSTANT
| VT_VOLATILE
))
5772 /* copy the pointer target symbol */
5773 type
.ref
= sym_push(SYM_FIELD
, &type
.ref
->type
,
5776 pointed_type(&type
)->t
|= newquals
;
5778 /* pointers to incomplete arrays get converted to
5779 pointers to completed ones if possible */
5780 if (pt1
->t
& VT_ARRAY
5781 && pt2
->t
& VT_ARRAY
5782 && pointed_type(&type
)->ref
->c
< 0
5783 && (pt1
->ref
->c
> 0 || pt2
->ref
->c
> 0))
5786 type
.ref
= sym_push(SYM_FIELD
, &type
.ref
->type
,
5788 pointed_type(&type
)->ref
=
5789 sym_push(SYM_FIELD
, &pointed_type(&type
)->ref
->type
,
5790 0, pointed_type(&type
)->ref
->c
);
5791 pointed_type(&type
)->ref
->c
=
5792 0 < pt1
->ref
->c
? pt1
->ref
->c
: pt2
->ref
->c
;
5795 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
5796 /* XXX: test structure compatibility */
5797 type
= bt1
== VT_STRUCT
? type1
: type2
;
5799 /* integer operations */
5800 type
.t
= VT_INT
| (VT_LONG
& (t1
| t2
));
5801 /* convert to unsigned if it does not fit in an integer */
5802 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_INT
| VT_UNSIGNED
) ||
5803 (t2
& (VT_BTYPE
| VT_UNSIGNED
| VT_BITFIELD
)) == (VT_INT
| VT_UNSIGNED
))
5804 type
.t
|= VT_UNSIGNED
;
5806 /* keep structs lvalue by transforming `(expr ? a : b)` to `*(expr ? &a : &b)` so
5807 that `(expr ? a : b).mem` does not error with "lvalue expected" */
5808 islv
= (vtop
->r
& VT_LVAL
) && (sv
.r
& VT_LVAL
) && VT_STRUCT
== (type
.t
& VT_BTYPE
);
5810 /* now we convert second operand */
5814 mk_pointer(&vtop
->type
);
5816 } else if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
5821 if (is_float(type
.t
)) {
5823 #ifdef TCC_TARGET_X86_64
5824 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
5828 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
5829 /* for long longs, we use fixed registers to avoid having
5830 to handle a complicated move */
5841 /* this is horrible, but we must also convert first
5847 mk_pointer(&vtop
->type
);
5849 } else if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
5853 if (c
< 0 || islv
) {
5855 move_reg(r2
, r1
, type
.t
);
5865 static void expr_eq(void)
5871 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
5872 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
5873 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
5888 ST_FUNC
void gexpr(void)
5899 /* parse a constant expression and return value in vtop. */
5900 static void expr_const1(void)
5909 /* parse an integer constant and return its value. */
5910 static inline int64_t expr_const64(void)
5914 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
5915 expect("constant expression");
5921 /* parse an integer constant and return its value.
5922 Complain if it doesn't fit 32bit (signed or unsigned). */
5923 ST_FUNC
int expr_const(void)
5926 int64_t wc
= expr_const64();
5928 if (c
!= wc
&& (unsigned)c
!= wc
)
5929 tcc_error("constant exceeds 32 bit");
5933 /* return the label token if current token is a label, otherwise
5935 static int is_label(void)
5939 /* fast test first */
5940 if (tok
< TOK_UIDENT
)
5942 /* no need to save tokc because tok is an identifier */
5948 unget_tok(last_tok
);
5953 #ifndef TCC_TARGET_ARM64
5954 static void gfunc_return(CType
*func_type
)
5956 if ((func_type
->t
& VT_BTYPE
) == VT_STRUCT
) {
5957 CType type
, ret_type
;
5958 int ret_align
, ret_nregs
, regsize
;
5959 ret_nregs
= gfunc_sret(func_type
, func_var
, &ret_type
,
5960 &ret_align
, ®size
);
5961 if (0 == ret_nregs
) {
5962 /* if returning structure, must copy it to implicit
5963 first pointer arg location */
5966 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
5969 /* copy structure value to pointer */
5972 /* returning structure packed into registers */
5973 int r
, size
, addr
, align
;
5974 size
= type_size(func_type
,&align
);
5975 if ((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) ||
5976 (vtop
->c
.i
& (ret_align
-1)))
5977 && (align
& (ret_align
-1))) {
5978 loc
= (loc
- size
) & -ret_align
;
5981 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
5985 vset(&ret_type
, VT_LOCAL
| VT_LVAL
, addr
);
5987 vtop
->type
= ret_type
;
5988 if (is_float(ret_type
.t
))
5989 r
= rc_fret(ret_type
.t
);
6000 if (--ret_nregs
== 0)
6002 /* We assume that when a structure is returned in multiple
6003 registers, their classes are consecutive values of the
6006 vtop
->c
.i
+= regsize
;
6010 } else if (is_float(func_type
->t
)) {
6011 gv(rc_fret(func_type
->t
));
6015 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
6019 static int case_cmp(const void *pa
, const void *pb
)
6021 int64_t a
= (*(struct case_t
**) pa
)->v1
;
6022 int64_t b
= (*(struct case_t
**) pb
)->v1
;
6023 return a
< b
? -1 : a
> b
;
6026 static void gcase(struct case_t
**base
, int len
, int *bsym
)
6030 int ll
= (vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
;
6048 gtst_addr(0, p
->sym
); /* v1 <= x <= v2 */
6050 gcase(base
, len
/2, bsym
);
6051 if (cur_switch
->def_sym
)
6052 gjmp_addr(cur_switch
->def_sym
);
6054 *bsym
= gjmp(*bsym
);
6058 base
+= e
; len
-= e
;
6068 if (p
->v1
== p
->v2
) {
6070 gtst_addr(0, p
->sym
);
6080 gtst_addr(0, p
->sym
);
6086 static void block(int *bsym
, int *csym
, int is_expr
)
6088 int a
, b
, c
, d
, cond
;
6091 /* generate line number info */
6092 if (tcc_state
->do_debug
)
6093 tcc_debug_line(tcc_state
);
6096 /* default return value is (void) */
6098 vtop
->type
.t
= VT_VOID
;
6101 if (tok
== TOK_IF
) {
6103 int saved_nocode_wanted
= nocode_wanted
;
6108 cond
= condition_3way();
6114 nocode_wanted
|= 0x20000000;
6115 block(bsym
, csym
, 0);
6117 nocode_wanted
= saved_nocode_wanted
;
6118 if (tok
== TOK_ELSE
) {
6123 nocode_wanted
|= 0x20000000;
6124 block(bsym
, csym
, 0);
6125 gsym(d
); /* patch else jmp */
6127 nocode_wanted
= saved_nocode_wanted
;
6130 } else if (tok
== TOK_WHILE
) {
6131 int saved_nocode_wanted
;
6132 nocode_wanted
&= ~0x20000000;
6142 saved_nocode_wanted
= nocode_wanted
;
6144 nocode_wanted
= saved_nocode_wanted
;
6149 } else if (tok
== '{') {
6150 Sym
*llabel
, *lcleanup
;
6151 int block_vla_sp_loc
= vla_sp_loc
, saved_vlas_in_scope
= vlas_in_scope
;
6152 int lncleanups
= ncleanups
;
6155 /* record local declaration stack position */
6157 llabel
= local_label_stack
;
6158 lcleanup
= current_cleanups
;
6161 /* handle local labels declarations */
6162 while (tok
== TOK_LABEL
) {
6165 if (tok
< TOK_UIDENT
)
6166 expect("label identifier");
6167 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
6177 while (tok
!= '}') {
6178 if ((a
= is_label()))
6185 block(bsym
, csym
, is_expr
);
6189 if (current_cleanups
!= lcleanup
) {
6193 for (pg
= &pending_gotos
; (g
= *pg
) && g
->c
> lncleanups
;)
6194 if (g
->prev_tok
->r
& LABEL_FORWARD
) {
6199 try_call_scope_cleanup(lcleanup
);
6200 pcl
->jnext
= gjmp(0);
6202 goto remove_pending
;
6211 if (!nocode_wanted
) {
6212 try_call_scope_cleanup(lcleanup
);
6216 current_cleanups
= lcleanup
;
6217 ncleanups
= lncleanups
;
6218 /* pop locally defined labels */
6219 label_pop(&local_label_stack
, llabel
, is_expr
);
6220 /* pop locally defined symbols */
6222 /* In the is_expr case (a statement expression is finished here),
6223 vtop might refer to symbols on the local_stack. Either via the
6224 type or via vtop->sym. We can't pop those nor any that in turn
6225 might be referred to. To make it easier we don't roll back
6226 any symbols in that case; some upper level call to block() will
6227 do that. We do have to remove such symbols from the lookup
6228 tables, though. sym_pop will do that. */
6229 sym_pop(&local_stack
, s
, is_expr
);
6231 /* Pop VLA frames and restore stack pointer if required */
6232 if (vlas_in_scope
> saved_vlas_in_scope
) {
6233 vla_sp_loc
= saved_vlas_in_scope
? block_vla_sp_loc
: vla_sp_root_loc
;
6236 vlas_in_scope
= saved_vlas_in_scope
;
6239 } else if (tok
== TOK_RETURN
) {
6243 gen_assign_cast(&func_vt
);
6244 try_call_scope_cleanup(NULL
);
6245 if ((func_vt
.t
& VT_BTYPE
) == VT_VOID
)
6248 gfunc_return(&func_vt
);
6250 try_call_scope_cleanup(NULL
);
6253 /* jump unless last stmt in top-level block */
6254 if (tok
!= '}' || local_scope
!= 1)
6256 nocode_wanted
|= 0x20000000;
6257 } else if (tok
== TOK_BREAK
) {
6260 tcc_error("cannot break");
6261 *bsym
= gjmp(*bsym
);
6264 nocode_wanted
|= 0x20000000;
6265 } else if (tok
== TOK_CONTINUE
) {
6268 tcc_error("cannot continue");
6269 vla_sp_restore_root();
6270 *csym
= gjmp(*csym
);
6273 nocode_wanted
|= 0x20000000;
6274 } else if (tok
== TOK_FOR
) {
6276 int saved_nocode_wanted
;
6277 nocode_wanted
&= ~0x20000000;
6283 /* c99 for-loop init decl? */
6284 if (!decl0(VT_LOCAL
, 1, NULL
)) {
6285 /* no, regular for-loop init expr */
6311 saved_nocode_wanted
= nocode_wanted
;
6313 nocode_wanted
= saved_nocode_wanted
;
6318 sym_pop(&local_stack
, s
, 0);
6321 if (tok
== TOK_DO
) {
6322 int saved_nocode_wanted
;
6323 nocode_wanted
&= ~0x20000000;
6329 saved_nocode_wanted
= nocode_wanted
;
6335 nocode_wanted
= saved_nocode_wanted
;
6339 nocode_wanted
= saved_nocode_wanted
;
6344 if (tok
== TOK_SWITCH
) {
6345 struct switch_t
*saved
, sw
;
6346 int saved_nocode_wanted
= nocode_wanted
;
6352 switchval
= *vtop
--;
6354 b
= gjmp(0); /* jump to first case */
6355 sw
.p
= NULL
; sw
.n
= 0; sw
.def_sym
= 0;
6359 nocode_wanted
= saved_nocode_wanted
;
6360 a
= gjmp(a
); /* add implicit break */
6363 qsort(sw
.p
, sw
.n
, sizeof(void*), case_cmp
);
6364 for (b
= 1; b
< sw
.n
; b
++)
6365 if (sw
.p
[b
- 1]->v2
>= sw
.p
[b
]->v1
)
6366 tcc_error("duplicate case value");
6367 /* Our switch table sorting is signed, so the compared
6368 value needs to be as well when it's 64bit. */
6369 if ((switchval
.type
.t
& VT_BTYPE
) == VT_LLONG
)
6370 switchval
.type
.t
&= ~VT_UNSIGNED
;
6372 gcase(sw
.p
, sw
.n
, &a
);
6375 gjmp_addr(sw
.def_sym
);
6376 dynarray_reset(&sw
.p
, &sw
.n
);
6381 if (tok
== TOK_CASE
) {
6382 struct case_t
*cr
= tcc_malloc(sizeof(struct case_t
));
6385 nocode_wanted
&= ~0x20000000;
6387 cr
->v1
= cr
->v2
= expr_const64();
6388 if (gnu_ext
&& tok
== TOK_DOTS
) {
6390 cr
->v2
= expr_const64();
6391 if (cr
->v2
< cr
->v1
)
6392 tcc_warning("empty case range");
6395 dynarray_add(&cur_switch
->p
, &cur_switch
->n
, cr
);
6398 goto block_after_label
;
6400 if (tok
== TOK_DEFAULT
) {
6405 if (cur_switch
->def_sym
)
6406 tcc_error("too many 'default'");
6407 cur_switch
->def_sym
= ind
;
6409 goto block_after_label
;
6411 if (tok
== TOK_GOTO
) {
6413 if (tok
== '*' && gnu_ext
) {
6417 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
6420 } else if (tok
>= TOK_UIDENT
) {
6421 s
= label_find(tok
);
6422 /* put forward definition if needed */
6424 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
6425 else if (s
->r
== LABEL_DECLARED
)
6426 s
->r
= LABEL_FORWARD
;
6428 vla_sp_restore_root();
6429 if (s
->r
& LABEL_FORWARD
) {
6430 /* start new goto chain for cleanups, linked via label->next */
6431 if (current_cleanups
) {
6432 sym_push2(&pending_gotos
, SYM_FIELD
, 0, ncleanups
);
6433 pending_gotos
->prev_tok
= s
;
6434 s
= sym_push2(&s
->next
, SYM_FIELD
, 0, 0);
6435 pending_gotos
->next
= s
;
6437 s
->jnext
= gjmp(s
->jnext
);
6439 try_call_cleanup_goto(s
->cleanupstate
);
6440 gjmp_addr(s
->jnext
);
6444 expect("label identifier");
6447 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
6456 if (s
->r
== LABEL_DEFINED
)
6457 tcc_error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
6458 s
->r
= LABEL_DEFINED
;
6460 Sym
*pcl
; /* pending cleanup goto */
6461 for (pcl
= s
->next
; pcl
; pcl
= pcl
->prev
)
6463 sym_pop(&s
->next
, NULL
, 0);
6467 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
6470 s
->cleanupstate
= current_cleanups
;
6472 /* we accept this, but it is a mistake */
6474 nocode_wanted
&= ~0x20000000;
6476 tcc_warning("deprecated use of label at end of compound statement");
6480 block(bsym
, csym
, is_expr
);
6483 /* expression case */
6498 /* This skips over a stream of tokens containing balanced {} and ()
6499 pairs, stopping at outer ',' ';' and '}' (or matching '}' if we started
6500 with a '{'). If STR then allocates and stores the skipped tokens
6501 in *STR. This doesn't check if () and {} are nested correctly,
6502 i.e. "({)}" is accepted. */
6503 static void skip_or_save_block(TokenString
**str
)
6505 int braces
= tok
== '{';
6508 *str
= tok_str_alloc();
6510 while ((level
> 0 || (tok
!= '}' && tok
!= ',' && tok
!= ';' && tok
!= ')'))) {
6512 if (tok
== TOK_EOF
) {
6513 if (str
|| level
> 0)
6514 tcc_error("unexpected end of file");
6519 tok_str_add_tok(*str
);
6522 if (t
== '{' || t
== '(') {
6524 } else if (t
== '}' || t
== ')') {
6526 if (level
== 0 && braces
&& t
== '}')
6531 tok_str_add(*str
, -1);
6532 tok_str_add(*str
, 0);
6536 #define EXPR_CONST 1
6539 static void parse_init_elem(int expr_type
)
6541 int saved_global_expr
;
6544 /* compound literals must be allocated globally in this case */
6545 saved_global_expr
= global_expr
;
6548 global_expr
= saved_global_expr
;
6549 /* NOTE: symbols are accepted, as well as lvalue for anon symbols
6550 (compound literals). */
6551 if (((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
6552 && ((vtop
->r
& (VT_SYM
|VT_LVAL
)) != (VT_SYM
|VT_LVAL
)
6553 || vtop
->sym
->v
< SYM_FIRST_ANOM
))
6554 #ifdef TCC_TARGET_PE
6555 || ((vtop
->r
& VT_SYM
) && vtop
->sym
->a
.dllimport
)
6558 tcc_error("initializer element is not constant");
6566 /* put zeros for variable based init */
6567 static void init_putz(Section
*sec
, unsigned long c
, int size
)
6570 /* nothing to do because globals are already set to zero */
6572 vpush_global_sym(&func_old_type
, TOK_memset
);
6574 #ifdef TCC_TARGET_ARM
6586 #define DIF_SIZE_ONLY 2
6587 #define DIF_HAVE_ELEM 4
6589 /* t is the array or struct type. c is the array or struct
6590 address. cur_field is the pointer to the current
6591 field, for arrays the 'c' member contains the current start
6592 index. 'flags' is as in decl_initializer.
6593 'al' contains the already initialized length of the
6594 current container (starting at c). This returns the new length of that. */
6595 static int decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
6596 Sym
**cur_field
, int flags
, int al
)
6599 int index
, index_last
, align
, l
, nb_elems
, elem_size
;
6600 unsigned long corig
= c
;
6604 if (flags
& DIF_HAVE_ELEM
)
6606 if (gnu_ext
&& (l
= is_label()) != 0)
6608 /* NOTE: we only support ranges for last designator */
6609 while (nb_elems
== 1 && (tok
== '[' || tok
== '.')) {
6611 if (!(type
->t
& VT_ARRAY
))
6612 expect("array type");
6614 index
= index_last
= expr_const();
6615 if (tok
== TOK_DOTS
&& gnu_ext
) {
6617 index_last
= expr_const();
6621 if (index
< 0 || (s
->c
>= 0 && index_last
>= s
->c
) ||
6623 tcc_error("invalid index");
6625 (*cur_field
)->c
= index_last
;
6626 type
= pointed_type(type
);
6627 elem_size
= type_size(type
, &align
);
6628 c
+= index
* elem_size
;
6629 nb_elems
= index_last
- index
+ 1;
6636 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
6637 expect("struct/union type");
6638 f
= find_field(type
, l
, &cumofs
);
6651 } else if (!gnu_ext
) {
6656 if (type
->t
& VT_ARRAY
) {
6657 index
= (*cur_field
)->c
;
6658 if (type
->ref
->c
>= 0 && index
>= type
->ref
->c
)
6659 tcc_error("index too large");
6660 type
= pointed_type(type
);
6661 c
+= index
* type_size(type
, &align
);
6664 while (f
&& (f
->v
& SYM_FIRST_ANOM
) && (f
->type
.t
& VT_BITFIELD
))
6665 *cur_field
= f
= f
->next
;
6667 tcc_error("too many field init");
6672 /* must put zero in holes (note that doing it that way
6673 ensures that it even works with designators) */
6674 if (!(flags
& DIF_SIZE_ONLY
) && c
- corig
> al
)
6675 init_putz(sec
, corig
+ al
, c
- corig
- al
);
6676 decl_initializer(type
, sec
, c
, flags
& ~DIF_FIRST
);
6678 /* XXX: make it more general */
6679 if (!(flags
& DIF_SIZE_ONLY
) && nb_elems
> 1) {
6680 unsigned long c_end
;
6685 vset(type
, VT_LOCAL
|VT_LVAL
, c
);
6686 for (i
= 1; i
< nb_elems
; i
++) {
6687 vset(type
, VT_LOCAL
|VT_LVAL
, c
+ elem_size
* i
);
6692 } else if (!NODATA_WANTED
) {
6693 c_end
= c
+ nb_elems
* elem_size
;
6694 if (c_end
> sec
->data_allocated
)
6695 section_realloc(sec
, c_end
);
6696 src
= sec
->data
+ c
;
6698 for(i
= 1; i
< nb_elems
; i
++) {
6700 memcpy(dst
, src
, elem_size
);
6704 c
+= nb_elems
* type_size(type
, &align
);
6710 /* store a value or an expression directly in global data or in local array */
6711 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
)
6718 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
6722 /* XXX: not portable */
6723 /* XXX: generate error if incorrect relocation */
6724 gen_assign_cast(&dtype
);
6725 bt
= type
->t
& VT_BTYPE
;
6727 if ((vtop
->r
& VT_SYM
)
6730 && (bt
!= (PTR_SIZE
== 8 ? VT_LLONG
: VT_INT
)
6731 || (type
->t
& VT_BITFIELD
))
6732 && !((vtop
->r
& VT_CONST
) && vtop
->sym
->v
>= SYM_FIRST_ANOM
)
6734 tcc_error("initializer element is not computable at load time");
6736 if (NODATA_WANTED
) {
6741 size
= type_size(type
, &align
);
6742 section_reserve(sec
, c
+ size
);
6743 ptr
= sec
->data
+ c
;
6745 /* XXX: make code faster ? */
6746 if ((vtop
->r
& (VT_SYM
|VT_CONST
)) == (VT_SYM
|VT_CONST
) &&
6747 vtop
->sym
->v
>= SYM_FIRST_ANOM
&&
6748 /* XXX This rejects compound literals like
6749 '(void *){ptr}'. The problem is that '&sym' is
6750 represented the same way, which would be ruled out
6751 by the SYM_FIRST_ANOM check above, but also '"string"'
6752 in 'char *p = "string"' is represented the same
6753 with the type being VT_PTR and the symbol being an
6754 anonymous one. That is, there's no difference in vtop
6755 between '(void *){x}' and '&(void *){x}'. Ignore
6756 pointer typed entities here. Hopefully no real code
6757 will every use compound literals with scalar type. */
6758 (vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
6759 /* These come from compound literals, memcpy stuff over. */
6763 esym
= elfsym(vtop
->sym
);
6764 ssec
= tcc_state
->sections
[esym
->st_shndx
];
6765 memmove (ptr
, ssec
->data
+ esym
->st_value
, size
);
6767 /* We need to copy over all memory contents, and that
6768 includes relocations. Use the fact that relocs are
6769 created it order, so look from the end of relocs
6770 until we hit one before the copied region. */
6771 int num_relocs
= ssec
->reloc
->data_offset
/ sizeof(*rel
);
6772 rel
= (ElfW_Rel
*)(ssec
->reloc
->data
+ ssec
->reloc
->data_offset
);
6773 while (num_relocs
--) {
6775 if (rel
->r_offset
>= esym
->st_value
+ size
)
6777 if (rel
->r_offset
< esym
->st_value
)
6779 /* Note: if the same fields are initialized multiple
6780 times (possible with designators) then we possibly
6781 add multiple relocations for the same offset here.
6782 That would lead to wrong code, the last reloc needs
6783 to win. We clean this up later after the whole
6784 initializer is parsed. */
6785 put_elf_reloca(symtab_section
, sec
,
6786 c
+ rel
->r_offset
- esym
->st_value
,
6787 ELFW(R_TYPE
)(rel
->r_info
),
6788 ELFW(R_SYM
)(rel
->r_info
),
6798 if (type
->t
& VT_BITFIELD
) {
6799 int bit_pos
, bit_size
, bits
, n
;
6800 unsigned char *p
, v
, m
;
6801 bit_pos
= BIT_POS(vtop
->type
.t
);
6802 bit_size
= BIT_SIZE(vtop
->type
.t
);
6803 p
= (unsigned char*)ptr
+ (bit_pos
>> 3);
6804 bit_pos
&= 7, bits
= 0;
6809 v
= vtop
->c
.i
>> bits
<< bit_pos
;
6810 m
= ((1 << n
) - 1) << bit_pos
;
6811 *p
= (*p
& ~m
) | (v
& m
);
6812 bits
+= n
, bit_size
-= n
, bit_pos
= 0, ++p
;
6816 /* XXX: when cross-compiling we assume that each type has the
6817 same representation on host and target, which is likely to
6818 be wrong in the case of long double */
6820 vtop
->c
.i
= vtop
->c
.i
!= 0;
6822 *(char *)ptr
|= vtop
->c
.i
;
6825 *(short *)ptr
|= vtop
->c
.i
;
6828 *(float*)ptr
= vtop
->c
.f
;
6831 *(double *)ptr
= vtop
->c
.d
;
6834 #if defined TCC_IS_NATIVE_387
6835 if (sizeof (long double) >= 10) /* zero pad ten-byte LD */
6836 memcpy(ptr
, &vtop
->c
.ld
, 10);
6838 else if (sizeof (long double) == sizeof (double))
6839 __asm__("fldl %1\nfstpt %0\n" : "=m" (*ptr
) : "m" (vtop
->c
.ld
));
6841 else if (vtop
->c
.ld
== 0.0)
6845 if (sizeof(long double) == LDOUBLE_SIZE
)
6846 *(long double*)ptr
= vtop
->c
.ld
;
6847 else if (sizeof(double) == LDOUBLE_SIZE
)
6848 *(double *)ptr
= (double)vtop
->c
.ld
;
6850 tcc_error("can't cross compile long double constants");
6854 *(long long *)ptr
|= vtop
->c
.i
;
6861 addr_t val
= vtop
->c
.i
;
6863 if (vtop
->r
& VT_SYM
)
6864 greloca(sec
, vtop
->sym
, c
, R_DATA_PTR
, val
);
6866 *(addr_t
*)ptr
|= val
;
6868 if (vtop
->r
& VT_SYM
)
6869 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
6870 *(addr_t
*)ptr
|= val
;
6876 int val
= vtop
->c
.i
;
6878 if (vtop
->r
& VT_SYM
)
6879 greloca(sec
, vtop
->sym
, c
, R_DATA_PTR
, val
);
6883 if (vtop
->r
& VT_SYM
)
6884 greloc(sec
, vtop
->sym
, c
, R_DATA_PTR
);
6893 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
6900 /* 't' contains the type and storage info. 'c' is the offset of the
6901 object in section 'sec'. If 'sec' is NULL, it means stack based
6902 allocation. 'flags & DIF_FIRST' is true if array '{' must be read (multi
6903 dimension implicit array init handling). 'flags & DIF_SIZE_ONLY' is true if
6904 size only evaluation is wanted (only for arrays). */
6905 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
6908 int len
, n
, no_oblock
, nb
, i
;
6914 if (!(flags
& DIF_HAVE_ELEM
) && tok
!= '{' &&
6915 /* In case of strings we have special handling for arrays, so
6916 don't consume them as initializer value (which would commit them
6917 to some anonymous symbol). */
6918 tok
!= TOK_LSTR
&& tok
!= TOK_STR
&&
6919 !(flags
& DIF_SIZE_ONLY
)) {
6920 parse_init_elem(!sec
? EXPR_ANY
: EXPR_CONST
);
6921 flags
|= DIF_HAVE_ELEM
;
6924 if ((flags
& DIF_HAVE_ELEM
) &&
6925 !(type
->t
& VT_ARRAY
) &&
6926 /* Use i_c_parameter_t, to strip toplevel qualifiers.
6927 The source type might have VT_CONSTANT set, which is
6928 of course assignable to non-const elements. */
6929 is_compatible_unqualified_types(type
, &vtop
->type
)) {
6930 init_putv(type
, sec
, c
);
6931 } else if (type
->t
& VT_ARRAY
) {
6934 t1
= pointed_type(type
);
6935 size1
= type_size(t1
, &align1
);
6938 if (((flags
& DIF_FIRST
) && tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
6941 tcc_error("character array initializer must be a literal,"
6942 " optionally enclosed in braces");
6947 /* only parse strings here if correct type (otherwise: handle
6948 them as ((w)char *) expressions */
6949 if ((tok
== TOK_LSTR
&&
6950 #ifdef TCC_TARGET_PE
6951 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
6953 (t1
->t
& VT_BTYPE
) == VT_INT
6955 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
6957 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
6960 /* compute maximum number of chars wanted */
6962 cstr_len
= tokc
.str
.size
;
6964 cstr_len
= tokc
.str
.size
/ sizeof(nwchar_t
);
6967 if (n
>= 0 && nb
> (n
- len
))
6969 if (!(flags
& DIF_SIZE_ONLY
)) {
6971 tcc_warning("initializer-string for array is too long");
6972 /* in order to go faster for common case (char
6973 string in global variable, we handle it
6975 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
6977 memcpy(sec
->data
+ c
+ len
, tokc
.str
.data
, nb
);
6981 ch
= ((unsigned char *)tokc
.str
.data
)[i
];
6983 ch
= ((nwchar_t
*)tokc
.str
.data
)[i
];
6985 init_putv(t1
, sec
, c
+ (len
+ i
) * size1
);
6992 /* only add trailing zero if enough storage (no
6993 warning in this case since it is standard) */
6994 if (n
< 0 || len
< n
) {
6995 if (!(flags
& DIF_SIZE_ONLY
)) {
6997 init_putv(t1
, sec
, c
+ (len
* size1
));
7008 while (tok
!= '}' || (flags
& DIF_HAVE_ELEM
)) {
7009 len
= decl_designator(type
, sec
, c
, &f
, flags
, len
);
7010 flags
&= ~DIF_HAVE_ELEM
;
7011 if (type
->t
& VT_ARRAY
) {
7013 /* special test for multi dimensional arrays (may not
7014 be strictly correct if designators are used at the
7016 if (no_oblock
&& len
>= n
*size1
)
7019 if (s
->type
.t
== VT_UNION
)
7023 if (no_oblock
&& f
== NULL
)
7032 /* put zeros at the end */
7033 if (!(flags
& DIF_SIZE_ONLY
) && len
< n
*size1
)
7034 init_putz(sec
, c
+ len
, n
*size1
- len
);
7037 /* patch type size if needed, which happens only for array types */
7039 s
->c
= size1
== 1 ? len
: ((len
+ size1
- 1)/size1
);
7040 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
7043 if ((flags
& DIF_FIRST
) || tok
== '{') {
7051 } else if (tok
== '{') {
7052 if (flags
& DIF_HAVE_ELEM
)
7055 decl_initializer(type
, sec
, c
, flags
& ~DIF_HAVE_ELEM
);
7057 } else if ((flags
& DIF_SIZE_ONLY
)) {
7058 /* If we supported only ISO C we wouldn't have to accept calling
7059 this on anything than an array if DIF_SIZE_ONLY (and even then
7060 only on the outermost level, so no recursion would be needed),
7061 because initializing a flex array member isn't supported.
7062 But GNU C supports it, so we need to recurse even into
7063 subfields of structs and arrays when DIF_SIZE_ONLY is set. */
7064 /* just skip expression */
7065 skip_or_save_block(NULL
);
7067 if (!(flags
& DIF_HAVE_ELEM
)) {
7068 /* This should happen only when we haven't parsed
7069 the init element above for fear of committing a
7070 string constant to memory too early. */
7071 if (tok
!= TOK_STR
&& tok
!= TOK_LSTR
)
7072 expect("string constant");
7073 parse_init_elem(!sec
? EXPR_ANY
: EXPR_CONST
);
7075 init_putv(type
, sec
, c
);
7079 /* parse an initializer for type 't' if 'has_init' is non zero, and
7080 allocate space in local or global data space ('r' is either
7081 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
7082 variable 'v' of scope 'scope' is declared before initializers
7083 are parsed. If 'v' is zero, then a reference to the new object
7084 is put in the value stack. If 'has_init' is 2, a special parsing
7085 is done to handle string constants. */
7086 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
7087 int has_init
, int v
, int scope
)
7089 int size
, align
, addr
;
7090 TokenString
*init_str
= NULL
;
7093 Sym
*flexible_array
;
7095 int saved_nocode_wanted
= nocode_wanted
;
7096 #ifdef CONFIG_TCC_BCHECK
7100 /* Always allocate static or global variables */
7101 if (v
&& (r
& VT_VALMASK
) == VT_CONST
)
7102 nocode_wanted
|= 0x80000000;
7104 #ifdef CONFIG_TCC_BCHECK
7105 bcheck
= tcc_state
->do_bounds_check
&& !NODATA_WANTED
;
7108 flexible_array
= NULL
;
7109 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
7110 Sym
*field
= type
->ref
->next
;
7113 field
= field
->next
;
7114 if (field
->type
.t
& VT_ARRAY
&& field
->type
.ref
->c
< 0)
7115 flexible_array
= field
;
7119 size
= type_size(type
, &align
);
7120 /* If unknown size, we must evaluate it before
7121 evaluating initializers because
7122 initializers can generate global data too
7123 (e.g. string pointers or ISOC99 compound
7124 literals). It also simplifies local
7125 initializers handling */
7126 if (size
< 0 || (flexible_array
&& has_init
)) {
7128 tcc_error("unknown type size");
7129 /* get all init string */
7130 if (has_init
== 2) {
7131 init_str
= tok_str_alloc();
7132 /* only get strings */
7133 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
7134 tok_str_add_tok(init_str
);
7137 tok_str_add(init_str
, -1);
7138 tok_str_add(init_str
, 0);
7140 skip_or_save_block(&init_str
);
7145 begin_macro(init_str
, 1);
7147 decl_initializer(type
, NULL
, 0, DIF_FIRST
| DIF_SIZE_ONLY
);
7148 /* prepare second initializer parsing */
7149 macro_ptr
= init_str
->str
;
7152 /* if still unknown size, error */
7153 size
= type_size(type
, &align
);
7155 tcc_error("unknown type size");
7157 /* If there's a flex member and it was used in the initializer
7159 if (flexible_array
&&
7160 flexible_array
->type
.ref
->c
> 0)
7161 size
+= flexible_array
->type
.ref
->c
7162 * pointed_size(&flexible_array
->type
);
7163 /* take into account specified alignment if bigger */
7164 if (ad
->a
.aligned
) {
7165 int speca
= 1 << (ad
->a
.aligned
- 1);
7168 } else if (ad
->a
.packed
) {
7172 if (!v
&& NODATA_WANTED
)
7173 size
= 0, align
= 1;
7175 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
7177 #ifdef CONFIG_TCC_BCHECK
7178 if (bcheck
&& (type
->t
& VT_ARRAY
)) {
7182 loc
= (loc
- size
) & -align
;
7184 #ifdef CONFIG_TCC_BCHECK
7185 /* handles bounds */
7186 /* XXX: currently, since we do only one pass, we cannot track
7187 '&' operators, so we add only arrays */
7188 if (bcheck
&& (type
->t
& VT_ARRAY
)) {
7190 /* add padding between regions */
7192 /* then add local bound info */
7193 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(addr_t
));
7194 bounds_ptr
[0] = addr
;
7195 bounds_ptr
[1] = size
;
7199 /* local variable */
7200 #ifdef CONFIG_TCC_ASM
7201 if (ad
->asm_label
) {
7202 int reg
= asm_parse_regvar(ad
->asm_label
);
7204 r
= (r
& ~VT_VALMASK
) | reg
;
7207 sym
= sym_push(v
, type
, r
, addr
);
7208 if (ad
->cleanup_func
) {
7209 Sym
*cls
= sym_push2(&all_cleanups
, SYM_FIELD
| ++ncleanups
, 0, 0);
7210 cls
->prev_tok
= sym
;
7211 cls
->next
= ad
->cleanup_func
;
7212 cls
->ncl
= current_cleanups
;
7213 current_cleanups
= cls
;
7218 /* push local reference */
7219 vset(type
, r
, addr
);
7222 if (v
&& scope
== VT_CONST
) {
7223 /* see if the symbol was already defined */
7226 patch_storage(sym
, ad
, type
);
7227 /* we accept several definitions of the same global variable. */
7228 if (!has_init
&& sym
->c
&& elfsym(sym
)->st_shndx
!= SHN_UNDEF
)
7233 /* allocate symbol in corresponding section */
7238 else if (tcc_state
->nocommon
)
7243 addr
= section_add(sec
, size
, align
);
7244 #ifdef CONFIG_TCC_BCHECK
7245 /* add padding if bound check */
7247 section_add(sec
, 1, 1);
7250 addr
= align
; /* SHN_COMMON is special, symbol value is align */
7251 sec
= common_section
;
7256 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
7257 patch_storage(sym
, ad
, NULL
);
7259 /* Local statics have a scope until now (for
7260 warnings), remove it here. */
7262 /* update symbol definition */
7263 put_extern_sym(sym
, sec
, addr
, size
);
7265 /* push global reference */
7266 vpush_ref(type
, sec
, addr
, size
);
7271 #ifdef CONFIG_TCC_BCHECK
7272 /* handles bounds now because the symbol must be defined
7273 before for the relocation */
7277 greloca(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_PTR
, 0);
7278 /* then add global bound info */
7279 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(addr_t
));
7280 bounds_ptr
[0] = 0; /* relocated */
7281 bounds_ptr
[1] = size
;
7286 if (type
->t
& VT_VLA
) {
7292 /* save current stack pointer */
7293 if (vlas_in_scope
== 0) {
7294 if (vla_sp_root_loc
== -1)
7295 vla_sp_root_loc
= (loc
-= PTR_SIZE
);
7296 gen_vla_sp_save(vla_sp_root_loc
);
7299 vla_runtime_type_size(type
, &a
);
7300 gen_vla_alloc(type
, a
);
7301 #if defined TCC_TARGET_PE && defined TCC_TARGET_X86_64
7302 /* on _WIN64, because of the function args scratch area, the
7303 result of alloca differs from RSP and is returned in RAX. */
7304 gen_vla_result(addr
), addr
= (loc
-= PTR_SIZE
);
7306 gen_vla_sp_save(addr
);
7310 } else if (has_init
) {
7311 size_t oldreloc_offset
= 0;
7312 if (sec
&& sec
->reloc
)
7313 oldreloc_offset
= sec
->reloc
->data_offset
;
7314 decl_initializer(type
, sec
, addr
, DIF_FIRST
);
7315 if (sec
&& sec
->reloc
)
7316 squeeze_multi_relocs(sec
, oldreloc_offset
);
7317 /* patch flexible array member size back to -1, */
7318 /* for possible subsequent similar declarations */
7320 flexible_array
->type
.ref
->c
= -1;
7324 /* restore parse state if needed */
7330 nocode_wanted
= saved_nocode_wanted
;
7333 /* parse a function defined by symbol 'sym' and generate its code in
7334 'cur_text_section' */
7335 static void gen_function(Sym
*sym
)
7338 ind
= cur_text_section
->data_offset
;
7339 if (sym
->a
.aligned
) {
7340 size_t newoff
= section_add(cur_text_section
, 0,
7341 1 << (sym
->a
.aligned
- 1));
7342 gen_fill_nops(newoff
- ind
);
7344 /* NOTE: we patch the symbol size later */
7345 put_extern_sym(sym
, cur_text_section
, ind
, 0);
7346 funcname
= get_tok_str(sym
->v
, NULL
);
7348 /* Initialize VLA state */
7350 vla_sp_root_loc
= -1;
7351 /* put debug symbol */
7352 tcc_debug_funcstart(tcc_state
, sym
);
7353 /* push a dummy symbol to enable local sym storage */
7354 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
7355 local_scope
= 1; /* for function parameters */
7356 gfunc_prolog(&sym
->type
);
7357 reset_local_scope();
7359 clear_temp_local_var_list();
7360 block(NULL
, NULL
, 0);
7361 if (!(nocode_wanted
& 0x20000000)
7362 && ((func_vt
.t
& VT_BTYPE
) == VT_INT
)
7363 && !strcmp (funcname
, "main"))
7367 gen_assign_cast(&func_vt
);
7368 gfunc_return(&func_vt
);
7373 cur_text_section
->data_offset
= ind
;
7374 label_pop(&global_label_stack
, NULL
, 0);
7375 /* reset local stack */
7376 reset_local_scope();
7377 sym_pop(&local_stack
, NULL
, 0);
7378 /* end of function */
7379 /* patch symbol size */
7380 elfsym(sym
)->st_size
= ind
- func_ind
;
7381 tcc_debug_funcend(tcc_state
, ind
- func_ind
);
7382 /* It's better to crash than to generate wrong code */
7383 cur_text_section
= NULL
;
7384 funcname
= ""; /* for safety */
7385 func_vt
.t
= VT_VOID
; /* for safety */
7386 func_var
= 0; /* for safety */
7387 ind
= 0; /* for safety */
7388 nocode_wanted
= 0x80000000;
7392 static void gen_inline_functions(TCCState
*s
)
7395 int inline_generated
, i
, ln
;
7396 struct InlineFunc
*fn
;
7398 ln
= file
->line_num
;
7399 /* iterate while inline function are referenced */
7401 inline_generated
= 0;
7402 for (i
= 0; i
< s
->nb_inline_fns
; ++i
) {
7403 fn
= s
->inline_fns
[i
];
7405 if (sym
&& sym
->c
) {
7406 /* the function was used: generate its code and
7407 convert it to a normal function */
7410 pstrcpy(file
->filename
, sizeof file
->filename
, fn
->filename
);
7411 sym
->type
.t
&= ~VT_INLINE
;
7413 begin_macro(fn
->func_str
, 1);
7415 cur_text_section
= text_section
;
7419 inline_generated
= 1;
7422 } while (inline_generated
);
7423 file
->line_num
= ln
;
7426 ST_FUNC
void free_inline_functions(TCCState
*s
)
7429 /* free tokens of unused inline functions */
7430 for (i
= 0; i
< s
->nb_inline_fns
; ++i
) {
7431 struct InlineFunc
*fn
= s
->inline_fns
[i
];
7433 tok_str_free(fn
->func_str
);
7435 dynarray_reset(&s
->inline_fns
, &s
->nb_inline_fns
);
7438 /* 'l' is VT_LOCAL or VT_CONST to define default storage type, or VT_CMP
7439 if parsing old style parameter decl list (and FUNC_SYM is set then) */
7440 static int decl0(int l
, int is_for_loop_init
, Sym
*func_sym
)
7445 AttributeDef ad
, adbase
;
7448 if (tok
== TOK_STATIC_ASSERT
) {
7456 tcc_error("%s", get_tok_str(tok
, &tokc
));
7462 if (!parse_btype(&btype
, &adbase
)) {
7463 if (is_for_loop_init
)
7465 /* skip redundant ';' if not in old parameter decl scope */
7466 if (tok
== ';' && l
!= VT_CMP
) {
7472 if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
7473 /* global asm block */
7477 if (tok
>= TOK_UIDENT
) {
7478 /* special test for old K&R protos without explicit int
7479 type. Only accepted when defining global data */
7483 expect("declaration");
7488 if ((btype
.t
& VT_BTYPE
) == VT_STRUCT
) {
7489 int v
= btype
.ref
->v
;
7490 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) >= SYM_FIRST_ANOM
)
7491 tcc_warning("unnamed struct/union that defines no instances");
7495 if (IS_ENUM(btype
.t
)) {
7500 while (1) { /* iterate thru each declaration */
7502 /* If the base type itself was an array type of unspecified
7503 size (like in 'typedef int arr[]; arr x = {1};') then
7504 we will overwrite the unknown size by the real one for
7505 this decl. We need to unshare the ref symbol holding
7507 if ((type
.t
& VT_ARRAY
) && type
.ref
->c
< 0) {
7508 type
.ref
= sym_push(SYM_FIELD
, &type
.ref
->type
, 0, type
.ref
->c
);
7511 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
7515 type_to_str(buf
, sizeof(buf
), &type
, get_tok_str(v
, NULL
));
7516 printf("type = '%s'\n", buf
);
7519 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
7520 if ((type
.t
& VT_STATIC
) && (l
== VT_LOCAL
)) {
7521 tcc_error("function without file scope cannot be static");
7523 /* if old style function prototype, we accept a
7526 if (sym
->f
.func_type
== FUNC_OLD
&& l
== VT_CONST
)
7527 decl0(VT_CMP
, 0, sym
);
7530 if (gnu_ext
&& (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
7531 ad
.asm_label
= asm_label_instr();
7532 /* parse one last attribute list, after asm label */
7533 parse_attribute(&ad
);
7538 #ifdef TCC_TARGET_PE
7539 if (ad
.a
.dllimport
|| ad
.a
.dllexport
) {
7540 if (type
.t
& (VT_STATIC
|VT_TYPEDEF
))
7541 tcc_error("cannot have dll linkage with static or typedef");
7542 if (ad
.a
.dllimport
) {
7543 if ((type
.t
& VT_BTYPE
) == VT_FUNC
)
7546 type
.t
|= VT_EXTERN
;
7552 tcc_error("cannot use local functions");
7553 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
7554 expect("function definition");
7556 /* reject abstract declarators in function definition
7557 make old style params without decl have int type */
7559 while ((sym
= sym
->next
) != NULL
) {
7560 if (!(sym
->v
& ~SYM_FIELD
))
7561 expect("identifier");
7562 if (sym
->type
.t
== VT_VOID
)
7563 sym
->type
= int_type
;
7566 /* XXX: cannot do better now: convert extern line to static inline */
7567 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
7568 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
7570 /* put function symbol */
7571 sym
= external_sym(v
, &type
, 0, &ad
);
7573 /* static inline functions are just recorded as a kind
7574 of macro. Their code will be emitted at the end of
7575 the compilation unit only if they are used */
7576 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
7577 (VT_INLINE
| VT_STATIC
)) {
7578 struct InlineFunc
*fn
;
7579 const char *filename
;
7581 filename
= file
? file
->filename
: "";
7582 fn
= tcc_malloc(sizeof *fn
+ strlen(filename
));
7583 strcpy(fn
->filename
, filename
);
7585 skip_or_save_block(&fn
->func_str
);
7586 dynarray_add(&tcc_state
->inline_fns
,
7587 &tcc_state
->nb_inline_fns
, fn
);
7589 /* compute text section */
7590 cur_text_section
= ad
.section
;
7591 if (!cur_text_section
)
7592 cur_text_section
= text_section
;
7598 /* find parameter in function parameter list */
7599 for (sym
= func_sym
->next
; sym
; sym
= sym
->next
)
7600 if ((sym
->v
& ~SYM_FIELD
) == v
)
7602 tcc_error("declaration for parameter '%s' but no such parameter",
7603 get_tok_str(v
, NULL
));
7605 if (type
.t
& VT_STORAGE
) /* 'register' is okay */
7606 tcc_error("storage class specified for '%s'",
7607 get_tok_str(v
, NULL
));
7608 if (sym
->type
.t
!= VT_VOID
)
7609 tcc_error("redefinition of parameter '%s'",
7610 get_tok_str(v
, NULL
));
7611 convert_parameter_type(&type
);
7613 } else if (type
.t
& VT_TYPEDEF
) {
7614 /* save typedefed type */
7615 /* XXX: test storage specifiers ? */
7617 if (sym
&& sym
->sym_scope
== local_scope
) {
7618 if (!is_compatible_types(&sym
->type
, &type
)
7619 || !(sym
->type
.t
& VT_TYPEDEF
))
7620 tcc_error("incompatible redefinition of '%s'",
7621 get_tok_str(v
, NULL
));
7624 sym
= sym_push(v
, &type
, 0, 0);
7628 } else if ((type
.t
& VT_BTYPE
) == VT_VOID
7629 && !(type
.t
& VT_EXTERN
)) {
7630 tcc_error("declaration of void object");
7633 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
7634 /* external function definition */
7635 /* specific case for func_call attribute */
7637 } else if (!(type
.t
& VT_ARRAY
)) {
7638 /* not lvalue if array */
7639 r
|= lvalue_type(type
.t
);
7641 has_init
= (tok
== '=');
7642 if (has_init
&& (type
.t
& VT_VLA
))
7643 tcc_error("variable length array cannot be initialized");
7644 if (((type
.t
& VT_EXTERN
) && (!has_init
|| l
!= VT_CONST
)) ||
7645 ((type
.t
& VT_BTYPE
) == VT_FUNC
) ||
7646 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
7647 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
7648 /* external variable or function */
7649 /* NOTE: as GCC, uninitialized global static
7650 arrays of null size are considered as
7652 type
.t
|= VT_EXTERN
;
7653 sym
= external_sym(v
, &type
, r
, &ad
);
7654 if (ad
.alias_target
) {
7657 alias_target
= sym_find(ad
.alias_target
);
7658 esym
= elfsym(alias_target
);
7660 tcc_error("unsupported forward __alias__ attribute");
7661 /* Local statics have a scope until now (for
7662 warnings), remove it here. */
7664 put_extern_sym2(sym
, esym
->st_shndx
, esym
->st_value
, esym
->st_size
, 0);
7667 if (type
.t
& VT_STATIC
)
7673 else if (l
== VT_CONST
)
7674 /* uninitialized global variables may be overridden */
7675 type
.t
|= VT_EXTERN
;
7676 decl_initializer_alloc(&type
, &ad
, r
, has_init
, v
, l
);
7680 if (is_for_loop_init
)
7692 static void decl(int l
)
7697 /* ------------------------------------------------------------------------- */